xref: /freebsd/sys/netinet/tcp_offload.c (revision e1c167d0190dfde72fa06350ef0049181cf79a4f)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2012 Chelsio Communications, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include "opt_inet.h"
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/eventhandler.h>
37 #include <sys/mbuf.h>
38 #include <sys/socket.h>
39 #include <sys/socketvar.h>
40 #include <sys/sockopt.h>
41 #include <net/if.h>
42 #include <net/if_var.h>
43 #include <net/route.h>
44 #include <netinet/in.h>
45 #include <netinet/in_pcb.h>
46 #include <netinet/tcp.h>
47 #include <netinet/tcp_offload.h>
48 #define	TCPOUTFLAGS
49 #include <netinet/tcp_fsm.h>
50 #include <netinet/tcp_var.h>
51 #include <netinet/toecore.h>
52 
53 int registered_toedevs;
54 
55 /*
56  * Provide an opportunity for a TOE driver to offload.
57  */
58 int
59 tcp_offload_connect(struct socket *so, struct sockaddr *nam)
60 {
61 	struct ifnet *ifp;
62 	struct toedev *tod;
63 	struct rtentry *rt;
64 	int error = EOPNOTSUPP;
65 
66 	INP_WLOCK_ASSERT(sotoinpcb(so));
67 	KASSERT(nam->sa_family == AF_INET || nam->sa_family == AF_INET6,
68 	    ("%s: called with sa_family %d", __func__, nam->sa_family));
69 
70 	if (registered_toedevs == 0)
71 		return (error);
72 
73 	rt = rtalloc1(nam, 0, 0);
74 	if (rt)
75 		RT_UNLOCK(rt);
76 	else
77 		return (EHOSTUNREACH);
78 
79 	ifp = rt->rt_ifp;
80 
81 	if (nam->sa_family == AF_INET && !(ifp->if_capenable & IFCAP_TOE4))
82 		goto done;
83 	if (nam->sa_family == AF_INET6 && !(ifp->if_capenable & IFCAP_TOE6))
84 		goto done;
85 
86 	tod = TOEDEV(ifp);
87 	if (tod != NULL)
88 		error = tod->tod_connect(tod, so, rt, nam);
89 done:
90 	RTFREE(rt);
91 	return (error);
92 }
93 
94 void
95 tcp_offload_listen_start(struct tcpcb *tp)
96 {
97 
98 	INP_WLOCK_ASSERT(tp->t_inpcb);
99 
100 	EVENTHANDLER_INVOKE(tcp_offload_listen_start, tp);
101 }
102 
103 void
104 tcp_offload_listen_stop(struct tcpcb *tp)
105 {
106 
107 	INP_WLOCK_ASSERT(tp->t_inpcb);
108 
109 	EVENTHANDLER_INVOKE(tcp_offload_listen_stop, tp);
110 }
111 
112 void
113 tcp_offload_input(struct tcpcb *tp, struct mbuf *m)
114 {
115 	struct toedev *tod = tp->tod;
116 
117 	KASSERT(tod != NULL, ("%s: tp->tod is NULL, tp %p", __func__, tp));
118 	INP_WLOCK_ASSERT(tp->t_inpcb);
119 
120 	tod->tod_input(tod, tp, m);
121 }
122 
123 int
124 tcp_offload_output(struct tcpcb *tp)
125 {
126 	struct toedev *tod = tp->tod;
127 	int error, flags;
128 
129 	KASSERT(tod != NULL, ("%s: tp->tod is NULL, tp %p", __func__, tp));
130 	INP_WLOCK_ASSERT(tp->t_inpcb);
131 
132 	flags = tcp_outflags[tp->t_state];
133 
134 	if (flags & TH_RST) {
135 		/* XXX: avoid repeated calls like we do for FIN */
136 		error = tod->tod_send_rst(tod, tp);
137 	} else if ((flags & TH_FIN || tp->t_flags & TF_NEEDFIN) &&
138 	    (tp->t_flags & TF_SENTFIN) == 0) {
139 		error = tod->tod_send_fin(tod, tp);
140 		if (error == 0)
141 			tp->t_flags |= TF_SENTFIN;
142 	} else
143 		error = tod->tod_output(tod, tp);
144 
145 	return (error);
146 }
147 
148 void
149 tcp_offload_rcvd(struct tcpcb *tp)
150 {
151 	struct toedev *tod = tp->tod;
152 
153 	KASSERT(tod != NULL, ("%s: tp->tod is NULL, tp %p", __func__, tp));
154 	INP_WLOCK_ASSERT(tp->t_inpcb);
155 
156 	tod->tod_rcvd(tod, tp);
157 }
158 
159 void
160 tcp_offload_ctloutput(struct tcpcb *tp, int sopt_dir, int sopt_name)
161 {
162 	struct toedev *tod = tp->tod;
163 
164 	KASSERT(tod != NULL, ("%s: tp->tod is NULL, tp %p", __func__, tp));
165 	INP_WLOCK_ASSERT(tp->t_inpcb);
166 
167 	tod->tod_ctloutput(tod, tp, sopt_dir, sopt_name);
168 }
169 
170 void
171 tcp_offload_tcp_info(struct tcpcb *tp, struct tcp_info *ti)
172 {
173 	struct toedev *tod = tp->tod;
174 
175 	KASSERT(tod != NULL, ("%s: tp->tod is NULL, tp %p", __func__, tp));
176 	INP_WLOCK_ASSERT(tp->t_inpcb);
177 
178 	tod->tod_tcp_info(tod, tp, ti);
179 }
180 
181 int
182 tcp_offload_alloc_tls_session(struct tcpcb *tp, struct ktls_session *tls)
183 {
184 	struct toedev *tod = tp->tod;
185 
186 	KASSERT(tod != NULL, ("%s: tp->tod is NULL, tp %p", __func__, tp));
187 	INP_WLOCK_ASSERT(tp->t_inpcb);
188 
189 	return (tod->tod_alloc_tls_session(tod, tp, tls));
190 }
191 
192 void
193 tcp_offload_detach(struct tcpcb *tp)
194 {
195 	struct toedev *tod = tp->tod;
196 
197 	KASSERT(tod != NULL, ("%s: tp->tod is NULL, tp %p", __func__, tp));
198 	INP_WLOCK_ASSERT(tp->t_inpcb);
199 
200 	tod->tod_pcb_detach(tod, tp);
201 }
202