xref: /freebsd/sys/netinet/tcp_offload.c (revision 257480b8ab7aee57ca2af13497f628b35b3ba08a)
1 /*-
2  * Copyright (c) 2012 Chelsio Communications, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include "opt_inet.h"
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/types.h>
35 #include <sys/mbuf.h>
36 #include <sys/socket.h>
37 #include <sys/socketvar.h>
38 #include <sys/sockopt.h>
39 #include <net/if.h>
40 #include <net/if_var.h>
41 #include <net/route.h>
42 #include <netinet/in.h>
43 #include <netinet/in_pcb.h>
44 #include <netinet/tcp.h>
45 #include <netinet/tcp_var.h>
46 #include <netinet/tcp_offload.h>
47 #include <net/rt_nhops.h>
48 #define	TCPOUTFLAGS
49 #include <netinet/tcp_fsm.h>
50 #include <netinet/toecore.h>
51 #include <netinet6/scope6_var.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 nhopu_extended nhu_ext;
64 	int af, error = EOPNOTSUPP;
65 	int fibnum;
66 
67 	INP_WLOCK_ASSERT(sotoinpcb(so));
68 	KASSERT(nam->sa_family == AF_INET || nam->sa_family == AF_INET6,
69 	    ("%s: called with sa_family %d", __func__, nam->sa_family));
70 
71 	if (registered_toedevs == 0)
72 		return (error);
73 
74 	af = nam->sa_family;
75 	fibnum = so->so_fibnum;
76 	ifp = NULL;
77 
78 	/* TODO: Multipath */
79 	if (af == AF_INET) {
80 		if (fib4_lookup_nh_ext(fibnum,
81 		    ((struct sockaddr_in *)nam)->sin_addr,
82 		    0, NHOP_LOOKUP_REF, &nhu_ext.u.nh4) != 0)
83 			return (EHOSTUNREACH);
84 
85 		ifp = nhu_ext.u.nh4.nh_ifp;
86 		if ((ifp->if_capenable & IFCAP_TOE4) == 0)
87 			goto done;
88 	} else if (af == AF_INET6) {
89 		struct sockaddr_in6 *sin6;
90 		struct in6_addr dst;
91 		uint32_t scopeid;
92 
93 		sin6 = (struct sockaddr_in6 *)nam;
94 		in6_splitscope(&sin6->sin6_addr, &dst, &scopeid);
95 
96 		if (fib6_lookup_nh_ext(fibnum, &dst, scopeid,
97 		    0, NHOP_LOOKUP_REF, &nhu_ext.u.nh6) != 0)
98 			return (EHOSTUNREACH);
99 
100 		ifp = nhu_ext.u.nh6.nh_ifp;
101 		if ((ifp->if_capenable & IFCAP_TOE6) == 0)
102 			goto done;
103 	}
104 
105 	tod = TOEDEV(ifp);
106 	if (tod != NULL)
107 		error = tod->tod_connect(tod, so, &nhu_ext, nam);
108 done:
109 	fib_free_nh_ext(fibnum, &nhu_ext);
110 	return (error);
111 }
112 
113 void
114 tcp_offload_listen_start(struct tcpcb *tp)
115 {
116 
117 	INP_WLOCK_ASSERT(tp->t_inpcb);
118 
119 	EVENTHANDLER_INVOKE(tcp_offload_listen_start, tp);
120 }
121 
122 void
123 tcp_offload_listen_stop(struct tcpcb *tp)
124 {
125 
126 	INP_WLOCK_ASSERT(tp->t_inpcb);
127 
128 	EVENTHANDLER_INVOKE(tcp_offload_listen_stop, tp);
129 }
130 
131 void
132 tcp_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));
137 	INP_WLOCK_ASSERT(tp->t_inpcb);
138 
139 	tod->tod_input(tod, tp, m);
140 }
141 
142 int
143 tcp_offload_output(struct tcpcb *tp)
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));
149 	INP_WLOCK_ASSERT(tp->t_inpcb);
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 
167 void
168 tcp_offload_rcvd(struct tcpcb *tp)
169 {
170 	struct toedev *tod = tp->tod;
171 
172 	KASSERT(tod != NULL, ("%s: tp->tod is NULL, tp %p", __func__, tp));
173 	INP_WLOCK_ASSERT(tp->t_inpcb);
174 
175 	tod->tod_rcvd(tod, tp);
176 }
177 
178 void
179 tcp_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));
184 	INP_WLOCK_ASSERT(tp->t_inpcb);
185 
186 	tod->tod_ctloutput(tod, tp, sopt_dir, sopt_name);
187 }
188 
189 void
190 tcp_offload_detach(struct tcpcb *tp)
191 {
192 	struct toedev *tod = tp->tod;
193 
194 	KASSERT(tod != NULL, ("%s: tp->tod is NULL, tp %p", __func__, tp));
195 	INP_WLOCK_ASSERT(tp->t_inpcb);
196 
197 	tod->tod_pcb_detach(tod, tp);
198 }
199