Statistics
| Branch: | Revision:

root / net.c @ 00c3a05b

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