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
|
// SPDX-License-Identifier: LGPL-2.1-or-later
/*
* This file is part of the PCEPlib, a PCEP protocol library.
*
* Copyright (C) 2020 Volta Networks https://voltanet.io/
*
* Author : Brady Johnson <brady@voltanet.io>
*
*/
/*
* Public PCEPlib PCC API implementation
*/
#include <zebra.h>
#include <pthread.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include "pcep_msg_messages.h"
#include "pcep_pcc_api.h"
#include "pcep_utils_counters.h"
#include "pcep_utils_logging.h"
/* Not using an array here since the enum pcep_event_type indeces go into the
* 100's */
const char MESSAGE_RECEIVED_STR[] = "MESSAGE_RECEIVED";
const char PCE_CLOSED_SOCKET_STR[] = "PCE_CLOSED_SOCKET";
const char PCE_SENT_PCEP_CLOSE_STR[] = "PCE_SENT_PCEP_CLOSE";
const char PCE_DEAD_TIMER_EXPIRED_STR[] = "PCE_DEAD_TIMER_EXPIRED";
const char PCE_OPEN_KEEP_WAIT_TIMER_EXPIRED_STR[] =
"PCE_OPEN_KEEP_WAIT_TIMER_EXPIRED";
const char PCC_CONNECTED_TO_PCE_STR[] = "PCC_CONNECTED_TO_PCE";
const char PCC_PCEP_SESSION_CLOSED_STR[] = "PCC_PCEP_SESSION_CLOSED";
const char PCC_RCVD_INVALID_OPEN_STR[] = "PCC_RCVD_INVALID_OPEN";
const char PCC_RCVD_MAX_INVALID_MSGS_STR[] = "PCC_RCVD_MAX_INVALID_MSGS";
const char PCC_RCVD_MAX_UNKOWN_MSGS_STR[] = "PCC_RCVD_MAX_UNKOWN_MSGS";
const char UNKNOWN_EVENT_STR[] = "UNKNOWN Event Type";
/* Session Logic Handle managed in pcep_session_logic.c */
extern pcep_event_queue *session_logic_event_queue_;
bool initialize_pcc(void)
{
if (!run_session_logic()) {
pcep_log(LOG_ERR, "%s: Error initializing PCC session logic.",
__func__);
return false;
}
return true;
}
bool initialize_pcc_infra(struct pceplib_infra_config *infra_config)
{
if (infra_config == NULL) {
return initialize_pcc();
}
if (!run_session_logic_with_infra(infra_config)) {
pcep_log(LOG_ERR,
"%s: Error initializing PCC session logic with infra.",
__func__);
return false;
}
return true;
}
/* this function is blocking */
bool initialize_pcc_wait_for_completion(void)
{
return run_session_logic_wait_for_completion();
}
bool destroy_pcc(void)
{
if (!stop_session_logic()) {
pcep_log(LOG_WARNING, "%s: Error stopping PCC session logic.",
__func__);
return false;
}
return true;
}
pcep_configuration *create_default_pcep_configuration(void)
{
pcep_configuration *config =
pceplib_malloc(PCEPLIB_INFRA, sizeof(pcep_configuration));
memset(config, 0, sizeof(pcep_configuration));
config->keep_alive_seconds = DEFAULT_CONFIG_KEEP_ALIVE;
/* This value will possibly be overwritten later with PCE config data */
config->keep_alive_pce_negotiated_timer_seconds =
DEFAULT_CONFIG_KEEP_ALIVE;
config->min_keep_alive_seconds = DEFAULT_MIN_CONFIG_KEEP_ALIVE;
config->max_keep_alive_seconds = DEFAULT_MAX_CONFIG_KEEP_ALIVE;
config->dead_timer_seconds = DEFAULT_CONFIG_DEAD_TIMER;
/* This value will be overwritten later with PCE config data */
config->dead_timer_pce_negotiated_seconds = DEFAULT_CONFIG_DEAD_TIMER;
config->min_dead_timer_seconds = DEFAULT_MIN_CONFIG_DEAD_TIMER;
config->max_dead_timer_seconds = DEFAULT_MAX_CONFIG_DEAD_TIMER;
config->request_time_seconds = DEFAULT_CONFIG_REQUEST_TIME;
config->max_unknown_messages = DEFAULT_CONFIG_MAX_UNKNOWN_MESSAGES;
config->max_unknown_requests = DEFAULT_CONFIG_MAX_UNKNOWN_REQUESTS;
config->socket_connect_timeout_millis =
DEFAULT_TCP_CONNECT_TIMEOUT_MILLIS;
config->support_stateful_pce_lsp_update = true;
config->support_pce_lsp_instantiation = true;
config->support_include_db_version = true;
config->lsp_db_version = 0;
config->support_lsp_triggered_resync = true;
config->support_lsp_delta_sync = true;
config->support_pce_triggered_initial_sync = true;
config->support_sr_te_pst = true;
config->pcc_can_resolve_nai_to_sid = true;
config->max_sid_depth = 0;
config->dst_pcep_port = 0;
config->src_pcep_port = 0;
config->src_ip.src_ipv4.s_addr = INADDR_ANY;
config->is_src_ipv6 = false;
config->pcep_msg_versioning = create_default_pcep_versioning();
config->tcp_authentication_str[0] = '\0';
config->is_tcp_auth_md5 = true;
return config;
}
void destroy_pcep_configuration(pcep_configuration *config)
{
destroy_pcep_versioning(config->pcep_msg_versioning);
pceplib_free(PCEPLIB_INFRA, config);
}
pcep_session *connect_pce(pcep_configuration *config, struct in_addr *pce_ip)
{
return create_pcep_session(config, pce_ip);
}
pcep_session *connect_pce_ipv6(pcep_configuration *config,
struct in6_addr *pce_ip)
{
return create_pcep_session_ipv6(config, pce_ip);
}
void disconnect_pce(pcep_session *session)
{
if (session_exists(session) == false) {
pcep_log(
LOG_WARNING,
"%s: disconnect_pce session [%p] has already been deleted",
__func__, session);
return;
}
if (session->socket_comm_session == NULL
|| session->socket_comm_session->socket_fd < 0) {
/* If the socket has already been closed, just destroy the
* session */
destroy_pcep_session(session);
} else {
/* This will cause the session to be destroyed AFTER the close
* message is sent */
session->destroy_session_after_write = true;
/* Send a PCEP close message */
close_pcep_session(session);
}
}
void send_message(pcep_session *session, struct pcep_message *msg,
bool free_after_send)
{
if (session == NULL || msg == NULL) {
pcep_log(LOG_DEBUG,
"%s: send_message NULL params session [%p] msg [%p]",
__func__, session, msg);
return;
}
if (session_exists(session) == false) {
pcep_log(
LOG_WARNING,
"%s: send_message session [%p] has already been deleted",
__func__, session);
return;
}
pcep_encode_message(msg, session->pcc_config.pcep_msg_versioning);
socket_comm_session_send_message(
session->socket_comm_session, (char *)msg->encoded_message,
msg->encoded_message_length, free_after_send);
increment_message_tx_counters(session, msg);
if (free_after_send == true) {
/* The encoded_message will be deleted once sent, so everything
* else in the message will be freed */
msg->encoded_message = NULL;
pcep_msg_free_message(msg);
}
}
/* Returns true if the queue is empty, false otherwise */
bool event_queue_is_empty(void)
{
if (session_logic_event_queue_ == NULL) {
pcep_log(
LOG_WARNING,
"%s: event_queue_is_empty Session Logic is not initialized yet",
__func__);
return false;
}
pthread_mutex_lock(&session_logic_event_queue_->event_queue_mutex);
bool is_empty =
(session_logic_event_queue_->event_queue->num_entries == 0);
pthread_mutex_unlock(&session_logic_event_queue_->event_queue_mutex);
return is_empty;
}
/* Return the number of events on the queue, 0 if empty */
uint32_t event_queue_num_events_available(void)
{
if (session_logic_event_queue_ == NULL) {
pcep_log(
LOG_WARNING,
"%s: event_queue_num_events_available Session Logic is not initialized yet",
__func__);
return 0;
}
pthread_mutex_lock(&session_logic_event_queue_->event_queue_mutex);
uint32_t num_events =
session_logic_event_queue_->event_queue->num_entries;
pthread_mutex_unlock(&session_logic_event_queue_->event_queue_mutex);
return num_events;
}
/* Return the next event on the queue, NULL if empty */
struct pcep_event *event_queue_get_event(void)
{
if (session_logic_event_queue_ == NULL) {
pcep_log(
LOG_WARNING,
"%s: event_queue_get_event Session Logic is not initialized yet",
__func__);
return NULL;
}
pthread_mutex_lock(&session_logic_event_queue_->event_queue_mutex);
struct pcep_event *event = (struct pcep_event *)queue_dequeue(
session_logic_event_queue_->event_queue);
pthread_mutex_unlock(&session_logic_event_queue_->event_queue_mutex);
return event;
}
/* Free the PCEP Event resources, including the PCEP message */
void destroy_pcep_event(struct pcep_event *event)
{
if (event == NULL) {
pcep_log(LOG_WARNING,
"%s: destroy_pcep_event cannot destroy NULL event",
__func__);
return;
}
if (event->event_type == MESSAGE_RECEIVED && event->message != NULL) {
pcep_msg_free_message(event->message);
}
pceplib_free(PCEPLIB_INFRA, event);
}
const char *get_event_type_str(int event_type)
{
switch (event_type) {
case MESSAGE_RECEIVED:
return MESSAGE_RECEIVED_STR;
break;
case PCE_CLOSED_SOCKET:
return PCE_CLOSED_SOCKET_STR;
break;
case PCE_SENT_PCEP_CLOSE:
return PCE_SENT_PCEP_CLOSE_STR;
break;
case PCE_DEAD_TIMER_EXPIRED:
return PCE_DEAD_TIMER_EXPIRED_STR;
break;
case PCE_OPEN_KEEP_WAIT_TIMER_EXPIRED:
return PCE_OPEN_KEEP_WAIT_TIMER_EXPIRED_STR;
break;
case PCC_CONNECTED_TO_PCE:
return PCC_CONNECTED_TO_PCE_STR;
break;
case PCC_PCEP_SESSION_CLOSED:
return PCC_PCEP_SESSION_CLOSED_STR;
break;
case PCC_RCVD_INVALID_OPEN:
return PCC_RCVD_INVALID_OPEN_STR;
break;
case PCC_RCVD_MAX_INVALID_MSGS:
return PCC_RCVD_MAX_INVALID_MSGS_STR;
break;
case PCC_RCVD_MAX_UNKOWN_MSGS:
return PCC_RCVD_MAX_UNKOWN_MSGS_STR;
break;
default:
return UNKNOWN_EVENT_STR;
break;
}
}
void dump_pcep_session_counters(pcep_session *session)
{
if (session_exists(session) == false) {
pcep_log(
LOG_WARNING,
"%s: dump_pcep_session_counters session [%p] has already been deleted",
__func__, session);
return;
}
/* Update the counters group name so that the PCE session connected time
* is accurate */
time_t now = time(NULL);
char counters_name[MAX_COUNTER_STR_LENGTH] = {0};
char ip_str[40] = {0};
if (session->socket_comm_session->is_ipv6) {
inet_ntop(AF_INET6,
&session->socket_comm_session->dest_sock_addr
.dest_sock_addr_ipv6.sin6_addr,
ip_str, 40);
} else {
inet_ntop(AF_INET,
&session->socket_comm_session->dest_sock_addr
.dest_sock_addr_ipv4.sin_addr,
ip_str, 40);
}
snprintf(counters_name, MAX_COUNTER_STR_LENGTH,
"PCEP Session [%d], connected to [%s] for [%u seconds]",
session->session_id, ip_str,
(uint32_t)(now - session->time_connected));
strlcpy(session->pcep_session_counters->counters_group_name,
counters_name,
sizeof(session->pcep_session_counters->counters_group_name));
dump_counters_group_to_log(session->pcep_session_counters);
}
void reset_pcep_session_counters(pcep_session *session)
{
if (session_exists(session) == false) {
pcep_log(
LOG_WARNING,
"%s: reset_pcep_session_counters session [%p] has already been deleted",
session);
return;
}
reset_group_counters(session->pcep_session_counters);
}
|