4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 #include "qemu/osdep.h"
30 #include "net/slirp.h"
34 #include "monitor/monitor.h"
35 #include "qemu/help_option.h"
36 #include "qapi/qapi-commands-net.h"
37 #include "qapi/qapi-visit-net.h"
38 #include "qapi/qmp/qdict.h"
39 #include "qapi/qmp/qerror.h"
40 #include "qemu/error-report.h"
41 #include "qemu/sockets.h"
42 #include "qemu/cutils.h"
43 #include "qemu/config-file.h"
46 #include "qemu/main-loop.h"
47 #include "qemu/option.h"
48 #include "qapi/error.h"
49 #include "qapi/opts-visitor.h"
50 #include "sysemu/sysemu.h"
51 #include "sysemu/qtest.h"
52 #include "net/filter.h"
53 #include "qapi/string-output-visitor.h"
55 /* Net bridge is currently not supported for W32. */
57 # define CONFIG_NET_BRIDGE
60 static VMChangeStateEntry
*net_change_state_entry
;
61 static QTAILQ_HEAD(, NetClientState
) net_clients
;
63 /***********************************************************/
64 /* network device redirectors */
66 static int get_str_sep(char *buf
, int buf_size
, const char **pp
, int sep
)
77 if (len
> buf_size
- 1)
86 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
,
95 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0) {
96 error_setg(errp
, "host address '%s' doesn't contain ':' "
97 "separating host from port", str
);
100 saddr
->sin_family
= AF_INET
;
101 if (buf
[0] == '\0') {
102 saddr
->sin_addr
.s_addr
= 0;
104 if (qemu_isdigit(buf
[0])) {
105 if (!inet_aton(buf
, &saddr
->sin_addr
)) {
106 error_setg(errp
, "host address '%s' is not a valid "
107 "IPv4 address", buf
);
111 he
= gethostbyname(buf
);
113 error_setg(errp
, "can't resolve host address '%s'", buf
);
116 saddr
->sin_addr
= *(struct in_addr
*)he
->h_addr
;
119 port
= strtol(p
, (char **)&r
, 0);
121 error_setg(errp
, "port number '%s' is invalid", p
);
124 saddr
->sin_port
= htons(port
);
128 char *qemu_mac_strdup_printf(const uint8_t *macaddr
)
130 return g_strdup_printf("%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",
131 macaddr
[0], macaddr
[1], macaddr
[2],
132 macaddr
[3], macaddr
[4], macaddr
[5]);
135 void qemu_format_nic_info_str(NetClientState
*nc
, uint8_t macaddr
[6])
137 snprintf(nc
->info_str
, sizeof(nc
->info_str
),
138 "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
140 macaddr
[0], macaddr
[1], macaddr
[2],
141 macaddr
[3], macaddr
[4], macaddr
[5]);
144 static int mac_table
[256] = {0};
146 static void qemu_macaddr_set_used(MACAddr
*macaddr
)
150 for (index
= 0x56; index
< 0xFF; index
++) {
151 if (macaddr
->a
[5] == index
) {
157 static void qemu_macaddr_set_free(MACAddr
*macaddr
)
160 static const MACAddr base
= { .a
= { 0x52, 0x54, 0x00, 0x12, 0x34, 0 } };
162 if (memcmp(macaddr
->a
, &base
.a
, (sizeof(base
.a
) - 1)) != 0) {
165 for (index
= 0x56; index
< 0xFF; index
++) {
166 if (macaddr
->a
[5] == index
) {
172 static int qemu_macaddr_get_free(void)
176 for (index
= 0x56; index
< 0xFF; index
++) {
177 if (mac_table
[index
] == 0) {
185 void qemu_macaddr_default_if_unset(MACAddr
*macaddr
)
187 static const MACAddr zero
= { .a
= { 0,0,0,0,0,0 } };
188 static const MACAddr base
= { .a
= { 0x52, 0x54, 0x00, 0x12, 0x34, 0 } };
190 if (memcmp(macaddr
, &zero
, sizeof(zero
)) != 0) {
191 if (memcmp(macaddr
->a
, &base
.a
, (sizeof(base
.a
) - 1)) != 0) {
194 qemu_macaddr_set_used(macaddr
);
199 macaddr
->a
[0] = 0x52;
200 macaddr
->a
[1] = 0x54;
201 macaddr
->a
[2] = 0x00;
202 macaddr
->a
[3] = 0x12;
203 macaddr
->a
[4] = 0x34;
204 macaddr
->a
[5] = qemu_macaddr_get_free();
205 qemu_macaddr_set_used(macaddr
);
209 * Generate a name for net client
211 * Only net clients created with the legacy -net option and NICs need this.
213 static char *assign_name(NetClientState
*nc1
, const char *model
)
218 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
222 if (strcmp(nc
->model
, model
) == 0) {
227 return g_strdup_printf("%s.%d", model
, id
);
230 static void qemu_net_client_destructor(NetClientState
*nc
)
235 static void qemu_net_client_setup(NetClientState
*nc
,
237 NetClientState
*peer
,
240 NetClientDestructor
*destructor
)
243 nc
->model
= g_strdup(model
);
245 nc
->name
= g_strdup(name
);
247 nc
->name
= assign_name(nc
, model
);
255 QTAILQ_INSERT_TAIL(&net_clients
, nc
, next
);
257 nc
->incoming_queue
= qemu_new_net_queue(qemu_deliver_packet_iov
, nc
);
258 nc
->destructor
= destructor
;
259 QTAILQ_INIT(&nc
->filters
);
262 NetClientState
*qemu_new_net_client(NetClientInfo
*info
,
263 NetClientState
*peer
,
269 assert(info
->size
>= sizeof(NetClientState
));
271 nc
= g_malloc0(info
->size
);
272 qemu_net_client_setup(nc
, info
, peer
, model
, name
,
273 qemu_net_client_destructor
);
278 NICState
*qemu_new_nic(NetClientInfo
*info
,
284 NetClientState
**peers
= conf
->peers
.ncs
;
286 int i
, queues
= MAX(1, conf
->peers
.queues
);
288 assert(info
->type
== NET_CLIENT_DRIVER_NIC
);
289 assert(info
->size
>= sizeof(NICState
));
291 nic
= g_malloc0(info
->size
+ sizeof(NetClientState
) * queues
);
292 nic
->ncs
= (void *)nic
+ info
->size
;
294 nic
->opaque
= opaque
;
296 for (i
= 0; i
< queues
; i
++) {
297 qemu_net_client_setup(&nic
->ncs
[i
], info
, peers
[i
], model
, name
,
299 nic
->ncs
[i
].queue_index
= i
;
305 NetClientState
*qemu_get_subqueue(NICState
*nic
, int queue_index
)
307 return nic
->ncs
+ queue_index
;
310 NetClientState
*qemu_get_queue(NICState
*nic
)
312 return qemu_get_subqueue(nic
, 0);
315 NICState
*qemu_get_nic(NetClientState
*nc
)
317 NetClientState
*nc0
= nc
- nc
->queue_index
;
319 return (NICState
*)((void *)nc0
- nc
->info
->size
);
322 void *qemu_get_nic_opaque(NetClientState
*nc
)
324 NICState
*nic
= qemu_get_nic(nc
);
329 static void qemu_cleanup_net_client(NetClientState
*nc
)
331 QTAILQ_REMOVE(&net_clients
, nc
, next
);
333 if (nc
->info
->cleanup
) {
334 nc
->info
->cleanup(nc
);
338 static void qemu_free_net_client(NetClientState
*nc
)
340 if (nc
->incoming_queue
) {
341 qemu_del_net_queue(nc
->incoming_queue
);
344 nc
->peer
->peer
= NULL
;
348 if (nc
->destructor
) {
353 void qemu_del_net_client(NetClientState
*nc
)
355 NetClientState
*ncs
[MAX_QUEUE_NUM
];
357 NetFilterState
*nf
, *next
;
359 assert(nc
->info
->type
!= NET_CLIENT_DRIVER_NIC
);
361 /* If the NetClientState belongs to a multiqueue backend, we will change all
362 * other NetClientStates also.
364 queues
= qemu_find_net_clients_except(nc
->name
, ncs
,
365 NET_CLIENT_DRIVER_NIC
,
369 QTAILQ_FOREACH_SAFE(nf
, &nc
->filters
, next
, next
) {
370 object_unparent(OBJECT(nf
));
373 /* If there is a peer NIC, delete and cleanup client, but do not free. */
374 if (nc
->peer
&& nc
->peer
->info
->type
== NET_CLIENT_DRIVER_NIC
) {
375 NICState
*nic
= qemu_get_nic(nc
->peer
);
376 if (nic
->peer_deleted
) {
379 nic
->peer_deleted
= true;
381 for (i
= 0; i
< queues
; i
++) {
382 ncs
[i
]->peer
->link_down
= true;
385 if (nc
->peer
->info
->link_status_changed
) {
386 nc
->peer
->info
->link_status_changed(nc
->peer
);
389 for (i
= 0; i
< queues
; i
++) {
390 qemu_cleanup_net_client(ncs
[i
]);
396 for (i
= 0; i
< queues
; i
++) {
397 qemu_cleanup_net_client(ncs
[i
]);
398 qemu_free_net_client(ncs
[i
]);
402 void qemu_del_nic(NICState
*nic
)
404 int i
, queues
= MAX(nic
->conf
->peers
.queues
, 1);
406 qemu_macaddr_set_free(&nic
->conf
->macaddr
);
408 /* If this is a peer NIC and peer has already been deleted, free it now. */
409 if (nic
->peer_deleted
) {
410 for (i
= 0; i
< queues
; i
++) {
411 qemu_free_net_client(qemu_get_subqueue(nic
, i
)->peer
);
415 for (i
= queues
- 1; i
>= 0; i
--) {
416 NetClientState
*nc
= qemu_get_subqueue(nic
, i
);
418 qemu_cleanup_net_client(nc
);
419 qemu_free_net_client(nc
);
425 void qemu_foreach_nic(qemu_nic_foreach func
, void *opaque
)
429 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
430 if (nc
->info
->type
== NET_CLIENT_DRIVER_NIC
) {
431 if (nc
->queue_index
== 0) {
432 func(qemu_get_nic(nc
), opaque
);
438 bool qemu_has_ufo(NetClientState
*nc
)
440 if (!nc
|| !nc
->info
->has_ufo
) {
444 return nc
->info
->has_ufo(nc
);
447 bool qemu_has_vnet_hdr(NetClientState
*nc
)
449 if (!nc
|| !nc
->info
->has_vnet_hdr
) {
453 return nc
->info
->has_vnet_hdr(nc
);
456 bool qemu_has_vnet_hdr_len(NetClientState
*nc
, int len
)
458 if (!nc
|| !nc
->info
->has_vnet_hdr_len
) {
462 return nc
->info
->has_vnet_hdr_len(nc
, len
);
465 void qemu_using_vnet_hdr(NetClientState
*nc
, bool enable
)
467 if (!nc
|| !nc
->info
->using_vnet_hdr
) {
471 nc
->info
->using_vnet_hdr(nc
, enable
);
474 void qemu_set_offload(NetClientState
*nc
, int csum
, int tso4
, int tso6
,
477 if (!nc
|| !nc
->info
->set_offload
) {
481 nc
->info
->set_offload(nc
, csum
, tso4
, tso6
, ecn
, ufo
);
484 void qemu_set_vnet_hdr_len(NetClientState
*nc
, int len
)
486 if (!nc
|| !nc
->info
->set_vnet_hdr_len
) {
490 nc
->vnet_hdr_len
= len
;
491 nc
->info
->set_vnet_hdr_len(nc
, len
);
494 int qemu_set_vnet_le(NetClientState
*nc
, bool is_le
)
496 #ifdef HOST_WORDS_BIGENDIAN
497 if (!nc
|| !nc
->info
->set_vnet_le
) {
501 return nc
->info
->set_vnet_le(nc
, is_le
);
507 int qemu_set_vnet_be(NetClientState
*nc
, bool is_be
)
509 #ifdef HOST_WORDS_BIGENDIAN
512 if (!nc
|| !nc
->info
->set_vnet_be
) {
516 return nc
->info
->set_vnet_be(nc
, is_be
);
520 int qemu_can_send_packet(NetClientState
*sender
)
522 int vm_running
= runstate_is_running();
532 if (sender
->peer
->receive_disabled
) {
534 } else if (sender
->peer
->info
->can_receive
&&
535 !sender
->peer
->info
->can_receive(sender
->peer
)) {
541 static ssize_t
filter_receive_iov(NetClientState
*nc
,
542 NetFilterDirection direction
,
543 NetClientState
*sender
,
545 const struct iovec
*iov
,
547 NetPacketSent
*sent_cb
)
550 NetFilterState
*nf
= NULL
;
552 if (direction
== NET_FILTER_DIRECTION_TX
) {
553 QTAILQ_FOREACH(nf
, &nc
->filters
, next
) {
554 ret
= qemu_netfilter_receive(nf
, direction
, sender
, flags
, iov
,
561 QTAILQ_FOREACH_REVERSE(nf
, &nc
->filters
, NetFilterHead
, next
) {
562 ret
= qemu_netfilter_receive(nf
, direction
, sender
, flags
, iov
,
573 static ssize_t
filter_receive(NetClientState
*nc
,
574 NetFilterDirection direction
,
575 NetClientState
*sender
,
579 NetPacketSent
*sent_cb
)
582 .iov_base
= (void *)data
,
586 return filter_receive_iov(nc
, direction
, sender
, flags
, &iov
, 1, sent_cb
);
589 void qemu_purge_queued_packets(NetClientState
*nc
)
595 qemu_net_queue_purge(nc
->peer
->incoming_queue
, nc
);
598 void qemu_flush_or_purge_queued_packets(NetClientState
*nc
, bool purge
)
600 nc
->receive_disabled
= 0;
602 if (nc
->peer
&& nc
->peer
->info
->type
== NET_CLIENT_DRIVER_HUBPORT
) {
603 if (net_hub_flush(nc
->peer
)) {
607 if (qemu_net_queue_flush(nc
->incoming_queue
)) {
608 /* We emptied the queue successfully, signal to the IO thread to repoll
609 * the file descriptor (for tap, for example).
613 /* Unable to empty the queue, purge remaining packets */
614 qemu_net_queue_purge(nc
->incoming_queue
, nc
);
618 void qemu_flush_queued_packets(NetClientState
*nc
)
620 qemu_flush_or_purge_queued_packets(nc
, false);
623 static ssize_t
qemu_send_packet_async_with_flags(NetClientState
*sender
,
625 const uint8_t *buf
, int size
,
626 NetPacketSent
*sent_cb
)
632 printf("qemu_send_packet_async:\n");
633 qemu_hexdump((const char *)buf
, stdout
, "net", size
);
636 if (sender
->link_down
|| !sender
->peer
) {
640 /* Let filters handle the packet first */
641 ret
= filter_receive(sender
, NET_FILTER_DIRECTION_TX
,
642 sender
, flags
, buf
, size
, sent_cb
);
647 ret
= filter_receive(sender
->peer
, NET_FILTER_DIRECTION_RX
,
648 sender
, flags
, buf
, size
, sent_cb
);
653 queue
= sender
->peer
->incoming_queue
;
655 return qemu_net_queue_send(queue
, sender
, flags
, buf
, size
, sent_cb
);
658 ssize_t
qemu_send_packet_async(NetClientState
*sender
,
659 const uint8_t *buf
, int size
,
660 NetPacketSent
*sent_cb
)
662 return qemu_send_packet_async_with_flags(sender
, QEMU_NET_PACKET_FLAG_NONE
,
666 void qemu_send_packet(NetClientState
*nc
, const uint8_t *buf
, int size
)
668 qemu_send_packet_async(nc
, buf
, size
, NULL
);
671 ssize_t
qemu_send_packet_raw(NetClientState
*nc
, const uint8_t *buf
, int size
)
673 return qemu_send_packet_async_with_flags(nc
, QEMU_NET_PACKET_FLAG_RAW
,
677 static ssize_t
nc_sendv_compat(NetClientState
*nc
, const struct iovec
*iov
,
678 int iovcnt
, unsigned flags
)
686 buffer
= iov
[0].iov_base
;
687 offset
= iov
[0].iov_len
;
689 offset
= iov_size(iov
, iovcnt
);
690 if (offset
> NET_BUFSIZE
) {
693 buf
= g_malloc(offset
);
695 offset
= iov_to_buf(iov
, iovcnt
, 0, buf
, offset
);
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 #ifdef CONFIG_NET_BRIDGE
950 [NET_CLIENT_DRIVER_BRIDGE
] = net_init_bridge
,
952 [NET_CLIENT_DRIVER_HUBPORT
] = net_init_hubport
,
953 #ifdef CONFIG_VHOST_NET_USED
954 [NET_CLIENT_DRIVER_VHOST_USER
] = net_init_vhost_user
,
957 [NET_CLIENT_DRIVER_L2TPV3
] = net_init_l2tpv3
,
962 static int net_client_init1(const void *object
, bool is_netdev
, Error
**errp
)
965 const Netdev
*netdev
;
967 NetClientState
*peer
= NULL
;
968 static bool vlan_warned
;
974 if (netdev
->type
== NET_CLIENT_DRIVER_NIC
||
975 !net_client_init_fun
[netdev
->type
]) {
976 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "type",
977 "a netdev backend type");
981 const NetLegacy
*net
= object
;
982 const NetLegacyOptions
*opts
= net
->opts
;
985 /* missing optional values have been initialized to "all bits zero" */
986 name
= net
->has_id
? net
->id
: net
->name
;
988 /* Map the old options to the new flat type */
989 switch (opts
->type
) {
990 case NET_LEGACY_OPTIONS_TYPE_NONE
:
991 return 0; /* nothing to do */
992 case NET_LEGACY_OPTIONS_TYPE_NIC
:
993 legacy
.type
= NET_CLIENT_DRIVER_NIC
;
994 legacy
.u
.nic
= opts
->u
.nic
;
996 case NET_LEGACY_OPTIONS_TYPE_USER
:
997 legacy
.type
= NET_CLIENT_DRIVER_USER
;
998 legacy
.u
.user
= opts
->u
.user
;
1000 case NET_LEGACY_OPTIONS_TYPE_TAP
:
1001 legacy
.type
= NET_CLIENT_DRIVER_TAP
;
1002 legacy
.u
.tap
= opts
->u
.tap
;
1004 case NET_LEGACY_OPTIONS_TYPE_L2TPV3
:
1005 legacy
.type
= NET_CLIENT_DRIVER_L2TPV3
;
1006 legacy
.u
.l2tpv3
= opts
->u
.l2tpv3
;
1008 case NET_LEGACY_OPTIONS_TYPE_SOCKET
:
1009 legacy
.type
= NET_CLIENT_DRIVER_SOCKET
;
1010 legacy
.u
.socket
= opts
->u
.socket
;
1012 case NET_LEGACY_OPTIONS_TYPE_VDE
:
1013 legacy
.type
= NET_CLIENT_DRIVER_VDE
;
1014 legacy
.u
.vde
= opts
->u
.vde
;
1016 case NET_LEGACY_OPTIONS_TYPE_BRIDGE
:
1017 legacy
.type
= NET_CLIENT_DRIVER_BRIDGE
;
1018 legacy
.u
.bridge
= opts
->u
.bridge
;
1020 case NET_LEGACY_OPTIONS_TYPE_NETMAP
:
1021 legacy
.type
= NET_CLIENT_DRIVER_NETMAP
;
1022 legacy
.u
.netmap
= opts
->u
.netmap
;
1024 case NET_LEGACY_OPTIONS_TYPE_VHOST_USER
:
1025 legacy
.type
= NET_CLIENT_DRIVER_VHOST_USER
;
1026 legacy
.u
.vhost_user
= opts
->u
.vhost_user
;
1032 if (!net_client_init_fun
[netdev
->type
]) {
1033 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "type",
1034 "a net backend type (maybe it is not compiled "
1035 "into this binary)");
1039 /* Do not add to a vlan if it's a nic with a netdev= parameter. */
1040 if (netdev
->type
!= NET_CLIENT_DRIVER_NIC
||
1041 !opts
->u
.nic
.has_netdev
) {
1042 peer
= net_hub_add_port(net
->has_vlan
? net
->vlan
: 0, NULL
, NULL
);
1045 if (net
->has_vlan
&& !vlan_warned
) {
1046 error_report("'vlan' is deprecated. Please use 'netdev' instead.");
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_str(netdev
->type
));
1062 static void show_netdevs(void)
1065 const char *available_netdevs
[] = {
1072 #ifdef CONFIG_L2TPV3
1078 #ifdef CONFIG_NET_BRIDGE
1081 #ifdef CONFIG_NETMAP
1089 printf("Available netdev backend types:\n");
1090 for (idx
= 0; idx
< ARRAY_SIZE(available_netdevs
); idx
++) {
1091 puts(available_netdevs
[idx
]);
1095 static int net_client_init(QemuOpts
*opts
, bool is_netdev
, Error
**errp
)
1097 void *object
= NULL
;
1100 Visitor
*v
= opts_visitor_new(opts
);
1102 if (is_netdev
&& is_help_option(qemu_opt_get(opts
, "type"))) {
1106 /* Parse convenience option format ip6-net=fec0::0[/64] */
1107 const char *ip6_net
= qemu_opt_get(opts
, "ipv6-net");
1110 char buf
[strlen(ip6_net
) + 1];
1112 if (get_str_sep(buf
, sizeof(buf
), &ip6_net
, '/') < 0) {
1113 /* Default 64bit prefix length. */
1114 qemu_opt_set(opts
, "ipv6-prefix", ip6_net
, &error_abort
);
1115 qemu_opt_set_number(opts
, "ipv6-prefixlen", 64, &error_abort
);
1117 /* User-specified prefix length. */
1121 qemu_opt_set(opts
, "ipv6-prefix", buf
, &error_abort
);
1122 err
= qemu_strtoul(ip6_net
, NULL
, 10, &len
);
1125 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
1126 "ipv6-prefix", "a number");
1128 qemu_opt_set_number(opts
, "ipv6-prefixlen", len
,
1132 qemu_opt_unset(opts
, "ipv6-net");
1137 visit_type_Netdev(v
, NULL
, (Netdev
**)&object
, &err
);
1139 visit_type_NetLegacy(v
, NULL
, (NetLegacy
**)&object
, &err
);
1143 ret
= net_client_init1(object
, is_netdev
, &err
);
1147 qapi_free_Netdev(object
);
1149 qapi_free_NetLegacy(object
);
1152 error_propagate(errp
, err
);
1157 void netdev_add(QemuOpts
*opts
, Error
**errp
)
1159 net_client_init(opts
, true, errp
);
1162 void qmp_netdev_add(QDict
*qdict
, QObject
**ret
, Error
**errp
)
1164 Error
*local_err
= NULL
;
1165 QemuOptsList
*opts_list
;
1168 opts_list
= qemu_find_opts_err("netdev", &local_err
);
1173 opts
= qemu_opts_from_qdict(opts_list
, qdict
, &local_err
);
1178 netdev_add(opts
, &local_err
);
1180 qemu_opts_del(opts
);
1185 error_propagate(errp
, local_err
);
1188 void qmp_netdev_del(const char *id
, Error
**errp
)
1193 nc
= qemu_find_netdev(id
);
1195 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1196 "Device '%s' not found", id
);
1200 opts
= qemu_opts_find(qemu_find_opts_err("netdev", NULL
), id
);
1202 error_setg(errp
, "Device '%s' is not a netdev", id
);
1206 qemu_del_net_client(nc
);
1207 qemu_opts_del(opts
);
1210 static void netfilter_print_info(Monitor
*mon
, NetFilterState
*nf
)
1213 ObjectProperty
*prop
;
1214 ObjectPropertyIterator iter
;
1217 /* generate info str */
1218 object_property_iter_init(&iter
, OBJECT(nf
));
1219 while ((prop
= object_property_iter_next(&iter
))) {
1220 if (!strcmp(prop
->name
, "type")) {
1223 v
= string_output_visitor_new(false, &str
);
1224 object_property_get(OBJECT(nf
), v
, prop
->name
, NULL
);
1225 visit_complete(v
, &str
);
1227 monitor_printf(mon
, ",%s=%s", prop
->name
, str
);
1230 monitor_printf(mon
, "\n");
1233 void print_net_client(Monitor
*mon
, NetClientState
*nc
)
1237 monitor_printf(mon
, "%s: index=%d,type=%s,%s\n", nc
->name
,
1239 NetClientDriver_str(nc
->info
->type
),
1241 if (!QTAILQ_EMPTY(&nc
->filters
)) {
1242 monitor_printf(mon
, "filters:\n");
1244 QTAILQ_FOREACH(nf
, &nc
->filters
, next
) {
1245 char *path
= object_get_canonical_path_component(OBJECT(nf
));
1247 monitor_printf(mon
, " - %s: type=%s", path
,
1248 object_get_typename(OBJECT(nf
)));
1249 netfilter_print_info(mon
, nf
);
1254 RxFilterInfoList
*qmp_query_rx_filter(bool has_name
, const char *name
,
1258 RxFilterInfoList
*filter_list
= NULL
, *last_entry
= NULL
;
1260 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
1261 RxFilterInfoList
*entry
;
1264 if (has_name
&& strcmp(nc
->name
, name
) != 0) {
1268 /* only query rx-filter information of NIC */
1269 if (nc
->info
->type
!= NET_CLIENT_DRIVER_NIC
) {
1271 error_setg(errp
, "net client(%s) isn't a NIC", name
);
1277 /* only query information on queue 0 since the info is per nic,
1280 if (nc
->queue_index
!= 0)
1283 if (nc
->info
->query_rx_filter
) {
1284 info
= nc
->info
->query_rx_filter(nc
);
1285 entry
= g_malloc0(sizeof(*entry
));
1286 entry
->value
= info
;
1289 filter_list
= entry
;
1291 last_entry
->next
= entry
;
1294 } else if (has_name
) {
1295 error_setg(errp
, "net client(%s) doesn't support"
1296 " rx-filter querying", name
);
1305 if (filter_list
== NULL
&& has_name
) {
1306 error_setg(errp
, "invalid net client name: %s", name
);
1312 void hmp_info_network(Monitor
*mon
, const QDict
*qdict
)
1314 NetClientState
*nc
, *peer
;
1315 NetClientDriver type
;
1319 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
1321 type
= nc
->info
->type
;
1323 /* Skip if already printed in hub info */
1324 if (net_hub_id_for_client(nc
, NULL
) == 0) {
1328 if (!peer
|| type
== NET_CLIENT_DRIVER_NIC
) {
1329 print_net_client(mon
, nc
);
1330 } /* else it's a netdev connected to a NIC, printed with the NIC */
1331 if (peer
&& type
== NET_CLIENT_DRIVER_NIC
) {
1332 monitor_printf(mon
, " \\ ");
1333 print_net_client(mon
, peer
);
1338 void qmp_set_link(const char *name
, bool up
, Error
**errp
)
1340 NetClientState
*ncs
[MAX_QUEUE_NUM
];
1344 queues
= qemu_find_net_clients_except(name
, ncs
,
1345 NET_CLIENT_DRIVER__MAX
,
1349 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1350 "Device '%s' not found", name
);
1355 for (i
= 0; i
< queues
; i
++) {
1356 ncs
[i
]->link_down
= !up
;
1359 if (nc
->info
->link_status_changed
) {
1360 nc
->info
->link_status_changed(nc
);
1364 /* Change peer link only if the peer is NIC and then notify peer.
1365 * If the peer is a HUBPORT or a backend, we do not change the
1368 * This behavior is compatible with qemu vlans where there could be
1369 * multiple clients that can still communicate with each other in
1370 * disconnected mode. For now maintain this compatibility.
1372 if (nc
->peer
->info
->type
== NET_CLIENT_DRIVER_NIC
) {
1373 for (i
= 0; i
< queues
; i
++) {
1374 ncs
[i
]->peer
->link_down
= !up
;
1377 if (nc
->peer
->info
->link_status_changed
) {
1378 nc
->peer
->info
->link_status_changed(nc
->peer
);
1383 static void net_vm_change_state_handler(void *opaque
, int running
,
1387 NetClientState
*tmp
;
1389 QTAILQ_FOREACH_SAFE(nc
, &net_clients
, next
, tmp
) {
1391 /* Flush queued packets and wake up backends. */
1392 if (nc
->peer
&& qemu_can_send_packet(nc
)) {
1393 qemu_flush_queued_packets(nc
->peer
);
1396 /* Complete all queued packets, to guarantee we don't modify
1397 * state later when VM is not running.
1399 qemu_flush_or_purge_queued_packets(nc
, true);
1404 void net_cleanup(void)
1408 /* We may del multiple entries during qemu_del_net_client(),
1409 * so QTAILQ_FOREACH_SAFE() is also not safe here.
1411 while (!QTAILQ_EMPTY(&net_clients
)) {
1412 nc
= QTAILQ_FIRST(&net_clients
);
1413 if (nc
->info
->type
== NET_CLIENT_DRIVER_NIC
) {
1414 qemu_del_nic(qemu_get_nic(nc
));
1416 qemu_del_net_client(nc
);
1420 qemu_del_vm_change_state_handler(net_change_state_entry
);
1423 void net_check_clients(void)
1428 net_hub_check_clients();
1430 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
1432 warn_report("%s %s has no peer",
1433 nc
->info
->type
== NET_CLIENT_DRIVER_NIC
1439 /* Check that all NICs requested via -net nic actually got created.
1440 * NICs created via -device don't need to be checked here because
1441 * they are always instantiated.
1443 for (i
= 0; i
< MAX_NICS
; i
++) {
1444 NICInfo
*nd
= &nd_table
[i
];
1445 if (nd
->used
&& !nd
->instantiated
) {
1446 warn_report("requested NIC (%s, model %s) "
1447 "was not created (not supported by this machine?)",
1448 nd
->name
? nd
->name
: "anonymous",
1449 nd
->model
? nd
->model
: "unspecified");
1454 static int net_init_client(void *dummy
, QemuOpts
*opts
, Error
**errp
)
1456 return net_client_init(opts
, false, errp
);
1459 static int net_init_netdev(void *dummy
, QemuOpts
*opts
, Error
**errp
)
1461 return net_client_init(opts
, true, errp
);
1464 /* For the convenience "--nic" parameter */
1465 static int net_param_nic(void *dummy
, QemuOpts
*opts
, Error
**errp
)
1472 type
= qemu_opt_get(opts
, "type");
1473 if (type
&& g_str_equal(type
, "none")) {
1474 return 0; /* Nothing to do, default_net is cleared in vl.c */
1477 idx
= nic_get_free_idx();
1478 if (idx
== -1 || nb_nics
>= MAX_NICS
) {
1479 error_setg(errp
, "no more on-board/default NIC slots available");
1484 qemu_opt_set(opts
, "type", "user", &error_abort
);
1487 ni
= &nd_table
[idx
];
1488 memset(ni
, 0, sizeof(*ni
));
1489 ni
->model
= qemu_opt_get_del(opts
, "model");
1491 /* Create an ID if the user did not specify one */
1492 nd_id
= g_strdup(qemu_opts_id(opts
));
1494 nd_id
= g_strdup_printf("__org.qemu.nic%i\n", idx
);
1495 qemu_opts_set_id(opts
, nd_id
);
1498 /* Handle MAC address */
1499 mac
= qemu_opt_get_del(opts
, "mac");
1501 ret
= net_parse_macaddr(ni
->macaddr
.a
, mac
);
1504 error_setg(errp
, "invalid syntax for ethernet address");
1507 if (is_multicast_ether_addr(ni
->macaddr
.a
)) {
1508 error_setg(errp
, "NIC cannot have multicast MAC address");
1512 qemu_macaddr_default_if_unset(&ni
->macaddr
);
1514 ret
= net_client_init(opts
, true, errp
);
1516 ni
->netdev
= qemu_find_netdev(nd_id
);
1525 int net_init_clients(Error
**errp
)
1527 net_change_state_entry
=
1528 qemu_add_vm_change_state_handler(net_vm_change_state_handler
, NULL
);
1530 QTAILQ_INIT(&net_clients
);
1532 if (qemu_opts_foreach(qemu_find_opts("netdev"),
1533 net_init_netdev
, NULL
, errp
)) {
1537 if (qemu_opts_foreach(qemu_find_opts("nic"), net_param_nic
, NULL
, errp
)) {
1541 if (qemu_opts_foreach(qemu_find_opts("net"), net_init_client
, NULL
, errp
)) {
1548 int net_client_parse(QemuOptsList
*opts_list
, const char *optarg
)
1550 if (!qemu_opts_parse_noisily(opts_list
, optarg
, true)) {
1559 uint32_t net_crc32(const uint8_t *p
, int len
)
1566 for (i
= 0; i
< len
; i
++) {
1568 for (j
= 0; j
< 8; j
++) {
1569 carry
= ((crc
& 0x80000000L
) ? 1 : 0) ^ (b
& 0x01);
1573 crc
= ((crc
^ POLYNOMIAL_BE
) | carry
);
1581 uint32_t net_crc32_le(const uint8_t *p
, int len
)
1588 for (i
= 0; i
< len
; i
++) {
1590 for (j
= 0; j
< 8; j
++) {
1591 carry
= (crc
& 0x1) ^ (b
& 0x01);
1595 crc
^= POLYNOMIAL_LE
;
1603 QemuOptsList qemu_netdev_opts
= {
1605 .implied_opt_name
= "type",
1606 .head
= QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts
.head
),
1609 * no elements => accept any params
1610 * validation will happen later
1612 { /* end of list */ }
1616 QemuOptsList qemu_nic_opts
= {
1618 .implied_opt_name
= "type",
1619 .head
= QTAILQ_HEAD_INITIALIZER(qemu_nic_opts
.head
),
1622 * no elements => accept any params
1623 * validation will happen later
1625 { /* end of list */ }
1629 QemuOptsList qemu_net_opts
= {
1631 .implied_opt_name
= "type",
1632 .head
= QTAILQ_HEAD_INITIALIZER(qemu_net_opts
.head
),
1635 * no elements => accept any params
1636 * validation will happen later
1638 { /* end of list */ }
1642 void net_socket_rs_init(SocketReadState
*rs
,
1643 SocketReadStateFinalize
*finalize
,
1647 rs
->vnet_hdr
= vnet_hdr
;
1650 rs
->vnet_hdr_len
= 0;
1651 memset(rs
->buf
, 0, sizeof(rs
->buf
));
1652 rs
->finalize
= finalize
;
1660 int net_fill_rstate(SocketReadState
*rs
, const uint8_t *buf
, int size
)
1665 /* Reassemble a packet from the network.
1666 * 0 = getting length.
1667 * 1 = getting vnet header length.
1670 switch (rs
->state
) {
1676 memcpy(rs
->buf
+ rs
->index
, buf
, l
);
1680 if (rs
->index
== 4) {
1682 rs
->packet_len
= ntohl(*(uint32_t *)rs
->buf
);
1688 rs
->vnet_hdr_len
= 0;
1697 memcpy(rs
->buf
+ rs
->index
, buf
, l
);
1701 if (rs
->index
== 4) {
1702 /* got vnet header length */
1703 rs
->vnet_hdr_len
= ntohl(*(uint32_t *)rs
->buf
);
1709 l
= rs
->packet_len
- rs
->index
;
1713 if (rs
->index
+ l
<= sizeof(rs
->buf
)) {
1714 memcpy(rs
->buf
+ rs
->index
, buf
, l
);
1716 fprintf(stderr
, "serious error: oversized packet received,"
1717 "connection terminated.\n");
1718 rs
->index
= rs
->state
= 0;
1725 if (rs
->index
>= rs
->packet_len
) {
1728 assert(rs
->finalize
);