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 bool qemu_get_using_vnet_hdr(NetClientState
*nc
)
534 if (!nc
|| !nc
->info
->get_using_vnet_hdr
) {
538 return nc
->info
->get_using_vnet_hdr(nc
);
541 void qemu_using_vnet_hdr(NetClientState
*nc
, bool enable
)
543 if (!nc
|| !nc
->info
->using_vnet_hdr
) {
547 nc
->info
->using_vnet_hdr(nc
, enable
);
550 void qemu_set_offload(NetClientState
*nc
, int csum
, int tso4
, int tso6
,
551 int ecn
, int ufo
, int uso4
, int uso6
)
553 if (!nc
|| !nc
->info
->set_offload
) {
557 nc
->info
->set_offload(nc
, csum
, tso4
, tso6
, ecn
, ufo
, uso4
, uso6
);
560 int qemu_get_vnet_hdr_len(NetClientState
*nc
)
562 if (!nc
|| !nc
->info
->get_vnet_hdr_len
) {
566 return nc
->info
->get_vnet_hdr_len(nc
);
569 void qemu_set_vnet_hdr_len(NetClientState
*nc
, int len
)
571 if (!nc
|| !nc
->info
->set_vnet_hdr_len
) {
575 nc
->vnet_hdr_len
= len
;
576 nc
->info
->set_vnet_hdr_len(nc
, len
);
579 int qemu_set_vnet_le(NetClientState
*nc
, bool is_le
)
582 if (!nc
|| !nc
->info
->set_vnet_le
) {
586 return nc
->info
->set_vnet_le(nc
, is_le
);
592 int qemu_set_vnet_be(NetClientState
*nc
, bool is_be
)
597 if (!nc
|| !nc
->info
->set_vnet_be
) {
601 return nc
->info
->set_vnet_be(nc
, is_be
);
605 int qemu_can_receive_packet(NetClientState
*nc
)
607 if (nc
->receive_disabled
) {
609 } else if (nc
->info
->can_receive
&&
610 !nc
->info
->can_receive(nc
)) {
616 int qemu_can_send_packet(NetClientState
*sender
)
618 int vm_running
= runstate_is_running();
628 return qemu_can_receive_packet(sender
->peer
);
631 static ssize_t
filter_receive_iov(NetClientState
*nc
,
632 NetFilterDirection direction
,
633 NetClientState
*sender
,
635 const struct iovec
*iov
,
637 NetPacketSent
*sent_cb
)
640 NetFilterState
*nf
= NULL
;
642 if (direction
== NET_FILTER_DIRECTION_TX
) {
643 QTAILQ_FOREACH(nf
, &nc
->filters
, next
) {
644 ret
= qemu_netfilter_receive(nf
, direction
, sender
, flags
, iov
,
651 QTAILQ_FOREACH_REVERSE(nf
, &nc
->filters
, next
) {
652 ret
= qemu_netfilter_receive(nf
, direction
, sender
, flags
, iov
,
663 static ssize_t
filter_receive(NetClientState
*nc
,
664 NetFilterDirection direction
,
665 NetClientState
*sender
,
669 NetPacketSent
*sent_cb
)
672 .iov_base
= (void *)data
,
676 return filter_receive_iov(nc
, direction
, sender
, flags
, &iov
, 1, sent_cb
);
679 void qemu_purge_queued_packets(NetClientState
*nc
)
685 qemu_net_queue_purge(nc
->peer
->incoming_queue
, nc
);
688 void qemu_flush_or_purge_queued_packets(NetClientState
*nc
, bool purge
)
690 nc
->receive_disabled
= 0;
692 if (nc
->peer
&& nc
->peer
->info
->type
== NET_CLIENT_DRIVER_HUBPORT
) {
693 if (net_hub_flush(nc
->peer
)) {
697 if (qemu_net_queue_flush(nc
->incoming_queue
)) {
698 /* We emptied the queue successfully, signal to the IO thread to repoll
699 * the file descriptor (for tap, for example).
703 /* Unable to empty the queue, purge remaining packets */
704 qemu_net_queue_purge(nc
->incoming_queue
, nc
->peer
);
708 void qemu_flush_queued_packets(NetClientState
*nc
)
710 qemu_flush_or_purge_queued_packets(nc
, false);
713 static ssize_t
qemu_send_packet_async_with_flags(NetClientState
*sender
,
715 const uint8_t *buf
, int size
,
716 NetPacketSent
*sent_cb
)
722 printf("qemu_send_packet_async:\n");
723 qemu_hexdump(stdout
, "net", buf
, size
);
726 if (sender
->link_down
|| !sender
->peer
) {
730 /* Let filters handle the packet first */
731 ret
= filter_receive(sender
, NET_FILTER_DIRECTION_TX
,
732 sender
, flags
, buf
, size
, sent_cb
);
737 ret
= filter_receive(sender
->peer
, NET_FILTER_DIRECTION_RX
,
738 sender
, flags
, buf
, size
, sent_cb
);
743 queue
= sender
->peer
->incoming_queue
;
745 return qemu_net_queue_send(queue
, sender
, flags
, buf
, size
, sent_cb
);
748 ssize_t
qemu_send_packet_async(NetClientState
*sender
,
749 const uint8_t *buf
, int size
,
750 NetPacketSent
*sent_cb
)
752 return qemu_send_packet_async_with_flags(sender
, QEMU_NET_PACKET_FLAG_NONE
,
756 ssize_t
qemu_send_packet(NetClientState
*nc
, const uint8_t *buf
, int size
)
758 return qemu_send_packet_async(nc
, buf
, size
, NULL
);
761 ssize_t
qemu_receive_packet(NetClientState
*nc
, const uint8_t *buf
, int size
)
763 if (!qemu_can_receive_packet(nc
)) {
767 return qemu_net_queue_receive(nc
->incoming_queue
, buf
, size
);
770 ssize_t
qemu_receive_packet_iov(NetClientState
*nc
, const struct iovec
*iov
,
773 if (!qemu_can_receive_packet(nc
)) {
777 return qemu_net_queue_receive_iov(nc
->incoming_queue
, iov
, iovcnt
);
780 ssize_t
qemu_send_packet_raw(NetClientState
*nc
, const uint8_t *buf
, int size
)
782 return qemu_send_packet_async_with_flags(nc
, QEMU_NET_PACKET_FLAG_RAW
,
786 static ssize_t
nc_sendv_compat(NetClientState
*nc
, const struct iovec
*iov
,
787 int iovcnt
, unsigned flags
)
795 buffer
= iov
[0].iov_base
;
796 offset
= iov
[0].iov_len
;
798 offset
= iov_size(iov
, iovcnt
);
799 if (offset
> NET_BUFSIZE
) {
802 buf
= g_malloc(offset
);
804 offset
= iov_to_buf(iov
, iovcnt
, 0, buf
, offset
);
807 if (flags
& QEMU_NET_PACKET_FLAG_RAW
&& nc
->info
->receive_raw
) {
808 ret
= nc
->info
->receive_raw(nc
, buffer
, offset
);
810 ret
= nc
->info
->receive(nc
, buffer
, offset
);
817 static ssize_t
qemu_deliver_packet_iov(NetClientState
*sender
,
819 const struct iovec
*iov
,
823 MemReentrancyGuard
*owned_reentrancy_guard
;
824 NetClientState
*nc
= opaque
;
829 return iov_size(iov
, iovcnt
);
832 if (nc
->receive_disabled
) {
836 if (nc
->info
->type
!= NET_CLIENT_DRIVER_NIC
||
837 qemu_get_nic(nc
)->reentrancy_guard
->engaged_in_io
) {
838 owned_reentrancy_guard
= NULL
;
840 owned_reentrancy_guard
= qemu_get_nic(nc
)->reentrancy_guard
;
841 owned_reentrancy_guard
->engaged_in_io
= true;
844 if (nc
->info
->receive_iov
&& !(flags
& QEMU_NET_PACKET_FLAG_RAW
)) {
845 ret
= nc
->info
->receive_iov(nc
, iov
, iovcnt
);
847 ret
= nc_sendv_compat(nc
, iov
, iovcnt
, flags
);
850 if (owned_reentrancy_guard
) {
851 owned_reentrancy_guard
->engaged_in_io
= false;
855 nc
->receive_disabled
= 1;
861 ssize_t
qemu_sendv_packet_async(NetClientState
*sender
,
862 const struct iovec
*iov
, int iovcnt
,
863 NetPacketSent
*sent_cb
)
866 size_t size
= iov_size(iov
, iovcnt
);
869 if (size
> NET_BUFSIZE
) {
873 if (sender
->link_down
|| !sender
->peer
) {
877 /* Let filters handle the packet first */
878 ret
= filter_receive_iov(sender
, NET_FILTER_DIRECTION_TX
, sender
,
879 QEMU_NET_PACKET_FLAG_NONE
, iov
, iovcnt
, sent_cb
);
884 ret
= filter_receive_iov(sender
->peer
, NET_FILTER_DIRECTION_RX
, sender
,
885 QEMU_NET_PACKET_FLAG_NONE
, iov
, iovcnt
, sent_cb
);
890 queue
= sender
->peer
->incoming_queue
;
892 return qemu_net_queue_send_iov(queue
, sender
,
893 QEMU_NET_PACKET_FLAG_NONE
,
894 iov
, iovcnt
, sent_cb
);
898 qemu_sendv_packet(NetClientState
*nc
, const struct iovec
*iov
, int iovcnt
)
900 return qemu_sendv_packet_async(nc
, iov
, iovcnt
, NULL
);
903 NetClientState
*qemu_find_netdev(const char *id
)
907 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
908 if (nc
->info
->type
== NET_CLIENT_DRIVER_NIC
)
910 if (!strcmp(nc
->name
, id
)) {
918 int qemu_find_net_clients_except(const char *id
, NetClientState
**ncs
,
919 NetClientDriver type
, int max
)
924 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
925 if (nc
->info
->type
== type
) {
928 if (!id
|| !strcmp(nc
->name
, id
)) {
939 static int nic_get_free_idx(void)
943 for (index
= 0; index
< MAX_NICS
; index
++)
944 if (!nd_table
[index
].used
)
949 GPtrArray
*qemu_get_nic_models(const char *device_type
)
951 GPtrArray
*nic_models
= g_ptr_array_new();
952 GSList
*list
= object_class_get_list_sorted(device_type
, false);
955 DeviceClass
*dc
= OBJECT_CLASS_CHECK(DeviceClass
, list
->data
,
958 if (test_bit(DEVICE_CATEGORY_NETWORK
, dc
->categories
) &&
959 dc
->user_creatable
) {
960 const char *name
= object_class_get_name(list
->data
);
962 * A network device might also be something else than a NIC, see
963 * e.g. the "rocker" device. Thus we have to look for the "netdev"
964 * property, too. Unfortunately, some devices like virtio-net only
965 * create this property during instance_init, so we have to create
966 * a temporary instance here to be able to check it.
968 Object
*obj
= object_new_with_class(OBJECT_CLASS(dc
));
969 if (object_property_find(obj
, "netdev")) {
970 g_ptr_array_add(nic_models
, (gpointer
)name
);
975 g_slist_free_1(list
);
978 g_ptr_array_add(nic_models
, NULL
);
983 static int net_init_nic(const Netdev
*netdev
, const char *name
,
984 NetClientState
*peer
, Error
**errp
)
988 const NetLegacyNicOptions
*nic
;
990 assert(netdev
->type
== NET_CLIENT_DRIVER_NIC
);
991 nic
= &netdev
->u
.nic
;
993 idx
= nic_get_free_idx();
994 if (idx
== -1 || nb_nics
>= MAX_NICS
) {
995 error_setg(errp
, "too many NICs");
1001 memset(nd
, 0, sizeof(*nd
));
1004 nd
->netdev
= qemu_find_netdev(nic
->netdev
);
1006 error_setg(errp
, "netdev '%s' not found", nic
->netdev
);
1013 nd
->name
= g_strdup(name
);
1015 nd
->model
= g_strdup(nic
->model
);
1018 nd
->devaddr
= g_strdup(nic
->addr
);
1022 net_parse_macaddr(nd
->macaddr
.a
, nic
->macaddr
) < 0) {
1023 error_setg(errp
, "invalid syntax for ethernet address");
1027 is_multicast_ether_addr(nd
->macaddr
.a
)) {
1029 "NIC cannot have multicast MAC address (odd 1st byte)");
1032 qemu_macaddr_default_if_unset(&nd
->macaddr
);
1034 if (nic
->has_vectors
) {
1035 if (nic
->vectors
> 0x7ffffff) {
1036 error_setg(errp
, "invalid # of vectors: %"PRIu32
, nic
->vectors
);
1039 nd
->nvectors
= nic
->vectors
;
1041 nd
->nvectors
= DEV_NVECTORS_UNSPECIFIED
;
1050 static gboolean
add_nic_result(gpointer key
, gpointer value
, gpointer user_data
)
1052 GPtrArray
*results
= user_data
;
1053 GPtrArray
*alias_list
= value
;
1054 const char *model
= key
;
1058 result
= g_strdup(model
);
1060 GString
*result_str
= g_string_new(model
);
1063 g_string_append(result_str
, " (aka ");
1064 for (i
= 0; i
< alias_list
->len
; i
++) {
1066 g_string_append(result_str
, ", ");
1068 g_string_append(result_str
, alias_list
->pdata
[i
]);
1070 g_string_append(result_str
, ")");
1071 result
= result_str
->str
;
1072 g_string_free(result_str
, false);
1073 g_ptr_array_unref(alias_list
);
1075 g_ptr_array_add(results
, result
);
1079 static int model_cmp(char **a
, char **b
)
1081 return strcmp(*a
, *b
);
1084 static void show_nic_models(void)
1086 GPtrArray
*results
= g_ptr_array_new();
1089 g_hash_table_foreach_remove(nic_model_help
, add_nic_result
, results
);
1090 g_ptr_array_sort(results
, (GCompareFunc
)model_cmp
);
1092 printf("Available NIC models for this configuration:\n");
1093 for (i
= 0 ; i
< results
->len
; i
++) {
1094 printf("%s\n", (char *)results
->pdata
[i
]);
1096 g_hash_table_unref(nic_model_help
);
1097 nic_model_help
= NULL
;
1100 static void add_nic_model_help(const char *model
, const char *alias
)
1102 GPtrArray
*alias_list
= NULL
;
1104 if (g_hash_table_lookup_extended(nic_model_help
, model
, NULL
,
1105 (gpointer
*)&alias_list
)) {
1106 /* Already exists, no alias to add: return */
1111 /* Check if this alias is already in the list. Add if not. */
1112 if (!g_ptr_array_find_with_equal_func(alias_list
, alias
,
1113 g_str_equal
, NULL
)) {
1114 g_ptr_array_add(alias_list
, g_strdup(alias
));
1119 /* Either this model wasn't in the list already, or a first alias added */
1121 alias_list
= g_ptr_array_new();
1122 g_ptr_array_set_free_func(alias_list
, g_free
);
1123 g_ptr_array_add(alias_list
, g_strdup(alias
));
1125 g_hash_table_replace(nic_model_help
, g_strdup(model
), alias_list
);
1128 NICInfo
*qemu_find_nic_info(const char *typename
, bool match_default
,
1134 if (nic_model_help
) {
1135 add_nic_model_help(typename
, alias
);
1138 for (i
= 0; i
< nb_nics
; i
++) {
1141 if (!nd
->used
|| nd
->instantiated
) {
1145 if ((match_default
&& !nd
->model
) || !g_strcmp0(nd
->model
, typename
)
1146 || (alias
&& !g_strcmp0(nd
->model
, alias
))) {
1154 /* "I have created a device. Please configure it if you can" */
1155 bool qemu_configure_nic_device(DeviceState
*dev
, bool match_default
,
1158 NICInfo
*nd
= qemu_find_nic_info(object_get_typename(OBJECT(dev
)),
1159 match_default
, alias
);
1162 qdev_set_nic_properties(dev
, nd
);
1168 /* "Please create a device, if you have a configuration for it" */
1169 DeviceState
*qemu_create_nic_device(const char *typename
, bool match_default
,
1172 NICInfo
*nd
= qemu_find_nic_info(typename
, match_default
, alias
);
1179 dev
= qdev_new(typename
);
1180 qdev_set_nic_properties(dev
, nd
);
1184 void qemu_create_nic_bus_devices(BusState
*bus
, const char *parent_type
,
1185 const char *default_model
,
1186 const char *alias
, const char *alias_target
)
1188 GPtrArray
*nic_models
= qemu_get_nic_models(parent_type
);
1194 if (nic_model_help
) {
1196 add_nic_model_help(alias_target
, alias
);
1198 for (i
= 0; i
< nic_models
->len
- 1; i
++) {
1199 add_nic_model_help(nic_models
->pdata
[i
], NULL
);
1203 /* Drop the NULL terminator which would make g_str_equal() unhappy */
1206 for (i
= 0; i
< nb_nics
; i
++) {
1209 if (!nd
->used
|| nd
->instantiated
) {
1213 model
= nd
->model
? nd
->model
: default_model
;
1218 /* Each bus type is allowed *one* substitution */
1219 if (g_str_equal(model
, alias
)) {
1220 model
= alias_target
;
1223 if (!g_ptr_array_find_with_equal_func(nic_models
, model
,
1224 g_str_equal
, NULL
)) {
1225 /* This NIC does not live on this bus. */
1229 dev
= qdev_new(model
);
1230 qdev_set_nic_properties(dev
, nd
);
1231 qdev_realize_and_unref(dev
, bus
, &error_fatal
);
1234 g_ptr_array_free(nic_models
, true);
1237 static int (* const net_client_init_fun
[NET_CLIENT_DRIVER__MAX
])(
1238 const Netdev
*netdev
,
1240 NetClientState
*peer
, Error
**errp
) = {
1241 [NET_CLIENT_DRIVER_NIC
] = net_init_nic
,
1243 [NET_CLIENT_DRIVER_USER
] = net_init_slirp
,
1245 [NET_CLIENT_DRIVER_TAP
] = net_init_tap
,
1246 [NET_CLIENT_DRIVER_SOCKET
] = net_init_socket
,
1247 [NET_CLIENT_DRIVER_STREAM
] = net_init_stream
,
1248 [NET_CLIENT_DRIVER_DGRAM
] = net_init_dgram
,
1250 [NET_CLIENT_DRIVER_VDE
] = net_init_vde
,
1252 #ifdef CONFIG_NETMAP
1253 [NET_CLIENT_DRIVER_NETMAP
] = net_init_netmap
,
1255 #ifdef CONFIG_AF_XDP
1256 [NET_CLIENT_DRIVER_AF_XDP
] = net_init_af_xdp
,
1258 #ifdef CONFIG_NET_BRIDGE
1259 [NET_CLIENT_DRIVER_BRIDGE
] = net_init_bridge
,
1261 [NET_CLIENT_DRIVER_HUBPORT
] = net_init_hubport
,
1262 #ifdef CONFIG_VHOST_NET_USER
1263 [NET_CLIENT_DRIVER_VHOST_USER
] = net_init_vhost_user
,
1265 #ifdef CONFIG_VHOST_NET_VDPA
1266 [NET_CLIENT_DRIVER_VHOST_VDPA
] = net_init_vhost_vdpa
,
1268 #ifdef CONFIG_L2TPV3
1269 [NET_CLIENT_DRIVER_L2TPV3
] = net_init_l2tpv3
,
1272 [NET_CLIENT_DRIVER_VMNET_HOST
] = net_init_vmnet_host
,
1273 [NET_CLIENT_DRIVER_VMNET_SHARED
] = net_init_vmnet_shared
,
1274 [NET_CLIENT_DRIVER_VMNET_BRIDGED
] = net_init_vmnet_bridged
,
1275 #endif /* CONFIG_VMNET */
1279 static int net_client_init1(const Netdev
*netdev
, bool is_netdev
, Error
**errp
)
1281 NetClientState
*peer
= NULL
;
1285 if (netdev
->type
== NET_CLIENT_DRIVER_NIC
||
1286 !net_client_init_fun
[netdev
->type
]) {
1287 error_setg(errp
, "network backend '%s' is not compiled into this binary",
1288 NetClientDriver_str(netdev
->type
));
1292 if (netdev
->type
== NET_CLIENT_DRIVER_NONE
) {
1293 return 0; /* nothing to do */
1295 if (netdev
->type
== NET_CLIENT_DRIVER_HUBPORT
) {
1296 error_setg(errp
, "network backend '%s' is only supported with -netdev/-nic",
1297 NetClientDriver_str(netdev
->type
));
1301 if (!net_client_init_fun
[netdev
->type
]) {
1302 error_setg(errp
, "network backend '%s' is not compiled into this binary",
1303 NetClientDriver_str(netdev
->type
));
1307 /* Do not add to a hub if it's a nic with a netdev= parameter. */
1308 if (netdev
->type
!= NET_CLIENT_DRIVER_NIC
||
1309 !netdev
->u
.nic
.netdev
) {
1310 peer
= net_hub_add_port(0, NULL
, NULL
);
1314 nc
= qemu_find_netdev(netdev
->id
);
1316 error_setg(errp
, "Duplicate ID '%s'", netdev
->id
);
1320 if (net_client_init_fun
[netdev
->type
](netdev
, netdev
->id
, peer
, errp
) < 0) {
1321 /* FIXME drop when all init functions store an Error */
1322 if (errp
&& !*errp
) {
1323 error_setg(errp
, "Device '%s' could not be initialized",
1324 NetClientDriver_str(netdev
->type
));
1330 nc
= qemu_find_netdev(netdev
->id
);
1332 nc
->is_netdev
= true;
1338 void show_netdevs(void)
1341 const char *available_netdevs
[] = {
1350 #ifdef CONFIG_L2TPV3
1356 #ifdef CONFIG_NET_BRIDGE
1359 #ifdef CONFIG_NETMAP
1362 #ifdef CONFIG_AF_XDP
1368 #ifdef CONFIG_VHOST_VDPA
1378 qemu_printf("Available netdev backend types:\n");
1379 for (idx
= 0; idx
< ARRAY_SIZE(available_netdevs
); idx
++) {
1380 qemu_printf("%s\n", available_netdevs
[idx
]);
1384 static int net_client_init(QemuOpts
*opts
, bool is_netdev
, Error
**errp
)
1386 gchar
**substrings
= NULL
;
1387 Netdev
*object
= NULL
;
1389 Visitor
*v
= opts_visitor_new(opts
);
1391 /* Parse convenience option format ipv6-net=fec0::0[/64] */
1392 const char *ip6_net
= qemu_opt_get(opts
, "ipv6-net");
1396 unsigned long prefix_len
= 64; /* Default 64bit prefix length. */
1398 substrings
= g_strsplit(ip6_net
, "/", 2);
1399 if (!substrings
|| !substrings
[0]) {
1400 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "ipv6-net",
1401 "a valid IPv6 prefix");
1405 prefix_addr
= substrings
[0];
1407 /* Handle user-specified prefix length. */
1408 if (substrings
[1] &&
1409 qemu_strtoul(substrings
[1], NULL
, 10, &prefix_len
))
1412 "parameter 'ipv6-net' expects a number after '/'");
1416 qemu_opt_set(opts
, "ipv6-prefix", prefix_addr
, &error_abort
);
1417 qemu_opt_set_number(opts
, "ipv6-prefixlen", prefix_len
,
1419 qemu_opt_unset(opts
, "ipv6-net");
1422 /* Create an ID for -net if the user did not specify one */
1423 if (!is_netdev
&& !qemu_opts_id(opts
)) {
1424 qemu_opts_set_id(opts
, id_generate(ID_NET
));
1427 if (visit_type_Netdev(v
, NULL
, &object
, errp
)) {
1428 ret
= net_client_init1(object
, is_netdev
, errp
);
1431 qapi_free_Netdev(object
);
1434 g_strfreev(substrings
);
1439 void netdev_add(QemuOpts
*opts
, Error
**errp
)
1441 net_client_init(opts
, true, errp
);
1444 void qmp_netdev_add(Netdev
*netdev
, Error
**errp
)
1446 if (!id_wellformed(netdev
->id
)) {
1447 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "id", "an identifier");
1451 net_client_init1(netdev
, true, errp
);
1454 void qmp_netdev_del(const char *id
, Error
**errp
)
1459 nc
= qemu_find_netdev(id
);
1461 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1462 "Device '%s' not found", id
);
1466 if (!nc
->is_netdev
) {
1467 error_setg(errp
, "Device '%s' is not a netdev", id
);
1471 qemu_del_net_client(nc
);
1474 * Wart: we need to delete the QemuOpts associated with netdevs
1475 * created via CLI or HMP, to avoid bogus "Duplicate ID" errors in
1478 opts
= qemu_opts_find(qemu_find_opts("netdev"), id
);
1480 qemu_opts_del(opts
);
1484 static void netfilter_print_info(Monitor
*mon
, NetFilterState
*nf
)
1487 ObjectProperty
*prop
;
1488 ObjectPropertyIterator iter
;
1491 /* generate info str */
1492 object_property_iter_init(&iter
, OBJECT(nf
));
1493 while ((prop
= object_property_iter_next(&iter
))) {
1494 if (!strcmp(prop
->name
, "type")) {
1497 v
= string_output_visitor_new(false, &str
);
1498 object_property_get(OBJECT(nf
), prop
->name
, v
, NULL
);
1499 visit_complete(v
, &str
);
1501 monitor_printf(mon
, ",%s=%s", prop
->name
, str
);
1504 monitor_printf(mon
, "\n");
1507 void print_net_client(Monitor
*mon
, NetClientState
*nc
)
1511 monitor_printf(mon
, "%s: index=%d,type=%s,%s\n", nc
->name
,
1513 NetClientDriver_str(nc
->info
->type
),
1515 if (!QTAILQ_EMPTY(&nc
->filters
)) {
1516 monitor_printf(mon
, "filters:\n");
1518 QTAILQ_FOREACH(nf
, &nc
->filters
, next
) {
1519 monitor_printf(mon
, " - %s: type=%s",
1520 object_get_canonical_path_component(OBJECT(nf
)),
1521 object_get_typename(OBJECT(nf
)));
1522 netfilter_print_info(mon
, nf
);
1526 RxFilterInfoList
*qmp_query_rx_filter(const char *name
, Error
**errp
)
1529 RxFilterInfoList
*filter_list
= NULL
, **tail
= &filter_list
;
1531 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
1534 if (name
&& strcmp(nc
->name
, name
) != 0) {
1538 /* only query rx-filter information of NIC */
1539 if (nc
->info
->type
!= NET_CLIENT_DRIVER_NIC
) {
1541 error_setg(errp
, "net client(%s) isn't a NIC", name
);
1542 assert(!filter_list
);
1548 /* only query information on queue 0 since the info is per nic,
1551 if (nc
->queue_index
!= 0)
1554 if (nc
->info
->query_rx_filter
) {
1555 info
= nc
->info
->query_rx_filter(nc
);
1556 QAPI_LIST_APPEND(tail
, info
);
1558 error_setg(errp
, "net client(%s) doesn't support"
1559 " rx-filter querying", name
);
1560 assert(!filter_list
);
1569 if (filter_list
== NULL
&& name
) {
1570 error_setg(errp
, "invalid net client name: %s", name
);
1576 void colo_notify_filters_event(int event
, Error
**errp
)
1580 NetFilterClass
*nfc
= NULL
;
1581 Error
*local_err
= NULL
;
1583 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
1584 QTAILQ_FOREACH(nf
, &nc
->filters
, next
) {
1585 nfc
= NETFILTER_GET_CLASS(OBJECT(nf
));
1586 nfc
->handle_event(nf
, event
, &local_err
);
1588 error_propagate(errp
, local_err
);
1595 void qmp_set_link(const char *name
, bool up
, Error
**errp
)
1597 NetClientState
*ncs
[MAX_QUEUE_NUM
];
1601 queues
= qemu_find_net_clients_except(name
, ncs
,
1602 NET_CLIENT_DRIVER__MAX
,
1606 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1607 "Device '%s' not found", name
);
1612 for (i
= 0; i
< queues
; i
++) {
1613 ncs
[i
]->link_down
= !up
;
1616 if (nc
->info
->link_status_changed
) {
1617 nc
->info
->link_status_changed(nc
);
1621 /* Change peer link only if the peer is NIC and then notify peer.
1622 * If the peer is a HUBPORT or a backend, we do not change the
1625 * This behavior is compatible with qemu hubs where there could be
1626 * multiple clients that can still communicate with each other in
1627 * disconnected mode. For now maintain this compatibility.
1629 if (nc
->peer
->info
->type
== NET_CLIENT_DRIVER_NIC
) {
1630 for (i
= 0; i
< queues
; i
++) {
1631 ncs
[i
]->peer
->link_down
= !up
;
1634 if (nc
->peer
->info
->link_status_changed
) {
1635 nc
->peer
->info
->link_status_changed(nc
->peer
);
1640 static void net_vm_change_state_handler(void *opaque
, bool running
,
1644 NetClientState
*tmp
;
1646 QTAILQ_FOREACH_SAFE(nc
, &net_clients
, next
, tmp
) {
1648 /* Flush queued packets and wake up backends. */
1649 if (nc
->peer
&& qemu_can_send_packet(nc
)) {
1650 qemu_flush_queued_packets(nc
->peer
);
1653 /* Complete all queued packets, to guarantee we don't modify
1654 * state later when VM is not running.
1656 qemu_flush_or_purge_queued_packets(nc
, true);
1661 void net_cleanup(void)
1663 NetClientState
*nc
, **p
= &QTAILQ_FIRST(&net_clients
);
1665 /*cleanup colo compare module for COLO*/
1666 colo_compare_cleanup();
1669 * Walk the net_clients list and remove the netdevs but *not* any
1670 * NET_CLIENT_DRIVER_NIC entries. The latter are owned by the device
1671 * model which created them, and in some cases (e.g. xen-net-device)
1672 * the device itself may do cleanup at exit and will be upset if we
1673 * just delete its NIC from underneath it.
1675 * Since qemu_del_net_client() may delete multiple entries, using
1676 * QTAILQ_FOREACH_SAFE() is not safe here. The only safe pointer
1677 * to keep as a bookmark is a NET_CLIENT_DRIVER_NIC entry, so keep
1678 * 'p' pointing to either the head of the list, or the 'next' field
1679 * of the latest NET_CLIENT_DRIVER_NIC, and operate on *p as we walk
1682 * The 'nc' variable isn't part of the list traversal; it's purely
1683 * for convenience as too much '(*p)->' has a tendency to make the
1684 * readers' eyes bleed.
1688 if (nc
->info
->type
== NET_CLIENT_DRIVER_NIC
) {
1689 /* Skip NET_CLIENT_DRIVER_NIC entries */
1690 p
= &QTAILQ_NEXT(nc
, next
);
1692 qemu_del_net_client(nc
);
1696 qemu_del_vm_change_state_handler(net_change_state_entry
);
1699 void net_check_clients(void)
1704 if (nic_model_help
) {
1708 net_hub_check_clients();
1710 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
1712 warn_report("%s %s has no peer",
1713 nc
->info
->type
== NET_CLIENT_DRIVER_NIC
1719 /* Check that all NICs requested via -net nic actually got created.
1720 * NICs created via -device don't need to be checked here because
1721 * they are always instantiated.
1723 for (i
= 0; i
< MAX_NICS
; i
++) {
1724 NICInfo
*nd
= &nd_table
[i
];
1725 if (nd
->used
&& !nd
->instantiated
) {
1726 warn_report("requested NIC (%s, model %s) "
1727 "was not created (not supported by this machine?)",
1728 nd
->name
? nd
->name
: "anonymous",
1729 nd
->model
? nd
->model
: "unspecified");
1734 static int net_init_client(void *dummy
, QemuOpts
*opts
, Error
**errp
)
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 (!nic_model_help
&& !g_strcmp0(ni
->model
, "help")) {
1793 nic_model_help
= g_hash_table_new_full(g_str_hash
, g_str_equal
,
1798 /* Create an ID if the user did not specify one */
1799 nd_id
= g_strdup(qemu_opts_id(opts
));
1801 nd_id
= id_generate(ID_NET
);
1802 qemu_opts_set_id(opts
, nd_id
);
1805 /* Handle MAC address */
1806 mac
= qemu_opt_get_del(opts
, "mac");
1808 ret
= net_parse_macaddr(ni
->macaddr
.a
, mac
);
1811 error_setg(errp
, "invalid syntax for ethernet address");
1814 if (is_multicast_ether_addr(ni
->macaddr
.a
)) {
1815 error_setg(errp
, "NIC cannot have multicast MAC address");
1820 qemu_macaddr_default_if_unset(&ni
->macaddr
);
1822 ret
= net_client_init(opts
, true, errp
);
1824 ni
->netdev
= qemu_find_netdev(nd_id
);
1834 static void netdev_init_modern(void)
1836 while (!QSIMPLEQ_EMPTY(&nd_queue
)) {
1837 NetdevQueueEntry
*nd
= QSIMPLEQ_FIRST(&nd_queue
);
1839 QSIMPLEQ_REMOVE_HEAD(&nd_queue
, entry
);
1840 loc_push_restore(&nd
->loc
);
1841 net_client_init1(nd
->nd
, true, &error_fatal
);
1843 qapi_free_Netdev(nd
->nd
);
1848 void net_init_clients(void)
1850 net_change_state_entry
=
1851 qemu_add_vm_change_state_handler(net_vm_change_state_handler
, NULL
);
1853 QTAILQ_INIT(&net_clients
);
1855 netdev_init_modern();
1857 qemu_opts_foreach(qemu_find_opts("netdev"), net_init_netdev
, NULL
,
1860 qemu_opts_foreach(qemu_find_opts("nic"), net_param_nic
, NULL
,
1863 qemu_opts_foreach(qemu_find_opts("net"), net_init_client
, NULL
,
1868 * Does this -netdev argument use modern rather than traditional syntax?
1869 * Modern syntax is to be parsed with netdev_parse_modern().
1870 * Traditional syntax is to be parsed with net_client_parse().
1872 bool netdev_is_modern(const char *optstr
)
1877 static QemuOptsList dummy_opts
= {
1879 .implied_opt_name
= "type",
1880 .head
= QTAILQ_HEAD_INITIALIZER(dummy_opts
.head
),
1884 if (optstr
[0] == '{') {
1885 /* This is JSON, which means it's modern syntax */
1889 opts
= qemu_opts_create(&dummy_opts
, NULL
, false, &error_abort
);
1890 qemu_opts_do_parse(opts
, optstr
, dummy_opts
.implied_opt_name
,
1892 type
= qemu_opt_get(opts
, "type");
1893 is_modern
= !g_strcmp0(type
, "stream") || !g_strcmp0(type
, "dgram");
1895 qemu_opts_reset(&dummy_opts
);
1901 * netdev_parse_modern() uses modern, more expressive syntax than
1902 * net_client_parse(), but supports only the -netdev option.
1903 * netdev_parse_modern() appends to @nd_queue, whereas net_client_parse()
1904 * appends to @qemu_netdev_opts.
1906 void netdev_parse_modern(const char *optstr
)
1909 NetdevQueueEntry
*nd
;
1911 v
= qobject_input_visitor_new_str(optstr
, "type", &error_fatal
);
1912 nd
= g_new(NetdevQueueEntry
, 1);
1913 visit_type_Netdev(v
, NULL
, &nd
->nd
, &error_fatal
);
1917 QSIMPLEQ_INSERT_TAIL(&nd_queue
, nd
, entry
);
1920 void net_client_parse(QemuOptsList
*opts_list
, const char *optstr
)
1922 if (!qemu_opts_parse_noisily(opts_list
, optstr
, true)) {
1929 uint32_t net_crc32(const uint8_t *p
, int len
)
1936 for (i
= 0; i
< len
; i
++) {
1938 for (j
= 0; j
< 8; j
++) {
1939 carry
= ((crc
& 0x80000000L
) ? 1 : 0) ^ (b
& 0x01);
1943 crc
= ((crc
^ POLYNOMIAL_BE
) | carry
);
1951 uint32_t net_crc32_le(const uint8_t *p
, int len
)
1958 for (i
= 0; i
< len
; i
++) {
1960 for (j
= 0; j
< 8; j
++) {
1961 carry
= (crc
& 0x1) ^ (b
& 0x01);
1965 crc
^= POLYNOMIAL_LE
;
1973 QemuOptsList qemu_netdev_opts
= {
1975 .implied_opt_name
= "type",
1976 .head
= QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts
.head
),
1979 * no elements => accept any params
1980 * validation will happen later
1982 { /* end of list */ }
1986 QemuOptsList qemu_nic_opts
= {
1988 .implied_opt_name
= "type",
1989 .head
= QTAILQ_HEAD_INITIALIZER(qemu_nic_opts
.head
),
1992 * no elements => accept any params
1993 * validation will happen later
1995 { /* end of list */ }
1999 QemuOptsList qemu_net_opts
= {
2001 .implied_opt_name
= "type",
2002 .head
= QTAILQ_HEAD_INITIALIZER(qemu_net_opts
.head
),
2005 * no elements => accept any params
2006 * validation will happen later
2008 { /* end of list */ }
2012 void net_socket_rs_init(SocketReadState
*rs
,
2013 SocketReadStateFinalize
*finalize
,
2017 rs
->vnet_hdr
= vnet_hdr
;
2020 rs
->vnet_hdr_len
= 0;
2021 memset(rs
->buf
, 0, sizeof(rs
->buf
));
2022 rs
->finalize
= finalize
;
2030 int net_fill_rstate(SocketReadState
*rs
, const uint8_t *buf
, int size
)
2035 /* Reassemble a packet from the network.
2036 * 0 = getting length.
2037 * 1 = getting vnet header length.
2040 switch (rs
->state
) {
2046 memcpy(rs
->buf
+ rs
->index
, buf
, l
);
2050 if (rs
->index
== 4) {
2052 rs
->packet_len
= ntohl(*(uint32_t *)rs
->buf
);
2058 rs
->vnet_hdr_len
= 0;
2067 memcpy(rs
->buf
+ rs
->index
, buf
, l
);
2071 if (rs
->index
== 4) {
2072 /* got vnet header length */
2073 rs
->vnet_hdr_len
= ntohl(*(uint32_t *)rs
->buf
);
2079 l
= rs
->packet_len
- rs
->index
;
2083 if (rs
->index
+ l
<= sizeof(rs
->buf
)) {
2084 memcpy(rs
->buf
+ rs
->index
, buf
, l
);
2086 fprintf(stderr
, "serious error: oversized packet received,"
2087 "connection terminated.\n");
2088 rs
->index
= rs
->state
= 0;
2095 if (rs
->index
>= rs
->packet_len
) {
2098 assert(rs
->finalize
);