OpenSDN source code
vlan_port_binding_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 };
10 #include <ovsdb_client.h>
11 #include <ovsdb_client_idl.h>
12 #include <ovsdb_client_session.h>
13 #include <physical_switch_ovsdb.h>
14 #include <logical_switch_ovsdb.h>
15 #include <physical_port_ovsdb.h>
17 #include <vm_interface_ksync.h>
18 
19 #include <oper/vn.h>
20 #include <oper/interface_common.h>
22 #include <ovsdb_sandesh.h>
23 #include <ovsdb_types.h>
24 
25 using namespace OVSDB;
26 
28  const std::string &physical_device, const std::string &physical_port,
29  uint16_t vlan_tag, const std::string &logical_switch) :
30  OvsdbDBEntry(table), logical_switch_name_(logical_switch),
31  physical_port_name_(physical_port), physical_device_name_(physical_device),
32  vlan_(vlan_tag), vmi_uuid_(boost::uuids::nil_uuid()),
33  old_logical_switch_name_(), logical_switch_(NULL, this) {
34 }
35 
37  const VlanLogicalInterface *entry) : OvsdbDBEntry(table),
38  logical_switch_name_(), physical_port_name_(entry->phy_intf_display_name()),
39  physical_device_name_(entry->phy_dev_display_name()), vlan_(entry->vlan()),
40  vmi_uuid_(boost::uuids::nil_uuid()), old_logical_switch_name_(),
41  logical_switch_(NULL, this) {
42 }
43 
45  const VlanPortBindingEntry *key) : OvsdbDBEntry(table),
46  logical_switch_name_(key->logical_switch_name_),
47  physical_port_name_(key->physical_port_name_),
48  physical_device_name_(key->physical_device_name_), vlan_(key->vlan_),
49  vmi_uuid_(boost::uuids::nil_uuid()), old_logical_switch_name_(),
50  logical_switch_(NULL, this) {
51 }
52 
54  if (!logical_switch_name_.empty()) {
55  LogicalSwitchTable *l_table =
57  LogicalSwitchEntry ls_key(l_table, logical_switch_name_.c_str());
59  static_cast<LogicalSwitchEntry *>(l_table->GetReference(&ls_key));
60  } else {
61  logical_switch_ = NULL;
62  }
63 }
64 
66  logical_switch_ = NULL;
67 }
68 
69 void VlanPortBindingEntry::AddMsg(struct ovsdb_idl_txn *txn) {
72  physical_port_ = p_table->GetReference(&key);
73  PhysicalPortEntry *port =
74  static_cast<PhysicalPortEntry *>(physical_port_.get());
75 
76  // update logical switch name propagated to OVSDB server, this will
77  // be used in change Operation to reduce the unncessary computation
78  // when there is change which VlanPortBindingEntry is not
79  // interested into
81  if (!logical_switch_name_.empty()) {
82  port->AddBinding(vlan_,
83  static_cast<LogicalSwitchEntry *>(logical_switch_.get()));
84  OVSDB_TRACE(Trace, "Adding port vlan binding port " +
86  " to Logical Switch " + logical_switch_name_);
87  } else {
88  OVSDB_TRACE(Trace, "Deleting port vlan binding port " +
90  port->DeleteBinding(vlan_, NULL);
91  }
92 
93  // Don't trigger update for stale entries
94  if (!stale()) {
95  port->TriggerUpdate();
96  }
97 }
98 
99 void VlanPortBindingEntry::ChangeMsg(struct ovsdb_idl_txn *txn) {
101  // no change return from here.
102  return;
103  }
104  PhysicalPortEntry *port =
105  static_cast<PhysicalPortEntry *>(physical_port_.get());
106  OVSDB_TRACE(Trace, "Changing port vlan binding port " +
108  port->DeleteBinding(vlan_, NULL);
109 
110  AddMsg(txn);
111 }
112 
113 void VlanPortBindingEntry::DeleteMsg(struct ovsdb_idl_txn *txn) {
114  if (!physical_port_) {
115  return;
116  }
117  PhysicalPortEntry *port =
118  static_cast<PhysicalPortEntry *>(physical_port_.get());
119  OVSDB_TRACE(Trace, "Deleting port vlan binding port " +
121  port->DeleteBinding(vlan_,
122  static_cast<LogicalSwitchEntry *>(logical_switch_.get()));
123  port->TriggerUpdate();
124 }
125 
127  VlanLogicalInterface *entry =
128  static_cast<VlanLogicalInterface *>(db_entry);
129  std::string ls_name =
130  (dynamic_cast<const VlanPortBindingTable *>(table_))->
131  GetLogicalSwitchName(entry);
132  boost::uuids::uuid vmi_uuid(boost::uuids::nil_uuid());
133  bool change = false;
134 
135  if (entry->vm_interface()) {
136  vmi_uuid = entry->vm_interface()->GetUuid();
137  }
138 
139  if (vmi_uuid_ != vmi_uuid) {
140  vmi_uuid_ = vmi_uuid;
141  change = true;
142  }
143 
144  if (ls_name != logical_switch_name_) {
145  logical_switch_name_ = ls_name;
146  change = true;
147  }
148  return change;
149 }
150 
151 bool VlanPortBindingEntry::IsLess(const KSyncEntry &entry) const {
152  const VlanPortBindingEntry &vps_entry =
153  static_cast<const VlanPortBindingEntry&>(entry);
154  if (vlan_ != vps_entry.vlan_)
155  return vlan_ < vps_entry.vlan_;
157  return physical_device_name_ < vps_entry.physical_device_name_;
158  return physical_port_name_ < vps_entry.physical_port_name_;
159 }
160 
162  PhysicalSwitchTable *ps_table =
164  PhysicalSwitchEntry ps_key(ps_table, physical_device_name_.c_str());
165  PhysicalSwitchEntry *p_switch =
166  static_cast<PhysicalSwitchEntry *>(ps_table->GetReference(&ps_key));
167  if (!p_switch->IsResolved()) {
168  OVSDB_TRACE(Trace, "Physical Switch unavailable for Port Vlan Binding "+
170  " to Logical Switch " + logical_switch_name_);
171  return p_switch;
172  }
173 
176  PhysicalPortEntry *p_port =
177  static_cast<PhysicalPortEntry *>(p_table->GetReference(&key));
178  if (!p_port->IsResolved()) {
179  OVSDB_TRACE(Trace, "Physical Port unavailable for Port Vlan Binding " +
181  " to Logical Switch " + logical_switch_name_);
182  return p_port;
183  }
184 
185  // check for VMI only if entry is not stale marked.
186  if (!stale()) {
187  VMInterfaceKSyncObject *vm_intf_table =
189  VMInterfaceKSyncEntry vm_intf_key(vm_intf_table, vmi_uuid_);
190  VMInterfaceKSyncEntry *vm_intf = static_cast<VMInterfaceKSyncEntry *>(
191  vm_intf_table->GetReference(&vm_intf_key));
192  if (!vm_intf->IsResolved()) {
193  OVSDB_TRACE(Trace, "VM Interface unavailable for Port Vlan Binding " +
195  " to Logical Switch " + logical_switch_name_);
196  return vm_intf;
197  } else if (logical_switch_name_.empty()) {
198  // update latest name after resolution.
199  logical_switch_name_ = vm_intf->vn_name();
200  }
201  }
202 
203  if (!logical_switch_name_.empty()) {
204  // Check only if logical switch name is present.
205  LogicalSwitchTable *l_table =
207  LogicalSwitchEntry ls_key(l_table, logical_switch_name_.c_str());
208  LogicalSwitchEntry *ls_entry =
209  static_cast<LogicalSwitchEntry *>(l_table->GetReference(&ls_key));
210  if (!ls_entry->IsResolved()) {
211  OVSDB_TRACE(Trace, "Logical Switch unavailable for Port Vlan "
212  "Binding " + physical_port_name_ + " vlan " +
213  integerToString(vlan_) + " to Logical Switch " +
215  return ls_entry;
216  }
217  }
218 
219  return NULL;
220 }
221 
222 const std::string &VlanPortBindingEntry::logical_switch_name() const {
223  return logical_switch_name_;
224 }
225 
226 const std::string &VlanPortBindingEntry::physical_port_name() const {
227  return physical_port_name_;
228 }
229 
230 const std::string &VlanPortBindingEntry::physical_device_name() const {
231  return physical_device_name_;
232 }
233 
234 uint16_t VlanPortBindingEntry::vlan() const {
235  return vlan_;
236 }
237 
239  OvsdbDBObject(idl, true) {
240 }
241 
243 }
244 
245 KSyncEntry *VlanPortBindingTable::Alloc(const KSyncEntry *key, uint32_t index) {
246  const VlanPortBindingEntry *k_entry =
247  static_cast<const VlanPortBindingEntry *>(key);
248  VlanPortBindingEntry *entry = new VlanPortBindingEntry(this, k_entry);
249  return entry;
250 }
251 
253  const VlanLogicalInterface *entry =
254  static_cast<const VlanLogicalInterface *>(db_entry);
255  VlanPortBindingEntry *key = new VlanPortBindingEntry(this, entry);
256  return static_cast<KSyncEntry *>(key);
257 }
258 
259 std::string
261  const VlanLogicalInterface *entry =
262  dynamic_cast<const VlanLogicalInterface *>(e);
263  std::string ls_name;
264  if (entry->vm_interface() && entry->vm_interface()->vn()) {
265  ls_name = UuidToString(entry->vm_interface()->vn()->GetUuid());
266  }
267 
268  return ls_name;
269 }
270 
272  const DBEntry *entry, const OvsdbDBEntry *ovsdb_entry) {
273  const VlanLogicalInterface *l_port =
274  dynamic_cast<const VlanLogicalInterface *>(entry);
275  if (l_port == NULL) {
276  // Ignore entries other than VLanLogicalInterface.
277  return DBFilterIgnore;
278  }
279 
280  // Logical interface without vm interface is incomplete entry
281  // for ovsdb, trigger delete.
282  if (l_port->vm_interface() == NULL) {
283  if (ovsdb_entry != NULL) {
284  OVSDB_TRACE(Trace, "VM Interface Unavialable, Deleting Logical "
285  "Port " + l_port->name());
286  } else {
287  OVSDB_TRACE(Trace, "VM Interface Unavialable, Ignoring Logical "
288  "Port " + l_port->name());
289  }
290  return DBFilterDelete;
291  }
292 
293  // Since we need physical port name and device name as key, ignore entry
294  // if physical port or device is not yet present.
295  if (l_port->phy_intf_display_name().empty()) {
296  OVSDB_TRACE(Trace, "Ignoring Port Vlan Binding due to physical port "
297  "name unavailablity Logical port = " + l_port->name());
298  return DBFilterIgnore;
299  }
300 
301  if (l_port->phy_dev_display_name().empty()) {
302  OVSDB_TRACE(Trace, "Ignoring Port Vlan Binding due to device name "
303  "unavailablity Logical port = " + l_port->name());
304  return DBFilterIgnore;
305  }
306 
307  // On LS change old vlan-port binding to old LS has to be removed from tor
308  // and new LS need to be added for the same vlan-port
309  if (ovsdb_entry &&
310  (dynamic_cast<const VlanPortBindingEntry*>(ovsdb_entry))->
311  logical_switch_name() != GetLogicalSwitchName(entry)) {
312  return DBFilterDelAdd;
313  }
314 
315  return DBFilterAccept;
316 }
317 
319 // Sandesh routines
322  std::string resp_ctx, AgentSandeshArguments &args) :
323  OvsdbSandeshTask(resp_ctx, args), physical_port_("") {
324  if (false == args.Get("physical_port", &physical_port_)) {
325  physical_port_ = "";
326  }
327 }
328 
330  std::string resp_ctx, const std::string &ip, uint32_t port,
331  const std::string &physical_port) :
332  OvsdbSandeshTask(resp_ctx, ip, port), physical_port_(physical_port) {
333 }
334 
336 }
337 
339  if (!physical_port_.empty()) {
340  args.Add("physical_port", physical_port_);
341  }
342 }
343 
346  if (!physical_port_.empty()) {
347  VlanPortBindingEntry *entry =
348  static_cast<VlanPortBindingEntry *>(kentry);
349  if (entry->physical_port_name().find(
350  physical_port_) != std::string::npos) {
351  return FilterAllow;
352  }
353  return FilterDeny;
354  }
355  return FilterAllow;
356 }
357 
359  SandeshResponse *resp) {
360  VlanPortBindingEntry *entry = static_cast<VlanPortBindingEntry *>(kentry);
361  OvsdbVlanPortBindingEntry oentry;
362  oentry.set_state(entry->StateString());
363  oentry.set_physical_port(entry->physical_port_name());
364  oentry.set_physical_device(entry->physical_device_name());
365  oentry.set_logical_switch(entry->logical_switch_name());
366  oentry.set_vlan(entry->vlan());
367  OvsdbVlanPortBindingResp *v_resp =
368  static_cast<OvsdbVlanPortBindingResp *>(resp);
369  std::vector<OvsdbVlanPortBindingEntry> &bindings =
370  const_cast<std::vector<OvsdbVlanPortBindingEntry>&>(
371  v_resp->get_bindings());
372  bindings.push_back(oentry);
373 }
374 
376  return static_cast<SandeshResponse *>(new OvsdbVlanPortBindingResp());
377 }
378 
379 KSyncObject *
381  return static_cast<KSyncObject *>(
382  session->client_idl()->vlan_port_table());
383 }
384 
385 void OvsdbVlanPortBindingReq::HandleRequest() const {
387  new VlanPortBindingSandeshTask(context(), get_session_remote_ip(),
388  get_session_remote_port(),
389  get_physical_port());
391  scheduler->Enqueue(task);
392 }
bool Add(const std::string &key, const std::string &val)
bool Get(const std::string &key, std::string *val) const
const std::string & name() const
Definition: interface.h:117
const boost::uuids::uuid & GetUuid() const
Definition: interface.h:116
std::string StateString() const
bool IsResolved()
bool stale() const
Definition: ksync_entry.h:180
KSyncEntry * GetReference(const KSyncEntry *key)
const std::string & phy_intf_display_name() const
VmInterface * vm_interface() const
const std::string & phy_dev_display_name() const
PhysicalPortTable * physical_port_table()
PhysicalSwitchTable * physical_switch_table()
VMInterfaceKSyncObject * vm_interface_table()
LogicalSwitchTable * logical_switch_table()
VlanPortBindingTable * vlan_port_table()
OvsdbDBObject * table_
Definition: ovsdb_entry.h:111
OvsdbClientIdl * client_idl()
Definition: ovsdb_object.h:77
void DeleteBinding(int16_t vlan, LogicalSwitchEntry *ls)
void AddBinding(int16_t vlan, LogicalSwitchEntry *ls)
const std::string & vn_name() const
void AddMsg(struct ovsdb_idl_txn *)
bool IsLess(const KSyncEntry &) const
const std::string & physical_port_name() const
const std::string & physical_device_name() const
VlanPortBindingEntry(VlanPortBindingTable *table, const std::string &physical_device, const std::string &physical_port, uint16_t vlan_tag, const std::string &logical_switch)
const std::string & logical_switch_name() const
void ChangeMsg(struct ovsdb_idl_txn *)
void DeleteMsg(struct ovsdb_idl_txn *)
VlanPortBindingSandeshTask(std::string resp_ctx, AgentSandeshArguments &args)
KSyncObject * GetObject(OvsdbClientSession *session)
void UpdateResp(KSyncEntry *kentry, SandeshResponse *resp)
FilterResp Filter(KSyncEntry *entry)
void EncodeArgs(AgentSandeshArguments &args)
DBFilterResp OvsdbDBEntryFilter(const DBEntry *entry, const OvsdbDBEntry *ovsdb_entry)
KSyncEntry * DBToKSyncEntry(const DBEntry *)
std::string GetLogicalSwitchName(const DBEntry *entry) const
VlanPortBindingTable(OvsdbClientIdl *idl)
KSyncEntry * Alloc(const KSyncEntry *key, uint32_t index)
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
const VnEntry * vn() const
const boost::uuids::uuid & GetUuid() const
Definition: vn.h:162
Definition: agent.h:44
#define OVSDB_TRACE(obj,...)
static std::string UuidToString(const boost::uuids::uuid &id)
Definition: string_util.h:139
static const std::string integerToString(const NumberType &num)
Definition: string_util.h:20
Definition: task_int.h:11
boost::uuids::uuid uuid