xref: /freebsd/sys/dev/cxgbe/tom/t4_connect.c (revision b00ab754)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2012 Chelsio Communications, Inc.
5  * All rights reserved.
6  * Written by: Navdeep Parhar <np@FreeBSD.org>
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include "opt_inet.h"
34 #include "opt_inet6.h"
35 
36 #ifdef TCP_OFFLOAD
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/ktr.h>
41 #include <sys/module.h>
42 #include <sys/protosw.h>
43 #include <sys/domain.h>
44 #include <sys/socket.h>
45 #include <sys/socketvar.h>
46 #include <sys/sysctl.h>
47 #include <net/ethernet.h>
48 #include <net/if.h>
49 #include <net/if_types.h>
50 #include <net/if_vlan_var.h>
51 #include <net/route.h>
52 #include <netinet/in.h>
53 #include <netinet/in_pcb.h>
54 #include <netinet/ip.h>
55 #define TCPSTATES
56 #include <netinet/tcp_fsm.h>
57 #include <netinet/tcp_var.h>
58 #include <netinet/toecore.h>
59 #include <netinet/cc/cc.h>
60 
61 #include "common/common.h"
62 #include "common/t4_msg.h"
63 #include "common/t4_regs.h"
64 #include "common/t4_regs_values.h"
65 #include "tom/t4_tom_l2t.h"
66 #include "tom/t4_tom.h"
67 
68 /*
69  * Active open succeeded.
70  */
71 static int
72 do_act_establish(struct sge_iq *iq, const struct rss_header *rss,
73     struct mbuf *m)
74 {
75 	struct adapter *sc = iq->adapter;
76 	const struct cpl_act_establish *cpl = (const void *)(rss + 1);
77 	u_int tid = GET_TID(cpl);
78 	u_int atid = G_TID_TID(ntohl(cpl->tos_atid));
79 	struct toepcb *toep = lookup_atid(sc, atid);
80 	struct inpcb *inp = toep->inp;
81 
82 	KASSERT(m == NULL, ("%s: wasn't expecting payload", __func__));
83 	KASSERT(toep->tid == atid, ("%s: toep tid/atid mismatch", __func__));
84 
85 	CTR3(KTR_CXGBE, "%s: atid %u, tid %u", __func__, atid, tid);
86 	free_atid(sc, atid);
87 
88 	CURVNET_SET(toep->vnet);
89 	INP_WLOCK(inp);
90 	toep->tid = tid;
91 	insert_tid(sc, tid, toep, inp->inp_vflag & INP_IPV6 ? 2 : 1);
92 	if (inp->inp_flags & INP_DROPPED) {
93 
94 		/* socket closed by the kernel before hw told us it connected */
95 
96 		send_flowc_wr(toep, NULL);
97 		send_reset(sc, toep, be32toh(cpl->snd_isn));
98 		goto done;
99 	}
100 
101 	make_established(toep, cpl->snd_isn, cpl->rcv_isn, cpl->tcp_opt);
102 
103 	if (toep->ulp_mode == ULP_MODE_TLS)
104 		tls_establish(toep);
105 
106 done:
107 	INP_WUNLOCK(inp);
108 	CURVNET_RESTORE();
109 	return (0);
110 }
111 
112 void
113 act_open_failure_cleanup(struct adapter *sc, u_int atid, u_int status)
114 {
115 	struct toepcb *toep = lookup_atid(sc, atid);
116 	struct inpcb *inp = toep->inp;
117 	struct toedev *tod = &toep->td->tod;
118 
119 	free_atid(sc, atid);
120 	toep->tid = -1;
121 
122 	CURVNET_SET(toep->vnet);
123 	if (status != EAGAIN)
124 		INP_INFO_RLOCK(&V_tcbinfo);
125 	INP_WLOCK(inp);
126 	toe_connect_failed(tod, inp, status);
127 	final_cpl_received(toep);	/* unlocks inp */
128 	if (status != EAGAIN)
129 		INP_INFO_RUNLOCK(&V_tcbinfo);
130 	CURVNET_RESTORE();
131 }
132 
133 /*
134  * Active open failed.
135  */
136 static int
137 do_act_open_rpl(struct sge_iq *iq, const struct rss_header *rss,
138     struct mbuf *m)
139 {
140 	struct adapter *sc = iq->adapter;
141 	const struct cpl_act_open_rpl *cpl = (const void *)(rss + 1);
142 	u_int atid = G_TID_TID(G_AOPEN_ATID(be32toh(cpl->atid_status)));
143 	u_int status = G_AOPEN_STATUS(be32toh(cpl->atid_status));
144 	struct toepcb *toep = lookup_atid(sc, atid);
145 	int rc;
146 
147 	KASSERT(m == NULL, ("%s: wasn't expecting payload", __func__));
148 	KASSERT(toep->tid == atid, ("%s: toep tid/atid mismatch", __func__));
149 
150 	CTR3(KTR_CXGBE, "%s: atid %u, status %u ", __func__, atid, status);
151 
152 	/* Ignore negative advice */
153 	if (negative_advice(status))
154 		return (0);
155 
156 	if (status && act_open_has_tid(status))
157 		release_tid(sc, GET_TID(cpl), toep->ctrlq);
158 
159 	rc = act_open_rpl_status_to_errno(status);
160 	act_open_failure_cleanup(sc, atid, rc);
161 
162 	return (0);
163 }
164 
165 /*
166  * Options2 for active open.
167  */
168 static uint32_t
169 calc_opt2a(struct socket *so, struct toepcb *toep,
170     const struct offload_settings *s)
171 {
172 	struct tcpcb *tp = so_sototcpcb(so);
173 	struct port_info *pi = toep->vi->pi;
174 	struct adapter *sc = pi->adapter;
175 	uint32_t opt2 = 0;
176 
177 	/*
178 	 * rx flow control, rx coalesce, congestion control, and tx pace are all
179 	 * explicitly set by the driver.  On T5+ the ISS is also set by the
180 	 * driver to the value picked by the kernel.
181 	 */
182 	if (is_t4(sc)) {
183 		opt2 |= F_RX_FC_VALID | F_RX_COALESCE_VALID;
184 		opt2 |= F_CONG_CNTRL_VALID | F_PACE_VALID;
185 	} else {
186 		opt2 |= F_T5_OPT_2_VALID;	/* all 4 valid */
187 		opt2 |= F_T5_ISS;		/* ISS provided in CPL */
188 	}
189 
190 	if (s->sack > 0 || (s->sack < 0 && (tp->t_flags & TF_SACK_PERMIT)))
191 		opt2 |= F_SACK_EN;
192 
193 	if (s->tstamp > 0 || (s->tstamp < 0 && (tp->t_flags & TF_REQ_TSTMP)))
194 		opt2 |= F_TSTAMPS_EN;
195 
196 	if (tp->t_flags & TF_REQ_SCALE)
197 		opt2 |= F_WND_SCALE_EN;
198 
199 	if (s->ecn > 0 || (s->ecn < 0 && V_tcp_do_ecn == 1))
200 		opt2 |= F_CCTRL_ECN;
201 
202 	/* XXX: F_RX_CHANNEL for multiple rx c-chan support goes here. */
203 
204 	opt2 |= V_TX_QUEUE(sc->params.tp.tx_modq[pi->tx_chan]);
205 
206 	/* These defaults are subject to ULP specific fixups later. */
207 	opt2 |= V_RX_FC_DDP(0) | V_RX_FC_DISABLE(0);
208 
209 	opt2 |= V_PACE(0);
210 
211 	if (s->cong_algo >= 0)
212 		opt2 |= V_CONG_CNTRL(s->cong_algo);
213 	else if (sc->tt.cong_algorithm >= 0)
214 		opt2 |= V_CONG_CNTRL(sc->tt.cong_algorithm & M_CONG_CNTRL);
215 	else {
216 		struct cc_algo *cc = CC_ALGO(tp);
217 
218 		if (strcasecmp(cc->name, "reno") == 0)
219 			opt2 |= V_CONG_CNTRL(CONG_ALG_RENO);
220 		else if (strcasecmp(cc->name, "tahoe") == 0)
221 			opt2 |= V_CONG_CNTRL(CONG_ALG_TAHOE);
222 		if (strcasecmp(cc->name, "newreno") == 0)
223 			opt2 |= V_CONG_CNTRL(CONG_ALG_NEWRENO);
224 		if (strcasecmp(cc->name, "highspeed") == 0)
225 			opt2 |= V_CONG_CNTRL(CONG_ALG_HIGHSPEED);
226 		else {
227 			/*
228 			 * Use newreno in case the algorithm selected by the
229 			 * host stack is not supported by the hardware.
230 			 */
231 			opt2 |= V_CONG_CNTRL(CONG_ALG_NEWRENO);
232 		}
233 	}
234 
235 	if (s->rx_coalesce > 0 || (s->rx_coalesce < 0 && sc->tt.rx_coalesce))
236 		opt2 |= V_RX_COALESCE(M_RX_COALESCE);
237 
238 	/* Note that ofld_rxq is already set according to s->rxq. */
239 	opt2 |= F_RSS_QUEUE_VALID;
240 	opt2 |= V_RSS_QUEUE(toep->ofld_rxq->iq.abs_id);
241 
242 #ifdef USE_DDP_RX_FLOW_CONTROL
243 	if (toep->ulp_mode == ULP_MODE_TCPDDP)
244 		opt2 |= F_RX_FC_DDP;
245 #endif
246 
247 	if (toep->ulp_mode == ULP_MODE_TLS) {
248 		opt2 &= ~V_RX_COALESCE(M_RX_COALESCE);
249 		opt2 |= F_RX_FC_DISABLE;
250 	}
251 
252 	return (htobe32(opt2));
253 }
254 
255 void
256 t4_init_connect_cpl_handlers(void)
257 {
258 
259 	t4_register_cpl_handler(CPL_ACT_ESTABLISH, do_act_establish);
260 	t4_register_shared_cpl_handler(CPL_ACT_OPEN_RPL, do_act_open_rpl,
261 	    CPL_COOKIE_TOM);
262 }
263 
264 void
265 t4_uninit_connect_cpl_handlers(void)
266 {
267 
268 	t4_register_cpl_handler(CPL_ACT_ESTABLISH, NULL);
269 	t4_register_shared_cpl_handler(CPL_ACT_OPEN_RPL, NULL, CPL_COOKIE_TOM);
270 }
271 
272 #define DONT_OFFLOAD_ACTIVE_OPEN(x)	do { \
273 	reason = __LINE__; \
274 	rc = (x); \
275 	goto failed; \
276 } while (0)
277 
278 static inline int
279 act_open_cpl_size(struct adapter *sc, int isipv6)
280 {
281 	int idx;
282 	static const int sz_table[3][2] = {
283 		{
284 			sizeof (struct cpl_act_open_req),
285 			sizeof (struct cpl_act_open_req6)
286 		},
287 		{
288 			sizeof (struct cpl_t5_act_open_req),
289 			sizeof (struct cpl_t5_act_open_req6)
290 		},
291 		{
292 			sizeof (struct cpl_t6_act_open_req),
293 			sizeof (struct cpl_t6_act_open_req6)
294 		},
295 	};
296 
297 	MPASS(chip_id(sc) >= CHELSIO_T4);
298 	idx = min(chip_id(sc) - CHELSIO_T4, 2);
299 
300 	return (sz_table[idx][!!isipv6]);
301 }
302 
303 /*
304  * active open (soconnect).
305  *
306  * State of affairs on entry:
307  * soisconnecting (so_state |= SS_ISCONNECTING)
308  * tcbinfo not locked (This has changed - used to be WLOCKed)
309  * inp WLOCKed
310  * tp->t_state = TCPS_SYN_SENT
311  * rtalloc1, RT_UNLOCK on rt.
312  */
313 int
314 t4_connect(struct toedev *tod, struct socket *so, struct rtentry *rt,
315     struct sockaddr *nam)
316 {
317 	struct adapter *sc = tod->tod_softc;
318 	struct tom_data *td = tod_td(tod);
319 	struct toepcb *toep = NULL;
320 	struct wrqe *wr = NULL;
321 	struct ifnet *rt_ifp = rt->rt_ifp;
322 	struct vi_info *vi;
323 	int mtu_idx, rscale, qid_atid, rc, isipv6, txqid, rxqid;
324 	struct inpcb *inp = sotoinpcb(so);
325 	struct tcpcb *tp = intotcpcb(inp);
326 	int reason;
327 	struct offload_settings settings;
328 	uint16_t vid = 0xffff;
329 
330 	INP_WLOCK_ASSERT(inp);
331 	KASSERT(nam->sa_family == AF_INET || nam->sa_family == AF_INET6,
332 	    ("%s: dest addr %p has family %u", __func__, nam, nam->sa_family));
333 
334 	if (rt_ifp->if_type == IFT_ETHER)
335 		vi = rt_ifp->if_softc;
336 	else if (rt_ifp->if_type == IFT_L2VLAN) {
337 		struct ifnet *ifp = VLAN_COOKIE(rt_ifp);
338 
339 		vi = ifp->if_softc;
340 		VLAN_TAG(rt_ifp, &vid);
341 	} else if (rt_ifp->if_type == IFT_IEEE8023ADLAG)
342 		DONT_OFFLOAD_ACTIVE_OPEN(ENOSYS); /* XXX: implement lagg+TOE */
343 	else
344 		DONT_OFFLOAD_ACTIVE_OPEN(ENOTSUP);
345 
346 	rw_rlock(&sc->policy_lock);
347 	settings = *lookup_offload_policy(sc, OPEN_TYPE_ACTIVE, NULL, vid, inp);
348 	rw_runlock(&sc->policy_lock);
349 	if (!settings.offload)
350 		DONT_OFFLOAD_ACTIVE_OPEN(EPERM);
351 
352 	if (settings.txq >= 0 && settings.txq < vi->nofldtxq)
353 		txqid = settings.txq;
354 	else
355 		txqid = arc4random() % vi->nofldtxq;
356 	txqid += vi->first_ofld_txq;
357 	if (settings.rxq >= 0 && settings.rxq < vi->nofldrxq)
358 		rxqid = settings.rxq;
359 	else
360 		rxqid = arc4random() % vi->nofldrxq;
361 	rxqid += vi->first_ofld_rxq;
362 
363 	toep = alloc_toepcb(vi, txqid, rxqid, M_NOWAIT | M_ZERO);
364 	if (toep == NULL)
365 		DONT_OFFLOAD_ACTIVE_OPEN(ENOMEM);
366 
367 	toep->tid = alloc_atid(sc, toep);
368 	if (toep->tid < 0)
369 		DONT_OFFLOAD_ACTIVE_OPEN(ENOMEM);
370 
371 	toep->l2te = t4_l2t_get(vi->pi, rt_ifp,
372 	    rt->rt_flags & RTF_GATEWAY ? rt->rt_gateway : nam);
373 	if (toep->l2te == NULL)
374 		DONT_OFFLOAD_ACTIVE_OPEN(ENOMEM);
375 
376 	isipv6 = nam->sa_family == AF_INET6;
377 	wr = alloc_wrqe(act_open_cpl_size(sc, isipv6), toep->ctrlq);
378 	if (wr == NULL)
379 		DONT_OFFLOAD_ACTIVE_OPEN(ENOMEM);
380 
381 	toep->vnet = so->so_vnet;
382 	set_ulp_mode(toep, select_ulp_mode(so, sc, &settings));
383 	SOCKBUF_LOCK(&so->so_rcv);
384 	/* opt0 rcv_bufsiz initially, assumes its normal meaning later */
385 	toep->rx_credits = min(select_rcv_wnd(so) >> 10, M_RCV_BUFSIZ);
386 	SOCKBUF_UNLOCK(&so->so_rcv);
387 
388 	/*
389 	 * The kernel sets request_r_scale based on sb_max whereas we need to
390 	 * take hardware's MAX_RCV_WND into account too.  This is normally a
391 	 * no-op as MAX_RCV_WND is much larger than the default sb_max.
392 	 */
393 	if (tp->t_flags & TF_REQ_SCALE)
394 		rscale = tp->request_r_scale = select_rcv_wscale();
395 	else
396 		rscale = 0;
397 	mtu_idx = find_best_mtu_idx(sc, &inp->inp_inc, &settings);
398 	qid_atid = V_TID_QID(toep->ofld_rxq->iq.abs_id) | V_TID_TID(toep->tid) |
399 	    V_TID_COOKIE(CPL_COOKIE_TOM);
400 
401 	if (isipv6) {
402 		struct cpl_act_open_req6 *cpl = wrtod(wr);
403 		struct cpl_t5_act_open_req6 *cpl5 = (void *)cpl;
404 		struct cpl_t6_act_open_req6 *cpl6 = (void *)cpl;
405 
406 		if ((inp->inp_vflag & INP_IPV6) == 0)
407 			DONT_OFFLOAD_ACTIVE_OPEN(ENOTSUP);
408 
409 		toep->ce = hold_lip(td, &inp->in6p_laddr, NULL);
410 		if (toep->ce == NULL)
411 			DONT_OFFLOAD_ACTIVE_OPEN(ENOENT);
412 
413 		switch (chip_id(sc)) {
414 		case CHELSIO_T4:
415 			INIT_TP_WR(cpl, 0);
416 			cpl->params = select_ntuple(vi, toep->l2te);
417 			break;
418 		case CHELSIO_T5:
419 			INIT_TP_WR(cpl5, 0);
420 			cpl5->iss = htobe32(tp->iss);
421 			cpl5->params = select_ntuple(vi, toep->l2te);
422 			break;
423 		case CHELSIO_T6:
424 		default:
425 			INIT_TP_WR(cpl6, 0);
426 			cpl6->iss = htobe32(tp->iss);
427 			cpl6->params = select_ntuple(vi, toep->l2te);
428 			break;
429 		}
430 		OPCODE_TID(cpl) = htobe32(MK_OPCODE_TID(CPL_ACT_OPEN_REQ6,
431 		    qid_atid));
432 		cpl->local_port = inp->inp_lport;
433 		cpl->local_ip_hi = *(uint64_t *)&inp->in6p_laddr.s6_addr[0];
434 		cpl->local_ip_lo = *(uint64_t *)&inp->in6p_laddr.s6_addr[8];
435 		cpl->peer_port = inp->inp_fport;
436 		cpl->peer_ip_hi = *(uint64_t *)&inp->in6p_faddr.s6_addr[0];
437 		cpl->peer_ip_lo = *(uint64_t *)&inp->in6p_faddr.s6_addr[8];
438 		cpl->opt0 = calc_opt0(so, vi, toep->l2te, mtu_idx, rscale,
439 		    toep->rx_credits, toep->ulp_mode, &settings);
440 		cpl->opt2 = calc_opt2a(so, toep, &settings);
441 	} else {
442 		struct cpl_act_open_req *cpl = wrtod(wr);
443 		struct cpl_t5_act_open_req *cpl5 = (void *)cpl;
444 		struct cpl_t6_act_open_req *cpl6 = (void *)cpl;
445 
446 		switch (chip_id(sc)) {
447 		case CHELSIO_T4:
448 			INIT_TP_WR(cpl, 0);
449 			cpl->params = select_ntuple(vi, toep->l2te);
450 			break;
451 		case CHELSIO_T5:
452 			INIT_TP_WR(cpl5, 0);
453 			cpl5->iss = htobe32(tp->iss);
454 			cpl5->params = select_ntuple(vi, toep->l2te);
455 			break;
456 		case CHELSIO_T6:
457 		default:
458 			INIT_TP_WR(cpl6, 0);
459 			cpl6->iss = htobe32(tp->iss);
460 			cpl6->params = select_ntuple(vi, toep->l2te);
461 			break;
462 		}
463 		OPCODE_TID(cpl) = htobe32(MK_OPCODE_TID(CPL_ACT_OPEN_REQ,
464 		    qid_atid));
465 		inp_4tuple_get(inp, &cpl->local_ip, &cpl->local_port,
466 		    &cpl->peer_ip, &cpl->peer_port);
467 		cpl->opt0 = calc_opt0(so, vi, toep->l2te, mtu_idx, rscale,
468 		    toep->rx_credits, toep->ulp_mode, &settings);
469 		cpl->opt2 = calc_opt2a(so, toep, &settings);
470 	}
471 
472 	CTR5(KTR_CXGBE, "%s: atid %u (%s), toep %p, inp %p", __func__,
473 	    toep->tid, tcpstates[tp->t_state], toep, inp);
474 
475 	offload_socket(so, toep);
476 	rc = t4_l2t_send(sc, wr, toep->l2te);
477 	if (rc == 0) {
478 		toep->flags |= TPF_CPL_PENDING;
479 		return (0);
480 	}
481 
482 	undo_offload_socket(so);
483 	reason = __LINE__;
484 failed:
485 	CTR3(KTR_CXGBE, "%s: not offloading (%d), rc %d", __func__, reason, rc);
486 
487 	if (wr)
488 		free_wrqe(wr);
489 
490 	if (toep) {
491 		if (toep->tid >= 0)
492 			free_atid(sc, toep->tid);
493 		if (toep->l2te)
494 			t4_l2t_release(toep->l2te);
495 		if (toep->ce)
496 			release_lip(td, toep->ce);
497 		free_toepcb(toep);
498 	}
499 
500 	return (rc);
501 }
502 #endif
503