xref: /freebsd/sys/netinet/tcp_offload.c (revision 2008043f386721d58158e37e0d7e50df8095942d)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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 #include "opt_inet.h"
31 #include "opt_inet6.h"
32 
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/eventhandler.h>
36 #include <sys/mbuf.h>
37 #include <sys/socket.h>
38 #include <sys/socketvar.h>
39 #include <sys/sockopt.h>
40 #include <net/if.h>
41 #include <net/if_var.h>
42 #include <net/if_private.h>
43 #include <net/route.h>
44 #include <net/route/nhop.h>
45 #include <netinet/in.h>
46 #include <netinet/in_pcb.h>
47 #include <netinet/in_fib.h>
48 #include <netinet6/in6_fib.h>
49 #include <netinet/tcp.h>
50 #include <netinet/tcp_offload.h>
51 #define	TCPOUTFLAGS
52 #include <netinet/tcp_fsm.h>
53 #include <netinet/tcp_var.h>
54 #include <netinet/toecore.h>
55 
56 int registered_toedevs;
57 
58 /*
59  * Provide an opportunity for a TOE driver to offload.
60  */
61 int
62 tcp_offload_connect(struct socket *so, struct sockaddr *nam)
63 {
64 	struct ifnet *ifp;
65 	struct toedev *tod;
66 	struct nhop_object *nh;
67 	struct epoch_tracker et;
68 	int error = EOPNOTSUPP;
69 
70 	INP_WLOCK_ASSERT(sotoinpcb(so));
71 	KASSERT(nam->sa_family == AF_INET || nam->sa_family == AF_INET6,
72 	    ("%s: called with sa_family %d", __func__, nam->sa_family));
73 
74 	if (registered_toedevs == 0)
75 		return (error);
76 
77 	NET_EPOCH_ENTER(et);
78 	nh = NULL;
79 #ifdef INET
80 	if (nam->sa_family == AF_INET)
81 		nh = fib4_lookup(0, ((struct sockaddr_in *)nam)->sin_addr,
82 		    NHR_NONE, 0, 0);
83 #endif
84 #if defined(INET) && defined(INET6)
85 	else
86 #endif
87 #ifdef INET6
88 	if (nam->sa_family == AF_INET6)
89 		nh = fib6_lookup(0, &((struct sockaddr_in6 *)nam)->sin6_addr,
90 		    NHR_NONE, 0, 0);
91 #endif
92 	if (nh == NULL) {
93 		NET_EPOCH_EXIT(et);
94 		return (EHOSTUNREACH);
95 	}
96 
97 	ifp = nh->nh_ifp;
98 
99 	if (nam->sa_family == AF_INET && !(ifp->if_capenable & IFCAP_TOE4))
100 		goto done;
101 	if (nam->sa_family == AF_INET6 && !(ifp->if_capenable & IFCAP_TOE6))
102 		goto done;
103 
104 	tod = TOEDEV(ifp);
105 	if (tod != NULL)
106 		error = tod->tod_connect(tod, so, nh, nam);
107 done:
108 	NET_EPOCH_EXIT(et);
109 	return (error);
110 }
111 
112 void
113 tcp_offload_listen_start(struct tcpcb *tp)
114 {
115 
116 	INP_WLOCK_ASSERT(tptoinpcb(tp));
117 
118 	EVENTHANDLER_INVOKE(tcp_offload_listen_start, tp);
119 }
120 
121 void
122 tcp_offload_listen_stop(struct tcpcb *tp)
123 {
124 
125 	INP_WLOCK_ASSERT(tptoinpcb(tp));
126 
127 	EVENTHANDLER_INVOKE(tcp_offload_listen_stop, tp);
128 }
129 
130 void
131 tcp_offload_input(struct tcpcb *tp, struct mbuf *m)
132 {
133 	struct toedev *tod = tp->tod;
134 
135 	KASSERT(tod != NULL, ("%s: tp->tod is NULL, tp %p", __func__, tp));
136 	INP_WLOCK_ASSERT(tptoinpcb(tp));
137 
138 	tod->tod_input(tod, tp, m);
139 }
140 
141 int
142 tcp_offload_output(struct tcpcb *tp)
143 {
144 	struct toedev *tod = tp->tod;
145 	int error, flags;
146 
147 	KASSERT(tod != NULL, ("%s: tp->tod is NULL, tp %p", __func__, tp));
148 	INP_WLOCK_ASSERT(tptoinpcb(tp));
149 
150 	flags = tcp_outflags[tp->t_state];
151 
152 	if (flags & TH_RST) {
153 		/* XXX: avoid repeated calls like we do for FIN */
154 		error = tod->tod_send_rst(tod, tp);
155 	} else if ((flags & TH_FIN || tp->t_flags & TF_NEEDFIN) &&
156 	    (tp->t_flags & TF_SENTFIN) == 0) {
157 		error = tod->tod_send_fin(tod, tp);
158 		if (error == 0)
159 			tp->t_flags |= TF_SENTFIN;
160 	} else
161 		error = tod->tod_output(tod, tp);
162 
163 	return (error);
164 }
165 
166 void
167 tcp_offload_rcvd(struct tcpcb *tp)
168 {
169 	struct toedev *tod = tp->tod;
170 
171 	KASSERT(tod != NULL, ("%s: tp->tod is NULL, tp %p", __func__, tp));
172 	INP_WLOCK_ASSERT(tptoinpcb(tp));
173 
174 	tod->tod_rcvd(tod, tp);
175 }
176 
177 void
178 tcp_offload_ctloutput(struct tcpcb *tp, int sopt_dir, int sopt_name)
179 {
180 	struct toedev *tod = tp->tod;
181 
182 	KASSERT(tod != NULL, ("%s: tp->tod is NULL, tp %p", __func__, tp));
183 	INP_WLOCK_ASSERT(tptoinpcb(tp));
184 
185 	tod->tod_ctloutput(tod, tp, sopt_dir, sopt_name);
186 }
187 
188 void
189 tcp_offload_tcp_info(const struct tcpcb *tp, struct tcp_info *ti)
190 {
191 	struct toedev *tod = tp->tod;
192 
193 	KASSERT(tod != NULL, ("%s: tp->tod is NULL, tp %p", __func__, tp));
194 	INP_LOCK_ASSERT(tptoinpcb(tp));
195 
196 	tod->tod_tcp_info(tod, tp, ti);
197 }
198 
199 int
200 tcp_offload_alloc_tls_session(struct tcpcb *tp, struct ktls_session *tls,
201     int direction)
202 {
203 	struct toedev *tod = tp->tod;
204 
205 	KASSERT(tod != NULL, ("%s: tp->tod is NULL, tp %p", __func__, tp));
206 	INP_WLOCK_ASSERT(tptoinpcb(tp));
207 
208 	return (tod->tod_alloc_tls_session(tod, tp, tls, direction));
209 }
210 
211 void
212 tcp_offload_detach(struct tcpcb *tp)
213 {
214 	struct toedev *tod = tp->tod;
215 
216 	KASSERT(tod != NULL, ("%s: tp->tod is NULL, tp %p", __func__, tp));
217 	INP_WLOCK_ASSERT(tptoinpcb(tp));
218 
219 	tod->tod_pcb_detach(tod, tp);
220 }
221 
222 void
223 tcp_offload_pmtu_update(struct tcpcb *tp, tcp_seq seq, int mtu)
224 {
225 	struct toedev *tod = tp->tod;
226 
227 	KASSERT(tod != NULL, ("%s: tp->tod is NULL, tp %p", __func__, tp));
228 	INP_WLOCK_ASSERT(tptoinpcb(tp));
229 
230 	tod->tod_pmtu_update(tod, tp, seq, mtu);
231 }
232