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
;
52 const char *host_net_devices
[] = {
56 #ifdef CONFIG_NET_BRIDGE
70 /***********************************************************/
71 /* network device redirectors */
73 #if defined(DEBUG_NET)
74 static void hex_dump(FILE *f
, const uint8_t *buf
, int size
)
78 for(i
=0;i
<size
;i
+=16) {
82 fprintf(f
, "%08x ", i
);
85 fprintf(f
, " %02x", buf
[i
+j
]);
92 if (c
< ' ' || c
> '~')
101 static int get_str_sep(char *buf
, int buf_size
, const char **pp
, int sep
)
112 if (len
> buf_size
- 1)
121 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
)
129 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
131 saddr
->sin_family
= AF_INET
;
132 if (buf
[0] == '\0') {
133 saddr
->sin_addr
.s_addr
= 0;
135 if (qemu_isdigit(buf
[0])) {
136 if (!inet_aton(buf
, &saddr
->sin_addr
))
139 if ((he
= gethostbyname(buf
)) == NULL
)
141 saddr
->sin_addr
= *(struct in_addr
*)he
->h_addr
;
144 port
= strtol(p
, (char **)&r
, 0);
147 saddr
->sin_port
= htons(port
);
151 void qemu_format_nic_info_str(NetClientState
*nc
, uint8_t macaddr
[6])
153 snprintf(nc
->info_str
, sizeof(nc
->info_str
),
154 "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
156 macaddr
[0], macaddr
[1], macaddr
[2],
157 macaddr
[3], macaddr
[4], macaddr
[5]);
160 void qemu_macaddr_default_if_unset(MACAddr
*macaddr
)
162 static int index
= 0;
163 static const MACAddr zero
= { .a
= { 0,0,0,0,0,0 } };
165 if (memcmp(macaddr
, &zero
, sizeof(zero
)) != 0)
167 macaddr
->a
[0] = 0x52;
168 macaddr
->a
[1] = 0x54;
169 macaddr
->a
[2] = 0x00;
170 macaddr
->a
[3] = 0x12;
171 macaddr
->a
[4] = 0x34;
172 macaddr
->a
[5] = 0x56 + index
++;
176 * Generate a name for net client
178 * Only net clients created with the legacy -net option and NICs need this.
180 static char *assign_name(NetClientState
*nc1
, const char *model
)
185 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
189 if (strcmp(nc
->model
, model
) == 0) {
194 return g_strdup_printf("%s.%d", model
, id
);
197 static void qemu_net_client_destructor(NetClientState
*nc
)
202 static void qemu_net_client_setup(NetClientState
*nc
,
204 NetClientState
*peer
,
207 NetClientDestructor
*destructor
)
210 nc
->model
= g_strdup(model
);
212 nc
->name
= g_strdup(name
);
214 nc
->name
= assign_name(nc
, model
);
222 QTAILQ_INSERT_TAIL(&net_clients
, nc
, next
);
224 nc
->incoming_queue
= qemu_new_net_queue(nc
);
225 nc
->destructor
= destructor
;
228 NetClientState
*qemu_new_net_client(NetClientInfo
*info
,
229 NetClientState
*peer
,
235 assert(info
->size
>= sizeof(NetClientState
));
237 nc
= g_malloc0(info
->size
);
238 qemu_net_client_setup(nc
, info
, peer
, model
, name
,
239 qemu_net_client_destructor
);
244 NICState
*qemu_new_nic(NetClientInfo
*info
,
250 NetClientState
**peers
= conf
->peers
.ncs
;
252 int i
, queues
= MAX(1, conf
->queues
);
254 assert(info
->type
== NET_CLIENT_OPTIONS_KIND_NIC
);
255 assert(info
->size
>= sizeof(NICState
));
257 nic
= g_malloc0(info
->size
+ sizeof(NetClientState
) * queues
);
258 nic
->ncs
= (void *)nic
+ info
->size
;
260 nic
->opaque
= opaque
;
262 for (i
= 0; i
< queues
; i
++) {
263 qemu_net_client_setup(&nic
->ncs
[i
], info
, peers
[i
], model
, name
,
265 nic
->ncs
[i
].queue_index
= i
;
271 NetClientState
*qemu_get_subqueue(NICState
*nic
, int queue_index
)
273 return nic
->ncs
+ queue_index
;
276 NetClientState
*qemu_get_queue(NICState
*nic
)
278 return qemu_get_subqueue(nic
, 0);
281 NICState
*qemu_get_nic(NetClientState
*nc
)
283 NetClientState
*nc0
= nc
- nc
->queue_index
;
285 return (NICState
*)((void *)nc0
- nc
->info
->size
);
288 void *qemu_get_nic_opaque(NetClientState
*nc
)
290 NICState
*nic
= qemu_get_nic(nc
);
295 static void qemu_cleanup_net_client(NetClientState
*nc
)
297 QTAILQ_REMOVE(&net_clients
, nc
, next
);
299 if (nc
->info
->cleanup
) {
300 nc
->info
->cleanup(nc
);
304 static void qemu_free_net_client(NetClientState
*nc
)
306 if (nc
->incoming_queue
) {
307 qemu_del_net_queue(nc
->incoming_queue
);
310 nc
->peer
->peer
= NULL
;
314 if (nc
->destructor
) {
319 void qemu_del_net_client(NetClientState
*nc
)
321 NetClientState
*ncs
[MAX_QUEUE_NUM
];
324 /* If the NetClientState belongs to a multiqueue backend, we will change all
325 * other NetClientStates also.
327 queues
= qemu_find_net_clients_except(nc
->name
, ncs
,
328 NET_CLIENT_OPTIONS_KIND_NIC
,
332 /* If there is a peer NIC, delete and cleanup client, but do not free. */
333 if (nc
->peer
&& nc
->peer
->info
->type
== NET_CLIENT_OPTIONS_KIND_NIC
) {
334 NICState
*nic
= qemu_get_nic(nc
->peer
);
335 if (nic
->peer_deleted
) {
338 nic
->peer_deleted
= true;
340 for (i
= 0; i
< queues
; i
++) {
341 ncs
[i
]->peer
->link_down
= true;
344 if (nc
->peer
->info
->link_status_changed
) {
345 nc
->peer
->info
->link_status_changed(nc
->peer
);
348 for (i
= 0; i
< queues
; i
++) {
349 qemu_cleanup_net_client(ncs
[i
]);
355 assert(nc
->info
->type
!= NET_CLIENT_OPTIONS_KIND_NIC
);
357 for (i
= 0; i
< queues
; i
++) {
358 qemu_cleanup_net_client(ncs
[i
]);
359 qemu_free_net_client(ncs
[i
]);
363 void qemu_del_nic(NICState
*nic
)
365 int i
, queues
= MAX(nic
->conf
->queues
, 1);
367 /* If this is a peer NIC and peer has already been deleted, free it now. */
368 if (nic
->peer_deleted
) {
369 for (i
= 0; i
< queues
; i
++) {
370 qemu_free_net_client(qemu_get_subqueue(nic
, i
)->peer
);
374 for (i
= queues
- 1; i
>= 0; i
--) {
375 NetClientState
*nc
= qemu_get_subqueue(nic
, i
);
377 qemu_cleanup_net_client(nc
);
378 qemu_free_net_client(nc
);
384 void qemu_foreach_nic(qemu_nic_foreach func
, void *opaque
)
388 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
389 if (nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_NIC
) {
390 if (nc
->queue_index
== 0) {
391 func(qemu_get_nic(nc
), opaque
);
397 bool qemu_has_ufo(NetClientState
*nc
)
399 if (!nc
|| !nc
->info
->has_ufo
) {
403 return nc
->info
->has_ufo(nc
);
406 bool qemu_has_vnet_hdr(NetClientState
*nc
)
408 if (!nc
|| !nc
->info
->has_vnet_hdr
) {
412 return nc
->info
->has_vnet_hdr(nc
);
415 bool qemu_has_vnet_hdr_len(NetClientState
*nc
, int len
)
417 if (!nc
|| !nc
->info
->has_vnet_hdr_len
) {
421 return nc
->info
->has_vnet_hdr_len(nc
, len
);
424 void qemu_using_vnet_hdr(NetClientState
*nc
, bool enable
)
426 if (!nc
|| !nc
->info
->using_vnet_hdr
) {
430 nc
->info
->using_vnet_hdr(nc
, enable
);
433 void qemu_set_offload(NetClientState
*nc
, int csum
, int tso4
, int tso6
,
436 if (!nc
|| !nc
->info
->set_offload
) {
440 nc
->info
->set_offload(nc
, csum
, tso4
, tso6
, ecn
, ufo
);
443 void qemu_set_vnet_hdr_len(NetClientState
*nc
, int len
)
445 if (!nc
|| !nc
->info
->set_vnet_hdr_len
) {
449 nc
->info
->set_vnet_hdr_len(nc
, len
);
452 int qemu_can_send_packet(NetClientState
*sender
)
458 if (sender
->peer
->receive_disabled
) {
460 } else if (sender
->peer
->info
->can_receive
&&
461 !sender
->peer
->info
->can_receive(sender
->peer
)) {
467 ssize_t
qemu_deliver_packet(NetClientState
*sender
,
473 NetClientState
*nc
= opaque
;
480 if (nc
->receive_disabled
) {
484 if (flags
& QEMU_NET_PACKET_FLAG_RAW
&& nc
->info
->receive_raw
) {
485 ret
= nc
->info
->receive_raw(nc
, data
, size
);
487 ret
= nc
->info
->receive(nc
, data
, size
);
491 nc
->receive_disabled
= 1;
497 void qemu_purge_queued_packets(NetClientState
*nc
)
503 qemu_net_queue_purge(nc
->peer
->incoming_queue
, nc
);
506 void qemu_flush_queued_packets(NetClientState
*nc
)
508 nc
->receive_disabled
= 0;
510 if (nc
->peer
&& nc
->peer
->info
->type
== NET_CLIENT_OPTIONS_KIND_HUBPORT
) {
511 if (net_hub_flush(nc
->peer
)) {
515 if (qemu_net_queue_flush(nc
->incoming_queue
)) {
516 /* We emptied the queue successfully, signal to the IO thread to repoll
517 * the file descriptor (for tap, for example).
523 static ssize_t
qemu_send_packet_async_with_flags(NetClientState
*sender
,
525 const uint8_t *buf
, int size
,
526 NetPacketSent
*sent_cb
)
531 printf("qemu_send_packet_async:\n");
532 hex_dump(stdout
, buf
, size
);
535 if (sender
->link_down
|| !sender
->peer
) {
539 queue
= sender
->peer
->incoming_queue
;
541 return qemu_net_queue_send(queue
, sender
, flags
, buf
, size
, sent_cb
);
544 ssize_t
qemu_send_packet_async(NetClientState
*sender
,
545 const uint8_t *buf
, int size
,
546 NetPacketSent
*sent_cb
)
548 return qemu_send_packet_async_with_flags(sender
, QEMU_NET_PACKET_FLAG_NONE
,
552 void qemu_send_packet(NetClientState
*nc
, const uint8_t *buf
, int size
)
554 qemu_send_packet_async(nc
, buf
, size
, NULL
);
557 ssize_t
qemu_send_packet_raw(NetClientState
*nc
, const uint8_t *buf
, int size
)
559 return qemu_send_packet_async_with_flags(nc
, QEMU_NET_PACKET_FLAG_RAW
,
563 static ssize_t
nc_sendv_compat(NetClientState
*nc
, const struct iovec
*iov
,
566 uint8_t buffer
[NET_BUFSIZE
];
569 offset
= iov_to_buf(iov
, iovcnt
, 0, buffer
, sizeof(buffer
));
571 return nc
->info
->receive(nc
, buffer
, offset
);
574 ssize_t
qemu_deliver_packet_iov(NetClientState
*sender
,
576 const struct iovec
*iov
,
580 NetClientState
*nc
= opaque
;
584 return iov_size(iov
, iovcnt
);
587 if (nc
->receive_disabled
) {
591 if (nc
->info
->receive_iov
) {
592 ret
= nc
->info
->receive_iov(nc
, iov
, iovcnt
);
594 ret
= nc_sendv_compat(nc
, iov
, iovcnt
);
598 nc
->receive_disabled
= 1;
604 ssize_t
qemu_sendv_packet_async(NetClientState
*sender
,
605 const struct iovec
*iov
, int iovcnt
,
606 NetPacketSent
*sent_cb
)
610 if (sender
->link_down
|| !sender
->peer
) {
611 return iov_size(iov
, iovcnt
);
614 queue
= sender
->peer
->incoming_queue
;
616 return qemu_net_queue_send_iov(queue
, sender
,
617 QEMU_NET_PACKET_FLAG_NONE
,
618 iov
, iovcnt
, sent_cb
);
622 qemu_sendv_packet(NetClientState
*nc
, const struct iovec
*iov
, int iovcnt
)
624 return qemu_sendv_packet_async(nc
, iov
, iovcnt
, NULL
);
627 NetClientState
*qemu_find_netdev(const char *id
)
631 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
632 if (nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_NIC
)
634 if (!strcmp(nc
->name
, id
)) {
642 int qemu_find_net_clients_except(const char *id
, NetClientState
**ncs
,
643 NetClientOptionsKind type
, int max
)
648 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
649 if (nc
->info
->type
== type
) {
652 if (!id
|| !strcmp(nc
->name
, id
)) {
663 static int nic_get_free_idx(void)
667 for (index
= 0; index
< MAX_NICS
; index
++)
668 if (!nd_table
[index
].used
)
673 int qemu_show_nic_models(const char *arg
, const char *const *models
)
677 if (!arg
|| !is_help_option(arg
)) {
681 fprintf(stderr
, "qemu: Supported NIC models: ");
682 for (i
= 0 ; models
[i
]; i
++)
683 fprintf(stderr
, "%s%c", models
[i
], models
[i
+1] ? ',' : '\n');
687 void qemu_check_nic_model(NICInfo
*nd
, const char *model
)
689 const char *models
[2];
694 if (qemu_show_nic_models(nd
->model
, models
))
696 if (qemu_find_nic_model(nd
, models
, model
) < 0)
700 int qemu_find_nic_model(NICInfo
*nd
, const char * const *models
,
701 const char *default_model
)
706 nd
->model
= g_strdup(default_model
);
708 for (i
= 0 ; models
[i
]; i
++) {
709 if (strcmp(nd
->model
, models
[i
]) == 0)
713 error_report("Unsupported NIC model: %s", nd
->model
);
717 static int net_init_nic(const NetClientOptions
*opts
, const char *name
,
718 NetClientState
*peer
)
722 const NetLegacyNicOptions
*nic
;
724 assert(opts
->kind
== NET_CLIENT_OPTIONS_KIND_NIC
);
727 idx
= nic_get_free_idx();
728 if (idx
== -1 || nb_nics
>= MAX_NICS
) {
729 error_report("Too Many NICs");
735 memset(nd
, 0, sizeof(*nd
));
737 if (nic
->has_netdev
) {
738 nd
->netdev
= qemu_find_netdev(nic
->netdev
);
740 error_report("netdev '%s' not found", nic
->netdev
);
747 nd
->name
= g_strdup(name
);
748 if (nic
->has_model
) {
749 nd
->model
= g_strdup(nic
->model
);
752 nd
->devaddr
= g_strdup(nic
->addr
);
755 if (nic
->has_macaddr
&&
756 net_parse_macaddr(nd
->macaddr
.a
, nic
->macaddr
) < 0) {
757 error_report("invalid syntax for ethernet address");
760 if (nic
->has_macaddr
&&
761 is_multicast_ether_addr(nd
->macaddr
.a
)) {
762 error_report("NIC cannot have multicast MAC address (odd 1st byte)");
765 qemu_macaddr_default_if_unset(&nd
->macaddr
);
767 if (nic
->has_vectors
) {
768 if (nic
->vectors
> 0x7ffffff) {
769 error_report("invalid # of vectors: %"PRIu32
, nic
->vectors
);
772 nd
->nvectors
= nic
->vectors
;
774 nd
->nvectors
= DEV_NVECTORS_UNSPECIFIED
;
784 static int (* const net_client_init_fun
[NET_CLIENT_OPTIONS_KIND_MAX
])(
785 const NetClientOptions
*opts
,
787 NetClientState
*peer
) = {
788 [NET_CLIENT_OPTIONS_KIND_NIC
] = net_init_nic
,
790 [NET_CLIENT_OPTIONS_KIND_USER
] = net_init_slirp
,
792 [NET_CLIENT_OPTIONS_KIND_TAP
] = net_init_tap
,
793 [NET_CLIENT_OPTIONS_KIND_SOCKET
] = net_init_socket
,
795 [NET_CLIENT_OPTIONS_KIND_VDE
] = net_init_vde
,
798 [NET_CLIENT_OPTIONS_KIND_NETMAP
] = net_init_netmap
,
800 [NET_CLIENT_OPTIONS_KIND_DUMP
] = net_init_dump
,
801 #ifdef CONFIG_NET_BRIDGE
802 [NET_CLIENT_OPTIONS_KIND_BRIDGE
] = net_init_bridge
,
804 [NET_CLIENT_OPTIONS_KIND_HUBPORT
] = net_init_hubport
,
808 static int net_client_init1(const void *object
, int is_netdev
, Error
**errp
)
811 const Netdev
*netdev
;
812 const NetLegacy
*net
;
814 const NetClientOptions
*opts
;
819 opts
= u
.netdev
->opts
;
822 switch (opts
->kind
) {
824 case NET_CLIENT_OPTIONS_KIND_USER
:
826 case NET_CLIENT_OPTIONS_KIND_TAP
:
827 case NET_CLIENT_OPTIONS_KIND_SOCKET
:
829 case NET_CLIENT_OPTIONS_KIND_VDE
:
832 case NET_CLIENT_OPTIONS_KIND_NETMAP
:
834 #ifdef CONFIG_NET_BRIDGE
835 case NET_CLIENT_OPTIONS_KIND_BRIDGE
:
837 case NET_CLIENT_OPTIONS_KIND_HUBPORT
:
841 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, "type",
842 "a netdev backend type");
848 /* missing optional values have been initialized to "all bits zero" */
849 name
= u
.net
->has_id
? u
.net
->id
: u
.net
->name
;
852 if (net_client_init_fun
[opts
->kind
]) {
853 NetClientState
*peer
= NULL
;
855 /* Do not add to a vlan if it's a -netdev or a nic with a netdev=
858 (opts
->kind
!= NET_CLIENT_OPTIONS_KIND_NIC
||
859 !opts
->nic
->has_netdev
)) {
860 peer
= net_hub_add_port(u
.net
->has_vlan
? u
.net
->vlan
: 0, NULL
);
863 if (net_client_init_fun
[opts
->kind
](opts
, name
, peer
) < 0) {
864 /* TODO push error reporting into init() methods */
865 error_set(errp
, QERR_DEVICE_INIT_FAILED
,
866 NetClientOptionsKind_lookup
[opts
->kind
]);
874 static void net_visit(Visitor
*v
, int is_netdev
, void **object
, Error
**errp
)
877 visit_type_Netdev(v
, (Netdev
**)object
, NULL
, errp
);
879 visit_type_NetLegacy(v
, (NetLegacy
**)object
, NULL
, errp
);
884 int net_client_init(QemuOpts
*opts
, int is_netdev
, Error
**errp
)
891 OptsVisitor
*ov
= opts_visitor_new(opts
);
893 net_visit(opts_get_visitor(ov
), is_netdev
, &object
, &err
);
894 opts_visitor_cleanup(ov
);
898 ret
= net_client_init1(object
, is_netdev
, &err
);
902 QapiDeallocVisitor
*dv
= qapi_dealloc_visitor_new();
904 net_visit(qapi_dealloc_get_visitor(dv
), is_netdev
, &object
, NULL
);
905 qapi_dealloc_visitor_cleanup(dv
);
908 error_propagate(errp
, err
);
913 static int net_host_check_device(const char *device
)
916 for (i
= 0; host_net_devices
[i
]; i
++) {
917 if (!strncmp(host_net_devices
[i
], device
,
918 strlen(host_net_devices
[i
]))) {
926 void net_host_device_add(Monitor
*mon
, const QDict
*qdict
)
928 const char *device
= qdict_get_str(qdict
, "device");
929 const char *opts_str
= qdict_get_try_str(qdict
, "opts");
930 Error
*local_err
= NULL
;
933 if (!net_host_check_device(device
)) {
934 monitor_printf(mon
, "invalid host network device %s\n", device
);
938 opts
= qemu_opts_parse(qemu_find_opts("net"), opts_str
? opts_str
: "", 0);
943 qemu_opt_set(opts
, "type", device
);
945 net_client_init(opts
, 0, &local_err
);
947 qerror_report_err(local_err
);
948 error_free(local_err
);
949 monitor_printf(mon
, "adding host network device %s failed\n", device
);
953 void net_host_device_remove(Monitor
*mon
, const QDict
*qdict
)
956 int vlan_id
= qdict_get_int(qdict
, "vlan_id");
957 const char *device
= qdict_get_str(qdict
, "device");
959 nc
= net_hub_find_client_by_name(vlan_id
, device
);
961 error_report("Host network device '%s' on hub '%d' not found",
965 if (!net_host_check_device(nc
->model
)) {
966 error_report("invalid host network device '%s'", device
);
969 qemu_del_net_client(nc
);
972 void netdev_add(QemuOpts
*opts
, Error
**errp
)
974 net_client_init(opts
, 1, errp
);
977 int qmp_netdev_add(Monitor
*mon
, const QDict
*qdict
, QObject
**ret
)
979 Error
*local_err
= NULL
;
980 QemuOptsList
*opts_list
;
983 opts_list
= qemu_find_opts_err("netdev", &local_err
);
988 opts
= qemu_opts_from_qdict(opts_list
, qdict
, &local_err
);
993 netdev_add(opts
, &local_err
);
1002 qerror_report_err(local_err
);
1003 error_free(local_err
);
1007 void qmp_netdev_del(const char *id
, Error
**errp
)
1012 nc
= qemu_find_netdev(id
);
1014 error_set(errp
, QERR_DEVICE_NOT_FOUND
, id
);
1018 opts
= qemu_opts_find(qemu_find_opts_err("netdev", NULL
), id
);
1020 error_setg(errp
, "Device '%s' is not a netdev", id
);
1024 qemu_del_net_client(nc
);
1025 qemu_opts_del(opts
);
1028 void print_net_client(Monitor
*mon
, NetClientState
*nc
)
1030 monitor_printf(mon
, "%s: index=%d,type=%s,%s\n", nc
->name
,
1032 NetClientOptionsKind_lookup
[nc
->info
->type
],
1036 RxFilterInfoList
*qmp_query_rx_filter(bool has_name
, const char *name
,
1040 RxFilterInfoList
*filter_list
= NULL
, *last_entry
= NULL
;
1042 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
1043 RxFilterInfoList
*entry
;
1046 if (has_name
&& strcmp(nc
->name
, name
) != 0) {
1050 /* only query rx-filter information of NIC */
1051 if (nc
->info
->type
!= NET_CLIENT_OPTIONS_KIND_NIC
) {
1053 error_setg(errp
, "net client(%s) isn't a NIC", name
);
1059 if (nc
->info
->query_rx_filter
) {
1060 info
= nc
->info
->query_rx_filter(nc
);
1061 entry
= g_malloc0(sizeof(*entry
));
1062 entry
->value
= info
;
1065 filter_list
= entry
;
1067 last_entry
->next
= entry
;
1070 } else if (has_name
) {
1071 error_setg(errp
, "net client(%s) doesn't support"
1072 " rx-filter querying", name
);
1081 if (filter_list
== NULL
&& has_name
) {
1082 error_setg(errp
, "invalid net client name: %s", name
);
1088 void do_info_network(Monitor
*mon
, const QDict
*qdict
)
1090 NetClientState
*nc
, *peer
;
1091 NetClientOptionsKind type
;
1095 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
1097 type
= nc
->info
->type
;
1099 /* Skip if already printed in hub info */
1100 if (net_hub_id_for_client(nc
, NULL
) == 0) {
1104 if (!peer
|| type
== NET_CLIENT_OPTIONS_KIND_NIC
) {
1105 print_net_client(mon
, nc
);
1106 } /* else it's a netdev connected to a NIC, printed with the NIC */
1107 if (peer
&& type
== NET_CLIENT_OPTIONS_KIND_NIC
) {
1108 monitor_printf(mon
, " \\ ");
1109 print_net_client(mon
, peer
);
1114 void qmp_set_link(const char *name
, bool up
, Error
**errp
)
1116 NetClientState
*ncs
[MAX_QUEUE_NUM
];
1120 queues
= qemu_find_net_clients_except(name
, ncs
,
1121 NET_CLIENT_OPTIONS_KIND_MAX
,
1125 error_set(errp
, QERR_DEVICE_NOT_FOUND
, name
);
1130 for (i
= 0; i
< queues
; i
++) {
1131 ncs
[i
]->link_down
= !up
;
1134 if (nc
->info
->link_status_changed
) {
1135 nc
->info
->link_status_changed(nc
);
1139 /* Change peer link only if the peer is NIC and then notify peer.
1140 * If the peer is a HUBPORT or a backend, we do not change the
1143 * This behavior is compatible with qemu vlans where there could be
1144 * multiple clients that can still communicate with each other in
1145 * disconnected mode. For now maintain this compatibility.
1147 if (nc
->peer
->info
->type
== NET_CLIENT_OPTIONS_KIND_NIC
) {
1148 for (i
= 0; i
< queues
; i
++) {
1149 ncs
[i
]->peer
->link_down
= !up
;
1152 if (nc
->peer
->info
->link_status_changed
) {
1153 nc
->peer
->info
->link_status_changed(nc
->peer
);
1158 void net_cleanup(void)
1162 /* We may del multiple entries during qemu_del_net_client(),
1163 * so QTAILQ_FOREACH_SAFE() is also not safe here.
1165 while (!QTAILQ_EMPTY(&net_clients
)) {
1166 nc
= QTAILQ_FIRST(&net_clients
);
1167 if (nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_NIC
) {
1168 qemu_del_nic(qemu_get_nic(nc
));
1170 qemu_del_net_client(nc
);
1175 void net_check_clients(void)
1180 /* Don't warn about the default network setup that you get if
1181 * no command line -net or -netdev options are specified. There
1182 * are two cases that we would otherwise complain about:
1183 * (1) board doesn't support a NIC but the implicit "-net nic"
1185 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
1186 * sets up a nic that isn't connected to anything.
1192 net_hub_check_clients();
1194 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
1196 fprintf(stderr
, "Warning: %s %s has no peer\n",
1197 nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_NIC
?
1198 "nic" : "netdev", nc
->name
);
1202 /* Check that all NICs requested via -net nic actually got created.
1203 * NICs created via -device don't need to be checked here because
1204 * they are always instantiated.
1206 for (i
= 0; i
< MAX_NICS
; i
++) {
1207 NICInfo
*nd
= &nd_table
[i
];
1208 if (nd
->used
&& !nd
->instantiated
) {
1209 fprintf(stderr
, "Warning: requested NIC (%s, model %s) "
1210 "was not created (not supported by this machine?)\n",
1211 nd
->name
? nd
->name
: "anonymous",
1212 nd
->model
? nd
->model
: "unspecified");
1217 static int net_init_client(QemuOpts
*opts
, void *dummy
)
1219 Error
*local_err
= NULL
;
1221 net_client_init(opts
, 0, &local_err
);
1223 qerror_report_err(local_err
);
1224 error_free(local_err
);
1231 static int net_init_netdev(QemuOpts
*opts
, void *dummy
)
1233 Error
*local_err
= NULL
;
1236 ret
= net_client_init(opts
, 1, &local_err
);
1238 qerror_report_err(local_err
);
1239 error_free(local_err
);
1246 int net_init_clients(void)
1248 QemuOptsList
*net
= qemu_find_opts("net");
1251 /* if no clients, we use a default config */
1252 qemu_opts_set(net
, NULL
, "type", "nic");
1254 qemu_opts_set(net
, NULL
, "type", "user");
1258 QTAILQ_INIT(&net_clients
);
1260 if (qemu_opts_foreach(qemu_find_opts("netdev"), net_init_netdev
, NULL
, 1) == -1)
1263 if (qemu_opts_foreach(net
, net_init_client
, NULL
, 1) == -1) {
1270 int net_client_parse(QemuOptsList
*opts_list
, const char *optarg
)
1272 #if defined(CONFIG_SLIRP)
1274 if (net_slirp_parse_legacy(opts_list
, optarg
, &ret
)) {
1279 if (!qemu_opts_parse(opts_list
, optarg
, 1)) {
1289 unsigned compute_mcast_idx(const uint8_t *ep
)
1296 for (i
= 0; i
< 6; i
++) {
1298 for (j
= 0; j
< 8; j
++) {
1299 carry
= ((crc
& 0x80000000L
) ? 1 : 0) ^ (b
& 0x01);
1303 crc
= ((crc
^ POLYNOMIAL
) | carry
);
1310 QemuOptsList qemu_netdev_opts
= {
1312 .implied_opt_name
= "type",
1313 .head
= QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts
.head
),
1316 * no elements => accept any params
1317 * validation will happen later
1319 { /* end of list */ }
1323 QemuOptsList qemu_net_opts
= {
1325 .implied_opt_name
= "type",
1326 .head
= QTAILQ_HEAD_INITIALIZER(qemu_net_opts
.head
),
1329 * no elements => accept any params
1330 * validation will happen later
1332 { /* end of list */ }