OpenSDN source code
ksync_vxlan_route.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  * Copyright Contributors to the OpenSDN project
4  */
5 
6 #include <cmn/agent_cmn.h>
7 
8 #include <ksync/ksync_index.h>
9 #include <ksync/ksync_entry.h>
10 #include <ksync/ksync_object.h>
11 
12 #include <vnc_cfg_types.h>
13 #include <bgp_schema_types.h>
14 #include <agent_types.h>
15 
16 #include <oper/peer.h>
17 #include <oper/vrf.h>
18 #include <oper/interface_common.h>
19 #include <oper/nexthop.h>
20 #include <oper/tunnel_nh.h>
21 #include <oper/multicast.h>
22 #include <oper/vn.h>
23 #include <oper/mirror_table.h>
24 #include <oper/vxlan.h>
25 #include <oper/mpls.h>
26 #include <oper/route_common.h>
27 #include <oper/bridge_route.h>
28 
29 #include "ksync_vxlan.h"
30 #include "ksync_vxlan_bridge.h"
31 #include "ksync_vxlan_port.h"
32 #include "ksync_vxlan_route.h"
33 
34 /**************************************************************************
35  **************************************************************************/
37  const KSyncVxlanRouteEntry *entry) :
38  KSyncDBEntry(), vrf_id_(entry->vrf_id()), ksync_obj_(obj) {
39 }
40 
42  const AgentRoute *route) :
43  KSyncDBEntry(), vrf_id_(route->vrf_id()), ksync_obj_(obj) {
44 }
45 
47 }
48 
50  return ksync_obj_;
51 }
52 
53 bool KSyncVxlanRouteEntry::IsLess(const KSyncEntry &rhs) const {
54  const KSyncVxlanRouteEntry &rhs_route =
55  static_cast<const KSyncVxlanRouteEntry &>(rhs);
57  Agent::RouteTableType rhs_type =
58  rhs_route.ksync_obj_->route_table()->GetTableType();
59 
60  if (lhs_type != rhs_type)
61  return lhs_type < rhs_type;
62 
63  if (vrf_id_ != rhs_route.vrf_id_)
64  return vrf_id_ < rhs_route.vrf_id_;
65 
66  return CompareRoute(rhs_route);
67 }
68 
69 /**************************************************************************
70  **************************************************************************/
72  const KSyncVxlanFdbEntry *entry) :
73  KSyncVxlanRouteEntry(obj, entry), bridge_(entry->bridge_),
74  mac_(entry->mac_), port_(entry->port_), tunnel_dest_(entry->tunnel_dest_) {
75 }
76 
78  const BridgeRouteEntry *route) :
79  KSyncVxlanRouteEntry(obj, route), bridge_(nullptr), mac_(route->prefix_address()),
80  port_(nullptr), tunnel_dest_() {
81 }
82 
84 }
85 
87  const KSyncVxlanFdbEntry &entry = static_cast
88  <const KSyncVxlanFdbEntry &>(rhs);
89  return (mac_.CompareTo(entry.mac_) < 0);
90 }
91 
92 std::string KSyncVxlanFdbEntry::ToString() const {
93  std::stringstream s;
94  s << "FDB : ";
95  return s.str();
96 }
97 
99  bool ret = false;
100 
102  uint32_t new_vxlan_id = VxLanTable::kInvalidvxlan_id;
103  uint32_t old_vxlan_id = VxLanTable::kInvalidvxlan_id;
104 
105  BridgeRouteEntry *fdb = static_cast<BridgeRouteEntry *>(e);
106  KSyncVxlanRouteObject *obj =
107  static_cast<KSyncVxlanRouteObject *>(GetObject());
108  Agent *agent = obj->ksync()->agent();
109 
110  const AgentPath *path = fdb->GetActivePath();
111  if (path) {
112  new_vxlan_id = path->vxlan_id();
113  }
114 
115  if (bridge_) {
116  old_vxlan_id = bridge_->vxlan_id();
117  }
118 
119  if (old_vxlan_id != new_vxlan_id) {
120  KSyncVxlanBridgeObject *bridge_obj =
121  ksync_object()->ksync()->bridge_obj();
122  VxLanIdKey key(new_vxlan_id);
123  VxLanId *vxlan = static_cast<VxLanId *>
124  (agent->vxlan_table()->FindActiveEntry(&key));
125  if (vxlan) {
126  KSyncEntry *vxlan_key = bridge_obj->DBToKSyncEntry(vxlan);
127  bridge = static_cast<KSyncVxlanBridgeEntry *>
128  (bridge_obj->GetReference(vxlan_key));
129  delete vxlan_key;
130  assert(bridge);
131  }
132  }
133 
134  if (bridge_ != bridge) {
135  bridge_ = bridge;
136  ret = true;
137  }
138 
139  // Look for change in nexthop
140  KSyncVxlanPortEntry *port = nullptr;
142  const NextHop *nh = fdb->GetActiveNextHop();
143  if (nh != nullptr) {
144  if (nh->GetType() == NextHop::INTERFACE) {
145  const InterfaceNH *intf_nh = static_cast<const InterfaceNH *>(nh);
146  KSyncVxlanPortObject *port_obj =
147  ksync_object()->ksync()->port_obj();
148  KSyncEntry *port_key =
149  port_obj->DBToKSyncEntry(intf_nh->GetInterface());
150  port = static_cast<KSyncVxlanPortEntry *>
151  (port_obj->GetReference(port_key));
152  delete port_key;
153  assert(port);
154  }
155 
156  if (nh->GetType() == NextHop::TUNNEL) {
157  const TunnelNH *tunnel_nh = static_cast<const TunnelNH *>(nh);
158  tunnel_dest = *tunnel_nh->GetDip();
159  }
160  }
161 
162  if (port_ != port) {
163  port_ = port;
164  ret = true;
165  }
166 
167  if (tunnel_dest_ != tunnel_dest) {
169  ret = true;
170  }
171 
172  return ret;
173 }
174 
176  if (bridge_ == nullptr) {
177  return KSyncVxlan::defer_entry();
178  }
179 
180  if (bridge_->IsResolved() == false) {
181  return bridge_;
182  }
183 
184 
185  if (port_ == nullptr && tunnel_dest_.to_ulong() == 0) {
186  return KSyncVxlan::defer_entry();
187  }
188 
189  if (port_ && port_->IsResolved() == false) {
190  return port_;
191  }
192 
193  return nullptr;
194 }
195 
196 /**************************************************************************
197  **************************************************************************/
199  AgentRouteTable *rt_table) :
200  KSyncDBObject("KSyncVxlanRouteObject"),
201  ksync_(vrf->ksync()),
202  marked_delete_(false),
203  table_delete_ref_(this, rt_table->deleter()) {
204  rt_table_ = rt_table;
205  RegisterDb(rt_table);
206 }
207 
210  table_delete_ref_.Reset(nullptr);
211 }
212 
214  if (IsEmpty() == true && marked_delete_ == true) {
215  ksync_->vrf_obj()->DelFromVrfMap(this);
217  }
218 }
219 
221  marked_delete_ = true;
222  Unregister();
223 }
224 
226  if (marked_delete_ == true) {
227  Unregister();
228  }
229 }
230 
231 /****************************************************************************
232  * VRF Notification handler
233  * Creates a KSyncVxlanRouteObject for every VRF that is created
234  * The RouteObject is stored in VrfRouteObjectMap
235  ***************************************************************************/
237 }
238 
240 }
241 
242 // Register to the VRF table
245  (boost::bind(&KSyncVxlanVrfObject::VrfNotify, this, _1, _2));
246 }
247 
249  const {
250  VrfRouteObjectMap::const_iterator it;
251  it = vrf_fdb_object_map_.find(vrf_id);
252  if (it != vrf_fdb_object_map_.end()) {
253  return it->second;
254  }
255 }
256 
257 void KSyncVxlanVrfObject::AddToVrfMap(uint32_t vrf_id,
258  KSyncVxlanRouteObject *rt) {
259  vrf_fdb_object_map_.insert(make_pair(vrf_id, rt));
260 }
261 
263  VrfRouteObjectMap::iterator it;
264  for (it = vrf_fdb_object_map_.begin(); it != vrf_fdb_object_map_.end();
265  ++it) {
266  if (it->second == rt) {
267  vrf_fdb_object_map_.erase(it);
268  return;
269  }
270  }
271 }
272 
274  VrfEntry *vrf = static_cast<VrfEntry *>(e);
275  VrfState *state = static_cast<VrfState *>
276  (vrf->GetState(partition->parent(), vrf_listener_id_));
277  if (vrf->IsDeleted()) {
278  if (state) {
279  vrf->ClearState(partition->parent(), vrf_listener_id_);
280  delete state;
281  }
282  return;
283  }
284 
285  if (state == nullptr) {
286  state = new VrfState();
287  state->seen_ = true;
288  vrf->SetState(partition->parent(), vrf_listener_id_, state);
290  }
291 
292  return;
293 }
294 
296 }
297 
300  vrf_listener_id_ = -1;
301 }
boost::asio::ip::address_v4 Ip4Address
Definition: address.h:15
AgentDBEntry * FindActiveEntry(const DBEntry *key)
Definition: agent_db.cc:111
uint32_t vxlan_id() const
Definition: agent_path.h:266
Agent supports multiple route tables - Inet-unicast (IPv4/IPv6), Inet-multicast, bridge,...
Definition: agent_route.h:110
virtual Agent::RouteTableType GetTableType() const =0
Base class for all Route entries in agent.
Definition: agent_route.h:225
const AgentPath * GetActivePath() const
Definition: agent_route.cc:878
const NextHop * GetActiveNextHop() const
Definition: agent_route.cc:883
Definition: agent.h:361
RouteTableType
Definition: agent.h:418
VrfTable * vrf_table() const
Definition: agent.h:488
VxLanTable * vxlan_table() const
Definition: agent.h:538
DBState * GetState(DBTableBase *tbl_base, ListenerId listener) const
Definition: db_entry.cc:38
void ClearState(DBTableBase *tbl_base, ListenerId listener)
Definition: db_entry.cc:74
bool IsDeleted() const
Definition: db_entry.h:49
void SetState(DBTableBase *tbl_base, ListenerId listener, DBState *state)
Definition: db_entry.cc:23
ListenerId Register(ChangeCallback callback, const std::string &name="unspecified")
Definition: db_table.cc:208
void Unregister(ListenerId listener)
Definition: db_table.cc:213
DBTableBase * parent()
const Interface * GetInterface() const
Definition: nexthop.h:1294
void UnregisterDb(DBTableBase *table)
DBTableBase * GetDBTable()
Definition: ksync_object.h:245
void RegisterDb(DBTableBase *table)
bool IsResolved()
static void Unregister(KSyncObject *)
KSyncEntry * GetReference(const KSyncEntry *key)
bool IsEmpty(void)
Definition: ksync_object.h:139
uint32_t vxlan_id() const
virtual KSyncEntry * DBToKSyncEntry(const DBEntry *e)=0
KSyncVxlanFdbEntry(KSyncVxlanRouteObject *obj, const KSyncVxlanFdbEntry *entry)
virtual std::string ToString() const
virtual bool CompareRoute(const KSyncVxlanRouteEntry &rhs) const
KSyncVxlanBridgeEntry * bridge_
KSyncVxlanPortEntry * port_
const Ip4Address & tunnel_dest() const
virtual bool Sync(DBEntry *e)
const KSyncVxlanBridgeEntry * bridge() const
virtual KSyncEntry * UnresolvedReference()
const KSyncVxlanPortEntry * port() const
virtual KSyncEntry * DBToKSyncEntry(const DBEntry *e)=0
KSyncDBObject * GetObject() const
KSyncVxlanRouteObject * ksync_obj_
KSyncVxlanRouteObject * ksync_object() const
virtual bool CompareRoute(const KSyncVxlanRouteEntry &rhs) const =0
virtual bool IsLess(const KSyncEntry &rhs) const
KSyncVxlanRouteEntry(KSyncVxlanRouteObject *obj, const KSyncVxlanRouteEntry *entry)
AgentRouteTable * rt_table_
LifetimeRef< KSyncVxlanRouteObject > table_delete_ref_
KSyncVxlan * ksync() const
KSyncVxlanRouteObject(KSyncVxlanVrfObject *vrf, AgentRouteTable *rt_table)
const AgentRouteTable * route_table() const
void AddToVrfMap(uint32_t vrf_id, KSyncVxlanRouteObject *)
KSyncVxlanVrfObject(KSyncVxlan *ksync)
void DelFromVrfMap(KSyncVxlanRouteObject *)
DBTableBase::ListenerId vrf_listener_id_
void VrfNotify(DBTablePartBase *partition, DBEntryBase *e)
VrfRouteObjectMap vrf_fdb_object_map_
KSyncVxlanRouteObject * GetRouteKSyncObject(uint32_t vrf_id) const
virtual KSyncVxlanRouteObject * AllocBridgeRouteTable(const VrfEntry *entry)=0
KSyncVxlanPortObject * port_obj() const
Definition: ksync_vxlan.cc:49
Agent * agent() const
Definition: ksync_vxlan.h:49
KSyncVxlanVrfObject * vrf_obj() const
Definition: ksync_vxlan.cc:57
static KSyncEntry * defer_entry()
Definition: ksync_vxlan.cc:65
KSyncVxlanBridgeObject * bridge_obj() const
Definition: ksync_vxlan.cc:41
void Reset(LifetimeActor *actor)
Definition: lifetime.h:83
int CompareTo(const MacAddress &rhs, int len=0) const
Definition: mac_address.cc:88
@ INTERFACE
Definition: nexthop.h:352
@ TUNNEL
Definition: nexthop.h:353
Type GetType() const
Definition: nexthop.h:406
const Ip4Address * GetDip() const
Definition: tunnel_nh.h:38
Definition: vrf.h:90
const uint32_t vrf_id() const
Definition: vrf.h:103
Definition: vxlan.h:14
static const uint32_t kInvalidvxlan_id
Definition: vxlan.h:141