150 lines
4.1 KiB
Plaintext
150 lines
4.1 KiB
Plaintext
/* -*- P4_16 -*- */
|
|
#include <core.p4>
|
|
#include <v1model.p4>
|
|
|
|
/*************************************************************************
|
|
*********************** H E A D E R S ***********************************
|
|
*************************************************************************/
|
|
|
|
typedef bit<9> egressSpec_t;
|
|
typedef bit<48> macAddr_t;
|
|
typedef bit<32> ip4Addr_t;
|
|
|
|
|
|
header ethernet_t {
|
|
macAddr_t dstAddr;
|
|
macAddr_t srcAddr;
|
|
bit<16> etherType;
|
|
}
|
|
|
|
|
|
struct metadata {
|
|
/* empty */
|
|
}
|
|
|
|
struct headers {
|
|
ethernet_t ethernet;
|
|
}
|
|
|
|
/*************************************************************************
|
|
*********************** P A R S E R ***********************************
|
|
*************************************************************************/
|
|
|
|
parser MyParser(packet_in packet,
|
|
out headers hdr,
|
|
inout metadata meta,
|
|
inout standard_metadata_t standard_metadata) {
|
|
|
|
state start {
|
|
transition parse_ethernet;
|
|
}
|
|
|
|
state parse_ethernet {
|
|
packet.extract(hdr.ethernet);
|
|
transition select(hdr.ethernet.etherType) {
|
|
default : accept;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
/*************************************************************************
|
|
************ C H E C K S U M V E R I F I C A T I O N *************
|
|
*************************************************************************/
|
|
|
|
control MyVerifyChecksum(inout headers hdr, inout metadata meta) {
|
|
apply { }
|
|
}
|
|
|
|
|
|
/*************************************************************************
|
|
************** 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) {
|
|
|
|
action drop() {
|
|
mark_to_drop(standard_metadata);
|
|
}
|
|
|
|
// TODO: define `multicast` action to multicast packets to group 1
|
|
// Hint: Check v1model for multicast group
|
|
|
|
action mac_forward(egressSpec_t port) {
|
|
standard_metadata.egress_spec = port;
|
|
}
|
|
|
|
table mac_lookup {
|
|
key = {
|
|
hdr.ethernet.dstAddr : exact;
|
|
}
|
|
actions = {
|
|
// TODO: add `multicast` action to the list of available actions
|
|
mac_forward;
|
|
drop;
|
|
}
|
|
size = 1024;
|
|
// TODO : replace default drop action by multicast
|
|
default_action = drop;
|
|
}
|
|
apply {
|
|
if (hdr.ethernet.isValid())
|
|
mac_lookup.apply();
|
|
}
|
|
}
|
|
|
|
/*************************************************************************
|
|
**************** 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) {
|
|
|
|
action drop() {
|
|
mark_to_drop(standard_metadata);
|
|
}
|
|
|
|
apply {
|
|
// Prune multicast packet to ingress port to preventing loop
|
|
if (standard_metadata.egress_port == standard_metadata.ingress_port)
|
|
drop();
|
|
}
|
|
}
|
|
|
|
/*************************************************************************
|
|
************* C H E C K S U M C O M P U T A T I O N **************
|
|
*************************************************************************/
|
|
|
|
control MyComputeChecksum(inout headers hdr, inout metadata meta) {
|
|
apply {
|
|
|
|
}
|
|
}
|
|
|
|
|
|
/*************************************************************************
|
|
*********************** D E P A R S E R *******************************
|
|
*************************************************************************/
|
|
|
|
control MyDeparser(packet_out packet, in headers hdr) {
|
|
apply {
|
|
packet.emit(hdr.ethernet);
|
|
}
|
|
}
|
|
|
|
/*************************************************************************
|
|
*********************** S W I T C H *******************************
|
|
*************************************************************************/
|
|
|
|
V1Switch(
|
|
MyParser(),
|
|
MyVerifyChecksum(),
|
|
MyIngress(),
|
|
MyEgress(),
|
|
MyComputeChecksum(),
|
|
MyDeparser()
|
|
) main;
|