OpenSDN source code
physical_port_ovsdb.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 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 
10 #include <base/task.h>
11 
13 #include <ovsdb_client.h>
14 #include <ovsdb_client_idl.h>
15 #include <ovsdb_client_session.h>
16 
17 #include <logical_switch_ovsdb.h>
18 #include <physical_port_ovsdb.h>
20 #include <ovsdb_sandesh.h>
21 #include <ovsdb_types.h>
22 
23 using namespace OVSDB;
24 
26  const std::string &dev_name, const std::string &name) :
27  OvsdbEntry(table), name_(name), dev_name_(dev_name), binding_table_(),
28  ovs_binding_table_() {
29 }
30 
32 }
33 
35  if (ovs_entry_ == NULL) {
36  return true;
37  }
38 
39  return OverrideOvs();
40 }
41 
43  return Add();
44 }
45 
47  // Nothing to do on delete, return true and be done
48  return true;
49 }
50 
51 bool PhysicalPortEntry::IsLess(const KSyncEntry &entry) const {
52  const PhysicalPortEntry &ps_entry =
53  static_cast<const PhysicalPortEntry&>(entry);
54  if (dev_name_ != ps_entry.dev_name_) {
55  return (dev_name_.compare(ps_entry.dev_name_) < 0);
56  }
57 
58  return (name_.compare(ps_entry.name_) < 0);
59 }
60 
62  return NULL;
63 }
64 
66  if (!IsResolved()) {
67  /*
68  * we can only modify the vlan bindings in physical port
69  * table as we don't own the table, we are not suppose to create
70  * a new port entry in the table, so return from here if entry is
71  * not resolved
72  */
73  return;
74  }
75  table_->Change(this);
76 }
77 
78 void PhysicalPortEntry::Encode(struct ovsdb_idl_txn *txn) {
79  struct ovsdb_wrapper_port_vlan_binding binding[binding_table_.size()];
80  VlanLSTable::iterator it = binding_table_.begin();
81  std::size_t i = 0;
82  for ( ; it != binding_table_.end(); it++) {
83  struct ovsdb_idl_row *ls = it->second->ovs_entry();
84  if (ls != NULL) {
85  binding[i].ls = ls;
86  binding[i].vlan = it->first;
87  i++;
88  }
89  }
91 }
92 
94  binding_table_[vlan] = ls;
95 }
96 
98  binding_table_.erase(vlan);
99 }
100 
101 const std::string &PhysicalPortEntry::name() const {
102  return name_;
103 }
104 
105 const std::string &PhysicalPortEntry::dev_name() const {
106  return dev_name_;
107 }
108 
111  return binding_table_;
112 }
113 
116  return ovs_binding_table_;
117 }
118 
121  return stats_table_;
122 }
123 
125  struct ovsdb_idl_txn *txn =
127  if (txn == NULL) {
128  // failed to create transaction because of idl marked for
129  // deletion return from here.
130  return true;
131  }
132  Encode(txn);
133  struct jsonrpc_msg *msg = ovsdb_wrapper_idl_txn_encode(txn);
134  if (msg == NULL) {
135  table_->client_idl()->DeleteTxn(txn);
136  return true;
137  }
138  OVSDB_TRACE(Trace, "Sending Vlan Port Binding update for Physical route " +
139  dev_name_ + " Physical Port " + name_);
141  return false;
142 }
143 
145  OvsdbObject(idl), stale_create_done_(false) {
147  boost::bind(&PhysicalPortTable::Notify, this, _1, _2));
148 }
149 
151 }
152 
154  struct ovsdb_idl_row *row) {
155  if (op == OvsdbClientIdl::OVSDB_DEL) {
156  DeletePortEntry(row);
157  } else if (op == OvsdbClientIdl::OVSDB_ADD) {
158  PhysicalPortEntry *entry = FindPortEntry(row);
159  if (entry) {
160  EntryOvsdbUpdate(entry);
161  }
162  }
163 }
164 
165 KSyncEntry *PhysicalPortTable::Alloc(const KSyncEntry *key, uint32_t index) {
166  const PhysicalPortEntry *k_entry =
167  static_cast<const PhysicalPortEntry *>(key);
168  PhysicalPortEntry *entry = new PhysicalPortEntry(this, k_entry->dev_name_,
169  k_entry->name_);
170  return entry;
171 }
172 
173 void PhysicalPortTable::CreatePortEntry(struct ovsdb_idl_row *row,
174  const std::string &physical_device) {
175  assert(FindPortEntry(row) == NULL);
176  // Create a port entry and add to the idl entry map
177  PhysicalPortEntry key(this, physical_device,
179  PhysicalPortEntry *entry = static_cast<PhysicalPortEntry *>(Find(&key));
180  if (entry == NULL) {
181  OVSDB_TRACE(Trace, "Add/Change of Physical Port " +
182  std::string(ovsdb_wrapper_physical_port_name(row)));
183  entry = static_cast<PhysicalPortEntry *>(Create(&key));
184  entry->ovs_entry_ = row;
185  } else if (!entry->IsActive()) {
186  // entry is present but it is a temp entry.
187  OVSDB_TRACE(Trace, "Add/Change of Physical Port " +
188  std::string(ovsdb_wrapper_physical_port_name(row)));
189  Change(entry);
190  // Set row pointer after triggering change to activate entry
191  // so that message is not encoded.
192  entry->ovs_entry_ = row;
193  }
194  EntryOvsdbUpdate(entry);
195  idl_entry_map_[row] = entry;
196 }
197 
199  IdlEntryMap::iterator it = idl_entry_map_.find(row);
200  if (it != idl_entry_map_.end()) {
201  return it->second;
202  }
203 
204  return NULL;
205 }
206 
207 void PhysicalPortTable::DeletePortEntry(struct ovsdb_idl_row *row) {
208  PhysicalPortEntry *entry = FindPortEntry(row);
209  if (entry == NULL) {
210  return;
211  }
212 
213  OVSDB_TRACE(Trace, "Delete of Physical Port " +
214  std::string(ovsdb_wrapper_physical_port_name(row)));
215  idl_entry_map_.erase(row);
216  entry->ovs_entry_ = NULL;
217  Delete(entry);
218 }
219 
221  stale_create_done_ = true;
222 }
223 
225  std::size_t count =
227  struct ovsdb_wrapper_port_vlan_binding new_bind[count];
229 
230  // clear the old ovs_binding_table and fill new entries.
231  entry->ovs_binding_table_.clear();
232  for (std::size_t i = 0; i < count; i++) {
233  LogicalSwitchEntry key(client_idl_->logical_switch_table(),
235  LogicalSwitchEntry *ls_entry =
236  static_cast<LogicalSwitchEntry *>(
237  client_idl_->logical_switch_table()->Find(&key));
238  if (ls_entry != NULL) {
239  entry->ovs_binding_table_[new_bind[i].vlan] = ls_entry;
240  }
241  }
242 
243  // Compare difference between tor agent and ovsdb server.
244  // on mis-match override and re-program Physical port
245  PhysicalPortEntry::VlanLSTable::iterator it =
246  entry->binding_table_.begin();
247  PhysicalPortEntry::VlanLSTable::iterator ovs_it =
248  entry->ovs_binding_table_.begin();
249  VlanPortBindingTable *vp_binding_table = client_idl_->vlan_port_table();
250  bool ret_override = false;
251  while (ovs_it != entry->ovs_binding_table_.end()) {
252  if (it != entry->binding_table_.end() && it->first == ovs_it->first) {
253  if (it->second != ovs_it->second) {
254  // mis-match of logical switch for the vlan
255  //break;
256  ret_override = true;
257  }
258  it++;
259  ovs_it++;
260  } else {
261  ret_override = true;
262  // mis-match of vlans in binding
263  if (stale_create_done_) {
264  // done creating stale entries, bail out and override
265  break;
266  }
267  if (it == entry->binding_table_.end() || it->first > ovs_it->first) {
268  // Create stale vlan port binding entry
269  VlanPortBindingEntry key(vp_binding_table, entry->dev_name(),
270  entry->name(), ovs_it->first,
271  ovs_it->second->name());
272  vp_binding_table->CreateStale(&key);
273  ovs_it++;
274  } else {
275  it++;
276  }
277  }
278  }
279 
280  if (entry->binding_table_.size() != entry->ovs_binding_table_.size()) {
281  // we need to anyway override OVSDB if the table size are different
282  ret_override = true;
283  }
284 
286  struct ovsdb_wrapper_port_vlan_stats stats[count];
288  entry->stats_table_.clear();
289  for (std::size_t i = 0; i < count; i++) {
290  entry->stats_table_[stats[i].vlan] = stats[i].stats;
291  }
292 
293  if (ret_override) {
294  // change entry to update vlan port bindings
295  Change(entry);
296  }
297 }
298 
300 // Sandesh routines
303  AgentSandeshArguments &args) :
304  OvsdbSandeshTask(resp_ctx, args), name_("") {
305  if (false == args.Get("name", &name_)) {
306  name_ = "";
307  }
308 }
309 
311  const std::string &ip,
312  uint32_t port,
313  const std::string &name) :
314  OvsdbSandeshTask(resp_ctx, ip, port), name_(name) {
315 }
316 
318 }
319 
321  if (!name_.empty()) {
322  args.Add("name", name_);
323  }
324 }
325 
328  if (!name_.empty()) {
329  PhysicalPortEntry *entry = static_cast<PhysicalPortEntry *>(kentry);
330  if (entry->name().find(name_) != std::string::npos) {
331  return FilterAllow;
332  }
333  return FilterDeny;
334  }
335  return FilterAllow;
336 }
337 
339  SandeshResponse *resp) {
340  PhysicalPortEntry *entry = static_cast<PhysicalPortEntry *>(kentry);
341  OvsdbPhysicalPortEntry pentry;
342  pentry.set_state(entry->StateString());
343  pentry.set_switch_name(entry->dev_name());
344  pentry.set_name(entry->name());
345  const PhysicalPortEntry::VlanLSTable &bindings =
346  entry->binding_table();
347  const PhysicalPortEntry::VlanStatsTable &stats_table =
348  entry->stats_table();
349  PhysicalPortEntry::VlanLSTable::const_iterator it =
350  bindings.begin();
351  std::vector<OvsdbPhysicalPortVlanInfo> vlan_list;
352  for (; it != bindings.end(); it++) {
353  OvsdbPhysicalPortVlanInfo vlan;
354  vlan.set_vlan(it->first);
355  vlan.set_logical_switch(it->second->name());
356  PhysicalPortEntry::VlanStatsTable::const_iterator stats_it =
357  stats_table.find(it->first);
358  if (stats_it != stats_table.end()) {
359  int64_t in_pkts, in_bytes, out_pkts, out_bytes;
361  &in_pkts, &in_bytes, &out_pkts, &out_bytes);
362  vlan.set_in_pkts(in_pkts);
363  vlan.set_in_bytes(in_bytes);
364  vlan.set_out_pkts(out_pkts);
365  vlan.set_out_bytes(out_bytes);
366  } else {
367  vlan.set_in_pkts(0);
368  vlan.set_in_bytes(0);
369  vlan.set_out_pkts(0);
370  vlan.set_out_bytes(0);
371  }
372  vlan_list.push_back(vlan);
373  }
374  pentry.set_vlans(vlan_list);
375 
376  OvsdbPhysicalPortResp *port_resp =
377  static_cast<OvsdbPhysicalPortResp *>(resp);
378  std::vector<OvsdbPhysicalPortEntry> &port_list =
379  const_cast<std::vector<OvsdbPhysicalPortEntry>&>(
380  port_resp->get_port());
381  port_list.push_back(pentry);
382 }
383 
385  return static_cast<SandeshResponse *>(new OvsdbPhysicalPortResp());
386 }
387 
389  return static_cast<KSyncObject *>(
390  session->client_idl()->physical_port_table());
391 }
392 
393 void OvsdbPhysicalPortReq::HandleRequest() const {
395  new PhysicalPortSandeshTask(context(), get_session_remote_ip(),
396  get_session_remote_port(),
397  get_name());
399  scheduler->Enqueue(task);
400 }
401 
bool Add(const std::string &key, const std::string &val)
bool Get(const std::string &key, std::string *val) const
bool IsActive()
Definition: ksync_entry.h:191
std::string StateString() const
bool IsResolved()
KSyncEntry * Find(const KSyncEntry *key)
void Change(KSyncEntry *entry)
KSyncEntry * CreateStale(const KSyncEntry *key)
KSyncEntry * Create(const KSyncEntry *key)
void Delete(KSyncEntry *entry)
PhysicalPortTable * physical_port_table()
struct ovsdb_idl_txn * CreateTxn(OvsdbEntryBase *entry, KSyncEntry::KSyncEvent ack_event=KSyncEntry::ADD_ACK)
void TxnScheduleJsonRpc(struct jsonrpc_msg *msg)
void DeleteTxn(struct ovsdb_idl_txn *txn)
void Register(EntryType type, NotifyCB cb)
OvsdbObject * table_
Definition: ovsdb_entry.h:53
struct ovsdb_idl_row * ovs_entry_
Definition: ovsdb_entry.h:54
struct ovsdb_idl_row * ovs_entry()
Definition: ovsdb_entry.h:48
OvsdbClientIdlPtr client_idl_
Definition: ovsdb_object.h:35
OvsdbClientIdl * client_idl()
Definition: ovsdb_object.h:32
const VlanStatsTable & stats_table() const
void Encode(struct ovsdb_idl_txn *)
PhysicalPortEntry(PhysicalPortTable *table, const std::string &dev_name, const std::string &name)
void DeleteBinding(int16_t vlan, LogicalSwitchEntry *ls)
const VlanLSTable & binding_table() const
std::map< uint32_t, struct ovsdb_idl_row * > VlanStatsTable
void AddBinding(int16_t vlan, LogicalSwitchEntry *ls)
const VlanLSTable & ovs_binding_table() const
const std::string & dev_name() const
std::map< uint32_t, LogicalSwitchEntry * > VlanLSTable
const std::string & name() const
bool IsLess(const KSyncEntry &) const
void EncodeArgs(AgentSandeshArguments &args)
FilterResp Filter(KSyncEntry *entry)
void UpdateResp(KSyncEntry *kentry, SandeshResponse *resp)
PhysicalPortSandeshTask(std::string resp_ctx, AgentSandeshArguments &args)
KSyncObject * GetObject(OvsdbClientSession *session)
PhysicalPortTable(OvsdbClientIdl *idl)
void DeletePortEntry(struct ovsdb_idl_row *row)
KSyncEntry * Alloc(const KSyncEntry *key, uint32_t index)
void CreatePortEntry(struct ovsdb_idl_row *row, const std::string &physical_device)
void EntryOvsdbUpdate(PhysicalPortEntry *entry)
PhysicalPortEntry * FindPortEntry(struct ovsdb_idl_row *row)
void Notify(OvsdbClientIdl::Op, struct ovsdb_idl_row *)
The TaskScheduler keeps track of what tasks are currently schedulable. When a task is enqueued it is ...
Definition: task.h:307
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: agent.h:44
#define OVSDB_TRACE(obj,...)
void ovsdb_wrapper_get_logical_binding_stats(struct ovsdb_idl_row *row, int64_t *in_pkts, int64_t *in_bytes, int64_t *out_pkts, int64_t *out_bytes)
size_t ovsdb_wrapper_physical_port_vlan_stats_count(struct ovsdb_idl_row *row)
size_t ovsdb_wrapper_physical_port_vlan_binding_count(struct ovsdb_idl_row *row)
char * ovsdb_wrapper_physical_port_name(struct ovsdb_idl_row *row)
void ovsdb_wrapper_update_physical_port(struct ovsdb_idl_txn *, struct ovsdb_idl_row *, struct ovsdb_wrapper_port_vlan_binding *, size_t binding_count)
void ovsdb_wrapper_physical_port_vlan_stats(struct ovsdb_idl_row *row, struct ovsdb_wrapper_port_vlan_stats *)
char * ovsdb_wrapper_logical_switch_name(struct ovsdb_idl_row *row)
void ovsdb_wrapper_physical_port_vlan_binding(struct ovsdb_idl_row *row, struct ovsdb_wrapper_port_vlan_binding *)
struct jsonrpc_msg * ovsdb_wrapper_idl_txn_encode(struct ovsdb_idl_txn *txn)
struct ovsdb_idl_row * ls
Definition: ovsdb_wrapper.h:13
struct ovsdb_idl_row * stats
Definition: ovsdb_wrapper.h:18
Definition: task_int.h:11