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"
32 #include "monitor/monitor.h"
33 #include "qemu-common.h"
34 #include "qemu/sockets.h"
35 #include "qemu/config-file.h"
36 #include "qmp-commands.h"
39 #include "qapi-visit.h"
40 #include "qapi/opts-visitor.h"
41 #include "qapi/dealloc-visitor.h"
43 /* Net bridge is currently not supported for W32. */
45 # define CONFIG_NET_BRIDGE
48 static QTAILQ_HEAD(, NetClientState
) net_clients
;
52 /***********************************************************/
53 /* network device redirectors */
55 #if defined(DEBUG_NET)
56 static void hex_dump(FILE *f
, const uint8_t *buf
, int size
)
60 for(i
=0;i
<size
;i
+=16) {
64 fprintf(f
, "%08x ", i
);
67 fprintf(f
, " %02x", buf
[i
+j
]);
74 if (c
< ' ' || c
> '~')
83 static int get_str_sep(char *buf
, int buf_size
, const char **pp
, int sep
)
94 if (len
> buf_size
- 1)
103 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
)
111 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
113 saddr
->sin_family
= AF_INET
;
114 if (buf
[0] == '\0') {
115 saddr
->sin_addr
.s_addr
= 0;
117 if (qemu_isdigit(buf
[0])) {
118 if (!inet_aton(buf
, &saddr
->sin_addr
))
121 if ((he
= gethostbyname(buf
)) == NULL
)
123 saddr
->sin_addr
= *(struct in_addr
*)he
->h_addr
;
126 port
= strtol(p
, (char **)&r
, 0);
129 saddr
->sin_port
= htons(port
);
133 void qemu_format_nic_info_str(NetClientState
*nc
, uint8_t macaddr
[6])
135 snprintf(nc
->info_str
, sizeof(nc
->info_str
),
136 "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
138 macaddr
[0], macaddr
[1], macaddr
[2],
139 macaddr
[3], macaddr
[4], macaddr
[5]);
142 void qemu_macaddr_default_if_unset(MACAddr
*macaddr
)
144 static int index
= 0;
145 static const MACAddr zero
= { .a
= { 0,0,0,0,0,0 } };
147 if (memcmp(macaddr
, &zero
, sizeof(zero
)) != 0)
149 macaddr
->a
[0] = 0x52;
150 macaddr
->a
[1] = 0x54;
151 macaddr
->a
[2] = 0x00;
152 macaddr
->a
[3] = 0x12;
153 macaddr
->a
[4] = 0x34;
154 macaddr
->a
[5] = 0x56 + index
++;
158 * Generate a name for net client
160 * Only net clients created with the legacy -net option and NICs need this.
162 static char *assign_name(NetClientState
*nc1
, const char *model
)
168 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
172 if (strcmp(nc
->model
, model
) == 0) {
177 snprintf(buf
, sizeof(buf
), "%s.%d", model
, id
);
179 return g_strdup(buf
);
182 static void qemu_net_client_destructor(NetClientState
*nc
)
187 static void qemu_net_client_setup(NetClientState
*nc
,
189 NetClientState
*peer
,
192 NetClientDestructor
*destructor
)
195 nc
->model
= g_strdup(model
);
197 nc
->name
= g_strdup(name
);
199 nc
->name
= assign_name(nc
, model
);
207 QTAILQ_INSERT_TAIL(&net_clients
, nc
, next
);
209 nc
->send_queue
= qemu_new_net_queue(nc
);
210 nc
->destructor
= destructor
;
213 NetClientState
*qemu_new_net_client(NetClientInfo
*info
,
214 NetClientState
*peer
,
220 assert(info
->size
>= sizeof(NetClientState
));
222 nc
= g_malloc0(info
->size
);
223 qemu_net_client_setup(nc
, info
, peer
, model
, name
,
224 qemu_net_client_destructor
);
229 NICState
*qemu_new_nic(NetClientInfo
*info
,
235 NetClientState
**peers
= conf
->peers
.ncs
;
237 int i
, queues
= MAX(1, conf
->queues
);
239 assert(info
->type
== NET_CLIENT_OPTIONS_KIND_NIC
);
240 assert(info
->size
>= sizeof(NICState
));
242 nic
= g_malloc0(info
->size
+ sizeof(NetClientState
) * queues
);
243 nic
->ncs
= (void *)nic
+ info
->size
;
245 nic
->opaque
= opaque
;
247 for (i
= 0; i
< queues
; i
++) {
248 qemu_net_client_setup(&nic
->ncs
[i
], info
, peers
[i
], model
, name
,
250 nic
->ncs
[i
].queue_index
= i
;
256 NetClientState
*qemu_get_subqueue(NICState
*nic
, int queue_index
)
258 return nic
->ncs
+ queue_index
;
261 NetClientState
*qemu_get_queue(NICState
*nic
)
263 return qemu_get_subqueue(nic
, 0);
266 NICState
*qemu_get_nic(NetClientState
*nc
)
268 NetClientState
*nc0
= nc
- nc
->queue_index
;
270 return (NICState
*)((void *)nc0
- nc
->info
->size
);
273 void *qemu_get_nic_opaque(NetClientState
*nc
)
275 NICState
*nic
= qemu_get_nic(nc
);
280 static void qemu_cleanup_net_client(NetClientState
*nc
)
282 QTAILQ_REMOVE(&net_clients
, nc
, next
);
284 if (nc
->info
->cleanup
) {
285 nc
->info
->cleanup(nc
);
289 static void qemu_free_net_client(NetClientState
*nc
)
291 if (nc
->send_queue
) {
292 qemu_del_net_queue(nc
->send_queue
);
295 nc
->peer
->peer
= NULL
;
299 if (nc
->destructor
) {
304 void qemu_del_net_client(NetClientState
*nc
)
306 NetClientState
*ncs
[MAX_QUEUE_NUM
];
309 /* If the NetClientState belongs to a multiqueue backend, we will change all
310 * other NetClientStates also.
312 queues
= qemu_find_net_clients_except(nc
->name
, ncs
,
313 NET_CLIENT_OPTIONS_KIND_NIC
,
317 /* If there is a peer NIC, delete and cleanup client, but do not free. */
318 if (nc
->peer
&& nc
->peer
->info
->type
== NET_CLIENT_OPTIONS_KIND_NIC
) {
319 NICState
*nic
= qemu_get_nic(nc
->peer
);
320 if (nic
->peer_deleted
) {
323 nic
->peer_deleted
= true;
325 for (i
= 0; i
< queues
; i
++) {
326 ncs
[i
]->peer
->link_down
= true;
329 if (nc
->peer
->info
->link_status_changed
) {
330 nc
->peer
->info
->link_status_changed(nc
->peer
);
333 for (i
= 0; i
< queues
; i
++) {
334 qemu_cleanup_net_client(ncs
[i
]);
340 assert(nc
->info
->type
!= NET_CLIENT_OPTIONS_KIND_NIC
);
342 for (i
= 0; i
< queues
; i
++) {
343 qemu_cleanup_net_client(ncs
[i
]);
344 qemu_free_net_client(ncs
[i
]);
348 void qemu_del_nic(NICState
*nic
)
350 int i
, queues
= MAX(nic
->conf
->queues
, 1);
352 /* If this is a peer NIC and peer has already been deleted, free it now. */
353 if (nic
->peer_deleted
) {
354 for (i
= 0; i
< queues
; i
++) {
355 qemu_free_net_client(qemu_get_subqueue(nic
, i
)->peer
);
359 for (i
= queues
- 1; i
>= 0; i
--) {
360 NetClientState
*nc
= qemu_get_subqueue(nic
, i
);
362 qemu_cleanup_net_client(nc
);
363 qemu_free_net_client(nc
);
369 void qemu_foreach_nic(qemu_nic_foreach func
, void *opaque
)
373 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
374 if (nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_NIC
) {
375 if (nc
->queue_index
== 0) {
376 func(qemu_get_nic(nc
), opaque
);
382 int qemu_can_send_packet(NetClientState
*sender
)
388 if (sender
->peer
->receive_disabled
) {
390 } else if (sender
->peer
->info
->can_receive
&&
391 !sender
->peer
->info
->can_receive(sender
->peer
)) {
397 ssize_t
qemu_deliver_packet(NetClientState
*sender
,
403 NetClientState
*nc
= opaque
;
410 if (nc
->receive_disabled
) {
414 if (flags
& QEMU_NET_PACKET_FLAG_RAW
&& nc
->info
->receive_raw
) {
415 ret
= nc
->info
->receive_raw(nc
, data
, size
);
417 ret
= nc
->info
->receive(nc
, data
, size
);
421 nc
->receive_disabled
= 1;
427 void qemu_purge_queued_packets(NetClientState
*nc
)
433 qemu_net_queue_purge(nc
->peer
->send_queue
, nc
);
436 void qemu_flush_queued_packets(NetClientState
*nc
)
438 nc
->receive_disabled
= 0;
440 if (nc
->peer
&& nc
->peer
->info
->type
== NET_CLIENT_OPTIONS_KIND_HUBPORT
) {
441 if (net_hub_flush(nc
->peer
)) {
446 if (qemu_net_queue_flush(nc
->send_queue
)) {
447 /* We emptied the queue successfully, signal to the IO thread to repoll
448 * the file descriptor (for tap, for example).
454 static ssize_t
qemu_send_packet_async_with_flags(NetClientState
*sender
,
456 const uint8_t *buf
, int size
,
457 NetPacketSent
*sent_cb
)
462 printf("qemu_send_packet_async:\n");
463 hex_dump(stdout
, buf
, size
);
466 if (sender
->link_down
|| !sender
->peer
) {
470 queue
= sender
->peer
->send_queue
;
472 return qemu_net_queue_send(queue
, sender
, flags
, buf
, size
, sent_cb
);
475 ssize_t
qemu_send_packet_async(NetClientState
*sender
,
476 const uint8_t *buf
, int size
,
477 NetPacketSent
*sent_cb
)
479 return qemu_send_packet_async_with_flags(sender
, QEMU_NET_PACKET_FLAG_NONE
,
483 void qemu_send_packet(NetClientState
*nc
, const uint8_t *buf
, int size
)
485 qemu_send_packet_async(nc
, buf
, size
, NULL
);
488 ssize_t
qemu_send_packet_raw(NetClientState
*nc
, const uint8_t *buf
, int size
)
490 return qemu_send_packet_async_with_flags(nc
, QEMU_NET_PACKET_FLAG_RAW
,
494 static ssize_t
nc_sendv_compat(NetClientState
*nc
, const struct iovec
*iov
,
497 uint8_t buffer
[NET_BUFSIZE
];
500 offset
= iov_to_buf(iov
, iovcnt
, 0, buffer
, sizeof(buffer
));
502 return nc
->info
->receive(nc
, buffer
, offset
);
505 ssize_t
qemu_deliver_packet_iov(NetClientState
*sender
,
507 const struct iovec
*iov
,
511 NetClientState
*nc
= opaque
;
515 return iov_size(iov
, iovcnt
);
518 if (nc
->receive_disabled
) {
522 if (nc
->info
->receive_iov
) {
523 ret
= nc
->info
->receive_iov(nc
, iov
, iovcnt
);
525 ret
= nc_sendv_compat(nc
, iov
, iovcnt
);
529 nc
->receive_disabled
= 1;
535 ssize_t
qemu_sendv_packet_async(NetClientState
*sender
,
536 const struct iovec
*iov
, int iovcnt
,
537 NetPacketSent
*sent_cb
)
541 if (sender
->link_down
|| !sender
->peer
) {
542 return iov_size(iov
, iovcnt
);
545 queue
= sender
->peer
->send_queue
;
547 return qemu_net_queue_send_iov(queue
, sender
,
548 QEMU_NET_PACKET_FLAG_NONE
,
549 iov
, iovcnt
, sent_cb
);
553 qemu_sendv_packet(NetClientState
*nc
, const struct iovec
*iov
, int iovcnt
)
555 return qemu_sendv_packet_async(nc
, iov
, iovcnt
, NULL
);
558 NetClientState
*qemu_find_netdev(const char *id
)
562 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
563 if (nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_NIC
)
565 if (!strcmp(nc
->name
, id
)) {
573 int qemu_find_net_clients_except(const char *id
, NetClientState
**ncs
,
574 NetClientOptionsKind type
, int max
)
579 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
580 if (nc
->info
->type
== type
) {
583 if (!strcmp(nc
->name
, id
)) {
594 static int nic_get_free_idx(void)
598 for (index
= 0; index
< MAX_NICS
; index
++)
599 if (!nd_table
[index
].used
)
604 int qemu_show_nic_models(const char *arg
, const char *const *models
)
608 if (!arg
|| !is_help_option(arg
)) {
612 fprintf(stderr
, "qemu: Supported NIC models: ");
613 for (i
= 0 ; models
[i
]; i
++)
614 fprintf(stderr
, "%s%c", models
[i
], models
[i
+1] ? ',' : '\n');
618 void qemu_check_nic_model(NICInfo
*nd
, const char *model
)
620 const char *models
[2];
625 if (qemu_show_nic_models(nd
->model
, models
))
627 if (qemu_find_nic_model(nd
, models
, model
) < 0)
631 int qemu_find_nic_model(NICInfo
*nd
, const char * const *models
,
632 const char *default_model
)
637 nd
->model
= g_strdup(default_model
);
639 for (i
= 0 ; models
[i
]; i
++) {
640 if (strcmp(nd
->model
, models
[i
]) == 0)
644 error_report("Unsupported NIC model: %s", nd
->model
);
648 static int net_init_nic(const NetClientOptions
*opts
, const char *name
,
649 NetClientState
*peer
)
653 const NetLegacyNicOptions
*nic
;
655 assert(opts
->kind
== NET_CLIENT_OPTIONS_KIND_NIC
);
658 idx
= nic_get_free_idx();
659 if (idx
== -1 || nb_nics
>= MAX_NICS
) {
660 error_report("Too Many NICs");
666 memset(nd
, 0, sizeof(*nd
));
668 if (nic
->has_netdev
) {
669 nd
->netdev
= qemu_find_netdev(nic
->netdev
);
671 error_report("netdev '%s' not found", nic
->netdev
);
678 nd
->name
= g_strdup(name
);
679 if (nic
->has_model
) {
680 nd
->model
= g_strdup(nic
->model
);
683 nd
->devaddr
= g_strdup(nic
->addr
);
686 if (nic
->has_macaddr
&&
687 net_parse_macaddr(nd
->macaddr
.a
, nic
->macaddr
) < 0) {
688 error_report("invalid syntax for ethernet address");
691 qemu_macaddr_default_if_unset(&nd
->macaddr
);
693 if (nic
->has_vectors
) {
694 if (nic
->vectors
> 0x7ffffff) {
695 error_report("invalid # of vectors: %"PRIu32
, nic
->vectors
);
698 nd
->nvectors
= nic
->vectors
;
700 nd
->nvectors
= DEV_NVECTORS_UNSPECIFIED
;
710 static int (* const net_client_init_fun
[NET_CLIENT_OPTIONS_KIND_MAX
])(
711 const NetClientOptions
*opts
,
713 NetClientState
*peer
) = {
714 [NET_CLIENT_OPTIONS_KIND_NIC
] = net_init_nic
,
716 [NET_CLIENT_OPTIONS_KIND_USER
] = net_init_slirp
,
718 [NET_CLIENT_OPTIONS_KIND_TAP
] = net_init_tap
,
719 [NET_CLIENT_OPTIONS_KIND_SOCKET
] = net_init_socket
,
721 [NET_CLIENT_OPTIONS_KIND_VDE
] = net_init_vde
,
723 [NET_CLIENT_OPTIONS_KIND_DUMP
] = net_init_dump
,
724 #ifdef CONFIG_NET_BRIDGE
725 [NET_CLIENT_OPTIONS_KIND_BRIDGE
] = net_init_bridge
,
727 [NET_CLIENT_OPTIONS_KIND_HUBPORT
] = net_init_hubport
,
731 static int net_client_init1(const void *object
, int is_netdev
, Error
**errp
)
734 const Netdev
*netdev
;
735 const NetLegacy
*net
;
737 const NetClientOptions
*opts
;
742 opts
= u
.netdev
->opts
;
745 switch (opts
->kind
) {
747 case NET_CLIENT_OPTIONS_KIND_USER
:
749 case NET_CLIENT_OPTIONS_KIND_TAP
:
750 case NET_CLIENT_OPTIONS_KIND_SOCKET
:
752 case NET_CLIENT_OPTIONS_KIND_VDE
:
754 #ifdef CONFIG_NET_BRIDGE
755 case NET_CLIENT_OPTIONS_KIND_BRIDGE
:
757 case NET_CLIENT_OPTIONS_KIND_HUBPORT
:
761 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, "type",
762 "a netdev backend type");
768 /* missing optional values have been initialized to "all bits zero" */
769 name
= u
.net
->has_id
? u
.net
->id
: u
.net
->name
;
772 if (net_client_init_fun
[opts
->kind
]) {
773 NetClientState
*peer
= NULL
;
775 /* Do not add to a vlan if it's a -netdev or a nic with a netdev=
778 (opts
->kind
!= NET_CLIENT_OPTIONS_KIND_NIC
||
779 !opts
->nic
->has_netdev
)) {
780 peer
= net_hub_add_port(u
.net
->has_vlan
? u
.net
->vlan
: 0, NULL
);
783 if (net_client_init_fun
[opts
->kind
](opts
, name
, peer
) < 0) {
784 /* TODO push error reporting into init() methods */
785 error_set(errp
, QERR_DEVICE_INIT_FAILED
,
786 NetClientOptionsKind_lookup
[opts
->kind
]);
794 static void net_visit(Visitor
*v
, int is_netdev
, void **object
, Error
**errp
)
797 visit_type_Netdev(v
, (Netdev
**)object
, NULL
, errp
);
799 visit_type_NetLegacy(v
, (NetLegacy
**)object
, NULL
, errp
);
804 int net_client_init(QemuOpts
*opts
, int is_netdev
, Error
**errp
)
811 OptsVisitor
*ov
= opts_visitor_new(opts
);
813 net_visit(opts_get_visitor(ov
), is_netdev
, &object
, &err
);
814 opts_visitor_cleanup(ov
);
818 ret
= net_client_init1(object
, is_netdev
, &err
);
822 QapiDeallocVisitor
*dv
= qapi_dealloc_visitor_new();
824 net_visit(qapi_dealloc_get_visitor(dv
), is_netdev
, &object
, NULL
);
825 qapi_dealloc_visitor_cleanup(dv
);
828 error_propagate(errp
, err
);
833 static int net_host_check_device(const char *device
)
836 const char *valid_param_list
[] = { "tap", "socket", "dump"
837 #ifdef CONFIG_NET_BRIDGE
847 for (i
= 0; i
< sizeof(valid_param_list
) / sizeof(char *); i
++) {
848 if (!strncmp(valid_param_list
[i
], device
,
849 strlen(valid_param_list
[i
])))
856 void net_host_device_add(Monitor
*mon
, const QDict
*qdict
)
858 const char *device
= qdict_get_str(qdict
, "device");
859 const char *opts_str
= qdict_get_try_str(qdict
, "opts");
860 Error
*local_err
= NULL
;
863 if (!net_host_check_device(device
)) {
864 monitor_printf(mon
, "invalid host network device %s\n", device
);
868 opts
= qemu_opts_parse(qemu_find_opts("net"), opts_str
? opts_str
: "", 0);
873 qemu_opt_set(opts
, "type", device
);
875 net_client_init(opts
, 0, &local_err
);
876 if (error_is_set(&local_err
)) {
877 qerror_report_err(local_err
);
878 error_free(local_err
);
879 monitor_printf(mon
, "adding host network device %s failed\n", device
);
883 void net_host_device_remove(Monitor
*mon
, const QDict
*qdict
)
886 int vlan_id
= qdict_get_int(qdict
, "vlan_id");
887 const char *device
= qdict_get_str(qdict
, "device");
889 nc
= net_hub_find_client_by_name(vlan_id
, device
);
893 if (!net_host_check_device(nc
->model
)) {
894 monitor_printf(mon
, "invalid host network device %s\n", device
);
897 qemu_del_net_client(nc
);
900 void netdev_add(QemuOpts
*opts
, Error
**errp
)
902 net_client_init(opts
, 1, errp
);
905 int qmp_netdev_add(Monitor
*mon
, const QDict
*qdict
, QObject
**ret
)
907 Error
*local_err
= NULL
;
908 QemuOptsList
*opts_list
;
911 opts_list
= qemu_find_opts_err("netdev", &local_err
);
912 if (error_is_set(&local_err
)) {
916 opts
= qemu_opts_from_qdict(opts_list
, qdict
, &local_err
);
917 if (error_is_set(&local_err
)) {
921 netdev_add(opts
, &local_err
);
922 if (error_is_set(&local_err
)) {
930 qerror_report_err(local_err
);
931 error_free(local_err
);
935 void qmp_netdev_del(const char *id
, Error
**errp
)
940 nc
= qemu_find_netdev(id
);
942 error_set(errp
, QERR_DEVICE_NOT_FOUND
, id
);
946 opts
= qemu_opts_find(qemu_find_opts_err("netdev", NULL
), id
);
948 error_setg(errp
, "Device '%s' is not a netdev", id
);
952 qemu_del_net_client(nc
);
956 void print_net_client(Monitor
*mon
, NetClientState
*nc
)
958 monitor_printf(mon
, "%s: index=%d,type=%s,%s\n", nc
->name
,
960 NetClientOptionsKind_lookup
[nc
->info
->type
],
964 RxFilterInfoList
*qmp_query_rx_filter(bool has_name
, const char *name
,
968 RxFilterInfoList
*filter_list
= NULL
, *last_entry
= NULL
;
970 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
971 RxFilterInfoList
*entry
;
974 if (has_name
&& strcmp(nc
->name
, name
) != 0) {
978 /* only query rx-filter information of NIC */
979 if (nc
->info
->type
!= NET_CLIENT_OPTIONS_KIND_NIC
) {
981 error_setg(errp
, "net client(%s) isn't a NIC", name
);
987 if (nc
->info
->query_rx_filter
) {
988 info
= nc
->info
->query_rx_filter(nc
);
989 entry
= g_malloc0(sizeof(*entry
));
995 last_entry
->next
= entry
;
998 } else if (has_name
) {
999 error_setg(errp
, "net client(%s) doesn't support"
1000 " rx-filter querying", name
);
1005 if (filter_list
== NULL
&& !error_is_set(errp
) && has_name
) {
1006 error_setg(errp
, "invalid net client name: %s", name
);
1012 void do_info_network(Monitor
*mon
, const QDict
*qdict
)
1014 NetClientState
*nc
, *peer
;
1015 NetClientOptionsKind type
;
1019 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
1021 type
= nc
->info
->type
;
1023 /* Skip if already printed in hub info */
1024 if (net_hub_id_for_client(nc
, NULL
) == 0) {
1028 if (!peer
|| type
== NET_CLIENT_OPTIONS_KIND_NIC
) {
1029 print_net_client(mon
, nc
);
1030 } /* else it's a netdev connected to a NIC, printed with the NIC */
1031 if (peer
&& type
== NET_CLIENT_OPTIONS_KIND_NIC
) {
1032 monitor_printf(mon
, " \\ ");
1033 print_net_client(mon
, peer
);
1038 void qmp_set_link(const char *name
, bool up
, Error
**errp
)
1040 NetClientState
*ncs
[MAX_QUEUE_NUM
];
1044 queues
= qemu_find_net_clients_except(name
, ncs
,
1045 NET_CLIENT_OPTIONS_KIND_MAX
,
1049 error_set(errp
, QERR_DEVICE_NOT_FOUND
, name
);
1054 for (i
= 0; i
< queues
; i
++) {
1055 ncs
[i
]->link_down
= !up
;
1058 if (nc
->info
->link_status_changed
) {
1059 nc
->info
->link_status_changed(nc
);
1062 /* Notify peer. Don't update peer link status: this makes it possible to
1063 * disconnect from host network without notifying the guest.
1064 * FIXME: is disconnected link status change operation useful?
1066 * Current behaviour is compatible with qemu vlans where there could be
1067 * multiple clients that can still communicate with each other in
1068 * disconnected mode. For now maintain this compatibility. */
1069 if (nc
->peer
&& nc
->peer
->info
->link_status_changed
) {
1070 nc
->peer
->info
->link_status_changed(nc
->peer
);
1074 void net_cleanup(void)
1078 /* We may del multiple entries during qemu_del_net_client(),
1079 * so QTAILQ_FOREACH_SAFE() is also not safe here.
1081 while (!QTAILQ_EMPTY(&net_clients
)) {
1082 nc
= QTAILQ_FIRST(&net_clients
);
1083 if (nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_NIC
) {
1084 qemu_del_nic(qemu_get_nic(nc
));
1086 qemu_del_net_client(nc
);
1091 void net_check_clients(void)
1096 /* Don't warn about the default network setup that you get if
1097 * no command line -net or -netdev options are specified. There
1098 * are two cases that we would otherwise complain about:
1099 * (1) board doesn't support a NIC but the implicit "-net nic"
1101 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
1102 * sets up a nic that isn't connected to anything.
1108 net_hub_check_clients();
1110 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
1112 fprintf(stderr
, "Warning: %s %s has no peer\n",
1113 nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_NIC
?
1114 "nic" : "netdev", nc
->name
);
1118 /* Check that all NICs requested via -net nic actually got created.
1119 * NICs created via -device don't need to be checked here because
1120 * they are always instantiated.
1122 for (i
= 0; i
< MAX_NICS
; i
++) {
1123 NICInfo
*nd
= &nd_table
[i
];
1124 if (nd
->used
&& !nd
->instantiated
) {
1125 fprintf(stderr
, "Warning: requested NIC (%s, model %s) "
1126 "was not created (not supported by this machine?)\n",
1127 nd
->name
? nd
->name
: "anonymous",
1128 nd
->model
? nd
->model
: "unspecified");
1133 static int net_init_client(QemuOpts
*opts
, void *dummy
)
1135 Error
*local_err
= NULL
;
1137 net_client_init(opts
, 0, &local_err
);
1138 if (error_is_set(&local_err
)) {
1139 qerror_report_err(local_err
);
1140 error_free(local_err
);
1147 static int net_init_netdev(QemuOpts
*opts
, void *dummy
)
1149 Error
*local_err
= NULL
;
1152 ret
= net_client_init(opts
, 1, &local_err
);
1153 if (error_is_set(&local_err
)) {
1154 qerror_report_err(local_err
);
1155 error_free(local_err
);
1162 int net_init_clients(void)
1164 QemuOptsList
*net
= qemu_find_opts("net");
1167 /* if no clients, we use a default config */
1168 qemu_opts_set(net
, NULL
, "type", "nic");
1170 qemu_opts_set(net
, NULL
, "type", "user");
1174 QTAILQ_INIT(&net_clients
);
1176 if (qemu_opts_foreach(qemu_find_opts("netdev"), net_init_netdev
, NULL
, 1) == -1)
1179 if (qemu_opts_foreach(net
, net_init_client
, NULL
, 1) == -1) {
1186 int net_client_parse(QemuOptsList
*opts_list
, const char *optarg
)
1188 #if defined(CONFIG_SLIRP)
1190 if (net_slirp_parse_legacy(opts_list
, optarg
, &ret
)) {
1195 if (!qemu_opts_parse(opts_list
, optarg
, 1)) {
1205 unsigned compute_mcast_idx(const uint8_t *ep
)
1212 for (i
= 0; i
< 6; i
++) {
1214 for (j
= 0; j
< 8; j
++) {
1215 carry
= ((crc
& 0x80000000L
) ? 1 : 0) ^ (b
& 0x01);
1219 crc
= ((crc
^ POLYNOMIAL
) | carry
);
1226 QemuOptsList qemu_netdev_opts
= {
1228 .implied_opt_name
= "type",
1229 .head
= QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts
.head
),
1232 * no elements => accept any params
1233 * validation will happen later
1235 { /* end of list */ }
1239 QemuOptsList qemu_net_opts
= {
1241 .implied_opt_name
= "type",
1242 .head
= QTAILQ_HEAD_INITIALIZER(qemu_net_opts
.head
),
1245 * no elements => accept any params
1246 * validation will happen later
1248 { /* end of list */ }