1be74aedeSMark Johnston /*- 2be74aedeSMark Johnston * SPDX-License-Identifier: BSD-2-Clause 3be74aedeSMark Johnston * 4be74aedeSMark Johnston * Copyright (c) 2019 Vincenzo Maffione <vmaffione@FreeBSD.org> 5be74aedeSMark Johnston * 6be74aedeSMark Johnston * Redistribution and use in source and binary forms, with or without 7be74aedeSMark Johnston * modification, are permitted provided that the following conditions 8be74aedeSMark Johnston * are met: 9be74aedeSMark Johnston * 1. Redistributions of source code must retain the above copyright 10be74aedeSMark Johnston * notice, this list of conditions and the following disclaimer. 11be74aedeSMark Johnston * 2. Redistributions in binary form must reproduce the above copyright 12be74aedeSMark Johnston * notice, this list of conditions and the following disclaimer in the 13be74aedeSMark Johnston * documentation and/or other materials provided with the distribution. 14be74aedeSMark Johnston * 15be74aedeSMark Johnston * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS``AS IS'' AND 16be74aedeSMark Johnston * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17be74aedeSMark Johnston * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 18be74aedeSMark Johnston * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS 19be74aedeSMark Johnston * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 20be74aedeSMark Johnston * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 21be74aedeSMark Johnston * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 22be74aedeSMark Johnston * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23be74aedeSMark Johnston * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 24be74aedeSMark Johnston * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 25be74aedeSMark Johnston * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26be74aedeSMark Johnston */ 27be74aedeSMark Johnston 28be74aedeSMark Johnston #ifndef __NET_BACKENDS_PRIV_H__ 29be74aedeSMark Johnston #define __NET_BACKENDS_PRIV_H__ 30be74aedeSMark Johnston 31be74aedeSMark Johnston #include <sys/linker_set.h> 32be74aedeSMark Johnston 33be74aedeSMark Johnston /* 34be74aedeSMark Johnston * Each network backend registers a set of function pointers that are 35be74aedeSMark Johnston * used to implement the net backends API. Frontends should not invoke 36be74aedeSMark Johnston * these functions directly, but should instead use the interface provided by 37be74aedeSMark Johnston * net_backends.h. 38be74aedeSMark Johnston */ 39be74aedeSMark Johnston struct net_backend { 40be74aedeSMark Johnston const char *prefix; /* prefix matching this backend */ 41be74aedeSMark Johnston 42be74aedeSMark Johnston /* 43be74aedeSMark Johnston * Routines used to initialize and cleanup the resources needed 44be74aedeSMark Johnston * by a backend. The cleanup function is used internally, 45be74aedeSMark Johnston * and should not be called by the frontend. 46be74aedeSMark Johnston */ 47be74aedeSMark Johnston int (*init)(struct net_backend *be, const char *devname, 48be74aedeSMark Johnston nvlist_t *nvl, net_be_rxeof_t cb, void *param); 49be74aedeSMark Johnston void (*cleanup)(struct net_backend *be); 50be74aedeSMark Johnston 51be74aedeSMark Johnston /* 52be74aedeSMark Johnston * Called to serve a guest transmit request. The scatter-gather 53be74aedeSMark Johnston * vector provided by the caller has 'iovcnt' elements and contains 54be74aedeSMark Johnston * the packet to send. 55be74aedeSMark Johnston */ 56be74aedeSMark Johnston ssize_t (*send)(struct net_backend *be, const struct iovec *iov, 57be74aedeSMark Johnston int iovcnt); 58be74aedeSMark Johnston 59be74aedeSMark Johnston /* 60be74aedeSMark Johnston * Get the length of the next packet that can be received from 61be74aedeSMark Johnston * the backend. If no packets are currently available, this 62be74aedeSMark Johnston * function returns 0. 63be74aedeSMark Johnston */ 64be74aedeSMark Johnston ssize_t (*peek_recvlen)(struct net_backend *be); 65be74aedeSMark Johnston 66be74aedeSMark Johnston /* 67be74aedeSMark Johnston * Called to receive a packet from the backend. When the function 68be74aedeSMark Johnston * returns a positive value 'len', the scatter-gather vector 69be74aedeSMark Johnston * provided by the caller contains a packet with such length. 70be74aedeSMark Johnston * The function returns 0 if the backend doesn't have a new packet to 71be74aedeSMark Johnston * receive. 72be74aedeSMark Johnston */ 73be74aedeSMark Johnston ssize_t (*recv)(struct net_backend *be, const struct iovec *iov, 74be74aedeSMark Johnston int iovcnt); 75be74aedeSMark Johnston 76be74aedeSMark Johnston /* 77be74aedeSMark Johnston * Ask the backend to enable or disable receive operation in the 78be74aedeSMark Johnston * backend. On return from a disable operation, it is guaranteed 79be74aedeSMark Johnston * that the receive callback won't be called until receive is 80be74aedeSMark Johnston * enabled again. Note however that it is up to the caller to make 81be74aedeSMark Johnston * sure that netbe_recv() is not currently being executed by another 82be74aedeSMark Johnston * thread. 83be74aedeSMark Johnston */ 84be74aedeSMark Johnston void (*recv_enable)(struct net_backend *be); 85be74aedeSMark Johnston void (*recv_disable)(struct net_backend *be); 86be74aedeSMark Johnston 87be74aedeSMark Johnston /* 88be74aedeSMark Johnston * Ask the backend for the virtio-net features it is able to 89be74aedeSMark Johnston * support. Possible features are TSO, UFO and checksum offloading 90be74aedeSMark Johnston * in both rx and tx direction and for both IPv4 and IPv6. 91be74aedeSMark Johnston */ 92be74aedeSMark Johnston uint64_t (*get_cap)(struct net_backend *be); 93be74aedeSMark Johnston 94be74aedeSMark Johnston /* 95be74aedeSMark Johnston * Tell the backend to enable/disable the specified virtio-net 96be74aedeSMark Johnston * features (capabilities). 97be74aedeSMark Johnston */ 98be74aedeSMark Johnston int (*set_cap)(struct net_backend *be, uint64_t features, 99be74aedeSMark Johnston unsigned int vnet_hdr_len); 100be74aedeSMark Johnston 101be74aedeSMark Johnston struct pci_vtnet_softc *sc; 102be74aedeSMark Johnston int fd; 103be74aedeSMark Johnston 104be74aedeSMark Johnston /* 105be74aedeSMark Johnston * Length of the virtio-net header used by the backend and the 106be74aedeSMark Johnston * frontend, respectively. A zero value means that the header 107be74aedeSMark Johnston * is not used. 108be74aedeSMark Johnston */ 109be74aedeSMark Johnston unsigned int be_vnet_hdr_len; 110be74aedeSMark Johnston unsigned int fe_vnet_hdr_len; 111be74aedeSMark Johnston 112be74aedeSMark Johnston /* Size of backend-specific private data. */ 113be74aedeSMark Johnston size_t priv_size; 114be74aedeSMark Johnston 115be74aedeSMark Johnston /* Backend-specific private data follows. */ 116be74aedeSMark Johnston }; 117be74aedeSMark Johnston 118be74aedeSMark Johnston #define NET_BE_PRIV(be) ((void *)((be) + 1)) 119be74aedeSMark Johnston 120be74aedeSMark Johnston SET_DECLARE(net_backend_set, struct net_backend); 121be74aedeSMark Johnston 122be74aedeSMark Johnston #define VNET_HDR_LEN sizeof(struct virtio_net_rxhdr) 123be74aedeSMark Johnston 124be74aedeSMark Johnston /* 125be74aedeSMark Johnston * Export the tap backend routines for the benefit of other backends which have 126be74aedeSMark Johnston * a similar interface to the kernel, i.e., they send and receive data using 127be74aedeSMark Johnston * standard I/O system calls with a single file descriptor. 128be74aedeSMark Johnston */ 129be74aedeSMark Johnston 130be74aedeSMark Johnston struct tap_priv { 131be74aedeSMark Johnston struct mevent *mevp; 132be74aedeSMark Johnston /* 133be74aedeSMark Johnston * A bounce buffer that allows us to implement the peek_recvlen 134be74aedeSMark Johnston * callback. In the future we may get the same information from 135be74aedeSMark Johnston * the kevent data. 136be74aedeSMark Johnston */ 137be74aedeSMark Johnston char bbuf[1 << 16]; 138be74aedeSMark Johnston ssize_t bbuflen; 139be74aedeSMark Johnston }; 140be74aedeSMark Johnston 141be74aedeSMark Johnston void tap_cleanup(struct net_backend *be); 142be74aedeSMark Johnston ssize_t tap_send(struct net_backend *be, const struct iovec *iov, int iovcnt); 143be74aedeSMark Johnston ssize_t tap_recv(struct net_backend *be, const struct iovec *iov, int iovcnt); 144be74aedeSMark Johnston ssize_t tap_peek_recvlen(struct net_backend *be); 145be74aedeSMark Johnston void tap_recv_enable(struct net_backend *be); 146be74aedeSMark Johnston ssize_t tap_recv(struct net_backend *be, const struct iovec *iov, int iovcnt); 147be74aedeSMark Johnston void tap_recv_disable(struct net_backend *be); 148be74aedeSMark Johnston uint64_t tap_get_cap(struct net_backend *be); 149be74aedeSMark Johnston int tap_set_cap(struct net_backend *be, uint64_t features, 150be74aedeSMark Johnston unsigned vnet_hdr_len); 151be74aedeSMark Johnston 152be74aedeSMark Johnston #endif /* !__NET_BACKENDS_PRIV_H__ */ 153