xref: /freebsd/sys/dev/cxgbe/tom/t4_tom_l2t.c (revision 6fe0a6c80a1aff14236924eb33e4013aa8c14f91)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2012 Chelsio Communications, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
15  *
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
26  * SUCH DAMAGE.
27  */
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include "opt_inet.h"
32 #include "opt_inet6.h"
33 
34 #ifdef TCP_OFFLOAD
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/module.h>
39 #include <sys/bus.h>
40 #include <sys/fnv_hash.h>
41 #include <sys/lock.h>
42 #include <sys/mutex.h>
43 #include <sys/rwlock.h>
44 #include <sys/socket.h>
45 #include <sys/socketvar.h>
46 #include <sys/sbuf.h>
47 #include <sys/taskqueue.h>
48 #include <net/if.h>
49 #include <net/if_types.h>
50 #include <net/ethernet.h>
51 #include <net/if_vlan_var.h>
52 #include <net/route.h>
53 #include <netinet/in.h>
54 #include <netinet/tcp_var.h>
55 #include <netinet/toecore.h>
56 
57 #include "common/common.h"
58 #include "common/t4_msg.h"
59 #include "tom/t4_tom_l2t.h"
60 #include "tom/t4_tom.h"
61 
62 #define VLAN_NONE	0xfff
63 
64 static inline void
65 l2t_hold(struct l2t_data *d, struct l2t_entry *e)
66 {
67 
68 	if (atomic_fetchadd_int(&e->refcnt, 1) == 0)  /* 0 -> 1 transition */
69 		atomic_subtract_int(&d->nfree, 1);
70 }
71 
72 static inline u_int
73 l2_hash(struct l2t_data *d, const struct sockaddr *sa, int ifindex)
74 {
75 	u_int hash, half = d->l2t_size / 2, start = 0;
76 	const void *key;
77 	size_t len;
78 
79 	KASSERT(sa->sa_family == AF_INET || sa->sa_family == AF_INET6,
80 	    ("%s: sa %p has unexpected sa_family %d", __func__, sa,
81 	    sa->sa_family));
82 
83 	if (sa->sa_family == AF_INET) {
84 		const struct sockaddr_in *sin = (const void *)sa;
85 
86 		key = &sin->sin_addr;
87 		len = sizeof(sin->sin_addr);
88 	} else {
89 		const struct sockaddr_in6 *sin6 = (const void *)sa;
90 
91 		key = &sin6->sin6_addr;
92 		len = sizeof(sin6->sin6_addr);
93 		start = half;
94 	}
95 
96 	hash = fnv_32_buf(key, len, FNV1_32_INIT);
97 	hash = fnv_32_buf(&ifindex, sizeof(ifindex), hash);
98 	hash %= half;
99 
100 	return (hash + start);
101 }
102 
103 static inline int
104 l2_cmp(const struct sockaddr *sa, struct l2t_entry *e)
105 {
106 
107 	KASSERT(sa->sa_family == AF_INET || sa->sa_family == AF_INET6,
108 	    ("%s: sa %p has unexpected sa_family %d", __func__, sa,
109 	    sa->sa_family));
110 
111 	if (sa->sa_family == AF_INET) {
112 		const struct sockaddr_in *sin = (const void *)sa;
113 
114 		return (e->addr[0] != sin->sin_addr.s_addr);
115 	} else {
116 		const struct sockaddr_in6 *sin6 = (const void *)sa;
117 
118 		return (memcmp(&e->addr[0], &sin6->sin6_addr, sizeof(e->addr)));
119 	}
120 }
121 
122 static inline void
123 l2_store(const struct sockaddr *sa, struct l2t_entry *e)
124 {
125 
126 	KASSERT(sa->sa_family == AF_INET || sa->sa_family == AF_INET6,
127 	    ("%s: sa %p has unexpected sa_family %d", __func__, sa,
128 	    sa->sa_family));
129 
130 	if (sa->sa_family == AF_INET) {
131 		const struct sockaddr_in *sin = (const void *)sa;
132 
133 		e->addr[0] = sin->sin_addr.s_addr;
134 		e->ipv6 = 0;
135 	} else {
136 		const struct sockaddr_in6 *sin6 = (const void *)sa;
137 
138 		memcpy(&e->addr[0], &sin6->sin6_addr, sizeof(e->addr));
139 		e->ipv6 = 1;
140 	}
141 }
142 
143 /*
144  * Add a WR to an L2T entry's queue of work requests awaiting resolution.
145  * Must be called with the entry's lock held.
146  */
147 static inline void
148 arpq_enqueue(struct l2t_entry *e, struct wrqe *wr)
149 {
150 	mtx_assert(&e->lock, MA_OWNED);
151 
152 	STAILQ_INSERT_TAIL(&e->wr_list, wr, link);
153 }
154 
155 static inline void
156 send_pending(struct adapter *sc, struct l2t_entry *e)
157 {
158 	struct wrqe *wr;
159 
160 	mtx_assert(&e->lock, MA_OWNED);
161 
162 	while ((wr = STAILQ_FIRST(&e->wr_list)) != NULL) {
163 		STAILQ_REMOVE_HEAD(&e->wr_list, link);
164 		t4_wrq_tx(sc, wr);
165 	}
166 }
167 
168 static void
169 resolution_failed(struct adapter *sc, struct l2t_entry *e)
170 {
171 	struct tom_data *td = sc->tom_softc;
172 
173 	mtx_assert(&e->lock, MA_OWNED);
174 
175 	mtx_lock(&td->unsent_wr_lock);
176 	STAILQ_CONCAT(&td->unsent_wr_list, &e->wr_list);
177 	mtx_unlock(&td->unsent_wr_lock);
178 
179 	taskqueue_enqueue(taskqueue_thread, &td->reclaim_wr_resources);
180 }
181 
182 static void
183 update_entry(struct adapter *sc, struct l2t_entry *e, uint8_t *lladdr,
184     uint16_t vtag)
185 {
186 
187 	mtx_assert(&e->lock, MA_OWNED);
188 
189 	/*
190 	 * The entry may be in active use (e->refcount > 0) or not.  We update
191 	 * it even when it's not as this simplifies the case where we decide to
192 	 * reuse the entry later.
193 	 */
194 
195 	if (lladdr == NULL &&
196 	    (e->state == L2T_STATE_RESOLVING || e->state == L2T_STATE_FAILED)) {
197 		/*
198 		 * Never got a valid L2 address for this one.  Just mark it as
199 		 * failed instead of removing it from the hash (for which we'd
200 		 * need to wlock the table).
201 		 */
202 		e->state = L2T_STATE_FAILED;
203 		resolution_failed(sc, e);
204 		return;
205 
206 	} else if (lladdr == NULL) {
207 
208 		/* Valid or already-stale entry was deleted (or expired) */
209 
210 		KASSERT(e->state == L2T_STATE_VALID ||
211 		    e->state == L2T_STATE_STALE,
212 		    ("%s: lladdr NULL, state %d", __func__, e->state));
213 
214 		e->state = L2T_STATE_STALE;
215 
216 	} else {
217 
218 		if (e->state == L2T_STATE_RESOLVING ||
219 		    e->state == L2T_STATE_FAILED ||
220 		    memcmp(e->dmac, lladdr, ETHER_ADDR_LEN)) {
221 
222 			/* unresolved -> resolved; or dmac changed */
223 
224 			memcpy(e->dmac, lladdr, ETHER_ADDR_LEN);
225 			e->vlan = vtag;
226 			t4_write_l2e(e, 1);
227 		}
228 		e->state = L2T_STATE_VALID;
229 	}
230 }
231 
232 static int
233 resolve_entry(struct adapter *sc, struct l2t_entry *e)
234 {
235 	struct tom_data *td = sc->tom_softc;
236 	struct toedev *tod = &td->tod;
237 	struct sockaddr_in sin = {0};
238 	struct sockaddr_in6 sin6 = {0};
239 	struct sockaddr *sa;
240 	uint8_t dmac[ETHER_HDR_LEN];
241 	uint16_t vtag;
242 	int rc;
243 
244 	if (e->ipv6 == 0) {
245 		sin.sin_family = AF_INET;
246 		sin.sin_len = sizeof(struct sockaddr_in);
247 		sin.sin_addr.s_addr = e->addr[0];
248 		sa = (void *)&sin;
249 	} else {
250 		sin6.sin6_family = AF_INET6;
251 		sin6.sin6_len = sizeof(struct sockaddr_in6);
252 		memcpy(&sin6.sin6_addr, &e->addr[0], sizeof(e->addr));
253 		sa = (void *)&sin6;
254 	}
255 
256 	vtag = EVL_MAKETAG(VLAN_NONE, 0, 0);
257 	rc = toe_l2_resolve(tod, e->ifp, sa, dmac, &vtag);
258 	if (rc == EWOULDBLOCK)
259 		return (rc);
260 
261 	mtx_lock(&e->lock);
262 	update_entry(sc, e, rc == 0 ? dmac : NULL, vtag);
263 	mtx_unlock(&e->lock);
264 
265 	return (rc);
266 }
267 
268 int
269 t4_l2t_send_slow(struct adapter *sc, struct wrqe *wr, struct l2t_entry *e)
270 {
271 
272 again:
273 	switch (e->state) {
274 	case L2T_STATE_STALE:     /* entry is stale, kick off revalidation */
275 
276 		resolve_entry(sc, e);
277 
278 		/* Fall through */
279 
280 	case L2T_STATE_VALID:     /* fast-path, send the packet on */
281 
282 		t4_wrq_tx(sc, wr);
283 		return (0);
284 
285 	case L2T_STATE_RESOLVING:
286 	case L2T_STATE_SYNC_WRITE:
287 
288 		mtx_lock(&e->lock);
289 		if (e->state != L2T_STATE_SYNC_WRITE &&
290 		    e->state != L2T_STATE_RESOLVING) {
291 			/* state changed by the time we got here */
292 			mtx_unlock(&e->lock);
293 			goto again;
294 		}
295 		arpq_enqueue(e, wr);
296 		mtx_unlock(&e->lock);
297 
298 		if (resolve_entry(sc, e) == EWOULDBLOCK)
299 			break;
300 
301 		mtx_lock(&e->lock);
302 		if (e->state == L2T_STATE_VALID && !STAILQ_EMPTY(&e->wr_list))
303 			send_pending(sc, e);
304 		if (e->state == L2T_STATE_FAILED)
305 			resolution_failed(sc, e);
306 		mtx_unlock(&e->lock);
307 		break;
308 
309 	case L2T_STATE_FAILED:
310 		return (EHOSTUNREACH);
311 	}
312 
313 	return (0);
314 }
315 
316 int
317 do_l2t_write_rpl2(struct sge_iq *iq, const struct rss_header *rss,
318     struct mbuf *m)
319 {
320 	struct adapter *sc = iq->adapter;
321 	const struct cpl_l2t_write_rpl *rpl = (const void *)(rss + 1);
322 	unsigned int tid = GET_TID(rpl);
323 	unsigned int idx = tid % L2T_SIZE;
324 
325 	if (__predict_false(rpl->status != CPL_ERR_NONE)) {
326 		log(LOG_ERR,
327 		    "Unexpected L2T_WRITE_RPL (%u) for entry at hw_idx %u\n",
328 		    rpl->status, idx);
329 		return (EINVAL);
330 	}
331 
332 	if (tid & F_SYNC_WR) {
333 		struct l2t_entry *e = &sc->l2t->l2tab[idx - sc->vres.l2t.start];
334 
335 		mtx_lock(&e->lock);
336 		if (e->state != L2T_STATE_SWITCHING) {
337 			send_pending(sc, e);
338 			e->state = L2T_STATE_VALID;
339 		}
340 		mtx_unlock(&e->lock);
341 	}
342 
343 	return (0);
344 }
345 
346 /*
347  * The TOE wants an L2 table entry that it can use to reach the next hop over
348  * the specified port.  Produce such an entry - create one if needed.
349  *
350  * Note that the ifnet could be a pseudo-device like if_vlan, if_lagg, etc. on
351  * top of the real cxgbe interface.
352  */
353 struct l2t_entry *
354 t4_l2t_get(struct port_info *pi, struct ifnet *ifp, struct sockaddr *sa)
355 {
356 	struct l2t_entry *e;
357 	struct adapter *sc = pi->adapter;
358 	struct l2t_data *d = sc->l2t;
359 	u_int hash, smt_idx = pi->port_id;
360 	uint16_t vid, pcp, vtag;
361 
362 	KASSERT(sa->sa_family == AF_INET || sa->sa_family == AF_INET6,
363 	    ("%s: sa %p has unexpected sa_family %d", __func__, sa,
364 	    sa->sa_family));
365 
366 	vid = VLAN_NONE;
367 	pcp = 0;
368 	if (ifp->if_type == IFT_L2VLAN) {
369 		VLAN_TAG(ifp, &vid);
370 		VLAN_PCP(ifp, &pcp);
371 	} else if (ifp->if_pcp != IFNET_PCP_NONE) {
372 		vid = 0;
373 		pcp = ifp->if_pcp;
374 	}
375 	vtag = EVL_MAKETAG(vid, pcp, 0);
376 
377 	hash = l2_hash(d, sa, ifp->if_index);
378 	rw_wlock(&d->lock);
379 	for (e = d->l2tab[hash].first; e; e = e->next) {
380 		if (l2_cmp(sa, e) == 0 && e->ifp == ifp && e->vlan == vtag &&
381 		    e->smt_idx == smt_idx) {
382 			l2t_hold(d, e);
383 			goto done;
384 		}
385 	}
386 
387 	/* Need to allocate a new entry */
388 	e = t4_alloc_l2e(d);
389 	if (e) {
390 		mtx_lock(&e->lock);          /* avoid race with t4_l2t_free */
391 		e->next = d->l2tab[hash].first;
392 		d->l2tab[hash].first = e;
393 
394 		e->state = L2T_STATE_RESOLVING;
395 		l2_store(sa, e);
396 		e->ifp = ifp;
397 		e->smt_idx = smt_idx;
398 		e->hash = hash;
399 		e->lport = pi->lport;
400 		e->wrq = &sc->sge.ctrlq[pi->port_id];
401 		e->iqid = sc->sge.ofld_rxq[pi->vi[0].first_ofld_rxq].iq.abs_id;
402 		atomic_store_rel_int(&e->refcnt, 1);
403 		e->vlan = vtag;
404 		mtx_unlock(&e->lock);
405 	}
406 done:
407 	rw_wunlock(&d->lock);
408 	return e;
409 }
410 
411 /*
412  * Called when the host's ARP layer makes a change to some entry that is loaded
413  * into the HW L2 table.
414  */
415 void
416 t4_l2_update(struct toedev *tod, struct ifnet *ifp, struct sockaddr *sa,
417     uint8_t *lladdr, uint16_t vtag)
418 {
419 	struct adapter *sc = tod->tod_softc;
420 	struct l2t_entry *e;
421 	struct l2t_data *d = sc->l2t;
422 	u_int hash;
423 
424 	KASSERT(d != NULL, ("%s: no L2 table", __func__));
425 
426 	hash = l2_hash(d, sa, ifp->if_index);
427 	rw_rlock(&d->lock);
428 	for (e = d->l2tab[hash].first; e; e = e->next) {
429 		if (l2_cmp(sa, e) == 0 && e->ifp == ifp) {
430 			mtx_lock(&e->lock);
431 			if (atomic_load_acq_int(&e->refcnt))
432 				goto found;
433 			e->state = L2T_STATE_STALE;
434 			mtx_unlock(&e->lock);
435 			break;
436 		}
437 	}
438 	rw_runlock(&d->lock);
439 
440 	/*
441 	 * This is of no interest to us.  We've never had an offloaded
442 	 * connection to this destination, and we aren't attempting one right
443 	 * now.
444 	 */
445 	return;
446 
447 found:
448 	rw_runlock(&d->lock);
449 
450 	KASSERT(e->state != L2T_STATE_UNUSED,
451 	    ("%s: unused entry in the hash.", __func__));
452 
453 	update_entry(sc, e, lladdr, vtag);
454 	mtx_unlock(&e->lock);
455 }
456 #endif
457