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