1/* $NetBSD: in_offload.c,v 1.7 2016/04/26 09:30:01 ozaki-r 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.7 2016/04/26 09:30:01 ozaki-r 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/ip_var.h>
41#include <netinet/tcp.h>
42#include <netinet/in_offload.h>
43
44struct ip_tso_output_args {
45 struct ifnet *ifp;
46 const struct sockaddr *sa;
47 struct rtentry *rt;
48};
49
50static int ip_tso_output_callback(void *, struct mbuf *);
51
52static int
53ip_tso_output_callback(void *vp, struct mbuf *m)
54{
55 struct ip_tso_output_args *args = vp;
56 struct ifnet *ifp = args->ifp;
57
58 return ip_if_output(ifp, m, args->sa, args->rt);
59}
60
61int
62ip_tso_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *sa,
63 struct rtentry *rt)
64{
65 struct ip_tso_output_args args;
66
67 args.ifp = ifp;
68 args.sa = sa;
69 args.rt = rt;
70
71 return tcp4_segment(m, ip_tso_output_callback, &args);
72}
73
74/*
75 * tcp4_segment: handle M_CSUM_TSOv4 by software.
76 *
77 * => always consume m.
78 * => call output_func with output_arg for each segments.
79 */
80
81int
82tcp4_segment(struct mbuf *m, int (*output_func)(void *, struct mbuf *),
83 void *output_arg)
84{
85 int mss;
86 int iphlen;
87 int thlen;
88 int hlen;
89 int len;
90 struct ip *iph;
91 struct tcphdr *th;
92 uint16_t ipid;
93 uint32_t tcpseq;
94 struct mbuf *hdr = NULL;
95 struct mbuf *t;
96 int error = 0;
97
98 KASSERT((m->m_flags & M_PKTHDR) != 0);
99 KASSERT((m->m_pkthdr.csum_flags & M_CSUM_TSOv4) != 0);
100
101 m->m_pkthdr.csum_flags = 0;
102
103 len = m->m_pkthdr.len;
104 KASSERT(len >= sizeof(*iph) + sizeof(*th));
105
106 if (m->m_len < sizeof(*iph)) {
107 m = m_pullup(m, sizeof(*iph));
108 if (m == NULL) {
109 error = ENOMEM;
110 goto quit;
111 }
112 }
113 iph = mtod(m, struct ip *);
114 iphlen = iph->ip_hl * 4;
115 KASSERT(iph->ip_v == IPVERSION);
116 KASSERT(iphlen >= sizeof(*iph));
117 KASSERT(iph->ip_p == IPPROTO_TCP);
118 ipid = ntohs(iph->ip_id);
119
120 hlen = iphlen + sizeof(*th);
121 if (m->m_len < hlen) {
122 m = m_pullup(m, hlen);
123 if (m == NULL) {
124 error = ENOMEM;
125 goto quit;
126 }
127 }
128 th = (void *)(mtod(m, char *) + iphlen);
129 tcpseq = ntohl(th->th_seq);
130 thlen = th->th_off * 4;
131 hlen = iphlen + thlen;
132
133 mss = m->m_pkthdr.segsz;
134 KASSERT(mss != 0);
135 KASSERT(len > hlen);
136
137 t = m_split(m, hlen, M_NOWAIT);
138 if (t == NULL) {
139 error = ENOMEM;
140 goto quit;
141 }
142 hdr = m;
143 m = t;
144 len -= hlen;
145 KASSERT(len % mss == 0);
146 while (len > 0) {
147 struct mbuf *n;
148
149 n = m_dup(hdr, 0, hlen, M_NOWAIT);
150 if (n == NULL) {
151 error = ENOMEM;
152 goto quit;
153 }
154 KASSERT(n->m_len == hlen); /* XXX */
155
156 t = m_split(m, mss, M_NOWAIT);
157 if (t == NULL) {
158 m_freem(n);
159 error = ENOMEM;
160 goto quit;
161 }
162 m_cat(n, m);
163 m = t;
164
165 KASSERT(n->m_len >= hlen); /* XXX */
166
167 n->m_pkthdr.len = hlen + mss;
168 iph = mtod(n, struct ip *);
169 KASSERT(iph->ip_v == IPVERSION);
170 iph->ip_len = htons(n->m_pkthdr.len);
171 iph->ip_id = htons(ipid);
172 th = (void *)(mtod(n, char *) + iphlen);
173 th->th_seq = htonl(tcpseq);
174 iph->ip_sum = 0;
175 iph->ip_sum = in_cksum(n, iphlen);
176 th->th_sum = 0;
177 th->th_sum = in4_cksum(n, IPPROTO_TCP, iphlen, thlen + mss);
178
179 error = (*output_func)(output_arg, n);
180 if (error) {
181 goto quit;
182 }
183
184 tcpseq += mss;
185 ipid++;
186 len -= mss;
187 }
188
189quit:
190 if (hdr != NULL) {
191 m_freem(hdr);
192 }
193 if (m != NULL) {
194 m_freem(m);
195 }
196
197 return error;
198}
199
200void
201ip_undefer_csum(struct mbuf *m, size_t hdrlen, int csum_flags)
202{
203 const size_t iphdrlen = M_CSUM_DATA_IPv4_IPHL(m->m_pkthdr.csum_data);
204 uint16_t csum;
205 uint16_t ip_len;
206 uint16_t *csump;
207
208 KASSERT(m->m_flags & M_PKTHDR);
209 KASSERT((m->m_pkthdr.csum_flags & csum_flags) == csum_flags);
210
211 if (__predict_true(hdrlen + sizeof(struct ip) <= m->m_len)) {
212 struct ip *ip = (struct ip *)(mtod(m, uint8_t *) + hdrlen);
213
214 ip_len = ip->ip_len;
215 csump = &ip->ip_sum;
216 } else {
217 const size_t ip_len_offset =
218 hdrlen + offsetof(struct ip, ip_len);
219
220 m_copydata(m, ip_len_offset, sizeof(ip_len), &ip_len);
221 csump = NULL;
222 }
223 ip_len = ntohs(ip_len);
224
225 if (csum_flags & M_CSUM_IPv4) {
226 csum = in4_cksum(m, 0, hdrlen, iphdrlen);
227 if (csump != NULL) {
228 *csump = csum;
229 } else {
230 const size_t offset = hdrlen +
231 offsetof(struct ip, ip_sum);
232
233 m_copyback(m, offset, sizeof(uint16_t), &csum);
234 }
235 }
236
237 if (csum_flags & (M_CSUM_UDPv4|M_CSUM_TCPv4)) {
238 size_t l4offset = hdrlen + iphdrlen;
239
240 csum = in4_cksum(m, 0, l4offset, ip_len - l4offset - hdrlen);
241 if (csum == 0 && (csum_flags & M_CSUM_UDPv4) != 0)
242 csum = 0xffff;
243
244 l4offset += M_CSUM_DATA_IPv4_OFFSET(m->m_pkthdr.csum_data);
245
246 if (__predict_true(l4offset + sizeof(uint16_t) <= m->m_len)) {
247 *(uint16_t *)(mtod(m, char *) + l4offset) = csum;
248 } else {
249 m_copyback(m, l4offset, sizeof(csum), (void *) &csum);
250 }
251 }
252
253 m->m_pkthdr.csum_flags ^= csum_flags;
254}
255