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