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.
This commit is contained in:
yamt 2011-04-25 22:11:31 +00:00
parent 1319ff4810
commit 45430a8699
1 changed files with 16 additions and 14 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: in_offload.c,v 1.4 2011/04/14 15:53:36 yamt Exp $ */
/* $NetBSD: in_offload.c,v 1.5 2011/04/25 22:11:31 yamt Exp $ */
/*-
* Copyright (c)2005, 2006 YAMAMOTO Takashi,
@ -27,7 +27,7 @@
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: in_offload.c,v 1.4 2011/04/14 15:53:36 yamt Exp $");
__KERNEL_RCSID(0, "$NetBSD: in_offload.c,v 1.5 2011/04/25 22:11:31 yamt Exp $");
#include <sys/param.h>
#include <sys/mbuf.h>
@ -203,42 +203,44 @@ quit:
void
ip_undefer_csum(struct mbuf *m, size_t hdrlen, int csum_flags)
{
KASSERT(m->m_flags & M_PKTHDR);
KASSERT((m->m_pkthdr.csum_flags & csum_flags) == csum_flags);
const size_t iphdrlen = M_CSUM_DATA_IPv4_IPHL(m->m_pkthdr.csum_data);
uint16_t csum;
uint16_t ip_len;
uint16_t *csump;
size_t iphdrlen;
KASSERT(m->m_flags & M_PKTHDR);
KASSERT((m->m_pkthdr.csum_flags & csum_flags) == csum_flags);
if (__predict_true(hdrlen + sizeof(struct ip) <= m->m_len)) {
struct ip *ip = (struct ip *)(mtod(m, uint8_t *) + hdrlen);
ip_len = ip->ip_len;
iphdrlen = ip->ip_hl << 2;
csump = &ip->ip_sum;
} else {
uint8_t ip_vhl;
const size_t ip_len_offset = hdrlen + offsetof(struct ip, ip_len);
m_copydata(m, hdrlen, sizeof(ip_vhl), &ip_vhl);
const size_t ip_len_offset =
hdrlen + offsetof(struct ip, ip_len);
m_copydata(m, ip_len_offset, sizeof(ip_len), &ip_len);
iphdrlen = (ip_vhl & 0x0f) << 2;
csump = NULL;
}
ip_len = ntohs(ip_len);
if (csum_flags & M_CSUM_IPv4) {
const size_t offset = hdrlen + offsetof(struct ip, ip_sum);
csum = in4_cksum(m, 0, hdrlen, iphdrlen);
if (csump != NULL) {
*csump = csum;
} else {
const size_t offset = hdrlen +
offsetof(struct ip, ip_sum);
m_copyback(m, offset, sizeof(uint16_t), &csum);
}
}
if (csum_flags & (M_CSUM_UDPv4|M_CSUM_TCPv4)) {
size_t l4offset = hdrlen
+ M_CSUM_DATA_IPv4_IPHL(m->m_pkthdr.csum_data);
size_t l4offset = hdrlen + iphdrlen;
csum = in4_cksum(m, 0, hdrlen + l4offset, ip_len - l4offset);
csum = in4_cksum(m, 0, l4offset, ip_len - l4offset - hdrlen);
if (csum == 0 && (csum_flags & M_CSUM_UDPv4) != 0)
csum = 0xffff;