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"
44 #include "qemu/ctype.h"
47 #include "qemu/main-loop.h"
48 #include "qemu/option.h"
49 #include "qapi/error.h"
50 #include "qapi/opts-visitor.h"
51 #include "sysemu/sysemu.h"
52 #include "sysemu/qtest.h"
53 #include "net/filter.h"
54 #include "qapi/string-output-visitor.h"
56 /* Net bridge is currently not supported for W32. */
58 # define CONFIG_NET_BRIDGE
61 static VMChangeStateEntry
*net_change_state_entry
;
62 static QTAILQ_HEAD(, NetClientState
) net_clients
;
64 /***********************************************************/
65 /* network device redirectors */
67 static int get_str_sep(char *buf
, int buf_size
, const char **pp
, int sep
)
78 if (len
> buf_size
- 1)
87 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
,
92 const char *addr
, *p
, *r
;
95 substrings
= g_strsplit(str
, ":", 2);
96 if (!substrings
|| !substrings
[0] || !substrings
[1]) {
97 error_setg(errp
, "host address '%s' doesn't contain ':' "
98 "separating host from port", str
);
103 addr
= substrings
[0];
106 saddr
->sin_family
= AF_INET
;
107 if (addr
[0] == '\0') {
108 saddr
->sin_addr
.s_addr
= 0;
110 if (qemu_isdigit(addr
[0])) {
111 if (!inet_aton(addr
, &saddr
->sin_addr
)) {
112 error_setg(errp
, "host address '%s' is not a valid "
113 "IPv4 address", addr
);
118 he
= gethostbyname(addr
);
120 error_setg(errp
, "can't resolve host address '%s'", addr
);
124 saddr
->sin_addr
= *(struct in_addr
*)he
->h_addr
;
127 port
= strtol(p
, (char **)&r
, 0);
129 error_setg(errp
, "port number '%s' is invalid", p
);
133 saddr
->sin_port
= htons(port
);
136 g_strfreev(substrings
);
140 char *qemu_mac_strdup_printf(const uint8_t *macaddr
)
142 return g_strdup_printf("%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",
143 macaddr
[0], macaddr
[1], macaddr
[2],
144 macaddr
[3], macaddr
[4], macaddr
[5]);
147 void qemu_format_nic_info_str(NetClientState
*nc
, uint8_t macaddr
[6])
149 snprintf(nc
->info_str
, sizeof(nc
->info_str
),
150 "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
152 macaddr
[0], macaddr
[1], macaddr
[2],
153 macaddr
[3], macaddr
[4], macaddr
[5]);
156 static int mac_table
[256] = {0};
158 static void qemu_macaddr_set_used(MACAddr
*macaddr
)
162 for (index
= 0x56; index
< 0xFF; index
++) {
163 if (macaddr
->a
[5] == index
) {
169 static void qemu_macaddr_set_free(MACAddr
*macaddr
)
172 static const MACAddr base
= { .a
= { 0x52, 0x54, 0x00, 0x12, 0x34, 0 } };
174 if (memcmp(macaddr
->a
, &base
.a
, (sizeof(base
.a
) - 1)) != 0) {
177 for (index
= 0x56; index
< 0xFF; index
++) {
178 if (macaddr
->a
[5] == index
) {
184 static int qemu_macaddr_get_free(void)
188 for (index
= 0x56; index
< 0xFF; index
++) {
189 if (mac_table
[index
] == 0) {
197 void qemu_macaddr_default_if_unset(MACAddr
*macaddr
)
199 static const MACAddr zero
= { .a
= { 0,0,0,0,0,0 } };
200 static const MACAddr base
= { .a
= { 0x52, 0x54, 0x00, 0x12, 0x34, 0 } };
202 if (memcmp(macaddr
, &zero
, sizeof(zero
)) != 0) {
203 if (memcmp(macaddr
->a
, &base
.a
, (sizeof(base
.a
) - 1)) != 0) {
206 qemu_macaddr_set_used(macaddr
);
211 macaddr
->a
[0] = 0x52;
212 macaddr
->a
[1] = 0x54;
213 macaddr
->a
[2] = 0x00;
214 macaddr
->a
[3] = 0x12;
215 macaddr
->a
[4] = 0x34;
216 macaddr
->a
[5] = qemu_macaddr_get_free();
217 qemu_macaddr_set_used(macaddr
);
221 * Generate a name for net client
223 * Only net clients created with the legacy -net option and NICs need this.
225 static char *assign_name(NetClientState
*nc1
, const char *model
)
230 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
234 if (strcmp(nc
->model
, model
) == 0) {
239 return g_strdup_printf("%s.%d", model
, id
);
242 static void qemu_net_client_destructor(NetClientState
*nc
)
246 static ssize_t
qemu_deliver_packet_iov(NetClientState
*sender
,
248 const struct iovec
*iov
,
252 static void qemu_net_client_setup(NetClientState
*nc
,
254 NetClientState
*peer
,
257 NetClientDestructor
*destructor
)
260 nc
->model
= g_strdup(model
);
262 nc
->name
= g_strdup(name
);
264 nc
->name
= assign_name(nc
, model
);
272 QTAILQ_INSERT_TAIL(&net_clients
, nc
, next
);
274 nc
->incoming_queue
= qemu_new_net_queue(qemu_deliver_packet_iov
, nc
);
275 nc
->destructor
= destructor
;
276 QTAILQ_INIT(&nc
->filters
);
279 NetClientState
*qemu_new_net_client(NetClientInfo
*info
,
280 NetClientState
*peer
,
286 assert(info
->size
>= sizeof(NetClientState
));
288 nc
= g_malloc0(info
->size
);
289 qemu_net_client_setup(nc
, info
, peer
, model
, name
,
290 qemu_net_client_destructor
);
295 NICState
*qemu_new_nic(NetClientInfo
*info
,
301 NetClientState
**peers
= conf
->peers
.ncs
;
303 int i
, queues
= MAX(1, conf
->peers
.queues
);
305 assert(info
->type
== NET_CLIENT_DRIVER_NIC
);
306 assert(info
->size
>= sizeof(NICState
));
308 nic
= g_malloc0(info
->size
+ sizeof(NetClientState
) * queues
);
309 nic
->ncs
= (void *)nic
+ info
->size
;
311 nic
->opaque
= opaque
;
313 for (i
= 0; i
< queues
; i
++) {
314 qemu_net_client_setup(&nic
->ncs
[i
], info
, peers
[i
], model
, name
,
316 nic
->ncs
[i
].queue_index
= i
;
322 NetClientState
*qemu_get_subqueue(NICState
*nic
, int queue_index
)
324 return nic
->ncs
+ queue_index
;
327 NetClientState
*qemu_get_queue(NICState
*nic
)
329 return qemu_get_subqueue(nic
, 0);
332 NICState
*qemu_get_nic(NetClientState
*nc
)
334 NetClientState
*nc0
= nc
- nc
->queue_index
;
336 return (NICState
*)((void *)nc0
- nc
->info
->size
);
339 void *qemu_get_nic_opaque(NetClientState
*nc
)
341 NICState
*nic
= qemu_get_nic(nc
);
346 static void qemu_cleanup_net_client(NetClientState
*nc
)
348 QTAILQ_REMOVE(&net_clients
, nc
, next
);
350 if (nc
->info
->cleanup
) {
351 nc
->info
->cleanup(nc
);
355 static void qemu_free_net_client(NetClientState
*nc
)
357 if (nc
->incoming_queue
) {
358 qemu_del_net_queue(nc
->incoming_queue
);
361 nc
->peer
->peer
= NULL
;
365 if (nc
->destructor
) {
370 void qemu_del_net_client(NetClientState
*nc
)
372 NetClientState
*ncs
[MAX_QUEUE_NUM
];
374 NetFilterState
*nf
, *next
;
376 assert(nc
->info
->type
!= NET_CLIENT_DRIVER_NIC
);
378 /* If the NetClientState belongs to a multiqueue backend, we will change all
379 * other NetClientStates also.
381 queues
= qemu_find_net_clients_except(nc
->name
, ncs
,
382 NET_CLIENT_DRIVER_NIC
,
386 QTAILQ_FOREACH_SAFE(nf
, &nc
->filters
, next
, next
) {
387 object_unparent(OBJECT(nf
));
390 /* If there is a peer NIC, delete and cleanup client, but do not free. */
391 if (nc
->peer
&& nc
->peer
->info
->type
== NET_CLIENT_DRIVER_NIC
) {
392 NICState
*nic
= qemu_get_nic(nc
->peer
);
393 if (nic
->peer_deleted
) {
396 nic
->peer_deleted
= true;
398 for (i
= 0; i
< queues
; i
++) {
399 ncs
[i
]->peer
->link_down
= true;
402 if (nc
->peer
->info
->link_status_changed
) {
403 nc
->peer
->info
->link_status_changed(nc
->peer
);
406 for (i
= 0; i
< queues
; i
++) {
407 qemu_cleanup_net_client(ncs
[i
]);
413 for (i
= 0; i
< queues
; i
++) {
414 qemu_cleanup_net_client(ncs
[i
]);
415 qemu_free_net_client(ncs
[i
]);
419 void qemu_del_nic(NICState
*nic
)
421 int i
, queues
= MAX(nic
->conf
->peers
.queues
, 1);
423 qemu_macaddr_set_free(&nic
->conf
->macaddr
);
425 /* If this is a peer NIC and peer has already been deleted, free it now. */
426 if (nic
->peer_deleted
) {
427 for (i
= 0; i
< queues
; i
++) {
428 qemu_free_net_client(qemu_get_subqueue(nic
, i
)->peer
);
432 for (i
= queues
- 1; i
>= 0; i
--) {
433 NetClientState
*nc
= qemu_get_subqueue(nic
, i
);
435 qemu_cleanup_net_client(nc
);
436 qemu_free_net_client(nc
);
442 void qemu_foreach_nic(qemu_nic_foreach func
, void *opaque
)
446 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
447 if (nc
->info
->type
== NET_CLIENT_DRIVER_NIC
) {
448 if (nc
->queue_index
== 0) {
449 func(qemu_get_nic(nc
), opaque
);
455 bool qemu_has_ufo(NetClientState
*nc
)
457 if (!nc
|| !nc
->info
->has_ufo
) {
461 return nc
->info
->has_ufo(nc
);
464 bool qemu_has_vnet_hdr(NetClientState
*nc
)
466 if (!nc
|| !nc
->info
->has_vnet_hdr
) {
470 return nc
->info
->has_vnet_hdr(nc
);
473 bool qemu_has_vnet_hdr_len(NetClientState
*nc
, int len
)
475 if (!nc
|| !nc
->info
->has_vnet_hdr_len
) {
479 return nc
->info
->has_vnet_hdr_len(nc
, len
);
482 void qemu_using_vnet_hdr(NetClientState
*nc
, bool enable
)
484 if (!nc
|| !nc
->info
->using_vnet_hdr
) {
488 nc
->info
->using_vnet_hdr(nc
, enable
);
491 void qemu_set_offload(NetClientState
*nc
, int csum
, int tso4
, int tso6
,
494 if (!nc
|| !nc
->info
->set_offload
) {
498 nc
->info
->set_offload(nc
, csum
, tso4
, tso6
, ecn
, ufo
);
501 void qemu_set_vnet_hdr_len(NetClientState
*nc
, int len
)
503 if (!nc
|| !nc
->info
->set_vnet_hdr_len
) {
507 nc
->vnet_hdr_len
= len
;
508 nc
->info
->set_vnet_hdr_len(nc
, len
);
511 int qemu_set_vnet_le(NetClientState
*nc
, bool is_le
)
513 #ifdef HOST_WORDS_BIGENDIAN
514 if (!nc
|| !nc
->info
->set_vnet_le
) {
518 return nc
->info
->set_vnet_le(nc
, is_le
);
524 int qemu_set_vnet_be(NetClientState
*nc
, bool is_be
)
526 #ifdef HOST_WORDS_BIGENDIAN
529 if (!nc
|| !nc
->info
->set_vnet_be
) {
533 return nc
->info
->set_vnet_be(nc
, is_be
);
537 int qemu_can_send_packet(NetClientState
*sender
)
539 int vm_running
= runstate_is_running();
549 if (sender
->peer
->receive_disabled
) {
551 } else if (sender
->peer
->info
->can_receive
&&
552 !sender
->peer
->info
->can_receive(sender
->peer
)) {
558 static ssize_t
filter_receive_iov(NetClientState
*nc
,
559 NetFilterDirection direction
,
560 NetClientState
*sender
,
562 const struct iovec
*iov
,
564 NetPacketSent
*sent_cb
)
567 NetFilterState
*nf
= NULL
;
569 if (direction
== NET_FILTER_DIRECTION_TX
) {
570 QTAILQ_FOREACH(nf
, &nc
->filters
, next
) {
571 ret
= qemu_netfilter_receive(nf
, direction
, sender
, flags
, iov
,
578 QTAILQ_FOREACH_REVERSE(nf
, &nc
->filters
, next
) {
579 ret
= qemu_netfilter_receive(nf
, direction
, sender
, flags
, iov
,
590 static ssize_t
filter_receive(NetClientState
*nc
,
591 NetFilterDirection direction
,
592 NetClientState
*sender
,
596 NetPacketSent
*sent_cb
)
599 .iov_base
= (void *)data
,
603 return filter_receive_iov(nc
, direction
, sender
, flags
, &iov
, 1, sent_cb
);
606 void qemu_purge_queued_packets(NetClientState
*nc
)
612 qemu_net_queue_purge(nc
->peer
->incoming_queue
, nc
);
615 void qemu_flush_or_purge_queued_packets(NetClientState
*nc
, bool purge
)
617 nc
->receive_disabled
= 0;
619 if (nc
->peer
&& nc
->peer
->info
->type
== NET_CLIENT_DRIVER_HUBPORT
) {
620 if (net_hub_flush(nc
->peer
)) {
624 if (qemu_net_queue_flush(nc
->incoming_queue
)) {
625 /* We emptied the queue successfully, signal to the IO thread to repoll
626 * the file descriptor (for tap, for example).
630 /* Unable to empty the queue, purge remaining packets */
631 qemu_net_queue_purge(nc
->incoming_queue
, nc
);
635 void qemu_flush_queued_packets(NetClientState
*nc
)
637 qemu_flush_or_purge_queued_packets(nc
, false);
640 static ssize_t
qemu_send_packet_async_with_flags(NetClientState
*sender
,
642 const uint8_t *buf
, int size
,
643 NetPacketSent
*sent_cb
)
649 printf("qemu_send_packet_async:\n");
650 qemu_hexdump((const char *)buf
, stdout
, "net", size
);
653 if (sender
->link_down
|| !sender
->peer
) {
657 /* Let filters handle the packet first */
658 ret
= filter_receive(sender
, NET_FILTER_DIRECTION_TX
,
659 sender
, flags
, buf
, size
, sent_cb
);
664 ret
= filter_receive(sender
->peer
, NET_FILTER_DIRECTION_RX
,
665 sender
, flags
, buf
, size
, sent_cb
);
670 queue
= sender
->peer
->incoming_queue
;
672 return qemu_net_queue_send(queue
, sender
, flags
, buf
, size
, sent_cb
);
675 ssize_t
qemu_send_packet_async(NetClientState
*sender
,
676 const uint8_t *buf
, int size
,
677 NetPacketSent
*sent_cb
)
679 return qemu_send_packet_async_with_flags(sender
, QEMU_NET_PACKET_FLAG_NONE
,
683 ssize_t
qemu_send_packet(NetClientState
*nc
, const uint8_t *buf
, int size
)
685 return qemu_send_packet_async(nc
, buf
, size
, NULL
);
688 ssize_t
qemu_send_packet_raw(NetClientState
*nc
, const uint8_t *buf
, int size
)
690 return qemu_send_packet_async_with_flags(nc
, QEMU_NET_PACKET_FLAG_RAW
,
694 static ssize_t
nc_sendv_compat(NetClientState
*nc
, const struct iovec
*iov
,
695 int iovcnt
, unsigned flags
)
703 buffer
= iov
[0].iov_base
;
704 offset
= iov
[0].iov_len
;
706 offset
= iov_size(iov
, iovcnt
);
707 if (offset
> NET_BUFSIZE
) {
710 buf
= g_malloc(offset
);
712 offset
= iov_to_buf(iov
, iovcnt
, 0, buf
, offset
);
715 if (flags
& QEMU_NET_PACKET_FLAG_RAW
&& nc
->info
->receive_raw
) {
716 ret
= nc
->info
->receive_raw(nc
, buffer
, offset
);
718 ret
= nc
->info
->receive(nc
, buffer
, offset
);
725 static ssize_t
qemu_deliver_packet_iov(NetClientState
*sender
,
727 const struct iovec
*iov
,
731 NetClientState
*nc
= opaque
;
736 return iov_size(iov
, iovcnt
);
739 if (nc
->receive_disabled
) {
743 if (nc
->info
->receive_iov
&& !(flags
& QEMU_NET_PACKET_FLAG_RAW
)) {
744 ret
= nc
->info
->receive_iov(nc
, iov
, iovcnt
);
746 ret
= nc_sendv_compat(nc
, iov
, iovcnt
, flags
);
750 nc
->receive_disabled
= 1;
756 ssize_t
qemu_sendv_packet_async(NetClientState
*sender
,
757 const struct iovec
*iov
, int iovcnt
,
758 NetPacketSent
*sent_cb
)
761 size_t size
= iov_size(iov
, iovcnt
);
764 if (size
> NET_BUFSIZE
) {
768 if (sender
->link_down
|| !sender
->peer
) {
772 /* Let filters handle the packet first */
773 ret
= filter_receive_iov(sender
, NET_FILTER_DIRECTION_TX
, sender
,
774 QEMU_NET_PACKET_FLAG_NONE
, iov
, iovcnt
, sent_cb
);
779 ret
= filter_receive_iov(sender
->peer
, NET_FILTER_DIRECTION_RX
, sender
,
780 QEMU_NET_PACKET_FLAG_NONE
, iov
, iovcnt
, sent_cb
);
785 queue
= sender
->peer
->incoming_queue
;
787 return qemu_net_queue_send_iov(queue
, sender
,
788 QEMU_NET_PACKET_FLAG_NONE
,
789 iov
, iovcnt
, sent_cb
);
793 qemu_sendv_packet(NetClientState
*nc
, const struct iovec
*iov
, int iovcnt
)
795 return qemu_sendv_packet_async(nc
, iov
, iovcnt
, NULL
);
798 NetClientState
*qemu_find_netdev(const char *id
)
802 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
803 if (nc
->info
->type
== NET_CLIENT_DRIVER_NIC
)
805 if (!strcmp(nc
->name
, id
)) {
813 int qemu_find_net_clients_except(const char *id
, NetClientState
**ncs
,
814 NetClientDriver type
, int max
)
819 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
820 if (nc
->info
->type
== type
) {
823 if (!id
|| !strcmp(nc
->name
, id
)) {
834 static int nic_get_free_idx(void)
838 for (index
= 0; index
< MAX_NICS
; index
++)
839 if (!nd_table
[index
].used
)
844 int qemu_show_nic_models(const char *arg
, const char *const *models
)
848 if (!arg
|| !is_help_option(arg
)) {
852 printf("Supported NIC models:\n");
853 for (i
= 0 ; models
[i
]; i
++) {
854 printf("%s\n", models
[i
]);
859 void qemu_check_nic_model(NICInfo
*nd
, const char *model
)
861 const char *models
[2];
866 if (qemu_show_nic_models(nd
->model
, models
))
868 if (qemu_find_nic_model(nd
, models
, model
) < 0)
872 int qemu_find_nic_model(NICInfo
*nd
, const char * const *models
,
873 const char *default_model
)
878 nd
->model
= g_strdup(default_model
);
880 for (i
= 0 ; models
[i
]; i
++) {
881 if (strcmp(nd
->model
, models
[i
]) == 0)
885 error_report("Unsupported NIC model: %s", nd
->model
);
889 static int net_init_nic(const Netdev
*netdev
, const char *name
,
890 NetClientState
*peer
, Error
**errp
)
894 const NetLegacyNicOptions
*nic
;
896 assert(netdev
->type
== NET_CLIENT_DRIVER_NIC
);
897 nic
= &netdev
->u
.nic
;
899 idx
= nic_get_free_idx();
900 if (idx
== -1 || nb_nics
>= MAX_NICS
) {
901 error_setg(errp
, "too many NICs");
907 memset(nd
, 0, sizeof(*nd
));
909 if (nic
->has_netdev
) {
910 nd
->netdev
= qemu_find_netdev(nic
->netdev
);
912 error_setg(errp
, "netdev '%s' not found", nic
->netdev
);
919 nd
->name
= g_strdup(name
);
920 if (nic
->has_model
) {
921 nd
->model
= g_strdup(nic
->model
);
924 nd
->devaddr
= g_strdup(nic
->addr
);
927 if (nic
->has_macaddr
&&
928 net_parse_macaddr(nd
->macaddr
.a
, nic
->macaddr
) < 0) {
929 error_setg(errp
, "invalid syntax for ethernet address");
932 if (nic
->has_macaddr
&&
933 is_multicast_ether_addr(nd
->macaddr
.a
)) {
935 "NIC cannot have multicast MAC address (odd 1st byte)");
938 qemu_macaddr_default_if_unset(&nd
->macaddr
);
940 if (nic
->has_vectors
) {
941 if (nic
->vectors
> 0x7ffffff) {
942 error_setg(errp
, "invalid # of vectors: %"PRIu32
, nic
->vectors
);
945 nd
->nvectors
= nic
->vectors
;
947 nd
->nvectors
= DEV_NVECTORS_UNSPECIFIED
;
957 static int (* const net_client_init_fun
[NET_CLIENT_DRIVER__MAX
])(
958 const Netdev
*netdev
,
960 NetClientState
*peer
, Error
**errp
) = {
961 [NET_CLIENT_DRIVER_NIC
] = net_init_nic
,
963 [NET_CLIENT_DRIVER_USER
] = net_init_slirp
,
965 [NET_CLIENT_DRIVER_TAP
] = net_init_tap
,
966 [NET_CLIENT_DRIVER_SOCKET
] = net_init_socket
,
968 [NET_CLIENT_DRIVER_VDE
] = net_init_vde
,
971 [NET_CLIENT_DRIVER_NETMAP
] = net_init_netmap
,
973 #ifdef CONFIG_NET_BRIDGE
974 [NET_CLIENT_DRIVER_BRIDGE
] = net_init_bridge
,
976 [NET_CLIENT_DRIVER_HUBPORT
] = net_init_hubport
,
977 #ifdef CONFIG_VHOST_NET_USER
978 [NET_CLIENT_DRIVER_VHOST_USER
] = net_init_vhost_user
,
981 [NET_CLIENT_DRIVER_L2TPV3
] = net_init_l2tpv3
,
986 static int net_client_init1(const void *object
, bool is_netdev
, Error
**errp
)
989 const Netdev
*netdev
;
991 NetClientState
*peer
= NULL
;
997 if (netdev
->type
== NET_CLIENT_DRIVER_NIC
||
998 !net_client_init_fun
[netdev
->type
]) {
999 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "type",
1000 "a netdev backend type");
1004 const NetLegacy
*net
= object
;
1005 const NetLegacyOptions
*opts
= net
->opts
;
1006 legacy
.id
= net
->id
;
1008 /* missing optional values have been initialized to "all bits zero" */
1009 name
= net
->has_id
? net
->id
: net
->name
;
1011 if (net
->has_name
) {
1012 warn_report("The 'name' parameter is deprecated, use 'id' instead");
1015 /* Map the old options to the new flat type */
1016 switch (opts
->type
) {
1017 case NET_LEGACY_OPTIONS_TYPE_NONE
:
1018 return 0; /* nothing to do */
1019 case NET_LEGACY_OPTIONS_TYPE_NIC
:
1020 legacy
.type
= NET_CLIENT_DRIVER_NIC
;
1021 legacy
.u
.nic
= opts
->u
.nic
;
1023 case NET_LEGACY_OPTIONS_TYPE_USER
:
1024 legacy
.type
= NET_CLIENT_DRIVER_USER
;
1025 legacy
.u
.user
= opts
->u
.user
;
1027 case NET_LEGACY_OPTIONS_TYPE_TAP
:
1028 legacy
.type
= NET_CLIENT_DRIVER_TAP
;
1029 legacy
.u
.tap
= opts
->u
.tap
;
1031 case NET_LEGACY_OPTIONS_TYPE_L2TPV3
:
1032 legacy
.type
= NET_CLIENT_DRIVER_L2TPV3
;
1033 legacy
.u
.l2tpv3
= opts
->u
.l2tpv3
;
1035 case NET_LEGACY_OPTIONS_TYPE_SOCKET
:
1036 legacy
.type
= NET_CLIENT_DRIVER_SOCKET
;
1037 legacy
.u
.socket
= opts
->u
.socket
;
1039 case NET_LEGACY_OPTIONS_TYPE_VDE
:
1040 legacy
.type
= NET_CLIENT_DRIVER_VDE
;
1041 legacy
.u
.vde
= opts
->u
.vde
;
1043 case NET_LEGACY_OPTIONS_TYPE_BRIDGE
:
1044 legacy
.type
= NET_CLIENT_DRIVER_BRIDGE
;
1045 legacy
.u
.bridge
= opts
->u
.bridge
;
1047 case NET_LEGACY_OPTIONS_TYPE_NETMAP
:
1048 legacy
.type
= NET_CLIENT_DRIVER_NETMAP
;
1049 legacy
.u
.netmap
= opts
->u
.netmap
;
1051 case NET_LEGACY_OPTIONS_TYPE_VHOST_USER
:
1052 legacy
.type
= NET_CLIENT_DRIVER_VHOST_USER
;
1053 legacy
.u
.vhost_user
= opts
->u
.vhost_user
;
1059 if (!net_client_init_fun
[netdev
->type
]) {
1060 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "type",
1061 "a net backend type (maybe it is not compiled "
1062 "into this binary)");
1066 /* Do not add to a hub if it's a nic with a netdev= parameter. */
1067 if (netdev
->type
!= NET_CLIENT_DRIVER_NIC
||
1068 !opts
->u
.nic
.has_netdev
) {
1069 peer
= net_hub_add_port(0, NULL
, NULL
);
1073 if (net_client_init_fun
[netdev
->type
](netdev
, name
, peer
, errp
) < 0) {
1074 /* FIXME drop when all init functions store an Error */
1075 if (errp
&& !*errp
) {
1076 error_setg(errp
, QERR_DEVICE_INIT_FAILED
,
1077 NetClientDriver_str(netdev
->type
));
1084 static void show_netdevs(void)
1087 const char *available_netdevs
[] = {
1094 #ifdef CONFIG_L2TPV3
1100 #ifdef CONFIG_NET_BRIDGE
1103 #ifdef CONFIG_NETMAP
1111 printf("Available netdev backend types:\n");
1112 for (idx
= 0; idx
< ARRAY_SIZE(available_netdevs
); idx
++) {
1113 puts(available_netdevs
[idx
]);
1117 static int net_client_init(QemuOpts
*opts
, bool is_netdev
, Error
**errp
)
1119 gchar
**substrings
= NULL
;
1120 void *object
= NULL
;
1123 Visitor
*v
= opts_visitor_new(opts
);
1125 const char *type
= qemu_opt_get(opts
, "type");
1127 if (is_netdev
&& type
&& is_help_option(type
)) {
1131 /* Parse convenience option format ip6-net=fec0::0[/64] */
1132 const char *ip6_net
= qemu_opt_get(opts
, "ipv6-net");
1136 unsigned long prefix_len
= 64; /* Default 64bit prefix length. */
1138 substrings
= g_strsplit(ip6_net
, "/", 2);
1139 if (!substrings
|| !substrings
[0]) {
1140 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "ipv6-net",
1141 "a valid IPv6 prefix");
1145 prefix_addr
= substrings
[0];
1147 if (substrings
[1]) {
1148 /* User-specified prefix length. */
1151 err
= qemu_strtoul(substrings
[1], NULL
, 10, &prefix_len
);
1153 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
1154 "ipv6-prefixlen", "a number");
1159 qemu_opt_set(opts
, "ipv6-prefix", prefix_addr
, &error_abort
);
1160 qemu_opt_set_number(opts
, "ipv6-prefixlen", prefix_len
,
1162 qemu_opt_unset(opts
, "ipv6-net");
1167 visit_type_Netdev(v
, NULL
, (Netdev
**)&object
, &err
);
1169 visit_type_NetLegacy(v
, NULL
, (NetLegacy
**)&object
, &err
);
1173 ret
= net_client_init1(object
, is_netdev
, &err
);
1177 qapi_free_Netdev(object
);
1179 qapi_free_NetLegacy(object
);
1183 error_propagate(errp
, err
);
1184 g_strfreev(substrings
);
1189 void netdev_add(QemuOpts
*opts
, Error
**errp
)
1191 net_client_init(opts
, true, errp
);
1194 void qmp_netdev_add(QDict
*qdict
, QObject
**ret
, Error
**errp
)
1196 Error
*local_err
= NULL
;
1197 QemuOptsList
*opts_list
;
1200 opts_list
= qemu_find_opts_err("netdev", &local_err
);
1205 opts
= qemu_opts_from_qdict(opts_list
, qdict
, &local_err
);
1210 netdev_add(opts
, &local_err
);
1212 qemu_opts_del(opts
);
1217 error_propagate(errp
, local_err
);
1220 void qmp_netdev_del(const char *id
, Error
**errp
)
1225 nc
= qemu_find_netdev(id
);
1227 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1228 "Device '%s' not found", id
);
1232 opts
= qemu_opts_find(qemu_find_opts_err("netdev", NULL
), id
);
1234 error_setg(errp
, "Device '%s' is not a netdev", id
);
1238 qemu_del_net_client(nc
);
1239 qemu_opts_del(opts
);
1242 static void netfilter_print_info(Monitor
*mon
, NetFilterState
*nf
)
1245 ObjectProperty
*prop
;
1246 ObjectPropertyIterator iter
;
1249 /* generate info str */
1250 object_property_iter_init(&iter
, OBJECT(nf
));
1251 while ((prop
= object_property_iter_next(&iter
))) {
1252 if (!strcmp(prop
->name
, "type")) {
1255 v
= string_output_visitor_new(false, &str
);
1256 object_property_get(OBJECT(nf
), v
, prop
->name
, NULL
);
1257 visit_complete(v
, &str
);
1259 monitor_printf(mon
, ",%s=%s", prop
->name
, str
);
1262 monitor_printf(mon
, "\n");
1265 void print_net_client(Monitor
*mon
, NetClientState
*nc
)
1269 monitor_printf(mon
, "%s: index=%d,type=%s,%s\n", nc
->name
,
1271 NetClientDriver_str(nc
->info
->type
),
1273 if (!QTAILQ_EMPTY(&nc
->filters
)) {
1274 monitor_printf(mon
, "filters:\n");
1276 QTAILQ_FOREACH(nf
, &nc
->filters
, next
) {
1277 char *path
= object_get_canonical_path_component(OBJECT(nf
));
1279 monitor_printf(mon
, " - %s: type=%s", path
,
1280 object_get_typename(OBJECT(nf
)));
1281 netfilter_print_info(mon
, nf
);
1286 RxFilterInfoList
*qmp_query_rx_filter(bool has_name
, const char *name
,
1290 RxFilterInfoList
*filter_list
= NULL
, *last_entry
= NULL
;
1292 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
1293 RxFilterInfoList
*entry
;
1296 if (has_name
&& strcmp(nc
->name
, name
) != 0) {
1300 /* only query rx-filter information of NIC */
1301 if (nc
->info
->type
!= NET_CLIENT_DRIVER_NIC
) {
1303 error_setg(errp
, "net client(%s) isn't a NIC", name
);
1309 /* only query information on queue 0 since the info is per nic,
1312 if (nc
->queue_index
!= 0)
1315 if (nc
->info
->query_rx_filter
) {
1316 info
= nc
->info
->query_rx_filter(nc
);
1317 entry
= g_malloc0(sizeof(*entry
));
1318 entry
->value
= info
;
1321 filter_list
= entry
;
1323 last_entry
->next
= entry
;
1326 } else if (has_name
) {
1327 error_setg(errp
, "net client(%s) doesn't support"
1328 " rx-filter querying", name
);
1337 if (filter_list
== NULL
&& has_name
) {
1338 error_setg(errp
, "invalid net client name: %s", name
);
1344 void hmp_info_network(Monitor
*mon
, const QDict
*qdict
)
1346 NetClientState
*nc
, *peer
;
1347 NetClientDriver type
;
1351 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
1353 type
= nc
->info
->type
;
1355 /* Skip if already printed in hub info */
1356 if (net_hub_id_for_client(nc
, NULL
) == 0) {
1360 if (!peer
|| type
== NET_CLIENT_DRIVER_NIC
) {
1361 print_net_client(mon
, nc
);
1362 } /* else it's a netdev connected to a NIC, printed with the NIC */
1363 if (peer
&& type
== NET_CLIENT_DRIVER_NIC
) {
1364 monitor_printf(mon
, " \\ ");
1365 print_net_client(mon
, peer
);
1370 void colo_notify_filters_event(int event
, Error
**errp
)
1374 NetFilterClass
*nfc
= NULL
;
1375 Error
*local_err
= NULL
;
1377 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
1378 QTAILQ_FOREACH(nf
, &nc
->filters
, next
) {
1379 nfc
= NETFILTER_GET_CLASS(OBJECT(nf
));
1380 nfc
->handle_event(nf
, event
, &local_err
);
1382 error_propagate(errp
, local_err
);
1389 void qmp_set_link(const char *name
, bool up
, Error
**errp
)
1391 NetClientState
*ncs
[MAX_QUEUE_NUM
];
1395 queues
= qemu_find_net_clients_except(name
, ncs
,
1396 NET_CLIENT_DRIVER__MAX
,
1400 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1401 "Device '%s' not found", name
);
1406 for (i
= 0; i
< queues
; i
++) {
1407 ncs
[i
]->link_down
= !up
;
1410 if (nc
->info
->link_status_changed
) {
1411 nc
->info
->link_status_changed(nc
);
1415 /* Change peer link only if the peer is NIC and then notify peer.
1416 * If the peer is a HUBPORT or a backend, we do not change the
1419 * This behavior is compatible with qemu hubs where there could be
1420 * multiple clients that can still communicate with each other in
1421 * disconnected mode. For now maintain this compatibility.
1423 if (nc
->peer
->info
->type
== NET_CLIENT_DRIVER_NIC
) {
1424 for (i
= 0; i
< queues
; i
++) {
1425 ncs
[i
]->peer
->link_down
= !up
;
1428 if (nc
->peer
->info
->link_status_changed
) {
1429 nc
->peer
->info
->link_status_changed(nc
->peer
);
1434 static void net_vm_change_state_handler(void *opaque
, int running
,
1438 NetClientState
*tmp
;
1440 QTAILQ_FOREACH_SAFE(nc
, &net_clients
, next
, tmp
) {
1442 /* Flush queued packets and wake up backends. */
1443 if (nc
->peer
&& qemu_can_send_packet(nc
)) {
1444 qemu_flush_queued_packets(nc
->peer
);
1447 /* Complete all queued packets, to guarantee we don't modify
1448 * state later when VM is not running.
1450 qemu_flush_or_purge_queued_packets(nc
, true);
1455 void net_cleanup(void)
1459 /* We may del multiple entries during qemu_del_net_client(),
1460 * so QTAILQ_FOREACH_SAFE() is also not safe here.
1462 while (!QTAILQ_EMPTY(&net_clients
)) {
1463 nc
= QTAILQ_FIRST(&net_clients
);
1464 if (nc
->info
->type
== NET_CLIENT_DRIVER_NIC
) {
1465 qemu_del_nic(qemu_get_nic(nc
));
1467 qemu_del_net_client(nc
);
1471 qemu_del_vm_change_state_handler(net_change_state_entry
);
1474 void net_check_clients(void)
1479 net_hub_check_clients();
1481 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
1483 warn_report("%s %s has no peer",
1484 nc
->info
->type
== NET_CLIENT_DRIVER_NIC
1490 /* Check that all NICs requested via -net nic actually got created.
1491 * NICs created via -device don't need to be checked here because
1492 * they are always instantiated.
1494 for (i
= 0; i
< MAX_NICS
; i
++) {
1495 NICInfo
*nd
= &nd_table
[i
];
1496 if (nd
->used
&& !nd
->instantiated
) {
1497 warn_report("requested NIC (%s, model %s) "
1498 "was not created (not supported by this machine?)",
1499 nd
->name
? nd
->name
: "anonymous",
1500 nd
->model
? nd
->model
: "unspecified");
1505 static int net_init_client(void *dummy
, QemuOpts
*opts
, Error
**errp
)
1507 return net_client_init(opts
, false, errp
);
1510 static int net_init_netdev(void *dummy
, QemuOpts
*opts
, Error
**errp
)
1512 return net_client_init(opts
, true, errp
);
1515 /* For the convenience "--nic" parameter */
1516 static int net_param_nic(void *dummy
, QemuOpts
*opts
, Error
**errp
)
1523 type
= qemu_opt_get(opts
, "type");
1524 if (type
&& g_str_equal(type
, "none")) {
1525 return 0; /* Nothing to do, default_net is cleared in vl.c */
1528 idx
= nic_get_free_idx();
1529 if (idx
== -1 || nb_nics
>= MAX_NICS
) {
1530 error_setg(errp
, "no more on-board/default NIC slots available");
1535 qemu_opt_set(opts
, "type", "user", &error_abort
);
1538 ni
= &nd_table
[idx
];
1539 memset(ni
, 0, sizeof(*ni
));
1540 ni
->model
= qemu_opt_get_del(opts
, "model");
1542 /* Create an ID if the user did not specify one */
1543 nd_id
= g_strdup(qemu_opts_id(opts
));
1545 nd_id
= g_strdup_printf("__org.qemu.nic%i\n", idx
);
1546 qemu_opts_set_id(opts
, nd_id
);
1549 /* Handle MAC address */
1550 mac
= qemu_opt_get_del(opts
, "mac");
1552 ret
= net_parse_macaddr(ni
->macaddr
.a
, mac
);
1555 error_setg(errp
, "invalid syntax for ethernet address");
1558 if (is_multicast_ether_addr(ni
->macaddr
.a
)) {
1559 error_setg(errp
, "NIC cannot have multicast MAC address");
1564 qemu_macaddr_default_if_unset(&ni
->macaddr
);
1566 ret
= net_client_init(opts
, true, errp
);
1568 ni
->netdev
= qemu_find_netdev(nd_id
);
1578 int net_init_clients(Error
**errp
)
1580 net_change_state_entry
=
1581 qemu_add_vm_change_state_handler(net_vm_change_state_handler
, NULL
);
1583 QTAILQ_INIT(&net_clients
);
1585 if (qemu_opts_foreach(qemu_find_opts("netdev"),
1586 net_init_netdev
, NULL
, errp
)) {
1590 if (qemu_opts_foreach(qemu_find_opts("nic"), net_param_nic
, NULL
, errp
)) {
1594 if (qemu_opts_foreach(qemu_find_opts("net"), net_init_client
, NULL
, errp
)) {
1601 int net_client_parse(QemuOptsList
*opts_list
, const char *optarg
)
1603 if (!qemu_opts_parse_noisily(opts_list
, optarg
, true)) {
1612 uint32_t net_crc32(const uint8_t *p
, int len
)
1619 for (i
= 0; i
< len
; i
++) {
1621 for (j
= 0; j
< 8; j
++) {
1622 carry
= ((crc
& 0x80000000L
) ? 1 : 0) ^ (b
& 0x01);
1626 crc
= ((crc
^ POLYNOMIAL_BE
) | carry
);
1634 uint32_t net_crc32_le(const uint8_t *p
, int len
)
1641 for (i
= 0; i
< len
; i
++) {
1643 for (j
= 0; j
< 8; j
++) {
1644 carry
= (crc
& 0x1) ^ (b
& 0x01);
1648 crc
^= POLYNOMIAL_LE
;
1656 QemuOptsList qemu_netdev_opts
= {
1658 .implied_opt_name
= "type",
1659 .head
= QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts
.head
),
1662 * no elements => accept any params
1663 * validation will happen later
1665 { /* end of list */ }
1669 QemuOptsList qemu_nic_opts
= {
1671 .implied_opt_name
= "type",
1672 .head
= QTAILQ_HEAD_INITIALIZER(qemu_nic_opts
.head
),
1675 * no elements => accept any params
1676 * validation will happen later
1678 { /* end of list */ }
1682 QemuOptsList qemu_net_opts
= {
1684 .implied_opt_name
= "type",
1685 .head
= QTAILQ_HEAD_INITIALIZER(qemu_net_opts
.head
),
1688 * no elements => accept any params
1689 * validation will happen later
1691 { /* end of list */ }
1695 void net_socket_rs_init(SocketReadState
*rs
,
1696 SocketReadStateFinalize
*finalize
,
1700 rs
->vnet_hdr
= vnet_hdr
;
1703 rs
->vnet_hdr_len
= 0;
1704 memset(rs
->buf
, 0, sizeof(rs
->buf
));
1705 rs
->finalize
= finalize
;
1713 int net_fill_rstate(SocketReadState
*rs
, const uint8_t *buf
, int size
)
1718 /* Reassemble a packet from the network.
1719 * 0 = getting length.
1720 * 1 = getting vnet header length.
1723 switch (rs
->state
) {
1729 memcpy(rs
->buf
+ rs
->index
, buf
, l
);
1733 if (rs
->index
== 4) {
1735 rs
->packet_len
= ntohl(*(uint32_t *)rs
->buf
);
1741 rs
->vnet_hdr_len
= 0;
1750 memcpy(rs
->buf
+ rs
->index
, buf
, l
);
1754 if (rs
->index
== 4) {
1755 /* got vnet header length */
1756 rs
->vnet_hdr_len
= ntohl(*(uint32_t *)rs
->buf
);
1762 l
= rs
->packet_len
- rs
->index
;
1766 if (rs
->index
+ l
<= sizeof(rs
->buf
)) {
1767 memcpy(rs
->buf
+ rs
->index
, buf
, l
);
1769 fprintf(stderr
, "serious error: oversized packet received,"
1770 "connection terminated.\n");
1771 rs
->index
= rs
->state
= 0;
1778 if (rs
->index
>= rs
->packet_len
) {
1781 assert(rs
->finalize
);