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