FD.io VPP  v19.08.1-401-g8e4ed521a
Vector Packet Processing
bfd_main.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011-2016 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 /**
16  * @file
17  * @brief BFD nodes implementation
18  */
19 
20 #if WITH_LIBSSL > 0
21 #include <openssl/sha.h>
22 #endif
23 
24 #if __SSE4_2__
25 #include <x86intrin.h>
26 #endif
27 
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>
38 #include <vlib/log.h>
39 
40 static u64
41 bfd_calc_echo_checksum (u32 discriminator, u64 expire_time, u32 secret)
42 {
43  u64 checksum = 0;
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);
48 #else
49  checksum = clib_xxhash (discriminator ^ expire_time ^ secret);
50 #endif
51  return checksum;
52 }
53 
54 static u64
56 {
57  return bm->cpu_cps * ((f64) us / USEC_PER_SECOND);
58 }
59 
60 u32
61 bfd_clocks_to_usec (const bfd_main_t * bm, u64 clocks)
62 {
63  return (clocks / bm->cpu_cps) * USEC_PER_SECOND;
64 }
65 
67 
68 u8 *
69 format_bfd_auth_key (u8 * s, va_list * args)
70 {
71  const bfd_auth_key_t *key = va_arg (*args, bfd_auth_key_t *);
72  if (key)
73  {
74  s = format (s, "{auth-type=%u:%s, conf-key-id=%u, use-count=%u}, ",
76  key->conf_key_id, key->use_count);
77  }
78  else
79  {
80  s = format (s, "{none}");
81  }
82  return s;
83 }
84 
85 /*
86  * We actually send all bfd pkts to the "error" node after scanning
87  * them, so the graph node has only one next-index. The "error-drop"
88  * node automatically bumps our per-node packet counters for us.
89  */
90 typedef enum
91 {
95 
96 static void bfd_on_state_change (bfd_main_t * bm, bfd_session_t * bs, u64 now,
97  int handling_wakeup);
98 
99 static void
101 {
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;
110  bs->remote_min_rx_usec = 1;
112  bs->remote_min_echo_rx_usec = 0;
114  bs->remote_demand = 0;
115  bs->auth.remote_seq_number = 0;
118  bs->echo_secret = random_u32 (&bm->random_seed);
119 }
120 
121 static void
123 {
124  if (bs->local_diag != code)
125  {
126  BFD_DBG ("set local_diag, bs_idx=%d: '%d:%s'", bs->bs_idx, code,
127  bfd_diag_code_string (code));
128  bs->local_diag = code;
129  }
130 }
131 
132 static void
134  bfd_state_e new_state, int handling_wakeup)
135 {
136  if (bs->local_state != new_state)
137  {
138  BFD_DBG ("Change state, bs_idx=%d: %s->%s", bs->bs_idx,
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);
143  }
144 }
145 
146 const char *
148 {
149  switch (state)
150  {
151 #define F(x) \
152  case BFD_POLL_##x: \
153  return "BFD_POLL_" #x;
155 #undef F
156  }
157  return "UNKNOWN";
158 }
159 
160 static void
162 {
163  if (bs->poll_state != state)
164  {
165  BFD_DBG ("Setting poll state=%s, bs_idx=%u",
166  bfd_poll_state_string (state), bs->bs_idx);
167  bs->poll_state = state;
168  }
169 }
170 
171 static void
173 {
176  BFD_DBG ("Recalculated transmit interval " BFD_CLK_FMT,
177  BFD_CLK_PRN (bs->transmit_interval_clocks));
178 }
179 
180 static void
182 {
186  BFD_DBG ("Recalculated echo transmit interval " BFD_CLK_FMT,
187  BFD_CLK_PRN (bs->echo_transmit_interval_clocks));
188 }
189 
190 static void
192 {
193  if (bs->local_detect_mult > 1)
194  {
195  /* common case - 75-100% of transmit interval */
197  (1 - .25 * (random_f64 (&bm->random_seed))) *
199  if (bs->tx_timeout_clocks < now)
200  {
201  /*
202  * the timeout is in the past, which means that either remote
203  * demand mode was set or performance/clock issues ...
204  */
205  BFD_DBG ("Missed %lu transmit events (now is %lu, calc "
206  "tx_timeout is %lu)",
207  (now - bs->tx_timeout_clocks) /
209  bs->tx_timeout_clocks = now;
210  }
211  }
212  else
213  {
214  /* special case - 75-90% of transmit interval */
216  (.9 - .15 * (random_f64 (&bm->random_seed))) *
218  if (bs->tx_timeout_clocks < now)
219  {
220  /*
221  * the timeout is in the past, which means that either remote
222  * demand mode was set or performance/clock issues ...
223  */
224  BFD_DBG ("Missed %lu transmit events (now is %lu, calc "
225  "tx_timeout is %lu)",
226  (now - bs->tx_timeout_clocks) /
228  bs->tx_timeout_clocks = now;
229  }
230  }
231  if (bs->tx_timeout_clocks)
232  {
233  BFD_DBG ("Next transmit in %lu clocks/%.02fs@%lu",
234  bs->tx_timeout_clocks - now,
235  (bs->tx_timeout_clocks - now) / bm->cpu_cps,
236  bs->tx_timeout_clocks);
237  }
238 }
239 
240 static void
242 {
245  if (bs->echo_tx_timeout_clocks < now)
246  {
247  /* huh, we've missed it already, transmit now */
248  BFD_DBG ("Missed %lu echo transmit events (now is %lu, calc tx_timeout "
249  "is %lu)",
250  (now - bs->echo_tx_timeout_clocks) /
252  now, bs->echo_tx_timeout_clocks);
253  bs->echo_tx_timeout_clocks = now;
254  }
255  BFD_DBG ("Next echo transmit in %lu clocks/%.02fs@%lu",
256  bs->echo_tx_timeout_clocks - now,
257  (bs->echo_tx_timeout_clocks - now) / bm->cpu_cps,
259 }
260 
261 static void
263 {
264  if (bs->local_state == BFD_STATE_init || bs->local_state == BFD_STATE_up)
265  {
267  bs->remote_detect_mult *
270  BFD_DBG ("Recalculated detection time %lu clocks/%.2fs",
272  bs->detection_time_clocks / bm->cpu_cps);
273  }
274 }
275 
276 static void
278  int handling_wakeup)
279 {
280  u64 next = 0;
281  u64 rx_timeout = 0;
282  u64 tx_timeout = 0;
283  if (BFD_STATE_up == bs->local_state)
284  {
285  rx_timeout = bs->last_rx_clocks + bs->detection_time_clocks;
286  }
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)
290  {
291  tx_timeout = bs->tx_timeout_clocks;
292  }
293  if (tx_timeout && rx_timeout)
294  {
295  next = clib_min (tx_timeout, rx_timeout);
296  }
297  else if (tx_timeout)
298  {
299  next = tx_timeout;
300  }
301  else if (rx_timeout)
302  {
303  next = rx_timeout;
304  }
305  if (bs->echo && next > bs->echo_tx_timeout_clocks)
306  {
307  next = bs->echo_tx_timeout_clocks;
308  }
309  BFD_DBG ("bs_idx=%u, tx_timeout=%lu, echo_tx_timeout=%lu, rx_timeout=%lu, "
310  "next=%s",
311  bs->bs_idx, tx_timeout, bs->echo_tx_timeout_clocks, rx_timeout,
312  next == tx_timeout
313  ? "tx" : (next == bs->echo_tx_timeout_clocks ? "echo tx" : "rx"));
314  /* sometimes the wheel expires an event a bit sooner than requested, account
315  for that here */
316  if (next && (now + bm->wheel_inaccuracy > bs->wheel_time_clocks ||
317  next < bs->wheel_time_clocks || !bs->wheel_time_clocks))
318  {
319  int send_signal = 0;
320  bs->wheel_time_clocks = next;
321  BFD_DBG ("timing_wheel_insert(%p, %lu (%ld clocks/%.2fs in the "
322  "future), %u);",
323  &bm->wheel, bs->wheel_time_clocks,
326  bm->cpu_cps, bs->bs_idx);
327  bfd_lock (bm);
329 
330  if (!handling_wakeup)
331  {
332 
333  /* Send only if it is earlier than current awaited wakeup time */
334  send_signal =
336  /*
337  * If the wake-up time is within 2x the delay of the event propagation delay,
338  * avoid the expense of sending the event. The 2x multiplier is to workaround the race whereby
339  * simultaneous event + expired timer create one recurring bogus wakeup/suspend instance,
340  * due to double scheduling of the node on the pending list.
341  */
344  /* Must be no events in flight to send an event */
346 
347  /* If we do send the signal, note this down along with the start timestamp */
348  if (send_signal)
349  {
352  }
353  }
354  bfd_unlock (bm);
355 
356  /* Use the multithreaded event sending so the workers can send events too */
357  if (send_signal)
358  {
362  }
363  }
364 }
365 
366 static void
368  bfd_session_t * bs, u64 now,
369  u64 desired_min_tx_clocks)
370 {
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);
377  bfd_calc_next_tx (bm, bs, now);
378 }
379 
380 static void
382  bfd_session_t * bs,
383  u64 required_min_rx_clocks)
384 {
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);
389 }
390 
391 static void
393  u64 now, u32 remote_required_min_rx_usec)
394 {
395  if (bs->remote_min_rx_usec != remote_required_min_rx_usec)
396  {
397  bs->remote_min_rx_usec = remote_required_min_rx_usec;
399  bfd_usec_to_clocks (bm, remote_required_min_rx_usec);
400  BFD_DBG ("Set remote min rx to " BFD_CLK_FMT,
401  BFD_CLK_PRN (bs->remote_min_rx_clocks));
402  bfd_recalc_detection_time (bm, bs);
403  bfd_recalc_tx_interval (bm, bs);
404  }
405 }
406 
407 static void
409  u64 now,
410  u32 remote_required_min_echo_rx_usec)
411 {
412  if (bs->remote_min_echo_rx_usec != remote_required_min_echo_rx_usec)
413  {
414  bs->remote_min_echo_rx_usec = remote_required_min_echo_rx_usec;
417  BFD_DBG ("Set remote min echo rx to " BFD_CLK_FMT,
418  BFD_CLK_PRN (bs->remote_min_echo_rx_clocks));
420  }
421 }
422 
423 static void
425  bfd_listen_event_e event, const bfd_session_t * bs)
426 {
427  bfd_notify_fn_t *fn;
428  vec_foreach (fn, bm->listeners)
429  {
430  (*fn) (event, bs);
431  }
432 }
433 
434 void
436 {
437  BFD_DBG ("\nStarting session: %U", format_bfd_session, bs);
438  vlib_log_info (bm->log_class, "start BFD session: %U",
442  bfd_recalc_tx_interval (bm, bs);
445  bfd_notify_listeners (bm, BFD_LISTEN_EVENT_CREATE, bs);
446 }
447 
448 void
450 {
451  bfd_main_t *bm = &bfd_main;
452  u64 now = clib_cpu_time_now ();
453  if (admin_up_down)
454  {
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",
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);
462  }
463  else
464  {
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",
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);
472  }
473 }
474 
475 u8 *
476 bfd_input_format_trace (u8 * s, va_list * args)
477 {
478  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
479  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
480  const bfd_input_trace_t *t = va_arg (*args, bfd_input_trace_t *);
481  const bfd_pkt_t *pkt = (bfd_pkt_t *) t->data;
482  if (t->len > STRUCT_SIZE_OF (bfd_pkt_t, head))
483  {
484  s = format (s, "BFD v%u, diag=%u(%s), state=%u(%s),\n"
485  " flags=(P:%u, F:%u, C:%u, A:%u, D:%u, M:%u), "
486  "detect_mult=%u, length=%u\n",
489  bfd_pkt_get_state (pkt),
491  bfd_pkt_get_poll (pkt), bfd_pkt_get_final (pkt),
494  bfd_pkt_get_multipoint (pkt), pkt->head.detect_mult,
495  pkt->head.length);
496  if (t->len >= sizeof (bfd_pkt_t) &&
497  pkt->head.length >= sizeof (bfd_pkt_t))
498  {
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));
509  }
510  if (t->len >= sizeof (bfd_pkt_with_common_auth_t) &&
511  pkt->head.length >= sizeof (bfd_pkt_with_common_auth_t) &&
513  {
514  const bfd_pkt_with_common_auth_t *with_auth = (void *) pkt;
515  const bfd_auth_common_t *common = &with_auth->common_auth;
516  s = format (s, "\n auth len: %u\n", common->len);
517  s = format (s, " auth type: %u:%s\n", common->type,
518  bfd_auth_type_str (common->type));
519  if (t->len >= sizeof (bfd_pkt_with_sha1_auth_t) &&
520  pkt->head.length >= sizeof (bfd_pkt_with_sha1_auth_t) &&
521  (BFD_AUTH_TYPE_keyed_sha1 == common->type ||
522  BFD_AUTH_TYPE_meticulous_keyed_sha1 == common->type))
523  {
524  const bfd_pkt_with_sha1_auth_t *with_sha1 = (void *) pkt;
525  const bfd_auth_sha1_t *sha1 = &with_sha1->sha1_auth;
526  s = format (s, " seq num: %u\n",
527  clib_net_to_host_u32 (sha1->seq_num));
528  s = format (s, " key id: %u\n", sha1->key_id);
529  s = format (s, " hash: %U", format_hex_bytes, sha1->hash,
530  sizeof (sha1->hash));
531  }
532  }
533  else
534  {
535  s = format (s, "\n");
536  }
537  }
538 
539  return s;
540 }
541 
542 typedef struct
543 {
546 
547 static void
549 {
550  bfd_main_t *bm = &bfd_main;
551  u32 bs_idx = a->bs_idx;
552  u32 valid_bs = 0;
553  bfd_session_t session_data;
554 
555  bfd_lock (bm);
556  if (!pool_is_free_index (bm->sessions, bs_idx))
557  {
558  bfd_session_t *bs = pool_elt_at_index (bm->sessions, bs_idx);
559  clib_memcpy (&session_data, bs, sizeof (bfd_session_t));
560  valid_bs = 1;
561  }
562  else
563  {
564  BFD_DBG ("Ignoring event RPC for non-existent session index %u",
565  bs_idx);
566  }
567  bfd_unlock (bm);
568 
569  if (valid_bs)
570  bfd_event (bm, &session_data);
571 }
572 
573 static void
575 {
576  const u32 data_size = sizeof (bfd_rpc_event_t);
577  u8 data[data_size];
578  bfd_rpc_event_t *event = (bfd_rpc_event_t *) data;
579 
580  event->bs_idx = bs_idx;
582 }
583 
584 typedef struct
585 {
588 
589 static void
591 {
592  bfd_main_t *bm = &bfd_main;
593  u32 bs_idx = a->bs_idx;
594  bfd_lock (bm);
595  if (!pool_is_free_index (bm->sessions, bs_idx))
596  {
597  bfd_session_t *bs = pool_elt_at_index (bm->sessions, bs_idx);
598  bfd_notify_listeners (bm, BFD_LISTEN_EVENT_UPDATE, bs);
599  }
600  else
601  {
602  BFD_DBG ("Ignoring notify RPC for non-existent session index %u",
603  bs_idx);
604  }
605  bfd_unlock (bm);
606 }
607 
608 static void
610 {
611  const u32 data_size = sizeof (bfd_rpc_notify_listeners_t);
612  u8 data[data_size];
614  notify->bs_idx = bs_idx;
616 }
617 
618 static void
620  int handling_wakeup)
621 {
622  BFD_DBG ("\nState changed: %U", format_bfd_session, bs);
623 
624  if (vlib_get_thread_index () == 0)
625  {
626  bfd_event (bm, bs);
627  }
628  else
629  {
630  /* without RPC - a REGRESSION: BFD event are not propagated */
631  bfd_event_rpc (bs->bs_idx);
632  }
633 
634  switch (bs->local_state)
635  {
636  case BFD_STATE_admin_down:
637  bs->echo = 0;
639  clib_max
644  bfd_set_timer (bm, bs, now, handling_wakeup);
645  break;
646  case BFD_STATE_down:
647  bs->echo = 0;
649  clib_max
654  bfd_set_timer (bm, bs, now, handling_wakeup);
655  break;
656  case BFD_STATE_init:
657  bs->echo = 0;
660  bfd_set_timer (bm, bs, now, handling_wakeup);
661  break;
662  case BFD_STATE_up:
665  if (BFD_POLL_NOT_NEEDED == bs->poll_state)
666  {
669  }
670  bfd_set_timer (bm, bs, now, handling_wakeup);
671  break;
672  }
673  if (vlib_get_thread_index () == 0)
674  {
675  bfd_notify_listeners (bm, BFD_LISTEN_EVENT_UPDATE, bs);
676  }
677  else
678  {
679  /* without RPC - a REGRESSION: state changes are not propagated */
681  }
682 }
683 
684 static void
686  bfd_main_t * bm, bfd_session_t * bs, u64 now)
687 {
688  /*
689  * if remote demand mode is set and we need to do a poll, set the next
690  * timeout so that the session wakes up immediately
691  */
692  if (bs->remote_demand && BFD_POLL_NEEDED == bs->poll_state &&
694  {
695  bs->tx_timeout_clocks = now;
696  }
697  bfd_recalc_detection_time (bm, bs);
698  bfd_set_timer (bm, bs, now, 0);
699 }
700 
701 static void
703 {
704  switch (bs->transport)
705  {
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 */ );
709  break;
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 */ );
713  break;
714  }
715 }
716 
717 static int
719 {
720  switch (bs->transport)
721  {
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);
725  break;
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);
729  break;
730  }
731  return 0;
732 }
733 
734 static int
736 {
737  switch (bs->transport)
738  {
739  case BFD_TRANSPORT_UDP4:
740  BFD_DBG ("Transport bfd echo via udp4, bs_idx=%u", bs->bs_idx);
741  return bfd_add_udp4_transport (vm, bi, bs, 1 /* is_echo */ );
742  break;
743  case BFD_TRANSPORT_UDP6:
744  BFD_DBG ("Transport bfd echo via udp6, bs_idx=%u", bs->bs_idx);
745  return bfd_add_udp6_transport (vm, bi, bs, 1 /* is_echo */ );
746  break;
747  }
748  return 0;
749 }
750 
751 static int
753 {
754  switch (bs->transport)
755  {
756  case BFD_TRANSPORT_UDP4:
757  BFD_DBG ("Transport bfd echo via udp4, bs_idx=%u", bs->bs_idx);
758  return bfd_transport_udp4 (vm, bi, bs);
759  break;
760  case BFD_TRANSPORT_UDP6:
761  BFD_DBG ("Transport bfd echo via udp6, bs_idx=%u", bs->bs_idx);
762  return bfd_transport_udp6 (vm, bi, bs);
763  break;
764  }
765  return 0;
766 }
767 
768 #if WITH_LIBSSL > 0
769 static void
770 bfd_add_sha1_auth_section (vlib_buffer_t * b, bfd_session_t * bs)
771 {
772  bfd_pkt_with_sha1_auth_t *pkt = vlib_buffer_get_current (b);
773  bfd_auth_sha1_t *auth = &pkt->sha1_auth;
774  b->current_length += sizeof (*auth);
775  pkt->pkt.head.length += sizeof (*auth);
776  bfd_pkt_set_auth_present (&pkt->pkt);
777  clib_memset (auth, 0, sizeof (*auth));
778  auth->type_len.type = bs->auth.curr_key->auth_type;
779  /*
780  * only meticulous authentication types require incrementing seq number
781  * for every message, but doing so doesn't violate the RFC
782  */
783  ++bs->auth.local_seq_number;
784  auth->type_len.len = sizeof (bfd_auth_sha1_t);
785  auth->key_id = bs->auth.curr_bfd_key_id;
786  auth->seq_num = clib_host_to_net_u32 (bs->auth.local_seq_number);
787  /*
788  * first copy the password into the packet, then calculate the hash
789  * and finally replace the password with the calculated hash
790  */
791  clib_memcpy (auth->hash, bs->auth.curr_key->key,
792  sizeof (bs->auth.curr_key->key));
793  unsigned char hash[sizeof (auth->hash)];
794  SHA1 ((unsigned char *) pkt, sizeof (*pkt), hash);
795  BFD_DBG ("hashing: %U", format_hex_bytes, pkt, sizeof (*pkt));
796  clib_memcpy (auth->hash, hash, sizeof (hash));
797 }
798 #endif
799 
800 static void
802 {
803  bfd_main_t *bm = &bfd_main;
804  if (bs->auth.curr_key)
805  {
806  const bfd_auth_type_e auth_type = bs->auth.curr_key->auth_type;
807  switch (auth_type)
808  {
809  case BFD_AUTH_TYPE_reserved:
810  /* fallthrough */
811  case BFD_AUTH_TYPE_simple_password:
812  /* fallthrough */
813  case BFD_AUTH_TYPE_keyed_md5:
814  /* fallthrough */
815  case BFD_AUTH_TYPE_meticulous_keyed_md5:
817  "internal error, unexpected BFD auth type '%d'",
818  auth_type);
819  break;
820 #if WITH_LIBSSL > 0
821  case BFD_AUTH_TYPE_keyed_sha1:
822  /* fallthrough */
823  case BFD_AUTH_TYPE_meticulous_keyed_sha1:
824  bfd_add_sha1_auth_section (b, bs);
825  break;
826 #else
827  case BFD_AUTH_TYPE_keyed_sha1:
828  /* fallthrough */
829  case BFD_AUTH_TYPE_meticulous_keyed_sha1:
831  "internal error, unexpected BFD auth type '%d'",
832  auth_type);
833  break;
834 #endif
835  }
836  }
837 }
838 
839 static int
841 {
842  if (BFD_STATE_up == bs->local_state && BFD_STATE_up == bs->remote_state &&
843  bs->remote_min_echo_rx_usec > 0)
844  {
845  switch (bs->transport)
846  {
847  case BFD_TRANSPORT_UDP4:
848  return bfd_udp_is_echo_available (BFD_TRANSPORT_UDP4);
849  case BFD_TRANSPORT_UDP6:
850  return bfd_udp_is_echo_available (BFD_TRANSPORT_UDP6);
851  }
852  }
853  return 0;
854 }
855 
856 static void
858  vlib_buffer_t * b)
859 {
860  bfd_pkt_t *pkt = vlib_buffer_get_current (b);
861  u32 bfd_length = 0;
862  bfd_length = sizeof (bfd_pkt_t);
863  clib_memset (pkt, 0, sizeof (*pkt));
864  bfd_pkt_set_version (pkt, 1);
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);
872  if (bs->echo)
873  {
874  pkt->req_min_rx =
875  clib_host_to_net_u32 (bfd_clocks_to_usec
877  }
878  else
879  {
880  pkt->req_min_rx =
881  clib_host_to_net_u32 (bs->config_required_min_rx_usec);
882  }
883  pkt->req_min_echo_rx = clib_host_to_net_u32 (1);
884  b->current_length = bfd_length;
885 }
886 
887 static void
889  bfd_main_t * bm, bfd_session_t * bs, u64 now)
890 {
891  if (!bfd_is_echo_possible (bs))
892  {
893  BFD_DBG ("\nSwitching off echo function: %U", format_bfd_session, bs);
894  bs->echo = 0;
895  return;
896  }
897  /* sometimes the wheel expires an event a bit sooner than requested,
898  account
899  for that here */
900  if (now + bm->wheel_inaccuracy >= bs->echo_tx_timeout_clocks)
901  {
902  BFD_DBG ("\nSending echo packet: %U", format_bfd_session, bs);
903  u32 bi;
904  if (vlib_buffer_alloc (vm, &bi, 1) != 1)
905  {
906  vlib_log_crit (bm->log_class, "buffer allocation failure");
907  return;
908  }
909  vlib_buffer_t *b = vlib_get_buffer (vm, bi);
910  ASSERT (b->current_data == 0);
912  bfd_echo_pkt_t *pkt = vlib_buffer_get_current (b);
913  clib_memset (pkt, 0, sizeof (*pkt));
914  pkt->discriminator = bs->local_discr;
915  pkt->expire_time_clocks =
917  pkt->checksum =
918  bfd_calc_echo_checksum (bs->local_discr, pkt->expire_time_clocks,
919  bs->echo_secret);
920  b->current_length = sizeof (*pkt);
921  if (!bfd_echo_add_transport_layer (vm, bi, bs))
922  {
923  BFD_ERR ("cannot send echo packet out, turning echo off");
924  bs->echo = 0;
925  vlib_buffer_free_one (vm, bi);
926  return;
927  }
928  if (!bfd_transport_echo (vm, bi, bs))
929  {
930  BFD_ERR ("cannot send echo packet out, turning echo off");
931  bs->echo = 0;
932  vlib_buffer_free_one (vm, bi);
933  return;
934  }
935  bs->echo_last_tx_clocks = now;
936  bfd_calc_next_echo_tx (bm, bs, now);
937  }
938  else
939  {
940  BFD_DBG
941  ("No need to send echo packet now, now is %lu, tx_timeout is %lu",
942  now, bs->echo_tx_timeout_clocks);
943  }
944 }
945 
946 static void
948  bfd_main_t * bm, bfd_session_t * bs, u64 now)
949 {
950  if (!bs->remote_min_rx_usec && BFD_POLL_NOT_NEEDED == bs->poll_state)
951  {
952  BFD_DBG ("Remote min rx interval is zero, not sending periodic control "
953  "frame");
954  return;
955  }
956  if (BFD_POLL_NOT_NEEDED == bs->poll_state && bs->remote_demand &&
957  BFD_STATE_up == bs->local_state && BFD_STATE_up == bs->remote_state)
958  {
959  /*
960  * A system MUST NOT periodically transmit BFD Control packets if Demand
961  * mode is active on the remote system (bfd.RemoteDemandMode is 1,
962  * bfd.SessionState is Up, and bfd.RemoteSessionState is Up) and a Poll
963  * Sequence is not being transmitted.
964  */
965  BFD_DBG ("Remote demand is set, not sending periodic control frame");
966  return;
967  }
968  /*
969  * sometimes the wheel expires an event a bit sooner than requested, account
970  * for that here
971  */
972  if (now + bm->wheel_inaccuracy >= bs->tx_timeout_clocks)
973  {
974  BFD_DBG ("\nSending periodic control frame: %U", format_bfd_session,
975  bs);
976  u32 bi;
977  if (vlib_buffer_alloc (vm, &bi, 1) != 1)
978  {
979  vlib_log_crit (bm->log_class, "buffer allocation failure");
980  return;
981  }
982  vlib_buffer_t *b = vlib_get_buffer (vm, bi);
983  ASSERT (b->current_data == 0);
985  bfd_init_control_frame (bm, bs, b);
986  switch (bs->poll_state)
987  {
988  case BFD_POLL_NEEDED:
989  if (now < bs->poll_state_start_or_timeout_clocks)
990  {
991  BFD_DBG ("Cannot start a poll sequence yet, need to wait "
992  "for " BFD_CLK_FMT,
993  BFD_CLK_PRN (bs->poll_state_start_or_timeout_clocks -
994  now));
995  break;
996  }
998  bfd_set_poll_state (bs, BFD_POLL_IN_PROGRESS);
999  /* fallthrough */
1000  case BFD_POLL_IN_PROGRESS:
1001  case BFD_POLL_IN_PROGRESS_AND_QUEUED:
1003  BFD_DBG ("Setting poll bit in packet, bs_idx=%u", bs->bs_idx);
1004  break;
1005  case BFD_POLL_NOT_NEEDED:
1006  /* fallthrough */
1007  break;
1008  }
1009  bfd_add_auth_section (b, bs);
1010  bfd_add_transport_layer (vm, bi, bs);
1011  if (!bfd_transport_control_frame (vm, bi, bs))
1012  {
1013  vlib_buffer_free_one (vm, bi);
1014  }
1015  bs->last_tx_clocks = now;
1016  bfd_calc_next_tx (bm, bs, now);
1017  }
1018  else
1019  {
1020  BFD_DBG
1021  ("No need to send control frame now, now is %lu, tx_timeout is %lu",
1022  now, bs->tx_timeout_clocks);
1023  }
1024 }
1025 
1026 void
1028  bfd_main_t * bm, bfd_session_t * bs,
1029  int is_local)
1030 {
1031  BFD_DBG ("Send final control frame for bs_idx=%lu", bs->bs_idx);
1032  bfd_init_control_frame (bm, bs, b);
1034  bfd_add_auth_section (b, bs);
1035  u32 bi = vlib_get_buffer_index (vm, b);
1036  bfd_add_transport_layer (vm, bi, bs);
1038  /*
1039  * RFC allows to include changes in final frame, so if there were any
1040  * pending, we already did that, thus we can clear any pending poll needs
1041  */
1042  bfd_set_poll_state (bs, BFD_POLL_NOT_NEEDED);
1043 }
1044 
1045 static void
1047  int handling_wakeup)
1048 {
1049  /*
1050  * sometimes the wheel expires an event a bit sooner than requested, account
1051  * for that here
1052  */
1053  if (bs->last_rx_clocks + bs->detection_time_clocks <=
1054  now + bm->wheel_inaccuracy)
1055  {
1056  BFD_DBG ("Rx timeout, session goes down");
1057  /*
1058  * RFC 5880 6.8.1. State Variables
1059 
1060  * bfd.RemoteDiscr
1061 
1062  * The remote discriminator for this BFD session. This is the
1063  * discriminator chosen by the remote system, and is totally opaque
1064  * to the local system. This MUST be initialized to zero. If a
1065  * period of a Detection Time passes without the receipt of a valid,
1066  * authenticated BFD packet from the remote system, this variable
1067  * MUST be set to zero.
1068  */
1069  bs->remote_discr = 0;
1070  bfd_set_diag (bs, BFD_DIAG_CODE_det_time_exp);
1071  bfd_set_state (bm, bs, BFD_STATE_down, handling_wakeup);
1072  /*
1073  * If the remote system does not receive any
1074  * BFD Control packets for a Detection Time, it SHOULD reset
1075  * bfd.RemoteMinRxInterval to its initial value of 1 (per section 6.8.1,
1076  * since it is no longer required to maintain previous session state)
1077  * and then can transmit at its own rate.
1078  */
1079  bfd_set_remote_required_min_rx (bm, bs, now, 1);
1080  }
1081  else if (bs->echo &&
1082  bs->echo_last_rx_clocks +
1084  now + bm->wheel_inaccuracy)
1085  {
1086  BFD_DBG ("Echo rx timeout, session goes down");
1087  bfd_set_diag (bs, BFD_DIAG_CODE_echo_failed);
1088  bfd_set_state (bm, bs, BFD_STATE_down, handling_wakeup);
1089  }
1090 }
1091 
1092 void
1094  bfd_session_t * bs, u64 now)
1095 {
1096  BFD_DBG ("Timeout for bs_idx=%lu", bs->bs_idx);
1097  switch (bs->local_state)
1098  {
1099  case BFD_STATE_admin_down:
1100  bfd_send_periodic (vm, rt, bm, bs, now);
1101  break;
1102  case BFD_STATE_down:
1103  bfd_send_periodic (vm, rt, bm, bs, now);
1104  break;
1105  case BFD_STATE_init:
1106  bfd_check_rx_timeout (bm, bs, now, 1);
1107  bfd_send_periodic (vm, rt, bm, bs, now);
1108  break;
1109  case BFD_STATE_up:
1110  bfd_check_rx_timeout (bm, bs, now, 1);
1111  if (BFD_POLL_NOT_NEEDED == bs->poll_state && !bs->echo &&
1112  bfd_is_echo_possible (bs))
1113  {
1114  /* switch on echo function as main detection method now */
1115  BFD_DBG ("Switching on echo function, bs_idx=%u", bs->bs_idx);
1116  bs->echo = 1;
1117  bs->echo_last_rx_clocks = now;
1118  bs->echo_tx_timeout_clocks = now;
1120  clib_max
1123  bfd_set_poll_state (bs, BFD_POLL_NEEDED);
1124  }
1125  bfd_send_periodic (vm, rt, bm, bs, now);
1126  if (bs->echo)
1127  {
1128  bfd_send_echo (vm, rt, bm, bs, now);
1129  }
1130  break;
1131  }
1132 }
1133 
1134 /*
1135  * bfd process node function
1136  */
1137 static uword
1139 {
1140  bfd_main_t *bm = &bfd_main;
1141  u32 *expired = 0;
1142  uword event_type, *event_data = 0;
1143 
1144  /* So we can send events to the bfd process */
1146 
1147  while (1)
1148  {
1149  u64 now = clib_cpu_time_now ();
1150  bfd_lock (bm);
1151  u64 next_expire = timing_wheel_next_expiring_elt_time (&bm->wheel);
1152  BFD_DBG ("timing_wheel_next_expiring_elt_time(%p) returns %lu",
1153  &bm->wheel, next_expire);
1155  (i64) next_expire >= 0 ? next_expire : ~0;
1156  bfd_unlock (bm);
1157  if ((i64) next_expire < 0)
1158  {
1159  BFD_DBG ("wait for event without timeout");
1160  (void) vlib_process_wait_for_event (vm);
1161  event_type = vlib_process_get_events (vm, &event_data);
1162  }
1163  else
1164  {
1165  f64 timeout = ((i64) next_expire - (i64) now) / bm->cpu_cps;
1166  BFD_DBG ("wait for event with timeout %.02f", timeout);
1167  if (timeout < 0)
1168  {
1169  BFD_DBG ("negative timeout, already expired, skipping wait");
1170  event_type = ~0;
1171  }
1172  else
1173  {
1174  (void) vlib_process_wait_for_event_or_clock (vm, timeout);
1175  event_type = vlib_process_get_events (vm, &event_data);
1176  }
1177  }
1178  now = clib_cpu_time_now ();
1179  uword *session_index;
1180  switch (event_type)
1181  {
1182  case ~0: /* no events => timeout */
1183  /* nothing to do here */
1184  break;
1185  case BFD_EVENT_RESCHEDULE:
1186  bfd_lock (bm);
1190  bfd_unlock (bm);
1191  /* nothing to do here - reschedule is done automatically after
1192  * each event or timeout */
1193  break;
1194  case BFD_EVENT_NEW_SESSION:
1195  vec_foreach (session_index, event_data)
1196  {
1197  bfd_lock (bm);
1198  if (!pool_is_free_index (bm->sessions, *session_index))
1199  {
1200  bfd_session_t *bs =
1201  pool_elt_at_index (bm->sessions, *session_index);
1202  bfd_send_periodic (vm, rt, bm, bs, now);
1203  bfd_set_timer (bm, bs, now, 1);
1204  }
1205  else
1206  {
1207  BFD_DBG ("Ignoring event for non-existent session index %u",
1208  (u32) * session_index);
1209  }
1210  bfd_unlock (bm);
1211  }
1212  break;
1214  vec_foreach (session_index, event_data)
1215  {
1216  bfd_lock (bm);
1217  if (!pool_is_free_index (bm->sessions, *session_index))
1218  {
1219  bfd_session_t *bs =
1220  pool_elt_at_index (bm->sessions, *session_index);
1221  bfd_on_config_change (vm, rt, bm, bs, now);
1222  }
1223  else
1224  {
1225  BFD_DBG ("Ignoring event for non-existent session index %u",
1226  (u32) * session_index);
1227  }
1228  bfd_unlock (bm);
1229  }
1230  break;
1231  default:
1232  vlib_log_err (bm->log_class, "BUG: event type 0x%wx", event_type);
1233  break;
1234  }
1235  BFD_DBG ("advancing wheel, now is %lu", now);
1236  BFD_DBG ("timing_wheel_advance (%p, %lu, %p, 0);", &bm->wheel, now,
1237  expired);
1238  bfd_lock (bm);
1239  expired = timing_wheel_advance (&bm->wheel, now, expired, 0);
1240  BFD_DBG ("Expired %d elements", vec_len (expired));
1241  u32 *p = NULL;
1242  vec_foreach (p, expired)
1243  {
1244  const u32 bs_idx = *p;
1245  if (!pool_is_free_index (bm->sessions, bs_idx))
1246  {
1247  bfd_session_t *bs = pool_elt_at_index (bm->sessions, bs_idx);
1248  bfd_on_timeout (vm, rt, bm, bs, now);
1249  bfd_set_timer (bm, bs, now, 1);
1250  }
1251  }
1252  bfd_unlock (bm);
1253  if (expired)
1254  {
1255  _vec_len (expired) = 0;
1256  }
1257  if (event_data)
1258  {
1259  _vec_len (event_data) = 0;
1260  }
1261  }
1262 
1263  return 0;
1264 }
1265 
1266 /*
1267  * bfd process node declaration
1268  */
1269 /* *INDENT-OFF* */
1271  .function = bfd_process,
1272  .type = VLIB_NODE_TYPE_PROCESS,
1273  .name = "bfd-process",
1274  .n_next_nodes = 0,
1275  .next_nodes = {},
1276 };
1277 /* *INDENT-ON* */
1278 
1279 static clib_error_t *
1281 {
1282  // bfd_main_t *bm = &bfd_main;
1283  // vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
1284  if (!(flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
1285  {
1286  /* TODO */
1287  }
1288  return 0;
1289 }
1290 
1292 
1293 static clib_error_t *
1295 {
1296  // bfd_main_t *bm = &bfd_main;
1297  if (flags & VNET_HW_INTERFACE_FLAG_LINK_UP)
1298  {
1299  /* TODO */
1300  }
1301  return 0;
1302 }
1303 
1305 
1306 void
1308 {
1309  bfd_main_t *bm = &bfd_main;
1310 
1311  vec_add1 (bm->listeners, fn);
1312 }
1313 
1314 /*
1315  * setup function
1316  */
1317 static clib_error_t *
1319 {
1321  u32 n_vlib_mains = tm->n_vlib_mains;
1322 #if BFD_DEBUG
1323  setbuf (stdout, NULL);
1324 #endif
1325  bfd_main_t *bm = &bfd_main;
1327  bm->vlib_main = vm;
1328  bm->vnet_main = vnet_get_main ();
1329  clib_memset (&bm->wheel, 0, sizeof (bm->wheel));
1331  BFD_DBG ("cps is %.2f", bm->cpu_cps);
1336  const u64 now = clib_cpu_time_now ();
1337  timing_wheel_init (&bm->wheel, now, bm->cpu_cps);
1339  bm->log_class = vlib_log_register_class ("bfd", 0);
1340  vlib_log_debug (bm->log_class, "initialized");
1341  bm->owner_thread_index = ~0;
1342  if (n_vlib_mains > 1)
1343  clib_spinlock_init (&bm->lock);
1344  return 0;
1345 }
1346 
1348 
1349 bfd_session_t *
1351 {
1352  bfd_session_t *result;
1353 
1354  bfd_lock (bm);
1355 
1356  pool_get (bm->sessions, result);
1357  clib_memset (result, 0, sizeof (*result));
1358  result->bs_idx = result - bm->sessions;
1359  result->transport = t;
1360  const unsigned limit = 1000;
1361  unsigned counter = 0;
1362  do
1363  {
1364  result->local_discr = random_u32 (&bm->random_seed);
1365  if (counter > limit)
1366  {
1367  vlib_log_crit (bm->log_class,
1368  "couldn't allocate unused session discriminator even "
1369  "after %u tries!", limit);
1370  pool_put (bm->sessions, result);
1371  bfd_unlock (bm);
1372  return NULL;
1373  }
1374  ++counter;
1375  }
1376  while (hash_get (bm->session_by_disc, result->local_discr));
1377  bfd_set_defaults (bm, result);
1378  hash_set (bm->session_by_disc, result->local_discr, result->bs_idx);
1379  bfd_unlock (bm);
1380  return result;
1381 }
1382 
1383 void
1385 {
1386  bfd_lock (bm);
1387 
1388  vlib_log_info (bm->log_class, "delete session: %U",
1390  bfd_notify_listeners (bm, BFD_LISTEN_EVENT_DELETE, bs);
1391  if (bs->auth.curr_key)
1392  {
1393  --bs->auth.curr_key->use_count;
1394  }
1395  if (bs->auth.next_key)
1396  {
1397  --bs->auth.next_key->use_count;
1398  }
1400  pool_put (bm->sessions, bs);
1401  bfd_unlock (bm);
1402 }
1403 
1404 bfd_session_t *
1406 {
1407  bfd_lock_check (bm);
1408  if (!pool_is_free_index (bm->sessions, bs_idx))
1409  {
1410  return pool_elt_at_index (bm->sessions, bs_idx);
1411  }
1412  return NULL;
1413 }
1414 
1415 bfd_session_t *
1417 {
1418  bfd_lock_check (bm);
1419  uword *p = hash_get (bfd_main.session_by_disc, disc);
1420  if (p)
1421  {
1422  return pool_elt_at_index (bfd_main.sessions, *p);
1423  }
1424  return NULL;
1425 }
1426 
1427 /**
1428  * @brief verify bfd packet - common checks
1429  *
1430  * @param pkt
1431  *
1432  * @return 1 if bfd packet is valid
1433  */
1434 int
1435 bfd_verify_pkt_common (const bfd_pkt_t * pkt)
1436 {
1437  if (1 != bfd_pkt_get_version (pkt))
1438  {
1439  BFD_ERR ("BFD verification failed - unexpected version: '%d'",
1440  bfd_pkt_get_version (pkt));
1441  return 0;
1442  }
1443  if (pkt->head.length < sizeof (bfd_pkt_t) ||
1444  (bfd_pkt_get_auth_present (pkt) &&
1445  pkt->head.length < sizeof (bfd_pkt_with_common_auth_t)))
1446  {
1447  BFD_ERR ("BFD verification failed - unexpected length: '%d' (auth "
1448  "present: %d)",
1449  pkt->head.length, bfd_pkt_get_auth_present (pkt));
1450  return 0;
1451  }
1452  if (!pkt->head.detect_mult)
1453  {
1454  BFD_ERR ("BFD verification failed - unexpected detect-mult: '%d'",
1455  pkt->head.detect_mult);
1456  return 0;
1457  }
1458  if (bfd_pkt_get_multipoint (pkt))
1459  {
1460  BFD_ERR ("BFD verification failed - unexpected multipoint: '%d'",
1461  bfd_pkt_get_multipoint (pkt));
1462  return 0;
1463  }
1464  if (!pkt->my_disc)
1465  {
1466  BFD_ERR ("BFD verification failed - unexpected my-disc: '%d'",
1467  pkt->my_disc);
1468  return 0;
1469  }
1470  if (!pkt->your_disc)
1471  {
1472  const u8 pkt_state = bfd_pkt_get_state (pkt);
1473  if (pkt_state != BFD_STATE_down && pkt_state != BFD_STATE_admin_down)
1474  {
1475  BFD_ERR ("BFD verification failed - unexpected state: '%s' "
1476  "(your-disc is zero)", bfd_state_string (pkt_state));
1477  return 0;
1478  }
1479  }
1480  return 1;
1481 }
1482 
1483 static void
1485 {
1486  BFD_DBG ("Switching authentication key from %U to %U for bs_idx=%u",
1488  bs->auth.next_key, bs->bs_idx);
1489  bs->auth.is_delayed = 0;
1490  if (bs->auth.curr_key)
1491  {
1492  --bs->auth.curr_key->use_count;
1493  }
1494  bs->auth.curr_key = bs->auth.next_key;
1495  bs->auth.next_key = NULL;
1497 }
1498 
1499 static int
1501 {
1502  if (BFD_AUTH_TYPE_meticulous_keyed_md5 == auth_type ||
1503  BFD_AUTH_TYPE_meticulous_keyed_sha1 == auth_type)
1504  {
1505  return 1;
1506  }
1507  return 0;
1508 }
1509 
1510 static int
1512  u32 received_seq_num, int is_meticulous)
1513 {
1514  /*
1515  * RFC 5880 6.8.1:
1516  *
1517  * This variable MUST be set to zero after no packets have been
1518  * received on this session for at least twice the Detection Time.
1519  */
1520  u64 now = clib_cpu_time_now ();
1521  if (now - bs->last_rx_clocks > bs->detection_time_clocks * 2)
1522  {
1523  BFD_DBG ("BFD peer unresponsive for %lu clocks, which is > 2 * "
1524  "detection_time=%u clocks, resetting remote_seq_number_known "
1525  "flag",
1526  now - bs->last_rx_clocks, bs->detection_time_clocks * 2);
1527  bs->auth.remote_seq_number_known = 0;
1528  }
1529  if (bs->auth.remote_seq_number_known)
1530  {
1531  /* remote sequence number is known, verify its validity */
1532  const u32 max_u32 = 0xffffffff;
1533  /* the calculation might wrap, account for the special case... */
1534  if (bs->auth.remote_seq_number > max_u32 - 3 * bs->local_detect_mult)
1535  {
1536  /*
1537  * special case
1538  *
1539  * x y z
1540  * |----------+----------------------------+-----------|
1541  * 0 ^ ^ 0xffffffff
1542  * | remote_seq_num------+
1543  * |
1544  * +-----(remote_seq_num + 3*detect_mult) % * 0xffffffff
1545  *
1546  * x + y + z = 0xffffffff
1547  * x + z = 3 * detect_mult
1548  */
1549  const u32 z = max_u32 - bs->auth.remote_seq_number;
1550  const u32 x = 3 * bs->local_detect_mult - z;
1551  if (received_seq_num > x &&
1552  received_seq_num < bs->auth.remote_seq_number + is_meticulous)
1553  {
1554  BFD_ERR
1555  ("Recvd sequence number=%u out of ranges <0, %u>, <%u, %u>",
1556  received_seq_num, x,
1557  bs->auth.remote_seq_number + is_meticulous, max_u32);
1558  return 0;
1559  }
1560  }
1561  else
1562  {
1563  /* regular case */
1564  const u32 min = bs->auth.remote_seq_number + is_meticulous;
1565  const u32 max =
1567  if (received_seq_num < min || received_seq_num > max)
1568  {
1569  BFD_ERR ("Recvd sequence number=%u out of range <%u, %u>",
1570  received_seq_num, min, max);
1571  return 0;
1572  }
1573  }
1574  }
1575  return 1;
1576 }
1577 
1578 static int
1579 bfd_verify_pkt_auth_key_sha1 (const bfd_pkt_t * pkt, u32 pkt_size,
1580  bfd_session_t * bs, u8 bfd_key_id,
1581  bfd_auth_key_t * auth_key)
1582 {
1583  ASSERT (auth_key->auth_type == BFD_AUTH_TYPE_keyed_sha1 ||
1584  auth_key->auth_type == BFD_AUTH_TYPE_meticulous_keyed_sha1);
1585 
1586  u8 result[SHA_DIGEST_LENGTH];
1587  bfd_pkt_with_common_auth_t *with_common = (void *) pkt;
1588  if (pkt_size < sizeof (*with_common))
1589  {
1590  BFD_ERR ("Packet size too small to hold authentication common header");
1591  return 0;
1592  }
1593  if (with_common->common_auth.type != auth_key->auth_type)
1594  {
1595  BFD_ERR ("BFD auth type mismatch, packet auth=%d:%s doesn't match "
1596  "in-use auth=%d:%s",
1597  with_common->common_auth.type,
1598  bfd_auth_type_str (with_common->common_auth.type),
1599  auth_key->auth_type, bfd_auth_type_str (auth_key->auth_type));
1600  return 0;
1601  }
1602  bfd_pkt_with_sha1_auth_t *with_sha1 = (void *) pkt;
1603  if (pkt_size < sizeof (*with_sha1) ||
1604  with_sha1->sha1_auth.type_len.len < sizeof (with_sha1->sha1_auth))
1605  {
1606  BFD_ERR
1607  ("BFD size mismatch, payload size=%u, expected=%u, auth_len=%u, "
1608  "expected=%u", pkt_size, sizeof (*with_sha1),
1609  with_sha1->sha1_auth.type_len.len, sizeof (with_sha1->sha1_auth));
1610  return 0;
1611  }
1612  if (with_sha1->sha1_auth.key_id != bfd_key_id)
1613  {
1614  BFD_ERR
1615  ("BFD key ID mismatch, packet key ID=%u doesn't match key ID=%u%s",
1616  with_sha1->sha1_auth.key_id, bfd_key_id,
1617  bs->
1618  auth.is_delayed ? " (but a delayed auth change is scheduled)" : "");
1619  return 0;
1620  }
1621  SHA_CTX ctx;
1622  if (!SHA1_Init (&ctx))
1623  {
1624  BFD_ERR ("SHA1_Init failed");
1625  return 0;
1626  }
1627  /* ignore last 20 bytes - use the actual key data instead pkt data */
1628  if (!SHA1_Update (&ctx, with_sha1,
1629  sizeof (*with_sha1) - sizeof (with_sha1->sha1_auth.hash)))
1630  {
1631  BFD_ERR ("SHA1_Update failed");
1632  return 0;
1633  }
1634  if (!SHA1_Update (&ctx, auth_key->key, sizeof (auth_key->key)))
1635  {
1636  BFD_ERR ("SHA1_Update failed");
1637  return 0;
1638  }
1639  if (!SHA1_Final (result, &ctx))
1640  {
1641  BFD_ERR ("SHA1_Final failed");
1642  return 0;
1643  }
1644  if (0 == memcmp (result, with_sha1->sha1_auth.hash, SHA_DIGEST_LENGTH))
1645  {
1646  return 1;
1647  }
1648  BFD_ERR ("SHA1 hash: %U doesn't match the expected value: %U",
1649  format_hex_bytes, with_sha1->sha1_auth.hash, SHA_DIGEST_LENGTH,
1650  format_hex_bytes, result, SHA_DIGEST_LENGTH);
1651  return 0;
1652 }
1653 
1654 static int
1655 bfd_verify_pkt_auth_key (const bfd_pkt_t * pkt, u32 pkt_size,
1656  bfd_session_t * bs, u8 bfd_key_id,
1657  bfd_auth_key_t * auth_key)
1658 {
1659  bfd_main_t *bm = &bfd_main;
1660  switch (auth_key->auth_type)
1661  {
1662  case BFD_AUTH_TYPE_reserved:
1663  vlib_log_err (bm->log_class,
1664  "internal error, unexpected auth_type=%d:%s",
1665  auth_key->auth_type,
1666  bfd_auth_type_str (auth_key->auth_type));
1667  return 0;
1668  case BFD_AUTH_TYPE_simple_password:
1669  vlib_log_err (bm->log_class,
1670  "internal error, not implemented, unexpected auth_type=%d:%s",
1671  auth_key->auth_type,
1672  bfd_auth_type_str (auth_key->auth_type));
1673  return 0;
1674  case BFD_AUTH_TYPE_keyed_md5:
1675  /* fallthrough */
1676  case BFD_AUTH_TYPE_meticulous_keyed_md5:
1677  vlib_log_err
1678  (bm->log_class,
1679  "internal error, not implemented, unexpected auth_type=%d:%s",
1680  auth_key->auth_type, bfd_auth_type_str (auth_key->auth_type));
1681  return 0;
1682  case BFD_AUTH_TYPE_keyed_sha1:
1683  /* fallthrough */
1684  case BFD_AUTH_TYPE_meticulous_keyed_sha1:
1685 #if WITH_LIBSSL > 0
1686  do
1687  {
1688  const u32 seq_num = clib_net_to_host_u32 (((bfd_pkt_with_sha1_auth_t
1689  *) pkt)->
1690  sha1_auth.seq_num);
1691  return bfd_verify_pkt_auth_seq_num (bs, seq_num,
1693  (auth_key->auth_type))
1694  && bfd_verify_pkt_auth_key_sha1 (pkt, pkt_size, bs, bfd_key_id,
1695  auth_key);
1696  }
1697  while (0);
1698 #else
1699  vlib_log_err
1700  (bm->log_class,
1701  "internal error, attempt to use SHA1 without SSL support");
1702  return 0;
1703 #endif
1704  }
1705  return 0;
1706 }
1707 
1708 /**
1709  * @brief verify bfd packet - authentication
1710  *
1711  * @param pkt
1712  *
1713  * @return 1 if bfd packet is valid
1714  */
1715 int
1716 bfd_verify_pkt_auth (const bfd_pkt_t * pkt, u16 pkt_size, bfd_session_t * bs)
1717 {
1718  if (bfd_pkt_get_auth_present (pkt))
1719  {
1720  /* authentication present in packet */
1721  if (!bs->auth.curr_key)
1722  {
1723  /* currently not using authentication - can we turn it on? */
1724  if (bs->auth.is_delayed && bs->auth.next_key)
1725  {
1726  /* yes, switch is scheduled - make sure the auth is valid */
1727  if (bfd_verify_pkt_auth_key (pkt, pkt_size, bs,
1728  bs->auth.next_bfd_key_id,
1729  bs->auth.next_key))
1730  {
1731  /* auth matches next key, do the switch, packet is valid */
1733  return 1;
1734  }
1735  }
1736  }
1737  else
1738  {
1739  /* yes, using authentication, verify the key */
1740  if (bfd_verify_pkt_auth_key (pkt, pkt_size, bs,
1741  bs->auth.curr_bfd_key_id,
1742  bs->auth.curr_key))
1743  {
1744  /* verification passed, packet is valid */
1745  return 1;
1746  }
1747  else
1748  {
1749  /* verification failed - but maybe we need to switch key */
1750  if (bs->auth.is_delayed && bs->auth.next_key)
1751  {
1752  /* delayed switch present, verify if that key works */
1753  if (bfd_verify_pkt_auth_key (pkt, pkt_size, bs,
1754  bs->auth.next_bfd_key_id,
1755  bs->auth.next_key))
1756  {
1757  /* auth matches next key, switch key, packet is valid */
1759  return 1;
1760  }
1761  }
1762  }
1763  }
1764  }
1765  else
1766  {
1767  /* authentication in packet not present */
1768  if (pkt_size > sizeof (*pkt))
1769  {
1770  BFD_ERR ("BFD verification failed - unexpected packet size '%d' "
1771  "(auth not present)", pkt_size);
1772  return 0;
1773  }
1774  if (bs->auth.curr_key)
1775  {
1776  /* currently authenticating - could we turn it off? */
1777  if (bs->auth.is_delayed && !bs->auth.next_key)
1778  {
1779  /* yes, delayed switch to NULL key is scheduled */
1781  return 1;
1782  }
1783  }
1784  else
1785  {
1786  /* no auth in packet, no auth in use - packet is valid */
1787  return 1;
1788  }
1789  }
1790  return 0;
1791 }
1792 
1793 void
1794 bfd_consume_pkt (bfd_main_t * bm, const bfd_pkt_t * pkt, u32 bs_idx)
1795 {
1796  bfd_lock_check (bm);
1797 
1798  bfd_session_t *bs = bfd_find_session_by_idx (bm, bs_idx);
1799  if (!bs || (pkt->your_disc && pkt->your_disc != bs->local_discr))
1800  {
1801  return;
1802  }
1803  BFD_DBG ("Scanning bfd packet, bs_idx=%d", bs->bs_idx);
1804  bs->remote_discr = pkt->my_disc;
1805  bs->remote_state = bfd_pkt_get_state (pkt);
1806  bs->remote_demand = bfd_pkt_get_demand (pkt);
1807  bs->remote_diag = bfd_pkt_get_diag_code (pkt);
1808  u64 now = clib_cpu_time_now ();
1809  bs->last_rx_clocks = now;
1810  if (bfd_pkt_get_auth_present (pkt))
1811  {
1812  bfd_auth_type_e auth_type =
1813  ((bfd_pkt_with_common_auth_t *) (pkt))->common_auth.type;
1814  switch (auth_type)
1815  {
1816  case BFD_AUTH_TYPE_reserved:
1817  /* fallthrough */
1818  case BFD_AUTH_TYPE_simple_password:
1819  /* fallthrough */
1820  case BFD_AUTH_TYPE_keyed_md5:
1821  /* fallthrough */
1822  case BFD_AUTH_TYPE_meticulous_keyed_md5:
1823  vlib_log_crit (bm->log_class,
1824  "internal error, unexpected auth_type=%d:%s",
1825  auth_type, bfd_auth_type_str (auth_type));
1826  break;
1827  case BFD_AUTH_TYPE_keyed_sha1:
1828  /* fallthrough */
1829  case BFD_AUTH_TYPE_meticulous_keyed_sha1:
1830  do
1831  {
1832  bfd_pkt_with_sha1_auth_t *with_sha1 =
1833  (bfd_pkt_with_sha1_auth_t *) pkt;
1834  bs->auth.remote_seq_number =
1835  clib_net_to_host_u32 (with_sha1->sha1_auth.seq_num);
1836  bs->auth.remote_seq_number_known = 1;
1837  BFD_DBG ("Received sequence number %u",
1838  bs->auth.remote_seq_number);
1839  }
1840  while (0);
1841  }
1842  }
1844  bfd_usec_to_clocks (bm, clib_net_to_host_u32 (pkt->des_min_tx));
1845  bs->remote_detect_mult = pkt->head.detect_mult;
1846  bfd_set_remote_required_min_rx (bm, bs, now,
1847  clib_net_to_host_u32 (pkt->req_min_rx));
1849  clib_net_to_host_u32
1850  (pkt->req_min_echo_rx));
1851  if (bfd_pkt_get_final (pkt))
1852  {
1853  if (BFD_POLL_IN_PROGRESS == bs->poll_state)
1854  {
1855  BFD_DBG ("Poll sequence terminated, bs_idx=%u", bs->bs_idx);
1856  bfd_set_poll_state (bs, BFD_POLL_NOT_NEEDED);
1857  if (BFD_STATE_up == bs->local_state)
1858  {
1860  clib_max (bs->echo *
1863  }
1864  }
1865  else if (BFD_POLL_IN_PROGRESS_AND_QUEUED == bs->poll_state)
1866  {
1867  /*
1868  * next poll sequence must be delayed by at least the round trip
1869  * time, so calculate that here
1870  */
1871  BFD_DBG ("Next poll sequence can commence in " BFD_CLK_FMT,
1872  BFD_CLK_PRN (now -
1875  now + (now - bs->poll_state_start_or_timeout_clocks);
1876  BFD_DBG
1877  ("Poll sequence terminated, but another is needed, bs_idx=%u",
1878  bs->bs_idx);
1879  bfd_set_poll_state (bs, BFD_POLL_NEEDED);
1880  }
1881  }
1882  bfd_calc_next_tx (bm, bs, now);
1883  bfd_set_timer (bm, bs, now, 0);
1884  if (BFD_STATE_admin_down == bs->local_state)
1885  {
1886  BFD_DBG ("Session is admin-down, ignoring packet, bs_idx=%u",
1887  bs->bs_idx);
1888  return;
1889  }
1890  if (BFD_STATE_admin_down == bs->remote_state)
1891  {
1892  bfd_set_diag (bs, BFD_DIAG_CODE_neighbor_sig_down);
1893  bfd_set_state (bm, bs, BFD_STATE_down, 0);
1894  }
1895  else if (BFD_STATE_down == bs->local_state)
1896  {
1897  if (BFD_STATE_down == bs->remote_state)
1898  {
1899  bfd_set_diag (bs, BFD_DIAG_CODE_no_diag);
1900  bfd_set_state (bm, bs, BFD_STATE_init, 0);
1901  }
1902  else if (BFD_STATE_init == bs->remote_state)
1903  {
1904  bfd_set_diag (bs, BFD_DIAG_CODE_no_diag);
1905  bfd_set_state (bm, bs, BFD_STATE_up, 0);
1906  }
1907  }
1908  else if (BFD_STATE_init == bs->local_state)
1909  {
1910  if (BFD_STATE_up == bs->remote_state ||
1911  BFD_STATE_init == bs->remote_state)
1912  {
1913  bfd_set_diag (bs, BFD_DIAG_CODE_no_diag);
1914  bfd_set_state (bm, bs, BFD_STATE_up, 0);
1915  }
1916  }
1917  else /* BFD_STATE_up == bs->local_state */
1918  {
1919  if (BFD_STATE_down == bs->remote_state)
1920  {
1921  bfd_set_diag (bs, BFD_DIAG_CODE_neighbor_sig_down);
1922  bfd_set_state (bm, bs, BFD_STATE_down, 0);
1923  }
1924  }
1925 }
1926 
1927 int
1929 {
1930  bfd_echo_pkt_t *pkt = NULL;
1931  if (b->current_length != sizeof (*pkt))
1932  {
1933  return 0;
1934  }
1935  pkt = vlib_buffer_get_current (b);
1936  bfd_session_t *bs = bfd_find_session_by_disc (bm, pkt->discriminator);
1937  if (!bs)
1938  {
1939  return 0;
1940  }
1941  BFD_DBG ("Scanning bfd echo packet, bs_idx=%d", bs->bs_idx);
1942  u64 checksum =
1943  bfd_calc_echo_checksum (bs->local_discr, pkt->expire_time_clocks,
1944  bs->echo_secret);
1945  if (checksum != pkt->checksum)
1946  {
1947  BFD_DBG ("Invalid echo packet, checksum mismatch");
1948  return 1;
1949  }
1950  u64 now = clib_cpu_time_now ();
1951  if (pkt->expire_time_clocks < now)
1952  {
1953  BFD_DBG ("Stale packet received, expire time %lu < now %lu",
1954  pkt->expire_time_clocks, now);
1955  }
1956  else
1957  {
1958  bs->echo_last_rx_clocks = now;
1959  }
1960  return 1;
1961 }
1962 
1963 u8 *
1964 format_bfd_session (u8 * s, va_list * args)
1965 {
1966  const bfd_session_t *bs = va_arg (*args, bfd_session_t *);
1967  u32 indent = format_get_indent (s) + vlib_log_get_indent ();
1968  s = format (s, "bs_idx=%u local-state=%s remote-state=%s\n"
1969  "%Ulocal-discriminator=%u remote-discriminator=%u\n"
1970  "%Ulocal-diag=%s echo-active=%s\n"
1971  "%Udesired-min-tx=%u required-min-rx=%u\n"
1972  "%Urequired-min-echo-rx=%u detect-mult=%u\n"
1973  "%Uremote-min-rx=%u remote-min-echo-rx=%u\n"
1974  "%Uremote-demand=%s poll-state=%s\n"
1975  "%Uauth: local-seq-num=%u remote-seq-num=%u\n"
1976  "%U is-delayed=%s\n"
1977  "%U curr-key=%U\n"
1978  "%U next-key=%U",
1979  bs->bs_idx, bfd_state_string (bs->local_state),
1981  bs->local_discr, bs->remote_discr, format_white_space, indent,
1983  (bs->echo ? "yes" : "no"), format_white_space, indent,
1985  format_white_space, indent, 1, bs->local_detect_mult,
1988  (bs->remote_demand ? "yes" : "no"),
1990  indent, bs->auth.local_seq_number, bs->auth.remote_seq_number,
1991  format_white_space, indent,
1992  (bs->auth.is_delayed ? "yes" : "no"), format_white_space,
1993  indent, format_bfd_auth_key, bs->auth.curr_key,
1995  bs->auth.next_key);
1996  return s;
1997 }
1998 
1999 u8 *
2000 format_bfd_session_brief (u8 * s, va_list * args)
2001 {
2002  const bfd_session_t *bs = va_arg (*args, bfd_session_t *);
2003  s =
2004  format (s, "bs_idx=%u local-state=%s remote-state=%s", bs->bs_idx,
2007  return s;
2008 }
2009 
2010 unsigned
2012 {
2013  if (auth_type == BFD_AUTH_TYPE_keyed_sha1 ||
2014  auth_type == BFD_AUTH_TYPE_meticulous_keyed_sha1)
2015  {
2016  return 1;
2017  }
2018  return 0;
2019 }
2020 
2023  u8 bfd_key_id, u8 is_delayed)
2024 {
2025  bfd_main_t *bm = &bfd_main;
2026  const uword *key_idx_p =
2027  hash_get (bm->auth_key_by_conf_key_id, conf_key_id);
2028  if (!key_idx_p)
2029  {
2030  vlib_log_err (bm->log_class,
2031  "authentication key with config ID %u doesn't exist)",
2032  conf_key_id);
2033  return VNET_API_ERROR_BFD_ENOENT;
2034  }
2035  const uword key_idx = *key_idx_p;
2036  bfd_auth_key_t *key = pool_elt_at_index (bm->auth_keys, key_idx);
2037  if (is_delayed)
2038  {
2039  if (bs->auth.next_key == key)
2040  {
2041  /* already using this key, no changes required */
2042  return 0;
2043  }
2044  bs->auth.next_key = key;
2045  bs->auth.next_bfd_key_id = bfd_key_id;
2046  bs->auth.is_delayed = 1;
2047  }
2048  else
2049  {
2050  if (bs->auth.curr_key == key)
2051  {
2052  /* already using this key, no changes required */
2053  return 0;
2054  }
2055  if (bs->auth.curr_key)
2056  {
2057  --bs->auth.curr_key->use_count;
2058  }
2059  bs->auth.curr_key = key;
2060  bs->auth.curr_bfd_key_id = bfd_key_id;
2061  bs->auth.is_delayed = 0;
2062  }
2063  ++key->use_count;
2064  BFD_DBG ("\nSession auth modified: %U", format_bfd_session, bs);
2065  vlib_log_info (bm->log_class, "session auth modified: %U",
2067  return 0;
2068 }
2069 
2072 {
2073  bfd_main_t *bm = &bfd_main;
2074 #if WITH_LIBSSL > 0
2075  if (!is_delayed)
2076  {
2077  /* not delayed - deactivate the current key right now */
2078  if (bs->auth.curr_key)
2079  {
2080  --bs->auth.curr_key->use_count;
2081  bs->auth.curr_key = NULL;
2082  }
2083  bs->auth.is_delayed = 0;
2084  }
2085  else
2086  {
2087  /* delayed - mark as so */
2088  bs->auth.is_delayed = 1;
2089  }
2090  /*
2091  * clear the next key unconditionally - either the auth change is not delayed
2092  * in which case the caller expects the session to not use authentication
2093  * from this point forward, or it is delayed, in which case the next_key
2094  * needs to be set to NULL to make it so in the future
2095  */
2096  if (bs->auth.next_key)
2097  {
2098  --bs->auth.next_key->use_count;
2099  bs->auth.next_key = NULL;
2100  }
2101  BFD_DBG ("\nSession auth modified: %U", format_bfd_session, bs);
2102  vlib_log_info (bm->log_class, "session auth modified: %U",
2104  return 0;
2105 #else
2106  vlib_log_err (bm->log_class,
2107  "SSL missing, cannot deactivate BFD authentication");
2108  return VNET_API_ERROR_BFD_NOTSUPP;
2109 #endif
2110 }
2111 
2114  u32 desired_min_tx_usec,
2115  u32 required_min_rx_usec, u8 detect_mult)
2116 {
2117  if (bs->local_detect_mult != detect_mult ||
2118  bs->config_desired_min_tx_usec != desired_min_tx_usec ||
2119  bs->config_required_min_rx_usec != required_min_rx_usec)
2120  {
2121  BFD_DBG ("\nChanging session params: %U", format_bfd_session, bs);
2122  switch (bs->poll_state)
2123  {
2124  case BFD_POLL_NOT_NEEDED:
2125  if (BFD_STATE_up == bs->local_state ||
2126  BFD_STATE_init == bs->local_state)
2127  {
2128  /* poll sequence is not needed for detect multiplier change */
2129  if (bs->config_desired_min_tx_usec != desired_min_tx_usec ||
2130  bs->config_required_min_rx_usec != required_min_rx_usec)
2131  {
2132  bfd_set_poll_state (bs, BFD_POLL_NEEDED);
2133  }
2134  }
2135  break;
2136  case BFD_POLL_NEEDED:
2137  case BFD_POLL_IN_PROGRESS_AND_QUEUED:
2138  /*
2139  * nothing to do - will be handled in the future poll which is
2140  * already scheduled for execution
2141  */
2142  break;
2143  case BFD_POLL_IN_PROGRESS:
2144  /* poll sequence is not needed for detect multiplier change */
2145  if (bs->config_desired_min_tx_usec != desired_min_tx_usec ||
2146  bs->config_required_min_rx_usec != required_min_rx_usec)
2147  {
2148  BFD_DBG ("Poll in progress, queueing extra poll, bs_idx=%u",
2149  bs->bs_idx);
2150  bfd_set_poll_state (bs, BFD_POLL_IN_PROGRESS_AND_QUEUED);
2151  }
2152  }
2153 
2154  bs->local_detect_mult = detect_mult;
2155  bs->config_desired_min_tx_usec = desired_min_tx_usec;
2157  bfd_usec_to_clocks (bm, desired_min_tx_usec);
2158  bs->config_required_min_rx_usec = required_min_rx_usec;
2160  bfd_usec_to_clocks (bm, required_min_rx_usec);
2161  BFD_DBG ("\nChanged session params: %U", format_bfd_session, bs);
2162 
2163  vlib_log_info (bm->log_class, "changed session params: %U",
2167  }
2168  else
2169  {
2170  BFD_DBG ("Ignore parameter change - no change, bs_idx=%u", bs->bs_idx);
2171  }
2172  return 0;
2173 }
2174 
2176 bfd_auth_set_key (u32 conf_key_id, u8 auth_type, u8 key_len,
2177  const u8 * key_data)
2178 {
2179  bfd_main_t *bm = &bfd_main;
2180 #if WITH_LIBSSL > 0
2181  bfd_auth_key_t *auth_key = NULL;
2182  if (!key_len || key_len > bfd_max_key_len_for_auth_type (auth_type))
2183  {
2184  vlib_log_err (bm->log_class,
2185  "invalid authentication key length for auth_type=%d:%s "
2186  "(key_len=%u, must be non-zero, expected max=%u)",
2187  auth_type, bfd_auth_type_str (auth_type), key_len,
2188  (u32) bfd_max_key_len_for_auth_type (auth_type));
2189  return VNET_API_ERROR_INVALID_VALUE;
2190  }
2191  if (!bfd_auth_type_supported (auth_type))
2192  {
2193  vlib_log_err (bm->log_class, "unsupported auth type=%d:%s", auth_type,
2194  bfd_auth_type_str (auth_type));
2195  return VNET_API_ERROR_BFD_NOTSUPP;
2196  }
2197  uword *key_idx_p = hash_get (bm->auth_key_by_conf_key_id, conf_key_id);
2198  if (key_idx_p)
2199  {
2200  /* modifying existing key - must not be used */
2201  const uword key_idx = *key_idx_p;
2202  auth_key = pool_elt_at_index (bm->auth_keys, key_idx);
2203  if (auth_key->use_count > 0)
2204  {
2205  vlib_log_err (bm->log_class,
2206  "authentication key with conf ID %u in use by %u BFD "
2207  "session(s) - cannot modify", conf_key_id,
2208  auth_key->use_count);
2209  return VNET_API_ERROR_BFD_EINUSE;
2210  }
2211  }
2212  else
2213  {
2214  /* adding new key */
2215  pool_get (bm->auth_keys, auth_key);
2216  auth_key->conf_key_id = conf_key_id;
2217  hash_set (bm->auth_key_by_conf_key_id, conf_key_id,
2218  auth_key - bm->auth_keys);
2219  }
2220  auth_key->auth_type = auth_type;
2221  clib_memset (auth_key->key, 0, sizeof (auth_key->key));
2222  clib_memcpy (auth_key->key, key_data, key_len);
2223  return 0;
2224 #else
2225  vlib_log_err (bm->log_class,
2226  "SSL missing, cannot manipulate authentication keys");
2227  return VNET_API_ERROR_BFD_NOTSUPP;
2228 #endif
2229 }
2230 
2232 bfd_auth_del_key (u32 conf_key_id)
2233 {
2234 #if WITH_LIBSSL > 0
2235  bfd_auth_key_t *auth_key = NULL;
2236  bfd_main_t *bm = &bfd_main;
2237  uword *key_idx_p = hash_get (bm->auth_key_by_conf_key_id, conf_key_id);
2238  if (key_idx_p)
2239  {
2240  /* deleting existing key - must not be used */
2241  const uword key_idx = *key_idx_p;
2242  auth_key = pool_elt_at_index (bm->auth_keys, key_idx);
2243  if (auth_key->use_count > 0)
2244  {
2245  vlib_log_err (bm->log_class,
2246  "authentication key with conf ID %u in use by %u BFD "
2247  "session(s) - cannot delete", conf_key_id,
2248  auth_key->use_count);
2249  return VNET_API_ERROR_BFD_EINUSE;
2250  }
2251  hash_unset (bm->auth_key_by_conf_key_id, conf_key_id);
2252  clib_memset (auth_key, 0, sizeof (*auth_key));
2253  pool_put (bm->auth_keys, auth_key);
2254  }
2255  else
2256  {
2257  /* no such key */
2258  vlib_log_err (bm->log_class,
2259  "authentication key with conf ID %u does not exist",
2260  conf_key_id);
2261  return VNET_API_ERROR_BFD_ENOENT;
2262  }
2263  return 0;
2264 #else
2265  vlib_log_err (bm->log_class,
2266  "SSL missing, cannot manipulate authentication keys");
2267  return VNET_API_ERROR_BFD_NOTSUPP;
2268 #endif
2269 }
2270 
2272 
2273 /*
2274  * fd.io coding-style-patch-verification: ON
2275  *
2276  * Local Variables:
2277  * eval: (c-set-style "gnu")
2278  * End:
2279  */
vlib_log_class_t vlib_log_register_class(char *class, char *subclass)
Definition: log.c:176
VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION(bfd_hw_interface_up_down)
static u64 bfd_usec_to_clocks(const bfd_main_t *bm, u64 us)
Definition: bfd_main.c:55
static int bfd_transport_control_frame(vlib_main_t *vm, u32 bi, bfd_session_t *bs)
Definition: bfd_main.c:718
u8 bfd_pkt_get_auth_present(const bfd_pkt_t *pkt)
Definition: bfd_protocol.c:98
static void bfd_notify_listeners_rpc(u32 bs_idx)
Definition: bfd_main.c:609
static void bfd_recalc_echo_tx_interval(bfd_main_t *bm, bfd_session_t *bs)
Definition: bfd_main.c:181
static void bfd_on_state_change(bfd_main_t *bm, bfd_session_t *bs, u64 now, int handling_wakeup)
Definition: bfd_main.c:619
bfd_session_t * bfd_get_session(bfd_main_t *bm, bfd_transport_e t)
Definition: bfd_main.c:1350
bfd_notify_fn_t * listeners
vector of callback notification functions
Definition: bfd_main.h:319
vnet_api_error_t
Definition: api_errno.h:154
#define hash_set(h, key, value)
Definition: hash.h:255
bfd_auth_type_e auth_type
authentication type for this key
Definition: bfd_main.h:55
u32 flags
Definition: vhost_user.h:141
void bfd_consume_pkt(bfd_main_t *bm, const bfd_pkt_t *pkt, u32 bs_idx)
Definition: bfd_main.c:1794
#define clib_min(x, y)
Definition: clib.h:295
u8 curr_bfd_key_id
current key ID sent out in bfd packet
Definition: bfd_main.h:220
#define CLIB_UNUSED(x)
Definition: clib.h:82
static uword random_default_seed(void)
Default random seed (unix/linux user-mode)
Definition: random.h:91
static void bfd_set_effective_required_min_rx(bfd_main_t *bm, bfd_session_t *bs, u64 required_min_rx_clocks)
Definition: bfd_main.c:381
static f64 vlib_process_wait_for_event_or_clock(vlib_main_t *vm, f64 dt)
Suspend a cooperative multi-tasking thread Waits for an event, or for the indicated number of seconds...
Definition: node_funcs.h:673
#define hash_unset(h, key)
Definition: hash.h:261
a
Definition: bitmap.h:538
static uword * vlib_process_wait_for_event(vlib_main_t *vm)
Definition: node_funcs.h:593
void bfd_pkt_set_poll(bfd_pkt_t *pkt)
Definition: bfd_protocol.c:66
bfd_main_t bfd_main
Definition: bfd_main.c:2271
vnet_main_t * vnet_get_main(void)
Definition: misc.c:46
void bfd_on_timeout(vlib_main_t *vm, vlib_node_runtime_t *rt, bfd_main_t *bm, bfd_session_t *bs, u64 now)
Definition: bfd_main.c:1093
#define BFD_ERR(...)
Definition: bfd_debug.h:75
i16 current_data
signed offset in data[], pre_data[] that we are currently processing.
Definition: buffer.h:110
unsigned long u64
Definition: types.h:89
u32 * auth_key_by_conf_key_id
hashmap - index in pool auth_keys by conf_key_id
Definition: bfd_main.h:316
static void bfd_session_switch_auth_to_next(bfd_session_t *bs)
Definition: bfd_main.c:1484
void timing_wheel_init(timing_wheel_t *w, u64 current_cpu_time, f64 cpu_clocks_per_second)
Definition: timing_wheel.c:21
u8 bfd_pkt_get_diag_code(const bfd_pkt_t *pkt)
Definition: bfd_protocol.c:35
static uword bfd_process(vlib_main_t *vm, vlib_node_runtime_t *rt, vlib_frame_t *f)
Definition: bfd_main.c:1138
bfd_session_t * bfd_find_session_by_disc(bfd_main_t *bm, u32 disc)
Definition: bfd_main.c:1416
u8 * format_bfd_auth_key(u8 *s, va_list *args)
Definition: bfd_main.c:69
u64 timing_wheel_next_expiring_elt_time(timing_wheel_t *w)
Definition: timing_wheel.c:342
#define NULL
Definition: clib.h:58
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
u64 echo_last_rx_clocks
timestamp of last echo packet received
Definition: bfd_main.h:181
f64 clocks_per_second
Definition: time.h:54
int bfd_verify_pkt_common(const bfd_pkt_t *pkt)
verify bfd packet - common checks
Definition: bfd_main.c:1435
static void bfd_set_state(bfd_main_t *bm, bfd_session_t *bs, bfd_state_e new_state, int handling_wakeup)
Definition: bfd_main.c:133
u32 echo_secret
secret used for calculating/checking checksum of echo packets
Definition: bfd_main.h:184
u16 current_length
Nbytes between current data and the end of this buffer.
Definition: buffer.h:113
bfd_diag_code_e local_diag
local diagnostics
Definition: bfd_main.h:100
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:522
static u64 clib_cpu_time_now(void)
Definition: time.h:81
static u64 clib_xxhash(u64 key)
Definition: xxhash.h:58
static void bfd_on_config_change(vlib_main_t *vm, vlib_node_runtime_t *rt, bfd_main_t *bm, bfd_session_t *bs, u64 now)
Definition: bfd_main.c:685
void bfd_put_session(bfd_main_t *bm, bfd_session_t *bs)
Definition: bfd_main.c:1384
u64 last_tx_clocks
timestamp of last packet transmitted
Definition: bfd_main.h:166
static u32 format_get_indent(u8 *s)
Definition: format.h:72
u8 remote_seq_number_known
set to 1 if remote sequence number is known
Definition: bfd_main.h:217
static void bfd_set_remote_required_min_rx(bfd_main_t *bm, bfd_session_t *bs, u64 now, u32 remote_required_min_rx_usec)
Definition: bfd_main.c:392
static void bfd_unlock(bfd_main_t *bm)
Definition: bfd_main.h:385
unsigned bfd_auth_type_supported(bfd_auth_type_e auth_type)
Definition: bfd_main.c:2011
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
u8 data[128]
Definition: ipsec.api:251
clib_time_t clib_time
Definition: main.h:87
uword owner_thread_index
Definition: bfd_main.h:267
bfd_diag_code_e
Definition: bfd_protocol.h:177
static void bfd_add_transport_layer(vlib_main_t *vm, u32 bi, bfd_session_t *bs)
Definition: bfd_main.c:702
void bfd_init_final_control_frame(vlib_main_t *vm, vlib_buffer_t *b, bfd_main_t *bm, bfd_session_t *bs, int is_local)
Definition: bfd_main.c:1027
u32 bfd_process_node_index
background process node index
Definition: bfd_main.h:294
u8 bfd_pkt_get_version(const bfd_pkt_t *pkt)
Definition: bfd_protocol.c:22
u64 remote_min_echo_rx_clocks
remote min echo rx interval (clocks)
Definition: bfd_main.h:139
void timing_wheel_insert(timing_wheel_t *w, u64 insert_cpu_time, u32 user_data)
Definition: timing_wheel.c:294
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
Definition: pool.h:236
u64 effective_required_min_rx_clocks
effective required min rx interval (clocks)
Definition: bfd_main.h:127
vlib_log_class_t log_class
log class
Definition: bfd_main.h:322
bfd_auth_type_e
Definition: bfd_protocol.h:36
unsigned char u8
Definition: types.h:56
u32 remote_seq_number
remote sequence number
Definition: bfd_main.h:214
int bfd_add_udp4_transport(vlib_main_t *vm, u32 bi, const bfd_session_t *bs, int is_echo)
Definition: bfd_udp.c:252
double f64
Definition: types.h:142
#define clib_memcpy(d, s, n)
Definition: string.h:180
timing_wheel_t wheel
timing wheel for scheduling timeouts
Definition: bfd_main.h:285
vnet_api_error_t bfd_auth_deactivate(bfd_session_t *bs, u8 is_delayed)
Definition: bfd_main.c:2071
static int bfd_verify_pkt_auth_seq_num(bfd_session_t *bs, u32 received_seq_num, int is_meticulous)
Definition: bfd_main.c:1511
u64 effective_desired_min_tx_clocks
effective desired min tx interval (clocks)
Definition: bfd_main.h:118
static void bfd_lock_check(bfd_main_t *bm)
Definition: bfd_main.h:401
u32 bfd_clocks_to_usec(const bfd_main_t *bm, u64 clocks)
Definition: bfd_main.c:61
u8 key[20]
key data directly usable for bfd purposes - already padded with zeroes (so we don&#39;t need the actual l...
Definition: bfd_main.h:52
vl_api_interface_index_t sw_if_index
Definition: gre.api:50
const char * bfd_poll_state_string(bfd_poll_state_e state)
Definition: bfd_main.c:147
u64 bfd_process_next_wakeup_clocks
When the bfd process is supposed to wake up next.
Definition: bfd_main.h:279
u32 random_seed
for generating random numbers
Definition: bfd_main.h:310
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:173
static uword vlib_process_get_events(vlib_main_t *vm, uword **data_vector)
Return the first event type which has occurred and a vector of per-event data of that type...
Definition: node_funcs.h:516
u8 * format_white_space(u8 *s, va_list *va)
Definition: std-formats.c:129
int bfd_transport_udp4(vlib_main_t *vm, u32 bi, const struct bfd_session_s *bs)
transport packet over udpv4
Definition: bfd_udp.c:437
u64 remote_min_rx_usec
remote min rx interval (microseconds)
Definition: bfd_main.h:130
static void bfd_rpc_event_cb(const bfd_rpc_event_t *a)
Definition: bfd_main.c:548
u8 * format_hex_bytes(u8 *s, va_list *va)
Definition: std-formats.c:84
u64 tx_timeout_clocks
next time at which to transmit a packet
Definition: bfd_main.h:163
void bfd_event(bfd_main_t *bm, bfd_session_t *bs)
Definition: bfd_api.c:200
void vl_api_rpc_call_main_thread(void *fp, u8 *data, u32 data_length)
Definition: vlib_api.c:600
vhost_vring_state_t state
Definition: vhost_user.h:146
u64 config_required_min_rx_clocks
configured required min rx interval (clocks)
Definition: bfd_main.h:124
unsigned int u32
Definition: types.h:88
bfd_auth_key_t * next_key
set to next key to use if delayed switch is enabled - in that case the key is switched when first inc...
Definition: bfd_main.h:208
static vlib_node_registration_t bfd_process_node
(constructor) VLIB_REGISTER_NODE (bfd_process_node)
Definition: bfd_main.c:66
#define vlib_log_debug(...)
Definition: log.h:106
static void bfd_set_poll_state(bfd_session_t *bs, bfd_poll_state_e state)
Definition: bfd_main.c:161
static u32 vlib_get_buffer_index(vlib_main_t *vm, void *p)
Translate buffer pointer into buffer index.
Definition: buffer_funcs.h:257
static void bfd_calc_next_echo_tx(bfd_main_t *bm, bfd_session_t *bs, u64 now)
Definition: bfd_main.c:241
bfd_session_t * bfd_find_session_by_idx(bfd_main_t *bm, uword bs_idx)
Definition: bfd_main.c:1405
static void clib_spinlock_init(clib_spinlock_t *p)
Definition: lock.h:63
static u64 bfd_calc_echo_checksum(u32 discriminator, u64 expire_time, u32 secret)
Definition: bfd_main.c:41
u8 remote_demand
1 if remote system sets demand mode, 0 otherwise
Definition: bfd_main.h:148
bfd_session_t * sessions
pool of bfd sessions context data
Definition: bfd_main.h:282
bfd_poll_state_e
Definition: bfd_main.h:64
bfd_auth_key_t * auth_keys
pool of authentication keys
Definition: bfd_main.h:313
#define hash_get(h, key)
Definition: hash.h:249
bfd_transport_e transport
transport type for this session
Definition: bfd_main.h:233
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:514
static clib_error_t * bfd_sw_interface_up_down(vnet_main_t *vnm, u32 sw_if_index, u32 flags)
Definition: bfd_main.c:1280
static void bfd_event_rpc(u32 bs_idx)
Definition: bfd_main.c:574
static void vlib_process_signal_event(vlib_main_t *vm, uword node_index, uword type_opaque, uword data)
Definition: node_funcs.h:934
u32 remote_discr
remote discriminator
Definition: bfd_main.h:109
u64 default_desired_min_tx_clocks
default desired min tx in clocks
Definition: bfd_main.h:304
u64 echo_last_tx_clocks
timestamp of last echo packet transmitted
Definition: bfd_main.h:178
long ctx[MAX_CONNS]
Definition: main.c:144
bfd_poll_state_e poll_state
state info regarding poll sequence
Definition: bfd_main.h:190
u8 bfd_pkt_get_control_plane_independent(const bfd_pkt_t *pkt)
Definition: bfd_protocol.c:84
unsigned short u16
Definition: types.h:57
signed long i64
Definition: types.h:78
f64 cpu_cps
cpu clocks per second
Definition: bfd_main.h:301
void bfd_pkt_set_auth_present(bfd_pkt_t *pkt)
Definition: bfd_protocol.c:104
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:229
BFD global declarations.
#define pool_put(P, E)
Free an object E in pool P.
Definition: pool.h:286
u64 echo_tx_timeout_clocks
next time at which to transmit echo packet
Definition: bfd_main.h:175
static void bfd_set_diag(bfd_session_t *bs, bfd_diag_code_e code)
Definition: bfd_main.c:122
u8 local_detect_mult
configured detect multiplier
Definition: bfd_main.h:145
static int bfd_is_echo_possible(bfd_session_t *bs)
Definition: bfd_main.c:840
u64 last_rx_clocks
timestamp of last packet received
Definition: bfd_main.h:169
vlib_thread_main_t vlib_thread_main
Definition: threads.c:38
int bfd_transport_udp6(vlib_main_t *vm, u32 bi, const struct bfd_session_s *bs)
transport packet over udpv6
Definition: bfd_udp.c:449
int bfd_add_udp6_transport(vlib_main_t *vm, u32 bi, const bfd_session_t *bs, int is_echo)
Definition: bfd_udp.c:307
u8 bfd_pkt_get_final(const bfd_pkt_t *pkt)
Definition: bfd_protocol.c:72
void bfd_pkt_set_final(bfd_pkt_t *pkt)
Definition: bfd_protocol.c:78
static void bfd_notify_listeners(bfd_main_t *bm, bfd_listen_event_e event, const bfd_session_t *bs)
Definition: bfd_main.c:424
bfd_diag_code_e remote_diag
remote diagnostics
Definition: bfd_main.h:103
static void bfd_send_periodic(vlib_main_t *vm, vlib_node_runtime_t *rt, bfd_main_t *bm, bfd_session_t *bs, u64 now)
Definition: bfd_main.c:947
#define BFD_DEFAULT_DESIRED_MIN_TX_USEC
default, slow transmission interval for BFD packets, per spec at least 1s
Definition: bfd_main.h:442
void bfd_session_start(bfd_main_t *bm, bfd_session_t *bs)
Definition: bfd_main.c:435
const char * bfd_auth_type_str(bfd_auth_type_e auth_type)
Definition: bfd_protocol.c:151
u64 transmit_interval_clocks
transmit interval
Definition: bfd_main.h:160
vnet_main_t * vnet_main
Definition: bfd_main.h:298
u8 bfd_pkt_get_multipoint(const bfd_pkt_t *pkt)
Definition: bfd_protocol.c:124
static void bfd_set_effective_desired_min_tx(bfd_main_t *bm, bfd_session_t *bs, u64 now, u64 desired_min_tx_clocks)
Definition: bfd_main.c:367
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:169
static_always_inline uword vlib_get_thread_index(void)
Definition: threads.h:213
vlib_main_t * vm
Definition: buffer.c:323
static void bfd_recalc_tx_interval(bfd_main_t *bm, bfd_session_t *bs)
Definition: bfd_main.c:172
BFD protocol declarations.
static void vlib_process_signal_event_mt(vlib_main_t *vm, uword node_index, uword type_opaque, uword data)
Signal event to process from any thread.
Definition: node_funcs.h:958
bfd_input_next_t
Definition: bfd_main.c:90
struct bfd_session_s::@52 auth
authentication information
u32 config_desired_min_tx_usec
configured desired min tx interval (microseconds)
Definition: bfd_main.h:112
u32 bfd_max_key_len_for_auth_type(bfd_auth_type_e auth_type)
get the maximum length of key data for given auth type
Definition: bfd_protocol.c:138
static void bfd_calc_next_tx(bfd_main_t *bm, bfd_session_t *bs, u64 now)
Definition: bfd_main.c:191
u8 * format_bfd_session(u8 *s, va_list *args)
Definition: bfd_main.c:1964
u32 * timing_wheel_advance(timing_wheel_t *w, u64 advance_cpu_time, u32 *expired_user_data, u64 *next_expiring_element_cpu_time)
Definition: timing_wheel.c:592
const char * bfd_diag_code_string(bfd_diag_code_e diag)
Definition: bfd_protocol.c:164
static void bfd_add_auth_section(vlib_buffer_t *b, bfd_session_t *bs)
Definition: bfd_main.c:801
static int bfd_verify_pkt_auth_key_sha1(const bfd_pkt_t *pkt, u32 pkt_size, bfd_session_t *bs, u8 bfd_key_id, bfd_auth_key_t *auth_key)
Definition: bfd_main.c:1579
u8 bfd_pkt_get_demand(const bfd_pkt_t *pkt)
Definition: bfd_protocol.c:110
vnet_api_error_t bfd_auth_activate(bfd_session_t *bs, u32 conf_key_id, u8 bfd_key_id, u8 is_delayed)
Definition: bfd_main.c:2022
BFD global declarations.
#define pool_is_free_index(P, I)
Use free bitmap to query whether given index is free.
Definition: pool.h:283
static void bfd_set_remote_required_min_echo_rx(bfd_main_t *bm, bfd_session_t *bs, u64 now, u32 remote_required_min_echo_rx_usec)
Definition: bfd_main.c:408
u64 poll_state_start_or_timeout_clocks
helper for delayed poll sequence - marks either start of running poll sequence or timeout...
Definition: bfd_main.h:196
bfd_state_e
Definition: bfd_protocol.h:195
u64 detection_time_clocks
detection time
Definition: bfd_main.h:187
u8 * bfd_input_format_trace(u8 *s, va_list *args)
Definition: bfd_main.c:476
#define BFD_DBG(...)
Definition: bfd_debug.h:74
void bfd_pkt_set_version(bfd_pkt_t *pkt, int version)
Definition: bfd_protocol.c:28
#define ASSERT(truth)
static void bfd_recalc_detection_time(bfd_main_t *bm, bfd_session_t *bs)
Definition: bfd_main.c:262
u64 config_desired_min_tx_clocks
configured desired min tx interval (clocks)
Definition: bfd_main.h:115
u64 bfd_process_wakeup_event_start_clocks
The timestamp of last wakeup event being sent.
Definition: bfd_main.h:273
u32 vlib_log_get_indent()
Definition: log.c:226
int bfd_udp_is_echo_available(bfd_transport_e transport)
check if the bfd udp layer is echo-capable at this time
Definition: bfd_udp.c:104
u8 echo
1 is echo function is active, 0 otherwise
Definition: bfd_main.h:154
u64 wheel_time_clocks
set to value of timer in timing wheel, 0 if never set
Definition: bfd_main.h:157
u64 remote_desired_min_tx_clocks
remote desired min tx interval (clocks)
Definition: bfd_main.h:142
u8 bfd_pkt_get_state(const bfd_pkt_t *pkt)
Definition: bfd_protocol.c:48
u32 local_discr
local discriminator
Definition: bfd_main.h:106
static f64 random_f64(u32 *seed)
Generate f64 random number in the interval [0,1].
Definition: random.h:145
bfd_hop_type_e hop_type
BFD hop type.
Definition: bfd_main.h:97
u32 conf_key_id
global configuration key ID
Definition: bfd_main.h:43
vnet_api_error_t bfd_auth_del_key(u32 conf_key_id)
delete existing authentication key
Definition: bfd_main.c:2232
vlib_main_t * vlib_main
convenience variables
Definition: bfd_main.h:297
bfd_state_e local_state
session state
Definition: bfd_main.h:91
static int bfd_echo_add_transport_layer(vlib_main_t *vm, u32 bi, bfd_session_t *bs)
Definition: bfd_main.c:735
#define clib_max(x, y)
Definition: clib.h:288
u32 * session_by_disc
hashmap - bfd session by discriminator
Definition: bfd_main.h:291
int bfd_consume_echo_pkt(bfd_main_t *bm, vlib_buffer_t *b)
Definition: bfd_main.c:1928
vnet_api_error_t bfd_session_set_params(bfd_main_t *bm, bfd_session_t *bs, u32 desired_min_tx_usec, u32 required_min_rx_usec, u8 detect_mult)
Definition: bfd_main.c:2113
void(* bfd_notify_fn_t)(bfd_listen_event_e, const bfd_session_t *)
session nitification call back function type
Definition: bfd_main.h:260
struct _vlib_node_registration vlib_node_registration_t
u64 echo_transmit_interval_clocks
transmit interval for echo packets
Definition: bfd_main.h:172
u32 local_seq_number
sequence number incremented occasionally or always (if meticulous)
Definition: bfd_main.h:211
void bfd_register_listener(bfd_notify_fn_t fn)
Register a callback function to receive session notifications.
Definition: bfd_main.c:1307
u32 config_required_min_rx_usec
configured required min rx interval (microseconds)
Definition: bfd_main.h:121
static void bfd_set_timer(bfd_main_t *bm, bfd_session_t *bs, u64 now, int handling_wakeup)
Definition: bfd_main.c:277
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
static void bfd_set_defaults(bfd_main_t *bm, bfd_session_t *bs)
Definition: bfd_main.c:100
u32 use_count
keeps track of how many sessions reference this key
Definition: bfd_main.h:46
#define VLIB_BUFFER_TRACE_TRAJECTORY_INIT(b)
Definition: buffer.h:489
VLIB buffer representation.
Definition: buffer.h:102
#define vlib_log_info(...)
Definition: log.h:105
u64 uword
Definition: types.h:112
u8 next_bfd_key_id
key ID to use when switched to next_key
Definition: bfd_main.h:223
static int bfd_verify_pkt_auth_key(const bfd_pkt_t *pkt, u32 pkt_size, bfd_session_t *bs, u8 bfd_key_id, bfd_auth_key_t *auth_key)
Definition: bfd_main.c:1655
#define vlib_log_crit(...)
Definition: log.h:101
typedef key
Definition: ipsec.api:247
#define F(x)
u8 bfd_pkt_get_poll(const bfd_pkt_t *pkt)
Definition: bfd_protocol.c:60
Linear Congruential Random Number Generator.
int bfd_process_wakeup_events_in_flight
Number of event wakeup RPCs in flight.
Definition: bfd_main.h:270
u64 bfd_process_wakeup_event_delay_clocks
The time it took the last wakeup event to make it to handling.
Definition: bfd_main.h:276
u32 bs_idx
index in bfd_main.sessions pool
Definition: bfd_main.h:88
u8 * format_bfd_session_brief(u8 *s, va_list *args)
Definition: bfd_main.c:2000
static int bfd_transport_echo(vlib_main_t *vm, u32 bi, bfd_session_t *bs)
Definition: bfd_main.c:752
static u32 random_u32(u32 *seed)
32-bit random number generator
Definition: random.h:69
u64 remote_min_echo_rx_usec
remote min echo rx interval (microseconds)
Definition: bfd_main.h:136
vnet_api_error_t bfd_auth_set_key(u32 conf_key_id, u8 auth_type, u8 key_len, const u8 *key_data)
create or modify bfd authentication key
Definition: bfd_main.c:2176
#define STRUCT_SIZE_OF(t, f)
Definition: clib.h:67
#define vec_foreach(var, vec)
Vector iterator.
bfd_state_e remote_state
remote session state
Definition: bfd_main.h:94
const char * bfd_state_string(bfd_state_e state)
Definition: bfd_protocol.c:177
#define vlib_log_err(...)
Definition: log.h:102
bfd packet trace capture
Definition: bfd_main.h:342
u64 min_required_min_rx_while_echo_clocks
minimum required min rx while echo function is active - clocks
Definition: bfd_main.h:307
int bfd_verify_pkt_auth(const bfd_pkt_t *pkt, u16 pkt_size, bfd_session_t *bs)
verify bfd packet - authentication
Definition: bfd_main.c:1716
static void vlib_buffer_free_one(vlib_main_t *vm, u32 buffer_index)
Free one buffer Shorthand to free a single buffer chain.
Definition: buffer_funcs.h:898
#define USEC_PER_SECOND
Definition: bfd_main.h:439
static void bfd_rpc_notify_listeners_cb(const bfd_rpc_notify_listeners_t *a)
Definition: bfd_main.c:590
static int bfd_auth_type_is_meticulous(bfd_auth_type_e auth_type)
Definition: bfd_main.c:1500
bfd_auth_key_t * curr_key
current key in use
Definition: bfd_main.h:202
clib_spinlock_t lock
lock to protect data structures
Definition: bfd_main.h:265
static u32 vlib_buffer_alloc(vlib_main_t *vm, u32 *buffers, u32 n_buffers)
Allocate buffers into supplied array.
Definition: buffer_funcs.h:612
static void bfd_send_echo(vlib_main_t *vm, vlib_node_runtime_t *rt, bfd_main_t *bm, bfd_session_t *bs, u64 now)
Definition: bfd_main.c:888
void bfd_pkt_set_state(bfd_pkt_t *pkt, int value)
Definition: bfd_protocol.c:54
static clib_error_t * bfd_hw_interface_up_down(vnet_main_t *vnm, u32 hw_if_index, u32 flags)
Definition: bfd_main.c:1294
u8 remote_detect_mult
remote detect multiplier
Definition: bfd_main.h:151
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:85
void bfd_pkt_set_diag_code(bfd_pkt_t *pkt, int value)
Definition: bfd_protocol.c:41
#define BFD_REQUIRED_MIN_RX_USEC_WHILE_ECHO
minimum required min rx set locally when echo function is used, per spec should be set to at least 1s...
Definition: bfd_main.h:448
u64 remote_min_rx_clocks
remote min rx interval (clocks)
Definition: bfd_main.h:133
u8 is_delayed
set to 1 if delayed action is pending, which might be activation of authentication, change of key or deactivation
Definition: bfd_main.h:229
static clib_error_t * bfd_main_init(vlib_main_t *vm)
Definition: bfd_main.c:1318
static void bfd_lock(bfd_main_t *bm)
Definition: bfd_main.h:368
bfd_transport_e
Definition: bfd_api.h:30
bfd_listen_event_e
Definition: bfd_main.h:250
u64 wheel_inaccuracy
timing wheel inaccuracy, in clocks
Definition: bfd_main.h:288
static void bfd_init_control_frame(bfd_main_t *bm, bfd_session_t *bs, vlib_buffer_t *b)
Definition: bfd_main.c:857
VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION(bfd_sw_interface_up_down)
void bfd_session_set_flags(bfd_session_t *bs, u8 admin_up_down)
Definition: bfd_main.c:449
static void bfd_check_rx_timeout(bfd_main_t *bm, bfd_session_t *bs, u64 now, int handling_wakeup)
Definition: bfd_main.c:1046