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