OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
peer_close_manager.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  */
4 
5 #ifndef SRC_BGP_PEER_CLOSE_MANAGER_H_
6 #define SRC_BGP_PEER_CLOSE_MANAGER_H_
7 
8 #include <list>
9 #include <string>
10 
11 #include "base/timer.h"
12 #include "base/util.h"
13 #include "base/queue_task.h"
14 #include "db/db_table_walker.h"
15 #include "bgp/ipeer.h"
16 
18 class BgpNeighborResp;
19 class BgpRoute;
20 class BgpTable;
21 class PeerCloseInfo;
22 
23 // PeerCloseManager
24 //
25 // Manager close process of an IPeer (And hence should support both BgpPeers
26 // and XmppPeers)
27 //
28 // Among other things, RibIns and RibOuts of peers must be closed/deleted
29 // completely before a peer can be completely closed/deleted. This class
30 // provides this capability.
31 //
32 // RibIn and RibOut close are handled by invoking Unregister request with
33 // BgpMembershipManager class.
34 //
35 // Once RibIns and RibOuts are processed, notification callback function is
36 // invoked to signal the completion of close process
37 //
39 public:
41  boost::asio::io_context *io_service);
42  explicit PeerCloseManager(IPeerClose *peer_close);
43  virtual ~PeerCloseManager();
44 
45  bool IsMembershipInUse() const {
47  }
48  bool IsMembershipInWait() const {
50  }
51  IPeerClose *peer_close() const { return peer_close_; }
53 
54  bool IsCloseInProgress() const { return state_ != NONE; }
55  bool IsInDeleteState() const { return state_ == DELETE; }
56  bool IsInGRTimerWaitState() const {
57  return state_ == GR_TIMER || state_ == LLGR_TIMER;
58  }
59  bool IsInLlgrTimerWaitState() const { return state_ == LLGR_TIMER; }
60  bool IsQueueEmpty() const { return event_queue_->IsQueueEmpty(); }
61 
62  void Close(bool graceful);
64  void MembershipRequest();
66  BgpNeighborResp *FillCloseInfo(BgpNeighborResp *resp) const;
68  BgpPath *path);
69  void UpdateRouteStats(Address::Family family, const BgpPath *old_path,
70  uint32_t path_flags) const;
71 
72 private:
73  friend class BgpServerUnitTest;
74  friend class PeerCloseTest;
75  friend class PeerCloseManagerTest;
76  friend class GracefulRestartTest;
77 
78  enum State {
88  };
89 
96  };
97 
98  enum EventType {
107  };
108 
109  struct Event {
111  event_type(event_type), graceful(graceful),
112  family(Address::UNSPEC) { }
114  event_type(event_type), graceful(true),
115  family(family) { }
116  explicit Event(EventType event_type) : event_type(event_type),
117  graceful(true),
118  family(Address::UNSPEC) { }
120  family(Address::UNSPEC) { }
121 
123  bool graceful;
125  };
126 
127  struct Stats {
128  Stats():
129  init(0),
130  close(0),
131  nested(0),
132  deletes(0),
133  stale(0),
134  llgr_stale(0),
135  sweep(0),
136  gr_timer(0),
137  llgr_timer(0) {
138  }
139 
140  struct RouteStats {
141  RouteStats() { reset(); }
142  bool IsSet() const {
143  return staled || llgr_staled || refreshed || fresh || deleted;
144  }
145 
146  void reset() {
147  staled = 0;
148  llgr_staled = 0;
149  refreshed = 0;
150  fresh = 0;
151  deleted = 0;
152  }
153 
154  tbb::atomic<uint64_t> staled;
155  tbb::atomic<uint64_t> llgr_staled;
156  tbb::atomic<uint64_t> refreshed;
157  tbb::atomic<uint64_t> fresh;
158  tbb::atomic<uint64_t> deleted;
159  };
160 
162  for (size_t i = 0; i < Address::NUM_FAMILIES; i++) {
163  route_stats[i].reset();
164  }
165  }
166 
167  uint64_t init;
168  uint64_t close;
169  uint64_t nested;
170  uint64_t deletes;
171  uint64_t stale;
172  uint64_t llgr_stale;
173  uint64_t sweep;
174  uint64_t gr_timer;
175  uint64_t llgr_timer;
177  };
178 
179  State state() const { return state_; }
180  const Stats &stats() const { return stats_; }
181  void Close(Event *event);
182  void ProcessEORMarkerReceived(Event *event);
183  virtual void StartRestartTimer(int time);
184  bool RestartTimerCallback();
185  void RestartTimerCallback(Event *event);
186  void ProcessClosure();
187  void CloseComplete();
189  std::string GetStateName(State state) const;
190  std::string GetMembershipStateName(MembershipState state) const;
191  void FillRouteCloseInfo(PeerCloseInfo *close_info) const;
192  void CloseInternal();
193  void MembershipRequest(Event *event);
194  bool MembershipRequestCallback(Event *event);
195  void StaleNotify();
196  bool EventCallback(Event *event);
197  std::string GetEventName(EventType eventType) const;
198  void EnqueueEvent(Event *event);
199  bool close_again() const { return close_again_; }
200  virtual bool AssertMembershipState(bool do_assert = true);
201  virtual bool AssertMembershipReqCount(bool do_assert = true);
202  virtual bool AssertSweepState(bool do_assert = true);
203  virtual bool AssertMembershipManagerInUse(bool do_assert = true);
206  }
207 
208  virtual bool CanUseMembershipManager() const;
209  virtual void GetRegisteredRibs(std::list<BgpTable *> *tables);
210  virtual bool IsRegistered(BgpTable *table) const;
211  virtual void Unregister(BgpTable *table);
212  virtual void WalkRibIn(BgpTable *table);
213  virtual void UnregisterRibOut(BgpTable *table);
214  virtual bool IsRibInRegistered(BgpTable *table) const;
215  virtual void UnregisterRibIn(BgpTable *table);
216 
219  boost::scoped_ptr<WorkQueue<Event *> > event_queue_;
222  bool graceful_;
228  tbb::atomic<int> membership_req_pending_;
229 };
230 
231 #endif // SRC_BGP_PEER_CLOSE_MANAGER_H_
IPeerClose * peer_close_
virtual bool IsRegistered(BgpTable *table) const
tbb::atomic< int > membership_req_pending_
Event(EventType event_type, bool graceful)
void set_membership_state(MembershipState state)
MembershipState membership_state_
std::string GetEventName(EventType eventType) const
std::set< Address::Family > Families
Definition: ipeer.h:159
virtual void StartRestartTimer(int time)
bool IsQueueEmpty() const
IPeerClose * peer_close() const
friend class GracefulRestartTest
bool IsInLlgrTimerWaitState() const
Family
Definition: address.h:24
bool close_again() const
bool IsInGRTimerWaitState() const
virtual void WalkRibIn(BgpTable *table)
IPeerClose::Families * families()
bool IsInDeleteState() const
virtual bool CanUseMembershipManager() const
void ProcessEORMarkerReceived(Address::Family family)
void EnqueueEvent(Event *event)
State state() const
PeerCloseManager(IPeerClose *peer_close, boost::asio::io_context *io_service)
friend class BgpServerUnitTest
virtual void Unregister(BgpTable *table)
virtual bool AssertMembershipReqCount(bool do_assert=true)
Event
Definition: http_client.h:27
virtual bool AssertSweepState(bool do_assert=true)
std::string GetMembershipStateName(MembershipState state) const
bool IsMembershipInUse() const
RouteStats route_stats[Address::NUM_FAMILIES]
bool IsCloseInProgress() const
void UpdateRouteStats(Address::Family family, const BgpPath *old_path, uint32_t path_flags) const
bool EventCallback(Event *event)
virtual void UnregisterRibIn(BgpTable *table)
virtual bool IsRibInRegistered(BgpTable *table) const
virtual void GetRegisteredRibs(std::list< BgpTable * > *tables)
void Close(bool graceful)
void FillRouteCloseInfo(PeerCloseInfo *close_info) const
const Stats & stats() const
bool MembershipPathCallback(DBTablePartBase *root, BgpRoute *rt, BgpPath *path)
friend class PeerCloseManagerTest
virtual bool AssertMembershipManagerInUse(bool do_assert=true)
std::string GetStateName(State state) const
virtual void UnregisterRibOut(BgpTable *table)
Event(EventType event_type)
BgpNeighborResp * FillCloseInfo(BgpNeighborResp *resp) const
Event(EventType event_type, Address::Family family)
virtual bool AssertMembershipState(bool do_assert=true)
Definition: timer.h:54
IPeerClose::Families families_
boost::scoped_ptr< WorkQueue< Event * > > event_queue_
friend class PeerCloseTest
bool IsMembershipInWait() const