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"
45 /* Net bridge is currently not supported for W32. */
47 # define CONFIG_NET_BRIDGE
50 static QTAILQ_HEAD(, NetClientState
) net_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(NetClientState
*nc
, uint8_t macaddr
[6])
137 snprintf(nc
->info_str
, sizeof(nc
->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
++;
160 * Generate a name for net client
162 * Only net clients created with the legacy -net option and NICs need this.
164 static char *assign_name(NetClientState
*nc1
, const char *model
)
169 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
173 if (strcmp(nc
->model
, model
) == 0) {
178 return g_strdup_printf("%s.%d", model
, id
);
181 static void qemu_net_client_destructor(NetClientState
*nc
)
186 static void qemu_net_client_setup(NetClientState
*nc
,
188 NetClientState
*peer
,
191 NetClientDestructor
*destructor
)
194 nc
->model
= g_strdup(model
);
196 nc
->name
= g_strdup(name
);
198 nc
->name
= assign_name(nc
, model
);
206 QTAILQ_INSERT_TAIL(&net_clients
, nc
, next
);
208 nc
->incoming_queue
= qemu_new_net_queue(nc
);
209 nc
->destructor
= destructor
;
212 NetClientState
*qemu_new_net_client(NetClientInfo
*info
,
213 NetClientState
*peer
,
219 assert(info
->size
>= sizeof(NetClientState
));
221 nc
= g_malloc0(info
->size
);
222 qemu_net_client_setup(nc
, info
, peer
, model
, name
,
223 qemu_net_client_destructor
);
228 NICState
*qemu_new_nic(NetClientInfo
*info
,
234 NetClientState
**peers
= conf
->peers
.ncs
;
236 int i
, queues
= MAX(1, conf
->queues
);
238 assert(info
->type
== NET_CLIENT_OPTIONS_KIND_NIC
);
239 assert(info
->size
>= sizeof(NICState
));
241 nic
= g_malloc0(info
->size
+ sizeof(NetClientState
) * queues
);
242 nic
->ncs
= (void *)nic
+ info
->size
;
244 nic
->opaque
= opaque
;
246 for (i
= 0; i
< queues
; i
++) {
247 qemu_net_client_setup(&nic
->ncs
[i
], info
, peers
[i
], model
, name
,
249 nic
->ncs
[i
].queue_index
= i
;
255 NetClientState
*qemu_get_subqueue(NICState
*nic
, int queue_index
)
257 return nic
->ncs
+ queue_index
;
260 NetClientState
*qemu_get_queue(NICState
*nic
)
262 return qemu_get_subqueue(nic
, 0);
265 NICState
*qemu_get_nic(NetClientState
*nc
)
267 NetClientState
*nc0
= nc
- nc
->queue_index
;
269 return (NICState
*)((void *)nc0
- nc
->info
->size
);
272 void *qemu_get_nic_opaque(NetClientState
*nc
)
274 NICState
*nic
= qemu_get_nic(nc
);
279 static void qemu_cleanup_net_client(NetClientState
*nc
)
281 QTAILQ_REMOVE(&net_clients
, nc
, next
);
283 if (nc
->info
->cleanup
) {
284 nc
->info
->cleanup(nc
);
288 static void qemu_free_net_client(NetClientState
*nc
)
290 if (nc
->incoming_queue
) {
291 qemu_del_net_queue(nc
->incoming_queue
);
294 nc
->peer
->peer
= NULL
;
298 if (nc
->destructor
) {
303 void qemu_del_net_client(NetClientState
*nc
)
305 NetClientState
*ncs
[MAX_QUEUE_NUM
];
308 /* If the NetClientState belongs to a multiqueue backend, we will change all
309 * other NetClientStates also.
311 queues
= qemu_find_net_clients_except(nc
->name
, ncs
,
312 NET_CLIENT_OPTIONS_KIND_NIC
,
316 /* If there is a peer NIC, delete and cleanup client, but do not free. */
317 if (nc
->peer
&& nc
->peer
->info
->type
== NET_CLIENT_OPTIONS_KIND_NIC
) {
318 NICState
*nic
= qemu_get_nic(nc
->peer
);
319 if (nic
->peer_deleted
) {
322 nic
->peer_deleted
= true;
324 for (i
= 0; i
< queues
; i
++) {
325 ncs
[i
]->peer
->link_down
= true;
328 if (nc
->peer
->info
->link_status_changed
) {
329 nc
->peer
->info
->link_status_changed(nc
->peer
);
332 for (i
= 0; i
< queues
; i
++) {
333 qemu_cleanup_net_client(ncs
[i
]);
339 assert(nc
->info
->type
!= NET_CLIENT_OPTIONS_KIND_NIC
);
341 for (i
= 0; i
< queues
; i
++) {
342 qemu_cleanup_net_client(ncs
[i
]);
343 qemu_free_net_client(ncs
[i
]);
347 void qemu_del_nic(NICState
*nic
)
349 int i
, queues
= MAX(nic
->conf
->queues
, 1);
351 /* If this is a peer NIC and peer has already been deleted, free it now. */
352 if (nic
->peer_deleted
) {
353 for (i
= 0; i
< queues
; i
++) {
354 qemu_free_net_client(qemu_get_subqueue(nic
, i
)->peer
);
358 for (i
= queues
- 1; i
>= 0; i
--) {
359 NetClientState
*nc
= qemu_get_subqueue(nic
, i
);
361 qemu_cleanup_net_client(nc
);
362 qemu_free_net_client(nc
);
368 void qemu_foreach_nic(qemu_nic_foreach func
, void *opaque
)
372 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
373 if (nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_NIC
) {
374 if (nc
->queue_index
== 0) {
375 func(qemu_get_nic(nc
), opaque
);
381 int qemu_can_send_packet(NetClientState
*sender
)
387 if (sender
->peer
->receive_disabled
) {
389 } else if (sender
->peer
->info
->can_receive
&&
390 !sender
->peer
->info
->can_receive(sender
->peer
)) {
396 ssize_t
qemu_deliver_packet(NetClientState
*sender
,
402 NetClientState
*nc
= opaque
;
409 if (nc
->receive_disabled
) {
413 if (flags
& QEMU_NET_PACKET_FLAG_RAW
&& nc
->info
->receive_raw
) {
414 ret
= nc
->info
->receive_raw(nc
, data
, size
);
416 ret
= nc
->info
->receive(nc
, data
, size
);
420 nc
->receive_disabled
= 1;
426 void qemu_purge_queued_packets(NetClientState
*nc
)
432 qemu_net_queue_purge(nc
->peer
->incoming_queue
, nc
);
435 void qemu_flush_queued_packets(NetClientState
*nc
)
437 nc
->receive_disabled
= 0;
439 if (nc
->peer
&& nc
->peer
->info
->type
== NET_CLIENT_OPTIONS_KIND_HUBPORT
) {
440 if (net_hub_flush(nc
->peer
)) {
444 if (qemu_net_queue_flush(nc
->incoming_queue
)) {
445 /* We emptied the queue successfully, signal to the IO thread to repoll
446 * the file descriptor (for tap, for example).
452 static ssize_t
qemu_send_packet_async_with_flags(NetClientState
*sender
,
454 const uint8_t *buf
, int size
,
455 NetPacketSent
*sent_cb
)
460 printf("qemu_send_packet_async:\n");
461 hex_dump(stdout
, buf
, size
);
464 if (sender
->link_down
|| !sender
->peer
) {
468 queue
= sender
->peer
->incoming_queue
;
470 return qemu_net_queue_send(queue
, sender
, flags
, buf
, size
, sent_cb
);
473 ssize_t
qemu_send_packet_async(NetClientState
*sender
,
474 const uint8_t *buf
, int size
,
475 NetPacketSent
*sent_cb
)
477 return qemu_send_packet_async_with_flags(sender
, QEMU_NET_PACKET_FLAG_NONE
,
481 void qemu_send_packet(NetClientState
*nc
, const uint8_t *buf
, int size
)
483 qemu_send_packet_async(nc
, buf
, size
, NULL
);
486 ssize_t
qemu_send_packet_raw(NetClientState
*nc
, const uint8_t *buf
, int size
)
488 return qemu_send_packet_async_with_flags(nc
, QEMU_NET_PACKET_FLAG_RAW
,
492 static ssize_t
nc_sendv_compat(NetClientState
*nc
, const struct iovec
*iov
,
495 uint8_t buffer
[NET_BUFSIZE
];
498 offset
= iov_to_buf(iov
, iovcnt
, 0, buffer
, sizeof(buffer
));
500 return nc
->info
->receive(nc
, buffer
, offset
);
503 ssize_t
qemu_deliver_packet_iov(NetClientState
*sender
,
505 const struct iovec
*iov
,
509 NetClientState
*nc
= opaque
;
513 return iov_size(iov
, iovcnt
);
516 if (nc
->receive_disabled
) {
520 if (nc
->info
->receive_iov
) {
521 ret
= nc
->info
->receive_iov(nc
, iov
, iovcnt
);
523 ret
= nc_sendv_compat(nc
, iov
, iovcnt
);
527 nc
->receive_disabled
= 1;
533 ssize_t
qemu_sendv_packet_async(NetClientState
*sender
,
534 const struct iovec
*iov
, int iovcnt
,
535 NetPacketSent
*sent_cb
)
539 if (sender
->link_down
|| !sender
->peer
) {
540 return iov_size(iov
, iovcnt
);
543 queue
= sender
->peer
->incoming_queue
;
545 return qemu_net_queue_send_iov(queue
, sender
,
546 QEMU_NET_PACKET_FLAG_NONE
,
547 iov
, iovcnt
, sent_cb
);
551 qemu_sendv_packet(NetClientState
*nc
, const struct iovec
*iov
, int iovcnt
)
553 return qemu_sendv_packet_async(nc
, iov
, iovcnt
, NULL
);
556 NetClientState
*qemu_find_netdev(const char *id
)
560 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
561 if (nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_NIC
)
563 if (!strcmp(nc
->name
, id
)) {
571 int qemu_find_net_clients_except(const char *id
, NetClientState
**ncs
,
572 NetClientOptionsKind type
, int max
)
577 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
578 if (nc
->info
->type
== type
) {
581 if (!strcmp(nc
->name
, id
)) {
592 static int nic_get_free_idx(void)
596 for (index
= 0; index
< MAX_NICS
; index
++)
597 if (!nd_table
[index
].used
)
602 int qemu_show_nic_models(const char *arg
, const char *const *models
)
606 if (!arg
|| !is_help_option(arg
)) {
610 fprintf(stderr
, "qemu: Supported NIC models: ");
611 for (i
= 0 ; models
[i
]; i
++)
612 fprintf(stderr
, "%s%c", models
[i
], models
[i
+1] ? ',' : '\n');
616 void qemu_check_nic_model(NICInfo
*nd
, const char *model
)
618 const char *models
[2];
623 if (qemu_show_nic_models(nd
->model
, models
))
625 if (qemu_find_nic_model(nd
, models
, model
) < 0)
629 int qemu_find_nic_model(NICInfo
*nd
, const char * const *models
,
630 const char *default_model
)
635 nd
->model
= g_strdup(default_model
);
637 for (i
= 0 ; models
[i
]; i
++) {
638 if (strcmp(nd
->model
, models
[i
]) == 0)
642 error_report("Unsupported NIC model: %s", nd
->model
);
646 static int net_init_nic(const NetClientOptions
*opts
, const char *name
,
647 NetClientState
*peer
)
651 const NetLegacyNicOptions
*nic
;
653 assert(opts
->kind
== NET_CLIENT_OPTIONS_KIND_NIC
);
656 idx
= nic_get_free_idx();
657 if (idx
== -1 || nb_nics
>= MAX_NICS
) {
658 error_report("Too Many NICs");
664 memset(nd
, 0, sizeof(*nd
));
666 if (nic
->has_netdev
) {
667 nd
->netdev
= qemu_find_netdev(nic
->netdev
);
669 error_report("netdev '%s' not found", nic
->netdev
);
676 nd
->name
= g_strdup(name
);
677 if (nic
->has_model
) {
678 nd
->model
= g_strdup(nic
->model
);
681 nd
->devaddr
= g_strdup(nic
->addr
);
684 if (nic
->has_macaddr
&&
685 net_parse_macaddr(nd
->macaddr
.a
, nic
->macaddr
) < 0) {
686 error_report("invalid syntax for ethernet address");
689 if (nic
->has_macaddr
&&
690 is_multicast_ether_addr(nd
->macaddr
.a
)) {
691 error_report("NIC cannot have multicast MAC address (odd 1st byte)");
694 qemu_macaddr_default_if_unset(&nd
->macaddr
);
696 if (nic
->has_vectors
) {
697 if (nic
->vectors
> 0x7ffffff) {
698 error_report("invalid # of vectors: %"PRIu32
, nic
->vectors
);
701 nd
->nvectors
= nic
->vectors
;
703 nd
->nvectors
= DEV_NVECTORS_UNSPECIFIED
;
713 static int (* const net_client_init_fun
[NET_CLIENT_OPTIONS_KIND_MAX
])(
714 const NetClientOptions
*opts
,
716 NetClientState
*peer
) = {
717 [NET_CLIENT_OPTIONS_KIND_NIC
] = net_init_nic
,
719 [NET_CLIENT_OPTIONS_KIND_USER
] = net_init_slirp
,
721 [NET_CLIENT_OPTIONS_KIND_TAP
] = net_init_tap
,
722 [NET_CLIENT_OPTIONS_KIND_SOCKET
] = net_init_socket
,
724 [NET_CLIENT_OPTIONS_KIND_VDE
] = net_init_vde
,
727 [NET_CLIENT_OPTIONS_KIND_NETMAP
] = net_init_netmap
,
729 [NET_CLIENT_OPTIONS_KIND_DUMP
] = net_init_dump
,
730 #ifdef CONFIG_NET_BRIDGE
731 [NET_CLIENT_OPTIONS_KIND_BRIDGE
] = net_init_bridge
,
733 [NET_CLIENT_OPTIONS_KIND_HUBPORT
] = net_init_hubport
,
737 static int net_client_init1(const void *object
, int is_netdev
, Error
**errp
)
740 const Netdev
*netdev
;
741 const NetLegacy
*net
;
743 const NetClientOptions
*opts
;
748 opts
= u
.netdev
->opts
;
751 switch (opts
->kind
) {
753 case NET_CLIENT_OPTIONS_KIND_USER
:
755 case NET_CLIENT_OPTIONS_KIND_TAP
:
756 case NET_CLIENT_OPTIONS_KIND_SOCKET
:
758 case NET_CLIENT_OPTIONS_KIND_VDE
:
761 case NET_CLIENT_OPTIONS_KIND_NETMAP
:
763 #ifdef CONFIG_NET_BRIDGE
764 case NET_CLIENT_OPTIONS_KIND_BRIDGE
:
766 case NET_CLIENT_OPTIONS_KIND_HUBPORT
:
770 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, "type",
771 "a netdev backend type");
777 /* missing optional values have been initialized to "all bits zero" */
778 name
= u
.net
->has_id
? u
.net
->id
: u
.net
->name
;
781 if (net_client_init_fun
[opts
->kind
]) {
782 NetClientState
*peer
= NULL
;
784 /* Do not add to a vlan if it's a -netdev or a nic with a netdev=
787 (opts
->kind
!= NET_CLIENT_OPTIONS_KIND_NIC
||
788 !opts
->nic
->has_netdev
)) {
789 peer
= net_hub_add_port(u
.net
->has_vlan
? u
.net
->vlan
: 0, NULL
);
792 if (net_client_init_fun
[opts
->kind
](opts
, name
, peer
) < 0) {
793 /* TODO push error reporting into init() methods */
794 error_set(errp
, QERR_DEVICE_INIT_FAILED
,
795 NetClientOptionsKind_lookup
[opts
->kind
]);
803 static void net_visit(Visitor
*v
, int is_netdev
, void **object
, Error
**errp
)
806 visit_type_Netdev(v
, (Netdev
**)object
, NULL
, errp
);
808 visit_type_NetLegacy(v
, (NetLegacy
**)object
, NULL
, errp
);
813 int net_client_init(QemuOpts
*opts
, int is_netdev
, Error
**errp
)
820 OptsVisitor
*ov
= opts_visitor_new(opts
);
822 net_visit(opts_get_visitor(ov
), is_netdev
, &object
, &err
);
823 opts_visitor_cleanup(ov
);
827 ret
= net_client_init1(object
, is_netdev
, &err
);
831 QapiDeallocVisitor
*dv
= qapi_dealloc_visitor_new();
833 net_visit(qapi_dealloc_get_visitor(dv
), is_netdev
, &object
, NULL
);
834 qapi_dealloc_visitor_cleanup(dv
);
837 error_propagate(errp
, err
);
842 static int net_host_check_device(const char *device
)
845 const char *valid_param_list
[] = { "tap", "socket", "dump"
846 #ifdef CONFIG_NET_BRIDGE
856 for (i
= 0; i
< ARRAY_SIZE(valid_param_list
); i
++) {
857 if (!strncmp(valid_param_list
[i
], device
,
858 strlen(valid_param_list
[i
])))
865 void net_host_device_add(Monitor
*mon
, const QDict
*qdict
)
867 const char *device
= qdict_get_str(qdict
, "device");
868 const char *opts_str
= qdict_get_try_str(qdict
, "opts");
869 Error
*local_err
= NULL
;
872 if (!net_host_check_device(device
)) {
873 monitor_printf(mon
, "invalid host network device %s\n", device
);
877 opts
= qemu_opts_parse(qemu_find_opts("net"), opts_str
? opts_str
: "", 0);
882 qemu_opt_set(opts
, "type", device
);
884 net_client_init(opts
, 0, &local_err
);
886 qerror_report_err(local_err
);
887 error_free(local_err
);
888 monitor_printf(mon
, "adding host network device %s failed\n", device
);
892 void net_host_device_remove(Monitor
*mon
, const QDict
*qdict
)
895 int vlan_id
= qdict_get_int(qdict
, "vlan_id");
896 const char *device
= qdict_get_str(qdict
, "device");
898 nc
= net_hub_find_client_by_name(vlan_id
, device
);
902 if (!net_host_check_device(nc
->model
)) {
903 monitor_printf(mon
, "invalid host network device %s\n", device
);
906 qemu_del_net_client(nc
);
909 void netdev_add(QemuOpts
*opts
, Error
**errp
)
911 net_client_init(opts
, 1, errp
);
914 int qmp_netdev_add(Monitor
*mon
, const QDict
*qdict
, QObject
**ret
)
916 Error
*local_err
= NULL
;
917 QemuOptsList
*opts_list
;
920 opts_list
= qemu_find_opts_err("netdev", &local_err
);
925 opts
= qemu_opts_from_qdict(opts_list
, qdict
, &local_err
);
930 netdev_add(opts
, &local_err
);
939 qerror_report_err(local_err
);
940 error_free(local_err
);
944 void qmp_netdev_del(const char *id
, Error
**errp
)
949 nc
= qemu_find_netdev(id
);
951 error_set(errp
, QERR_DEVICE_NOT_FOUND
, id
);
955 opts
= qemu_opts_find(qemu_find_opts_err("netdev", NULL
), id
);
957 error_setg(errp
, "Device '%s' is not a netdev", id
);
961 qemu_del_net_client(nc
);
965 void print_net_client(Monitor
*mon
, NetClientState
*nc
)
967 monitor_printf(mon
, "%s: index=%d,type=%s,%s\n", nc
->name
,
969 NetClientOptionsKind_lookup
[nc
->info
->type
],
973 RxFilterInfoList
*qmp_query_rx_filter(bool has_name
, const char *name
,
977 RxFilterInfoList
*filter_list
= NULL
, *last_entry
= NULL
;
979 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
980 RxFilterInfoList
*entry
;
983 if (has_name
&& strcmp(nc
->name
, name
) != 0) {
987 /* only query rx-filter information of NIC */
988 if (nc
->info
->type
!= NET_CLIENT_OPTIONS_KIND_NIC
) {
990 error_setg(errp
, "net client(%s) isn't a NIC", name
);
996 if (nc
->info
->query_rx_filter
) {
997 info
= nc
->info
->query_rx_filter(nc
);
998 entry
= g_malloc0(sizeof(*entry
));
1002 filter_list
= entry
;
1004 last_entry
->next
= entry
;
1007 } else if (has_name
) {
1008 error_setg(errp
, "net client(%s) doesn't support"
1009 " rx-filter querying", name
);
1014 if (filter_list
== NULL
&& !error_is_set(errp
) && has_name
) {
1015 error_setg(errp
, "invalid net client name: %s", name
);
1021 void do_info_network(Monitor
*mon
, const QDict
*qdict
)
1023 NetClientState
*nc
, *peer
;
1024 NetClientOptionsKind type
;
1028 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
1030 type
= nc
->info
->type
;
1032 /* Skip if already printed in hub info */
1033 if (net_hub_id_for_client(nc
, NULL
) == 0) {
1037 if (!peer
|| type
== NET_CLIENT_OPTIONS_KIND_NIC
) {
1038 print_net_client(mon
, nc
);
1039 } /* else it's a netdev connected to a NIC, printed with the NIC */
1040 if (peer
&& type
== NET_CLIENT_OPTIONS_KIND_NIC
) {
1041 monitor_printf(mon
, " \\ ");
1042 print_net_client(mon
, peer
);
1047 void qmp_set_link(const char *name
, bool up
, Error
**errp
)
1049 NetClientState
*ncs
[MAX_QUEUE_NUM
];
1053 queues
= qemu_find_net_clients_except(name
, ncs
,
1054 NET_CLIENT_OPTIONS_KIND_MAX
,
1058 error_set(errp
, QERR_DEVICE_NOT_FOUND
, name
);
1063 for (i
= 0; i
< queues
; i
++) {
1064 ncs
[i
]->link_down
= !up
;
1067 if (nc
->info
->link_status_changed
) {
1068 nc
->info
->link_status_changed(nc
);
1072 /* Change peer link only if the peer is NIC and then notify peer.
1073 * If the peer is a HUBPORT or a backend, we do not change the
1076 * This behavior is compatible with qemu vlans where there could be
1077 * multiple clients that can still communicate with each other in
1078 * disconnected mode. For now maintain this compatibility.
1080 if (nc
->peer
->info
->type
== NET_CLIENT_OPTIONS_KIND_NIC
) {
1081 for (i
= 0; i
< queues
; i
++) {
1082 ncs
[i
]->peer
->link_down
= !up
;
1085 if (nc
->peer
->info
->link_status_changed
) {
1086 nc
->peer
->info
->link_status_changed(nc
->peer
);
1091 void net_cleanup(void)
1095 /* We may del multiple entries during qemu_del_net_client(),
1096 * so QTAILQ_FOREACH_SAFE() is also not safe here.
1098 while (!QTAILQ_EMPTY(&net_clients
)) {
1099 nc
= QTAILQ_FIRST(&net_clients
);
1100 if (nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_NIC
) {
1101 qemu_del_nic(qemu_get_nic(nc
));
1103 qemu_del_net_client(nc
);
1108 void net_check_clients(void)
1113 /* Don't warn about the default network setup that you get if
1114 * no command line -net or -netdev options are specified. There
1115 * are two cases that we would otherwise complain about:
1116 * (1) board doesn't support a NIC but the implicit "-net nic"
1118 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
1119 * sets up a nic that isn't connected to anything.
1125 net_hub_check_clients();
1127 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
1129 fprintf(stderr
, "Warning: %s %s has no peer\n",
1130 nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_NIC
?
1131 "nic" : "netdev", nc
->name
);
1135 /* Check that all NICs requested via -net nic actually got created.
1136 * NICs created via -device don't need to be checked here because
1137 * they are always instantiated.
1139 for (i
= 0; i
< MAX_NICS
; i
++) {
1140 NICInfo
*nd
= &nd_table
[i
];
1141 if (nd
->used
&& !nd
->instantiated
) {
1142 fprintf(stderr
, "Warning: requested NIC (%s, model %s) "
1143 "was not created (not supported by this machine?)\n",
1144 nd
->name
? nd
->name
: "anonymous",
1145 nd
->model
? nd
->model
: "unspecified");
1150 static int net_init_client(QemuOpts
*opts
, void *dummy
)
1152 Error
*local_err
= NULL
;
1154 net_client_init(opts
, 0, &local_err
);
1156 qerror_report_err(local_err
);
1157 error_free(local_err
);
1164 static int net_init_netdev(QemuOpts
*opts
, void *dummy
)
1166 Error
*local_err
= NULL
;
1169 ret
= net_client_init(opts
, 1, &local_err
);
1171 qerror_report_err(local_err
);
1172 error_free(local_err
);
1179 int net_init_clients(void)
1181 QemuOptsList
*net
= qemu_find_opts("net");
1184 /* if no clients, we use a default config */
1185 qemu_opts_set(net
, NULL
, "type", "nic");
1187 qemu_opts_set(net
, NULL
, "type", "user");
1191 QTAILQ_INIT(&net_clients
);
1193 if (qemu_opts_foreach(qemu_find_opts("netdev"), net_init_netdev
, NULL
, 1) == -1)
1196 if (qemu_opts_foreach(net
, net_init_client
, NULL
, 1) == -1) {
1203 int net_client_parse(QemuOptsList
*opts_list
, const char *optarg
)
1205 #if defined(CONFIG_SLIRP)
1207 if (net_slirp_parse_legacy(opts_list
, optarg
, &ret
)) {
1212 if (!qemu_opts_parse(opts_list
, optarg
, 1)) {
1222 unsigned compute_mcast_idx(const uint8_t *ep
)
1229 for (i
= 0; i
< 6; i
++) {
1231 for (j
= 0; j
< 8; j
++) {
1232 carry
= ((crc
& 0x80000000L
) ? 1 : 0) ^ (b
& 0x01);
1236 crc
= ((crc
^ POLYNOMIAL
) | carry
);
1243 QemuOptsList qemu_netdev_opts
= {
1245 .implied_opt_name
= "type",
1246 .head
= QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts
.head
),
1249 * no elements => accept any params
1250 * validation will happen later
1252 { /* end of list */ }
1256 QemuOptsList qemu_net_opts
= {
1258 .implied_opt_name
= "type",
1259 .head
= QTAILQ_HEAD_INITIALIZER(qemu_net_opts
.head
),
1262 * no elements => accept any params
1263 * validation will happen later
1265 { /* end of list */ }