xref: /freebsd/sys/dev/cxgbe/tom/t4_connect.c (revision a03411e84728e9b267056fd31c7d1d9d1dc1b01e)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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 #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/ktr.h>
39 #include <sys/module.h>
40 #include <sys/protosw.h>
41 #include <sys/domain.h>
42 #include <sys/socket.h>
43 #include <sys/socketvar.h>
44 #include <sys/sysctl.h>
45 #include <net/ethernet.h>
46 #include <net/if.h>
47 #include <net/if_types.h>
48 #include <net/if_vlan_var.h>
49 #include <net/route.h>
50 #include <net/route/nhop.h>
51 #include <netinet/in.h>
52 #include <netinet/in_pcb.h>
53 #include <netinet/ip.h>
54 #define TCPSTATES
55 #include <netinet/tcp_fsm.h>
56 #include <netinet/tcp_var.h>
57 #include <netinet/toecore.h>
58 #include <netinet/cc/cc.h>
59 
60 #include "common/common.h"
61 #include "common/t4_msg.h"
62 #include "common/t4_regs.h"
63 #include "common/t4_regs_values.h"
64 #include "t4_clip.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, be32toh(cpl->snd_isn) - 1,
102 	    be32toh(cpl->rcv_isn) - 1, cpl->tcp_opt);
103 	inp->inp_flowtype = M_HASHTYPE_OPAQUE;
104 	inp->inp_flowid = tid;
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 	struct epoch_tracker et;
119 
120 	free_atid(sc, atid);
121 	toep->tid = -1;
122 
123 	CURVNET_SET(toep->vnet);
124 	if (status != EAGAIN)
125 		NET_EPOCH_ENTER(et);
126 	INP_WLOCK(inp);
127 	toe_connect_failed(tod, inp, status);
128 	final_cpl_received(toep);	/* unlocks inp */
129 	if (status != EAGAIN)
130 		NET_EPOCH_EXIT(et);
131 	CURVNET_RESTORE();
132 }
133 
134 /*
135  * Active open failed.
136  */
137 static int
138 do_act_open_rpl(struct sge_iq *iq, const struct rss_header *rss,
139     struct mbuf *m)
140 {
141 	struct adapter *sc = iq->adapter;
142 	const struct cpl_act_open_rpl *cpl = (const void *)(rss + 1);
143 	u_int atid = G_TID_TID(G_AOPEN_ATID(be32toh(cpl->atid_status)));
144 	u_int status = G_AOPEN_STATUS(be32toh(cpl->atid_status));
145 	struct toepcb *toep = lookup_atid(sc, atid);
146 	int rc;
147 
148 	KASSERT(m == NULL, ("%s: wasn't expecting payload", __func__));
149 	KASSERT(toep->tid == atid, ("%s: toep tid/atid mismatch", __func__));
150 
151 	CTR3(KTR_CXGBE, "%s: atid %u, status %u ", __func__, atid, status);
152 
153 	/* Ignore negative advice */
154 	if (negative_advice(status))
155 		return (0);
156 
157 	if (status && act_open_has_tid(status))
158 		release_tid(sc, GET_TID(cpl), toep->ctrlq);
159 
160 	rc = act_open_rpl_status_to_errno(status);
161 	act_open_failure_cleanup(sc, atid, rc);
162 
163 	return (0);
164 }
165 
166 void
167 t4_init_connect_cpl_handlers(void)
168 {
169 
170 	t4_register_cpl_handler(CPL_ACT_ESTABLISH, do_act_establish);
171 	t4_register_shared_cpl_handler(CPL_ACT_OPEN_RPL, do_act_open_rpl,
172 	    CPL_COOKIE_TOM);
173 }
174 
175 void
176 t4_uninit_connect_cpl_handlers(void)
177 {
178 
179 	t4_register_cpl_handler(CPL_ACT_ESTABLISH, NULL);
180 	t4_register_shared_cpl_handler(CPL_ACT_OPEN_RPL, NULL, CPL_COOKIE_TOM);
181 }
182 
183 #ifdef KTR
184 #define DONT_OFFLOAD_ACTIVE_OPEN(x)	do { \
185 	reason = __LINE__; \
186 	rc = (x); \
187 	goto failed; \
188 } while (0)
189 #else
190 #define DONT_OFFLOAD_ACTIVE_OPEN(x)	do { \
191 	rc = (x); \
192 	goto failed; \
193 } while (0)
194 #endif
195 
196 static inline int
197 act_open_cpl_size(struct adapter *sc, int isipv6)
198 {
199 	int idx;
200 	static const int sz_table[3][2] = {
201 		{
202 			sizeof (struct cpl_act_open_req),
203 			sizeof (struct cpl_act_open_req6)
204 		},
205 		{
206 			sizeof (struct cpl_t5_act_open_req),
207 			sizeof (struct cpl_t5_act_open_req6)
208 		},
209 		{
210 			sizeof (struct cpl_t6_act_open_req),
211 			sizeof (struct cpl_t6_act_open_req6)
212 		},
213 	};
214 
215 	MPASS(chip_id(sc) >= CHELSIO_T4);
216 	idx = min(chip_id(sc) - CHELSIO_T4, 2);
217 
218 	return (sz_table[idx][!!isipv6]);
219 }
220 
221 /*
222  * active open (soconnect).
223  *
224  * State of affairs on entry:
225  * soisconnecting (so_state |= SS_ISCONNECTING)
226  * tcbinfo not locked (This has changed - used to be WLOCKed)
227  * inp WLOCKed
228  * tp->t_state = TCPS_SYN_SENT
229  * rtalloc1, RT_UNLOCK on rt.
230  */
231 int
232 t4_connect(struct toedev *tod, struct socket *so, struct nhop_object *nh,
233     struct sockaddr *nam)
234 {
235 	struct adapter *sc = tod->tod_softc;
236 	struct toepcb *toep = NULL;
237 	struct wrqe *wr = NULL;
238 	if_t rt_ifp = nh->nh_ifp;
239 	struct vi_info *vi;
240 	int qid_atid, rc, isipv6;
241 	struct inpcb *inp = sotoinpcb(so);
242 	struct tcpcb *tp = intotcpcb(inp);
243 #ifdef KTR
244 	int reason;
245 #endif
246 	struct offload_settings settings;
247 	struct epoch_tracker et;
248 	uint16_t vid = 0xfff, pcp = 0;
249 
250 	INP_WLOCK_ASSERT(inp);
251 	KASSERT(nam->sa_family == AF_INET || nam->sa_family == AF_INET6,
252 	    ("%s: dest addr %p has family %u", __func__, nam, nam->sa_family));
253 
254 	if (if_gettype(rt_ifp) == IFT_ETHER)
255 		vi = if_getsoftc(rt_ifp);
256 	else if (if_gettype(rt_ifp) == IFT_L2VLAN) {
257 		if_t ifp = VLAN_TRUNKDEV(rt_ifp);
258 
259 		vi = if_getsoftc(ifp);
260 		VLAN_TAG(rt_ifp, &vid);
261 		VLAN_PCP(rt_ifp, &pcp);
262 	} else if (if_gettype(rt_ifp) == IFT_IEEE8023ADLAG)
263 		DONT_OFFLOAD_ACTIVE_OPEN(ENOSYS); /* XXX: implement lagg+TOE */
264 	else
265 		DONT_OFFLOAD_ACTIVE_OPEN(ENOTSUP);
266 	if (sc->flags & KERN_TLS_ON)
267 		DONT_OFFLOAD_ACTIVE_OPEN(ENOTSUP);
268 
269 	rw_rlock(&sc->policy_lock);
270 	settings = *lookup_offload_policy(sc, OPEN_TYPE_ACTIVE, NULL,
271 	    EVL_MAKETAG(vid, pcp, 0), inp);
272 	rw_runlock(&sc->policy_lock);
273 	if (!settings.offload)
274 		DONT_OFFLOAD_ACTIVE_OPEN(EPERM);
275 
276 	toep = alloc_toepcb(vi, M_NOWAIT);
277 	if (toep == NULL)
278 		DONT_OFFLOAD_ACTIVE_OPEN(ENOMEM);
279 
280 	toep->tid = alloc_atid(sc, toep);
281 	if (toep->tid < 0)
282 		DONT_OFFLOAD_ACTIVE_OPEN(ENOMEM);
283 
284 	toep->l2te = t4_l2t_get(vi->pi, rt_ifp,
285 	    nh->nh_flags & NHF_GATEWAY ? &nh->gw_sa : nam);
286 	if (toep->l2te == NULL)
287 		DONT_OFFLOAD_ACTIVE_OPEN(ENOMEM);
288 
289 	toep->vnet = so->so_vnet;
290 	init_conn_params(vi, &settings, &inp->inp_inc, so, NULL,
291 	    toep->l2te->idx, &toep->params);
292 	init_toepcb(vi, toep);
293 
294 	isipv6 = nam->sa_family == AF_INET6;
295 	wr = alloc_wrqe(act_open_cpl_size(sc, isipv6), toep->ctrlq);
296 	if (wr == NULL)
297 		DONT_OFFLOAD_ACTIVE_OPEN(ENOMEM);
298 
299 	qid_atid = V_TID_QID(toep->ofld_rxq->iq.abs_id) | V_TID_TID(toep->tid) |
300 	    V_TID_COOKIE(CPL_COOKIE_TOM);
301 
302 	if (isipv6) {
303 		struct cpl_act_open_req6 *cpl = wrtod(wr);
304 		struct cpl_t5_act_open_req6 *cpl5 = (void *)cpl;
305 		struct cpl_t6_act_open_req6 *cpl6 = (void *)cpl;
306 
307 		if ((inp->inp_vflag & INP_IPV6) == 0)
308 			DONT_OFFLOAD_ACTIVE_OPEN(ENOTSUP);
309 
310 		toep->ce = t4_get_clip_entry(sc, &inp->in6p_laddr, true);
311 		if (toep->ce == NULL)
312 			DONT_OFFLOAD_ACTIVE_OPEN(ENOENT);
313 
314 		switch (chip_id(sc)) {
315 		case CHELSIO_T4:
316 			INIT_TP_WR(cpl, 0);
317 			cpl->params = select_ntuple(vi, toep->l2te);
318 			break;
319 		case CHELSIO_T5:
320 			INIT_TP_WR(cpl5, 0);
321 			cpl5->iss = htobe32(tp->iss);
322 			cpl5->params = select_ntuple(vi, toep->l2te);
323 			break;
324 		case CHELSIO_T6:
325 		default:
326 			INIT_TP_WR(cpl6, 0);
327 			cpl6->iss = htobe32(tp->iss);
328 			cpl6->params = select_ntuple(vi, toep->l2te);
329 			break;
330 		}
331 		OPCODE_TID(cpl) = htobe32(MK_OPCODE_TID(CPL_ACT_OPEN_REQ6,
332 		    qid_atid));
333 		cpl->local_port = inp->inp_lport;
334 		cpl->local_ip_hi = *(uint64_t *)&inp->in6p_laddr.s6_addr[0];
335 		cpl->local_ip_lo = *(uint64_t *)&inp->in6p_laddr.s6_addr[8];
336 		cpl->peer_port = inp->inp_fport;
337 		cpl->peer_ip_hi = *(uint64_t *)&inp->in6p_faddr.s6_addr[0];
338 		cpl->peer_ip_lo = *(uint64_t *)&inp->in6p_faddr.s6_addr[8];
339 		cpl->opt0 = calc_options0(vi, &toep->params);
340 		cpl->opt2 = calc_options2(vi, &toep->params);
341 
342 		CTR6(KTR_CXGBE,
343 		    "%s: atid %u, toep %p, inp %p, opt0 %#016lx, opt2 %#08x",
344 		    __func__, toep->tid, toep, inp, be64toh(cpl->opt0),
345 		    be32toh(cpl->opt2));
346 	} else {
347 		struct cpl_act_open_req *cpl = wrtod(wr);
348 		struct cpl_t5_act_open_req *cpl5 = (void *)cpl;
349 		struct cpl_t6_act_open_req *cpl6 = (void *)cpl;
350 
351 		switch (chip_id(sc)) {
352 		case CHELSIO_T4:
353 			INIT_TP_WR(cpl, 0);
354 			cpl->params = select_ntuple(vi, toep->l2te);
355 			break;
356 		case CHELSIO_T5:
357 			INIT_TP_WR(cpl5, 0);
358 			cpl5->iss = htobe32(tp->iss);
359 			cpl5->params = select_ntuple(vi, toep->l2te);
360 			break;
361 		case CHELSIO_T6:
362 		default:
363 			INIT_TP_WR(cpl6, 0);
364 			cpl6->iss = htobe32(tp->iss);
365 			cpl6->params = select_ntuple(vi, toep->l2te);
366 			break;
367 		}
368 		OPCODE_TID(cpl) = htobe32(MK_OPCODE_TID(CPL_ACT_OPEN_REQ,
369 		    qid_atid));
370 		inp_4tuple_get(inp, &cpl->local_ip, &cpl->local_port,
371 		    &cpl->peer_ip, &cpl->peer_port);
372 		cpl->opt0 = calc_options0(vi, &toep->params);
373 		cpl->opt2 = calc_options2(vi, &toep->params);
374 
375 		CTR6(KTR_CXGBE,
376 		    "%s: atid %u, toep %p, inp %p, opt0 %#016lx, opt2 %#08x",
377 		    __func__, toep->tid, toep, inp, be64toh(cpl->opt0),
378 		    be32toh(cpl->opt2));
379 	}
380 
381 	offload_socket(so, toep);
382 	NET_EPOCH_ENTER(et);
383 	rc = t4_l2t_send(sc, wr, toep->l2te);
384 	NET_EPOCH_EXIT(et);
385 	if (rc == 0) {
386 		toep->flags |= TPF_CPL_PENDING;
387 		return (0);
388 	}
389 
390 	undo_offload_socket(so);
391 #if defined(KTR)
392 	reason = __LINE__;
393 #endif
394 failed:
395 	CTR3(KTR_CXGBE, "%s: not offloading (%d), rc %d", __func__, reason, rc);
396 
397 	if (wr)
398 		free_wrqe(wr);
399 
400 	if (toep) {
401 		if (toep->tid >= 0)
402 			free_atid(sc, toep->tid);
403 		if (toep->l2te)
404 			t4_l2t_release(toep->l2te);
405 		if (toep->ce)
406 			t4_release_clip_entry(sc, toep->ce);
407 		free_toepcb(toep);
408 	}
409 
410 	return (rc);
411 }
412 #endif
413