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