master-thesis/p4src/minip4_solution.p4
Nico Schottelius 6007001838 -1
2019-07-27 21:38:13 +02:00

214 lines
5.2 KiB
Text

#include <core.p4>
#include <sume_switch.p4>
#include "headers.p4"
#define Parsed_packet headers
#define user_metadata_t metadata
// digest_data, MUST be 256 bits -- not using it
struct digest_data_t {
bit<256> unused;
}
/********************************************************************************
* Parser
*/
parser RealParser(
packet_in packet,
out Parsed_packet hdr,
out user_metadata_t meta,
out digest_data_t digest_data,
inout sume_metadata_t standard_metadata) {
state start {
packet.extract(hdr.ethernet);
// meta.unused = 0;
digest_data.unused = 0;
transition accept;
}
}
@Xilinx_MaxPacketRegion(1024)
parser TopParser(
packet_in b,
out Parsed_packet p,
out user_metadata_t user_metadata,
out digest_data_t digest_data,
inout sume_metadata_t sume_metadata) {
RealParser() realparser;
state start {
realparser.apply(b, p, user_metadata, digest_data, sume_metadata);
transition accept;
}
}
/********************************************************************************
* Main
*/
control RealMain(
inout Parsed_packet hdr,
inout user_metadata_t meta,
inout digest_data_t digest_data,
inout sume_metadata_t sume_metadata) {
#include "actions_nat64_generic.p4"
#include "actions_egress.p4"
#include "actions_delta_checksum.p4"
action swap_eth_addresses() {
mac_addr_t temp = hdr.ethernet.dst_addr;
hdr.ethernet.dst_addr = hdr.ethernet.src_addr;
hdr.ethernet.src_addr = temp;
/* set egress port */
sume_metadata.dst_port = sume_metadata.src_port;
}
action send_to_port1() {
sume_metadata.dst_port = 1;
}
action send_to_all_ports() {
/* Taken from commands.txt of the "int" project:
table_cam_add_entry forward set_output_port 0xffffffffffff => 0b01010101
python convert:
>>> 0b01010101
85
*/
sume_metadata.dst_port = 85;
}
action do_nothing() {
mac_addr_t temp = hdr.ethernet.dst_addr;
}
table lookup_table {
key = {
hdr.ethernet.dst_addr: exact;
}
actions = {
swap_eth_addresses;
do_nothing;
send_to_port1;
send_to_all_ports;
}
size = 64;
default_action = send_to_port1; // test_port1()
}
apply {
bit<17> tmp17 = 0;
bool apply_v4networks = false;
bool apply_v6networks = false;
if(hdr.ipv6.isValid()) {
apply_v6networks = true;
if(nat64.apply().hit) { /* generic / static nat64 done */
if(hdr.icmp6.isValid()) {
nat64_icmp6_generic();
if(hdr.icmp6.type == ICMP6_ECHO_REPLY) {
hdr.icmp.type = ICMP_ECHO_REPLY;
hdr.icmp.code = 0;
}
if(hdr.icmp6.type == ICMP6_ECHO_REQUEST) {
hdr.icmp.type = ICMP_ECHO_REQUEST;
hdr.icmp.code = 0;
}
}
if(hdr.udp.isValid()) {
delta_udp_from_v6_to_v4
}
if(hdr.tcp.isValid()) {
delta_tcp_from_v6_to_v4
}
apply_v4networks = false;
apply_v6networks = true;
}
} else if(hdr.ipv4.isValid()) {
apply_v4networks = true;
if(nat46.apply().hit) {
if(hdr.icmp.isValid()) {
nat46_icmp_generic();
if(hdr.icmp.type == ICMP_ECHO_REPLY) {
hdr.icmp6.type = ICMP6_ECHO_REPLY;
}
if(hdr.icmp.type == ICMP_ECHO_REQUEST) {
hdr.icmp6.type = ICMP6_ECHO_REQUEST;
}
}
if(hdr.udp.isValid()) {
delta_udp_from_v4_to_v6
}
if(hdr.tcp.isValid()) {
delta_tcp_from_v4_to_v6
}
apply_v4networks = true;
apply_v6networks = false;
}
}
if(apply_v4networks == true) {
v4_networks.apply();
}
if(apply_v6networks == true) {
v6_networks.apply();
}
lookup_table.apply();
}
}
control TopPipe(
inout Parsed_packet p,
inout user_metadata_t user_metadata,
inout digest_data_t digest_data,
inout sume_metadata_t sume_metadata) {
RealMain() realmain;
apply {
realmain.apply(p, user_metadata, digest_data, sume_metadata);
}
}
/********************************************************************************
* Deparser
*/
@Xilinx_MaxPacketRegion(1024)
control TopDeparser(
packet_out b,
in Parsed_packet p,
in user_metadata_t user_metadata,
inout digest_data_t digest_data,
inout sume_metadata_t sume_metadata) {
apply {
b.emit(p.ethernet);
}
}
/********************************************************************************
* Switch
*/
SimpleSumeSwitch(
TopParser(),
TopPipe(),
TopDeparser()
) main;