4 * Copyright (c) 2003-2008 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
25 #include "qemu/osdep.h"
30 #include "hw/qdev-properties.h"
31 #include "net/slirp.h"
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"
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"
60 /* Net bridge is currently not supported for W32. */
62 # define CONFIG_NET_BRIDGE
65 static VMChangeStateEntry
*net_change_state_entry
;
66 NetClientStateList net_clients
;
68 typedef struct NetdevQueueEntry
{
71 QSIMPLEQ_ENTRY(NetdevQueueEntry
) entry
;
74 typedef QSIMPLEQ_HEAD(, NetdevQueueEntry
) NetdevQueue
;
76 static NetdevQueue nd_queue
= QSIMPLEQ_HEAD_INITIALIZER(nd_queue
);
78 /***********************************************************/
79 /* network device redirectors */
81 int convert_host_port(struct sockaddr_in
*saddr
, const char *host
,
82 const char *port
, Error
**errp
)
88 memset(saddr
, 0, sizeof(*saddr
));
90 saddr
->sin_family
= AF_INET
;
91 if (host
[0] == '\0') {
92 saddr
->sin_addr
.s_addr
= 0;
94 if (qemu_isdigit(host
[0])) {
95 if (!inet_aton(host
, &saddr
->sin_addr
)) {
96 error_setg(errp
, "host address '%s' is not a valid "
97 "IPv4 address", host
);
101 he
= gethostbyname(host
);
103 error_setg(errp
, "can't resolve host address '%s'", host
);
106 saddr
->sin_addr
= *(struct in_addr
*)he
->h_addr
;
109 if (qemu_strtol(port
, &r
, 0, &p
) != 0) {
110 error_setg(errp
, "port number '%s' is invalid", port
);
113 saddr
->sin_port
= htons(p
);
117 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
,
123 substrings
= g_strsplit(str
, ":", 2);
124 if (!substrings
|| !substrings
[0] || !substrings
[1]) {
125 error_setg(errp
, "host address '%s' doesn't contain ':' "
126 "separating host from port", str
);
131 ret
= convert_host_port(saddr
, substrings
[0], substrings
[1], errp
);
134 g_strfreev(substrings
);
138 char *qemu_mac_strdup_printf(const uint8_t *macaddr
)
140 return g_strdup_printf("%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",
141 macaddr
[0], macaddr
[1], macaddr
[2],
142 macaddr
[3], macaddr
[4], macaddr
[5]);
145 void qemu_set_info_str(NetClientState
*nc
, const char *fmt
, ...)
150 vsnprintf(nc
->info_str
, sizeof(nc
->info_str
), fmt
, ap
);
154 void qemu_format_nic_info_str(NetClientState
*nc
, uint8_t macaddr
[6])
156 qemu_set_info_str(nc
, "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
157 nc
->model
, macaddr
[0], macaddr
[1], macaddr
[2],
158 macaddr
[3], macaddr
[4], macaddr
[5]);
161 static int mac_table
[256] = {0};
163 static void qemu_macaddr_set_used(MACAddr
*macaddr
)
167 for (index
= 0x56; index
< 0xFF; index
++) {
168 if (macaddr
->a
[5] == index
) {
174 static void qemu_macaddr_set_free(MACAddr
*macaddr
)
177 static const MACAddr base
= { .a
= { 0x52, 0x54, 0x00, 0x12, 0x34, 0 } };
179 if (memcmp(macaddr
->a
, &base
.a
, (sizeof(base
.a
) - 1)) != 0) {
182 for (index
= 0x56; index
< 0xFF; index
++) {
183 if (macaddr
->a
[5] == index
) {
189 static int qemu_macaddr_get_free(void)
193 for (index
= 0x56; index
< 0xFF; index
++) {
194 if (mac_table
[index
] == 0) {
202 void qemu_macaddr_default_if_unset(MACAddr
*macaddr
)
204 static const MACAddr zero
= { .a
= { 0,0,0,0,0,0 } };
205 static const MACAddr base
= { .a
= { 0x52, 0x54, 0x00, 0x12, 0x34, 0 } };
207 if (memcmp(macaddr
, &zero
, sizeof(zero
)) != 0) {
208 if (memcmp(macaddr
->a
, &base
.a
, (sizeof(base
.a
) - 1)) != 0) {
211 qemu_macaddr_set_used(macaddr
);
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;
221 macaddr
->a
[5] = qemu_macaddr_get_free();
222 qemu_macaddr_set_used(macaddr
);
226 * Generate a name for net client
228 * Only net clients created with the legacy -net option and NICs need this.
230 static char *assign_name(NetClientState
*nc1
, const char *model
)
235 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
239 if (strcmp(nc
->model
, model
) == 0) {
244 return g_strdup_printf("%s.%d", model
, id
);
247 static void qemu_net_client_destructor(NetClientState
*nc
)
251 static ssize_t
qemu_deliver_packet_iov(NetClientState
*sender
,
253 const struct iovec
*iov
,
257 static void qemu_net_client_setup(NetClientState
*nc
,
259 NetClientState
*peer
,
262 NetClientDestructor
*destructor
,
266 nc
->model
= g_strdup(model
);
268 nc
->name
= g_strdup(name
);
270 nc
->name
= assign_name(nc
, model
);
278 QTAILQ_INSERT_TAIL(&net_clients
, nc
, next
);
280 nc
->incoming_queue
= qemu_new_net_queue(qemu_deliver_packet_iov
, nc
);
281 nc
->destructor
= destructor
;
282 nc
->is_datapath
= is_datapath
;
283 QTAILQ_INIT(&nc
->filters
);
286 NetClientState
*qemu_new_net_client(NetClientInfo
*info
,
287 NetClientState
*peer
,
293 assert(info
->size
>= sizeof(NetClientState
));
295 nc
= g_malloc0(info
->size
);
296 qemu_net_client_setup(nc
, info
, peer
, model
, name
,
297 qemu_net_client_destructor
, true);
302 NetClientState
*qemu_new_net_control_client(NetClientInfo
*info
,
303 NetClientState
*peer
,
309 assert(info
->size
>= sizeof(NetClientState
));
311 nc
= g_malloc0(info
->size
);
312 qemu_net_client_setup(nc
, info
, peer
, model
, name
,
313 qemu_net_client_destructor
, false);
318 NICState
*qemu_new_nic(NetClientInfo
*info
,
322 MemReentrancyGuard
*reentrancy_guard
,
325 NetClientState
**peers
= conf
->peers
.ncs
;
327 int i
, queues
= MAX(1, conf
->peers
.queues
);
329 assert(info
->type
== NET_CLIENT_DRIVER_NIC
);
330 assert(info
->size
>= sizeof(NICState
));
332 nic
= g_malloc0(info
->size
+ sizeof(NetClientState
) * queues
);
333 nic
->ncs
= (void *)nic
+ info
->size
;
335 nic
->reentrancy_guard
= reentrancy_guard
,
336 nic
->opaque
= opaque
;
338 for (i
= 0; i
< queues
; i
++) {
339 qemu_net_client_setup(&nic
->ncs
[i
], info
, peers
[i
], model
, name
,
341 nic
->ncs
[i
].queue_index
= i
;
347 NetClientState
*qemu_get_subqueue(NICState
*nic
, int queue_index
)
349 return nic
->ncs
+ queue_index
;
352 NetClientState
*qemu_get_queue(NICState
*nic
)
354 return qemu_get_subqueue(nic
, 0);
357 NICState
*qemu_get_nic(NetClientState
*nc
)
359 NetClientState
*nc0
= nc
- nc
->queue_index
;
361 return (NICState
*)((void *)nc0
- nc
->info
->size
);
364 void *qemu_get_nic_opaque(NetClientState
*nc
)
366 NICState
*nic
= qemu_get_nic(nc
);
371 NetClientState
*qemu_get_peer(NetClientState
*nc
, int queue_index
)
374 NetClientState
*ncs
= nc
+ queue_index
;
378 static void qemu_cleanup_net_client(NetClientState
*nc
)
380 QTAILQ_REMOVE(&net_clients
, nc
, next
);
382 if (nc
->info
->cleanup
) {
383 nc
->info
->cleanup(nc
);
387 static void qemu_free_net_client(NetClientState
*nc
)
389 if (nc
->incoming_queue
) {
390 qemu_del_net_queue(nc
->incoming_queue
);
393 nc
->peer
->peer
= NULL
;
397 if (nc
->destructor
) {
402 void qemu_del_net_client(NetClientState
*nc
)
404 NetClientState
*ncs
[MAX_QUEUE_NUM
];
406 NetFilterState
*nf
, *next
;
408 assert(nc
->info
->type
!= NET_CLIENT_DRIVER_NIC
);
410 /* If the NetClientState belongs to a multiqueue backend, we will change all
411 * other NetClientStates also.
413 queues
= qemu_find_net_clients_except(nc
->name
, ncs
,
414 NET_CLIENT_DRIVER_NIC
,
418 QTAILQ_FOREACH_SAFE(nf
, &nc
->filters
, next
, next
) {
419 object_unparent(OBJECT(nf
));
422 /* If there is a peer NIC, delete and cleanup client, but do not free. */
423 if (nc
->peer
&& nc
->peer
->info
->type
== NET_CLIENT_DRIVER_NIC
) {
424 NICState
*nic
= qemu_get_nic(nc
->peer
);
425 if (nic
->peer_deleted
) {
428 nic
->peer_deleted
= true;
430 for (i
= 0; i
< queues
; i
++) {
431 ncs
[i
]->peer
->link_down
= true;
434 if (nc
->peer
->info
->link_status_changed
) {
435 nc
->peer
->info
->link_status_changed(nc
->peer
);
438 for (i
= 0; i
< queues
; i
++) {
439 qemu_cleanup_net_client(ncs
[i
]);
445 for (i
= 0; i
< queues
; i
++) {
446 qemu_cleanup_net_client(ncs
[i
]);
447 qemu_free_net_client(ncs
[i
]);
451 void qemu_del_nic(NICState
*nic
)
453 int i
, queues
= MAX(nic
->conf
->peers
.queues
, 1);
455 qemu_macaddr_set_free(&nic
->conf
->macaddr
);
457 for (i
= 0; i
< queues
; i
++) {
458 NetClientState
*nc
= qemu_get_subqueue(nic
, i
);
459 /* If this is a peer NIC and peer has already been deleted, free it now. */
460 if (nic
->peer_deleted
) {
461 qemu_free_net_client(nc
->peer
);
462 } else if (nc
->peer
) {
463 /* if there are RX packets pending, complete them */
464 qemu_purge_queued_packets(nc
->peer
);
468 for (i
= queues
- 1; i
>= 0; i
--) {
469 NetClientState
*nc
= qemu_get_subqueue(nic
, i
);
471 qemu_cleanup_net_client(nc
);
472 qemu_free_net_client(nc
);
478 void qemu_foreach_nic(qemu_nic_foreach func
, void *opaque
)
482 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
483 if (nc
->info
->type
== NET_CLIENT_DRIVER_NIC
) {
484 if (nc
->queue_index
== 0) {
485 func(qemu_get_nic(nc
), opaque
);
491 bool qemu_has_ufo(NetClientState
*nc
)
493 if (!nc
|| !nc
->info
->has_ufo
) {
497 return nc
->info
->has_ufo(nc
);
500 bool qemu_has_uso(NetClientState
*nc
)
502 if (!nc
|| !nc
->info
->has_uso
) {
506 return nc
->info
->has_uso(nc
);
509 bool qemu_has_vnet_hdr(NetClientState
*nc
)
511 if (!nc
|| !nc
->info
->has_vnet_hdr
) {
515 return nc
->info
->has_vnet_hdr(nc
);
518 bool qemu_has_vnet_hdr_len(NetClientState
*nc
, int len
)
520 if (!nc
|| !nc
->info
->has_vnet_hdr_len
) {
524 return nc
->info
->has_vnet_hdr_len(nc
, len
);
527 bool qemu_get_using_vnet_hdr(NetClientState
*nc
)
529 if (!nc
|| !nc
->info
->get_using_vnet_hdr
) {
533 return nc
->info
->get_using_vnet_hdr(nc
);
536 void qemu_using_vnet_hdr(NetClientState
*nc
, bool enable
)
538 if (!nc
|| !nc
->info
->using_vnet_hdr
) {
542 nc
->info
->using_vnet_hdr(nc
, enable
);
545 void qemu_set_offload(NetClientState
*nc
, int csum
, int tso4
, int tso6
,
546 int ecn
, int ufo
, int uso4
, int uso6
)
548 if (!nc
|| !nc
->info
->set_offload
) {
552 nc
->info
->set_offload(nc
, csum
, tso4
, tso6
, ecn
, ufo
, uso4
, uso6
);
555 int qemu_get_vnet_hdr_len(NetClientState
*nc
)
557 if (!nc
|| !nc
->info
->get_vnet_hdr_len
) {
561 return nc
->info
->get_vnet_hdr_len(nc
);
564 void qemu_set_vnet_hdr_len(NetClientState
*nc
, int len
)
566 if (!nc
|| !nc
->info
->set_vnet_hdr_len
) {
570 nc
->vnet_hdr_len
= len
;
571 nc
->info
->set_vnet_hdr_len(nc
, len
);
574 int qemu_set_vnet_le(NetClientState
*nc
, bool is_le
)
577 if (!nc
|| !nc
->info
->set_vnet_le
) {
581 return nc
->info
->set_vnet_le(nc
, is_le
);
587 int qemu_set_vnet_be(NetClientState
*nc
, bool is_be
)
592 if (!nc
|| !nc
->info
->set_vnet_be
) {
596 return nc
->info
->set_vnet_be(nc
, is_be
);
600 int qemu_can_receive_packet(NetClientState
*nc
)
602 if (nc
->receive_disabled
) {
604 } else if (nc
->info
->can_receive
&&
605 !nc
->info
->can_receive(nc
)) {
611 int qemu_can_send_packet(NetClientState
*sender
)
613 int vm_running
= runstate_is_running();
623 return qemu_can_receive_packet(sender
->peer
);
626 static ssize_t
filter_receive_iov(NetClientState
*nc
,
627 NetFilterDirection direction
,
628 NetClientState
*sender
,
630 const struct iovec
*iov
,
632 NetPacketSent
*sent_cb
)
635 NetFilterState
*nf
= NULL
;
637 if (direction
== NET_FILTER_DIRECTION_TX
) {
638 QTAILQ_FOREACH(nf
, &nc
->filters
, next
) {
639 ret
= qemu_netfilter_receive(nf
, direction
, sender
, flags
, iov
,
646 QTAILQ_FOREACH_REVERSE(nf
, &nc
->filters
, next
) {
647 ret
= qemu_netfilter_receive(nf
, direction
, sender
, flags
, iov
,
658 static ssize_t
filter_receive(NetClientState
*nc
,
659 NetFilterDirection direction
,
660 NetClientState
*sender
,
664 NetPacketSent
*sent_cb
)
667 .iov_base
= (void *)data
,
671 return filter_receive_iov(nc
, direction
, sender
, flags
, &iov
, 1, sent_cb
);
674 void qemu_purge_queued_packets(NetClientState
*nc
)
680 qemu_net_queue_purge(nc
->peer
->incoming_queue
, nc
);
683 void qemu_flush_or_purge_queued_packets(NetClientState
*nc
, bool purge
)
685 nc
->receive_disabled
= 0;
687 if (nc
->peer
&& nc
->peer
->info
->type
== NET_CLIENT_DRIVER_HUBPORT
) {
688 if (net_hub_flush(nc
->peer
)) {
692 if (qemu_net_queue_flush(nc
->incoming_queue
)) {
693 /* We emptied the queue successfully, signal to the IO thread to repoll
694 * the file descriptor (for tap, for example).
698 /* Unable to empty the queue, purge remaining packets */
699 qemu_net_queue_purge(nc
->incoming_queue
, nc
->peer
);
703 void qemu_flush_queued_packets(NetClientState
*nc
)
705 qemu_flush_or_purge_queued_packets(nc
, false);
708 static ssize_t
qemu_send_packet_async_with_flags(NetClientState
*sender
,
710 const uint8_t *buf
, int size
,
711 NetPacketSent
*sent_cb
)
717 printf("qemu_send_packet_async:\n");
718 qemu_hexdump(stdout
, "net", buf
, size
);
721 if (sender
->link_down
|| !sender
->peer
) {
725 /* Let filters handle the packet first */
726 ret
= filter_receive(sender
, NET_FILTER_DIRECTION_TX
,
727 sender
, flags
, buf
, size
, sent_cb
);
732 ret
= filter_receive(sender
->peer
, NET_FILTER_DIRECTION_RX
,
733 sender
, flags
, buf
, size
, sent_cb
);
738 queue
= sender
->peer
->incoming_queue
;
740 return qemu_net_queue_send(queue
, sender
, flags
, buf
, size
, sent_cb
);
743 ssize_t
qemu_send_packet_async(NetClientState
*sender
,
744 const uint8_t *buf
, int size
,
745 NetPacketSent
*sent_cb
)
747 return qemu_send_packet_async_with_flags(sender
, QEMU_NET_PACKET_FLAG_NONE
,
751 ssize_t
qemu_send_packet(NetClientState
*nc
, const uint8_t *buf
, int size
)
753 return qemu_send_packet_async(nc
, buf
, size
, NULL
);
756 ssize_t
qemu_receive_packet(NetClientState
*nc
, const uint8_t *buf
, int size
)
758 if (!qemu_can_receive_packet(nc
)) {
762 return qemu_net_queue_receive(nc
->incoming_queue
, buf
, size
);
765 ssize_t
qemu_receive_packet_iov(NetClientState
*nc
, const struct iovec
*iov
,
768 if (!qemu_can_receive_packet(nc
)) {
772 return qemu_net_queue_receive_iov(nc
->incoming_queue
, iov
, iovcnt
);
775 ssize_t
qemu_send_packet_raw(NetClientState
*nc
, const uint8_t *buf
, int size
)
777 return qemu_send_packet_async_with_flags(nc
, QEMU_NET_PACKET_FLAG_RAW
,
781 static ssize_t
nc_sendv_compat(NetClientState
*nc
, const struct iovec
*iov
,
782 int iovcnt
, unsigned flags
)
790 buffer
= iov
[0].iov_base
;
791 offset
= iov
[0].iov_len
;
793 offset
= iov_size(iov
, iovcnt
);
794 if (offset
> NET_BUFSIZE
) {
797 buf
= g_malloc(offset
);
799 offset
= iov_to_buf(iov
, iovcnt
, 0, buf
, offset
);
802 if (flags
& QEMU_NET_PACKET_FLAG_RAW
&& nc
->info
->receive_raw
) {
803 ret
= nc
->info
->receive_raw(nc
, buffer
, offset
);
805 ret
= nc
->info
->receive(nc
, buffer
, offset
);
812 static ssize_t
qemu_deliver_packet_iov(NetClientState
*sender
,
814 const struct iovec
*iov
,
818 MemReentrancyGuard
*owned_reentrancy_guard
;
819 NetClientState
*nc
= opaque
;
824 return iov_size(iov
, iovcnt
);
827 if (nc
->receive_disabled
) {
831 if (nc
->info
->type
!= NET_CLIENT_DRIVER_NIC
||
832 qemu_get_nic(nc
)->reentrancy_guard
->engaged_in_io
) {
833 owned_reentrancy_guard
= NULL
;
835 owned_reentrancy_guard
= qemu_get_nic(nc
)->reentrancy_guard
;
836 owned_reentrancy_guard
->engaged_in_io
= true;
839 if (nc
->info
->receive_iov
&& !(flags
& QEMU_NET_PACKET_FLAG_RAW
)) {
840 ret
= nc
->info
->receive_iov(nc
, iov
, iovcnt
);
842 ret
= nc_sendv_compat(nc
, iov
, iovcnt
, flags
);
845 if (owned_reentrancy_guard
) {
846 owned_reentrancy_guard
->engaged_in_io
= false;
850 nc
->receive_disabled
= 1;
856 ssize_t
qemu_sendv_packet_async(NetClientState
*sender
,
857 const struct iovec
*iov
, int iovcnt
,
858 NetPacketSent
*sent_cb
)
861 size_t size
= iov_size(iov
, iovcnt
);
864 if (size
> NET_BUFSIZE
) {
868 if (sender
->link_down
|| !sender
->peer
) {
872 /* Let filters handle the packet first */
873 ret
= filter_receive_iov(sender
, NET_FILTER_DIRECTION_TX
, sender
,
874 QEMU_NET_PACKET_FLAG_NONE
, iov
, iovcnt
, sent_cb
);
879 ret
= filter_receive_iov(sender
->peer
, NET_FILTER_DIRECTION_RX
, sender
,
880 QEMU_NET_PACKET_FLAG_NONE
, iov
, iovcnt
, sent_cb
);
885 queue
= sender
->peer
->incoming_queue
;
887 return qemu_net_queue_send_iov(queue
, sender
,
888 QEMU_NET_PACKET_FLAG_NONE
,
889 iov
, iovcnt
, sent_cb
);
893 qemu_sendv_packet(NetClientState
*nc
, const struct iovec
*iov
, int iovcnt
)
895 return qemu_sendv_packet_async(nc
, iov
, iovcnt
, NULL
);
898 NetClientState
*qemu_find_netdev(const char *id
)
902 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
903 if (nc
->info
->type
== NET_CLIENT_DRIVER_NIC
)
905 if (!strcmp(nc
->name
, id
)) {
913 int qemu_find_net_clients_except(const char *id
, NetClientState
**ncs
,
914 NetClientDriver type
, int max
)
919 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
920 if (nc
->info
->type
== type
) {
923 if (!id
|| !strcmp(nc
->name
, id
)) {
934 static int nic_get_free_idx(void)
938 for (index
= 0; index
< MAX_NICS
; index
++)
939 if (!nd_table
[index
].used
)
944 GPtrArray
*qemu_get_nic_models(const char *device_type
)
946 GPtrArray
*nic_models
= g_ptr_array_new();
947 GSList
*list
= object_class_get_list_sorted(device_type
, false);
950 DeviceClass
*dc
= OBJECT_CLASS_CHECK(DeviceClass
, list
->data
,
953 if (test_bit(DEVICE_CATEGORY_NETWORK
, dc
->categories
) &&
954 dc
->user_creatable
) {
955 const char *name
= object_class_get_name(list
->data
);
957 * A network device might also be something else than a NIC, see
958 * e.g. the "rocker" device. Thus we have to look for the "netdev"
959 * property, too. Unfortunately, some devices like virtio-net only
960 * create this property during instance_init, so we have to create
961 * a temporary instance here to be able to check it.
963 Object
*obj
= object_new_with_class(OBJECT_CLASS(dc
));
964 if (object_property_find(obj
, "netdev")) {
965 g_ptr_array_add(nic_models
, (gpointer
)name
);
970 g_slist_free_1(list
);
973 g_ptr_array_add(nic_models
, NULL
);
978 int qemu_show_nic_models(const char *arg
, const char *const *models
)
982 if (!arg
|| !is_help_option(arg
)) {
986 printf("Available NIC models:\n");
987 for (i
= 0 ; models
[i
]; i
++) {
988 printf("%s\n", models
[i
]);
993 void qemu_check_nic_model(NICInfo
*nd
, const char *model
)
995 const char *models
[2];
1000 if (qemu_show_nic_models(nd
->model
, models
))
1002 if (qemu_find_nic_model(nd
, models
, model
) < 0)
1006 int qemu_find_nic_model(NICInfo
*nd
, const char * const *models
,
1007 const char *default_model
)
1012 nd
->model
= g_strdup(default_model
);
1014 for (i
= 0 ; models
[i
]; i
++) {
1015 if (strcmp(nd
->model
, models
[i
]) == 0)
1019 error_report("Unsupported NIC model: %s", nd
->model
);
1023 static int net_init_nic(const Netdev
*netdev
, const char *name
,
1024 NetClientState
*peer
, Error
**errp
)
1028 const NetLegacyNicOptions
*nic
;
1030 assert(netdev
->type
== NET_CLIENT_DRIVER_NIC
);
1031 nic
= &netdev
->u
.nic
;
1033 idx
= nic_get_free_idx();
1034 if (idx
== -1 || nb_nics
>= MAX_NICS
) {
1035 error_setg(errp
, "too many NICs");
1039 nd
= &nd_table
[idx
];
1041 memset(nd
, 0, sizeof(*nd
));
1044 nd
->netdev
= qemu_find_netdev(nic
->netdev
);
1046 error_setg(errp
, "netdev '%s' not found", nic
->netdev
);
1053 nd
->name
= g_strdup(name
);
1055 nd
->model
= g_strdup(nic
->model
);
1058 nd
->devaddr
= g_strdup(nic
->addr
);
1062 net_parse_macaddr(nd
->macaddr
.a
, nic
->macaddr
) < 0) {
1063 error_setg(errp
, "invalid syntax for ethernet address");
1067 is_multicast_ether_addr(nd
->macaddr
.a
)) {
1069 "NIC cannot have multicast MAC address (odd 1st byte)");
1072 qemu_macaddr_default_if_unset(&nd
->macaddr
);
1074 if (nic
->has_vectors
) {
1075 if (nic
->vectors
> 0x7ffffff) {
1076 error_setg(errp
, "invalid # of vectors: %"PRIu32
, nic
->vectors
);
1079 nd
->nvectors
= nic
->vectors
;
1081 nd
->nvectors
= DEV_NVECTORS_UNSPECIFIED
;
1091 static int (* const net_client_init_fun
[NET_CLIENT_DRIVER__MAX
])(
1092 const Netdev
*netdev
,
1094 NetClientState
*peer
, Error
**errp
) = {
1095 [NET_CLIENT_DRIVER_NIC
] = net_init_nic
,
1097 [NET_CLIENT_DRIVER_USER
] = net_init_slirp
,
1099 [NET_CLIENT_DRIVER_TAP
] = net_init_tap
,
1100 [NET_CLIENT_DRIVER_SOCKET
] = net_init_socket
,
1101 [NET_CLIENT_DRIVER_STREAM
] = net_init_stream
,
1102 [NET_CLIENT_DRIVER_DGRAM
] = net_init_dgram
,
1104 [NET_CLIENT_DRIVER_VDE
] = net_init_vde
,
1106 #ifdef CONFIG_NETMAP
1107 [NET_CLIENT_DRIVER_NETMAP
] = net_init_netmap
,
1109 #ifdef CONFIG_AF_XDP
1110 [NET_CLIENT_DRIVER_AF_XDP
] = net_init_af_xdp
,
1112 #ifdef CONFIG_NET_BRIDGE
1113 [NET_CLIENT_DRIVER_BRIDGE
] = net_init_bridge
,
1115 [NET_CLIENT_DRIVER_HUBPORT
] = net_init_hubport
,
1116 #ifdef CONFIG_VHOST_NET_USER
1117 [NET_CLIENT_DRIVER_VHOST_USER
] = net_init_vhost_user
,
1119 #ifdef CONFIG_VHOST_NET_VDPA
1120 [NET_CLIENT_DRIVER_VHOST_VDPA
] = net_init_vhost_vdpa
,
1122 #ifdef CONFIG_L2TPV3
1123 [NET_CLIENT_DRIVER_L2TPV3
] = net_init_l2tpv3
,
1126 [NET_CLIENT_DRIVER_VMNET_HOST
] = net_init_vmnet_host
,
1127 [NET_CLIENT_DRIVER_VMNET_SHARED
] = net_init_vmnet_shared
,
1128 [NET_CLIENT_DRIVER_VMNET_BRIDGED
] = net_init_vmnet_bridged
,
1129 #endif /* CONFIG_VMNET */
1133 static int net_client_init1(const Netdev
*netdev
, bool is_netdev
, Error
**errp
)
1135 NetClientState
*peer
= NULL
;
1139 if (netdev
->type
== NET_CLIENT_DRIVER_NIC
||
1140 !net_client_init_fun
[netdev
->type
]) {
1141 error_setg(errp
, "network backend '%s' is not compiled into this binary",
1142 NetClientDriver_str(netdev
->type
));
1146 if (netdev
->type
== NET_CLIENT_DRIVER_NONE
) {
1147 return 0; /* nothing to do */
1149 if (netdev
->type
== NET_CLIENT_DRIVER_HUBPORT
) {
1150 error_setg(errp
, "network backend '%s' is only supported with -netdev/-nic",
1151 NetClientDriver_str(netdev
->type
));
1155 if (!net_client_init_fun
[netdev
->type
]) {
1156 error_setg(errp
, "network backend '%s' is not compiled into this binary",
1157 NetClientDriver_str(netdev
->type
));
1161 /* Do not add to a hub if it's a nic with a netdev= parameter. */
1162 if (netdev
->type
!= NET_CLIENT_DRIVER_NIC
||
1163 !netdev
->u
.nic
.netdev
) {
1164 peer
= net_hub_add_port(0, NULL
, NULL
);
1168 nc
= qemu_find_netdev(netdev
->id
);
1170 error_setg(errp
, "Duplicate ID '%s'", netdev
->id
);
1174 if (net_client_init_fun
[netdev
->type
](netdev
, netdev
->id
, peer
, errp
) < 0) {
1175 /* FIXME drop when all init functions store an Error */
1176 if (errp
&& !*errp
) {
1177 error_setg(errp
, "Device '%s' could not be initialized",
1178 NetClientDriver_str(netdev
->type
));
1184 nc
= qemu_find_netdev(netdev
->id
);
1186 nc
->is_netdev
= true;
1192 void show_netdevs(void)
1195 const char *available_netdevs
[] = {
1204 #ifdef CONFIG_L2TPV3
1210 #ifdef CONFIG_NET_BRIDGE
1213 #ifdef CONFIG_NETMAP
1216 #ifdef CONFIG_AF_XDP
1222 #ifdef CONFIG_VHOST_VDPA
1232 qemu_printf("Available netdev backend types:\n");
1233 for (idx
= 0; idx
< ARRAY_SIZE(available_netdevs
); idx
++) {
1234 qemu_printf("%s\n", available_netdevs
[idx
]);
1238 static int net_client_init(QemuOpts
*opts
, bool is_netdev
, Error
**errp
)
1240 gchar
**substrings
= NULL
;
1241 Netdev
*object
= NULL
;
1243 Visitor
*v
= opts_visitor_new(opts
);
1245 /* Parse convenience option format ipv6-net=fec0::0[/64] */
1246 const char *ip6_net
= qemu_opt_get(opts
, "ipv6-net");
1250 unsigned long prefix_len
= 64; /* Default 64bit prefix length. */
1252 substrings
= g_strsplit(ip6_net
, "/", 2);
1253 if (!substrings
|| !substrings
[0]) {
1254 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "ipv6-net",
1255 "a valid IPv6 prefix");
1259 prefix_addr
= substrings
[0];
1261 /* Handle user-specified prefix length. */
1262 if (substrings
[1] &&
1263 qemu_strtoul(substrings
[1], NULL
, 10, &prefix_len
))
1266 "parameter 'ipv6-net' expects a number after '/'");
1270 qemu_opt_set(opts
, "ipv6-prefix", prefix_addr
, &error_abort
);
1271 qemu_opt_set_number(opts
, "ipv6-prefixlen", prefix_len
,
1273 qemu_opt_unset(opts
, "ipv6-net");
1276 /* Create an ID for -net if the user did not specify one */
1277 if (!is_netdev
&& !qemu_opts_id(opts
)) {
1278 qemu_opts_set_id(opts
, id_generate(ID_NET
));
1281 if (visit_type_Netdev(v
, NULL
, &object
, errp
)) {
1282 ret
= net_client_init1(object
, is_netdev
, errp
);
1285 qapi_free_Netdev(object
);
1288 g_strfreev(substrings
);
1293 void netdev_add(QemuOpts
*opts
, Error
**errp
)
1295 net_client_init(opts
, true, errp
);
1298 void qmp_netdev_add(Netdev
*netdev
, Error
**errp
)
1300 if (!id_wellformed(netdev
->id
)) {
1301 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "id", "an identifier");
1305 net_client_init1(netdev
, true, errp
);
1308 void qmp_netdev_del(const char *id
, Error
**errp
)
1313 nc
= qemu_find_netdev(id
);
1315 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1316 "Device '%s' not found", id
);
1320 if (!nc
->is_netdev
) {
1321 error_setg(errp
, "Device '%s' is not a netdev", id
);
1325 qemu_del_net_client(nc
);
1328 * Wart: we need to delete the QemuOpts associated with netdevs
1329 * created via CLI or HMP, to avoid bogus "Duplicate ID" errors in
1332 opts
= qemu_opts_find(qemu_find_opts("netdev"), id
);
1334 qemu_opts_del(opts
);
1338 static void netfilter_print_info(Monitor
*mon
, NetFilterState
*nf
)
1341 ObjectProperty
*prop
;
1342 ObjectPropertyIterator iter
;
1345 /* generate info str */
1346 object_property_iter_init(&iter
, OBJECT(nf
));
1347 while ((prop
= object_property_iter_next(&iter
))) {
1348 if (!strcmp(prop
->name
, "type")) {
1351 v
= string_output_visitor_new(false, &str
);
1352 object_property_get(OBJECT(nf
), prop
->name
, v
, NULL
);
1353 visit_complete(v
, &str
);
1355 monitor_printf(mon
, ",%s=%s", prop
->name
, str
);
1358 monitor_printf(mon
, "\n");
1361 void print_net_client(Monitor
*mon
, NetClientState
*nc
)
1365 monitor_printf(mon
, "%s: index=%d,type=%s,%s\n", nc
->name
,
1367 NetClientDriver_str(nc
->info
->type
),
1369 if (!QTAILQ_EMPTY(&nc
->filters
)) {
1370 monitor_printf(mon
, "filters:\n");
1372 QTAILQ_FOREACH(nf
, &nc
->filters
, next
) {
1373 monitor_printf(mon
, " - %s: type=%s",
1374 object_get_canonical_path_component(OBJECT(nf
)),
1375 object_get_typename(OBJECT(nf
)));
1376 netfilter_print_info(mon
, nf
);
1380 RxFilterInfoList
*qmp_query_rx_filter(const char *name
, Error
**errp
)
1383 RxFilterInfoList
*filter_list
= NULL
, **tail
= &filter_list
;
1385 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
1388 if (name
&& strcmp(nc
->name
, name
) != 0) {
1392 /* only query rx-filter information of NIC */
1393 if (nc
->info
->type
!= NET_CLIENT_DRIVER_NIC
) {
1395 error_setg(errp
, "net client(%s) isn't a NIC", name
);
1396 assert(!filter_list
);
1402 /* only query information on queue 0 since the info is per nic,
1405 if (nc
->queue_index
!= 0)
1408 if (nc
->info
->query_rx_filter
) {
1409 info
= nc
->info
->query_rx_filter(nc
);
1410 QAPI_LIST_APPEND(tail
, info
);
1412 error_setg(errp
, "net client(%s) doesn't support"
1413 " rx-filter querying", name
);
1414 assert(!filter_list
);
1423 if (filter_list
== NULL
&& name
) {
1424 error_setg(errp
, "invalid net client name: %s", name
);
1430 void colo_notify_filters_event(int event
, Error
**errp
)
1434 NetFilterClass
*nfc
= NULL
;
1435 Error
*local_err
= NULL
;
1437 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
1438 QTAILQ_FOREACH(nf
, &nc
->filters
, next
) {
1439 nfc
= NETFILTER_GET_CLASS(OBJECT(nf
));
1440 nfc
->handle_event(nf
, event
, &local_err
);
1442 error_propagate(errp
, local_err
);
1449 void qmp_set_link(const char *name
, bool up
, Error
**errp
)
1451 NetClientState
*ncs
[MAX_QUEUE_NUM
];
1455 queues
= qemu_find_net_clients_except(name
, ncs
,
1456 NET_CLIENT_DRIVER__MAX
,
1460 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1461 "Device '%s' not found", name
);
1466 for (i
= 0; i
< queues
; i
++) {
1467 ncs
[i
]->link_down
= !up
;
1470 if (nc
->info
->link_status_changed
) {
1471 nc
->info
->link_status_changed(nc
);
1475 /* Change peer link only if the peer is NIC and then notify peer.
1476 * If the peer is a HUBPORT or a backend, we do not change the
1479 * This behavior is compatible with qemu hubs where there could be
1480 * multiple clients that can still communicate with each other in
1481 * disconnected mode. For now maintain this compatibility.
1483 if (nc
->peer
->info
->type
== NET_CLIENT_DRIVER_NIC
) {
1484 for (i
= 0; i
< queues
; i
++) {
1485 ncs
[i
]->peer
->link_down
= !up
;
1488 if (nc
->peer
->info
->link_status_changed
) {
1489 nc
->peer
->info
->link_status_changed(nc
->peer
);
1494 static void net_vm_change_state_handler(void *opaque
, bool running
,
1498 NetClientState
*tmp
;
1500 QTAILQ_FOREACH_SAFE(nc
, &net_clients
, next
, tmp
) {
1502 /* Flush queued packets and wake up backends. */
1503 if (nc
->peer
&& qemu_can_send_packet(nc
)) {
1504 qemu_flush_queued_packets(nc
->peer
);
1507 /* Complete all queued packets, to guarantee we don't modify
1508 * state later when VM is not running.
1510 qemu_flush_or_purge_queued_packets(nc
, true);
1515 void net_cleanup(void)
1517 NetClientState
*nc
, **p
= &QTAILQ_FIRST(&net_clients
);
1519 /*cleanup colo compare module for COLO*/
1520 colo_compare_cleanup();
1523 * Walk the net_clients list and remove the netdevs but *not* any
1524 * NET_CLIENT_DRIVER_NIC entries. The latter are owned by the device
1525 * model which created them, and in some cases (e.g. xen-net-device)
1526 * the device itself may do cleanup at exit and will be upset if we
1527 * just delete its NIC from underneath it.
1529 * Since qemu_del_net_client() may delete multiple entries, using
1530 * QTAILQ_FOREACH_SAFE() is not safe here. The only safe pointer
1531 * to keep as a bookmark is a NET_CLIENT_DRIVER_NIC entry, so keep
1532 * 'p' pointing to either the head of the list, or the 'next' field
1533 * of the latest NET_CLIENT_DRIVER_NIC, and operate on *p as we walk
1536 * The 'nc' variable isn't part of the list traversal; it's purely
1537 * for convenience as too much '(*p)->' has a tendency to make the
1538 * readers' eyes bleed.
1542 if (nc
->info
->type
== NET_CLIENT_DRIVER_NIC
) {
1543 /* Skip NET_CLIENT_DRIVER_NIC entries */
1544 p
= &QTAILQ_NEXT(nc
, next
);
1546 qemu_del_net_client(nc
);
1550 qemu_del_vm_change_state_handler(net_change_state_entry
);
1553 void net_check_clients(void)
1558 net_hub_check_clients();
1560 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
1562 warn_report("%s %s has no peer",
1563 nc
->info
->type
== NET_CLIENT_DRIVER_NIC
1569 /* Check that all NICs requested via -net nic actually got created.
1570 * NICs created via -device don't need to be checked here because
1571 * they are always instantiated.
1573 for (i
= 0; i
< MAX_NICS
; i
++) {
1574 NICInfo
*nd
= &nd_table
[i
];
1575 if (nd
->used
&& !nd
->instantiated
) {
1576 warn_report("requested NIC (%s, model %s) "
1577 "was not created (not supported by this machine?)",
1578 nd
->name
? nd
->name
: "anonymous",
1579 nd
->model
? nd
->model
: "unspecified");
1584 static int net_init_client(void *dummy
, QemuOpts
*opts
, Error
**errp
)
1586 return net_client_init(opts
, false, errp
);
1589 static int net_init_netdev(void *dummy
, QemuOpts
*opts
, Error
**errp
)
1591 const char *type
= qemu_opt_get(opts
, "type");
1593 if (type
&& is_help_option(type
)) {
1597 return net_client_init(opts
, true, errp
);
1600 /* For the convenience "--nic" parameter */
1601 static int net_param_nic(void *dummy
, QemuOpts
*opts
, Error
**errp
)
1608 type
= qemu_opt_get(opts
, "type");
1610 if (g_str_equal(type
, "none")) {
1611 return 0; /* Nothing to do, default_net is cleared in vl.c */
1613 if (is_help_option(type
)) {
1614 GPtrArray
*nic_models
= qemu_get_nic_models(TYPE_DEVICE
);
1617 qemu_show_nic_models(type
, (const char **)nic_models
->pdata
);
1618 g_ptr_array_free(nic_models
, true);
1623 idx
= nic_get_free_idx();
1624 if (idx
== -1 || nb_nics
>= MAX_NICS
) {
1625 error_setg(errp
, "no more on-board/default NIC slots available");
1630 qemu_opt_set(opts
, "type", "user", &error_abort
);
1633 ni
= &nd_table
[idx
];
1634 memset(ni
, 0, sizeof(*ni
));
1635 ni
->model
= qemu_opt_get_del(opts
, "model");
1637 /* Create an ID if the user did not specify one */
1638 nd_id
= g_strdup(qemu_opts_id(opts
));
1640 nd_id
= id_generate(ID_NET
);
1641 qemu_opts_set_id(opts
, nd_id
);
1644 /* Handle MAC address */
1645 mac
= qemu_opt_get_del(opts
, "mac");
1647 ret
= net_parse_macaddr(ni
->macaddr
.a
, mac
);
1650 error_setg(errp
, "invalid syntax for ethernet address");
1653 if (is_multicast_ether_addr(ni
->macaddr
.a
)) {
1654 error_setg(errp
, "NIC cannot have multicast MAC address");
1659 qemu_macaddr_default_if_unset(&ni
->macaddr
);
1661 ret
= net_client_init(opts
, true, errp
);
1663 ni
->netdev
= qemu_find_netdev(nd_id
);
1673 static void netdev_init_modern(void)
1675 while (!QSIMPLEQ_EMPTY(&nd_queue
)) {
1676 NetdevQueueEntry
*nd
= QSIMPLEQ_FIRST(&nd_queue
);
1678 QSIMPLEQ_REMOVE_HEAD(&nd_queue
, entry
);
1679 loc_push_restore(&nd
->loc
);
1680 net_client_init1(nd
->nd
, true, &error_fatal
);
1682 qapi_free_Netdev(nd
->nd
);
1687 void net_init_clients(void)
1689 net_change_state_entry
=
1690 qemu_add_vm_change_state_handler(net_vm_change_state_handler
, NULL
);
1692 QTAILQ_INIT(&net_clients
);
1694 netdev_init_modern();
1696 qemu_opts_foreach(qemu_find_opts("netdev"), net_init_netdev
, NULL
,
1699 qemu_opts_foreach(qemu_find_opts("nic"), net_param_nic
, NULL
,
1702 qemu_opts_foreach(qemu_find_opts("net"), net_init_client
, NULL
,
1707 * Does this -netdev argument use modern rather than traditional syntax?
1708 * Modern syntax is to be parsed with netdev_parse_modern().
1709 * Traditional syntax is to be parsed with net_client_parse().
1711 bool netdev_is_modern(const char *optstr
)
1716 static QemuOptsList dummy_opts
= {
1718 .implied_opt_name
= "type",
1719 .head
= QTAILQ_HEAD_INITIALIZER(dummy_opts
.head
),
1723 if (optstr
[0] == '{') {
1724 /* This is JSON, which means it's modern syntax */
1728 opts
= qemu_opts_create(&dummy_opts
, NULL
, false, &error_abort
);
1729 qemu_opts_do_parse(opts
, optstr
, dummy_opts
.implied_opt_name
,
1731 type
= qemu_opt_get(opts
, "type");
1732 is_modern
= !g_strcmp0(type
, "stream") || !g_strcmp0(type
, "dgram");
1734 qemu_opts_reset(&dummy_opts
);
1740 * netdev_parse_modern() uses modern, more expressive syntax than
1741 * net_client_parse(), but supports only the -netdev option.
1742 * netdev_parse_modern() appends to @nd_queue, whereas net_client_parse()
1743 * appends to @qemu_netdev_opts.
1745 void netdev_parse_modern(const char *optstr
)
1748 NetdevQueueEntry
*nd
;
1750 v
= qobject_input_visitor_new_str(optstr
, "type", &error_fatal
);
1751 nd
= g_new(NetdevQueueEntry
, 1);
1752 visit_type_Netdev(v
, NULL
, &nd
->nd
, &error_fatal
);
1756 QSIMPLEQ_INSERT_TAIL(&nd_queue
, nd
, entry
);
1759 void net_client_parse(QemuOptsList
*opts_list
, const char *optstr
)
1761 if (!qemu_opts_parse_noisily(opts_list
, optstr
, true)) {
1768 uint32_t net_crc32(const uint8_t *p
, int len
)
1775 for (i
= 0; i
< len
; i
++) {
1777 for (j
= 0; j
< 8; j
++) {
1778 carry
= ((crc
& 0x80000000L
) ? 1 : 0) ^ (b
& 0x01);
1782 crc
= ((crc
^ POLYNOMIAL_BE
) | carry
);
1790 uint32_t net_crc32_le(const uint8_t *p
, int len
)
1797 for (i
= 0; i
< len
; i
++) {
1799 for (j
= 0; j
< 8; j
++) {
1800 carry
= (crc
& 0x1) ^ (b
& 0x01);
1804 crc
^= POLYNOMIAL_LE
;
1812 QemuOptsList qemu_netdev_opts
= {
1814 .implied_opt_name
= "type",
1815 .head
= QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts
.head
),
1818 * no elements => accept any params
1819 * validation will happen later
1821 { /* end of list */ }
1825 QemuOptsList qemu_nic_opts
= {
1827 .implied_opt_name
= "type",
1828 .head
= QTAILQ_HEAD_INITIALIZER(qemu_nic_opts
.head
),
1831 * no elements => accept any params
1832 * validation will happen later
1834 { /* end of list */ }
1838 QemuOptsList qemu_net_opts
= {
1840 .implied_opt_name
= "type",
1841 .head
= QTAILQ_HEAD_INITIALIZER(qemu_net_opts
.head
),
1844 * no elements => accept any params
1845 * validation will happen later
1847 { /* end of list */ }
1851 void net_socket_rs_init(SocketReadState
*rs
,
1852 SocketReadStateFinalize
*finalize
,
1856 rs
->vnet_hdr
= vnet_hdr
;
1859 rs
->vnet_hdr_len
= 0;
1860 memset(rs
->buf
, 0, sizeof(rs
->buf
));
1861 rs
->finalize
= finalize
;
1869 int net_fill_rstate(SocketReadState
*rs
, const uint8_t *buf
, int size
)
1874 /* Reassemble a packet from the network.
1875 * 0 = getting length.
1876 * 1 = getting vnet header length.
1879 switch (rs
->state
) {
1885 memcpy(rs
->buf
+ rs
->index
, buf
, l
);
1889 if (rs
->index
== 4) {
1891 rs
->packet_len
= ntohl(*(uint32_t *)rs
->buf
);
1897 rs
->vnet_hdr_len
= 0;
1906 memcpy(rs
->buf
+ rs
->index
, buf
, l
);
1910 if (rs
->index
== 4) {
1911 /* got vnet header length */
1912 rs
->vnet_hdr_len
= ntohl(*(uint32_t *)rs
->buf
);
1918 l
= rs
->packet_len
- rs
->index
;
1922 if (rs
->index
+ l
<= sizeof(rs
->buf
)) {
1923 memcpy(rs
->buf
+ rs
->index
, buf
, l
);
1925 fprintf(stderr
, "serious error: oversized packet received,"
1926 "connection terminated.\n");
1927 rs
->index
= rs
->state
= 0;
1934 if (rs
->index
>= rs
->packet_len
) {
1937 assert(rs
->finalize
);