4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 #include "config-host.h"
29 #include "net/slirp.h"
33 #include "monitor/monitor.h"
34 #include "qemu-common.h"
35 #include "qapi/qmp/qerror.h"
36 #include "qemu/error-report.h"
37 #include "qemu/sockets.h"
38 #include "qemu/config-file.h"
39 #include "qmp-commands.h"
42 #include "qemu/main-loop.h"
43 #include "qapi-visit.h"
44 #include "qapi/opts-visitor.h"
45 #include "qapi/dealloc-visitor.h"
46 #include "sysemu/sysemu.h"
47 #include "net/filter.h"
49 /* Net bridge is currently not supported for W32. */
51 # define CONFIG_NET_BRIDGE
54 static VMChangeStateEntry
*net_change_state_entry
;
55 static QTAILQ_HEAD(, NetClientState
) net_clients
;
57 const char *host_net_devices
[] = {
61 #ifdef CONFIG_NET_BRIDGE
79 /***********************************************************/
80 /* network device redirectors */
82 #if defined(DEBUG_NET)
83 static void hex_dump(FILE *f
, const uint8_t *buf
, int size
)
87 for(i
=0;i
<size
;i
+=16) {
91 fprintf(f
, "%08x ", i
);
94 fprintf(f
, " %02x", buf
[i
+j
]);
101 if (c
< ' ' || c
> '~')
110 static int get_str_sep(char *buf
, int buf_size
, const char **pp
, int sep
)
121 if (len
> buf_size
- 1)
130 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
)
138 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
140 saddr
->sin_family
= AF_INET
;
141 if (buf
[0] == '\0') {
142 saddr
->sin_addr
.s_addr
= 0;
144 if (qemu_isdigit(buf
[0])) {
145 if (!inet_aton(buf
, &saddr
->sin_addr
))
148 if ((he
= gethostbyname(buf
)) == NULL
)
150 saddr
->sin_addr
= *(struct in_addr
*)he
->h_addr
;
153 port
= strtol(p
, (char **)&r
, 0);
156 saddr
->sin_port
= htons(port
);
160 char *qemu_mac_strdup_printf(const uint8_t *macaddr
)
162 return g_strdup_printf("%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",
163 macaddr
[0], macaddr
[1], macaddr
[2],
164 macaddr
[3], macaddr
[4], macaddr
[5]);
167 void qemu_format_nic_info_str(NetClientState
*nc
, uint8_t macaddr
[6])
169 snprintf(nc
->info_str
, sizeof(nc
->info_str
),
170 "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
172 macaddr
[0], macaddr
[1], macaddr
[2],
173 macaddr
[3], macaddr
[4], macaddr
[5]);
176 static int mac_table
[256] = {0};
178 static void qemu_macaddr_set_used(MACAddr
*macaddr
)
182 for (index
= 0x56; index
< 0xFF; index
++) {
183 if (macaddr
->a
[5] == index
) {
189 static void qemu_macaddr_set_free(MACAddr
*macaddr
)
192 static const MACAddr base
= { .a
= { 0x52, 0x54, 0x00, 0x12, 0x34, 0 } };
194 if (memcmp(macaddr
->a
, &base
.a
, (sizeof(base
.a
) - 1)) != 0) {
197 for (index
= 0x56; index
< 0xFF; index
++) {
198 if (macaddr
->a
[5] == index
) {
204 static int qemu_macaddr_get_free(void)
208 for (index
= 0x56; index
< 0xFF; index
++) {
209 if (mac_table
[index
] == 0) {
217 void qemu_macaddr_default_if_unset(MACAddr
*macaddr
)
219 static const MACAddr zero
= { .a
= { 0,0,0,0,0,0 } };
220 static const MACAddr base
= { .a
= { 0x52, 0x54, 0x00, 0x12, 0x34, 0 } };
222 if (memcmp(macaddr
, &zero
, sizeof(zero
)) != 0) {
223 if (memcmp(macaddr
->a
, &base
.a
, (sizeof(base
.a
) - 1)) != 0) {
226 qemu_macaddr_set_used(macaddr
);
231 macaddr
->a
[0] = 0x52;
232 macaddr
->a
[1] = 0x54;
233 macaddr
->a
[2] = 0x00;
234 macaddr
->a
[3] = 0x12;
235 macaddr
->a
[4] = 0x34;
236 macaddr
->a
[5] = qemu_macaddr_get_free();
237 qemu_macaddr_set_used(macaddr
);
241 * Generate a name for net client
243 * Only net clients created with the legacy -net option and NICs need this.
245 static char *assign_name(NetClientState
*nc1
, const char *model
)
250 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
254 if (strcmp(nc
->model
, model
) == 0) {
259 return g_strdup_printf("%s.%d", model
, id
);
262 static void qemu_net_client_destructor(NetClientState
*nc
)
267 static void qemu_net_client_setup(NetClientState
*nc
,
269 NetClientState
*peer
,
272 NetClientDestructor
*destructor
)
275 nc
->model
= g_strdup(model
);
277 nc
->name
= g_strdup(name
);
279 nc
->name
= assign_name(nc
, model
);
287 QTAILQ_INSERT_TAIL(&net_clients
, nc
, next
);
289 nc
->incoming_queue
= qemu_new_net_queue(qemu_deliver_packet_iov
, nc
);
290 nc
->destructor
= destructor
;
291 QTAILQ_INIT(&nc
->filters
);
294 NetClientState
*qemu_new_net_client(NetClientInfo
*info
,
295 NetClientState
*peer
,
301 assert(info
->size
>= sizeof(NetClientState
));
303 nc
= g_malloc0(info
->size
);
304 qemu_net_client_setup(nc
, info
, peer
, model
, name
,
305 qemu_net_client_destructor
);
310 NICState
*qemu_new_nic(NetClientInfo
*info
,
316 NetClientState
**peers
= conf
->peers
.ncs
;
318 int i
, queues
= MAX(1, conf
->peers
.queues
);
320 assert(info
->type
== NET_CLIENT_OPTIONS_KIND_NIC
);
321 assert(info
->size
>= sizeof(NICState
));
323 nic
= g_malloc0(info
->size
+ sizeof(NetClientState
) * queues
);
324 nic
->ncs
= (void *)nic
+ info
->size
;
326 nic
->opaque
= opaque
;
328 for (i
= 0; i
< queues
; i
++) {
329 qemu_net_client_setup(&nic
->ncs
[i
], info
, peers
[i
], model
, name
,
331 nic
->ncs
[i
].queue_index
= i
;
337 NetClientState
*qemu_get_subqueue(NICState
*nic
, int queue_index
)
339 return nic
->ncs
+ queue_index
;
342 NetClientState
*qemu_get_queue(NICState
*nic
)
344 return qemu_get_subqueue(nic
, 0);
347 NICState
*qemu_get_nic(NetClientState
*nc
)
349 NetClientState
*nc0
= nc
- nc
->queue_index
;
351 return (NICState
*)((void *)nc0
- nc
->info
->size
);
354 void *qemu_get_nic_opaque(NetClientState
*nc
)
356 NICState
*nic
= qemu_get_nic(nc
);
361 static void qemu_cleanup_net_client(NetClientState
*nc
)
363 QTAILQ_REMOVE(&net_clients
, nc
, next
);
365 if (nc
->info
->cleanup
) {
366 nc
->info
->cleanup(nc
);
370 static void qemu_free_net_client(NetClientState
*nc
)
372 if (nc
->incoming_queue
) {
373 qemu_del_net_queue(nc
->incoming_queue
);
376 nc
->peer
->peer
= NULL
;
380 if (nc
->destructor
) {
385 void qemu_del_net_client(NetClientState
*nc
)
387 NetClientState
*ncs
[MAX_QUEUE_NUM
];
389 NetFilterState
*nf
, *next
;
391 assert(nc
->info
->type
!= NET_CLIENT_OPTIONS_KIND_NIC
);
393 /* If the NetClientState belongs to a multiqueue backend, we will change all
394 * other NetClientStates also.
396 queues
= qemu_find_net_clients_except(nc
->name
, ncs
,
397 NET_CLIENT_OPTIONS_KIND_NIC
,
401 QTAILQ_FOREACH_SAFE(nf
, &nc
->filters
, next
, next
) {
402 object_unparent(OBJECT(nf
));
405 /* If there is a peer NIC, delete and cleanup client, but do not free. */
406 if (nc
->peer
&& nc
->peer
->info
->type
== NET_CLIENT_OPTIONS_KIND_NIC
) {
407 NICState
*nic
= qemu_get_nic(nc
->peer
);
408 if (nic
->peer_deleted
) {
411 nic
->peer_deleted
= true;
413 for (i
= 0; i
< queues
; i
++) {
414 ncs
[i
]->peer
->link_down
= true;
417 if (nc
->peer
->info
->link_status_changed
) {
418 nc
->peer
->info
->link_status_changed(nc
->peer
);
421 for (i
= 0; i
< queues
; i
++) {
422 qemu_cleanup_net_client(ncs
[i
]);
428 for (i
= 0; i
< queues
; i
++) {
429 qemu_cleanup_net_client(ncs
[i
]);
430 qemu_free_net_client(ncs
[i
]);
434 void qemu_del_nic(NICState
*nic
)
436 int i
, queues
= MAX(nic
->conf
->peers
.queues
, 1);
438 qemu_macaddr_set_free(&nic
->conf
->macaddr
);
440 /* If this is a peer NIC and peer has already been deleted, free it now. */
441 if (nic
->peer_deleted
) {
442 for (i
= 0; i
< queues
; i
++) {
443 qemu_free_net_client(qemu_get_subqueue(nic
, i
)->peer
);
447 for (i
= queues
- 1; i
>= 0; i
--) {
448 NetClientState
*nc
= qemu_get_subqueue(nic
, i
);
450 qemu_cleanup_net_client(nc
);
451 qemu_free_net_client(nc
);
457 void qemu_foreach_nic(qemu_nic_foreach func
, void *opaque
)
461 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
462 if (nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_NIC
) {
463 if (nc
->queue_index
== 0) {
464 func(qemu_get_nic(nc
), opaque
);
470 bool qemu_has_ufo(NetClientState
*nc
)
472 if (!nc
|| !nc
->info
->has_ufo
) {
476 return nc
->info
->has_ufo(nc
);
479 bool qemu_has_vnet_hdr(NetClientState
*nc
)
481 if (!nc
|| !nc
->info
->has_vnet_hdr
) {
485 return nc
->info
->has_vnet_hdr(nc
);
488 bool qemu_has_vnet_hdr_len(NetClientState
*nc
, int len
)
490 if (!nc
|| !nc
->info
->has_vnet_hdr_len
) {
494 return nc
->info
->has_vnet_hdr_len(nc
, len
);
497 void qemu_using_vnet_hdr(NetClientState
*nc
, bool enable
)
499 if (!nc
|| !nc
->info
->using_vnet_hdr
) {
503 nc
->info
->using_vnet_hdr(nc
, enable
);
506 void qemu_set_offload(NetClientState
*nc
, int csum
, int tso4
, int tso6
,
509 if (!nc
|| !nc
->info
->set_offload
) {
513 nc
->info
->set_offload(nc
, csum
, tso4
, tso6
, ecn
, ufo
);
516 void qemu_set_vnet_hdr_len(NetClientState
*nc
, int len
)
518 if (!nc
|| !nc
->info
->set_vnet_hdr_len
) {
522 nc
->info
->set_vnet_hdr_len(nc
, len
);
525 int qemu_set_vnet_le(NetClientState
*nc
, bool is_le
)
527 #ifdef HOST_WORDS_BIGENDIAN
528 if (!nc
|| !nc
->info
->set_vnet_le
) {
532 return nc
->info
->set_vnet_le(nc
, is_le
);
538 int qemu_set_vnet_be(NetClientState
*nc
, bool is_be
)
540 #ifdef HOST_WORDS_BIGENDIAN
543 if (!nc
|| !nc
->info
->set_vnet_be
) {
547 return nc
->info
->set_vnet_be(nc
, is_be
);
551 int qemu_can_send_packet(NetClientState
*sender
)
553 int vm_running
= runstate_is_running();
563 if (sender
->peer
->receive_disabled
) {
565 } else if (sender
->peer
->info
->can_receive
&&
566 !sender
->peer
->info
->can_receive(sender
->peer
)) {
572 static ssize_t
filter_receive_iov(NetClientState
*nc
,
573 NetFilterDirection direction
,
574 NetClientState
*sender
,
576 const struct iovec
*iov
,
578 NetPacketSent
*sent_cb
)
581 NetFilterState
*nf
= NULL
;
583 QTAILQ_FOREACH(nf
, &nc
->filters
, next
) {
584 ret
= qemu_netfilter_receive(nf
, direction
, sender
, flags
, iov
,
594 static ssize_t
filter_receive(NetClientState
*nc
,
595 NetFilterDirection direction
,
596 NetClientState
*sender
,
600 NetPacketSent
*sent_cb
)
603 .iov_base
= (void *)data
,
607 return filter_receive_iov(nc
, direction
, sender
, flags
, &iov
, 1, sent_cb
);
610 void qemu_purge_queued_packets(NetClientState
*nc
)
616 qemu_net_queue_purge(nc
->peer
->incoming_queue
, nc
);
620 void qemu_flush_or_purge_queued_packets(NetClientState
*nc
, bool purge
)
622 nc
->receive_disabled
= 0;
624 if (nc
->peer
&& nc
->peer
->info
->type
== NET_CLIENT_OPTIONS_KIND_HUBPORT
) {
625 if (net_hub_flush(nc
->peer
)) {
629 if (qemu_net_queue_flush(nc
->incoming_queue
)) {
630 /* We emptied the queue successfully, signal to the IO thread to repoll
631 * the file descriptor (for tap, for example).
635 /* Unable to empty the queue, purge remaining packets */
636 qemu_net_queue_purge(nc
->incoming_queue
, nc
);
640 void qemu_flush_queued_packets(NetClientState
*nc
)
642 qemu_flush_or_purge_queued_packets(nc
, false);
645 static ssize_t
qemu_send_packet_async_with_flags(NetClientState
*sender
,
647 const uint8_t *buf
, int size
,
648 NetPacketSent
*sent_cb
)
654 printf("qemu_send_packet_async:\n");
655 hex_dump(stdout
, buf
, size
);
658 if (sender
->link_down
|| !sender
->peer
) {
662 /* Let filters handle the packet first */
663 ret
= filter_receive(sender
, NET_FILTER_DIRECTION_TX
,
664 sender
, flags
, buf
, size
, sent_cb
);
669 ret
= filter_receive(sender
->peer
, NET_FILTER_DIRECTION_RX
,
670 sender
, flags
, buf
, size
, sent_cb
);
675 queue
= sender
->peer
->incoming_queue
;
677 return qemu_net_queue_send(queue
, sender
, flags
, buf
, size
, sent_cb
);
680 ssize_t
qemu_send_packet_async(NetClientState
*sender
,
681 const uint8_t *buf
, int size
,
682 NetPacketSent
*sent_cb
)
684 return qemu_send_packet_async_with_flags(sender
, QEMU_NET_PACKET_FLAG_NONE
,
688 void qemu_send_packet(NetClientState
*nc
, const uint8_t *buf
, int size
)
690 qemu_send_packet_async(nc
, buf
, size
, NULL
);
693 ssize_t
qemu_send_packet_raw(NetClientState
*nc
, const uint8_t *buf
, int size
)
695 return qemu_send_packet_async_with_flags(nc
, QEMU_NET_PACKET_FLAG_RAW
,
699 static ssize_t
nc_sendv_compat(NetClientState
*nc
, const struct iovec
*iov
,
700 int iovcnt
, unsigned flags
)
702 uint8_t buf
[NET_BUFSIZE
];
707 buffer
= iov
[0].iov_base
;
708 offset
= iov
[0].iov_len
;
711 offset
= iov_to_buf(iov
, iovcnt
, 0, buffer
, sizeof(buffer
));
714 if (flags
& QEMU_NET_PACKET_FLAG_RAW
&& nc
->info
->receive_raw
) {
715 return nc
->info
->receive_raw(nc
, buffer
, offset
);
717 return nc
->info
->receive(nc
, buffer
, offset
);
721 ssize_t
qemu_deliver_packet_iov(NetClientState
*sender
,
723 const struct iovec
*iov
,
727 NetClientState
*nc
= opaque
;
731 return iov_size(iov
, iovcnt
);
734 if (nc
->receive_disabled
) {
738 if (nc
->info
->receive_iov
) {
739 ret
= nc
->info
->receive_iov(nc
, iov
, iovcnt
);
741 ret
= nc_sendv_compat(nc
, iov
, iovcnt
, flags
);
745 nc
->receive_disabled
= 1;
751 ssize_t
qemu_sendv_packet_async(NetClientState
*sender
,
752 const struct iovec
*iov
, int iovcnt
,
753 NetPacketSent
*sent_cb
)
758 if (sender
->link_down
|| !sender
->peer
) {
759 return iov_size(iov
, iovcnt
);
762 /* Let filters handle the packet first */
763 ret
= filter_receive_iov(sender
, NET_FILTER_DIRECTION_TX
, sender
,
764 QEMU_NET_PACKET_FLAG_NONE
, iov
, iovcnt
, sent_cb
);
769 ret
= filter_receive_iov(sender
->peer
, NET_FILTER_DIRECTION_RX
, sender
,
770 QEMU_NET_PACKET_FLAG_NONE
, iov
, iovcnt
, sent_cb
);
775 queue
= sender
->peer
->incoming_queue
;
777 return qemu_net_queue_send_iov(queue
, sender
,
778 QEMU_NET_PACKET_FLAG_NONE
,
779 iov
, iovcnt
, sent_cb
);
783 qemu_sendv_packet(NetClientState
*nc
, const struct iovec
*iov
, int iovcnt
)
785 return qemu_sendv_packet_async(nc
, iov
, iovcnt
, NULL
);
788 NetClientState
*qemu_find_netdev(const char *id
)
792 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
793 if (nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_NIC
)
795 if (!strcmp(nc
->name
, id
)) {
803 int qemu_find_net_clients_except(const char *id
, NetClientState
**ncs
,
804 NetClientOptionsKind type
, int max
)
809 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
810 if (nc
->info
->type
== type
) {
813 if (!id
|| !strcmp(nc
->name
, id
)) {
824 static int nic_get_free_idx(void)
828 for (index
= 0; index
< MAX_NICS
; index
++)
829 if (!nd_table
[index
].used
)
834 int qemu_show_nic_models(const char *arg
, const char *const *models
)
838 if (!arg
|| !is_help_option(arg
)) {
842 fprintf(stderr
, "qemu: Supported NIC models: ");
843 for (i
= 0 ; models
[i
]; i
++)
844 fprintf(stderr
, "%s%c", models
[i
], models
[i
+1] ? ',' : '\n');
848 void qemu_check_nic_model(NICInfo
*nd
, const char *model
)
850 const char *models
[2];
855 if (qemu_show_nic_models(nd
->model
, models
))
857 if (qemu_find_nic_model(nd
, models
, model
) < 0)
861 int qemu_find_nic_model(NICInfo
*nd
, const char * const *models
,
862 const char *default_model
)
867 nd
->model
= g_strdup(default_model
);
869 for (i
= 0 ; models
[i
]; i
++) {
870 if (strcmp(nd
->model
, models
[i
]) == 0)
874 error_report("Unsupported NIC model: %s", nd
->model
);
878 static int net_init_nic(const NetClientOptions
*opts
, const char *name
,
879 NetClientState
*peer
, Error
**errp
)
883 const NetLegacyNicOptions
*nic
;
885 assert(opts
->kind
== NET_CLIENT_OPTIONS_KIND_NIC
);
888 idx
= nic_get_free_idx();
889 if (idx
== -1 || nb_nics
>= MAX_NICS
) {
890 error_setg(errp
, "too many NICs");
896 memset(nd
, 0, sizeof(*nd
));
898 if (nic
->has_netdev
) {
899 nd
->netdev
= qemu_find_netdev(nic
->netdev
);
901 error_setg(errp
, "netdev '%s' not found", nic
->netdev
);
908 nd
->name
= g_strdup(name
);
909 if (nic
->has_model
) {
910 nd
->model
= g_strdup(nic
->model
);
913 nd
->devaddr
= g_strdup(nic
->addr
);
916 if (nic
->has_macaddr
&&
917 net_parse_macaddr(nd
->macaddr
.a
, nic
->macaddr
) < 0) {
918 error_setg(errp
, "invalid syntax for ethernet address");
921 if (nic
->has_macaddr
&&
922 is_multicast_ether_addr(nd
->macaddr
.a
)) {
924 "NIC cannot have multicast MAC address (odd 1st byte)");
927 qemu_macaddr_default_if_unset(&nd
->macaddr
);
929 if (nic
->has_vectors
) {
930 if (nic
->vectors
> 0x7ffffff) {
931 error_setg(errp
, "invalid # of vectors: %"PRIu32
, nic
->vectors
);
934 nd
->nvectors
= nic
->vectors
;
936 nd
->nvectors
= DEV_NVECTORS_UNSPECIFIED
;
946 static int (* const net_client_init_fun
[NET_CLIENT_OPTIONS_KIND_MAX
])(
947 const NetClientOptions
*opts
,
949 NetClientState
*peer
, Error
**errp
) = {
950 [NET_CLIENT_OPTIONS_KIND_NIC
] = net_init_nic
,
952 [NET_CLIENT_OPTIONS_KIND_USER
] = net_init_slirp
,
954 [NET_CLIENT_OPTIONS_KIND_TAP
] = net_init_tap
,
955 [NET_CLIENT_OPTIONS_KIND_SOCKET
] = net_init_socket
,
957 [NET_CLIENT_OPTIONS_KIND_VDE
] = net_init_vde
,
960 [NET_CLIENT_OPTIONS_KIND_NETMAP
] = net_init_netmap
,
962 [NET_CLIENT_OPTIONS_KIND_DUMP
] = net_init_dump
,
963 #ifdef CONFIG_NET_BRIDGE
964 [NET_CLIENT_OPTIONS_KIND_BRIDGE
] = net_init_bridge
,
966 [NET_CLIENT_OPTIONS_KIND_HUBPORT
] = net_init_hubport
,
967 #ifdef CONFIG_VHOST_NET_USED
968 [NET_CLIENT_OPTIONS_KIND_VHOST_USER
] = net_init_vhost_user
,
971 [NET_CLIENT_OPTIONS_KIND_L2TPV3
] = net_init_l2tpv3
,
976 static int net_client_init1(const void *object
, int is_netdev
, Error
**errp
)
978 const NetClientOptions
*opts
;
980 NetClientState
*peer
= NULL
;
983 const Netdev
*netdev
= object
;
987 if (opts
->kind
== NET_CLIENT_OPTIONS_KIND_DUMP
||
988 opts
->kind
== NET_CLIENT_OPTIONS_KIND_NIC
||
989 !net_client_init_fun
[opts
->kind
]) {
990 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "type",
991 "a netdev backend type");
995 const NetLegacy
*net
= object
;
997 /* missing optional values have been initialized to "all bits zero" */
998 name
= net
->has_id
? net
->id
: net
->name
;
1000 if (opts
->kind
== NET_CLIENT_OPTIONS_KIND_NONE
) {
1001 return 0; /* nothing to do */
1003 if (opts
->kind
== NET_CLIENT_OPTIONS_KIND_HUBPORT
) {
1004 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "type",
1009 if (!net_client_init_fun
[opts
->kind
]) {
1010 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "type",
1011 "a net backend type (maybe it is not compiled "
1012 "into this binary)");
1016 /* Do not add to a vlan if it's a nic with a netdev= parameter. */
1017 if (opts
->kind
!= NET_CLIENT_OPTIONS_KIND_NIC
||
1018 !opts
->nic
->has_netdev
) {
1019 peer
= net_hub_add_port(net
->has_vlan
? net
->vlan
: 0, NULL
);
1023 if (net_client_init_fun
[opts
->kind
](opts
, name
, peer
, errp
) < 0) {
1024 /* FIXME drop when all init functions store an Error */
1025 if (errp
&& !*errp
) {
1026 error_setg(errp
, QERR_DEVICE_INIT_FAILED
,
1027 NetClientOptionsKind_lookup
[opts
->kind
]);
1035 static void net_visit(Visitor
*v
, int is_netdev
, void **object
, Error
**errp
)
1038 visit_type_Netdev(v
, (Netdev
**)object
, NULL
, errp
);
1040 visit_type_NetLegacy(v
, (NetLegacy
**)object
, NULL
, errp
);
1045 int net_client_init(QemuOpts
*opts
, int is_netdev
, Error
**errp
)
1047 void *object
= NULL
;
1052 OptsVisitor
*ov
= opts_visitor_new(opts
);
1054 net_visit(opts_get_visitor(ov
), is_netdev
, &object
, &err
);
1055 opts_visitor_cleanup(ov
);
1059 ret
= net_client_init1(object
, is_netdev
, &err
);
1063 QapiDeallocVisitor
*dv
= qapi_dealloc_visitor_new();
1065 net_visit(qapi_dealloc_get_visitor(dv
), is_netdev
, &object
, NULL
);
1066 qapi_dealloc_visitor_cleanup(dv
);
1069 error_propagate(errp
, err
);
1074 static int net_host_check_device(const char *device
)
1077 for (i
= 0; host_net_devices
[i
]; i
++) {
1078 if (!strncmp(host_net_devices
[i
], device
,
1079 strlen(host_net_devices
[i
]))) {
1087 void hmp_host_net_add(Monitor
*mon
, const QDict
*qdict
)
1089 const char *device
= qdict_get_str(qdict
, "device");
1090 const char *opts_str
= qdict_get_try_str(qdict
, "opts");
1091 Error
*local_err
= NULL
;
1094 if (!net_host_check_device(device
)) {
1095 monitor_printf(mon
, "invalid host network device %s\n", device
);
1099 opts
= qemu_opts_parse_noisily(qemu_find_opts("net"),
1100 opts_str
? opts_str
: "", false);
1105 qemu_opt_set(opts
, "type", device
, &error_abort
);
1107 net_client_init(opts
, 0, &local_err
);
1109 error_report_err(local_err
);
1110 monitor_printf(mon
, "adding host network device %s failed\n", device
);
1114 void hmp_host_net_remove(Monitor
*mon
, const QDict
*qdict
)
1117 int vlan_id
= qdict_get_int(qdict
, "vlan_id");
1118 const char *device
= qdict_get_str(qdict
, "device");
1120 nc
= net_hub_find_client_by_name(vlan_id
, device
);
1122 error_report("Host network device '%s' on hub '%d' not found",
1126 if (nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_NIC
) {
1127 error_report("invalid host network device '%s'", device
);
1131 qemu_del_net_client(nc
->peer
);
1132 qemu_del_net_client(nc
);
1135 void netdev_add(QemuOpts
*opts
, Error
**errp
)
1137 net_client_init(opts
, 1, errp
);
1140 void qmp_netdev_add(QDict
*qdict
, QObject
**ret
, Error
**errp
)
1142 Error
*local_err
= NULL
;
1143 QemuOptsList
*opts_list
;
1146 opts_list
= qemu_find_opts_err("netdev", &local_err
);
1151 opts
= qemu_opts_from_qdict(opts_list
, qdict
, &local_err
);
1156 netdev_add(opts
, &local_err
);
1158 qemu_opts_del(opts
);
1163 error_propagate(errp
, local_err
);
1166 void qmp_netdev_del(const char *id
, Error
**errp
)
1171 nc
= qemu_find_netdev(id
);
1173 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1174 "Device '%s' not found", id
);
1178 opts
= qemu_opts_find(qemu_find_opts_err("netdev", NULL
), id
);
1180 error_setg(errp
, "Device '%s' is not a netdev", id
);
1184 qemu_del_net_client(nc
);
1185 qemu_opts_del(opts
);
1188 void print_net_client(Monitor
*mon
, NetClientState
*nc
)
1192 monitor_printf(mon
, "%s: index=%d,type=%s,%s\n", nc
->name
,
1194 NetClientOptionsKind_lookup
[nc
->info
->type
],
1196 if (!QTAILQ_EMPTY(&nc
->filters
)) {
1197 monitor_printf(mon
, "filters:\n");
1199 QTAILQ_FOREACH(nf
, &nc
->filters
, next
) {
1200 monitor_printf(mon
, " - %s: type=%s%s\n",
1201 object_get_canonical_path_component(OBJECT(nf
)),
1202 object_get_typename(OBJECT(nf
)),
1207 RxFilterInfoList
*qmp_query_rx_filter(bool has_name
, const char *name
,
1211 RxFilterInfoList
*filter_list
= NULL
, *last_entry
= NULL
;
1213 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
1214 RxFilterInfoList
*entry
;
1217 if (has_name
&& strcmp(nc
->name
, name
) != 0) {
1221 /* only query rx-filter information of NIC */
1222 if (nc
->info
->type
!= NET_CLIENT_OPTIONS_KIND_NIC
) {
1224 error_setg(errp
, "net client(%s) isn't a NIC", name
);
1230 if (nc
->info
->query_rx_filter
) {
1231 info
= nc
->info
->query_rx_filter(nc
);
1232 entry
= g_malloc0(sizeof(*entry
));
1233 entry
->value
= info
;
1236 filter_list
= entry
;
1238 last_entry
->next
= entry
;
1241 } else if (has_name
) {
1242 error_setg(errp
, "net client(%s) doesn't support"
1243 " rx-filter querying", name
);
1252 if (filter_list
== NULL
&& has_name
) {
1253 error_setg(errp
, "invalid net client name: %s", name
);
1259 void hmp_info_network(Monitor
*mon
, const QDict
*qdict
)
1261 NetClientState
*nc
, *peer
;
1262 NetClientOptionsKind type
;
1266 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
1268 type
= nc
->info
->type
;
1270 /* Skip if already printed in hub info */
1271 if (net_hub_id_for_client(nc
, NULL
) == 0) {
1275 if (!peer
|| type
== NET_CLIENT_OPTIONS_KIND_NIC
) {
1276 print_net_client(mon
, nc
);
1277 } /* else it's a netdev connected to a NIC, printed with the NIC */
1278 if (peer
&& type
== NET_CLIENT_OPTIONS_KIND_NIC
) {
1279 monitor_printf(mon
, " \\ ");
1280 print_net_client(mon
, peer
);
1285 void qmp_set_link(const char *name
, bool up
, Error
**errp
)
1287 NetClientState
*ncs
[MAX_QUEUE_NUM
];
1291 queues
= qemu_find_net_clients_except(name
, ncs
,
1292 NET_CLIENT_OPTIONS_KIND_MAX
,
1296 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1297 "Device '%s' not found", name
);
1302 for (i
= 0; i
< queues
; i
++) {
1303 ncs
[i
]->link_down
= !up
;
1306 if (nc
->info
->link_status_changed
) {
1307 nc
->info
->link_status_changed(nc
);
1311 /* Change peer link only if the peer is NIC and then notify peer.
1312 * If the peer is a HUBPORT or a backend, we do not change the
1315 * This behavior is compatible with qemu vlans where there could be
1316 * multiple clients that can still communicate with each other in
1317 * disconnected mode. For now maintain this compatibility.
1319 if (nc
->peer
->info
->type
== NET_CLIENT_OPTIONS_KIND_NIC
) {
1320 for (i
= 0; i
< queues
; i
++) {
1321 ncs
[i
]->peer
->link_down
= !up
;
1324 if (nc
->peer
->info
->link_status_changed
) {
1325 nc
->peer
->info
->link_status_changed(nc
->peer
);
1330 static void net_vm_change_state_handler(void *opaque
, int running
,
1334 NetClientState
*tmp
;
1336 QTAILQ_FOREACH_SAFE(nc
, &net_clients
, next
, tmp
) {
1338 /* Flush queued packets and wake up backends. */
1339 if (nc
->peer
&& qemu_can_send_packet(nc
)) {
1340 qemu_flush_queued_packets(nc
->peer
);
1343 /* Complete all queued packets, to guarantee we don't modify
1344 * state later when VM is not running.
1346 qemu_flush_or_purge_queued_packets(nc
, true);
1351 void net_cleanup(void)
1355 /* We may del multiple entries during qemu_del_net_client(),
1356 * so QTAILQ_FOREACH_SAFE() is also not safe here.
1358 while (!QTAILQ_EMPTY(&net_clients
)) {
1359 nc
= QTAILQ_FIRST(&net_clients
);
1360 if (nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_NIC
) {
1361 qemu_del_nic(qemu_get_nic(nc
));
1363 qemu_del_net_client(nc
);
1367 qemu_del_vm_change_state_handler(net_change_state_entry
);
1370 void net_check_clients(void)
1375 /* Don't warn about the default network setup that you get if
1376 * no command line -net or -netdev options are specified. There
1377 * are two cases that we would otherwise complain about:
1378 * (1) board doesn't support a NIC but the implicit "-net nic"
1380 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
1381 * sets up a nic that isn't connected to anything.
1387 net_hub_check_clients();
1389 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
1391 fprintf(stderr
, "Warning: %s %s has no peer\n",
1392 nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_NIC
?
1393 "nic" : "netdev", nc
->name
);
1397 /* Check that all NICs requested via -net nic actually got created.
1398 * NICs created via -device don't need to be checked here because
1399 * they are always instantiated.
1401 for (i
= 0; i
< MAX_NICS
; i
++) {
1402 NICInfo
*nd
= &nd_table
[i
];
1403 if (nd
->used
&& !nd
->instantiated
) {
1404 fprintf(stderr
, "Warning: requested NIC (%s, model %s) "
1405 "was not created (not supported by this machine?)\n",
1406 nd
->name
? nd
->name
: "anonymous",
1407 nd
->model
? nd
->model
: "unspecified");
1412 static int net_init_client(void *dummy
, QemuOpts
*opts
, Error
**errp
)
1414 Error
*local_err
= NULL
;
1416 net_client_init(opts
, 0, &local_err
);
1418 error_report_err(local_err
);
1425 static int net_init_netdev(void *dummy
, QemuOpts
*opts
, Error
**errp
)
1427 Error
*local_err
= NULL
;
1430 ret
= net_client_init(opts
, 1, &local_err
);
1432 error_report_err(local_err
);
1439 int net_init_clients(void)
1441 QemuOptsList
*net
= qemu_find_opts("net");
1444 /* if no clients, we use a default config */
1445 qemu_opts_set(net
, NULL
, "type", "nic", &error_abort
);
1447 qemu_opts_set(net
, NULL
, "type", "user", &error_abort
);
1451 net_change_state_entry
=
1452 qemu_add_vm_change_state_handler(net_vm_change_state_handler
, NULL
);
1454 QTAILQ_INIT(&net_clients
);
1456 if (qemu_opts_foreach(qemu_find_opts("netdev"),
1457 net_init_netdev
, NULL
, NULL
)) {
1461 if (qemu_opts_foreach(net
, net_init_client
, NULL
, NULL
)) {
1468 int net_client_parse(QemuOptsList
*opts_list
, const char *optarg
)
1470 #if defined(CONFIG_SLIRP)
1472 if (net_slirp_parse_legacy(opts_list
, optarg
, &ret
)) {
1477 if (!qemu_opts_parse_noisily(opts_list
, optarg
, true)) {
1487 unsigned compute_mcast_idx(const uint8_t *ep
)
1494 for (i
= 0; i
< 6; i
++) {
1496 for (j
= 0; j
< 8; j
++) {
1497 carry
= ((crc
& 0x80000000L
) ? 1 : 0) ^ (b
& 0x01);
1501 crc
= ((crc
^ POLYNOMIAL
) | carry
);
1508 QemuOptsList qemu_netdev_opts
= {
1510 .implied_opt_name
= "type",
1511 .head
= QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts
.head
),
1514 * no elements => accept any params
1515 * validation will happen later
1517 { /* end of list */ }
1521 QemuOptsList qemu_net_opts
= {
1523 .implied_opt_name
= "type",
1524 .head
= QTAILQ_HEAD_INITIALIZER(qemu_net_opts
.head
),
1527 * no elements => accept any params
1528 * validation will happen later
1530 { /* end of list */ }