/[dynamips]/trunk/hv_nio.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /trunk/hv_nio.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 12 - (show annotations)
Sat Oct 6 16:45:40 2007 UTC (11 years, 7 months ago) by dpavlin
File MIME type: text/plain
File size: 9121 byte(s)
make working copy

1 /*
2 * Cisco router simulation platform.
3 * Copyright (c) 2006 Christophe Fillot (cf@utc.fr)
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 <sys/mman.h>
15 #include <signal.h>
16 #include <fcntl.h>
17 #include <errno.h>
18 #include <assert.h>
19 #include <stdarg.h>
20 #include <sys/ioctl.h>
21 #include <sys/types.h>
22 #include <sys/socket.h>
23 #include <arpa/inet.h>
24 #include <pthread.h>
25
26 #include "utils.h"
27 #include "net.h"
28 #include "atm.h"
29 #include "frame_relay.h"
30 #include "crc.h"
31 #include "net_io.h"
32 #include "net_io_bridge.h"
33 #include "net_io_filter.h"
34 #ifdef GEN_ETH
35 #include "gen_eth.h"
36 #endif
37 #include "registry.h"
38 #include "hypervisor.h"
39
40 /*
41 * Create a UDP NIO
42 *
43 * Parameters: <nio_name> <local_port> <remote_host> <remote_port>
44 */
45 static int cmd_create_udp(hypervisor_conn_t *conn,int argc,char *argv[])
46 {
47 netio_desc_t *nio;
48
49 nio = netio_desc_create_udp(argv[0],atoi(argv[1]),argv[2],atoi(argv[3]));
50
51 if (!nio) {
52 hypervisor_send_reply(conn,HSC_ERR_CREATE,1,
53 "unable to create UDP NIO");
54 return(-1);
55 }
56
57 netio_release(argv[0]);
58 hypervisor_send_reply(conn,HSC_INFO_OK,1,"NIO '%s' created",argv[0]);
59 return(0);
60 }
61
62 /*
63 * Create a UNIX NIO
64 *
65 * Parameters: <nio_name> <local_file> <remote_file>
66 */
67 static int cmd_create_unix(hypervisor_conn_t *conn,int argc,char *argv[])
68 {
69 netio_desc_t *nio;
70
71 nio = netio_desc_create_unix(argv[0],argv[1],argv[2]);
72
73 if (!nio) {
74 hypervisor_send_reply(conn,HSC_ERR_CREATE,1,"unable to create UNIX NIO");
75 return(-1);
76 }
77
78 netio_release(argv[0]);
79 hypervisor_send_reply(conn,HSC_INFO_OK,1,"NIO '%s' created",argv[0]);
80 return(0);
81 }
82
83 /*
84 * Create a VDE NIO
85 *
86 * Parameters: <nio_name> <control_file> <local_file>
87 */
88 static int cmd_create_vde(hypervisor_conn_t *conn,int argc,char *argv[])
89 {
90 netio_desc_t *nio;
91
92 nio = netio_desc_create_vde(argv[0],argv[1],argv[2]);
93
94 if (!nio) {
95 hypervisor_send_reply(conn,HSC_ERR_CREATE,1,"unable to create VDE NIO");
96 return(-1);
97 }
98
99 netio_release(argv[0]);
100 hypervisor_send_reply(conn,HSC_INFO_OK,1,"NIO '%s' created",argv[0]);
101 return(0);
102 }
103
104 /*
105 * Create a TAP NIO
106 *
107 * Parameters: <nio_name> <tap_device>
108 */
109 static int cmd_create_tap(hypervisor_conn_t *conn,int argc,char *argv[])
110 {
111 netio_desc_t *nio;
112
113 nio = netio_desc_create_tap(argv[0],argv[1]);
114
115 if (!nio) {
116 hypervisor_send_reply(conn,HSC_ERR_CREATE,1,"unable to create TAP NIO");
117 return(-1);
118 }
119
120 netio_release(argv[0]);
121 hypervisor_send_reply(conn,HSC_INFO_OK,1,"NIO '%s' created",argv[0]);
122 return(0);
123 }
124
125 /*
126 * Create a generic ethernet PCAP NIO
127 *
128 * Parameters: <nio_name> <eth_device>
129 */
130 #ifdef GEN_ETH
131 static int cmd_create_gen_eth(hypervisor_conn_t *conn,int argc,char *argv[])
132 {
133 netio_desc_t *nio;
134
135 nio = netio_desc_create_geneth(argv[0],argv[1]);
136
137 if (!nio) {
138 hypervisor_send_reply(conn,HSC_ERR_CREATE,1,
139 "unable to create generic ethernet NIO");
140 return(-1);
141 }
142
143 netio_release(argv[0]);
144 hypervisor_send_reply(conn,HSC_INFO_OK,1,"NIO '%s' created",argv[0]);
145 return(0);
146 }
147 #endif
148
149 /*
150 * Create a linux raw ethernet NIO
151 *
152 * Parameters: <nio_name> <eth_device>
153 */
154 #ifdef LINUX_ETH
155 static int cmd_create_linux_eth(hypervisor_conn_t *conn,int argc,char *argv[])
156 {
157 netio_desc_t *nio;
158
159 nio = netio_desc_create_lnxeth(argv[0],argv[1]);
160
161 if (!nio) {
162 hypervisor_send_reply(conn,HSC_ERR_CREATE,1,
163 "unable to create Linux raw ethernet NIO");
164 return(-1);
165 }
166
167 netio_release(argv[0]);
168 hypervisor_send_reply(conn,HSC_INFO_OK,1,"NIO '%s' created",argv[0]);
169 return(0);
170 }
171 #endif
172
173 /*
174 * Create a Null NIO
175 *
176 * Parameters: <nio_name>
177 */
178 static int cmd_create_null(hypervisor_conn_t *conn,int argc,char *argv[])
179 {
180 netio_desc_t *nio;
181
182 nio = netio_desc_create_null(argv[0]);
183
184 if (!nio) {
185 hypervisor_send_reply(conn,HSC_ERR_CREATE,1,
186 "unable to create Null NIO");
187 return(-1);
188 }
189
190 netio_release(argv[0]);
191 hypervisor_send_reply(conn,HSC_INFO_OK,1,"NIO '%s' created",argv[0]);
192 return(0);
193 }
194
195 /*
196 * Create a FIFO NIO
197 *
198 * Parameters: <nio_name>
199 */
200 static int cmd_create_fifo(hypervisor_conn_t *conn,int argc,char *argv[])
201 {
202 netio_desc_t *nio;
203
204 nio = netio_desc_create_fifo(argv[0]);
205
206 if (!nio) {
207 hypervisor_send_reply(conn,HSC_ERR_CREATE,1,
208 "unable to create FIFO NIO");
209 return(-1);
210 }
211
212 netio_release(argv[0]);
213 hypervisor_send_reply(conn,HSC_INFO_OK,1,"NIO '%s' created",argv[0]);
214 return(0);
215 }
216
217 /*
218 * Establish a cross-connect between 2 FIFO NIO
219 *
220 * Parameters: <nio_A_name> <nio_B_name>
221 */
222 static int cmd_crossconnect_fifo(hypervisor_conn_t *conn,int argc,char *argv[])
223 {
224 netio_desc_t *a,*b;
225
226 if (!(a = hypervisor_find_object(conn,argv[0],OBJ_TYPE_NIO)))
227 return(-1);
228
229 if (!(b = hypervisor_find_object(conn,argv[1],OBJ_TYPE_NIO))) {
230 netio_release(argv[0]);
231 return(-1);
232 }
233
234 netio_fifo_crossconnect(a,b);
235
236 netio_release(argv[0]);
237 netio_release(argv[1]);
238
239 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
240 return(0);
241 }
242
243 /* Delete a NIO */
244 static int cmd_delete(hypervisor_conn_t *conn,int argc,char *argv[])
245 {
246 int res;
247
248 res = netio_delete(argv[0]);
249
250 if (res == 1) {
251 hypervisor_send_reply(conn,HSC_INFO_OK,1,"NIO '%s' deleted",argv[0]);
252 } else {
253 hypervisor_send_reply(conn,HSC_ERR_DELETE,1,
254 "unable to delete NIO '%s'",argv[0]);
255 }
256
257 return(res);
258 }
259
260 /*
261 * Enable/Disable debugging for an NIO
262 *
263 * Parameters: <nio_name> <debug_level>
264 */
265 static int cmd_set_debug(hypervisor_conn_t *conn,int argc,char *argv[])
266 {
267 netio_desc_t *nio;
268
269 if (!(nio = hypervisor_find_object(conn,argv[0],OBJ_TYPE_NIO)))
270 return(-1);
271
272 nio->debug = atoi(argv[1]);
273
274 netio_release(argv[0]);
275 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
276 return(0);
277 }
278
279 /* Bind a packet filter */
280 static int cmd_bind_filter(hypervisor_conn_t *conn,int argc,char *argv[])
281 {
282 netio_desc_t *nio;
283 int res;
284
285 if (!(nio = hypervisor_find_object(conn,argv[0],OBJ_TYPE_NIO)))
286 return(-1);
287
288 res = netio_filter_bind(nio,atoi(argv[1]),argv[2]);
289 netio_release(argv[0]);
290
291 if (!res) {
292 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
293 } else {
294 hypervisor_send_reply(conn,HSC_ERR_UNK_OBJ,1,
295 "Unknown filter %s",argv[2]);
296 }
297 return(0);
298 }
299
300 /* Unbind a packet filter */
301 static int cmd_unbind_filter(hypervisor_conn_t *conn,int argc,char *argv[])
302 {
303 netio_desc_t *nio;
304 int res;
305
306 if (!(nio = hypervisor_find_object(conn,argv[0],OBJ_TYPE_NIO)))
307 return(-1);
308
309 res = netio_filter_unbind(nio,atoi(argv[1]));
310 netio_release(argv[0]);
311
312 if (!res) {
313 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
314 } else {
315 hypervisor_send_reply(conn,HSC_ERR_UNK_OBJ,1,
316 "No filter previously defined");
317 }
318 return(0);
319 }
320
321
322 /* Setup a packet filter for a given NIO */
323 static int cmd_setup_filter(hypervisor_conn_t *conn,int argc,char *argv[])
324 {
325 netio_desc_t *nio;
326
327 if (!(nio = hypervisor_find_object(conn,argv[0],OBJ_TYPE_NIO)))
328 return(-1);
329
330 netio_filter_setup(nio,atoi(argv[1]),argc-2,&argv[2]);
331 netio_release(argv[0]);
332
333 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
334 return(0);
335 }
336
337 /* Show info about a NIO object */
338 static void cmd_show_nio_list(registry_entry_t *entry,void *opt,int *err)
339 {
340 hypervisor_conn_t *conn = opt;
341 hypervisor_send_reply(conn,HSC_INFO_MSG,0,"%s",entry->name);
342 }
343
344 /* NIO List */
345 static int cmd_nio_list(hypervisor_conn_t *conn,int argc,char *argv[])
346 {
347 int err = 0;
348 registry_foreach_type(OBJ_TYPE_NIO,cmd_show_nio_list,conn,&err);
349 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
350 return(0);
351 }
352
353 /* NIO commands */
354 static hypervisor_cmd_t nio_cmd_array[] = {
355 { "create_udp", 4, 4, cmd_create_udp, NULL },
356 { "create_unix", 3, 3, cmd_create_unix, NULL },
357 { "create_vde", 3, 3, cmd_create_vde, NULL },
358 { "create_tap", 2, 2, cmd_create_tap, NULL },
359 #ifdef GEN_ETH
360 { "create_gen_eth", 2, 2, cmd_create_gen_eth, NULL },
361 #endif
362 #ifdef LINUX_ETH
363 { "create_linux_eth", 2, 2, cmd_create_linux_eth, NULL },
364 #endif
365 { "create_null", 1, 1, cmd_create_null, NULL },
366 { "create_fifo", 1, 1, cmd_create_fifo, NULL },
367 { "crossconnect_fifo", 2, 2, cmd_crossconnect_fifo, NULL },
368 { "delete", 1, 1, cmd_delete, NULL },
369 { "set_debug", 2, 2, cmd_set_debug, NULL },
370 { "bind_filter", 3, 3, cmd_bind_filter, NULL },
371 { "unbind_filter", 2, 2, cmd_unbind_filter, NULL },
372 { "setup_filter", 2, 10, cmd_setup_filter, NULL },
373 { "list", 0, 0, cmd_nio_list, NULL },
374 { NULL, -1, -1, NULL, NULL },
375 };
376
377 /* Hypervisor NIO initialization */
378 int hypervisor_nio_init(void)
379 {
380 hypervisor_module_t *module;
381
382 module = hypervisor_register_module("nio",NULL);
383 assert(module != NULL);
384
385 hypervisor_register_cmd_array(module,nio_cmd_array);
386 return(0);
387 }

  ViewVC Help
Powered by ViewVC 1.1.26