Mon Apr 25 22:11:32 2011 UTC ()
ip_undefer_csum:
- don't forget ntohs.
- don't add hdrlen twice for l4 header offset.
- use M_CSUM_DATA_IPv4_IPHL instead of extracting it from ip header.
- simplify code.
- KNF.


(yamt)
diff -r1.4 -r1.5 src/sys/netinet/in_offload.c

cvs diff -r1.4 -r1.5 src/sys/netinet/in_offload.c (switch to unified diff)

--- src/sys/netinet/in_offload.c 2011/04/14 15:53:36 1.4
+++ src/sys/netinet/in_offload.c 2011/04/25 22:11:31 1.5
@@ -1,255 +1,257 @@ @@ -1,255 +1,257 @@
1/* $NetBSD: in_offload.c,v 1.4 2011/04/14 15:53:36 yamt Exp $ */ 1/* $NetBSD: in_offload.c,v 1.5 2011/04/25 22:11:31 yamt Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c)2005, 2006 YAMAMOTO Takashi, 4 * Copyright (c)2005, 2006 YAMAMOTO Takashi,
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 14 * documentation and/or other materials provided with the distribution.
15 * 15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 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 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 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 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 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 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE. 26 * SUCH DAMAGE.
27 */ 27 */
28 28
29#include <sys/cdefs.h> 29#include <sys/cdefs.h>
30__KERNEL_RCSID(0, "$NetBSD: in_offload.c,v 1.4 2011/04/14 15:53:36 yamt Exp $"); 30__KERNEL_RCSID(0, "$NetBSD: in_offload.c,v 1.5 2011/04/25 22:11:31 yamt Exp $");
31 31
32#include <sys/param.h> 32#include <sys/param.h>
33#include <sys/mbuf.h> 33#include <sys/mbuf.h>
34 34
35#include <net/if.h> 35#include <net/if.h>
36 36
37#include <netinet/in.h> 37#include <netinet/in.h>
38#include <netinet/in_systm.h> 38#include <netinet/in_systm.h>
39#include <netinet/ip.h> 39#include <netinet/ip.h>
40#include <netinet/tcp.h> 40#include <netinet/tcp.h>
41#include <netinet/in_offload.h> 41#include <netinet/in_offload.h>
42 42
43struct ip_tso_output_args { 43struct ip_tso_output_args {
44 struct ifnet *ifp; 44 struct ifnet *ifp;
45 const struct sockaddr *sa; 45 const struct sockaddr *sa;
46 struct rtentry *rt; 46 struct rtentry *rt;
47}; 47};
48 48
49static int ip_tso_output_callback(void *, struct mbuf *); 49static int ip_tso_output_callback(void *, struct mbuf *);
50 50
51static int 51static int
52ip_tso_output_callback(void *vp, struct mbuf *m) 52ip_tso_output_callback(void *vp, struct mbuf *m)
53{ 53{
54 struct ip_tso_output_args *args = vp; 54 struct ip_tso_output_args *args = vp;
55 struct ifnet *ifp = args->ifp; 55 struct ifnet *ifp = args->ifp;
56 int error; 56 int error;
57 57
58 KERNEL_LOCK(1, NULL); 58 KERNEL_LOCK(1, NULL);
59 error = (*ifp->if_output)(ifp, m, args->sa, args->rt); 59 error = (*ifp->if_output)(ifp, m, args->sa, args->rt);
60 KERNEL_UNLOCK_ONE(NULL); 60 KERNEL_UNLOCK_ONE(NULL);
61 return error; 61 return error;
62} 62}
63 63
64int 64int
65ip_tso_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *sa, 65ip_tso_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *sa,
66 struct rtentry *rt) 66 struct rtentry *rt)
67{ 67{
68 struct ip_tso_output_args args; 68 struct ip_tso_output_args args;
69 69
70 args.ifp = ifp; 70 args.ifp = ifp;
71 args.sa = sa; 71 args.sa = sa;
72 args.rt = rt; 72 args.rt = rt;
73 73
74 return tcp4_segment(m, ip_tso_output_callback, &args); 74 return tcp4_segment(m, ip_tso_output_callback, &args);
75} 75}
76 76
77/* 77/*
78 * tcp4_segment: handle M_CSUM_TSOv4 by software. 78 * tcp4_segment: handle M_CSUM_TSOv4 by software.
79 * 79 *
80 * => always consume m. 80 * => always consume m.
81 * => call output_func with output_arg for each segments. 81 * => call output_func with output_arg for each segments.
82 */ 82 */
83 83
84int 84int
85tcp4_segment(struct mbuf *m, int (*output_func)(void *, struct mbuf *), 85tcp4_segment(struct mbuf *m, int (*output_func)(void *, struct mbuf *),
86 void *output_arg) 86 void *output_arg)
87{ 87{
88 int mss; 88 int mss;
89 int iphlen; 89 int iphlen;
90 int thlen; 90 int thlen;
91 int hlen; 91 int hlen;
92 int len; 92 int len;
93 struct ip *iph; 93 struct ip *iph;
94 struct tcphdr *th; 94 struct tcphdr *th;
95 uint16_t ipid; 95 uint16_t ipid;
96 uint32_t tcpseq; 96 uint32_t tcpseq;
97 struct mbuf *hdr = NULL; 97 struct mbuf *hdr = NULL;
98 struct mbuf *t; 98 struct mbuf *t;
99 int error = 0; 99 int error = 0;
100 100
101 KASSERT((m->m_flags & M_PKTHDR) != 0); 101 KASSERT((m->m_flags & M_PKTHDR) != 0);
102 KASSERT((m->m_pkthdr.csum_flags & M_CSUM_TSOv4) != 0); 102 KASSERT((m->m_pkthdr.csum_flags & M_CSUM_TSOv4) != 0);
103 103
104 m->m_pkthdr.csum_flags = 0; 104 m->m_pkthdr.csum_flags = 0;
105 105
106 len = m->m_pkthdr.len; 106 len = m->m_pkthdr.len;
107 KASSERT(len >= sizeof(*iph) + sizeof(*th)); 107 KASSERT(len >= sizeof(*iph) + sizeof(*th));
108 108
109 if (m->m_len < sizeof(*iph)) { 109 if (m->m_len < sizeof(*iph)) {
110 m = m_pullup(m, sizeof(*iph)); 110 m = m_pullup(m, sizeof(*iph));
111 if (m == NULL) { 111 if (m == NULL) {
112 error = ENOMEM; 112 error = ENOMEM;
113 goto quit; 113 goto quit;
114 } 114 }
115 } 115 }
116 iph = mtod(m, struct ip *); 116 iph = mtod(m, struct ip *);
117 iphlen = iph->ip_hl * 4; 117 iphlen = iph->ip_hl * 4;
118 KASSERT(iph->ip_v == IPVERSION); 118 KASSERT(iph->ip_v == IPVERSION);
119 KASSERT(iphlen >= sizeof(*iph)); 119 KASSERT(iphlen >= sizeof(*iph));
120 KASSERT(iph->ip_p == IPPROTO_TCP); 120 KASSERT(iph->ip_p == IPPROTO_TCP);
121 ipid = ntohs(iph->ip_id); 121 ipid = ntohs(iph->ip_id);
122 122
123 hlen = iphlen + sizeof(*th); 123 hlen = iphlen + sizeof(*th);
124 if (m->m_len < hlen) { 124 if (m->m_len < hlen) {
125 m = m_pullup(m, hlen); 125 m = m_pullup(m, hlen);
126 if (m == NULL) { 126 if (m == NULL) {
127 error = ENOMEM; 127 error = ENOMEM;
128 goto quit; 128 goto quit;
129 } 129 }
130 } 130 }
131 th = (void *)(mtod(m, char *) + iphlen); 131 th = (void *)(mtod(m, char *) + iphlen);
132 tcpseq = ntohl(th->th_seq); 132 tcpseq = ntohl(th->th_seq);
133 thlen = th->th_off * 4; 133 thlen = th->th_off * 4;
134 hlen = iphlen + thlen; 134 hlen = iphlen + thlen;
135 135
136 mss = m->m_pkthdr.segsz; 136 mss = m->m_pkthdr.segsz;
137 KASSERT(mss != 0); 137 KASSERT(mss != 0);
138 KASSERT(len > hlen); 138 KASSERT(len > hlen);
139 139
140 t = m_split(m, hlen, M_NOWAIT); 140 t = m_split(m, hlen, M_NOWAIT);
141 if (t == NULL) { 141 if (t == NULL) {
142 error = ENOMEM; 142 error = ENOMEM;
143 goto quit; 143 goto quit;
144 } 144 }
145 hdr = m; 145 hdr = m;
146 m = t; 146 m = t;
147 len -= hlen; 147 len -= hlen;
148 KASSERT(len % mss == 0); 148 KASSERT(len % mss == 0);
149 while (len > 0) { 149 while (len > 0) {
150 struct mbuf *n; 150 struct mbuf *n;
151 151
152 n = m_dup(hdr, 0, hlen, M_NOWAIT); 152 n = m_dup(hdr, 0, hlen, M_NOWAIT);
153 if (n == NULL) { 153 if (n == NULL) {
154 error = ENOMEM; 154 error = ENOMEM;
155 goto quit; 155 goto quit;
156 } 156 }
157 KASSERT(n->m_len == hlen); /* XXX */ 157 KASSERT(n->m_len == hlen); /* XXX */
158 158
159 t = m_split(m, mss, M_NOWAIT); 159 t = m_split(m, mss, M_NOWAIT);
160 if (t == NULL) { 160 if (t == NULL) {
161 m_freem(n); 161 m_freem(n);
162 error = ENOMEM; 162 error = ENOMEM;
163 goto quit; 163 goto quit;
164 } 164 }
165 m_cat(n, m); 165 m_cat(n, m);
166 m = t; 166 m = t;
167 167
168 KASSERT(n->m_len >= hlen); /* XXX */ 168 KASSERT(n->m_len >= hlen); /* XXX */
169 169
170 n->m_pkthdr.len = hlen + mss; 170 n->m_pkthdr.len = hlen + mss;
171 iph = mtod(n, struct ip *); 171 iph = mtod(n, struct ip *);
172 KASSERT(iph->ip_v == IPVERSION); 172 KASSERT(iph->ip_v == IPVERSION);
173 iph->ip_len = htons(n->m_pkthdr.len); 173 iph->ip_len = htons(n->m_pkthdr.len);
174 iph->ip_id = htons(ipid); 174 iph->ip_id = htons(ipid);
175 th = (void *)(mtod(n, char *) + iphlen); 175 th = (void *)(mtod(n, char *) + iphlen);
176 th->th_seq = htonl(tcpseq); 176 th->th_seq = htonl(tcpseq);
177 iph->ip_sum = 0; 177 iph->ip_sum = 0;
178 iph->ip_sum = in_cksum(n, iphlen); 178 iph->ip_sum = in_cksum(n, iphlen);
179 th->th_sum = 0; 179 th->th_sum = 0;
180 th->th_sum = in4_cksum(n, IPPROTO_TCP, iphlen, thlen + mss); 180 th->th_sum = in4_cksum(n, IPPROTO_TCP, iphlen, thlen + mss);
181 181
182 error = (*output_func)(output_arg, n); 182 error = (*output_func)(output_arg, n);
183 if (error) { 183 if (error) {
184 goto quit; 184 goto quit;
185 } 185 }
186 186
187 tcpseq += mss; 187 tcpseq += mss;
188 ipid++; 188 ipid++;
189 len -= mss; 189 len -= mss;
190 } 190 }
191 191
192quit: 192quit:
193 if (hdr != NULL) { 193 if (hdr != NULL) {
194 m_freem(hdr); 194 m_freem(hdr);
195 } 195 }
196 if (m != NULL) { 196 if (m != NULL) {
197 m_freem(m); 197 m_freem(m);
198 } 198 }
199 199
200 return error; 200 return error;
201} 201}
202 202
203void 203void
204ip_undefer_csum(struct mbuf *m, size_t hdrlen, int csum_flags) 204ip_undefer_csum(struct mbuf *m, size_t hdrlen, int csum_flags)
205{ 205{
206 KASSERT(m->m_flags & M_PKTHDR); 206 const size_t iphdrlen = M_CSUM_DATA_IPv4_IPHL(m->m_pkthdr.csum_data);
207 KASSERT((m->m_pkthdr.csum_flags & csum_flags) == csum_flags); 
208 uint16_t csum; 207 uint16_t csum;
209 uint16_t ip_len; 208 uint16_t ip_len;
210 uint16_t *csump; 209 uint16_t *csump;
211 size_t iphdrlen; 210
 211 KASSERT(m->m_flags & M_PKTHDR);
 212 KASSERT((m->m_pkthdr.csum_flags & csum_flags) == csum_flags);
212 213
213 if (__predict_true(hdrlen + sizeof(struct ip) <= m->m_len)) { 214 if (__predict_true(hdrlen + sizeof(struct ip) <= m->m_len)) {
214 struct ip *ip = (struct ip *)(mtod(m, uint8_t *) + hdrlen); 215 struct ip *ip = (struct ip *)(mtod(m, uint8_t *) + hdrlen);
 216
215 ip_len = ip->ip_len; 217 ip_len = ip->ip_len;
216 iphdrlen = ip->ip_hl << 2; 
217 csump = &ip->ip_sum; 218 csump = &ip->ip_sum;
218 } else { 219 } else {
219 uint8_t ip_vhl; 220 const size_t ip_len_offset =
220 const size_t ip_len_offset = hdrlen + offsetof(struct ip, ip_len); 221 hdrlen + offsetof(struct ip, ip_len);
221 m_copydata(m, hdrlen, sizeof(ip_vhl), &ip_vhl); 222
222 m_copydata(m, ip_len_offset, sizeof(ip_len), &ip_len); 223 m_copydata(m, ip_len_offset, sizeof(ip_len), &ip_len);
223 iphdrlen = (ip_vhl & 0x0f) << 2; 
224 csump = NULL; 224 csump = NULL;
225 } 225 }
 226 ip_len = ntohs(ip_len);
226 227
227 if (csum_flags & M_CSUM_IPv4) { 228 if (csum_flags & M_CSUM_IPv4) {
228 const size_t offset = hdrlen + offsetof(struct ip, ip_sum); 
229 csum = in4_cksum(m, 0, hdrlen, iphdrlen); 229 csum = in4_cksum(m, 0, hdrlen, iphdrlen);
230 if (csump != NULL) { 230 if (csump != NULL) {
231 *csump = csum; 231 *csump = csum;
232 } else { 232 } else {
 233 const size_t offset = hdrlen +
 234 offsetof(struct ip, ip_sum);
 235
233 m_copyback(m, offset, sizeof(uint16_t), &csum); 236 m_copyback(m, offset, sizeof(uint16_t), &csum);
234 } 237 }
235 } 238 }
236 239
237 if (csum_flags & (M_CSUM_UDPv4|M_CSUM_TCPv4)) { 240 if (csum_flags & (M_CSUM_UDPv4|M_CSUM_TCPv4)) {
238 size_t l4offset = hdrlen 241 size_t l4offset = hdrlen + iphdrlen;
239 + M_CSUM_DATA_IPv4_IPHL(m->m_pkthdr.csum_data); 
240 242
241 csum = in4_cksum(m, 0, hdrlen + l4offset, ip_len - l4offset); 243 csum = in4_cksum(m, 0, l4offset, ip_len - l4offset - hdrlen);
242 if (csum == 0 && (csum_flags & M_CSUM_UDPv4) != 0) 244 if (csum == 0 && (csum_flags & M_CSUM_UDPv4) != 0)
243 csum = 0xffff; 245 csum = 0xffff;
244 246
245 l4offset += M_CSUM_DATA_IPv4_OFFSET(m->m_pkthdr.csum_data); 247 l4offset += M_CSUM_DATA_IPv4_OFFSET(m->m_pkthdr.csum_data);
246 248
247 if (__predict_true(l4offset + sizeof(uint16_t) <= m->m_len)) { 249 if (__predict_true(l4offset + sizeof(uint16_t) <= m->m_len)) {
248 *(uint16_t *)(mtod(m, char *) + l4offset) = csum; 250 *(uint16_t *)(mtod(m, char *) + l4offset) = csum;
249 } else { 251 } else {
250 m_copyback(m, l4offset, sizeof(csum), (void *) &csum); 252 m_copyback(m, l4offset, sizeof(csum), (void *) &csum);
251 } 253 }
252 } 254 }
253 255
254 m->m_pkthdr.csum_flags ^= csum_flags; 256 m->m_pkthdr.csum_flags ^= csum_flags;
255} 257}