|
| 1 | +/* -*- P4_16 -*- */ |
| 2 | +#include <core.p4> |
| 3 | +#include <v1model.p4> |
| 4 | + |
| 5 | +/************************************************************************* |
| 6 | +*********************** H E A D E R S *********************************** |
| 7 | +*************************************************************************/ |
| 8 | + |
| 9 | +typedef bit<9> egressSpec_t; |
| 10 | +typedef bit<48> macAddr_t; |
| 11 | + |
| 12 | +header ethernet_t { |
| 13 | + //TODO: define the following header fields |
| 14 | + //macAddr_t type destination address |
| 15 | + //macAddr_t type source address |
| 16 | + /16 bit etherType |
| 17 | +} |
| 18 | + |
| 19 | +struct metadata { |
| 20 | + /* empty */ |
| 21 | +} |
| 22 | + |
| 23 | +struct headers { |
| 24 | + //TODO: define a header ethernet of type ethernet_t |
| 25 | +} |
| 26 | + |
| 27 | +/************************************************************************* |
| 28 | +*********************** P A R S E R *********************************** |
| 29 | +*************************************************************************/ |
| 30 | + |
| 31 | +parser MyParser(packet_in packet, |
| 32 | + out headers hdr, |
| 33 | + inout metadata meta, |
| 34 | + inout standard_metadata_t standard_metadata) { |
| 35 | + |
| 36 | + state start { |
| 37 | + //TODO: define a state that extracts the ethernet header |
| 38 | + //and transitions to accept |
| 39 | + } |
| 40 | + |
| 41 | +} |
| 42 | + |
| 43 | + |
| 44 | +/************************************************************************* |
| 45 | +************ C H E C K S U M V E R I F I C A T I O N ************* |
| 46 | +*************************************************************************/ |
| 47 | + |
| 48 | +control MyVerifyChecksum(inout headers hdr, inout metadata meta) { |
| 49 | + apply { } |
| 50 | +} |
| 51 | + |
| 52 | + |
| 53 | +/************************************************************************* |
| 54 | +************** I N G R E S S P R O C E S S I N G ******************* |
| 55 | +*************************************************************************/ |
| 56 | + |
| 57 | +control MyIngress(inout headers hdr, |
| 58 | + inout metadata meta, |
| 59 | + inout standard_metadata_t standard_metadata) { |
| 60 | + |
| 61 | + action swap_mac_addresses() { |
| 62 | + macAddr_t tmp_mac; |
| 63 | + //TODO: swap source and destination MAC addresses |
| 64 | + //use the defined temp variable tmp_mac |
| 65 | + |
| 66 | + //TODO: send the packet back to the same port |
| 67 | + } |
| 68 | + |
| 69 | + action drop() { |
| 70 | + mark_to_drop(standard_metadata); |
| 71 | + } |
| 72 | + |
| 73 | + table src_mac_drop { |
| 74 | + key = { |
| 75 | + //TODO: define an exact match key using the source MAC address |
| 76 | + } |
| 77 | + actions = { |
| 78 | + //TODO: define 3 actions: swap_mac_addresses, drop, NoAction. |
| 79 | + } |
| 80 | + //TODO: define a table size of 1024 entries |
| 81 | + |
| 82 | + //TODO: define the default action to return the packet to the source |
| 83 | + } |
| 84 | + |
| 85 | + apply { |
| 86 | + //TODO: Check if the Ethernet header is valid |
| 87 | + //if so, lookup the source MAC in the table and decide what to do |
| 88 | + } |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | + |
| 93 | + |
| 94 | + |
| 95 | + |
| 96 | +/************************************************************************* |
| 97 | +**************** E G R E S S P R O C E S S I N G ******************* |
| 98 | +*************************************************************************/ |
| 99 | + |
| 100 | +control MyEgress(inout headers hdr, |
| 101 | + inout metadata meta, |
| 102 | + inout standard_metadata_t standard_metadata) { |
| 103 | + apply { } |
| 104 | +} |
| 105 | + |
| 106 | +/************************************************************************* |
| 107 | +************* C H E C K S U M C O M P U T A T I O N ************** |
| 108 | +*************************************************************************/ |
| 109 | + |
| 110 | +control MyComputeChecksum(inout headers hdr, inout metadata meta) { |
| 111 | + apply { |
| 112 | + |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | + |
| 117 | +/************************************************************************* |
| 118 | +*********************** D E P A R S E R ******************************* |
| 119 | +*************************************************************************/ |
| 120 | + |
| 121 | +control MyDeparser(packet_out packet, in headers hdr) { |
| 122 | + apply { |
| 123 | + //TODO: emit the packet with a valid Ethernet header |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +/************************************************************************* |
| 128 | +*********************** S W I T C H ******************************* |
| 129 | +*************************************************************************/ |
| 130 | + |
| 131 | +V1Switch( |
| 132 | +MyParser(), |
| 133 | +MyVerifyChecksum(), |
| 134 | +MyIngress(), |
| 135 | +MyEgress(), |
| 136 | +MyComputeChecksum(), |
| 137 | +MyDeparser() |
| 138 | +) main; |
0 commit comments