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"
59 #include "standard-headers/linux/virtio_net.h"
61 /* Net bridge is currently not supported for W32. */
63 # define CONFIG_NET_BRIDGE
66 static VMChangeStateEntry
*net_change_state_entry
;
67 NetClientStateList net_clients
;
69 typedef struct NetdevQueueEntry
{
72 QSIMPLEQ_ENTRY(NetdevQueueEntry
) entry
;
75 typedef QSIMPLEQ_HEAD(, NetdevQueueEntry
) NetdevQueue
;
77 static NetdevQueue nd_queue
= QSIMPLEQ_HEAD_INITIALIZER(nd_queue
);
79 static GHashTable
*nic_model_help
;
82 static NICInfo nd_table
[MAX_NICS
];
84 /***********************************************************/
85 /* network device redirectors */
87 int convert_host_port(struct sockaddr_in
*saddr
, const char *host
,
88 const char *port
, Error
**errp
)
94 memset(saddr
, 0, sizeof(*saddr
));
96 saddr
->sin_family
= AF_INET
;
97 if (host
[0] == '\0') {
98 saddr
->sin_addr
.s_addr
= 0;
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
);
107 he
= gethostbyname(host
);
109 error_setg(errp
, "can't resolve host address '%s'", host
);
112 saddr
->sin_addr
= *(struct in_addr
*)he
->h_addr
;
115 if (qemu_strtol(port
, &r
, 0, &p
) != 0) {
116 error_setg(errp
, "port number '%s' is invalid", port
);
119 saddr
->sin_port
= htons(p
);
123 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
,
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
);
137 ret
= convert_host_port(saddr
, substrings
[0], substrings
[1], errp
);
140 g_strfreev(substrings
);
144 char *qemu_mac_strdup_printf(const uint8_t *macaddr
)
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]);
151 void qemu_set_info_str(NetClientState
*nc
, const char *fmt
, ...)
156 vsnprintf(nc
->info_str
, sizeof(nc
->info_str
), fmt
, ap
);
160 void qemu_format_nic_info_str(NetClientState
*nc
, uint8_t macaddr
[6])
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]);
167 static int mac_table
[256] = {0};
169 static void qemu_macaddr_set_used(MACAddr
*macaddr
)
173 for (index
= 0x56; index
< 0xFF; index
++) {
174 if (macaddr
->a
[5] == index
) {
180 static void qemu_macaddr_set_free(MACAddr
*macaddr
)
183 static const MACAddr base
= { .a
= { 0x52, 0x54, 0x00, 0x12, 0x34, 0 } };
185 if (memcmp(macaddr
->a
, &base
.a
, (sizeof(base
.a
) - 1)) != 0) {
188 for (index
= 0x56; index
< 0xFF; index
++) {
189 if (macaddr
->a
[5] == index
) {
195 static int qemu_macaddr_get_free(void)
199 for (index
= 0x56; index
< 0xFF; index
++) {
200 if (mac_table
[index
] == 0) {
208 void qemu_macaddr_default_if_unset(MACAddr
*macaddr
)
210 static const MACAddr zero
= { .a
= { 0,0,0,0,0,0 } };
211 static const MACAddr base
= { .a
= { 0x52, 0x54, 0x00, 0x12, 0x34, 0 } };
213 if (memcmp(macaddr
, &zero
, sizeof(zero
)) != 0) {
214 if (memcmp(macaddr
->a
, &base
.a
, (sizeof(base
.a
) - 1)) != 0) {
217 qemu_macaddr_set_used(macaddr
);
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
);
232 * Generate a name for net client
234 * Only net clients created with the legacy -net option and NICs need this.
236 static char *assign_name(NetClientState
*nc1
, const char *model
)
241 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
245 if (strcmp(nc
->model
, model
) == 0) {
250 return g_strdup_printf("%s.%d", model
, id
);
253 static void qemu_net_client_destructor(NetClientState
*nc
)
257 static ssize_t
qemu_deliver_packet_iov(NetClientState
*sender
,
259 const struct iovec
*iov
,
263 static void qemu_net_client_setup(NetClientState
*nc
,
265 NetClientState
*peer
,
268 NetClientDestructor
*destructor
,
272 nc
->model
= g_strdup(model
);
274 nc
->name
= g_strdup(name
);
276 nc
->name
= assign_name(nc
, model
);
284 QTAILQ_INSERT_TAIL(&net_clients
, nc
, next
);
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
);
292 NetClientState
*qemu_new_net_client(NetClientInfo
*info
,
293 NetClientState
*peer
,
299 assert(info
->size
>= sizeof(NetClientState
));
301 nc
= g_malloc0(info
->size
);
302 qemu_net_client_setup(nc
, info
, peer
, model
, name
,
303 qemu_net_client_destructor
, true);
308 NetClientState
*qemu_new_net_control_client(NetClientInfo
*info
,
309 NetClientState
*peer
,
315 assert(info
->size
>= sizeof(NetClientState
));
317 nc
= g_malloc0(info
->size
);
318 qemu_net_client_setup(nc
, info
, peer
, model
, name
,
319 qemu_net_client_destructor
, false);
324 NICState
*qemu_new_nic(NetClientInfo
*info
,
328 MemReentrancyGuard
*reentrancy_guard
,
331 NetClientState
**peers
= conf
->peers
.ncs
;
333 int i
, queues
= MAX(1, conf
->peers
.queues
);
335 assert(info
->type
== NET_CLIENT_DRIVER_NIC
);
336 assert(info
->size
>= sizeof(NICState
));
338 nic
= g_malloc0(info
->size
+ sizeof(NetClientState
) * queues
);
339 nic
->ncs
= (void *)nic
+ info
->size
;
341 nic
->reentrancy_guard
= reentrancy_guard
,
342 nic
->opaque
= opaque
;
344 for (i
= 0; i
< queues
; i
++) {
345 qemu_net_client_setup(&nic
->ncs
[i
], info
, peers
[i
], model
, name
,
347 nic
->ncs
[i
].queue_index
= i
;
353 NetClientState
*qemu_get_subqueue(NICState
*nic
, int queue_index
)
355 return nic
->ncs
+ queue_index
;
358 NetClientState
*qemu_get_queue(NICState
*nic
)
360 return qemu_get_subqueue(nic
, 0);
363 NICState
*qemu_get_nic(NetClientState
*nc
)
365 NetClientState
*nc0
= nc
- nc
->queue_index
;
367 return (NICState
*)((void *)nc0
- nc
->info
->size
);
370 void *qemu_get_nic_opaque(NetClientState
*nc
)
372 NICState
*nic
= qemu_get_nic(nc
);
377 NetClientState
*qemu_get_peer(NetClientState
*nc
, int queue_index
)
380 NetClientState
*ncs
= nc
+ queue_index
;
384 static void qemu_cleanup_net_client(NetClientState
*nc
)
386 QTAILQ_REMOVE(&net_clients
, nc
, next
);
388 if (nc
->info
->cleanup
) {
389 nc
->info
->cleanup(nc
);
393 static void qemu_free_net_client(NetClientState
*nc
)
395 if (nc
->incoming_queue
) {
396 qemu_del_net_queue(nc
->incoming_queue
);
399 nc
->peer
->peer
= NULL
;
403 if (nc
->destructor
) {
408 void qemu_del_net_client(NetClientState
*nc
)
410 NetClientState
*ncs
[MAX_QUEUE_NUM
];
412 NetFilterState
*nf
, *next
;
414 assert(nc
->info
->type
!= NET_CLIENT_DRIVER_NIC
);
416 /* If the NetClientState belongs to a multiqueue backend, we will change all
417 * other NetClientStates also.
419 queues
= qemu_find_net_clients_except(nc
->name
, ncs
,
420 NET_CLIENT_DRIVER_NIC
,
424 QTAILQ_FOREACH_SAFE(nf
, &nc
->filters
, next
, next
) {
425 object_unparent(OBJECT(nf
));
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
) {
434 nic
->peer_deleted
= true;
436 for (i
= 0; i
< queues
; i
++) {
437 ncs
[i
]->peer
->link_down
= true;
440 if (nc
->peer
->info
->link_status_changed
) {
441 nc
->peer
->info
->link_status_changed(nc
->peer
);
444 for (i
= 0; i
< queues
; i
++) {
445 qemu_cleanup_net_client(ncs
[i
]);
451 for (i
= 0; i
< queues
; i
++) {
452 qemu_cleanup_net_client(ncs
[i
]);
453 qemu_free_net_client(ncs
[i
]);
457 void qemu_del_nic(NICState
*nic
)
459 int i
, queues
= MAX(nic
->conf
->peers
.queues
, 1);
461 qemu_macaddr_set_free(&nic
->conf
->macaddr
);
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
);
474 for (i
= queues
- 1; i
>= 0; i
--) {
475 NetClientState
*nc
= qemu_get_subqueue(nic
, i
);
477 qemu_cleanup_net_client(nc
);
478 qemu_free_net_client(nc
);
484 void qemu_foreach_nic(qemu_nic_foreach func
, void *opaque
)
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
);
497 bool qemu_has_ufo(NetClientState
*nc
)
499 if (!nc
|| !nc
->info
->has_ufo
) {
503 return nc
->info
->has_ufo(nc
);
506 bool qemu_has_uso(NetClientState
*nc
)
508 if (!nc
|| !nc
->info
->has_uso
) {
512 return nc
->info
->has_uso(nc
);
515 bool qemu_has_vnet_hdr(NetClientState
*nc
)
517 if (!nc
|| !nc
->info
->has_vnet_hdr
) {
521 return nc
->info
->has_vnet_hdr(nc
);
524 bool qemu_has_vnet_hdr_len(NetClientState
*nc
, int len
)
526 if (!nc
|| !nc
->info
->has_vnet_hdr_len
) {
530 return nc
->info
->has_vnet_hdr_len(nc
, len
);
533 void qemu_set_offload(NetClientState
*nc
, int csum
, int tso4
, int tso6
,
534 int ecn
, int ufo
, int uso4
, int uso6
)
536 if (!nc
|| !nc
->info
->set_offload
) {
540 nc
->info
->set_offload(nc
, csum
, tso4
, tso6
, ecn
, ufo
, uso4
, uso6
);
543 int qemu_get_vnet_hdr_len(NetClientState
*nc
)
545 return nc
->vnet_hdr_len
;
548 void qemu_set_vnet_hdr_len(NetClientState
*nc
, int len
)
550 if (!nc
|| !nc
->info
->set_vnet_hdr_len
) {
554 assert(len
== sizeof(struct virtio_net_hdr_mrg_rxbuf
) ||
555 len
== sizeof(struct virtio_net_hdr
) ||
556 len
== sizeof(struct virtio_net_hdr_v1_hash
));
558 nc
->vnet_hdr_len
= len
;
559 nc
->info
->set_vnet_hdr_len(nc
, len
);
562 int qemu_set_vnet_le(NetClientState
*nc
, bool is_le
)
565 if (!nc
|| !nc
->info
->set_vnet_le
) {
569 return nc
->info
->set_vnet_le(nc
, is_le
);
575 int qemu_set_vnet_be(NetClientState
*nc
, bool is_be
)
580 if (!nc
|| !nc
->info
->set_vnet_be
) {
584 return nc
->info
->set_vnet_be(nc
, is_be
);
588 int qemu_can_receive_packet(NetClientState
*nc
)
590 if (nc
->receive_disabled
) {
592 } else if (nc
->info
->can_receive
&&
593 !nc
->info
->can_receive(nc
)) {
599 int qemu_can_send_packet(NetClientState
*sender
)
601 int vm_running
= runstate_is_running();
611 return qemu_can_receive_packet(sender
->peer
);
614 static ssize_t
filter_receive_iov(NetClientState
*nc
,
615 NetFilterDirection direction
,
616 NetClientState
*sender
,
618 const struct iovec
*iov
,
620 NetPacketSent
*sent_cb
)
623 NetFilterState
*nf
= NULL
;
625 if (direction
== NET_FILTER_DIRECTION_TX
) {
626 QTAILQ_FOREACH(nf
, &nc
->filters
, next
) {
627 ret
= qemu_netfilter_receive(nf
, direction
, sender
, flags
, iov
,
634 QTAILQ_FOREACH_REVERSE(nf
, &nc
->filters
, next
) {
635 ret
= qemu_netfilter_receive(nf
, direction
, sender
, flags
, iov
,
646 static ssize_t
filter_receive(NetClientState
*nc
,
647 NetFilterDirection direction
,
648 NetClientState
*sender
,
652 NetPacketSent
*sent_cb
)
655 .iov_base
= (void *)data
,
659 return filter_receive_iov(nc
, direction
, sender
, flags
, &iov
, 1, sent_cb
);
662 void qemu_purge_queued_packets(NetClientState
*nc
)
668 qemu_net_queue_purge(nc
->peer
->incoming_queue
, nc
);
671 void qemu_flush_or_purge_queued_packets(NetClientState
*nc
, bool purge
)
673 nc
->receive_disabled
= 0;
675 if (nc
->peer
&& nc
->peer
->info
->type
== NET_CLIENT_DRIVER_HUBPORT
) {
676 if (net_hub_flush(nc
->peer
)) {
680 if (qemu_net_queue_flush(nc
->incoming_queue
)) {
681 /* We emptied the queue successfully, signal to the IO thread to repoll
682 * the file descriptor (for tap, for example).
686 /* Unable to empty the queue, purge remaining packets */
687 qemu_net_queue_purge(nc
->incoming_queue
, nc
->peer
);
691 void qemu_flush_queued_packets(NetClientState
*nc
)
693 qemu_flush_or_purge_queued_packets(nc
, false);
696 static ssize_t
qemu_send_packet_async_with_flags(NetClientState
*sender
,
698 const uint8_t *buf
, int size
,
699 NetPacketSent
*sent_cb
)
705 printf("qemu_send_packet_async:\n");
706 qemu_hexdump(stdout
, "net", buf
, size
);
709 if (sender
->link_down
|| !sender
->peer
) {
713 /* Let filters handle the packet first */
714 ret
= filter_receive(sender
, NET_FILTER_DIRECTION_TX
,
715 sender
, flags
, buf
, size
, sent_cb
);
720 ret
= filter_receive(sender
->peer
, NET_FILTER_DIRECTION_RX
,
721 sender
, flags
, buf
, size
, sent_cb
);
726 queue
= sender
->peer
->incoming_queue
;
728 return qemu_net_queue_send(queue
, sender
, flags
, buf
, size
, sent_cb
);
731 ssize_t
qemu_send_packet_async(NetClientState
*sender
,
732 const uint8_t *buf
, int size
,
733 NetPacketSent
*sent_cb
)
735 return qemu_send_packet_async_with_flags(sender
, QEMU_NET_PACKET_FLAG_NONE
,
739 ssize_t
qemu_send_packet(NetClientState
*nc
, const uint8_t *buf
, int size
)
741 return qemu_send_packet_async(nc
, buf
, size
, NULL
);
744 ssize_t
qemu_receive_packet(NetClientState
*nc
, const uint8_t *buf
, int size
)
746 if (!qemu_can_receive_packet(nc
)) {
750 return qemu_net_queue_receive(nc
->incoming_queue
, buf
, size
);
753 ssize_t
qemu_send_packet_raw(NetClientState
*nc
, const uint8_t *buf
, int size
)
755 return qemu_send_packet_async_with_flags(nc
, QEMU_NET_PACKET_FLAG_RAW
,
759 static ssize_t
nc_sendv_compat(NetClientState
*nc
, const struct iovec
*iov
,
760 int iovcnt
, unsigned flags
)
768 buffer
= iov
[0].iov_base
;
769 offset
= iov
[0].iov_len
;
771 offset
= iov_size(iov
, iovcnt
);
772 if (offset
> NET_BUFSIZE
) {
775 buf
= g_malloc(offset
);
777 offset
= iov_to_buf(iov
, iovcnt
, 0, buf
, offset
);
780 ret
= nc
->info
->receive(nc
, buffer
, offset
);
786 static ssize_t
qemu_deliver_packet_iov(NetClientState
*sender
,
788 const struct iovec
*iov
,
792 MemReentrancyGuard
*owned_reentrancy_guard
;
793 NetClientState
*nc
= opaque
;
795 struct virtio_net_hdr_v1_hash vnet_hdr
= { };
796 g_autofree
struct iovec
*iov_copy
= NULL
;
800 return iov_size(iov
, iovcnt
);
803 if (nc
->receive_disabled
) {
807 if (nc
->info
->type
!= NET_CLIENT_DRIVER_NIC
||
808 qemu_get_nic(nc
)->reentrancy_guard
->engaged_in_io
) {
809 owned_reentrancy_guard
= NULL
;
811 owned_reentrancy_guard
= qemu_get_nic(nc
)->reentrancy_guard
;
812 owned_reentrancy_guard
->engaged_in_io
= true;
815 if ((flags
& QEMU_NET_PACKET_FLAG_RAW
) && nc
->vnet_hdr_len
) {
816 iov_copy
= g_new(struct iovec
, iovcnt
+ 1);
817 iov_copy
[0].iov_base
= &vnet_hdr
;
818 iov_copy
[0].iov_len
= nc
->vnet_hdr_len
;
819 memcpy(&iov_copy
[1], iov
, iovcnt
* sizeof(*iov
));
823 if (nc
->info
->receive_iov
) {
824 ret
= nc
->info
->receive_iov(nc
, iov
, iovcnt
);
826 ret
= nc_sendv_compat(nc
, iov
, iovcnt
, flags
);
829 if (owned_reentrancy_guard
) {
830 owned_reentrancy_guard
->engaged_in_io
= false;
834 nc
->receive_disabled
= 1;
840 ssize_t
qemu_sendv_packet_async(NetClientState
*sender
,
841 const struct iovec
*iov
, int iovcnt
,
842 NetPacketSent
*sent_cb
)
845 size_t size
= iov_size(iov
, iovcnt
);
848 if (size
> NET_BUFSIZE
) {
852 if (sender
->link_down
|| !sender
->peer
) {
856 /* Let filters handle the packet first */
857 ret
= filter_receive_iov(sender
, NET_FILTER_DIRECTION_TX
, sender
,
858 QEMU_NET_PACKET_FLAG_NONE
, iov
, iovcnt
, sent_cb
);
863 ret
= filter_receive_iov(sender
->peer
, NET_FILTER_DIRECTION_RX
, sender
,
864 QEMU_NET_PACKET_FLAG_NONE
, iov
, iovcnt
, sent_cb
);
869 queue
= sender
->peer
->incoming_queue
;
871 return qemu_net_queue_send_iov(queue
, sender
,
872 QEMU_NET_PACKET_FLAG_NONE
,
873 iov
, iovcnt
, sent_cb
);
877 qemu_sendv_packet(NetClientState
*nc
, const struct iovec
*iov
, int iovcnt
)
879 return qemu_sendv_packet_async(nc
, iov
, iovcnt
, NULL
);
882 NetClientState
*qemu_find_netdev(const char *id
)
886 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
887 if (nc
->info
->type
== NET_CLIENT_DRIVER_NIC
)
889 if (!strcmp(nc
->name
, id
)) {
897 int qemu_find_net_clients_except(const char *id
, NetClientState
**ncs
,
898 NetClientDriver type
, int max
)
903 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
904 if (nc
->info
->type
== type
) {
907 if (!id
|| !strcmp(nc
->name
, id
)) {
918 static int nic_get_free_idx(void)
922 for (index
= 0; index
< MAX_NICS
; index
++)
923 if (!nd_table
[index
].used
)
928 GPtrArray
*qemu_get_nic_models(const char *device_type
)
930 GPtrArray
*nic_models
= g_ptr_array_new();
931 GSList
*list
= object_class_get_list_sorted(device_type
, false);
934 DeviceClass
*dc
= OBJECT_CLASS_CHECK(DeviceClass
, list
->data
,
937 if (test_bit(DEVICE_CATEGORY_NETWORK
, dc
->categories
) &&
938 dc
->user_creatable
) {
939 const char *name
= object_class_get_name(list
->data
);
941 * A network device might also be something else than a NIC, see
942 * e.g. the "rocker" device. Thus we have to look for the "netdev"
943 * property, too. Unfortunately, some devices like virtio-net only
944 * create this property during instance_init, so we have to create
945 * a temporary instance here to be able to check it.
947 Object
*obj
= object_new_with_class(OBJECT_CLASS(dc
));
948 if (object_property_find(obj
, "netdev")) {
949 g_ptr_array_add(nic_models
, (gpointer
)name
);
954 g_slist_free_1(list
);
957 g_ptr_array_add(nic_models
, NULL
);
962 static int net_init_nic(const Netdev
*netdev
, const char *name
,
963 NetClientState
*peer
, Error
**errp
)
967 const NetLegacyNicOptions
*nic
;
969 assert(netdev
->type
== NET_CLIENT_DRIVER_NIC
);
970 nic
= &netdev
->u
.nic
;
972 idx
= nic_get_free_idx();
973 if (idx
== -1 || nb_nics
>= MAX_NICS
) {
974 error_setg(errp
, "too many NICs");
980 memset(nd
, 0, sizeof(*nd
));
983 nd
->netdev
= qemu_find_netdev(nic
->netdev
);
985 error_setg(errp
, "netdev '%s' not found", nic
->netdev
);
992 nd
->name
= g_strdup(name
);
994 nd
->model
= g_strdup(nic
->model
);
997 nd
->devaddr
= g_strdup(nic
->addr
);
1001 net_parse_macaddr(nd
->macaddr
.a
, nic
->macaddr
) < 0) {
1002 error_setg(errp
, "invalid syntax for ethernet address");
1006 is_multicast_ether_addr(nd
->macaddr
.a
)) {
1008 "NIC cannot have multicast MAC address (odd 1st byte)");
1011 qemu_macaddr_default_if_unset(&nd
->macaddr
);
1013 if (nic
->has_vectors
) {
1014 if (nic
->vectors
> 0x7ffffff) {
1015 error_setg(errp
, "invalid # of vectors: %"PRIu32
, nic
->vectors
);
1018 nd
->nvectors
= nic
->vectors
;
1020 nd
->nvectors
= DEV_NVECTORS_UNSPECIFIED
;
1029 static gboolean
add_nic_result(gpointer key
, gpointer value
, gpointer user_data
)
1031 GPtrArray
*results
= user_data
;
1032 GPtrArray
*alias_list
= value
;
1033 const char *model
= key
;
1037 result
= g_strdup(model
);
1039 GString
*result_str
= g_string_new(model
);
1042 g_string_append(result_str
, " (aka ");
1043 for (i
= 0; i
< alias_list
->len
; i
++) {
1045 g_string_append(result_str
, ", ");
1047 g_string_append(result_str
, alias_list
->pdata
[i
]);
1049 g_string_append(result_str
, ")");
1050 result
= result_str
->str
;
1051 g_string_free(result_str
, false);
1052 g_ptr_array_unref(alias_list
);
1054 g_ptr_array_add(results
, result
);
1058 static int model_cmp(char **a
, char **b
)
1060 return strcmp(*a
, *b
);
1063 static void show_nic_models(void)
1065 GPtrArray
*results
= g_ptr_array_new();
1068 g_hash_table_foreach_remove(nic_model_help
, add_nic_result
, results
);
1069 g_ptr_array_sort(results
, (GCompareFunc
)model_cmp
);
1071 printf("Available NIC models for this configuration:\n");
1072 for (i
= 0 ; i
< results
->len
; i
++) {
1073 printf("%s\n", (char *)results
->pdata
[i
]);
1075 g_hash_table_unref(nic_model_help
);
1076 nic_model_help
= NULL
;
1079 static void add_nic_model_help(const char *model
, const char *alias
)
1081 GPtrArray
*alias_list
= NULL
;
1083 if (g_hash_table_lookup_extended(nic_model_help
, model
, NULL
,
1084 (gpointer
*)&alias_list
)) {
1085 /* Already exists, no alias to add: return */
1090 /* Check if this alias is already in the list. Add if not. */
1091 if (!g_ptr_array_find_with_equal_func(alias_list
, alias
,
1092 g_str_equal
, NULL
)) {
1093 g_ptr_array_add(alias_list
, g_strdup(alias
));
1098 /* Either this model wasn't in the list already, or a first alias added */
1100 alias_list
= g_ptr_array_new();
1101 g_ptr_array_set_free_func(alias_list
, g_free
);
1102 g_ptr_array_add(alias_list
, g_strdup(alias
));
1104 g_hash_table_replace(nic_model_help
, g_strdup(model
), alias_list
);
1107 NICInfo
*qemu_find_nic_info(const char *typename
, bool match_default
,
1113 if (nic_model_help
) {
1114 add_nic_model_help(typename
, alias
);
1117 for (i
= 0; i
< nb_nics
; i
++) {
1120 if (!nd
->used
|| nd
->instantiated
) {
1124 if ((match_default
&& !nd
->model
) || !g_strcmp0(nd
->model
, typename
)
1125 || (alias
&& !g_strcmp0(nd
->model
, alias
))) {
1132 static bool is_nic_model_help_option(const char *model
)
1134 if (model
&& is_help_option(model
)) {
1136 * Trigger the help output by instantiating the hash table which
1137 * will gather tha available models as they get registered.
1139 if (!nic_model_help
) {
1140 nic_model_help
= g_hash_table_new_full(g_str_hash
, g_str_equal
,
1148 /* "I have created a device. Please configure it if you can" */
1149 bool qemu_configure_nic_device(DeviceState
*dev
, bool match_default
,
1152 NICInfo
*nd
= qemu_find_nic_info(object_get_typename(OBJECT(dev
)),
1153 match_default
, alias
);
1156 qdev_set_nic_properties(dev
, nd
);
1162 /* "Please create a device, if you have a configuration for it" */
1163 DeviceState
*qemu_create_nic_device(const char *typename
, bool match_default
,
1166 NICInfo
*nd
= qemu_find_nic_info(typename
, match_default
, alias
);
1173 dev
= qdev_new(typename
);
1174 qdev_set_nic_properties(dev
, nd
);
1178 void qemu_create_nic_bus_devices(BusState
*bus
, const char *parent_type
,
1179 const char *default_model
,
1180 const char *alias
, const char *alias_target
)
1182 GPtrArray
*nic_models
= qemu_get_nic_models(parent_type
);
1188 if (nic_model_help
) {
1190 add_nic_model_help(alias_target
, alias
);
1192 for (i
= 0; i
< nic_models
->len
- 1; i
++) {
1193 add_nic_model_help(nic_models
->pdata
[i
], NULL
);
1197 /* Drop the NULL terminator which would make g_str_equal() unhappy */
1200 for (i
= 0; i
< nb_nics
; i
++) {
1203 if (!nd
->used
|| nd
->instantiated
) {
1207 model
= nd
->model
? nd
->model
: default_model
;
1212 /* Each bus type is allowed *one* substitution */
1213 if (g_str_equal(model
, alias
)) {
1214 model
= alias_target
;
1217 if (!g_ptr_array_find_with_equal_func(nic_models
, model
,
1218 g_str_equal
, NULL
)) {
1219 /* This NIC does not live on this bus. */
1223 dev
= qdev_new(model
);
1224 qdev_set_nic_properties(dev
, nd
);
1225 qdev_realize_and_unref(dev
, bus
, &error_fatal
);
1228 g_ptr_array_free(nic_models
, true);
1231 static int (* const net_client_init_fun
[NET_CLIENT_DRIVER__MAX
])(
1232 const Netdev
*netdev
,
1234 NetClientState
*peer
, Error
**errp
) = {
1235 [NET_CLIENT_DRIVER_NIC
] = net_init_nic
,
1237 [NET_CLIENT_DRIVER_USER
] = net_init_slirp
,
1239 [NET_CLIENT_DRIVER_TAP
] = net_init_tap
,
1240 [NET_CLIENT_DRIVER_SOCKET
] = net_init_socket
,
1241 [NET_CLIENT_DRIVER_STREAM
] = net_init_stream
,
1242 [NET_CLIENT_DRIVER_DGRAM
] = net_init_dgram
,
1244 [NET_CLIENT_DRIVER_VDE
] = net_init_vde
,
1246 #ifdef CONFIG_NETMAP
1247 [NET_CLIENT_DRIVER_NETMAP
] = net_init_netmap
,
1249 #ifdef CONFIG_AF_XDP
1250 [NET_CLIENT_DRIVER_AF_XDP
] = net_init_af_xdp
,
1252 #ifdef CONFIG_NET_BRIDGE
1253 [NET_CLIENT_DRIVER_BRIDGE
] = net_init_bridge
,
1255 [NET_CLIENT_DRIVER_HUBPORT
] = net_init_hubport
,
1256 #ifdef CONFIG_VHOST_NET_USER
1257 [NET_CLIENT_DRIVER_VHOST_USER
] = net_init_vhost_user
,
1259 #ifdef CONFIG_VHOST_NET_VDPA
1260 [NET_CLIENT_DRIVER_VHOST_VDPA
] = net_init_vhost_vdpa
,
1262 #ifdef CONFIG_L2TPV3
1263 [NET_CLIENT_DRIVER_L2TPV3
] = net_init_l2tpv3
,
1266 [NET_CLIENT_DRIVER_VMNET_HOST
] = net_init_vmnet_host
,
1267 [NET_CLIENT_DRIVER_VMNET_SHARED
] = net_init_vmnet_shared
,
1268 [NET_CLIENT_DRIVER_VMNET_BRIDGED
] = net_init_vmnet_bridged
,
1269 #endif /* CONFIG_VMNET */
1273 static int net_client_init1(const Netdev
*netdev
, bool is_netdev
, Error
**errp
)
1275 NetClientState
*peer
= NULL
;
1279 if (netdev
->type
== NET_CLIENT_DRIVER_NIC
||
1280 !net_client_init_fun
[netdev
->type
]) {
1281 error_setg(errp
, "network backend '%s' is not compiled into this binary",
1282 NetClientDriver_str(netdev
->type
));
1286 if (netdev
->type
== NET_CLIENT_DRIVER_NONE
) {
1287 return 0; /* nothing to do */
1289 if (netdev
->type
== NET_CLIENT_DRIVER_HUBPORT
) {
1290 error_setg(errp
, "network backend '%s' is only supported with -netdev/-nic",
1291 NetClientDriver_str(netdev
->type
));
1295 if (!net_client_init_fun
[netdev
->type
]) {
1296 error_setg(errp
, "network backend '%s' is not compiled into this binary",
1297 NetClientDriver_str(netdev
->type
));
1301 /* Do not add to a hub if it's a nic with a netdev= parameter. */
1302 if (netdev
->type
!= NET_CLIENT_DRIVER_NIC
||
1303 !netdev
->u
.nic
.netdev
) {
1304 peer
= net_hub_add_port(0, NULL
, NULL
);
1308 nc
= qemu_find_netdev(netdev
->id
);
1310 error_setg(errp
, "Duplicate ID '%s'", netdev
->id
);
1314 if (net_client_init_fun
[netdev
->type
](netdev
, netdev
->id
, peer
, errp
) < 0) {
1315 /* FIXME drop when all init functions store an Error */
1316 if (errp
&& !*errp
) {
1317 error_setg(errp
, "Device '%s' could not be initialized",
1318 NetClientDriver_str(netdev
->type
));
1324 nc
= qemu_find_netdev(netdev
->id
);
1326 nc
->is_netdev
= true;
1332 void show_netdevs(void)
1335 const char *available_netdevs
[] = {
1344 #ifdef CONFIG_L2TPV3
1350 #ifdef CONFIG_NET_BRIDGE
1353 #ifdef CONFIG_NETMAP
1356 #ifdef CONFIG_AF_XDP
1362 #ifdef CONFIG_VHOST_VDPA
1372 qemu_printf("Available netdev backend types:\n");
1373 for (idx
= 0; idx
< ARRAY_SIZE(available_netdevs
); idx
++) {
1374 qemu_printf("%s\n", available_netdevs
[idx
]);
1378 static int net_client_init(QemuOpts
*opts
, bool is_netdev
, Error
**errp
)
1380 gchar
**substrings
= NULL
;
1381 Netdev
*object
= NULL
;
1383 Visitor
*v
= opts_visitor_new(opts
);
1385 /* Parse convenience option format ipv6-net=fec0::0[/64] */
1386 const char *ip6_net
= qemu_opt_get(opts
, "ipv6-net");
1390 unsigned long prefix_len
= 64; /* Default 64bit prefix length. */
1392 substrings
= g_strsplit(ip6_net
, "/", 2);
1393 if (!substrings
|| !substrings
[0]) {
1394 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "ipv6-net",
1395 "a valid IPv6 prefix");
1399 prefix_addr
= substrings
[0];
1401 /* Handle user-specified prefix length. */
1402 if (substrings
[1] &&
1403 qemu_strtoul(substrings
[1], NULL
, 10, &prefix_len
))
1406 "parameter 'ipv6-net' expects a number after '/'");
1410 qemu_opt_set(opts
, "ipv6-prefix", prefix_addr
, &error_abort
);
1411 qemu_opt_set_number(opts
, "ipv6-prefixlen", prefix_len
,
1413 qemu_opt_unset(opts
, "ipv6-net");
1416 /* Create an ID for -net if the user did not specify one */
1417 if (!is_netdev
&& !qemu_opts_id(opts
)) {
1418 qemu_opts_set_id(opts
, id_generate(ID_NET
));
1421 if (visit_type_Netdev(v
, NULL
, &object
, errp
)) {
1422 ret
= net_client_init1(object
, is_netdev
, errp
);
1425 qapi_free_Netdev(object
);
1428 g_strfreev(substrings
);
1433 void netdev_add(QemuOpts
*opts
, Error
**errp
)
1435 net_client_init(opts
, true, errp
);
1438 void qmp_netdev_add(Netdev
*netdev
, Error
**errp
)
1440 if (!id_wellformed(netdev
->id
)) {
1441 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "id", "an identifier");
1445 net_client_init1(netdev
, true, errp
);
1448 void qmp_netdev_del(const char *id
, Error
**errp
)
1453 nc
= qemu_find_netdev(id
);
1455 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1456 "Device '%s' not found", id
);
1460 if (!nc
->is_netdev
) {
1461 error_setg(errp
, "Device '%s' is not a netdev", id
);
1465 qemu_del_net_client(nc
);
1468 * Wart: we need to delete the QemuOpts associated with netdevs
1469 * created via CLI or HMP, to avoid bogus "Duplicate ID" errors in
1472 opts
= qemu_opts_find(qemu_find_opts("netdev"), id
);
1474 qemu_opts_del(opts
);
1478 static void netfilter_print_info(Monitor
*mon
, NetFilterState
*nf
)
1481 ObjectProperty
*prop
;
1482 ObjectPropertyIterator iter
;
1485 /* generate info str */
1486 object_property_iter_init(&iter
, OBJECT(nf
));
1487 while ((prop
= object_property_iter_next(&iter
))) {
1488 if (!strcmp(prop
->name
, "type")) {
1491 v
= string_output_visitor_new(false, &str
);
1492 object_property_get(OBJECT(nf
), prop
->name
, v
, NULL
);
1493 visit_complete(v
, &str
);
1495 monitor_printf(mon
, ",%s=%s", prop
->name
, str
);
1498 monitor_printf(mon
, "\n");
1501 void print_net_client(Monitor
*mon
, NetClientState
*nc
)
1505 monitor_printf(mon
, "%s: index=%d,type=%s,%s\n", nc
->name
,
1507 NetClientDriver_str(nc
->info
->type
),
1509 if (!QTAILQ_EMPTY(&nc
->filters
)) {
1510 monitor_printf(mon
, "filters:\n");
1512 QTAILQ_FOREACH(nf
, &nc
->filters
, next
) {
1513 monitor_printf(mon
, " - %s: type=%s",
1514 object_get_canonical_path_component(OBJECT(nf
)),
1515 object_get_typename(OBJECT(nf
)));
1516 netfilter_print_info(mon
, nf
);
1520 RxFilterInfoList
*qmp_query_rx_filter(const char *name
, Error
**errp
)
1523 RxFilterInfoList
*filter_list
= NULL
, **tail
= &filter_list
;
1525 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
1528 if (name
&& strcmp(nc
->name
, name
) != 0) {
1532 /* only query rx-filter information of NIC */
1533 if (nc
->info
->type
!= NET_CLIENT_DRIVER_NIC
) {
1535 error_setg(errp
, "net client(%s) isn't a NIC", name
);
1536 assert(!filter_list
);
1542 /* only query information on queue 0 since the info is per nic,
1545 if (nc
->queue_index
!= 0)
1548 if (nc
->info
->query_rx_filter
) {
1549 info
= nc
->info
->query_rx_filter(nc
);
1550 QAPI_LIST_APPEND(tail
, info
);
1552 error_setg(errp
, "net client(%s) doesn't support"
1553 " rx-filter querying", name
);
1554 assert(!filter_list
);
1563 if (filter_list
== NULL
&& name
) {
1564 error_setg(errp
, "invalid net client name: %s", name
);
1570 void colo_notify_filters_event(int event
, Error
**errp
)
1574 NetFilterClass
*nfc
= NULL
;
1575 Error
*local_err
= NULL
;
1577 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
1578 QTAILQ_FOREACH(nf
, &nc
->filters
, next
) {
1579 nfc
= NETFILTER_GET_CLASS(OBJECT(nf
));
1580 nfc
->handle_event(nf
, event
, &local_err
);
1582 error_propagate(errp
, local_err
);
1589 void qmp_set_link(const char *name
, bool up
, Error
**errp
)
1591 NetClientState
*ncs
[MAX_QUEUE_NUM
];
1595 queues
= qemu_find_net_clients_except(name
, ncs
,
1596 NET_CLIENT_DRIVER__MAX
,
1600 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1601 "Device '%s' not found", name
);
1606 for (i
= 0; i
< queues
; i
++) {
1607 ncs
[i
]->link_down
= !up
;
1610 if (nc
->info
->link_status_changed
) {
1611 nc
->info
->link_status_changed(nc
);
1615 /* Change peer link only if the peer is NIC and then notify peer.
1616 * If the peer is a HUBPORT or a backend, we do not change the
1619 * This behavior is compatible with qemu hubs where there could be
1620 * multiple clients that can still communicate with each other in
1621 * disconnected mode. For now maintain this compatibility.
1623 if (nc
->peer
->info
->type
== NET_CLIENT_DRIVER_NIC
) {
1624 for (i
= 0; i
< queues
; i
++) {
1625 ncs
[i
]->peer
->link_down
= !up
;
1628 if (nc
->peer
->info
->link_status_changed
) {
1629 nc
->peer
->info
->link_status_changed(nc
->peer
);
1634 static void net_vm_change_state_handler(void *opaque
, bool running
,
1638 NetClientState
*tmp
;
1640 QTAILQ_FOREACH_SAFE(nc
, &net_clients
, next
, tmp
) {
1642 /* Flush queued packets and wake up backends. */
1643 if (nc
->peer
&& qemu_can_send_packet(nc
)) {
1644 qemu_flush_queued_packets(nc
->peer
);
1647 /* Complete all queued packets, to guarantee we don't modify
1648 * state later when VM is not running.
1650 qemu_flush_or_purge_queued_packets(nc
, true);
1655 void net_cleanup(void)
1657 NetClientState
*nc
, **p
= &QTAILQ_FIRST(&net_clients
);
1659 /*cleanup colo compare module for COLO*/
1660 colo_compare_cleanup();
1663 * Walk the net_clients list and remove the netdevs but *not* any
1664 * NET_CLIENT_DRIVER_NIC entries. The latter are owned by the device
1665 * model which created them, and in some cases (e.g. xen-net-device)
1666 * the device itself may do cleanup at exit and will be upset if we
1667 * just delete its NIC from underneath it.
1669 * Since qemu_del_net_client() may delete multiple entries, using
1670 * QTAILQ_FOREACH_SAFE() is not safe here. The only safe pointer
1671 * to keep as a bookmark is a NET_CLIENT_DRIVER_NIC entry, so keep
1672 * 'p' pointing to either the head of the list, or the 'next' field
1673 * of the latest NET_CLIENT_DRIVER_NIC, and operate on *p as we walk
1676 * The 'nc' variable isn't part of the list traversal; it's purely
1677 * for convenience as too much '(*p)->' has a tendency to make the
1678 * readers' eyes bleed.
1682 if (nc
->info
->type
== NET_CLIENT_DRIVER_NIC
) {
1683 /* Skip NET_CLIENT_DRIVER_NIC entries */
1684 p
= &QTAILQ_NEXT(nc
, next
);
1686 qemu_del_net_client(nc
);
1690 qemu_del_vm_change_state_handler(net_change_state_entry
);
1693 void net_check_clients(void)
1698 if (nic_model_help
) {
1702 net_hub_check_clients();
1704 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
1706 warn_report("%s %s has no peer",
1707 nc
->info
->type
== NET_CLIENT_DRIVER_NIC
1713 /* Check that all NICs requested via -net nic actually got created.
1714 * NICs created via -device don't need to be checked here because
1715 * they are always instantiated.
1717 for (i
= 0; i
< MAX_NICS
; i
++) {
1718 NICInfo
*nd
= &nd_table
[i
];
1719 if (nd
->used
&& !nd
->instantiated
) {
1720 warn_report("requested NIC (%s, model %s) "
1721 "was not created (not supported by this machine?)",
1722 nd
->name
? nd
->name
: "anonymous",
1723 nd
->model
? nd
->model
: "unspecified");
1728 static int net_init_client(void *dummy
, QemuOpts
*opts
, Error
**errp
)
1730 const char *model
= qemu_opt_get(opts
, "model");
1732 if (is_nic_model_help_option(model
)) {
1736 return net_client_init(opts
, false, errp
);
1739 static int net_init_netdev(void *dummy
, QemuOpts
*opts
, Error
**errp
)
1741 const char *type
= qemu_opt_get(opts
, "type");
1743 if (type
&& is_help_option(type
)) {
1747 return net_client_init(opts
, true, errp
);
1750 /* For the convenience "--nic" parameter */
1751 static int net_param_nic(void *dummy
, QemuOpts
*opts
, Error
**errp
)
1758 type
= qemu_opt_get(opts
, "type");
1760 if (g_str_equal(type
, "none")) {
1761 return 0; /* Nothing to do, default_net is cleared in vl.c */
1763 if (is_help_option(type
)) {
1764 GPtrArray
*nic_models
= qemu_get_nic_models(TYPE_DEVICE
);
1768 printf("Available NIC models "
1769 "(use -nic model=help for a filtered list):\n");
1770 for (i
= 0 ; nic_models
->pdata
[i
]; i
++) {
1771 printf("%s\n", (char *)nic_models
->pdata
[i
]);
1773 g_ptr_array_free(nic_models
, true);
1778 idx
= nic_get_free_idx();
1779 if (idx
== -1 || nb_nics
>= MAX_NICS
) {
1780 error_setg(errp
, "no more on-board/default NIC slots available");
1785 qemu_opt_set(opts
, "type", "user", &error_abort
);
1788 ni
= &nd_table
[idx
];
1789 memset(ni
, 0, sizeof(*ni
));
1790 ni
->model
= qemu_opt_get_del(opts
, "model");
1792 if (is_nic_model_help_option(ni
->model
)) {
1796 /* Create an ID if the user did not specify one */
1797 nd_id
= g_strdup(qemu_opts_id(opts
));
1799 nd_id
= id_generate(ID_NET
);
1800 qemu_opts_set_id(opts
, nd_id
);
1803 /* Handle MAC address */
1804 mac
= qemu_opt_get_del(opts
, "mac");
1806 ret
= net_parse_macaddr(ni
->macaddr
.a
, mac
);
1809 error_setg(errp
, "invalid syntax for ethernet address");
1812 if (is_multicast_ether_addr(ni
->macaddr
.a
)) {
1813 error_setg(errp
, "NIC cannot have multicast MAC address");
1818 qemu_macaddr_default_if_unset(&ni
->macaddr
);
1820 ret
= net_client_init(opts
, true, errp
);
1822 ni
->netdev
= qemu_find_netdev(nd_id
);
1832 static void netdev_init_modern(void)
1834 while (!QSIMPLEQ_EMPTY(&nd_queue
)) {
1835 NetdevQueueEntry
*nd
= QSIMPLEQ_FIRST(&nd_queue
);
1837 QSIMPLEQ_REMOVE_HEAD(&nd_queue
, entry
);
1838 loc_push_restore(&nd
->loc
);
1839 net_client_init1(nd
->nd
, true, &error_fatal
);
1841 qapi_free_Netdev(nd
->nd
);
1846 void net_init_clients(void)
1848 net_change_state_entry
=
1849 qemu_add_vm_change_state_handler(net_vm_change_state_handler
, NULL
);
1851 QTAILQ_INIT(&net_clients
);
1853 netdev_init_modern();
1855 qemu_opts_foreach(qemu_find_opts("netdev"), net_init_netdev
, NULL
,
1858 qemu_opts_foreach(qemu_find_opts("nic"), net_param_nic
, NULL
,
1861 qemu_opts_foreach(qemu_find_opts("net"), net_init_client
, NULL
,
1866 * Does this -netdev argument use modern rather than traditional syntax?
1867 * Modern syntax is to be parsed with netdev_parse_modern().
1868 * Traditional syntax is to be parsed with net_client_parse().
1870 bool netdev_is_modern(const char *optstr
)
1875 static QemuOptsList dummy_opts
= {
1877 .implied_opt_name
= "type",
1878 .head
= QTAILQ_HEAD_INITIALIZER(dummy_opts
.head
),
1882 if (optstr
[0] == '{') {
1883 /* This is JSON, which means it's modern syntax */
1887 opts
= qemu_opts_create(&dummy_opts
, NULL
, false, &error_abort
);
1888 qemu_opts_do_parse(opts
, optstr
, dummy_opts
.implied_opt_name
,
1890 type
= qemu_opt_get(opts
, "type");
1891 is_modern
= !g_strcmp0(type
, "stream") || !g_strcmp0(type
, "dgram");
1893 qemu_opts_reset(&dummy_opts
);
1899 * netdev_parse_modern() uses modern, more expressive syntax than
1900 * net_client_parse(), but supports only the -netdev option.
1901 * netdev_parse_modern() appends to @nd_queue, whereas net_client_parse()
1902 * appends to @qemu_netdev_opts.
1904 void netdev_parse_modern(const char *optstr
)
1907 NetdevQueueEntry
*nd
;
1909 v
= qobject_input_visitor_new_str(optstr
, "type", &error_fatal
);
1910 nd
= g_new(NetdevQueueEntry
, 1);
1911 visit_type_Netdev(v
, NULL
, &nd
->nd
, &error_fatal
);
1915 QSIMPLEQ_INSERT_TAIL(&nd_queue
, nd
, entry
);
1918 void net_client_parse(QemuOptsList
*opts_list
, const char *optstr
)
1920 if (!qemu_opts_parse_noisily(opts_list
, optstr
, true)) {
1927 uint32_t net_crc32(const uint8_t *p
, int len
)
1934 for (i
= 0; i
< len
; i
++) {
1936 for (j
= 0; j
< 8; j
++) {
1937 carry
= ((crc
& 0x80000000L
) ? 1 : 0) ^ (b
& 0x01);
1941 crc
= ((crc
^ POLYNOMIAL_BE
) | carry
);
1949 uint32_t net_crc32_le(const uint8_t *p
, int len
)
1956 for (i
= 0; i
< len
; i
++) {
1958 for (j
= 0; j
< 8; j
++) {
1959 carry
= (crc
& 0x1) ^ (b
& 0x01);
1963 crc
^= POLYNOMIAL_LE
;
1971 QemuOptsList qemu_netdev_opts
= {
1973 .implied_opt_name
= "type",
1974 .head
= QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts
.head
),
1977 * no elements => accept any params
1978 * validation will happen later
1980 { /* end of list */ }
1984 QemuOptsList qemu_nic_opts
= {
1986 .implied_opt_name
= "type",
1987 .head
= QTAILQ_HEAD_INITIALIZER(qemu_nic_opts
.head
),
1990 * no elements => accept any params
1991 * validation will happen later
1993 { /* end of list */ }
1997 QemuOptsList qemu_net_opts
= {
1999 .implied_opt_name
= "type",
2000 .head
= QTAILQ_HEAD_INITIALIZER(qemu_net_opts
.head
),
2003 * no elements => accept any params
2004 * validation will happen later
2006 { /* end of list */ }
2010 void net_socket_rs_init(SocketReadState
*rs
,
2011 SocketReadStateFinalize
*finalize
,
2015 rs
->vnet_hdr
= vnet_hdr
;
2018 rs
->vnet_hdr_len
= 0;
2019 memset(rs
->buf
, 0, sizeof(rs
->buf
));
2020 rs
->finalize
= finalize
;
2028 int net_fill_rstate(SocketReadState
*rs
, const uint8_t *buf
, int size
)
2033 /* Reassemble a packet from the network.
2034 * 0 = getting length.
2035 * 1 = getting vnet header length.
2038 switch (rs
->state
) {
2044 memcpy(rs
->buf
+ rs
->index
, buf
, l
);
2048 if (rs
->index
== 4) {
2050 rs
->packet_len
= ntohl(*(uint32_t *)rs
->buf
);
2056 rs
->vnet_hdr_len
= 0;
2065 memcpy(rs
->buf
+ rs
->index
, buf
, l
);
2069 if (rs
->index
== 4) {
2070 /* got vnet header length */
2071 rs
->vnet_hdr_len
= ntohl(*(uint32_t *)rs
->buf
);
2077 l
= rs
->packet_len
- rs
->index
;
2081 if (rs
->index
+ l
<= sizeof(rs
->buf
)) {
2082 memcpy(rs
->buf
+ rs
->index
, buf
, l
);
2084 fprintf(stderr
, "serious error: oversized packet received,"
2085 "connection terminated.\n");
2086 rs
->index
= rs
->state
= 0;
2093 if (rs
->index
>= rs
->packet_len
) {
2096 assert(rs
->finalize
);