orchestrator.p4 4.31 KB
Newer Older
1 2 3 4 5 6 7
#include <core.p4>
#define V1MODEL_VERSION 20200408
#include <v1model.p4>
#include "includes/defines.p4"
#include "includes/headers.p4"
#include "includes/parsers.p4"

8 9
//extern void prime();
//extern void prime2();
10 11 12

control ingress(inout headers hdr, inout metadata meta, inout standard_metadata_t standard_metadata) {

13 14 15 16 17
    register<bit<8>>(1) function_id_check; 
    register<bit<64>>(1) fwd_checks; 
    bit<8> pc;
    bit<64> pc2;

18 19 20 21 22 23 24 25 26 27 28 29 30
    @name(".fwd_act") action fwd_act(bit<16> port) {
        standard_metadata.egress_spec = port;
    }

    @name(".fwd") table fwd {
        actions = {
            fwd_act;
        }
        key = {
            standard_metadata.ingress_port : exact;
        }
    }

31
    @name(".dispatch_act") action dispatch_act(bit<32> dstAddr, bit<16> dstPort, bit<48> ethernetAddr , bit<16> egress_port) {
32 33
        hdr.ipv4.dstAddr = dstAddr;
        hdr.udp.dstPort = dstPort;
34
        hdr.ethernet.dstAddr = ethernetAddr;
35 36 37 38 39 40 41
        //prime();
    }
    @name(".prime1_act") action prime1_act(bit<32> dstAddr, bit<16> dstPort, bit<48> ethernetAddr , bit<16> egress_port) {
        hdr.ipv4.dstAddr = dstAddr;
        hdr.udp.dstPort = dstPort;
        hdr.ethernet.dstAddr = ethernetAddr;
        //prime();
42
    }
43 44 45 46 47 48 49
    @name(".prime2_act") action prime2_act(bit<32> dstAddr, bit<16> dstPort, bit<48> ethernetAddr , bit<16> egress_port) {
        hdr.ipv4.dstAddr = dstAddr;
        hdr.udp.dstPort = dstPort;
        hdr.ethernet.dstAddr = ethernetAddr;
        //prime2();
    }
    
50 51 52 53

    @name(".dispatch") table dispatch {
        actions = {
            dispatch_act;
54 55
            //prime1_act;
            //prime2_act;
56 57
        }
        key = {
58
            hdr.map_hdr.function_id : exact;
59 60 61
        }
    }
    apply {
62
        if (hdr.ipv4.isValid() && hdr.udp.dstPort == DISPATCHER_PORT) {
63 64 65 66
            //function_id_check.read(pc,0);
            //pc = 8w2;
            //pc = hdr.map_hdr.function_id;
            //function_id_check.write(0,pc);
67
            dispatch.apply();
68
            fwd.apply();
69 70 71
        } else {
            fwd.apply();
        }
72 73 74
        fwd_checks.read(pc2,0);
        pc2 = pc2 + 1;
        fwd_checks.write(0,pc2);
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
    }
}

control egress(inout headers hdr, inout metadata meta, inout standard_metadata_t standard_metadata) {

    // @name(".ethernet_set_mac_act") action ethernet_set_mac_act(bit<48> smac, bit<48> dmac) {
    //     hdr.ethernet.srcAddr = smac;
    //     hdr.ethernet.dstAddr = dmac;
    // }
    // @name(".ethernet_set_mac") table ethernet_set_mac {
    //     actions = {
    //         ethernet_set_mac_act;
    //     }
    //     key = {
    //         standard_metadata.egress_port: exact;
    //     }
    // }

    @name("fix_checksum") action fix_checksum() {
        hdr.udp.checksum = 16w0;
    }
  

    apply {
        // if (hdr.udp.dstPort == MDS_PORT) {           
        //     ethernet_set_mac.apply();
        // }
        fix_checksum();
    }
}

control DeparserImpl(packet_out packet, in headers hdr) {
    apply {
        packet.emit<ethernet_t>(hdr.ethernet);
        packet.emit<ipv4_t>(hdr.ipv4);
        packet.emit<udp_t>(hdr.udp);
        packet.emit<map_hdr_t>(hdr.map_hdr);
    }
}

control verifyChecksum(inout headers hdr, inout metadata meta) {
    apply {
        verify_checksum(
            hdr.ipv4.isValid(),
            { hdr.ipv4.version,
            hdr.ipv4.ihl,
            hdr.ipv4.diffserv,
            hdr.ipv4.totalLen,
            hdr.ipv4.identification,
            hdr.ipv4.flags,
            hdr.ipv4.fragOffset,
            hdr.ipv4.ttl,
            hdr.ipv4.protocol,
            hdr.ipv4.srcAddr,
            hdr.ipv4.dstAddr },
            hdr.ipv4.hdrChecksum,
            HashAlgorithm.csum16);
    }
}

control computeChecksum(inout headers hdr, inout metadata meta) {
    apply {
        update_checksum(
            hdr.ipv4.isValid(),
            { hdr.ipv4.version,
            hdr.ipv4.ihl,
            hdr.ipv4.diffserv,
            hdr.ipv4.totalLen,
            hdr.ipv4.identification,
            hdr.ipv4.flags,
            hdr.ipv4.fragOffset,
            hdr.ipv4.ttl,
            hdr.ipv4.protocol,
            hdr.ipv4.srcAddr,
            hdr.ipv4.dstAddr },
            hdr.ipv4.hdrChecksum,
            HashAlgorithm.csum16);
    }
}

V1Switch<headers, metadata>(ParserImpl(), verifyChecksum(), ingress(), egress(), computeChecksum(), DeparserImpl()) main;