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