OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
peer.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  */
4 
5 #ifndef vnsw_agent_peer_h_
6 #define vnsw_agent_peer_h_
7 
8 #include <string>
9 #include <map>
10 #include <tbb/mutex.h>
11 #include <db/db_table_walker.h>
12 #include <base/address.h>
13 #include <boost/intrusive_ptr.hpp>
15 
16 #define LOCAL_PEER_NAME "Local"
17 #define LOCAL_VM_PEER_NAME "Local_Vm"
18 #define LOCAL_VM_PORT_PEER_NAME "LocalVmPort"
19 #define NOVA_PEER_NAME "Nova"
20 #define LINKLOCAL_PEER_NAME "LinkLocal"
21 #define ECMP_PEER_NAME "Ecmp"
22 #define VGW_PEER_NAME "Vgw"
23 #define EVPN_ROUTING_PEER_NAME "EVPN Router"
24 #define EVPN_PEER_NAME "EVPN"
25 #define MULTICAST_PEER_NAME "Multicast"
26 #define MULTICAST_TOR_PEER_NAME "Multicast TOR"
27 #define MULTICAST_FABRIC_TREE_BUILDER_NAME "MulticastTreeBuilder"
28 #define MAC_VM_BINDING_PEER_NAME "MacVmBindingPeer"
29 #define MAC_LEARNING_PEER_NAME "DynamicMacLearningPeer"
30 #define FABRIC_RT_EXPORT "FabricRouteExport"
31 #define LOCAL_VM_EXPORT_PEER "LocalVmExportPeer"
32 
33 class AgentXmppChannel;
35 class VrfTable;
36 class AgentPath;
37 
38 class Peer;
39 void intrusive_ptr_add_ref(const Peer* p);
40 void intrusive_ptr_release(const Peer* p);
41 typedef boost::intrusive_ptr<const Peer> PeerConstPtr;
42 typedef boost::intrusive_ptr<Peer> PeerPtr;
43 
44 class Peer {
45 public:
46  typedef std::map<std::string, Peer *> PeerMap;
47  typedef std::pair<std::string, Peer *> PeerPair;
48  enum Type {
66  MAC_LEARNING_PEER, /* The peer has the lowest priority
67  to prevent interference with other paths */
68  };
69 
70  Peer(Type type, const std::string &name, bool controller_export);
71  virtual ~Peer();
72 
73  bool IsLess(const Peer *rhs) const {
74  if (type_ != rhs->type_) {
75  return type_ < rhs->type_;
76  }
77 
78  return Compare(rhs);
79  }
80  virtual bool Compare(const Peer *rhs) const {return false;}
81  // Should we export path from this peer to controller?
82  virtual bool export_to_controller() const {return export_to_controller_;}
83  virtual const Ip4Address *NexthopIp(Agent *agent,
84  const AgentPath *path) const;
85 
86  const std::string &GetName() const { return name_; }
87  const Type GetType() const { return type_; }
88 
89  virtual bool SkipAddChangeRequest() const { return false; }
90 
91  virtual bool IsDeleted() const { return false; }
92 
93  uint32_t refcount() const { return refcount_; }
94  uint64_t sequence_number() const {return sequence_number_;}
96 
97 private:
98  friend void intrusive_ptr_add_ref(const Peer *p);
99  friend void intrusive_ptr_release(const Peer *p);
100 
101  virtual bool DeleteOnZeroRefcount() const;
102 
104  std::string name_;
106  mutable tbb::atomic<uint32_t> refcount_;
107  // Sequence number can be used for tracking event based changes like
108  // add/update on peer flaps(as example).
111 };
112 
113 // DynamicPeer is one of the base class for Peer to be used for
114 // all the Dynamic Peers.
115 // This provide Peer pointer sanity using references ensuring
116 // that the Peer pointer will be accessible till all the Async
117 // DB Requests for this Peer has been processed and all the route
118 // Paths are removed
119 class DynamicPeer : public Peer {
120 public:
121  // Dynamic peer clean up is supposed to be relatively faster process
122  // however trigger a timeoout after 5 mins to catch if any issue
123  // happend
124  static const uint32_t kDeleteTimeout = 300 * 1000;
125 
126  DynamicPeer(Agent *agent, Type type, const std::string &name,
127  bool controller_export);
128  virtual ~DynamicPeer();
129 
130  // Skip Add/Change request if set
131  virtual bool SkipAddChangeRequest() const { return skip_add_change_; }
132 
133  virtual bool IsDeleted() const { return deleted_; }
134 
135  // only sets skip_add_change to true, should be set only just
136  // before triggering a delete on Peer and a reset is not needed
138 
139  static void ProcessDelete(DynamicPeer *p);
140 
141  bool DeleteTimeout();
142 
143 private:
144  friend void intrusive_ptr_add_ref(const Peer *p);
145  friend void intrusive_ptr_release(const Peer *p);
146 
147  virtual bool DeleteOnZeroRefcount() const;
148 
150  tbb::atomic<bool> deleted_;
151  tbb::atomic<bool> skip_add_change_;
153 };
154 
155 // Peer used for BGP paths
156 class BgpPeer : public DynamicPeer {
157 public:
158  typedef boost::function<void()> WalkDoneCb;
159  BgpPeer(AgentXmppChannel *channel,
160  const Ip4Address &server_ip, const std::string &name,
161  DBTableBase::ListenerId id, Peer::Type bgp_peer_type);
162  virtual ~BgpPeer();
163 
164  bool Compare(const Peer *rhs) const {
165  const BgpPeer *bgp = static_cast<const BgpPeer *>(rhs);
166  return server_ip_ < bgp->server_ip_;
167  }
168 
169  // For testing
172 
173  // Table Walkers
174  //Notify walker
175  void PeerNotifyRoutes(WalkDoneCb cb);
176  void PeerNotifyMulticastRoutes(bool associate);
177  void AllocPeerNotifyWalker();
179  void StopPeerNotifyRoutes();
180  //Del peer walker
181  void DelPeerRoutes(WalkDoneCb walk_done_cb,
182  uint64_t sequence_number);
183  void DeleteStale();
184  void AllocDeleteStaleWalker();
186  void AllocDeletePeerWalker();
188  void StopDeleteStale();
189 
193 
194  //Helper routines to get export state for vrf and route
196  DBEntryBase *e);
198  DBEntryBase *e);
199  void DeleteVrfState(DBTablePartBase *partition, DBEntryBase *entry);
200 
201  uint32_t setup_time() const {return setup_time_;}
202  Agent *agent() const;
204  uint64_t ChannelSequenceNumber() const;
208 
209 private:
213  uint32_t setup_time_;
221 };
222 
223 // Peer for local-vm-port paths. There can be multiple VMs with same IP.
224 // They are all added as different path. ECMP path will consolidate all
225 // local-vm-port paths
226 class LocalVmPortPeer : public Peer {
227 public:
228  LocalVmPortPeer(const std::string &name, uint64_t handle) :
229  Peer(Peer::LOCAL_VM_PORT_PEER, name, true), handle_(handle) {
230  }
231 
232  virtual ~LocalVmPortPeer() { }
233 
234  bool Compare(const Peer *rhs) const {
235  const LocalVmPortPeer *local =
236  static_cast<const LocalVmPortPeer *>(rhs);
237  return handle_ < local->handle_;
238  }
239 
240 private:
241  uint64_t handle_;
243 };
244 
245 // ECMP peer
246 class EcmpPeer : public Peer {
247 public:
248  EcmpPeer() : Peer(Peer::ECMP_PEER, "ECMP", true) { }
249  virtual ~EcmpPeer() { }
250 
251  bool Compare(const Peer *rhs) const { return false; }
252 private:
254 };
255 
256 // EVPN peer
257 class EvpnPeer : public Peer {
258 public:
259  typedef boost::shared_ptr<EvpnPeer> EvpnPeerRef;
260 
261  EvpnPeer() : Peer(Peer::EVPN_PEER, "EVPN", false) { }
262  virtual ~EvpnPeer() { }
263 
264  bool Compare(const Peer *rhs) const { return false; }
265 private:
267 };
268 
269 // Inet EVPN peer
270 class InetEvpnPeer : public Peer {
271 public:
272  typedef boost::shared_ptr<EvpnPeer> InetEvpnPeerRef;
273 
274  InetEvpnPeer() : Peer(Peer::INET_EVPN_PEER, "INET-EVPN", false) { }
275  virtual ~InetEvpnPeer() { }
276 
277  bool Compare(const Peer *rhs) const { return false; }
278 private:
280 };
281 
282 // EVPN routing peer
283 class EvpnRoutingPeer : public Peer {
284 public:
285  typedef boost::shared_ptr<EvpnPeer> EvpnRoutingPeerRef;
286 
287  EvpnRoutingPeer() : Peer(Peer::EVPN_ROUTING_PEER, "EVPN-ROUTING", false) { }
288  virtual ~EvpnRoutingPeer() { }
289 
290  bool Compare(const Peer *rhs) const { return false; }
291 private:
293 };
294 
295 // EVPN routing peer
296 class VxlanBgpPeer : public Peer {
297 public:
298  typedef boost::shared_ptr<VxlanBgpPeer> VxlanBgpPeerRef;
299 
300  VxlanBgpPeer() : Peer(Peer::VXLAN_BGP_PEER, "VxLAN BGP PEER", false) { }
301  virtual ~VxlanBgpPeer() { }
302 
303  bool Compare(const Peer *rhs) const { return false; }
304 private:
306 };
307 
308 #endif // vnsw_agent_peer_h_
const std::string & GetName() const
Definition: peer.h:86
void set_delete_stale_walker_cb(WalkDoneCb cb)
Definition: peer.cc:343
void StopDeleteStale()
Definition: peer.cc:235
bool Compare(const Peer *rhs) const
Definition: peer.h:303
tbb::atomic< uint32_t > refcount_
Definition: peer.h:106
void PeerNotifyMulticastRoutes(bool associate)
Definition: peer.cc:162
int intrusive_ptr_add_ref(const AsPath *cpath)
Definition: bgp_aspath.h:147
void ReleaseDeletePeerWalker()
Definition: peer.cc:202
bool Compare(const Peer *rhs) const
Definition: peer.h:234
std::map< std::string, Peer * > PeerMap
Definition: peer.h:46
Ip4Address server_ip_
Definition: peer.h:211
virtual bool SkipAddChangeRequest() const
Definition: peer.h:131
Definition: vrf.h:268
WalkDoneCb route_walker_cb_
Definition: peer.h:217
void DeleteStale()
Definition: peer.cc:224
Timer * delete_timeout_timer_
Definition: peer.h:149
DynamicPeer(Agent *agent, Type type, const std::string &name, bool controller_export)
Definition: peer.cc:43
std::pair< std::string, Peer * > PeerPair
Definition: peer.h:47
EcmpPeer()
Definition: peer.h:248
void ReleaseDeleteStaleWalker()
Definition: peer.cc:179
virtual ~VxlanBgpPeer()
Definition: peer.h:301
bool export_to_controller_
Definition: peer.h:105
boost::shared_ptr< EvpnPeer > EvpnPeerRef
Definition: peer.h:259
int ListenerId
Definition: db_table.h:62
void DelPeerRoutes(WalkDoneCb walk_done_cb, uint64_t sequence_number)
Definition: peer.cc:213
boost::shared_ptr< EvpnPeer > InetEvpnPeerRef
Definition: peer.h:272
bool Compare(const Peer *rhs) const
Definition: peer.h:277
void AllocDeletePeerWalker()
Definition: peer.cc:191
ControllerRouteWalker * delete_peer_walker() const
Definition: peer.cc:250
DISALLOW_COPY_AND_ASSIGN(InetEvpnPeer)
boost::intrusive_ptr< Peer > PeerPtr
Definition: peer.h:42
AgentRouteWalkerPtr route_walker_
Definition: peer.h:214
void set_route_walker_cb(WalkDoneCb cb)
Definition: peer.cc:339
virtual ~Peer()
Definition: peer.cc:24
void AllocPeerNotifyWalker()
Definition: peer.cc:128
WalkDoneCb delete_stale_walker_cb_
Definition: peer.h:218
virtual bool DeleteOnZeroRefcount() const
Definition: peer.cc:27
bool IsLess(const Peer *rhs) const
Definition: peer.h:73
DBState * GetRouteExportState(DBTablePartBase *partition, DBEntryBase *e)
Definition: peer.cc:303
virtual bool DeleteOnZeroRefcount() const
Definition: peer.cc:87
Peer(Type type, const std::string &name, bool controller_export)
Definition: peer.cc:18
friend void intrusive_ptr_add_ref(const Peer *p)
Definition: peer.cc:31
bool DeleteTimeout()
Definition: peer.cc:82
Definition: peer.h:246
virtual bool SkipAddChangeRequest() const
Definition: peer.h:89
uint64_t ChannelSequenceNumber() const
Definition: peer.cc:335
virtual bool IsDeleted() const
Definition: peer.h:91
std::string name_
Definition: peer.h:104
BgpPeer(BgpServer *server, RoutingInstance *instance, const BgpNeighborConfig *config)
Definition: bgp_peer.cc:469
const Type GetType() const
Definition: peer.h:87
DBTableBase::ListenerId id_
Definition: peer.h:212
virtual const Ip4Address * NexthopIp(Agent *agent, const AgentPath *path) const
Definition: peer.cc:98
void StopPeerNotifyRoutes()
Definition: peer.cc:155
DISALLOW_COPY_AND_ASSIGN(EvpnPeer)
virtual ~EcmpPeer()
Definition: peer.h:249
uint32_t setup_time() const
Definition: peer.h:201
virtual bool Compare(const Peer *rhs) const
Definition: peer.h:80
uint8_t type
Definition: load_balance.h:109
bool Compare(const Peer *rhs) const
Definition: peer.h:164
Definition: agent.h:358
virtual ~EvpnRoutingPeer()
Definition: peer.h:288
void PeerNotifyRoutes(WalkDoneCb cb)
Definition: peer.cc:149
AgentRouteWalkerPtr delete_peer_walker_
Definition: peer.h:215
DISALLOW_COPY_AND_ASSIGN(VxlanBgpPeer)
DBTableBase::ListenerId GetVrfExportListenerId()
Definition: peer.h:171
uint64_t sequence_number() const
Definition: peer.h:94
ControllerRouteWalker * route_walker() const
Definition: peer.cc:242
boost::shared_ptr< VxlanBgpPeer > VxlanBgpPeerRef
Definition: peer.h:298
uint64_t handle_
Definition: peer.h:241
tbb::atomic< bool > skip_add_change_
Definition: peer.h:151
virtual ~EvpnPeer()
Definition: peer.h:262
AgentXmppChannel * GetAgentXmppChannel() const
Definition: peer.cc:331
tbb::atomic< bool > deleted_
Definition: peer.h:150
DBState * GetVrfExportState(DBTablePartBase *partition, DBEntryBase *e)
Definition: peer.cc:294
void incr_sequence_number()
Definition: peer.h:95
DISALLOW_COPY_AND_ASSIGN(EcmpPeer)
void DeleteVrfState(DBTablePartBase *partition, DBEntryBase *entry)
Definition: peer.cc:259
Definition: peer.h:44
virtual ~BgpPeer()
Definition: bgp_peer.cc:625
EvpnRoutingPeer()
Definition: peer.h:287
LocalVmPortPeer(const std::string &name, uint64_t handle)
Definition: peer.h:228
boost::intrusive_ptr< AgentRouteWalker > AgentRouteWalkerPtr
WalkDoneCb delete_peer_walker_cb_
Definition: peer.h:219
boost::asio::ip::address_v4 Ip4Address
Definition: address.h:14
void AllocDeleteStaleWalker()
Definition: peer.cc:168
friend void intrusive_ptr_release(const Peer *p)
Definition: peer.cc:37
void SetVrfListenerId(DBTableBase::ListenerId id)
Definition: peer.h:170
Type type_
Definition: peer.h:103
boost::intrusive_ptr< const Peer > PeerConstPtr
Definition: peer.h:41
static const uint32_t kDeleteTimeout
Definition: peer.h:124
virtual ~DynamicPeer()
Definition: peer.cc:55
boost::function< void()> WalkDoneCb
Definition: peer.h:158
DISALLOW_COPY_AND_ASSIGN(BgpPeer)
InetEvpnPeer()
Definition: peer.h:274
boost::shared_ptr< EvpnPeer > EvpnRoutingPeerRef
Definition: peer.h:285
DISALLOW_COPY_AND_ASSIGN(LocalVmPortPeer)
DISALLOW_COPY_AND_ASSIGN(DynamicPeer)
bool Compare(const Peer *rhs) const
Definition: peer.h:264
VxlanBgpPeer()
Definition: peer.h:300
void intrusive_ptr_release(const AsPath *cpath)
Definition: bgp_aspath.h:155
AgentRouteWalkerPtr delete_stale_walker_
Definition: peer.h:216
void set_delete_peer_walker_cb(WalkDoneCb cb)
Definition: peer.cc:346
virtual bool IsDeleted() const
Definition: peer.h:133
virtual ~LocalVmPortPeer()
Definition: peer.h:232
friend void intrusive_ptr_add_ref(const Peer *p)
Definition: peer.cc:31
virtual bool export_to_controller() const
Definition: peer.h:82
bool Compare(const Peer *rhs) const
Definition: peer.h:290
static void ProcessDelete(DynamicPeer *p)
Definition: peer.cc:63
AgentXmppChannel * channel_
Definition: peer.h:210
void ReleasePeerNotifyWalker()
Definition: peer.cc:139
Agent * agent() const
Definition: peer.cc:327
Definition: timer.h:54
DISALLOW_COPY_AND_ASSIGN(EvpnRoutingPeer)
virtual ~InetEvpnPeer()
Definition: peer.h:275
EvpnPeer()
Definition: peer.h:261
uint32_t refcount() const
Definition: peer.h:93
uint32_t setup_time_
Definition: peer.h:213
Definition: peer.h:257
ControllerRouteWalker * delete_stale_walker() const
Definition: peer.cc:246
DISALLOW_COPY_AND_ASSIGN(Peer)
friend void intrusive_ptr_release(const Peer *p)
Definition: peer.cc:37
uint64_t sequence_number_
Definition: peer.h:109
Type
Definition: peer.h:48
bool Compare(const Peer *rhs) const
Definition: peer.h:251
void StopRouteExports()
Definition: peer.h:137