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 static GHashTable
*nic_model_help
;
81 static NICInfo nd_table
[MAX_NICS
];
83 /***********************************************************/
84 /* network device redirectors */
86 int convert_host_port(struct sockaddr_in
*saddr
, const char *host
,
87 const char *port
, Error
**errp
)
93 memset(saddr
, 0, sizeof(*saddr
));
95 saddr
->sin_family
= AF_INET
;
96 if (host
[0] == '\0') {
97 saddr
->sin_addr
.s_addr
= 0;
99 if (qemu_isdigit(host
[0])) {
100 if (!inet_aton(host
, &saddr
->sin_addr
)) {
101 error_setg(errp
, "host address '%s' is not a valid "
102 "IPv4 address", host
);
106 he
= gethostbyname(host
);
108 error_setg(errp
, "can't resolve host address '%s'", host
);
111 saddr
->sin_addr
= *(struct in_addr
*)he
->h_addr
;
114 if (qemu_strtol(port
, &r
, 0, &p
) != 0) {
115 error_setg(errp
, "port number '%s' is invalid", port
);
118 saddr
->sin_port
= htons(p
);
122 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
,
128 substrings
= g_strsplit(str
, ":", 2);
129 if (!substrings
|| !substrings
[0] || !substrings
[1]) {
130 error_setg(errp
, "host address '%s' doesn't contain ':' "
131 "separating host from port", str
);
136 ret
= convert_host_port(saddr
, substrings
[0], substrings
[1], errp
);
139 g_strfreev(substrings
);
143 char *qemu_mac_strdup_printf(const uint8_t *macaddr
)
145 return g_strdup_printf("%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",
146 macaddr
[0], macaddr
[1], macaddr
[2],
147 macaddr
[3], macaddr
[4], macaddr
[5]);
150 void qemu_set_info_str(NetClientState
*nc
, const char *fmt
, ...)
155 vsnprintf(nc
->info_str
, sizeof(nc
->info_str
), fmt
, ap
);
159 void qemu_format_nic_info_str(NetClientState
*nc
, uint8_t macaddr
[6])
161 qemu_set_info_str(nc
, "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
162 nc
->model
, macaddr
[0], macaddr
[1], macaddr
[2],
163 macaddr
[3], macaddr
[4], macaddr
[5]);
166 static int mac_table
[256] = {0};
168 static void qemu_macaddr_set_used(MACAddr
*macaddr
)
172 for (index
= 0x56; index
< 0xFF; index
++) {
173 if (macaddr
->a
[5] == index
) {
179 static void qemu_macaddr_set_free(MACAddr
*macaddr
)
182 static const MACAddr base
= { .a
= { 0x52, 0x54, 0x00, 0x12, 0x34, 0 } };
184 if (memcmp(macaddr
->a
, &base
.a
, (sizeof(base
.a
) - 1)) != 0) {
187 for (index
= 0x56; index
< 0xFF; index
++) {
188 if (macaddr
->a
[5] == index
) {
194 static int qemu_macaddr_get_free(void)
198 for (index
= 0x56; index
< 0xFF; index
++) {
199 if (mac_table
[index
] == 0) {
207 void qemu_macaddr_default_if_unset(MACAddr
*macaddr
)
209 static const MACAddr zero
= { .a
= { 0,0,0,0,0,0 } };
210 static const MACAddr base
= { .a
= { 0x52, 0x54, 0x00, 0x12, 0x34, 0 } };
212 if (memcmp(macaddr
, &zero
, sizeof(zero
)) != 0) {
213 if (memcmp(macaddr
->a
, &base
.a
, (sizeof(base
.a
) - 1)) != 0) {
216 qemu_macaddr_set_used(macaddr
);
221 macaddr
->a
[0] = 0x52;
222 macaddr
->a
[1] = 0x54;
223 macaddr
->a
[2] = 0x00;
224 macaddr
->a
[3] = 0x12;
225 macaddr
->a
[4] = 0x34;
226 macaddr
->a
[5] = qemu_macaddr_get_free();
227 qemu_macaddr_set_used(macaddr
);
231 * Generate a name for net client
233 * Only net clients created with the legacy -net option and NICs need this.
235 static char *assign_name(NetClientState
*nc1
, const char *model
)
240 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
244 if (strcmp(nc
->model
, model
) == 0) {
249 return g_strdup_printf("%s.%d", model
, id
);
252 static void qemu_net_client_destructor(NetClientState
*nc
)
256 static ssize_t
qemu_deliver_packet_iov(NetClientState
*sender
,
258 const struct iovec
*iov
,
262 static void qemu_net_client_setup(NetClientState
*nc
,
264 NetClientState
*peer
,
267 NetClientDestructor
*destructor
,
271 nc
->model
= g_strdup(model
);
273 nc
->name
= g_strdup(name
);
275 nc
->name
= assign_name(nc
, model
);
283 QTAILQ_INSERT_TAIL(&net_clients
, nc
, next
);
285 nc
->incoming_queue
= qemu_new_net_queue(qemu_deliver_packet_iov
, nc
);
286 nc
->destructor
= destructor
;
287 nc
->is_datapath
= is_datapath
;
288 QTAILQ_INIT(&nc
->filters
);
291 NetClientState
*qemu_new_net_client(NetClientInfo
*info
,
292 NetClientState
*peer
,
298 assert(info
->size
>= sizeof(NetClientState
));
300 nc
= g_malloc0(info
->size
);
301 qemu_net_client_setup(nc
, info
, peer
, model
, name
,
302 qemu_net_client_destructor
, true);
307 NetClientState
*qemu_new_net_control_client(NetClientInfo
*info
,
308 NetClientState
*peer
,
314 assert(info
->size
>= sizeof(NetClientState
));
316 nc
= g_malloc0(info
->size
);
317 qemu_net_client_setup(nc
, info
, peer
, model
, name
,
318 qemu_net_client_destructor
, false);
323 NICState
*qemu_new_nic(NetClientInfo
*info
,
327 MemReentrancyGuard
*reentrancy_guard
,
330 NetClientState
**peers
= conf
->peers
.ncs
;
332 int i
, queues
= MAX(1, conf
->peers
.queues
);
334 assert(info
->type
== NET_CLIENT_DRIVER_NIC
);
335 assert(info
->size
>= sizeof(NICState
));
337 nic
= g_malloc0(info
->size
+ sizeof(NetClientState
) * queues
);
338 nic
->ncs
= (void *)nic
+ info
->size
;
340 nic
->reentrancy_guard
= reentrancy_guard
,
341 nic
->opaque
= opaque
;
343 for (i
= 0; i
< queues
; i
++) {
344 qemu_net_client_setup(&nic
->ncs
[i
], info
, peers
[i
], model
, name
,
346 nic
->ncs
[i
].queue_index
= i
;
352 NetClientState
*qemu_get_subqueue(NICState
*nic
, int queue_index
)
354 return nic
->ncs
+ queue_index
;
357 NetClientState
*qemu_get_queue(NICState
*nic
)
359 return qemu_get_subqueue(nic
, 0);
362 NICState
*qemu_get_nic(NetClientState
*nc
)
364 NetClientState
*nc0
= nc
- nc
->queue_index
;
366 return (NICState
*)((void *)nc0
- nc
->info
->size
);
369 void *qemu_get_nic_opaque(NetClientState
*nc
)
371 NICState
*nic
= qemu_get_nic(nc
);
376 NetClientState
*qemu_get_peer(NetClientState
*nc
, int queue_index
)
379 NetClientState
*ncs
= nc
+ queue_index
;
383 static void qemu_cleanup_net_client(NetClientState
*nc
)
385 QTAILQ_REMOVE(&net_clients
, nc
, next
);
387 if (nc
->info
->cleanup
) {
388 nc
->info
->cleanup(nc
);
392 static void qemu_free_net_client(NetClientState
*nc
)
394 if (nc
->incoming_queue
) {
395 qemu_del_net_queue(nc
->incoming_queue
);
398 nc
->peer
->peer
= NULL
;
402 if (nc
->destructor
) {
407 void qemu_del_net_client(NetClientState
*nc
)
409 NetClientState
*ncs
[MAX_QUEUE_NUM
];
411 NetFilterState
*nf
, *next
;
413 assert(nc
->info
->type
!= NET_CLIENT_DRIVER_NIC
);
415 /* If the NetClientState belongs to a multiqueue backend, we will change all
416 * other NetClientStates also.
418 queues
= qemu_find_net_clients_except(nc
->name
, ncs
,
419 NET_CLIENT_DRIVER_NIC
,
423 QTAILQ_FOREACH_SAFE(nf
, &nc
->filters
, next
, next
) {
424 object_unparent(OBJECT(nf
));
427 /* If there is a peer NIC, delete and cleanup client, but do not free. */
428 if (nc
->peer
&& nc
->peer
->info
->type
== NET_CLIENT_DRIVER_NIC
) {
429 NICState
*nic
= qemu_get_nic(nc
->peer
);
430 if (nic
->peer_deleted
) {
433 nic
->peer_deleted
= true;
435 for (i
= 0; i
< queues
; i
++) {
436 ncs
[i
]->peer
->link_down
= true;
439 if (nc
->peer
->info
->link_status_changed
) {
440 nc
->peer
->info
->link_status_changed(nc
->peer
);
443 for (i
= 0; i
< queues
; i
++) {
444 qemu_cleanup_net_client(ncs
[i
]);
450 for (i
= 0; i
< queues
; i
++) {
451 qemu_cleanup_net_client(ncs
[i
]);
452 qemu_free_net_client(ncs
[i
]);
456 void qemu_del_nic(NICState
*nic
)
458 int i
, queues
= MAX(nic
->conf
->peers
.queues
, 1);
460 qemu_macaddr_set_free(&nic
->conf
->macaddr
);
462 for (i
= 0; i
< queues
; i
++) {
463 NetClientState
*nc
= qemu_get_subqueue(nic
, i
);
464 /* If this is a peer NIC and peer has already been deleted, free it now. */
465 if (nic
->peer_deleted
) {
466 qemu_free_net_client(nc
->peer
);
467 } else if (nc
->peer
) {
468 /* if there are RX packets pending, complete them */
469 qemu_purge_queued_packets(nc
->peer
);
473 for (i
= queues
- 1; i
>= 0; i
--) {
474 NetClientState
*nc
= qemu_get_subqueue(nic
, i
);
476 qemu_cleanup_net_client(nc
);
477 qemu_free_net_client(nc
);
483 void qemu_foreach_nic(qemu_nic_foreach func
, void *opaque
)
487 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
488 if (nc
->info
->type
== NET_CLIENT_DRIVER_NIC
) {
489 if (nc
->queue_index
== 0) {
490 func(qemu_get_nic(nc
), opaque
);
496 bool qemu_has_ufo(NetClientState
*nc
)
498 if (!nc
|| !nc
->info
->has_ufo
) {
502 return nc
->info
->has_ufo(nc
);
505 bool qemu_has_uso(NetClientState
*nc
)
507 if (!nc
|| !nc
->info
->has_uso
) {
511 return nc
->info
->has_uso(nc
);
514 bool qemu_has_vnet_hdr(NetClientState
*nc
)
516 if (!nc
|| !nc
->info
->has_vnet_hdr
) {
520 return nc
->info
->has_vnet_hdr(nc
);
523 bool qemu_has_vnet_hdr_len(NetClientState
*nc
, int len
)
525 if (!nc
|| !nc
->info
->has_vnet_hdr_len
) {
529 return nc
->info
->has_vnet_hdr_len(nc
, len
);
532 void qemu_set_offload(NetClientState
*nc
, int csum
, int tso4
, int tso6
,
533 int ecn
, int ufo
, int uso4
, int uso6
)
535 if (!nc
|| !nc
->info
->set_offload
) {
539 nc
->info
->set_offload(nc
, csum
, tso4
, tso6
, ecn
, ufo
, uso4
, uso6
);
542 int qemu_get_vnet_hdr_len(NetClientState
*nc
)
544 return nc
->vnet_hdr_len
;
547 void qemu_set_vnet_hdr_len(NetClientState
*nc
, int len
)
549 if (!nc
|| !nc
->info
->set_vnet_hdr_len
) {
553 nc
->vnet_hdr_len
= len
;
554 nc
->info
->set_vnet_hdr_len(nc
, len
);
557 int qemu_set_vnet_le(NetClientState
*nc
, bool is_le
)
560 if (!nc
|| !nc
->info
->set_vnet_le
) {
564 return nc
->info
->set_vnet_le(nc
, is_le
);
570 int qemu_set_vnet_be(NetClientState
*nc
, bool is_be
)
575 if (!nc
|| !nc
->info
->set_vnet_be
) {
579 return nc
->info
->set_vnet_be(nc
, is_be
);
583 int qemu_can_receive_packet(NetClientState
*nc
)
585 if (nc
->receive_disabled
) {
587 } else if (nc
->info
->can_receive
&&
588 !nc
->info
->can_receive(nc
)) {
594 int qemu_can_send_packet(NetClientState
*sender
)
596 int vm_running
= runstate_is_running();
606 return qemu_can_receive_packet(sender
->peer
);
609 static ssize_t
filter_receive_iov(NetClientState
*nc
,
610 NetFilterDirection direction
,
611 NetClientState
*sender
,
613 const struct iovec
*iov
,
615 NetPacketSent
*sent_cb
)
618 NetFilterState
*nf
= NULL
;
620 if (direction
== NET_FILTER_DIRECTION_TX
) {
621 QTAILQ_FOREACH(nf
, &nc
->filters
, next
) {
622 ret
= qemu_netfilter_receive(nf
, direction
, sender
, flags
, iov
,
629 QTAILQ_FOREACH_REVERSE(nf
, &nc
->filters
, next
) {
630 ret
= qemu_netfilter_receive(nf
, direction
, sender
, flags
, iov
,
641 static ssize_t
filter_receive(NetClientState
*nc
,
642 NetFilterDirection direction
,
643 NetClientState
*sender
,
647 NetPacketSent
*sent_cb
)
650 .iov_base
= (void *)data
,
654 return filter_receive_iov(nc
, direction
, sender
, flags
, &iov
, 1, sent_cb
);
657 void qemu_purge_queued_packets(NetClientState
*nc
)
663 qemu_net_queue_purge(nc
->peer
->incoming_queue
, nc
);
666 void qemu_flush_or_purge_queued_packets(NetClientState
*nc
, bool purge
)
668 nc
->receive_disabled
= 0;
670 if (nc
->peer
&& nc
->peer
->info
->type
== NET_CLIENT_DRIVER_HUBPORT
) {
671 if (net_hub_flush(nc
->peer
)) {
675 if (qemu_net_queue_flush(nc
->incoming_queue
)) {
676 /* We emptied the queue successfully, signal to the IO thread to repoll
677 * the file descriptor (for tap, for example).
681 /* Unable to empty the queue, purge remaining packets */
682 qemu_net_queue_purge(nc
->incoming_queue
, nc
->peer
);
686 void qemu_flush_queued_packets(NetClientState
*nc
)
688 qemu_flush_or_purge_queued_packets(nc
, false);
691 static ssize_t
qemu_send_packet_async_with_flags(NetClientState
*sender
,
693 const uint8_t *buf
, int size
,
694 NetPacketSent
*sent_cb
)
700 printf("qemu_send_packet_async:\n");
701 qemu_hexdump(stdout
, "net", buf
, size
);
704 if (sender
->link_down
|| !sender
->peer
) {
708 /* Let filters handle the packet first */
709 ret
= filter_receive(sender
, NET_FILTER_DIRECTION_TX
,
710 sender
, flags
, buf
, size
, sent_cb
);
715 ret
= filter_receive(sender
->peer
, NET_FILTER_DIRECTION_RX
,
716 sender
, flags
, buf
, size
, sent_cb
);
721 queue
= sender
->peer
->incoming_queue
;
723 return qemu_net_queue_send(queue
, sender
, flags
, buf
, size
, sent_cb
);
726 ssize_t
qemu_send_packet_async(NetClientState
*sender
,
727 const uint8_t *buf
, int size
,
728 NetPacketSent
*sent_cb
)
730 return qemu_send_packet_async_with_flags(sender
, QEMU_NET_PACKET_FLAG_NONE
,
734 ssize_t
qemu_send_packet(NetClientState
*nc
, const uint8_t *buf
, int size
)
736 return qemu_send_packet_async(nc
, buf
, size
, NULL
);
739 ssize_t
qemu_receive_packet(NetClientState
*nc
, const uint8_t *buf
, int size
)
741 if (!qemu_can_receive_packet(nc
)) {
745 return qemu_net_queue_receive(nc
->incoming_queue
, buf
, size
);
748 ssize_t
qemu_receive_packet_iov(NetClientState
*nc
, const struct iovec
*iov
,
751 if (!qemu_can_receive_packet(nc
)) {
755 return qemu_net_queue_receive_iov(nc
->incoming_queue
, iov
, iovcnt
);
758 ssize_t
qemu_send_packet_raw(NetClientState
*nc
, const uint8_t *buf
, int size
)
760 return qemu_send_packet_async_with_flags(nc
, QEMU_NET_PACKET_FLAG_RAW
,
764 static ssize_t
nc_sendv_compat(NetClientState
*nc
, const struct iovec
*iov
,
765 int iovcnt
, unsigned flags
)
773 buffer
= iov
[0].iov_base
;
774 offset
= iov
[0].iov_len
;
776 offset
= iov_size(iov
, iovcnt
);
777 if (offset
> NET_BUFSIZE
) {
780 buf
= g_malloc(offset
);
782 offset
= iov_to_buf(iov
, iovcnt
, 0, buf
, offset
);
785 if (flags
& QEMU_NET_PACKET_FLAG_RAW
&& nc
->info
->receive_raw
) {
786 ret
= nc
->info
->receive_raw(nc
, buffer
, offset
);
788 ret
= nc
->info
->receive(nc
, buffer
, offset
);
795 static ssize_t
qemu_deliver_packet_iov(NetClientState
*sender
,
797 const struct iovec
*iov
,
801 MemReentrancyGuard
*owned_reentrancy_guard
;
802 NetClientState
*nc
= opaque
;
807 return iov_size(iov
, iovcnt
);
810 if (nc
->receive_disabled
) {
814 if (nc
->info
->type
!= NET_CLIENT_DRIVER_NIC
||
815 qemu_get_nic(nc
)->reentrancy_guard
->engaged_in_io
) {
816 owned_reentrancy_guard
= NULL
;
818 owned_reentrancy_guard
= qemu_get_nic(nc
)->reentrancy_guard
;
819 owned_reentrancy_guard
->engaged_in_io
= true;
822 if (nc
->info
->receive_iov
&& !(flags
& QEMU_NET_PACKET_FLAG_RAW
)) {
823 ret
= nc
->info
->receive_iov(nc
, iov
, iovcnt
);
825 ret
= nc_sendv_compat(nc
, iov
, iovcnt
, flags
);
828 if (owned_reentrancy_guard
) {
829 owned_reentrancy_guard
->engaged_in_io
= false;
833 nc
->receive_disabled
= 1;
839 ssize_t
qemu_sendv_packet_async(NetClientState
*sender
,
840 const struct iovec
*iov
, int iovcnt
,
841 NetPacketSent
*sent_cb
)
844 size_t size
= iov_size(iov
, iovcnt
);
847 if (size
> NET_BUFSIZE
) {
851 if (sender
->link_down
|| !sender
->peer
) {
855 /* Let filters handle the packet first */
856 ret
= filter_receive_iov(sender
, NET_FILTER_DIRECTION_TX
, sender
,
857 QEMU_NET_PACKET_FLAG_NONE
, iov
, iovcnt
, sent_cb
);
862 ret
= filter_receive_iov(sender
->peer
, NET_FILTER_DIRECTION_RX
, sender
,
863 QEMU_NET_PACKET_FLAG_NONE
, iov
, iovcnt
, sent_cb
);
868 queue
= sender
->peer
->incoming_queue
;
870 return qemu_net_queue_send_iov(queue
, sender
,
871 QEMU_NET_PACKET_FLAG_NONE
,
872 iov
, iovcnt
, sent_cb
);
876 qemu_sendv_packet(NetClientState
*nc
, const struct iovec
*iov
, int iovcnt
)
878 return qemu_sendv_packet_async(nc
, iov
, iovcnt
, NULL
);
881 NetClientState
*qemu_find_netdev(const char *id
)
885 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
886 if (nc
->info
->type
== NET_CLIENT_DRIVER_NIC
)
888 if (!strcmp(nc
->name
, id
)) {
896 int qemu_find_net_clients_except(const char *id
, NetClientState
**ncs
,
897 NetClientDriver type
, int max
)
902 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
903 if (nc
->info
->type
== type
) {
906 if (!id
|| !strcmp(nc
->name
, id
)) {
917 static int nic_get_free_idx(void)
921 for (index
= 0; index
< MAX_NICS
; index
++)
922 if (!nd_table
[index
].used
)
927 GPtrArray
*qemu_get_nic_models(const char *device_type
)
929 GPtrArray
*nic_models
= g_ptr_array_new();
930 GSList
*list
= object_class_get_list_sorted(device_type
, false);
933 DeviceClass
*dc
= OBJECT_CLASS_CHECK(DeviceClass
, list
->data
,
936 if (test_bit(DEVICE_CATEGORY_NETWORK
, dc
->categories
) &&
937 dc
->user_creatable
) {
938 const char *name
= object_class_get_name(list
->data
);
940 * A network device might also be something else than a NIC, see
941 * e.g. the "rocker" device. Thus we have to look for the "netdev"
942 * property, too. Unfortunately, some devices like virtio-net only
943 * create this property during instance_init, so we have to create
944 * a temporary instance here to be able to check it.
946 Object
*obj
= object_new_with_class(OBJECT_CLASS(dc
));
947 if (object_property_find(obj
, "netdev")) {
948 g_ptr_array_add(nic_models
, (gpointer
)name
);
953 g_slist_free_1(list
);
956 g_ptr_array_add(nic_models
, NULL
);
961 static int net_init_nic(const Netdev
*netdev
, const char *name
,
962 NetClientState
*peer
, Error
**errp
)
966 const NetLegacyNicOptions
*nic
;
968 assert(netdev
->type
== NET_CLIENT_DRIVER_NIC
);
969 nic
= &netdev
->u
.nic
;
971 idx
= nic_get_free_idx();
972 if (idx
== -1 || nb_nics
>= MAX_NICS
) {
973 error_setg(errp
, "too many NICs");
979 memset(nd
, 0, sizeof(*nd
));
982 nd
->netdev
= qemu_find_netdev(nic
->netdev
);
984 error_setg(errp
, "netdev '%s' not found", nic
->netdev
);
991 nd
->name
= g_strdup(name
);
993 nd
->model
= g_strdup(nic
->model
);
996 nd
->devaddr
= g_strdup(nic
->addr
);
1000 net_parse_macaddr(nd
->macaddr
.a
, nic
->macaddr
) < 0) {
1001 error_setg(errp
, "invalid syntax for ethernet address");
1005 is_multicast_ether_addr(nd
->macaddr
.a
)) {
1007 "NIC cannot have multicast MAC address (odd 1st byte)");
1010 qemu_macaddr_default_if_unset(&nd
->macaddr
);
1012 if (nic
->has_vectors
) {
1013 if (nic
->vectors
> 0x7ffffff) {
1014 error_setg(errp
, "invalid # of vectors: %"PRIu32
, nic
->vectors
);
1017 nd
->nvectors
= nic
->vectors
;
1019 nd
->nvectors
= DEV_NVECTORS_UNSPECIFIED
;
1028 static gboolean
add_nic_result(gpointer key
, gpointer value
, gpointer user_data
)
1030 GPtrArray
*results
= user_data
;
1031 GPtrArray
*alias_list
= value
;
1032 const char *model
= key
;
1036 result
= g_strdup(model
);
1038 GString
*result_str
= g_string_new(model
);
1041 g_string_append(result_str
, " (aka ");
1042 for (i
= 0; i
< alias_list
->len
; i
++) {
1044 g_string_append(result_str
, ", ");
1046 g_string_append(result_str
, alias_list
->pdata
[i
]);
1048 g_string_append(result_str
, ")");
1049 result
= result_str
->str
;
1050 g_string_free(result_str
, false);
1051 g_ptr_array_unref(alias_list
);
1053 g_ptr_array_add(results
, result
);
1057 static int model_cmp(char **a
, char **b
)
1059 return strcmp(*a
, *b
);
1062 static void show_nic_models(void)
1064 GPtrArray
*results
= g_ptr_array_new();
1067 g_hash_table_foreach_remove(nic_model_help
, add_nic_result
, results
);
1068 g_ptr_array_sort(results
, (GCompareFunc
)model_cmp
);
1070 printf("Available NIC models for this configuration:\n");
1071 for (i
= 0 ; i
< results
->len
; i
++) {
1072 printf("%s\n", (char *)results
->pdata
[i
]);
1074 g_hash_table_unref(nic_model_help
);
1075 nic_model_help
= NULL
;
1078 static void add_nic_model_help(const char *model
, const char *alias
)
1080 GPtrArray
*alias_list
= NULL
;
1082 if (g_hash_table_lookup_extended(nic_model_help
, model
, NULL
,
1083 (gpointer
*)&alias_list
)) {
1084 /* Already exists, no alias to add: return */
1089 /* Check if this alias is already in the list. Add if not. */
1090 if (!g_ptr_array_find_with_equal_func(alias_list
, alias
,
1091 g_str_equal
, NULL
)) {
1092 g_ptr_array_add(alias_list
, g_strdup(alias
));
1097 /* Either this model wasn't in the list already, or a first alias added */
1099 alias_list
= g_ptr_array_new();
1100 g_ptr_array_set_free_func(alias_list
, g_free
);
1101 g_ptr_array_add(alias_list
, g_strdup(alias
));
1103 g_hash_table_replace(nic_model_help
, g_strdup(model
), alias_list
);
1106 NICInfo
*qemu_find_nic_info(const char *typename
, bool match_default
,
1112 if (nic_model_help
) {
1113 add_nic_model_help(typename
, alias
);
1116 for (i
= 0; i
< nb_nics
; i
++) {
1119 if (!nd
->used
|| nd
->instantiated
) {
1123 if ((match_default
&& !nd
->model
) || !g_strcmp0(nd
->model
, typename
)
1124 || (alias
&& !g_strcmp0(nd
->model
, alias
))) {
1132 /* "I have created a device. Please configure it if you can" */
1133 bool qemu_configure_nic_device(DeviceState
*dev
, bool match_default
,
1136 NICInfo
*nd
= qemu_find_nic_info(object_get_typename(OBJECT(dev
)),
1137 match_default
, alias
);
1140 qdev_set_nic_properties(dev
, nd
);
1146 /* "Please create a device, if you have a configuration for it" */
1147 DeviceState
*qemu_create_nic_device(const char *typename
, bool match_default
,
1150 NICInfo
*nd
= qemu_find_nic_info(typename
, match_default
, alias
);
1157 dev
= qdev_new(typename
);
1158 qdev_set_nic_properties(dev
, nd
);
1162 void qemu_create_nic_bus_devices(BusState
*bus
, const char *parent_type
,
1163 const char *default_model
,
1164 const char *alias
, const char *alias_target
)
1166 GPtrArray
*nic_models
= qemu_get_nic_models(parent_type
);
1172 if (nic_model_help
) {
1174 add_nic_model_help(alias_target
, alias
);
1176 for (i
= 0; i
< nic_models
->len
- 1; i
++) {
1177 add_nic_model_help(nic_models
->pdata
[i
], NULL
);
1181 /* Drop the NULL terminator which would make g_str_equal() unhappy */
1184 for (i
= 0; i
< nb_nics
; i
++) {
1187 if (!nd
->used
|| nd
->instantiated
) {
1191 model
= nd
->model
? nd
->model
: default_model
;
1196 /* Each bus type is allowed *one* substitution */
1197 if (g_str_equal(model
, alias
)) {
1198 model
= alias_target
;
1201 if (!g_ptr_array_find_with_equal_func(nic_models
, model
,
1202 g_str_equal
, NULL
)) {
1203 /* This NIC does not live on this bus. */
1207 dev
= qdev_new(model
);
1208 qdev_set_nic_properties(dev
, nd
);
1209 qdev_realize_and_unref(dev
, bus
, &error_fatal
);
1212 g_ptr_array_free(nic_models
, true);
1215 static int (* const net_client_init_fun
[NET_CLIENT_DRIVER__MAX
])(
1216 const Netdev
*netdev
,
1218 NetClientState
*peer
, Error
**errp
) = {
1219 [NET_CLIENT_DRIVER_NIC
] = net_init_nic
,
1221 [NET_CLIENT_DRIVER_USER
] = net_init_slirp
,
1223 [NET_CLIENT_DRIVER_TAP
] = net_init_tap
,
1224 [NET_CLIENT_DRIVER_SOCKET
] = net_init_socket
,
1225 [NET_CLIENT_DRIVER_STREAM
] = net_init_stream
,
1226 [NET_CLIENT_DRIVER_DGRAM
] = net_init_dgram
,
1228 [NET_CLIENT_DRIVER_VDE
] = net_init_vde
,
1230 #ifdef CONFIG_NETMAP
1231 [NET_CLIENT_DRIVER_NETMAP
] = net_init_netmap
,
1233 #ifdef CONFIG_AF_XDP
1234 [NET_CLIENT_DRIVER_AF_XDP
] = net_init_af_xdp
,
1236 #ifdef CONFIG_NET_BRIDGE
1237 [NET_CLIENT_DRIVER_BRIDGE
] = net_init_bridge
,
1239 [NET_CLIENT_DRIVER_HUBPORT
] = net_init_hubport
,
1240 #ifdef CONFIG_VHOST_NET_USER
1241 [NET_CLIENT_DRIVER_VHOST_USER
] = net_init_vhost_user
,
1243 #ifdef CONFIG_VHOST_NET_VDPA
1244 [NET_CLIENT_DRIVER_VHOST_VDPA
] = net_init_vhost_vdpa
,
1246 #ifdef CONFIG_L2TPV3
1247 [NET_CLIENT_DRIVER_L2TPV3
] = net_init_l2tpv3
,
1250 [NET_CLIENT_DRIVER_VMNET_HOST
] = net_init_vmnet_host
,
1251 [NET_CLIENT_DRIVER_VMNET_SHARED
] = net_init_vmnet_shared
,
1252 [NET_CLIENT_DRIVER_VMNET_BRIDGED
] = net_init_vmnet_bridged
,
1253 #endif /* CONFIG_VMNET */
1257 static int net_client_init1(const Netdev
*netdev
, bool is_netdev
, Error
**errp
)
1259 NetClientState
*peer
= NULL
;
1263 if (netdev
->type
== NET_CLIENT_DRIVER_NIC
||
1264 !net_client_init_fun
[netdev
->type
]) {
1265 error_setg(errp
, "network backend '%s' is not compiled into this binary",
1266 NetClientDriver_str(netdev
->type
));
1270 if (netdev
->type
== NET_CLIENT_DRIVER_NONE
) {
1271 return 0; /* nothing to do */
1273 if (netdev
->type
== NET_CLIENT_DRIVER_HUBPORT
) {
1274 error_setg(errp
, "network backend '%s' is only supported with -netdev/-nic",
1275 NetClientDriver_str(netdev
->type
));
1279 if (!net_client_init_fun
[netdev
->type
]) {
1280 error_setg(errp
, "network backend '%s' is not compiled into this binary",
1281 NetClientDriver_str(netdev
->type
));
1285 /* Do not add to a hub if it's a nic with a netdev= parameter. */
1286 if (netdev
->type
!= NET_CLIENT_DRIVER_NIC
||
1287 !netdev
->u
.nic
.netdev
) {
1288 peer
= net_hub_add_port(0, NULL
, NULL
);
1292 nc
= qemu_find_netdev(netdev
->id
);
1294 error_setg(errp
, "Duplicate ID '%s'", netdev
->id
);
1298 if (net_client_init_fun
[netdev
->type
](netdev
, netdev
->id
, peer
, errp
) < 0) {
1299 /* FIXME drop when all init functions store an Error */
1300 if (errp
&& !*errp
) {
1301 error_setg(errp
, "Device '%s' could not be initialized",
1302 NetClientDriver_str(netdev
->type
));
1308 nc
= qemu_find_netdev(netdev
->id
);
1310 nc
->is_netdev
= true;
1316 void show_netdevs(void)
1319 const char *available_netdevs
[] = {
1328 #ifdef CONFIG_L2TPV3
1334 #ifdef CONFIG_NET_BRIDGE
1337 #ifdef CONFIG_NETMAP
1340 #ifdef CONFIG_AF_XDP
1346 #ifdef CONFIG_VHOST_VDPA
1356 qemu_printf("Available netdev backend types:\n");
1357 for (idx
= 0; idx
< ARRAY_SIZE(available_netdevs
); idx
++) {
1358 qemu_printf("%s\n", available_netdevs
[idx
]);
1362 static int net_client_init(QemuOpts
*opts
, bool is_netdev
, Error
**errp
)
1364 gchar
**substrings
= NULL
;
1365 Netdev
*object
= NULL
;
1367 Visitor
*v
= opts_visitor_new(opts
);
1369 /* Parse convenience option format ipv6-net=fec0::0[/64] */
1370 const char *ip6_net
= qemu_opt_get(opts
, "ipv6-net");
1374 unsigned long prefix_len
= 64; /* Default 64bit prefix length. */
1376 substrings
= g_strsplit(ip6_net
, "/", 2);
1377 if (!substrings
|| !substrings
[0]) {
1378 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "ipv6-net",
1379 "a valid IPv6 prefix");
1383 prefix_addr
= substrings
[0];
1385 /* Handle user-specified prefix length. */
1386 if (substrings
[1] &&
1387 qemu_strtoul(substrings
[1], NULL
, 10, &prefix_len
))
1390 "parameter 'ipv6-net' expects a number after '/'");
1394 qemu_opt_set(opts
, "ipv6-prefix", prefix_addr
, &error_abort
);
1395 qemu_opt_set_number(opts
, "ipv6-prefixlen", prefix_len
,
1397 qemu_opt_unset(opts
, "ipv6-net");
1400 /* Create an ID for -net if the user did not specify one */
1401 if (!is_netdev
&& !qemu_opts_id(opts
)) {
1402 qemu_opts_set_id(opts
, id_generate(ID_NET
));
1405 if (visit_type_Netdev(v
, NULL
, &object
, errp
)) {
1406 ret
= net_client_init1(object
, is_netdev
, errp
);
1409 qapi_free_Netdev(object
);
1412 g_strfreev(substrings
);
1417 void netdev_add(QemuOpts
*opts
, Error
**errp
)
1419 net_client_init(opts
, true, errp
);
1422 void qmp_netdev_add(Netdev
*netdev
, Error
**errp
)
1424 if (!id_wellformed(netdev
->id
)) {
1425 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "id", "an identifier");
1429 net_client_init1(netdev
, true, errp
);
1432 void qmp_netdev_del(const char *id
, Error
**errp
)
1437 nc
= qemu_find_netdev(id
);
1439 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1440 "Device '%s' not found", id
);
1444 if (!nc
->is_netdev
) {
1445 error_setg(errp
, "Device '%s' is not a netdev", id
);
1449 qemu_del_net_client(nc
);
1452 * Wart: we need to delete the QemuOpts associated with netdevs
1453 * created via CLI or HMP, to avoid bogus "Duplicate ID" errors in
1456 opts
= qemu_opts_find(qemu_find_opts("netdev"), id
);
1458 qemu_opts_del(opts
);
1462 static void netfilter_print_info(Monitor
*mon
, NetFilterState
*nf
)
1465 ObjectProperty
*prop
;
1466 ObjectPropertyIterator iter
;
1469 /* generate info str */
1470 object_property_iter_init(&iter
, OBJECT(nf
));
1471 while ((prop
= object_property_iter_next(&iter
))) {
1472 if (!strcmp(prop
->name
, "type")) {
1475 v
= string_output_visitor_new(false, &str
);
1476 object_property_get(OBJECT(nf
), prop
->name
, v
, NULL
);
1477 visit_complete(v
, &str
);
1479 monitor_printf(mon
, ",%s=%s", prop
->name
, str
);
1482 monitor_printf(mon
, "\n");
1485 void print_net_client(Monitor
*mon
, NetClientState
*nc
)
1489 monitor_printf(mon
, "%s: index=%d,type=%s,%s\n", nc
->name
,
1491 NetClientDriver_str(nc
->info
->type
),
1493 if (!QTAILQ_EMPTY(&nc
->filters
)) {
1494 monitor_printf(mon
, "filters:\n");
1496 QTAILQ_FOREACH(nf
, &nc
->filters
, next
) {
1497 monitor_printf(mon
, " - %s: type=%s",
1498 object_get_canonical_path_component(OBJECT(nf
)),
1499 object_get_typename(OBJECT(nf
)));
1500 netfilter_print_info(mon
, nf
);
1504 RxFilterInfoList
*qmp_query_rx_filter(const char *name
, Error
**errp
)
1507 RxFilterInfoList
*filter_list
= NULL
, **tail
= &filter_list
;
1509 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
1512 if (name
&& strcmp(nc
->name
, name
) != 0) {
1516 /* only query rx-filter information of NIC */
1517 if (nc
->info
->type
!= NET_CLIENT_DRIVER_NIC
) {
1519 error_setg(errp
, "net client(%s) isn't a NIC", name
);
1520 assert(!filter_list
);
1526 /* only query information on queue 0 since the info is per nic,
1529 if (nc
->queue_index
!= 0)
1532 if (nc
->info
->query_rx_filter
) {
1533 info
= nc
->info
->query_rx_filter(nc
);
1534 QAPI_LIST_APPEND(tail
, info
);
1536 error_setg(errp
, "net client(%s) doesn't support"
1537 " rx-filter querying", name
);
1538 assert(!filter_list
);
1547 if (filter_list
== NULL
&& name
) {
1548 error_setg(errp
, "invalid net client name: %s", name
);
1554 void colo_notify_filters_event(int event
, Error
**errp
)
1558 NetFilterClass
*nfc
= NULL
;
1559 Error
*local_err
= NULL
;
1561 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
1562 QTAILQ_FOREACH(nf
, &nc
->filters
, next
) {
1563 nfc
= NETFILTER_GET_CLASS(OBJECT(nf
));
1564 nfc
->handle_event(nf
, event
, &local_err
);
1566 error_propagate(errp
, local_err
);
1573 void qmp_set_link(const char *name
, bool up
, Error
**errp
)
1575 NetClientState
*ncs
[MAX_QUEUE_NUM
];
1579 queues
= qemu_find_net_clients_except(name
, ncs
,
1580 NET_CLIENT_DRIVER__MAX
,
1584 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1585 "Device '%s' not found", name
);
1590 for (i
= 0; i
< queues
; i
++) {
1591 ncs
[i
]->link_down
= !up
;
1594 if (nc
->info
->link_status_changed
) {
1595 nc
->info
->link_status_changed(nc
);
1599 /* Change peer link only if the peer is NIC and then notify peer.
1600 * If the peer is a HUBPORT or a backend, we do not change the
1603 * This behavior is compatible with qemu hubs where there could be
1604 * multiple clients that can still communicate with each other in
1605 * disconnected mode. For now maintain this compatibility.
1607 if (nc
->peer
->info
->type
== NET_CLIENT_DRIVER_NIC
) {
1608 for (i
= 0; i
< queues
; i
++) {
1609 ncs
[i
]->peer
->link_down
= !up
;
1612 if (nc
->peer
->info
->link_status_changed
) {
1613 nc
->peer
->info
->link_status_changed(nc
->peer
);
1618 static void net_vm_change_state_handler(void *opaque
, bool running
,
1622 NetClientState
*tmp
;
1624 QTAILQ_FOREACH_SAFE(nc
, &net_clients
, next
, tmp
) {
1626 /* Flush queued packets and wake up backends. */
1627 if (nc
->peer
&& qemu_can_send_packet(nc
)) {
1628 qemu_flush_queued_packets(nc
->peer
);
1631 /* Complete all queued packets, to guarantee we don't modify
1632 * state later when VM is not running.
1634 qemu_flush_or_purge_queued_packets(nc
, true);
1639 void net_cleanup(void)
1641 NetClientState
*nc
, **p
= &QTAILQ_FIRST(&net_clients
);
1643 /*cleanup colo compare module for COLO*/
1644 colo_compare_cleanup();
1647 * Walk the net_clients list and remove the netdevs but *not* any
1648 * NET_CLIENT_DRIVER_NIC entries. The latter are owned by the device
1649 * model which created them, and in some cases (e.g. xen-net-device)
1650 * the device itself may do cleanup at exit and will be upset if we
1651 * just delete its NIC from underneath it.
1653 * Since qemu_del_net_client() may delete multiple entries, using
1654 * QTAILQ_FOREACH_SAFE() is not safe here. The only safe pointer
1655 * to keep as a bookmark is a NET_CLIENT_DRIVER_NIC entry, so keep
1656 * 'p' pointing to either the head of the list, or the 'next' field
1657 * of the latest NET_CLIENT_DRIVER_NIC, and operate on *p as we walk
1660 * The 'nc' variable isn't part of the list traversal; it's purely
1661 * for convenience as too much '(*p)->' has a tendency to make the
1662 * readers' eyes bleed.
1666 if (nc
->info
->type
== NET_CLIENT_DRIVER_NIC
) {
1667 /* Skip NET_CLIENT_DRIVER_NIC entries */
1668 p
= &QTAILQ_NEXT(nc
, next
);
1670 qemu_del_net_client(nc
);
1674 qemu_del_vm_change_state_handler(net_change_state_entry
);
1677 void net_check_clients(void)
1682 if (nic_model_help
) {
1686 net_hub_check_clients();
1688 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
1690 warn_report("%s %s has no peer",
1691 nc
->info
->type
== NET_CLIENT_DRIVER_NIC
1697 /* Check that all NICs requested via -net nic actually got created.
1698 * NICs created via -device don't need to be checked here because
1699 * they are always instantiated.
1701 for (i
= 0; i
< MAX_NICS
; i
++) {
1702 NICInfo
*nd
= &nd_table
[i
];
1703 if (nd
->used
&& !nd
->instantiated
) {
1704 warn_report("requested NIC (%s, model %s) "
1705 "was not created (not supported by this machine?)",
1706 nd
->name
? nd
->name
: "anonymous",
1707 nd
->model
? nd
->model
: "unspecified");
1712 static int net_init_client(void *dummy
, QemuOpts
*opts
, Error
**errp
)
1714 return net_client_init(opts
, false, errp
);
1717 static int net_init_netdev(void *dummy
, QemuOpts
*opts
, Error
**errp
)
1719 const char *type
= qemu_opt_get(opts
, "type");
1721 if (type
&& is_help_option(type
)) {
1725 return net_client_init(opts
, true, errp
);
1728 /* For the convenience "--nic" parameter */
1729 static int net_param_nic(void *dummy
, QemuOpts
*opts
, Error
**errp
)
1736 type
= qemu_opt_get(opts
, "type");
1738 if (g_str_equal(type
, "none")) {
1739 return 0; /* Nothing to do, default_net is cleared in vl.c */
1741 if (is_help_option(type
)) {
1742 GPtrArray
*nic_models
= qemu_get_nic_models(TYPE_DEVICE
);
1746 printf("Available NIC models "
1747 "(use -nic model=help for a filtered list):\n");
1748 for (i
= 0 ; nic_models
->pdata
[i
]; i
++) {
1749 printf("%s\n", (char *)nic_models
->pdata
[i
]);
1751 g_ptr_array_free(nic_models
, true);
1756 idx
= nic_get_free_idx();
1757 if (idx
== -1 || nb_nics
>= MAX_NICS
) {
1758 error_setg(errp
, "no more on-board/default NIC slots available");
1763 qemu_opt_set(opts
, "type", "user", &error_abort
);
1766 ni
= &nd_table
[idx
];
1767 memset(ni
, 0, sizeof(*ni
));
1768 ni
->model
= qemu_opt_get_del(opts
, "model");
1770 if (!nic_model_help
&& !g_strcmp0(ni
->model
, "help")) {
1771 nic_model_help
= g_hash_table_new_full(g_str_hash
, g_str_equal
,
1776 /* Create an ID if the user did not specify one */
1777 nd_id
= g_strdup(qemu_opts_id(opts
));
1779 nd_id
= id_generate(ID_NET
);
1780 qemu_opts_set_id(opts
, nd_id
);
1783 /* Handle MAC address */
1784 mac
= qemu_opt_get_del(opts
, "mac");
1786 ret
= net_parse_macaddr(ni
->macaddr
.a
, mac
);
1789 error_setg(errp
, "invalid syntax for ethernet address");
1792 if (is_multicast_ether_addr(ni
->macaddr
.a
)) {
1793 error_setg(errp
, "NIC cannot have multicast MAC address");
1798 qemu_macaddr_default_if_unset(&ni
->macaddr
);
1800 ret
= net_client_init(opts
, true, errp
);
1802 ni
->netdev
= qemu_find_netdev(nd_id
);
1812 static void netdev_init_modern(void)
1814 while (!QSIMPLEQ_EMPTY(&nd_queue
)) {
1815 NetdevQueueEntry
*nd
= QSIMPLEQ_FIRST(&nd_queue
);
1817 QSIMPLEQ_REMOVE_HEAD(&nd_queue
, entry
);
1818 loc_push_restore(&nd
->loc
);
1819 net_client_init1(nd
->nd
, true, &error_fatal
);
1821 qapi_free_Netdev(nd
->nd
);
1826 void net_init_clients(void)
1828 net_change_state_entry
=
1829 qemu_add_vm_change_state_handler(net_vm_change_state_handler
, NULL
);
1831 QTAILQ_INIT(&net_clients
);
1833 netdev_init_modern();
1835 qemu_opts_foreach(qemu_find_opts("netdev"), net_init_netdev
, NULL
,
1838 qemu_opts_foreach(qemu_find_opts("nic"), net_param_nic
, NULL
,
1841 qemu_opts_foreach(qemu_find_opts("net"), net_init_client
, NULL
,
1846 * Does this -netdev argument use modern rather than traditional syntax?
1847 * Modern syntax is to be parsed with netdev_parse_modern().
1848 * Traditional syntax is to be parsed with net_client_parse().
1850 bool netdev_is_modern(const char *optstr
)
1855 static QemuOptsList dummy_opts
= {
1857 .implied_opt_name
= "type",
1858 .head
= QTAILQ_HEAD_INITIALIZER(dummy_opts
.head
),
1862 if (optstr
[0] == '{') {
1863 /* This is JSON, which means it's modern syntax */
1867 opts
= qemu_opts_create(&dummy_opts
, NULL
, false, &error_abort
);
1868 qemu_opts_do_parse(opts
, optstr
, dummy_opts
.implied_opt_name
,
1870 type
= qemu_opt_get(opts
, "type");
1871 is_modern
= !g_strcmp0(type
, "stream") || !g_strcmp0(type
, "dgram");
1873 qemu_opts_reset(&dummy_opts
);
1879 * netdev_parse_modern() uses modern, more expressive syntax than
1880 * net_client_parse(), but supports only the -netdev option.
1881 * netdev_parse_modern() appends to @nd_queue, whereas net_client_parse()
1882 * appends to @qemu_netdev_opts.
1884 void netdev_parse_modern(const char *optstr
)
1887 NetdevQueueEntry
*nd
;
1889 v
= qobject_input_visitor_new_str(optstr
, "type", &error_fatal
);
1890 nd
= g_new(NetdevQueueEntry
, 1);
1891 visit_type_Netdev(v
, NULL
, &nd
->nd
, &error_fatal
);
1895 QSIMPLEQ_INSERT_TAIL(&nd_queue
, nd
, entry
);
1898 void net_client_parse(QemuOptsList
*opts_list
, const char *optstr
)
1900 if (!qemu_opts_parse_noisily(opts_list
, optstr
, true)) {
1907 uint32_t net_crc32(const uint8_t *p
, int len
)
1914 for (i
= 0; i
< len
; i
++) {
1916 for (j
= 0; j
< 8; j
++) {
1917 carry
= ((crc
& 0x80000000L
) ? 1 : 0) ^ (b
& 0x01);
1921 crc
= ((crc
^ POLYNOMIAL_BE
) | carry
);
1929 uint32_t net_crc32_le(const uint8_t *p
, int len
)
1936 for (i
= 0; i
< len
; i
++) {
1938 for (j
= 0; j
< 8; j
++) {
1939 carry
= (crc
& 0x1) ^ (b
& 0x01);
1943 crc
^= POLYNOMIAL_LE
;
1951 QemuOptsList qemu_netdev_opts
= {
1953 .implied_opt_name
= "type",
1954 .head
= QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts
.head
),
1957 * no elements => accept any params
1958 * validation will happen later
1960 { /* end of list */ }
1964 QemuOptsList qemu_nic_opts
= {
1966 .implied_opt_name
= "type",
1967 .head
= QTAILQ_HEAD_INITIALIZER(qemu_nic_opts
.head
),
1970 * no elements => accept any params
1971 * validation will happen later
1973 { /* end of list */ }
1977 QemuOptsList qemu_net_opts
= {
1979 .implied_opt_name
= "type",
1980 .head
= QTAILQ_HEAD_INITIALIZER(qemu_net_opts
.head
),
1983 * no elements => accept any params
1984 * validation will happen later
1986 { /* end of list */ }
1990 void net_socket_rs_init(SocketReadState
*rs
,
1991 SocketReadStateFinalize
*finalize
,
1995 rs
->vnet_hdr
= vnet_hdr
;
1998 rs
->vnet_hdr_len
= 0;
1999 memset(rs
->buf
, 0, sizeof(rs
->buf
));
2000 rs
->finalize
= finalize
;
2008 int net_fill_rstate(SocketReadState
*rs
, const uint8_t *buf
, int size
)
2013 /* Reassemble a packet from the network.
2014 * 0 = getting length.
2015 * 1 = getting vnet header length.
2018 switch (rs
->state
) {
2024 memcpy(rs
->buf
+ rs
->index
, buf
, l
);
2028 if (rs
->index
== 4) {
2030 rs
->packet_len
= ntohl(*(uint32_t *)rs
->buf
);
2036 rs
->vnet_hdr_len
= 0;
2045 memcpy(rs
->buf
+ rs
->index
, buf
, l
);
2049 if (rs
->index
== 4) {
2050 /* got vnet header length */
2051 rs
->vnet_hdr_len
= ntohl(*(uint32_t *)rs
->buf
);
2057 l
= rs
->packet_len
- rs
->index
;
2061 if (rs
->index
+ l
<= sizeof(rs
->buf
)) {
2062 memcpy(rs
->buf
+ rs
->index
, buf
, l
);
2064 fprintf(stderr
, "serious error: oversized packet received,"
2065 "connection terminated.\n");
2066 rs
->index
= rs
->state
= 0;
2073 if (rs
->index
>= rs
->packet_len
) {
2076 assert(rs
->finalize
);