2 * Copyright (c) 2012 Chelsio Communications, Inc.
4 * Written by: Navdeep Parhar <np@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
32 #include "opt_inet6.h"
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.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>
47 #include <net/if_types.h>
48 #include <net/if_vlan_var.h>
49 #include <net/route.h>
50 #include <netinet/in.h>
51 #include <netinet/in_pcb.h>
52 #include <netinet/ip.h>
54 #include <netinet/tcp_fsm.h>
55 #include <netinet/tcp_var.h>
56 #include <netinet/toecore.h>
57 #include <netinet/cc/cc.h>
59 #include "common/common.h"
60 #include "common/t4_msg.h"
61 #include "common/t4_regs.h"
62 #include "common/t4_regs_values.h"
63 #include "tom/t4_tom_l2t.h"
64 #include "tom/t4_tom.h"
67 static int alloc_atid(struct adapter *, void *);
68 static void *lookup_atid(struct adapter *, int);
69 static void free_atid(struct adapter *, int);
72 alloc_atid(struct adapter *sc, void *ctx)
74 struct tid_info *t = &sc->tids;
77 mtx_lock(&t->atid_lock);
79 union aopen_entry *p = t->afree;
81 atid = p - t->atid_tab;
86 mtx_unlock(&t->atid_lock);
91 lookup_atid(struct adapter *sc, int atid)
93 struct tid_info *t = &sc->tids;
95 return (t->atid_tab[atid].data);
99 free_atid(struct adapter *sc, int atid)
101 struct tid_info *t = &sc->tids;
102 union aopen_entry *p = &t->atid_tab[atid];
104 mtx_lock(&t->atid_lock);
108 mtx_unlock(&t->atid_lock);
112 * Active open succeeded.
115 do_act_establish(struct sge_iq *iq, const struct rss_header *rss,
118 struct adapter *sc = iq->adapter;
119 const struct cpl_act_establish *cpl = (const void *)(rss + 1);
120 u_int tid = GET_TID(cpl);
121 u_int atid = G_TID_TID(ntohl(cpl->tos_atid));
122 struct toepcb *toep = lookup_atid(sc, atid);
123 struct inpcb *inp = toep->inp;
125 KASSERT(m == NULL, ("%s: wasn't expecting payload", __func__));
126 KASSERT(toep->tid == atid, ("%s: toep tid/atid mismatch", __func__));
128 CTR3(KTR_CXGBE, "%s: atid %u, tid %u", __func__, atid, tid);
131 CURVNET_SET(toep->vnet);
134 insert_tid(sc, tid, toep, inp->inp_vflag & INP_IPV6 ? 2 : 1);
135 if (inp->inp_flags & INP_DROPPED) {
137 /* socket closed by the kernel before hw told us it connected */
139 send_flowc_wr(toep, NULL);
140 send_reset(sc, toep, be32toh(cpl->snd_isn));
144 make_established(toep, cpl->snd_isn, cpl->rcv_isn, cpl->tcp_opt);
146 if (toep->ulp_mode == ULP_MODE_TLS)
156 * Convert an ACT_OPEN_RPL status to an errno.
159 act_open_rpl_status_to_errno(int status)
163 case CPL_ERR_CONN_RESET:
164 return (ECONNREFUSED);
165 case CPL_ERR_ARP_MISS:
166 return (EHOSTUNREACH);
167 case CPL_ERR_CONN_TIMEDOUT:
169 case CPL_ERR_TCAM_FULL:
171 case CPL_ERR_CONN_EXIST:
172 log(LOG_ERR, "ACTIVE_OPEN_RPL: 4-tuple in use\n");
180 act_open_failure_cleanup(struct adapter *sc, u_int atid, u_int status)
182 struct toepcb *toep = lookup_atid(sc, atid);
183 struct inpcb *inp = toep->inp;
184 struct toedev *tod = &toep->td->tod;
189 CURVNET_SET(toep->vnet);
190 if (status != EAGAIN)
191 INP_INFO_RLOCK(&V_tcbinfo);
193 toe_connect_failed(tod, inp, status);
194 final_cpl_received(toep); /* unlocks inp */
195 if (status != EAGAIN)
196 INP_INFO_RUNLOCK(&V_tcbinfo);
201 * Active open failed.
204 do_act_open_rpl(struct sge_iq *iq, const struct rss_header *rss,
207 struct adapter *sc = iq->adapter;
208 const struct cpl_act_open_rpl *cpl = (const void *)(rss + 1);
209 u_int atid = G_TID_TID(G_AOPEN_ATID(be32toh(cpl->atid_status)));
210 u_int status = G_AOPEN_STATUS(be32toh(cpl->atid_status));
211 struct toepcb *toep = lookup_atid(sc, atid);
214 KASSERT(m == NULL, ("%s: wasn't expecting payload", __func__));
215 KASSERT(toep->tid == atid, ("%s: toep tid/atid mismatch", __func__));
217 CTR3(KTR_CXGBE, "%s: atid %u, status %u ", __func__, atid, status);
219 /* Ignore negative advice */
220 if (negative_advice(status))
223 if (status && act_open_has_tid(status))
224 release_tid(sc, GET_TID(cpl), toep->ctrlq);
226 rc = act_open_rpl_status_to_errno(status);
227 act_open_failure_cleanup(sc, atid, rc);
233 * Options2 for active open.
236 calc_opt2a(struct socket *so, struct toepcb *toep,
237 const struct offload_settings *s)
239 struct tcpcb *tp = so_sototcpcb(so);
240 struct port_info *pi = toep->vi->pi;
241 struct adapter *sc = pi->adapter;
245 * rx flow control, rx coalesce, congestion control, and tx pace are all
246 * explicitly set by the driver. On T5+ the ISS is also set by the
247 * driver to the value picked by the kernel.
250 opt2 |= F_RX_FC_VALID | F_RX_COALESCE_VALID;
251 opt2 |= F_CONG_CNTRL_VALID | F_PACE_VALID;
253 opt2 |= F_T5_OPT_2_VALID; /* all 4 valid */
254 opt2 |= F_T5_ISS; /* ISS provided in CPL */
257 if (s->sack > 0 || (s->sack < 0 && (tp->t_flags & TF_SACK_PERMIT)))
260 if (s->tstamp > 0 || (s->tstamp < 0 && (tp->t_flags & TF_REQ_TSTMP)))
261 opt2 |= F_TSTAMPS_EN;
263 if (tp->t_flags & TF_REQ_SCALE)
264 opt2 |= F_WND_SCALE_EN;
266 if (s->ecn > 0 || (s->ecn < 0 && V_tcp_do_ecn == 1))
269 /* XXX: F_RX_CHANNEL for multiple rx c-chan support goes here. */
271 opt2 |= V_TX_QUEUE(sc->params.tp.tx_modq[pi->tx_chan]);
273 /* These defaults are subject to ULP specific fixups later. */
274 opt2 |= V_RX_FC_DDP(0) | V_RX_FC_DISABLE(0);
278 if (s->cong_algo >= 0)
279 opt2 |= V_CONG_CNTRL(s->cong_algo);
280 else if (sc->tt.cong_algorithm >= 0)
281 opt2 |= V_CONG_CNTRL(sc->tt.cong_algorithm & M_CONG_CNTRL);
283 struct cc_algo *cc = CC_ALGO(tp);
285 if (strcasecmp(cc->name, "reno") == 0)
286 opt2 |= V_CONG_CNTRL(CONG_ALG_RENO);
287 else if (strcasecmp(cc->name, "tahoe") == 0)
288 opt2 |= V_CONG_CNTRL(CONG_ALG_TAHOE);
289 if (strcasecmp(cc->name, "newreno") == 0)
290 opt2 |= V_CONG_CNTRL(CONG_ALG_NEWRENO);
291 if (strcasecmp(cc->name, "highspeed") == 0)
292 opt2 |= V_CONG_CNTRL(CONG_ALG_HIGHSPEED);
295 * Use newreno in case the algorithm selected by the
296 * host stack is not supported by the hardware.
298 opt2 |= V_CONG_CNTRL(CONG_ALG_NEWRENO);
302 if (s->rx_coalesce > 0 || (s->rx_coalesce < 0 && sc->tt.rx_coalesce))
303 opt2 |= V_RX_COALESCE(M_RX_COALESCE);
305 /* Note that ofld_rxq is already set according to s->rxq. */
306 opt2 |= F_RSS_QUEUE_VALID;
307 opt2 |= V_RSS_QUEUE(toep->ofld_rxq->iq.abs_id);
309 #ifdef USE_DDP_RX_FLOW_CONTROL
310 if (toep->ulp_mode == ULP_MODE_TCPDDP)
314 if (toep->ulp_mode == ULP_MODE_TLS) {
315 opt2 &= ~V_RX_COALESCE(M_RX_COALESCE);
316 opt2 |= F_RX_FC_DISABLE;
319 return (htobe32(opt2));
323 t4_init_connect_cpl_handlers(void)
326 t4_register_cpl_handler(CPL_ACT_ESTABLISH, do_act_establish);
327 t4_register_cpl_handler(CPL_ACT_OPEN_RPL, do_act_open_rpl);
331 t4_uninit_connect_cpl_handlers(void)
334 t4_register_cpl_handler(CPL_ACT_ESTABLISH, NULL);
335 t4_register_cpl_handler(CPL_ACT_OPEN_RPL, NULL);
338 #define DONT_OFFLOAD_ACTIVE_OPEN(x) do { \
345 act_open_cpl_size(struct adapter *sc, int isipv6)
348 static const int sz_table[3][2] = {
350 sizeof (struct cpl_act_open_req),
351 sizeof (struct cpl_act_open_req6)
354 sizeof (struct cpl_t5_act_open_req),
355 sizeof (struct cpl_t5_act_open_req6)
358 sizeof (struct cpl_t6_act_open_req),
359 sizeof (struct cpl_t6_act_open_req6)
363 MPASS(chip_id(sc) >= CHELSIO_T4);
364 idx = min(chip_id(sc) - CHELSIO_T4, 2);
366 return (sz_table[idx][!!isipv6]);
370 * active open (soconnect).
372 * State of affairs on entry:
373 * soisconnecting (so_state |= SS_ISCONNECTING)
374 * tcbinfo not locked (This has changed - used to be WLOCKed)
376 * tp->t_state = TCPS_SYN_SENT
377 * rtalloc1, RT_UNLOCK on rt.
380 t4_connect(struct toedev *tod, struct socket *so, struct rtentry *rt,
381 struct sockaddr *nam)
383 struct adapter *sc = tod->tod_softc;
384 struct tom_data *td = tod_td(tod);
385 struct toepcb *toep = NULL;
386 struct wrqe *wr = NULL;
387 struct ifnet *rt_ifp = rt->rt_ifp;
389 int mtu_idx, rscale, qid_atid, rc, isipv6, txqid, rxqid;
390 struct inpcb *inp = sotoinpcb(so);
391 struct tcpcb *tp = intotcpcb(inp);
393 struct offload_settings settings;
394 uint16_t vid = 0xffff;
396 INP_WLOCK_ASSERT(inp);
397 KASSERT(nam->sa_family == AF_INET || nam->sa_family == AF_INET6,
398 ("%s: dest addr %p has family %u", __func__, nam, nam->sa_family));
400 if (rt_ifp->if_type == IFT_ETHER)
401 vi = rt_ifp->if_softc;
402 else if (rt_ifp->if_type == IFT_L2VLAN) {
403 struct ifnet *ifp = VLAN_COOKIE(rt_ifp);
406 VLAN_TAG(rt_ifp, &vid);
407 } else if (rt_ifp->if_type == IFT_IEEE8023ADLAG)
408 DONT_OFFLOAD_ACTIVE_OPEN(ENOSYS); /* XXX: implement lagg+TOE */
410 DONT_OFFLOAD_ACTIVE_OPEN(ENOTSUP);
412 rw_rlock(&sc->policy_lock);
413 settings = *lookup_offload_policy(sc, OPEN_TYPE_ACTIVE, NULL, vid, inp);
414 rw_runlock(&sc->policy_lock);
415 if (!settings.offload)
416 DONT_OFFLOAD_ACTIVE_OPEN(EPERM);
418 if (settings.txq >= 0 && settings.txq < vi->nofldtxq)
419 txqid = settings.txq;
421 txqid = arc4random() % vi->nofldtxq;
422 txqid += vi->first_ofld_txq;
423 if (settings.rxq >= 0 && settings.rxq < vi->nofldrxq)
424 rxqid = settings.rxq;
426 rxqid = arc4random() % vi->nofldrxq;
427 rxqid += vi->first_ofld_rxq;
429 toep = alloc_toepcb(vi, txqid, rxqid, M_NOWAIT | M_ZERO);
431 DONT_OFFLOAD_ACTIVE_OPEN(ENOMEM);
433 toep->tid = alloc_atid(sc, toep);
435 DONT_OFFLOAD_ACTIVE_OPEN(ENOMEM);
437 toep->l2te = t4_l2t_get(vi->pi, rt_ifp,
438 rt->rt_flags & RTF_GATEWAY ? rt->rt_gateway : nam);
439 if (toep->l2te == NULL)
440 DONT_OFFLOAD_ACTIVE_OPEN(ENOMEM);
442 isipv6 = nam->sa_family == AF_INET6;
443 wr = alloc_wrqe(act_open_cpl_size(sc, isipv6), toep->ctrlq);
445 DONT_OFFLOAD_ACTIVE_OPEN(ENOMEM);
447 toep->vnet = so->so_vnet;
448 set_ulp_mode(toep, select_ulp_mode(so, sc, &settings));
449 SOCKBUF_LOCK(&so->so_rcv);
450 /* opt0 rcv_bufsiz initially, assumes its normal meaning later */
451 toep->rx_credits = min(select_rcv_wnd(so) >> 10, M_RCV_BUFSIZ);
452 SOCKBUF_UNLOCK(&so->so_rcv);
455 * The kernel sets request_r_scale based on sb_max whereas we need to
456 * take hardware's MAX_RCV_WND into account too. This is normally a
457 * no-op as MAX_RCV_WND is much larger than the default sb_max.
459 if (tp->t_flags & TF_REQ_SCALE)
460 rscale = tp->request_r_scale = select_rcv_wscale();
463 mtu_idx = find_best_mtu_idx(sc, &inp->inp_inc, &settings);
464 qid_atid = (toep->ofld_rxq->iq.abs_id << 14) | toep->tid;
467 struct cpl_act_open_req6 *cpl = wrtod(wr);
468 struct cpl_t5_act_open_req6 *cpl5 = (void *)cpl;
469 struct cpl_t6_act_open_req6 *cpl6 = (void *)cpl;
471 if ((inp->inp_vflag & INP_IPV6) == 0)
472 DONT_OFFLOAD_ACTIVE_OPEN(ENOTSUP);
474 toep->ce = hold_lip(td, &inp->in6p_laddr, NULL);
475 if (toep->ce == NULL)
476 DONT_OFFLOAD_ACTIVE_OPEN(ENOENT);
478 switch (chip_id(sc)) {
481 cpl->params = select_ntuple(vi, toep->l2te);
485 cpl5->iss = htobe32(tp->iss);
486 cpl5->params = select_ntuple(vi, toep->l2te);
491 cpl6->iss = htobe32(tp->iss);
492 cpl6->params = select_ntuple(vi, toep->l2te);
495 OPCODE_TID(cpl) = htobe32(MK_OPCODE_TID(CPL_ACT_OPEN_REQ6,
497 cpl->local_port = inp->inp_lport;
498 cpl->local_ip_hi = *(uint64_t *)&inp->in6p_laddr.s6_addr[0];
499 cpl->local_ip_lo = *(uint64_t *)&inp->in6p_laddr.s6_addr[8];
500 cpl->peer_port = inp->inp_fport;
501 cpl->peer_ip_hi = *(uint64_t *)&inp->in6p_faddr.s6_addr[0];
502 cpl->peer_ip_lo = *(uint64_t *)&inp->in6p_faddr.s6_addr[8];
503 cpl->opt0 = calc_opt0(so, vi, toep->l2te, mtu_idx, rscale,
504 toep->rx_credits, toep->ulp_mode, &settings);
505 cpl->opt2 = calc_opt2a(so, toep, &settings);
507 struct cpl_act_open_req *cpl = wrtod(wr);
508 struct cpl_t5_act_open_req *cpl5 = (void *)cpl;
509 struct cpl_t6_act_open_req *cpl6 = (void *)cpl;
511 switch (chip_id(sc)) {
514 cpl->params = select_ntuple(vi, toep->l2te);
518 cpl5->iss = htobe32(tp->iss);
519 cpl5->params = select_ntuple(vi, toep->l2te);
524 cpl6->iss = htobe32(tp->iss);
525 cpl6->params = select_ntuple(vi, toep->l2te);
528 OPCODE_TID(cpl) = htobe32(MK_OPCODE_TID(CPL_ACT_OPEN_REQ,
530 inp_4tuple_get(inp, &cpl->local_ip, &cpl->local_port,
531 &cpl->peer_ip, &cpl->peer_port);
532 cpl->opt0 = calc_opt0(so, vi, toep->l2te, mtu_idx, rscale,
533 toep->rx_credits, toep->ulp_mode, &settings);
534 cpl->opt2 = calc_opt2a(so, toep, &settings);
537 CTR5(KTR_CXGBE, "%s: atid %u (%s), toep %p, inp %p", __func__,
538 toep->tid, tcpstates[tp->t_state], toep, inp);
540 offload_socket(so, toep);
541 rc = t4_l2t_send(sc, wr, toep->l2te);
543 toep->flags |= TPF_CPL_PENDING;
547 undo_offload_socket(so);
550 CTR3(KTR_CXGBE, "%s: not offloading (%d), rc %d", __func__, reason, rc);
557 free_atid(sc, toep->tid);
559 t4_l2t_release(toep->l2te);
561 release_lip(td, toep->ce);