Debian Codegraph @

common/hv_nio.c

raw
1 /*
2 * Cisco router simulation platform.
3 * Copyright (c) 2006 Christophe Fillot ([email protected])
4 *
5 * Hypervisor NIO routines.
6 */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <string.h>
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <signal.h>
15 #include <fcntl.h>
16 #include <errno.h>
17 #include <assert.h>
18 #include <stdarg.h>
19 #include <sys/ioctl.h>
20 #include <sys/types.h>
21 #include <sys/socket.h>
22 #include <arpa/inet.h>
23 #include <pthread.h>
24
25 #include "utils.h"
26 #include "net.h"
27 #include "atm.h"
28 #include "frame_relay.h"
29 #include "crc.h"
30 #include "net_io.h"
31 #include "net_io_bridge.h"
32 #include "net_io_filter.h"
33 #ifdef GEN_ETH
34 #include "gen_eth.h"
35 #endif
36 #include "registry.h"
37 #include "hypervisor.h"
38
39 /*
40 * Create a UDP NIO
41 *
42 * Parameters: <nio_name> <local_port> <remote_host> <remote_port>
43 */
44 static int cmd_create_udp(hypervisor_conn_t *conn,int argc,char *argv[])
45 {
46 netio_desc_t *nio;
47
48 nio = netio_desc_create_udp(argv[0],atoi(argv[1]),argv[2],atoi(argv[3]));
49
50 if (!nio) {
51 hypervisor_send_reply(conn,HSC_ERR_CREATE,1,
52 "unable to create UDP NIO");
53 return(-1);
54 }
55
56 netio_release(argv[0]);
57 hypervisor_send_reply(conn,HSC_INFO_OK,1,"NIO '%s' created",argv[0]);
58 return(0);
59 }
60
61 /*
62 * Create a Auto UDP NIO
63 *
64 * Parameters: <nio_name> <local_addr> <local_port_start> <local_port_end>
65 */
66 static int cmd_create_udp_auto(hypervisor_conn_t *conn,int argc,char *argv[])
67 {
68 netio_desc_t *nio;
69 int local_port;
70
71 nio = netio_desc_create_udp_auto(argv[0],argv[1],atoi(argv[2]),atoi(argv[3]));
72
73 if (!nio) {
74 hypervisor_send_reply(conn,HSC_ERR_CREATE,1,
75 "unable to create UDP Auto NIO");
76 return(-1);
77 }
78
79 local_port = netio_udp_auto_get_local_port(nio);
80
81 netio_release(argv[0]);
82 hypervisor_send_reply(conn,HSC_INFO_OK,1,"%d",local_port);
83 return(0);
84 }
85
86 /*
87 * Connect an UDP Auto NIO to a remote host/port.
88 *
89 * Parameters: <nio_name> <remote_host> <remote_port>
90 */
91 static int cmd_connect_udp_auto(hypervisor_conn_t *conn,int argc,char *argv[])
92 {
93 netio_desc_t *nio;
94 int res;
95
96 if (!(nio = hypervisor_find_object(conn,argv[0],OBJ_TYPE_NIO)))
97 return(-1);
98
99 res = netio_udp_auto_connect(nio,argv[1],atoi(argv[2]));
100 netio_release(argv[0]);
101
102 if (res == 0) {
103 hypervisor_send_reply(conn,HSC_INFO_OK,1,"NIO '%s' connected",argv[0]);
104 return(0);
105 } else {
106 hypervisor_send_reply(conn,HSC_ERR_CREATE,1,"unable to connect NIO");
107 return(-1);
108 }
109 }
110
111 /*
112 * Create a UNIX NIO
113 *
114 * Parameters: <nio_name> <local_file> <remote_file>
115 */
116 static int cmd_create_unix(hypervisor_conn_t *conn,int argc,char *argv[])
117 {
118 netio_desc_t *nio;
119
120 nio = netio_desc_create_unix(argv[0],argv[1],argv[2]);
121
122 if (!nio) {
123 hypervisor_send_reply(conn,HSC_ERR_CREATE,1,"unable to create UNIX NIO");
124 return(-1);
125 }
126
127 netio_release(argv[0]);
128 hypervisor_send_reply(conn,HSC_INFO_OK,1,"NIO '%s' created",argv[0]);
129 return(0);
130 }
131
132 /*
133 * Create a VDE NIO
134 *
135 * Parameters: <nio_name> <control_file> <local_file>
136 */
137 static int cmd_create_vde(hypervisor_conn_t *conn,int argc,char *argv[])
138 {
139 netio_desc_t *nio;
140
141 nio = netio_desc_create_vde(argv[0],argv[1],argv[2]);
142
143 if (!nio) {
144 hypervisor_send_reply(conn,HSC_ERR_CREATE,1,"unable to create VDE NIO");
145 return(-1);
146 }
147
148 netio_release(argv[0]);
149 hypervisor_send_reply(conn,HSC_INFO_OK,1,"NIO '%s' created",argv[0]);
150 return(0);
151 }
152
153 /*
154 * Create a TAP NIO
155 *
156 * Parameters: <nio_name> <tap_device>
157 */
158 static int cmd_create_tap(hypervisor_conn_t *conn,int argc,char *argv[])
159 {
160 netio_desc_t *nio;
161
162 nio = netio_desc_create_tap(argv[0],argv[1]);
163
164 if (!nio) {
165 hypervisor_send_reply(conn,HSC_ERR_CREATE,1,"unable to create TAP NIO");
166 return(-1);
167 }
168
169 netio_release(argv[0]);
170 hypervisor_send_reply(conn,HSC_INFO_OK,1,"NIO '%s' created",argv[0]);
171 return(0);
172 }
173
174 /*
175 * Create a generic ethernet PCAP NIO
176 *
177 * Parameters: <nio_name> <eth_device>
178 */
179 #ifdef GEN_ETH
180 static int cmd_create_gen_eth(hypervisor_conn_t *conn,int argc,char *argv[])
181 {
182 netio_desc_t *nio;
183
184 nio = netio_desc_create_geneth(argv[0],argv[1]);
185
186 if (!nio) {
187 hypervisor_send_reply(conn,HSC_ERR_CREATE,1,
188 "unable to create generic ethernet NIO");
189 return(-1);
190 }
191
192 netio_release(argv[0]);
193 hypervisor_send_reply(conn,HSC_INFO_OK,1,"NIO '%s' created",argv[0]);
194 return(0);
195 }
196 #endif
197
198 /*
199 * Create a linux raw ethernet NIO
200 *
201 * Parameters: <nio_name> <eth_device>
202 */
203 #ifdef LINUX_ETH
204 static int cmd_create_linux_eth(hypervisor_conn_t *conn,int argc,char *argv[])
205 {
206 netio_desc_t *nio;
207
208 nio = netio_desc_create_lnxeth(argv[0],argv[1]);
209
210 if (!nio) {
211 hypervisor_send_reply(conn,HSC_ERR_CREATE,1,
212 "unable to create Linux raw ethernet NIO");
213 return(-1);
214 }
215
216 netio_release(argv[0]);
217 hypervisor_send_reply(conn,HSC_INFO_OK,1,"NIO '%s' created",argv[0]);
218 return(0);
219 }
220 #endif
221
222 /*
223 * Create a Null NIO
224 *
225 * Parameters: <nio_name>
226 */
227 static int cmd_create_null(hypervisor_conn_t *conn,int argc,char *argv[])
228 {
229 netio_desc_t *nio;
230
231 nio = netio_desc_create_null(argv[0]);
232
233 if (!nio) {
234 hypervisor_send_reply(conn,HSC_ERR_CREATE,1,
235 "unable to create Null NIO");
236 return(-1);
237 }
238
239 netio_release(argv[0]);
240 hypervisor_send_reply(conn,HSC_INFO_OK,1,"NIO '%s' created",argv[0]);
241 return(0);
242 }
243
244 /*
245 * Create a FIFO NIO
246 *
247 * Parameters: <nio_name>
248 */
249 static int cmd_create_fifo(hypervisor_conn_t *conn,int argc,char *argv[])
250 {
251 netio_desc_t *nio;
252
253 nio = netio_desc_create_fifo(argv[0]);
254
255 if (!nio) {
256 hypervisor_send_reply(conn,HSC_ERR_CREATE,1,
257 "unable to create FIFO NIO");
258 return(-1);
259 }
260
261 netio_release(argv[0]);
262 hypervisor_send_reply(conn,HSC_INFO_OK,1,"NIO '%s' created",argv[0]);
263 return(0);
264 }
265
266 /*
267 * Establish a cross-connect between 2 FIFO NIO
268 *
269 * Parameters: <nio_A_name> <nio_B_name>
270 */
271 static int cmd_crossconnect_fifo(hypervisor_conn_t *conn,int argc,char *argv[])
272 {
273 netio_desc_t *a,*b;
274
275 if (!(a = hypervisor_find_object(conn,argv[0],OBJ_TYPE_NIO)))
276 return(-1);
277
278 if (!(b = hypervisor_find_object(conn,argv[1],OBJ_TYPE_NIO))) {
279 netio_release(argv[0]);
280 return(-1);
281 }
282
283 netio_fifo_crossconnect(a,b);
284
285 netio_release(argv[0]);
286 netio_release(argv[1]);
287
288 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
289 return(0);
290 }
291
292 /* Rename a NIO */
293 static int cmd_rename(hypervisor_conn_t *conn,int argc,char *argv[])
294 {
295 netio_desc_t *nio;
296 char *newname;
297
298 if (!(nio = hypervisor_find_object(conn,argv[0],OBJ_TYPE_NIO)))
299 return(-1);
300
301 if (registry_exists(argv[1],OBJ_TYPE_NIO)) {
302 netio_release(argv[0]);
303 hypervisor_send_reply(conn,HSC_ERR_RENAME,1,
304 "unable to rename NIO '%s', '%s' already exists",
305 argv[0],argv[1]);
306 return(-1);
307 }
308
309 if(!(newname = strdup(argv[1]))) {
310 netio_release(argv[0]);
311 hypervisor_send_reply(conn,HSC_ERR_RENAME,1,
312 "unable to rename NIO '%s', out of memory",
313 argv[0]);
314 return(-1);
315 }
316
317 if (registry_rename(argv[0],newname,OBJ_TYPE_NIO)) {
318 free(newname);
319 netio_release(argv[0]);
320 hypervisor_send_reply(conn,HSC_ERR_RENAME,1,
321 "unable to rename NIO '%s'",
322 argv[0]);
323 return(-1);
324 }
325
326 free(nio->name);
327 nio->name = newname;
328
329 netio_release(argv[1]);
330 hypervisor_send_reply(conn,HSC_INFO_OK,1,"NIO '%s' renamed to '%s'",argv[0],argv[1]);
331 return(0);
332 }
333
334 /* Delete a NIO */
335 static int cmd_delete(hypervisor_conn_t *conn,int argc,char *argv[])
336 {
337 int res;
338
339 res = netio_delete(argv[0]);
340
341 if (res == 1) {
342 hypervisor_send_reply(conn,HSC_INFO_OK,1,"NIO '%s' deleted",argv[0]);
343 } else {
344 hypervisor_send_reply(conn,HSC_ERR_DELETE,1,
345 "unable to delete NIO '%s'",argv[0]);
346 }
347
348 return(res);
349 }
350
351 /*
352 * Enable/Disable debugging for an NIO
353 *
354 * Parameters: <nio_name> <debug_level>
355 */
356 static int cmd_set_debug(hypervisor_conn_t *conn,int argc,char *argv[])
357 {
358 netio_desc_t *nio;
359
360 if (!(nio = hypervisor_find_object(conn,argv[0],OBJ_TYPE_NIO)))
361 return(-1);
362
363 nio->debug = atoi(argv[1]);
364
365 netio_release(argv[0]);
366 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
367 return(0);
368 }
369
370 /* Bind a packet filter */
371 static int cmd_bind_filter(hypervisor_conn_t *conn,int argc,char *argv[])
372 {
373 netio_desc_t *nio;
374 int res;
375
376 if (!(nio = hypervisor_find_object(conn,argv[0],OBJ_TYPE_NIO)))
377 return(-1);
378
379 res = netio_filter_bind(nio,atoi(argv[1]),argv[2]);
380 netio_release(argv[0]);
381
382 if (!res) {
383 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
384 } else {
385 hypervisor_send_reply(conn,HSC_ERR_UNK_OBJ,1,
386 "Unknown filter %s",argv[2]);
387 }
388 return(0);
389 }
390
391 /* Unbind a packet filter */
392 static int cmd_unbind_filter(hypervisor_conn_t *conn,int argc,char *argv[])
393 {
394 netio_desc_t *nio;
395 int res;
396
397 if (!(nio = hypervisor_find_object(conn,argv[0],OBJ_TYPE_NIO)))
398 return(-1);
399
400 res = netio_filter_unbind(nio,atoi(argv[1]));
401 netio_release(argv[0]);
402
403 if (!res) {
404 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
405 } else {
406 hypervisor_send_reply(conn,HSC_ERR_UNK_OBJ,1,
407 "No filter previously defined");
408 }
409 return(0);
410 }
411
412
413 /* Setup a packet filter for a given NIO */
414 static int cmd_setup_filter(hypervisor_conn_t *conn,int argc,char *argv[])
415 {
416 netio_desc_t *nio;
417 int res;
418
419 if (!(nio = hypervisor_find_object(conn,argv[0],OBJ_TYPE_NIO)))
420 return(-1);
421
422 res = netio_filter_setup(nio,atoi(argv[1]),argc-2,&argv[2]);
423 netio_release(argv[0]);
424
425 if (!res) {
426 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
427 } else {
428 hypervisor_send_reply(conn,HSC_ERR_UNSPECIFIED,1,"Failed to setup filter");
429 }
430 return(0);
431 }
432
433 /* Get statistics of a NIO */
434 static int cmd_get_stats(hypervisor_conn_t *conn,int argc,char *argv[])
435 {
436 netio_desc_t *nio;
437 m_uint64_t spi,spo,sbi,sbo;
438
439 if (!(nio = hypervisor_find_object(conn,argv[0],OBJ_TYPE_NIO)))
440 return(-1);
441
442 spi = nio->stats_pkts_in;
443 spo = nio->stats_pkts_out;
444 sbi = nio->stats_bytes_in;
445 sbo = nio->stats_bytes_out;
446
447 netio_release(argv[0]);
448
449 hypervisor_send_reply(conn,HSC_INFO_OK,1,"%llu %llu %llu %llu",
450 spi,spo,sbi,sbo);
451 return(0);
452 }
453
454 /* Reset statistics of a NIO */
455 static int cmd_reset_stats(hypervisor_conn_t *conn,int argc,char *argv[])
456 {
457 netio_desc_t *nio;
458
459 if (!(nio = hypervisor_find_object(conn,argv[0],OBJ_TYPE_NIO)))
460 return(-1);
461
462 netio_reset_stats(nio);
463 netio_release(argv[0]);
464
465 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
466 return(0);
467 }
468
469 /* Set bandwidth constraint */
470 static int cmd_set_bandwidth(hypervisor_conn_t *conn,int argc,char *argv[])
471 {
472 netio_desc_t *nio;
473
474 if (!(nio = hypervisor_find_object(conn,argv[0],OBJ_TYPE_NIO)))
475 return(-1);
476
477 netio_set_bandwidth(nio,atoi(argv[1]));
478 netio_release(argv[0]);
479
480 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
481 return(0);
482 }
483
484 /* Show info about a NIO object */
485 static void cmd_show_nio_list(registry_entry_t *entry,void *opt,int *err)
486 {
487 hypervisor_conn_t *conn = opt;
488 hypervisor_send_reply(conn,HSC_INFO_MSG,0,"%s",entry->name);
489 }
490
491 /* NIO List */
492 static int cmd_nio_list(hypervisor_conn_t *conn,int argc,char *argv[])
493 {
494 int err = 0;
495 registry_foreach_type(OBJ_TYPE_NIO,cmd_show_nio_list,conn,&err);
496 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
497 return(0);
498 }
499
500 /* NIO commands */
501 static hypervisor_cmd_t nio_cmd_array[] = {
502 { "create_udp", 4, 4, cmd_create_udp, NULL },
503 { "create_udp_auto", 4, 4, cmd_create_udp_auto, NULL },
504 { "connect_udp_auto", 3, 3, cmd_connect_udp_auto, NULL },
505 { "create_unix", 3, 3, cmd_create_unix, NULL },
506 { "create_vde", 3, 3, cmd_create_vde, NULL },
507 { "create_tap", 2, 2, cmd_create_tap, NULL },
508 #ifdef GEN_ETH
509 { "create_gen_eth", 2, 2, cmd_create_gen_eth, NULL },
510 #endif
511 #ifdef LINUX_ETH
512 { "create_linux_eth", 2, 2, cmd_create_linux_eth, NULL },
513 #endif
514 { "create_null", 1, 1, cmd_create_null, NULL },
515 { "create_fifo", 1, 1, cmd_create_fifo, NULL },
516 { "crossconnect_fifo", 2, 2, cmd_crossconnect_fifo, NULL },
517 { "rename", 2, 2, cmd_rename, NULL },
518 { "delete", 1, 1, cmd_delete, NULL },
519 { "set_debug", 2, 2, cmd_set_debug, NULL },
520 { "bind_filter", 3, 3, cmd_bind_filter, NULL },
521 { "unbind_filter", 2, 2, cmd_unbind_filter, NULL },
522 { "setup_filter", 2, 10, cmd_setup_filter, NULL },
523 { "get_stats", 1, 1, cmd_get_stats },
524 { "reset_stats", 1, 1, cmd_reset_stats },
525 { "set_bandwidth", 2, 2, cmd_set_bandwidth },
526 { "list", 0, 0, cmd_nio_list, NULL },
527 { NULL, -1, -1, NULL, NULL },
528 };
529
530 /* Hypervisor NIO initialization */
531 int hypervisor_nio_init(void)
532 {
533 hypervisor_module_t *module;
534
535 module = hypervisor_register_module("nio",NULL);
536 assert(module != NULL);
537
538 hypervisor_register_cmd_array(module,nio_cmd_array);
539 return(0);
540 }