OpenSDN source code
linux_vxlan.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 #include <cmn/agent_cmn.h>
6 
7 #include <ksync/ksync_index.h>
8 #include <ksync/ksync_entry.h>
9 #include <ksync/ksync_object.h>
10 
11 #include <vnc_cfg_types.h>
12 #include <bgp_schema_types.h>
13 #include <agent_types.h>
14 
15 #include <oper/peer.h>
16 #include <oper/vrf.h>
17 #include <oper/interface_common.h>
18 #include <oper/nexthop.h>
19 #include <oper/multicast.h>
20 #include <oper/vn.h>
21 #include <oper/mirror_table.h>
22 #include <oper/vxlan.h>
23 #include <oper/mpls.h>
24 #include <oper/route_common.h>
25 #include <oper/bridge_route.h>
26 
31 
32 #include "linux_vxlan.h"
33 #include "linux_bridge.h"
34 #include "linux_port.h"
35 #include "linux_fdb.h"
36 
41 }
42 
44 }
45 
46 static void Execute(const string &str) {
47  std::cout << str << std::endl;
48  if (system(str.c_str()) < 0) {
49  LOG(ERROR, "Error executing command : " << str);
50  }
51 }
52 
53 /****************************************************************************
54  * Implementation of KSyncLinuxBridgeEntry
55  ****************************************************************************/
57  const VxLanId *vxlan) :
58  KSyncVxlanBridgeEntry(obj, vxlan) {
59  std::stringstream s;
60  s << "br-" << vxlan_id();
61  name_ = s.str();
62 
63  std::stringstream s1;
64  s1 << "vxlan-" << vxlan_id();
65  vxlan_port_name_ = s1.str();
66 }
67 
69  const KSyncLinuxBridgeEntry *entry) :
70  KSyncVxlanBridgeEntry(obj, entry) {
71  std::stringstream s;
72  s << "br-" << vxlan_id();
73  name_ = s.str();
74 
75  std::stringstream s1;
76  s1 << "vxlan-" << vxlan_id();
77  vxlan_port_name_ = s1.str();
78 }
79 
81  std::stringstream s;
82  s << "brctl addbr " << name_;
83  Execute(s.str());
84 
85  s.str("");
86  s << "ip link add " << vxlan_port_name_ << " type vxlan id " << vxlan_id()
87  << " group 239.1.1.1 dstport 0";
88  Execute(s.str());
89 
90  s.str("");
91  s << "brctl addif " << name_ << " " << vxlan_port_name_;
92  Execute(s.str());
93 
94  s.str("");
95  s << "ifconfig " << name_ << " up";
96  Execute(s.str());
97 
98  s.str("");
99  s << "ifconfig " << vxlan_port_name_ << " up";
100  Execute(s.str());
101 
102  return true;
103 }
104 
106  std::stringstream s;
107  s << "brctl delif " << name_ << " " << vxlan_port_name_;
108  Execute(s.str());
109 
110  s.str("");
111  s << "ip link del " << vxlan_port_name_ << " type vxlan id " << vxlan_id();
112  Execute(s.str());
113 
114  s.str("");
115  s << " brctl delbr " << name_;
116  Execute(s.str());
117  return true;
118 }
119 
120 /****************************************************************************
121  * Implementation of KSyncLinuxBridgeObject
122  ****************************************************************************/
124  KSyncVxlanBridgeObject(ksync) {
125 }
126 
128  uint32_t index) {
129  const KSyncLinuxBridgeEntry *bridge =
130  static_cast<const KSyncLinuxBridgeEntry *>(entry);
131  return new KSyncLinuxBridgeEntry(this, bridge);
132 }
133 
135  const VxLanId *vxlan = static_cast<const VxLanId *>(e);
136  return static_cast<KSyncEntry *>(new KSyncLinuxBridgeEntry(this, vxlan));
137 }
138 
139 /****************************************************************************
140  * Implementation of KSyncLinuxPortEntry
141  ****************************************************************************/
143  const Interface *intrface) :
144  KSyncVxlanPortEntry(obj, intrface), old_bridge_(nullptr) {
145 }
146 
148  const KSyncLinuxPortEntry *entry) :
149  KSyncVxlanPortEntry(obj, entry), old_bridge_(nullptr) {
150 }
151 
153  const KSyncLinuxBridgeEntry *br =
154  dynamic_cast<const KSyncLinuxBridgeEntry *>(bridge());
155 
156  if (br == old_bridge_) {
157  return true;
158  }
159 
160  std::stringstream s;
161  if (old_bridge_) {
162  s << "brctl delif " << old_bridge_->name() << " " << port_name();
163  Execute(s.str());
164  }
165 
166  if (br) {
167  s.str("");
168  old_bridge_ = br;
169  s << "brctl addif " << br->name() << " " << port_name();
170  Execute(s.str());
171  }
172  return true;
173 }
174 
176  return Add();
177 }
178 
180  if (old_bridge_ == nullptr)
181  return true;
182 
183  std::stringstream s;
184  s << "brctl delif " << old_bridge_->name() << " " << port_name();
185  Execute(s.str());
186  return true;
187 }
188 
189 /****************************************************************************
190  * Implementation of KSyncLinuxPortObject
191  ****************************************************************************/
193  KSyncVxlanPortObject(ksync) {
194 }
195 
197  uint32_t index) {
198  const KSyncLinuxPortEntry *intrface =
199  static_cast<const KSyncLinuxPortEntry *>(entry);
200  return new KSyncLinuxPortEntry(this, intrface);
201 }
202 
204  const Interface *intrface = static_cast<const Interface *>(e);
205 
206  switch (intrface->type()) {
207  case Interface::PHYSICAL:
209  return new KSyncLinuxPortEntry(this, intrface);
210  break;
211 
212  default:
213  assert(0);
214  break;
215  }
216  return nullptr;
217 }
218 
219 /****************************************************************************
220  * Implementation of KSyncLinuxFdbEntry
221  ****************************************************************************/
223  const KSyncLinuxFdbEntry *entry) :
224  KSyncVxlanFdbEntry(obj, entry) {
225 }
226 
228  const BridgeRouteEntry *route) :
229  KSyncVxlanFdbEntry(obj, route) {
230 }
231 
233 }
234 
235 static void MacToStr(char *buff, const MacAddress &mac) {
236  sprintf(buff, "%02x:%02x:%02x:%02x:%02x:%02x",
237  mac[0],
238  mac[1],
239  mac[2],
240  mac[3],
241  mac[4],
242  mac[5]);
243 }
244 
246  char buff[64];
247  MacToStr(buff, mac());
248 
249  if (port() != nullptr) {
250  std::stringstream s;
251  s << "bridge fdb add " << buff << " dev " << port()->port_name()
252  << " master";
253  //Execute(s.str());
254  } else if (tunnel_dest().to_ulong() != 0) {
255  const KSyncLinuxBridgeEntry *br =
256  dynamic_cast<const KSyncLinuxBridgeEntry *>(bridge());
257 
258  std::stringstream s;
259  s << "bridge fdb add " << buff << " dst " << tunnel_dest() << " dev "
260  << br->vxlan_port_name();
261  Execute(s.str());
262  }
263 
264  return true;
265 }
266 
268  return Add();
269 }
270 
272  char buff[64];
273  MacToStr(buff, mac());
274 
275  if (port() != nullptr) {
276  std::stringstream s;
277  s << "bridge fdb del " << buff << " dev " << port()->port_name()
278  << " master";
279  //Execute(s.str());
280  } else if (tunnel_dest().to_ulong() != 0) {
281  const KSyncLinuxBridgeEntry *br =
282  dynamic_cast<const KSyncLinuxBridgeEntry *>(bridge());
283 
284  std::stringstream s;
285  s << "bridge fdb del " << buff << " dev " << br->vxlan_port_name();
286  Execute(s.str());
287  }
288 
289  return true;
290 }
291 
292 /****************************************************************************
293  * Implementation of KSyncLinuxFdbObject
294  ****************************************************************************/
296  AgentRouteTable *rt_table) :
297  KSyncVxlanRouteObject(vrf, rt_table) {
298 }
299 
301 }
302 
304  uint32_t index) {
305  const KSyncLinuxFdbEntry *fdb =
306  static_cast<const KSyncLinuxFdbEntry *>(entry);
307  return new KSyncLinuxFdbEntry(this, fdb);
308 }
309 
311  const BridgeRouteEntry *fdb = static_cast<const BridgeRouteEntry *>(e);
312  return new KSyncLinuxFdbEntry(this, fdb);
313 }
314 
315 /****************************************************************************
316  * Implementation of KSyncLinuxVrfObject
317  ****************************************************************************/
319  KSyncVxlanVrfObject(ksync) {
320 }
321 
323 }
324 
326 (const VrfEntry *vrf) {
327  return new KSyncLinuxFdbObject(this, vrf->GetBridgeRouteTable());
328 }
Agent supports multiple route tables - Inet-unicast (IPv4/IPv6), Inet-multicast, bridge,...
Definition: agent_route.h:110
Definition: agent.h:361
@ VM_INTERFACE
Definition: interface.h:38
@ PHYSICAL
Definition: interface.h:32
Type type() const
Definition: interface.h:115
const std::string name() const
Definition: linux_bridge.h:36
std::string vxlan_port_name_
Definition: linux_bridge.h:47
KSyncLinuxBridgeEntry(KSyncLinuxBridgeObject *obj, const VxLanId *vxlan)
Definition: linux_vxlan.cc:56
const std::string vxlan_port_name() const
Definition: linux_bridge.h:37
KSyncEntry * DBToKSyncEntry(const DBEntry *e)
Definition: linux_vxlan.cc:134
KSyncEntry * Alloc(const KSyncEntry *entry, uint32_t index)
Definition: linux_vxlan.cc:127
KSyncLinuxBridgeObject(KSyncLinuxVxlan *ksync)
Definition: linux_vxlan.cc:123
KSyncLinuxFdbEntry(KSyncLinuxFdbObject *obj, const BridgeRouteEntry *route)
Definition: linux_vxlan.cc:227
virtual ~KSyncLinuxFdbEntry()
Definition: linux_vxlan.cc:232
KSyncEntry * Alloc(const KSyncEntry *entry, uint32_t index)
Definition: linux_vxlan.cc:303
virtual ~KSyncLinuxFdbObject()
Definition: linux_vxlan.cc:300
KSyncEntry * DBToKSyncEntry(const DBEntry *e)
Definition: linux_vxlan.cc:310
KSyncLinuxFdbObject(KSyncLinuxVrfObject *vrf, AgentRouteTable *rt_table)
Definition: linux_vxlan.cc:295
const KSyncLinuxBridgeEntry * old_bridge_
Definition: linux_port.h:46
KSyncLinuxPortEntry(KSyncLinuxPortObject *obj, const Interface *intrface)
Definition: linux_vxlan.cc:142
KSyncLinuxPortObject(KSyncLinuxVxlan *ksync)
Definition: linux_vxlan.cc:192
virtual KSyncEntry * DBToKSyncEntry(const DBEntry *e)
Definition: linux_vxlan.cc:203
virtual KSyncEntry * Alloc(const KSyncEntry *entry, uint32_t index)
Definition: linux_vxlan.cc:196
KSyncVxlanRouteObject * AllocBridgeRouteTable(const VrfEntry *entry)
Definition: linux_vxlan.cc:326
KSyncLinuxVrfObject(KSyncLinuxVxlan *ksync)
Definition: linux_vxlan.cc:318
virtual ~KSyncLinuxVrfObject()
Definition: linux_vxlan.cc:322
KSyncLinuxVxlan(Agent *agent)
Definition: linux_vxlan.cc:37
uint32_t vxlan_id() const
const Ip4Address & tunnel_dest() const
const KSyncVxlanBridgeEntry * bridge() const
const KSyncVxlanPortEntry * port() const
const MacAddress & mac() const
const std::string & port_name() const
const KSyncVxlanBridgeEntry * bridge() const
void set_bridge_obj(KSyncVxlanBridgeObject *obj)
Definition: ksync_vxlan.cc:45
void set_vrf_obj(KSyncVxlanVrfObject *obj)
Definition: ksync_vxlan.cc:61
void set_port_obj(KSyncVxlanPortObject *obj)
Definition: ksync_vxlan.cc:53
Definition: vrf.h:90
AgentRouteTable * GetBridgeRouteTable() const
Definition: vrf.cc:335
Definition: vxlan.h:14
static void MacToStr(char *buff, const MacAddress &mac)
Definition: linux_vxlan.cc:235
static void Execute(const string &str)
Definition: linux_vxlan.cc:46
#define LOG(_Level, _Msg)
Definition: logging.h:35