2 * This file is part of the PCEPlib, a PCEP protocol library.
4 * Copyright (C) 2020 Volta Networks https://voltanet.io/
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 * Author : Brady Johnson <brady@voltanet.io>
32 #include "pcep_msg_encoding.h"
33 #include "pcep_session_logic.h"
34 #include "pcep_session_logic_internals.h"
35 #include "pcep_timers.h"
36 #include "pcep_utils_counters.h"
37 #include "pcep_utils_ordered_list.h"
38 #include "pcep_utils_logging.h"
39 #include "pcep_utils_memory.h"
42 * public API function implementations for the session_logic
45 pcep_session_logic_handle
*session_logic_handle_
= NULL
;
46 pcep_event_queue
*session_logic_event_queue_
= NULL
;
49 void send_pcep_open(pcep_session
*session
); /* forward decl */
51 static bool run_session_logic_common()
53 if (session_logic_handle_
!= NULL
) {
55 "%s: Session Logic is already initialized.", __func__
);
59 session_logic_handle_
= pceplib_malloc(
60 PCEPLIB_INFRA
, sizeof(pcep_session_logic_handle
));
61 memset(session_logic_handle_
, 0, sizeof(pcep_session_logic_handle
));
63 session_logic_handle_
->active
= true;
64 session_logic_handle_
->session_logic_condition
= false;
65 session_logic_handle_
->session_list
=
66 ordered_list_initialize(pointer_compare_function
);
67 session_logic_handle_
->session_event_queue
= queue_initialize();
69 /* Initialize the event queue */
70 session_logic_event_queue_
=
71 pceplib_malloc(PCEPLIB_INFRA
, sizeof(pcep_event_queue
));
72 session_logic_event_queue_
->event_queue
= queue_initialize();
73 if (pthread_mutex_init(&(session_logic_event_queue_
->event_queue_mutex
),
78 "%s: Cannot initialize session_logic event queue mutex.",
83 pthread_cond_init(&(session_logic_handle_
->session_logic_cond_var
),
86 if (pthread_mutex_init(&(session_logic_handle_
->session_logic_mutex
),
89 pcep_log(LOG_ERR
, "%s: Cannot initialize session_logic mutex.",
94 if (pthread_mutex_init(&(session_logic_handle_
->session_list_mutex
),
97 pcep_log(LOG_ERR
, "%s: Cannot initialize session_list mutex.",
106 bool run_session_logic()
108 if (!run_session_logic_common()) {
112 if (pthread_create(&(session_logic_handle_
->session_logic_thread
), NULL
,
113 session_logic_loop
, session_logic_handle_
)) {
114 pcep_log(LOG_ERR
, "%s: Cannot initialize session_logic thread.",
119 if (!initialize_timers(session_logic_timer_expire_handler
)) {
120 pcep_log(LOG_ERR
, "%s: Cannot initialize session_logic timers.",
125 /* No need to call initialize_socket_comm_loop() since it will be
126 * called internally when the first socket_comm_session is created. */
132 bool run_session_logic_with_infra(pceplib_infra_config
*infra_config
)
134 if (infra_config
== NULL
) {
135 return run_session_logic();
138 /* Initialize the memory infrastructure before anything gets allocated
140 if (infra_config
->pceplib_infra_mt
!= NULL
141 && infra_config
->pceplib_messages_mt
!= NULL
) {
142 pceplib_memory_initialize(
143 infra_config
->pceplib_infra_mt
,
144 infra_config
->pceplib_messages_mt
,
145 infra_config
->malloc_func
, infra_config
->calloc_func
,
146 infra_config
->realloc_func
, infra_config
->strdup_func
,
147 infra_config
->free_func
);
150 if (!run_session_logic_common()) {
154 /* Create the pcep_session_logic pthread so it can be managed externally
156 if (infra_config
->pthread_create_func
!= NULL
) {
157 if (infra_config
->pthread_create_func(
158 &(session_logic_handle_
->session_logic_thread
),
159 NULL
, session_logic_loop
, session_logic_handle_
,
160 "pcep_session_logic")) {
163 "%s: Cannot initialize external session_logic thread.",
169 &(session_logic_handle_
->session_logic_thread
),
170 NULL
, session_logic_loop
, session_logic_handle_
)) {
172 "%s: Cannot initialize session_logic thread.",
178 session_logic_event_queue_
->event_callback
=
179 infra_config
->pcep_event_func
;
180 session_logic_event_queue_
->event_callback_data
=
181 infra_config
->external_infra_data
;
183 if (!initialize_timers_external_infra(
184 session_logic_timer_expire_handler
,
185 infra_config
->external_infra_data
,
186 infra_config
->timer_create_func
,
187 infra_config
->timer_cancel_func
,
188 infra_config
->pthread_create_func
)) {
191 "%s: Cannot initialize session_logic timers with infra.",
196 /* We found a problem with the FRR sockets, where not all the KeepAlive
197 * messages were received, so if the pthread_create_func is set, the
198 * internal PCEPlib socket infrastructure will be used. */
200 /* For the SocketComm, the socket_read/write_func and the
201 * pthread_create_func are mutually exclusive. */
202 if (infra_config
->pthread_create_func
!= NULL
) {
203 if (!initialize_socket_comm_external_infra(
204 infra_config
->external_infra_data
, NULL
, NULL
,
205 infra_config
->pthread_create_func
)) {
208 "%s: Cannot initialize session_logic socket comm with infra.",
212 } else if (infra_config
->socket_read_func
!= NULL
213 && infra_config
->socket_write_func
!= NULL
) {
214 if (!initialize_socket_comm_external_infra(
215 infra_config
->external_infra_data
,
216 infra_config
->socket_read_func
,
217 infra_config
->socket_write_func
, NULL
)) {
220 "%s: Cannot initialize session_logic socket comm with infra.",
229 bool run_session_logic_wait_for_completion()
231 if (!run_session_logic()) {
235 /* Blocking call, waits for session logic thread to complete */
236 pthread_join(session_logic_handle_
->session_logic_thread
, NULL
);
242 bool stop_session_logic()
244 if (session_logic_handle_
== NULL
) {
245 pcep_log(LOG_WARNING
, "%s: Session logic already stopped",
250 session_logic_handle_
->active
= false;
253 pthread_mutex_lock(&(session_logic_handle_
->session_logic_mutex
));
254 session_logic_handle_
->session_logic_condition
= true;
255 pthread_cond_signal(&(session_logic_handle_
->session_logic_cond_var
));
256 pthread_mutex_unlock(&(session_logic_handle_
->session_logic_mutex
));
257 pthread_join(session_logic_handle_
->session_logic_thread
, NULL
);
259 pthread_mutex_destroy(&(session_logic_handle_
->session_logic_mutex
));
260 pthread_mutex_destroy(&(session_logic_handle_
->session_list_mutex
));
261 ordered_list_destroy(session_logic_handle_
->session_list
);
262 queue_destroy(session_logic_handle_
->session_event_queue
);
264 /* destroy the event_queue */
265 pthread_mutex_destroy(&(session_logic_event_queue_
->event_queue_mutex
));
266 queue_destroy(session_logic_event_queue_
->event_queue
);
267 pceplib_free(PCEPLIB_INFRA
, session_logic_event_queue_
);
269 /* Explicitly stop the socket comm loop started by the pcep_sessions */
270 destroy_socket_comm_loop();
272 pceplib_free(PCEPLIB_INFRA
, session_logic_handle_
);
273 session_logic_handle_
= NULL
;
279 void close_pcep_session(pcep_session
*session
)
281 close_pcep_session_with_reason(session
, PCEP_CLOSE_REASON_NO
);
284 void close_pcep_session_with_reason(pcep_session
*session
,
285 enum pcep_close_reason reason
)
287 struct pcep_message
*close_msg
= pcep_msg_create_close(reason
);
291 "%s: [%ld-%ld] pcep_session_logic send pcep_close message for session [%d]",
292 __func__
, time(NULL
), pthread_self(), session
->session_id
);
294 session_send_message(session
, close_msg
);
295 socket_comm_session_close_tcp_after_write(session
->socket_comm_session
);
296 session
->session_state
= SESSION_STATE_INITIALIZED
;
300 void destroy_pcep_session(pcep_session
*session
)
302 if (session
== NULL
) {
303 pcep_log(LOG_WARNING
, "%s: Cannot destroy NULL session",
308 /* Remove the session from the session_list and synchronize session
309 * destroy with the session_logic_loop, so that no in-flight events
310 * will be handled now that the session is destroyed. */
311 pthread_mutex_lock(&(session_logic_handle_
->session_list_mutex
));
312 ordered_list_remove_first_node_equals(
313 session_logic_handle_
->session_list
, session
);
315 "%s: destroy_pcep_session delete session_list sessionPtr %p",
318 pcep_session_cancel_timers(session
);
319 delete_counters_group(session
->pcep_session_counters
);
320 queue_destroy_with_data(session
->num_unknown_messages_time_queue
);
321 socket_comm_session_teardown(session
->socket_comm_session
);
323 if (session
->pcc_config
.pcep_msg_versioning
!= NULL
) {
324 pceplib_free(PCEPLIB_INFRA
,
325 session
->pcc_config
.pcep_msg_versioning
);
328 if (session
->pce_config
.pcep_msg_versioning
!= NULL
) {
329 pceplib_free(PCEPLIB_INFRA
,
330 session
->pce_config
.pcep_msg_versioning
);
333 int session_id
= session
->session_id
;
334 pceplib_free(PCEPLIB_INFRA
, session
);
335 pcep_log(LOG_INFO
, "%s: [%ld-%ld] session [%d] destroyed", __func__
,
336 time(NULL
), pthread_self(), session_id
);
337 pthread_mutex_unlock(&(session_logic_handle_
->session_list_mutex
));
340 void pcep_session_cancel_timers(pcep_session
*session
)
342 if (session
== NULL
) {
346 if (session
->timer_id_dead_timer
!= TIMER_ID_NOT_SET
) {
347 cancel_timer(session
->timer_id_dead_timer
);
350 if (session
->timer_id_keep_alive
!= TIMER_ID_NOT_SET
) {
351 cancel_timer(session
->timer_id_keep_alive
);
354 if (session
->timer_id_open_keep_wait
!= TIMER_ID_NOT_SET
) {
355 cancel_timer(session
->timer_id_open_keep_wait
);
358 if (session
->timer_id_open_keep_alive
!= TIMER_ID_NOT_SET
) {
359 cancel_timer(session
->timer_id_open_keep_alive
);
363 /* Internal util function */
364 static int get_next_session_id()
366 if (session_id_
== INT_MAX
) {
370 return session_id_
++;
373 /* Internal util function */
374 static pcep_session
*create_pcep_session_pre_setup(pcep_configuration
*config
)
376 if (config
== NULL
) {
377 pcep_log(LOG_WARNING
,
378 "%s: Cannot create pcep session with NULL config",
383 pcep_session
*session
=
384 pceplib_malloc(PCEPLIB_INFRA
, sizeof(pcep_session
));
385 memset(session
, 0, sizeof(pcep_session
));
386 session
->session_id
= get_next_session_id();
387 session
->session_state
= SESSION_STATE_INITIALIZED
;
388 session
->timer_id_open_keep_wait
= TIMER_ID_NOT_SET
;
389 session
->timer_id_open_keep_alive
= TIMER_ID_NOT_SET
;
390 session
->timer_id_dead_timer
= TIMER_ID_NOT_SET
;
391 session
->timer_id_keep_alive
= TIMER_ID_NOT_SET
;
392 session
->stateful_pce
= false;
393 session
->num_unknown_messages_time_queue
= queue_initialize();
394 session
->pce_open_received
= false;
395 session
->pce_open_rejected
= false;
396 session
->pce_open_keep_alive_sent
= false;
397 session
->pcc_open_rejected
= false;
398 session
->pce_open_accepted
= false;
399 session
->pcc_open_accepted
= false;
400 session
->destroy_session_after_write
= false;
401 session
->lsp_db_version
= config
->lsp_db_version
;
402 memcpy(&(session
->pcc_config
), config
, sizeof(pcep_configuration
));
403 /* copy the pcc_config to the pce_config until we receive the open
404 * keep_alive response */
405 memcpy(&(session
->pce_config
), config
, sizeof(pcep_configuration
));
406 if (config
->pcep_msg_versioning
!= NULL
) {
407 session
->pcc_config
.pcep_msg_versioning
= pceplib_malloc(
408 PCEPLIB_INFRA
, sizeof(struct pcep_versioning
));
409 memcpy(session
->pcc_config
.pcep_msg_versioning
,
410 config
->pcep_msg_versioning
,
411 sizeof(struct pcep_versioning
));
412 session
->pce_config
.pcep_msg_versioning
= pceplib_malloc(
413 PCEPLIB_INFRA
, sizeof(struct pcep_versioning
));
414 memcpy(session
->pce_config
.pcep_msg_versioning
,
415 config
->pcep_msg_versioning
,
416 sizeof(struct pcep_versioning
));
419 pthread_mutex_lock(&(session_logic_handle_
->session_list_mutex
));
420 ordered_list_add_node(session_logic_handle_
->session_list
, session
);
423 "%s: create_pcep_session_pre_setup add session_list sessionPtr %p",
425 pthread_mutex_unlock(&(session_logic_handle_
->session_list_mutex
));
430 /* Internal util function */
431 static bool create_pcep_session_post_setup(pcep_session
*session
)
433 if (!socket_comm_session_connect_tcp(session
->socket_comm_session
)) {
434 pcep_log(LOG_WARNING
, "%s: Cannot establish TCP socket.",
436 destroy_pcep_session(session
);
441 session
->time_connected
= time(NULL
);
442 create_session_counters(session
);
444 send_pcep_open(session
);
446 session
->session_state
= SESSION_STATE_PCEP_CONNECTING
;
447 session
->timer_id_open_keep_wait
=
448 create_timer(session
->pcc_config
.keep_alive_seconds
, session
);
449 // session->session_state = SESSION_STATE_OPENED;
454 pcep_session
*create_pcep_session(pcep_configuration
*config
,
455 struct in_addr
*pce_ip
)
457 if (pce_ip
== NULL
) {
458 pcep_log(LOG_WARNING
,
459 "%s: Cannot create pcep session with NULL pce_ip",
464 pcep_session
*session
= create_pcep_session_pre_setup(config
);
465 if (session
== NULL
) {
469 session
->socket_comm_session
= socket_comm_session_initialize_with_src(
470 NULL
, session_logic_msg_ready_handler
,
471 session_logic_message_sent_handler
,
472 session_logic_conn_except_notifier
, &(config
->src_ip
.src_ipv4
),
473 ((config
->src_pcep_port
== 0) ? PCEP_TCP_PORT
474 : config
->src_pcep_port
),
476 ((config
->dst_pcep_port
== 0) ? PCEP_TCP_PORT
477 : config
->dst_pcep_port
),
478 config
->socket_connect_timeout_millis
,
479 config
->tcp_authentication_str
, config
->is_tcp_auth_md5
,
481 if (session
->socket_comm_session
== NULL
) {
482 pcep_log(LOG_WARNING
,
483 "%s: Cannot establish socket_comm_session.", __func__
);
484 destroy_pcep_session(session
);
489 if (create_pcep_session_post_setup(session
) == false) {
496 pcep_session
*create_pcep_session_ipv6(pcep_configuration
*config
,
497 struct in6_addr
*pce_ip
)
499 if (pce_ip
== NULL
) {
500 pcep_log(LOG_WARNING
,
501 "%s: Cannot create pcep session with NULL pce_ip",
506 pcep_session
*session
= create_pcep_session_pre_setup(config
);
507 if (session
== NULL
) {
511 session
->socket_comm_session
=
512 socket_comm_session_initialize_with_src_ipv6(
513 NULL
, session_logic_msg_ready_handler
,
514 session_logic_message_sent_handler
,
515 session_logic_conn_except_notifier
,
516 &(config
->src_ip
.src_ipv6
),
517 ((config
->src_pcep_port
== 0) ? PCEP_TCP_PORT
518 : config
->src_pcep_port
),
520 ((config
->dst_pcep_port
== 0) ? PCEP_TCP_PORT
521 : config
->dst_pcep_port
),
522 config
->socket_connect_timeout_millis
,
523 config
->tcp_authentication_str
, config
->is_tcp_auth_md5
,
525 if (session
->socket_comm_session
== NULL
) {
526 pcep_log(LOG_WARNING
,
527 "%s: Cannot establish ipv6 socket_comm_session.",
529 destroy_pcep_session(session
);
534 if (create_pcep_session_post_setup(session
) == false) {
542 void session_send_message(pcep_session
*session
, struct pcep_message
*message
)
544 pcep_encode_message(message
, session
->pcc_config
.pcep_msg_versioning
);
545 socket_comm_session_send_message(session
->socket_comm_session
,
546 (char *)message
->encoded_message
,
547 message
->encoded_message_length
, true);
549 increment_message_tx_counters(session
, message
);
551 /* The message->encoded_message will be freed in
552 * socket_comm_session_send_message() once sent.
553 * Setting to NULL here so pcep_msg_free_message() does not free it */
554 message
->encoded_message
= NULL
;
555 pcep_msg_free_message(message
);
559 /* This function is also used in pcep_session_logic_states.c */
560 struct pcep_message
*create_pcep_open(pcep_session
*session
)
562 /* create and send PCEP open
563 * with PCEP, the PCC sends the config the PCE should use in the open
565 * and the PCE will send an open with the config the PCC should use. */
566 double_linked_list
*tlv_list
= dll_initialize();
567 if (session
->pcc_config
.support_stateful_pce_lsp_update
568 || session
->pcc_config
.support_pce_lsp_instantiation
569 || session
->pcc_config
.support_include_db_version
570 || session
->pcc_config
.support_lsp_triggered_resync
571 || session
->pcc_config
.support_lsp_delta_sync
572 || session
->pcc_config
.support_pce_triggered_initial_sync
) {
573 /* Prepend this TLV as the first in the list */
576 pcep_tlv_create_stateful_pce_capability(
578 .support_stateful_pce_lsp_update
, /* U
582 .support_include_db_version
, /* S flag
585 .support_lsp_triggered_resync
, /* T flag
588 .support_lsp_delta_sync
, /* D flag */
590 .support_pce_triggered_initial_sync
, /* F flag */
592 .support_pce_lsp_instantiation
)); /* I
597 if (session
->pcc_config
.support_include_db_version
) {
598 if (session
->pcc_config
.lsp_db_version
!= 0) {
600 pcep_tlv_create_lsp_db_version(
601 session
->pcc_config
.lsp_db_version
));
605 if (session
->pcc_config
.support_sr_te_pst
) {
608 if (session
->pcc_config
.pcep_msg_versioning
609 ->draft_ietf_pce_segment_routing_07
611 flag_n
= session
->pcc_config
.pcc_can_resolve_nai_to_sid
;
612 flag_x
= (session
->pcc_config
.max_sid_depth
== 0);
615 struct pcep_object_tlv_sr_pce_capability
*sr_pce_cap_tlv
=
616 pcep_tlv_create_sr_pce_capability(
618 session
->pcc_config
.max_sid_depth
);
620 double_linked_list
*sub_tlv_list
= NULL
;
621 if (session
->pcc_config
.pcep_msg_versioning
622 ->draft_ietf_pce_segment_routing_07
624 /* With draft07, send the sr_pce_cap_tlv as a normal TLV
626 dll_append(tlv_list
, sr_pce_cap_tlv
);
628 /* With draft16, send the sr_pce_cap_tlv as a sub-TLV in
629 * the path_setup_type_capability TLV */
630 sub_tlv_list
= dll_initialize();
631 dll_append(sub_tlv_list
, sr_pce_cap_tlv
);
635 pceplib_malloc(PCEPLIB_MESSAGES
, sizeof(uint8_t));
637 double_linked_list
*pst_list
= dll_initialize();
638 dll_append(pst_list
, pst
);
639 dll_append(tlv_list
, pcep_tlv_create_path_setup_type_capability(
640 pst_list
, sub_tlv_list
));
643 struct pcep_message
*open_msg
= pcep_msg_create_open_with_tlvs(
644 session
->pcc_config
.keep_alive_seconds
,
645 session
->pcc_config
.dead_timer_seconds
, session
->session_id
,
650 "%s: [%ld-%ld] pcep_session_logic create open message: TLVs [%d] for session [%d]",
651 __func__
, time(NULL
), pthread_self(), tlv_list
->num_entries
,
652 session
->session_id
);
658 void send_pcep_open(pcep_session
*session
)
660 session_send_message(session
, create_pcep_open(session
));
663 /* This is a blocking call, since it is synchronized with destroy_pcep_session()
664 * and session_logic_loop(). It may be possible that the session has been
665 * deleted but API users havent been informed yet.
667 bool session_exists(pcep_session
*session
)
669 if (session_logic_handle_
== NULL
) {
671 "%s: session_exists session_logic_handle_ is NULL",
676 pthread_mutex_lock(&(session_logic_handle_
->session_list_mutex
));
678 (ordered_list_find(session_logic_handle_
->session_list
, session
)
680 pthread_mutex_unlock(&(session_logic_handle_
->session_list_mutex
));