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