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 UDP transport layer implementation
19 #include <vppinfra/types.h>
20 #include <vlibmemory/api.h>
21 #include <vlib/vlib.h>
22 #include <vlib/buffer.h>
23 #include <vnet/ip/format.h>
24 #include <vnet/ethernet/packet.h>
25 #include <vnet/udp/udp_local.h>
26 #include <vnet/udp/udp_packet.h>
27 #include <vnet/ip/lookup.h>
28 #include <vnet/ip/icmp46_packet.h>
29 #include <vnet/ip/ip4.h>
30 #include <vnet/ip/ip6.h>
31 #include <vnet/ip/ip6_packet.h>
32 #include <vnet/ip/ip6_link.h>
33 #include <vnet/adj/adj.h>
34 #include <vnet/adj/adj_nbr.h>
35 #include <vnet/dpo/receive_dpo.h>
36 #include <vnet/fib/fib_entry.h>
37 #include <vnet/fib/fib_table.h>
38 #include <vlib/stats/stats.h>
39 #include <vnet/bfd/bfd_debug.h>
40 #include <vnet/bfd/bfd_udp.h>
41 #include <vnet/bfd/bfd_main.h>
42 #include <vnet/bfd/bfd_api.h>
43 #include <vnet/bfd/bfd.api_enum.h>
48 /* hashmap - bfd session index by bfd key - used for CLI/API lookup, where
49 * discriminator is unknown */
50 mhash_t bfd_session_idx_by_bfd_key;
51 /* convenience variable */
52 vnet_main_t *vnet_main;
53 /* flag indicating whether echo_source_sw_if_index holds a valid value */
54 int echo_source_is_set;
55 /* loopback interface used to get echo source ip */
56 u32 echo_source_sw_if_index;
57 /* node index of "ip4-arp" node */
59 /* node index of "ip6-discover-neighbor" node */
61 /* node index of "ip4-rewrite" node */
63 /* node index of "ip6-rewrite" node */
65 /* node index of "ip4-midchain" node */
67 /* node index of "ip6-midchain" node */
70 vlib_log_class_t log_class;
71 /* number of active udp4 sessions */
72 u32 udp4_sessions_count;
73 u32 udp4_sessions_count_stat_seg_entry;
74 /* number of active udp6 sessions */
75 u32 udp6_sessions_count;
76 u32 udp6_sessions_count_stat_seg_entry;
79 static vlib_node_registration_t bfd_udp4_input_node;
80 static vlib_node_registration_t bfd_udp6_input_node;
81 static vlib_node_registration_t bfd_udp_echo4_input_node;
82 static vlib_node_registration_t bfd_udp_echo6_input_node;
84 bfd_udp_main_t bfd_udp_main;
87 bfd_udp_update_stat_segment_entry (u32 entry, u64 value)
89 vlib_stats_segment_lock ();
90 vlib_stats_set_gauge (entry, value);
91 vlib_stats_segment_unlock ();
95 bfd_udp_set_echo_source (u32 sw_if_index)
97 vnet_sw_interface_t *sw_if =
98 vnet_get_sw_interface_or_null (bfd_udp_main.vnet_main, sw_if_index);
101 bfd_udp_main.echo_source_sw_if_index = sw_if_index;
102 bfd_udp_main.echo_source_is_set = 1;
105 return VNET_API_ERROR_BFD_ENOENT;
109 bfd_udp_del_echo_source ()
111 bfd_udp_main.echo_source_sw_if_index = ~0;
112 bfd_udp_main.echo_source_is_set = 0;
117 bfd_udp_is_echo_available (bfd_transport_e transport)
119 if (!bfd_udp_main.echo_source_is_set)
121 BFD_DBG ("UDP echo source not set - echo not available");
125 * for the echo to work, we need a loopback interface with at least one
126 * address with netmask length at most 31 (ip4) or 127 (ip6) so that we can
127 * pick an unused address from that subnet
129 vnet_sw_interface_t *sw_if =
130 vnet_get_sw_interface_or_null (bfd_udp_main.vnet_main,
131 bfd_udp_main.echo_source_sw_if_index);
132 if (sw_if && sw_if->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
134 if (BFD_TRANSPORT_UDP4 == transport)
136 ip4_main_t *im = &ip4_main;
137 ip_interface_address_t *ia = NULL;
139 foreach_ip_interface_address (&im->lookup_main, ia,
140 bfd_udp_main.echo_source_sw_if_index,
141 0 /* honor unnumbered */, ({
142 if (ia->address_length <= 31)
149 else if (BFD_TRANSPORT_UDP6 == transport)
151 ip6_main_t *im = &ip6_main;
152 ip_interface_address_t *ia = NULL;
154 foreach_ip_interface_address (&im->lookup_main, ia,
155 bfd_udp_main.echo_source_sw_if_index,
156 0 /* honor unnumbered */, ({
157 if (ia->address_length <= 127)
165 BFD_DBG ("No usable IP address for UDP echo - echo not available");
170 bfd_udp_bs_idx_to_sport (u32 bs_idx)
172 /* The source port MUST be in the range 49152 through 65535. The same UDP
173 * source port number MUST be used for all BFD Control packets associated
174 * with a particular session. The source port number SHOULD be unique among
175 * all BFD sessions on the system. If more than 16384 BFD sessions are
176 * simultaneously active, UDP source port numbers MAY be reused on
177 * multiple sessions, but the number of distinct uses of the same UDP
178 * source port number SHOULD be minimized.
180 return 49152 + bs_idx % (65535 - 49152 + 1);
184 bfd_udp_get_echo_src_ip4 (ip4_address_t * addr)
186 if (!bfd_udp_main.echo_source_is_set)
188 BFD_ERR ("cannot find ip4 address, echo source not set");
191 ip_interface_address_t *ia = NULL;
192 ip4_main_t *im = &ip4_main;
195 foreach_ip_interface_address (
196 &im->lookup_main, ia, bfd_udp_main.echo_source_sw_if_index,
197 0 /* honor unnumbered */, ({
199 ip_interface_address_get_address (&im->lookup_main, ia);
200 if (ia->address_length <= 31)
202 addr->as_u32 = clib_host_to_net_u32 (x->as_u32);
204 * flip the last bit to get a different address, might be network,
208 addr->as_u32 = clib_net_to_host_u32 (addr->as_u32);
213 BFD_ERR ("cannot find ip4 address, no usable address found");
218 bfd_udp_get_echo_src_ip6 (ip6_address_t * addr)
220 if (!bfd_udp_main.echo_source_is_set)
222 BFD_ERR ("cannot find ip6 address, echo source not set");
225 ip_interface_address_t *ia = NULL;
226 ip6_main_t *im = &ip6_main;
229 foreach_ip_interface_address (
230 &im->lookup_main, ia, bfd_udp_main.echo_source_sw_if_index,
231 0 /* honor unnumbered */, ({
233 ip_interface_address_get_address (&im->lookup_main, ia);
234 if (ia->address_length <= 127)
237 addr->as_u8[15] ^= 1; /* flip the last bit of the address */
242 BFD_ERR ("cannot find ip6 address, no usable address found");
247 bfd_udp_get_echo_source (int *is_set, u32 * sw_if_index,
248 int *have_usable_ip4, ip4_address_t * ip4,
249 int *have_usable_ip6, ip6_address_t * ip6)
251 if (bfd_udp_main.echo_source_is_set)
254 *sw_if_index = bfd_udp_main.echo_source_sw_if_index;
255 *have_usable_ip4 = bfd_udp_get_echo_src_ip4 (ip4);
256 *have_usable_ip6 = bfd_udp_get_echo_src_ip6 (ip6);
265 bfd_add_udp4_transport (vlib_main_t * vm, u32 bi, const bfd_session_t * bs,
268 const bfd_udp_session_t *bus = &bs->udp;
269 const bfd_udp_key_t *key = &bus->key;
270 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
272 b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
273 vnet_buffer (b)->ip.adj_index[VLIB_RX] = bus->adj_index;
274 vnet_buffer (b)->ip.adj_index[VLIB_TX] = bus->adj_index;
275 vnet_buffer (b)->sw_if_index[VLIB_RX] = 0;
276 vnet_buffer (b)->sw_if_index[VLIB_TX] = ~0;
282 ip4_udp_headers *headers = NULL;
283 vlib_buffer_advance (b, -sizeof (*headers));
284 headers = vlib_buffer_get_current (b);
285 clib_memset (headers, 0, sizeof (*headers));
286 headers->ip4.ip_version_and_header_length = 0x45;
287 headers->ip4.ttl = 255;
288 headers->ip4.protocol = IP_PROTOCOL_UDP;
289 headers->udp.src_port =
290 clib_host_to_net_u16 (bfd_udp_bs_idx_to_sport (bs->bs_idx));
294 if (!(rv = bfd_udp_get_echo_src_ip4 (&headers->ip4.src_address)))
298 headers->ip4.dst_address.as_u32 = key->local_addr.ip4.as_u32;
299 headers->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_bfd_echo4);
303 headers->ip4.src_address.as_u32 = key->local_addr.ip4.as_u32;
304 headers->ip4.dst_address.as_u32 = key->peer_addr.ip4.as_u32;
305 headers->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_bfd4);
308 /* fix ip length, checksum and udp length */
309 const u16 ip_length = vlib_buffer_length_in_chain (vm, b);
311 headers->ip4.length = clib_host_to_net_u16 (ip_length);
312 headers->ip4.checksum = ip4_header_checksum (&headers->ip4);
314 const u16 udp_length = ip_length - (sizeof (headers->ip4));
315 headers->udp.length = clib_host_to_net_u16 (udp_length);
320 bfd_add_udp6_transport (vlib_main_t * vm, u32 bi, const bfd_session_t * bs,
323 const bfd_udp_session_t *bus = &bs->udp;
324 const bfd_udp_key_t *key = &bus->key;
325 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
327 b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
328 vnet_buffer (b)->ip.adj_index[VLIB_RX] = bus->adj_index;
329 vnet_buffer (b)->ip.adj_index[VLIB_TX] = bus->adj_index;
330 vnet_buffer (b)->sw_if_index[VLIB_RX] = 0;
331 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
337 ip6_udp_headers *headers = NULL;
338 vlib_buffer_advance (b, -sizeof (*headers));
339 headers = vlib_buffer_get_current (b);
340 clib_memset (headers, 0, sizeof (*headers));
341 headers->ip6.ip_version_traffic_class_and_flow_label =
342 clib_host_to_net_u32 (0x6 << 28);
343 headers->ip6.hop_limit = 255;
344 headers->ip6.protocol = IP_PROTOCOL_UDP;
345 headers->udp.src_port =
346 clib_host_to_net_u16 (bfd_udp_bs_idx_to_sport (bs->bs_idx));
350 if (!(rv = bfd_udp_get_echo_src_ip6 (&headers->ip6.src_address)))
354 clib_memcpy_fast (&headers->ip6.dst_address, &key->local_addr.ip6,
355 sizeof (headers->ip6.dst_address));
357 headers->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_bfd_echo6);
361 clib_memcpy_fast (&headers->ip6.src_address, &key->local_addr.ip6,
362 sizeof (headers->ip6.src_address));
363 clib_memcpy_fast (&headers->ip6.dst_address, &key->peer_addr.ip6,
364 sizeof (headers->ip6.dst_address));
365 headers->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_bfd6);
368 /* fix ip payload length and udp length */
369 const u16 udp_length =
370 vlib_buffer_length_in_chain (vm, b) - (sizeof (headers->ip6));
371 headers->udp.length = clib_host_to_net_u16 (udp_length);
372 headers->ip6.payload_length = headers->udp.length;
374 /* IPv6 UDP checksum is mandatory */
376 headers->udp.checksum =
377 ip6_tcp_udp_icmp_compute_checksum (vm, b, &headers->ip6, &bogus);
379 if (headers->udp.checksum == 0)
381 headers->udp.checksum = 0xffff;
387 bfd_create_frame_to_next_node (vlib_main_t *vm, bfd_main_t *bm,
388 const bfd_session_t *bs, u32 bi, u32 next_node,
389 vlib_combined_counter_main_t *tx_counter)
391 vlib_frame_t *f = vlib_get_frame_to_node (vm, next_node);
392 u32 *to_next = vlib_frame_vector_args (f);
395 vlib_put_frame_to_node (vm, next_node, f);
396 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
397 vlib_increment_combined_counter (tx_counter, vm->thread_index, bs->bs_idx, 1,
398 vlib_buffer_length_in_chain (vm, b));
402 bfd_udp_calc_next_node (const struct bfd_session_s *bs, u32 * next_node)
404 vnet_main_t *vnm = vnet_get_main ();
405 const bfd_udp_session_t *bus = &bs->udp;
406 ip_adjacency_t *adj = adj_get (bus->adj_index);
408 /* don't try to send the buffer if the interface is not up */
409 if (!vnet_sw_interface_is_up (vnm, bus->key.sw_if_index))
412 switch (adj->lookup_next_index)
414 case IP_LOOKUP_NEXT_ARP:
415 switch (bs->transport)
417 case BFD_TRANSPORT_UDP4:
418 *next_node = bfd_udp_main.ip4_arp_idx;
420 case BFD_TRANSPORT_UDP6:
421 *next_node = bfd_udp_main.ip6_ndp_idx;
425 case IP_LOOKUP_NEXT_REWRITE:
426 switch (bs->transport)
428 case BFD_TRANSPORT_UDP4:
429 *next_node = bfd_udp_main.ip4_rewrite_idx;
431 case BFD_TRANSPORT_UDP6:
432 *next_node = bfd_udp_main.ip6_rewrite_idx;
436 case IP_LOOKUP_NEXT_MIDCHAIN:
437 switch (bs->transport)
439 case BFD_TRANSPORT_UDP4:
440 *next_node = bfd_udp_main.ip4_midchain_idx;
442 case BFD_TRANSPORT_UDP6:
443 *next_node = bfd_udp_main.ip6_midchain_idx;
455 bfd_transport_udp4 (vlib_main_t *vm, u32 bi, const struct bfd_session_s *bs,
459 int rv = bfd_udp_calc_next_node (bs, &next_node);
460 bfd_main_t *bm = bfd_udp_main.bfd_main;
463 bfd_create_frame_to_next_node (vm, bm, bs, bi, next_node,
464 is_echo ? &bm->tx_echo_counter :
471 bfd_transport_udp6 (vlib_main_t *vm, u32 bi, const struct bfd_session_s *bs,
475 int rv = bfd_udp_calc_next_node (bs, &next_node);
476 bfd_main_t *bm = bfd_udp_main.bfd_main;
479 bfd_create_frame_to_next_node (
480 vm, bfd_udp_main.bfd_main, bs, bi, next_node,
481 is_echo ? &bm->tx_echo_counter : &bm->tx_counter);
486 static bfd_session_t *
487 bfd_lookup_session (bfd_udp_main_t * bum, const bfd_udp_key_t * key)
489 uword *p = mhash_get (&bum->bfd_session_idx_by_bfd_key, key);
492 return bfd_find_session_by_idx (bum->bfd_main, *p);
498 bfd_udp_key_init (bfd_udp_key_t * key, u32 sw_if_index,
499 const ip46_address_t * local_addr,
500 const ip46_address_t * peer_addr)
502 clib_memset (key, 0, sizeof (*key));
503 key->sw_if_index = sw_if_index;
504 key->local_addr.as_u64[0] = local_addr->as_u64[0];
505 key->local_addr.as_u64[1] = local_addr->as_u64[1];
506 key->peer_addr.as_u64[0] = peer_addr->as_u64[0];
507 key->peer_addr.as_u64[1] = peer_addr->as_u64[1];
510 static vnet_api_error_t
511 bfd_udp_add_session_internal (vlib_main_t * vm, bfd_udp_main_t * bum,
512 u32 sw_if_index, u32 desired_min_tx_usec,
513 u32 required_min_rx_usec, u8 detect_mult,
514 const ip46_address_t * local_addr,
515 const ip46_address_t * peer_addr,
516 bfd_session_t ** bs_out)
518 /* get a pool entry and if we end up not needing it, give it back */
519 bfd_transport_e t = BFD_TRANSPORT_UDP4;
520 if (!ip46_address_is_ip4 (local_addr))
522 t = BFD_TRANSPORT_UDP6;
524 bfd_session_t *bs = bfd_get_session (bum->bfd_main, t);
527 return VNET_API_ERROR_BFD_EAGAIN;
529 bfd_udp_session_t *bus = &bs->udp;
530 clib_memset (bus, 0, sizeof (*bus));
531 bus->adj_index = ADJ_INDEX_INVALID;
532 bfd_udp_key_t *key = &bus->key;
533 bfd_udp_key_init (key, sw_if_index, local_addr, peer_addr);
534 const bfd_session_t *tmp = bfd_lookup_session (bum, key);
537 vlib_log_err (bum->log_class,
538 "duplicate bfd-udp session, existing bs_idx=%d",
540 bfd_put_session (bum->bfd_main, bs);
541 return VNET_API_ERROR_BFD_EEXIST;
543 mhash_set (&bum->bfd_session_idx_by_bfd_key, key, bs->bs_idx, NULL);
544 BFD_DBG ("session created, bs_idx=%u, sw_if_index=%d, local=%U, peer=%U",
545 bs->bs_idx, key->sw_if_index, format_ip46_address,
546 &key->local_addr, IP46_TYPE_ANY, format_ip46_address,
547 &key->peer_addr, IP46_TYPE_ANY);
548 vlib_log_info (bum->log_class, "create BFD session: %U",
549 format_bfd_session, bs);
550 const ip46_address_t *peer =
551 (vnet_sw_interface_is_p2p (vnet_get_main (), key->sw_if_index) ?
554 if (BFD_TRANSPORT_UDP4 == t)
556 bus->adj_index = adj_nbr_add_or_lock (FIB_PROTOCOL_IP4, VNET_LINK_IP4,
557 peer, key->sw_if_index);
558 BFD_DBG ("adj_nbr_add_or_lock(FIB_PROTOCOL_IP4, VNET_LINK_IP4, %U, %d) "
560 format_ip46_address, peer, IP46_TYPE_ANY, key->sw_if_index,
562 ++bum->udp4_sessions_count;
563 bfd_udp_update_stat_segment_entry (
564 bum->udp4_sessions_count_stat_seg_entry, bum->udp4_sessions_count);
565 if (1 == bum->udp4_sessions_count)
567 udp_register_dst_port (vm, UDP_DST_PORT_bfd4,
568 bfd_udp4_input_node.index, 1);
569 udp_register_dst_port (vm, UDP_DST_PORT_bfd_echo4,
570 bfd_udp_echo4_input_node.index, 1);
575 bus->adj_index = adj_nbr_add_or_lock (FIB_PROTOCOL_IP6, VNET_LINK_IP6,
576 peer, key->sw_if_index);
577 BFD_DBG ("adj_nbr_add_or_lock(FIB_PROTOCOL_IP6, VNET_LINK_IP6, %U, %d) "
579 format_ip46_address, peer, IP46_TYPE_ANY, key->sw_if_index,
581 ++bum->udp6_sessions_count;
582 bfd_udp_update_stat_segment_entry (
583 bum->udp6_sessions_count_stat_seg_entry, bum->udp6_sessions_count);
584 if (1 == bum->udp6_sessions_count)
586 udp_register_dst_port (vm, UDP_DST_PORT_bfd6,
587 bfd_udp6_input_node.index, 0);
588 udp_register_dst_port (vm, UDP_DST_PORT_bfd_echo6,
589 bfd_udp_echo6_input_node.index, 0);
593 return bfd_session_set_params (bum->bfd_main, bs, desired_min_tx_usec,
594 required_min_rx_usec, detect_mult);
597 static vnet_api_error_t
598 bfd_udp_validate_api_input (u32 sw_if_index,
599 const ip46_address_t * local_addr,
600 const ip46_address_t * peer_addr)
602 bfd_udp_main_t *bum = &bfd_udp_main;
603 vnet_sw_interface_t *sw_if =
604 vnet_get_sw_interface_or_null (bfd_udp_main.vnet_main, sw_if_index);
607 vlib_log_err (bum->log_class,
608 "got NULL sw_if when getting interface by index %u",
610 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
612 if (ip46_address_is_ip4 (local_addr))
614 if (!ip46_address_is_ip4 (peer_addr))
616 vlib_log_err (bum->log_class,
617 "IP family mismatch (local is ipv4, peer is ipv6)");
618 return VNET_API_ERROR_INVALID_ARGUMENT;
623 if (ip46_address_is_ip4 (peer_addr))
625 vlib_log_err (bum->log_class,
626 "IP family mismatch (local is ipv6, peer is ipv4)");
627 return VNET_API_ERROR_INVALID_ARGUMENT;
634 static vnet_api_error_t
635 bfd_udp_find_session_by_api_input (u32 sw_if_index,
636 const ip46_address_t * local_addr,
637 const ip46_address_t * peer_addr,
638 bfd_session_t ** bs_out)
640 vnet_api_error_t rv =
641 bfd_udp_validate_api_input (sw_if_index, local_addr, peer_addr);
644 bfd_udp_main_t *bum = &bfd_udp_main;
646 bfd_udp_key_init (&key, sw_if_index, local_addr, peer_addr);
647 bfd_session_t *bs = bfd_lookup_session (bum, &key);
654 vlib_log_err (bum->log_class,
655 "BFD session not found, sw_if_index=%u, local=%U, peer=%U",
656 sw_if_index, format_ip46_address, local_addr,
657 IP46_TYPE_ANY, format_ip46_address, peer_addr,
659 return VNET_API_ERROR_BFD_ENOENT;
665 static vnet_api_error_t
666 bfd_api_verify_common (u32 sw_if_index, u32 desired_min_tx_usec,
667 u8 detect_mult, const ip46_address_t *local_addr,
668 const ip46_address_t *peer_addr)
670 bfd_udp_main_t *bum = &bfd_udp_main;
671 vnet_api_error_t rv =
672 bfd_udp_validate_api_input (sw_if_index, local_addr, peer_addr);
679 vlib_log_err (bum->log_class, "detect_mult < 1");
680 return VNET_API_ERROR_INVALID_ARGUMENT;
682 if (desired_min_tx_usec < 1)
684 vlib_log_err (bum->log_class, "desired_min_tx_usec < 1");
685 return VNET_API_ERROR_INVALID_ARGUMENT;
691 bfd_udp_del_session_internal (vlib_main_t * vm, bfd_session_t * bs)
693 bfd_udp_main_t *bum = &bfd_udp_main;
694 BFD_DBG ("free bfd-udp session, bs_idx=%d", bs->bs_idx);
695 bfd_session_stop (bum->bfd_main, bs);
696 mhash_unset (&bum->bfd_session_idx_by_bfd_key, &bs->udp.key, NULL);
697 adj_unlock (bs->udp.adj_index);
698 switch (bs->transport)
700 case BFD_TRANSPORT_UDP4:
701 --bum->udp4_sessions_count;
702 bfd_udp_update_stat_segment_entry (
703 bum->udp4_sessions_count_stat_seg_entry, bum->udp4_sessions_count);
704 if (!bum->udp4_sessions_count)
706 udp_unregister_dst_port (vm, UDP_DST_PORT_bfd4, 1);
707 udp_unregister_dst_port (vm, UDP_DST_PORT_bfd_echo4, 1);
710 case BFD_TRANSPORT_UDP6:
711 --bum->udp6_sessions_count;
712 bfd_udp_update_stat_segment_entry (
713 bum->udp6_sessions_count_stat_seg_entry, bum->udp6_sessions_count);
714 if (!bum->udp6_sessions_count)
716 udp_unregister_dst_port (vm, UDP_DST_PORT_bfd6, 0);
717 udp_unregister_dst_port (vm, UDP_DST_PORT_bfd_echo6, 0);
721 bfd_put_session (bum->bfd_main, bs);
724 static vnet_api_error_t
725 bfd_udp_add_and_start_session (u32 sw_if_index,
726 const ip46_address_t *local_addr,
727 const ip46_address_t *peer_addr,
728 u32 desired_min_tx_usec,
729 u32 required_min_rx_usec, u8 detect_mult,
730 u8 is_authenticated, u32 conf_key_id,
733 bfd_session_t *bs = NULL;
736 rv = bfd_udp_add_session_internal (
737 vlib_get_main (), &bfd_udp_main, sw_if_index, desired_min_tx_usec,
738 required_min_rx_usec, detect_mult, local_addr, peer_addr, &bs);
740 if (!rv && is_authenticated)
742 rv = bfd_auth_activate (bs, conf_key_id, bfd_key_id,
743 0 /* is not delayed */);
746 bfd_udp_del_session_internal (vlib_get_main (), bs);
751 bfd_session_start (bfd_udp_main.bfd_main, bs);
758 bfd_udp_add_session (u32 sw_if_index, const ip46_address_t * local_addr,
759 const ip46_address_t * peer_addr,
760 u32 desired_min_tx_usec, u32 required_min_rx_usec,
761 u8 detect_mult, u8 is_authenticated, u32 conf_key_id,
764 bfd_main_t *bm = &bfd_main;
767 vnet_api_error_t rv = bfd_api_verify_common (
768 sw_if_index, desired_min_tx_usec, detect_mult, local_addr, peer_addr);
771 rv = bfd_udp_add_and_start_session (
772 sw_if_index, local_addr, peer_addr, desired_min_tx_usec,
773 required_min_rx_usec, detect_mult, is_authenticated, conf_key_id,
781 bfd_udp_upd_session (u32 sw_if_index, const ip46_address_t *local_addr,
782 const ip46_address_t *peer_addr, u32 desired_min_tx_usec,
783 u32 required_min_rx_usec, u8 detect_mult,
784 u8 is_authenticated, u32 conf_key_id, u8 bfd_key_id)
786 bfd_main_t *bm = &bfd_main;
789 vnet_api_error_t rv = bfd_api_verify_common (
790 sw_if_index, desired_min_tx_usec, detect_mult, local_addr, peer_addr);
793 bfd_session_t *bs = NULL;
795 rv = bfd_udp_find_session_by_api_input (sw_if_index, local_addr,
797 if (VNET_API_ERROR_BFD_ENOENT == rv)
798 rv = bfd_udp_add_and_start_session (
799 sw_if_index, local_addr, peer_addr, desired_min_tx_usec,
800 required_min_rx_usec, detect_mult, is_authenticated, conf_key_id,
803 rv = bfd_session_set_params (bfd_udp_main.bfd_main, bs,
804 desired_min_tx_usec, required_min_rx_usec,
813 bfd_udp_mod_session (u32 sw_if_index, const ip46_address_t *local_addr,
814 const ip46_address_t *peer_addr, u32 desired_min_tx_usec,
815 u32 required_min_rx_usec, u8 detect_mult)
817 bfd_session_t *bs = NULL;
818 bfd_main_t *bm = &bfd_main;
819 vnet_api_error_t error;
821 vnet_api_error_t rv =
822 bfd_udp_find_session_by_api_input (sw_if_index, local_addr, peer_addr,
830 error = bfd_session_set_params (bfd_udp_main.bfd_main, bs,
831 desired_min_tx_usec, required_min_rx_usec,
838 bfd_udp_del_session (u32 sw_if_index,
839 const ip46_address_t * local_addr,
840 const ip46_address_t * peer_addr)
842 bfd_session_t *bs = NULL;
843 bfd_main_t *bm = &bfd_main;
845 vnet_api_error_t rv =
846 bfd_udp_find_session_by_api_input (sw_if_index, local_addr, peer_addr,
853 bfd_udp_del_session_internal (vlib_get_main (), bs);
859 bfd_udp_session_set_flags (vlib_main_t * vm, u32 sw_if_index,
860 const ip46_address_t * local_addr,
861 const ip46_address_t * peer_addr, u8 admin_up_down)
863 bfd_session_t *bs = NULL;
864 bfd_main_t *bm = &bfd_main;
866 vnet_api_error_t rv =
867 bfd_udp_find_session_by_api_input (sw_if_index, local_addr, peer_addr,
874 bfd_session_set_flags (vm, bs, admin_up_down);
880 bfd_udp_auth_activate (u32 sw_if_index,
881 const ip46_address_t * local_addr,
882 const ip46_address_t * peer_addr,
883 u32 conf_key_id, u8 key_id, u8 is_delayed)
885 bfd_main_t *bm = &bfd_main;
887 vnet_api_error_t error;
889 bfd_session_t *bs = NULL;
890 vnet_api_error_t rv =
891 bfd_udp_find_session_by_api_input (sw_if_index, local_addr, peer_addr,
898 error = bfd_auth_activate (bs, conf_key_id, key_id, is_delayed);
904 bfd_udp_auth_deactivate (u32 sw_if_index,
905 const ip46_address_t * local_addr,
906 const ip46_address_t * peer_addr, u8 is_delayed)
908 bfd_main_t *bm = &bfd_main;
909 vnet_api_error_t error;
911 bfd_session_t *bs = NULL;
912 vnet_api_error_t rv =
913 bfd_udp_find_session_by_api_input (sw_if_index, local_addr, peer_addr,
920 error = bfd_auth_deactivate (bs, is_delayed);
927 BFD_UDP_INPUT_NEXT_NORMAL,
928 BFD_UDP_INPUT_NEXT_REPLY_ARP,
929 BFD_UDP_INPUT_NEXT_REPLY_REWRITE,
930 BFD_UDP_INPUT_NEXT_REPLY_MIDCHAIN,
931 BFD_UDP_INPUT_N_NEXT,
932 } bfd_udp_input_next_t;
936 BFD_UDP_ECHO_INPUT_NEXT_NORMAL,
937 BFD_UDP_ECHO_INPUT_NEXT_REPLY_ARP,
938 BFD_UDP_ECHO_INPUT_NEXT_REPLY_REWRITE,
939 BFD_UDP_ECHO_INPUT_N_NEXT,
940 } bfd_udp_echo_input_next_t;
942 static_always_inline vl_counter_bfd_udp_enum_t
943 bfd_error_to_udp (bfd_error_t e)
945 /* The UDP error is a super set of the proto independent errors */
946 return ((vl_counter_bfd_udp_enum_t) e);
950 bfd_udp4_find_headers (vlib_buffer_t * b, ip4_header_t ** ip4,
953 /* sanity check first */
954 const i32 start = vnet_buffer (b)->l3_hdr_offset;
955 if (start < -(signed) sizeof (b->pre_data))
957 BFD_ERR ("Start of ip header is before pre_data, ignoring");
962 *ip4 = (ip4_header_t *) (b->data + start);
963 if ((u8 *) * ip4 > (u8 *) vlib_buffer_get_current (b))
965 BFD_ERR ("Start of ip header is beyond current data, ignoring");
970 *udp = (udp_header_t *) ((*ip4) + 1);
973 static vl_counter_bfd_udp_enum_t
974 bfd_udp4_verify_transport (const ip4_header_t *ip4, const udp_header_t *udp,
975 const bfd_session_t *bs)
977 const bfd_udp_session_t *bus = &bs->udp;
978 const bfd_udp_key_t *key = &bus->key;
979 if (ip4->src_address.as_u32 != key->peer_addr.ip4.as_u32)
981 BFD_ERR ("IPv4 src addr mismatch, got %U, expected %U",
982 format_ip4_address, ip4->src_address.as_u8, format_ip4_address,
983 key->peer_addr.ip4.as_u8);
984 return BFD_UDP_ERROR_SRC_MISMATCH;
986 if (ip4->dst_address.as_u32 != key->local_addr.ip4.as_u32)
988 BFD_ERR ("IPv4 dst addr mismatch, got %U, expected %U",
989 format_ip4_address, ip4->dst_address.as_u8, format_ip4_address,
990 key->local_addr.ip4.as_u8);
991 return BFD_UDP_ERROR_DST_MISMATCH;
993 const u8 expected_ttl = 255;
994 if (ip4->ttl != expected_ttl)
996 BFD_ERR ("IPv4 unexpected TTL value %u, expected %u", ip4->ttl,
998 return BFD_UDP_ERROR_TTL;
1000 if (clib_net_to_host_u16 (udp->src_port) < 49152)
1002 BFD_ERR ("Invalid UDP src port %u, out of range <49152,65535>",
1005 return BFD_UDP_ERROR_NONE;
1015 bfd_rpc_update_session (vlib_main_t *vm, u32 bs_idx, const bfd_pkt_t *pkt)
1017 bfd_main_t *bm = &bfd_main;
1020 err = bfd_consume_pkt (vm, bm, pkt, bs_idx);
1026 static vl_counter_bfd_udp_enum_t
1027 bfd_udp4_scan (vlib_main_t *vm, vlib_buffer_t *b, bfd_session_t **bs_out)
1029 const bfd_pkt_t *pkt = vlib_buffer_get_current (b);
1030 if (sizeof (*pkt) > b->current_length)
1033 ("Payload size %d too small to hold bfd packet of minimum size %d",
1034 b->current_length, sizeof (*pkt));
1035 return BFD_UDP_ERROR_BAD;
1039 bfd_udp4_find_headers (b, &ip4, &udp);
1042 BFD_ERR ("Couldn't find ip4 or udp header");
1043 return BFD_UDP_ERROR_BAD;
1045 const u32 udp_payload_length = udp->length - sizeof (*udp);
1046 if (pkt->head.length > udp_payload_length)
1049 ("BFD packet length is larger than udp payload length (%u > %u)",
1050 pkt->head.length, udp_payload_length);
1051 return BFD_UDP_ERROR_LENGTH;
1053 vl_counter_bfd_udp_enum_t err;
1054 if (BFD_UDP_ERROR_NONE !=
1055 (err = bfd_error_to_udp (bfd_verify_pkt_common (pkt))))
1059 bfd_session_t *bs = NULL;
1062 BFD_DBG ("Looking up BFD session using discriminator %u",
1064 bs = bfd_find_session_by_disc (bfd_udp_main.bfd_main, pkt->your_disc);
1069 clib_memset (&key, 0, sizeof (key));
1070 key.sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
1071 key.local_addr.ip4.as_u32 = ip4->dst_address.as_u32;
1072 key.peer_addr.ip4.as_u32 = ip4->src_address.as_u32;
1073 BFD_DBG ("Looking up BFD session using key (sw_if_index=%u, local=%U, "
1075 key.sw_if_index, format_ip4_address, key.local_addr.ip4.as_u8,
1076 format_ip4_address, key.peer_addr.ip4.as_u8);
1077 bs = bfd_lookup_session (&bfd_udp_main, &key);
1081 BFD_ERR ("BFD session lookup failed - no session matches BFD pkt");
1082 return BFD_UDP_ERROR_NO_SESSION;
1084 BFD_DBG ("BFD session found, bs_idx=%u", bs->bs_idx);
1085 if (!bfd_verify_pkt_auth (vm, pkt, b->current_length, bs))
1087 BFD_ERR ("Packet verification failed, dropping packet");
1088 return BFD_UDP_ERROR_FAILED_VERIFICATION;
1090 if (BFD_UDP_ERROR_NONE != (err = bfd_udp4_verify_transport (ip4, udp, bs)))
1094 err = bfd_error_to_udp (bfd_rpc_update_session (vm, bs->bs_idx, pkt));
1100 bfd_udp6_find_headers (vlib_buffer_t * b, ip6_header_t ** ip6,
1101 udp_header_t ** udp)
1103 /* sanity check first */
1104 const i32 start = vnet_buffer (b)->l3_hdr_offset;
1105 if (start < -(signed) sizeof (b->pre_data))
1107 BFD_ERR ("Start of ip header is before pre_data, ignoring");
1112 *ip6 = (ip6_header_t *) (b->data + start);
1113 if ((u8 *) * ip6 > (u8 *) vlib_buffer_get_current (b))
1115 BFD_ERR ("Start of ip header is beyond current data, ignoring");
1120 if ((*ip6)->protocol != IP_PROTOCOL_UDP)
1122 BFD_ERR ("Unexpected protocol in IPv6 header '%u', expected '%u' (== "
1123 "IP_PROTOCOL_UDP)", (*ip6)->protocol, IP_PROTOCOL_UDP);
1128 *udp = (udp_header_t *) ((*ip6) + 1);
1131 static vl_counter_bfd_udp_enum_t
1132 bfd_udp6_verify_transport (const ip6_header_t *ip6, const udp_header_t *udp,
1133 const bfd_session_t *bs)
1135 const bfd_udp_session_t *bus = &bs->udp;
1136 const bfd_udp_key_t *key = &bus->key;
1137 if (ip6->src_address.as_u64[0] != key->peer_addr.ip6.as_u64[0] &&
1138 ip6->src_address.as_u64[1] != key->peer_addr.ip6.as_u64[1])
1140 BFD_ERR ("IP src addr mismatch, got %U, expected %U",
1141 format_ip6_address, ip6, format_ip6_address,
1142 &key->peer_addr.ip6);
1143 return BFD_UDP_ERROR_SRC_MISMATCH;
1145 if (ip6->dst_address.as_u64[0] != key->local_addr.ip6.as_u64[0] &&
1146 ip6->dst_address.as_u64[1] != key->local_addr.ip6.as_u64[1])
1148 BFD_ERR ("IP dst addr mismatch, got %U, expected %U",
1149 format_ip6_address, ip6, format_ip6_address,
1150 &key->local_addr.ip6);
1151 return BFD_UDP_ERROR_DST_MISMATCH;
1153 const u8 expected_hop_limit = 255;
1154 if (ip6->hop_limit != expected_hop_limit)
1156 BFD_ERR ("IPv6 unexpected hop-limit value %u, expected %u",
1157 ip6->hop_limit, expected_hop_limit);
1158 return BFD_UDP_ERROR_TTL;
1160 if (clib_net_to_host_u16 (udp->src_port) < 49152)
1162 BFD_ERR ("Invalid UDP src port %u, out of range <49152,65535>",
1165 return BFD_UDP_ERROR_NONE;
1168 static vl_counter_bfd_udp_enum_t
1169 bfd_udp6_scan (vlib_main_t *vm, vlib_buffer_t *b, bfd_session_t **bs_out)
1171 const bfd_pkt_t *pkt = vlib_buffer_get_current (b);
1172 if (sizeof (*pkt) > b->current_length)
1175 ("Payload size %d too small to hold bfd packet of minimum size %d",
1176 b->current_length, sizeof (*pkt));
1177 return BFD_UDP_ERROR_BAD;
1181 bfd_udp6_find_headers (b, &ip6, &udp);
1184 BFD_ERR ("Couldn't find ip6 or udp header");
1185 return BFD_UDP_ERROR_BAD;
1187 const u32 udp_payload_length = udp->length - sizeof (*udp);
1188 if (pkt->head.length > udp_payload_length)
1191 ("BFD packet length is larger than udp payload length (%u > %u)",
1192 pkt->head.length, udp_payload_length);
1193 return BFD_UDP_ERROR_BAD;
1195 vl_counter_bfd_udp_enum_t err;
1196 if (BFD_UDP_ERROR_NONE !=
1197 (err = bfd_error_to_udp (bfd_verify_pkt_common (pkt))))
1201 bfd_session_t *bs = NULL;
1204 BFD_DBG ("Looking up BFD session using discriminator %u",
1206 bs = bfd_find_session_by_disc (bfd_udp_main.bfd_main, pkt->your_disc);
1211 clib_memset (&key, 0, sizeof (key));
1212 key.sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
1213 key.local_addr.ip6.as_u64[0] = ip6->dst_address.as_u64[0];
1214 key.local_addr.ip6.as_u64[1] = ip6->dst_address.as_u64[1];
1215 key.peer_addr.ip6.as_u64[0] = ip6->src_address.as_u64[0];
1216 key.peer_addr.ip6.as_u64[1] = ip6->src_address.as_u64[1];
1217 BFD_DBG ("Looking up BFD session using key (sw_if_index=%u, local=%U, "
1219 key.sw_if_index, format_ip6_address, &key.local_addr,
1220 format_ip6_address, &key.peer_addr);
1221 bs = bfd_lookup_session (&bfd_udp_main, &key);
1225 BFD_ERR ("BFD session lookup failed - no session matches BFD pkt");
1226 return BFD_UDP_ERROR_NO_SESSION;
1228 BFD_DBG ("BFD session found, bs_idx=%u", bs->bs_idx);
1229 if (!bfd_verify_pkt_auth (vm, pkt, b->current_length, bs))
1231 BFD_ERR ("Packet verification failed, dropping packet");
1232 return BFD_UDP_ERROR_FAILED_VERIFICATION;
1234 if (BFD_UDP_ERROR_NONE != (err = bfd_udp6_verify_transport (ip6, udp, bs)))
1238 err = bfd_error_to_udp (bfd_rpc_update_session (vm, bs->bs_idx, pkt));
1244 * Process a frame of bfd packets
1245 * Expect 1 packet / frame
1248 bfd_udp_input (vlib_main_t * vm, vlib_node_runtime_t * rt,
1249 vlib_frame_t * f, int is_ipv6)
1251 u32 n_left_from, *from;
1252 bfd_input_trace_t *t0;
1253 bfd_main_t *bm = &bfd_main;
1255 from = vlib_frame_vector_args (f); /* array of buffer indices */
1256 n_left_from = f->n_vectors; /* number of buffer indices */
1258 while (n_left_from > 0)
1265 b0 = vlib_get_buffer (vm, bi0);
1267 bfd_session_t *bs = NULL;
1269 /* If this pkt is traced, snapshot the data */
1270 if (b0->flags & VLIB_BUFFER_IS_TRACED)
1273 t0 = vlib_add_trace (vm, rt, b0, sizeof (*t0));
1274 len = (b0->current_length < sizeof (t0->data)) ? b0->current_length
1275 : sizeof (t0->data);
1277 clib_memcpy_fast (t0->data, vlib_buffer_get_current (b0), len);
1280 /* scan this bfd pkt. error0 is the counter index to bmp */
1284 error0 = bfd_udp6_scan (vm, b0, &bs);
1288 error0 = bfd_udp4_scan (vm, b0, &bs);
1290 b0->error = rt->errors[error0];
1292 next0 = BFD_UDP_INPUT_NEXT_NORMAL;
1293 if (BFD_UDP_ERROR_NONE == error0)
1295 vlib_increment_combined_counter (
1296 &bm->rx_counter, vm->thread_index, bs->bs_idx, 1,
1297 vlib_buffer_length_in_chain (vm, b0));
1299 * if everything went fine, check for poll bit, if present, re-use
1300 * the buffer and based on (now updated) session parameters, send
1301 * the final packet back
1303 const bfd_pkt_t *pkt = vlib_buffer_get_current (b0);
1304 if (bfd_pkt_get_poll (pkt))
1306 b0->current_data = 0;
1307 b0->current_length = 0;
1308 bfd_init_final_control_frame (vm, b0, bs);
1311 vlib_node_increment_counter (vm, bfd_udp6_input_node.index,
1316 vlib_node_increment_counter (vm, bfd_udp4_input_node.index,
1319 const bfd_udp_session_t *bus = &bs->udp;
1320 ip_adjacency_t *adj = adj_get (bus->adj_index);
1321 switch (adj->lookup_next_index)
1323 case IP_LOOKUP_NEXT_ARP:
1324 next0 = BFD_UDP_INPUT_NEXT_REPLY_ARP;
1326 case IP_LOOKUP_NEXT_REWRITE:
1327 next0 = BFD_UDP_INPUT_NEXT_REPLY_REWRITE;
1329 case IP_LOOKUP_NEXT_MIDCHAIN:
1330 next0 = BFD_UDP_INPUT_NEXT_REPLY_MIDCHAIN;
1339 vlib_set_next_frame_buffer (vm, rt, next0, bi0);
1345 return f->n_vectors;
1349 bfd_udp4_input (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
1351 return bfd_udp_input (vm, rt, f, 0);
1355 * bfd input graph node declaration
1358 VLIB_REGISTER_NODE (bfd_udp4_input_node, static) = {
1359 .function = bfd_udp4_input,
1360 .name = "bfd-udp4-input",
1361 .vector_size = sizeof (u32),
1362 .type = VLIB_NODE_TYPE_INTERNAL,
1364 .n_errors = BFD_UDP_N_ERROR,
1365 .error_counters = bfd_udp_error_counters,
1367 .format_trace = bfd_input_format_trace,
1369 .n_next_nodes = BFD_UDP_INPUT_N_NEXT,
1372 [BFD_UDP_INPUT_NEXT_NORMAL] = "error-drop",
1373 [BFD_UDP_INPUT_NEXT_REPLY_ARP] = "ip4-arp",
1374 [BFD_UDP_INPUT_NEXT_REPLY_REWRITE] = "ip4-lookup",
1375 [BFD_UDP_INPUT_NEXT_REPLY_MIDCHAIN] = "ip4-midchain",
1381 bfd_udp6_input (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
1383 return bfd_udp_input (vm, rt, f, 1);
1387 VLIB_REGISTER_NODE (bfd_udp6_input_node, static) = {
1388 .function = bfd_udp6_input,
1389 .name = "bfd-udp6-input",
1390 .vector_size = sizeof (u32),
1391 .type = VLIB_NODE_TYPE_INTERNAL,
1393 .n_errors = BFD_UDP_N_ERROR,
1394 .error_counters = bfd_udp_error_counters,
1396 .format_trace = bfd_input_format_trace,
1398 .n_next_nodes = BFD_UDP_INPUT_N_NEXT,
1401 [BFD_UDP_INPUT_NEXT_NORMAL] = "error-drop",
1402 [BFD_UDP_INPUT_NEXT_REPLY_ARP] = "ip6-discover-neighbor",
1403 [BFD_UDP_INPUT_NEXT_REPLY_REWRITE] = "ip6-lookup",
1404 [BFD_UDP_INPUT_NEXT_REPLY_MIDCHAIN] = "ip6-midchain",
1410 * Process a frame of bfd echo packets
1411 * Expect 1 packet / frame
1414 bfd_udp_echo_input (vlib_main_t * vm, vlib_node_runtime_t * rt,
1415 vlib_frame_t * f, int is_ipv6)
1417 u32 n_left_from, *from;
1418 bfd_input_trace_t *t0;
1419 bfd_main_t *bm = &bfd_main;
1421 from = vlib_frame_vector_args (f); /* array of buffer indices */
1422 n_left_from = f->n_vectors; /* number of buffer indices */
1424 while (n_left_from > 0)
1431 b0 = vlib_get_buffer (vm, bi0);
1433 /* If this pkt is traced, snapshot the data */
1434 if (b0->flags & VLIB_BUFFER_IS_TRACED)
1437 t0 = vlib_add_trace (vm, rt, b0, sizeof (*t0));
1438 len = (b0->current_length < sizeof (t0->data)) ? b0->current_length
1439 : sizeof (t0->data);
1441 clib_memcpy_fast (t0->data, vlib_buffer_get_current (b0), len);
1444 bfd_session_t *bs = NULL;
1446 if ((bs = bfd_consume_echo_pkt (vm, bfd_udp_main.bfd_main, b0)))
1448 b0->error = rt->errors[BFD_UDP_ERROR_NONE];
1449 next0 = BFD_UDP_ECHO_INPUT_NEXT_NORMAL;
1453 /* loop back the packet */
1454 b0->error = rt->errors[BFD_UDP_ERROR_NONE];
1457 vlib_node_increment_counter (vm, bfd_udp_echo6_input_node.index,
1458 BFD_UDP_ERROR_NONE, 1);
1462 vlib_node_increment_counter (vm, bfd_udp_echo4_input_node.index,
1463 BFD_UDP_ERROR_NONE, 1);
1465 next0 = BFD_UDP_ECHO_INPUT_NEXT_REPLY_REWRITE;
1472 vlib_increment_combined_counter (
1473 &bm->rx_echo_counter, vm->thread_index, bs->bs_idx, 1,
1474 vlib_buffer_length_in_chain (vm, b0));
1477 vlib_set_next_frame_buffer (vm, rt, next0, bi0);
1483 return f->n_vectors;
1487 bfd_udp_echo4_input (vlib_main_t * vm, vlib_node_runtime_t * rt,
1490 return bfd_udp_echo_input (vm, rt, f, 0);
1494 bfd_echo_input_format_trace (u8 * s, va_list * args)
1496 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1497 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1498 const bfd_udp_echo_input_trace_t *t =
1499 va_arg (*args, bfd_udp_echo_input_trace_t *);
1500 if (t->len > STRUCT_SIZE_OF (bfd_pkt_t, head))
1502 s = format (s, "BFD ECHO:\n");
1503 s = format (s, " data: %U", format_hexdump, t->data, t->len);
1510 * bfd input graph node declaration
1513 VLIB_REGISTER_NODE (bfd_udp_echo4_input_node, static) = {
1514 .function = bfd_udp_echo4_input,
1515 .name = "bfd-udp-echo4-input",
1516 .vector_size = sizeof (u32),
1517 .type = VLIB_NODE_TYPE_INTERNAL,
1519 .n_errors = BFD_UDP_N_ERROR,
1520 .error_counters = bfd_udp_error_counters,
1522 .format_trace = bfd_echo_input_format_trace,
1524 .n_next_nodes = BFD_UDP_ECHO_INPUT_N_NEXT,
1527 [BFD_UDP_ECHO_INPUT_NEXT_NORMAL] = "error-drop",
1528 [BFD_UDP_ECHO_INPUT_NEXT_REPLY_ARP] = "ip4-arp",
1529 [BFD_UDP_ECHO_INPUT_NEXT_REPLY_REWRITE] = "ip4-lookup",
1535 bfd_udp_echo6_input (vlib_main_t * vm, vlib_node_runtime_t * rt,
1538 return bfd_udp_echo_input (vm, rt, f, 1);
1542 VLIB_REGISTER_NODE (bfd_udp_echo6_input_node, static) = {
1543 .function = bfd_udp_echo6_input,
1544 .name = "bfd-udp-echo6-input",
1545 .vector_size = sizeof (u32),
1546 .type = VLIB_NODE_TYPE_INTERNAL,
1548 .n_errors = BFD_UDP_N_ERROR,
1549 .error_counters = bfd_udp_error_counters,
1551 .format_trace = bfd_echo_input_format_trace,
1553 .n_next_nodes = BFD_UDP_ECHO_INPUT_N_NEXT,
1556 [BFD_UDP_ECHO_INPUT_NEXT_NORMAL] = "error-drop",
1557 [BFD_UDP_ECHO_INPUT_NEXT_REPLY_ARP] = "ip6-discover-neighbor",
1558 [BFD_UDP_ECHO_INPUT_NEXT_REPLY_REWRITE] = "ip6-lookup",
1564 static clib_error_t *
1565 bfd_udp_sw_if_add_del (CLIB_UNUSED (vnet_main_t *vnm), u32 sw_if_index,
1568 u32 *to_be_freed = NULL;
1569 bfd_udp_main_t *bum = &bfd_udp_main;
1570 BFD_DBG ("sw_if_add_del called, sw_if_index=%u, is_create=%u", sw_if_index,
1575 pool_foreach (bs, bum->bfd_main->sessions)
1577 if (bs->transport != BFD_TRANSPORT_UDP4 &&
1578 bs->transport != BFD_TRANSPORT_UDP6)
1582 if (bs->udp.key.sw_if_index != sw_if_index)
1586 vec_add1 (to_be_freed, bs->bs_idx);
1590 vec_foreach (bs_idx, to_be_freed)
1592 bfd_session_t *bs = pool_elt_at_index (bum->bfd_main->sessions, *bs_idx);
1593 vlib_log_notice (bum->log_class,
1594 "removal of sw_if_index=%u forces removal of bfd "
1595 "session with bs_idx=%u",
1596 sw_if_index, bs->bs_idx);
1597 bfd_session_set_flags (vlib_get_main (), bs, 0);
1598 bfd_udp_del_session_internal (vlib_get_main (), bs);
1603 VNET_SW_INTERFACE_ADD_DEL_FUNCTION (bfd_udp_sw_if_add_del);
1606 bfd_udp_stats_init (bfd_udp_main_t *bum)
1608 const char *name4 = "/bfd/udp4/sessions";
1609 bum->udp4_sessions_count_stat_seg_entry = vlib_stats_add_gauge ("%s", name4);
1611 vlib_stats_set_gauge (bum->udp4_sessions_count_stat_seg_entry, 0);
1612 if (~0 == bum->udp4_sessions_count_stat_seg_entry)
1614 return clib_error_return (
1615 0, "Could not create stat segment entry for %s", name4);
1617 const char *name6 = "/bfd/udp6/sessions";
1618 bum->udp6_sessions_count_stat_seg_entry = vlib_stats_add_gauge ("%s", name6);
1620 vlib_stats_set_gauge (bum->udp6_sessions_count_stat_seg_entry, 0);
1621 if (~0 == bum->udp6_sessions_count_stat_seg_entry)
1623 return clib_error_return (
1624 0, "Could not create stat segment entry for %s", name6);
1633 static clib_error_t *
1634 bfd_udp_init (vlib_main_t * vm)
1636 bfd_udp_main.udp4_sessions_count = 0;
1637 bfd_udp_main.udp6_sessions_count = 0;
1638 mhash_init (&bfd_udp_main.bfd_session_idx_by_bfd_key, sizeof (uword),
1639 sizeof (bfd_udp_key_t));
1640 bfd_udp_main.bfd_main = &bfd_main;
1641 bfd_udp_main.vnet_main = vnet_get_main ();
1642 vlib_node_t *node = vlib_get_node_by_name (vm, (u8 *) "ip4-arp");
1644 bfd_udp_main.ip4_arp_idx = node->index;
1645 node = vlib_get_node_by_name (vm, (u8 *) "ip6-discover-neighbor");
1647 bfd_udp_main.ip6_ndp_idx = node->index;
1648 node = vlib_get_node_by_name (vm, (u8 *) "ip4-rewrite");
1650 bfd_udp_main.ip4_rewrite_idx = node->index;
1651 node = vlib_get_node_by_name (vm, (u8 *) "ip6-rewrite");
1653 bfd_udp_main.ip6_rewrite_idx = node->index;
1654 node = vlib_get_node_by_name (vm, (u8 *) "ip4-midchain");
1656 bfd_udp_main.ip4_midchain_idx = node->index;
1657 node = vlib_get_node_by_name (vm, (u8 *) "ip6-midchain");
1659 bfd_udp_main.ip6_midchain_idx = node->index;
1661 bfd_udp_stats_init (&bfd_udp_main);
1663 bfd_udp_main.log_class = vlib_log_register_class ("bfd", "udp");
1664 vlib_log_debug (bfd_udp_main.log_class, "initialized");
1668 VLIB_INIT_FUNCTION (bfd_udp_init);
1671 * fd.io coding-style-patch-verification: ON
1674 * eval: (c-set-style "gnu")