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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * @brief BFD nodes implementation
21 #include <openssl/sha.h>
25 #include <x86intrin.h>
28 #include <vlibmemory/api.h>
29 #include <vppinfra/random.h>
30 #include <vppinfra/error.h>
31 #include <vppinfra/hash.h>
32 #include <vppinfra/xxhash.h>
33 #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>
49 bfd_calc_echo_checksum (u32 discriminator, u64 expire_time, u32 secret)
52 #if defined(clib_crc32c_uses_intrinsics) && !defined (__i386__)
53 checksum = crc32_u64 (0, discriminator);
54 checksum = crc32_u64 (checksum, expire_time);
55 checksum = crc32_u64 (checksum, secret);
57 checksum = clib_xxhash (discriminator ^ expire_time ^ secret);
63 bfd_usec_to_clocks (const bfd_main_t * bm, u64 us)
65 return bm->cpu_cps * ((f64) us / USEC_PER_SECOND);
69 bfd_clocks_to_usec (const bfd_main_t * bm, u64 clocks)
71 return (clocks / bm->cpu_cps) * USEC_PER_SECOND;
74 static vlib_node_registration_t bfd_process_node;
77 format_bfd_auth_key (u8 * s, va_list * args)
79 const bfd_auth_key_t *key = va_arg (*args, bfd_auth_key_t *);
82 s = format (s, "{auth-type=%u:%s, conf-key-id=%u, use-count=%u}, ",
83 key->auth_type, bfd_auth_type_str (key->auth_type),
84 key->conf_key_id, key->use_count);
88 s = format (s, "{none}");
94 * We actually send all bfd pkts to the "error" node after scanning
95 * them, so the graph node has only one next-index. The "error-drop"
96 * node automatically bumps our per-node packet counters for us.
100 BFD_INPUT_NEXT_NORMAL,
104 static void bfd_on_state_change (bfd_main_t * bm, bfd_session_t * bs, u64 now,
105 int handling_wakeup);
108 bfd_set_defaults (bfd_main_t * bm, bfd_session_t * bs)
110 bs->local_state = BFD_STATE_down;
111 bs->local_diag = BFD_DIAG_CODE_no_diag;
112 bs->remote_state = BFD_STATE_down;
113 bs->remote_discr = 0;
114 bs->hop_type = BFD_HOP_TYPE_SINGLE;
115 bs->config_desired_min_tx_usec = BFD_DEFAULT_DESIRED_MIN_TX_USEC;
116 bs->config_desired_min_tx_clocks = bm->default_desired_min_tx_clocks;
117 bs->effective_desired_min_tx_clocks = bm->default_desired_min_tx_clocks;
118 bs->remote_min_rx_usec = 1;
119 bs->remote_min_rx_clocks = bfd_usec_to_clocks (bm, bs->remote_min_rx_usec);
120 bs->remote_min_echo_rx_usec = 0;
121 bs->remote_min_echo_rx_clocks = 0;
122 bs->remote_demand = 0;
123 bs->auth.remote_seq_number = 0;
124 bs->auth.remote_seq_number_known = 0;
125 bs->auth.local_seq_number = random_u32 (&bm->random_seed);
126 bs->echo_secret = random_u32 (&bm->random_seed);
130 bfd_set_diag (bfd_session_t * bs, bfd_diag_code_e code)
132 if (bs->local_diag != code)
134 BFD_DBG ("set local_diag, bs_idx=%d: '%d:%s'", bs->bs_idx, code,
135 bfd_diag_code_string (code));
136 bs->local_diag = code;
141 bfd_set_state (bfd_main_t * bm, bfd_session_t * bs,
142 bfd_state_e new_state, int handling_wakeup)
144 if (bs->local_state != new_state)
146 BFD_DBG ("Change state, bs_idx=%d: %s->%s", bs->bs_idx,
147 bfd_state_string (bs->local_state),
148 bfd_state_string (new_state));
149 bs->local_state = new_state;
150 bfd_on_state_change (bm, bs, clib_cpu_time_now (), handling_wakeup);
155 bfd_poll_state_string (bfd_poll_state_e state)
161 return "BFD_POLL_" #x;
162 foreach_bfd_poll_state (F)
169 bfd_set_poll_state (bfd_session_t * bs, bfd_poll_state_e state)
171 if (bs->poll_state != state)
173 BFD_DBG ("Setting poll state=%s, bs_idx=%u",
174 bfd_poll_state_string (state), bs->bs_idx);
175 bs->poll_state = state;
180 bfd_recalc_tx_interval (bfd_main_t * bm, bfd_session_t * bs)
182 bs->transmit_interval_clocks =
183 clib_max (bs->effective_desired_min_tx_clocks, bs->remote_min_rx_clocks);
184 BFD_DBG ("Recalculated transmit interval " BFD_CLK_FMT,
185 BFD_CLK_PRN (bs->transmit_interval_clocks));
189 bfd_recalc_echo_tx_interval (bfd_main_t * bm, bfd_session_t * bs)
191 bs->echo_transmit_interval_clocks =
192 clib_max (bs->effective_desired_min_tx_clocks,
193 bs->remote_min_echo_rx_clocks);
194 BFD_DBG ("Recalculated echo transmit interval " BFD_CLK_FMT,
195 BFD_CLK_PRN (bs->echo_transmit_interval_clocks));
199 bfd_calc_next_tx (bfd_main_t * bm, bfd_session_t * bs, u64 now)
201 if (bs->local_detect_mult > 1)
203 /* common case - 75-100% of transmit interval */
204 bs->tx_timeout_clocks = bs->last_tx_clocks +
205 (1 - .25 * (random_f64 (&bm->random_seed))) *
206 bs->transmit_interval_clocks;
207 if (bs->tx_timeout_clocks < now)
210 * the timeout is in the past, which means that either remote
211 * demand mode was set or performance/clock issues ...
213 BFD_DBG ("Missed %lu transmit events (now is %lu, calc "
214 "tx_timeout is %lu)",
215 (now - bs->tx_timeout_clocks) /
216 bs->transmit_interval_clocks, now, bs->tx_timeout_clocks);
217 bs->tx_timeout_clocks = now;
222 /* special case - 75-90% of transmit interval */
223 bs->tx_timeout_clocks = bs->last_tx_clocks +
224 (.9 - .15 * (random_f64 (&bm->random_seed))) *
225 bs->transmit_interval_clocks;
226 if (bs->tx_timeout_clocks < now)
229 * the timeout is in the past, which means that either remote
230 * demand mode was set or performance/clock issues ...
232 BFD_DBG ("Missed %lu transmit events (now is %lu, calc "
233 "tx_timeout is %lu)",
234 (now - bs->tx_timeout_clocks) /
235 bs->transmit_interval_clocks, now, bs->tx_timeout_clocks);
236 bs->tx_timeout_clocks = now;
239 if (bs->tx_timeout_clocks)
241 BFD_DBG ("Next transmit in %lu clocks/%.02fs@%lu",
242 bs->tx_timeout_clocks - now,
243 (bs->tx_timeout_clocks - now) / bm->cpu_cps,
244 bs->tx_timeout_clocks);
249 bfd_calc_next_echo_tx (bfd_main_t * bm, bfd_session_t * bs, u64 now)
251 bs->echo_tx_timeout_clocks =
252 bs->echo_last_tx_clocks + bs->echo_transmit_interval_clocks;
253 if (bs->echo_tx_timeout_clocks < now)
255 /* huh, we've missed it already, transmit now */
256 BFD_DBG ("Missed %lu echo transmit events (now is %lu, calc tx_timeout "
258 (now - bs->echo_tx_timeout_clocks) /
259 bs->echo_transmit_interval_clocks,
260 now, bs->echo_tx_timeout_clocks);
261 bs->echo_tx_timeout_clocks = now;
263 BFD_DBG ("Next echo transmit in %lu clocks/%.02fs@%lu",
264 bs->echo_tx_timeout_clocks - now,
265 (bs->echo_tx_timeout_clocks - now) / bm->cpu_cps,
266 bs->echo_tx_timeout_clocks);
270 bfd_recalc_detection_time (bfd_main_t * bm, bfd_session_t * bs)
272 if (bs->local_state == BFD_STATE_init || bs->local_state == BFD_STATE_up)
274 bs->detection_time_clocks =
275 bs->remote_detect_mult *
276 clib_max (bs->effective_required_min_rx_clocks,
277 bs->remote_desired_min_tx_clocks);
278 BFD_DBG ("Recalculated detection time %lu clocks/%.2fs",
279 bs->detection_time_clocks,
280 bs->detection_time_clocks / bm->cpu_cps);
285 bfd_set_timer (bfd_main_t * bm, bfd_session_t * bs, u64 now,
291 if (BFD_STATE_up == bs->local_state)
293 rx_timeout = bs->last_rx_clocks + bs->detection_time_clocks;
295 if (BFD_STATE_up != bs->local_state ||
296 (!bs->remote_demand && bs->remote_min_rx_usec) ||
297 BFD_POLL_NOT_NEEDED != bs->poll_state)
299 tx_timeout = bs->tx_timeout_clocks;
301 if (tx_timeout && rx_timeout)
303 next = clib_min (tx_timeout, rx_timeout);
313 if (bs->echo && next > bs->echo_tx_timeout_clocks)
315 next = bs->echo_tx_timeout_clocks;
317 BFD_DBG ("bs_idx=%u, tx_timeout=%lu, echo_tx_timeout=%lu, rx_timeout=%lu, "
319 bs->bs_idx, tx_timeout, bs->echo_tx_timeout_clocks, rx_timeout,
321 ? "tx" : (next == bs->echo_tx_timeout_clocks ? "echo tx" : "rx"));
322 /* sometimes the wheel expires an event a bit sooner than requested, account
324 if (next && (now + bm->wheel_inaccuracy > bs->wheel_time_clocks ||
325 next < bs->wheel_time_clocks || !bs->wheel_time_clocks))
328 bs->wheel_time_clocks = next;
329 BFD_DBG ("timing_wheel_insert(%p, %lu (%ld clocks/%.2fs in the "
331 &bm->wheel, bs->wheel_time_clocks,
332 (i64) bs->wheel_time_clocks - clib_cpu_time_now (),
333 (i64) (bs->wheel_time_clocks - clib_cpu_time_now ()) /
334 bm->cpu_cps, bs->bs_idx);
336 timing_wheel_insert (&bm->wheel, bs->wheel_time_clocks, bs->bs_idx);
338 if (!handling_wakeup)
341 /* Send only if it is earlier than current awaited wakeup time */
343 (bs->wheel_time_clocks < bm->bfd_process_next_wakeup_clocks) &&
345 * If the wake-up time is within 2x the delay of the event propagation delay,
346 * avoid the expense of sending the event. The 2x multiplier is to workaround the race whereby
347 * simultaneous event + expired timer create one recurring bogus wakeup/suspend instance,
348 * due to double scheduling of the node on the pending list.
350 (bm->bfd_process_next_wakeup_clocks - bs->wheel_time_clocks >
351 2 * bm->bfd_process_wakeup_event_delay_clocks) &&
352 /* Must be no events in flight to send an event */
353 (!bm->bfd_process_wakeup_events_in_flight);
355 /* If we do send the signal, note this down along with the start timestamp */
358 bm->bfd_process_wakeup_events_in_flight++;
359 bm->bfd_process_wakeup_event_start_clocks = now;
364 /* Use the multithreaded event sending so the workers can send events too */
367 vlib_process_signal_event_mt (bm->vlib_main,
368 bm->bfd_process_node_index,
369 BFD_EVENT_RESCHEDULE, ~0);
375 bfd_set_effective_desired_min_tx (bfd_main_t * bm,
376 bfd_session_t * bs, u64 now,
377 u64 desired_min_tx_clocks)
379 bs->effective_desired_min_tx_clocks = desired_min_tx_clocks;
380 BFD_DBG ("Set effective desired min tx to " BFD_CLK_FMT,
381 BFD_CLK_PRN (bs->effective_desired_min_tx_clocks));
382 bfd_recalc_detection_time (bm, bs);
383 bfd_recalc_tx_interval (bm, bs);
384 bfd_recalc_echo_tx_interval (bm, bs);
385 bfd_calc_next_tx (bm, bs, now);
389 bfd_set_effective_required_min_rx (bfd_main_t * bm,
391 u64 required_min_rx_clocks)
393 bs->effective_required_min_rx_clocks = required_min_rx_clocks;
394 BFD_DBG ("Set effective required min rx to " BFD_CLK_FMT,
395 BFD_CLK_PRN (bs->effective_required_min_rx_clocks));
396 bfd_recalc_detection_time (bm, bs);
400 bfd_set_remote_required_min_rx (bfd_main_t * bm, bfd_session_t * bs,
401 u64 now, u32 remote_required_min_rx_usec)
403 if (bs->remote_min_rx_usec != remote_required_min_rx_usec)
405 bs->remote_min_rx_usec = remote_required_min_rx_usec;
406 bs->remote_min_rx_clocks =
407 bfd_usec_to_clocks (bm, remote_required_min_rx_usec);
408 BFD_DBG ("Set remote min rx to " BFD_CLK_FMT,
409 BFD_CLK_PRN (bs->remote_min_rx_clocks));
410 bfd_recalc_detection_time (bm, bs);
411 bfd_recalc_tx_interval (bm, bs);
416 bfd_set_remote_required_min_echo_rx (bfd_main_t * bm, bfd_session_t * bs,
418 u32 remote_required_min_echo_rx_usec)
420 if (bs->remote_min_echo_rx_usec != remote_required_min_echo_rx_usec)
422 bs->remote_min_echo_rx_usec = remote_required_min_echo_rx_usec;
423 bs->remote_min_echo_rx_clocks =
424 bfd_usec_to_clocks (bm, bs->remote_min_echo_rx_usec);
425 BFD_DBG ("Set remote min echo rx to " BFD_CLK_FMT,
426 BFD_CLK_PRN (bs->remote_min_echo_rx_clocks));
427 bfd_recalc_echo_tx_interval (bm, bs);
432 bfd_notify_listeners (bfd_main_t * bm,
433 bfd_listen_event_e event, const bfd_session_t * bs)
436 vec_foreach (fn, bm->listeners)
443 bfd_session_start (bfd_main_t * bm, bfd_session_t * bs)
445 BFD_DBG ("\nStarting session: %U", format_bfd_session, bs);
446 vlib_log_info (bm->log_class, "start BFD session: %U",
447 format_bfd_session_brief, bs);
448 bfd_set_effective_required_min_rx (bm, bs,
449 bs->config_required_min_rx_clocks);
450 bfd_recalc_tx_interval (bm, bs);
451 vlib_process_signal_event (bm->vlib_main, bm->bfd_process_node_index,
452 BFD_EVENT_NEW_SESSION, bs->bs_idx);
453 bfd_notify_listeners (bm, BFD_LISTEN_EVENT_CREATE, bs);
457 bfd_session_set_flags (bfd_session_t * bs, u8 admin_up_down)
459 bfd_main_t *bm = &bfd_main;
460 u64 now = clib_cpu_time_now ();
463 BFD_DBG ("Session set admin-up, bs-idx=%u", bs->bs_idx);
464 vlib_log_info (bm->log_class, "set session admin-up: %U",
465 format_bfd_session_brief, bs);
466 bfd_set_state (bm, bs, BFD_STATE_down, 0);
467 bfd_set_diag (bs, BFD_DIAG_CODE_no_diag);
468 bfd_calc_next_tx (bm, bs, now);
469 bfd_set_timer (bm, bs, now, 0);
473 BFD_DBG ("Session set admin-down, bs-idx=%u", bs->bs_idx);
474 vlib_log_info (bm->log_class, "set session admin-down: %U",
475 format_bfd_session_brief, bs);
476 bfd_set_diag (bs, BFD_DIAG_CODE_admin_down);
477 bfd_set_state (bm, bs, BFD_STATE_admin_down, 0);
478 bfd_calc_next_tx (bm, bs, now);
479 bfd_set_timer (bm, bs, now, 0);
484 bfd_input_format_trace (u8 * s, va_list * args)
486 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
487 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
488 const bfd_input_trace_t *t = va_arg (*args, bfd_input_trace_t *);
489 const bfd_pkt_t *pkt = (bfd_pkt_t *) t->data;
490 if (t->len > STRUCT_SIZE_OF (bfd_pkt_t, head))
492 s = format (s, "BFD v%u, diag=%u(%s), state=%u(%s),\n"
493 " flags=(P:%u, F:%u, C:%u, A:%u, D:%u, M:%u), "
494 "detect_mult=%u, length=%u\n",
495 bfd_pkt_get_version (pkt), bfd_pkt_get_diag_code (pkt),
496 bfd_diag_code_string (bfd_pkt_get_diag_code (pkt)),
497 bfd_pkt_get_state (pkt),
498 bfd_state_string (bfd_pkt_get_state (pkt)),
499 bfd_pkt_get_poll (pkt), bfd_pkt_get_final (pkt),
500 bfd_pkt_get_control_plane_independent (pkt),
501 bfd_pkt_get_auth_present (pkt), bfd_pkt_get_demand (pkt),
502 bfd_pkt_get_multipoint (pkt), pkt->head.detect_mult,
504 if (t->len >= sizeof (bfd_pkt_t) &&
505 pkt->head.length >= sizeof (bfd_pkt_t))
507 s = format (s, " my discriminator: %u\n",
508 clib_net_to_host_u32 (pkt->my_disc));
509 s = format (s, " your discriminator: %u\n",
510 clib_net_to_host_u32 (pkt->your_disc));
511 s = format (s, " desired min tx interval: %u\n",
512 clib_net_to_host_u32 (pkt->des_min_tx));
513 s = format (s, " required min rx interval: %u\n",
514 clib_net_to_host_u32 (pkt->req_min_rx));
515 s = format (s, " required min echo rx interval: %u",
516 clib_net_to_host_u32 (pkt->req_min_echo_rx));
518 if (t->len >= sizeof (bfd_pkt_with_common_auth_t) &&
519 pkt->head.length >= sizeof (bfd_pkt_with_common_auth_t) &&
520 bfd_pkt_get_auth_present (pkt))
522 const bfd_pkt_with_common_auth_t *with_auth = (void *) pkt;
523 const bfd_auth_common_t *common = &with_auth->common_auth;
524 s = format (s, "\n auth len: %u\n", common->len);
525 s = format (s, " auth type: %u:%s\n", common->type,
526 bfd_auth_type_str (common->type));
527 if (t->len >= sizeof (bfd_pkt_with_sha1_auth_t) &&
528 pkt->head.length >= sizeof (bfd_pkt_with_sha1_auth_t) &&
529 (BFD_AUTH_TYPE_keyed_sha1 == common->type ||
530 BFD_AUTH_TYPE_meticulous_keyed_sha1 == common->type))
532 const bfd_pkt_with_sha1_auth_t *with_sha1 = (void *) pkt;
533 const bfd_auth_sha1_t *sha1 = &with_sha1->sha1_auth;
534 s = format (s, " seq num: %u\n",
535 clib_net_to_host_u32 (sha1->seq_num));
536 s = format (s, " key id: %u\n", sha1->key_id);
537 s = format (s, " hash: %U", format_hex_bytes, sha1->hash,
538 sizeof (sha1->hash));
543 s = format (s, "\n");
556 bfd_rpc_event_cb (const bfd_rpc_event_t * a)
558 bfd_main_t *bm = &bfd_main;
559 u32 bs_idx = a->bs_idx;
561 bfd_session_t session_data;
564 if (!pool_is_free_index (bm->sessions, bs_idx))
566 bfd_session_t *bs = pool_elt_at_index (bm->sessions, bs_idx);
567 clib_memcpy (&session_data, bs, sizeof (bfd_session_t));
572 BFD_DBG ("Ignoring event RPC for non-existent session index %u",
578 bfd_event (bm, &session_data);
582 bfd_event_rpc (u32 bs_idx)
584 const u32 data_size = sizeof (bfd_rpc_event_t);
586 bfd_rpc_event_t *event = (bfd_rpc_event_t *) data;
588 event->bs_idx = bs_idx;
589 vl_api_rpc_call_main_thread (bfd_rpc_event_cb, data, data_size);
595 } bfd_rpc_notify_listeners_t;
598 bfd_rpc_notify_listeners_cb (const bfd_rpc_notify_listeners_t * a)
600 bfd_main_t *bm = &bfd_main;
601 u32 bs_idx = a->bs_idx;
603 if (!pool_is_free_index (bm->sessions, bs_idx))
605 bfd_session_t *bs = pool_elt_at_index (bm->sessions, bs_idx);
606 bfd_notify_listeners (bm, BFD_LISTEN_EVENT_UPDATE, bs);
610 BFD_DBG ("Ignoring notify RPC for non-existent session index %u",
617 bfd_notify_listeners_rpc (u32 bs_idx)
619 const u32 data_size = sizeof (bfd_rpc_notify_listeners_t);
621 bfd_rpc_notify_listeners_t *notify = (bfd_rpc_notify_listeners_t *) data;
622 notify->bs_idx = bs_idx;
623 vl_api_rpc_call_main_thread (bfd_rpc_notify_listeners_cb, data, data_size);
627 bfd_on_state_change (bfd_main_t * bm, bfd_session_t * bs, u64 now,
630 BFD_DBG ("\nState changed: %U", format_bfd_session, bs);
632 if (vlib_get_thread_index () == 0)
638 /* without RPC - a REGRESSION: BFD event are not propagated */
639 bfd_event_rpc (bs->bs_idx);
642 switch (bs->local_state)
644 case BFD_STATE_admin_down:
646 bfd_set_effective_desired_min_tx (bm, bs, now,
648 (bs->config_desired_min_tx_clocks,
649 bm->default_desired_min_tx_clocks));
650 bfd_set_effective_required_min_rx (bm, bs,
651 bs->config_required_min_rx_clocks);
652 bfd_set_timer (bm, bs, now, handling_wakeup);
656 bfd_set_effective_desired_min_tx (bm, bs, now,
658 (bs->config_desired_min_tx_clocks,
659 bm->default_desired_min_tx_clocks));
660 bfd_set_effective_required_min_rx (bm, bs,
661 bs->config_required_min_rx_clocks);
662 bfd_set_timer (bm, bs, now, handling_wakeup);
666 bfd_set_effective_desired_min_tx (bm, bs, now,
667 bs->config_desired_min_tx_clocks);
668 bfd_set_timer (bm, bs, now, handling_wakeup);
671 bfd_set_effective_desired_min_tx (bm, bs, now,
672 bs->config_desired_min_tx_clocks);
673 if (BFD_POLL_NOT_NEEDED == bs->poll_state)
675 bfd_set_effective_required_min_rx (bm, bs,
676 bs->config_required_min_rx_clocks);
678 bfd_set_timer (bm, bs, now, handling_wakeup);
681 if (vlib_get_thread_index () == 0)
683 bfd_notify_listeners (bm, BFD_LISTEN_EVENT_UPDATE, bs);
687 /* without RPC - a REGRESSION: state changes are not propagated */
688 bfd_notify_listeners_rpc (bs->bs_idx);
693 bfd_on_config_change (vlib_main_t * vm, vlib_node_runtime_t * rt,
694 bfd_main_t * bm, bfd_session_t * bs, u64 now)
697 * if remote demand mode is set and we need to do a poll, set the next
698 * timeout so that the session wakes up immediately
700 if (bs->remote_demand && BFD_POLL_NEEDED == bs->poll_state &&
701 bs->poll_state_start_or_timeout_clocks < now)
703 bs->tx_timeout_clocks = now;
705 bfd_recalc_detection_time (bm, bs);
706 bfd_set_timer (bm, bs, now, 0);
710 bfd_add_transport_layer (vlib_main_t * vm, u32 bi, bfd_session_t * bs)
712 switch (bs->transport)
714 case BFD_TRANSPORT_UDP4:
715 BFD_DBG ("Transport bfd via udp4, bs_idx=%u", bs->bs_idx);
716 bfd_add_udp4_transport (vm, bi, bs, 0 /* is_echo */ );
718 case BFD_TRANSPORT_UDP6:
719 BFD_DBG ("Transport bfd via udp6, bs_idx=%u", bs->bs_idx);
720 bfd_add_udp6_transport (vm, bi, bs, 0 /* is_echo */ );
726 bfd_transport_control_frame (vlib_main_t * vm, u32 bi, bfd_session_t * bs)
728 switch (bs->transport)
730 case BFD_TRANSPORT_UDP4:
731 BFD_DBG ("Transport bfd via udp4, bs_idx=%u", bs->bs_idx);
732 return bfd_transport_udp4 (vm, bi, bs);
734 case BFD_TRANSPORT_UDP6:
735 BFD_DBG ("Transport bfd via udp6, bs_idx=%u", bs->bs_idx);
736 return bfd_transport_udp6 (vm, bi, bs);
743 bfd_echo_add_transport_layer (vlib_main_t * vm, u32 bi, bfd_session_t * bs)
745 switch (bs->transport)
747 case BFD_TRANSPORT_UDP4:
748 BFD_DBG ("Transport bfd echo via udp4, bs_idx=%u", bs->bs_idx);
749 return bfd_add_udp4_transport (vm, bi, bs, 1 /* is_echo */ );
751 case BFD_TRANSPORT_UDP6:
752 BFD_DBG ("Transport bfd echo via udp6, bs_idx=%u", bs->bs_idx);
753 return bfd_add_udp6_transport (vm, bi, bs, 1 /* is_echo */ );
760 bfd_transport_echo (vlib_main_t * vm, u32 bi, bfd_session_t * bs)
762 switch (bs->transport)
764 case BFD_TRANSPORT_UDP4:
765 BFD_DBG ("Transport bfd echo via udp4, bs_idx=%u", bs->bs_idx);
766 return bfd_transport_udp4 (vm, bi, bs);
768 case BFD_TRANSPORT_UDP6:
769 BFD_DBG ("Transport bfd echo via udp6, bs_idx=%u", bs->bs_idx);
770 return bfd_transport_udp6 (vm, bi, bs);
778 bfd_add_sha1_auth_section (vlib_buffer_t * b, bfd_session_t * bs)
780 bfd_pkt_with_sha1_auth_t *pkt = vlib_buffer_get_current (b);
781 bfd_auth_sha1_t *auth = &pkt->sha1_auth;
782 b->current_length += sizeof (*auth);
783 pkt->pkt.head.length += sizeof (*auth);
784 bfd_pkt_set_auth_present (&pkt->pkt);
785 memset (auth, 0, sizeof (*auth));
786 auth->type_len.type = bs->auth.curr_key->auth_type;
788 * only meticulous authentication types require incrementing seq number
789 * for every message, but doing so doesn't violate the RFC
791 ++bs->auth.local_seq_number;
792 auth->type_len.len = sizeof (bfd_auth_sha1_t);
793 auth->key_id = bs->auth.curr_bfd_key_id;
794 auth->seq_num = clib_host_to_net_u32 (bs->auth.local_seq_number);
796 * first copy the password into the packet, then calculate the hash
797 * and finally replace the password with the calculated hash
799 clib_memcpy (auth->hash, bs->auth.curr_key->key,
800 sizeof (bs->auth.curr_key->key));
801 unsigned char hash[sizeof (auth->hash)];
802 SHA1 ((unsigned char *) pkt, sizeof (*pkt), hash);
803 BFD_DBG ("hashing: %U", format_hex_bytes, pkt, sizeof (*pkt));
804 clib_memcpy (auth->hash, hash, sizeof (hash));
809 bfd_add_auth_section (vlib_buffer_t * b, bfd_session_t * bs)
811 bfd_main_t *bm = &bfd_main;
812 if (bs->auth.curr_key)
814 const bfd_auth_type_e auth_type = bs->auth.curr_key->auth_type;
817 case BFD_AUTH_TYPE_reserved:
819 case BFD_AUTH_TYPE_simple_password:
821 case BFD_AUTH_TYPE_keyed_md5:
823 case BFD_AUTH_TYPE_meticulous_keyed_md5:
824 vlib_log_crit (bm->log_class,
825 "internal error, unexpected BFD auth type '%d'",
829 case BFD_AUTH_TYPE_keyed_sha1:
831 case BFD_AUTH_TYPE_meticulous_keyed_sha1:
832 bfd_add_sha1_auth_section (b, bs);
835 case BFD_AUTH_TYPE_keyed_sha1:
837 case BFD_AUTH_TYPE_meticulous_keyed_sha1:
838 vlib_log_crit (bm->log_class,
839 "internal error, unexpected BFD auth type '%d'",
848 bfd_is_echo_possible (bfd_session_t * bs)
850 if (BFD_STATE_up == bs->local_state && BFD_STATE_up == bs->remote_state &&
851 bs->remote_min_echo_rx_usec > 0)
853 switch (bs->transport)
855 case BFD_TRANSPORT_UDP4:
856 return bfd_udp_is_echo_available (BFD_TRANSPORT_UDP4);
857 case BFD_TRANSPORT_UDP6:
858 return bfd_udp_is_echo_available (BFD_TRANSPORT_UDP6);
865 bfd_init_control_frame (bfd_main_t * bm, bfd_session_t * bs,
868 bfd_pkt_t *pkt = vlib_buffer_get_current (b);
870 bfd_length = sizeof (bfd_pkt_t);
871 memset (pkt, 0, sizeof (*pkt));
872 bfd_pkt_set_version (pkt, 1);
873 bfd_pkt_set_diag_code (pkt, bs->local_diag);
874 bfd_pkt_set_state (pkt, bs->local_state);
875 pkt->head.detect_mult = bs->local_detect_mult;
876 pkt->head.length = bfd_length;
877 pkt->my_disc = bs->local_discr;
878 pkt->your_disc = bs->remote_discr;
879 pkt->des_min_tx = clib_host_to_net_u32 (bs->config_desired_min_tx_usec);
883 clib_host_to_net_u32 (bfd_clocks_to_usec
884 (bm, bs->effective_required_min_rx_clocks));
889 clib_host_to_net_u32 (bs->config_required_min_rx_usec);
891 pkt->req_min_echo_rx = clib_host_to_net_u32 (1);
892 b->current_length = bfd_length;
896 bfd_send_echo (vlib_main_t * vm, vlib_node_runtime_t * rt,
897 bfd_main_t * bm, bfd_session_t * bs, u64 now)
899 if (!bfd_is_echo_possible (bs))
901 BFD_DBG ("\nSwitching off echo function: %U", format_bfd_session, bs);
905 /* sometimes the wheel expires an event a bit sooner than requested,
908 if (now + bm->wheel_inaccuracy >= bs->echo_tx_timeout_clocks)
910 BFD_DBG ("\nSending echo packet: %U", format_bfd_session, bs);
912 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
914 vlib_log_crit (bm->log_class, "buffer allocation failure");
917 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
918 ASSERT (b->current_data == 0);
919 memset (vnet_buffer (b), 0, sizeof (*vnet_buffer (b)));
920 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b);
921 bfd_echo_pkt_t *pkt = vlib_buffer_get_current (b);
922 memset (pkt, 0, sizeof (*pkt));
923 pkt->discriminator = bs->local_discr;
924 pkt->expire_time_clocks =
925 now + bs->echo_transmit_interval_clocks * bs->local_detect_mult;
927 bfd_calc_echo_checksum (bs->local_discr, pkt->expire_time_clocks,
929 b->current_length = sizeof (*pkt);
930 if (!bfd_echo_add_transport_layer (vm, bi, bs))
932 BFD_ERR ("cannot send echo packet out, turning echo off");
934 vlib_buffer_free_one (vm, bi);
937 if (!bfd_transport_echo (vm, bi, bs))
939 BFD_ERR ("cannot send echo packet out, turning echo off");
941 vlib_buffer_free_one (vm, bi);
944 bs->echo_last_tx_clocks = now;
945 bfd_calc_next_echo_tx (bm, bs, now);
950 ("No need to send echo packet now, now is %lu, tx_timeout is %lu",
951 now, bs->echo_tx_timeout_clocks);
956 bfd_send_periodic (vlib_main_t * vm, vlib_node_runtime_t * rt,
957 bfd_main_t * bm, bfd_session_t * bs, u64 now)
959 if (!bs->remote_min_rx_usec && BFD_POLL_NOT_NEEDED == bs->poll_state)
961 BFD_DBG ("Remote min rx interval is zero, not sending periodic control "
965 if (BFD_POLL_NOT_NEEDED == bs->poll_state && bs->remote_demand &&
966 BFD_STATE_up == bs->local_state && BFD_STATE_up == bs->remote_state)
969 * A system MUST NOT periodically transmit BFD Control packets if Demand
970 * mode is active on the remote system (bfd.RemoteDemandMode is 1,
971 * bfd.SessionState is Up, and bfd.RemoteSessionState is Up) and a Poll
972 * Sequence is not being transmitted.
974 BFD_DBG ("Remote demand is set, not sending periodic control frame");
978 * sometimes the wheel expires an event a bit sooner than requested, account
981 if (now + bm->wheel_inaccuracy >= bs->tx_timeout_clocks)
983 BFD_DBG ("\nSending periodic control frame: %U", format_bfd_session,
986 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
988 vlib_log_crit (bm->log_class, "buffer allocation failure");
991 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
992 ASSERT (b->current_data == 0);
993 memset (vnet_buffer (b), 0, sizeof (*vnet_buffer (b)));
994 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b);
995 bfd_init_control_frame (bm, bs, b);
996 switch (bs->poll_state)
998 case BFD_POLL_NEEDED:
999 if (now < bs->poll_state_start_or_timeout_clocks)
1001 BFD_DBG ("Cannot start a poll sequence yet, need to wait "
1003 BFD_CLK_PRN (bs->poll_state_start_or_timeout_clocks -
1007 bs->poll_state_start_or_timeout_clocks = now;
1008 bfd_set_poll_state (bs, BFD_POLL_IN_PROGRESS);
1010 case BFD_POLL_IN_PROGRESS:
1011 case BFD_POLL_IN_PROGRESS_AND_QUEUED:
1012 bfd_pkt_set_poll (vlib_buffer_get_current (b));
1013 BFD_DBG ("Setting poll bit in packet, bs_idx=%u", bs->bs_idx);
1015 case BFD_POLL_NOT_NEEDED:
1019 bfd_add_auth_section (b, bs);
1020 bfd_add_transport_layer (vm, bi, bs);
1021 if (!bfd_transport_control_frame (vm, bi, bs))
1023 vlib_buffer_free_one (vm, bi);
1025 bs->last_tx_clocks = now;
1026 bfd_calc_next_tx (bm, bs, now);
1031 ("No need to send control frame now, now is %lu, tx_timeout is %lu",
1032 now, bs->tx_timeout_clocks);
1037 bfd_init_final_control_frame (vlib_main_t * vm, vlib_buffer_t * b,
1038 bfd_main_t * bm, bfd_session_t * bs,
1041 BFD_DBG ("Send final control frame for bs_idx=%lu", bs->bs_idx);
1042 bfd_init_control_frame (bm, bs, b);
1043 bfd_pkt_set_final (vlib_buffer_get_current (b));
1044 bfd_add_auth_section (b, bs);
1045 u32 bi = vlib_get_buffer_index (vm, b);
1046 bfd_add_transport_layer (vm, bi, bs);
1047 bs->last_tx_clocks = clib_cpu_time_now ();
1049 * RFC allows to include changes in final frame, so if there were any
1050 * pending, we already did that, thus we can clear any pending poll needs
1052 bfd_set_poll_state (bs, BFD_POLL_NOT_NEEDED);
1056 bfd_check_rx_timeout (bfd_main_t * bm, bfd_session_t * bs, u64 now,
1057 int handling_wakeup)
1060 * sometimes the wheel expires an event a bit sooner than requested, account
1063 if (bs->last_rx_clocks + bs->detection_time_clocks <=
1064 now + bm->wheel_inaccuracy)
1066 BFD_DBG ("Rx timeout, session goes down");
1067 bfd_set_diag (bs, BFD_DIAG_CODE_det_time_exp);
1068 bfd_set_state (bm, bs, BFD_STATE_down, handling_wakeup);
1070 * If the remote system does not receive any
1071 * BFD Control packets for a Detection Time, it SHOULD reset
1072 * bfd.RemoteMinRxInterval to its initial value of 1 (per section 6.8.1,
1073 * since it is no longer required to maintain previous session state)
1074 * and then can transmit at its own rate.
1076 bfd_set_remote_required_min_rx (bm, bs, now, 1);
1078 else if (bs->echo &&
1079 bs->echo_last_rx_clocks +
1080 bs->echo_transmit_interval_clocks * bs->local_detect_mult <=
1081 now + bm->wheel_inaccuracy)
1083 BFD_DBG ("Echo rx timeout, session goes down");
1084 bfd_set_diag (bs, BFD_DIAG_CODE_echo_failed);
1085 bfd_set_state (bm, bs, BFD_STATE_down, handling_wakeup);
1090 bfd_on_timeout (vlib_main_t * vm, vlib_node_runtime_t * rt, bfd_main_t * bm,
1091 bfd_session_t * bs, u64 now)
1093 BFD_DBG ("Timeout for bs_idx=%lu", bs->bs_idx);
1094 switch (bs->local_state)
1096 case BFD_STATE_admin_down:
1097 bfd_send_periodic (vm, rt, bm, bs, now);
1099 case BFD_STATE_down:
1100 bfd_send_periodic (vm, rt, bm, bs, now);
1102 case BFD_STATE_init:
1103 bfd_check_rx_timeout (bm, bs, now, 1);
1104 bfd_send_periodic (vm, rt, bm, bs, now);
1107 bfd_check_rx_timeout (bm, bs, now, 1);
1108 if (BFD_POLL_NOT_NEEDED == bs->poll_state && !bs->echo &&
1109 bfd_is_echo_possible (bs))
1111 /* switch on echo function as main detection method now */
1112 BFD_DBG ("Switching on echo function, bs_idx=%u", bs->bs_idx);
1114 bs->echo_last_rx_clocks = now;
1115 bs->echo_tx_timeout_clocks = now;
1116 bfd_set_effective_required_min_rx (bm, bs,
1118 (bm->min_required_min_rx_while_echo_clocks,
1119 bs->config_required_min_rx_clocks));
1120 bfd_set_poll_state (bs, BFD_POLL_NEEDED);
1122 bfd_send_periodic (vm, rt, bm, bs, now);
1125 bfd_send_echo (vm, rt, bm, bs, now);
1132 * bfd process node function
1135 bfd_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
1137 bfd_main_t *bm = &bfd_main;
1139 uword event_type, *event_data = 0;
1141 /* So we can send events to the bfd process */
1142 bm->bfd_process_node_index = bfd_process_node.index;
1146 u64 now = clib_cpu_time_now ();
1148 u64 next_expire = timing_wheel_next_expiring_elt_time (&bm->wheel);
1149 BFD_DBG ("timing_wheel_next_expiring_elt_time(%p) returns %lu",
1150 &bm->wheel, next_expire);
1151 bm->bfd_process_next_wakeup_clocks =
1152 (i64) next_expire >= 0 ? next_expire : ~0;
1154 if ((i64) next_expire < 0)
1156 BFD_DBG ("wait for event without timeout");
1157 (void) vlib_process_wait_for_event (vm);
1158 event_type = vlib_process_get_events (vm, &event_data);
1162 f64 timeout = ((i64) next_expire - (i64) now) / bm->cpu_cps;
1163 BFD_DBG ("wait for event with timeout %.02f", timeout);
1166 BFD_DBG ("negative timeout, already expired, skipping wait");
1171 (void) vlib_process_wait_for_event_or_clock (vm, timeout);
1172 event_type = vlib_process_get_events (vm, &event_data);
1175 now = clib_cpu_time_now ();
1178 case ~0: /* no events => timeout */
1179 /* nothing to do here */
1181 case BFD_EVENT_RESCHEDULE:
1183 bm->bfd_process_wakeup_event_delay_clocks =
1184 now - bm->bfd_process_wakeup_event_start_clocks;
1185 bm->bfd_process_wakeup_events_in_flight--;
1187 /* nothing to do here - reschedule is done automatically after
1188 * each event or timeout */
1190 case BFD_EVENT_NEW_SESSION:
1192 if (!pool_is_free_index (bm->sessions, *event_data))
1195 pool_elt_at_index (bm->sessions, *event_data);
1196 bfd_send_periodic (vm, rt, bm, bs, now);
1197 bfd_set_timer (bm, bs, now, 1);
1201 BFD_DBG ("Ignoring event for non-existent session index %u",
1202 (u32) * event_data);
1206 case BFD_EVENT_CONFIG_CHANGED:
1208 if (!pool_is_free_index (bm->sessions, *event_data))
1211 pool_elt_at_index (bm->sessions, *event_data);
1212 bfd_on_config_change (vm, rt, bm, bs, now);
1216 BFD_DBG ("Ignoring event for non-existent session index %u",
1217 (u32) * event_data);
1222 vlib_log_err (bm->log_class, "BUG: event type 0x%wx", event_type);
1225 BFD_DBG ("advancing wheel, now is %lu", now);
1226 BFD_DBG ("timing_wheel_advance (%p, %lu, %p, 0);", &bm->wheel, now,
1229 expired = timing_wheel_advance (&bm->wheel, now, expired, 0);
1230 BFD_DBG ("Expired %d elements", vec_len (expired));
1232 vec_foreach (p, expired)
1234 const u32 bs_idx = *p;
1235 if (!pool_is_free_index (bm->sessions, bs_idx))
1237 bfd_session_t *bs = pool_elt_at_index (bm->sessions, bs_idx);
1238 bfd_on_timeout (vm, rt, bm, bs, now);
1239 bfd_set_timer (bm, bs, now, 1);
1245 _vec_len (expired) = 0;
1249 _vec_len (event_data) = 0;
1257 * bfd process node declaration
1260 VLIB_REGISTER_NODE (bfd_process_node, static) = {
1261 .function = bfd_process,
1262 .type = VLIB_NODE_TYPE_PROCESS,
1263 .name = "bfd-process",
1269 static clib_error_t *
1270 bfd_sw_interface_up_down (vnet_main_t * vnm, u32 sw_if_index, u32 flags)
1272 // bfd_main_t *bm = &bfd_main;
1273 // vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
1274 if (!(flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
1281 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (bfd_sw_interface_up_down);
1283 static clib_error_t *
1284 bfd_hw_interface_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
1286 // bfd_main_t *bm = &bfd_main;
1287 if (flags & VNET_HW_INTERFACE_FLAG_LINK_UP)
1294 VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION (bfd_hw_interface_up_down);
1297 bfd_register_listener (bfd_notify_fn_t fn)
1299 bfd_main_t *bm = &bfd_main;
1301 vec_add1 (bm->listeners, fn);
1307 static clib_error_t *
1308 bfd_main_init (vlib_main_t * vm)
1310 vlib_thread_main_t *tm = &vlib_thread_main;
1311 u32 n_vlib_mains = tm->n_vlib_mains;
1313 setbuf (stdout, NULL);
1315 bfd_main_t *bm = &bfd_main;
1316 bm->random_seed = random_default_seed ();
1318 bm->vnet_main = vnet_get_main ();
1319 memset (&bm->wheel, 0, sizeof (bm->wheel));
1320 bm->cpu_cps = vm->clib_time.clocks_per_second;
1321 BFD_DBG ("cps is %.2f", bm->cpu_cps);
1322 bm->default_desired_min_tx_clocks =
1323 bfd_usec_to_clocks (bm, BFD_DEFAULT_DESIRED_MIN_TX_USEC);
1324 bm->min_required_min_rx_while_echo_clocks =
1325 bfd_usec_to_clocks (bm, BFD_REQUIRED_MIN_RX_USEC_WHILE_ECHO);
1326 const u64 now = clib_cpu_time_now ();
1327 timing_wheel_init (&bm->wheel, now, bm->cpu_cps);
1328 bm->wheel_inaccuracy = 2 << bm->wheel.log2_clocks_per_bin;
1329 bm->log_class = vlib_log_register_class ("bfd", 0);
1330 vlib_log_debug (bm->log_class, "initialized");
1331 bm->owner_thread_index = ~0;
1332 if (n_vlib_mains > 1)
1333 clib_spinlock_init (&bm->lock);
1337 VLIB_INIT_FUNCTION (bfd_main_init);
1340 bfd_get_session (bfd_main_t * bm, bfd_transport_e t)
1342 bfd_session_t *result;
1346 pool_get (bm->sessions, result);
1347 memset (result, 0, sizeof (*result));
1348 result->bs_idx = result - bm->sessions;
1349 result->transport = t;
1350 const unsigned limit = 1000;
1351 unsigned counter = 0;
1354 result->local_discr = random_u32 (&bm->random_seed);
1355 if (counter > limit)
1357 vlib_log_crit (bm->log_class,
1358 "couldn't allocate unused session discriminator even "
1359 "after %u tries!", limit);
1360 pool_put (bm->sessions, result);
1366 while (hash_get (bm->session_by_disc, result->local_discr));
1367 bfd_set_defaults (bm, result);
1368 hash_set (bm->session_by_disc, result->local_discr, result->bs_idx);
1374 bfd_put_session (bfd_main_t * bm, bfd_session_t * bs)
1378 vlib_log_info (bm->log_class, "delete session: %U",
1379 format_bfd_session_brief, bs);
1380 bfd_notify_listeners (bm, BFD_LISTEN_EVENT_DELETE, bs);
1381 if (bs->auth.curr_key)
1383 --bs->auth.curr_key->use_count;
1385 if (bs->auth.next_key)
1387 --bs->auth.next_key->use_count;
1389 hash_unset (bm->session_by_disc, bs->local_discr);
1390 pool_put (bm->sessions, bs);
1395 bfd_find_session_by_idx (bfd_main_t * bm, uword bs_idx)
1397 bfd_lock_check (bm);
1398 if (!pool_is_free_index (bm->sessions, bs_idx))
1400 return pool_elt_at_index (bm->sessions, bs_idx);
1406 bfd_find_session_by_disc (bfd_main_t * bm, u32 disc)
1408 bfd_lock_check (bm);
1409 uword *p = hash_get (bfd_main.session_by_disc, disc);
1412 return pool_elt_at_index (bfd_main.sessions, *p);
1418 * @brief verify bfd packet - common checks
1422 * @return 1 if bfd packet is valid
1425 bfd_verify_pkt_common (const bfd_pkt_t * pkt)
1427 if (1 != bfd_pkt_get_version (pkt))
1429 BFD_ERR ("BFD verification failed - unexpected version: '%d'",
1430 bfd_pkt_get_version (pkt));
1433 if (pkt->head.length < sizeof (bfd_pkt_t) ||
1434 (bfd_pkt_get_auth_present (pkt) &&
1435 pkt->head.length < sizeof (bfd_pkt_with_common_auth_t)))
1437 BFD_ERR ("BFD verification failed - unexpected length: '%d' (auth "
1439 pkt->head.length, bfd_pkt_get_auth_present (pkt));
1442 if (!pkt->head.detect_mult)
1444 BFD_ERR ("BFD verification failed - unexpected detect-mult: '%d'",
1445 pkt->head.detect_mult);
1448 if (bfd_pkt_get_multipoint (pkt))
1450 BFD_ERR ("BFD verification failed - unexpected multipoint: '%d'",
1451 bfd_pkt_get_multipoint (pkt));
1456 BFD_ERR ("BFD verification failed - unexpected my-disc: '%d'",
1460 if (!pkt->your_disc)
1462 const u8 pkt_state = bfd_pkt_get_state (pkt);
1463 if (pkt_state != BFD_STATE_down && pkt_state != BFD_STATE_admin_down)
1465 BFD_ERR ("BFD verification failed - unexpected state: '%s' "
1466 "(your-disc is zero)", bfd_state_string (pkt_state));
1474 bfd_session_switch_auth_to_next (bfd_session_t * bs)
1476 BFD_DBG ("Switching authentication key from %U to %U for bs_idx=%u",
1477 format_bfd_auth_key, bs->auth.curr_key, format_bfd_auth_key,
1478 bs->auth.next_key, bs->bs_idx);
1479 bs->auth.is_delayed = 0;
1480 if (bs->auth.curr_key)
1482 --bs->auth.curr_key->use_count;
1484 bs->auth.curr_key = bs->auth.next_key;
1485 bs->auth.next_key = NULL;
1486 bs->auth.curr_bfd_key_id = bs->auth.next_bfd_key_id;
1490 bfd_auth_type_is_meticulous (bfd_auth_type_e auth_type)
1492 if (BFD_AUTH_TYPE_meticulous_keyed_md5 == auth_type ||
1493 BFD_AUTH_TYPE_meticulous_keyed_sha1 == auth_type)
1501 bfd_verify_pkt_auth_seq_num (bfd_session_t * bs,
1502 u32 received_seq_num, int is_meticulous)
1507 * This variable MUST be set to zero after no packets have been
1508 * received on this session for at least twice the Detection Time.
1510 u64 now = clib_cpu_time_now ();
1511 if (now - bs->last_rx_clocks > bs->detection_time_clocks * 2)
1513 BFD_DBG ("BFD peer unresponsive for %lu clocks, which is > 2 * "
1514 "detection_time=%u clocks, resetting remote_seq_number_known "
1516 now - bs->last_rx_clocks, bs->detection_time_clocks * 2);
1517 bs->auth.remote_seq_number_known = 0;
1519 if (bs->auth.remote_seq_number_known)
1521 /* remote sequence number is known, verify its validity */
1522 const u32 max_u32 = 0xffffffff;
1523 /* the calculation might wrap, account for the special case... */
1524 if (bs->auth.remote_seq_number > max_u32 - 3 * bs->local_detect_mult)
1530 * |----------+----------------------------+-----------|
1532 * | remote_seq_num------+
1534 * +-----(remote_seq_num + 3*detect_mult) % * 0xffffffff
1536 * x + y + z = 0xffffffff
1537 * x + z = 3 * detect_mult
1539 const u32 z = max_u32 - bs->auth.remote_seq_number;
1540 const u32 x = 3 * bs->local_detect_mult - z;
1541 if (received_seq_num > x &&
1542 received_seq_num < bs->auth.remote_seq_number + is_meticulous)
1545 ("Recvd sequence number=%u out of ranges <0, %u>, <%u, %u>",
1546 received_seq_num, x,
1547 bs->auth.remote_seq_number + is_meticulous, max_u32);
1554 const u32 min = bs->auth.remote_seq_number + is_meticulous;
1556 bs->auth.remote_seq_number + 3 * bs->local_detect_mult;
1557 if (received_seq_num < min || received_seq_num > max)
1559 BFD_ERR ("Recvd sequence number=%u out of range <%u, %u>",
1560 received_seq_num, min, max);
1569 bfd_verify_pkt_auth_key_sha1 (const bfd_pkt_t * pkt, u32 pkt_size,
1570 bfd_session_t * bs, u8 bfd_key_id,
1571 bfd_auth_key_t * auth_key)
1573 ASSERT (auth_key->auth_type == BFD_AUTH_TYPE_keyed_sha1 ||
1574 auth_key->auth_type == BFD_AUTH_TYPE_meticulous_keyed_sha1);
1576 u8 result[SHA_DIGEST_LENGTH];
1577 bfd_pkt_with_common_auth_t *with_common = (void *) pkt;
1578 if (pkt_size < sizeof (*with_common))
1580 BFD_ERR ("Packet size too small to hold authentication common header");
1583 if (with_common->common_auth.type != auth_key->auth_type)
1585 BFD_ERR ("BFD auth type mismatch, packet auth=%d:%s doesn't match "
1586 "in-use auth=%d:%s",
1587 with_common->common_auth.type,
1588 bfd_auth_type_str (with_common->common_auth.type),
1589 auth_key->auth_type, bfd_auth_type_str (auth_key->auth_type));
1592 bfd_pkt_with_sha1_auth_t *with_sha1 = (void *) pkt;
1593 if (pkt_size < sizeof (*with_sha1) ||
1594 with_sha1->sha1_auth.type_len.len < sizeof (with_sha1->sha1_auth))
1597 ("BFD size mismatch, payload size=%u, expected=%u, auth_len=%u, "
1598 "expected=%u", pkt_size, sizeof (*with_sha1),
1599 with_sha1->sha1_auth.type_len.len, sizeof (with_sha1->sha1_auth));
1602 if (with_sha1->sha1_auth.key_id != bfd_key_id)
1605 ("BFD key ID mismatch, packet key ID=%u doesn't match key ID=%u%s",
1606 with_sha1->sha1_auth.key_id, bfd_key_id,
1608 auth.is_delayed ? " (but a delayed auth change is scheduled)" : "");
1612 if (!SHA1_Init (&ctx))
1614 BFD_ERR ("SHA1_Init failed");
1617 /* ignore last 20 bytes - use the actual key data instead pkt data */
1618 if (!SHA1_Update (&ctx, with_sha1,
1619 sizeof (*with_sha1) - sizeof (with_sha1->sha1_auth.hash)))
1621 BFD_ERR ("SHA1_Update failed");
1624 if (!SHA1_Update (&ctx, auth_key->key, sizeof (auth_key->key)))
1626 BFD_ERR ("SHA1_Update failed");
1629 if (!SHA1_Final (result, &ctx))
1631 BFD_ERR ("SHA1_Final failed");
1634 if (0 == memcmp (result, with_sha1->sha1_auth.hash, SHA_DIGEST_LENGTH))
1638 BFD_ERR ("SHA1 hash: %U doesn't match the expected value: %U",
1639 format_hex_bytes, with_sha1->sha1_auth.hash, SHA_DIGEST_LENGTH,
1640 format_hex_bytes, result, SHA_DIGEST_LENGTH);
1645 bfd_verify_pkt_auth_key (const bfd_pkt_t * pkt, u32 pkt_size,
1646 bfd_session_t * bs, u8 bfd_key_id,
1647 bfd_auth_key_t * auth_key)
1649 bfd_main_t *bm = &bfd_main;
1650 switch (auth_key->auth_type)
1652 case BFD_AUTH_TYPE_reserved:
1653 vlib_log_err (bm->log_class,
1654 "internal error, unexpected auth_type=%d:%s",
1655 auth_key->auth_type,
1656 bfd_auth_type_str (auth_key->auth_type));
1658 case BFD_AUTH_TYPE_simple_password:
1659 vlib_log_err (bm->log_class,
1660 "internal error, not implemented, unexpected auth_type=%d:%s",
1661 auth_key->auth_type,
1662 bfd_auth_type_str (auth_key->auth_type));
1664 case BFD_AUTH_TYPE_keyed_md5:
1666 case BFD_AUTH_TYPE_meticulous_keyed_md5:
1669 "internal error, not implemented, unexpected auth_type=%d:%s",
1670 auth_key->auth_type, bfd_auth_type_str (auth_key->auth_type));
1672 case BFD_AUTH_TYPE_keyed_sha1:
1674 case BFD_AUTH_TYPE_meticulous_keyed_sha1:
1678 const u32 seq_num = clib_net_to_host_u32 (((bfd_pkt_with_sha1_auth_t
1681 return bfd_verify_pkt_auth_seq_num (bs, seq_num,
1682 bfd_auth_type_is_meticulous
1683 (auth_key->auth_type))
1684 && bfd_verify_pkt_auth_key_sha1 (pkt, pkt_size, bs, bfd_key_id,
1691 "internal error, attempt to use SHA1 without SSL support");
1699 * @brief verify bfd packet - authentication
1703 * @return 1 if bfd packet is valid
1706 bfd_verify_pkt_auth (const bfd_pkt_t * pkt, u16 pkt_size, bfd_session_t * bs)
1708 if (bfd_pkt_get_auth_present (pkt))
1710 /* authentication present in packet */
1711 if (!bs->auth.curr_key)
1713 /* currently not using authentication - can we turn it on? */
1714 if (bs->auth.is_delayed && bs->auth.next_key)
1716 /* yes, switch is scheduled - make sure the auth is valid */
1717 if (bfd_verify_pkt_auth_key (pkt, pkt_size, bs,
1718 bs->auth.next_bfd_key_id,
1721 /* auth matches next key, do the switch, packet is valid */
1722 bfd_session_switch_auth_to_next (bs);
1729 /* yes, using authentication, verify the key */
1730 if (bfd_verify_pkt_auth_key (pkt, pkt_size, bs,
1731 bs->auth.curr_bfd_key_id,
1734 /* verification passed, packet is valid */
1739 /* verification failed - but maybe we need to switch key */
1740 if (bs->auth.is_delayed && bs->auth.next_key)
1742 /* delayed switch present, verify if that key works */
1743 if (bfd_verify_pkt_auth_key (pkt, pkt_size, bs,
1744 bs->auth.next_bfd_key_id,
1747 /* auth matches next key, switch key, packet is valid */
1748 bfd_session_switch_auth_to_next (bs);
1757 /* authentication in packet not present */
1758 if (pkt_size > sizeof (*pkt))
1760 BFD_ERR ("BFD verification failed - unexpected packet size '%d' "
1761 "(auth not present)", pkt_size);
1764 if (bs->auth.curr_key)
1766 /* currently authenticating - could we turn it off? */
1767 if (bs->auth.is_delayed && !bs->auth.next_key)
1769 /* yes, delayed switch to NULL key is scheduled */
1770 bfd_session_switch_auth_to_next (bs);
1776 /* no auth in packet, no auth in use - packet is valid */
1784 bfd_consume_pkt (bfd_main_t * bm, const bfd_pkt_t * pkt, u32 bs_idx)
1786 bfd_lock_check (bm);
1788 bfd_session_t *bs = bfd_find_session_by_idx (bm, bs_idx);
1789 if (!bs || (pkt->your_disc && pkt->your_disc != bs->local_discr))
1793 BFD_DBG ("Scanning bfd packet, bs_idx=%d", bs->bs_idx);
1794 bs->remote_discr = pkt->my_disc;
1795 bs->remote_state = bfd_pkt_get_state (pkt);
1796 bs->remote_demand = bfd_pkt_get_demand (pkt);
1797 bs->remote_diag = bfd_pkt_get_diag_code (pkt);
1798 u64 now = clib_cpu_time_now ();
1799 bs->last_rx_clocks = now;
1800 if (bfd_pkt_get_auth_present (pkt))
1802 bfd_auth_type_e auth_type =
1803 ((bfd_pkt_with_common_auth_t *) (pkt))->common_auth.type;
1806 case BFD_AUTH_TYPE_reserved:
1808 case BFD_AUTH_TYPE_simple_password:
1810 case BFD_AUTH_TYPE_keyed_md5:
1812 case BFD_AUTH_TYPE_meticulous_keyed_md5:
1813 vlib_log_crit (bm->log_class,
1814 "internal error, unexpected auth_type=%d:%s",
1815 auth_type, bfd_auth_type_str (auth_type));
1817 case BFD_AUTH_TYPE_keyed_sha1:
1819 case BFD_AUTH_TYPE_meticulous_keyed_sha1:
1822 bfd_pkt_with_sha1_auth_t *with_sha1 =
1823 (bfd_pkt_with_sha1_auth_t *) pkt;
1824 bs->auth.remote_seq_number =
1825 clib_net_to_host_u32 (with_sha1->sha1_auth.seq_num);
1826 bs->auth.remote_seq_number_known = 1;
1827 BFD_DBG ("Received sequence number %u",
1828 bs->auth.remote_seq_number);
1833 bs->remote_desired_min_tx_clocks =
1834 bfd_usec_to_clocks (bm, clib_net_to_host_u32 (pkt->des_min_tx));
1835 bs->remote_detect_mult = pkt->head.detect_mult;
1836 bfd_set_remote_required_min_rx (bm, bs, now,
1837 clib_net_to_host_u32 (pkt->req_min_rx));
1838 bfd_set_remote_required_min_echo_rx (bm, bs, now,
1839 clib_net_to_host_u32
1840 (pkt->req_min_echo_rx));
1841 if (bfd_pkt_get_final (pkt))
1843 if (BFD_POLL_IN_PROGRESS == bs->poll_state)
1845 BFD_DBG ("Poll sequence terminated, bs_idx=%u", bs->bs_idx);
1846 bfd_set_poll_state (bs, BFD_POLL_NOT_NEEDED);
1847 if (BFD_STATE_up == bs->local_state)
1849 bfd_set_effective_required_min_rx (bm, bs,
1850 clib_max (bs->echo *
1851 bm->min_required_min_rx_while_echo_clocks,
1852 bs->config_required_min_rx_clocks));
1855 else if (BFD_POLL_IN_PROGRESS_AND_QUEUED == bs->poll_state)
1858 * next poll sequence must be delayed by at least the round trip
1859 * time, so calculate that here
1861 BFD_DBG ("Next poll sequence can commence in " BFD_CLK_FMT,
1863 bs->poll_state_start_or_timeout_clocks));
1864 bs->poll_state_start_or_timeout_clocks =
1865 now + (now - bs->poll_state_start_or_timeout_clocks);
1867 ("Poll sequence terminated, but another is needed, bs_idx=%u",
1869 bfd_set_poll_state (bs, BFD_POLL_NEEDED);
1872 bfd_calc_next_tx (bm, bs, now);
1873 bfd_set_timer (bm, bs, now, 0);
1874 if (BFD_STATE_admin_down == bs->local_state)
1876 BFD_DBG ("Session is admin-down, ignoring packet, bs_idx=%u",
1880 if (BFD_STATE_admin_down == bs->remote_state)
1882 bfd_set_diag (bs, BFD_DIAG_CODE_neighbor_sig_down);
1883 bfd_set_state (bm, bs, BFD_STATE_down, 0);
1885 else if (BFD_STATE_down == bs->local_state)
1887 if (BFD_STATE_down == bs->remote_state)
1889 bfd_set_diag (bs, BFD_DIAG_CODE_no_diag);
1890 bfd_set_state (bm, bs, BFD_STATE_init, 0);
1892 else if (BFD_STATE_init == bs->remote_state)
1894 bfd_set_diag (bs, BFD_DIAG_CODE_no_diag);
1895 bfd_set_state (bm, bs, BFD_STATE_up, 0);
1898 else if (BFD_STATE_init == bs->local_state)
1900 if (BFD_STATE_up == bs->remote_state ||
1901 BFD_STATE_init == bs->remote_state)
1903 bfd_set_diag (bs, BFD_DIAG_CODE_no_diag);
1904 bfd_set_state (bm, bs, BFD_STATE_up, 0);
1907 else /* BFD_STATE_up == bs->local_state */
1909 if (BFD_STATE_down == bs->remote_state)
1911 bfd_set_diag (bs, BFD_DIAG_CODE_neighbor_sig_down);
1912 bfd_set_state (bm, bs, BFD_STATE_down, 0);
1918 bfd_consume_echo_pkt (bfd_main_t * bm, vlib_buffer_t * b)
1920 bfd_echo_pkt_t *pkt = NULL;
1921 if (b->current_length != sizeof (*pkt))
1925 pkt = vlib_buffer_get_current (b);
1926 bfd_session_t *bs = bfd_find_session_by_disc (bm, pkt->discriminator);
1931 BFD_DBG ("Scanning bfd echo packet, bs_idx=%d", bs->bs_idx);
1933 bfd_calc_echo_checksum (bs->local_discr, pkt->expire_time_clocks,
1935 if (checksum != pkt->checksum)
1937 BFD_DBG ("Invalid echo packet, checksum mismatch");
1940 u64 now = clib_cpu_time_now ();
1941 if (pkt->expire_time_clocks < now)
1943 BFD_DBG ("Stale packet received, expire time %lu < now %lu",
1944 pkt->expire_time_clocks, now);
1948 bs->echo_last_rx_clocks = now;
1954 format_bfd_session (u8 * s, va_list * args)
1956 const bfd_session_t *bs = va_arg (*args, bfd_session_t *);
1957 u32 indent = format_get_indent (s) + vlib_log_get_indent ();
1958 s = format (s, "bs_idx=%u local-state=%s remote-state=%s\n"
1959 "%Ulocal-discriminator=%u remote-discriminator=%u\n"
1960 "%Ulocal-diag=%s echo-active=%s\n"
1961 "%Udesired-min-tx=%u required-min-rx=%u\n"
1962 "%Urequired-min-echo-rx=%u detect-mult=%u\n"
1963 "%Uremote-min-rx=%u remote-min-echo-rx=%u\n"
1964 "%Uremote-demand=%s poll-state=%s\n"
1965 "%Uauth: local-seq-num=%u remote-seq-num=%u\n"
1966 "%U is-delayed=%s\n"
1969 bs->bs_idx, bfd_state_string (bs->local_state),
1970 bfd_state_string (bs->remote_state), format_white_space, indent,
1971 bs->local_discr, bs->remote_discr, format_white_space, indent,
1972 bfd_diag_code_string (bs->local_diag),
1973 (bs->echo ? "yes" : "no"), format_white_space, indent,
1974 bs->config_desired_min_tx_usec, bs->config_required_min_rx_usec,
1975 format_white_space, indent, 1, bs->local_detect_mult,
1976 format_white_space, indent, bs->remote_min_rx_usec,
1977 bs->remote_min_echo_rx_usec, format_white_space, indent,
1978 (bs->remote_demand ? "yes" : "no"),
1979 bfd_poll_state_string (bs->poll_state), format_white_space,
1980 indent, bs->auth.local_seq_number, bs->auth.remote_seq_number,
1981 format_white_space, indent,
1982 (bs->auth.is_delayed ? "yes" : "no"), format_white_space,
1983 indent, format_bfd_auth_key, bs->auth.curr_key,
1984 format_white_space, indent, format_bfd_auth_key,
1990 format_bfd_session_brief (u8 * s, va_list * args)
1992 const bfd_session_t *bs = va_arg (*args, bfd_session_t *);
1994 format (s, "bs_idx=%u local-state=%s remote-state=%s", bs->bs_idx,
1995 bfd_state_string (bs->local_state),
1996 bfd_state_string (bs->remote_state));
2001 bfd_auth_type_supported (bfd_auth_type_e auth_type)
2003 if (auth_type == BFD_AUTH_TYPE_keyed_sha1 ||
2004 auth_type == BFD_AUTH_TYPE_meticulous_keyed_sha1)
2012 bfd_auth_activate (bfd_session_t * bs, u32 conf_key_id,
2013 u8 bfd_key_id, u8 is_delayed)
2015 bfd_main_t *bm = &bfd_main;
2016 const uword *key_idx_p =
2017 hash_get (bm->auth_key_by_conf_key_id, conf_key_id);
2020 vlib_log_err (bm->log_class,
2021 "authentication key with config ID %u doesn't exist)",
2023 return VNET_API_ERROR_BFD_ENOENT;
2025 const uword key_idx = *key_idx_p;
2026 bfd_auth_key_t *key = pool_elt_at_index (bm->auth_keys, key_idx);
2029 if (bs->auth.next_key == key)
2031 /* already using this key, no changes required */
2034 bs->auth.next_key = key;
2035 bs->auth.next_bfd_key_id = bfd_key_id;
2036 bs->auth.is_delayed = 1;
2040 if (bs->auth.curr_key == key)
2042 /* already using this key, no changes required */
2045 if (bs->auth.curr_key)
2047 --bs->auth.curr_key->use_count;
2049 bs->auth.curr_key = key;
2050 bs->auth.curr_bfd_key_id = bfd_key_id;
2051 bs->auth.is_delayed = 0;
2054 BFD_DBG ("\nSession auth modified: %U", format_bfd_session, bs);
2055 vlib_log_info (bm->log_class, "session auth modified: %U",
2056 format_bfd_session_brief, bs);
2061 bfd_auth_deactivate (bfd_session_t * bs, u8 is_delayed)
2063 bfd_main_t *bm = &bfd_main;
2067 /* not delayed - deactivate the current key right now */
2068 if (bs->auth.curr_key)
2070 --bs->auth.curr_key->use_count;
2071 bs->auth.curr_key = NULL;
2073 bs->auth.is_delayed = 0;
2077 /* delayed - mark as so */
2078 bs->auth.is_delayed = 1;
2081 * clear the next key unconditionally - either the auth change is not delayed
2082 * in which case the caller expects the session to not use authentication
2083 * from this point forward, or it is delayed, in which case the next_key
2084 * needs to be set to NULL to make it so in the future
2086 if (bs->auth.next_key)
2088 --bs->auth.next_key->use_count;
2089 bs->auth.next_key = NULL;
2091 BFD_DBG ("\nSession auth modified: %U", format_bfd_session, bs);
2092 vlib_log_info (bm->log_class, "session auth modified: %U",
2093 format_bfd_session_brief, bs);
2096 vlib_log_err (bm->log_class,
2097 "SSL missing, cannot deactivate BFD authentication");
2098 return VNET_API_ERROR_BFD_NOTSUPP;
2103 bfd_session_set_params (bfd_main_t * bm, bfd_session_t * bs,
2104 u32 desired_min_tx_usec,
2105 u32 required_min_rx_usec, u8 detect_mult)
2107 if (bs->local_detect_mult != detect_mult ||
2108 bs->config_desired_min_tx_usec != desired_min_tx_usec ||
2109 bs->config_required_min_rx_usec != required_min_rx_usec)
2111 BFD_DBG ("\nChanging session params: %U", format_bfd_session, bs);
2112 switch (bs->poll_state)
2114 case BFD_POLL_NOT_NEEDED:
2115 if (BFD_STATE_up == bs->local_state ||
2116 BFD_STATE_init == bs->local_state)
2118 /* poll sequence is not needed for detect multiplier change */
2119 if (bs->config_desired_min_tx_usec != desired_min_tx_usec ||
2120 bs->config_required_min_rx_usec != required_min_rx_usec)
2122 bfd_set_poll_state (bs, BFD_POLL_NEEDED);
2126 case BFD_POLL_NEEDED:
2127 case BFD_POLL_IN_PROGRESS_AND_QUEUED:
2129 * nothing to do - will be handled in the future poll which is
2130 * already scheduled for execution
2133 case BFD_POLL_IN_PROGRESS:
2134 /* poll sequence is not needed for detect multiplier change */
2135 if (bs->config_desired_min_tx_usec != desired_min_tx_usec ||
2136 bs->config_required_min_rx_usec != required_min_rx_usec)
2138 BFD_DBG ("Poll in progress, queueing extra poll, bs_idx=%u",
2140 bfd_set_poll_state (bs, BFD_POLL_IN_PROGRESS_AND_QUEUED);
2144 bs->local_detect_mult = detect_mult;
2145 bs->config_desired_min_tx_usec = desired_min_tx_usec;
2146 bs->config_desired_min_tx_clocks =
2147 bfd_usec_to_clocks (bm, desired_min_tx_usec);
2148 bs->config_required_min_rx_usec = required_min_rx_usec;
2149 bs->config_required_min_rx_clocks =
2150 bfd_usec_to_clocks (bm, required_min_rx_usec);
2151 BFD_DBG ("\nChanged session params: %U", format_bfd_session, bs);
2153 vlib_log_info (bm->log_class, "changed session params: %U",
2154 format_bfd_session_brief, bs);
2155 vlib_process_signal_event (bm->vlib_main, bm->bfd_process_node_index,
2156 BFD_EVENT_CONFIG_CHANGED, bs->bs_idx);
2160 BFD_DBG ("Ignore parameter change - no change, bs_idx=%u", bs->bs_idx);
2166 bfd_auth_set_key (u32 conf_key_id, u8 auth_type, u8 key_len,
2167 const u8 * key_data)
2169 bfd_main_t *bm = &bfd_main;
2171 bfd_auth_key_t *auth_key = NULL;
2172 if (!key_len || key_len > bfd_max_key_len_for_auth_type (auth_type))
2174 vlib_log_err (bm->log_class,
2175 "invalid authentication key length for auth_type=%d:%s "
2176 "(key_len=%u, must be non-zero, expected max=%u)",
2177 auth_type, bfd_auth_type_str (auth_type), key_len,
2178 (u32) bfd_max_key_len_for_auth_type (auth_type));
2179 return VNET_API_ERROR_INVALID_VALUE;
2181 if (!bfd_auth_type_supported (auth_type))
2183 vlib_log_err (bm->log_class, "unsupported auth type=%d:%s", auth_type,
2184 bfd_auth_type_str (auth_type));
2185 return VNET_API_ERROR_BFD_NOTSUPP;
2187 uword *key_idx_p = hash_get (bm->auth_key_by_conf_key_id, conf_key_id);
2190 /* modifying existing key - must not be used */
2191 const uword key_idx = *key_idx_p;
2192 auth_key = pool_elt_at_index (bm->auth_keys, key_idx);
2193 if (auth_key->use_count > 0)
2195 vlib_log_err (bm->log_class,
2196 "authentication key with conf ID %u in use by %u BFD "
2197 "session(s) - cannot modify", conf_key_id,
2198 auth_key->use_count);
2199 return VNET_API_ERROR_BFD_EINUSE;
2204 /* adding new key */
2205 pool_get (bm->auth_keys, auth_key);
2206 auth_key->conf_key_id = conf_key_id;
2207 hash_set (bm->auth_key_by_conf_key_id, conf_key_id,
2208 auth_key - bm->auth_keys);
2210 auth_key->auth_type = auth_type;
2211 memset (auth_key->key, 0, sizeof (auth_key->key));
2212 clib_memcpy (auth_key->key, key_data, key_len);
2215 vlib_log_err (bm->log_class,
2216 "SSL missing, cannot manipulate authentication keys");
2217 return VNET_API_ERROR_BFD_NOTSUPP;
2222 bfd_auth_del_key (u32 conf_key_id)
2225 bfd_auth_key_t *auth_key = NULL;
2226 bfd_main_t *bm = &bfd_main;
2227 uword *key_idx_p = hash_get (bm->auth_key_by_conf_key_id, conf_key_id);
2230 /* deleting existing key - must not be used */
2231 const uword key_idx = *key_idx_p;
2232 auth_key = pool_elt_at_index (bm->auth_keys, key_idx);
2233 if (auth_key->use_count > 0)
2235 vlib_log_err (bm->log_class,
2236 "authentication key with conf ID %u in use by %u BFD "
2237 "session(s) - cannot delete", conf_key_id,
2238 auth_key->use_count);
2239 return VNET_API_ERROR_BFD_EINUSE;
2241 hash_unset (bm->auth_key_by_conf_key_id, conf_key_id);
2242 memset (auth_key, 0, sizeof (*auth_key));
2243 pool_put (bm->auth_keys, auth_key);
2248 vlib_log_err (bm->log_class,
2249 "authentication key with conf ID %u does not exist",
2251 return VNET_API_ERROR_BFD_ENOENT;
2255 vlib_log_err (bm->log_class,
2256 "SSL missing, cannot manipulate authentication keys");
2257 return VNET_API_ERROR_BFD_NOTSUPP;
2261 bfd_main_t bfd_main;
2264 * fd.io coding-style-patch-verification: ON
2267 * eval: (c-set-style "gnu")