OpenSDN source code
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 #include <atomic>
11 
12 #include "base/timer.h"
13 #include "base/util.h"
14 #include "base/queue_task.h"
15 #include "db/db_table_walker.h"
16 #include "bgp/ipeer.h"
17 
19 class BgpNeighborResp;
20 class BgpRoute;
21 class BgpTable;
22 class PeerCloseInfo;
23 
24 // PeerCloseManager
25 //
26 // Manager close process of an IPeer (And hence should support both BgpPeers
27 // and XmppPeers)
28 //
29 // Among other things, RibIns and RibOuts of peers must be closed/deleted
30 // completely before a peer can be completely closed/deleted. This class
31 // provides this capability.
32 //
33 // RibIn and RibOut close are handled by invoking Unregister request with
34 // BgpMembershipManager class.
35 //
36 // Once RibIns and RibOuts are processed, notification callback function is
37 // invoked to signal the completion of close process
38 //
40 public:
42  boost::asio::io_context *io_service);
44  virtual ~PeerCloseManager();
45 
46  bool IsMembershipInUse() const {
48  }
49  bool IsMembershipInWait() const {
51  }
52  IPeerClose *peer_close() const { return peer_close_; }
54 
55  bool IsCloseInProgress() const { return state_ != NONE; }
56  bool IsInDeleteState() const { return state_ == DELETE; }
57  bool IsInGRTimerWaitState() const {
58  return state_ == GR_TIMER || state_ == LLGR_TIMER;
59  }
60  bool IsInLlgrTimerWaitState() const { return state_ == LLGR_TIMER; }
61  bool IsQueueEmpty() const { return event_queue_->IsQueueEmpty(); }
62 
63  void Close(bool graceful);
65  void MembershipRequest();
67  BgpNeighborResp *FillCloseInfo(BgpNeighborResp *resp) const;
69  BgpPath *path);
70  void UpdateRouteStats(Address::Family family, const BgpPath *old_path,
71  uint32_t path_flags) const;
72 
73 private:
74  friend class BgpServerUnitTest;
75  friend class PeerCloseTest;
76  friend class PeerCloseManagerTest;
77  friend class GracefulRestartTest;
78 
79  enum State {
89  };
90 
97  };
98 
99  enum EventType {
108  };
109 
110  struct Event {
113  family(Address::UNSPEC) { }
116  family(family) { }
118  graceful(true),
119  family(Address::UNSPEC) { }
121  family(Address::UNSPEC) { }
122 
124  bool graceful;
126  };
127 
128  struct Stats {
129  Stats():
130  init(0),
131  close(0),
132  nested(0),
133  deletes(0),
134  stale(0),
135  llgr_stale(0),
136  sweep(0),
137  gr_timer(0),
138  llgr_timer(0) {
139  }
140 
141  struct RouteStats {
142  RouteStats() { reset(); }
143  bool IsSet() const {
144  return staled || llgr_staled || refreshed || fresh || deleted;
145  }
146 
147  RouteStats& operator=(const RouteStats& other) {
148  if (this != &other) {
149  staled.store(other.staled);
150  llgr_staled.store(other.llgr_staled);
151  refreshed.store(other.refreshed);
152  fresh.store(other.fresh);
153  deleted.store(other.deleted);
154  }
155  return *this;
156  }
157 
158  void reset() {
159  staled = 0;
160  llgr_staled = 0;
161  refreshed = 0;
162  fresh = 0;
163  deleted = 0;
164  }
165 
166  std::atomic<uint64_t> staled;
167  std::atomic<uint64_t> llgr_staled;
168  std::atomic<uint64_t> refreshed;
169  std::atomic<uint64_t> fresh;
170  std::atomic<uint64_t> deleted;
171  };
172 
174  for (size_t i = 0; i < Address::NUM_FAMILIES; i++) {
175  route_stats[i].reset();
176  }
177  }
178 
179  uint64_t init;
180  uint64_t close;
181  uint64_t nested;
182  uint64_t deletes;
183  uint64_t stale;
184  uint64_t llgr_stale;
185  uint64_t sweep;
186  uint64_t gr_timer;
187  uint64_t llgr_timer;
189  };
190 
191  State state() const { return state_; }
192  const Stats &stats() const { return stats_; }
193  void Close(Event *event);
194  void ProcessEORMarkerReceived(Event *event);
195  virtual void StartRestartTimer(int time);
196  bool RestartTimerCallback();
197  void RestartTimerCallback(Event *event);
198  void ProcessClosure();
199  void CloseComplete();
201  std::string GetStateName(State state) const;
202  std::string GetMembershipStateName(MembershipState state) const;
203  void FillRouteCloseInfo(PeerCloseInfo *close_info) const;
204  void CloseInternal();
205  void MembershipRequest(Event *event);
206  bool MembershipRequestCallback(Event *event);
207  void StaleNotify();
208  bool EventCallback(Event *event);
209  std::string GetEventName(EventType eventType) const;
210  void EnqueueEvent(Event *event);
211  bool close_again() const { return close_again_; }
212  virtual bool AssertMembershipState(bool do_assert = true);
213  virtual bool AssertMembershipReqCount(bool do_assert = true);
214  virtual bool AssertSweepState(bool do_assert = true);
215  virtual bool AssertMembershipManagerInUse(bool do_assert = true);
218  }
219 
220  virtual bool CanUseMembershipManager() const;
221  virtual void GetRegisteredRibs(std::list<BgpTable *> *tables);
222  virtual bool IsRegistered(BgpTable *table) const;
223  virtual void Unregister(BgpTable *table);
224  virtual void WalkRibIn(BgpTable *table);
225  virtual void UnregisterRibOut(BgpTable *table);
226  virtual bool IsRibInRegistered(BgpTable *table) const;
227  virtual void UnregisterRibIn(BgpTable *table);
228 
231  boost::scoped_ptr<WorkQueue<Event *> > event_queue_;
234  bool graceful_;
240  std::atomic<int> membership_req_pending_;
241 };
242 
243 #endif // SRC_BGP_PEER_CLOSE_MANAGER_H_
Family
Definition: address.h:24
@ NUM_FAMILIES
Definition: address.h:35
std::set< Address::Family > Families
Definition: ipeer.h:159
bool MembershipPathCallback(DBTablePartBase *root, BgpRoute *rt, BgpPath *path)
virtual bool IsRegistered(BgpTable *table) const
PeerCloseManager(IPeerClose *peer_close, boost::asio::io_context *io_service)
IPeerClose * peer_close_
friend class PeerCloseTest
virtual void UnregisterRibIn(BgpTable *table)
boost::scoped_ptr< WorkQueue< Event * > > event_queue_
virtual void GetRegisteredRibs(std::list< BgpTable * > *tables)
void set_membership_state(MembershipState state)
bool IsMembershipInUse() const
const Stats & stats() const
std::string GetEventName(EventType eventType) const
virtual bool AssertMembershipReqCount(bool do_assert=true)
void Close(bool graceful)
bool EventCallback(Event *event)
friend class BgpServerUnitTest
virtual void StartRestartTimer(int time)
IPeerClose * peer_close() const
virtual void WalkRibIn(BgpTable *table)
BgpNeighborResp * FillCloseInfo(BgpNeighborResp *resp) const
void ProcessEORMarkerReceived(Address::Family family)
std::string GetMembershipStateName(MembershipState state) const
void UpdateRouteStats(Address::Family family, const BgpPath *old_path, uint32_t path_flags) const
virtual bool CanUseMembershipManager() const
void EnqueueEvent(Event *event)
bool IsInDeleteState() const
virtual void UnregisterRibOut(BgpTable *table)
bool IsCloseInProgress() const
std::atomic< int > membership_req_pending_
bool close_again() const
friend class GracefulRestartTest
MembershipState membership_state_
bool IsMembershipInWait() const
IPeerClose::Families * families()
virtual bool AssertSweepState(bool do_assert=true)
friend class PeerCloseManagerTest
std::string GetStateName(State state) const
bool IsInGRTimerWaitState() const
virtual bool AssertMembershipState(bool do_assert=true)
IPeerClose::Families families_
bool IsQueueEmpty() const
virtual bool IsRibInRegistered(BgpTable *table) const
void FillRouteCloseInfo(PeerCloseInfo *close_info) const
bool IsInLlgrTimerWaitState() const
virtual void Unregister(BgpTable *table)
virtual bool AssertMembershipManagerInUse(bool do_assert=true)
Definition: timer.h:57
Event
Definition: http_client.h:29
Event(EventType event_type, bool graceful)
Event(EventType event_type)
Event(EventType event_type, Address::Family family)
RouteStats & operator=(const RouteStats &other)
RouteStats route_stats[Address::NUM_FAMILIES]