-
Notifications
You must be signed in to change notification settings - Fork 390
/
bgp_peer.h
353 lines (281 loc) · 10.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
/*
* Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
*/
#ifndef __BGP_PEER_H__
#define __BGP_PEER_H__
#include <set>
#include <memory>
#include <boost/asio/ip/tcp.hpp>
#include <boost/scoped_ptr.hpp>
#include <tbb/spin_mutex.h>
#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_ribout.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 BgpServer;
class BgpSession;
class RoutingInstance;
class StateMachine;
class BgpSession;
class BgpPeerInfo;
class BgpNeighborResp;
class BgpSandeshContext;
// A BGP peer along with its session and state machine.
class BgpPeer : public IPeer {
public:
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);
// 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();
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();
void StopKeepaliveTimer();
bool KeepaliveTimerRunning();
void SetSendReady();
// thread: io::ReaderTask
void SetCapabilities(const BgpProto::OpenMessage *msg);
void ResetCapabilities();
// Table registration.
void RegisterAllTables();
// accessors
virtual BgpServer *server() { return server_; }
const BgpServer *server() const { return server_; }
unsigned long 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_; }
const std::string &peer_name() const { return peer_name_; }
const std::string &peer_basename() const { return peer_basename_; }
StateMachine::State GetState() const;
virtual const std::string GetStateName() const;
void set_session(BgpSession *session);
void clear_session();
BgpSession *session();
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 bgp_identifier() const;
const AddressFamilyList &families() const {
return family_;
}
void AddFamily(Address::Family family) {
family_.insert(family);
}
bool LookupFamily(Address::Family family) {
return (family_.find(family) != family_.end());
}
bool IsFamilyNegotiated(Address::Family family);
RoutingInstance *GetRoutingInstance() {
return rtinstance_;
}
int GetIndex() const {
return index_;
}
virtual BgpProto::BgpPeerType PeerType() const {
return peer_type_;
}
const BgpNeighborConfig *config() const { return config_; }
virtual void SetDataCollectionKey(BgpPeerInfo *peer_info) const;
void FillNeighborInfo(BgpSandeshContext *bsc,
std::vector<BgpNeighborResp> *nbr_list, bool summary) const;
// thread-safe
bool IsDeleted() const;
bool IsAdminDown() const { return admin_down_; }
bool IsCloseInProgress() const;
virtual bool IsReady() const;
virtual bool IsXmppPeer() const;
void Close();
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 increment_flap_count();
void reset_flap_count();
uint64_t flap_count() const { return 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;
static void FillBgpNeighborDebugState(BgpNeighborResp &resp, const IPeerDebugStats *peer);
bool ResumeClose();
void MembershipRequestCallback(IPeer *ipeer, BgpTable *table);
virtual void UpdateRefCount(int count) const { refcount_ += count; }
virtual tbb::atomic<int> GetRefCount() const { return refcount_; }
void RegisterToVpnTables();
StateMachine *state_machine() { return state_machine_.get(); }
const StateMachine *state_machine() const { return state_machine_.get(); }
bool GetBestAuthKeyItem(AuthenticationKey *auth_key);
void InstallAuthKeys(TcpSession *session);
std::string GetInuseAuthKeyValue() const;
void SetListenSocketAuthKey(const AuthenticationKey &auth_key);
void ClearListenSocketAuthKey();
private:
friend class BgpConfigTest;
friend class BgpPeerTest;
friend class BgpServerUnitTest;
friend class StateMachineUnitTest;
class DeleteActor;
class PeerClose;
class PeerStats;
void KeepaliveTimerErrorHandler(std::string error_name,
std::string error_message);
virtual void StartKeepaliveTimerUnlocked();
void StopKeepaliveTimerUnlocked();
bool KeepaliveTimerExpired();
void ReceiveEndOfRIB(Address::Family family, size_t msgsize);
void SendEndOfRIB(Address::Family family);
void StartEndOfRibTimer();
bool EndOfRibTimerExpired();
void EndOfRibTimerErrorHandler(std::string error_name,
std::string error_message);
virtual void BindLocalEndpoint(BgpSession *session);
void UnregisterAllTables();
virtual bool MpNlriAllowed(uint16_t afi, uint8_t safi);
BgpAttrPtr GetMpNlriNexthop(BgpMpNlri *nlri, BgpAttrPtr attr);
bool GetBestAuthKey(AuthenticationKey *auth_key, KeyType *key_type) const;
void ProcessAuthKeyChainConfig(const BgpNeighborConfig *config);
void LogInstallAuthKeys(const std::string &oper,
const AuthenticationKey &auth_key, KeyType key_type);
void SetInuseAuthKeyInfo(const AuthenticationKey &key, KeyType type);
void ResetInuseAuthKeyInfo();
void PostCloseRelease();
void CustomClose();
std::string BytesToHexString(const u_int8_t *msg, size_t size);
BgpServer *server_;
// Backpointer to routing instance
RoutingInstance *rtinstance_;
BgpPeerKey peer_key_;
std::string peer_name_;
std::string peer_basename_;
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_;
BgpSession *session_;
Timer *keepalive_timer_;
Timer *end_of_rib_timer_;
bool send_ready_;
bool admin_down_;
boost::scoped_ptr<StateMachine> state_machine_;
uint64_t membership_req_pending_;
bool defer_close_;
bool vpn_tables_registered_;
std::vector<BgpProto::OpenMessage::Capability *> capabilities_;
as_t local_as_;
as_t peer_as_;
uint32_t remote_bgp_id_; // network order
uint32_t local_bgp_id_; // network order
AddressFamilyList family_;
std::vector<std::string> configured_families_;
std::vector<std::string> negotiated_families_;
BgpProto::BgpPeerType peer_type_;
RibExportPolicy policy_;
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> refcount_;
uint64_t flap_count_;
uint64_t last_flap_;
AuthenticationData auth_data_;
AuthenticationKey inuse_auth_key_;
KeyType inuse_authkey_type_;
DISALLOW_COPY_AND_ASSIGN(BgpPeer);
};
#endif