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 need this. Naming is
161 * mandatory for net clients created with -netdev.
163 static char *assign_name(NetClientState
*nc1
, const char *model
)
169 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
173 /* For compatibility only bump id for net clients on a vlan */
174 if (strcmp(nc
->model
, model
) == 0 &&
175 net_hub_id_for_client(nc
, NULL
) == 0) {
180 snprintf(buf
, sizeof(buf
), "%s.%d", model
, id
);
182 return g_strdup(buf
);
185 static void qemu_net_client_destructor(NetClientState
*nc
)
190 static void qemu_net_client_setup(NetClientState
*nc
,
192 NetClientState
*peer
,
195 NetClientDestructor
*destructor
)
198 nc
->model
= g_strdup(model
);
200 nc
->name
= g_strdup(name
);
202 nc
->name
= assign_name(nc
, model
);
210 QTAILQ_INSERT_TAIL(&net_clients
, nc
, next
);
212 nc
->send_queue
= qemu_new_net_queue(nc
);
213 nc
->destructor
= destructor
;
216 NetClientState
*qemu_new_net_client(NetClientInfo
*info
,
217 NetClientState
*peer
,
223 assert(info
->size
>= sizeof(NetClientState
));
225 nc
= g_malloc0(info
->size
);
226 qemu_net_client_setup(nc
, info
, peer
, model
, name
,
227 qemu_net_client_destructor
);
232 NICState
*qemu_new_nic(NetClientInfo
*info
,
239 NetClientState
**peers
= conf
->peers
.ncs
;
243 assert(info
->type
== NET_CLIENT_OPTIONS_KIND_NIC
);
244 assert(info
->size
>= sizeof(NICState
));
246 nc
= qemu_new_net_client(info
, peers
[0], model
, name
);
249 nic
= qemu_get_nic(nc
);
251 nic
->opaque
= opaque
;
253 for (i
= 1; i
< conf
->queues
; i
++) {
254 qemu_net_client_setup(&nic
->ncs
[i
], info
, peers
[i
], model
, nc
->name
,
256 nic
->ncs
[i
].queue_index
= i
;
262 NetClientState
*qemu_get_subqueue(NICState
*nic
, int queue_index
)
264 return &nic
->ncs
[queue_index
];
267 NetClientState
*qemu_get_queue(NICState
*nic
)
269 return qemu_get_subqueue(nic
, 0);
272 NICState
*qemu_get_nic(NetClientState
*nc
)
274 NetClientState
*nc0
= nc
- nc
->queue_index
;
276 return DO_UPCAST(NICState
, ncs
[0], nc0
);
279 void *qemu_get_nic_opaque(NetClientState
*nc
)
281 NICState
*nic
= qemu_get_nic(nc
);
286 static void qemu_cleanup_net_client(NetClientState
*nc
)
288 QTAILQ_REMOVE(&net_clients
, nc
, next
);
290 if (nc
->info
->cleanup
) {
291 nc
->info
->cleanup(nc
);
295 static void qemu_free_net_client(NetClientState
*nc
)
297 if (nc
->send_queue
) {
298 qemu_del_net_queue(nc
->send_queue
);
301 nc
->peer
->peer
= NULL
;
305 if (nc
->destructor
) {
310 void qemu_del_net_client(NetClientState
*nc
)
312 NetClientState
*ncs
[MAX_QUEUE_NUM
];
315 /* If the NetClientState belongs to a multiqueue backend, we will change all
316 * other NetClientStates also.
318 queues
= qemu_find_net_clients_except(nc
->name
, ncs
,
319 NET_CLIENT_OPTIONS_KIND_NIC
,
323 /* If there is a peer NIC, delete and cleanup client, but do not free. */
324 if (nc
->peer
&& nc
->peer
->info
->type
== NET_CLIENT_OPTIONS_KIND_NIC
) {
325 NICState
*nic
= qemu_get_nic(nc
->peer
);
326 if (nic
->peer_deleted
) {
329 nic
->peer_deleted
= true;
331 for (i
= 0; i
< queues
; i
++) {
332 ncs
[i
]->peer
->link_down
= true;
335 if (nc
->peer
->info
->link_status_changed
) {
336 nc
->peer
->info
->link_status_changed(nc
->peer
);
339 for (i
= 0; i
< queues
; i
++) {
340 qemu_cleanup_net_client(ncs
[i
]);
346 assert(nc
->info
->type
!= NET_CLIENT_OPTIONS_KIND_NIC
);
348 for (i
= 0; i
< queues
; i
++) {
349 qemu_cleanup_net_client(ncs
[i
]);
350 qemu_free_net_client(ncs
[i
]);
354 void qemu_del_nic(NICState
*nic
)
356 int i
, queues
= MAX(nic
->conf
->queues
, 1);
358 /* If this is a peer NIC and peer has already been deleted, free it now. */
359 if (nic
->peer_deleted
) {
360 for (i
= 0; i
< queues
; i
++) {
361 qemu_free_net_client(qemu_get_subqueue(nic
, i
)->peer
);
365 for (i
= queues
- 1; i
>= 0; i
--) {
366 NetClientState
*nc
= qemu_get_subqueue(nic
, i
);
368 qemu_cleanup_net_client(nc
);
369 qemu_free_net_client(nc
);
373 void qemu_foreach_nic(qemu_nic_foreach func
, void *opaque
)
377 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
378 if (nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_NIC
) {
379 if (nc
->queue_index
== 0) {
380 func(qemu_get_nic(nc
), opaque
);
386 int qemu_can_send_packet(NetClientState
*sender
)
392 if (sender
->peer
->receive_disabled
) {
394 } else if (sender
->peer
->info
->can_receive
&&
395 !sender
->peer
->info
->can_receive(sender
->peer
)) {
401 ssize_t
qemu_deliver_packet(NetClientState
*sender
,
407 NetClientState
*nc
= opaque
;
414 if (nc
->receive_disabled
) {
418 if (flags
& QEMU_NET_PACKET_FLAG_RAW
&& nc
->info
->receive_raw
) {
419 ret
= nc
->info
->receive_raw(nc
, data
, size
);
421 ret
= nc
->info
->receive(nc
, data
, size
);
425 nc
->receive_disabled
= 1;
431 void qemu_purge_queued_packets(NetClientState
*nc
)
437 qemu_net_queue_purge(nc
->peer
->send_queue
, nc
);
440 void qemu_flush_queued_packets(NetClientState
*nc
)
442 nc
->receive_disabled
= 0;
444 if (nc
->peer
&& nc
->peer
->info
->type
== NET_CLIENT_OPTIONS_KIND_HUBPORT
) {
445 if (net_hub_flush(nc
->peer
)) {
450 if (qemu_net_queue_flush(nc
->send_queue
)) {
451 /* We emptied the queue successfully, signal to the IO thread to repoll
452 * the file descriptor (for tap, for example).
458 static ssize_t
qemu_send_packet_async_with_flags(NetClientState
*sender
,
460 const uint8_t *buf
, int size
,
461 NetPacketSent
*sent_cb
)
466 printf("qemu_send_packet_async:\n");
467 hex_dump(stdout
, buf
, size
);
470 if (sender
->link_down
|| !sender
->peer
) {
474 queue
= sender
->peer
->send_queue
;
476 return qemu_net_queue_send(queue
, sender
, flags
, buf
, size
, sent_cb
);
479 ssize_t
qemu_send_packet_async(NetClientState
*sender
,
480 const uint8_t *buf
, int size
,
481 NetPacketSent
*sent_cb
)
483 return qemu_send_packet_async_with_flags(sender
, QEMU_NET_PACKET_FLAG_NONE
,
487 void qemu_send_packet(NetClientState
*nc
, const uint8_t *buf
, int size
)
489 qemu_send_packet_async(nc
, buf
, size
, NULL
);
492 ssize_t
qemu_send_packet_raw(NetClientState
*nc
, const uint8_t *buf
, int size
)
494 return qemu_send_packet_async_with_flags(nc
, QEMU_NET_PACKET_FLAG_RAW
,
498 static ssize_t
nc_sendv_compat(NetClientState
*nc
, const struct iovec
*iov
,
501 uint8_t buffer
[4096];
504 offset
= iov_to_buf(iov
, iovcnt
, 0, buffer
, sizeof(buffer
));
506 return nc
->info
->receive(nc
, buffer
, offset
);
509 ssize_t
qemu_deliver_packet_iov(NetClientState
*sender
,
511 const struct iovec
*iov
,
515 NetClientState
*nc
= opaque
;
519 return iov_size(iov
, iovcnt
);
522 if (nc
->receive_disabled
) {
526 if (nc
->info
->receive_iov
) {
527 ret
= nc
->info
->receive_iov(nc
, iov
, iovcnt
);
529 ret
= nc_sendv_compat(nc
, iov
, iovcnt
);
533 nc
->receive_disabled
= 1;
539 ssize_t
qemu_sendv_packet_async(NetClientState
*sender
,
540 const struct iovec
*iov
, int iovcnt
,
541 NetPacketSent
*sent_cb
)
545 if (sender
->link_down
|| !sender
->peer
) {
546 return iov_size(iov
, iovcnt
);
549 queue
= sender
->peer
->send_queue
;
551 return qemu_net_queue_send_iov(queue
, sender
,
552 QEMU_NET_PACKET_FLAG_NONE
,
553 iov
, iovcnt
, sent_cb
);
557 qemu_sendv_packet(NetClientState
*nc
, const struct iovec
*iov
, int iovcnt
)
559 return qemu_sendv_packet_async(nc
, iov
, iovcnt
, NULL
);
562 NetClientState
*qemu_find_netdev(const char *id
)
566 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
567 if (nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_NIC
)
569 if (!strcmp(nc
->name
, id
)) {
577 int qemu_find_net_clients_except(const char *id
, NetClientState
**ncs
,
578 NetClientOptionsKind type
, int max
)
583 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
584 if (nc
->info
->type
== type
) {
587 if (!strcmp(nc
->name
, id
)) {
598 static int nic_get_free_idx(void)
602 for (index
= 0; index
< MAX_NICS
; index
++)
603 if (!nd_table
[index
].used
)
608 int qemu_show_nic_models(const char *arg
, const char *const *models
)
612 if (!arg
|| !is_help_option(arg
)) {
616 fprintf(stderr
, "qemu: Supported NIC models: ");
617 for (i
= 0 ; models
[i
]; i
++)
618 fprintf(stderr
, "%s%c", models
[i
], models
[i
+1] ? ',' : '\n');
622 void qemu_check_nic_model(NICInfo
*nd
, const char *model
)
624 const char *models
[2];
629 if (qemu_show_nic_models(nd
->model
, models
))
631 if (qemu_find_nic_model(nd
, models
, model
) < 0)
635 int qemu_find_nic_model(NICInfo
*nd
, const char * const *models
,
636 const char *default_model
)
641 nd
->model
= g_strdup(default_model
);
643 for (i
= 0 ; models
[i
]; i
++) {
644 if (strcmp(nd
->model
, models
[i
]) == 0)
648 error_report("Unsupported NIC model: %s", nd
->model
);
652 static int net_init_nic(const NetClientOptions
*opts
, const char *name
,
653 NetClientState
*peer
)
657 const NetLegacyNicOptions
*nic
;
659 assert(opts
->kind
== NET_CLIENT_OPTIONS_KIND_NIC
);
662 idx
= nic_get_free_idx();
663 if (idx
== -1 || nb_nics
>= MAX_NICS
) {
664 error_report("Too Many NICs");
670 memset(nd
, 0, sizeof(*nd
));
672 if (nic
->has_netdev
) {
673 nd
->netdev
= qemu_find_netdev(nic
->netdev
);
675 error_report("netdev '%s' not found", nic
->netdev
);
682 nd
->name
= g_strdup(name
);
683 if (nic
->has_model
) {
684 nd
->model
= g_strdup(nic
->model
);
687 nd
->devaddr
= g_strdup(nic
->addr
);
690 if (nic
->has_macaddr
&&
691 net_parse_macaddr(nd
->macaddr
.a
, nic
->macaddr
) < 0) {
692 error_report("invalid syntax for ethernet address");
695 qemu_macaddr_default_if_unset(&nd
->macaddr
);
697 if (nic
->has_vectors
) {
698 if (nic
->vectors
> 0x7ffffff) {
699 error_report("invalid # of vectors: %"PRIu32
, nic
->vectors
);
702 nd
->nvectors
= nic
->vectors
;
704 nd
->nvectors
= DEV_NVECTORS_UNSPECIFIED
;
714 static int (* const net_client_init_fun
[NET_CLIENT_OPTIONS_KIND_MAX
])(
715 const NetClientOptions
*opts
,
717 NetClientState
*peer
) = {
718 [NET_CLIENT_OPTIONS_KIND_NIC
] = net_init_nic
,
720 [NET_CLIENT_OPTIONS_KIND_USER
] = net_init_slirp
,
722 [NET_CLIENT_OPTIONS_KIND_TAP
] = net_init_tap
,
723 [NET_CLIENT_OPTIONS_KIND_SOCKET
] = net_init_socket
,
725 [NET_CLIENT_OPTIONS_KIND_VDE
] = net_init_vde
,
727 [NET_CLIENT_OPTIONS_KIND_DUMP
] = net_init_dump
,
728 #ifdef CONFIG_NET_BRIDGE
729 [NET_CLIENT_OPTIONS_KIND_BRIDGE
] = net_init_bridge
,
731 [NET_CLIENT_OPTIONS_KIND_HUBPORT
] = net_init_hubport
,
735 static int net_client_init1(const void *object
, int is_netdev
, Error
**errp
)
738 const Netdev
*netdev
;
739 const NetLegacy
*net
;
741 const NetClientOptions
*opts
;
746 opts
= u
.netdev
->opts
;
749 switch (opts
->kind
) {
751 case NET_CLIENT_OPTIONS_KIND_USER
:
753 case NET_CLIENT_OPTIONS_KIND_TAP
:
754 case NET_CLIENT_OPTIONS_KIND_SOCKET
:
756 case NET_CLIENT_OPTIONS_KIND_VDE
:
758 #ifdef CONFIG_NET_BRIDGE
759 case NET_CLIENT_OPTIONS_KIND_BRIDGE
:
761 case NET_CLIENT_OPTIONS_KIND_HUBPORT
:
765 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, "type",
766 "a netdev backend type");
772 /* missing optional values have been initialized to "all bits zero" */
773 name
= u
.net
->has_id
? u
.net
->id
: u
.net
->name
;
776 if (net_client_init_fun
[opts
->kind
]) {
777 NetClientState
*peer
= NULL
;
779 /* Do not add to a vlan if it's a -netdev or a nic with a netdev=
782 (opts
->kind
!= NET_CLIENT_OPTIONS_KIND_NIC
||
783 !opts
->nic
->has_netdev
)) {
784 peer
= net_hub_add_port(u
.net
->has_vlan
? u
.net
->vlan
: 0, NULL
);
787 if (net_client_init_fun
[opts
->kind
](opts
, name
, peer
) < 0) {
788 /* TODO push error reporting into init() methods */
789 error_set(errp
, QERR_DEVICE_INIT_FAILED
,
790 NetClientOptionsKind_lookup
[opts
->kind
]);
798 static void net_visit(Visitor
*v
, int is_netdev
, void **object
, Error
**errp
)
801 visit_type_Netdev(v
, (Netdev
**)object
, NULL
, errp
);
803 visit_type_NetLegacy(v
, (NetLegacy
**)object
, NULL
, errp
);
808 int net_client_init(QemuOpts
*opts
, int is_netdev
, Error
**errp
)
815 OptsVisitor
*ov
= opts_visitor_new(opts
);
817 net_visit(opts_get_visitor(ov
), is_netdev
, &object
, &err
);
818 opts_visitor_cleanup(ov
);
822 ret
= net_client_init1(object
, is_netdev
, &err
);
826 QapiDeallocVisitor
*dv
= qapi_dealloc_visitor_new();
828 net_visit(qapi_dealloc_get_visitor(dv
), is_netdev
, &object
, NULL
);
829 qapi_dealloc_visitor_cleanup(dv
);
832 error_propagate(errp
, err
);
837 static int net_host_check_device(const char *device
)
840 const char *valid_param_list
[] = { "tap", "socket", "dump"
841 #ifdef CONFIG_NET_BRIDGE
851 for (i
= 0; i
< sizeof(valid_param_list
) / sizeof(char *); i
++) {
852 if (!strncmp(valid_param_list
[i
], device
,
853 strlen(valid_param_list
[i
])))
860 void net_host_device_add(Monitor
*mon
, const QDict
*qdict
)
862 const char *device
= qdict_get_str(qdict
, "device");
863 const char *opts_str
= qdict_get_try_str(qdict
, "opts");
864 Error
*local_err
= NULL
;
867 if (!net_host_check_device(device
)) {
868 monitor_printf(mon
, "invalid host network device %s\n", device
);
872 opts
= qemu_opts_parse(qemu_find_opts("net"), opts_str
? opts_str
: "", 0);
877 qemu_opt_set(opts
, "type", device
);
879 net_client_init(opts
, 0, &local_err
);
880 if (error_is_set(&local_err
)) {
881 qerror_report_err(local_err
);
882 error_free(local_err
);
883 monitor_printf(mon
, "adding host network device %s failed\n", device
);
887 void net_host_device_remove(Monitor
*mon
, const QDict
*qdict
)
890 int vlan_id
= qdict_get_int(qdict
, "vlan_id");
891 const char *device
= qdict_get_str(qdict
, "device");
893 nc
= net_hub_find_client_by_name(vlan_id
, device
);
897 if (!net_host_check_device(nc
->model
)) {
898 monitor_printf(mon
, "invalid host network device %s\n", device
);
901 qemu_del_net_client(nc
);
904 void netdev_add(QemuOpts
*opts
, Error
**errp
)
906 net_client_init(opts
, 1, errp
);
909 int qmp_netdev_add(Monitor
*mon
, const QDict
*qdict
, QObject
**ret
)
911 Error
*local_err
= NULL
;
912 QemuOptsList
*opts_list
;
915 opts_list
= qemu_find_opts_err("netdev", &local_err
);
916 if (error_is_set(&local_err
)) {
920 opts
= qemu_opts_from_qdict(opts_list
, qdict
, &local_err
);
921 if (error_is_set(&local_err
)) {
925 netdev_add(opts
, &local_err
);
926 if (error_is_set(&local_err
)) {
934 qerror_report_err(local_err
);
935 error_free(local_err
);
939 void qmp_netdev_del(const char *id
, Error
**errp
)
944 nc
= qemu_find_netdev(id
);
946 error_set(errp
, QERR_DEVICE_NOT_FOUND
, id
);
950 opts
= qemu_opts_find(qemu_find_opts_err("netdev", NULL
), id
);
952 error_setg(errp
, "Device '%s' is not a netdev", id
);
956 qemu_del_net_client(nc
);
960 void print_net_client(Monitor
*mon
, NetClientState
*nc
)
962 monitor_printf(mon
, "%s: index=%d,type=%s,%s\n", nc
->name
,
964 NetClientOptionsKind_lookup
[nc
->info
->type
],
968 void do_info_network(Monitor
*mon
, const QDict
*qdict
)
970 NetClientState
*nc
, *peer
;
971 NetClientOptionsKind type
;
975 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
977 type
= nc
->info
->type
;
979 /* Skip if already printed in hub info */
980 if (net_hub_id_for_client(nc
, NULL
) == 0) {
984 if (!peer
|| type
== NET_CLIENT_OPTIONS_KIND_NIC
) {
985 print_net_client(mon
, nc
);
986 } /* else it's a netdev connected to a NIC, printed with the NIC */
987 if (peer
&& type
== NET_CLIENT_OPTIONS_KIND_NIC
) {
988 monitor_printf(mon
, " \\ ");
989 print_net_client(mon
, peer
);
994 void qmp_set_link(const char *name
, bool up
, Error
**errp
)
996 NetClientState
*ncs
[MAX_QUEUE_NUM
];
1000 queues
= qemu_find_net_clients_except(name
, ncs
,
1001 NET_CLIENT_OPTIONS_KIND_MAX
,
1005 error_set(errp
, QERR_DEVICE_NOT_FOUND
, name
);
1010 for (i
= 0; i
< queues
; i
++) {
1011 ncs
[i
]->link_down
= !up
;
1014 if (nc
->info
->link_status_changed
) {
1015 nc
->info
->link_status_changed(nc
);
1018 /* Notify peer. Don't update peer link status: this makes it possible to
1019 * disconnect from host network without notifying the guest.
1020 * FIXME: is disconnected link status change operation useful?
1022 * Current behaviour is compatible with qemu vlans where there could be
1023 * multiple clients that can still communicate with each other in
1024 * disconnected mode. For now maintain this compatibility. */
1025 if (nc
->peer
&& nc
->peer
->info
->link_status_changed
) {
1026 nc
->peer
->info
->link_status_changed(nc
->peer
);
1030 void net_cleanup(void)
1034 /* We may del multiple entries during qemu_del_net_client(),
1035 * so QTAILQ_FOREACH_SAFE() is also not safe here.
1037 while (!QTAILQ_EMPTY(&net_clients
)) {
1038 nc
= QTAILQ_FIRST(&net_clients
);
1039 if (nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_NIC
) {
1040 qemu_del_nic(qemu_get_nic(nc
));
1042 qemu_del_net_client(nc
);
1047 void net_check_clients(void)
1052 /* Don't warn about the default network setup that you get if
1053 * no command line -net or -netdev options are specified. There
1054 * are two cases that we would otherwise complain about:
1055 * (1) board doesn't support a NIC but the implicit "-net nic"
1057 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
1058 * sets up a nic that isn't connected to anything.
1064 net_hub_check_clients();
1066 QTAILQ_FOREACH(nc
, &net_clients
, next
) {
1068 fprintf(stderr
, "Warning: %s %s has no peer\n",
1069 nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_NIC
?
1070 "nic" : "netdev", nc
->name
);
1074 /* Check that all NICs requested via -net nic actually got created.
1075 * NICs created via -device don't need to be checked here because
1076 * they are always instantiated.
1078 for (i
= 0; i
< MAX_NICS
; i
++) {
1079 NICInfo
*nd
= &nd_table
[i
];
1080 if (nd
->used
&& !nd
->instantiated
) {
1081 fprintf(stderr
, "Warning: requested NIC (%s, model %s) "
1082 "was not created (not supported by this machine?)\n",
1083 nd
->name
? nd
->name
: "anonymous",
1084 nd
->model
? nd
->model
: "unspecified");
1089 static int net_init_client(QemuOpts
*opts
, void *dummy
)
1091 Error
*local_err
= NULL
;
1093 net_client_init(opts
, 0, &local_err
);
1094 if (error_is_set(&local_err
)) {
1095 qerror_report_err(local_err
);
1096 error_free(local_err
);
1103 static int net_init_netdev(QemuOpts
*opts
, void *dummy
)
1105 Error
*local_err
= NULL
;
1108 ret
= net_client_init(opts
, 1, &local_err
);
1109 if (error_is_set(&local_err
)) {
1110 qerror_report_err(local_err
);
1111 error_free(local_err
);
1118 int net_init_clients(void)
1120 QemuOptsList
*net
= qemu_find_opts("net");
1123 /* if no clients, we use a default config */
1124 qemu_opts_set(net
, NULL
, "type", "nic");
1126 qemu_opts_set(net
, NULL
, "type", "user");
1130 QTAILQ_INIT(&net_clients
);
1132 if (qemu_opts_foreach(qemu_find_opts("netdev"), net_init_netdev
, NULL
, 1) == -1)
1135 if (qemu_opts_foreach(net
, net_init_client
, NULL
, 1) == -1) {
1142 int net_client_parse(QemuOptsList
*opts_list
, const char *optarg
)
1144 #if defined(CONFIG_SLIRP)
1146 if (net_slirp_parse_legacy(opts_list
, optarg
, &ret
)) {
1151 if (!qemu_opts_parse(opts_list
, optarg
, 1)) {
1161 unsigned compute_mcast_idx(const uint8_t *ep
)
1168 for (i
= 0; i
< 6; i
++) {
1170 for (j
= 0; j
< 8; j
++) {
1171 carry
= ((crc
& 0x80000000L
) ? 1 : 0) ^ (b
& 0x01);
1175 crc
= ((crc
^ POLYNOMIAL
) | carry
);
1182 QemuOptsList qemu_netdev_opts
= {
1184 .implied_opt_name
= "type",
1185 .head
= QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts
.head
),
1188 * no elements => accept any params
1189 * validation will happen later
1191 { /* end of list */ }
1195 QemuOptsList qemu_net_opts
= {
1197 .implied_opt_name
= "type",
1198 .head
= QTAILQ_HEAD_INITIALIZER(qemu_net_opts
.head
),
1201 * no elements => accept any params
1202 * validation will happen later
1204 { /* end of list */ }