Statistics
| Branch: | Revision:

root / net.c @ 463ce4ae

History | View | Annotate | Download (38.9 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 1df49e04 Mark McLoughlin
#include "qemu-common.h"
36 511d2b14 blueswir1
#include "qemu_socket.h"
37 75422b0d Amit Shah
#include "hw/qdev.h"
38 ce053661 Benjamin Poirier
#include "iov.h"
39 511d2b14 blueswir1
40 5610c3aa Mark McLoughlin
static QTAILQ_HEAD(, VLANState) vlans;
41 577c4af9 Mark McLoughlin
static QTAILQ_HEAD(, VLANClientState) non_vlan_clients;
42 63a01ef8 aliguori
43 cb4522cc Gerd Hoffmann
int default_net = 1;
44 cb4522cc Gerd Hoffmann
45 63a01ef8 aliguori
/***********************************************************/
46 63a01ef8 aliguori
/* network device redirectors */
47 63a01ef8 aliguori
48 68ac40d2 Mark McLoughlin
#if defined(DEBUG_NET)
49 63a01ef8 aliguori
static void hex_dump(FILE *f, const uint8_t *buf, int size)
50 63a01ef8 aliguori
{
51 63a01ef8 aliguori
    int len, i, j, c;
52 63a01ef8 aliguori
53 63a01ef8 aliguori
    for(i=0;i<size;i+=16) {
54 63a01ef8 aliguori
        len = size - i;
55 63a01ef8 aliguori
        if (len > 16)
56 63a01ef8 aliguori
            len = 16;
57 63a01ef8 aliguori
        fprintf(f, "%08x ", i);
58 63a01ef8 aliguori
        for(j=0;j<16;j++) {
59 63a01ef8 aliguori
            if (j < len)
60 63a01ef8 aliguori
                fprintf(f, " %02x", buf[i+j]);
61 63a01ef8 aliguori
            else
62 63a01ef8 aliguori
                fprintf(f, "   ");
63 63a01ef8 aliguori
        }
64 63a01ef8 aliguori
        fprintf(f, " ");
65 63a01ef8 aliguori
        for(j=0;j<len;j++) {
66 63a01ef8 aliguori
            c = buf[i+j];
67 63a01ef8 aliguori
            if (c < ' ' || c > '~')
68 63a01ef8 aliguori
                c = '.';
69 63a01ef8 aliguori
            fprintf(f, "%c", c);
70 63a01ef8 aliguori
        }
71 63a01ef8 aliguori
        fprintf(f, "\n");
72 63a01ef8 aliguori
    }
73 63a01ef8 aliguori
}
74 63a01ef8 aliguori
#endif
75 63a01ef8 aliguori
76 63a01ef8 aliguori
static int get_str_sep(char *buf, int buf_size, const char **pp, int sep)
77 63a01ef8 aliguori
{
78 63a01ef8 aliguori
    const char *p, *p1;
79 63a01ef8 aliguori
    int len;
80 63a01ef8 aliguori
    p = *pp;
81 63a01ef8 aliguori
    p1 = strchr(p, sep);
82 63a01ef8 aliguori
    if (!p1)
83 63a01ef8 aliguori
        return -1;
84 63a01ef8 aliguori
    len = p1 - p;
85 63a01ef8 aliguori
    p1++;
86 63a01ef8 aliguori
    if (buf_size > 0) {
87 63a01ef8 aliguori
        if (len > buf_size - 1)
88 63a01ef8 aliguori
            len = buf_size - 1;
89 63a01ef8 aliguori
        memcpy(buf, p, len);
90 63a01ef8 aliguori
        buf[len] = '\0';
91 63a01ef8 aliguori
    }
92 63a01ef8 aliguori
    *pp = p1;
93 63a01ef8 aliguori
    return 0;
94 63a01ef8 aliguori
}
95 63a01ef8 aliguori
96 63a01ef8 aliguori
int parse_host_port(struct sockaddr_in *saddr, const char *str)
97 63a01ef8 aliguori
{
98 63a01ef8 aliguori
    char buf[512];
99 63a01ef8 aliguori
    struct hostent *he;
100 63a01ef8 aliguori
    const char *p, *r;
101 63a01ef8 aliguori
    int port;
102 63a01ef8 aliguori
103 63a01ef8 aliguori
    p = str;
104 63a01ef8 aliguori
    if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
105 63a01ef8 aliguori
        return -1;
106 63a01ef8 aliguori
    saddr->sin_family = AF_INET;
107 63a01ef8 aliguori
    if (buf[0] == '\0') {
108 63a01ef8 aliguori
        saddr->sin_addr.s_addr = 0;
109 63a01ef8 aliguori
    } else {
110 cd390083 blueswir1
        if (qemu_isdigit(buf[0])) {
111 63a01ef8 aliguori
            if (!inet_aton(buf, &saddr->sin_addr))
112 63a01ef8 aliguori
                return -1;
113 63a01ef8 aliguori
        } else {
114 63a01ef8 aliguori
            if ((he = gethostbyname(buf)) == NULL)
115 63a01ef8 aliguori
                return - 1;
116 63a01ef8 aliguori
            saddr->sin_addr = *(struct in_addr *)he->h_addr;
117 63a01ef8 aliguori
        }
118 63a01ef8 aliguori
    }
119 63a01ef8 aliguori
    port = strtol(p, (char **)&r, 0);
120 63a01ef8 aliguori
    if (r == p)
121 63a01ef8 aliguori
        return -1;
122 63a01ef8 aliguori
    saddr->sin_port = htons(port);
123 63a01ef8 aliguori
    return 0;
124 63a01ef8 aliguori
}
125 63a01ef8 aliguori
126 7cb7434b aliguori
void qemu_format_nic_info_str(VLANClientState *vc, uint8_t macaddr[6])
127 7cb7434b aliguori
{
128 7cb7434b aliguori
    snprintf(vc->info_str, sizeof(vc->info_str),
129 4dda4063 aliguori
             "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
130 4dda4063 aliguori
             vc->model,
131 7cb7434b aliguori
             macaddr[0], macaddr[1], macaddr[2],
132 7cb7434b aliguori
             macaddr[3], macaddr[4], macaddr[5]);
133 7cb7434b aliguori
}
134 7cb7434b aliguori
135 76d32cba Gerd Hoffmann
void qemu_macaddr_default_if_unset(MACAddr *macaddr)
136 76d32cba Gerd Hoffmann
{
137 76d32cba Gerd Hoffmann
    static int index = 0;
138 76d32cba Gerd Hoffmann
    static const MACAddr zero = { .a = { 0,0,0,0,0,0 } };
139 76d32cba Gerd Hoffmann
140 76d32cba Gerd Hoffmann
    if (memcmp(macaddr, &zero, sizeof(zero)) != 0)
141 76d32cba Gerd Hoffmann
        return;
142 76d32cba Gerd Hoffmann
    macaddr->a[0] = 0x52;
143 76d32cba Gerd Hoffmann
    macaddr->a[1] = 0x54;
144 76d32cba Gerd Hoffmann
    macaddr->a[2] = 0x00;
145 76d32cba Gerd Hoffmann
    macaddr->a[3] = 0x12;
146 76d32cba Gerd Hoffmann
    macaddr->a[4] = 0x34;
147 76d32cba Gerd Hoffmann
    macaddr->a[5] = 0x56 + index++;
148 76d32cba Gerd Hoffmann
}
149 76d32cba Gerd Hoffmann
150 676cff29 aliguori
static char *assign_name(VLANClientState *vc1, const char *model)
151 676cff29 aliguori
{
152 676cff29 aliguori
    VLANState *vlan;
153 53e51d85 Markus Armbruster
    VLANClientState *vc;
154 676cff29 aliguori
    char buf[256];
155 676cff29 aliguori
    int id = 0;
156 676cff29 aliguori
157 5610c3aa Mark McLoughlin
    QTAILQ_FOREACH(vlan, &vlans, next) {
158 5610c3aa Mark McLoughlin
        QTAILQ_FOREACH(vc, &vlan->clients, next) {
159 5610c3aa Mark McLoughlin
            if (vc != vc1 && strcmp(vc->model, model) == 0) {
160 676cff29 aliguori
                id++;
161 5610c3aa Mark McLoughlin
            }
162 5610c3aa Mark McLoughlin
        }
163 676cff29 aliguori
    }
164 676cff29 aliguori
165 53e51d85 Markus Armbruster
    QTAILQ_FOREACH(vc, &non_vlan_clients, next) {
166 53e51d85 Markus Armbruster
        if (vc != vc1 && strcmp(vc->model, model) == 0) {
167 53e51d85 Markus Armbruster
            id++;
168 53e51d85 Markus Armbruster
        }
169 53e51d85 Markus Armbruster
    }
170 53e51d85 Markus Armbruster
171 676cff29 aliguori
    snprintf(buf, sizeof(buf), "%s.%d", model, id);
172 676cff29 aliguori
173 7267c094 Anthony Liguori
    return g_strdup(buf);
174 676cff29 aliguori
}
175 676cff29 aliguori
176 9a6ecb30 Mark McLoughlin
static ssize_t qemu_deliver_packet(VLANClientState *sender,
177 c0b8e49c Mark McLoughlin
                                   unsigned flags,
178 9a6ecb30 Mark McLoughlin
                                   const uint8_t *data,
179 9a6ecb30 Mark McLoughlin
                                   size_t size,
180 9a6ecb30 Mark McLoughlin
                                   void *opaque);
181 9a6ecb30 Mark McLoughlin
static ssize_t qemu_deliver_packet_iov(VLANClientState *sender,
182 c0b8e49c Mark McLoughlin
                                       unsigned flags,
183 9a6ecb30 Mark McLoughlin
                                       const struct iovec *iov,
184 9a6ecb30 Mark McLoughlin
                                       int iovcnt,
185 9a6ecb30 Mark McLoughlin
                                       void *opaque);
186 9a6ecb30 Mark McLoughlin
187 45460d1a Mark McLoughlin
VLANClientState *qemu_new_net_client(NetClientInfo *info,
188 45460d1a Mark McLoughlin
                                     VLANState *vlan,
189 45460d1a Mark McLoughlin
                                     VLANClientState *peer,
190 45460d1a Mark McLoughlin
                                     const char *model,
191 45460d1a Mark McLoughlin
                                     const char *name)
192 63a01ef8 aliguori
{
193 5610c3aa Mark McLoughlin
    VLANClientState *vc;
194 5610c3aa Mark McLoughlin
195 45460d1a Mark McLoughlin
    assert(info->size >= sizeof(VLANClientState));
196 45460d1a Mark McLoughlin
197 7267c094 Anthony Liguori
    vc = g_malloc0(info->size);
198 5610c3aa Mark McLoughlin
199 665a3b07 Mark McLoughlin
    vc->info = info;
200 7267c094 Anthony Liguori
    vc->model = g_strdup(model);
201 45460d1a Mark McLoughlin
    if (name) {
202 7267c094 Anthony Liguori
        vc->name = g_strdup(name);
203 45460d1a Mark McLoughlin
    } else {
204 7a9f6e4a aliguori
        vc->name = assign_name(vc, model);
205 45460d1a Mark McLoughlin
    }
206 5610c3aa Mark McLoughlin
207 d80b9fc6 Mark McLoughlin
    if (vlan) {
208 283c7c63 Mark McLoughlin
        assert(!peer);
209 d80b9fc6 Mark McLoughlin
        vc->vlan = vlan;
210 d80b9fc6 Mark McLoughlin
        QTAILQ_INSERT_TAIL(&vc->vlan->clients, vc, next);
211 577c4af9 Mark McLoughlin
    } else {
212 283c7c63 Mark McLoughlin
        if (peer) {
213 27f3f8a3 Markus Armbruster
            assert(!peer->peer);
214 283c7c63 Mark McLoughlin
            vc->peer = peer;
215 283c7c63 Mark McLoughlin
            peer->peer = vc;
216 283c7c63 Mark McLoughlin
        }
217 577c4af9 Mark McLoughlin
        QTAILQ_INSERT_TAIL(&non_vlan_clients, vc, next);
218 9a6ecb30 Mark McLoughlin
219 9a6ecb30 Mark McLoughlin
        vc->send_queue = qemu_new_net_queue(qemu_deliver_packet,
220 9a6ecb30 Mark McLoughlin
                                            qemu_deliver_packet_iov,
221 9a6ecb30 Mark McLoughlin
                                            vc);
222 d80b9fc6 Mark McLoughlin
    }
223 63a01ef8 aliguori
224 63a01ef8 aliguori
    return vc;
225 63a01ef8 aliguori
}
226 63a01ef8 aliguori
227 ebef2c09 Mark McLoughlin
NICState *qemu_new_nic(NetClientInfo *info,
228 ebef2c09 Mark McLoughlin
                       NICConf *conf,
229 ebef2c09 Mark McLoughlin
                       const char *model,
230 ebef2c09 Mark McLoughlin
                       const char *name,
231 ebef2c09 Mark McLoughlin
                       void *opaque)
232 ebef2c09 Mark McLoughlin
{
233 ebef2c09 Mark McLoughlin
    VLANClientState *nc;
234 ebef2c09 Mark McLoughlin
    NICState *nic;
235 ebef2c09 Mark McLoughlin
236 ebef2c09 Mark McLoughlin
    assert(info->type == NET_CLIENT_TYPE_NIC);
237 ebef2c09 Mark McLoughlin
    assert(info->size >= sizeof(NICState));
238 ebef2c09 Mark McLoughlin
239 ebef2c09 Mark McLoughlin
    nc = qemu_new_net_client(info, conf->vlan, conf->peer, model, name);
240 ebef2c09 Mark McLoughlin
241 ebef2c09 Mark McLoughlin
    nic = DO_UPCAST(NICState, nc, nc);
242 ebef2c09 Mark McLoughlin
    nic->conf = conf;
243 ebef2c09 Mark McLoughlin
    nic->opaque = opaque;
244 ebef2c09 Mark McLoughlin
245 ebef2c09 Mark McLoughlin
    return nic;
246 ebef2c09 Mark McLoughlin
}
247 ebef2c09 Mark McLoughlin
248 a083a89d Michael S. Tsirkin
static void qemu_cleanup_vlan_client(VLANClientState *vc)
249 63a01ef8 aliguori
{
250 d80b9fc6 Mark McLoughlin
    if (vc->vlan) {
251 d80b9fc6 Mark McLoughlin
        QTAILQ_REMOVE(&vc->vlan->clients, vc, next);
252 577c4af9 Mark McLoughlin
    } else {
253 577c4af9 Mark McLoughlin
        QTAILQ_REMOVE(&non_vlan_clients, vc, next);
254 d80b9fc6 Mark McLoughlin
    }
255 63a01ef8 aliguori
256 665a3b07 Mark McLoughlin
    if (vc->info->cleanup) {
257 665a3b07 Mark McLoughlin
        vc->info->cleanup(vc);
258 5610c3aa Mark McLoughlin
    }
259 a083a89d Michael S. Tsirkin
}
260 5610c3aa Mark McLoughlin
261 a083a89d Michael S. Tsirkin
static void qemu_free_vlan_client(VLANClientState *vc)
262 a083a89d Michael S. Tsirkin
{
263 a083a89d Michael S. Tsirkin
    if (!vc->vlan) {
264 a083a89d Michael S. Tsirkin
        if (vc->send_queue) {
265 a083a89d Michael S. Tsirkin
            qemu_del_net_queue(vc->send_queue);
266 a083a89d Michael S. Tsirkin
        }
267 a083a89d Michael S. Tsirkin
        if (vc->peer) {
268 a083a89d Michael S. Tsirkin
            vc->peer->peer = NULL;
269 a083a89d Michael S. Tsirkin
        }
270 a083a89d Michael S. Tsirkin
    }
271 7267c094 Anthony Liguori
    g_free(vc->name);
272 7267c094 Anthony Liguori
    g_free(vc->model);
273 7267c094 Anthony Liguori
    g_free(vc);
274 63a01ef8 aliguori
}
275 63a01ef8 aliguori
276 a083a89d Michael S. Tsirkin
void qemu_del_vlan_client(VLANClientState *vc)
277 a083a89d Michael S. Tsirkin
{
278 a083a89d Michael S. Tsirkin
    /* If there is a peer NIC, delete and cleanup client, but do not free. */
279 a083a89d Michael S. Tsirkin
    if (!vc->vlan && vc->peer && vc->peer->info->type == NET_CLIENT_TYPE_NIC) {
280 a083a89d Michael S. Tsirkin
        NICState *nic = DO_UPCAST(NICState, nc, vc->peer);
281 a083a89d Michael S. Tsirkin
        if (nic->peer_deleted) {
282 a083a89d Michael S. Tsirkin
            return;
283 a083a89d Michael S. Tsirkin
        }
284 a083a89d Michael S. Tsirkin
        nic->peer_deleted = true;
285 a083a89d Michael S. Tsirkin
        /* Let NIC know peer is gone. */
286 a083a89d Michael S. Tsirkin
        vc->peer->link_down = true;
287 a083a89d Michael S. Tsirkin
        if (vc->peer->info->link_status_changed) {
288 a083a89d Michael S. Tsirkin
            vc->peer->info->link_status_changed(vc->peer);
289 a083a89d Michael S. Tsirkin
        }
290 a083a89d Michael S. Tsirkin
        qemu_cleanup_vlan_client(vc);
291 a083a89d Michael S. Tsirkin
        return;
292 a083a89d Michael S. Tsirkin
    }
293 a083a89d Michael S. Tsirkin
294 a083a89d Michael S. Tsirkin
    /* If this is a peer NIC and peer has already been deleted, free it now. */
295 a083a89d Michael S. Tsirkin
    if (!vc->vlan && vc->peer && vc->info->type == NET_CLIENT_TYPE_NIC) {
296 a083a89d Michael S. Tsirkin
        NICState *nic = DO_UPCAST(NICState, nc, vc);
297 a083a89d Michael S. Tsirkin
        if (nic->peer_deleted) {
298 a083a89d Michael S. Tsirkin
            qemu_free_vlan_client(vc->peer);
299 a083a89d Michael S. Tsirkin
        }
300 a083a89d Michael S. Tsirkin
    }
301 a083a89d Michael S. Tsirkin
302 a083a89d Michael S. Tsirkin
    qemu_cleanup_vlan_client(vc);
303 a083a89d Michael S. Tsirkin
    qemu_free_vlan_client(vc);
304 a083a89d Michael S. Tsirkin
}
305 a083a89d Michael S. Tsirkin
306 68ac40d2 Mark McLoughlin
VLANClientState *
307 1a609520 Jan Kiszka
qemu_find_vlan_client_by_name(Monitor *mon, int vlan_id,
308 1a609520 Jan Kiszka
                              const char *client_str)
309 1a609520 Jan Kiszka
{
310 1a609520 Jan Kiszka
    VLANState *vlan;
311 1a609520 Jan Kiszka
    VLANClientState *vc;
312 1a609520 Jan Kiszka
313 1a609520 Jan Kiszka
    vlan = qemu_find_vlan(vlan_id, 0);
314 1a609520 Jan Kiszka
    if (!vlan) {
315 1a609520 Jan Kiszka
        monitor_printf(mon, "unknown VLAN %d\n", vlan_id);
316 1a609520 Jan Kiszka
        return NULL;
317 1a609520 Jan Kiszka
    }
318 1a609520 Jan Kiszka
319 5610c3aa Mark McLoughlin
    QTAILQ_FOREACH(vc, &vlan->clients, next) {
320 1a609520 Jan Kiszka
        if (!strcmp(vc->name, client_str)) {
321 1a609520 Jan Kiszka
            break;
322 1a609520 Jan Kiszka
        }
323 1a609520 Jan Kiszka
    }
324 1a609520 Jan Kiszka
    if (!vc) {
325 1a609520 Jan Kiszka
        monitor_printf(mon, "can't find device %s on VLAN %d\n",
326 1a609520 Jan Kiszka
                       client_str, vlan_id);
327 1a609520 Jan Kiszka
    }
328 1a609520 Jan Kiszka
329 1a609520 Jan Kiszka
    return vc;
330 1a609520 Jan Kiszka
}
331 1a609520 Jan Kiszka
332 57f9ef17 Mark McLoughlin
void qemu_foreach_nic(qemu_nic_foreach func, void *opaque)
333 57f9ef17 Mark McLoughlin
{
334 57f9ef17 Mark McLoughlin
    VLANClientState *nc;
335 57f9ef17 Mark McLoughlin
    VLANState *vlan;
336 57f9ef17 Mark McLoughlin
337 57f9ef17 Mark McLoughlin
    QTAILQ_FOREACH(nc, &non_vlan_clients, next) {
338 57f9ef17 Mark McLoughlin
        if (nc->info->type == NET_CLIENT_TYPE_NIC) {
339 57f9ef17 Mark McLoughlin
            func(DO_UPCAST(NICState, nc, nc), opaque);
340 57f9ef17 Mark McLoughlin
        }
341 57f9ef17 Mark McLoughlin
    }
342 57f9ef17 Mark McLoughlin
343 57f9ef17 Mark McLoughlin
    QTAILQ_FOREACH(vlan, &vlans, next) {
344 57f9ef17 Mark McLoughlin
        QTAILQ_FOREACH(nc, &vlan->clients, next) {
345 57f9ef17 Mark McLoughlin
            if (nc->info->type == NET_CLIENT_TYPE_NIC) {
346 57f9ef17 Mark McLoughlin
                func(DO_UPCAST(NICState, nc, nc), opaque);
347 57f9ef17 Mark McLoughlin
            }
348 57f9ef17 Mark McLoughlin
        }
349 57f9ef17 Mark McLoughlin
    }
350 57f9ef17 Mark McLoughlin
}
351 57f9ef17 Mark McLoughlin
352 2e1e0641 Mark McLoughlin
int qemu_can_send_packet(VLANClientState *sender)
353 63a01ef8 aliguori
{
354 2e1e0641 Mark McLoughlin
    VLANState *vlan = sender->vlan;
355 63a01ef8 aliguori
    VLANClientState *vc;
356 63a01ef8 aliguori
357 9a6ecb30 Mark McLoughlin
    if (sender->peer) {
358 893379ef Mark McLoughlin
        if (sender->peer->receive_disabled) {
359 893379ef Mark McLoughlin
            return 0;
360 665a3b07 Mark McLoughlin
        } else if (sender->peer->info->can_receive &&
361 665a3b07 Mark McLoughlin
                   !sender->peer->info->can_receive(sender->peer)) {
362 9a6ecb30 Mark McLoughlin
            return 0;
363 893379ef Mark McLoughlin
        } else {
364 893379ef Mark McLoughlin
            return 1;
365 9a6ecb30 Mark McLoughlin
        }
366 9a6ecb30 Mark McLoughlin
    }
367 9a6ecb30 Mark McLoughlin
368 d80b9fc6 Mark McLoughlin
    if (!sender->vlan) {
369 d80b9fc6 Mark McLoughlin
        return 1;
370 d80b9fc6 Mark McLoughlin
    }
371 d80b9fc6 Mark McLoughlin
372 5610c3aa Mark McLoughlin
    QTAILQ_FOREACH(vc, &vlan->clients, next) {
373 2e1e0641 Mark McLoughlin
        if (vc == sender) {
374 2e1e0641 Mark McLoughlin
            continue;
375 2e1e0641 Mark McLoughlin
        }
376 2e1e0641 Mark McLoughlin
377 cda9046b Mark McLoughlin
        /* no can_receive() handler, they can always receive */
378 60c07d93 Vincent Palatin
        if (vc->info->can_receive && !vc->info->can_receive(vc)) {
379 60c07d93 Vincent Palatin
            return 0;
380 63a01ef8 aliguori
        }
381 63a01ef8 aliguori
    }
382 60c07d93 Vincent Palatin
    return 1;
383 63a01ef8 aliguori
}
384 63a01ef8 aliguori
385 9a6ecb30 Mark McLoughlin
static ssize_t qemu_deliver_packet(VLANClientState *sender,
386 c0b8e49c Mark McLoughlin
                                   unsigned flags,
387 9a6ecb30 Mark McLoughlin
                                   const uint8_t *data,
388 9a6ecb30 Mark McLoughlin
                                   size_t size,
389 9a6ecb30 Mark McLoughlin
                                   void *opaque)
390 9a6ecb30 Mark McLoughlin
{
391 9a6ecb30 Mark McLoughlin
    VLANClientState *vc = opaque;
392 893379ef Mark McLoughlin
    ssize_t ret;
393 9a6ecb30 Mark McLoughlin
394 9a6ecb30 Mark McLoughlin
    if (vc->link_down) {
395 9a6ecb30 Mark McLoughlin
        return size;
396 9a6ecb30 Mark McLoughlin
    }
397 9a6ecb30 Mark McLoughlin
398 893379ef Mark McLoughlin
    if (vc->receive_disabled) {
399 893379ef Mark McLoughlin
        return 0;
400 893379ef Mark McLoughlin
    }
401 893379ef Mark McLoughlin
402 665a3b07 Mark McLoughlin
    if (flags & QEMU_NET_PACKET_FLAG_RAW && vc->info->receive_raw) {
403 665a3b07 Mark McLoughlin
        ret = vc->info->receive_raw(vc, data, size);
404 893379ef Mark McLoughlin
    } else {
405 665a3b07 Mark McLoughlin
        ret = vc->info->receive(vc, data, size);
406 893379ef Mark McLoughlin
    }
407 893379ef Mark McLoughlin
408 893379ef Mark McLoughlin
    if (ret == 0) {
409 893379ef Mark McLoughlin
        vc->receive_disabled = 1;
410 893379ef Mark McLoughlin
    };
411 893379ef Mark McLoughlin
412 893379ef Mark McLoughlin
    return ret;
413 9a6ecb30 Mark McLoughlin
}
414 9a6ecb30 Mark McLoughlin
415 f7105843 Mark McLoughlin
static ssize_t qemu_vlan_deliver_packet(VLANClientState *sender,
416 c0b8e49c Mark McLoughlin
                                        unsigned flags,
417 f7105843 Mark McLoughlin
                                        const uint8_t *buf,
418 f7105843 Mark McLoughlin
                                        size_t size,
419 f7105843 Mark McLoughlin
                                        void *opaque)
420 63a01ef8 aliguori
{
421 f7105843 Mark McLoughlin
    VLANState *vlan = opaque;
422 63a01ef8 aliguori
    VLANClientState *vc;
423 893379ef Mark McLoughlin
    ssize_t ret = -1;
424 63a01ef8 aliguori
425 f7105843 Mark McLoughlin
    QTAILQ_FOREACH(vc, &vlan->clients, next) {
426 3e021d40 Mark McLoughlin
        ssize_t len;
427 3e021d40 Mark McLoughlin
428 3e021d40 Mark McLoughlin
        if (vc == sender) {
429 3e021d40 Mark McLoughlin
            continue;
430 764a4d1d aliguori
        }
431 3e021d40 Mark McLoughlin
432 3e021d40 Mark McLoughlin
        if (vc->link_down) {
433 3e021d40 Mark McLoughlin
            ret = size;
434 3e021d40 Mark McLoughlin
            continue;
435 3e021d40 Mark McLoughlin
        }
436 3e021d40 Mark McLoughlin
437 893379ef Mark McLoughlin
        if (vc->receive_disabled) {
438 893379ef Mark McLoughlin
            ret = 0;
439 893379ef Mark McLoughlin
            continue;
440 893379ef Mark McLoughlin
        }
441 893379ef Mark McLoughlin
442 665a3b07 Mark McLoughlin
        if (flags & QEMU_NET_PACKET_FLAG_RAW && vc->info->receive_raw) {
443 665a3b07 Mark McLoughlin
            len = vc->info->receive_raw(vc, buf, size);
444 893379ef Mark McLoughlin
        } else {
445 665a3b07 Mark McLoughlin
            len = vc->info->receive(vc, buf, size);
446 893379ef Mark McLoughlin
        }
447 893379ef Mark McLoughlin
448 893379ef Mark McLoughlin
        if (len == 0) {
449 893379ef Mark McLoughlin
            vc->receive_disabled = 1;
450 893379ef Mark McLoughlin
        }
451 3e021d40 Mark McLoughlin
452 3e021d40 Mark McLoughlin
        ret = (ret >= 0) ? ret : len;
453 893379ef Mark McLoughlin
454 764a4d1d aliguori
    }
455 3e021d40 Mark McLoughlin
456 3e021d40 Mark McLoughlin
    return ret;
457 764a4d1d aliguori
}
458 764a4d1d aliguori
459 8cad5516 Mark McLoughlin
void qemu_purge_queued_packets(VLANClientState *vc)
460 8cad5516 Mark McLoughlin
{
461 9a6ecb30 Mark McLoughlin
    NetQueue *queue;
462 9a6ecb30 Mark McLoughlin
463 9a6ecb30 Mark McLoughlin
    if (!vc->peer && !vc->vlan) {
464 d80b9fc6 Mark McLoughlin
        return;
465 9a6ecb30 Mark McLoughlin
    }
466 d80b9fc6 Mark McLoughlin
467 9a6ecb30 Mark McLoughlin
    if (vc->peer) {
468 9a6ecb30 Mark McLoughlin
        queue = vc->peer->send_queue;
469 9a6ecb30 Mark McLoughlin
    } else {
470 9a6ecb30 Mark McLoughlin
        queue = vc->vlan->send_queue;
471 9a6ecb30 Mark McLoughlin
    }
472 9a6ecb30 Mark McLoughlin
473 9a6ecb30 Mark McLoughlin
    qemu_net_queue_purge(queue, vc);
474 8cad5516 Mark McLoughlin
}
475 8cad5516 Mark McLoughlin
476 f3b6c7fc Mark McLoughlin
void qemu_flush_queued_packets(VLANClientState *vc)
477 e94667b9 Mark McLoughlin
{
478 9a6ecb30 Mark McLoughlin
    NetQueue *queue;
479 9a6ecb30 Mark McLoughlin
480 893379ef Mark McLoughlin
    vc->receive_disabled = 0;
481 893379ef Mark McLoughlin
482 9a6ecb30 Mark McLoughlin
    if (vc->vlan) {
483 9a6ecb30 Mark McLoughlin
        queue = vc->vlan->send_queue;
484 9a6ecb30 Mark McLoughlin
    } else {
485 9a6ecb30 Mark McLoughlin
        queue = vc->send_queue;
486 9a6ecb30 Mark McLoughlin
    }
487 d80b9fc6 Mark McLoughlin
488 9a6ecb30 Mark McLoughlin
    qemu_net_queue_flush(queue);
489 e94667b9 Mark McLoughlin
}
490 e94667b9 Mark McLoughlin
491 ca77d175 Mark McLoughlin
static ssize_t qemu_send_packet_async_with_flags(VLANClientState *sender,
492 ca77d175 Mark McLoughlin
                                                 unsigned flags,
493 ca77d175 Mark McLoughlin
                                                 const uint8_t *buf, int size,
494 ca77d175 Mark McLoughlin
                                                 NetPacketSent *sent_cb)
495 764a4d1d aliguori
{
496 9a6ecb30 Mark McLoughlin
    NetQueue *queue;
497 436e5e53 aliguori
498 63a01ef8 aliguori
#ifdef DEBUG_NET
499 d80b9fc6 Mark McLoughlin
    printf("qemu_send_packet_async:\n");
500 63a01ef8 aliguori
    hex_dump(stdout, buf, size);
501 63a01ef8 aliguori
#endif
502 f3b6c7fc Mark McLoughlin
503 9a6ecb30 Mark McLoughlin
    if (sender->link_down || (!sender->peer && !sender->vlan)) {
504 9a6ecb30 Mark McLoughlin
        return size;
505 9a6ecb30 Mark McLoughlin
    }
506 9a6ecb30 Mark McLoughlin
507 9a6ecb30 Mark McLoughlin
    if (sender->peer) {
508 9a6ecb30 Mark McLoughlin
        queue = sender->peer->send_queue;
509 9a6ecb30 Mark McLoughlin
    } else {
510 9a6ecb30 Mark McLoughlin
        queue = sender->vlan->send_queue;
511 9a6ecb30 Mark McLoughlin
    }
512 9a6ecb30 Mark McLoughlin
513 ca77d175 Mark McLoughlin
    return qemu_net_queue_send(queue, sender, flags, buf, size, sent_cb);
514 ca77d175 Mark McLoughlin
}
515 ca77d175 Mark McLoughlin
516 ca77d175 Mark McLoughlin
ssize_t qemu_send_packet_async(VLANClientState *sender,
517 ca77d175 Mark McLoughlin
                               const uint8_t *buf, int size,
518 ca77d175 Mark McLoughlin
                               NetPacketSent *sent_cb)
519 ca77d175 Mark McLoughlin
{
520 ca77d175 Mark McLoughlin
    return qemu_send_packet_async_with_flags(sender, QEMU_NET_PACKET_FLAG_NONE,
521 ca77d175 Mark McLoughlin
                                             buf, size, sent_cb);
522 f3b6c7fc Mark McLoughlin
}
523 f3b6c7fc Mark McLoughlin
524 f3b6c7fc Mark McLoughlin
void qemu_send_packet(VLANClientState *vc, const uint8_t *buf, int size)
525 f3b6c7fc Mark McLoughlin
{
526 f3b6c7fc Mark McLoughlin
    qemu_send_packet_async(vc, buf, size, NULL);
527 63a01ef8 aliguori
}
528 63a01ef8 aliguori
529 ca77d175 Mark McLoughlin
ssize_t qemu_send_packet_raw(VLANClientState *vc, const uint8_t *buf, int size)
530 ca77d175 Mark McLoughlin
{
531 ca77d175 Mark McLoughlin
    return qemu_send_packet_async_with_flags(vc, QEMU_NET_PACKET_FLAG_RAW,
532 ca77d175 Mark McLoughlin
                                             buf, size, NULL);
533 ca77d175 Mark McLoughlin
}
534 ca77d175 Mark McLoughlin
535 fbe78f4f aliguori
static ssize_t vc_sendv_compat(VLANClientState *vc, const struct iovec *iov,
536 fbe78f4f aliguori
                               int iovcnt)
537 fbe78f4f aliguori
{
538 fbe78f4f aliguori
    uint8_t buffer[4096];
539 ce053661 Benjamin Poirier
    size_t offset;
540 fbe78f4f aliguori
541 ce053661 Benjamin Poirier
    offset = iov_to_buf(iov, iovcnt, buffer, 0, sizeof(buffer));
542 fbe78f4f aliguori
543 665a3b07 Mark McLoughlin
    return vc->info->receive(vc, buffer, offset);
544 fbe78f4f aliguori
}
545 fbe78f4f aliguori
546 9a6ecb30 Mark McLoughlin
static ssize_t qemu_deliver_packet_iov(VLANClientState *sender,
547 c0b8e49c Mark McLoughlin
                                       unsigned flags,
548 9a6ecb30 Mark McLoughlin
                                       const struct iovec *iov,
549 9a6ecb30 Mark McLoughlin
                                       int iovcnt,
550 9a6ecb30 Mark McLoughlin
                                       void *opaque)
551 9a6ecb30 Mark McLoughlin
{
552 9a6ecb30 Mark McLoughlin
    VLANClientState *vc = opaque;
553 9a6ecb30 Mark McLoughlin
554 9a6ecb30 Mark McLoughlin
    if (vc->link_down) {
555 ce053661 Benjamin Poirier
        return iov_size(iov, iovcnt);
556 9a6ecb30 Mark McLoughlin
    }
557 9a6ecb30 Mark McLoughlin
558 665a3b07 Mark McLoughlin
    if (vc->info->receive_iov) {
559 665a3b07 Mark McLoughlin
        return vc->info->receive_iov(vc, iov, iovcnt);
560 9a6ecb30 Mark McLoughlin
    } else {
561 9a6ecb30 Mark McLoughlin
        return vc_sendv_compat(vc, iov, iovcnt);
562 9a6ecb30 Mark McLoughlin
    }
563 9a6ecb30 Mark McLoughlin
}
564 9a6ecb30 Mark McLoughlin
565 f7105843 Mark McLoughlin
static ssize_t qemu_vlan_deliver_packet_iov(VLANClientState *sender,
566 c0b8e49c Mark McLoughlin
                                            unsigned flags,
567 f7105843 Mark McLoughlin
                                            const struct iovec *iov,
568 f7105843 Mark McLoughlin
                                            int iovcnt,
569 f7105843 Mark McLoughlin
                                            void *opaque)
570 fbe78f4f aliguori
{
571 f7105843 Mark McLoughlin
    VLANState *vlan = opaque;
572 fbe78f4f aliguori
    VLANClientState *vc;
573 f7105843 Mark McLoughlin
    ssize_t ret = -1;
574 e94667b9 Mark McLoughlin
575 f7105843 Mark McLoughlin
    QTAILQ_FOREACH(vc, &vlan->clients, next) {
576 e94667b9 Mark McLoughlin
        ssize_t len;
577 e94667b9 Mark McLoughlin
578 e94667b9 Mark McLoughlin
        if (vc == sender) {
579 e94667b9 Mark McLoughlin
            continue;
580 e94667b9 Mark McLoughlin
        }
581 e94667b9 Mark McLoughlin
582 e94667b9 Mark McLoughlin
        if (vc->link_down) {
583 ce053661 Benjamin Poirier
            ret = iov_size(iov, iovcnt);
584 e94667b9 Mark McLoughlin
            continue;
585 e94667b9 Mark McLoughlin
        }
586 e94667b9 Mark McLoughlin
587 ca77d175 Mark McLoughlin
        assert(!(flags & QEMU_NET_PACKET_FLAG_RAW));
588 ca77d175 Mark McLoughlin
589 665a3b07 Mark McLoughlin
        if (vc->info->receive_iov) {
590 665a3b07 Mark McLoughlin
            len = vc->info->receive_iov(vc, iov, iovcnt);
591 e94667b9 Mark McLoughlin
        } else {
592 e94667b9 Mark McLoughlin
            len = vc_sendv_compat(vc, iov, iovcnt);
593 e94667b9 Mark McLoughlin
        }
594 e94667b9 Mark McLoughlin
595 e94667b9 Mark McLoughlin
        ret = (ret >= 0) ? ret : len;
596 e94667b9 Mark McLoughlin
    }
597 e94667b9 Mark McLoughlin
598 e94667b9 Mark McLoughlin
    return ret;
599 e94667b9 Mark McLoughlin
}
600 e94667b9 Mark McLoughlin
601 f3b6c7fc Mark McLoughlin
ssize_t qemu_sendv_packet_async(VLANClientState *sender,
602 f3b6c7fc Mark McLoughlin
                                const struct iovec *iov, int iovcnt,
603 f3b6c7fc Mark McLoughlin
                                NetPacketSent *sent_cb)
604 e94667b9 Mark McLoughlin
{
605 9a6ecb30 Mark McLoughlin
    NetQueue *queue;
606 9a6ecb30 Mark McLoughlin
607 9a6ecb30 Mark McLoughlin
    if (sender->link_down || (!sender->peer && !sender->vlan)) {
608 ce053661 Benjamin Poirier
        return iov_size(iov, iovcnt);
609 e94667b9 Mark McLoughlin
    }
610 e94667b9 Mark McLoughlin
611 9a6ecb30 Mark McLoughlin
    if (sender->peer) {
612 9a6ecb30 Mark McLoughlin
        queue = sender->peer->send_queue;
613 9a6ecb30 Mark McLoughlin
    } else {
614 9a6ecb30 Mark McLoughlin
        queue = sender->vlan->send_queue;
615 9a6ecb30 Mark McLoughlin
    }
616 9a6ecb30 Mark McLoughlin
617 c0b8e49c Mark McLoughlin
    return qemu_net_queue_send_iov(queue, sender,
618 c0b8e49c Mark McLoughlin
                                   QEMU_NET_PACKET_FLAG_NONE,
619 c0b8e49c Mark McLoughlin
                                   iov, iovcnt, sent_cb);
620 fbe78f4f aliguori
}
621 fbe78f4f aliguori
622 f3b6c7fc Mark McLoughlin
ssize_t
623 f3b6c7fc Mark McLoughlin
qemu_sendv_packet(VLANClientState *vc, const struct iovec *iov, int iovcnt)
624 f3b6c7fc Mark McLoughlin
{
625 f3b6c7fc Mark McLoughlin
    return qemu_sendv_packet_async(vc, iov, iovcnt, NULL);
626 f3b6c7fc Mark McLoughlin
}
627 f3b6c7fc Mark McLoughlin
628 63a01ef8 aliguori
/* find or alloc a new VLAN */
629 1a609520 Jan Kiszka
VLANState *qemu_find_vlan(int id, int allocate)
630 63a01ef8 aliguori
{
631 5610c3aa Mark McLoughlin
    VLANState *vlan;
632 5610c3aa Mark McLoughlin
633 5610c3aa Mark McLoughlin
    QTAILQ_FOREACH(vlan, &vlans, next) {
634 5610c3aa Mark McLoughlin
        if (vlan->id == id) {
635 63a01ef8 aliguori
            return vlan;
636 5610c3aa Mark McLoughlin
        }
637 63a01ef8 aliguori
    }
638 5610c3aa Mark McLoughlin
639 1a609520 Jan Kiszka
    if (!allocate) {
640 1a609520 Jan Kiszka
        return NULL;
641 1a609520 Jan Kiszka
    }
642 5610c3aa Mark McLoughlin
643 7267c094 Anthony Liguori
    vlan = g_malloc0(sizeof(VLANState));
644 63a01ef8 aliguori
    vlan->id = id;
645 5610c3aa Mark McLoughlin
    QTAILQ_INIT(&vlan->clients);
646 f7105843 Mark McLoughlin
647 f7105843 Mark McLoughlin
    vlan->send_queue = qemu_new_net_queue(qemu_vlan_deliver_packet,
648 f7105843 Mark McLoughlin
                                          qemu_vlan_deliver_packet_iov,
649 f7105843 Mark McLoughlin
                                          vlan);
650 5610c3aa Mark McLoughlin
651 5610c3aa Mark McLoughlin
    QTAILQ_INSERT_TAIL(&vlans, vlan, next);
652 5610c3aa Mark McLoughlin
653 63a01ef8 aliguori
    return vlan;
654 63a01ef8 aliguori
}
655 63a01ef8 aliguori
656 2ef924b4 Gerd Hoffmann
VLANClientState *qemu_find_netdev(const char *id)
657 5869c4d5 Mark McLoughlin
{
658 5869c4d5 Mark McLoughlin
    VLANClientState *vc;
659 5869c4d5 Mark McLoughlin
660 5869c4d5 Mark McLoughlin
    QTAILQ_FOREACH(vc, &non_vlan_clients, next) {
661 85dde9a9 Markus Armbruster
        if (vc->info->type == NET_CLIENT_TYPE_NIC)
662 85dde9a9 Markus Armbruster
            continue;
663 5869c4d5 Mark McLoughlin
        if (!strcmp(vc->name, id)) {
664 5869c4d5 Mark McLoughlin
            return vc;
665 5869c4d5 Mark McLoughlin
        }
666 5869c4d5 Mark McLoughlin
    }
667 5869c4d5 Mark McLoughlin
668 5869c4d5 Mark McLoughlin
    return NULL;
669 5869c4d5 Mark McLoughlin
}
670 5869c4d5 Mark McLoughlin
671 7697079b aliguori
static int nic_get_free_idx(void)
672 7697079b aliguori
{
673 7697079b aliguori
    int index;
674 7697079b aliguori
675 7697079b aliguori
    for (index = 0; index < MAX_NICS; index++)
676 7697079b aliguori
        if (!nd_table[index].used)
677 7697079b aliguori
            return index;
678 7697079b aliguori
    return -1;
679 7697079b aliguori
}
680 7697079b aliguori
681 07caea31 Markus Armbruster
int qemu_show_nic_models(const char *arg, const char *const *models)
682 07caea31 Markus Armbruster
{
683 07caea31 Markus Armbruster
    int i;
684 07caea31 Markus Armbruster
685 07caea31 Markus Armbruster
    if (!arg || strcmp(arg, "?"))
686 07caea31 Markus Armbruster
        return 0;
687 07caea31 Markus Armbruster
688 07caea31 Markus Armbruster
    fprintf(stderr, "qemu: Supported NIC models: ");
689 07caea31 Markus Armbruster
    for (i = 0 ; models[i]; i++)
690 07caea31 Markus Armbruster
        fprintf(stderr, "%s%c", models[i], models[i+1] ? ',' : '\n');
691 07caea31 Markus Armbruster
    return 1;
692 07caea31 Markus Armbruster
}
693 07caea31 Markus Armbruster
694 d07f22c5 aliguori
void qemu_check_nic_model(NICInfo *nd, const char *model)
695 d07f22c5 aliguori
{
696 d07f22c5 aliguori
    const char *models[2];
697 d07f22c5 aliguori
698 d07f22c5 aliguori
    models[0] = model;
699 d07f22c5 aliguori
    models[1] = NULL;
700 d07f22c5 aliguori
701 07caea31 Markus Armbruster
    if (qemu_show_nic_models(nd->model, models))
702 07caea31 Markus Armbruster
        exit(0);
703 07caea31 Markus Armbruster
    if (qemu_find_nic_model(nd, models, model) < 0)
704 07caea31 Markus Armbruster
        exit(1);
705 d07f22c5 aliguori
}
706 d07f22c5 aliguori
707 07caea31 Markus Armbruster
int qemu_find_nic_model(NICInfo *nd, const char * const *models,
708 07caea31 Markus Armbruster
                        const char *default_model)
709 d07f22c5 aliguori
{
710 07caea31 Markus Armbruster
    int i;
711 d07f22c5 aliguori
712 d07f22c5 aliguori
    if (!nd->model)
713 7267c094 Anthony Liguori
        nd->model = g_strdup(default_model);
714 d07f22c5 aliguori
715 07caea31 Markus Armbruster
    for (i = 0 ; models[i]; i++) {
716 07caea31 Markus Armbruster
        if (strcmp(nd->model, models[i]) == 0)
717 07caea31 Markus Armbruster
            return i;
718 d07f22c5 aliguori
    }
719 d07f22c5 aliguori
720 6daf194d Markus Armbruster
    error_report("Unsupported NIC model: %s", nd->model);
721 07caea31 Markus Armbruster
    return -1;
722 d07f22c5 aliguori
}
723 d07f22c5 aliguori
724 5281d757 Mark McLoughlin
int net_handle_fd_param(Monitor *mon, const char *param)
725 c1d6eed7 Mark McLoughlin
{
726 f7c31d63 Jason Wang
    int fd;
727 f7c31d63 Jason Wang
728 f7c31d63 Jason Wang
    if (!qemu_isdigit(param[0]) && mon) {
729 c1d6eed7 Mark McLoughlin
730 c1d6eed7 Mark McLoughlin
        fd = monitor_get_fd(mon, param);
731 c1d6eed7 Mark McLoughlin
        if (fd == -1) {
732 1ecda02b Markus Armbruster
            error_report("No file descriptor named %s found", param);
733 c1d6eed7 Mark McLoughlin
            return -1;
734 c1d6eed7 Mark McLoughlin
        }
735 c1d6eed7 Mark McLoughlin
    } else {
736 f7c31d63 Jason Wang
        char *endptr = NULL;
737 f7c31d63 Jason Wang
738 f7c31d63 Jason Wang
        fd = strtol(param, &endptr, 10);
739 f7c31d63 Jason Wang
        if (*endptr || (fd == 0 && param == endptr)) {
740 f7c31d63 Jason Wang
            return -1;
741 f7c31d63 Jason Wang
        }
742 c1d6eed7 Mark McLoughlin
    }
743 f7c31d63 Jason Wang
744 f7c31d63 Jason Wang
    return fd;
745 c1d6eed7 Mark McLoughlin
}
746 c1d6eed7 Mark McLoughlin
747 f6b134ac Mark McLoughlin
static int net_init_nic(QemuOpts *opts,
748 f6b134ac Mark McLoughlin
                        Monitor *mon,
749 f6b134ac Mark McLoughlin
                        const char *name,
750 f6b134ac Mark McLoughlin
                        VLANState *vlan)
751 f83c6e10 Mark McLoughlin
{
752 f83c6e10 Mark McLoughlin
    int idx;
753 f83c6e10 Mark McLoughlin
    NICInfo *nd;
754 5869c4d5 Mark McLoughlin
    const char *netdev;
755 f83c6e10 Mark McLoughlin
756 f83c6e10 Mark McLoughlin
    idx = nic_get_free_idx();
757 f83c6e10 Mark McLoughlin
    if (idx == -1 || nb_nics >= MAX_NICS) {
758 1ecda02b Markus Armbruster
        error_report("Too Many NICs");
759 f83c6e10 Mark McLoughlin
        return -1;
760 f83c6e10 Mark McLoughlin
    }
761 f83c6e10 Mark McLoughlin
762 f83c6e10 Mark McLoughlin
    nd = &nd_table[idx];
763 f83c6e10 Mark McLoughlin
764 f83c6e10 Mark McLoughlin
    memset(nd, 0, sizeof(*nd));
765 f83c6e10 Mark McLoughlin
766 5869c4d5 Mark McLoughlin
    if ((netdev = qemu_opt_get(opts, "netdev"))) {
767 5869c4d5 Mark McLoughlin
        nd->netdev = qemu_find_netdev(netdev);
768 5869c4d5 Mark McLoughlin
        if (!nd->netdev) {
769 1ecda02b Markus Armbruster
            error_report("netdev '%s' not found", netdev);
770 5869c4d5 Mark McLoughlin
            return -1;
771 5869c4d5 Mark McLoughlin
        }
772 5869c4d5 Mark McLoughlin
    } else {
773 5869c4d5 Mark McLoughlin
        assert(vlan);
774 5869c4d5 Mark McLoughlin
        nd->vlan = vlan;
775 5869c4d5 Mark McLoughlin
    }
776 6d952ebe Mark McLoughlin
    if (name) {
777 7267c094 Anthony Liguori
        nd->name = g_strdup(name);
778 6d952ebe Mark McLoughlin
    }
779 f83c6e10 Mark McLoughlin
    if (qemu_opt_get(opts, "model")) {
780 7267c094 Anthony Liguori
        nd->model = g_strdup(qemu_opt_get(opts, "model"));
781 f83c6e10 Mark McLoughlin
    }
782 f83c6e10 Mark McLoughlin
    if (qemu_opt_get(opts, "addr")) {
783 7267c094 Anthony Liguori
        nd->devaddr = g_strdup(qemu_opt_get(opts, "addr"));
784 f83c6e10 Mark McLoughlin
    }
785 f83c6e10 Mark McLoughlin
786 f83c6e10 Mark McLoughlin
    if (qemu_opt_get(opts, "macaddr") &&
787 6eed1856 Jan Kiszka
        net_parse_macaddr(nd->macaddr.a, qemu_opt_get(opts, "macaddr")) < 0) {
788 1ecda02b Markus Armbruster
        error_report("invalid syntax for ethernet address");
789 f83c6e10 Mark McLoughlin
        return -1;
790 f83c6e10 Mark McLoughlin
    }
791 6eed1856 Jan Kiszka
    qemu_macaddr_default_if_unset(&nd->macaddr);
792 f83c6e10 Mark McLoughlin
793 75422b0d Amit Shah
    nd->nvectors = qemu_opt_get_number(opts, "vectors",
794 75422b0d Amit Shah
                                       DEV_NVECTORS_UNSPECIFIED);
795 75422b0d Amit Shah
    if (nd->nvectors != DEV_NVECTORS_UNSPECIFIED &&
796 f83c6e10 Mark McLoughlin
        (nd->nvectors < 0 || nd->nvectors > 0x7ffffff)) {
797 1ecda02b Markus Armbruster
        error_report("invalid # of vectors: %d", nd->nvectors);
798 f83c6e10 Mark McLoughlin
        return -1;
799 f83c6e10 Mark McLoughlin
    }
800 f83c6e10 Mark McLoughlin
801 f83c6e10 Mark McLoughlin
    nd->used = 1;
802 f83c6e10 Mark McLoughlin
    nb_nics++;
803 f83c6e10 Mark McLoughlin
804 f83c6e10 Mark McLoughlin
    return idx;
805 f83c6e10 Mark McLoughlin
}
806 f83c6e10 Mark McLoughlin
807 f83c6e10 Mark McLoughlin
#define NET_COMMON_PARAMS_DESC                     \
808 f83c6e10 Mark McLoughlin
    {                                              \
809 f83c6e10 Mark McLoughlin
        .name = "type",                            \
810 f83c6e10 Mark McLoughlin
        .type = QEMU_OPT_STRING,                   \
811 f83c6e10 Mark McLoughlin
        .help = "net client type (nic, tap etc.)", \
812 f83c6e10 Mark McLoughlin
     }, {                                          \
813 f83c6e10 Mark McLoughlin
        .name = "vlan",                            \
814 f83c6e10 Mark McLoughlin
        .type = QEMU_OPT_NUMBER,                   \
815 f83c6e10 Mark McLoughlin
        .help = "vlan number",                     \
816 f83c6e10 Mark McLoughlin
     }, {                                          \
817 f83c6e10 Mark McLoughlin
        .name = "name",                            \
818 f83c6e10 Mark McLoughlin
        .type = QEMU_OPT_STRING,                   \
819 f83c6e10 Mark McLoughlin
        .help = "identifier for monitor commands", \
820 f83c6e10 Mark McLoughlin
     }
821 f83c6e10 Mark McLoughlin
822 6d952ebe Mark McLoughlin
typedef int (*net_client_init_func)(QemuOpts *opts,
823 6d952ebe Mark McLoughlin
                                    Monitor *mon,
824 f6b134ac Mark McLoughlin
                                    const char *name,
825 f6b134ac Mark McLoughlin
                                    VLANState *vlan);
826 f83c6e10 Mark McLoughlin
827 f83c6e10 Mark McLoughlin
/* magic number, but compiler will warn if too small */
828 f83c6e10 Mark McLoughlin
#define NET_MAX_DESC 20
829 f83c6e10 Mark McLoughlin
830 238431a9 Blue Swirl
static const struct {
831 f83c6e10 Mark McLoughlin
    const char *type;
832 f83c6e10 Mark McLoughlin
    net_client_init_func init;
833 f83c6e10 Mark McLoughlin
    QemuOptDesc desc[NET_MAX_DESC];
834 6f7b3b1b Jan Kiszka
} net_client_types[NET_CLIENT_TYPE_MAX] = {
835 6f7b3b1b Jan Kiszka
    [NET_CLIENT_TYPE_NONE] = {
836 f83c6e10 Mark McLoughlin
        .type = "none",
837 f83c6e10 Mark McLoughlin
        .desc = {
838 f83c6e10 Mark McLoughlin
            NET_COMMON_PARAMS_DESC,
839 f83c6e10 Mark McLoughlin
            { /* end of list */ }
840 f83c6e10 Mark McLoughlin
        },
841 6f7b3b1b Jan Kiszka
    },
842 6f7b3b1b Jan Kiszka
    [NET_CLIENT_TYPE_NIC] = {
843 f83c6e10 Mark McLoughlin
        .type = "nic",
844 f83c6e10 Mark McLoughlin
        .init = net_init_nic,
845 f83c6e10 Mark McLoughlin
        .desc = {
846 f83c6e10 Mark McLoughlin
            NET_COMMON_PARAMS_DESC,
847 f83c6e10 Mark McLoughlin
            {
848 5869c4d5 Mark McLoughlin
                .name = "netdev",
849 5869c4d5 Mark McLoughlin
                .type = QEMU_OPT_STRING,
850 5869c4d5 Mark McLoughlin
                .help = "id of -netdev to connect to",
851 5869c4d5 Mark McLoughlin
            },
852 5869c4d5 Mark McLoughlin
            {
853 f83c6e10 Mark McLoughlin
                .name = "macaddr",
854 f83c6e10 Mark McLoughlin
                .type = QEMU_OPT_STRING,
855 f83c6e10 Mark McLoughlin
                .help = "MAC address",
856 f83c6e10 Mark McLoughlin
            }, {
857 f83c6e10 Mark McLoughlin
                .name = "model",
858 f83c6e10 Mark McLoughlin
                .type = QEMU_OPT_STRING,
859 f83c6e10 Mark McLoughlin
                .help = "device model (e1000, rtl8139, virtio etc.)",
860 f83c6e10 Mark McLoughlin
            }, {
861 f83c6e10 Mark McLoughlin
                .name = "addr",
862 f83c6e10 Mark McLoughlin
                .type = QEMU_OPT_STRING,
863 f83c6e10 Mark McLoughlin
                .help = "PCI device address",
864 f83c6e10 Mark McLoughlin
            }, {
865 f83c6e10 Mark McLoughlin
                .name = "vectors",
866 f83c6e10 Mark McLoughlin
                .type = QEMU_OPT_NUMBER,
867 f83c6e10 Mark McLoughlin
                .help = "number of MSI-x vectors, 0 to disable MSI-X",
868 f83c6e10 Mark McLoughlin
            },
869 f83c6e10 Mark McLoughlin
            { /* end of list */ }
870 f83c6e10 Mark McLoughlin
        },
871 6f7b3b1b Jan Kiszka
    },
872 ec302ffd Mark McLoughlin
#ifdef CONFIG_SLIRP
873 6f7b3b1b Jan Kiszka
    [NET_CLIENT_TYPE_USER] = {
874 ec302ffd Mark McLoughlin
        .type = "user",
875 ec302ffd Mark McLoughlin
        .init = net_init_slirp,
876 ec302ffd Mark McLoughlin
        .desc = {
877 ec302ffd Mark McLoughlin
            NET_COMMON_PARAMS_DESC,
878 ec302ffd Mark McLoughlin
            {
879 ec302ffd Mark McLoughlin
                .name = "hostname",
880 ec302ffd Mark McLoughlin
                .type = QEMU_OPT_STRING,
881 ec302ffd Mark McLoughlin
                .help = "client hostname reported by the builtin DHCP server",
882 ec302ffd Mark McLoughlin
            }, {
883 ec302ffd Mark McLoughlin
                .name = "restrict",
884 ec302ffd Mark McLoughlin
                .type = QEMU_OPT_STRING,
885 ec302ffd Mark McLoughlin
                .help = "isolate the guest from the host (y|yes|n|no)",
886 ec302ffd Mark McLoughlin
            }, {
887 ec302ffd Mark McLoughlin
                .name = "ip",
888 ec302ffd Mark McLoughlin
                .type = QEMU_OPT_STRING,
889 ec302ffd Mark McLoughlin
                .help = "legacy parameter, use net= instead",
890 ec302ffd Mark McLoughlin
            }, {
891 ec302ffd Mark McLoughlin
                .name = "net",
892 ec302ffd Mark McLoughlin
                .type = QEMU_OPT_STRING,
893 ec302ffd Mark McLoughlin
                .help = "IP address and optional netmask",
894 ec302ffd Mark McLoughlin
            }, {
895 ec302ffd Mark McLoughlin
                .name = "host",
896 ec302ffd Mark McLoughlin
                .type = QEMU_OPT_STRING,
897 ec302ffd Mark McLoughlin
                .help = "guest-visible address of the host",
898 ec302ffd Mark McLoughlin
            }, {
899 ec302ffd Mark McLoughlin
                .name = "tftp",
900 ec302ffd Mark McLoughlin
                .type = QEMU_OPT_STRING,
901 ec302ffd Mark McLoughlin
                .help = "root directory of the built-in TFTP server",
902 ec302ffd Mark McLoughlin
            }, {
903 ec302ffd Mark McLoughlin
                .name = "bootfile",
904 ec302ffd Mark McLoughlin
                .type = QEMU_OPT_STRING,
905 ec302ffd Mark McLoughlin
                .help = "BOOTP filename, for use with tftp=",
906 ec302ffd Mark McLoughlin
            }, {
907 ec302ffd Mark McLoughlin
                .name = "dhcpstart",
908 ec302ffd Mark McLoughlin
                .type = QEMU_OPT_STRING,
909 ec302ffd Mark McLoughlin
                .help = "the first of the 16 IPs the built-in DHCP server can assign",
910 ec302ffd Mark McLoughlin
            }, {
911 ec302ffd Mark McLoughlin
                .name = "dns",
912 ec302ffd Mark McLoughlin
                .type = QEMU_OPT_STRING,
913 ec302ffd Mark McLoughlin
                .help = "guest-visible address of the virtual nameserver",
914 ec302ffd Mark McLoughlin
            }, {
915 ec302ffd Mark McLoughlin
                .name = "smb",
916 ec302ffd Mark McLoughlin
                .type = QEMU_OPT_STRING,
917 ec302ffd Mark McLoughlin
                .help = "root directory of the built-in SMB server",
918 ec302ffd Mark McLoughlin
            }, {
919 ec302ffd Mark McLoughlin
                .name = "smbserver",
920 ec302ffd Mark McLoughlin
                .type = QEMU_OPT_STRING,
921 ec302ffd Mark McLoughlin
                .help = "IP address of the built-in SMB server",
922 ec302ffd Mark McLoughlin
            }, {
923 ec302ffd Mark McLoughlin
                .name = "hostfwd",
924 ec302ffd Mark McLoughlin
                .type = QEMU_OPT_STRING,
925 ec302ffd Mark McLoughlin
                .help = "guest port number to forward incoming TCP or UDP connections",
926 ec302ffd Mark McLoughlin
            }, {
927 ec302ffd Mark McLoughlin
                .name = "guestfwd",
928 ec302ffd Mark McLoughlin
                .type = QEMU_OPT_STRING,
929 ec302ffd Mark McLoughlin
                .help = "IP address and port to forward guest TCP connections",
930 ec302ffd Mark McLoughlin
            },
931 ec302ffd Mark McLoughlin
            { /* end of list */ }
932 ec302ffd Mark McLoughlin
        },
933 6f7b3b1b Jan Kiszka
    },
934 ec302ffd Mark McLoughlin
#endif
935 6f7b3b1b Jan Kiszka
    [NET_CLIENT_TYPE_TAP] = {
936 8a1c5235 Mark McLoughlin
        .type = "tap",
937 a8ed73f7 Mark McLoughlin
        .init = net_init_tap,
938 8a1c5235 Mark McLoughlin
        .desc = {
939 8a1c5235 Mark McLoughlin
            NET_COMMON_PARAMS_DESC,
940 8a1c5235 Mark McLoughlin
            {
941 8a1c5235 Mark McLoughlin
                .name = "ifname",
942 8a1c5235 Mark McLoughlin
                .type = QEMU_OPT_STRING,
943 8a1c5235 Mark McLoughlin
                .help = "interface name",
944 8a1c5235 Mark McLoughlin
            },
945 a8ed73f7 Mark McLoughlin
#ifndef _WIN32
946 8a1c5235 Mark McLoughlin
            {
947 8a1c5235 Mark McLoughlin
                .name = "fd",
948 8a1c5235 Mark McLoughlin
                .type = QEMU_OPT_STRING,
949 8a1c5235 Mark McLoughlin
                .help = "file descriptor of an already opened tap",
950 8a1c5235 Mark McLoughlin
            }, {
951 8a1c5235 Mark McLoughlin
                .name = "script",
952 8a1c5235 Mark McLoughlin
                .type = QEMU_OPT_STRING,
953 8a1c5235 Mark McLoughlin
                .help = "script to initialize the interface",
954 8a1c5235 Mark McLoughlin
            }, {
955 8a1c5235 Mark McLoughlin
                .name = "downscript",
956 8a1c5235 Mark McLoughlin
                .type = QEMU_OPT_STRING,
957 8a1c5235 Mark McLoughlin
                .help = "script to shut down the interface",
958 8a1c5235 Mark McLoughlin
            }, {
959 8a1c5235 Mark McLoughlin
                .name = "sndbuf",
960 8a1c5235 Mark McLoughlin
                .type = QEMU_OPT_SIZE,
961 8a1c5235 Mark McLoughlin
                .help = "send buffer limit"
962 baf74c95 Mark McLoughlin
            }, {
963 baf74c95 Mark McLoughlin
                .name = "vnet_hdr",
964 baf74c95 Mark McLoughlin
                .type = QEMU_OPT_BOOL,
965 baf74c95 Mark McLoughlin
                .help = "enable the IFF_VNET_HDR flag on the tap interface"
966 82b0d80e Michael S. Tsirkin
            }, {
967 82b0d80e Michael S. Tsirkin
                .name = "vhost",
968 82b0d80e Michael S. Tsirkin
                .type = QEMU_OPT_BOOL,
969 82b0d80e Michael S. Tsirkin
                .help = "enable vhost-net network accelerator",
970 82b0d80e Michael S. Tsirkin
            }, {
971 82b0d80e Michael S. Tsirkin
                .name = "vhostfd",
972 82b0d80e Michael S. Tsirkin
                .type = QEMU_OPT_STRING,
973 82b0d80e Michael S. Tsirkin
                .help = "file descriptor of an already opened vhost net device",
974 96c94b29 Jason Wang
            }, {
975 96c94b29 Jason Wang
                .name = "vhostforce",
976 96c94b29 Jason Wang
                .type = QEMU_OPT_BOOL,
977 96c94b29 Jason Wang
                .help = "force vhost on for non-MSIX virtio guests",
978 96c94b29 Jason Wang
        },
979 a8ed73f7 Mark McLoughlin
#endif /* _WIN32 */
980 8a1c5235 Mark McLoughlin
            { /* end of list */ }
981 8a1c5235 Mark McLoughlin
        },
982 6f7b3b1b Jan Kiszka
    },
983 6f7b3b1b Jan Kiszka
    [NET_CLIENT_TYPE_SOCKET] = {
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 3a75e74c Mike Ryan
            }, {
1005 3a75e74c Mike Ryan
                .name = "localaddr",
1006 3a75e74c Mike Ryan
                .type = QEMU_OPT_STRING,
1007 3a75e74c Mike Ryan
                .help = "source address for multicast packets",
1008 88ce16ca Mark McLoughlin
            },
1009 88ce16ca Mark McLoughlin
            { /* end of list */ }
1010 88ce16ca Mark McLoughlin
        },
1011 6f7b3b1b Jan Kiszka
    },
1012 dd51058d Mark McLoughlin
#ifdef CONFIG_VDE
1013 6f7b3b1b Jan Kiszka
    [NET_CLIENT_TYPE_VDE] = {
1014 dd51058d Mark McLoughlin
        .type = "vde",
1015 dd51058d Mark McLoughlin
        .init = net_init_vde,
1016 dd51058d Mark McLoughlin
        .desc = {
1017 dd51058d Mark McLoughlin
            NET_COMMON_PARAMS_DESC,
1018 dd51058d Mark McLoughlin
            {
1019 dd51058d Mark McLoughlin
                .name = "sock",
1020 dd51058d Mark McLoughlin
                .type = QEMU_OPT_STRING,
1021 dd51058d Mark McLoughlin
                .help = "socket path",
1022 dd51058d Mark McLoughlin
            }, {
1023 dd51058d Mark McLoughlin
                .name = "port",
1024 dd51058d Mark McLoughlin
                .type = QEMU_OPT_NUMBER,
1025 dd51058d Mark McLoughlin
                .help = "port number",
1026 dd51058d Mark McLoughlin
            }, {
1027 dd51058d Mark McLoughlin
                .name = "group",
1028 dd51058d Mark McLoughlin
                .type = QEMU_OPT_STRING,
1029 dd51058d Mark McLoughlin
                .help = "group owner of socket",
1030 dd51058d Mark McLoughlin
            }, {
1031 dd51058d Mark McLoughlin
                .name = "mode",
1032 dd51058d Mark McLoughlin
                .type = QEMU_OPT_NUMBER,
1033 dd51058d Mark McLoughlin
                .help = "permissions for socket",
1034 dd51058d Mark McLoughlin
            },
1035 dd51058d Mark McLoughlin
            { /* end of list */ }
1036 dd51058d Mark McLoughlin
        },
1037 6f7b3b1b Jan Kiszka
    },
1038 dd51058d Mark McLoughlin
#endif
1039 6f7b3b1b Jan Kiszka
    [NET_CLIENT_TYPE_DUMP] = {
1040 ed2955c2 Mark McLoughlin
        .type = "dump",
1041 ed2955c2 Mark McLoughlin
        .init = net_init_dump,
1042 ed2955c2 Mark McLoughlin
        .desc = {
1043 ed2955c2 Mark McLoughlin
            NET_COMMON_PARAMS_DESC,
1044 ed2955c2 Mark McLoughlin
            {
1045 ed2955c2 Mark McLoughlin
                .name = "len",
1046 ed2955c2 Mark McLoughlin
                .type = QEMU_OPT_SIZE,
1047 ed2955c2 Mark McLoughlin
                .help = "per-packet size limit (64k default)",
1048 ed2955c2 Mark McLoughlin
            }, {
1049 ed2955c2 Mark McLoughlin
                .name = "file",
1050 ed2955c2 Mark McLoughlin
                .type = QEMU_OPT_STRING,
1051 ed2955c2 Mark McLoughlin
                .help = "dump file path (default is qemu-vlan0.pcap)",
1052 ed2955c2 Mark McLoughlin
            },
1053 ed2955c2 Mark McLoughlin
            { /* end of list */ }
1054 ed2955c2 Mark McLoughlin
        },
1055 f83c6e10 Mark McLoughlin
    },
1056 f83c6e10 Mark McLoughlin
};
1057 f83c6e10 Mark McLoughlin
1058 f6b134ac Mark McLoughlin
int net_client_init(Monitor *mon, QemuOpts *opts, int is_netdev)
1059 f83c6e10 Mark McLoughlin
{
1060 6d952ebe Mark McLoughlin
    const char *name;
1061 f83c6e10 Mark McLoughlin
    const char *type;
1062 f83c6e10 Mark McLoughlin
    int i;
1063 f83c6e10 Mark McLoughlin
1064 f83c6e10 Mark McLoughlin
    type = qemu_opt_get(opts, "type");
1065 5294e2c7 Markus Armbruster
    if (!type) {
1066 5294e2c7 Markus Armbruster
        qerror_report(QERR_MISSING_PARAMETER, "type");
1067 5294e2c7 Markus Armbruster
        return -1;
1068 5294e2c7 Markus Armbruster
    }
1069 f83c6e10 Mark McLoughlin
1070 5294e2c7 Markus Armbruster
    if (is_netdev) {
1071 f6b134ac Mark McLoughlin
        if (strcmp(type, "tap") != 0 &&
1072 f6b134ac Mark McLoughlin
#ifdef CONFIG_SLIRP
1073 f6b134ac Mark McLoughlin
            strcmp(type, "user") != 0 &&
1074 f6b134ac Mark McLoughlin
#endif
1075 f6b134ac Mark McLoughlin
#ifdef CONFIG_VDE
1076 f6b134ac Mark McLoughlin
            strcmp(type, "vde") != 0 &&
1077 f6b134ac Mark McLoughlin
#endif
1078 f6b134ac Mark McLoughlin
            strcmp(type, "socket") != 0) {
1079 5294e2c7 Markus Armbruster
            qerror_report(QERR_INVALID_PARAMETER_VALUE, "type",
1080 5294e2c7 Markus Armbruster
                          "a netdev backend type");
1081 f6b134ac Mark McLoughlin
            return -1;
1082 f6b134ac Mark McLoughlin
        }
1083 f6b134ac Mark McLoughlin
1084 f6b134ac Mark McLoughlin
        if (qemu_opt_get(opts, "vlan")) {
1085 5294e2c7 Markus Armbruster
            qerror_report(QERR_INVALID_PARAMETER, "vlan");
1086 f6b134ac Mark McLoughlin
            return -1;
1087 f6b134ac Mark McLoughlin
        }
1088 f6b134ac Mark McLoughlin
        if (qemu_opt_get(opts, "name")) {
1089 5294e2c7 Markus Armbruster
            qerror_report(QERR_INVALID_PARAMETER, "name");
1090 f6b134ac Mark McLoughlin
            return -1;
1091 f6b134ac Mark McLoughlin
        }
1092 f6b134ac Mark McLoughlin
        if (!qemu_opts_id(opts)) {
1093 5294e2c7 Markus Armbruster
            qerror_report(QERR_MISSING_PARAMETER, "id");
1094 f6b134ac Mark McLoughlin
            return -1;
1095 f6b134ac Mark McLoughlin
        }
1096 f6b134ac Mark McLoughlin
    }
1097 f6b134ac Mark McLoughlin
1098 6d952ebe Mark McLoughlin
    name = qemu_opts_id(opts);
1099 6d952ebe Mark McLoughlin
    if (!name) {
1100 6d952ebe Mark McLoughlin
        name = qemu_opt_get(opts, "name");
1101 6d952ebe Mark McLoughlin
    }
1102 6d952ebe Mark McLoughlin
1103 6f7b3b1b Jan Kiszka
    for (i = 0; i < NET_CLIENT_TYPE_MAX; i++) {
1104 6f7b3b1b Jan Kiszka
        if (net_client_types[i].type != NULL &&
1105 6f7b3b1b Jan Kiszka
            !strcmp(net_client_types[i].type, type)) {
1106 f6b134ac Mark McLoughlin
            VLANState *vlan = NULL;
1107 50e32ea8 Amit Shah
            int ret;
1108 f6b134ac Mark McLoughlin
1109 f83c6e10 Mark McLoughlin
            if (qemu_opts_validate(opts, &net_client_types[i].desc[0]) == -1) {
1110 f83c6e10 Mark McLoughlin
                return -1;
1111 f83c6e10 Mark McLoughlin
            }
1112 f83c6e10 Mark McLoughlin
1113 5869c4d5 Mark McLoughlin
            /* Do not add to a vlan if it's a -netdev or a nic with a
1114 5869c4d5 Mark McLoughlin
             * netdev= parameter. */
1115 5869c4d5 Mark McLoughlin
            if (!(is_netdev ||
1116 5869c4d5 Mark McLoughlin
                  (strcmp(type, "nic") == 0 && qemu_opt_get(opts, "netdev")))) {
1117 f6b134ac Mark McLoughlin
                vlan = qemu_find_vlan(qemu_opt_get_number(opts, "vlan", 0), 1);
1118 f6b134ac Mark McLoughlin
            }
1119 f6b134ac Mark McLoughlin
1120 03c71553 Amit Shah
            ret = 0;
1121 f83c6e10 Mark McLoughlin
            if (net_client_types[i].init) {
1122 50e32ea8 Amit Shah
                ret = net_client_types[i].init(opts, mon, name, vlan);
1123 50e32ea8 Amit Shah
                if (ret < 0) {
1124 5294e2c7 Markus Armbruster
                    /* TODO push error reporting into init() methods */
1125 5294e2c7 Markus Armbruster
                    qerror_report(QERR_DEVICE_INIT_FAILED, type);
1126 5294e2c7 Markus Armbruster
                    return -1;
1127 5294e2c7 Markus Armbruster
                }
1128 f83c6e10 Mark McLoughlin
            }
1129 50e32ea8 Amit Shah
            return ret;
1130 f83c6e10 Mark McLoughlin
        }
1131 f83c6e10 Mark McLoughlin
    }
1132 f83c6e10 Mark McLoughlin
1133 5294e2c7 Markus Armbruster
    qerror_report(QERR_INVALID_PARAMETER_VALUE, "type",
1134 5294e2c7 Markus Armbruster
                  "a network client type");
1135 f83c6e10 Mark McLoughlin
    return -1;
1136 f83c6e10 Mark McLoughlin
}
1137 f83c6e10 Mark McLoughlin
1138 6f338c34 aliguori
static int net_host_check_device(const char *device)
1139 6f338c34 aliguori
{
1140 6f338c34 aliguori
    int i;
1141 bb9ea79e aliguori
    const char *valid_param_list[] = { "tap", "socket", "dump"
1142 6f338c34 aliguori
#ifdef CONFIG_SLIRP
1143 6f338c34 aliguori
                                       ,"user"
1144 6f338c34 aliguori
#endif
1145 6f338c34 aliguori
#ifdef CONFIG_VDE
1146 6f338c34 aliguori
                                       ,"vde"
1147 6f338c34 aliguori
#endif
1148 6f338c34 aliguori
    };
1149 6f338c34 aliguori
    for (i = 0; i < sizeof(valid_param_list) / sizeof(char *); i++) {
1150 6f338c34 aliguori
        if (!strncmp(valid_param_list[i], device,
1151 6f338c34 aliguori
                     strlen(valid_param_list[i])))
1152 6f338c34 aliguori
            return 1;
1153 6f338c34 aliguori
    }
1154 6f338c34 aliguori
1155 6f338c34 aliguori
    return 0;
1156 6f338c34 aliguori
}
1157 6f338c34 aliguori
1158 f18c16de Luiz Capitulino
void net_host_device_add(Monitor *mon, const QDict *qdict)
1159 6f338c34 aliguori
{
1160 f18c16de Luiz Capitulino
    const char *device = qdict_get_str(qdict, "device");
1161 7f1c9d20 Mark McLoughlin
    const char *opts_str = qdict_get_try_str(qdict, "opts");
1162 7f1c9d20 Mark McLoughlin
    QemuOpts *opts;
1163 f18c16de Luiz Capitulino
1164 6f338c34 aliguori
    if (!net_host_check_device(device)) {
1165 376253ec aliguori
        monitor_printf(mon, "invalid host network device %s\n", device);
1166 6f338c34 aliguori
        return;
1167 6f338c34 aliguori
    }
1168 7f1c9d20 Mark McLoughlin
1169 3329f07b Gerd Hoffmann
    opts = qemu_opts_parse(qemu_find_opts("net"), opts_str ? opts_str : "", 0);
1170 7f1c9d20 Mark McLoughlin
    if (!opts) {
1171 7f1c9d20 Mark McLoughlin
        return;
1172 7f1c9d20 Mark McLoughlin
    }
1173 7f1c9d20 Mark McLoughlin
1174 7f1c9d20 Mark McLoughlin
    qemu_opt_set(opts, "type", device);
1175 7f1c9d20 Mark McLoughlin
1176 f6b134ac Mark McLoughlin
    if (net_client_init(mon, opts, 0) < 0) {
1177 5c8be678 aliguori
        monitor_printf(mon, "adding host network device %s failed\n", device);
1178 5c8be678 aliguori
    }
1179 6f338c34 aliguori
}
1180 6f338c34 aliguori
1181 f18c16de Luiz Capitulino
void net_host_device_remove(Monitor *mon, const QDict *qdict)
1182 6f338c34 aliguori
{
1183 6f338c34 aliguori
    VLANClientState *vc;
1184 f18c16de Luiz Capitulino
    int vlan_id = qdict_get_int(qdict, "vlan_id");
1185 f18c16de Luiz Capitulino
    const char *device = qdict_get_str(qdict, "device");
1186 6f338c34 aliguori
1187 1a609520 Jan Kiszka
    vc = qemu_find_vlan_client_by_name(mon, vlan_id, device);
1188 6f338c34 aliguori
    if (!vc) {
1189 6f338c34 aliguori
        return;
1190 6f338c34 aliguori
    }
1191 e8f1f9db aliguori
    if (!net_host_check_device(vc->model)) {
1192 e8f1f9db aliguori
        monitor_printf(mon, "invalid host network device %s\n", device);
1193 e8f1f9db aliguori
        return;
1194 e8f1f9db aliguori
    }
1195 6f338c34 aliguori
    qemu_del_vlan_client(vc);
1196 6f338c34 aliguori
}
1197 6f338c34 aliguori
1198 ae82d324 Markus Armbruster
int do_netdev_add(Monitor *mon, const QDict *qdict, QObject **ret_data)
1199 ae82d324 Markus Armbruster
{
1200 ae82d324 Markus Armbruster
    QemuOpts *opts;
1201 ae82d324 Markus Armbruster
    int res;
1202 ae82d324 Markus Armbruster
1203 3329f07b Gerd Hoffmann
    opts = qemu_opts_from_qdict(qemu_find_opts("netdev"), qdict);
1204 ae82d324 Markus Armbruster
    if (!opts) {
1205 ae82d324 Markus Armbruster
        return -1;
1206 ae82d324 Markus Armbruster
    }
1207 ae82d324 Markus Armbruster
1208 ae82d324 Markus Armbruster
    res = net_client_init(mon, opts, 1);
1209 410cbafe Yoshiaki Tamura
    if (res < 0) {
1210 410cbafe Yoshiaki Tamura
        qemu_opts_del(opts);
1211 410cbafe Yoshiaki Tamura
    }
1212 410cbafe Yoshiaki Tamura
1213 ae82d324 Markus Armbruster
    return res;
1214 ae82d324 Markus Armbruster
}
1215 ae82d324 Markus Armbruster
1216 ae82d324 Markus Armbruster
int do_netdev_del(Monitor *mon, const QDict *qdict, QObject **ret_data)
1217 ae82d324 Markus Armbruster
{
1218 ae82d324 Markus Armbruster
    const char *id = qdict_get_str(qdict, "id");
1219 ae82d324 Markus Armbruster
    VLANClientState *vc;
1220 ae82d324 Markus Armbruster
1221 ae82d324 Markus Armbruster
    vc = qemu_find_netdev(id);
1222 85dde9a9 Markus Armbruster
    if (!vc) {
1223 ae82d324 Markus Armbruster
        qerror_report(QERR_DEVICE_NOT_FOUND, id);
1224 ae82d324 Markus Armbruster
        return -1;
1225 ae82d324 Markus Armbruster
    }
1226 ae82d324 Markus Armbruster
    qemu_del_vlan_client(vc);
1227 3329f07b Gerd Hoffmann
    qemu_opts_del(qemu_opts_find(qemu_find_opts("netdev"), id));
1228 ae82d324 Markus Armbruster
    return 0;
1229 ae82d324 Markus Armbruster
}
1230 ae82d324 Markus Armbruster
1231 44e798d3 Jan Kiszka
static void print_net_client(Monitor *mon, VLANClientState *vc)
1232 44e798d3 Jan Kiszka
{
1233 44e798d3 Jan Kiszka
    monitor_printf(mon, "%s: type=%s,%s\n", vc->name,
1234 44e798d3 Jan Kiszka
                   net_client_types[vc->info->type].type, vc->info_str);
1235 44e798d3 Jan Kiszka
}
1236 44e798d3 Jan Kiszka
1237 376253ec aliguori
void do_info_network(Monitor *mon)
1238 63a01ef8 aliguori
{
1239 63a01ef8 aliguori
    VLANState *vlan;
1240 19061e63 Jan Kiszka
    VLANClientState *vc, *peer;
1241 19061e63 Jan Kiszka
    net_client_type type;
1242 63a01ef8 aliguori
1243 5610c3aa Mark McLoughlin
    QTAILQ_FOREACH(vlan, &vlans, next) {
1244 376253ec aliguori
        monitor_printf(mon, "VLAN %d devices:\n", vlan->id);
1245 5610c3aa Mark McLoughlin
1246 5610c3aa Mark McLoughlin
        QTAILQ_FOREACH(vc, &vlan->clients, next) {
1247 44e798d3 Jan Kiszka
            monitor_printf(mon, "  ");
1248 44e798d3 Jan Kiszka
            print_net_client(mon, vc);
1249 5610c3aa Mark McLoughlin
        }
1250 63a01ef8 aliguori
    }
1251 a0104e0e Markus Armbruster
    monitor_printf(mon, "Devices not on any VLAN:\n");
1252 a0104e0e Markus Armbruster
    QTAILQ_FOREACH(vc, &non_vlan_clients, next) {
1253 19061e63 Jan Kiszka
        peer = vc->peer;
1254 19061e63 Jan Kiszka
        type = vc->info->type;
1255 19061e63 Jan Kiszka
        if (!peer || type == NET_CLIENT_TYPE_NIC) {
1256 44e798d3 Jan Kiszka
            monitor_printf(mon, "  ");
1257 44e798d3 Jan Kiszka
            print_net_client(mon, vc);
1258 19061e63 Jan Kiszka
        } /* else it's a netdev connected to a NIC, printed with the NIC */
1259 19061e63 Jan Kiszka
        if (peer && type == NET_CLIENT_TYPE_NIC) {
1260 44e798d3 Jan Kiszka
            monitor_printf(mon, "   \\ ");
1261 44e798d3 Jan Kiszka
            print_net_client(mon, peer);
1262 a0104e0e Markus Armbruster
        }
1263 a0104e0e Markus Armbruster
    }
1264 63a01ef8 aliguori
}
1265 63a01ef8 aliguori
1266 5369e3c0 Markus Armbruster
int do_set_link(Monitor *mon, const QDict *qdict, QObject **ret_data)
1267 436e5e53 aliguori
{
1268 436e5e53 aliguori
    VLANState *vlan;
1269 436e5e53 aliguori
    VLANClientState *vc = NULL;
1270 f18c16de Luiz Capitulino
    const char *name = qdict_get_str(qdict, "name");
1271 c9b26a4c Markus Armbruster
    int up = qdict_get_bool(qdict, "up");
1272 436e5e53 aliguori
1273 5610c3aa Mark McLoughlin
    QTAILQ_FOREACH(vlan, &vlans, next) {
1274 5610c3aa Mark McLoughlin
        QTAILQ_FOREACH(vc, &vlan->clients, next) {
1275 5610c3aa Mark McLoughlin
            if (strcmp(vc->name, name) == 0) {
1276 dd5de373 edgar_igl
                goto done;
1277 5610c3aa Mark McLoughlin
            }
1278 5610c3aa Mark McLoughlin
        }
1279 5610c3aa Mark McLoughlin
    }
1280 85dde9a9 Markus Armbruster
    QTAILQ_FOREACH(vc, &non_vlan_clients, next) {
1281 85dde9a9 Markus Armbruster
        if (!strcmp(vc->name, name)) {
1282 85dde9a9 Markus Armbruster
            goto done;
1283 85dde9a9 Markus Armbruster
        }
1284 85dde9a9 Markus Armbruster
    }
1285 dd5de373 edgar_igl
done:
1286 436e5e53 aliguori
1287 436e5e53 aliguori
    if (!vc) {
1288 5369e3c0 Markus Armbruster
        qerror_report(QERR_DEVICE_NOT_FOUND, name);
1289 5369e3c0 Markus Armbruster
        return -1;
1290 436e5e53 aliguori
    }
1291 436e5e53 aliguori
1292 c9b26a4c Markus Armbruster
    vc->link_down = !up;
1293 436e5e53 aliguori
1294 665a3b07 Mark McLoughlin
    if (vc->info->link_status_changed) {
1295 665a3b07 Mark McLoughlin
        vc->info->link_status_changed(vc);
1296 665a3b07 Mark McLoughlin
    }
1297 ab1cbe1c Michael S. Tsirkin
1298 ab1cbe1c Michael S. Tsirkin
    /* Notify peer. Don't update peer link status: this makes it possible to
1299 ab1cbe1c Michael S. Tsirkin
     * disconnect from host network without notifying the guest.
1300 ab1cbe1c Michael S. Tsirkin
     * FIXME: is disconnected link status change operation useful?
1301 ab1cbe1c Michael S. Tsirkin
     *
1302 ab1cbe1c Michael S. Tsirkin
     * Current behaviour is compatible with qemu vlans where there could be
1303 ab1cbe1c Michael S. Tsirkin
     * multiple clients that can still communicate with each other in
1304 ab1cbe1c Michael S. Tsirkin
     * disconnected mode. For now maintain this compatibility. */
1305 ab1cbe1c Michael S. Tsirkin
    if (vc->peer && vc->peer->info->link_status_changed) {
1306 ab1cbe1c Michael S. Tsirkin
        vc->peer->info->link_status_changed(vc->peer);
1307 ab1cbe1c Michael S. Tsirkin
    }
1308 5369e3c0 Markus Armbruster
    return 0;
1309 436e5e53 aliguori
}
1310 436e5e53 aliguori
1311 63a01ef8 aliguori
void net_cleanup(void)
1312 63a01ef8 aliguori
{
1313 63a01ef8 aliguori
    VLANState *vlan;
1314 577c4af9 Mark McLoughlin
    VLANClientState *vc, *next_vc;
1315 63a01ef8 aliguori
1316 5610c3aa Mark McLoughlin
    QTAILQ_FOREACH(vlan, &vlans, next) {
1317 5610c3aa Mark McLoughlin
        QTAILQ_FOREACH_SAFE(vc, &vlan->clients, next, next_vc) {
1318 b946a153 aliguori
            qemu_del_vlan_client(vc);
1319 63a01ef8 aliguori
        }
1320 63a01ef8 aliguori
    }
1321 577c4af9 Mark McLoughlin
1322 577c4af9 Mark McLoughlin
    QTAILQ_FOREACH_SAFE(vc, &non_vlan_clients, next, next_vc) {
1323 577c4af9 Mark McLoughlin
        qemu_del_vlan_client(vc);
1324 577c4af9 Mark McLoughlin
    }
1325 63a01ef8 aliguori
}
1326 63a01ef8 aliguori
1327 668680f7 Markus Armbruster
void net_check_clients(void)
1328 63a01ef8 aliguori
{
1329 63a01ef8 aliguori
    VLANState *vlan;
1330 62112d18 Markus Armbruster
    VLANClientState *vc;
1331 48e2faf2 Peter Maydell
    int i;
1332 63a01ef8 aliguori
1333 641f6eae Peter Maydell
    /* Don't warn about the default network setup that you get if
1334 641f6eae Peter Maydell
     * no command line -net or -netdev options are specified. There
1335 641f6eae Peter Maydell
     * are two cases that we would otherwise complain about:
1336 641f6eae Peter Maydell
     * (1) board doesn't support a NIC but the implicit "-net nic"
1337 641f6eae Peter Maydell
     * requested one
1338 641f6eae Peter Maydell
     * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
1339 641f6eae Peter Maydell
     * sets up a nic that isn't connected to anything.
1340 641f6eae Peter Maydell
     */
1341 641f6eae Peter Maydell
    if (default_net) {
1342 641f6eae Peter Maydell
        return;
1343 641f6eae Peter Maydell
    }
1344 641f6eae Peter Maydell
1345 5610c3aa Mark McLoughlin
    QTAILQ_FOREACH(vlan, &vlans, next) {
1346 ac60cc18 Tristan Gingold
        int has_nic = 0, has_host_dev = 0;
1347 ac60cc18 Tristan Gingold
1348 62112d18 Markus Armbruster
        QTAILQ_FOREACH(vc, &vlan->clients, next) {
1349 62112d18 Markus Armbruster
            switch (vc->info->type) {
1350 62112d18 Markus Armbruster
            case NET_CLIENT_TYPE_NIC:
1351 62112d18 Markus Armbruster
                has_nic = 1;
1352 62112d18 Markus Armbruster
                break;
1353 6f7b3b1b Jan Kiszka
            case NET_CLIENT_TYPE_USER:
1354 62112d18 Markus Armbruster
            case NET_CLIENT_TYPE_TAP:
1355 62112d18 Markus Armbruster
            case NET_CLIENT_TYPE_SOCKET:
1356 62112d18 Markus Armbruster
            case NET_CLIENT_TYPE_VDE:
1357 62112d18 Markus Armbruster
                has_host_dev = 1;
1358 62112d18 Markus Armbruster
                break;
1359 62112d18 Markus Armbruster
            default: ;
1360 62112d18 Markus Armbruster
            }
1361 62112d18 Markus Armbruster
        }
1362 62112d18 Markus Armbruster
        if (has_host_dev && !has_nic)
1363 63a01ef8 aliguori
            fprintf(stderr, "Warning: vlan %d with no nics\n", vlan->id);
1364 62112d18 Markus Armbruster
        if (has_nic && !has_host_dev)
1365 63a01ef8 aliguori
            fprintf(stderr,
1366 63a01ef8 aliguori
                    "Warning: vlan %d is not connected to host network\n",
1367 63a01ef8 aliguori
                    vlan->id);
1368 63a01ef8 aliguori
    }
1369 efe32fdd Markus Armbruster
    QTAILQ_FOREACH(vc, &non_vlan_clients, next) {
1370 efe32fdd Markus Armbruster
        if (!vc->peer) {
1371 efe32fdd Markus Armbruster
            fprintf(stderr, "Warning: %s %s has no peer\n",
1372 efe32fdd Markus Armbruster
                    vc->info->type == NET_CLIENT_TYPE_NIC ? "nic" : "netdev",
1373 efe32fdd Markus Armbruster
                    vc->name);
1374 efe32fdd Markus Armbruster
        }
1375 efe32fdd Markus Armbruster
    }
1376 48e2faf2 Peter Maydell
1377 48e2faf2 Peter Maydell
    /* Check that all NICs requested via -net nic actually got created.
1378 48e2faf2 Peter Maydell
     * NICs created via -device don't need to be checked here because
1379 48e2faf2 Peter Maydell
     * they are always instantiated.
1380 48e2faf2 Peter Maydell
     */
1381 48e2faf2 Peter Maydell
    for (i = 0; i < MAX_NICS; i++) {
1382 48e2faf2 Peter Maydell
        NICInfo *nd = &nd_table[i];
1383 48e2faf2 Peter Maydell
        if (nd->used && !nd->instantiated) {
1384 48e2faf2 Peter Maydell
            fprintf(stderr, "Warning: requested NIC (%s, model %s) "
1385 48e2faf2 Peter Maydell
                    "was not created (not supported by this machine?)\n",
1386 48e2faf2 Peter Maydell
                    nd->name ? nd->name : "anonymous",
1387 48e2faf2 Peter Maydell
                    nd->model ? nd->model : "unspecified");
1388 48e2faf2 Peter Maydell
        }
1389 48e2faf2 Peter Maydell
    }
1390 63a01ef8 aliguori
}
1391 dc1c9fe8 Mark McLoughlin
1392 dc1c9fe8 Mark McLoughlin
static int net_init_client(QemuOpts *opts, void *dummy)
1393 dc1c9fe8 Mark McLoughlin
{
1394 c1671a08 Mark McLoughlin
    if (net_client_init(NULL, opts, 0) < 0)
1395 c1671a08 Mark McLoughlin
        return -1;
1396 c1671a08 Mark McLoughlin
    return 0;
1397 f6b134ac Mark McLoughlin
}
1398 f6b134ac Mark McLoughlin
1399 f6b134ac Mark McLoughlin
static int net_init_netdev(QemuOpts *opts, void *dummy)
1400 f6b134ac Mark McLoughlin
{
1401 f6b134ac Mark McLoughlin
    return net_client_init(NULL, opts, 1);
1402 dc1c9fe8 Mark McLoughlin
}
1403 dc1c9fe8 Mark McLoughlin
1404 dc1c9fe8 Mark McLoughlin
int net_init_clients(void)
1405 dc1c9fe8 Mark McLoughlin
{
1406 3329f07b Gerd Hoffmann
    QemuOptsList *net = qemu_find_opts("net");
1407 3329f07b Gerd Hoffmann
1408 cb4522cc Gerd Hoffmann
    if (default_net) {
1409 dc1c9fe8 Mark McLoughlin
        /* if no clients, we use a default config */
1410 3329f07b Gerd Hoffmann
        qemu_opts_set(net, NULL, "type", "nic");
1411 dc1c9fe8 Mark McLoughlin
#ifdef CONFIG_SLIRP
1412 3329f07b Gerd Hoffmann
        qemu_opts_set(net, NULL, "type", "user");
1413 dc1c9fe8 Mark McLoughlin
#endif
1414 dc1c9fe8 Mark McLoughlin
    }
1415 dc1c9fe8 Mark McLoughlin
1416 5610c3aa Mark McLoughlin
    QTAILQ_INIT(&vlans);
1417 577c4af9 Mark McLoughlin
    QTAILQ_INIT(&non_vlan_clients);
1418 5610c3aa Mark McLoughlin
1419 3329f07b Gerd Hoffmann
    if (qemu_opts_foreach(qemu_find_opts("netdev"), net_init_netdev, NULL, 1) == -1)
1420 f6b134ac Mark McLoughlin
        return -1;
1421 f6b134ac Mark McLoughlin
1422 3329f07b Gerd Hoffmann
    if (qemu_opts_foreach(net, net_init_client, NULL, 1) == -1) {
1423 dc1c9fe8 Mark McLoughlin
        return -1;
1424 dc1c9fe8 Mark McLoughlin
    }
1425 dc1c9fe8 Mark McLoughlin
1426 dc1c9fe8 Mark McLoughlin
    return 0;
1427 dc1c9fe8 Mark McLoughlin
}
1428 dc1c9fe8 Mark McLoughlin
1429 7f161aae Mark McLoughlin
int net_client_parse(QemuOptsList *opts_list, const char *optarg)
1430 dc1c9fe8 Mark McLoughlin
{
1431 a3a766e7 Juan Quintela
#if defined(CONFIG_SLIRP)
1432 68ac40d2 Mark McLoughlin
    int ret;
1433 68ac40d2 Mark McLoughlin
    if (net_slirp_parse_legacy(opts_list, optarg, &ret)) {
1434 dc1c9fe8 Mark McLoughlin
        return ret;
1435 dc1c9fe8 Mark McLoughlin
    }
1436 a3a766e7 Juan Quintela
#endif
1437 68ac40d2 Mark McLoughlin
1438 8212c64f Markus Armbruster
    if (!qemu_opts_parse(opts_list, optarg, 1)) {
1439 dc1c9fe8 Mark McLoughlin
        return -1;
1440 dc1c9fe8 Mark McLoughlin
    }
1441 dc1c9fe8 Mark McLoughlin
1442 cb4522cc Gerd Hoffmann
    default_net = 0;
1443 dc1c9fe8 Mark McLoughlin
    return 0;
1444 dc1c9fe8 Mark McLoughlin
}