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