OpenSDN source code
controller_route_walker.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 
6 #include <boost/shared_ptr.hpp>
7 
8 #include <sandesh/sandesh_trace.h>
9 
10 #include <cmn/agent_cmn.h>
11 #include <oper/route_common.h>
13 #include <oper/peer.h>
14 #include <oper/vrf.h>
15 #include <oper/mirror_table.h>
16 #include <oper/agent_sandesh.h>
17 
20 #include "controller/controller_types.h"
24 
26  Peer *peer) :
27  AgentRouteWalker(name, dynamic_cast<BgpPeer *>(peer)->agent()),
28  peer_(peer), associate_(false), type_(NOTIFYALL), sequence_number_(0) {
29 }
30 
31 // Takes action based on context of walk. These walks are not parallel.
32 // At a time peer can be only in one state.
34  DBEntryBase *entry) {
35  VrfEntry *vrf = static_cast<VrfEntry *>(entry);
36  // Notification from deleted VRF should have taken care of all operations
37  // w.r.t. peer, see VrfExport::Notify
38  // Exception is DelPeer walk. Reason being that this walk will start when
39  // peer goes down in agent xmpp channel. When it happens bgp peer in channel
40  // is reset. Any add/delete notification on said VRF checks if xmpp channel
41  // is active which internally checks for bgp peer. In current case it will
42  // be NULL and will in-turn ignore notification for delete. State will
43  // not be deleted for that peer in vrf. To delete state, delpeer walk will
44  // have to traverse deleted VRF as well.
45  if (vrf->IsDeleted() && (type_ != DELPEER))
46  return true;
47 
48  switch (type_) {
49  case NOTIFYALL:
50  return VrfNotifyAll(partition, entry);
51  case NOTIFYMULTICAST:
52  return VrfNotifyMulticast(partition, entry);
53  case DELPEER:
54  return VrfDelPeer(partition, entry);
55  case DELSTALE:
56  return VrfDelStale(partition, entry);
57  default:
58  return false;
59  }
60  return false;
61 }
62 
63 /*
64  * Notification for vrf entry - Creates states (VRF and route) and
65  * send subscription to control node
66  * This will be called for active bgp peer only.
67  */
69  DBEntryBase *entry) {
70  VrfEntry *vrf = static_cast<VrfEntry *>(entry);
71  if (peer_->GetType() == Peer::BGP_PEER) {
72  BgpPeer *bgp_peer = static_cast<BgpPeer *>(peer_);
73 
75  VrfExport::State *state =
76  static_cast<VrfExport::State *>(vrf->GetState(partition->parent(),
77  id));
78  if (state) {
79  /* state for __default__ instance will not be created if the
80  * xmpp channel is up the first time as export code registers to
81  * vrf-table after entry for __default__ instance is created */
82  state->force_chg_ = true;
83  }
84 
85  //Pass this object pointer so that VrfExport::Notify can start the route
86  //walk if required on this VRF. Also it adds state if none is found.
88  partition, entry);
89  CONTROLLER_ROUTE_WALKER_TRACE(Walker, "Vrf Notify all", vrf->GetName(),
90  bgp_peer->GetName());
91  return true;
92  }
93  return false;
94 }
95 
96 /*
97  * Delete peer notifications for VRF.
98  */
100  DBEntryBase *entry) {
101  VrfEntry *vrf = static_cast<VrfEntry *>(entry);
102  if (peer_->GetType() == Peer::BGP_PEER) {
103  BgpPeer *bgp_peer = static_cast<BgpPeer *>(peer_);
104  VrfExport::State *state = static_cast<VrfExport::State *>
105  (bgp_peer->GetVrfExportState(partition, vrf));
106  // State can be NULL, when vrf delete has happened before peer goes down
107  // and VRF is not deleted before delpeer walker reaches the entry.
108  // Controller Notify would have deleted state before delpeer was invoked.
109  if (!state)
110  return true;
111  // skip starting walk on route tables if all the the route tables
112  // are already delete, this also safe-gaurds that StartRouteWalk
113  // will not be the first reference on the deleted VRF which will
114  // end up taking reference and setting refcount state on deleted
115  // VRF which can cause Bug - 1495824
116  if (vrf->AllRouteTableDeleted()) return true;
117  // Register Callback for deletion of VRF state on completion of route
118  // walks
120  RouteWalkDoneForVrfCallback(boost::bind(
122  this, _1));
123  }
125  return true;
126  }
127  return false;
128 }
129 
130 /*
131  * Delete stale notifications for VRF.
132  */
134  DBEntryBase *entry) {
135  VrfEntry *vrf = static_cast<VrfEntry *>(entry);
136  if (peer_->GetType() == Peer::BGP_PEER) {
137  // skip starting walk on route tables if all the the route tables
138  // are already delete, this also safe-gaurds that StartRouteWalk
139  // will not be the first reference on the deleted VRF which will
140  // end up taking reference and setting refcount state on deleted
141  // VRF which can cause Bug - 1495824
142  if (vrf->AllRouteTableDeleted()) return true;
143  // Register Callback for deletion of VRF state on completion of route
144  // walks
146  RouteWalkDoneForVrfCallback(boost::bind(
148  this, _1));
149  }
151  return true;
152  }
153  return false;
154 }
155 
157  DBEntryBase *entry) {
158  VrfEntry *vrf = static_cast<VrfEntry *>(entry);
159  CONTROLLER_ROUTE_WALKER_TRACE(Walker, "Vrf Multicast", vrf->GetName(),
160  peer_->GetName());
161  return VrfNotifyInternal(partition, entry);
162 }
163 
164 //Common routeine if basic vrf and peer check is required for the walk
166  DBEntryBase *entry) {
167  VrfEntry *vrf = static_cast<VrfEntry *>(entry);
168  if (peer_->GetType() == Peer::BGP_PEER) {
169  BgpPeer *bgp_peer = static_cast<BgpPeer *>(peer_);
170  if (IgnoreNotify()) {
171  return false;
172  }
173 
175  VrfExport::State *state =
176  static_cast<VrfExport::State *>(vrf->GetState(partition->parent(),
177  id));
178  if (state) {
180 
181  if (Agent::GetInstance()->mulitcast_builder() ==
182  bgp_peer->GetAgentXmppChannel()) {
183  state->mcast_exported_ = associate_;
184  }
185  }
186 
187  return true;
188  }
189  return false;
190 }
191 
192 // Takes action based on context of walk. These walks are not parallel.
193 // At a time peer can be only in one state.
195  DBEntryBase *entry) {
196  switch (type_) {
197  case NOTIFYALL:
198  return RouteNotifyAll(partition, entry);
199  case NOTIFYMULTICAST:
200  return RouteNotifyMulticast(partition, entry);
201  case DELPEER:
202  return RouteDelPeer(partition, entry);
203  case DELSTALE:
204  return RouteDelStale(partition, entry);
205  default:
206  return false;
207  }
208  return false;
209 }
210 
212  DBEntryBase *entry) {
213  AgentRoute *route = static_cast<AgentRoute *>(entry);
214 
215  if ((type_ == NOTIFYMULTICAST) && !route->is_multicast())
216  return true;
217 
218  BgpPeer *bgp_peer = static_cast<BgpPeer *>(peer_);
219  if (IgnoreNotify()) {
220  return false;
221  }
222 
223  Agent::RouteTableType table_type = route->GetTableType();
224 
225  //Get the route state
226  RouteExport::State *route_state = static_cast<RouteExport::State *>
227  (bgp_peer->GetRouteExportState(partition, entry));
228  if (route_state) {
229  // Forcibly send the notification to peer.
230  route_state->force_chg_ = true;
231  }
232 
233  VrfEntry *vrf = route->vrf();
234  DBTablePartBase *vrf_partition = agent()->vrf_table()->
235  GetTablePartition(vrf);
236  VrfExport::State *vs = static_cast<VrfExport::State *>
237  (bgp_peer->GetVrfExportState(vrf_partition, vrf));
238 
239  if (vs) {
240  vs->rt_export_[table_type]->
241  Notify(agent(), bgp_peer->GetAgentXmppChannel(), associate_,
242  table_type, partition, entry);
243  }
244  return true;
245 }
246 
248  DBEntryBase *entry) {
249  AgentRoute *route = static_cast<AgentRoute *>(entry);
250  CONTROLLER_ROUTE_WALKER_TRACE(Walker, "Route NotifyAll", route->ToString(),
251  peer_->GetName());
252  return RouteNotifyInternal(partition, entry);
253 }
254 
256  DBEntryBase *entry) {
257  AgentRoute *route = static_cast<AgentRoute *>(entry);
258  CONTROLLER_ROUTE_WALKER_TRACE(Walker, "Route Multicast Notify", route->ToString(),
259  peer_->GetName());
260  return RouteNotifyInternal(partition, entry);
261 }
262 
264  DBEntryBase *entry) {
265  AgentRoute *route = static_cast<AgentRoute *>(entry);
266  if (!route)
267  return true;
268 
269  //Enqueue path delete.
270  AgentRouteKey *key = (static_cast<AgentRouteKey *>(route->
271  GetDBRequestKey().get()))->Clone();
272  key->set_peer(peer_);
274  req.key.reset(key);
275  req.data.reset(new StalePathData(sequence_number_));
276  AgentRouteTable *table = static_cast<AgentRouteTable *>(route->get_table());
277  table->Process(req);
278  return true;
279 }
280 
281 // Deletes the peer and corresponding state in route
283  DBEntryBase *entry) {
284  AgentRoute *route = static_cast<AgentRoute *>(entry);
285  BgpPeer *bgp_peer = static_cast<BgpPeer *>(peer_);
286 
287  if (!route)
288  return true;
289 
290  CONTROLLER_ROUTE_WALKER_TRACE(Walker, "Route Delpeer", route->ToString(),
291  peer_->GetName());
292 
293  VrfEntry *vrf = route->vrf();
294  DBTablePartBase *vrf_partition = agent()->vrf_table()->
295  GetTablePartition(vrf);
296  VrfExport::State *vrf_state = static_cast<VrfExport::State *>
297  (bgp_peer->GetVrfExportState(vrf_partition, vrf));
298  RouteExport::State *state = static_cast<RouteExport::State *>
299  (bgp_peer->GetRouteExportState(partition, entry));
300  if (vrf_state && state && vrf_state->rt_export_[route->GetTableType()]) {
301  route->ClearState(partition->parent(), vrf_state->rt_export_[route->
302  GetTableType()]->GetListenerId());
303  delete state;
304  CONTROLLER_ROUTE_WALKER_TRACE(Walker, "DelPeer route walk, delete state",
305  route->ToString(), peer_->GetName());
306  }
307 
308  //Enqueue path delete.
309  AgentRouteKey *key = (static_cast<AgentRouteKey *>(route->
310  GetDBRequestKey().get()))->Clone();
311  key->set_peer(peer_);
313  req.key.reset(key);
314  req.data.reset();
315  AgentRouteTable *table = static_cast<AgentRouteTable *>(route->get_table());
316  table->Process(req);
317  return true;
318 }
319 
320 // Called when for a VRF all route table walks are complete.
321 // Deletes the VRF state of that peer.
323  // Currently used only for delete peer handling
324  // Deletes the state and release the listener id
325  if (type_ != DELPEER)
326  return;
327 
328  CONTROLLER_ROUTE_WALKER_TRACE(Walker, "Route Walk done", vrf->GetName(),
329  peer_->GetName());
330  BgpPeer *bgp_peer = static_cast<BgpPeer *>(peer_);
331  DBEntryBase *entry = static_cast<DBEntryBase *>(vrf);
332  DBTablePartBase *partition = agent()->vrf_table()->GetTablePartition(vrf);
333  bgp_peer->DeleteVrfState(partition, entry);
334 }
335 
336 // walk_done_cb - Called back when all walk i.e. VRF and route are done.
338  AgentRouteWalker::WalkDone walk_done_cb) {
339  associate_ = associate;
340  type_ = type;
341  WalkDoneCallback(walk_done_cb);
342 
343  StartVrfWalk();
344 }
345 
347  Type type) {
348  associate_ = associate;
349  type_ = type;
351 }
352 
354  return ((type_ == DELSTALE) || (type_ == DELPEER));
355 }
356 
358  BgpPeer *bgp_peer = static_cast<BgpPeer *>(peer_);
359  return bgp_peer->SkipAddChangeRequest();
360 }
Agent supports multiple route tables - Inet-unicast (IPv4/IPv6), Inet-multicast, bridge,...
Definition: agent_route.h:110
void Process(DBRequest &req)
Definition: agent_route.cc:187
void RouteWalkDoneForVrfCallback(RouteWalkDoneCb cb)
void StartRouteWalk(VrfEntry *vrf)
void WalkDoneCallback(WalkDone cb)
boost::function< void()> WalkDone
Agent * agent() const
Base class for all Route entries in agent.
Definition: agent_route.h:225
VrfEntry * vrf() const
Definition: agent_route.h:276
virtual std::string ToString() const =0
bool is_multicast() const
Definition: agent_route.h:275
virtual Agent::RouteTableType GetTableType() const =0
RouteTableType
Definition: agent.h:418
VrfTable * vrf_table() const
Definition: agent.h:488
static Agent * GetInstance()
Definition: agent.h:439
DBState * GetRouteExportState(DBTablePartBase *partition, DBEntryBase *e)
Definition: peer.cc:304
DBTableBase::ListenerId GetVrfExportListenerId()
Definition: peer.h:172
DBState * GetVrfExportState(DBTablePartBase *partition, DBEntryBase *e)
Definition: peer.cc:295
AgentXmppChannel * GetAgentXmppChannel() const
Definition: peer.cc:332
void DeleteVrfState(DBTablePartBase *partition, DBEntryBase *entry)
Definition: peer.cc:260
bool VrfNotifyAll(DBTablePartBase *partition, DBEntryBase *e)
void RouteWalkDoneForVrf(VrfEntry *vrf)
bool RouteDelPeer(DBTablePartBase *partition, DBEntryBase *e)
bool VrfDelStale(DBTablePartBase *partition, DBEntryBase *e)
ControllerRouteWalker(const std::string &name, Peer *peer)
bool RouteNotifyInternal(DBTablePartBase *partition, DBEntryBase *e)
void StartRouteWalk(VrfEntry *vrf, bool associate, Type type)
virtual bool VrfWalkNotify(DBTablePartBase *partition, DBEntryBase *e)
bool VrfDelPeer(DBTablePartBase *partition, DBEntryBase *e)
bool RouteNotifyMulticast(DBTablePartBase *partition, DBEntryBase *e)
virtual bool RouteWalkNotify(DBTablePartBase *partition, DBEntryBase *e)
bool RouteNotifyAll(DBTablePartBase *partition, DBEntryBase *e)
bool RouteDelStale(DBTablePartBase *partition, DBEntryBase *e)
bool VrfNotifyMulticast(DBTablePartBase *partition, DBEntryBase *e)
bool VrfNotifyInternal(DBTablePartBase *partition, DBEntryBase *e)
void Start(Type type, bool associate, AgentRouteWalker::WalkDone walk_done_cb)
DBState * GetState(DBTableBase *tbl_base, ListenerId listener) const
Definition: db_entry.cc:38
DBTableBase * get_table() const
Definition: db_entry.cc:120
void ClearState(DBTableBase *tbl_base, ListenerId listener)
Definition: db_entry.cc:74
bool IsDeleted() const
Definition: db_entry.h:49
int ListenerId
Definition: db_table.h:63
DBTableBase * parent()
virtual DBTablePartBase * GetTablePartition(const DBRequestKey *key)
Definition: db_table.cc:463
virtual bool SkipAddChangeRequest() const
Definition: peer.h:132
Definition: peer.h:45
const Type GetType() const
Definition: peer.h:88
@ BGP_PEER
Definition: peer.h:52
const std::string & GetName() const
Definition: peer.h:87
DBTableBase::ListenerId GetListenerId() const
Definition: vrf.h:90
const string & GetName() const
Definition: vrf.h:104
bool AllRouteTableDeleted() const
Definition: vrf.cc:269
static void Notify(const Agent *agent, AgentXmppChannel *, DBTablePartBase *partition, DBEntryBase *e)
#define CONTROLLER_ROUTE_WALKER_TRACE(obj,...)
uint8_t type
Definition: load_balance.h:2
void set_peer(const Peer *peer)
Definition: agent_route.h:49
virtual AgentRouteKey * Clone() const =0
@ DB_ENTRY_DELETE
Definition: db_table.h:40
std::unique_ptr< DBRequestKey > key
Definition: db_table.h:49
std::unique_ptr< DBRequestData > data
Definition: db_table.h:50
RouteExport * rt_export_[Agent::ROUTE_TABLE_MAX]