update checksum code

This commit is contained in:
Nico Schottelius 2019-07-17 15:46:54 +02:00
commit 512fca1349
2 changed files with 47 additions and 15 deletions

View file

@ -1,6 +1,18 @@
#ifndef NICO_DELTA_CHECKSUM
#define NICO_DELTA_CHECKSUM
/* copied from
https://p4.org/p4-spec/docs/PSA-v1.1.0.html#appendix-internetchecksum-implementation
*/
bit<16> ones_complement_sum(in bit<16> x, in bit<16> y) {
bit<17> ret = (bit<17>) x + (bit<17>) y;
if (ret[16:16] == 1) {
ret = ret + 1;
}
return ret[15:0];
}
action v4sum() {
bit<16> tmp = 0;
@ -12,7 +24,7 @@ action v4sum() {
tmp = tmp + (bit<16>) hdr.ipv4.totalLen -20; // 16 bit
tmp = tmp + (bit<16>) hdr.ipv4.protocol; // 8 bit
meta.v4sum = tmp;
meta.v4sum = ~tmp;
}
action v6sum() {
@ -39,7 +51,7 @@ action v6sum() {
tmp = tmp + (bit<16>) hdr.ipv6.payload_length; // 16 bit
tmp = tmp + (bit<16>) hdr.ipv6.next_header; // 8 bit
meta.v6sum = tmp;
meta.v6sum = ~tmp;
}
action delta_prepare()
@ -52,28 +64,41 @@ action delta_udp_from_v4_to_v6()
{
delta_prepare();
bit<16> tmp = ~hdr.udp.checksum;
tmp = tmp + meta.v6sum;
tmp = tmp - meta.v4sum;
hdr.udp.checksum = ~tmp;
bit<16> tmp = 0;
tmp = ones_complement_sum(hdr.udp.checksum, meta.v6sum);
hdr.udp.checksum = ones_complement_sum(tmp, 0xffff - meta.v6sum);
}
action delta_tcp_from_v4_to_v6()
{
delta_prepare();
bit<16> tmp = ~hdr.tcp.checksum;
tmp = tmp + meta.v6sum;
tmp = tmp - meta.v4sum;
hdr.tcp.checksum = ~tmp;
bit<16> tmp = 0;
tmp = ones_complement_sum(hdr.tcp.checksum, meta.v6sum);
hdr.tcp.checksum = ones_complement_sum(tmp, 0xffff - meta.v6sum);
}
action delta_ipv4_from_v6_to_v4()
{
delta_prepare();
bit<16> tmp = 0;
tmp = tmp + (bit<16>) hdr.ipv4.version; /* 4 bit */
tmp = tmp + (bit<16>) hdr.ipv4.ihl; /* 4 bit */
tmp = tmp + (bit<16>) hdr.ipv4.diff_serv; /* 6 bit */
tmp = tmp + (bit<16>) hdr.ipv4.ecn; /* 2 bit */
tmp = tmp + (bit<16>) hdr.ipv4.totalLen; /* 16 bit */
tmp = tmp + (bit<16>) hdr.ipv4.identification; /* 16 bit */
tmp = tmp + (bit<16>) hdr.ipv4.flags; /* 3 bit */
/* we don't have ANY checksum, but tcp or udp: we can
base on that ones for calculating the diff for IPv4
Does NOT contain payload! -> can be done manually
tmp = tmp + (bit<16>) hdr.ipv4.fragOffset;
tmp = tmp + (bit<16>) hdr.ipv4.ttl,
tmp = tmp + (bit<16>) hdr.ipv4.protocol,
tmp = tmp + (bit<16>) hdr.ipv4.src_addr,
tmp = tmp + (bit<16>) hdr.ipv4.dst_addr
*/
hdr.ipv4.checksum = 0; /* TO BE DONE! */
}