tcp_offload.c revision 194739
1/*-
2 * Copyright (c) 2007, Chelsio 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 are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 *    this list of conditions and the following disclaimer.
10 *
11 * 2. Neither the name of the Chelsio Corporation nor the names of its
12 *    contributors may be used to endorse or promote products derived from
13 *    this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include <sys/cdefs.h>
29__FBSDID("$FreeBSD: head/sys/netinet/tcp_offload.c 194739 2009-06-23 17:03:45Z bz $");
30
31#include <sys/param.h>
32#include <sys/systm.h>
33#include <sys/types.h>
34#include <sys/malloc.h>
35#include <sys/kernel.h>
36#include <sys/sysctl.h>
37#include <sys/mbuf.h>
38#include <sys/socket.h>
39#include <sys/socketvar.h>
40#include <sys/vimage.h>
41
42#include <net/if.h>
43#include <net/if_types.h>
44#include <net/if_var.h>
45#include <net/route.h>
46
47#include <netinet/in.h>
48#include <netinet/in_systm.h>
49#include <netinet/in_pcb.h>
50#include <netinet/tcp.h>
51#include <netinet/tcp_var.h>
52#include <netinet/tcp_offload.h>
53#include <netinet/toedev.h>
54#include <netinet/vinet.h>
55
56uint32_t toedev_registration_count;
57
58int
59tcp_offload_connect(struct socket *so, struct sockaddr *nam)
60{
61	struct ifnet *ifp;
62	struct toedev *tdev;
63	struct rtentry *rt;
64	int error;
65
66	if (toedev_registration_count == 0)
67		return (EINVAL);
68
69	/*
70	 * Look up the route used for the connection to
71	 * determine if it uses an interface capable of
72	 * offloading the connection.
73	 */
74	rt = rtalloc1(nam, 0 /*report*/, 0 /*ignflags*/);
75	if (rt)
76		RT_UNLOCK(rt);
77	else
78		return (EHOSTUNREACH);
79
80	ifp = rt->rt_ifp;
81	if ((ifp->if_capenable & IFCAP_TOE) == 0) {
82		error = EINVAL;
83		goto fail;
84	}
85
86	tdev = TOEDEV(ifp);
87	if (tdev == NULL) {
88		error = EPERM;
89		goto fail;
90	}
91
92	if (tdev->tod_can_offload(tdev, so) == 0) {
93		error = EPERM;
94		goto fail;
95	}
96
97	return (tdev->tod_connect(tdev, so, rt, nam));
98fail:
99	RTFREE(rt);
100	return (error);
101}
102
103
104/*
105 * This file contains code as a short-term staging area before it is moved in
106 * to sys/netinet/tcp_offload.c
107 */
108
109void
110tcp_offload_twstart(struct tcpcb *tp)
111{
112	INIT_VNET_INET(curvnet);
113
114	INP_INFO_WLOCK(&V_tcbinfo);
115	INP_WLOCK(tp->t_inpcb);
116	tcp_twstart(tp);
117	INP_INFO_WUNLOCK(&V_tcbinfo);
118}
119
120struct tcpcb *
121tcp_offload_close(struct tcpcb *tp)
122{
123	INIT_VNET_INET(curvnet);
124
125	INP_INFO_WLOCK(&V_tcbinfo);
126	INP_WLOCK(tp->t_inpcb);
127	tp = tcp_close(tp);
128	INP_INFO_WUNLOCK(&V_tcbinfo);
129	if (tp)
130		INP_WUNLOCK(tp->t_inpcb);
131
132	return (tp);
133}
134
135struct tcpcb *
136tcp_offload_drop(struct tcpcb *tp, int error)
137{
138	INIT_VNET_INET(curvnet);
139
140	INP_INFO_WLOCK(&V_tcbinfo);
141	INP_WLOCK(tp->t_inpcb);
142	tp = tcp_drop(tp, error);
143	INP_INFO_WUNLOCK(&V_tcbinfo);
144	if (tp)
145		INP_WUNLOCK(tp->t_inpcb);
146
147	return (tp);
148}
149
150