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 void qemu_format_nic_info_str(NetClientState
*nc
, uint8_t macaddr
[6])
156 snprintf(nc
->info_str
, sizeof(nc
->info_str
),
157 "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
159 macaddr
[0], macaddr
[1], macaddr
[2],
160 macaddr
[3], macaddr
[4], macaddr
[5]);
163 void qemu_macaddr_default_if_unset(MACAddr
*macaddr
)
165 static int index
= 0;
166 static const MACAddr zero
= { .a
= { 0,0,0,0,0,0 } };
168 if (memcmp(macaddr
, &zero
, sizeof(zero
)) != 0)
170 macaddr
->a
[0] = 0x52;
171 macaddr
->a
[1] = 0x54;
172 macaddr
->a
[2] = 0x00;
173 macaddr
->a
[3] = 0x12;
174 macaddr
->a
[4] = 0x34;
175 macaddr
->a
[5] = 0x56 + index
++;
179 * Generate a name for net client
181 * Only net clients created with the legacy -net option and NICs need this.
183 static char *assign_name(NetClientState
*nc1
, const char *model
)
188 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
192 if (strcmp(nc
->model
, model
) == 0) {
197 return g_strdup_printf("%s.%d", model
, id
);
200 static void qemu_net_client_destructor(NetClientState
*nc
)
205 static void qemu_net_client_setup(NetClientState
*nc
,
207 NetClientState
*peer
,
210 NetClientDestructor
*destructor
)
213 nc
->model
= g_strdup(model
);
215 nc
->name
= g_strdup(name
);
217 nc
->name
= assign_name(nc
, model
);
225 QTAILQ_INSERT_TAIL(&net_clients
, nc
, next
);
227 nc
->incoming_queue
= qemu_new_net_queue(nc
);
228 nc
->destructor
= destructor
;
231 NetClientState
*qemu_new_net_client(NetClientInfo
*info
,
232 NetClientState
*peer
,
238 assert(info
->size
>= sizeof(NetClientState
));
240 nc
= g_malloc0(info
->size
);
241 qemu_net_client_setup(nc
, info
, peer
, model
, name
,
242 qemu_net_client_destructor
);
247 NICState
*qemu_new_nic(NetClientInfo
*info
,
253 NetClientState
**peers
= conf
->peers
.ncs
;
255 int i
, queues
= MAX(1, conf
->peers
.queues
);
257 assert(info
->type
== NET_CLIENT_OPTIONS_KIND_NIC
);
258 assert(info
->size
>= sizeof(NICState
));
260 nic
= g_malloc0(info
->size
+ sizeof(NetClientState
) * queues
);
261 nic
->ncs
= (void *)nic
+ info
->size
;
263 nic
->opaque
= opaque
;
265 for (i
= 0; i
< queues
; i
++) {
266 qemu_net_client_setup(&nic
->ncs
[i
], info
, peers
[i
], model
, name
,
268 nic
->ncs
[i
].queue_index
= i
;
274 NetClientState
*qemu_get_subqueue(NICState
*nic
, int queue_index
)
276 return nic
->ncs
+ queue_index
;
279 NetClientState
*qemu_get_queue(NICState
*nic
)
281 return qemu_get_subqueue(nic
, 0);
284 NICState
*qemu_get_nic(NetClientState
*nc
)
286 NetClientState
*nc0
= nc
- nc
->queue_index
;
288 return (NICState
*)((void *)nc0
- nc
->info
->size
);
291 void *qemu_get_nic_opaque(NetClientState
*nc
)
293 NICState
*nic
= qemu_get_nic(nc
);
298 static void qemu_cleanup_net_client(NetClientState
*nc
)
300 QTAILQ_REMOVE(&net_clients
, nc
, next
);
302 if (nc
->info
->cleanup
) {
303 nc
->info
->cleanup(nc
);
307 static void qemu_free_net_client(NetClientState
*nc
)
309 if (nc
->incoming_queue
) {
310 qemu_del_net_queue(nc
->incoming_queue
);
313 nc
->peer
->peer
= NULL
;
317 if (nc
->destructor
) {
322 void qemu_del_net_client(NetClientState
*nc
)
324 NetClientState
*ncs
[MAX_QUEUE_NUM
];
327 /* If the NetClientState belongs to a multiqueue backend, we will change all
328 * other NetClientStates also.
330 queues
= qemu_find_net_clients_except(nc
->name
, ncs
,
331 NET_CLIENT_OPTIONS_KIND_NIC
,
335 /* If there is a peer NIC, delete and cleanup client, but do not free. */
336 if (nc
->peer
&& nc
->peer
->info
->type
== NET_CLIENT_OPTIONS_KIND_NIC
) {
337 NICState
*nic
= qemu_get_nic(nc
->peer
);
338 if (nic
->peer_deleted
) {
341 nic
->peer_deleted
= true;
343 for (i
= 0; i
< queues
; i
++) {
344 ncs
[i
]->peer
->link_down
= true;
347 if (nc
->peer
->info
->link_status_changed
) {
348 nc
->peer
->info
->link_status_changed(nc
->peer
);
351 for (i
= 0; i
< queues
; i
++) {
352 qemu_cleanup_net_client(ncs
[i
]);
358 assert(nc
->info
->type
!= NET_CLIENT_OPTIONS_KIND_NIC
);
360 for (i
= 0; i
< queues
; i
++) {
361 qemu_cleanup_net_client(ncs
[i
]);
362 qemu_free_net_client(ncs
[i
]);
366 void qemu_del_nic(NICState
*nic
)
368 int i
, queues
= MAX(nic
->conf
->peers
.queues
, 1);
370 /* If this is a peer NIC and peer has already been deleted, free it now. */
371 if (nic
->peer_deleted
) {
372 for (i
= 0; i
< queues
; i
++) {
373 qemu_free_net_client(qemu_get_subqueue(nic
, i
)->peer
);
377 for (i
= queues
- 1; i
>= 0; i
--) {
378 NetClientState
*nc
= qemu_get_subqueue(nic
, i
);
380 qemu_cleanup_net_client(nc
);
381 qemu_free_net_client(nc
);
387 void qemu_foreach_nic(qemu_nic_foreach func
, void *opaque
)
391 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
392 if (nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_NIC
) {
393 if (nc
->queue_index
== 0) {
394 func(qemu_get_nic(nc
), opaque
);
400 bool qemu_has_ufo(NetClientState
*nc
)
402 if (!nc
|| !nc
->info
->has_ufo
) {
406 return nc
->info
->has_ufo(nc
);
409 bool qemu_has_vnet_hdr(NetClientState
*nc
)
411 if (!nc
|| !nc
->info
->has_vnet_hdr
) {
415 return nc
->info
->has_vnet_hdr(nc
);
418 bool qemu_has_vnet_hdr_len(NetClientState
*nc
, int len
)
420 if (!nc
|| !nc
->info
->has_vnet_hdr_len
) {
424 return nc
->info
->has_vnet_hdr_len(nc
, len
);
427 void qemu_using_vnet_hdr(NetClientState
*nc
, bool enable
)
429 if (!nc
|| !nc
->info
->using_vnet_hdr
) {
433 nc
->info
->using_vnet_hdr(nc
, enable
);
436 void qemu_set_offload(NetClientState
*nc
, int csum
, int tso4
, int tso6
,
439 if (!nc
|| !nc
->info
->set_offload
) {
443 nc
->info
->set_offload(nc
, csum
, tso4
, tso6
, ecn
, ufo
);
446 void qemu_set_vnet_hdr_len(NetClientState
*nc
, int len
)
448 if (!nc
|| !nc
->info
->set_vnet_hdr_len
) {
452 nc
->info
->set_vnet_hdr_len(nc
, len
);
455 int qemu_can_send_packet(NetClientState
*sender
)
457 int vm_running
= runstate_is_running();
467 if (sender
->peer
->receive_disabled
) {
469 } else if (sender
->peer
->info
->can_receive
&&
470 !sender
->peer
->info
->can_receive(sender
->peer
)) {
476 ssize_t
qemu_deliver_packet(NetClientState
*sender
,
482 NetClientState
*nc
= opaque
;
489 if (nc
->receive_disabled
) {
493 if (flags
& QEMU_NET_PACKET_FLAG_RAW
&& nc
->info
->receive_raw
) {
494 ret
= nc
->info
->receive_raw(nc
, data
, size
);
496 ret
= nc
->info
->receive(nc
, data
, size
);
500 nc
->receive_disabled
= 1;
506 void qemu_purge_queued_packets(NetClientState
*nc
)
512 qemu_net_queue_purge(nc
->peer
->incoming_queue
, nc
);
516 void qemu_flush_or_purge_queued_packets(NetClientState
*nc
, bool purge
)
518 nc
->receive_disabled
= 0;
520 if (nc
->peer
&& nc
->peer
->info
->type
== NET_CLIENT_OPTIONS_KIND_HUBPORT
) {
521 if (net_hub_flush(nc
->peer
)) {
525 if (qemu_net_queue_flush(nc
->incoming_queue
)) {
526 /* We emptied the queue successfully, signal to the IO thread to repoll
527 * the file descriptor (for tap, for example).
531 /* Unable to empty the queue, purge remaining packets */
532 qemu_net_queue_purge(nc
->incoming_queue
, nc
);
536 void qemu_flush_queued_packets(NetClientState
*nc
)
538 qemu_flush_or_purge_queued_packets(nc
, false);
541 static ssize_t
qemu_send_packet_async_with_flags(NetClientState
*sender
,
543 const uint8_t *buf
, int size
,
544 NetPacketSent
*sent_cb
)
549 printf("qemu_send_packet_async:\n");
550 hex_dump(stdout
, buf
, size
);
553 if (sender
->link_down
|| !sender
->peer
) {
557 queue
= sender
->peer
->incoming_queue
;
559 return qemu_net_queue_send(queue
, sender
, flags
, buf
, size
, sent_cb
);
562 ssize_t
qemu_send_packet_async(NetClientState
*sender
,
563 const uint8_t *buf
, int size
,
564 NetPacketSent
*sent_cb
)
566 return qemu_send_packet_async_with_flags(sender
, QEMU_NET_PACKET_FLAG_NONE
,
570 void qemu_send_packet(NetClientState
*nc
, const uint8_t *buf
, int size
)
572 qemu_send_packet_async(nc
, buf
, size
, NULL
);
575 ssize_t
qemu_send_packet_raw(NetClientState
*nc
, const uint8_t *buf
, int size
)
577 return qemu_send_packet_async_with_flags(nc
, QEMU_NET_PACKET_FLAG_RAW
,
581 static ssize_t
nc_sendv_compat(NetClientState
*nc
, const struct iovec
*iov
,
584 uint8_t buffer
[NET_BUFSIZE
];
587 offset
= iov_to_buf(iov
, iovcnt
, 0, buffer
, sizeof(buffer
));
589 return nc
->info
->receive(nc
, buffer
, offset
);
592 ssize_t
qemu_deliver_packet_iov(NetClientState
*sender
,
594 const struct iovec
*iov
,
598 NetClientState
*nc
= opaque
;
602 return iov_size(iov
, iovcnt
);
605 if (nc
->receive_disabled
) {
609 if (nc
->info
->receive_iov
) {
610 ret
= nc
->info
->receive_iov(nc
, iov
, iovcnt
);
612 ret
= nc_sendv_compat(nc
, iov
, iovcnt
);
616 nc
->receive_disabled
= 1;
622 ssize_t
qemu_sendv_packet_async(NetClientState
*sender
,
623 const struct iovec
*iov
, int iovcnt
,
624 NetPacketSent
*sent_cb
)
628 if (sender
->link_down
|| !sender
->peer
) {
629 return iov_size(iov
, iovcnt
);
632 queue
= sender
->peer
->incoming_queue
;
634 return qemu_net_queue_send_iov(queue
, sender
,
635 QEMU_NET_PACKET_FLAG_NONE
,
636 iov
, iovcnt
, sent_cb
);
640 qemu_sendv_packet(NetClientState
*nc
, const struct iovec
*iov
, int iovcnt
)
642 return qemu_sendv_packet_async(nc
, iov
, iovcnt
, NULL
);
645 NetClientState
*qemu_find_netdev(const char *id
)
649 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
650 if (nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_NIC
)
652 if (!strcmp(nc
->name
, id
)) {
660 int qemu_find_net_clients_except(const char *id
, NetClientState
**ncs
,
661 NetClientOptionsKind type
, int max
)
666 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
667 if (nc
->info
->type
== type
) {
670 if (!id
|| !strcmp(nc
->name
, id
)) {
681 static int nic_get_free_idx(void)
685 for (index
= 0; index
< MAX_NICS
; index
++)
686 if (!nd_table
[index
].used
)
691 int qemu_show_nic_models(const char *arg
, const char *const *models
)
695 if (!arg
|| !is_help_option(arg
)) {
699 fprintf(stderr
, "qemu: Supported NIC models: ");
700 for (i
= 0 ; models
[i
]; i
++)
701 fprintf(stderr
, "%s%c", models
[i
], models
[i
+1] ? ',' : '\n');
705 void qemu_check_nic_model(NICInfo
*nd
, const char *model
)
707 const char *models
[2];
712 if (qemu_show_nic_models(nd
->model
, models
))
714 if (qemu_find_nic_model(nd
, models
, model
) < 0)
718 int qemu_find_nic_model(NICInfo
*nd
, const char * const *models
,
719 const char *default_model
)
724 nd
->model
= g_strdup(default_model
);
726 for (i
= 0 ; models
[i
]; i
++) {
727 if (strcmp(nd
->model
, models
[i
]) == 0)
731 error_report("Unsupported NIC model: %s", nd
->model
);
735 static int net_init_nic(const NetClientOptions
*opts
, const char *name
,
736 NetClientState
*peer
)
740 const NetLegacyNicOptions
*nic
;
742 assert(opts
->kind
== NET_CLIENT_OPTIONS_KIND_NIC
);
745 idx
= nic_get_free_idx();
746 if (idx
== -1 || nb_nics
>= MAX_NICS
) {
747 error_report("Too Many NICs");
753 memset(nd
, 0, sizeof(*nd
));
755 if (nic
->has_netdev
) {
756 nd
->netdev
= qemu_find_netdev(nic
->netdev
);
758 error_report("netdev '%s' not found", nic
->netdev
);
765 nd
->name
= g_strdup(name
);
766 if (nic
->has_model
) {
767 nd
->model
= g_strdup(nic
->model
);
770 nd
->devaddr
= g_strdup(nic
->addr
);
773 if (nic
->has_macaddr
&&
774 net_parse_macaddr(nd
->macaddr
.a
, nic
->macaddr
) < 0) {
775 error_report("invalid syntax for ethernet address");
778 if (nic
->has_macaddr
&&
779 is_multicast_ether_addr(nd
->macaddr
.a
)) {
780 error_report("NIC cannot have multicast MAC address (odd 1st byte)");
783 qemu_macaddr_default_if_unset(&nd
->macaddr
);
785 if (nic
->has_vectors
) {
786 if (nic
->vectors
> 0x7ffffff) {
787 error_report("invalid # of vectors: %"PRIu32
, nic
->vectors
);
790 nd
->nvectors
= nic
->vectors
;
792 nd
->nvectors
= DEV_NVECTORS_UNSPECIFIED
;
802 static int (* const net_client_init_fun
[NET_CLIENT_OPTIONS_KIND_MAX
])(
803 const NetClientOptions
*opts
,
805 NetClientState
*peer
) = {
806 [NET_CLIENT_OPTIONS_KIND_NIC
] = net_init_nic
,
808 [NET_CLIENT_OPTIONS_KIND_USER
] = net_init_slirp
,
810 [NET_CLIENT_OPTIONS_KIND_TAP
] = net_init_tap
,
811 [NET_CLIENT_OPTIONS_KIND_SOCKET
] = net_init_socket
,
813 [NET_CLIENT_OPTIONS_KIND_VDE
] = net_init_vde
,
816 [NET_CLIENT_OPTIONS_KIND_NETMAP
] = net_init_netmap
,
818 [NET_CLIENT_OPTIONS_KIND_DUMP
] = net_init_dump
,
819 #ifdef CONFIG_NET_BRIDGE
820 [NET_CLIENT_OPTIONS_KIND_BRIDGE
] = net_init_bridge
,
822 [NET_CLIENT_OPTIONS_KIND_HUBPORT
] = net_init_hubport
,
823 #ifdef CONFIG_VHOST_NET_USED
824 [NET_CLIENT_OPTIONS_KIND_VHOST_USER
] = net_init_vhost_user
,
827 [NET_CLIENT_OPTIONS_KIND_L2TPV3
] = net_init_l2tpv3
,
832 static int net_client_init1(const void *object
, int is_netdev
, Error
**errp
)
835 const Netdev
*netdev
;
836 const NetLegacy
*net
;
838 const NetClientOptions
*opts
;
843 opts
= u
.netdev
->opts
;
846 switch (opts
->kind
) {
848 case NET_CLIENT_OPTIONS_KIND_USER
:
850 case NET_CLIENT_OPTIONS_KIND_TAP
:
851 case NET_CLIENT_OPTIONS_KIND_SOCKET
:
853 case NET_CLIENT_OPTIONS_KIND_VDE
:
856 case NET_CLIENT_OPTIONS_KIND_NETMAP
:
858 #ifdef CONFIG_NET_BRIDGE
859 case NET_CLIENT_OPTIONS_KIND_BRIDGE
:
861 case NET_CLIENT_OPTIONS_KIND_HUBPORT
:
862 #ifdef CONFIG_VHOST_NET_USED
863 case NET_CLIENT_OPTIONS_KIND_VHOST_USER
:
866 case NET_CLIENT_OPTIONS_KIND_L2TPV3
:
871 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, "type",
872 "a netdev backend type");
878 /* missing optional values have been initialized to "all bits zero" */
879 name
= u
.net
->has_id
? u
.net
->id
: u
.net
->name
;
882 if (net_client_init_fun
[opts
->kind
]) {
883 NetClientState
*peer
= NULL
;
885 /* Do not add to a vlan if it's a -netdev or a nic with a netdev=
888 (opts
->kind
!= NET_CLIENT_OPTIONS_KIND_NIC
||
889 !opts
->nic
->has_netdev
)) {
890 peer
= net_hub_add_port(u
.net
->has_vlan
? u
.net
->vlan
: 0, NULL
);
893 if (net_client_init_fun
[opts
->kind
](opts
, name
, peer
) < 0) {
894 /* TODO push error reporting into init() methods */
895 error_set(errp
, QERR_DEVICE_INIT_FAILED
,
896 NetClientOptionsKind_lookup
[opts
->kind
]);
904 static void net_visit(Visitor
*v
, int is_netdev
, void **object
, Error
**errp
)
907 visit_type_Netdev(v
, (Netdev
**)object
, NULL
, errp
);
909 visit_type_NetLegacy(v
, (NetLegacy
**)object
, NULL
, errp
);
914 int net_client_init(QemuOpts
*opts
, int is_netdev
, Error
**errp
)
921 OptsVisitor
*ov
= opts_visitor_new(opts
);
923 net_visit(opts_get_visitor(ov
), is_netdev
, &object
, &err
);
924 opts_visitor_cleanup(ov
);
928 ret
= net_client_init1(object
, is_netdev
, &err
);
932 QapiDeallocVisitor
*dv
= qapi_dealloc_visitor_new();
934 net_visit(qapi_dealloc_get_visitor(dv
), is_netdev
, &object
, NULL
);
935 qapi_dealloc_visitor_cleanup(dv
);
938 error_propagate(errp
, err
);
943 static int net_host_check_device(const char *device
)
946 for (i
= 0; host_net_devices
[i
]; i
++) {
947 if (!strncmp(host_net_devices
[i
], device
,
948 strlen(host_net_devices
[i
]))) {
956 void hmp_host_net_add(Monitor
*mon
, const QDict
*qdict
)
958 const char *device
= qdict_get_str(qdict
, "device");
959 const char *opts_str
= qdict_get_try_str(qdict
, "opts");
960 Error
*local_err
= NULL
;
963 if (!net_host_check_device(device
)) {
964 monitor_printf(mon
, "invalid host network device %s\n", device
);
968 opts
= qemu_opts_parse(qemu_find_opts("net"), opts_str
? opts_str
: "", 0);
973 qemu_opt_set(opts
, "type", device
);
975 net_client_init(opts
, 0, &local_err
);
977 qerror_report_err(local_err
);
978 error_free(local_err
);
979 monitor_printf(mon
, "adding host network device %s failed\n", device
);
983 void hmp_host_net_remove(Monitor
*mon
, const QDict
*qdict
)
986 int vlan_id
= qdict_get_int(qdict
, "vlan_id");
987 const char *device
= qdict_get_str(qdict
, "device");
989 nc
= net_hub_find_client_by_name(vlan_id
, device
);
991 error_report("Host network device '%s' on hub '%d' not found",
995 if (!net_host_check_device(nc
->model
)) {
996 error_report("invalid host network device '%s'", device
);
1000 qemu_del_net_client(nc
->peer
);
1001 qemu_del_net_client(nc
);
1004 void netdev_add(QemuOpts
*opts
, Error
**errp
)
1006 net_client_init(opts
, 1, errp
);
1009 int qmp_netdev_add(Monitor
*mon
, const QDict
*qdict
, QObject
**ret
)
1011 Error
*local_err
= NULL
;
1012 QemuOptsList
*opts_list
;
1015 opts_list
= qemu_find_opts_err("netdev", &local_err
);
1020 opts
= qemu_opts_from_qdict(opts_list
, qdict
, &local_err
);
1025 netdev_add(opts
, &local_err
);
1027 qemu_opts_del(opts
);
1034 qerror_report_err(local_err
);
1035 error_free(local_err
);
1039 void qmp_netdev_del(const char *id
, Error
**errp
)
1044 nc
= qemu_find_netdev(id
);
1046 error_set(errp
, QERR_DEVICE_NOT_FOUND
, id
);
1050 opts
= qemu_opts_find(qemu_find_opts_err("netdev", NULL
), id
);
1052 error_setg(errp
, "Device '%s' is not a netdev", id
);
1056 qemu_del_net_client(nc
);
1057 qemu_opts_del(opts
);
1060 void print_net_client(Monitor
*mon
, NetClientState
*nc
)
1062 monitor_printf(mon
, "%s: index=%d,type=%s,%s\n", nc
->name
,
1064 NetClientOptionsKind_lookup
[nc
->info
->type
],
1068 RxFilterInfoList
*qmp_query_rx_filter(bool has_name
, const char *name
,
1072 RxFilterInfoList
*filter_list
= NULL
, *last_entry
= NULL
;
1074 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
1075 RxFilterInfoList
*entry
;
1078 if (has_name
&& strcmp(nc
->name
, name
) != 0) {
1082 /* only query rx-filter information of NIC */
1083 if (nc
->info
->type
!= NET_CLIENT_OPTIONS_KIND_NIC
) {
1085 error_setg(errp
, "net client(%s) isn't a NIC", name
);
1091 if (nc
->info
->query_rx_filter
) {
1092 info
= nc
->info
->query_rx_filter(nc
);
1093 entry
= g_malloc0(sizeof(*entry
));
1094 entry
->value
= info
;
1097 filter_list
= entry
;
1099 last_entry
->next
= entry
;
1102 } else if (has_name
) {
1103 error_setg(errp
, "net client(%s) doesn't support"
1104 " rx-filter querying", name
);
1113 if (filter_list
== NULL
&& has_name
) {
1114 error_setg(errp
, "invalid net client name: %s", name
);
1120 void hmp_info_network(Monitor
*mon
, const QDict
*qdict
)
1122 NetClientState
*nc
, *peer
;
1123 NetClientOptionsKind type
;
1127 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
1129 type
= nc
->info
->type
;
1131 /* Skip if already printed in hub info */
1132 if (net_hub_id_for_client(nc
, NULL
) == 0) {
1136 if (!peer
|| type
== NET_CLIENT_OPTIONS_KIND_NIC
) {
1137 print_net_client(mon
, nc
);
1138 } /* else it's a netdev connected to a NIC, printed with the NIC */
1139 if (peer
&& type
== NET_CLIENT_OPTIONS_KIND_NIC
) {
1140 monitor_printf(mon
, " \\ ");
1141 print_net_client(mon
, peer
);
1146 void qmp_set_link(const char *name
, bool up
, Error
**errp
)
1148 NetClientState
*ncs
[MAX_QUEUE_NUM
];
1152 queues
= qemu_find_net_clients_except(name
, ncs
,
1153 NET_CLIENT_OPTIONS_KIND_MAX
,
1157 error_set(errp
, QERR_DEVICE_NOT_FOUND
, name
);
1162 for (i
= 0; i
< queues
; i
++) {
1163 ncs
[i
]->link_down
= !up
;
1166 if (nc
->info
->link_status_changed
) {
1167 nc
->info
->link_status_changed(nc
);
1171 /* Change peer link only if the peer is NIC and then notify peer.
1172 * If the peer is a HUBPORT or a backend, we do not change the
1175 * This behavior is compatible with qemu vlans where there could be
1176 * multiple clients that can still communicate with each other in
1177 * disconnected mode. For now maintain this compatibility.
1179 if (nc
->peer
->info
->type
== NET_CLIENT_OPTIONS_KIND_NIC
) {
1180 for (i
= 0; i
< queues
; i
++) {
1181 ncs
[i
]->peer
->link_down
= !up
;
1184 if (nc
->peer
->info
->link_status_changed
) {
1185 nc
->peer
->info
->link_status_changed(nc
->peer
);
1190 static void net_vm_change_state_handler(void *opaque
, int running
,
1193 /* Complete all queued packets, to guarantee we don't modify
1194 * state later when VM is not running.
1198 NetClientState
*tmp
;
1200 QTAILQ_FOREACH_SAFE(nc
, &net_clients
, next
, tmp
) {
1201 qemu_flush_or_purge_queued_packets(nc
, true);
1206 void net_cleanup(void)
1210 /* We may del multiple entries during qemu_del_net_client(),
1211 * so QTAILQ_FOREACH_SAFE() is also not safe here.
1213 while (!QTAILQ_EMPTY(&net_clients
)) {
1214 nc
= QTAILQ_FIRST(&net_clients
);
1215 if (nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_NIC
) {
1216 qemu_del_nic(qemu_get_nic(nc
));
1218 qemu_del_net_client(nc
);
1222 qemu_del_vm_change_state_handler(net_change_state_entry
);
1225 void net_check_clients(void)
1230 /* Don't warn about the default network setup that you get if
1231 * no command line -net or -netdev options are specified. There
1232 * are two cases that we would otherwise complain about:
1233 * (1) board doesn't support a NIC but the implicit "-net nic"
1235 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
1236 * sets up a nic that isn't connected to anything.
1242 net_hub_check_clients();
1244 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
1246 fprintf(stderr
, "Warning: %s %s has no peer\n",
1247 nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_NIC
?
1248 "nic" : "netdev", nc
->name
);
1252 /* Check that all NICs requested via -net nic actually got created.
1253 * NICs created via -device don't need to be checked here because
1254 * they are always instantiated.
1256 for (i
= 0; i
< MAX_NICS
; i
++) {
1257 NICInfo
*nd
= &nd_table
[i
];
1258 if (nd
->used
&& !nd
->instantiated
) {
1259 fprintf(stderr
, "Warning: requested NIC (%s, model %s) "
1260 "was not created (not supported by this machine?)\n",
1261 nd
->name
? nd
->name
: "anonymous",
1262 nd
->model
? nd
->model
: "unspecified");
1267 static int net_init_client(QemuOpts
*opts
, void *dummy
)
1269 Error
*local_err
= NULL
;
1271 net_client_init(opts
, 0, &local_err
);
1273 qerror_report_err(local_err
);
1274 error_free(local_err
);
1281 static int net_init_netdev(QemuOpts
*opts
, void *dummy
)
1283 Error
*local_err
= NULL
;
1286 ret
= net_client_init(opts
, 1, &local_err
);
1288 qerror_report_err(local_err
);
1289 error_free(local_err
);
1296 int net_init_clients(void)
1298 QemuOptsList
*net
= qemu_find_opts("net");
1301 /* if no clients, we use a default config */
1302 qemu_opts_set(net
, NULL
, "type", "nic");
1304 qemu_opts_set(net
, NULL
, "type", "user");
1308 net_change_state_entry
=
1309 qemu_add_vm_change_state_handler(net_vm_change_state_handler
, NULL
);
1311 QTAILQ_INIT(&net_clients
);
1313 if (qemu_opts_foreach(qemu_find_opts("netdev"), net_init_netdev
, NULL
, 1) == -1)
1316 if (qemu_opts_foreach(net
, net_init_client
, NULL
, 1) == -1) {
1323 int net_client_parse(QemuOptsList
*opts_list
, const char *optarg
)
1325 #if defined(CONFIG_SLIRP)
1327 if (net_slirp_parse_legacy(opts_list
, optarg
, &ret
)) {
1332 if (!qemu_opts_parse(opts_list
, optarg
, 1)) {
1342 unsigned compute_mcast_idx(const uint8_t *ep
)
1349 for (i
= 0; i
< 6; i
++) {
1351 for (j
= 0; j
< 8; j
++) {
1352 carry
= ((crc
& 0x80000000L
) ? 1 : 0) ^ (b
& 0x01);
1356 crc
= ((crc
^ POLYNOMIAL
) | carry
);
1363 QemuOptsList qemu_netdev_opts
= {
1365 .implied_opt_name
= "type",
1366 .head
= QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts
.head
),
1369 * no elements => accept any params
1370 * validation will happen later
1372 { /* end of list */ }
1376 QemuOptsList qemu_net_opts
= {
1378 .implied_opt_name
= "type",
1379 .head
= QTAILQ_HEAD_INITIALIZER(qemu_net_opts
.head
),
1382 * no elements => accept any params
1383 * validation will happen later
1385 { /* end of list */ }