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