cxgb_l2t.c revision 1.5 1 1.1 jklos /**************************************************************************
2 1.1 jklos
3 1.1 jklos Copyright (c) 2007, Chelsio Inc.
4 1.1 jklos All rights reserved.
5 1.1 jklos
6 1.1 jklos Redistribution and use in source and binary forms, with or without
7 1.1 jklos modification, are permitted provided that the following conditions are met:
8 1.1 jklos
9 1.1 jklos 1. Redistributions of source code must retain the above copyright notice,
10 1.1 jklos this list of conditions and the following disclaimer.
11 1.1 jklos
12 1.1 jklos 2. Neither the name of the Chelsio Corporation nor the names of its
13 1.1 jklos contributors may be used to endorse or promote products derived from
14 1.1 jklos this software without specific prior written permission.
15 1.1 jklos
16 1.1 jklos THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 1.1 jklos AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.1 jklos IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.1 jklos ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20 1.1 jklos LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1 jklos CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1 jklos SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1 jklos INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1 jklos CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1 jklos ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1 jklos POSSIBILITY OF SUCH DAMAGE.
27 1.1 jklos
28 1.1 jklos ***************************************************************************/
29 1.1 jklos
30 1.1 jklos #include <sys/cdefs.h>
31 1.5 maxv __KERNEL_RCSID(0, "$NetBSD: cxgb_l2t.c,v 1.5 2018/12/22 14:28:56 maxv Exp $");
32 1.1 jklos
33 1.1 jklos #include <sys/param.h>
34 1.1 jklos #include <sys/systm.h>
35 1.1 jklos #include <sys/kernel.h>
36 1.1 jklos #include <sys/lock.h>
37 1.1 jklos #include <sys/mutex.h>
38 1.1 jklos
39 1.1 jklos #include <sys/socket.h>
40 1.1 jklos #include <sys/socketvar.h>
41 1.1 jklos #include <net/if.h>
42 1.1 jklos #include <netinet/in.h>
43 1.1 jklos #include <netinet/in_var.h>
44 1.1 jklos #include <netinet/if_inarp.h>
45 1.1 jklos #include <net/if_dl.h>
46 1.1 jklos #include <net/route.h>
47 1.1 jklos #include <netinet/in.h>
48 1.1 jklos
49 1.1 jklos #ifdef CONFIG_DEFINED
50 1.1 jklos #include <cxgb_include.h>
51 1.1 jklos #else
52 1.1 jklos #include "cxgb_include.h"
53 1.1 jklos #endif
54 1.1 jklos
55 1.1 jklos #define VLAN_NONE 0xfff
56 1.1 jklos #define SDL(s) ((struct sockaddr_dl *)s)
57 1.1 jklos #define RT_ENADDR(rt) ((u_char *)LLADDR(SDL((rt))))
58 1.1 jklos #define rt_expire rt_rmx.rmx_expire
59 1.1 jklos
60 1.1 jklos /*
61 1.1 jklos * Module locking notes: There is a RW lock protecting the L2 table as a
62 1.1 jklos * whole plus a spinlock per L2T entry. Entry lookups and allocations happen
63 1.1 jklos * under the protection of the table lock, individual entry changes happen
64 1.1 jklos * while holding that entry's spinlock. The table lock nests outside the
65 1.1 jklos * entry locks. Allocations of new entries take the table lock as writers so
66 1.1 jklos * no other lookups can happen while allocating new entries. Entry updates
67 1.1 jklos * take the table lock as readers so multiple entries can be updated in
68 1.1 jklos * parallel. An L2T entry can be dropped by decrementing its reference count
69 1.1 jklos * and therefore can happen in parallel with entry allocation but no entry
70 1.1 jklos * can change state or increment its ref count during allocation as both of
71 1.1 jklos * these perform lookups.
72 1.1 jklos */
73 1.1 jklos
74 1.1 jklos static inline unsigned int
75 1.1 jklos vlan_prio(const struct l2t_entry *e)
76 1.1 jklos {
77 1.1 jklos return e->vlan >> 13;
78 1.1 jklos }
79 1.1 jklos
80 1.1 jklos static inline unsigned int
81 1.1 jklos arp_hash(u32 key, int ifindex, const struct l2t_data *d)
82 1.1 jklos {
83 1.1 jklos return jhash_2words(key, ifindex, 0) & (d->nentries - 1);
84 1.1 jklos }
85 1.1 jklos
86 1.1 jklos static inline void
87 1.1 jklos neigh_replace(struct l2t_entry *e, struct rtentry *rt)
88 1.1 jklos {
89 1.1 jklos RT_LOCK(rt);
90 1.1 jklos RT_ADDREF(rt);
91 1.1 jklos RT_UNLOCK(rt);
92 1.1 jklos
93 1.1 jklos if (e->neigh) {
94 1.1 jklos RT_LOCK(e->neigh);
95 1.1 jklos RT_REMREF(e->neigh);
96 1.1 jklos RT_UNLOCK(e->neigh);
97 1.1 jklos }
98 1.1 jklos e->neigh = rt;
99 1.1 jklos }
100 1.1 jklos
101 1.1 jklos /*
102 1.1 jklos * Set up an L2T entry and send any packets waiting in the arp queue. The
103 1.1 jklos * supplied mbuf is used for the CPL_L2T_WRITE_REQ. Must be called with the
104 1.1 jklos * entry locked.
105 1.1 jklos */
106 1.1 jklos static int
107 1.1 jklos setup_l2e_send_pending(struct toedev *dev, struct mbuf *m,
108 1.1 jklos struct l2t_entry *e)
109 1.1 jklos {
110 1.1 jklos struct cpl_l2t_write_req *req;
111 1.1 jklos
112 1.1 jklos if (!m) {
113 1.1 jklos if ((m = m_gethdr(M_NOWAIT, MT_DATA)) == NULL)
114 1.1 jklos return (ENOMEM);
115 1.1 jklos }
116 1.1 jklos /*
117 1.5 maxv * XXX m_align
118 1.1 jklos */
119 1.1 jklos req = mtod(m, struct cpl_l2t_write_req *);
120 1.1 jklos req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
121 1.1 jklos OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_L2T_WRITE_REQ, e->idx));
122 1.1 jklos req->params = htonl(V_L2T_W_IDX(e->idx) | V_L2T_W_IFF(e->smt_idx) |
123 1.1 jklos V_L2T_W_VLAN(e->vlan & EVL_VLID_MASK) |
124 1.1 jklos V_L2T_W_PRIO(vlan_prio(e)));
125 1.1 jklos
126 1.1 jklos memcpy(e->dmac, RT_ENADDR(e->neigh), sizeof(e->dmac));
127 1.1 jklos memcpy(req->dst_mac, e->dmac, sizeof(req->dst_mac));
128 1.1 jklos m_set_priority(m, CPL_PRIORITY_CONTROL);
129 1.1 jklos while (e->arpq_head) {
130 1.4 maxv m = e->arpq_head; /* XXX XXX XXX: Memory leak? */
131 1.1 jklos e->arpq_head = m->m_next;
132 1.1 jklos m->m_next = NULL;
133 1.1 jklos }
134 1.1 jklos e->arpq_tail = NULL;
135 1.1 jklos e->state = L2T_STATE_VALID;
136 1.1 jklos
137 1.1 jklos return 0;
138 1.1 jklos }
139 1.1 jklos
140 1.1 jklos /*
141 1.1 jklos * Add a packet to the an L2T entry's queue of packets awaiting resolution.
142 1.1 jklos * Must be called with the entry's lock held.
143 1.1 jklos */
144 1.1 jklos static inline void
145 1.1 jklos arpq_enqueue(struct l2t_entry *e, struct mbuf *m)
146 1.1 jklos {
147 1.1 jklos m->m_next = NULL;
148 1.1 jklos if (e->arpq_head)
149 1.1 jklos e->arpq_tail->m_next = m;
150 1.1 jklos else
151 1.1 jklos e->arpq_head = m;
152 1.1 jklos e->arpq_tail = m;
153 1.1 jklos }
154 1.1 jklos
155 1.1 jklos int
156 1.1 jklos t3_l2t_send_slow(struct toedev *dev, struct mbuf *m,
157 1.1 jklos struct l2t_entry *e)
158 1.1 jklos {
159 1.1 jklos struct rtentry *rt;
160 1.1 jklos struct mbuf *m0;
161 1.1 jklos
162 1.1 jklos if ((m0 = m_gethdr(M_NOWAIT, MT_DATA)) == NULL)
163 1.1 jklos return (ENOMEM);
164 1.1 jklos
165 1.1 jklos rt = e->neigh;
166 1.1 jklos
167 1.1 jklos again:
168 1.1 jklos switch (e->state) {
169 1.1 jklos case L2T_STATE_STALE: /* entry is stale, kick off revalidation */
170 1.1 jklos arpresolve(rt->rt_ifp, rt, m0, rt->rt_gateway, RT_ENADDR(rt));
171 1.1 jklos mtx_lock(&e->lock);
172 1.1 jklos if (e->state == L2T_STATE_STALE)
173 1.1 jklos e->state = L2T_STATE_VALID;
174 1.1 jklos mtx_unlock(&e->lock);
175 1.1 jklos case L2T_STATE_VALID: /* fast-path, send the packet on */
176 1.1 jklos case L2T_STATE_RESOLVING:
177 1.1 jklos mtx_lock(&e->lock);
178 1.1 jklos if (e->state != L2T_STATE_RESOLVING) { // ARP already completed
179 1.1 jklos mtx_unlock(&e->lock);
180 1.1 jklos goto again;
181 1.1 jklos }
182 1.1 jklos arpq_enqueue(e, m);
183 1.1 jklos mtx_unlock(&e->lock);
184 1.1 jklos
185 1.4 maxv /* XXX XXX XXX: Memory leak? */
186 1.1 jklos if ((m0 = m_gethdr(M_NOWAIT, MT_DATA)) == NULL)
187 1.1 jklos return (ENOMEM);
188 1.1 jklos /*
189 1.1 jklos * Only the first packet added to the arpq should kick off
190 1.1 jklos * resolution. However, because the m_gethdr below can fail,
191 1.1 jklos * we allow each packet added to the arpq to retry resolution
192 1.1 jklos * as a way of recovering from transient memory exhaustion.
193 1.1 jklos * A better way would be to use a work request to retry L2T
194 1.1 jklos * entries when there's no memory.
195 1.1 jklos */
196 1.1 jklos if (arpresolve(rt->rt_ifp, rt, m0, rt->rt_gateway, RT_ENADDR(rt)) == 0) {
197 1.1 jklos
198 1.1 jklos mtx_lock(&e->lock);
199 1.1 jklos if (e->arpq_head)
200 1.1 jklos setup_l2e_send_pending(dev, m, e);
201 1.1 jklos else
202 1.1 jklos m_freem(m);
203 1.1 jklos mtx_unlock(&e->lock);
204 1.1 jklos }
205 1.1 jklos }
206 1.1 jklos return 0;
207 1.1 jklos }
208 1.1 jklos
209 1.1 jklos void
210 1.1 jklos t3_l2t_send_event(struct toedev *dev, struct l2t_entry *e)
211 1.1 jklos {
212 1.1 jklos struct rtentry *rt;
213 1.1 jklos struct mbuf *m0;
214 1.1 jklos
215 1.1 jklos if ((m0 = m_gethdr(M_NOWAIT, MT_DATA)) == NULL)
216 1.1 jklos return;
217 1.1 jklos
218 1.1 jklos rt = e->neigh;
219 1.1 jklos again:
220 1.1 jklos switch (e->state) {
221 1.1 jklos case L2T_STATE_STALE: /* entry is stale, kick off revalidation */
222 1.1 jklos arpresolve(rt->rt_ifp, rt, m0, rt->rt_gateway, RT_ENADDR(rt));
223 1.1 jklos mtx_lock(&e->lock);
224 1.1 jklos if (e->state == L2T_STATE_STALE) {
225 1.1 jklos e->state = L2T_STATE_VALID;
226 1.1 jklos }
227 1.1 jklos mtx_unlock(&e->lock);
228 1.1 jklos return;
229 1.1 jklos case L2T_STATE_VALID: /* fast-path, send the packet on */
230 1.1 jklos return;
231 1.1 jklos case L2T_STATE_RESOLVING:
232 1.1 jklos mtx_lock(&e->lock);
233 1.1 jklos if (e->state != L2T_STATE_RESOLVING) { // ARP already completed
234 1.1 jklos mtx_unlock(&e->lock);
235 1.1 jklos goto again;
236 1.1 jklos }
237 1.1 jklos mtx_unlock(&e->lock);
238 1.1 jklos
239 1.1 jklos if ((m0 = m_gethdr(M_NOWAIT, MT_DATA)) == NULL)
240 1.1 jklos return;
241 1.1 jklos /*
242 1.1 jklos * Only the first packet added to the arpq should kick off
243 1.1 jklos * resolution. However, because the alloc_skb below can fail,
244 1.1 jklos * we allow each packet added to the arpq to retry resolution
245 1.1 jklos * as a way of recovering from transient memory exhaustion.
246 1.1 jklos * A better way would be to use a work request to retry L2T
247 1.1 jklos * entries when there's no memory.
248 1.1 jklos */
249 1.1 jklos arpresolve(rt->rt_ifp, rt, m0, rt->rt_gateway, RT_ENADDR(rt));
250 1.1 jklos
251 1.1 jklos }
252 1.1 jklos return;
253 1.1 jklos }
254 1.1 jklos /*
255 1.1 jklos * Allocate a free L2T entry. Must be called with l2t_data.lock held.
256 1.1 jklos */
257 1.1 jklos static struct l2t_entry *
258 1.1 jklos alloc_l2e(struct l2t_data *d)
259 1.1 jklos {
260 1.1 jklos struct l2t_entry *end, *e, **p;
261 1.1 jklos
262 1.1 jklos if (!atomic_load_acq_int(&d->nfree))
263 1.1 jklos return NULL;
264 1.1 jklos
265 1.1 jklos /* there's definitely a free entry */
266 1.1 jklos for (e = d->rover, end = &d->l2tab[d->nentries]; e != end; ++e)
267 1.1 jklos if (atomic_load_acq_int(&e->refcnt) == 0)
268 1.1 jklos goto found;
269 1.1 jklos
270 1.1 jklos for (e = &d->l2tab[1]; atomic_load_acq_int(&e->refcnt); ++e) ;
271 1.1 jklos found:
272 1.1 jklos d->rover = e + 1;
273 1.1 jklos atomic_add_int(&d->nfree, -1);
274 1.1 jklos
275 1.1 jklos /*
276 1.1 jklos * The entry we found may be an inactive entry that is
277 1.1 jklos * presently in the hash table. We need to remove it.
278 1.1 jklos */
279 1.1 jklos if (e->state != L2T_STATE_UNUSED) {
280 1.1 jklos int hash = arp_hash(e->addr, e->ifindex, d);
281 1.1 jklos
282 1.1 jklos for (p = &d->l2tab[hash].first; *p; p = &(*p)->next)
283 1.1 jklos if (*p == e) {
284 1.1 jklos *p = e->next;
285 1.1 jklos break;
286 1.1 jklos }
287 1.1 jklos e->state = L2T_STATE_UNUSED;
288 1.1 jklos }
289 1.1 jklos return e;
290 1.1 jklos }
291 1.1 jklos
292 1.1 jklos /*
293 1.1 jklos * Called when an L2T entry has no more users. The entry is left in the hash
294 1.1 jklos * table since it is likely to be reused but we also bump nfree to indicate
295 1.1 jklos * that the entry can be reallocated for a different neighbor. We also drop
296 1.1 jklos * the existing neighbor reference in case the neighbor is going away and is
297 1.1 jklos * waiting on our reference.
298 1.1 jklos *
299 1.1 jklos * Because entries can be reallocated to other neighbors once their ref count
300 1.1 jklos * drops to 0 we need to take the entry's lock to avoid races with a new
301 1.1 jklos * incarnation.
302 1.1 jklos */
303 1.1 jklos void
304 1.1 jklos t3_l2e_free(struct l2t_data *d, struct l2t_entry *e)
305 1.1 jklos {
306 1.1 jklos mtx_lock(&e->lock);
307 1.1 jklos if (atomic_load_acq_int(&e->refcnt) == 0) { /* hasn't been recycled */
308 1.1 jklos if (e->neigh) {
309 1.1 jklos RT_LOCK(e->neigh);
310 1.1 jklos RT_REMREF(e->neigh);
311 1.1 jklos RT_UNLOCK(e->neigh);
312 1.1 jklos e->neigh = NULL;
313 1.1 jklos }
314 1.1 jklos }
315 1.1 jklos mtx_unlock(&e->lock);
316 1.1 jklos atomic_add_int(&d->nfree, 1);
317 1.1 jklos }
318 1.1 jklos
319 1.1 jklos /*
320 1.1 jklos * Update an L2T entry that was previously used for the same next hop as neigh.
321 1.1 jklos * Must be called with softirqs disabled.
322 1.1 jklos */
323 1.1 jklos static inline void
324 1.1 jklos reuse_entry(struct l2t_entry *e, struct rtentry *neigh)
325 1.1 jklos {
326 1.1 jklos struct llinfo_arp *la;
327 1.1 jklos
328 1.1 jklos la = (struct llinfo_arp *)neigh->rt_llinfo;
329 1.1 jklos
330 1.1 jklos mtx_lock(&e->lock); /* avoid race with t3_l2t_free */
331 1.1 jklos if (neigh != e->neigh)
332 1.1 jklos neigh_replace(e, neigh);
333 1.1 jklos
334 1.1 jklos if (memcmp(e->dmac, RT_ENADDR(neigh), sizeof(e->dmac)) ||
335 1.1 jklos (neigh->rt_expire > time_uptime))
336 1.1 jklos e->state = L2T_STATE_RESOLVING;
337 1.1 jklos else if (la->la_hold == NULL)
338 1.1 jklos e->state = L2T_STATE_VALID;
339 1.1 jklos else
340 1.1 jklos e->state = L2T_STATE_STALE;
341 1.1 jklos mtx_unlock(&e->lock);
342 1.1 jklos }
343 1.1 jklos
344 1.1 jklos struct l2t_entry *
345 1.1 jklos t3_l2t_get(struct toedev *dev, struct rtentry *neigh,
346 1.1 jklos unsigned int smt_idx)
347 1.1 jklos {
348 1.1 jklos struct l2t_entry *e;
349 1.1 jklos struct l2t_data *d = L2DATA(dev);
350 1.2 joerg u32 addr = ((struct sockaddr_in *)rt_getkey(neigh))->sin_addr.s_addr;
351 1.1 jklos int ifidx = neigh->rt_ifp->if_index;
352 1.1 jklos int hash = arp_hash(addr, ifidx, d);
353 1.1 jklos
354 1.1 jklos rw_wlock(&d->lock);
355 1.1 jklos for (e = d->l2tab[hash].first; e; e = e->next)
356 1.1 jklos if (e->addr == addr && e->ifindex == ifidx &&
357 1.1 jklos e->smt_idx == smt_idx) {
358 1.1 jklos l2t_hold(d, e);
359 1.1 jklos if (atomic_load_acq_int(&e->refcnt) == 1)
360 1.1 jklos reuse_entry(e, neigh);
361 1.1 jklos goto done;
362 1.1 jklos }
363 1.1 jklos
364 1.1 jklos /* Need to allocate a new entry */
365 1.1 jklos e = alloc_l2e(d);
366 1.1 jklos if (e) {
367 1.1 jklos mtx_lock(&e->lock); /* avoid race with t3_l2t_free */
368 1.1 jklos e->next = d->l2tab[hash].first;
369 1.1 jklos d->l2tab[hash].first = e;
370 1.1 jklos e->state = L2T_STATE_RESOLVING;
371 1.1 jklos e->addr = addr;
372 1.1 jklos e->ifindex = ifidx;
373 1.1 jklos e->smt_idx = smt_idx;
374 1.1 jklos atomic_store_rel_int(&e->refcnt, 1);
375 1.1 jklos neigh_replace(e, neigh);
376 1.1 jklos #ifdef notyet
377 1.1 jklos /*
378 1.1 jklos * XXX need to add accessor function for vlan tag
379 1.1 jklos */
380 1.1 jklos if (neigh->rt_ifp->if_vlantrunk)
381 1.1 jklos e->vlan = VLAN_DEV_INFO(neigh->dev)->vlan_id;
382 1.1 jklos else
383 1.1 jklos #endif
384 1.1 jklos e->vlan = VLAN_NONE;
385 1.1 jklos mtx_unlock(&e->lock);
386 1.1 jklos }
387 1.1 jklos done:
388 1.1 jklos rw_wunlock(&d->lock);
389 1.1 jklos return e;
390 1.1 jklos }
391 1.1 jklos
392 1.1 jklos /*
393 1.1 jklos * Called when address resolution fails for an L2T entry to handle packets
394 1.1 jklos * on the arpq head. If a packet specifies a failure handler it is invoked,
395 1.1 jklos * otherwise the packets is sent to the TOE.
396 1.1 jklos *
397 1.1 jklos * XXX: maybe we should abandon the latter behavior and just require a failure
398 1.1 jklos * handler.
399 1.1 jklos */
400 1.1 jklos static void
401 1.1 jklos handle_failed_resolution(struct toedev *dev, struct mbuf *arpq)
402 1.1 jklos {
403 1.1 jklos
404 1.1 jklos while (arpq) {
405 1.1 jklos struct mbuf *m = arpq;
406 1.1 jklos #ifdef notyet
407 1.1 jklos struct l2t_mbuf_cb *cb = L2T_MBUF_CB(m);
408 1.1 jklos #endif
409 1.1 jklos arpq = m->m_next;
410 1.1 jklos m->m_next = NULL;
411 1.1 jklos #ifdef notyet
412 1.1 jklos if (cb->arp_failure_handler)
413 1.1 jklos cb->arp_failure_handler(dev, m);
414 1.1 jklos else
415 1.1 jklos #endif
416 1.1 jklos }
417 1.1 jklos
418 1.1 jklos }
419 1.1 jklos
420 1.1 jklos #if defined(NETEVENT) || !defined(CONFIG_CHELSIO_T3_MODULE)
421 1.1 jklos /*
422 1.1 jklos * Called when the host's ARP layer makes a change to some entry that is
423 1.1 jklos * loaded into the HW L2 table.
424 1.1 jklos */
425 1.1 jklos void
426 1.1 jklos t3_l2t_update(struct toedev *dev, struct rtentry *neigh)
427 1.1 jklos {
428 1.1 jklos struct l2t_entry *e;
429 1.1 jklos struct mbuf *arpq = NULL;
430 1.1 jklos struct l2t_data *d = L2DATA(dev);
431 1.2 joerg u32 addr = ((struct sockaddr_in *)rt_getkey(neigh))->sin_addr.s_addr;
432 1.1 jklos int ifidx = neigh->rt_ifp->if_index;
433 1.1 jklos int hash = arp_hash(addr, ifidx, d);
434 1.1 jklos struct llinfo_arp *la;
435 1.1 jklos
436 1.1 jklos rw_rlock(&d->lock);
437 1.1 jklos for (e = d->l2tab[hash].first; e; e = e->next)
438 1.1 jklos if (e->addr == addr && e->ifindex == ifidx) {
439 1.1 jklos mtx_lock(&e->lock);
440 1.1 jklos goto found;
441 1.1 jklos }
442 1.1 jklos rw_runlock(&d->lock);
443 1.1 jklos return;
444 1.1 jklos
445 1.1 jklos found:
446 1.1 jklos rw_runlock(&d->lock);
447 1.1 jklos if (atomic_load_acq_int(&e->refcnt)) {
448 1.1 jklos if (neigh != e->neigh)
449 1.1 jklos neigh_replace(e, neigh);
450 1.1 jklos
451 1.1 jklos la = (struct llinfo_arp *)neigh->rt_llinfo;
452 1.1 jklos if (e->state == L2T_STATE_RESOLVING) {
453 1.1 jklos
454 1.1 jklos if (la->la_asked >= 5 /* arp_maxtries */) {
455 1.1 jklos arpq = e->arpq_head;
456 1.1 jklos e->arpq_head = e->arpq_tail = NULL;
457 1.1 jklos } else if (la->la_hold == NULL)
458 1.1 jklos setup_l2e_send_pending(dev, NULL, e);
459 1.1 jklos } else {
460 1.1 jklos e->state = (la->la_hold == NULL) ?
461 1.1 jklos L2T_STATE_VALID : L2T_STATE_STALE;
462 1.1 jklos if (memcmp(e->dmac, RT_ENADDR(neigh), 6))
463 1.1 jklos setup_l2e_send_pending(dev, NULL, e);
464 1.1 jklos }
465 1.1 jklos }
466 1.1 jklos mtx_unlock(&e->lock);
467 1.1 jklos
468 1.1 jklos if (arpq)
469 1.1 jklos handle_failed_resolution(dev, arpq);
470 1.1 jklos }
471 1.1 jklos #else
472 1.1 jklos /*
473 1.1 jklos * Called from a kprobe, interrupts are off.
474 1.1 jklos */
475 1.1 jklos void
476 1.1 jklos t3_l2t_update(struct toedev *dev, struct rtentry *neigh)
477 1.1 jklos {
478 1.1 jklos struct l2t_entry *e;
479 1.1 jklos struct l2t_data *d = L2DATA(dev);
480 1.1 jklos u32 addr = *(u32 *) rt_key(neigh);
481 1.1 jklos int ifidx = neigh->dev->ifindex;
482 1.1 jklos int hash = arp_hash(addr, ifidx, d);
483 1.1 jklos
484 1.1 jklos rw_rlock(&d->lock);
485 1.1 jklos for (e = d->l2tab[hash].first; e; e = e->next)
486 1.1 jklos if (e->addr == addr && e->ifindex == ifidx) {
487 1.1 jklos mtx_lock(&e->lock);
488 1.1 jklos if (atomic_load_acq_int(&e->refcnt)) {
489 1.1 jklos if (neigh != e->neigh)
490 1.1 jklos neigh_replace(e, neigh);
491 1.1 jklos e->tdev = dev;
492 1.1 jklos mod_timer(&e->update_timer, jiffies + 1);
493 1.1 jklos }
494 1.1 jklos mtx_unlock(&e->lock);
495 1.1 jklos break;
496 1.1 jklos }
497 1.1 jklos rw_runlock(&d->lock);
498 1.1 jklos }
499 1.1 jklos
500 1.1 jklos static void
501 1.1 jklos update_timer_cb(unsigned long data)
502 1.1 jklos {
503 1.1 jklos struct mbuf *arpq = NULL;
504 1.1 jklos struct l2t_entry *e = (struct l2t_entry *)data;
505 1.1 jklos struct rtentry *neigh = e->neigh;
506 1.1 jklos struct toedev *dev = e->tdev;
507 1.1 jklos
508 1.1 jklos barrier();
509 1.1 jklos if (!atomic_load_acq_int(&e->refcnt))
510 1.1 jklos return;
511 1.1 jklos
512 1.1 jklos rw_rlock(&neigh->lock);
513 1.1 jklos mtx_lock(&e->lock);
514 1.1 jklos
515 1.1 jklos if (atomic_load_acq_int(&e->refcnt)) {
516 1.1 jklos if (e->state == L2T_STATE_RESOLVING) {
517 1.1 jklos if (neigh->nud_state & NUD_FAILED) {
518 1.1 jklos arpq = e->arpq_head;
519 1.1 jklos e->arpq_head = e->arpq_tail = NULL;
520 1.1 jklos } else if (neigh_is_connected(neigh) && e->arpq_head)
521 1.1 jklos setup_l2e_send_pending(dev, NULL, e);
522 1.1 jklos } else {
523 1.1 jklos e->state = neigh_is_connected(neigh) ?
524 1.1 jklos L2T_STATE_VALID : L2T_STATE_STALE;
525 1.1 jklos if (memcmp(e->dmac, RT_ENADDR(neigh), sizeof(e->dmac)))
526 1.1 jklos setup_l2e_send_pending(dev, NULL, e);
527 1.1 jklos }
528 1.1 jklos }
529 1.1 jklos mtx_unlock(&e->lock);
530 1.1 jklos rw_runlock(&neigh->lock);
531 1.1 jklos
532 1.1 jklos if (arpq)
533 1.1 jklos handle_failed_resolution(dev, arpq);
534 1.1 jklos }
535 1.1 jklos #endif
536 1.1 jklos
537 1.1 jklos struct l2t_data *
538 1.1 jklos t3_init_l2t(unsigned int l2t_capacity)
539 1.1 jklos {
540 1.1 jklos struct l2t_data *d;
541 1.1 jklos int i, size = sizeof(*d) + l2t_capacity * sizeof(struct l2t_entry);
542 1.1 jklos
543 1.1 jklos d = cxgb_alloc_mem(size);
544 1.1 jklos if (!d)
545 1.1 jklos return NULL;
546 1.1 jklos
547 1.1 jklos d->nentries = l2t_capacity;
548 1.1 jklos d->rover = &d->l2tab[1]; /* entry 0 is not used */
549 1.1 jklos atomic_store_rel_int(&d->nfree, l2t_capacity - 1);
550 1.1 jklos rw_init(&d->lock, "L2T");
551 1.1 jklos
552 1.1 jklos for (i = 0; i < l2t_capacity; ++i) {
553 1.1 jklos d->l2tab[i].idx = i;
554 1.1 jklos d->l2tab[i].state = L2T_STATE_UNUSED;
555 1.1 jklos mtx_init(&d->l2tab[i].lock, "L2TAB", NULL, MTX_DEF);
556 1.1 jklos atomic_store_rel_int(&d->l2tab[i].refcnt, 0);
557 1.1 jklos #ifndef NETEVENT
558 1.1 jklos #ifdef CONFIG_CHELSIO_T3_MODULE
559 1.1 jklos setup_timer(&d->l2tab[i].update_timer, update_timer_cb,
560 1.1 jklos (unsigned long)&d->l2tab[i]);
561 1.1 jklos #endif
562 1.1 jklos #endif
563 1.1 jklos }
564 1.1 jklos return d;
565 1.1 jklos }
566 1.1 jklos
567 1.1 jklos void
568 1.1 jklos t3_free_l2t(struct l2t_data *d)
569 1.1 jklos {
570 1.1 jklos #ifndef NETEVENT
571 1.1 jklos #ifdef CONFIG_CHELSIO_T3_MODULE
572 1.1 jklos int i;
573 1.1 jklos
574 1.1 jklos /* Stop all L2T timers */
575 1.1 jklos for (i = 0; i < d->nentries; ++i)
576 1.1 jklos del_timer_sync(&d->l2tab[i].update_timer);
577 1.1 jklos #endif
578 1.1 jklos #endif
579 1.1 jklos cxgb_free_mem(d);
580 1.1 jklos }
581 1.1 jklos
582 1.1 jklos #ifdef CONFIG_PROC_FS
583 1.1 jklos #include <linux/module.h>
584 1.1 jklos #include <linux/proc_fs.h>
585 1.1 jklos #include <linux/seq_file.h>
586 1.1 jklos
587 1.1 jklos static inline void *
588 1.1 jklos l2t_get_idx(struct seq_file *seq, loff_t pos)
589 1.1 jklos {
590 1.1 jklos struct l2t_data *d = seq->private;
591 1.1 jklos
592 1.1 jklos return pos >= d->nentries ? NULL : &d->l2tab[pos];
593 1.1 jklos }
594 1.1 jklos
595 1.1 jklos static void *
596 1.1 jklos l2t_seq_start(struct seq_file *seq, loff_t *pos)
597 1.1 jklos {
598 1.1 jklos return *pos ? l2t_get_idx(seq, *pos) : SEQ_START_TOKEN;
599 1.1 jklos }
600 1.1 jklos
601 1.1 jklos static void *
602 1.1 jklos l2t_seq_next(struct seq_file *seq, void *v, loff_t *pos)
603 1.1 jklos {
604 1.1 jklos v = l2t_get_idx(seq, *pos + 1);
605 1.1 jklos if (v)
606 1.1 jklos ++*pos;
607 1.1 jklos return v;
608 1.1 jklos }
609 1.1 jklos
610 1.1 jklos static void
611 1.1 jklos l2t_seq_stop(struct seq_file *seq, void *v)
612 1.1 jklos {
613 1.1 jklos }
614 1.1 jklos
615 1.1 jklos static char
616 1.1 jklos l2e_state(const struct l2t_entry *e)
617 1.1 jklos {
618 1.1 jklos switch (e->state) {
619 1.1 jklos case L2T_STATE_VALID: return 'V'; /* valid, fast-path entry */
620 1.1 jklos case L2T_STATE_STALE: return 'S'; /* needs revalidation, but usable */
621 1.1 jklos case L2T_STATE_RESOLVING:
622 1.1 jklos return e->arpq_head ? 'A' : 'R';
623 1.1 jklos default:
624 1.1 jklos return 'U';
625 1.1 jklos }
626 1.1 jklos }
627 1.1 jklos
628 1.1 jklos static int
629 1.1 jklos l2t_seq_show(struct seq_file *seq, void *v)
630 1.1 jklos {
631 1.1 jklos if (v == SEQ_START_TOKEN)
632 1.1 jklos seq_puts(seq, "Index IP address Ethernet address VLAN "
633 1.1 jklos "Prio State Users SMTIDX Port\n");
634 1.1 jklos else {
635 1.1 jklos char ip[20];
636 1.1 jklos struct l2t_entry *e = v;
637 1.1 jklos
638 1.1 jklos mtx_lock(&e->lock);
639 1.3 christos snprintf(ip, sizeof(ip), "%u.%u.%u.%u", NIPQUAD(e->addr));
640 1.1 jklos seq_printf(seq, "%-5u %-15s %02x:%02x:%02x:%02x:%02x:%02x %4d"
641 1.1 jklos " %3u %c %7u %4u %s\n",
642 1.1 jklos e->idx, ip, e->dmac[0], e->dmac[1], e->dmac[2],
643 1.1 jklos e->dmac[3], e->dmac[4], e->dmac[5],
644 1.1 jklos e->vlan & EVL_VLID_MASK, vlan_prio(e),
645 1.1 jklos l2e_state(e), atomic_load_acq_int(&e->refcnt), e->smt_idx,
646 1.1 jklos e->neigh ? e->neigh->dev->name : "");
647 1.1 jklos mtx_unlock(&e->lock);
648 1.1 jklos }
649 1.1 jklos return 0;
650 1.1 jklos }
651 1.1 jklos
652 1.1 jklos #endif
653