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 /* 113 * Convert an ACT_OPEN_RPL status to an errno. 114 */ 115 static inline int 116 act_open_rpl_status_to_errno(int status) 117 { 118 119 switch (status) { 120 case CPL_ERR_CONN_RESET: 121 return (ECONNREFUSED); 122 case CPL_ERR_ARP_MISS: 123 return (EHOSTUNREACH); 124 case CPL_ERR_CONN_TIMEDOUT: 125 return (ETIMEDOUT); 126 case CPL_ERR_TCAM_FULL: 127 return (EAGAIN); 128 case CPL_ERR_CONN_EXIST: 129 log(LOG_ERR, "ACTIVE_OPEN_RPL: 4-tuple in use\n"); 130 return (EAGAIN); 131 default: 132 return (EIO); 133 } 134 } 135 136 void 137 act_open_failure_cleanup(struct adapter *sc, u_int atid, u_int status) 138 { 139 struct toepcb *toep = lookup_atid(sc, atid); 140 struct inpcb *inp = toep->inp; 141 struct toedev *tod = &toep->td->tod; 142 143 free_atid(sc, atid); 144 toep->tid = -1; 145 146 CURVNET_SET(toep->vnet); 147 if (status != EAGAIN) 148 INP_INFO_RLOCK(&V_tcbinfo); 149 INP_WLOCK(inp); 150 toe_connect_failed(tod, inp, status); 151 final_cpl_received(toep); /* unlocks inp */ 152 if (status != EAGAIN) 153 INP_INFO_RUNLOCK(&V_tcbinfo); 154 CURVNET_RESTORE(); 155 } 156 157 /* 158 * Active open failed. 159 */ 160 static int 161 do_act_open_rpl(struct sge_iq *iq, const struct rss_header *rss, 162 struct mbuf *m) 163 { 164 struct adapter *sc = iq->adapter; 165 const struct cpl_act_open_rpl *cpl = (const void *)(rss + 1); 166 u_int atid = G_TID_TID(G_AOPEN_ATID(be32toh(cpl->atid_status))); 167 u_int status = G_AOPEN_STATUS(be32toh(cpl->atid_status)); 168 struct toepcb *toep = lookup_atid(sc, atid); 169 int rc; 170 171 KASSERT(m == NULL, ("%s: wasn't expecting payload", __func__)); 172 KASSERT(toep->tid == atid, ("%s: toep tid/atid mismatch", __func__)); 173 174 CTR3(KTR_CXGBE, "%s: atid %u, status %u ", __func__, atid, status); 175 176 /* Ignore negative advice */ 177 if (negative_advice(status)) 178 return (0); 179 180 if (status && act_open_has_tid(status)) 181 release_tid(sc, GET_TID(cpl), toep->ctrlq); 182 183 rc = act_open_rpl_status_to_errno(status); 184 act_open_failure_cleanup(sc, atid, rc); 185 186 return (0); 187 } 188 189 /* 190 * Options2 for active open. 191 */ 192 static uint32_t 193 calc_opt2a(struct socket *so, struct toepcb *toep, 194 const struct offload_settings *s) 195 { 196 struct tcpcb *tp = so_sototcpcb(so); 197 struct port_info *pi = toep->vi->pi; 198 struct adapter *sc = pi->adapter; 199 uint32_t opt2 = 0; 200 201 /* 202 * rx flow control, rx coalesce, congestion control, and tx pace are all 203 * explicitly set by the driver. On T5+ the ISS is also set by the 204 * driver to the value picked by the kernel. 205 */ 206 if (is_t4(sc)) { 207 opt2 |= F_RX_FC_VALID | F_RX_COALESCE_VALID; 208 opt2 |= F_CONG_CNTRL_VALID | F_PACE_VALID; 209 } else { 210 opt2 |= F_T5_OPT_2_VALID; /* all 4 valid */ 211 opt2 |= F_T5_ISS; /* ISS provided in CPL */ 212 } 213 214 if (s->sack > 0 || (s->sack < 0 && (tp->t_flags & TF_SACK_PERMIT))) 215 opt2 |= F_SACK_EN; 216 217 if (s->tstamp > 0 || (s->tstamp < 0 && (tp->t_flags & TF_REQ_TSTMP))) 218 opt2 |= F_TSTAMPS_EN; 219 220 if (tp->t_flags & TF_REQ_SCALE) 221 opt2 |= F_WND_SCALE_EN; 222 223 if (s->ecn > 0 || (s->ecn < 0 && V_tcp_do_ecn == 1)) 224 opt2 |= F_CCTRL_ECN; 225 226 /* XXX: F_RX_CHANNEL for multiple rx c-chan support goes here. */ 227 228 opt2 |= V_TX_QUEUE(sc->params.tp.tx_modq[pi->tx_chan]); 229 230 /* These defaults are subject to ULP specific fixups later. */ 231 opt2 |= V_RX_FC_DDP(0) | V_RX_FC_DISABLE(0); 232 233 opt2 |= V_PACE(0); 234 235 if (s->cong_algo >= 0) 236 opt2 |= V_CONG_CNTRL(s->cong_algo); 237 else if (sc->tt.cong_algorithm >= 0) 238 opt2 |= V_CONG_CNTRL(sc->tt.cong_algorithm & M_CONG_CNTRL); 239 else { 240 struct cc_algo *cc = CC_ALGO(tp); 241 242 if (strcasecmp(cc->name, "reno") == 0) 243 opt2 |= V_CONG_CNTRL(CONG_ALG_RENO); 244 else if (strcasecmp(cc->name, "tahoe") == 0) 245 opt2 |= V_CONG_CNTRL(CONG_ALG_TAHOE); 246 if (strcasecmp(cc->name, "newreno") == 0) 247 opt2 |= V_CONG_CNTRL(CONG_ALG_NEWRENO); 248 if (strcasecmp(cc->name, "highspeed") == 0) 249 opt2 |= V_CONG_CNTRL(CONG_ALG_HIGHSPEED); 250 else { 251 /* 252 * Use newreno in case the algorithm selected by the 253 * host stack is not supported by the hardware. 254 */ 255 opt2 |= V_CONG_CNTRL(CONG_ALG_NEWRENO); 256 } 257 } 258 259 if (s->rx_coalesce > 0 || (s->rx_coalesce < 0 && sc->tt.rx_coalesce)) 260 opt2 |= V_RX_COALESCE(M_RX_COALESCE); 261 262 /* Note that ofld_rxq is already set according to s->rxq. */ 263 opt2 |= F_RSS_QUEUE_VALID; 264 opt2 |= V_RSS_QUEUE(toep->ofld_rxq->iq.abs_id); 265 266 #ifdef USE_DDP_RX_FLOW_CONTROL 267 if (toep->ulp_mode == ULP_MODE_TCPDDP) 268 opt2 |= F_RX_FC_DDP; 269 #endif 270 271 if (toep->ulp_mode == ULP_MODE_TLS) { 272 opt2 &= ~V_RX_COALESCE(M_RX_COALESCE); 273 opt2 |= F_RX_FC_DISABLE; 274 } 275 276 return (htobe32(opt2)); 277 } 278 279 void 280 t4_init_connect_cpl_handlers(void) 281 { 282 283 t4_register_cpl_handler(CPL_ACT_ESTABLISH, do_act_establish); 284 t4_register_shared_cpl_handler(CPL_ACT_OPEN_RPL, do_act_open_rpl, 285 CPL_COOKIE_TOM); 286 } 287 288 void 289 t4_uninit_connect_cpl_handlers(void) 290 { 291 292 t4_register_cpl_handler(CPL_ACT_ESTABLISH, NULL); 293 t4_register_shared_cpl_handler(CPL_ACT_OPEN_RPL, NULL, CPL_COOKIE_TOM); 294 } 295 296 #define DONT_OFFLOAD_ACTIVE_OPEN(x) do { \ 297 reason = __LINE__; \ 298 rc = (x); \ 299 goto failed; \ 300 } while (0) 301 302 static inline int 303 act_open_cpl_size(struct adapter *sc, int isipv6) 304 { 305 int idx; 306 static const int sz_table[3][2] = { 307 { 308 sizeof (struct cpl_act_open_req), 309 sizeof (struct cpl_act_open_req6) 310 }, 311 { 312 sizeof (struct cpl_t5_act_open_req), 313 sizeof (struct cpl_t5_act_open_req6) 314 }, 315 { 316 sizeof (struct cpl_t6_act_open_req), 317 sizeof (struct cpl_t6_act_open_req6) 318 }, 319 }; 320 321 MPASS(chip_id(sc) >= CHELSIO_T4); 322 idx = min(chip_id(sc) - CHELSIO_T4, 2); 323 324 return (sz_table[idx][!!isipv6]); 325 } 326 327 /* 328 * active open (soconnect). 329 * 330 * State of affairs on entry: 331 * soisconnecting (so_state |= SS_ISCONNECTING) 332 * tcbinfo not locked (This has changed - used to be WLOCKed) 333 * inp WLOCKed 334 * tp->t_state = TCPS_SYN_SENT 335 * rtalloc1, RT_UNLOCK on rt. 336 */ 337 int 338 t4_connect(struct toedev *tod, struct socket *so, struct rtentry *rt, 339 struct sockaddr *nam) 340 { 341 struct adapter *sc = tod->tod_softc; 342 struct tom_data *td = tod_td(tod); 343 struct toepcb *toep = NULL; 344 struct wrqe *wr = NULL; 345 struct ifnet *rt_ifp = rt->rt_ifp; 346 struct vi_info *vi; 347 int mtu_idx, rscale, qid_atid, rc, isipv6, txqid, rxqid; 348 struct inpcb *inp = sotoinpcb(so); 349 struct tcpcb *tp = intotcpcb(inp); 350 int reason; 351 struct offload_settings settings; 352 uint16_t vid = 0xffff; 353 354 INP_WLOCK_ASSERT(inp); 355 KASSERT(nam->sa_family == AF_INET || nam->sa_family == AF_INET6, 356 ("%s: dest addr %p has family %u", __func__, nam, nam->sa_family)); 357 358 if (rt_ifp->if_type == IFT_ETHER) 359 vi = rt_ifp->if_softc; 360 else if (rt_ifp->if_type == IFT_L2VLAN) { 361 struct ifnet *ifp = VLAN_COOKIE(rt_ifp); 362 363 vi = ifp->if_softc; 364 VLAN_TAG(rt_ifp, &vid); 365 } else if (rt_ifp->if_type == IFT_IEEE8023ADLAG) 366 DONT_OFFLOAD_ACTIVE_OPEN(ENOSYS); /* XXX: implement lagg+TOE */ 367 else 368 DONT_OFFLOAD_ACTIVE_OPEN(ENOTSUP); 369 370 rw_rlock(&sc->policy_lock); 371 settings = *lookup_offload_policy(sc, OPEN_TYPE_ACTIVE, NULL, vid, inp); 372 rw_runlock(&sc->policy_lock); 373 if (!settings.offload) 374 DONT_OFFLOAD_ACTIVE_OPEN(EPERM); 375 376 if (settings.txq >= 0 && settings.txq < vi->nofldtxq) 377 txqid = settings.txq; 378 else 379 txqid = arc4random() % vi->nofldtxq; 380 txqid += vi->first_ofld_txq; 381 if (settings.rxq >= 0 && settings.rxq < vi->nofldrxq) 382 rxqid = settings.rxq; 383 else 384 rxqid = arc4random() % vi->nofldrxq; 385 rxqid += vi->first_ofld_rxq; 386 387 toep = alloc_toepcb(vi, txqid, rxqid, M_NOWAIT | M_ZERO); 388 if (toep == NULL) 389 DONT_OFFLOAD_ACTIVE_OPEN(ENOMEM); 390 391 toep->tid = alloc_atid(sc, toep); 392 if (toep->tid < 0) 393 DONT_OFFLOAD_ACTIVE_OPEN(ENOMEM); 394 395 toep->l2te = t4_l2t_get(vi->pi, rt_ifp, 396 rt->rt_flags & RTF_GATEWAY ? rt->rt_gateway : nam); 397 if (toep->l2te == NULL) 398 DONT_OFFLOAD_ACTIVE_OPEN(ENOMEM); 399 400 isipv6 = nam->sa_family == AF_INET6; 401 wr = alloc_wrqe(act_open_cpl_size(sc, isipv6), toep->ctrlq); 402 if (wr == NULL) 403 DONT_OFFLOAD_ACTIVE_OPEN(ENOMEM); 404 405 toep->vnet = so->so_vnet; 406 set_ulp_mode(toep, select_ulp_mode(so, sc, &settings)); 407 SOCKBUF_LOCK(&so->so_rcv); 408 /* opt0 rcv_bufsiz initially, assumes its normal meaning later */ 409 toep->rx_credits = min(select_rcv_wnd(so) >> 10, M_RCV_BUFSIZ); 410 SOCKBUF_UNLOCK(&so->so_rcv); 411 412 /* 413 * The kernel sets request_r_scale based on sb_max whereas we need to 414 * take hardware's MAX_RCV_WND into account too. This is normally a 415 * no-op as MAX_RCV_WND is much larger than the default sb_max. 416 */ 417 if (tp->t_flags & TF_REQ_SCALE) 418 rscale = tp->request_r_scale = select_rcv_wscale(); 419 else 420 rscale = 0; 421 mtu_idx = find_best_mtu_idx(sc, &inp->inp_inc, &settings); 422 qid_atid = V_TID_QID(toep->ofld_rxq->iq.abs_id) | V_TID_TID(toep->tid) | 423 V_TID_COOKIE(CPL_COOKIE_TOM); 424 425 if (isipv6) { 426 struct cpl_act_open_req6 *cpl = wrtod(wr); 427 struct cpl_t5_act_open_req6 *cpl5 = (void *)cpl; 428 struct cpl_t6_act_open_req6 *cpl6 = (void *)cpl; 429 430 if ((inp->inp_vflag & INP_IPV6) == 0) 431 DONT_OFFLOAD_ACTIVE_OPEN(ENOTSUP); 432 433 toep->ce = hold_lip(td, &inp->in6p_laddr, NULL); 434 if (toep->ce == NULL) 435 DONT_OFFLOAD_ACTIVE_OPEN(ENOENT); 436 437 switch (chip_id(sc)) { 438 case CHELSIO_T4: 439 INIT_TP_WR(cpl, 0); 440 cpl->params = select_ntuple(vi, toep->l2te); 441 break; 442 case CHELSIO_T5: 443 INIT_TP_WR(cpl5, 0); 444 cpl5->iss = htobe32(tp->iss); 445 cpl5->params = select_ntuple(vi, toep->l2te); 446 break; 447 case CHELSIO_T6: 448 default: 449 INIT_TP_WR(cpl6, 0); 450 cpl6->iss = htobe32(tp->iss); 451 cpl6->params = select_ntuple(vi, toep->l2te); 452 break; 453 } 454 OPCODE_TID(cpl) = htobe32(MK_OPCODE_TID(CPL_ACT_OPEN_REQ6, 455 qid_atid)); 456 cpl->local_port = inp->inp_lport; 457 cpl->local_ip_hi = *(uint64_t *)&inp->in6p_laddr.s6_addr[0]; 458 cpl->local_ip_lo = *(uint64_t *)&inp->in6p_laddr.s6_addr[8]; 459 cpl->peer_port = inp->inp_fport; 460 cpl->peer_ip_hi = *(uint64_t *)&inp->in6p_faddr.s6_addr[0]; 461 cpl->peer_ip_lo = *(uint64_t *)&inp->in6p_faddr.s6_addr[8]; 462 cpl->opt0 = calc_opt0(so, vi, toep->l2te, mtu_idx, rscale, 463 toep->rx_credits, toep->ulp_mode, &settings); 464 cpl->opt2 = calc_opt2a(so, toep, &settings); 465 } else { 466 struct cpl_act_open_req *cpl = wrtod(wr); 467 struct cpl_t5_act_open_req *cpl5 = (void *)cpl; 468 struct cpl_t6_act_open_req *cpl6 = (void *)cpl; 469 470 switch (chip_id(sc)) { 471 case CHELSIO_T4: 472 INIT_TP_WR(cpl, 0); 473 cpl->params = select_ntuple(vi, toep->l2te); 474 break; 475 case CHELSIO_T5: 476 INIT_TP_WR(cpl5, 0); 477 cpl5->iss = htobe32(tp->iss); 478 cpl5->params = select_ntuple(vi, toep->l2te); 479 break; 480 case CHELSIO_T6: 481 default: 482 INIT_TP_WR(cpl6, 0); 483 cpl6->iss = htobe32(tp->iss); 484 cpl6->params = select_ntuple(vi, toep->l2te); 485 break; 486 } 487 OPCODE_TID(cpl) = htobe32(MK_OPCODE_TID(CPL_ACT_OPEN_REQ, 488 qid_atid)); 489 inp_4tuple_get(inp, &cpl->local_ip, &cpl->local_port, 490 &cpl->peer_ip, &cpl->peer_port); 491 cpl->opt0 = calc_opt0(so, vi, toep->l2te, mtu_idx, rscale, 492 toep->rx_credits, toep->ulp_mode, &settings); 493 cpl->opt2 = calc_opt2a(so, toep, &settings); 494 } 495 496 CTR5(KTR_CXGBE, "%s: atid %u (%s), toep %p, inp %p", __func__, 497 toep->tid, tcpstates[tp->t_state], toep, inp); 498 499 offload_socket(so, toep); 500 rc = t4_l2t_send(sc, wr, toep->l2te); 501 if (rc == 0) { 502 toep->flags |= TPF_CPL_PENDING; 503 return (0); 504 } 505 506 undo_offload_socket(so); 507 reason = __LINE__; 508 failed: 509 CTR3(KTR_CXGBE, "%s: not offloading (%d), rc %d", __func__, reason, rc); 510 511 if (wr) 512 free_wrqe(wr); 513 514 if (toep) { 515 if (toep->tid >= 0) 516 free_atid(sc, toep->tid); 517 if (toep->l2te) 518 t4_l2t_release(toep->l2te); 519 if (toep->ce) 520 release_lip(td, toep->ce); 521 free_toepcb(toep); 522 } 523 524 return (rc); 525 } 526 #endif 527