blob: d722a55274c4e56e94b590069d1d1db90995d67a [file] [log] [blame]
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001/*
2 * Copyright (c) 2011-2016 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15/**
16 * @file
17 * @brief BFD global declarations
18 */
19#ifndef __included_bfd_main_h__
20#define __included_bfd_main_h__
21
22#include <vppinfra/timing_wheel.h>
23#include <vnet/vnet.h>
24#include <vnet/bfd/bfd_protocol.h>
25#include <vnet/bfd/bfd_udp.h>
26
Klement Sekera0e3c0de2016-09-29 14:43:44 +020027#define foreach_bfd_mode(F) \
28 F (asynchronous) \
29 F (demand)
30
31typedef enum
32{
33#define F(x) BFD_MODE_##x,
34 foreach_bfd_mode (F)
35#undef F
36} bfd_mode_e;
37
38typedef struct
39{
Klement Sekerab16bfe32017-02-28 11:56:48 +010040 /** global configuration key ID */
Klement Sekerab17dd962017-01-09 07:43:48 +010041 u32 conf_key_id;
42
Klement Sekerab16bfe32017-02-28 11:56:48 +010043 /** keeps track of how many sessions reference this key */
Klement Sekerab17dd962017-01-09 07:43:48 +010044 u32 use_count;
45
Klement Sekerab16bfe32017-02-28 11:56:48 +010046 /**
Klement Sekerab17dd962017-01-09 07:43:48 +010047 * key data directly usable for bfd purposes - already padded with zeroes
48 * (so we don't need the actual length)
49 */
50 u8 key[20];
51
Klement Sekerab16bfe32017-02-28 11:56:48 +010052 /** authentication type for this key */
Klement Sekerab17dd962017-01-09 07:43:48 +010053 bfd_auth_type_e auth_type;
54} bfd_auth_key_t;
55
Klement Sekera239790f2017-02-16 10:53:53 +010056#define foreach_bfd_poll_state(F) \
57 F (NOT_NEEDED) \
58 F (NEEDED) \
59 F (IN_PROGRESS) \
60 F (IN_PROGRESS_AND_QUEUED)
Klement Sekeraa57a9702017-02-02 06:58:07 +010061
62typedef enum
63{
Klement Sekera239790f2017-02-16 10:53:53 +010064#define F(x) BFD_POLL_##x,
Klement Sekeraa57a9702017-02-02 06:58:07 +010065 foreach_bfd_poll_state (F)
66#undef F
67} bfd_poll_state_e;
68
Klement Sekera2bce0332017-02-09 06:03:46 +010069typedef struct bfd_session_s
Klement Sekerab17dd962017-01-09 07:43:48 +010070{
Klement Sekerab16bfe32017-02-28 11:56:48 +010071 /** index in bfd_main.sessions pool */
Klement Sekera637b9c42016-12-08 05:19:14 +010072 u32 bs_idx;
Klement Sekera0e3c0de2016-09-29 14:43:44 +020073
Klement Sekerab16bfe32017-02-28 11:56:48 +010074 /** session state */
Klement Sekera0e3c0de2016-09-29 14:43:44 +020075 bfd_state_e local_state;
76
Klement Sekerab16bfe32017-02-28 11:56:48 +010077 /** remote session state */
Klement Sekera73884482017-02-23 09:26:30 +010078 bfd_state_e remote_state;
79
Klement Sekerab16bfe32017-02-28 11:56:48 +010080 /** local diagnostics */
Klement Sekera0e3c0de2016-09-29 14:43:44 +020081 bfd_diag_code_e local_diag;
82
Klement Sekerab16bfe32017-02-28 11:56:48 +010083 /** remote diagnostics */
Klement Sekera73884482017-02-23 09:26:30 +010084 bfd_diag_code_e remote_diag;
Klement Sekera0e3c0de2016-09-29 14:43:44 +020085
Klement Sekerab16bfe32017-02-28 11:56:48 +010086 /** local discriminator */
Klement Sekera0e3c0de2016-09-29 14:43:44 +020087 u32 local_discr;
88
Klement Sekerab16bfe32017-02-28 11:56:48 +010089 /** remote discriminator */
Klement Sekera0e3c0de2016-09-29 14:43:44 +020090 u32 remote_discr;
91
Klement Sekerab16bfe32017-02-28 11:56:48 +010092 /** configured desired min tx interval (microseconds) */
Klement Sekeraa57a9702017-02-02 06:58:07 +010093 u32 config_desired_min_tx_usec;
Klement Sekera0e3c0de2016-09-29 14:43:44 +020094
Klement Sekerab16bfe32017-02-28 11:56:48 +010095 /** configured desired min tx interval (clocks) */
Klement Sekeraa57a9702017-02-02 06:58:07 +010096 u64 config_desired_min_tx_clocks;
Klement Sekera0e3c0de2016-09-29 14:43:44 +020097
Klement Sekerab16bfe32017-02-28 11:56:48 +010098 /** effective desired min tx interval (clocks) */
Klement Sekeraa57a9702017-02-02 06:58:07 +010099 u64 effective_desired_min_tx_clocks;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200100
Klement Sekerab16bfe32017-02-28 11:56:48 +0100101 /** configured required min rx interval (microseconds) */
Klement Sekeraa57a9702017-02-02 06:58:07 +0100102 u32 config_required_min_rx_usec;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200103
Klement Sekerab16bfe32017-02-28 11:56:48 +0100104 /** configured required min rx interval (clocks) */
Klement Sekeraa57a9702017-02-02 06:58:07 +0100105 u64 config_required_min_rx_clocks;
106
Klement Sekerab16bfe32017-02-28 11:56:48 +0100107 /** effective required min rx interval (clocks) */
Klement Sekeraa57a9702017-02-02 06:58:07 +0100108 u64 effective_required_min_rx_clocks;
Klement Sekera3e0a3562016-12-19 09:05:21 +0100109
Klement Sekerab16bfe32017-02-28 11:56:48 +0100110 /** remote min rx interval (microseconds) */
Klement Sekeraa57a9702017-02-02 06:58:07 +0100111 u64 remote_min_rx_usec;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200112
Klement Sekerab16bfe32017-02-28 11:56:48 +0100113 /** remote min rx interval (clocks) */
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200114 u64 remote_min_rx_clocks;
115
Klement Sekerab16bfe32017-02-28 11:56:48 +0100116 /** remote min echo rx interval (microseconds) */
Klement Sekera239790f2017-02-16 10:53:53 +0100117 u64 remote_min_echo_rx_usec;
118
Klement Sekerab16bfe32017-02-28 11:56:48 +0100119 /** remote min echo rx interval (clocks) */
Klement Sekera239790f2017-02-16 10:53:53 +0100120 u64 remote_min_echo_rx_clocks;
121
Klement Sekerab16bfe32017-02-28 11:56:48 +0100122 /** remote desired min tx interval (clocks) */
Klement Sekeraa57a9702017-02-02 06:58:07 +0100123 u64 remote_desired_min_tx_clocks;
124
Klement Sekerab16bfe32017-02-28 11:56:48 +0100125 /** configured detect multiplier */
Klement Sekeraa57a9702017-02-02 06:58:07 +0100126 u8 local_detect_mult;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200127
Klement Sekerab16bfe32017-02-28 11:56:48 +0100128 /** 1 if remote system sets demand mode, 0 otherwise */
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200129 u8 remote_demand;
130
Klement Sekerab16bfe32017-02-28 11:56:48 +0100131 /** remote detect multiplier */
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200132 u8 remote_detect_mult;
133
Klement Sekerab16bfe32017-02-28 11:56:48 +0100134 /** 1 is echo function is active, 0 otherwise */
Klement Sekera239790f2017-02-16 10:53:53 +0100135 u8 echo;
136
Klement Sekerab16bfe32017-02-28 11:56:48 +0100137 /** set to value of timer in timing wheel, 0 if never set */
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200138 u64 wheel_time_clocks;
139
Klement Sekerab16bfe32017-02-28 11:56:48 +0100140 /** transmit interval */
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200141 u64 transmit_interval_clocks;
142
Klement Sekerab16bfe32017-02-28 11:56:48 +0100143 /** next time at which to transmit a packet */
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200144 u64 tx_timeout_clocks;
145
Klement Sekerab16bfe32017-02-28 11:56:48 +0100146 /** timestamp of last packet transmitted */
Klement Sekera3e0a3562016-12-19 09:05:21 +0100147 u64 last_tx_clocks;
148
Klement Sekerab16bfe32017-02-28 11:56:48 +0100149 /** timestamp of last packet received */
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200150 u64 last_rx_clocks;
151
Klement Sekerab16bfe32017-02-28 11:56:48 +0100152 /** transmit interval for echo packets */
Klement Sekera239790f2017-02-16 10:53:53 +0100153 u64 echo_transmit_interval_clocks;
154
Klement Sekerab16bfe32017-02-28 11:56:48 +0100155 /** next time at which to transmit echo packet */
Klement Sekera239790f2017-02-16 10:53:53 +0100156 u64 echo_tx_timeout_clocks;
157
Klement Sekerab16bfe32017-02-28 11:56:48 +0100158 /** timestamp of last echo packet transmitted */
Klement Sekera239790f2017-02-16 10:53:53 +0100159 u64 echo_last_tx_clocks;
160
Klement Sekerab16bfe32017-02-28 11:56:48 +0100161 /** timestamp of last echo packet received */
Klement Sekera239790f2017-02-16 10:53:53 +0100162 u64 echo_last_rx_clocks;
163
Klement Sekerab16bfe32017-02-28 11:56:48 +0100164 /** secret used for calculating/checking checksum of echo packets */
Klement Sekera239790f2017-02-16 10:53:53 +0100165 u32 echo_secret;
166
Klement Sekerab16bfe32017-02-28 11:56:48 +0100167 /** detection time */
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200168 u64 detection_time_clocks;
169
Klement Sekerab16bfe32017-02-28 11:56:48 +0100170 /** state info regarding poll sequence */
Klement Sekeraa57a9702017-02-02 06:58:07 +0100171 bfd_poll_state_e poll_state;
172
Klement Sekerab16bfe32017-02-28 11:56:48 +0100173 /**
Klement Sekera239790f2017-02-16 10:53:53 +0100174 * helper for delayed poll sequence - marks either start of running poll
175 * sequence or timeout, after which we can start the next poll sequnce
176 */
177 u64 poll_state_start_or_timeout_clocks;
178
Klement Sekerab16bfe32017-02-28 11:56:48 +0100179 /** authentication information */
Klement Sekerab17dd962017-01-09 07:43:48 +0100180 struct
181 {
Klement Sekerab16bfe32017-02-28 11:56:48 +0100182 /** current key in use */
Klement Sekerab17dd962017-01-09 07:43:48 +0100183 bfd_auth_key_t *curr_key;
184
Klement Sekerab16bfe32017-02-28 11:56:48 +0100185 /**
Klement Sekerab17dd962017-01-09 07:43:48 +0100186 * set to next key to use if delayed switch is enabled - in that case
187 * the key is switched when first incoming packet is signed with next_key
188 */
189 bfd_auth_key_t *next_key;
190
Klement Sekerab16bfe32017-02-28 11:56:48 +0100191 /** sequence number incremented occasionally or always (if meticulous) */
Klement Sekerab17dd962017-01-09 07:43:48 +0100192 u32 local_seq_number;
193
Klement Sekerab16bfe32017-02-28 11:56:48 +0100194 /** remote sequence number */
Klement Sekerab17dd962017-01-09 07:43:48 +0100195 u32 remote_seq_number;
196
Klement Sekerab16bfe32017-02-28 11:56:48 +0100197 /** set to 1 if remote sequence number is known */
Klement Sekerab17dd962017-01-09 07:43:48 +0100198 u8 remote_seq_number_known;
199
Klement Sekerab16bfe32017-02-28 11:56:48 +0100200 /** current key ID sent out in bfd packet */
Klement Sekerab17dd962017-01-09 07:43:48 +0100201 u8 curr_bfd_key_id;
202
Klement Sekerab16bfe32017-02-28 11:56:48 +0100203 /** key ID to use when switched to next_key */
Klement Sekerab17dd962017-01-09 07:43:48 +0100204 u8 next_bfd_key_id;
205
Klement Sekerab16bfe32017-02-28 11:56:48 +0100206 /**
Klement Sekerab17dd962017-01-09 07:43:48 +0100207 * set to 1 if delayed action is pending, which might be activation
208 * of authentication, change of key or deactivation
209 */
210 u8 is_delayed;
211 } auth;
212
Klement Sekerab16bfe32017-02-28 11:56:48 +0100213 /** transport type for this session */
Klement Sekera239790f2017-02-16 10:53:53 +0100214 bfd_transport_e transport;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200215
Klement Sekerab16bfe32017-02-28 11:56:48 +0100216 /** union of transport-specific data */
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200217 union
218 {
219 bfd_udp_session_t udp;
220 };
221} bfd_session_t;
222
223typedef struct
224{
Klement Sekerab16bfe32017-02-28 11:56:48 +0100225 /** pool of bfd sessions context data */
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200226 bfd_session_t *sessions;
227
Klement Sekerab16bfe32017-02-28 11:56:48 +0100228 /** timing wheel for scheduling timeouts */
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200229 timing_wheel_t wheel;
230
Klement Sekerab16bfe32017-02-28 11:56:48 +0100231 /** timing wheel inaccuracy, in clocks */
Klement Sekera637b9c42016-12-08 05:19:14 +0100232 u64 wheel_inaccuracy;
233
Klement Sekerab16bfe32017-02-28 11:56:48 +0100234 /** hashmap - bfd session by discriminator */
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200235 u32 *session_by_disc;
236
Klement Sekerab16bfe32017-02-28 11:56:48 +0100237 /** background process node index */
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200238 u32 bfd_process_node_index;
239
Klement Sekerab16bfe32017-02-28 11:56:48 +0100240 /** convenience variables */
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200241 vlib_main_t *vlib_main;
242 vnet_main_t *vnet_main;
243
Klement Sekerab16bfe32017-02-28 11:56:48 +0100244 /** cpu clocks per second */
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200245 f64 cpu_cps;
246
Klement Sekerab16bfe32017-02-28 11:56:48 +0100247 /** default desired min tx in clocks */
Klement Sekeraa57a9702017-02-02 06:58:07 +0100248 u64 default_desired_min_tx_clocks;
249
Klement Sekerab16bfe32017-02-28 11:56:48 +0100250 /** minimum required min rx while echo function is active - clocks */
Klement Sekera239790f2017-02-16 10:53:53 +0100251 u64 min_required_min_rx_while_echo_clocks;
252
Klement Sekerab16bfe32017-02-28 11:56:48 +0100253 /** for generating random numbers */
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200254 u32 random_seed;
255
Klement Sekerab16bfe32017-02-28 11:56:48 +0100256 /** pool of authentication keys */
Klement Sekerab17dd962017-01-09 07:43:48 +0100257 bfd_auth_key_t *auth_keys;
258
Klement Sekerab16bfe32017-02-28 11:56:48 +0100259 /** hashmap - index in pool auth_keys by conf_key_id */
Klement Sekerab17dd962017-01-09 07:43:48 +0100260 u32 *auth_key_by_conf_key_id;
261
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200262} bfd_main_t;
263
264extern bfd_main_t bfd_main;
265
Klement Sekerab16bfe32017-02-28 11:56:48 +0100266/** Packet counters */
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200267#define foreach_bfd_error(F) \
268 F (NONE, "good bfd packets (processed)") \
269 F (BAD, "invalid bfd packets") \
270 F (DISABLED, "bfd packets received on disabled interfaces")
271
272typedef enum
273{
274#define F(sym, str) BFD_ERROR_##sym,
275 foreach_bfd_error (F)
276#undef F
277 BFD_N_ERROR,
278} bfd_error_t;
279
Klement Sekerab16bfe32017-02-28 11:56:48 +0100280/** bfd packet trace capture */
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200281typedef struct
282{
283 u32 len;
284 u8 data[400];
285} bfd_input_trace_t;
286
287enum
288{
289 BFD_EVENT_RESCHEDULE = 1,
290 BFD_EVENT_NEW_SESSION,
Klement Sekeraa57a9702017-02-02 06:58:07 +0100291 BFD_EVENT_CONFIG_CHANGED,
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200292} bfd_process_event_e;
293
Klement Sekera239790f2017-02-16 10:53:53 +0100294/* *INDENT-OFF* */
Klement Sekerab16bfe32017-02-28 11:56:48 +0100295/** echo packet structure */
Klement Sekera239790f2017-02-16 10:53:53 +0100296typedef CLIB_PACKED (struct {
Klement Sekerab16bfe32017-02-28 11:56:48 +0100297 /** local discriminator */
Klement Sekera239790f2017-02-16 10:53:53 +0100298 u32 discriminator;
Klement Sekerab16bfe32017-02-28 11:56:48 +0100299 /** expire time of this packet - clocks */
Klement Sekera239790f2017-02-16 10:53:53 +0100300 u64 expire_time_clocks;
Klement Sekerab16bfe32017-02-28 11:56:48 +0100301 /** checksum - based on discriminator, local secret and expire time */
Klement Sekera239790f2017-02-16 10:53:53 +0100302 u64 checksum;
303}) bfd_echo_pkt_t;
304/* *INDENT-ON* */
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200305
Klement Sekera239790f2017-02-16 10:53:53 +0100306u8 *bfd_input_format_trace (u8 * s, va_list * args);
307bfd_session_t *bfd_get_session (bfd_main_t * bm, bfd_transport_e t);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200308void bfd_put_session (bfd_main_t * bm, bfd_session_t * bs);
309bfd_session_t *bfd_find_session_by_idx (bfd_main_t * bm, uword bs_idx);
310bfd_session_t *bfd_find_session_by_disc (bfd_main_t * bm, u32 disc);
311void bfd_session_start (bfd_main_t * bm, bfd_session_t * bs);
312void bfd_consume_pkt (bfd_main_t * bm, const bfd_pkt_t * bfd, u32 bs_idx);
Klement Sekera239790f2017-02-16 10:53:53 +0100313int bfd_consume_echo_pkt (bfd_main_t * bm, vlib_buffer_t * b);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200314int bfd_verify_pkt_common (const bfd_pkt_t * pkt);
Klement Sekerab17dd962017-01-09 07:43:48 +0100315int bfd_verify_pkt_auth (const bfd_pkt_t * pkt, u16 pkt_size,
316 bfd_session_t * bs);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200317void bfd_event (bfd_main_t * bm, bfd_session_t * bs);
Klement Sekerab17dd962017-01-09 07:43:48 +0100318void bfd_init_final_control_frame (vlib_main_t * vm, vlib_buffer_t * b,
Klement Sekerae50e8562017-04-04 16:19:48 +0200319 bfd_main_t * bm, bfd_session_t * bs,
320 int is_local);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200321u8 *format_bfd_session (u8 * s, va_list * args);
Klement Sekera73884482017-02-23 09:26:30 +0100322u8 *format_bfd_auth_key (u8 * s, va_list * args);
Klement Sekerab17dd962017-01-09 07:43:48 +0100323void bfd_session_set_flags (bfd_session_t * bs, u8 admin_up_down);
324unsigned bfd_auth_type_supported (bfd_auth_type_e auth_type);
325vnet_api_error_t bfd_auth_activate (bfd_session_t * bs, u32 conf_key_id,
326 u8 bfd_key_id, u8 is_delayed);
327vnet_api_error_t bfd_auth_deactivate (bfd_session_t * bs, u8 is_delayed);
Klement Sekera239790f2017-02-16 10:53:53 +0100328vnet_api_error_t bfd_session_set_params (bfd_main_t * bm, bfd_session_t * bs,
329 u32 desired_min_tx_usec,
330 u32 required_min_rx_usec,
331 u8 detect_mult);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200332
Klement Sekera73884482017-02-23 09:26:30 +0100333u32 bfd_clocks_to_usec (const bfd_main_t * bm, u64 clocks);
334const char *bfd_poll_state_string (bfd_poll_state_e state);
335
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200336#define USEC_PER_MS 1000LL
337#define USEC_PER_SECOND (1000 * USEC_PER_MS)
338
Klement Sekerab16bfe32017-02-28 11:56:48 +0100339/** default, slow transmission interval for BFD packets, per spec at least 1s */
Klement Sekera239790f2017-02-16 10:53:53 +0100340#define BFD_DEFAULT_DESIRED_MIN_TX_USEC USEC_PER_SECOND
341
Klement Sekerab16bfe32017-02-28 11:56:48 +0100342/**
Klement Sekera239790f2017-02-16 10:53:53 +0100343 * minimum required min rx set locally when echo function is used, per spec
344 * should be set to at least 1s
345 */
346#define BFD_REQUIRED_MIN_RX_USEC_WHILE_ECHO USEC_PER_SECOND
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200347
348#endif /* __included_bfd_main_h__ */
349
350/*
351 * fd.io coding-style-patch-verification: ON
352 *
353 * Local Variables:
354 * eval: (c-set-style "gnu")
355 * End:
356 */