tcp_offload.c revision 182591
1174704Skmacy/*-
2174704Skmacy * Copyright (c) 2007, Chelsio Inc.
3174704Skmacy * All rights reserved.
4174704Skmacy *
5174704Skmacy * Redistribution and use in source and binary forms, with or without
6174704Skmacy * modification, are permitted provided that the following conditions are met:
7174704Skmacy *
8174704Skmacy * 1. Redistributions of source code must retain the above copyright notice,
9174704Skmacy *    this list of conditions and the following disclaimer.
10174704Skmacy *
11174704Skmacy * 2. Neither the name of the Chelsio Corporation nor the names of its
12174704Skmacy *    contributors may be used to endorse or promote products derived from
13174704Skmacy *    this software without specific prior written permission.
14174704Skmacy *
15174704Skmacy * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16174704Skmacy * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17174704Skmacy * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18174704Skmacy * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19174704Skmacy * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20174704Skmacy * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21174704Skmacy * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22174704Skmacy * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23174704Skmacy * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24174704Skmacy * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25174704Skmacy * POSSIBILITY OF SUCH DAMAGE.
26174704Skmacy */
27174704Skmacy
28174704Skmacy#include <sys/cdefs.h>
29174704Skmacy__FBSDID("$FreeBSD: head/sys/netinet/tcp_offload.c 182591 2008-09-01 05:30:22Z kmacy $");
30174704Skmacy
31174704Skmacy#include <sys/param.h>
32174704Skmacy#include <sys/systm.h>
33174704Skmacy#include <sys/types.h>
34174704Skmacy#include <sys/malloc.h>
35174704Skmacy#include <sys/kernel.h>
36174704Skmacy#include <sys/sysctl.h>
37174704Skmacy#include <sys/mbuf.h>
38174704Skmacy#include <sys/socket.h>
39174704Skmacy#include <sys/socketvar.h>
40181803Sbz#include <sys/vimage.h>
41174704Skmacy
42174704Skmacy#include <net/if.h>
43174704Skmacy#include <net/if_types.h>
44174704Skmacy#include <net/if_var.h>
45174704Skmacy
46174704Skmacy#include <netinet/in.h>
47174704Skmacy#include <netinet/in_systm.h>
48174704Skmacy#include <netinet/in_pcb.h>
49174704Skmacy#include <netinet/tcp.h>
50174704Skmacy#include <netinet/tcp_var.h>
51174704Skmacy#include <netinet/tcp_offload.h>
52174704Skmacy#include <netinet/toedev.h>
53174704Skmacy
54182591Skmacyuint32_t toedev_registration_count;
55182591Skmacy
56174704Skmacyint
57174704Skmacytcp_offload_connect(struct socket *so, struct sockaddr *nam)
58174704Skmacy{
59174704Skmacy	struct ifnet *ifp;
60174704Skmacy	struct toedev *tdev;
61174704Skmacy	struct rtentry *rt;
62174704Skmacy	int error;
63174704Skmacy
64182591Skmacy	if (toedev_registration_count == 0)
65182591Skmacy		return (EINVAL);
66182591Skmacy
67174704Skmacy	/*
68174704Skmacy	 * Look up the route used for the connection to
69174704Skmacy	 * determine if it uses an interface capable of
70174704Skmacy	 * offloading the connection.
71174704Skmacy	 */
72182591Skmacy	rt = rtalloc1(nam, 0 /*report*/, 0 /*ignflags*/);
73174704Skmacy	if (rt)
74174704Skmacy		RT_UNLOCK(rt);
75174704Skmacy	else
76174704Skmacy		return (EHOSTUNREACH);
77174704Skmacy
78174704Skmacy	ifp = rt->rt_ifp;
79174704Skmacy	if ((ifp->if_capenable & IFCAP_TOE) == 0) {
80174704Skmacy		error = EINVAL;
81174704Skmacy		goto fail;
82174704Skmacy	}
83174704Skmacy
84174704Skmacy	tdev = TOEDEV(ifp);
85174704Skmacy	if (tdev == NULL) {
86174704Skmacy		error = EPERM;
87174704Skmacy		goto fail;
88174704Skmacy	}
89174704Skmacy
90174704Skmacy	if (tdev->tod_can_offload(tdev, so) == 0) {
91174704Skmacy		error = EPERM;
92174704Skmacy		goto fail;
93174704Skmacy	}
94174704Skmacy
95174704Skmacy	return (tdev->tod_connect(tdev, so, rt, nam));
96174704Skmacyfail:
97174704Skmacy	RTFREE(rt);
98174704Skmacy	return (error);
99174704Skmacy}
100180648Skmacy
101180648Skmacy
102180648Skmacy/*
103180648Skmacy * This file contains code as a short-term staging area before it is moved in
104180648Skmacy * to sys/netinet/tcp_offload.c
105180648Skmacy */
106180648Skmacy
107180648Skmacyvoid
108180648Skmacytcp_offload_twstart(struct tcpcb *tp)
109180648Skmacy{
110180648Skmacy
111181803Sbz	INP_INFO_WLOCK(&V_tcbinfo);
112180674Skmacy	INP_WLOCK(tp->t_inpcb);
113180648Skmacy	tcp_twstart(tp);
114181803Sbz	INP_INFO_WUNLOCK(&V_tcbinfo);
115180648Skmacy}
116180648Skmacy
117180648Skmacystruct tcpcb *
118180648Skmacytcp_offload_close(struct tcpcb *tp)
119180648Skmacy{
120180648Skmacy
121181803Sbz	INP_INFO_WLOCK(&V_tcbinfo);
122180648Skmacy	INP_WLOCK(tp->t_inpcb);
123180648Skmacy	tp = tcp_close(tp);
124181803Sbz	INP_INFO_WUNLOCK(&V_tcbinfo);
125180648Skmacy	if (tp)
126180648Skmacy		INP_WUNLOCK(tp->t_inpcb);
127180648Skmacy
128180648Skmacy	return (tp);
129180648Skmacy}
130180648Skmacy
131180648Skmacystruct tcpcb *
132180648Skmacytcp_offload_drop(struct tcpcb *tp, int error)
133180648Skmacy{
134180648Skmacy
135181803Sbz	INP_INFO_WLOCK(&V_tcbinfo);
136180648Skmacy	INP_WLOCK(tp->t_inpcb);
137180648Skmacy	tp = tcp_drop(tp, error);
138181803Sbz	INP_INFO_WUNLOCK(&V_tcbinfo);
139180648Skmacy	if (tp)
140180648Skmacy		INP_WUNLOCK(tp->t_inpcb);
141180648Skmacy
142180648Skmacy	return (tp);
143180648Skmacy}
144180648Skmacy
145