375 lines
11 KiB
Text
375 lines
11 KiB
Text
/* -*- P4_16 -*- */
|
|
#include <core.p4>
|
|
#include <v1model.p4>
|
|
|
|
#include "headers.p4"
|
|
#include "parsers.p4"
|
|
#include "checksums.p4"
|
|
#include "settings.p4"
|
|
|
|
|
|
/*************************************************************************
|
|
************** I N G R E S S P R O C E S S I N G *******************
|
|
*************************************************************************/
|
|
|
|
control MyIngress(inout headers hdr,
|
|
inout metadata meta,
|
|
inout standard_metadata_t standard_metadata) {
|
|
|
|
/********************** ACTIONS ***********************************/
|
|
|
|
action drop() {
|
|
mark_to_drop();
|
|
}
|
|
|
|
action set_egress_port (port_t out_port) {
|
|
standard_metadata.egress_spec = out_port;
|
|
}
|
|
|
|
action controller_debug() {
|
|
meta.task = TASK_DEBUG;
|
|
meta.ingress_port = standard_metadata.ingress_port;
|
|
clone3(CloneType.I2E, 100, meta);
|
|
}
|
|
|
|
action controller_reply(task_t task) {
|
|
meta.task = task;
|
|
meta.ingress_port = standard_metadata.ingress_port;
|
|
clone3(CloneType.I2E, 100, meta);
|
|
}
|
|
|
|
action multicast_pkg(mcast_t mcast_grp) { /* Output PKG on correct ports (plural) */
|
|
standard_metadata.mcast_grp = mcast_grp;
|
|
}
|
|
|
|
|
|
/********************** NAT64 / NAT46 ACTIONS ***********************************/
|
|
|
|
/* NAT64 protocol unspecific changes */
|
|
action nat64_generic(ipv4_addr_t src, ipv4_addr_t dst) {
|
|
/* Stuff that might need to be fixed */
|
|
hdr.ipv4.version = (bit<4>)4;
|
|
hdr.ipv4.diff_serv = (bit<6>)0; // no ToS
|
|
hdr.ipv4.ecn = (bit<2>)0; // unsupported
|
|
|
|
hdr.ipv4.ihl = (bit<4>) 5; // internet header length -- needs to be dynamic!?
|
|
hdr.ipv4.totalLen = (bit<16>) hdr.ipv6.payload_length + 5; // should probably also dynamic
|
|
|
|
hdr.ipv4.identification = (bit<16>) 0; // no support for fragments
|
|
hdr.ipv4.flags = (bit<3>) 0; // DF bit and more fragments, unsupported ATM
|
|
hdr.ipv4.fragOffset = (bit<13>) 0; // 0 as there are no fragments
|
|
|
|
/* Stuff that should be fine */
|
|
hdr.ethernet.ethertype = TYPE_IPV4;
|
|
|
|
hdr.ipv4.dst_addr = dst;
|
|
hdr.ipv4.src_addr = src;
|
|
|
|
hdr.ipv4.ttl = hdr.ipv6.hop_limit;
|
|
hdr.ipv4.protocol = hdr.ipv6.next_header;
|
|
|
|
hdr.ipv6.setInvalid();
|
|
hdr.ipv4.setValid();
|
|
}
|
|
|
|
/* NAT46: protocol unspecific changes */
|
|
action nat46_generic(ipv6_addr_t src, ipv6_addr_t dst) {
|
|
hdr.ethernet.ethertype=TYPE_IPV6;
|
|
|
|
hdr.ipv6.dst_addr = dst;
|
|
hdr.ipv6.src_addr = src;
|
|
|
|
hdr.ipv6.version = (bit<4>)6;
|
|
hdr.ipv6.traffic_class = (bit<8>) hdr.ipv4.diff_serv;
|
|
hdr.ipv6.flow_label = (bit<20>) 0;
|
|
hdr.ipv6.payload_length = (bit<16>) hdr.ipv4.totalLen - 20;
|
|
|
|
hdr.ipv6.next_header = hdr.ipv4.protocol;
|
|
hdr.ipv6.hop_limit = hdr.ipv4.ttl;
|
|
|
|
hdr.ipv4.setInvalid();
|
|
hdr.ipv6.setValid();
|
|
}
|
|
|
|
|
|
/* nat64_prefix is the same as the matching key, but without the mask */
|
|
action nat64_static(ipv6_addr_t v6_src, ipv4_addr_t v4_dst, ipv6_addr_t nat64_prefix) {
|
|
ipv6_addr_t src_offset = hdr.ipv6.src_addr - v6_src;
|
|
ipv4_addr_t src = v4_dst + (ipv4_addr_t) src_offset;
|
|
|
|
ipv4_addr_t dst = (ipv4_addr_t) (hdr.ipv6.dst_addr - nat64_prefix);
|
|
|
|
nat64_generic(src, dst);
|
|
|
|
/* fix the protocol specific translations */
|
|
// switch() ...
|
|
}
|
|
|
|
/* matching key: v4_network specified again */
|
|
action nat46_static(ipv6_addr_t v6_src, ipv4_addr_t v4_dst, ipv6_addr_t nat64_prefix) {
|
|
ipv6_addr_t src = nat64_prefix + (ipv6_addr_t) hdr.ipv4.src_addr;
|
|
|
|
ipv4_addr_t dst_offset = hdr.ipv4.dst_addr - v4_dst;
|
|
ipv6_addr_t dst = v6_src + (ipv6_addr_t) dst_offset;
|
|
|
|
nat46_generic(src, dst);
|
|
|
|
/* fix the protocol specific translations */
|
|
// switch() ...
|
|
}
|
|
|
|
table nat64 {
|
|
key = {
|
|
// hdr.ipv6.src_addr: lpm;
|
|
hdr.ipv6.dst_addr: lpm;
|
|
}
|
|
actions = {
|
|
controller_debug;
|
|
nat64_static;
|
|
NoAction;
|
|
}
|
|
size = NAT64_TABLE_SIZE;
|
|
default_action = controller_debug;
|
|
}
|
|
|
|
table nat46 {
|
|
key = {
|
|
// hdr.ipv4.src_addr: lpm;
|
|
hdr.ipv4.dst_addr: lpm;
|
|
}
|
|
actions = {
|
|
controller_debug;
|
|
nat46_static;
|
|
NoAction;
|
|
}
|
|
size = NAT64_TABLE_SIZE;
|
|
default_action = controller_debug;
|
|
}
|
|
|
|
|
|
/********************** ICMP6 ***********************************/
|
|
|
|
/* old/unused action */
|
|
action icmp6_answer() {
|
|
if(hdr.icmp6.isValid()) {
|
|
if(hdr.icmp6.code == ICMP6_ECHO_REQUEST) {
|
|
ipv6_addr_t tmp = hdr.ipv6.src_addr;
|
|
hdr.ipv6.src_addr = hdr.ipv6.dst_addr;
|
|
hdr.ipv6.dst_addr = tmp;
|
|
hdr.icmp6.code = ICMP6_ECHO_REPLY;
|
|
}
|
|
}
|
|
}
|
|
|
|
action icmp6_neighbor_solicitation(ipv6_addr_t addr, mac_addr_t mac_addr) {
|
|
/* egress = ingress */
|
|
standard_metadata.egress_spec = standard_metadata.ingress_port;
|
|
|
|
/* 1. IPv6 changes */
|
|
hdr.ipv6.dst_addr = hdr.ipv6.src_addr;
|
|
hdr.ipv6.src_addr = addr;
|
|
|
|
/* 2. ICMP6 changes */
|
|
hdr.icmp6.type = ICMP6_NA;
|
|
hdr.icmp6.code = 0;
|
|
// checksum is calculated in deparser
|
|
|
|
/* 3. icmp6/neighbor advertisement: values taken from real world answers */
|
|
hdr.icmp6_na_ns.router = 0;
|
|
hdr.icmp6_na_ns.solicitated = 1;
|
|
hdr.icmp6_na_ns.override = 1;
|
|
hdr.icmp6_na_ns.reserved = 0;
|
|
hdr.icmp6_na_ns.target_addr = addr;
|
|
|
|
/* 4. Link layer options */
|
|
hdr.icmp6_option_link_layer_addr.type = ICMP6_NDP_OPT_TARGET_LL;
|
|
hdr.icmp6_option_link_layer_addr.ll_length = 1; /* 1* 64 bit */
|
|
hdr.icmp6_option_link_layer_addr.mac_addr = mac_addr;
|
|
|
|
|
|
/* version1: rebuilding packet */
|
|
/*
|
|
truncate((bit<32>)(112 + 320 + 32)/8);
|
|
hdr.icmp6_na.setValid();
|
|
|
|
hdr.icmp6_na.solicitated = 1;
|
|
hdr.icmp6_na.override = 1;
|
|
*/
|
|
|
|
/* checksum trigger / content */
|
|
meta.task = TASK_CHECKSUM_ICMP6_NA;
|
|
meta.cast_length = (bit<32>) hdr.ipv6.payload_length;
|
|
}
|
|
|
|
action icmp6_echo_reply() {
|
|
mac_addr_t mac_tmp = hdr.ethernet.dst_addr;
|
|
hdr.ethernet.dst_addr = hdr.ethernet.src_addr;
|
|
hdr.ethernet.src_addr = mac_tmp;
|
|
|
|
ipv6_addr_t addr_tmp = hdr.ipv6.dst_addr;
|
|
hdr.ipv6.dst_addr = hdr.ipv6.src_addr;
|
|
hdr.ipv6.src_addr = addr_tmp;
|
|
|
|
hdr.icmp6.type = ICMP6_ECHO_REPLY;
|
|
|
|
meta.task = TASK_CHECKSUM_ICMP6;
|
|
meta.cast_length = (bit<32>) hdr.ipv6.payload_length;
|
|
}
|
|
|
|
table icmp6 {
|
|
key = {
|
|
hdr.ipv6.dst_addr: lpm;
|
|
hdr.icmp6.type: exact;
|
|
}
|
|
actions = {
|
|
controller_debug;
|
|
icmp6_neighbor_solicitation;
|
|
icmp6_echo_reply;
|
|
NoAction;
|
|
}
|
|
size = ICMP6_TABLE_SIZE;
|
|
default_action = NoAction;
|
|
}
|
|
|
|
/********************** debugging / general support ***********************************/
|
|
|
|
table port2mcast {
|
|
key = {
|
|
standard_metadata.ingress_port : exact;
|
|
}
|
|
actions = {
|
|
multicast_pkg;
|
|
controller_debug;
|
|
NoAction;
|
|
}
|
|
size = NDP_TABLE_SIZE;
|
|
default_action = NoAction;
|
|
// default_action = controller_debug;
|
|
}
|
|
|
|
/* Handle multicast registration of NDP */
|
|
table addr2mcast {
|
|
key = {
|
|
hdr.ipv6.dst_addr: exact;
|
|
}
|
|
actions = {
|
|
multicast_pkg;
|
|
controller_debug;
|
|
NoAction;
|
|
}
|
|
size = NDP_TABLE_SIZE;
|
|
default_action = NoAction;
|
|
// default_action = controller_debug;
|
|
}
|
|
|
|
/********************** NDP support for OTHERS (unused ATM) ***********************************/
|
|
|
|
table ndp {
|
|
key = {
|
|
hdr.ipv6.dst_addr: lpm;
|
|
standard_metadata.ingress_port : exact;
|
|
}
|
|
actions = {
|
|
multicast_pkg;
|
|
controller_debug;
|
|
NoAction;
|
|
}
|
|
size = NDP_TABLE_SIZE;
|
|
default_action = controller_debug;
|
|
}
|
|
|
|
|
|
|
|
/********************** ROUTING (egress definiton) TABLES ***********************************/
|
|
|
|
table v6_addresses {
|
|
key = {
|
|
hdr.ipv6.dst_addr: exact;
|
|
}
|
|
actions = {
|
|
controller_debug;
|
|
controller_reply;
|
|
icmp6_echo_reply;
|
|
NoAction;
|
|
}
|
|
size = ADDRESS_TABLE_SIZE;
|
|
default_action = NoAction;
|
|
|
|
}
|
|
|
|
table v6_networks {
|
|
key = {
|
|
hdr.ipv6.dst_addr: lpm;
|
|
}
|
|
actions = {
|
|
set_egress_port;
|
|
controller_debug;
|
|
controller_reply;
|
|
NoAction;
|
|
}
|
|
size = ROUTING_TABLE_SIZE;
|
|
// default_action = NoAction;
|
|
default_action = controller_debug;
|
|
}
|
|
|
|
table v4_networks {
|
|
key = {
|
|
hdr.ipv4.dst_addr: lpm;
|
|
}
|
|
actions = {
|
|
set_egress_port;
|
|
NoAction;
|
|
}
|
|
size = ROUTING_TABLE_SIZE;
|
|
default_action = NoAction;
|
|
}
|
|
|
|
/********************** APPLYING TABLES ***********************************/
|
|
apply {
|
|
if(hdr.ipv6.isValid()) {
|
|
icmp6.apply(); /* icmp6 echo, icmp6 ndp */
|
|
nat64.apply(); /* v6 -> v4 */
|
|
v6_networks.apply(); /* routing, egress */
|
|
}
|
|
if(hdr.ipv4.isValid()) {
|
|
nat46.apply(); /* v4->v6 */
|
|
v4_networks.apply(); /* routing, egress */
|
|
}
|
|
}
|
|
}
|
|
|
|
/*************************************************************************
|
|
**************** E G R E S S P R O C E S S I N G *******************
|
|
*************************************************************************/
|
|
|
|
control MyEgress(inout headers hdr,
|
|
inout metadata meta,
|
|
inout standard_metadata_t standard_metadata) {
|
|
apply {
|
|
// ingress clone
|
|
if (standard_metadata.instance_type == 1){
|
|
hdr.cpu.setValid();
|
|
hdr.cpu.task = meta.task;
|
|
hdr.cpu.ethertype = hdr.ethernet.ethertype;
|
|
hdr.cpu.ingress_port = (bit<16>) meta.ingress_port;
|
|
|
|
hdr.ethernet.ethertype = TYPE_CPU;
|
|
}
|
|
}
|
|
}
|
|
|
|
/*************************************************************************
|
|
*********************** S W I T C H *******************************
|
|
*************************************************************************/
|
|
|
|
V1Switch(
|
|
MyParser(),
|
|
MyVerifyChecksum(),
|
|
MyIngress(),
|
|
MyEgress(),
|
|
MyComputeChecksum(),
|
|
MyDeparser()
|
|
) main;
|
|
|
|
|
|
// truncate((bit<32>)22); //ether+cpu header
|