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 "net/filter.h"
49 #include "qapi/string-output-visitor.h"
51 /* Net bridge is currently not supported for W32. */
53 # define CONFIG_NET_BRIDGE
56 static VMChangeStateEntry
*net_change_state_entry
;
57 static QTAILQ_HEAD(, NetClientState
) net_clients
;
59 const char *host_net_devices
[] = {
63 #ifdef CONFIG_NET_BRIDGE
81 /***********************************************************/
82 /* network device redirectors */
84 static int get_str_sep(char *buf
, int buf_size
, const char **pp
, int sep
)
95 if (len
> buf_size
- 1)
104 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
)
112 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
114 saddr
->sin_family
= AF_INET
;
115 if (buf
[0] == '\0') {
116 saddr
->sin_addr
.s_addr
= 0;
118 if (qemu_isdigit(buf
[0])) {
119 if (!inet_aton(buf
, &saddr
->sin_addr
))
122 if ((he
= gethostbyname(buf
)) == NULL
)
124 saddr
->sin_addr
= *(struct in_addr
*)he
->h_addr
;
127 port
= strtol(p
, (char **)&r
, 0);
130 saddr
->sin_port
= htons(port
);
134 char *qemu_mac_strdup_printf(const uint8_t *macaddr
)
136 return g_strdup_printf("%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",
137 macaddr
[0], macaddr
[1], macaddr
[2],
138 macaddr
[3], macaddr
[4], macaddr
[5]);
141 void qemu_format_nic_info_str(NetClientState
*nc
, uint8_t macaddr
[6])
143 snprintf(nc
->info_str
, sizeof(nc
->info_str
),
144 "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
146 macaddr
[0], macaddr
[1], macaddr
[2],
147 macaddr
[3], macaddr
[4], macaddr
[5]);
150 static int mac_table
[256] = {0};
152 static void qemu_macaddr_set_used(MACAddr
*macaddr
)
156 for (index
= 0x56; index
< 0xFF; index
++) {
157 if (macaddr
->a
[5] == index
) {
163 static void qemu_macaddr_set_free(MACAddr
*macaddr
)
166 static const MACAddr base
= { .a
= { 0x52, 0x54, 0x00, 0x12, 0x34, 0 } };
168 if (memcmp(macaddr
->a
, &base
.a
, (sizeof(base
.a
) - 1)) != 0) {
171 for (index
= 0x56; index
< 0xFF; index
++) {
172 if (macaddr
->a
[5] == index
) {
178 static int qemu_macaddr_get_free(void)
182 for (index
= 0x56; index
< 0xFF; index
++) {
183 if (mac_table
[index
] == 0) {
191 void qemu_macaddr_default_if_unset(MACAddr
*macaddr
)
193 static const MACAddr zero
= { .a
= { 0,0,0,0,0,0 } };
194 static const MACAddr base
= { .a
= { 0x52, 0x54, 0x00, 0x12, 0x34, 0 } };
196 if (memcmp(macaddr
, &zero
, sizeof(zero
)) != 0) {
197 if (memcmp(macaddr
->a
, &base
.a
, (sizeof(base
.a
) - 1)) != 0) {
200 qemu_macaddr_set_used(macaddr
);
205 macaddr
->a
[0] = 0x52;
206 macaddr
->a
[1] = 0x54;
207 macaddr
->a
[2] = 0x00;
208 macaddr
->a
[3] = 0x12;
209 macaddr
->a
[4] = 0x34;
210 macaddr
->a
[5] = qemu_macaddr_get_free();
211 qemu_macaddr_set_used(macaddr
);
215 * Generate a name for net client
217 * Only net clients created with the legacy -net option and NICs need this.
219 static char *assign_name(NetClientState
*nc1
, const char *model
)
224 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
228 if (strcmp(nc
->model
, model
) == 0) {
233 return g_strdup_printf("%s.%d", model
, id
);
236 static void qemu_net_client_destructor(NetClientState
*nc
)
241 static void qemu_net_client_setup(NetClientState
*nc
,
243 NetClientState
*peer
,
246 NetClientDestructor
*destructor
)
249 nc
->model
= g_strdup(model
);
251 nc
->name
= g_strdup(name
);
253 nc
->name
= assign_name(nc
, model
);
261 QTAILQ_INSERT_TAIL(&net_clients
, nc
, next
);
263 nc
->incoming_queue
= qemu_new_net_queue(qemu_deliver_packet_iov
, nc
);
264 nc
->destructor
= destructor
;
265 QTAILQ_INIT(&nc
->filters
);
268 NetClientState
*qemu_new_net_client(NetClientInfo
*info
,
269 NetClientState
*peer
,
275 assert(info
->size
>= sizeof(NetClientState
));
277 nc
= g_malloc0(info
->size
);
278 qemu_net_client_setup(nc
, info
, peer
, model
, name
,
279 qemu_net_client_destructor
);
284 NICState
*qemu_new_nic(NetClientInfo
*info
,
290 NetClientState
**peers
= conf
->peers
.ncs
;
292 int i
, queues
= MAX(1, conf
->peers
.queues
);
294 assert(info
->type
== NET_CLIENT_OPTIONS_KIND_NIC
);
295 assert(info
->size
>= sizeof(NICState
));
297 nic
= g_malloc0(info
->size
+ sizeof(NetClientState
) * queues
);
298 nic
->ncs
= (void *)nic
+ info
->size
;
300 nic
->opaque
= opaque
;
302 for (i
= 0; i
< queues
; i
++) {
303 qemu_net_client_setup(&nic
->ncs
[i
], info
, peers
[i
], model
, name
,
305 nic
->ncs
[i
].queue_index
= i
;
311 NetClientState
*qemu_get_subqueue(NICState
*nic
, int queue_index
)
313 return nic
->ncs
+ queue_index
;
316 NetClientState
*qemu_get_queue(NICState
*nic
)
318 return qemu_get_subqueue(nic
, 0);
321 NICState
*qemu_get_nic(NetClientState
*nc
)
323 NetClientState
*nc0
= nc
- nc
->queue_index
;
325 return (NICState
*)((void *)nc0
- nc
->info
->size
);
328 void *qemu_get_nic_opaque(NetClientState
*nc
)
330 NICState
*nic
= qemu_get_nic(nc
);
335 static void qemu_cleanup_net_client(NetClientState
*nc
)
337 QTAILQ_REMOVE(&net_clients
, nc
, next
);
339 if (nc
->info
->cleanup
) {
340 nc
->info
->cleanup(nc
);
344 static void qemu_free_net_client(NetClientState
*nc
)
346 if (nc
->incoming_queue
) {
347 qemu_del_net_queue(nc
->incoming_queue
);
350 nc
->peer
->peer
= NULL
;
354 if (nc
->destructor
) {
359 void qemu_del_net_client(NetClientState
*nc
)
361 NetClientState
*ncs
[MAX_QUEUE_NUM
];
363 NetFilterState
*nf
, *next
;
365 assert(nc
->info
->type
!= NET_CLIENT_OPTIONS_KIND_NIC
);
367 /* If the NetClientState belongs to a multiqueue backend, we will change all
368 * other NetClientStates also.
370 queues
= qemu_find_net_clients_except(nc
->name
, ncs
,
371 NET_CLIENT_OPTIONS_KIND_NIC
,
375 QTAILQ_FOREACH_SAFE(nf
, &nc
->filters
, next
, next
) {
376 object_unparent(OBJECT(nf
));
379 /* If there is a peer NIC, delete and cleanup client, but do not free. */
380 if (nc
->peer
&& nc
->peer
->info
->type
== NET_CLIENT_OPTIONS_KIND_NIC
) {
381 NICState
*nic
= qemu_get_nic(nc
->peer
);
382 if (nic
->peer_deleted
) {
385 nic
->peer_deleted
= true;
387 for (i
= 0; i
< queues
; i
++) {
388 ncs
[i
]->peer
->link_down
= true;
391 if (nc
->peer
->info
->link_status_changed
) {
392 nc
->peer
->info
->link_status_changed(nc
->peer
);
395 for (i
= 0; i
< queues
; i
++) {
396 qemu_cleanup_net_client(ncs
[i
]);
402 for (i
= 0; i
< queues
; i
++) {
403 qemu_cleanup_net_client(ncs
[i
]);
404 qemu_free_net_client(ncs
[i
]);
408 void qemu_del_nic(NICState
*nic
)
410 int i
, queues
= MAX(nic
->conf
->peers
.queues
, 1);
412 qemu_macaddr_set_free(&nic
->conf
->macaddr
);
414 /* If this is a peer NIC and peer has already been deleted, free it now. */
415 if (nic
->peer_deleted
) {
416 for (i
= 0; i
< queues
; i
++) {
417 qemu_free_net_client(qemu_get_subqueue(nic
, i
)->peer
);
421 for (i
= queues
- 1; i
>= 0; i
--) {
422 NetClientState
*nc
= qemu_get_subqueue(nic
, i
);
424 qemu_cleanup_net_client(nc
);
425 qemu_free_net_client(nc
);
431 void qemu_foreach_nic(qemu_nic_foreach func
, void *opaque
)
435 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
436 if (nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_NIC
) {
437 if (nc
->queue_index
== 0) {
438 func(qemu_get_nic(nc
), opaque
);
444 bool qemu_has_ufo(NetClientState
*nc
)
446 if (!nc
|| !nc
->info
->has_ufo
) {
450 return nc
->info
->has_ufo(nc
);
453 bool qemu_has_vnet_hdr(NetClientState
*nc
)
455 if (!nc
|| !nc
->info
->has_vnet_hdr
) {
459 return nc
->info
->has_vnet_hdr(nc
);
462 bool qemu_has_vnet_hdr_len(NetClientState
*nc
, int len
)
464 if (!nc
|| !nc
->info
->has_vnet_hdr_len
) {
468 return nc
->info
->has_vnet_hdr_len(nc
, len
);
471 void qemu_using_vnet_hdr(NetClientState
*nc
, bool enable
)
473 if (!nc
|| !nc
->info
->using_vnet_hdr
) {
477 nc
->info
->using_vnet_hdr(nc
, enable
);
480 void qemu_set_offload(NetClientState
*nc
, int csum
, int tso4
, int tso6
,
483 if (!nc
|| !nc
->info
->set_offload
) {
487 nc
->info
->set_offload(nc
, csum
, tso4
, tso6
, ecn
, ufo
);
490 void qemu_set_vnet_hdr_len(NetClientState
*nc
, int len
)
492 if (!nc
|| !nc
->info
->set_vnet_hdr_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_OPTIONS_KIND_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 buf
= g_new(uint8_t, NET_BUFSIZE
);
697 offset
= iov_to_buf(iov
, iovcnt
, 0, buf
, NET_BUFSIZE
);
700 if (flags
& QEMU_NET_PACKET_FLAG_RAW
&& nc
->info
->receive_raw
) {
701 ret
= nc
->info
->receive_raw(nc
, buffer
, offset
);
703 ret
= nc
->info
->receive(nc
, buffer
, offset
);
710 ssize_t
qemu_deliver_packet_iov(NetClientState
*sender
,
712 const struct iovec
*iov
,
716 NetClientState
*nc
= opaque
;
720 return iov_size(iov
, iovcnt
);
723 if (nc
->receive_disabled
) {
727 if (nc
->info
->receive_iov
) {
728 ret
= nc
->info
->receive_iov(nc
, iov
, iovcnt
);
730 ret
= nc_sendv_compat(nc
, iov
, iovcnt
, flags
);
734 nc
->receive_disabled
= 1;
740 ssize_t
qemu_sendv_packet_async(NetClientState
*sender
,
741 const struct iovec
*iov
, int iovcnt
,
742 NetPacketSent
*sent_cb
)
747 if (sender
->link_down
|| !sender
->peer
) {
748 return iov_size(iov
, iovcnt
);
751 /* Let filters handle the packet first */
752 ret
= filter_receive_iov(sender
, NET_FILTER_DIRECTION_TX
, sender
,
753 QEMU_NET_PACKET_FLAG_NONE
, iov
, iovcnt
, sent_cb
);
758 ret
= filter_receive_iov(sender
->peer
, NET_FILTER_DIRECTION_RX
, sender
,
759 QEMU_NET_PACKET_FLAG_NONE
, iov
, iovcnt
, sent_cb
);
764 queue
= sender
->peer
->incoming_queue
;
766 return qemu_net_queue_send_iov(queue
, sender
,
767 QEMU_NET_PACKET_FLAG_NONE
,
768 iov
, iovcnt
, sent_cb
);
772 qemu_sendv_packet(NetClientState
*nc
, const struct iovec
*iov
, int iovcnt
)
774 return qemu_sendv_packet_async(nc
, iov
, iovcnt
, NULL
);
777 NetClientState
*qemu_find_netdev(const char *id
)
781 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
782 if (nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_NIC
)
784 if (!strcmp(nc
->name
, id
)) {
792 int qemu_find_net_clients_except(const char *id
, NetClientState
**ncs
,
793 NetClientOptionsKind type
, int max
)
798 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
799 if (nc
->info
->type
== type
) {
802 if (!id
|| !strcmp(nc
->name
, id
)) {
813 static int nic_get_free_idx(void)
817 for (index
= 0; index
< MAX_NICS
; index
++)
818 if (!nd_table
[index
].used
)
823 int qemu_show_nic_models(const char *arg
, const char *const *models
)
827 if (!arg
|| !is_help_option(arg
)) {
831 fprintf(stderr
, "qemu: Supported NIC models: ");
832 for (i
= 0 ; models
[i
]; i
++)
833 fprintf(stderr
, "%s%c", models
[i
], models
[i
+1] ? ',' : '\n');
837 void qemu_check_nic_model(NICInfo
*nd
, const char *model
)
839 const char *models
[2];
844 if (qemu_show_nic_models(nd
->model
, models
))
846 if (qemu_find_nic_model(nd
, models
, model
) < 0)
850 int qemu_find_nic_model(NICInfo
*nd
, const char * const *models
,
851 const char *default_model
)
856 nd
->model
= g_strdup(default_model
);
858 for (i
= 0 ; models
[i
]; i
++) {
859 if (strcmp(nd
->model
, models
[i
]) == 0)
863 error_report("Unsupported NIC model: %s", nd
->model
);
867 static int net_init_nic(const NetClientOptions
*opts
, const char *name
,
868 NetClientState
*peer
, Error
**errp
)
872 const NetLegacyNicOptions
*nic
;
874 assert(opts
->type
== NET_CLIENT_OPTIONS_KIND_NIC
);
875 nic
= opts
->u
.nic
.data
;
877 idx
= nic_get_free_idx();
878 if (idx
== -1 || nb_nics
>= MAX_NICS
) {
879 error_setg(errp
, "too many NICs");
885 memset(nd
, 0, sizeof(*nd
));
887 if (nic
->has_netdev
) {
888 nd
->netdev
= qemu_find_netdev(nic
->netdev
);
890 error_setg(errp
, "netdev '%s' not found", nic
->netdev
);
897 nd
->name
= g_strdup(name
);
898 if (nic
->has_model
) {
899 nd
->model
= g_strdup(nic
->model
);
902 nd
->devaddr
= g_strdup(nic
->addr
);
905 if (nic
->has_macaddr
&&
906 net_parse_macaddr(nd
->macaddr
.a
, nic
->macaddr
) < 0) {
907 error_setg(errp
, "invalid syntax for ethernet address");
910 if (nic
->has_macaddr
&&
911 is_multicast_ether_addr(nd
->macaddr
.a
)) {
913 "NIC cannot have multicast MAC address (odd 1st byte)");
916 qemu_macaddr_default_if_unset(&nd
->macaddr
);
918 if (nic
->has_vectors
) {
919 if (nic
->vectors
> 0x7ffffff) {
920 error_setg(errp
, "invalid # of vectors: %"PRIu32
, nic
->vectors
);
923 nd
->nvectors
= nic
->vectors
;
925 nd
->nvectors
= DEV_NVECTORS_UNSPECIFIED
;
935 static int (* const net_client_init_fun
[NET_CLIENT_OPTIONS_KIND__MAX
])(
936 const NetClientOptions
*opts
,
938 NetClientState
*peer
, Error
**errp
) = {
939 [NET_CLIENT_OPTIONS_KIND_NIC
] = net_init_nic
,
941 [NET_CLIENT_OPTIONS_KIND_USER
] = net_init_slirp
,
943 [NET_CLIENT_OPTIONS_KIND_TAP
] = net_init_tap
,
944 [NET_CLIENT_OPTIONS_KIND_SOCKET
] = net_init_socket
,
946 [NET_CLIENT_OPTIONS_KIND_VDE
] = net_init_vde
,
949 [NET_CLIENT_OPTIONS_KIND_NETMAP
] = net_init_netmap
,
951 [NET_CLIENT_OPTIONS_KIND_DUMP
] = net_init_dump
,
952 #ifdef CONFIG_NET_BRIDGE
953 [NET_CLIENT_OPTIONS_KIND_BRIDGE
] = net_init_bridge
,
955 [NET_CLIENT_OPTIONS_KIND_HUBPORT
] = net_init_hubport
,
956 #ifdef CONFIG_VHOST_NET_USED
957 [NET_CLIENT_OPTIONS_KIND_VHOST_USER
] = net_init_vhost_user
,
960 [NET_CLIENT_OPTIONS_KIND_L2TPV3
] = net_init_l2tpv3
,
965 static int net_client_init1(const void *object
, int is_netdev
, Error
**errp
)
967 const NetClientOptions
*opts
;
969 NetClientState
*peer
= NULL
;
972 const Netdev
*netdev
= object
;
976 if (opts
->type
== NET_CLIENT_OPTIONS_KIND_DUMP
||
977 opts
->type
== NET_CLIENT_OPTIONS_KIND_NIC
||
978 !net_client_init_fun
[opts
->type
]) {
979 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "type",
980 "a netdev backend type");
984 const NetLegacy
*net
= object
;
986 /* missing optional values have been initialized to "all bits zero" */
987 name
= net
->has_id
? net
->id
: net
->name
;
989 if (opts
->type
== NET_CLIENT_OPTIONS_KIND_NONE
) {
990 return 0; /* nothing to do */
992 if (opts
->type
== NET_CLIENT_OPTIONS_KIND_HUBPORT
) {
993 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "type",
998 if (!net_client_init_fun
[opts
->type
]) {
999 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "type",
1000 "a net backend type (maybe it is not compiled "
1001 "into this binary)");
1005 /* Do not add to a vlan if it's a nic with a netdev= parameter. */
1006 if (opts
->type
!= NET_CLIENT_OPTIONS_KIND_NIC
||
1007 !opts
->u
.nic
.data
->has_netdev
) {
1008 peer
= net_hub_add_port(net
->has_vlan
? net
->vlan
: 0, NULL
);
1012 if (net_client_init_fun
[opts
->type
](opts
, name
, peer
, errp
) < 0) {
1013 /* FIXME drop when all init functions store an Error */
1014 if (errp
&& !*errp
) {
1015 error_setg(errp
, QERR_DEVICE_INIT_FAILED
,
1016 NetClientOptionsKind_lookup
[opts
->type
]);
1024 int net_client_init(QemuOpts
*opts
, int is_netdev
, Error
**errp
)
1026 void *object
= NULL
;
1029 OptsVisitor
*ov
= opts_visitor_new(opts
);
1030 Visitor
*v
= opts_get_visitor(ov
);
1033 /* Parse convenience option format ip6-net=fec0::0[/64] */
1034 const char *ip6_net
= qemu_opt_get(opts
, "ipv6-net");
1037 char buf
[strlen(ip6_net
) + 1];
1039 if (get_str_sep(buf
, sizeof(buf
), &ip6_net
, '/') < 0) {
1040 /* Default 64bit prefix length. */
1041 qemu_opt_set(opts
, "ipv6-prefix", ip6_net
, &error_abort
);
1042 qemu_opt_set_number(opts
, "ipv6-prefixlen", 64, &error_abort
);
1044 /* User-specified prefix length. */
1048 qemu_opt_set(opts
, "ipv6-prefix", buf
, &error_abort
);
1049 err
= qemu_strtoul(ip6_net
, NULL
, 10, &len
);
1052 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
1053 "ipv6-prefix", "a number");
1055 qemu_opt_set_number(opts
, "ipv6-prefixlen", len
,
1059 qemu_opt_unset(opts
, "ipv6-net");
1064 visit_type_Netdev(v
, NULL
, (Netdev
**)&object
, &err
);
1066 visit_type_NetLegacy(v
, NULL
, (NetLegacy
**)&object
, &err
);
1070 ret
= net_client_init1(object
, is_netdev
, &err
);
1074 qapi_free_Netdev(object
);
1076 qapi_free_NetLegacy(object
);
1079 error_propagate(errp
, err
);
1080 opts_visitor_cleanup(ov
);
1085 static int net_host_check_device(const char *device
)
1088 for (i
= 0; host_net_devices
[i
]; i
++) {
1089 if (!strncmp(host_net_devices
[i
], device
,
1090 strlen(host_net_devices
[i
]))) {
1098 void hmp_host_net_add(Monitor
*mon
, const QDict
*qdict
)
1100 const char *device
= qdict_get_str(qdict
, "device");
1101 const char *opts_str
= qdict_get_try_str(qdict
, "opts");
1102 Error
*local_err
= NULL
;
1105 if (!net_host_check_device(device
)) {
1106 monitor_printf(mon
, "invalid host network device %s\n", device
);
1110 opts
= qemu_opts_parse_noisily(qemu_find_opts("net"),
1111 opts_str
? opts_str
: "", false);
1116 qemu_opt_set(opts
, "type", device
, &error_abort
);
1118 net_client_init(opts
, 0, &local_err
);
1120 error_report_err(local_err
);
1121 monitor_printf(mon
, "adding host network device %s failed\n", device
);
1125 void hmp_host_net_remove(Monitor
*mon
, const QDict
*qdict
)
1128 int vlan_id
= qdict_get_int(qdict
, "vlan_id");
1129 const char *device
= qdict_get_str(qdict
, "device");
1131 nc
= net_hub_find_client_by_name(vlan_id
, device
);
1133 error_report("Host network device '%s' on hub '%d' not found",
1137 if (nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_NIC
) {
1138 error_report("invalid host network device '%s'", device
);
1142 qemu_del_net_client(nc
->peer
);
1143 qemu_del_net_client(nc
);
1146 void netdev_add(QemuOpts
*opts
, Error
**errp
)
1148 net_client_init(opts
, 1, errp
);
1151 void qmp_netdev_add(QDict
*qdict
, QObject
**ret
, Error
**errp
)
1153 Error
*local_err
= NULL
;
1154 QemuOptsList
*opts_list
;
1157 opts_list
= qemu_find_opts_err("netdev", &local_err
);
1162 opts
= qemu_opts_from_qdict(opts_list
, qdict
, &local_err
);
1167 netdev_add(opts
, &local_err
);
1169 qemu_opts_del(opts
);
1174 error_propagate(errp
, local_err
);
1177 void qmp_netdev_del(const char *id
, Error
**errp
)
1182 nc
= qemu_find_netdev(id
);
1184 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1185 "Device '%s' not found", id
);
1189 opts
= qemu_opts_find(qemu_find_opts_err("netdev", NULL
), id
);
1191 error_setg(errp
, "Device '%s' is not a netdev", id
);
1195 qemu_del_net_client(nc
);
1196 qemu_opts_del(opts
);
1199 static void netfilter_print_info(Monitor
*mon
, NetFilterState
*nf
)
1202 ObjectProperty
*prop
;
1203 ObjectPropertyIterator iter
;
1204 StringOutputVisitor
*ov
;
1206 /* generate info str */
1207 object_property_iter_init(&iter
, OBJECT(nf
));
1208 while ((prop
= object_property_iter_next(&iter
))) {
1209 if (!strcmp(prop
->name
, "type")) {
1212 ov
= string_output_visitor_new(false);
1213 object_property_get(OBJECT(nf
), string_output_get_visitor(ov
),
1215 str
= string_output_get_string(ov
);
1216 string_output_visitor_cleanup(ov
);
1217 monitor_printf(mon
, ",%s=%s", prop
->name
, str
);
1220 monitor_printf(mon
, "\n");
1223 void print_net_client(Monitor
*mon
, NetClientState
*nc
)
1227 monitor_printf(mon
, "%s: index=%d,type=%s,%s\n", nc
->name
,
1229 NetClientOptionsKind_lookup
[nc
->info
->type
],
1231 if (!QTAILQ_EMPTY(&nc
->filters
)) {
1232 monitor_printf(mon
, "filters:\n");
1234 QTAILQ_FOREACH(nf
, &nc
->filters
, next
) {
1235 char *path
= object_get_canonical_path_component(OBJECT(nf
));
1237 monitor_printf(mon
, " - %s: type=%s", path
,
1238 object_get_typename(OBJECT(nf
)));
1239 netfilter_print_info(mon
, nf
);
1244 RxFilterInfoList
*qmp_query_rx_filter(bool has_name
, const char *name
,
1248 RxFilterInfoList
*filter_list
= NULL
, *last_entry
= NULL
;
1250 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
1251 RxFilterInfoList
*entry
;
1254 if (has_name
&& strcmp(nc
->name
, name
) != 0) {
1258 /* only query rx-filter information of NIC */
1259 if (nc
->info
->type
!= NET_CLIENT_OPTIONS_KIND_NIC
) {
1261 error_setg(errp
, "net client(%s) isn't a NIC", name
);
1267 /* only query information on queue 0 since the info is per nic,
1270 if (nc
->queue_index
!= 0)
1273 if (nc
->info
->query_rx_filter
) {
1274 info
= nc
->info
->query_rx_filter(nc
);
1275 entry
= g_malloc0(sizeof(*entry
));
1276 entry
->value
= info
;
1279 filter_list
= entry
;
1281 last_entry
->next
= entry
;
1284 } else if (has_name
) {
1285 error_setg(errp
, "net client(%s) doesn't support"
1286 " rx-filter querying", name
);
1295 if (filter_list
== NULL
&& has_name
) {
1296 error_setg(errp
, "invalid net client name: %s", name
);
1302 void hmp_info_network(Monitor
*mon
, const QDict
*qdict
)
1304 NetClientState
*nc
, *peer
;
1305 NetClientOptionsKind type
;
1309 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
1311 type
= nc
->info
->type
;
1313 /* Skip if already printed in hub info */
1314 if (net_hub_id_for_client(nc
, NULL
) == 0) {
1318 if (!peer
|| type
== NET_CLIENT_OPTIONS_KIND_NIC
) {
1319 print_net_client(mon
, nc
);
1320 } /* else it's a netdev connected to a NIC, printed with the NIC */
1321 if (peer
&& type
== NET_CLIENT_OPTIONS_KIND_NIC
) {
1322 monitor_printf(mon
, " \\ ");
1323 print_net_client(mon
, peer
);
1328 void qmp_set_link(const char *name
, bool up
, Error
**errp
)
1330 NetClientState
*ncs
[MAX_QUEUE_NUM
];
1334 queues
= qemu_find_net_clients_except(name
, ncs
,
1335 NET_CLIENT_OPTIONS_KIND__MAX
,
1339 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1340 "Device '%s' not found", name
);
1345 for (i
= 0; i
< queues
; i
++) {
1346 ncs
[i
]->link_down
= !up
;
1349 if (nc
->info
->link_status_changed
) {
1350 nc
->info
->link_status_changed(nc
);
1354 /* Change peer link only if the peer is NIC and then notify peer.
1355 * If the peer is a HUBPORT or a backend, we do not change the
1358 * This behavior is compatible with qemu vlans where there could be
1359 * multiple clients that can still communicate with each other in
1360 * disconnected mode. For now maintain this compatibility.
1362 if (nc
->peer
->info
->type
== NET_CLIENT_OPTIONS_KIND_NIC
) {
1363 for (i
= 0; i
< queues
; i
++) {
1364 ncs
[i
]->peer
->link_down
= !up
;
1367 if (nc
->peer
->info
->link_status_changed
) {
1368 nc
->peer
->info
->link_status_changed(nc
->peer
);
1373 static void net_vm_change_state_handler(void *opaque
, int running
,
1377 NetClientState
*tmp
;
1379 QTAILQ_FOREACH_SAFE(nc
, &net_clients
, next
, tmp
) {
1381 /* Flush queued packets and wake up backends. */
1382 if (nc
->peer
&& qemu_can_send_packet(nc
)) {
1383 qemu_flush_queued_packets(nc
->peer
);
1386 /* Complete all queued packets, to guarantee we don't modify
1387 * state later when VM is not running.
1389 qemu_flush_or_purge_queued_packets(nc
, true);
1394 void net_cleanup(void)
1398 /* We may del multiple entries during qemu_del_net_client(),
1399 * so QTAILQ_FOREACH_SAFE() is also not safe here.
1401 while (!QTAILQ_EMPTY(&net_clients
)) {
1402 nc
= QTAILQ_FIRST(&net_clients
);
1403 if (nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_NIC
) {
1404 qemu_del_nic(qemu_get_nic(nc
));
1406 qemu_del_net_client(nc
);
1410 qemu_del_vm_change_state_handler(net_change_state_entry
);
1413 void net_check_clients(void)
1418 /* Don't warn about the default network setup that you get if
1419 * no command line -net or -netdev options are specified. There
1420 * are two cases that we would otherwise complain about:
1421 * (1) board doesn't support a NIC but the implicit "-net nic"
1423 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
1424 * sets up a nic that isn't connected to anything.
1430 net_hub_check_clients();
1432 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
1434 fprintf(stderr
, "Warning: %s %s has no peer\n",
1435 nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_NIC
?
1436 "nic" : "netdev", nc
->name
);
1440 /* Check that all NICs requested via -net nic actually got created.
1441 * NICs created via -device don't need to be checked here because
1442 * they are always instantiated.
1444 for (i
= 0; i
< MAX_NICS
; i
++) {
1445 NICInfo
*nd
= &nd_table
[i
];
1446 if (nd
->used
&& !nd
->instantiated
) {
1447 fprintf(stderr
, "Warning: requested NIC (%s, model %s) "
1448 "was not created (not supported by this machine?)\n",
1449 nd
->name
? nd
->name
: "anonymous",
1450 nd
->model
? nd
->model
: "unspecified");
1455 static int net_init_client(void *dummy
, QemuOpts
*opts
, Error
**errp
)
1457 Error
*local_err
= NULL
;
1459 net_client_init(opts
, 0, &local_err
);
1461 error_report_err(local_err
);
1468 static int net_init_netdev(void *dummy
, QemuOpts
*opts
, Error
**errp
)
1470 Error
*local_err
= NULL
;
1473 ret
= net_client_init(opts
, 1, &local_err
);
1475 error_report_err(local_err
);
1482 int net_init_clients(void)
1484 QemuOptsList
*net
= qemu_find_opts("net");
1487 /* if no clients, we use a default config */
1488 qemu_opts_set(net
, NULL
, "type", "nic", &error_abort
);
1490 qemu_opts_set(net
, NULL
, "type", "user", &error_abort
);
1494 net_change_state_entry
=
1495 qemu_add_vm_change_state_handler(net_vm_change_state_handler
, NULL
);
1497 QTAILQ_INIT(&net_clients
);
1499 if (qemu_opts_foreach(qemu_find_opts("netdev"),
1500 net_init_netdev
, NULL
, NULL
)) {
1504 if (qemu_opts_foreach(net
, net_init_client
, NULL
, NULL
)) {
1511 int net_client_parse(QemuOptsList
*opts_list
, const char *optarg
)
1513 #if defined(CONFIG_SLIRP)
1515 if (net_slirp_parse_legacy(opts_list
, optarg
, &ret
)) {
1520 if (!qemu_opts_parse_noisily(opts_list
, optarg
, true)) {
1530 unsigned compute_mcast_idx(const uint8_t *ep
)
1537 for (i
= 0; i
< 6; i
++) {
1539 for (j
= 0; j
< 8; j
++) {
1540 carry
= ((crc
& 0x80000000L
) ? 1 : 0) ^ (b
& 0x01);
1544 crc
= ((crc
^ POLYNOMIAL
) | carry
);
1551 QemuOptsList qemu_netdev_opts
= {
1553 .implied_opt_name
= "type",
1554 .head
= QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts
.head
),
1557 * no elements => accept any params
1558 * validation will happen later
1560 { /* end of list */ }
1564 QemuOptsList qemu_net_opts
= {
1566 .implied_opt_name
= "type",
1567 .head
= QTAILQ_HEAD_INITIALIZER(qemu_net_opts
.head
),
1570 * no elements => accept any params
1571 * validation will happen later
1573 { /* end of list */ }