1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 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 28 #ifndef __NET_BACKENDS_PRIV_H__ 29 #define __NET_BACKENDS_PRIV_H__ 30 31 #include <sys/linker_set.h> 32 33 /* 34 * Each network backend registers a set of function pointers that are 35 * used to implement the net backends API. 36 * This might need to be exposed if we implement backends in separate files. 37 */ 38 struct net_backend { 39 const char *prefix; /* prefix matching this backend */ 40 41 /* 42 * Routines used to initialize and cleanup the resources needed 43 * by a backend. The cleanup function is used internally, 44 * and should not be called by the frontend. 45 */ 46 int (*init)(struct net_backend *be, const char *devname, 47 nvlist_t *nvl, net_be_rxeof_t cb, void *param); 48 void (*cleanup)(struct net_backend *be); 49 50 /* 51 * Called to serve a guest transmit request. The scatter-gather 52 * vector provided by the caller has 'iovcnt' elements and contains 53 * the packet to send. 54 */ 55 ssize_t (*send)(struct net_backend *be, const struct iovec *iov, 56 int iovcnt); 57 58 /* 59 * Get the length of the next packet that can be received from 60 * the backend. If no packets are currently available, this 61 * function returns 0. 62 */ 63 ssize_t (*peek_recvlen)(struct net_backend *be); 64 65 /* 66 * Called to receive a packet from the backend. When the function 67 * returns a positive value 'len', the scatter-gather vector 68 * provided by the caller contains a packet with such length. 69 * The function returns 0 if the backend doesn't have a new packet to 70 * receive. 71 */ 72 ssize_t (*recv)(struct net_backend *be, const struct iovec *iov, 73 int iovcnt); 74 75 /* 76 * Ask the backend to enable or disable receive operation in the 77 * backend. On return from a disable operation, it is guaranteed 78 * that the receive callback won't be called until receive is 79 * enabled again. Note however that it is up to the caller to make 80 * sure that netbe_recv() is not currently being executed by another 81 * thread. 82 */ 83 void (*recv_enable)(struct net_backend *be); 84 void (*recv_disable)(struct net_backend *be); 85 86 /* 87 * Ask the backend for the virtio-net features it is able to 88 * support. Possible features are TSO, UFO and checksum offloading 89 * in both rx and tx direction and for both IPv4 and IPv6. 90 */ 91 uint64_t (*get_cap)(struct net_backend *be); 92 93 /* 94 * Tell the backend to enable/disable the specified virtio-net 95 * features (capabilities). 96 */ 97 int (*set_cap)(struct net_backend *be, uint64_t features, 98 unsigned int vnet_hdr_len); 99 100 #ifndef __FreeBSD__ 101 int (*get_mac)(struct net_backend *be, void *, size_t *); 102 #endif 103 104 struct pci_vtnet_softc *sc; 105 int fd; 106 107 /* 108 * Length of the virtio-net header used by the backend and the 109 * frontend, respectively. A zero value means that the header 110 * is not used. 111 */ 112 unsigned int be_vnet_hdr_len; 113 unsigned int fe_vnet_hdr_len; 114 115 /* Size of backend-specific private data. */ 116 size_t priv_size; 117 118 /* Backend-specific private data follows. */ 119 }; 120 121 #define NET_BE_PRIV(be) ((void *)((be) + 1)) 122 123 SET_DECLARE(net_backend_set, struct net_backend); 124 125 #define VNET_HDR_LEN sizeof(struct virtio_net_rxhdr) 126 127 #ifdef __FreeBSD__ 128 129 /* 130 * Export the tap backend routines for the benefit of other backends which have 131 * a similar interface to the kernel, i.e., they send and receive data using 132 * standard I/O system calls with a single file descriptor. 133 */ 134 135 struct tap_priv { 136 struct mevent *mevp; 137 /* 138 * A bounce buffer that allows us to implement the peek_recvlen 139 * callback. In the future we may get the same information from 140 * the kevent data. 141 */ 142 char bbuf[1 << 16]; 143 ssize_t bbuflen; 144 }; 145 146 void tap_cleanup(struct net_backend *be); 147 ssize_t tap_send(struct net_backend *be, const struct iovec *iov, int io 148 vcnt); 149 ssize_t tap_recv(struct net_backend *be, const struct iovec *iov, int io 150 vcnt); 151 ssize_t tap_peek_recvlen(struct net_backend *be); 152 void tap_recv_enable(struct net_backend *be); 153 ssize_t tap_recv(struct net_backend *be, const struct iovec *iov, int io 154 vcnt); 155 void tap_recv_disable(struct net_backend *be); 156 uint64_t tap_get_cap(struct net_backend *be); 157 int tap_set_cap(struct net_backend *be, uint64_t features, 158 unsigned vnet_hdr_len); 159 160 #endif /* __FreeBSD__ */ 161 162 #endif /* !__NET_BACKENDS_PRIV_H__ */ 163