xref: /freebsd-14.2/sys/dev/netmap/netmap_bdg.h (revision 2a7db7a6)
1*2a7db7a6SVincenzo Maffione /*-
2*2a7db7a6SVincenzo Maffione  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3*2a7db7a6SVincenzo Maffione  *
4*2a7db7a6SVincenzo Maffione  * Copyright (C) 2013-2018 Universita` di Pisa
5*2a7db7a6SVincenzo Maffione  * All rights reserved.
6*2a7db7a6SVincenzo Maffione  *
7*2a7db7a6SVincenzo Maffione  * Redistribution and use in source and binary forms, with or without
8*2a7db7a6SVincenzo Maffione  * modification, are permitted provided that the following conditions
9*2a7db7a6SVincenzo Maffione  * are met:
10*2a7db7a6SVincenzo Maffione  *   1. Redistributions of source code must retain the above copyright
11*2a7db7a6SVincenzo Maffione  *      notice, this list of conditions and the following disclaimer.
12*2a7db7a6SVincenzo Maffione  *   2. Redistributions in binary form must reproduce the above copyright
13*2a7db7a6SVincenzo Maffione  *      notice, this list of conditions and the following disclaimer in the
14*2a7db7a6SVincenzo Maffione  *      documentation and/or other materials provided with the distribution.
15*2a7db7a6SVincenzo Maffione  *
16*2a7db7a6SVincenzo Maffione  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17*2a7db7a6SVincenzo Maffione  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*2a7db7a6SVincenzo Maffione  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*2a7db7a6SVincenzo Maffione  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20*2a7db7a6SVincenzo Maffione  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21*2a7db7a6SVincenzo Maffione  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22*2a7db7a6SVincenzo Maffione  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23*2a7db7a6SVincenzo Maffione  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24*2a7db7a6SVincenzo Maffione  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25*2a7db7a6SVincenzo Maffione  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*2a7db7a6SVincenzo Maffione  * SUCH DAMAGE.
27*2a7db7a6SVincenzo Maffione  *
28*2a7db7a6SVincenzo Maffione  * $FreeBSD$
29*2a7db7a6SVincenzo Maffione  */
30*2a7db7a6SVincenzo Maffione #ifndef _NET_NETMAP_BDG_H_
31*2a7db7a6SVincenzo Maffione #define _NET_NETMAP_BDG_H_
32*2a7db7a6SVincenzo Maffione 
33*2a7db7a6SVincenzo Maffione #if defined(__FreeBSD__)
34*2a7db7a6SVincenzo Maffione #define BDG_RWLOCK_T		struct rwlock // struct rwlock
35*2a7db7a6SVincenzo Maffione 
36*2a7db7a6SVincenzo Maffione #define	BDG_RWINIT(b)		\
37*2a7db7a6SVincenzo Maffione 	rw_init_flags(&(b)->bdg_lock, "bdg lock", RW_NOWITNESS)
38*2a7db7a6SVincenzo Maffione #define BDG_WLOCK(b)		rw_wlock(&(b)->bdg_lock)
39*2a7db7a6SVincenzo Maffione #define BDG_WUNLOCK(b)		rw_wunlock(&(b)->bdg_lock)
40*2a7db7a6SVincenzo Maffione #define BDG_RLOCK(b)		rw_rlock(&(b)->bdg_lock)
41*2a7db7a6SVincenzo Maffione #define BDG_RTRYLOCK(b)		rw_try_rlock(&(b)->bdg_lock)
42*2a7db7a6SVincenzo Maffione #define BDG_RUNLOCK(b)		rw_runlock(&(b)->bdg_lock)
43*2a7db7a6SVincenzo Maffione #define BDG_RWDESTROY(b)	rw_destroy(&(b)->bdg_lock)
44*2a7db7a6SVincenzo Maffione 
45*2a7db7a6SVincenzo Maffione #endif /* __FreeBSD__ */
46*2a7db7a6SVincenzo Maffione 
47*2a7db7a6SVincenzo Maffione /* XXX Should go away after fixing find_bridge() - Michio */
48*2a7db7a6SVincenzo Maffione #define NM_BDG_HASH		1024	/* forwarding table entries */
49*2a7db7a6SVincenzo Maffione 
50*2a7db7a6SVincenzo Maffione /* XXX revise this */
51*2a7db7a6SVincenzo Maffione struct nm_hash_ent {
52*2a7db7a6SVincenzo Maffione 	uint64_t	mac;	/* the top 2 bytes are the epoch */
53*2a7db7a6SVincenzo Maffione 	uint64_t	ports;
54*2a7db7a6SVincenzo Maffione };
55*2a7db7a6SVincenzo Maffione 
56*2a7db7a6SVincenzo Maffione /* Default size for the Maximum Frame Size. */
57*2a7db7a6SVincenzo Maffione #define NM_BDG_MFS_DEFAULT	1514
58*2a7db7a6SVincenzo Maffione 
59*2a7db7a6SVincenzo Maffione /*
60*2a7db7a6SVincenzo Maffione  * nm_bridge is a descriptor for a VALE switch.
61*2a7db7a6SVincenzo Maffione  * Interfaces for a bridge are all in bdg_ports[].
62*2a7db7a6SVincenzo Maffione  * The array has fixed size, an empty entry does not terminate
63*2a7db7a6SVincenzo Maffione  * the search, but lookups only occur on attach/detach so we
64*2a7db7a6SVincenzo Maffione  * don't mind if they are slow.
65*2a7db7a6SVincenzo Maffione  *
66*2a7db7a6SVincenzo Maffione  * The bridge is non blocking on the transmit ports: excess
67*2a7db7a6SVincenzo Maffione  * packets are dropped if there is no room on the output port.
68*2a7db7a6SVincenzo Maffione  *
69*2a7db7a6SVincenzo Maffione  * bdg_lock protects accesses to the bdg_ports array.
70*2a7db7a6SVincenzo Maffione  * This is a rw lock (or equivalent).
71*2a7db7a6SVincenzo Maffione  */
72*2a7db7a6SVincenzo Maffione #define NM_BDG_IFNAMSIZ IFNAMSIZ
73*2a7db7a6SVincenzo Maffione struct nm_bridge {
74*2a7db7a6SVincenzo Maffione 	/* XXX what is the proper alignment/layout ? */
75*2a7db7a6SVincenzo Maffione 	BDG_RWLOCK_T	bdg_lock;	/* protects bdg_ports */
76*2a7db7a6SVincenzo Maffione 	int		bdg_namelen;
77*2a7db7a6SVincenzo Maffione 	uint32_t	bdg_active_ports;
78*2a7db7a6SVincenzo Maffione 	char		bdg_basename[NM_BDG_IFNAMSIZ];
79*2a7db7a6SVincenzo Maffione 
80*2a7db7a6SVincenzo Maffione 	/* Indexes of active ports (up to active_ports)
81*2a7db7a6SVincenzo Maffione 	 * and all other remaining ports.
82*2a7db7a6SVincenzo Maffione 	 */
83*2a7db7a6SVincenzo Maffione 	uint32_t	bdg_port_index[NM_BDG_MAXPORTS];
84*2a7db7a6SVincenzo Maffione 	/* used by netmap_bdg_detach_common() */
85*2a7db7a6SVincenzo Maffione 	uint32_t	tmp_bdg_port_index[NM_BDG_MAXPORTS];
86*2a7db7a6SVincenzo Maffione 
87*2a7db7a6SVincenzo Maffione 	struct netmap_vp_adapter *bdg_ports[NM_BDG_MAXPORTS];
88*2a7db7a6SVincenzo Maffione 
89*2a7db7a6SVincenzo Maffione 	/*
90*2a7db7a6SVincenzo Maffione 	 * Programmable lookup functions to figure out the destination port.
91*2a7db7a6SVincenzo Maffione 	 * It returns either of an index of the destination port,
92*2a7db7a6SVincenzo Maffione 	 * NM_BDG_BROADCAST to broadcast this packet, or NM_BDG_NOPORT not to
93*2a7db7a6SVincenzo Maffione 	 * forward this packet.  ring_nr is the source ring index, and the
94*2a7db7a6SVincenzo Maffione 	 * function may overwrite this value to forward this packet to a
95*2a7db7a6SVincenzo Maffione 	 * different ring index.
96*2a7db7a6SVincenzo Maffione 	 * The function is set by netmap_bdg_regops().
97*2a7db7a6SVincenzo Maffione 	 */
98*2a7db7a6SVincenzo Maffione 	struct netmap_bdg_ops *bdg_ops;
99*2a7db7a6SVincenzo Maffione 
100*2a7db7a6SVincenzo Maffione 	/*
101*2a7db7a6SVincenzo Maffione 	 * Contains the data structure used by the bdg_ops.lookup function.
102*2a7db7a6SVincenzo Maffione 	 * By default points to *ht which is allocated on attach and used by the default lookup
103*2a7db7a6SVincenzo Maffione 	 * otherwise will point to the data structure received by netmap_bdg_regops().
104*2a7db7a6SVincenzo Maffione 	 */
105*2a7db7a6SVincenzo Maffione 	void *private_data;
106*2a7db7a6SVincenzo Maffione 	struct nm_hash_ent *ht;
107*2a7db7a6SVincenzo Maffione 
108*2a7db7a6SVincenzo Maffione 	/* Currently used to specify if the bridge is still in use while empty and
109*2a7db7a6SVincenzo Maffione 	 * if it has been put in exclusive mode by an external module, see netmap_bdg_regops()
110*2a7db7a6SVincenzo Maffione 	 * and netmap_bdg_create().
111*2a7db7a6SVincenzo Maffione 	 */
112*2a7db7a6SVincenzo Maffione #define NM_BDG_ACTIVE		1
113*2a7db7a6SVincenzo Maffione #define NM_BDG_EXCLUSIVE	2
114*2a7db7a6SVincenzo Maffione 	uint8_t			bdg_flags;
115*2a7db7a6SVincenzo Maffione 
116*2a7db7a6SVincenzo Maffione 
117*2a7db7a6SVincenzo Maffione #ifdef CONFIG_NET_NS
118*2a7db7a6SVincenzo Maffione 	struct net *ns;
119*2a7db7a6SVincenzo Maffione #endif /* CONFIG_NET_NS */
120*2a7db7a6SVincenzo Maffione };
121*2a7db7a6SVincenzo Maffione 
122*2a7db7a6SVincenzo Maffione static inline void *
123*2a7db7a6SVincenzo Maffione nm_bdg_get_auth_token(struct nm_bridge *b)
124*2a7db7a6SVincenzo Maffione {
125*2a7db7a6SVincenzo Maffione 	return b->ht;
126*2a7db7a6SVincenzo Maffione }
127*2a7db7a6SVincenzo Maffione 
128*2a7db7a6SVincenzo Maffione /* bridge not in exclusive mode ==> always valid
129*2a7db7a6SVincenzo Maffione  * bridge in exclusive mode (created through netmap_bdg_create()) ==> check authentication token
130*2a7db7a6SVincenzo Maffione  */
131*2a7db7a6SVincenzo Maffione static inline int
132*2a7db7a6SVincenzo Maffione nm_bdg_valid_auth_token(struct nm_bridge *b, void *auth_token)
133*2a7db7a6SVincenzo Maffione {
134*2a7db7a6SVincenzo Maffione 	return !(b->bdg_flags & NM_BDG_EXCLUSIVE) || b->ht == auth_token;
135*2a7db7a6SVincenzo Maffione }
136*2a7db7a6SVincenzo Maffione 
137*2a7db7a6SVincenzo Maffione int netmap_get_bdg_na(struct nmreq_header *hdr, struct netmap_adapter **na,
138*2a7db7a6SVincenzo Maffione 	struct netmap_mem_d *nmd, int create, struct netmap_bdg_ops *ops);
139*2a7db7a6SVincenzo Maffione 
140*2a7db7a6SVincenzo Maffione struct nm_bridge *nm_find_bridge(const char *name, int create, struct netmap_bdg_ops *ops);
141*2a7db7a6SVincenzo Maffione int netmap_bdg_free(struct nm_bridge *b);
142*2a7db7a6SVincenzo Maffione void netmap_bdg_detach_common(struct nm_bridge *b, int hw, int sw);
143*2a7db7a6SVincenzo Maffione int netmap_vp_bdg_ctl(struct nmreq_header *hdr, struct netmap_adapter *na);
144*2a7db7a6SVincenzo Maffione int netmap_vp_reg(struct netmap_adapter *na, int onoff);
145*2a7db7a6SVincenzo Maffione int netmap_bwrap_reg(struct netmap_adapter *, int onoff);
146*2a7db7a6SVincenzo Maffione int netmap_vp_reg(struct netmap_adapter *na, int onoff);
147*2a7db7a6SVincenzo Maffione int netmap_vp_rxsync(struct netmap_kring *kring, int flags);
148*2a7db7a6SVincenzo Maffione int netmap_bwrap_notify(struct netmap_kring *kring, int flags);
149*2a7db7a6SVincenzo Maffione int netmap_bwrap_attach_common(struct netmap_adapter *na,
150*2a7db7a6SVincenzo Maffione 		struct netmap_adapter *hwna);
151*2a7db7a6SVincenzo Maffione int netmap_bwrap_krings_create_common(struct netmap_adapter *na);
152*2a7db7a6SVincenzo Maffione void netmap_bwrap_krings_delete_common(struct netmap_adapter *na);
153*2a7db7a6SVincenzo Maffione #define NM_NEED_BWRAP (-2)
154*2a7db7a6SVincenzo Maffione #endif /* _NET_NETMAP_BDG_H_ */
155*2a7db7a6SVincenzo Maffione 
156