Line data Source code
1 : /*
2 : * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3 : * Copyright Contributors to the OpenSDN project
4 : */
5 :
6 : #include <boost/uuid/uuid_io.hpp>
7 : #include <cmn/agent_cmn.h>
8 : #include <init/agent_param.h>
9 : #include <base/task_annotations.h>
10 : #include <oper/interface_common.h>
11 : #include <oper/vrf.h>
12 : #include <oper/nexthop.h>
13 : #include <oper/mpls.h>
14 : #include <oper/mirror_table.h>
15 : #include <oper/agent_sandesh.h>
16 : #include <resource_manager/resource_manager.h>
17 : #include <resource_manager/resource_table.h>
18 : #include <resource_manager/mpls_index.h>
19 :
20 : using namespace std;
21 :
22 : SandeshTraceBufferPtr MplsTraceBuf(SandeshTraceBufferCreate("MplsTrace", 1000));
23 :
24 : /****************************************************************************
25 : * MplsLabel routines
26 : ***************************************************************************/
27 448 : MplsLabel::MplsLabel(Agent *agent, uint32_t label) :
28 448 : agent_(agent), label_(label), free_label_(false) {
29 448 : }
30 :
31 896 : MplsLabel::~MplsLabel() {
32 448 : if (free_label_) {
33 82 : if (label_ != MplsTable::kInvalidLabel) {
34 82 : MplsTable *table = static_cast<MplsTable *>(get_table());
35 82 : table->FreeMplsLabelIndex(label_);
36 : }
37 82 : agent_->resource_manager()->Release(Resource::MPLS_INDEX, label_);
38 : }
39 896 : }
40 :
41 3078 : bool MplsLabel::IsLess(const DBEntry &rhs) const {
42 3078 : const MplsLabel &mpls = static_cast<const MplsLabel &>(rhs);
43 3078 : return label_ < mpls.label_;
44 : }
45 :
46 0 : std::string MplsLabel::ToString() const {
47 0 : return "MPLS";
48 : }
49 :
50 74 : DBEntryBase::KeyPtr MplsLabel::GetDBRequestKey() const {
51 74 : MplsLabelKey *key = new MplsLabelKey(label_);
52 74 : return DBEntryBase::KeyPtr(key);
53 : }
54 :
55 0 : void MplsLabel::SetKey(const DBRequestKey *k) {
56 0 : const MplsLabelKey *key = static_cast<const MplsLabelKey *>(k);
57 0 : label_ = key->label();
58 0 : }
59 :
60 82 : uint32_t MplsLabel::GetRefCount() const {
61 82 : return AgentRefCount<MplsLabel>::GetRefCount();
62 : }
63 :
64 82 : void MplsLabel::Add(const DBRequest *req) {
65 82 : free_label_ = true;
66 82 : ChangeInternal(req);
67 82 : SendObjectLog(agent_->mpls_table(), AgentLogEvent::ADD);
68 82 : return;
69 : }
70 :
71 41 : bool MplsLabel::Change(const DBRequest *req) {
72 41 : const AgentDBTable *table = static_cast<const AgentDBTable *>(get_table());
73 41 : bool ret = ChangeInternal(req);
74 41 : SendObjectLog(table, AgentLogEvent::CHANGE);
75 41 : return ret;
76 : }
77 :
78 82 : void MplsLabel::Delete(const DBRequest *req) {
79 82 : const AgentDBTable *table = static_cast<const AgentDBTable *>(get_table());
80 82 : SendObjectLog(table, AgentLogEvent::DEL);
81 82 : return;
82 : }
83 :
84 123 : bool MplsLabel::ChangeInternal(const DBRequest *req) {
85 123 : NextHopTable *nh_table = agent_->nexthop_table();
86 123 : MplsLabelData *data = static_cast<MplsLabelData *>(req->data.get());
87 : NextHop *nh =
88 123 : static_cast<NextHop *>(nh_table->FindActiveEntry(data->nh_key()));
89 123 : if (!nh) {
90 : // NextHop not found, point mpls label to discard
91 0 : DiscardNH key;
92 0 : nh = static_cast<NextHop *>(nh_table->FindActiveEntry(&key));
93 0 : }
94 :
95 123 : return ChangeNH(nh);
96 : }
97 :
98 166 : bool MplsLabel::ChangeNH(NextHop *nh) {
99 166 : if (nh_ == nh)
100 61 : return false;
101 :
102 105 : assert(nh);
103 105 : nh_ = nh;
104 :
105 105 : if (IsFabricMulticastReservedLabel()) {
106 0 : CompositeNH *cnh = dynamic_cast<CompositeNH*>(nh);
107 0 : if (cnh && cnh->vrf()) {
108 0 : FmgVrfNhMap::iterator it = fmg_nh_list_.begin();
109 0 : while( it != fmg_nh_list_.end()) {
110 0 : if (it->first != cnh->vrf()->GetName()) {
111 0 : FmgVrfNhMap::iterator temp_it;
112 0 : temp_it = it;
113 0 : it++;
114 0 : fmg_nh_list_.erase(temp_it);
115 : } else {
116 0 : it++;
117 : }
118 : }
119 0 : fmg_nh_list_[cnh->vrf()->GetName()] = nh;
120 : }
121 : }
122 :
123 105 : SyncDependentPath();
124 105 : return true;
125 : }
126 :
127 105 : void MplsLabel::SyncDependentPath() {
128 105 : MPLS_TRACE(MplsTrace, "Syncing routes for label ", label());
129 105 : for (DependentPathList::iterator iter =
130 210 : mpls_label_.begin(); iter != mpls_label_.end(); iter++) {
131 0 : AgentRoute *rt = iter.operator->();
132 0 : rt->EnqueueRouteResync();
133 : }
134 105 : }
135 :
136 114 : bool MplsLabel::IsFabricMulticastReservedLabel() const {
137 : //MplsTable *table = static_cast<MplsTable *>(get_table());
138 114 : MplsTable *table = static_cast<MplsTable *>(agent_->mpls_table());
139 114 : return table->IsFabricMulticastLabel(label_);
140 : }
141 :
142 : /****************************************************************************
143 : * MplsLabel Sandesh routines
144 : ***************************************************************************/
145 0 : bool MplsLabel::DBEntrySandesh(Sandesh *sresp, std::string &name) const {
146 0 : MplsResp *resp = static_cast<MplsResp *>(sresp);
147 :
148 0 : MplsSandeshData data;
149 0 : data.set_label(label_);
150 0 : nh_->SetNHSandeshData(data.nh);
151 : std::vector<MplsSandeshData> &list =
152 0 : const_cast<std::vector<MplsSandeshData>&>(resp->get_mpls_list());
153 0 : list.push_back(data);
154 :
155 0 : return true;
156 0 : }
157 :
158 205 : void MplsLabel::SendObjectLog(const AgentDBTable *table,
159 : AgentLogEvent::type event) const {
160 205 : MplsObjectLogInfo info;
161 205 : string str, type_str, nh_type;
162 205 : info.set_type(type_str);
163 205 : info.set_label((int)label_);
164 205 : switch (event) {
165 82 : case AgentLogEvent::ADD:
166 82 : str.assign("Addition ");
167 82 : break;
168 82 : case AgentLogEvent::DEL:
169 82 : str.assign("Deletion ");
170 82 : info.set_event(str);
171 82 : OPER_TRACE_ENTRY(Mpls, table, info);
172 82 : return;
173 41 : case AgentLogEvent::CHANGE:
174 41 : str.assign("Modification ");
175 41 : break;
176 0 : default:
177 0 : str.assign("Unknown");
178 0 : break;
179 : }
180 123 : info.set_event(str);
181 123 : const NextHop *nh = nexthop();
182 123 : const Interface *intf = NULL;
183 : /* Mpls is not expected to have any other nexthop apart from Interface
184 : or Vlan */
185 123 : if (nh != NULL) {
186 123 : string policy_str("Disabled");
187 : const InterfaceNH *if_nh;
188 : const VlanNH *vlan_nh;
189 :
190 123 : switch(nh->GetType()) {
191 70 : case NextHop::INTERFACE:
192 70 : nh_type.assign("INTERFACE");
193 70 : if_nh = static_cast<const InterfaceNH *>(nh);
194 70 : intf = if_nh->GetInterface();
195 70 : if (if_nh->PolicyEnabled()) {
196 28 : policy_str.assign("Enabled");
197 : }
198 70 : info.set_policy(policy_str);
199 70 : break;
200 0 : case NextHop::VLAN:
201 0 : nh_type.assign("VLAN");
202 0 : vlan_nh = static_cast<const VlanNH *>(nh);
203 0 : intf = vlan_nh->GetInterface();
204 0 : info.set_vlan_tag(vlan_nh->GetVlanTag());
205 0 : break;
206 39 : case NextHop::COMPOSITE:
207 39 : nh_type.assign("Composite");
208 39 : break;
209 14 : default:
210 14 : nh_type.assign("unknown");
211 14 : break;
212 : }
213 123 : }
214 123 : info.set_nh_type(nh_type);
215 : /* Interface Nexthop pointed by Mpls object will always be of type VMPORT */
216 123 : if (intf) {
217 70 : string if_type_str;
218 70 : switch(intf->type()) {
219 70 : case Interface::VM_INTERFACE:
220 70 : if_type_str.assign("VM_INTERFACE");
221 70 : break;
222 0 : default:
223 0 : if_type_str.assign("Invalid");
224 0 : break;
225 : }
226 70 : info.set_intf_type(if_type_str);
227 70 : info.set_intf_uuid(UuidToString(intf->GetUuid()));
228 70 : info.set_intf_name(intf->name());
229 70 : }
230 123 : OPER_TRACE_ENTRY(Mpls, table, info);
231 451 : }
232 :
233 : /****************************************************************************
234 : * MplsTable routines
235 : ***************************************************************************/
236 1 : MplsTable::MplsTable(DB *db, const std::string &name) :
237 1 : AgentDBTable(db, name) {
238 1 : }
239 :
240 2 : MplsTable::~MplsTable() {
241 2 : }
242 :
243 1 : DBTableBase *MplsTable::CreateTable(DB *db, const std::string &name) {
244 1 : MplsTable *table = new MplsTable(db, name);
245 1 : table->Init();
246 1 : return table;
247 : };
248 :
249 205 : void MplsTable::Process(DBRequest &req) {
250 205 : agent()->ConcurrencyCheck();
251 : DBTablePartition *tpart =
252 205 : static_cast<DBTablePartition *>(GetTablePartition(req.key.get()));
253 205 : tpart->Process(NULL, &req);
254 205 : }
255 :
256 : /*
257 : * Allocates label from resource manager, currently used for evpn and
258 : * ecmp labels.
259 : */
260 0 : uint32_t MplsTable::AllocLabel(ResourceManager::KeyPtr key) {
261 : uint32_t label = ((static_cast<IndexResourceData *>(agent()->resource_manager()->
262 0 : Allocate(key).get()))->index());
263 0 : assert(label != MplsTable::kInvalidLabel);
264 0 : return label;
265 : }
266 :
267 : //Free label from resource manager and delete db entry
268 1 : void MplsTable::FreeLabel(uint32_t label) {
269 1 : FreeLabel(label, std::string());
270 1 : }
271 :
272 8 : void MplsTable::FreeLabel(uint32_t label, const std::string &vrf_name) {
273 8 : DBRequest req;
274 8 : req.oper = DBRequest::DB_ENTRY_DELETE;
275 :
276 8 : MplsLabelKey *key = new MplsLabelKey(label);
277 8 : MplsLabelData *data = new MplsLabelData(NULL);
278 8 : data->set_vrf_name(vrf_name);
279 8 : req.key.reset(key);
280 8 : req.data.reset(data);
281 :
282 8 : Process(req);
283 8 : }
284 :
285 366 : std::unique_ptr<DBEntry> MplsTable::AllocEntry(const DBRequestKey *k) const {
286 366 : const MplsLabelKey *key = static_cast<const MplsLabelKey *>(k);
287 366 : MplsLabel *mpls = new MplsLabel(agent(), key->label());
288 366 : return std::unique_ptr<DBEntry>(static_cast<DBEntry *>(mpls));
289 : }
290 :
291 82 : DBEntry *MplsTable::Add(const DBRequest *req) {
292 82 : CheckVrLabelLimit();
293 82 : MplsLabelKey *key = static_cast<MplsLabelKey *>(req->key.get());
294 82 : assert(key->label() != MplsTable::kInvalidLabel);
295 :
296 82 : MplsLabel *mpls = new MplsLabel(agent(), key->label());
297 82 : label_table_.InsertAtIndex(mpls->label(), mpls);
298 82 : mpls->Add(req);
299 82 : return mpls;
300 : }
301 :
302 41 : bool MplsTable::OnChange(DBEntry *entry, const DBRequest *req) {
303 41 : MplsLabel *mpls = static_cast<MplsLabel *>(entry);
304 41 : return mpls->Change(req);
305 : }
306 :
307 82 : bool MplsTable::Delete(DBEntry *entry, const DBRequest *req) {
308 82 : MplsLabel *mpls = static_cast<MplsLabel *>(entry);
309 82 : if (IsFabricMulticastLabel(mpls->label())) {
310 0 : MplsLabelData *data = static_cast<MplsLabelData *>(req->data.get());
311 : // For multicast labels we not expect to be here
312 : // via MplsTable::OnZeroRefcount where data is not set.
313 0 : assert(data);
314 0 : if (mpls->fmg_nh_list().find(data->vrf_name()) !=
315 0 : mpls->fmg_nh_list().end()) {
316 0 : mpls->fmg_nh_list().erase(data->vrf_name());
317 : }
318 0 : if (mpls->fmg_nh_list().empty() == false) {
319 0 : if (mpls->ChangeNH(mpls->fmg_nh_list().begin()->second.get())) {
320 : DBTablePartBase *tpart =
321 0 : static_cast<DBTablePartition *>(GetTablePartition(mpls));
322 0 : tpart->Notify(mpls);
323 : }
324 0 : return false;
325 : }
326 : }
327 82 : mpls->Delete(req);
328 82 : CheckVrLabelLimit();
329 82 : return true;
330 : }
331 :
332 74 : void MplsTable::OnZeroRefcount(AgentDBEntry *e) {
333 74 : agent()->ConcurrencyCheck();
334 :
335 : //Delete db entry
336 74 : DBRequest req(DBRequest::DB_ENTRY_DELETE);
337 74 : req.key = e->GetDBRequestKey();
338 74 : req.data.reset(NULL);
339 74 : Process(req);
340 74 : }
341 :
342 49 : uint32_t MplsTable::CreateRouteLabel(uint32_t label, const NextHopKey *nh_key,
343 : const std::string &vrf_name,
344 : const std::string &route) {
345 49 : if (label == MplsTable::kInvalidLabel) {
346 : ResourceManager::KeyPtr key(new RouteMplsResourceKey(agent()->
347 8 : resource_manager(), vrf_name,
348 8 : route));
349 : label = ((static_cast<IndexResourceData *>(agent()->resource_manager()->
350 8 : Allocate(key).get()))->index());
351 8 : assert(label != MplsTable::kInvalidLabel);
352 8 : assert(FindMplsLabel(label) == NULL);
353 8 : }
354 :
355 49 : DBRequest req;
356 49 : req.oper = DBRequest::DB_ENTRY_ADD_CHANGE;
357 :
358 49 : MplsLabelKey *key = new MplsLabelKey(label);
359 49 : req.key.reset(key);
360 :
361 49 : MplsLabelData *data = new MplsLabelData(nh_key->Clone());
362 49 : data->set_vrf_name(vrf_name);
363 49 : req.data.reset(data);
364 :
365 49 : Process(req);
366 49 : return label;
367 49 : }
368 :
369 228 : bool MplsTable::IsFabricMulticastLabel(uint32_t label) const {
370 684 : for (uint8_t count = 0; count < MAX_XMPP_SERVERS; count++) {
371 456 : if ((label >= multicast_label_start_[count]) &&
372 10 : (label <= multicast_label_end_[count])) return true;
373 : }
374 228 : return false;
375 : }
376 :
377 3 : void MplsTable::ReserveLabel(uint32_t start, uint32_t end) {
378 : // We want to allocate labels from an offset
379 : // Pre-allocate entries
380 19 : for (uint32_t i = start; i <= end; i++) {
381 16 : agent()->resource_manager()->ReserveIndex(Resource::MPLS_INDEX, i);
382 : }
383 3 : }
384 :
385 1 : void MplsTable::FreeReserveLabel(uint32_t start, uint32_t end) {
386 : // We want to allocate labels from an offset
387 : // Pre-allocate entries
388 17 : for (uint32_t i = start; i <= end; i++) {
389 16 : agent()->resource_manager()->ReleaseIndex(Resource::MPLS_INDEX, i);
390 : }
391 1 : }
392 :
393 2 : void MplsTable::ReserveMulticastLabel(uint32_t start, uint32_t end,
394 : uint8_t idx) {
395 2 : multicast_label_start_[idx] = start;
396 2 : multicast_label_end_[idx] = end;
397 2 : ReserveLabel(start, end);
398 2 : }
399 :
400 35 : MplsLabel *MplsTable::FindMplsLabel(uint32_t label) {
401 35 : MplsLabelKey key(label);
402 70 : return static_cast<MplsLabel *>(Find(&key, false));
403 35 : }
404 :
405 : // Allocate label for next-hop(interface, vrf, vlan)
406 74 : MplsLabel *MplsTable::AllocLabel(const NextHopKey *nh_key) {
407 74 : switch(nh_key->GetType()) {
408 74 : case NextHop::INTERFACE:
409 : case NextHop::VLAN:
410 : case NextHop::VRF:
411 74 : break;
412 0 : default:
413 0 : assert(0);
414 : }
415 :
416 : // Allocate label from resource manager
417 : ResourceManager::KeyPtr rkey(new NexthopIndexResourceKey(
418 74 : agent()->resource_manager(),
419 148 : nh_key->Clone()));
420 : uint32_t label = ((static_cast<IndexResourceData *>(agent()->resource_manager()->
421 74 : Allocate(rkey).get()))->index());
422 74 : assert(label != MplsTable::kInvalidLabel);
423 :
424 : // Add MplsLabel db entry
425 74 : DBRequest req;
426 74 : req.oper = DBRequest::DB_ENTRY_ADD_CHANGE;
427 :
428 74 : MplsLabelKey *key = new MplsLabelKey(label);
429 74 : req.key.reset(key);
430 :
431 74 : MplsLabelData *data = new MplsLabelData(nh_key->Clone());
432 74 : req.data.reset(data);
433 :
434 74 : agent()->mpls_table()->Process(req);
435 :
436 : // Return MplsLabel db entry for nh to hold reference
437 74 : MplsLabel *mpls_label = static_cast<MplsLabel *>
438 74 : (agent()->mpls_table()->FindActiveEntry(key));
439 74 : assert(mpls_label);
440 :
441 148 : return mpls_label;
442 : }
443 :
444 164 : void MplsTable::CheckVrLabelLimit() {
445 164 : VrLimitExceeded &vr_limits = agent()->get_vr_limits_exceeded_map();
446 164 : VrLimitExceeded::iterator vr_limit_itr = vr_limits.find("vr_mpls_labels");
447 164 : if (vr_limit_itr->second == "Normal") {
448 164 : if (label_table_.InUseIndexCount() >= ((agent()->vr_limit_high_watermark() *
449 164 : agent()->vrouter_max_labels())/100) ) {
450 0 : vr_limit_itr->second.assign(std::string("Exceeded"));
451 0 : LOG(ERROR, "Vrouter Mpls Labels Exceeded.");
452 : }
453 0 : } else if ( vr_limit_itr->second == "Exceeded") {
454 0 : if (label_table_.InUseIndexCount() >= agent()->vrouter_max_labels()) {
455 0 : vr_limit_itr->second.assign(std::string("TableLimit"));
456 0 : LOG(ERROR, "Vrouter Mpls Lablels Table Limit Reached. Skip Label Add.");
457 0 : } else if ( label_table_.InUseIndexCount() < ((agent()->vr_limit_low_watermark() *
458 0 : agent()->vrouter_max_labels())/100) ) {
459 0 : vr_limit_itr->second.assign(std::string("Normal"));
460 : }
461 0 : } else if ( vr_limit_itr->second == "TableLimit" ) {
462 0 : if (label_table_.InUseIndexCount() <
463 0 : ((agent()->vrouter_max_labels()*95)/100) ) {
464 0 : vr_limit_itr->second.assign(std::string("Exceeded"));
465 0 : LOG(ERROR, "Vrouter Mpls Labels Exceeded.");
466 : }
467 : }
468 164 : agent()->set_vr_limits_exceeded_map(vr_limits);
469 164 : }
470 :
471 0 : AgentSandeshPtr MplsTable::GetAgentSandesh(const AgentSandeshArguments *args,
472 : const std::string &context) {
473 : return AgentSandeshPtr(new AgentMplsSandesh
474 0 : (context, args->GetString("type"),
475 0 : args->GetString("label")));
476 : }
477 :
478 0 : void MplsReq::HandleRequest() const {
479 0 : AgentSandeshPtr sand(new AgentMplsSandesh(context(), get_type(),
480 0 : get_label()));
481 0 : sand->DoSandesh(sand);
482 0 : }
|