X-Git-Url: https://gerrit.fd.io/r/gitweb?a=blobdiff_plain;f=src%2Fvnet%2Fbfd%2Fbfd_udp.c;h=0a9106e78e5964e611c056e8cdf733ff58209143;hb=cbe25aab3be72154f2c706c39eeba6a77f34450f;hp=cf05089b430f2883ee7da46fadabc0edbd2a8f45;hpb=68b0fb0c620c7451ef1a6380c43c39de6614db51;p=vpp.git diff --git a/src/vnet/bfd/bfd_udp.c b/src/vnet/bfd/bfd_udp.c index cf05089b430..0a9106e78e5 100644 --- a/src/vnet/bfd/bfd_udp.c +++ b/src/vnet/bfd/bfd_udp.c @@ -12,6 +12,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +/** + * @file + * @brief BFD UDP transport layer implementation + */ #include #include #include @@ -25,6 +29,7 @@ #include #include #include +#include #include #include #include @@ -47,6 +52,24 @@ typedef struct int echo_source_is_set; /* loopback interface used to get echo source ip */ u32 echo_source_sw_if_index; + /* node index of "ip4-arp" node */ + u32 ip4_arp_idx; + /* node index of "ip6-discover-neighbor" node */ + u32 ip6_ndp_idx; + /* node index of "ip4-rewrite" node */ + u32 ip4_rewrite_idx; + /* node index of "ip6-rewrite" node */ + u32 ip6_rewrite_idx; + /* node index of "ip4-midchain" node */ + u32 ip4_midchain_idx; + /* node index of "ip6-midchain" node */ + u32 ip6_midchain_idx; + /* log class */ + vlib_log_class_t log_class; + /* number of active udp4 sessions */ + u32 udp4_sessions_count; + /* number of active udp6 sessions */ + u32 udp6_sessions_count; } bfd_udp_main_t; static vlib_node_registration_t bfd_udp4_input_node; @@ -60,8 +83,7 @@ vnet_api_error_t bfd_udp_set_echo_source (u32 sw_if_index) { vnet_sw_interface_t *sw_if = - vnet_get_sw_interface_safe (bfd_udp_main.vnet_main, - bfd_udp_main.echo_source_sw_if_index); + vnet_get_sw_interface_or_null (bfd_udp_main.vnet_main, sw_if_index); if (sw_if) { bfd_udp_main.echo_source_sw_if_index = sw_if_index; @@ -84,6 +106,7 @@ bfd_udp_is_echo_available (bfd_transport_e transport) { if (!bfd_udp_main.echo_source_is_set) { + BFD_DBG ("UDP echo source not set - echo not available"); return 0; } /* @@ -92,8 +115,8 @@ bfd_udp_is_echo_available (bfd_transport_e transport) * pick an unused address from that subnet */ vnet_sw_interface_t *sw_if = - vnet_get_sw_interface_safe (bfd_udp_main.vnet_main, - bfd_udp_main.echo_source_sw_if_index); + vnet_get_sw_interface_or_null (bfd_udp_main.vnet_main, + bfd_udp_main.echo_source_sw_if_index); if (sw_if && sw_if->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) { if (BFD_TRANSPORT_UDP4 == transport) @@ -127,6 +150,7 @@ bfd_udp_is_echo_available (bfd_transport_e transport) /* *INDENT-ON* */ } } + BFD_DBG ("No usable IP address for UDP echo - echo not available"); return 0; } @@ -144,11 +168,6 @@ bfd_udp_bs_idx_to_sport (u32 bs_idx) return 49152 + bs_idx % (65535 - 49152 + 1); } -static void -lol () -{ -} - int bfd_udp_get_echo_src_ip4 (ip4_address_t * addr) { @@ -169,11 +188,11 @@ bfd_udp_get_echo_src_ip4 (ip4_address_t * addr) if (ia->address_length <= 31) { addr->as_u32 = clib_host_to_net_u32 (x->as_u32); - /* - * flip the last bit to get a different address, might be network, - * we don't care ... - */ - addr->as_u32 ^= 1; + /* + * flip the last bit to get a different address, might be network, + * we don't care ... + */ + addr->as_u32 ^= 1; addr->as_u32 = clib_net_to_host_u32 (addr->as_u32); return 1; } @@ -204,7 +223,6 @@ bfd_udp_get_echo_src_ip6 (ip6_address_t * addr) { *addr = *x; addr->as_u8[15] ^= 1; /* flip the last bit of the address */ - lol (); return 1; } })); @@ -213,16 +231,37 @@ bfd_udp_get_echo_src_ip6 (ip6_address_t * addr) return 0; } +void +bfd_udp_get_echo_source (int *is_set, u32 * sw_if_index, + int *have_usable_ip4, ip4_address_t * ip4, + int *have_usable_ip6, ip6_address_t * ip6) +{ + if (bfd_udp_main.echo_source_is_set) + { + *is_set = 1; + *sw_if_index = bfd_udp_main.echo_source_sw_if_index; + *have_usable_ip4 = bfd_udp_get_echo_src_ip4 (ip4); + *have_usable_ip6 = bfd_udp_get_echo_src_ip6 (ip6); + } + else + { + *is_set = 0; + } +} + int -bfd_add_udp4_transport (vlib_main_t * vm, vlib_buffer_t * b, - const bfd_session_t * bs, int is_echo) +bfd_add_udp4_transport (vlib_main_t * vm, u32 bi, const bfd_session_t * bs, + int is_echo) { const bfd_udp_session_t *bus = &bs->udp; const bfd_udp_key_t *key = &bus->key; + vlib_buffer_t *b = vlib_get_buffer (vm, bi); - b->flags |= VNET_BUFFER_LOCALLY_ORIGINATED; + b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED; vnet_buffer (b)->ip.adj_index[VLIB_RX] = bus->adj_index; vnet_buffer (b)->ip.adj_index[VLIB_TX] = bus->adj_index; + vnet_buffer (b)->sw_if_index[VLIB_RX] = 0; + vnet_buffer (b)->sw_if_index[VLIB_TX] = ~0; typedef struct { ip4_header_t ip4; @@ -231,7 +270,7 @@ bfd_add_udp4_transport (vlib_main_t * vm, vlib_buffer_t * b, ip4_udp_headers *headers = NULL; vlib_buffer_advance (b, -sizeof (*headers)); headers = vlib_buffer_get_current (b); - memset (headers, 0, sizeof (*headers)); + clib_memset (headers, 0, sizeof (*headers)); headers->ip4.ip_version_and_header_length = 0x45; headers->ip4.ttl = 255; headers->ip4.protocol = IP_PROTOCOL_UDP; @@ -266,15 +305,18 @@ bfd_add_udp4_transport (vlib_main_t * vm, vlib_buffer_t * b, } int -bfd_add_udp6_transport (vlib_main_t * vm, vlib_buffer_t * b, - const bfd_session_t * bs, int is_echo) +bfd_add_udp6_transport (vlib_main_t * vm, u32 bi, const bfd_session_t * bs, + int is_echo) { const bfd_udp_session_t *bus = &bs->udp; const bfd_udp_key_t *key = &bus->key; + vlib_buffer_t *b = vlib_get_buffer (vm, bi); - b->flags |= VNET_BUFFER_LOCALLY_ORIGINATED; + b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED; vnet_buffer (b)->ip.adj_index[VLIB_RX] = bus->adj_index; vnet_buffer (b)->ip.adj_index[VLIB_TX] = bus->adj_index; + vnet_buffer (b)->sw_if_index[VLIB_RX] = 0; + vnet_buffer (b)->sw_if_index[VLIB_TX] = 0; typedef struct { ip6_header_t ip6; @@ -283,7 +325,7 @@ bfd_add_udp6_transport (vlib_main_t * vm, vlib_buffer_t * b, ip6_udp_headers *headers = NULL; vlib_buffer_advance (b, -sizeof (*headers)); headers = vlib_buffer_get_current (b); - memset (headers, 0, sizeof (*headers)); + clib_memset (headers, 0, sizeof (*headers)); headers->ip6.ip_version_traffic_class_and_flow_label = clib_host_to_net_u32 (0x6 << 28); headers->ip6.hop_limit = 255; @@ -297,17 +339,17 @@ bfd_add_udp6_transport (vlib_main_t * vm, vlib_buffer_t * b, { return rv; } - clib_memcpy (&headers->ip6.dst_address, &key->local_addr.ip6, - sizeof (headers->ip6.dst_address)); + clib_memcpy_fast (&headers->ip6.dst_address, &key->local_addr.ip6, + sizeof (headers->ip6.dst_address)); headers->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_bfd_echo6); } else { - clib_memcpy (&headers->ip6.src_address, &key->local_addr.ip6, - sizeof (headers->ip6.src_address)); - clib_memcpy (&headers->ip6.dst_address, &key->peer_addr.ip6, - sizeof (headers->ip6.dst_address)); + clib_memcpy_fast (&headers->ip6.src_address, &key->local_addr.ip6, + sizeof (headers->ip6.src_address)); + clib_memcpy_fast (&headers->ip6.dst_address, &key->peer_addr.ip6, + sizeof (headers->ip6.dst_address)); headers->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_bfd6); } @@ -329,6 +371,93 @@ bfd_add_udp6_transport (vlib_main_t * vm, vlib_buffer_t * b, return 1; } +static void +bfd_create_frame_to_next_node (vlib_main_t * vm, u32 bi, u32 next_node) +{ + vlib_frame_t *f = vlib_get_frame_to_node (vm, next_node); + u32 *to_next = vlib_frame_vector_args (f); + to_next[0] = bi; + f->n_vectors = 1; + vlib_put_frame_to_node (vm, next_node, f); +} + +int +bfd_udp_calc_next_node (const struct bfd_session_s *bs, u32 * next_node) +{ + vnet_main_t *vnm = vnet_get_main (); + const bfd_udp_session_t *bus = &bs->udp; + ip_adjacency_t *adj = adj_get (bus->adj_index); + + /* don't try to send the buffer if the interface is not up */ + if (!vnet_sw_interface_is_up (vnm, bus->key.sw_if_index)) + return 0; + + switch (adj->lookup_next_index) + { + case IP_LOOKUP_NEXT_ARP: + switch (bs->transport) + { + case BFD_TRANSPORT_UDP4: + *next_node = bfd_udp_main.ip4_arp_idx; + return 1; + case BFD_TRANSPORT_UDP6: + *next_node = bfd_udp_main.ip6_ndp_idx; + return 1; + } + break; + case IP_LOOKUP_NEXT_REWRITE: + switch (bs->transport) + { + case BFD_TRANSPORT_UDP4: + *next_node = bfd_udp_main.ip4_rewrite_idx; + return 1; + case BFD_TRANSPORT_UDP6: + *next_node = bfd_udp_main.ip6_rewrite_idx; + return 1; + } + break; + case IP_LOOKUP_NEXT_MIDCHAIN: + switch (bs->transport) + { + case BFD_TRANSPORT_UDP4: + *next_node = bfd_udp_main.ip4_midchain_idx; + return 1; + case BFD_TRANSPORT_UDP6: + *next_node = bfd_udp_main.ip6_midchain_idx; + return 1; + } + break; + default: + /* drop */ + break; + } + return 0; +} + +int +bfd_transport_udp4 (vlib_main_t * vm, u32 bi, const struct bfd_session_s *bs) +{ + u32 next_node; + int rv = bfd_udp_calc_next_node (bs, &next_node); + if (rv) + { + bfd_create_frame_to_next_node (vm, bi, next_node); + } + return rv; +} + +int +bfd_transport_udp6 (vlib_main_t * vm, u32 bi, const struct bfd_session_s *bs) +{ + u32 next_node; + int rv = bfd_udp_calc_next_node (bs, &next_node); + if (rv) + { + bfd_create_frame_to_next_node (vm, bi, next_node); + } + return 1; +} + static bfd_session_t * bfd_lookup_session (bfd_udp_main_t * bum, const bfd_udp_key_t * key) { @@ -345,7 +474,7 @@ bfd_udp_key_init (bfd_udp_key_t * key, u32 sw_if_index, const ip46_address_t * local_addr, const ip46_address_t * peer_addr) { - memset (key, 0, sizeof (*key)); + clib_memset (key, 0, sizeof (*key)); key->sw_if_index = sw_if_index; key->local_addr.as_u64[0] = local_addr->as_u64[0]; key->local_addr.as_u64[1] = local_addr->as_u64[1]; @@ -354,8 +483,8 @@ bfd_udp_key_init (bfd_udp_key_t * key, u32 sw_if_index, } static vnet_api_error_t -bfd_udp_add_session_internal (bfd_udp_main_t * bum, u32 sw_if_index, - u32 desired_min_tx_usec, +bfd_udp_add_session_internal (vlib_main_t * vm, bfd_udp_main_t * bum, + u32 sw_if_index, u32 desired_min_tx_usec, u32 required_min_rx_usec, u8 detect_mult, const ip46_address_t * local_addr, const ip46_address_t * peer_addr, @@ -370,17 +499,17 @@ bfd_udp_add_session_internal (bfd_udp_main_t * bum, u32 sw_if_index, bfd_session_t *bs = bfd_get_session (bum->bfd_main, t); if (!bs) { - bfd_put_session (bum->bfd_main, bs); return VNET_API_ERROR_BFD_EAGAIN; } bfd_udp_session_t *bus = &bs->udp; - memset (bus, 0, sizeof (*bus)); + clib_memset (bus, 0, sizeof (*bus)); bfd_udp_key_t *key = &bus->key; bfd_udp_key_init (key, sw_if_index, local_addr, peer_addr); const bfd_session_t *tmp = bfd_lookup_session (bum, key); if (tmp) { - clib_warning ("duplicate bfd-udp session, existing bs_idx=%d", + vlib_log_err (bum->log_class, + "duplicate bfd-udp session, existing bs_idx=%d", tmp->bs_idx); bfd_put_session (bum->bfd_main, bs); return VNET_API_ERROR_BFD_EEXIST; @@ -390,6 +519,8 @@ bfd_udp_add_session_internal (bfd_udp_main_t * bum, u32 sw_if_index, bs->bs_idx, key->sw_if_index, format_ip46_address, &key->local_addr, IP46_TYPE_ANY, format_ip46_address, &key->peer_addr, IP46_TYPE_ANY); + vlib_log_info (bum->log_class, "create BFD session: %U", + format_bfd_session, bs); if (BFD_TRANSPORT_UDP4 == t) { bus->adj_index = adj_nbr_add_or_lock (FIB_PROTOCOL_IP4, VNET_LINK_IP4, @@ -398,21 +529,14 @@ bfd_udp_add_session_internal (bfd_udp_main_t * bum, u32 sw_if_index, BFD_DBG ("adj_nbr_add_or_lock(FIB_PROTOCOL_IP4, VNET_LINK_IP4, %U, %d) " "returns %d", format_ip46_address, &key->peer_addr, IP46_TYPE_ANY, key->sw_if_index, bus->adj_index); - - fib_prefix_t fib_prefix; - memset (&fib_prefix, 0, sizeof (fib_prefix)); - fib_prefix.fp_len = 0; - fib_prefix.fp_proto = FIB_PROTOCOL_IP4; - fib_prefix.fp_addr = key->local_addr; - u32 fib_index = fib_table_find (FIB_PROTOCOL_IP4, 0); /* FIXME table id 0? */ - dpo_id_t dpo = DPO_INVALID; - dpo_proto_t dproto; - dproto = fib_proto_to_dpo (fib_prefix.fp_proto); - receive_dpo_add_or_lock (dproto, ~0, NULL, &dpo); - fib_table_entry_special_dpo_update (fib_index, &fib_prefix, - FIB_SOURCE_API, - FIB_ENTRY_FLAG_LOCAL, &dpo); - dpo_reset (&dpo); + ++bum->udp4_sessions_count; + if (1 == bum->udp4_sessions_count) + { + udp_register_dst_port (vm, UDP_DST_PORT_bfd4, + bfd_udp4_input_node.index, 1); + udp_register_dst_port (vm, UDP_DST_PORT_bfd_echo4, + bfd_udp_echo4_input_node.index, 1); + } } else { @@ -422,6 +546,14 @@ bfd_udp_add_session_internal (bfd_udp_main_t * bum, u32 sw_if_index, BFD_DBG ("adj_nbr_add_or_lock(FIB_PROTOCOL_IP6, VNET_LINK_IP6, %U, %d) " "returns %d", format_ip46_address, &key->peer_addr, IP46_TYPE_ANY, key->sw_if_index, bus->adj_index); + ++bum->udp6_sessions_count; + if (1 == bum->udp6_sessions_count) + { + udp_register_dst_port (vm, UDP_DST_PORT_bfd6, + bfd_udp6_input_node.index, 0); + udp_register_dst_port (vm, UDP_DST_PORT_bfd_echo6, + bfd_udp_echo6_input_node.index, 0); + } } *bs_out = bs; return bfd_session_set_params (bum->bfd_main, bs, desired_min_tx_usec, @@ -433,20 +565,24 @@ bfd_udp_validate_api_input (u32 sw_if_index, const ip46_address_t * local_addr, const ip46_address_t * peer_addr) { + bfd_udp_main_t *bum = &bfd_udp_main; vnet_sw_interface_t *sw_if = - vnet_get_sw_interface_safe (bfd_udp_main.vnet_main, sw_if_index); + vnet_get_sw_interface_or_null (bfd_udp_main.vnet_main, sw_if_index); u8 local_ip_valid = 0; ip_interface_address_t *ia = NULL; if (!sw_if) { - clib_warning ("got NULL sw_if"); + vlib_log_err (bum->log_class, + "got NULL sw_if when getting interface by index %u", + sw_if_index); return VNET_API_ERROR_INVALID_SW_IF_INDEX; } if (ip46_address_is_ip4 (local_addr)) { if (!ip46_address_is_ip4 (peer_addr)) { - clib_warning ("IP family mismatch"); + vlib_log_err (bum->log_class, + "IP family mismatch (local is ipv4, peer is ipv6)"); return VNET_API_ERROR_INVALID_ARGUMENT; } ip4_main_t *im = &ip4_main; @@ -469,29 +605,47 @@ bfd_udp_validate_api_input (u32 sw_if_index, { if (ip46_address_is_ip4 (peer_addr)) { - clib_warning ("IP family mismatch"); + vlib_log_err (bum->log_class, + "IP family mismatch (local is ipv6, peer is ipv4)"); return VNET_API_ERROR_INVALID_ARGUMENT; } - ip6_main_t *im = &ip6_main; - /* *INDENT-OFF* */ - foreach_ip_interface_address ( - &im->lookup_main, ia, sw_if_index, 0 /* honor unnumbered */, ({ - ip6_address_t *x = - ip_interface_address_get_address (&im->lookup_main, ia); - if (local_addr->ip6.as_u64[0] == x->as_u64[0] && - local_addr->ip6.as_u64[1] == x->as_u64[1]) - { - /* valid address for this interface */ - local_ip_valid = 1; - break; - } - })); - /* *INDENT-ON* */ + + if (ip6_address_is_link_local_unicast (&local_addr->ip6)) + { + const ip6_address_t *ll_addr; + ll_addr = ip6_get_link_local_address (sw_if_index); + if (ip6_address_is_equal (ll_addr, &local_addr->ip6)) + { + /* valid address for this interface */ + local_ip_valid = 1; + } + } + else + { + ip6_main_t *im = &ip6_main; + /* *INDENT-OFF* */ + foreach_ip_interface_address ( + &im->lookup_main, ia, sw_if_index, 0 /* honor unnumbered */, ({ + ip6_address_t *x = + ip_interface_address_get_address (&im->lookup_main, ia); + if (local_addr->ip6.as_u64[0] == x->as_u64[0] && + local_addr->ip6.as_u64[1] == x->as_u64[1]) + { + /* valid address for this interface */ + local_ip_valid = 1; + break; + } + })); + /* *INDENT-ON* */ + } } if (!local_ip_valid) { - clib_warning ("address not found on interface"); + vlib_log_err (bum->log_class, + "local address %U not found on interface with index %u", + format_ip46_address, local_addr, IP46_TYPE_ANY, + sw_if_index); return VNET_API_ERROR_ADDRESS_NOT_FOUND_FOR_INTERFACE; } @@ -518,10 +672,11 @@ bfd_udp_find_session_by_api_input (u32 sw_if_index, } else { - clib_warning - ("BFD session not found (sw_if_index=%u, local=%U, peer=%U", - sw_if_index, format_ip46_address, local_addr, IP46_TYPE_ANY, - format_ip46_address, peer_addr, IP46_TYPE_ANY); + vlib_log_err (bum->log_class, + "BFD session not found, sw_if_index=%u, local=%U, peer=%U", + sw_if_index, format_ip46_address, local_addr, + IP46_TYPE_ANY, format_ip46_address, peer_addr, + IP46_TYPE_ANY); return VNET_API_ERROR_BFD_ENOENT; } } @@ -534,6 +689,7 @@ bfd_api_verify_common (u32 sw_if_index, u32 desired_min_tx_usec, const ip46_address_t * local_addr, const ip46_address_t * peer_addr) { + bfd_udp_main_t *bum = &bfd_udp_main; vnet_api_error_t rv = bfd_udp_validate_api_input (sw_if_index, local_addr, peer_addr); if (rv) @@ -542,24 +698,43 @@ bfd_api_verify_common (u32 sw_if_index, u32 desired_min_tx_usec, } if (detect_mult < 1) { - clib_warning ("detect_mult < 1"); + vlib_log_err (bum->log_class, "detect_mult < 1"); return VNET_API_ERROR_INVALID_ARGUMENT; } if (desired_min_tx_usec < 1) { - clib_warning ("desired_min_tx_usec < 1"); + vlib_log_err (bum->log_class, "desired_min_tx_usec < 1"); return VNET_API_ERROR_INVALID_ARGUMENT; } return 0; } static void -bfd_udp_del_session_internal (bfd_session_t * bs) +bfd_udp_del_session_internal (vlib_main_t * vm, bfd_session_t * bs) { bfd_udp_main_t *bum = &bfd_udp_main; BFD_DBG ("free bfd-udp session, bs_idx=%d", bs->bs_idx); mhash_unset (&bum->bfd_session_idx_by_bfd_key, &bs->udp.key, NULL); adj_unlock (bs->udp.adj_index); + switch (bs->transport) + { + case BFD_TRANSPORT_UDP4: + --bum->udp4_sessions_count; + if (!bum->udp4_sessions_count) + { + udp_unregister_dst_port (vm, UDP_DST_PORT_bfd4, 1); + udp_unregister_dst_port (vm, UDP_DST_PORT_bfd_echo4, 1); + } + break; + case BFD_TRANSPORT_UDP6: + --bum->udp6_sessions_count; + if (!bum->udp6_sessions_count) + { + udp_unregister_dst_port (vm, UDP_DST_PORT_bfd6, 0); + udp_unregister_dst_port (vm, UDP_DST_PORT_bfd_echo6, 0); + } + break; + } bfd_put_session (bum->bfd_main, bs); } @@ -570,6 +745,9 @@ bfd_udp_add_session (u32 sw_if_index, const ip46_address_t * local_addr, u8 detect_mult, u8 is_authenticated, u32 conf_key_id, u8 bfd_key_id) { + bfd_main_t *bm = &bfd_main; + bfd_lock (bm); + vnet_api_error_t rv = bfd_api_verify_common (sw_if_index, desired_min_tx_usec, required_min_rx_usec, detect_mult, @@ -578,8 +756,8 @@ bfd_udp_add_session (u32 sw_if_index, const ip46_address_t * local_addr, if (!rv) { rv = - bfd_udp_add_session_internal (&bfd_udp_main, sw_if_index, - desired_min_tx_usec, + bfd_udp_add_session_internal (vlib_get_main (), &bfd_udp_main, + sw_if_index, desired_min_tx_usec, required_min_rx_usec, detect_mult, local_addr, peer_addr, &bs); } @@ -589,12 +767,13 @@ bfd_udp_add_session (u32 sw_if_index, const ip46_address_t * local_addr, rv = bfd_auth_activate (bs, conf_key_id, bfd_key_id, 0 /* is not delayed */ ); #else - clib_warning ("SSL missing, cannot add authenticated BFD session"); + vlib_log_err (bfd_udp_main.log_class, + "SSL missing, cannot add authenticated BFD session"); rv = VNET_API_ERROR_BFD_NOTSUPP; #endif if (rv) { - bfd_udp_del_session_internal (bs); + bfd_udp_del_session_internal (vlib_get_main (), bs); } } if (!rv) @@ -602,6 +781,7 @@ bfd_udp_add_session (u32 sw_if_index, const ip46_address_t * local_addr, bfd_session_start (bfd_udp_main.bfd_main, bs); } + bfd_unlock (bm); return rv; } @@ -613,17 +793,23 @@ bfd_udp_mod_session (u32 sw_if_index, u32 required_min_rx_usec, u8 detect_mult) { bfd_session_t *bs = NULL; + bfd_main_t *bm = &bfd_main; + vnet_api_error_t error; + bfd_lock (bm); vnet_api_error_t rv = bfd_udp_find_session_by_api_input (sw_if_index, local_addr, peer_addr, &bs); if (rv) { + bfd_unlock (bm); return rv; } - return bfd_session_set_params (bfd_udp_main.bfd_main, bs, - desired_min_tx_usec, required_min_rx_usec, - detect_mult); + error = bfd_session_set_params (bfd_udp_main.bfd_main, bs, + desired_min_tx_usec, required_min_rx_usec, + detect_mult); + bfd_unlock (bm); + return error; } vnet_api_error_t @@ -632,14 +818,18 @@ bfd_udp_del_session (u32 sw_if_index, const ip46_address_t * peer_addr) { bfd_session_t *bs = NULL; + bfd_main_t *bm = &bfd_main; + bfd_lock (bm); vnet_api_error_t rv = bfd_udp_find_session_by_api_input (sw_if_index, local_addr, peer_addr, &bs); if (rv) { + bfd_unlock (bm); return rv; } - bfd_udp_del_session_internal (bs); + bfd_udp_del_session_internal (vlib_get_main (), bs); + bfd_unlock (bm); return 0; } @@ -649,114 +839,31 @@ bfd_udp_session_set_flags (u32 sw_if_index, const ip46_address_t * peer_addr, u8 admin_up_down) { bfd_session_t *bs = NULL; + bfd_main_t *bm = &bfd_main; + bfd_lock (bm); vnet_api_error_t rv = bfd_udp_find_session_by_api_input (sw_if_index, local_addr, peer_addr, &bs); if (rv) { + bfd_unlock (bm); return rv; } bfd_session_set_flags (bs, admin_up_down); + bfd_unlock (bm); return 0; } -vnet_api_error_t -bfd_auth_set_key (u32 conf_key_id, u8 auth_type, u8 key_len, - const u8 * key_data) -{ -#if WITH_LIBSSL > 0 - bfd_auth_key_t *auth_key = NULL; - if (!key_len || key_len > bfd_max_len_for_auth_type (auth_type)) - { - clib_warning ("Invalid authentication key length for auth_type=%d:%s " - "(key_len=%u, must be " - "non-zero, expected max=%u)", - auth_type, bfd_auth_type_str (auth_type), key_len, - (u32) bfd_max_len_for_auth_type (auth_type)); - return VNET_API_ERROR_INVALID_VALUE; - } - if (!bfd_auth_type_supported (auth_type)) - { - clib_warning ("Unsupported auth type=%d:%s", auth_type, - bfd_auth_type_str (auth_type)); - return VNET_API_ERROR_BFD_NOTSUPP; - } - bfd_main_t *bm = bfd_udp_main.bfd_main; - uword *key_idx_p = hash_get (bm->auth_key_by_conf_key_id, conf_key_id); - if (key_idx_p) - { - /* modifying existing key - must not be used */ - const uword key_idx = *key_idx_p; - auth_key = pool_elt_at_index (bm->auth_keys, key_idx); - if (auth_key->use_count > 0) - { - clib_warning ("Authentication key with conf ID %u in use by %u BFD " - "sessions - cannot modify", - conf_key_id, auth_key->use_count); - return VNET_API_ERROR_BFD_EINUSE; - } - } - else - { - /* adding new key */ - pool_get (bm->auth_keys, auth_key); - auth_key->conf_key_id = conf_key_id; - hash_set (bm->auth_key_by_conf_key_id, conf_key_id, - auth_key - bm->auth_keys); - } - auth_key->auth_type = auth_type; - memset (auth_key->key, 0, sizeof (auth_key->key)); - clib_memcpy (auth_key->key, key_data, key_len); - return 0; -#else - clib_warning ("SSL missing, cannot manipulate authentication keys"); - return VNET_API_ERROR_BFD_NOTSUPP; -#endif -} - -vnet_api_error_t -bfd_auth_del_key (u32 conf_key_id) -{ -#if WITH_LIBSSL > 0 - bfd_auth_key_t *auth_key = NULL; - bfd_main_t *bm = bfd_udp_main.bfd_main; - uword *key_idx_p = hash_get (bm->auth_key_by_conf_key_id, conf_key_id); - if (key_idx_p) - { - /* deleting existing key - must not be used */ - const uword key_idx = *key_idx_p; - auth_key = pool_elt_at_index (bm->auth_keys, key_idx); - if (auth_key->use_count > 0) - { - clib_warning ("Authentication key with conf ID %u in use by %u BFD " - "sessions - cannot delete", - conf_key_id, auth_key->use_count); - return VNET_API_ERROR_BFD_EINUSE; - } - hash_unset (bm->auth_key_by_conf_key_id, conf_key_id); - memset (auth_key, 0, sizeof (*auth_key)); - pool_put (bm->auth_keys, auth_key); - } - else - { - /* no such key */ - clib_warning ("Authentication key with conf ID %u does not exist", - conf_key_id); - return VNET_API_ERROR_BFD_ENOENT; - } - return 0; -#else - clib_warning ("SSL missing, cannot manipulate authentication keys"); - return VNET_API_ERROR_BFD_NOTSUPP; -#endif -} - vnet_api_error_t bfd_udp_auth_activate (u32 sw_if_index, const ip46_address_t * local_addr, const ip46_address_t * peer_addr, u32 conf_key_id, u8 key_id, u8 is_delayed) { + bfd_main_t *bm = &bfd_main; + bfd_lock (bm); + vnet_api_error_t error; + #if WITH_LIBSSL > 0 bfd_session_t *bs = NULL; vnet_api_error_t rv = @@ -764,11 +871,16 @@ bfd_udp_auth_activate (u32 sw_if_index, &bs); if (rv) { + bfd_unlock (bm); return rv; } - return bfd_auth_activate (bs, conf_key_id, key_id, is_delayed); + error = bfd_auth_activate (bs, conf_key_id, key_id, is_delayed); + bfd_unlock (bm); + return error; #else - clib_warning ("SSL missing, cannot activate BFD authentication"); + vlib_log_err (bfd_udp_main->log_class, + "SSL missing, cannot activate BFD authentication"); + bfd_unlock (bm); return VNET_API_ERROR_BFD_NOTSUPP; #endif } @@ -778,21 +890,29 @@ bfd_udp_auth_deactivate (u32 sw_if_index, const ip46_address_t * local_addr, const ip46_address_t * peer_addr, u8 is_delayed) { + bfd_main_t *bm = &bfd_main; + vnet_api_error_t error; + bfd_lock (bm); bfd_session_t *bs = NULL; vnet_api_error_t rv = bfd_udp_find_session_by_api_input (sw_if_index, local_addr, peer_addr, &bs); if (rv) { + bfd_unlock (bm); return rv; } - return bfd_auth_deactivate (bs, is_delayed); + error = bfd_auth_deactivate (bs, is_delayed); + bfd_unlock (bm); + return error; } typedef enum { BFD_UDP_INPUT_NEXT_NORMAL, - BFD_UDP_INPUT_NEXT_REPLY, + BFD_UDP_INPUT_NEXT_REPLY_ARP, + BFD_UDP_INPUT_NEXT_REPLY_REWRITE, + BFD_UDP_INPUT_NEXT_REPLY_MIDCHAIN, BFD_UDP_INPUT_N_NEXT, } bfd_udp_input_next_t; @@ -819,6 +939,14 @@ typedef enum BFD_UDP_N_ERROR, } bfd_udp_error_t; +typedef enum +{ + BFD_UDP_ECHO_INPUT_NEXT_NORMAL, + BFD_UDP_ECHO_INPUT_NEXT_REPLY_ARP, + BFD_UDP_ECHO_INPUT_NEXT_REPLY_REWRITE, + BFD_UDP_ECHO_INPUT_N_NEXT, +} bfd_udp_echo_input_next_t; + /* Packet counters - BFD ECHO packets */ #define foreach_bfd_udp_echo_error(F) \ F (NONE, "good bfd echo packets (processed)") \ @@ -847,7 +975,7 @@ bfd_udp4_find_headers (vlib_buffer_t * b, ip4_header_t ** ip4, udp_header_t ** udp) { /* sanity check first */ - const i32 start = vnet_buffer (b)->ip.start_of_ip_header; + const i32 start = vnet_buffer (b)->l3_hdr_offset; if (start < 0 && start < sizeof (b->pre_data)) { BFD_ERR ("Start of ip header is before pre_data, ignoring"); @@ -907,23 +1035,13 @@ typedef struct bfd_pkt_t pkt; } bfd_rpc_update_t; -static void -bfd_rpc_update_session_cb (const bfd_rpc_update_t * a) -{ - bfd_consume_pkt (bfd_udp_main.bfd_main, &a->pkt, a->bs_idx); -} - static void bfd_rpc_update_session (u32 bs_idx, const bfd_pkt_t * pkt) { - /* packet length was already verified to be correct by the caller */ - const u32 data_size = sizeof (bfd_rpc_update_t) - - STRUCT_SIZE_OF (bfd_rpc_update_t, pkt) + pkt->head.length; - u8 data[data_size]; - bfd_rpc_update_t *update = (bfd_rpc_update_t *) data; - update->bs_idx = bs_idx; - clib_memcpy (&update->pkt, pkt, pkt->head.length); - vl_api_rpc_call_main_thread (bfd_rpc_update_session_cb, data, data_size); + bfd_main_t *bm = &bfd_main; + bfd_lock (bm); + bfd_consume_pkt (bm, pkt, bs_idx); + bfd_unlock (bm); } static bfd_udp_error_t @@ -968,7 +1086,7 @@ bfd_udp4_scan (vlib_main_t * vm, vlib_node_runtime_t * rt, else { bfd_udp_key_t key; - memset (&key, 0, sizeof (key)); + clib_memset (&key, 0, sizeof (key)); key.sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX]; key.local_addr.ip4.as_u32 = ip4->dst_address.as_u32; key.peer_addr.ip4.as_u32 = ip4->src_address.as_u32; @@ -1004,7 +1122,7 @@ bfd_udp6_find_headers (vlib_buffer_t * b, ip6_header_t ** ip6, udp_header_t ** udp) { /* sanity check first */ - const i32 start = vnet_buffer (b)->ip.start_of_ip_header; + const i32 start = vnet_buffer (b)->l3_hdr_offset; if (start < 0 && start < sizeof (b->pre_data)) { BFD_ERR ("Start of ip header is before pre_data, ignoring"); @@ -1110,7 +1228,7 @@ bfd_udp6_scan (vlib_main_t * vm, vlib_node_runtime_t * rt, else { bfd_udp_key_t key; - memset (&key, 0, sizeof (key)); + clib_memset (&key, 0, sizeof (key)); key.sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX]; key.local_addr.ip6.as_u64[0] = ip6->dst_address.as_u64[0]; key.local_addr.ip6.as_u64[1] = ip6->dst_address.as_u64[1]; @@ -1153,6 +1271,7 @@ bfd_udp_input (vlib_main_t * vm, vlib_node_runtime_t * rt, { u32 n_left_from, *from; bfd_input_trace_t *t0; + bfd_main_t *bm = &bfd_main; from = vlib_frame_vector_args (f); /* array of buffer indices */ n_left_from = f->n_vectors; /* number of buffer indices */ @@ -1176,10 +1295,11 @@ bfd_udp_input (vlib_main_t * vm, vlib_node_runtime_t * rt, len = (b0->current_length < sizeof (t0->data)) ? b0->current_length : sizeof (t0->data); t0->len = len; - clib_memcpy (t0->data, vlib_buffer_get_current (b0), len); + clib_memcpy_fast (t0->data, vlib_buffer_get_current (b0), len); } /* scan this bfd pkt. error0 is the counter index to bmp */ + bfd_lock (bm); if (is_ipv6) { error0 = bfd_udp6_scan (vm, rt, b0, &bs); @@ -1201,8 +1321,10 @@ bfd_udp_input (vlib_main_t * vm, vlib_node_runtime_t * rt, const bfd_pkt_t *pkt = vlib_buffer_get_current (b0); if (bfd_pkt_get_poll (pkt)) { - bfd_init_final_control_frame (vm, b0, bfd_udp_main.bfd_main, - bs); + b0->current_data = 0; + b0->current_length = 0; + bfd_init_final_control_frame (vm, b0, bfd_udp_main.bfd_main, bs, + 0); if (is_ipv6) { vlib_node_increment_counter (vm, bfd_udp6_input_node.index, @@ -1213,9 +1335,26 @@ bfd_udp_input (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_node_increment_counter (vm, bfd_udp4_input_node.index, b0->error, 1); } - next0 = BFD_UDP_INPUT_NEXT_REPLY; + const bfd_udp_session_t *bus = &bs->udp; + ip_adjacency_t *adj = adj_get (bus->adj_index); + switch (adj->lookup_next_index) + { + case IP_LOOKUP_NEXT_ARP: + next0 = BFD_UDP_INPUT_NEXT_REPLY_ARP; + break; + case IP_LOOKUP_NEXT_REWRITE: + next0 = BFD_UDP_INPUT_NEXT_REPLY_REWRITE; + break; + case IP_LOOKUP_NEXT_MIDCHAIN: + next0 = BFD_UDP_INPUT_NEXT_REPLY_MIDCHAIN; + break; + default: + /* drop */ + break; + } } } + bfd_unlock (bm); vlib_set_next_frame_buffer (vm, rt, next0, bi0); from += 1; @@ -1250,7 +1389,9 @@ VLIB_REGISTER_NODE (bfd_udp4_input_node, static) = { .next_nodes = { [BFD_UDP_INPUT_NEXT_NORMAL] = "error-drop", - [BFD_UDP_INPUT_NEXT_REPLY] = "ip4-lookup", + [BFD_UDP_INPUT_NEXT_REPLY_ARP] = "ip4-arp", + [BFD_UDP_INPUT_NEXT_REPLY_REWRITE] = "ip4-lookup", + [BFD_UDP_INPUT_NEXT_REPLY_MIDCHAIN] = "ip4-midchain", }, }; /* *INDENT-ON* */ @@ -1277,7 +1418,9 @@ VLIB_REGISTER_NODE (bfd_udp6_input_node, static) = { .next_nodes = { [BFD_UDP_INPUT_NEXT_NORMAL] = "error-drop", - [BFD_UDP_INPUT_NEXT_REPLY] = "ip6-lookup", + [BFD_UDP_INPUT_NEXT_REPLY_ARP] = "ip6-discover-neighbor", + [BFD_UDP_INPUT_NEXT_REPLY_REWRITE] = "ip6-lookup", + [BFD_UDP_INPUT_NEXT_REPLY_MIDCHAIN] = "ip6-midchain", }, }; /* *INDENT-ON* */ @@ -1292,6 +1435,7 @@ bfd_udp_echo_input (vlib_main_t * vm, vlib_node_runtime_t * rt, { u32 n_left_from, *from; bfd_input_trace_t *t0; + bfd_main_t *bm = &bfd_main; from = vlib_frame_vector_args (f); /* array of buffer indices */ n_left_from = f->n_vectors; /* number of buffer indices */ @@ -1313,13 +1457,14 @@ bfd_udp_echo_input (vlib_main_t * vm, vlib_node_runtime_t * rt, len = (b0->current_length < sizeof (t0->data)) ? b0->current_length : sizeof (t0->data); t0->len = len; - clib_memcpy (t0->data, vlib_buffer_get_current (b0), len); + clib_memcpy_fast (t0->data, vlib_buffer_get_current (b0), len); } + bfd_lock (bm); if (bfd_consume_echo_pkt (bfd_udp_main.bfd_main, b0)) { b0->error = rt->errors[BFD_UDP_ERROR_NONE]; - next0 = BFD_UDP_INPUT_NEXT_NORMAL; + next0 = BFD_UDP_ECHO_INPUT_NEXT_NORMAL; } else { @@ -1335,9 +1480,10 @@ bfd_udp_echo_input (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_node_increment_counter (vm, bfd_udp_echo4_input_node.index, b0->error, 1); } - next0 = BFD_UDP_INPUT_NEXT_REPLY; + next0 = BFD_UDP_ECHO_INPUT_NEXT_REPLY_REWRITE; } + bfd_unlock (bm); vlib_set_next_frame_buffer (vm, rt, next0, bi0); from += 1; @@ -1385,11 +1531,12 @@ VLIB_REGISTER_NODE (bfd_udp_echo4_input_node, static) = { .format_trace = bfd_echo_input_format_trace, - .n_next_nodes = BFD_UDP_INPUT_N_NEXT, + .n_next_nodes = BFD_UDP_ECHO_INPUT_N_NEXT, .next_nodes = { - [BFD_UDP_INPUT_NEXT_NORMAL] = "error-drop", - [BFD_UDP_INPUT_NEXT_REPLY] = "ip4-lookup", + [BFD_UDP_ECHO_INPUT_NEXT_NORMAL] = "error-drop", + [BFD_UDP_ECHO_INPUT_NEXT_REPLY_ARP] = "ip4-arp", + [BFD_UDP_ECHO_INPUT_NEXT_REPLY_REWRITE] = "ip4-lookup", }, }; /* *INDENT-ON* */ @@ -1413,40 +1560,52 @@ VLIB_REGISTER_NODE (bfd_udp_echo6_input_node, static) = { .format_trace = bfd_echo_input_format_trace, - .n_next_nodes = BFD_UDP_INPUT_N_NEXT, + .n_next_nodes = BFD_UDP_ECHO_INPUT_N_NEXT, .next_nodes = { - [BFD_UDP_INPUT_NEXT_NORMAL] = "error-drop", - [BFD_UDP_INPUT_NEXT_REPLY] = "ip6-lookup", + [BFD_UDP_ECHO_INPUT_NEXT_NORMAL] = "error-drop", + [BFD_UDP_ECHO_INPUT_NEXT_REPLY_ARP] = "ip6-discover-neighbor", + [BFD_UDP_ECHO_INPUT_NEXT_REPLY_REWRITE] = "ip6-lookup", }, }; /* *INDENT-ON* */ static clib_error_t * -bfd_sw_interface_up_down (vnet_main_t * vnm, u32 sw_if_index, u32 flags) +bfd_udp_sw_if_add_del (vnet_main_t * vnm, u32 sw_if_index, u32 is_create) { - // vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm, sw_if_index); - if (!(flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)) - { - /* TODO */ - } - return 0; -} - -VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (bfd_sw_interface_up_down); - -static clib_error_t * -bfd_hw_interface_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags) -{ - if (flags & VNET_HW_INTERFACE_FLAG_LINK_UP) - { - /* TODO */ - } + bfd_session_t **to_be_freed = NULL; + bfd_udp_main_t *bum = &bfd_udp_main; + BFD_DBG ("sw_if_add_del called, sw_if_index=%u, is_create=%u", sw_if_index, + is_create); + if (!is_create) + { + bfd_session_t *bs; + pool_foreach (bs, bfd_udp_main.bfd_main->sessions, + { + if (bs->transport != BFD_TRANSPORT_UDP4 && + bs->transport != BFD_TRANSPORT_UDP6) + { + continue;} + if (bs->udp.key.sw_if_index != sw_if_index) + { + continue;} + vec_add1 (to_be_freed, bs);} + ); + } + bfd_session_t **bs; + vec_foreach (bs, to_be_freed) + { + vlib_log_notice (bum->log_class, + "removal of sw_if_index=%u forces removal of bfd session " + "with bs_idx=%u", sw_if_index, (*bs)->bs_idx); + bfd_session_set_flags (*bs, 0); + bfd_udp_del_session_internal (vlib_get_main (), *bs); + } return 0; } -VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION (bfd_hw_interface_up_down); +VNET_SW_INTERFACE_ADD_DEL_FUNCTION (bfd_udp_sw_if_add_del); /* * setup function @@ -1454,16 +1613,33 @@ VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION (bfd_hw_interface_up_down); static clib_error_t * bfd_udp_init (vlib_main_t * vm) { + bfd_udp_main.udp4_sessions_count = 0; + bfd_udp_main.udp6_sessions_count = 0; mhash_init (&bfd_udp_main.bfd_session_idx_by_bfd_key, sizeof (uword), sizeof (bfd_udp_key_t)); bfd_udp_main.bfd_main = &bfd_main; bfd_udp_main.vnet_main = vnet_get_main (); - udp_register_dst_port (vm, UDP_DST_PORT_bfd4, bfd_udp4_input_node.index, 1); - udp_register_dst_port (vm, UDP_DST_PORT_bfd6, bfd_udp6_input_node.index, 0); - udp_register_dst_port (vm, UDP_DST_PORT_bfd_echo4, - bfd_udp_echo4_input_node.index, 1); - udp_register_dst_port (vm, UDP_DST_PORT_bfd_echo6, - bfd_udp_echo6_input_node.index, 0); + vlib_node_t *node = vlib_get_node_by_name (vm, (u8 *) "ip4-arp"); + ASSERT (node); + bfd_udp_main.ip4_arp_idx = node->index; + node = vlib_get_node_by_name (vm, (u8 *) "ip6-discover-neighbor"); + ASSERT (node); + bfd_udp_main.ip6_ndp_idx = node->index; + node = vlib_get_node_by_name (vm, (u8 *) "ip4-rewrite"); + ASSERT (node); + bfd_udp_main.ip4_rewrite_idx = node->index; + node = vlib_get_node_by_name (vm, (u8 *) "ip6-rewrite"); + ASSERT (node); + bfd_udp_main.ip6_rewrite_idx = node->index; + node = vlib_get_node_by_name (vm, (u8 *) "ip4-midchain"); + ASSERT (node); + bfd_udp_main.ip4_midchain_idx = node->index; + node = vlib_get_node_by_name (vm, (u8 *) "ip6-midchain"); + ASSERT (node); + bfd_udp_main.ip6_midchain_idx = node->index; + + bfd_udp_main.log_class = vlib_log_register_class ("bfd", "udp"); + vlib_log_debug (bfd_udp_main.log_class, "initialized"); return 0; }