2 * Copyright (c) 2012 Chelsio Communications, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
30 #include "opt_inet6.h"
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
38 #include <sys/fnv_hash.h>
40 #include <sys/mutex.h>
41 #include <sys/rwlock.h>
42 #include <sys/socket.h>
44 #include <sys/taskqueue.h>
46 #include <net/if_types.h>
47 #include <net/ethernet.h>
48 #include <net/if_vlan_var.h>
49 #include <net/route.h>
50 #include <netinet/in.h>
51 #include <netinet/toecore.h>
53 #include "common/common.h"
54 #include "common/t4_msg.h"
55 #include "tom/t4_tom_l2t.h"
56 #include "tom/t4_tom.h"
58 #define VLAN_NONE 0xfff
61 l2t_hold(struct l2t_data *d, struct l2t_entry *e)
64 if (atomic_fetchadd_int(&e->refcnt, 1) == 0) /* 0 -> 1 transition */
65 atomic_subtract_int(&d->nfree, 1);
69 l2_hash(struct l2t_data *d, const struct sockaddr *sa, int ifindex)
71 u_int hash, half = d->l2t_size / 2, start = 0;
75 KASSERT(sa->sa_family == AF_INET || sa->sa_family == AF_INET6,
76 ("%s: sa %p has unexpected sa_family %d", __func__, sa,
79 if (sa->sa_family == AF_INET) {
80 const struct sockaddr_in *sin = (const void *)sa;
83 len = sizeof(sin->sin_addr);
85 const struct sockaddr_in6 *sin6 = (const void *)sa;
87 key = &sin6->sin6_addr;
88 len = sizeof(sin6->sin6_addr);
92 hash = fnv_32_buf(key, len, FNV1_32_INIT);
93 hash = fnv_32_buf(&ifindex, sizeof(ifindex), hash);
96 return (hash + start);
100 l2_cmp(const struct sockaddr *sa, struct l2t_entry *e)
103 KASSERT(sa->sa_family == AF_INET || sa->sa_family == AF_INET6,
104 ("%s: sa %p has unexpected sa_family %d", __func__, sa,
107 if (sa->sa_family == AF_INET) {
108 const struct sockaddr_in *sin = (const void *)sa;
110 return (e->addr[0] != sin->sin_addr.s_addr);
112 const struct sockaddr_in6 *sin6 = (const void *)sa;
114 return (memcmp(&e->addr[0], &sin6->sin6_addr, sizeof(e->addr)));
119 l2_store(const struct sockaddr *sa, struct l2t_entry *e)
122 KASSERT(sa->sa_family == AF_INET || sa->sa_family == AF_INET6,
123 ("%s: sa %p has unexpected sa_family %d", __func__, sa,
126 if (sa->sa_family == AF_INET) {
127 const struct sockaddr_in *sin = (const void *)sa;
129 e->addr[0] = sin->sin_addr.s_addr;
132 const struct sockaddr_in6 *sin6 = (const void *)sa;
134 memcpy(&e->addr[0], &sin6->sin6_addr, sizeof(e->addr));
140 * Add a WR to an L2T entry's queue of work requests awaiting resolution.
141 * Must be called with the entry's lock held.
144 arpq_enqueue(struct l2t_entry *e, struct wrqe *wr)
146 mtx_assert(&e->lock, MA_OWNED);
148 STAILQ_INSERT_TAIL(&e->wr_list, wr, link);
152 send_pending(struct adapter *sc, struct l2t_entry *e)
156 mtx_assert(&e->lock, MA_OWNED);
158 while ((wr = STAILQ_FIRST(&e->wr_list)) != NULL) {
159 STAILQ_REMOVE_HEAD(&e->wr_list, link);
165 resolution_failed(struct adapter *sc, struct l2t_entry *e)
167 struct tom_data *td = sc->tom_softc;
169 mtx_assert(&e->lock, MA_OWNED);
171 mtx_lock(&td->unsent_wr_lock);
172 STAILQ_CONCAT(&td->unsent_wr_list, &e->wr_list);
173 mtx_unlock(&td->unsent_wr_lock);
175 taskqueue_enqueue(taskqueue_thread, &td->reclaim_wr_resources);
179 update_entry(struct adapter *sc, struct l2t_entry *e, uint8_t *lladdr,
183 mtx_assert(&e->lock, MA_OWNED);
186 * The entry may be in active use (e->refcount > 0) or not. We update
187 * it even when it's not as this simplifies the case where we decide to
188 * reuse the entry later.
191 if (lladdr == NULL &&
192 (e->state == L2T_STATE_RESOLVING || e->state == L2T_STATE_FAILED)) {
194 * Never got a valid L2 address for this one. Just mark it as
195 * failed instead of removing it from the hash (for which we'd
196 * need to wlock the table).
198 e->state = L2T_STATE_FAILED;
199 resolution_failed(sc, e);
202 } else if (lladdr == NULL) {
204 /* Valid or already-stale entry was deleted (or expired) */
206 KASSERT(e->state == L2T_STATE_VALID ||
207 e->state == L2T_STATE_STALE,
208 ("%s: lladdr NULL, state %d", __func__, e->state));
210 e->state = L2T_STATE_STALE;
214 if (e->state == L2T_STATE_RESOLVING ||
215 e->state == L2T_STATE_FAILED ||
216 memcmp(e->dmac, lladdr, ETHER_ADDR_LEN)) {
218 /* unresolved -> resolved; or dmac changed */
220 memcpy(e->dmac, lladdr, ETHER_ADDR_LEN);
222 t4_write_l2e(sc, e, 1);
224 e->state = L2T_STATE_VALID;
229 resolve_entry(struct adapter *sc, struct l2t_entry *e)
231 struct tom_data *td = sc->tom_softc;
232 struct toedev *tod = &td->tod;
233 struct sockaddr_in sin = {0};
234 struct sockaddr_in6 sin6 = {0};
236 uint8_t dmac[ETHER_HDR_LEN];
237 uint16_t vtag = VLAN_NONE;
241 sin.sin_family = AF_INET;
242 sin.sin_len = sizeof(struct sockaddr_in);
243 sin.sin_addr.s_addr = e->addr[0];
246 sin6.sin6_family = AF_INET6;
247 sin6.sin6_len = sizeof(struct sockaddr_in6);
248 memcpy(&sin6.sin6_addr, &e->addr[0], sizeof(e->addr));
252 rc = toe_l2_resolve(tod, e->ifp, sa, dmac, &vtag);
253 if (rc == EWOULDBLOCK)
257 update_entry(sc, e, rc == 0 ? dmac : NULL, vtag);
258 mtx_unlock(&e->lock);
264 t4_l2t_send_slow(struct adapter *sc, struct wrqe *wr, struct l2t_entry *e)
269 case L2T_STATE_STALE: /* entry is stale, kick off revalidation */
271 if (resolve_entry(sc, e) != EWOULDBLOCK)
272 goto again; /* entry updated, re-examine state */
276 case L2T_STATE_VALID: /* fast-path, send the packet on */
281 case L2T_STATE_RESOLVING:
282 case L2T_STATE_SYNC_WRITE:
285 if (e->state != L2T_STATE_SYNC_WRITE &&
286 e->state != L2T_STATE_RESOLVING) {
287 /* state changed by the time we got here */
288 mtx_unlock(&e->lock);
292 mtx_unlock(&e->lock);
294 if (resolve_entry(sc, e) == EWOULDBLOCK)
298 if (e->state == L2T_STATE_VALID && !STAILQ_EMPTY(&e->wr_list))
300 if (e->state == L2T_STATE_FAILED)
301 resolution_failed(sc, e);
302 mtx_unlock(&e->lock);
305 case L2T_STATE_FAILED:
306 return (EHOSTUNREACH);
313 * Called when an L2T entry has no more users. The entry is left in the hash
314 * table since it is likely to be reused but we also bump nfree to indicate
315 * that the entry can be reallocated for a different neighbor. We also drop
316 * the existing neighbor reference in case the neighbor is going away and is
317 * waiting on our reference.
319 * Because entries can be reallocated to other neighbors once their ref count
320 * drops to 0 we need to take the entry's lock to avoid races with a new
325 do_l2t_write_rpl2(struct sge_iq *iq, const struct rss_header *rss,
328 struct adapter *sc = iq->adapter;
329 const struct cpl_l2t_write_rpl *rpl = (const void *)(rss + 1);
330 unsigned int tid = GET_TID(rpl);
331 unsigned int idx = tid % L2T_SIZE;
334 rc = do_l2t_write_rpl(iq, rss, m);
338 if (tid & F_SYNC_WR) {
339 struct l2t_entry *e = &sc->l2t->l2tab[idx - sc->vres.l2t.start];
342 if (e->state != L2T_STATE_SWITCHING) {
344 e->state = L2T_STATE_VALID;
346 mtx_unlock(&e->lock);
353 t4_init_l2t_cpl_handlers(struct adapter *sc)
356 t4_register_cpl_handler(sc, CPL_L2T_WRITE_RPL, do_l2t_write_rpl2);
360 t4_uninit_l2t_cpl_handlers(struct adapter *sc)
363 t4_register_cpl_handler(sc, CPL_L2T_WRITE_RPL, do_l2t_write_rpl);
367 * The TOE wants an L2 table entry that it can use to reach the next hop over
368 * the specified port. Produce such an entry - create one if needed.
370 * Note that the ifnet could be a pseudo-device like if_vlan, if_lagg, etc. on
371 * top of the real cxgbe interface.
374 t4_l2t_get(struct port_info *pi, struct ifnet *ifp, struct sockaddr *sa)
377 struct l2t_data *d = pi->adapter->l2t;
378 u_int hash, smt_idx = pi->port_id;
380 KASSERT(sa->sa_family == AF_INET || sa->sa_family == AF_INET6,
381 ("%s: sa %p has unexpected sa_family %d", __func__, sa,
385 if (ifp->if_type == IFT_L2VLAN)
389 hash = l2_hash(d, sa, ifp->if_index);
391 for (e = d->l2tab[hash].first; e; e = e->next) {
392 if (l2_cmp(sa, e) == 0 && e->ifp == ifp &&
393 e->smt_idx == smt_idx) {
399 /* Need to allocate a new entry */
402 mtx_lock(&e->lock); /* avoid race with t4_l2t_free */
403 e->next = d->l2tab[hash].first;
404 d->l2tab[hash].first = e;
406 e->state = L2T_STATE_RESOLVING;
409 e->smt_idx = smt_idx;
411 e->lport = pi->lport;
412 atomic_store_rel_int(&e->refcnt, 1);
414 if (ifp->if_type == IFT_L2VLAN)
415 VLAN_TAG(ifp, &e->vlan);
419 mtx_unlock(&e->lock);
422 rw_wunlock(&d->lock);
427 * Called when the host's ARP layer makes a change to some entry that is loaded
428 * into the HW L2 table.
431 t4_l2_update(struct toedev *tod, struct ifnet *ifp, struct sockaddr *sa,
432 uint8_t *lladdr, uint16_t vtag)
434 struct adapter *sc = tod->tod_softc;
436 struct l2t_data *d = sc->l2t;
439 KASSERT(d != NULL, ("%s: no L2 table", __func__));
441 hash = l2_hash(d, sa, ifp->if_index);
443 for (e = d->l2tab[hash].first; e; e = e->next) {
444 if (l2_cmp(sa, e) == 0 && e->ifp == ifp) {
446 if (atomic_load_acq_int(&e->refcnt))
448 e->state = L2T_STATE_STALE;
449 mtx_unlock(&e->lock);
453 rw_runlock(&d->lock);
456 * This is of no interest to us. We've never had an offloaded
457 * connection to this destination, and we aren't attempting one right
463 rw_runlock(&d->lock);
465 KASSERT(e->state != L2T_STATE_UNUSED,
466 ("%s: unused entry in the hash.", __func__));
468 update_entry(sc, e, lladdr, vtag);
469 mtx_unlock(&e->lock);