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>
46 STATIC_ASSERT ((int) BFD_ERROR_##sym == (int) BFD_UDP_ERROR_##sym, \
47 "BFD error enums mismatch");
50 STATIC_ASSERT ((int) BFD_N_ERROR <= (int) BFD_UDP_N_ERROR,
51 "BFD error enum sizes mismatch");
56 /* hashmap - bfd session index by bfd key - used for CLI/API lookup, where
57 * discriminator is unknown */
58 mhash_t bfd_session_idx_by_bfd_key;
59 /* convenience variable */
60 vnet_main_t *vnet_main;
61 /* flag indicating whether echo_source_sw_if_index holds a valid value */
62 int echo_source_is_set;
63 /* loopback interface used to get echo source ip */
64 u32 echo_source_sw_if_index;
66 vlib_log_class_t log_class;
67 /* number of active udp4 sessions */
68 u32 udp4_sessions_count;
69 u32 udp4_sessions_count_stat_seg_entry;
70 /* number of active udp6 sessions */
71 u32 udp6_sessions_count;
72 u32 udp6_sessions_count_stat_seg_entry;
75 static vlib_node_registration_t bfd_udp4_input_node;
76 static vlib_node_registration_t bfd_udp6_input_node;
77 static vlib_node_registration_t bfd_udp_echo4_input_node;
78 static vlib_node_registration_t bfd_udp_echo6_input_node;
80 bfd_udp_main_t bfd_udp_main;
83 bfd_udp_update_stat_segment_entry (u32 entry, u64 value)
85 vlib_stats_segment_lock ();
86 vlib_stats_set_gauge (entry, value);
87 vlib_stats_segment_unlock ();
91 bfd_udp_set_echo_source (u32 sw_if_index)
93 vnet_sw_interface_t *sw_if =
94 vnet_get_sw_interface_or_null (bfd_udp_main.vnet_main, sw_if_index);
97 bfd_udp_main.echo_source_sw_if_index = sw_if_index;
98 bfd_udp_main.echo_source_is_set = 1;
101 return VNET_API_ERROR_BFD_ENOENT;
105 bfd_udp_del_echo_source ()
107 bfd_udp_main.echo_source_sw_if_index = ~0;
108 bfd_udp_main.echo_source_is_set = 0;
113 bfd_udp_is_echo_available (bfd_transport_e transport)
115 if (!bfd_udp_main.echo_source_is_set)
117 BFD_DBG ("UDP echo source not set - echo not available");
121 * for the echo to work, we need a loopback interface with at least one
122 * address with netmask length at most 31 (ip4) or 127 (ip6) so that we can
123 * pick an unused address from that subnet
125 vnet_sw_interface_t *sw_if =
126 vnet_get_sw_interface_or_null (bfd_udp_main.vnet_main,
127 bfd_udp_main.echo_source_sw_if_index);
128 if (sw_if && sw_if->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
130 if (BFD_TRANSPORT_UDP4 == transport)
132 ip4_main_t *im = &ip4_main;
133 ip_interface_address_t *ia = NULL;
134 foreach_ip_interface_address (&im->lookup_main, ia,
135 bfd_udp_main.echo_source_sw_if_index,
136 0 /* honor unnumbered */, ({
137 if (ia->address_length <= 31)
143 else if (BFD_TRANSPORT_UDP6 == transport)
145 ip6_main_t *im = &ip6_main;
146 ip_interface_address_t *ia = NULL;
147 foreach_ip_interface_address (&im->lookup_main, ia,
148 bfd_udp_main.echo_source_sw_if_index,
149 0 /* honor unnumbered */, ({
150 if (ia->address_length <= 127)
157 BFD_DBG ("No usable IP address for UDP echo - echo not available");
162 bfd_udp_bs_idx_to_sport (u32 bs_idx)
164 /* The source port MUST be in the range 49152 through 65535. The same UDP
165 * source port number MUST be used for all BFD Control packets associated
166 * with a particular session. The source port number SHOULD be unique among
167 * all BFD sessions on the system. If more than 16384 BFD sessions are
168 * simultaneously active, UDP source port numbers MAY be reused on
169 * multiple sessions, but the number of distinct uses of the same UDP
170 * source port number SHOULD be minimized.
172 return 49152 + bs_idx % (65535 - 49152 + 1);
176 bfd_udp_get_echo_src_ip4 (ip4_address_t * addr)
178 if (!bfd_udp_main.echo_source_is_set)
180 BFD_ERR ("cannot find ip4 address, echo source not set");
183 ip_interface_address_t *ia = NULL;
184 ip4_main_t *im = &ip4_main;
186 foreach_ip_interface_address (
187 &im->lookup_main, ia, bfd_udp_main.echo_source_sw_if_index,
188 0 /* honor unnumbered */, ({
190 ip_interface_address_get_address (&im->lookup_main, ia);
191 if (ia->address_length <= 31)
193 addr->as_u32 = clib_host_to_net_u32 (x->as_u32);
195 * flip the last bit to get a different address, might be network,
199 addr->as_u32 = clib_net_to_host_u32 (addr->as_u32);
203 BFD_ERR ("cannot find ip4 address, no usable address found");
208 bfd_udp_get_echo_src_ip6 (ip6_address_t * addr)
210 if (!bfd_udp_main.echo_source_is_set)
212 BFD_ERR ("cannot find ip6 address, echo source not set");
215 ip_interface_address_t *ia = NULL;
216 ip6_main_t *im = &ip6_main;
218 foreach_ip_interface_address (
219 &im->lookup_main, ia, bfd_udp_main.echo_source_sw_if_index,
220 0 /* honor unnumbered */, ({
222 ip_interface_address_get_address (&im->lookup_main, ia);
223 if (ia->address_length <= 127)
226 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
1352 VLIB_REGISTER_NODE (bfd_udp4_input_node, static) = {
1353 .function = bfd_udp4_input,
1354 .name = "bfd-udp4-input",
1355 .vector_size = sizeof (u32),
1356 .type = VLIB_NODE_TYPE_INTERNAL,
1358 .n_errors = BFD_UDP_N_ERROR,
1359 .error_counters = bfd_udp_error_counters,
1361 .format_trace = bfd_input_format_trace,
1363 .n_next_nodes = BFD_UDP_INPUT_N_NEXT,
1366 [BFD_UDP_INPUT_NEXT_NORMAL] = "error-drop",
1367 [BFD_UDP_INPUT_NEXT_REPLY_ARP] = "ip4-arp",
1368 [BFD_UDP_INPUT_NEXT_REPLY_REWRITE] = "ip4-lookup",
1369 [BFD_UDP_INPUT_NEXT_REPLY_MIDCHAIN] = "ip4-midchain",
1374 bfd_udp6_input (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
1376 return bfd_udp_input (vm, rt, f, 1);
1379 VLIB_REGISTER_NODE (bfd_udp6_input_node, static) = {
1380 .function = bfd_udp6_input,
1381 .name = "bfd-udp6-input",
1382 .vector_size = sizeof (u32),
1383 .type = VLIB_NODE_TYPE_INTERNAL,
1385 .n_errors = BFD_UDP_N_ERROR,
1386 .error_counters = bfd_udp_error_counters,
1388 .format_trace = bfd_input_format_trace,
1390 .n_next_nodes = BFD_UDP_INPUT_N_NEXT,
1393 [BFD_UDP_INPUT_NEXT_NORMAL] = "error-drop",
1394 [BFD_UDP_INPUT_NEXT_REPLY_ARP] = "ip6-discover-neighbor",
1395 [BFD_UDP_INPUT_NEXT_REPLY_REWRITE] = "ip6-lookup",
1396 [BFD_UDP_INPUT_NEXT_REPLY_MIDCHAIN] = "ip6-midchain",
1401 * Process a frame of bfd echo packets
1402 * Expect 1 packet / frame
1405 bfd_udp_echo_input (vlib_main_t * vm, vlib_node_runtime_t * rt,
1406 vlib_frame_t * f, int is_ipv6)
1408 u32 n_left_from, *from;
1409 bfd_input_trace_t *t0;
1410 bfd_main_t *bm = &bfd_main;
1412 from = vlib_frame_vector_args (f); /* array of buffer indices */
1413 n_left_from = f->n_vectors; /* number of buffer indices */
1415 while (n_left_from > 0)
1422 b0 = vlib_get_buffer (vm, bi0);
1424 /* If this pkt is traced, snapshot the data */
1425 if (b0->flags & VLIB_BUFFER_IS_TRACED)
1428 t0 = vlib_add_trace (vm, rt, b0, sizeof (*t0));
1429 len = (b0->current_length < sizeof (t0->data)) ? b0->current_length
1430 : sizeof (t0->data);
1432 clib_memcpy_fast (t0->data, vlib_buffer_get_current (b0), len);
1435 bfd_session_t *bs = NULL;
1437 if ((bs = bfd_consume_echo_pkt (vm, bfd_udp_main.bfd_main, b0)))
1439 b0->error = rt->errors[BFD_UDP_ERROR_NONE];
1440 next0 = BFD_UDP_ECHO_INPUT_NEXT_NORMAL;
1444 /* loop back the packet */
1445 b0->error = rt->errors[BFD_UDP_ERROR_NONE];
1448 vlib_node_increment_counter (vm, bfd_udp_echo6_input_node.index,
1449 BFD_UDP_ERROR_NONE, 1);
1453 vlib_node_increment_counter (vm, bfd_udp_echo4_input_node.index,
1454 BFD_UDP_ERROR_NONE, 1);
1456 next0 = BFD_UDP_ECHO_INPUT_NEXT_REPLY_REWRITE;
1463 vlib_increment_combined_counter (
1464 &bm->rx_echo_counter, vm->thread_index, bs->bs_idx, 1,
1465 vlib_buffer_length_in_chain (vm, b0));
1468 vlib_set_next_frame_buffer (vm, rt, next0, bi0);
1474 return f->n_vectors;
1478 bfd_udp_echo4_input (vlib_main_t * vm, vlib_node_runtime_t * rt,
1481 return bfd_udp_echo_input (vm, rt, f, 0);
1485 bfd_echo_input_format_trace (u8 * s, va_list * args)
1487 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1488 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1489 const bfd_udp_echo_input_trace_t *t =
1490 va_arg (*args, bfd_udp_echo_input_trace_t *);
1491 if (t->len > STRUCT_SIZE_OF (bfd_pkt_t, head))
1493 s = format (s, "BFD ECHO:\n");
1494 s = format (s, " data: %U", format_hexdump, t->data, t->len);
1501 * bfd input graph node declaration
1503 VLIB_REGISTER_NODE (bfd_udp_echo4_input_node, static) = {
1504 .function = bfd_udp_echo4_input,
1505 .name = "bfd-udp-echo4-input",
1506 .vector_size = sizeof (u32),
1507 .type = VLIB_NODE_TYPE_INTERNAL,
1509 .n_errors = BFD_UDP_N_ERROR,
1510 .error_counters = bfd_udp_error_counters,
1512 .format_trace = bfd_echo_input_format_trace,
1514 .n_next_nodes = BFD_UDP_ECHO_INPUT_N_NEXT,
1517 [BFD_UDP_ECHO_INPUT_NEXT_NORMAL] = "error-drop",
1518 [BFD_UDP_ECHO_INPUT_NEXT_REPLY_ARP] = "ip4-arp",
1519 [BFD_UDP_ECHO_INPUT_NEXT_REPLY_REWRITE] = "ip4-lookup",
1524 bfd_udp_echo6_input (vlib_main_t * vm, vlib_node_runtime_t * rt,
1527 return bfd_udp_echo_input (vm, rt, f, 1);
1530 VLIB_REGISTER_NODE (bfd_udp_echo6_input_node, static) = {
1531 .function = bfd_udp_echo6_input,
1532 .name = "bfd-udp-echo6-input",
1533 .vector_size = sizeof (u32),
1534 .type = VLIB_NODE_TYPE_INTERNAL,
1536 .n_errors = BFD_UDP_N_ERROR,
1537 .error_counters = bfd_udp_error_counters,
1539 .format_trace = bfd_echo_input_format_trace,
1541 .n_next_nodes = BFD_UDP_ECHO_INPUT_N_NEXT,
1544 [BFD_UDP_ECHO_INPUT_NEXT_NORMAL] = "error-drop",
1545 [BFD_UDP_ECHO_INPUT_NEXT_REPLY_ARP] = "ip6-discover-neighbor",
1546 [BFD_UDP_ECHO_INPUT_NEXT_REPLY_REWRITE] = "ip6-lookup",
1551 static clib_error_t *
1552 bfd_udp_sw_if_add_del (CLIB_UNUSED (vnet_main_t *vnm), u32 sw_if_index,
1555 u32 *to_be_freed = NULL;
1556 bfd_udp_main_t *bum = &bfd_udp_main;
1557 BFD_DBG ("sw_if_add_del called, sw_if_index=%u, is_create=%u", sw_if_index,
1562 pool_foreach (bs, bum->bfd_main->sessions)
1564 if (bs->transport != BFD_TRANSPORT_UDP4 &&
1565 bs->transport != BFD_TRANSPORT_UDP6)
1569 if (bs->udp.key.sw_if_index != sw_if_index)
1573 vec_add1 (to_be_freed, bs->bs_idx);
1577 vec_foreach (bs_idx, to_be_freed)
1579 bfd_session_t *bs = pool_elt_at_index (bum->bfd_main->sessions, *bs_idx);
1580 vlib_log_notice (bum->log_class,
1581 "removal of sw_if_index=%u forces removal of bfd "
1582 "session with bs_idx=%u",
1583 sw_if_index, bs->bs_idx);
1584 bfd_session_set_flags (vlib_get_main (), bs, 0);
1585 bfd_udp_del_session_internal (vlib_get_main (), bs);
1590 VNET_SW_INTERFACE_ADD_DEL_FUNCTION (bfd_udp_sw_if_add_del);
1593 bfd_udp_stats_init (bfd_udp_main_t *bum)
1595 const char *name4 = "/bfd/udp4/sessions";
1596 bum->udp4_sessions_count_stat_seg_entry = vlib_stats_add_gauge ("%s", name4);
1598 vlib_stats_set_gauge (bum->udp4_sessions_count_stat_seg_entry, 0);
1599 if (~0 == bum->udp4_sessions_count_stat_seg_entry)
1601 return clib_error_return (
1602 0, "Could not create stat segment entry for %s", name4);
1604 const char *name6 = "/bfd/udp6/sessions";
1605 bum->udp6_sessions_count_stat_seg_entry = vlib_stats_add_gauge ("%s", name6);
1607 vlib_stats_set_gauge (bum->udp6_sessions_count_stat_seg_entry, 0);
1608 if (~0 == bum->udp6_sessions_count_stat_seg_entry)
1610 return clib_error_return (
1611 0, "Could not create stat segment entry for %s", name6);
1620 static clib_error_t *
1621 bfd_udp_init (vlib_main_t * vm)
1623 bfd_udp_main.udp4_sessions_count = 0;
1624 bfd_udp_main.udp6_sessions_count = 0;
1625 mhash_init (&bfd_udp_main.bfd_session_idx_by_bfd_key, sizeof (uword),
1626 sizeof (bfd_udp_key_t));
1627 bfd_udp_main.bfd_main = &bfd_main;
1628 bfd_udp_main.vnet_main = vnet_get_main ();
1629 bfd_udp_stats_init (&bfd_udp_main);
1631 bfd_udp_main.log_class = vlib_log_register_class ("bfd", "udp");
1632 vlib_log_debug (bfd_udp_main.log_class, "initialized");
1636 VLIB_INIT_FUNCTION (bfd_udp_init);
1639 * fd.io coding-style-patch-verification: ON
1642 * eval: (c-set-style "gnu")