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