4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 #include "config-host.h"
29 #include "net/socket.h"
31 #include "net/slirp.h"
35 #include "qemu-common.h"
36 #include "qemu_socket.h"
38 static QTAILQ_HEAD(, VLANState
) vlans
;
39 static QTAILQ_HEAD(, VLANClientState
) non_vlan_clients
;
41 /***********************************************************/
42 /* network device redirectors */
44 #if defined(DEBUG_NET)
45 static void hex_dump(FILE *f
, const uint8_t *buf
, int size
)
49 for(i
=0;i
<size
;i
+=16) {
53 fprintf(f
, "%08x ", i
);
56 fprintf(f
, " %02x", buf
[i
+j
]);
63 if (c
< ' ' || c
> '~')
72 static int parse_macaddr(uint8_t *macaddr
, const char *p
)
79 offset
= strtol(p
, &last_char
, 0);
80 if (0 == errno
&& '\0' == *last_char
&&
81 offset
>= 0 && offset
<= 0xFFFFFF) {
82 macaddr
[3] = (offset
& 0xFF0000) >> 16;
83 macaddr
[4] = (offset
& 0xFF00) >> 8;
84 macaddr
[5] = offset
& 0xFF;
87 for(i
= 0; i
< 6; i
++) {
88 macaddr
[i
] = strtol(p
, (char **)&p
, 16);
93 if (*p
!= ':' && *p
!= '-')
104 static int get_str_sep(char *buf
, int buf_size
, const char **pp
, int sep
)
115 if (len
> buf_size
- 1)
124 int parse_host_src_port(struct sockaddr_in
*haddr
,
125 struct sockaddr_in
*saddr
,
126 const char *input_str
)
128 char *str
= strdup(input_str
);
129 char *host_str
= str
;
131 const char *src_str2
;
135 * Chop off any extra arguments at the end of the string which
136 * would start with a comma, then fill in the src port information
137 * if it was provided else use the "any address" and "any port".
139 if ((ptr
= strchr(str
,',')))
142 if ((src_str
= strchr(input_str
,'@'))) {
147 if (parse_host_port(haddr
, host_str
) < 0)
151 if (!src_str
|| *src_str
== '\0')
154 if (parse_host_port(saddr
, src_str2
) < 0)
165 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
)
173 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
175 saddr
->sin_family
= AF_INET
;
176 if (buf
[0] == '\0') {
177 saddr
->sin_addr
.s_addr
= 0;
179 if (qemu_isdigit(buf
[0])) {
180 if (!inet_aton(buf
, &saddr
->sin_addr
))
183 if ((he
= gethostbyname(buf
)) == NULL
)
185 saddr
->sin_addr
= *(struct in_addr
*)he
->h_addr
;
188 port
= strtol(p
, (char **)&r
, 0);
191 saddr
->sin_port
= htons(port
);
195 void qemu_format_nic_info_str(VLANClientState
*vc
, uint8_t macaddr
[6])
197 snprintf(vc
->info_str
, sizeof(vc
->info_str
),
198 "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
200 macaddr
[0], macaddr
[1], macaddr
[2],
201 macaddr
[3], macaddr
[4], macaddr
[5]);
204 void qemu_macaddr_default_if_unset(MACAddr
*macaddr
)
206 static int index
= 0;
207 static const MACAddr zero
= { .a
= { 0,0,0,0,0,0 } };
209 if (memcmp(macaddr
, &zero
, sizeof(zero
)) != 0)
211 macaddr
->a
[0] = 0x52;
212 macaddr
->a
[1] = 0x54;
213 macaddr
->a
[2] = 0x00;
214 macaddr
->a
[3] = 0x12;
215 macaddr
->a
[4] = 0x34;
216 macaddr
->a
[5] = 0x56 + index
++;
219 static char *assign_name(VLANClientState
*vc1
, const char *model
)
225 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
228 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
229 if (vc
!= vc1
&& strcmp(vc
->model
, model
) == 0) {
235 snprintf(buf
, sizeof(buf
), "%s.%d", model
, id
);
237 return qemu_strdup(buf
);
240 static ssize_t
qemu_deliver_packet(VLANClientState
*sender
,
245 static ssize_t
qemu_deliver_packet_iov(VLANClientState
*sender
,
247 const struct iovec
*iov
,
251 VLANClientState
*qemu_new_vlan_client(net_client_type type
,
253 VLANClientState
*peer
,
256 NetCanReceive
*can_receive
,
258 NetReceive
*receive_raw
,
259 NetReceiveIOV
*receive_iov
,
265 vc
= qemu_mallocz(sizeof(VLANClientState
));
268 vc
->model
= qemu_strdup(model
);
270 vc
->name
= qemu_strdup(name
);
272 vc
->name
= assign_name(vc
, model
);
273 vc
->can_receive
= can_receive
;
274 vc
->receive
= receive
;
275 vc
->receive_raw
= receive_raw
;
276 vc
->receive_iov
= receive_iov
;
277 vc
->cleanup
= cleanup
;
283 QTAILQ_INSERT_TAIL(&vc
->vlan
->clients
, vc
, next
);
289 QTAILQ_INSERT_TAIL(&non_vlan_clients
, vc
, next
);
291 vc
->send_queue
= qemu_new_net_queue(qemu_deliver_packet
,
292 qemu_deliver_packet_iov
,
299 void qemu_del_vlan_client(VLANClientState
*vc
)
302 QTAILQ_REMOVE(&vc
->vlan
->clients
, vc
, next
);
304 if (vc
->send_queue
) {
305 qemu_del_net_queue(vc
->send_queue
);
307 QTAILQ_REMOVE(&non_vlan_clients
, vc
, next
);
309 vc
->peer
->peer
= NULL
;
318 qemu_free(vc
->model
);
322 VLANClientState
*qemu_find_vlan_client(VLANState
*vlan
, void *opaque
)
326 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
327 if (vc
->opaque
== opaque
) {
336 qemu_find_vlan_client_by_name(Monitor
*mon
, int vlan_id
,
337 const char *client_str
)
342 vlan
= qemu_find_vlan(vlan_id
, 0);
344 monitor_printf(mon
, "unknown VLAN %d\n", vlan_id
);
348 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
349 if (!strcmp(vc
->name
, client_str
)) {
354 monitor_printf(mon
, "can't find device %s on VLAN %d\n",
355 client_str
, vlan_id
);
361 int qemu_can_send_packet(VLANClientState
*sender
)
363 VLANState
*vlan
= sender
->vlan
;
367 if (sender
->peer
->receive_disabled
) {
369 } else if (sender
->peer
->can_receive
&&
370 !sender
->peer
->can_receive(sender
->peer
)) {
381 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
386 /* no can_receive() handler, they can always receive */
387 if (!vc
->can_receive
|| vc
->can_receive(vc
)) {
394 static ssize_t
qemu_deliver_packet(VLANClientState
*sender
,
400 VLANClientState
*vc
= opaque
;
407 if (vc
->receive_disabled
) {
411 if (flags
& QEMU_NET_PACKET_FLAG_RAW
&& vc
->receive_raw
) {
412 ret
= vc
->receive_raw(vc
, data
, size
);
414 ret
= vc
->receive(vc
, data
, size
);
418 vc
->receive_disabled
= 1;
424 static ssize_t
qemu_vlan_deliver_packet(VLANClientState
*sender
,
430 VLANState
*vlan
= opaque
;
434 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
446 if (vc
->receive_disabled
) {
451 if (flags
& QEMU_NET_PACKET_FLAG_RAW
&& vc
->receive_raw
) {
452 len
= vc
->receive_raw(vc
, buf
, size
);
454 len
= vc
->receive(vc
, buf
, size
);
458 vc
->receive_disabled
= 1;
461 ret
= (ret
>= 0) ? ret
: len
;
468 void qemu_purge_queued_packets(VLANClientState
*vc
)
472 if (!vc
->peer
&& !vc
->vlan
) {
477 queue
= vc
->peer
->send_queue
;
479 queue
= vc
->vlan
->send_queue
;
482 qemu_net_queue_purge(queue
, vc
);
485 void qemu_flush_queued_packets(VLANClientState
*vc
)
489 vc
->receive_disabled
= 0;
492 queue
= vc
->vlan
->send_queue
;
494 queue
= vc
->send_queue
;
497 qemu_net_queue_flush(queue
);
500 static ssize_t
qemu_send_packet_async_with_flags(VLANClientState
*sender
,
502 const uint8_t *buf
, int size
,
503 NetPacketSent
*sent_cb
)
508 printf("qemu_send_packet_async:\n");
509 hex_dump(stdout
, buf
, size
);
512 if (sender
->link_down
|| (!sender
->peer
&& !sender
->vlan
)) {
517 queue
= sender
->peer
->send_queue
;
519 queue
= sender
->vlan
->send_queue
;
522 return qemu_net_queue_send(queue
, sender
, flags
, buf
, size
, sent_cb
);
525 ssize_t
qemu_send_packet_async(VLANClientState
*sender
,
526 const uint8_t *buf
, int size
,
527 NetPacketSent
*sent_cb
)
529 return qemu_send_packet_async_with_flags(sender
, QEMU_NET_PACKET_FLAG_NONE
,
533 void qemu_send_packet(VLANClientState
*vc
, const uint8_t *buf
, int size
)
535 qemu_send_packet_async(vc
, buf
, size
, NULL
);
538 ssize_t
qemu_send_packet_raw(VLANClientState
*vc
, const uint8_t *buf
, int size
)
540 return qemu_send_packet_async_with_flags(vc
, QEMU_NET_PACKET_FLAG_RAW
,
544 static ssize_t
vc_sendv_compat(VLANClientState
*vc
, const struct iovec
*iov
,
547 uint8_t buffer
[4096];
551 for (i
= 0; i
< iovcnt
; i
++) {
554 len
= MIN(sizeof(buffer
) - offset
, iov
[i
].iov_len
);
555 memcpy(buffer
+ offset
, iov
[i
].iov_base
, len
);
559 return vc
->receive(vc
, buffer
, offset
);
562 static ssize_t
calc_iov_length(const struct iovec
*iov
, int iovcnt
)
567 for (i
= 0; i
< iovcnt
; i
++)
568 offset
+= iov
[i
].iov_len
;
572 static ssize_t
qemu_deliver_packet_iov(VLANClientState
*sender
,
574 const struct iovec
*iov
,
578 VLANClientState
*vc
= opaque
;
581 return calc_iov_length(iov
, iovcnt
);
584 if (vc
->receive_iov
) {
585 return vc
->receive_iov(vc
, iov
, iovcnt
);
587 return vc_sendv_compat(vc
, iov
, iovcnt
);
591 static ssize_t
qemu_vlan_deliver_packet_iov(VLANClientState
*sender
,
593 const struct iovec
*iov
,
597 VLANState
*vlan
= opaque
;
601 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
609 ret
= calc_iov_length(iov
, iovcnt
);
613 assert(!(flags
& QEMU_NET_PACKET_FLAG_RAW
));
615 if (vc
->receive_iov
) {
616 len
= vc
->receive_iov(vc
, iov
, iovcnt
);
618 len
= vc_sendv_compat(vc
, iov
, iovcnt
);
621 ret
= (ret
>= 0) ? ret
: len
;
627 ssize_t
qemu_sendv_packet_async(VLANClientState
*sender
,
628 const struct iovec
*iov
, int iovcnt
,
629 NetPacketSent
*sent_cb
)
633 if (sender
->link_down
|| (!sender
->peer
&& !sender
->vlan
)) {
634 return calc_iov_length(iov
, iovcnt
);
638 queue
= sender
->peer
->send_queue
;
640 queue
= sender
->vlan
->send_queue
;
643 return qemu_net_queue_send_iov(queue
, sender
,
644 QEMU_NET_PACKET_FLAG_NONE
,
645 iov
, iovcnt
, sent_cb
);
649 qemu_sendv_packet(VLANClientState
*vc
, const struct iovec
*iov
, int iovcnt
)
651 return qemu_sendv_packet_async(vc
, iov
, iovcnt
, NULL
);
654 /* find or alloc a new VLAN */
655 VLANState
*qemu_find_vlan(int id
, int allocate
)
659 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
660 if (vlan
->id
== id
) {
669 vlan
= qemu_mallocz(sizeof(VLANState
));
671 QTAILQ_INIT(&vlan
->clients
);
673 vlan
->send_queue
= qemu_new_net_queue(qemu_vlan_deliver_packet
,
674 qemu_vlan_deliver_packet_iov
,
677 QTAILQ_INSERT_TAIL(&vlans
, vlan
, next
);
682 VLANClientState
*qemu_find_netdev(const char *id
)
686 QTAILQ_FOREACH(vc
, &non_vlan_clients
, next
) {
687 if (!strcmp(vc
->name
, id
)) {
695 static int nic_get_free_idx(void)
699 for (index
= 0; index
< MAX_NICS
; index
++)
700 if (!nd_table
[index
].used
)
705 int qemu_show_nic_models(const char *arg
, const char *const *models
)
709 if (!arg
|| strcmp(arg
, "?"))
712 fprintf(stderr
, "qemu: Supported NIC models: ");
713 for (i
= 0 ; models
[i
]; i
++)
714 fprintf(stderr
, "%s%c", models
[i
], models
[i
+1] ? ',' : '\n');
718 void qemu_check_nic_model(NICInfo
*nd
, const char *model
)
720 const char *models
[2];
725 if (qemu_show_nic_models(nd
->model
, models
))
727 if (qemu_find_nic_model(nd
, models
, model
) < 0)
731 int qemu_find_nic_model(NICInfo
*nd
, const char * const *models
,
732 const char *default_model
)
737 nd
->model
= qemu_strdup(default_model
);
739 for (i
= 0 ; models
[i
]; i
++) {
740 if (strcmp(nd
->model
, models
[i
]) == 0)
744 qemu_error("qemu: Unsupported NIC model: %s\n", nd
->model
);
748 int net_handle_fd_param(Monitor
*mon
, const char *param
)
750 if (!qemu_isdigit(param
[0])) {
753 fd
= monitor_get_fd(mon
, param
);
755 qemu_error("No file descriptor named %s found", param
);
761 return strtol(param
, NULL
, 0);
765 static int net_init_nic(QemuOpts
*opts
,
774 idx
= nic_get_free_idx();
775 if (idx
== -1 || nb_nics
>= MAX_NICS
) {
776 qemu_error("Too Many NICs\n");
782 memset(nd
, 0, sizeof(*nd
));
784 if ((netdev
= qemu_opt_get(opts
, "netdev"))) {
785 nd
->netdev
= qemu_find_netdev(netdev
);
787 qemu_error("netdev '%s' not found\n", netdev
);
795 nd
->name
= qemu_strdup(name
);
797 if (qemu_opt_get(opts
, "model")) {
798 nd
->model
= qemu_strdup(qemu_opt_get(opts
, "model"));
800 if (qemu_opt_get(opts
, "addr")) {
801 nd
->devaddr
= qemu_strdup(qemu_opt_get(opts
, "addr"));
804 nd
->macaddr
[0] = 0x52;
805 nd
->macaddr
[1] = 0x54;
806 nd
->macaddr
[2] = 0x00;
807 nd
->macaddr
[3] = 0x12;
808 nd
->macaddr
[4] = 0x34;
809 nd
->macaddr
[5] = 0x56 + idx
;
811 if (qemu_opt_get(opts
, "macaddr") &&
812 parse_macaddr(nd
->macaddr
, qemu_opt_get(opts
, "macaddr")) < 0) {
813 qemu_error("invalid syntax for ethernet address\n");
817 nd
->nvectors
= qemu_opt_get_number(opts
, "vectors", NIC_NVECTORS_UNSPECIFIED
);
818 if (nd
->nvectors
!= NIC_NVECTORS_UNSPECIFIED
&&
819 (nd
->nvectors
< 0 || nd
->nvectors
> 0x7ffffff)) {
820 qemu_error("invalid # of vectors: %d\n", nd
->nvectors
);
826 nd
->vlan
->nb_guest_devs
++;
833 #define NET_COMMON_PARAMS_DESC \
836 .type = QEMU_OPT_STRING, \
837 .help = "net client type (nic, tap etc.)", \
840 .type = QEMU_OPT_NUMBER, \
841 .help = "vlan number", \
844 .type = QEMU_OPT_STRING, \
845 .help = "identifier for monitor commands", \
848 typedef int (*net_client_init_func
)(QemuOpts
*opts
,
853 /* magic number, but compiler will warn if too small */
854 #define NET_MAX_DESC 20
858 net_client_init_func init
;
859 QemuOptDesc desc
[NET_MAX_DESC
];
860 } net_client_types
[] = {
864 NET_COMMON_PARAMS_DESC
,
865 { /* end of list */ }
869 .init
= net_init_nic
,
871 NET_COMMON_PARAMS_DESC
,
874 .type
= QEMU_OPT_STRING
,
875 .help
= "id of -netdev to connect to",
879 .type
= QEMU_OPT_STRING
,
880 .help
= "MAC address",
883 .type
= QEMU_OPT_STRING
,
884 .help
= "device model (e1000, rtl8139, virtio etc.)",
887 .type
= QEMU_OPT_STRING
,
888 .help
= "PCI device address",
891 .type
= QEMU_OPT_NUMBER
,
892 .help
= "number of MSI-x vectors, 0 to disable MSI-X",
894 { /* end of list */ }
899 .init
= net_init_slirp
,
901 NET_COMMON_PARAMS_DESC
,
904 .type
= QEMU_OPT_STRING
,
905 .help
= "client hostname reported by the builtin DHCP server",
908 .type
= QEMU_OPT_STRING
,
909 .help
= "isolate the guest from the host (y|yes|n|no)",
912 .type
= QEMU_OPT_STRING
,
913 .help
= "legacy parameter, use net= instead",
916 .type
= QEMU_OPT_STRING
,
917 .help
= "IP address and optional netmask",
920 .type
= QEMU_OPT_STRING
,
921 .help
= "guest-visible address of the host",
924 .type
= QEMU_OPT_STRING
,
925 .help
= "root directory of the built-in TFTP server",
928 .type
= QEMU_OPT_STRING
,
929 .help
= "BOOTP filename, for use with tftp=",
932 .type
= QEMU_OPT_STRING
,
933 .help
= "the first of the 16 IPs the built-in DHCP server can assign",
936 .type
= QEMU_OPT_STRING
,
937 .help
= "guest-visible address of the virtual nameserver",
940 .type
= QEMU_OPT_STRING
,
941 .help
= "root directory of the built-in SMB server",
944 .type
= QEMU_OPT_STRING
,
945 .help
= "IP address of the built-in SMB server",
948 .type
= QEMU_OPT_STRING
,
949 .help
= "guest port number to forward incoming TCP or UDP connections",
952 .type
= QEMU_OPT_STRING
,
953 .help
= "IP address and port to forward guest TCP connections",
955 { /* end of list */ }
960 .init
= net_init_tap
,
962 NET_COMMON_PARAMS_DESC
,
965 .type
= QEMU_OPT_STRING
,
966 .help
= "interface name",
971 .type
= QEMU_OPT_STRING
,
972 .help
= "file descriptor of an already opened tap",
975 .type
= QEMU_OPT_STRING
,
976 .help
= "script to initialize the interface",
978 .name
= "downscript",
979 .type
= QEMU_OPT_STRING
,
980 .help
= "script to shut down the interface",
983 .type
= QEMU_OPT_SIZE
,
984 .help
= "send buffer limit"
987 .type
= QEMU_OPT_BOOL
,
988 .help
= "enable the IFF_VNET_HDR flag on the tap interface"
991 { /* end of list */ }
995 .init
= net_init_socket
,
997 NET_COMMON_PARAMS_DESC
,
1000 .type
= QEMU_OPT_STRING
,
1001 .help
= "file descriptor of an already opened socket",
1004 .type
= QEMU_OPT_STRING
,
1005 .help
= "port number, and optional hostname, to listen on",
1008 .type
= QEMU_OPT_STRING
,
1009 .help
= "port number, and optional hostname, to connect to",
1012 .type
= QEMU_OPT_STRING
,
1013 .help
= "UDP multicast address and port number",
1015 { /* end of list */ }
1020 .init
= net_init_vde
,
1022 NET_COMMON_PARAMS_DESC
,
1025 .type
= QEMU_OPT_STRING
,
1026 .help
= "socket path",
1029 .type
= QEMU_OPT_NUMBER
,
1030 .help
= "port number",
1033 .type
= QEMU_OPT_STRING
,
1034 .help
= "group owner of socket",
1037 .type
= QEMU_OPT_NUMBER
,
1038 .help
= "permissions for socket",
1040 { /* end of list */ }
1045 .init
= net_init_dump
,
1047 NET_COMMON_PARAMS_DESC
,
1050 .type
= QEMU_OPT_SIZE
,
1051 .help
= "per-packet size limit (64k default)",
1054 .type
= QEMU_OPT_STRING
,
1055 .help
= "dump file path (default is qemu-vlan0.pcap)",
1057 { /* end of list */ }
1060 { /* end of list */ }
1063 int net_client_init(Monitor
*mon
, QemuOpts
*opts
, int is_netdev
)
1069 type
= qemu_opt_get(opts
, "type");
1071 qemu_error("No type specified for -net\n");
1076 if (strcmp(type
, "tap") != 0 &&
1078 strcmp(type
, "user") != 0 &&
1081 strcmp(type
, "vde") != 0 &&
1083 strcmp(type
, "socket") != 0) {
1084 qemu_error("The '%s' network backend type is not valid with -netdev\n",
1089 if (qemu_opt_get(opts
, "vlan")) {
1090 qemu_error("The 'vlan' parameter is not valid with -netdev\n");
1093 if (qemu_opt_get(opts
, "name")) {
1094 qemu_error("The 'name' parameter is not valid with -netdev\n");
1097 if (!qemu_opts_id(opts
)) {
1098 qemu_error("The id= parameter is required with -netdev\n");
1103 name
= qemu_opts_id(opts
);
1105 name
= qemu_opt_get(opts
, "name");
1108 for (i
= 0; net_client_types
[i
].type
!= NULL
; i
++) {
1109 if (!strcmp(net_client_types
[i
].type
, type
)) {
1110 VLANState
*vlan
= NULL
;
1112 if (qemu_opts_validate(opts
, &net_client_types
[i
].desc
[0]) == -1) {
1116 /* Do not add to a vlan if it's a -netdev or a nic with a
1117 * netdev= parameter. */
1119 (strcmp(type
, "nic") == 0 && qemu_opt_get(opts
, "netdev")))) {
1120 vlan
= qemu_find_vlan(qemu_opt_get_number(opts
, "vlan", 0), 1);
1123 if (net_client_types
[i
].init
) {
1124 return net_client_types
[i
].init(opts
, mon
, name
, vlan
);
1131 qemu_error("Invalid -net type '%s'\n", type
);
1135 void net_client_uninit(NICInfo
*nd
)
1138 nd
->vlan
->nb_guest_devs
--;
1142 qemu_free(nd
->model
);
1143 qemu_free(nd
->name
);
1144 qemu_free(nd
->devaddr
);
1149 static int net_host_check_device(const char *device
)
1152 const char *valid_param_list
[] = { "tap", "socket", "dump"
1160 for (i
= 0; i
< sizeof(valid_param_list
) / sizeof(char *); i
++) {
1161 if (!strncmp(valid_param_list
[i
], device
,
1162 strlen(valid_param_list
[i
])))
1169 void net_host_device_add(Monitor
*mon
, const QDict
*qdict
)
1171 const char *device
= qdict_get_str(qdict
, "device");
1172 const char *opts_str
= qdict_get_try_str(qdict
, "opts");
1175 if (!net_host_check_device(device
)) {
1176 monitor_printf(mon
, "invalid host network device %s\n", device
);
1180 opts
= qemu_opts_parse(&qemu_net_opts
, opts_str
? opts_str
: "", NULL
);
1182 monitor_printf(mon
, "parsing network options '%s' failed\n",
1183 opts_str
? opts_str
: "");
1187 qemu_opt_set(opts
, "type", device
);
1189 if (net_client_init(mon
, opts
, 0) < 0) {
1190 monitor_printf(mon
, "adding host network device %s failed\n", device
);
1194 void net_host_device_remove(Monitor
*mon
, const QDict
*qdict
)
1196 VLANClientState
*vc
;
1197 int vlan_id
= qdict_get_int(qdict
, "vlan_id");
1198 const char *device
= qdict_get_str(qdict
, "device");
1200 vc
= qemu_find_vlan_client_by_name(mon
, vlan_id
, device
);
1204 if (!net_host_check_device(vc
->model
)) {
1205 monitor_printf(mon
, "invalid host network device %s\n", device
);
1208 qemu_del_vlan_client(vc
);
1211 void net_set_boot_mask(int net_boot_mask
)
1215 /* Only the first four NICs may be bootable */
1216 net_boot_mask
= net_boot_mask
& 0xF;
1218 for (i
= 0; i
< nb_nics
; i
++) {
1219 if (net_boot_mask
& (1 << i
)) {
1220 nd_table
[i
].bootable
= 1;
1221 net_boot_mask
&= ~(1 << i
);
1225 if (net_boot_mask
) {
1226 fprintf(stderr
, "Cannot boot from non-existent NIC\n");
1231 void do_info_network(Monitor
*mon
)
1235 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
1236 VLANClientState
*vc
;
1238 monitor_printf(mon
, "VLAN %d devices:\n", vlan
->id
);
1240 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
1241 monitor_printf(mon
, " %s: %s\n", vc
->name
, vc
->info_str
);
1246 void do_set_link(Monitor
*mon
, const QDict
*qdict
)
1249 VLANClientState
*vc
= NULL
;
1250 const char *name
= qdict_get_str(qdict
, "name");
1251 const char *up_or_down
= qdict_get_str(qdict
, "up_or_down");
1253 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
1254 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
1255 if (strcmp(vc
->name
, name
) == 0) {
1263 monitor_printf(mon
, "could not find network device '%s'\n", name
);
1267 if (strcmp(up_or_down
, "up") == 0)
1269 else if (strcmp(up_or_down
, "down") == 0)
1272 monitor_printf(mon
, "invalid link status '%s'; only 'up' or 'down' "
1273 "valid\n", up_or_down
);
1275 if (vc
->link_status_changed
)
1276 vc
->link_status_changed(vc
);
1279 void net_cleanup(void)
1282 VLANClientState
*vc
, *next_vc
;
1284 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
1285 QTAILQ_FOREACH_SAFE(vc
, &vlan
->clients
, next
, next_vc
) {
1286 qemu_del_vlan_client(vc
);
1290 QTAILQ_FOREACH_SAFE(vc
, &non_vlan_clients
, next
, next_vc
) {
1291 qemu_del_vlan_client(vc
);
1295 static void net_check_clients(void)
1299 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
1300 if (vlan
->nb_guest_devs
== 0 && vlan
->nb_host_devs
== 0)
1302 if (vlan
->nb_guest_devs
== 0)
1303 fprintf(stderr
, "Warning: vlan %d with no nics\n", vlan
->id
);
1304 if (vlan
->nb_host_devs
== 0)
1306 "Warning: vlan %d is not connected to host network\n",
1311 static int net_init_client(QemuOpts
*opts
, void *dummy
)
1313 if (net_client_init(NULL
, opts
, 0) < 0)
1318 static int net_init_netdev(QemuOpts
*opts
, void *dummy
)
1320 return net_client_init(NULL
, opts
, 1);
1323 int net_init_clients(void)
1325 if (QTAILQ_EMPTY(&qemu_net_opts
.head
)) {
1326 /* if no clients, we use a default config */
1327 qemu_opts_set(&qemu_net_opts
, NULL
, "type", "nic");
1329 qemu_opts_set(&qemu_net_opts
, NULL
, "type", "user");
1333 QTAILQ_INIT(&vlans
);
1334 QTAILQ_INIT(&non_vlan_clients
);
1336 if (qemu_opts_foreach(&qemu_netdev_opts
, net_init_netdev
, NULL
, 1) == -1)
1339 if (qemu_opts_foreach(&qemu_net_opts
, net_init_client
, NULL
, 1) == -1) {
1343 net_check_clients();
1348 int net_client_parse(QemuOptsList
*opts_list
, const char *optarg
)
1350 #if defined(CONFIG_SLIRP)
1352 if (net_slirp_parse_legacy(opts_list
, optarg
, &ret
)) {
1357 if (!qemu_opts_parse(opts_list
, optarg
, "type")) {