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
79 /***********************************************************/
80 /* network device redirectors */
82 static int get_str_sep(char *buf
, int buf_size
, const char **pp
, int sep
)
93 if (len
> buf_size
- 1)
102 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
)
110 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
112 saddr
->sin_family
= AF_INET
;
113 if (buf
[0] == '\0') {
114 saddr
->sin_addr
.s_addr
= 0;
116 if (qemu_isdigit(buf
[0])) {
117 if (!inet_aton(buf
, &saddr
->sin_addr
))
120 if ((he
= gethostbyname(buf
)) == NULL
)
122 saddr
->sin_addr
= *(struct in_addr
*)he
->h_addr
;
125 port
= strtol(p
, (char **)&r
, 0);
128 saddr
->sin_port
= htons(port
);
132 char *qemu_mac_strdup_printf(const uint8_t *macaddr
)
134 return g_strdup_printf("%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",
135 macaddr
[0], macaddr
[1], macaddr
[2],
136 macaddr
[3], macaddr
[4], macaddr
[5]);
139 void qemu_format_nic_info_str(NetClientState
*nc
, uint8_t macaddr
[6])
141 snprintf(nc
->info_str
, sizeof(nc
->info_str
),
142 "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
144 macaddr
[0], macaddr
[1], macaddr
[2],
145 macaddr
[3], macaddr
[4], macaddr
[5]);
148 static int mac_table
[256] = {0};
150 static void qemu_macaddr_set_used(MACAddr
*macaddr
)
154 for (index
= 0x56; index
< 0xFF; index
++) {
155 if (macaddr
->a
[5] == index
) {
161 static void qemu_macaddr_set_free(MACAddr
*macaddr
)
164 static const MACAddr base
= { .a
= { 0x52, 0x54, 0x00, 0x12, 0x34, 0 } };
166 if (memcmp(macaddr
->a
, &base
.a
, (sizeof(base
.a
) - 1)) != 0) {
169 for (index
= 0x56; index
< 0xFF; index
++) {
170 if (macaddr
->a
[5] == index
) {
176 static int qemu_macaddr_get_free(void)
180 for (index
= 0x56; index
< 0xFF; index
++) {
181 if (mac_table
[index
] == 0) {
189 void qemu_macaddr_default_if_unset(MACAddr
*macaddr
)
191 static const MACAddr zero
= { .a
= { 0,0,0,0,0,0 } };
192 static const MACAddr base
= { .a
= { 0x52, 0x54, 0x00, 0x12, 0x34, 0 } };
194 if (memcmp(macaddr
, &zero
, sizeof(zero
)) != 0) {
195 if (memcmp(macaddr
->a
, &base
.a
, (sizeof(base
.a
) - 1)) != 0) {
198 qemu_macaddr_set_used(macaddr
);
203 macaddr
->a
[0] = 0x52;
204 macaddr
->a
[1] = 0x54;
205 macaddr
->a
[2] = 0x00;
206 macaddr
->a
[3] = 0x12;
207 macaddr
->a
[4] = 0x34;
208 macaddr
->a
[5] = qemu_macaddr_get_free();
209 qemu_macaddr_set_used(macaddr
);
213 * Generate a name for net client
215 * Only net clients created with the legacy -net option and NICs need this.
217 static char *assign_name(NetClientState
*nc1
, const char *model
)
222 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
226 if (strcmp(nc
->model
, model
) == 0) {
231 return g_strdup_printf("%s.%d", model
, id
);
234 static void qemu_net_client_destructor(NetClientState
*nc
)
239 static void qemu_net_client_setup(NetClientState
*nc
,
241 NetClientState
*peer
,
244 NetClientDestructor
*destructor
)
247 nc
->model
= g_strdup(model
);
249 nc
->name
= g_strdup(name
);
251 nc
->name
= assign_name(nc
, model
);
259 QTAILQ_INSERT_TAIL(&net_clients
, nc
, next
);
261 nc
->incoming_queue
= qemu_new_net_queue(qemu_deliver_packet_iov
, nc
);
262 nc
->destructor
= destructor
;
263 QTAILQ_INIT(&nc
->filters
);
266 NetClientState
*qemu_new_net_client(NetClientInfo
*info
,
267 NetClientState
*peer
,
273 assert(info
->size
>= sizeof(NetClientState
));
275 nc
= g_malloc0(info
->size
);
276 qemu_net_client_setup(nc
, info
, peer
, model
, name
,
277 qemu_net_client_destructor
);
282 NICState
*qemu_new_nic(NetClientInfo
*info
,
288 NetClientState
**peers
= conf
->peers
.ncs
;
290 int i
, queues
= MAX(1, conf
->peers
.queues
);
292 assert(info
->type
== NET_CLIENT_DRIVER_NIC
);
293 assert(info
->size
>= sizeof(NICState
));
295 nic
= g_malloc0(info
->size
+ sizeof(NetClientState
) * queues
);
296 nic
->ncs
= (void *)nic
+ info
->size
;
298 nic
->opaque
= opaque
;
300 for (i
= 0; i
< queues
; i
++) {
301 qemu_net_client_setup(&nic
->ncs
[i
], info
, peers
[i
], model
, name
,
303 nic
->ncs
[i
].queue_index
= i
;
309 NetClientState
*qemu_get_subqueue(NICState
*nic
, int queue_index
)
311 return nic
->ncs
+ queue_index
;
314 NetClientState
*qemu_get_queue(NICState
*nic
)
316 return qemu_get_subqueue(nic
, 0);
319 NICState
*qemu_get_nic(NetClientState
*nc
)
321 NetClientState
*nc0
= nc
- nc
->queue_index
;
323 return (NICState
*)((void *)nc0
- nc
->info
->size
);
326 void *qemu_get_nic_opaque(NetClientState
*nc
)
328 NICState
*nic
= qemu_get_nic(nc
);
333 static void qemu_cleanup_net_client(NetClientState
*nc
)
335 QTAILQ_REMOVE(&net_clients
, nc
, next
);
337 if (nc
->info
->cleanup
) {
338 nc
->info
->cleanup(nc
);
342 static void qemu_free_net_client(NetClientState
*nc
)
344 if (nc
->incoming_queue
) {
345 qemu_del_net_queue(nc
->incoming_queue
);
348 nc
->peer
->peer
= NULL
;
352 if (nc
->destructor
) {
357 void qemu_del_net_client(NetClientState
*nc
)
359 NetClientState
*ncs
[MAX_QUEUE_NUM
];
361 NetFilterState
*nf
, *next
;
363 assert(nc
->info
->type
!= NET_CLIENT_DRIVER_NIC
);
365 /* If the NetClientState belongs to a multiqueue backend, we will change all
366 * other NetClientStates also.
368 queues
= qemu_find_net_clients_except(nc
->name
, ncs
,
369 NET_CLIENT_DRIVER_NIC
,
373 QTAILQ_FOREACH_SAFE(nf
, &nc
->filters
, next
, next
) {
374 object_unparent(OBJECT(nf
));
377 /* If there is a peer NIC, delete and cleanup client, but do not free. */
378 if (nc
->peer
&& nc
->peer
->info
->type
== NET_CLIENT_DRIVER_NIC
) {
379 NICState
*nic
= qemu_get_nic(nc
->peer
);
380 if (nic
->peer_deleted
) {
383 nic
->peer_deleted
= true;
385 for (i
= 0; i
< queues
; i
++) {
386 ncs
[i
]->peer
->link_down
= true;
389 if (nc
->peer
->info
->link_status_changed
) {
390 nc
->peer
->info
->link_status_changed(nc
->peer
);
393 for (i
= 0; i
< queues
; i
++) {
394 qemu_cleanup_net_client(ncs
[i
]);
400 for (i
= 0; i
< queues
; i
++) {
401 qemu_cleanup_net_client(ncs
[i
]);
402 qemu_free_net_client(ncs
[i
]);
406 void qemu_del_nic(NICState
*nic
)
408 int i
, queues
= MAX(nic
->conf
->peers
.queues
, 1);
410 qemu_macaddr_set_free(&nic
->conf
->macaddr
);
412 /* If this is a peer NIC and peer has already been deleted, free it now. */
413 if (nic
->peer_deleted
) {
414 for (i
= 0; i
< queues
; i
++) {
415 qemu_free_net_client(qemu_get_subqueue(nic
, i
)->peer
);
419 for (i
= queues
- 1; i
>= 0; i
--) {
420 NetClientState
*nc
= qemu_get_subqueue(nic
, i
);
422 qemu_cleanup_net_client(nc
);
423 qemu_free_net_client(nc
);
429 void qemu_foreach_nic(qemu_nic_foreach func
, void *opaque
)
433 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
434 if (nc
->info
->type
== NET_CLIENT_DRIVER_NIC
) {
435 if (nc
->queue_index
== 0) {
436 func(qemu_get_nic(nc
), opaque
);
442 bool qemu_has_ufo(NetClientState
*nc
)
444 if (!nc
|| !nc
->info
->has_ufo
) {
448 return nc
->info
->has_ufo(nc
);
451 bool qemu_has_vnet_hdr(NetClientState
*nc
)
453 if (!nc
|| !nc
->info
->has_vnet_hdr
) {
457 return nc
->info
->has_vnet_hdr(nc
);
460 bool qemu_has_vnet_hdr_len(NetClientState
*nc
, int len
)
462 if (!nc
|| !nc
->info
->has_vnet_hdr_len
) {
466 return nc
->info
->has_vnet_hdr_len(nc
, len
);
469 void qemu_using_vnet_hdr(NetClientState
*nc
, bool enable
)
471 if (!nc
|| !nc
->info
->using_vnet_hdr
) {
475 nc
->info
->using_vnet_hdr(nc
, enable
);
478 void qemu_set_offload(NetClientState
*nc
, int csum
, int tso4
, int tso6
,
481 if (!nc
|| !nc
->info
->set_offload
) {
485 nc
->info
->set_offload(nc
, csum
, tso4
, tso6
, ecn
, ufo
);
488 void qemu_set_vnet_hdr_len(NetClientState
*nc
, int len
)
490 if (!nc
|| !nc
->info
->set_vnet_hdr_len
) {
494 nc
->info
->set_vnet_hdr_len(nc
, len
);
497 int qemu_set_vnet_le(NetClientState
*nc
, bool is_le
)
499 #ifdef HOST_WORDS_BIGENDIAN
500 if (!nc
|| !nc
->info
->set_vnet_le
) {
504 return nc
->info
->set_vnet_le(nc
, is_le
);
510 int qemu_set_vnet_be(NetClientState
*nc
, bool is_be
)
512 #ifdef HOST_WORDS_BIGENDIAN
515 if (!nc
|| !nc
->info
->set_vnet_be
) {
519 return nc
->info
->set_vnet_be(nc
, is_be
);
523 int qemu_can_send_packet(NetClientState
*sender
)
525 int vm_running
= runstate_is_running();
535 if (sender
->peer
->receive_disabled
) {
537 } else if (sender
->peer
->info
->can_receive
&&
538 !sender
->peer
->info
->can_receive(sender
->peer
)) {
544 static ssize_t
filter_receive_iov(NetClientState
*nc
,
545 NetFilterDirection direction
,
546 NetClientState
*sender
,
548 const struct iovec
*iov
,
550 NetPacketSent
*sent_cb
)
553 NetFilterState
*nf
= NULL
;
555 if (direction
== NET_FILTER_DIRECTION_TX
) {
556 QTAILQ_FOREACH(nf
, &nc
->filters
, next
) {
557 ret
= qemu_netfilter_receive(nf
, direction
, sender
, flags
, iov
,
564 QTAILQ_FOREACH_REVERSE(nf
, &nc
->filters
, NetFilterHead
, next
) {
565 ret
= qemu_netfilter_receive(nf
, direction
, sender
, flags
, iov
,
576 static ssize_t
filter_receive(NetClientState
*nc
,
577 NetFilterDirection direction
,
578 NetClientState
*sender
,
582 NetPacketSent
*sent_cb
)
585 .iov_base
= (void *)data
,
589 return filter_receive_iov(nc
, direction
, sender
, flags
, &iov
, 1, sent_cb
);
592 void qemu_purge_queued_packets(NetClientState
*nc
)
598 qemu_net_queue_purge(nc
->peer
->incoming_queue
, nc
);
602 void qemu_flush_or_purge_queued_packets(NetClientState
*nc
, bool purge
)
604 nc
->receive_disabled
= 0;
606 if (nc
->peer
&& nc
->peer
->info
->type
== NET_CLIENT_DRIVER_HUBPORT
) {
607 if (net_hub_flush(nc
->peer
)) {
611 if (qemu_net_queue_flush(nc
->incoming_queue
)) {
612 /* We emptied the queue successfully, signal to the IO thread to repoll
613 * the file descriptor (for tap, for example).
617 /* Unable to empty the queue, purge remaining packets */
618 qemu_net_queue_purge(nc
->incoming_queue
, nc
);
622 void qemu_flush_queued_packets(NetClientState
*nc
)
624 qemu_flush_or_purge_queued_packets(nc
, false);
627 static ssize_t
qemu_send_packet_async_with_flags(NetClientState
*sender
,
629 const uint8_t *buf
, int size
,
630 NetPacketSent
*sent_cb
)
636 printf("qemu_send_packet_async:\n");
637 qemu_hexdump((const char *)buf
, stdout
, "net", size
);
640 if (sender
->link_down
|| !sender
->peer
) {
644 /* Let filters handle the packet first */
645 ret
= filter_receive(sender
, NET_FILTER_DIRECTION_TX
,
646 sender
, flags
, buf
, size
, sent_cb
);
651 ret
= filter_receive(sender
->peer
, NET_FILTER_DIRECTION_RX
,
652 sender
, flags
, buf
, size
, sent_cb
);
657 queue
= sender
->peer
->incoming_queue
;
659 return qemu_net_queue_send(queue
, sender
, flags
, buf
, size
, sent_cb
);
662 ssize_t
qemu_send_packet_async(NetClientState
*sender
,
663 const uint8_t *buf
, int size
,
664 NetPacketSent
*sent_cb
)
666 return qemu_send_packet_async_with_flags(sender
, QEMU_NET_PACKET_FLAG_NONE
,
670 void qemu_send_packet(NetClientState
*nc
, const uint8_t *buf
, int size
)
672 qemu_send_packet_async(nc
, buf
, size
, NULL
);
675 ssize_t
qemu_send_packet_raw(NetClientState
*nc
, const uint8_t *buf
, int size
)
677 return qemu_send_packet_async_with_flags(nc
, QEMU_NET_PACKET_FLAG_RAW
,
681 static ssize_t
nc_sendv_compat(NetClientState
*nc
, const struct iovec
*iov
,
682 int iovcnt
, unsigned flags
)
690 buffer
= iov
[0].iov_base
;
691 offset
= iov
[0].iov_len
;
693 buf
= g_new(uint8_t, NET_BUFSIZE
);
695 offset
= iov_to_buf(iov
, iovcnt
, 0, buf
, NET_BUFSIZE
);
698 if (flags
& QEMU_NET_PACKET_FLAG_RAW
&& nc
->info
->receive_raw
) {
699 ret
= nc
->info
->receive_raw(nc
, buffer
, offset
);
701 ret
= nc
->info
->receive(nc
, buffer
, offset
);
708 ssize_t
qemu_deliver_packet_iov(NetClientState
*sender
,
710 const struct iovec
*iov
,
714 NetClientState
*nc
= opaque
;
718 return iov_size(iov
, iovcnt
);
721 if (nc
->receive_disabled
) {
725 if (nc
->info
->receive_iov
&& !(flags
& QEMU_NET_PACKET_FLAG_RAW
)) {
726 ret
= nc
->info
->receive_iov(nc
, iov
, iovcnt
);
728 ret
= nc_sendv_compat(nc
, iov
, iovcnt
, flags
);
732 nc
->receive_disabled
= 1;
738 ssize_t
qemu_sendv_packet_async(NetClientState
*sender
,
739 const struct iovec
*iov
, int iovcnt
,
740 NetPacketSent
*sent_cb
)
745 if (sender
->link_down
|| !sender
->peer
) {
746 return iov_size(iov
, iovcnt
);
749 /* Let filters handle the packet first */
750 ret
= filter_receive_iov(sender
, NET_FILTER_DIRECTION_TX
, sender
,
751 QEMU_NET_PACKET_FLAG_NONE
, iov
, iovcnt
, sent_cb
);
756 ret
= filter_receive_iov(sender
->peer
, NET_FILTER_DIRECTION_RX
, sender
,
757 QEMU_NET_PACKET_FLAG_NONE
, iov
, iovcnt
, sent_cb
);
762 queue
= sender
->peer
->incoming_queue
;
764 return qemu_net_queue_send_iov(queue
, sender
,
765 QEMU_NET_PACKET_FLAG_NONE
,
766 iov
, iovcnt
, sent_cb
);
770 qemu_sendv_packet(NetClientState
*nc
, const struct iovec
*iov
, int iovcnt
)
772 return qemu_sendv_packet_async(nc
, iov
, iovcnt
, NULL
);
775 NetClientState
*qemu_find_netdev(const char *id
)
779 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
780 if (nc
->info
->type
== NET_CLIENT_DRIVER_NIC
)
782 if (!strcmp(nc
->name
, id
)) {
790 int qemu_find_net_clients_except(const char *id
, NetClientState
**ncs
,
791 NetClientDriver type
, int max
)
796 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
797 if (nc
->info
->type
== type
) {
800 if (!id
|| !strcmp(nc
->name
, id
)) {
811 static int nic_get_free_idx(void)
815 for (index
= 0; index
< MAX_NICS
; index
++)
816 if (!nd_table
[index
].used
)
821 int qemu_show_nic_models(const char *arg
, const char *const *models
)
825 if (!arg
|| !is_help_option(arg
)) {
829 fprintf(stderr
, "qemu: Supported NIC models: ");
830 for (i
= 0 ; models
[i
]; i
++)
831 fprintf(stderr
, "%s%c", models
[i
], models
[i
+1] ? ',' : '\n');
835 void qemu_check_nic_model(NICInfo
*nd
, const char *model
)
837 const char *models
[2];
842 if (qemu_show_nic_models(nd
->model
, models
))
844 if (qemu_find_nic_model(nd
, models
, model
) < 0)
848 int qemu_find_nic_model(NICInfo
*nd
, const char * const *models
,
849 const char *default_model
)
854 nd
->model
= g_strdup(default_model
);
856 for (i
= 0 ; models
[i
]; i
++) {
857 if (strcmp(nd
->model
, models
[i
]) == 0)
861 error_report("Unsupported NIC model: %s", nd
->model
);
865 static int net_init_nic(const Netdev
*netdev
, const char *name
,
866 NetClientState
*peer
, Error
**errp
)
870 const NetLegacyNicOptions
*nic
;
872 assert(netdev
->type
== NET_CLIENT_DRIVER_NIC
);
873 nic
= &netdev
->u
.nic
;
875 idx
= nic_get_free_idx();
876 if (idx
== -1 || nb_nics
>= MAX_NICS
) {
877 error_setg(errp
, "too many NICs");
883 memset(nd
, 0, sizeof(*nd
));
885 if (nic
->has_netdev
) {
886 nd
->netdev
= qemu_find_netdev(nic
->netdev
);
888 error_setg(errp
, "netdev '%s' not found", nic
->netdev
);
895 nd
->name
= g_strdup(name
);
896 if (nic
->has_model
) {
897 nd
->model
= g_strdup(nic
->model
);
900 nd
->devaddr
= g_strdup(nic
->addr
);
903 if (nic
->has_macaddr
&&
904 net_parse_macaddr(nd
->macaddr
.a
, nic
->macaddr
) < 0) {
905 error_setg(errp
, "invalid syntax for ethernet address");
908 if (nic
->has_macaddr
&&
909 is_multicast_ether_addr(nd
->macaddr
.a
)) {
911 "NIC cannot have multicast MAC address (odd 1st byte)");
914 qemu_macaddr_default_if_unset(&nd
->macaddr
);
916 if (nic
->has_vectors
) {
917 if (nic
->vectors
> 0x7ffffff) {
918 error_setg(errp
, "invalid # of vectors: %"PRIu32
, nic
->vectors
);
921 nd
->nvectors
= nic
->vectors
;
923 nd
->nvectors
= DEV_NVECTORS_UNSPECIFIED
;
933 static int (* const net_client_init_fun
[NET_CLIENT_DRIVER__MAX
])(
934 const Netdev
*netdev
,
936 NetClientState
*peer
, Error
**errp
) = {
937 [NET_CLIENT_DRIVER_NIC
] = net_init_nic
,
939 [NET_CLIENT_DRIVER_USER
] = net_init_slirp
,
941 [NET_CLIENT_DRIVER_TAP
] = net_init_tap
,
942 [NET_CLIENT_DRIVER_SOCKET
] = net_init_socket
,
944 [NET_CLIENT_DRIVER_VDE
] = net_init_vde
,
947 [NET_CLIENT_DRIVER_NETMAP
] = net_init_netmap
,
949 [NET_CLIENT_DRIVER_DUMP
] = net_init_dump
,
950 #ifdef CONFIG_NET_BRIDGE
951 [NET_CLIENT_DRIVER_BRIDGE
] = net_init_bridge
,
953 [NET_CLIENT_DRIVER_HUBPORT
] = net_init_hubport
,
954 #ifdef CONFIG_VHOST_NET_USED
955 [NET_CLIENT_DRIVER_VHOST_USER
] = net_init_vhost_user
,
958 [NET_CLIENT_DRIVER_L2TPV3
] = net_init_l2tpv3
,
963 static int net_client_init1(const void *object
, int is_netdev
, Error
**errp
)
966 const Netdev
*netdev
;
968 NetClientState
*peer
= NULL
;
974 if (netdev
->type
== NET_CLIENT_DRIVER_DUMP
||
975 netdev
->type
== NET_CLIENT_DRIVER_NIC
||
976 !net_client_init_fun
[netdev
->type
]) {
977 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "type",
978 "a netdev backend type");
982 const NetLegacy
*net
= object
;
983 const NetLegacyOptions
*opts
= net
->opts
;
986 /* missing optional values have been initialized to "all bits zero" */
987 name
= net
->has_id
? net
->id
: net
->name
;
989 /* Map the old options to the new flat type */
990 switch (opts
->type
) {
991 case NET_LEGACY_OPTIONS_KIND_NONE
:
992 return 0; /* nothing to do */
993 case NET_LEGACY_OPTIONS_KIND_NIC
:
994 legacy
.type
= NET_CLIENT_DRIVER_NIC
;
995 legacy
.u
.nic
= *opts
->u
.nic
.data
;
997 case NET_LEGACY_OPTIONS_KIND_USER
:
998 legacy
.type
= NET_CLIENT_DRIVER_USER
;
999 legacy
.u
.user
= *opts
->u
.user
.data
;
1001 case NET_LEGACY_OPTIONS_KIND_TAP
:
1002 legacy
.type
= NET_CLIENT_DRIVER_TAP
;
1003 legacy
.u
.tap
= *opts
->u
.tap
.data
;
1005 case NET_LEGACY_OPTIONS_KIND_L2TPV3
:
1006 legacy
.type
= NET_CLIENT_DRIVER_L2TPV3
;
1007 legacy
.u
.l2tpv3
= *opts
->u
.l2tpv3
.data
;
1009 case NET_LEGACY_OPTIONS_KIND_SOCKET
:
1010 legacy
.type
= NET_CLIENT_DRIVER_SOCKET
;
1011 legacy
.u
.socket
= *opts
->u
.socket
.data
;
1013 case NET_LEGACY_OPTIONS_KIND_VDE
:
1014 legacy
.type
= NET_CLIENT_DRIVER_VDE
;
1015 legacy
.u
.vde
= *opts
->u
.vde
.data
;
1017 case NET_LEGACY_OPTIONS_KIND_DUMP
:
1018 legacy
.type
= NET_CLIENT_DRIVER_DUMP
;
1019 legacy
.u
.dump
= *opts
->u
.dump
.data
;
1021 case NET_LEGACY_OPTIONS_KIND_BRIDGE
:
1022 legacy
.type
= NET_CLIENT_DRIVER_BRIDGE
;
1023 legacy
.u
.bridge
= *opts
->u
.bridge
.data
;
1025 case NET_LEGACY_OPTIONS_KIND_NETMAP
:
1026 legacy
.type
= NET_CLIENT_DRIVER_NETMAP
;
1027 legacy
.u
.netmap
= *opts
->u
.netmap
.data
;
1029 case NET_LEGACY_OPTIONS_KIND_VHOST_USER
:
1030 legacy
.type
= NET_CLIENT_DRIVER_VHOST_USER
;
1031 legacy
.u
.vhost_user
= *opts
->u
.vhost_user
.data
;
1037 if (!net_client_init_fun
[netdev
->type
]) {
1038 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "type",
1039 "a net backend type (maybe it is not compiled "
1040 "into this binary)");
1044 /* Do not add to a vlan if it's a nic with a netdev= parameter. */
1045 if (netdev
->type
!= NET_CLIENT_DRIVER_NIC
||
1046 !opts
->u
.nic
.data
->has_netdev
) {
1047 peer
= net_hub_add_port(net
->has_vlan
? net
->vlan
: 0, NULL
);
1051 if (net_client_init_fun
[netdev
->type
](netdev
, name
, peer
, errp
) < 0) {
1052 /* FIXME drop when all init functions store an Error */
1053 if (errp
&& !*errp
) {
1054 error_setg(errp
, QERR_DEVICE_INIT_FAILED
,
1055 NetClientDriver_lookup
[netdev
->type
]);
1063 int net_client_init(QemuOpts
*opts
, int is_netdev
, Error
**errp
)
1065 void *object
= NULL
;
1068 Visitor
*v
= opts_visitor_new(opts
);
1071 /* Parse convenience option format ip6-net=fec0::0[/64] */
1072 const char *ip6_net
= qemu_opt_get(opts
, "ipv6-net");
1075 char buf
[strlen(ip6_net
) + 1];
1077 if (get_str_sep(buf
, sizeof(buf
), &ip6_net
, '/') < 0) {
1078 /* Default 64bit prefix length. */
1079 qemu_opt_set(opts
, "ipv6-prefix", ip6_net
, &error_abort
);
1080 qemu_opt_set_number(opts
, "ipv6-prefixlen", 64, &error_abort
);
1082 /* User-specified prefix length. */
1086 qemu_opt_set(opts
, "ipv6-prefix", buf
, &error_abort
);
1087 err
= qemu_strtoul(ip6_net
, NULL
, 10, &len
);
1090 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
1091 "ipv6-prefix", "a number");
1093 qemu_opt_set_number(opts
, "ipv6-prefixlen", len
,
1097 qemu_opt_unset(opts
, "ipv6-net");
1102 visit_type_Netdev(v
, NULL
, (Netdev
**)&object
, &err
);
1104 visit_type_NetLegacy(v
, NULL
, (NetLegacy
**)&object
, &err
);
1108 ret
= net_client_init1(object
, is_netdev
, &err
);
1112 qapi_free_Netdev(object
);
1114 qapi_free_NetLegacy(object
);
1117 error_propagate(errp
, err
);
1123 static int net_host_check_device(const char *device
)
1126 for (i
= 0; host_net_devices
[i
]; i
++) {
1127 if (!strncmp(host_net_devices
[i
], device
,
1128 strlen(host_net_devices
[i
]))) {
1136 void hmp_host_net_add(Monitor
*mon
, const QDict
*qdict
)
1138 const char *device
= qdict_get_str(qdict
, "device");
1139 const char *opts_str
= qdict_get_try_str(qdict
, "opts");
1140 Error
*local_err
= NULL
;
1143 if (!net_host_check_device(device
)) {
1144 monitor_printf(mon
, "invalid host network device %s\n", device
);
1148 opts
= qemu_opts_parse_noisily(qemu_find_opts("net"),
1149 opts_str
? opts_str
: "", false);
1154 qemu_opt_set(opts
, "type", device
, &error_abort
);
1156 net_client_init(opts
, 0, &local_err
);
1158 error_report_err(local_err
);
1159 monitor_printf(mon
, "adding host network device %s failed\n", device
);
1163 void hmp_host_net_remove(Monitor
*mon
, const QDict
*qdict
)
1166 int vlan_id
= qdict_get_int(qdict
, "vlan_id");
1167 const char *device
= qdict_get_str(qdict
, "device");
1169 nc
= net_hub_find_client_by_name(vlan_id
, device
);
1171 error_report("Host network device '%s' on hub '%d' not found",
1175 if (nc
->info
->type
== NET_CLIENT_DRIVER_NIC
) {
1176 error_report("invalid host network device '%s'", device
);
1180 qemu_del_net_client(nc
->peer
);
1181 qemu_del_net_client(nc
);
1184 void netdev_add(QemuOpts
*opts
, Error
**errp
)
1186 net_client_init(opts
, 1, errp
);
1189 void qmp_netdev_add(QDict
*qdict
, QObject
**ret
, Error
**errp
)
1191 Error
*local_err
= NULL
;
1192 QemuOptsList
*opts_list
;
1195 opts_list
= qemu_find_opts_err("netdev", &local_err
);
1200 opts
= qemu_opts_from_qdict(opts_list
, qdict
, &local_err
);
1205 netdev_add(opts
, &local_err
);
1207 qemu_opts_del(opts
);
1212 error_propagate(errp
, local_err
);
1215 void qmp_netdev_del(const char *id
, Error
**errp
)
1220 nc
= qemu_find_netdev(id
);
1222 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1223 "Device '%s' not found", id
);
1227 opts
= qemu_opts_find(qemu_find_opts_err("netdev", NULL
), id
);
1229 error_setg(errp
, "Device '%s' is not a netdev", id
);
1233 qemu_del_net_client(nc
);
1234 qemu_opts_del(opts
);
1237 static void netfilter_print_info(Monitor
*mon
, NetFilterState
*nf
)
1240 ObjectProperty
*prop
;
1241 ObjectPropertyIterator iter
;
1244 /* generate info str */
1245 object_property_iter_init(&iter
, OBJECT(nf
));
1246 while ((prop
= object_property_iter_next(&iter
))) {
1247 if (!strcmp(prop
->name
, "type")) {
1250 v
= string_output_visitor_new(false, &str
);
1251 object_property_get(OBJECT(nf
), v
, prop
->name
, NULL
);
1252 visit_complete(v
, &str
);
1254 monitor_printf(mon
, ",%s=%s", prop
->name
, str
);
1257 monitor_printf(mon
, "\n");
1260 void print_net_client(Monitor
*mon
, NetClientState
*nc
)
1264 monitor_printf(mon
, "%s: index=%d,type=%s,%s\n", nc
->name
,
1266 NetClientDriver_lookup
[nc
->info
->type
],
1268 if (!QTAILQ_EMPTY(&nc
->filters
)) {
1269 monitor_printf(mon
, "filters:\n");
1271 QTAILQ_FOREACH(nf
, &nc
->filters
, next
) {
1272 char *path
= object_get_canonical_path_component(OBJECT(nf
));
1274 monitor_printf(mon
, " - %s: type=%s", path
,
1275 object_get_typename(OBJECT(nf
)));
1276 netfilter_print_info(mon
, nf
);
1281 RxFilterInfoList
*qmp_query_rx_filter(bool has_name
, const char *name
,
1285 RxFilterInfoList
*filter_list
= NULL
, *last_entry
= NULL
;
1287 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
1288 RxFilterInfoList
*entry
;
1291 if (has_name
&& strcmp(nc
->name
, name
) != 0) {
1295 /* only query rx-filter information of NIC */
1296 if (nc
->info
->type
!= NET_CLIENT_DRIVER_NIC
) {
1298 error_setg(errp
, "net client(%s) isn't a NIC", name
);
1304 /* only query information on queue 0 since the info is per nic,
1307 if (nc
->queue_index
!= 0)
1310 if (nc
->info
->query_rx_filter
) {
1311 info
= nc
->info
->query_rx_filter(nc
);
1312 entry
= g_malloc0(sizeof(*entry
));
1313 entry
->value
= info
;
1316 filter_list
= entry
;
1318 last_entry
->next
= entry
;
1321 } else if (has_name
) {
1322 error_setg(errp
, "net client(%s) doesn't support"
1323 " rx-filter querying", name
);
1332 if (filter_list
== NULL
&& has_name
) {
1333 error_setg(errp
, "invalid net client name: %s", name
);
1339 void hmp_info_network(Monitor
*mon
, const QDict
*qdict
)
1341 NetClientState
*nc
, *peer
;
1342 NetClientDriver type
;
1346 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
1348 type
= nc
->info
->type
;
1350 /* Skip if already printed in hub info */
1351 if (net_hub_id_for_client(nc
, NULL
) == 0) {
1355 if (!peer
|| type
== NET_CLIENT_DRIVER_NIC
) {
1356 print_net_client(mon
, nc
);
1357 } /* else it's a netdev connected to a NIC, printed with the NIC */
1358 if (peer
&& type
== NET_CLIENT_DRIVER_NIC
) {
1359 monitor_printf(mon
, " \\ ");
1360 print_net_client(mon
, peer
);
1365 void qmp_set_link(const char *name
, bool up
, Error
**errp
)
1367 NetClientState
*ncs
[MAX_QUEUE_NUM
];
1371 queues
= qemu_find_net_clients_except(name
, ncs
,
1372 NET_CLIENT_DRIVER__MAX
,
1376 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1377 "Device '%s' not found", name
);
1382 for (i
= 0; i
< queues
; i
++) {
1383 ncs
[i
]->link_down
= !up
;
1386 if (nc
->info
->link_status_changed
) {
1387 nc
->info
->link_status_changed(nc
);
1391 /* Change peer link only if the peer is NIC and then notify peer.
1392 * If the peer is a HUBPORT or a backend, we do not change the
1395 * This behavior is compatible with qemu vlans where there could be
1396 * multiple clients that can still communicate with each other in
1397 * disconnected mode. For now maintain this compatibility.
1399 if (nc
->peer
->info
->type
== NET_CLIENT_DRIVER_NIC
) {
1400 for (i
= 0; i
< queues
; i
++) {
1401 ncs
[i
]->peer
->link_down
= !up
;
1404 if (nc
->peer
->info
->link_status_changed
) {
1405 nc
->peer
->info
->link_status_changed(nc
->peer
);
1410 static void net_vm_change_state_handler(void *opaque
, int running
,
1414 NetClientState
*tmp
;
1416 QTAILQ_FOREACH_SAFE(nc
, &net_clients
, next
, tmp
) {
1418 /* Flush queued packets and wake up backends. */
1419 if (nc
->peer
&& qemu_can_send_packet(nc
)) {
1420 qemu_flush_queued_packets(nc
->peer
);
1423 /* Complete all queued packets, to guarantee we don't modify
1424 * state later when VM is not running.
1426 qemu_flush_or_purge_queued_packets(nc
, true);
1431 void net_cleanup(void)
1435 /* We may del multiple entries during qemu_del_net_client(),
1436 * so QTAILQ_FOREACH_SAFE() is also not safe here.
1438 while (!QTAILQ_EMPTY(&net_clients
)) {
1439 nc
= QTAILQ_FIRST(&net_clients
);
1440 if (nc
->info
->type
== NET_CLIENT_DRIVER_NIC
) {
1441 qemu_del_nic(qemu_get_nic(nc
));
1443 qemu_del_net_client(nc
);
1447 qemu_del_vm_change_state_handler(net_change_state_entry
);
1450 void net_check_clients(void)
1455 net_hub_check_clients();
1457 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
1459 fprintf(stderr
, "Warning: %s %s has no peer\n",
1460 nc
->info
->type
== NET_CLIENT_DRIVER_NIC
?
1461 "nic" : "netdev", nc
->name
);
1465 /* Check that all NICs requested via -net nic actually got created.
1466 * NICs created via -device don't need to be checked here because
1467 * they are always instantiated.
1469 for (i
= 0; i
< MAX_NICS
; i
++) {
1470 NICInfo
*nd
= &nd_table
[i
];
1471 if (nd
->used
&& !nd
->instantiated
) {
1472 fprintf(stderr
, "Warning: requested NIC (%s, model %s) "
1473 "was not created (not supported by this machine?)\n",
1474 nd
->name
? nd
->name
: "anonymous",
1475 nd
->model
? nd
->model
: "unspecified");
1480 static int net_init_client(void *dummy
, QemuOpts
*opts
, Error
**errp
)
1482 Error
*local_err
= NULL
;
1484 net_client_init(opts
, 0, &local_err
);
1486 error_report_err(local_err
);
1493 static int net_init_netdev(void *dummy
, QemuOpts
*opts
, Error
**errp
)
1495 Error
*local_err
= NULL
;
1498 ret
= net_client_init(opts
, 1, &local_err
);
1500 error_report_err(local_err
);
1507 int net_init_clients(void)
1509 QemuOptsList
*net
= qemu_find_opts("net");
1511 net_change_state_entry
=
1512 qemu_add_vm_change_state_handler(net_vm_change_state_handler
, NULL
);
1514 QTAILQ_INIT(&net_clients
);
1516 if (qemu_opts_foreach(qemu_find_opts("netdev"),
1517 net_init_netdev
, NULL
, NULL
)) {
1521 if (qemu_opts_foreach(net
, net_init_client
, NULL
, NULL
)) {
1528 int net_client_parse(QemuOptsList
*opts_list
, const char *optarg
)
1530 #if defined(CONFIG_SLIRP)
1532 if (net_slirp_parse_legacy(opts_list
, optarg
, &ret
)) {
1537 if (!qemu_opts_parse_noisily(opts_list
, optarg
, true)) {
1546 unsigned compute_mcast_idx(const uint8_t *ep
)
1553 for (i
= 0; i
< 6; i
++) {
1555 for (j
= 0; j
< 8; j
++) {
1556 carry
= ((crc
& 0x80000000L
) ? 1 : 0) ^ (b
& 0x01);
1560 crc
= ((crc
^ POLYNOMIAL
) | carry
);
1567 QemuOptsList qemu_netdev_opts
= {
1569 .implied_opt_name
= "type",
1570 .head
= QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts
.head
),
1573 * no elements => accept any params
1574 * validation will happen later
1576 { /* end of list */ }
1580 QemuOptsList qemu_net_opts
= {
1582 .implied_opt_name
= "type",
1583 .head
= QTAILQ_HEAD_INITIALIZER(qemu_net_opts
.head
),
1586 * no elements => accept any params
1587 * validation will happen later
1589 { /* end of list */ }
1593 void net_socket_rs_init(SocketReadState
*rs
,
1594 SocketReadStateFinalize
*finalize
)
1599 memset(rs
->buf
, 0, sizeof(rs
->buf
));
1600 rs
->finalize
= finalize
;
1605 * 0: SocketReadState is not ready
1606 * 1: SocketReadState is ready
1607 * otherwise error occurs
1609 int net_fill_rstate(SocketReadState
*rs
, const uint8_t *buf
, int size
)
1614 /* reassemble a packet from the network */
1615 switch (rs
->state
) { /* 0 = getting length, 1 = getting data */
1621 memcpy(rs
->buf
+ rs
->index
, buf
, l
);
1625 if (rs
->index
== 4) {
1627 rs
->packet_len
= ntohl(*(uint32_t *)rs
->buf
);
1633 l
= rs
->packet_len
- rs
->index
;
1637 if (rs
->index
+ l
<= sizeof(rs
->buf
)) {
1638 memcpy(rs
->buf
+ rs
->index
, buf
, l
);
1640 fprintf(stderr
, "serious error: oversized packet received,"
1641 "connection terminated.\n");
1642 rs
->index
= rs
->state
= 0;
1649 if (rs
->index
>= rs
->packet_len
) {