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