OpenSDN source code
bgp_peer_close.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  * Copyright Contributors to the OpenSDN project
4  */
5 
6 #include <boost/foreach.hpp>
7 #include <boost/assign/list_of.hpp>
8 #include <boost/scoped_ptr.hpp>
9 
10 #include "base/task_annotations.h"
11 #include "net/bgp_af.h"
12 #include "bgp/bgp_factory.h"
13 #include "bgp/bgp_path.h"
14 #include "bgp/bgp_log.h"
15 #include "bgp/bgp_peer.h"
16 #include "bgp/bgp_peer_close.h"
17 #include "bgp/bgp_peer_types.h"
18 #include "bgp/peer_close_manager.h"
19 
20 using boost::assign::list_of;
21 using std::back_inserter;
22 using std::includes;
23 using std::string;
24 using std::vector;
25 
27  : peer_(peer), flap_count_(0) {
28 }
29 
31 }
32 
34  return peer_->close_manager();
35 }
36 
38  return peer_->CustomClose();
39 }
40 
43 }
44 
46 }
47 
49  negotiated_families_.clear();
50 }
51 
52 bool BgpPeerClose::IsReady() const {
53  return peer_->IsReady();
54 }
55 
57  return peer_;
58 }
59 
60 // Return the time to wait for, in seconds to exit GR_TIMER state.
62  return gr_params_.time;
63 }
64 
65 // Return the time to wait for, in seconds to exit LLGR_TIMER state.
67  return llgr_params_.time;
68 }
69 
71  peer_->ReceiveEndOfRIB(family, 0);
72 }
73 
74 const char *BgpPeerClose::GetTaskName() const {
75  return "bgp::Config";
76 }
77 
79  return 0;
80 }
81 
84 }
85 
87  return peer_->server()->IsGRHelperModeEnabled();
88 }
89 
90 const std::vector<std::string> &BgpPeerClose::PeerNegotiatedFamilies() const {
91  return peer_->negotiated_families();
92 }
93 
95  return peer_->IsDeleted();
96 }
97 
99  return peer_->IsAdminDown();
100 }
101 
103  return peer_->server()->IsDeleted();
104 }
105 
107  return peer_->server()->admin_down();
108 }
109 
111  return GetManager()->IsInGRTimerWaitState();
112 }
113 
116 }
117 
118 const std::vector<std::string> &BgpPeerClose::negotiated_families() const {
119  return negotiated_families_;
120 }
121 
122 const std::vector<BgpProto::OpenMessage::Capability *> &
124  return peer_->capabilities();
125 }
126 
128  const BgpPath *old_path,
129  uint32_t path_flags) const {
130  GetManager()->UpdateRouteStats(family, old_path, path_flags);
131 }
132 
133 void BgpPeerClose::Close(bool graceful) {
134  // Abort GR-Closure if this request is for non-graceful closure.
135  // Reset GR-Closure if previous closure is still in progress or if
136  // this is a flip (from established state).
137  if (!graceful || flap_count_ != peer_->total_flap_count()) {
138  if (flap_count_ != peer_->total_flap_count()) {
139  flap_count_++;
140  assert(peer_->total_flap_count() == flap_count_);
141  }
142  GetManager()->Close(graceful);
143  return;
144  }
145 
146  // Ignore if close is already in progress.
147  if (GetManager()->IsCloseInProgress() && !IsInGRTimerWaitState())
148  return;
149 
150  if (peer_->IsDeleted()) {
151  peer_->RetryDelete();
152  } else {
153  CustomClose();
154  CloseComplete();
155  }
156 }
157 
161  gr_families_.clear();
162  llgr_families_.clear();
163  negotiated_families_.clear();
164  if (peer_->IsDeleted()) {
165  peer_->RetryDelete();
166  } else {
167  CloseComplete();
168  }
169 }
170 
172  // Check if GR Helper mode is disabled.
173  if (!IsGRHelperModeEnabled()) {
174  BGP_LOG_PEER(Message, peer_, SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_ALL,
175  BGP_PEER_DIR_IN, "GR Helper mode is not enabled because it is not"
176  " configured");
177  return false;
178  }
179 
180  // Check if GR is supported by the peer.
181  if (gr_params_.families.empty()) {
182  BGP_LOG_PEER(Message, peer_, SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_ALL,
183  BGP_PEER_DIR_IN, "GR Helper mode is not enabled because received "
184  "GR address families list is empty");
185  return false;
186  }
187 
188  // LLGR should be in effect to enable GR Helper mode with 0 restart time.
190  BGP_LOG_PEER(Message, peer_, SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_ALL,
191  BGP_PEER_DIR_IN, "GR Helper mode is not enabled because received "
192  "GR restart time value is 0 seconds and (there is no applicable "
193  "LLGR as well");
194  return false;
195  }
196 
197  // Abort GR if currently negotiated families differ from already
198  // staled address families.
199  if (!negotiated_families().empty() &&
201  BGP_LOG_PEER(Message, peer_, SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_ALL,
202  BGP_PEER_DIR_IN, "GR Helper mode is aborted because received "
203  "GR families list differs from the list received last time");
204  return false;
205  }
206 
207  // If GR is not supported for any of the negotiated address family,
208  // then consider GR as not supported
210  BGP_LOG_PEER(Message, peer_, SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_ALL,
211  BGP_PEER_DIR_IN, "GR Helper mode is not enabled because GR address "
212  "families differs from negotiated address families");
213  return false;
214  }
215 
216  // Make sure that forwarding state is preserved for all families in
217  // the restarting speaker. (Except for ERMVPN)
218  for (auto family : gr_params_.families) {
219  // Check if forwarding state was preserved during restart.
220  if (family.forwarding_state_preserved())
221  continue;
222 
223  // Ignore forwarding-state preservation check for certain families.
224  Address::Family addr_family =
225  BgpAf::AfiSafiToFamily(family.afi, family.safi);
226  if (addr_family == Address::ERMVPN || addr_family == Address::MVPN)
227  continue;
228 
229  string family_str = Address::FamilyToString(addr_family);
230  BGP_LOG_PEER(Message, peer_, SandeshLevel::SYS_DEBUG,
231  BGP_LOG_FLAG_ALL, BGP_PEER_DIR_IN, "GR Helper mode is not "
232  "enabled because after restart, GR forwarding state is not "
233  "preserved for address family " << family_str);
234  return false;
235  }
236  return true;
237 }
238 
239 // If the peer is deleted or administratively held down, do not attempt
240 // graceful restart
242  if (IsPeerDeleted()) {
243  BGP_LOG_PEER(Message, peer_, SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_ALL,
244  BGP_PEER_DIR_IN, "GR Helper mode is not enabled because BgpPeer has"
245  " been deleted");
246  return false;
247  }
248 
249  if (IsPeerAdminDown()) {
250  BGP_LOG_PEER(Message, peer_, SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_ALL,
251  BGP_PEER_DIR_IN, "GR Helper mode is not enabled because BgpPeer has"
252  " been held administratively down");
253  return false;
254  }
255 
256  if (IsServerDeleted()) {
257  BGP_LOG_PEER(Message, peer_, SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_ALL,
258  BGP_PEER_DIR_IN, "GR Helper mode is not enabled because BgpServer "
259  "has been deletd");
260  return false;
261  }
262 
263  if (IsServerAdminDown()) {
264  BGP_LOG_PEER(Message, peer_, SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_ALL,
265  BGP_PEER_DIR_IN, "GR Helper mode is not enabled because BgpServer "
266  "has been held administratively down");
267  return false;
268  }
269 
270  if (!IsGRReady())
271  return false;
272  return true;
273 }
274 
275 // LLGR families should be identical to GR families.
277  // Keep a sorted list of unsupported families.
278  static vector<string> unsupported_families = list_of
281 
283  return true;
284 
285  // Ignore families mis-match for certain families.
286  vector<string> differing_families;
287  std::set_symmetric_difference(gr_families_.begin(), gr_families_.end(),
288  llgr_families_.begin(), llgr_families_.end(),
289  back_inserter(differing_families));
290 
291  // Ignore if differing families are only those which are unsupported.
292  return includes(unsupported_families.begin(), unsupported_families.end(),
293  differing_families.begin(), differing_families.end());
294 }
295 
296 // Check if we need to trigger Long Lived Graceful Restart. In addition to
297 // normal GR checks, we also need to check LLGR capability was negotiated
298 // and non-zero restart time was inferred.
300  if (!IsCloseGraceful())
301  return false;
303 }
304 
306  if (llgr_params_.families.empty()) {
307  BGP_LOG_PEER(Message, peer_, SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_ALL,
309  "No LLGR support due to empty LLGR address families list");
310  return false;
311  }
312  if (!llgr_params_.time) {
313  BGP_LOG_PEER(Message, peer_, SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_ALL,
314  BGP_PEER_DIR_IN, "No LLGR support due to zero time value");
315  return false;
316  }
317 
319  BGP_LOG_PEER(Message, peer_, SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_ALL,
320  BGP_PEER_DIR_IN, "No LLGR support due to dissimilar "
321  "GR address families and LLGR address families");
322  return false;
323  }
324 
325  // Make sure that forwarding state is preserved for all families in
326  // the restarting speaker.
327  for (auto family : llgr_params_.families) {
328  // Ignore forwarding-state preservation check for certain families.
329  Address::Family addr_family =
330  BgpAf::AfiSafiToFamily(family.afi, family.safi);
331  if (addr_family == Address::ERMVPN || addr_family == Address::MVPN)
332  continue;
333 
334  if (!family.forwarding_state_preserved()) {
335  string family_str = Address::FamilyToString(addr_family);
336  BGP_LOG_PEER(Message, peer_, SandeshLevel::SYS_DEBUG,
337  BGP_LOG_FLAG_ALL, BGP_PEER_DIR_IN, "GR Helper mode is not "
338  "enabled because after restart, LLGR forwarding state is not "
339  "preserved for address family " << family_str);
340  return false;
341  }
342  }
343  return true;
344 }
345 
348 }
349 
350 // Close process for this peer is complete. Restart the state machine and
351 // attempt to bring up session with the neighbor
353  if (!peer_->IsDeleted() && !peer_->IsAdminDown())
355 }
356 
358  families->clear();
359  for (const auto& family : gr_families_) {
360  families->insert(Address::FamilyFromString(family));
361  }
362 }
363 
365  families->clear();
366  for (const auto& family : llgr_families_) {
367  families->insert(Address::FamilyFromString(family));
368  }
369 }
370 
372  BgpProto::OpenMessage::OptParam *opt_param) {
373  vector<Address::Family> gr_families;
374  vector<uint8_t> afi_flags;
375 
376  // Indicate to the Peer if we are in restarting phase.
377  bool restarted = peer_->IsServerStartingUp();
378 
379  // Indicate EOR support by default.
380  if (!peer_->server()->global_config()->gr_enable()) {
383  afi_flags,
384  gr_families);
385  opt_param->capabilities.push_back(gr_cap);
386  return;
387  }
388 
389  for (auto family : peer_->supported_families()) {
390  if (!peer_->LookupFamily(family))
391  continue;
392  gr_families.push_back(family);
393  afi_flags.push_back(BgpProto::OpenMessage::Capability::GR::
394  ForwardingStatePreservedFlag);
395  }
396 
397  uint16_t time = peer_->server()->GetGracefulRestartTime();
399  BgpProto::OpenMessage::Capability::GR::Encode(time, restarted, true,
400  afi_flags, gr_families);
401  opt_param->capabilities.push_back(gr_cap);
402 }
403 
404 // Process received GR and LLGR Capabilities. Return true if the values are sane
405 // to proceed with further processing. Return false if not to abort any ongoing
406 // GR and instead trigger non-graceful closure.
407 bool BgpPeerClose::SetGRCapabilities(BgpPeerInfoData *peer_info) {
410  &gr_families_);
411 
413  capabilities());
415  &llgr_families_);
416 
417  if (peer_info) {
418  peer_info->set_graceful_restart_families(gr_families_);
419  peer_info->set_long_lived_graceful_restart_families(llgr_families_);
420  peer_info->set_graceful_restart_time(GetGracefulRestartTime());
421  peer_info->set_long_lived_graceful_restart_time(
423  assert(!peer_info->get_name().empty());
424  BGP_UVE_SEND(BGPPeerInfo, *peer_info);
425  }
426 
427  // If we are not in GR Timer waiting state, then there is no case to abort
428  // GR when new session is coming up.
429  if (!IsInGRTimerWaitState())
430  return true;
431 
432  // If LLGR is no longer supported, terminate GR right away. This can happen
433  // due to mis-match between gr and llgr afis. For now, we expect an
434  // identical set.
436  return IsCloseLongLivedGraceful();
437  return IsCloseGraceful();
438 }
439 
441  BgpProto::OpenMessage::OptParam *opt_param) {
442  if (!peer_->server()->global_config()->gr_enable() ||
444  return;
445  }
446 
447  vector<Address::Family> llgr_families;
448  for (auto family : peer_->supported_families()) {
449  if (peer_->LookupFamily(family))
450  llgr_families.push_back(family);
451  }
452 
453  uint32_t time = peer_->server()->GetLongLivedGracefulRestartTime();
454  uint8_t afi_flags =
458  llgr_families);
459  opt_param->capabilities.push_back(llgr_cap);
460 }
461 
462 void BgpPeerClose::FillNeighborInfo(BgpNeighborResp *bnr) const {
463  bnr->set_graceful_restart_address_families(gr_families_);
464  bnr->set_long_lived_graceful_restart_address_families(llgr_families_);
465  bnr->set_graceful_restart_time(GetGracefulRestartTime());
466  bnr->set_long_lived_graceful_restart_time(
468 }
#define BGP_LOG_FLAG_ALL
Definition: bgp_log.h:45
#define BGP_LOG_PEER(type, peer, level, flags, dir, arg)
Definition: bgp_log.h:160
#define BGP_PEER_DIR_IN
Definition: bgp_log.h:140
#define BGP_UVE_SEND(type, object)
Definition: bgp_log.h:47
Family
Definition: address.h:25
@ ERMVPN
Definition: address.h:33
@ MVPN
Definition: address.h:34
static Family FamilyFromString(const std::string &family)
Definition: address.cc:55
static std::string FamilyToString(Family fmly)
Definition: address.cc:64
static Address::Family AfiSafiToFamily(uint16_t afi, uint8_t safi)
Definition: bgp_af.cc:72
bool gr_enable() const
Definition: bgp_config.h:645
virtual void GracefulRestartStale()
virtual void GracefulRestartSweep()
void FillNeighborInfo(BgpNeighborResp *bnr) const
BgpPeer * peer_
virtual void Close(bool graceful)
uint64_t flap_count_
virtual void GetGracefulRestartFamilies(Families *families) const
virtual const std::vector< BgpProto::OpenMessage::Capability * > & capabilities() const
virtual bool IsServerAdminDown() const
virtual bool IsServerDeleted() const
virtual bool IsInLlgrTimerWaitState() const
std::vector< std::string > gr_families_
std::vector< std::string > llgr_families_
virtual const char * GetTaskName() const
virtual bool IsPeerAdminDown() const
virtual PeerCloseManager * GetManager() const
virtual int GetGracefulRestartTime() const
virtual ~BgpPeerClose()
virtual IPeer * peer() const
virtual bool IsGRReady() const
virtual const std::vector< std::string > & PeerNegotiatedFamilies() const
virtual void CloseComplete()
bool IsLlgrSupportedForFamilies() const
virtual int GetLongLivedGracefulRestartTime() const
virtual void MembershipRequestCallbackComplete()
virtual int GetTaskInstance() const
BgpProto::OpenMessage::Capability::LLGR llgr_params_
void AddLLGRCapabilities(BgpProto::OpenMessage::OptParam *opt_param)
virtual bool IsPeerDeleted() const
virtual bool IsGRHelperModeEnabled() const
virtual bool IsCloseLongLivedGraceful() const
virtual void LongLivedGracefulRestartStale()
std::vector< std::string > negotiated_families_
virtual bool IsReady() const
virtual void RestartStateMachine()
virtual const std::vector< std::string > & negotiated_families() const
BgpProto::OpenMessage::Capability::GR gr_params_
virtual void Delete()
virtual bool IsInGRTimerWaitState() const
virtual void GetLongLivedGracefulRestartFamilies(Families *families) const
bool SetGRCapabilities(BgpPeerInfoData *peer_info)
bool IsCloseLongLivedGracefulInternal() const
virtual bool IsCloseGraceful() const
virtual void UpdateRouteStats(Address::Family family, const BgpPath *old_path, uint32_t path_flags) const
virtual void ReceiveEndOfRIB(Address::Family family)
std::set< Address::Family > Families
BgpPeerClose(BgpPeer *peer)
void AddGRCapabilities(BgpProto::OpenMessage::OptParam *opt_param)
virtual void CustomClose()
const std::vector< BgpProto::OpenMessage::Capability * > & capabilities() const
Definition: bgp_peer.h:349
void CustomClose()
Definition: bgp_peer.cc:1251
StateMachine * state_machine()
Definition: bgp_peer.h:328
virtual bool IsReady() const
Definition: bgp_peer.cc:1207
bool IsDeleted() const
Definition: bgp_peer.cc:1314
virtual BgpServer * server()
Definition: bgp_peer.h:159
void ReceiveEndOfRIB(Address::Family family, size_t msgsize)
Definition: bgp_peer.cc:271
uint64_t total_flap_count() const
Definition: bgp_peer.h:253
bool IsAdminDown() const
Definition: bgp_peer.h:223
const std::vector< std::string > & negotiated_families() const
Definition: bgp_peer.h:344
virtual bool IsServerStartingUp() const
Definition: bgp_peer.cc:320
void RetryDelete()
Definition: bgp_peer.cc:2471
PeerCloseManager * close_manager()
Definition: bgp_peer.h:357
static const std::vector< Address::Family > & supported_families()
Definition: bgp_peer.h:353
bool LookupFamily(Address::Family family)
Definition: bgp_peer.h:199
bool IsDeleted() const
Definition: bgp_server.cc:423
uint16_t GetGracefulRestartTime() const
Definition: bgp_server.cc:705
BgpGlobalSystemConfig * global_config()
Definition: bgp_server.h:298
bool admin_down() const
Definition: bgp_server.h:210
bool IsGRHelperModeEnabled() const
Definition: bgp_server.cc:743
uint32_t GetLongLivedGracefulRestartTime() const
Definition: bgp_server.cc:711
Definition: ipeer.h:187
void Close(bool graceful)
void UpdateRouteStats(Address::Family family, const BgpPath *old_path, uint32_t path_flags) const
bool IsInGRTimerWaitState() const
bool IsInLlgrTimerWaitState() const
static Capability * Encode(uint16_t gr_time, bool restarted, bool notification, const std::vector< uint8_t > &gr_afi_flags, const std::vector< Address::Family > &gr_families)
Definition: bgp_proto.cc:130
std::vector< Family > families
Definition: bgp_proto.h:171
static void GetFamilies(const GR &gr_params, std::vector< std::string > *families)
Definition: bgp_proto.cc:157
static bool Decode(GR *gr_params, const std::vector< Capability * > &capabilities)
Definition: bgp_proto.cc:176
static void GetFamilies(const LLGR &llgr_params, std::vector< std::string > *families)
Definition: bgp_proto.cc:261
static Capability * Encode(uint32_t llgr_time, uint8_t llgr_afi_flags, const std::vector< Address::Family > &llgr_families)
Definition: bgp_proto.cc:208
static bool Decode(LLGR *llgr_params, const std::vector< Capability * > &capabilities)
Definition: bgp_proto.cc:228
std::vector< Capability * > capabilities
Definition: bgp_proto.h:218
#define CHECK_CONCURRENCY(...)