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 "qemu/sockets.h"
36 #include "qemu/config-file.h"
37 #include "qmp-commands.h"
40 #include "qemu/main-loop.h"
41 #include "qapi-visit.h"
42 #include "qapi/opts-visitor.h"
43 #include "qapi/dealloc-visitor.h"
44 #include "sysemu/sysemu.h"
46 /* Net bridge is currently not supported for W32. */
48 # define CONFIG_NET_BRIDGE
51 static VMChangeStateEntry
*net_change_state_entry
;
52 static QTAILQ_HEAD(, NetClientState
) net_clients
;
54 const char *host_net_devices
[] = {
58 #ifdef CONFIG_NET_BRIDGE
73 /***********************************************************/
74 /* network device redirectors */
76 #if defined(DEBUG_NET)
77 static void hex_dump(FILE *f
, const uint8_t *buf
, int size
)
81 for(i
=0;i
<size
;i
+=16) {
85 fprintf(f
, "%08x ", i
);
88 fprintf(f
, " %02x", buf
[i
+j
]);
95 if (c
< ' ' || c
> '~')
104 static int get_str_sep(char *buf
, int buf_size
, const char **pp
, int sep
)
115 if (len
> buf_size
- 1)
124 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
)
132 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
134 saddr
->sin_family
= AF_INET
;
135 if (buf
[0] == '\0') {
136 saddr
->sin_addr
.s_addr
= 0;
138 if (qemu_isdigit(buf
[0])) {
139 if (!inet_aton(buf
, &saddr
->sin_addr
))
142 if ((he
= gethostbyname(buf
)) == NULL
)
144 saddr
->sin_addr
= *(struct in_addr
*)he
->h_addr
;
147 port
= strtol(p
, (char **)&r
, 0);
150 saddr
->sin_port
= htons(port
);
154 char *qemu_mac_strdup_printf(const uint8_t *macaddr
)
156 return g_strdup_printf("%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",
157 macaddr
[0], macaddr
[1], macaddr
[2],
158 macaddr
[3], macaddr
[4], macaddr
[5]);
161 void qemu_format_nic_info_str(NetClientState
*nc
, uint8_t macaddr
[6])
163 snprintf(nc
->info_str
, sizeof(nc
->info_str
),
164 "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
166 macaddr
[0], macaddr
[1], macaddr
[2],
167 macaddr
[3], macaddr
[4], macaddr
[5]);
170 void qemu_macaddr_default_if_unset(MACAddr
*macaddr
)
172 static int index
= 0;
173 static const MACAddr zero
= { .a
= { 0,0,0,0,0,0 } };
175 if (memcmp(macaddr
, &zero
, sizeof(zero
)) != 0)
177 macaddr
->a
[0] = 0x52;
178 macaddr
->a
[1] = 0x54;
179 macaddr
->a
[2] = 0x00;
180 macaddr
->a
[3] = 0x12;
181 macaddr
->a
[4] = 0x34;
182 macaddr
->a
[5] = 0x56 + index
++;
186 * Generate a name for net client
188 * Only net clients created with the legacy -net option and NICs need this.
190 static char *assign_name(NetClientState
*nc1
, const char *model
)
195 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
199 if (strcmp(nc
->model
, model
) == 0) {
204 return g_strdup_printf("%s.%d", model
, id
);
207 static void qemu_net_client_destructor(NetClientState
*nc
)
212 static void qemu_net_client_setup(NetClientState
*nc
,
214 NetClientState
*peer
,
217 NetClientDestructor
*destructor
)
220 nc
->model
= g_strdup(model
);
222 nc
->name
= g_strdup(name
);
224 nc
->name
= assign_name(nc
, model
);
232 QTAILQ_INSERT_TAIL(&net_clients
, nc
, next
);
234 nc
->incoming_queue
= qemu_new_net_queue(nc
);
235 nc
->destructor
= destructor
;
238 NetClientState
*qemu_new_net_client(NetClientInfo
*info
,
239 NetClientState
*peer
,
245 assert(info
->size
>= sizeof(NetClientState
));
247 nc
= g_malloc0(info
->size
);
248 qemu_net_client_setup(nc
, info
, peer
, model
, name
,
249 qemu_net_client_destructor
);
254 NICState
*qemu_new_nic(NetClientInfo
*info
,
260 NetClientState
**peers
= conf
->peers
.ncs
;
262 int i
, queues
= MAX(1, conf
->peers
.queues
);
264 assert(info
->type
== NET_CLIENT_OPTIONS_KIND_NIC
);
265 assert(info
->size
>= sizeof(NICState
));
267 nic
= g_malloc0(info
->size
+ sizeof(NetClientState
) * queues
);
268 nic
->ncs
= (void *)nic
+ info
->size
;
270 nic
->opaque
= opaque
;
272 for (i
= 0; i
< queues
; i
++) {
273 qemu_net_client_setup(&nic
->ncs
[i
], info
, peers
[i
], model
, name
,
275 nic
->ncs
[i
].queue_index
= i
;
281 NetClientState
*qemu_get_subqueue(NICState
*nic
, int queue_index
)
283 return nic
->ncs
+ queue_index
;
286 NetClientState
*qemu_get_queue(NICState
*nic
)
288 return qemu_get_subqueue(nic
, 0);
291 NICState
*qemu_get_nic(NetClientState
*nc
)
293 NetClientState
*nc0
= nc
- nc
->queue_index
;
295 return (NICState
*)((void *)nc0
- nc
->info
->size
);
298 void *qemu_get_nic_opaque(NetClientState
*nc
)
300 NICState
*nic
= qemu_get_nic(nc
);
305 static void qemu_cleanup_net_client(NetClientState
*nc
)
307 QTAILQ_REMOVE(&net_clients
, nc
, next
);
309 if (nc
->info
->cleanup
) {
310 nc
->info
->cleanup(nc
);
314 static void qemu_free_net_client(NetClientState
*nc
)
316 if (nc
->incoming_queue
) {
317 qemu_del_net_queue(nc
->incoming_queue
);
320 nc
->peer
->peer
= NULL
;
324 if (nc
->destructor
) {
329 void qemu_del_net_client(NetClientState
*nc
)
331 NetClientState
*ncs
[MAX_QUEUE_NUM
];
334 assert(nc
->info
->type
!= NET_CLIENT_OPTIONS_KIND_NIC
);
336 /* If the NetClientState belongs to a multiqueue backend, we will change all
337 * other NetClientStates also.
339 queues
= qemu_find_net_clients_except(nc
->name
, ncs
,
340 NET_CLIENT_OPTIONS_KIND_NIC
,
344 /* If there is a peer NIC, delete and cleanup client, but do not free. */
345 if (nc
->peer
&& nc
->peer
->info
->type
== NET_CLIENT_OPTIONS_KIND_NIC
) {
346 NICState
*nic
= qemu_get_nic(nc
->peer
);
347 if (nic
->peer_deleted
) {
350 nic
->peer_deleted
= true;
352 for (i
= 0; i
< queues
; i
++) {
353 ncs
[i
]->peer
->link_down
= true;
356 if (nc
->peer
->info
->link_status_changed
) {
357 nc
->peer
->info
->link_status_changed(nc
->peer
);
360 for (i
= 0; i
< queues
; i
++) {
361 qemu_cleanup_net_client(ncs
[i
]);
367 for (i
= 0; i
< queues
; i
++) {
368 qemu_cleanup_net_client(ncs
[i
]);
369 qemu_free_net_client(ncs
[i
]);
373 void qemu_del_nic(NICState
*nic
)
375 int i
, queues
= MAX(nic
->conf
->peers
.queues
, 1);
377 /* If this is a peer NIC and peer has already been deleted, free it now. */
378 if (nic
->peer_deleted
) {
379 for (i
= 0; i
< queues
; i
++) {
380 qemu_free_net_client(qemu_get_subqueue(nic
, i
)->peer
);
384 for (i
= queues
- 1; i
>= 0; i
--) {
385 NetClientState
*nc
= qemu_get_subqueue(nic
, i
);
387 qemu_cleanup_net_client(nc
);
388 qemu_free_net_client(nc
);
394 void qemu_foreach_nic(qemu_nic_foreach func
, void *opaque
)
398 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
399 if (nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_NIC
) {
400 if (nc
->queue_index
== 0) {
401 func(qemu_get_nic(nc
), opaque
);
407 bool qemu_has_ufo(NetClientState
*nc
)
409 if (!nc
|| !nc
->info
->has_ufo
) {
413 return nc
->info
->has_ufo(nc
);
416 bool qemu_has_vnet_hdr(NetClientState
*nc
)
418 if (!nc
|| !nc
->info
->has_vnet_hdr
) {
422 return nc
->info
->has_vnet_hdr(nc
);
425 bool qemu_has_vnet_hdr_len(NetClientState
*nc
, int len
)
427 if (!nc
|| !nc
->info
->has_vnet_hdr_len
) {
431 return nc
->info
->has_vnet_hdr_len(nc
, len
);
434 void qemu_using_vnet_hdr(NetClientState
*nc
, bool enable
)
436 if (!nc
|| !nc
->info
->using_vnet_hdr
) {
440 nc
->info
->using_vnet_hdr(nc
, enable
);
443 void qemu_set_offload(NetClientState
*nc
, int csum
, int tso4
, int tso6
,
446 if (!nc
|| !nc
->info
->set_offload
) {
450 nc
->info
->set_offload(nc
, csum
, tso4
, tso6
, ecn
, ufo
);
453 void qemu_set_vnet_hdr_len(NetClientState
*nc
, int len
)
455 if (!nc
|| !nc
->info
->set_vnet_hdr_len
) {
459 nc
->info
->set_vnet_hdr_len(nc
, len
);
462 int qemu_can_send_packet(NetClientState
*sender
)
464 int vm_running
= runstate_is_running();
474 if (sender
->peer
->receive_disabled
) {
476 } else if (sender
->peer
->info
->can_receive
&&
477 !sender
->peer
->info
->can_receive(sender
->peer
)) {
483 ssize_t
qemu_deliver_packet(NetClientState
*sender
,
489 NetClientState
*nc
= opaque
;
496 if (nc
->receive_disabled
) {
500 if (flags
& QEMU_NET_PACKET_FLAG_RAW
&& nc
->info
->receive_raw
) {
501 ret
= nc
->info
->receive_raw(nc
, data
, size
);
503 ret
= nc
->info
->receive(nc
, data
, size
);
507 nc
->receive_disabled
= 1;
513 void qemu_purge_queued_packets(NetClientState
*nc
)
519 qemu_net_queue_purge(nc
->peer
->incoming_queue
, nc
);
523 void qemu_flush_or_purge_queued_packets(NetClientState
*nc
, bool purge
)
525 nc
->receive_disabled
= 0;
527 if (nc
->peer
&& nc
->peer
->info
->type
== NET_CLIENT_OPTIONS_KIND_HUBPORT
) {
528 if (net_hub_flush(nc
->peer
)) {
532 if (qemu_net_queue_flush(nc
->incoming_queue
)) {
533 /* We emptied the queue successfully, signal to the IO thread to repoll
534 * the file descriptor (for tap, for example).
538 /* Unable to empty the queue, purge remaining packets */
539 qemu_net_queue_purge(nc
->incoming_queue
, nc
);
543 void qemu_flush_queued_packets(NetClientState
*nc
)
545 qemu_flush_or_purge_queued_packets(nc
, false);
548 static ssize_t
qemu_send_packet_async_with_flags(NetClientState
*sender
,
550 const uint8_t *buf
, int size
,
551 NetPacketSent
*sent_cb
)
556 printf("qemu_send_packet_async:\n");
557 hex_dump(stdout
, buf
, size
);
560 if (sender
->link_down
|| !sender
->peer
) {
564 queue
= sender
->peer
->incoming_queue
;
566 return qemu_net_queue_send(queue
, sender
, flags
, buf
, size
, sent_cb
);
569 ssize_t
qemu_send_packet_async(NetClientState
*sender
,
570 const uint8_t *buf
, int size
,
571 NetPacketSent
*sent_cb
)
573 return qemu_send_packet_async_with_flags(sender
, QEMU_NET_PACKET_FLAG_NONE
,
577 void qemu_send_packet(NetClientState
*nc
, const uint8_t *buf
, int size
)
579 qemu_send_packet_async(nc
, buf
, size
, NULL
);
582 ssize_t
qemu_send_packet_raw(NetClientState
*nc
, const uint8_t *buf
, int size
)
584 return qemu_send_packet_async_with_flags(nc
, QEMU_NET_PACKET_FLAG_RAW
,
588 static ssize_t
nc_sendv_compat(NetClientState
*nc
, const struct iovec
*iov
,
591 uint8_t buffer
[NET_BUFSIZE
];
594 offset
= iov_to_buf(iov
, iovcnt
, 0, buffer
, sizeof(buffer
));
596 return nc
->info
->receive(nc
, buffer
, offset
);
599 ssize_t
qemu_deliver_packet_iov(NetClientState
*sender
,
601 const struct iovec
*iov
,
605 NetClientState
*nc
= opaque
;
609 return iov_size(iov
, iovcnt
);
612 if (nc
->receive_disabled
) {
616 if (nc
->info
->receive_iov
) {
617 ret
= nc
->info
->receive_iov(nc
, iov
, iovcnt
);
619 ret
= nc_sendv_compat(nc
, iov
, iovcnt
);
623 nc
->receive_disabled
= 1;
629 ssize_t
qemu_sendv_packet_async(NetClientState
*sender
,
630 const struct iovec
*iov
, int iovcnt
,
631 NetPacketSent
*sent_cb
)
635 if (sender
->link_down
|| !sender
->peer
) {
636 return iov_size(iov
, iovcnt
);
639 queue
= sender
->peer
->incoming_queue
;
641 return qemu_net_queue_send_iov(queue
, sender
,
642 QEMU_NET_PACKET_FLAG_NONE
,
643 iov
, iovcnt
, sent_cb
);
647 qemu_sendv_packet(NetClientState
*nc
, const struct iovec
*iov
, int iovcnt
)
649 return qemu_sendv_packet_async(nc
, iov
, iovcnt
, NULL
);
652 NetClientState
*qemu_find_netdev(const char *id
)
656 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
657 if (nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_NIC
)
659 if (!strcmp(nc
->name
, id
)) {
667 int qemu_find_net_clients_except(const char *id
, NetClientState
**ncs
,
668 NetClientOptionsKind type
, int max
)
673 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
674 if (nc
->info
->type
== type
) {
677 if (!id
|| !strcmp(nc
->name
, id
)) {
688 static int nic_get_free_idx(void)
692 for (index
= 0; index
< MAX_NICS
; index
++)
693 if (!nd_table
[index
].used
)
698 int qemu_show_nic_models(const char *arg
, const char *const *models
)
702 if (!arg
|| !is_help_option(arg
)) {
706 fprintf(stderr
, "qemu: Supported NIC models: ");
707 for (i
= 0 ; models
[i
]; i
++)
708 fprintf(stderr
, "%s%c", models
[i
], models
[i
+1] ? ',' : '\n');
712 void qemu_check_nic_model(NICInfo
*nd
, const char *model
)
714 const char *models
[2];
719 if (qemu_show_nic_models(nd
->model
, models
))
721 if (qemu_find_nic_model(nd
, models
, model
) < 0)
725 int qemu_find_nic_model(NICInfo
*nd
, const char * const *models
,
726 const char *default_model
)
731 nd
->model
= g_strdup(default_model
);
733 for (i
= 0 ; models
[i
]; i
++) {
734 if (strcmp(nd
->model
, models
[i
]) == 0)
738 error_report("Unsupported NIC model: %s", nd
->model
);
742 static int net_init_nic(const NetClientOptions
*opts
, const char *name
,
743 NetClientState
*peer
, Error
**errp
)
747 const NetLegacyNicOptions
*nic
;
749 assert(opts
->kind
== NET_CLIENT_OPTIONS_KIND_NIC
);
752 idx
= nic_get_free_idx();
753 if (idx
== -1 || nb_nics
>= MAX_NICS
) {
754 error_setg(errp
, "too many NICs");
760 memset(nd
, 0, sizeof(*nd
));
762 if (nic
->has_netdev
) {
763 nd
->netdev
= qemu_find_netdev(nic
->netdev
);
765 error_setg(errp
, "netdev '%s' not found", nic
->netdev
);
772 nd
->name
= g_strdup(name
);
773 if (nic
->has_model
) {
774 nd
->model
= g_strdup(nic
->model
);
777 nd
->devaddr
= g_strdup(nic
->addr
);
780 if (nic
->has_macaddr
&&
781 net_parse_macaddr(nd
->macaddr
.a
, nic
->macaddr
) < 0) {
782 error_setg(errp
, "invalid syntax for ethernet address");
785 if (nic
->has_macaddr
&&
786 is_multicast_ether_addr(nd
->macaddr
.a
)) {
788 "NIC cannot have multicast MAC address (odd 1st byte)");
791 qemu_macaddr_default_if_unset(&nd
->macaddr
);
793 if (nic
->has_vectors
) {
794 if (nic
->vectors
> 0x7ffffff) {
795 error_setg(errp
, "invalid # of vectors: %"PRIu32
, nic
->vectors
);
798 nd
->nvectors
= nic
->vectors
;
800 nd
->nvectors
= DEV_NVECTORS_UNSPECIFIED
;
810 static int (* const net_client_init_fun
[NET_CLIENT_OPTIONS_KIND_MAX
])(
811 const NetClientOptions
*opts
,
813 NetClientState
*peer
, Error
**errp
) = {
814 [NET_CLIENT_OPTIONS_KIND_NIC
] = net_init_nic
,
816 [NET_CLIENT_OPTIONS_KIND_USER
] = net_init_slirp
,
818 [NET_CLIENT_OPTIONS_KIND_TAP
] = net_init_tap
,
819 [NET_CLIENT_OPTIONS_KIND_SOCKET
] = net_init_socket
,
821 [NET_CLIENT_OPTIONS_KIND_VDE
] = net_init_vde
,
824 [NET_CLIENT_OPTIONS_KIND_NETMAP
] = net_init_netmap
,
826 [NET_CLIENT_OPTIONS_KIND_DUMP
] = net_init_dump
,
827 #ifdef CONFIG_NET_BRIDGE
828 [NET_CLIENT_OPTIONS_KIND_BRIDGE
] = net_init_bridge
,
830 [NET_CLIENT_OPTIONS_KIND_HUBPORT
] = net_init_hubport
,
831 #ifdef CONFIG_VHOST_NET_USED
832 [NET_CLIENT_OPTIONS_KIND_VHOST_USER
] = net_init_vhost_user
,
835 [NET_CLIENT_OPTIONS_KIND_L2TPV3
] = net_init_l2tpv3
,
840 static int net_client_init1(const void *object
, int is_netdev
, Error
**errp
)
843 const Netdev
*netdev
;
844 const NetLegacy
*net
;
846 const NetClientOptions
*opts
;
851 opts
= u
.netdev
->opts
;
854 switch (opts
->kind
) {
856 case NET_CLIENT_OPTIONS_KIND_USER
:
858 case NET_CLIENT_OPTIONS_KIND_TAP
:
859 case NET_CLIENT_OPTIONS_KIND_SOCKET
:
861 case NET_CLIENT_OPTIONS_KIND_VDE
:
864 case NET_CLIENT_OPTIONS_KIND_NETMAP
:
866 #ifdef CONFIG_NET_BRIDGE
867 case NET_CLIENT_OPTIONS_KIND_BRIDGE
:
869 case NET_CLIENT_OPTIONS_KIND_HUBPORT
:
870 #ifdef CONFIG_VHOST_NET_USED
871 case NET_CLIENT_OPTIONS_KIND_VHOST_USER
:
874 case NET_CLIENT_OPTIONS_KIND_L2TPV3
:
879 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, "type",
880 "a netdev backend type");
886 if (opts
->kind
== NET_CLIENT_OPTIONS_KIND_HUBPORT
) {
887 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, "type",
891 /* missing optional values have been initialized to "all bits zero" */
892 name
= u
.net
->has_id
? u
.net
->id
: u
.net
->name
;
895 if (net_client_init_fun
[opts
->kind
]) {
896 NetClientState
*peer
= NULL
;
898 /* Do not add to a vlan if it's a -netdev or a nic with a netdev=
901 (opts
->kind
!= NET_CLIENT_OPTIONS_KIND_NIC
||
902 !opts
->nic
->has_netdev
)) {
903 peer
= net_hub_add_port(u
.net
->has_vlan
? u
.net
->vlan
: 0, NULL
);
906 if (net_client_init_fun
[opts
->kind
](opts
, name
, peer
, errp
) < 0) {
907 /* FIXME drop when all init functions store an Error */
908 if (errp
&& !*errp
) {
909 error_set(errp
, QERR_DEVICE_INIT_FAILED
,
910 NetClientOptionsKind_lookup
[opts
->kind
]);
919 static void net_visit(Visitor
*v
, int is_netdev
, void **object
, Error
**errp
)
922 visit_type_Netdev(v
, (Netdev
**)object
, NULL
, errp
);
924 visit_type_NetLegacy(v
, (NetLegacy
**)object
, NULL
, errp
);
929 int net_client_init(QemuOpts
*opts
, int is_netdev
, Error
**errp
)
936 OptsVisitor
*ov
= opts_visitor_new(opts
);
938 net_visit(opts_get_visitor(ov
), is_netdev
, &object
, &err
);
939 opts_visitor_cleanup(ov
);
943 ret
= net_client_init1(object
, is_netdev
, &err
);
947 QapiDeallocVisitor
*dv
= qapi_dealloc_visitor_new();
949 net_visit(qapi_dealloc_get_visitor(dv
), is_netdev
, &object
, NULL
);
950 qapi_dealloc_visitor_cleanup(dv
);
953 error_propagate(errp
, err
);
958 static int net_host_check_device(const char *device
)
961 for (i
= 0; host_net_devices
[i
]; i
++) {
962 if (!strncmp(host_net_devices
[i
], device
,
963 strlen(host_net_devices
[i
]))) {
971 void hmp_host_net_add(Monitor
*mon
, const QDict
*qdict
)
973 const char *device
= qdict_get_str(qdict
, "device");
974 const char *opts_str
= qdict_get_try_str(qdict
, "opts");
975 Error
*local_err
= NULL
;
978 if (!net_host_check_device(device
)) {
979 monitor_printf(mon
, "invalid host network device %s\n", device
);
983 opts
= qemu_opts_parse(qemu_find_opts("net"), opts_str
? opts_str
: "", 0);
988 qemu_opt_set(opts
, "type", device
, &error_abort
);
990 net_client_init(opts
, 0, &local_err
);
992 error_report_err(local_err
);
993 monitor_printf(mon
, "adding host network device %s failed\n", device
);
997 void hmp_host_net_remove(Monitor
*mon
, const QDict
*qdict
)
1000 int vlan_id
= qdict_get_int(qdict
, "vlan_id");
1001 const char *device
= qdict_get_str(qdict
, "device");
1003 nc
= net_hub_find_client_by_name(vlan_id
, device
);
1005 error_report("Host network device '%s' on hub '%d' not found",
1009 if (nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_NIC
) {
1010 error_report("invalid host network device '%s'", device
);
1014 qemu_del_net_client(nc
->peer
);
1015 qemu_del_net_client(nc
);
1018 void netdev_add(QemuOpts
*opts
, Error
**errp
)
1020 net_client_init(opts
, 1, errp
);
1023 int qmp_netdev_add(Monitor
*mon
, const QDict
*qdict
, QObject
**ret
)
1025 Error
*local_err
= NULL
;
1026 QemuOptsList
*opts_list
;
1029 opts_list
= qemu_find_opts_err("netdev", &local_err
);
1034 opts
= qemu_opts_from_qdict(opts_list
, qdict
, &local_err
);
1039 netdev_add(opts
, &local_err
);
1041 qemu_opts_del(opts
);
1048 qerror_report_err(local_err
);
1049 error_free(local_err
);
1053 void qmp_netdev_del(const char *id
, Error
**errp
)
1058 nc
= qemu_find_netdev(id
);
1060 error_set(errp
, QERR_DEVICE_NOT_FOUND
, id
);
1064 opts
= qemu_opts_find(qemu_find_opts_err("netdev", NULL
), id
);
1066 error_setg(errp
, "Device '%s' is not a netdev", id
);
1070 qemu_del_net_client(nc
);
1071 qemu_opts_del(opts
);
1074 void print_net_client(Monitor
*mon
, NetClientState
*nc
)
1076 monitor_printf(mon
, "%s: index=%d,type=%s,%s\n", nc
->name
,
1078 NetClientOptionsKind_lookup
[nc
->info
->type
],
1082 RxFilterInfoList
*qmp_query_rx_filter(bool has_name
, const char *name
,
1086 RxFilterInfoList
*filter_list
= NULL
, *last_entry
= NULL
;
1088 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
1089 RxFilterInfoList
*entry
;
1092 if (has_name
&& strcmp(nc
->name
, name
) != 0) {
1096 /* only query rx-filter information of NIC */
1097 if (nc
->info
->type
!= NET_CLIENT_OPTIONS_KIND_NIC
) {
1099 error_setg(errp
, "net client(%s) isn't a NIC", name
);
1105 if (nc
->info
->query_rx_filter
) {
1106 info
= nc
->info
->query_rx_filter(nc
);
1107 entry
= g_malloc0(sizeof(*entry
));
1108 entry
->value
= info
;
1111 filter_list
= entry
;
1113 last_entry
->next
= entry
;
1116 } else if (has_name
) {
1117 error_setg(errp
, "net client(%s) doesn't support"
1118 " rx-filter querying", name
);
1127 if (filter_list
== NULL
&& has_name
) {
1128 error_setg(errp
, "invalid net client name: %s", name
);
1134 void hmp_info_network(Monitor
*mon
, const QDict
*qdict
)
1136 NetClientState
*nc
, *peer
;
1137 NetClientOptionsKind type
;
1141 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
1143 type
= nc
->info
->type
;
1145 /* Skip if already printed in hub info */
1146 if (net_hub_id_for_client(nc
, NULL
) == 0) {
1150 if (!peer
|| type
== NET_CLIENT_OPTIONS_KIND_NIC
) {
1151 print_net_client(mon
, nc
);
1152 } /* else it's a netdev connected to a NIC, printed with the NIC */
1153 if (peer
&& type
== NET_CLIENT_OPTIONS_KIND_NIC
) {
1154 monitor_printf(mon
, " \\ ");
1155 print_net_client(mon
, peer
);
1160 void qmp_set_link(const char *name
, bool up
, Error
**errp
)
1162 NetClientState
*ncs
[MAX_QUEUE_NUM
];
1166 queues
= qemu_find_net_clients_except(name
, ncs
,
1167 NET_CLIENT_OPTIONS_KIND_MAX
,
1171 error_set(errp
, QERR_DEVICE_NOT_FOUND
, name
);
1176 for (i
= 0; i
< queues
; i
++) {
1177 ncs
[i
]->link_down
= !up
;
1180 if (nc
->info
->link_status_changed
) {
1181 nc
->info
->link_status_changed(nc
);
1185 /* Change peer link only if the peer is NIC and then notify peer.
1186 * If the peer is a HUBPORT or a backend, we do not change the
1189 * This behavior is compatible with qemu vlans where there could be
1190 * multiple clients that can still communicate with each other in
1191 * disconnected mode. For now maintain this compatibility.
1193 if (nc
->peer
->info
->type
== NET_CLIENT_OPTIONS_KIND_NIC
) {
1194 for (i
= 0; i
< queues
; i
++) {
1195 ncs
[i
]->peer
->link_down
= !up
;
1198 if (nc
->peer
->info
->link_status_changed
) {
1199 nc
->peer
->info
->link_status_changed(nc
->peer
);
1204 static void net_vm_change_state_handler(void *opaque
, int running
,
1207 /* Complete all queued packets, to guarantee we don't modify
1208 * state later when VM is not running.
1212 NetClientState
*tmp
;
1214 QTAILQ_FOREACH_SAFE(nc
, &net_clients
, next
, tmp
) {
1215 qemu_flush_or_purge_queued_packets(nc
, true);
1220 void net_cleanup(void)
1224 /* We may del multiple entries during qemu_del_net_client(),
1225 * so QTAILQ_FOREACH_SAFE() is also not safe here.
1227 while (!QTAILQ_EMPTY(&net_clients
)) {
1228 nc
= QTAILQ_FIRST(&net_clients
);
1229 if (nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_NIC
) {
1230 qemu_del_nic(qemu_get_nic(nc
));
1232 qemu_del_net_client(nc
);
1236 qemu_del_vm_change_state_handler(net_change_state_entry
);
1239 void net_check_clients(void)
1244 /* Don't warn about the default network setup that you get if
1245 * no command line -net or -netdev options are specified. There
1246 * are two cases that we would otherwise complain about:
1247 * (1) board doesn't support a NIC but the implicit "-net nic"
1249 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
1250 * sets up a nic that isn't connected to anything.
1256 net_hub_check_clients();
1258 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
1260 fprintf(stderr
, "Warning: %s %s has no peer\n",
1261 nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_NIC
?
1262 "nic" : "netdev", nc
->name
);
1266 /* Check that all NICs requested via -net nic actually got created.
1267 * NICs created via -device don't need to be checked here because
1268 * they are always instantiated.
1270 for (i
= 0; i
< MAX_NICS
; i
++) {
1271 NICInfo
*nd
= &nd_table
[i
];
1272 if (nd
->used
&& !nd
->instantiated
) {
1273 fprintf(stderr
, "Warning: requested NIC (%s, model %s) "
1274 "was not created (not supported by this machine?)\n",
1275 nd
->name
? nd
->name
: "anonymous",
1276 nd
->model
? nd
->model
: "unspecified");
1281 static int net_init_client(QemuOpts
*opts
, void *dummy
)
1283 Error
*local_err
= NULL
;
1285 net_client_init(opts
, 0, &local_err
);
1287 error_report_err(local_err
);
1294 static int net_init_netdev(QemuOpts
*opts
, void *dummy
)
1296 Error
*local_err
= NULL
;
1299 ret
= net_client_init(opts
, 1, &local_err
);
1301 error_report_err(local_err
);
1308 int net_init_clients(void)
1310 QemuOptsList
*net
= qemu_find_opts("net");
1313 /* if no clients, we use a default config */
1314 qemu_opts_set(net
, NULL
, "type", "nic", &error_abort
);
1316 qemu_opts_set(net
, NULL
, "type", "user", &error_abort
);
1320 net_change_state_entry
=
1321 qemu_add_vm_change_state_handler(net_vm_change_state_handler
, NULL
);
1323 QTAILQ_INIT(&net_clients
);
1325 if (qemu_opts_foreach(qemu_find_opts("netdev"), net_init_netdev
, NULL
, 1) == -1)
1328 if (qemu_opts_foreach(net
, net_init_client
, NULL
, 1) == -1) {
1335 int net_client_parse(QemuOptsList
*opts_list
, const char *optarg
)
1337 #if defined(CONFIG_SLIRP)
1339 if (net_slirp_parse_legacy(opts_list
, optarg
, &ret
)) {
1344 if (!qemu_opts_parse(opts_list
, optarg
, 1)) {
1354 unsigned compute_mcast_idx(const uint8_t *ep
)
1361 for (i
= 0; i
< 6; i
++) {
1363 for (j
= 0; j
< 8; j
++) {
1364 carry
= ((crc
& 0x80000000L
) ? 1 : 0) ^ (b
& 0x01);
1368 crc
= ((crc
^ POLYNOMIAL
) | carry
);
1375 QemuOptsList qemu_netdev_opts
= {
1377 .implied_opt_name
= "type",
1378 .head
= QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts
.head
),
1381 * no elements => accept any params
1382 * validation will happen later
1384 { /* end of list */ }
1388 QemuOptsList qemu_net_opts
= {
1390 .implied_opt_name
= "type",
1391 .head
= QTAILQ_HEAD_INITIALIZER(qemu_net_opts
.head
),
1394 * no elements => accept any params
1395 * validation will happen later
1397 { /* end of list */ }