OpenSDN source code
ha_stale_dev_vn.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 };
10 #include <ovsdb_client.h>
11 #include <ovsdb_route_peer.h>
13 #include <ha_stale_dev_vn.h>
14 #include <ha_stale_vn.h>
15 #include <ha_stale_l2_route.h>
17 #include <oper/vn.h>
18 #include <oper/vrf.h>
19 #include <oper/nexthop.h>
20 #include <oper/tunnel_nh.h>
21 #include <oper/agent_path.h>
22 #include <oper/bridge_route.h>
23 
24 #include <ovsdb_sandesh.h>
25 #include <ovsdb_types.h>
26 
27 using namespace OVSDB;
28 
30  const boost::uuids::uuid &vn_uuid) : OvsdbDBEntry(table),
31  vn_uuid_(vn_uuid), l2_table_(NULL), old_l2_table_(NULL),
32  oper_bridge_table_(NULL), dev_ip_(), vn_name_(""), vxlan_id_(0) {
33 }
34 
36  assert(l2_table_ == NULL);
37  assert(old_l2_table_ == NULL);
38 }
39 
41  // if table is scheduled for delete, return from here
42  // and wait for delete callback
43  if (table_->delete_scheduled()) {
44  return true;
45  }
46 
47  bool ret = true;
48  // On device ip, vxlan id or Agent Route Table pointer change delete
49  // previous route table and add new with updated vxlan id
50  if (l2_table_ != NULL &&
52  ret = Delete();
53  }
54 
55  // create route table and register
56  if (l2_table_ == NULL) {
58  }
59 
60  // trigger update params for L2 Table to pick new vxlan id / tor ip
61  l2_table_->UpdateParams(this);
62  return ret;
63 }
64 
66  return Add();
67 }
68 
70  // delete route table.
71  if (l2_table_ != NULL) {
73  assert(old_l2_table_ == NULL);
75  l2_table_ = NULL;
76  return false;
77  }
78 
79  return true;
80 }
81 
83  bool change = false;
84  // check if route table is available.
85  const PhysicalDeviceVn *dev_vn =
86  static_cast<const PhysicalDeviceVn *>(db_entry);
87  const VrfEntry *vrf = dev_vn->vn()->GetVrf();
88  if (vrf == NULL) {
89  // trigger change and wait for VRF
90  oper_bridge_table_ = NULL;
91  change = true;
92  } else if (oper_bridge_table_ != vrf->GetBridgeRouteTable()) {
94  change = true;
95  }
96  if (dev_ip_ != dev_vn->tor_ip()) {
97  dev_ip_ = dev_vn->tor_ip();
98  change = true;
99  }
100  if (vn_name_ != dev_vn->vn()->GetName()) {
101  vn_name_ = dev_vn->vn()->GetName();
102  change = true;
103  }
104  if (vxlan_id_ != (uint32_t)dev_vn->vxlan_id()) {
105  vxlan_id_ = dev_vn->vxlan_id();
106  change = true;
107  }
108  return change;
109 }
110 
111 bool HaStaleDevVnEntry::IsLess(const KSyncEntry &entry) const {
112  const HaStaleDevVnEntry &dev_vn_entry =
113  static_cast<const HaStaleDevVnEntry &>(entry);
114  return vn_uuid_ < dev_vn_entry.vn_uuid_;
115 }
116 
119  static_cast<HaStaleDevVnTable *>(table_);
120  HaStaleVnEntry key(table->vn_table_, vn_uuid_);
121  HaStaleVnEntry *entry = static_cast<HaStaleVnEntry*>
122  (table->vn_table_->GetReference(&key));
123  // Wait for vn-vrf link
124  if (!entry->IsResolved()) {
125  return entry;
126  }
127 
128  if (oper_bridge_table_ == NULL) {
129  // update route table when vn-vrf link is available
130  oper_bridge_table_ = entry->bridge_table();
131  }
132 
133  if (vn_name_.empty()) {
134  // update vn name from vn if available
135  vn_name_ = entry->vn_name();
136  }
137  return NULL;
138 }
139 
141  OvsdbDBObject *object = static_cast<OvsdbDBObject*>(GetObject());
142  assert(old_l2_table_ == table);
143  if (l2_table_ != NULL) {
144  old_l2_table_ = NULL;
145  object->SafeNotifyEvent(this, KSyncEntry::ADD_ACK);
146  } else {
147  old_l2_table_ = NULL;
148  object->SafeNotifyEvent(this, KSyncEntry::DEL_ACK);
149  }
150 }
151 
153  const HaStaleDevVnTable *reflector_table =
154  static_cast<const HaStaleDevVnTable *>(table_);
155  return reflector_table->agent();
156 }
157 
159  const HaStaleDevVnTable *reflector_table =
160  static_cast<const HaStaleDevVnTable *>(table_);
161  return reflector_table->route_peer();
162 }
163 
164 const std::string &HaStaleDevVnEntry::dev_name() const {
165  const HaStaleDevVnTable *reflector_table =
166  static_cast<const HaStaleDevVnTable *>(table_);
167  return reflector_table->dev_name();
168 }
169 
171  const HaStaleDevVnTable *reflector_table =
172  static_cast<const HaStaleDevVnTable *>(table_);
173  return reflector_table->state();
174 }
175 
177  return dev_ip_;
178 }
179 
180 const std::string &HaStaleDevVnEntry::vn_name() const {
181  return vn_name_;
182 }
183 
184 uint32_t HaStaleDevVnEntry::vxlan_id() const {
185  return vxlan_id_;
186 }
187 
189  OvsPeerManager *manager, ConnectionStateEntry *state,
190  std::string &dev_name) :
191  OvsdbDBObject(NULL, false), agent_(agent), manager_(manager),
192  dev_name_(dev_name), state_(state),
193  vn_table_(new HaStaleVnTable(agent, this)), time_stamp_(1),
194  stale_clear_timer_(TimerManager::CreateTimer(
195  *(agent->event_manager())->io_service(),
196  "OVSDB Route Replicator cleanup timer",
197  agent->task_scheduler()->GetTaskId("Agent::KSync"), 0)) {
199  agent->task_scheduler()->GetTaskId("Agent::KSync"), 0,
200  boost::bind(&HaStaleDevVnTable::VnReEval, this, _1));
201  vn_reeval_queue_->set_name("OVSDB VN re-evaluation queue");
202  Ip4Address zero_ip;
203  route_peer_.reset(manager->Allocate(zero_ip));
204  route_peer_->set_ha_stale_export(true);
206 }
207 
209  // validate vn_table and vn_reeval_queue are deleted
210  // before the destuctor is called
211  assert(vn_table_ == NULL && vn_reeval_queue_ == NULL);
212 
213  // Cancel timer if running
216  manager_->Free(route_peer_.release());
217 }
218 
219 KSyncEntry *HaStaleDevVnTable::Alloc(const KSyncEntry *key, uint32_t index) {
220  const HaStaleDevVnEntry *k_entry =
221  static_cast<const HaStaleDevVnEntry *>(key);
222  HaStaleDevVnEntry *entry = new HaStaleDevVnEntry(this, k_entry->vn_uuid_);
223  return entry;
224 }
225 
227  const PhysicalDeviceVn *entry =
228  static_cast<const PhysicalDeviceVn *>(db_entry);
229  HaStaleDevVnEntry *key =
230  new HaStaleDevVnEntry(this, entry->vn()->GetUuid());
231  return static_cast<KSyncEntry *>(key);
232 }
233 
235  const DBEntry *entry, const OvsdbDBEntry *ovsdb_entry) {
236  const PhysicalDeviceVn *dev_vn =
237  static_cast<const PhysicalDeviceVn *>(entry);
238 
239  // Delete the entry which has invalid VxLAN id associated.
240  if (dev_vn->vxlan_id() == 0) {
241  return DBFilterDelete;
242  }
243 
244  // Accept only devices with name matched to dev_name_
245  if (dev_vn->device_display_name() != dev_name_) {
246  return DBFilterDelete;
247  }
248 
249  if (vn_table_ == NULL) {
250  // delete table delete notify the entry
251  return DBFilterDelete;
252  }
253 
254  return DBFilterAccept;
255 }
256 
258  return agent_;
259 }
260 
262  if (vn_table_ != NULL) {
264  vn_table_ = NULL;
265  }
266 
267  if (vn_reeval_queue_ != NULL) {
269  delete vn_reeval_queue_;
270  vn_reeval_queue_ = NULL;
271  }
272 }
273 
276  // unregister the object if emptytable is called with
277  // object being scheduled for delete
278  if (delete_scheduled()) {
280  }
281 }
282 
284  // vn_reeval_queue_ will be NULL if delete of the table
285  // is already triggered
286  if (vn_reeval_queue_ != NULL) {
287  vn_reeval_queue_->Enqueue(vn_uuid);
288  }
289 }
290 
292  HaStaleDevVnEntry key(this, vn_uuid);
293  HaStaleDevVnEntry *entry =
294  static_cast<HaStaleDevVnEntry*>(Find(&key));
295  if (entry && !entry->IsDeleted()) {
296  Change(entry);
297  }
298  return true;
299 }
300 
302  return route_peer_.get();
303 }
304 
305 const std::string &HaStaleDevVnTable::dev_name() const {
306  return dev_name_;
307 }
308 
310  return state_.get();
311 }
312 
313 void HaStaleDevVnTable::StaleClearAddEntry(uint64_t time_stamp,
314  HaStaleL2RouteEntry *entry,
315  StaleClearL2EntryCb cb) {
316  if (stale_l2_entry_map_.empty()) {
317  // start the timer while adding the first entry
319  boost::bind(&HaStaleDevVnTable::StaleClearTimerCb, this));
320  }
321  StaleL2Entry l2_entry(time_stamp, entry);
322  stale_l2_entry_map_[l2_entry] = cb;
323 }
324 
325 void HaStaleDevVnTable::StaleClearDelEntry(uint64_t time_stamp,
326  HaStaleL2RouteEntry *entry) {
327  StaleL2Entry l2_entry(time_stamp, entry);
328  stale_l2_entry_map_.erase(l2_entry);
329  if (stale_l2_entry_map_.empty()) {
330  // stop the timer on last entry removal
332  }
333 }
334 
336  uint32_t count = 0;
337  time_stamp_++;
338  uint32_t timer =
340  while (!stale_l2_entry_map_.empty() && count < kNumEntriesPerIteration) {
341  CbMap::iterator it = stale_l2_entry_map_.begin();
342  if (time_stamp_ - it->first.time_stamp < timer) {
343  // first entry is yet to age
344  break;
345  }
346  StaleClearL2EntryCb cb = it->second;
347  cb();
348  count++;
349  }
350 
351  if (stale_l2_entry_map_.empty()) {
352  // do not restart the timer if all entries are removed
353  return false;
354  }
355  return true;
356 }
357 
359 // Sandesh routines
362  std::string resp_ctx, AgentSandeshArguments &args) :
363  OvsdbSandeshTask(resp_ctx, args), dev_name_(""), vn_uuid_("") {
364  if (false == args.Get("dev_name", &dev_name_)) {
365  dev_name_ = "";
366  }
367  if (false == args.Get("vn_uuid", &vn_uuid_)) {
368  vn_uuid_ = "";
369  }
370 }
371 
373  const std::string &dev_name,
374  const std::string &vn_uuid) :
375  OvsdbSandeshTask(resp_ctx, "0.0.0.0", 0), dev_name_(dev_name), vn_uuid_(vn_uuid) {
376 }
377 
379 }
380 
382  if (!dev_name_.empty()) {
383  args.Add("dev_name", dev_name_);
384  }
385  if (!vn_uuid_.empty()) {
386  args.Add("vn_uuid", vn_uuid_);
387  }
388 }
389 
392  if (!vn_uuid_.empty()) {
393  HaStaleDevVnEntry *entry = static_cast<HaStaleDevVnEntry *>(kentry);
394  if (UuidToString(entry->vn_uuid()).find(vn_uuid_) != std::string::npos) {
395  return FilterAllow;
396  }
397  return FilterDeny;
398  }
399  return FilterAllow;
400 }
401 
403  SandeshResponse *resp) {
404  HaStaleDevVnEntry *entry = static_cast<HaStaleDevVnEntry *>(kentry);
405  OvsdbHaStaleDevVnExport dentry;
406  dentry.set_state(entry->StateString());
407  dentry.set_dev_name(entry->dev_name());
408  dentry.set_dev_ip(entry->dev_ip().to_string());
409  dentry.set_vn_uuid(UuidToString(entry->vn_uuid()));
410  dentry.set_vn_name(entry->vn_name());
411  dentry.set_vxlan_id(entry->vxlan_id());
413  UuidToString(entry->vn_uuid()), "");
414  dentry.set_l2_route_table(task.EncodeFirstPage());
415 
416  OvsdbHaStaleDevVnExportResp *d_resp =
417  static_cast<OvsdbHaStaleDevVnExportResp *>(resp);
418  std::vector<OvsdbHaStaleDevVnExport> &dev_vn =
419  const_cast<std::vector<OvsdbHaStaleDevVnExport>&>(
420  d_resp->get_dev_vn());
421  dev_vn.push_back(dentry);
422 }
423 
425  return static_cast<SandeshResponse *>(new OvsdbHaStaleDevVnExportResp());
426 }
427 
429  ConnectionStateTable *con_table =
431  ConnectionStateEntry *con_entry = con_table->Find(dev_name_);
432  if (con_entry == NULL) {
433  return NULL;
434  }
435  return static_cast<KSyncObject *>(con_entry->ha_stale_dev_vn_table());
436 }
437 
438 void OvsdbHaStaleDevVnExportReq::HandleRequest() const {
440  new HaStaleDevVnSandeshTask(context(), get_dev_name(),
441  get_vn_uuid());
443  scheduler->Enqueue(task);
444 }
445 
boost::asio::ip::address IpAddress
Definition: address.h:14
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
Definition: agent.h:361
PhysicalDeviceVnTable * physical_device_vn_table() const
Definition: agent.h:638
OVSDB::OvsdbClient * ovsdb_client() const
Definition: agent.h:1129
TaskScheduler * task_scheduler() const
Definition: agent.h:1123
static Agent * GetInstance()
Definition: agent.h:439
DBTableBase * GetDBTable()
Definition: ksync_object.h:245
std::string StateString() const
bool IsResolved()
bool IsDeleted()
Definition: ksync_entry.h:182
static void Unregister(KSyncObject *)
bool delete_scheduled()
Definition: ksync_object.h:147
KSyncEntry * Find(const KSyncEntry *key)
void Change(KSyncEntry *entry)
void SafeNotifyEvent(KSyncEntry *entry, KSyncEntry::KSyncEvent event)
KSyncEntry * GetReference(const KSyncEntry *key)
HaStaleDevVnTable * ha_stale_dev_vn_table() const
ConnectionStateEntry * Find(const std::string &device_name)
IpAddress dev_ip() const
const std::string & dev_name() const
ConnectionStateEntry * state() const
void TriggerAck(HaStaleL2RouteTable *table)
HaStaleL2RouteTable * l2_table_
bool IsLess(const KSyncEntry &) const
boost::uuids::uuid vn_uuid_
const boost::uuids::uuid & vn_uuid() const
HaStaleL2RouteTable * old_l2_table_
const std::string & vn_name() const
AgentRouteTable * oper_bridge_table_
KSyncEntry * UnresolvedReference()
HaStaleDevVnEntry(OvsdbDBObject *table, const boost::uuids::uuid &vn_uuid)
OvsPeer * route_peer() const
FilterResp Filter(KSyncEntry *entry)
void UpdateResp(KSyncEntry *kentry, SandeshResponse *resp)
void EncodeArgs(AgentSandeshArguments &args)
KSyncObject * GetObject(OvsdbClientSession *session)
HaStaleDevVnSandeshTask(std::string resp_ctx, AgentSandeshArguments &args)
DBFilterResp OvsdbDBEntryFilter(const DBEntry *entry, const OvsdbDBEntry *ovsdb_entry)
static const uint32_t kStaleTimerJobInterval
HaStaleVnTable * vn_table_
std::unique_ptr< OvsPeer > route_peer_
const std::string & dev_name() const
void StaleClearDelEntry(uint64_t time_stamp, HaStaleL2RouteEntry *entry)
void VnReEvalEnqueue(const boost::uuids::uuid &vn_uuid)
uint64_t time_stamp() const
HaStaleDevVnTable(Agent *agent, OvsPeerManager *manager, ConnectionStateEntry *state, std::string &dev_name)
boost::function< void(void)> StaleClearL2EntryCb
void StaleClearAddEntry(uint64_t time_stamp, HaStaleL2RouteEntry *entry, StaleClearL2EntryCb cb)
OvsPeerManager * manager_
ConnectionStateEntry * state() const
static const uint32_t kNumEntriesPerIteration
KSyncEntry * DBToKSyncEntry(const DBEntry *)
KSyncEntry * Alloc(const KSyncEntry *key, uint32_t index)
bool VnReEval(const boost::uuids::uuid &vn_uuid)
WorkQueue< boost::uuids::uuid > * vn_reeval_queue_
ConnectionStateEntryPtr state_
OvsPeer * route_peer() const
friend class HaStaleDevVnEntry
void UpdateParams(HaStaleDevVnEntry *dev_vn)
AgentRouteTable * bridge_table() const
Definition: ha_stale_vn.cc:74
const std::string & vn_name() const
Definition: ha_stale_vn.cc:70
ConnectionStateTable * connection_table()
Definition: ovsdb_client.cc:41
int ha_stale_route_interval() const
Definition: ovsdb_client.cc:63
KSyncObject * GetObject() const
Definition: ovsdb_entry.cc:195
OvsdbDBObject * table_
Definition: ovsdb_entry.h:111
OvsdbDBObject * table()
Definition: ovsdb_entry.h:97
virtual void OvsdbRegisterDBTable(DBTable *tbl)
Definition: ovsdb_object.cc:77
virtual void EmptyTable(void)
void Free(OvsPeer *peer)
OvsPeer * Allocate(const IpAddress &peer_ip)
const std::string & device_display_name() const
const IpAddress & tor_ip() const
VnEntry * vn() const
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
static bool DeleteTimer(Timer *Timer)
Definition: timer.cc:222
bool Cancel()
Definition: timer.cc:150
bool Start(int time, Handler handler, ErrorHandler error_handler=NULL)
Definition: timer.cc:108
const boost::uuids::uuid & GetUuid() const
Definition: vn.h:162
VrfEntry * GetVrf() const
Definition: vn.h:171
const string & GetName() const
Definition: vn.h:163
Definition: vrf.h:90
AgentRouteTable * GetBridgeRouteTable() const
Definition: vrf.cc:335
bool Enqueue(QueueEntryT entry)
Definition: queue_task.h:249
void Shutdown(bool delete_entries=true)
Definition: queue_task.h:153
void set_name(const std::string &name)
Definition: queue_task.h:308
Definition: agent.h:44
static std::string UuidToString(const boost::uuids::uuid &id)
Definition: string_util.h:139
Definition: task_int.h:11
struct task_ task
boost::uuids::uuid uuid