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 <vnet/bfd/bfd_debug.h>
39 #include <vnet/bfd/bfd_udp.h>
40 #include <vnet/bfd/bfd_main.h>
41 #include <vnet/bfd/bfd_api.h>
46 /* hashmap - bfd session index by bfd key - used for CLI/API lookup, where
47 * discriminator is unknown */
48 mhash_t bfd_session_idx_by_bfd_key;
49 /* convenience variable */
50 vnet_main_t *vnet_main;
51 /* flag indicating whether echo_source_sw_if_index holds a valid value */
52 int echo_source_is_set;
53 /* loopback interface used to get echo source ip */
54 u32 echo_source_sw_if_index;
55 /* node index of "ip4-arp" node */
57 /* node index of "ip6-discover-neighbor" node */
59 /* node index of "ip4-rewrite" node */
61 /* node index of "ip6-rewrite" node */
63 /* node index of "ip4-midchain" node */
65 /* node index of "ip6-midchain" node */
68 vlib_log_class_t log_class;
69 /* number of active udp4 sessions */
70 u32 udp4_sessions_count;
71 /* number of active udp6 sessions */
72 u32 udp6_sessions_count;
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_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 (u32 sw_if_index)
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, u32 bi, u32 next_node)
377 vlib_frame_t *f = vlib_get_frame_to_node (vm, next_node);
378 u32 *to_next = vlib_frame_vector_args (f);
381 vlib_put_frame_to_node (vm, next_node, f);
385 bfd_udp_calc_next_node (const struct bfd_session_s *bs, u32 * next_node)
387 vnet_main_t *vnm = vnet_get_main ();
388 const bfd_udp_session_t *bus = &bs->udp;
389 ip_adjacency_t *adj = adj_get (bus->adj_index);
391 /* don't try to send the buffer if the interface is not up */
392 if (!vnet_sw_interface_is_up (vnm, bus->key.sw_if_index))
395 switch (adj->lookup_next_index)
397 case IP_LOOKUP_NEXT_ARP:
398 switch (bs->transport)
400 case BFD_TRANSPORT_UDP4:
401 *next_node = bfd_udp_main.ip4_arp_idx;
403 case BFD_TRANSPORT_UDP6:
404 *next_node = bfd_udp_main.ip6_ndp_idx;
408 case IP_LOOKUP_NEXT_REWRITE:
409 switch (bs->transport)
411 case BFD_TRANSPORT_UDP4:
412 *next_node = bfd_udp_main.ip4_rewrite_idx;
414 case BFD_TRANSPORT_UDP6:
415 *next_node = bfd_udp_main.ip6_rewrite_idx;
419 case IP_LOOKUP_NEXT_MIDCHAIN:
420 switch (bs->transport)
422 case BFD_TRANSPORT_UDP4:
423 *next_node = bfd_udp_main.ip4_midchain_idx;
425 case BFD_TRANSPORT_UDP6:
426 *next_node = bfd_udp_main.ip6_midchain_idx;
438 bfd_transport_udp4 (vlib_main_t * vm, u32 bi, const struct bfd_session_s *bs)
441 int rv = bfd_udp_calc_next_node (bs, &next_node);
444 bfd_create_frame_to_next_node (vm, bi, next_node);
450 bfd_transport_udp6 (vlib_main_t * vm, u32 bi, const struct bfd_session_s *bs)
453 int rv = bfd_udp_calc_next_node (bs, &next_node);
456 bfd_create_frame_to_next_node (vm, bi, next_node);
461 static bfd_session_t *
462 bfd_lookup_session (bfd_udp_main_t * bum, const bfd_udp_key_t * key)
464 uword *p = mhash_get (&bum->bfd_session_idx_by_bfd_key, key);
467 return bfd_find_session_by_idx (bum->bfd_main, *p);
473 bfd_udp_key_init (bfd_udp_key_t * key, u32 sw_if_index,
474 const ip46_address_t * local_addr,
475 const ip46_address_t * peer_addr)
477 clib_memset (key, 0, sizeof (*key));
478 key->sw_if_index = sw_if_index;
479 key->local_addr.as_u64[0] = local_addr->as_u64[0];
480 key->local_addr.as_u64[1] = local_addr->as_u64[1];
481 key->peer_addr.as_u64[0] = peer_addr->as_u64[0];
482 key->peer_addr.as_u64[1] = peer_addr->as_u64[1];
485 static vnet_api_error_t
486 bfd_udp_add_session_internal (vlib_main_t * vm, bfd_udp_main_t * bum,
487 u32 sw_if_index, u32 desired_min_tx_usec,
488 u32 required_min_rx_usec, u8 detect_mult,
489 const ip46_address_t * local_addr,
490 const ip46_address_t * peer_addr,
491 bfd_session_t ** bs_out)
493 /* get a pool entry and if we end up not needing it, give it back */
494 bfd_transport_e t = BFD_TRANSPORT_UDP4;
495 if (!ip46_address_is_ip4 (local_addr))
497 t = BFD_TRANSPORT_UDP6;
499 bfd_session_t *bs = bfd_get_session (bum->bfd_main, t);
502 return VNET_API_ERROR_BFD_EAGAIN;
504 bfd_udp_session_t *bus = &bs->udp;
505 clib_memset (bus, 0, sizeof (*bus));
506 bfd_udp_key_t *key = &bus->key;
507 bfd_udp_key_init (key, sw_if_index, local_addr, peer_addr);
508 const bfd_session_t *tmp = bfd_lookup_session (bum, key);
511 vlib_log_err (bum->log_class,
512 "duplicate bfd-udp session, existing bs_idx=%d",
514 bfd_put_session (bum->bfd_main, bs);
515 return VNET_API_ERROR_BFD_EEXIST;
517 mhash_set (&bum->bfd_session_idx_by_bfd_key, key, bs->bs_idx, NULL);
518 BFD_DBG ("session created, bs_idx=%u, sw_if_index=%d, local=%U, peer=%U",
519 bs->bs_idx, key->sw_if_index, format_ip46_address,
520 &key->local_addr, IP46_TYPE_ANY, format_ip46_address,
521 &key->peer_addr, IP46_TYPE_ANY);
522 vlib_log_info (bum->log_class, "create BFD session: %U",
523 format_bfd_session, bs);
524 if (BFD_TRANSPORT_UDP4 == t)
526 bus->adj_index = adj_nbr_add_or_lock (FIB_PROTOCOL_IP4, VNET_LINK_IP4,
529 BFD_DBG ("adj_nbr_add_or_lock(FIB_PROTOCOL_IP4, VNET_LINK_IP4, %U, %d) "
530 "returns %d", format_ip46_address, &key->peer_addr,
531 IP46_TYPE_ANY, key->sw_if_index, bus->adj_index);
532 ++bum->udp4_sessions_count;
533 if (1 == bum->udp4_sessions_count)
535 udp_register_dst_port (vm, UDP_DST_PORT_bfd4,
536 bfd_udp4_input_node.index, 1);
537 udp_register_dst_port (vm, UDP_DST_PORT_bfd_echo4,
538 bfd_udp_echo4_input_node.index, 1);
543 bus->adj_index = adj_nbr_add_or_lock (FIB_PROTOCOL_IP6, VNET_LINK_IP6,
546 BFD_DBG ("adj_nbr_add_or_lock(FIB_PROTOCOL_IP6, VNET_LINK_IP6, %U, %d) "
547 "returns %d", format_ip46_address, &key->peer_addr,
548 IP46_TYPE_ANY, key->sw_if_index, bus->adj_index);
549 ++bum->udp6_sessions_count;
550 if (1 == bum->udp6_sessions_count)
552 udp_register_dst_port (vm, UDP_DST_PORT_bfd6,
553 bfd_udp6_input_node.index, 0);
554 udp_register_dst_port (vm, UDP_DST_PORT_bfd_echo6,
555 bfd_udp_echo6_input_node.index, 0);
559 return bfd_session_set_params (bum->bfd_main, bs, desired_min_tx_usec,
560 required_min_rx_usec, detect_mult);
563 static vnet_api_error_t
564 bfd_udp_validate_api_input (u32 sw_if_index,
565 const ip46_address_t * local_addr,
566 const ip46_address_t * peer_addr)
568 bfd_udp_main_t *bum = &bfd_udp_main;
569 vnet_sw_interface_t *sw_if =
570 vnet_get_sw_interface_or_null (bfd_udp_main.vnet_main, sw_if_index);
571 u8 local_ip_valid = 0;
572 ip_interface_address_t *ia = NULL;
575 vlib_log_err (bum->log_class,
576 "got NULL sw_if when getting interface by index %u",
578 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
580 if (ip46_address_is_ip4 (local_addr))
582 if (!ip46_address_is_ip4 (peer_addr))
584 vlib_log_err (bum->log_class,
585 "IP family mismatch (local is ipv4, peer is ipv6)");
586 return VNET_API_ERROR_INVALID_ARGUMENT;
588 ip4_main_t *im = &ip4_main;
591 foreach_ip_interface_address (
592 &im->lookup_main, ia, sw_if_index, 0 /* honor unnumbered */, ({
594 ip_interface_address_get_address (&im->lookup_main, ia);
595 if (x->as_u32 == local_addr->ip4.as_u32)
597 /* valid address for this interface */
606 if (ip46_address_is_ip4 (peer_addr))
608 vlib_log_err (bum->log_class,
609 "IP family mismatch (local is ipv6, peer is ipv4)");
610 return VNET_API_ERROR_INVALID_ARGUMENT;
613 if (ip6_address_is_link_local_unicast (&local_addr->ip6))
615 const ip6_address_t *ll_addr;
616 ll_addr = ip6_get_link_local_address (sw_if_index);
617 if (ll_addr && ip6_address_is_equal (ll_addr, &local_addr->ip6))
619 /* valid address for this interface */
625 ip6_main_t *im = &ip6_main;
627 foreach_ip_interface_address (
628 &im->lookup_main, ia, sw_if_index, 0 /* honor unnumbered */, ({
630 ip_interface_address_get_address (&im->lookup_main, ia);
631 if (local_addr->ip6.as_u64[0] == x->as_u64[0] &&
632 local_addr->ip6.as_u64[1] == x->as_u64[1])
634 /* valid address for this interface */
645 vlib_log_err (bum->log_class,
646 "local address %U not found on interface with index %u",
647 format_ip46_address, local_addr, IP46_TYPE_ANY,
649 return VNET_API_ERROR_ADDRESS_NOT_FOUND_FOR_INTERFACE;
655 static vnet_api_error_t
656 bfd_udp_find_session_by_api_input (u32 sw_if_index,
657 const ip46_address_t * local_addr,
658 const ip46_address_t * peer_addr,
659 bfd_session_t ** bs_out)
661 vnet_api_error_t rv =
662 bfd_udp_validate_api_input (sw_if_index, local_addr, peer_addr);
665 bfd_udp_main_t *bum = &bfd_udp_main;
667 bfd_udp_key_init (&key, sw_if_index, local_addr, peer_addr);
668 bfd_session_t *bs = bfd_lookup_session (bum, &key);
675 vlib_log_err (bum->log_class,
676 "BFD session not found, sw_if_index=%u, local=%U, peer=%U",
677 sw_if_index, format_ip46_address, local_addr,
678 IP46_TYPE_ANY, format_ip46_address, peer_addr,
680 return VNET_API_ERROR_BFD_ENOENT;
686 static vnet_api_error_t
687 bfd_api_verify_common (u32 sw_if_index, u32 desired_min_tx_usec,
688 u32 required_min_rx_usec, u8 detect_mult,
689 const ip46_address_t * local_addr,
690 const ip46_address_t * peer_addr)
692 bfd_udp_main_t *bum = &bfd_udp_main;
693 vnet_api_error_t rv =
694 bfd_udp_validate_api_input (sw_if_index, local_addr, peer_addr);
701 vlib_log_err (bum->log_class, "detect_mult < 1");
702 return VNET_API_ERROR_INVALID_ARGUMENT;
704 if (desired_min_tx_usec < 1)
706 vlib_log_err (bum->log_class, "desired_min_tx_usec < 1");
707 return VNET_API_ERROR_INVALID_ARGUMENT;
713 bfd_udp_del_session_internal (vlib_main_t * vm, bfd_session_t * bs)
715 bfd_udp_main_t *bum = &bfd_udp_main;
716 BFD_DBG ("free bfd-udp session, bs_idx=%d", bs->bs_idx);
717 mhash_unset (&bum->bfd_session_idx_by_bfd_key, &bs->udp.key, NULL);
718 adj_unlock (bs->udp.adj_index);
719 switch (bs->transport)
721 case BFD_TRANSPORT_UDP4:
722 --bum->udp4_sessions_count;
723 if (!bum->udp4_sessions_count)
725 udp_unregister_dst_port (vm, UDP_DST_PORT_bfd4, 1);
726 udp_unregister_dst_port (vm, UDP_DST_PORT_bfd_echo4, 1);
729 case BFD_TRANSPORT_UDP6:
730 --bum->udp6_sessions_count;
731 if (!bum->udp6_sessions_count)
733 udp_unregister_dst_port (vm, UDP_DST_PORT_bfd6, 0);
734 udp_unregister_dst_port (vm, UDP_DST_PORT_bfd_echo6, 0);
738 bfd_put_session (bum->bfd_main, bs);
741 static vnet_api_error_t
742 bfd_udp_add_and_start_session (u32 sw_if_index,
743 const ip46_address_t *local_addr,
744 const ip46_address_t *peer_addr,
745 u32 desired_min_tx_usec,
746 u32 required_min_rx_usec, u8 detect_mult,
747 u8 is_authenticated, u32 conf_key_id,
750 bfd_session_t *bs = NULL;
753 rv = bfd_udp_add_session_internal (
754 vlib_get_main (), &bfd_udp_main, sw_if_index, desired_min_tx_usec,
755 required_min_rx_usec, detect_mult, local_addr, peer_addr, &bs);
757 if (!rv && is_authenticated)
759 rv = bfd_auth_activate (bs, conf_key_id, bfd_key_id,
760 0 /* is not delayed */);
763 bfd_udp_del_session_internal (vlib_get_main (), bs);
768 bfd_session_start (bfd_udp_main.bfd_main, bs);
775 bfd_udp_add_session (u32 sw_if_index, const ip46_address_t * local_addr,
776 const ip46_address_t * peer_addr,
777 u32 desired_min_tx_usec, u32 required_min_rx_usec,
778 u8 detect_mult, u8 is_authenticated, u32 conf_key_id,
781 bfd_main_t *bm = &bfd_main;
784 vnet_api_error_t rv =
785 bfd_api_verify_common (sw_if_index, desired_min_tx_usec,
786 required_min_rx_usec, detect_mult,
787 local_addr, peer_addr);
790 rv = bfd_udp_add_and_start_session (
791 sw_if_index, local_addr, peer_addr, desired_min_tx_usec,
792 required_min_rx_usec, detect_mult, is_authenticated, conf_key_id,
800 bfd_udp_upd_session (u32 sw_if_index, const ip46_address_t *local_addr,
801 const ip46_address_t *peer_addr, u32 desired_min_tx_usec,
802 u32 required_min_rx_usec, u8 detect_mult,
803 u8 is_authenticated, u32 conf_key_id, u8 bfd_key_id)
805 bfd_main_t *bm = &bfd_main;
808 vnet_api_error_t rv = bfd_api_verify_common (
809 sw_if_index, desired_min_tx_usec, required_min_rx_usec, detect_mult,
810 local_addr, peer_addr);
813 bfd_session_t *bs = NULL;
815 rv = bfd_udp_find_session_by_api_input (sw_if_index, local_addr,
817 if (VNET_API_ERROR_BFD_ENOENT == rv)
818 rv = bfd_udp_add_and_start_session (
819 sw_if_index, local_addr, peer_addr, desired_min_tx_usec,
820 required_min_rx_usec, detect_mult, is_authenticated, conf_key_id,
823 rv = bfd_session_set_params (bfd_udp_main.bfd_main, bs,
824 desired_min_tx_usec, required_min_rx_usec,
833 bfd_udp_mod_session (u32 sw_if_index, const ip46_address_t *local_addr,
834 const ip46_address_t *peer_addr, u32 desired_min_tx_usec,
835 u32 required_min_rx_usec, u8 detect_mult)
837 bfd_session_t *bs = NULL;
838 bfd_main_t *bm = &bfd_main;
839 vnet_api_error_t error;
841 vnet_api_error_t rv =
842 bfd_udp_find_session_by_api_input (sw_if_index, local_addr, peer_addr,
850 error = bfd_session_set_params (bfd_udp_main.bfd_main, bs,
851 desired_min_tx_usec, required_min_rx_usec,
858 bfd_udp_del_session (u32 sw_if_index,
859 const ip46_address_t * local_addr,
860 const ip46_address_t * peer_addr)
862 bfd_session_t *bs = NULL;
863 bfd_main_t *bm = &bfd_main;
865 vnet_api_error_t rv =
866 bfd_udp_find_session_by_api_input (sw_if_index, local_addr, peer_addr,
873 bfd_udp_del_session_internal (vlib_get_main (), bs);
879 bfd_udp_session_set_flags (vlib_main_t * vm, u32 sw_if_index,
880 const ip46_address_t * local_addr,
881 const ip46_address_t * peer_addr, u8 admin_up_down)
883 bfd_session_t *bs = NULL;
884 bfd_main_t *bm = &bfd_main;
886 vnet_api_error_t rv =
887 bfd_udp_find_session_by_api_input (sw_if_index, local_addr, peer_addr,
894 bfd_session_set_flags (vm, bs, admin_up_down);
900 bfd_udp_auth_activate (u32 sw_if_index,
901 const ip46_address_t * local_addr,
902 const ip46_address_t * peer_addr,
903 u32 conf_key_id, u8 key_id, u8 is_delayed)
905 bfd_main_t *bm = &bfd_main;
907 vnet_api_error_t error;
909 bfd_session_t *bs = NULL;
910 vnet_api_error_t rv =
911 bfd_udp_find_session_by_api_input (sw_if_index, local_addr, peer_addr,
918 error = bfd_auth_activate (bs, conf_key_id, key_id, is_delayed);
924 bfd_udp_auth_deactivate (u32 sw_if_index,
925 const ip46_address_t * local_addr,
926 const ip46_address_t * peer_addr, u8 is_delayed)
928 bfd_main_t *bm = &bfd_main;
929 vnet_api_error_t error;
931 bfd_session_t *bs = NULL;
932 vnet_api_error_t rv =
933 bfd_udp_find_session_by_api_input (sw_if_index, local_addr, peer_addr,
940 error = bfd_auth_deactivate (bs, is_delayed);
947 BFD_UDP_INPUT_NEXT_NORMAL,
948 BFD_UDP_INPUT_NEXT_REPLY_ARP,
949 BFD_UDP_INPUT_NEXT_REPLY_REWRITE,
950 BFD_UDP_INPUT_NEXT_REPLY_MIDCHAIN,
951 BFD_UDP_INPUT_N_NEXT,
952 } bfd_udp_input_next_t;
954 /* Packet counters - BFD control frames */
955 #define foreach_bfd_udp_error(F) \
956 F (NONE, "good bfd packets (processed)") \
957 F (BAD, "invalid bfd packets")
959 #define F(sym, string) static char BFD_UDP_ERR_##sym##_STR[] = string;
960 foreach_bfd_udp_error (F);
963 static char *bfd_udp_error_strings[] = {
964 #define F(sym, string) BFD_UDP_ERR_##sym##_STR,
965 foreach_bfd_udp_error (F)
971 #define F(sym, str) BFD_UDP_ERROR_##sym,
972 foreach_bfd_udp_error (F)
979 BFD_UDP_ECHO_INPUT_NEXT_NORMAL,
980 BFD_UDP_ECHO_INPUT_NEXT_REPLY_ARP,
981 BFD_UDP_ECHO_INPUT_NEXT_REPLY_REWRITE,
982 BFD_UDP_ECHO_INPUT_N_NEXT,
983 } bfd_udp_echo_input_next_t;
985 /* Packet counters - BFD ECHO packets */
986 #define foreach_bfd_udp_echo_error(F) \
987 F (NONE, "good bfd echo packets (processed)") \
988 F (BAD, "invalid bfd echo packets")
990 #define F(sym, string) static char BFD_UDP_ECHO_ERR_##sym##_STR[] = string;
991 foreach_bfd_udp_echo_error (F);
994 static char *bfd_udp_echo_error_strings[] = {
995 #define F(sym, string) BFD_UDP_ECHO_ERR_##sym##_STR,
996 foreach_bfd_udp_echo_error (F)
1002 #define F(sym, str) BFD_UDP_ECHO_ERROR_##sym,
1003 foreach_bfd_udp_echo_error (F)
1005 BFD_UDP_ECHO_N_ERROR,
1006 } bfd_udp_echo_error_t;
1009 bfd_udp4_find_headers (vlib_buffer_t * b, ip4_header_t ** ip4,
1010 udp_header_t ** udp)
1012 /* sanity check first */
1013 const i32 start = vnet_buffer (b)->l3_hdr_offset;
1014 if (start < 0 && start < sizeof (b->pre_data))
1016 BFD_ERR ("Start of ip header is before pre_data, ignoring");
1021 *ip4 = (ip4_header_t *) (b->data + start);
1022 if ((u8 *) * ip4 > (u8 *) vlib_buffer_get_current (b))
1024 BFD_ERR ("Start of ip header is beyond current data, ignoring");
1029 *udp = (udp_header_t *) ((*ip4) + 1);
1032 static bfd_udp_error_t
1033 bfd_udp4_verify_transport (const ip4_header_t * ip4,
1034 const udp_header_t * udp, const bfd_session_t * bs)
1036 const bfd_udp_session_t *bus = &bs->udp;
1037 const bfd_udp_key_t *key = &bus->key;
1038 if (ip4->src_address.as_u32 != key->peer_addr.ip4.as_u32)
1040 BFD_ERR ("IPv4 src addr mismatch, got %U, expected %U",
1041 format_ip4_address, ip4->src_address.as_u8, format_ip4_address,
1042 key->peer_addr.ip4.as_u8);
1043 return BFD_UDP_ERROR_BAD;
1045 if (ip4->dst_address.as_u32 != key->local_addr.ip4.as_u32)
1047 BFD_ERR ("IPv4 dst addr mismatch, got %U, expected %U",
1048 format_ip4_address, ip4->dst_address.as_u8, format_ip4_address,
1049 key->local_addr.ip4.as_u8);
1050 return BFD_UDP_ERROR_BAD;
1052 const u8 expected_ttl = 255;
1053 if (ip4->ttl != expected_ttl)
1055 BFD_ERR ("IPv4 unexpected TTL value %u, expected %u", ip4->ttl,
1057 return BFD_UDP_ERROR_BAD;
1059 if (clib_net_to_host_u16 (udp->src_port) < 49152)
1061 BFD_ERR ("Invalid UDP src port %u, out of range <49152,65535>",
1064 return BFD_UDP_ERROR_NONE;
1074 bfd_rpc_update_session (vlib_main_t * vm, u32 bs_idx, const bfd_pkt_t * pkt)
1076 bfd_main_t *bm = &bfd_main;
1078 bfd_consume_pkt (vm, bm, pkt, bs_idx);
1082 static bfd_udp_error_t
1083 bfd_udp4_scan (vlib_main_t * vm, vlib_node_runtime_t * rt,
1084 vlib_buffer_t * b, bfd_session_t ** bs_out)
1086 const bfd_pkt_t *pkt = vlib_buffer_get_current (b);
1087 if (sizeof (*pkt) > b->current_length)
1090 ("Payload size %d too small to hold bfd packet of minimum size %d",
1091 b->current_length, sizeof (*pkt));
1092 return BFD_UDP_ERROR_BAD;
1096 bfd_udp4_find_headers (b, &ip4, &udp);
1099 BFD_ERR ("Couldn't find ip4 or udp header");
1100 return BFD_UDP_ERROR_BAD;
1102 const u32 udp_payload_length = udp->length - sizeof (*udp);
1103 if (pkt->head.length > udp_payload_length)
1106 ("BFD packet length is larger than udp payload length (%u > %u)",
1107 pkt->head.length, udp_payload_length);
1108 return BFD_UDP_ERROR_BAD;
1110 if (!bfd_verify_pkt_common (pkt))
1112 return BFD_UDP_ERROR_BAD;
1114 bfd_session_t *bs = NULL;
1117 BFD_DBG ("Looking up BFD session using discriminator %u",
1119 bs = bfd_find_session_by_disc (bfd_udp_main.bfd_main, pkt->your_disc);
1124 clib_memset (&key, 0, sizeof (key));
1125 key.sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
1126 key.local_addr.ip4.as_u32 = ip4->dst_address.as_u32;
1127 key.peer_addr.ip4.as_u32 = ip4->src_address.as_u32;
1128 BFD_DBG ("Looking up BFD session using key (sw_if_index=%u, local=%U, "
1130 key.sw_if_index, format_ip4_address, key.local_addr.ip4.as_u8,
1131 format_ip4_address, key.peer_addr.ip4.as_u8);
1132 bs = bfd_lookup_session (&bfd_udp_main, &key);
1136 BFD_ERR ("BFD session lookup failed - no session matches BFD pkt");
1137 return BFD_UDP_ERROR_BAD;
1139 BFD_DBG ("BFD session found, bs_idx=%u", bs->bs_idx);
1140 if (!bfd_verify_pkt_auth (vm, pkt, b->current_length, bs))
1142 BFD_ERR ("Packet verification failed, dropping packet");
1143 return BFD_UDP_ERROR_BAD;
1145 bfd_udp_error_t err;
1146 if (BFD_UDP_ERROR_NONE != (err = bfd_udp4_verify_transport (ip4, udp, bs)))
1150 bfd_rpc_update_session (vm, bs->bs_idx, pkt);
1152 return BFD_UDP_ERROR_NONE;
1156 bfd_udp6_find_headers (vlib_buffer_t * b, ip6_header_t ** ip6,
1157 udp_header_t ** udp)
1159 /* sanity check first */
1160 const i32 start = vnet_buffer (b)->l3_hdr_offset;
1161 if (start < 0 && start < sizeof (b->pre_data))
1163 BFD_ERR ("Start of ip header is before pre_data, ignoring");
1168 *ip6 = (ip6_header_t *) (b->data + start);
1169 if ((u8 *) * ip6 > (u8 *) vlib_buffer_get_current (b))
1171 BFD_ERR ("Start of ip header is beyond current data, ignoring");
1176 if ((*ip6)->protocol != IP_PROTOCOL_UDP)
1178 BFD_ERR ("Unexpected protocol in IPv6 header '%u', expected '%u' (== "
1179 "IP_PROTOCOL_UDP)", (*ip6)->protocol, IP_PROTOCOL_UDP);
1184 *udp = (udp_header_t *) ((*ip6) + 1);
1187 static bfd_udp_error_t
1188 bfd_udp6_verify_transport (const ip6_header_t * ip6,
1189 const udp_header_t * udp, const bfd_session_t * bs)
1191 const bfd_udp_session_t *bus = &bs->udp;
1192 const bfd_udp_key_t *key = &bus->key;
1193 if (ip6->src_address.as_u64[0] != key->peer_addr.ip6.as_u64[0] &&
1194 ip6->src_address.as_u64[1] != key->peer_addr.ip6.as_u64[1])
1196 BFD_ERR ("IP src addr mismatch, got %U, expected %U",
1197 format_ip6_address, ip6, format_ip6_address,
1198 &key->peer_addr.ip6);
1199 return BFD_UDP_ERROR_BAD;
1201 if (ip6->dst_address.as_u64[0] != key->local_addr.ip6.as_u64[0] &&
1202 ip6->dst_address.as_u64[1] != key->local_addr.ip6.as_u64[1])
1204 BFD_ERR ("IP dst addr mismatch, got %U, expected %U",
1205 format_ip6_address, ip6, format_ip6_address,
1206 &key->local_addr.ip6);
1207 return BFD_UDP_ERROR_BAD;
1209 const u8 expected_hop_limit = 255;
1210 if (ip6->hop_limit != expected_hop_limit)
1212 BFD_ERR ("IPv6 unexpected hop-limit value %u, expected %u",
1213 ip6->hop_limit, expected_hop_limit);
1214 return BFD_UDP_ERROR_BAD;
1216 if (clib_net_to_host_u16 (udp->src_port) < 49152)
1218 BFD_ERR ("Invalid UDP src port %u, out of range <49152,65535>",
1221 return BFD_UDP_ERROR_NONE;
1224 static bfd_udp_error_t
1225 bfd_udp6_scan (vlib_main_t * vm, vlib_node_runtime_t * rt,
1226 vlib_buffer_t * b, bfd_session_t ** bs_out)
1228 const bfd_pkt_t *pkt = vlib_buffer_get_current (b);
1229 if (sizeof (*pkt) > b->current_length)
1232 ("Payload size %d too small to hold bfd packet of minimum size %d",
1233 b->current_length, sizeof (*pkt));
1234 return BFD_UDP_ERROR_BAD;
1238 bfd_udp6_find_headers (b, &ip6, &udp);
1241 BFD_ERR ("Couldn't find ip6 or udp header");
1242 return BFD_UDP_ERROR_BAD;
1244 const u32 udp_payload_length = udp->length - sizeof (*udp);
1245 if (pkt->head.length > udp_payload_length)
1248 ("BFD packet length is larger than udp payload length (%u > %u)",
1249 pkt->head.length, udp_payload_length);
1250 return BFD_UDP_ERROR_BAD;
1252 if (!bfd_verify_pkt_common (pkt))
1254 return BFD_UDP_ERROR_BAD;
1256 bfd_session_t *bs = NULL;
1259 BFD_DBG ("Looking up BFD session using discriminator %u",
1261 bs = bfd_find_session_by_disc (bfd_udp_main.bfd_main, pkt->your_disc);
1266 clib_memset (&key, 0, sizeof (key));
1267 key.sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
1268 key.local_addr.ip6.as_u64[0] = ip6->dst_address.as_u64[0];
1269 key.local_addr.ip6.as_u64[1] = ip6->dst_address.as_u64[1];
1270 key.peer_addr.ip6.as_u64[0] = ip6->src_address.as_u64[0];
1271 key.peer_addr.ip6.as_u64[1] = ip6->src_address.as_u64[1];
1272 BFD_DBG ("Looking up BFD session using key (sw_if_index=%u, local=%U, "
1274 key.sw_if_index, format_ip6_address, &key.local_addr,
1275 format_ip6_address, &key.peer_addr);
1276 bs = bfd_lookup_session (&bfd_udp_main, &key);
1280 BFD_ERR ("BFD session lookup failed - no session matches BFD pkt");
1281 return BFD_UDP_ERROR_BAD;
1283 BFD_DBG ("BFD session found, bs_idx=%u", bs->bs_idx);
1284 if (!bfd_verify_pkt_auth (vm, pkt, b->current_length, bs))
1286 BFD_ERR ("Packet verification failed, dropping packet");
1287 return BFD_UDP_ERROR_BAD;
1289 bfd_udp_error_t err;
1290 if (BFD_UDP_ERROR_NONE != (err = bfd_udp6_verify_transport (ip6, udp, bs)))
1294 bfd_rpc_update_session (vm, bs->bs_idx, pkt);
1296 return BFD_UDP_ERROR_NONE;
1300 * Process a frame of bfd packets
1301 * Expect 1 packet / frame
1304 bfd_udp_input (vlib_main_t * vm, vlib_node_runtime_t * rt,
1305 vlib_frame_t * f, int is_ipv6)
1307 u32 n_left_from, *from;
1308 bfd_input_trace_t *t0;
1309 bfd_main_t *bm = &bfd_main;
1311 from = vlib_frame_vector_args (f); /* array of buffer indices */
1312 n_left_from = f->n_vectors; /* number of buffer indices */
1314 while (n_left_from > 0)
1321 b0 = vlib_get_buffer (vm, bi0);
1323 bfd_session_t *bs = NULL;
1325 /* If this pkt is traced, snapshot the data */
1326 if (b0->flags & VLIB_BUFFER_IS_TRACED)
1329 t0 = vlib_add_trace (vm, rt, b0, sizeof (*t0));
1330 len = (b0->current_length < sizeof (t0->data)) ? b0->current_length
1331 : sizeof (t0->data);
1333 clib_memcpy_fast (t0->data, vlib_buffer_get_current (b0), len);
1336 /* scan this bfd pkt. error0 is the counter index to bmp */
1340 error0 = bfd_udp6_scan (vm, rt, b0, &bs);
1344 error0 = bfd_udp4_scan (vm, rt, b0, &bs);
1346 b0->error = rt->errors[error0];
1348 next0 = BFD_UDP_INPUT_NEXT_NORMAL;
1349 if (BFD_UDP_ERROR_NONE == error0)
1352 * if everything went fine, check for poll bit, if present, re-use
1353 * the buffer and based on (now updated) session parameters, send
1354 * the final packet back
1356 const bfd_pkt_t *pkt = vlib_buffer_get_current (b0);
1357 if (bfd_pkt_get_poll (pkt))
1359 b0->current_data = 0;
1360 b0->current_length = 0;
1361 bfd_init_final_control_frame (vm, b0, bfd_udp_main.bfd_main, bs,
1365 vlib_node_increment_counter (vm, bfd_udp6_input_node.index,
1370 vlib_node_increment_counter (vm, bfd_udp4_input_node.index,
1373 const bfd_udp_session_t *bus = &bs->udp;
1374 ip_adjacency_t *adj = adj_get (bus->adj_index);
1375 switch (adj->lookup_next_index)
1377 case IP_LOOKUP_NEXT_ARP:
1378 next0 = BFD_UDP_INPUT_NEXT_REPLY_ARP;
1380 case IP_LOOKUP_NEXT_REWRITE:
1381 next0 = BFD_UDP_INPUT_NEXT_REPLY_REWRITE;
1383 case IP_LOOKUP_NEXT_MIDCHAIN:
1384 next0 = BFD_UDP_INPUT_NEXT_REPLY_MIDCHAIN;
1393 vlib_set_next_frame_buffer (vm, rt, next0, bi0);
1399 return f->n_vectors;
1403 bfd_udp4_input (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
1405 return bfd_udp_input (vm, rt, f, 0);
1409 * bfd input graph node declaration
1412 VLIB_REGISTER_NODE (bfd_udp4_input_node, static) = {
1413 .function = bfd_udp4_input,
1414 .name = "bfd-udp4-input",
1415 .vector_size = sizeof (u32),
1416 .type = VLIB_NODE_TYPE_INTERNAL,
1418 .n_errors = BFD_UDP_N_ERROR,
1419 .error_strings = bfd_udp_error_strings,
1421 .format_trace = bfd_input_format_trace,
1423 .n_next_nodes = BFD_UDP_INPUT_N_NEXT,
1426 [BFD_UDP_INPUT_NEXT_NORMAL] = "error-drop",
1427 [BFD_UDP_INPUT_NEXT_REPLY_ARP] = "ip4-arp",
1428 [BFD_UDP_INPUT_NEXT_REPLY_REWRITE] = "ip4-lookup",
1429 [BFD_UDP_INPUT_NEXT_REPLY_MIDCHAIN] = "ip4-midchain",
1435 bfd_udp6_input (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
1437 return bfd_udp_input (vm, rt, f, 1);
1441 VLIB_REGISTER_NODE (bfd_udp6_input_node, static) = {
1442 .function = bfd_udp6_input,
1443 .name = "bfd-udp6-input",
1444 .vector_size = sizeof (u32),
1445 .type = VLIB_NODE_TYPE_INTERNAL,
1447 .n_errors = BFD_UDP_N_ERROR,
1448 .error_strings = bfd_udp_error_strings,
1450 .format_trace = bfd_input_format_trace,
1452 .n_next_nodes = BFD_UDP_INPUT_N_NEXT,
1455 [BFD_UDP_INPUT_NEXT_NORMAL] = "error-drop",
1456 [BFD_UDP_INPUT_NEXT_REPLY_ARP] = "ip6-discover-neighbor",
1457 [BFD_UDP_INPUT_NEXT_REPLY_REWRITE] = "ip6-lookup",
1458 [BFD_UDP_INPUT_NEXT_REPLY_MIDCHAIN] = "ip6-midchain",
1464 * Process a frame of bfd echo packets
1465 * Expect 1 packet / frame
1468 bfd_udp_echo_input (vlib_main_t * vm, vlib_node_runtime_t * rt,
1469 vlib_frame_t * f, int is_ipv6)
1471 u32 n_left_from, *from;
1472 bfd_input_trace_t *t0;
1473 bfd_main_t *bm = &bfd_main;
1475 from = vlib_frame_vector_args (f); /* array of buffer indices */
1476 n_left_from = f->n_vectors; /* number of buffer indices */
1478 while (n_left_from > 0)
1485 b0 = vlib_get_buffer (vm, bi0);
1487 /* If this pkt is traced, snapshot the data */
1488 if (b0->flags & VLIB_BUFFER_IS_TRACED)
1491 t0 = vlib_add_trace (vm, rt, b0, sizeof (*t0));
1492 len = (b0->current_length < sizeof (t0->data)) ? b0->current_length
1493 : sizeof (t0->data);
1495 clib_memcpy_fast (t0->data, vlib_buffer_get_current (b0), len);
1499 if (bfd_consume_echo_pkt (vm, bfd_udp_main.bfd_main, b0))
1501 b0->error = rt->errors[BFD_UDP_ERROR_NONE];
1502 next0 = BFD_UDP_ECHO_INPUT_NEXT_NORMAL;
1506 /* loop back the packet */
1507 b0->error = rt->errors[BFD_UDP_ERROR_NONE];
1510 vlib_node_increment_counter (vm, bfd_udp_echo6_input_node.index,
1515 vlib_node_increment_counter (vm, bfd_udp_echo4_input_node.index,
1518 next0 = BFD_UDP_ECHO_INPUT_NEXT_REPLY_REWRITE;
1522 vlib_set_next_frame_buffer (vm, rt, next0, bi0);
1528 return f->n_vectors;
1532 bfd_udp_echo4_input (vlib_main_t * vm, vlib_node_runtime_t * rt,
1535 return bfd_udp_echo_input (vm, rt, f, 0);
1539 bfd_echo_input_format_trace (u8 * s, va_list * args)
1541 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1542 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1543 const bfd_udp_echo_input_trace_t *t =
1544 va_arg (*args, bfd_udp_echo_input_trace_t *);
1545 if (t->len > STRUCT_SIZE_OF (bfd_pkt_t, head))
1547 s = format (s, "BFD ECHO:\n");
1548 s = format (s, " data: %U", format_hexdump, t->data, t->len);
1555 * bfd input graph node declaration
1558 VLIB_REGISTER_NODE (bfd_udp_echo4_input_node, static) = {
1559 .function = bfd_udp_echo4_input,
1560 .name = "bfd-udp-echo4-input",
1561 .vector_size = sizeof (u32),
1562 .type = VLIB_NODE_TYPE_INTERNAL,
1564 .n_errors = BFD_UDP_ECHO_N_ERROR,
1565 .error_strings = bfd_udp_error_strings,
1567 .format_trace = bfd_echo_input_format_trace,
1569 .n_next_nodes = BFD_UDP_ECHO_INPUT_N_NEXT,
1572 [BFD_UDP_ECHO_INPUT_NEXT_NORMAL] = "error-drop",
1573 [BFD_UDP_ECHO_INPUT_NEXT_REPLY_ARP] = "ip4-arp",
1574 [BFD_UDP_ECHO_INPUT_NEXT_REPLY_REWRITE] = "ip4-lookup",
1580 bfd_udp_echo6_input (vlib_main_t * vm, vlib_node_runtime_t * rt,
1583 return bfd_udp_echo_input (vm, rt, f, 1);
1587 VLIB_REGISTER_NODE (bfd_udp_echo6_input_node, static) = {
1588 .function = bfd_udp_echo6_input,
1589 .name = "bfd-udp-echo6-input",
1590 .vector_size = sizeof (u32),
1591 .type = VLIB_NODE_TYPE_INTERNAL,
1593 .n_errors = BFD_UDP_ECHO_N_ERROR,
1594 .error_strings = bfd_udp_echo_error_strings,
1596 .format_trace = bfd_echo_input_format_trace,
1598 .n_next_nodes = BFD_UDP_ECHO_INPUT_N_NEXT,
1601 [BFD_UDP_ECHO_INPUT_NEXT_NORMAL] = "error-drop",
1602 [BFD_UDP_ECHO_INPUT_NEXT_REPLY_ARP] = "ip6-discover-neighbor",
1603 [BFD_UDP_ECHO_INPUT_NEXT_REPLY_REWRITE] = "ip6-lookup",
1609 static clib_error_t *
1610 bfd_udp_sw_if_add_del (vnet_main_t * vnm, u32 sw_if_index, u32 is_create)
1612 bfd_session_t **to_be_freed = NULL;
1613 bfd_udp_main_t *bum = &bfd_udp_main;
1614 BFD_DBG ("sw_if_add_del called, sw_if_index=%u, is_create=%u", sw_if_index,
1619 pool_foreach (bs, bfd_udp_main.bfd_main->sessions)
1621 if (bs->transport != BFD_TRANSPORT_UDP4 &&
1622 bs->transport != BFD_TRANSPORT_UDP6)
1626 if (bs->udp.key.sw_if_index != sw_if_index)
1630 vec_add1 (to_be_freed, bs);
1634 vec_foreach (bs, to_be_freed)
1636 vlib_log_notice (bum->log_class,
1637 "removal of sw_if_index=%u forces removal of bfd session "
1638 "with bs_idx=%u", sw_if_index, (*bs)->bs_idx);
1639 bfd_session_set_flags (vlib_get_main (), *bs, 0);
1640 bfd_udp_del_session_internal (vlib_get_main (), *bs);
1645 VNET_SW_INTERFACE_ADD_DEL_FUNCTION (bfd_udp_sw_if_add_del);
1650 static clib_error_t *
1651 bfd_udp_init (vlib_main_t * vm)
1653 bfd_udp_main.udp4_sessions_count = 0;
1654 bfd_udp_main.udp6_sessions_count = 0;
1655 mhash_init (&bfd_udp_main.bfd_session_idx_by_bfd_key, sizeof (uword),
1656 sizeof (bfd_udp_key_t));
1657 bfd_udp_main.bfd_main = &bfd_main;
1658 bfd_udp_main.vnet_main = vnet_get_main ();
1659 vlib_node_t *node = vlib_get_node_by_name (vm, (u8 *) "ip4-arp");
1661 bfd_udp_main.ip4_arp_idx = node->index;
1662 node = vlib_get_node_by_name (vm, (u8 *) "ip6-discover-neighbor");
1664 bfd_udp_main.ip6_ndp_idx = node->index;
1665 node = vlib_get_node_by_name (vm, (u8 *) "ip4-rewrite");
1667 bfd_udp_main.ip4_rewrite_idx = node->index;
1668 node = vlib_get_node_by_name (vm, (u8 *) "ip6-rewrite");
1670 bfd_udp_main.ip6_rewrite_idx = node->index;
1671 node = vlib_get_node_by_name (vm, (u8 *) "ip4-midchain");
1673 bfd_udp_main.ip4_midchain_idx = node->index;
1674 node = vlib_get_node_by_name (vm, (u8 *) "ip6-midchain");
1676 bfd_udp_main.ip6_midchain_idx = node->index;
1678 bfd_udp_main.log_class = vlib_log_register_class ("bfd", "udp");
1679 vlib_log_debug (bfd_udp_main.log_class, "initialized");
1683 VLIB_INIT_FUNCTION (bfd_udp_init);
1686 * fd.io coding-style-patch-verification: ON
1689 * eval: (c-set-style "gnu")