OpenSDN source code
logical_interface.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 #include <boost/uuid/uuid_io.hpp>
7 #include <vnc_cfg_types.h>
8 #include <cmn/agent_cmn.h>
9 
10 #include <ifmap/ifmap_node.h>
11 #include <cfg/cfg_init.h>
12 #include <oper/agent_sandesh.h>
14 #include <oper/interface_common.h>
15 #include <oper/physical_device.h>
16 #include <oper/logical_interface.h>
17 #include <oper/vm_interface.h>
18 #include <oper/config_manager.h>
19 #include <oper/vn.h>
20 
21 #include <vector>
22 #include <string>
23 
24 using std::string;
25 using std::unique_ptr;
26 using boost::uuids::uuid;
27 
29 // LogicalInterface routines
32  const std::string &name,
33  const boost::uuids::uuid &logical_router_uuid) :
34  Interface(Interface::LOGICAL, uuid, name, NULL, true, logical_router_uuid),
35  display_name_(),
36  physical_interface_(),
37  vm_interface_(),
38  physical_device_(NULL),
39  phy_dev_display_name_(),
40  phy_intf_display_name_(),
41  vn_uuid_() {
42 }
43 
45 }
46 
48  return physical_device_.get();
49 }
50 
52  return UuidToString(uuid_);
53 }
54 
55 bool LogicalInterface::CmpInterface(const DBEntry &rhs) const {
56  const LogicalInterface &a = static_cast<const LogicalInterface &>(rhs);
57  return (uuid_ < a.uuid_);
58 }
59 
61  const LogicalInterfaceData *data) {
62  bool ret = false;
63 
64  if (display_name_ != data->display_name_) {
66  ret = true;
67  }
68 
70  Interface *intf = static_cast<PhysicalInterface *>
71  (table->agent()->interface_table()->FindActiveEntry(&phy_key));
72  if (intf == NULL) {
74  intf = static_cast<RemotePhysicalInterface *>
75  (table->agent()->interface_table()->FindActiveEntry(&rem_key));
76  }
77 
78  if (intf != physical_interface_.get()) {
79  physical_interface_.reset(intf);
80  ret = true;
81  }
82 
84  OPER_TRACE_ENTRY(Trace, table,
85  "Changing Physical Interface display name from " \
86  + phy_intf_display_name_ + " to " +
89  ret = true;
90  }
91 
93  Interface *intrface = static_cast<Interface *>
94  (table->agent()->interface_table()->FindActiveEntry(&vmi_key));
95  if (intrface != vm_interface_.get()) {
96  vm_interface_.reset(intrface);
97  ret = true;
98  }
99 
100  boost::uuids::uuid old_vn_uuid = vn_uuid_;
101  VmInterface *vmi = static_cast<VmInterface *>(vm_interface_.get());
102  if (vm_interface_.get() && vmi->vn()) {
103  vn_uuid_ = vmi->vn()->GetUuid();
104  } else {
105  vn_uuid_ = boost::uuids::nil_uuid();
106  }
107  if (old_vn_uuid != vn_uuid_)
108  ret = true;
109 
110  vm_uuid_ = data->vm_interface_;
111 
112  PhysicalDevice *dev = table->agent()->physical_device_table()->
113  Find(data->device_uuid_);
114  if (dev != physical_device_.get()) {
115  physical_device_.reset(dev);
116  ret = true;
117  }
118 
120  OPER_TRACE_ENTRY(Trace, table,
121  "Changing Physical Device display name from " \
122  + phy_dev_display_name_ + " to " +
123  data->phy_dev_display_name_);
125  ret = true;
126  }
127 
128  return ret;
129 }
130 
132  return true;
133 }
134 
137  os_params_.mac_.Zero();
138  os_params_.os_oper_state_ = true;
139 }
140 
142  return static_cast<VmInterface *>(vm_interface_.get());
143 }
144 
146  return static_cast<Interface *>(physical_interface_.get());
147 }
148 
150 // LogicalInterfaceKey routines
152 
154  const std::string &name) :
155  InterfaceKey(AgentKey::ADD_DEL_CHANGE, Interface::LOGICAL, uuid, name,
156  false) {
157 }
158 
160 }
161 
163 // LogicalInterfaceData routines
166  const std::string &display_name,
167  const std::string &port,
168  const boost::uuids::uuid &vif,
169  const uuid &device_uuid,
170  const std::string &phy_dev_display_name,
171  const std::string &phy_intf_display_name) :
172  InterfaceData(agent, node, Interface::TRANSPORT_INVALID),
173  display_name_(display_name),
174  physical_interface_(port), vm_interface_(vif), device_uuid_(device_uuid),
175  phy_dev_display_name_(phy_dev_display_name),
176  phy_intf_display_name_(phy_intf_display_name) {
177 }
178 
180 }
181 
183 // VlanLogicalInterface routines
186  const std::string &name,
187  uint16_t vlan,
188  const boost::uuids::uuid &logical_router_uuid) :
189  LogicalInterface(uuid, name, logical_router_uuid), vlan_(vlan) {
190 }
191 
193 }
194 
197  return DBEntryBase::KeyPtr(key);
198 }
199 
201 // VlanLogicalInterfaceKey routines
204  const std::string &name) :
205  LogicalInterfaceKey(uuid, name) {
206 }
207 
209 }
210 
213  const {
214  return new VlanLogicalInterface(uuid_, name_, 0, boost::uuids::nil_uuid());
215 }
216 
219  const InterfaceData *d) const {
220  const VlanLogicalInterfaceData *data =
221  static_cast<const VlanLogicalInterfaceData *>(d);
223  data->vlan_,
224  data->logical_router_uuid_);
225 
226  intf->OnChange(table, data);
227  return intf;
228 }
229 
231  return new VlanLogicalInterfaceKey(uuid_, name_);
232 }
233 
235 (Agent *agent, IFMapNode *node, const std::string &display_name,
236  const std::string &physical_interface,
237  const boost::uuids::uuid &vif, const boost::uuids::uuid &u,
238  const std::string &phy_dev_display_name,
239  const std::string &phy_intf_display_name, uint16_t vlan) :
240  LogicalInterfaceData(agent, node, display_name, physical_interface, vif, u,
241  phy_dev_display_name, phy_intf_display_name),
242  vlan_(vlan) {
243 }
244 
246 }
247 
248 #if 0
249 void VlanLogicalInterface::SetSandeshData(SandeshLogicalInterface *data) const {
250  data->set_vlan_tag(vlan_);
251 }
252 #endif
254 // Config handling routines
257  const boost::uuids::uuid &u) {
258  return new VlanLogicalInterfaceKey(u, node->name());
259 }
260 
262  const uuid &u,
263  const autogen::LogicalInterface *port) {
264  // Find link with physical-interface adjacency
265  string physical_interface;
266  string phy_dev_display_name;
267  string phy_intf_display_name;
268  IFMapNode *adj_node = NULL;
269  boost::uuids::uuid dev_uuid = boost::uuids::nil_uuid();
270  adj_node = agent->config_manager()->FindAdjacentIFMapNode(node,
271  "physical-interface");
272  IFMapNode *prouter_node = NULL;
273 
274  if (adj_node) {
275  physical_interface = adj_node->name();
276  autogen::PhysicalInterface *port =
277  static_cast <autogen::PhysicalInterface *>(adj_node->GetObject());
278  assert(port);
279  phy_intf_display_name = port->display_name();
280  prouter_node = agent->config_manager()->
281  FindAdjacentIFMapNode(adj_node, "physical-router");
282  if (prouter_node) {
283  autogen::PhysicalRouter *router =
284  static_cast<autogen::PhysicalRouter *>(prouter_node->GetObject());
285  phy_dev_display_name = router->display_name();
286  autogen::IdPermsType id_perms = router->id_perms();
287  CfgUuidSet(id_perms.uuid.uuid_mslong, id_perms.uuid.uuid_lslong,
288  dev_uuid);
289  }
290  }
291 
292  // Find link with virtual-machine-interface adjacency
293  boost::uuids::uuid vmi_uuid = boost::uuids::nil_uuid();
294  adj_node = agent->config_manager()->FindAdjacentIFMapNode
295  (node, "virtual-machine-interface");
296  if (adj_node) {
297  autogen::VirtualMachineInterface *vmi =
298  static_cast<autogen::VirtualMachineInterface *>
299  (adj_node->GetObject());
300  autogen::IdPermsType id_perms = vmi->id_perms();
301  CfgUuidSet(id_perms.uuid.uuid_mslong, id_perms.uuid.uuid_lslong,
302  vmi_uuid);
303  }
304 
305  string dev_name;
306  adj_node = agent->config_manager()->FindAdjacentIFMapNode
307  (node, "physical-router");
308  if (adj_node) {
309  dev_name = adj_node->name();
310  if (dev_uuid != boost::uuids::nil_uuid()) {
311  IFMAP_ERROR(LogicalInterfaceConfiguration,
312  "Both physical-router and physical-interface links for "
313  "interface:", node->name(),
314  "physical interface", physical_interface,
315  "prouter name", dev_name);
316  }
317  autogen::PhysicalRouter *router =
318  static_cast<autogen::PhysicalRouter *>(adj_node->GetObject());
319  autogen::IdPermsType id_perms = router->id_perms();
320  CfgUuidSet(id_perms.uuid.uuid_mslong, id_perms.uuid.uuid_lslong,
321  dev_uuid);
322  }
323 
324  // Logical-Interface must have VLAN-Tag field. Ignore the interface if
325  // VLAN-Tag is not yet present
326  if (port->IsPropertySet(autogen::LogicalInterface::VLAN_TAG) == false) {
327  OperConfigInfo t;
328  t.set_name(node->name());
329  t.set_uuid(UuidToString(u));
330  t.set_message("VLAN-Tag property not set. Ignoring node");
331 
332  OPER_IFMAP_TRACE(Config, t);
333  return NULL;
334  }
335 
336  return new VlanLogicalInterfaceData(agent, node, port->display_name(),
337  physical_interface, vmi_uuid, dev_uuid,
338  phy_dev_display_name,
339  phy_intf_display_name,
340  port->vlan_tag());
341 }
342 
344  boost::uuids::uuid &u) {
345 
346  autogen::LogicalInterface *port =
347  static_cast <autogen::LogicalInterface *>(node->GetObject());
348  assert(port);
349 
350  autogen::IdPermsType id_perms = port->id_perms();
351  CfgUuidSet(id_perms.uuid.uuid_mslong, id_perms.uuid.uuid_lslong, u);
352  return true;
353 }
354 
356  DBRequest &req,
357  const boost::uuids::uuid &u) {
358  autogen::LogicalInterface *port =
359  static_cast <autogen::LogicalInterface *>(node->GetObject());
360  assert(port);
361 
362  req.key.reset(BuildKey(node, u));
363  if (node->IsDeleted()) {
365  return true;
366  }
367 
369  req.data.reset(BuildData(agent(), node, u, port));
370 
371  if (req.data.get() != NULL) {
373  Enqueue(&req);
374  }
375  return false;
376 }
377 
379  DBRequest &req,
380  const boost::uuids::uuid &u) {
381  autogen::LogicalInterface *port =
382  static_cast <autogen::LogicalInterface *>(node->GetObject());
383  assert(port);
384 
385  req.key.reset(BuildKey(node, u));
386  if (req.oper == DBRequest::DB_ENTRY_DELETE || node->IsDeleted()) {
388  return true;
389  }
390 
392  return false;
393 }
#define OPER_IFMAP_TRACE(obj,...)
Definition: agent_cmn.h:89
#define IFMAP_ERROR(obj,...)
Definition: agent_cmn.h:94
static void CfgUuidSet(uint64_t ms_long, uint64_t ls_long, boost::uuids::uuid &u)
Definition: agent_cmn.h:67
#define OPER_TRACE_ENTRY(obj, table,...)
Definition: agent_db.h:237
AgentDBEntry * FindActiveEntry(const DBEntry *key)
Definition: agent_db.cc:111
Definition: agent.h:361
InterfaceTable * interface_table() const
Definition: agent.h:468
ConfigManager * config_manager() const
Definition: agent.cc:893
PhysicalDeviceTable * physical_device_table() const
Definition: agent.h:631
void AddLogicalInterfaceNode(IFMapNode *node)
IFMapNode * FindAdjacentIFMapNode(IFMapNode *node, const char *type)
std::unique_ptr< DBRequestKey > KeyPtr
Definition: db_entry.h:25
bool IsDeleted() const
Definition: db_entry.h:49
void Delete()
Definition: db_entry.cc:132
bool Enqueue(DBRequest *req)
Definition: db_table.cc:221
const std::string & name() const
Definition: ifmap_node.h:49
IFMapObject * GetObject()
Definition: ifmap_node.cc:64
bool LogicalInterfaceProcessConfig(IFMapNode *node, DBRequest &req, const boost::uuids::uuid &u)
bool LogicalInterfaceIFNodeToUuid(IFMapNode *node, boost::uuids::uuid &u)
bool LogicalInterfaceIFNodeToReq(IFMapNode *node, DBRequest &req, const boost::uuids::uuid &u)
uint32_t li_ifnode_to_req_
Definition: interface.h:499
Agent * agent() const
Definition: interface.h:450
boost::uuids::uuid uuid_
Definition: interface.h:165
const std::string & name() const
Definition: interface.h:117
struct InterfaceOsParams os_params_
Definition: interface.h:191
static const uint32_t kInvalidIndex
Definition: interface.h:73
boost::uuids::uuid vn_uuid_
InterfaceRef vm_interface_
virtual ~LogicalInterface()
PhysicalDeviceRef physical_device_
virtual bool CmpInterface(const DBEntry &rhs) const
virtual void GetOsParams(Agent *agent)
std::string phy_dev_display_name_
boost::uuids::uuid vm_uuid_
std::string display_name_
PhysicalDevice * physical_device() const
VmInterface * vm_interface() const
InterfaceRef physical_interface_
std::string phy_intf_display_name_
virtual bool OnChange(const InterfaceTable *table, const LogicalInterfaceData *data)
Interface * physical_interface() const
virtual std::string ToString() const
LogicalInterface(const boost::uuids::uuid &uuid, const std::string &name, const boost::uuids::uuid &logical_router_uuid)
void Zero()
Definition: mac_address.h:132
Definition: trace.h:289
VlanLogicalInterface(const boost::uuids::uuid &uuid, const std::string &name, uint16_t vlan, const boost::uuids::uuid &lr_uuid)
virtual DBEntryBase::KeyPtr GetDBRequestKey() const
const VnEntry * vn() const
const boost::uuids::uuid & GetUuid() const
Definition: vn.h:162
static LogicalInterfaceKey * BuildKey(IFMapNode *node, const boost::uuids::uuid &u)
static LogicalInterfaceData * BuildData(Agent *agent, IFMapNode *node, const uuid &u, const autogen::LogicalInterface *port)
static std::string UuidToString(const boost::uuids::uuid &id)
Definition: string_util.h:139
@ ADD_DEL_CHANGE
Definition: agent_db.h:101
DBOperation oper
Definition: db_table.h:43
@ DB_ENTRY_DELETE
Definition: db_table.h:40
@ 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
boost::uuids::uuid logical_router_uuid_
Definition: interface.h:270
boost::uuids::uuid uuid_
Definition: interface.h:247
std::string name_
Definition: interface.h:248
LogicalInterfaceData(Agent *agent, IFMapNode *node, const std::string &display_name, const std::string &physical_interface, const boost::uuids::uuid &vif, const boost::uuids::uuid &device_uuid, const std::string &phy_dev_display_name, const std::string &phy_intf_display_name)
std::string phy_dev_display_name_
boost::uuids::uuid device_uuid_
std::string physical_interface_
std::string phy_intf_display_name_
boost::uuids::uuid vm_interface_
LogicalInterfaceKey(const boost::uuids::uuid &uuid, const std::string &name)
VlanLogicalInterfaceData(Agent *agent, IFMapNode *node, const std::string &display_name, const std::string &physical_interface, const boost::uuids::uuid &vif, const boost::uuids::uuid &device_uuid, const std::string &phy_dev_display_name, const std::string &phy_intf_display_name, uint16_t vlan)
virtual InterfaceKey * Clone() const
VlanLogicalInterfaceKey(const boost::uuids::uuid &uuid, const std::string &name)
virtual LogicalInterface * AllocEntry(const InterfaceTable *table) const
boost::uuids::uuid uuid