xref: /netbsd-src/sys/netinet/in_offload.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*	$NetBSD: in_offload.c,v 1.2 2007/04/24 23:43:50 dyoung Exp $	*/
2 
3 /*-
4  * Copyright (c)2005, 2006 YAMAMOTO Takashi,
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: in_offload.c,v 1.2 2007/04/24 23:43:50 dyoung Exp $");
31 
32 #include <sys/param.h>
33 #include <sys/mbuf.h>
34 
35 #include <net/if.h>
36 
37 #include <netinet/in.h>
38 #include <netinet/in_systm.h>
39 #include <netinet/ip.h>
40 #include <netinet/tcp.h>
41 #include <netinet/in_offload.h>
42 
43 struct ip_tso_output_args {
44 	struct ifnet *ifp;
45 	const struct sockaddr *sa;
46 	struct rtentry *rt;
47 };
48 
49 static int ip_tso_output_callback(void *, struct mbuf *);
50 
51 static int
52 ip_tso_output_callback(void *vp, struct mbuf *m)
53 {
54 	struct ip_tso_output_args *args = vp;
55 	struct ifnet *ifp = args->ifp;
56 
57 	return (*ifp->if_output)(ifp, m, args->sa, args->rt);
58 }
59 
60 int
61 ip_tso_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *sa,
62     struct rtentry *rt)
63 {
64 	struct ip_tso_output_args args;
65 
66 	args.ifp = ifp;
67 	args.sa = sa;
68 	args.rt = rt;
69 
70 	return tcp4_segment(m, ip_tso_output_callback, &args);
71 }
72 
73 /*
74  * tcp4_segment: handle M_CSUM_TSOv4 by software.
75  *
76  * => always consume m.
77  * => call output_func with output_arg for each segments.
78  */
79 
80 int
81 tcp4_segment(struct mbuf *m, int (*output_func)(void *, struct mbuf *),
82     void *output_arg)
83 {
84 	int mss;
85 	int iphlen;
86 	int thlen;
87 	int hlen;
88 	int len;
89 	struct ip *iph;
90 	struct tcphdr *th;
91 	uint16_t ipid;
92 	uint32_t tcpseq;
93 	struct mbuf *hdr = NULL;
94 	struct mbuf *t;
95 	int error = 0;
96 
97 	KASSERT((m->m_flags & M_PKTHDR) != 0);
98 	KASSERT((m->m_pkthdr.csum_flags & M_CSUM_TSOv4) != 0);
99 
100 	m->m_pkthdr.csum_flags = 0;
101 
102 	len = m->m_pkthdr.len;
103 	KASSERT(len >= sizeof(*iph) + sizeof(*th));
104 
105 	if (m->m_len < sizeof(*iph)) {
106 		m = m_pullup(m, sizeof(*iph));
107 		if (m == NULL) {
108 			error = ENOMEM;
109 			goto quit;
110 		}
111 	}
112 	iph = mtod(m, struct ip *);
113 	iphlen = iph->ip_hl * 4;
114 	KASSERT(iph->ip_v == IPVERSION);
115 	KASSERT(iphlen >= sizeof(*iph));
116 	KASSERT(iph->ip_p == IPPROTO_TCP);
117 	ipid = ntohs(iph->ip_id);
118 
119 	hlen = iphlen + sizeof(*th);
120 	if (m->m_len < hlen) {
121 		m = m_pullup(m, hlen);
122 		if (m == NULL) {
123 			error = ENOMEM;
124 			goto quit;
125 		}
126 	}
127 	th = (void *)(mtod(m, char *) + iphlen);
128 	tcpseq = ntohl(th->th_seq);
129 	thlen = th->th_off * 4;
130 	hlen = iphlen + thlen;
131 
132 	mss = m->m_pkthdr.segsz;
133 	KASSERT(mss != 0);
134 	KASSERT(len > hlen);
135 
136 	t = m_split(m, hlen, M_NOWAIT);
137 	if (t == NULL) {
138 		error = ENOMEM;
139 		goto quit;
140 	}
141 	hdr = m;
142 	m = t;
143 	len -= hlen;
144 	KASSERT(len % mss == 0);
145 	while (len > 0) {
146 		struct mbuf *n;
147 
148 		n = m_dup(hdr, 0, hlen, M_NOWAIT);
149 		if (n == NULL) {
150 			error = ENOMEM;
151 			goto quit;
152 		}
153 		KASSERT(n->m_len == hlen); /* XXX */
154 
155 		t = m_split(m, mss, M_NOWAIT);
156 		if (t == NULL) {
157 			m_freem(n);
158 			error = ENOMEM;
159 			goto quit;
160 		}
161 		m_cat(n, m);
162 		m = t;
163 
164 		KASSERT(n->m_len >= hlen); /* XXX */
165 
166 		n->m_pkthdr.len = hlen + mss;
167 		iph = mtod(n, struct ip *);
168 		KASSERT(iph->ip_v == IPVERSION);
169 		iph->ip_len = htons(n->m_pkthdr.len);
170 		iph->ip_id = htons(ipid);
171 		th = (void *)(mtod(n, char *) + iphlen);
172 		th->th_seq = htonl(tcpseq);
173 		iph->ip_sum = 0;
174 		iph->ip_sum = in_cksum(n, iphlen);
175 		th->th_sum = 0;
176 		th->th_sum = in4_cksum(n, IPPROTO_TCP, iphlen, thlen + mss);
177 
178 		error = (*output_func)(output_arg, n);
179 		if (error) {
180 			goto quit;
181 		}
182 
183 		tcpseq += mss;
184 		ipid++;
185 		len -= mss;
186 	}
187 
188 quit:
189 	if (hdr != NULL) {
190 		m_freem(hdr);
191 	}
192 	if (m != NULL) {
193 		m_freem(m);
194 	}
195 
196 	return error;
197 }
198