OpenSDN source code
multicast_mac_local_ovsdb.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Juniper Networks, Inc. All rights reserved.
3  * Copyright Contributors to the OpenSDN project
4  */
5 
6 extern "C" {
7 #include <ovsdb_wrapper.h>
8 };
9 #include <cmn/agent.h>
10 #include <oper/vn.h>
11 #include <oper/vrf.h>
12 #include <oper/vxlan.h>
15 #include <ovsdb_client.h>
16 #include <ovsdb_client_idl.h>
17 #include <ovsdb_client_session.h>
18 #include <base/util.h>
19 #include <base/string_util.h>
20 #include <oper/agent_sandesh.h>
21 #include <ovsdb_sandesh.h>
22 #include <ovsdb_types.h>
23 #include <ovsdb_route_peer.h>
24 #include <logical_switch_ovsdb.h>
26 #include <vn_ovsdb.h>
27 
28 using namespace OVSDB;
29 using std::string;
30 
32  const MulticastMacLocalEntry *key) : OvsdbEntry(table),
33  logical_switch_name_(key->logical_switch_name_), vrf_(NULL, this),
34  vxlan_id_(key->vxlan_id_), row_list_(key->row_list_) {
35 }
36 
38  const std::string &logical_switch_name)
39  : OvsdbEntry(table), logical_switch_name_(logical_switch_name),
40  vrf_(NULL, this), vxlan_id_(0), row_list_() {
41 }
42 
44  const std::string &logical_switch_name, struct ovsdb_idl_row *row)
45  : OvsdbEntry(table), logical_switch_name_(logical_switch_name),
46  vrf_(NULL, this), vxlan_id_(0), row_list_() {
47  if (row) {
48  row_list_.insert(row);
49  }
50 }
51 
53  VnOvsdbObject *vn_object = table_->client_idl()->vn_ovsdb();
55  VnOvsdbEntry *vn_entry =
56  static_cast<VnOvsdbEntry *>(vn_object->GetReference(&vn_key));
57  return vn_entry;
58 }
59 
61  OVSDB::VnOvsdbEntry *vn_entry = GetVnEntry();
62  if ((vn_entry == NULL) || (vn_entry->vrf() == NULL)) {
63  OnVrfDelete();
64  // Issue a ADD_CHANGE_REQ to push entry into defer state.
65  // Whenever VN entry is back this will be updated.
67  }
68 }
69 
71  if (vrf_ == NULL)
72  return;
73 
74  MulticastMacLocalOvsdb *table = static_cast<MulticastMacLocalOvsdb *>(table_);
75  OVSDB_TRACE(Trace, "Deleting Multicast Route VN uuid " + logical_switch_name_);
76  TorIpList::iterator it_ip = tor_ip_list_.begin();
77  for (; it_ip != tor_ip_list_.end(); it_ip++) {
79  (*it_ip));
80  }
81  tor_ip_list_.clear();
83  this));
84  // remove vrf reference after deleting route
85  vrf_ = NULL;
86  return;
87 }
88 
90  OvsdbIdlRowList::iterator it = row_list_.begin();
91  TorIpList old_tor_ip_list = tor_ip_list_;
92  uint32_t old_vxlan_id = vxlan_id_;
93  // clear the list to hold new entries
94  tor_ip_list_.clear();
95 
96  MulticastMacLocalOvsdb *table = static_cast<MulticastMacLocalOvsdb *>(table_);
97 
98  OVSDB::VnOvsdbEntry *vn_entry = GetVnEntry();
99  // Take vrf reference to genrate withdraw/delete route request
100  vrf_ = vn_entry->vrf();
101  OVSDB_TRACE(Trace, "Adding multicast Route VN uuid " + logical_switch_name_);
102  vxlan_id_ = vn_entry->vxlan_id();
103 
104  //In case vxlan id is changed then remove old vaxlan id before auditing
105  //list.
106  if (old_vxlan_id != vxlan_id_) {
107  TorIpList::iterator it_ip = old_tor_ip_list.begin();
108  for (; it_ip != old_tor_ip_list.end(); it_ip++) {
109  table->peer()->DeleteOvsPeerMulticastRoute(vrf_.get(), old_vxlan_id,
110  (*it_ip));
111  }
112  //No need of old list, so flush it and honor new list
113  old_tor_ip_list.clear();
114  }
115 
117  this));
118 
119  for (; it != row_list_.end(); it++) {
120  struct ovsdb_idl_row *l_set =
122  std::size_t count =
124  for (std::size_t i = 0; i != count; i++) {
125  struct ovsdb_idl_row *locator =
127  std::string tor_ip_str =
129  boost::system::error_code ec;
130  Ip4Address tor_ip = Ip4Address::from_string(tor_ip_str, ec);
131  if (tor_ip.to_ulong() == 0) {
132  // 0 ip is not valid ip to export, continue to next row
133  continue;
134  }
135  // insert the current ip to new list
136  tor_ip_list_.insert(tor_ip);
137  // remove from the old list
138  old_tor_ip_list.erase(tor_ip);
139 
140  // export the OVS route to Oper Db
141  table->peer()->AddOvsPeerMulticastRoute(vrf_.get(), vxlan_id_,
142  vn_entry->name(),
143  table_->client_idl()->tsn_ip(),
144  tor_ip);
145  }
146  }
147 
148  TorIpList::iterator it_ip = old_tor_ip_list.begin();
149  for (; it_ip != old_tor_ip_list.end(); it_ip++) {
151  (*it_ip));
152  }
153 
154  return true;
155 }
156 
158  return Add();
159 }
160 
162  OnVrfDelete();
163  return true;
164 }
165 
166 bool MulticastMacLocalEntry::IsLess(const KSyncEntry& entry) const {
167  const MulticastMacLocalEntry &mcast =
168  static_cast<const MulticastMacLocalEntry&>(entry);
170 }
171 
173  VnOvsdbEntry *vn_entry = GetVnEntry();
174  if (!vn_entry->IsResolved()) {
175  OVSDB_TRACE(Trace, "Skipping multicast route add " +
176  logical_switch_name_ + " due to unavailable VN ");
177  return vn_entry;
178  }
179 
180  return NULL;
181 }
182 
185  return tor_ip_list_;
186 }
187 
189  return logical_switch_name_;
190 }
191 
193  OvsdbObject(idl), peer_(peer) {
195  idl->agent()->task_scheduler()->GetTaskId("Agent::KSync"), 0,
196  boost::bind(&MulticastMacLocalOvsdb::VrfReEval, this, _1));
197  vrf_reeval_queue_->set_name("OVSDB VRF Multicast local mac re-evaluation "
198  "queue");
199  // register to listen updates for multicast mac local
201  boost::bind(&MulticastMacLocalOvsdb::Notify, this, _1, _2));
202  // registeration to physical locator set is also required, since
203  // an update for physical locators in the set can show up after
204  // multicast row update // during which we need to trigger
205  // re-evaluation of the exported multicast route
207  boost::bind(&MulticastMacLocalOvsdb::LocatorSetNotify, this,
208  _1, _2));
209 }
210 
212  vrf_reeval_queue_->Shutdown();
213  delete vrf_reeval_queue_;
214 }
215 
217  return peer_;
218 }
219 
221  vrf_reeval_queue_->Enqueue(vrf);
222 }
223 
224 //Only executed for VRF delete
226  // iterate through dependency list and trigger del and add
227  VrfDepList::iterator it =
228  vrf_dep_list_.upper_bound(VrfDepEntry(vrf_ref.get(), NULL));
229  while (it != vrf_dep_list_.end()) {
230  if (it->first != vrf_ref.get()) {
231  break;
232  }
233  MulticastMacLocalEntry *m_entry = it->second;
234  it++;
235  m_entry->EvaluateVrfDependency(vrf_ref.get());
236  }
237  return true;
238 }
239 
241  struct ovsdb_idl_row *row) {
242  const char *ls_name = ovsdb_wrapper_mcast_mac_local_logical_switch(row);
243 
244  LogicalSwitchTable *l_table = client_idl_->logical_switch_table();
245  l_table->OvsdbMcastLocalMacNotify(op, row);
246 
247  /* ignore if ls_name is not present */
248  if (ls_name == NULL) {
249  return;
250  }
251 
252  std::string ls_name_str(ls_name);
253  MulticastMacLocalEntry key(this, ls_name, row);
254  MulticastMacLocalEntry *entry =
255  static_cast<MulticastMacLocalEntry*>(FindActiveEntry(&key));
256  struct ovsdb_idl_row *l_set =
258  // physical locator set is immutable so it will not change
259  // for given multicast row, trigger delete for row and
260  // wait for locator set to be available
261  if (op == OvsdbClientIdl::OVSDB_DEL || l_set == NULL) {
262  if (entry != NULL) {
263  entry->row_list_.erase(row);
264  if (l_set != NULL) {
265  locator_dep_list_.erase(l_set);
266  }
267  if (entry->row_list_.empty()) {
268  // delete entry if the last idl row is removed
269  Delete(entry);
270  } else {
271  // trigger change on entry to remove the delete ToR IP
272  // from the list
273  Change(entry);
274  }
275  }
276  } else if (op == OvsdbClientIdl::OVSDB_ADD) {
277  if (entry == NULL) {
278  entry = static_cast<MulticastMacLocalEntry*>(Create(&key));
279  } else {
280  entry->row_list_.insert(row);
281  // trigger change on entry to add the new ToR IP
282  // to the list
283  Change(entry);
284  }
285  locator_dep_list_[l_set] = entry;
286  }
287 }
288 
290  struct ovsdb_idl_row *row) {
291  if (op == OvsdbClientIdl::OVSDB_DEL) {
292  locator_dep_list_.erase(row);
293  return;
294  }
295  OvsdbIdlDepList::iterator it = locator_dep_list_.find(row);
296  if (it != locator_dep_list_.end()) {
297  Change(it->second);
298  }
299 }
300 
302  const MulticastMacLocalEntry *k_entry =
303  static_cast<const MulticastMacLocalEntry *>(key);
304  MulticastMacLocalEntry *entry = new MulticastMacLocalEntry(this, k_entry);
305  return entry;
306 }
307 
309 // Sandesh routines
312  std::string resp_ctx, AgentSandeshArguments &args) :
313  OvsdbSandeshTask(resp_ctx, args) {
314  if (args.Get("ls_name", &ls_name_) == false) {
315  ls_name_ = "";
316  }
317 }
318 
320  std::string resp_ctx, const std::string &ip, uint32_t port,
321  const std::string &ls) :
322  OvsdbSandeshTask(resp_ctx, ip, port), ls_name_(ls) {
323 }
324 
326 }
327 
329  if (!ls_name_.empty()) {
330  args.Add("ls_name", ls_name_);
331  }
332 }
333 
336  if (!ls_name_.empty()) {
337  MulticastMacLocalEntry *entry =
338  static_cast<MulticastMacLocalEntry *>(kentry);
339  if (entry->logical_switch_name().find(ls_name_) != std::string::npos) {
340  return FilterAllow;
341  }
342  return FilterDeny;
343  }
344  return FilterAllow;
345 }
346 
348  SandeshResponse *resp) {
349  MulticastMacLocalEntry *entry =
350  static_cast<MulticastMacLocalEntry *>(kentry);
351  OvsdbMulticastMacLocalEntry oentry;
352  oentry.set_state(entry->StateString());
353  oentry.set_mac("ff:ff:ff:ff:ff:ff");
354  oentry.set_logical_switch(entry->logical_switch_name());
355  oentry.set_vxlan_id(entry->vxlan_id());
356  std::vector<std::string> &tor_ip =
357  const_cast<std::vector<std::string>&>(oentry.get_tor_ip());
358  MulticastMacLocalEntry::TorIpList::const_iterator it =
359  entry->tor_ip_list().begin();
360  for (; it != entry->tor_ip_list().end(); it++) {
361  tor_ip.push_back((*it).to_string());
362  }
363  OvsdbMulticastMacLocalResp *m_resp =
364  static_cast<OvsdbMulticastMacLocalResp *>(resp);
365  std::vector<OvsdbMulticastMacLocalEntry> &macs =
366  const_cast<std::vector<OvsdbMulticastMacLocalEntry>&>(
367  m_resp->get_macs());
368  macs.push_back(oentry);
369 }
370 
372  return static_cast<SandeshResponse *>(new OvsdbMulticastMacLocalResp());
373 }
374 
376  OvsdbClientSession *session) {
377  return static_cast<KSyncObject *>(
378  session->client_idl()->multicast_mac_local_ovsdb());
379 }
380 
381 void OvsdbMulticastMacLocalReq::HandleRequest() const {
383  new MulticastMacLocalSandeshTask(context(), get_session_remote_ip(),
384  get_session_remote_port(),
385  get_logical_switch());
387  scheduler->Enqueue(task);
388 }
boost::asio::ip::address_v4 Ip4Address
Definition: address.h:15
bool Add(const std::string &key, const std::string &val)
bool Get(const std::string &key, std::string *val) const
TaskScheduler * task_scheduler() const
Definition: agent.h:1123
std::string StateString() const
@ ADD_CHANGE_REQ
Definition: ksync_entry.h:53
bool IsResolved()
void Change(KSyncEntry *entry)
void SafeNotifyEvent(KSyncEntry *entry, KSyncEntry::KSyncEvent event)
KSyncEntry * GetReference(const KSyncEntry *key)
KSyncEntry * Create(const KSyncEntry *key)
void Delete(KSyncEntry *entry)
void OvsdbMcastLocalMacNotify(OvsdbClientIdl::Op, struct ovsdb_idl_row *)
OVSDB::VnOvsdbEntry * GetVnEntry() const
bool IsLess(const KSyncEntry &) const
const std::string & logical_switch_name() const
MulticastMacLocalEntry(MulticastMacLocalOvsdb *table, const MulticastMacLocalEntry *key)
void LocatorSetNotify(OvsdbClientIdl::Op op, struct ovsdb_idl_row *row)
MulticastMacLocalOvsdb(OvsdbClientIdl *idl, OvsPeer *peer)
std::pair< VrfEntry *, MulticastMacLocalEntry * > VrfDepEntry
void Notify(OvsdbClientIdl::Op op, struct ovsdb_idl_row *row)
KSyncEntry * Alloc(const KSyncEntry *key, uint32_t index)
WorkQueue< VrfEntryRef > * vrf_reeval_queue_
void UpdateResp(KSyncEntry *kentry, SandeshResponse *resp)
void EncodeArgs(AgentSandeshArguments &args)
KSyncObject * GetObject(OvsdbClientSession *session)
MulticastMacLocalSandeshTask(std::string resp_ctx, AgentSandeshArguments &args)
MulticastMacLocalOvsdb * multicast_mac_local_ovsdb()
VnOvsdbObject * vn_ovsdb()
void Register(EntryType type, NotifyCB cb)
OvsdbObject * table_
Definition: ovsdb_entry.h:53
OvsdbClientIdlPtr client_idl_
Definition: ovsdb_object.h:35
OvsdbClientIdl * client_idl()
Definition: ovsdb_object.h:32
KSyncEntry * FindActiveEntry(KSyncEntry *key)
Definition: ovsdb_object.cc:24
uint32_t vxlan_id()
Definition: vn_ovsdb.h:45
const std::string & name()
Definition: vn_ovsdb.h:46
VrfEntry * vrf()
Definition: vn_ovsdb.cc:93
void DeleteOvsPeerMulticastRoute(const VrfEntry *vrf, uint32_t vxlan_id, const Ip4Address &tor_ip)
void AddOvsPeerMulticastRoute(const VrfEntry *vrf, uint32_t vxlan_id, const std::string &vn_name_, const Ip4Address &tsn_ip, const Ip4Address &tor_ip)
The TaskScheduler keeps track of what tasks are currently schedulable. When a task is enqueued it is ...
Definition: task.h:307
int GetTaskId(const std::string &name)
Definition: task.cc:884
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
Definition: trace.h:289
Definition: vrf.h:90
Definition: agent.h:44
#define OVSDB_TRACE(obj,...)
char * ovsdb_wrapper_mcast_mac_local_logical_switch(struct ovsdb_idl_row *row)
size_t ovsdb_wrapper_physical_locator_set_locator_count(struct ovsdb_idl_row *row)
struct ovsdb_idl_row ** ovsdb_wrapper_physical_locator_set_locators(struct ovsdb_idl_row *row)
struct ovsdb_idl_row * ovsdb_wrapper_mcast_mac_local_physical_locator_set(struct ovsdb_idl_row *row)
char * ovsdb_wrapper_physical_locator_dst_ip(struct ovsdb_idl_row *row)
static boost::uuids::uuid StringToUuid(const std::string &str)
Definition: string_util.h:146
Definition: task_int.h:11