root / net.c @ 44e4c0ba
History | View | Annotate | Download (35.2 kB)
1 | 63a01ef8 | aliguori | /*
|
---|---|---|---|
2 | 63a01ef8 | aliguori | * QEMU System Emulator
|
3 | 63a01ef8 | aliguori | *
|
4 | 63a01ef8 | aliguori | * Copyright (c) 2003-2008 Fabrice Bellard
|
5 | 63a01ef8 | aliguori | *
|
6 | 63a01ef8 | aliguori | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | 63a01ef8 | aliguori | * of this software and associated documentation files (the "Software"), to deal
|
8 | 63a01ef8 | aliguori | * in the Software without restriction, including without limitation the rights
|
9 | 63a01ef8 | aliguori | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | 63a01ef8 | aliguori | * copies of the Software, and to permit persons to whom the Software is
|
11 | 63a01ef8 | aliguori | * furnished to do so, subject to the following conditions:
|
12 | 63a01ef8 | aliguori | *
|
13 | 63a01ef8 | aliguori | * The above copyright notice and this permission notice shall be included in
|
14 | 63a01ef8 | aliguori | * all copies or substantial portions of the Software.
|
15 | 63a01ef8 | aliguori | *
|
16 | 63a01ef8 | aliguori | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | 63a01ef8 | aliguori | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | 63a01ef8 | aliguori | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 | 63a01ef8 | aliguori | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | 63a01ef8 | aliguori | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | 63a01ef8 | aliguori | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 | 63a01ef8 | aliguori | * THE SOFTWARE.
|
23 | 63a01ef8 | aliguori | */
|
24 | 1df49e04 | Mark McLoughlin | #include "net.h" |
25 | 63a01ef8 | aliguori | |
26 | d40cdb10 | blueswir1 | #include "config-host.h" |
27 | d40cdb10 | blueswir1 | |
28 | a8ed73f7 | Mark McLoughlin | #include "net/tap.h" |
29 | 42281ac9 | Mark McLoughlin | #include "net/socket.h" |
30 | 1abecf77 | Mark McLoughlin | #include "net/dump.h" |
31 | 68ac40d2 | Mark McLoughlin | #include "net/slirp.h" |
32 | 5c361cc3 | Mark McLoughlin | #include "net/vde.h" |
33 | f1d078c3 | Mark McLoughlin | #include "net/util.h" |
34 | 511d2b14 | blueswir1 | #include "monitor.h" |
35 | 511d2b14 | blueswir1 | #include "sysemu.h" |
36 | 1df49e04 | Mark McLoughlin | #include "qemu-common.h" |
37 | 511d2b14 | blueswir1 | #include "qemu_socket.h" |
38 | 511d2b14 | blueswir1 | |
39 | 5610c3aa | Mark McLoughlin | static QTAILQ_HEAD(, VLANState) vlans;
|
40 | 577c4af9 | Mark McLoughlin | static QTAILQ_HEAD(, VLANClientState) non_vlan_clients;
|
41 | 63a01ef8 | aliguori | |
42 | cb4522cc | Gerd Hoffmann | int default_net = 1; |
43 | cb4522cc | Gerd Hoffmann | |
44 | 63a01ef8 | aliguori | /***********************************************************/
|
45 | 63a01ef8 | aliguori | /* network device redirectors */
|
46 | 63a01ef8 | aliguori | |
47 | 68ac40d2 | Mark McLoughlin | #if defined(DEBUG_NET)
|
48 | 63a01ef8 | aliguori | static void hex_dump(FILE *f, const uint8_t *buf, int size) |
49 | 63a01ef8 | aliguori | { |
50 | 63a01ef8 | aliguori | int len, i, j, c;
|
51 | 63a01ef8 | aliguori | |
52 | 63a01ef8 | aliguori | for(i=0;i<size;i+=16) { |
53 | 63a01ef8 | aliguori | len = size - i; |
54 | 63a01ef8 | aliguori | if (len > 16) |
55 | 63a01ef8 | aliguori | len = 16;
|
56 | 63a01ef8 | aliguori | fprintf(f, "%08x ", i);
|
57 | 63a01ef8 | aliguori | for(j=0;j<16;j++) { |
58 | 63a01ef8 | aliguori | if (j < len)
|
59 | 63a01ef8 | aliguori | fprintf(f, " %02x", buf[i+j]);
|
60 | 63a01ef8 | aliguori | else
|
61 | 63a01ef8 | aliguori | fprintf(f, " ");
|
62 | 63a01ef8 | aliguori | } |
63 | 63a01ef8 | aliguori | fprintf(f, " ");
|
64 | 63a01ef8 | aliguori | for(j=0;j<len;j++) { |
65 | 63a01ef8 | aliguori | c = buf[i+j]; |
66 | 63a01ef8 | aliguori | if (c < ' ' || c > '~') |
67 | 63a01ef8 | aliguori | c = '.';
|
68 | 63a01ef8 | aliguori | fprintf(f, "%c", c);
|
69 | 63a01ef8 | aliguori | } |
70 | 63a01ef8 | aliguori | fprintf(f, "\n");
|
71 | 63a01ef8 | aliguori | } |
72 | 63a01ef8 | aliguori | } |
73 | 63a01ef8 | aliguori | #endif
|
74 | 63a01ef8 | aliguori | |
75 | 63a01ef8 | aliguori | static int get_str_sep(char *buf, int buf_size, const char **pp, int sep) |
76 | 63a01ef8 | aliguori | { |
77 | 63a01ef8 | aliguori | const char *p, *p1; |
78 | 63a01ef8 | aliguori | int len;
|
79 | 63a01ef8 | aliguori | p = *pp; |
80 | 63a01ef8 | aliguori | p1 = strchr(p, sep); |
81 | 63a01ef8 | aliguori | if (!p1)
|
82 | 63a01ef8 | aliguori | return -1; |
83 | 63a01ef8 | aliguori | len = p1 - p; |
84 | 63a01ef8 | aliguori | p1++; |
85 | 63a01ef8 | aliguori | if (buf_size > 0) { |
86 | 63a01ef8 | aliguori | if (len > buf_size - 1) |
87 | 63a01ef8 | aliguori | len = buf_size - 1;
|
88 | 63a01ef8 | aliguori | memcpy(buf, p, len); |
89 | 63a01ef8 | aliguori | buf[len] = '\0';
|
90 | 63a01ef8 | aliguori | } |
91 | 63a01ef8 | aliguori | *pp = p1; |
92 | 63a01ef8 | aliguori | return 0; |
93 | 63a01ef8 | aliguori | } |
94 | 63a01ef8 | aliguori | |
95 | 63a01ef8 | aliguori | int parse_host_src_port(struct sockaddr_in *haddr, |
96 | 63a01ef8 | aliguori | struct sockaddr_in *saddr,
|
97 | 63a01ef8 | aliguori | const char *input_str) |
98 | 63a01ef8 | aliguori | { |
99 | 63a01ef8 | aliguori | char *str = strdup(input_str);
|
100 | 63a01ef8 | aliguori | char *host_str = str;
|
101 | 63a01ef8 | aliguori | char *src_str;
|
102 | 63a01ef8 | aliguori | const char *src_str2; |
103 | 63a01ef8 | aliguori | char *ptr;
|
104 | 63a01ef8 | aliguori | |
105 | 63a01ef8 | aliguori | /*
|
106 | 63a01ef8 | aliguori | * Chop off any extra arguments at the end of the string which
|
107 | 63a01ef8 | aliguori | * would start with a comma, then fill in the src port information
|
108 | 63a01ef8 | aliguori | * if it was provided else use the "any address" and "any port".
|
109 | 63a01ef8 | aliguori | */
|
110 | 63a01ef8 | aliguori | if ((ptr = strchr(str,','))) |
111 | 63a01ef8 | aliguori | *ptr = '\0';
|
112 | 63a01ef8 | aliguori | |
113 | 63a01ef8 | aliguori | if ((src_str = strchr(input_str,'@'))) { |
114 | 63a01ef8 | aliguori | *src_str = '\0';
|
115 | 63a01ef8 | aliguori | src_str++; |
116 | 63a01ef8 | aliguori | } |
117 | 63a01ef8 | aliguori | |
118 | 63a01ef8 | aliguori | if (parse_host_port(haddr, host_str) < 0) |
119 | 63a01ef8 | aliguori | goto fail;
|
120 | 63a01ef8 | aliguori | |
121 | 63a01ef8 | aliguori | src_str2 = src_str; |
122 | 63a01ef8 | aliguori | if (!src_str || *src_str == '\0') |
123 | 63a01ef8 | aliguori | src_str2 = ":0";
|
124 | 63a01ef8 | aliguori | |
125 | 63a01ef8 | aliguori | if (parse_host_port(saddr, src_str2) < 0) |
126 | 63a01ef8 | aliguori | goto fail;
|
127 | 63a01ef8 | aliguori | |
128 | 63a01ef8 | aliguori | free(str); |
129 | 63a01ef8 | aliguori | return(0); |
130 | 63a01ef8 | aliguori | |
131 | 63a01ef8 | aliguori | fail:
|
132 | 63a01ef8 | aliguori | free(str); |
133 | 63a01ef8 | aliguori | return -1; |
134 | 63a01ef8 | aliguori | } |
135 | 63a01ef8 | aliguori | |
136 | 63a01ef8 | aliguori | int parse_host_port(struct sockaddr_in *saddr, const char *str) |
137 | 63a01ef8 | aliguori | { |
138 | 63a01ef8 | aliguori | char buf[512]; |
139 | 63a01ef8 | aliguori | struct hostent *he;
|
140 | 63a01ef8 | aliguori | const char *p, *r; |
141 | 63a01ef8 | aliguori | int port;
|
142 | 63a01ef8 | aliguori | |
143 | 63a01ef8 | aliguori | p = str; |
144 | 63a01ef8 | aliguori | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) |
145 | 63a01ef8 | aliguori | return -1; |
146 | 63a01ef8 | aliguori | saddr->sin_family = AF_INET; |
147 | 63a01ef8 | aliguori | if (buf[0] == '\0') { |
148 | 63a01ef8 | aliguori | saddr->sin_addr.s_addr = 0;
|
149 | 63a01ef8 | aliguori | } else {
|
150 | cd390083 | blueswir1 | if (qemu_isdigit(buf[0])) { |
151 | 63a01ef8 | aliguori | if (!inet_aton(buf, &saddr->sin_addr))
|
152 | 63a01ef8 | aliguori | return -1; |
153 | 63a01ef8 | aliguori | } else {
|
154 | 63a01ef8 | aliguori | if ((he = gethostbyname(buf)) == NULL) |
155 | 63a01ef8 | aliguori | return - 1; |
156 | 63a01ef8 | aliguori | saddr->sin_addr = *(struct in_addr *)he->h_addr;
|
157 | 63a01ef8 | aliguori | } |
158 | 63a01ef8 | aliguori | } |
159 | 63a01ef8 | aliguori | port = strtol(p, (char **)&r, 0); |
160 | 63a01ef8 | aliguori | if (r == p)
|
161 | 63a01ef8 | aliguori | return -1; |
162 | 63a01ef8 | aliguori | saddr->sin_port = htons(port); |
163 | 63a01ef8 | aliguori | return 0; |
164 | 63a01ef8 | aliguori | } |
165 | 63a01ef8 | aliguori | |
166 | 7cb7434b | aliguori | void qemu_format_nic_info_str(VLANClientState *vc, uint8_t macaddr[6]) |
167 | 7cb7434b | aliguori | { |
168 | 7cb7434b | aliguori | snprintf(vc->info_str, sizeof(vc->info_str),
|
169 | 4dda4063 | aliguori | "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
|
170 | 4dda4063 | aliguori | vc->model, |
171 | 7cb7434b | aliguori | macaddr[0], macaddr[1], macaddr[2], |
172 | 7cb7434b | aliguori | macaddr[3], macaddr[4], macaddr[5]); |
173 | 7cb7434b | aliguori | } |
174 | 7cb7434b | aliguori | |
175 | 76d32cba | Gerd Hoffmann | void qemu_macaddr_default_if_unset(MACAddr *macaddr)
|
176 | 76d32cba | Gerd Hoffmann | { |
177 | 76d32cba | Gerd Hoffmann | static int index = 0; |
178 | 76d32cba | Gerd Hoffmann | static const MACAddr zero = { .a = { 0,0,0,0,0,0 } }; |
179 | 76d32cba | Gerd Hoffmann | |
180 | 76d32cba | Gerd Hoffmann | if (memcmp(macaddr, &zero, sizeof(zero)) != 0) |
181 | 76d32cba | Gerd Hoffmann | return;
|
182 | 76d32cba | Gerd Hoffmann | macaddr->a[0] = 0x52; |
183 | 76d32cba | Gerd Hoffmann | macaddr->a[1] = 0x54; |
184 | 76d32cba | Gerd Hoffmann | macaddr->a[2] = 0x00; |
185 | 76d32cba | Gerd Hoffmann | macaddr->a[3] = 0x12; |
186 | 76d32cba | Gerd Hoffmann | macaddr->a[4] = 0x34; |
187 | 76d32cba | Gerd Hoffmann | macaddr->a[5] = 0x56 + index++; |
188 | 76d32cba | Gerd Hoffmann | } |
189 | 76d32cba | Gerd Hoffmann | |
190 | 676cff29 | aliguori | static char *assign_name(VLANClientState *vc1, const char *model) |
191 | 676cff29 | aliguori | { |
192 | 676cff29 | aliguori | VLANState *vlan; |
193 | 676cff29 | aliguori | char buf[256]; |
194 | 676cff29 | aliguori | int id = 0; |
195 | 676cff29 | aliguori | |
196 | 5610c3aa | Mark McLoughlin | QTAILQ_FOREACH(vlan, &vlans, next) { |
197 | 676cff29 | aliguori | VLANClientState *vc; |
198 | 676cff29 | aliguori | |
199 | 5610c3aa | Mark McLoughlin | QTAILQ_FOREACH(vc, &vlan->clients, next) { |
200 | 5610c3aa | Mark McLoughlin | if (vc != vc1 && strcmp(vc->model, model) == 0) { |
201 | 676cff29 | aliguori | id++; |
202 | 5610c3aa | Mark McLoughlin | } |
203 | 5610c3aa | Mark McLoughlin | } |
204 | 676cff29 | aliguori | } |
205 | 676cff29 | aliguori | |
206 | 676cff29 | aliguori | snprintf(buf, sizeof(buf), "%s.%d", model, id); |
207 | 676cff29 | aliguori | |
208 | 02374aa0 | Mark McLoughlin | return qemu_strdup(buf);
|
209 | 676cff29 | aliguori | } |
210 | 676cff29 | aliguori | |
211 | 9a6ecb30 | Mark McLoughlin | static ssize_t qemu_deliver_packet(VLANClientState *sender,
|
212 | c0b8e49c | Mark McLoughlin | unsigned flags,
|
213 | 9a6ecb30 | Mark McLoughlin | const uint8_t *data,
|
214 | 9a6ecb30 | Mark McLoughlin | size_t size, |
215 | 9a6ecb30 | Mark McLoughlin | void *opaque);
|
216 | 9a6ecb30 | Mark McLoughlin | static ssize_t qemu_deliver_packet_iov(VLANClientState *sender,
|
217 | c0b8e49c | Mark McLoughlin | unsigned flags,
|
218 | 9a6ecb30 | Mark McLoughlin | const struct iovec *iov, |
219 | 9a6ecb30 | Mark McLoughlin | int iovcnt,
|
220 | 9a6ecb30 | Mark McLoughlin | void *opaque);
|
221 | 9a6ecb30 | Mark McLoughlin | |
222 | 45460d1a | Mark McLoughlin | VLANClientState *qemu_new_net_client(NetClientInfo *info, |
223 | 45460d1a | Mark McLoughlin | VLANState *vlan, |
224 | 45460d1a | Mark McLoughlin | VLANClientState *peer, |
225 | 45460d1a | Mark McLoughlin | const char *model, |
226 | 45460d1a | Mark McLoughlin | const char *name) |
227 | 63a01ef8 | aliguori | { |
228 | 5610c3aa | Mark McLoughlin | VLANClientState *vc; |
229 | 5610c3aa | Mark McLoughlin | |
230 | 45460d1a | Mark McLoughlin | assert(info->size >= sizeof(VLANClientState));
|
231 | 45460d1a | Mark McLoughlin | |
232 | 45460d1a | Mark McLoughlin | vc = qemu_mallocz(info->size); |
233 | 5610c3aa | Mark McLoughlin | |
234 | 665a3b07 | Mark McLoughlin | vc->info = info; |
235 | 02374aa0 | Mark McLoughlin | vc->model = qemu_strdup(model); |
236 | 45460d1a | Mark McLoughlin | if (name) {
|
237 | 02374aa0 | Mark McLoughlin | vc->name = qemu_strdup(name); |
238 | 45460d1a | Mark McLoughlin | } else {
|
239 | 7a9f6e4a | aliguori | vc->name = assign_name(vc, model); |
240 | 45460d1a | Mark McLoughlin | } |
241 | 5610c3aa | Mark McLoughlin | |
242 | d80b9fc6 | Mark McLoughlin | if (vlan) {
|
243 | 283c7c63 | Mark McLoughlin | assert(!peer); |
244 | d80b9fc6 | Mark McLoughlin | vc->vlan = vlan; |
245 | d80b9fc6 | Mark McLoughlin | QTAILQ_INSERT_TAIL(&vc->vlan->clients, vc, next); |
246 | 577c4af9 | Mark McLoughlin | } else {
|
247 | 283c7c63 | Mark McLoughlin | if (peer) {
|
248 | 283c7c63 | Mark McLoughlin | vc->peer = peer; |
249 | 283c7c63 | Mark McLoughlin | peer->peer = vc; |
250 | 283c7c63 | Mark McLoughlin | } |
251 | 577c4af9 | Mark McLoughlin | QTAILQ_INSERT_TAIL(&non_vlan_clients, vc, next); |
252 | 9a6ecb30 | Mark McLoughlin | |
253 | 9a6ecb30 | Mark McLoughlin | vc->send_queue = qemu_new_net_queue(qemu_deliver_packet, |
254 | 9a6ecb30 | Mark McLoughlin | qemu_deliver_packet_iov, |
255 | 9a6ecb30 | Mark McLoughlin | vc); |
256 | d80b9fc6 | Mark McLoughlin | } |
257 | 63a01ef8 | aliguori | |
258 | 63a01ef8 | aliguori | return vc;
|
259 | 63a01ef8 | aliguori | } |
260 | 63a01ef8 | aliguori | |
261 | ebef2c09 | Mark McLoughlin | NICState *qemu_new_nic(NetClientInfo *info, |
262 | ebef2c09 | Mark McLoughlin | NICConf *conf, |
263 | ebef2c09 | Mark McLoughlin | const char *model, |
264 | ebef2c09 | Mark McLoughlin | const char *name, |
265 | ebef2c09 | Mark McLoughlin | void *opaque)
|
266 | ebef2c09 | Mark McLoughlin | { |
267 | ebef2c09 | Mark McLoughlin | VLANClientState *nc; |
268 | ebef2c09 | Mark McLoughlin | NICState *nic; |
269 | ebef2c09 | Mark McLoughlin | |
270 | ebef2c09 | Mark McLoughlin | assert(info->type == NET_CLIENT_TYPE_NIC); |
271 | ebef2c09 | Mark McLoughlin | assert(info->size >= sizeof(NICState));
|
272 | ebef2c09 | Mark McLoughlin | |
273 | ebef2c09 | Mark McLoughlin | nc = qemu_new_net_client(info, conf->vlan, conf->peer, model, name); |
274 | ebef2c09 | Mark McLoughlin | |
275 | ebef2c09 | Mark McLoughlin | nic = DO_UPCAST(NICState, nc, nc); |
276 | ebef2c09 | Mark McLoughlin | nic->conf = conf; |
277 | ebef2c09 | Mark McLoughlin | nic->opaque = opaque; |
278 | ebef2c09 | Mark McLoughlin | |
279 | ebef2c09 | Mark McLoughlin | return nic;
|
280 | ebef2c09 | Mark McLoughlin | } |
281 | ebef2c09 | Mark McLoughlin | |
282 | 63a01ef8 | aliguori | void qemu_del_vlan_client(VLANClientState *vc)
|
283 | 63a01ef8 | aliguori | { |
284 | d80b9fc6 | Mark McLoughlin | if (vc->vlan) {
|
285 | d80b9fc6 | Mark McLoughlin | QTAILQ_REMOVE(&vc->vlan->clients, vc, next); |
286 | 577c4af9 | Mark McLoughlin | } else {
|
287 | 9a6ecb30 | Mark McLoughlin | if (vc->send_queue) {
|
288 | 9a6ecb30 | Mark McLoughlin | qemu_del_net_queue(vc->send_queue); |
289 | 9a6ecb30 | Mark McLoughlin | } |
290 | 577c4af9 | Mark McLoughlin | QTAILQ_REMOVE(&non_vlan_clients, vc, next); |
291 | 283c7c63 | Mark McLoughlin | if (vc->peer) {
|
292 | 283c7c63 | Mark McLoughlin | vc->peer->peer = NULL;
|
293 | 283c7c63 | Mark McLoughlin | } |
294 | d80b9fc6 | Mark McLoughlin | } |
295 | 63a01ef8 | aliguori | |
296 | 665a3b07 | Mark McLoughlin | if (vc->info->cleanup) {
|
297 | 665a3b07 | Mark McLoughlin | vc->info->cleanup(vc); |
298 | 5610c3aa | Mark McLoughlin | } |
299 | 5610c3aa | Mark McLoughlin | |
300 | 5610c3aa | Mark McLoughlin | qemu_free(vc->name); |
301 | 5610c3aa | Mark McLoughlin | qemu_free(vc->model); |
302 | 5610c3aa | Mark McLoughlin | qemu_free(vc); |
303 | 63a01ef8 | aliguori | } |
304 | 63a01ef8 | aliguori | |
305 | 68ac40d2 | Mark McLoughlin | VLANClientState * |
306 | 1a609520 | Jan Kiszka | qemu_find_vlan_client_by_name(Monitor *mon, int vlan_id,
|
307 | 1a609520 | Jan Kiszka | const char *client_str) |
308 | 1a609520 | Jan Kiszka | { |
309 | 1a609520 | Jan Kiszka | VLANState *vlan; |
310 | 1a609520 | Jan Kiszka | VLANClientState *vc; |
311 | 1a609520 | Jan Kiszka | |
312 | 1a609520 | Jan Kiszka | vlan = qemu_find_vlan(vlan_id, 0);
|
313 | 1a609520 | Jan Kiszka | if (!vlan) {
|
314 | 1a609520 | Jan Kiszka | monitor_printf(mon, "unknown VLAN %d\n", vlan_id);
|
315 | 1a609520 | Jan Kiszka | return NULL; |
316 | 1a609520 | Jan Kiszka | } |
317 | 1a609520 | Jan Kiszka | |
318 | 5610c3aa | Mark McLoughlin | QTAILQ_FOREACH(vc, &vlan->clients, next) { |
319 | 1a609520 | Jan Kiszka | if (!strcmp(vc->name, client_str)) {
|
320 | 1a609520 | Jan Kiszka | break;
|
321 | 1a609520 | Jan Kiszka | } |
322 | 1a609520 | Jan Kiszka | } |
323 | 1a609520 | Jan Kiszka | if (!vc) {
|
324 | 1a609520 | Jan Kiszka | monitor_printf(mon, "can't find device %s on VLAN %d\n",
|
325 | 1a609520 | Jan Kiszka | client_str, vlan_id); |
326 | 1a609520 | Jan Kiszka | } |
327 | 1a609520 | Jan Kiszka | |
328 | 1a609520 | Jan Kiszka | return vc;
|
329 | 1a609520 | Jan Kiszka | } |
330 | 1a609520 | Jan Kiszka | |
331 | 57f9ef17 | Mark McLoughlin | void qemu_foreach_nic(qemu_nic_foreach func, void *opaque) |
332 | 57f9ef17 | Mark McLoughlin | { |
333 | 57f9ef17 | Mark McLoughlin | VLANClientState *nc; |
334 | 57f9ef17 | Mark McLoughlin | VLANState *vlan; |
335 | 57f9ef17 | Mark McLoughlin | |
336 | 57f9ef17 | Mark McLoughlin | QTAILQ_FOREACH(nc, &non_vlan_clients, next) { |
337 | 57f9ef17 | Mark McLoughlin | if (nc->info->type == NET_CLIENT_TYPE_NIC) {
|
338 | 57f9ef17 | Mark McLoughlin | func(DO_UPCAST(NICState, nc, nc), opaque); |
339 | 57f9ef17 | Mark McLoughlin | } |
340 | 57f9ef17 | Mark McLoughlin | } |
341 | 57f9ef17 | Mark McLoughlin | |
342 | 57f9ef17 | Mark McLoughlin | QTAILQ_FOREACH(vlan, &vlans, next) { |
343 | 57f9ef17 | Mark McLoughlin | QTAILQ_FOREACH(nc, &vlan->clients, next) { |
344 | 57f9ef17 | Mark McLoughlin | if (nc->info->type == NET_CLIENT_TYPE_NIC) {
|
345 | 57f9ef17 | Mark McLoughlin | func(DO_UPCAST(NICState, nc, nc), opaque); |
346 | 57f9ef17 | Mark McLoughlin | } |
347 | 57f9ef17 | Mark McLoughlin | } |
348 | 57f9ef17 | Mark McLoughlin | } |
349 | 57f9ef17 | Mark McLoughlin | } |
350 | 57f9ef17 | Mark McLoughlin | |
351 | 2e1e0641 | Mark McLoughlin | int qemu_can_send_packet(VLANClientState *sender)
|
352 | 63a01ef8 | aliguori | { |
353 | 2e1e0641 | Mark McLoughlin | VLANState *vlan = sender->vlan; |
354 | 63a01ef8 | aliguori | VLANClientState *vc; |
355 | 63a01ef8 | aliguori | |
356 | 9a6ecb30 | Mark McLoughlin | if (sender->peer) {
|
357 | 893379ef | Mark McLoughlin | if (sender->peer->receive_disabled) {
|
358 | 893379ef | Mark McLoughlin | return 0; |
359 | 665a3b07 | Mark McLoughlin | } else if (sender->peer->info->can_receive && |
360 | 665a3b07 | Mark McLoughlin | !sender->peer->info->can_receive(sender->peer)) { |
361 | 9a6ecb30 | Mark McLoughlin | return 0; |
362 | 893379ef | Mark McLoughlin | } else {
|
363 | 893379ef | Mark McLoughlin | return 1; |
364 | 9a6ecb30 | Mark McLoughlin | } |
365 | 9a6ecb30 | Mark McLoughlin | } |
366 | 9a6ecb30 | Mark McLoughlin | |
367 | d80b9fc6 | Mark McLoughlin | if (!sender->vlan) {
|
368 | d80b9fc6 | Mark McLoughlin | return 1; |
369 | d80b9fc6 | Mark McLoughlin | } |
370 | d80b9fc6 | Mark McLoughlin | |
371 | 5610c3aa | Mark McLoughlin | QTAILQ_FOREACH(vc, &vlan->clients, next) { |
372 | 2e1e0641 | Mark McLoughlin | if (vc == sender) {
|
373 | 2e1e0641 | Mark McLoughlin | continue;
|
374 | 2e1e0641 | Mark McLoughlin | } |
375 | 2e1e0641 | Mark McLoughlin | |
376 | cda9046b | Mark McLoughlin | /* no can_receive() handler, they can always receive */
|
377 | 665a3b07 | Mark McLoughlin | if (!vc->info->can_receive || vc->info->can_receive(vc)) {
|
378 | 2e1e0641 | Mark McLoughlin | return 1; |
379 | 63a01ef8 | aliguori | } |
380 | 63a01ef8 | aliguori | } |
381 | 63a01ef8 | aliguori | return 0; |
382 | 63a01ef8 | aliguori | } |
383 | 63a01ef8 | aliguori | |
384 | 9a6ecb30 | Mark McLoughlin | static ssize_t qemu_deliver_packet(VLANClientState *sender,
|
385 | c0b8e49c | Mark McLoughlin | unsigned flags,
|
386 | 9a6ecb30 | Mark McLoughlin | const uint8_t *data,
|
387 | 9a6ecb30 | Mark McLoughlin | size_t size, |
388 | 9a6ecb30 | Mark McLoughlin | void *opaque)
|
389 | 9a6ecb30 | Mark McLoughlin | { |
390 | 9a6ecb30 | Mark McLoughlin | VLANClientState *vc = opaque; |
391 | 893379ef | Mark McLoughlin | ssize_t ret; |
392 | 9a6ecb30 | Mark McLoughlin | |
393 | 9a6ecb30 | Mark McLoughlin | if (vc->link_down) {
|
394 | 9a6ecb30 | Mark McLoughlin | return size;
|
395 | 9a6ecb30 | Mark McLoughlin | } |
396 | 9a6ecb30 | Mark McLoughlin | |
397 | 893379ef | Mark McLoughlin | if (vc->receive_disabled) {
|
398 | 893379ef | Mark McLoughlin | return 0; |
399 | 893379ef | Mark McLoughlin | } |
400 | 893379ef | Mark McLoughlin | |
401 | 665a3b07 | Mark McLoughlin | if (flags & QEMU_NET_PACKET_FLAG_RAW && vc->info->receive_raw) {
|
402 | 665a3b07 | Mark McLoughlin | ret = vc->info->receive_raw(vc, data, size); |
403 | 893379ef | Mark McLoughlin | } else {
|
404 | 665a3b07 | Mark McLoughlin | ret = vc->info->receive(vc, data, size); |
405 | 893379ef | Mark McLoughlin | } |
406 | 893379ef | Mark McLoughlin | |
407 | 893379ef | Mark McLoughlin | if (ret == 0) { |
408 | 893379ef | Mark McLoughlin | vc->receive_disabled = 1;
|
409 | 893379ef | Mark McLoughlin | }; |
410 | 893379ef | Mark McLoughlin | |
411 | 893379ef | Mark McLoughlin | return ret;
|
412 | 9a6ecb30 | Mark McLoughlin | } |
413 | 9a6ecb30 | Mark McLoughlin | |
414 | f7105843 | Mark McLoughlin | static ssize_t qemu_vlan_deliver_packet(VLANClientState *sender,
|
415 | c0b8e49c | Mark McLoughlin | unsigned flags,
|
416 | f7105843 | Mark McLoughlin | const uint8_t *buf,
|
417 | f7105843 | Mark McLoughlin | size_t size, |
418 | f7105843 | Mark McLoughlin | void *opaque)
|
419 | 63a01ef8 | aliguori | { |
420 | f7105843 | Mark McLoughlin | VLANState *vlan = opaque; |
421 | 63a01ef8 | aliguori | VLANClientState *vc; |
422 | 893379ef | Mark McLoughlin | ssize_t ret = -1;
|
423 | 63a01ef8 | aliguori | |
424 | f7105843 | Mark McLoughlin | QTAILQ_FOREACH(vc, &vlan->clients, next) { |
425 | 3e021d40 | Mark McLoughlin | ssize_t len; |
426 | 3e021d40 | Mark McLoughlin | |
427 | 3e021d40 | Mark McLoughlin | if (vc == sender) {
|
428 | 3e021d40 | Mark McLoughlin | continue;
|
429 | 764a4d1d | aliguori | } |
430 | 3e021d40 | Mark McLoughlin | |
431 | 3e021d40 | Mark McLoughlin | if (vc->link_down) {
|
432 | 3e021d40 | Mark McLoughlin | ret = size; |
433 | 3e021d40 | Mark McLoughlin | continue;
|
434 | 3e021d40 | Mark McLoughlin | } |
435 | 3e021d40 | Mark McLoughlin | |
436 | 893379ef | Mark McLoughlin | if (vc->receive_disabled) {
|
437 | 893379ef | Mark McLoughlin | ret = 0;
|
438 | 893379ef | Mark McLoughlin | continue;
|
439 | 893379ef | Mark McLoughlin | } |
440 | 893379ef | Mark McLoughlin | |
441 | 665a3b07 | Mark McLoughlin | if (flags & QEMU_NET_PACKET_FLAG_RAW && vc->info->receive_raw) {
|
442 | 665a3b07 | Mark McLoughlin | len = vc->info->receive_raw(vc, buf, size); |
443 | 893379ef | Mark McLoughlin | } else {
|
444 | 665a3b07 | Mark McLoughlin | len = vc->info->receive(vc, buf, size); |
445 | 893379ef | Mark McLoughlin | } |
446 | 893379ef | Mark McLoughlin | |
447 | 893379ef | Mark McLoughlin | if (len == 0) { |
448 | 893379ef | Mark McLoughlin | vc->receive_disabled = 1;
|
449 | 893379ef | Mark McLoughlin | } |
450 | 3e021d40 | Mark McLoughlin | |
451 | 3e021d40 | Mark McLoughlin | ret = (ret >= 0) ? ret : len;
|
452 | 893379ef | Mark McLoughlin | |
453 | 764a4d1d | aliguori | } |
454 | 3e021d40 | Mark McLoughlin | |
455 | 3e021d40 | Mark McLoughlin | return ret;
|
456 | 764a4d1d | aliguori | } |
457 | 764a4d1d | aliguori | |
458 | 8cad5516 | Mark McLoughlin | void qemu_purge_queued_packets(VLANClientState *vc)
|
459 | 8cad5516 | Mark McLoughlin | { |
460 | 9a6ecb30 | Mark McLoughlin | NetQueue *queue; |
461 | 9a6ecb30 | Mark McLoughlin | |
462 | 9a6ecb30 | Mark McLoughlin | if (!vc->peer && !vc->vlan) {
|
463 | d80b9fc6 | Mark McLoughlin | return;
|
464 | 9a6ecb30 | Mark McLoughlin | } |
465 | d80b9fc6 | Mark McLoughlin | |
466 | 9a6ecb30 | Mark McLoughlin | if (vc->peer) {
|
467 | 9a6ecb30 | Mark McLoughlin | queue = vc->peer->send_queue; |
468 | 9a6ecb30 | Mark McLoughlin | } else {
|
469 | 9a6ecb30 | Mark McLoughlin | queue = vc->vlan->send_queue; |
470 | 9a6ecb30 | Mark McLoughlin | } |
471 | 9a6ecb30 | Mark McLoughlin | |
472 | 9a6ecb30 | Mark McLoughlin | qemu_net_queue_purge(queue, vc); |
473 | 8cad5516 | Mark McLoughlin | } |
474 | 8cad5516 | Mark McLoughlin | |
475 | f3b6c7fc | Mark McLoughlin | void qemu_flush_queued_packets(VLANClientState *vc)
|
476 | e94667b9 | Mark McLoughlin | { |
477 | 9a6ecb30 | Mark McLoughlin | NetQueue *queue; |
478 | 9a6ecb30 | Mark McLoughlin | |
479 | 893379ef | Mark McLoughlin | vc->receive_disabled = 0;
|
480 | 893379ef | Mark McLoughlin | |
481 | 9a6ecb30 | Mark McLoughlin | if (vc->vlan) {
|
482 | 9a6ecb30 | Mark McLoughlin | queue = vc->vlan->send_queue; |
483 | 9a6ecb30 | Mark McLoughlin | } else {
|
484 | 9a6ecb30 | Mark McLoughlin | queue = vc->send_queue; |
485 | 9a6ecb30 | Mark McLoughlin | } |
486 | d80b9fc6 | Mark McLoughlin | |
487 | 9a6ecb30 | Mark McLoughlin | qemu_net_queue_flush(queue); |
488 | e94667b9 | Mark McLoughlin | } |
489 | e94667b9 | Mark McLoughlin | |
490 | ca77d175 | Mark McLoughlin | static ssize_t qemu_send_packet_async_with_flags(VLANClientState *sender,
|
491 | ca77d175 | Mark McLoughlin | unsigned flags,
|
492 | ca77d175 | Mark McLoughlin | const uint8_t *buf, int size, |
493 | ca77d175 | Mark McLoughlin | NetPacketSent *sent_cb) |
494 | 764a4d1d | aliguori | { |
495 | 9a6ecb30 | Mark McLoughlin | NetQueue *queue; |
496 | 436e5e53 | aliguori | |
497 | 63a01ef8 | aliguori | #ifdef DEBUG_NET
|
498 | d80b9fc6 | Mark McLoughlin | printf("qemu_send_packet_async:\n");
|
499 | 63a01ef8 | aliguori | hex_dump(stdout, buf, size); |
500 | 63a01ef8 | aliguori | #endif
|
501 | f3b6c7fc | Mark McLoughlin | |
502 | 9a6ecb30 | Mark McLoughlin | if (sender->link_down || (!sender->peer && !sender->vlan)) {
|
503 | 9a6ecb30 | Mark McLoughlin | return size;
|
504 | 9a6ecb30 | Mark McLoughlin | } |
505 | 9a6ecb30 | Mark McLoughlin | |
506 | 9a6ecb30 | Mark McLoughlin | if (sender->peer) {
|
507 | 9a6ecb30 | Mark McLoughlin | queue = sender->peer->send_queue; |
508 | 9a6ecb30 | Mark McLoughlin | } else {
|
509 | 9a6ecb30 | Mark McLoughlin | queue = sender->vlan->send_queue; |
510 | 9a6ecb30 | Mark McLoughlin | } |
511 | 9a6ecb30 | Mark McLoughlin | |
512 | ca77d175 | Mark McLoughlin | return qemu_net_queue_send(queue, sender, flags, buf, size, sent_cb);
|
513 | ca77d175 | Mark McLoughlin | } |
514 | ca77d175 | Mark McLoughlin | |
515 | ca77d175 | Mark McLoughlin | ssize_t qemu_send_packet_async(VLANClientState *sender, |
516 | ca77d175 | Mark McLoughlin | const uint8_t *buf, int size, |
517 | ca77d175 | Mark McLoughlin | NetPacketSent *sent_cb) |
518 | ca77d175 | Mark McLoughlin | { |
519 | ca77d175 | Mark McLoughlin | return qemu_send_packet_async_with_flags(sender, QEMU_NET_PACKET_FLAG_NONE,
|
520 | ca77d175 | Mark McLoughlin | buf, size, sent_cb); |
521 | f3b6c7fc | Mark McLoughlin | } |
522 | f3b6c7fc | Mark McLoughlin | |
523 | f3b6c7fc | Mark McLoughlin | void qemu_send_packet(VLANClientState *vc, const uint8_t *buf, int size) |
524 | f3b6c7fc | Mark McLoughlin | { |
525 | f3b6c7fc | Mark McLoughlin | qemu_send_packet_async(vc, buf, size, NULL);
|
526 | 63a01ef8 | aliguori | } |
527 | 63a01ef8 | aliguori | |
528 | ca77d175 | Mark McLoughlin | ssize_t qemu_send_packet_raw(VLANClientState *vc, const uint8_t *buf, int size) |
529 | ca77d175 | Mark McLoughlin | { |
530 | ca77d175 | Mark McLoughlin | return qemu_send_packet_async_with_flags(vc, QEMU_NET_PACKET_FLAG_RAW,
|
531 | ca77d175 | Mark McLoughlin | buf, size, NULL);
|
532 | ca77d175 | Mark McLoughlin | } |
533 | ca77d175 | Mark McLoughlin | |
534 | fbe78f4f | aliguori | static ssize_t vc_sendv_compat(VLANClientState *vc, const struct iovec *iov, |
535 | fbe78f4f | aliguori | int iovcnt)
|
536 | fbe78f4f | aliguori | { |
537 | fbe78f4f | aliguori | uint8_t buffer[4096];
|
538 | fbe78f4f | aliguori | size_t offset = 0;
|
539 | fbe78f4f | aliguori | int i;
|
540 | fbe78f4f | aliguori | |
541 | fbe78f4f | aliguori | for (i = 0; i < iovcnt; i++) { |
542 | fbe78f4f | aliguori | size_t len; |
543 | fbe78f4f | aliguori | |
544 | fbe78f4f | aliguori | len = MIN(sizeof(buffer) - offset, iov[i].iov_len);
|
545 | fbe78f4f | aliguori | memcpy(buffer + offset, iov[i].iov_base, len); |
546 | fbe78f4f | aliguori | offset += len; |
547 | fbe78f4f | aliguori | } |
548 | fbe78f4f | aliguori | |
549 | 665a3b07 | Mark McLoughlin | return vc->info->receive(vc, buffer, offset);
|
550 | fbe78f4f | aliguori | } |
551 | fbe78f4f | aliguori | |
552 | e0e7877a | aliguori | static ssize_t calc_iov_length(const struct iovec *iov, int iovcnt) |
553 | e0e7877a | aliguori | { |
554 | e0e7877a | aliguori | size_t offset = 0;
|
555 | e0e7877a | aliguori | int i;
|
556 | e0e7877a | aliguori | |
557 | e0e7877a | aliguori | for (i = 0; i < iovcnt; i++) |
558 | e0e7877a | aliguori | offset += iov[i].iov_len; |
559 | e0e7877a | aliguori | return offset;
|
560 | e0e7877a | aliguori | } |
561 | e0e7877a | aliguori | |
562 | 9a6ecb30 | Mark McLoughlin | static ssize_t qemu_deliver_packet_iov(VLANClientState *sender,
|
563 | c0b8e49c | Mark McLoughlin | unsigned flags,
|
564 | 9a6ecb30 | Mark McLoughlin | const struct iovec *iov, |
565 | 9a6ecb30 | Mark McLoughlin | int iovcnt,
|
566 | 9a6ecb30 | Mark McLoughlin | void *opaque)
|
567 | 9a6ecb30 | Mark McLoughlin | { |
568 | 9a6ecb30 | Mark McLoughlin | VLANClientState *vc = opaque; |
569 | 9a6ecb30 | Mark McLoughlin | |
570 | 9a6ecb30 | Mark McLoughlin | if (vc->link_down) {
|
571 | 9a6ecb30 | Mark McLoughlin | return calc_iov_length(iov, iovcnt);
|
572 | 9a6ecb30 | Mark McLoughlin | } |
573 | 9a6ecb30 | Mark McLoughlin | |
574 | 665a3b07 | Mark McLoughlin | if (vc->info->receive_iov) {
|
575 | 665a3b07 | Mark McLoughlin | return vc->info->receive_iov(vc, iov, iovcnt);
|
576 | 9a6ecb30 | Mark McLoughlin | } else {
|
577 | 9a6ecb30 | Mark McLoughlin | return vc_sendv_compat(vc, iov, iovcnt);
|
578 | 9a6ecb30 | Mark McLoughlin | } |
579 | 9a6ecb30 | Mark McLoughlin | } |
580 | 9a6ecb30 | Mark McLoughlin | |
581 | f7105843 | Mark McLoughlin | static ssize_t qemu_vlan_deliver_packet_iov(VLANClientState *sender,
|
582 | c0b8e49c | Mark McLoughlin | unsigned flags,
|
583 | f7105843 | Mark McLoughlin | const struct iovec *iov, |
584 | f7105843 | Mark McLoughlin | int iovcnt,
|
585 | f7105843 | Mark McLoughlin | void *opaque)
|
586 | fbe78f4f | aliguori | { |
587 | f7105843 | Mark McLoughlin | VLANState *vlan = opaque; |
588 | fbe78f4f | aliguori | VLANClientState *vc; |
589 | f7105843 | Mark McLoughlin | ssize_t ret = -1;
|
590 | e94667b9 | Mark McLoughlin | |
591 | f7105843 | Mark McLoughlin | QTAILQ_FOREACH(vc, &vlan->clients, next) { |
592 | e94667b9 | Mark McLoughlin | ssize_t len; |
593 | e94667b9 | Mark McLoughlin | |
594 | e94667b9 | Mark McLoughlin | if (vc == sender) {
|
595 | e94667b9 | Mark McLoughlin | continue;
|
596 | e94667b9 | Mark McLoughlin | } |
597 | e94667b9 | Mark McLoughlin | |
598 | e94667b9 | Mark McLoughlin | if (vc->link_down) {
|
599 | e94667b9 | Mark McLoughlin | ret = calc_iov_length(iov, iovcnt); |
600 | e94667b9 | Mark McLoughlin | continue;
|
601 | e94667b9 | Mark McLoughlin | } |
602 | e94667b9 | Mark McLoughlin | |
603 | ca77d175 | Mark McLoughlin | assert(!(flags & QEMU_NET_PACKET_FLAG_RAW)); |
604 | ca77d175 | Mark McLoughlin | |
605 | 665a3b07 | Mark McLoughlin | if (vc->info->receive_iov) {
|
606 | 665a3b07 | Mark McLoughlin | len = vc->info->receive_iov(vc, iov, iovcnt); |
607 | e94667b9 | Mark McLoughlin | } else {
|
608 | e94667b9 | Mark McLoughlin | len = vc_sendv_compat(vc, iov, iovcnt); |
609 | e94667b9 | Mark McLoughlin | } |
610 | e94667b9 | Mark McLoughlin | |
611 | e94667b9 | Mark McLoughlin | ret = (ret >= 0) ? ret : len;
|
612 | e94667b9 | Mark McLoughlin | } |
613 | e94667b9 | Mark McLoughlin | |
614 | e94667b9 | Mark McLoughlin | return ret;
|
615 | e94667b9 | Mark McLoughlin | } |
616 | e94667b9 | Mark McLoughlin | |
617 | f3b6c7fc | Mark McLoughlin | ssize_t qemu_sendv_packet_async(VLANClientState *sender, |
618 | f3b6c7fc | Mark McLoughlin | const struct iovec *iov, int iovcnt, |
619 | f3b6c7fc | Mark McLoughlin | NetPacketSent *sent_cb) |
620 | e94667b9 | Mark McLoughlin | { |
621 | 9a6ecb30 | Mark McLoughlin | NetQueue *queue; |
622 | 9a6ecb30 | Mark McLoughlin | |
623 | 9a6ecb30 | Mark McLoughlin | if (sender->link_down || (!sender->peer && !sender->vlan)) {
|
624 | e94667b9 | Mark McLoughlin | return calc_iov_length(iov, iovcnt);
|
625 | e94667b9 | Mark McLoughlin | } |
626 | e94667b9 | Mark McLoughlin | |
627 | 9a6ecb30 | Mark McLoughlin | if (sender->peer) {
|
628 | 9a6ecb30 | Mark McLoughlin | queue = sender->peer->send_queue; |
629 | 9a6ecb30 | Mark McLoughlin | } else {
|
630 | 9a6ecb30 | Mark McLoughlin | queue = sender->vlan->send_queue; |
631 | 9a6ecb30 | Mark McLoughlin | } |
632 | 9a6ecb30 | Mark McLoughlin | |
633 | c0b8e49c | Mark McLoughlin | return qemu_net_queue_send_iov(queue, sender,
|
634 | c0b8e49c | Mark McLoughlin | QEMU_NET_PACKET_FLAG_NONE, |
635 | c0b8e49c | Mark McLoughlin | iov, iovcnt, sent_cb); |
636 | fbe78f4f | aliguori | } |
637 | fbe78f4f | aliguori | |
638 | f3b6c7fc | Mark McLoughlin | ssize_t |
639 | f3b6c7fc | Mark McLoughlin | qemu_sendv_packet(VLANClientState *vc, const struct iovec *iov, int iovcnt) |
640 | f3b6c7fc | Mark McLoughlin | { |
641 | f3b6c7fc | Mark McLoughlin | return qemu_sendv_packet_async(vc, iov, iovcnt, NULL); |
642 | f3b6c7fc | Mark McLoughlin | } |
643 | f3b6c7fc | Mark McLoughlin | |
644 | 63a01ef8 | aliguori | /* find or alloc a new VLAN */
|
645 | 1a609520 | Jan Kiszka | VLANState *qemu_find_vlan(int id, int allocate) |
646 | 63a01ef8 | aliguori | { |
647 | 5610c3aa | Mark McLoughlin | VLANState *vlan; |
648 | 5610c3aa | Mark McLoughlin | |
649 | 5610c3aa | Mark McLoughlin | QTAILQ_FOREACH(vlan, &vlans, next) { |
650 | 5610c3aa | Mark McLoughlin | if (vlan->id == id) {
|
651 | 63a01ef8 | aliguori | return vlan;
|
652 | 5610c3aa | Mark McLoughlin | } |
653 | 63a01ef8 | aliguori | } |
654 | 5610c3aa | Mark McLoughlin | |
655 | 1a609520 | Jan Kiszka | if (!allocate) {
|
656 | 1a609520 | Jan Kiszka | return NULL; |
657 | 1a609520 | Jan Kiszka | } |
658 | 5610c3aa | Mark McLoughlin | |
659 | 63a01ef8 | aliguori | vlan = qemu_mallocz(sizeof(VLANState));
|
660 | 63a01ef8 | aliguori | vlan->id = id; |
661 | 5610c3aa | Mark McLoughlin | QTAILQ_INIT(&vlan->clients); |
662 | f7105843 | Mark McLoughlin | |
663 | f7105843 | Mark McLoughlin | vlan->send_queue = qemu_new_net_queue(qemu_vlan_deliver_packet, |
664 | f7105843 | Mark McLoughlin | qemu_vlan_deliver_packet_iov, |
665 | f7105843 | Mark McLoughlin | vlan); |
666 | 5610c3aa | Mark McLoughlin | |
667 | 5610c3aa | Mark McLoughlin | QTAILQ_INSERT_TAIL(&vlans, vlan, next); |
668 | 5610c3aa | Mark McLoughlin | |
669 | 63a01ef8 | aliguori | return vlan;
|
670 | 63a01ef8 | aliguori | } |
671 | 63a01ef8 | aliguori | |
672 | 2ef924b4 | Gerd Hoffmann | VLANClientState *qemu_find_netdev(const char *id) |
673 | 5869c4d5 | Mark McLoughlin | { |
674 | 5869c4d5 | Mark McLoughlin | VLANClientState *vc; |
675 | 5869c4d5 | Mark McLoughlin | |
676 | 5869c4d5 | Mark McLoughlin | QTAILQ_FOREACH(vc, &non_vlan_clients, next) { |
677 | 5869c4d5 | Mark McLoughlin | if (!strcmp(vc->name, id)) {
|
678 | 5869c4d5 | Mark McLoughlin | return vc;
|
679 | 5869c4d5 | Mark McLoughlin | } |
680 | 5869c4d5 | Mark McLoughlin | } |
681 | 5869c4d5 | Mark McLoughlin | |
682 | 5869c4d5 | Mark McLoughlin | return NULL; |
683 | 5869c4d5 | Mark McLoughlin | } |
684 | 5869c4d5 | Mark McLoughlin | |
685 | 7697079b | aliguori | static int nic_get_free_idx(void) |
686 | 7697079b | aliguori | { |
687 | 7697079b | aliguori | int index;
|
688 | 7697079b | aliguori | |
689 | 7697079b | aliguori | for (index = 0; index < MAX_NICS; index++) |
690 | 7697079b | aliguori | if (!nd_table[index].used)
|
691 | 7697079b | aliguori | return index;
|
692 | 7697079b | aliguori | return -1; |
693 | 7697079b | aliguori | } |
694 | 7697079b | aliguori | |
695 | 07caea31 | Markus Armbruster | int qemu_show_nic_models(const char *arg, const char *const *models) |
696 | 07caea31 | Markus Armbruster | { |
697 | 07caea31 | Markus Armbruster | int i;
|
698 | 07caea31 | Markus Armbruster | |
699 | 07caea31 | Markus Armbruster | if (!arg || strcmp(arg, "?")) |
700 | 07caea31 | Markus Armbruster | return 0; |
701 | 07caea31 | Markus Armbruster | |
702 | 07caea31 | Markus Armbruster | fprintf(stderr, "qemu: Supported NIC models: ");
|
703 | 07caea31 | Markus Armbruster | for (i = 0 ; models[i]; i++) |
704 | 07caea31 | Markus Armbruster | fprintf(stderr, "%s%c", models[i], models[i+1] ? ',' : '\n'); |
705 | 07caea31 | Markus Armbruster | return 1; |
706 | 07caea31 | Markus Armbruster | } |
707 | 07caea31 | Markus Armbruster | |
708 | d07f22c5 | aliguori | void qemu_check_nic_model(NICInfo *nd, const char *model) |
709 | d07f22c5 | aliguori | { |
710 | d07f22c5 | aliguori | const char *models[2]; |
711 | d07f22c5 | aliguori | |
712 | d07f22c5 | aliguori | models[0] = model;
|
713 | d07f22c5 | aliguori | models[1] = NULL; |
714 | d07f22c5 | aliguori | |
715 | 07caea31 | Markus Armbruster | if (qemu_show_nic_models(nd->model, models))
|
716 | 07caea31 | Markus Armbruster | exit(0);
|
717 | 07caea31 | Markus Armbruster | if (qemu_find_nic_model(nd, models, model) < 0) |
718 | 07caea31 | Markus Armbruster | exit(1);
|
719 | d07f22c5 | aliguori | } |
720 | d07f22c5 | aliguori | |
721 | 07caea31 | Markus Armbruster | int qemu_find_nic_model(NICInfo *nd, const char * const *models, |
722 | 07caea31 | Markus Armbruster | const char *default_model) |
723 | d07f22c5 | aliguori | { |
724 | 07caea31 | Markus Armbruster | int i;
|
725 | d07f22c5 | aliguori | |
726 | d07f22c5 | aliguori | if (!nd->model)
|
727 | 32a8e14a | Mark McLoughlin | nd->model = qemu_strdup(default_model); |
728 | d07f22c5 | aliguori | |
729 | 07caea31 | Markus Armbruster | for (i = 0 ; models[i]; i++) { |
730 | 07caea31 | Markus Armbruster | if (strcmp(nd->model, models[i]) == 0) |
731 | 07caea31 | Markus Armbruster | return i;
|
732 | d07f22c5 | aliguori | } |
733 | d07f22c5 | aliguori | |
734 | 07caea31 | Markus Armbruster | qemu_error("qemu: Unsupported NIC model: %s\n", nd->model);
|
735 | 07caea31 | Markus Armbruster | return -1; |
736 | d07f22c5 | aliguori | } |
737 | d07f22c5 | aliguori | |
738 | 5281d757 | Mark McLoughlin | int net_handle_fd_param(Monitor *mon, const char *param) |
739 | c1d6eed7 | Mark McLoughlin | { |
740 | c1d6eed7 | Mark McLoughlin | if (!qemu_isdigit(param[0])) { |
741 | c1d6eed7 | Mark McLoughlin | int fd;
|
742 | c1d6eed7 | Mark McLoughlin | |
743 | c1d6eed7 | Mark McLoughlin | fd = monitor_get_fd(mon, param); |
744 | c1d6eed7 | Mark McLoughlin | if (fd == -1) { |
745 | fb12577c | Markus Armbruster | qemu_error("No file descriptor named %s found", param);
|
746 | c1d6eed7 | Mark McLoughlin | return -1; |
747 | c1d6eed7 | Mark McLoughlin | } |
748 | c1d6eed7 | Mark McLoughlin | |
749 | c1d6eed7 | Mark McLoughlin | return fd;
|
750 | c1d6eed7 | Mark McLoughlin | } else {
|
751 | c1d6eed7 | Mark McLoughlin | return strtol(param, NULL, 0); |
752 | c1d6eed7 | Mark McLoughlin | } |
753 | c1d6eed7 | Mark McLoughlin | } |
754 | c1d6eed7 | Mark McLoughlin | |
755 | f6b134ac | Mark McLoughlin | static int net_init_nic(QemuOpts *opts, |
756 | f6b134ac | Mark McLoughlin | Monitor *mon, |
757 | f6b134ac | Mark McLoughlin | const char *name, |
758 | f6b134ac | Mark McLoughlin | VLANState *vlan) |
759 | f83c6e10 | Mark McLoughlin | { |
760 | f83c6e10 | Mark McLoughlin | int idx;
|
761 | f83c6e10 | Mark McLoughlin | NICInfo *nd; |
762 | 5869c4d5 | Mark McLoughlin | const char *netdev; |
763 | f83c6e10 | Mark McLoughlin | |
764 | f83c6e10 | Mark McLoughlin | idx = nic_get_free_idx(); |
765 | f83c6e10 | Mark McLoughlin | if (idx == -1 || nb_nics >= MAX_NICS) { |
766 | f83c6e10 | Mark McLoughlin | qemu_error("Too Many NICs\n");
|
767 | f83c6e10 | Mark McLoughlin | return -1; |
768 | f83c6e10 | Mark McLoughlin | } |
769 | f83c6e10 | Mark McLoughlin | |
770 | f83c6e10 | Mark McLoughlin | nd = &nd_table[idx]; |
771 | f83c6e10 | Mark McLoughlin | |
772 | f83c6e10 | Mark McLoughlin | memset(nd, 0, sizeof(*nd)); |
773 | f83c6e10 | Mark McLoughlin | |
774 | 5869c4d5 | Mark McLoughlin | if ((netdev = qemu_opt_get(opts, "netdev"))) { |
775 | 5869c4d5 | Mark McLoughlin | nd->netdev = qemu_find_netdev(netdev); |
776 | 5869c4d5 | Mark McLoughlin | if (!nd->netdev) {
|
777 | 5869c4d5 | Mark McLoughlin | qemu_error("netdev '%s' not found\n", netdev);
|
778 | 5869c4d5 | Mark McLoughlin | return -1; |
779 | 5869c4d5 | Mark McLoughlin | } |
780 | 5869c4d5 | Mark McLoughlin | } else {
|
781 | 5869c4d5 | Mark McLoughlin | assert(vlan); |
782 | 5869c4d5 | Mark McLoughlin | nd->vlan = vlan; |
783 | 5869c4d5 | Mark McLoughlin | } |
784 | 6d952ebe | Mark McLoughlin | if (name) {
|
785 | 6d952ebe | Mark McLoughlin | nd->name = qemu_strdup(name); |
786 | 6d952ebe | Mark McLoughlin | } |
787 | f83c6e10 | Mark McLoughlin | if (qemu_opt_get(opts, "model")) { |
788 | f83c6e10 | Mark McLoughlin | nd->model = qemu_strdup(qemu_opt_get(opts, "model"));
|
789 | f83c6e10 | Mark McLoughlin | } |
790 | f83c6e10 | Mark McLoughlin | if (qemu_opt_get(opts, "addr")) { |
791 | f83c6e10 | Mark McLoughlin | nd->devaddr = qemu_strdup(qemu_opt_get(opts, "addr"));
|
792 | f83c6e10 | Mark McLoughlin | } |
793 | f83c6e10 | Mark McLoughlin | |
794 | f83c6e10 | Mark McLoughlin | nd->macaddr[0] = 0x52; |
795 | f83c6e10 | Mark McLoughlin | nd->macaddr[1] = 0x54; |
796 | f83c6e10 | Mark McLoughlin | nd->macaddr[2] = 0x00; |
797 | f83c6e10 | Mark McLoughlin | nd->macaddr[3] = 0x12; |
798 | f83c6e10 | Mark McLoughlin | nd->macaddr[4] = 0x34; |
799 | f83c6e10 | Mark McLoughlin | nd->macaddr[5] = 0x56 + idx; |
800 | f83c6e10 | Mark McLoughlin | |
801 | f83c6e10 | Mark McLoughlin | if (qemu_opt_get(opts, "macaddr") && |
802 | f1d078c3 | Mark McLoughlin | net_parse_macaddr(nd->macaddr, qemu_opt_get(opts, "macaddr")) < 0) { |
803 | f83c6e10 | Mark McLoughlin | qemu_error("invalid syntax for ethernet address\n");
|
804 | f83c6e10 | Mark McLoughlin | return -1; |
805 | f83c6e10 | Mark McLoughlin | } |
806 | f83c6e10 | Mark McLoughlin | |
807 | f83c6e10 | Mark McLoughlin | nd->nvectors = qemu_opt_get_number(opts, "vectors", NIC_NVECTORS_UNSPECIFIED);
|
808 | f83c6e10 | Mark McLoughlin | if (nd->nvectors != NIC_NVECTORS_UNSPECIFIED &&
|
809 | f83c6e10 | Mark McLoughlin | (nd->nvectors < 0 || nd->nvectors > 0x7ffffff)) { |
810 | f83c6e10 | Mark McLoughlin | qemu_error("invalid # of vectors: %d\n", nd->nvectors);
|
811 | f83c6e10 | Mark McLoughlin | return -1; |
812 | f83c6e10 | Mark McLoughlin | } |
813 | f83c6e10 | Mark McLoughlin | |
814 | f83c6e10 | Mark McLoughlin | nd->used = 1;
|
815 | 5869c4d5 | Mark McLoughlin | if (vlan) {
|
816 | 5869c4d5 | Mark McLoughlin | nd->vlan->nb_guest_devs++; |
817 | 5869c4d5 | Mark McLoughlin | } |
818 | f83c6e10 | Mark McLoughlin | nb_nics++; |
819 | f83c6e10 | Mark McLoughlin | |
820 | f83c6e10 | Mark McLoughlin | return idx;
|
821 | f83c6e10 | Mark McLoughlin | } |
822 | f83c6e10 | Mark McLoughlin | |
823 | f83c6e10 | Mark McLoughlin | #define NET_COMMON_PARAMS_DESC \
|
824 | f83c6e10 | Mark McLoughlin | { \ |
825 | f83c6e10 | Mark McLoughlin | .name = "type", \
|
826 | f83c6e10 | Mark McLoughlin | .type = QEMU_OPT_STRING, \ |
827 | f83c6e10 | Mark McLoughlin | .help = "net client type (nic, tap etc.)", \
|
828 | f83c6e10 | Mark McLoughlin | }, { \ |
829 | f83c6e10 | Mark McLoughlin | .name = "vlan", \
|
830 | f83c6e10 | Mark McLoughlin | .type = QEMU_OPT_NUMBER, \ |
831 | f83c6e10 | Mark McLoughlin | .help = "vlan number", \
|
832 | f83c6e10 | Mark McLoughlin | }, { \ |
833 | f83c6e10 | Mark McLoughlin | .name = "name", \
|
834 | f83c6e10 | Mark McLoughlin | .type = QEMU_OPT_STRING, \ |
835 | f83c6e10 | Mark McLoughlin | .help = "identifier for monitor commands", \
|
836 | f83c6e10 | Mark McLoughlin | } |
837 | f83c6e10 | Mark McLoughlin | |
838 | 6d952ebe | Mark McLoughlin | typedef int (*net_client_init_func)(QemuOpts *opts, |
839 | 6d952ebe | Mark McLoughlin | Monitor *mon, |
840 | f6b134ac | Mark McLoughlin | const char *name, |
841 | f6b134ac | Mark McLoughlin | VLANState *vlan); |
842 | f83c6e10 | Mark McLoughlin | |
843 | f83c6e10 | Mark McLoughlin | /* magic number, but compiler will warn if too small */
|
844 | f83c6e10 | Mark McLoughlin | #define NET_MAX_DESC 20 |
845 | f83c6e10 | Mark McLoughlin | |
846 | f83c6e10 | Mark McLoughlin | static struct { |
847 | f83c6e10 | Mark McLoughlin | const char *type; |
848 | f83c6e10 | Mark McLoughlin | net_client_init_func init; |
849 | f83c6e10 | Mark McLoughlin | QemuOptDesc desc[NET_MAX_DESC]; |
850 | f83c6e10 | Mark McLoughlin | } net_client_types[] = { |
851 | f83c6e10 | Mark McLoughlin | { |
852 | f83c6e10 | Mark McLoughlin | .type = "none",
|
853 | f83c6e10 | Mark McLoughlin | .desc = { |
854 | f83c6e10 | Mark McLoughlin | NET_COMMON_PARAMS_DESC, |
855 | f83c6e10 | Mark McLoughlin | { /* end of list */ }
|
856 | f83c6e10 | Mark McLoughlin | }, |
857 | f83c6e10 | Mark McLoughlin | }, { |
858 | f83c6e10 | Mark McLoughlin | .type = "nic",
|
859 | f83c6e10 | Mark McLoughlin | .init = net_init_nic, |
860 | f83c6e10 | Mark McLoughlin | .desc = { |
861 | f83c6e10 | Mark McLoughlin | NET_COMMON_PARAMS_DESC, |
862 | f83c6e10 | Mark McLoughlin | { |
863 | 5869c4d5 | Mark McLoughlin | .name = "netdev",
|
864 | 5869c4d5 | Mark McLoughlin | .type = QEMU_OPT_STRING, |
865 | 5869c4d5 | Mark McLoughlin | .help = "id of -netdev to connect to",
|
866 | 5869c4d5 | Mark McLoughlin | }, |
867 | 5869c4d5 | Mark McLoughlin | { |
868 | f83c6e10 | Mark McLoughlin | .name = "macaddr",
|
869 | f83c6e10 | Mark McLoughlin | .type = QEMU_OPT_STRING, |
870 | f83c6e10 | Mark McLoughlin | .help = "MAC address",
|
871 | f83c6e10 | Mark McLoughlin | }, { |
872 | f83c6e10 | Mark McLoughlin | .name = "model",
|
873 | f83c6e10 | Mark McLoughlin | .type = QEMU_OPT_STRING, |
874 | f83c6e10 | Mark McLoughlin | .help = "device model (e1000, rtl8139, virtio etc.)",
|
875 | f83c6e10 | Mark McLoughlin | }, { |
876 | f83c6e10 | Mark McLoughlin | .name = "addr",
|
877 | f83c6e10 | Mark McLoughlin | .type = QEMU_OPT_STRING, |
878 | f83c6e10 | Mark McLoughlin | .help = "PCI device address",
|
879 | f83c6e10 | Mark McLoughlin | }, { |
880 | f83c6e10 | Mark McLoughlin | .name = "vectors",
|
881 | f83c6e10 | Mark McLoughlin | .type = QEMU_OPT_NUMBER, |
882 | f83c6e10 | Mark McLoughlin | .help = "number of MSI-x vectors, 0 to disable MSI-X",
|
883 | f83c6e10 | Mark McLoughlin | }, |
884 | f83c6e10 | Mark McLoughlin | { /* end of list */ }
|
885 | f83c6e10 | Mark McLoughlin | }, |
886 | ec302ffd | Mark McLoughlin | #ifdef CONFIG_SLIRP
|
887 | ec302ffd | Mark McLoughlin | }, { |
888 | ec302ffd | Mark McLoughlin | .type = "user",
|
889 | ec302ffd | Mark McLoughlin | .init = net_init_slirp, |
890 | ec302ffd | Mark McLoughlin | .desc = { |
891 | ec302ffd | Mark McLoughlin | NET_COMMON_PARAMS_DESC, |
892 | ec302ffd | Mark McLoughlin | { |
893 | ec302ffd | Mark McLoughlin | .name = "hostname",
|
894 | ec302ffd | Mark McLoughlin | .type = QEMU_OPT_STRING, |
895 | ec302ffd | Mark McLoughlin | .help = "client hostname reported by the builtin DHCP server",
|
896 | ec302ffd | Mark McLoughlin | }, { |
897 | ec302ffd | Mark McLoughlin | .name = "restrict",
|
898 | ec302ffd | Mark McLoughlin | .type = QEMU_OPT_STRING, |
899 | ec302ffd | Mark McLoughlin | .help = "isolate the guest from the host (y|yes|n|no)",
|
900 | ec302ffd | Mark McLoughlin | }, { |
901 | ec302ffd | Mark McLoughlin | .name = "ip",
|
902 | ec302ffd | Mark McLoughlin | .type = QEMU_OPT_STRING, |
903 | ec302ffd | Mark McLoughlin | .help = "legacy parameter, use net= instead",
|
904 | ec302ffd | Mark McLoughlin | }, { |
905 | ec302ffd | Mark McLoughlin | .name = "net",
|
906 | ec302ffd | Mark McLoughlin | .type = QEMU_OPT_STRING, |
907 | ec302ffd | Mark McLoughlin | .help = "IP address and optional netmask",
|
908 | ec302ffd | Mark McLoughlin | }, { |
909 | ec302ffd | Mark McLoughlin | .name = "host",
|
910 | ec302ffd | Mark McLoughlin | .type = QEMU_OPT_STRING, |
911 | ec302ffd | Mark McLoughlin | .help = "guest-visible address of the host",
|
912 | ec302ffd | Mark McLoughlin | }, { |
913 | ec302ffd | Mark McLoughlin | .name = "tftp",
|
914 | ec302ffd | Mark McLoughlin | .type = QEMU_OPT_STRING, |
915 | ec302ffd | Mark McLoughlin | .help = "root directory of the built-in TFTP server",
|
916 | ec302ffd | Mark McLoughlin | }, { |
917 | ec302ffd | Mark McLoughlin | .name = "bootfile",
|
918 | ec302ffd | Mark McLoughlin | .type = QEMU_OPT_STRING, |
919 | ec302ffd | Mark McLoughlin | .help = "BOOTP filename, for use with tftp=",
|
920 | ec302ffd | Mark McLoughlin | }, { |
921 | ec302ffd | Mark McLoughlin | .name = "dhcpstart",
|
922 | ec302ffd | Mark McLoughlin | .type = QEMU_OPT_STRING, |
923 | ec302ffd | Mark McLoughlin | .help = "the first of the 16 IPs the built-in DHCP server can assign",
|
924 | ec302ffd | Mark McLoughlin | }, { |
925 | ec302ffd | Mark McLoughlin | .name = "dns",
|
926 | ec302ffd | Mark McLoughlin | .type = QEMU_OPT_STRING, |
927 | ec302ffd | Mark McLoughlin | .help = "guest-visible address of the virtual nameserver",
|
928 | ec302ffd | Mark McLoughlin | }, { |
929 | ec302ffd | Mark McLoughlin | .name = "smb",
|
930 | ec302ffd | Mark McLoughlin | .type = QEMU_OPT_STRING, |
931 | ec302ffd | Mark McLoughlin | .help = "root directory of the built-in SMB server",
|
932 | ec302ffd | Mark McLoughlin | }, { |
933 | ec302ffd | Mark McLoughlin | .name = "smbserver",
|
934 | ec302ffd | Mark McLoughlin | .type = QEMU_OPT_STRING, |
935 | ec302ffd | Mark McLoughlin | .help = "IP address of the built-in SMB server",
|
936 | ec302ffd | Mark McLoughlin | }, { |
937 | ec302ffd | Mark McLoughlin | .name = "hostfwd",
|
938 | ec302ffd | Mark McLoughlin | .type = QEMU_OPT_STRING, |
939 | ec302ffd | Mark McLoughlin | .help = "guest port number to forward incoming TCP or UDP connections",
|
940 | ec302ffd | Mark McLoughlin | }, { |
941 | ec302ffd | Mark McLoughlin | .name = "guestfwd",
|
942 | ec302ffd | Mark McLoughlin | .type = QEMU_OPT_STRING, |
943 | ec302ffd | Mark McLoughlin | .help = "IP address and port to forward guest TCP connections",
|
944 | ec302ffd | Mark McLoughlin | }, |
945 | ec302ffd | Mark McLoughlin | { /* end of list */ }
|
946 | ec302ffd | Mark McLoughlin | }, |
947 | ec302ffd | Mark McLoughlin | #endif
|
948 | 8a1c5235 | Mark McLoughlin | }, { |
949 | 8a1c5235 | Mark McLoughlin | .type = "tap",
|
950 | a8ed73f7 | Mark McLoughlin | .init = net_init_tap, |
951 | 8a1c5235 | Mark McLoughlin | .desc = { |
952 | 8a1c5235 | Mark McLoughlin | NET_COMMON_PARAMS_DESC, |
953 | 8a1c5235 | Mark McLoughlin | { |
954 | 8a1c5235 | Mark McLoughlin | .name = "ifname",
|
955 | 8a1c5235 | Mark McLoughlin | .type = QEMU_OPT_STRING, |
956 | 8a1c5235 | Mark McLoughlin | .help = "interface name",
|
957 | 8a1c5235 | Mark McLoughlin | }, |
958 | a8ed73f7 | Mark McLoughlin | #ifndef _WIN32
|
959 | 8a1c5235 | Mark McLoughlin | { |
960 | 8a1c5235 | Mark McLoughlin | .name = "fd",
|
961 | 8a1c5235 | Mark McLoughlin | .type = QEMU_OPT_STRING, |
962 | 8a1c5235 | Mark McLoughlin | .help = "file descriptor of an already opened tap",
|
963 | 8a1c5235 | Mark McLoughlin | }, { |
964 | 8a1c5235 | Mark McLoughlin | .name = "script",
|
965 | 8a1c5235 | Mark McLoughlin | .type = QEMU_OPT_STRING, |
966 | 8a1c5235 | Mark McLoughlin | .help = "script to initialize the interface",
|
967 | 8a1c5235 | Mark McLoughlin | }, { |
968 | 8a1c5235 | Mark McLoughlin | .name = "downscript",
|
969 | 8a1c5235 | Mark McLoughlin | .type = QEMU_OPT_STRING, |
970 | 8a1c5235 | Mark McLoughlin | .help = "script to shut down the interface",
|
971 | 8a1c5235 | Mark McLoughlin | }, { |
972 | 8a1c5235 | Mark McLoughlin | .name = "sndbuf",
|
973 | 8a1c5235 | Mark McLoughlin | .type = QEMU_OPT_SIZE, |
974 | 8a1c5235 | Mark McLoughlin | .help = "send buffer limit"
|
975 | baf74c95 | Mark McLoughlin | }, { |
976 | baf74c95 | Mark McLoughlin | .name = "vnet_hdr",
|
977 | baf74c95 | Mark McLoughlin | .type = QEMU_OPT_BOOL, |
978 | baf74c95 | Mark McLoughlin | .help = "enable the IFF_VNET_HDR flag on the tap interface"
|
979 | 8a1c5235 | Mark McLoughlin | }, |
980 | a8ed73f7 | Mark McLoughlin | #endif /* _WIN32 */ |
981 | 8a1c5235 | Mark McLoughlin | { /* end of list */ }
|
982 | 8a1c5235 | Mark McLoughlin | }, |
983 | 88ce16ca | Mark McLoughlin | }, { |
984 | 88ce16ca | Mark McLoughlin | .type = "socket",
|
985 | 88ce16ca | Mark McLoughlin | .init = net_init_socket, |
986 | 88ce16ca | Mark McLoughlin | .desc = { |
987 | 88ce16ca | Mark McLoughlin | NET_COMMON_PARAMS_DESC, |
988 | 88ce16ca | Mark McLoughlin | { |
989 | 88ce16ca | Mark McLoughlin | .name = "fd",
|
990 | 88ce16ca | Mark McLoughlin | .type = QEMU_OPT_STRING, |
991 | 88ce16ca | Mark McLoughlin | .help = "file descriptor of an already opened socket",
|
992 | 88ce16ca | Mark McLoughlin | }, { |
993 | 88ce16ca | Mark McLoughlin | .name = "listen",
|
994 | 88ce16ca | Mark McLoughlin | .type = QEMU_OPT_STRING, |
995 | 88ce16ca | Mark McLoughlin | .help = "port number, and optional hostname, to listen on",
|
996 | 88ce16ca | Mark McLoughlin | }, { |
997 | 88ce16ca | Mark McLoughlin | .name = "connect",
|
998 | 88ce16ca | Mark McLoughlin | .type = QEMU_OPT_STRING, |
999 | 88ce16ca | Mark McLoughlin | .help = "port number, and optional hostname, to connect to",
|
1000 | 88ce16ca | Mark McLoughlin | }, { |
1001 | 88ce16ca | Mark McLoughlin | .name = "mcast",
|
1002 | 88ce16ca | Mark McLoughlin | .type = QEMU_OPT_STRING, |
1003 | 88ce16ca | Mark McLoughlin | .help = "UDP multicast address and port number",
|
1004 | 88ce16ca | Mark McLoughlin | }, |
1005 | 88ce16ca | Mark McLoughlin | { /* end of list */ }
|
1006 | 88ce16ca | Mark McLoughlin | }, |
1007 | dd51058d | Mark McLoughlin | #ifdef CONFIG_VDE
|
1008 | dd51058d | Mark McLoughlin | }, { |
1009 | dd51058d | Mark McLoughlin | .type = "vde",
|
1010 | dd51058d | Mark McLoughlin | .init = net_init_vde, |
1011 | dd51058d | Mark McLoughlin | .desc = { |
1012 | dd51058d | Mark McLoughlin | NET_COMMON_PARAMS_DESC, |
1013 | dd51058d | Mark McLoughlin | { |
1014 | dd51058d | Mark McLoughlin | .name = "sock",
|
1015 | dd51058d | Mark McLoughlin | .type = QEMU_OPT_STRING, |
1016 | dd51058d | Mark McLoughlin | .help = "socket path",
|
1017 | dd51058d | Mark McLoughlin | }, { |
1018 | dd51058d | Mark McLoughlin | .name = "port",
|
1019 | dd51058d | Mark McLoughlin | .type = QEMU_OPT_NUMBER, |
1020 | dd51058d | Mark McLoughlin | .help = "port number",
|
1021 | dd51058d | Mark McLoughlin | }, { |
1022 | dd51058d | Mark McLoughlin | .name = "group",
|
1023 | dd51058d | Mark McLoughlin | .type = QEMU_OPT_STRING, |
1024 | dd51058d | Mark McLoughlin | .help = "group owner of socket",
|
1025 | dd51058d | Mark McLoughlin | }, { |
1026 | dd51058d | Mark McLoughlin | .name = "mode",
|
1027 | dd51058d | Mark McLoughlin | .type = QEMU_OPT_NUMBER, |
1028 | dd51058d | Mark McLoughlin | .help = "permissions for socket",
|
1029 | dd51058d | Mark McLoughlin | }, |
1030 | dd51058d | Mark McLoughlin | { /* end of list */ }
|
1031 | dd51058d | Mark McLoughlin | }, |
1032 | dd51058d | Mark McLoughlin | #endif
|
1033 | ed2955c2 | Mark McLoughlin | }, { |
1034 | ed2955c2 | Mark McLoughlin | .type = "dump",
|
1035 | ed2955c2 | Mark McLoughlin | .init = net_init_dump, |
1036 | ed2955c2 | Mark McLoughlin | .desc = { |
1037 | ed2955c2 | Mark McLoughlin | NET_COMMON_PARAMS_DESC, |
1038 | ed2955c2 | Mark McLoughlin | { |
1039 | ed2955c2 | Mark McLoughlin | .name = "len",
|
1040 | ed2955c2 | Mark McLoughlin | .type = QEMU_OPT_SIZE, |
1041 | ed2955c2 | Mark McLoughlin | .help = "per-packet size limit (64k default)",
|
1042 | ed2955c2 | Mark McLoughlin | }, { |
1043 | ed2955c2 | Mark McLoughlin | .name = "file",
|
1044 | ed2955c2 | Mark McLoughlin | .type = QEMU_OPT_STRING, |
1045 | ed2955c2 | Mark McLoughlin | .help = "dump file path (default is qemu-vlan0.pcap)",
|
1046 | ed2955c2 | Mark McLoughlin | }, |
1047 | ed2955c2 | Mark McLoughlin | { /* end of list */ }
|
1048 | ed2955c2 | Mark McLoughlin | }, |
1049 | f83c6e10 | Mark McLoughlin | }, |
1050 | f83c6e10 | Mark McLoughlin | { /* end of list */ }
|
1051 | f83c6e10 | Mark McLoughlin | }; |
1052 | f83c6e10 | Mark McLoughlin | |
1053 | f6b134ac | Mark McLoughlin | int net_client_init(Monitor *mon, QemuOpts *opts, int is_netdev) |
1054 | f83c6e10 | Mark McLoughlin | { |
1055 | 6d952ebe | Mark McLoughlin | const char *name; |
1056 | f83c6e10 | Mark McLoughlin | const char *type; |
1057 | f83c6e10 | Mark McLoughlin | int i;
|
1058 | f83c6e10 | Mark McLoughlin | |
1059 | f83c6e10 | Mark McLoughlin | type = qemu_opt_get(opts, "type");
|
1060 | f83c6e10 | Mark McLoughlin | |
1061 | 0f2fbf40 | Mark McLoughlin | if (!is_netdev) {
|
1062 | 0f2fbf40 | Mark McLoughlin | if (!type) {
|
1063 | 0f2fbf40 | Mark McLoughlin | qemu_error("No type specified for -net\n");
|
1064 | 0f2fbf40 | Mark McLoughlin | return -1; |
1065 | 0f2fbf40 | Mark McLoughlin | } |
1066 | 0f2fbf40 | Mark McLoughlin | } else {
|
1067 | 0f2fbf40 | Mark McLoughlin | if (!type) {
|
1068 | 0f2fbf40 | Mark McLoughlin | qemu_error("No type specified for -netdev\n");
|
1069 | 0f2fbf40 | Mark McLoughlin | return -1; |
1070 | 0f2fbf40 | Mark McLoughlin | } |
1071 | 0f2fbf40 | Mark McLoughlin | |
1072 | f6b134ac | Mark McLoughlin | if (strcmp(type, "tap") != 0 && |
1073 | f6b134ac | Mark McLoughlin | #ifdef CONFIG_SLIRP
|
1074 | f6b134ac | Mark McLoughlin | strcmp(type, "user") != 0 && |
1075 | f6b134ac | Mark McLoughlin | #endif
|
1076 | f6b134ac | Mark McLoughlin | #ifdef CONFIG_VDE
|
1077 | f6b134ac | Mark McLoughlin | strcmp(type, "vde") != 0 && |
1078 | f6b134ac | Mark McLoughlin | #endif
|
1079 | f6b134ac | Mark McLoughlin | strcmp(type, "socket") != 0) { |
1080 | f6b134ac | Mark McLoughlin | qemu_error("The '%s' network backend type is not valid with -netdev\n",
|
1081 | f6b134ac | Mark McLoughlin | type); |
1082 | f6b134ac | Mark McLoughlin | return -1; |
1083 | f6b134ac | Mark McLoughlin | } |
1084 | f6b134ac | Mark McLoughlin | |
1085 | f6b134ac | Mark McLoughlin | if (qemu_opt_get(opts, "vlan")) { |
1086 | f6b134ac | Mark McLoughlin | qemu_error("The 'vlan' parameter is not valid with -netdev\n");
|
1087 | f6b134ac | Mark McLoughlin | return -1; |
1088 | f6b134ac | Mark McLoughlin | } |
1089 | f6b134ac | Mark McLoughlin | if (qemu_opt_get(opts, "name")) { |
1090 | f6b134ac | Mark McLoughlin | qemu_error("The 'name' parameter is not valid with -netdev\n");
|
1091 | f6b134ac | Mark McLoughlin | return -1; |
1092 | f6b134ac | Mark McLoughlin | } |
1093 | f6b134ac | Mark McLoughlin | if (!qemu_opts_id(opts)) {
|
1094 | f6b134ac | Mark McLoughlin | qemu_error("The id= parameter is required with -netdev\n");
|
1095 | f6b134ac | Mark McLoughlin | return -1; |
1096 | f6b134ac | Mark McLoughlin | } |
1097 | f6b134ac | Mark McLoughlin | } |
1098 | f6b134ac | Mark McLoughlin | |
1099 | 6d952ebe | Mark McLoughlin | name = qemu_opts_id(opts); |
1100 | 6d952ebe | Mark McLoughlin | if (!name) {
|
1101 | 6d952ebe | Mark McLoughlin | name = qemu_opt_get(opts, "name");
|
1102 | 6d952ebe | Mark McLoughlin | } |
1103 | 6d952ebe | Mark McLoughlin | |
1104 | f83c6e10 | Mark McLoughlin | for (i = 0; net_client_types[i].type != NULL; i++) { |
1105 | f83c6e10 | Mark McLoughlin | if (!strcmp(net_client_types[i].type, type)) {
|
1106 | f6b134ac | Mark McLoughlin | VLANState *vlan = NULL;
|
1107 | f6b134ac | Mark McLoughlin | |
1108 | f83c6e10 | Mark McLoughlin | if (qemu_opts_validate(opts, &net_client_types[i].desc[0]) == -1) { |
1109 | f83c6e10 | Mark McLoughlin | return -1; |
1110 | f83c6e10 | Mark McLoughlin | } |
1111 | f83c6e10 | Mark McLoughlin | |
1112 | 5869c4d5 | Mark McLoughlin | /* Do not add to a vlan if it's a -netdev or a nic with a
|
1113 | 5869c4d5 | Mark McLoughlin | * netdev= parameter. */
|
1114 | 5869c4d5 | Mark McLoughlin | if (!(is_netdev ||
|
1115 | 5869c4d5 | Mark McLoughlin | (strcmp(type, "nic") == 0 && qemu_opt_get(opts, "netdev")))) { |
1116 | f6b134ac | Mark McLoughlin | vlan = qemu_find_vlan(qemu_opt_get_number(opts, "vlan", 0), 1); |
1117 | f6b134ac | Mark McLoughlin | } |
1118 | f6b134ac | Mark McLoughlin | |
1119 | f83c6e10 | Mark McLoughlin | if (net_client_types[i].init) {
|
1120 | f6b134ac | Mark McLoughlin | return net_client_types[i].init(opts, mon, name, vlan);
|
1121 | f83c6e10 | Mark McLoughlin | } else {
|
1122 | f83c6e10 | Mark McLoughlin | return 0; |
1123 | f83c6e10 | Mark McLoughlin | } |
1124 | f83c6e10 | Mark McLoughlin | } |
1125 | f83c6e10 | Mark McLoughlin | } |
1126 | f83c6e10 | Mark McLoughlin | |
1127 | f83c6e10 | Mark McLoughlin | qemu_error("Invalid -net type '%s'\n", type);
|
1128 | f83c6e10 | Mark McLoughlin | return -1; |
1129 | f83c6e10 | Mark McLoughlin | } |
1130 | f83c6e10 | Mark McLoughlin | |
1131 | 8b13c4a7 | aliguori | void net_client_uninit(NICInfo *nd)
|
1132 | 8b13c4a7 | aliguori | { |
1133 | d80b9fc6 | Mark McLoughlin | if (nd->vlan) {
|
1134 | d80b9fc6 | Mark McLoughlin | nd->vlan->nb_guest_devs--; |
1135 | d80b9fc6 | Mark McLoughlin | } |
1136 | 8b13c4a7 | aliguori | nb_nics--; |
1137 | a9796703 | Glauber Costa | |
1138 | 9203f520 | Mark McLoughlin | qemu_free(nd->model); |
1139 | 9203f520 | Mark McLoughlin | qemu_free(nd->name); |
1140 | 9203f520 | Mark McLoughlin | qemu_free(nd->devaddr); |
1141 | a9796703 | Glauber Costa | |
1142 | d2cffe30 | Mark McLoughlin | nd->used = 0;
|
1143 | 8b13c4a7 | aliguori | } |
1144 | 8b13c4a7 | aliguori | |
1145 | 6f338c34 | aliguori | static int net_host_check_device(const char *device) |
1146 | 6f338c34 | aliguori | { |
1147 | 6f338c34 | aliguori | int i;
|
1148 | bb9ea79e | aliguori | const char *valid_param_list[] = { "tap", "socket", "dump" |
1149 | 6f338c34 | aliguori | #ifdef CONFIG_SLIRP
|
1150 | 6f338c34 | aliguori | ,"user"
|
1151 | 6f338c34 | aliguori | #endif
|
1152 | 6f338c34 | aliguori | #ifdef CONFIG_VDE
|
1153 | 6f338c34 | aliguori | ,"vde"
|
1154 | 6f338c34 | aliguori | #endif
|
1155 | 6f338c34 | aliguori | }; |
1156 | 6f338c34 | aliguori | for (i = 0; i < sizeof(valid_param_list) / sizeof(char *); i++) { |
1157 | 6f338c34 | aliguori | if (!strncmp(valid_param_list[i], device,
|
1158 | 6f338c34 | aliguori | strlen(valid_param_list[i]))) |
1159 | 6f338c34 | aliguori | return 1; |
1160 | 6f338c34 | aliguori | } |
1161 | 6f338c34 | aliguori | |
1162 | 6f338c34 | aliguori | return 0; |
1163 | 6f338c34 | aliguori | } |
1164 | 6f338c34 | aliguori | |
1165 | f18c16de | Luiz Capitulino | void net_host_device_add(Monitor *mon, const QDict *qdict) |
1166 | 6f338c34 | aliguori | { |
1167 | f18c16de | Luiz Capitulino | const char *device = qdict_get_str(qdict, "device"); |
1168 | 7f1c9d20 | Mark McLoughlin | const char *opts_str = qdict_get_try_str(qdict, "opts"); |
1169 | 7f1c9d20 | Mark McLoughlin | QemuOpts *opts; |
1170 | f18c16de | Luiz Capitulino | |
1171 | 6f338c34 | aliguori | if (!net_host_check_device(device)) {
|
1172 | 376253ec | aliguori | monitor_printf(mon, "invalid host network device %s\n", device);
|
1173 | 6f338c34 | aliguori | return;
|
1174 | 6f338c34 | aliguori | } |
1175 | 7f1c9d20 | Mark McLoughlin | |
1176 | 7f1c9d20 | Mark McLoughlin | opts = qemu_opts_parse(&qemu_net_opts, opts_str ? opts_str : "", NULL); |
1177 | 7f1c9d20 | Mark McLoughlin | if (!opts) {
|
1178 | 7f1c9d20 | Mark McLoughlin | monitor_printf(mon, "parsing network options '%s' failed\n",
|
1179 | 7f1c9d20 | Mark McLoughlin | opts_str ? opts_str : "");
|
1180 | 7f1c9d20 | Mark McLoughlin | return;
|
1181 | 7f1c9d20 | Mark McLoughlin | } |
1182 | 7f1c9d20 | Mark McLoughlin | |
1183 | 7f1c9d20 | Mark McLoughlin | qemu_opt_set(opts, "type", device);
|
1184 | 7f1c9d20 | Mark McLoughlin | |
1185 | f6b134ac | Mark McLoughlin | if (net_client_init(mon, opts, 0) < 0) { |
1186 | 5c8be678 | aliguori | monitor_printf(mon, "adding host network device %s failed\n", device);
|
1187 | 5c8be678 | aliguori | } |
1188 | 6f338c34 | aliguori | } |
1189 | 6f338c34 | aliguori | |
1190 | f18c16de | Luiz Capitulino | void net_host_device_remove(Monitor *mon, const QDict *qdict) |
1191 | 6f338c34 | aliguori | { |
1192 | 6f338c34 | aliguori | VLANClientState *vc; |
1193 | f18c16de | Luiz Capitulino | int vlan_id = qdict_get_int(qdict, "vlan_id"); |
1194 | f18c16de | Luiz Capitulino | const char *device = qdict_get_str(qdict, "device"); |
1195 | 6f338c34 | aliguori | |
1196 | 1a609520 | Jan Kiszka | vc = qemu_find_vlan_client_by_name(mon, vlan_id, device); |
1197 | 6f338c34 | aliguori | if (!vc) {
|
1198 | 6f338c34 | aliguori | return;
|
1199 | 6f338c34 | aliguori | } |
1200 | e8f1f9db | aliguori | if (!net_host_check_device(vc->model)) {
|
1201 | e8f1f9db | aliguori | monitor_printf(mon, "invalid host network device %s\n", device);
|
1202 | e8f1f9db | aliguori | return;
|
1203 | e8f1f9db | aliguori | } |
1204 | 6f338c34 | aliguori | qemu_del_vlan_client(vc); |
1205 | 6f338c34 | aliguori | } |
1206 | 6f338c34 | aliguori | |
1207 | 406c8df3 | Glauber Costa | void net_set_boot_mask(int net_boot_mask) |
1208 | 406c8df3 | Glauber Costa | { |
1209 | 406c8df3 | Glauber Costa | int i;
|
1210 | 406c8df3 | Glauber Costa | |
1211 | 406c8df3 | Glauber Costa | /* Only the first four NICs may be bootable */
|
1212 | 406c8df3 | Glauber Costa | net_boot_mask = net_boot_mask & 0xF;
|
1213 | 406c8df3 | Glauber Costa | |
1214 | 406c8df3 | Glauber Costa | for (i = 0; i < nb_nics; i++) { |
1215 | 406c8df3 | Glauber Costa | if (net_boot_mask & (1 << i)) { |
1216 | 406c8df3 | Glauber Costa | nd_table[i].bootable = 1;
|
1217 | 406c8df3 | Glauber Costa | net_boot_mask &= ~(1 << i);
|
1218 | 406c8df3 | Glauber Costa | } |
1219 | 406c8df3 | Glauber Costa | } |
1220 | 406c8df3 | Glauber Costa | |
1221 | 406c8df3 | Glauber Costa | if (net_boot_mask) {
|
1222 | 406c8df3 | Glauber Costa | fprintf(stderr, "Cannot boot from non-existent NIC\n");
|
1223 | 406c8df3 | Glauber Costa | exit(1);
|
1224 | 406c8df3 | Glauber Costa | } |
1225 | 406c8df3 | Glauber Costa | } |
1226 | 406c8df3 | Glauber Costa | |
1227 | 376253ec | aliguori | void do_info_network(Monitor *mon)
|
1228 | 63a01ef8 | aliguori | { |
1229 | 63a01ef8 | aliguori | VLANState *vlan; |
1230 | 63a01ef8 | aliguori | |
1231 | 5610c3aa | Mark McLoughlin | QTAILQ_FOREACH(vlan, &vlans, next) { |
1232 | 5610c3aa | Mark McLoughlin | VLANClientState *vc; |
1233 | 5610c3aa | Mark McLoughlin | |
1234 | 376253ec | aliguori | monitor_printf(mon, "VLAN %d devices:\n", vlan->id);
|
1235 | 5610c3aa | Mark McLoughlin | |
1236 | 5610c3aa | Mark McLoughlin | QTAILQ_FOREACH(vc, &vlan->clients, next) { |
1237 | 376253ec | aliguori | monitor_printf(mon, " %s: %s\n", vc->name, vc->info_str);
|
1238 | 5610c3aa | Mark McLoughlin | } |
1239 | 63a01ef8 | aliguori | } |
1240 | 63a01ef8 | aliguori | } |
1241 | 63a01ef8 | aliguori | |
1242 | f18c16de | Luiz Capitulino | void do_set_link(Monitor *mon, const QDict *qdict) |
1243 | 436e5e53 | aliguori | { |
1244 | 436e5e53 | aliguori | VLANState *vlan; |
1245 | 436e5e53 | aliguori | VLANClientState *vc = NULL;
|
1246 | f18c16de | Luiz Capitulino | const char *name = qdict_get_str(qdict, "name"); |
1247 | f18c16de | Luiz Capitulino | const char *up_or_down = qdict_get_str(qdict, "up_or_down"); |
1248 | 436e5e53 | aliguori | |
1249 | 5610c3aa | Mark McLoughlin | QTAILQ_FOREACH(vlan, &vlans, next) { |
1250 | 5610c3aa | Mark McLoughlin | QTAILQ_FOREACH(vc, &vlan->clients, next) { |
1251 | 5610c3aa | Mark McLoughlin | if (strcmp(vc->name, name) == 0) { |
1252 | dd5de373 | edgar_igl | goto done;
|
1253 | 5610c3aa | Mark McLoughlin | } |
1254 | 5610c3aa | Mark McLoughlin | } |
1255 | 5610c3aa | Mark McLoughlin | } |
1256 | dd5de373 | edgar_igl | done:
|
1257 | 436e5e53 | aliguori | |
1258 | 436e5e53 | aliguori | if (!vc) {
|
1259 | 7dc3fa09 | Stefan Weil | monitor_printf(mon, "could not find network device '%s'\n", name);
|
1260 | c3cf0d3f | Luiz Capitulino | return;
|
1261 | 436e5e53 | aliguori | } |
1262 | 436e5e53 | aliguori | |
1263 | 436e5e53 | aliguori | if (strcmp(up_or_down, "up") == 0) |
1264 | 436e5e53 | aliguori | vc->link_down = 0;
|
1265 | 436e5e53 | aliguori | else if (strcmp(up_or_down, "down") == 0) |
1266 | 436e5e53 | aliguori | vc->link_down = 1;
|
1267 | 436e5e53 | aliguori | else
|
1268 | 376253ec | aliguori | monitor_printf(mon, "invalid link status '%s'; only 'up' or 'down' "
|
1269 | 376253ec | aliguori | "valid\n", up_or_down);
|
1270 | 436e5e53 | aliguori | |
1271 | 665a3b07 | Mark McLoughlin | if (vc->info->link_status_changed) {
|
1272 | 665a3b07 | Mark McLoughlin | vc->info->link_status_changed(vc); |
1273 | 665a3b07 | Mark McLoughlin | } |
1274 | 436e5e53 | aliguori | } |
1275 | 436e5e53 | aliguori | |
1276 | 63a01ef8 | aliguori | void net_cleanup(void) |
1277 | 63a01ef8 | aliguori | { |
1278 | 63a01ef8 | aliguori | VLANState *vlan; |
1279 | 577c4af9 | Mark McLoughlin | VLANClientState *vc, *next_vc; |
1280 | 63a01ef8 | aliguori | |
1281 | 5610c3aa | Mark McLoughlin | QTAILQ_FOREACH(vlan, &vlans, next) { |
1282 | 5610c3aa | Mark McLoughlin | QTAILQ_FOREACH_SAFE(vc, &vlan->clients, next, next_vc) { |
1283 | b946a153 | aliguori | qemu_del_vlan_client(vc); |
1284 | 63a01ef8 | aliguori | } |
1285 | 63a01ef8 | aliguori | } |
1286 | 577c4af9 | Mark McLoughlin | |
1287 | 577c4af9 | Mark McLoughlin | QTAILQ_FOREACH_SAFE(vc, &non_vlan_clients, next, next_vc) { |
1288 | 577c4af9 | Mark McLoughlin | qemu_del_vlan_client(vc); |
1289 | 577c4af9 | Mark McLoughlin | } |
1290 | 63a01ef8 | aliguori | } |
1291 | 63a01ef8 | aliguori | |
1292 | dc1c9fe8 | Mark McLoughlin | static void net_check_clients(void) |
1293 | 63a01ef8 | aliguori | { |
1294 | 63a01ef8 | aliguori | VLANState *vlan; |
1295 | 63a01ef8 | aliguori | |
1296 | 5610c3aa | Mark McLoughlin | QTAILQ_FOREACH(vlan, &vlans, next) { |
1297 | 63a01ef8 | aliguori | if (vlan->nb_guest_devs == 0 && vlan->nb_host_devs == 0) |
1298 | 63a01ef8 | aliguori | continue;
|
1299 | 63a01ef8 | aliguori | if (vlan->nb_guest_devs == 0) |
1300 | 63a01ef8 | aliguori | fprintf(stderr, "Warning: vlan %d with no nics\n", vlan->id);
|
1301 | 63a01ef8 | aliguori | if (vlan->nb_host_devs == 0) |
1302 | 63a01ef8 | aliguori | fprintf(stderr, |
1303 | 63a01ef8 | aliguori | "Warning: vlan %d is not connected to host network\n",
|
1304 | 63a01ef8 | aliguori | vlan->id); |
1305 | 63a01ef8 | aliguori | } |
1306 | 63a01ef8 | aliguori | } |
1307 | dc1c9fe8 | Mark McLoughlin | |
1308 | dc1c9fe8 | Mark McLoughlin | static int net_init_client(QemuOpts *opts, void *dummy) |
1309 | dc1c9fe8 | Mark McLoughlin | { |
1310 | c1671a08 | Mark McLoughlin | if (net_client_init(NULL, opts, 0) < 0) |
1311 | c1671a08 | Mark McLoughlin | return -1; |
1312 | c1671a08 | Mark McLoughlin | return 0; |
1313 | f6b134ac | Mark McLoughlin | } |
1314 | f6b134ac | Mark McLoughlin | |
1315 | f6b134ac | Mark McLoughlin | static int net_init_netdev(QemuOpts *opts, void *dummy) |
1316 | f6b134ac | Mark McLoughlin | { |
1317 | f6b134ac | Mark McLoughlin | return net_client_init(NULL, opts, 1); |
1318 | dc1c9fe8 | Mark McLoughlin | } |
1319 | dc1c9fe8 | Mark McLoughlin | |
1320 | dc1c9fe8 | Mark McLoughlin | int net_init_clients(void) |
1321 | dc1c9fe8 | Mark McLoughlin | { |
1322 | cb4522cc | Gerd Hoffmann | if (default_net) {
|
1323 | dc1c9fe8 | Mark McLoughlin | /* if no clients, we use a default config */
|
1324 | dc1c9fe8 | Mark McLoughlin | qemu_opts_set(&qemu_net_opts, NULL, "type", "nic"); |
1325 | dc1c9fe8 | Mark McLoughlin | #ifdef CONFIG_SLIRP
|
1326 | dc1c9fe8 | Mark McLoughlin | qemu_opts_set(&qemu_net_opts, NULL, "type", "user"); |
1327 | dc1c9fe8 | Mark McLoughlin | #endif
|
1328 | dc1c9fe8 | Mark McLoughlin | } |
1329 | dc1c9fe8 | Mark McLoughlin | |
1330 | 5610c3aa | Mark McLoughlin | QTAILQ_INIT(&vlans); |
1331 | 577c4af9 | Mark McLoughlin | QTAILQ_INIT(&non_vlan_clients); |
1332 | 5610c3aa | Mark McLoughlin | |
1333 | f6b134ac | Mark McLoughlin | if (qemu_opts_foreach(&qemu_netdev_opts, net_init_netdev, NULL, 1) == -1) |
1334 | f6b134ac | Mark McLoughlin | return -1; |
1335 | f6b134ac | Mark McLoughlin | |
1336 | dc1c9fe8 | Mark McLoughlin | if (qemu_opts_foreach(&qemu_net_opts, net_init_client, NULL, 1) == -1) { |
1337 | dc1c9fe8 | Mark McLoughlin | return -1; |
1338 | dc1c9fe8 | Mark McLoughlin | } |
1339 | dc1c9fe8 | Mark McLoughlin | |
1340 | dc1c9fe8 | Mark McLoughlin | net_check_clients(); |
1341 | dc1c9fe8 | Mark McLoughlin | |
1342 | dc1c9fe8 | Mark McLoughlin | return 0; |
1343 | dc1c9fe8 | Mark McLoughlin | } |
1344 | dc1c9fe8 | Mark McLoughlin | |
1345 | 7f161aae | Mark McLoughlin | int net_client_parse(QemuOptsList *opts_list, const char *optarg) |
1346 | dc1c9fe8 | Mark McLoughlin | { |
1347 | a3a766e7 | Juan Quintela | #if defined(CONFIG_SLIRP)
|
1348 | 68ac40d2 | Mark McLoughlin | int ret;
|
1349 | 68ac40d2 | Mark McLoughlin | if (net_slirp_parse_legacy(opts_list, optarg, &ret)) {
|
1350 | dc1c9fe8 | Mark McLoughlin | return ret;
|
1351 | dc1c9fe8 | Mark McLoughlin | } |
1352 | a3a766e7 | Juan Quintela | #endif
|
1353 | 68ac40d2 | Mark McLoughlin | |
1354 | 7f161aae | Mark McLoughlin | if (!qemu_opts_parse(opts_list, optarg, "type")) { |
1355 | dc1c9fe8 | Mark McLoughlin | return -1; |
1356 | dc1c9fe8 | Mark McLoughlin | } |
1357 | dc1c9fe8 | Mark McLoughlin | |
1358 | cb4522cc | Gerd Hoffmann | default_net = 0;
|
1359 | dc1c9fe8 | Mark McLoughlin | return 0; |
1360 | dc1c9fe8 | Mark McLoughlin | } |