-
Notifications
You must be signed in to change notification settings - Fork 22
/
l2_switch.p4
114 lines (99 loc) · 2.6 KB
/
l2_switch.p4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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
#define V1MODEL_VERSION 20200408
#include <core.p4>
#include <v1model.p4>
typedef bit<16> McastGrp_t;
typedef bit<48> MacAddr_t;
header ethernet_t {
MacAddr_t dstAddr;
MacAddr_t srcAddr;
bit<16> etherType;
}
struct metadata {
}
struct headers {
ethernet_t ethernet;
}
parser ParserImpl(packet_in packet, out headers hdr, inout metadata meta, inout standard_metadata_t standard_metadata) {
state parse_ethernet {
packet.extract(hdr.ethernet);
transition accept;
}
state start {
transition parse_ethernet;
}
}
counter<PortId_t>(512, CounterType.packets) igPortsCounts;
counter<PortId_t>(512, CounterType.packets) egPortsCounts;
control EgressImpl(inout headers hdr, inout metadata meta, inout standard_metadata_t standard_metadata) {
apply {
if (standard_metadata.egress_port == standard_metadata.ingress_port) {
mark_to_drop(standard_metadata);
} else {
egPortsCounts.count(standard_metadata.egress_port);
}
}
}
struct digest_t {
MacAddr_t srcAddr;
PortId_t ingressPort;
}
control IngressImpl(inout headers hdr, inout metadata meta, inout standard_metadata_t standard_metadata) {
action drop() {
mark_to_drop(standard_metadata);
}
action learn_mac() {
digest<digest_t>(0, {hdr.ethernet.srcAddr, standard_metadata.ingress_port});
}
action fwd(PortId_t eg_port) {
standard_metadata.egress_spec = eg_port;
}
action broadcast(McastGrp_t mgrp) {
standard_metadata.mcast_grp = mgrp;
}
table smac {
key = {
hdr.ethernet.srcAddr: exact;
}
actions = {
learn_mac;
NoAction;
}
const default_action = learn_mac();
size = 4096;
support_timeout = true;
}
table dmac {
key = {
hdr.ethernet.dstAddr: exact;
}
actions = {
fwd;
broadcast;
drop;
}
default_action = drop();
size = 4096;
}
apply {
igPortsCounts.count(standard_metadata.ingress_port);
smac.apply();
dmac.apply();
}
}
control DeparserImpl(packet_out packet, in headers hdr) {
apply {
packet.emit(hdr.ethernet);
}
}
control verifyChecksum(inout headers hdr, inout metadata meta) {
apply { }
}
control computeChecksum(inout headers hdr, inout metadata meta) {
apply { }
}
V1Switch(p = ParserImpl(),
ig = IngressImpl(),
vr = verifyChecksum(),
eg = EgressImpl(),
ck = computeChecksum(),
dep = DeparserImpl()) main;