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