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
26 #include "config-host.h"
29 #include "net/socket.h"
31 #include "net/slirp.h"
36 #include "qemu-common.h"
37 #include "qemu_socket.h"
38 #include "qmp-commands.h"
41 #include "qapi-visit.h"
42 #include "qapi/opts-visitor.h"
43 #include "qapi/qapi-dealloc-visitor.h"
45 /* Net bridge is currently not supported for W32. */
47 # define CONFIG_NET_BRIDGE
50 static QTAILQ_HEAD(, VLANState
) vlans
;
51 static QTAILQ_HEAD(, VLANClientState
) non_vlan_clients
;
55 /***********************************************************/
56 /* network device redirectors */
58 #if defined(DEBUG_NET)
59 static void hex_dump(FILE *f
, const uint8_t *buf
, int size
)
63 for(i
=0;i
<size
;i
+=16) {
67 fprintf(f
, "%08x ", i
);
70 fprintf(f
, " %02x", buf
[i
+j
]);
77 if (c
< ' ' || c
> '~')
86 static int get_str_sep(char *buf
, int buf_size
, const char **pp
, int sep
)
97 if (len
> buf_size
- 1)
106 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
)
114 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
116 saddr
->sin_family
= AF_INET
;
117 if (buf
[0] == '\0') {
118 saddr
->sin_addr
.s_addr
= 0;
120 if (qemu_isdigit(buf
[0])) {
121 if (!inet_aton(buf
, &saddr
->sin_addr
))
124 if ((he
= gethostbyname(buf
)) == NULL
)
126 saddr
->sin_addr
= *(struct in_addr
*)he
->h_addr
;
129 port
= strtol(p
, (char **)&r
, 0);
132 saddr
->sin_port
= htons(port
);
136 void qemu_format_nic_info_str(VLANClientState
*vc
, uint8_t macaddr
[6])
138 snprintf(vc
->info_str
, sizeof(vc
->info_str
),
139 "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
141 macaddr
[0], macaddr
[1], macaddr
[2],
142 macaddr
[3], macaddr
[4], macaddr
[5]);
145 void qemu_macaddr_default_if_unset(MACAddr
*macaddr
)
147 static int index
= 0;
148 static const MACAddr zero
= { .a
= { 0,0,0,0,0,0 } };
150 if (memcmp(macaddr
, &zero
, sizeof(zero
)) != 0)
152 macaddr
->a
[0] = 0x52;
153 macaddr
->a
[1] = 0x54;
154 macaddr
->a
[2] = 0x00;
155 macaddr
->a
[3] = 0x12;
156 macaddr
->a
[4] = 0x34;
157 macaddr
->a
[5] = 0x56 + index
++;
161 * Generate a name for net client
163 * Only net clients created with the legacy -net option need this. Naming is
164 * mandatory for net clients created with -netdev.
166 static char *assign_name(VLANClientState
*vc1
, const char *model
)
172 QTAILQ_FOREACH(vc
, &non_vlan_clients
, next
) {
176 /* For compatibility only bump id for net clients on a vlan */
177 if (strcmp(vc
->model
, model
) == 0 &&
178 net_hub_id_for_client(vc
, NULL
) == 0) {
183 snprintf(buf
, sizeof(buf
), "%s.%d", model
, id
);
185 return g_strdup(buf
);
188 static ssize_t
qemu_deliver_packet(VLANClientState
*sender
,
193 static ssize_t
qemu_deliver_packet_iov(VLANClientState
*sender
,
195 const struct iovec
*iov
,
199 VLANClientState
*qemu_new_net_client(NetClientInfo
*info
,
200 VLANClientState
*peer
,
206 assert(info
->size
>= sizeof(VLANClientState
));
208 vc
= g_malloc0(info
->size
);
211 vc
->model
= g_strdup(model
);
213 vc
->name
= g_strdup(name
);
215 vc
->name
= assign_name(vc
, model
);
223 QTAILQ_INSERT_TAIL(&non_vlan_clients
, vc
, next
);
225 vc
->send_queue
= qemu_new_net_queue(qemu_deliver_packet
,
226 qemu_deliver_packet_iov
,
232 NICState
*qemu_new_nic(NetClientInfo
*info
,
241 assert(info
->type
== NET_CLIENT_OPTIONS_KIND_NIC
);
242 assert(info
->size
>= sizeof(NICState
));
244 nc
= qemu_new_net_client(info
, conf
->peer
, model
, name
);
246 nic
= DO_UPCAST(NICState
, nc
, nc
);
248 nic
->opaque
= opaque
;
253 static void qemu_cleanup_vlan_client(VLANClientState
*vc
)
256 QTAILQ_REMOVE(&vc
->vlan
->clients
, vc
, next
);
258 QTAILQ_REMOVE(&non_vlan_clients
, vc
, next
);
261 if (vc
->info
->cleanup
) {
262 vc
->info
->cleanup(vc
);
266 static void qemu_free_vlan_client(VLANClientState
*vc
)
269 if (vc
->send_queue
) {
270 qemu_del_net_queue(vc
->send_queue
);
273 vc
->peer
->peer
= NULL
;
281 void qemu_del_vlan_client(VLANClientState
*vc
)
283 /* If there is a peer NIC, delete and cleanup client, but do not free. */
284 if (!vc
->vlan
&& vc
->peer
&& vc
->peer
->info
->type
== NET_CLIENT_OPTIONS_KIND_NIC
) {
285 NICState
*nic
= DO_UPCAST(NICState
, nc
, vc
->peer
);
286 if (nic
->peer_deleted
) {
289 nic
->peer_deleted
= true;
290 /* Let NIC know peer is gone. */
291 vc
->peer
->link_down
= true;
292 if (vc
->peer
->info
->link_status_changed
) {
293 vc
->peer
->info
->link_status_changed(vc
->peer
);
295 qemu_cleanup_vlan_client(vc
);
299 /* If this is a peer NIC and peer has already been deleted, free it now. */
300 if (!vc
->vlan
&& vc
->peer
&& vc
->info
->type
== NET_CLIENT_OPTIONS_KIND_NIC
) {
301 NICState
*nic
= DO_UPCAST(NICState
, nc
, vc
);
302 if (nic
->peer_deleted
) {
303 qemu_free_vlan_client(vc
->peer
);
307 qemu_cleanup_vlan_client(vc
);
308 qemu_free_vlan_client(vc
);
311 void qemu_foreach_nic(qemu_nic_foreach func
, void *opaque
)
316 QTAILQ_FOREACH(nc
, &non_vlan_clients
, next
) {
317 if (nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_NIC
) {
318 func(DO_UPCAST(NICState
, nc
, nc
), opaque
);
322 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
323 QTAILQ_FOREACH(nc
, &vlan
->clients
, next
) {
324 if (nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_NIC
) {
325 func(DO_UPCAST(NICState
, nc
, nc
), opaque
);
331 int qemu_can_send_packet(VLANClientState
*sender
)
333 VLANState
*vlan
= sender
->vlan
;
337 if (sender
->peer
->receive_disabled
) {
339 } else if (sender
->peer
->info
->can_receive
&&
340 !sender
->peer
->info
->can_receive(sender
->peer
)) {
351 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
356 /* no can_receive() handler, they can always receive */
357 if (vc
->info
->can_receive
&& !vc
->info
->can_receive(vc
)) {
364 static ssize_t
qemu_deliver_packet(VLANClientState
*sender
,
370 VLANClientState
*vc
= opaque
;
377 if (vc
->receive_disabled
) {
381 if (flags
& QEMU_NET_PACKET_FLAG_RAW
&& vc
->info
->receive_raw
) {
382 ret
= vc
->info
->receive_raw(vc
, data
, size
);
384 ret
= vc
->info
->receive(vc
, data
, size
);
388 vc
->receive_disabled
= 1;
394 static ssize_t
qemu_vlan_deliver_packet(VLANClientState
*sender
,
400 VLANState
*vlan
= opaque
;
404 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
416 if (vc
->receive_disabled
) {
421 if (flags
& QEMU_NET_PACKET_FLAG_RAW
&& vc
->info
->receive_raw
) {
422 len
= vc
->info
->receive_raw(vc
, buf
, size
);
424 len
= vc
->info
->receive(vc
, buf
, size
);
428 vc
->receive_disabled
= 1;
431 ret
= (ret
>= 0) ? ret
: len
;
438 void qemu_purge_queued_packets(VLANClientState
*vc
)
442 if (!vc
->peer
&& !vc
->vlan
) {
447 queue
= vc
->peer
->send_queue
;
449 queue
= vc
->vlan
->send_queue
;
452 qemu_net_queue_purge(queue
, vc
);
455 void qemu_flush_queued_packets(VLANClientState
*vc
)
459 vc
->receive_disabled
= 0;
462 queue
= vc
->vlan
->send_queue
;
464 queue
= vc
->send_queue
;
467 qemu_net_queue_flush(queue
);
470 static ssize_t
qemu_send_packet_async_with_flags(VLANClientState
*sender
,
472 const uint8_t *buf
, int size
,
473 NetPacketSent
*sent_cb
)
478 printf("qemu_send_packet_async:\n");
479 hex_dump(stdout
, buf
, size
);
482 if (sender
->link_down
|| (!sender
->peer
&& !sender
->vlan
)) {
487 queue
= sender
->peer
->send_queue
;
489 queue
= sender
->vlan
->send_queue
;
492 return qemu_net_queue_send(queue
, sender
, flags
, buf
, size
, sent_cb
);
495 ssize_t
qemu_send_packet_async(VLANClientState
*sender
,
496 const uint8_t *buf
, int size
,
497 NetPacketSent
*sent_cb
)
499 return qemu_send_packet_async_with_flags(sender
, QEMU_NET_PACKET_FLAG_NONE
,
503 void qemu_send_packet(VLANClientState
*vc
, const uint8_t *buf
, int size
)
505 qemu_send_packet_async(vc
, buf
, size
, NULL
);
508 ssize_t
qemu_send_packet_raw(VLANClientState
*vc
, const uint8_t *buf
, int size
)
510 return qemu_send_packet_async_with_flags(vc
, QEMU_NET_PACKET_FLAG_RAW
,
514 static ssize_t
vc_sendv_compat(VLANClientState
*vc
, const struct iovec
*iov
,
517 uint8_t buffer
[4096];
520 offset
= iov_to_buf(iov
, iovcnt
, 0, buffer
, sizeof(buffer
));
522 return vc
->info
->receive(vc
, buffer
, offset
);
525 static ssize_t
qemu_deliver_packet_iov(VLANClientState
*sender
,
527 const struct iovec
*iov
,
531 VLANClientState
*vc
= opaque
;
534 return iov_size(iov
, iovcnt
);
537 if (vc
->info
->receive_iov
) {
538 return vc
->info
->receive_iov(vc
, iov
, iovcnt
);
540 return vc_sendv_compat(vc
, iov
, iovcnt
);
544 static ssize_t
qemu_vlan_deliver_packet_iov(VLANClientState
*sender
,
546 const struct iovec
*iov
,
550 VLANState
*vlan
= opaque
;
554 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
562 ret
= iov_size(iov
, iovcnt
);
566 assert(!(flags
& QEMU_NET_PACKET_FLAG_RAW
));
568 if (vc
->info
->receive_iov
) {
569 len
= vc
->info
->receive_iov(vc
, iov
, iovcnt
);
571 len
= vc_sendv_compat(vc
, iov
, iovcnt
);
574 ret
= (ret
>= 0) ? ret
: len
;
580 ssize_t
qemu_sendv_packet_async(VLANClientState
*sender
,
581 const struct iovec
*iov
, int iovcnt
,
582 NetPacketSent
*sent_cb
)
586 if (sender
->link_down
|| (!sender
->peer
&& !sender
->vlan
)) {
587 return iov_size(iov
, iovcnt
);
591 queue
= sender
->peer
->send_queue
;
593 queue
= sender
->vlan
->send_queue
;
596 return qemu_net_queue_send_iov(queue
, sender
,
597 QEMU_NET_PACKET_FLAG_NONE
,
598 iov
, iovcnt
, sent_cb
);
602 qemu_sendv_packet(VLANClientState
*vc
, const struct iovec
*iov
, int iovcnt
)
604 return qemu_sendv_packet_async(vc
, iov
, iovcnt
, NULL
);
607 /* find or alloc a new VLAN */
608 VLANState
*qemu_find_vlan(int id
, int allocate
)
612 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
613 if (vlan
->id
== id
) {
622 vlan
= g_malloc0(sizeof(VLANState
));
624 QTAILQ_INIT(&vlan
->clients
);
626 vlan
->send_queue
= qemu_new_net_queue(qemu_vlan_deliver_packet
,
627 qemu_vlan_deliver_packet_iov
,
630 QTAILQ_INSERT_TAIL(&vlans
, vlan
, next
);
635 VLANClientState
*qemu_find_netdev(const char *id
)
639 QTAILQ_FOREACH(vc
, &non_vlan_clients
, next
) {
640 if (vc
->info
->type
== NET_CLIENT_OPTIONS_KIND_NIC
)
642 if (!strcmp(vc
->name
, id
)) {
650 static int nic_get_free_idx(void)
654 for (index
= 0; index
< MAX_NICS
; index
++)
655 if (!nd_table
[index
].used
)
660 int qemu_show_nic_models(const char *arg
, const char *const *models
)
664 if (!arg
|| strcmp(arg
, "?"))
667 fprintf(stderr
, "qemu: Supported NIC models: ");
668 for (i
= 0 ; models
[i
]; i
++)
669 fprintf(stderr
, "%s%c", models
[i
], models
[i
+1] ? ',' : '\n');
673 void qemu_check_nic_model(NICInfo
*nd
, const char *model
)
675 const char *models
[2];
680 if (qemu_show_nic_models(nd
->model
, models
))
682 if (qemu_find_nic_model(nd
, models
, model
) < 0)
686 int qemu_find_nic_model(NICInfo
*nd
, const char * const *models
,
687 const char *default_model
)
692 nd
->model
= g_strdup(default_model
);
694 for (i
= 0 ; models
[i
]; i
++) {
695 if (strcmp(nd
->model
, models
[i
]) == 0)
699 error_report("Unsupported NIC model: %s", nd
->model
);
703 int net_handle_fd_param(Monitor
*mon
, const char *param
)
707 if (!qemu_isdigit(param
[0]) && mon
) {
709 fd
= monitor_get_fd(mon
, param
);
711 error_report("No file descriptor named %s found", param
);
715 fd
= qemu_parse_fd(param
);
721 static int net_init_nic(const NetClientOptions
*opts
, const char *name
,
722 VLANClientState
*peer
)
726 const NetLegacyNicOptions
*nic
;
728 assert(opts
->kind
== NET_CLIENT_OPTIONS_KIND_NIC
);
731 idx
= nic_get_free_idx();
732 if (idx
== -1 || nb_nics
>= MAX_NICS
) {
733 error_report("Too Many NICs");
739 memset(nd
, 0, sizeof(*nd
));
741 if (nic
->has_netdev
) {
742 nd
->netdev
= qemu_find_netdev(nic
->netdev
);
744 error_report("netdev '%s' not found", nic
->netdev
);
752 nd
->name
= g_strdup(name
);
754 if (nic
->has_model
) {
755 nd
->model
= g_strdup(nic
->model
);
758 nd
->devaddr
= g_strdup(nic
->addr
);
761 if (nic
->has_macaddr
&&
762 net_parse_macaddr(nd
->macaddr
.a
, nic
->macaddr
) < 0) {
763 error_report("invalid syntax for ethernet address");
766 qemu_macaddr_default_if_unset(&nd
->macaddr
);
768 if (nic
->has_vectors
) {
769 if (nic
->vectors
> 0x7ffffff) {
770 error_report("invalid # of vectors: %"PRIu32
, nic
->vectors
);
773 nd
->nvectors
= nic
->vectors
;
775 nd
->nvectors
= DEV_NVECTORS_UNSPECIFIED
;
785 static int (* const net_client_init_fun
[NET_CLIENT_OPTIONS_KIND_MAX
])(
786 const NetClientOptions
*opts
,
788 VLANClientState
*peer
) = {
789 [NET_CLIENT_OPTIONS_KIND_NIC
] = net_init_nic
,
791 [NET_CLIENT_OPTIONS_KIND_USER
] = net_init_slirp
,
793 [NET_CLIENT_OPTIONS_KIND_TAP
] = net_init_tap
,
794 [NET_CLIENT_OPTIONS_KIND_SOCKET
] = net_init_socket
,
796 [NET_CLIENT_OPTIONS_KIND_VDE
] = net_init_vde
,
798 [NET_CLIENT_OPTIONS_KIND_DUMP
] = net_init_dump
,
799 #ifdef CONFIG_NET_BRIDGE
800 [NET_CLIENT_OPTIONS_KIND_BRIDGE
] = net_init_bridge
,
802 [NET_CLIENT_OPTIONS_KIND_HUBPORT
] = net_init_hubport
,
806 static int net_client_init1(const void *object
, int is_netdev
, Error
**errp
)
809 const Netdev
*netdev
;
810 const NetLegacy
*net
;
812 const NetClientOptions
*opts
;
817 opts
= u
.netdev
->opts
;
820 switch (opts
->kind
) {
822 case NET_CLIENT_OPTIONS_KIND_USER
:
824 case NET_CLIENT_OPTIONS_KIND_TAP
:
825 case NET_CLIENT_OPTIONS_KIND_SOCKET
:
827 case NET_CLIENT_OPTIONS_KIND_VDE
:
829 #ifdef CONFIG_NET_BRIDGE
830 case NET_CLIENT_OPTIONS_KIND_BRIDGE
:
832 case NET_CLIENT_OPTIONS_KIND_HUBPORT
:
836 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, "type",
837 "a netdev backend type");
843 /* missing optional values have been initialized to "all bits zero" */
844 name
= u
.net
->has_id
? u
.net
->id
: u
.net
->name
;
847 if (net_client_init_fun
[opts
->kind
]) {
848 VLANClientState
*peer
= NULL
;
850 /* Do not add to a vlan if it's a -netdev or a nic with a netdev=
853 (opts
->kind
!= NET_CLIENT_OPTIONS_KIND_NIC
||
854 !opts
->nic
->has_netdev
)) {
855 peer
= net_hub_add_port(u
.net
->has_vlan
? u
.net
->vlan
: 0, NULL
);
858 if (net_client_init_fun
[opts
->kind
](opts
, name
, peer
) < 0) {
859 /* TODO push error reporting into init() methods */
860 error_set(errp
, QERR_DEVICE_INIT_FAILED
,
861 NetClientOptionsKind_lookup
[opts
->kind
]);
869 static void net_visit(Visitor
*v
, int is_netdev
, void **object
, Error
**errp
)
872 visit_type_Netdev(v
, (Netdev
**)object
, NULL
, errp
);
874 visit_type_NetLegacy(v
, (NetLegacy
**)object
, NULL
, errp
);
879 int net_client_init(QemuOpts
*opts
, int is_netdev
, Error
**errp
)
886 OptsVisitor
*ov
= opts_visitor_new(opts
);
888 net_visit(opts_get_visitor(ov
), is_netdev
, &object
, &err
);
889 opts_visitor_cleanup(ov
);
893 ret
= net_client_init1(object
, is_netdev
, &err
);
897 QapiDeallocVisitor
*dv
= qapi_dealloc_visitor_new();
899 net_visit(qapi_dealloc_get_visitor(dv
), is_netdev
, &object
, NULL
);
900 qapi_dealloc_visitor_cleanup(dv
);
903 error_propagate(errp
, err
);
908 static int net_host_check_device(const char *device
)
911 const char *valid_param_list
[] = { "tap", "socket", "dump"
912 #ifdef CONFIG_NET_BRIDGE
922 for (i
= 0; i
< sizeof(valid_param_list
) / sizeof(char *); i
++) {
923 if (!strncmp(valid_param_list
[i
], device
,
924 strlen(valid_param_list
[i
])))
931 void net_host_device_add(Monitor
*mon
, const QDict
*qdict
)
933 const char *device
= qdict_get_str(qdict
, "device");
934 const char *opts_str
= qdict_get_try_str(qdict
, "opts");
935 Error
*local_err
= NULL
;
938 if (!net_host_check_device(device
)) {
939 monitor_printf(mon
, "invalid host network device %s\n", device
);
943 opts
= qemu_opts_parse(qemu_find_opts("net"), opts_str
? opts_str
: "", 0);
948 qemu_opt_set(opts
, "type", device
);
950 net_client_init(opts
, 0, &local_err
);
951 if (error_is_set(&local_err
)) {
952 qerror_report_err(local_err
);
953 error_free(local_err
);
954 monitor_printf(mon
, "adding host network device %s failed\n", device
);
958 void net_host_device_remove(Monitor
*mon
, const QDict
*qdict
)
961 int vlan_id
= qdict_get_int(qdict
, "vlan_id");
962 const char *device
= qdict_get_str(qdict
, "device");
964 vc
= net_hub_find_client_by_name(vlan_id
, device
);
968 if (!net_host_check_device(vc
->model
)) {
969 monitor_printf(mon
, "invalid host network device %s\n", device
);
972 qemu_del_vlan_client(vc
);
975 void netdev_add(QemuOpts
*opts
, Error
**errp
)
977 net_client_init(opts
, 1, errp
);
980 int qmp_netdev_add(Monitor
*mon
, const QDict
*qdict
, QObject
**ret
)
982 Error
*local_err
= NULL
;
983 QemuOptsList
*opts_list
;
986 opts_list
= qemu_find_opts_err("netdev", &local_err
);
987 if (error_is_set(&local_err
)) {
991 opts
= qemu_opts_from_qdict(opts_list
, qdict
, &local_err
);
992 if (error_is_set(&local_err
)) {
996 netdev_add(opts
, &local_err
);
997 if (error_is_set(&local_err
)) {
1005 qerror_report_err(local_err
);
1006 error_free(local_err
);
1010 void qmp_netdev_del(const char *id
, Error
**errp
)
1012 VLANClientState
*vc
;
1014 vc
= qemu_find_netdev(id
);
1016 error_set(errp
, QERR_DEVICE_NOT_FOUND
, id
);
1020 qemu_del_vlan_client(vc
);
1021 qemu_opts_del(qemu_opts_find(qemu_find_opts_err("netdev", errp
), id
));
1024 static void print_net_client(Monitor
*mon
, VLANClientState
*vc
)
1026 monitor_printf(mon
, "%s: type=%s,%s\n", vc
->name
,
1027 NetClientOptionsKind_lookup
[vc
->info
->type
], vc
->info_str
);
1030 void do_info_network(Monitor
*mon
)
1033 VLANClientState
*vc
, *peer
;
1034 NetClientOptionsKind type
;
1036 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
1037 monitor_printf(mon
, "VLAN %d devices:\n", vlan
->id
);
1039 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
1040 monitor_printf(mon
, " ");
1041 print_net_client(mon
, vc
);
1044 monitor_printf(mon
, "Devices not on any VLAN:\n");
1045 QTAILQ_FOREACH(vc
, &non_vlan_clients
, next
) {
1047 type
= vc
->info
->type
;
1048 if (!peer
|| type
== NET_CLIENT_OPTIONS_KIND_NIC
) {
1049 monitor_printf(mon
, " ");
1050 print_net_client(mon
, vc
);
1051 } /* else it's a netdev connected to a NIC, printed with the NIC */
1052 if (peer
&& type
== NET_CLIENT_OPTIONS_KIND_NIC
) {
1053 monitor_printf(mon
, " \\ ");
1054 print_net_client(mon
, peer
);
1060 void qmp_set_link(const char *name
, bool up
, Error
**errp
)
1063 VLANClientState
*vc
= NULL
;
1065 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
1066 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
1067 if (strcmp(vc
->name
, name
) == 0) {
1072 QTAILQ_FOREACH(vc
, &non_vlan_clients
, next
) {
1073 if (!strcmp(vc
->name
, name
)) {
1080 error_set(errp
, QERR_DEVICE_NOT_FOUND
, name
);
1084 vc
->link_down
= !up
;
1086 if (vc
->info
->link_status_changed
) {
1087 vc
->info
->link_status_changed(vc
);
1090 /* Notify peer. Don't update peer link status: this makes it possible to
1091 * disconnect from host network without notifying the guest.
1092 * FIXME: is disconnected link status change operation useful?
1094 * Current behaviour is compatible with qemu vlans where there could be
1095 * multiple clients that can still communicate with each other in
1096 * disconnected mode. For now maintain this compatibility. */
1097 if (vc
->peer
&& vc
->peer
->info
->link_status_changed
) {
1098 vc
->peer
->info
->link_status_changed(vc
->peer
);
1102 void net_cleanup(void)
1105 VLANClientState
*vc
, *next_vc
;
1107 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
1108 QTAILQ_FOREACH_SAFE(vc
, &vlan
->clients
, next
, next_vc
) {
1109 qemu_del_vlan_client(vc
);
1113 QTAILQ_FOREACH_SAFE(vc
, &non_vlan_clients
, next
, next_vc
) {
1114 qemu_del_vlan_client(vc
);
1118 void net_check_clients(void)
1120 VLANClientState
*vc
;
1123 /* Don't warn about the default network setup that you get if
1124 * no command line -net or -netdev options are specified. There
1125 * are two cases that we would otherwise complain about:
1126 * (1) board doesn't support a NIC but the implicit "-net nic"
1128 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
1129 * sets up a nic that isn't connected to anything.
1135 net_hub_check_clients();
1137 QTAILQ_FOREACH(vc
, &non_vlan_clients
, next
) {
1139 fprintf(stderr
, "Warning: %s %s has no peer\n",
1140 vc
->info
->type
== NET_CLIENT_OPTIONS_KIND_NIC
? "nic" : "netdev",
1145 /* Check that all NICs requested via -net nic actually got created.
1146 * NICs created via -device don't need to be checked here because
1147 * they are always instantiated.
1149 for (i
= 0; i
< MAX_NICS
; i
++) {
1150 NICInfo
*nd
= &nd_table
[i
];
1151 if (nd
->used
&& !nd
->instantiated
) {
1152 fprintf(stderr
, "Warning: requested NIC (%s, model %s) "
1153 "was not created (not supported by this machine?)\n",
1154 nd
->name
? nd
->name
: "anonymous",
1155 nd
->model
? nd
->model
: "unspecified");
1160 static int net_init_client(QemuOpts
*opts
, void *dummy
)
1162 Error
*local_err
= NULL
;
1164 net_client_init(opts
, 0, &local_err
);
1165 if (error_is_set(&local_err
)) {
1166 qerror_report_err(local_err
);
1167 error_free(local_err
);
1174 static int net_init_netdev(QemuOpts
*opts
, void *dummy
)
1176 Error
*local_err
= NULL
;
1179 ret
= net_client_init(opts
, 1, &local_err
);
1180 if (error_is_set(&local_err
)) {
1181 qerror_report_err(local_err
);
1182 error_free(local_err
);
1189 int net_init_clients(void)
1191 QemuOptsList
*net
= qemu_find_opts("net");
1194 /* if no clients, we use a default config */
1195 qemu_opts_set(net
, NULL
, "type", "nic");
1197 qemu_opts_set(net
, NULL
, "type", "user");
1201 QTAILQ_INIT(&vlans
);
1202 QTAILQ_INIT(&non_vlan_clients
);
1204 if (qemu_opts_foreach(qemu_find_opts("netdev"), net_init_netdev
, NULL
, 1) == -1)
1207 if (qemu_opts_foreach(net
, net_init_client
, NULL
, 1) == -1) {
1214 int net_client_parse(QemuOptsList
*opts_list
, const char *optarg
)
1216 #if defined(CONFIG_SLIRP)
1218 if (net_slirp_parse_legacy(opts_list
, optarg
, &ret
)) {
1223 if (!qemu_opts_parse(opts_list
, optarg
, 1)) {
1233 unsigned compute_mcast_idx(const uint8_t *ep
)
1240 for (i
= 0; i
< 6; i
++) {
1242 for (j
= 0; j
< 8; j
++) {
1243 carry
= ((crc
& 0x80000000L
) ? 1 : 0) ^ (b
& 0x01);
1247 crc
= ((crc
^ POLYNOMIAL
) | carry
);