OpenSDN source code
mac_learning.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 Juniper Networks, Inc. All rights reserved.
3  * Copyright Contributors to the OpenSDN project
4  */
5 #include <init/agent_param.h>
6 #include <oper/vn.h>
7 #include <oper/sg.h>
8 #include <oper/vrf.h>
9 #include <oper/nexthop.h>
10 #include <oper/interface_common.h>
11 #include <oper/route_common.h>
12 #include "mac_learning_proto.h"
14 #include "mac_learning_init.h"
15 #include "mac_learning.h"
16 #include "mac_learning_mgmt.h"
17 #include "mac_aging.h"
18 #include "vr_bridge.h"
19 
20 
22  const MacAddress &mac,
23  uint32_t index):
24  MacLearningEntry(vrf_id),mac_learning_table_(table), key_(vrf_id, mac), index_(index) {
25  vrf_ = table->agent()->vrf_table()->
26  FindVrfFromIdIncludingDeletedVrf(key_.vrf_id_);
27 }
28 
30  deleted_ = true;
33 
36  vrf()->GetName(), mac(), Ip4Address(0), 32, 0, NULL);
37 }
38 
40  if (Add()) {
43  }
44 }
45 
47  if (Add()) {
50  }
51 }
52 
55 }
57  uint32_t vrf_id,
58  const MacAddress &mac,
59  uint32_t index,
60  InterfaceConstRef intf):
61  MacPbbLearningEntry(table, vrf_id, mac, index), intf_(intf) {
62 }
63 
65  const VmInterface *vm_intf = dynamic_cast<const VmInterface *>(intf_.get());
66  assert(vm_intf);
67 
68  if (vrf()->IsActive() == false) {
69  return false;
70  }
71 
72  if (vm_intf->vn() == NULL) {
73  return false;
74  }
75 
76  if (vm_intf->vn() == NULL) {
77  return false;
78  }
79 
80  SecurityGroupList sg_list;
81  vm_intf->CopySgIdList(&sg_list);
82 
83  TagList tag_list;
84  vm_intf->CopyTagIdList(&tag_list);
85 
87  AddLocalVmRouteReq(mac_learning_table_->agent()->mac_learning_peer(),
88  vrf()->GetName(),
89  mac(), vm_intf, Ip4Address(0),
90  vm_intf->l2_label(),
91  vm_intf->vn()->GetName(), sg_list, tag_list,
92  PathPreference(), 0,
93  vm_intf->etree_leaf());
94  return true;
95 }
96 
98  uint32_t vrf_id,
99  const MacAddress &mac,
100  uint32_t index,
101  IpAddress remote_ip):
102  MacPbbLearningEntry(table, vrf_id, mac, index), remote_ip_(remote_ip) {
103 }
104 
106  return true;
107 }
108 
110  uint32_t vrf_id,
111  const MacAddress &mac,
112  uint32_t index,
113  const MacAddress &bmac) :
114  MacPbbLearningEntry(table, vrf_id, mac, index), bmac_(bmac) {
115 }
116 
118  assert(vrf_);
119  uint32_t isid = vrf_->isid();
120  std::string bmac_vrf_name = vrf_->bmac_vrf_name();
121  PBBRoute *data = new PBBRoute(VrfKey(bmac_vrf_name), bmac_, isid,
122  VnListType(),
125  mac_learning_peer(),
126  vrf()->GetName(),
127  mac(), Ip4Address(0), 32, 0, data);
128  return true;
129 }
130 
132  TokenPool *pool):
133  partition_(partition), pool_(pool),
134  queue_(partition_->agent()->task_scheduler()->GetTaskId(kTaskMacLearning),
135  partition->id(),
136  boost::bind(&MacLearningRequestQueue::HandleEvent, this, _1)) {
138  this));
139 }
140 
142  if (pool_) {
143  return pool_->TokenCheck();
144  }
145 
146  return true;
147 }
148 
150  return partition_->RequestHandler(ptr);
151 }
152 
154  MacLearningProto *proto,
155  uint32_t id):
156  agent_(agent), id_(id),
157  add_request_queue_(this, proto->add_tokens()),
158  change_request_queue_(this, proto->change_tokens()),
159  delete_request_queue_(this, proto->delete_tokens()) {
160  aging_partition_.reset(new MacAgingPartition(agent, id));
161 }
162 
164  assert(mac_learning_table_.size() == 0);
165 }
166 
168  if (agent_->mac_learning_proto()->add_tokens() == pool) {
170  } else if (agent_->mac_learning_proto()->change_tokens() == pool) {
172  } else {
174  }
175 }
176 
178  switch(ptr->event()) {
180  agent()->mac_learning_proto()->ProcessProto(ptr->pkt_info());
181  break;
182 
184  Add(ptr->mac_learning_entry());
185  break;
186 
188  Resync(ptr->mac_learning_entry());
189  break;
190 
192  Delete(ptr->mac_learning_entry());
193  break;
194 
197  mac_learning_db_client()->FreeDBState(ptr->db_entry(),
198  ptr->gen_id());
199  break;
200 
201  default:
202  assert(0);
203  }
204  return true;
205 }
206 
209  if (add == false) {
211  }
214 }
215 
217  MacPbbLearningEntry *entry = dynamic_cast<MacPbbLearningEntry *>(ptr.get());
218  if (!entry) {
219  return;
220  }
221  MacLearningKey key(ptr->vrf_id(), entry->mac());
222 
223  std::pair<MacLearningEntryTable::iterator, bool> it =
224  mac_learning_table_.insert(MacLearningEntryPair(key, ptr));
225  if (it.second == false) {
226  //Entry already present, clear the entry and delete it from
227  //aging tree
228  ptr->CopyToken(it.first->second.get());
229  if (it.first->second->deleted() == false) {
231  MacLearningEntryRequest::DELETE_MAC, it.first->second));
232  aging_partition_->Enqueue(aging_req);
233  }
234  mac_learning_table_[key] = ptr;
235  }
236 
237  ptr->AddWithToken();
238  EnqueueMgmtReq(ptr, true);
241  aging_partition_->Enqueue(aging_req);
242 }
243 
245  if (ptr->deleted() == true) {
246  return;
247  }
248  MacPbbLearningEntry *entry = dynamic_cast<MacPbbLearningEntry *>(ptr.get());
249  if (!entry) {
250  return;
251  }
252  MacLearningKey key(ptr->vrf_id(), entry->mac());
253  if (mac_learning_table_.find(key) == mac_learning_table_.end()) {
254  return;
255  }
256 
257  ptr->Delete();
260  ptr));
261  aging_partition_->Enqueue(aging_req);
262  EnqueueMgmtReq(ptr, false);
263 }
264 
266  MacPbbLearningEntry *entry = dynamic_cast<MacPbbLearningEntry *>(ptr.get());
267  if (!entry) {
268  return;
269  }
270  MacLearningKey key(ptr->vrf_id(), entry->mac());
271  if (mac_learning_table_.find(key) == mac_learning_table_.end()) {
272  return;
273  }
274 
275  ptr->Resync();
276 }
277 
279  switch(req->event()) {
285  break;
286 
289  break;
290 
293  break;
294 
295  default:
296  assert(0);
297  }
298  return;
299 }
300 
303  MacLearningEntryTable::iterator it = mac_learning_table_.find(key);
304  if (it == mac_learning_table_.end()) {
305  return NULL;
306  }
307  return it->second.get();
308 }
309 
312  MacLearningEntryTable::iterator it = mac_learning_table_.find(key);
313  return it->second;
314 }
315 
317  MacLearningEntry *mac_entry = Find(key);
318  if (mac_entry) {
319  mac_entry->ReleaseToken();
320  if (mac_entry->deleted()) {
321  mac_learning_table_.erase(key);
322  }
323  }
324 }
325 
327  MacEntryResp *resp,
328  std::string resp_ctx,
329  std::string key,
330  const MacAddress &mac) :
331  Task((TaskScheduler::GetInstance()->
332  GetTaskId("Agent::MacLearningSandeshTask")), 0),
333  agent_(agent), resp_(resp), resp_data_(resp_ctx), partition_id_(0),
334  vrf_id_(0), mac_(MacAddress::ZeroMac()), exact_match_(false),
335  user_given_mac_(mac) {
336  if (key != agent_->NullString()) {
337  SetMacKey(key);
338  }
339 }
340 
342 }
343 
345  const char ch = kDelimiter;
346  size_t n = std::count(key.begin(), key.end(), ch);
347  if (n != 3) {
348  return false;
349  }
350 
351  std::stringstream ss(key);
352  string item;
353 
354  if (getline(ss, item, ch)) {
355  std::istringstream(item) >> partition_id_;
356  }
357  if (getline(ss, item, ch)) {
358  std::istringstream(item) >> vrf_id_;
359  }
360  if (getline(ss, item, ch)) {
362  }
363  if (getline(ss, item, ch)) {
364  std::istringstream(item) >> exact_match_;
365  }
366  return true;
367 }
368 
369 string
371  std::stringstream ss;
372  ss << partition_id_ << kDelimiter;
373  ss << vrf_id_ << kDelimiter;
374  ss << mac_.ToString();
375  ss << kDelimiter << exact_match_;
376  return ss.str();
377 }
378 
382  return NULL;
383  }
384 
386  Find(partition_id_);
387  return mp;
388 }
389 
390 bool
392  std::vector<SandeshMacEntry>& list =
393  const_cast<std::vector<SandeshMacEntry>&>(resp_->get_mac_entry_list());
394  uint32_t entries_count = 0;
395 
396  while (entries_count < kMaxResponse) {
397  const MacLearningPartition *mp = GetPartition();
398  if (mp == NULL) {
399  break;
400  }
401 
403  MacLearningPartition::MacLearningEntryTable::const_iterator it;
405  it = mp->mac_learning_table_.find(key);
406  } else {
407  it = mp->mac_learning_table_.upper_bound(key);
408  }
409 
410  while (entries_count < kMaxResponse) {
411  if (exact_match_ && it->first.vrf_id_ != vrf_id_) {
412  break;
413  } else {
414  vrf_id_ = it->first.vrf_id_;
415  }
416 
417  if (it == mp->mac_learning_table_.end()) {
418  break;
419  }
420 
421  const MacAgingTable *at =
422  mp->aging_partition()->Find(it->first.vrf_id_);
423  //Find the aging entry
424  const MacAgingEntry *aging_entry = NULL;
425  if (at) {
426  aging_entry = at->Find(it->second.get());
427  }
428  if (aging_entry) {
429  SandeshMacEntry data;
430  data.set_partition(partition_id_);
431  aging_entry->FillSandesh(&data);
432  list.push_back(data);
433  }
434 
435  entries_count++;
437  break;
438  }
439  mac_ = it->first.mac_;
440  it++;
441  }
442 
443  if (entries_count >= kMaxResponse) {
444  break;
445  }
446 
447  if (exact_match_ == false) {
448  vrf_id_++;
449  }
450 
451  if (it == mp->mac_learning_table_.end()) {
452  partition_id_++;
453  if (exact_match_ == false) {
454  vrf_id_ = 0;
455  }
456  }
457 
459  //If entry is found in current partition
460  //move on to next partition
461  if (it != mp->mac_learning_table_.end()) {
462  partition_id_++;
463  }
465  } else {
467  }
468  }
469 
470  if (partition_id_ < agent_->params()->mac_learning_thread_count()) {
471  resp_->set_mac_key(GetMacKey());
472  }
473 
475  return true;
476 }
477 
479  resp->set_context(resp_data_);
480  resp->set_more(false);
481  resp->Response();
482 }
483 
484 void FetchMacEntry::HandleRequest() const {
485  Agent *agent = Agent::GetInstance();
486 
487  std::ostringstream str;
488  str << "0" << MacLearningSandeshResp::kDelimiter << get_vrf_id() <<
490 
491  bool exact_match = false;
492  if (get_vrf_id() != 0 || get_mac() != agent->NullString()) {
493  exact_match = true;
494  }
495  str << MacLearningSandeshResp::kDelimiter << exact_match;
496 
497  MacAddress mac = MacAddress::FromString(get_mac());
498 
499  MacEntryResp *resp = new MacEntryResp();
501  context(),
502  str.str(), mac);
504  scheduler->Enqueue(task);
505 }
506 
507 void NextMacEntrySet::HandleRequest() const {
508  Agent *agent = Agent::GetInstance();
509 
510  MacEntryResp *resp = new MacEntryResp();
512  context(),
513  get_mac_entry_key(),
516  scheduler->Enqueue(task);
517 }
boost::asio::ip::address IpAddress
Definition: address.h:14
boost::asio::ip::address_v4 Ip4Address
Definition: address.h:15
boost::intrusive_ptr< const Interface > InterfaceConstRef
Definition: agent.h:52
std::vector< uint64_t > TagList
Definition: agent.h:203
std::vector< int > SecurityGroupList
Definition: agent.h:202
std::set< std::string > VnListType
Definition: agent.h:213
#define kTaskMacLearning
Definition: agent.h:344
uint32_t mac_learning_thread_count() const
Definition: agent_param.h:533
Definition: agent.h:361
MacLearningModule * mac_learning_module() const
Definition: agent.h:1016
AgentParam * params() const
Definition: agent.h:1227
MacLearningProto * mac_learning_proto() const
Definition: agent.h:1008
VrfTable * vrf_table() const
Definition: agent.h:488
const Peer * mac_learning_peer() const
Definition: agent.h:1039
static const std::string & NullString()
Definition: agent.h:440
EvpnAgentRouteTable * fabric_evpn_table() const
Definition: agent.h:614
static Agent * GetInstance()
Definition: agent.h:439
static void AddRemoteVmRouteReq(const Peer *peer, const std::string &vrf_name, const MacAddress &mac, const IpAddress &ip_addr, uint32_t plen, uint32_t ethernet_tag, AgentRouteData *data)
uint32_t l2_label() const
Definition: interface.h:131
std::string ToString() const
Definition: mac_address.cc:54
static MacAddress FromString(const std::string &str, boost::system::error_code *error=NULL)
Definition: mac_address.cc:72
static const MacAddress & ZeroMac()
Definition: mac_address.h:159
void FillSandesh(SandeshMacEntry *sme) const
Definition: mac_aging.cc:28
MacAgingTable * Find(uint32_t id)
Definition: mac_aging.h:128
const MacAgingEntry * Find(MacLearningEntry *me) const
Definition: mac_aging.h:85
MacLearningEntryLocal(MacLearningPartition *table, uint32_t vrf_id, const MacAddress &mac, uint32_t index, InterfaceConstRef intf)
Definition: mac_learning.cc:56
InterfaceConstRef intf_
Definition: mac_learning.h:162
MacLearningEntryPBB(MacLearningPartition *table, uint32_t vrf_id, const MacAddress &mac, uint32_t index, const MacAddress &bmac)
const MacAddress bmac_
Definition: mac_learning.h:201
MacLearningEntryRemote(MacLearningPartition *table, uint32_t vrf_id, const MacAddress &mac, uint32_t index, const IpAddress remote_ip)
Definition: mac_learning.cc:97
bool deleted() const
VrfEntry * vrf() const
virtual void ReleaseToken()
void Enqueue(MacLearningMgmtRequestPtr &ptr)
MacLearningMgmtManager * mac_learning_mgmt() const
void Resync(MacLearningEntryPtr ptr)
MacLearningEntry * Find(const MacLearningKey &key)
virtual ~MacLearningPartition()
void ReleaseToken(const MacLearningKey &key)
MacLearningEntryPtr TestGet(const MacLearningKey &key)
MacLearningRequestQueue add_request_queue_
Definition: mac_learning.h:255
MacAgingPartition * aging_partition() const
Definition: mac_learning.h:234
void EnqueueMgmtReq(MacLearningEntryPtr ptr, bool add)
std::pair< MacLearningKey, MacLearningEntryPtr > MacLearningEntryPair
Definition: mac_learning.h:212
void Add(MacLearningEntryPtr ptr)
MacLearningPartition(Agent *agent, MacLearningProto *proto, uint32_t id)
MacLearningRequestQueue change_request_queue_
Definition: mac_learning.h:256
void Delete(MacLearningEntryPtr ptr)
MacLearningEntryTable mac_learning_table_
Definition: mac_learning.h:254
void Enqueue(MacLearningEntryRequestPtr req)
void MayBeStartRunner(TokenPool *pool)
boost::shared_ptr< MacAgingPartition > aging_partition_
Definition: mac_learning.h:258
MacLearningRequestQueue delete_request_queue_
Definition: mac_learning.h:257
bool RequestHandler(MacLearningEntryRequestPtr ptr)
TokenPool * change_tokens()
TokenPool * add_tokens()
MacLearningRequestQueue(MacLearningPartition *partition, TokenPool *pool)
MacLearningPartition * partition_
void Enqueue(MacLearningEntryRequestPtr ptr)
virtual bool HandleEvent(MacLearningEntryRequestPtr ptr)
virtual bool TokenCheck()
static const uint32_t kMaxResponse
Definition: mac_learning.h:264
static const char kDelimiter
Definition: mac_learning.h:265
void SendResponse(SandeshResponse *resp)
MacEntryResp * resp_
Definition: mac_learning.h:282
bool SetMacKey(string key)
std::string GetMacKey()
MacLearningSandeshResp(Agent *agent, MacEntryResp *resp, std::string resp_ctx, std::string key, const MacAddress &mac)
MacAddress user_given_mac_
Definition: mac_learning.h:288
bool Run()
Code to execute in a task. Returns true if task is completed. Return false to reschedule the task.
const MacLearningPartition * GetPartition()
virtual ~MacLearningSandeshResp()
MacLearningPartition * mac_learning_table_
Definition: mac_learning.h:137
virtual bool Add()=0
void EnqueueToTable(MacLearningEntryRequestPtr req)
Definition: mac_learning.cc:53
virtual void Delete()
Definition: mac_learning.cc:29
MacPbbLearningEntry(MacLearningPartition *table, uint32_t vrf_id, const MacAddress &mac, uint32_t index)
Definition: mac_learning.cc:21
MacLearningKey key_
Definition: mac_learning.h:138
void AddToken(TokenPtr ptr)
Definition: mac_learning.h:111
const MacAddress & mac() const
Definition: mac_learning.h:99
virtual void Resync()
Definition: mac_learning.cc:46
virtual void AddWithToken()
Definition: mac_learning.cc:39
bool ProcessProto(boost::shared_ptr< PktInfo > msg_info)
virtual void Response()
Definition: cpp/sandesh.h:505
virtual void set_more(const bool val)=0
void set_context(std::string context)
Definition: cpp/sandesh.h:313
The TaskScheduler keeps track of what tasks are currently schedulable. When a task is enqueued it is ...
Definition: task.h:307
void Enqueue(Task *task)
Enqueues a task for running. Starts task if all policy rules are met else puts task in waitq....
Definition: task.cc:662
static TaskScheduler * GetInstance()
Definition: task.cc:574
Task is a class to describe a computational task within OpenSDN control plane applications....
Definition: task.h:82
bool TokenCheck() const
Definition: flow_token.cc:32
bool etree_leaf() const
void CopyTagIdList(TagList *tag_id_list) const
const VnEntry * vn() const
void CopySgIdList(SecurityGroupList *sg_id_list) const
const string & GetName() const
Definition: vn.h:163
void SetStartRunnerFunc(StartRunnerFunc start_runner_fn)
Definition: queue_task.h:193
boost::shared_ptr< MacLearningEntry > MacLearningEntryPtr
boost::shared_ptr< MacLearningEntryRequest > MacLearningEntryRequestPtr
boost::shared_ptr< MacLearningMgmtRequest > MacLearningMgmtRequestPtr
const uint32_t vrf_id_
Definition: vrf.h:26
Definition: task_int.h:11