]> git.proxmox.com Git - mirror_frr.git/blob - pceplib/pcep_session_logic.c
Merge pull request #7963 from volta-networks/feat_pceplib_into_frr_github
[mirror_frr.git] / pceplib / pcep_session_logic.c
1 /*
2 * This file is part of the PCEPlib, a PCEP protocol library.
3 *
4 * Copyright (C) 2020 Volta Networks https://voltanet.io/
5 *
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.
10 *
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.
15 *
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/>.
18 *
19 * Author : Brady Johnson <brady@voltanet.io>
20 *
21 */
22
23
24 #include <errno.h>
25 #include <limits.h>
26 #include <pthread.h>
27 #include <stdbool.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <time.h>
31
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"
40
41 /*
42 * public API function implementations for the session_logic
43 */
44
45 pcep_session_logic_handle *session_logic_handle_ = NULL;
46 pcep_event_queue *session_logic_event_queue_ = NULL;
47 int session_id_ = 0;
48
49 void send_pcep_open(pcep_session *session); /* forward decl */
50
51 static bool run_session_logic_common()
52 {
53 if (session_logic_handle_ != NULL) {
54 pcep_log(LOG_WARNING,
55 "%s: Session Logic is already initialized.", __func__);
56 return false;
57 }
58
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));
62
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();
68
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),
74 NULL)
75 != 0) {
76 pcep_log(
77 LOG_ERR,
78 "%s: Cannot initialize session_logic event queue mutex.",
79 __func__);
80 return false;
81 }
82
83 pthread_cond_init(&(session_logic_handle_->session_logic_cond_var),
84 NULL);
85
86 if (pthread_mutex_init(&(session_logic_handle_->session_logic_mutex),
87 NULL)
88 != 0) {
89 pcep_log(LOG_ERR, "%s: Cannot initialize session_logic mutex.",
90 __func__);
91 return false;
92 }
93
94 if (pthread_mutex_init(&(session_logic_handle_->session_list_mutex),
95 NULL)
96 != 0) {
97 pcep_log(LOG_ERR, "%s: Cannot initialize session_list mutex.",
98 __func__);
99 return false;
100 }
101
102 return true;
103 }
104
105
106 bool run_session_logic()
107 {
108 if (!run_session_logic_common()) {
109 return false;
110 }
111
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.",
115 __func__);
116 return false;
117 }
118
119 if (!initialize_timers(session_logic_timer_expire_handler)) {
120 pcep_log(LOG_ERR, "%s: Cannot initialize session_logic timers.",
121 __func__);
122 return false;
123 }
124
125 /* No need to call initialize_socket_comm_loop() since it will be
126 * called internally when the first socket_comm_session is created. */
127
128 return true;
129 }
130
131
132 bool run_session_logic_with_infra(pceplib_infra_config *infra_config)
133 {
134 if (infra_config == NULL) {
135 return run_session_logic();
136 }
137
138 /* Initialize the memory infrastructure before anything gets allocated
139 */
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);
148 }
149
150 if (!run_session_logic_common()) {
151 return false;
152 }
153
154 /* Create the pcep_session_logic pthread so it can be managed externally
155 */
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")) {
161 pcep_log(
162 LOG_ERR,
163 "%s: Cannot initialize external session_logic thread.",
164 __func__);
165 return false;
166 }
167 } else {
168 if (pthread_create(
169 &(session_logic_handle_->session_logic_thread),
170 NULL, session_logic_loop, session_logic_handle_)) {
171 pcep_log(LOG_ERR,
172 "%s: Cannot initialize session_logic thread.",
173 __func__);
174 return false;
175 }
176 }
177
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;
182
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)) {
189 pcep_log(
190 LOG_ERR,
191 "%s: Cannot initialize session_logic timers with infra.",
192 __func__);
193 return false;
194 }
195
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. */
199
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)) {
206 pcep_log(
207 LOG_ERR,
208 "%s: Cannot initialize session_logic socket comm with infra.",
209 __func__);
210 return false;
211 }
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)) {
218 pcep_log(
219 LOG_ERR,
220 "%s: Cannot initialize session_logic socket comm with infra.",
221 __func__);
222 return false;
223 }
224 }
225
226 return true;
227 }
228
229 bool run_session_logic_wait_for_completion()
230 {
231 if (!run_session_logic()) {
232 return false;
233 }
234
235 /* Blocking call, waits for session logic thread to complete */
236 pthread_join(session_logic_handle_->session_logic_thread, NULL);
237
238 return true;
239 }
240
241
242 bool stop_session_logic()
243 {
244 if (session_logic_handle_ == NULL) {
245 pcep_log(LOG_WARNING, "%s: Session logic already stopped",
246 __func__);
247 return false;
248 }
249
250 session_logic_handle_->active = false;
251 teardown_timers();
252
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);
258
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);
263
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_);
268
269 /* Explicitly stop the socket comm loop started by the pcep_sessions */
270 destroy_socket_comm_loop();
271
272 pceplib_free(PCEPLIB_INFRA, session_logic_handle_);
273 session_logic_handle_ = NULL;
274
275 return true;
276 }
277
278
279 void close_pcep_session(pcep_session *session)
280 {
281 close_pcep_session_with_reason(session, PCEP_CLOSE_REASON_NO);
282 }
283
284 void close_pcep_session_with_reason(pcep_session *session,
285 enum pcep_close_reason reason)
286 {
287 struct pcep_message *close_msg = pcep_msg_create_close(reason);
288
289 pcep_log(
290 LOG_INFO,
291 "%s: [%ld-%ld] pcep_session_logic send pcep_close message for session [%d]",
292 __func__, time(NULL), pthread_self(), session->session_id);
293
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;
297 }
298
299
300 void destroy_pcep_session(pcep_session *session)
301 {
302 if (session == NULL) {
303 pcep_log(LOG_WARNING, "%s: Cannot destroy NULL session",
304 __func__);
305 return;
306 }
307
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);
314 pcep_log(LOG_DEBUG,
315 "%s: destroy_pcep_session delete session_list sessionPtr %p",
316 __func__, session);
317
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);
322
323 if (session->pcc_config.pcep_msg_versioning != NULL) {
324 pceplib_free(PCEPLIB_INFRA,
325 session->pcc_config.pcep_msg_versioning);
326 }
327
328 if (session->pce_config.pcep_msg_versioning != NULL) {
329 pceplib_free(PCEPLIB_INFRA,
330 session->pce_config.pcep_msg_versioning);
331 }
332
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));
338 }
339
340 void pcep_session_cancel_timers(pcep_session *session)
341 {
342 if (session == NULL) {
343 return;
344 }
345
346 if (session->timer_id_dead_timer != TIMER_ID_NOT_SET) {
347 cancel_timer(session->timer_id_dead_timer);
348 }
349
350 if (session->timer_id_keep_alive != TIMER_ID_NOT_SET) {
351 cancel_timer(session->timer_id_keep_alive);
352 }
353
354 if (session->timer_id_open_keep_wait != TIMER_ID_NOT_SET) {
355 cancel_timer(session->timer_id_open_keep_wait);
356 }
357
358 if (session->timer_id_open_keep_alive != TIMER_ID_NOT_SET) {
359 cancel_timer(session->timer_id_open_keep_alive);
360 }
361 }
362
363 /* Internal util function */
364 static int get_next_session_id()
365 {
366 if (session_id_ == INT_MAX) {
367 session_id_ = 0;
368 }
369
370 return session_id_++;
371 }
372
373 /* Internal util function */
374 static pcep_session *create_pcep_session_pre_setup(pcep_configuration *config)
375 {
376 if (config == NULL) {
377 pcep_log(LOG_WARNING,
378 "%s: Cannot create pcep session with NULL config",
379 __func__);
380 return NULL;
381 }
382
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));
417 }
418
419 pthread_mutex_lock(&(session_logic_handle_->session_list_mutex));
420 ordered_list_add_node(session_logic_handle_->session_list, session);
421 pcep_log(
422 LOG_DEBUG,
423 "%s: create_pcep_session_pre_setup add session_list sessionPtr %p",
424 __func__, session);
425 pthread_mutex_unlock(&(session_logic_handle_->session_list_mutex));
426
427 return session;
428 }
429
430 /* Internal util function */
431 static bool create_pcep_session_post_setup(pcep_session *session)
432 {
433 if (!socket_comm_session_connect_tcp(session->socket_comm_session)) {
434 pcep_log(LOG_WARNING, "%s: Cannot establish TCP socket.",
435 __func__);
436 destroy_pcep_session(session);
437
438 return false;
439 }
440
441 session->time_connected = time(NULL);
442 create_session_counters(session);
443
444 send_pcep_open(session);
445
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;
450
451 return true;
452 }
453
454 pcep_session *create_pcep_session(pcep_configuration *config,
455 struct in_addr *pce_ip)
456 {
457 if (pce_ip == NULL) {
458 pcep_log(LOG_WARNING,
459 "%s: Cannot create pcep session with NULL pce_ip",
460 __func__);
461 return NULL;
462 }
463
464 pcep_session *session = create_pcep_session_pre_setup(config);
465 if (session == NULL) {
466 return NULL;
467 }
468
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),
475 pce_ip,
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,
480 session);
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);
485
486 return NULL;
487 }
488
489 if (create_pcep_session_post_setup(session) == false) {
490 return NULL;
491 }
492
493 return session;
494 }
495
496 pcep_session *create_pcep_session_ipv6(pcep_configuration *config,
497 struct in6_addr *pce_ip)
498 {
499 if (pce_ip == NULL) {
500 pcep_log(LOG_WARNING,
501 "%s: Cannot create pcep session with NULL pce_ip",
502 __func__);
503 return NULL;
504 }
505
506 pcep_session *session = create_pcep_session_pre_setup(config);
507 if (session == NULL) {
508 return NULL;
509 }
510
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),
519 pce_ip,
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,
524 session);
525 if (session->socket_comm_session == NULL) {
526 pcep_log(LOG_WARNING,
527 "%s: Cannot establish ipv6 socket_comm_session.",
528 __func__);
529 destroy_pcep_session(session);
530
531 return NULL;
532 }
533
534 if (create_pcep_session_post_setup(session) == false) {
535 return NULL;
536 }
537
538 return session;
539 }
540
541
542 void session_send_message(pcep_session *session, struct pcep_message *message)
543 {
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);
548
549 increment_message_tx_counters(session, message);
550
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);
556 }
557
558
559 /* This function is also used in pcep_session_logic_states.c */
560 struct pcep_message *create_pcep_open(pcep_session *session)
561 {
562 /* create and send PCEP open
563 * with PCEP, the PCC sends the config the PCE should use in the open
564 * message,
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 */
574 dll_append(
575 tlv_list,
576 pcep_tlv_create_stateful_pce_capability(
577 session->pcc_config
578 .support_stateful_pce_lsp_update, /* U
579 flag
580 */
581 session->pcc_config
582 .support_include_db_version, /* S flag
583 */
584 session->pcc_config
585 .support_lsp_triggered_resync, /* T flag
586 */
587 session->pcc_config
588 .support_lsp_delta_sync, /* D flag */
589 session->pcc_config
590 .support_pce_triggered_initial_sync, /* F flag */
591 session->pcc_config
592 .support_pce_lsp_instantiation)); /* I
593 flag
594 */
595 }
596
597 if (session->pcc_config.support_include_db_version) {
598 if (session->pcc_config.lsp_db_version != 0) {
599 dll_append(tlv_list,
600 pcep_tlv_create_lsp_db_version(
601 session->pcc_config.lsp_db_version));
602 }
603 }
604
605 if (session->pcc_config.support_sr_te_pst) {
606 bool flag_n = false;
607 bool flag_x = false;
608 if (session->pcc_config.pcep_msg_versioning
609 ->draft_ietf_pce_segment_routing_07
610 == false) {
611 flag_n = session->pcc_config.pcc_can_resolve_nai_to_sid;
612 flag_x = (session->pcc_config.max_sid_depth == 0);
613 }
614
615 struct pcep_object_tlv_sr_pce_capability *sr_pce_cap_tlv =
616 pcep_tlv_create_sr_pce_capability(
617 flag_n, flag_x,
618 session->pcc_config.max_sid_depth);
619
620 double_linked_list *sub_tlv_list = NULL;
621 if (session->pcc_config.pcep_msg_versioning
622 ->draft_ietf_pce_segment_routing_07
623 == true) {
624 /* With draft07, send the sr_pce_cap_tlv as a normal TLV
625 */
626 dll_append(tlv_list, sr_pce_cap_tlv);
627 } else {
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);
632 }
633
634 uint8_t *pst =
635 pceplib_malloc(PCEPLIB_MESSAGES, sizeof(uint8_t));
636 *pst = SR_TE_PST;
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));
641 }
642
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,
646 tlv_list);
647
648 pcep_log(
649 LOG_INFO,
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);
653
654 return (open_msg);
655 }
656
657
658 void send_pcep_open(pcep_session *session)
659 {
660 session_send_message(session, create_pcep_open(session));
661 }
662
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.
666 */
667 bool session_exists(pcep_session *session)
668 {
669 if (session_logic_handle_ == NULL) {
670 pcep_log(LOG_DEBUG,
671 "%s: session_exists session_logic_handle_ is NULL",
672 __func__);
673 return false;
674 }
675
676 pthread_mutex_lock(&(session_logic_handle_->session_list_mutex));
677 bool retval =
678 (ordered_list_find(session_logic_handle_->session_list, session)
679 != NULL);
680 pthread_mutex_unlock(&(session_logic_handle_->session_list_mutex));
681
682 return retval;
683 }