]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/netinet/tcp_offload.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / sys / netinet / tcp_offload.c
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$");
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
41 #include <net/if.h>
42 #include <net/if_types.h>
43 #include <net/if_var.h>
44
45 #include <netinet/in.h>
46 #include <netinet/in_systm.h>
47 #include <netinet/in_pcb.h>
48 #include <netinet/tcp.h>
49 #include <netinet/tcp_var.h>
50 #include <netinet/tcp_offload.h>
51 #include <netinet/toedev.h>
52
53 uint32_t toedev_registration_count;
54
55 int
56 tcp_offload_connect(struct socket *so, struct sockaddr *nam)
57 {
58         struct ifnet *ifp;
59         struct toedev *tdev;
60         struct rtentry *rt;
61         int error;
62         
63         if (toedev_registration_count == 0)
64                 return (EINVAL);
65         
66         /*
67          * Look up the route used for the connection to 
68          * determine if it uses an interface capable of
69          * offloading the connection.
70          */
71         rt = rtalloc1(nam, 0 /*report*/, 0 /*ignflags*/);
72         if (rt) 
73                 RT_UNLOCK(rt);
74         else 
75                 return (EHOSTUNREACH);
76
77         ifp = rt->rt_ifp;
78         if ((ifp->if_capenable & IFCAP_TOE) == 0) {
79                 error = EINVAL;
80                 goto fail;
81         }
82         
83         tdev = TOEDEV(ifp);
84         if (tdev == NULL) {
85                 error = EPERM;
86                 goto fail;
87         }
88         
89         if (tdev->tod_can_offload(tdev, so) == 0) {
90                 error = EPERM;
91                 goto fail;
92         }
93         
94         return (tdev->tod_connect(tdev, so, rt, nam));
95 fail:
96         RTFREE(rt);
97         return (error);
98 }
99
100
101 /*
102  * This file contains code as a short-term staging area before it is moved in 
103  * to sys/netinet/tcp_offload.c
104  */
105
106 void
107 tcp_offload_twstart(struct tcpcb *tp)
108 {
109
110         INP_INFO_WLOCK(&tcbinfo);
111         INP_WLOCK(tp->t_inpcb);
112         tcp_twstart(tp);
113         INP_INFO_WUNLOCK(&tcbinfo);
114 }
115
116 struct tcpcb *
117 tcp_offload_close(struct tcpcb *tp)
118 {
119         
120         INP_INFO_WLOCK(&tcbinfo);
121         INP_WLOCK(tp->t_inpcb);
122         tp = tcp_close(tp);
123         INP_INFO_WUNLOCK(&tcbinfo);
124         if (tp)
125                 INP_WUNLOCK(tp->t_inpcb);
126
127         return (tp);
128 }
129
130 struct tcpcb *
131 tcp_offload_drop(struct tcpcb *tp, int error)
132 {
133         
134         INP_INFO_WLOCK(&tcbinfo);
135         INP_WLOCK(tp->t_inpcb);
136         tp = tcp_drop(tp, error);
137         INP_INFO_WUNLOCK(&tcbinfo);
138         if (tp)
139                 INP_WUNLOCK(tp->t_inpcb);
140
141         return (tp);
142 }
143