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 */
24
25 #include "qemu/osdep.h"
26
27 #include "net/net.h"
28 #include "clients.h"
29 #include "hub.h"
30 #include "hw/qdev-properties.h"
31 #include "net/slirp.h"
32 #include "net/eth.h"
33 #include "util.h"
34
35 #include "monitor/monitor.h"
36 #include "qemu/help_option.h"
37 #include "qapi/qapi-commands-net.h"
38 #include "qapi/qapi-visit-net.h"
39 #include "qapi/qmp/qdict.h"
40 #include "qapi/qmp/qerror.h"
41 #include "qemu/error-report.h"
42 #include "qemu/sockets.h"
43 #include "qemu/cutils.h"
44 #include "qemu/config-file.h"
45 #include "qemu/ctype.h"
46 #include "qemu/id.h"
47 #include "qemu/iov.h"
48 #include "qemu/qemu-print.h"
49 #include "qemu/main-loop.h"
50 #include "qemu/option.h"
51 #include "qemu/keyval.h"
52 #include "qapi/error.h"
53 #include "qapi/opts-visitor.h"
54 #include "sysemu/runstate.h"
55 #include "net/colo-compare.h"
56 #include "net/filter.h"
57 #include "qapi/string-output-visitor.h"
58 #include "qapi/qobject-input-visitor.h"
59 #include "standard-headers/linux/virtio_net.h"
60
61 /* Net bridge is currently not supported for W32. */
62 #if !defined(_WIN32)
63 # define CONFIG_NET_BRIDGE
64 #endif
65
66 static VMChangeStateEntry *net_change_state_entry;
67 NetClientStateList net_clients;
68
69 typedef struct NetdevQueueEntry {
70 Netdev *nd;
71 Location loc;
72 QSIMPLEQ_ENTRY(NetdevQueueEntry) entry;
73 } NetdevQueueEntry;
74
75 typedef QSIMPLEQ_HEAD(, NetdevQueueEntry) NetdevQueue;
76
77 static NetdevQueue nd_queue = QSIMPLEQ_HEAD_INITIALIZER(nd_queue);
78
79 static GHashTable *nic_model_help;
80
81 static int nb_nics;
82 static NICInfo nd_table[MAX_NICS];
83
84 /***********************************************************/
85 /* network device redirectors */
86
convert_host_port(struct sockaddr_in * saddr,const char * host,const char * port,Error ** errp)87 int convert_host_port(struct sockaddr_in *saddr, const char *host,
88 const char *port, Error **errp)
89 {
90 struct hostent *he;
91 const char *r;
92 long p;
93
94 memset(saddr, 0, sizeof(*saddr));
95
96 saddr->sin_family = AF_INET;
97 if (host[0] == '\0') {
98 saddr->sin_addr.s_addr = 0;
99 } else {
100 if (qemu_isdigit(host[0])) {
101 if (!inet_aton(host, &saddr->sin_addr)) {
102 error_setg(errp, "host address '%s' is not a valid "
103 "IPv4 address", host);
104 return -1;
105 }
106 } else {
107 he = gethostbyname(host);
108 if (he == NULL) {
109 error_setg(errp, "can't resolve host address '%s'", host);
110 return -1;
111 }
112 saddr->sin_addr = *(struct in_addr *)he->h_addr;
113 }
114 }
115 if (qemu_strtol(port, &r, 0, &p) != 0) {
116 error_setg(errp, "port number '%s' is invalid", port);
117 return -1;
118 }
119 saddr->sin_port = htons(p);
120 return 0;
121 }
122
parse_host_port(struct sockaddr_in * saddr,const char * str,Error ** errp)123 int parse_host_port(struct sockaddr_in *saddr, const char *str,
124 Error **errp)
125 {
126 gchar **substrings;
127 int ret;
128
129 substrings = g_strsplit(str, ":", 2);
130 if (!substrings || !substrings[0] || !substrings[1]) {
131 error_setg(errp, "host address '%s' doesn't contain ':' "
132 "separating host from port", str);
133 ret = -1;
134 goto out;
135 }
136
137 ret = convert_host_port(saddr, substrings[0], substrings[1], errp);
138
139 out:
140 g_strfreev(substrings);
141 return ret;
142 }
143
qemu_mac_strdup_printf(const uint8_t * macaddr)144 char *qemu_mac_strdup_printf(const uint8_t *macaddr)
145 {
146 return g_strdup_printf("%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",
147 macaddr[0], macaddr[1], macaddr[2],
148 macaddr[3], macaddr[4], macaddr[5]);
149 }
150
qemu_set_info_str(NetClientState * nc,const char * fmt,...)151 void qemu_set_info_str(NetClientState *nc, const char *fmt, ...)
152 {
153 va_list ap;
154
155 va_start(ap, fmt);
156 vsnprintf(nc->info_str, sizeof(nc->info_str), fmt, ap);
157 va_end(ap);
158 }
159
qemu_format_nic_info_str(NetClientState * nc,uint8_t macaddr[6])160 void qemu_format_nic_info_str(NetClientState *nc, uint8_t macaddr[6])
161 {
162 qemu_set_info_str(nc, "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
163 nc->model, macaddr[0], macaddr[1], macaddr[2],
164 macaddr[3], macaddr[4], macaddr[5]);
165 }
166
167 static int mac_table[256] = {0};
168
qemu_macaddr_set_used(MACAddr * macaddr)169 static void qemu_macaddr_set_used(MACAddr *macaddr)
170 {
171 int index;
172
173 for (index = 0x56; index < 0xFF; index++) {
174 if (macaddr->a[5] == index) {
175 mac_table[index]++;
176 }
177 }
178 }
179
qemu_macaddr_set_free(MACAddr * macaddr)180 static void qemu_macaddr_set_free(MACAddr *macaddr)
181 {
182 int index;
183 static const MACAddr base = { .a = { 0x52, 0x54, 0x00, 0x12, 0x34, 0 } };
184
185 if (memcmp(macaddr->a, &base.a, (sizeof(base.a) - 1)) != 0) {
186 return;
187 }
188 for (index = 0x56; index < 0xFF; index++) {
189 if (macaddr->a[5] == index) {
190 mac_table[index]--;
191 }
192 }
193 }
194
qemu_macaddr_get_free(void)195 static int qemu_macaddr_get_free(void)
196 {
197 int index;
198
199 for (index = 0x56; index < 0xFF; index++) {
200 if (mac_table[index] == 0) {
201 return index;
202 }
203 }
204
205 return -1;
206 }
207
qemu_macaddr_default_if_unset(MACAddr * macaddr)208 void qemu_macaddr_default_if_unset(MACAddr *macaddr)
209 {
210 static const MACAddr zero = { .a = { 0,0,0,0,0,0 } };
211 static const MACAddr base = { .a = { 0x52, 0x54, 0x00, 0x12, 0x34, 0 } };
212
213 if (memcmp(macaddr, &zero, sizeof(zero)) != 0) {
214 if (memcmp(macaddr->a, &base.a, (sizeof(base.a) - 1)) != 0) {
215 return;
216 } else {
217 qemu_macaddr_set_used(macaddr);
218 return;
219 }
220 }
221
222 macaddr->a[0] = 0x52;
223 macaddr->a[1] = 0x54;
224 macaddr->a[2] = 0x00;
225 macaddr->a[3] = 0x12;
226 macaddr->a[4] = 0x34;
227 macaddr->a[5] = qemu_macaddr_get_free();
228 qemu_macaddr_set_used(macaddr);
229 }
230
231 /**
232 * Generate a name for net client
233 *
234 * Only net clients created with the legacy -net option and NICs need this.
235 */
assign_name(NetClientState * nc1,const char * model)236 static char *assign_name(NetClientState *nc1, const char *model)
237 {
238 NetClientState *nc;
239 int id = 0;
240
241 QTAILQ_FOREACH(nc, &net_clients, next) {
242 if (nc == nc1) {
243 continue;
244 }
245 if (strcmp(nc->model, model) == 0) {
246 id++;
247 }
248 }
249
250 return g_strdup_printf("%s.%d", model, id);
251 }
252
qemu_net_client_destructor(NetClientState * nc)253 static void qemu_net_client_destructor(NetClientState *nc)
254 {
255 g_free(nc);
256 }
257 static ssize_t qemu_deliver_packet_iov(NetClientState *sender,
258 unsigned flags,
259 const struct iovec *iov,
260 int iovcnt,
261 void *opaque);
262
qemu_net_client_setup(NetClientState * nc,NetClientInfo * info,NetClientState * peer,const char * model,const char * name,NetClientDestructor * destructor,bool is_datapath)263 static void qemu_net_client_setup(NetClientState *nc,
264 NetClientInfo *info,
265 NetClientState *peer,
266 const char *model,
267 const char *name,
268 NetClientDestructor *destructor,
269 bool is_datapath)
270 {
271 nc->info = info;
272 nc->model = g_strdup(model);
273 if (name) {
274 nc->name = g_strdup(name);
275 } else {
276 nc->name = assign_name(nc, model);
277 }
278
279 if (peer) {
280 assert(!peer->peer);
281 nc->peer = peer;
282 peer->peer = nc;
283 }
284 QTAILQ_INSERT_TAIL(&net_clients, nc, next);
285
286 nc->incoming_queue = qemu_new_net_queue(qemu_deliver_packet_iov, nc);
287 nc->destructor = destructor;
288 nc->is_datapath = is_datapath;
289 QTAILQ_INIT(&nc->filters);
290 }
291
qemu_new_net_client(NetClientInfo * info,NetClientState * peer,const char * model,const char * name)292 NetClientState *qemu_new_net_client(NetClientInfo *info,
293 NetClientState *peer,
294 const char *model,
295 const char *name)
296 {
297 NetClientState *nc;
298
299 assert(info->size >= sizeof(NetClientState));
300
301 nc = g_malloc0(info->size);
302 qemu_net_client_setup(nc, info, peer, model, name,
303 qemu_net_client_destructor, true);
304
305 return nc;
306 }
307
qemu_new_net_control_client(NetClientInfo * info,NetClientState * peer,const char * model,const char * name)308 NetClientState *qemu_new_net_control_client(NetClientInfo *info,
309 NetClientState *peer,
310 const char *model,
311 const char *name)
312 {
313 NetClientState *nc;
314
315 assert(info->size >= sizeof(NetClientState));
316
317 nc = g_malloc0(info->size);
318 qemu_net_client_setup(nc, info, peer, model, name,
319 qemu_net_client_destructor, false);
320
321 return nc;
322 }
323
qemu_new_nic(NetClientInfo * info,NICConf * conf,const char * model,const char * name,MemReentrancyGuard * reentrancy_guard,void * opaque)324 NICState *qemu_new_nic(NetClientInfo *info,
325 NICConf *conf,
326 const char *model,
327 const char *name,
328 MemReentrancyGuard *reentrancy_guard,
329 void *opaque)
330 {
331 NetClientState **peers = conf->peers.ncs;
332 NICState *nic;
333 int i, queues = MAX(1, conf->peers.queues);
334
335 assert(info->type == NET_CLIENT_DRIVER_NIC);
336 assert(info->size >= sizeof(NICState));
337
338 nic = g_malloc0(info->size + sizeof(NetClientState) * queues);
339 nic->ncs = (void *)nic + info->size;
340 nic->conf = conf;
341 nic->reentrancy_guard = reentrancy_guard,
342 nic->opaque = opaque;
343
344 for (i = 0; i < queues; i++) {
345 qemu_net_client_setup(&nic->ncs[i], info, peers[i], model, name,
346 NULL, true);
347 nic->ncs[i].queue_index = i;
348 }
349
350 return nic;
351 }
352
qemu_get_subqueue(NICState * nic,int queue_index)353 NetClientState *qemu_get_subqueue(NICState *nic, int queue_index)
354 {
355 return nic->ncs + queue_index;
356 }
357
qemu_get_queue(NICState * nic)358 NetClientState *qemu_get_queue(NICState *nic)
359 {
360 return qemu_get_subqueue(nic, 0);
361 }
362
qemu_get_nic(NetClientState * nc)363 NICState *qemu_get_nic(NetClientState *nc)
364 {
365 NetClientState *nc0 = nc - nc->queue_index;
366
367 return (NICState *)((void *)nc0 - nc->info->size);
368 }
369
qemu_get_nic_opaque(NetClientState * nc)370 void *qemu_get_nic_opaque(NetClientState *nc)
371 {
372 NICState *nic = qemu_get_nic(nc);
373
374 return nic->opaque;
375 }
376
qemu_get_peer(NetClientState * nc,int queue_index)377 NetClientState *qemu_get_peer(NetClientState *nc, int queue_index)
378 {
379 assert(nc != NULL);
380 NetClientState *ncs = nc + queue_index;
381 return ncs->peer;
382 }
383
qemu_cleanup_net_client(NetClientState * nc)384 static void qemu_cleanup_net_client(NetClientState *nc)
385 {
386 QTAILQ_REMOVE(&net_clients, nc, next);
387
388 if (nc->info->cleanup) {
389 nc->info->cleanup(nc);
390 }
391 }
392
qemu_free_net_client(NetClientState * nc)393 static void qemu_free_net_client(NetClientState *nc)
394 {
395 if (nc->incoming_queue) {
396 qemu_del_net_queue(nc->incoming_queue);
397 }
398 if (nc->peer) {
399 nc->peer->peer = NULL;
400 }
401 g_free(nc->name);
402 g_free(nc->model);
403 if (nc->destructor) {
404 nc->destructor(nc);
405 }
406 }
407
qemu_del_net_client(NetClientState * nc)408 void qemu_del_net_client(NetClientState *nc)
409 {
410 NetClientState *ncs[MAX_QUEUE_NUM];
411 int queues, i;
412 NetFilterState *nf, *next;
413
414 assert(nc->info->type != NET_CLIENT_DRIVER_NIC);
415
416 /* If the NetClientState belongs to a multiqueue backend, we will change all
417 * other NetClientStates also.
418 */
419 queues = qemu_find_net_clients_except(nc->name, ncs,
420 NET_CLIENT_DRIVER_NIC,
421 MAX_QUEUE_NUM);
422 assert(queues != 0);
423
424 QTAILQ_FOREACH_SAFE(nf, &nc->filters, next, next) {
425 object_unparent(OBJECT(nf));
426 }
427
428 /* If there is a peer NIC, delete and cleanup client, but do not free. */
429 if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_NIC) {
430 NICState *nic = qemu_get_nic(nc->peer);
431 if (nic->peer_deleted) {
432 return;
433 }
434 nic->peer_deleted = true;
435
436 for (i = 0; i < queues; i++) {
437 ncs[i]->peer->link_down = true;
438 }
439
440 if (nc->peer->info->link_status_changed) {
441 nc->peer->info->link_status_changed(nc->peer);
442 }
443
444 for (i = 0; i < queues; i++) {
445 qemu_cleanup_net_client(ncs[i]);
446 }
447
448 return;
449 }
450
451 for (i = 0; i < queues; i++) {
452 qemu_cleanup_net_client(ncs[i]);
453 qemu_free_net_client(ncs[i]);
454 }
455 }
456
qemu_del_nic(NICState * nic)457 void qemu_del_nic(NICState *nic)
458 {
459 int i, queues = MAX(nic->conf->peers.queues, 1);
460
461 qemu_macaddr_set_free(&nic->conf->macaddr);
462
463 for (i = 0; i < queues; i++) {
464 NetClientState *nc = qemu_get_subqueue(nic, i);
465 /* If this is a peer NIC and peer has already been deleted, free it now. */
466 if (nic->peer_deleted) {
467 qemu_free_net_client(nc->peer);
468 } else if (nc->peer) {
469 /* if there are RX packets pending, complete them */
470 qemu_purge_queued_packets(nc->peer);
471 }
472 }
473
474 for (i = queues - 1; i >= 0; i--) {
475 NetClientState *nc = qemu_get_subqueue(nic, i);
476
477 qemu_cleanup_net_client(nc);
478 qemu_free_net_client(nc);
479 }
480
481 g_free(nic);
482 }
483
qemu_foreach_nic(qemu_nic_foreach func,void * opaque)484 void qemu_foreach_nic(qemu_nic_foreach func, void *opaque)
485 {
486 NetClientState *nc;
487
488 QTAILQ_FOREACH(nc, &net_clients, next) {
489 if (nc->info->type == NET_CLIENT_DRIVER_NIC) {
490 if (nc->queue_index == 0) {
491 func(qemu_get_nic(nc), opaque);
492 }
493 }
494 }
495 }
496
qemu_has_ufo(NetClientState * nc)497 bool qemu_has_ufo(NetClientState *nc)
498 {
499 if (!nc || !nc->info->has_ufo) {
500 return false;
501 }
502
503 return nc->info->has_ufo(nc);
504 }
505
qemu_has_uso(NetClientState * nc)506 bool qemu_has_uso(NetClientState *nc)
507 {
508 if (!nc || !nc->info->has_uso) {
509 return false;
510 }
511
512 return nc->info->has_uso(nc);
513 }
514
qemu_has_vnet_hdr(NetClientState * nc)515 bool qemu_has_vnet_hdr(NetClientState *nc)
516 {
517 if (!nc || !nc->info->has_vnet_hdr) {
518 return false;
519 }
520
521 return nc->info->has_vnet_hdr(nc);
522 }
523
qemu_has_vnet_hdr_len(NetClientState * nc,int len)524 bool qemu_has_vnet_hdr_len(NetClientState *nc, int len)
525 {
526 if (!nc || !nc->info->has_vnet_hdr_len) {
527 return false;
528 }
529
530 return nc->info->has_vnet_hdr_len(nc, len);
531 }
532
qemu_set_offload(NetClientState * nc,int csum,int tso4,int tso6,int ecn,int ufo,int uso4,int uso6)533 void qemu_set_offload(NetClientState *nc, int csum, int tso4, int tso6,
534 int ecn, int ufo, int uso4, int uso6)
535 {
536 if (!nc || !nc->info->set_offload) {
537 return;
538 }
539
540 nc->info->set_offload(nc, csum, tso4, tso6, ecn, ufo, uso4, uso6);
541 }
542
qemu_get_vnet_hdr_len(NetClientState * nc)543 int qemu_get_vnet_hdr_len(NetClientState *nc)
544 {
545 if (!nc) {
546 return 0;
547 }
548
549 return nc->vnet_hdr_len;
550 }
551
qemu_set_vnet_hdr_len(NetClientState * nc,int len)552 void qemu_set_vnet_hdr_len(NetClientState *nc, int len)
553 {
554 if (!nc || !nc->info->set_vnet_hdr_len) {
555 return;
556 }
557
558 assert(len == sizeof(struct virtio_net_hdr_mrg_rxbuf) ||
559 len == sizeof(struct virtio_net_hdr) ||
560 len == sizeof(struct virtio_net_hdr_v1_hash));
561
562 nc->vnet_hdr_len = len;
563 nc->info->set_vnet_hdr_len(nc, len);
564 }
565
qemu_set_vnet_le(NetClientState * nc,bool is_le)566 int qemu_set_vnet_le(NetClientState *nc, bool is_le)
567 {
568 #if HOST_BIG_ENDIAN
569 if (!nc || !nc->info->set_vnet_le) {
570 return -ENOSYS;
571 }
572
573 return nc->info->set_vnet_le(nc, is_le);
574 #else
575 return 0;
576 #endif
577 }
578
qemu_set_vnet_be(NetClientState * nc,bool is_be)579 int qemu_set_vnet_be(NetClientState *nc, bool is_be)
580 {
581 #if HOST_BIG_ENDIAN
582 return 0;
583 #else
584 if (!nc || !nc->info->set_vnet_be) {
585 return -ENOSYS;
586 }
587
588 return nc->info->set_vnet_be(nc, is_be);
589 #endif
590 }
591
qemu_can_receive_packet(NetClientState * nc)592 int qemu_can_receive_packet(NetClientState *nc)
593 {
594 if (nc->receive_disabled) {
595 return 0;
596 } else if (nc->info->can_receive &&
597 !nc->info->can_receive(nc)) {
598 return 0;
599 }
600 return 1;
601 }
602
qemu_can_send_packet(NetClientState * sender)603 int qemu_can_send_packet(NetClientState *sender)
604 {
605 int vm_running = runstate_is_running();
606
607 if (!vm_running) {
608 return 0;
609 }
610
611 if (!sender->peer) {
612 return 1;
613 }
614
615 return qemu_can_receive_packet(sender->peer);
616 }
617
filter_receive_iov(NetClientState * nc,NetFilterDirection direction,NetClientState * sender,unsigned flags,const struct iovec * iov,int iovcnt,NetPacketSent * sent_cb)618 static ssize_t filter_receive_iov(NetClientState *nc,
619 NetFilterDirection direction,
620 NetClientState *sender,
621 unsigned flags,
622 const struct iovec *iov,
623 int iovcnt,
624 NetPacketSent *sent_cb)
625 {
626 ssize_t ret = 0;
627 NetFilterState *nf = NULL;
628
629 if (direction == NET_FILTER_DIRECTION_TX) {
630 QTAILQ_FOREACH(nf, &nc->filters, next) {
631 ret = qemu_netfilter_receive(nf, direction, sender, flags, iov,
632 iovcnt, sent_cb);
633 if (ret) {
634 return ret;
635 }
636 }
637 } else {
638 QTAILQ_FOREACH_REVERSE(nf, &nc->filters, next) {
639 ret = qemu_netfilter_receive(nf, direction, sender, flags, iov,
640 iovcnt, sent_cb);
641 if (ret) {
642 return ret;
643 }
644 }
645 }
646
647 return ret;
648 }
649
filter_receive(NetClientState * nc,NetFilterDirection direction,NetClientState * sender,unsigned flags,const uint8_t * data,size_t size,NetPacketSent * sent_cb)650 static ssize_t filter_receive(NetClientState *nc,
651 NetFilterDirection direction,
652 NetClientState *sender,
653 unsigned flags,
654 const uint8_t *data,
655 size_t size,
656 NetPacketSent *sent_cb)
657 {
658 struct iovec iov = {
659 .iov_base = (void *)data,
660 .iov_len = size
661 };
662
663 return filter_receive_iov(nc, direction, sender, flags, &iov, 1, sent_cb);
664 }
665
qemu_purge_queued_packets(NetClientState * nc)666 void qemu_purge_queued_packets(NetClientState *nc)
667 {
668 if (!nc->peer) {
669 return;
670 }
671
672 qemu_net_queue_purge(nc->peer->incoming_queue, nc);
673 }
674
qemu_flush_or_purge_queued_packets(NetClientState * nc,bool purge)675 void qemu_flush_or_purge_queued_packets(NetClientState *nc, bool purge)
676 {
677 nc->receive_disabled = 0;
678
679 if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_HUBPORT) {
680 if (net_hub_flush(nc->peer)) {
681 qemu_notify_event();
682 }
683 }
684 if (qemu_net_queue_flush(nc->incoming_queue)) {
685 /* We emptied the queue successfully, signal to the IO thread to repoll
686 * the file descriptor (for tap, for example).
687 */
688 qemu_notify_event();
689 } else if (purge) {
690 /* Unable to empty the queue, purge remaining packets */
691 qemu_net_queue_purge(nc->incoming_queue, nc->peer);
692 }
693 }
694
qemu_flush_queued_packets(NetClientState * nc)695 void qemu_flush_queued_packets(NetClientState *nc)
696 {
697 qemu_flush_or_purge_queued_packets(nc, false);
698 }
699
qemu_send_packet_async_with_flags(NetClientState * sender,unsigned flags,const uint8_t * buf,int size,NetPacketSent * sent_cb)700 static ssize_t qemu_send_packet_async_with_flags(NetClientState *sender,
701 unsigned flags,
702 const uint8_t *buf, int size,
703 NetPacketSent *sent_cb)
704 {
705 NetQueue *queue;
706 int ret;
707
708 #ifdef DEBUG_NET
709 printf("qemu_send_packet_async:\n");
710 qemu_hexdump(stdout, "net", buf, size);
711 #endif
712
713 if (sender->link_down || !sender->peer) {
714 return size;
715 }
716
717 /* Let filters handle the packet first */
718 ret = filter_receive(sender, NET_FILTER_DIRECTION_TX,
719 sender, flags, buf, size, sent_cb);
720 if (ret) {
721 return ret;
722 }
723
724 ret = filter_receive(sender->peer, NET_FILTER_DIRECTION_RX,
725 sender, flags, buf, size, sent_cb);
726 if (ret) {
727 return ret;
728 }
729
730 queue = sender->peer->incoming_queue;
731
732 return qemu_net_queue_send(queue, sender, flags, buf, size, sent_cb);
733 }
734
qemu_send_packet_async(NetClientState * sender,const uint8_t * buf,int size,NetPacketSent * sent_cb)735 ssize_t qemu_send_packet_async(NetClientState *sender,
736 const uint8_t *buf, int size,
737 NetPacketSent *sent_cb)
738 {
739 return qemu_send_packet_async_with_flags(sender, QEMU_NET_PACKET_FLAG_NONE,
740 buf, size, sent_cb);
741 }
742
qemu_send_packet(NetClientState * nc,const uint8_t * buf,int size)743 ssize_t qemu_send_packet(NetClientState *nc, const uint8_t *buf, int size)
744 {
745 return qemu_send_packet_async(nc, buf, size, NULL);
746 }
747
qemu_receive_packet(NetClientState * nc,const uint8_t * buf,int size)748 ssize_t qemu_receive_packet(NetClientState *nc, const uint8_t *buf, int size)
749 {
750 if (!qemu_can_receive_packet(nc)) {
751 return 0;
752 }
753
754 return qemu_net_queue_receive(nc->incoming_queue, buf, size);
755 }
756
qemu_receive_packet_iov(NetClientState * nc,const struct iovec * iov,int iovcnt)757 ssize_t qemu_receive_packet_iov(NetClientState *nc, const struct iovec *iov,
758 int iovcnt)
759 {
760 if (!qemu_can_receive_packet(nc)) {
761 return 0;
762 }
763
764 return qemu_net_queue_receive_iov(nc->incoming_queue, iov, iovcnt);
765 }
766
qemu_send_packet_raw(NetClientState * nc,const uint8_t * buf,int size)767 ssize_t qemu_send_packet_raw(NetClientState *nc, const uint8_t *buf, int size)
768 {
769 return qemu_send_packet_async_with_flags(nc, QEMU_NET_PACKET_FLAG_RAW,
770 buf, size, NULL);
771 }
772
nc_sendv_compat(NetClientState * nc,const struct iovec * iov,int iovcnt,unsigned flags)773 static ssize_t nc_sendv_compat(NetClientState *nc, const struct iovec *iov,
774 int iovcnt, unsigned flags)
775 {
776 uint8_t *buf = NULL;
777 uint8_t *buffer;
778 size_t offset;
779 ssize_t ret;
780
781 if (iovcnt == 1) {
782 buffer = iov[0].iov_base;
783 offset = iov[0].iov_len;
784 } else {
785 offset = iov_size(iov, iovcnt);
786 if (offset > NET_BUFSIZE) {
787 return -1;
788 }
789 buf = g_malloc(offset);
790 buffer = buf;
791 offset = iov_to_buf(iov, iovcnt, 0, buf, offset);
792 }
793
794 ret = nc->info->receive(nc, buffer, offset);
795
796 g_free(buf);
797 return ret;
798 }
799
qemu_deliver_packet_iov(NetClientState * sender,unsigned flags,const struct iovec * iov,int iovcnt,void * opaque)800 static ssize_t qemu_deliver_packet_iov(NetClientState *sender,
801 unsigned flags,
802 const struct iovec *iov,
803 int iovcnt,
804 void *opaque)
805 {
806 MemReentrancyGuard *owned_reentrancy_guard;
807 NetClientState *nc = opaque;
808 int ret;
809 struct virtio_net_hdr_v1_hash vnet_hdr = { };
810 g_autofree struct iovec *iov_copy = NULL;
811
812
813 if (nc->link_down) {
814 return iov_size(iov, iovcnt);
815 }
816
817 if (nc->receive_disabled) {
818 return 0;
819 }
820
821 if (nc->info->type != NET_CLIENT_DRIVER_NIC ||
822 qemu_get_nic(nc)->reentrancy_guard->engaged_in_io) {
823 owned_reentrancy_guard = NULL;
824 } else {
825 owned_reentrancy_guard = qemu_get_nic(nc)->reentrancy_guard;
826 owned_reentrancy_guard->engaged_in_io = true;
827 }
828
829 if ((flags & QEMU_NET_PACKET_FLAG_RAW) && nc->vnet_hdr_len) {
830 iov_copy = g_new(struct iovec, iovcnt + 1);
831 iov_copy[0].iov_base = &vnet_hdr;
832 iov_copy[0].iov_len = nc->vnet_hdr_len;
833 memcpy(&iov_copy[1], iov, iovcnt * sizeof(*iov));
834 iov = iov_copy;
835 }
836
837 if (nc->info->receive_iov) {
838 ret = nc->info->receive_iov(nc, iov, iovcnt);
839 } else {
840 ret = nc_sendv_compat(nc, iov, iovcnt, flags);
841 }
842
843 if (owned_reentrancy_guard) {
844 owned_reentrancy_guard->engaged_in_io = false;
845 }
846
847 if (ret == 0) {
848 nc->receive_disabled = 1;
849 }
850
851 return ret;
852 }
853
qemu_sendv_packet_async(NetClientState * sender,const struct iovec * iov,int iovcnt,NetPacketSent * sent_cb)854 ssize_t qemu_sendv_packet_async(NetClientState *sender,
855 const struct iovec *iov, int iovcnt,
856 NetPacketSent *sent_cb)
857 {
858 NetQueue *queue;
859 size_t size = iov_size(iov, iovcnt);
860 int ret;
861
862 if (size > NET_BUFSIZE) {
863 return size;
864 }
865
866 if (sender->link_down || !sender->peer) {
867 return size;
868 }
869
870 /* Let filters handle the packet first */
871 ret = filter_receive_iov(sender, NET_FILTER_DIRECTION_TX, sender,
872 QEMU_NET_PACKET_FLAG_NONE, iov, iovcnt, sent_cb);
873 if (ret) {
874 return ret;
875 }
876
877 ret = filter_receive_iov(sender->peer, NET_FILTER_DIRECTION_RX, sender,
878 QEMU_NET_PACKET_FLAG_NONE, iov, iovcnt, sent_cb);
879 if (ret) {
880 return ret;
881 }
882
883 queue = sender->peer->incoming_queue;
884
885 return qemu_net_queue_send_iov(queue, sender,
886 QEMU_NET_PACKET_FLAG_NONE,
887 iov, iovcnt, sent_cb);
888 }
889
890 ssize_t
qemu_sendv_packet(NetClientState * nc,const struct iovec * iov,int iovcnt)891 qemu_sendv_packet(NetClientState *nc, const struct iovec *iov, int iovcnt)
892 {
893 return qemu_sendv_packet_async(nc, iov, iovcnt, NULL);
894 }
895
qemu_find_netdev(const char * id)896 NetClientState *qemu_find_netdev(const char *id)
897 {
898 NetClientState *nc;
899
900 QTAILQ_FOREACH(nc, &net_clients, next) {
901 if (nc->info->type == NET_CLIENT_DRIVER_NIC)
902 continue;
903 if (!strcmp(nc->name, id)) {
904 return nc;
905 }
906 }
907
908 return NULL;
909 }
910
qemu_find_net_clients_except(const char * id,NetClientState ** ncs,NetClientDriver type,int max)911 int qemu_find_net_clients_except(const char *id, NetClientState **ncs,
912 NetClientDriver type, int max)
913 {
914 NetClientState *nc;
915 int ret = 0;
916
917 QTAILQ_FOREACH(nc, &net_clients, next) {
918 if (nc->info->type == type) {
919 continue;
920 }
921 if (!id || !strcmp(nc->name, id)) {
922 if (ret < max) {
923 ncs[ret] = nc;
924 }
925 ret++;
926 }
927 }
928
929 return ret;
930 }
931
nic_get_free_idx(void)932 static int nic_get_free_idx(void)
933 {
934 int index;
935
936 for (index = 0; index < MAX_NICS; index++)
937 if (!nd_table[index].used)
938 return index;
939 return -1;
940 }
941
qemu_get_nic_models(const char * device_type)942 GPtrArray *qemu_get_nic_models(const char *device_type)
943 {
944 GPtrArray *nic_models = g_ptr_array_new();
945 GSList *list = object_class_get_list_sorted(device_type, false);
946
947 while (list) {
948 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, list->data,
949 TYPE_DEVICE);
950 GSList *next;
951 if (test_bit(DEVICE_CATEGORY_NETWORK, dc->categories) &&
952 dc->user_creatable) {
953 const char *name = object_class_get_name(list->data);
954 /*
955 * A network device might also be something else than a NIC, see
956 * e.g. the "rocker" device. Thus we have to look for the "netdev"
957 * property, too. Unfortunately, some devices like virtio-net only
958 * create this property during instance_init, so we have to create
959 * a temporary instance here to be able to check it.
960 */
961 Object *obj = object_new_with_class(OBJECT_CLASS(dc));
962 if (object_property_find(obj, "netdev")) {
963 g_ptr_array_add(nic_models, (gpointer)name);
964 }
965 object_unref(obj);
966 }
967 next = list->next;
968 g_slist_free_1(list);
969 list = next;
970 }
971 g_ptr_array_add(nic_models, NULL);
972
973 return nic_models;
974 }
975
net_init_nic(const Netdev * netdev,const char * name,NetClientState * peer,Error ** errp)976 static int net_init_nic(const Netdev *netdev, const char *name,
977 NetClientState *peer, Error **errp)
978 {
979 int idx;
980 NICInfo *nd;
981 const NetLegacyNicOptions *nic;
982
983 assert(netdev->type == NET_CLIENT_DRIVER_NIC);
984 nic = &netdev->u.nic;
985
986 idx = nic_get_free_idx();
987 if (idx == -1 || nb_nics >= MAX_NICS) {
988 error_setg(errp, "too many NICs");
989 return -1;
990 }
991
992 nd = &nd_table[idx];
993
994 memset(nd, 0, sizeof(*nd));
995
996 if (nic->netdev) {
997 nd->netdev = qemu_find_netdev(nic->netdev);
998 if (!nd->netdev) {
999 error_setg(errp, "netdev '%s' not found", nic->netdev);
1000 return -1;
1001 }
1002 } else {
1003 assert(peer);
1004 nd->netdev = peer;
1005 }
1006 nd->name = g_strdup(name);
1007 if (nic->model) {
1008 nd->model = g_strdup(nic->model);
1009 }
1010 if (nic->addr) {
1011 nd->devaddr = g_strdup(nic->addr);
1012 }
1013
1014 if (nic->macaddr &&
1015 net_parse_macaddr(nd->macaddr.a, nic->macaddr) < 0) {
1016 error_setg(errp, "invalid syntax for ethernet address");
1017 return -1;
1018 }
1019 if (nic->macaddr &&
1020 is_multicast_ether_addr(nd->macaddr.a)) {
1021 error_setg(errp,
1022 "NIC cannot have multicast MAC address (odd 1st byte)");
1023 return -1;
1024 }
1025 qemu_macaddr_default_if_unset(&nd->macaddr);
1026
1027 if (nic->has_vectors) {
1028 if (nic->vectors > 0x7ffffff) {
1029 error_setg(errp, "invalid # of vectors: %"PRIu32, nic->vectors);
1030 return -1;
1031 }
1032 nd->nvectors = nic->vectors;
1033 } else {
1034 nd->nvectors = DEV_NVECTORS_UNSPECIFIED;
1035 }
1036
1037 nd->used = 1;
1038 nb_nics++;
1039
1040 return idx;
1041 }
1042
add_nic_result(gpointer key,gpointer value,gpointer user_data)1043 static gboolean add_nic_result(gpointer key, gpointer value, gpointer user_data)
1044 {
1045 GPtrArray *results = user_data;
1046 GPtrArray *alias_list = value;
1047 const char *model = key;
1048 char *result;
1049
1050 if (!alias_list) {
1051 result = g_strdup(model);
1052 } else {
1053 GString *result_str = g_string_new(model);
1054 int i;
1055
1056 g_string_append(result_str, " (aka ");
1057 for (i = 0; i < alias_list->len; i++) {
1058 if (i) {
1059 g_string_append(result_str, ", ");
1060 }
1061 g_string_append(result_str, alias_list->pdata[i]);
1062 }
1063 g_string_append(result_str, ")");
1064 result = result_str->str;
1065 g_string_free(result_str, false);
1066 g_ptr_array_unref(alias_list);
1067 }
1068 g_ptr_array_add(results, result);
1069 return true;
1070 }
1071
model_cmp(char ** a,char ** b)1072 static int model_cmp(char **a, char **b)
1073 {
1074 return strcmp(*a, *b);
1075 }
1076
show_nic_models(void)1077 static void show_nic_models(void)
1078 {
1079 GPtrArray *results = g_ptr_array_new();
1080 int i;
1081
1082 g_hash_table_foreach_remove(nic_model_help, add_nic_result, results);
1083 g_ptr_array_sort(results, (GCompareFunc)model_cmp);
1084
1085 printf("Available NIC models for this configuration:\n");
1086 for (i = 0 ; i < results->len; i++) {
1087 printf("%s\n", (char *)results->pdata[i]);
1088 }
1089 g_hash_table_unref(nic_model_help);
1090 nic_model_help = NULL;
1091 }
1092
add_nic_model_help(const char * model,const char * alias)1093 static void add_nic_model_help(const char *model, const char *alias)
1094 {
1095 GPtrArray *alias_list = NULL;
1096
1097 if (g_hash_table_lookup_extended(nic_model_help, model, NULL,
1098 (gpointer *)&alias_list)) {
1099 /* Already exists, no alias to add: return */
1100 if (!alias) {
1101 return;
1102 }
1103 if (alias_list) {
1104 /* Check if this alias is already in the list. Add if not. */
1105 if (!g_ptr_array_find_with_equal_func(alias_list, alias,
1106 g_str_equal, NULL)) {
1107 g_ptr_array_add(alias_list, g_strdup(alias));
1108 }
1109 return;
1110 }
1111 }
1112 /* Either this model wasn't in the list already, or a first alias added */
1113 if (alias) {
1114 alias_list = g_ptr_array_new();
1115 g_ptr_array_set_free_func(alias_list, g_free);
1116 g_ptr_array_add(alias_list, g_strdup(alias));
1117 }
1118 g_hash_table_replace(nic_model_help, g_strdup(model), alias_list);
1119 }
1120
qemu_find_nic_info(const char * typename,bool match_default,const char * alias)1121 NICInfo *qemu_find_nic_info(const char *typename, bool match_default,
1122 const char *alias)
1123 {
1124 NICInfo *nd;
1125 int i;
1126
1127 if (nic_model_help) {
1128 add_nic_model_help(typename, alias);
1129 }
1130
1131 for (i = 0; i < nb_nics; i++) {
1132 nd = &nd_table[i];
1133
1134 if (!nd->used || nd->instantiated) {
1135 continue;
1136 }
1137
1138 if ((match_default && !nd->model) || !g_strcmp0(nd->model, typename)
1139 || (alias && !g_strcmp0(nd->model, alias))) {
1140 return nd;
1141 }
1142 }
1143 return NULL;
1144 }
1145
is_nic_model_help_option(const char * model)1146 static bool is_nic_model_help_option(const char *model)
1147 {
1148 if (model && is_help_option(model)) {
1149 /*
1150 * Trigger the help output by instantiating the hash table which
1151 * will gather tha available models as they get registered.
1152 */
1153 if (!nic_model_help) {
1154 nic_model_help = g_hash_table_new_full(g_str_hash, g_str_equal,
1155 g_free, NULL);
1156 }
1157 return true;
1158 }
1159 return false;
1160 }
1161
1162 /* "I have created a device. Please configure it if you can" */
qemu_configure_nic_device(DeviceState * dev,bool match_default,const char * alias)1163 bool qemu_configure_nic_device(DeviceState *dev, bool match_default,
1164 const char *alias)
1165 {
1166 NICInfo *nd = qemu_find_nic_info(object_get_typename(OBJECT(dev)),
1167 match_default, alias);
1168
1169 if (nd) {
1170 qdev_set_nic_properties(dev, nd);
1171 return true;
1172 }
1173 return false;
1174 }
1175
1176 /* "Please create a device, if you have a configuration for it" */
qemu_create_nic_device(const char * typename,bool match_default,const char * alias)1177 DeviceState *qemu_create_nic_device(const char *typename, bool match_default,
1178 const char *alias)
1179 {
1180 NICInfo *nd = qemu_find_nic_info(typename, match_default, alias);
1181 DeviceState *dev;
1182
1183 if (!nd) {
1184 return NULL;
1185 }
1186
1187 dev = qdev_new(typename);
1188 qdev_set_nic_properties(dev, nd);
1189 return dev;
1190 }
1191
qemu_create_nic_bus_devices(BusState * bus,const char * parent_type,const char * default_model,const char * alias,const char * alias_target)1192 void qemu_create_nic_bus_devices(BusState *bus, const char *parent_type,
1193 const char *default_model,
1194 const char *alias, const char *alias_target)
1195 {
1196 GPtrArray *nic_models = qemu_get_nic_models(parent_type);
1197 const char *model;
1198 DeviceState *dev;
1199 NICInfo *nd;
1200 int i;
1201
1202 if (nic_model_help) {
1203 if (alias_target) {
1204 add_nic_model_help(alias_target, alias);
1205 }
1206 for (i = 0; i < nic_models->len - 1; i++) {
1207 add_nic_model_help(nic_models->pdata[i], NULL);
1208 }
1209 }
1210
1211 /* Drop the NULL terminator which would make g_str_equal() unhappy */
1212 nic_models->len--;
1213
1214 for (i = 0; i < nb_nics; i++) {
1215 nd = &nd_table[i];
1216
1217 if (!nd->used || nd->instantiated) {
1218 continue;
1219 }
1220
1221 model = nd->model ? nd->model : default_model;
1222 if (!model) {
1223 continue;
1224 }
1225
1226 /* Each bus type is allowed *one* substitution */
1227 if (g_str_equal(model, alias)) {
1228 model = alias_target;
1229 }
1230
1231 if (!g_ptr_array_find_with_equal_func(nic_models, model,
1232 g_str_equal, NULL)) {
1233 /* This NIC does not live on this bus. */
1234 continue;
1235 }
1236
1237 dev = qdev_new(model);
1238 qdev_set_nic_properties(dev, nd);
1239 qdev_realize_and_unref(dev, bus, &error_fatal);
1240 }
1241
1242 g_ptr_array_free(nic_models, true);
1243 }
1244
1245 static int (* const net_client_init_fun[NET_CLIENT_DRIVER__MAX])(
1246 const Netdev *netdev,
1247 const char *name,
1248 NetClientState *peer, Error **errp) = {
1249 [NET_CLIENT_DRIVER_NIC] = net_init_nic,
1250 #ifdef CONFIG_SLIRP
1251 [NET_CLIENT_DRIVER_USER] = net_init_slirp,
1252 #endif
1253 [NET_CLIENT_DRIVER_TAP] = net_init_tap,
1254 [NET_CLIENT_DRIVER_SOCKET] = net_init_socket,
1255 [NET_CLIENT_DRIVER_STREAM] = net_init_stream,
1256 [NET_CLIENT_DRIVER_DGRAM] = net_init_dgram,
1257 #ifdef CONFIG_VDE
1258 [NET_CLIENT_DRIVER_VDE] = net_init_vde,
1259 #endif
1260 #ifdef CONFIG_NETMAP
1261 [NET_CLIENT_DRIVER_NETMAP] = net_init_netmap,
1262 #endif
1263 #ifdef CONFIG_AF_XDP
1264 [NET_CLIENT_DRIVER_AF_XDP] = net_init_af_xdp,
1265 #endif
1266 #ifdef CONFIG_NET_BRIDGE
1267 [NET_CLIENT_DRIVER_BRIDGE] = net_init_bridge,
1268 #endif
1269 [NET_CLIENT_DRIVER_HUBPORT] = net_init_hubport,
1270 #ifdef CONFIG_VHOST_NET_USER
1271 [NET_CLIENT_DRIVER_VHOST_USER] = net_init_vhost_user,
1272 #endif
1273 #ifdef CONFIG_VHOST_NET_VDPA
1274 [NET_CLIENT_DRIVER_VHOST_VDPA] = net_init_vhost_vdpa,
1275 #endif
1276 #ifdef CONFIG_L2TPV3
1277 [NET_CLIENT_DRIVER_L2TPV3] = net_init_l2tpv3,
1278 #endif
1279 #ifdef CONFIG_VMNET
1280 [NET_CLIENT_DRIVER_VMNET_HOST] = net_init_vmnet_host,
1281 [NET_CLIENT_DRIVER_VMNET_SHARED] = net_init_vmnet_shared,
1282 [NET_CLIENT_DRIVER_VMNET_BRIDGED] = net_init_vmnet_bridged,
1283 #endif /* CONFIG_VMNET */
1284 };
1285
1286
net_client_init1(const Netdev * netdev,bool is_netdev,Error ** errp)1287 static int net_client_init1(const Netdev *netdev, bool is_netdev, Error **errp)
1288 {
1289 NetClientState *peer = NULL;
1290 NetClientState *nc;
1291
1292 if (is_netdev) {
1293 if (netdev->type == NET_CLIENT_DRIVER_NIC ||
1294 !net_client_init_fun[netdev->type]) {
1295 error_setg(errp, "network backend '%s' is not compiled into this binary",
1296 NetClientDriver_str(netdev->type));
1297 return -1;
1298 }
1299 } else {
1300 if (netdev->type == NET_CLIENT_DRIVER_NONE) {
1301 return 0; /* nothing to do */
1302 }
1303 if (netdev->type == NET_CLIENT_DRIVER_HUBPORT) {
1304 error_setg(errp, "network backend '%s' is only supported with -netdev/-nic",
1305 NetClientDriver_str(netdev->type));
1306 return -1;
1307 }
1308
1309 if (!net_client_init_fun[netdev->type]) {
1310 error_setg(errp, "network backend '%s' is not compiled into this binary",
1311 NetClientDriver_str(netdev->type));
1312 return -1;
1313 }
1314
1315 /* Do not add to a hub if it's a nic with a netdev= parameter. */
1316 if (netdev->type != NET_CLIENT_DRIVER_NIC ||
1317 !netdev->u.nic.netdev) {
1318 peer = net_hub_add_port(0, NULL, NULL);
1319 }
1320 }
1321
1322 nc = qemu_find_netdev(netdev->id);
1323 if (nc) {
1324 error_setg(errp, "Duplicate ID '%s'", netdev->id);
1325 return -1;
1326 }
1327
1328 if (net_client_init_fun[netdev->type](netdev, netdev->id, peer, errp) < 0) {
1329 /* FIXME drop when all init functions store an Error */
1330 if (errp && !*errp) {
1331 error_setg(errp, "Device '%s' could not be initialized",
1332 NetClientDriver_str(netdev->type));
1333 }
1334 return -1;
1335 }
1336
1337 if (is_netdev) {
1338 nc = qemu_find_netdev(netdev->id);
1339 assert(nc);
1340 nc->is_netdev = true;
1341 }
1342
1343 return 0;
1344 }
1345
show_netdevs(void)1346 void show_netdevs(void)
1347 {
1348 int idx;
1349 const char *available_netdevs[] = {
1350 "socket",
1351 "stream",
1352 "dgram",
1353 "hubport",
1354 "tap",
1355 #ifdef CONFIG_SLIRP
1356 "user",
1357 #endif
1358 #ifdef CONFIG_L2TPV3
1359 "l2tpv3",
1360 #endif
1361 #ifdef CONFIG_VDE
1362 "vde",
1363 #endif
1364 #ifdef CONFIG_NET_BRIDGE
1365 "bridge",
1366 #endif
1367 #ifdef CONFIG_NETMAP
1368 "netmap",
1369 #endif
1370 #ifdef CONFIG_AF_XDP
1371 "af-xdp",
1372 #endif
1373 #ifdef CONFIG_POSIX
1374 "vhost-user",
1375 #endif
1376 #ifdef CONFIG_VHOST_VDPA
1377 "vhost-vdpa",
1378 #endif
1379 #ifdef CONFIG_VMNET
1380 "vmnet-host",
1381 "vmnet-shared",
1382 "vmnet-bridged",
1383 #endif
1384 };
1385
1386 qemu_printf("Available netdev backend types:\n");
1387 for (idx = 0; idx < ARRAY_SIZE(available_netdevs); idx++) {
1388 qemu_printf("%s\n", available_netdevs[idx]);
1389 }
1390 }
1391
net_client_init(QemuOpts * opts,bool is_netdev,Error ** errp)1392 static int net_client_init(QemuOpts *opts, bool is_netdev, Error **errp)
1393 {
1394 gchar **substrings = NULL;
1395 Netdev *object = NULL;
1396 int ret = -1;
1397 Visitor *v = opts_visitor_new(opts);
1398
1399 /* Parse convenience option format ipv6-net=fec0::0[/64] */
1400 const char *ip6_net = qemu_opt_get(opts, "ipv6-net");
1401
1402 if (ip6_net) {
1403 char *prefix_addr;
1404 unsigned long prefix_len = 64; /* Default 64bit prefix length. */
1405
1406 substrings = g_strsplit(ip6_net, "/", 2);
1407 if (!substrings || !substrings[0]) {
1408 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "ipv6-net",
1409 "a valid IPv6 prefix");
1410 goto out;
1411 }
1412
1413 prefix_addr = substrings[0];
1414
1415 /* Handle user-specified prefix length. */
1416 if (substrings[1] &&
1417 qemu_strtoul(substrings[1], NULL, 10, &prefix_len))
1418 {
1419 error_setg(errp,
1420 "parameter 'ipv6-net' expects a number after '/'");
1421 goto out;
1422 }
1423
1424 qemu_opt_set(opts, "ipv6-prefix", prefix_addr, &error_abort);
1425 qemu_opt_set_number(opts, "ipv6-prefixlen", prefix_len,
1426 &error_abort);
1427 qemu_opt_unset(opts, "ipv6-net");
1428 }
1429
1430 /* Create an ID for -net if the user did not specify one */
1431 if (!is_netdev && !qemu_opts_id(opts)) {
1432 qemu_opts_set_id(opts, id_generate(ID_NET));
1433 }
1434
1435 if (visit_type_Netdev(v, NULL, &object, errp)) {
1436 ret = net_client_init1(object, is_netdev, errp);
1437 }
1438
1439 qapi_free_Netdev(object);
1440
1441 out:
1442 g_strfreev(substrings);
1443 visit_free(v);
1444 return ret;
1445 }
1446
netdev_add(QemuOpts * opts,Error ** errp)1447 void netdev_add(QemuOpts *opts, Error **errp)
1448 {
1449 net_client_init(opts, true, errp);
1450 }
1451
qmp_netdev_add(Netdev * netdev,Error ** errp)1452 void qmp_netdev_add(Netdev *netdev, Error **errp)
1453 {
1454 if (!id_wellformed(netdev->id)) {
1455 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "id", "an identifier");
1456 return;
1457 }
1458
1459 net_client_init1(netdev, true, errp);
1460 }
1461
qmp_netdev_del(const char * id,Error ** errp)1462 void qmp_netdev_del(const char *id, Error **errp)
1463 {
1464 NetClientState *nc;
1465 QemuOpts *opts;
1466
1467 nc = qemu_find_netdev(id);
1468 if (!nc) {
1469 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1470 "Device '%s' not found", id);
1471 return;
1472 }
1473
1474 if (!nc->is_netdev) {
1475 error_setg(errp, "Device '%s' is not a netdev", id);
1476 return;
1477 }
1478
1479 qemu_del_net_client(nc);
1480
1481 /*
1482 * Wart: we need to delete the QemuOpts associated with netdevs
1483 * created via CLI or HMP, to avoid bogus "Duplicate ID" errors in
1484 * HMP netdev_add.
1485 */
1486 opts = qemu_opts_find(qemu_find_opts("netdev"), id);
1487 if (opts) {
1488 qemu_opts_del(opts);
1489 }
1490 }
1491
netfilter_print_info(Monitor * mon,NetFilterState * nf)1492 static void netfilter_print_info(Monitor *mon, NetFilterState *nf)
1493 {
1494 char *str;
1495 ObjectProperty *prop;
1496 ObjectPropertyIterator iter;
1497 Visitor *v;
1498
1499 /* generate info str */
1500 object_property_iter_init(&iter, OBJECT(nf));
1501 while ((prop = object_property_iter_next(&iter))) {
1502 if (!strcmp(prop->name, "type")) {
1503 continue;
1504 }
1505 v = string_output_visitor_new(false, &str);
1506 object_property_get(OBJECT(nf), prop->name, v, NULL);
1507 visit_complete(v, &str);
1508 visit_free(v);
1509 monitor_printf(mon, ",%s=%s", prop->name, str);
1510 g_free(str);
1511 }
1512 monitor_printf(mon, "\n");
1513 }
1514
print_net_client(Monitor * mon,NetClientState * nc)1515 void print_net_client(Monitor *mon, NetClientState *nc)
1516 {
1517 NetFilterState *nf;
1518
1519 monitor_printf(mon, "%s: index=%d,type=%s,%s\n", nc->name,
1520 nc->queue_index,
1521 NetClientDriver_str(nc->info->type),
1522 nc->info_str);
1523 if (!QTAILQ_EMPTY(&nc->filters)) {
1524 monitor_printf(mon, "filters:\n");
1525 }
1526 QTAILQ_FOREACH(nf, &nc->filters, next) {
1527 monitor_printf(mon, " - %s: type=%s",
1528 object_get_canonical_path_component(OBJECT(nf)),
1529 object_get_typename(OBJECT(nf)));
1530 netfilter_print_info(mon, nf);
1531 }
1532 }
1533
qmp_query_rx_filter(const char * name,Error ** errp)1534 RxFilterInfoList *qmp_query_rx_filter(const char *name, Error **errp)
1535 {
1536 NetClientState *nc;
1537 RxFilterInfoList *filter_list = NULL, **tail = &filter_list;
1538
1539 QTAILQ_FOREACH(nc, &net_clients, next) {
1540 RxFilterInfo *info;
1541
1542 if (name && strcmp(nc->name, name) != 0) {
1543 continue;
1544 }
1545
1546 /* only query rx-filter information of NIC */
1547 if (nc->info->type != NET_CLIENT_DRIVER_NIC) {
1548 if (name) {
1549 error_setg(errp, "net client(%s) isn't a NIC", name);
1550 assert(!filter_list);
1551 return NULL;
1552 }
1553 continue;
1554 }
1555
1556 /* only query information on queue 0 since the info is per nic,
1557 * not per queue
1558 */
1559 if (nc->queue_index != 0)
1560 continue;
1561
1562 if (nc->info->query_rx_filter) {
1563 info = nc->info->query_rx_filter(nc);
1564 QAPI_LIST_APPEND(tail, info);
1565 } else if (name) {
1566 error_setg(errp, "net client(%s) doesn't support"
1567 " rx-filter querying", name);
1568 assert(!filter_list);
1569 return NULL;
1570 }
1571
1572 if (name) {
1573 break;
1574 }
1575 }
1576
1577 if (filter_list == NULL && name) {
1578 error_setg(errp, "invalid net client name: %s", name);
1579 }
1580
1581 return filter_list;
1582 }
1583
colo_notify_filters_event(int event,Error ** errp)1584 void colo_notify_filters_event(int event, Error **errp)
1585 {
1586 NetClientState *nc;
1587 NetFilterState *nf;
1588 NetFilterClass *nfc = NULL;
1589 Error *local_err = NULL;
1590
1591 QTAILQ_FOREACH(nc, &net_clients, next) {
1592 QTAILQ_FOREACH(nf, &nc->filters, next) {
1593 nfc = NETFILTER_GET_CLASS(OBJECT(nf));
1594 nfc->handle_event(nf, event, &local_err);
1595 if (local_err) {
1596 error_propagate(errp, local_err);
1597 return;
1598 }
1599 }
1600 }
1601 }
1602
qmp_set_link(const char * name,bool up,Error ** errp)1603 void qmp_set_link(const char *name, bool up, Error **errp)
1604 {
1605 NetClientState *ncs[MAX_QUEUE_NUM];
1606 NetClientState *nc;
1607 int queues, i;
1608
1609 queues = qemu_find_net_clients_except(name, ncs,
1610 NET_CLIENT_DRIVER__MAX,
1611 MAX_QUEUE_NUM);
1612
1613 if (queues == 0) {
1614 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1615 "Device '%s' not found", name);
1616 return;
1617 }
1618 nc = ncs[0];
1619
1620 for (i = 0; i < queues; i++) {
1621 ncs[i]->link_down = !up;
1622 }
1623
1624 if (nc->info->link_status_changed) {
1625 nc->info->link_status_changed(nc);
1626 }
1627
1628 if (nc->peer) {
1629 /* Change peer link only if the peer is NIC and then notify peer.
1630 * If the peer is a HUBPORT or a backend, we do not change the
1631 * link status.
1632 *
1633 * This behavior is compatible with qemu hubs where there could be
1634 * multiple clients that can still communicate with each other in
1635 * disconnected mode. For now maintain this compatibility.
1636 */
1637 if (nc->peer->info->type == NET_CLIENT_DRIVER_NIC) {
1638 for (i = 0; i < queues; i++) {
1639 ncs[i]->peer->link_down = !up;
1640 }
1641 }
1642 if (nc->peer->info->link_status_changed) {
1643 nc->peer->info->link_status_changed(nc->peer);
1644 }
1645 }
1646 }
1647
net_vm_change_state_handler(void * opaque,bool running,RunState state)1648 static void net_vm_change_state_handler(void *opaque, bool running,
1649 RunState state)
1650 {
1651 NetClientState *nc;
1652 NetClientState *tmp;
1653
1654 QTAILQ_FOREACH_SAFE(nc, &net_clients, next, tmp) {
1655 if (running) {
1656 /* Flush queued packets and wake up backends. */
1657 if (nc->peer && qemu_can_send_packet(nc)) {
1658 qemu_flush_queued_packets(nc->peer);
1659 }
1660 } else {
1661 /* Complete all queued packets, to guarantee we don't modify
1662 * state later when VM is not running.
1663 */
1664 qemu_flush_or_purge_queued_packets(nc, true);
1665 }
1666 }
1667 }
1668
net_cleanup(void)1669 void net_cleanup(void)
1670 {
1671 NetClientState *nc, **p = &QTAILQ_FIRST(&net_clients);
1672
1673 /*cleanup colo compare module for COLO*/
1674 colo_compare_cleanup();
1675
1676 /*
1677 * Walk the net_clients list and remove the netdevs but *not* any
1678 * NET_CLIENT_DRIVER_NIC entries. The latter are owned by the device
1679 * model which created them, and in some cases (e.g. xen-net-device)
1680 * the device itself may do cleanup at exit and will be upset if we
1681 * just delete its NIC from underneath it.
1682 *
1683 * Since qemu_del_net_client() may delete multiple entries, using
1684 * QTAILQ_FOREACH_SAFE() is not safe here. The only safe pointer
1685 * to keep as a bookmark is a NET_CLIENT_DRIVER_NIC entry, so keep
1686 * 'p' pointing to either the head of the list, or the 'next' field
1687 * of the latest NET_CLIENT_DRIVER_NIC, and operate on *p as we walk
1688 * the list.
1689 *
1690 * The 'nc' variable isn't part of the list traversal; it's purely
1691 * for convenience as too much '(*p)->' has a tendency to make the
1692 * readers' eyes bleed.
1693 */
1694 while (*p) {
1695 nc = *p;
1696 if (nc->info->type == NET_CLIENT_DRIVER_NIC) {
1697 /* Skip NET_CLIENT_DRIVER_NIC entries */
1698 p = &QTAILQ_NEXT(nc, next);
1699 } else {
1700 qemu_del_net_client(nc);
1701 }
1702 }
1703
1704 qemu_del_vm_change_state_handler(net_change_state_entry);
1705 }
1706
net_check_clients(void)1707 void net_check_clients(void)
1708 {
1709 NetClientState *nc;
1710 int i;
1711
1712 if (nic_model_help) {
1713 show_nic_models();
1714 exit(0);
1715 }
1716 net_hub_check_clients();
1717
1718 QTAILQ_FOREACH(nc, &net_clients, next) {
1719 if (!nc->peer) {
1720 warn_report("%s %s has no peer",
1721 nc->info->type == NET_CLIENT_DRIVER_NIC
1722 ? "nic" : "netdev",
1723 nc->name);
1724 }
1725 }
1726
1727 /* Check that all NICs requested via -net nic actually got created.
1728 * NICs created via -device don't need to be checked here because
1729 * they are always instantiated.
1730 */
1731 for (i = 0; i < MAX_NICS; i++) {
1732 NICInfo *nd = &nd_table[i];
1733 if (nd->used && !nd->instantiated) {
1734 warn_report("requested NIC (%s, model %s) "
1735 "was not created (not supported by this machine?)",
1736 nd->name ? nd->name : "anonymous",
1737 nd->model ? nd->model : "unspecified");
1738 }
1739 }
1740 }
1741
net_init_client(void * dummy,QemuOpts * opts,Error ** errp)1742 static int net_init_client(void *dummy, QemuOpts *opts, Error **errp)
1743 {
1744 const char *model = qemu_opt_get(opts, "model");
1745
1746 if (is_nic_model_help_option(model)) {
1747 return 0;
1748 }
1749
1750 return net_client_init(opts, false, errp);
1751 }
1752
net_init_netdev(void * dummy,QemuOpts * opts,Error ** errp)1753 static int net_init_netdev(void *dummy, QemuOpts *opts, Error **errp)
1754 {
1755 const char *type = qemu_opt_get(opts, "type");
1756
1757 if (type && is_help_option(type)) {
1758 show_netdevs();
1759 exit(0);
1760 }
1761 return net_client_init(opts, true, errp);
1762 }
1763
1764 /* For the convenience "--nic" parameter */
net_param_nic(void * dummy,QemuOpts * opts,Error ** errp)1765 static int net_param_nic(void *dummy, QemuOpts *opts, Error **errp)
1766 {
1767 char *mac, *nd_id;
1768 int idx, ret;
1769 NICInfo *ni;
1770 const char *type;
1771
1772 type = qemu_opt_get(opts, "type");
1773 if (type) {
1774 if (g_str_equal(type, "none")) {
1775 return 0; /* Nothing to do, default_net is cleared in vl.c */
1776 }
1777 if (is_help_option(type)) {
1778 GPtrArray *nic_models = qemu_get_nic_models(TYPE_DEVICE);
1779 int i;
1780 show_netdevs();
1781 printf("\n");
1782 printf("Available NIC models "
1783 "(use -nic model=help for a filtered list):\n");
1784 for (i = 0 ; nic_models->pdata[i]; i++) {
1785 printf("%s\n", (char *)nic_models->pdata[i]);
1786 }
1787 g_ptr_array_free(nic_models, true);
1788 exit(0);
1789 }
1790 }
1791
1792 idx = nic_get_free_idx();
1793 if (idx == -1 || nb_nics >= MAX_NICS) {
1794 error_setg(errp, "no more on-board/default NIC slots available");
1795 return -1;
1796 }
1797
1798 if (!type) {
1799 qemu_opt_set(opts, "type", "user", &error_abort);
1800 }
1801
1802 ni = &nd_table[idx];
1803 memset(ni, 0, sizeof(*ni));
1804 ni->model = qemu_opt_get_del(opts, "model");
1805
1806 if (is_nic_model_help_option(ni->model)) {
1807 return 0;
1808 }
1809
1810 /* Create an ID if the user did not specify one */
1811 nd_id = g_strdup(qemu_opts_id(opts));
1812 if (!nd_id) {
1813 nd_id = id_generate(ID_NET);
1814 qemu_opts_set_id(opts, nd_id);
1815 }
1816
1817 /* Handle MAC address */
1818 mac = qemu_opt_get_del(opts, "mac");
1819 if (mac) {
1820 ret = net_parse_macaddr(ni->macaddr.a, mac);
1821 g_free(mac);
1822 if (ret) {
1823 error_setg(errp, "invalid syntax for ethernet address");
1824 goto out;
1825 }
1826 if (is_multicast_ether_addr(ni->macaddr.a)) {
1827 error_setg(errp, "NIC cannot have multicast MAC address");
1828 ret = -1;
1829 goto out;
1830 }
1831 }
1832 qemu_macaddr_default_if_unset(&ni->macaddr);
1833
1834 ret = net_client_init(opts, true, errp);
1835 if (ret == 0) {
1836 ni->netdev = qemu_find_netdev(nd_id);
1837 ni->used = true;
1838 nb_nics++;
1839 }
1840
1841 out:
1842 g_free(nd_id);
1843 return ret;
1844 }
1845
netdev_init_modern(void)1846 static void netdev_init_modern(void)
1847 {
1848 while (!QSIMPLEQ_EMPTY(&nd_queue)) {
1849 NetdevQueueEntry *nd = QSIMPLEQ_FIRST(&nd_queue);
1850
1851 QSIMPLEQ_REMOVE_HEAD(&nd_queue, entry);
1852 loc_push_restore(&nd->loc);
1853 net_client_init1(nd->nd, true, &error_fatal);
1854 loc_pop(&nd->loc);
1855 qapi_free_Netdev(nd->nd);
1856 g_free(nd);
1857 }
1858 }
1859
net_init_clients(void)1860 void net_init_clients(void)
1861 {
1862 net_change_state_entry =
1863 qemu_add_vm_change_state_handler(net_vm_change_state_handler, NULL);
1864
1865 QTAILQ_INIT(&net_clients);
1866
1867 netdev_init_modern();
1868
1869 qemu_opts_foreach(qemu_find_opts("netdev"), net_init_netdev, NULL,
1870 &error_fatal);
1871
1872 qemu_opts_foreach(qemu_find_opts("nic"), net_param_nic, NULL,
1873 &error_fatal);
1874
1875 qemu_opts_foreach(qemu_find_opts("net"), net_init_client, NULL,
1876 &error_fatal);
1877 }
1878
1879 /*
1880 * Does this -netdev argument use modern rather than traditional syntax?
1881 * Modern syntax is to be parsed with netdev_parse_modern().
1882 * Traditional syntax is to be parsed with net_client_parse().
1883 */
netdev_is_modern(const char * optstr)1884 bool netdev_is_modern(const char *optstr)
1885 {
1886 QemuOpts *opts;
1887 bool is_modern;
1888 const char *type;
1889 static QemuOptsList dummy_opts = {
1890 .name = "netdev",
1891 .implied_opt_name = "type",
1892 .head = QTAILQ_HEAD_INITIALIZER(dummy_opts.head),
1893 .desc = { { } },
1894 };
1895
1896 if (optstr[0] == '{') {
1897 /* This is JSON, which means it's modern syntax */
1898 return true;
1899 }
1900
1901 opts = qemu_opts_create(&dummy_opts, NULL, false, &error_abort);
1902 qemu_opts_do_parse(opts, optstr, dummy_opts.implied_opt_name,
1903 &error_abort);
1904 type = qemu_opt_get(opts, "type");
1905 is_modern = !g_strcmp0(type, "stream") || !g_strcmp0(type, "dgram");
1906
1907 qemu_opts_reset(&dummy_opts);
1908
1909 return is_modern;
1910 }
1911
1912 /*
1913 * netdev_parse_modern() uses modern, more expressive syntax than
1914 * net_client_parse(), but supports only the -netdev option.
1915 * netdev_parse_modern() appends to @nd_queue, whereas net_client_parse()
1916 * appends to @qemu_netdev_opts.
1917 */
netdev_parse_modern(const char * optstr)1918 void netdev_parse_modern(const char *optstr)
1919 {
1920 Visitor *v;
1921 NetdevQueueEntry *nd;
1922
1923 v = qobject_input_visitor_new_str(optstr, "type", &error_fatal);
1924 nd = g_new(NetdevQueueEntry, 1);
1925 visit_type_Netdev(v, NULL, &nd->nd, &error_fatal);
1926 visit_free(v);
1927 loc_save(&nd->loc);
1928
1929 QSIMPLEQ_INSERT_TAIL(&nd_queue, nd, entry);
1930 }
1931
net_client_parse(QemuOptsList * opts_list,const char * optstr)1932 void net_client_parse(QemuOptsList *opts_list, const char *optstr)
1933 {
1934 if (!qemu_opts_parse_noisily(opts_list, optstr, true)) {
1935 exit(1);
1936 }
1937 }
1938
1939 /* From FreeBSD */
1940 /* XXX: optimize */
net_crc32(const uint8_t * p,int len)1941 uint32_t net_crc32(const uint8_t *p, int len)
1942 {
1943 uint32_t crc;
1944 int carry, i, j;
1945 uint8_t b;
1946
1947 crc = 0xffffffff;
1948 for (i = 0; i < len; i++) {
1949 b = *p++;
1950 for (j = 0; j < 8; j++) {
1951 carry = ((crc & 0x80000000L) ? 1 : 0) ^ (b & 0x01);
1952 crc <<= 1;
1953 b >>= 1;
1954 if (carry) {
1955 crc = ((crc ^ POLYNOMIAL_BE) | carry);
1956 }
1957 }
1958 }
1959
1960 return crc;
1961 }
1962
net_crc32_le(const uint8_t * p,int len)1963 uint32_t net_crc32_le(const uint8_t *p, int len)
1964 {
1965 uint32_t crc;
1966 int carry, i, j;
1967 uint8_t b;
1968
1969 crc = 0xffffffff;
1970 for (i = 0; i < len; i++) {
1971 b = *p++;
1972 for (j = 0; j < 8; j++) {
1973 carry = (crc & 0x1) ^ (b & 0x01);
1974 crc >>= 1;
1975 b >>= 1;
1976 if (carry) {
1977 crc ^= POLYNOMIAL_LE;
1978 }
1979 }
1980 }
1981
1982 return crc;
1983 }
1984
1985 QemuOptsList qemu_netdev_opts = {
1986 .name = "netdev",
1987 .implied_opt_name = "type",
1988 .head = QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts.head),
1989 .desc = {
1990 /*
1991 * no elements => accept any params
1992 * validation will happen later
1993 */
1994 { /* end of list */ }
1995 },
1996 };
1997
1998 QemuOptsList qemu_nic_opts = {
1999 .name = "nic",
2000 .implied_opt_name = "type",
2001 .head = QTAILQ_HEAD_INITIALIZER(qemu_nic_opts.head),
2002 .desc = {
2003 /*
2004 * no elements => accept any params
2005 * validation will happen later
2006 */
2007 { /* end of list */ }
2008 },
2009 };
2010
2011 QemuOptsList qemu_net_opts = {
2012 .name = "net",
2013 .implied_opt_name = "type",
2014 .head = QTAILQ_HEAD_INITIALIZER(qemu_net_opts.head),
2015 .desc = {
2016 /*
2017 * no elements => accept any params
2018 * validation will happen later
2019 */
2020 { /* end of list */ }
2021 },
2022 };
2023
net_socket_rs_init(SocketReadState * rs,SocketReadStateFinalize * finalize,bool vnet_hdr)2024 void net_socket_rs_init(SocketReadState *rs,
2025 SocketReadStateFinalize *finalize,
2026 bool vnet_hdr)
2027 {
2028 rs->state = 0;
2029 rs->vnet_hdr = vnet_hdr;
2030 rs->index = 0;
2031 rs->packet_len = 0;
2032 rs->vnet_hdr_len = 0;
2033 memset(rs->buf, 0, sizeof(rs->buf));
2034 rs->finalize = finalize;
2035 }
2036
2037 /*
2038 * Returns
2039 * 0: success
2040 * -1: error occurs
2041 */
net_fill_rstate(SocketReadState * rs,const uint8_t * buf,int size)2042 int net_fill_rstate(SocketReadState *rs, const uint8_t *buf, int size)
2043 {
2044 unsigned int l;
2045
2046 while (size > 0) {
2047 /* Reassemble a packet from the network.
2048 * 0 = getting length.
2049 * 1 = getting vnet header length.
2050 * 2 = getting data.
2051 */
2052 switch (rs->state) {
2053 case 0:
2054 l = 4 - rs->index;
2055 if (l > size) {
2056 l = size;
2057 }
2058 memcpy(rs->buf + rs->index, buf, l);
2059 buf += l;
2060 size -= l;
2061 rs->index += l;
2062 if (rs->index == 4) {
2063 /* got length */
2064 rs->packet_len = ntohl(*(uint32_t *)rs->buf);
2065 rs->index = 0;
2066 if (rs->vnet_hdr) {
2067 rs->state = 1;
2068 } else {
2069 rs->state = 2;
2070 rs->vnet_hdr_len = 0;
2071 }
2072 }
2073 break;
2074 case 1:
2075 l = 4 - rs->index;
2076 if (l > size) {
2077 l = size;
2078 }
2079 memcpy(rs->buf + rs->index, buf, l);
2080 buf += l;
2081 size -= l;
2082 rs->index += l;
2083 if (rs->index == 4) {
2084 /* got vnet header length */
2085 rs->vnet_hdr_len = ntohl(*(uint32_t *)rs->buf);
2086 rs->index = 0;
2087 rs->state = 2;
2088 }
2089 break;
2090 case 2:
2091 l = rs->packet_len - rs->index;
2092 if (l > size) {
2093 l = size;
2094 }
2095 if (rs->index + l <= sizeof(rs->buf)) {
2096 memcpy(rs->buf + rs->index, buf, l);
2097 } else {
2098 fprintf(stderr, "serious error: oversized packet received,"
2099 "connection terminated.\n");
2100 rs->index = rs->state = 0;
2101 return -1;
2102 }
2103
2104 rs->index += l;
2105 buf += l;
2106 size -= l;
2107 if (rs->index >= rs->packet_len) {
2108 rs->index = 0;
2109 rs->state = 0;
2110 assert(rs->finalize);
2111 rs->finalize(rs);
2112 }
2113 break;
2114 }
2115 }
2116
2117 assert(size == 0);
2118 return 0;
2119 }
2120