blob: 0cc7d72c600a66f8315ef037dd148bf9b0ce0ad3 [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 nodes implementation
18 */
19
Klement Sekera239790f2017-02-16 10:53:53 +010020#if WITH_LIBSSL > 0
21#include <openssl/sha.h>
22#endif
23
24#if __SSE4_2__
25#include <x86intrin.h>
26#endif
27
Dave Barach1e3417f2018-07-25 08:30:27 -040028#include <vlibmemory/api.h>
Klement Sekera0e3c0de2016-09-29 14:43:44 +020029#include <vppinfra/random.h>
30#include <vppinfra/error.h>
31#include <vppinfra/hash.h>
Klement Sekera239790f2017-02-16 10:53:53 +010032#include <vppinfra/xxhash.h>
Klement Sekera0e3c0de2016-09-29 14:43:44 +020033#include <vnet/ethernet/ethernet.h>
34#include <vnet/ethernet/packet.h>
35#include <vnet/bfd/bfd_debug.h>
36#include <vnet/bfd/bfd_protocol.h>
37#include <vnet/bfd/bfd_main.h>
Damjan Marion07a38572018-01-21 06:44:18 -080038#include <vlib/log.h>
Klement Sekera239790f2017-02-16 10:53:53 +010039
40static u64
41bfd_calc_echo_checksum (u32 discriminator, u64 expire_time, u32 secret)
42{
43 u64 checksum = 0;
Gabriel Ganne8e66b9b2017-12-14 16:20:37 +010044#if defined(clib_crc32c_uses_intrinsics) && !defined (__i386__)
45 checksum = crc32_u64 (0, discriminator);
46 checksum = crc32_u64 (checksum, expire_time);
47 checksum = crc32_u64 (checksum, secret);
Klement Sekera239790f2017-02-16 10:53:53 +010048#else
49 checksum = clib_xxhash (discriminator ^ expire_time ^ secret);
Klement Sekerab17dd962017-01-09 07:43:48 +010050#endif
Klement Sekera239790f2017-02-16 10:53:53 +010051 return checksum;
52}
Klement Sekera0e3c0de2016-09-29 14:43:44 +020053
54static u64
Klement Sekeraa57a9702017-02-02 06:58:07 +010055bfd_usec_to_clocks (const bfd_main_t * bm, u64 us)
Klement Sekera0e3c0de2016-09-29 14:43:44 +020056{
57 return bm->cpu_cps * ((f64) us / USEC_PER_SECOND);
58}
59
Klement Sekera73884482017-02-23 09:26:30 +010060u32
Klement Sekera239790f2017-02-16 10:53:53 +010061bfd_clocks_to_usec (const bfd_main_t * bm, u64 clocks)
62{
63 return (clocks / bm->cpu_cps) * USEC_PER_SECOND;
64}
65
Klement Sekera0e3c0de2016-09-29 14:43:44 +020066static vlib_node_registration_t bfd_process_node;
67
Klement Sekera73884482017-02-23 09:26:30 +010068u8 *
Klement Sekerab17dd962017-01-09 07:43:48 +010069format_bfd_auth_key (u8 * s, va_list * args)
70{
71 const bfd_auth_key_t *key = va_arg (*args, bfd_auth_key_t *);
72 if (key)
73 {
74 s = format (s, "{auth-type=%u:%s, conf-key-id=%u, use-count=%u}, ",
75 key->auth_type, bfd_auth_type_str (key->auth_type),
76 key->conf_key_id, key->use_count);
77 }
78 else
79 {
80 s = format (s, "{none}");
81 }
82 return s;
83}
84
Klement Sekera0e3c0de2016-09-29 14:43:44 +020085/*
86 * We actually send all bfd pkts to the "error" node after scanning
87 * them, so the graph node has only one next-index. The "error-drop"
88 * node automatically bumps our per-node packet counters for us.
89 */
90typedef enum
91{
92 BFD_INPUT_NEXT_NORMAL,
93 BFD_INPUT_N_NEXT,
94} bfd_input_next_t;
95
Klement Sekerae4504c62016-12-08 10:16:41 +010096static void bfd_on_state_change (bfd_main_t * bm, bfd_session_t * bs, u64 now,
97 int handling_wakeup);
Klement Sekera0e3c0de2016-09-29 14:43:44 +020098
99static void
100bfd_set_defaults (bfd_main_t * bm, bfd_session_t * bs)
101{
102 bs->local_state = BFD_STATE_down;
103 bs->local_diag = BFD_DIAG_CODE_no_diag;
104 bs->remote_state = BFD_STATE_down;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200105 bs->remote_discr = 0;
Neale Ranns88fc83e2017-04-05 08:11:14 -0700106 bs->hop_type = BFD_HOP_TYPE_SINGLE;
Klement Sekera239790f2017-02-16 10:53:53 +0100107 bs->config_desired_min_tx_usec = BFD_DEFAULT_DESIRED_MIN_TX_USEC;
Klement Sekeraa57a9702017-02-02 06:58:07 +0100108 bs->config_desired_min_tx_clocks = bm->default_desired_min_tx_clocks;
109 bs->effective_desired_min_tx_clocks = bm->default_desired_min_tx_clocks;
110 bs->remote_min_rx_usec = 1;
111 bs->remote_min_rx_clocks = bfd_usec_to_clocks (bm, bs->remote_min_rx_usec);
Klement Sekera239790f2017-02-16 10:53:53 +0100112 bs->remote_min_echo_rx_usec = 0;
113 bs->remote_min_echo_rx_clocks = 0;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200114 bs->remote_demand = 0;
Klement Sekerab17dd962017-01-09 07:43:48 +0100115 bs->auth.remote_seq_number = 0;
116 bs->auth.remote_seq_number_known = 0;
117 bs->auth.local_seq_number = random_u32 (&bm->random_seed);
Klement Sekera239790f2017-02-16 10:53:53 +0100118 bs->echo_secret = random_u32 (&bm->random_seed);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200119}
120
121static void
122bfd_set_diag (bfd_session_t * bs, bfd_diag_code_e code)
123{
124 if (bs->local_diag != code)
125 {
126 BFD_DBG ("set local_diag, bs_idx=%d: '%d:%s'", bs->bs_idx, code,
127 bfd_diag_code_string (code));
128 bs->local_diag = code;
129 }
130}
131
132static void
Klement Sekerae4504c62016-12-08 10:16:41 +0100133bfd_set_state (bfd_main_t * bm, bfd_session_t * bs,
134 bfd_state_e new_state, int handling_wakeup)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200135{
136 if (bs->local_state != new_state)
137 {
138 BFD_DBG ("Change state, bs_idx=%d: %s->%s", bs->bs_idx,
139 bfd_state_string (bs->local_state),
140 bfd_state_string (new_state));
141 bs->local_state = new_state;
Klement Sekerae4504c62016-12-08 10:16:41 +0100142 bfd_on_state_change (bm, bs, clib_cpu_time_now (), handling_wakeup);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200143 }
144}
145
Klement Sekera73884482017-02-23 09:26:30 +0100146const char *
Klement Sekera239790f2017-02-16 10:53:53 +0100147bfd_poll_state_string (bfd_poll_state_e state)
148{
149 switch (state)
150 {
151#define F(x) \
152 case BFD_POLL_##x: \
153 return "BFD_POLL_" #x;
154 foreach_bfd_poll_state (F)
155#undef F
156 }
157 return "UNKNOWN";
158}
159
160static void
161bfd_set_poll_state (bfd_session_t * bs, bfd_poll_state_e state)
162{
163 if (bs->poll_state != state)
164 {
165 BFD_DBG ("Setting poll state=%s, bs_idx=%u",
166 bfd_poll_state_string (state), bs->bs_idx);
167 bs->poll_state = state;
168 }
169}
170
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200171static void
172bfd_recalc_tx_interval (bfd_main_t * bm, bfd_session_t * bs)
173{
Klement Sekera239790f2017-02-16 10:53:53 +0100174 bs->transmit_interval_clocks =
175 clib_max (bs->effective_desired_min_tx_clocks, bs->remote_min_rx_clocks);
176 BFD_DBG ("Recalculated transmit interval " BFD_CLK_FMT,
177 BFD_CLK_PRN (bs->transmit_interval_clocks));
178}
179
180static void
181bfd_recalc_echo_tx_interval (bfd_main_t * bm, bfd_session_t * bs)
182{
183 bs->echo_transmit_interval_clocks =
184 clib_max (bs->effective_desired_min_tx_clocks,
185 bs->remote_min_echo_rx_clocks);
186 BFD_DBG ("Recalculated echo transmit interval " BFD_CLK_FMT,
187 BFD_CLK_PRN (bs->echo_transmit_interval_clocks));
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200188}
189
190static void
191bfd_calc_next_tx (bfd_main_t * bm, bfd_session_t * bs, u64 now)
192{
Klement Sekera239790f2017-02-16 10:53:53 +0100193 if (bs->local_detect_mult > 1)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200194 {
Klement Sekera239790f2017-02-16 10:53:53 +0100195 /* common case - 75-100% of transmit interval */
196 bs->tx_timeout_clocks = bs->last_tx_clocks +
197 (1 - .25 * (random_f64 (&bm->random_seed))) *
198 bs->transmit_interval_clocks;
199 if (bs->tx_timeout_clocks < now)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200200 {
Klement Sekera239790f2017-02-16 10:53:53 +0100201 /*
202 * the timeout is in the past, which means that either remote
203 * demand mode was set or performance/clock issues ...
204 */
205 BFD_DBG ("Missed %lu transmit events (now is %lu, calc "
206 "tx_timeout is %lu)",
207 (now - bs->tx_timeout_clocks) /
208 bs->transmit_interval_clocks, now, bs->tx_timeout_clocks);
209 bs->tx_timeout_clocks = now;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200210 }
211 }
212 else
213 {
Klement Sekera239790f2017-02-16 10:53:53 +0100214 /* special case - 75-90% of transmit interval */
215 bs->tx_timeout_clocks = bs->last_tx_clocks +
216 (.9 - .15 * (random_f64 (&bm->random_seed))) *
217 bs->transmit_interval_clocks;
218 if (bs->tx_timeout_clocks < now)
219 {
220 /*
221 * the timeout is in the past, which means that either remote
222 * demand mode was set or performance/clock issues ...
223 */
224 BFD_DBG ("Missed %lu transmit events (now is %lu, calc "
225 "tx_timeout is %lu)",
226 (now - bs->tx_timeout_clocks) /
227 bs->transmit_interval_clocks, now, bs->tx_timeout_clocks);
228 bs->tx_timeout_clocks = now;
229 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200230 }
231 if (bs->tx_timeout_clocks)
232 {
233 BFD_DBG ("Next transmit in %lu clocks/%.02fs@%lu",
234 bs->tx_timeout_clocks - now,
235 (bs->tx_timeout_clocks - now) / bm->cpu_cps,
236 bs->tx_timeout_clocks);
237 }
238}
239
240static void
Klement Sekera239790f2017-02-16 10:53:53 +0100241bfd_calc_next_echo_tx (bfd_main_t * bm, bfd_session_t * bs, u64 now)
242{
243 bs->echo_tx_timeout_clocks =
244 bs->echo_last_tx_clocks + bs->echo_transmit_interval_clocks;
245 if (bs->echo_tx_timeout_clocks < now)
246 {
247 /* huh, we've missed it already, transmit now */
248 BFD_DBG ("Missed %lu echo transmit events (now is %lu, calc tx_timeout "
249 "is %lu)",
250 (now - bs->echo_tx_timeout_clocks) /
251 bs->echo_transmit_interval_clocks,
252 now, bs->echo_tx_timeout_clocks);
253 bs->echo_tx_timeout_clocks = now;
254 }
255 BFD_DBG ("Next echo transmit in %lu clocks/%.02fs@%lu",
256 bs->echo_tx_timeout_clocks - now,
257 (bs->echo_tx_timeout_clocks - now) / bm->cpu_cps,
258 bs->echo_tx_timeout_clocks);
259}
260
261static void
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200262bfd_recalc_detection_time (bfd_main_t * bm, bfd_session_t * bs)
263{
Klement Sekera73884482017-02-23 09:26:30 +0100264 if (bs->local_state == BFD_STATE_init || bs->local_state == BFD_STATE_up)
265 {
266 bs->detection_time_clocks =
267 bs->remote_detect_mult *
268 clib_max (bs->effective_required_min_rx_clocks,
269 bs->remote_desired_min_tx_clocks);
270 BFD_DBG ("Recalculated detection time %lu clocks/%.2fs",
271 bs->detection_time_clocks,
272 bs->detection_time_clocks / bm->cpu_cps);
273 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200274}
275
276static void
Klement Sekerae4504c62016-12-08 10:16:41 +0100277bfd_set_timer (bfd_main_t * bm, bfd_session_t * bs, u64 now,
278 int handling_wakeup)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200279{
280 u64 next = 0;
281 u64 rx_timeout = 0;
Klement Sekera239790f2017-02-16 10:53:53 +0100282 u64 tx_timeout = 0;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200283 if (BFD_STATE_up == bs->local_state)
284 {
285 rx_timeout = bs->last_rx_clocks + bs->detection_time_clocks;
286 }
Klement Sekera73884482017-02-23 09:26:30 +0100287 if (BFD_STATE_up != bs->local_state ||
288 (!bs->remote_demand && bs->remote_min_rx_usec) ||
Klement Sekera239790f2017-02-16 10:53:53 +0100289 BFD_POLL_NOT_NEEDED != bs->poll_state)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200290 {
Klement Sekera239790f2017-02-16 10:53:53 +0100291 tx_timeout = bs->tx_timeout_clocks;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200292 }
Klement Sekera239790f2017-02-16 10:53:53 +0100293 if (tx_timeout && rx_timeout)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200294 {
Klement Sekera239790f2017-02-16 10:53:53 +0100295 next = clib_min (tx_timeout, rx_timeout);
296 }
297 else if (tx_timeout)
298 {
299 next = tx_timeout;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200300 }
301 else if (rx_timeout)
302 {
303 next = rx_timeout;
304 }
Klement Sekera239790f2017-02-16 10:53:53 +0100305 if (bs->echo && next > bs->echo_tx_timeout_clocks)
306 {
307 next = bs->echo_tx_timeout_clocks;
308 }
309 BFD_DBG ("bs_idx=%u, tx_timeout=%lu, echo_tx_timeout=%lu, rx_timeout=%lu, "
310 "next=%s",
311 bs->bs_idx, tx_timeout, bs->echo_tx_timeout_clocks, rx_timeout,
312 next == tx_timeout
313 ? "tx" : (next == bs->echo_tx_timeout_clocks ? "echo tx" : "rx"));
Klement Sekera637b9c42016-12-08 05:19:14 +0100314 /* sometimes the wheel expires an event a bit sooner than requested, account
315 for that here */
316 if (next && (now + bm->wheel_inaccuracy > bs->wheel_time_clocks ||
317 next < bs->wheel_time_clocks || !bs->wheel_time_clocks))
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200318 {
Dave Barach1e3417f2018-07-25 08:30:27 -0400319 int send_signal = 0;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200320 bs->wheel_time_clocks = next;
321 BFD_DBG ("timing_wheel_insert(%p, %lu (%ld clocks/%.2fs in the "
322 "future), %u);",
323 &bm->wheel, bs->wheel_time_clocks,
324 (i64) bs->wheel_time_clocks - clib_cpu_time_now (),
325 (i64) (bs->wheel_time_clocks - clib_cpu_time_now ()) /
326 bm->cpu_cps, bs->bs_idx);
Dave Barach1e3417f2018-07-25 08:30:27 -0400327 bfd_lock (bm);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200328 timing_wheel_insert (&bm->wheel, bs->wheel_time_clocks, bs->bs_idx);
Dave Barach1e3417f2018-07-25 08:30:27 -0400329
Klement Sekerae4504c62016-12-08 10:16:41 +0100330 if (!handling_wakeup)
331 {
Dave Barach1e3417f2018-07-25 08:30:27 -0400332
333 /* Send only if it is earlier than current awaited wakeup time */
334 send_signal =
335 (bs->wheel_time_clocks < bm->bfd_process_next_wakeup_clocks) &&
336 /*
337 * If the wake-up time is within 2x the delay of the event propagation delay,
338 * avoid the expense of sending the event. The 2x multiplier is to workaround the race whereby
339 * simultaneous event + expired timer create one recurring bogus wakeup/suspend instance,
340 * due to double scheduling of the node on the pending list.
341 */
342 (bm->bfd_process_next_wakeup_clocks - bs->wheel_time_clocks >
343 2 * bm->bfd_process_wakeup_event_delay_clocks) &&
344 /* Must be no events in flight to send an event */
345 (!bm->bfd_process_wakeup_events_in_flight);
346
347 /* If we do send the signal, note this down along with the start timestamp */
348 if (send_signal)
349 {
350 bm->bfd_process_wakeup_events_in_flight++;
351 bm->bfd_process_wakeup_event_start_clocks = now;
352 }
353 }
354 bfd_unlock (bm);
355
356 /* Use the multithreaded event sending so the workers can send events too */
357 if (send_signal)
358 {
359 vlib_process_signal_event_mt (bm->vlib_main,
360 bm->bfd_process_node_index,
361 BFD_EVENT_RESCHEDULE, ~0);
Klement Sekerae4504c62016-12-08 10:16:41 +0100362 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200363 }
364}
365
366static void
Klement Sekeraa57a9702017-02-02 06:58:07 +0100367bfd_set_effective_desired_min_tx (bfd_main_t * bm,
368 bfd_session_t * bs, u64 now,
Klement Sekeraaeeac3b2017-02-14 07:11:52 +0100369 u64 desired_min_tx_clocks)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200370{
Klement Sekeraa57a9702017-02-02 06:58:07 +0100371 bs->effective_desired_min_tx_clocks = desired_min_tx_clocks;
372 BFD_DBG ("Set effective desired min tx to " BFD_CLK_FMT,
373 BFD_CLK_PRN (bs->effective_desired_min_tx_clocks));
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200374 bfd_recalc_detection_time (bm, bs);
375 bfd_recalc_tx_interval (bm, bs);
Klement Sekera239790f2017-02-16 10:53:53 +0100376 bfd_recalc_echo_tx_interval (bm, bs);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200377 bfd_calc_next_tx (bm, bs, now);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200378}
379
Klement Sekera637b9c42016-12-08 05:19:14 +0100380static void
Klement Sekeraa57a9702017-02-02 06:58:07 +0100381bfd_set_effective_required_min_rx (bfd_main_t * bm,
Klement Sekera73884482017-02-23 09:26:30 +0100382 bfd_session_t * bs,
Klement Sekeraaeeac3b2017-02-14 07:11:52 +0100383 u64 required_min_rx_clocks)
Klement Sekeraa57a9702017-02-02 06:58:07 +0100384{
385 bs->effective_required_min_rx_clocks = required_min_rx_clocks;
386 BFD_DBG ("Set effective required min rx to " BFD_CLK_FMT,
387 BFD_CLK_PRN (bs->effective_required_min_rx_clocks));
388 bfd_recalc_detection_time (bm, bs);
Klement Sekeraa57a9702017-02-02 06:58:07 +0100389}
390
391static void
Klement Sekera637b9c42016-12-08 05:19:14 +0100392bfd_set_remote_required_min_rx (bfd_main_t * bm, bfd_session_t * bs,
Klement Sekera239790f2017-02-16 10:53:53 +0100393 u64 now, u32 remote_required_min_rx_usec)
Klement Sekera637b9c42016-12-08 05:19:14 +0100394{
Klement Sekera239790f2017-02-16 10:53:53 +0100395 if (bs->remote_min_rx_usec != remote_required_min_rx_usec)
396 {
397 bs->remote_min_rx_usec = remote_required_min_rx_usec;
398 bs->remote_min_rx_clocks =
399 bfd_usec_to_clocks (bm, remote_required_min_rx_usec);
400 BFD_DBG ("Set remote min rx to " BFD_CLK_FMT,
401 BFD_CLK_PRN (bs->remote_min_rx_clocks));
402 bfd_recalc_detection_time (bm, bs);
403 bfd_recalc_tx_interval (bm, bs);
404 }
405}
406
407static void
408bfd_set_remote_required_min_echo_rx (bfd_main_t * bm, bfd_session_t * bs,
409 u64 now,
410 u32 remote_required_min_echo_rx_usec)
411{
412 if (bs->remote_min_echo_rx_usec != remote_required_min_echo_rx_usec)
413 {
414 bs->remote_min_echo_rx_usec = remote_required_min_echo_rx_usec;
415 bs->remote_min_echo_rx_clocks =
416 bfd_usec_to_clocks (bm, bs->remote_min_echo_rx_usec);
417 BFD_DBG ("Set remote min echo rx to " BFD_CLK_FMT,
418 BFD_CLK_PRN (bs->remote_min_echo_rx_clocks));
419 bfd_recalc_echo_tx_interval (bm, bs);
420 }
Klement Sekera637b9c42016-12-08 05:19:14 +0100421}
422
Neale Ranns88fc83e2017-04-05 08:11:14 -0700423static void
424bfd_notify_listeners (bfd_main_t * bm,
425 bfd_listen_event_e event, const bfd_session_t * bs)
426{
427 bfd_notify_fn_t *fn;
428 vec_foreach (fn, bm->listeners)
429 {
430 (*fn) (event, bs);
431 }
432}
433
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200434void
435bfd_session_start (bfd_main_t * bm, bfd_session_t * bs)
436{
Klement Sekera239790f2017-02-16 10:53:53 +0100437 BFD_DBG ("\nStarting session: %U", format_bfd_session, bs);
Damjan Marion07a38572018-01-21 06:44:18 -0800438 vlib_log_info (bm->log_class, "start BFD session: %U",
439 format_bfd_session_brief, bs);
Klement Sekera73884482017-02-23 09:26:30 +0100440 bfd_set_effective_required_min_rx (bm, bs,
441 bs->config_required_min_rx_clocks);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200442 bfd_recalc_tx_interval (bm, bs);
443 vlib_process_signal_event (bm->vlib_main, bm->bfd_process_node_index,
444 BFD_EVENT_NEW_SESSION, bs->bs_idx);
Neale Ranns88fc83e2017-04-05 08:11:14 -0700445 bfd_notify_listeners (bm, BFD_LISTEN_EVENT_CREATE, bs);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200446}
447
Klement Sekerab17dd962017-01-09 07:43:48 +0100448void
449bfd_session_set_flags (bfd_session_t * bs, u8 admin_up_down)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200450{
451 bfd_main_t *bm = &bfd_main;
Klement Sekera73884482017-02-23 09:26:30 +0100452 u64 now = clib_cpu_time_now ();
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200453 if (admin_up_down)
454 {
Klement Sekerac48829b2017-02-14 07:55:57 +0100455 BFD_DBG ("Session set admin-up, bs-idx=%u", bs->bs_idx);
Damjan Marion07a38572018-01-21 06:44:18 -0800456 vlib_log_info (bm->log_class, "set session admin-up: %U",
457 format_bfd_session_brief, bs);
Klement Sekerae4504c62016-12-08 10:16:41 +0100458 bfd_set_state (bm, bs, BFD_STATE_down, 0);
Klement Sekerac48829b2017-02-14 07:55:57 +0100459 bfd_set_diag (bs, BFD_DIAG_CODE_no_diag);
Klement Sekera73884482017-02-23 09:26:30 +0100460 bfd_calc_next_tx (bm, bs, now);
461 bfd_set_timer (bm, bs, now, 0);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200462 }
463 else
464 {
Klement Sekerac48829b2017-02-14 07:55:57 +0100465 BFD_DBG ("Session set admin-down, bs-idx=%u", bs->bs_idx);
Damjan Marion07a38572018-01-21 06:44:18 -0800466 vlib_log_info (bm->log_class, "set session admin-down: %U",
467 format_bfd_session_brief, bs);
Klement Sekerac48829b2017-02-14 07:55:57 +0100468 bfd_set_diag (bs, BFD_DIAG_CODE_admin_down);
Klement Sekerae4504c62016-12-08 10:16:41 +0100469 bfd_set_state (bm, bs, BFD_STATE_admin_down, 0);
Klement Sekera73884482017-02-23 09:26:30 +0100470 bfd_calc_next_tx (bm, bs, now);
471 bfd_set_timer (bm, bs, now, 0);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200472 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200473}
474
475u8 *
476bfd_input_format_trace (u8 * s, va_list * args)
477{
478 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
479 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
480 const bfd_input_trace_t *t = va_arg (*args, bfd_input_trace_t *);
481 const bfd_pkt_t *pkt = (bfd_pkt_t *) t->data;
482 if (t->len > STRUCT_SIZE_OF (bfd_pkt_t, head))
483 {
484 s = format (s, "BFD v%u, diag=%u(%s), state=%u(%s),\n"
Klement Sekerab17dd962017-01-09 07:43:48 +0100485 " flags=(P:%u, F:%u, C:%u, A:%u, D:%u, M:%u), "
486 "detect_mult=%u, length=%u\n",
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200487 bfd_pkt_get_version (pkt), bfd_pkt_get_diag_code (pkt),
488 bfd_diag_code_string (bfd_pkt_get_diag_code (pkt)),
489 bfd_pkt_get_state (pkt),
490 bfd_state_string (bfd_pkt_get_state (pkt)),
491 bfd_pkt_get_poll (pkt), bfd_pkt_get_final (pkt),
492 bfd_pkt_get_control_plane_independent (pkt),
493 bfd_pkt_get_auth_present (pkt), bfd_pkt_get_demand (pkt),
494 bfd_pkt_get_multipoint (pkt), pkt->head.detect_mult,
495 pkt->head.length);
Klement Sekerab17dd962017-01-09 07:43:48 +0100496 if (t->len >= sizeof (bfd_pkt_t) &&
497 pkt->head.length >= sizeof (bfd_pkt_t))
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200498 {
Klement Sekerad3ba5152017-02-14 03:09:17 +0100499 s = format (s, " my discriminator: %u\n",
500 clib_net_to_host_u32 (pkt->my_disc));
501 s = format (s, " your discriminator: %u\n",
502 clib_net_to_host_u32 (pkt->your_disc));
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200503 s = format (s, " desired min tx interval: %u\n",
504 clib_net_to_host_u32 (pkt->des_min_tx));
505 s = format (s, " required min rx interval: %u\n",
506 clib_net_to_host_u32 (pkt->req_min_rx));
Klement Sekera46a87ad2017-01-02 08:22:23 +0100507 s = format (s, " required min echo rx interval: %u",
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200508 clib_net_to_host_u32 (pkt->req_min_echo_rx));
509 }
Klement Sekerad3ba5152017-02-14 03:09:17 +0100510 if (t->len >= sizeof (bfd_pkt_with_common_auth_t) &&
511 pkt->head.length >= sizeof (bfd_pkt_with_common_auth_t) &&
512 bfd_pkt_get_auth_present (pkt))
513 {
514 const bfd_pkt_with_common_auth_t *with_auth = (void *) pkt;
515 const bfd_auth_common_t *common = &with_auth->common_auth;
516 s = format (s, "\n auth len: %u\n", common->len);
517 s = format (s, " auth type: %u:%s\n", common->type,
518 bfd_auth_type_str (common->type));
519 if (t->len >= sizeof (bfd_pkt_with_sha1_auth_t) &&
520 pkt->head.length >= sizeof (bfd_pkt_with_sha1_auth_t) &&
521 (BFD_AUTH_TYPE_keyed_sha1 == common->type ||
522 BFD_AUTH_TYPE_meticulous_keyed_sha1 == common->type))
523 {
524 const bfd_pkt_with_sha1_auth_t *with_sha1 = (void *) pkt;
525 const bfd_auth_sha1_t *sha1 = &with_sha1->sha1_auth;
526 s = format (s, " seq num: %u\n",
527 clib_net_to_host_u32 (sha1->seq_num));
528 s = format (s, " key id: %u\n", sha1->key_id);
529 s = format (s, " hash: %U", format_hex_bytes, sha1->hash,
530 sizeof (sha1->hash));
531 }
532 }
533 else
534 {
535 s = format (s, "\n");
536 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200537 }
538
539 return s;
540}
541
Dave Barach1e3417f2018-07-25 08:30:27 -0400542typedef struct
543{
544 u32 bs_idx;
545} bfd_rpc_event_t;
546
547static void
548bfd_rpc_event_cb (const bfd_rpc_event_t * a)
549{
550 bfd_main_t *bm = &bfd_main;
551 u32 bs_idx = a->bs_idx;
552 u32 valid_bs = 0;
553 bfd_session_t session_data;
554
555 bfd_lock (bm);
556 if (!pool_is_free_index (bm->sessions, bs_idx))
557 {
558 bfd_session_t *bs = pool_elt_at_index (bm->sessions, bs_idx);
559 clib_memcpy (&session_data, bs, sizeof (bfd_session_t));
560 valid_bs = 1;
561 }
562 else
563 {
564 BFD_DBG ("Ignoring event RPC for non-existent session index %u",
565 bs_idx);
566 }
567 bfd_unlock (bm);
568
569 if (valid_bs)
570 bfd_event (bm, &session_data);
571}
572
573static void
574bfd_event_rpc (u32 bs_idx)
575{
576 const u32 data_size = sizeof (bfd_rpc_event_t);
577 u8 data[data_size];
578 bfd_rpc_event_t *event = (bfd_rpc_event_t *) data;
579
580 event->bs_idx = bs_idx;
581 vl_api_rpc_call_main_thread (bfd_rpc_event_cb, data, data_size);
582}
583
584typedef struct
585{
586 u32 bs_idx;
587} bfd_rpc_notify_listeners_t;
588
589static void
590bfd_rpc_notify_listeners_cb (const bfd_rpc_notify_listeners_t * a)
591{
592 bfd_main_t *bm = &bfd_main;
593 u32 bs_idx = a->bs_idx;
594 bfd_lock (bm);
595 if (!pool_is_free_index (bm->sessions, bs_idx))
596 {
597 bfd_session_t *bs = pool_elt_at_index (bm->sessions, bs_idx);
598 bfd_notify_listeners (bm, BFD_LISTEN_EVENT_UPDATE, bs);
599 }
600 else
601 {
602 BFD_DBG ("Ignoring notify RPC for non-existent session index %u",
603 bs_idx);
604 }
605 bfd_unlock (bm);
606}
607
608static void
609bfd_notify_listeners_rpc (u32 bs_idx)
610{
611 const u32 data_size = sizeof (bfd_rpc_notify_listeners_t);
612 u8 data[data_size];
613 bfd_rpc_notify_listeners_t *notify = (bfd_rpc_notify_listeners_t *) data;
614 notify->bs_idx = bs_idx;
615 vl_api_rpc_call_main_thread (bfd_rpc_notify_listeners_cb, data, data_size);
616}
617
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200618static void
Klement Sekerae4504c62016-12-08 10:16:41 +0100619bfd_on_state_change (bfd_main_t * bm, bfd_session_t * bs, u64 now,
620 int handling_wakeup)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200621{
Klement Sekera239790f2017-02-16 10:53:53 +0100622 BFD_DBG ("\nState changed: %U", format_bfd_session, bs);
Dave Barach1e3417f2018-07-25 08:30:27 -0400623
624 if (vlib_get_thread_index () == 0)
625 {
626 bfd_event (bm, bs);
627 }
628 else
629 {
630 /* without RPC - a REGRESSION: BFD event are not propagated */
631 bfd_event_rpc (bs->bs_idx);
632 }
633
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200634 switch (bs->local_state)
635 {
636 case BFD_STATE_admin_down:
Klement Sekera239790f2017-02-16 10:53:53 +0100637 bs->echo = 0;
Klement Sekeraa57a9702017-02-02 06:58:07 +0100638 bfd_set_effective_desired_min_tx (bm, bs, now,
639 clib_max
640 (bs->config_desired_min_tx_clocks,
Klement Sekeraaeeac3b2017-02-14 07:11:52 +0100641 bm->default_desired_min_tx_clocks));
Klement Sekera73884482017-02-23 09:26:30 +0100642 bfd_set_effective_required_min_rx (bm, bs,
Klement Sekeraaeeac3b2017-02-14 07:11:52 +0100643 bs->config_required_min_rx_clocks);
644 bfd_set_timer (bm, bs, now, handling_wakeup);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200645 break;
646 case BFD_STATE_down:
Klement Sekera239790f2017-02-16 10:53:53 +0100647 bs->echo = 0;
Klement Sekeraa57a9702017-02-02 06:58:07 +0100648 bfd_set_effective_desired_min_tx (bm, bs, now,
649 clib_max
650 (bs->config_desired_min_tx_clocks,
Klement Sekeraaeeac3b2017-02-14 07:11:52 +0100651 bm->default_desired_min_tx_clocks));
Klement Sekera73884482017-02-23 09:26:30 +0100652 bfd_set_effective_required_min_rx (bm, bs,
Klement Sekeraaeeac3b2017-02-14 07:11:52 +0100653 bs->config_required_min_rx_clocks);
654 bfd_set_timer (bm, bs, now, handling_wakeup);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200655 break;
656 case BFD_STATE_init:
Klement Sekera239790f2017-02-16 10:53:53 +0100657 bs->echo = 0;
Klement Sekeraa57a9702017-02-02 06:58:07 +0100658 bfd_set_effective_desired_min_tx (bm, bs, now,
Klement Sekerac48829b2017-02-14 07:55:57 +0100659 bs->config_desired_min_tx_clocks);
Klement Sekeraaeeac3b2017-02-14 07:11:52 +0100660 bfd_set_timer (bm, bs, now, handling_wakeup);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200661 break;
662 case BFD_STATE_up:
Klement Sekeraaeeac3b2017-02-14 07:11:52 +0100663 bfd_set_effective_desired_min_tx (bm, bs, now,
664 bs->config_desired_min_tx_clocks);
Klement Sekera239790f2017-02-16 10:53:53 +0100665 if (BFD_POLL_NOT_NEEDED == bs->poll_state)
Klement Sekeraa57a9702017-02-02 06:58:07 +0100666 {
Klement Sekera73884482017-02-23 09:26:30 +0100667 bfd_set_effective_required_min_rx (bm, bs,
Klement Sekeraaeeac3b2017-02-14 07:11:52 +0100668 bs->config_required_min_rx_clocks);
Klement Sekeraa57a9702017-02-02 06:58:07 +0100669 }
Klement Sekeraaeeac3b2017-02-14 07:11:52 +0100670 bfd_set_timer (bm, bs, now, handling_wakeup);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200671 break;
672 }
Dave Barach1e3417f2018-07-25 08:30:27 -0400673 if (vlib_get_thread_index () == 0)
674 {
675 bfd_notify_listeners (bm, BFD_LISTEN_EVENT_UPDATE, bs);
676 }
677 else
678 {
679 /* without RPC - a REGRESSION: state changes are not propagated */
680 bfd_notify_listeners_rpc (bs->bs_idx);
681 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200682}
683
684static void
Klement Sekeraa57a9702017-02-02 06:58:07 +0100685bfd_on_config_change (vlib_main_t * vm, vlib_node_runtime_t * rt,
686 bfd_main_t * bm, bfd_session_t * bs, u64 now)
687{
Klement Sekera239790f2017-02-16 10:53:53 +0100688 /*
689 * if remote demand mode is set and we need to do a poll, set the next
690 * timeout so that the session wakes up immediately
691 */
692 if (bs->remote_demand && BFD_POLL_NEEDED == bs->poll_state &&
693 bs->poll_state_start_or_timeout_clocks < now)
Klement Sekeraa57a9702017-02-02 06:58:07 +0100694 {
Klement Sekera239790f2017-02-16 10:53:53 +0100695 bs->tx_timeout_clocks = now;
Klement Sekeraa57a9702017-02-02 06:58:07 +0100696 }
697 bfd_recalc_detection_time (bm, bs);
698 bfd_set_timer (bm, bs, now, 0);
699}
700
701static void
Klement Sekerae50e8562017-04-04 16:19:48 +0200702bfd_add_transport_layer (vlib_main_t * vm, u32 bi, bfd_session_t * bs)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200703{
704 switch (bs->transport)
705 {
706 case BFD_TRANSPORT_UDP4:
Klement Sekera46a87ad2017-01-02 08:22:23 +0100707 BFD_DBG ("Transport bfd via udp4, bs_idx=%u", bs->bs_idx);
Klement Sekerae50e8562017-04-04 16:19:48 +0200708 bfd_add_udp4_transport (vm, bi, bs, 0 /* is_echo */ );
Klement Sekera46a87ad2017-01-02 08:22:23 +0100709 break;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200710 case BFD_TRANSPORT_UDP6:
Klement Sekera46a87ad2017-01-02 08:22:23 +0100711 BFD_DBG ("Transport bfd via udp6, bs_idx=%u", bs->bs_idx);
Klement Sekerae50e8562017-04-04 16:19:48 +0200712 bfd_add_udp6_transport (vm, bi, bs, 0 /* is_echo */ );
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200713 break;
714 }
715}
716
Klement Sekera239790f2017-02-16 10:53:53 +0100717static int
Klement Sekerae50e8562017-04-04 16:19:48 +0200718bfd_transport_control_frame (vlib_main_t * vm, u32 bi, bfd_session_t * bs)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200719{
Klement Sekera239790f2017-02-16 10:53:53 +0100720 switch (bs->transport)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200721 {
Klement Sekera239790f2017-02-16 10:53:53 +0100722 case BFD_TRANSPORT_UDP4:
Klement Sekerae50e8562017-04-04 16:19:48 +0200723 BFD_DBG ("Transport bfd via udp4, bs_idx=%u", bs->bs_idx);
724 return bfd_transport_udp4 (vm, bi, bs);
Klement Sekera239790f2017-02-16 10:53:53 +0100725 break;
726 case BFD_TRANSPORT_UDP6:
Klement Sekerae50e8562017-04-04 16:19:48 +0200727 BFD_DBG ("Transport bfd via udp6, bs_idx=%u", bs->bs_idx);
728 return bfd_transport_udp6 (vm, bi, bs);
Klement Sekera239790f2017-02-16 10:53:53 +0100729 break;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200730 }
Klement Sekera239790f2017-02-16 10:53:53 +0100731 return 0;
732}
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200733
Klement Sekerae50e8562017-04-04 16:19:48 +0200734static int
735bfd_echo_add_transport_layer (vlib_main_t * vm, u32 bi, bfd_session_t * bs)
Klement Sekera239790f2017-02-16 10:53:53 +0100736{
Klement Sekerae50e8562017-04-04 16:19:48 +0200737 switch (bs->transport)
738 {
739 case BFD_TRANSPORT_UDP4:
740 BFD_DBG ("Transport bfd echo via udp4, bs_idx=%u", bs->bs_idx);
741 return bfd_add_udp4_transport (vm, bi, bs, 1 /* is_echo */ );
742 break;
743 case BFD_TRANSPORT_UDP6:
744 BFD_DBG ("Transport bfd echo via udp6, bs_idx=%u", bs->bs_idx);
745 return bfd_add_udp6_transport (vm, bi, bs, 1 /* is_echo */ );
746 break;
747 }
748 return 0;
749}
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200750
Klement Sekerae50e8562017-04-04 16:19:48 +0200751static int
752bfd_transport_echo (vlib_main_t * vm, u32 bi, bfd_session_t * bs)
753{
754 switch (bs->transport)
755 {
756 case BFD_TRANSPORT_UDP4:
757 BFD_DBG ("Transport bfd echo via udp4, bs_idx=%u", bs->bs_idx);
758 return bfd_transport_udp4 (vm, bi, bs);
759 break;
760 case BFD_TRANSPORT_UDP6:
761 BFD_DBG ("Transport bfd echo via udp6, bs_idx=%u", bs->bs_idx);
762 return bfd_transport_udp6 (vm, bi, bs);
763 break;
764 }
765 return 0;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200766}
767
Klement Sekerab17dd962017-01-09 07:43:48 +0100768#if WITH_LIBSSL > 0
769static void
770bfd_add_sha1_auth_section (vlib_buffer_t * b, bfd_session_t * bs)
771{
772 bfd_pkt_with_sha1_auth_t *pkt = vlib_buffer_get_current (b);
773 bfd_auth_sha1_t *auth = &pkt->sha1_auth;
774 b->current_length += sizeof (*auth);
775 pkt->pkt.head.length += sizeof (*auth);
776 bfd_pkt_set_auth_present (&pkt->pkt);
Dave Barachb7b92992018-10-17 10:38:51 -0400777 clib_memset (auth, 0, sizeof (*auth));
Klement Sekerab17dd962017-01-09 07:43:48 +0100778 auth->type_len.type = bs->auth.curr_key->auth_type;
779 /*
780 * only meticulous authentication types require incrementing seq number
781 * for every message, but doing so doesn't violate the RFC
782 */
783 ++bs->auth.local_seq_number;
784 auth->type_len.len = sizeof (bfd_auth_sha1_t);
785 auth->key_id = bs->auth.curr_bfd_key_id;
786 auth->seq_num = clib_host_to_net_u32 (bs->auth.local_seq_number);
787 /*
788 * first copy the password into the packet, then calculate the hash
789 * and finally replace the password with the calculated hash
790 */
791 clib_memcpy (auth->hash, bs->auth.curr_key->key,
792 sizeof (bs->auth.curr_key->key));
793 unsigned char hash[sizeof (auth->hash)];
794 SHA1 ((unsigned char *) pkt, sizeof (*pkt), hash);
795 BFD_DBG ("hashing: %U", format_hex_bytes, pkt, sizeof (*pkt));
796 clib_memcpy (auth->hash, hash, sizeof (hash));
Klement Sekerab17dd962017-01-09 07:43:48 +0100797}
Klement Sekera6f966492017-02-08 07:42:08 +0100798#endif
Klement Sekerab17dd962017-01-09 07:43:48 +0100799
800static void
801bfd_add_auth_section (vlib_buffer_t * b, bfd_session_t * bs)
802{
Damjan Marion07a38572018-01-21 06:44:18 -0800803 bfd_main_t *bm = &bfd_main;
Klement Sekerab17dd962017-01-09 07:43:48 +0100804 if (bs->auth.curr_key)
805 {
806 const bfd_auth_type_e auth_type = bs->auth.curr_key->auth_type;
807 switch (auth_type)
808 {
809 case BFD_AUTH_TYPE_reserved:
810 /* fallthrough */
811 case BFD_AUTH_TYPE_simple_password:
812 /* fallthrough */
813 case BFD_AUTH_TYPE_keyed_md5:
814 /* fallthrough */
815 case BFD_AUTH_TYPE_meticulous_keyed_md5:
Damjan Marion07a38572018-01-21 06:44:18 -0800816 vlib_log_crit (bm->log_class,
817 "internal error, unexpected BFD auth type '%d'",
818 auth_type);
Klement Sekerab17dd962017-01-09 07:43:48 +0100819 break;
820#if WITH_LIBSSL > 0
821 case BFD_AUTH_TYPE_keyed_sha1:
822 /* fallthrough */
823 case BFD_AUTH_TYPE_meticulous_keyed_sha1:
824 bfd_add_sha1_auth_section (b, bs);
825 break;
826#else
827 case BFD_AUTH_TYPE_keyed_sha1:
828 /* fallthrough */
829 case BFD_AUTH_TYPE_meticulous_keyed_sha1:
Damjan Marion07a38572018-01-21 06:44:18 -0800830 vlib_log_crit (bm->log_class,
831 "internal error, unexpected BFD auth type '%d'",
832 auth_type);
Klement Sekerab17dd962017-01-09 07:43:48 +0100833 break;
834#endif
835 }
836 }
837}
838
Klement Sekera239790f2017-02-16 10:53:53 +0100839static int
840bfd_is_echo_possible (bfd_session_t * bs)
841{
842 if (BFD_STATE_up == bs->local_state && BFD_STATE_up == bs->remote_state &&
843 bs->remote_min_echo_rx_usec > 0)
844 {
845 switch (bs->transport)
846 {
847 case BFD_TRANSPORT_UDP4:
848 return bfd_udp_is_echo_available (BFD_TRANSPORT_UDP4);
849 case BFD_TRANSPORT_UDP6:
850 return bfd_udp_is_echo_available (BFD_TRANSPORT_UDP6);
851 }
852 }
853 return 0;
854}
855
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200856static void
Klement Sekera239790f2017-02-16 10:53:53 +0100857bfd_init_control_frame (bfd_main_t * bm, bfd_session_t * bs,
858 vlib_buffer_t * b)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200859{
860 bfd_pkt_t *pkt = vlib_buffer_get_current (b);
Klement Sekerab17dd962017-01-09 07:43:48 +0100861 u32 bfd_length = 0;
862 bfd_length = sizeof (bfd_pkt_t);
Dave Barachb7b92992018-10-17 10:38:51 -0400863 clib_memset (pkt, 0, sizeof (*pkt));
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200864 bfd_pkt_set_version (pkt, 1);
865 bfd_pkt_set_diag_code (pkt, bs->local_diag);
866 bfd_pkt_set_state (pkt, bs->local_state);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200867 pkt->head.detect_mult = bs->local_detect_mult;
Klement Sekerae50e8562017-04-04 16:19:48 +0200868 pkt->head.length = bfd_length;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200869 pkt->my_disc = bs->local_discr;
870 pkt->your_disc = bs->remote_discr;
Klement Sekeraa57a9702017-02-02 06:58:07 +0100871 pkt->des_min_tx = clib_host_to_net_u32 (bs->config_desired_min_tx_usec);
Klement Sekera239790f2017-02-16 10:53:53 +0100872 if (bs->echo)
873 {
874 pkt->req_min_rx =
875 clib_host_to_net_u32 (bfd_clocks_to_usec
876 (bm, bs->effective_required_min_rx_clocks));
877 }
878 else
879 {
880 pkt->req_min_rx =
881 clib_host_to_net_u32 (bs->config_required_min_rx_usec);
882 }
Klement Sekeraa57a9702017-02-02 06:58:07 +0100883 pkt->req_min_echo_rx = clib_host_to_net_u32 (1);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200884 b->current_length = bfd_length;
885}
886
887static void
Klement Sekera239790f2017-02-16 10:53:53 +0100888bfd_send_echo (vlib_main_t * vm, vlib_node_runtime_t * rt,
Klement Sekerae50e8562017-04-04 16:19:48 +0200889 bfd_main_t * bm, bfd_session_t * bs, u64 now)
Klement Sekera239790f2017-02-16 10:53:53 +0100890{
891 if (!bfd_is_echo_possible (bs))
892 {
893 BFD_DBG ("\nSwitching off echo function: %U", format_bfd_session, bs);
894 bs->echo = 0;
895 return;
896 }
Klement Sekerae50e8562017-04-04 16:19:48 +0200897 /* sometimes the wheel expires an event a bit sooner than requested,
898 account
Klement Sekera239790f2017-02-16 10:53:53 +0100899 for that here */
900 if (now + bm->wheel_inaccuracy >= bs->echo_tx_timeout_clocks)
901 {
902 BFD_DBG ("\nSending echo packet: %U", format_bfd_session, bs);
903 u32 bi;
904 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
905 {
Damjan Marion07a38572018-01-21 06:44:18 -0800906 vlib_log_crit (bm->log_class, "buffer allocation failure");
Klement Sekera239790f2017-02-16 10:53:53 +0100907 return;
908 }
909 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
910 ASSERT (b->current_data == 0);
Klement Sekerae50e8562017-04-04 16:19:48 +0200911 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b);
Klement Sekera239790f2017-02-16 10:53:53 +0100912 bfd_echo_pkt_t *pkt = vlib_buffer_get_current (b);
Dave Barachb7b92992018-10-17 10:38:51 -0400913 clib_memset (pkt, 0, sizeof (*pkt));
Klement Sekera239790f2017-02-16 10:53:53 +0100914 pkt->discriminator = bs->local_discr;
915 pkt->expire_time_clocks =
916 now + bs->echo_transmit_interval_clocks * bs->local_detect_mult;
917 pkt->checksum =
918 bfd_calc_echo_checksum (bs->local_discr, pkt->expire_time_clocks,
919 bs->echo_secret);
920 b->current_length = sizeof (*pkt);
Klement Sekerae50e8562017-04-04 16:19:48 +0200921 if (!bfd_echo_add_transport_layer (vm, bi, bs))
922 {
923 BFD_ERR ("cannot send echo packet out, turning echo off");
924 bs->echo = 0;
925 vlib_buffer_free_one (vm, bi);
926 return;
927 }
928 if (!bfd_transport_echo (vm, bi, bs))
Klement Sekera239790f2017-02-16 10:53:53 +0100929 {
930 BFD_ERR ("cannot send echo packet out, turning echo off");
931 bs->echo = 0;
932 vlib_buffer_free_one (vm, bi);
933 return;
934 }
935 bs->echo_last_tx_clocks = now;
936 bfd_calc_next_echo_tx (bm, bs, now);
Klement Sekera239790f2017-02-16 10:53:53 +0100937 }
938 else
939 {
940 BFD_DBG
941 ("No need to send echo packet now, now is %lu, tx_timeout is %lu",
942 now, bs->echo_tx_timeout_clocks);
943 }
944}
945
946static void
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200947bfd_send_periodic (vlib_main_t * vm, vlib_node_runtime_t * rt,
Klement Sekerae50e8562017-04-04 16:19:48 +0200948 bfd_main_t * bm, bfd_session_t * bs, u64 now)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200949{
Klement Sekera239790f2017-02-16 10:53:53 +0100950 if (!bs->remote_min_rx_usec && BFD_POLL_NOT_NEEDED == bs->poll_state)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200951 {
Klement Sekera239790f2017-02-16 10:53:53 +0100952 BFD_DBG ("Remote min rx interval is zero, not sending periodic control "
953 "frame");
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200954 return;
955 }
Klement Sekera239790f2017-02-16 10:53:53 +0100956 if (BFD_POLL_NOT_NEEDED == bs->poll_state && bs->remote_demand &&
Klement Sekerad3ba5152017-02-14 03:09:17 +0100957 BFD_STATE_up == bs->local_state && BFD_STATE_up == bs->remote_state)
958 {
959 /*
960 * A system MUST NOT periodically transmit BFD Control packets if Demand
961 * mode is active on the remote system (bfd.RemoteDemandMode is 1,
962 * bfd.SessionState is Up, and bfd.RemoteSessionState is Up) and a Poll
963 * Sequence is not being transmitted.
964 */
Klement Sekera239790f2017-02-16 10:53:53 +0100965 BFD_DBG ("Remote demand is set, not sending periodic control frame");
Klement Sekerad3ba5152017-02-14 03:09:17 +0100966 return;
967 }
Klement Sekerae50e8562017-04-04 16:19:48 +0200968 /*
969 * sometimes the wheel expires an event a bit sooner than requested, account
970 * for that here
971 */
Klement Sekera637b9c42016-12-08 05:19:14 +0100972 if (now + bm->wheel_inaccuracy >= bs->tx_timeout_clocks)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200973 {
Klement Sekera239790f2017-02-16 10:53:53 +0100974 BFD_DBG ("\nSending periodic control frame: %U", format_bfd_session,
975 bs);
976 u32 bi;
977 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200978 {
Damjan Marion07a38572018-01-21 06:44:18 -0800979 vlib_log_crit (bm->log_class, "buffer allocation failure");
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200980 return;
981 }
Klement Sekera239790f2017-02-16 10:53:53 +0100982 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
983 ASSERT (b->current_data == 0);
Klement Sekerae50e8562017-04-04 16:19:48 +0200984 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b);
Klement Sekera239790f2017-02-16 10:53:53 +0100985 bfd_init_control_frame (bm, bs, b);
986 switch (bs->poll_state)
Klement Sekeraa57a9702017-02-02 06:58:07 +0100987 {
Klement Sekera239790f2017-02-16 10:53:53 +0100988 case BFD_POLL_NEEDED:
989 if (now < bs->poll_state_start_or_timeout_clocks)
990 {
991 BFD_DBG ("Cannot start a poll sequence yet, need to wait "
992 "for " BFD_CLK_FMT,
993 BFD_CLK_PRN (bs->poll_state_start_or_timeout_clocks -
994 now));
995 break;
996 }
997 bs->poll_state_start_or_timeout_clocks = now;
998 bfd_set_poll_state (bs, BFD_POLL_IN_PROGRESS);
999 /* fallthrough */
1000 case BFD_POLL_IN_PROGRESS:
1001 case BFD_POLL_IN_PROGRESS_AND_QUEUED:
Klement Sekeraa57a9702017-02-02 06:58:07 +01001002 bfd_pkt_set_poll (vlib_buffer_get_current (b));
Klement Sekeraa57a9702017-02-02 06:58:07 +01001003 BFD_DBG ("Setting poll bit in packet, bs_idx=%u", bs->bs_idx);
Klement Sekera239790f2017-02-16 10:53:53 +01001004 break;
1005 case BFD_POLL_NOT_NEEDED:
1006 /* fallthrough */
1007 break;
Klement Sekeraa57a9702017-02-02 06:58:07 +01001008 }
1009 bfd_add_auth_section (b, bs);
Klement Sekerae50e8562017-04-04 16:19:48 +02001010 bfd_add_transport_layer (vm, bi, bs);
1011 if (!bfd_transport_control_frame (vm, bi, bs))
1012 {
1013 vlib_buffer_free_one (vm, bi);
1014 }
Klement Sekera3e0a3562016-12-19 09:05:21 +01001015 bs->last_tx_clocks = now;
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001016 bfd_calc_next_tx (bm, bs, now);
1017 }
1018 else
1019 {
Klement Sekera637b9c42016-12-08 05:19:14 +01001020 BFD_DBG
1021 ("No need to send control frame now, now is %lu, tx_timeout is %lu",
1022 now, bs->tx_timeout_clocks);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001023 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001024}
1025
1026void
Klement Sekerab17dd962017-01-09 07:43:48 +01001027bfd_init_final_control_frame (vlib_main_t * vm, vlib_buffer_t * b,
Klement Sekerae50e8562017-04-04 16:19:48 +02001028 bfd_main_t * bm, bfd_session_t * bs,
1029 int is_local)
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001030{
1031 BFD_DBG ("Send final control frame for bs_idx=%lu", bs->bs_idx);
Klement Sekera239790f2017-02-16 10:53:53 +01001032 bfd_init_control_frame (bm, bs, b);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001033 bfd_pkt_set_final (vlib_buffer_get_current (b));
Klement Sekeraa57a9702017-02-02 06:58:07 +01001034 bfd_add_auth_section (b, bs);
Klement Sekerae50e8562017-04-04 16:19:48 +02001035 u32 bi = vlib_get_buffer_index (vm, b);
1036 bfd_add_transport_layer (vm, bi, bs);
Klement Sekera3e0a3562016-12-19 09:05:21 +01001037 bs->last_tx_clocks = clib_cpu_time_now ();
Klement Sekeraa57a9702017-02-02 06:58:07 +01001038 /*
1039 * RFC allows to include changes in final frame, so if there were any
1040 * pending, we already did that, thus we can clear any pending poll needs
1041 */
Klement Sekera239790f2017-02-16 10:53:53 +01001042 bfd_set_poll_state (bs, BFD_POLL_NOT_NEEDED);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001043}
1044
1045static void
Klement Sekerae4504c62016-12-08 10:16:41 +01001046bfd_check_rx_timeout (bfd_main_t * bm, bfd_session_t * bs, u64 now,
1047 int handling_wakeup)
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001048{
Klement Sekerae50e8562017-04-04 16:19:48 +02001049 /*
1050 * sometimes the wheel expires an event a bit sooner than requested, account
1051 * for that here
1052 */
Klement Sekera637b9c42016-12-08 05:19:14 +01001053 if (bs->last_rx_clocks + bs->detection_time_clocks <=
1054 now + bm->wheel_inaccuracy)
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001055 {
1056 BFD_DBG ("Rx timeout, session goes down");
1057 bfd_set_diag (bs, BFD_DIAG_CODE_det_time_exp);
Klement Sekerae4504c62016-12-08 10:16:41 +01001058 bfd_set_state (bm, bs, BFD_STATE_down, handling_wakeup);
Klement Sekeraa57a9702017-02-02 06:58:07 +01001059 /*
1060 * If the remote system does not receive any
1061 * BFD Control packets for a Detection Time, it SHOULD reset
1062 * bfd.RemoteMinRxInterval to its initial value of 1 (per section 6.8.1,
1063 * since it is no longer required to maintain previous session state)
1064 * and then can transmit at its own rate.
1065 */
Klement Sekera239790f2017-02-16 10:53:53 +01001066 bfd_set_remote_required_min_rx (bm, bs, now, 1);
1067 }
1068 else if (bs->echo &&
1069 bs->echo_last_rx_clocks +
1070 bs->echo_transmit_interval_clocks * bs->local_detect_mult <=
1071 now + bm->wheel_inaccuracy)
1072 {
1073 BFD_DBG ("Echo rx timeout, session goes down");
1074 bfd_set_diag (bs, BFD_DIAG_CODE_echo_failed);
1075 bfd_set_state (bm, bs, BFD_STATE_down, handling_wakeup);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001076 }
1077}
1078
1079void
1080bfd_on_timeout (vlib_main_t * vm, vlib_node_runtime_t * rt, bfd_main_t * bm,
1081 bfd_session_t * bs, u64 now)
1082{
1083 BFD_DBG ("Timeout for bs_idx=%lu", bs->bs_idx);
1084 switch (bs->local_state)
1085 {
1086 case BFD_STATE_admin_down:
Klement Sekerae50e8562017-04-04 16:19:48 +02001087 bfd_send_periodic (vm, rt, bm, bs, now);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001088 break;
1089 case BFD_STATE_down:
Klement Sekerae50e8562017-04-04 16:19:48 +02001090 bfd_send_periodic (vm, rt, bm, bs, now);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001091 break;
1092 case BFD_STATE_init:
Klement Sekerae4504c62016-12-08 10:16:41 +01001093 bfd_check_rx_timeout (bm, bs, now, 1);
Klement Sekerae50e8562017-04-04 16:19:48 +02001094 bfd_send_periodic (vm, rt, bm, bs, now);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001095 break;
Klement Sekera239790f2017-02-16 10:53:53 +01001096 case BFD_STATE_up:
1097 bfd_check_rx_timeout (bm, bs, now, 1);
1098 if (BFD_POLL_NOT_NEEDED == bs->poll_state && !bs->echo &&
1099 bfd_is_echo_possible (bs))
1100 {
1101 /* switch on echo function as main detection method now */
1102 BFD_DBG ("Switching on echo function, bs_idx=%u", bs->bs_idx);
1103 bs->echo = 1;
1104 bs->echo_last_rx_clocks = now;
1105 bs->echo_tx_timeout_clocks = now;
Klement Sekera73884482017-02-23 09:26:30 +01001106 bfd_set_effective_required_min_rx (bm, bs,
Klement Sekera239790f2017-02-16 10:53:53 +01001107 clib_max
1108 (bm->min_required_min_rx_while_echo_clocks,
1109 bs->config_required_min_rx_clocks));
1110 bfd_set_poll_state (bs, BFD_POLL_NEEDED);
1111 }
Klement Sekerae50e8562017-04-04 16:19:48 +02001112 bfd_send_periodic (vm, rt, bm, bs, now);
Klement Sekera239790f2017-02-16 10:53:53 +01001113 if (bs->echo)
1114 {
Klement Sekerae50e8562017-04-04 16:19:48 +02001115 bfd_send_echo (vm, rt, bm, bs, now);
Klement Sekera239790f2017-02-16 10:53:53 +01001116 }
1117 break;
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001118 }
1119}
1120
1121/*
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001122 * bfd process node function
1123 */
1124static uword
1125bfd_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
1126{
1127 bfd_main_t *bm = &bfd_main;
1128 u32 *expired = 0;
1129 uword event_type, *event_data = 0;
1130
1131 /* So we can send events to the bfd process */
1132 bm->bfd_process_node_index = bfd_process_node.index;
1133
1134 while (1)
1135 {
1136 u64 now = clib_cpu_time_now ();
Dave Barach1e3417f2018-07-25 08:30:27 -04001137 bfd_lock (bm);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001138 u64 next_expire = timing_wheel_next_expiring_elt_time (&bm->wheel);
1139 BFD_DBG ("timing_wheel_next_expiring_elt_time(%p) returns %lu",
1140 &bm->wheel, next_expire);
Dave Barach1e3417f2018-07-25 08:30:27 -04001141 bm->bfd_process_next_wakeup_clocks =
1142 (i64) next_expire >= 0 ? next_expire : ~0;
1143 bfd_unlock (bm);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001144 if ((i64) next_expire < 0)
1145 {
1146 BFD_DBG ("wait for event without timeout");
1147 (void) vlib_process_wait_for_event (vm);
Klement Sekera0c1519b2016-12-08 05:03:32 +01001148 event_type = vlib_process_get_events (vm, &event_data);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001149 }
1150 else
1151 {
1152 f64 timeout = ((i64) next_expire - (i64) now) / bm->cpu_cps;
1153 BFD_DBG ("wait for event with timeout %.02f", timeout);
Klement Sekera0c1519b2016-12-08 05:03:32 +01001154 if (timeout < 0)
1155 {
1156 BFD_DBG ("negative timeout, already expired, skipping wait");
1157 event_type = ~0;
1158 }
1159 else
1160 {
1161 (void) vlib_process_wait_for_event_or_clock (vm, timeout);
1162 event_type = vlib_process_get_events (vm, &event_data);
1163 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001164 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001165 now = clib_cpu_time_now ();
Eyal Bari0db9b042018-10-11 14:09:58 +03001166 uword *session_index;
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001167 switch (event_type)
1168 {
1169 case ~0: /* no events => timeout */
1170 /* nothing to do here */
1171 break;
1172 case BFD_EVENT_RESCHEDULE:
Dave Barach1e3417f2018-07-25 08:30:27 -04001173 bfd_lock (bm);
1174 bm->bfd_process_wakeup_event_delay_clocks =
1175 now - bm->bfd_process_wakeup_event_start_clocks;
1176 bm->bfd_process_wakeup_events_in_flight--;
1177 bfd_unlock (bm);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001178 /* nothing to do here - reschedule is done automatically after
1179 * each event or timeout */
1180 break;
1181 case BFD_EVENT_NEW_SESSION:
Eyal Bari0db9b042018-10-11 14:09:58 +03001182 vec_foreach (session_index, event_data)
1183 {
1184 bfd_lock (bm);
1185 if (!pool_is_free_index (bm->sessions, *session_index))
1186 {
1187 bfd_session_t *bs =
1188 pool_elt_at_index (bm->sessions, *session_index);
1189 bfd_send_periodic (vm, rt, bm, bs, now);
1190 bfd_set_timer (bm, bs, now, 1);
1191 }
1192 else
1193 {
1194 BFD_DBG ("Ignoring event for non-existent session index %u",
1195 (u32) * session_index);
1196 }
1197 bfd_unlock (bm);
1198 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001199 break;
Klement Sekeraa57a9702017-02-02 06:58:07 +01001200 case BFD_EVENT_CONFIG_CHANGED:
Eyal Bari0db9b042018-10-11 14:09:58 +03001201 vec_foreach (session_index, event_data)
1202 {
1203 bfd_lock (bm);
1204 if (!pool_is_free_index (bm->sessions, *session_index))
1205 {
1206 bfd_session_t *bs =
1207 pool_elt_at_index (bm->sessions, *session_index);
1208 bfd_on_config_change (vm, rt, bm, bs, now);
1209 }
1210 else
1211 {
1212 BFD_DBG ("Ignoring event for non-existent session index %u",
1213 (u32) * session_index);
1214 }
1215 bfd_unlock (bm);
1216 }
Klement Sekeraa57a9702017-02-02 06:58:07 +01001217 break;
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001218 default:
Damjan Marion07a38572018-01-21 06:44:18 -08001219 vlib_log_err (bm->log_class, "BUG: event type 0x%wx", event_type);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001220 break;
1221 }
1222 BFD_DBG ("advancing wheel, now is %lu", now);
1223 BFD_DBG ("timing_wheel_advance (%p, %lu, %p, 0);", &bm->wheel, now,
1224 expired);
Dave Barach1e3417f2018-07-25 08:30:27 -04001225 bfd_lock (bm);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001226 expired = timing_wheel_advance (&bm->wheel, now, expired, 0);
1227 BFD_DBG ("Expired %d elements", vec_len (expired));
1228 u32 *p = NULL;
1229 vec_foreach (p, expired)
1230 {
1231 const u32 bs_idx = *p;
1232 if (!pool_is_free_index (bm->sessions, bs_idx))
1233 {
1234 bfd_session_t *bs = pool_elt_at_index (bm->sessions, bs_idx);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001235 bfd_on_timeout (vm, rt, bm, bs, now);
Klement Sekera239790f2017-02-16 10:53:53 +01001236 bfd_set_timer (bm, bs, now, 1);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001237 }
1238 }
Dave Barach1e3417f2018-07-25 08:30:27 -04001239 bfd_unlock (bm);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001240 if (expired)
1241 {
1242 _vec_len (expired) = 0;
1243 }
1244 if (event_data)
1245 {
1246 _vec_len (event_data) = 0;
1247 }
1248 }
1249
1250 return 0;
1251}
1252
1253/*
1254 * bfd process node declaration
1255 */
1256/* *INDENT-OFF* */
1257VLIB_REGISTER_NODE (bfd_process_node, static) = {
1258 .function = bfd_process,
1259 .type = VLIB_NODE_TYPE_PROCESS,
1260 .name = "bfd-process",
Klement Sekera46a87ad2017-01-02 08:22:23 +01001261 .n_next_nodes = 0,
1262 .next_nodes = {},
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001263};
1264/* *INDENT-ON* */
1265
1266static clib_error_t *
1267bfd_sw_interface_up_down (vnet_main_t * vnm, u32 sw_if_index, u32 flags)
1268{
1269 // bfd_main_t *bm = &bfd_main;
1270 // vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
1271 if (!(flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
1272 {
1273 /* TODO */
1274 }
1275 return 0;
1276}
1277
1278VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (bfd_sw_interface_up_down);
1279
1280static clib_error_t *
1281bfd_hw_interface_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
1282{
1283 // bfd_main_t *bm = &bfd_main;
1284 if (flags & VNET_HW_INTERFACE_FLAG_LINK_UP)
1285 {
1286 /* TODO */
1287 }
1288 return 0;
1289}
1290
1291VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION (bfd_hw_interface_up_down);
1292
Neale Ranns88fc83e2017-04-05 08:11:14 -07001293void
1294bfd_register_listener (bfd_notify_fn_t fn)
1295{
1296 bfd_main_t *bm = &bfd_main;
1297
1298 vec_add1 (bm->listeners, fn);
1299}
1300
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001301/*
1302 * setup function
1303 */
1304static clib_error_t *
1305bfd_main_init (vlib_main_t * vm)
1306{
Dave Barach1e3417f2018-07-25 08:30:27 -04001307 vlib_thread_main_t *tm = &vlib_thread_main;
1308 u32 n_vlib_mains = tm->n_vlib_mains;
Klement Sekerab17dd962017-01-09 07:43:48 +01001309#if BFD_DEBUG
1310 setbuf (stdout, NULL);
1311#endif
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001312 bfd_main_t *bm = &bfd_main;
1313 bm->random_seed = random_default_seed ();
1314 bm->vlib_main = vm;
1315 bm->vnet_main = vnet_get_main ();
Dave Barachb7b92992018-10-17 10:38:51 -04001316 clib_memset (&bm->wheel, 0, sizeof (bm->wheel));
Klement Sekerab17dd962017-01-09 07:43:48 +01001317 bm->cpu_cps = vm->clib_time.clocks_per_second;
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001318 BFD_DBG ("cps is %.2f", bm->cpu_cps);
Klement Sekeraa57a9702017-02-02 06:58:07 +01001319 bm->default_desired_min_tx_clocks =
Klement Sekera239790f2017-02-16 10:53:53 +01001320 bfd_usec_to_clocks (bm, BFD_DEFAULT_DESIRED_MIN_TX_USEC);
1321 bm->min_required_min_rx_while_echo_clocks =
1322 bfd_usec_to_clocks (bm, BFD_REQUIRED_MIN_RX_USEC_WHILE_ECHO);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001323 const u64 now = clib_cpu_time_now ();
1324 timing_wheel_init (&bm->wheel, now, bm->cpu_cps);
Klement Sekera637b9c42016-12-08 05:19:14 +01001325 bm->wheel_inaccuracy = 2 << bm->wheel.log2_clocks_per_bin;
Damjan Marion07a38572018-01-21 06:44:18 -08001326 bm->log_class = vlib_log_register_class ("bfd", 0);
1327 vlib_log_debug (bm->log_class, "initialized");
Dave Barach1e3417f2018-07-25 08:30:27 -04001328 bm->owner_thread_index = ~0;
1329 if (n_vlib_mains > 1)
1330 clib_spinlock_init (&bm->lock);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001331 return 0;
1332}
1333
1334VLIB_INIT_FUNCTION (bfd_main_init);
1335
1336bfd_session_t *
Klement Sekera239790f2017-02-16 10:53:53 +01001337bfd_get_session (bfd_main_t * bm, bfd_transport_e t)
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001338{
1339 bfd_session_t *result;
Dave Barach1e3417f2018-07-25 08:30:27 -04001340
1341 bfd_lock (bm);
1342
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001343 pool_get (bm->sessions, result);
Dave Barachb7b92992018-10-17 10:38:51 -04001344 clib_memset (result, 0, sizeof (*result));
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001345 result->bs_idx = result - bm->sessions;
1346 result->transport = t;
Klement Sekera239790f2017-02-16 10:53:53 +01001347 const unsigned limit = 1000;
1348 unsigned counter = 0;
1349 do
1350 {
1351 result->local_discr = random_u32 (&bm->random_seed);
1352 if (counter > limit)
1353 {
Damjan Marion07a38572018-01-21 06:44:18 -08001354 vlib_log_crit (bm->log_class,
1355 "couldn't allocate unused session discriminator even "
1356 "after %u tries!", limit);
Klement Sekera239790f2017-02-16 10:53:53 +01001357 pool_put (bm->sessions, result);
Dave Barach1e3417f2018-07-25 08:30:27 -04001358 bfd_unlock (bm);
Klement Sekera239790f2017-02-16 10:53:53 +01001359 return NULL;
1360 }
1361 ++counter;
1362 }
1363 while (hash_get (bm->session_by_disc, result->local_discr));
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001364 bfd_set_defaults (bm, result);
1365 hash_set (bm->session_by_disc, result->local_discr, result->bs_idx);
Dave Barach1e3417f2018-07-25 08:30:27 -04001366 bfd_unlock (bm);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001367 return result;
1368}
1369
1370void
1371bfd_put_session (bfd_main_t * bm, bfd_session_t * bs)
1372{
Dave Barach1e3417f2018-07-25 08:30:27 -04001373 bfd_lock (bm);
1374
Damjan Marion07a38572018-01-21 06:44:18 -08001375 vlib_log_info (bm->log_class, "delete session: %U",
1376 format_bfd_session_brief, bs);
Neale Ranns88fc83e2017-04-05 08:11:14 -07001377 bfd_notify_listeners (bm, BFD_LISTEN_EVENT_DELETE, bs);
Klement Sekerab17dd962017-01-09 07:43:48 +01001378 if (bs->auth.curr_key)
1379 {
1380 --bs->auth.curr_key->use_count;
1381 }
1382 if (bs->auth.next_key)
1383 {
1384 --bs->auth.next_key->use_count;
1385 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001386 hash_unset (bm->session_by_disc, bs->local_discr);
1387 pool_put (bm->sessions, bs);
Dave Barach1e3417f2018-07-25 08:30:27 -04001388 bfd_unlock (bm);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001389}
1390
1391bfd_session_t *
1392bfd_find_session_by_idx (bfd_main_t * bm, uword bs_idx)
1393{
Dave Barach1e3417f2018-07-25 08:30:27 -04001394 bfd_lock_check (bm);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001395 if (!pool_is_free_index (bm->sessions, bs_idx))
1396 {
1397 return pool_elt_at_index (bm->sessions, bs_idx);
1398 }
1399 return NULL;
1400}
1401
1402bfd_session_t *
1403bfd_find_session_by_disc (bfd_main_t * bm, u32 disc)
1404{
Dave Barach1e3417f2018-07-25 08:30:27 -04001405 bfd_lock_check (bm);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001406 uword *p = hash_get (bfd_main.session_by_disc, disc);
1407 if (p)
1408 {
1409 return pool_elt_at_index (bfd_main.sessions, *p);
1410 }
1411 return NULL;
1412}
1413
1414/**
1415 * @brief verify bfd packet - common checks
1416 *
1417 * @param pkt
1418 *
1419 * @return 1 if bfd packet is valid
1420 */
1421int
1422bfd_verify_pkt_common (const bfd_pkt_t * pkt)
1423{
1424 if (1 != bfd_pkt_get_version (pkt))
1425 {
1426 BFD_ERR ("BFD verification failed - unexpected version: '%d'",
1427 bfd_pkt_get_version (pkt));
1428 return 0;
1429 }
1430 if (pkt->head.length < sizeof (bfd_pkt_t) ||
1431 (bfd_pkt_get_auth_present (pkt) &&
Klement Sekerab17dd962017-01-09 07:43:48 +01001432 pkt->head.length < sizeof (bfd_pkt_with_common_auth_t)))
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001433 {
1434 BFD_ERR ("BFD verification failed - unexpected length: '%d' (auth "
1435 "present: %d)",
1436 pkt->head.length, bfd_pkt_get_auth_present (pkt));
1437 return 0;
1438 }
1439 if (!pkt->head.detect_mult)
1440 {
1441 BFD_ERR ("BFD verification failed - unexpected detect-mult: '%d'",
1442 pkt->head.detect_mult);
1443 return 0;
1444 }
1445 if (bfd_pkt_get_multipoint (pkt))
1446 {
1447 BFD_ERR ("BFD verification failed - unexpected multipoint: '%d'",
1448 bfd_pkt_get_multipoint (pkt));
1449 return 0;
1450 }
1451 if (!pkt->my_disc)
1452 {
1453 BFD_ERR ("BFD verification failed - unexpected my-disc: '%d'",
1454 pkt->my_disc);
1455 return 0;
1456 }
1457 if (!pkt->your_disc)
1458 {
1459 const u8 pkt_state = bfd_pkt_get_state (pkt);
1460 if (pkt_state != BFD_STATE_down && pkt_state != BFD_STATE_admin_down)
1461 {
1462 BFD_ERR ("BFD verification failed - unexpected state: '%s' "
1463 "(your-disc is zero)", bfd_state_string (pkt_state));
1464 return 0;
1465 }
1466 }
1467 return 1;
1468}
1469
Klement Sekerab17dd962017-01-09 07:43:48 +01001470static void
1471bfd_session_switch_auth_to_next (bfd_session_t * bs)
1472{
1473 BFD_DBG ("Switching authentication key from %U to %U for bs_idx=%u",
1474 format_bfd_auth_key, bs->auth.curr_key, format_bfd_auth_key,
1475 bs->auth.next_key, bs->bs_idx);
1476 bs->auth.is_delayed = 0;
1477 if (bs->auth.curr_key)
1478 {
1479 --bs->auth.curr_key->use_count;
1480 }
1481 bs->auth.curr_key = bs->auth.next_key;
1482 bs->auth.next_key = NULL;
1483 bs->auth.curr_bfd_key_id = bs->auth.next_bfd_key_id;
1484}
1485
1486static int
1487bfd_auth_type_is_meticulous (bfd_auth_type_e auth_type)
1488{
1489 if (BFD_AUTH_TYPE_meticulous_keyed_md5 == auth_type ||
1490 BFD_AUTH_TYPE_meticulous_keyed_sha1 == auth_type)
1491 {
1492 return 1;
1493 }
1494 return 0;
1495}
1496
1497static int
1498bfd_verify_pkt_auth_seq_num (bfd_session_t * bs,
1499 u32 received_seq_num, int is_meticulous)
1500{
1501 /*
1502 * RFC 5880 6.8.1:
1503 *
1504 * This variable MUST be set to zero after no packets have been
1505 * received on this session for at least twice the Detection Time.
1506 */
1507 u64 now = clib_cpu_time_now ();
1508 if (now - bs->last_rx_clocks > bs->detection_time_clocks * 2)
1509 {
1510 BFD_DBG ("BFD peer unresponsive for %lu clocks, which is > 2 * "
1511 "detection_time=%u clocks, resetting remote_seq_number_known "
1512 "flag",
1513 now - bs->last_rx_clocks, bs->detection_time_clocks * 2);
1514 bs->auth.remote_seq_number_known = 0;
1515 }
1516 if (bs->auth.remote_seq_number_known)
1517 {
1518 /* remote sequence number is known, verify its validity */
1519 const u32 max_u32 = 0xffffffff;
1520 /* the calculation might wrap, account for the special case... */
1521 if (bs->auth.remote_seq_number > max_u32 - 3 * bs->local_detect_mult)
1522 {
1523 /*
1524 * special case
1525 *
1526 * x y z
1527 * |----------+----------------------------+-----------|
1528 * 0 ^ ^ 0xffffffff
1529 * | remote_seq_num------+
1530 * |
1531 * +-----(remote_seq_num + 3*detect_mult) % * 0xffffffff
1532 *
1533 * x + y + z = 0xffffffff
1534 * x + z = 3 * detect_mult
1535 */
1536 const u32 z = max_u32 - bs->auth.remote_seq_number;
1537 const u32 x = 3 * bs->local_detect_mult - z;
1538 if (received_seq_num > x &&
1539 received_seq_num < bs->auth.remote_seq_number + is_meticulous)
1540 {
1541 BFD_ERR
1542 ("Recvd sequence number=%u out of ranges <0, %u>, <%u, %u>",
1543 received_seq_num, x,
1544 bs->auth.remote_seq_number + is_meticulous, max_u32);
1545 return 0;
1546 }
1547 }
1548 else
1549 {
1550 /* regular case */
1551 const u32 min = bs->auth.remote_seq_number + is_meticulous;
1552 const u32 max =
1553 bs->auth.remote_seq_number + 3 * bs->local_detect_mult;
1554 if (received_seq_num < min || received_seq_num > max)
1555 {
1556 BFD_ERR ("Recvd sequence number=%u out of range <%u, %u>",
1557 received_seq_num, min, max);
1558 return 0;
1559 }
1560 }
1561 }
1562 return 1;
1563}
1564
1565static int
1566bfd_verify_pkt_auth_key_sha1 (const bfd_pkt_t * pkt, u32 pkt_size,
1567 bfd_session_t * bs, u8 bfd_key_id,
1568 bfd_auth_key_t * auth_key)
1569{
1570 ASSERT (auth_key->auth_type == BFD_AUTH_TYPE_keyed_sha1 ||
1571 auth_key->auth_type == BFD_AUTH_TYPE_meticulous_keyed_sha1);
1572
1573 u8 result[SHA_DIGEST_LENGTH];
1574 bfd_pkt_with_common_auth_t *with_common = (void *) pkt;
1575 if (pkt_size < sizeof (*with_common))
1576 {
1577 BFD_ERR ("Packet size too small to hold authentication common header");
1578 return 0;
1579 }
1580 if (with_common->common_auth.type != auth_key->auth_type)
1581 {
1582 BFD_ERR ("BFD auth type mismatch, packet auth=%d:%s doesn't match "
1583 "in-use auth=%d:%s",
1584 with_common->common_auth.type,
1585 bfd_auth_type_str (with_common->common_auth.type),
1586 auth_key->auth_type, bfd_auth_type_str (auth_key->auth_type));
1587 return 0;
1588 }
1589 bfd_pkt_with_sha1_auth_t *with_sha1 = (void *) pkt;
1590 if (pkt_size < sizeof (*with_sha1) ||
1591 with_sha1->sha1_auth.type_len.len < sizeof (with_sha1->sha1_auth))
1592 {
1593 BFD_ERR
1594 ("BFD size mismatch, payload size=%u, expected=%u, auth_len=%u, "
1595 "expected=%u", pkt_size, sizeof (*with_sha1),
1596 with_sha1->sha1_auth.type_len.len, sizeof (with_sha1->sha1_auth));
1597 return 0;
1598 }
1599 if (with_sha1->sha1_auth.key_id != bfd_key_id)
1600 {
1601 BFD_ERR
1602 ("BFD key ID mismatch, packet key ID=%u doesn't match key ID=%u%s",
1603 with_sha1->sha1_auth.key_id, bfd_key_id,
1604 bs->
1605 auth.is_delayed ? " (but a delayed auth change is scheduled)" : "");
1606 return 0;
1607 }
1608 SHA_CTX ctx;
1609 if (!SHA1_Init (&ctx))
1610 {
1611 BFD_ERR ("SHA1_Init failed");
1612 return 0;
1613 }
1614 /* ignore last 20 bytes - use the actual key data instead pkt data */
1615 if (!SHA1_Update (&ctx, with_sha1,
1616 sizeof (*with_sha1) - sizeof (with_sha1->sha1_auth.hash)))
1617 {
1618 BFD_ERR ("SHA1_Update failed");
1619 return 0;
1620 }
1621 if (!SHA1_Update (&ctx, auth_key->key, sizeof (auth_key->key)))
1622 {
1623 BFD_ERR ("SHA1_Update failed");
1624 return 0;
1625 }
1626 if (!SHA1_Final (result, &ctx))
1627 {
1628 BFD_ERR ("SHA1_Final failed");
1629 return 0;
1630 }
1631 if (0 == memcmp (result, with_sha1->sha1_auth.hash, SHA_DIGEST_LENGTH))
1632 {
1633 return 1;
1634 }
1635 BFD_ERR ("SHA1 hash: %U doesn't match the expected value: %U",
1636 format_hex_bytes, with_sha1->sha1_auth.hash, SHA_DIGEST_LENGTH,
1637 format_hex_bytes, result, SHA_DIGEST_LENGTH);
1638 return 0;
1639}
1640
1641static int
1642bfd_verify_pkt_auth_key (const bfd_pkt_t * pkt, u32 pkt_size,
1643 bfd_session_t * bs, u8 bfd_key_id,
1644 bfd_auth_key_t * auth_key)
1645{
Damjan Marion07a38572018-01-21 06:44:18 -08001646 bfd_main_t *bm = &bfd_main;
Klement Sekerab17dd962017-01-09 07:43:48 +01001647 switch (auth_key->auth_type)
1648 {
1649 case BFD_AUTH_TYPE_reserved:
Damjan Marion07a38572018-01-21 06:44:18 -08001650 vlib_log_err (bm->log_class,
1651 "internal error, unexpected auth_type=%d:%s",
Klement Sekerab17dd962017-01-09 07:43:48 +01001652 auth_key->auth_type,
1653 bfd_auth_type_str (auth_key->auth_type));
1654 return 0;
1655 case BFD_AUTH_TYPE_simple_password:
Damjan Marion07a38572018-01-21 06:44:18 -08001656 vlib_log_err (bm->log_class,
1657 "internal error, not implemented, unexpected auth_type=%d:%s",
1658 auth_key->auth_type,
1659 bfd_auth_type_str (auth_key->auth_type));
Klement Sekerab17dd962017-01-09 07:43:48 +01001660 return 0;
1661 case BFD_AUTH_TYPE_keyed_md5:
1662 /* fallthrough */
1663 case BFD_AUTH_TYPE_meticulous_keyed_md5:
Damjan Marion07a38572018-01-21 06:44:18 -08001664 vlib_log_err
1665 (bm->log_class,
1666 "internal error, not implemented, unexpected auth_type=%d:%s",
Klement Sekerab17dd962017-01-09 07:43:48 +01001667 auth_key->auth_type, bfd_auth_type_str (auth_key->auth_type));
1668 return 0;
1669 case BFD_AUTH_TYPE_keyed_sha1:
1670 /* fallthrough */
1671 case BFD_AUTH_TYPE_meticulous_keyed_sha1:
1672#if WITH_LIBSSL > 0
1673 do
1674 {
1675 const u32 seq_num = clib_net_to_host_u32 (((bfd_pkt_with_sha1_auth_t
1676 *) pkt)->
1677 sha1_auth.seq_num);
1678 return bfd_verify_pkt_auth_seq_num (bs, seq_num,
1679 bfd_auth_type_is_meticulous
1680 (auth_key->auth_type))
1681 && bfd_verify_pkt_auth_key_sha1 (pkt, pkt_size, bs, bfd_key_id,
1682 auth_key);
1683 }
1684 while (0);
1685#else
Damjan Marion07a38572018-01-21 06:44:18 -08001686 vlib_log_err
1687 (bm->log_class,
1688 "internal error, attempt to use SHA1 without SSL support");
Klement Sekerab17dd962017-01-09 07:43:48 +01001689 return 0;
1690#endif
1691 }
1692 return 0;
1693}
1694
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001695/**
1696 * @brief verify bfd packet - authentication
1697 *
1698 * @param pkt
1699 *
1700 * @return 1 if bfd packet is valid
1701 */
1702int
Klement Sekerab17dd962017-01-09 07:43:48 +01001703bfd_verify_pkt_auth (const bfd_pkt_t * pkt, u16 pkt_size, bfd_session_t * bs)
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001704{
Klement Sekerab17dd962017-01-09 07:43:48 +01001705 if (bfd_pkt_get_auth_present (pkt))
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001706 {
Klement Sekerab17dd962017-01-09 07:43:48 +01001707 /* authentication present in packet */
1708 if (!bs->auth.curr_key)
1709 {
1710 /* currently not using authentication - can we turn it on? */
1711 if (bs->auth.is_delayed && bs->auth.next_key)
1712 {
1713 /* yes, switch is scheduled - make sure the auth is valid */
1714 if (bfd_verify_pkt_auth_key (pkt, pkt_size, bs,
1715 bs->auth.next_bfd_key_id,
1716 bs->auth.next_key))
1717 {
1718 /* auth matches next key, do the switch, packet is valid */
1719 bfd_session_switch_auth_to_next (bs);
1720 return 1;
1721 }
1722 }
1723 }
1724 else
1725 {
1726 /* yes, using authentication, verify the key */
1727 if (bfd_verify_pkt_auth_key (pkt, pkt_size, bs,
1728 bs->auth.curr_bfd_key_id,
1729 bs->auth.curr_key))
1730 {
1731 /* verification passed, packet is valid */
1732 return 1;
1733 }
1734 else
1735 {
1736 /* verification failed - but maybe we need to switch key */
1737 if (bs->auth.is_delayed && bs->auth.next_key)
1738 {
1739 /* delayed switch present, verify if that key works */
1740 if (bfd_verify_pkt_auth_key (pkt, pkt_size, bs,
1741 bs->auth.next_bfd_key_id,
1742 bs->auth.next_key))
1743 {
1744 /* auth matches next key, switch key, packet is valid */
1745 bfd_session_switch_auth_to_next (bs);
1746 return 1;
1747 }
1748 }
1749 }
1750 }
1751 }
1752 else
1753 {
1754 /* authentication in packet not present */
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001755 if (pkt_size > sizeof (*pkt))
1756 {
1757 BFD_ERR ("BFD verification failed - unexpected packet size '%d' "
1758 "(auth not present)", pkt_size);
1759 return 0;
1760 }
Klement Sekerab17dd962017-01-09 07:43:48 +01001761 if (bs->auth.curr_key)
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001762 {
Klement Sekerab17dd962017-01-09 07:43:48 +01001763 /* currently authenticating - could we turn it off? */
1764 if (bs->auth.is_delayed && !bs->auth.next_key)
1765 {
1766 /* yes, delayed switch to NULL key is scheduled */
1767 bfd_session_switch_auth_to_next (bs);
1768 return 1;
1769 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001770 }
Klement Sekerab17dd962017-01-09 07:43:48 +01001771 else
1772 {
1773 /* no auth in packet, no auth in use - packet is valid */
1774 return 1;
1775 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001776 }
Klement Sekerab17dd962017-01-09 07:43:48 +01001777 return 0;
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001778}
1779
1780void
1781bfd_consume_pkt (bfd_main_t * bm, const bfd_pkt_t * pkt, u32 bs_idx)
1782{
Dave Barach1e3417f2018-07-25 08:30:27 -04001783 bfd_lock_check (bm);
1784
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001785 bfd_session_t *bs = bfd_find_session_by_idx (bm, bs_idx);
Klement Sekera0e2e0df2017-03-03 08:51:08 +01001786 if (!bs || (pkt->your_disc && pkt->your_disc != bs->local_discr))
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001787 {
1788 return;
1789 }
1790 BFD_DBG ("Scanning bfd packet, bs_idx=%d", bs->bs_idx);
1791 bs->remote_discr = pkt->my_disc;
1792 bs->remote_state = bfd_pkt_get_state (pkt);
1793 bs->remote_demand = bfd_pkt_get_demand (pkt);
Klement Sekera73884482017-02-23 09:26:30 +01001794 bs->remote_diag = bfd_pkt_get_diag_code (pkt);
Klement Sekera637b9c42016-12-08 05:19:14 +01001795 u64 now = clib_cpu_time_now ();
1796 bs->last_rx_clocks = now;
Klement Sekerab17dd962017-01-09 07:43:48 +01001797 if (bfd_pkt_get_auth_present (pkt))
1798 {
1799 bfd_auth_type_e auth_type =
1800 ((bfd_pkt_with_common_auth_t *) (pkt))->common_auth.type;
1801 switch (auth_type)
1802 {
1803 case BFD_AUTH_TYPE_reserved:
1804 /* fallthrough */
1805 case BFD_AUTH_TYPE_simple_password:
1806 /* fallthrough */
1807 case BFD_AUTH_TYPE_keyed_md5:
1808 /* fallthrough */
1809 case BFD_AUTH_TYPE_meticulous_keyed_md5:
Damjan Marion07a38572018-01-21 06:44:18 -08001810 vlib_log_crit (bm->log_class,
1811 "internal error, unexpected auth_type=%d:%s",
1812 auth_type, bfd_auth_type_str (auth_type));
Klement Sekerab17dd962017-01-09 07:43:48 +01001813 break;
1814 case BFD_AUTH_TYPE_keyed_sha1:
1815 /* fallthrough */
1816 case BFD_AUTH_TYPE_meticulous_keyed_sha1:
1817 do
1818 {
1819 bfd_pkt_with_sha1_auth_t *with_sha1 =
1820 (bfd_pkt_with_sha1_auth_t *) pkt;
1821 bs->auth.remote_seq_number =
1822 clib_net_to_host_u32 (with_sha1->sha1_auth.seq_num);
1823 bs->auth.remote_seq_number_known = 1;
1824 BFD_DBG ("Received sequence number %u",
1825 bs->auth.remote_seq_number);
1826 }
1827 while (0);
1828 }
1829 }
Klement Sekeraa57a9702017-02-02 06:58:07 +01001830 bs->remote_desired_min_tx_clocks =
1831 bfd_usec_to_clocks (bm, clib_net_to_host_u32 (pkt->des_min_tx));
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001832 bs->remote_detect_mult = pkt->head.detect_mult;
Klement Sekera637b9c42016-12-08 05:19:14 +01001833 bfd_set_remote_required_min_rx (bm, bs, now,
Klement Sekera239790f2017-02-16 10:53:53 +01001834 clib_net_to_host_u32 (pkt->req_min_rx));
1835 bfd_set_remote_required_min_echo_rx (bm, bs, now,
1836 clib_net_to_host_u32
1837 (pkt->req_min_echo_rx));
Klement Sekera239790f2017-02-16 10:53:53 +01001838 if (bfd_pkt_get_final (pkt))
Klement Sekeraa57a9702017-02-02 06:58:07 +01001839 {
Klement Sekera239790f2017-02-16 10:53:53 +01001840 if (BFD_POLL_IN_PROGRESS == bs->poll_state)
Klement Sekeraa57a9702017-02-02 06:58:07 +01001841 {
Klement Sekera239790f2017-02-16 10:53:53 +01001842 BFD_DBG ("Poll sequence terminated, bs_idx=%u", bs->bs_idx);
1843 bfd_set_poll_state (bs, BFD_POLL_NOT_NEEDED);
1844 if (BFD_STATE_up == bs->local_state)
1845 {
Klement Sekera73884482017-02-23 09:26:30 +01001846 bfd_set_effective_required_min_rx (bm, bs,
Klement Sekera239790f2017-02-16 10:53:53 +01001847 clib_max (bs->echo *
1848 bm->min_required_min_rx_while_echo_clocks,
1849 bs->config_required_min_rx_clocks));
1850 }
1851 }
1852 else if (BFD_POLL_IN_PROGRESS_AND_QUEUED == bs->poll_state)
1853 {
1854 /*
1855 * next poll sequence must be delayed by at least the round trip
1856 * time, so calculate that here
1857 */
1858 BFD_DBG ("Next poll sequence can commence in " BFD_CLK_FMT,
1859 BFD_CLK_PRN (now -
1860 bs->poll_state_start_or_timeout_clocks));
1861 bs->poll_state_start_or_timeout_clocks =
1862 now + (now - bs->poll_state_start_or_timeout_clocks);
1863 BFD_DBG
1864 ("Poll sequence terminated, but another is needed, bs_idx=%u",
1865 bs->bs_idx);
1866 bfd_set_poll_state (bs, BFD_POLL_NEEDED);
Klement Sekeraa57a9702017-02-02 06:58:07 +01001867 }
1868 }
Klement Sekera239790f2017-02-16 10:53:53 +01001869 bfd_calc_next_tx (bm, bs, now);
1870 bfd_set_timer (bm, bs, now, 0);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001871 if (BFD_STATE_admin_down == bs->local_state)
Klement Sekerac48829b2017-02-14 07:55:57 +01001872 {
1873 BFD_DBG ("Session is admin-down, ignoring packet, bs_idx=%u",
1874 bs->bs_idx);
1875 return;
1876 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001877 if (BFD_STATE_admin_down == bs->remote_state)
1878 {
1879 bfd_set_diag (bs, BFD_DIAG_CODE_neighbor_sig_down);
Klement Sekerae4504c62016-12-08 10:16:41 +01001880 bfd_set_state (bm, bs, BFD_STATE_down, 0);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001881 }
1882 else if (BFD_STATE_down == bs->local_state)
1883 {
1884 if (BFD_STATE_down == bs->remote_state)
1885 {
Klement Sekerae50e8562017-04-04 16:19:48 +02001886 bfd_set_diag (bs, BFD_DIAG_CODE_no_diag);
Klement Sekerae4504c62016-12-08 10:16:41 +01001887 bfd_set_state (bm, bs, BFD_STATE_init, 0);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001888 }
1889 else if (BFD_STATE_init == bs->remote_state)
1890 {
Klement Sekerae50e8562017-04-04 16:19:48 +02001891 bfd_set_diag (bs, BFD_DIAG_CODE_no_diag);
Klement Sekerae4504c62016-12-08 10:16:41 +01001892 bfd_set_state (bm, bs, BFD_STATE_up, 0);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001893 }
1894 }
1895 else if (BFD_STATE_init == bs->local_state)
1896 {
1897 if (BFD_STATE_up == bs->remote_state ||
1898 BFD_STATE_init == bs->remote_state)
1899 {
Klement Sekerae50e8562017-04-04 16:19:48 +02001900 bfd_set_diag (bs, BFD_DIAG_CODE_no_diag);
Klement Sekerae4504c62016-12-08 10:16:41 +01001901 bfd_set_state (bm, bs, BFD_STATE_up, 0);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001902 }
1903 }
1904 else /* BFD_STATE_up == bs->local_state */
1905 {
1906 if (BFD_STATE_down == bs->remote_state)
1907 {
1908 bfd_set_diag (bs, BFD_DIAG_CODE_neighbor_sig_down);
Klement Sekerae4504c62016-12-08 10:16:41 +01001909 bfd_set_state (bm, bs, BFD_STATE_down, 0);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001910 }
1911 }
1912}
1913
Klement Sekera239790f2017-02-16 10:53:53 +01001914int
1915bfd_consume_echo_pkt (bfd_main_t * bm, vlib_buffer_t * b)
Klement Sekeraa57a9702017-02-02 06:58:07 +01001916{
Klement Sekera239790f2017-02-16 10:53:53 +01001917 bfd_echo_pkt_t *pkt = NULL;
1918 if (b->current_length != sizeof (*pkt))
Klement Sekeraa57a9702017-02-02 06:58:07 +01001919 {
Klement Sekera239790f2017-02-16 10:53:53 +01001920 return 0;
Klement Sekeraa57a9702017-02-02 06:58:07 +01001921 }
Klement Sekera239790f2017-02-16 10:53:53 +01001922 pkt = vlib_buffer_get_current (b);
1923 bfd_session_t *bs = bfd_find_session_by_disc (bm, pkt->discriminator);
1924 if (!bs)
1925 {
1926 return 0;
1927 }
1928 BFD_DBG ("Scanning bfd echo packet, bs_idx=%d", bs->bs_idx);
1929 u64 checksum =
1930 bfd_calc_echo_checksum (bs->local_discr, pkt->expire_time_clocks,
1931 bs->echo_secret);
1932 if (checksum != pkt->checksum)
1933 {
1934 BFD_DBG ("Invalid echo packet, checksum mismatch");
1935 return 1;
1936 }
1937 u64 now = clib_cpu_time_now ();
1938 if (pkt->expire_time_clocks < now)
1939 {
1940 BFD_DBG ("Stale packet received, expire time %lu < now %lu",
1941 pkt->expire_time_clocks, now);
1942 }
1943 else
1944 {
1945 bs->echo_last_rx_clocks = now;
1946 }
1947 return 1;
Klement Sekeraa57a9702017-02-02 06:58:07 +01001948}
1949
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001950u8 *
1951format_bfd_session (u8 * s, va_list * args)
1952{
1953 const bfd_session_t *bs = va_arg (*args, bfd_session_t *);
Damjan Marion07a38572018-01-21 06:44:18 -08001954 u32 indent = format_get_indent (s) + vlib_log_get_indent ();
Klement Sekera239790f2017-02-16 10:53:53 +01001955 s = format (s, "bs_idx=%u local-state=%s remote-state=%s\n"
1956 "%Ulocal-discriminator=%u remote-discriminator=%u\n"
1957 "%Ulocal-diag=%s echo-active=%s\n"
1958 "%Udesired-min-tx=%u required-min-rx=%u\n"
1959 "%Urequired-min-echo-rx=%u detect-mult=%u\n"
1960 "%Uremote-min-rx=%u remote-min-echo-rx=%u\n"
1961 "%Uremote-demand=%s poll-state=%s\n"
1962 "%Uauth: local-seq-num=%u remote-seq-num=%u\n"
1963 "%U is-delayed=%s\n"
1964 "%U curr-key=%U\n"
1965 "%U next-key=%U",
Klement Sekerab17dd962017-01-09 07:43:48 +01001966 bs->bs_idx, bfd_state_string (bs->local_state),
Klement Sekera239790f2017-02-16 10:53:53 +01001967 bfd_state_string (bs->remote_state), format_white_space, indent,
1968 bs->local_discr, bs->remote_discr, format_white_space, indent,
1969 bfd_diag_code_string (bs->local_diag),
1970 (bs->echo ? "yes" : "no"), format_white_space, indent,
Klement Sekeraa57a9702017-02-02 06:58:07 +01001971 bs->config_desired_min_tx_usec, bs->config_required_min_rx_usec,
Klement Sekera239790f2017-02-16 10:53:53 +01001972 format_white_space, indent, 1, bs->local_detect_mult,
1973 format_white_space, indent, bs->remote_min_rx_usec,
1974 bs->remote_min_echo_rx_usec, format_white_space, indent,
1975 (bs->remote_demand ? "yes" : "no"),
1976 bfd_poll_state_string (bs->poll_state), format_white_space,
1977 indent, bs->auth.local_seq_number, bs->auth.remote_seq_number,
1978 format_white_space, indent,
1979 (bs->auth.is_delayed ? "yes" : "no"), format_white_space,
1980 indent, format_bfd_auth_key, bs->auth.curr_key,
1981 format_white_space, indent, format_bfd_auth_key,
1982 bs->auth.next_key);
Klement Sekerab17dd962017-01-09 07:43:48 +01001983 return s;
1984}
1985
Damjan Marion07a38572018-01-21 06:44:18 -08001986u8 *
1987format_bfd_session_brief (u8 * s, va_list * args)
1988{
1989 const bfd_session_t *bs = va_arg (*args, bfd_session_t *);
1990 s =
1991 format (s, "bs_idx=%u local-state=%s remote-state=%s", bs->bs_idx,
1992 bfd_state_string (bs->local_state),
1993 bfd_state_string (bs->remote_state));
1994 return s;
1995}
1996
Klement Sekerab17dd962017-01-09 07:43:48 +01001997unsigned
1998bfd_auth_type_supported (bfd_auth_type_e auth_type)
1999{
2000 if (auth_type == BFD_AUTH_TYPE_keyed_sha1 ||
2001 auth_type == BFD_AUTH_TYPE_meticulous_keyed_sha1)
2002 {
2003 return 1;
2004 }
2005 return 0;
2006}
2007
2008vnet_api_error_t
2009bfd_auth_activate (bfd_session_t * bs, u32 conf_key_id,
2010 u8 bfd_key_id, u8 is_delayed)
2011{
2012 bfd_main_t *bm = &bfd_main;
2013 const uword *key_idx_p =
2014 hash_get (bm->auth_key_by_conf_key_id, conf_key_id);
2015 if (!key_idx_p)
2016 {
Damjan Marion07a38572018-01-21 06:44:18 -08002017 vlib_log_err (bm->log_class,
2018 "authentication key with config ID %u doesn't exist)",
Klement Sekerab17dd962017-01-09 07:43:48 +01002019 conf_key_id);
2020 return VNET_API_ERROR_BFD_ENOENT;
2021 }
2022 const uword key_idx = *key_idx_p;
2023 bfd_auth_key_t *key = pool_elt_at_index (bm->auth_keys, key_idx);
2024 if (is_delayed)
2025 {
2026 if (bs->auth.next_key == key)
2027 {
2028 /* already using this key, no changes required */
2029 return 0;
2030 }
2031 bs->auth.next_key = key;
2032 bs->auth.next_bfd_key_id = bfd_key_id;
2033 bs->auth.is_delayed = 1;
2034 }
2035 else
2036 {
2037 if (bs->auth.curr_key == key)
2038 {
2039 /* already using this key, no changes required */
2040 return 0;
2041 }
2042 if (bs->auth.curr_key)
2043 {
2044 --bs->auth.curr_key->use_count;
2045 }
2046 bs->auth.curr_key = key;
2047 bs->auth.curr_bfd_key_id = bfd_key_id;
2048 bs->auth.is_delayed = 0;
2049 }
2050 ++key->use_count;
Klement Sekera239790f2017-02-16 10:53:53 +01002051 BFD_DBG ("\nSession auth modified: %U", format_bfd_session, bs);
Damjan Marion07a38572018-01-21 06:44:18 -08002052 vlib_log_info (bm->log_class, "session auth modified: %U",
2053 format_bfd_session_brief, bs);
Klement Sekerab17dd962017-01-09 07:43:48 +01002054 return 0;
2055}
2056
2057vnet_api_error_t
2058bfd_auth_deactivate (bfd_session_t * bs, u8 is_delayed)
2059{
Damjan Marion07a38572018-01-21 06:44:18 -08002060 bfd_main_t *bm = &bfd_main;
Klement Sekerab17dd962017-01-09 07:43:48 +01002061#if WITH_LIBSSL > 0
2062 if (!is_delayed)
2063 {
2064 /* not delayed - deactivate the current key right now */
2065 if (bs->auth.curr_key)
2066 {
2067 --bs->auth.curr_key->use_count;
2068 bs->auth.curr_key = NULL;
2069 }
2070 bs->auth.is_delayed = 0;
2071 }
2072 else
2073 {
2074 /* delayed - mark as so */
2075 bs->auth.is_delayed = 1;
2076 }
2077 /*
2078 * clear the next key unconditionally - either the auth change is not delayed
2079 * in which case the caller expects the session to not use authentication
2080 * from this point forward, or it is delayed, in which case the next_key
2081 * needs to be set to NULL to make it so in the future
2082 */
2083 if (bs->auth.next_key)
2084 {
2085 --bs->auth.next_key->use_count;
2086 bs->auth.next_key = NULL;
2087 }
Klement Sekera239790f2017-02-16 10:53:53 +01002088 BFD_DBG ("\nSession auth modified: %U", format_bfd_session, bs);
Damjan Marion07a38572018-01-21 06:44:18 -08002089 vlib_log_info (bm->log_class, "session auth modified: %U",
2090 format_bfd_session_brief, bs);
Klement Sekerab17dd962017-01-09 07:43:48 +01002091 return 0;
2092#else
Damjan Marion07a38572018-01-21 06:44:18 -08002093 vlib_log_err (bm->log_class,
2094 "SSL missing, cannot deactivate BFD authentication");
Klement Sekerab17dd962017-01-09 07:43:48 +01002095 return VNET_API_ERROR_BFD_NOTSUPP;
2096#endif
Klement Sekera0e3c0de2016-09-29 14:43:44 +02002097}
2098
Klement Sekeraa57a9702017-02-02 06:58:07 +01002099vnet_api_error_t
2100bfd_session_set_params (bfd_main_t * bm, bfd_session_t * bs,
2101 u32 desired_min_tx_usec,
2102 u32 required_min_rx_usec, u8 detect_mult)
2103{
2104 if (bs->local_detect_mult != detect_mult ||
2105 bs->config_desired_min_tx_usec != desired_min_tx_usec ||
2106 bs->config_required_min_rx_usec != required_min_rx_usec)
2107 {
Klement Sekera239790f2017-02-16 10:53:53 +01002108 BFD_DBG ("\nChanging session params: %U", format_bfd_session, bs);
Klement Sekeraa57a9702017-02-02 06:58:07 +01002109 switch (bs->poll_state)
2110 {
Klement Sekera239790f2017-02-16 10:53:53 +01002111 case BFD_POLL_NOT_NEEDED:
Klement Sekeraa57a9702017-02-02 06:58:07 +01002112 if (BFD_STATE_up == bs->local_state ||
2113 BFD_STATE_init == bs->local_state)
2114 {
2115 /* poll sequence is not needed for detect multiplier change */
2116 if (bs->config_desired_min_tx_usec != desired_min_tx_usec ||
2117 bs->config_required_min_rx_usec != required_min_rx_usec)
2118 {
Klement Sekera239790f2017-02-16 10:53:53 +01002119 bfd_set_poll_state (bs, BFD_POLL_NEEDED);
Klement Sekeraa57a9702017-02-02 06:58:07 +01002120 }
2121 }
2122 break;
Klement Sekera239790f2017-02-16 10:53:53 +01002123 case BFD_POLL_NEEDED:
2124 case BFD_POLL_IN_PROGRESS_AND_QUEUED:
2125 /*
2126 * nothing to do - will be handled in the future poll which is
2127 * already scheduled for execution
2128 */
Klement Sekeraa57a9702017-02-02 06:58:07 +01002129 break;
Klement Sekera239790f2017-02-16 10:53:53 +01002130 case BFD_POLL_IN_PROGRESS:
2131 /* poll sequence is not needed for detect multiplier change */
2132 if (bs->config_desired_min_tx_usec != desired_min_tx_usec ||
2133 bs->config_required_min_rx_usec != required_min_rx_usec)
2134 {
2135 BFD_DBG ("Poll in progress, queueing extra poll, bs_idx=%u",
2136 bs->bs_idx);
2137 bfd_set_poll_state (bs, BFD_POLL_IN_PROGRESS_AND_QUEUED);
2138 }
Klement Sekeraa57a9702017-02-02 06:58:07 +01002139 }
2140
2141 bs->local_detect_mult = detect_mult;
2142 bs->config_desired_min_tx_usec = desired_min_tx_usec;
2143 bs->config_desired_min_tx_clocks =
2144 bfd_usec_to_clocks (bm, desired_min_tx_usec);
2145 bs->config_required_min_rx_usec = required_min_rx_usec;
2146 bs->config_required_min_rx_clocks =
2147 bfd_usec_to_clocks (bm, required_min_rx_usec);
Klement Sekera239790f2017-02-16 10:53:53 +01002148 BFD_DBG ("\nChanged session params: %U", format_bfd_session, bs);
Klement Sekeraa57a9702017-02-02 06:58:07 +01002149
Damjan Marion07a38572018-01-21 06:44:18 -08002150 vlib_log_info (bm->log_class, "changed session params: %U",
2151 format_bfd_session_brief, bs);
Klement Sekeraa57a9702017-02-02 06:58:07 +01002152 vlib_process_signal_event (bm->vlib_main, bm->bfd_process_node_index,
2153 BFD_EVENT_CONFIG_CHANGED, bs->bs_idx);
2154 }
2155 else
2156 {
2157 BFD_DBG ("Ignore parameter change - no change, bs_idx=%u", bs->bs_idx);
2158 }
2159 return 0;
2160}
2161
Klement Sekera73884482017-02-23 09:26:30 +01002162vnet_api_error_t
2163bfd_auth_set_key (u32 conf_key_id, u8 auth_type, u8 key_len,
2164 const u8 * key_data)
2165{
Damjan Marion07a38572018-01-21 06:44:18 -08002166 bfd_main_t *bm = &bfd_main;
Klement Sekera73884482017-02-23 09:26:30 +01002167#if WITH_LIBSSL > 0
2168 bfd_auth_key_t *auth_key = NULL;
Klement Sekerab16bfe32017-02-28 11:56:48 +01002169 if (!key_len || key_len > bfd_max_key_len_for_auth_type (auth_type))
Klement Sekera73884482017-02-23 09:26:30 +01002170 {
Damjan Marion07a38572018-01-21 06:44:18 -08002171 vlib_log_err (bm->log_class,
2172 "invalid authentication key length for auth_type=%d:%s "
2173 "(key_len=%u, must be non-zero, expected max=%u)",
Klement Sekera73884482017-02-23 09:26:30 +01002174 auth_type, bfd_auth_type_str (auth_type), key_len,
Klement Sekerab16bfe32017-02-28 11:56:48 +01002175 (u32) bfd_max_key_len_for_auth_type (auth_type));
Klement Sekera73884482017-02-23 09:26:30 +01002176 return VNET_API_ERROR_INVALID_VALUE;
2177 }
2178 if (!bfd_auth_type_supported (auth_type))
2179 {
Damjan Marion07a38572018-01-21 06:44:18 -08002180 vlib_log_err (bm->log_class, "unsupported auth type=%d:%s", auth_type,
Klement Sekera73884482017-02-23 09:26:30 +01002181 bfd_auth_type_str (auth_type));
2182 return VNET_API_ERROR_BFD_NOTSUPP;
2183 }
Klement Sekera73884482017-02-23 09:26:30 +01002184 uword *key_idx_p = hash_get (bm->auth_key_by_conf_key_id, conf_key_id);
2185 if (key_idx_p)
2186 {
2187 /* modifying existing key - must not be used */
2188 const uword key_idx = *key_idx_p;
2189 auth_key = pool_elt_at_index (bm->auth_keys, key_idx);
2190 if (auth_key->use_count > 0)
2191 {
Damjan Marion07a38572018-01-21 06:44:18 -08002192 vlib_log_err (bm->log_class,
2193 "authentication key with conf ID %u in use by %u BFD "
2194 "session(s) - cannot modify", conf_key_id,
2195 auth_key->use_count);
Klement Sekera73884482017-02-23 09:26:30 +01002196 return VNET_API_ERROR_BFD_EINUSE;
2197 }
2198 }
2199 else
2200 {
2201 /* adding new key */
2202 pool_get (bm->auth_keys, auth_key);
2203 auth_key->conf_key_id = conf_key_id;
2204 hash_set (bm->auth_key_by_conf_key_id, conf_key_id,
2205 auth_key - bm->auth_keys);
2206 }
2207 auth_key->auth_type = auth_type;
Dave Barachb7b92992018-10-17 10:38:51 -04002208 clib_memset (auth_key->key, 0, sizeof (auth_key->key));
Klement Sekera73884482017-02-23 09:26:30 +01002209 clib_memcpy (auth_key->key, key_data, key_len);
2210 return 0;
2211#else
Damjan Marion07a38572018-01-21 06:44:18 -08002212 vlib_log_err (bm->log_class,
2213 "SSL missing, cannot manipulate authentication keys");
Klement Sekera73884482017-02-23 09:26:30 +01002214 return VNET_API_ERROR_BFD_NOTSUPP;
2215#endif
2216}
2217
2218vnet_api_error_t
2219bfd_auth_del_key (u32 conf_key_id)
2220{
2221#if WITH_LIBSSL > 0
2222 bfd_auth_key_t *auth_key = NULL;
2223 bfd_main_t *bm = &bfd_main;
2224 uword *key_idx_p = hash_get (bm->auth_key_by_conf_key_id, conf_key_id);
2225 if (key_idx_p)
2226 {
2227 /* deleting existing key - must not be used */
2228 const uword key_idx = *key_idx_p;
2229 auth_key = pool_elt_at_index (bm->auth_keys, key_idx);
2230 if (auth_key->use_count > 0)
2231 {
Damjan Marion07a38572018-01-21 06:44:18 -08002232 vlib_log_err (bm->log_class,
2233 "authentication key with conf ID %u in use by %u BFD "
2234 "session(s) - cannot delete", conf_key_id,
2235 auth_key->use_count);
Klement Sekera73884482017-02-23 09:26:30 +01002236 return VNET_API_ERROR_BFD_EINUSE;
2237 }
2238 hash_unset (bm->auth_key_by_conf_key_id, conf_key_id);
Dave Barachb7b92992018-10-17 10:38:51 -04002239 clib_memset (auth_key, 0, sizeof (*auth_key));
Klement Sekera73884482017-02-23 09:26:30 +01002240 pool_put (bm->auth_keys, auth_key);
2241 }
2242 else
2243 {
2244 /* no such key */
Damjan Marion07a38572018-01-21 06:44:18 -08002245 vlib_log_err (bm->log_class,
2246 "authentication key with conf ID %u does not exist",
Klement Sekera73884482017-02-23 09:26:30 +01002247 conf_key_id);
2248 return VNET_API_ERROR_BFD_ENOENT;
2249 }
2250 return 0;
2251#else
Damjan Marion07a38572018-01-21 06:44:18 -08002252 vlib_log_err (bm->log_class,
2253 "SSL missing, cannot manipulate authentication keys");
Klement Sekera73884482017-02-23 09:26:30 +01002254 return VNET_API_ERROR_BFD_NOTSUPP;
2255#endif
2256}
2257
Klement Sekera0e3c0de2016-09-29 14:43:44 +02002258bfd_main_t bfd_main;
2259
2260/*
2261 * fd.io coding-style-patch-verification: ON
2262 *
2263 * Local Variables:
2264 * eval: (c-set-style "gnu")
2265 * End:
2266 */