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