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