master-thesis/p4src/actions_delta_checksum.p4

118 lines
3.7 KiB
Text
Raw Normal View History

2019-07-10 07:07:17 +00:00
#ifndef NICO_DELTA_CHECKSUM
#define NICO_DELTA_CHECKSUM
2019-07-17 13:46:54 +00:00
2019-07-17 13:48:16 +00:00
#include "headers.p4"
2019-07-17 13:46:54 +00:00
action v4sum() {
2019-07-16 10:06:26 +00:00
bit<16> tmp = 0;
2019-07-16 10:06:26 +00:00
tmp = tmp + (bit<16>) hdr.ipv4.src_addr[15:0]; // 16 bit
tmp = tmp + (bit<16>) hdr.ipv4.src_addr[31:16]; // 16 bit
tmp = tmp + (bit<16>) hdr.ipv4.dst_addr[15:0]; // 16 bit
tmp = tmp + (bit<16>) hdr.ipv4.dst_addr[31:16]; // 16 bit
2019-07-16 10:06:26 +00:00
tmp = tmp + (bit<16>) hdr.ipv4.totalLen -20; // 16 bit
tmp = tmp + (bit<16>) hdr.ipv4.protocol; // 8 bit
2019-07-17 13:46:54 +00:00
meta.v4sum = ~tmp;
}
action v6sum() {
2019-07-16 10:06:26 +00:00
bit<16> tmp = 0;
tmp = tmp + (bit<16>) hdr.ipv6.src_addr[15:0]; // 16 bit
tmp = tmp + (bit<16>) hdr.ipv6.src_addr[31:16]; // 16 bit
tmp = tmp + (bit<16>) hdr.ipv6.src_addr[47:32]; // 16 bit
tmp = tmp + (bit<16>) hdr.ipv6.src_addr[63:48]; // 16 bit
tmp = tmp + (bit<16>) hdr.ipv6.src_addr[79:64]; // 16 bit
tmp = tmp + (bit<16>) hdr.ipv6.src_addr[95:80]; // 16 bit
tmp = tmp + (bit<16>) hdr.ipv6.src_addr[111:96]; // 16 bit
tmp = tmp + (bit<16>) hdr.ipv6.src_addr[127:112]; // 16 bit
tmp = tmp + (bit<16>) hdr.ipv6.dst_addr[15:0]; // 16 bit
tmp = tmp + (bit<16>) hdr.ipv6.dst_addr[31:16]; // 16 bit
tmp = tmp + (bit<16>) hdr.ipv6.dst_addr[47:32]; // 16 bit
tmp = tmp + (bit<16>) hdr.ipv6.dst_addr[63:48]; // 16 bit
tmp = tmp + (bit<16>) hdr.ipv6.dst_addr[79:64]; // 16 bit
tmp = tmp + (bit<16>) hdr.ipv6.dst_addr[95:80]; // 16 bit
tmp = tmp + (bit<16>) hdr.ipv6.dst_addr[111:96]; // 16 bit
tmp = tmp + (bit<16>) hdr.ipv6.dst_addr[127:112]; // 16 bit
tmp = tmp + (bit<16>) hdr.ipv6.payload_length; // 16 bit
tmp = tmp + (bit<16>) hdr.ipv6.next_header; // 8 bit
2019-07-16 09:53:00 +00:00
2019-07-17 13:46:54 +00:00
meta.v6sum = ~tmp;
}
2019-07-10 07:07:17 +00:00
action delta_prepare()
2019-07-11 06:47:34 +00:00
{
v4sum();
v6sum();
}
action delta_udp_from_v4_to_v6()
{
2019-07-16 10:06:26 +00:00
delta_prepare();
bit<17> tmp = (bit<17>) hdr.udp.checksum + (bit<17>) meta.v6sum;
if (tmp[16:16] == 1) {
tmp = tmp + 1;
tmp[16:16] = 0;
}
tmp = tmp + (bit<17>) (0xffff - meta.v6sum);
if (tmp[16:16] == 1) {
tmp = tmp + 1;
tmp[16:16] = 0;
}
2019-07-17 15:38:55 +00:00
hdr.udp.checksum = (bit<16>) tmp;
2019-07-11 06:47:34 +00:00
}
action delta_tcp_from_v4_to_v6()
{
2019-07-16 10:06:26 +00:00
delta_prepare();
bit<17> tmp = (bit<17>) hdr.tcp.checksum + (bit<17>) meta.v6sum;
if (tmp[16:16] == 1) {
tmp = tmp + 1;
tmp[16:16] = 0;
}
tmp = tmp + (bit<17>) (0xffff - meta.v6sum);
if (tmp[16:16] == 1) {
tmp = tmp + 1;
tmp[16:16] = 0;
}
2019-07-17 15:38:55 +00:00
hdr.tcp.checksum = (bit<16>) tmp;
2019-07-11 06:47:34 +00:00
}
action delta_ipv4_from_v6_to_v4()
{
2019-07-17 13:46:54 +00:00
bit<16> tmp = 0;
2019-07-15 14:48:24 +00:00
2019-07-17 13:46:54 +00:00
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 */
2019-07-16 10:06:26 +00:00
/* we don't have ANY checksum, but tcp or udp: we can
base on that ones for calculating the diff for IPv4
2019-07-17 13:46:54 +00:00
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
2019-07-16 10:06:26 +00:00
*/
hdr.ipv4.checksum = 0; /* TO BE DONE! */
2019-07-11 06:47:34 +00:00
}
2019-07-15 14:50:09 +00:00
2019-07-10 07:07:17 +00:00
#endif