xref: /freebsd/sys/netinet/tcp_offload.c (revision 5b9c547c)
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 #define	TCPOUTFLAGS
48 #include <netinet/tcp_fsm.h>
49 #include <netinet/toecore.h>
50 
51 int registered_toedevs;
52 
53 /*
54  * Provide an opportunity for a TOE driver to offload.
55  */
56 int
57 tcp_offload_connect(struct socket *so, struct sockaddr *nam)
58 {
59 	struct ifnet *ifp;
60 	struct toedev *tod;
61 	struct rtentry *rt;
62 	int error = EOPNOTSUPP;
63 
64 	INP_WLOCK_ASSERT(sotoinpcb(so));
65 	KASSERT(nam->sa_family == AF_INET || nam->sa_family == AF_INET6,
66 	    ("%s: called with sa_family %d", __func__, nam->sa_family));
67 
68 	if (registered_toedevs == 0)
69 		return (error);
70 
71 	rt = rtalloc1(nam, 0, 0);
72 	if (rt)
73 		RT_UNLOCK(rt);
74 	else
75 		return (EHOSTUNREACH);
76 
77 	ifp = rt->rt_ifp;
78 
79 	if (nam->sa_family == AF_INET && !(ifp->if_capenable & IFCAP_TOE4))
80 		goto done;
81 	if (nam->sa_family == AF_INET6 && !(ifp->if_capenable & IFCAP_TOE6))
82 		goto done;
83 
84 	tod = TOEDEV(ifp);
85 	if (tod != NULL)
86 		error = tod->tod_connect(tod, so, rt, nam);
87 done:
88 	RTFREE(rt);
89 	return (error);
90 }
91 
92 void
93 tcp_offload_listen_start(struct tcpcb *tp)
94 {
95 
96 	INP_WLOCK_ASSERT(tp->t_inpcb);
97 
98 	EVENTHANDLER_INVOKE(tcp_offload_listen_start, tp);
99 }
100 
101 void
102 tcp_offload_listen_stop(struct tcpcb *tp)
103 {
104 
105 	INP_WLOCK_ASSERT(tp->t_inpcb);
106 
107 	EVENTHANDLER_INVOKE(tcp_offload_listen_stop, tp);
108 }
109 
110 void
111 tcp_offload_input(struct tcpcb *tp, struct mbuf *m)
112 {
113 	struct toedev *tod = tp->tod;
114 
115 	KASSERT(tod != NULL, ("%s: tp->tod is NULL, tp %p", __func__, tp));
116 	INP_WLOCK_ASSERT(tp->t_inpcb);
117 
118 	tod->tod_input(tod, tp, m);
119 }
120 
121 int
122 tcp_offload_output(struct tcpcb *tp)
123 {
124 	struct toedev *tod = tp->tod;
125 	int error, flags;
126 
127 	KASSERT(tod != NULL, ("%s: tp->tod is NULL, tp %p", __func__, tp));
128 	INP_WLOCK_ASSERT(tp->t_inpcb);
129 
130 	flags = tcp_outflags[tp->t_state];
131 
132 	if (flags & TH_RST) {
133 		/* XXX: avoid repeated calls like we do for FIN */
134 		error = tod->tod_send_rst(tod, tp);
135 	} else if ((flags & TH_FIN || tp->t_flags & TF_NEEDFIN) &&
136 	    (tp->t_flags & TF_SENTFIN) == 0) {
137 		error = tod->tod_send_fin(tod, tp);
138 		if (error == 0)
139 			tp->t_flags |= TF_SENTFIN;
140 	} else
141 		error = tod->tod_output(tod, tp);
142 
143 	return (error);
144 }
145 
146 void
147 tcp_offload_rcvd(struct tcpcb *tp)
148 {
149 	struct toedev *tod = tp->tod;
150 
151 	KASSERT(tod != NULL, ("%s: tp->tod is NULL, tp %p", __func__, tp));
152 	INP_WLOCK_ASSERT(tp->t_inpcb);
153 
154 	tod->tod_rcvd(tod, tp);
155 }
156 
157 void
158 tcp_offload_ctloutput(struct tcpcb *tp, int sopt_dir, int sopt_name)
159 {
160 	struct toedev *tod = tp->tod;
161 
162 	KASSERT(tod != NULL, ("%s: tp->tod is NULL, tp %p", __func__, tp));
163 	INP_WLOCK_ASSERT(tp->t_inpcb);
164 
165 	tod->tod_ctloutput(tod, tp, sopt_dir, sopt_name);
166 }
167 
168 void
169 tcp_offload_detach(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(tp->t_inpcb);
175 
176 	tod->tod_pcb_detach(tod, tp);
177 }
178