2019-07-10 07:07:17 +00:00
|
|
|
#ifndef NICO_DELTA_CHECKSUM
|
|
|
|
#define NICO_DELTA_CHECKSUM
|
|
|
|
|
2019-07-10 11:38:59 +00:00
|
|
|
action v4sum() {
|
2019-07-15 12:18:35 +00:00
|
|
|
bit<32> tmp = 0;
|
|
|
|
bit<32> carryover = 0;
|
2019-07-10 11:38:59 +00:00
|
|
|
|
2019-07-15 12:18:35 +00:00
|
|
|
tmp = tmp + (bit<32>) hdr.ipv4.src_addr[15:0]; // 16 bit
|
|
|
|
tmp = tmp + (bit<32>) hdr.ipv4.src_addr[31:16]; // 16 bit
|
|
|
|
tmp = tmp + (bit<32>) hdr.ipv4.dst_addr[15:0]; // 16 bit
|
|
|
|
tmp = tmp + (bit<32>) hdr.ipv4.dst_addr[31:16]; // 16 bit
|
2019-07-10 11:38:59 +00:00
|
|
|
|
2019-07-15 12:18:35 +00:00
|
|
|
tmp = tmp + (bit<32>) hdr.ipv4.totalLen -20; // 16 bit
|
|
|
|
tmp = tmp + (bit<32>) hdr.ipv4.protocol; // 8 bit
|
|
|
|
|
|
|
|
carryover = tmp >> 16; /* maximum is 6*(2**16) >> 16 == 6 */
|
|
|
|
tmp = (tmp & 0xffff) + carryover; /* Now tmp contains at maximum 65541 */
|
|
|
|
carryover = tmp >> 16; /* Now carryover contains at maximum 1 */
|
|
|
|
tmp = (tmp & 0xffff) + carryover; /* No overrun possible anymore */
|
|
|
|
|
|
|
|
meta.v4sum = (bit<16>) tmp;
|
2019-07-10 11:38:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
action v6sum() {
|
2019-07-15 12:18:35 +00:00
|
|
|
bit<32> tmp = 0;
|
|
|
|
bit<32> carryover = 0;
|
|
|
|
|
|
|
|
tmp = tmp + (bit<32>) hdr.ipv6.src_addr[15:0]; // 16 bit
|
|
|
|
tmp = tmp + (bit<32>) hdr.ipv6.src_addr[31:16]; // 16 bit
|
|
|
|
tmp = tmp + (bit<32>) hdr.ipv6.src_addr[47:32]; // 16 bit
|
|
|
|
tmp = tmp + (bit<32>) hdr.ipv6.src_addr[63:48]; // 16 bit
|
|
|
|
tmp = tmp + (bit<32>) hdr.ipv6.src_addr[79:64]; // 16 bit
|
|
|
|
tmp = tmp + (bit<32>) hdr.ipv6.src_addr[95:80]; // 16 bit
|
|
|
|
tmp = tmp + (bit<32>) hdr.ipv6.src_addr[111:96]; // 16 bit
|
|
|
|
tmp = tmp + (bit<32>) hdr.ipv6.src_addr[127:112]; // 16 bit
|
|
|
|
|
|
|
|
tmp = tmp + (bit<32>) hdr.ipv6.dst_addr[15:0]; // 16 bit
|
|
|
|
tmp = tmp + (bit<32>) hdr.ipv6.dst_addr[31:16]; // 16 bit
|
|
|
|
tmp = tmp + (bit<32>) hdr.ipv6.dst_addr[47:32]; // 16 bit
|
|
|
|
tmp = tmp + (bit<32>) hdr.ipv6.dst_addr[63:48]; // 16 bit
|
|
|
|
tmp = tmp + (bit<32>) hdr.ipv6.dst_addr[79:64]; // 16 bit
|
|
|
|
tmp = tmp + (bit<32>) hdr.ipv6.dst_addr[95:80]; // 16 bit
|
|
|
|
tmp = tmp + (bit<32>) hdr.ipv6.dst_addr[111:96]; // 16 bit
|
|
|
|
tmp = tmp + (bit<32>) hdr.ipv6.dst_addr[127:112]; // 16 bit
|
|
|
|
|
|
|
|
tmp = tmp + (bit<32>) hdr.ipv6.payload_length; // 16 bit
|
|
|
|
tmp = tmp + (bit<32>) hdr.ipv6.next_header; // 8 bit
|
|
|
|
|
|
|
|
carryover = tmp >> 16; /* maximum is 18*(2**16) >> 16 == 18 */
|
|
|
|
tmp = (tmp & 0xffff) + carryover; /* Now tmp contains at maximum 65554*/
|
|
|
|
carryover = tmp >> 16; /* Now carryover contains at maximum 1 */
|
|
|
|
tmp = (tmp & 0xffff) + carryover; /* No overrun possible anymore */
|
|
|
|
|
|
|
|
meta.v6sum = (bit<16>) tmp;
|
2019-07-10 11:38:59 +00:00
|
|
|
}
|
2019-07-10 07:07:17 +00:00
|
|
|
|
2019-07-13 16:05:50 +00:00
|
|
|
action delta_prepare()
|
2019-07-11 06:47:34 +00:00
|
|
|
{
|
|
|
|
v4sum();
|
|
|
|
v6sum();
|
2019-07-13 16:05:50 +00:00
|
|
|
if(meta.v6sum > meta.v4sum) {
|
|
|
|
meta.headerdiff = meta.v6sum - meta.v4sum;
|
|
|
|
} else {
|
|
|
|
meta.headerdiff = meta.v4sum - meta.v6sum;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
action delta_udp_from_v4_to_v6()
|
|
|
|
{
|
|
|
|
delta_prepare();
|
2019-07-15 12:25:50 +00:00
|
|
|
hdr.udp.checksum = hdr.udp.checksum + ~meta.headerdiff +1;
|
2019-07-11 06:47:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
action delta_tcp_from_v4_to_v6()
|
|
|
|
{
|
2019-07-13 16:05:50 +00:00
|
|
|
delta_prepare();
|
2019-07-15 12:25:50 +00:00
|
|
|
hdr.tcp.checksum = hdr.tcp.checksum + ~meta.headerdiff +1;
|
2019-07-11 06:47:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
action delta_ipv4_from_v6_to_v4()
|
|
|
|
{
|
2019-07-13 16:05:50 +00:00
|
|
|
delta_prepare();
|
|
|
|
hdr.tcp.checksum = hdr.tcp.checksum + ~meta.headerdiff;
|
2019-07-11 06:47:34 +00:00
|
|
|
}
|
|
|
|
|
2019-07-10 07:07:17 +00:00
|
|
|
#endif
|