xref: /freebsd/sys/net/if_llatbl.h (revision ee24d3b8401985c99a4a1755765fe9332daee568)
1 /*
2  * Copyright (c) 2004 Luigi Rizzo, Alessandro Cerri. All rights reserved.
3  * Copyright (c) 2004-2008 Qing Li. All rights reserved.
4  * Copyright (c) 2008 Kip Macy. All rights reserved.
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 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 PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #ifndef	_NET_IF_LLATBL_H_
31 #define	_NET_IF_LLATBL_H_
32 
33 #include "opt_ofed.h"
34 
35 #include <sys/_rwlock.h>
36 #include <netinet/in.h>
37 
38 struct ifnet;
39 struct sysctl_req;
40 struct rt_msghdr;
41 struct rt_addrinfo;
42 
43 struct llentry;
44 LIST_HEAD(llentries, llentry);
45 
46 extern struct rwlock lltable_rwlock;
47 #define	LLTABLE_RLOCK()		rw_rlock(&lltable_rwlock)
48 #define	LLTABLE_RUNLOCK()	rw_runlock(&lltable_rwlock)
49 #define	LLTABLE_WLOCK()		rw_wlock(&lltable_rwlock)
50 #define	LLTABLE_WUNLOCK()	rw_wunlock(&lltable_rwlock)
51 #define	LLTABLE_LOCK_ASSERT()	rw_assert(&lltable_rwlock, RA_LOCKED)
52 
53 /*
54  * Code referencing llentry must at least hold
55  * a shared lock
56  */
57 struct llentry {
58 	LIST_ENTRY(llentry)	 lle_next;
59 	struct rwlock		 lle_lock;
60 	struct lltable		 *lle_tbl;
61 	struct llentries	 *lle_head;
62 	void			(*lle_free)(struct lltable *, struct llentry *);
63 	struct mbuf		 *la_hold;
64 	int			 la_numheld;  /* # of packets currently held */
65 	time_t			 la_expire;
66 	uint16_t		 la_flags;
67 	uint16_t		 la_asked;
68 	uint16_t		 la_preempt;
69 	uint16_t		 ln_byhint;
70 	int16_t			 ln_state;	/* IPv6 has ND6_LLINFO_NOSTATE == -2 */
71 	uint16_t		 ln_router;
72 	time_t			 ln_ntick;
73 	int			 lle_refcnt;
74 
75 	union {
76 		uint64_t	mac_aligned;
77 		uint16_t	mac16[3];
78 #ifdef OFED
79 		uint8_t		mac8[20];	/* IB needs 20 bytes. */
80 #endif
81 	} ll_addr;
82 
83 	/* XXX af-private? */
84 	union {
85 		struct callout	ln_timer_ch;
86 		struct callout  la_timer;
87 	} lle_timer;
88 	/* NB: struct sockaddr must immediately follow */
89 };
90 
91 #define	LLE_WLOCK(lle)		rw_wlock(&(lle)->lle_lock)
92 #define	LLE_RLOCK(lle)		rw_rlock(&(lle)->lle_lock)
93 #define	LLE_WUNLOCK(lle)	rw_wunlock(&(lle)->lle_lock)
94 #define	LLE_RUNLOCK(lle)	rw_runlock(&(lle)->lle_lock)
95 #define	LLE_DOWNGRADE(lle)	rw_downgrade(&(lle)->lle_lock)
96 #define	LLE_TRY_UPGRADE(lle)	rw_try_upgrade(&(lle)->lle_lock)
97 #define	LLE_LOCK_INIT(lle)	rw_init_flags(&(lle)->lle_lock, "lle", RW_DUPOK)
98 #define	LLE_LOCK_DESTROY(lle)	rw_destroy(&(lle)->lle_lock)
99 #define	LLE_WLOCK_ASSERT(lle)	rw_assert(&(lle)->lle_lock, RA_WLOCKED)
100 
101 #define LLE_IS_VALID(lle)	(((lle) != NULL) && ((lle) != (void *)-1))
102 
103 #define	LLE_ADDREF(lle) do {					\
104 	LLE_WLOCK_ASSERT(lle);					\
105 	KASSERT((lle)->lle_refcnt >= 0,				\
106 	    ("negative refcnt %d on lle %p",			\
107 	    (lle)->lle_refcnt, (lle)));				\
108 	(lle)->lle_refcnt++;					\
109 } while (0)
110 
111 #define	LLE_REMREF(lle)	do {					\
112 	LLE_WLOCK_ASSERT(lle);					\
113 	KASSERT((lle)->lle_refcnt > 0,				\
114 	    ("bogus refcnt %d on lle %p",			\
115 	    (lle)->lle_refcnt, (lle)));				\
116 	(lle)->lle_refcnt--;					\
117 } while (0)
118 
119 #define	LLE_FREE_LOCKED(lle) do {				\
120 	if ((lle)->lle_refcnt == 1)				\
121 		(lle)->lle_free((lle)->lle_tbl, (lle));		\
122 	else {							\
123 		LLE_REMREF(lle);				\
124 		LLE_WUNLOCK(lle);				\
125 	}							\
126 	/* guard against invalid refs */			\
127 	(lle) = NULL;						\
128 } while (0)
129 
130 #define	LLE_FREE(lle) do {					\
131 	LLE_WLOCK(lle);						\
132 	LLE_FREE_LOCKED(lle);					\
133 } while (0)
134 
135 
136 #define	ln_timer_ch	lle_timer.ln_timer_ch
137 #define	la_timer	lle_timer.la_timer
138 
139 /* XXX bad name */
140 #define	L3_ADDR(lle)	((struct sockaddr *)(&lle[1]))
141 #define	L3_ADDR_LEN(lle)	(((struct sockaddr *)(&lle[1]))->sa_len)
142 
143 #ifndef LLTBL_HASHTBL_SIZE
144 #define	LLTBL_HASHTBL_SIZE	32	/* default 32 ? */
145 #endif
146 
147 #ifndef LLTBL_HASHMASK
148 #define	LLTBL_HASHMASK	(LLTBL_HASHTBL_SIZE - 1)
149 #endif
150 
151 struct lltable {
152 	SLIST_ENTRY(lltable)	llt_link;
153 	struct llentries	lle_head[LLTBL_HASHTBL_SIZE];
154 	int			llt_af;
155 	struct ifnet		*llt_ifp;
156 
157 	void			(*llt_prefix_free)(struct lltable *,
158 				    const struct sockaddr *prefix,
159 				    const struct sockaddr *mask,
160 				    u_int flags);
161 	struct llentry *	(*llt_lookup)(struct lltable *, u_int flags,
162 				    const struct sockaddr *l3addr);
163 	int			(*llt_dump)(struct lltable *,
164 				    struct sysctl_req *);
165 };
166 MALLOC_DECLARE(M_LLTABLE);
167 
168 /*
169  * flags to be passed to arplookup.
170  */
171 #define	LLE_DELETED	0x0001	/* entry must be deleted */
172 #define	LLE_STATIC	0x0002	/* entry is static */
173 #define	LLE_IFADDR	0x0004	/* entry is interface addr */
174 #define	LLE_VALID	0x0008	/* ll_addr is valid */
175 #define	LLE_PROXY	0x0010	/* proxy entry ??? */
176 #define	LLE_PUB		0x0020	/* publish entry ??? */
177 #define	LLE_LINKED	0x0040	/* linked to lookup structure */
178 #define	LLE_EXCLUSIVE	0x2000	/* return lle xlocked  */
179 #define	LLE_DELETE	0x4000	/* delete on a lookup - match LLE_IFADDR */
180 #define	LLE_CREATE	0x8000	/* create on a lookup miss */
181 
182 #define LLATBL_HASH(key, mask) \
183 	(((((((key >> 8) ^ key) >> 8) ^ key) >> 8) ^ key) & mask)
184 
185 struct lltable *lltable_init(struct ifnet *, int);
186 void		lltable_free(struct lltable *);
187 void		lltable_prefix_free(int, struct sockaddr *,
188 		    struct sockaddr *, u_int);
189 #if 0
190 void		lltable_drain(int);
191 #endif
192 int		lltable_sysctl_dumparp(int, struct sysctl_req *);
193 
194 size_t		llentry_free(struct llentry *);
195 struct llentry  *llentry_alloc(struct ifnet *, struct lltable *,
196 		    struct sockaddr_storage *);
197 
198 /*
199  * Generic link layer address lookup function.
200  */
201 static __inline struct llentry *
202 lla_lookup(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
203 {
204 	return llt->llt_lookup(llt, flags, l3addr);
205 }
206 
207 int		lla_rt_output(struct rt_msghdr *, struct rt_addrinfo *);
208 #endif  /* _NET_IF_LLATBL_H_ */
209