1 // SPDX-License-Identifier: LGPL-2.1-or-later
3 * This file is part of the PCEPlib, a PCEP protocol library.
5 * Copyright (C) 2020 Volta Networks https://voltanet.io/
7 * Author : Brady Johnson <brady@voltanet.io>
12 #ifndef INCLUDE_PCEPSESSIONLOGIC_H_
13 #define INCLUDE_PCEPSESSIONLOGIC_H_
16 #include <netinet/tcp.h>
18 #include "pcep_msg_encoding.h"
19 #include "pcep_socket_comm.h"
20 #include "pcep_msg_objects.h"
21 #include "pcep_msg_tools.h"
22 #include "pcep_timers.h"
23 #include "pcep_utils_queue.h"
24 #include "pcep_utils_memory.h"
26 #define PCEP_TCP_PORT 4189
28 typedef struct pcep_configuration_
{
29 /* These are the configuration values that will
30 * be sent to the PCE in the PCEP Open message */
31 int keep_alive_seconds
;
32 int dead_timer_seconds
;
33 int dead_timer_pce_negotiated_seconds
; /* Config data negotiated with
35 int keep_alive_pce_negotiated_timer_seconds
; /* Config data negotiated
37 int request_time_seconds
;
39 /* These are the acceptable ranges of values received by
40 * the PCE in the initial PCEP Open Message. If a value is
41 * received outside of these ranges, then the Open message
42 * will be rejected. */
43 int min_keep_alive_seconds
;
44 int max_keep_alive_seconds
;
45 int min_dead_timer_seconds
;
46 int max_dead_timer_seconds
;
48 /* If more than this many unknown messages/requests are received
49 * per minute, then the session will be closed. */
50 int max_unknown_messages
;
51 int max_unknown_requests
;
53 /* Maximum amount of time to wait to connect to the
54 * PCE TCP socket before failing, in milliseconds. */
55 uint32_t socket_connect_timeout_millis
;
57 /* Set if the PCE/PCC will support stateful PCE LSP Updates
58 * according to RCF8231, section 7.1.1, defaults to true.
59 * Will cause an additional TLV to be sent from the PCC in
61 bool support_stateful_pce_lsp_update
;
63 /* RFC 8281: I-bit, the PCC allows instantiation of an LSP by a PCE */
64 bool support_pce_lsp_instantiation
;
66 /* RFC 8232: S-bit, the PCC will include the LSP-DB-VERSION
67 * TLV in each LSP object */
68 bool support_include_db_version
;
70 /* Only set if support_include_db_version is true and if the LSP-DB
71 * survived a restart and is available. If this has a value other than
72 * 0, then a LSP-DB-VERSION TLV will be sent in the OPEN object. This
73 * value will be copied over to the pcep_session upon init. */
74 uint64_t lsp_db_version
;
76 /* RFC 8232: T-bit, the PCE can trigger resynchronization of
77 * LSPs at any point in the life of the session */
78 bool support_lsp_triggered_resync
;
80 /* RFC 8232: D-bit, the PCEP speaker allows incremental (delta)
81 * State Synchronization */
82 bool support_lsp_delta_sync
;
84 /* RFC 8232: F-bit, the PCE SHOULD trigger initial (first)
85 * State Synchronization */
86 bool support_pce_triggered_initial_sync
;
88 /* draft-ietf-pce-segment-routing-16: Send a SR PCE Capability
89 * sub-TLV in a Path Setup Type Capability TLV with a PST = 1,
90 * Path is setup using SR TE. */
91 bool support_sr_te_pst
;
92 /* Used in the SR PCE Capability sub-TLV */
93 bool pcc_can_resolve_nai_to_sid
;
94 /* Used in the SR TE Capability sub-TLV, 0 means there are no max sid
96 uint8_t max_sid_depth
;
98 /* If set to 0, then the default 4189 PCEP port will be used */
99 uint16_t dst_pcep_port
;
101 /* If set to 0, then the default 4189 PCEP port will be used.
102 * This is according to the RFC5440, Section 5 */
103 uint16_t src_pcep_port
;
106 struct in_addr src_ipv4
;
107 struct in6_addr src_ipv6
;
111 struct pcep_versioning
*pcep_msg_versioning
;
113 char tcp_authentication_str
[PCEP_MD5SIG_MAXKEYLEN
];
114 bool is_tcp_auth_md5
; /* true: RFC 2385, false: RFC 5925 */
116 } pcep_configuration
;
119 typedef enum pcep_session_state_
{
120 SESSION_STATE_UNKNOWN
= 0,
121 SESSION_STATE_INITIALIZED
= 1,
122 SESSION_STATE_PCEP_CONNECTING
= 2,
123 SESSION_STATE_PCEP_CONNECTED
= 3
125 } pcep_session_state
;
128 typedef struct pcep_session_
{
130 pcep_session_state session_state
;
131 int timer_id_open_keep_wait
;
132 int timer_id_open_keep_alive
;
133 int timer_id_dead_timer
;
134 int timer_id_keep_alive
;
135 bool pce_open_received
;
136 bool pce_open_rejected
;
137 bool pce_open_accepted
;
138 bool pce_open_keep_alive_sent
;
139 bool pcc_open_rejected
;
140 bool pcc_open_accepted
;
142 time_t time_connected
;
143 uint64_t lsp_db_version
;
144 queue_handle
*num_unknown_messages_time_queue
;
145 /* set this flag when finalizing the session */
146 bool destroy_session_after_write
;
147 pcep_socket_comm_session
*socket_comm_session
;
148 /* Configuration sent from the PCC to the PCE */
149 pcep_configuration pcc_config
;
150 /* Configuration received from the PCE, to be used in the PCC */
151 pcep_configuration pce_config
;
152 struct counters_group
*pcep_session_counters
;
157 typedef enum pcep_event_type
{
158 MESSAGE_RECEIVED
= 0,
159 PCE_CLOSED_SOCKET
= 1,
160 PCE_SENT_PCEP_CLOSE
= 2,
161 PCE_DEAD_TIMER_EXPIRED
= 3,
162 PCE_OPEN_KEEP_WAIT_TIMER_EXPIRED
= 4,
163 PCC_CONNECTED_TO_PCE
= 100,
164 PCC_CONNECTION_FAILURE
= 101,
165 PCC_PCEP_SESSION_CLOSED
= 102,
166 PCC_RCVD_INVALID_OPEN
= 103,
167 PCC_SENT_INVALID_OPEN
= 104,
168 PCC_RCVD_MAX_INVALID_MSGS
= 105,
169 PCC_RCVD_MAX_UNKOWN_MSGS
= 106
174 typedef struct pcep_event
{
175 enum pcep_event_type event_type
;
177 struct pcep_message
*message
;
178 pcep_session
*session
;
182 typedef void (*pceplib_pcep_event_callback
)(void *cb_data
, pcep_event
*);
183 typedef int (*pthread_create_callback
)(pthread_t
*pthread_id
,
184 const pthread_attr_t
*attr
,
185 void *(*start_routine
)(void *),
186 void *data
, const char *thread_name
);
189 typedef struct pcep_event_queue
{
190 /* The event_queue and event_callback are mutually exclusive.
191 * If the event_callback is configured, then the event_queue
192 * will not be used. */
193 queue_handle
*event_queue
;
194 pthread_mutex_t event_queue_mutex
;
195 pceplib_pcep_event_callback event_callback
;
196 void *event_callback_data
;
201 typedef struct pceplib_infra_config
{
202 /* Memory infrastructure */
203 void *pceplib_infra_mt
;
204 void *pceplib_messages_mt
;
205 pceplib_malloc_func malloc_func
;
206 pceplib_calloc_func calloc_func
;
207 pceplib_realloc_func realloc_func
;
208 pceplib_strdup_func strdup_func
;
209 pceplib_free_func free_func
;
211 /* External Timer and Socket infrastructure */
212 void *external_infra_data
;
213 ext_timer_create timer_create_func
;
214 ext_timer_cancel timer_cancel_func
;
215 ext_socket_write socket_write_func
;
216 ext_socket_read socket_read_func
;
218 /* External pcep_event infrastructure */
219 pceplib_pcep_event_callback pcep_event_func
;
221 /* Callback to create pthreads */
222 pthread_create_callback pthread_create_func
;
224 } pceplib_infra_config
;
227 * Counters Sub-groups definitions
229 typedef enum pcep_session_counters_subgroup_ids
{
230 COUNTER_SUBGROUP_ID_RX_MSG
= 0,
231 COUNTER_SUBGROUP_ID_TX_MSG
= 1,
232 COUNTER_SUBGROUP_ID_RX_OBJ
= 2,
233 COUNTER_SUBGROUP_ID_TX_OBJ
= 3,
234 COUNTER_SUBGROUP_ID_RX_SUBOBJ
= 4,
235 COUNTER_SUBGROUP_ID_TX_SUBOBJ
= 5,
236 COUNTER_SUBGROUP_ID_RX_RO_SR_SUBOBJ
= 6,
237 COUNTER_SUBGROUP_ID_TX_RO_SR_SUBOBJ
= 7,
238 COUNTER_SUBGROUP_ID_RX_TLV
= 8,
239 COUNTER_SUBGROUP_ID_TX_TLV
= 9,
240 COUNTER_SUBGROUP_ID_EVENT
= 10
242 } pcep_session_counters_subgroup_ids
;
244 bool run_session_logic(void);
245 bool run_session_logic_with_infra(pceplib_infra_config
*infra_config
);
247 bool run_session_logic_wait_for_completion(void);
249 bool stop_session_logic(void);
251 /* Uses the standard PCEP TCP dest port = 4189 and an ephemeral src port.
252 * To use a specific dest or src port, set them other than 0 in the
253 * pcep_configuration. */
254 pcep_session
*create_pcep_session(pcep_configuration
*config
,
255 struct in_addr
*pce_ip
);
256 pcep_session
*create_pcep_session_ipv6(pcep_configuration
*config
,
257 struct in6_addr
*pce_ip
);
259 /* Send a PCEP close for this pcep_session */
260 void close_pcep_session(pcep_session
*session
);
261 void close_pcep_session_with_reason(pcep_session
*session
,
262 enum pcep_close_reason
);
264 /* Destroy the PCEP session, a PCEP close should have
265 * already been sent with close_pcep_session() */
266 void destroy_pcep_session(pcep_session
*session
);
268 void pcep_session_cancel_timers(pcep_session
*session
);
270 /* Increments transmitted message counters, additionally counters for the
271 * objects, sub-objects, and TLVs in the message will be incremented. Received
272 * counters are incremented internally. */
273 void increment_message_tx_counters(pcep_session
*session
,
274 struct pcep_message
*message
);
276 bool session_exists(pcep_session
*session
);
278 #endif /* INCLUDE_PCEPSESSIONLOGIC_H_ */