]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/bhyve/net_backends.c
bhyve: add missing license identifiers in net_utils and net_backend
[FreeBSD/FreeBSD.git] / usr.sbin / bhyve / net_backends.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2019 Vincenzo Maffione <vmaffione@FreeBSD.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
19  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
20  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
21  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
22  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
24  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
25  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  * $FreeBSD$
28  */
29
30 /*
31  * This file implements multiple network backends (tap, netmap, ...),
32  * to be used by network frontends such as virtio-net and e1000.
33  * The API to access the backend (e.g. send/receive packets, negotiate
34  * features) is exported by net_backends.h.
35  */
36
37 #include <sys/types.h>          /* u_short etc */
38 #ifndef WITHOUT_CAPSICUM
39 #include <sys/capsicum.h>
40 #endif
41 #include <sys/cdefs.h>
42 #include <sys/ioctl.h>
43 #include <sys/mman.h>
44 #include <sys/uio.h>
45
46 #include <net/if.h>
47 #include <net/netmap.h>
48 #include <net/netmap_virt.h>
49 #define NETMAP_WITH_LIBS
50 #include <net/netmap_user.h>
51
52 #ifndef WITHOUT_CAPSICUM
53 #include <capsicum_helpers.h>
54 #endif
55 #include <err.h>
56 #include <errno.h>
57 #include <fcntl.h>
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <stdint.h>
61 #include <string.h>
62 #include <unistd.h>
63 #include <sysexits.h>
64 #include <assert.h>
65 #include <pthread.h>
66 #include <pthread_np.h>
67 #include <poll.h>
68 #include <assert.h>
69
70
71 #include "iov.h"
72 #include "mevent.h"
73 #include "net_backends.h"
74
75 #include <sys/linker_set.h>
76
77 /*
78  * Each network backend registers a set of function pointers that are
79  * used to implement the net backends API.
80  * This might need to be exposed if we implement backends in separate files.
81  */
82 struct net_backend {
83         const char *prefix;     /* prefix matching this backend */
84
85         /*
86          * Routines used to initialize and cleanup the resources needed
87          * by a backend. The cleanup function is used internally,
88          * and should not be called by the frontend.
89          */
90         int (*init)(struct net_backend *be, const char *devname,
91             net_be_rxeof_t cb, void *param);
92         void (*cleanup)(struct net_backend *be);
93
94         /*
95          * Called to serve a guest transmit request. The scatter-gather
96          * vector provided by the caller has 'iovcnt' elements and contains
97          * the packet to send.
98          */
99         ssize_t (*send)(struct net_backend *be, struct iovec *iov, int iovcnt);
100
101         /*
102          * Called to receive a packet from the backend. When the function
103          * returns a positive value 'len', the scatter-gather vector
104          * provided by the caller contains a packet with such length.
105          * The function returns 0 if the backend doesn't have a new packet to
106          * receive.
107          */
108         ssize_t (*recv)(struct net_backend *be, struct iovec *iov, int iovcnt);
109
110         /*
111          * Ask the backend for the virtio-net features it is able to
112          * support. Possible features are TSO, UFO and checksum offloading
113          * in both rx and tx direction and for both IPv4 and IPv6.
114          */
115         uint64_t (*get_cap)(struct net_backend *be);
116
117         /*
118          * Tell the backend to enable/disable the specified virtio-net
119          * features (capabilities).
120          */
121         int (*set_cap)(struct net_backend *be, uint64_t features,
122             unsigned int vnet_hdr_len);
123
124         struct pci_vtnet_softc *sc;
125         int fd;
126
127         /*
128          * Length of the virtio-net header used by the backend and the
129          * frontend, respectively. A zero value means that the header
130          * is not used.
131          */
132         unsigned int be_vnet_hdr_len;
133         unsigned int fe_vnet_hdr_len;
134
135         /* Size of backend-specific private data. */
136         size_t priv_size;
137
138         /* Room for backend-specific data. */
139         char opaque[0];
140 };
141
142 SET_DECLARE(net_backend_set, struct net_backend);
143
144 #define VNET_HDR_LEN    sizeof(struct virtio_net_rxhdr)
145
146 #define WPRINTF(params) printf params
147
148 /*
149  * The tap backend
150  */
151
152 struct tap_priv {
153         struct mevent *mevp;
154 };
155
156 static void
157 tap_cleanup(struct net_backend *be)
158 {
159         struct tap_priv *priv = (struct tap_priv *)be->opaque;
160
161         if (priv->mevp) {
162                 mevent_delete(priv->mevp);
163         }
164         if (be->fd != -1) {
165                 close(be->fd);
166                 be->fd = -1;
167         }
168 }
169
170 static int
171 tap_init(struct net_backend *be, const char *devname,
172          net_be_rxeof_t cb, void *param)
173 {
174         struct tap_priv *priv = (struct tap_priv *)be->opaque;
175         char tbuf[80];
176         int fd;
177         int opt = 1;
178 #ifndef WITHOUT_CAPSICUM
179         cap_rights_t rights;
180 #endif
181
182         if (cb == NULL) {
183                 WPRINTF(("TAP backend requires non-NULL callback\n"));
184                 return (-1);
185         }
186
187         strcpy(tbuf, "/dev/");
188         strlcat(tbuf, devname, sizeof(tbuf));
189
190         fd = open(tbuf, O_RDWR);
191         if (fd == -1) {
192                 WPRINTF(("open of tap device %s failed\n", tbuf));
193                 goto error;
194         }
195
196         /*
197          * Set non-blocking and register for read
198          * notifications with the event loop
199          */
200         if (ioctl(fd, FIONBIO, &opt) < 0) {
201                 WPRINTF(("tap device O_NONBLOCK failed\n"));
202                 goto error;
203         }
204
205 #ifndef WITHOUT_CAPSICUM
206         cap_rights_init(&rights, CAP_EVENT, CAP_READ, CAP_WRITE);
207         if (caph_rights_limit(fd, &rights) == -1)
208                 errx(EX_OSERR, "Unable to apply rights for sandbox");
209 #endif
210
211         priv->mevp = mevent_add(fd, EVF_READ, cb, param);
212         if (priv->mevp == NULL) {
213                 WPRINTF(("Could not register event\n"));
214                 goto error;
215         }
216
217         be->fd = fd;
218
219         return (0);
220
221 error:
222         tap_cleanup(be);
223         return (-1);
224 }
225
226 /*
227  * Called to send a buffer chain out to the tap device
228  */
229 static ssize_t
230 tap_send(struct net_backend *be, struct iovec *iov, int iovcnt)
231 {
232         return (writev(be->fd, iov, iovcnt));
233 }
234
235 static ssize_t
236 tap_recv(struct net_backend *be, struct iovec *iov, int iovcnt)
237 {
238         ssize_t ret;
239
240         /* Should never be called without a valid tap fd */
241         assert(be->fd != -1);
242
243         ret = readv(be->fd, iov, iovcnt);
244
245         if (ret < 0 && errno == EWOULDBLOCK) {
246                 return (0);
247         }
248
249         return (ret);
250 }
251
252 static uint64_t
253 tap_get_cap(struct net_backend *be)
254 {
255
256         return (0); /* no capabilities for now */
257 }
258
259 static int
260 tap_set_cap(struct net_backend *be, uint64_t features,
261                 unsigned vnet_hdr_len)
262 {
263
264         return ((features || vnet_hdr_len) ? -1 : 0);
265 }
266
267 static struct net_backend tap_backend = {
268         .prefix = "tap",
269         .priv_size = sizeof(struct tap_priv),
270         .init = tap_init,
271         .cleanup = tap_cleanup,
272         .send = tap_send,
273         .recv = tap_recv,
274         .get_cap = tap_get_cap,
275         .set_cap = tap_set_cap,
276 };
277
278 /* A clone of the tap backend, with a different prefix. */
279 static struct net_backend vmnet_backend = {
280         .prefix = "vmnet",
281         .priv_size = sizeof(struct tap_priv),
282         .init = tap_init,
283         .cleanup = tap_cleanup,
284         .send = tap_send,
285         .recv = tap_recv,
286         .get_cap = tap_get_cap,
287         .set_cap = tap_set_cap,
288 };
289
290 DATA_SET(net_backend_set, tap_backend);
291 DATA_SET(net_backend_set, vmnet_backend);
292
293 /*
294  * The netmap backend
295  */
296
297 /* The virtio-net features supported by netmap. */
298 #define NETMAP_FEATURES (VIRTIO_NET_F_CSUM | VIRTIO_NET_F_HOST_TSO4 | \
299                 VIRTIO_NET_F_HOST_TSO6 | VIRTIO_NET_F_HOST_UFO | \
300                 VIRTIO_NET_F_GUEST_CSUM | VIRTIO_NET_F_GUEST_TSO4 | \
301                 VIRTIO_NET_F_GUEST_TSO6 | VIRTIO_NET_F_GUEST_UFO)
302
303 struct netmap_priv {
304         char ifname[IFNAMSIZ];
305         struct nm_desc *nmd;
306         uint16_t memid;
307         struct netmap_ring *rx;
308         struct netmap_ring *tx;
309         struct mevent *mevp;
310         net_be_rxeof_t cb;
311         void *cb_param;
312 };
313
314 static void
315 nmreq_init(struct nmreq *req, char *ifname)
316 {
317
318         memset(req, 0, sizeof(*req));
319         strlcpy(req->nr_name, ifname, sizeof(req->nr_name));
320         req->nr_version = NETMAP_API;
321 }
322
323 static int
324 netmap_set_vnet_hdr_len(struct net_backend *be, int vnet_hdr_len)
325 {
326         int err;
327         struct nmreq req;
328         struct netmap_priv *priv = (struct netmap_priv *)be->opaque;
329
330         nmreq_init(&req, priv->ifname);
331         req.nr_cmd = NETMAP_BDG_VNET_HDR;
332         req.nr_arg1 = vnet_hdr_len;
333         err = ioctl(be->fd, NIOCREGIF, &req);
334         if (err) {
335                 WPRINTF(("Unable to set vnet header length %d\n",
336                                 vnet_hdr_len));
337                 return (err);
338         }
339
340         be->be_vnet_hdr_len = vnet_hdr_len;
341
342         return (0);
343 }
344
345 static int
346 netmap_has_vnet_hdr_len(struct net_backend *be, unsigned vnet_hdr_len)
347 {
348         int prev_hdr_len = be->be_vnet_hdr_len;
349         int ret;
350
351         if (vnet_hdr_len == prev_hdr_len) {
352                 return (1);
353         }
354
355         ret = netmap_set_vnet_hdr_len(be, vnet_hdr_len);
356         if (ret) {
357                 return (0);
358         }
359
360         netmap_set_vnet_hdr_len(be, prev_hdr_len);
361
362         return (1);
363 }
364
365 static uint64_t
366 netmap_get_cap(struct net_backend *be)
367 {
368
369         return (netmap_has_vnet_hdr_len(be, VNET_HDR_LEN) ?
370             NETMAP_FEATURES : 0);
371 }
372
373 static int
374 netmap_set_cap(struct net_backend *be, uint64_t features,
375                unsigned vnet_hdr_len)
376 {
377
378         return (netmap_set_vnet_hdr_len(be, vnet_hdr_len));
379 }
380
381 static int
382 netmap_init(struct net_backend *be, const char *devname,
383             net_be_rxeof_t cb, void *param)
384 {
385         struct netmap_priv *priv = (struct netmap_priv *)be->opaque;
386
387         strlcpy(priv->ifname, devname, sizeof(priv->ifname));
388         priv->ifname[sizeof(priv->ifname) - 1] = '\0';
389
390         priv->nmd = nm_open(priv->ifname, NULL, NETMAP_NO_TX_POLL, NULL);
391         if (priv->nmd == NULL) {
392                 WPRINTF(("Unable to nm_open(): interface '%s', errno (%s)\n",
393                         devname, strerror(errno)));
394                 free(priv);
395                 return (-1);
396         }
397
398         priv->memid = priv->nmd->req.nr_arg2;
399         priv->tx = NETMAP_TXRING(priv->nmd->nifp, 0);
400         priv->rx = NETMAP_RXRING(priv->nmd->nifp, 0);
401         priv->cb = cb;
402         priv->cb_param = param;
403         be->fd = priv->nmd->fd;
404
405         priv->mevp = mevent_add(be->fd, EVF_READ, cb, param);
406         if (priv->mevp == NULL) {
407                 WPRINTF(("Could not register event\n"));
408                 return (-1);
409         }
410
411         return (0);
412 }
413
414 static void
415 netmap_cleanup(struct net_backend *be)
416 {
417         struct netmap_priv *priv = (struct netmap_priv *)be->opaque;
418
419         if (priv->mevp) {
420                 mevent_delete(priv->mevp);
421         }
422         if (priv->nmd) {
423                 nm_close(priv->nmd);
424         }
425         be->fd = -1;
426 }
427
428 static ssize_t
429 netmap_send(struct net_backend *be, struct iovec *iov,
430             int iovcnt)
431 {
432         struct netmap_priv *priv = (struct netmap_priv *)be->opaque;
433         struct netmap_ring *ring;
434         ssize_t totlen = 0;
435         int nm_buf_size;
436         int nm_buf_len;
437         uint32_t head;
438         void *nm_buf;
439         int j;
440
441         ring = priv->tx;
442         head = ring->head;
443         if (head == ring->tail) {
444                 WPRINTF(("No space, drop %zu bytes\n", count_iov(iov, iovcnt)));
445                 goto txsync;
446         }
447         nm_buf = NETMAP_BUF(ring, ring->slot[head].buf_idx);
448         nm_buf_size = ring->nr_buf_size;
449         nm_buf_len = 0;
450
451         for (j = 0; j < iovcnt; j++) {
452                 int iov_frag_size = iov[j].iov_len;
453                 void *iov_frag_buf = iov[j].iov_base;
454
455                 totlen += iov_frag_size;
456
457                 /*
458                  * Split each iovec fragment over more netmap slots, if
459                  * necessary.
460                  */
461                 for (;;) {
462                         int copylen;
463
464                         copylen = iov_frag_size < nm_buf_size ? iov_frag_size : nm_buf_size;
465                         memcpy(nm_buf, iov_frag_buf, copylen);
466
467                         iov_frag_buf += copylen;
468                         iov_frag_size -= copylen;
469                         nm_buf += copylen;
470                         nm_buf_size -= copylen;
471                         nm_buf_len += copylen;
472
473                         if (iov_frag_size == 0) {
474                                 break;
475                         }
476
477                         ring->slot[head].len = nm_buf_len;
478                         ring->slot[head].flags = NS_MOREFRAG;
479                         head = nm_ring_next(ring, head);
480                         if (head == ring->tail) {
481                                 /*
482                                  * We ran out of netmap slots while
483                                  * splitting the iovec fragments.
484                                  */
485                                 WPRINTF(("No space, drop %zu bytes\n",
486                                    count_iov(iov, iovcnt)));
487                                 goto txsync;
488                         }
489                         nm_buf = NETMAP_BUF(ring, ring->slot[head].buf_idx);
490                         nm_buf_size = ring->nr_buf_size;
491                         nm_buf_len = 0;
492                 }
493         }
494
495         /* Complete the last slot, which must not have NS_MOREFRAG set. */
496         ring->slot[head].len = nm_buf_len;
497         ring->slot[head].flags = 0;
498         head = nm_ring_next(ring, head);
499
500         /* Now update ring->head and ring->cur. */
501         ring->head = ring->cur = head;
502 txsync:
503         ioctl(be->fd, NIOCTXSYNC, NULL);
504
505         return (totlen);
506 }
507
508 static ssize_t
509 netmap_recv(struct net_backend *be, struct iovec *iov, int iovcnt)
510 {
511         struct netmap_priv *priv = (struct netmap_priv *)be->opaque;
512         struct netmap_slot *slot = NULL;
513         struct netmap_ring *ring;
514         void *iov_frag_buf;
515         int iov_frag_size;
516         ssize_t totlen = 0;
517         uint32_t head;
518
519         assert(iovcnt);
520
521         ring = priv->rx;
522         head = ring->head;
523         iov_frag_buf = iov->iov_base;
524         iov_frag_size = iov->iov_len;
525
526         do {
527                 int nm_buf_len;
528                 void *nm_buf;
529
530                 if (head == ring->tail) {
531                         return (0);
532                 }
533
534                 slot = ring->slot + head;
535                 nm_buf = NETMAP_BUF(ring, slot->buf_idx);
536                 nm_buf_len = slot->len;
537
538                 for (;;) {
539                         int copylen = nm_buf_len < iov_frag_size ?
540                             nm_buf_len : iov_frag_size;
541
542                         memcpy(iov_frag_buf, nm_buf, copylen);
543                         nm_buf += copylen;
544                         nm_buf_len -= copylen;
545                         iov_frag_buf += copylen;
546                         iov_frag_size -= copylen;
547                         totlen += copylen;
548
549                         if (nm_buf_len == 0) {
550                                 break;
551                         }
552
553                         iov++;
554                         iovcnt--;
555                         if (iovcnt == 0) {
556                                 /* No space to receive. */
557                                 WPRINTF(("Short iov, drop %zd bytes\n",
558                                     totlen));
559                                 return (-ENOSPC);
560                         }
561                         iov_frag_buf = iov->iov_base;
562                         iov_frag_size = iov->iov_len;
563                 }
564
565                 head = nm_ring_next(ring, head);
566
567         } while (slot->flags & NS_MOREFRAG);
568
569         /* Release slots to netmap. */
570         ring->head = ring->cur = head;
571
572         return (totlen);
573 }
574
575 static struct net_backend netmap_backend = {
576         .prefix = "netmap",
577         .priv_size = sizeof(struct netmap_priv),
578         .init = netmap_init,
579         .cleanup = netmap_cleanup,
580         .send = netmap_send,
581         .recv = netmap_recv,
582         .get_cap = netmap_get_cap,
583         .set_cap = netmap_set_cap,
584 };
585
586 /* A clone of the netmap backend, with a different prefix. */
587 static struct net_backend vale_backend = {
588         .prefix = "vale",
589         .priv_size = sizeof(struct netmap_priv),
590         .init = netmap_init,
591         .cleanup = netmap_cleanup,
592         .send = netmap_send,
593         .recv = netmap_recv,
594         .get_cap = netmap_get_cap,
595         .set_cap = netmap_set_cap,
596 };
597
598 DATA_SET(net_backend_set, netmap_backend);
599 DATA_SET(net_backend_set, vale_backend);
600
601 /*
602  * Initialize a backend and attach to the frontend.
603  * This is called during frontend initialization.
604  *  @pbe is a pointer to the backend to be initialized
605  *  @devname is the backend-name as supplied on the command line,
606  *      e.g. -s 2:0,frontend-name,backend-name[,other-args]
607  *  @cb is the receive callback supplied by the frontend,
608  *      and it is invoked in the event loop when a receive
609  *      event is generated in the hypervisor,
610  *  @param is a pointer to the frontend, and normally used as
611  *      the argument for the callback.
612  */
613 int
614 netbe_init(struct net_backend **ret, const char *devname, net_be_rxeof_t cb,
615     void *param)
616 {
617         struct net_backend **pbe, *nbe, *tbe = NULL;
618         int err;
619
620         /*
621          * Find the network backend that matches the user-provided
622          * device name. net_backend_set is built using a linker set.
623          */
624         SET_FOREACH(pbe, net_backend_set) {
625                 if (strncmp(devname, (*pbe)->prefix,
626                     strlen((*pbe)->prefix)) == 0) {
627                         tbe = *pbe;
628                         assert(tbe->init != NULL);
629                         assert(tbe->cleanup != NULL);
630                         assert(tbe->send != NULL);
631                         assert(tbe->recv != NULL);
632                         assert(tbe->get_cap != NULL);
633                         assert(tbe->set_cap != NULL);
634                         break;
635                 }
636         }
637
638         *ret = NULL;
639         if (tbe == NULL)
640                 return (EINVAL);
641         nbe = calloc(1, sizeof(*nbe) + tbe->priv_size);
642         *nbe = *tbe;    /* copy the template */
643         nbe->fd = -1;
644         nbe->sc = param;
645         nbe->be_vnet_hdr_len = 0;
646         nbe->fe_vnet_hdr_len = 0;
647
648         /* Initialize the backend. */
649         err = nbe->init(nbe, devname, cb, param);
650         if (err) {
651                 free(nbe);
652                 return (err);
653         }
654
655         *ret = nbe;
656
657         return (0);
658 }
659
660 void
661 netbe_cleanup(struct net_backend *be)
662 {
663
664         if (be != NULL) {
665                 be->cleanup(be);
666                 free(be);
667         }
668 }
669
670 uint64_t
671 netbe_get_cap(struct net_backend *be)
672 {
673
674         assert(be != NULL);
675         return (be->get_cap(be));
676 }
677
678 int
679 netbe_set_cap(struct net_backend *be, uint64_t features,
680               unsigned vnet_hdr_len)
681 {
682         int ret;
683
684         assert(be != NULL);
685
686         /* There are only three valid lengths, i.e., 0, 10 and 12. */
687         if (vnet_hdr_len && vnet_hdr_len != VNET_HDR_LEN
688                 && vnet_hdr_len != (VNET_HDR_LEN - sizeof(uint16_t)))
689                 return (-1);
690
691         be->fe_vnet_hdr_len = vnet_hdr_len;
692
693         ret = be->set_cap(be, features, vnet_hdr_len);
694         assert(be->be_vnet_hdr_len == 0 ||
695                be->be_vnet_hdr_len == be->fe_vnet_hdr_len);
696
697         return (ret);
698 }
699
700 static __inline struct iovec *
701 iov_trim(struct iovec *iov, int *iovcnt, unsigned int tlen)
702 {
703         struct iovec *riov;
704
705         /* XXX short-cut: assume first segment is >= tlen */
706         assert(iov[0].iov_len >= tlen);
707
708         iov[0].iov_len -= tlen;
709         if (iov[0].iov_len == 0) {
710                 assert(*iovcnt > 1);
711                 *iovcnt -= 1;
712                 riov = &iov[1];
713         } else {
714                 iov[0].iov_base = (void *)((uintptr_t)iov[0].iov_base + tlen);
715                 riov = &iov[0];
716         }
717
718         return (riov);
719 }
720
721 ssize_t
722 netbe_send(struct net_backend *be, struct iovec *iov, int iovcnt)
723 {
724
725         assert(be != NULL);
726         if (be->be_vnet_hdr_len != be->fe_vnet_hdr_len) {
727                 /*
728                  * The frontend uses a virtio-net header, but the backend
729                  * does not. We ignore it (as it must be all zeroes) and
730                  * strip it.
731                  */
732                 assert(be->be_vnet_hdr_len == 0);
733                 iov = iov_trim(iov, &iovcnt, be->fe_vnet_hdr_len);
734         }
735
736         return (be->send(be, iov, iovcnt));
737 }
738
739 /*
740  * Try to read a packet from the backend, without blocking.
741  * If no packets are available, return 0. In case of success, return
742  * the length of the packet just read. Return -1 in case of errors.
743  */
744 ssize_t
745 netbe_recv(struct net_backend *be, struct iovec *iov, int iovcnt)
746 {
747         /* Length of prepended virtio-net header. */
748         unsigned int hlen = be->fe_vnet_hdr_len;
749         int ret;
750
751         assert(be != NULL);
752
753         if (hlen && hlen != be->be_vnet_hdr_len) {
754                 /*
755                  * The frontend uses a virtio-net header, but the backend
756                  * does not. We need to prepend a zeroed header.
757                  */
758                 struct virtio_net_rxhdr *vh;
759
760                 assert(be->be_vnet_hdr_len == 0);
761
762                 /*
763                  * Get a pointer to the rx header, and use the
764                  * data immediately following it for the packet buffer.
765                  */
766                 vh = iov[0].iov_base;
767                 iov = iov_trim(iov, &iovcnt, hlen);
768
769                 /*
770                  * The only valid field in the rx packet header is the
771                  * number of buffers if merged rx bufs were negotiated.
772                  */
773                 memset(vh, 0, hlen);
774                 if (hlen == VNET_HDR_LEN) {
775                         vh->vrh_bufs = 1;
776                 }
777         }
778
779         ret = be->recv(be, iov, iovcnt);
780         if (ret > 0) {
781                 ret += hlen;
782         }
783
784         return (ret);
785 }
786
787 /*
788  * Read a packet from the backend and discard it.
789  * Returns the size of the discarded packet or zero if no packet was available.
790  * A negative error code is returned in case of read error.
791  */
792 ssize_t
793 netbe_rx_discard(struct net_backend *be)
794 {
795         /*
796          * MP note: the dummybuf is only used to discard frames,
797          * so there is no need for it to be per-vtnet or locked.
798          * We only make it large enough for TSO-sized segment.
799          */
800         static uint8_t dummybuf[65536 + 64];
801         struct iovec iov;
802
803         iov.iov_base = dummybuf;
804         iov.iov_len = sizeof(dummybuf);
805
806         return netbe_recv(be, &iov, 1);
807 }
808