/
bgp_peer.h
490 lines (409 loc) · 16.7 KB
/
bgp_peer.h
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
/*
* Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
*/
#ifndef SRC_BGP_BGP_PEER_H__
#define SRC_BGP_BGP_PEER_H__
#include <boost/asio/ip/tcp.hpp>
#include <boost/scoped_ptr.hpp>
#include <tbb/spin_mutex.h>
#include <map>
#include <memory>
#include <set>
#include <string>
#include <vector>
#include "base/lifetime.h"
#include "base/util.h"
#include "base/task_trigger.h"
#include "base/timer.h"
#include "bgp/bgp_config.h"
#include "bgp/bgp_debug.h"
#include "bgp/bgp_peer_key.h"
#include "bgp/bgp_proto.h"
#include "bgp/bgp_rib_policy.h"
#include "bgp/ipeer.h"
#include "bgp/bgp_peer_close.h"
#include "bgp/state_machine.h"
#include "net/address.h"
class BgpNeighborConfig;
class BgpPeerInfo;
class BgpPeerInfoData;
class BgpServer;
class BgpSession;
class BgpSession;
class BgpNeighborResp;
class BgpSandeshContext;
class RoutingInstance;
//
// This contains per address family attributes.
// A BgpPeer contains a vector of pointers to this structure, with an entry
// for each value in Address::Family i.e. the vector can be indexed using a
// Address::Family value.
//
struct BgpPeerFamilyAttributes {
BgpPeerFamilyAttributes(const BgpNeighborConfig *config,
const BgpFamilyAttributesConfig &family_config);
uint8_t loop_count;
uint32_t prefix_limit;
IpAddress gateway_address;
};
//
// Comparator for BgpPeerFamilyAttributes.
//
struct BgpPeerFamilyAttributesCompare {
int operator()(const BgpPeerFamilyAttributes *lhs,
const BgpPeerFamilyAttributes *rhs) const {
if (lhs && rhs) {
KEY_COMPARE(lhs->loop_count, rhs->loop_count);
KEY_COMPARE(lhs->prefix_limit, rhs->prefix_limit);
KEY_COMPARE(lhs->gateway_address, rhs->gateway_address);
} else {
KEY_COMPARE(lhs, rhs);
}
return 0;
}
};
// A BGP peer along with its session and state machine.
class BgpPeer : public IPeer {
public:
static const int kMinEndOfRibSendTimeUsecs = 10000000; // 10 Seconds
static const int kMaxEndOfRibSendTimeUsecs = 60000000; // 60 Seconds
static const int kEndOfRibSendRetryTimeMsecs = 2000; // 2 Seconds
typedef std::set<Address::Family> AddressFamilyList;
typedef AuthenticationData::KeyType KeyType;
BgpPeer(BgpServer *server, RoutingInstance *instance,
const BgpNeighborConfig *config);
virtual ~BgpPeer();
// Interface methods
// thread-safe
virtual std::string ToString() const;
virtual std::string ToUVEKey() const;
// thread: bgp::SendTask
// Used to send an UPDATE message on the socket.
virtual bool SendUpdate(const uint8_t *msg, size_t msgsize);
virtual bool FlushUpdate();
// thread: bgp::config
void ConfigUpdate(const BgpNeighborConfig *config);
void ClearConfig();
// thread: event manager thread.
// Invoked from BgpServer when a session is accepted.
bool AcceptSession(BgpSession *session);
BgpSession *CreateSession();
virtual void SetAdminState(bool down);
// Messages
// thread: bgp::StateMachine
void SendOpen(TcpSession *session);
// thread: bgp::StateMachine, KA timer handler
void SendKeepalive(bool from_timer);
// thread: bgp::StateMachine, io::ReaderTask
void SendNotification(BgpSession *, int code, int subcode = 0,
const std::string &data = std::string());
// thread: io::ReaderTask
void ProcessUpdate(const BgpProto::Update *msg, size_t msgsize = 0);
// thread: io::ReaderTask
virtual bool ReceiveMsg(BgpSession *session, const u_int8_t *msg,
size_t size);
void StartKeepaliveTimer();
bool KeepaliveTimerRunning();
void SetSendReady();
// thread: io::ReaderTask
bool SetCapabilities(const BgpProto::OpenMessage *msg);
void ResetCapabilities();
// Table registration.
void RegisterAllTables();
// accessors
virtual BgpServer *server() { return server_; }
virtual BgpServer *server() const { return server_; }
uint32_t PeerAddress() const { return peer_key_.address(); }
const std::string peer_address_string() const {
return peer_key_.endpoint.address().to_string();
}
const BgpPeerKey &peer_key() const { return peer_key_; }
uint16_t peer_port() const { return peer_port_; }
std::string transport_address_string() const;
std::string gateway_address_string(Address::Family family) const;
const std::string &peer_name() const { return peer_name_; }
const std::string &peer_basename() const { return peer_basename_; }
std::string router_type() const { return router_type_; }
TcpSession::Endpoint endpoint() const { return endpoint_; }
StateMachine::State GetState() const;
virtual const std::string GetStateName() const;
void set_session(BgpSession *session);
void clear_session();
BgpSession *session();
uint16_t hold_time() const { return hold_time_; }
as_t local_as() const { return local_as_; }
as_t peer_as() const { return peer_as_; }
// The BGP Identifier in host byte order.
virtual uint32_t local_bgp_identifier() const;
std::string local_bgp_identifier_string() const;
virtual uint32_t bgp_identifier() const;
std::string bgp_identifier_string() const;
const std::vector<std::string> &configured_families() const {
return configured_families_;
}
bool LookupFamily(Address::Family family) {
return (family_attributes_list_[family] != NULL);
}
bool IsFamilyNegotiated(Address::Family family);
RoutingInstance *GetRoutingInstance() { return rtinstance_; }
RoutingInstance *GetRoutingInstance() const { return rtinstance_; }
int GetIndex() const { return index_; }
int GetTaskInstance() const;
virtual BgpProto::BgpPeerType PeerType() const {
return peer_type_;
}
const BgpNeighborConfig *config() const { return config_; }
virtual void SetDataCollectionKey(BgpPeerInfo *peer_info) const;
void FillNeighborInfo(const BgpSandeshContext *bsc, BgpNeighborResp *bnr,
bool summary) const;
// thread-safe
bool IsDeleted() const;
bool IsAdminDown() const { return admin_down_; }
bool IsPassive() const { return passive_; }
bool IsCloseInProgress() const;
virtual bool IsReady() const;
virtual bool IsXmppPeer() const;
virtual bool CanUseMembershipManager() const;
virtual bool IsRegistrationRequired() const { return true; }
virtual uint64_t GetElapsedTimeSinceLastStateChange() const;
void Close(bool non_graceful);
void Clear(int subcode);
virtual IPeerClose *peer_close();
virtual IPeerDebugStats *peer_stats();
virtual const IPeerDebugStats *peer_stats() const;
void ManagedDelete();
void RetryDelete();
LifetimeActor *deleter();
void Initialize();
void NotifyEstablished(bool established);
void increment_flap_count();
void reset_flap_count();
uint64_t flap_count() const { return flap_count_; }
uint64_t last_flap() const { return last_flap_; }
uint64_t total_flap_count() const { return total_flap_count_; }
std::string last_flap_at() const;
void inc_rx_open();
void inc_rx_keepalive();
void inc_rx_update();
void inc_rx_notification();
void inc_tx_open();
void inc_tx_keepalive();
void inc_tx_update();
void inc_tx_notification();
void inc_rx_end_of_rib();
void inc_rx_route_reach(uint64_t count);
void inc_rx_route_unreach(uint64_t count);
void inc_tx_end_of_rib();
uint64_t get_rx_keepalive() const;
uint64_t get_rx_update() const;
uint64_t get_rx_notification() const;
uint64_t get_tx_keepalive() const;
uint64_t get_tx_update() const;
uint64_t get_rx_end_of_rib() const;
uint64_t get_rx_route_reach() const;
uint64_t get_rx_route_unreach() const;
uint64_t get_rx_route_total() const;
uint64_t get_tx_end_of_rib() const;
uint64_t get_tx_route_reach() const;
uint64_t get_tx_route_unreach() const;
uint64_t get_tx_route_total() const;
void inc_connect_error();
void inc_connect_timer_expired();
void inc_hold_timer_expired();
void inc_open_error();
void inc_update_error();
uint64_t get_connect_error() const;
uint64_t get_connect_timer_expired() const;
uint64_t get_hold_timer_expired() const;
uint64_t get_open_error() const;
uint64_t get_update_error() const;
uint64_t get_socket_reads() const;
uint64_t get_socket_writes() const;
static void FillBgpNeighborDebugState(BgpNeighborResp *bnr,
const IPeerDebugStats *peer);
bool ResumeClose();
void MembershipRequestCallback(BgpTable *table);
virtual void UpdateTotalPathCount(int count) const {
total_path_count_ += count;
}
virtual int GetTotalPathCount() const { return total_path_count_; }
virtual void UpdatePrimaryPathCount(int count) const {
primary_path_count_ += count;
}
virtual int GetPrimaryPathCount() const { return primary_path_count_; }
void RegisterToVpnTables();
StateMachine *state_machine() { return state_machine_.get(); }
const StateMachine *state_machine() const { return state_machine_.get(); }
bool GetBestAuthKeyItem(AuthenticationKey *auth_key);
bool InstallAuthKeys();
std::string GetInuseAuthKeyValue() const;
void SetListenSocketAuthKey(const AuthenticationKey &auth_key,
KeyType key_type);
void ClearListenSocketAuthKey();
void SetSessionSocketAuthKey(TcpSession *session);
void AddGRCapabilities(BgpProto::OpenMessage::OptParam *opt_param);
bool SetGRCapabilities(BgpPeerInfoData *peer_info);
void AddLLGRCapabilities(BgpProto::OpenMessage::OptParam *opt_param);
const BgpProto::OpenMessage::Capability::GR &gr_params() const {
return gr_params_;
}
BgpProto::OpenMessage::Capability::GR &gr_params() { return gr_params_; }
BgpProto::OpenMessage::Capability::LLGR &llgr_params() {
return llgr_params_;
}
const BgpProto::OpenMessage::Capability::LLGR &llgr_params() const {
return llgr_params_;
}
bool SkipNotificationSend(int code, int subcode) const;
virtual bool SkipNotificationReceive(int code, int subcode) const;
void Register(BgpTable *table, const RibExportPolicy &policy);
void Register(BgpTable *table);
bool EndOfRibSendTimerExpired(Address::Family family);
protected:
const std::vector<std::string> &negotiated_families() const {
return negotiated_families_;
}
const std::vector<std::string> &graceful_restart_families() const {
return graceful_restart_families_;
}
std::vector<std::string> &graceful_restart_families() {
return graceful_restart_families_;
}
const std::vector<std::string> &
long_lived_graceful_restart_families() const {
return long_lived_graceful_restart_families_;
}
std::vector<std::string> &long_lived_graceful_restart_families() {
return long_lived_graceful_restart_families_;
}
virtual void SendEndOfRIBActual(Address::Family family);
virtual void SendEndOfRIB(Address::Family family);
int membership_req_pending() const { return membership_req_pending_; }
private:
friend class BgpConfigTest;
friend class BgpPeerTest;
friend class BgpServerUnitTest;
friend class StateMachineUnitTest;
static const size_t kBufferSize = 32768;
class DeleteActor;
class PeerClose;
class PeerStats;
typedef std::map<Address::Family, const uint8_t *> FamilyToCapabilityMap;
typedef std::vector<BgpPeerFamilyAttributes *> FamilyAttributesList;
void KeepaliveTimerErrorHandler(std::string error_name,
std::string error_message);
virtual void StartKeepaliveTimerUnlocked();
void StopKeepaliveTimerUnlocked();
bool KeepaliveTimerExpired();
RibExportPolicy BuildRibExportPolicy(Address::Family family) const;
void ReceiveEndOfRIB(Address::Family family, size_t msgsize);
void StartEndOfRibTimer();
bool EndOfRibTimerExpired();
void EndOfRibTimerErrorHandler(std::string error_name,
std::string error_message);
virtual void BindLocalEndpoint(BgpSession *session);
void UnregisterAllTables();
void BGPPeerInfoSend(const BgpPeerInfoData &peer_info) const;
virtual bool MembershipPathCallback(DBTablePartBase *tpart,
BgpRoute *route, BgpPath *path);
uint32_t GetPathFlags(Address::Family family, const BgpAttr *attr) const;
virtual bool MpNlriAllowed(uint16_t afi, uint8_t safi);
BgpAttrPtr GetMpNlriNexthop(BgpMpNlri *nlri, BgpAttrPtr attr);
template <typename TableT, typename PrefixT>
void ProcessNlri(Address::Family family, DBRequest::DBOperation oper,
const BgpMpNlri *nlri, BgpAttrPtr attr, uint32_t flags);
bool GetBestAuthKey(AuthenticationKey *auth_key, KeyType *key_type) const;
bool ProcessAuthKeyChainConfig(const BgpNeighborConfig *config);
void LogInstallAuthKeys(const std::string &socket_name,
const std::string &oper, const AuthenticationKey &auth_key,
KeyType key_type);
void SetInuseAuthKeyInfo(const AuthenticationKey &key, KeyType type);
void ResetInuseAuthKeyInfo();
bool ProcessFamilyAttributesConfig(const BgpNeighborConfig *config);
void ProcessEndpointConfig(const BgpNeighborConfig *config);
void PostCloseRelease();
void CustomClose();
void FillBgpNeighborFamilyAttributes(BgpNeighborResp *nbr) const;
void FillCloseInfo(BgpNeighborResp *resp) const;
std::string BytesToHexString(const u_int8_t *msg, size_t size);
virtual uint32_t GetOutputQueueDepth(Address::Family family) const;
static const std::vector<Address::Family> supported_families_;
BgpServer *server_;
RoutingInstance *rtinstance_;
TcpSession::Endpoint endpoint_;
BgpPeerKey peer_key_;
uint16_t peer_port_;
std::string peer_name_;
std::string peer_basename_;
std::string router_type_; // bgp_schema.xsd:BgpRouterType
mutable std::string to_str_;
mutable std::string uve_key_str_;
const BgpNeighborConfig *config_;
// Global peer index
int index_;
TaskTrigger trigger_;
// The mutex is used to protect the session, keepalive timer and the
// send ready state.
//
// The session is accessed from the bgp::Send task and the keepalive
// timer handler and gets set/cleared from the bgp::StateMachine task.
//
// The keepalive timer can get started from the io thread (either via
// the SetSendReady callback or from the timer handler) or from the
// bgp::Send task or the bgp::StateMachine task. It can get stopped
// from the bgp::Send task or the bgp::StateMachine task.
//
// The send ready state gets modified from the bgp::Send task or from
// the io thread (either via the the SetSendReady callback or from the
// timer handler).
//
// Note that the mutex will not be heavily contended since we expect
// the bgp::Send task to lock it most frequently while all other tasks
// and the io thread should need to lock it once every few seconds at
// most. Hence we choose a spin_mutex.
tbb::spin_mutex spin_mutex_;
uint8_t buffer_[kBufferSize];
size_t buffer_len_;
BgpSession *session_;
Timer *keepalive_timer_;
Timer *end_of_rib_timer_;
Timer *end_of_rib_send_timer_[Address::NUM_FAMILIES];
bool send_ready_;
bool admin_down_;
bool passive_;
bool resolve_paths_;
bool as_override_;
tbb::atomic<int> membership_req_pending_;
bool defer_close_;
bool non_graceful_close_;
bool vpn_tables_registered_;
std::vector<BgpProto::OpenMessage::Capability *> capabilities_;
uint16_t hold_time_;
as_t local_as_;
as_t peer_as_;
uint32_t local_bgp_id_; // network order
uint32_t peer_bgp_id_; // network order
FamilyAttributesList family_attributes_list_;
std::vector<std::string> configured_families_;
std::vector<std::string> negotiated_families_;
std::vector<std::string> graceful_restart_families_;
std::vector<std::string> long_lived_graceful_restart_families_;
BgpProto::BgpPeerType peer_type_;
boost::scoped_ptr<StateMachine> state_machine_;
boost::scoped_ptr<PeerClose> peer_close_;
boost::scoped_ptr<PeerStats> peer_stats_;
boost::scoped_ptr<DeleteActor> deleter_;
LifetimeRef<BgpPeer> instance_delete_ref_;
mutable tbb::atomic<int> total_path_count_;
mutable tbb::atomic<int> primary_path_count_;
uint64_t flap_count_;
uint64_t total_flap_count_;
uint64_t last_flap_;
AuthenticationData auth_data_;
AuthenticationKey inuse_auth_key_;
KeyType inuse_authkey_type_;
BgpProto::OpenMessage::Capability::GR gr_params_;
BgpProto::OpenMessage::Capability::LLGR llgr_params_;
DISALLOW_COPY_AND_ASSIGN(BgpPeer);
};
#endif // SRC_BGP_BGP_PEER_H__