]>
Commit | Line | Data |
---|---|---|
63a01ef8 AL |
1 | /* |
2 | * QEMU System Emulator | |
3 | * | |
4 | * Copyright (c) 2003-2008 Fabrice Bellard | |
5 | * | |
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 | * of this software and associated documentation files (the "Software"), to deal | |
8 | * in the Software without restriction, including without limitation the rights | |
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | * copies of the Software, and to permit persons to whom the Software is | |
11 | * furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 | * THE SOFTWARE. | |
23 | */ | |
1df49e04 | 24 | #include "net.h" |
63a01ef8 | 25 | |
d40cdb10 BS |
26 | #include "config-host.h" |
27 | ||
a8ed73f7 | 28 | #include "net/tap.h" |
42281ac9 | 29 | #include "net/socket.h" |
1abecf77 | 30 | #include "net/dump.h" |
68ac40d2 | 31 | #include "net/slirp.h" |
5c361cc3 | 32 | #include "net/vde.h" |
f1d078c3 | 33 | #include "net/util.h" |
511d2b14 BS |
34 | #include "monitor.h" |
35 | #include "sysemu.h" | |
1df49e04 | 36 | #include "qemu-common.h" |
511d2b14 | 37 | #include "qemu_socket.h" |
75422b0d | 38 | #include "hw/qdev.h" |
511d2b14 | 39 | |
5610c3aa | 40 | static QTAILQ_HEAD(, VLANState) vlans; |
577c4af9 | 41 | static QTAILQ_HEAD(, VLANClientState) non_vlan_clients; |
63a01ef8 | 42 | |
cb4522cc GH |
43 | int default_net = 1; |
44 | ||
63a01ef8 AL |
45 | /***********************************************************/ |
46 | /* network device redirectors */ | |
47 | ||
68ac40d2 | 48 | #if defined(DEBUG_NET) |
63a01ef8 AL |
49 | static void hex_dump(FILE *f, const uint8_t *buf, int size) |
50 | { | |
51 | int len, i, j, c; | |
52 | ||
53 | for(i=0;i<size;i+=16) { | |
54 | len = size - i; | |
55 | if (len > 16) | |
56 | len = 16; | |
57 | fprintf(f, "%08x ", i); | |
58 | for(j=0;j<16;j++) { | |
59 | if (j < len) | |
60 | fprintf(f, " %02x", buf[i+j]); | |
61 | else | |
62 | fprintf(f, " "); | |
63 | } | |
64 | fprintf(f, " "); | |
65 | for(j=0;j<len;j++) { | |
66 | c = buf[i+j]; | |
67 | if (c < ' ' || c > '~') | |
68 | c = '.'; | |
69 | fprintf(f, "%c", c); | |
70 | } | |
71 | fprintf(f, "\n"); | |
72 | } | |
73 | } | |
74 | #endif | |
75 | ||
63a01ef8 AL |
76 | static int get_str_sep(char *buf, int buf_size, const char **pp, int sep) |
77 | { | |
78 | const char *p, *p1; | |
79 | int len; | |
80 | p = *pp; | |
81 | p1 = strchr(p, sep); | |
82 | if (!p1) | |
83 | return -1; | |
84 | len = p1 - p; | |
85 | p1++; | |
86 | if (buf_size > 0) { | |
87 | if (len > buf_size - 1) | |
88 | len = buf_size - 1; | |
89 | memcpy(buf, p, len); | |
90 | buf[len] = '\0'; | |
91 | } | |
92 | *pp = p1; | |
93 | return 0; | |
94 | } | |
95 | ||
96 | int parse_host_src_port(struct sockaddr_in *haddr, | |
97 | struct sockaddr_in *saddr, | |
98 | const char *input_str) | |
99 | { | |
6265eb26 | 100 | char *str = qemu_strdup(input_str); |
63a01ef8 AL |
101 | char *host_str = str; |
102 | char *src_str; | |
103 | const char *src_str2; | |
104 | char *ptr; | |
105 | ||
106 | /* | |
107 | * Chop off any extra arguments at the end of the string which | |
108 | * would start with a comma, then fill in the src port information | |
109 | * if it was provided else use the "any address" and "any port". | |
110 | */ | |
111 | if ((ptr = strchr(str,','))) | |
112 | *ptr = '\0'; | |
113 | ||
114 | if ((src_str = strchr(input_str,'@'))) { | |
115 | *src_str = '\0'; | |
116 | src_str++; | |
117 | } | |
118 | ||
119 | if (parse_host_port(haddr, host_str) < 0) | |
120 | goto fail; | |
121 | ||
122 | src_str2 = src_str; | |
123 | if (!src_str || *src_str == '\0') | |
124 | src_str2 = ":0"; | |
125 | ||
126 | if (parse_host_port(saddr, src_str2) < 0) | |
127 | goto fail; | |
128 | ||
129 | free(str); | |
130 | return(0); | |
131 | ||
132 | fail: | |
133 | free(str); | |
134 | return -1; | |
135 | } | |
136 | ||
137 | int parse_host_port(struct sockaddr_in *saddr, const char *str) | |
138 | { | |
139 | char buf[512]; | |
140 | struct hostent *he; | |
141 | const char *p, *r; | |
142 | int port; | |
143 | ||
144 | p = str; | |
145 | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) | |
146 | return -1; | |
147 | saddr->sin_family = AF_INET; | |
148 | if (buf[0] == '\0') { | |
149 | saddr->sin_addr.s_addr = 0; | |
150 | } else { | |
cd390083 | 151 | if (qemu_isdigit(buf[0])) { |
63a01ef8 AL |
152 | if (!inet_aton(buf, &saddr->sin_addr)) |
153 | return -1; | |
154 | } else { | |
155 | if ((he = gethostbyname(buf)) == NULL) | |
156 | return - 1; | |
157 | saddr->sin_addr = *(struct in_addr *)he->h_addr; | |
158 | } | |
159 | } | |
160 | port = strtol(p, (char **)&r, 0); | |
161 | if (r == p) | |
162 | return -1; | |
163 | saddr->sin_port = htons(port); | |
164 | return 0; | |
165 | } | |
166 | ||
7cb7434b AL |
167 | void qemu_format_nic_info_str(VLANClientState *vc, uint8_t macaddr[6]) |
168 | { | |
169 | snprintf(vc->info_str, sizeof(vc->info_str), | |
4dda4063 AL |
170 | "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x", |
171 | vc->model, | |
7cb7434b AL |
172 | macaddr[0], macaddr[1], macaddr[2], |
173 | macaddr[3], macaddr[4], macaddr[5]); | |
174 | } | |
175 | ||
76d32cba GH |
176 | void qemu_macaddr_default_if_unset(MACAddr *macaddr) |
177 | { | |
178 | static int index = 0; | |
179 | static const MACAddr zero = { .a = { 0,0,0,0,0,0 } }; | |
180 | ||
181 | if (memcmp(macaddr, &zero, sizeof(zero)) != 0) | |
182 | return; | |
183 | macaddr->a[0] = 0x52; | |
184 | macaddr->a[1] = 0x54; | |
185 | macaddr->a[2] = 0x00; | |
186 | macaddr->a[3] = 0x12; | |
187 | macaddr->a[4] = 0x34; | |
188 | macaddr->a[5] = 0x56 + index++; | |
189 | } | |
190 | ||
676cff29 AL |
191 | static char *assign_name(VLANClientState *vc1, const char *model) |
192 | { | |
193 | VLANState *vlan; | |
194 | char buf[256]; | |
195 | int id = 0; | |
196 | ||
5610c3aa | 197 | QTAILQ_FOREACH(vlan, &vlans, next) { |
676cff29 AL |
198 | VLANClientState *vc; |
199 | ||
5610c3aa MM |
200 | QTAILQ_FOREACH(vc, &vlan->clients, next) { |
201 | if (vc != vc1 && strcmp(vc->model, model) == 0) { | |
676cff29 | 202 | id++; |
5610c3aa MM |
203 | } |
204 | } | |
676cff29 AL |
205 | } |
206 | ||
207 | snprintf(buf, sizeof(buf), "%s.%d", model, id); | |
208 | ||
02374aa0 | 209 | return qemu_strdup(buf); |
676cff29 AL |
210 | } |
211 | ||
9a6ecb30 | 212 | static ssize_t qemu_deliver_packet(VLANClientState *sender, |
c0b8e49c | 213 | unsigned flags, |
9a6ecb30 MM |
214 | const uint8_t *data, |
215 | size_t size, | |
216 | void *opaque); | |
217 | static ssize_t qemu_deliver_packet_iov(VLANClientState *sender, | |
c0b8e49c | 218 | unsigned flags, |
9a6ecb30 MM |
219 | const struct iovec *iov, |
220 | int iovcnt, | |
221 | void *opaque); | |
222 | ||
45460d1a MM |
223 | VLANClientState *qemu_new_net_client(NetClientInfo *info, |
224 | VLANState *vlan, | |
225 | VLANClientState *peer, | |
226 | const char *model, | |
227 | const char *name) | |
63a01ef8 | 228 | { |
5610c3aa MM |
229 | VLANClientState *vc; |
230 | ||
45460d1a MM |
231 | assert(info->size >= sizeof(VLANClientState)); |
232 | ||
233 | vc = qemu_mallocz(info->size); | |
5610c3aa | 234 | |
665a3b07 | 235 | vc->info = info; |
02374aa0 | 236 | vc->model = qemu_strdup(model); |
45460d1a | 237 | if (name) { |
02374aa0 | 238 | vc->name = qemu_strdup(name); |
45460d1a | 239 | } else { |
7a9f6e4a | 240 | vc->name = assign_name(vc, model); |
45460d1a | 241 | } |
5610c3aa | 242 | |
d80b9fc6 | 243 | if (vlan) { |
283c7c63 | 244 | assert(!peer); |
d80b9fc6 MM |
245 | vc->vlan = vlan; |
246 | QTAILQ_INSERT_TAIL(&vc->vlan->clients, vc, next); | |
577c4af9 | 247 | } else { |
283c7c63 | 248 | if (peer) { |
27f3f8a3 | 249 | assert(!peer->peer); |
283c7c63 MM |
250 | vc->peer = peer; |
251 | peer->peer = vc; | |
252 | } | |
577c4af9 | 253 | QTAILQ_INSERT_TAIL(&non_vlan_clients, vc, next); |
9a6ecb30 MM |
254 | |
255 | vc->send_queue = qemu_new_net_queue(qemu_deliver_packet, | |
256 | qemu_deliver_packet_iov, | |
257 | vc); | |
d80b9fc6 | 258 | } |
63a01ef8 | 259 | |
63a01ef8 AL |
260 | return vc; |
261 | } | |
262 | ||
ebef2c09 MM |
263 | NICState *qemu_new_nic(NetClientInfo *info, |
264 | NICConf *conf, | |
265 | const char *model, | |
266 | const char *name, | |
267 | void *opaque) | |
268 | { | |
269 | VLANClientState *nc; | |
270 | NICState *nic; | |
271 | ||
272 | assert(info->type == NET_CLIENT_TYPE_NIC); | |
273 | assert(info->size >= sizeof(NICState)); | |
274 | ||
275 | nc = qemu_new_net_client(info, conf->vlan, conf->peer, model, name); | |
276 | ||
277 | nic = DO_UPCAST(NICState, nc, nc); | |
278 | nic->conf = conf; | |
279 | nic->opaque = opaque; | |
280 | ||
281 | return nic; | |
282 | } | |
283 | ||
63a01ef8 AL |
284 | void qemu_del_vlan_client(VLANClientState *vc) |
285 | { | |
d80b9fc6 MM |
286 | if (vc->vlan) { |
287 | QTAILQ_REMOVE(&vc->vlan->clients, vc, next); | |
577c4af9 | 288 | } else { |
9a6ecb30 MM |
289 | if (vc->send_queue) { |
290 | qemu_del_net_queue(vc->send_queue); | |
291 | } | |
577c4af9 | 292 | QTAILQ_REMOVE(&non_vlan_clients, vc, next); |
283c7c63 MM |
293 | if (vc->peer) { |
294 | vc->peer->peer = NULL; | |
295 | } | |
d80b9fc6 | 296 | } |
63a01ef8 | 297 | |
665a3b07 MM |
298 | if (vc->info->cleanup) { |
299 | vc->info->cleanup(vc); | |
5610c3aa MM |
300 | } |
301 | ||
302 | qemu_free(vc->name); | |
303 | qemu_free(vc->model); | |
304 | qemu_free(vc); | |
63a01ef8 AL |
305 | } |
306 | ||
68ac40d2 | 307 | VLANClientState * |
1a609520 JK |
308 | qemu_find_vlan_client_by_name(Monitor *mon, int vlan_id, |
309 | const char *client_str) | |
310 | { | |
311 | VLANState *vlan; | |
312 | VLANClientState *vc; | |
313 | ||
314 | vlan = qemu_find_vlan(vlan_id, 0); | |
315 | if (!vlan) { | |
316 | monitor_printf(mon, "unknown VLAN %d\n", vlan_id); | |
317 | return NULL; | |
318 | } | |
319 | ||
5610c3aa | 320 | QTAILQ_FOREACH(vc, &vlan->clients, next) { |
1a609520 JK |
321 | if (!strcmp(vc->name, client_str)) { |
322 | break; | |
323 | } | |
324 | } | |
325 | if (!vc) { | |
326 | monitor_printf(mon, "can't find device %s on VLAN %d\n", | |
327 | client_str, vlan_id); | |
328 | } | |
329 | ||
330 | return vc; | |
331 | } | |
332 | ||
57f9ef17 MM |
333 | void qemu_foreach_nic(qemu_nic_foreach func, void *opaque) |
334 | { | |
335 | VLANClientState *nc; | |
336 | VLANState *vlan; | |
337 | ||
338 | QTAILQ_FOREACH(nc, &non_vlan_clients, next) { | |
339 | if (nc->info->type == NET_CLIENT_TYPE_NIC) { | |
340 | func(DO_UPCAST(NICState, nc, nc), opaque); | |
341 | } | |
342 | } | |
343 | ||
344 | QTAILQ_FOREACH(vlan, &vlans, next) { | |
345 | QTAILQ_FOREACH(nc, &vlan->clients, next) { | |
346 | if (nc->info->type == NET_CLIENT_TYPE_NIC) { | |
347 | func(DO_UPCAST(NICState, nc, nc), opaque); | |
348 | } | |
349 | } | |
350 | } | |
351 | } | |
352 | ||
2e1e0641 | 353 | int qemu_can_send_packet(VLANClientState *sender) |
63a01ef8 | 354 | { |
2e1e0641 | 355 | VLANState *vlan = sender->vlan; |
63a01ef8 AL |
356 | VLANClientState *vc; |
357 | ||
9a6ecb30 | 358 | if (sender->peer) { |
893379ef MM |
359 | if (sender->peer->receive_disabled) { |
360 | return 0; | |
665a3b07 MM |
361 | } else if (sender->peer->info->can_receive && |
362 | !sender->peer->info->can_receive(sender->peer)) { | |
9a6ecb30 | 363 | return 0; |
893379ef MM |
364 | } else { |
365 | return 1; | |
9a6ecb30 MM |
366 | } |
367 | } | |
368 | ||
d80b9fc6 MM |
369 | if (!sender->vlan) { |
370 | return 1; | |
371 | } | |
372 | ||
5610c3aa | 373 | QTAILQ_FOREACH(vc, &vlan->clients, next) { |
2e1e0641 MM |
374 | if (vc == sender) { |
375 | continue; | |
376 | } | |
377 | ||
cda9046b | 378 | /* no can_receive() handler, they can always receive */ |
665a3b07 | 379 | if (!vc->info->can_receive || vc->info->can_receive(vc)) { |
2e1e0641 | 380 | return 1; |
63a01ef8 AL |
381 | } |
382 | } | |
383 | return 0; | |
384 | } | |
385 | ||
9a6ecb30 | 386 | static ssize_t qemu_deliver_packet(VLANClientState *sender, |
c0b8e49c | 387 | unsigned flags, |
9a6ecb30 MM |
388 | const uint8_t *data, |
389 | size_t size, | |
390 | void *opaque) | |
391 | { | |
392 | VLANClientState *vc = opaque; | |
893379ef | 393 | ssize_t ret; |
9a6ecb30 MM |
394 | |
395 | if (vc->link_down) { | |
396 | return size; | |
397 | } | |
398 | ||
893379ef MM |
399 | if (vc->receive_disabled) { |
400 | return 0; | |
401 | } | |
402 | ||
665a3b07 MM |
403 | if (flags & QEMU_NET_PACKET_FLAG_RAW && vc->info->receive_raw) { |
404 | ret = vc->info->receive_raw(vc, data, size); | |
893379ef | 405 | } else { |
665a3b07 | 406 | ret = vc->info->receive(vc, data, size); |
893379ef MM |
407 | } |
408 | ||
409 | if (ret == 0) { | |
410 | vc->receive_disabled = 1; | |
411 | }; | |
412 | ||
413 | return ret; | |
9a6ecb30 MM |
414 | } |
415 | ||
f7105843 | 416 | static ssize_t qemu_vlan_deliver_packet(VLANClientState *sender, |
c0b8e49c | 417 | unsigned flags, |
f7105843 MM |
418 | const uint8_t *buf, |
419 | size_t size, | |
420 | void *opaque) | |
63a01ef8 | 421 | { |
f7105843 | 422 | VLANState *vlan = opaque; |
63a01ef8 | 423 | VLANClientState *vc; |
893379ef | 424 | ssize_t ret = -1; |
63a01ef8 | 425 | |
f7105843 | 426 | QTAILQ_FOREACH(vc, &vlan->clients, next) { |
3e021d40 MM |
427 | ssize_t len; |
428 | ||
429 | if (vc == sender) { | |
430 | continue; | |
764a4d1d | 431 | } |
3e021d40 MM |
432 | |
433 | if (vc->link_down) { | |
434 | ret = size; | |
435 | continue; | |
436 | } | |
437 | ||
893379ef MM |
438 | if (vc->receive_disabled) { |
439 | ret = 0; | |
440 | continue; | |
441 | } | |
442 | ||
665a3b07 MM |
443 | if (flags & QEMU_NET_PACKET_FLAG_RAW && vc->info->receive_raw) { |
444 | len = vc->info->receive_raw(vc, buf, size); | |
893379ef | 445 | } else { |
665a3b07 | 446 | len = vc->info->receive(vc, buf, size); |
893379ef MM |
447 | } |
448 | ||
449 | if (len == 0) { | |
450 | vc->receive_disabled = 1; | |
451 | } | |
3e021d40 MM |
452 | |
453 | ret = (ret >= 0) ? ret : len; | |
893379ef | 454 | |
764a4d1d | 455 | } |
3e021d40 MM |
456 | |
457 | return ret; | |
764a4d1d AL |
458 | } |
459 | ||
8cad5516 MM |
460 | void qemu_purge_queued_packets(VLANClientState *vc) |
461 | { | |
9a6ecb30 MM |
462 | NetQueue *queue; |
463 | ||
464 | if (!vc->peer && !vc->vlan) { | |
d80b9fc6 | 465 | return; |
9a6ecb30 | 466 | } |
d80b9fc6 | 467 | |
9a6ecb30 MM |
468 | if (vc->peer) { |
469 | queue = vc->peer->send_queue; | |
470 | } else { | |
471 | queue = vc->vlan->send_queue; | |
472 | } | |
473 | ||
474 | qemu_net_queue_purge(queue, vc); | |
8cad5516 MM |
475 | } |
476 | ||
f3b6c7fc | 477 | void qemu_flush_queued_packets(VLANClientState *vc) |
e94667b9 | 478 | { |
9a6ecb30 MM |
479 | NetQueue *queue; |
480 | ||
893379ef MM |
481 | vc->receive_disabled = 0; |
482 | ||
9a6ecb30 MM |
483 | if (vc->vlan) { |
484 | queue = vc->vlan->send_queue; | |
485 | } else { | |
486 | queue = vc->send_queue; | |
487 | } | |
d80b9fc6 | 488 | |
9a6ecb30 | 489 | qemu_net_queue_flush(queue); |
e94667b9 MM |
490 | } |
491 | ||
ca77d175 MM |
492 | static ssize_t qemu_send_packet_async_with_flags(VLANClientState *sender, |
493 | unsigned flags, | |
494 | const uint8_t *buf, int size, | |
495 | NetPacketSent *sent_cb) | |
764a4d1d | 496 | { |
9a6ecb30 | 497 | NetQueue *queue; |
436e5e53 | 498 | |
63a01ef8 | 499 | #ifdef DEBUG_NET |
d80b9fc6 | 500 | printf("qemu_send_packet_async:\n"); |
63a01ef8 AL |
501 | hex_dump(stdout, buf, size); |
502 | #endif | |
f3b6c7fc | 503 | |
9a6ecb30 MM |
504 | if (sender->link_down || (!sender->peer && !sender->vlan)) { |
505 | return size; | |
506 | } | |
507 | ||
508 | if (sender->peer) { | |
509 | queue = sender->peer->send_queue; | |
510 | } else { | |
511 | queue = sender->vlan->send_queue; | |
512 | } | |
513 | ||
ca77d175 MM |
514 | return qemu_net_queue_send(queue, sender, flags, buf, size, sent_cb); |
515 | } | |
516 | ||
517 | ssize_t qemu_send_packet_async(VLANClientState *sender, | |
518 | const uint8_t *buf, int size, | |
519 | NetPacketSent *sent_cb) | |
520 | { | |
521 | return qemu_send_packet_async_with_flags(sender, QEMU_NET_PACKET_FLAG_NONE, | |
522 | buf, size, sent_cb); | |
f3b6c7fc MM |
523 | } |
524 | ||
525 | void qemu_send_packet(VLANClientState *vc, const uint8_t *buf, int size) | |
526 | { | |
527 | qemu_send_packet_async(vc, buf, size, NULL); | |
63a01ef8 AL |
528 | } |
529 | ||
ca77d175 MM |
530 | ssize_t qemu_send_packet_raw(VLANClientState *vc, const uint8_t *buf, int size) |
531 | { | |
532 | return qemu_send_packet_async_with_flags(vc, QEMU_NET_PACKET_FLAG_RAW, | |
533 | buf, size, NULL); | |
534 | } | |
535 | ||
fbe78f4f AL |
536 | static ssize_t vc_sendv_compat(VLANClientState *vc, const struct iovec *iov, |
537 | int iovcnt) | |
538 | { | |
539 | uint8_t buffer[4096]; | |
540 | size_t offset = 0; | |
541 | int i; | |
542 | ||
543 | for (i = 0; i < iovcnt; i++) { | |
544 | size_t len; | |
545 | ||
546 | len = MIN(sizeof(buffer) - offset, iov[i].iov_len); | |
547 | memcpy(buffer + offset, iov[i].iov_base, len); | |
548 | offset += len; | |
549 | } | |
550 | ||
665a3b07 | 551 | return vc->info->receive(vc, buffer, offset); |
fbe78f4f AL |
552 | } |
553 | ||
e0e7877a AL |
554 | static ssize_t calc_iov_length(const struct iovec *iov, int iovcnt) |
555 | { | |
556 | size_t offset = 0; | |
557 | int i; | |
558 | ||
559 | for (i = 0; i < iovcnt; i++) | |
560 | offset += iov[i].iov_len; | |
561 | return offset; | |
562 | } | |
563 | ||
9a6ecb30 | 564 | static ssize_t qemu_deliver_packet_iov(VLANClientState *sender, |
c0b8e49c | 565 | unsigned flags, |
9a6ecb30 MM |
566 | const struct iovec *iov, |
567 | int iovcnt, | |
568 | void *opaque) | |
569 | { | |
570 | VLANClientState *vc = opaque; | |
571 | ||
572 | if (vc->link_down) { | |
573 | return calc_iov_length(iov, iovcnt); | |
574 | } | |
575 | ||
665a3b07 MM |
576 | if (vc->info->receive_iov) { |
577 | return vc->info->receive_iov(vc, iov, iovcnt); | |
9a6ecb30 MM |
578 | } else { |
579 | return vc_sendv_compat(vc, iov, iovcnt); | |
580 | } | |
581 | } | |
582 | ||
f7105843 | 583 | static ssize_t qemu_vlan_deliver_packet_iov(VLANClientState *sender, |
c0b8e49c | 584 | unsigned flags, |
f7105843 MM |
585 | const struct iovec *iov, |
586 | int iovcnt, | |
587 | void *opaque) | |
fbe78f4f | 588 | { |
f7105843 | 589 | VLANState *vlan = opaque; |
fbe78f4f | 590 | VLANClientState *vc; |
f7105843 | 591 | ssize_t ret = -1; |
e94667b9 | 592 | |
f7105843 | 593 | QTAILQ_FOREACH(vc, &vlan->clients, next) { |
e94667b9 MM |
594 | ssize_t len; |
595 | ||
596 | if (vc == sender) { | |
597 | continue; | |
598 | } | |
599 | ||
600 | if (vc->link_down) { | |
601 | ret = calc_iov_length(iov, iovcnt); | |
602 | continue; | |
603 | } | |
604 | ||
ca77d175 MM |
605 | assert(!(flags & QEMU_NET_PACKET_FLAG_RAW)); |
606 | ||
665a3b07 MM |
607 | if (vc->info->receive_iov) { |
608 | len = vc->info->receive_iov(vc, iov, iovcnt); | |
e94667b9 MM |
609 | } else { |
610 | len = vc_sendv_compat(vc, iov, iovcnt); | |
611 | } | |
612 | ||
613 | ret = (ret >= 0) ? ret : len; | |
614 | } | |
615 | ||
e94667b9 MM |
616 | return ret; |
617 | } | |
618 | ||
f3b6c7fc MM |
619 | ssize_t qemu_sendv_packet_async(VLANClientState *sender, |
620 | const struct iovec *iov, int iovcnt, | |
621 | NetPacketSent *sent_cb) | |
e94667b9 | 622 | { |
9a6ecb30 MM |
623 | NetQueue *queue; |
624 | ||
625 | if (sender->link_down || (!sender->peer && !sender->vlan)) { | |
e94667b9 MM |
626 | return calc_iov_length(iov, iovcnt); |
627 | } | |
628 | ||
9a6ecb30 MM |
629 | if (sender->peer) { |
630 | queue = sender->peer->send_queue; | |
631 | } else { | |
632 | queue = sender->vlan->send_queue; | |
633 | } | |
634 | ||
c0b8e49c MM |
635 | return qemu_net_queue_send_iov(queue, sender, |
636 | QEMU_NET_PACKET_FLAG_NONE, | |
637 | iov, iovcnt, sent_cb); | |
fbe78f4f AL |
638 | } |
639 | ||
f3b6c7fc MM |
640 | ssize_t |
641 | qemu_sendv_packet(VLANClientState *vc, const struct iovec *iov, int iovcnt) | |
642 | { | |
643 | return qemu_sendv_packet_async(vc, iov, iovcnt, NULL); | |
644 | } | |
645 | ||
63a01ef8 | 646 | /* find or alloc a new VLAN */ |
1a609520 | 647 | VLANState *qemu_find_vlan(int id, int allocate) |
63a01ef8 | 648 | { |
5610c3aa MM |
649 | VLANState *vlan; |
650 | ||
651 | QTAILQ_FOREACH(vlan, &vlans, next) { | |
652 | if (vlan->id == id) { | |
63a01ef8 | 653 | return vlan; |
5610c3aa | 654 | } |
63a01ef8 | 655 | } |
5610c3aa | 656 | |
1a609520 JK |
657 | if (!allocate) { |
658 | return NULL; | |
659 | } | |
5610c3aa | 660 | |
63a01ef8 | 661 | vlan = qemu_mallocz(sizeof(VLANState)); |
63a01ef8 | 662 | vlan->id = id; |
5610c3aa | 663 | QTAILQ_INIT(&vlan->clients); |
f7105843 MM |
664 | |
665 | vlan->send_queue = qemu_new_net_queue(qemu_vlan_deliver_packet, | |
666 | qemu_vlan_deliver_packet_iov, | |
667 | vlan); | |
5610c3aa MM |
668 | |
669 | QTAILQ_INSERT_TAIL(&vlans, vlan, next); | |
670 | ||
63a01ef8 AL |
671 | return vlan; |
672 | } | |
673 | ||
2ef924b4 | 674 | VLANClientState *qemu_find_netdev(const char *id) |
5869c4d5 MM |
675 | { |
676 | VLANClientState *vc; | |
677 | ||
678 | QTAILQ_FOREACH(vc, &non_vlan_clients, next) { | |
679 | if (!strcmp(vc->name, id)) { | |
680 | return vc; | |
681 | } | |
682 | } | |
683 | ||
684 | return NULL; | |
685 | } | |
686 | ||
7697079b AL |
687 | static int nic_get_free_idx(void) |
688 | { | |
689 | int index; | |
690 | ||
691 | for (index = 0; index < MAX_NICS; index++) | |
692 | if (!nd_table[index].used) | |
693 | return index; | |
694 | return -1; | |
695 | } | |
696 | ||
07caea31 MA |
697 | int qemu_show_nic_models(const char *arg, const char *const *models) |
698 | { | |
699 | int i; | |
700 | ||
701 | if (!arg || strcmp(arg, "?")) | |
702 | return 0; | |
703 | ||
704 | fprintf(stderr, "qemu: Supported NIC models: "); | |
705 | for (i = 0 ; models[i]; i++) | |
706 | fprintf(stderr, "%s%c", models[i], models[i+1] ? ',' : '\n'); | |
707 | return 1; | |
708 | } | |
709 | ||
d07f22c5 AL |
710 | void qemu_check_nic_model(NICInfo *nd, const char *model) |
711 | { | |
712 | const char *models[2]; | |
713 | ||
714 | models[0] = model; | |
715 | models[1] = NULL; | |
716 | ||
07caea31 MA |
717 | if (qemu_show_nic_models(nd->model, models)) |
718 | exit(0); | |
719 | if (qemu_find_nic_model(nd, models, model) < 0) | |
720 | exit(1); | |
d07f22c5 AL |
721 | } |
722 | ||
07caea31 MA |
723 | int qemu_find_nic_model(NICInfo *nd, const char * const *models, |
724 | const char *default_model) | |
d07f22c5 | 725 | { |
07caea31 | 726 | int i; |
d07f22c5 AL |
727 | |
728 | if (!nd->model) | |
32a8e14a | 729 | nd->model = qemu_strdup(default_model); |
d07f22c5 | 730 | |
07caea31 MA |
731 | for (i = 0 ; models[i]; i++) { |
732 | if (strcmp(nd->model, models[i]) == 0) | |
733 | return i; | |
d07f22c5 AL |
734 | } |
735 | ||
1ecda02b | 736 | error_report("qemu: Unsupported NIC model: %s", nd->model); |
07caea31 | 737 | return -1; |
d07f22c5 AL |
738 | } |
739 | ||
5281d757 | 740 | int net_handle_fd_param(Monitor *mon, const char *param) |
c1d6eed7 MM |
741 | { |
742 | if (!qemu_isdigit(param[0])) { | |
743 | int fd; | |
744 | ||
745 | fd = monitor_get_fd(mon, param); | |
746 | if (fd == -1) { | |
1ecda02b | 747 | error_report("No file descriptor named %s found", param); |
c1d6eed7 MM |
748 | return -1; |
749 | } | |
750 | ||
751 | return fd; | |
752 | } else { | |
753 | return strtol(param, NULL, 0); | |
754 | } | |
755 | } | |
756 | ||
f6b134ac MM |
757 | static int net_init_nic(QemuOpts *opts, |
758 | Monitor *mon, | |
759 | const char *name, | |
760 | VLANState *vlan) | |
f83c6e10 MM |
761 | { |
762 | int idx; | |
763 | NICInfo *nd; | |
5869c4d5 | 764 | const char *netdev; |
f83c6e10 MM |
765 | |
766 | idx = nic_get_free_idx(); | |
767 | if (idx == -1 || nb_nics >= MAX_NICS) { | |
1ecda02b | 768 | error_report("Too Many NICs"); |
f83c6e10 MM |
769 | return -1; |
770 | } | |
771 | ||
772 | nd = &nd_table[idx]; | |
773 | ||
774 | memset(nd, 0, sizeof(*nd)); | |
775 | ||
5869c4d5 MM |
776 | if ((netdev = qemu_opt_get(opts, "netdev"))) { |
777 | nd->netdev = qemu_find_netdev(netdev); | |
778 | if (!nd->netdev) { | |
1ecda02b | 779 | error_report("netdev '%s' not found", netdev); |
5869c4d5 MM |
780 | return -1; |
781 | } | |
782 | } else { | |
783 | assert(vlan); | |
784 | nd->vlan = vlan; | |
785 | } | |
6d952ebe MM |
786 | if (name) { |
787 | nd->name = qemu_strdup(name); | |
788 | } | |
f83c6e10 MM |
789 | if (qemu_opt_get(opts, "model")) { |
790 | nd->model = qemu_strdup(qemu_opt_get(opts, "model")); | |
791 | } | |
792 | if (qemu_opt_get(opts, "addr")) { | |
793 | nd->devaddr = qemu_strdup(qemu_opt_get(opts, "addr")); | |
794 | } | |
795 | ||
796 | nd->macaddr[0] = 0x52; | |
797 | nd->macaddr[1] = 0x54; | |
798 | nd->macaddr[2] = 0x00; | |
799 | nd->macaddr[3] = 0x12; | |
800 | nd->macaddr[4] = 0x34; | |
801 | nd->macaddr[5] = 0x56 + idx; | |
802 | ||
803 | if (qemu_opt_get(opts, "macaddr") && | |
f1d078c3 | 804 | net_parse_macaddr(nd->macaddr, qemu_opt_get(opts, "macaddr")) < 0) { |
1ecda02b | 805 | error_report("invalid syntax for ethernet address"); |
f83c6e10 MM |
806 | return -1; |
807 | } | |
808 | ||
75422b0d AS |
809 | nd->nvectors = qemu_opt_get_number(opts, "vectors", |
810 | DEV_NVECTORS_UNSPECIFIED); | |
811 | if (nd->nvectors != DEV_NVECTORS_UNSPECIFIED && | |
f83c6e10 | 812 | (nd->nvectors < 0 || nd->nvectors > 0x7ffffff)) { |
1ecda02b | 813 | error_report("invalid # of vectors: %d", nd->nvectors); |
f83c6e10 MM |
814 | return -1; |
815 | } | |
816 | ||
817 | nd->used = 1; | |
f83c6e10 MM |
818 | nb_nics++; |
819 | ||
820 | return idx; | |
821 | } | |
822 | ||
823 | #define NET_COMMON_PARAMS_DESC \ | |
824 | { \ | |
825 | .name = "type", \ | |
826 | .type = QEMU_OPT_STRING, \ | |
827 | .help = "net client type (nic, tap etc.)", \ | |
828 | }, { \ | |
829 | .name = "vlan", \ | |
830 | .type = QEMU_OPT_NUMBER, \ | |
831 | .help = "vlan number", \ | |
832 | }, { \ | |
833 | .name = "name", \ | |
834 | .type = QEMU_OPT_STRING, \ | |
835 | .help = "identifier for monitor commands", \ | |
836 | } | |
837 | ||
6d952ebe MM |
838 | typedef int (*net_client_init_func)(QemuOpts *opts, |
839 | Monitor *mon, | |
f6b134ac MM |
840 | const char *name, |
841 | VLANState *vlan); | |
f83c6e10 MM |
842 | |
843 | /* magic number, but compiler will warn if too small */ | |
844 | #define NET_MAX_DESC 20 | |
845 | ||
238431a9 | 846 | static const struct { |
f83c6e10 MM |
847 | const char *type; |
848 | net_client_init_func init; | |
849 | QemuOptDesc desc[NET_MAX_DESC]; | |
850 | } net_client_types[] = { | |
851 | { | |
852 | .type = "none", | |
853 | .desc = { | |
854 | NET_COMMON_PARAMS_DESC, | |
855 | { /* end of list */ } | |
856 | }, | |
857 | }, { | |
858 | .type = "nic", | |
859 | .init = net_init_nic, | |
860 | .desc = { | |
861 | NET_COMMON_PARAMS_DESC, | |
5869c4d5 MM |
862 | { |
863 | .name = "netdev", | |
864 | .type = QEMU_OPT_STRING, | |
865 | .help = "id of -netdev to connect to", | |
866 | }, | |
f83c6e10 MM |
867 | { |
868 | .name = "macaddr", | |
869 | .type = QEMU_OPT_STRING, | |
870 | .help = "MAC address", | |
871 | }, { | |
872 | .name = "model", | |
873 | .type = QEMU_OPT_STRING, | |
874 | .help = "device model (e1000, rtl8139, virtio etc.)", | |
875 | }, { | |
876 | .name = "addr", | |
877 | .type = QEMU_OPT_STRING, | |
878 | .help = "PCI device address", | |
879 | }, { | |
880 | .name = "vectors", | |
881 | .type = QEMU_OPT_NUMBER, | |
882 | .help = "number of MSI-x vectors, 0 to disable MSI-X", | |
883 | }, | |
884 | { /* end of list */ } | |
885 | }, | |
ec302ffd MM |
886 | #ifdef CONFIG_SLIRP |
887 | }, { | |
888 | .type = "user", | |
889 | .init = net_init_slirp, | |
890 | .desc = { | |
891 | NET_COMMON_PARAMS_DESC, | |
892 | { | |
893 | .name = "hostname", | |
894 | .type = QEMU_OPT_STRING, | |
895 | .help = "client hostname reported by the builtin DHCP server", | |
896 | }, { | |
897 | .name = "restrict", | |
898 | .type = QEMU_OPT_STRING, | |
899 | .help = "isolate the guest from the host (y|yes|n|no)", | |
900 | }, { | |
901 | .name = "ip", | |
902 | .type = QEMU_OPT_STRING, | |
903 | .help = "legacy parameter, use net= instead", | |
904 | }, { | |
905 | .name = "net", | |
906 | .type = QEMU_OPT_STRING, | |
907 | .help = "IP address and optional netmask", | |
908 | }, { | |
909 | .name = "host", | |
910 | .type = QEMU_OPT_STRING, | |
911 | .help = "guest-visible address of the host", | |
912 | }, { | |
913 | .name = "tftp", | |
914 | .type = QEMU_OPT_STRING, | |
915 | .help = "root directory of the built-in TFTP server", | |
916 | }, { | |
917 | .name = "bootfile", | |
918 | .type = QEMU_OPT_STRING, | |
919 | .help = "BOOTP filename, for use with tftp=", | |
920 | }, { | |
921 | .name = "dhcpstart", | |
922 | .type = QEMU_OPT_STRING, | |
923 | .help = "the first of the 16 IPs the built-in DHCP server can assign", | |
924 | }, { | |
925 | .name = "dns", | |
926 | .type = QEMU_OPT_STRING, | |
927 | .help = "guest-visible address of the virtual nameserver", | |
928 | }, { | |
929 | .name = "smb", | |
930 | .type = QEMU_OPT_STRING, | |
931 | .help = "root directory of the built-in SMB server", | |
932 | }, { | |
933 | .name = "smbserver", | |
934 | .type = QEMU_OPT_STRING, | |
935 | .help = "IP address of the built-in SMB server", | |
936 | }, { | |
937 | .name = "hostfwd", | |
938 | .type = QEMU_OPT_STRING, | |
939 | .help = "guest port number to forward incoming TCP or UDP connections", | |
940 | }, { | |
941 | .name = "guestfwd", | |
942 | .type = QEMU_OPT_STRING, | |
943 | .help = "IP address and port to forward guest TCP connections", | |
944 | }, | |
945 | { /* end of list */ } | |
946 | }, | |
8a1c5235 | 947 | #endif |
8a1c5235 MM |
948 | }, { |
949 | .type = "tap", | |
a8ed73f7 | 950 | .init = net_init_tap, |
8a1c5235 MM |
951 | .desc = { |
952 | NET_COMMON_PARAMS_DESC, | |
953 | { | |
954 | .name = "ifname", | |
955 | .type = QEMU_OPT_STRING, | |
956 | .help = "interface name", | |
957 | }, | |
a8ed73f7 | 958 | #ifndef _WIN32 |
8a1c5235 MM |
959 | { |
960 | .name = "fd", | |
961 | .type = QEMU_OPT_STRING, | |
962 | .help = "file descriptor of an already opened tap", | |
8a1c5235 MM |
963 | }, { |
964 | .name = "script", | |
965 | .type = QEMU_OPT_STRING, | |
966 | .help = "script to initialize the interface", | |
967 | }, { | |
968 | .name = "downscript", | |
969 | .type = QEMU_OPT_STRING, | |
970 | .help = "script to shut down the interface", | |
8a1c5235 MM |
971 | }, { |
972 | .name = "sndbuf", | |
973 | .type = QEMU_OPT_SIZE, | |
974 | .help = "send buffer limit" | |
baf74c95 MM |
975 | }, { |
976 | .name = "vnet_hdr", | |
977 | .type = QEMU_OPT_BOOL, | |
978 | .help = "enable the IFF_VNET_HDR flag on the tap interface" | |
82b0d80e MT |
979 | }, { |
980 | .name = "vhost", | |
981 | .type = QEMU_OPT_BOOL, | |
982 | .help = "enable vhost-net network accelerator", | |
983 | }, { | |
984 | .name = "vhostfd", | |
985 | .type = QEMU_OPT_STRING, | |
986 | .help = "file descriptor of an already opened vhost net device", | |
8a1c5235 | 987 | }, |
a8ed73f7 | 988 | #endif /* _WIN32 */ |
8a1c5235 MM |
989 | { /* end of list */ } |
990 | }, | |
88ce16ca MM |
991 | }, { |
992 | .type = "socket", | |
993 | .init = net_init_socket, | |
994 | .desc = { | |
995 | NET_COMMON_PARAMS_DESC, | |
996 | { | |
997 | .name = "fd", | |
998 | .type = QEMU_OPT_STRING, | |
999 | .help = "file descriptor of an already opened socket", | |
1000 | }, { | |
1001 | .name = "listen", | |
1002 | .type = QEMU_OPT_STRING, | |
1003 | .help = "port number, and optional hostname, to listen on", | |
1004 | }, { | |
1005 | .name = "connect", | |
1006 | .type = QEMU_OPT_STRING, | |
1007 | .help = "port number, and optional hostname, to connect to", | |
1008 | }, { | |
1009 | .name = "mcast", | |
1010 | .type = QEMU_OPT_STRING, | |
1011 | .help = "UDP multicast address and port number", | |
1012 | }, | |
1013 | { /* end of list */ } | |
1014 | }, | |
dd51058d MM |
1015 | #ifdef CONFIG_VDE |
1016 | }, { | |
1017 | .type = "vde", | |
1018 | .init = net_init_vde, | |
1019 | .desc = { | |
1020 | NET_COMMON_PARAMS_DESC, | |
1021 | { | |
1022 | .name = "sock", | |
1023 | .type = QEMU_OPT_STRING, | |
1024 | .help = "socket path", | |
1025 | }, { | |
1026 | .name = "port", | |
1027 | .type = QEMU_OPT_NUMBER, | |
1028 | .help = "port number", | |
1029 | }, { | |
1030 | .name = "group", | |
1031 | .type = QEMU_OPT_STRING, | |
1032 | .help = "group owner of socket", | |
1033 | }, { | |
1034 | .name = "mode", | |
1035 | .type = QEMU_OPT_NUMBER, | |
1036 | .help = "permissions for socket", | |
1037 | }, | |
1038 | { /* end of list */ } | |
1039 | }, | |
1040 | #endif | |
ed2955c2 MM |
1041 | }, { |
1042 | .type = "dump", | |
1043 | .init = net_init_dump, | |
1044 | .desc = { | |
1045 | NET_COMMON_PARAMS_DESC, | |
1046 | { | |
1047 | .name = "len", | |
1048 | .type = QEMU_OPT_SIZE, | |
1049 | .help = "per-packet size limit (64k default)", | |
1050 | }, { | |
1051 | .name = "file", | |
1052 | .type = QEMU_OPT_STRING, | |
1053 | .help = "dump file path (default is qemu-vlan0.pcap)", | |
1054 | }, | |
1055 | { /* end of list */ } | |
1056 | }, | |
f83c6e10 MM |
1057 | }, |
1058 | { /* end of list */ } | |
1059 | }; | |
1060 | ||
f6b134ac | 1061 | int net_client_init(Monitor *mon, QemuOpts *opts, int is_netdev) |
f83c6e10 | 1062 | { |
6d952ebe | 1063 | const char *name; |
f83c6e10 MM |
1064 | const char *type; |
1065 | int i; | |
1066 | ||
1067 | type = qemu_opt_get(opts, "type"); | |
5294e2c7 MA |
1068 | if (!type) { |
1069 | qerror_report(QERR_MISSING_PARAMETER, "type"); | |
1070 | return -1; | |
1071 | } | |
f83c6e10 | 1072 | |
5294e2c7 | 1073 | if (is_netdev) { |
f6b134ac MM |
1074 | if (strcmp(type, "tap") != 0 && |
1075 | #ifdef CONFIG_SLIRP | |
1076 | strcmp(type, "user") != 0 && | |
1077 | #endif | |
1078 | #ifdef CONFIG_VDE | |
1079 | strcmp(type, "vde") != 0 && | |
1080 | #endif | |
1081 | strcmp(type, "socket") != 0) { | |
5294e2c7 MA |
1082 | qerror_report(QERR_INVALID_PARAMETER_VALUE, "type", |
1083 | "a netdev backend type"); | |
f6b134ac MM |
1084 | return -1; |
1085 | } | |
1086 | ||
1087 | if (qemu_opt_get(opts, "vlan")) { | |
5294e2c7 | 1088 | qerror_report(QERR_INVALID_PARAMETER, "vlan"); |
f6b134ac MM |
1089 | return -1; |
1090 | } | |
1091 | if (qemu_opt_get(opts, "name")) { | |
5294e2c7 | 1092 | qerror_report(QERR_INVALID_PARAMETER, "name"); |
f6b134ac MM |
1093 | return -1; |
1094 | } | |
1095 | if (!qemu_opts_id(opts)) { | |
5294e2c7 | 1096 | qerror_report(QERR_MISSING_PARAMETER, "id"); |
f6b134ac MM |
1097 | return -1; |
1098 | } | |
1099 | } | |
1100 | ||
6d952ebe MM |
1101 | name = qemu_opts_id(opts); |
1102 | if (!name) { | |
1103 | name = qemu_opt_get(opts, "name"); | |
1104 | } | |
1105 | ||
f83c6e10 MM |
1106 | for (i = 0; net_client_types[i].type != NULL; i++) { |
1107 | if (!strcmp(net_client_types[i].type, type)) { | |
f6b134ac | 1108 | VLANState *vlan = NULL; |
50e32ea8 | 1109 | int ret; |
f6b134ac | 1110 | |
f83c6e10 MM |
1111 | if (qemu_opts_validate(opts, &net_client_types[i].desc[0]) == -1) { |
1112 | return -1; | |
1113 | } | |
1114 | ||
5869c4d5 MM |
1115 | /* Do not add to a vlan if it's a -netdev or a nic with a |
1116 | * netdev= parameter. */ | |
1117 | if (!(is_netdev || | |
1118 | (strcmp(type, "nic") == 0 && qemu_opt_get(opts, "netdev")))) { | |
f6b134ac MM |
1119 | vlan = qemu_find_vlan(qemu_opt_get_number(opts, "vlan", 0), 1); |
1120 | } | |
1121 | ||
50e32ea8 | 1122 | ret = -1; |
f83c6e10 | 1123 | if (net_client_types[i].init) { |
50e32ea8 AS |
1124 | ret = net_client_types[i].init(opts, mon, name, vlan); |
1125 | if (ret < 0) { | |
5294e2c7 MA |
1126 | /* TODO push error reporting into init() methods */ |
1127 | qerror_report(QERR_DEVICE_INIT_FAILED, type); | |
1128 | return -1; | |
1129 | } | |
f83c6e10 | 1130 | } |
50e32ea8 | 1131 | return ret; |
f83c6e10 MM |
1132 | } |
1133 | } | |
1134 | ||
5294e2c7 MA |
1135 | qerror_report(QERR_INVALID_PARAMETER_VALUE, "type", |
1136 | "a network client type"); | |
f83c6e10 MM |
1137 | return -1; |
1138 | } | |
1139 | ||
6f338c34 AL |
1140 | static int net_host_check_device(const char *device) |
1141 | { | |
1142 | int i; | |
bb9ea79e | 1143 | const char *valid_param_list[] = { "tap", "socket", "dump" |
6f338c34 AL |
1144 | #ifdef CONFIG_SLIRP |
1145 | ,"user" | |
1146 | #endif | |
1147 | #ifdef CONFIG_VDE | |
1148 | ,"vde" | |
1149 | #endif | |
1150 | }; | |
1151 | for (i = 0; i < sizeof(valid_param_list) / sizeof(char *); i++) { | |
1152 | if (!strncmp(valid_param_list[i], device, | |
1153 | strlen(valid_param_list[i]))) | |
1154 | return 1; | |
1155 | } | |
1156 | ||
1157 | return 0; | |
1158 | } | |
1159 | ||
f18c16de | 1160 | void net_host_device_add(Monitor *mon, const QDict *qdict) |
6f338c34 | 1161 | { |
f18c16de | 1162 | const char *device = qdict_get_str(qdict, "device"); |
7f1c9d20 MM |
1163 | const char *opts_str = qdict_get_try_str(qdict, "opts"); |
1164 | QemuOpts *opts; | |
f18c16de | 1165 | |
6f338c34 | 1166 | if (!net_host_check_device(device)) { |
376253ec | 1167 | monitor_printf(mon, "invalid host network device %s\n", device); |
6f338c34 AL |
1168 | return; |
1169 | } | |
7f1c9d20 | 1170 | |
8212c64f | 1171 | opts = qemu_opts_parse(&qemu_net_opts, opts_str ? opts_str : "", 0); |
7f1c9d20 | 1172 | if (!opts) { |
7f1c9d20 MM |
1173 | return; |
1174 | } | |
1175 | ||
1176 | qemu_opt_set(opts, "type", device); | |
1177 | ||
f6b134ac | 1178 | if (net_client_init(mon, opts, 0) < 0) { |
5c8be678 AL |
1179 | monitor_printf(mon, "adding host network device %s failed\n", device); |
1180 | } | |
6f338c34 AL |
1181 | } |
1182 | ||
f18c16de | 1183 | void net_host_device_remove(Monitor *mon, const QDict *qdict) |
6f338c34 | 1184 | { |
6f338c34 | 1185 | VLANClientState *vc; |
f18c16de LC |
1186 | int vlan_id = qdict_get_int(qdict, "vlan_id"); |
1187 | const char *device = qdict_get_str(qdict, "device"); | |
6f338c34 | 1188 | |
1a609520 | 1189 | vc = qemu_find_vlan_client_by_name(mon, vlan_id, device); |
6f338c34 | 1190 | if (!vc) { |
6f338c34 AL |
1191 | return; |
1192 | } | |
e8f1f9db AL |
1193 | if (!net_host_check_device(vc->model)) { |
1194 | monitor_printf(mon, "invalid host network device %s\n", device); | |
1195 | return; | |
1196 | } | |
6f338c34 AL |
1197 | qemu_del_vlan_client(vc); |
1198 | } | |
1199 | ||
ae82d324 MA |
1200 | int do_netdev_add(Monitor *mon, const QDict *qdict, QObject **ret_data) |
1201 | { | |
1202 | QemuOpts *opts; | |
1203 | int res; | |
1204 | ||
1205 | opts = qemu_opts_from_qdict(&qemu_netdev_opts, qdict); | |
1206 | if (!opts) { | |
1207 | return -1; | |
1208 | } | |
1209 | ||
1210 | res = net_client_init(mon, opts, 1); | |
1211 | return res; | |
1212 | } | |
1213 | ||
ae82d324 MA |
1214 | int do_netdev_del(Monitor *mon, const QDict *qdict, QObject **ret_data) |
1215 | { | |
1216 | const char *id = qdict_get_str(qdict, "id"); | |
1217 | VLANClientState *vc; | |
1218 | ||
1219 | vc = qemu_find_netdev(id); | |
1220 | if (!vc || vc->info->type == NET_CLIENT_TYPE_NIC) { | |
1221 | qerror_report(QERR_DEVICE_NOT_FOUND, id); | |
1222 | return -1; | |
1223 | } | |
ae82d324 MA |
1224 | qemu_del_vlan_client(vc); |
1225 | qemu_opts_del(qemu_opts_find(&qemu_netdev_opts, id)); | |
1226 | return 0; | |
1227 | } | |
1228 | ||
376253ec | 1229 | void do_info_network(Monitor *mon) |
63a01ef8 AL |
1230 | { |
1231 | VLANState *vlan; | |
a0104e0e | 1232 | VLANClientState *vc; |
63a01ef8 | 1233 | |
5610c3aa | 1234 | QTAILQ_FOREACH(vlan, &vlans, next) { |
376253ec | 1235 | monitor_printf(mon, "VLAN %d devices:\n", vlan->id); |
5610c3aa MM |
1236 | |
1237 | QTAILQ_FOREACH(vc, &vlan->clients, next) { | |
376253ec | 1238 | monitor_printf(mon, " %s: %s\n", vc->name, vc->info_str); |
5610c3aa | 1239 | } |
63a01ef8 | 1240 | } |
a0104e0e MA |
1241 | monitor_printf(mon, "Devices not on any VLAN:\n"); |
1242 | QTAILQ_FOREACH(vc, &non_vlan_clients, next) { | |
1243 | monitor_printf(mon, " %s: %s", vc->name, vc->info_str); | |
1244 | if (vc->peer) { | |
1245 | monitor_printf(mon, " peer=%s", vc->peer->name); | |
1246 | } | |
1247 | monitor_printf(mon, "\n"); | |
1248 | } | |
63a01ef8 AL |
1249 | } |
1250 | ||
5369e3c0 | 1251 | int do_set_link(Monitor *mon, const QDict *qdict, QObject **ret_data) |
436e5e53 AL |
1252 | { |
1253 | VLANState *vlan; | |
1254 | VLANClientState *vc = NULL; | |
f18c16de | 1255 | const char *name = qdict_get_str(qdict, "name"); |
c9b26a4c | 1256 | int up = qdict_get_bool(qdict, "up"); |
436e5e53 | 1257 | |
5610c3aa MM |
1258 | QTAILQ_FOREACH(vlan, &vlans, next) { |
1259 | QTAILQ_FOREACH(vc, &vlan->clients, next) { | |
1260 | if (strcmp(vc->name, name) == 0) { | |
dd5de373 | 1261 | goto done; |
5610c3aa MM |
1262 | } |
1263 | } | |
1264 | } | |
2583ba97 | 1265 | vc = qemu_find_netdev(name); |
dd5de373 | 1266 | done: |
436e5e53 AL |
1267 | |
1268 | if (!vc) { | |
5369e3c0 MA |
1269 | qerror_report(QERR_DEVICE_NOT_FOUND, name); |
1270 | return -1; | |
436e5e53 AL |
1271 | } |
1272 | ||
c9b26a4c | 1273 | vc->link_down = !up; |
436e5e53 | 1274 | |
665a3b07 MM |
1275 | if (vc->info->link_status_changed) { |
1276 | vc->info->link_status_changed(vc); | |
1277 | } | |
5369e3c0 | 1278 | return 0; |
436e5e53 AL |
1279 | } |
1280 | ||
63a01ef8 AL |
1281 | void net_cleanup(void) |
1282 | { | |
1283 | VLANState *vlan; | |
577c4af9 | 1284 | VLANClientState *vc, *next_vc; |
63a01ef8 | 1285 | |
5610c3aa | 1286 | QTAILQ_FOREACH(vlan, &vlans, next) { |
5610c3aa | 1287 | QTAILQ_FOREACH_SAFE(vc, &vlan->clients, next, next_vc) { |
b946a153 | 1288 | qemu_del_vlan_client(vc); |
63a01ef8 AL |
1289 | } |
1290 | } | |
577c4af9 MM |
1291 | |
1292 | QTAILQ_FOREACH_SAFE(vc, &non_vlan_clients, next, next_vc) { | |
1293 | qemu_del_vlan_client(vc); | |
1294 | } | |
63a01ef8 AL |
1295 | } |
1296 | ||
668680f7 | 1297 | void net_check_clients(void) |
63a01ef8 AL |
1298 | { |
1299 | VLANState *vlan; | |
62112d18 | 1300 | VLANClientState *vc; |
64e69d50 | 1301 | int has_nic = 0, has_host_dev = 0; |
63a01ef8 | 1302 | |
5610c3aa | 1303 | QTAILQ_FOREACH(vlan, &vlans, next) { |
62112d18 MA |
1304 | QTAILQ_FOREACH(vc, &vlan->clients, next) { |
1305 | switch (vc->info->type) { | |
1306 | case NET_CLIENT_TYPE_NIC: | |
1307 | has_nic = 1; | |
1308 | break; | |
1309 | case NET_CLIENT_TYPE_SLIRP: | |
1310 | case NET_CLIENT_TYPE_TAP: | |
1311 | case NET_CLIENT_TYPE_SOCKET: | |
1312 | case NET_CLIENT_TYPE_VDE: | |
1313 | has_host_dev = 1; | |
1314 | break; | |
1315 | default: ; | |
1316 | } | |
1317 | } | |
1318 | if (has_host_dev && !has_nic) | |
63a01ef8 | 1319 | fprintf(stderr, "Warning: vlan %d with no nics\n", vlan->id); |
62112d18 | 1320 | if (has_nic && !has_host_dev) |
63a01ef8 AL |
1321 | fprintf(stderr, |
1322 | "Warning: vlan %d is not connected to host network\n", | |
1323 | vlan->id); | |
1324 | } | |
efe32fdd MA |
1325 | QTAILQ_FOREACH(vc, &non_vlan_clients, next) { |
1326 | if (!vc->peer) { | |
1327 | fprintf(stderr, "Warning: %s %s has no peer\n", | |
1328 | vc->info->type == NET_CLIENT_TYPE_NIC ? "nic" : "netdev", | |
1329 | vc->name); | |
1330 | } | |
1331 | } | |
63a01ef8 | 1332 | } |
dc1c9fe8 MM |
1333 | |
1334 | static int net_init_client(QemuOpts *opts, void *dummy) | |
1335 | { | |
c1671a08 MM |
1336 | if (net_client_init(NULL, opts, 0) < 0) |
1337 | return -1; | |
1338 | return 0; | |
f6b134ac MM |
1339 | } |
1340 | ||
1341 | static int net_init_netdev(QemuOpts *opts, void *dummy) | |
1342 | { | |
1343 | return net_client_init(NULL, opts, 1); | |
dc1c9fe8 MM |
1344 | } |
1345 | ||
1346 | int net_init_clients(void) | |
1347 | { | |
cb4522cc | 1348 | if (default_net) { |
dc1c9fe8 MM |
1349 | /* if no clients, we use a default config */ |
1350 | qemu_opts_set(&qemu_net_opts, NULL, "type", "nic"); | |
1351 | #ifdef CONFIG_SLIRP | |
1352 | qemu_opts_set(&qemu_net_opts, NULL, "type", "user"); | |
1353 | #endif | |
1354 | } | |
1355 | ||
5610c3aa | 1356 | QTAILQ_INIT(&vlans); |
577c4af9 | 1357 | QTAILQ_INIT(&non_vlan_clients); |
5610c3aa | 1358 | |
f6b134ac MM |
1359 | if (qemu_opts_foreach(&qemu_netdev_opts, net_init_netdev, NULL, 1) == -1) |
1360 | return -1; | |
1361 | ||
dc1c9fe8 MM |
1362 | if (qemu_opts_foreach(&qemu_net_opts, net_init_client, NULL, 1) == -1) { |
1363 | return -1; | |
1364 | } | |
1365 | ||
dc1c9fe8 MM |
1366 | return 0; |
1367 | } | |
1368 | ||
7f161aae | 1369 | int net_client_parse(QemuOptsList *opts_list, const char *optarg) |
dc1c9fe8 | 1370 | { |
a3a766e7 | 1371 | #if defined(CONFIG_SLIRP) |
68ac40d2 MM |
1372 | int ret; |
1373 | if (net_slirp_parse_legacy(opts_list, optarg, &ret)) { | |
dc1c9fe8 MM |
1374 | return ret; |
1375 | } | |
a3a766e7 | 1376 | #endif |
68ac40d2 | 1377 | |
8212c64f | 1378 | if (!qemu_opts_parse(opts_list, optarg, 1)) { |
dc1c9fe8 MM |
1379 | return -1; |
1380 | } | |
1381 | ||
cb4522cc | 1382 | default_net = 0; |
dc1c9fe8 MM |
1383 | return 0; |
1384 | } |