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