mirror of
https://passt.top/passt
synced 2025-07-29 04:28:00 +02:00
checksum: Introduce AVX2 implementation, unify helpers
Provide an AVX2-based function using compiler intrinsics for TCP/IP-style checksums. The load/unpack/add idea and implementation is largely based on code from BESS (the Berkeley Extensible Software Switch) licensed as 3-Clause BSD, with a number of modifications to further decrease pipeline stalls and to minimise cache pollution. This speeds up considerably data paths from sockets to tap interfaces, decreasing overhead for checksum computation, with 16-64KiB packet buffers, from approximately 11% to 7%. The rest is just syscalls at this point. While at it, provide convenience targets in the Makefile for avx2, avx2_debug, and debug targets -- these simply add target-specific CFLAGS to the build. Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
This commit is contained in:
parent
0be49ccd93
commit
17765f8de0
8 changed files with 332 additions and 92 deletions
80
util.c
80
util.c
|
@ -76,86 +76,6 @@ logfn(info, LOG_INFO)
|
|||
logfn(debug, LOG_DEBUG)
|
||||
#endif
|
||||
|
||||
/**
|
||||
* sum_16b() - Calculate sum of 16-bit words
|
||||
* @buf: Input buffer
|
||||
* @len: Buffer length
|
||||
*
|
||||
* Return: 32-bit sum of 16-bit words
|
||||
*/
|
||||
uint32_t sum_16b(void *buf, size_t len)
|
||||
{
|
||||
uint32_t sum = 0;
|
||||
uint16_t *p = buf;
|
||||
size_t len1 = len / 2;
|
||||
size_t off;
|
||||
|
||||
for (off = 0; off < len1; off++, p++)
|
||||
sum += *p;
|
||||
|
||||
if (len % 2)
|
||||
sum += *p & 0xff;
|
||||
|
||||
return sum;
|
||||
}
|
||||
|
||||
/**
|
||||
* csum_fold() - Fold long sum for IP and TCP checksum
|
||||
* @sum: Original long sum
|
||||
*
|
||||
* Return: 16-bit folded sum
|
||||
*/
|
||||
uint16_t csum_fold(uint32_t sum)
|
||||
{
|
||||
while (sum >> 16)
|
||||
sum = (sum & 0xffff) + (sum >> 16);
|
||||
|
||||
return sum;
|
||||
}
|
||||
|
||||
/**
|
||||
* csum_ip4() - Calculate IPv4 checksum
|
||||
* @buf: Packet buffer, L3 headers
|
||||
* @len: Total L3 packet length
|
||||
*
|
||||
* Return: 16-bit IPv4-style checksum
|
||||
*/
|
||||
uint16_t csum_ip4(void *buf, size_t len)
|
||||
{
|
||||
return ~csum_fold(sum_16b(buf, len));
|
||||
}
|
||||
|
||||
/**
|
||||
* csum_tcp4() - Calculate TCP checksum for IPv4 and set in place
|
||||
* @iph: Packet buffer, IP header
|
||||
*/
|
||||
void csum_tcp4(struct iphdr *iph)
|
||||
{
|
||||
struct tcphdr *th = (struct tcphdr *)((char *)iph + iph->ihl * 4);
|
||||
uint16_t tlen = ntohs(iph->tot_len) - iph->ihl * 4, *p = (uint16_t *)th;
|
||||
uint32_t sum = 0;
|
||||
|
||||
sum += (iph->saddr >> 16) & 0xffff;
|
||||
sum += iph->saddr & 0xffff;
|
||||
sum += (iph->daddr >> 16) & 0xffff;
|
||||
sum += iph->daddr & 0xffff;
|
||||
|
||||
sum += htons(IPPROTO_TCP);
|
||||
sum += htons(tlen);
|
||||
|
||||
th->check = 0;
|
||||
while (tlen > 1) {
|
||||
sum += *p++;
|
||||
tlen -= 2;
|
||||
}
|
||||
|
||||
if (tlen > 0) {
|
||||
sum += *p & htons(0xff00);
|
||||
}
|
||||
|
||||
th->check = (uint16_t)~csum_fold(sum);
|
||||
}
|
||||
|
||||
/**
|
||||
* ipv6_l4hdr() - Find pointer to L4 header in IPv6 packet and extract protocol
|
||||
* @ip6h: IPv6 header
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue