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