]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/net/if_llatbl.h
Fix lagg failover due to missing notifications
[FreeBSD/FreeBSD.git] / sys / net / if_llatbl.h
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 <sys/_rwlock.h>
34 #include <netinet/in.h>
35
36 struct ifnet;
37 struct sysctl_req;
38 struct rt_msghdr;
39 struct rt_addrinfo;
40
41 struct llentry;
42 LIST_HEAD(llentries, llentry);
43
44 extern struct rwlock lltable_rwlock;
45 #define LLTABLE_RLOCK()         rw_rlock(&lltable_rwlock)
46 #define LLTABLE_RUNLOCK()       rw_runlock(&lltable_rwlock)
47 #define LLTABLE_WLOCK()         rw_wlock(&lltable_rwlock)
48 #define LLTABLE_WUNLOCK()       rw_wunlock(&lltable_rwlock)
49 #define LLTABLE_LOCK_ASSERT()   rw_assert(&lltable_rwlock, RA_LOCKED)
50
51 /*
52  * Code referencing llentry must at least hold
53  * a shared lock
54  */
55 struct llentry {
56         LIST_ENTRY(llentry)      lle_next;
57         union {
58                 struct in_addr  addr4;
59                 struct in6_addr addr6;
60         } r_l3addr;
61         union {
62                 uint64_t        mac_aligned;
63                 uint16_t        mac16[3];
64                 uint8_t         mac8[20];       /* IB needs 20 bytes. */
65         } ll_addr;
66         uint16_t                r_flags;        /* LLE runtime flags */
67         uint16_t                r_skip_req;     /* feedback from fast path */
68         uint64_t                spare1;
69
70         struct lltable           *lle_tbl;
71         struct llentries         *lle_head;
72         void                    (*lle_free)(struct llentry *);
73         struct mbuf              *la_hold;
74         int                      la_numheld;  /* # of packets currently held */
75         time_t                   la_expire;
76         uint16_t                 la_flags;
77         uint16_t                 la_asked;
78         uint16_t                 la_preempt;
79         int16_t                  ln_state;      /* IPv6 has ND6_LLINFO_NOSTATE == -2 */
80         uint16_t                 ln_router;
81         time_t                   ln_ntick;
82         time_t                  lle_remtime;    /* Real time remaining */
83         time_t                  lle_hittime;    /* Time when r_skip_req was unset */
84         int                      lle_refcnt;
85
86         LIST_ENTRY(llentry)     lle_chain;      /* chain of deleted items */
87         struct callout          lle_timer;
88         struct rwlock            lle_lock;
89         struct mtx              req_mtx;
90 };
91
92 #define LLE_WLOCK(lle)          rw_wlock(&(lle)->lle_lock)
93 #define LLE_RLOCK(lle)          rw_rlock(&(lle)->lle_lock)
94 #define LLE_WUNLOCK(lle)        rw_wunlock(&(lle)->lle_lock)
95 #define LLE_RUNLOCK(lle)        rw_runlock(&(lle)->lle_lock)
96 #define LLE_DOWNGRADE(lle)      rw_downgrade(&(lle)->lle_lock)
97 #define LLE_TRY_UPGRADE(lle)    rw_try_upgrade(&(lle)->lle_lock)
98 #define LLE_LOCK_INIT(lle)      rw_init_flags(&(lle)->lle_lock, "lle", RW_DUPOK)
99 #define LLE_LOCK_DESTROY(lle)   rw_destroy(&(lle)->lle_lock)
100 #define LLE_WLOCK_ASSERT(lle)   rw_assert(&(lle)->lle_lock, RA_WLOCKED)
101
102 #define LLE_REQ_INIT(lle)       mtx_init(&(lle)->req_mtx, "lle req", \
103         NULL, MTX_DEF)
104 #define LLE_REQ_DESTROY(lle)    mtx_destroy(&(lle)->req_mtx)
105 #define LLE_REQ_LOCK(lle)       mtx_lock(&(lle)->req_mtx)
106 #define LLE_REQ_UNLOCK(lle)     mtx_unlock(&(lle)->req_mtx)
107
108 #define LLE_IS_VALID(lle)       (((lle) != NULL) && ((lle) != (void *)-1))
109
110 #define LLE_ADDREF(lle) do {                                    \
111         LLE_WLOCK_ASSERT(lle);                                  \
112         KASSERT((lle)->lle_refcnt >= 0,                         \
113             ("negative refcnt %d on lle %p",                    \
114             (lle)->lle_refcnt, (lle)));                         \
115         (lle)->lle_refcnt++;                                    \
116 } while (0)
117
118 #define LLE_REMREF(lle) do {                                    \
119         LLE_WLOCK_ASSERT(lle);                                  \
120         KASSERT((lle)->lle_refcnt > 0,                          \
121             ("bogus refcnt %d on lle %p",                       \
122             (lle)->lle_refcnt, (lle)));                         \
123         (lle)->lle_refcnt--;                                    \
124 } while (0)
125
126 #define LLE_FREE_LOCKED(lle) do {                               \
127         if ((lle)->lle_refcnt == 1)                             \
128                 (lle)->lle_free(lle);                           \
129         else {                                                  \
130                 LLE_REMREF(lle);                                \
131                 LLE_WUNLOCK(lle);                               \
132         }                                                       \
133         /* guard against invalid refs */                        \
134         (lle) = NULL;                                           \
135 } while (0)
136
137 #define LLE_FREE(lle) do {                                      \
138         LLE_WLOCK(lle);                                         \
139         LLE_FREE_LOCKED(lle);                                   \
140 } while (0)
141
142
143 typedef struct llentry *(llt_lookup_t)(struct lltable *, u_int flags,
144     const struct sockaddr *l3addr);
145 typedef struct llentry *(llt_alloc_t)(struct lltable *, u_int flags,
146     const struct sockaddr *l3addr);
147 typedef void (llt_delete_t)(struct lltable *, struct llentry *);
148 typedef void (llt_prefix_free_t)(struct lltable *,
149     const struct sockaddr *addr, const struct sockaddr *mask, u_int flags);
150 typedef int (llt_dump_entry_t)(struct lltable *, struct llentry *,
151     struct sysctl_req *);
152 typedef uint32_t (llt_hash_t)(const struct llentry *, uint32_t);
153 typedef int (llt_match_prefix_t)(const struct sockaddr *,
154     const struct sockaddr *, u_int, struct llentry *);
155 typedef void (llt_free_entry_t)(struct lltable *, struct llentry *);
156 typedef void (llt_fill_sa_entry_t)(const struct llentry *, struct sockaddr *);
157 typedef void (llt_free_tbl_t)(struct lltable *);
158 typedef void (llt_link_entry_t)(struct lltable *, struct llentry *);
159 typedef void (llt_unlink_entry_t)(struct llentry *);
160
161 typedef int (llt_foreach_cb_t)(struct lltable *, struct llentry *, void *);
162 typedef int (llt_foreach_entry_t)(struct lltable *, llt_foreach_cb_t *, void *);
163
164 struct lltable {
165         SLIST_ENTRY(lltable)    llt_link;
166         int                     llt_af;
167         int                     llt_hsize;
168         struct llentries        *lle_head;
169         struct ifnet            *llt_ifp;
170
171         llt_lookup_t            *llt_lookup;
172         llt_alloc_t             *llt_alloc_entry;
173         llt_delete_t            *llt_delete_entry;
174         llt_prefix_free_t       *llt_prefix_free;
175         llt_dump_entry_t        *llt_dump_entry;
176         llt_hash_t              *llt_hash;
177         llt_match_prefix_t      *llt_match_prefix;
178         llt_free_entry_t        *llt_free_entry;
179         llt_foreach_entry_t     *llt_foreach_entry;
180         llt_link_entry_t        *llt_link_entry;
181         llt_unlink_entry_t      *llt_unlink_entry;
182         llt_fill_sa_entry_t     *llt_fill_sa_entry;
183         llt_free_tbl_t          *llt_free_tbl;
184 };
185
186 MALLOC_DECLARE(M_LLTABLE);
187
188 /*
189  * LLentry flags
190  */
191 #define LLE_DELETED     0x0001  /* entry must be deleted */
192 #define LLE_STATIC      0x0002  /* entry is static */
193 #define LLE_IFADDR      0x0004  /* entry is interface addr */
194 #define LLE_VALID       0x0008  /* ll_addr is valid */
195 #define LLE_REDIRECT    0x0010  /* installed by redirect; has host rtentry */
196 #define LLE_PUB         0x0020  /* publish entry ??? */
197 #define LLE_LINKED      0x0040  /* linked to lookup structure */
198 /* LLE request flags */
199 #define LLE_EXCLUSIVE   0x2000  /* return lle xlocked  */
200 #define LLE_UNLOCKED    0x4000  /* return lle unlocked */
201
202 /* LLE flags used by fastpath code */
203 #define RLLE_VALID      0x0001          /* entry is valid */
204 #define RLLE_IFADDR     LLE_IFADDR      /* entry is ifaddr */
205
206 #define LLATBL_HASH(key, mask) \
207         (((((((key >> 8) ^ key) >> 8) ^ key) >> 8) ^ key) & mask)
208
209 struct lltable *lltable_allocate_htbl(uint32_t hsize);
210 void            lltable_free(struct lltable *);
211 void            lltable_link(struct lltable *llt);
212 void            lltable_prefix_free(int, struct sockaddr *,
213                     struct sockaddr *, u_int);
214 #if 0
215 void            lltable_drain(int);
216 #endif
217 int             lltable_sysctl_dumparp(int, struct sysctl_req *);
218
219 size_t          llentry_free(struct llentry *);
220 struct llentry  *llentry_alloc(struct ifnet *, struct lltable *,
221                     struct sockaddr_storage *);
222
223 /* helper functions */
224 size_t lltable_drop_entry_queue(struct llentry *);
225 void lltable_set_entry_addr(struct ifnet *ifp, struct llentry *lle,
226     const char *lladdr);
227 int lltable_try_set_entry_addr(struct ifnet *ifp, struct llentry *lle,
228     const char *lladdr);
229
230 struct llentry *lltable_alloc_entry(struct lltable *llt, u_int flags,
231     const struct sockaddr *l4addr);
232 void lltable_free_entry(struct lltable *llt, struct llentry *lle);
233 int lltable_delete_addr(struct lltable *llt, u_int flags,
234     const struct sockaddr *l3addr);
235 void lltable_link_entry(struct lltable *llt, struct llentry *lle);
236 void lltable_unlink_entry(struct lltable *llt, struct llentry *lle);
237 void lltable_fill_sa_entry(const struct llentry *lle, struct sockaddr *sa);
238 struct ifnet *lltable_get_ifp(const struct lltable *llt);
239 int lltable_get_af(const struct lltable *llt);
240
241 int lltable_foreach_lle(struct lltable *llt, llt_foreach_cb_t *f,
242     void *farg);
243 /*
244  * Generic link layer address lookup function.
245  */
246 static __inline struct llentry *
247 lla_lookup(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
248 {
249
250         return (llt->llt_lookup(llt, flags, l3addr));
251 }
252
253 int             lla_rt_output(struct rt_msghdr *, struct rt_addrinfo *);
254
255 #include <sys/eventhandler.h>
256 enum {
257         LLENTRY_RESOLVED,
258         LLENTRY_TIMEDOUT,
259         LLENTRY_DELETED,
260         LLENTRY_EXPIRED,
261 };
262 typedef void (*lle_event_fn)(void *, struct llentry *, int);
263 EVENTHANDLER_DECLARE(lle_event, lle_event_fn);
264 #endif  /* _NET_IF_LLATBL_H_ */