/
bgp_session_manager.cc
224 lines (198 loc) · 7.04 KB
/
bgp_session_manager.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
/*
* Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
*/
#include "bgp/bgp_session_manager.h"
#include "base/task_annotations.h"
#include "bgp/bgp_log.h"
#include "bgp/bgp_session.h"
#include "bgp/routing-instance/peer_manager.h"
BgpSessionManager::BgpSessionManager(EventManager *evm, BgpServer *server)
: TcpServer(evm),
server_(server),
session_queue_(
TaskScheduler::GetInstance()->GetTaskId("bgp::Config"), 0,
boost::bind(&BgpSessionManager::ProcessSession, this, _1)),
write_ready_queue_(
TaskScheduler::GetInstance()->GetTaskId("bgp::Config"), 0,
boost::bind(&BgpSessionManager::ProcessWriteReady, this, _1)) {
}
BgpSessionManager::~BgpSessionManager() {
}
//
// Start listening at the given port.
//
bool BgpSessionManager::Initialize(short port) {
return TcpServer::Initialize(port);
}
//
// Called from BgpServer::DeleteActor's Shutdown method.
// Shutdown the TcpServer.
// Register an exit callback to the WorkQueues so that we can ask BgpServer
// to retry deletion when a WorkQueue becomes empty.
//
void BgpSessionManager::Shutdown() {
CHECK_CONCURRENCY("bgp::Config");
TcpServer::Shutdown();
session_queue_.SetExitCallback(
boost::bind(&BgpSessionManager::WorkQueueExitCallback, this, _1));
write_ready_queue_.SetExitCallback(
boost::bind(&BgpSessionManager::WorkQueueExitCallback, this, _1));
}
//
// Called when the BgpServer is being destroyed.
//
// The WorkQueues need to be shutdown as the last step to ensure that all
// entries get deleted. Note that there's no need to call DeleteSession on
// the sessions in the WorkQueues since ClearSessions does the same thing.
//
void BgpSessionManager::Terminate() {
CHECK_CONCURRENCY("bgp::Config");
server_ = NULL;
ClearSessions();
session_queue_.Shutdown();
write_ready_queue_.Shutdown();
}
//
// Return true if all WorkQueues are empty.
//
bool BgpSessionManager::MayDelete() const {
if (!session_queue_.IsQueueEmpty())
return false;
if (!write_ready_queue_.IsQueueEmpty())
return false;
return true;
}
//
// Add a BgpSession to the write ready WorkQueue.
// Take a reference to make sure that BgpSession doesn't get deleted before
// it's processed.
//
void BgpSessionManager::EnqueueWriteReady(BgpSession *session) {
if (!server_ || server_->IsDeleted())
return;
write_ready_queue_.Enqueue(TcpSessionPtr(session));
}
//
// Handler for BgpSessions that are dequeued from the write ready WorkQueue.
//
// The BgpServer does not get destroyed if the WorkQueue is non-empty.
//
bool BgpSessionManager::ProcessWriteReady(TcpSessionPtr tcp_session) {
BgpSession *session = static_cast<BgpSession *>(tcp_session.get());
session->ProcessWriteReady();
return true;
}
//
// Search for a matching BgpPeer.
// First look for an exact match in the EndpointToBgpPeerList in BgpServer.
// Then look for a matching address in the master instance.
//
BgpPeer *BgpSessionManager::FindPeer(Endpoint remote) {
BgpPeer *peer = server_->FindPeer(remote);
if (peer)
return peer;
const RoutingInstance *instance =
server_->routing_instance_mgr()->GetDefaultRoutingInstance();
return (instance ? instance->peer_manager()->PeerLookup(remote) : NULL);
}
//
// Create an active BgpSession.
//
TcpSession *BgpSessionManager::CreateSession() {
TcpSession *session = TcpServer::CreateSession();
Socket *socket = session->socket();
boost::system::error_code ec;
socket->open(boost::asio::ip::tcp::v4(), ec);
if (ec || (ec = session->SetSocketOptions()) || socket_open_failure()) {
BGP_LOG_STR(BgpMessage, SandeshLevel::SYS_WARN, BGP_LOG_FLAG_ALL,
"Failed to open bgp socket, error: " << ec.message());
DeleteSession(session);
return NULL;
}
return session;
}
//
// Allocate a new BgpSession.
// Called via CreateSession or when the TcpServer accepts a passive session.
//
TcpSession *BgpSessionManager::AllocSession(Socket *socket) {
TcpSession *session = new BgpSession(this, socket);
return session;
}
//
// Accept incoming BgpSession and add to session WorkQueue for processing.
// This ensures that we don't try to access the BgpServer data structures
// from the IO thread while they are being modified from bgp::Config task.
//
// Stop accepting sessions after delete of the BgpServer gets triggered.
// Note that the BgpServer, and hence the BgpSessionManager will not get
// destroyed if the WorkQueue is non-empty.
//
bool BgpSessionManager::AcceptSession(TcpSession *tcp_session) {
if (!server_ || server_->IsDeleted())
return false;
BgpSession *session = dynamic_cast<BgpSession *>(tcp_session);
session->set_read_on_connect(false);
session_queue_.Enqueue(session);
return true;
}
//
// Handler for BgpSessions that are dequeued from the session WorkQueue.
//
// The BgpServer does not get destroyed if the WorkQueue is non-empty.
//
bool BgpSessionManager::ProcessSession(BgpSession *session) {
CHECK_CONCURRENCY("bgp::Config");
BgpPeer *peer = FindPeer(session->remote_endpoint());
// Ignore if server is being deleted.
if (!server_ || server_->IsDeleted()) {
session->SendNotification(BgpProto::Notification::Cease,
BgpProto::Notification::PeerDeconfigured);
DeleteSession(session);
return true;
}
// Ignore if this server is being held administratively down.
if (server_->admin_down()) {
session->SendNotification(BgpProto::Notification::Cease,
BgpProto::Notification::AdminShutdown);
DeleteSession(session);
return true;
}
// Ignore if this peer is not configured or is being deleted.
if (peer == NULL || peer->deleter()->IsDeleted()) {
session->SendNotification(BgpProto::Notification::Cease,
BgpProto::Notification::PeerDeconfigured);
BGP_LOG_STR(BgpMessage, SandeshLevel::SYS_WARN,
BGP_LOG_FLAG_TRACE, "Remote end-point not found");
DeleteSession(session);
return true;
}
// Ignore if this peer is being held administratively down.
if (peer->IsAdminDown()) {
session->SendNotification(BgpProto::Notification::Cease,
BgpProto::Notification::AdminShutdown);
DeleteSession(session);
return true;
}
// Ignore if this peer is being closed.
if (peer->IsCloseInProgress()) {
session->SendNotification(BgpProto::Notification::Cease,
BgpProto::Notification::ConnectionRejected);
DeleteSession(session);
return true;
}
peer->AcceptSession(session);
return true;
}
//
// Exit callback for the session and write ready WorkQueues.
//
void BgpSessionManager::WorkQueueExitCallback(bool done) {
server_->RetryDelete();
}
size_t BgpSessionManager::GetSessionQueueSize() const {
return session_queue_.Length();
}
void BgpSessionManager::SetSessionQueueDisable(bool disabled) {
session_queue_.set_disable(disabled);
}