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"
35 #include "qemu-common.h"
36 #include "qemu_socket.h"
37 #include "qmp-commands.h"
40 #include "qapi-visit.h"
41 #include "qapi/opts-visitor.h"
42 #include "qapi/qapi-dealloc-visitor.h"
44 /* Net bridge is currently not supported for W32. */
46 # define CONFIG_NET_BRIDGE
49 static QTAILQ_HEAD(, VLANState
) vlans
;
50 static QTAILQ_HEAD(, VLANClientState
) non_vlan_clients
;
54 /***********************************************************/
55 /* network device redirectors */
57 #if defined(DEBUG_NET)
58 static void hex_dump(FILE *f
, const uint8_t *buf
, int size
)
62 for(i
=0;i
<size
;i
+=16) {
66 fprintf(f
, "%08x ", i
);
69 fprintf(f
, " %02x", buf
[i
+j
]);
76 if (c
< ' ' || c
> '~')
85 static int get_str_sep(char *buf
, int buf_size
, const char **pp
, int sep
)
96 if (len
> buf_size
- 1)
105 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
)
113 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
115 saddr
->sin_family
= AF_INET
;
116 if (buf
[0] == '\0') {
117 saddr
->sin_addr
.s_addr
= 0;
119 if (qemu_isdigit(buf
[0])) {
120 if (!inet_aton(buf
, &saddr
->sin_addr
))
123 if ((he
= gethostbyname(buf
)) == NULL
)
125 saddr
->sin_addr
= *(struct in_addr
*)he
->h_addr
;
128 port
= strtol(p
, (char **)&r
, 0);
131 saddr
->sin_port
= htons(port
);
135 void qemu_format_nic_info_str(VLANClientState
*vc
, uint8_t macaddr
[6])
137 snprintf(vc
->info_str
, sizeof(vc
->info_str
),
138 "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
140 macaddr
[0], macaddr
[1], macaddr
[2],
141 macaddr
[3], macaddr
[4], macaddr
[5]);
144 void qemu_macaddr_default_if_unset(MACAddr
*macaddr
)
146 static int index
= 0;
147 static const MACAddr zero
= { .a
= { 0,0,0,0,0,0 } };
149 if (memcmp(macaddr
, &zero
, sizeof(zero
)) != 0)
151 macaddr
->a
[0] = 0x52;
152 macaddr
->a
[1] = 0x54;
153 macaddr
->a
[2] = 0x00;
154 macaddr
->a
[3] = 0x12;
155 macaddr
->a
[4] = 0x34;
156 macaddr
->a
[5] = 0x56 + index
++;
159 static char *assign_name(VLANClientState
*vc1
, const char *model
)
166 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
167 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
168 if (vc
!= vc1
&& strcmp(vc
->model
, model
) == 0) {
174 QTAILQ_FOREACH(vc
, &non_vlan_clients
, next
) {
175 if (vc
!= vc1
&& strcmp(vc
->model
, model
) == 0) {
180 snprintf(buf
, sizeof(buf
), "%s.%d", model
, id
);
182 return g_strdup(buf
);
185 static ssize_t
qemu_deliver_packet(VLANClientState
*sender
,
190 static ssize_t
qemu_deliver_packet_iov(VLANClientState
*sender
,
192 const struct iovec
*iov
,
196 VLANClientState
*qemu_new_net_client(NetClientInfo
*info
,
198 VLANClientState
*peer
,
204 assert(info
->size
>= sizeof(VLANClientState
));
206 vc
= g_malloc0(info
->size
);
209 vc
->model
= g_strdup(model
);
211 vc
->name
= g_strdup(name
);
213 vc
->name
= assign_name(vc
, model
);
219 QTAILQ_INSERT_TAIL(&vc
->vlan
->clients
, vc
, next
);
226 QTAILQ_INSERT_TAIL(&non_vlan_clients
, vc
, next
);
228 vc
->send_queue
= qemu_new_net_queue(qemu_deliver_packet
,
229 qemu_deliver_packet_iov
,
236 NICState
*qemu_new_nic(NetClientInfo
*info
,
245 assert(info
->type
== NET_CLIENT_OPTIONS_KIND_NIC
);
246 assert(info
->size
>= sizeof(NICState
));
248 nc
= qemu_new_net_client(info
, conf
->vlan
, conf
->peer
, model
, name
);
250 nic
= DO_UPCAST(NICState
, nc
, nc
);
252 nic
->opaque
= opaque
;
257 static void qemu_cleanup_vlan_client(VLANClientState
*vc
)
260 QTAILQ_REMOVE(&vc
->vlan
->clients
, vc
, next
);
262 QTAILQ_REMOVE(&non_vlan_clients
, vc
, next
);
265 if (vc
->info
->cleanup
) {
266 vc
->info
->cleanup(vc
);
270 static void qemu_free_vlan_client(VLANClientState
*vc
)
273 if (vc
->send_queue
) {
274 qemu_del_net_queue(vc
->send_queue
);
277 vc
->peer
->peer
= NULL
;
285 void qemu_del_vlan_client(VLANClientState
*vc
)
287 /* If there is a peer NIC, delete and cleanup client, but do not free. */
288 if (!vc
->vlan
&& vc
->peer
&& vc
->peer
->info
->type
== NET_CLIENT_OPTIONS_KIND_NIC
) {
289 NICState
*nic
= DO_UPCAST(NICState
, nc
, vc
->peer
);
290 if (nic
->peer_deleted
) {
293 nic
->peer_deleted
= true;
294 /* Let NIC know peer is gone. */
295 vc
->peer
->link_down
= true;
296 if (vc
->peer
->info
->link_status_changed
) {
297 vc
->peer
->info
->link_status_changed(vc
->peer
);
299 qemu_cleanup_vlan_client(vc
);
303 /* If this is a peer NIC and peer has already been deleted, free it now. */
304 if (!vc
->vlan
&& vc
->peer
&& vc
->info
->type
== NET_CLIENT_OPTIONS_KIND_NIC
) {
305 NICState
*nic
= DO_UPCAST(NICState
, nc
, vc
);
306 if (nic
->peer_deleted
) {
307 qemu_free_vlan_client(vc
->peer
);
311 qemu_cleanup_vlan_client(vc
);
312 qemu_free_vlan_client(vc
);
316 qemu_find_vlan_client_by_name(Monitor
*mon
, int vlan_id
,
317 const char *client_str
)
322 vlan
= qemu_find_vlan(vlan_id
, 0);
324 monitor_printf(mon
, "unknown VLAN %d\n", vlan_id
);
328 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
329 if (!strcmp(vc
->name
, client_str
)) {
334 monitor_printf(mon
, "can't find device %s on VLAN %d\n",
335 client_str
, vlan_id
);
341 void qemu_foreach_nic(qemu_nic_foreach func
, void *opaque
)
346 QTAILQ_FOREACH(nc
, &non_vlan_clients
, next
) {
347 if (nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_NIC
) {
348 func(DO_UPCAST(NICState
, nc
, nc
), opaque
);
352 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
353 QTAILQ_FOREACH(nc
, &vlan
->clients
, next
) {
354 if (nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_NIC
) {
355 func(DO_UPCAST(NICState
, nc
, nc
), opaque
);
361 int qemu_can_send_packet(VLANClientState
*sender
)
363 VLANState
*vlan
= sender
->vlan
;
367 if (sender
->peer
->receive_disabled
) {
369 } else if (sender
->peer
->info
->can_receive
&&
370 !sender
->peer
->info
->can_receive(sender
->peer
)) {
381 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
386 /* no can_receive() handler, they can always receive */
387 if (vc
->info
->can_receive
&& !vc
->info
->can_receive(vc
)) {
394 static ssize_t
qemu_deliver_packet(VLANClientState
*sender
,
400 VLANClientState
*vc
= opaque
;
407 if (vc
->receive_disabled
) {
411 if (flags
& QEMU_NET_PACKET_FLAG_RAW
&& vc
->info
->receive_raw
) {
412 ret
= vc
->info
->receive_raw(vc
, data
, size
);
414 ret
= vc
->info
->receive(vc
, data
, size
);
418 vc
->receive_disabled
= 1;
424 static ssize_t
qemu_vlan_deliver_packet(VLANClientState
*sender
,
430 VLANState
*vlan
= opaque
;
434 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
446 if (vc
->receive_disabled
) {
451 if (flags
& QEMU_NET_PACKET_FLAG_RAW
&& vc
->info
->receive_raw
) {
452 len
= vc
->info
->receive_raw(vc
, buf
, size
);
454 len
= vc
->info
->receive(vc
, buf
, size
);
458 vc
->receive_disabled
= 1;
461 ret
= (ret
>= 0) ? ret
: len
;
468 void qemu_purge_queued_packets(VLANClientState
*vc
)
472 if (!vc
->peer
&& !vc
->vlan
) {
477 queue
= vc
->peer
->send_queue
;
479 queue
= vc
->vlan
->send_queue
;
482 qemu_net_queue_purge(queue
, vc
);
485 void qemu_flush_queued_packets(VLANClientState
*vc
)
489 vc
->receive_disabled
= 0;
492 queue
= vc
->vlan
->send_queue
;
494 queue
= vc
->send_queue
;
497 qemu_net_queue_flush(queue
);
500 static ssize_t
qemu_send_packet_async_with_flags(VLANClientState
*sender
,
502 const uint8_t *buf
, int size
,
503 NetPacketSent
*sent_cb
)
508 printf("qemu_send_packet_async:\n");
509 hex_dump(stdout
, buf
, size
);
512 if (sender
->link_down
|| (!sender
->peer
&& !sender
->vlan
)) {
517 queue
= sender
->peer
->send_queue
;
519 queue
= sender
->vlan
->send_queue
;
522 return qemu_net_queue_send(queue
, sender
, flags
, buf
, size
, sent_cb
);
525 ssize_t
qemu_send_packet_async(VLANClientState
*sender
,
526 const uint8_t *buf
, int size
,
527 NetPacketSent
*sent_cb
)
529 return qemu_send_packet_async_with_flags(sender
, QEMU_NET_PACKET_FLAG_NONE
,
533 void qemu_send_packet(VLANClientState
*vc
, const uint8_t *buf
, int size
)
535 qemu_send_packet_async(vc
, buf
, size
, NULL
);
538 ssize_t
qemu_send_packet_raw(VLANClientState
*vc
, const uint8_t *buf
, int size
)
540 return qemu_send_packet_async_with_flags(vc
, QEMU_NET_PACKET_FLAG_RAW
,
544 static ssize_t
vc_sendv_compat(VLANClientState
*vc
, const struct iovec
*iov
,
547 uint8_t buffer
[4096];
550 offset
= iov_to_buf(iov
, iovcnt
, 0, buffer
, sizeof(buffer
));
552 return vc
->info
->receive(vc
, buffer
, offset
);
555 static ssize_t
qemu_deliver_packet_iov(VLANClientState
*sender
,
557 const struct iovec
*iov
,
561 VLANClientState
*vc
= opaque
;
564 return iov_size(iov
, iovcnt
);
567 if (vc
->info
->receive_iov
) {
568 return vc
->info
->receive_iov(vc
, iov
, iovcnt
);
570 return vc_sendv_compat(vc
, iov
, iovcnt
);
574 static ssize_t
qemu_vlan_deliver_packet_iov(VLANClientState
*sender
,
576 const struct iovec
*iov
,
580 VLANState
*vlan
= opaque
;
584 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
592 ret
= iov_size(iov
, iovcnt
);
596 assert(!(flags
& QEMU_NET_PACKET_FLAG_RAW
));
598 if (vc
->info
->receive_iov
) {
599 len
= vc
->info
->receive_iov(vc
, iov
, iovcnt
);
601 len
= vc_sendv_compat(vc
, iov
, iovcnt
);
604 ret
= (ret
>= 0) ? ret
: len
;
610 ssize_t
qemu_sendv_packet_async(VLANClientState
*sender
,
611 const struct iovec
*iov
, int iovcnt
,
612 NetPacketSent
*sent_cb
)
616 if (sender
->link_down
|| (!sender
->peer
&& !sender
->vlan
)) {
617 return iov_size(iov
, iovcnt
);
621 queue
= sender
->peer
->send_queue
;
623 queue
= sender
->vlan
->send_queue
;
626 return qemu_net_queue_send_iov(queue
, sender
,
627 QEMU_NET_PACKET_FLAG_NONE
,
628 iov
, iovcnt
, sent_cb
);
632 qemu_sendv_packet(VLANClientState
*vc
, const struct iovec
*iov
, int iovcnt
)
634 return qemu_sendv_packet_async(vc
, iov
, iovcnt
, NULL
);
637 /* find or alloc a new VLAN */
638 VLANState
*qemu_find_vlan(int id
, int allocate
)
642 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
643 if (vlan
->id
== id
) {
652 vlan
= g_malloc0(sizeof(VLANState
));
654 QTAILQ_INIT(&vlan
->clients
);
656 vlan
->send_queue
= qemu_new_net_queue(qemu_vlan_deliver_packet
,
657 qemu_vlan_deliver_packet_iov
,
660 QTAILQ_INSERT_TAIL(&vlans
, vlan
, next
);
665 VLANClientState
*qemu_find_netdev(const char *id
)
669 QTAILQ_FOREACH(vc
, &non_vlan_clients
, next
) {
670 if (vc
->info
->type
== NET_CLIENT_OPTIONS_KIND_NIC
)
672 if (!strcmp(vc
->name
, id
)) {
680 static int nic_get_free_idx(void)
684 for (index
= 0; index
< MAX_NICS
; index
++)
685 if (!nd_table
[index
].used
)
690 int qemu_show_nic_models(const char *arg
, const char *const *models
)
694 if (!arg
|| strcmp(arg
, "?"))
697 fprintf(stderr
, "qemu: Supported NIC models: ");
698 for (i
= 0 ; models
[i
]; i
++)
699 fprintf(stderr
, "%s%c", models
[i
], models
[i
+1] ? ',' : '\n');
703 void qemu_check_nic_model(NICInfo
*nd
, const char *model
)
705 const char *models
[2];
710 if (qemu_show_nic_models(nd
->model
, models
))
712 if (qemu_find_nic_model(nd
, models
, model
) < 0)
716 int qemu_find_nic_model(NICInfo
*nd
, const char * const *models
,
717 const char *default_model
)
722 nd
->model
= g_strdup(default_model
);
724 for (i
= 0 ; models
[i
]; i
++) {
725 if (strcmp(nd
->model
, models
[i
]) == 0)
729 error_report("Unsupported NIC model: %s", nd
->model
);
733 int net_handle_fd_param(Monitor
*mon
, const char *param
)
737 if (!qemu_isdigit(param
[0]) && mon
) {
739 fd
= monitor_get_fd(mon
, param
);
741 error_report("No file descriptor named %s found", param
);
745 fd
= qemu_parse_fd(param
);
751 static int net_init_nic(const NetClientOptions
*opts
, const char *name
,
756 const NetLegacyNicOptions
*nic
;
758 assert(opts
->kind
== NET_CLIENT_OPTIONS_KIND_NIC
);
761 idx
= nic_get_free_idx();
762 if (idx
== -1 || nb_nics
>= MAX_NICS
) {
763 error_report("Too Many NICs");
769 memset(nd
, 0, sizeof(*nd
));
771 if (nic
->has_netdev
) {
772 nd
->netdev
= qemu_find_netdev(nic
->netdev
);
774 error_report("netdev '%s' not found", nic
->netdev
);
782 nd
->name
= g_strdup(name
);
784 if (nic
->has_model
) {
785 nd
->model
= g_strdup(nic
->model
);
788 nd
->devaddr
= g_strdup(nic
->addr
);
791 if (nic
->has_macaddr
&&
792 net_parse_macaddr(nd
->macaddr
.a
, nic
->macaddr
) < 0) {
793 error_report("invalid syntax for ethernet address");
796 qemu_macaddr_default_if_unset(&nd
->macaddr
);
798 if (nic
->has_vectors
) {
799 if (nic
->vectors
> 0x7ffffff) {
800 error_report("invalid # of vectors: %"PRIu32
, nic
->vectors
);
803 nd
->nvectors
= nic
->vectors
;
805 nd
->nvectors
= DEV_NVECTORS_UNSPECIFIED
;
815 static int (* const net_client_init_fun
[NET_CLIENT_OPTIONS_KIND_MAX
])(
816 const NetClientOptions
*opts
,
819 [NET_CLIENT_OPTIONS_KIND_NIC
] = net_init_nic
,
821 [NET_CLIENT_OPTIONS_KIND_USER
] = net_init_slirp
,
823 [NET_CLIENT_OPTIONS_KIND_TAP
] = net_init_tap
,
824 [NET_CLIENT_OPTIONS_KIND_SOCKET
] = net_init_socket
,
826 [NET_CLIENT_OPTIONS_KIND_VDE
] = net_init_vde
,
828 [NET_CLIENT_OPTIONS_KIND_DUMP
] = net_init_dump
,
829 #ifdef CONFIG_NET_BRIDGE
830 [NET_CLIENT_OPTIONS_KIND_BRIDGE
] = net_init_bridge
,
835 static int net_client_init1(const void *object
, int is_netdev
, Error
**errp
)
838 const Netdev
*netdev
;
839 const NetLegacy
*net
;
841 const NetClientOptions
*opts
;
846 opts
= u
.netdev
->opts
;
849 switch (opts
->kind
) {
851 case NET_CLIENT_OPTIONS_KIND_USER
:
853 case NET_CLIENT_OPTIONS_KIND_TAP
:
854 case NET_CLIENT_OPTIONS_KIND_SOCKET
:
856 case NET_CLIENT_OPTIONS_KIND_VDE
:
858 #ifdef CONFIG_NET_BRIDGE
859 case NET_CLIENT_OPTIONS_KIND_BRIDGE
:
864 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, "type",
865 "a netdev backend type");
871 /* missing optional values have been initialized to "all bits zero" */
872 name
= u
.net
->has_id
? u
.net
->id
: u
.net
->name
;
875 if (net_client_init_fun
[opts
->kind
]) {
876 VLANState
*vlan
= NULL
;
878 /* Do not add to a vlan if it's a -netdev or a nic with a netdev=
881 (opts
->kind
!= NET_CLIENT_OPTIONS_KIND_NIC
||
882 !opts
->nic
->has_netdev
)) {
883 vlan
= qemu_find_vlan(u
.net
->has_vlan
? u
.net
->vlan
: 0, true);
886 if (net_client_init_fun
[opts
->kind
](opts
, name
, vlan
) < 0) {
887 /* TODO push error reporting into init() methods */
888 error_set(errp
, QERR_DEVICE_INIT_FAILED
,
889 NetClientOptionsKind_lookup
[opts
->kind
]);
897 static void net_visit(Visitor
*v
, int is_netdev
, void **object
, Error
**errp
)
900 visit_type_Netdev(v
, (Netdev
**)object
, NULL
, errp
);
902 visit_type_NetLegacy(v
, (NetLegacy
**)object
, NULL
, errp
);
907 int net_client_init(QemuOpts
*opts
, int is_netdev
, Error
**errp
)
914 OptsVisitor
*ov
= opts_visitor_new(opts
);
916 net_visit(opts_get_visitor(ov
), is_netdev
, &object
, &err
);
917 opts_visitor_cleanup(ov
);
921 ret
= net_client_init1(object
, is_netdev
, &err
);
925 QapiDeallocVisitor
*dv
= qapi_dealloc_visitor_new();
927 net_visit(qapi_dealloc_get_visitor(dv
), is_netdev
, &object
, NULL
);
928 qapi_dealloc_visitor_cleanup(dv
);
931 error_propagate(errp
, err
);
936 static int net_host_check_device(const char *device
)
939 const char *valid_param_list
[] = { "tap", "socket", "dump"
940 #ifdef CONFIG_NET_BRIDGE
950 for (i
= 0; i
< sizeof(valid_param_list
) / sizeof(char *); i
++) {
951 if (!strncmp(valid_param_list
[i
], device
,
952 strlen(valid_param_list
[i
])))
959 void net_host_device_add(Monitor
*mon
, const QDict
*qdict
)
961 const char *device
= qdict_get_str(qdict
, "device");
962 const char *opts_str
= qdict_get_try_str(qdict
, "opts");
963 Error
*local_err
= NULL
;
966 if (!net_host_check_device(device
)) {
967 monitor_printf(mon
, "invalid host network device %s\n", device
);
971 opts
= qemu_opts_parse(qemu_find_opts("net"), opts_str
? opts_str
: "", 0);
976 qemu_opt_set(opts
, "type", device
);
978 net_client_init(opts
, 0, &local_err
);
979 if (error_is_set(&local_err
)) {
980 qerror_report_err(local_err
);
981 error_free(local_err
);
982 monitor_printf(mon
, "adding host network device %s failed\n", device
);
986 void net_host_device_remove(Monitor
*mon
, const QDict
*qdict
)
989 int vlan_id
= qdict_get_int(qdict
, "vlan_id");
990 const char *device
= qdict_get_str(qdict
, "device");
992 vc
= qemu_find_vlan_client_by_name(mon
, vlan_id
, device
);
996 if (!net_host_check_device(vc
->model
)) {
997 monitor_printf(mon
, "invalid host network device %s\n", device
);
1000 qemu_del_vlan_client(vc
);
1003 void netdev_add(QemuOpts
*opts
, Error
**errp
)
1005 net_client_init(opts
, 1, errp
);
1008 int qmp_netdev_add(Monitor
*mon
, const QDict
*qdict
, QObject
**ret
)
1010 Error
*local_err
= NULL
;
1011 QemuOptsList
*opts_list
;
1014 opts_list
= qemu_find_opts_err("netdev", &local_err
);
1015 if (error_is_set(&local_err
)) {
1019 opts
= qemu_opts_from_qdict(opts_list
, qdict
, &local_err
);
1020 if (error_is_set(&local_err
)) {
1024 netdev_add(opts
, &local_err
);
1025 if (error_is_set(&local_err
)) {
1026 qemu_opts_del(opts
);
1033 qerror_report_err(local_err
);
1034 error_free(local_err
);
1038 void qmp_netdev_del(const char *id
, Error
**errp
)
1040 VLANClientState
*vc
;
1042 vc
= qemu_find_netdev(id
);
1044 error_set(errp
, QERR_DEVICE_NOT_FOUND
, id
);
1048 qemu_del_vlan_client(vc
);
1049 qemu_opts_del(qemu_opts_find(qemu_find_opts_err("netdev", errp
), id
));
1052 static void print_net_client(Monitor
*mon
, VLANClientState
*vc
)
1054 monitor_printf(mon
, "%s: type=%s,%s\n", vc
->name
,
1055 NetClientOptionsKind_lookup
[vc
->info
->type
], vc
->info_str
);
1058 void do_info_network(Monitor
*mon
)
1061 VLANClientState
*vc
, *peer
;
1062 NetClientOptionsKind type
;
1064 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
1065 monitor_printf(mon
, "VLAN %d devices:\n", vlan
->id
);
1067 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
1068 monitor_printf(mon
, " ");
1069 print_net_client(mon
, vc
);
1072 monitor_printf(mon
, "Devices not on any VLAN:\n");
1073 QTAILQ_FOREACH(vc
, &non_vlan_clients
, next
) {
1075 type
= vc
->info
->type
;
1076 if (!peer
|| type
== NET_CLIENT_OPTIONS_KIND_NIC
) {
1077 monitor_printf(mon
, " ");
1078 print_net_client(mon
, vc
);
1079 } /* else it's a netdev connected to a NIC, printed with the NIC */
1080 if (peer
&& type
== NET_CLIENT_OPTIONS_KIND_NIC
) {
1081 monitor_printf(mon
, " \\ ");
1082 print_net_client(mon
, peer
);
1087 void qmp_set_link(const char *name
, bool up
, Error
**errp
)
1090 VLANClientState
*vc
= NULL
;
1092 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
1093 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
1094 if (strcmp(vc
->name
, name
) == 0) {
1099 QTAILQ_FOREACH(vc
, &non_vlan_clients
, next
) {
1100 if (!strcmp(vc
->name
, name
)) {
1107 error_set(errp
, QERR_DEVICE_NOT_FOUND
, name
);
1111 vc
->link_down
= !up
;
1113 if (vc
->info
->link_status_changed
) {
1114 vc
->info
->link_status_changed(vc
);
1117 /* Notify peer. Don't update peer link status: this makes it possible to
1118 * disconnect from host network without notifying the guest.
1119 * FIXME: is disconnected link status change operation useful?
1121 * Current behaviour is compatible with qemu vlans where there could be
1122 * multiple clients that can still communicate with each other in
1123 * disconnected mode. For now maintain this compatibility. */
1124 if (vc
->peer
&& vc
->peer
->info
->link_status_changed
) {
1125 vc
->peer
->info
->link_status_changed(vc
->peer
);
1129 void net_cleanup(void)
1132 VLANClientState
*vc
, *next_vc
;
1134 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
1135 QTAILQ_FOREACH_SAFE(vc
, &vlan
->clients
, next
, next_vc
) {
1136 qemu_del_vlan_client(vc
);
1140 QTAILQ_FOREACH_SAFE(vc
, &non_vlan_clients
, next
, next_vc
) {
1141 qemu_del_vlan_client(vc
);
1145 void net_check_clients(void)
1148 VLANClientState
*vc
;
1151 /* Don't warn about the default network setup that you get if
1152 * no command line -net or -netdev options are specified. There
1153 * are two cases that we would otherwise complain about:
1154 * (1) board doesn't support a NIC but the implicit "-net nic"
1156 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
1157 * sets up a nic that isn't connected to anything.
1163 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
1164 int has_nic
= 0, has_host_dev
= 0;
1166 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
1167 switch (vc
->info
->type
) {
1168 case NET_CLIENT_OPTIONS_KIND_NIC
:
1171 case NET_CLIENT_OPTIONS_KIND_USER
:
1172 case NET_CLIENT_OPTIONS_KIND_TAP
:
1173 case NET_CLIENT_OPTIONS_KIND_SOCKET
:
1174 case NET_CLIENT_OPTIONS_KIND_VDE
:
1180 if (has_host_dev
&& !has_nic
)
1181 fprintf(stderr
, "Warning: vlan %d with no nics\n", vlan
->id
);
1182 if (has_nic
&& !has_host_dev
)
1184 "Warning: vlan %d is not connected to host network\n",
1187 QTAILQ_FOREACH(vc
, &non_vlan_clients
, next
) {
1189 fprintf(stderr
, "Warning: %s %s has no peer\n",
1190 vc
->info
->type
== NET_CLIENT_OPTIONS_KIND_NIC
? "nic" : "netdev",
1195 /* Check that all NICs requested via -net nic actually got created.
1196 * NICs created via -device don't need to be checked here because
1197 * they are always instantiated.
1199 for (i
= 0; i
< MAX_NICS
; i
++) {
1200 NICInfo
*nd
= &nd_table
[i
];
1201 if (nd
->used
&& !nd
->instantiated
) {
1202 fprintf(stderr
, "Warning: requested NIC (%s, model %s) "
1203 "was not created (not supported by this machine?)\n",
1204 nd
->name
? nd
->name
: "anonymous",
1205 nd
->model
? nd
->model
: "unspecified");
1210 static int net_init_client(QemuOpts
*opts
, void *dummy
)
1212 Error
*local_err
= NULL
;
1214 net_client_init(opts
, 0, &local_err
);
1215 if (error_is_set(&local_err
)) {
1216 qerror_report_err(local_err
);
1217 error_free(local_err
);
1224 static int net_init_netdev(QemuOpts
*opts
, void *dummy
)
1226 Error
*local_err
= NULL
;
1229 ret
= net_client_init(opts
, 1, &local_err
);
1230 if (error_is_set(&local_err
)) {
1231 qerror_report_err(local_err
);
1232 error_free(local_err
);
1239 int net_init_clients(void)
1241 QemuOptsList
*net
= qemu_find_opts("net");
1244 /* if no clients, we use a default config */
1245 qemu_opts_set(net
, NULL
, "type", "nic");
1247 qemu_opts_set(net
, NULL
, "type", "user");
1251 QTAILQ_INIT(&vlans
);
1252 QTAILQ_INIT(&non_vlan_clients
);
1254 if (qemu_opts_foreach(qemu_find_opts("netdev"), net_init_netdev
, NULL
, 1) == -1)
1257 if (qemu_opts_foreach(net
, net_init_client
, NULL
, 1) == -1) {
1264 int net_client_parse(QemuOptsList
*opts_list
, const char *optarg
)
1266 #if defined(CONFIG_SLIRP)
1268 if (net_slirp_parse_legacy(opts_list
, optarg
, &ret
)) {
1273 if (!qemu_opts_parse(opts_list
, optarg
, 1)) {
1283 unsigned compute_mcast_idx(const uint8_t *ep
)
1290 for (i
= 0; i
< 6; i
++) {
1292 for (j
= 0; j
< 8; j
++) {
1293 carry
= ((crc
& 0x80000000L
) ? 1 : 0) ^ (b
& 0x01);
1297 crc
= ((crc
^ POLYNOMIAL
) | carry
);