xref: /freebsd/usr.sbin/bhyve/net_backends.h (revision 401ab69cff8fa2320a9f8ea4baa114a6da6c952b)
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_H__
29 #define __NET_BACKENDS_H__
30 
31 #include <stdint.h>
32 
33 /* Opaque type representing a network backend. */
34 typedef struct net_backend net_backend_t;
35 
36 /* Interface between network frontends and the network backends. */
37 typedef void (*net_be_rxeof_t)(int, enum ev_type, void *param);
38 int	netbe_init(net_backend_t **be, nvlist_t *nvl, net_be_rxeof_t cb,
39             void *param);
40 int	netbe_legacy_config(nvlist_t *nvl, const char *opts);
41 void	netbe_cleanup(net_backend_t *be);
42 uint64_t netbe_get_cap(net_backend_t *be);
43 int	 netbe_set_cap(net_backend_t *be, uint64_t cap,
44              unsigned vnet_hdr_len);
45 size_t	netbe_get_vnet_hdr_len(net_backend_t *be);
46 ssize_t	netbe_send(net_backend_t *be, const struct iovec *iov, int iovcnt);
47 ssize_t	netbe_peek_recvlen(net_backend_t *be);
48 ssize_t	netbe_recv(net_backend_t *be, const struct iovec *iov, int iovcnt);
49 ssize_t	netbe_rx_discard(net_backend_t *be);
50 void	netbe_rx_disable(net_backend_t *be);
51 void	netbe_rx_enable(net_backend_t *be);
52 
53 
54 /*
55  * Network device capabilities taken from the VirtIO standard.
56  * Despite the name, these capabilities can be used by different frontends
57  * (virtio-net, ptnet) and supported by different backends (netmap, tap, ...).
58  */
59 #define	VIRTIO_NET_F_CSUM	(1 <<  0) /* host handles partial cksum */
60 #define	VIRTIO_NET_F_GUEST_CSUM	(1 <<  1) /* guest handles partial cksum */
61 #define	VIRTIO_NET_F_MTU	(1 <<  3) /* initial MTU advice */
62 #define	VIRTIO_NET_F_MAC	(1 <<  5) /* host supplies MAC */
63 #define	VIRTIO_NET_F_GSO_DEPREC	(1 <<  6) /* deprecated: host handles GSO */
64 #define	VIRTIO_NET_F_GUEST_TSO4	(1 <<  7) /* guest can rcv TSOv4 */
65 #define	VIRTIO_NET_F_GUEST_TSO6	(1 <<  8) /* guest can rcv TSOv6 */
66 #define	VIRTIO_NET_F_GUEST_ECN	(1 <<  9) /* guest can rcv TSO with ECN */
67 #define	VIRTIO_NET_F_GUEST_UFO	(1 << 10) /* guest can rcv UFO */
68 #define	VIRTIO_NET_F_HOST_TSO4	(1 << 11) /* host can rcv TSOv4 */
69 #define	VIRTIO_NET_F_HOST_TSO6	(1 << 12) /* host can rcv TSOv6 */
70 #define	VIRTIO_NET_F_HOST_ECN	(1 << 13) /* host can rcv TSO with ECN */
71 #define	VIRTIO_NET_F_HOST_UFO	(1 << 14) /* host can rcv UFO */
72 #define	VIRTIO_NET_F_MRG_RXBUF	(1 << 15) /* host can merge RX buffers */
73 #define	VIRTIO_NET_F_STATUS	(1 << 16) /* config status field available */
74 #define	VIRTIO_NET_F_CTRL_VQ	(1 << 17) /* control channel available */
75 #define	VIRTIO_NET_F_CTRL_RX	(1 << 18) /* control channel RX mode support */
76 #define	VIRTIO_NET_F_CTRL_VLAN	(1 << 19) /* control channel VLAN filtering */
77 #define	VIRTIO_NET_F_GUEST_ANNOUNCE \
78 				(1 << 21) /* guest can send gratuitous pkts */
79 #define	VIRTIO_NET_F_MQ		(1 << 22) /* host supports multiple VQ pairs */
80 
81 /*
82  * Fixed network header size
83  */
84 struct virtio_net_rxhdr {
85 	uint8_t		vrh_flags;
86 	uint8_t		vrh_gso_type;
87 	uint16_t	vrh_hdr_len;
88 	uint16_t	vrh_gso_size;
89 	uint16_t	vrh_csum_start;
90 	uint16_t	vrh_csum_offset;
91 	uint16_t	vrh_bufs;
92 } __packed;
93 
94 #endif /* __NET_BACKENDS_H__ */
95