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_receive_packet_iov(NetClientState
*nc
, const struct iovec
*iov
,
756 if (!qemu_can_receive_packet(nc
)) {
760 return qemu_net_queue_receive_iov(nc
->incoming_queue
, iov
, iovcnt
);
763 ssize_t
qemu_send_packet_raw(NetClientState
*nc
, const uint8_t *buf
, int size
)
765 return qemu_send_packet_async_with_flags(nc
, QEMU_NET_PACKET_FLAG_RAW
,
769 static ssize_t
nc_sendv_compat(NetClientState
*nc
, const struct iovec
*iov
,
770 int iovcnt
, unsigned flags
)
778 buffer
= iov
[0].iov_base
;
779 offset
= iov
[0].iov_len
;
781 offset
= iov_size(iov
, iovcnt
);
782 if (offset
> NET_BUFSIZE
) {
785 buf
= g_malloc(offset
);
787 offset
= iov_to_buf(iov
, iovcnt
, 0, buf
, offset
);
790 ret
= nc
->info
->receive(nc
, buffer
, offset
);
796 static ssize_t
qemu_deliver_packet_iov(NetClientState
*sender
,
798 const struct iovec
*iov
,
802 MemReentrancyGuard
*owned_reentrancy_guard
;
803 NetClientState
*nc
= opaque
;
805 struct virtio_net_hdr_v1_hash vnet_hdr
= { };
806 g_autofree
struct iovec
*iov_copy
= NULL
;
810 return iov_size(iov
, iovcnt
);
813 if (nc
->receive_disabled
) {
817 if (nc
->info
->type
!= NET_CLIENT_DRIVER_NIC
||
818 qemu_get_nic(nc
)->reentrancy_guard
->engaged_in_io
) {
819 owned_reentrancy_guard
= NULL
;
821 owned_reentrancy_guard
= qemu_get_nic(nc
)->reentrancy_guard
;
822 owned_reentrancy_guard
->engaged_in_io
= true;
825 if ((flags
& QEMU_NET_PACKET_FLAG_RAW
) && nc
->vnet_hdr_len
) {
826 iov_copy
= g_new(struct iovec
, iovcnt
+ 1);
827 iov_copy
[0].iov_base
= &vnet_hdr
;
828 iov_copy
[0].iov_len
= nc
->vnet_hdr_len
;
829 memcpy(&iov_copy
[1], iov
, iovcnt
* sizeof(*iov
));
833 if (nc
->info
->receive_iov
) {
834 ret
= nc
->info
->receive_iov(nc
, iov
, iovcnt
);
836 ret
= nc_sendv_compat(nc
, iov
, iovcnt
, flags
);
839 if (owned_reentrancy_guard
) {
840 owned_reentrancy_guard
->engaged_in_io
= false;
844 nc
->receive_disabled
= 1;
850 ssize_t
qemu_sendv_packet_async(NetClientState
*sender
,
851 const struct iovec
*iov
, int iovcnt
,
852 NetPacketSent
*sent_cb
)
855 size_t size
= iov_size(iov
, iovcnt
);
858 if (size
> NET_BUFSIZE
) {
862 if (sender
->link_down
|| !sender
->peer
) {
866 /* Let filters handle the packet first */
867 ret
= filter_receive_iov(sender
, NET_FILTER_DIRECTION_TX
, sender
,
868 QEMU_NET_PACKET_FLAG_NONE
, iov
, iovcnt
, sent_cb
);
873 ret
= filter_receive_iov(sender
->peer
, NET_FILTER_DIRECTION_RX
, sender
,
874 QEMU_NET_PACKET_FLAG_NONE
, iov
, iovcnt
, sent_cb
);
879 queue
= sender
->peer
->incoming_queue
;
881 return qemu_net_queue_send_iov(queue
, sender
,
882 QEMU_NET_PACKET_FLAG_NONE
,
883 iov
, iovcnt
, sent_cb
);
887 qemu_sendv_packet(NetClientState
*nc
, const struct iovec
*iov
, int iovcnt
)
889 return qemu_sendv_packet_async(nc
, iov
, iovcnt
, NULL
);
892 NetClientState
*qemu_find_netdev(const char *id
)
896 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
897 if (nc
->info
->type
== NET_CLIENT_DRIVER_NIC
)
899 if (!strcmp(nc
->name
, id
)) {
907 int qemu_find_net_clients_except(const char *id
, NetClientState
**ncs
,
908 NetClientDriver type
, int max
)
913 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
914 if (nc
->info
->type
== type
) {
917 if (!id
|| !strcmp(nc
->name
, id
)) {
928 static int nic_get_free_idx(void)
932 for (index
= 0; index
< MAX_NICS
; index
++)
933 if (!nd_table
[index
].used
)
938 GPtrArray
*qemu_get_nic_models(const char *device_type
)
940 GPtrArray
*nic_models
= g_ptr_array_new();
941 GSList
*list
= object_class_get_list_sorted(device_type
, false);
944 DeviceClass
*dc
= OBJECT_CLASS_CHECK(DeviceClass
, list
->data
,
947 if (test_bit(DEVICE_CATEGORY_NETWORK
, dc
->categories
) &&
948 dc
->user_creatable
) {
949 const char *name
= object_class_get_name(list
->data
);
951 * A network device might also be something else than a NIC, see
952 * e.g. the "rocker" device. Thus we have to look for the "netdev"
953 * property, too. Unfortunately, some devices like virtio-net only
954 * create this property during instance_init, so we have to create
955 * a temporary instance here to be able to check it.
957 Object
*obj
= object_new_with_class(OBJECT_CLASS(dc
));
958 if (object_property_find(obj
, "netdev")) {
959 g_ptr_array_add(nic_models
, (gpointer
)name
);
964 g_slist_free_1(list
);
967 g_ptr_array_add(nic_models
, NULL
);
972 static int net_init_nic(const Netdev
*netdev
, const char *name
,
973 NetClientState
*peer
, Error
**errp
)
977 const NetLegacyNicOptions
*nic
;
979 assert(netdev
->type
== NET_CLIENT_DRIVER_NIC
);
980 nic
= &netdev
->u
.nic
;
982 idx
= nic_get_free_idx();
983 if (idx
== -1 || nb_nics
>= MAX_NICS
) {
984 error_setg(errp
, "too many NICs");
990 memset(nd
, 0, sizeof(*nd
));
993 nd
->netdev
= qemu_find_netdev(nic
->netdev
);
995 error_setg(errp
, "netdev '%s' not found", nic
->netdev
);
1002 nd
->name
= g_strdup(name
);
1004 nd
->model
= g_strdup(nic
->model
);
1007 nd
->devaddr
= g_strdup(nic
->addr
);
1011 net_parse_macaddr(nd
->macaddr
.a
, nic
->macaddr
) < 0) {
1012 error_setg(errp
, "invalid syntax for ethernet address");
1016 is_multicast_ether_addr(nd
->macaddr
.a
)) {
1018 "NIC cannot have multicast MAC address (odd 1st byte)");
1021 qemu_macaddr_default_if_unset(&nd
->macaddr
);
1023 if (nic
->has_vectors
) {
1024 if (nic
->vectors
> 0x7ffffff) {
1025 error_setg(errp
, "invalid # of vectors: %"PRIu32
, nic
->vectors
);
1028 nd
->nvectors
= nic
->vectors
;
1030 nd
->nvectors
= DEV_NVECTORS_UNSPECIFIED
;
1039 static gboolean
add_nic_result(gpointer key
, gpointer value
, gpointer user_data
)
1041 GPtrArray
*results
= user_data
;
1042 GPtrArray
*alias_list
= value
;
1043 const char *model
= key
;
1047 result
= g_strdup(model
);
1049 GString
*result_str
= g_string_new(model
);
1052 g_string_append(result_str
, " (aka ");
1053 for (i
= 0; i
< alias_list
->len
; i
++) {
1055 g_string_append(result_str
, ", ");
1057 g_string_append(result_str
, alias_list
->pdata
[i
]);
1059 g_string_append(result_str
, ")");
1060 result
= result_str
->str
;
1061 g_string_free(result_str
, false);
1062 g_ptr_array_unref(alias_list
);
1064 g_ptr_array_add(results
, result
);
1068 static int model_cmp(char **a
, char **b
)
1070 return strcmp(*a
, *b
);
1073 static void show_nic_models(void)
1075 GPtrArray
*results
= g_ptr_array_new();
1078 g_hash_table_foreach_remove(nic_model_help
, add_nic_result
, results
);
1079 g_ptr_array_sort(results
, (GCompareFunc
)model_cmp
);
1081 printf("Available NIC models for this configuration:\n");
1082 for (i
= 0 ; i
< results
->len
; i
++) {
1083 printf("%s\n", (char *)results
->pdata
[i
]);
1085 g_hash_table_unref(nic_model_help
);
1086 nic_model_help
= NULL
;
1089 static void add_nic_model_help(const char *model
, const char *alias
)
1091 GPtrArray
*alias_list
= NULL
;
1093 if (g_hash_table_lookup_extended(nic_model_help
, model
, NULL
,
1094 (gpointer
*)&alias_list
)) {
1095 /* Already exists, no alias to add: return */
1100 /* Check if this alias is already in the list. Add if not. */
1101 if (!g_ptr_array_find_with_equal_func(alias_list
, alias
,
1102 g_str_equal
, NULL
)) {
1103 g_ptr_array_add(alias_list
, g_strdup(alias
));
1108 /* Either this model wasn't in the list already, or a first alias added */
1110 alias_list
= g_ptr_array_new();
1111 g_ptr_array_set_free_func(alias_list
, g_free
);
1112 g_ptr_array_add(alias_list
, g_strdup(alias
));
1114 g_hash_table_replace(nic_model_help
, g_strdup(model
), alias_list
);
1117 NICInfo
*qemu_find_nic_info(const char *typename
, bool match_default
,
1123 if (nic_model_help
) {
1124 add_nic_model_help(typename
, alias
);
1127 for (i
= 0; i
< nb_nics
; i
++) {
1130 if (!nd
->used
|| nd
->instantiated
) {
1134 if ((match_default
&& !nd
->model
) || !g_strcmp0(nd
->model
, typename
)
1135 || (alias
&& !g_strcmp0(nd
->model
, alias
))) {
1143 /* "I have created a device. Please configure it if you can" */
1144 bool qemu_configure_nic_device(DeviceState
*dev
, bool match_default
,
1147 NICInfo
*nd
= qemu_find_nic_info(object_get_typename(OBJECT(dev
)),
1148 match_default
, alias
);
1151 qdev_set_nic_properties(dev
, nd
);
1157 /* "Please create a device, if you have a configuration for it" */
1158 DeviceState
*qemu_create_nic_device(const char *typename
, bool match_default
,
1161 NICInfo
*nd
= qemu_find_nic_info(typename
, match_default
, alias
);
1168 dev
= qdev_new(typename
);
1169 qdev_set_nic_properties(dev
, nd
);
1173 void qemu_create_nic_bus_devices(BusState
*bus
, const char *parent_type
,
1174 const char *default_model
,
1175 const char *alias
, const char *alias_target
)
1177 GPtrArray
*nic_models
= qemu_get_nic_models(parent_type
);
1183 if (nic_model_help
) {
1185 add_nic_model_help(alias_target
, alias
);
1187 for (i
= 0; i
< nic_models
->len
- 1; i
++) {
1188 add_nic_model_help(nic_models
->pdata
[i
], NULL
);
1192 /* Drop the NULL terminator which would make g_str_equal() unhappy */
1195 for (i
= 0; i
< nb_nics
; i
++) {
1198 if (!nd
->used
|| nd
->instantiated
) {
1202 model
= nd
->model
? nd
->model
: default_model
;
1207 /* Each bus type is allowed *one* substitution */
1208 if (g_str_equal(model
, alias
)) {
1209 model
= alias_target
;
1212 if (!g_ptr_array_find_with_equal_func(nic_models
, model
,
1213 g_str_equal
, NULL
)) {
1214 /* This NIC does not live on this bus. */
1218 dev
= qdev_new(model
);
1219 qdev_set_nic_properties(dev
, nd
);
1220 qdev_realize_and_unref(dev
, bus
, &error_fatal
);
1223 g_ptr_array_free(nic_models
, true);
1226 static int (* const net_client_init_fun
[NET_CLIENT_DRIVER__MAX
])(
1227 const Netdev
*netdev
,
1229 NetClientState
*peer
, Error
**errp
) = {
1230 [NET_CLIENT_DRIVER_NIC
] = net_init_nic
,
1232 [NET_CLIENT_DRIVER_USER
] = net_init_slirp
,
1234 [NET_CLIENT_DRIVER_TAP
] = net_init_tap
,
1235 [NET_CLIENT_DRIVER_SOCKET
] = net_init_socket
,
1236 [NET_CLIENT_DRIVER_STREAM
] = net_init_stream
,
1237 [NET_CLIENT_DRIVER_DGRAM
] = net_init_dgram
,
1239 [NET_CLIENT_DRIVER_VDE
] = net_init_vde
,
1241 #ifdef CONFIG_NETMAP
1242 [NET_CLIENT_DRIVER_NETMAP
] = net_init_netmap
,
1244 #ifdef CONFIG_AF_XDP
1245 [NET_CLIENT_DRIVER_AF_XDP
] = net_init_af_xdp
,
1247 #ifdef CONFIG_NET_BRIDGE
1248 [NET_CLIENT_DRIVER_BRIDGE
] = net_init_bridge
,
1250 [NET_CLIENT_DRIVER_HUBPORT
] = net_init_hubport
,
1251 #ifdef CONFIG_VHOST_NET_USER
1252 [NET_CLIENT_DRIVER_VHOST_USER
] = net_init_vhost_user
,
1254 #ifdef CONFIG_VHOST_NET_VDPA
1255 [NET_CLIENT_DRIVER_VHOST_VDPA
] = net_init_vhost_vdpa
,
1257 #ifdef CONFIG_L2TPV3
1258 [NET_CLIENT_DRIVER_L2TPV3
] = net_init_l2tpv3
,
1261 [NET_CLIENT_DRIVER_VMNET_HOST
] = net_init_vmnet_host
,
1262 [NET_CLIENT_DRIVER_VMNET_SHARED
] = net_init_vmnet_shared
,
1263 [NET_CLIENT_DRIVER_VMNET_BRIDGED
] = net_init_vmnet_bridged
,
1264 #endif /* CONFIG_VMNET */
1268 static int net_client_init1(const Netdev
*netdev
, bool is_netdev
, Error
**errp
)
1270 NetClientState
*peer
= NULL
;
1274 if (netdev
->type
== NET_CLIENT_DRIVER_NIC
||
1275 !net_client_init_fun
[netdev
->type
]) {
1276 error_setg(errp
, "network backend '%s' is not compiled into this binary",
1277 NetClientDriver_str(netdev
->type
));
1281 if (netdev
->type
== NET_CLIENT_DRIVER_NONE
) {
1282 return 0; /* nothing to do */
1284 if (netdev
->type
== NET_CLIENT_DRIVER_HUBPORT
) {
1285 error_setg(errp
, "network backend '%s' is only supported with -netdev/-nic",
1286 NetClientDriver_str(netdev
->type
));
1290 if (!net_client_init_fun
[netdev
->type
]) {
1291 error_setg(errp
, "network backend '%s' is not compiled into this binary",
1292 NetClientDriver_str(netdev
->type
));
1296 /* Do not add to a hub if it's a nic with a netdev= parameter. */
1297 if (netdev
->type
!= NET_CLIENT_DRIVER_NIC
||
1298 !netdev
->u
.nic
.netdev
) {
1299 peer
= net_hub_add_port(0, NULL
, NULL
);
1303 nc
= qemu_find_netdev(netdev
->id
);
1305 error_setg(errp
, "Duplicate ID '%s'", netdev
->id
);
1309 if (net_client_init_fun
[netdev
->type
](netdev
, netdev
->id
, peer
, errp
) < 0) {
1310 /* FIXME drop when all init functions store an Error */
1311 if (errp
&& !*errp
) {
1312 error_setg(errp
, "Device '%s' could not be initialized",
1313 NetClientDriver_str(netdev
->type
));
1319 nc
= qemu_find_netdev(netdev
->id
);
1321 nc
->is_netdev
= true;
1327 void show_netdevs(void)
1330 const char *available_netdevs
[] = {
1339 #ifdef CONFIG_L2TPV3
1345 #ifdef CONFIG_NET_BRIDGE
1348 #ifdef CONFIG_NETMAP
1351 #ifdef CONFIG_AF_XDP
1357 #ifdef CONFIG_VHOST_VDPA
1367 qemu_printf("Available netdev backend types:\n");
1368 for (idx
= 0; idx
< ARRAY_SIZE(available_netdevs
); idx
++) {
1369 qemu_printf("%s\n", available_netdevs
[idx
]);
1373 static int net_client_init(QemuOpts
*opts
, bool is_netdev
, Error
**errp
)
1375 gchar
**substrings
= NULL
;
1376 Netdev
*object
= NULL
;
1378 Visitor
*v
= opts_visitor_new(opts
);
1380 /* Parse convenience option format ipv6-net=fec0::0[/64] */
1381 const char *ip6_net
= qemu_opt_get(opts
, "ipv6-net");
1385 unsigned long prefix_len
= 64; /* Default 64bit prefix length. */
1387 substrings
= g_strsplit(ip6_net
, "/", 2);
1388 if (!substrings
|| !substrings
[0]) {
1389 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "ipv6-net",
1390 "a valid IPv6 prefix");
1394 prefix_addr
= substrings
[0];
1396 /* Handle user-specified prefix length. */
1397 if (substrings
[1] &&
1398 qemu_strtoul(substrings
[1], NULL
, 10, &prefix_len
))
1401 "parameter 'ipv6-net' expects a number after '/'");
1405 qemu_opt_set(opts
, "ipv6-prefix", prefix_addr
, &error_abort
);
1406 qemu_opt_set_number(opts
, "ipv6-prefixlen", prefix_len
,
1408 qemu_opt_unset(opts
, "ipv6-net");
1411 /* Create an ID for -net if the user did not specify one */
1412 if (!is_netdev
&& !qemu_opts_id(opts
)) {
1413 qemu_opts_set_id(opts
, id_generate(ID_NET
));
1416 if (visit_type_Netdev(v
, NULL
, &object
, errp
)) {
1417 ret
= net_client_init1(object
, is_netdev
, errp
);
1420 qapi_free_Netdev(object
);
1423 g_strfreev(substrings
);
1428 void netdev_add(QemuOpts
*opts
, Error
**errp
)
1430 net_client_init(opts
, true, errp
);
1433 void qmp_netdev_add(Netdev
*netdev
, Error
**errp
)
1435 if (!id_wellformed(netdev
->id
)) {
1436 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "id", "an identifier");
1440 net_client_init1(netdev
, true, errp
);
1443 void qmp_netdev_del(const char *id
, Error
**errp
)
1448 nc
= qemu_find_netdev(id
);
1450 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1451 "Device '%s' not found", id
);
1455 if (!nc
->is_netdev
) {
1456 error_setg(errp
, "Device '%s' is not a netdev", id
);
1460 qemu_del_net_client(nc
);
1463 * Wart: we need to delete the QemuOpts associated with netdevs
1464 * created via CLI or HMP, to avoid bogus "Duplicate ID" errors in
1467 opts
= qemu_opts_find(qemu_find_opts("netdev"), id
);
1469 qemu_opts_del(opts
);
1473 static void netfilter_print_info(Monitor
*mon
, NetFilterState
*nf
)
1476 ObjectProperty
*prop
;
1477 ObjectPropertyIterator iter
;
1480 /* generate info str */
1481 object_property_iter_init(&iter
, OBJECT(nf
));
1482 while ((prop
= object_property_iter_next(&iter
))) {
1483 if (!strcmp(prop
->name
, "type")) {
1486 v
= string_output_visitor_new(false, &str
);
1487 object_property_get(OBJECT(nf
), prop
->name
, v
, NULL
);
1488 visit_complete(v
, &str
);
1490 monitor_printf(mon
, ",%s=%s", prop
->name
, str
);
1493 monitor_printf(mon
, "\n");
1496 void print_net_client(Monitor
*mon
, NetClientState
*nc
)
1500 monitor_printf(mon
, "%s: index=%d,type=%s,%s\n", nc
->name
,
1502 NetClientDriver_str(nc
->info
->type
),
1504 if (!QTAILQ_EMPTY(&nc
->filters
)) {
1505 monitor_printf(mon
, "filters:\n");
1507 QTAILQ_FOREACH(nf
, &nc
->filters
, next
) {
1508 monitor_printf(mon
, " - %s: type=%s",
1509 object_get_canonical_path_component(OBJECT(nf
)),
1510 object_get_typename(OBJECT(nf
)));
1511 netfilter_print_info(mon
, nf
);
1515 RxFilterInfoList
*qmp_query_rx_filter(const char *name
, Error
**errp
)
1518 RxFilterInfoList
*filter_list
= NULL
, **tail
= &filter_list
;
1520 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
1523 if (name
&& strcmp(nc
->name
, name
) != 0) {
1527 /* only query rx-filter information of NIC */
1528 if (nc
->info
->type
!= NET_CLIENT_DRIVER_NIC
) {
1530 error_setg(errp
, "net client(%s) isn't a NIC", name
);
1531 assert(!filter_list
);
1537 /* only query information on queue 0 since the info is per nic,
1540 if (nc
->queue_index
!= 0)
1543 if (nc
->info
->query_rx_filter
) {
1544 info
= nc
->info
->query_rx_filter(nc
);
1545 QAPI_LIST_APPEND(tail
, info
);
1547 error_setg(errp
, "net client(%s) doesn't support"
1548 " rx-filter querying", name
);
1549 assert(!filter_list
);
1558 if (filter_list
== NULL
&& name
) {
1559 error_setg(errp
, "invalid net client name: %s", name
);
1565 void colo_notify_filters_event(int event
, Error
**errp
)
1569 NetFilterClass
*nfc
= NULL
;
1570 Error
*local_err
= NULL
;
1572 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
1573 QTAILQ_FOREACH(nf
, &nc
->filters
, next
) {
1574 nfc
= NETFILTER_GET_CLASS(OBJECT(nf
));
1575 nfc
->handle_event(nf
, event
, &local_err
);
1577 error_propagate(errp
, local_err
);
1584 void qmp_set_link(const char *name
, bool up
, Error
**errp
)
1586 NetClientState
*ncs
[MAX_QUEUE_NUM
];
1590 queues
= qemu_find_net_clients_except(name
, ncs
,
1591 NET_CLIENT_DRIVER__MAX
,
1595 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1596 "Device '%s' not found", name
);
1601 for (i
= 0; i
< queues
; i
++) {
1602 ncs
[i
]->link_down
= !up
;
1605 if (nc
->info
->link_status_changed
) {
1606 nc
->info
->link_status_changed(nc
);
1610 /* Change peer link only if the peer is NIC and then notify peer.
1611 * If the peer is a HUBPORT or a backend, we do not change the
1614 * This behavior is compatible with qemu hubs where there could be
1615 * multiple clients that can still communicate with each other in
1616 * disconnected mode. For now maintain this compatibility.
1618 if (nc
->peer
->info
->type
== NET_CLIENT_DRIVER_NIC
) {
1619 for (i
= 0; i
< queues
; i
++) {
1620 ncs
[i
]->peer
->link_down
= !up
;
1623 if (nc
->peer
->info
->link_status_changed
) {
1624 nc
->peer
->info
->link_status_changed(nc
->peer
);
1629 static void net_vm_change_state_handler(void *opaque
, bool running
,
1633 NetClientState
*tmp
;
1635 QTAILQ_FOREACH_SAFE(nc
, &net_clients
, next
, tmp
) {
1637 /* Flush queued packets and wake up backends. */
1638 if (nc
->peer
&& qemu_can_send_packet(nc
)) {
1639 qemu_flush_queued_packets(nc
->peer
);
1642 /* Complete all queued packets, to guarantee we don't modify
1643 * state later when VM is not running.
1645 qemu_flush_or_purge_queued_packets(nc
, true);
1650 void net_cleanup(void)
1652 NetClientState
*nc
, **p
= &QTAILQ_FIRST(&net_clients
);
1654 /*cleanup colo compare module for COLO*/
1655 colo_compare_cleanup();
1658 * Walk the net_clients list and remove the netdevs but *not* any
1659 * NET_CLIENT_DRIVER_NIC entries. The latter are owned by the device
1660 * model which created them, and in some cases (e.g. xen-net-device)
1661 * the device itself may do cleanup at exit and will be upset if we
1662 * just delete its NIC from underneath it.
1664 * Since qemu_del_net_client() may delete multiple entries, using
1665 * QTAILQ_FOREACH_SAFE() is not safe here. The only safe pointer
1666 * to keep as a bookmark is a NET_CLIENT_DRIVER_NIC entry, so keep
1667 * 'p' pointing to either the head of the list, or the 'next' field
1668 * of the latest NET_CLIENT_DRIVER_NIC, and operate on *p as we walk
1671 * The 'nc' variable isn't part of the list traversal; it's purely
1672 * for convenience as too much '(*p)->' has a tendency to make the
1673 * readers' eyes bleed.
1677 if (nc
->info
->type
== NET_CLIENT_DRIVER_NIC
) {
1678 /* Skip NET_CLIENT_DRIVER_NIC entries */
1679 p
= &QTAILQ_NEXT(nc
, next
);
1681 qemu_del_net_client(nc
);
1685 qemu_del_vm_change_state_handler(net_change_state_entry
);
1688 void net_check_clients(void)
1693 if (nic_model_help
) {
1697 net_hub_check_clients();
1699 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
1701 warn_report("%s %s has no peer",
1702 nc
->info
->type
== NET_CLIENT_DRIVER_NIC
1708 /* Check that all NICs requested via -net nic actually got created.
1709 * NICs created via -device don't need to be checked here because
1710 * they are always instantiated.
1712 for (i
= 0; i
< MAX_NICS
; i
++) {
1713 NICInfo
*nd
= &nd_table
[i
];
1714 if (nd
->used
&& !nd
->instantiated
) {
1715 warn_report("requested NIC (%s, model %s) "
1716 "was not created (not supported by this machine?)",
1717 nd
->name
? nd
->name
: "anonymous",
1718 nd
->model
? nd
->model
: "unspecified");
1723 static int net_init_client(void *dummy
, QemuOpts
*opts
, Error
**errp
)
1725 return net_client_init(opts
, false, errp
);
1728 static int net_init_netdev(void *dummy
, QemuOpts
*opts
, Error
**errp
)
1730 const char *type
= qemu_opt_get(opts
, "type");
1732 if (type
&& is_help_option(type
)) {
1736 return net_client_init(opts
, true, errp
);
1739 /* For the convenience "--nic" parameter */
1740 static int net_param_nic(void *dummy
, QemuOpts
*opts
, Error
**errp
)
1747 type
= qemu_opt_get(opts
, "type");
1749 if (g_str_equal(type
, "none")) {
1750 return 0; /* Nothing to do, default_net is cleared in vl.c */
1752 if (is_help_option(type
)) {
1753 GPtrArray
*nic_models
= qemu_get_nic_models(TYPE_DEVICE
);
1757 printf("Available NIC models "
1758 "(use -nic model=help for a filtered list):\n");
1759 for (i
= 0 ; nic_models
->pdata
[i
]; i
++) {
1760 printf("%s\n", (char *)nic_models
->pdata
[i
]);
1762 g_ptr_array_free(nic_models
, true);
1767 idx
= nic_get_free_idx();
1768 if (idx
== -1 || nb_nics
>= MAX_NICS
) {
1769 error_setg(errp
, "no more on-board/default NIC slots available");
1774 qemu_opt_set(opts
, "type", "user", &error_abort
);
1777 ni
= &nd_table
[idx
];
1778 memset(ni
, 0, sizeof(*ni
));
1779 ni
->model
= qemu_opt_get_del(opts
, "model");
1781 if (!nic_model_help
&& !g_strcmp0(ni
->model
, "help")) {
1782 nic_model_help
= g_hash_table_new_full(g_str_hash
, g_str_equal
,
1787 /* Create an ID if the user did not specify one */
1788 nd_id
= g_strdup(qemu_opts_id(opts
));
1790 nd_id
= id_generate(ID_NET
);
1791 qemu_opts_set_id(opts
, nd_id
);
1794 /* Handle MAC address */
1795 mac
= qemu_opt_get_del(opts
, "mac");
1797 ret
= net_parse_macaddr(ni
->macaddr
.a
, mac
);
1800 error_setg(errp
, "invalid syntax for ethernet address");
1803 if (is_multicast_ether_addr(ni
->macaddr
.a
)) {
1804 error_setg(errp
, "NIC cannot have multicast MAC address");
1809 qemu_macaddr_default_if_unset(&ni
->macaddr
);
1811 ret
= net_client_init(opts
, true, errp
);
1813 ni
->netdev
= qemu_find_netdev(nd_id
);
1823 static void netdev_init_modern(void)
1825 while (!QSIMPLEQ_EMPTY(&nd_queue
)) {
1826 NetdevQueueEntry
*nd
= QSIMPLEQ_FIRST(&nd_queue
);
1828 QSIMPLEQ_REMOVE_HEAD(&nd_queue
, entry
);
1829 loc_push_restore(&nd
->loc
);
1830 net_client_init1(nd
->nd
, true, &error_fatal
);
1832 qapi_free_Netdev(nd
->nd
);
1837 void net_init_clients(void)
1839 net_change_state_entry
=
1840 qemu_add_vm_change_state_handler(net_vm_change_state_handler
, NULL
);
1842 QTAILQ_INIT(&net_clients
);
1844 netdev_init_modern();
1846 qemu_opts_foreach(qemu_find_opts("netdev"), net_init_netdev
, NULL
,
1849 qemu_opts_foreach(qemu_find_opts("nic"), net_param_nic
, NULL
,
1852 qemu_opts_foreach(qemu_find_opts("net"), net_init_client
, NULL
,
1857 * Does this -netdev argument use modern rather than traditional syntax?
1858 * Modern syntax is to be parsed with netdev_parse_modern().
1859 * Traditional syntax is to be parsed with net_client_parse().
1861 bool netdev_is_modern(const char *optstr
)
1866 static QemuOptsList dummy_opts
= {
1868 .implied_opt_name
= "type",
1869 .head
= QTAILQ_HEAD_INITIALIZER(dummy_opts
.head
),
1873 if (optstr
[0] == '{') {
1874 /* This is JSON, which means it's modern syntax */
1878 opts
= qemu_opts_create(&dummy_opts
, NULL
, false, &error_abort
);
1879 qemu_opts_do_parse(opts
, optstr
, dummy_opts
.implied_opt_name
,
1881 type
= qemu_opt_get(opts
, "type");
1882 is_modern
= !g_strcmp0(type
, "stream") || !g_strcmp0(type
, "dgram");
1884 qemu_opts_reset(&dummy_opts
);
1890 * netdev_parse_modern() uses modern, more expressive syntax than
1891 * net_client_parse(), but supports only the -netdev option.
1892 * netdev_parse_modern() appends to @nd_queue, whereas net_client_parse()
1893 * appends to @qemu_netdev_opts.
1895 void netdev_parse_modern(const char *optstr
)
1898 NetdevQueueEntry
*nd
;
1900 v
= qobject_input_visitor_new_str(optstr
, "type", &error_fatal
);
1901 nd
= g_new(NetdevQueueEntry
, 1);
1902 visit_type_Netdev(v
, NULL
, &nd
->nd
, &error_fatal
);
1906 QSIMPLEQ_INSERT_TAIL(&nd_queue
, nd
, entry
);
1909 void net_client_parse(QemuOptsList
*opts_list
, const char *optstr
)
1911 if (!qemu_opts_parse_noisily(opts_list
, optstr
, true)) {
1918 uint32_t net_crc32(const uint8_t *p
, int len
)
1925 for (i
= 0; i
< len
; i
++) {
1927 for (j
= 0; j
< 8; j
++) {
1928 carry
= ((crc
& 0x80000000L
) ? 1 : 0) ^ (b
& 0x01);
1932 crc
= ((crc
^ POLYNOMIAL_BE
) | carry
);
1940 uint32_t net_crc32_le(const uint8_t *p
, int len
)
1947 for (i
= 0; i
< len
; i
++) {
1949 for (j
= 0; j
< 8; j
++) {
1950 carry
= (crc
& 0x1) ^ (b
& 0x01);
1954 crc
^= POLYNOMIAL_LE
;
1962 QemuOptsList qemu_netdev_opts
= {
1964 .implied_opt_name
= "type",
1965 .head
= QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts
.head
),
1968 * no elements => accept any params
1969 * validation will happen later
1971 { /* end of list */ }
1975 QemuOptsList qemu_nic_opts
= {
1977 .implied_opt_name
= "type",
1978 .head
= QTAILQ_HEAD_INITIALIZER(qemu_nic_opts
.head
),
1981 * no elements => accept any params
1982 * validation will happen later
1984 { /* end of list */ }
1988 QemuOptsList qemu_net_opts
= {
1990 .implied_opt_name
= "type",
1991 .head
= QTAILQ_HEAD_INITIALIZER(qemu_net_opts
.head
),
1994 * no elements => accept any params
1995 * validation will happen later
1997 { /* end of list */ }
2001 void net_socket_rs_init(SocketReadState
*rs
,
2002 SocketReadStateFinalize
*finalize
,
2006 rs
->vnet_hdr
= vnet_hdr
;
2009 rs
->vnet_hdr_len
= 0;
2010 memset(rs
->buf
, 0, sizeof(rs
->buf
));
2011 rs
->finalize
= finalize
;
2019 int net_fill_rstate(SocketReadState
*rs
, const uint8_t *buf
, int size
)
2024 /* Reassemble a packet from the network.
2025 * 0 = getting length.
2026 * 1 = getting vnet header length.
2029 switch (rs
->state
) {
2035 memcpy(rs
->buf
+ rs
->index
, buf
, l
);
2039 if (rs
->index
== 4) {
2041 rs
->packet_len
= ntohl(*(uint32_t *)rs
->buf
);
2047 rs
->vnet_hdr_len
= 0;
2056 memcpy(rs
->buf
+ rs
->index
, buf
, l
);
2060 if (rs
->index
== 4) {
2061 /* got vnet header length */
2062 rs
->vnet_hdr_len
= ntohl(*(uint32_t *)rs
->buf
);
2068 l
= rs
->packet_len
- rs
->index
;
2072 if (rs
->index
+ l
<= sizeof(rs
->buf
)) {
2073 memcpy(rs
->buf
+ rs
->index
, buf
, l
);
2075 fprintf(stderr
, "serious error: oversized packet received,"
2076 "connection terminated.\n");
2077 rs
->index
= rs
->state
= 0;
2084 if (rs
->index
>= rs
->packet_len
) {
2087 assert(rs
->finalize
);