]>
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 | */ | |
d40cdb10 BS |
24 | #include "config-host.h" |
25 | ||
1422e32d | 26 | #include "net/net.h" |
fd9400b3 PB |
27 | #include "clients.h" |
28 | #include "hub.h" | |
1422e32d | 29 | #include "net/slirp.h" |
d60b20cf | 30 | #include "net/eth.h" |
fd9400b3 | 31 | #include "util.h" |
a245fc18 | 32 | |
83c9089e | 33 | #include "monitor/monitor.h" |
1df49e04 | 34 | #include "qemu-common.h" |
cc7a8ea7 | 35 | #include "qapi/qmp/qerror.h" |
d49b6836 | 36 | #include "qemu/error-report.h" |
1de7afc9 PB |
37 | #include "qemu/sockets.h" |
38 | #include "qemu/config-file.h" | |
4b37156c | 39 | #include "qmp-commands.h" |
75422b0d | 40 | #include "hw/qdev.h" |
1de7afc9 | 41 | #include "qemu/iov.h" |
6a1751b7 | 42 | #include "qemu/main-loop.h" |
6687b79d LE |
43 | #include "qapi-visit.h" |
44 | #include "qapi/opts-visitor.h" | |
7b1b5d19 | 45 | #include "qapi/dealloc-visitor.h" |
e1d64c08 | 46 | #include "sysemu/sysemu.h" |
fdccce45 | 47 | #include "net/filter.h" |
511d2b14 | 48 | |
2944e4ec SW |
49 | /* Net bridge is currently not supported for W32. */ |
50 | #if !defined(_WIN32) | |
51 | # define CONFIG_NET_BRIDGE | |
52 | #endif | |
53 | ||
ca77d85e | 54 | static VMChangeStateEntry *net_change_state_entry; |
4e68f7a0 | 55 | static QTAILQ_HEAD(, NetClientState) net_clients; |
63a01ef8 | 56 | |
84007e81 HB |
57 | const char *host_net_devices[] = { |
58 | "tap", | |
59 | "socket", | |
60 | "dump", | |
61 | #ifdef CONFIG_NET_BRIDGE | |
62 | "bridge", | |
63 | #endif | |
027a247b SH |
64 | #ifdef CONFIG_NETMAP |
65 | "netmap", | |
66 | #endif | |
84007e81 HB |
67 | #ifdef CONFIG_SLIRP |
68 | "user", | |
69 | #endif | |
70 | #ifdef CONFIG_VDE | |
71 | "vde", | |
72 | #endif | |
03ce5744 | 73 | "vhost-user", |
84007e81 HB |
74 | NULL, |
75 | }; | |
76 | ||
cb4522cc GH |
77 | int default_net = 1; |
78 | ||
63a01ef8 AL |
79 | /***********************************************************/ |
80 | /* network device redirectors */ | |
81 | ||
68ac40d2 | 82 | #if defined(DEBUG_NET) |
63a01ef8 AL |
83 | static void hex_dump(FILE *f, const uint8_t *buf, int size) |
84 | { | |
85 | int len, i, j, c; | |
86 | ||
87 | for(i=0;i<size;i+=16) { | |
88 | len = size - i; | |
89 | if (len > 16) | |
90 | len = 16; | |
91 | fprintf(f, "%08x ", i); | |
92 | for(j=0;j<16;j++) { | |
93 | if (j < len) | |
94 | fprintf(f, " %02x", buf[i+j]); | |
95 | else | |
96 | fprintf(f, " "); | |
97 | } | |
98 | fprintf(f, " "); | |
99 | for(j=0;j<len;j++) { | |
100 | c = buf[i+j]; | |
101 | if (c < ' ' || c > '~') | |
102 | c = '.'; | |
103 | fprintf(f, "%c", c); | |
104 | } | |
105 | fprintf(f, "\n"); | |
106 | } | |
107 | } | |
108 | #endif | |
109 | ||
63a01ef8 AL |
110 | static int get_str_sep(char *buf, int buf_size, const char **pp, int sep) |
111 | { | |
112 | const char *p, *p1; | |
113 | int len; | |
114 | p = *pp; | |
115 | p1 = strchr(p, sep); | |
116 | if (!p1) | |
117 | return -1; | |
118 | len = p1 - p; | |
119 | p1++; | |
120 | if (buf_size > 0) { | |
121 | if (len > buf_size - 1) | |
122 | len = buf_size - 1; | |
123 | memcpy(buf, p, len); | |
124 | buf[len] = '\0'; | |
125 | } | |
126 | *pp = p1; | |
127 | return 0; | |
128 | } | |
129 | ||
63a01ef8 AL |
130 | int parse_host_port(struct sockaddr_in *saddr, const char *str) |
131 | { | |
132 | char buf[512]; | |
133 | struct hostent *he; | |
134 | const char *p, *r; | |
135 | int port; | |
136 | ||
137 | p = str; | |
138 | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) | |
139 | return -1; | |
140 | saddr->sin_family = AF_INET; | |
141 | if (buf[0] == '\0') { | |
142 | saddr->sin_addr.s_addr = 0; | |
143 | } else { | |
cd390083 | 144 | if (qemu_isdigit(buf[0])) { |
63a01ef8 AL |
145 | if (!inet_aton(buf, &saddr->sin_addr)) |
146 | return -1; | |
147 | } else { | |
148 | if ((he = gethostbyname(buf)) == NULL) | |
149 | return - 1; | |
150 | saddr->sin_addr = *(struct in_addr *)he->h_addr; | |
151 | } | |
152 | } | |
153 | port = strtol(p, (char **)&r, 0); | |
154 | if (r == p) | |
155 | return -1; | |
156 | saddr->sin_port = htons(port); | |
157 | return 0; | |
158 | } | |
159 | ||
890ee6ab SF |
160 | char *qemu_mac_strdup_printf(const uint8_t *macaddr) |
161 | { | |
162 | return g_strdup_printf("%.2x:%.2x:%.2x:%.2x:%.2x:%.2x", | |
163 | macaddr[0], macaddr[1], macaddr[2], | |
164 | macaddr[3], macaddr[4], macaddr[5]); | |
165 | } | |
166 | ||
35277d14 | 167 | void qemu_format_nic_info_str(NetClientState *nc, uint8_t macaddr[6]) |
7cb7434b | 168 | { |
35277d14 | 169 | snprintf(nc->info_str, sizeof(nc->info_str), |
4dda4063 | 170 | "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x", |
35277d14 | 171 | nc->model, |
7cb7434b AL |
172 | macaddr[0], macaddr[1], macaddr[2], |
173 | macaddr[3], macaddr[4], macaddr[5]); | |
174 | } | |
175 | ||
2bc22a58 SZ |
176 | static int mac_table[256] = {0}; |
177 | ||
178 | static void qemu_macaddr_set_used(MACAddr *macaddr) | |
179 | { | |
180 | int index; | |
181 | ||
182 | for (index = 0x56; index < 0xFF; index++) { | |
183 | if (macaddr->a[5] == index) { | |
184 | mac_table[index]++; | |
185 | } | |
186 | } | |
187 | } | |
188 | ||
189 | static void qemu_macaddr_set_free(MACAddr *macaddr) | |
190 | { | |
191 | int index; | |
192 | static const MACAddr base = { .a = { 0x52, 0x54, 0x00, 0x12, 0x34, 0 } }; | |
193 | ||
194 | if (memcmp(macaddr->a, &base.a, (sizeof(base.a) - 1)) != 0) { | |
195 | return; | |
196 | } | |
197 | for (index = 0x56; index < 0xFF; index++) { | |
198 | if (macaddr->a[5] == index) { | |
199 | mac_table[index]--; | |
200 | } | |
201 | } | |
202 | } | |
203 | ||
204 | static int qemu_macaddr_get_free(void) | |
205 | { | |
206 | int index; | |
207 | ||
208 | for (index = 0x56; index < 0xFF; index++) { | |
209 | if (mac_table[index] == 0) { | |
210 | return index; | |
211 | } | |
212 | } | |
213 | ||
214 | return -1; | |
215 | } | |
216 | ||
76d32cba GH |
217 | void qemu_macaddr_default_if_unset(MACAddr *macaddr) |
218 | { | |
76d32cba | 219 | static const MACAddr zero = { .a = { 0,0,0,0,0,0 } }; |
2bc22a58 SZ |
220 | static const MACAddr base = { .a = { 0x52, 0x54, 0x00, 0x12, 0x34, 0 } }; |
221 | ||
222 | if (memcmp(macaddr, &zero, sizeof(zero)) != 0) { | |
223 | if (memcmp(macaddr->a, &base.a, (sizeof(base.a) - 1)) != 0) { | |
224 | return; | |
225 | } else { | |
226 | qemu_macaddr_set_used(macaddr); | |
227 | return; | |
228 | } | |
229 | } | |
76d32cba | 230 | |
76d32cba GH |
231 | macaddr->a[0] = 0x52; |
232 | macaddr->a[1] = 0x54; | |
233 | macaddr->a[2] = 0x00; | |
234 | macaddr->a[3] = 0x12; | |
235 | macaddr->a[4] = 0x34; | |
2bc22a58 SZ |
236 | macaddr->a[5] = qemu_macaddr_get_free(); |
237 | qemu_macaddr_set_used(macaddr); | |
76d32cba GH |
238 | } |
239 | ||
d33d93b2 SH |
240 | /** |
241 | * Generate a name for net client | |
242 | * | |
c963530a | 243 | * Only net clients created with the legacy -net option and NICs need this. |
d33d93b2 | 244 | */ |
35277d14 | 245 | static char *assign_name(NetClientState *nc1, const char *model) |
676cff29 | 246 | { |
35277d14 | 247 | NetClientState *nc; |
676cff29 AL |
248 | int id = 0; |
249 | ||
35277d14 SH |
250 | QTAILQ_FOREACH(nc, &net_clients, next) { |
251 | if (nc == nc1) { | |
d33d93b2 | 252 | continue; |
5610c3aa | 253 | } |
c963530a | 254 | if (strcmp(nc->model, model) == 0) { |
53e51d85 MA |
255 | id++; |
256 | } | |
257 | } | |
258 | ||
4bf2c138 | 259 | return g_strdup_printf("%s.%d", model, id); |
676cff29 AL |
260 | } |
261 | ||
f7860455 JW |
262 | static void qemu_net_client_destructor(NetClientState *nc) |
263 | { | |
264 | g_free(nc); | |
265 | } | |
266 | ||
18a1541a JW |
267 | static void qemu_net_client_setup(NetClientState *nc, |
268 | NetClientInfo *info, | |
269 | NetClientState *peer, | |
270 | const char *model, | |
f7860455 JW |
271 | const char *name, |
272 | NetClientDestructor *destructor) | |
63a01ef8 | 273 | { |
35277d14 SH |
274 | nc->info = info; |
275 | nc->model = g_strdup(model); | |
45460d1a | 276 | if (name) { |
35277d14 | 277 | nc->name = g_strdup(name); |
45460d1a | 278 | } else { |
35277d14 | 279 | nc->name = assign_name(nc, model); |
45460d1a | 280 | } |
5610c3aa | 281 | |
ab5f3f84 SH |
282 | if (peer) { |
283 | assert(!peer->peer); | |
35277d14 SH |
284 | nc->peer = peer; |
285 | peer->peer = nc; | |
d80b9fc6 | 286 | } |
35277d14 | 287 | QTAILQ_INSERT_TAIL(&net_clients, nc, next); |
63a01ef8 | 288 | |
067404be | 289 | nc->incoming_queue = qemu_new_net_queue(nc); |
f7860455 | 290 | nc->destructor = destructor; |
fdccce45 | 291 | QTAILQ_INIT(&nc->filters); |
18a1541a JW |
292 | } |
293 | ||
294 | NetClientState *qemu_new_net_client(NetClientInfo *info, | |
295 | NetClientState *peer, | |
296 | const char *model, | |
297 | const char *name) | |
298 | { | |
299 | NetClientState *nc; | |
300 | ||
301 | assert(info->size >= sizeof(NetClientState)); | |
302 | ||
303 | nc = g_malloc0(info->size); | |
f7860455 JW |
304 | qemu_net_client_setup(nc, info, peer, model, name, |
305 | qemu_net_client_destructor); | |
18a1541a | 306 | |
35277d14 | 307 | return nc; |
63a01ef8 AL |
308 | } |
309 | ||
ebef2c09 MM |
310 | NICState *qemu_new_nic(NetClientInfo *info, |
311 | NICConf *conf, | |
312 | const char *model, | |
313 | const char *name, | |
314 | void *opaque) | |
315 | { | |
1ceef9f2 | 316 | NetClientState **peers = conf->peers.ncs; |
ebef2c09 | 317 | NICState *nic; |
575a1c0e | 318 | int i, queues = MAX(1, conf->peers.queues); |
ebef2c09 | 319 | |
2be64a68 | 320 | assert(info->type == NET_CLIENT_OPTIONS_KIND_NIC); |
ebef2c09 MM |
321 | assert(info->size >= sizeof(NICState)); |
322 | ||
f6b26cf2 JW |
323 | nic = g_malloc0(info->size + sizeof(NetClientState) * queues); |
324 | nic->ncs = (void *)nic + info->size; | |
ebef2c09 MM |
325 | nic->conf = conf; |
326 | nic->opaque = opaque; | |
327 | ||
f6b26cf2 JW |
328 | for (i = 0; i < queues; i++) { |
329 | qemu_net_client_setup(&nic->ncs[i], info, peers[i], model, name, | |
1ceef9f2 JW |
330 | NULL); |
331 | nic->ncs[i].queue_index = i; | |
332 | } | |
333 | ||
ebef2c09 MM |
334 | return nic; |
335 | } | |
336 | ||
1ceef9f2 JW |
337 | NetClientState *qemu_get_subqueue(NICState *nic, int queue_index) |
338 | { | |
f6b26cf2 | 339 | return nic->ncs + queue_index; |
1ceef9f2 JW |
340 | } |
341 | ||
b356f76d JW |
342 | NetClientState *qemu_get_queue(NICState *nic) |
343 | { | |
1ceef9f2 | 344 | return qemu_get_subqueue(nic, 0); |
b356f76d JW |
345 | } |
346 | ||
cc1f0f45 JW |
347 | NICState *qemu_get_nic(NetClientState *nc) |
348 | { | |
1ceef9f2 JW |
349 | NetClientState *nc0 = nc - nc->queue_index; |
350 | ||
f6b26cf2 | 351 | return (NICState *)((void *)nc0 - nc->info->size); |
cc1f0f45 JW |
352 | } |
353 | ||
354 | void *qemu_get_nic_opaque(NetClientState *nc) | |
355 | { | |
356 | NICState *nic = qemu_get_nic(nc); | |
357 | ||
358 | return nic->opaque; | |
359 | } | |
360 | ||
b20c6b9e | 361 | static void qemu_cleanup_net_client(NetClientState *nc) |
63a01ef8 | 362 | { |
35277d14 | 363 | QTAILQ_REMOVE(&net_clients, nc, next); |
63a01ef8 | 364 | |
cc2a9043 AF |
365 | if (nc->info->cleanup) { |
366 | nc->info->cleanup(nc); | |
367 | } | |
a083a89d | 368 | } |
5610c3aa | 369 | |
b20c6b9e | 370 | static void qemu_free_net_client(NetClientState *nc) |
a083a89d | 371 | { |
067404be JK |
372 | if (nc->incoming_queue) { |
373 | qemu_del_net_queue(nc->incoming_queue); | |
a005d073 | 374 | } |
35277d14 SH |
375 | if (nc->peer) { |
376 | nc->peer->peer = NULL; | |
a083a89d | 377 | } |
35277d14 SH |
378 | g_free(nc->name); |
379 | g_free(nc->model); | |
f7860455 JW |
380 | if (nc->destructor) { |
381 | nc->destructor(nc); | |
382 | } | |
63a01ef8 AL |
383 | } |
384 | ||
b20c6b9e | 385 | void qemu_del_net_client(NetClientState *nc) |
a083a89d | 386 | { |
1ceef9f2 JW |
387 | NetClientState *ncs[MAX_QUEUE_NUM]; |
388 | int queues, i; | |
fdccce45 | 389 | NetFilterState *nf, *next; |
1ceef9f2 | 390 | |
7fb43911 PB |
391 | assert(nc->info->type != NET_CLIENT_OPTIONS_KIND_NIC); |
392 | ||
1ceef9f2 JW |
393 | /* If the NetClientState belongs to a multiqueue backend, we will change all |
394 | * other NetClientStates also. | |
395 | */ | |
396 | queues = qemu_find_net_clients_except(nc->name, ncs, | |
397 | NET_CLIENT_OPTIONS_KIND_NIC, | |
398 | MAX_QUEUE_NUM); | |
399 | assert(queues != 0); | |
400 | ||
fdccce45 YH |
401 | QTAILQ_FOREACH_SAFE(nf, &nc->filters, next, next) { |
402 | object_unparent(OBJECT(nf)); | |
403 | } | |
404 | ||
a083a89d | 405 | /* If there is a peer NIC, delete and cleanup client, but do not free. */ |
35277d14 | 406 | if (nc->peer && nc->peer->info->type == NET_CLIENT_OPTIONS_KIND_NIC) { |
cc1f0f45 | 407 | NICState *nic = qemu_get_nic(nc->peer); |
a083a89d MT |
408 | if (nic->peer_deleted) { |
409 | return; | |
410 | } | |
411 | nic->peer_deleted = true; | |
1ceef9f2 JW |
412 | |
413 | for (i = 0; i < queues; i++) { | |
414 | ncs[i]->peer->link_down = true; | |
415 | } | |
416 | ||
35277d14 SH |
417 | if (nc->peer->info->link_status_changed) { |
418 | nc->peer->info->link_status_changed(nc->peer); | |
a083a89d | 419 | } |
1ceef9f2 JW |
420 | |
421 | for (i = 0; i < queues; i++) { | |
422 | qemu_cleanup_net_client(ncs[i]); | |
423 | } | |
424 | ||
a083a89d MT |
425 | return; |
426 | } | |
427 | ||
1ceef9f2 JW |
428 | for (i = 0; i < queues; i++) { |
429 | qemu_cleanup_net_client(ncs[i]); | |
430 | qemu_free_net_client(ncs[i]); | |
431 | } | |
948ecf21 JW |
432 | } |
433 | ||
434 | void qemu_del_nic(NICState *nic) | |
435 | { | |
575a1c0e | 436 | int i, queues = MAX(nic->conf->peers.queues, 1); |
1ceef9f2 | 437 | |
2bc22a58 SZ |
438 | qemu_macaddr_set_free(&nic->conf->macaddr); |
439 | ||
a083a89d | 440 | /* If this is a peer NIC and peer has already been deleted, free it now. */ |
1ceef9f2 JW |
441 | if (nic->peer_deleted) { |
442 | for (i = 0; i < queues; i++) { | |
443 | qemu_free_net_client(qemu_get_subqueue(nic, i)->peer); | |
1a609520 JK |
444 | } |
445 | } | |
1a609520 | 446 | |
1ceef9f2 JW |
447 | for (i = queues - 1; i >= 0; i--) { |
448 | NetClientState *nc = qemu_get_subqueue(nic, i); | |
449 | ||
450 | qemu_cleanup_net_client(nc); | |
451 | qemu_free_net_client(nc); | |
452 | } | |
f6b26cf2 JW |
453 | |
454 | g_free(nic); | |
1a609520 JK |
455 | } |
456 | ||
57f9ef17 MM |
457 | void qemu_foreach_nic(qemu_nic_foreach func, void *opaque) |
458 | { | |
4e68f7a0 | 459 | NetClientState *nc; |
57f9ef17 | 460 | |
94878994 | 461 | QTAILQ_FOREACH(nc, &net_clients, next) { |
2be64a68 | 462 | if (nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC) { |
1ceef9f2 JW |
463 | if (nc->queue_index == 0) { |
464 | func(qemu_get_nic(nc), opaque); | |
465 | } | |
57f9ef17 MM |
466 | } |
467 | } | |
57f9ef17 MM |
468 | } |
469 | ||
d6085e3a | 470 | bool qemu_has_ufo(NetClientState *nc) |
1f55ac45 | 471 | { |
d6085e3a | 472 | if (!nc || !nc->info->has_ufo) { |
1f55ac45 VM |
473 | return false; |
474 | } | |
475 | ||
d6085e3a | 476 | return nc->info->has_ufo(nc); |
1f55ac45 VM |
477 | } |
478 | ||
d6085e3a | 479 | bool qemu_has_vnet_hdr(NetClientState *nc) |
1f55ac45 | 480 | { |
d6085e3a | 481 | if (!nc || !nc->info->has_vnet_hdr) { |
1f55ac45 VM |
482 | return false; |
483 | } | |
484 | ||
d6085e3a | 485 | return nc->info->has_vnet_hdr(nc); |
1f55ac45 VM |
486 | } |
487 | ||
d6085e3a | 488 | bool qemu_has_vnet_hdr_len(NetClientState *nc, int len) |
1f55ac45 | 489 | { |
d6085e3a | 490 | if (!nc || !nc->info->has_vnet_hdr_len) { |
1f55ac45 VM |
491 | return false; |
492 | } | |
493 | ||
d6085e3a | 494 | return nc->info->has_vnet_hdr_len(nc, len); |
1f55ac45 VM |
495 | } |
496 | ||
d6085e3a | 497 | void qemu_using_vnet_hdr(NetClientState *nc, bool enable) |
1f55ac45 | 498 | { |
d6085e3a | 499 | if (!nc || !nc->info->using_vnet_hdr) { |
1f55ac45 VM |
500 | return; |
501 | } | |
502 | ||
d6085e3a | 503 | nc->info->using_vnet_hdr(nc, enable); |
1f55ac45 VM |
504 | } |
505 | ||
d6085e3a | 506 | void qemu_set_offload(NetClientState *nc, int csum, int tso4, int tso6, |
1f55ac45 VM |
507 | int ecn, int ufo) |
508 | { | |
d6085e3a | 509 | if (!nc || !nc->info->set_offload) { |
1f55ac45 VM |
510 | return; |
511 | } | |
512 | ||
d6085e3a | 513 | nc->info->set_offload(nc, csum, tso4, tso6, ecn, ufo); |
1f55ac45 VM |
514 | } |
515 | ||
d6085e3a | 516 | void qemu_set_vnet_hdr_len(NetClientState *nc, int len) |
1f55ac45 | 517 | { |
d6085e3a | 518 | if (!nc || !nc->info->set_vnet_hdr_len) { |
1f55ac45 VM |
519 | return; |
520 | } | |
521 | ||
d6085e3a | 522 | nc->info->set_vnet_hdr_len(nc, len); |
1f55ac45 VM |
523 | } |
524 | ||
c80cd6bb GK |
525 | int qemu_set_vnet_le(NetClientState *nc, bool is_le) |
526 | { | |
527 | if (!nc || !nc->info->set_vnet_le) { | |
528 | return -ENOSYS; | |
529 | } | |
530 | ||
531 | return nc->info->set_vnet_le(nc, is_le); | |
532 | } | |
533 | ||
534 | int qemu_set_vnet_be(NetClientState *nc, bool is_be) | |
535 | { | |
536 | if (!nc || !nc->info->set_vnet_be) { | |
537 | return -ENOSYS; | |
538 | } | |
539 | ||
540 | return nc->info->set_vnet_be(nc, is_be); | |
541 | } | |
542 | ||
4e68f7a0 | 543 | int qemu_can_send_packet(NetClientState *sender) |
63a01ef8 | 544 | { |
e1d64c08 HZ |
545 | int vm_running = runstate_is_running(); |
546 | ||
547 | if (!vm_running) { | |
548 | return 0; | |
549 | } | |
550 | ||
a005d073 | 551 | if (!sender->peer) { |
d80b9fc6 MM |
552 | return 1; |
553 | } | |
554 | ||
a005d073 SH |
555 | if (sender->peer->receive_disabled) { |
556 | return 0; | |
557 | } else if (sender->peer->info->can_receive && | |
558 | !sender->peer->info->can_receive(sender->peer)) { | |
559 | return 0; | |
63a01ef8 | 560 | } |
60c07d93 | 561 | return 1; |
63a01ef8 AL |
562 | } |
563 | ||
e64c770d YH |
564 | static ssize_t filter_receive_iov(NetClientState *nc, |
565 | NetFilterDirection direction, | |
566 | NetClientState *sender, | |
567 | unsigned flags, | |
568 | const struct iovec *iov, | |
569 | int iovcnt, | |
570 | NetPacketSent *sent_cb) | |
571 | { | |
572 | ssize_t ret = 0; | |
573 | NetFilterState *nf = NULL; | |
574 | ||
575 | QTAILQ_FOREACH(nf, &nc->filters, next) { | |
576 | ret = qemu_netfilter_receive(nf, direction, sender, flags, iov, | |
577 | iovcnt, sent_cb); | |
578 | if (ret) { | |
579 | return ret; | |
580 | } | |
581 | } | |
582 | ||
583 | return ret; | |
584 | } | |
585 | ||
586 | static ssize_t filter_receive(NetClientState *nc, | |
587 | NetFilterDirection direction, | |
588 | NetClientState *sender, | |
589 | unsigned flags, | |
590 | const uint8_t *data, | |
591 | size_t size, | |
592 | NetPacketSent *sent_cb) | |
593 | { | |
594 | struct iovec iov = { | |
595 | .iov_base = (void *)data, | |
596 | .iov_len = size | |
597 | }; | |
598 | ||
599 | return filter_receive_iov(nc, direction, sender, flags, &iov, 1, sent_cb); | |
600 | } | |
601 | ||
86a77c38 ZYW |
602 | ssize_t qemu_deliver_packet(NetClientState *sender, |
603 | unsigned flags, | |
604 | const uint8_t *data, | |
605 | size_t size, | |
606 | void *opaque) | |
9a6ecb30 | 607 | { |
35277d14 | 608 | NetClientState *nc = opaque; |
893379ef | 609 | ssize_t ret; |
9a6ecb30 | 610 | |
35277d14 | 611 | if (nc->link_down) { |
9a6ecb30 MM |
612 | return size; |
613 | } | |
614 | ||
35277d14 | 615 | if (nc->receive_disabled) { |
893379ef MM |
616 | return 0; |
617 | } | |
618 | ||
35277d14 SH |
619 | if (flags & QEMU_NET_PACKET_FLAG_RAW && nc->info->receive_raw) { |
620 | ret = nc->info->receive_raw(nc, data, size); | |
893379ef | 621 | } else { |
35277d14 | 622 | ret = nc->info->receive(nc, data, size); |
893379ef MM |
623 | } |
624 | ||
625 | if (ret == 0) { | |
35277d14 | 626 | nc->receive_disabled = 1; |
b9259652 | 627 | } |
893379ef MM |
628 | |
629 | return ret; | |
9a6ecb30 MM |
630 | } |
631 | ||
35277d14 | 632 | void qemu_purge_queued_packets(NetClientState *nc) |
8cad5516 | 633 | { |
35277d14 | 634 | if (!nc->peer) { |
d80b9fc6 | 635 | return; |
9a6ecb30 | 636 | } |
d80b9fc6 | 637 | |
067404be | 638 | qemu_net_queue_purge(nc->peer->incoming_queue, nc); |
8cad5516 MM |
639 | } |
640 | ||
ca77d85e MT |
641 | static |
642 | void qemu_flush_or_purge_queued_packets(NetClientState *nc, bool purge) | |
e94667b9 | 643 | { |
35277d14 | 644 | nc->receive_disabled = 0; |
9a6ecb30 | 645 | |
199ee608 LR |
646 | if (nc->peer && nc->peer->info->type == NET_CLIENT_OPTIONS_KIND_HUBPORT) { |
647 | if (net_hub_flush(nc->peer)) { | |
648 | qemu_notify_event(); | |
649 | } | |
199ee608 | 650 | } |
067404be | 651 | if (qemu_net_queue_flush(nc->incoming_queue)) { |
987a9b48 PB |
652 | /* We emptied the queue successfully, signal to the IO thread to repoll |
653 | * the file descriptor (for tap, for example). | |
654 | */ | |
655 | qemu_notify_event(); | |
ca77d85e MT |
656 | } else if (purge) { |
657 | /* Unable to empty the queue, purge remaining packets */ | |
658 | qemu_net_queue_purge(nc->incoming_queue, nc); | |
987a9b48 | 659 | } |
e94667b9 MM |
660 | } |
661 | ||
ca77d85e MT |
662 | void qemu_flush_queued_packets(NetClientState *nc) |
663 | { | |
664 | qemu_flush_or_purge_queued_packets(nc, false); | |
665 | } | |
666 | ||
4e68f7a0 | 667 | static ssize_t qemu_send_packet_async_with_flags(NetClientState *sender, |
ca77d175 MM |
668 | unsigned flags, |
669 | const uint8_t *buf, int size, | |
670 | NetPacketSent *sent_cb) | |
764a4d1d | 671 | { |
9a6ecb30 | 672 | NetQueue *queue; |
e64c770d | 673 | int ret; |
436e5e53 | 674 | |
63a01ef8 | 675 | #ifdef DEBUG_NET |
d80b9fc6 | 676 | printf("qemu_send_packet_async:\n"); |
63a01ef8 AL |
677 | hex_dump(stdout, buf, size); |
678 | #endif | |
f3b6c7fc | 679 | |
a005d073 | 680 | if (sender->link_down || !sender->peer) { |
9a6ecb30 MM |
681 | return size; |
682 | } | |
683 | ||
e64c770d YH |
684 | /* Let filters handle the packet first */ |
685 | ret = filter_receive(sender, NET_FILTER_DIRECTION_TX, | |
686 | sender, flags, buf, size, sent_cb); | |
687 | if (ret) { | |
688 | return ret; | |
689 | } | |
690 | ||
691 | ret = filter_receive(sender->peer, NET_FILTER_DIRECTION_RX, | |
692 | sender, flags, buf, size, sent_cb); | |
693 | if (ret) { | |
694 | return ret; | |
695 | } | |
696 | ||
067404be | 697 | queue = sender->peer->incoming_queue; |
9a6ecb30 | 698 | |
ca77d175 MM |
699 | return qemu_net_queue_send(queue, sender, flags, buf, size, sent_cb); |
700 | } | |
701 | ||
4e68f7a0 | 702 | ssize_t qemu_send_packet_async(NetClientState *sender, |
ca77d175 MM |
703 | const uint8_t *buf, int size, |
704 | NetPacketSent *sent_cb) | |
705 | { | |
706 | return qemu_send_packet_async_with_flags(sender, QEMU_NET_PACKET_FLAG_NONE, | |
707 | buf, size, sent_cb); | |
f3b6c7fc MM |
708 | } |
709 | ||
35277d14 | 710 | void qemu_send_packet(NetClientState *nc, const uint8_t *buf, int size) |
f3b6c7fc | 711 | { |
35277d14 | 712 | qemu_send_packet_async(nc, buf, size, NULL); |
63a01ef8 AL |
713 | } |
714 | ||
35277d14 | 715 | ssize_t qemu_send_packet_raw(NetClientState *nc, const uint8_t *buf, int size) |
ca77d175 | 716 | { |
35277d14 | 717 | return qemu_send_packet_async_with_flags(nc, QEMU_NET_PACKET_FLAG_RAW, |
ca77d175 MM |
718 | buf, size, NULL); |
719 | } | |
720 | ||
35277d14 | 721 | static ssize_t nc_sendv_compat(NetClientState *nc, const struct iovec *iov, |
fbe78f4f AL |
722 | int iovcnt) |
723 | { | |
d32fcad3 | 724 | uint8_t buffer[NET_BUFSIZE]; |
ce053661 | 725 | size_t offset; |
fbe78f4f | 726 | |
dcf6f5e1 | 727 | offset = iov_to_buf(iov, iovcnt, 0, buffer, sizeof(buffer)); |
fbe78f4f | 728 | |
35277d14 | 729 | return nc->info->receive(nc, buffer, offset); |
fbe78f4f AL |
730 | } |
731 | ||
86a77c38 ZYW |
732 | ssize_t qemu_deliver_packet_iov(NetClientState *sender, |
733 | unsigned flags, | |
734 | const struct iovec *iov, | |
735 | int iovcnt, | |
736 | void *opaque) | |
9a6ecb30 | 737 | { |
35277d14 | 738 | NetClientState *nc = opaque; |
c67f5dc1 | 739 | int ret; |
9a6ecb30 | 740 | |
35277d14 | 741 | if (nc->link_down) { |
ce053661 | 742 | return iov_size(iov, iovcnt); |
9a6ecb30 MM |
743 | } |
744 | ||
c67f5dc1 SH |
745 | if (nc->receive_disabled) { |
746 | return 0; | |
747 | } | |
748 | ||
35277d14 | 749 | if (nc->info->receive_iov) { |
c67f5dc1 | 750 | ret = nc->info->receive_iov(nc, iov, iovcnt); |
9a6ecb30 | 751 | } else { |
c67f5dc1 SH |
752 | ret = nc_sendv_compat(nc, iov, iovcnt); |
753 | } | |
754 | ||
755 | if (ret == 0) { | |
756 | nc->receive_disabled = 1; | |
e94667b9 | 757 | } |
c67f5dc1 SH |
758 | |
759 | return ret; | |
e94667b9 MM |
760 | } |
761 | ||
4e68f7a0 | 762 | ssize_t qemu_sendv_packet_async(NetClientState *sender, |
f3b6c7fc MM |
763 | const struct iovec *iov, int iovcnt, |
764 | NetPacketSent *sent_cb) | |
e94667b9 | 765 | { |
9a6ecb30 | 766 | NetQueue *queue; |
e64c770d | 767 | int ret; |
9a6ecb30 | 768 | |
a005d073 | 769 | if (sender->link_down || !sender->peer) { |
ce053661 | 770 | return iov_size(iov, iovcnt); |
e94667b9 MM |
771 | } |
772 | ||
e64c770d YH |
773 | /* Let filters handle the packet first */ |
774 | ret = filter_receive_iov(sender, NET_FILTER_DIRECTION_TX, sender, | |
775 | QEMU_NET_PACKET_FLAG_NONE, iov, iovcnt, sent_cb); | |
776 | if (ret) { | |
777 | return ret; | |
778 | } | |
779 | ||
780 | ret = filter_receive_iov(sender->peer, NET_FILTER_DIRECTION_RX, sender, | |
781 | QEMU_NET_PACKET_FLAG_NONE, iov, iovcnt, sent_cb); | |
782 | if (ret) { | |
783 | return ret; | |
784 | } | |
785 | ||
067404be | 786 | queue = sender->peer->incoming_queue; |
9a6ecb30 | 787 | |
c0b8e49c MM |
788 | return qemu_net_queue_send_iov(queue, sender, |
789 | QEMU_NET_PACKET_FLAG_NONE, | |
790 | iov, iovcnt, sent_cb); | |
fbe78f4f AL |
791 | } |
792 | ||
f3b6c7fc | 793 | ssize_t |
35277d14 | 794 | qemu_sendv_packet(NetClientState *nc, const struct iovec *iov, int iovcnt) |
f3b6c7fc | 795 | { |
35277d14 | 796 | return qemu_sendv_packet_async(nc, iov, iovcnt, NULL); |
63a01ef8 AL |
797 | } |
798 | ||
4e68f7a0 | 799 | NetClientState *qemu_find_netdev(const char *id) |
5869c4d5 | 800 | { |
35277d14 | 801 | NetClientState *nc; |
5869c4d5 | 802 | |
35277d14 SH |
803 | QTAILQ_FOREACH(nc, &net_clients, next) { |
804 | if (nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC) | |
85dde9a9 | 805 | continue; |
35277d14 SH |
806 | if (!strcmp(nc->name, id)) { |
807 | return nc; | |
5869c4d5 MM |
808 | } |
809 | } | |
810 | ||
811 | return NULL; | |
812 | } | |
813 | ||
6c51ae73 JW |
814 | int qemu_find_net_clients_except(const char *id, NetClientState **ncs, |
815 | NetClientOptionsKind type, int max) | |
816 | { | |
817 | NetClientState *nc; | |
818 | int ret = 0; | |
819 | ||
820 | QTAILQ_FOREACH(nc, &net_clients, next) { | |
821 | if (nc->info->type == type) { | |
822 | continue; | |
823 | } | |
40d19394 | 824 | if (!id || !strcmp(nc->name, id)) { |
6c51ae73 JW |
825 | if (ret < max) { |
826 | ncs[ret] = nc; | |
827 | } | |
828 | ret++; | |
829 | } | |
830 | } | |
831 | ||
832 | return ret; | |
833 | } | |
834 | ||
7697079b AL |
835 | static int nic_get_free_idx(void) |
836 | { | |
837 | int index; | |
838 | ||
839 | for (index = 0; index < MAX_NICS; index++) | |
840 | if (!nd_table[index].used) | |
841 | return index; | |
842 | return -1; | |
843 | } | |
844 | ||
07caea31 MA |
845 | int qemu_show_nic_models(const char *arg, const char *const *models) |
846 | { | |
847 | int i; | |
848 | ||
c8057f95 | 849 | if (!arg || !is_help_option(arg)) { |
07caea31 | 850 | return 0; |
c8057f95 | 851 | } |
07caea31 MA |
852 | |
853 | fprintf(stderr, "qemu: Supported NIC models: "); | |
854 | for (i = 0 ; models[i]; i++) | |
855 | fprintf(stderr, "%s%c", models[i], models[i+1] ? ',' : '\n'); | |
856 | return 1; | |
857 | } | |
858 | ||
d07f22c5 AL |
859 | void qemu_check_nic_model(NICInfo *nd, const char *model) |
860 | { | |
861 | const char *models[2]; | |
862 | ||
863 | models[0] = model; | |
864 | models[1] = NULL; | |
865 | ||
07caea31 MA |
866 | if (qemu_show_nic_models(nd->model, models)) |
867 | exit(0); | |
868 | if (qemu_find_nic_model(nd, models, model) < 0) | |
869 | exit(1); | |
d07f22c5 AL |
870 | } |
871 | ||
07caea31 MA |
872 | int qemu_find_nic_model(NICInfo *nd, const char * const *models, |
873 | const char *default_model) | |
d07f22c5 | 874 | { |
07caea31 | 875 | int i; |
d07f22c5 AL |
876 | |
877 | if (!nd->model) | |
7267c094 | 878 | nd->model = g_strdup(default_model); |
d07f22c5 | 879 | |
07caea31 MA |
880 | for (i = 0 ; models[i]; i++) { |
881 | if (strcmp(nd->model, models[i]) == 0) | |
882 | return i; | |
d07f22c5 AL |
883 | } |
884 | ||
6daf194d | 885 | error_report("Unsupported NIC model: %s", nd->model); |
07caea31 | 886 | return -1; |
d07f22c5 AL |
887 | } |
888 | ||
1a0c0958 | 889 | static int net_init_nic(const NetClientOptions *opts, const char *name, |
a30ecde6 | 890 | NetClientState *peer, Error **errp) |
f83c6e10 MM |
891 | { |
892 | int idx; | |
893 | NICInfo *nd; | |
2456f36f LE |
894 | const NetLegacyNicOptions *nic; |
895 | ||
896 | assert(opts->kind == NET_CLIENT_OPTIONS_KIND_NIC); | |
897 | nic = opts->nic; | |
f83c6e10 MM |
898 | |
899 | idx = nic_get_free_idx(); | |
900 | if (idx == -1 || nb_nics >= MAX_NICS) { | |
66308868 | 901 | error_setg(errp, "too many NICs"); |
f83c6e10 MM |
902 | return -1; |
903 | } | |
904 | ||
905 | nd = &nd_table[idx]; | |
906 | ||
907 | memset(nd, 0, sizeof(*nd)); | |
908 | ||
2456f36f LE |
909 | if (nic->has_netdev) { |
910 | nd->netdev = qemu_find_netdev(nic->netdev); | |
5869c4d5 | 911 | if (!nd->netdev) { |
66308868 | 912 | error_setg(errp, "netdev '%s' not found", nic->netdev); |
5869c4d5 MM |
913 | return -1; |
914 | } | |
915 | } else { | |
d33d93b2 SH |
916 | assert(peer); |
917 | nd->netdev = peer; | |
5869c4d5 | 918 | } |
c64f50d1 | 919 | nd->name = g_strdup(name); |
2456f36f LE |
920 | if (nic->has_model) { |
921 | nd->model = g_strdup(nic->model); | |
f83c6e10 | 922 | } |
2456f36f LE |
923 | if (nic->has_addr) { |
924 | nd->devaddr = g_strdup(nic->addr); | |
f83c6e10 MM |
925 | } |
926 | ||
2456f36f LE |
927 | if (nic->has_macaddr && |
928 | net_parse_macaddr(nd->macaddr.a, nic->macaddr) < 0) { | |
66308868 | 929 | error_setg(errp, "invalid syntax for ethernet address"); |
f83c6e10 MM |
930 | return -1; |
931 | } | |
d60b20cf DK |
932 | if (nic->has_macaddr && |
933 | is_multicast_ether_addr(nd->macaddr.a)) { | |
66308868 MA |
934 | error_setg(errp, |
935 | "NIC cannot have multicast MAC address (odd 1st byte)"); | |
d60b20cf DK |
936 | return -1; |
937 | } | |
6eed1856 | 938 | qemu_macaddr_default_if_unset(&nd->macaddr); |
f83c6e10 | 939 | |
2456f36f LE |
940 | if (nic->has_vectors) { |
941 | if (nic->vectors > 0x7ffffff) { | |
66308868 | 942 | error_setg(errp, "invalid # of vectors: %"PRIu32, nic->vectors); |
2456f36f LE |
943 | return -1; |
944 | } | |
945 | nd->nvectors = nic->vectors; | |
946 | } else { | |
947 | nd->nvectors = DEV_NVECTORS_UNSPECIFIED; | |
f83c6e10 MM |
948 | } |
949 | ||
950 | nd->used = 1; | |
f83c6e10 MM |
951 | nb_nics++; |
952 | ||
953 | return idx; | |
954 | } | |
955 | ||
6687b79d LE |
956 | |
957 | static int (* const net_client_init_fun[NET_CLIENT_OPTIONS_KIND_MAX])( | |
1a0c0958 | 958 | const NetClientOptions *opts, |
6687b79d | 959 | const char *name, |
a30ecde6 | 960 | NetClientState *peer, Error **errp) = { |
f6c874e3 | 961 | [NET_CLIENT_OPTIONS_KIND_NIC] = net_init_nic, |
ec302ffd | 962 | #ifdef CONFIG_SLIRP |
f6c874e3 | 963 | [NET_CLIENT_OPTIONS_KIND_USER] = net_init_slirp, |
2944e4ec | 964 | #endif |
f6c874e3 SH |
965 | [NET_CLIENT_OPTIONS_KIND_TAP] = net_init_tap, |
966 | [NET_CLIENT_OPTIONS_KIND_SOCKET] = net_init_socket, | |
dd51058d | 967 | #ifdef CONFIG_VDE |
f6c874e3 | 968 | [NET_CLIENT_OPTIONS_KIND_VDE] = net_init_vde, |
58952137 VM |
969 | #endif |
970 | #ifdef CONFIG_NETMAP | |
971 | [NET_CLIENT_OPTIONS_KIND_NETMAP] = net_init_netmap, | |
dd51058d | 972 | #endif |
f6c874e3 | 973 | [NET_CLIENT_OPTIONS_KIND_DUMP] = net_init_dump, |
2944e4ec | 974 | #ifdef CONFIG_NET_BRIDGE |
f6c874e3 | 975 | [NET_CLIENT_OPTIONS_KIND_BRIDGE] = net_init_bridge, |
6687b79d | 976 | #endif |
f6c874e3 | 977 | [NET_CLIENT_OPTIONS_KIND_HUBPORT] = net_init_hubport, |
03ce5744 NN |
978 | #ifdef CONFIG_VHOST_NET_USED |
979 | [NET_CLIENT_OPTIONS_KIND_VHOST_USER] = net_init_vhost_user, | |
980 | #endif | |
015a33bd | 981 | #ifdef CONFIG_L2TPV3 |
3fb69aa1 AI |
982 | [NET_CLIENT_OPTIONS_KIND_L2TPV3] = net_init_l2tpv3, |
983 | #endif | |
f83c6e10 MM |
984 | }; |
985 | ||
6687b79d | 986 | |
1a0c0958 | 987 | static int net_client_init1(const void *object, int is_netdev, Error **errp) |
f83c6e10 | 988 | { |
6687b79d | 989 | const NetClientOptions *opts; |
6d952ebe | 990 | const char *name; |
4ef0defb | 991 | NetClientState *peer = NULL; |
f83c6e10 | 992 | |
5294e2c7 | 993 | if (is_netdev) { |
1e81aba5 SH |
994 | const Netdev *netdev = object; |
995 | opts = netdev->opts; | |
996 | name = netdev->id; | |
6687b79d | 997 | |
1322629b SH |
998 | if (opts->kind == NET_CLIENT_OPTIONS_KIND_DUMP || |
999 | opts->kind == NET_CLIENT_OPTIONS_KIND_NIC || | |
1000 | !net_client_init_fun[opts->kind]) { | |
c6bd8c70 MA |
1001 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "type", |
1002 | "a netdev backend type"); | |
f6b134ac MM |
1003 | return -1; |
1004 | } | |
6687b79d | 1005 | } else { |
1e81aba5 SH |
1006 | const NetLegacy *net = object; |
1007 | opts = net->opts; | |
6687b79d | 1008 | /* missing optional values have been initialized to "all bits zero" */ |
1e81aba5 | 1009 | name = net->has_id ? net->id : net->name; |
d139e9a6 SH |
1010 | |
1011 | if (opts->kind == NET_CLIENT_OPTIONS_KIND_NONE) { | |
1012 | return 0; /* nothing to do */ | |
1013 | } | |
1e81aba5 SH |
1014 | if (opts->kind == NET_CLIENT_OPTIONS_KIND_HUBPORT) { |
1015 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "type", | |
1016 | "a net type"); | |
1017 | return -1; | |
1018 | } | |
d139e9a6 SH |
1019 | |
1020 | if (!net_client_init_fun[opts->kind]) { | |
1021 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "type", | |
1022 | "a net backend type (maybe it is not compiled " | |
1023 | "into this binary)"); | |
1024 | return -1; | |
1025 | } | |
f6b134ac | 1026 | |
1e81aba5 SH |
1027 | /* Do not add to a vlan if it's a nic with a netdev= parameter. */ |
1028 | if (opts->kind != NET_CLIENT_OPTIONS_KIND_NIC || | |
1029 | !opts->nic->has_netdev) { | |
1030 | peer = net_hub_add_port(net->has_vlan ? net->vlan : 0, NULL); | |
1031 | } | |
4ef0defb | 1032 | } |
6687b79d | 1033 | |
4ef0defb SH |
1034 | if (net_client_init_fun[opts->kind](opts, name, peer, errp) < 0) { |
1035 | /* FIXME drop when all init functions store an Error */ | |
1036 | if (errp && !*errp) { | |
1037 | error_setg(errp, QERR_DEVICE_INIT_FAILED, | |
1038 | NetClientOptionsKind_lookup[opts->kind]); | |
f6b134ac | 1039 | } |
4ef0defb | 1040 | return -1; |
f6b134ac | 1041 | } |
6687b79d LE |
1042 | return 0; |
1043 | } | |
1044 | ||
f6b134ac | 1045 | |
6687b79d LE |
1046 | static void net_visit(Visitor *v, int is_netdev, void **object, Error **errp) |
1047 | { | |
1048 | if (is_netdev) { | |
1049 | visit_type_Netdev(v, (Netdev **)object, NULL, errp); | |
1050 | } else { | |
1051 | visit_type_NetLegacy(v, (NetLegacy **)object, NULL, errp); | |
6d952ebe | 1052 | } |
6687b79d | 1053 | } |
6d952ebe | 1054 | |
f6b134ac | 1055 | |
6687b79d LE |
1056 | int net_client_init(QemuOpts *opts, int is_netdev, Error **errp) |
1057 | { | |
1058 | void *object = NULL; | |
1059 | Error *err = NULL; | |
1060 | int ret = -1; | |
f83c6e10 | 1061 | |
6687b79d LE |
1062 | { |
1063 | OptsVisitor *ov = opts_visitor_new(opts); | |
f6b134ac | 1064 | |
6687b79d LE |
1065 | net_visit(opts_get_visitor(ov), is_netdev, &object, &err); |
1066 | opts_visitor_cleanup(ov); | |
f83c6e10 MM |
1067 | } |
1068 | ||
6687b79d | 1069 | if (!err) { |
1a0c0958 | 1070 | ret = net_client_init1(object, is_netdev, &err); |
6687b79d LE |
1071 | } |
1072 | ||
1073 | if (object) { | |
1074 | QapiDeallocVisitor *dv = qapi_dealloc_visitor_new(); | |
1075 | ||
1076 | net_visit(qapi_dealloc_get_visitor(dv), is_netdev, &object, NULL); | |
1077 | qapi_dealloc_visitor_cleanup(dv); | |
1078 | } | |
1079 | ||
1080 | error_propagate(errp, err); | |
1081 | return ret; | |
f83c6e10 MM |
1082 | } |
1083 | ||
6687b79d | 1084 | |
6f338c34 AL |
1085 | static int net_host_check_device(const char *device) |
1086 | { | |
1087 | int i; | |
84007e81 HB |
1088 | for (i = 0; host_net_devices[i]; i++) { |
1089 | if (!strncmp(host_net_devices[i], device, | |
1090 | strlen(host_net_devices[i]))) { | |
6f338c34 | 1091 | return 1; |
84007e81 | 1092 | } |
6f338c34 AL |
1093 | } |
1094 | ||
1095 | return 0; | |
1096 | } | |
1097 | ||
3e5a50d6 | 1098 | void hmp_host_net_add(Monitor *mon, const QDict *qdict) |
6f338c34 | 1099 | { |
f18c16de | 1100 | const char *device = qdict_get_str(qdict, "device"); |
7f1c9d20 | 1101 | const char *opts_str = qdict_get_try_str(qdict, "opts"); |
4559a1db | 1102 | Error *local_err = NULL; |
7f1c9d20 | 1103 | QemuOpts *opts; |
f18c16de | 1104 | |
6f338c34 | 1105 | if (!net_host_check_device(device)) { |
376253ec | 1106 | monitor_printf(mon, "invalid host network device %s\n", device); |
6f338c34 AL |
1107 | return; |
1108 | } | |
7f1c9d20 | 1109 | |
70b94331 MA |
1110 | opts = qemu_opts_parse_noisily(qemu_find_opts("net"), |
1111 | opts_str ? opts_str : "", false); | |
7f1c9d20 | 1112 | if (!opts) { |
7f1c9d20 MM |
1113 | return; |
1114 | } | |
1115 | ||
f43e47db | 1116 | qemu_opt_set(opts, "type", device, &error_abort); |
7f1c9d20 | 1117 | |
4559a1db | 1118 | net_client_init(opts, 0, &local_err); |
84d18f06 | 1119 | if (local_err) { |
12d0cc2d | 1120 | error_report_err(local_err); |
5c8be678 AL |
1121 | monitor_printf(mon, "adding host network device %s failed\n", device); |
1122 | } | |
6f338c34 AL |
1123 | } |
1124 | ||
3e5a50d6 | 1125 | void hmp_host_net_remove(Monitor *mon, const QDict *qdict) |
6f338c34 | 1126 | { |
35277d14 | 1127 | NetClientState *nc; |
f18c16de LC |
1128 | int vlan_id = qdict_get_int(qdict, "vlan_id"); |
1129 | const char *device = qdict_get_str(qdict, "device"); | |
6f338c34 | 1130 | |
35277d14 SH |
1131 | nc = net_hub_find_client_by_name(vlan_id, device); |
1132 | if (!nc) { | |
86e11772 HB |
1133 | error_report("Host network device '%s' on hub '%d' not found", |
1134 | device, vlan_id); | |
6f338c34 AL |
1135 | return; |
1136 | } | |
7fb43911 | 1137 | if (nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC) { |
86e11772 | 1138 | error_report("invalid host network device '%s'", device); |
e8f1f9db AL |
1139 | return; |
1140 | } | |
64a55d60 JW |
1141 | |
1142 | qemu_del_net_client(nc->peer); | |
b20c6b9e | 1143 | qemu_del_net_client(nc); |
6f338c34 AL |
1144 | } |
1145 | ||
928059a3 LC |
1146 | void netdev_add(QemuOpts *opts, Error **errp) |
1147 | { | |
1148 | net_client_init(opts, 1, errp); | |
1149 | } | |
1150 | ||
485febc6 | 1151 | void qmp_netdev_add(QDict *qdict, QObject **ret, Error **errp) |
ae82d324 | 1152 | { |
4e89978e | 1153 | Error *local_err = NULL; |
928059a3 | 1154 | QemuOptsList *opts_list; |
ae82d324 | 1155 | QemuOpts *opts; |
ae82d324 | 1156 | |
928059a3 | 1157 | opts_list = qemu_find_opts_err("netdev", &local_err); |
84d18f06 | 1158 | if (local_err) { |
485febc6 | 1159 | goto out; |
ae82d324 MA |
1160 | } |
1161 | ||
928059a3 | 1162 | opts = qemu_opts_from_qdict(opts_list, qdict, &local_err); |
84d18f06 | 1163 | if (local_err) { |
485febc6 | 1164 | goto out; |
928059a3 LC |
1165 | } |
1166 | ||
1167 | netdev_add(opts, &local_err); | |
84d18f06 | 1168 | if (local_err) { |
410cbafe | 1169 | qemu_opts_del(opts); |
485febc6 | 1170 | goto out; |
410cbafe YT |
1171 | } |
1172 | ||
485febc6 MA |
1173 | out: |
1174 | error_propagate(errp, local_err); | |
ae82d324 MA |
1175 | } |
1176 | ||
5f964155 | 1177 | void qmp_netdev_del(const char *id, Error **errp) |
ae82d324 | 1178 | { |
35277d14 | 1179 | NetClientState *nc; |
645c9496 | 1180 | QemuOpts *opts; |
ae82d324 | 1181 | |
35277d14 SH |
1182 | nc = qemu_find_netdev(id); |
1183 | if (!nc) { | |
75158ebb MA |
1184 | error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, |
1185 | "Device '%s' not found", id); | |
5f964155 | 1186 | return; |
ae82d324 | 1187 | } |
5f964155 | 1188 | |
645c9496 SH |
1189 | opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), id); |
1190 | if (!opts) { | |
1191 | error_setg(errp, "Device '%s' is not a netdev", id); | |
1192 | return; | |
1193 | } | |
1194 | ||
b20c6b9e | 1195 | qemu_del_net_client(nc); |
645c9496 | 1196 | qemu_opts_del(opts); |
ae82d324 MA |
1197 | } |
1198 | ||
1a859593 | 1199 | void print_net_client(Monitor *mon, NetClientState *nc) |
44e798d3 | 1200 | { |
1ceef9f2 JW |
1201 | monitor_printf(mon, "%s: index=%d,type=%s,%s\n", nc->name, |
1202 | nc->queue_index, | |
1203 | NetClientOptionsKind_lookup[nc->info->type], | |
1204 | nc->info_str); | |
44e798d3 JK |
1205 | } |
1206 | ||
b1be4280 AK |
1207 | RxFilterInfoList *qmp_query_rx_filter(bool has_name, const char *name, |
1208 | Error **errp) | |
1209 | { | |
1210 | NetClientState *nc; | |
1211 | RxFilterInfoList *filter_list = NULL, *last_entry = NULL; | |
1212 | ||
1213 | QTAILQ_FOREACH(nc, &net_clients, next) { | |
1214 | RxFilterInfoList *entry; | |
1215 | RxFilterInfo *info; | |
1216 | ||
1217 | if (has_name && strcmp(nc->name, name) != 0) { | |
1218 | continue; | |
1219 | } | |
1220 | ||
1221 | /* only query rx-filter information of NIC */ | |
1222 | if (nc->info->type != NET_CLIENT_OPTIONS_KIND_NIC) { | |
1223 | if (has_name) { | |
1224 | error_setg(errp, "net client(%s) isn't a NIC", name); | |
9083da1d | 1225 | return NULL; |
b1be4280 AK |
1226 | } |
1227 | continue; | |
1228 | } | |
1229 | ||
1230 | if (nc->info->query_rx_filter) { | |
1231 | info = nc->info->query_rx_filter(nc); | |
1232 | entry = g_malloc0(sizeof(*entry)); | |
1233 | entry->value = info; | |
1234 | ||
1235 | if (!filter_list) { | |
1236 | filter_list = entry; | |
1237 | } else { | |
1238 | last_entry->next = entry; | |
1239 | } | |
1240 | last_entry = entry; | |
1241 | } else if (has_name) { | |
1242 | error_setg(errp, "net client(%s) doesn't support" | |
1243 | " rx-filter querying", name); | |
9083da1d | 1244 | return NULL; |
b1be4280 | 1245 | } |
638fb141 MA |
1246 | |
1247 | if (has_name) { | |
1248 | break; | |
1249 | } | |
b1be4280 AK |
1250 | } |
1251 | ||
9083da1d | 1252 | if (filter_list == NULL && has_name) { |
b1be4280 AK |
1253 | error_setg(errp, "invalid net client name: %s", name); |
1254 | } | |
1255 | ||
1256 | return filter_list; | |
1257 | } | |
1258 | ||
1ce6be24 | 1259 | void hmp_info_network(Monitor *mon, const QDict *qdict) |
63a01ef8 | 1260 | { |
35277d14 | 1261 | NetClientState *nc, *peer; |
2be64a68 | 1262 | NetClientOptionsKind type; |
63a01ef8 | 1263 | |
1a859593 ZYW |
1264 | net_hub_info(mon); |
1265 | ||
35277d14 SH |
1266 | QTAILQ_FOREACH(nc, &net_clients, next) { |
1267 | peer = nc->peer; | |
1268 | type = nc->info->type; | |
5610c3aa | 1269 | |
1a859593 ZYW |
1270 | /* Skip if already printed in hub info */ |
1271 | if (net_hub_id_for_client(nc, NULL) == 0) { | |
1272 | continue; | |
5610c3aa | 1273 | } |
1a859593 | 1274 | |
2be64a68 | 1275 | if (!peer || type == NET_CLIENT_OPTIONS_KIND_NIC) { |
35277d14 | 1276 | print_net_client(mon, nc); |
19061e63 | 1277 | } /* else it's a netdev connected to a NIC, printed with the NIC */ |
2be64a68 | 1278 | if (peer && type == NET_CLIENT_OPTIONS_KIND_NIC) { |
1a859593 | 1279 | monitor_printf(mon, " \\ "); |
44e798d3 | 1280 | print_net_client(mon, peer); |
a0104e0e | 1281 | } |
a0104e0e | 1282 | } |
63a01ef8 AL |
1283 | } |
1284 | ||
4b37156c | 1285 | void qmp_set_link(const char *name, bool up, Error **errp) |
436e5e53 | 1286 | { |
1ceef9f2 JW |
1287 | NetClientState *ncs[MAX_QUEUE_NUM]; |
1288 | NetClientState *nc; | |
1289 | int queues, i; | |
436e5e53 | 1290 | |
1ceef9f2 JW |
1291 | queues = qemu_find_net_clients_except(name, ncs, |
1292 | NET_CLIENT_OPTIONS_KIND_MAX, | |
1293 | MAX_QUEUE_NUM); | |
1294 | ||
1295 | if (queues == 0) { | |
75158ebb MA |
1296 | error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, |
1297 | "Device '%s' not found", name); | |
4b37156c | 1298 | return; |
436e5e53 | 1299 | } |
1ceef9f2 | 1300 | nc = ncs[0]; |
436e5e53 | 1301 | |
1ceef9f2 JW |
1302 | for (i = 0; i < queues; i++) { |
1303 | ncs[i]->link_down = !up; | |
1304 | } | |
436e5e53 | 1305 | |
35277d14 SH |
1306 | if (nc->info->link_status_changed) { |
1307 | nc->info->link_status_changed(nc); | |
665a3b07 | 1308 | } |
ab1cbe1c | 1309 | |
02d38fcb VY |
1310 | if (nc->peer) { |
1311 | /* Change peer link only if the peer is NIC and then notify peer. | |
1312 | * If the peer is a HUBPORT or a backend, we do not change the | |
1313 | * link status. | |
1314 | * | |
1315 | * This behavior is compatible with qemu vlans where there could be | |
1316 | * multiple clients that can still communicate with each other in | |
1317 | * disconnected mode. For now maintain this compatibility. | |
1318 | */ | |
1319 | if (nc->peer->info->type == NET_CLIENT_OPTIONS_KIND_NIC) { | |
1320 | for (i = 0; i < queues; i++) { | |
1321 | ncs[i]->peer->link_down = !up; | |
1322 | } | |
1323 | } | |
1324 | if (nc->peer->info->link_status_changed) { | |
1325 | nc->peer->info->link_status_changed(nc->peer); | |
1326 | } | |
ab1cbe1c | 1327 | } |
436e5e53 AL |
1328 | } |
1329 | ||
ca77d85e MT |
1330 | static void net_vm_change_state_handler(void *opaque, int running, |
1331 | RunState state) | |
1332 | { | |
625de449 FZ |
1333 | NetClientState *nc; |
1334 | NetClientState *tmp; | |
ca77d85e | 1335 | |
625de449 FZ |
1336 | QTAILQ_FOREACH_SAFE(nc, &net_clients, next, tmp) { |
1337 | if (running) { | |
1338 | /* Flush queued packets and wake up backends. */ | |
1339 | if (nc->peer && qemu_can_send_packet(nc)) { | |
1340 | qemu_flush_queued_packets(nc->peer); | |
1341 | } | |
1342 | } else { | |
1343 | /* Complete all queued packets, to guarantee we don't modify | |
1344 | * state later when VM is not running. | |
1345 | */ | |
ca77d85e MT |
1346 | qemu_flush_or_purge_queued_packets(nc, true); |
1347 | } | |
1348 | } | |
1349 | } | |
1350 | ||
63a01ef8 AL |
1351 | void net_cleanup(void) |
1352 | { | |
1ceef9f2 | 1353 | NetClientState *nc; |
577c4af9 | 1354 | |
1ceef9f2 JW |
1355 | /* We may del multiple entries during qemu_del_net_client(), |
1356 | * so QTAILQ_FOREACH_SAFE() is also not safe here. | |
1357 | */ | |
1358 | while (!QTAILQ_EMPTY(&net_clients)) { | |
1359 | nc = QTAILQ_FIRST(&net_clients); | |
948ecf21 JW |
1360 | if (nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC) { |
1361 | qemu_del_nic(qemu_get_nic(nc)); | |
1362 | } else { | |
1363 | qemu_del_net_client(nc); | |
1364 | } | |
577c4af9 | 1365 | } |
ca77d85e MT |
1366 | |
1367 | qemu_del_vm_change_state_handler(net_change_state_entry); | |
63a01ef8 AL |
1368 | } |
1369 | ||
668680f7 | 1370 | void net_check_clients(void) |
63a01ef8 | 1371 | { |
35277d14 | 1372 | NetClientState *nc; |
48e2faf2 | 1373 | int i; |
63a01ef8 | 1374 | |
641f6eae PM |
1375 | /* Don't warn about the default network setup that you get if |
1376 | * no command line -net or -netdev options are specified. There | |
1377 | * are two cases that we would otherwise complain about: | |
1378 | * (1) board doesn't support a NIC but the implicit "-net nic" | |
1379 | * requested one | |
1380 | * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic" | |
1381 | * sets up a nic that isn't connected to anything. | |
1382 | */ | |
1383 | if (default_net) { | |
1384 | return; | |
1385 | } | |
1386 | ||
81017645 | 1387 | net_hub_check_clients(); |
ac60cc18 | 1388 | |
35277d14 SH |
1389 | QTAILQ_FOREACH(nc, &net_clients, next) { |
1390 | if (!nc->peer) { | |
efe32fdd | 1391 | fprintf(stderr, "Warning: %s %s has no peer\n", |
35277d14 SH |
1392 | nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC ? |
1393 | "nic" : "netdev", nc->name); | |
efe32fdd MA |
1394 | } |
1395 | } | |
48e2faf2 PM |
1396 | |
1397 | /* Check that all NICs requested via -net nic actually got created. | |
1398 | * NICs created via -device don't need to be checked here because | |
1399 | * they are always instantiated. | |
1400 | */ | |
1401 | for (i = 0; i < MAX_NICS; i++) { | |
1402 | NICInfo *nd = &nd_table[i]; | |
1403 | if (nd->used && !nd->instantiated) { | |
1404 | fprintf(stderr, "Warning: requested NIC (%s, model %s) " | |
1405 | "was not created (not supported by this machine?)\n", | |
1406 | nd->name ? nd->name : "anonymous", | |
1407 | nd->model ? nd->model : "unspecified"); | |
1408 | } | |
1409 | } | |
63a01ef8 | 1410 | } |
dc1c9fe8 | 1411 | |
28d0de7a | 1412 | static int net_init_client(void *dummy, QemuOpts *opts, Error **errp) |
dc1c9fe8 | 1413 | { |
4559a1db LC |
1414 | Error *local_err = NULL; |
1415 | ||
1416 | net_client_init(opts, 0, &local_err); | |
84d18f06 | 1417 | if (local_err) { |
12d0cc2d | 1418 | error_report_err(local_err); |
c1671a08 | 1419 | return -1; |
4559a1db LC |
1420 | } |
1421 | ||
c1671a08 | 1422 | return 0; |
f6b134ac MM |
1423 | } |
1424 | ||
28d0de7a | 1425 | static int net_init_netdev(void *dummy, QemuOpts *opts, Error **errp) |
f6b134ac | 1426 | { |
4559a1db LC |
1427 | Error *local_err = NULL; |
1428 | int ret; | |
1429 | ||
1430 | ret = net_client_init(opts, 1, &local_err); | |
84d18f06 | 1431 | if (local_err) { |
12d0cc2d | 1432 | error_report_err(local_err); |
4559a1db LC |
1433 | return -1; |
1434 | } | |
1435 | ||
1436 | return ret; | |
dc1c9fe8 MM |
1437 | } |
1438 | ||
1439 | int net_init_clients(void) | |
1440 | { | |
3329f07b GH |
1441 | QemuOptsList *net = qemu_find_opts("net"); |
1442 | ||
cb4522cc | 1443 | if (default_net) { |
dc1c9fe8 | 1444 | /* if no clients, we use a default config */ |
79087c78 | 1445 | qemu_opts_set(net, NULL, "type", "nic", &error_abort); |
dc1c9fe8 | 1446 | #ifdef CONFIG_SLIRP |
79087c78 | 1447 | qemu_opts_set(net, NULL, "type", "user", &error_abort); |
dc1c9fe8 MM |
1448 | #endif |
1449 | } | |
1450 | ||
ca77d85e MT |
1451 | net_change_state_entry = |
1452 | qemu_add_vm_change_state_handler(net_vm_change_state_handler, NULL); | |
1453 | ||
94878994 | 1454 | QTAILQ_INIT(&net_clients); |
5610c3aa | 1455 | |
28d0de7a MA |
1456 | if (qemu_opts_foreach(qemu_find_opts("netdev"), |
1457 | net_init_netdev, NULL, NULL)) { | |
f6b134ac | 1458 | return -1; |
a4c7367f | 1459 | } |
f6b134ac | 1460 | |
28d0de7a | 1461 | if (qemu_opts_foreach(net, net_init_client, NULL, NULL)) { |
dc1c9fe8 MM |
1462 | return -1; |
1463 | } | |
1464 | ||
dc1c9fe8 MM |
1465 | return 0; |
1466 | } | |
1467 | ||
7f161aae | 1468 | int net_client_parse(QemuOptsList *opts_list, const char *optarg) |
dc1c9fe8 | 1469 | { |
a3a766e7 | 1470 | #if defined(CONFIG_SLIRP) |
68ac40d2 MM |
1471 | int ret; |
1472 | if (net_slirp_parse_legacy(opts_list, optarg, &ret)) { | |
dc1c9fe8 MM |
1473 | return ret; |
1474 | } | |
a3a766e7 | 1475 | #endif |
68ac40d2 | 1476 | |
70b94331 | 1477 | if (!qemu_opts_parse_noisily(opts_list, optarg, true)) { |
dc1c9fe8 MM |
1478 | return -1; |
1479 | } | |
1480 | ||
cb4522cc | 1481 | default_net = 0; |
dc1c9fe8 MM |
1482 | return 0; |
1483 | } | |
7fc8d918 JW |
1484 | |
1485 | /* From FreeBSD */ | |
1486 | /* XXX: optimize */ | |
1487 | unsigned compute_mcast_idx(const uint8_t *ep) | |
1488 | { | |
1489 | uint32_t crc; | |
1490 | int carry, i, j; | |
1491 | uint8_t b; | |
1492 | ||
1493 | crc = 0xffffffff; | |
1494 | for (i = 0; i < 6; i++) { | |
1495 | b = *ep++; | |
1496 | for (j = 0; j < 8; j++) { | |
1497 | carry = ((crc & 0x80000000L) ? 1 : 0) ^ (b & 0x01); | |
1498 | crc <<= 1; | |
1499 | b >>= 1; | |
1500 | if (carry) { | |
1501 | crc = ((crc ^ POLYNOMIAL) | carry); | |
1502 | } | |
1503 | } | |
1504 | } | |
1505 | return crc >> 26; | |
1506 | } | |
4d454574 PB |
1507 | |
1508 | QemuOptsList qemu_netdev_opts = { | |
1509 | .name = "netdev", | |
1510 | .implied_opt_name = "type", | |
1511 | .head = QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts.head), | |
1512 | .desc = { | |
1513 | /* | |
1514 | * no elements => accept any params | |
1515 | * validation will happen later | |
1516 | */ | |
1517 | { /* end of list */ } | |
1518 | }, | |
1519 | }; | |
1520 | ||
1521 | QemuOptsList qemu_net_opts = { | |
1522 | .name = "net", | |
1523 | .implied_opt_name = "type", | |
1524 | .head = QTAILQ_HEAD_INITIALIZER(qemu_net_opts.head), | |
1525 | .desc = { | |
1526 | /* | |
1527 | * no elements => accept any params | |
1528 | * validation will happen later | |
1529 | */ | |
1530 | { /* end of list */ } | |
1531 | }, | |
1532 | }; |