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
)
234 static ssize_t
qemu_deliver_packet_iov(NetClientState
*sender
,
236 const struct iovec
*iov
,
240 static void qemu_net_client_setup(NetClientState
*nc
,
242 NetClientState
*peer
,
245 NetClientDestructor
*destructor
)
248 nc
->model
= g_strdup(model
);
250 nc
->name
= g_strdup(name
);
252 nc
->name
= assign_name(nc
, model
);
260 QTAILQ_INSERT_TAIL(&net_clients
, nc
, next
);
262 nc
->incoming_queue
= qemu_new_net_queue(qemu_deliver_packet_iov
, nc
);
263 nc
->destructor
= destructor
;
264 QTAILQ_INIT(&nc
->filters
);
267 NetClientState
*qemu_new_net_client(NetClientInfo
*info
,
268 NetClientState
*peer
,
274 assert(info
->size
>= sizeof(NetClientState
));
276 nc
= g_malloc0(info
->size
);
277 qemu_net_client_setup(nc
, info
, peer
, model
, name
,
278 qemu_net_client_destructor
);
283 NICState
*qemu_new_nic(NetClientInfo
*info
,
289 NetClientState
**peers
= conf
->peers
.ncs
;
291 int i
, queues
= MAX(1, conf
->peers
.queues
);
293 assert(info
->type
== NET_CLIENT_DRIVER_NIC
);
294 assert(info
->size
>= sizeof(NICState
));
296 nic
= g_malloc0(info
->size
+ sizeof(NetClientState
) * queues
);
297 nic
->ncs
= (void *)nic
+ info
->size
;
299 nic
->opaque
= opaque
;
301 for (i
= 0; i
< queues
; i
++) {
302 qemu_net_client_setup(&nic
->ncs
[i
], info
, peers
[i
], model
, name
,
304 nic
->ncs
[i
].queue_index
= i
;
310 NetClientState
*qemu_get_subqueue(NICState
*nic
, int queue_index
)
312 return nic
->ncs
+ queue_index
;
315 NetClientState
*qemu_get_queue(NICState
*nic
)
317 return qemu_get_subqueue(nic
, 0);
320 NICState
*qemu_get_nic(NetClientState
*nc
)
322 NetClientState
*nc0
= nc
- nc
->queue_index
;
324 return (NICState
*)((void *)nc0
- nc
->info
->size
);
327 void *qemu_get_nic_opaque(NetClientState
*nc
)
329 NICState
*nic
= qemu_get_nic(nc
);
334 static void qemu_cleanup_net_client(NetClientState
*nc
)
336 QTAILQ_REMOVE(&net_clients
, nc
, next
);
338 if (nc
->info
->cleanup
) {
339 nc
->info
->cleanup(nc
);
343 static void qemu_free_net_client(NetClientState
*nc
)
345 if (nc
->incoming_queue
) {
346 qemu_del_net_queue(nc
->incoming_queue
);
349 nc
->peer
->peer
= NULL
;
353 if (nc
->destructor
) {
358 void qemu_del_net_client(NetClientState
*nc
)
360 NetClientState
*ncs
[MAX_QUEUE_NUM
];
362 NetFilterState
*nf
, *next
;
364 assert(nc
->info
->type
!= NET_CLIENT_DRIVER_NIC
);
366 /* If the NetClientState belongs to a multiqueue backend, we will change all
367 * other NetClientStates also.
369 queues
= qemu_find_net_clients_except(nc
->name
, ncs
,
370 NET_CLIENT_DRIVER_NIC
,
374 QTAILQ_FOREACH_SAFE(nf
, &nc
->filters
, next
, next
) {
375 object_unparent(OBJECT(nf
));
378 /* If there is a peer NIC, delete and cleanup client, but do not free. */
379 if (nc
->peer
&& nc
->peer
->info
->type
== NET_CLIENT_DRIVER_NIC
) {
380 NICState
*nic
= qemu_get_nic(nc
->peer
);
381 if (nic
->peer_deleted
) {
384 nic
->peer_deleted
= true;
386 for (i
= 0; i
< queues
; i
++) {
387 ncs
[i
]->peer
->link_down
= true;
390 if (nc
->peer
->info
->link_status_changed
) {
391 nc
->peer
->info
->link_status_changed(nc
->peer
);
394 for (i
= 0; i
< queues
; i
++) {
395 qemu_cleanup_net_client(ncs
[i
]);
401 for (i
= 0; i
< queues
; i
++) {
402 qemu_cleanup_net_client(ncs
[i
]);
403 qemu_free_net_client(ncs
[i
]);
407 void qemu_del_nic(NICState
*nic
)
409 int i
, queues
= MAX(nic
->conf
->peers
.queues
, 1);
411 qemu_macaddr_set_free(&nic
->conf
->macaddr
);
413 /* If this is a peer NIC and peer has already been deleted, free it now. */
414 if (nic
->peer_deleted
) {
415 for (i
= 0; i
< queues
; i
++) {
416 qemu_free_net_client(qemu_get_subqueue(nic
, i
)->peer
);
420 for (i
= queues
- 1; i
>= 0; i
--) {
421 NetClientState
*nc
= qemu_get_subqueue(nic
, i
);
423 qemu_cleanup_net_client(nc
);
424 qemu_free_net_client(nc
);
430 void qemu_foreach_nic(qemu_nic_foreach func
, void *opaque
)
434 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
435 if (nc
->info
->type
== NET_CLIENT_DRIVER_NIC
) {
436 if (nc
->queue_index
== 0) {
437 func(qemu_get_nic(nc
), opaque
);
443 bool qemu_has_ufo(NetClientState
*nc
)
445 if (!nc
|| !nc
->info
->has_ufo
) {
449 return nc
->info
->has_ufo(nc
);
452 bool qemu_has_vnet_hdr(NetClientState
*nc
)
454 if (!nc
|| !nc
->info
->has_vnet_hdr
) {
458 return nc
->info
->has_vnet_hdr(nc
);
461 bool qemu_has_vnet_hdr_len(NetClientState
*nc
, int len
)
463 if (!nc
|| !nc
->info
->has_vnet_hdr_len
) {
467 return nc
->info
->has_vnet_hdr_len(nc
, len
);
470 void qemu_using_vnet_hdr(NetClientState
*nc
, bool enable
)
472 if (!nc
|| !nc
->info
->using_vnet_hdr
) {
476 nc
->info
->using_vnet_hdr(nc
, enable
);
479 void qemu_set_offload(NetClientState
*nc
, int csum
, int tso4
, int tso6
,
482 if (!nc
|| !nc
->info
->set_offload
) {
486 nc
->info
->set_offload(nc
, csum
, tso4
, tso6
, ecn
, ufo
);
489 void qemu_set_vnet_hdr_len(NetClientState
*nc
, int len
)
491 if (!nc
|| !nc
->info
->set_vnet_hdr_len
) {
495 nc
->vnet_hdr_len
= len
;
496 nc
->info
->set_vnet_hdr_len(nc
, len
);
499 int qemu_set_vnet_le(NetClientState
*nc
, bool is_le
)
501 #ifdef HOST_WORDS_BIGENDIAN
502 if (!nc
|| !nc
->info
->set_vnet_le
) {
506 return nc
->info
->set_vnet_le(nc
, is_le
);
512 int qemu_set_vnet_be(NetClientState
*nc
, bool is_be
)
514 #ifdef HOST_WORDS_BIGENDIAN
517 if (!nc
|| !nc
->info
->set_vnet_be
) {
521 return nc
->info
->set_vnet_be(nc
, is_be
);
525 int qemu_can_send_packet(NetClientState
*sender
)
527 int vm_running
= runstate_is_running();
537 if (sender
->peer
->receive_disabled
) {
539 } else if (sender
->peer
->info
->can_receive
&&
540 !sender
->peer
->info
->can_receive(sender
->peer
)) {
546 static ssize_t
filter_receive_iov(NetClientState
*nc
,
547 NetFilterDirection direction
,
548 NetClientState
*sender
,
550 const struct iovec
*iov
,
552 NetPacketSent
*sent_cb
)
555 NetFilterState
*nf
= NULL
;
557 if (direction
== NET_FILTER_DIRECTION_TX
) {
558 QTAILQ_FOREACH(nf
, &nc
->filters
, next
) {
559 ret
= qemu_netfilter_receive(nf
, direction
, sender
, flags
, iov
,
566 QTAILQ_FOREACH_REVERSE(nf
, &nc
->filters
, NetFilterHead
, next
) {
567 ret
= qemu_netfilter_receive(nf
, direction
, sender
, flags
, iov
,
578 static ssize_t
filter_receive(NetClientState
*nc
,
579 NetFilterDirection direction
,
580 NetClientState
*sender
,
584 NetPacketSent
*sent_cb
)
587 .iov_base
= (void *)data
,
591 return filter_receive_iov(nc
, direction
, sender
, flags
, &iov
, 1, sent_cb
);
594 void qemu_purge_queued_packets(NetClientState
*nc
)
600 qemu_net_queue_purge(nc
->peer
->incoming_queue
, nc
);
603 void qemu_flush_or_purge_queued_packets(NetClientState
*nc
, bool purge
)
605 nc
->receive_disabled
= 0;
607 if (nc
->peer
&& nc
->peer
->info
->type
== NET_CLIENT_DRIVER_HUBPORT
) {
608 if (net_hub_flush(nc
->peer
)) {
612 if (qemu_net_queue_flush(nc
->incoming_queue
)) {
613 /* We emptied the queue successfully, signal to the IO thread to repoll
614 * the file descriptor (for tap, for example).
618 /* Unable to empty the queue, purge remaining packets */
619 qemu_net_queue_purge(nc
->incoming_queue
, nc
);
623 void qemu_flush_queued_packets(NetClientState
*nc
)
625 qemu_flush_or_purge_queued_packets(nc
, false);
628 static ssize_t
qemu_send_packet_async_with_flags(NetClientState
*sender
,
630 const uint8_t *buf
, int size
,
631 NetPacketSent
*sent_cb
)
637 printf("qemu_send_packet_async:\n");
638 qemu_hexdump((const char *)buf
, stdout
, "net", size
);
641 if (sender
->link_down
|| !sender
->peer
) {
645 /* Let filters handle the packet first */
646 ret
= filter_receive(sender
, NET_FILTER_DIRECTION_TX
,
647 sender
, flags
, buf
, size
, sent_cb
);
652 ret
= filter_receive(sender
->peer
, NET_FILTER_DIRECTION_RX
,
653 sender
, flags
, buf
, size
, sent_cb
);
658 queue
= sender
->peer
->incoming_queue
;
660 return qemu_net_queue_send(queue
, sender
, flags
, buf
, size
, sent_cb
);
663 ssize_t
qemu_send_packet_async(NetClientState
*sender
,
664 const uint8_t *buf
, int size
,
665 NetPacketSent
*sent_cb
)
667 return qemu_send_packet_async_with_flags(sender
, QEMU_NET_PACKET_FLAG_NONE
,
671 void qemu_send_packet(NetClientState
*nc
, const uint8_t *buf
, int size
)
673 qemu_send_packet_async(nc
, buf
, size
, NULL
);
676 ssize_t
qemu_send_packet_raw(NetClientState
*nc
, const uint8_t *buf
, int size
)
678 return qemu_send_packet_async_with_flags(nc
, QEMU_NET_PACKET_FLAG_RAW
,
682 static ssize_t
nc_sendv_compat(NetClientState
*nc
, const struct iovec
*iov
,
683 int iovcnt
, unsigned flags
)
691 buffer
= iov
[0].iov_base
;
692 offset
= iov
[0].iov_len
;
694 offset
= iov_size(iov
, iovcnt
);
695 if (offset
> NET_BUFSIZE
) {
698 buf
= g_malloc(offset
);
700 offset
= iov_to_buf(iov
, iovcnt
, 0, buf
, offset
);
703 if (flags
& QEMU_NET_PACKET_FLAG_RAW
&& nc
->info
->receive_raw
) {
704 ret
= nc
->info
->receive_raw(nc
, buffer
, offset
);
706 ret
= nc
->info
->receive(nc
, buffer
, offset
);
713 static ssize_t
qemu_deliver_packet_iov(NetClientState
*sender
,
715 const struct iovec
*iov
,
719 NetClientState
*nc
= opaque
;
724 return iov_size(iov
, iovcnt
);
727 if (nc
->receive_disabled
) {
731 if (nc
->info
->receive_iov
&& !(flags
& QEMU_NET_PACKET_FLAG_RAW
)) {
732 ret
= nc
->info
->receive_iov(nc
, iov
, iovcnt
);
734 ret
= nc_sendv_compat(nc
, iov
, iovcnt
, flags
);
738 nc
->receive_disabled
= 1;
744 ssize_t
qemu_sendv_packet_async(NetClientState
*sender
,
745 const struct iovec
*iov
, int iovcnt
,
746 NetPacketSent
*sent_cb
)
749 size_t size
= iov_size(iov
, iovcnt
);
752 if (size
> NET_BUFSIZE
) {
756 if (sender
->link_down
|| !sender
->peer
) {
760 /* Let filters handle the packet first */
761 ret
= filter_receive_iov(sender
, NET_FILTER_DIRECTION_TX
, sender
,
762 QEMU_NET_PACKET_FLAG_NONE
, iov
, iovcnt
, sent_cb
);
767 ret
= filter_receive_iov(sender
->peer
, NET_FILTER_DIRECTION_RX
, sender
,
768 QEMU_NET_PACKET_FLAG_NONE
, iov
, iovcnt
, sent_cb
);
773 queue
= sender
->peer
->incoming_queue
;
775 return qemu_net_queue_send_iov(queue
, sender
,
776 QEMU_NET_PACKET_FLAG_NONE
,
777 iov
, iovcnt
, sent_cb
);
781 qemu_sendv_packet(NetClientState
*nc
, const struct iovec
*iov
, int iovcnt
)
783 return qemu_sendv_packet_async(nc
, iov
, iovcnt
, NULL
);
786 NetClientState
*qemu_find_netdev(const char *id
)
790 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
791 if (nc
->info
->type
== NET_CLIENT_DRIVER_NIC
)
793 if (!strcmp(nc
->name
, id
)) {
801 int qemu_find_net_clients_except(const char *id
, NetClientState
**ncs
,
802 NetClientDriver type
, int max
)
807 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
808 if (nc
->info
->type
== type
) {
811 if (!id
|| !strcmp(nc
->name
, id
)) {
822 static int nic_get_free_idx(void)
826 for (index
= 0; index
< MAX_NICS
; index
++)
827 if (!nd_table
[index
].used
)
832 int qemu_show_nic_models(const char *arg
, const char *const *models
)
836 if (!arg
|| !is_help_option(arg
)) {
840 fprintf(stderr
, "qemu: Supported NIC models: ");
841 for (i
= 0 ; models
[i
]; i
++)
842 fprintf(stderr
, "%s%c", models
[i
], models
[i
+1] ? ',' : '\n');
846 void qemu_check_nic_model(NICInfo
*nd
, const char *model
)
848 const char *models
[2];
853 if (qemu_show_nic_models(nd
->model
, models
))
855 if (qemu_find_nic_model(nd
, models
, model
) < 0)
859 int qemu_find_nic_model(NICInfo
*nd
, const char * const *models
,
860 const char *default_model
)
865 nd
->model
= g_strdup(default_model
);
867 for (i
= 0 ; models
[i
]; i
++) {
868 if (strcmp(nd
->model
, models
[i
]) == 0)
872 error_report("Unsupported NIC model: %s", nd
->model
);
876 static int net_init_nic(const Netdev
*netdev
, const char *name
,
877 NetClientState
*peer
, Error
**errp
)
881 const NetLegacyNicOptions
*nic
;
883 assert(netdev
->type
== NET_CLIENT_DRIVER_NIC
);
884 nic
= &netdev
->u
.nic
;
886 idx
= nic_get_free_idx();
887 if (idx
== -1 || nb_nics
>= MAX_NICS
) {
888 error_setg(errp
, "too many NICs");
894 memset(nd
, 0, sizeof(*nd
));
896 if (nic
->has_netdev
) {
897 nd
->netdev
= qemu_find_netdev(nic
->netdev
);
899 error_setg(errp
, "netdev '%s' not found", nic
->netdev
);
906 nd
->name
= g_strdup(name
);
907 if (nic
->has_model
) {
908 nd
->model
= g_strdup(nic
->model
);
911 nd
->devaddr
= g_strdup(nic
->addr
);
914 if (nic
->has_macaddr
&&
915 net_parse_macaddr(nd
->macaddr
.a
, nic
->macaddr
) < 0) {
916 error_setg(errp
, "invalid syntax for ethernet address");
919 if (nic
->has_macaddr
&&
920 is_multicast_ether_addr(nd
->macaddr
.a
)) {
922 "NIC cannot have multicast MAC address (odd 1st byte)");
925 qemu_macaddr_default_if_unset(&nd
->macaddr
);
927 if (nic
->has_vectors
) {
928 if (nic
->vectors
> 0x7ffffff) {
929 error_setg(errp
, "invalid # of vectors: %"PRIu32
, nic
->vectors
);
932 nd
->nvectors
= nic
->vectors
;
934 nd
->nvectors
= DEV_NVECTORS_UNSPECIFIED
;
944 static int (* const net_client_init_fun
[NET_CLIENT_DRIVER__MAX
])(
945 const Netdev
*netdev
,
947 NetClientState
*peer
, Error
**errp
) = {
948 [NET_CLIENT_DRIVER_NIC
] = net_init_nic
,
950 [NET_CLIENT_DRIVER_USER
] = net_init_slirp
,
952 [NET_CLIENT_DRIVER_TAP
] = net_init_tap
,
953 [NET_CLIENT_DRIVER_SOCKET
] = net_init_socket
,
955 [NET_CLIENT_DRIVER_VDE
] = net_init_vde
,
958 [NET_CLIENT_DRIVER_NETMAP
] = net_init_netmap
,
960 #ifdef CONFIG_NET_BRIDGE
961 [NET_CLIENT_DRIVER_BRIDGE
] = net_init_bridge
,
963 [NET_CLIENT_DRIVER_HUBPORT
] = net_init_hubport
,
964 #ifdef CONFIG_VHOST_NET_USED
965 [NET_CLIENT_DRIVER_VHOST_USER
] = net_init_vhost_user
,
968 [NET_CLIENT_DRIVER_L2TPV3
] = net_init_l2tpv3
,
973 static int net_client_init1(const void *object
, bool is_netdev
, Error
**errp
)
976 const Netdev
*netdev
;
978 NetClientState
*peer
= NULL
;
984 if (netdev
->type
== NET_CLIENT_DRIVER_NIC
||
985 !net_client_init_fun
[netdev
->type
]) {
986 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "type",
987 "a netdev backend type");
991 const NetLegacy
*net
= object
;
992 const NetLegacyOptions
*opts
= net
->opts
;
995 /* missing optional values have been initialized to "all bits zero" */
996 name
= net
->has_id
? net
->id
: net
->name
;
999 warn_report("The 'name' parameter is deprecated, use 'id' instead");
1002 /* Map the old options to the new flat type */
1003 switch (opts
->type
) {
1004 case NET_LEGACY_OPTIONS_TYPE_NONE
:
1005 return 0; /* nothing to do */
1006 case NET_LEGACY_OPTIONS_TYPE_NIC
:
1007 legacy
.type
= NET_CLIENT_DRIVER_NIC
;
1008 legacy
.u
.nic
= opts
->u
.nic
;
1010 case NET_LEGACY_OPTIONS_TYPE_USER
:
1011 legacy
.type
= NET_CLIENT_DRIVER_USER
;
1012 legacy
.u
.user
= opts
->u
.user
;
1014 case NET_LEGACY_OPTIONS_TYPE_TAP
:
1015 legacy
.type
= NET_CLIENT_DRIVER_TAP
;
1016 legacy
.u
.tap
= opts
->u
.tap
;
1018 case NET_LEGACY_OPTIONS_TYPE_L2TPV3
:
1019 legacy
.type
= NET_CLIENT_DRIVER_L2TPV3
;
1020 legacy
.u
.l2tpv3
= opts
->u
.l2tpv3
;
1022 case NET_LEGACY_OPTIONS_TYPE_SOCKET
:
1023 legacy
.type
= NET_CLIENT_DRIVER_SOCKET
;
1024 legacy
.u
.socket
= opts
->u
.socket
;
1026 case NET_LEGACY_OPTIONS_TYPE_VDE
:
1027 legacy
.type
= NET_CLIENT_DRIVER_VDE
;
1028 legacy
.u
.vde
= opts
->u
.vde
;
1030 case NET_LEGACY_OPTIONS_TYPE_BRIDGE
:
1031 legacy
.type
= NET_CLIENT_DRIVER_BRIDGE
;
1032 legacy
.u
.bridge
= opts
->u
.bridge
;
1034 case NET_LEGACY_OPTIONS_TYPE_NETMAP
:
1035 legacy
.type
= NET_CLIENT_DRIVER_NETMAP
;
1036 legacy
.u
.netmap
= opts
->u
.netmap
;
1038 case NET_LEGACY_OPTIONS_TYPE_VHOST_USER
:
1039 legacy
.type
= NET_CLIENT_DRIVER_VHOST_USER
;
1040 legacy
.u
.vhost_user
= opts
->u
.vhost_user
;
1046 if (!net_client_init_fun
[netdev
->type
]) {
1047 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "type",
1048 "a net backend type (maybe it is not compiled "
1049 "into this binary)");
1053 /* Do not add to a hub if it's a nic with a netdev= parameter. */
1054 if (netdev
->type
!= NET_CLIENT_DRIVER_NIC
||
1055 !opts
->u
.nic
.has_netdev
) {
1056 peer
= net_hub_add_port(0, NULL
, NULL
);
1060 if (net_client_init_fun
[netdev
->type
](netdev
, name
, peer
, errp
) < 0) {
1061 /* FIXME drop when all init functions store an Error */
1062 if (errp
&& !*errp
) {
1063 error_setg(errp
, QERR_DEVICE_INIT_FAILED
,
1064 NetClientDriver_str(netdev
->type
));
1071 static void show_netdevs(void)
1074 const char *available_netdevs
[] = {
1081 #ifdef CONFIG_L2TPV3
1087 #ifdef CONFIG_NET_BRIDGE
1090 #ifdef CONFIG_NETMAP
1098 printf("Available netdev backend types:\n");
1099 for (idx
= 0; idx
< ARRAY_SIZE(available_netdevs
); idx
++) {
1100 puts(available_netdevs
[idx
]);
1104 static int net_client_init(QemuOpts
*opts
, bool is_netdev
, Error
**errp
)
1106 void *object
= NULL
;
1109 Visitor
*v
= opts_visitor_new(opts
);
1111 const char *type
= qemu_opt_get(opts
, "type");
1113 if (is_netdev
&& type
&& is_help_option(type
)) {
1117 /* Parse convenience option format ip6-net=fec0::0[/64] */
1118 const char *ip6_net
= qemu_opt_get(opts
, "ipv6-net");
1121 char buf
[strlen(ip6_net
) + 1];
1123 if (get_str_sep(buf
, sizeof(buf
), &ip6_net
, '/') < 0) {
1124 /* Default 64bit prefix length. */
1125 qemu_opt_set(opts
, "ipv6-prefix", ip6_net
, &error_abort
);
1126 qemu_opt_set_number(opts
, "ipv6-prefixlen", 64, &error_abort
);
1128 /* User-specified prefix length. */
1132 qemu_opt_set(opts
, "ipv6-prefix", buf
, &error_abort
);
1133 err
= qemu_strtoul(ip6_net
, NULL
, 10, &len
);
1136 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
1137 "ipv6-prefix", "a number");
1139 qemu_opt_set_number(opts
, "ipv6-prefixlen", len
,
1143 qemu_opt_unset(opts
, "ipv6-net");
1148 visit_type_Netdev(v
, NULL
, (Netdev
**)&object
, &err
);
1150 visit_type_NetLegacy(v
, NULL
, (NetLegacy
**)&object
, &err
);
1154 ret
= net_client_init1(object
, is_netdev
, &err
);
1158 qapi_free_Netdev(object
);
1160 qapi_free_NetLegacy(object
);
1163 error_propagate(errp
, err
);
1168 void netdev_add(QemuOpts
*opts
, Error
**errp
)
1170 net_client_init(opts
, true, errp
);
1173 void qmp_netdev_add(QDict
*qdict
, QObject
**ret
, Error
**errp
)
1175 Error
*local_err
= NULL
;
1176 QemuOptsList
*opts_list
;
1179 opts_list
= qemu_find_opts_err("netdev", &local_err
);
1184 opts
= qemu_opts_from_qdict(opts_list
, qdict
, &local_err
);
1189 netdev_add(opts
, &local_err
);
1191 qemu_opts_del(opts
);
1196 error_propagate(errp
, local_err
);
1199 void qmp_netdev_del(const char *id
, Error
**errp
)
1204 nc
= qemu_find_netdev(id
);
1206 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1207 "Device '%s' not found", id
);
1211 opts
= qemu_opts_find(qemu_find_opts_err("netdev", NULL
), id
);
1213 error_setg(errp
, "Device '%s' is not a netdev", id
);
1217 qemu_del_net_client(nc
);
1218 qemu_opts_del(opts
);
1221 static void netfilter_print_info(Monitor
*mon
, NetFilterState
*nf
)
1224 ObjectProperty
*prop
;
1225 ObjectPropertyIterator iter
;
1228 /* generate info str */
1229 object_property_iter_init(&iter
, OBJECT(nf
));
1230 while ((prop
= object_property_iter_next(&iter
))) {
1231 if (!strcmp(prop
->name
, "type")) {
1234 v
= string_output_visitor_new(false, &str
);
1235 object_property_get(OBJECT(nf
), v
, prop
->name
, NULL
);
1236 visit_complete(v
, &str
);
1238 monitor_printf(mon
, ",%s=%s", prop
->name
, str
);
1241 monitor_printf(mon
, "\n");
1244 void print_net_client(Monitor
*mon
, NetClientState
*nc
)
1248 monitor_printf(mon
, "%s: index=%d,type=%s,%s\n", nc
->name
,
1250 NetClientDriver_str(nc
->info
->type
),
1252 if (!QTAILQ_EMPTY(&nc
->filters
)) {
1253 monitor_printf(mon
, "filters:\n");
1255 QTAILQ_FOREACH(nf
, &nc
->filters
, next
) {
1256 char *path
= object_get_canonical_path_component(OBJECT(nf
));
1258 monitor_printf(mon
, " - %s: type=%s", path
,
1259 object_get_typename(OBJECT(nf
)));
1260 netfilter_print_info(mon
, nf
);
1265 RxFilterInfoList
*qmp_query_rx_filter(bool has_name
, const char *name
,
1269 RxFilterInfoList
*filter_list
= NULL
, *last_entry
= NULL
;
1271 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
1272 RxFilterInfoList
*entry
;
1275 if (has_name
&& strcmp(nc
->name
, name
) != 0) {
1279 /* only query rx-filter information of NIC */
1280 if (nc
->info
->type
!= NET_CLIENT_DRIVER_NIC
) {
1282 error_setg(errp
, "net client(%s) isn't a NIC", name
);
1288 /* only query information on queue 0 since the info is per nic,
1291 if (nc
->queue_index
!= 0)
1294 if (nc
->info
->query_rx_filter
) {
1295 info
= nc
->info
->query_rx_filter(nc
);
1296 entry
= g_malloc0(sizeof(*entry
));
1297 entry
->value
= info
;
1300 filter_list
= entry
;
1302 last_entry
->next
= entry
;
1305 } else if (has_name
) {
1306 error_setg(errp
, "net client(%s) doesn't support"
1307 " rx-filter querying", name
);
1316 if (filter_list
== NULL
&& has_name
) {
1317 error_setg(errp
, "invalid net client name: %s", name
);
1323 void hmp_info_network(Monitor
*mon
, const QDict
*qdict
)
1325 NetClientState
*nc
, *peer
;
1326 NetClientDriver type
;
1330 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
1332 type
= nc
->info
->type
;
1334 /* Skip if already printed in hub info */
1335 if (net_hub_id_for_client(nc
, NULL
) == 0) {
1339 if (!peer
|| type
== NET_CLIENT_DRIVER_NIC
) {
1340 print_net_client(mon
, nc
);
1341 } /* else it's a netdev connected to a NIC, printed with the NIC */
1342 if (peer
&& type
== NET_CLIENT_DRIVER_NIC
) {
1343 monitor_printf(mon
, " \\ ");
1344 print_net_client(mon
, peer
);
1349 void colo_notify_filters_event(int event
, Error
**errp
)
1353 NetFilterClass
*nfc
= NULL
;
1354 Error
*local_err
= NULL
;
1356 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
1357 QTAILQ_FOREACH(nf
, &nc
->filters
, next
) {
1358 nfc
= NETFILTER_GET_CLASS(OBJECT(nf
));
1359 nfc
->handle_event(nf
, event
, &local_err
);
1361 error_propagate(errp
, local_err
);
1368 void qmp_set_link(const char *name
, bool up
, Error
**errp
)
1370 NetClientState
*ncs
[MAX_QUEUE_NUM
];
1374 queues
= qemu_find_net_clients_except(name
, ncs
,
1375 NET_CLIENT_DRIVER__MAX
,
1379 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1380 "Device '%s' not found", name
);
1385 for (i
= 0; i
< queues
; i
++) {
1386 ncs
[i
]->link_down
= !up
;
1389 if (nc
->info
->link_status_changed
) {
1390 nc
->info
->link_status_changed(nc
);
1394 /* Change peer link only if the peer is NIC and then notify peer.
1395 * If the peer is a HUBPORT or a backend, we do not change the
1398 * This behavior is compatible with qemu hubs where there could be
1399 * multiple clients that can still communicate with each other in
1400 * disconnected mode. For now maintain this compatibility.
1402 if (nc
->peer
->info
->type
== NET_CLIENT_DRIVER_NIC
) {
1403 for (i
= 0; i
< queues
; i
++) {
1404 ncs
[i
]->peer
->link_down
= !up
;
1407 if (nc
->peer
->info
->link_status_changed
) {
1408 nc
->peer
->info
->link_status_changed(nc
->peer
);
1413 static void net_vm_change_state_handler(void *opaque
, int running
,
1417 NetClientState
*tmp
;
1419 QTAILQ_FOREACH_SAFE(nc
, &net_clients
, next
, tmp
) {
1421 /* Flush queued packets and wake up backends. */
1422 if (nc
->peer
&& qemu_can_send_packet(nc
)) {
1423 qemu_flush_queued_packets(nc
->peer
);
1426 /* Complete all queued packets, to guarantee we don't modify
1427 * state later when VM is not running.
1429 qemu_flush_or_purge_queued_packets(nc
, true);
1434 void net_cleanup(void)
1438 /* We may del multiple entries during qemu_del_net_client(),
1439 * so QTAILQ_FOREACH_SAFE() is also not safe here.
1441 while (!QTAILQ_EMPTY(&net_clients
)) {
1442 nc
= QTAILQ_FIRST(&net_clients
);
1443 if (nc
->info
->type
== NET_CLIENT_DRIVER_NIC
) {
1444 qemu_del_nic(qemu_get_nic(nc
));
1446 qemu_del_net_client(nc
);
1450 qemu_del_vm_change_state_handler(net_change_state_entry
);
1453 void net_check_clients(void)
1458 net_hub_check_clients();
1460 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
1462 warn_report("%s %s has no peer",
1463 nc
->info
->type
== NET_CLIENT_DRIVER_NIC
1469 /* Check that all NICs requested via -net nic actually got created.
1470 * NICs created via -device don't need to be checked here because
1471 * they are always instantiated.
1473 for (i
= 0; i
< MAX_NICS
; i
++) {
1474 NICInfo
*nd
= &nd_table
[i
];
1475 if (nd
->used
&& !nd
->instantiated
) {
1476 warn_report("requested NIC (%s, model %s) "
1477 "was not created (not supported by this machine?)",
1478 nd
->name
? nd
->name
: "anonymous",
1479 nd
->model
? nd
->model
: "unspecified");
1484 static int net_init_client(void *dummy
, QemuOpts
*opts
, Error
**errp
)
1486 return net_client_init(opts
, false, errp
);
1489 static int net_init_netdev(void *dummy
, QemuOpts
*opts
, Error
**errp
)
1491 return net_client_init(opts
, true, errp
);
1494 /* For the convenience "--nic" parameter */
1495 static int net_param_nic(void *dummy
, QemuOpts
*opts
, Error
**errp
)
1502 type
= qemu_opt_get(opts
, "type");
1503 if (type
&& g_str_equal(type
, "none")) {
1504 return 0; /* Nothing to do, default_net is cleared in vl.c */
1507 idx
= nic_get_free_idx();
1508 if (idx
== -1 || nb_nics
>= MAX_NICS
) {
1509 error_setg(errp
, "no more on-board/default NIC slots available");
1514 qemu_opt_set(opts
, "type", "user", &error_abort
);
1517 ni
= &nd_table
[idx
];
1518 memset(ni
, 0, sizeof(*ni
));
1519 ni
->model
= qemu_opt_get_del(opts
, "model");
1521 /* Create an ID if the user did not specify one */
1522 nd_id
= g_strdup(qemu_opts_id(opts
));
1524 nd_id
= g_strdup_printf("__org.qemu.nic%i\n", idx
);
1525 qemu_opts_set_id(opts
, nd_id
);
1528 /* Handle MAC address */
1529 mac
= qemu_opt_get_del(opts
, "mac");
1531 ret
= net_parse_macaddr(ni
->macaddr
.a
, mac
);
1534 error_setg(errp
, "invalid syntax for ethernet address");
1537 if (is_multicast_ether_addr(ni
->macaddr
.a
)) {
1538 error_setg(errp
, "NIC cannot have multicast MAC address");
1543 qemu_macaddr_default_if_unset(&ni
->macaddr
);
1545 ret
= net_client_init(opts
, true, errp
);
1547 ni
->netdev
= qemu_find_netdev(nd_id
);
1557 int net_init_clients(Error
**errp
)
1559 net_change_state_entry
=
1560 qemu_add_vm_change_state_handler(net_vm_change_state_handler
, NULL
);
1562 QTAILQ_INIT(&net_clients
);
1564 if (qemu_opts_foreach(qemu_find_opts("netdev"),
1565 net_init_netdev
, NULL
, errp
)) {
1569 if (qemu_opts_foreach(qemu_find_opts("nic"), net_param_nic
, NULL
, errp
)) {
1573 if (qemu_opts_foreach(qemu_find_opts("net"), net_init_client
, NULL
, errp
)) {
1580 int net_client_parse(QemuOptsList
*opts_list
, const char *optarg
)
1582 if (!qemu_opts_parse_noisily(opts_list
, optarg
, true)) {
1591 uint32_t net_crc32(const uint8_t *p
, int len
)
1598 for (i
= 0; i
< len
; i
++) {
1600 for (j
= 0; j
< 8; j
++) {
1601 carry
= ((crc
& 0x80000000L
) ? 1 : 0) ^ (b
& 0x01);
1605 crc
= ((crc
^ POLYNOMIAL_BE
) | carry
);
1613 uint32_t net_crc32_le(const uint8_t *p
, int len
)
1620 for (i
= 0; i
< len
; i
++) {
1622 for (j
= 0; j
< 8; j
++) {
1623 carry
= (crc
& 0x1) ^ (b
& 0x01);
1627 crc
^= POLYNOMIAL_LE
;
1635 QemuOptsList qemu_netdev_opts
= {
1637 .implied_opt_name
= "type",
1638 .head
= QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts
.head
),
1641 * no elements => accept any params
1642 * validation will happen later
1644 { /* end of list */ }
1648 QemuOptsList qemu_nic_opts
= {
1650 .implied_opt_name
= "type",
1651 .head
= QTAILQ_HEAD_INITIALIZER(qemu_nic_opts
.head
),
1654 * no elements => accept any params
1655 * validation will happen later
1657 { /* end of list */ }
1661 QemuOptsList qemu_net_opts
= {
1663 .implied_opt_name
= "type",
1664 .head
= QTAILQ_HEAD_INITIALIZER(qemu_net_opts
.head
),
1667 * no elements => accept any params
1668 * validation will happen later
1670 { /* end of list */ }
1674 void net_socket_rs_init(SocketReadState
*rs
,
1675 SocketReadStateFinalize
*finalize
,
1679 rs
->vnet_hdr
= vnet_hdr
;
1682 rs
->vnet_hdr_len
= 0;
1683 memset(rs
->buf
, 0, sizeof(rs
->buf
));
1684 rs
->finalize
= finalize
;
1692 int net_fill_rstate(SocketReadState
*rs
, const uint8_t *buf
, int size
)
1697 /* Reassemble a packet from the network.
1698 * 0 = getting length.
1699 * 1 = getting vnet header length.
1702 switch (rs
->state
) {
1708 memcpy(rs
->buf
+ rs
->index
, buf
, l
);
1712 if (rs
->index
== 4) {
1714 rs
->packet_len
= ntohl(*(uint32_t *)rs
->buf
);
1720 rs
->vnet_hdr_len
= 0;
1729 memcpy(rs
->buf
+ rs
->index
, buf
, l
);
1733 if (rs
->index
== 4) {
1734 /* got vnet header length */
1735 rs
->vnet_hdr_len
= ntohl(*(uint32_t *)rs
->buf
);
1741 l
= rs
->packet_len
- rs
->index
;
1745 if (rs
->index
+ l
<= sizeof(rs
->buf
)) {
1746 memcpy(rs
->buf
+ rs
->index
, buf
, l
);
1748 fprintf(stderr
, "serious error: oversized packet received,"
1749 "connection terminated.\n");
1750 rs
->index
= rs
->state
= 0;
1757 if (rs
->index
>= rs
->packet_len
) {
1760 assert(rs
->finalize
);