OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
mac_learning.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 Juniper Networks, Inc. All rights reserved.
3  */
4 
5 #ifndef SRC_VNSW_AGENT_MAC_LEARNING_MAC_LEARNING_H_
6 #define SRC_VNSW_AGENT_MAC_LEARNING_MAC_LEARNING_H_
7 
8 #include "cmn/agent.h"
9 #include "mac_learning_key.h"
10 #include "mac_learning_base.h"
11 #include "mac_learning_event.h"
12 #include "pkt/flow_token.h"
13 class MacEntryResp;
14 /*
15  * High level mac learning modules
16  *
17  * MacLearningProto:
18  * Processes the packet trapped for MAC learning, based on the hash
19  * of the VRF and MAC particular MAC Learning parition would be chosen
20  * for further processing
21  *
22  * MacLearningPartition:
23  * A logical context for processing MAC learning requests in parallel
24  * This modules learns the MAC entry and
25  * 1> Enqueues a request for route add
26  * 2> Enqueues a request to MacLearningMgmt for dependency tracking
27  * 3> Enqueues a request to MacAgingPartiton for aging
28  *
29  * MacLearningMgmt:
30  * This modules builds a dependency tracking for a MAC entry.
31  * MAC entry would be dependent on interface, VRF and
32  * a route(in case of PBB tunnel learnt MAC). So given a DB object
33  * it would have a list of all the MAC learnt on that object so that
34  * any change can result in resync of MAC entry.
35  *
36  * MacLearningDBClient:
37  * Listens for change on DB object and enqueues a request to MacLearningMgmt
38  * for revaluation of MAC entry depenedent on this DB object.
39  * Current DB tables of intereset inclue:
40  * 1> Interface (For local learnt mac address)
41  * 2> Route (For mac learnt over PBB)
42  * 3> VRF
43  *
44  * MacAgingPatition:
45  * For each MacLearningPartition there will be MacAgingPartition, which
46  * maintains a per VRF list of MAC entries, upon timer expiry based on
47  * aging timeout configured on VRF and no. of entries in VRF no. of entries
48  * would be visited for stats and aged if no activity is seen on the entry.
49  *
50  * ++++++++++++++++++++
51  * + Mac Aging X +
52  * ++++++++++++++++++++
53  * |
54  * |
55  * |
56  * ++++++++++++++++++++++++
57  * -->+ MacLearningPartitionX+--|
58  * | ++++++++++++++++++++++++ |
59  * +++++++++++++++ | | N:1 +++++++++++++++++++
60  * +Packet Proto +-->| |<--->+ MacLearningMgmt +
61  * +++++++++++++++ | | +++++++++++++++++++
62  * | ++++++++++++++++++++++++ | ^
63  * -->+ MacLearningPartitionY+--| |
64  * ++++++++++++++++++++++++ |
65  * | ++++++++++++++++
66  * |1:1 + MacLearning +
67  * | + DB Client +
68  * ++++++++++++++++++++ ++++++++++++++++
69  * + Mac Aging Y +
70  * ++++++++++++++++++++
71  */
72 
74 class MacAgingTable;
75 class MacAgingPartition;
76 
78 public:
79  typedef std::vector<TokenPtr> TokenList;
81  const MacAddress &mac, uint32_t index);
82  virtual ~MacPbbLearningEntry() {}
83  virtual bool Add() = 0;
84  virtual void Delete();
85  virtual void Resync();
86  virtual void AddWithToken();
87 
89  return mac_learning_table_;
90  }
91 
92  uint32_t index() const {
93  return index_;
94  }
95 
96  const MacAddress& mac() const {
97  return key_.mac_;
98  }
99 
100  uint32_t vrf_id() {
101  return key_.vrf_id_;
102  }
103 
104  const MacLearningKey& key() const {
105  return key_;
106  }
107 
108  void AddToken(TokenPtr ptr) {
109  tbb::mutex::scoped_lock lock(mutex_);
110  list_.push_back(ptr);
111  }
112 
113  void ReleaseToken() {
114  tbb::mutex::scoped_lock lock(mutex_);
115  list_.clear();
116  }
117 
119  MacPbbLearningEntry *entry1 =
120  dynamic_cast<MacPbbLearningEntry *>(entry);
121  tbb::mutex::scoped_lock lock(mutex_);
122  tbb::mutex::scoped_lock lock2(entry1->mutex_);
123  list_ = entry1->list_;
124  entry1->list_.clear();
125  }
126 
127  bool HasTokens() {
128  tbb::mutex::scoped_lock lock(mutex_);
129  return list_.size();
130  }
132 
133 protected:
136  uint32_t index_;
137  uint32_t ethernet_tag_;
139  tbb::mutex mutex_;
140 private:
142 };
143 //Structure used to hold MAC entry learned
144 //on local interface
146 public:
148  const MacAddress &mac, uint32_t index,
151 
152  bool Add();
153 
154  const Interface* intf() {
155  return intf_.get();
156  }
157 
158 private:
161 };
162 
163 //Structure used to hold MAC entry learned on
164 //tunnel packet i.e vxlan scenario for now
166 public:
168  const MacAddress &mac, uint32_t index,
169  const IpAddress remote_ip);
171 
172  bool Add();
173 
174  const IpAddress& remote_ip() const {
175  return remote_ip_;
176  }
177 private:
180 };
181 
182 //Structure used to hold MAC entry learned on
183 //PBB tunnel packet
185 public:
187  const MacAddress &mac, uint32_t index,
188  const MacAddress &bmac);
189  virtual ~MacLearningEntryPBB() {}
190 
191  bool Add();
192 
193  const MacAddress& bmac() const {
194  return bmac_;
195  }
196 
197 private:
200 };
201 
202 //Mac learning Parition holds all the mac entries hashed
203 //based on VRF + MAC, and corresponding to each partition
204 //there will be a aging partition holding all the MAC entries
205 //present in this partiton
207 public:
208  typedef std::pair<MacLearningKey,
210  typedef std::map<MacLearningKey,
213 
215  uint32_t id);
216  virtual ~MacLearningPartition();
217  void Add(MacLearningEntryPtr ptr);
218  void Resync(MacLearningEntryPtr ptr);
219  void Delete(MacLearningEntryPtr ptr);
220  void DeleteAll();
221  void ReleaseToken(const MacLearningKey &key);
222  MacLearningEntry* Find(const MacLearningKey &key);
223  //To be used in test cases only
226 
228  return agent_;
229  }
230 
232  return aging_partition_.get();
233  }
234 
235  uint32_t id() const {
236  return id_;
237  }
238 
240  void EnqueueMgmtReq(MacLearningEntryPtr ptr, bool add);
241  void MayBeStartRunner(TokenPool *pool);
242 
243  void SetDeleteQueueDisable(bool disable) {
245  }
246 
247 private:
250  uint32_t id_;
255  boost::shared_ptr<MacAgingPartition> aging_partition_;
257 };
258 
259 class MacLearningSandeshResp : public Task {
260 public:
261  const static uint32_t kMaxResponse = 100;
262  const static char kDelimiter = '-';
263  MacLearningSandeshResp(Agent *agent, MacEntryResp *resp,
264  std::string resp_ctx,
265  std::string key,
266  const MacAddress &mac);
267  virtual ~MacLearningSandeshResp();
268  std::string Description() const { return "MacLearningSandeshRespTask"; }
269 
270 private:
271  bool Run();
272  bool SetMacKey(string key);
273  void SendResponse(SandeshResponse *resp);
274 
276  std::string GetMacKey();
277 
279  MacEntryResp *resp_;
280  std::string resp_data_;
281  uint32_t partition_id_;
282  uint32_t vrf_id_;
287 };
288 #endif
virtual ~MacLearningPartition()
void Add(MacLearningEntryPtr ptr)
MacLearningEntryRemote(MacLearningPartition *table, uint32_t vrf_id, const MacAddress &mac, uint32_t index, const IpAddress remote_ip)
Definition: mac_learning.cc:96
MacEntryResp * resp_
Definition: mac_learning.h:279
std::map< MacLearningKey, MacLearningEntryPtr, MacLearningKeyCmp > MacLearningEntryTable
Definition: mac_learning.h:212
void AddToken(TokenPtr ptr)
Definition: mac_learning.h:108
uint32_t index() const
Definition: mac_learning.h:92
const MacAddress & mac() const
Definition: mac_learning.h:96
virtual ~MacLearningEntryLocal()
Definition: mac_learning.h:150
MacAgingPartition * aging_partition() const
Definition: mac_learning.h:231
const IpAddress & remote_ip() const
Definition: mac_learning.h:174
void SetDeleteQueueDisable(bool disable)
Definition: mac_learning.h:243
DISALLOW_COPY_AND_ASSIGN(MacLearningEntryRemote)
const MacLearningKey & key() const
Definition: mac_learning.h:104
MacLearningRequestQueue change_request_queue_
Definition: mac_learning.h:253
const MacAddress bmac_
Definition: mac_learning.h:198
DISALLOW_COPY_AND_ASSIGN(MacLearningSandeshResp)
virtual ~MacLearningSandeshResp()
uint32_t id() const
Definition: mac_learning.h:235
boost::asio::ip::address IpAddress
Definition: address.h:13
MacAddress user_given_mac_
Definition: mac_learning.h:285
void CopyToken(MacLearningEntry *entry)
Definition: mac_learning.h:118
DISALLOW_COPY_AND_ASSIGN(MacLearningEntryLocal)
DISALLOW_COPY_AND_ASSIGN(MacLearningEntryPBB)
std::string GetMacKey()
MacLearningRequestQueue delete_request_queue_
Definition: mac_learning.h:254
void EnqueueMgmtReq(MacLearningEntryPtr ptr, bool add)
boost::shared_ptr< MacAgingPartition > aging_partition_
Definition: mac_learning.h:255
virtual ~MacPbbLearningEntry()
Definition: mac_learning.h:82
DISALLOW_COPY_AND_ASSIGN(MacPbbLearningEntry)
DISALLOW_COPY_AND_ASSIGN(MacLearningPartition)
virtual void AddWithToken()
Definition: mac_learning.cc:38
std::string Description() const
Definition: mac_learning.h:268
bool RequestHandler(MacLearningEntryRequestPtr ptr)
const MacLearningPartition * GetPartition()
virtual void Resync()
Definition: mac_learning.cc:45
Definition: agent.h:358
MacLearningSandeshResp(Agent *agent, MacEntryResp *resp, std::string resp_ctx, std::string key, const MacAddress &mac)
MacLearningPartition * mac_learning_table_
Definition: mac_learning.h:134
void MayBeStartRunner(TokenPool *pool)
MacLearningEntryPBB(MacLearningPartition *table, uint32_t vrf_id, const MacAddress &mac, uint32_t index, const MacAddress &bmac)
bool Run()
Code to execute. Returns true if task is completed. Return false to reschedule the task...
bool SetMacKey(string key)
MacLearningKey key_
Definition: mac_learning.h:135
std::pair< MacLearningKey, MacLearningEntryPtr > MacLearningEntryPair
Definition: mac_learning.h:209
boost::intrusive_ptr< const Interface > InterfaceConstRef
Definition: agent.h:51
boost::shared_ptr< MacLearningEntryRequest > MacLearningEntryRequestPtr
void ReleaseToken(const MacLearningKey &key)
static const char kDelimiter
Definition: mac_learning.h:262
MacLearningPartition(Agent *agent, MacLearningProto *proto, uint32_t id)
MacPbbLearningEntry(MacLearningPartition *table, uint32_t vrf_id, const MacAddress &mac, uint32_t index)
Definition: mac_learning.cc:20
virtual void Delete()
Definition: mac_learning.cc:28
MacLearningPartition * mac_learning_table() const
Definition: mac_learning.h:88
std::vector< TokenPtr > TokenList
Definition: mac_learning.h:79
MacLearningEntryLocal(MacLearningPartition *table, uint32_t vrf_id, const MacAddress &mac, uint32_t index, InterfaceConstRef intf)
Definition: mac_learning.cc:55
void Delete(MacLearningEntryPtr ptr)
void EnqueueToTable(MacLearningEntryRequestPtr req)
Definition: mac_learning.cc:52
const Interface * intf()
Definition: mac_learning.h:154
void SendResponse(SandeshResponse *resp)
virtual ~MacLearningEntryPBB()
Definition: mac_learning.h:189
virtual bool Add()=0
void Resync(MacLearningEntryPtr ptr)
const uint32_t vrf_id_
void SetQueueDisable(bool disable)
static const uint32_t kMaxResponse
Definition: mac_learning.h:261
boost::shared_ptr< Token > TokenPtr
Definition: flow_token.h:11
MacLearningEntryTable mac_learning_table_
Definition: mac_learning.h:251
virtual ~MacLearningEntryRemote()
Definition: mac_learning.h:170
MacLearningRequestQueue add_request_queue_
Definition: mac_learning.h:252
Task is a wrapper over tbb::task to support policies.
Definition: task.h:86
void Enqueue(MacLearningEntryRequestPtr req)
const MacAddress mac_
const MacAddress & bmac() const
Definition: mac_learning.h:193
MacLearningEntry * Find(const MacLearningKey &key)
boost::shared_ptr< MacLearningEntry > MacLearningEntryPtr
InterfaceConstRef intf_
Definition: mac_learning.h:159
MacLearningEntryPtr TestGet(const MacLearningKey &key)