forked from p4lang/behavioral-model
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple_pre_lag.cpp
206 lines (188 loc) · 6.61 KB
/
simple_pre_lag.cpp
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/* Copyright 2013-present Barefoot Networks, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Srikrishna Gopu ([email protected])
* Antonin Bas ([email protected])
*
*/
#include <bm/bm_sim/simple_pre_lag.h>
#include <bm/bm_sim/logger.h>
#include <string>
#include <vector>
#include "jsoncpp/json.h"
namespace bm {
McSimplePre::McReturnCode
McSimplePreLAG::mc_node_create(const rid_t rid,
const PortMap &port_map,
const LagMap &lag_map,
l1_hdl_t *l1_hdl) {
boost::unique_lock<boost::shared_mutex> lock(mutex);
l2_hdl_t l2_hdl;
size_t num_l1_entries = l1_entries.size();
size_t num_l2_entries = l2_entries.size();
if (num_l1_entries >= L1_MAX_ENTRIES) {
Logger::get()->error("node create failed, l1 table full");
return TABLE_FULL;
}
if (num_l2_entries >= L2_MAX_ENTRIES) {
Logger::get()->error("node create failed, l2 table full");
return TABLE_FULL;
}
if (l1_handles.get_handle(l1_hdl)) {
Logger::get()->error("node create failed, failed to obtain l1 handle");
return ERROR;
}
if (l2_handles.get_handle(&l2_hdl)) {
Logger::get()->error("node create failed, failed to obtain l2 handle");
return ERROR;
}
l1_entries.insert(std::make_pair(*l1_hdl, L1Entry(rid)));
auto &l1_entry = l1_entries.at(*l1_hdl);
l1_entry.l2_hdl = l2_hdl;
l2_entries.insert(
std::make_pair(l2_hdl, L2Entry(*l1_hdl, port_map, lag_map)));
Logger::get()->debug("node created for rid {}", rid);
return SUCCESS;
}
McSimplePre::McReturnCode
McSimplePreLAG::mc_node_update(const l1_hdl_t l1_hdl,
const PortMap &port_map,
const LagMap &lag_map) {
boost::unique_lock<boost::shared_mutex> lock(mutex);
if (!l1_handles.valid_handle(l1_hdl)) {
Logger::get()->error("node update failed, invalid l1 handle");
return INVALID_L1_HANDLE;
}
auto &l1_entry = l1_entries.at(l1_hdl);
auto l2_hdl = l1_entry.l2_hdl;
auto &l2_entry = l2_entries.at(l2_hdl);
l2_entry.port_map = port_map;
l2_entry.lag_map = lag_map;
Logger::get()->debug("node updated for rid {}", l1_entry.rid);
return SUCCESS;
}
McSimplePre::McReturnCode
McSimplePreLAG::mc_set_lag_membership(const lag_id_t lag_index,
const PortMap &port_map) {
boost::unique_lock<boost::shared_mutex> lock(mutex);
uint16_t member_count = 0;
if (lag_index > LAG_MAX_ENTRIES) {
Logger::get()->error("lag membership set failed, invalid lag index");
return ERROR;
}
for (unsigned int j = 0; j < port_map.size(); j++) {
if (port_map[j]) {
member_count++;
}
}
auto &lag_entry = lag_entries[lag_index];
lag_entry.member_count = member_count;
lag_entry.port_map = port_map;
Logger::get()->debug("lag membership set for lag index {}", lag_index);
return SUCCESS;
}
std::string
McSimplePreLAG::mc_get_entries() const {
Json::Value root(Json::objectValue);
{
boost::unique_lock<boost::shared_mutex> lock(mutex);
get_entries_common(&root);
Json::Value lags(Json::arrayValue);
for (const auto &p : lag_entries) {
Json::Value lag(Json::objectValue);
lag["id"] = Json::Value(Json::UInt(p.first));
const auto &entry = p.second;
Json::Value ports(Json::arrayValue);
for (size_t i = 0; i < entry.port_map.size(); i++) {
if (entry.port_map[i]) ports.append(Json::Value(Json::UInt(i)));
}
lag["ports"] = ports;
lags.append(lag);
}
root["lags"] = lags;
}
std::stringstream ss;
ss << root;
return ss.str();
}
void
McSimplePreLAG::reset_state() {
Logger::get()->debug("resetting PRE state");
boost::unique_lock<boost::shared_mutex> lock(mutex);
McSimplePre::reset_state_();
lag_entries.clear();
}
std::vector<McSimplePre::McOut>
McSimplePreLAG::replicate(const McSimplePre::McIn ingress_info) const {
std::vector<McSimplePre::McOut> egress_info_list;
egress_port_t port_id;
lag_id_t lag_index;
int lag_hash = 0xFF; // TODO(unknown): get lag hash from metadata
int port_count1 = 0, port_count2 = 0;
McSimplePre::McOut egress_info;
boost::shared_lock<boost::shared_mutex> lock(mutex);
auto mgid_it = mgid_entries.find(ingress_info.mgid);
if (mgid_it == mgid_entries.end()) {
Logger::get()->warn("Replication requested for mgid {}, which is not known "
"to the PRE", ingress_info.mgid);
return {};
}
const auto &mgid_entry = mgid_it->second;
for (const l1_hdl_t &l1_hdl : mgid_entry.l1_list) {
const auto &l1_entry = l1_entries.at(l1_hdl);
egress_info.rid = l1_entry.rid;
// Port replication
const auto &l2_entry = l2_entries.at(l1_entry.l2_hdl);
for (port_id = 0; port_id < l2_entry.port_map.size(); port_id++) {
if (l2_entry.port_map[port_id]) {
egress_info.egress_port = port_id;
egress_info_list.push_back(egress_info);
}
}
// Lag replication
for (lag_index = 0; lag_index < l2_entry.lag_map.size(); lag_index++) {
if (l2_entry.lag_map[lag_index]) {
auto lag_entry_it = lag_entries.find(lag_index);
if (lag_entry_it == lag_entries.end()) {
Logger::get()->warn("PRE LAG membership for LAG index {} not set",
lag_index);
continue;
}
const auto &lag_entry = lag_entry_it->second;
const auto &port_map = lag_entry.port_map;
if (lag_entry.member_count == 0) {
Logger::get()->debug("PRE LAG membership is empty for LAG index {}",
lag_index);
continue;
}
port_count1 = (lag_hash % lag_entry.member_count) + 1;
port_count2 = 0;
for (port_id = 0; port_id < port_map.size(); port_id++) {
if (port_map[port_id]) {
port_count2++;
}
if (port_count1 == port_count2) {
egress_info.egress_port = port_id;
egress_info_list.push_back(egress_info);
break;
}
}
}
}
}
BMLOG_DEBUG("number of packets replicated : {}", egress_info_list.size());
return egress_info_list;
}
} // namespace bm