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;
58 vlib_log_class_t log_class;
59 /* number of active udp4 sessions */
60 u32 udp4_sessions_count;
61 u32 udp4_sessions_count_stat_seg_entry;
62 /* number of active udp6 sessions */
63 u32 udp6_sessions_count;
64 u32 udp6_sessions_count_stat_seg_entry;
67 static vlib_node_registration_t bfd_udp4_input_node;
68 static vlib_node_registration_t bfd_udp6_input_node;
69 static vlib_node_registration_t bfd_udp_echo4_input_node;
70 static vlib_node_registration_t bfd_udp_echo6_input_node;
72 bfd_udp_main_t bfd_udp_main;
75 bfd_udp_update_stat_segment_entry (u32 entry, u64 value)
77 vlib_stats_segment_lock ();
78 vlib_stats_set_gauge (entry, value);
79 vlib_stats_segment_unlock ();
83 bfd_udp_set_echo_source (u32 sw_if_index)
85 vnet_sw_interface_t *sw_if =
86 vnet_get_sw_interface_or_null (bfd_udp_main.vnet_main, sw_if_index);
89 bfd_udp_main.echo_source_sw_if_index = sw_if_index;
90 bfd_udp_main.echo_source_is_set = 1;
93 return VNET_API_ERROR_BFD_ENOENT;
97 bfd_udp_del_echo_source ()
99 bfd_udp_main.echo_source_sw_if_index = ~0;
100 bfd_udp_main.echo_source_is_set = 0;
105 bfd_udp_is_echo_available (bfd_transport_e transport)
107 if (!bfd_udp_main.echo_source_is_set)
109 BFD_DBG ("UDP echo source not set - echo not available");
113 * for the echo to work, we need a loopback interface with at least one
114 * address with netmask length at most 31 (ip4) or 127 (ip6) so that we can
115 * pick an unused address from that subnet
117 vnet_sw_interface_t *sw_if =
118 vnet_get_sw_interface_or_null (bfd_udp_main.vnet_main,
119 bfd_udp_main.echo_source_sw_if_index);
120 if (sw_if && sw_if->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
122 if (BFD_TRANSPORT_UDP4 == transport)
124 ip4_main_t *im = &ip4_main;
125 ip_interface_address_t *ia = NULL;
127 foreach_ip_interface_address (&im->lookup_main, ia,
128 bfd_udp_main.echo_source_sw_if_index,
129 0 /* honor unnumbered */, ({
130 if (ia->address_length <= 31)
137 else if (BFD_TRANSPORT_UDP6 == transport)
139 ip6_main_t *im = &ip6_main;
140 ip_interface_address_t *ia = NULL;
142 foreach_ip_interface_address (&im->lookup_main, ia,
143 bfd_udp_main.echo_source_sw_if_index,
144 0 /* honor unnumbered */, ({
145 if (ia->address_length <= 127)
153 BFD_DBG ("No usable IP address for UDP echo - echo not available");
158 bfd_udp_bs_idx_to_sport (u32 bs_idx)
160 /* The source port MUST be in the range 49152 through 65535. The same UDP
161 * source port number MUST be used for all BFD Control packets associated
162 * with a particular session. The source port number SHOULD be unique among
163 * all BFD sessions on the system. If more than 16384 BFD sessions are
164 * simultaneously active, UDP source port numbers MAY be reused on
165 * multiple sessions, but the number of distinct uses of the same UDP
166 * source port number SHOULD be minimized.
168 return 49152 + bs_idx % (65535 - 49152 + 1);
172 bfd_udp_get_echo_src_ip4 (ip4_address_t * addr)
174 if (!bfd_udp_main.echo_source_is_set)
176 BFD_ERR ("cannot find ip4 address, echo source not set");
179 ip_interface_address_t *ia = NULL;
180 ip4_main_t *im = &ip4_main;
183 foreach_ip_interface_address (
184 &im->lookup_main, ia, bfd_udp_main.echo_source_sw_if_index,
185 0 /* honor unnumbered */, ({
187 ip_interface_address_get_address (&im->lookup_main, ia);
188 if (ia->address_length <= 31)
190 addr->as_u32 = clib_host_to_net_u32 (x->as_u32);
192 * flip the last bit to get a different address, might be network,
196 addr->as_u32 = clib_net_to_host_u32 (addr->as_u32);
201 BFD_ERR ("cannot find ip4 address, no usable address found");
206 bfd_udp_get_echo_src_ip6 (ip6_address_t * addr)
208 if (!bfd_udp_main.echo_source_is_set)
210 BFD_ERR ("cannot find ip6 address, echo source not set");
213 ip_interface_address_t *ia = NULL;
214 ip6_main_t *im = &ip6_main;
217 foreach_ip_interface_address (
218 &im->lookup_main, ia, bfd_udp_main.echo_source_sw_if_index,
219 0 /* honor unnumbered */, ({
221 ip_interface_address_get_address (&im->lookup_main, ia);
222 if (ia->address_length <= 127)
225 addr->as_u8[15] ^= 1; /* flip the last bit of the address */
230 BFD_ERR ("cannot find ip6 address, no usable address found");
235 bfd_udp_get_echo_source (int *is_set, u32 * sw_if_index,
236 int *have_usable_ip4, ip4_address_t * ip4,
237 int *have_usable_ip6, ip6_address_t * ip6)
239 if (bfd_udp_main.echo_source_is_set)
242 *sw_if_index = bfd_udp_main.echo_source_sw_if_index;
243 *have_usable_ip4 = bfd_udp_get_echo_src_ip4 (ip4);
244 *have_usable_ip6 = bfd_udp_get_echo_src_ip6 (ip6);
253 bfd_add_udp4_transport (vlib_main_t * vm, u32 bi, const bfd_session_t * bs,
256 const bfd_udp_session_t *bus = &bs->udp;
257 const bfd_udp_key_t *key = &bus->key;
258 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
260 b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
261 vnet_buffer (b)->ip.adj_index[VLIB_RX] = bus->adj_index;
262 vnet_buffer (b)->ip.adj_index[VLIB_TX] = bus->adj_index;
263 vnet_buffer (b)->sw_if_index[VLIB_RX] = 0;
264 vnet_buffer (b)->sw_if_index[VLIB_TX] = ~0;
270 ip4_udp_headers *headers = NULL;
271 vlib_buffer_advance (b, -sizeof (*headers));
272 headers = vlib_buffer_get_current (b);
273 clib_memset (headers, 0, sizeof (*headers));
274 headers->ip4.ip_version_and_header_length = 0x45;
275 headers->ip4.ttl = 255;
276 headers->ip4.protocol = IP_PROTOCOL_UDP;
277 headers->udp.src_port =
278 clib_host_to_net_u16 (bfd_udp_bs_idx_to_sport (bs->bs_idx));
282 if (!(rv = bfd_udp_get_echo_src_ip4 (&headers->ip4.src_address)))
286 headers->ip4.dst_address.as_u32 = key->local_addr.ip4.as_u32;
287 headers->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_bfd_echo4);
291 headers->ip4.src_address.as_u32 = key->local_addr.ip4.as_u32;
292 headers->ip4.dst_address.as_u32 = key->peer_addr.ip4.as_u32;
293 headers->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_bfd4);
296 /* fix ip length, checksum and udp length */
297 const u16 ip_length = vlib_buffer_length_in_chain (vm, b);
299 headers->ip4.length = clib_host_to_net_u16 (ip_length);
300 headers->ip4.checksum = ip4_header_checksum (&headers->ip4);
302 const u16 udp_length = ip_length - (sizeof (headers->ip4));
303 headers->udp.length = clib_host_to_net_u16 (udp_length);
308 bfd_add_udp6_transport (vlib_main_t * vm, u32 bi, const bfd_session_t * bs,
311 const bfd_udp_session_t *bus = &bs->udp;
312 const bfd_udp_key_t *key = &bus->key;
313 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
315 b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
316 vnet_buffer (b)->ip.adj_index[VLIB_RX] = bus->adj_index;
317 vnet_buffer (b)->ip.adj_index[VLIB_TX] = bus->adj_index;
318 vnet_buffer (b)->sw_if_index[VLIB_RX] = 0;
319 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
325 ip6_udp_headers *headers = NULL;
326 vlib_buffer_advance (b, -sizeof (*headers));
327 headers = vlib_buffer_get_current (b);
328 clib_memset (headers, 0, sizeof (*headers));
329 headers->ip6.ip_version_traffic_class_and_flow_label =
330 clib_host_to_net_u32 (0x6 << 28);
331 headers->ip6.hop_limit = 255;
332 headers->ip6.protocol = IP_PROTOCOL_UDP;
333 headers->udp.src_port =
334 clib_host_to_net_u16 (bfd_udp_bs_idx_to_sport (bs->bs_idx));
338 if (!(rv = bfd_udp_get_echo_src_ip6 (&headers->ip6.src_address)))
342 clib_memcpy_fast (&headers->ip6.dst_address, &key->local_addr.ip6,
343 sizeof (headers->ip6.dst_address));
345 headers->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_bfd_echo6);
349 clib_memcpy_fast (&headers->ip6.src_address, &key->local_addr.ip6,
350 sizeof (headers->ip6.src_address));
351 clib_memcpy_fast (&headers->ip6.dst_address, &key->peer_addr.ip6,
352 sizeof (headers->ip6.dst_address));
353 headers->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_bfd6);
356 /* fix ip payload length and udp length */
357 const u16 udp_length =
358 vlib_buffer_length_in_chain (vm, b) - (sizeof (headers->ip6));
359 headers->udp.length = clib_host_to_net_u16 (udp_length);
360 headers->ip6.payload_length = headers->udp.length;
362 /* IPv6 UDP checksum is mandatory */
364 headers->udp.checksum =
365 ip6_tcp_udp_icmp_compute_checksum (vm, b, &headers->ip6, &bogus);
367 if (headers->udp.checksum == 0)
369 headers->udp.checksum = 0xffff;
375 bfd_create_frame_to_next_node (vlib_main_t *vm, vlib_node_runtime_t *rt,
376 u32 bi, const bfd_session_t *bs, u32 next,
377 vlib_combined_counter_main_t *tx_counter)
379 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
380 vlib_node_t *from_node = vlib_get_node (vm, rt->node_index);
381 ASSERT (next < vec_len (from_node->next_nodes));
382 u32 to_node_index = from_node->next_nodes[next];
383 vlib_frame_t *f = vlib_get_frame_to_node (vm, to_node_index);
384 u32 *to_next = vlib_frame_vector_args (f);
387 if (b->flags & VLIB_BUFFER_IS_TRACED)
389 f->frame_flags |= VLIB_NODE_FLAG_TRACE;
391 vlib_put_frame_to_node (vm, to_node_index, f);
392 vlib_increment_combined_counter (tx_counter, vm->thread_index, bs->bs_idx, 1,
393 vlib_buffer_length_in_chain (vm, b));
397 bfd_udp_calc_next_node (const struct bfd_session_s *bs, u32 * next_node)
399 vnet_main_t *vnm = vnet_get_main ();
400 const bfd_udp_session_t *bus = &bs->udp;
401 ip_adjacency_t *adj = adj_get (bus->adj_index);
403 /* don't try to send the buffer if the interface is not up */
404 if (!vnet_sw_interface_is_up (vnm, bus->key.sw_if_index))
407 switch (adj->lookup_next_index)
409 case IP_LOOKUP_NEXT_ARP:
410 switch (bs->transport)
412 case BFD_TRANSPORT_UDP4:
413 *next_node = BFD_TX_IP4_ARP;
415 case BFD_TRANSPORT_UDP6:
416 *next_node = BFD_TX_IP6_NDP;
420 case IP_LOOKUP_NEXT_REWRITE:
421 switch (bs->transport)
423 case BFD_TRANSPORT_UDP4:
424 *next_node = BFD_TX_IP4_REWRITE;
426 case BFD_TRANSPORT_UDP6:
427 *next_node = BFD_TX_IP6_REWRITE;
431 case IP_LOOKUP_NEXT_MIDCHAIN:
432 switch (bs->transport)
434 case BFD_TRANSPORT_UDP4:
435 *next_node = BFD_TX_IP4_MIDCHAIN;
437 case BFD_TRANSPORT_UDP6:
438 *next_node = BFD_TX_IP6_MIDCHAIN;
450 bfd_transport_udp4 (vlib_main_t *vm, vlib_node_runtime_t *rt, u32 bi,
451 const struct bfd_session_s *bs, int is_echo)
454 int rv = bfd_udp_calc_next_node (bs, &next_node);
455 bfd_main_t *bm = bfd_udp_main.bfd_main;
458 bfd_create_frame_to_next_node (vm, rt, bi, bs, next_node,
459 is_echo ? &bm->tx_echo_counter :
466 bfd_transport_udp6 (vlib_main_t *vm, vlib_node_runtime_t *rt, u32 bi,
467 const struct bfd_session_s *bs, int is_echo)
470 int rv = bfd_udp_calc_next_node (bs, &next_node);
471 bfd_main_t *bm = bfd_udp_main.bfd_main;
474 bfd_create_frame_to_next_node (vm, rt, bi, bs, next_node,
475 is_echo ? &bm->tx_echo_counter :
481 static bfd_session_t *
482 bfd_lookup_session (bfd_udp_main_t * bum, const bfd_udp_key_t * key)
484 uword *p = mhash_get (&bum->bfd_session_idx_by_bfd_key, key);
487 return bfd_find_session_by_idx (bum->bfd_main, *p);
493 bfd_udp_key_init (bfd_udp_key_t * key, u32 sw_if_index,
494 const ip46_address_t * local_addr,
495 const ip46_address_t * peer_addr)
497 clib_memset (key, 0, sizeof (*key));
498 key->sw_if_index = sw_if_index;
499 key->local_addr.as_u64[0] = local_addr->as_u64[0];
500 key->local_addr.as_u64[1] = local_addr->as_u64[1];
501 key->peer_addr.as_u64[0] = peer_addr->as_u64[0];
502 key->peer_addr.as_u64[1] = peer_addr->as_u64[1];
505 static vnet_api_error_t
506 bfd_udp_add_session_internal (vlib_main_t * vm, bfd_udp_main_t * bum,
507 u32 sw_if_index, u32 desired_min_tx_usec,
508 u32 required_min_rx_usec, u8 detect_mult,
509 const ip46_address_t * local_addr,
510 const ip46_address_t * peer_addr,
511 bfd_session_t ** bs_out)
513 /* get a pool entry and if we end up not needing it, give it back */
514 bfd_transport_e t = BFD_TRANSPORT_UDP4;
515 if (!ip46_address_is_ip4 (local_addr))
517 t = BFD_TRANSPORT_UDP6;
519 bfd_session_t *bs = bfd_get_session (bum->bfd_main, t);
522 return VNET_API_ERROR_BFD_EAGAIN;
524 bfd_udp_session_t *bus = &bs->udp;
525 clib_memset (bus, 0, sizeof (*bus));
526 bus->adj_index = ADJ_INDEX_INVALID;
527 bfd_udp_key_t *key = &bus->key;
528 bfd_udp_key_init (key, sw_if_index, local_addr, peer_addr);
529 const bfd_session_t *tmp = bfd_lookup_session (bum, key);
532 vlib_log_err (bum->log_class,
533 "duplicate bfd-udp session, existing bs_idx=%d",
535 bfd_put_session (bum->bfd_main, bs);
536 return VNET_API_ERROR_BFD_EEXIST;
538 mhash_set (&bum->bfd_session_idx_by_bfd_key, key, bs->bs_idx, NULL);
539 BFD_DBG ("session created, bs_idx=%u, sw_if_index=%d, local=%U, peer=%U",
540 bs->bs_idx, key->sw_if_index, format_ip46_address,
541 &key->local_addr, IP46_TYPE_ANY, format_ip46_address,
542 &key->peer_addr, IP46_TYPE_ANY);
543 vlib_log_info (bum->log_class, "create BFD session: %U",
544 format_bfd_session, bs);
545 const ip46_address_t *peer =
546 (vnet_sw_interface_is_p2p (vnet_get_main (), key->sw_if_index) ?
549 if (BFD_TRANSPORT_UDP4 == t)
551 bus->adj_index = adj_nbr_add_or_lock (FIB_PROTOCOL_IP4, VNET_LINK_IP4,
552 peer, key->sw_if_index);
553 BFD_DBG ("adj_nbr_add_or_lock(FIB_PROTOCOL_IP4, VNET_LINK_IP4, %U, %d) "
555 format_ip46_address, peer, IP46_TYPE_ANY, key->sw_if_index,
557 ++bum->udp4_sessions_count;
558 bfd_udp_update_stat_segment_entry (
559 bum->udp4_sessions_count_stat_seg_entry, bum->udp4_sessions_count);
560 if (1 == bum->udp4_sessions_count)
562 udp_register_dst_port (vm, UDP_DST_PORT_bfd4,
563 bfd_udp4_input_node.index, 1);
564 udp_register_dst_port (vm, UDP_DST_PORT_bfd_echo4,
565 bfd_udp_echo4_input_node.index, 1);
570 bus->adj_index = adj_nbr_add_or_lock (FIB_PROTOCOL_IP6, VNET_LINK_IP6,
571 peer, key->sw_if_index);
572 BFD_DBG ("adj_nbr_add_or_lock(FIB_PROTOCOL_IP6, VNET_LINK_IP6, %U, %d) "
574 format_ip46_address, peer, IP46_TYPE_ANY, key->sw_if_index,
576 ++bum->udp6_sessions_count;
577 bfd_udp_update_stat_segment_entry (
578 bum->udp6_sessions_count_stat_seg_entry, bum->udp6_sessions_count);
579 if (1 == bum->udp6_sessions_count)
581 udp_register_dst_port (vm, UDP_DST_PORT_bfd6,
582 bfd_udp6_input_node.index, 0);
583 udp_register_dst_port (vm, UDP_DST_PORT_bfd_echo6,
584 bfd_udp_echo6_input_node.index, 0);
588 return bfd_session_set_params (bum->bfd_main, bs, desired_min_tx_usec,
589 required_min_rx_usec, detect_mult);
592 static vnet_api_error_t
593 bfd_udp_validate_api_input (u32 sw_if_index,
594 const ip46_address_t * local_addr,
595 const ip46_address_t * peer_addr)
597 bfd_udp_main_t *bum = &bfd_udp_main;
598 vnet_sw_interface_t *sw_if =
599 vnet_get_sw_interface_or_null (bfd_udp_main.vnet_main, sw_if_index);
602 vlib_log_err (bum->log_class,
603 "got NULL sw_if when getting interface by index %u",
605 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
607 if (ip46_address_is_ip4 (local_addr))
609 if (!ip46_address_is_ip4 (peer_addr))
611 vlib_log_err (bum->log_class,
612 "IP family mismatch (local is ipv4, peer is ipv6)");
613 return VNET_API_ERROR_INVALID_ARGUMENT;
618 if (ip46_address_is_ip4 (peer_addr))
620 vlib_log_err (bum->log_class,
621 "IP family mismatch (local is ipv6, peer is ipv4)");
622 return VNET_API_ERROR_INVALID_ARGUMENT;
629 static vnet_api_error_t
630 bfd_udp_find_session_by_api_input (u32 sw_if_index,
631 const ip46_address_t * local_addr,
632 const ip46_address_t * peer_addr,
633 bfd_session_t ** bs_out)
635 vnet_api_error_t rv =
636 bfd_udp_validate_api_input (sw_if_index, local_addr, peer_addr);
639 bfd_udp_main_t *bum = &bfd_udp_main;
641 bfd_udp_key_init (&key, sw_if_index, local_addr, peer_addr);
642 bfd_session_t *bs = bfd_lookup_session (bum, &key);
649 vlib_log_err (bum->log_class,
650 "BFD session not found, sw_if_index=%u, local=%U, peer=%U",
651 sw_if_index, format_ip46_address, local_addr,
652 IP46_TYPE_ANY, format_ip46_address, peer_addr,
654 return VNET_API_ERROR_BFD_ENOENT;
660 static vnet_api_error_t
661 bfd_api_verify_common (u32 sw_if_index, u32 desired_min_tx_usec,
662 u8 detect_mult, const ip46_address_t *local_addr,
663 const ip46_address_t *peer_addr)
665 bfd_udp_main_t *bum = &bfd_udp_main;
666 vnet_api_error_t rv =
667 bfd_udp_validate_api_input (sw_if_index, local_addr, peer_addr);
674 vlib_log_err (bum->log_class, "detect_mult < 1");
675 return VNET_API_ERROR_INVALID_ARGUMENT;
677 if (desired_min_tx_usec < 1)
679 vlib_log_err (bum->log_class, "desired_min_tx_usec < 1");
680 return VNET_API_ERROR_INVALID_ARGUMENT;
686 bfd_udp_del_session_internal (vlib_main_t * vm, bfd_session_t * bs)
688 bfd_udp_main_t *bum = &bfd_udp_main;
689 BFD_DBG ("free bfd-udp session, bs_idx=%d", bs->bs_idx);
690 bfd_session_stop (bum->bfd_main, bs);
691 mhash_unset (&bum->bfd_session_idx_by_bfd_key, &bs->udp.key, NULL);
692 adj_unlock (bs->udp.adj_index);
693 switch (bs->transport)
695 case BFD_TRANSPORT_UDP4:
696 --bum->udp4_sessions_count;
697 bfd_udp_update_stat_segment_entry (
698 bum->udp4_sessions_count_stat_seg_entry, bum->udp4_sessions_count);
699 if (!bum->udp4_sessions_count)
701 udp_unregister_dst_port (vm, UDP_DST_PORT_bfd4, 1);
702 udp_unregister_dst_port (vm, UDP_DST_PORT_bfd_echo4, 1);
705 case BFD_TRANSPORT_UDP6:
706 --bum->udp6_sessions_count;
707 bfd_udp_update_stat_segment_entry (
708 bum->udp6_sessions_count_stat_seg_entry, bum->udp6_sessions_count);
709 if (!bum->udp6_sessions_count)
711 udp_unregister_dst_port (vm, UDP_DST_PORT_bfd6, 0);
712 udp_unregister_dst_port (vm, UDP_DST_PORT_bfd_echo6, 0);
716 bfd_put_session (bum->bfd_main, bs);
719 static vnet_api_error_t
720 bfd_udp_add_and_start_session (u32 sw_if_index,
721 const ip46_address_t *local_addr,
722 const ip46_address_t *peer_addr,
723 u32 desired_min_tx_usec,
724 u32 required_min_rx_usec, u8 detect_mult,
725 u8 is_authenticated, u32 conf_key_id,
728 bfd_session_t *bs = NULL;
731 rv = bfd_udp_add_session_internal (
732 vlib_get_main (), &bfd_udp_main, sw_if_index, desired_min_tx_usec,
733 required_min_rx_usec, detect_mult, local_addr, peer_addr, &bs);
735 if (!rv && is_authenticated)
737 rv = bfd_auth_activate (bs, conf_key_id, bfd_key_id,
738 0 /* is not delayed */);
741 bfd_udp_del_session_internal (vlib_get_main (), bs);
746 bfd_session_start (bfd_udp_main.bfd_main, bs);
753 bfd_udp_add_session (u32 sw_if_index, const ip46_address_t * local_addr,
754 const ip46_address_t * peer_addr,
755 u32 desired_min_tx_usec, u32 required_min_rx_usec,
756 u8 detect_mult, u8 is_authenticated, u32 conf_key_id,
759 bfd_main_t *bm = &bfd_main;
762 vnet_api_error_t rv = bfd_api_verify_common (
763 sw_if_index, desired_min_tx_usec, detect_mult, local_addr, peer_addr);
766 rv = bfd_udp_add_and_start_session (
767 sw_if_index, local_addr, peer_addr, desired_min_tx_usec,
768 required_min_rx_usec, detect_mult, is_authenticated, conf_key_id,
776 bfd_udp_upd_session (u32 sw_if_index, const ip46_address_t *local_addr,
777 const ip46_address_t *peer_addr, u32 desired_min_tx_usec,
778 u32 required_min_rx_usec, u8 detect_mult,
779 u8 is_authenticated, u32 conf_key_id, u8 bfd_key_id)
781 bfd_main_t *bm = &bfd_main;
784 vnet_api_error_t rv = bfd_api_verify_common (
785 sw_if_index, desired_min_tx_usec, detect_mult, local_addr, peer_addr);
788 bfd_session_t *bs = NULL;
790 rv = bfd_udp_find_session_by_api_input (sw_if_index, local_addr,
792 if (VNET_API_ERROR_BFD_ENOENT == rv)
793 rv = bfd_udp_add_and_start_session (
794 sw_if_index, local_addr, peer_addr, desired_min_tx_usec,
795 required_min_rx_usec, detect_mult, is_authenticated, conf_key_id,
798 rv = bfd_session_set_params (bfd_udp_main.bfd_main, bs,
799 desired_min_tx_usec, required_min_rx_usec,
808 bfd_udp_mod_session (u32 sw_if_index, const ip46_address_t *local_addr,
809 const ip46_address_t *peer_addr, u32 desired_min_tx_usec,
810 u32 required_min_rx_usec, u8 detect_mult)
812 bfd_session_t *bs = NULL;
813 bfd_main_t *bm = &bfd_main;
814 vnet_api_error_t error;
816 vnet_api_error_t rv =
817 bfd_udp_find_session_by_api_input (sw_if_index, local_addr, peer_addr,
825 error = bfd_session_set_params (bfd_udp_main.bfd_main, bs,
826 desired_min_tx_usec, required_min_rx_usec,
833 bfd_udp_del_session (u32 sw_if_index,
834 const ip46_address_t * local_addr,
835 const ip46_address_t * peer_addr)
837 bfd_session_t *bs = NULL;
838 bfd_main_t *bm = &bfd_main;
840 vnet_api_error_t rv =
841 bfd_udp_find_session_by_api_input (sw_if_index, local_addr, peer_addr,
848 bfd_udp_del_session_internal (vlib_get_main (), bs);
854 bfd_udp_session_set_flags (vlib_main_t * vm, u32 sw_if_index,
855 const ip46_address_t * local_addr,
856 const ip46_address_t * peer_addr, u8 admin_up_down)
858 bfd_session_t *bs = NULL;
859 bfd_main_t *bm = &bfd_main;
861 vnet_api_error_t rv =
862 bfd_udp_find_session_by_api_input (sw_if_index, local_addr, peer_addr,
869 bfd_session_set_flags (vm, bs, admin_up_down);
875 bfd_udp_auth_activate (u32 sw_if_index,
876 const ip46_address_t * local_addr,
877 const ip46_address_t * peer_addr,
878 u32 conf_key_id, u8 key_id, u8 is_delayed)
880 bfd_main_t *bm = &bfd_main;
882 vnet_api_error_t error;
884 bfd_session_t *bs = NULL;
885 vnet_api_error_t rv =
886 bfd_udp_find_session_by_api_input (sw_if_index, local_addr, peer_addr,
893 error = bfd_auth_activate (bs, conf_key_id, key_id, is_delayed);
899 bfd_udp_auth_deactivate (u32 sw_if_index,
900 const ip46_address_t * local_addr,
901 const ip46_address_t * peer_addr, u8 is_delayed)
903 bfd_main_t *bm = &bfd_main;
904 vnet_api_error_t error;
906 bfd_session_t *bs = NULL;
907 vnet_api_error_t rv =
908 bfd_udp_find_session_by_api_input (sw_if_index, local_addr, peer_addr,
915 error = bfd_auth_deactivate (bs, is_delayed);
922 BFD_UDP_INPUT_NEXT_NORMAL,
923 BFD_UDP_INPUT_NEXT_REPLY_ARP,
924 BFD_UDP_INPUT_NEXT_REPLY_REWRITE,
925 BFD_UDP_INPUT_NEXT_REPLY_MIDCHAIN,
926 BFD_UDP_INPUT_N_NEXT,
927 } bfd_udp_input_next_t;
931 BFD_UDP_ECHO_INPUT_NEXT_NORMAL,
932 BFD_UDP_ECHO_INPUT_NEXT_REPLY_ARP,
933 BFD_UDP_ECHO_INPUT_NEXT_REPLY_REWRITE,
934 BFD_UDP_ECHO_INPUT_N_NEXT,
935 } bfd_udp_echo_input_next_t;
937 static_always_inline vl_counter_bfd_udp_enum_t
938 bfd_error_to_udp (bfd_error_t e)
940 /* The UDP error is a super set of the proto independent errors */
941 return ((vl_counter_bfd_udp_enum_t) e);
945 bfd_udp4_find_headers (vlib_buffer_t * b, ip4_header_t ** ip4,
948 /* sanity check first */
949 const i32 start = vnet_buffer (b)->l3_hdr_offset;
950 if (start < -(signed) sizeof (b->pre_data))
952 BFD_ERR ("Start of ip header is before pre_data, ignoring");
957 *ip4 = (ip4_header_t *) (b->data + start);
958 if ((u8 *) * ip4 > (u8 *) vlib_buffer_get_current (b))
960 BFD_ERR ("Start of ip header is beyond current data, ignoring");
965 *udp = (udp_header_t *) ((*ip4) + 1);
968 static vl_counter_bfd_udp_enum_t
969 bfd_udp4_verify_transport (const ip4_header_t *ip4, const udp_header_t *udp,
970 const bfd_session_t *bs)
972 const bfd_udp_session_t *bus = &bs->udp;
973 const bfd_udp_key_t *key = &bus->key;
974 if (ip4->src_address.as_u32 != key->peer_addr.ip4.as_u32)
976 BFD_ERR ("IPv4 src addr mismatch, got %U, expected %U",
977 format_ip4_address, ip4->src_address.as_u8, format_ip4_address,
978 key->peer_addr.ip4.as_u8);
979 return BFD_UDP_ERROR_SRC_MISMATCH;
981 if (ip4->dst_address.as_u32 != key->local_addr.ip4.as_u32)
983 BFD_ERR ("IPv4 dst addr mismatch, got %U, expected %U",
984 format_ip4_address, ip4->dst_address.as_u8, format_ip4_address,
985 key->local_addr.ip4.as_u8);
986 return BFD_UDP_ERROR_DST_MISMATCH;
988 const u8 expected_ttl = 255;
989 if (ip4->ttl != expected_ttl)
991 BFD_ERR ("IPv4 unexpected TTL value %u, expected %u", ip4->ttl,
993 return BFD_UDP_ERROR_TTL;
995 if (clib_net_to_host_u16 (udp->src_port) < 49152)
997 BFD_ERR ("Invalid UDP src port %u, out of range <49152,65535>",
1000 return BFD_UDP_ERROR_NONE;
1010 bfd_rpc_update_session (vlib_main_t *vm, u32 bs_idx, const bfd_pkt_t *pkt)
1012 bfd_main_t *bm = &bfd_main;
1015 err = bfd_consume_pkt (vm, bm, pkt, bs_idx);
1021 static vl_counter_bfd_udp_enum_t
1022 bfd_udp4_scan (vlib_main_t *vm, vlib_buffer_t *b, bfd_session_t **bs_out)
1024 const bfd_pkt_t *pkt = vlib_buffer_get_current (b);
1025 if (sizeof (*pkt) > b->current_length)
1028 ("Payload size %d too small to hold bfd packet of minimum size %d",
1029 b->current_length, sizeof (*pkt));
1030 return BFD_UDP_ERROR_BAD;
1034 bfd_udp4_find_headers (b, &ip4, &udp);
1037 BFD_ERR ("Couldn't find ip4 or udp header");
1038 return BFD_UDP_ERROR_BAD;
1040 const u32 udp_payload_length = udp->length - sizeof (*udp);
1041 if (pkt->head.length > udp_payload_length)
1044 ("BFD packet length is larger than udp payload length (%u > %u)",
1045 pkt->head.length, udp_payload_length);
1046 return BFD_UDP_ERROR_LENGTH;
1048 vl_counter_bfd_udp_enum_t err;
1049 if (BFD_UDP_ERROR_NONE !=
1050 (err = bfd_error_to_udp (bfd_verify_pkt_common (pkt))))
1054 bfd_session_t *bs = NULL;
1057 BFD_DBG ("Looking up BFD session using discriminator %u",
1059 bs = bfd_find_session_by_disc (bfd_udp_main.bfd_main, pkt->your_disc);
1064 clib_memset (&key, 0, sizeof (key));
1065 key.sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
1066 key.local_addr.ip4.as_u32 = ip4->dst_address.as_u32;
1067 key.peer_addr.ip4.as_u32 = ip4->src_address.as_u32;
1068 BFD_DBG ("Looking up BFD session using key (sw_if_index=%u, local=%U, "
1070 key.sw_if_index, format_ip4_address, key.local_addr.ip4.as_u8,
1071 format_ip4_address, key.peer_addr.ip4.as_u8);
1072 bs = bfd_lookup_session (&bfd_udp_main, &key);
1076 BFD_ERR ("BFD session lookup failed - no session matches BFD pkt");
1077 return BFD_UDP_ERROR_NO_SESSION;
1079 BFD_DBG ("BFD session found, bs_idx=%u", bs->bs_idx);
1080 if (!bfd_verify_pkt_auth (vm, pkt, b->current_length, bs))
1082 BFD_ERR ("Packet verification failed, dropping packet");
1083 return BFD_UDP_ERROR_FAILED_VERIFICATION;
1085 if (BFD_UDP_ERROR_NONE != (err = bfd_udp4_verify_transport (ip4, udp, bs)))
1089 err = bfd_error_to_udp (bfd_rpc_update_session (vm, bs->bs_idx, pkt));
1095 bfd_udp6_find_headers (vlib_buffer_t * b, ip6_header_t ** ip6,
1096 udp_header_t ** udp)
1098 /* sanity check first */
1099 const i32 start = vnet_buffer (b)->l3_hdr_offset;
1100 if (start < -(signed) sizeof (b->pre_data))
1102 BFD_ERR ("Start of ip header is before pre_data, ignoring");
1107 *ip6 = (ip6_header_t *) (b->data + start);
1108 if ((u8 *) * ip6 > (u8 *) vlib_buffer_get_current (b))
1110 BFD_ERR ("Start of ip header is beyond current data, ignoring");
1115 if ((*ip6)->protocol != IP_PROTOCOL_UDP)
1117 BFD_ERR ("Unexpected protocol in IPv6 header '%u', expected '%u' (== "
1118 "IP_PROTOCOL_UDP)", (*ip6)->protocol, IP_PROTOCOL_UDP);
1123 *udp = (udp_header_t *) ((*ip6) + 1);
1126 static vl_counter_bfd_udp_enum_t
1127 bfd_udp6_verify_transport (const ip6_header_t *ip6, const udp_header_t *udp,
1128 const bfd_session_t *bs)
1130 const bfd_udp_session_t *bus = &bs->udp;
1131 const bfd_udp_key_t *key = &bus->key;
1132 if (ip6->src_address.as_u64[0] != key->peer_addr.ip6.as_u64[0] &&
1133 ip6->src_address.as_u64[1] != key->peer_addr.ip6.as_u64[1])
1135 BFD_ERR ("IP src addr mismatch, got %U, expected %U",
1136 format_ip6_address, ip6, format_ip6_address,
1137 &key->peer_addr.ip6);
1138 return BFD_UDP_ERROR_SRC_MISMATCH;
1140 if (ip6->dst_address.as_u64[0] != key->local_addr.ip6.as_u64[0] &&
1141 ip6->dst_address.as_u64[1] != key->local_addr.ip6.as_u64[1])
1143 BFD_ERR ("IP dst addr mismatch, got %U, expected %U",
1144 format_ip6_address, ip6, format_ip6_address,
1145 &key->local_addr.ip6);
1146 return BFD_UDP_ERROR_DST_MISMATCH;
1148 const u8 expected_hop_limit = 255;
1149 if (ip6->hop_limit != expected_hop_limit)
1151 BFD_ERR ("IPv6 unexpected hop-limit value %u, expected %u",
1152 ip6->hop_limit, expected_hop_limit);
1153 return BFD_UDP_ERROR_TTL;
1155 if (clib_net_to_host_u16 (udp->src_port) < 49152)
1157 BFD_ERR ("Invalid UDP src port %u, out of range <49152,65535>",
1160 return BFD_UDP_ERROR_NONE;
1163 static vl_counter_bfd_udp_enum_t
1164 bfd_udp6_scan (vlib_main_t *vm, vlib_buffer_t *b, bfd_session_t **bs_out)
1166 const bfd_pkt_t *pkt = vlib_buffer_get_current (b);
1167 if (sizeof (*pkt) > b->current_length)
1170 ("Payload size %d too small to hold bfd packet of minimum size %d",
1171 b->current_length, sizeof (*pkt));
1172 return BFD_UDP_ERROR_BAD;
1176 bfd_udp6_find_headers (b, &ip6, &udp);
1179 BFD_ERR ("Couldn't find ip6 or udp header");
1180 return BFD_UDP_ERROR_BAD;
1182 const u32 udp_payload_length = udp->length - sizeof (*udp);
1183 if (pkt->head.length > udp_payload_length)
1186 ("BFD packet length is larger than udp payload length (%u > %u)",
1187 pkt->head.length, udp_payload_length);
1188 return BFD_UDP_ERROR_BAD;
1190 vl_counter_bfd_udp_enum_t err;
1191 if (BFD_UDP_ERROR_NONE !=
1192 (err = bfd_error_to_udp (bfd_verify_pkt_common (pkt))))
1196 bfd_session_t *bs = NULL;
1199 BFD_DBG ("Looking up BFD session using discriminator %u",
1201 bs = bfd_find_session_by_disc (bfd_udp_main.bfd_main, pkt->your_disc);
1206 clib_memset (&key, 0, sizeof (key));
1207 key.sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
1208 key.local_addr.ip6.as_u64[0] = ip6->dst_address.as_u64[0];
1209 key.local_addr.ip6.as_u64[1] = ip6->dst_address.as_u64[1];
1210 key.peer_addr.ip6.as_u64[0] = ip6->src_address.as_u64[0];
1211 key.peer_addr.ip6.as_u64[1] = ip6->src_address.as_u64[1];
1212 BFD_DBG ("Looking up BFD session using key (sw_if_index=%u, local=%U, "
1214 key.sw_if_index, format_ip6_address, &key.local_addr,
1215 format_ip6_address, &key.peer_addr);
1216 bs = bfd_lookup_session (&bfd_udp_main, &key);
1220 BFD_ERR ("BFD session lookup failed - no session matches BFD pkt");
1221 return BFD_UDP_ERROR_NO_SESSION;
1223 BFD_DBG ("BFD session found, bs_idx=%u", bs->bs_idx);
1224 if (!bfd_verify_pkt_auth (vm, pkt, b->current_length, bs))
1226 BFD_ERR ("Packet verification failed, dropping packet");
1227 return BFD_UDP_ERROR_FAILED_VERIFICATION;
1229 if (BFD_UDP_ERROR_NONE != (err = bfd_udp6_verify_transport (ip6, udp, bs)))
1233 err = bfd_error_to_udp (bfd_rpc_update_session (vm, bs->bs_idx, pkt));
1239 * Process a frame of bfd packets
1240 * Expect 1 packet / frame
1243 bfd_udp_input (vlib_main_t * vm, vlib_node_runtime_t * rt,
1244 vlib_frame_t * f, int is_ipv6)
1246 u32 n_left_from, *from;
1247 bfd_input_trace_t *t0;
1248 bfd_main_t *bm = &bfd_main;
1250 from = vlib_frame_vector_args (f); /* array of buffer indices */
1251 n_left_from = f->n_vectors; /* number of buffer indices */
1253 while (n_left_from > 0)
1260 b0 = vlib_get_buffer (vm, bi0);
1262 bfd_session_t *bs = NULL;
1264 /* If this pkt is traced, snapshot the data */
1265 if (b0->flags & VLIB_BUFFER_IS_TRACED)
1268 t0 = vlib_add_trace (vm, rt, b0, sizeof (*t0));
1269 len = (b0->current_length < sizeof (t0->data)) ? b0->current_length
1270 : sizeof (t0->data);
1272 clib_memcpy_fast (t0->data, vlib_buffer_get_current (b0), len);
1275 /* scan this bfd pkt. error0 is the counter index to bmp */
1279 error0 = bfd_udp6_scan (vm, b0, &bs);
1283 error0 = bfd_udp4_scan (vm, b0, &bs);
1285 b0->error = rt->errors[error0];
1287 next0 = BFD_UDP_INPUT_NEXT_NORMAL;
1288 if (BFD_UDP_ERROR_NONE == error0)
1290 vlib_increment_combined_counter (
1291 &bm->rx_counter, vm->thread_index, bs->bs_idx, 1,
1292 vlib_buffer_length_in_chain (vm, b0));
1294 * if everything went fine, check for poll bit, if present, re-use
1295 * the buffer and based on (now updated) session parameters, send
1296 * the final packet back
1298 const bfd_pkt_t *pkt = vlib_buffer_get_current (b0);
1299 if (bfd_pkt_get_poll (pkt))
1301 b0->current_data = 0;
1302 b0->current_length = 0;
1303 bfd_init_final_control_frame (vm, b0, bs);
1306 vlib_node_increment_counter (vm, bfd_udp6_input_node.index,
1311 vlib_node_increment_counter (vm, bfd_udp4_input_node.index,
1314 const bfd_udp_session_t *bus = &bs->udp;
1315 ip_adjacency_t *adj = adj_get (bus->adj_index);
1316 switch (adj->lookup_next_index)
1318 case IP_LOOKUP_NEXT_ARP:
1319 next0 = BFD_UDP_INPUT_NEXT_REPLY_ARP;
1321 case IP_LOOKUP_NEXT_REWRITE:
1322 next0 = BFD_UDP_INPUT_NEXT_REPLY_REWRITE;
1324 case IP_LOOKUP_NEXT_MIDCHAIN:
1325 next0 = BFD_UDP_INPUT_NEXT_REPLY_MIDCHAIN;
1334 vlib_set_next_frame_buffer (vm, rt, next0, bi0);
1340 return f->n_vectors;
1344 bfd_udp4_input (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
1346 return bfd_udp_input (vm, rt, f, 0);
1350 * bfd input graph node declaration
1353 VLIB_REGISTER_NODE (bfd_udp4_input_node, static) = {
1354 .function = bfd_udp4_input,
1355 .name = "bfd-udp4-input",
1356 .vector_size = sizeof (u32),
1357 .type = VLIB_NODE_TYPE_INTERNAL,
1359 .n_errors = BFD_UDP_N_ERROR,
1360 .error_counters = bfd_udp_error_counters,
1362 .format_trace = bfd_input_format_trace,
1364 .n_next_nodes = BFD_UDP_INPUT_N_NEXT,
1367 [BFD_UDP_INPUT_NEXT_NORMAL] = "error-drop",
1368 [BFD_UDP_INPUT_NEXT_REPLY_ARP] = "ip4-arp",
1369 [BFD_UDP_INPUT_NEXT_REPLY_REWRITE] = "ip4-lookup",
1370 [BFD_UDP_INPUT_NEXT_REPLY_MIDCHAIN] = "ip4-midchain",
1376 bfd_udp6_input (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
1378 return bfd_udp_input (vm, rt, f, 1);
1382 VLIB_REGISTER_NODE (bfd_udp6_input_node, static) = {
1383 .function = bfd_udp6_input,
1384 .name = "bfd-udp6-input",
1385 .vector_size = sizeof (u32),
1386 .type = VLIB_NODE_TYPE_INTERNAL,
1388 .n_errors = BFD_UDP_N_ERROR,
1389 .error_counters = bfd_udp_error_counters,
1391 .format_trace = bfd_input_format_trace,
1393 .n_next_nodes = BFD_UDP_INPUT_N_NEXT,
1396 [BFD_UDP_INPUT_NEXT_NORMAL] = "error-drop",
1397 [BFD_UDP_INPUT_NEXT_REPLY_ARP] = "ip6-discover-neighbor",
1398 [BFD_UDP_INPUT_NEXT_REPLY_REWRITE] = "ip6-lookup",
1399 [BFD_UDP_INPUT_NEXT_REPLY_MIDCHAIN] = "ip6-midchain",
1405 * Process a frame of bfd echo packets
1406 * Expect 1 packet / frame
1409 bfd_udp_echo_input (vlib_main_t * vm, vlib_node_runtime_t * rt,
1410 vlib_frame_t * f, int is_ipv6)
1412 u32 n_left_from, *from;
1413 bfd_input_trace_t *t0;
1414 bfd_main_t *bm = &bfd_main;
1416 from = vlib_frame_vector_args (f); /* array of buffer indices */
1417 n_left_from = f->n_vectors; /* number of buffer indices */
1419 while (n_left_from > 0)
1426 b0 = vlib_get_buffer (vm, bi0);
1428 /* If this pkt is traced, snapshot the data */
1429 if (b0->flags & VLIB_BUFFER_IS_TRACED)
1432 t0 = vlib_add_trace (vm, rt, b0, sizeof (*t0));
1433 len = (b0->current_length < sizeof (t0->data)) ? b0->current_length
1434 : sizeof (t0->data);
1436 clib_memcpy_fast (t0->data, vlib_buffer_get_current (b0), len);
1439 bfd_session_t *bs = NULL;
1441 if ((bs = bfd_consume_echo_pkt (vm, bfd_udp_main.bfd_main, b0)))
1443 b0->error = rt->errors[BFD_UDP_ERROR_NONE];
1444 next0 = BFD_UDP_ECHO_INPUT_NEXT_NORMAL;
1448 /* loop back the packet */
1449 b0->error = rt->errors[BFD_UDP_ERROR_NONE];
1452 vlib_node_increment_counter (vm, bfd_udp_echo6_input_node.index,
1453 BFD_UDP_ERROR_NONE, 1);
1457 vlib_node_increment_counter (vm, bfd_udp_echo4_input_node.index,
1458 BFD_UDP_ERROR_NONE, 1);
1460 next0 = BFD_UDP_ECHO_INPUT_NEXT_REPLY_REWRITE;
1467 vlib_increment_combined_counter (
1468 &bm->rx_echo_counter, vm->thread_index, bs->bs_idx, 1,
1469 vlib_buffer_length_in_chain (vm, b0));
1472 vlib_set_next_frame_buffer (vm, rt, next0, bi0);
1478 return f->n_vectors;
1482 bfd_udp_echo4_input (vlib_main_t * vm, vlib_node_runtime_t * rt,
1485 return bfd_udp_echo_input (vm, rt, f, 0);
1489 bfd_echo_input_format_trace (u8 * s, va_list * args)
1491 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1492 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1493 const bfd_udp_echo_input_trace_t *t =
1494 va_arg (*args, bfd_udp_echo_input_trace_t *);
1495 if (t->len > STRUCT_SIZE_OF (bfd_pkt_t, head))
1497 s = format (s, "BFD ECHO:\n");
1498 s = format (s, " data: %U", format_hexdump, t->data, t->len);
1505 * bfd input graph node declaration
1508 VLIB_REGISTER_NODE (bfd_udp_echo4_input_node, static) = {
1509 .function = bfd_udp_echo4_input,
1510 .name = "bfd-udp-echo4-input",
1511 .vector_size = sizeof (u32),
1512 .type = VLIB_NODE_TYPE_INTERNAL,
1514 .n_errors = BFD_UDP_N_ERROR,
1515 .error_counters = bfd_udp_error_counters,
1517 .format_trace = bfd_echo_input_format_trace,
1519 .n_next_nodes = BFD_UDP_ECHO_INPUT_N_NEXT,
1522 [BFD_UDP_ECHO_INPUT_NEXT_NORMAL] = "error-drop",
1523 [BFD_UDP_ECHO_INPUT_NEXT_REPLY_ARP] = "ip4-arp",
1524 [BFD_UDP_ECHO_INPUT_NEXT_REPLY_REWRITE] = "ip4-lookup",
1530 bfd_udp_echo6_input (vlib_main_t * vm, vlib_node_runtime_t * rt,
1533 return bfd_udp_echo_input (vm, rt, f, 1);
1537 VLIB_REGISTER_NODE (bfd_udp_echo6_input_node, static) = {
1538 .function = bfd_udp_echo6_input,
1539 .name = "bfd-udp-echo6-input",
1540 .vector_size = sizeof (u32),
1541 .type = VLIB_NODE_TYPE_INTERNAL,
1543 .n_errors = BFD_UDP_N_ERROR,
1544 .error_counters = bfd_udp_error_counters,
1546 .format_trace = bfd_echo_input_format_trace,
1548 .n_next_nodes = BFD_UDP_ECHO_INPUT_N_NEXT,
1551 [BFD_UDP_ECHO_INPUT_NEXT_NORMAL] = "error-drop",
1552 [BFD_UDP_ECHO_INPUT_NEXT_REPLY_ARP] = "ip6-discover-neighbor",
1553 [BFD_UDP_ECHO_INPUT_NEXT_REPLY_REWRITE] = "ip6-lookup",
1559 static clib_error_t *
1560 bfd_udp_sw_if_add_del (CLIB_UNUSED (vnet_main_t *vnm), u32 sw_if_index,
1563 u32 *to_be_freed = NULL;
1564 bfd_udp_main_t *bum = &bfd_udp_main;
1565 BFD_DBG ("sw_if_add_del called, sw_if_index=%u, is_create=%u", sw_if_index,
1570 pool_foreach (bs, bum->bfd_main->sessions)
1572 if (bs->transport != BFD_TRANSPORT_UDP4 &&
1573 bs->transport != BFD_TRANSPORT_UDP6)
1577 if (bs->udp.key.sw_if_index != sw_if_index)
1581 vec_add1 (to_be_freed, bs->bs_idx);
1585 vec_foreach (bs_idx, to_be_freed)
1587 bfd_session_t *bs = pool_elt_at_index (bum->bfd_main->sessions, *bs_idx);
1588 vlib_log_notice (bum->log_class,
1589 "removal of sw_if_index=%u forces removal of bfd "
1590 "session with bs_idx=%u",
1591 sw_if_index, bs->bs_idx);
1592 bfd_session_set_flags (vlib_get_main (), bs, 0);
1593 bfd_udp_del_session_internal (vlib_get_main (), bs);
1598 VNET_SW_INTERFACE_ADD_DEL_FUNCTION (bfd_udp_sw_if_add_del);
1601 bfd_udp_stats_init (bfd_udp_main_t *bum)
1603 const char *name4 = "/bfd/udp4/sessions";
1604 bum->udp4_sessions_count_stat_seg_entry = vlib_stats_add_gauge ("%s", name4);
1606 vlib_stats_set_gauge (bum->udp4_sessions_count_stat_seg_entry, 0);
1607 if (~0 == bum->udp4_sessions_count_stat_seg_entry)
1609 return clib_error_return (
1610 0, "Could not create stat segment entry for %s", name4);
1612 const char *name6 = "/bfd/udp6/sessions";
1613 bum->udp6_sessions_count_stat_seg_entry = vlib_stats_add_gauge ("%s", name6);
1615 vlib_stats_set_gauge (bum->udp6_sessions_count_stat_seg_entry, 0);
1616 if (~0 == bum->udp6_sessions_count_stat_seg_entry)
1618 return clib_error_return (
1619 0, "Could not create stat segment entry for %s", name6);
1628 static clib_error_t *
1629 bfd_udp_init (vlib_main_t * vm)
1631 bfd_udp_main.udp4_sessions_count = 0;
1632 bfd_udp_main.udp6_sessions_count = 0;
1633 mhash_init (&bfd_udp_main.bfd_session_idx_by_bfd_key, sizeof (uword),
1634 sizeof (bfd_udp_key_t));
1635 bfd_udp_main.bfd_main = &bfd_main;
1636 bfd_udp_main.vnet_main = vnet_get_main ();
1637 bfd_udp_stats_init (&bfd_udp_main);
1639 bfd_udp_main.log_class = vlib_log_register_class ("bfd", "udp");
1640 vlib_log_debug (bfd_udp_main.log_class, "initialized");
1644 VLIB_INIT_FUNCTION (bfd_udp_init);
1647 * fd.io coding-style-patch-verification: ON
1650 * eval: (c-set-style "gnu")