2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2012 Chelsio Communications, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
32 #include "opt_inet6.h"
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/module.h>
40 #include <sys/fnv_hash.h>
42 #include <sys/mutex.h>
43 #include <sys/rwlock.h>
44 #include <sys/socket.h>
46 #include <sys/taskqueue.h>
48 #include <net/if_types.h>
49 #include <net/ethernet.h>
50 #include <net/if_vlan_var.h>
51 #include <net/route.h>
52 #include <netinet/in.h>
53 #include <netinet/toecore.h>
55 #include "common/common.h"
56 #include "common/t4_msg.h"
57 #include "tom/t4_tom_l2t.h"
58 #include "tom/t4_tom.h"
60 #define VLAN_NONE 0xfff
63 l2t_hold(struct l2t_data *d, struct l2t_entry *e)
66 if (atomic_fetchadd_int(&e->refcnt, 1) == 0) /* 0 -> 1 transition */
67 atomic_subtract_int(&d->nfree, 1);
71 l2_hash(struct l2t_data *d, const struct sockaddr *sa, int ifindex)
73 u_int hash, half = d->l2t_size / 2, start = 0;
77 KASSERT(sa->sa_family == AF_INET || sa->sa_family == AF_INET6,
78 ("%s: sa %p has unexpected sa_family %d", __func__, sa,
81 if (sa->sa_family == AF_INET) {
82 const struct sockaddr_in *sin = (const void *)sa;
85 len = sizeof(sin->sin_addr);
87 const struct sockaddr_in6 *sin6 = (const void *)sa;
89 key = &sin6->sin6_addr;
90 len = sizeof(sin6->sin6_addr);
94 hash = fnv_32_buf(key, len, FNV1_32_INIT);
95 hash = fnv_32_buf(&ifindex, sizeof(ifindex), hash);
98 return (hash + start);
102 l2_cmp(const struct sockaddr *sa, struct l2t_entry *e)
105 KASSERT(sa->sa_family == AF_INET || sa->sa_family == AF_INET6,
106 ("%s: sa %p has unexpected sa_family %d", __func__, sa,
109 if (sa->sa_family == AF_INET) {
110 const struct sockaddr_in *sin = (const void *)sa;
112 return (e->addr[0] != sin->sin_addr.s_addr);
114 const struct sockaddr_in6 *sin6 = (const void *)sa;
116 return (memcmp(&e->addr[0], &sin6->sin6_addr, sizeof(e->addr)));
121 l2_store(const struct sockaddr *sa, struct l2t_entry *e)
124 KASSERT(sa->sa_family == AF_INET || sa->sa_family == AF_INET6,
125 ("%s: sa %p has unexpected sa_family %d", __func__, sa,
128 if (sa->sa_family == AF_INET) {
129 const struct sockaddr_in *sin = (const void *)sa;
131 e->addr[0] = sin->sin_addr.s_addr;
134 const struct sockaddr_in6 *sin6 = (const void *)sa;
136 memcpy(&e->addr[0], &sin6->sin6_addr, sizeof(e->addr));
142 * Add a WR to an L2T entry's queue of work requests awaiting resolution.
143 * Must be called with the entry's lock held.
146 arpq_enqueue(struct l2t_entry *e, struct wrqe *wr)
148 mtx_assert(&e->lock, MA_OWNED);
150 STAILQ_INSERT_TAIL(&e->wr_list, wr, link);
154 send_pending(struct adapter *sc, struct l2t_entry *e)
158 mtx_assert(&e->lock, MA_OWNED);
160 while ((wr = STAILQ_FIRST(&e->wr_list)) != NULL) {
161 STAILQ_REMOVE_HEAD(&e->wr_list, link);
167 resolution_failed(struct adapter *sc, struct l2t_entry *e)
169 struct tom_data *td = sc->tom_softc;
171 mtx_assert(&e->lock, MA_OWNED);
173 mtx_lock(&td->unsent_wr_lock);
174 STAILQ_CONCAT(&td->unsent_wr_list, &e->wr_list);
175 mtx_unlock(&td->unsent_wr_lock);
177 taskqueue_enqueue(taskqueue_thread, &td->reclaim_wr_resources);
181 update_entry(struct adapter *sc, struct l2t_entry *e, uint8_t *lladdr,
185 mtx_assert(&e->lock, MA_OWNED);
188 * The entry may be in active use (e->refcount > 0) or not. We update
189 * it even when it's not as this simplifies the case where we decide to
190 * reuse the entry later.
193 if (lladdr == NULL &&
194 (e->state == L2T_STATE_RESOLVING || e->state == L2T_STATE_FAILED)) {
196 * Never got a valid L2 address for this one. Just mark it as
197 * failed instead of removing it from the hash (for which we'd
198 * need to wlock the table).
200 e->state = L2T_STATE_FAILED;
201 resolution_failed(sc, e);
204 } else if (lladdr == NULL) {
206 /* Valid or already-stale entry was deleted (or expired) */
208 KASSERT(e->state == L2T_STATE_VALID ||
209 e->state == L2T_STATE_STALE,
210 ("%s: lladdr NULL, state %d", __func__, e->state));
212 e->state = L2T_STATE_STALE;
216 if (e->state == L2T_STATE_RESOLVING ||
217 e->state == L2T_STATE_FAILED ||
218 memcmp(e->dmac, lladdr, ETHER_ADDR_LEN)) {
220 /* unresolved -> resolved; or dmac changed */
222 memcpy(e->dmac, lladdr, ETHER_ADDR_LEN);
226 e->state = L2T_STATE_VALID;
231 resolve_entry(struct adapter *sc, struct l2t_entry *e)
233 struct tom_data *td = sc->tom_softc;
234 struct toedev *tod = &td->tod;
235 struct sockaddr_in sin = {0};
236 struct sockaddr_in6 sin6 = {0};
238 uint8_t dmac[ETHER_HDR_LEN];
243 sin.sin_family = AF_INET;
244 sin.sin_len = sizeof(struct sockaddr_in);
245 sin.sin_addr.s_addr = e->addr[0];
248 sin6.sin6_family = AF_INET6;
249 sin6.sin6_len = sizeof(struct sockaddr_in6);
250 memcpy(&sin6.sin6_addr, &e->addr[0], sizeof(e->addr));
254 vtag = EVL_MAKETAG(VLAN_NONE, 0, 0);
255 rc = toe_l2_resolve(tod, e->ifp, sa, dmac, &vtag);
256 if (rc == EWOULDBLOCK)
260 update_entry(sc, e, rc == 0 ? dmac : NULL, vtag);
261 mtx_unlock(&e->lock);
267 t4_l2t_send_slow(struct adapter *sc, struct wrqe *wr, struct l2t_entry *e)
272 case L2T_STATE_STALE: /* entry is stale, kick off revalidation */
274 if (resolve_entry(sc, e) != EWOULDBLOCK)
275 goto again; /* entry updated, re-examine state */
279 case L2T_STATE_VALID: /* fast-path, send the packet on */
284 case L2T_STATE_RESOLVING:
285 case L2T_STATE_SYNC_WRITE:
288 if (e->state != L2T_STATE_SYNC_WRITE &&
289 e->state != L2T_STATE_RESOLVING) {
290 /* state changed by the time we got here */
291 mtx_unlock(&e->lock);
295 mtx_unlock(&e->lock);
297 if (resolve_entry(sc, e) == EWOULDBLOCK)
301 if (e->state == L2T_STATE_VALID && !STAILQ_EMPTY(&e->wr_list))
303 if (e->state == L2T_STATE_FAILED)
304 resolution_failed(sc, e);
305 mtx_unlock(&e->lock);
308 case L2T_STATE_FAILED:
309 return (EHOSTUNREACH);
316 do_l2t_write_rpl2(struct sge_iq *iq, const struct rss_header *rss,
319 struct adapter *sc = iq->adapter;
320 const struct cpl_l2t_write_rpl *rpl = (const void *)(rss + 1);
321 unsigned int tid = GET_TID(rpl);
322 unsigned int idx = tid % L2T_SIZE;
324 if (__predict_false(rpl->status != CPL_ERR_NONE)) {
326 "Unexpected L2T_WRITE_RPL (%u) for entry at hw_idx %u\n",
331 if (tid & F_SYNC_WR) {
332 struct l2t_entry *e = &sc->l2t->l2tab[idx - sc->vres.l2t.start];
335 if (e->state != L2T_STATE_SWITCHING) {
337 e->state = L2T_STATE_VALID;
339 mtx_unlock(&e->lock);
346 * The TOE wants an L2 table entry that it can use to reach the next hop over
347 * the specified port. Produce such an entry - create one if needed.
349 * Note that the ifnet could be a pseudo-device like if_vlan, if_lagg, etc. on
350 * top of the real cxgbe interface.
353 t4_l2t_get(struct port_info *pi, struct ifnet *ifp, struct sockaddr *sa)
356 struct adapter *sc = pi->adapter;
357 struct l2t_data *d = sc->l2t;
358 u_int hash, smt_idx = pi->port_id;
359 uint16_t vid, pcp, vtag;
361 KASSERT(sa->sa_family == AF_INET || sa->sa_family == AF_INET6,
362 ("%s: sa %p has unexpected sa_family %d", __func__, sa,
367 if (ifp->if_type == IFT_L2VLAN) {
370 } else if (ifp->if_pcp != IFNET_PCP_NONE) {
374 vtag = EVL_MAKETAG(vid, pcp, 0);
376 hash = l2_hash(d, sa, ifp->if_index);
378 for (e = d->l2tab[hash].first; e; e = e->next) {
379 if (l2_cmp(sa, e) == 0 && e->ifp == ifp && e->vlan == vtag &&
380 e->smt_idx == smt_idx) {
386 /* Need to allocate a new entry */
389 mtx_lock(&e->lock); /* avoid race with t4_l2t_free */
390 e->next = d->l2tab[hash].first;
391 d->l2tab[hash].first = e;
393 e->state = L2T_STATE_RESOLVING;
396 e->smt_idx = smt_idx;
398 e->lport = pi->lport;
399 e->wrq = &sc->sge.ctrlq[pi->port_id];
400 e->iqid = sc->sge.ofld_rxq[pi->vi[0].first_ofld_rxq].iq.abs_id;
401 atomic_store_rel_int(&e->refcnt, 1);
403 mtx_unlock(&e->lock);
406 rw_wunlock(&d->lock);
411 * Called when the host's ARP layer makes a change to some entry that is loaded
412 * into the HW L2 table.
415 t4_l2_update(struct toedev *tod, struct ifnet *ifp, struct sockaddr *sa,
416 uint8_t *lladdr, uint16_t vtag)
418 struct adapter *sc = tod->tod_softc;
420 struct l2t_data *d = sc->l2t;
423 KASSERT(d != NULL, ("%s: no L2 table", __func__));
425 hash = l2_hash(d, sa, ifp->if_index);
427 for (e = d->l2tab[hash].first; e; e = e->next) {
428 if (l2_cmp(sa, e) == 0 && e->ifp == ifp) {
430 if (atomic_load_acq_int(&e->refcnt))
432 e->state = L2T_STATE_STALE;
433 mtx_unlock(&e->lock);
437 rw_runlock(&d->lock);
440 * This is of no interest to us. We've never had an offloaded
441 * connection to this destination, and we aren't attempting one right
447 rw_runlock(&d->lock);
449 KASSERT(e->state != L2T_STATE_UNUSED,
450 ("%s: unused entry in the hash.", __func__));
452 update_entry(sc, e, lladdr, vtag);
453 mtx_unlock(&e->lock);