/
pub_sub.p4
156 lines (128 loc) · 4.51 KB
/
pub_sub.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
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
156
/******************* -*- P4_16 -*- *****************/
/******** pubsub << REGISTER-BASED >> ***********/
#include <core.p4>
#include <v1model.p4>
#include "include/pubsub_define.p4"
#include "include/pubsub_header.p4"
#include "include/pubsub_parser.p4"
#include "include/pubsub_checksum.p4"
/*************************************************************************
************** I N G R E S S P R O C E S S I N G *******************
*************************************************************************/
control MyIngress(inout headers hdr,
inout local_metadata_t local_metadata,
inout standard_metadata_t standard_metadata) {
register<bit<4>>(4) subIndxPort;
action drop() {
mark_to_drop(standard_metadata);
}
action ipCode() {
local_metadata.ipDstCode = hdr.ipv4.dstAddr[31:24];
}
action check_flags_plus_in_port_mask() {
bit<9> tmp1;
bit<32> tmp2;
tmp1 = standard_metadata.ingress_port - 1;
local_metadata.port_indx = (bit<4>)1 << (bit<4>)tmp1[3:0];
tmp2 = hdr.ipv4.dstAddr;
local_metadata.pubsub_flags = (bit<2>)tmp2[23:22];
local_metadata.pubsub_indx = (bit<22>)tmp2[21:0];
}
action update_registers_subscribe() {
bit<4> tmp;
subIndxPort.read(tmp, (bit<32>)(local_metadata.pubsub_indx - 1));
subIndxPort.write((bit<32>)(local_metadata.pubsub_indx - 1), (tmp | local_metadata.port_indx));
}
action update_registers_unsubscribe() {
bit<4> tmp;
subIndxPort.read(tmp, (bit<32>)(local_metadata.pubsub_indx - 1));
subIndxPort.write((bit<32>)(local_metadata.pubsub_indx - 1), (~local_metadata.port_indx & tmp));
}
action publish() {
bit<22> tmp1;
bit<4> tmp2;
tmp1 = local_metadata.pubsub_indx - 1;
subIndxPort.read(tmp2, (bit<32>)tmp1);
local_metadata.mcastGrp_id = tmp2;
}
action set_mcast_grp() {
standard_metadata.mcast_grp = (bit<16>)local_metadata.mcastGrp_id;
}
action ipv4_forward(mac_addr_t dstAddr, port_num_t port) {
standard_metadata.egress_spec = port;
hdr.ethernet.srcAddr = hdr.ethernet.dstAddr;
hdr.ethernet.dstAddr = dstAddr;
hdr.ipv4.ttl = hdr.ipv4.ttl - 1;
}
action pubsub_reg_forward(port_num_t st_mc_grp) {
bit<4> tmp;
tmp = local_metadata.port_indx;
standard_metadata.mcast_grp = (bit<16>)(~tmp & (bit<4>)st_mc_grp);
}
table ipv4_lpm {
key = {
hdr.ipv4.dstAddr: lpm;
}
actions = {
ipv4_forward;
drop;
NoAction;
}
size = 1024;
default_action = drop();
}
table distribution_tree_pubsub_lpm {
key = {
hdr.ipv4.dstAddr: lpm;
}
actions = {
pubsub_reg_forward;
drop;
NoAction;
}
size = 1024;
default_action = drop();
}
apply {
if (hdr.ipv4.isValid()) {
ipCode();
if (local_metadata.ipDstCode == 239 && hdr.udp.isValid() && hdr.udp.dstPrt == 65432) {
check_flags_plus_in_port_mask();
if (local_metadata.pubsub_flags == 0) { // PUBLISH
publish();
set_mcast_grp();
}
else if (local_metadata.pubsub_flags == 2) { // SUBSCRIBE remove
update_registers_unsubscribe();
distribution_tree_pubsub_lpm.apply();
}
else if (local_metadata.pubsub_flags == 3) { // SUBSCRIBE register
update_registers_subscribe();
distribution_tree_pubsub_lpm.apply();
}
}
else {
ipv4_lpm.apply();
}
}
}
}
/*************************************************************************
**************** E G R E S S P R O C E S S I N G *******************
*************************************************************************/
control MyEgress(inout headers hdr,
inout local_metadata_t meta,
inout standard_metadata_t standard_metadata) {
apply { }
}
/*************************************************************************
*********************** S W I T C H *******************************
*************************************************************************/
V1Switch(
MyParser(),
MyVerifyChecksum(),
MyIngress(),
MyEgress(),
MyComputeChecksum(),
MyDeparser()
) main;