Exercise for L2 multicast (#288)
This commit is contained in:
149
exercises/multicast/solution/multicast.p4
Normal file
149
exercises/multicast/solution/multicast.p4
Normal file
@@ -0,0 +1,149 @@
|
||||
/* -*- 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);
|
||||
}
|
||||
|
||||
action multicast() {
|
||||
standard_metadata.mcast_grp = 1;
|
||||
}
|
||||
|
||||
action mac_forward(egressSpec_t port) {
|
||||
standard_metadata.egress_spec = port;
|
||||
}
|
||||
|
||||
table mac_lookup {
|
||||
key = {
|
||||
hdr.ethernet.dstAddr : exact;
|
||||
}
|
||||
actions = {
|
||||
multicast;
|
||||
mac_forward;
|
||||
drop;
|
||||
}
|
||||
size = 1024;
|
||||
default_action = multicast;
|
||||
}
|
||||
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;
|
||||
70
exercises/multicast/solution/s1-runtime.json
Normal file
70
exercises/multicast/solution/s1-runtime.json
Normal file
@@ -0,0 +1,70 @@
|
||||
{
|
||||
"target": "bmv2",
|
||||
"p4info": "build/multicast.p4.p4info.txt",
|
||||
"bmv2_json": "build/multicast.json",
|
||||
"table_entries": [
|
||||
{
|
||||
"table": "MyIngress.mac_lookup",
|
||||
"match": {
|
||||
"hdr.ethernet.dstAddr": "08:00:00:00:01:11"
|
||||
},
|
||||
"action_name": "MyIngress.mac_forward",
|
||||
"action_params": {
|
||||
"port": 1
|
||||
}
|
||||
},
|
||||
{
|
||||
"table": "MyIngress.mac_lookup",
|
||||
"match": {
|
||||
"hdr.ethernet.dstAddr": "08:00:00:00:02:22"
|
||||
},
|
||||
"action_name": "MyIngress.mac_forward",
|
||||
"action_params": {
|
||||
"port": 2
|
||||
}
|
||||
},
|
||||
{
|
||||
"table": "MyIngress.mac_lookup",
|
||||
"match": {
|
||||
"hdr.ethernet.dstAddr": "08:00:00:00:03:33"
|
||||
},
|
||||
"action_name": "MyIngress.mac_forward",
|
||||
"action_params": {
|
||||
"port": 3
|
||||
}
|
||||
},
|
||||
{
|
||||
"table": "MyIngress.mac_lookup",
|
||||
"match": {
|
||||
"hdr.ethernet.dstAddr": "08:00:00:00:04:44"
|
||||
},
|
||||
"action_name": "MyIngress.mac_forward",
|
||||
"action_params": {
|
||||
"port": 4
|
||||
}
|
||||
}
|
||||
],
|
||||
"multicast_group_entries" : [
|
||||
{
|
||||
"multicast_group_id" : 1,
|
||||
"replicas" : [
|
||||
{
|
||||
"egress_port" : 1,
|
||||
"instance" : 1
|
||||
},
|
||||
{
|
||||
"egress_port" : 2,
|
||||
"instance" : 1
|
||||
},
|
||||
{
|
||||
"egress_port" : 3,
|
||||
"instance" : 1
|
||||
},
|
||||
{
|
||||
"egress_port" : 4,
|
||||
"instance" : 1
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user