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