Statistics
| Branch: | Revision:

root / net.c @ 73f5e313

History | View | Annotate | Download (38.8 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 443916d1 Stefan Berger
        fd = qemu_parse_fd(param);
737 c1d6eed7 Mark McLoughlin
    }
738 f7c31d63 Jason Wang
739 f7c31d63 Jason Wang
    return fd;
740 c1d6eed7 Mark McLoughlin
}
741 c1d6eed7 Mark McLoughlin
742 f6b134ac Mark McLoughlin
static int net_init_nic(QemuOpts *opts,
743 f6b134ac Mark McLoughlin
                        Monitor *mon,
744 f6b134ac Mark McLoughlin
                        const char *name,
745 f6b134ac Mark McLoughlin
                        VLANState *vlan)
746 f83c6e10 Mark McLoughlin
{
747 f83c6e10 Mark McLoughlin
    int idx;
748 f83c6e10 Mark McLoughlin
    NICInfo *nd;
749 5869c4d5 Mark McLoughlin
    const char *netdev;
750 f83c6e10 Mark McLoughlin
751 f83c6e10 Mark McLoughlin
    idx = nic_get_free_idx();
752 f83c6e10 Mark McLoughlin
    if (idx == -1 || nb_nics >= MAX_NICS) {
753 1ecda02b Markus Armbruster
        error_report("Too Many NICs");
754 f83c6e10 Mark McLoughlin
        return -1;
755 f83c6e10 Mark McLoughlin
    }
756 f83c6e10 Mark McLoughlin
757 f83c6e10 Mark McLoughlin
    nd = &nd_table[idx];
758 f83c6e10 Mark McLoughlin
759 f83c6e10 Mark McLoughlin
    memset(nd, 0, sizeof(*nd));
760 f83c6e10 Mark McLoughlin
761 5869c4d5 Mark McLoughlin
    if ((netdev = qemu_opt_get(opts, "netdev"))) {
762 5869c4d5 Mark McLoughlin
        nd->netdev = qemu_find_netdev(netdev);
763 5869c4d5 Mark McLoughlin
        if (!nd->netdev) {
764 1ecda02b Markus Armbruster
            error_report("netdev '%s' not found", netdev);
765 5869c4d5 Mark McLoughlin
            return -1;
766 5869c4d5 Mark McLoughlin
        }
767 5869c4d5 Mark McLoughlin
    } else {
768 5869c4d5 Mark McLoughlin
        assert(vlan);
769 5869c4d5 Mark McLoughlin
        nd->vlan = vlan;
770 5869c4d5 Mark McLoughlin
    }
771 6d952ebe Mark McLoughlin
    if (name) {
772 7267c094 Anthony Liguori
        nd->name = g_strdup(name);
773 6d952ebe Mark McLoughlin
    }
774 f83c6e10 Mark McLoughlin
    if (qemu_opt_get(opts, "model")) {
775 7267c094 Anthony Liguori
        nd->model = g_strdup(qemu_opt_get(opts, "model"));
776 f83c6e10 Mark McLoughlin
    }
777 f83c6e10 Mark McLoughlin
    if (qemu_opt_get(opts, "addr")) {
778 7267c094 Anthony Liguori
        nd->devaddr = g_strdup(qemu_opt_get(opts, "addr"));
779 f83c6e10 Mark McLoughlin
    }
780 f83c6e10 Mark McLoughlin
781 f83c6e10 Mark McLoughlin
    if (qemu_opt_get(opts, "macaddr") &&
782 6eed1856 Jan Kiszka
        net_parse_macaddr(nd->macaddr.a, qemu_opt_get(opts, "macaddr")) < 0) {
783 1ecda02b Markus Armbruster
        error_report("invalid syntax for ethernet address");
784 f83c6e10 Mark McLoughlin
        return -1;
785 f83c6e10 Mark McLoughlin
    }
786 6eed1856 Jan Kiszka
    qemu_macaddr_default_if_unset(&nd->macaddr);
787 f83c6e10 Mark McLoughlin
788 75422b0d Amit Shah
    nd->nvectors = qemu_opt_get_number(opts, "vectors",
789 75422b0d Amit Shah
                                       DEV_NVECTORS_UNSPECIFIED);
790 75422b0d Amit Shah
    if (nd->nvectors != DEV_NVECTORS_UNSPECIFIED &&
791 f83c6e10 Mark McLoughlin
        (nd->nvectors < 0 || nd->nvectors > 0x7ffffff)) {
792 1ecda02b Markus Armbruster
        error_report("invalid # of vectors: %d", nd->nvectors);
793 f83c6e10 Mark McLoughlin
        return -1;
794 f83c6e10 Mark McLoughlin
    }
795 f83c6e10 Mark McLoughlin
796 f83c6e10 Mark McLoughlin
    nd->used = 1;
797 f83c6e10 Mark McLoughlin
    nb_nics++;
798 f83c6e10 Mark McLoughlin
799 f83c6e10 Mark McLoughlin
    return idx;
800 f83c6e10 Mark McLoughlin
}
801 f83c6e10 Mark McLoughlin
802 f83c6e10 Mark McLoughlin
#define NET_COMMON_PARAMS_DESC                     \
803 f83c6e10 Mark McLoughlin
    {                                              \
804 f83c6e10 Mark McLoughlin
        .name = "type",                            \
805 f83c6e10 Mark McLoughlin
        .type = QEMU_OPT_STRING,                   \
806 f83c6e10 Mark McLoughlin
        .help = "net client type (nic, tap etc.)", \
807 f83c6e10 Mark McLoughlin
     }, {                                          \
808 f83c6e10 Mark McLoughlin
        .name = "vlan",                            \
809 f83c6e10 Mark McLoughlin
        .type = QEMU_OPT_NUMBER,                   \
810 f83c6e10 Mark McLoughlin
        .help = "vlan number",                     \
811 f83c6e10 Mark McLoughlin
     }, {                                          \
812 f83c6e10 Mark McLoughlin
        .name = "name",                            \
813 f83c6e10 Mark McLoughlin
        .type = QEMU_OPT_STRING,                   \
814 f83c6e10 Mark McLoughlin
        .help = "identifier for monitor commands", \
815 f83c6e10 Mark McLoughlin
     }
816 f83c6e10 Mark McLoughlin
817 6d952ebe Mark McLoughlin
typedef int (*net_client_init_func)(QemuOpts *opts,
818 6d952ebe Mark McLoughlin
                                    Monitor *mon,
819 f6b134ac Mark McLoughlin
                                    const char *name,
820 f6b134ac Mark McLoughlin
                                    VLANState *vlan);
821 f83c6e10 Mark McLoughlin
822 f83c6e10 Mark McLoughlin
/* magic number, but compiler will warn if too small */
823 f83c6e10 Mark McLoughlin
#define NET_MAX_DESC 20
824 f83c6e10 Mark McLoughlin
825 238431a9 Blue Swirl
static const struct {
826 f83c6e10 Mark McLoughlin
    const char *type;
827 f83c6e10 Mark McLoughlin
    net_client_init_func init;
828 f83c6e10 Mark McLoughlin
    QemuOptDesc desc[NET_MAX_DESC];
829 6f7b3b1b Jan Kiszka
} net_client_types[NET_CLIENT_TYPE_MAX] = {
830 6f7b3b1b Jan Kiszka
    [NET_CLIENT_TYPE_NONE] = {
831 f83c6e10 Mark McLoughlin
        .type = "none",
832 f83c6e10 Mark McLoughlin
        .desc = {
833 f83c6e10 Mark McLoughlin
            NET_COMMON_PARAMS_DESC,
834 f83c6e10 Mark McLoughlin
            { /* end of list */ }
835 f83c6e10 Mark McLoughlin
        },
836 6f7b3b1b Jan Kiszka
    },
837 6f7b3b1b Jan Kiszka
    [NET_CLIENT_TYPE_NIC] = {
838 f83c6e10 Mark McLoughlin
        .type = "nic",
839 f83c6e10 Mark McLoughlin
        .init = net_init_nic,
840 f83c6e10 Mark McLoughlin
        .desc = {
841 f83c6e10 Mark McLoughlin
            NET_COMMON_PARAMS_DESC,
842 f83c6e10 Mark McLoughlin
            {
843 5869c4d5 Mark McLoughlin
                .name = "netdev",
844 5869c4d5 Mark McLoughlin
                .type = QEMU_OPT_STRING,
845 5869c4d5 Mark McLoughlin
                .help = "id of -netdev to connect to",
846 5869c4d5 Mark McLoughlin
            },
847 5869c4d5 Mark McLoughlin
            {
848 f83c6e10 Mark McLoughlin
                .name = "macaddr",
849 f83c6e10 Mark McLoughlin
                .type = QEMU_OPT_STRING,
850 f83c6e10 Mark McLoughlin
                .help = "MAC address",
851 f83c6e10 Mark McLoughlin
            }, {
852 f83c6e10 Mark McLoughlin
                .name = "model",
853 f83c6e10 Mark McLoughlin
                .type = QEMU_OPT_STRING,
854 f83c6e10 Mark McLoughlin
                .help = "device model (e1000, rtl8139, virtio etc.)",
855 f83c6e10 Mark McLoughlin
            }, {
856 f83c6e10 Mark McLoughlin
                .name = "addr",
857 f83c6e10 Mark McLoughlin
                .type = QEMU_OPT_STRING,
858 f83c6e10 Mark McLoughlin
                .help = "PCI device address",
859 f83c6e10 Mark McLoughlin
            }, {
860 f83c6e10 Mark McLoughlin
                .name = "vectors",
861 f83c6e10 Mark McLoughlin
                .type = QEMU_OPT_NUMBER,
862 f83c6e10 Mark McLoughlin
                .help = "number of MSI-x vectors, 0 to disable MSI-X",
863 f83c6e10 Mark McLoughlin
            },
864 f83c6e10 Mark McLoughlin
            { /* end of list */ }
865 f83c6e10 Mark McLoughlin
        },
866 6f7b3b1b Jan Kiszka
    },
867 ec302ffd Mark McLoughlin
#ifdef CONFIG_SLIRP
868 6f7b3b1b Jan Kiszka
    [NET_CLIENT_TYPE_USER] = {
869 ec302ffd Mark McLoughlin
        .type = "user",
870 ec302ffd Mark McLoughlin
        .init = net_init_slirp,
871 ec302ffd Mark McLoughlin
        .desc = {
872 ec302ffd Mark McLoughlin
            NET_COMMON_PARAMS_DESC,
873 ec302ffd Mark McLoughlin
            {
874 ec302ffd Mark McLoughlin
                .name = "hostname",
875 ec302ffd Mark McLoughlin
                .type = QEMU_OPT_STRING,
876 ec302ffd Mark McLoughlin
                .help = "client hostname reported by the builtin DHCP server",
877 ec302ffd Mark McLoughlin
            }, {
878 ec302ffd Mark McLoughlin
                .name = "restrict",
879 ec302ffd Mark McLoughlin
                .type = QEMU_OPT_STRING,
880 ec302ffd Mark McLoughlin
                .help = "isolate the guest from the host (y|yes|n|no)",
881 ec302ffd Mark McLoughlin
            }, {
882 ec302ffd Mark McLoughlin
                .name = "ip",
883 ec302ffd Mark McLoughlin
                .type = QEMU_OPT_STRING,
884 ec302ffd Mark McLoughlin
                .help = "legacy parameter, use net= instead",
885 ec302ffd Mark McLoughlin
            }, {
886 ec302ffd Mark McLoughlin
                .name = "net",
887 ec302ffd Mark McLoughlin
                .type = QEMU_OPT_STRING,
888 ec302ffd Mark McLoughlin
                .help = "IP address and optional netmask",
889 ec302ffd Mark McLoughlin
            }, {
890 ec302ffd Mark McLoughlin
                .name = "host",
891 ec302ffd Mark McLoughlin
                .type = QEMU_OPT_STRING,
892 ec302ffd Mark McLoughlin
                .help = "guest-visible address of the host",
893 ec302ffd Mark McLoughlin
            }, {
894 ec302ffd Mark McLoughlin
                .name = "tftp",
895 ec302ffd Mark McLoughlin
                .type = QEMU_OPT_STRING,
896 ec302ffd Mark McLoughlin
                .help = "root directory of the built-in TFTP server",
897 ec302ffd Mark McLoughlin
            }, {
898 ec302ffd Mark McLoughlin
                .name = "bootfile",
899 ec302ffd Mark McLoughlin
                .type = QEMU_OPT_STRING,
900 ec302ffd Mark McLoughlin
                .help = "BOOTP filename, for use with tftp=",
901 ec302ffd Mark McLoughlin
            }, {
902 ec302ffd Mark McLoughlin
                .name = "dhcpstart",
903 ec302ffd Mark McLoughlin
                .type = QEMU_OPT_STRING,
904 ec302ffd Mark McLoughlin
                .help = "the first of the 16 IPs the built-in DHCP server can assign",
905 ec302ffd Mark McLoughlin
            }, {
906 ec302ffd Mark McLoughlin
                .name = "dns",
907 ec302ffd Mark McLoughlin
                .type = QEMU_OPT_STRING,
908 ec302ffd Mark McLoughlin
                .help = "guest-visible address of the virtual nameserver",
909 ec302ffd Mark McLoughlin
            }, {
910 ec302ffd Mark McLoughlin
                .name = "smb",
911 ec302ffd Mark McLoughlin
                .type = QEMU_OPT_STRING,
912 ec302ffd Mark McLoughlin
                .help = "root directory of the built-in SMB server",
913 ec302ffd Mark McLoughlin
            }, {
914 ec302ffd Mark McLoughlin
                .name = "smbserver",
915 ec302ffd Mark McLoughlin
                .type = QEMU_OPT_STRING,
916 ec302ffd Mark McLoughlin
                .help = "IP address of the built-in SMB server",
917 ec302ffd Mark McLoughlin
            }, {
918 ec302ffd Mark McLoughlin
                .name = "hostfwd",
919 ec302ffd Mark McLoughlin
                .type = QEMU_OPT_STRING,
920 ec302ffd Mark McLoughlin
                .help = "guest port number to forward incoming TCP or UDP connections",
921 ec302ffd Mark McLoughlin
            }, {
922 ec302ffd Mark McLoughlin
                .name = "guestfwd",
923 ec302ffd Mark McLoughlin
                .type = QEMU_OPT_STRING,
924 ec302ffd Mark McLoughlin
                .help = "IP address and port to forward guest TCP connections",
925 ec302ffd Mark McLoughlin
            },
926 ec302ffd Mark McLoughlin
            { /* end of list */ }
927 ec302ffd Mark McLoughlin
        },
928 6f7b3b1b Jan Kiszka
    },
929 ec302ffd Mark McLoughlin
#endif
930 6f7b3b1b Jan Kiszka
    [NET_CLIENT_TYPE_TAP] = {
931 8a1c5235 Mark McLoughlin
        .type = "tap",
932 a8ed73f7 Mark McLoughlin
        .init = net_init_tap,
933 8a1c5235 Mark McLoughlin
        .desc = {
934 8a1c5235 Mark McLoughlin
            NET_COMMON_PARAMS_DESC,
935 8a1c5235 Mark McLoughlin
            {
936 8a1c5235 Mark McLoughlin
                .name = "ifname",
937 8a1c5235 Mark McLoughlin
                .type = QEMU_OPT_STRING,
938 8a1c5235 Mark McLoughlin
                .help = "interface name",
939 8a1c5235 Mark McLoughlin
            },
940 a8ed73f7 Mark McLoughlin
#ifndef _WIN32
941 8a1c5235 Mark McLoughlin
            {
942 8a1c5235 Mark McLoughlin
                .name = "fd",
943 8a1c5235 Mark McLoughlin
                .type = QEMU_OPT_STRING,
944 8a1c5235 Mark McLoughlin
                .help = "file descriptor of an already opened tap",
945 8a1c5235 Mark McLoughlin
            }, {
946 8a1c5235 Mark McLoughlin
                .name = "script",
947 8a1c5235 Mark McLoughlin
                .type = QEMU_OPT_STRING,
948 8a1c5235 Mark McLoughlin
                .help = "script to initialize the interface",
949 8a1c5235 Mark McLoughlin
            }, {
950 8a1c5235 Mark McLoughlin
                .name = "downscript",
951 8a1c5235 Mark McLoughlin
                .type = QEMU_OPT_STRING,
952 8a1c5235 Mark McLoughlin
                .help = "script to shut down the interface",
953 8a1c5235 Mark McLoughlin
            }, {
954 8a1c5235 Mark McLoughlin
                .name = "sndbuf",
955 8a1c5235 Mark McLoughlin
                .type = QEMU_OPT_SIZE,
956 8a1c5235 Mark McLoughlin
                .help = "send buffer limit"
957 baf74c95 Mark McLoughlin
            }, {
958 baf74c95 Mark McLoughlin
                .name = "vnet_hdr",
959 baf74c95 Mark McLoughlin
                .type = QEMU_OPT_BOOL,
960 baf74c95 Mark McLoughlin
                .help = "enable the IFF_VNET_HDR flag on the tap interface"
961 82b0d80e Michael S. Tsirkin
            }, {
962 82b0d80e Michael S. Tsirkin
                .name = "vhost",
963 82b0d80e Michael S. Tsirkin
                .type = QEMU_OPT_BOOL,
964 82b0d80e Michael S. Tsirkin
                .help = "enable vhost-net network accelerator",
965 82b0d80e Michael S. Tsirkin
            }, {
966 82b0d80e Michael S. Tsirkin
                .name = "vhostfd",
967 82b0d80e Michael S. Tsirkin
                .type = QEMU_OPT_STRING,
968 82b0d80e Michael S. Tsirkin
                .help = "file descriptor of an already opened vhost net device",
969 96c94b29 Jason Wang
            }, {
970 96c94b29 Jason Wang
                .name = "vhostforce",
971 96c94b29 Jason Wang
                .type = QEMU_OPT_BOOL,
972 96c94b29 Jason Wang
                .help = "force vhost on for non-MSIX virtio guests",
973 96c94b29 Jason Wang
        },
974 a8ed73f7 Mark McLoughlin
#endif /* _WIN32 */
975 8a1c5235 Mark McLoughlin
            { /* end of list */ }
976 8a1c5235 Mark McLoughlin
        },
977 6f7b3b1b Jan Kiszka
    },
978 6f7b3b1b Jan Kiszka
    [NET_CLIENT_TYPE_SOCKET] = {
979 88ce16ca Mark McLoughlin
        .type = "socket",
980 88ce16ca Mark McLoughlin
        .init = net_init_socket,
981 88ce16ca Mark McLoughlin
        .desc = {
982 88ce16ca Mark McLoughlin
            NET_COMMON_PARAMS_DESC,
983 88ce16ca Mark McLoughlin
            {
984 88ce16ca Mark McLoughlin
                .name = "fd",
985 88ce16ca Mark McLoughlin
                .type = QEMU_OPT_STRING,
986 88ce16ca Mark McLoughlin
                .help = "file descriptor of an already opened socket",
987 88ce16ca Mark McLoughlin
            }, {
988 88ce16ca Mark McLoughlin
                .name = "listen",
989 88ce16ca Mark McLoughlin
                .type = QEMU_OPT_STRING,
990 88ce16ca Mark McLoughlin
                .help = "port number, and optional hostname, to listen on",
991 88ce16ca Mark McLoughlin
            }, {
992 88ce16ca Mark McLoughlin
                .name = "connect",
993 88ce16ca Mark McLoughlin
                .type = QEMU_OPT_STRING,
994 88ce16ca Mark McLoughlin
                .help = "port number, and optional hostname, to connect to",
995 88ce16ca Mark McLoughlin
            }, {
996 88ce16ca Mark McLoughlin
                .name = "mcast",
997 88ce16ca Mark McLoughlin
                .type = QEMU_OPT_STRING,
998 88ce16ca Mark McLoughlin
                .help = "UDP multicast address and port number",
999 3a75e74c Mike Ryan
            }, {
1000 3a75e74c Mike Ryan
                .name = "localaddr",
1001 3a75e74c Mike Ryan
                .type = QEMU_OPT_STRING,
1002 3a75e74c Mike Ryan
                .help = "source address for multicast packets",
1003 88ce16ca Mark McLoughlin
            },
1004 88ce16ca Mark McLoughlin
            { /* end of list */ }
1005 88ce16ca Mark McLoughlin
        },
1006 6f7b3b1b Jan Kiszka
    },
1007 dd51058d Mark McLoughlin
#ifdef CONFIG_VDE
1008 6f7b3b1b Jan Kiszka
    [NET_CLIENT_TYPE_VDE] = {
1009 dd51058d Mark McLoughlin
        .type = "vde",
1010 dd51058d Mark McLoughlin
        .init = net_init_vde,
1011 dd51058d Mark McLoughlin
        .desc = {
1012 dd51058d Mark McLoughlin
            NET_COMMON_PARAMS_DESC,
1013 dd51058d Mark McLoughlin
            {
1014 dd51058d Mark McLoughlin
                .name = "sock",
1015 dd51058d Mark McLoughlin
                .type = QEMU_OPT_STRING,
1016 dd51058d Mark McLoughlin
                .help = "socket path",
1017 dd51058d Mark McLoughlin
            }, {
1018 dd51058d Mark McLoughlin
                .name = "port",
1019 dd51058d Mark McLoughlin
                .type = QEMU_OPT_NUMBER,
1020 dd51058d Mark McLoughlin
                .help = "port number",
1021 dd51058d Mark McLoughlin
            }, {
1022 dd51058d Mark McLoughlin
                .name = "group",
1023 dd51058d Mark McLoughlin
                .type = QEMU_OPT_STRING,
1024 dd51058d Mark McLoughlin
                .help = "group owner of socket",
1025 dd51058d Mark McLoughlin
            }, {
1026 dd51058d Mark McLoughlin
                .name = "mode",
1027 dd51058d Mark McLoughlin
                .type = QEMU_OPT_NUMBER,
1028 dd51058d Mark McLoughlin
                .help = "permissions for socket",
1029 dd51058d Mark McLoughlin
            },
1030 dd51058d Mark McLoughlin
            { /* end of list */ }
1031 dd51058d Mark McLoughlin
        },
1032 6f7b3b1b Jan Kiszka
    },
1033 dd51058d Mark McLoughlin
#endif
1034 6f7b3b1b Jan Kiszka
    [NET_CLIENT_TYPE_DUMP] = {
1035 ed2955c2 Mark McLoughlin
        .type = "dump",
1036 ed2955c2 Mark McLoughlin
        .init = net_init_dump,
1037 ed2955c2 Mark McLoughlin
        .desc = {
1038 ed2955c2 Mark McLoughlin
            NET_COMMON_PARAMS_DESC,
1039 ed2955c2 Mark McLoughlin
            {
1040 ed2955c2 Mark McLoughlin
                .name = "len",
1041 ed2955c2 Mark McLoughlin
                .type = QEMU_OPT_SIZE,
1042 ed2955c2 Mark McLoughlin
                .help = "per-packet size limit (64k default)",
1043 ed2955c2 Mark McLoughlin
            }, {
1044 ed2955c2 Mark McLoughlin
                .name = "file",
1045 ed2955c2 Mark McLoughlin
                .type = QEMU_OPT_STRING,
1046 ed2955c2 Mark McLoughlin
                .help = "dump file path (default is qemu-vlan0.pcap)",
1047 ed2955c2 Mark McLoughlin
            },
1048 ed2955c2 Mark McLoughlin
            { /* end of list */ }
1049 ed2955c2 Mark McLoughlin
        },
1050 f83c6e10 Mark McLoughlin
    },
1051 f83c6e10 Mark McLoughlin
};
1052 f83c6e10 Mark McLoughlin
1053 f6b134ac Mark McLoughlin
int net_client_init(Monitor *mon, QemuOpts *opts, int is_netdev)
1054 f83c6e10 Mark McLoughlin
{
1055 6d952ebe Mark McLoughlin
    const char *name;
1056 f83c6e10 Mark McLoughlin
    const char *type;
1057 f83c6e10 Mark McLoughlin
    int i;
1058 f83c6e10 Mark McLoughlin
1059 f83c6e10 Mark McLoughlin
    type = qemu_opt_get(opts, "type");
1060 5294e2c7 Markus Armbruster
    if (!type) {
1061 5294e2c7 Markus Armbruster
        qerror_report(QERR_MISSING_PARAMETER, "type");
1062 5294e2c7 Markus Armbruster
        return -1;
1063 5294e2c7 Markus Armbruster
    }
1064 f83c6e10 Mark McLoughlin
1065 5294e2c7 Markus Armbruster
    if (is_netdev) {
1066 f6b134ac Mark McLoughlin
        if (strcmp(type, "tap") != 0 &&
1067 f6b134ac Mark McLoughlin
#ifdef CONFIG_SLIRP
1068 f6b134ac Mark McLoughlin
            strcmp(type, "user") != 0 &&
1069 f6b134ac Mark McLoughlin
#endif
1070 f6b134ac Mark McLoughlin
#ifdef CONFIG_VDE
1071 f6b134ac Mark McLoughlin
            strcmp(type, "vde") != 0 &&
1072 f6b134ac Mark McLoughlin
#endif
1073 f6b134ac Mark McLoughlin
            strcmp(type, "socket") != 0) {
1074 5294e2c7 Markus Armbruster
            qerror_report(QERR_INVALID_PARAMETER_VALUE, "type",
1075 5294e2c7 Markus Armbruster
                          "a netdev backend type");
1076 f6b134ac Mark McLoughlin
            return -1;
1077 f6b134ac Mark McLoughlin
        }
1078 f6b134ac Mark McLoughlin
1079 f6b134ac Mark McLoughlin
        if (qemu_opt_get(opts, "vlan")) {
1080 5294e2c7 Markus Armbruster
            qerror_report(QERR_INVALID_PARAMETER, "vlan");
1081 f6b134ac Mark McLoughlin
            return -1;
1082 f6b134ac Mark McLoughlin
        }
1083 f6b134ac Mark McLoughlin
        if (qemu_opt_get(opts, "name")) {
1084 5294e2c7 Markus Armbruster
            qerror_report(QERR_INVALID_PARAMETER, "name");
1085 f6b134ac Mark McLoughlin
            return -1;
1086 f6b134ac Mark McLoughlin
        }
1087 f6b134ac Mark McLoughlin
        if (!qemu_opts_id(opts)) {
1088 5294e2c7 Markus Armbruster
            qerror_report(QERR_MISSING_PARAMETER, "id");
1089 f6b134ac Mark McLoughlin
            return -1;
1090 f6b134ac Mark McLoughlin
        }
1091 f6b134ac Mark McLoughlin
    }
1092 f6b134ac Mark McLoughlin
1093 6d952ebe Mark McLoughlin
    name = qemu_opts_id(opts);
1094 6d952ebe Mark McLoughlin
    if (!name) {
1095 6d952ebe Mark McLoughlin
        name = qemu_opt_get(opts, "name");
1096 6d952ebe Mark McLoughlin
    }
1097 6d952ebe Mark McLoughlin
1098 6f7b3b1b Jan Kiszka
    for (i = 0; i < NET_CLIENT_TYPE_MAX; i++) {
1099 6f7b3b1b Jan Kiszka
        if (net_client_types[i].type != NULL &&
1100 6f7b3b1b Jan Kiszka
            !strcmp(net_client_types[i].type, type)) {
1101 f6b134ac Mark McLoughlin
            VLANState *vlan = NULL;
1102 50e32ea8 Amit Shah
            int ret;
1103 f6b134ac Mark McLoughlin
1104 f83c6e10 Mark McLoughlin
            if (qemu_opts_validate(opts, &net_client_types[i].desc[0]) == -1) {
1105 f83c6e10 Mark McLoughlin
                return -1;
1106 f83c6e10 Mark McLoughlin
            }
1107 f83c6e10 Mark McLoughlin
1108 5869c4d5 Mark McLoughlin
            /* Do not add to a vlan if it's a -netdev or a nic with a
1109 5869c4d5 Mark McLoughlin
             * netdev= parameter. */
1110 5869c4d5 Mark McLoughlin
            if (!(is_netdev ||
1111 5869c4d5 Mark McLoughlin
                  (strcmp(type, "nic") == 0 && qemu_opt_get(opts, "netdev")))) {
1112 f6b134ac Mark McLoughlin
                vlan = qemu_find_vlan(qemu_opt_get_number(opts, "vlan", 0), 1);
1113 f6b134ac Mark McLoughlin
            }
1114 f6b134ac Mark McLoughlin
1115 03c71553 Amit Shah
            ret = 0;
1116 f83c6e10 Mark McLoughlin
            if (net_client_types[i].init) {
1117 50e32ea8 Amit Shah
                ret = net_client_types[i].init(opts, mon, name, vlan);
1118 50e32ea8 Amit Shah
                if (ret < 0) {
1119 5294e2c7 Markus Armbruster
                    /* TODO push error reporting into init() methods */
1120 5294e2c7 Markus Armbruster
                    qerror_report(QERR_DEVICE_INIT_FAILED, type);
1121 5294e2c7 Markus Armbruster
                    return -1;
1122 5294e2c7 Markus Armbruster
                }
1123 f83c6e10 Mark McLoughlin
            }
1124 50e32ea8 Amit Shah
            return ret;
1125 f83c6e10 Mark McLoughlin
        }
1126 f83c6e10 Mark McLoughlin
    }
1127 f83c6e10 Mark McLoughlin
1128 5294e2c7 Markus Armbruster
    qerror_report(QERR_INVALID_PARAMETER_VALUE, "type",
1129 5294e2c7 Markus Armbruster
                  "a network client type");
1130 f83c6e10 Mark McLoughlin
    return -1;
1131 f83c6e10 Mark McLoughlin
}
1132 f83c6e10 Mark McLoughlin
1133 6f338c34 aliguori
static int net_host_check_device(const char *device)
1134 6f338c34 aliguori
{
1135 6f338c34 aliguori
    int i;
1136 bb9ea79e aliguori
    const char *valid_param_list[] = { "tap", "socket", "dump"
1137 6f338c34 aliguori
#ifdef CONFIG_SLIRP
1138 6f338c34 aliguori
                                       ,"user"
1139 6f338c34 aliguori
#endif
1140 6f338c34 aliguori
#ifdef CONFIG_VDE
1141 6f338c34 aliguori
                                       ,"vde"
1142 6f338c34 aliguori
#endif
1143 6f338c34 aliguori
    };
1144 6f338c34 aliguori
    for (i = 0; i < sizeof(valid_param_list) / sizeof(char *); i++) {
1145 6f338c34 aliguori
        if (!strncmp(valid_param_list[i], device,
1146 6f338c34 aliguori
                     strlen(valid_param_list[i])))
1147 6f338c34 aliguori
            return 1;
1148 6f338c34 aliguori
    }
1149 6f338c34 aliguori
1150 6f338c34 aliguori
    return 0;
1151 6f338c34 aliguori
}
1152 6f338c34 aliguori
1153 f18c16de Luiz Capitulino
void net_host_device_add(Monitor *mon, const QDict *qdict)
1154 6f338c34 aliguori
{
1155 f18c16de Luiz Capitulino
    const char *device = qdict_get_str(qdict, "device");
1156 7f1c9d20 Mark McLoughlin
    const char *opts_str = qdict_get_try_str(qdict, "opts");
1157 7f1c9d20 Mark McLoughlin
    QemuOpts *opts;
1158 f18c16de Luiz Capitulino
1159 6f338c34 aliguori
    if (!net_host_check_device(device)) {
1160 376253ec aliguori
        monitor_printf(mon, "invalid host network device %s\n", device);
1161 6f338c34 aliguori
        return;
1162 6f338c34 aliguori
    }
1163 7f1c9d20 Mark McLoughlin
1164 3329f07b Gerd Hoffmann
    opts = qemu_opts_parse(qemu_find_opts("net"), opts_str ? opts_str : "", 0);
1165 7f1c9d20 Mark McLoughlin
    if (!opts) {
1166 7f1c9d20 Mark McLoughlin
        return;
1167 7f1c9d20 Mark McLoughlin
    }
1168 7f1c9d20 Mark McLoughlin
1169 7f1c9d20 Mark McLoughlin
    qemu_opt_set(opts, "type", device);
1170 7f1c9d20 Mark McLoughlin
1171 f6b134ac Mark McLoughlin
    if (net_client_init(mon, opts, 0) < 0) {
1172 5c8be678 aliguori
        monitor_printf(mon, "adding host network device %s failed\n", device);
1173 5c8be678 aliguori
    }
1174 6f338c34 aliguori
}
1175 6f338c34 aliguori
1176 f18c16de Luiz Capitulino
void net_host_device_remove(Monitor *mon, const QDict *qdict)
1177 6f338c34 aliguori
{
1178 6f338c34 aliguori
    VLANClientState *vc;
1179 f18c16de Luiz Capitulino
    int vlan_id = qdict_get_int(qdict, "vlan_id");
1180 f18c16de Luiz Capitulino
    const char *device = qdict_get_str(qdict, "device");
1181 6f338c34 aliguori
1182 1a609520 Jan Kiszka
    vc = qemu_find_vlan_client_by_name(mon, vlan_id, device);
1183 6f338c34 aliguori
    if (!vc) {
1184 6f338c34 aliguori
        return;
1185 6f338c34 aliguori
    }
1186 e8f1f9db aliguori
    if (!net_host_check_device(vc->model)) {
1187 e8f1f9db aliguori
        monitor_printf(mon, "invalid host network device %s\n", device);
1188 e8f1f9db aliguori
        return;
1189 e8f1f9db aliguori
    }
1190 6f338c34 aliguori
    qemu_del_vlan_client(vc);
1191 6f338c34 aliguori
}
1192 6f338c34 aliguori
1193 ae82d324 Markus Armbruster
int do_netdev_add(Monitor *mon, const QDict *qdict, QObject **ret_data)
1194 ae82d324 Markus Armbruster
{
1195 ae82d324 Markus Armbruster
    QemuOpts *opts;
1196 ae82d324 Markus Armbruster
    int res;
1197 ae82d324 Markus Armbruster
1198 3329f07b Gerd Hoffmann
    opts = qemu_opts_from_qdict(qemu_find_opts("netdev"), qdict);
1199 ae82d324 Markus Armbruster
    if (!opts) {
1200 ae82d324 Markus Armbruster
        return -1;
1201 ae82d324 Markus Armbruster
    }
1202 ae82d324 Markus Armbruster
1203 ae82d324 Markus Armbruster
    res = net_client_init(mon, opts, 1);
1204 410cbafe Yoshiaki Tamura
    if (res < 0) {
1205 410cbafe Yoshiaki Tamura
        qemu_opts_del(opts);
1206 410cbafe Yoshiaki Tamura
    }
1207 410cbafe Yoshiaki Tamura
1208 ae82d324 Markus Armbruster
    return res;
1209 ae82d324 Markus Armbruster
}
1210 ae82d324 Markus Armbruster
1211 ae82d324 Markus Armbruster
int do_netdev_del(Monitor *mon, const QDict *qdict, QObject **ret_data)
1212 ae82d324 Markus Armbruster
{
1213 ae82d324 Markus Armbruster
    const char *id = qdict_get_str(qdict, "id");
1214 ae82d324 Markus Armbruster
    VLANClientState *vc;
1215 ae82d324 Markus Armbruster
1216 ae82d324 Markus Armbruster
    vc = qemu_find_netdev(id);
1217 85dde9a9 Markus Armbruster
    if (!vc) {
1218 ae82d324 Markus Armbruster
        qerror_report(QERR_DEVICE_NOT_FOUND, id);
1219 ae82d324 Markus Armbruster
        return -1;
1220 ae82d324 Markus Armbruster
    }
1221 ae82d324 Markus Armbruster
    qemu_del_vlan_client(vc);
1222 3329f07b Gerd Hoffmann
    qemu_opts_del(qemu_opts_find(qemu_find_opts("netdev"), id));
1223 ae82d324 Markus Armbruster
    return 0;
1224 ae82d324 Markus Armbruster
}
1225 ae82d324 Markus Armbruster
1226 44e798d3 Jan Kiszka
static void print_net_client(Monitor *mon, VLANClientState *vc)
1227 44e798d3 Jan Kiszka
{
1228 44e798d3 Jan Kiszka
    monitor_printf(mon, "%s: type=%s,%s\n", vc->name,
1229 44e798d3 Jan Kiszka
                   net_client_types[vc->info->type].type, vc->info_str);
1230 44e798d3 Jan Kiszka
}
1231 44e798d3 Jan Kiszka
1232 376253ec aliguori
void do_info_network(Monitor *mon)
1233 63a01ef8 aliguori
{
1234 63a01ef8 aliguori
    VLANState *vlan;
1235 19061e63 Jan Kiszka
    VLANClientState *vc, *peer;
1236 19061e63 Jan Kiszka
    net_client_type type;
1237 63a01ef8 aliguori
1238 5610c3aa Mark McLoughlin
    QTAILQ_FOREACH(vlan, &vlans, next) {
1239 376253ec aliguori
        monitor_printf(mon, "VLAN %d devices:\n", vlan->id);
1240 5610c3aa Mark McLoughlin
1241 5610c3aa Mark McLoughlin
        QTAILQ_FOREACH(vc, &vlan->clients, next) {
1242 44e798d3 Jan Kiszka
            monitor_printf(mon, "  ");
1243 44e798d3 Jan Kiszka
            print_net_client(mon, vc);
1244 5610c3aa Mark McLoughlin
        }
1245 63a01ef8 aliguori
    }
1246 a0104e0e Markus Armbruster
    monitor_printf(mon, "Devices not on any VLAN:\n");
1247 a0104e0e Markus Armbruster
    QTAILQ_FOREACH(vc, &non_vlan_clients, next) {
1248 19061e63 Jan Kiszka
        peer = vc->peer;
1249 19061e63 Jan Kiszka
        type = vc->info->type;
1250 19061e63 Jan Kiszka
        if (!peer || type == NET_CLIENT_TYPE_NIC) {
1251 44e798d3 Jan Kiszka
            monitor_printf(mon, "  ");
1252 44e798d3 Jan Kiszka
            print_net_client(mon, vc);
1253 19061e63 Jan Kiszka
        } /* else it's a netdev connected to a NIC, printed with the NIC */
1254 19061e63 Jan Kiszka
        if (peer && type == NET_CLIENT_TYPE_NIC) {
1255 44e798d3 Jan Kiszka
            monitor_printf(mon, "   \\ ");
1256 44e798d3 Jan Kiszka
            print_net_client(mon, peer);
1257 a0104e0e Markus Armbruster
        }
1258 a0104e0e Markus Armbruster
    }
1259 63a01ef8 aliguori
}
1260 63a01ef8 aliguori
1261 5369e3c0 Markus Armbruster
int do_set_link(Monitor *mon, const QDict *qdict, QObject **ret_data)
1262 436e5e53 aliguori
{
1263 436e5e53 aliguori
    VLANState *vlan;
1264 436e5e53 aliguori
    VLANClientState *vc = NULL;
1265 f18c16de Luiz Capitulino
    const char *name = qdict_get_str(qdict, "name");
1266 c9b26a4c Markus Armbruster
    int up = qdict_get_bool(qdict, "up");
1267 436e5e53 aliguori
1268 5610c3aa Mark McLoughlin
    QTAILQ_FOREACH(vlan, &vlans, next) {
1269 5610c3aa Mark McLoughlin
        QTAILQ_FOREACH(vc, &vlan->clients, next) {
1270 5610c3aa Mark McLoughlin
            if (strcmp(vc->name, name) == 0) {
1271 dd5de373 edgar_igl
                goto done;
1272 5610c3aa Mark McLoughlin
            }
1273 5610c3aa Mark McLoughlin
        }
1274 5610c3aa Mark McLoughlin
    }
1275 85dde9a9 Markus Armbruster
    QTAILQ_FOREACH(vc, &non_vlan_clients, next) {
1276 85dde9a9 Markus Armbruster
        if (!strcmp(vc->name, name)) {
1277 85dde9a9 Markus Armbruster
            goto done;
1278 85dde9a9 Markus Armbruster
        }
1279 85dde9a9 Markus Armbruster
    }
1280 dd5de373 edgar_igl
done:
1281 436e5e53 aliguori
1282 436e5e53 aliguori
    if (!vc) {
1283 5369e3c0 Markus Armbruster
        qerror_report(QERR_DEVICE_NOT_FOUND, name);
1284 5369e3c0 Markus Armbruster
        return -1;
1285 436e5e53 aliguori
    }
1286 436e5e53 aliguori
1287 c9b26a4c Markus Armbruster
    vc->link_down = !up;
1288 436e5e53 aliguori
1289 665a3b07 Mark McLoughlin
    if (vc->info->link_status_changed) {
1290 665a3b07 Mark McLoughlin
        vc->info->link_status_changed(vc);
1291 665a3b07 Mark McLoughlin
    }
1292 ab1cbe1c Michael S. Tsirkin
1293 ab1cbe1c Michael S. Tsirkin
    /* Notify peer. Don't update peer link status: this makes it possible to
1294 ab1cbe1c Michael S. Tsirkin
     * disconnect from host network without notifying the guest.
1295 ab1cbe1c Michael S. Tsirkin
     * FIXME: is disconnected link status change operation useful?
1296 ab1cbe1c Michael S. Tsirkin
     *
1297 ab1cbe1c Michael S. Tsirkin
     * Current behaviour is compatible with qemu vlans where there could be
1298 ab1cbe1c Michael S. Tsirkin
     * multiple clients that can still communicate with each other in
1299 ab1cbe1c Michael S. Tsirkin
     * disconnected mode. For now maintain this compatibility. */
1300 ab1cbe1c Michael S. Tsirkin
    if (vc->peer && vc->peer->info->link_status_changed) {
1301 ab1cbe1c Michael S. Tsirkin
        vc->peer->info->link_status_changed(vc->peer);
1302 ab1cbe1c Michael S. Tsirkin
    }
1303 5369e3c0 Markus Armbruster
    return 0;
1304 436e5e53 aliguori
}
1305 436e5e53 aliguori
1306 63a01ef8 aliguori
void net_cleanup(void)
1307 63a01ef8 aliguori
{
1308 63a01ef8 aliguori
    VLANState *vlan;
1309 577c4af9 Mark McLoughlin
    VLANClientState *vc, *next_vc;
1310 63a01ef8 aliguori
1311 5610c3aa Mark McLoughlin
    QTAILQ_FOREACH(vlan, &vlans, next) {
1312 5610c3aa Mark McLoughlin
        QTAILQ_FOREACH_SAFE(vc, &vlan->clients, next, next_vc) {
1313 b946a153 aliguori
            qemu_del_vlan_client(vc);
1314 63a01ef8 aliguori
        }
1315 63a01ef8 aliguori
    }
1316 577c4af9 Mark McLoughlin
1317 577c4af9 Mark McLoughlin
    QTAILQ_FOREACH_SAFE(vc, &non_vlan_clients, next, next_vc) {
1318 577c4af9 Mark McLoughlin
        qemu_del_vlan_client(vc);
1319 577c4af9 Mark McLoughlin
    }
1320 63a01ef8 aliguori
}
1321 63a01ef8 aliguori
1322 668680f7 Markus Armbruster
void net_check_clients(void)
1323 63a01ef8 aliguori
{
1324 63a01ef8 aliguori
    VLANState *vlan;
1325 62112d18 Markus Armbruster
    VLANClientState *vc;
1326 48e2faf2 Peter Maydell
    int i;
1327 63a01ef8 aliguori
1328 641f6eae Peter Maydell
    /* Don't warn about the default network setup that you get if
1329 641f6eae Peter Maydell
     * no command line -net or -netdev options are specified. There
1330 641f6eae Peter Maydell
     * are two cases that we would otherwise complain about:
1331 641f6eae Peter Maydell
     * (1) board doesn't support a NIC but the implicit "-net nic"
1332 641f6eae Peter Maydell
     * requested one
1333 641f6eae Peter Maydell
     * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
1334 641f6eae Peter Maydell
     * sets up a nic that isn't connected to anything.
1335 641f6eae Peter Maydell
     */
1336 641f6eae Peter Maydell
    if (default_net) {
1337 641f6eae Peter Maydell
        return;
1338 641f6eae Peter Maydell
    }
1339 641f6eae Peter Maydell
1340 5610c3aa Mark McLoughlin
    QTAILQ_FOREACH(vlan, &vlans, next) {
1341 ac60cc18 Tristan Gingold
        int has_nic = 0, has_host_dev = 0;
1342 ac60cc18 Tristan Gingold
1343 62112d18 Markus Armbruster
        QTAILQ_FOREACH(vc, &vlan->clients, next) {
1344 62112d18 Markus Armbruster
            switch (vc->info->type) {
1345 62112d18 Markus Armbruster
            case NET_CLIENT_TYPE_NIC:
1346 62112d18 Markus Armbruster
                has_nic = 1;
1347 62112d18 Markus Armbruster
                break;
1348 6f7b3b1b Jan Kiszka
            case NET_CLIENT_TYPE_USER:
1349 62112d18 Markus Armbruster
            case NET_CLIENT_TYPE_TAP:
1350 62112d18 Markus Armbruster
            case NET_CLIENT_TYPE_SOCKET:
1351 62112d18 Markus Armbruster
            case NET_CLIENT_TYPE_VDE:
1352 62112d18 Markus Armbruster
                has_host_dev = 1;
1353 62112d18 Markus Armbruster
                break;
1354 62112d18 Markus Armbruster
            default: ;
1355 62112d18 Markus Armbruster
            }
1356 62112d18 Markus Armbruster
        }
1357 62112d18 Markus Armbruster
        if (has_host_dev && !has_nic)
1358 63a01ef8 aliguori
            fprintf(stderr, "Warning: vlan %d with no nics\n", vlan->id);
1359 62112d18 Markus Armbruster
        if (has_nic && !has_host_dev)
1360 63a01ef8 aliguori
            fprintf(stderr,
1361 63a01ef8 aliguori
                    "Warning: vlan %d is not connected to host network\n",
1362 63a01ef8 aliguori
                    vlan->id);
1363 63a01ef8 aliguori
    }
1364 efe32fdd Markus Armbruster
    QTAILQ_FOREACH(vc, &non_vlan_clients, next) {
1365 efe32fdd Markus Armbruster
        if (!vc->peer) {
1366 efe32fdd Markus Armbruster
            fprintf(stderr, "Warning: %s %s has no peer\n",
1367 efe32fdd Markus Armbruster
                    vc->info->type == NET_CLIENT_TYPE_NIC ? "nic" : "netdev",
1368 efe32fdd Markus Armbruster
                    vc->name);
1369 efe32fdd Markus Armbruster
        }
1370 efe32fdd Markus Armbruster
    }
1371 48e2faf2 Peter Maydell
1372 48e2faf2 Peter Maydell
    /* Check that all NICs requested via -net nic actually got created.
1373 48e2faf2 Peter Maydell
     * NICs created via -device don't need to be checked here because
1374 48e2faf2 Peter Maydell
     * they are always instantiated.
1375 48e2faf2 Peter Maydell
     */
1376 48e2faf2 Peter Maydell
    for (i = 0; i < MAX_NICS; i++) {
1377 48e2faf2 Peter Maydell
        NICInfo *nd = &nd_table[i];
1378 48e2faf2 Peter Maydell
        if (nd->used && !nd->instantiated) {
1379 48e2faf2 Peter Maydell
            fprintf(stderr, "Warning: requested NIC (%s, model %s) "
1380 48e2faf2 Peter Maydell
                    "was not created (not supported by this machine?)\n",
1381 48e2faf2 Peter Maydell
                    nd->name ? nd->name : "anonymous",
1382 48e2faf2 Peter Maydell
                    nd->model ? nd->model : "unspecified");
1383 48e2faf2 Peter Maydell
        }
1384 48e2faf2 Peter Maydell
    }
1385 63a01ef8 aliguori
}
1386 dc1c9fe8 Mark McLoughlin
1387 dc1c9fe8 Mark McLoughlin
static int net_init_client(QemuOpts *opts, void *dummy)
1388 dc1c9fe8 Mark McLoughlin
{
1389 c1671a08 Mark McLoughlin
    if (net_client_init(NULL, opts, 0) < 0)
1390 c1671a08 Mark McLoughlin
        return -1;
1391 c1671a08 Mark McLoughlin
    return 0;
1392 f6b134ac Mark McLoughlin
}
1393 f6b134ac Mark McLoughlin
1394 f6b134ac Mark McLoughlin
static int net_init_netdev(QemuOpts *opts, void *dummy)
1395 f6b134ac Mark McLoughlin
{
1396 f6b134ac Mark McLoughlin
    return net_client_init(NULL, opts, 1);
1397 dc1c9fe8 Mark McLoughlin
}
1398 dc1c9fe8 Mark McLoughlin
1399 dc1c9fe8 Mark McLoughlin
int net_init_clients(void)
1400 dc1c9fe8 Mark McLoughlin
{
1401 3329f07b Gerd Hoffmann
    QemuOptsList *net = qemu_find_opts("net");
1402 3329f07b Gerd Hoffmann
1403 cb4522cc Gerd Hoffmann
    if (default_net) {
1404 dc1c9fe8 Mark McLoughlin
        /* if no clients, we use a default config */
1405 3329f07b Gerd Hoffmann
        qemu_opts_set(net, NULL, "type", "nic");
1406 dc1c9fe8 Mark McLoughlin
#ifdef CONFIG_SLIRP
1407 3329f07b Gerd Hoffmann
        qemu_opts_set(net, NULL, "type", "user");
1408 dc1c9fe8 Mark McLoughlin
#endif
1409 dc1c9fe8 Mark McLoughlin
    }
1410 dc1c9fe8 Mark McLoughlin
1411 5610c3aa Mark McLoughlin
    QTAILQ_INIT(&vlans);
1412 577c4af9 Mark McLoughlin
    QTAILQ_INIT(&non_vlan_clients);
1413 5610c3aa Mark McLoughlin
1414 3329f07b Gerd Hoffmann
    if (qemu_opts_foreach(qemu_find_opts("netdev"), net_init_netdev, NULL, 1) == -1)
1415 f6b134ac Mark McLoughlin
        return -1;
1416 f6b134ac Mark McLoughlin
1417 3329f07b Gerd Hoffmann
    if (qemu_opts_foreach(net, net_init_client, NULL, 1) == -1) {
1418 dc1c9fe8 Mark McLoughlin
        return -1;
1419 dc1c9fe8 Mark McLoughlin
    }
1420 dc1c9fe8 Mark McLoughlin
1421 dc1c9fe8 Mark McLoughlin
    return 0;
1422 dc1c9fe8 Mark McLoughlin
}
1423 dc1c9fe8 Mark McLoughlin
1424 7f161aae Mark McLoughlin
int net_client_parse(QemuOptsList *opts_list, const char *optarg)
1425 dc1c9fe8 Mark McLoughlin
{
1426 a3a766e7 Juan Quintela
#if defined(CONFIG_SLIRP)
1427 68ac40d2 Mark McLoughlin
    int ret;
1428 68ac40d2 Mark McLoughlin
    if (net_slirp_parse_legacy(opts_list, optarg, &ret)) {
1429 dc1c9fe8 Mark McLoughlin
        return ret;
1430 dc1c9fe8 Mark McLoughlin
    }
1431 a3a766e7 Juan Quintela
#endif
1432 68ac40d2 Mark McLoughlin
1433 8212c64f Markus Armbruster
    if (!qemu_opts_parse(opts_list, optarg, 1)) {
1434 dc1c9fe8 Mark McLoughlin
        return -1;
1435 dc1c9fe8 Mark McLoughlin
    }
1436 dc1c9fe8 Mark McLoughlin
1437 cb4522cc Gerd Hoffmann
    default_net = 0;
1438 dc1c9fe8 Mark McLoughlin
    return 0;
1439 dc1c9fe8 Mark McLoughlin
}