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
24 #include "qemu/osdep.h"
29 #include "net/slirp.h"
33 #include "monitor/monitor.h"
34 #include "qemu-common.h"
35 #include "qemu/help_option.h"
36 #include "qapi/qmp/qerror.h"
37 #include "qemu/error-report.h"
38 #include "qemu/sockets.h"
39 #include "qemu/cutils.h"
40 #include "qemu/config-file.h"
41 #include "qmp-commands.h"
44 #include "qemu/main-loop.h"
45 #include "qapi-visit.h"
46 #include "qapi/opts-visitor.h"
47 #include "sysemu/sysemu.h"
48 #include "sysemu/qtest.h"
49 #include "net/filter.h"
50 #include "qapi/string-output-visitor.h"
52 /* Net bridge is currently not supported for W32. */
54 # define CONFIG_NET_BRIDGE
57 static VMChangeStateEntry
*net_change_state_entry
;
58 static QTAILQ_HEAD(, NetClientState
) net_clients
;
60 const char *host_net_devices
[] = {
64 #ifdef CONFIG_NET_BRIDGE
80 /***********************************************************/
81 /* network device redirectors */
83 static int get_str_sep(char *buf
, int buf_size
, const char **pp
, int sep
)
94 if (len
> buf_size
- 1)
103 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
)
111 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
113 saddr
->sin_family
= AF_INET
;
114 if (buf
[0] == '\0') {
115 saddr
->sin_addr
.s_addr
= 0;
117 if (qemu_isdigit(buf
[0])) {
118 if (!inet_aton(buf
, &saddr
->sin_addr
))
121 if ((he
= gethostbyname(buf
)) == NULL
)
123 saddr
->sin_addr
= *(struct in_addr
*)he
->h_addr
;
126 port
= strtol(p
, (char **)&r
, 0);
129 saddr
->sin_port
= htons(port
);
133 char *qemu_mac_strdup_printf(const uint8_t *macaddr
)
135 return g_strdup_printf("%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",
136 macaddr
[0], macaddr
[1], macaddr
[2],
137 macaddr
[3], macaddr
[4], macaddr
[5]);
140 void qemu_format_nic_info_str(NetClientState
*nc
, uint8_t macaddr
[6])
142 snprintf(nc
->info_str
, sizeof(nc
->info_str
),
143 "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
145 macaddr
[0], macaddr
[1], macaddr
[2],
146 macaddr
[3], macaddr
[4], macaddr
[5]);
149 static int mac_table
[256] = {0};
151 static void qemu_macaddr_set_used(MACAddr
*macaddr
)
155 for (index
= 0x56; index
< 0xFF; index
++) {
156 if (macaddr
->a
[5] == index
) {
162 static void qemu_macaddr_set_free(MACAddr
*macaddr
)
165 static const MACAddr base
= { .a
= { 0x52, 0x54, 0x00, 0x12, 0x34, 0 } };
167 if (memcmp(macaddr
->a
, &base
.a
, (sizeof(base
.a
) - 1)) != 0) {
170 for (index
= 0x56; index
< 0xFF; index
++) {
171 if (macaddr
->a
[5] == index
) {
177 static int qemu_macaddr_get_free(void)
181 for (index
= 0x56; index
< 0xFF; index
++) {
182 if (mac_table
[index
] == 0) {
190 void qemu_macaddr_default_if_unset(MACAddr
*macaddr
)
192 static const MACAddr zero
= { .a
= { 0,0,0,0,0,0 } };
193 static const MACAddr base
= { .a
= { 0x52, 0x54, 0x00, 0x12, 0x34, 0 } };
195 if (memcmp(macaddr
, &zero
, sizeof(zero
)) != 0) {
196 if (memcmp(macaddr
->a
, &base
.a
, (sizeof(base
.a
) - 1)) != 0) {
199 qemu_macaddr_set_used(macaddr
);
204 macaddr
->a
[0] = 0x52;
205 macaddr
->a
[1] = 0x54;
206 macaddr
->a
[2] = 0x00;
207 macaddr
->a
[3] = 0x12;
208 macaddr
->a
[4] = 0x34;
209 macaddr
->a
[5] = qemu_macaddr_get_free();
210 qemu_macaddr_set_used(macaddr
);
214 * Generate a name for net client
216 * Only net clients created with the legacy -net option and NICs need this.
218 static char *assign_name(NetClientState
*nc1
, const char *model
)
223 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
227 if (strcmp(nc
->model
, model
) == 0) {
232 return g_strdup_printf("%s.%d", model
, id
);
235 static void qemu_net_client_destructor(NetClientState
*nc
)
240 static void qemu_net_client_setup(NetClientState
*nc
,
242 NetClientState
*peer
,
245 NetClientDestructor
*destructor
)
248 nc
->model
= g_strdup(model
);
250 nc
->name
= g_strdup(name
);
252 nc
->name
= assign_name(nc
, model
);
260 QTAILQ_INSERT_TAIL(&net_clients
, nc
, next
);
262 nc
->incoming_queue
= qemu_new_net_queue(qemu_deliver_packet_iov
, nc
);
263 nc
->destructor
= destructor
;
264 QTAILQ_INIT(&nc
->filters
);
267 NetClientState
*qemu_new_net_client(NetClientInfo
*info
,
268 NetClientState
*peer
,
274 assert(info
->size
>= sizeof(NetClientState
));
276 nc
= g_malloc0(info
->size
);
277 qemu_net_client_setup(nc
, info
, peer
, model
, name
,
278 qemu_net_client_destructor
);
283 NICState
*qemu_new_nic(NetClientInfo
*info
,
289 NetClientState
**peers
= conf
->peers
.ncs
;
291 int i
, queues
= MAX(1, conf
->peers
.queues
);
293 assert(info
->type
== NET_CLIENT_DRIVER_NIC
);
294 assert(info
->size
>= sizeof(NICState
));
296 nic
= g_malloc0(info
->size
+ sizeof(NetClientState
) * queues
);
297 nic
->ncs
= (void *)nic
+ info
->size
;
299 nic
->opaque
= opaque
;
301 for (i
= 0; i
< queues
; i
++) {
302 qemu_net_client_setup(&nic
->ncs
[i
], info
, peers
[i
], model
, name
,
304 nic
->ncs
[i
].queue_index
= i
;
310 NetClientState
*qemu_get_subqueue(NICState
*nic
, int queue_index
)
312 return nic
->ncs
+ queue_index
;
315 NetClientState
*qemu_get_queue(NICState
*nic
)
317 return qemu_get_subqueue(nic
, 0);
320 NICState
*qemu_get_nic(NetClientState
*nc
)
322 NetClientState
*nc0
= nc
- nc
->queue_index
;
324 return (NICState
*)((void *)nc0
- nc
->info
->size
);
327 void *qemu_get_nic_opaque(NetClientState
*nc
)
329 NICState
*nic
= qemu_get_nic(nc
);
334 static void qemu_cleanup_net_client(NetClientState
*nc
)
336 QTAILQ_REMOVE(&net_clients
, nc
, next
);
338 if (nc
->info
->cleanup
) {
339 nc
->info
->cleanup(nc
);
343 static void qemu_free_net_client(NetClientState
*nc
)
345 if (nc
->incoming_queue
) {
346 qemu_del_net_queue(nc
->incoming_queue
);
349 nc
->peer
->peer
= NULL
;
353 if (nc
->destructor
) {
358 void qemu_del_net_client(NetClientState
*nc
)
360 NetClientState
*ncs
[MAX_QUEUE_NUM
];
362 NetFilterState
*nf
, *next
;
364 assert(nc
->info
->type
!= NET_CLIENT_DRIVER_NIC
);
366 /* If the NetClientState belongs to a multiqueue backend, we will change all
367 * other NetClientStates also.
369 queues
= qemu_find_net_clients_except(nc
->name
, ncs
,
370 NET_CLIENT_DRIVER_NIC
,
374 QTAILQ_FOREACH_SAFE(nf
, &nc
->filters
, next
, next
) {
375 object_unparent(OBJECT(nf
));
378 /* If there is a peer NIC, delete and cleanup client, but do not free. */
379 if (nc
->peer
&& nc
->peer
->info
->type
== NET_CLIENT_DRIVER_NIC
) {
380 NICState
*nic
= qemu_get_nic(nc
->peer
);
381 if (nic
->peer_deleted
) {
384 nic
->peer_deleted
= true;
386 for (i
= 0; i
< queues
; i
++) {
387 ncs
[i
]->peer
->link_down
= true;
390 if (nc
->peer
->info
->link_status_changed
) {
391 nc
->peer
->info
->link_status_changed(nc
->peer
);
394 for (i
= 0; i
< queues
; i
++) {
395 qemu_cleanup_net_client(ncs
[i
]);
401 for (i
= 0; i
< queues
; i
++) {
402 qemu_cleanup_net_client(ncs
[i
]);
403 qemu_free_net_client(ncs
[i
]);
407 void qemu_del_nic(NICState
*nic
)
409 int i
, queues
= MAX(nic
->conf
->peers
.queues
, 1);
411 qemu_macaddr_set_free(&nic
->conf
->macaddr
);
413 /* If this is a peer NIC and peer has already been deleted, free it now. */
414 if (nic
->peer_deleted
) {
415 for (i
= 0; i
< queues
; i
++) {
416 qemu_free_net_client(qemu_get_subqueue(nic
, i
)->peer
);
420 for (i
= queues
- 1; i
>= 0; i
--) {
421 NetClientState
*nc
= qemu_get_subqueue(nic
, i
);
423 qemu_cleanup_net_client(nc
);
424 qemu_free_net_client(nc
);
430 void qemu_foreach_nic(qemu_nic_foreach func
, void *opaque
)
434 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
435 if (nc
->info
->type
== NET_CLIENT_DRIVER_NIC
) {
436 if (nc
->queue_index
== 0) {
437 func(qemu_get_nic(nc
), opaque
);
443 bool qemu_has_ufo(NetClientState
*nc
)
445 if (!nc
|| !nc
->info
->has_ufo
) {
449 return nc
->info
->has_ufo(nc
);
452 bool qemu_has_vnet_hdr(NetClientState
*nc
)
454 if (!nc
|| !nc
->info
->has_vnet_hdr
) {
458 return nc
->info
->has_vnet_hdr(nc
);
461 bool qemu_has_vnet_hdr_len(NetClientState
*nc
, int len
)
463 if (!nc
|| !nc
->info
->has_vnet_hdr_len
) {
467 return nc
->info
->has_vnet_hdr_len(nc
, len
);
470 void qemu_using_vnet_hdr(NetClientState
*nc
, bool enable
)
472 if (!nc
|| !nc
->info
->using_vnet_hdr
) {
476 nc
->info
->using_vnet_hdr(nc
, enable
);
479 void qemu_set_offload(NetClientState
*nc
, int csum
, int tso4
, int tso6
,
482 if (!nc
|| !nc
->info
->set_offload
) {
486 nc
->info
->set_offload(nc
, csum
, tso4
, tso6
, ecn
, ufo
);
489 void qemu_set_vnet_hdr_len(NetClientState
*nc
, int len
)
491 if (!nc
|| !nc
->info
->set_vnet_hdr_len
) {
495 nc
->vnet_hdr_len
= len
;
496 nc
->info
->set_vnet_hdr_len(nc
, len
);
499 int qemu_set_vnet_le(NetClientState
*nc
, bool is_le
)
501 #ifdef HOST_WORDS_BIGENDIAN
502 if (!nc
|| !nc
->info
->set_vnet_le
) {
506 return nc
->info
->set_vnet_le(nc
, is_le
);
512 int qemu_set_vnet_be(NetClientState
*nc
, bool is_be
)
514 #ifdef HOST_WORDS_BIGENDIAN
517 if (!nc
|| !nc
->info
->set_vnet_be
) {
521 return nc
->info
->set_vnet_be(nc
, is_be
);
525 int qemu_can_send_packet(NetClientState
*sender
)
527 int vm_running
= runstate_is_running();
537 if (sender
->peer
->receive_disabled
) {
539 } else if (sender
->peer
->info
->can_receive
&&
540 !sender
->peer
->info
->can_receive(sender
->peer
)) {
546 static ssize_t
filter_receive_iov(NetClientState
*nc
,
547 NetFilterDirection direction
,
548 NetClientState
*sender
,
550 const struct iovec
*iov
,
552 NetPacketSent
*sent_cb
)
555 NetFilterState
*nf
= NULL
;
557 if (direction
== NET_FILTER_DIRECTION_TX
) {
558 QTAILQ_FOREACH(nf
, &nc
->filters
, next
) {
559 ret
= qemu_netfilter_receive(nf
, direction
, sender
, flags
, iov
,
566 QTAILQ_FOREACH_REVERSE(nf
, &nc
->filters
, NetFilterHead
, next
) {
567 ret
= qemu_netfilter_receive(nf
, direction
, sender
, flags
, iov
,
578 static ssize_t
filter_receive(NetClientState
*nc
,
579 NetFilterDirection direction
,
580 NetClientState
*sender
,
584 NetPacketSent
*sent_cb
)
587 .iov_base
= (void *)data
,
591 return filter_receive_iov(nc
, direction
, sender
, flags
, &iov
, 1, sent_cb
);
594 void qemu_purge_queued_packets(NetClientState
*nc
)
600 qemu_net_queue_purge(nc
->peer
->incoming_queue
, nc
);
604 void qemu_flush_or_purge_queued_packets(NetClientState
*nc
, bool purge
)
606 nc
->receive_disabled
= 0;
608 if (nc
->peer
&& nc
->peer
->info
->type
== NET_CLIENT_DRIVER_HUBPORT
) {
609 if (net_hub_flush(nc
->peer
)) {
613 if (qemu_net_queue_flush(nc
->incoming_queue
)) {
614 /* We emptied the queue successfully, signal to the IO thread to repoll
615 * the file descriptor (for tap, for example).
619 /* Unable to empty the queue, purge remaining packets */
620 qemu_net_queue_purge(nc
->incoming_queue
, nc
);
624 void qemu_flush_queued_packets(NetClientState
*nc
)
626 qemu_flush_or_purge_queued_packets(nc
, false);
629 static ssize_t
qemu_send_packet_async_with_flags(NetClientState
*sender
,
631 const uint8_t *buf
, int size
,
632 NetPacketSent
*sent_cb
)
638 printf("qemu_send_packet_async:\n");
639 qemu_hexdump((const char *)buf
, stdout
, "net", size
);
642 if (sender
->link_down
|| !sender
->peer
) {
646 /* Let filters handle the packet first */
647 ret
= filter_receive(sender
, NET_FILTER_DIRECTION_TX
,
648 sender
, flags
, buf
, size
, sent_cb
);
653 ret
= filter_receive(sender
->peer
, NET_FILTER_DIRECTION_RX
,
654 sender
, flags
, buf
, size
, sent_cb
);
659 queue
= sender
->peer
->incoming_queue
;
661 return qemu_net_queue_send(queue
, sender
, flags
, buf
, size
, sent_cb
);
664 ssize_t
qemu_send_packet_async(NetClientState
*sender
,
665 const uint8_t *buf
, int size
,
666 NetPacketSent
*sent_cb
)
668 return qemu_send_packet_async_with_flags(sender
, QEMU_NET_PACKET_FLAG_NONE
,
672 void qemu_send_packet(NetClientState
*nc
, const uint8_t *buf
, int size
)
674 qemu_send_packet_async(nc
, buf
, size
, NULL
);
677 ssize_t
qemu_send_packet_raw(NetClientState
*nc
, const uint8_t *buf
, int size
)
679 return qemu_send_packet_async_with_flags(nc
, QEMU_NET_PACKET_FLAG_RAW
,
683 static ssize_t
nc_sendv_compat(NetClientState
*nc
, const struct iovec
*iov
,
684 int iovcnt
, unsigned flags
)
692 buffer
= iov
[0].iov_base
;
693 offset
= iov
[0].iov_len
;
695 offset
= iov_size(iov
, iovcnt
);
696 if (offset
> NET_BUFSIZE
) {
699 buf
= g_malloc(offset
);
701 offset
= iov_to_buf(iov
, iovcnt
, 0, buf
, offset
);
704 if (flags
& QEMU_NET_PACKET_FLAG_RAW
&& nc
->info
->receive_raw
) {
705 ret
= nc
->info
->receive_raw(nc
, buffer
, offset
);
707 ret
= nc
->info
->receive(nc
, buffer
, offset
);
714 ssize_t
qemu_deliver_packet_iov(NetClientState
*sender
,
716 const struct iovec
*iov
,
720 NetClientState
*nc
= opaque
;
724 return iov_size(iov
, iovcnt
);
727 if (nc
->receive_disabled
) {
731 if (nc
->info
->receive_iov
&& !(flags
& QEMU_NET_PACKET_FLAG_RAW
)) {
732 ret
= nc
->info
->receive_iov(nc
, iov
, iovcnt
);
734 ret
= nc_sendv_compat(nc
, iov
, iovcnt
, flags
);
738 nc
->receive_disabled
= 1;
744 ssize_t
qemu_sendv_packet_async(NetClientState
*sender
,
745 const struct iovec
*iov
, int iovcnt
,
746 NetPacketSent
*sent_cb
)
751 if (sender
->link_down
|| !sender
->peer
) {
752 return iov_size(iov
, iovcnt
);
755 /* Let filters handle the packet first */
756 ret
= filter_receive_iov(sender
, NET_FILTER_DIRECTION_TX
, sender
,
757 QEMU_NET_PACKET_FLAG_NONE
, iov
, iovcnt
, sent_cb
);
762 ret
= filter_receive_iov(sender
->peer
, NET_FILTER_DIRECTION_RX
, sender
,
763 QEMU_NET_PACKET_FLAG_NONE
, iov
, iovcnt
, sent_cb
);
768 queue
= sender
->peer
->incoming_queue
;
770 return qemu_net_queue_send_iov(queue
, sender
,
771 QEMU_NET_PACKET_FLAG_NONE
,
772 iov
, iovcnt
, sent_cb
);
776 qemu_sendv_packet(NetClientState
*nc
, const struct iovec
*iov
, int iovcnt
)
778 return qemu_sendv_packet_async(nc
, iov
, iovcnt
, NULL
);
781 NetClientState
*qemu_find_netdev(const char *id
)
785 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
786 if (nc
->info
->type
== NET_CLIENT_DRIVER_NIC
)
788 if (!strcmp(nc
->name
, id
)) {
796 int qemu_find_net_clients_except(const char *id
, NetClientState
**ncs
,
797 NetClientDriver type
, int max
)
802 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
803 if (nc
->info
->type
== type
) {
806 if (!id
|| !strcmp(nc
->name
, id
)) {
817 static int nic_get_free_idx(void)
821 for (index
= 0; index
< MAX_NICS
; index
++)
822 if (!nd_table
[index
].used
)
827 int qemu_show_nic_models(const char *arg
, const char *const *models
)
831 if (!arg
|| !is_help_option(arg
)) {
835 fprintf(stderr
, "qemu: Supported NIC models: ");
836 for (i
= 0 ; models
[i
]; i
++)
837 fprintf(stderr
, "%s%c", models
[i
], models
[i
+1] ? ',' : '\n');
841 void qemu_check_nic_model(NICInfo
*nd
, const char *model
)
843 const char *models
[2];
848 if (qemu_show_nic_models(nd
->model
, models
))
850 if (qemu_find_nic_model(nd
, models
, model
) < 0)
854 int qemu_find_nic_model(NICInfo
*nd
, const char * const *models
,
855 const char *default_model
)
860 nd
->model
= g_strdup(default_model
);
862 for (i
= 0 ; models
[i
]; i
++) {
863 if (strcmp(nd
->model
, models
[i
]) == 0)
867 error_report("Unsupported NIC model: %s", nd
->model
);
871 static int net_init_nic(const Netdev
*netdev
, const char *name
,
872 NetClientState
*peer
, Error
**errp
)
876 const NetLegacyNicOptions
*nic
;
878 assert(netdev
->type
== NET_CLIENT_DRIVER_NIC
);
879 nic
= &netdev
->u
.nic
;
881 idx
= nic_get_free_idx();
882 if (idx
== -1 || nb_nics
>= MAX_NICS
) {
883 error_setg(errp
, "too many NICs");
889 memset(nd
, 0, sizeof(*nd
));
891 if (nic
->has_netdev
) {
892 nd
->netdev
= qemu_find_netdev(nic
->netdev
);
894 error_setg(errp
, "netdev '%s' not found", nic
->netdev
);
901 nd
->name
= g_strdup(name
);
902 if (nic
->has_model
) {
903 nd
->model
= g_strdup(nic
->model
);
906 nd
->devaddr
= g_strdup(nic
->addr
);
909 if (nic
->has_macaddr
&&
910 net_parse_macaddr(nd
->macaddr
.a
, nic
->macaddr
) < 0) {
911 error_setg(errp
, "invalid syntax for ethernet address");
914 if (nic
->has_macaddr
&&
915 is_multicast_ether_addr(nd
->macaddr
.a
)) {
917 "NIC cannot have multicast MAC address (odd 1st byte)");
920 qemu_macaddr_default_if_unset(&nd
->macaddr
);
922 if (nic
->has_vectors
) {
923 if (nic
->vectors
> 0x7ffffff) {
924 error_setg(errp
, "invalid # of vectors: %"PRIu32
, nic
->vectors
);
927 nd
->nvectors
= nic
->vectors
;
929 nd
->nvectors
= DEV_NVECTORS_UNSPECIFIED
;
939 static int (* const net_client_init_fun
[NET_CLIENT_DRIVER__MAX
])(
940 const Netdev
*netdev
,
942 NetClientState
*peer
, Error
**errp
) = {
943 [NET_CLIENT_DRIVER_NIC
] = net_init_nic
,
945 [NET_CLIENT_DRIVER_USER
] = net_init_slirp
,
947 [NET_CLIENT_DRIVER_TAP
] = net_init_tap
,
948 [NET_CLIENT_DRIVER_SOCKET
] = net_init_socket
,
950 [NET_CLIENT_DRIVER_VDE
] = net_init_vde
,
953 [NET_CLIENT_DRIVER_NETMAP
] = net_init_netmap
,
955 [NET_CLIENT_DRIVER_DUMP
] = net_init_dump
,
956 #ifdef CONFIG_NET_BRIDGE
957 [NET_CLIENT_DRIVER_BRIDGE
] = net_init_bridge
,
959 [NET_CLIENT_DRIVER_HUBPORT
] = net_init_hubport
,
960 #ifdef CONFIG_VHOST_NET_USED
961 [NET_CLIENT_DRIVER_VHOST_USER
] = net_init_vhost_user
,
964 [NET_CLIENT_DRIVER_L2TPV3
] = net_init_l2tpv3
,
969 static int net_client_init1(const void *object
, bool is_netdev
, Error
**errp
)
972 const Netdev
*netdev
;
974 NetClientState
*peer
= NULL
;
975 static bool vlan_warned
;
981 if (netdev
->type
== NET_CLIENT_DRIVER_DUMP
||
982 netdev
->type
== NET_CLIENT_DRIVER_NIC
||
983 !net_client_init_fun
[netdev
->type
]) {
984 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "type",
985 "a netdev backend type");
989 const NetLegacy
*net
= object
;
990 const NetLegacyOptions
*opts
= net
->opts
;
993 /* missing optional values have been initialized to "all bits zero" */
994 name
= net
->has_id
? net
->id
: net
->name
;
996 /* Map the old options to the new flat type */
997 switch (opts
->type
) {
998 case NET_LEGACY_OPTIONS_TYPE_NONE
:
999 return 0; /* nothing to do */
1000 case NET_LEGACY_OPTIONS_TYPE_NIC
:
1001 legacy
.type
= NET_CLIENT_DRIVER_NIC
;
1002 legacy
.u
.nic
= opts
->u
.nic
;
1004 case NET_LEGACY_OPTIONS_TYPE_USER
:
1005 legacy
.type
= NET_CLIENT_DRIVER_USER
;
1006 legacy
.u
.user
= opts
->u
.user
;
1008 case NET_LEGACY_OPTIONS_TYPE_TAP
:
1009 legacy
.type
= NET_CLIENT_DRIVER_TAP
;
1010 legacy
.u
.tap
= opts
->u
.tap
;
1012 case NET_LEGACY_OPTIONS_TYPE_L2TPV3
:
1013 legacy
.type
= NET_CLIENT_DRIVER_L2TPV3
;
1014 legacy
.u
.l2tpv3
= opts
->u
.l2tpv3
;
1016 case NET_LEGACY_OPTIONS_TYPE_SOCKET
:
1017 legacy
.type
= NET_CLIENT_DRIVER_SOCKET
;
1018 legacy
.u
.socket
= opts
->u
.socket
;
1020 case NET_LEGACY_OPTIONS_TYPE_VDE
:
1021 legacy
.type
= NET_CLIENT_DRIVER_VDE
;
1022 legacy
.u
.vde
= opts
->u
.vde
;
1024 case NET_LEGACY_OPTIONS_TYPE_DUMP
:
1025 legacy
.type
= NET_CLIENT_DRIVER_DUMP
;
1026 legacy
.u
.dump
= opts
->u
.dump
;
1028 case NET_LEGACY_OPTIONS_TYPE_BRIDGE
:
1029 legacy
.type
= NET_CLIENT_DRIVER_BRIDGE
;
1030 legacy
.u
.bridge
= opts
->u
.bridge
;
1032 case NET_LEGACY_OPTIONS_TYPE_NETMAP
:
1033 legacy
.type
= NET_CLIENT_DRIVER_NETMAP
;
1034 legacy
.u
.netmap
= opts
->u
.netmap
;
1036 case NET_LEGACY_OPTIONS_TYPE_VHOST_USER
:
1037 legacy
.type
= NET_CLIENT_DRIVER_VHOST_USER
;
1038 legacy
.u
.vhost_user
= opts
->u
.vhost_user
;
1044 if (!net_client_init_fun
[netdev
->type
]) {
1045 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "type",
1046 "a net backend type (maybe it is not compiled "
1047 "into this binary)");
1051 /* Do not add to a vlan if it's a nic with a netdev= parameter. */
1052 if (netdev
->type
!= NET_CLIENT_DRIVER_NIC
||
1053 !opts
->u
.nic
.has_netdev
) {
1054 peer
= net_hub_add_port(net
->has_vlan
? net
->vlan
: 0, NULL
);
1057 if (net
->has_vlan
&& !vlan_warned
) {
1058 error_report("'vlan' is deprecated. Please use 'netdev' instead.");
1063 if (net_client_init_fun
[netdev
->type
](netdev
, name
, peer
, errp
) < 0) {
1064 /* FIXME drop when all init functions store an Error */
1065 if (errp
&& !*errp
) {
1066 error_setg(errp
, QERR_DEVICE_INIT_FAILED
,
1067 NetClientDriver_lookup
[netdev
->type
]);
1075 int net_client_init(QemuOpts
*opts
, bool is_netdev
, Error
**errp
)
1077 void *object
= NULL
;
1080 Visitor
*v
= opts_visitor_new(opts
);
1083 /* Parse convenience option format ip6-net=fec0::0[/64] */
1084 const char *ip6_net
= qemu_opt_get(opts
, "ipv6-net");
1087 char buf
[strlen(ip6_net
) + 1];
1089 if (get_str_sep(buf
, sizeof(buf
), &ip6_net
, '/') < 0) {
1090 /* Default 64bit prefix length. */
1091 qemu_opt_set(opts
, "ipv6-prefix", ip6_net
, &error_abort
);
1092 qemu_opt_set_number(opts
, "ipv6-prefixlen", 64, &error_abort
);
1094 /* User-specified prefix length. */
1098 qemu_opt_set(opts
, "ipv6-prefix", buf
, &error_abort
);
1099 err
= qemu_strtoul(ip6_net
, NULL
, 10, &len
);
1102 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
1103 "ipv6-prefix", "a number");
1105 qemu_opt_set_number(opts
, "ipv6-prefixlen", len
,
1109 qemu_opt_unset(opts
, "ipv6-net");
1114 visit_type_Netdev(v
, NULL
, (Netdev
**)&object
, &err
);
1116 visit_type_NetLegacy(v
, NULL
, (NetLegacy
**)&object
, &err
);
1120 ret
= net_client_init1(object
, is_netdev
, &err
);
1124 qapi_free_Netdev(object
);
1126 qapi_free_NetLegacy(object
);
1129 error_propagate(errp
, err
);
1135 static int net_host_check_device(const char *device
)
1138 for (i
= 0; host_net_devices
[i
]; i
++) {
1139 if (!strncmp(host_net_devices
[i
], device
,
1140 strlen(host_net_devices
[i
]))) {
1148 void hmp_host_net_add(Monitor
*mon
, const QDict
*qdict
)
1150 const char *device
= qdict_get_str(qdict
, "device");
1151 const char *opts_str
= qdict_get_try_str(qdict
, "opts");
1152 Error
*local_err
= NULL
;
1156 if (!warned
&& !qtest_enabled()) {
1157 error_report("host_net_add is deprecated, use netdev_add instead");
1161 if (!net_host_check_device(device
)) {
1162 monitor_printf(mon
, "invalid host network device %s\n", device
);
1166 opts
= qemu_opts_parse_noisily(qemu_find_opts("net"),
1167 opts_str
? opts_str
: "", false);
1172 qemu_opt_set(opts
, "type", device
, &error_abort
);
1174 net_client_init(opts
, false, &local_err
);
1176 error_report_err(local_err
);
1177 monitor_printf(mon
, "adding host network device %s failed\n", device
);
1181 void hmp_host_net_remove(Monitor
*mon
, const QDict
*qdict
)
1184 int vlan_id
= qdict_get_int(qdict
, "vlan_id");
1185 const char *device
= qdict_get_str(qdict
, "device");
1188 if (!warned
&& !qtest_enabled()) {
1189 error_report("host_net_remove is deprecated, use netdev_del instead");
1193 nc
= net_hub_find_client_by_name(vlan_id
, device
);
1195 error_report("Host network device '%s' on hub '%d' not found",
1199 if (nc
->info
->type
== NET_CLIENT_DRIVER_NIC
) {
1200 error_report("invalid host network device '%s'", device
);
1204 qemu_del_net_client(nc
->peer
);
1205 qemu_del_net_client(nc
);
1206 qemu_opts_del(qemu_opts_find(qemu_find_opts("net"), device
));
1209 void netdev_add(QemuOpts
*opts
, Error
**errp
)
1211 net_client_init(opts
, true, errp
);
1214 void qmp_netdev_add(QDict
*qdict
, QObject
**ret
, Error
**errp
)
1216 Error
*local_err
= NULL
;
1217 QemuOptsList
*opts_list
;
1220 opts_list
= qemu_find_opts_err("netdev", &local_err
);
1225 opts
= qemu_opts_from_qdict(opts_list
, qdict
, &local_err
);
1230 netdev_add(opts
, &local_err
);
1232 qemu_opts_del(opts
);
1237 error_propagate(errp
, local_err
);
1240 void qmp_netdev_del(const char *id
, Error
**errp
)
1245 nc
= qemu_find_netdev(id
);
1247 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1248 "Device '%s' not found", id
);
1252 opts
= qemu_opts_find(qemu_find_opts_err("netdev", NULL
), id
);
1254 error_setg(errp
, "Device '%s' is not a netdev", id
);
1258 qemu_del_net_client(nc
);
1259 qemu_opts_del(opts
);
1262 static void netfilter_print_info(Monitor
*mon
, NetFilterState
*nf
)
1265 ObjectProperty
*prop
;
1266 ObjectPropertyIterator iter
;
1269 /* generate info str */
1270 object_property_iter_init(&iter
, OBJECT(nf
));
1271 while ((prop
= object_property_iter_next(&iter
))) {
1272 if (!strcmp(prop
->name
, "type")) {
1275 v
= string_output_visitor_new(false, &str
);
1276 object_property_get(OBJECT(nf
), v
, prop
->name
, NULL
);
1277 visit_complete(v
, &str
);
1279 monitor_printf(mon
, ",%s=%s", prop
->name
, str
);
1282 monitor_printf(mon
, "\n");
1285 void print_net_client(Monitor
*mon
, NetClientState
*nc
)
1289 monitor_printf(mon
, "%s: index=%d,type=%s,%s\n", nc
->name
,
1291 NetClientDriver_lookup
[nc
->info
->type
],
1293 if (!QTAILQ_EMPTY(&nc
->filters
)) {
1294 monitor_printf(mon
, "filters:\n");
1296 QTAILQ_FOREACH(nf
, &nc
->filters
, next
) {
1297 char *path
= object_get_canonical_path_component(OBJECT(nf
));
1299 monitor_printf(mon
, " - %s: type=%s", path
,
1300 object_get_typename(OBJECT(nf
)));
1301 netfilter_print_info(mon
, nf
);
1306 RxFilterInfoList
*qmp_query_rx_filter(bool has_name
, const char *name
,
1310 RxFilterInfoList
*filter_list
= NULL
, *last_entry
= NULL
;
1312 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
1313 RxFilterInfoList
*entry
;
1316 if (has_name
&& strcmp(nc
->name
, name
) != 0) {
1320 /* only query rx-filter information of NIC */
1321 if (nc
->info
->type
!= NET_CLIENT_DRIVER_NIC
) {
1323 error_setg(errp
, "net client(%s) isn't a NIC", name
);
1329 /* only query information on queue 0 since the info is per nic,
1332 if (nc
->queue_index
!= 0)
1335 if (nc
->info
->query_rx_filter
) {
1336 info
= nc
->info
->query_rx_filter(nc
);
1337 entry
= g_malloc0(sizeof(*entry
));
1338 entry
->value
= info
;
1341 filter_list
= entry
;
1343 last_entry
->next
= entry
;
1346 } else if (has_name
) {
1347 error_setg(errp
, "net client(%s) doesn't support"
1348 " rx-filter querying", name
);
1357 if (filter_list
== NULL
&& has_name
) {
1358 error_setg(errp
, "invalid net client name: %s", name
);
1364 void hmp_info_network(Monitor
*mon
, const QDict
*qdict
)
1366 NetClientState
*nc
, *peer
;
1367 NetClientDriver type
;
1371 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
1373 type
= nc
->info
->type
;
1375 /* Skip if already printed in hub info */
1376 if (net_hub_id_for_client(nc
, NULL
) == 0) {
1380 if (!peer
|| type
== NET_CLIENT_DRIVER_NIC
) {
1381 print_net_client(mon
, nc
);
1382 } /* else it's a netdev connected to a NIC, printed with the NIC */
1383 if (peer
&& type
== NET_CLIENT_DRIVER_NIC
) {
1384 monitor_printf(mon
, " \\ ");
1385 print_net_client(mon
, peer
);
1390 void qmp_set_link(const char *name
, bool up
, Error
**errp
)
1392 NetClientState
*ncs
[MAX_QUEUE_NUM
];
1396 queues
= qemu_find_net_clients_except(name
, ncs
,
1397 NET_CLIENT_DRIVER__MAX
,
1401 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1402 "Device '%s' not found", name
);
1407 for (i
= 0; i
< queues
; i
++) {
1408 ncs
[i
]->link_down
= !up
;
1411 if (nc
->info
->link_status_changed
) {
1412 nc
->info
->link_status_changed(nc
);
1416 /* Change peer link only if the peer is NIC and then notify peer.
1417 * If the peer is a HUBPORT or a backend, we do not change the
1420 * This behavior is compatible with qemu vlans where there could be
1421 * multiple clients that can still communicate with each other in
1422 * disconnected mode. For now maintain this compatibility.
1424 if (nc
->peer
->info
->type
== NET_CLIENT_DRIVER_NIC
) {
1425 for (i
= 0; i
< queues
; i
++) {
1426 ncs
[i
]->peer
->link_down
= !up
;
1429 if (nc
->peer
->info
->link_status_changed
) {
1430 nc
->peer
->info
->link_status_changed(nc
->peer
);
1435 static void net_vm_change_state_handler(void *opaque
, int running
,
1439 NetClientState
*tmp
;
1441 QTAILQ_FOREACH_SAFE(nc
, &net_clients
, next
, tmp
) {
1443 /* Flush queued packets and wake up backends. */
1444 if (nc
->peer
&& qemu_can_send_packet(nc
)) {
1445 qemu_flush_queued_packets(nc
->peer
);
1448 /* Complete all queued packets, to guarantee we don't modify
1449 * state later when VM is not running.
1451 qemu_flush_or_purge_queued_packets(nc
, true);
1456 void net_cleanup(void)
1460 /* We may del multiple entries during qemu_del_net_client(),
1461 * so QTAILQ_FOREACH_SAFE() is also not safe here.
1463 while (!QTAILQ_EMPTY(&net_clients
)) {
1464 nc
= QTAILQ_FIRST(&net_clients
);
1465 if (nc
->info
->type
== NET_CLIENT_DRIVER_NIC
) {
1466 qemu_del_nic(qemu_get_nic(nc
));
1468 qemu_del_net_client(nc
);
1472 qemu_del_vm_change_state_handler(net_change_state_entry
);
1475 void net_check_clients(void)
1480 net_hub_check_clients();
1482 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
1484 fprintf(stderr
, "Warning: %s %s has no peer\n",
1485 nc
->info
->type
== NET_CLIENT_DRIVER_NIC
?
1486 "nic" : "netdev", nc
->name
);
1490 /* Check that all NICs requested via -net nic actually got created.
1491 * NICs created via -device don't need to be checked here because
1492 * they are always instantiated.
1494 for (i
= 0; i
< MAX_NICS
; i
++) {
1495 NICInfo
*nd
= &nd_table
[i
];
1496 if (nd
->used
&& !nd
->instantiated
) {
1497 fprintf(stderr
, "Warning: requested NIC (%s, model %s) "
1498 "was not created (not supported by this machine?)\n",
1499 nd
->name
? nd
->name
: "anonymous",
1500 nd
->model
? nd
->model
: "unspecified");
1505 static int net_init_client(void *dummy
, QemuOpts
*opts
, Error
**errp
)
1507 Error
*local_err
= NULL
;
1509 net_client_init(opts
, false, &local_err
);
1511 error_report_err(local_err
);
1518 static int net_init_netdev(void *dummy
, QemuOpts
*opts
, Error
**errp
)
1520 Error
*local_err
= NULL
;
1523 ret
= net_client_init(opts
, true, &local_err
);
1525 error_report_err(local_err
);
1532 int net_init_clients(void)
1534 QemuOptsList
*net
= qemu_find_opts("net");
1536 net_change_state_entry
=
1537 qemu_add_vm_change_state_handler(net_vm_change_state_handler
, NULL
);
1539 QTAILQ_INIT(&net_clients
);
1541 if (qemu_opts_foreach(qemu_find_opts("netdev"),
1542 net_init_netdev
, NULL
, NULL
)) {
1546 if (qemu_opts_foreach(net
, net_init_client
, NULL
, NULL
)) {
1553 int net_client_parse(QemuOptsList
*opts_list
, const char *optarg
)
1555 #if defined(CONFIG_SLIRP)
1557 if (net_slirp_parse_legacy(opts_list
, optarg
, &ret
)) {
1562 if (!qemu_opts_parse_noisily(opts_list
, optarg
, true)) {
1571 unsigned compute_mcast_idx(const uint8_t *ep
)
1578 for (i
= 0; i
< 6; i
++) {
1580 for (j
= 0; j
< 8; j
++) {
1581 carry
= ((crc
& 0x80000000L
) ? 1 : 0) ^ (b
& 0x01);
1585 crc
= ((crc
^ POLYNOMIAL
) | carry
);
1592 QemuOptsList qemu_netdev_opts
= {
1594 .implied_opt_name
= "type",
1595 .head
= QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts
.head
),
1598 * no elements => accept any params
1599 * validation will happen later
1601 { /* end of list */ }
1605 QemuOptsList qemu_net_opts
= {
1607 .implied_opt_name
= "type",
1608 .head
= QTAILQ_HEAD_INITIALIZER(qemu_net_opts
.head
),
1611 * no elements => accept any params
1612 * validation will happen later
1614 { /* end of list */ }
1618 void net_socket_rs_init(SocketReadState
*rs
,
1619 SocketReadStateFinalize
*finalize
,
1623 rs
->vnet_hdr
= vnet_hdr
;
1626 rs
->vnet_hdr_len
= 0;
1627 memset(rs
->buf
, 0, sizeof(rs
->buf
));
1628 rs
->finalize
= finalize
;
1636 int net_fill_rstate(SocketReadState
*rs
, const uint8_t *buf
, int size
)
1641 /* Reassemble a packet from the network.
1642 * 0 = getting length.
1643 * 1 = getting vnet header length.
1646 switch (rs
->state
) {
1652 memcpy(rs
->buf
+ rs
->index
, buf
, l
);
1656 if (rs
->index
== 4) {
1658 rs
->packet_len
= ntohl(*(uint32_t *)rs
->buf
);
1664 rs
->vnet_hdr_len
= 0;
1673 memcpy(rs
->buf
+ rs
->index
, buf
, l
);
1677 if (rs
->index
== 4) {
1678 /* got vnet header length */
1679 rs
->vnet_hdr_len
= ntohl(*(uint32_t *)rs
->buf
);
1685 l
= rs
->packet_len
- rs
->index
;
1689 if (rs
->index
+ l
<= sizeof(rs
->buf
)) {
1690 memcpy(rs
->buf
+ rs
->index
, buf
, l
);
1692 fprintf(stderr
, "serious error: oversized packet received,"
1693 "connection terminated.\n");
1694 rs
->index
= rs
->state
= 0;
1701 if (rs
->index
>= rs
->packet_len
) {
1704 assert(rs
->finalize
);