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