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