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