2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2012 Chelsio Communications, Inc.
6 * Written by: Navdeep Parhar <np@FreeBSD.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
34 #include "opt_inet6.h"
36 #include <sys/param.h>
37 #include <sys/kernel.h>
38 #include <sys/systm.h>
39 #include <sys/malloc.h>
41 #include <sys/module.h>
42 #include <sys/types.h>
43 #include <sys/sockopt.h>
44 #include <sys/sysctl.h>
45 #include <sys/socket.h>
47 #include <net/ethernet.h>
49 #include <net/if_var.h>
50 #include <net/if_types.h>
51 #include <net/if_vlan_var.h>
52 #include <net/if_llatbl.h>
53 #include <net/route.h>
55 #include <netinet/if_ether.h>
56 #include <netinet/in.h>
57 #include <netinet/in_pcb.h>
58 #include <netinet/in_var.h>
59 #include <netinet6/in6_var.h>
60 #include <netinet6/in6_pcb.h>
61 #include <netinet6/nd6.h>
63 #include <netinet/tcp.h>
64 #include <netinet/tcp_fsm.h>
65 #include <netinet/tcp_timer.h>
66 #include <netinet/tcp_var.h>
67 #include <netinet/tcp_syncache.h>
68 #include <netinet/tcp_offload.h>
69 #include <netinet/toecore.h>
71 static struct mtx toedev_lock;
72 static TAILQ_HEAD(, toedev) toedev_list;
73 static eventhandler_tag listen_start_eh;
74 static eventhandler_tag listen_stop_eh;
75 static eventhandler_tag lle_event_eh;
78 toedev_connect(struct toedev *tod __unused, struct socket *so __unused,
79 struct rtentry *rt __unused, struct sockaddr *nam __unused)
86 toedev_listen_start(struct toedev *tod __unused, struct tcpcb *tp __unused)
93 toedev_listen_stop(struct toedev *tod __unused, struct tcpcb *tp __unused)
100 toedev_input(struct toedev *tod __unused, struct tcpcb *tp __unused,
109 toedev_rcvd(struct toedev *tod __unused, struct tcpcb *tp __unused)
116 toedev_output(struct toedev *tod __unused, struct tcpcb *tp __unused)
123 toedev_pcb_detach(struct toedev *tod __unused, struct tcpcb *tp __unused)
130 toedev_l2_update(struct toedev *tod __unused, struct ifnet *ifp __unused,
131 struct sockaddr *sa __unused, uint8_t *lladdr __unused,
132 uint16_t vtag __unused)
139 toedev_route_redirect(struct toedev *tod __unused, struct ifnet *ifp __unused,
140 struct rtentry *rt0 __unused, struct rtentry *rt1 __unused)
147 toedev_syncache_added(struct toedev *tod __unused, void *ctx __unused)
154 toedev_syncache_removed(struct toedev *tod __unused, void *ctx __unused)
161 toedev_syncache_respond(struct toedev *tod __unused, void *ctx __unused,
170 toedev_offload_socket(struct toedev *tod __unused, void *ctx __unused,
171 struct socket *so __unused)
178 toedev_ctloutput(struct toedev *tod __unused, struct tcpcb *tp __unused,
179 int sopt_dir __unused, int sopt_name __unused)
186 toedev_tcp_info(struct toedev *tod __unused, struct tcpcb *tp __unused,
187 struct tcp_info *ti __unused)
194 * Inform one or more TOE devices about a listening socket.
197 toe_listen_start(struct inpcb *inp, void *arg)
199 struct toedev *t, *tod;
202 INP_WLOCK_ASSERT(inp);
203 KASSERT(inp->inp_pcbinfo == &V_tcbinfo,
204 ("%s: inp is not a TCP inp", __func__));
206 if (inp->inp_flags & (INP_DROPPED | INP_TIMEWAIT))
210 if (tp->t_state != TCPS_LISTEN)
214 mtx_lock(&toedev_lock);
215 TAILQ_FOREACH(tod, &toedev_list, link) {
216 if (t == NULL || t == tod)
217 tod->tod_listen_start(tod, tp);
219 mtx_unlock(&toedev_lock);
223 toe_listen_start_event(void *arg __unused, struct tcpcb *tp)
225 struct inpcb *inp = tp->t_inpcb;
227 INP_WLOCK_ASSERT(inp);
228 KASSERT(tp->t_state == TCPS_LISTEN,
229 ("%s: t_state %s", __func__, tcpstates[tp->t_state]));
231 toe_listen_start(inp, NULL);
235 toe_listen_stop_event(void *arg __unused, struct tcpcb *tp)
239 struct inpcb *inp = tp->t_inpcb;
242 INP_WLOCK_ASSERT(inp);
243 KASSERT(tp->t_state == TCPS_LISTEN,
244 ("%s: t_state %s", __func__, tcpstates[tp->t_state]));
246 mtx_lock(&toedev_lock);
247 TAILQ_FOREACH(tod, &toedev_list, link)
248 tod->tod_listen_stop(tod, tp);
249 mtx_unlock(&toedev_lock);
253 * Fill up a freshly allocated toedev struct with reasonable defaults.
256 init_toedev(struct toedev *tod)
259 tod->tod_softc = NULL;
262 * Provide no-op defaults so that the kernel can call any toedev
263 * function without having to check whether the TOE driver supplied one
266 tod->tod_connect = toedev_connect;
267 tod->tod_listen_start = toedev_listen_start;
268 tod->tod_listen_stop = toedev_listen_stop;
269 tod->tod_input = toedev_input;
270 tod->tod_rcvd = toedev_rcvd;
271 tod->tod_output = toedev_output;
272 tod->tod_send_rst = toedev_output;
273 tod->tod_send_fin = toedev_output;
274 tod->tod_pcb_detach = toedev_pcb_detach;
275 tod->tod_l2_update = toedev_l2_update;
276 tod->tod_route_redirect = toedev_route_redirect;
277 tod->tod_syncache_added = toedev_syncache_added;
278 tod->tod_syncache_removed = toedev_syncache_removed;
279 tod->tod_syncache_respond = toedev_syncache_respond;
280 tod->tod_offload_socket = toedev_offload_socket;
281 tod->tod_ctloutput = toedev_ctloutput;
282 tod->tod_tcp_info = toedev_tcp_info;
286 * Register an active TOE device with the system. This allows it to receive
287 * notifications from the kernel.
290 register_toedev(struct toedev *tod)
294 mtx_lock(&toedev_lock);
295 TAILQ_FOREACH(t, &toedev_list, link) {
297 mtx_unlock(&toedev_lock);
302 TAILQ_INSERT_TAIL(&toedev_list, tod, link);
303 registered_toedevs++;
304 mtx_unlock(&toedev_lock);
306 inp_apply_all(toe_listen_start, tod);
312 * Remove the TOE device from the global list of active TOE devices. It is the
313 * caller's responsibility to ensure that the TOE device is quiesced prior to
317 unregister_toedev(struct toedev *tod)
319 struct toedev *t, *t2;
322 mtx_lock(&toedev_lock);
323 TAILQ_FOREACH_SAFE(t, &toedev_list, link, t2) {
325 TAILQ_REMOVE(&toedev_list, tod, link);
326 registered_toedevs--;
331 KASSERT(registered_toedevs >= 0,
332 ("%s: registered_toedevs (%d) < 0", __func__, registered_toedevs));
333 mtx_unlock(&toedev_lock);
338 toe_syncache_add(struct in_conninfo *inc, struct tcpopt *to, struct tcphdr *th,
339 struct inpcb *inp, void *tod, void *todctx)
341 struct socket *lso = inp->inp_socket;
343 INP_WLOCK_ASSERT(inp);
345 syncache_add(inc, to, th, inp, &lso, NULL, tod, todctx);
349 toe_syncache_expand(struct in_conninfo *inc, struct tcpopt *to,
350 struct tcphdr *th, struct socket **lsop)
353 INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
355 return (syncache_expand(inc, to, th, lsop, NULL));
359 * General purpose check to see if a 4-tuple is in use by the kernel. If a TCP
360 * header (presumably for an incoming SYN) is also provided, an existing 4-tuple
361 * in TIME_WAIT may be assassinated freeing it up for re-use.
363 * Note that the TCP header must have been run through tcp_fields_to_host() or
367 toe_4tuple_check(struct in_conninfo *inc, struct tcphdr *th, struct ifnet *ifp)
371 if (inc->inc_flags & INC_ISIPV6) {
372 inp = in6_pcblookup(&V_tcbinfo, &inc->inc6_faddr,
373 inc->inc_fport, &inc->inc6_laddr, inc->inc_lport,
374 INPLOOKUP_WLOCKPCB, ifp);
376 inp = in_pcblookup(&V_tcbinfo, inc->inc_faddr, inc->inc_fport,
377 inc->inc_laddr, inc->inc_lport, INPLOOKUP_WLOCKPCB, ifp);
380 INP_WLOCK_ASSERT(inp);
382 if ((inp->inp_flags & INP_TIMEWAIT) && th != NULL) {
384 INP_INFO_RLOCK_ASSERT(&V_tcbinfo); /* for twcheck */
385 if (!tcp_twcheck(inp, NULL, th, NULL, 0))
397 toe_lle_event(void *arg __unused, struct llentry *lle, int evt)
405 struct sockaddr_in6 sin6;
407 LLE_WLOCK_ASSERT(lle);
409 ifp = lltable_get_ifp(lle->lle_tbl);
410 family = lltable_get_af(lle->lle_tbl);
412 if (family != AF_INET && family != AF_INET6)
415 * Not interested if the interface's TOE capability is not enabled.
417 if ((family == AF_INET && !(ifp->if_capenable & IFCAP_TOE4)) ||
418 (family == AF_INET6 && !(ifp->if_capenable & IFCAP_TOE6)))
425 sa = (struct sockaddr *)&sin6;
426 lltable_fill_sa_entry(lle, sa);
429 if (evt != LLENTRY_RESOLVED) {
432 * LLENTRY_TIMEDOUT, LLENTRY_DELETED, LLENTRY_EXPIRED all mean
433 * this entry is going to be deleted.
439 KASSERT(lle->la_flags & LLE_VALID,
440 ("%s: %p resolved but not valid?", __func__, lle));
442 lladdr = (uint8_t *)lle->ll_addr;
444 VLAN_TAG(ifp, &vtag);
448 tod->tod_l2_update(tod, ifp, sa, lladdr, vtag);
452 * Returns 0 or EWOULDBLOCK on success (any other value is an error). 0 means
453 * lladdr and vtag are valid on return, EWOULDBLOCK means the TOE driver's
454 * tod_l2_update will be called later, when the entry is resolved or times out.
457 toe_l2_resolve(struct toedev *tod, struct ifnet *ifp, struct sockaddr *sa,
458 uint8_t *lladdr, uint16_t *vtag)
462 switch (sa->sa_family) {
465 rc = arpresolve(ifp, 0, NULL, sa, lladdr, NULL, NULL);
470 rc = nd6_resolve(ifp, 0, NULL, sa, lladdr, NULL, NULL);
474 return (EPROTONOSUPPORT);
479 if (VLAN_TAG(ifp, vtag) != 0)
488 toe_connect_failed(struct toedev *tod, struct inpcb *inp, int err)
491 INP_WLOCK_ASSERT(inp);
493 if (!(inp->inp_flags & INP_DROPPED)) {
494 struct tcpcb *tp = intotcpcb(inp);
496 KASSERT(tp->t_flags & TF_TOE,
497 ("%s: tp %p not offloaded.", __func__, tp));
502 * Temporary failure during offload, take this PCB back.
503 * Detach from the TOE driver and do the rest of what
504 * TCP's pru_connect would have done if the connection
508 tod->tod_pcb_detach(tod, tp);
509 KASSERT(!(tp->t_flags & TF_TOE),
510 ("%s: tp %p still offloaded.", __func__, tp));
511 tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
512 (void) tp->t_fb->tfb_tcp_output(tp);
515 INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
516 tp = tcp_drop(tp, err);
518 INP_WLOCK(inp); /* re-acquire */
521 INP_WLOCK_ASSERT(inp);
528 mtx_init(&toedev_lock, "toedev lock", NULL, MTX_DEF);
529 TAILQ_INIT(&toedev_list);
531 listen_start_eh = EVENTHANDLER_REGISTER(tcp_offload_listen_start,
532 toe_listen_start_event, NULL, EVENTHANDLER_PRI_ANY);
533 listen_stop_eh = EVENTHANDLER_REGISTER(tcp_offload_listen_stop,
534 toe_listen_stop_event, NULL, EVENTHANDLER_PRI_ANY);
535 lle_event_eh = EVENTHANDLER_REGISTER(lle_event, toe_lle_event, NULL,
536 EVENTHANDLER_PRI_ANY);
545 mtx_lock(&toedev_lock);
546 if (!TAILQ_EMPTY(&toedev_list)) {
547 mtx_unlock(&toedev_lock);
551 EVENTHANDLER_DEREGISTER(tcp_offload_listen_start, listen_start_eh);
552 EVENTHANDLER_DEREGISTER(tcp_offload_listen_stop, listen_stop_eh);
553 EVENTHANDLER_DEREGISTER(lle_event, lle_event_eh);
555 mtx_unlock(&toedev_lock);
556 mtx_destroy(&toedev_lock);
562 toecore_mod_handler(module_t mod, int cmd, void *arg)
566 return (toecore_load());
568 if (cmd == MOD_UNLOAD)
569 return (toecore_unload());
574 static moduledata_t mod_data= {
580 MODULE_VERSION(toecore, 1);
581 DECLARE_MODULE(toecore, mod_data, SI_SUB_EXEC, SI_ORDER_ANY);