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>
41 bfd_calc_echo_checksum (u32 discriminator, u64 expire_time, u32 secret)
44 #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);
49 checksum = clib_xxhash (discriminator ^ expire_time ^ secret);
55 bfd_usec_to_clocks (const bfd_main_t * bm, u64 us)
57 return bm->cpu_cps * ((f64) us / USEC_PER_SECOND);
61 bfd_clocks_to_usec (const bfd_main_t * bm, u64 clocks)
63 return (clocks / bm->cpu_cps) * USEC_PER_SECOND;
66 static vlib_node_registration_t bfd_process_node;
69 format_bfd_auth_key (u8 * s, va_list * args)
71 const bfd_auth_key_t *key = va_arg (*args, bfd_auth_key_t *);
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);
80 s = format (s, "{none}");
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.
92 BFD_INPUT_NEXT_NORMAL,
96 static void bfd_on_state_change (bfd_main_t * bm, bfd_session_t * bs, u64 now,
100 bfd_set_defaults (bfd_main_t * bm, bfd_session_t * bs)
102 bs->local_state = BFD_STATE_down;
103 bs->local_diag = BFD_DIAG_CODE_no_diag;
104 bs->remote_state = BFD_STATE_down;
105 bs->remote_discr = 0;
106 bs->hop_type = BFD_HOP_TYPE_SINGLE;
107 bs->config_desired_min_tx_usec = BFD_DEFAULT_DESIRED_MIN_TX_USEC;
108 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);
112 bs->remote_min_echo_rx_usec = 0;
113 bs->remote_min_echo_rx_clocks = 0;
114 bs->remote_demand = 0;
115 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);
118 bs->echo_secret = random_u32 (&bm->random_seed);
122 bfd_set_diag (bfd_session_t * bs, bfd_diag_code_e code)
124 if (bs->local_diag != code)
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;
133 bfd_set_state (bfd_main_t * bm, bfd_session_t * bs,
134 bfd_state_e new_state, int handling_wakeup)
136 if (bs->local_state != new_state)
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;
142 bfd_on_state_change (bm, bs, clib_cpu_time_now (), handling_wakeup);
147 bfd_poll_state_string (bfd_poll_state_e state)
153 return "BFD_POLL_" #x;
154 foreach_bfd_poll_state (F)
161 bfd_set_poll_state (bfd_session_t * bs, bfd_poll_state_e state)
163 if (bs->poll_state != state)
165 BFD_DBG ("Setting poll state=%s, bs_idx=%u",
166 bfd_poll_state_string (state), bs->bs_idx);
167 bs->poll_state = state;
172 bfd_recalc_tx_interval (bfd_main_t * bm, bfd_session_t * bs)
174 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));
181 bfd_recalc_echo_tx_interval (bfd_main_t * bm, bfd_session_t * bs)
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));
191 bfd_calc_next_tx (bfd_main_t * bm, bfd_session_t * bs, u64 now)
193 if (bs->local_detect_mult > 1)
195 /* 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)
202 * the timeout is in the past, which means that either remote
203 * demand mode was set or performance/clock issues ...
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;
214 /* 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)
221 * the timeout is in the past, which means that either remote
222 * demand mode was set or performance/clock issues ...
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;
231 if (bs->tx_timeout_clocks)
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);
241 bfd_calc_next_echo_tx (bfd_main_t * bm, bfd_session_t * bs, u64 now)
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)
247 /* huh, we've missed it already, transmit now */
248 BFD_DBG ("Missed %lu echo transmit events (now is %lu, calc tx_timeout "
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;
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);
262 bfd_recalc_detection_time (bfd_main_t * bm, bfd_session_t * bs)
264 if (bs->local_state == BFD_STATE_init || bs->local_state == BFD_STATE_up)
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);
277 bfd_set_timer (bfd_main_t * bm, bfd_session_t * bs, u64 now,
283 if (BFD_STATE_up == bs->local_state)
285 rx_timeout = bs->last_rx_clocks + bs->detection_time_clocks;
287 if (BFD_STATE_up != bs->local_state ||
288 (!bs->remote_demand && bs->remote_min_rx_usec) ||
289 BFD_POLL_NOT_NEEDED != bs->poll_state)
291 tx_timeout = bs->tx_timeout_clocks;
293 if (tx_timeout && rx_timeout)
295 next = clib_min (tx_timeout, rx_timeout);
305 if (bs->echo && next > bs->echo_tx_timeout_clocks)
307 next = bs->echo_tx_timeout_clocks;
309 BFD_DBG ("bs_idx=%u, tx_timeout=%lu, echo_tx_timeout=%lu, rx_timeout=%lu, "
311 bs->bs_idx, tx_timeout, bs->echo_tx_timeout_clocks, rx_timeout,
313 ? "tx" : (next == bs->echo_tx_timeout_clocks ? "echo tx" : "rx"));
314 /* sometimes the wheel expires an event a bit sooner than requested, account
316 if (next && (now + bm->wheel_inaccuracy > bs->wheel_time_clocks ||
317 next < bs->wheel_time_clocks || !bs->wheel_time_clocks))
320 bs->wheel_time_clocks = next;
321 BFD_DBG ("timing_wheel_insert(%p, %lu (%ld clocks/%.2fs in the "
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);
328 timing_wheel_insert (&bm->wheel, bs->wheel_time_clocks, bs->bs_idx);
330 if (!handling_wakeup)
333 /* Send only if it is earlier than current awaited wakeup time */
335 (bs->wheel_time_clocks < bm->bfd_process_next_wakeup_clocks) &&
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.
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);
347 /* If we do send the signal, note this down along with the start timestamp */
350 bm->bfd_process_wakeup_events_in_flight++;
351 bm->bfd_process_wakeup_event_start_clocks = now;
356 /* Use the multithreaded event sending so the workers can send events too */
359 vlib_process_signal_event_mt (bm->vlib_main,
360 bm->bfd_process_node_index,
361 BFD_EVENT_RESCHEDULE, ~0);
367 bfd_set_effective_desired_min_tx (bfd_main_t * bm,
368 bfd_session_t * bs, u64 now,
369 u64 desired_min_tx_clocks)
371 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));
374 bfd_recalc_detection_time (bm, bs);
375 bfd_recalc_tx_interval (bm, bs);
376 bfd_recalc_echo_tx_interval (bm, bs);
377 bfd_calc_next_tx (bm, bs, now);
381 bfd_set_effective_required_min_rx (bfd_main_t * bm,
383 u64 required_min_rx_clocks)
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);
392 bfd_set_remote_required_min_rx (bfd_main_t * bm, bfd_session_t * bs,
393 u64 now, u32 remote_required_min_rx_usec)
395 if (bs->remote_min_rx_usec != remote_required_min_rx_usec)
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);
408 bfd_set_remote_required_min_echo_rx (bfd_main_t * bm, bfd_session_t * bs,
410 u32 remote_required_min_echo_rx_usec)
412 if (bs->remote_min_echo_rx_usec != remote_required_min_echo_rx_usec)
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);
424 bfd_notify_listeners (bfd_main_t * bm,
425 bfd_listen_event_e event, const bfd_session_t * bs)
428 vec_foreach (fn, bm->listeners)
435 bfd_session_start (bfd_main_t * bm, bfd_session_t * bs)
437 BFD_DBG ("\nStarting session: %U", format_bfd_session, bs);
438 vlib_log_info (bm->log_class, "start BFD session: %U",
439 format_bfd_session_brief, bs);
440 bfd_set_effective_required_min_rx (bm, bs,
441 bs->config_required_min_rx_clocks);
442 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);
445 bfd_notify_listeners (bm, BFD_LISTEN_EVENT_CREATE, bs);
449 bfd_session_set_flags (bfd_session_t * bs, u8 admin_up_down)
451 bfd_main_t *bm = &bfd_main;
452 u64 now = clib_cpu_time_now ();
455 BFD_DBG ("Session set admin-up, bs-idx=%u", bs->bs_idx);
456 vlib_log_info (bm->log_class, "set session admin-up: %U",
457 format_bfd_session_brief, bs);
458 bfd_set_state (bm, bs, BFD_STATE_down, 0);
459 bfd_set_diag (bs, BFD_DIAG_CODE_no_diag);
460 bfd_calc_next_tx (bm, bs, now);
461 bfd_set_timer (bm, bs, now, 0);
465 BFD_DBG ("Session set admin-down, bs-idx=%u", bs->bs_idx);
466 vlib_log_info (bm->log_class, "set session admin-down: %U",
467 format_bfd_session_brief, bs);
468 bfd_set_diag (bs, BFD_DIAG_CODE_admin_down);
469 bfd_set_state (bm, bs, BFD_STATE_admin_down, 0);
470 bfd_calc_next_tx (bm, bs, now);
471 bfd_set_timer (bm, bs, now, 0);
476 bfd_input_format_trace (u8 * s, va_list * args)
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))
484 s = format (s, "BFD v%u, diag=%u(%s), state=%u(%s),\n"
485 " flags=(P:%u, F:%u, C:%u, A:%u, D:%u, M:%u), "
486 "detect_mult=%u, length=%u\n",
487 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,
496 if (t->len >= sizeof (bfd_pkt_t) &&
497 pkt->head.length >= sizeof (bfd_pkt_t))
499 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));
503 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));
507 s = format (s, " required min echo rx interval: %u",
508 clib_net_to_host_u32 (pkt->req_min_echo_rx));
510 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))
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))
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));
535 s = format (s, "\n");
548 bfd_rpc_event_cb (const bfd_rpc_event_t * a)
550 bfd_main_t *bm = &bfd_main;
551 u32 bs_idx = a->bs_idx;
553 bfd_session_t session_data;
556 if (!pool_is_free_index (bm->sessions, bs_idx))
558 bfd_session_t *bs = pool_elt_at_index (bm->sessions, bs_idx);
559 clib_memcpy (&session_data, bs, sizeof (bfd_session_t));
564 BFD_DBG ("Ignoring event RPC for non-existent session index %u",
570 bfd_event (bm, &session_data);
574 bfd_event_rpc (u32 bs_idx)
576 const u32 data_size = sizeof (bfd_rpc_event_t);
578 bfd_rpc_event_t *event = (bfd_rpc_event_t *) data;
580 event->bs_idx = bs_idx;
581 vl_api_rpc_call_main_thread (bfd_rpc_event_cb, data, data_size);
587 } bfd_rpc_notify_listeners_t;
590 bfd_rpc_notify_listeners_cb (const bfd_rpc_notify_listeners_t * a)
592 bfd_main_t *bm = &bfd_main;
593 u32 bs_idx = a->bs_idx;
595 if (!pool_is_free_index (bm->sessions, bs_idx))
597 bfd_session_t *bs = pool_elt_at_index (bm->sessions, bs_idx);
598 bfd_notify_listeners (bm, BFD_LISTEN_EVENT_UPDATE, bs);
602 BFD_DBG ("Ignoring notify RPC for non-existent session index %u",
609 bfd_notify_listeners_rpc (u32 bs_idx)
611 const u32 data_size = sizeof (bfd_rpc_notify_listeners_t);
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);
619 bfd_on_state_change (bfd_main_t * bm, bfd_session_t * bs, u64 now,
622 BFD_DBG ("\nState changed: %U", format_bfd_session, bs);
624 if (vlib_get_thread_index () == 0)
630 /* without RPC - a REGRESSION: BFD event are not propagated */
631 bfd_event_rpc (bs->bs_idx);
634 switch (bs->local_state)
636 case BFD_STATE_admin_down:
638 bfd_set_effective_desired_min_tx (bm, bs, now,
640 (bs->config_desired_min_tx_clocks,
641 bm->default_desired_min_tx_clocks));
642 bfd_set_effective_required_min_rx (bm, bs,
643 bs->config_required_min_rx_clocks);
644 bfd_set_timer (bm, bs, now, handling_wakeup);
648 bfd_set_effective_desired_min_tx (bm, bs, now,
650 (bs->config_desired_min_tx_clocks,
651 bm->default_desired_min_tx_clocks));
652 bfd_set_effective_required_min_rx (bm, bs,
653 bs->config_required_min_rx_clocks);
654 bfd_set_timer (bm, bs, now, handling_wakeup);
658 bfd_set_effective_desired_min_tx (bm, bs, now,
659 bs->config_desired_min_tx_clocks);
660 bfd_set_timer (bm, bs, now, handling_wakeup);
663 bfd_set_effective_desired_min_tx (bm, bs, now,
664 bs->config_desired_min_tx_clocks);
665 if (BFD_POLL_NOT_NEEDED == bs->poll_state)
667 bfd_set_effective_required_min_rx (bm, bs,
668 bs->config_required_min_rx_clocks);
670 bfd_set_timer (bm, bs, now, handling_wakeup);
673 if (vlib_get_thread_index () == 0)
675 bfd_notify_listeners (bm, BFD_LISTEN_EVENT_UPDATE, bs);
679 /* without RPC - a REGRESSION: state changes are not propagated */
680 bfd_notify_listeners_rpc (bs->bs_idx);
685 bfd_on_config_change (vlib_main_t * vm, vlib_node_runtime_t * rt,
686 bfd_main_t * bm, bfd_session_t * bs, u64 now)
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
692 if (bs->remote_demand && BFD_POLL_NEEDED == bs->poll_state &&
693 bs->poll_state_start_or_timeout_clocks < now)
695 bs->tx_timeout_clocks = now;
697 bfd_recalc_detection_time (bm, bs);
698 bfd_set_timer (bm, bs, now, 0);
702 bfd_add_transport_layer (vlib_main_t * vm, u32 bi, bfd_session_t * bs)
704 switch (bs->transport)
706 case BFD_TRANSPORT_UDP4:
707 BFD_DBG ("Transport bfd via udp4, bs_idx=%u", bs->bs_idx);
708 bfd_add_udp4_transport (vm, bi, bs, 0 /* is_echo */ );
710 case BFD_TRANSPORT_UDP6:
711 BFD_DBG ("Transport bfd via udp6, bs_idx=%u", bs->bs_idx);
712 bfd_add_udp6_transport (vm, bi, bs, 0 /* is_echo */ );
718 bfd_transport_control_frame (vlib_main_t * vm, u32 bi, bfd_session_t * bs)
720 switch (bs->transport)
722 case BFD_TRANSPORT_UDP4:
723 BFD_DBG ("Transport bfd via udp4, bs_idx=%u", bs->bs_idx);
724 return bfd_transport_udp4 (vm, bi, bs);
726 case BFD_TRANSPORT_UDP6:
727 BFD_DBG ("Transport bfd via udp6, bs_idx=%u", bs->bs_idx);
728 return bfd_transport_udp6 (vm, bi, bs);
735 bfd_echo_add_transport_layer (vlib_main_t * vm, u32 bi, bfd_session_t * bs)
737 switch (bs->transport)
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 */ );
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 */ );
752 bfd_transport_echo (vlib_main_t * vm, u32 bi, bfd_session_t * bs)
754 switch (bs->transport)
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);
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);
770 bfd_add_sha1_auth_section (vlib_buffer_t * b, bfd_session_t * bs)
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);
777 clib_memset (auth, 0, sizeof (*auth));
778 auth->type_len.type = bs->auth.curr_key->auth_type;
780 * only meticulous authentication types require incrementing seq number
781 * for every message, but doing so doesn't violate the RFC
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);
788 * first copy the password into the packet, then calculate the hash
789 * and finally replace the password with the calculated hash
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));
801 bfd_add_auth_section (vlib_buffer_t * b, bfd_session_t * bs)
803 bfd_main_t *bm = &bfd_main;
804 if (bs->auth.curr_key)
806 const bfd_auth_type_e auth_type = bs->auth.curr_key->auth_type;
809 case BFD_AUTH_TYPE_reserved:
811 case BFD_AUTH_TYPE_simple_password:
813 case BFD_AUTH_TYPE_keyed_md5:
815 case BFD_AUTH_TYPE_meticulous_keyed_md5:
816 vlib_log_crit (bm->log_class,
817 "internal error, unexpected BFD auth type '%d'",
821 case BFD_AUTH_TYPE_keyed_sha1:
823 case BFD_AUTH_TYPE_meticulous_keyed_sha1:
824 bfd_add_sha1_auth_section (b, bs);
827 case BFD_AUTH_TYPE_keyed_sha1:
829 case BFD_AUTH_TYPE_meticulous_keyed_sha1:
830 vlib_log_crit (bm->log_class,
831 "internal error, unexpected BFD auth type '%d'",
840 bfd_is_echo_possible (bfd_session_t * bs)
842 if (BFD_STATE_up == bs->local_state && BFD_STATE_up == bs->remote_state &&
843 bs->remote_min_echo_rx_usec > 0)
845 switch (bs->transport)
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);
857 bfd_init_control_frame (bfd_main_t * bm, bfd_session_t * bs,
860 bfd_pkt_t *pkt = vlib_buffer_get_current (b);
862 bfd_length = sizeof (bfd_pkt_t);
863 clib_memset (pkt, 0, sizeof (*pkt));
864 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);
867 pkt->head.detect_mult = bs->local_detect_mult;
868 pkt->head.length = bfd_length;
869 pkt->my_disc = bs->local_discr;
870 pkt->your_disc = bs->remote_discr;
871 pkt->des_min_tx = clib_host_to_net_u32 (bs->config_desired_min_tx_usec);
875 clib_host_to_net_u32 (bfd_clocks_to_usec
876 (bm, bs->effective_required_min_rx_clocks));
881 clib_host_to_net_u32 (bs->config_required_min_rx_usec);
883 pkt->req_min_echo_rx = clib_host_to_net_u32 (1);
884 b->current_length = bfd_length;
888 bfd_send_echo (vlib_main_t * vm, vlib_node_runtime_t * rt,
889 bfd_main_t * bm, bfd_session_t * bs, u64 now)
891 if (!bfd_is_echo_possible (bs))
893 BFD_DBG ("\nSwitching off echo function: %U", format_bfd_session, bs);
897 /* sometimes the wheel expires an event a bit sooner than requested,
900 if (now + bm->wheel_inaccuracy >= bs->echo_tx_timeout_clocks)
902 BFD_DBG ("\nSending echo packet: %U", format_bfd_session, bs);
904 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
906 vlib_log_crit (bm->log_class, "buffer allocation failure");
909 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
910 ASSERT (b->current_data == 0);
911 clib_memset (vnet_buffer (b), 0, sizeof (*vnet_buffer (b)));
912 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b);
913 bfd_echo_pkt_t *pkt = vlib_buffer_get_current (b);
914 clib_memset (pkt, 0, sizeof (*pkt));
915 pkt->discriminator = bs->local_discr;
916 pkt->expire_time_clocks =
917 now + bs->echo_transmit_interval_clocks * bs->local_detect_mult;
919 bfd_calc_echo_checksum (bs->local_discr, pkt->expire_time_clocks,
921 b->current_length = sizeof (*pkt);
922 if (!bfd_echo_add_transport_layer (vm, bi, bs))
924 BFD_ERR ("cannot send echo packet out, turning echo off");
926 vlib_buffer_free_one (vm, bi);
929 if (!bfd_transport_echo (vm, bi, bs))
931 BFD_ERR ("cannot send echo packet out, turning echo off");
933 vlib_buffer_free_one (vm, bi);
936 bs->echo_last_tx_clocks = now;
937 bfd_calc_next_echo_tx (bm, bs, now);
942 ("No need to send echo packet now, now is %lu, tx_timeout is %lu",
943 now, bs->echo_tx_timeout_clocks);
948 bfd_send_periodic (vlib_main_t * vm, vlib_node_runtime_t * rt,
949 bfd_main_t * bm, bfd_session_t * bs, u64 now)
951 if (!bs->remote_min_rx_usec && BFD_POLL_NOT_NEEDED == bs->poll_state)
953 BFD_DBG ("Remote min rx interval is zero, not sending periodic control "
957 if (BFD_POLL_NOT_NEEDED == bs->poll_state && bs->remote_demand &&
958 BFD_STATE_up == bs->local_state && BFD_STATE_up == bs->remote_state)
961 * A system MUST NOT periodically transmit BFD Control packets if Demand
962 * mode is active on the remote system (bfd.RemoteDemandMode is 1,
963 * bfd.SessionState is Up, and bfd.RemoteSessionState is Up) and a Poll
964 * Sequence is not being transmitted.
966 BFD_DBG ("Remote demand is set, not sending periodic control frame");
970 * sometimes the wheel expires an event a bit sooner than requested, account
973 if (now + bm->wheel_inaccuracy >= bs->tx_timeout_clocks)
975 BFD_DBG ("\nSending periodic control frame: %U", format_bfd_session,
978 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
980 vlib_log_crit (bm->log_class, "buffer allocation failure");
983 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
984 ASSERT (b->current_data == 0);
985 clib_memset (vnet_buffer (b), 0, sizeof (*vnet_buffer (b)));
986 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b);
987 bfd_init_control_frame (bm, bs, b);
988 switch (bs->poll_state)
990 case BFD_POLL_NEEDED:
991 if (now < bs->poll_state_start_or_timeout_clocks)
993 BFD_DBG ("Cannot start a poll sequence yet, need to wait "
995 BFD_CLK_PRN (bs->poll_state_start_or_timeout_clocks -
999 bs->poll_state_start_or_timeout_clocks = now;
1000 bfd_set_poll_state (bs, BFD_POLL_IN_PROGRESS);
1002 case BFD_POLL_IN_PROGRESS:
1003 case BFD_POLL_IN_PROGRESS_AND_QUEUED:
1004 bfd_pkt_set_poll (vlib_buffer_get_current (b));
1005 BFD_DBG ("Setting poll bit in packet, bs_idx=%u", bs->bs_idx);
1007 case BFD_POLL_NOT_NEEDED:
1011 bfd_add_auth_section (b, bs);
1012 bfd_add_transport_layer (vm, bi, bs);
1013 if (!bfd_transport_control_frame (vm, bi, bs))
1015 vlib_buffer_free_one (vm, bi);
1017 bs->last_tx_clocks = now;
1018 bfd_calc_next_tx (bm, bs, now);
1023 ("No need to send control frame now, now is %lu, tx_timeout is %lu",
1024 now, bs->tx_timeout_clocks);
1029 bfd_init_final_control_frame (vlib_main_t * vm, vlib_buffer_t * b,
1030 bfd_main_t * bm, bfd_session_t * bs,
1033 BFD_DBG ("Send final control frame for bs_idx=%lu", bs->bs_idx);
1034 bfd_init_control_frame (bm, bs, b);
1035 bfd_pkt_set_final (vlib_buffer_get_current (b));
1036 bfd_add_auth_section (b, bs);
1037 u32 bi = vlib_get_buffer_index (vm, b);
1038 bfd_add_transport_layer (vm, bi, bs);
1039 bs->last_tx_clocks = clib_cpu_time_now ();
1041 * RFC allows to include changes in final frame, so if there were any
1042 * pending, we already did that, thus we can clear any pending poll needs
1044 bfd_set_poll_state (bs, BFD_POLL_NOT_NEEDED);
1048 bfd_check_rx_timeout (bfd_main_t * bm, bfd_session_t * bs, u64 now,
1049 int handling_wakeup)
1052 * sometimes the wheel expires an event a bit sooner than requested, account
1055 if (bs->last_rx_clocks + bs->detection_time_clocks <=
1056 now + bm->wheel_inaccuracy)
1058 BFD_DBG ("Rx timeout, session goes down");
1059 bfd_set_diag (bs, BFD_DIAG_CODE_det_time_exp);
1060 bfd_set_state (bm, bs, BFD_STATE_down, handling_wakeup);
1062 * If the remote system does not receive any
1063 * BFD Control packets for a Detection Time, it SHOULD reset
1064 * bfd.RemoteMinRxInterval to its initial value of 1 (per section 6.8.1,
1065 * since it is no longer required to maintain previous session state)
1066 * and then can transmit at its own rate.
1068 bfd_set_remote_required_min_rx (bm, bs, now, 1);
1070 else if (bs->echo &&
1071 bs->echo_last_rx_clocks +
1072 bs->echo_transmit_interval_clocks * bs->local_detect_mult <=
1073 now + bm->wheel_inaccuracy)
1075 BFD_DBG ("Echo rx timeout, session goes down");
1076 bfd_set_diag (bs, BFD_DIAG_CODE_echo_failed);
1077 bfd_set_state (bm, bs, BFD_STATE_down, handling_wakeup);
1082 bfd_on_timeout (vlib_main_t * vm, vlib_node_runtime_t * rt, bfd_main_t * bm,
1083 bfd_session_t * bs, u64 now)
1085 BFD_DBG ("Timeout for bs_idx=%lu", bs->bs_idx);
1086 switch (bs->local_state)
1088 case BFD_STATE_admin_down:
1089 bfd_send_periodic (vm, rt, bm, bs, now);
1091 case BFD_STATE_down:
1092 bfd_send_periodic (vm, rt, bm, bs, now);
1094 case BFD_STATE_init:
1095 bfd_check_rx_timeout (bm, bs, now, 1);
1096 bfd_send_periodic (vm, rt, bm, bs, now);
1099 bfd_check_rx_timeout (bm, bs, now, 1);
1100 if (BFD_POLL_NOT_NEEDED == bs->poll_state && !bs->echo &&
1101 bfd_is_echo_possible (bs))
1103 /* switch on echo function as main detection method now */
1104 BFD_DBG ("Switching on echo function, bs_idx=%u", bs->bs_idx);
1106 bs->echo_last_rx_clocks = now;
1107 bs->echo_tx_timeout_clocks = now;
1108 bfd_set_effective_required_min_rx (bm, bs,
1110 (bm->min_required_min_rx_while_echo_clocks,
1111 bs->config_required_min_rx_clocks));
1112 bfd_set_poll_state (bs, BFD_POLL_NEEDED);
1114 bfd_send_periodic (vm, rt, bm, bs, now);
1117 bfd_send_echo (vm, rt, bm, bs, now);
1124 * bfd process node function
1127 bfd_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
1129 bfd_main_t *bm = &bfd_main;
1131 uword event_type, *event_data = 0;
1133 /* So we can send events to the bfd process */
1134 bm->bfd_process_node_index = bfd_process_node.index;
1138 u64 now = clib_cpu_time_now ();
1140 u64 next_expire = timing_wheel_next_expiring_elt_time (&bm->wheel);
1141 BFD_DBG ("timing_wheel_next_expiring_elt_time(%p) returns %lu",
1142 &bm->wheel, next_expire);
1143 bm->bfd_process_next_wakeup_clocks =
1144 (i64) next_expire >= 0 ? next_expire : ~0;
1146 if ((i64) next_expire < 0)
1148 BFD_DBG ("wait for event without timeout");
1149 (void) vlib_process_wait_for_event (vm);
1150 event_type = vlib_process_get_events (vm, &event_data);
1154 f64 timeout = ((i64) next_expire - (i64) now) / bm->cpu_cps;
1155 BFD_DBG ("wait for event with timeout %.02f", timeout);
1158 BFD_DBG ("negative timeout, already expired, skipping wait");
1163 (void) vlib_process_wait_for_event_or_clock (vm, timeout);
1164 event_type = vlib_process_get_events (vm, &event_data);
1167 now = clib_cpu_time_now ();
1168 uword *session_index;
1171 case ~0: /* no events => timeout */
1172 /* nothing to do here */
1174 case BFD_EVENT_RESCHEDULE:
1176 bm->bfd_process_wakeup_event_delay_clocks =
1177 now - bm->bfd_process_wakeup_event_start_clocks;
1178 bm->bfd_process_wakeup_events_in_flight--;
1180 /* nothing to do here - reschedule is done automatically after
1181 * each event or timeout */
1183 case BFD_EVENT_NEW_SESSION:
1184 vec_foreach (session_index, event_data)
1187 if (!pool_is_free_index (bm->sessions, *session_index))
1190 pool_elt_at_index (bm->sessions, *session_index);
1191 bfd_send_periodic (vm, rt, bm, bs, now);
1192 bfd_set_timer (bm, bs, now, 1);
1196 BFD_DBG ("Ignoring event for non-existent session index %u",
1197 (u32) * session_index);
1202 case BFD_EVENT_CONFIG_CHANGED:
1203 vec_foreach (session_index, event_data)
1206 if (!pool_is_free_index (bm->sessions, *session_index))
1209 pool_elt_at_index (bm->sessions, *session_index);
1210 bfd_on_config_change (vm, rt, bm, bs, now);
1214 BFD_DBG ("Ignoring event for non-existent session index %u",
1215 (u32) * session_index);
1221 vlib_log_err (bm->log_class, "BUG: event type 0x%wx", event_type);
1224 BFD_DBG ("advancing wheel, now is %lu", now);
1225 BFD_DBG ("timing_wheel_advance (%p, %lu, %p, 0);", &bm->wheel, now,
1228 expired = timing_wheel_advance (&bm->wheel, now, expired, 0);
1229 BFD_DBG ("Expired %d elements", vec_len (expired));
1231 vec_foreach (p, expired)
1233 const u32 bs_idx = *p;
1234 if (!pool_is_free_index (bm->sessions, bs_idx))
1236 bfd_session_t *bs = pool_elt_at_index (bm->sessions, bs_idx);
1237 bfd_on_timeout (vm, rt, bm, bs, now);
1238 bfd_set_timer (bm, bs, now, 1);
1244 _vec_len (expired) = 0;
1248 _vec_len (event_data) = 0;
1256 * bfd process node declaration
1259 VLIB_REGISTER_NODE (bfd_process_node, static) = {
1260 .function = bfd_process,
1261 .type = VLIB_NODE_TYPE_PROCESS,
1262 .name = "bfd-process",
1268 static clib_error_t *
1269 bfd_sw_interface_up_down (vnet_main_t * vnm, u32 sw_if_index, u32 flags)
1271 // bfd_main_t *bm = &bfd_main;
1272 // vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
1273 if (!(flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
1280 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (bfd_sw_interface_up_down);
1282 static clib_error_t *
1283 bfd_hw_interface_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
1285 // bfd_main_t *bm = &bfd_main;
1286 if (flags & VNET_HW_INTERFACE_FLAG_LINK_UP)
1293 VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION (bfd_hw_interface_up_down);
1296 bfd_register_listener (bfd_notify_fn_t fn)
1298 bfd_main_t *bm = &bfd_main;
1300 vec_add1 (bm->listeners, fn);
1306 static clib_error_t *
1307 bfd_main_init (vlib_main_t * vm)
1309 vlib_thread_main_t *tm = &vlib_thread_main;
1310 u32 n_vlib_mains = tm->n_vlib_mains;
1312 setbuf (stdout, NULL);
1314 bfd_main_t *bm = &bfd_main;
1315 bm->random_seed = random_default_seed ();
1317 bm->vnet_main = vnet_get_main ();
1318 clib_memset (&bm->wheel, 0, sizeof (bm->wheel));
1319 bm->cpu_cps = vm->clib_time.clocks_per_second;
1320 BFD_DBG ("cps is %.2f", bm->cpu_cps);
1321 bm->default_desired_min_tx_clocks =
1322 bfd_usec_to_clocks (bm, BFD_DEFAULT_DESIRED_MIN_TX_USEC);
1323 bm->min_required_min_rx_while_echo_clocks =
1324 bfd_usec_to_clocks (bm, BFD_REQUIRED_MIN_RX_USEC_WHILE_ECHO);
1325 const u64 now = clib_cpu_time_now ();
1326 timing_wheel_init (&bm->wheel, now, bm->cpu_cps);
1327 bm->wheel_inaccuracy = 2 << bm->wheel.log2_clocks_per_bin;
1328 bm->log_class = vlib_log_register_class ("bfd", 0);
1329 vlib_log_debug (bm->log_class, "initialized");
1330 bm->owner_thread_index = ~0;
1331 if (n_vlib_mains > 1)
1332 clib_spinlock_init (&bm->lock);
1336 VLIB_INIT_FUNCTION (bfd_main_init);
1339 bfd_get_session (bfd_main_t * bm, bfd_transport_e t)
1341 bfd_session_t *result;
1345 pool_get (bm->sessions, result);
1346 clib_memset (result, 0, sizeof (*result));
1347 result->bs_idx = result - bm->sessions;
1348 result->transport = t;
1349 const unsigned limit = 1000;
1350 unsigned counter = 0;
1353 result->local_discr = random_u32 (&bm->random_seed);
1354 if (counter > limit)
1356 vlib_log_crit (bm->log_class,
1357 "couldn't allocate unused session discriminator even "
1358 "after %u tries!", limit);
1359 pool_put (bm->sessions, result);
1365 while (hash_get (bm->session_by_disc, result->local_discr));
1366 bfd_set_defaults (bm, result);
1367 hash_set (bm->session_by_disc, result->local_discr, result->bs_idx);
1373 bfd_put_session (bfd_main_t * bm, bfd_session_t * bs)
1377 vlib_log_info (bm->log_class, "delete session: %U",
1378 format_bfd_session_brief, bs);
1379 bfd_notify_listeners (bm, BFD_LISTEN_EVENT_DELETE, bs);
1380 if (bs->auth.curr_key)
1382 --bs->auth.curr_key->use_count;
1384 if (bs->auth.next_key)
1386 --bs->auth.next_key->use_count;
1388 hash_unset (bm->session_by_disc, bs->local_discr);
1389 pool_put (bm->sessions, bs);
1394 bfd_find_session_by_idx (bfd_main_t * bm, uword bs_idx)
1396 bfd_lock_check (bm);
1397 if (!pool_is_free_index (bm->sessions, bs_idx))
1399 return pool_elt_at_index (bm->sessions, bs_idx);
1405 bfd_find_session_by_disc (bfd_main_t * bm, u32 disc)
1407 bfd_lock_check (bm);
1408 uword *p = hash_get (bfd_main.session_by_disc, disc);
1411 return pool_elt_at_index (bfd_main.sessions, *p);
1417 * @brief verify bfd packet - common checks
1421 * @return 1 if bfd packet is valid
1424 bfd_verify_pkt_common (const bfd_pkt_t * pkt)
1426 if (1 != bfd_pkt_get_version (pkt))
1428 BFD_ERR ("BFD verification failed - unexpected version: '%d'",
1429 bfd_pkt_get_version (pkt));
1432 if (pkt->head.length < sizeof (bfd_pkt_t) ||
1433 (bfd_pkt_get_auth_present (pkt) &&
1434 pkt->head.length < sizeof (bfd_pkt_with_common_auth_t)))
1436 BFD_ERR ("BFD verification failed - unexpected length: '%d' (auth "
1438 pkt->head.length, bfd_pkt_get_auth_present (pkt));
1441 if (!pkt->head.detect_mult)
1443 BFD_ERR ("BFD verification failed - unexpected detect-mult: '%d'",
1444 pkt->head.detect_mult);
1447 if (bfd_pkt_get_multipoint (pkt))
1449 BFD_ERR ("BFD verification failed - unexpected multipoint: '%d'",
1450 bfd_pkt_get_multipoint (pkt));
1455 BFD_ERR ("BFD verification failed - unexpected my-disc: '%d'",
1459 if (!pkt->your_disc)
1461 const u8 pkt_state = bfd_pkt_get_state (pkt);
1462 if (pkt_state != BFD_STATE_down && pkt_state != BFD_STATE_admin_down)
1464 BFD_ERR ("BFD verification failed - unexpected state: '%s' "
1465 "(your-disc is zero)", bfd_state_string (pkt_state));
1473 bfd_session_switch_auth_to_next (bfd_session_t * bs)
1475 BFD_DBG ("Switching authentication key from %U to %U for bs_idx=%u",
1476 format_bfd_auth_key, bs->auth.curr_key, format_bfd_auth_key,
1477 bs->auth.next_key, bs->bs_idx);
1478 bs->auth.is_delayed = 0;
1479 if (bs->auth.curr_key)
1481 --bs->auth.curr_key->use_count;
1483 bs->auth.curr_key = bs->auth.next_key;
1484 bs->auth.next_key = NULL;
1485 bs->auth.curr_bfd_key_id = bs->auth.next_bfd_key_id;
1489 bfd_auth_type_is_meticulous (bfd_auth_type_e auth_type)
1491 if (BFD_AUTH_TYPE_meticulous_keyed_md5 == auth_type ||
1492 BFD_AUTH_TYPE_meticulous_keyed_sha1 == auth_type)
1500 bfd_verify_pkt_auth_seq_num (bfd_session_t * bs,
1501 u32 received_seq_num, int is_meticulous)
1506 * This variable MUST be set to zero after no packets have been
1507 * received on this session for at least twice the Detection Time.
1509 u64 now = clib_cpu_time_now ();
1510 if (now - bs->last_rx_clocks > bs->detection_time_clocks * 2)
1512 BFD_DBG ("BFD peer unresponsive for %lu clocks, which is > 2 * "
1513 "detection_time=%u clocks, resetting remote_seq_number_known "
1515 now - bs->last_rx_clocks, bs->detection_time_clocks * 2);
1516 bs->auth.remote_seq_number_known = 0;
1518 if (bs->auth.remote_seq_number_known)
1520 /* remote sequence number is known, verify its validity */
1521 const u32 max_u32 = 0xffffffff;
1522 /* the calculation might wrap, account for the special case... */
1523 if (bs->auth.remote_seq_number > max_u32 - 3 * bs->local_detect_mult)
1529 * |----------+----------------------------+-----------|
1531 * | remote_seq_num------+
1533 * +-----(remote_seq_num + 3*detect_mult) % * 0xffffffff
1535 * x + y + z = 0xffffffff
1536 * x + z = 3 * detect_mult
1538 const u32 z = max_u32 - bs->auth.remote_seq_number;
1539 const u32 x = 3 * bs->local_detect_mult - z;
1540 if (received_seq_num > x &&
1541 received_seq_num < bs->auth.remote_seq_number + is_meticulous)
1544 ("Recvd sequence number=%u out of ranges <0, %u>, <%u, %u>",
1545 received_seq_num, x,
1546 bs->auth.remote_seq_number + is_meticulous, max_u32);
1553 const u32 min = bs->auth.remote_seq_number + is_meticulous;
1555 bs->auth.remote_seq_number + 3 * bs->local_detect_mult;
1556 if (received_seq_num < min || received_seq_num > max)
1558 BFD_ERR ("Recvd sequence number=%u out of range <%u, %u>",
1559 received_seq_num, min, max);
1568 bfd_verify_pkt_auth_key_sha1 (const bfd_pkt_t * pkt, u32 pkt_size,
1569 bfd_session_t * bs, u8 bfd_key_id,
1570 bfd_auth_key_t * auth_key)
1572 ASSERT (auth_key->auth_type == BFD_AUTH_TYPE_keyed_sha1 ||
1573 auth_key->auth_type == BFD_AUTH_TYPE_meticulous_keyed_sha1);
1575 u8 result[SHA_DIGEST_LENGTH];
1576 bfd_pkt_with_common_auth_t *with_common = (void *) pkt;
1577 if (pkt_size < sizeof (*with_common))
1579 BFD_ERR ("Packet size too small to hold authentication common header");
1582 if (with_common->common_auth.type != auth_key->auth_type)
1584 BFD_ERR ("BFD auth type mismatch, packet auth=%d:%s doesn't match "
1585 "in-use auth=%d:%s",
1586 with_common->common_auth.type,
1587 bfd_auth_type_str (with_common->common_auth.type),
1588 auth_key->auth_type, bfd_auth_type_str (auth_key->auth_type));
1591 bfd_pkt_with_sha1_auth_t *with_sha1 = (void *) pkt;
1592 if (pkt_size < sizeof (*with_sha1) ||
1593 with_sha1->sha1_auth.type_len.len < sizeof (with_sha1->sha1_auth))
1596 ("BFD size mismatch, payload size=%u, expected=%u, auth_len=%u, "
1597 "expected=%u", pkt_size, sizeof (*with_sha1),
1598 with_sha1->sha1_auth.type_len.len, sizeof (with_sha1->sha1_auth));
1601 if (with_sha1->sha1_auth.key_id != bfd_key_id)
1604 ("BFD key ID mismatch, packet key ID=%u doesn't match key ID=%u%s",
1605 with_sha1->sha1_auth.key_id, bfd_key_id,
1607 auth.is_delayed ? " (but a delayed auth change is scheduled)" : "");
1611 if (!SHA1_Init (&ctx))
1613 BFD_ERR ("SHA1_Init failed");
1616 /* ignore last 20 bytes - use the actual key data instead pkt data */
1617 if (!SHA1_Update (&ctx, with_sha1,
1618 sizeof (*with_sha1) - sizeof (with_sha1->sha1_auth.hash)))
1620 BFD_ERR ("SHA1_Update failed");
1623 if (!SHA1_Update (&ctx, auth_key->key, sizeof (auth_key->key)))
1625 BFD_ERR ("SHA1_Update failed");
1628 if (!SHA1_Final (result, &ctx))
1630 BFD_ERR ("SHA1_Final failed");
1633 if (0 == memcmp (result, with_sha1->sha1_auth.hash, SHA_DIGEST_LENGTH))
1637 BFD_ERR ("SHA1 hash: %U doesn't match the expected value: %U",
1638 format_hex_bytes, with_sha1->sha1_auth.hash, SHA_DIGEST_LENGTH,
1639 format_hex_bytes, result, SHA_DIGEST_LENGTH);
1644 bfd_verify_pkt_auth_key (const bfd_pkt_t * pkt, u32 pkt_size,
1645 bfd_session_t * bs, u8 bfd_key_id,
1646 bfd_auth_key_t * auth_key)
1648 bfd_main_t *bm = &bfd_main;
1649 switch (auth_key->auth_type)
1651 case BFD_AUTH_TYPE_reserved:
1652 vlib_log_err (bm->log_class,
1653 "internal error, unexpected auth_type=%d:%s",
1654 auth_key->auth_type,
1655 bfd_auth_type_str (auth_key->auth_type));
1657 case BFD_AUTH_TYPE_simple_password:
1658 vlib_log_err (bm->log_class,
1659 "internal error, not implemented, unexpected auth_type=%d:%s",
1660 auth_key->auth_type,
1661 bfd_auth_type_str (auth_key->auth_type));
1663 case BFD_AUTH_TYPE_keyed_md5:
1665 case BFD_AUTH_TYPE_meticulous_keyed_md5:
1668 "internal error, not implemented, unexpected auth_type=%d:%s",
1669 auth_key->auth_type, bfd_auth_type_str (auth_key->auth_type));
1671 case BFD_AUTH_TYPE_keyed_sha1:
1673 case BFD_AUTH_TYPE_meticulous_keyed_sha1:
1677 const u32 seq_num = clib_net_to_host_u32 (((bfd_pkt_with_sha1_auth_t
1680 return bfd_verify_pkt_auth_seq_num (bs, seq_num,
1681 bfd_auth_type_is_meticulous
1682 (auth_key->auth_type))
1683 && bfd_verify_pkt_auth_key_sha1 (pkt, pkt_size, bs, bfd_key_id,
1690 "internal error, attempt to use SHA1 without SSL support");
1698 * @brief verify bfd packet - authentication
1702 * @return 1 if bfd packet is valid
1705 bfd_verify_pkt_auth (const bfd_pkt_t * pkt, u16 pkt_size, bfd_session_t * bs)
1707 if (bfd_pkt_get_auth_present (pkt))
1709 /* authentication present in packet */
1710 if (!bs->auth.curr_key)
1712 /* currently not using authentication - can we turn it on? */
1713 if (bs->auth.is_delayed && bs->auth.next_key)
1715 /* yes, switch is scheduled - make sure the auth is valid */
1716 if (bfd_verify_pkt_auth_key (pkt, pkt_size, bs,
1717 bs->auth.next_bfd_key_id,
1720 /* auth matches next key, do the switch, packet is valid */
1721 bfd_session_switch_auth_to_next (bs);
1728 /* yes, using authentication, verify the key */
1729 if (bfd_verify_pkt_auth_key (pkt, pkt_size, bs,
1730 bs->auth.curr_bfd_key_id,
1733 /* verification passed, packet is valid */
1738 /* verification failed - but maybe we need to switch key */
1739 if (bs->auth.is_delayed && bs->auth.next_key)
1741 /* delayed switch present, verify if that key works */
1742 if (bfd_verify_pkt_auth_key (pkt, pkt_size, bs,
1743 bs->auth.next_bfd_key_id,
1746 /* auth matches next key, switch key, packet is valid */
1747 bfd_session_switch_auth_to_next (bs);
1756 /* authentication in packet not present */
1757 if (pkt_size > sizeof (*pkt))
1759 BFD_ERR ("BFD verification failed - unexpected packet size '%d' "
1760 "(auth not present)", pkt_size);
1763 if (bs->auth.curr_key)
1765 /* currently authenticating - could we turn it off? */
1766 if (bs->auth.is_delayed && !bs->auth.next_key)
1768 /* yes, delayed switch to NULL key is scheduled */
1769 bfd_session_switch_auth_to_next (bs);
1775 /* no auth in packet, no auth in use - packet is valid */
1783 bfd_consume_pkt (bfd_main_t * bm, const bfd_pkt_t * pkt, u32 bs_idx)
1785 bfd_lock_check (bm);
1787 bfd_session_t *bs = bfd_find_session_by_idx (bm, bs_idx);
1788 if (!bs || (pkt->your_disc && pkt->your_disc != bs->local_discr))
1792 BFD_DBG ("Scanning bfd packet, bs_idx=%d", bs->bs_idx);
1793 bs->remote_discr = pkt->my_disc;
1794 bs->remote_state = bfd_pkt_get_state (pkt);
1795 bs->remote_demand = bfd_pkt_get_demand (pkt);
1796 bs->remote_diag = bfd_pkt_get_diag_code (pkt);
1797 u64 now = clib_cpu_time_now ();
1798 bs->last_rx_clocks = now;
1799 if (bfd_pkt_get_auth_present (pkt))
1801 bfd_auth_type_e auth_type =
1802 ((bfd_pkt_with_common_auth_t *) (pkt))->common_auth.type;
1805 case BFD_AUTH_TYPE_reserved:
1807 case BFD_AUTH_TYPE_simple_password:
1809 case BFD_AUTH_TYPE_keyed_md5:
1811 case BFD_AUTH_TYPE_meticulous_keyed_md5:
1812 vlib_log_crit (bm->log_class,
1813 "internal error, unexpected auth_type=%d:%s",
1814 auth_type, bfd_auth_type_str (auth_type));
1816 case BFD_AUTH_TYPE_keyed_sha1:
1818 case BFD_AUTH_TYPE_meticulous_keyed_sha1:
1821 bfd_pkt_with_sha1_auth_t *with_sha1 =
1822 (bfd_pkt_with_sha1_auth_t *) pkt;
1823 bs->auth.remote_seq_number =
1824 clib_net_to_host_u32 (with_sha1->sha1_auth.seq_num);
1825 bs->auth.remote_seq_number_known = 1;
1826 BFD_DBG ("Received sequence number %u",
1827 bs->auth.remote_seq_number);
1832 bs->remote_desired_min_tx_clocks =
1833 bfd_usec_to_clocks (bm, clib_net_to_host_u32 (pkt->des_min_tx));
1834 bs->remote_detect_mult = pkt->head.detect_mult;
1835 bfd_set_remote_required_min_rx (bm, bs, now,
1836 clib_net_to_host_u32 (pkt->req_min_rx));
1837 bfd_set_remote_required_min_echo_rx (bm, bs, now,
1838 clib_net_to_host_u32
1839 (pkt->req_min_echo_rx));
1840 if (bfd_pkt_get_final (pkt))
1842 if (BFD_POLL_IN_PROGRESS == bs->poll_state)
1844 BFD_DBG ("Poll sequence terminated, bs_idx=%u", bs->bs_idx);
1845 bfd_set_poll_state (bs, BFD_POLL_NOT_NEEDED);
1846 if (BFD_STATE_up == bs->local_state)
1848 bfd_set_effective_required_min_rx (bm, bs,
1849 clib_max (bs->echo *
1850 bm->min_required_min_rx_while_echo_clocks,
1851 bs->config_required_min_rx_clocks));
1854 else if (BFD_POLL_IN_PROGRESS_AND_QUEUED == bs->poll_state)
1857 * next poll sequence must be delayed by at least the round trip
1858 * time, so calculate that here
1860 BFD_DBG ("Next poll sequence can commence in " BFD_CLK_FMT,
1862 bs->poll_state_start_or_timeout_clocks));
1863 bs->poll_state_start_or_timeout_clocks =
1864 now + (now - bs->poll_state_start_or_timeout_clocks);
1866 ("Poll sequence terminated, but another is needed, bs_idx=%u",
1868 bfd_set_poll_state (bs, BFD_POLL_NEEDED);
1871 bfd_calc_next_tx (bm, bs, now);
1872 bfd_set_timer (bm, bs, now, 0);
1873 if (BFD_STATE_admin_down == bs->local_state)
1875 BFD_DBG ("Session is admin-down, ignoring packet, bs_idx=%u",
1879 if (BFD_STATE_admin_down == bs->remote_state)
1881 bfd_set_diag (bs, BFD_DIAG_CODE_neighbor_sig_down);
1882 bfd_set_state (bm, bs, BFD_STATE_down, 0);
1884 else if (BFD_STATE_down == bs->local_state)
1886 if (BFD_STATE_down == bs->remote_state)
1888 bfd_set_diag (bs, BFD_DIAG_CODE_no_diag);
1889 bfd_set_state (bm, bs, BFD_STATE_init, 0);
1891 else if (BFD_STATE_init == bs->remote_state)
1893 bfd_set_diag (bs, BFD_DIAG_CODE_no_diag);
1894 bfd_set_state (bm, bs, BFD_STATE_up, 0);
1897 else if (BFD_STATE_init == bs->local_state)
1899 if (BFD_STATE_up == bs->remote_state ||
1900 BFD_STATE_init == bs->remote_state)
1902 bfd_set_diag (bs, BFD_DIAG_CODE_no_diag);
1903 bfd_set_state (bm, bs, BFD_STATE_up, 0);
1906 else /* BFD_STATE_up == bs->local_state */
1908 if (BFD_STATE_down == bs->remote_state)
1910 bfd_set_diag (bs, BFD_DIAG_CODE_neighbor_sig_down);
1911 bfd_set_state (bm, bs, BFD_STATE_down, 0);
1917 bfd_consume_echo_pkt (bfd_main_t * bm, vlib_buffer_t * b)
1919 bfd_echo_pkt_t *pkt = NULL;
1920 if (b->current_length != sizeof (*pkt))
1924 pkt = vlib_buffer_get_current (b);
1925 bfd_session_t *bs = bfd_find_session_by_disc (bm, pkt->discriminator);
1930 BFD_DBG ("Scanning bfd echo packet, bs_idx=%d", bs->bs_idx);
1932 bfd_calc_echo_checksum (bs->local_discr, pkt->expire_time_clocks,
1934 if (checksum != pkt->checksum)
1936 BFD_DBG ("Invalid echo packet, checksum mismatch");
1939 u64 now = clib_cpu_time_now ();
1940 if (pkt->expire_time_clocks < now)
1942 BFD_DBG ("Stale packet received, expire time %lu < now %lu",
1943 pkt->expire_time_clocks, now);
1947 bs->echo_last_rx_clocks = now;
1953 format_bfd_session (u8 * s, va_list * args)
1955 const bfd_session_t *bs = va_arg (*args, bfd_session_t *);
1956 u32 indent = format_get_indent (s) + vlib_log_get_indent ();
1957 s = format (s, "bs_idx=%u local-state=%s remote-state=%s\n"
1958 "%Ulocal-discriminator=%u remote-discriminator=%u\n"
1959 "%Ulocal-diag=%s echo-active=%s\n"
1960 "%Udesired-min-tx=%u required-min-rx=%u\n"
1961 "%Urequired-min-echo-rx=%u detect-mult=%u\n"
1962 "%Uremote-min-rx=%u remote-min-echo-rx=%u\n"
1963 "%Uremote-demand=%s poll-state=%s\n"
1964 "%Uauth: local-seq-num=%u remote-seq-num=%u\n"
1965 "%U is-delayed=%s\n"
1968 bs->bs_idx, bfd_state_string (bs->local_state),
1969 bfd_state_string (bs->remote_state), format_white_space, indent,
1970 bs->local_discr, bs->remote_discr, format_white_space, indent,
1971 bfd_diag_code_string (bs->local_diag),
1972 (bs->echo ? "yes" : "no"), format_white_space, indent,
1973 bs->config_desired_min_tx_usec, bs->config_required_min_rx_usec,
1974 format_white_space, indent, 1, bs->local_detect_mult,
1975 format_white_space, indent, bs->remote_min_rx_usec,
1976 bs->remote_min_echo_rx_usec, format_white_space, indent,
1977 (bs->remote_demand ? "yes" : "no"),
1978 bfd_poll_state_string (bs->poll_state), format_white_space,
1979 indent, bs->auth.local_seq_number, bs->auth.remote_seq_number,
1980 format_white_space, indent,
1981 (bs->auth.is_delayed ? "yes" : "no"), format_white_space,
1982 indent, format_bfd_auth_key, bs->auth.curr_key,
1983 format_white_space, indent, format_bfd_auth_key,
1989 format_bfd_session_brief (u8 * s, va_list * args)
1991 const bfd_session_t *bs = va_arg (*args, bfd_session_t *);
1993 format (s, "bs_idx=%u local-state=%s remote-state=%s", bs->bs_idx,
1994 bfd_state_string (bs->local_state),
1995 bfd_state_string (bs->remote_state));
2000 bfd_auth_type_supported (bfd_auth_type_e auth_type)
2002 if (auth_type == BFD_AUTH_TYPE_keyed_sha1 ||
2003 auth_type == BFD_AUTH_TYPE_meticulous_keyed_sha1)
2011 bfd_auth_activate (bfd_session_t * bs, u32 conf_key_id,
2012 u8 bfd_key_id, u8 is_delayed)
2014 bfd_main_t *bm = &bfd_main;
2015 const uword *key_idx_p =
2016 hash_get (bm->auth_key_by_conf_key_id, conf_key_id);
2019 vlib_log_err (bm->log_class,
2020 "authentication key with config ID %u doesn't exist)",
2022 return VNET_API_ERROR_BFD_ENOENT;
2024 const uword key_idx = *key_idx_p;
2025 bfd_auth_key_t *key = pool_elt_at_index (bm->auth_keys, key_idx);
2028 if (bs->auth.next_key == key)
2030 /* already using this key, no changes required */
2033 bs->auth.next_key = key;
2034 bs->auth.next_bfd_key_id = bfd_key_id;
2035 bs->auth.is_delayed = 1;
2039 if (bs->auth.curr_key == key)
2041 /* already using this key, no changes required */
2044 if (bs->auth.curr_key)
2046 --bs->auth.curr_key->use_count;
2048 bs->auth.curr_key = key;
2049 bs->auth.curr_bfd_key_id = bfd_key_id;
2050 bs->auth.is_delayed = 0;
2053 BFD_DBG ("\nSession auth modified: %U", format_bfd_session, bs);
2054 vlib_log_info (bm->log_class, "session auth modified: %U",
2055 format_bfd_session_brief, bs);
2060 bfd_auth_deactivate (bfd_session_t * bs, u8 is_delayed)
2062 bfd_main_t *bm = &bfd_main;
2066 /* not delayed - deactivate the current key right now */
2067 if (bs->auth.curr_key)
2069 --bs->auth.curr_key->use_count;
2070 bs->auth.curr_key = NULL;
2072 bs->auth.is_delayed = 0;
2076 /* delayed - mark as so */
2077 bs->auth.is_delayed = 1;
2080 * clear the next key unconditionally - either the auth change is not delayed
2081 * in which case the caller expects the session to not use authentication
2082 * from this point forward, or it is delayed, in which case the next_key
2083 * needs to be set to NULL to make it so in the future
2085 if (bs->auth.next_key)
2087 --bs->auth.next_key->use_count;
2088 bs->auth.next_key = NULL;
2090 BFD_DBG ("\nSession auth modified: %U", format_bfd_session, bs);
2091 vlib_log_info (bm->log_class, "session auth modified: %U",
2092 format_bfd_session_brief, bs);
2095 vlib_log_err (bm->log_class,
2096 "SSL missing, cannot deactivate BFD authentication");
2097 return VNET_API_ERROR_BFD_NOTSUPP;
2102 bfd_session_set_params (bfd_main_t * bm, bfd_session_t * bs,
2103 u32 desired_min_tx_usec,
2104 u32 required_min_rx_usec, u8 detect_mult)
2106 if (bs->local_detect_mult != detect_mult ||
2107 bs->config_desired_min_tx_usec != desired_min_tx_usec ||
2108 bs->config_required_min_rx_usec != required_min_rx_usec)
2110 BFD_DBG ("\nChanging session params: %U", format_bfd_session, bs);
2111 switch (bs->poll_state)
2113 case BFD_POLL_NOT_NEEDED:
2114 if (BFD_STATE_up == bs->local_state ||
2115 BFD_STATE_init == bs->local_state)
2117 /* poll sequence is not needed for detect multiplier change */
2118 if (bs->config_desired_min_tx_usec != desired_min_tx_usec ||
2119 bs->config_required_min_rx_usec != required_min_rx_usec)
2121 bfd_set_poll_state (bs, BFD_POLL_NEEDED);
2125 case BFD_POLL_NEEDED:
2126 case BFD_POLL_IN_PROGRESS_AND_QUEUED:
2128 * nothing to do - will be handled in the future poll which is
2129 * already scheduled for execution
2132 case BFD_POLL_IN_PROGRESS:
2133 /* poll sequence is not needed for detect multiplier change */
2134 if (bs->config_desired_min_tx_usec != desired_min_tx_usec ||
2135 bs->config_required_min_rx_usec != required_min_rx_usec)
2137 BFD_DBG ("Poll in progress, queueing extra poll, bs_idx=%u",
2139 bfd_set_poll_state (bs, BFD_POLL_IN_PROGRESS_AND_QUEUED);
2143 bs->local_detect_mult = detect_mult;
2144 bs->config_desired_min_tx_usec = desired_min_tx_usec;
2145 bs->config_desired_min_tx_clocks =
2146 bfd_usec_to_clocks (bm, desired_min_tx_usec);
2147 bs->config_required_min_rx_usec = required_min_rx_usec;
2148 bs->config_required_min_rx_clocks =
2149 bfd_usec_to_clocks (bm, required_min_rx_usec);
2150 BFD_DBG ("\nChanged session params: %U", format_bfd_session, bs);
2152 vlib_log_info (bm->log_class, "changed session params: %U",
2153 format_bfd_session_brief, bs);
2154 vlib_process_signal_event (bm->vlib_main, bm->bfd_process_node_index,
2155 BFD_EVENT_CONFIG_CHANGED, bs->bs_idx);
2159 BFD_DBG ("Ignore parameter change - no change, bs_idx=%u", bs->bs_idx);
2165 bfd_auth_set_key (u32 conf_key_id, u8 auth_type, u8 key_len,
2166 const u8 * key_data)
2168 bfd_main_t *bm = &bfd_main;
2170 bfd_auth_key_t *auth_key = NULL;
2171 if (!key_len || key_len > bfd_max_key_len_for_auth_type (auth_type))
2173 vlib_log_err (bm->log_class,
2174 "invalid authentication key length for auth_type=%d:%s "
2175 "(key_len=%u, must be non-zero, expected max=%u)",
2176 auth_type, bfd_auth_type_str (auth_type), key_len,
2177 (u32) bfd_max_key_len_for_auth_type (auth_type));
2178 return VNET_API_ERROR_INVALID_VALUE;
2180 if (!bfd_auth_type_supported (auth_type))
2182 vlib_log_err (bm->log_class, "unsupported auth type=%d:%s", auth_type,
2183 bfd_auth_type_str (auth_type));
2184 return VNET_API_ERROR_BFD_NOTSUPP;
2186 uword *key_idx_p = hash_get (bm->auth_key_by_conf_key_id, conf_key_id);
2189 /* modifying existing key - must not be used */
2190 const uword key_idx = *key_idx_p;
2191 auth_key = pool_elt_at_index (bm->auth_keys, key_idx);
2192 if (auth_key->use_count > 0)
2194 vlib_log_err (bm->log_class,
2195 "authentication key with conf ID %u in use by %u BFD "
2196 "session(s) - cannot modify", conf_key_id,
2197 auth_key->use_count);
2198 return VNET_API_ERROR_BFD_EINUSE;
2203 /* adding new key */
2204 pool_get (bm->auth_keys, auth_key);
2205 auth_key->conf_key_id = conf_key_id;
2206 hash_set (bm->auth_key_by_conf_key_id, conf_key_id,
2207 auth_key - bm->auth_keys);
2209 auth_key->auth_type = auth_type;
2210 clib_memset (auth_key->key, 0, sizeof (auth_key->key));
2211 clib_memcpy (auth_key->key, key_data, key_len);
2214 vlib_log_err (bm->log_class,
2215 "SSL missing, cannot manipulate authentication keys");
2216 return VNET_API_ERROR_BFD_NOTSUPP;
2221 bfd_auth_del_key (u32 conf_key_id)
2224 bfd_auth_key_t *auth_key = NULL;
2225 bfd_main_t *bm = &bfd_main;
2226 uword *key_idx_p = hash_get (bm->auth_key_by_conf_key_id, conf_key_id);
2229 /* deleting existing key - must not be used */
2230 const uword key_idx = *key_idx_p;
2231 auth_key = pool_elt_at_index (bm->auth_keys, key_idx);
2232 if (auth_key->use_count > 0)
2234 vlib_log_err (bm->log_class,
2235 "authentication key with conf ID %u in use by %u BFD "
2236 "session(s) - cannot delete", conf_key_id,
2237 auth_key->use_count);
2238 return VNET_API_ERROR_BFD_EINUSE;
2240 hash_unset (bm->auth_key_by_conf_key_id, conf_key_id);
2241 clib_memset (auth_key, 0, sizeof (*auth_key));
2242 pool_put (bm->auth_keys, auth_key);
2247 vlib_log_err (bm->log_class,
2248 "authentication key with conf ID %u does not exist",
2250 return VNET_API_ERROR_BFD_ENOENT;
2254 vlib_log_err (bm->log_class,
2255 "SSL missing, cannot manipulate authentication keys");
2256 return VNET_API_ERROR_BFD_NOTSUPP;
2260 bfd_main_t bfd_main;
2263 * fd.io coding-style-patch-verification: ON
2266 * eval: (c-set-style "gnu")