OpenSDN source code
mac_ip_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_ip_learning.h"
16 #include "mac_learning_mgmt.h"
17 #include "mac_learning_event.h"
18 
19 
21  agent_(agent),
22  work_queue_(this), macip_map_mutex_() {
24  boost::bind(&MacIpLearningTable::MacIpEntryHcNotify, this, _1));
25 }
26 
28  switch(ptr->event()) {
30  agent()->mac_learning_proto()->ProcessProto(ptr->pkt_info());
31  break;
32 
34  Add(ptr->mac_learning_entry());
35  break;
36 
38  Resync(ptr->mac_learning_entry());
39  break;
40 
42  Delete(ptr->mac_learning_entry());
43  break;
45  DetectIpMove(ptr);
46  break;
47 
50  break;
51 
52  default:
53  assert(0);
54  }
55  return true;
56 }
59  if (add == false) {
61  }
64 }
66  MacIpLearningEntry *entry = dynamic_cast<MacIpLearningEntry *>(ptr.get());
67  MacIpLearningKey key(entry->vrf_id(), entry->IpAddr());
68  //check whether IP belongs to subnet of VN
69  const VmInterface *vm_intf =
70  dynamic_cast<const VmInterface *>(entry->intf());
71  if (!vm_intf ||
72  !vm_intf->vn()->GetIpam(entry->IpAddr())) {
73  MAC_IP_LEARNING_TRACE(MacLearningTraceBuf, entry->vrf()->GetName(),
74  entry->IpAddr().to_string(), entry->Mac().ToString(),
75  entry->intf()->name(),
76  "ip address does not belong to same subnet, ignoring");
77  return;
78  }
79  std::scoped_lock lock(macip_map_mutex_);
80  MacIpLearningEntryMap::iterator it = mac_ip_learning_entry_map_.find(key);
81  if (it != mac_ip_learning_entry_map_.end()) {
82  MacIpLearningEntry *existing_entry =
83  dynamic_cast<MacIpLearningEntry *>(it->second.get());
84  if ( existing_entry && existing_entry->Mac() == entry->Mac()) {
85  // ignore duplicate add requests, it is possible that
86  // duplicate requests may come till route processing is done.
87  MAC_IP_LEARNING_TRACE(MacLearningTraceBuf, entry->vrf()->GetName(),
88  entry->IpAddr().to_string(), entry->Mac().ToString(),
89  entry->intf()->name(),
90  "duplicate add request, ingoring");
91  return;
92  }
93  MAC_IP_LEARNING_TRACE(MacLearningTraceBuf, entry->vrf()->GetName(),
94  entry->IpAddr().to_string(), entry->Mac().ToString(),
95  entry->intf()->name(),
96  "local IP move detected, delete and add with new mac");
97  //Entry already present, clear the entry
98  if (it->second->deleted() == false) {
99  it->second->Delete();
100  EnqueueMgmtReq(it->second, false);
101  }
102  mac_ip_learning_entry_map_[key] = ptr;
103  } else {
104  // check whether mac limit reached for learning new mac-ip
105  // on the interface
106  if (vm_intf->IsMaxMacIpLearnt()) {
107  MAC_IP_LEARNING_TRACE(MacLearningTraceBuf, entry->vrf()->GetName(),
108  entry->IpAddr().to_string(), entry->Mac().ToString(),
109  entry->intf()->name(),
110  "max mac ip learnt limit reached on interface");
111  return;
112  }
113 
115  }
116  ptr->Add();
117  EnqueueMgmtReq(ptr, true);
118 }
119 
121  if (ptr->deleted() == true) {
122  return;
123  }
124  MacIpLearningEntry *entry = dynamic_cast<MacIpLearningEntry *>(ptr.get());
125  if (entry == NULL) {
126  return;
127  }
128 
129  std::scoped_lock lock(macip_map_mutex_);
130  MacIpLearningKey key(ptr->vrf_id(), entry->IpAddr());
132  return;
133  }
134  MAC_IP_LEARNING_TRACE(MacLearningTraceBuf, entry->vrf()->GetName(),
135  entry->IpAddr().to_string(), entry->Mac().ToString(),
136  entry->intf()->name(), "Delete");
137 
138  ptr->Delete();
139  mac_ip_learning_entry_map_.erase(key);
140  EnqueueMgmtReq(ptr, false);
141 }
142 
144  MacIpLearningEntry *entry = dynamic_cast<MacIpLearningEntry *>(ptr.get());
145  if (entry == NULL) {
146  return;
147  }
148  MacIpLearningKey key(ptr->vrf_id(), entry->IpAddr());
149  std::scoped_lock lock(macip_map_mutex_);
151  return;
152  }
153 
154  MAC_IP_LEARNING_TRACE(MacLearningTraceBuf, entry->vrf()->GetName(),
155  entry->IpAddr().to_string(), entry->Mac().ToString(),
156  entry->intf()->name(), "Resync");
157  ptr->Resync();
158  EnqueueMgmtReq(ptr, true);
159 }
160 
162  MacIpLearningKey key(ptr->vrf_id(), ptr->ip());
163  std::scoped_lock lock(macip_map_mutex_);
164  MacIpLearningEntryMap::iterator it = mac_ip_learning_entry_map_.find(key);
165  if (it == mac_ip_learning_entry_map_.end()) {
166  return;
167  }
168  MacIpLearningEntry *mac_ip_entry = dynamic_cast<MacIpLearningEntry *>( it->second.get());
169  if (!mac_ip_entry || mac_ip_entry->Mac() == ptr->mac()) {
170  return;
171  }
172 
173  VrfEntry *vrf = Agent::GetInstance()->vrf_table()->FindVrfFromId(ptr->vrf_id());
174  MAC_IP_LEARNING_TRACE(MacLearningTraceBuf, vrf?vrf->GetName():"NULL",
175  ptr->ip().to_string(), mac_ip_entry->Mac().ToString(),
176  mac_ip_entry->intf()->name(),
177  "IP Move detected, deleting local entry");
178 
179  it->second->Delete();
180  EnqueueMgmtReq(it->second, false);
181  mac_ip_learning_entry_map_.erase(key);
182 }
184  MacAddress &mac) {
187  vrf_id, ip, mac));
188  Enqueue(ptr);
189 }
191  const HealthCheckInstanceService *instance_service) {
192  const HealthCheckMacIpInstanceService *mac_ip_instance_service =
193  dynamic_cast<const HealthCheckMacIpInstanceService *>(instance_service);
194  if (mac_ip_instance_service) {
195  IpAddress ip = mac_ip_instance_service->destination_ip();
196  MacAddress mac = mac_ip_instance_service->destination_mac();
197  uint32_t vrf_id =
198  mac_ip_instance_service->interface()->vrf()->vrf_id();
201  vrf_id, ip, mac));
202  Enqueue(ptr);
203  }
204 }
206  MacIpLearningKey key(ptr->vrf_id(), ptr->ip());
207  std::scoped_lock lock(macip_map_mutex_);
208  MacIpLearningEntryMap::iterator it = mac_ip_learning_entry_map_.find(key);
209  if (it == mac_ip_learning_entry_map_.end()) {
210  return;
211  }
212  MacIpLearningEntry *mac_ip_entry = dynamic_cast<MacIpLearningEntry *>( it->second.get());
213  if (mac_ip_entry->Mac() == ptr->mac()) {
214  MAC_IP_LEARNING_TRACE(MacLearningTraceBuf, mac_ip_entry->vrf()->GetName(),
215  mac_ip_entry->IpAddr().to_string(), mac_ip_entry->Mac().ToString(),
216  mac_ip_entry->intf()->name(), "MACIP unreachable, trigger delete");
217  it->second->Delete();
218  EnqueueMgmtReq(it->second, false);
219  mac_ip_learning_entry_map_.erase(key);
220  }
221 }
222 
224  work_queue_.Enqueue(req);
225  return;
226 }
229  std::scoped_lock lock(macip_map_mutex_);
230  MacIpLearningEntryMap::iterator it = mac_ip_learning_entry_map_.find(key);
231  if (it == mac_ip_learning_entry_map_.end()) {
232  return NULL;
233  }
234 
235  MacIpLearningEntry *mac_ip_entry = dynamic_cast<MacIpLearningEntry *>( it->second.get());
236  return mac_ip_entry;
237 }
239  MacIpLearningKey key(vrf_id, ip);
240  MacIpLearningEntry *entry = Find(key);
241  if (entry) {
242  return entry->Mac();
243  }
244  return MacAddress();
245 
246 }
247 
249  uint32_t vrf_id, const IpAddress &ip,
250  const MacAddress &mac, InterfaceConstRef intf) :
251  MacLearningEntry(vrf_id), mac_ip_learning_table_(table),
252  key_(vrf_id, ip), ip_(ip), mac_(mac),
253  intf_(intf), vn_(NULL), hc_service_(NULL), hc_instance_(NULL) {
254  const VmInterface *vm_port = dynamic_cast<const VmInterface *>(intf_.get());
255  assert(vm_port);
256  vn_ = vm_port->vn();
257 
258 }
261  data->is_add = true;
262  data->mac_ip_list_.list_.insert(
264  DBRequest req;
266  req.key.reset(new VmInterfaceKey(AgentKey::RESYNC, intf_->GetUuid(), ""));
267  req.data.reset(data);
270  return 0;
271 }
272 
274  if (hc_instance_ && hc_service_) {
276  hc_instance_ = NULL;
277  hc_service_ = NULL;
278  }
280  data->is_add = false;
281  data->mac_ip_list_.list_.insert(
283  DBRequest req;
285  req.key.reset(new VmInterfaceKey(AgentKey::RESYNC, intf_->GetUuid(), ""));
286  req.data.reset(data);
288 }
289 
292 }
293 
296 }
297 
299  HealthCheckService *hc_service =
300  GetHealthCheckService(vn_.get());
301  if (hc_service_ != hc_service) {
302  if (hc_service_ == NULL) {
303  AddHealthCheckService(hc_service);
304  } else {
305  if (hc_instance_) {
307  }
308  hc_instance_ = NULL;
309  hc_service_= NULL;
310  if (hc_service) {
311  AddHealthCheckService(hc_service);
312  }
313  }
314  } else if (hc_service_) {
316  }
317 }
319  if (service) {
320  hc_service_ = service;
321  IpAddress gateway_ip = vn_->GetGatewayFromIpam(ip_);
322  const VmInterface *vm_intf = static_cast< const VmInterface *>(intf_.get());
323  //TODO: add validation check for null gateway ip
325  const_cast< VmInterface *>(vm_intf), NULL,
326  gateway_ip, ip_, mac_, false, false);
328  }
329 }
330 
332  //1. check if hc is attached
333  const boost::uuids::uuid hc_uuid = vn->health_check_uuid();
334  if (hc_uuid == boost::uuids::nil_uuid()) {
335  return NULL;
336  }
337  HealthCheckService *hc_service = Agent::GetInstance()->
338  health_check_table()->Find(hc_uuid);
339  //2. if attached , check whether target ip present in tragte ip list
340  if (!hc_service) {
341  return NULL;
342  }
343  if (hc_service->IsTargetIpPresent(IpAddr())) {
344  return hc_service;
345  }
346 
347  return NULL;
348 }
350  table_(table),
351  queue_(table_->agent()->task_scheduler()->GetTaskId(kTaskMacLearning),
352  0,
353  boost::bind(&MacIpLearningRequestQueue::HandleEvent,this,_1)) {
354 }
355 
357  return table_->RequestHandler(ptr);
358 }
boost::asio::ip::address IpAddress
Definition: address.h:14
boost::intrusive_ptr< const Interface > InterfaceConstRef
Definition: agent.h:52
#define kTaskMacLearning
Definition: agent.h:344
Definition: agent.h:361
InterfaceTable * interface_table() const
Definition: agent.h:468
MacLearningModule * mac_learning_module() const
Definition: agent.h:1016
MacLearningProto * mac_learning_proto() const
Definition: agent.h:1008
VrfTable * vrf_table() const
Definition: agent.h:488
static Agent * GetInstance()
Definition: agent.h:439
HealthCheckTable * health_check_table() const
Definition: agent.cc:937
bool Enqueue(DBRequest *req)
Definition: db_table.cc:221
void set_service(HealthCheckService *service)
Definition: health_check.cc:83
IpAddress destination_ip() const
InterfaceRef interface() const
Definition: health_check.h:148
virtual bool UpdateInstanceTask()
Definition: health_check.h:131
virtual const MacAddress destination_mac() const
Definition: health_check.h:273
void StopHealthCheckService(HealthCheckInstanceBase *instance)
bool IsTargetIpPresent(IpAddress &ip)
Definition: health_check.h:362
HealthCheckInstanceBase * StartHealthCheckService(VmInterface *intrface, VmInterface *paired_vmi, const IpAddress &source_ip, const IpAddress &destination_ip, const MacAddress &destination_mac, bool ignore_status_event, bool multi_hop)
void RegisterHealthCheckNotifyCallback(HealthCheckNotifyCallback fn)
Definition: health_check.h:461
const std::string & name() const
Definition: interface.h:117
std::string ToString() const
Definition: mac_address.cc:54
IpAddress & IpAddr()
const Interface * intf()
InterfaceConstRef intf_
MacIpLearningEntry(MacIpLearningTable *table, uint32_t vrf_id_, const IpAddress &ip, const MacAddress &mac, InterfaceConstRef intf)
MacAddress & Mac()
void AddHealthCheckService(HealthCheckService *service)
VnEntryConstRef vn_
HealthCheckService * hc_service_
void EnqueueToTable(MacLearningEntryRequestPtr req)
HealthCheckInstanceBase * hc_instance_
MacIpLearningTable * mac_ip_learning_table_
HealthCheckService * GetHealthCheckService(const VnEntry *vn)
MacIpLearningTable * table_
bool HandleEvent(MacLearningEntryRequestPtr ptr)
void Enqueue(MacLearningEntryRequestPtr ptr)
MacIpLearningRequestQueue(MacIpLearningTable *table)
MacAddress GetPairedMacAddress(uint32_t vrf_id, const IpAddress &ip)
void Add(MacLearningEntryPtr ptr)
void Enqueue(MacLearningEntryRequestPtr req)
void DetectIpMove(MacLearningEntryRequestPtr ptr)
bool RequestHandler(MacLearningEntryRequestPtr ptr)
void EnqueueMgmtReq(MacLearningEntryPtr ptr, bool add)
MacIpLearningEntryMap mac_ip_learning_entry_map_
MacIpLearningTable(Agent *agent, MacLearningProto *proto)
std::mutex macip_map_mutex_
MacIpLearningRequestQueue work_queue_
void Delete(MacLearningEntryPtr ptr)
std::pair< MacIpLearningKey, MacLearningEntryPtr > MacIpLearningEntryPair
void MacIpEntryUnreachable(MacLearningEntryRequestPtr ptr)
void Resync(MacLearningEntryPtr ptr)
MacIpLearningEntry * Find(const MacIpLearningKey &key)
void MacIpEntryHcNotify(const HealthCheckInstanceService *instance_service)
VrfEntry * vrf() const
void Enqueue(MacLearningMgmtRequestPtr &ptr)
MacLearningMgmtManager * mac_learning_mgmt() const
bool ProcessProto(boost::shared_ptr< PktInfo > msg_info)
bool IsMaxMacIpLearnt() const
const VnEntry * vn() const
Definition: vn.h:152
const boost::uuids::uuid & health_check_uuid() const
Definition: vn.h:246
const VnIpam * GetIpam(const IpAddress &ip) const
Definition: vn.cc:653
Definition: vrf.h:90
const string & GetName() const
Definition: vrf.h:104
VrfEntry * FindVrfFromId(size_t index)
Definition: vrf.cc:885
boost::shared_ptr< MacLearningEntry > MacLearningEntryPtr
boost::shared_ptr< MacLearningEntryRequest > MacLearningEntryRequestPtr
SandeshTraceBufferPtr MacLearningTraceBuf
boost::shared_ptr< MacLearningMgmtRequest > MacLearningMgmtRequestPtr
@ RESYNC
Definition: agent_db.h:104
DBOperation oper
Definition: db_table.h:43
@ 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
VmInterface::LearntMacIpList mac_ip_list_
boost::uuids::uuid uuid