4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 #include "qemu/osdep.h"
29 #include "net/slirp.h"
33 #include "monitor/monitor.h"
34 #include "qemu-common.h"
35 #include "qemu/help_option.h"
36 #include "qapi/qmp/qerror.h"
37 #include "qemu/error-report.h"
38 #include "qemu/sockets.h"
39 #include "qemu/cutils.h"
40 #include "qemu/config-file.h"
41 #include "qmp-commands.h"
44 #include "qemu/main-loop.h"
45 #include "qapi-visit.h"
46 #include "qapi/opts-visitor.h"
47 #include "sysemu/sysemu.h"
48 #include "net/filter.h"
49 #include "qapi/string-output-visitor.h"
51 /* Net bridge is currently not supported for W32. */
53 # define CONFIG_NET_BRIDGE
56 static VMChangeStateEntry
*net_change_state_entry
;
57 static QTAILQ_HEAD(, NetClientState
) net_clients
;
59 const char *host_net_devices
[] = {
63 #ifdef CONFIG_NET_BRIDGE
81 /***********************************************************/
82 /* network device redirectors */
84 #if defined(DEBUG_NET)
85 static void hex_dump(FILE *f
, const uint8_t *buf
, int size
)
89 for(i
=0;i
<size
;i
+=16) {
93 fprintf(f
, "%08x ", i
);
96 fprintf(f
, " %02x", buf
[i
+j
]);
103 if (c
< ' ' || c
> '~')
112 static int get_str_sep(char *buf
, int buf_size
, const char **pp
, int sep
)
123 if (len
> buf_size
- 1)
132 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
)
140 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
142 saddr
->sin_family
= AF_INET
;
143 if (buf
[0] == '\0') {
144 saddr
->sin_addr
.s_addr
= 0;
146 if (qemu_isdigit(buf
[0])) {
147 if (!inet_aton(buf
, &saddr
->sin_addr
))
150 if ((he
= gethostbyname(buf
)) == NULL
)
152 saddr
->sin_addr
= *(struct in_addr
*)he
->h_addr
;
155 port
= strtol(p
, (char **)&r
, 0);
158 saddr
->sin_port
= htons(port
);
162 char *qemu_mac_strdup_printf(const uint8_t *macaddr
)
164 return g_strdup_printf("%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",
165 macaddr
[0], macaddr
[1], macaddr
[2],
166 macaddr
[3], macaddr
[4], macaddr
[5]);
169 void qemu_format_nic_info_str(NetClientState
*nc
, uint8_t macaddr
[6])
171 snprintf(nc
->info_str
, sizeof(nc
->info_str
),
172 "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
174 macaddr
[0], macaddr
[1], macaddr
[2],
175 macaddr
[3], macaddr
[4], macaddr
[5]);
178 static int mac_table
[256] = {0};
180 static void qemu_macaddr_set_used(MACAddr
*macaddr
)
184 for (index
= 0x56; index
< 0xFF; index
++) {
185 if (macaddr
->a
[5] == index
) {
191 static void qemu_macaddr_set_free(MACAddr
*macaddr
)
194 static const MACAddr base
= { .a
= { 0x52, 0x54, 0x00, 0x12, 0x34, 0 } };
196 if (memcmp(macaddr
->a
, &base
.a
, (sizeof(base
.a
) - 1)) != 0) {
199 for (index
= 0x56; index
< 0xFF; index
++) {
200 if (macaddr
->a
[5] == index
) {
206 static int qemu_macaddr_get_free(void)
210 for (index
= 0x56; index
< 0xFF; index
++) {
211 if (mac_table
[index
] == 0) {
219 void qemu_macaddr_default_if_unset(MACAddr
*macaddr
)
221 static const MACAddr zero
= { .a
= { 0,0,0,0,0,0 } };
222 static const MACAddr base
= { .a
= { 0x52, 0x54, 0x00, 0x12, 0x34, 0 } };
224 if (memcmp(macaddr
, &zero
, sizeof(zero
)) != 0) {
225 if (memcmp(macaddr
->a
, &base
.a
, (sizeof(base
.a
) - 1)) != 0) {
228 qemu_macaddr_set_used(macaddr
);
233 macaddr
->a
[0] = 0x52;
234 macaddr
->a
[1] = 0x54;
235 macaddr
->a
[2] = 0x00;
236 macaddr
->a
[3] = 0x12;
237 macaddr
->a
[4] = 0x34;
238 macaddr
->a
[5] = qemu_macaddr_get_free();
239 qemu_macaddr_set_used(macaddr
);
243 * Generate a name for net client
245 * Only net clients created with the legacy -net option and NICs need this.
247 static char *assign_name(NetClientState
*nc1
, const char *model
)
252 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
256 if (strcmp(nc
->model
, model
) == 0) {
261 return g_strdup_printf("%s.%d", model
, id
);
264 static void qemu_net_client_destructor(NetClientState
*nc
)
269 static void qemu_net_client_setup(NetClientState
*nc
,
271 NetClientState
*peer
,
274 NetClientDestructor
*destructor
)
277 nc
->model
= g_strdup(model
);
279 nc
->name
= g_strdup(name
);
281 nc
->name
= assign_name(nc
, model
);
289 QTAILQ_INSERT_TAIL(&net_clients
, nc
, next
);
291 nc
->incoming_queue
= qemu_new_net_queue(qemu_deliver_packet_iov
, nc
);
292 nc
->destructor
= destructor
;
293 QTAILQ_INIT(&nc
->filters
);
296 NetClientState
*qemu_new_net_client(NetClientInfo
*info
,
297 NetClientState
*peer
,
303 assert(info
->size
>= sizeof(NetClientState
));
305 nc
= g_malloc0(info
->size
);
306 qemu_net_client_setup(nc
, info
, peer
, model
, name
,
307 qemu_net_client_destructor
);
312 NICState
*qemu_new_nic(NetClientInfo
*info
,
318 NetClientState
**peers
= conf
->peers
.ncs
;
320 int i
, queues
= MAX(1, conf
->peers
.queues
);
322 assert(info
->type
== NET_CLIENT_OPTIONS_KIND_NIC
);
323 assert(info
->size
>= sizeof(NICState
));
325 nic
= g_malloc0(info
->size
+ sizeof(NetClientState
) * queues
);
326 nic
->ncs
= (void *)nic
+ info
->size
;
328 nic
->opaque
= opaque
;
330 for (i
= 0; i
< queues
; i
++) {
331 qemu_net_client_setup(&nic
->ncs
[i
], info
, peers
[i
], model
, name
,
333 nic
->ncs
[i
].queue_index
= i
;
339 NetClientState
*qemu_get_subqueue(NICState
*nic
, int queue_index
)
341 return nic
->ncs
+ queue_index
;
344 NetClientState
*qemu_get_queue(NICState
*nic
)
346 return qemu_get_subqueue(nic
, 0);
349 NICState
*qemu_get_nic(NetClientState
*nc
)
351 NetClientState
*nc0
= nc
- nc
->queue_index
;
353 return (NICState
*)((void *)nc0
- nc
->info
->size
);
356 void *qemu_get_nic_opaque(NetClientState
*nc
)
358 NICState
*nic
= qemu_get_nic(nc
);
363 static void qemu_cleanup_net_client(NetClientState
*nc
)
365 QTAILQ_REMOVE(&net_clients
, nc
, next
);
367 if (nc
->info
->cleanup
) {
368 nc
->info
->cleanup(nc
);
372 static void qemu_free_net_client(NetClientState
*nc
)
374 if (nc
->incoming_queue
) {
375 qemu_del_net_queue(nc
->incoming_queue
);
378 nc
->peer
->peer
= NULL
;
382 if (nc
->destructor
) {
387 void qemu_del_net_client(NetClientState
*nc
)
389 NetClientState
*ncs
[MAX_QUEUE_NUM
];
391 NetFilterState
*nf
, *next
;
393 assert(nc
->info
->type
!= NET_CLIENT_OPTIONS_KIND_NIC
);
395 /* If the NetClientState belongs to a multiqueue backend, we will change all
396 * other NetClientStates also.
398 queues
= qemu_find_net_clients_except(nc
->name
, ncs
,
399 NET_CLIENT_OPTIONS_KIND_NIC
,
403 QTAILQ_FOREACH_SAFE(nf
, &nc
->filters
, next
, next
) {
404 object_unparent(OBJECT(nf
));
407 /* If there is a peer NIC, delete and cleanup client, but do not free. */
408 if (nc
->peer
&& nc
->peer
->info
->type
== NET_CLIENT_OPTIONS_KIND_NIC
) {
409 NICState
*nic
= qemu_get_nic(nc
->peer
);
410 if (nic
->peer_deleted
) {
413 nic
->peer_deleted
= true;
415 for (i
= 0; i
< queues
; i
++) {
416 ncs
[i
]->peer
->link_down
= true;
419 if (nc
->peer
->info
->link_status_changed
) {
420 nc
->peer
->info
->link_status_changed(nc
->peer
);
423 for (i
= 0; i
< queues
; i
++) {
424 qemu_cleanup_net_client(ncs
[i
]);
430 for (i
= 0; i
< queues
; i
++) {
431 qemu_cleanup_net_client(ncs
[i
]);
432 qemu_free_net_client(ncs
[i
]);
436 void qemu_del_nic(NICState
*nic
)
438 int i
, queues
= MAX(nic
->conf
->peers
.queues
, 1);
440 qemu_macaddr_set_free(&nic
->conf
->macaddr
);
442 /* If this is a peer NIC and peer has already been deleted, free it now. */
443 if (nic
->peer_deleted
) {
444 for (i
= 0; i
< queues
; i
++) {
445 qemu_free_net_client(qemu_get_subqueue(nic
, i
)->peer
);
449 for (i
= queues
- 1; i
>= 0; i
--) {
450 NetClientState
*nc
= qemu_get_subqueue(nic
, i
);
452 qemu_cleanup_net_client(nc
);
453 qemu_free_net_client(nc
);
459 void qemu_foreach_nic(qemu_nic_foreach func
, void *opaque
)
463 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
464 if (nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_NIC
) {
465 if (nc
->queue_index
== 0) {
466 func(qemu_get_nic(nc
), opaque
);
472 bool qemu_has_ufo(NetClientState
*nc
)
474 if (!nc
|| !nc
->info
->has_ufo
) {
478 return nc
->info
->has_ufo(nc
);
481 bool qemu_has_vnet_hdr(NetClientState
*nc
)
483 if (!nc
|| !nc
->info
->has_vnet_hdr
) {
487 return nc
->info
->has_vnet_hdr(nc
);
490 bool qemu_has_vnet_hdr_len(NetClientState
*nc
, int len
)
492 if (!nc
|| !nc
->info
->has_vnet_hdr_len
) {
496 return nc
->info
->has_vnet_hdr_len(nc
, len
);
499 void qemu_using_vnet_hdr(NetClientState
*nc
, bool enable
)
501 if (!nc
|| !nc
->info
->using_vnet_hdr
) {
505 nc
->info
->using_vnet_hdr(nc
, enable
);
508 void qemu_set_offload(NetClientState
*nc
, int csum
, int tso4
, int tso6
,
511 if (!nc
|| !nc
->info
->set_offload
) {
515 nc
->info
->set_offload(nc
, csum
, tso4
, tso6
, ecn
, ufo
);
518 void qemu_set_vnet_hdr_len(NetClientState
*nc
, int len
)
520 if (!nc
|| !nc
->info
->set_vnet_hdr_len
) {
524 nc
->info
->set_vnet_hdr_len(nc
, len
);
527 int qemu_set_vnet_le(NetClientState
*nc
, bool is_le
)
529 #ifdef HOST_WORDS_BIGENDIAN
530 if (!nc
|| !nc
->info
->set_vnet_le
) {
534 return nc
->info
->set_vnet_le(nc
, is_le
);
540 int qemu_set_vnet_be(NetClientState
*nc
, bool is_be
)
542 #ifdef HOST_WORDS_BIGENDIAN
545 if (!nc
|| !nc
->info
->set_vnet_be
) {
549 return nc
->info
->set_vnet_be(nc
, is_be
);
553 int qemu_can_send_packet(NetClientState
*sender
)
555 int vm_running
= runstate_is_running();
565 if (sender
->peer
->receive_disabled
) {
567 } else if (sender
->peer
->info
->can_receive
&&
568 !sender
->peer
->info
->can_receive(sender
->peer
)) {
574 static ssize_t
filter_receive_iov(NetClientState
*nc
,
575 NetFilterDirection direction
,
576 NetClientState
*sender
,
578 const struct iovec
*iov
,
580 NetPacketSent
*sent_cb
)
583 NetFilterState
*nf
= NULL
;
585 if (direction
== NET_FILTER_DIRECTION_TX
) {
586 QTAILQ_FOREACH(nf
, &nc
->filters
, next
) {
587 ret
= qemu_netfilter_receive(nf
, direction
, sender
, flags
, iov
,
594 QTAILQ_FOREACH_REVERSE(nf
, &nc
->filters
, NetFilterHead
, next
) {
595 ret
= qemu_netfilter_receive(nf
, direction
, sender
, flags
, iov
,
606 static ssize_t
filter_receive(NetClientState
*nc
,
607 NetFilterDirection direction
,
608 NetClientState
*sender
,
612 NetPacketSent
*sent_cb
)
615 .iov_base
= (void *)data
,
619 return filter_receive_iov(nc
, direction
, sender
, flags
, &iov
, 1, sent_cb
);
622 void qemu_purge_queued_packets(NetClientState
*nc
)
628 qemu_net_queue_purge(nc
->peer
->incoming_queue
, nc
);
632 void qemu_flush_or_purge_queued_packets(NetClientState
*nc
, bool purge
)
634 nc
->receive_disabled
= 0;
636 if (nc
->peer
&& nc
->peer
->info
->type
== NET_CLIENT_OPTIONS_KIND_HUBPORT
) {
637 if (net_hub_flush(nc
->peer
)) {
641 if (qemu_net_queue_flush(nc
->incoming_queue
)) {
642 /* We emptied the queue successfully, signal to the IO thread to repoll
643 * the file descriptor (for tap, for example).
647 /* Unable to empty the queue, purge remaining packets */
648 qemu_net_queue_purge(nc
->incoming_queue
, nc
);
652 void qemu_flush_queued_packets(NetClientState
*nc
)
654 qemu_flush_or_purge_queued_packets(nc
, false);
657 static ssize_t
qemu_send_packet_async_with_flags(NetClientState
*sender
,
659 const uint8_t *buf
, int size
,
660 NetPacketSent
*sent_cb
)
666 printf("qemu_send_packet_async:\n");
667 hex_dump(stdout
, buf
, size
);
670 if (sender
->link_down
|| !sender
->peer
) {
674 /* Let filters handle the packet first */
675 ret
= filter_receive(sender
, NET_FILTER_DIRECTION_TX
,
676 sender
, flags
, buf
, size
, sent_cb
);
681 ret
= filter_receive(sender
->peer
, NET_FILTER_DIRECTION_RX
,
682 sender
, flags
, buf
, size
, sent_cb
);
687 queue
= sender
->peer
->incoming_queue
;
689 return qemu_net_queue_send(queue
, sender
, flags
, buf
, size
, sent_cb
);
692 ssize_t
qemu_send_packet_async(NetClientState
*sender
,
693 const uint8_t *buf
, int size
,
694 NetPacketSent
*sent_cb
)
696 return qemu_send_packet_async_with_flags(sender
, QEMU_NET_PACKET_FLAG_NONE
,
700 void qemu_send_packet(NetClientState
*nc
, const uint8_t *buf
, int size
)
702 qemu_send_packet_async(nc
, buf
, size
, NULL
);
705 ssize_t
qemu_send_packet_raw(NetClientState
*nc
, const uint8_t *buf
, int size
)
707 return qemu_send_packet_async_with_flags(nc
, QEMU_NET_PACKET_FLAG_RAW
,
711 static ssize_t
nc_sendv_compat(NetClientState
*nc
, const struct iovec
*iov
,
712 int iovcnt
, unsigned flags
)
714 uint8_t buf
[NET_BUFSIZE
];
719 buffer
= iov
[0].iov_base
;
720 offset
= iov
[0].iov_len
;
723 offset
= iov_to_buf(iov
, iovcnt
, 0, buf
, sizeof(buf
));
726 if (flags
& QEMU_NET_PACKET_FLAG_RAW
&& nc
->info
->receive_raw
) {
727 return nc
->info
->receive_raw(nc
, buffer
, offset
);
729 return nc
->info
->receive(nc
, buffer
, offset
);
733 ssize_t
qemu_deliver_packet_iov(NetClientState
*sender
,
735 const struct iovec
*iov
,
739 NetClientState
*nc
= opaque
;
743 return iov_size(iov
, iovcnt
);
746 if (nc
->receive_disabled
) {
750 if (nc
->info
->receive_iov
) {
751 ret
= nc
->info
->receive_iov(nc
, iov
, iovcnt
);
753 ret
= nc_sendv_compat(nc
, iov
, iovcnt
, flags
);
757 nc
->receive_disabled
= 1;
763 ssize_t
qemu_sendv_packet_async(NetClientState
*sender
,
764 const struct iovec
*iov
, int iovcnt
,
765 NetPacketSent
*sent_cb
)
770 if (sender
->link_down
|| !sender
->peer
) {
771 return iov_size(iov
, iovcnt
);
774 /* Let filters handle the packet first */
775 ret
= filter_receive_iov(sender
, NET_FILTER_DIRECTION_TX
, sender
,
776 QEMU_NET_PACKET_FLAG_NONE
, iov
, iovcnt
, sent_cb
);
781 ret
= filter_receive_iov(sender
->peer
, NET_FILTER_DIRECTION_RX
, sender
,
782 QEMU_NET_PACKET_FLAG_NONE
, iov
, iovcnt
, sent_cb
);
787 queue
= sender
->peer
->incoming_queue
;
789 return qemu_net_queue_send_iov(queue
, sender
,
790 QEMU_NET_PACKET_FLAG_NONE
,
791 iov
, iovcnt
, sent_cb
);
795 qemu_sendv_packet(NetClientState
*nc
, const struct iovec
*iov
, int iovcnt
)
797 return qemu_sendv_packet_async(nc
, iov
, iovcnt
, NULL
);
800 NetClientState
*qemu_find_netdev(const char *id
)
804 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
805 if (nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_NIC
)
807 if (!strcmp(nc
->name
, id
)) {
815 int qemu_find_net_clients_except(const char *id
, NetClientState
**ncs
,
816 NetClientOptionsKind type
, int max
)
821 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
822 if (nc
->info
->type
== type
) {
825 if (!id
|| !strcmp(nc
->name
, id
)) {
836 static int nic_get_free_idx(void)
840 for (index
= 0; index
< MAX_NICS
; index
++)
841 if (!nd_table
[index
].used
)
846 int qemu_show_nic_models(const char *arg
, const char *const *models
)
850 if (!arg
|| !is_help_option(arg
)) {
854 fprintf(stderr
, "qemu: Supported NIC models: ");
855 for (i
= 0 ; models
[i
]; i
++)
856 fprintf(stderr
, "%s%c", models
[i
], models
[i
+1] ? ',' : '\n');
860 void qemu_check_nic_model(NICInfo
*nd
, const char *model
)
862 const char *models
[2];
867 if (qemu_show_nic_models(nd
->model
, models
))
869 if (qemu_find_nic_model(nd
, models
, model
) < 0)
873 int qemu_find_nic_model(NICInfo
*nd
, const char * const *models
,
874 const char *default_model
)
879 nd
->model
= g_strdup(default_model
);
881 for (i
= 0 ; models
[i
]; i
++) {
882 if (strcmp(nd
->model
, models
[i
]) == 0)
886 error_report("Unsupported NIC model: %s", nd
->model
);
890 static int net_init_nic(const NetClientOptions
*opts
, const char *name
,
891 NetClientState
*peer
, Error
**errp
)
895 const NetLegacyNicOptions
*nic
;
897 assert(opts
->type
== NET_CLIENT_OPTIONS_KIND_NIC
);
898 nic
= opts
->u
.nic
.data
;
900 idx
= nic_get_free_idx();
901 if (idx
== -1 || nb_nics
>= MAX_NICS
) {
902 error_setg(errp
, "too many NICs");
908 memset(nd
, 0, sizeof(*nd
));
910 if (nic
->has_netdev
) {
911 nd
->netdev
= qemu_find_netdev(nic
->netdev
);
913 error_setg(errp
, "netdev '%s' not found", nic
->netdev
);
920 nd
->name
= g_strdup(name
);
921 if (nic
->has_model
) {
922 nd
->model
= g_strdup(nic
->model
);
925 nd
->devaddr
= g_strdup(nic
->addr
);
928 if (nic
->has_macaddr
&&
929 net_parse_macaddr(nd
->macaddr
.a
, nic
->macaddr
) < 0) {
930 error_setg(errp
, "invalid syntax for ethernet address");
933 if (nic
->has_macaddr
&&
934 is_multicast_ether_addr(nd
->macaddr
.a
)) {
936 "NIC cannot have multicast MAC address (odd 1st byte)");
939 qemu_macaddr_default_if_unset(&nd
->macaddr
);
941 if (nic
->has_vectors
) {
942 if (nic
->vectors
> 0x7ffffff) {
943 error_setg(errp
, "invalid # of vectors: %"PRIu32
, nic
->vectors
);
946 nd
->nvectors
= nic
->vectors
;
948 nd
->nvectors
= DEV_NVECTORS_UNSPECIFIED
;
958 static int (* const net_client_init_fun
[NET_CLIENT_OPTIONS_KIND__MAX
])(
959 const NetClientOptions
*opts
,
961 NetClientState
*peer
, Error
**errp
) = {
962 [NET_CLIENT_OPTIONS_KIND_NIC
] = net_init_nic
,
964 [NET_CLIENT_OPTIONS_KIND_USER
] = net_init_slirp
,
966 [NET_CLIENT_OPTIONS_KIND_TAP
] = net_init_tap
,
967 [NET_CLIENT_OPTIONS_KIND_SOCKET
] = net_init_socket
,
969 [NET_CLIENT_OPTIONS_KIND_VDE
] = net_init_vde
,
972 [NET_CLIENT_OPTIONS_KIND_NETMAP
] = net_init_netmap
,
974 [NET_CLIENT_OPTIONS_KIND_DUMP
] = net_init_dump
,
975 #ifdef CONFIG_NET_BRIDGE
976 [NET_CLIENT_OPTIONS_KIND_BRIDGE
] = net_init_bridge
,
978 [NET_CLIENT_OPTIONS_KIND_HUBPORT
] = net_init_hubport
,
979 #ifdef CONFIG_VHOST_NET_USED
980 [NET_CLIENT_OPTIONS_KIND_VHOST_USER
] = net_init_vhost_user
,
983 [NET_CLIENT_OPTIONS_KIND_L2TPV3
] = net_init_l2tpv3
,
988 static int net_client_init1(const void *object
, int is_netdev
, Error
**errp
)
990 const NetClientOptions
*opts
;
992 NetClientState
*peer
= NULL
;
995 const Netdev
*netdev
= object
;
999 if (opts
->type
== NET_CLIENT_OPTIONS_KIND_DUMP
||
1000 opts
->type
== NET_CLIENT_OPTIONS_KIND_NIC
||
1001 !net_client_init_fun
[opts
->type
]) {
1002 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "type",
1003 "a netdev backend type");
1007 const NetLegacy
*net
= object
;
1009 /* missing optional values have been initialized to "all bits zero" */
1010 name
= net
->has_id
? net
->id
: net
->name
;
1012 if (opts
->type
== NET_CLIENT_OPTIONS_KIND_NONE
) {
1013 return 0; /* nothing to do */
1015 if (opts
->type
== NET_CLIENT_OPTIONS_KIND_HUBPORT
) {
1016 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "type",
1021 if (!net_client_init_fun
[opts
->type
]) {
1022 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "type",
1023 "a net backend type (maybe it is not compiled "
1024 "into this binary)");
1028 /* Do not add to a vlan if it's a nic with a netdev= parameter. */
1029 if (opts
->type
!= NET_CLIENT_OPTIONS_KIND_NIC
||
1030 !opts
->u
.nic
.data
->has_netdev
) {
1031 peer
= net_hub_add_port(net
->has_vlan
? net
->vlan
: 0, NULL
);
1035 if (net_client_init_fun
[opts
->type
](opts
, name
, peer
, errp
) < 0) {
1036 /* FIXME drop when all init functions store an Error */
1037 if (errp
&& !*errp
) {
1038 error_setg(errp
, QERR_DEVICE_INIT_FAILED
,
1039 NetClientOptionsKind_lookup
[opts
->type
]);
1047 int net_client_init(QemuOpts
*opts
, int is_netdev
, Error
**errp
)
1049 void *object
= NULL
;
1052 OptsVisitor
*ov
= opts_visitor_new(opts
);
1053 Visitor
*v
= opts_get_visitor(ov
);
1056 /* Parse convenience option format ip6-net=fec0::0[/64] */
1057 const char *ip6_net
= qemu_opt_get(opts
, "ipv6-net");
1060 char buf
[strlen(ip6_net
) + 1];
1062 if (get_str_sep(buf
, sizeof(buf
), &ip6_net
, '/') < 0) {
1063 /* Default 64bit prefix length. */
1064 qemu_opt_set(opts
, "ipv6-prefix", ip6_net
, &error_abort
);
1065 qemu_opt_set_number(opts
, "ipv6-prefixlen", 64, &error_abort
);
1067 /* User-specified prefix length. */
1071 qemu_opt_set(opts
, "ipv6-prefix", buf
, &error_abort
);
1072 err
= qemu_strtoul(ip6_net
, NULL
, 10, &len
);
1075 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
1076 "ipv6-prefix", "a number");
1078 qemu_opt_set_number(opts
, "ipv6-prefixlen", len
,
1082 qemu_opt_unset(opts
, "ipv6-net");
1087 visit_type_Netdev(v
, NULL
, (Netdev
**)&object
, &err
);
1089 visit_type_NetLegacy(v
, NULL
, (NetLegacy
**)&object
, &err
);
1093 ret
= net_client_init1(object
, is_netdev
, &err
);
1097 qapi_free_Netdev(object
);
1099 qapi_free_NetLegacy(object
);
1102 error_propagate(errp
, err
);
1107 static int net_host_check_device(const char *device
)
1110 for (i
= 0; host_net_devices
[i
]; i
++) {
1111 if (!strncmp(host_net_devices
[i
], device
,
1112 strlen(host_net_devices
[i
]))) {
1120 void hmp_host_net_add(Monitor
*mon
, const QDict
*qdict
)
1122 const char *device
= qdict_get_str(qdict
, "device");
1123 const char *opts_str
= qdict_get_try_str(qdict
, "opts");
1124 Error
*local_err
= NULL
;
1127 if (!net_host_check_device(device
)) {
1128 monitor_printf(mon
, "invalid host network device %s\n", device
);
1132 opts
= qemu_opts_parse_noisily(qemu_find_opts("net"),
1133 opts_str
? opts_str
: "", false);
1138 qemu_opt_set(opts
, "type", device
, &error_abort
);
1140 net_client_init(opts
, 0, &local_err
);
1142 error_report_err(local_err
);
1143 monitor_printf(mon
, "adding host network device %s failed\n", device
);
1147 void hmp_host_net_remove(Monitor
*mon
, const QDict
*qdict
)
1150 int vlan_id
= qdict_get_int(qdict
, "vlan_id");
1151 const char *device
= qdict_get_str(qdict
, "device");
1153 nc
= net_hub_find_client_by_name(vlan_id
, device
);
1155 error_report("Host network device '%s' on hub '%d' not found",
1159 if (nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_NIC
) {
1160 error_report("invalid host network device '%s'", device
);
1164 qemu_del_net_client(nc
->peer
);
1165 qemu_del_net_client(nc
);
1168 void netdev_add(QemuOpts
*opts
, Error
**errp
)
1170 net_client_init(opts
, 1, 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
;
1226 StringOutputVisitor
*ov
;
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 ov
= string_output_visitor_new(false);
1235 object_property_get(OBJECT(nf
), string_output_get_visitor(ov
),
1237 str
= string_output_get_string(ov
);
1238 string_output_visitor_cleanup(ov
);
1239 monitor_printf(mon
, ",%s=%s", prop
->name
, str
);
1242 monitor_printf(mon
, "\n");
1245 void print_net_client(Monitor
*mon
, NetClientState
*nc
)
1249 monitor_printf(mon
, "%s: index=%d,type=%s,%s\n", nc
->name
,
1251 NetClientOptionsKind_lookup
[nc
->info
->type
],
1253 if (!QTAILQ_EMPTY(&nc
->filters
)) {
1254 monitor_printf(mon
, "filters:\n");
1256 QTAILQ_FOREACH(nf
, &nc
->filters
, next
) {
1257 char *path
= object_get_canonical_path_component(OBJECT(nf
));
1259 monitor_printf(mon
, " - %s: type=%s", path
,
1260 object_get_typename(OBJECT(nf
)));
1261 netfilter_print_info(mon
, nf
);
1266 RxFilterInfoList
*qmp_query_rx_filter(bool has_name
, const char *name
,
1270 RxFilterInfoList
*filter_list
= NULL
, *last_entry
= NULL
;
1272 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
1273 RxFilterInfoList
*entry
;
1276 if (has_name
&& strcmp(nc
->name
, name
) != 0) {
1280 /* only query rx-filter information of NIC */
1281 if (nc
->info
->type
!= NET_CLIENT_OPTIONS_KIND_NIC
) {
1283 error_setg(errp
, "net client(%s) isn't a NIC", name
);
1289 /* only query information on queue 0 since the info is per nic,
1292 if (nc
->queue_index
!= 0)
1295 if (nc
->info
->query_rx_filter
) {
1296 info
= nc
->info
->query_rx_filter(nc
);
1297 entry
= g_malloc0(sizeof(*entry
));
1298 entry
->value
= info
;
1301 filter_list
= entry
;
1303 last_entry
->next
= entry
;
1306 } else if (has_name
) {
1307 error_setg(errp
, "net client(%s) doesn't support"
1308 " rx-filter querying", name
);
1317 if (filter_list
== NULL
&& has_name
) {
1318 error_setg(errp
, "invalid net client name: %s", name
);
1324 void hmp_info_network(Monitor
*mon
, const QDict
*qdict
)
1326 NetClientState
*nc
, *peer
;
1327 NetClientOptionsKind type
;
1331 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
1333 type
= nc
->info
->type
;
1335 /* Skip if already printed in hub info */
1336 if (net_hub_id_for_client(nc
, NULL
) == 0) {
1340 if (!peer
|| type
== NET_CLIENT_OPTIONS_KIND_NIC
) {
1341 print_net_client(mon
, nc
);
1342 } /* else it's a netdev connected to a NIC, printed with the NIC */
1343 if (peer
&& type
== NET_CLIENT_OPTIONS_KIND_NIC
) {
1344 monitor_printf(mon
, " \\ ");
1345 print_net_client(mon
, peer
);
1350 void qmp_set_link(const char *name
, bool up
, Error
**errp
)
1352 NetClientState
*ncs
[MAX_QUEUE_NUM
];
1356 queues
= qemu_find_net_clients_except(name
, ncs
,
1357 NET_CLIENT_OPTIONS_KIND__MAX
,
1361 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1362 "Device '%s' not found", name
);
1367 for (i
= 0; i
< queues
; i
++) {
1368 ncs
[i
]->link_down
= !up
;
1371 if (nc
->info
->link_status_changed
) {
1372 nc
->info
->link_status_changed(nc
);
1376 /* Change peer link only if the peer is NIC and then notify peer.
1377 * If the peer is a HUBPORT or a backend, we do not change the
1380 * This behavior is compatible with qemu vlans where there could be
1381 * multiple clients that can still communicate with each other in
1382 * disconnected mode. For now maintain this compatibility.
1384 if (nc
->peer
->info
->type
== NET_CLIENT_OPTIONS_KIND_NIC
) {
1385 for (i
= 0; i
< queues
; i
++) {
1386 ncs
[i
]->peer
->link_down
= !up
;
1389 if (nc
->peer
->info
->link_status_changed
) {
1390 nc
->peer
->info
->link_status_changed(nc
->peer
);
1395 static void net_vm_change_state_handler(void *opaque
, int running
,
1399 NetClientState
*tmp
;
1401 QTAILQ_FOREACH_SAFE(nc
, &net_clients
, next
, tmp
) {
1403 /* Flush queued packets and wake up backends. */
1404 if (nc
->peer
&& qemu_can_send_packet(nc
)) {
1405 qemu_flush_queued_packets(nc
->peer
);
1408 /* Complete all queued packets, to guarantee we don't modify
1409 * state later when VM is not running.
1411 qemu_flush_or_purge_queued_packets(nc
, true);
1416 void net_cleanup(void)
1420 /* We may del multiple entries during qemu_del_net_client(),
1421 * so QTAILQ_FOREACH_SAFE() is also not safe here.
1423 while (!QTAILQ_EMPTY(&net_clients
)) {
1424 nc
= QTAILQ_FIRST(&net_clients
);
1425 if (nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_NIC
) {
1426 qemu_del_nic(qemu_get_nic(nc
));
1428 qemu_del_net_client(nc
);
1432 qemu_del_vm_change_state_handler(net_change_state_entry
);
1435 void net_check_clients(void)
1440 /* Don't warn about the default network setup that you get if
1441 * no command line -net or -netdev options are specified. There
1442 * are two cases that we would otherwise complain about:
1443 * (1) board doesn't support a NIC but the implicit "-net nic"
1445 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
1446 * sets up a nic that isn't connected to anything.
1452 net_hub_check_clients();
1454 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
1456 fprintf(stderr
, "Warning: %s %s has no peer\n",
1457 nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_NIC
?
1458 "nic" : "netdev", nc
->name
);
1462 /* Check that all NICs requested via -net nic actually got created.
1463 * NICs created via -device don't need to be checked here because
1464 * they are always instantiated.
1466 for (i
= 0; i
< MAX_NICS
; i
++) {
1467 NICInfo
*nd
= &nd_table
[i
];
1468 if (nd
->used
&& !nd
->instantiated
) {
1469 fprintf(stderr
, "Warning: requested NIC (%s, model %s) "
1470 "was not created (not supported by this machine?)\n",
1471 nd
->name
? nd
->name
: "anonymous",
1472 nd
->model
? nd
->model
: "unspecified");
1477 static int net_init_client(void *dummy
, QemuOpts
*opts
, Error
**errp
)
1479 Error
*local_err
= NULL
;
1481 net_client_init(opts
, 0, &local_err
);
1483 error_report_err(local_err
);
1490 static int net_init_netdev(void *dummy
, QemuOpts
*opts
, Error
**errp
)
1492 Error
*local_err
= NULL
;
1495 ret
= net_client_init(opts
, 1, &local_err
);
1497 error_report_err(local_err
);
1504 int net_init_clients(void)
1506 QemuOptsList
*net
= qemu_find_opts("net");
1509 /* if no clients, we use a default config */
1510 qemu_opts_set(net
, NULL
, "type", "nic", &error_abort
);
1512 qemu_opts_set(net
, NULL
, "type", "user", &error_abort
);
1516 net_change_state_entry
=
1517 qemu_add_vm_change_state_handler(net_vm_change_state_handler
, NULL
);
1519 QTAILQ_INIT(&net_clients
);
1521 if (qemu_opts_foreach(qemu_find_opts("netdev"),
1522 net_init_netdev
, NULL
, NULL
)) {
1526 if (qemu_opts_foreach(net
, net_init_client
, NULL
, NULL
)) {
1533 int net_client_parse(QemuOptsList
*opts_list
, const char *optarg
)
1535 #if defined(CONFIG_SLIRP)
1537 if (net_slirp_parse_legacy(opts_list
, optarg
, &ret
)) {
1542 if (!qemu_opts_parse_noisily(opts_list
, optarg
, true)) {
1552 unsigned compute_mcast_idx(const uint8_t *ep
)
1559 for (i
= 0; i
< 6; i
++) {
1561 for (j
= 0; j
< 8; j
++) {
1562 carry
= ((crc
& 0x80000000L
) ? 1 : 0) ^ (b
& 0x01);
1566 crc
= ((crc
^ POLYNOMIAL
) | carry
);
1573 QemuOptsList qemu_netdev_opts
= {
1575 .implied_opt_name
= "type",
1576 .head
= QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts
.head
),
1579 * no elements => accept any params
1580 * validation will happen later
1582 { /* end of list */ }
1586 QemuOptsList qemu_net_opts
= {
1588 .implied_opt_name
= "type",
1589 .head
= QTAILQ_HEAD_INITIALIZER(qemu_net_opts
.head
),
1592 * no elements => accept any params
1593 * validation will happen later
1595 { /* end of list */ }