X-Git-Url: https://gerrit.fd.io/r/gitweb?a=blobdiff_plain;f=vnet%2Fvnet%2Fethernet%2Farp.c;h=ec138586ff4841ee806b282fa21a171218254b0b;hb=3e046ea96e7e9d98a8dd67eab84031e1d71b4422;hp=1346d605ea8872f3c279db955a963234bdfa242b;hpb=e70dcc8676b6b2db74989e43f116bbb200239bf8;p=vpp.git diff --git a/vnet/vnet/ethernet/arp.c b/vnet/vnet/ethernet/arp.c index 1346d605ea8..ec138586ff4 100644 --- a/vnet/vnet/ethernet/arp.c +++ b/vnet/vnet/ethernet/arp.c @@ -16,33 +16,37 @@ */ #include +#include #include #include #include #include +#include +#include +#include -void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length); - -typedef struct -{ - u32 sw_if_index; - u32 fib_index; - ip4_address_t ip4_address; -} ethernet_arp_ip4_key_t; - -typedef struct -{ - ethernet_arp_ip4_key_t key; - u8 ethernet_address[6]; +/** + * @file + * @brief IPv4 ARP. + * + * This file contains code to manage the IPv4 ARP tables (IP Address + * to MAC Address lookup). + */ - u16 flags; -#define ETHERNET_ARP_IP4_ENTRY_FLAG_STATIC (1 << 0) -#define ETHERNET_ARP_IP4_ENTRY_FLAG_GLEAN (2 << 0) - u64 cpu_time_last_updated; +void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length); - u32 *adjacencies; -} ethernet_arp_ip4_entry_t; +/** + * @brief Per-interface ARP configuration and state + */ +typedef struct ethernet_arp_interface_t_ +{ + /** + * Hash table of ARP entries. + * Since this hash table is per-interface, the key is only the IPv4 address. + */ + uword *arp_entries; +} ethernet_arp_interface_t; typedef struct { @@ -77,18 +81,34 @@ typedef struct ethernet_arp_ip4_entry_t *ip4_entry_pool; - mhash_t ip4_entry_by_key; - /* ARP attack mitigation */ u32 arp_delete_rotor; u32 limit_arp_cache_size; + /** Per interface state */ + ethernet_arp_interface_t *ethernet_arp_by_sw_if_index; + /* Proxy arp vector */ ethernet_proxy_arp_t *proxy_arps; } ethernet_arp_main_t; static ethernet_arp_main_t ethernet_arp_main; +typedef struct +{ + u32 sw_if_index; + ethernet_arp_ip4_over_ethernet_address_t a; + int is_static; + int flags; +#define ETHERNET_ARP_ARGS_REMOVE (1<<0) +#define ETHERNET_ARP_ARGS_FLUSH (1<<1) +#define ETHERNET_ARP_ARGS_POPULATE (1<<2) +} vnet_arp_set_ip4_over_ethernet_rpc_args_t; + +static void +set_ip4_over_ethernet_rpc_callback (vnet_arp_set_ip4_over_ethernet_rpc_args_t + * a); + static u8 * format_ethernet_arp_hardware_type (u8 * s, va_list * va) { @@ -213,33 +233,29 @@ format_ethernet_arp_header (u8 * s, va_list * va) return s; } -static u8 * +u8 * format_ethernet_arp_ip4_entry (u8 * s, va_list * va) { vnet_main_t *vnm = va_arg (*va, vnet_main_t *); ethernet_arp_ip4_entry_t *e = va_arg (*va, ethernet_arp_ip4_entry_t *); vnet_sw_interface_t *si; - ip4_fib_t *fib; u8 *flags = 0; if (!e) - return format (s, "%=12s%=6s%=16s%=6s%=20s%=24s", "Time", "FIB", "IP4", + return format (s, "%=12s%=16s%=6s%=20s%=24s", "Time", "IP4", "Flags", "Ethernet", "Interface"); - fib = find_ip4_fib_by_table_index_or_id (&ip4_main, e->key.fib_index, - IP4_ROUTE_FLAG_FIB_INDEX); - si = vnet_get_sw_interface (vnm, e->key.sw_if_index); - - if (e->flags & ETHERNET_ARP_IP4_ENTRY_FLAG_GLEAN) - flags = format (flags, "G"); + si = vnet_get_sw_interface (vnm, e->sw_if_index); if (e->flags & ETHERNET_ARP_IP4_ENTRY_FLAG_STATIC) flags = format (flags, "S"); - s = format (s, "%=12U%=6u%=16U%=6s%=20U%=24U", + if (e->flags & ETHERNET_ARP_IP4_ENTRY_FLAG_DYNAMIC) + flags = format (flags, "D"); + + s = format (s, "%=12U%=16U%=6s%=20U%=24U", format_vlib_cpu_time, vnm->vlib_main, e->cpu_time_last_updated, - fib->table_id, - format_ip4_address, &e->key.ip4_address, + format_ip4_address, &e->ip4_address, flags ? (char *) flags : "", format_ethernet_address, e->ethernet_address, format_vnet_sw_interface_name, vnm, si); @@ -267,220 +283,291 @@ format_ethernet_arp_input_trace (u8 * s, va_list * va) return s; } -clib_error_t * -ethernet_arp_sw_interface_up_down (vnet_main_t * vnm, - u32 sw_if_index, u32 flags) +static u8 * +format_arp_term_input_trace (u8 * s, va_list * va) { - ethernet_arp_main_t *am = ðernet_arp_main; - ethernet_arp_ip4_entry_t *e; - u32 i; - u32 *to_add_del = 0; + CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *); + CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *); + ethernet_arp_input_trace_t *t = va_arg (*va, ethernet_arp_input_trace_t *); - /* *INDENT-OFF* */ - pool_foreach (e, am->ip4_entry_pool, ({ - if (e->key.sw_if_index == sw_if_index) - vec_add1 (to_add_del, e - am->ip4_entry_pool); - })); - /* *INDENT-ON* */ + /* arp-term trace data saved is either arp or ip6/icmp6 packet: + - for arp, the 1st 16-bit field is hw type of value of 0x0001. + - for ip6, the first nibble has value of 6. */ + s = format (s, "%U", t->packet_data[0] == 0 ? + format_ethernet_arp_header : format_ip6_header, + t->packet_data, sizeof (t->packet_data)); - for (i = 0; i < vec_len (to_add_del); i++) - { - ethernet_arp_ip4_over_ethernet_address_t arp_add; - e = pool_elt_at_index (am->ip4_entry_pool, to_add_del[i]); + return s; +} - clib_memcpy (&arp_add.ethernet, e->ethernet_address, 6); - arp_add.ip4.as_u32 = e->key.ip4_address.as_u32; +static void +arp_nbr_probe (ip_adjacency_t * adj) +{ + vnet_main_t *vnm = vnet_get_main (); + ip4_main_t *im = &ip4_main; + ip_interface_address_t *ia; + ethernet_arp_header_t *h; + vnet_hw_interface_t *hi; + vnet_sw_interface_t *si; + ip4_address_t *src; + vlib_buffer_t *b; + vlib_main_t *vm; + u32 bi = 0; - if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) - { - vnet_arp_set_ip4_over_ethernet (vnm, - e->key.sw_if_index, - e->key.fib_index, &arp_add, - e->flags & - ETHERNET_ARP_IP4_ENTRY_FLAG_STATIC); - } - else if ((e->flags & ETHERNET_ARP_IP4_ENTRY_FLAG_STATIC) == 0) - { - vnet_arp_unset_ip4_over_ethernet (vnm, - e->key.sw_if_index, - e->key.fib_index, &arp_add); - } + vm = vlib_get_main (); + + si = vnet_get_sw_interface (vnm, adj->rewrite_header.sw_if_index); + + if (!(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)) + { + return; } - vec_free (to_add_del); - return 0; + src = + ip4_interface_address_matching_destination (im, + &adj->sub_type.nbr.next_hop. + ip4, + adj->rewrite_header. + sw_if_index, &ia); + if (!src) + { + return; + } + + h = + vlib_packet_template_get_packet (vm, &im->ip4_arp_request_packet_template, + &bi); + + hi = vnet_get_sup_hw_interface (vnm, adj->rewrite_header.sw_if_index); + + clib_memcpy (h->ip4_over_ethernet[0].ethernet, + hi->hw_address, sizeof (h->ip4_over_ethernet[0].ethernet)); + + h->ip4_over_ethernet[0].ip4 = src[0]; + h->ip4_over_ethernet[1].ip4 = adj->sub_type.nbr.next_hop.ip4; + + b = vlib_get_buffer (vm, bi); + vnet_buffer (b)->sw_if_index[VLIB_RX] = + vnet_buffer (b)->sw_if_index[VLIB_TX] = adj->rewrite_header.sw_if_index; + + /* Add encapsulation string for software interface (e.g. ethernet header). */ + vnet_rewrite_one_header (adj[0], h, sizeof (ethernet_header_t)); + vlib_buffer_advance (b, -adj->rewrite_header.data_bytes); + + { + vlib_frame_t *f = vlib_get_frame_to_node (vm, hi->output_node_index); + u32 *to_next = vlib_frame_vector_args (f); + to_next[0] = bi; + f->n_vectors = 1; + vlib_put_frame_to_node (vm, hi->output_node_index, f); + } } -VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (ethernet_arp_sw_interface_up_down); +static void +arp_mk_complete (adj_index_t ai, ethernet_arp_ip4_entry_t * e) +{ + adj_nbr_update_rewrite + (ai, ADJ_NBR_REWRITE_FLAG_COMPLETE, + ethernet_build_rewrite (vnet_get_main (), + e->sw_if_index, + adj_get_link_type (ai), e->ethernet_address)); +} -static int -vnet_arp_set_ip4_over_ethernet_internal (vnet_main_t * vnm, - u32 sw_if_index, - u32 fib_index, - void *a_arg, int is_static); +static void +arp_mk_incomplete (adj_index_t ai, ethernet_arp_ip4_entry_t * e) +{ + adj_nbr_update_rewrite + (ai, + ADJ_NBR_REWRITE_FLAG_INCOMPLETE, + ethernet_build_rewrite (vnet_get_main (), + e->sw_if_index, + VNET_LINK_ARP, + VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST)); +} -static int -vnet_arp_unset_ip4_over_ethernet_internal (vnet_main_t * vnm, - u32 sw_if_index, - u32 fib_index, void *a_arg); +static ethernet_arp_ip4_entry_t * +arp_entry_find (ethernet_arp_interface_t * eai, const ip4_address_t * addr) +{ + ethernet_arp_main_t *am = ðernet_arp_main; + ethernet_arp_ip4_entry_t *e = NULL; + uword *p; -typedef struct + if (NULL != eai->arp_entries) + { + p = hash_get (eai->arp_entries, addr->as_u32); + if (!p) + return (NULL); + + e = pool_elt_at_index (am->ip4_entry_pool, p[0]); + } + + return (e); +} + +static adj_walk_rc_t +arp_mk_complete_walk (adj_index_t ai, void *ctx) { - u32 sw_if_index; - u32 fib_index; - ethernet_arp_ip4_over_ethernet_address_t a; - int is_static; - int is_remove; /* set is_remove=1 to clear arp entry */ -} vnet_arp_set_ip4_over_ethernet_rpc_args_t; + ethernet_arp_ip4_entry_t *e = ctx; + + arp_mk_complete (ai, e); -static void set_ip4_over_ethernet_rpc_callback - (vnet_arp_set_ip4_over_ethernet_rpc_args_t * a) + return (ADJ_WALK_RC_CONTINUE); +} + +static adj_walk_rc_t +arp_mk_incomplete_walk (adj_index_t ai, void *ctx) { - vnet_main_t *vm = vnet_get_main (); - ASSERT (os_get_cpu_number () == 0); + ethernet_arp_ip4_entry_t *e = ctx; - if (a->is_remove) - vnet_arp_unset_ip4_over_ethernet_internal (vm, - a->sw_if_index, - a->fib_index, &(a->a)); - else - vnet_arp_set_ip4_over_ethernet_internal (vm, - a->sw_if_index, - a->fib_index, - &(a->a), a->is_static); + arp_mk_incomplete (ai, e); + + return (ADJ_WALK_RC_CONTINUE); } -int -vnet_arp_set_ip4_over_ethernet (vnet_main_t * vnm, - u32 sw_if_index, - u32 fib_index, void *a_arg, int is_static) +void +arp_update_adjacency (vnet_main_t * vnm, u32 sw_if_index, u32 ai) { - ethernet_arp_ip4_over_ethernet_address_t *a = a_arg; - vnet_arp_set_ip4_over_ethernet_rpc_args_t args; + ethernet_arp_main_t *am = ðernet_arp_main; + ethernet_arp_interface_t *arp_int; + ethernet_arp_ip4_entry_t *e; + ip_adjacency_t *adj; - args.sw_if_index = sw_if_index; - args.fib_index = fib_index; - args.is_static = is_static; - args.is_remove = 0; - clib_memcpy (&args.a, a, sizeof (*a)); + adj = adj_get (ai); - vl_api_rpc_call_main_thread (set_ip4_over_ethernet_rpc_callback, - (u8 *) & args, sizeof (args)); - return 0; + vec_validate (am->ethernet_arp_by_sw_if_index, sw_if_index); + arp_int = &am->ethernet_arp_by_sw_if_index[sw_if_index]; + e = arp_entry_find (arp_int, &adj->sub_type.nbr.next_hop.ip4); + + if (NULL != e) + { + adj_nbr_walk_nh4 (sw_if_index, + &e->ip4_address, arp_mk_complete_walk, e); + } + else + { + /* + * no matching ARP entry. + * construct the rewire required to for an ARP packet, and stick + * that in the adj's pipe to smoke. + */ + adj_nbr_update_rewrite (ai, + ADJ_NBR_REWRITE_FLAG_INCOMPLETE, + ethernet_build_rewrite (vnm, + sw_if_index, + VNET_LINK_ARP, + VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST)); + + /* + * since the FIB has added this adj for a route, it makes sense it may + * want to forward traffic sometime soon. Let's send a speculative ARP. + * just one. If we were to do periodically that wouldn't be bad either, + * but that's more code than i'm prepared to write at this time for + * relatively little reward. + */ + arp_nbr_probe (adj); + } } int vnet_arp_set_ip4_over_ethernet_internal (vnet_main_t * vnm, - u32 sw_if_index, - u32 fib_index, - void *a_arg, int is_static) + vnet_arp_set_ip4_over_ethernet_rpc_args_t + * args) { - ethernet_arp_ip4_key_t k; ethernet_arp_ip4_entry_t *e = 0; ethernet_arp_main_t *am = ðernet_arp_main; - ethernet_arp_ip4_over_ethernet_address_t *a = a_arg; + ethernet_arp_ip4_over_ethernet_address_t *a = &args->a; vlib_main_t *vm = vlib_get_main (); - ip4_main_t *im = &ip4_main; - ip_lookup_main_t *lm = &im->lookup_main; int make_new_arp_cache_entry = 1; uword *p; - ip4_add_del_route_args_t args; - ip_adjacency_t adj, *existing_adj; pending_resolution_t *pr, *mc; + ethernet_arp_interface_t *arp_int; + int is_static = args->is_static; + u32 sw_if_index = args->sw_if_index; - u32 next_index; - u32 adj_index; - - fib_index = (fib_index != (u32) ~ 0) - ? fib_index : im->fib_index_by_sw_if_index[sw_if_index]; + vec_validate (am->ethernet_arp_by_sw_if_index, sw_if_index); - k.sw_if_index = sw_if_index; - k.ip4_address = a->ip4; - k.fib_index = fib_index; + arp_int = &am->ethernet_arp_by_sw_if_index[sw_if_index]; - p = mhash_get (&am->ip4_entry_by_key, &k); - if (p) + if (NULL != arp_int->arp_entries) { - e = pool_elt_at_index (am->ip4_entry_pool, p[0]); + p = hash_get (arp_int->arp_entries, a->ip4.as_u32); + if (p) + { + e = pool_elt_at_index (am->ip4_entry_pool, p[0]); - /* Refuse to over-write static arp. */ - if (!is_static && (e->flags & ETHERNET_ARP_IP4_ENTRY_FLAG_STATIC)) - return -2; - make_new_arp_cache_entry = 0; + /* Refuse to over-write static arp. */ + if (!is_static && (e->flags & ETHERNET_ARP_IP4_ENTRY_FLAG_STATIC)) + return -2; + make_new_arp_cache_entry = 0; + } } - /* Note: always install the route. It might have been deleted */ - memset (&adj, 0, sizeof (adj)); - adj.lookup_next_index = IP_LOOKUP_NEXT_REWRITE; - adj.n_adj = 1; /* otherwise signature compare fails */ + if (make_new_arp_cache_entry) + { + fib_prefix_t pfx = { + .fp_len = 32, + .fp_proto = FIB_PROTOCOL_IP4, + .fp_addr = { + .ip4 = a->ip4, + } + , + }; + u32 fib_index; - vnet_rewrite_for_sw_interface (vnm, VNET_L3_PACKET_TYPE_IP4, sw_if_index, ip4_rewrite_node.index, a->ethernet, /* destination address */ - &adj.rewrite_header, - sizeof (adj.rewrite_data)); + pool_get (am->ip4_entry_pool, e); - /* result of this lookup should be next-hop adjacency */ - adj_index = ip4_fib_lookup_with_table (im, fib_index, &a->ip4, 0); - existing_adj = ip_get_adjacency (lm, adj_index); + if (NULL == arp_int->arp_entries) + { + arp_int->arp_entries = hash_create (0, sizeof (u32)); + } - if (existing_adj->lookup_next_index == IP_LOOKUP_NEXT_ARP && - existing_adj->arp.next_hop.ip4.as_u32 == a->ip4.as_u32) - { - u32 *ai; - u32 *adjs = vec_dup (e->adjacencies); - /* Update all adj assigned to this arp entry */ - vec_foreach (ai, adjs) - { - int i; - ip_adjacency_t *uadj = ip_get_adjacency (lm, *ai); - for (i = 0; i < uadj->n_adj; i++) - if (uadj[i].lookup_next_index == IP_LOOKUP_NEXT_ARP && - uadj[i].arp.next_hop.ip4.as_u32 == a->ip4.as_u32) - ip_update_adjacency (lm, *ai + i, &adj); - } - vec_free (adjs); + hash_set (arp_int->arp_entries, a->ip4.as_u32, e - am->ip4_entry_pool); + + e->sw_if_index = sw_if_index; + e->ip4_address = a->ip4; + clib_memcpy (e->ethernet_address, + a->ethernet, sizeof (e->ethernet_address)); + + fib_index = ip4_fib_table_get_index_for_sw_if_index (e->sw_if_index); + e->fib_entry_index = + fib_table_entry_update_one_path (fib_index, + &pfx, + FIB_SOURCE_ADJ, + FIB_ENTRY_FLAG_ATTACHED, + FIB_PROTOCOL_IP4, + &pfx.fp_addr, + e->sw_if_index, + ~0, + 1, NULL, FIB_ROUTE_PATH_FLAG_NONE); } else { - /* Check that new adjacency actually isn't exactly the same as - * what is already there. If we over-write the adjacency with - * exactly the same info, its technically a new adjacency with - * new counters, but to user it appears as counters reset. + /* + * prevent a DoS attack from the data-plane that + * spams us with no-op updates to the MAC address */ - if (vnet_ip_adjacency_share_compare (&adj, existing_adj) == 0) - { - /* create new adj */ - args.table_index_or_table_id = fib_index; - args.flags = - IP4_ROUTE_FLAG_FIB_INDEX | IP4_ROUTE_FLAG_ADD | - IP4_ROUTE_FLAG_NEIGHBOR; - args.dst_address = a->ip4; - args.dst_address_length = 32; - args.adj_index = ~0; - args.add_adj = &adj; - args.n_add_adj = 1; - ip4_add_del_route (im, &args); - } - } + if (0 == memcmp (e->ethernet_address, + a->ethernet, sizeof (e->ethernet_address))) + return -1; - if (make_new_arp_cache_entry) - { - pool_get (am->ip4_entry_pool, e); - mhash_set (&am->ip4_entry_by_key, &k, e - am->ip4_entry_pool, - /* old value */ 0); - e->key = k; + /* Update time stamp and ethernet address. */ + clib_memcpy (e->ethernet_address, a->ethernet, + sizeof (e->ethernet_address)); } - /* Update time stamp and ethernet address. */ - clib_memcpy (e->ethernet_address, a->ethernet, - sizeof (e->ethernet_address)); e->cpu_time_last_updated = clib_cpu_time_now (); if (is_static) e->flags |= ETHERNET_ARP_IP4_ENTRY_FLAG_STATIC; + else + e->flags |= ETHERNET_ARP_IP4_ENTRY_FLAG_DYNAMIC; + + adj_nbr_walk_nh4 (sw_if_index, &e->ip4_address, arp_mk_complete_walk, e); /* Customer(s) waiting for this address to be resolved? */ p = hash_get (am->pending_resolutions_by_address, a->ip4.as_u32); if (p) { + u32 next_index; next_index = p[0]; while (next_index != (u32) ~ 0) @@ -499,6 +586,7 @@ vnet_arp_set_ip4_over_ethernet_internal (vnet_main_t * vnm, p = hash_get (am->mac_changes_by_address, a->ip4.as_u32); if (p) { + u32 next_index; next_index = p[0]; while (next_index != (u32) ~ 0) @@ -512,7 +600,7 @@ vnet_arp_set_ip4_over_ethernet_internal (vnet_main_t * vnm, if (fp) rv = (*fp) (mc->data, a->ethernet, sw_if_index, 0); - /* + /* * Signal the resolver process, as long as the user * says they want to be notified */ @@ -661,6 +749,7 @@ typedef enum _ (l2_address_mismatch, "ARP hw addr does not match L2 frame src addr") \ _ (missing_interface_address, "ARP missing interface address") \ _ (gratuitous_arp, "ARP probe or announcement dropped") \ + _ (interface_no_table, "Interface is not mapped to an IP table") \ typedef enum { @@ -670,29 +759,6 @@ typedef enum ETHERNET_ARP_N_ERROR, } ethernet_arp_input_error_t; -/* get first interface address */ -ip4_address_t * -ip4_interface_first_address (ip4_main_t * im, u32 sw_if_index, - ip_interface_address_t ** result_ia) -{ - ip_lookup_main_t *lm = &im->lookup_main; - ip_interface_address_t *ia = 0; - ip4_address_t *result = 0; - - /* *INDENT-OFF* */ - foreach_ip_interface_address (lm, ia, sw_if_index, - 1 /* honor unnumbered */ , - ({ - ip4_address_t * a = - ip_interface_address_get_address (lm, ia); - result = a; break; - })); - /* *INDENT-ON* */ - - if (result_ia) - *result_ia = result ? ia : 0; - return result; -} static void unset_random_arp_entry (void) @@ -720,16 +786,14 @@ unset_random_arp_entry (void) e = pool_elt_at_index (am->ip4_entry_pool, index); clib_memcpy (&delme.ethernet, e->ethernet_address, 6); - delme.ip4.as_u32 = e->key.ip4_address.as_u32; + delme.ip4.as_u32 = e->ip4_address.as_u32; - vnet_arp_unset_ip4_over_ethernet (vnm, e->key.sw_if_index, - e->key.fib_index, &delme); + vnet_arp_unset_ip4_over_ethernet (vnm, e->sw_if_index, &delme); } -static void +static int arp_unnumbered (vlib_buffer_t * p0, - u32 pi0, - ethernet_header_t * eth0, ip_interface_address_t * ifa0) + u32 pi0, ethernet_header_t * eth0, u32 sw_if_index) { vlib_main_t *vm = vlib_get_main (); vnet_main_t *vnm = vnet_get_main (); @@ -750,7 +814,7 @@ arp_unnumbered (vlib_buffer_t * p0, clib_memcpy (dst_mac_address, eth0->dst_address, sizeof (dst_mac_address)); /* Figure out which sw_if_index supplied the address */ - unnum_src_sw_if_index = ifa0->sw_if_index; + unnum_src_sw_if_index = sw_if_index; /* Track down all users of the unnumbered source */ /* *INDENT-OFF* */ @@ -764,7 +828,10 @@ arp_unnumbered (vlib_buffer_t * p0, })); /* *INDENT-ON* */ - ASSERT (vec_len (broadcast_swifs)); + /* If there are no interfaces un-unmbered to this interface, + we are done here. */ + if (0 == vec_len (broadcast_swifs)) + return 0; /* Allocate buffering if we need it */ if (vec_len (broadcast_swifs) > 1) @@ -869,6 +936,8 @@ arp_unnumbered (vlib_buffer_t * p0, vec_free (broadcast_swifs); vec_free (buffers); + + return !0; } static uword @@ -901,13 +970,14 @@ arp_input (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame) vnet_hw_interface_t *hw_if0; ethernet_arp_header_t *arp0; ethernet_header_t *eth0; - ip_interface_address_t *ifa0; ip_adjacency_t *adj0; - ip4_address_t *if_addr0; - ip4_address_t proxy_src; - u32 pi0, error0, next0, sw_if_index0; - u8 is_request0, src_is_local0, dst_is_local0, is_unnum0; + ip4_address_t *if_addr0, proxy_src; + u32 pi0, error0, next0, sw_if_index0, conn_sw_if_index0, fib_index0; + u8 is_request0, dst_is_local0, is_unnum0; ethernet_proxy_arp_t *pa; + fib_node_index_t dst_fei, src_fei; + fib_prefix_t pfx0; + fib_entry_flag_t src_flags, dst_flags; pi0 = from[0]; to_next[0] = pi0; @@ -915,6 +985,7 @@ arp_input (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame) to_next += 1; n_left_from -= 1; n_left_to_next -= 1; + pa = 0; p0 = vlib_get_buffer (vm, pi0); arp0 = vlib_buffer_get_current (p0); @@ -936,43 +1007,59 @@ arp_input (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame) sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX]; if (error0) - goto drop1; + goto drop2; /* Check that IP address is local and matches incoming interface. */ - if_addr0 = - ip4_interface_address_matching_destination (im4, - &arp0-> - ip4_over_ethernet[1]. - ip4, sw_if_index0, - &ifa0); - if (!if_addr0) + fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0); + if (~0 == fib_index0) + { + error0 = ETHERNET_ARP_ERROR_interface_no_table; + goto drop2; + + } + dst_fei = ip4_fib_table_lookup (ip4_fib_get (fib_index0), + &arp0->ip4_over_ethernet[1].ip4, + 32); + dst_flags = fib_entry_get_flags_for_source (dst_fei, + FIB_SOURCE_INTERFACE); + + conn_sw_if_index0 = + fib_entry_get_resolving_interface_for_source (dst_fei, + FIB_SOURCE_INTERFACE); + + if (!(FIB_ENTRY_FLAG_CONNECTED & dst_flags)) { error0 = ETHERNET_ARP_ERROR_l3_dst_address_not_local; goto drop1; } /* Honor unnumbered interface, if any */ - is_unnum0 = sw_if_index0 != ifa0->sw_if_index; + is_unnum0 = sw_if_index0 != conn_sw_if_index0; /* Source must also be local to subnet of matching interface address. */ - if (!ip4_destination_matches_interface - (im4, &arp0->ip4_over_ethernet[0].ip4, ifa0)) + src_fei = ip4_fib_table_lookup (ip4_fib_get (fib_index0), + &arp0->ip4_over_ethernet[0].ip4, + 32); + src_flags = fib_entry_get_flags (src_fei); + + if (!((FIB_ENTRY_FLAG_ATTACHED & src_flags) || + (FIB_ENTRY_FLAG_CONNECTED & src_flags)) || + sw_if_index0 != fib_entry_get_resolving_interface (src_fei)) { error0 = ETHERNET_ARP_ERROR_l3_src_address_not_local; - goto drop1; + goto drop2; } /* Reject requests/replies with our local interface address. */ - src_is_local0 = - if_addr0->as_u32 == arp0->ip4_over_ethernet[0].ip4.as_u32; - if (src_is_local0) + if (FIB_ENTRY_FLAG_LOCAL & src_flags) { error0 = ETHERNET_ARP_ERROR_l3_src_address_is_local; - goto drop1; + goto drop2; } - dst_is_local0 = - if_addr0->as_u32 == arp0->ip4_over_ethernet[1].ip4.as_u32; + dst_is_local0 = (FIB_ENTRY_FLAG_LOCAL & dst_flags); + fib_entry_get_prefix (dst_fei, &pfx0); + if_addr0 = &pfx0.fp_addr.ip4; /* Fill in ethernet header. */ eth0 = ethernet_buffer_get_header (p0); @@ -996,7 +1083,6 @@ arp_input (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame) unset_random_arp_entry (); vnet_arp_set_ip4_over_ethernet (vnm, sw_if_index0, - (u32) ~ 0 /* default fib */ , &arp0->ip4_over_ethernet[0], 0 /* is_static */ ); error0 = ETHERNET_ARP_ERROR_l3_src_address_learned; @@ -1037,21 +1123,28 @@ arp_input (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame) clib_memcpy (eth0->src_address, hw_if0->hw_address, 6); /* Figure out how much to rewind current data from adjacency. */ - if (ifa0) + /* get the adj from the destination's covering connected */ + if (NULL == pa) { - adj0 = ip_get_adjacency (&ip4_main.lookup_main, - ifa0->neighbor_probe_adj_index); - if (adj0->lookup_next_index != IP_LOOKUP_NEXT_ARP) + adj0 = + adj_get (fib_entry_get_adj_for_source + (ip4_fib_table_lookup + (ip4_fib_get (fib_index0), + &arp0->ip4_over_ethernet[1].ip4, 31), + FIB_SOURCE_INTERFACE)); + if (adj0->lookup_next_index != IP_LOOKUP_NEXT_GLEAN) { error0 = ETHERNET_ARP_ERROR_missing_interface_address; goto drop2; } if (is_unnum0) - arp_unnumbered (p0, pi0, eth0, ifa0); + { + if (!arp_unnumbered (p0, pi0, eth0, conn_sw_if_index0)) + goto drop2; + } else vlib_buffer_advance (p0, -adj0->rewrite_header.data_bytes); } - vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next, n_left_to_next, pi0, next0); @@ -1095,15 +1188,16 @@ arp_input (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame) proxy_src.as_u32 = arp0->ip4_over_ethernet[1].ip4.data_u32; - /* + /* * Rewind buffer, direct code above not to - * think too hard about it. - * $$$ is the answer ever anything other than - * vlib_buffer_reset(..)? + * think too hard about it. */ - ifa0 = 0; if_addr0 = &proxy_src; - vlib_buffer_reset (p0); + is_unnum0 = 0; + i32 ethernet_start = + vnet_buffer (p0)->ethernet.start_of_ethernet_header; + i32 rewind = p0->current_data - ethernet_start; + vlib_buffer_advance (p0, -rewind); n_proxy_arp_replies_sent++; goto send_reply; } @@ -1165,13 +1259,31 @@ ip4_arp_entry_sort (void *a1, void *a2) int cmp; vnet_main_t *vnm = vnet_get_main (); - cmp = vnet_sw_interface_compare - (vnm, e1->key.sw_if_index, e2->key.sw_if_index); + cmp = vnet_sw_interface_compare (vnm, e1->sw_if_index, e2->sw_if_index); if (!cmp) - cmp = ip4_address_compare (&e1->key.ip4_address, &e2->key.ip4_address); + cmp = ip4_address_compare (&e1->ip4_address, &e2->ip4_address); return cmp; } +ethernet_arp_ip4_entry_t * +ip4_neighbor_entries (u32 sw_if_index) +{ + ethernet_arp_main_t *am = ðernet_arp_main; + ethernet_arp_ip4_entry_t *n, *ns = 0; + + /* *INDENT-OFF* */ + pool_foreach (n, am->ip4_entry_pool, ({ + if (sw_if_index != ~0 && n->sw_if_index != sw_if_index) + continue; + vec_add1 (ns, n[0]); + })); + /* *INDENT-ON* */ + + if (ns) + vec_sort_with_function (ns, ip4_arp_entry_sort); + return ns; +} + static clib_error_t * show_ip4_arp (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd) @@ -1187,22 +1299,12 @@ show_ip4_arp (vlib_main_t * vm, sw_if_index = ~0; (void) unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index); - es = 0; - /* *INDENT-OFF* */ - pool_foreach (e, am->ip4_entry_pool, - ({ - vec_add1 (es, e[0]); - })); - /* *INDENT-ON* */ - + es = ip4_neighbor_entries (sw_if_index); if (es) { - vec_sort_with_function (es, ip4_arp_entry_sort); vlib_cli_output (vm, "%U", format_ethernet_arp_ip4_entry, vnm, 0); vec_foreach (e, es) { - if (sw_if_index != ~0 && e->key.sw_if_index != sw_if_index) - continue; vlib_cli_output (vm, "%U", format_ethernet_arp_ip4_entry, vnm, e); } vec_free (es); @@ -1223,11 +1325,25 @@ show_ip4_arp (vlib_main_t * vm, return error; } +/*? + * Display all the IPv4 ARP entries. + * + * @cliexpar + * Example of how to display the IPv4 ARP table: + * @cliexstart{show ip arp} + * Time FIB IP4 Flags Ethernet Interface + * 346.3028 0 6.1.1.3 de:ad:be:ef:ba:be GigabitEthernet2/0/0 + * 3077.4271 0 6.1.1.4 S de:ad:be:ef:ff:ff GigabitEthernet2/0/0 + * 2998.6409 1 6.2.2.3 de:ad:be:ef:00:01 GigabitEthernet2/0/0 + * Proxy arps enabled for: + * Fib_index 0 6.0.0.1 - 6.0.0.11 + * @cliexend + ?*/ /* *INDENT-OFF* */ VLIB_CLI_COMMAND (show_ip4_arp_command, static) = { .path = "show ip arp", .function = show_ip4_arp, - .short_help = "Show ARP table", + .short_help = "show ip arp", }; /* *INDENT-ON* */ @@ -1305,80 +1421,127 @@ ip4_set_arp_limit (u32 arp_limit) return 0; } -static void -arp_ip4_entry_del_adj (ethernet_arp_ip4_entry_t * e, u32 adj_index) +/** + * @brief Control Plane hook to remove an ARP entry + */ +int +vnet_arp_unset_ip4_over_ethernet (vnet_main_t * vnm, + u32 sw_if_index, void *a_arg) { - int done = 0; - int i; + ethernet_arp_ip4_over_ethernet_address_t *a = a_arg; + vnet_arp_set_ip4_over_ethernet_rpc_args_t args; - while (!done) - { - vec_foreach_index (i, e->adjacencies) - if (vec_elt (e->adjacencies, i) == adj_index) - { - vec_del1 (e->adjacencies, i); - continue; - } - done = 1; - } + args.sw_if_index = sw_if_index; + args.flags = ETHERNET_ARP_ARGS_REMOVE; + clib_memcpy (&args.a, a, sizeof (*a)); + + vl_api_rpc_call_main_thread (set_ip4_over_ethernet_rpc_callback, + (u8 *) & args, sizeof (args)); + return 0; } -static void -arp_ip4_entry_add_adj (ethernet_arp_ip4_entry_t * e, u32 adj_index) +/** + * @brief Internally generated event to flush the ARP cache on an + * interface state change event. + * A flush will remove dynamic ARP entries, and for statics remove the MAC + * address from the corresponding adjacencies. + */ +static int +vnet_arp_flush_ip4_over_ethernet (vnet_main_t * vnm, + u32 sw_if_index, void *a_arg) { - int i; - vec_foreach_index (i, e->adjacencies) - if (vec_elt (e->adjacencies, i) == adj_index) - return; - vec_add1 (e->adjacencies, adj_index); + ethernet_arp_ip4_over_ethernet_address_t *a = a_arg; + vnet_arp_set_ip4_over_ethernet_rpc_args_t args; + + args.sw_if_index = sw_if_index; + args.flags = ETHERNET_ARP_ARGS_FLUSH; + clib_memcpy (&args.a, a, sizeof (*a)); + + vl_api_rpc_call_main_thread (set_ip4_over_ethernet_rpc_callback, + (u8 *) & args, sizeof (args)); + return 0; +} + +/** + * @brief Internally generated event to populate the ARP cache on an + * interface state change event. + * For static entries this will re-source the adjacencies. + * + * @param sw_if_index The interface on which the ARP entires are acted + */ +static int +vnet_arp_populate_ip4_over_ethernet (vnet_main_t * vnm, + u32 sw_if_index, void *a_arg) +{ + ethernet_arp_ip4_over_ethernet_address_t *a = a_arg; + vnet_arp_set_ip4_over_ethernet_rpc_args_t args; + + args.sw_if_index = sw_if_index; + args.flags = ETHERNET_ARP_ARGS_POPULATE; + clib_memcpy (&args.a, a, sizeof (*a)); + + vl_api_rpc_call_main_thread (set_ip4_over_ethernet_rpc_callback, + (u8 *) & args, sizeof (args)); + return 0; } +/* + * arp_add_del_interface_address + * + * callback when an interface address is added or deleted + */ static void -arp_add_del_adj_cb (struct ip_lookup_main_t *lm, - u32 adj_index, ip_adjacency_t * adj, u32 is_del) +arp_add_del_interface_address (ip4_main_t * im, + uword opaque, + u32 sw_if_index, + ip4_address_t * address, + u32 address_length, + u32 if_address_index, u32 is_del) { + /* + * Flush the ARP cache of all entries covered by the address + * that is being removed. + */ ethernet_arp_main_t *am = ðernet_arp_main; - ip4_main_t *im = &ip4_main; - ethernet_arp_ip4_key_t k; - ethernet_arp_ip4_entry_t *e = 0; - uword *p; - u32 ai; + ethernet_arp_ip4_entry_t *e; + + if (vec_len (am->ethernet_arp_by_sw_if_index) <= sw_if_index) + return; - for (ai = adj->heap_handle; ai < adj->heap_handle + adj->n_adj; ai++) + if (is_del) { - adj = ip_get_adjacency (lm, ai); - if (adj->lookup_next_index == IP_LOOKUP_NEXT_ARP - && adj->arp.next_hop.ip4.as_u32) - { - k.sw_if_index = adj->rewrite_header.sw_if_index; - k.ip4_address.as_u32 = adj->arp.next_hop.ip4.as_u32; - k.fib_index = - im->fib_index_by_sw_if_index[adj->rewrite_header.sw_if_index]; - p = mhash_get (&am->ip4_entry_by_key, &k); - if (p) - e = pool_elt_at_index (am->ip4_entry_pool, p[0]); - } - else - continue; + ethernet_arp_interface_t *eai; + u32 i, *to_delete = 0; + hash_pair_t *pair; + + eai = &am->ethernet_arp_by_sw_if_index[sw_if_index]; + + /* *INDENT-OFF* */ + hash_foreach_pair (pair, eai->arp_entries, + ({ + e = pool_elt_at_index(am->ip4_entry_pool, + pair->value[0]); + if (ip4_destination_matches_route (im, &e->ip4_address, + address, address_length)) + { + vec_add1 (to_delete, e - am->ip4_entry_pool); + } + })); + /* *INDENT-ON* */ - if (is_del) + for (i = 0; i < vec_len (to_delete); i++) { - if (!e) - clib_warning ("Adjacency contains unknown ARP next hop %U (del)", - format_ip46_address, &adj->arp.next_hop, - IP46_TYPE_IP4); - else - arp_ip4_entry_del_adj (e, adj->heap_handle); - } - else /* add */ - { - if (!e) - clib_warning ("Adjacency contains unknown ARP next hop %U (add)", - format_ip46_address, &adj->arp.next_hop, - IP46_TYPE_IP4); - else - arp_ip4_entry_add_adj (e, adj->heap_handle); + ethernet_arp_ip4_over_ethernet_address_t delme; + e = pool_elt_at_index (am->ip4_entry_pool, to_delete[i]); + + clib_memcpy (&delme.ethernet, e->ethernet_address, 6); + delme.ip4.as_u32 = e->ip4_address.as_u32; + + vnet_arp_flush_ip4_over_ethernet (vnet_get_main (), + e->sw_if_index, &delme); } + + vec_free (to_delete); } } @@ -1386,10 +1549,9 @@ static clib_error_t * ethernet_arp_init (vlib_main_t * vm) { ethernet_arp_main_t *am = ðernet_arp_main; - pg_node_t *pn; - clib_error_t *error; ip4_main_t *im = &ip4_main; - ip_lookup_main_t *lm = &im->lookup_main; + clib_error_t *error; + pg_node_t *pn; if ((error = vlib_call_init_function (vm, ethernet_init))) return error; @@ -1404,10 +1566,6 @@ ethernet_arp_init (vlib_main_t * vm) foreach_ethernet_arp_opcode; #undef _ - mhash_init (&am->ip4_entry_by_key, - /* value size */ sizeof (uword), - /* key size */ sizeof (ethernet_arp_ip4_key_t)); - /* $$$ configurable */ am->limit_arp_cache_size = 50000; @@ -1427,100 +1585,165 @@ ethernet_arp_init (vlib_main_t * vm) #undef _ } - ip_register_add_del_adjacency_callback (lm, arp_add_del_adj_cb); + ip4_add_del_interface_address_callback_t cb; + cb.function = arp_add_del_interface_address; + cb.function_opaque = 0; + vec_add1 (im->add_del_interface_address_callbacks, cb); return 0; } VLIB_INIT_FUNCTION (ethernet_arp_init); -int -vnet_arp_unset_ip4_over_ethernet (vnet_main_t * vnm, - u32 sw_if_index, u32 fib_index, void *a_arg) +static void +arp_entry_free (ethernet_arp_interface_t * eai, ethernet_arp_ip4_entry_t * e) { - ethernet_arp_ip4_over_ethernet_address_t *a = a_arg; - vnet_arp_set_ip4_over_ethernet_rpc_args_t args; - - args.sw_if_index = sw_if_index; - args.fib_index = fib_index; - args.is_remove = 1; - clib_memcpy (&args.a, a, sizeof (*a)); + ethernet_arp_main_t *am = ðernet_arp_main; - vl_api_rpc_call_main_thread (set_ip4_over_ethernet_rpc_callback, - (u8 *) & args, sizeof (args)); - return 0; + fib_table_entry_delete_index (e->fib_entry_index, FIB_SOURCE_ADJ); + hash_unset (eai->arp_entries, e->ip4_address.as_u32); + pool_put (am->ip4_entry_pool, e); } static inline int vnet_arp_unset_ip4_over_ethernet_internal (vnet_main_t * vnm, - u32 sw_if_index, - u32 fib_index, void *a_arg) + vnet_arp_set_ip4_over_ethernet_rpc_args_t + * args) { + ethernet_arp_main_t *am = ðernet_arp_main; ethernet_arp_ip4_entry_t *e; + ethernet_arp_interface_t *eai; + + eai = &am->ethernet_arp_by_sw_if_index[args->sw_if_index]; + + e = arp_entry_find (eai, &args->a.ip4); + + if (NULL != e) + { + adj_nbr_walk_nh4 (e->sw_if_index, + &e->ip4_address, arp_mk_incomplete_walk, e); + arp_entry_free (eai, e); + } + + return 0; +} + +static int +vnet_arp_flush_ip4_over_ethernet_internal (vnet_main_t * vnm, + vnet_arp_set_ip4_over_ethernet_rpc_args_t + * args) +{ ethernet_arp_main_t *am = ðernet_arp_main; - ethernet_arp_ip4_over_ethernet_address_t *a = a_arg; - ethernet_arp_ip4_key_t k; - uword *p; - ip4_add_del_route_args_t args; - ip4_main_t *im = &ip4_main; - ip_lookup_main_t *lm = &im->lookup_main; - u32 adj_index; - ip_adjacency_t *adj; + ethernet_arp_ip4_entry_t *e; + ethernet_arp_interface_t *eai; - k.sw_if_index = sw_if_index; - k.ip4_address = a->ip4; - k.fib_index = fib_index; - p = mhash_get (&am->ip4_entry_by_key, &k); - if (!p) - return -1; - - memset (&args, 0, sizeof (args)); - - /* - * Make sure that the route actually exists before we try to delete it, - * and make sure that it's a rewrite adjacency. - * - * If we point 1-N unnumbered interfaces at a loopback interface and - * shut down the loopback before shutting down 1-N unnumbered - * interfaces, the ARP cache will still have an entry, - * but the route will have disappeared. - * - * See also ip4_del_interface_routes (...) - * -> ip4_delete_matching_routes (...). - */ + eai = &am->ethernet_arp_by_sw_if_index[args->sw_if_index]; - adj_index = ip4_fib_lookup_with_table - (im, fib_index, &a->ip4, 1 /* disable default route */ ); + e = arp_entry_find (eai, &args->a.ip4); - /* Miss adj? Forget it... */ - if (adj_index != lm->miss_adj_index) + if (NULL != e) { - adj = ip_get_adjacency (lm, adj_index); - /* - * Stupid control-plane trick: - * admin down an interface (removes arp routes from fib), - * bring the interface back up (does not reinstall them) - * then remove the arp cache entry (yuck). When that happens, - * the adj we find here will be the interface subnet ARP adj. + adj_nbr_walk_nh4 (e->sw_if_index, + &e->ip4_address, arp_mk_incomplete_walk, e); + + /* + * The difference between flush and unset, is that an unset + * means delete for static and dynamic entries. A flush + * means delete only for dynamic. Flushing is what the DP + * does in response to interface events. unset is only done + * by the control plane. */ - if (adj->lookup_next_index == IP_LOOKUP_NEXT_REWRITE) + if (e->flags & ETHERNET_ARP_IP4_ENTRY_FLAG_DYNAMIC) { - args.table_index_or_table_id = fib_index; - args.flags = IP4_ROUTE_FLAG_FIB_INDEX | IP4_ROUTE_FLAG_DEL - | IP4_ROUTE_FLAG_NEIGHBOR; - args.dst_address = a->ip4; - args.dst_address_length = 32; - ip4_add_del_route (im, &args); - ip4_maybe_remap_adjacencies (im, fib_index, args.flags); + arp_entry_free (eai, e); } } + return (0); +} + +static int +vnet_arp_populate_ip4_over_ethernet_internal (vnet_main_t * vnm, + vnet_arp_set_ip4_over_ethernet_rpc_args_t + * args) +{ + ethernet_arp_main_t *am = ðernet_arp_main; + ethernet_arp_ip4_entry_t *e; + ethernet_arp_interface_t *eai; + + eai = &am->ethernet_arp_by_sw_if_index[args->sw_if_index]; + + e = arp_entry_find (eai, &args->a.ip4); + + if (NULL != e) + { + adj_nbr_walk_nh4 (e->sw_if_index, + &e->ip4_address, arp_mk_complete_walk, e); + } + return (0); +} + +static void +set_ip4_over_ethernet_rpc_callback (vnet_arp_set_ip4_over_ethernet_rpc_args_t + * a) +{ + vnet_main_t *vm = vnet_get_main (); + ASSERT (os_get_cpu_number () == 0); + + if (a->flags & ETHERNET_ARP_ARGS_REMOVE) + vnet_arp_unset_ip4_over_ethernet_internal (vm, a); + else if (a->flags & ETHERNET_ARP_ARGS_FLUSH) + vnet_arp_flush_ip4_over_ethernet_internal (vm, a); + else if (a->flags & ETHERNET_ARP_ARGS_POPULATE) + vnet_arp_populate_ip4_over_ethernet_internal (vm, a); + else + vnet_arp_set_ip4_over_ethernet_internal (vm, a); +} + +/** + * @brief Invoked when the interface's admin state changes + */ +static clib_error_t * +ethernet_arp_sw_interface_up_down (vnet_main_t * vnm, + u32 sw_if_index, u32 flags) +{ + ethernet_arp_main_t *am = ðernet_arp_main; + ethernet_arp_ip4_entry_t *e; + u32 i, *to_delete = 0; + + /* *INDENT-OFF* */ + pool_foreach (e, am->ip4_entry_pool, + ({ + if (e->sw_if_index == sw_if_index) + vec_add1 (to_delete, + e - am->ip4_entry_pool); + })); + /* *INDENT-ON* */ + + for (i = 0; i < vec_len (to_delete); i++) + { + ethernet_arp_ip4_over_ethernet_address_t delme; + e = pool_elt_at_index (am->ip4_entry_pool, to_delete[i]); + + clib_memcpy (&delme.ethernet, e->ethernet_address, 6); + delme.ip4.as_u32 = e->ip4_address.as_u32; + + if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) + { + vnet_arp_populate_ip4_over_ethernet (vnm, e->sw_if_index, &delme); + } + else + { + vnet_arp_flush_ip4_over_ethernet (vnm, e->sw_if_index, &delme); + } + + } + vec_free (to_delete); - e = pool_elt_at_index (am->ip4_entry_pool, p[0]); - mhash_unset (&am->ip4_entry_by_key, &e->key, 0); - pool_put (am->ip4_entry_pool, e); return 0; } +VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (ethernet_arp_sw_interface_up_down); + static void increment_ip4_and_mac_address (ethernet_arp_ip4_over_ethernet_address_t * a) { @@ -1544,6 +1767,23 @@ increment_ip4_and_mac_address (ethernet_arp_ip4_over_ethernet_address_t * a) } } +int +vnet_arp_set_ip4_over_ethernet (vnet_main_t * vnm, + u32 sw_if_index, void *a_arg, int is_static) +{ + ethernet_arp_ip4_over_ethernet_address_t *a = a_arg; + vnet_arp_set_ip4_over_ethernet_rpc_args_t args; + + args.sw_if_index = sw_if_index; + args.is_static = is_static; + args.flags = 0; + clib_memcpy (&args.a, a, sizeof (*a)); + + vl_api_rpc_call_main_thread (set_ip4_over_ethernet_rpc_callback, + (u8 *) & args, sizeof (args)); + return 0; +} + int vnet_proxy_arp_add_del (ip4_address_t * lo_addr, ip4_address_t * hi_addr, u32 fib_index, int is_del) @@ -1582,7 +1822,7 @@ vnet_proxy_arp_add_del (ip4_address_t * lo_addr, } /* - * Remove any proxy arp entries asdociated with the + * Remove any proxy arp entries asdociated with the * specificed fib. */ int @@ -1619,57 +1859,6 @@ vnet_proxy_arp_fib_reset (u32 fib_id) return 0; } -u32 -vnet_arp_glean_add (u32 fib_index, void *next_hop_arg) -{ - ethernet_arp_main_t *am = ðernet_arp_main; - ip4_main_t *im = &ip4_main; - ip_lookup_main_t *lm = &im->lookup_main; - ip4_address_t *next_hop = next_hop_arg; - ip_adjacency_t add_adj, *adj; - ip4_add_del_route_args_t args; - ethernet_arp_ip4_entry_t *e; - ethernet_arp_ip4_key_t k; - u32 adj_index; - - adj_index = ip4_fib_lookup_with_table (im, fib_index, next_hop, 0); - adj = ip_get_adjacency (lm, adj_index); - - if (!adj || adj->lookup_next_index != IP_LOOKUP_NEXT_ARP) - return ~0; - - if (adj->arp.next_hop.ip4.as_u32 != 0) - return adj_index; - - k.sw_if_index = adj->rewrite_header.sw_if_index; - k.fib_index = fib_index; - k.ip4_address.as_u32 = next_hop->as_u32; - - if (mhash_get (&am->ip4_entry_by_key, &k)) - return adj_index; - - pool_get (am->ip4_entry_pool, e); - mhash_set (&am->ip4_entry_by_key, &k, e - am->ip4_entry_pool, - /* old value */ 0); - e->key = k; - e->cpu_time_last_updated = clib_cpu_time_now (); - e->flags = ETHERNET_ARP_IP4_ENTRY_FLAG_GLEAN; - - memset (&args, 0, sizeof (args)); - clib_memcpy (&add_adj, adj, sizeof (add_adj)); - ip46_address_set_ip4 (&add_adj.arp.next_hop, next_hop); /* install neighbor /32 route */ - args.table_index_or_table_id = fib_index; - args.flags = - IP4_ROUTE_FLAG_FIB_INDEX | IP4_ROUTE_FLAG_ADD | IP4_ROUTE_FLAG_NEIGHBOR; - args.dst_address.as_u32 = next_hop->as_u32; - args.dst_address_length = 32; - args.adj_index = ~0; - args.add_adj = &add_adj; - args.n_add_adj = 1; - ip4_add_del_route (im, &args); - return ip4_fib_lookup_with_table (im, fib_index, next_hop, 0); -} - static clib_error_t * ip_arp_add_del_command_fn (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd) @@ -1743,7 +1932,7 @@ ip_arp_add_del_command_fn (vlib_main_t * vm, 1 /* type */ , 0 /* data */ ); vnet_arp_set_ip4_over_ethernet - (vnm, sw_if_index, fib_index, &addr, is_static); + (vnm, sw_if_index, &addr, is_static); vlib_process_wait_for_event (vm); event_type = vlib_process_get_events (vm, &event_data); @@ -1752,8 +1941,7 @@ ip_arp_add_del_command_fn (vlib_main_t * vm, clib_warning ("event type %d unexpected", event_type); } else - vnet_arp_unset_ip4_over_ethernet - (vnm, sw_if_index, fib_index, &addr); + vnet_arp_unset_ip4_over_ethernet (vnm, sw_if_index, &addr); increment_ip4_and_mac_address (&addr); } @@ -1768,18 +1956,46 @@ ip_arp_add_del_command_fn (vlib_main_t * vm, } /* *INDENT-OFF* */ +/*? + * Add or delete IPv4 ARP cache entries. + * + * @note 'set ip arp' options (e.g. delete, static, 'fib-id ', + * 'count ', 'interface ip4_addr mac_addr') can be added in + * any order and combination. + * + * @cliexpar + * @parblock + * Add or delete IPv4 ARP cache entries as follows. MAC Address can be in + * either aa:bb:cc:dd:ee:ff format or aabb.ccdd.eeff format. + * @cliexcmd{set ip arp GigabitEthernet2/0/0 6.0.0.3 dead.beef.babe} + * @cliexcmd{set ip arp delete GigabitEthernet2/0/0 6.0.0.3 de:ad:be:ef:ba:be} + * + * To add or delete an IPv4 ARP cache entry to or from a specific fib + * table: + * @cliexcmd{set ip arp fib-id 1 GigabitEthernet2/0/0 6.0.0.3 dead.beef.babe} + * @cliexcmd{set ip arp fib-id 1 delete GigabitEthernet2/0/0 6.0.0.3 dead.beef.babe} + * + * Add or delete IPv4 static ARP cache entries as follows: + * @cliexcmd{set ip arp static GigabitEthernet2/0/0 6.0.0.3 dead.beef.babe} + * @cliexcmd{set ip arp static delete GigabitEthernet2/0/0 6.0.0.3 dead.beef.babe} + * + * For testing / debugging purposes, the 'set ip arp' command can add or + * delete multiple entries. Supply the 'count N' parameter: + * @cliexcmd{set ip arp count 10 GigabitEthernet2/0/0 6.0.0.3 dead.beef.babe} + * @endparblock + ?*/ VLIB_CLI_COMMAND (ip_arp_add_del_command, static) = { .path = "set ip arp", .short_help = - "set ip arp [del] [static] [count ] [fib-id ] [proxy - ]", + "set ip arp [del] [static] [count ] [fib-id ] [proxy - ]", .function = ip_arp_add_del_command_fn, }; /* *INDENT-ON* */ static clib_error_t * set_int_proxy_arp_command_fn (vlib_main_t * vm, - unformat_input_t * input, - vlib_cli_command_t * cmd) + unformat_input_t * + input, vlib_cli_command_t * cmd) { vnet_main_t *vnm = vnet_get_main (); u32 sw_if_index; @@ -1815,18 +2031,37 @@ set_int_proxy_arp_command_fn (vlib_main_t * vm, } /* *INDENT-OFF* */ +/*? + * Enable proxy-arp on an interface. The vpp stack will answer ARP + * requests for the indicated address range. Multiple proxy-arp + * ranges may be provisioned. + * + * @note Proxy ARP as a technology is infamous for blackholing traffic. + * Also, the underlying implementation has not been performance-tuned. + * Avoid creating an unnecessarily large set of ranges. + * + * @cliexpar + * To enable proxy arp on a range of addresses, use: + * @cliexcmd{set ip arp proxy 6.0.0.1 - 6.0.0.11} + * Append 'del' to delete a range of proxy ARP addresses: + * @cliexcmd{set ip arp proxy 6.0.0.1 - 6.0.0.11 del} + * You must then specifically enable proxy arp on individual interfaces: + * @cliexcmd{set interface proxy-arp GigabitEthernet0/8/0 enable} + * To disable proxy arp on an individual interface: + * @cliexcmd{set interface proxy-arp GigabitEthernet0/8/0 disable} + ?*/ VLIB_CLI_COMMAND (set_int_proxy_enable_command, static) = { .path = "set interface proxy-arp", .short_help = - "set interface proxy-arp [enable|disable]", + "set interface proxy-arp [enable|disable]", .function = set_int_proxy_arp_command_fn, }; /* *INDENT-ON* */ /* - * ARP Termination in a L2 Bridge Domain based on an - * IP4 to MAC hash table mac_by_ip4 for each BD. + * ARP/ND Termination in a L2 Bridge Domain based on IP4/IP6 to MAC + * hash tables mac_by_ip4 and mac_by_ip6 for each BD. */ typedef enum { @@ -1863,6 +2098,7 @@ arp_term_l2bd (vlib_main_t * vm, vlib_buffer_t *p0; ethernet_header_t *eth0; ethernet_arp_header_t *arp0; + ip6_header_t *iph0; u8 *l3h0; u32 pi0, error0, next0, sw_if_index0; u16 ethertype0; @@ -1883,6 +2119,13 @@ arp_term_l2bd (vlib_main_t * vm, ethertype0 = clib_net_to_host_u16 (*(u16 *) (l3h0 - 2)); arp0 = (ethernet_arp_header_t *) l3h0; + if (PREDICT_FALSE ((ethertype0 != ETHERNET_TYPE_ARP) || + (arp0->opcode != + clib_host_to_net_u16 + (ETHERNET_ARP_OPCODE_request)))) + goto check_ip6_nd; + + /* Must be ARP request packet here */ if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE) && (p0->flags & VLIB_BUFFER_IS_TRACED))) { @@ -1891,17 +2134,11 @@ arp_term_l2bd (vlib_main_t * vm, clib_memcpy (t0, l3h0, sizeof (ethernet_arp_input_trace_t)); } - if (PREDICT_FALSE ((ethertype0 != ETHERNET_TYPE_ARP) || - (arp0->opcode != - clib_host_to_net_u16 - (ETHERNET_ARP_OPCODE_request)))) - goto next_l2_feature; - error0 = ETHERNET_ARP_ERROR_replies_sent; error0 = (arp0->l2_type != - clib_net_to_host_u16 (ETHERNET_ARP_HARDWARE_TYPE_ethernet) ? - ETHERNET_ARP_ERROR_l2_type_not_ethernet : error0); + clib_net_to_host_u16 (ETHERNET_ARP_HARDWARE_TYPE_ethernet) + ? ETHERNET_ARP_ERROR_l2_type_not_ethernet : error0); error0 = (arp0->l3_type != clib_net_to_host_u16 (ETHERNET_TYPE_IP4) ? @@ -1912,8 +2149,8 @@ arp_term_l2bd (vlib_main_t * vm, if (error0) goto drop; - // Trash ARP packets whose ARP-level source addresses do not - // match their L2-frame-level source addresses */ + /* Trash ARP packets whose ARP-level source addresses do not + match their L2-frame-level source addresses */ if (PREDICT_FALSE (memcmp (eth0->src_address, arp0->ip4_over_ethernet[0].ethernet, @@ -1923,7 +2160,7 @@ arp_term_l2bd (vlib_main_t * vm, goto drop; } - // Check if anyone want ARP request events for L2 BDs + /* Check if anyone want ARP request events for L2 BDs */ { pending_resolution_t *mc; ethernet_arp_main_t *am = ðernet_arp_main; @@ -1937,13 +2174,13 @@ arp_term_l2bd (vlib_main_t * vm, int rv = 1; mc = pool_elt_at_index (am->mac_changes, next_index); fp = mc->data_callback; - // Call the callback, return 1 to suppress dup events */ + /* Call the callback, return 1 to suppress dup events */ if (fp) rv = (*fp) (mc->data, arp0->ip4_over_ethernet[0].ethernet, sw_if_index0, arp0->ip4_over_ethernet[0].ip4.as_u32); - // Signal the resolver process + /* Signal the resolver process */ if (rv == 0) vlib_process_signal_event (vm, mc->node_index, mc->type_opaque, mc->data); @@ -1952,7 +2189,7 @@ arp_term_l2bd (vlib_main_t * vm, } } - // lookup BD mac_by_ip4 hash table for MAC entry + /* lookup BD mac_by_ip4 hash table for MAC entry */ ip0 = arp0->ip4_over_ethernet[1].ip4.as_u32; bd_index0 = vnet_buffer (p0)->l2.bd_index; if (PREDICT_FALSE ((bd_index0 != last_bd_index) @@ -1964,10 +2201,10 @@ arp_term_l2bd (vlib_main_t * vm, macp0 = (u8 *) hash_get (last_bd_config->mac_by_ip4, ip0); if (PREDICT_FALSE (!macp0)) - goto next_l2_feature; // MAC not found + goto next_l2_feature; /* MAC not found */ - // MAC found, send ARP reply - - // Convert ARP request packet to ARP reply + /* MAC found, send ARP reply - + Convert ARP request packet to ARP reply */ arp0->opcode = clib_host_to_net_u16 (ETHERNET_ARP_OPCODE_reply); arp0->ip4_over_ethernet[1] = arp0->ip4_over_ethernet[0]; arp0->ip4_over_ethernet[0].ip4.as_u32 = ip0; @@ -1976,8 +2213,9 @@ arp_term_l2bd (vlib_main_t * vm, clib_memcpy (eth0->src_address, macp0, 6); n_replies_sent += 1; - // For BVI, need to use l2-fwd node to send ARP reply as - // l2-output node cannot output packet to BVI properly + output_response: + /* For BVI, need to use l2-fwd node to send ARP reply as + l2-output node cannot output packet to BVI properly */ cfg0 = vec_elt_at_index (l2im->configs, sw_if_index0); if (PREDICT_FALSE (cfg0->bvi)) { @@ -1986,28 +2224,46 @@ arp_term_l2bd (vlib_main_t * vm, goto next_l2_feature; } - // Send ARP reply back out input interface through l2-output + /* Send ARP/ND reply back out input interface through l2-output */ vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index0; next0 = ARP_TERM_NEXT_L2_OUTPUT; - // Note that output to VXLAN tunnel will fail due to SHG which - // is probably desireable since ARP termination is not intended - // for ARP requests from other hosts. If output to VXLAN tunnel is - // required, however, can just clear the SHG in packet as follows: - // vnet_buffer(p0)->l2.shg = 0; - - vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next, - n_left_to_next, pi0, next0); + /* Note that output to VXLAN tunnel will fail due to SHG which + is probably desireable since ARP termination is not intended + for ARP requests from other hosts. If output to VXLAN tunnel is + required, however, can just clear the SHG in packet as follows: + vnet_buffer(p0)->l2.shg = 0; */ + vlib_validate_buffer_enqueue_x1 (vm, node, next_index, + to_next, n_left_to_next, pi0, + next0); continue; + check_ip6_nd: + /* IP6 ND event notification or solicitation handling to generate + local response instead of flooding */ + iph0 = (ip6_header_t *) l3h0; + if (PREDICT_FALSE (ethertype0 == ETHERNET_TYPE_IP6 && + iph0->protocol == IP_PROTOCOL_ICMP6 && + !ip6_address_is_unspecified + (&iph0->src_address))) + { + sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX]; + if (vnet_ip6_nd_term + (vm, node, p0, eth0, iph0, sw_if_index0, + vnet_buffer (p0)->l2.bd_index, vnet_buffer (p0)->l2.shg)) + goto output_response; + } + next_l2_feature: { u32 feature_bitmap0 = vnet_buffer (p0)->l2.feature_bitmap & ~L2INPUT_FEAT_ARP_TERM; vnet_buffer (p0)->l2.feature_bitmap = feature_bitmap0; - next0 = feat_bitmap_get_next_node_index (arp_term_next_node_index, - feature_bitmap0); - vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next, - n_left_to_next, pi0, next0); + next0 = + feat_bitmap_get_next_node_index (arp_term_next_node_index, + feature_bitmap0); + vlib_validate_buffer_enqueue_x1 (vm, node, next_index, + to_next, n_left_to_next, + pi0, next0); continue; } @@ -2021,8 +2277,9 @@ arp_term_l2bd (vlib_main_t * vm, next0 = ARP_TERM_NEXT_DROP; p0->error = node->errors[error0]; - vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next, - n_left_to_next, pi0, next0); + vlib_validate_buffer_enqueue_x1 (vm, node, next_index, + to_next, n_left_to_next, pi0, + next0); } vlib_put_next_frame (vm, node, next_index, n_left_to_next); @@ -2046,13 +2303,14 @@ VLIB_REGISTER_NODE (arp_term_l2bd_node, static) = { [ARP_TERM_NEXT_DROP] = "error-drop", }, .format_buffer = format_ethernet_arp_header, - .format_trace = format_ethernet_arp_input_trace, + .format_trace = format_arp_term_input_trace, }; /* *INDENT-ON* */ clib_error_t * arp_term_init (vlib_main_t * vm) -{ // Initialize the feature next-node indexes +{ + // Initialize the feature next-node indexes feat_bitmap_init_next_nodes (vm, arp_term_l2bd_node.index, L2INPUT_N_FEAT, @@ -2063,6 +2321,30 @@ arp_term_init (vlib_main_t * vm) VLIB_INIT_FUNCTION (arp_term_init); +void +change_arp_mac (u32 sw_if_index, ethernet_arp_ip4_entry_t * e) +{ + if (e->sw_if_index == sw_if_index) + { + adj_nbr_walk_nh4 (e->sw_if_index, + &e->ip4_address, arp_mk_complete_walk, e); + } +} + +void +ethernet_arp_change_mac (vnet_main_t * vnm, u32 sw_if_index) +{ + ethernet_arp_main_t *am = ðernet_arp_main; + ethernet_arp_ip4_entry_t *e; + + /* *INDENT-OFF* */ + pool_foreach (e, am->ip4_entry_pool, + ({ + change_arp_mac (sw_if_index, e); + })); + /* *INDENT-ON* */ +} + /* * fd.io coding-style-patch-verification: ON *