/
bgp_peer_close.cc
391 lines (330 loc) · 12.8 KB
/
bgp_peer_close.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/*
* Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
*/
#include "bgp/bgp_peer_close.h"
#include "bgp/bgp_log.h"
#include "bgp/bgp_peer_membership.h"
#include "bgp/bgp_peer_types.h"
#include "bgp/bgp_route.h"
#include "bgp/bgp_server.h"
#define PEER_CLOSE_MANAGER_LOG(msg) \
BGP_LOG_PEER(Event, peer_, SandeshLevel::SYS_INFO, BGP_LOG_FLAG_ALL, \
BGP_PEER_DIR_NA, "PeerCloseManager: State " << GetStateName(state_) << \
", CloseAgain? " << (close_again_ ? "Yes" : "No") << ": " << msg);
#define MOVE_TO_STATE(state) \
do { \
assert(state_ != state); \
PEER_CLOSE_MANAGER_LOG("Move to state " << GetStateName(state)); \
state_ = state; \
} while (false)
// Create an instance of PeerCloseManager with back reference to parent IPeer
PeerCloseManager::PeerCloseManager(IPeer *peer) :
peer_(peer), stale_timer_(NULL), sweep_timer_(NULL), state_(NONE),
close_again_(false) {
stats_.init++;
if (peer->server()) {
stale_timer_ = TimerManager::CreateTimer(*peer->server()->ioservice(),
"Graceful Restart StaleTimer");
sweep_timer_ = TimerManager::CreateTimer(*peer->server()->ioservice(),
"Graceful Restart SweepTimer");
}
}
PeerCloseManager::~PeerCloseManager() {
TimerManager::DeleteTimer(stale_timer_);
TimerManager::DeleteTimer(sweep_timer_);
}
const std::string PeerCloseManager::GetStateName(State state) const {
switch (state) {
case NONE:
return "NONE";
case GR_TIMER:
return "GR_TIMER";
case STALE:
return "STALE";
case SWEEP:
return "SWEEP";
case DELETE:
return "DELETE";
}
assert(false);
return "";
}
// Trigger closure of an IPeer
//
// Graceful close_state_: NONE
// 1. RibIn Stale Marking and Ribout deletion close_state_: STALE
// 2. StateMachine restart and GR timer start close_state_: GR_TIMER
//
// Peer IsReady() in timer callback
// 3. RibIn Sweep and Ribout Generation close_state_: SWEEP
// 4. UnregisterPeerComplete close_state_: NONE
//
// Peer not IsReady() in timer callback
// Goto step A
// Close() call during any state other than NONE and DELETE
// Cancel GR timer and restart GR Closure all over again
//
// NonGraceful close_state_ = * (except DELETE)
// A. RibIn deletion and Ribout deletion close_state_ = DELETE
// B. UnregisterPeerComplete => Peers delete/StateMachine restart
// close_state_ = NONE
void PeerCloseManager::Close() {
tbb::recursive_mutex::scoped_lock lock(mutex_);
stats_.close++;
// Ignore nested closures
if (close_again_) {
stats_.nested++;
PEER_CLOSE_MANAGER_LOG("Nested close calls ignored");
return;
}
switch (state_) {
case NONE:
ProcessClosure();
break;
case GR_TIMER:
PEER_CLOSE_MANAGER_LOG("Nested close: Restart GR");
close_again_ = true;
CloseComplete();
break;
case STALE:
case SWEEP:
case DELETE:
PEER_CLOSE_MANAGER_LOG("Nested close");
close_again_ = true;
break;
}
}
// Process RibIn staling related activities during peer closure
// Return true if at least ome time is started, false otherwise
void PeerCloseManager::StartRestartTimer(int time) {
tbb::recursive_mutex::scoped_lock lock(mutex_);
if (state_ != GR_TIMER)
return;
stale_timer_->Cancel();
PEER_CLOSE_MANAGER_LOG("GR Timer started to fire after " << time <<
" seconds");
stale_timer_->Start(time,
boost::bind(&PeerCloseManager::RestartTimerCallback, this));
}
bool PeerCloseManager::RestartTimerCallback() {
tbb::recursive_mutex::scoped_lock lock(mutex_);
PEER_CLOSE_MANAGER_LOG("GR Timer callback started");
if (state_ == GR_TIMER)
ProcessClosure();
return false;
}
// Route stale timer callback. If the peer has come back up, sweep routes for
// those address families that are still active. Delete the rest
void PeerCloseManager::ProcessClosure() {
// If the peer is back up and this address family is still supported,
// sweep old paths which may not have come back in the new session
switch (state_) {
case NONE:
if (!peer_->peer_close()->IsCloseGraceful()) {
MOVE_TO_STATE(DELETE);
stats_.deletes++;
} else {
MOVE_TO_STATE(STALE);
stats_.stale++;
peer_->peer_close()->GracefulRestartStale();
}
break;
case GR_TIMER:
if (peer_->IsReady()) {
MOVE_TO_STATE(SWEEP);
stats_.sweep++;
} else {
MOVE_TO_STATE(DELETE);
stats_.deletes++;
}
break;
case STALE:
case SWEEP:
case DELETE:
assert(false);
return;
}
if (state_ == DELETE)
peer_->peer_close()->CustomClose();
peer_->server()->membership_mgr()->UnregisterPeer(peer_,
boost::bind(&PeerCloseManager::GetCloseAction, this, _1, state_),
boost::bind(&PeerCloseManager::UnregisterPeerComplete, this, _1, _2));
}
bool PeerCloseManager::IsCloseInProgress() {
tbb::recursive_mutex::scoped_lock lock(mutex_);
return state_ != NONE;
}
void PeerCloseManager::CloseComplete() {
MOVE_TO_STATE(NONE);
stale_timer_->Cancel();
sweep_timer_->Cancel();
stats_.init++;
// Nested closures trigger fresh GR
if (close_again_) {
close_again_ = false;
Close();
}
}
bool PeerCloseManager::ProcessSweepStateActions() {
assert(state_ == SWEEP);
// Notify clients to trigger sweep as appropriate.
peer_->peer_close()->GracefulRestartSweep();
CloseComplete();
return false;
}
void PeerCloseManager::TriggerSweepStateActions() {
PEER_CLOSE_MANAGER_LOG("Sweep Timer started to fire right away");
sweep_timer_->Cancel();
sweep_timer_->Start(0,
boost::bind(&PeerCloseManager::ProcessSweepStateActions, this));
}
// Concurrency: Runs in the context of the BGP peer rib membership task.
//
// Close process for this peer in terms of walking RibIns and RibOuts are
// complete. Do the final cleanups necessary and notify interested party
void PeerCloseManager::UnregisterPeerComplete(IPeer *ipeer, BgpTable *table) {
tbb::recursive_mutex::scoped_lock lock(mutex_);
assert(state_ == STALE || state_ == SWEEP || state_ == DELETE);
PEER_CLOSE_MANAGER_LOG("RibWalk completed");
if (state_ == DELETE) {
peer_->peer_close()->Delete();
MOVE_TO_STATE(NONE);
stats_.init++;
close_again_ = false;
return;
}
if (state_ == STALE) {
// If any stale timer has to be launched, then to wait for some time
// hoping for the peer (and the paths) to come back up.
peer_->peer_close()->CloseComplete();
MOVE_TO_STATE(GR_TIMER);
StartRestartTimer(PeerCloseManager::kDefaultGracefulRestartTimeMsecs);
stats_.gr_timer++;
return;
}
TriggerSweepStateActions();
}
// Get the type of RibIn close action at start (Not during graceful restart
// timer callback, where in we walk the Rib again to sweep the routes)
int PeerCloseManager::GetCloseAction(IPeerRib *peer_rib, State state) {
int action = MembershipRequest::INVALID;
if ((state == STALE || state == DELETE) && peer_rib->IsRibOutRegistered())
action |= static_cast<int>(MembershipRequest::RIBOUT_DELETE);
if (!peer_rib->IsRibInRegistered())
return action;
// If graceful restart timer is already running, then this is a second
// close before previous restart has completed. Abort graceful restart
// and delete the routes instead
switch (state) {
case NONE:
break;
case STALE:
action |= static_cast<int>(MembershipRequest::RIBIN_STALE);
break;
case GR_TIMER:
break;
case SWEEP:
action |= static_cast<int>(MembershipRequest::RIBIN_SWEEP);
break;
case DELETE:
action |= static_cast<int>(MembershipRequest::RIBIN_DELETE);
break;
}
return (action);
}
// For graceful-restart, we take mark-and-sweep approach instead of directly
// deleting the paths. In the first walk, local-preference is lowered so that
// the paths are least preferred and they are marked stale. After some time, if
// the peer session does not come back up, we delete all the paths and the peer
// itself. If the session did come back up, we flush only those paths that were
// not learned again in the new session.
//
// Concurrency: Runs in the context of the DB Walker task launched by peer rib
// membership manager
//
// DBWalker callback routine for each of the RibIn prefix.
void PeerCloseManager::ProcessRibIn(DBTablePartBase *root, BgpRoute *rt,
BgpTable *table, int action_mask) {
DBRequest::DBOperation oper;
BgpAttrPtr attrs;
MembershipRequest::Action action;
// Look for the flags that we care about
action = static_cast<MembershipRequest::Action>(action_mask &
(MembershipRequest::RIBIN_STALE |
MembershipRequest::RIBIN_SWEEP |
MembershipRequest::RIBIN_DELETE));
if (action == MembershipRequest::INVALID)
return;
bool notify_rt = false;
// Process all paths sourced from this peer_. Multiple paths could exist
// in ecmp cases.
for (Route::PathList::iterator it = rt->GetPathList().begin(), next = it;
it != rt->GetPathList().end(); it = next) {
next++;
BgpPath *path = static_cast<BgpPath *>(it.operator->());
// Skip paths from other peers.
if (path->GetPeer() != peer_)
continue;
// Skip resolved paths - PathResolver is responsible for them.
if (path->IsResolved())
continue;
// Skip secondary paths.
if (dynamic_cast<BgpSecondaryPath *>(path))
continue;
uint32_t stale = 0;
switch (action) {
case MembershipRequest::RIBIN_SWEEP:
// Stale paths must be deleted
if (!path->IsStale())
return;
path->ResetStale();
stats_.deleted_state_paths++;
oper = DBRequest::DB_ENTRY_DELETE;
attrs = NULL;
break;
case MembershipRequest::RIBIN_DELETE:
// This path must be deleted. Hence attr is not required
stats_.deleted_paths++;
oper = DBRequest::DB_ENTRY_DELETE;
attrs = NULL;
break;
case MembershipRequest::RIBIN_STALE:
// This path must be marked for staling. Update the local
// preference and update the route accordingly
oper = DBRequest::DB_ENTRY_ADD_CHANGE;
stats_.marked_state_paths++;
// Update attrs with maximum local preference so that this path
// is least preferred
// TODO(ananth): Check for the right local-pref value to use
attrs = peer_->server()->attr_db()->\
ReplaceLocalPreferenceAndLocate(path->GetAttr(), 1);
stale = BgpPath::Stale;
break;
default:
return;
}
// Feed the route modify/delete request to the table input process.
notify_rt |= table->InputCommon(root, rt, path, peer_, NULL, oper,
attrs, path->GetPathId(),
path->GetFlags() | stale,
path->GetLabel());
}
table->InputCommonPostProcess(root, rt, notify_rt);
}
void PeerCloseManager::FillCloseInfo(BgpNeighborResp *resp) {
tbb::recursive_mutex::scoped_lock lock(mutex_);
PeerCloseInfo peer_close_info;
peer_close_info.state = GetStateName(state_);
peer_close_info.close_again = close_again_;
peer_close_info.init = stats_.init;
peer_close_info.close = stats_.close;
peer_close_info.nested = stats_.nested;
peer_close_info.deletes = stats_.deletes;
peer_close_info.stale = stats_.stale;
peer_close_info.sweep = stats_.sweep;
peer_close_info.gr_timer = stats_.gr_timer;
peer_close_info.deleted_state_paths = stats_.deleted_state_paths;
peer_close_info.deleted_paths = stats_.deleted_paths;
peer_close_info.marked_state_paths = stats_.marked_state_paths;
resp->set_peer_close_info(peer_close_info);
}