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