diff options
Diffstat (limited to 'vnet/vnet/ethernet')
-rw-r--r-- | vnet/vnet/ethernet/arp.c | 1853 | ||||
-rw-r--r-- | vnet/vnet/ethernet/arp_packet.h | 134 | ||||
-rw-r--r-- | vnet/vnet/ethernet/cli.c | 124 | ||||
-rw-r--r-- | vnet/vnet/ethernet/error.def | 46 | ||||
-rw-r--r-- | vnet/vnet/ethernet/ethernet.h | 450 | ||||
-rw-r--r-- | vnet/vnet/ethernet/format.c | 301 | ||||
-rw-r--r-- | vnet/vnet/ethernet/init.c | 95 | ||||
-rw-r--r-- | vnet/vnet/ethernet/interface.c | 462 | ||||
-rw-r--r-- | vnet/vnet/ethernet/mac_swap.c | 380 | ||||
-rw-r--r-- | vnet/vnet/ethernet/node.c | 1112 | ||||
-rw-r--r-- | vnet/vnet/ethernet/packet.h | 95 | ||||
-rw-r--r-- | vnet/vnet/ethernet/pg.c | 177 | ||||
-rw-r--r-- | vnet/vnet/ethernet/types.def | 112 |
13 files changed, 5341 insertions, 0 deletions
diff --git a/vnet/vnet/ethernet/arp.c b/vnet/vnet/ethernet/arp.c new file mode 100644 index 00000000000..446c76a1e41 --- /dev/null +++ b/vnet/vnet/ethernet/arp.c @@ -0,0 +1,1853 @@ +/* + * ethernet/arp.c: IP v4 ARP node + * + * Copyright (c) 2010 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <vnet/ip/ip.h> +#include <vnet/ethernet/ethernet.h> +#include <vnet/ethernet/arp_packet.h> +#include <vnet/l2/l2_input.h> +#include <vppinfra/mhash.h> + +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]; + + u16 flags; +#define ETHERNET_ARP_IP4_ENTRY_FLAG_STATIC (1 << 0) + + u64 cpu_time_last_updated; +} ethernet_arp_ip4_entry_t; + +typedef struct { + u32 lo_addr; + u32 hi_addr; + u32 fib_index; +} ethernet_proxy_arp_t; + +typedef struct { + u32 next_index; + uword node_index; + uword type_opaque; + uword data; + /* Used for arp event notification only */ + void * data_callback; + u32 pid; +} pending_resolution_t; + +typedef struct { + /* Hash tables mapping name to opcode. */ + uword * opcode_by_name; + + /* lite beer "glean" adjacency handling */ + uword * pending_resolutions_by_address; + pending_resolution_t * pending_resolutions; + + /* Mac address change notification */ + uword * mac_changes_by_address; + pending_resolution_t * mac_changes; + + u32 * arp_input_next_index_by_hw_if_index; + + 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; + + /* Proxy arp vector */ + ethernet_proxy_arp_t * proxy_arps; +} ethernet_arp_main_t; + +static ethernet_arp_main_t ethernet_arp_main; + +static u8 * format_ethernet_arp_hardware_type (u8 * s, va_list * va) +{ + ethernet_arp_hardware_type_t h = va_arg (*va, ethernet_arp_hardware_type_t); + char * t = 0; + switch (h) + { +#define _(n,f) case n: t = #f; break; + foreach_ethernet_arp_hardware_type; +#undef _ + + default: + return format (s, "unknown 0x%x", h); + } + + return format (s, "%s", t); +} + +static u8 * format_ethernet_arp_opcode (u8 * s, va_list * va) +{ + ethernet_arp_opcode_t o = va_arg (*va, ethernet_arp_opcode_t); + char * t = 0; + switch (o) + { +#define _(f) case ETHERNET_ARP_OPCODE_##f: t = #f; break; + foreach_ethernet_arp_opcode; +#undef _ + + default: + return format (s, "unknown 0x%x", o); + } + + return format (s, "%s", t); +} + +static uword +unformat_ethernet_arp_opcode_host_byte_order (unformat_input_t * input, + va_list * args) +{ + int * result = va_arg (*args, int *); + ethernet_arp_main_t * am = ðernet_arp_main; + int x, i; + + /* Numeric opcode. */ + if (unformat (input, "0x%x", &x) + || unformat (input, "%d", &x)) + { + if (x >= (1 << 16)) + return 0; + *result = x; + return 1; + } + + /* Named type. */ + if (unformat_user (input, unformat_vlib_number_by_name, + am->opcode_by_name, &i)) + { + *result = i; + return 1; + } + + return 0; +} + +static uword +unformat_ethernet_arp_opcode_net_byte_order (unformat_input_t * input, + va_list * args) +{ + int * result = va_arg (*args, int *); + if (! unformat_user (input, unformat_ethernet_arp_opcode_host_byte_order, result)) + return 0; + + *result = clib_host_to_net_u16 ((u16) *result); + return 1; +} + +static u8 * format_ethernet_arp_header (u8 * s, va_list * va) +{ + ethernet_arp_header_t * a = va_arg (*va, ethernet_arp_header_t *); + u32 max_header_bytes = va_arg (*va, u32); + uword indent; + u16 l2_type, l3_type; + + if (max_header_bytes != 0 && sizeof (a[0]) > max_header_bytes) + return format (s, "ARP header truncated"); + + l2_type = clib_net_to_host_u16 (a->l2_type); + l3_type = clib_net_to_host_u16 (a->l3_type); + + indent = format_get_indent (s); + + s = format (s, "%U, type %U/%U, address size %d/%d", + format_ethernet_arp_opcode, clib_net_to_host_u16 (a->opcode), + format_ethernet_arp_hardware_type, l2_type, + format_ethernet_type, l3_type, + a->n_l2_address_bytes, a->n_l3_address_bytes); + + if (l2_type == ETHERNET_ARP_HARDWARE_TYPE_ethernet + && l3_type == ETHERNET_TYPE_IP4) + { + s = format (s, "\n%U%U/%U -> %U/%U", + format_white_space, indent, + format_ethernet_address, a->ip4_over_ethernet[0].ethernet, + format_ip4_address, &a->ip4_over_ethernet[0].ip4, + format_ethernet_address, a->ip4_over_ethernet[1].ethernet, + format_ip4_address, &a->ip4_over_ethernet[1].ip4); + } + else + { + uword n2 = a->n_l2_address_bytes; + uword n3 = a->n_l3_address_bytes; + s = format (s, "\n%U%U/%U -> %U/%U", + format_white_space, indent, + format_hex_bytes, a->data + 0*n2 + 0*n3, n2, + format_hex_bytes, a->data + 1*n2 + 0*n3, n3, + format_hex_bytes, a->data + 1*n2 + 1*n3, n2, + format_hex_bytes, a->data + 2*n2 + 1*n3, n3); + } + + return s; +} + +static 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; + + if (! e) + return format (s, "%=12s%=6s%=16s%=4s%=20s%=24s", "Time", "FIB", "IP4", + "Static", "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); + s = format (s, "%=12U%=6u%=16U%=4s%=20U%=25U", + format_vlib_cpu_time, vnm->vlib_main, e->cpu_time_last_updated, + fib->table_id, + format_ip4_address, &e->key.ip4_address, + (e->flags & ETHERNET_ARP_IP4_ENTRY_FLAG_STATIC) ? "S" : "", + format_ethernet_address, e->ethernet_address, + format_vnet_sw_interface_name, vnm, si); + + return s; +} + +typedef struct { + u8 packet_data[64]; +} ethernet_arp_input_trace_t; + +static u8 * format_ethernet_arp_input_trace (u8 * s, va_list * va) +{ + 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 *); + + s = format (s, "%U", + format_ethernet_arp_header, + t->packet_data, sizeof (t->packet_data)); + + return s; +} + +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; + + if (! (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)) + { + u32 i, * to_delete = 0; + + pool_foreach (e, am->ip4_entry_pool, ({ + if (e->key.sw_if_index == sw_if_index) + vec_add1 (to_delete, e - am->ip4_entry_pool); + })); + + 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]); + + memcpy (&delme.ethernet, e->ethernet_address, 6); + delme.ip4.as_u32 = e->key.ip4_address.as_u32; + + vnet_arp_unset_ip4_over_ethernet (vnm, e->key.sw_if_index, + e->key.fib_index, &delme); + } + + vec_free (to_delete); + } + + return 0; +} + +VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (ethernet_arp_sw_interface_up_down); + +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 int +vnet_arp_unset_ip4_over_ethernet_internal (vnet_main_t * vnm, + u32 sw_if_index, + u32 fib_index, + void * a_arg); + +typedef struct { + 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; + +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->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); +} + +int +vnet_arp_set_ip4_over_ethernet (vnet_main_t * vnm, + u32 sw_if_index, + u32 fib_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.fib_index = fib_index; + args.is_static = is_static; + args.is_remove = 0; + 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_arp_set_ip4_over_ethernet_internal (vnet_main_t * vnm, + u32 sw_if_index, + u32 fib_index, + void * a_arg, + int is_static) +{ + 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; + vlib_main_t * vm = vlib_get_main(); + ip4_main_t * im = &ip4_main; + int make_new_arp_cache_entry=1; + uword * p; + ip4_add_del_route_args_t args; + ip_adjacency_t adj; + pending_resolution_t * pr, * mc; + + u32 next_index; + + fib_index = (fib_index != (u32)~0) + ? fib_index : im->fib_index_by_sw_if_index[sw_if_index]; + + 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) + { + e = pool_elt_at_index (am->ip4_entry_pool, p[0]); + + /* Refuse to over-write static arp. */ + if (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; + + 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)); + + 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 (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. */ + 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; + + /* Customer(s) waiting for this address to be resolved? */ + p = hash_get (am->pending_resolutions_by_address, a->ip4.as_u32); + if (p) + { + next_index = p[0]; + + while (next_index != (u32)~0) + { + pr = pool_elt_at_index (am->pending_resolutions, next_index); + vlib_process_signal_event (vm, pr->node_index, + pr->type_opaque, + pr->data); + next_index = pr->next_index; + pool_put (am->pending_resolutions, pr); + } + + hash_unset (am->pending_resolutions_by_address, a->ip4.as_u32); + } + + /* Customer(s) requesting ARP event for this address? */ + p = hash_get (am->mac_changes_by_address, a->ip4.as_u32); + if (p) + { + next_index = p[0]; + + while (next_index != (u32)~0) + { + int (*fp)(u32, u8 *, u32, u32); + int rv = 1; + mc = pool_elt_at_index (am->mac_changes, next_index); + fp = mc->data_callback; + + /* Call the user's data callback, return 1 to suppress dup events */ + 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 + */ + if (rv == 0) + vlib_process_signal_event (vm, mc->node_index, + mc->type_opaque, + mc->data); + next_index = mc->next_index; + } + } + + return 0; +} + +void vnet_register_ip4_arp_resolution_event (vnet_main_t * vnm, + void * address_arg, + uword node_index, + uword type_opaque, + uword data) +{ + ethernet_arp_main_t * am = ðernet_arp_main; + ip4_address_t * address = address_arg; + uword * p; + pending_resolution_t * pr; + + pool_get (am->pending_resolutions, pr); + + pr->next_index = ~0; + pr->node_index = node_index; + pr->type_opaque = type_opaque; + pr->data = data; + pr->data_callback = 0; + + p = hash_get (am->pending_resolutions_by_address, address->as_u32); + if (p) + { + /* Insert new resolution at the head of the list */ + pr->next_index = p[0]; + hash_unset (am->pending_resolutions_by_address, address->as_u32); + } + + hash_set (am->pending_resolutions_by_address, address->as_u32, + pr - am->pending_resolutions); +} + +int vnet_add_del_ip4_arp_change_event (vnet_main_t * vnm, + void * data_callback, + u32 pid, + void * address_arg, + uword node_index, + uword type_opaque, + uword data, int is_add) +{ + ethernet_arp_main_t * am = ðernet_arp_main; + ip4_address_t * address = address_arg; + uword * p; + pending_resolution_t * mc; + void (*fp)(u32, u8 *) = data_callback; + + if (is_add) + { + pool_get (am->mac_changes, mc); + + mc->next_index = ~0; + mc->node_index = node_index; + mc->type_opaque = type_opaque; + mc->data = data; + mc->data_callback = data_callback; + mc->pid = pid; + + p = hash_get (am->mac_changes_by_address, address->as_u32); + if (p) + { + /* Insert new resolution at the head of the list */ + mc->next_index = p[0]; + hash_unset (am->mac_changes_by_address, address->as_u32); + } + + hash_set (am->mac_changes_by_address, address->as_u32, + mc - am->mac_changes); + return 0; + } + else + { + u32 index; + pending_resolution_t * mc_last = 0; + + p = hash_get (am->mac_changes_by_address, address->as_u32); + if (p == 0) + return VNET_API_ERROR_NO_SUCH_ENTRY; + + index = p[0]; + + while (index != (u32)~0) + { + mc = pool_elt_at_index (am->mac_changes, index); + if (mc->node_index == node_index && + mc->type_opaque == type_opaque && + mc->pid == pid) + { + /* Clients may need to clean up pool entries, too */ + if (fp) + (*fp)(mc->data, 0 /* no new mac addrs */); + if (index == p[0]) + { + hash_unset (am->mac_changes_by_address, address->as_u32); + if (mc->next_index != ~0) + hash_set (am->mac_changes_by_address, address->as_u32, + mc->next_index); + pool_put (am->mac_changes, mc); + return 0; + } + else + { + ASSERT(mc_last); + mc_last->next_index = mc->next_index; + pool_put (am->mac_changes, mc); + return 0; + } + } + mc_last = mc; + index = mc->next_index; + } + + return VNET_API_ERROR_NO_SUCH_ENTRY; + } +} + +/* Either we drop the packet or we send a reply to the sender. */ +typedef enum { + ARP_INPUT_NEXT_DROP, + ARP_INPUT_N_NEXT, +} arp_input_next_t; + +#define foreach_ethernet_arp_error \ + _ (replies_sent, "ARP replies sent") \ + _ (l2_type_not_ethernet, "L2 type not ethernet") \ + _ (l3_type_not_ip4, "L3 type not IP4") \ + _ (l3_src_address_not_local, "IP4 source address not local to subnet") \ + _ (l3_dst_address_not_local, "IP4 destination address not local to subnet") \ + _ (l3_src_address_is_local, "IP4 source address matches local interface") \ + _ (l3_src_address_learned, "ARP request IP4 source address learned") \ + _ (replies_received, "ARP replies received") \ + _ (opcode_not_request, "ARP opcode not request") \ + _ (proxy_arp_replies_sent, "Proxy ARP replies sent") \ + _ (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") \ + +typedef enum { +#define _(sym,string) ETHERNET_ARP_ERROR_##sym, + foreach_ethernet_arp_error +#undef _ + 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; + + 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; + })); + if (result_ia) + *result_ia = result ? ia : 0; + return result; +} + +static void unset_random_arp_entry (void) +{ + ethernet_arp_main_t * am = ðernet_arp_main; + ethernet_arp_ip4_entry_t * e; + vnet_main_t * vnm = vnet_get_main(); + ethernet_arp_ip4_over_ethernet_address_t delme; + u32 index; + + index = pool_next_index (am->ip4_entry_pool, am->arp_delete_rotor); + am->arp_delete_rotor = index; + + /* Try again from elt 0, could happen if an intfc goes down */ + if (index == ~0) + { + index = pool_next_index (am->ip4_entry_pool, am->arp_delete_rotor); + am->arp_delete_rotor = index; + } + + /* Nothing left in the pool */ + if (index == ~0) + return; + + e = pool_elt_at_index (am->ip4_entry_pool, index); + + memcpy (&delme.ethernet, e->ethernet_address, 6); + delme.ip4.as_u32 = e->key.ip4_address.as_u32; + + vnet_arp_unset_ip4_over_ethernet (vnm, e->key.sw_if_index, + e->key.fib_index, &delme); +} + +static u32 arp_unnumbered (vlib_buffer_t * p0, + u32 pi0, + ethernet_header_t * eth0, + ip_interface_address_t * ifa0) +{ + ethernet_arp_main_t * am = ðernet_arp_main; + vlib_main_t * vm = vlib_get_main(); + vnet_main_t * vnm = vnet_get_main(); + vnet_interface_main_t * vim = &vnm->interface_main; + vnet_sw_interface_t * si; + vnet_hw_interface_t * hi; + u32 unnum_src_sw_if_index; + u32 * broadcast_swifs = 0; + u32 * buffers = 0; + u32 n_alloc = 0; + vlib_buffer_t * b0; + int i; + u8 dst_mac_address[6]; + i16 header_size; + ethernet_arp_header_t * arp0; + + /* Save the dst mac address */ + 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; + + /* Track down all users of the unnumbered source */ + pool_foreach (si, vim->sw_interfaces, + ({ + if (si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED && + (si->unnumbered_sw_if_index == unnum_src_sw_if_index)) + { + vec_add1 (broadcast_swifs, si->sw_if_index); + } + })); + + + ASSERT (vec_len(broadcast_swifs)); + + /* Allocate buffering if we need it */ + if (vec_len(broadcast_swifs) > 1) + { + vec_validate (buffers, vec_len(broadcast_swifs)-2); + n_alloc = vlib_buffer_alloc (vm, buffers, vec_len(buffers)); + _vec_len (buffers) = n_alloc; + for (i = 0; i < n_alloc; i++) + { + b0 = vlib_get_buffer (vm, buffers[i]); + + /* xerox (partially built) ARP pkt */ + memcpy (b0->data, p0->data, p0->current_length + p0->current_data); + b0->current_data = p0->current_data; + b0->current_length = p0->current_length; + vnet_buffer(b0)->sw_if_index[VLIB_RX] = + vnet_buffer(p0)->sw_if_index[VLIB_RX]; + } + } + + vec_insert (buffers, 1, 0); + buffers[0] = pi0; + + for (i = 0; i < vec_len(buffers); i++) + { + b0 = vlib_get_buffer(vm, buffers[i]); + arp0 = vlib_buffer_get_current (b0); + + hi = vnet_get_sup_hw_interface (vnm, broadcast_swifs[i]); + si = vnet_get_sw_interface (vnm, broadcast_swifs[i]); + + /* For decoration, most likely */ + vnet_buffer(b0)->sw_if_index[VLIB_TX] = hi->sw_if_index; + + /* Fix ARP pkt src address */ + memcpy (arp0->ip4_over_ethernet[0].ethernet, hi->hw_address, 6); + + /* Build L2 encaps for this swif */ + header_size = sizeof (ethernet_header_t); + if (si->sub.eth.flags.one_tag) + header_size += 4; + else if (si->sub.eth.flags.two_tags) + header_size += 8; + + vlib_buffer_advance (b0, -header_size); + eth0 = vlib_buffer_get_current (b0); + + if (si->sub.eth.flags.one_tag) { + ethernet_vlan_header_t * outer = (void *) (eth0 + 1); + + eth0->type = si->sub.eth.flags.dot1ad ? + clib_host_to_net_u16 (ETHERNET_TYPE_DOT1AD) : + clib_host_to_net_u16 (ETHERNET_TYPE_VLAN); + outer->priority_cfi_and_id = + clib_host_to_net_u16 (si->sub.eth.outer_vlan_id); + outer->type = clib_host_to_net_u16 (ETHERNET_TYPE_ARP); + + } else if (si->sub.eth.flags.two_tags) { + ethernet_vlan_header_t * outer = (void *) (eth0 + 1); + ethernet_vlan_header_t * inner = (void *) (outer + 1); + + eth0->type = si->sub.eth.flags.dot1ad ? + clib_host_to_net_u16 (ETHERNET_TYPE_DOT1AD) : + clib_host_to_net_u16 (ETHERNET_TYPE_VLAN); + outer->priority_cfi_and_id = + clib_host_to_net_u16 (si->sub.eth.outer_vlan_id); + outer->type = clib_host_to_net_u16 (ETHERNET_TYPE_VLAN); + inner->priority_cfi_and_id = + clib_host_to_net_u16 (si->sub.eth.inner_vlan_id); + inner->type = clib_host_to_net_u16 (ETHERNET_TYPE_ARP); + + } else { + eth0->type = clib_host_to_net_u16 (ETHERNET_TYPE_ARP); + } + + /* Restore the original dst address, set src address */ + memcpy (eth0->dst_address, dst_mac_address, sizeof (eth0->dst_address)); + memcpy (eth0->src_address, hi->hw_address, sizeof (eth0->src_address)); + + /* Transmit replicas */ + if (i > 0) + { + 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] = buffers[i]; + f->n_vectors = 1; + vlib_put_frame_to_node (vm, hi->output_node_index, f); + } + } + + hi = vnet_get_sup_hw_interface (vnm, broadcast_swifs[0]); + + vec_free (broadcast_swifs); + vec_free (buffers); + + /* The regular path outputs the original pkt.. */ + return vec_elt (am->arp_input_next_index_by_hw_if_index, hi->hw_if_index); +} + +static uword +arp_input (vlib_main_t * vm, + vlib_node_runtime_t * node, + vlib_frame_t * frame) +{ + ethernet_arp_main_t * am = ðernet_arp_main; + vnet_main_t * vnm = vnet_get_main(); + ip4_main_t * im4 = &ip4_main; + u32 n_left_from, next_index, * from, * to_next; + u32 n_replies_sent = 0, n_proxy_arp_replies_sent = 0; + + from = vlib_frame_vector_args (frame); + n_left_from = frame->n_vectors; + next_index = node->cached_next_index; + + if (node->flags & VLIB_NODE_FLAG_TRACE) + vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors, + /* stride */ 1, + sizeof (ethernet_arp_input_trace_t)); + + while (n_left_from > 0) + { + u32 n_left_to_next; + + vlib_get_next_frame (vm, node, next_index, + to_next, n_left_to_next); + + while (n_left_from > 0 && n_left_to_next > 0) + { + vlib_buffer_t * p0; + 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; + ethernet_proxy_arp_t * pa; + + pi0 = from[0]; + to_next[0] = pi0; + from += 1; + to_next += 1; + n_left_from -= 1; + n_left_to_next -= 1; + + p0 = vlib_get_buffer (vm, pi0); + arp0 = vlib_buffer_get_current (p0); + + is_request0 = arp0->opcode + == clib_host_to_net_u16 (ETHERNET_ARP_OPCODE_request); + + 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); + error0 = (arp0->l3_type != clib_net_to_host_u16 (ETHERNET_TYPE_IP4) + ? ETHERNET_ARP_ERROR_l3_type_not_ip4 + : error0); + + sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX]; + + if (error0) + goto drop1; + + /* 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) + { + 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; + + /* Source must also be local to subnet of matching interface address. */ + if (! ip4_destination_matches_interface (im4, &arp0->ip4_over_ethernet[0].ip4, ifa0)) + { + error0 = ETHERNET_ARP_ERROR_l3_src_address_not_local; + goto drop1; + } + + /* 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) + { + error0 = ETHERNET_ARP_ERROR_l3_src_address_is_local; + goto drop1; + } + + dst_is_local0 = if_addr0->as_u32 == arp0->ip4_over_ethernet[1].ip4.as_u32; + + /* Fill in ethernet header. */ + eth0 = ethernet_buffer_get_header (p0); + + /* Trash ARP packets whose ARP-level source addresses do not + match their L2-frame-level source addresses */ + if (memcmp (eth0->src_address, arp0->ip4_over_ethernet[0].ethernet, + sizeof (eth0->src_address))) + { + error0 = ETHERNET_ARP_ERROR_l2_address_mismatch; + goto drop2; + } + + /* Learn or update sender's mapping only for requests or unicasts + that don't match local interface address. */ + if (ethernet_address_cast (eth0->dst_address) == ETHERNET_ADDRESS_UNICAST + || is_request0) + { + if (am->limit_arp_cache_size && + pool_elts (am->ip4_entry_pool) >= am->limit_arp_cache_size) + 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; + } + + /* Only send a reply for requests sent which match a local interface. */ + if (! (is_request0 && dst_is_local0)) + { + error0 = (arp0->opcode == clib_host_to_net_u16 (ETHERNET_ARP_OPCODE_reply) + ? ETHERNET_ARP_ERROR_replies_received : error0); + goto drop1; + } + + /* Send a reply. */ + send_reply: + vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index0; + hw_if0 = vnet_get_sup_hw_interface (vnm, sw_if_index0); + + /* Can happen in a multi-core env. */ + if (PREDICT_FALSE(hw_if0->hw_if_index >= vec_len (am->arp_input_next_index_by_hw_if_index))) + { + error0 = ETHERNET_ARP_ERROR_missing_interface_address; + goto drop2; + } + + next0 = vec_elt (am->arp_input_next_index_by_hw_if_index, hw_if0->hw_if_index); + + arp0->opcode = clib_host_to_net_u16 (ETHERNET_ARP_OPCODE_reply); + + arp0->ip4_over_ethernet[1] = arp0->ip4_over_ethernet[0]; + + memcpy (arp0->ip4_over_ethernet[0].ethernet, hw_if0->hw_address, 6); + clib_mem_unaligned (&arp0->ip4_over_ethernet[0].ip4.data_u32, u32) = if_addr0->data_u32; + + /* Hardware must be ethernet-like. */ + ASSERT (vec_len (hw_if0->hw_address) == 6); + + memcpy (eth0->dst_address, eth0->src_address, 6); + memcpy (eth0->src_address, hw_if0->hw_address, 6); + + /* Figure out how much to rewind current data from adjacency. */ + if (ifa0) + { + adj0 = ip_get_adjacency (&ip4_main.lookup_main, + ifa0->neighbor_probe_adj_index); + if (adj0->lookup_next_index != IP_LOOKUP_NEXT_ARP) + { + error0 = ETHERNET_ARP_ERROR_missing_interface_address; + goto drop2; + } + if (is_unnum0) + next0 = arp_unnumbered (p0, pi0, eth0, ifa0); + 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); + + n_replies_sent += 1; + continue; + + drop1: + if (0 == arp0->ip4_over_ethernet[0].ip4.as_u32 || + (arp0->ip4_over_ethernet[0].ip4.as_u32 == + arp0->ip4_over_ethernet[1].ip4.as_u32)) + { + error0 = ETHERNET_ARP_ERROR_gratuitous_arp; + goto drop2; + } + /* See if proxy arp is configured for the address */ + if (is_request0) + { + vnet_sw_interface_t * si; + u32 this_addr = clib_net_to_host_u32 + (arp0->ip4_over_ethernet[1].ip4.as_u32); + u32 fib_index0; + + si = vnet_get_sw_interface (vnm, sw_if_index0); + + if (!(si->flags & VNET_SW_INTERFACE_FLAG_PROXY_ARP)) + goto drop2; + + fib_index0 = vec_elt (im4->fib_index_by_sw_if_index, + sw_if_index0); + + vec_foreach (pa, am->proxy_arps) + { + u32 lo_addr = clib_net_to_host_u32 (pa->lo_addr); + u32 hi_addr = clib_net_to_host_u32 (pa->hi_addr); + + /* an ARP request hit in the proxy-arp table? */ + if ((this_addr >= lo_addr && this_addr <= hi_addr) && + (fib_index0 == pa->fib_index)) + { + eth0 = ethernet_buffer_get_header (p0); + 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(..)? + */ + ifa0 = 0; + if_addr0 = &proxy_src; + vlib_buffer_reset (p0); + n_proxy_arp_replies_sent++; + goto send_reply; + } + } + } + + drop2: + + next0 = ARP_INPUT_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_put_next_frame (vm, node, next_index, n_left_to_next); + } + + vlib_error_count (vm, node->node_index, + ETHERNET_ARP_ERROR_replies_sent, + n_replies_sent - n_proxy_arp_replies_sent); + + vlib_error_count (vm, node->node_index, + ETHERNET_ARP_ERROR_proxy_arp_replies_sent, + n_proxy_arp_replies_sent); + return frame->n_vectors; +} + +static char * ethernet_arp_error_strings[] = { +#define _(sym,string) string, + foreach_ethernet_arp_error +#undef _ +}; + +VLIB_REGISTER_NODE (arp_input_node,static) = { + .function = arp_input, + .name = "arp-input", + .vector_size = sizeof (u32), + + .n_errors = ETHERNET_ARP_N_ERROR, + .error_strings = ethernet_arp_error_strings, + + .n_next_nodes = ARP_INPUT_N_NEXT, + .next_nodes = { + [ARP_INPUT_NEXT_DROP] = "error-drop", + }, + + .format_buffer = format_ethernet_arp_header, + .format_trace = format_ethernet_arp_input_trace, +}; + +clib_error_t * +ethernet_arp_hw_interface_link_up_down (vnet_main_t * vnm, + u32 hw_if_index, + u32 flags) +{ + ethernet_arp_main_t * am = ðernet_arp_main; + vnet_hw_interface_t * hw_if; + + hw_if = vnet_get_hw_interface (vnm, hw_if_index); + + /* Fill in lookup tables with default table (0). */ + vec_validate_init_empty (am->arp_input_next_index_by_hw_if_index, hw_if_index, ~0); + am->arp_input_next_index_by_hw_if_index[hw_if_index] + = vlib_node_add_next (vnm->vlib_main, arp_input_node.index, hw_if->output_node_index); + + return 0; +} + +VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION (ethernet_arp_hw_interface_link_up_down); + +static int +ip4_arp_entry_sort (void *a1, void *a2) +{ + ethernet_arp_ip4_entry_t * e1 = a1; + ethernet_arp_ip4_entry_t * e2 = 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); + if (! cmp) + cmp = ip4_address_compare (&e1->key.ip4_address, &e2->key.ip4_address); + return cmp; +} + +static clib_error_t * +show_ip4_arp (vlib_main_t * vm, + unformat_input_t * input, + vlib_cli_command_t * cmd) +{ + vnet_main_t * vnm = vnet_get_main(); + ethernet_arp_main_t * am = ðernet_arp_main; + ethernet_arp_ip4_entry_t * e, * es; + ethernet_proxy_arp_t * pa; + clib_error_t * error = 0; + u32 sw_if_index; + + /* Filter entries by interface if given. */ + sw_if_index = ~0; + (void) unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index); + + es = 0; + pool_foreach (e, am->ip4_entry_pool, ({ vec_add1 (es, e[0]); })); + 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); + + if (vec_len (am->proxy_arps)) + { + vlib_cli_output (vm, "Proxy arps enabled for:"); + vec_foreach(pa, am->proxy_arps) + { + vlib_cli_output (vm, "Fib_index %d %U - %U ", + pa->fib_index, + format_ip4_address, &pa->lo_addr, + format_ip4_address, &pa->hi_addr); + } + } + + return error; +} + +VLIB_CLI_COMMAND (show_ip4_arp_command, static) = { + .path = "show ip arp", + .function = show_ip4_arp, + .short_help = "Show ARP table", +}; + +typedef struct { + pg_edit_t l2_type, l3_type; + pg_edit_t n_l2_address_bytes, n_l3_address_bytes; + pg_edit_t opcode; + struct { + pg_edit_t ethernet; + pg_edit_t ip4; + } ip4_over_ethernet[2]; +} pg_ethernet_arp_header_t; + +static inline void +pg_ethernet_arp_header_init (pg_ethernet_arp_header_t * p) +{ + /* Initialize fields that are not bit fields in the IP header. */ +#define _(f) pg_edit_init (&p->f, ethernet_arp_header_t, f); + _ (l2_type); + _ (l3_type); + _ (n_l2_address_bytes); + _ (n_l3_address_bytes); + _ (opcode); + _ (ip4_over_ethernet[0].ethernet); + _ (ip4_over_ethernet[0].ip4); + _ (ip4_over_ethernet[1].ethernet); + _ (ip4_over_ethernet[1].ip4); +#undef _ +} + +uword +unformat_pg_arp_header (unformat_input_t * input, va_list * args) +{ + pg_stream_t * s = va_arg (*args, pg_stream_t *); + pg_ethernet_arp_header_t * p; + u32 group_index; + + p = pg_create_edit_group (s, sizeof (p[0]), sizeof (ethernet_arp_header_t), + &group_index); + pg_ethernet_arp_header_init (p); + + /* Defaults. */ + pg_edit_set_fixed (&p->l2_type, ETHERNET_ARP_HARDWARE_TYPE_ethernet); + pg_edit_set_fixed (&p->l3_type, ETHERNET_TYPE_IP4); + pg_edit_set_fixed (&p->n_l2_address_bytes, 6); + pg_edit_set_fixed (&p->n_l3_address_bytes, 4); + + if (! unformat (input, "%U: %U/%U -> %U/%U", + unformat_pg_edit, + unformat_ethernet_arp_opcode_net_byte_order, &p->opcode, + unformat_pg_edit, + unformat_ethernet_address, &p->ip4_over_ethernet[0].ethernet, + unformat_pg_edit, + unformat_ip4_address, &p->ip4_over_ethernet[0].ip4, + unformat_pg_edit, + unformat_ethernet_address, &p->ip4_over_ethernet[1].ethernet, + unformat_pg_edit, + unformat_ip4_address, &p->ip4_over_ethernet[1].ip4)) + { + /* Free up any edits we may have added. */ + pg_free_edit_group (s); + return 0; + } + return 1; +} + +clib_error_t *ip4_set_arp_limit (u32 arp_limit) +{ + ethernet_arp_main_t * am = ðernet_arp_main; + + am->limit_arp_cache_size = arp_limit; + return 0; +} + +static clib_error_t * ethernet_arp_init (vlib_main_t * vm) +{ + ethernet_arp_main_t * am = ðernet_arp_main; + pg_node_t * pn; + + ethernet_register_input_type (vm, ETHERNET_TYPE_ARP, arp_input_node.index); + + pn = pg_get_node (arp_input_node.index); + pn->unformat_edit = unformat_pg_arp_header; + + am->opcode_by_name = hash_create_string (0, sizeof (uword)); +#define _(o) hash_set_mem (am->opcode_by_name, #o, ETHERNET_ARP_OPCODE_##o); + 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; + + am->pending_resolutions_by_address = hash_create (0, sizeof (uword)); + am->mac_changes_by_address = hash_create (0, sizeof (uword)); + + /* don't trace ARP error packets */ + { + vlib_node_runtime_t *rt = + vlib_node_get_runtime (vm, arp_input_node.index); + +#define _(a,b) \ + vnet_pcap_drop_trace_filter_add_del \ + (rt->errors[ETHERNET_ARP_ERROR_##a], \ + 1 /* is_add */); + foreach_ethernet_arp_error +#undef _ + } + + 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) +{ + 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; + 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 inline int +vnet_arp_unset_ip4_over_ethernet_internal (vnet_main_t * vnm, + u32 sw_if_index, + u32 fib_index, + void * a_arg) +{ + ethernet_arp_ip4_entry_t * e; + 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; + + 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 (...). + */ + + adj_index = ip4_fib_lookup_with_table + (im, fib_index, &a->ip4, 1 /* disable default route */); + + /* Miss adj? Forget it... */ + if (adj_index != lm->miss_adj_index) { + 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. + */ + if (adj->lookup_next_index == IP_LOOKUP_NEXT_REWRITE) { + 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); + } + } + + 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; +} + +static void +increment_ip4_and_mac_address (ethernet_arp_ip4_over_ethernet_address_t *a) +{ + u8 old; + int i; + + for (i = 3; i >= 0; i--) + { + old = a->ip4.as_u8[i]; + a->ip4.as_u8[i] += 1; + if (old < a->ip4.as_u8[i]) + break; + } + + for (i = 5; i >= 0; i--) + { + old = a->ethernet[i]; + a->ethernet[i] += 1; + if (old < a->ethernet[i]) + break; + } +} + +int vnet_proxy_arp_add_del (ip4_address_t *lo_addr, + ip4_address_t *hi_addr, + u32 fib_index, int is_del) +{ + ethernet_arp_main_t *am = ðernet_arp_main; + ethernet_proxy_arp_t *pa; + u32 found_at_index = ~0; + + vec_foreach (pa, am->proxy_arps) + { + if (pa->lo_addr == lo_addr->as_u32 + && pa->hi_addr == hi_addr->as_u32 + && pa->fib_index == fib_index) + { + found_at_index = pa - am->proxy_arps; + break; + } + } + + if (found_at_index != ~0) + { + /* Delete, otherwise it's already in the table */ + if (is_del) + vec_delete (am->proxy_arps, 1, found_at_index); + return 0; + } + /* delete, no such entry */ + if (is_del) + return VNET_API_ERROR_NO_SUCH_ENTRY; + + /* add, not in table */ + vec_add2 (am->proxy_arps, pa, 1); + pa->lo_addr = lo_addr->as_u32; + pa->hi_addr = hi_addr->as_u32; + pa->fib_index = fib_index; + return 0; +} + +/* + * Remove any proxy arp entries asdociated with the + * specificed fib. + */ +int vnet_proxy_arp_fib_reset (u32 fib_id) +{ + ip4_main_t * im = &ip4_main; + ethernet_arp_main_t *am = ðernet_arp_main; + ethernet_proxy_arp_t *pa; + u32 * entries_to_delete = 0; + u32 fib_index; + uword * p; + int i; + + p = hash_get (im->fib_index_by_table_id, fib_id); + if (! p) + return VNET_API_ERROR_NO_SUCH_ENTRY; + fib_index = p[0]; + + vec_foreach (pa, am->proxy_arps) + { + if (pa->fib_index == fib_index) + { + vec_add1 (entries_to_delete, pa - am->proxy_arps); + } + } + + for (i = 0; i < vec_len(entries_to_delete); i++) + { + vec_delete (am->proxy_arps, 1, entries_to_delete[i]); + } + + vec_free (entries_to_delete); + + return 0; +} + +static clib_error_t * +ip_arp_add_del_command_fn (vlib_main_t * vm, + unformat_input_t * input, + vlib_cli_command_t * cmd) +{ + vnet_main_t * vnm = vnet_get_main(); + u32 sw_if_index; + ethernet_arp_ip4_over_ethernet_address_t lo_addr, hi_addr, addr; + int addr_valid = 0; + int is_del = 0; + int count = 1; + u32 fib_index = 0; + u32 fib_id; + int is_static = 0; + int is_proxy = 0; + + while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) + { + /* set ip arp TenGigE1/1/0/1 1.2.3.4 aa:bb:... or aabb.ccdd... */ + if (unformat (input, "%U %U %U", + unformat_vnet_sw_interface, vnm, &sw_if_index, + unformat_ip4_address, &addr.ip4, + unformat_ethernet_address, &addr.ethernet)) + addr_valid = 1; + + else if (unformat (input, "delete") || unformat (input, "del")) + is_del = 1; + + else if (unformat (input, "static")) + is_static = 1; + + else if (unformat (input, "count %d", &count)) + ; + + else if (unformat (input, "fib-id %d", &fib_id)) + { + ip4_main_t * im = &ip4_main; + uword * p = hash_get (im->fib_index_by_table_id, fib_id); + if (! p) + return clib_error_return (0, "fib ID %d doesn't exist\n", + fib_id); + fib_index = p[0]; + } + + else if (unformat (input, "proxy %U - %U", + unformat_ip4_address, &lo_addr.ip4, + unformat_ip4_address, &hi_addr.ip4)) + is_proxy = 1; + else + break; + } + + if (is_proxy) + { + (void) vnet_proxy_arp_add_del (&lo_addr.ip4, &hi_addr.ip4, + fib_index, is_del); + return 0; + } + + if (addr_valid) + { + int i; + + for (i = 0; i < count; i++) + { + if (is_del == 0) + { + uword event_type, * event_data = 0; + + /* Park the debug CLI until the arp entry is installed */ + vnet_register_ip4_arp_resolution_event + (vnm, &addr.ip4, vlib_current_process(vm), + 1 /* type */, 0 /* data */); + + vnet_arp_set_ip4_over_ethernet + (vnm, sw_if_index, fib_index, &addr, is_static); + + vlib_process_wait_for_event (vm); + event_type = vlib_process_get_events (vm, &event_data); + vec_reset_length(event_data); + if (event_type != 1) + clib_warning ("event type %d unexpected", event_type); + } + else + vnet_arp_unset_ip4_over_ethernet + (vnm, sw_if_index, fib_index, &addr); + + increment_ip4_and_mac_address (&addr); + } + } + else + { + return clib_error_return (0, "unknown input `%U'", + format_unformat_error, input); + } + + return 0; +} + +VLIB_CLI_COMMAND (ip_arp_add_del_command, static) = { + .path = "set ip arp", + .short_help = "set ip arp [del] <intfc> <ip-address> <mac-address>", + .function = ip_arp_add_del_command_fn, +}; + +static clib_error_t * +set_int_proxy_arp_command_fn (vlib_main_t * vm, + unformat_input_t * input, + vlib_cli_command_t * cmd) +{ + vnet_main_t * vnm = vnet_get_main(); + u32 sw_if_index; + vnet_sw_interface_t * si; + int enable = 0; + int intfc_set = 0; + + while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) + { + if (unformat (input, "%U", unformat_vnet_sw_interface, + vnm, &sw_if_index)) + intfc_set = 1; + else if (unformat (input, "enable") || unformat (input, "on")) + enable = 1; + else if (unformat (input, "disable") || unformat (input, "off")) + enable = 0; + else + break; + } + + if (intfc_set == 0) + return clib_error_return (0, "unknown input '%U'", + format_unformat_error, input); + + si = vnet_get_sw_interface (vnm, sw_if_index); + ASSERT(si); + if (enable) + si->flags |= VNET_SW_INTERFACE_FLAG_PROXY_ARP; + else + si->flags &= ~VNET_SW_INTERFACE_FLAG_PROXY_ARP; + + return 0; +} + +VLIB_CLI_COMMAND (set_int_proxy_enable_command, static) = { + .path = "set interface proxy-arp", + .short_help = "set interface proxy-arp <intfc> [enable|disable]", + .function = set_int_proxy_arp_command_fn, +}; + + +/* + * ARP Termination in a L2 Bridge Domain based on an + * IP4 to MAC hash table mac_by_ip4 for each BD. + */ +typedef enum { + ARP_TERM_NEXT_L2_OUTPUT, + ARP_TERM_NEXT_DROP, + ARP_TERM_N_NEXT, +} arp_term_next_t; + +u32 arp_term_next_node_index[32]; + +static uword +arp_term_l2bd (vlib_main_t * vm, + vlib_node_runtime_t * node, + vlib_frame_t * frame) +{ + l2input_main_t * l2im = &l2input_main; + u32 n_left_from, next_index, * from, * to_next; + u32 n_replies_sent = 0; + u16 last_bd_index = ~0; + l2_bridge_domain_t * last_bd_config = 0; + l2_input_config_t * cfg0; + + from = vlib_frame_vector_args (frame); + n_left_from = frame->n_vectors; + next_index = node->cached_next_index; + + while (n_left_from > 0) + { + u32 n_left_to_next; + + vlib_get_next_frame (vm, node, next_index, + to_next, n_left_to_next); + + while (n_left_from > 0 && n_left_to_next > 0) + { + vlib_buffer_t * p0; + ethernet_header_t * eth0; + ethernet_arp_header_t * arp0; + u8 * l3h0; + u32 pi0, error0, next0, sw_if_index0; + u16 ethertype0; + u16 bd_index0; + u32 ip0; + u8 * macp0; + + pi0 = from[0]; + to_next[0] = pi0; + from += 1; + to_next += 1; + n_left_from -= 1; + n_left_to_next -= 1; + + p0 = vlib_get_buffer (vm, pi0); + eth0 = vlib_buffer_get_current (p0); + l3h0 = (u8 *)eth0 + vnet_buffer(p0)->l2.l2_len; + ethertype0 = clib_net_to_host_u16(*(u16 *)(l3h0 - 2)); + arp0 = (ethernet_arp_header_t *) l3h0; + + if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE) && + (p0->flags & VLIB_BUFFER_IS_TRACED))) + { + u8 *t0 = vlib_add_trace ( + vm, node, p0, sizeof(ethernet_arp_input_trace_t)); + 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); + error0 = (arp0->l3_type != clib_net_to_host_u16 (ETHERNET_TYPE_IP4) + ? ETHERNET_ARP_ERROR_l3_type_not_ip4 + : error0); + + sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX]; + + if (error0) + goto drop; + + // 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, + sizeof (eth0->src_address)))) + { + error0 = ETHERNET_ARP_ERROR_l2_address_mismatch; + goto drop; + } + + // Check if anyone want ARP request events for L2 BDs + { + pending_resolution_t * mc; + ethernet_arp_main_t * am = ðernet_arp_main; + uword *p = hash_get (am->mac_changes_by_address, 0); + if (p && (vnet_buffer(p0)->l2.shg == 0)) + { // Only SHG 0 interface which is more likely local + u32 next_index = p[0]; + while (next_index != (u32)~0) + { + int (*fp)(u32, u8 *, u32, u32); + 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 */ + 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 + if (rv == 0) + vlib_process_signal_event (vm, mc->node_index, + mc->type_opaque, + mc->data); + next_index = mc->next_index; + } + } + } + + // 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) || (last_bd_index == ~0))) + { + last_bd_index = bd_index0; + last_bd_config = vec_elt_at_index(l2im->bd_configs, bd_index0); + } + macp0 = (u8 *) hash_get (last_bd_config->mac_by_ip4, ip0); + + if (PREDICT_FALSE(!macp0)) + goto next_l2_feature; // MAC not found + + // 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; + memcpy (arp0->ip4_over_ethernet[0].ethernet, macp0, 6); + memcpy (eth0->dst_address, eth0->src_address, 6); + 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 + cfg0 = vec_elt_at_index(l2im->configs, sw_if_index0); + if (PREDICT_FALSE (cfg0->bvi)) + { + vnet_buffer(p0)->l2.feature_bitmap |= L2INPUT_FEAT_FWD; + vnet_buffer (p0)->sw_if_index[VLIB_RX] = 0; + goto next_l2_feature; + } + + // Send ARP 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); + continue; + + 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); + continue; + } + + drop: + if (0 == arp0->ip4_over_ethernet[0].ip4.as_u32 || + (arp0->ip4_over_ethernet[0].ip4.as_u32 == + arp0->ip4_over_ethernet[1].ip4.as_u32)) + { + error0 = ETHERNET_ARP_ERROR_gratuitous_arp; + } + 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_put_next_frame (vm, node, next_index, n_left_to_next); + } + + vlib_error_count (vm, node->node_index, + ETHERNET_ARP_ERROR_replies_sent, + n_replies_sent); + return frame->n_vectors; +} + +VLIB_REGISTER_NODE (arp_term_l2bd_node,static) = { + .function = arp_term_l2bd, + .name = "arp-term-l2bd", + .vector_size = sizeof (u32), + + .n_errors = ETHERNET_ARP_N_ERROR, + .error_strings = ethernet_arp_error_strings, + + .n_next_nodes = ARP_TERM_N_NEXT, + .next_nodes = { + [ARP_TERM_NEXT_L2_OUTPUT] = "l2-output", + [ARP_TERM_NEXT_DROP] = "error-drop", + }, + + .format_buffer = format_ethernet_arp_header, + .format_trace = format_ethernet_arp_input_trace, +}; + +clib_error_t *arp_term_init (vlib_main_t *vm) +{ // Initialize the feature next-node indexes + feat_bitmap_init_next_nodes(vm, + arp_term_l2bd_node.index, + L2INPUT_N_FEAT, + l2input_get_feat_names(), + arp_term_next_node_index); + return 0; +} + +VLIB_INIT_FUNCTION (arp_term_init); diff --git a/vnet/vnet/ethernet/arp_packet.h b/vnet/vnet/ethernet/arp_packet.h new file mode 100644 index 00000000000..c2214949d89 --- /dev/null +++ b/vnet/vnet/ethernet/arp_packet.h @@ -0,0 +1,134 @@ +/* + * ethernet/arp.c: IP v4 ARP node + * + * Copyright (c) 2010 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef included_ethernet_arp_packet_h +#define included_ethernet_arp_packet_h + +#define foreach_ethernet_arp_hardware_type \ + _ (0, reserved) \ + _ (1, ethernet) \ + _ (2, experimental_ethernet) \ + _ (3, ax_25) \ + _ (4, proteon_pronet_token_ring) \ + _ (5, chaos) \ + _ (6, ieee_802) \ + _ (7, arcnet) \ + _ (8, hyperchannel) \ + _ (9, lanstar) \ + _ (10, autonet) \ + _ (11, localtalk) \ + _ (12, localnet) \ + _ (13, ultra_link) \ + _ (14, smds) \ + _ (15, frame_relay) \ + _ (16, atm) \ + _ (17, hdlc) \ + _ (18, fibre_channel) \ + _ (19, atm19) \ + _ (20, serial_line) \ + _ (21, atm21) \ + _ (22, mil_std_188_220) \ + _ (23, metricom) \ + _ (24, ieee_1394) \ + _ (25, mapos) \ + _ (26, twinaxial) \ + _ (27, eui_64) \ + _ (28, hiparp) \ + _ (29, iso_7816_3) \ + _ (30, arpsec) \ + _ (31, ipsec_tunnel) \ + _ (32, infiniband) \ + _ (33, cai) \ + _ (34, wiegand) \ + _ (35, pure_ip) \ + _ (36, hw_exp1) \ + _ (256, hw_exp2) + +#define foreach_ethernet_arp_opcode \ + _ (reserved) \ + _ (request) \ + _ (reply) \ + _ (reverse_request) \ + _ (reverse_reply) \ + _ (drarp_request) \ + _ (drarp_reply) \ + _ (drarp_error) \ + _ (inarp_request) \ + _ (inarp_reply) \ + _ (arp_nak) \ + _ (mars_request) \ + _ (mars_multi) \ + _ (mars_mserv) \ + _ (mars_join) \ + _ (mars_leave) \ + _ (mars_nak) \ + _ (mars_unserv) \ + _ (mars_sjoin) \ + _ (mars_sleave) \ + _ (mars_grouplist_request) \ + _ (mars_grouplist_reply) \ + _ (mars_redirect_map) \ + _ (mapos_unarp) \ + _ (exp1) \ + _ (exp2) + +typedef enum { +#define _(n,f) ETHERNET_ARP_HARDWARE_TYPE_##f = (n), + foreach_ethernet_arp_hardware_type +#undef _ +} ethernet_arp_hardware_type_t; + +typedef enum { +#define _(f) ETHERNET_ARP_OPCODE_##f, + foreach_ethernet_arp_opcode +#undef _ + ETHERNET_ARP_N_OPCODE, +} ethernet_arp_opcode_t; + +typedef enum { + IP4_ARP_NEXT_DROP, + IP4_ARP_N_NEXT, +} ip4_arp_next_t; + +typedef enum { + IP4_ARP_ERROR_DROP, + IP4_ARP_ERROR_REQUEST_SENT, + IP4_ARP_ERROR_NON_ARP_ADJ, + IP4_ARP_ERROR_REPLICATE_DROP, + IP4_ARP_ERROR_REPLICATE_FAIL +} ip4_arp_error_t; + +typedef CLIB_PACKED (struct { + u8 ethernet[6]; + ip4_address_t ip4; +}) ethernet_arp_ip4_over_ethernet_address_t; + +typedef struct { + u16 l2_type; + u16 l3_type; + u8 n_l2_address_bytes; + u8 n_l3_address_bytes; + u16 opcode; + union { + ethernet_arp_ip4_over_ethernet_address_t ip4_over_ethernet[2]; + + /* Others... */ + u8 data[0]; + }; +} ethernet_arp_header_t; + +#endif /* included_ethernet_arp_packet_h */ diff --git a/vnet/vnet/ethernet/cli.c b/vnet/vnet/ethernet/cli.c new file mode 100644 index 00000000000..cbf240c7f68 --- /dev/null +++ b/vnet/vnet/ethernet/cli.c @@ -0,0 +1,124 @@ +/* + * Copyright (c) 2015 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/* + * cli.c: ethernet CLI + * + * Copyright (c) 2008 Eliot Dresselhaus + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#include <vlib/vlib.h> +#include <vnet/ethernet/ethernet.h> + +VLIB_CLI_COMMAND (vlib_cli_ethernet_command, static) = { + .path = "ethernet", + .short_help = "Ethernet commands", +}; + +static clib_error_t * +promiscuous_cmd (vlib_main_t * vm, + unformat_input_t * input, + vlib_cli_command_t * cmd) +{ + vnet_main_t * vnm = vnet_get_main(); + u32 hw_if_index; + u32 flags = ETHERNET_INTERFACE_FLAG_ACCEPT_ALL; + + if (unformat (input, "on %U", + unformat_ethernet_interface, vnm, &hw_if_index)) + { + ethernet_set_flags (vnm, hw_if_index, flags); + } + else if (unformat (input, "off %U", + unformat_ethernet_interface, vnm, &hw_if_index)) + { + flags = 0; + ethernet_set_flags (vnm, hw_if_index, flags); + } + else + return clib_error_return (0, "unknown input `%U'", + format_unformat_error, input); + return 0; +} + +VLIB_CLI_COMMAND (ethernet_promiscuous_command, static) = { + .path = "ethernet promiscuous", + .short_help = "ethernet promiscuous [on | off] <intfc>", + .function = promiscuous_cmd, +}; + +static clib_error_t * +mtu_cmd (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd) +{ + vnet_main_t * vnm = vnet_get_main(); + u32 hw_if_index, mtu; + u32 flags = ETHERNET_INTERFACE_FLAG_MTU; + + if (unformat (input, "%d %U", &mtu, + unformat_ethernet_interface, vnm, &hw_if_index)) + { + vnet_hw_interface_t * hi = vnet_get_hw_interface (vnm, hw_if_index); + + if (mtu < ETHERNET_MIN_PACKET_BYTES) + return clib_error_return (0, "Invalid mtu (%d): " + "must be >= min pkt bytes (%d)", mtu, + hi->min_packet_bytes); + + if (mtu > ETHERNET_MAX_PACKET_BYTES) + return clib_error_return (0, "Invalid mtu (%d): must be <= 9216", mtu); + + if (hi->max_packet_bytes != mtu) + { + hi->max_packet_bytes = mtu; + ethernet_set_flags (vnm, hw_if_index, flags); + } + } + else + return clib_error_return (0, "unknown input `%U'", + format_unformat_error, input); + return 0; +} + +VLIB_CLI_COMMAND (ethernet_mtu_command, static) = { + .path = "ethernet mtu", + .short_help = "ethernet mtu <64-9216> <intfc>", + .function = mtu_cmd, +}; + +clib_error_t * +ethernet_cli_init (vlib_main_t * vm) +{ + return 0; +} + +VLIB_INIT_FUNCTION (ethernet_cli_init); diff --git a/vnet/vnet/ethernet/error.def b/vnet/vnet/ethernet/error.def new file mode 100644 index 00000000000..36679c0ce1c --- /dev/null +++ b/vnet/vnet/ethernet/error.def @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2015 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/* + * ethernet_error.def: ethernet errors + * + * Copyright (c) 2008 Eliot Dresselhaus + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +ethernet_error (NONE, PUNT, "no error") +ethernet_error (BAD_LLC_LENGTH, DROP, "llc length > packet length") +ethernet_error (UNKNOWN_TYPE, PUNT, "unknown ethernet type") +ethernet_error (UNKNOWN_VLAN, DROP, "unknown vlan") +ethernet_error (L3_MAC_MISMATCH, DROP, "l3 mac mismatch") +ethernet_error (DOWN, DROP, "subinterface down") + diff --git a/vnet/vnet/ethernet/ethernet.h b/vnet/vnet/ethernet/ethernet.h new file mode 100644 index 00000000000..266e1d79afb --- /dev/null +++ b/vnet/vnet/ethernet/ethernet.h @@ -0,0 +1,450 @@ +/* + * Copyright (c) 2015 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/* + * ethernet.h: types/functions for ethernet. + * + * Copyright (c) 2008 Eliot Dresselhaus + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#ifndef included_ethernet_h +#define included_ethernet_h + +#include <vnet/vnet.h> +#include <vnet/ethernet/packet.h> +#include <vnet/pg/pg.h> + +always_inline u64 +ethernet_mac_address_u64 (u8 * a) +{ return (((u64) a[0] << (u64) (5*8)) + | ((u64) a[1] << (u64) (4*8)) + | ((u64) a[2] << (u64) (3*8)) + | ((u64) a[3] << (u64) (2*8)) + | ((u64) a[4] << (u64) (1*8)) + | ((u64) a[5] << (u64) (0*8))); } + +static inline int ethernet_mac_address_is_multicast_u64 (u64 a) +{ + return (a & (1ULL<<(5*8))) != 0; +} + +/* Max. sized ethernet/vlan header for parsing. */ +typedef struct { + ethernet_header_t ethernet; + + /* Allow up to 2 stacked vlan headers. */ + ethernet_vlan_header_t vlan[2]; +} ethernet_max_header_t; + +struct vnet_hw_interface_t; +/* Ethernet flag change callback. */ +typedef u32 (ethernet_flag_change_function_t) +(vnet_main_t * vnm, struct vnet_hw_interface_t * hi, u32 flags); + +#define ETHERNET_MIN_PACKET_BYTES 64 +#define ETHERNET_MAX_PACKET_BYTES 9216 + +/* Ethernet interface instance. */ +typedef struct ethernet_interface { + + /* Accept all packets (promiscuous mode). */ +#define ETHERNET_INTERFACE_FLAG_ACCEPT_ALL (1 << 0) +#define ETHERNET_INTERFACE_FLAG_CONFIG_PROMISC(flags) \ + (((flags) & ~ETHERNET_INTERFACE_FLAG_ACCEPT_ALL) == 0) + + /* Change MTU on interface from hw interface structure */ +#define ETHERNET_INTERFACE_FLAG_MTU (1 << 1) +#define ETHERNET_INTERFACE_FLAG_CONFIG_MTU(flags) \ + ((flags) & ETHERNET_INTERFACE_FLAG_MTU) + + /* Callback, e.g. to turn on/off promiscuous mode */ + ethernet_flag_change_function_t * flag_change; + + u32 driver_instance; + + /* Ethernet (MAC) address for this interface. */ + u8 address[6]; +} ethernet_interface_t; + +vnet_hw_interface_class_t ethernet_hw_interface_class; + +typedef struct { + /* Name (a c string). */ + char * name; + + /* Ethernet type in host byte order. */ + ethernet_type_t type; + + /* Node which handles this type. */ + u32 node_index; + + /* Next index for this type. */ + u32 next_index; +} ethernet_type_info_t; + +typedef enum { +#define ethernet_error(n,c,s) ETHERNET_ERROR_##n, +#include <vnet/ethernet/error.def> +#undef ethernet_error + ETHERNET_N_ERROR, +} ethernet_error_t; + + +// Structs used when parsing packet to find sw_if_index + +typedef struct { + u32 sw_if_index; + u32 flags; + // config entry is-valid flag + // exact match flags (valid if packet has 0/1/2/3 tags) + // L2 vs L3 forwarding mode +#define SUBINT_CONFIG_MATCH_0_TAG (1<<0) +#define SUBINT_CONFIG_MATCH_1_TAG (1<<1) +#define SUBINT_CONFIG_MATCH_2_TAG (1<<2) +#define SUBINT_CONFIG_MATCH_3_TAG (1<<3) +#define SUBINT_CONFIG_VALID (1<<4) +#define SUBINT_CONFIG_L2 (1<<5) + +} subint_config_t; + +always_inline u32 +eth_create_valid_subint_match_flags (u32 num_tags) { + return SUBINT_CONFIG_VALID | (1 << num_tags); +} + + +typedef struct { + subint_config_t untagged_subint; + subint_config_t default_subint; + u16 dot1q_vlans; // pool id for vlan table + u16 dot1ad_vlans; // pool id for vlan table +} main_intf_t; + +typedef struct { + subint_config_t single_tag_subint; + subint_config_t inner_any_subint; + u32 qinqs; // pool id for qinq table +} vlan_intf_t; + +typedef struct { + vlan_intf_t vlans[ETHERNET_N_VLAN]; +} vlan_table_t; + +typedef struct { + subint_config_t subint; +} qinq_intf_t; + +typedef struct { + qinq_intf_t vlans[ETHERNET_N_VLAN]; +} qinq_table_t; + +// Structure mapping to a next index based on ethertype. +// Common ethertypes are stored explicitly, others are +// stored in a sparse table. +typedef struct { + /* Sparse vector mapping ethernet type in network byte order + to next index. */ + u16 * input_next_by_type; + u32 * sparse_index_by_input_next_index; + + /* cached next indexes for common ethertypes */ + u32 input_next_ip4; + u32 input_next_ip6; + u32 input_next_mpls; +} next_by_ethertype_t; + + +typedef struct { + vlib_main_t * vlib_main; + + /* next node index for the L3 input node of each ethertype */ + next_by_ethertype_t l3_next; + + /* next node index for L2 interfaces */ + u32 l2_next; + + /* flag and next node index for L3 redirect */ + u32 redirect_l3; + u32 redirect_l3_next; + + /* Pool of ethernet interface instances. */ + ethernet_interface_t * interfaces; + + ethernet_type_info_t * type_infos; + + /* Hash tables mapping name/type to type info index. */ + uword * type_info_by_name, * type_info_by_type; + + // The root of the vlan parsing tables. A vector with one element + // for each main interface, indexed by hw_if_index. + main_intf_t * main_intfs; + + // Pool of vlan tables + vlan_table_t * vlan_pool; + + // Pool of qinq tables; + qinq_table_t * qinq_pool; + + /* Set to one to use AB.CD.EF instead of A:B:C:D:E:F as ethernet format. */ + int format_ethernet_address_16bit; + +} ethernet_main_t; + +ethernet_main_t ethernet_main; + +always_inline ethernet_type_info_t * +ethernet_get_type_info (ethernet_main_t * em, ethernet_type_t type) +{ + uword * p = hash_get (em->type_info_by_type, type); + return p ? vec_elt_at_index (em->type_infos, p[0]) : 0; +} + +ethernet_interface_t * +ethernet_get_interface (ethernet_main_t * em, u32 hw_if_index); + +clib_error_t * +ethernet_register_interface (vnet_main_t * vnm, + u32 dev_class_index, + u32 dev_instance, + u8 * address, + u32 * hw_if_index_return, + ethernet_flag_change_function_t flag_change); + +void ethernet_delete_interface (vnet_main_t * vnm, u32 hw_if_index); + +/* Register given node index to take input for given ethernet type. */ +void +ethernet_register_input_type (vlib_main_t * vm, + ethernet_type_t type, + u32 node_index); + +/* Register given node index to take input for packet from L2 interfaces. */ +void +ethernet_register_l2_input (vlib_main_t * vm, + u32 node_index); + +/* Register given node index to take redirected L3 traffic, and enable L3 redirect */ +void +ethernet_register_l3_redirect (vlib_main_t * vm, + u32 node_index); + +/* Formats ethernet address X:X:X:X:X:X */ +u8 * format_ethernet_address (u8 * s, va_list * args); +u8 * format_ethernet_type (u8 * s, va_list * args); +u8 * format_ethernet_header (u8 * s, va_list * args); +u8 * format_ethernet_header_with_length (u8 * s, va_list * args); + +/* Parse ethernet address in either X:X:X:X:X:X unix or X.X.X cisco format. */ +uword +unformat_ethernet_address (unformat_input_t * input, va_list * args); + +/* Parse ethernet type as 0xXXXX or type name from ethernet/types.def. + In either host or network byte order. */ +uword +unformat_ethernet_type_host_byte_order (unformat_input_t * input, + va_list * args); +uword +unformat_ethernet_type_net_byte_order (unformat_input_t * input, + va_list * args); + +/* Parse ethernet header. */ +uword +unformat_ethernet_header (unformat_input_t * input, va_list * args); + +/* Parse ethernet interface name; return hw_if_index. */ +uword unformat_ethernet_interface (unformat_input_t * input, va_list * args); + +uword unformat_pg_ethernet_header (unformat_input_t * input, va_list * args); + +always_inline void +ethernet_setup_node (vlib_main_t * vm, u32 node_index) +{ + vlib_node_t * n = vlib_get_node (vm, node_index); + pg_node_t * pn = pg_get_node (node_index); + + n->format_buffer = format_ethernet_header_with_length; + n->unformat_buffer = unformat_ethernet_header; + pn->unformat_edit = unformat_pg_ethernet_header; +} + +always_inline ethernet_header_t * +ethernet_buffer_get_header (vlib_buffer_t * b) +{ + return (void *) + (b->data + + vnet_buffer (b)->ethernet.start_of_ethernet_header); +} + +ethernet_main_t * ethernet_get_main (vlib_main_t * vm); +u32 ethernet_set_flags (vnet_main_t * vnm, u32 hw_if_index, u32 flags); +void ethernet_sw_interface_set_l2_mode (vnet_main_t * vnm, u32 sw_if_index, u32 l2); +void ethernet_set_rx_redirect (vnet_main_t * vnm, vnet_hw_interface_t * hi, u32 enable); + +int +vnet_arp_set_ip4_over_ethernet (vnet_main_t * vnm, + u32 sw_if_index, + u32 fib_index, + void *a_arg, + int is_static); + +int +vnet_arp_unset_ip4_over_ethernet (vnet_main_t * vnm, + u32 sw_if_index, u32 fib_index, + void * a_arg); + +int vnet_proxy_arp_fib_reset (u32 fib_id); + +clib_error_t * next_by_ethertype_init (next_by_ethertype_t * l3_next); +clib_error_t * next_by_ethertype_register (next_by_ethertype_t * l3_next, + u32 ethertype, + u32 next_index); + +int vnet_create_loopback_interface (u32 * sw_if_indexp, u8 *mac_address); +int vnet_delete_loopback_interface (u32 sw_if_index); + +// Perform ethernet subinterface classification table lookups given +// the ports's sw_if_index and fields extracted from the ethernet header. +// The resulting tables are used by identify_subint(). +always_inline void +eth_vlan_table_lookups (ethernet_main_t *em, + vnet_main_t * vnm, + u32 port_sw_if_index0, + u16 first_ethertype, + u16 outer_id, + u16 inner_id, + vnet_hw_interface_t ** hi, + main_intf_t **main_intf, + vlan_intf_t **vlan_intf, + qinq_intf_t **qinq_intf) +{ + vlan_table_t *vlan_table; + qinq_table_t *qinq_table; + u32 vlan_table_id; + + // Read the main, vlan, and qinq interface table entries + // TODO: Consider if/how to prefetch tables. Also consider + // single-entry cache to skip table lookups and identify_subint() + // processing. + *hi = vnet_get_sup_hw_interface (vnm, port_sw_if_index0); + *main_intf = vec_elt_at_index (em->main_intfs, (*hi)->hw_if_index); + + // Always read the vlan and qinq tables, even if there are not that + // many tags on the packet. This makes the lookups and comparisons + // easier (and less branchy). + vlan_table_id = (first_ethertype == ETHERNET_TYPE_DOT1AD) ? + (*main_intf)->dot1ad_vlans : + (*main_intf)->dot1q_vlans; + vlan_table = vec_elt_at_index (em->vlan_pool, vlan_table_id); + *vlan_intf = &vlan_table->vlans[outer_id]; + + qinq_table = vec_elt_at_index (em->qinq_pool, (*vlan_intf)->qinqs); + *qinq_intf = &qinq_table->vlans[inner_id]; +} + + +// Determine the subinterface for this packet, given the result of the +// vlan table lookups and vlan header parsing. Check the most specific +// matches first. +// Returns 1 if a matching subinterface was found, otherwise returns 0. +always_inline u32 +eth_identify_subint (vnet_hw_interface_t * hi, + vlib_buffer_t * b0, + u32 match_flags, + main_intf_t * main_intf, + vlan_intf_t * vlan_intf, + qinq_intf_t * qinq_intf, + u32 * new_sw_if_index, + u8 * error0, + u32 * is_l2) +{ + subint_config_t * subint; + + // Each comparison is checking both the valid flag and the number of tags + // (incorporating exact-match/non-exact-match). + + // check for specific double tag + subint = &qinq_intf->subint; + if ((subint->flags & match_flags) == match_flags) goto matched; + + // check for specific outer and 'any' inner + subint = &vlan_intf->inner_any_subint; + if ((subint->flags & match_flags) == match_flags) goto matched; + + // check for specific single tag + subint = &vlan_intf->single_tag_subint; + if ((subint->flags & match_flags) == match_flags) goto matched; + + // check for untagged interface + subint = &main_intf->untagged_subint; + if ((subint->flags & match_flags) == match_flags) goto matched; + + // check for default interface + subint = &main_intf->default_subint; + if ((subint->flags & match_flags) == match_flags) goto matched; + + // No matching subinterface + *new_sw_if_index = ~0; + *error0 = ETHERNET_ERROR_UNKNOWN_VLAN; + *is_l2 = 0; + return 0; + + matched: + *new_sw_if_index = subint->sw_if_index; + *is_l2 = subint->flags & SUBINT_CONFIG_L2; + return 1; +} + +always_inline ethernet_main_t * +vnet_get_ethernet_main (void) +{ + return ðernet_main; +} + +void vnet_register_ip4_arp_resolution_event (vnet_main_t * vnm, + void * address_arg, + uword node_index, + uword type_opaque, + uword data); + + +int vnet_add_del_ip4_arp_change_event (vnet_main_t * vnm, + void * data_callback, + u32 pid, + void * address_arg, + uword node_index, + uword type_opaque, + uword data, int is_add); + +vlib_node_registration_t ethernet_input_node; + +#endif /* included_ethernet_h */ diff --git a/vnet/vnet/ethernet/format.c b/vnet/vnet/ethernet/format.c new file mode 100644 index 00000000000..be456d56d71 --- /dev/null +++ b/vnet/vnet/ethernet/format.c @@ -0,0 +1,301 @@ +/* + * Copyright (c) 2015 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/* + * ethernet_format.c: ethernet formatting/parsing. + * + * Copyright (c) 2008 Eliot Dresselhaus + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#include <vlib/vlib.h> +#include <vnet/ethernet/ethernet.h> + +u8 * format_ethernet_address (u8 * s, va_list * args) +{ + ethernet_main_t * em = ðernet_main; + u8 * a = va_arg (*args, u8 *); + + if (em->format_ethernet_address_16bit) + return format (s, "%02x%02x.%02x%02x.%02x%02x", + a[0], a[1], a[2], a[3], a[4], a[5]); + else + return format (s, "%02x:%02x:%02x:%02x:%02x:%02x", + a[0], a[1], a[2], a[3], a[4], a[5]); +} + +u8 * format_ethernet_type (u8 * s, va_list * args) +{ + ethernet_type_t type = va_arg (*args, u32); + ethernet_main_t * em = ðernet_main; + ethernet_type_info_t * t = ethernet_get_type_info (em, type); + + if (t) + s = format (s, "%s", t->name); + else + s = format (s, "0x%04x", type); + + return s; +} + +u8 * format_ethernet_header_with_length (u8 * s, va_list * args) +{ + ethernet_max_header_t * m = va_arg (*args, ethernet_max_header_t *); + u32 max_header_bytes = va_arg (*args, u32); + ethernet_main_t * em = ðernet_main; + ethernet_header_t * e = &m->ethernet; + ethernet_vlan_header_t * v; + ethernet_type_t type = clib_net_to_host_u16 (e->type); + u32 n_vlan = 0, i, header_bytes; + uword indent; + + while (type == ETHERNET_TYPE_VLAN + && n_vlan < ARRAY_LEN (m->vlan)) + { + v = m->vlan + n_vlan; + type = clib_net_to_host_u16 (v->type); + n_vlan++; + } + + header_bytes = sizeof (e[0]) + n_vlan * sizeof (v[0]); + if (max_header_bytes != 0 && header_bytes > max_header_bytes) + return format (s, "ethernet header truncated"); + + indent = format_get_indent (s); + + s = format (s, "%U: %U -> %U", + format_ethernet_type, type, + format_ethernet_address, e->src_address, + format_ethernet_address, e->dst_address); + + for (i = 0; i < n_vlan; i++) + { + u32 v = clib_net_to_host_u16 (m->vlan[i].priority_cfi_and_id); + u32 vid = (v & 0xfff); + u32 cfi = (v >> 12) & 1; + u32 pri = (v >> 13); + + s = format (s, " vlan %d", vid); + if (pri != 0) + s = format (s, " priority %d", pri); + if (cfi != 0) + s = format (s, " cfi"); + } + + if (max_header_bytes != 0 && header_bytes < max_header_bytes) + { + ethernet_type_info_t * ti; + vlib_node_t * node = 0; + + ti = ethernet_get_type_info (em, type); + if (ti && ti->node_index != ~0) + node = vlib_get_node (em->vlib_main, ti->node_index); + if (node && node->format_buffer) + s = format (s, "\n%U%U", + format_white_space, indent, + node->format_buffer, (void *) m + header_bytes, + max_header_bytes - header_bytes); + } + + return s; +} + +u8 * format_ethernet_header (u8 * s, va_list * args) +{ + ethernet_max_header_t * m = va_arg (*args, ethernet_max_header_t *); + return format (s, "%U", format_ethernet_header_with_length, m, 0); +} + +/* Parse X:X:X:X:X:X unix style ethernet address. */ +static uword +unformat_ethernet_address_unix (unformat_input_t * input, va_list * args) +{ + u8 * result = va_arg (*args, u8 *); + u32 i, a[6]; + + if (! unformat (input, "%_%x:%x:%x:%x:%x:%x%_", + &a[0], &a[1], &a[2], &a[3], &a[4], &a[5])) + return 0; + + /* Check range. */ + for (i = 0; i < ARRAY_LEN (a); i++) + if (a[i] >= (1 << 8)) + return 0; + + for (i = 0; i < ARRAY_LEN (a); i++) + result[i] = a[i]; + + return 1; +} + +/* Parse X.X.X cisco style ethernet address. */ +static uword +unformat_ethernet_address_cisco (unformat_input_t * input, va_list * args) +{ + u8 * result = va_arg (*args, u8 *); + u32 i, a[3]; + + if (! unformat (input, "%_%x.%x.%x%_", &a[0], &a[1], &a[2])) + return 0; + + /* Check range. */ + for (i = 0; i < ARRAY_LEN (a); i++) + if (a[i] >= (1 << 16)) + return 0; + + result[0] = (a[0] >> 8) & 0xff; + result[1] = (a[0] >> 0) & 0xff; + result[2] = (a[1] >> 8) & 0xff; + result[3] = (a[1] >> 0) & 0xff; + result[4] = (a[2] >> 8) & 0xff; + result[5] = (a[2] >> 0) & 0xff; + + return 1; +} + +/* Parse ethernet address; accept either unix or style addresses. */ +uword +unformat_ethernet_address (unformat_input_t * input, va_list * args) +{ + u8 * result = va_arg (*args, u8 *); + return (unformat_user (input, unformat_ethernet_address_unix, result) + || unformat_user (input, unformat_ethernet_address_cisco, result)); +} + +/* Returns ethernet type as an int in host byte order. */ +uword +unformat_ethernet_type_host_byte_order (unformat_input_t * input, + va_list * args) +{ + u16 * result = va_arg (*args, u16 *); + ethernet_main_t * em = ðernet_main; + int type, i; + + /* Numeric type. */ + if (unformat (input, "0x%x", &type) + || unformat (input, "%d", &type)) + { + if (type >= (1 << 16)) + return 0; + *result = type; + return 1; + } + + /* Named type. */ + if (unformat_user (input, unformat_vlib_number_by_name, + em->type_info_by_name, &i)) + { + ethernet_type_info_t * ti = vec_elt_at_index (em->type_infos, i); + *result = ti->type; + return 1; + } + + return 0; +} + +uword +unformat_ethernet_type_net_byte_order (unformat_input_t * input, + va_list * args) +{ + u16 * result = va_arg (*args, u16 *); + if (! unformat_user (input, unformat_ethernet_type_host_byte_order, result)) + return 0; + + *result = clib_host_to_net_u16 ((u16) *result); + return 1; +} + +uword +unformat_ethernet_header (unformat_input_t * input, va_list * args) +{ + u8 ** result = va_arg (*args, u8 **); + ethernet_max_header_t _m, * m = &_m; + ethernet_header_t * e = &m->ethernet; + u16 type; + u32 n_vlan; + + if (! unformat (input, "%U: %U -> %U", + unformat_ethernet_type_host_byte_order, &type, + unformat_ethernet_address, &e->src_address, + unformat_ethernet_address, &e->dst_address)) + return 0; + + n_vlan = 0; + while (unformat (input, "vlan")) + { + u32 id, priority; + + if (! unformat_user (input, unformat_vlib_number, &id) + || id >= ETHERNET_N_VLAN) + return 0; + + if (unformat (input, "priority %d", &priority)) + { + if (priority >= 8) + return 0; + id |= priority << 13; + } + + if (unformat (input, "cfi")) + id |= 1 << 12; + + /* Too many vlans given. */ + if (n_vlan >= ARRAY_LEN (m->vlan)) + return 0; + + m->vlan[n_vlan].priority_cfi_and_id = clib_host_to_net_u16 (id); + n_vlan++; + } + + if (n_vlan == 0) + e->type = clib_host_to_net_u16 (type); + else + { + int i; + + e->type = clib_host_to_net_u16 (ETHERNET_TYPE_VLAN); + for (i = 0; i < n_vlan - 1; i++) + m->vlan[i].type = clib_host_to_net_u16 (ETHERNET_TYPE_VLAN); + m->vlan[n_vlan - 1].type = clib_host_to_net_u16 (type); + } + + /* Add header to result. */ + { + void * p; + u32 n_bytes = sizeof (e[0]) + n_vlan * sizeof (m->vlan[0]); + + vec_add2 (*result, p, n_bytes); + memcpy (p, m, n_bytes); + } + + return 1; +} diff --git a/vnet/vnet/ethernet/init.c b/vnet/vnet/ethernet/init.c new file mode 100644 index 00000000000..4ac14e208be --- /dev/null +++ b/vnet/vnet/ethernet/init.c @@ -0,0 +1,95 @@ +/* + * Copyright (c) 2015 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/* + * ethernet_init.c: ethernet initialization + * + * Copyright (c) 2008 Eliot Dresselhaus + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#include <vlib/vlib.h> +#include <vnet/ethernet/ethernet.h> + +/* Global main structure. */ +ethernet_main_t ethernet_main; + +static void add_type (ethernet_main_t * em, + ethernet_type_t type, + char * type_name) +{ + ethernet_type_info_t * ti; + u32 i; + + vec_add2 (em->type_infos, ti, 1); + i = ti - em->type_infos; + + ti->name = type_name; + ti->type = type; + ti->next_index = ti->node_index = ~0; + + hash_set (em->type_info_by_type, type, i); + hash_set_mem (em->type_info_by_name, ti->name, i); +} + +static clib_error_t * ethernet_init (vlib_main_t * vm) +{ + ethernet_main_t * em = ðernet_main; + clib_error_t * error; + + em->vlib_main = vm; + + em->type_info_by_name = hash_create_string (0, sizeof (uword)); + em->type_info_by_type = hash_create (0, sizeof (uword)); + +#define ethernet_type(n,s) add_type (em, ETHERNET_TYPE_##s, #s); +#include "types.def" +#undef ethernet_type + + if ((error = vlib_call_init_function (vm, llc_init))) + return error; + if ((error = vlib_call_init_function (vm, ethernet_input_init))) + return error; + if ((error = vlib_call_init_function (vm, ethernet_cli_init))) + return error; + + return error; +} + +VLIB_INIT_FUNCTION (ethernet_init); + +ethernet_main_t * ethernet_get_main (vlib_main_t * vm) +{ + vlib_call_init_function (vm, ethernet_init); + return ðernet_main; +} + diff --git a/vnet/vnet/ethernet/interface.c b/vnet/vnet/ethernet/interface.c new file mode 100644 index 00000000000..fe7eb76bf46 --- /dev/null +++ b/vnet/vnet/ethernet/interface.c @@ -0,0 +1,462 @@ +/* + * Copyright (c) 2015 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/* + * ethernet_interface.c: ethernet interfaces + * + * Copyright (c) 2008 Eliot Dresselhaus + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#include <vnet/vnet.h> +#include <vnet/ip/ip.h> +#include <vnet/pg/pg.h> +#include <vnet/ethernet/ethernet.h> + +static uword ethernet_set_rewrite (vnet_main_t * vnm, + u32 sw_if_index, + u32 l3_type, + void * dst_address, + void * rewrite, + uword max_rewrite_bytes) +{ + vnet_sw_interface_t * sub_sw = vnet_get_sw_interface (vnm, sw_if_index); + vnet_sw_interface_t * sup_sw = vnet_get_sup_sw_interface (vnm, sw_if_index); + vnet_hw_interface_t * hw = vnet_get_sup_hw_interface (vnm, sw_if_index); + ethernet_main_t * em = ðernet_main; + ethernet_interface_t * ei; + ethernet_header_t * h = rewrite; + ethernet_type_t type; + uword n_bytes = sizeof (h[0]); + + if (sub_sw != sup_sw) { + if (sub_sw->sub.eth.flags.one_tag) { + n_bytes += sizeof (ethernet_vlan_header_t); + } else if (sub_sw->sub.eth.flags.two_tags) { + n_bytes += 2 * (sizeof (ethernet_vlan_header_t)); + } + // Check for encaps that are not supported for L3 interfaces + if (!(sub_sw->sub.eth.flags.exact_match) || + (sub_sw->sub.eth.flags.default_sub) || + (sub_sw->sub.eth.flags.outer_vlan_id_any) || + (sub_sw->sub.eth.flags.inner_vlan_id_any)) { + return 0; + } + } + + if (n_bytes > max_rewrite_bytes) + return 0; + + switch (l3_type) { +#define _(a,b) case VNET_L3_PACKET_TYPE_##a: type = ETHERNET_TYPE_##b; break + _ (IP4, IP4); + _ (IP6, IP6); + _ (MPLS_UNICAST, MPLS_UNICAST); + _ (MPLS_MULTICAST, MPLS_MULTICAST); + _ (ARP, ARP); +#undef _ + default: + return 0; + } + + ei = pool_elt_at_index (em->interfaces, hw->hw_instance); + memcpy (h->src_address, ei->address, sizeof (h->src_address)); + if (dst_address) + memcpy (h->dst_address, dst_address, sizeof (h->dst_address)); + else + memset (h->dst_address, ~0, sizeof (h->dst_address)); /* broadcast */ + + if (sub_sw->sub.eth.flags.one_tag) { + ethernet_vlan_header_t * outer = (void *) (h + 1); + + h->type = sub_sw->sub.eth.flags.dot1ad ? + clib_host_to_net_u16 (ETHERNET_TYPE_DOT1AD) : + clib_host_to_net_u16 (ETHERNET_TYPE_VLAN); + outer->priority_cfi_and_id = clib_host_to_net_u16 (sub_sw->sub.eth.outer_vlan_id); + outer->type = clib_host_to_net_u16 (type); + + } else if (sub_sw->sub.eth.flags.two_tags) { + ethernet_vlan_header_t * outer = (void *) (h + 1); + ethernet_vlan_header_t * inner = (void *) (outer + 1); + + h->type = sub_sw->sub.eth.flags.dot1ad ? + clib_host_to_net_u16 (ETHERNET_TYPE_DOT1AD) : + clib_host_to_net_u16 (ETHERNET_TYPE_VLAN); + outer->priority_cfi_and_id = clib_host_to_net_u16 (sub_sw->sub.eth.outer_vlan_id); + outer->type = clib_host_to_net_u16 (ETHERNET_TYPE_VLAN); + inner->priority_cfi_and_id = clib_host_to_net_u16 (sub_sw->sub.eth.inner_vlan_id); + inner->type = clib_host_to_net_u16 (type); + + } else { + h->type = clib_host_to_net_u16 (type); + } + + return n_bytes; +} + +VNET_HW_INTERFACE_CLASS (ethernet_hw_interface_class) = { + .name = "Ethernet", + .format_address = format_ethernet_address, + .format_header = format_ethernet_header_with_length, + .unformat_hw_address = unformat_ethernet_address, + .unformat_header = unformat_ethernet_header, + .set_rewrite = ethernet_set_rewrite, +}; + +uword unformat_ethernet_interface (unformat_input_t * input, va_list * args) +{ + vnet_main_t * vnm = va_arg (*args, vnet_main_t *); + u32 * result = va_arg (*args, u32 *); + u32 hw_if_index; + ethernet_main_t * em = ðernet_main; + ethernet_interface_t * eif; + + if (! unformat_user (input, unformat_vnet_hw_interface, vnm, &hw_if_index)) + return 0; + + eif = ethernet_get_interface (em, hw_if_index); + if (eif) + { + *result = hw_if_index; + return 1; + } + return 0; +} + +clib_error_t * +ethernet_register_interface (vnet_main_t * vnm, + u32 dev_class_index, + u32 dev_instance, + u8 * address, + u32 * hw_if_index_return, + ethernet_flag_change_function_t flag_change) +{ + ethernet_main_t * em = ðernet_main; + ethernet_interface_t * ei; + vnet_hw_interface_t * hi; + clib_error_t * error = 0; + u32 hw_if_index; + + pool_get (em->interfaces, ei); + ei->flag_change = flag_change; + + hw_if_index = vnet_register_interface + (vnm, + dev_class_index, dev_instance, + ethernet_hw_interface_class.index, + ei - em->interfaces); + *hw_if_index_return = hw_if_index; + + hi = vnet_get_hw_interface (vnm, hw_if_index); + + ethernet_setup_node (vnm->vlib_main, hi->output_node_index); + + hi->min_packet_bytes = ETHERNET_MIN_PACKET_BYTES; + hi->max_packet_bytes = ETHERNET_MAX_PACKET_BYTES; + hi->per_packet_overhead_bytes = + /* preamble */ 8 + /* inter frame gap */ 12; + + /* Standard default ethernet MTU. */ + hi->max_l3_packet_bytes[VLIB_RX] = hi->max_l3_packet_bytes[VLIB_TX] = 1500; + + memcpy (ei->address, address, sizeof (ei->address)); + vec_free (hi->hw_address); + vec_add (hi->hw_address, address, sizeof (ei->address)); + + if (error) + { + pool_put (em->interfaces, ei); + return error; + } + return error; +} + +void +ethernet_delete_interface (vnet_main_t * vnm, u32 hw_if_index) +{ + ethernet_main_t * em = ðernet_main; + ethernet_interface_t * ei; + vnet_hw_interface_t * hi; + main_intf_t * main_intf; + vlan_table_t * vlan_table; + u32 idx; + + hi = vnet_get_hw_interface (vnm, hw_if_index); + ei = pool_elt_at_index (em->interfaces, hi->hw_instance); + + /* Delete vlan mapping table for dot1q and dot1ad. */ + main_intf = vec_elt_at_index (em->main_intfs, hi->hw_if_index); + if (main_intf->dot1q_vlans) { + vlan_table = vec_elt_at_index (em->vlan_pool, main_intf->dot1q_vlans); + for (idx=0; idx<ETHERNET_N_VLAN; idx++ ) { + if (vlan_table->vlans[idx].qinqs) { + pool_put_index(em->qinq_pool, vlan_table->vlans[idx].qinqs); + } + } + pool_put_index(em->vlan_pool, main_intf->dot1q_vlans); + } + if (main_intf->dot1ad_vlans) { + vlan_table = vec_elt_at_index (em->vlan_pool, main_intf->dot1ad_vlans); + for (idx=0; idx<ETHERNET_N_VLAN; idx++ ) { + if (vlan_table->vlans[idx].qinqs) { + pool_put_index(em->qinq_pool, vlan_table->vlans[idx].qinqs); + } + } + pool_put_index(em->vlan_pool, main_intf->dot1ad_vlans); + } + + vnet_delete_hw_interface (vnm, hw_if_index); + pool_put (em->interfaces, ei); +} + +u32 +ethernet_set_flags (vnet_main_t * vnm, u32 hw_if_index, u32 flags) +{ + ethernet_main_t * em = ðernet_main; + vnet_hw_interface_t * hi; + ethernet_interface_t * ei; + + hi = vnet_get_hw_interface (vnm, hw_if_index); + + ASSERT (hi->hw_class_index == ethernet_hw_interface_class.index); + + ei = pool_elt_at_index (em->interfaces, hi->hw_instance); + if (ei->flag_change) + return ei->flag_change (vnm, hi, flags); + return (u32)~0; +} + +#define VNET_SIMULATED_ETHERNET_TX_NEXT_ETHERNET_INPUT VNET_INTERFACE_TX_N_NEXT + +/* Echo packets back to ethernet input. */ +static uword +simulated_ethernet_interface_tx (vlib_main_t * vm, + vlib_node_runtime_t * node, + vlib_frame_t * frame) +{ + u32 n_left_from, n_left_to_next, n_copy, * from, * to_next; + u32 next_index = VNET_SIMULATED_ETHERNET_TX_NEXT_ETHERNET_INPUT; + u32 i; + vlib_buffer_t * b; + + n_left_from = frame->n_vectors; + from = vlib_frame_args (frame); + + while (n_left_from > 0) + { + vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); + + n_copy = clib_min (n_left_from, n_left_to_next); + + memcpy (to_next, from, n_copy * sizeof (from[0])); + n_left_to_next -= n_copy; + n_left_from -= n_copy; + for (i = 0; i < n_copy; i++) + { + b = vlib_get_buffer (vm, from[i]); + /* Set up RX and TX indices as if received from a real driver */ + vnet_buffer (b)->sw_if_index[VLIB_RX] = + vnet_buffer (b)->sw_if_index[VLIB_TX]; + vnet_buffer (b)->sw_if_index[VLIB_TX] = (u32) ~0; + } + + vlib_put_next_frame (vm, node, next_index, n_left_to_next); + } + + return n_left_from; +} + +static u8 * format_simulated_ethernet_name (u8 * s, va_list * args) +{ + u32 dev_instance = va_arg (*args, u32); + return format (s, "loop%d", dev_instance); +} + +VNET_DEVICE_CLASS (ethernet_simulated_device_class) = { + .name = "Loopback", + .format_device_name = format_simulated_ethernet_name, + .tx_function = simulated_ethernet_interface_tx, +}; + +int vnet_create_loopback_interface (u32 * sw_if_indexp, u8 *mac_address) +{ + vnet_main_t * vnm = vnet_get_main(); + vlib_main_t * vm = vlib_get_main(); + clib_error_t * error; + static u32 instance; + u8 address[6]; + u32 hw_if_index; + vnet_hw_interface_t * hw_if; + u32 slot; + int rv = 0; + + ASSERT(sw_if_indexp); + + *sw_if_indexp = (u32)~0; + + memset (address, 0, sizeof (address)); + + /* + * Default MAC address (dead:0000:0000 + instance) is allocated + * if zero mac_address is configured. Otherwise, user-configurable MAC + * address is programmed on the loopback interface. + */ + if (memcmp (address, mac_address, sizeof (address))) + memcpy (address, mac_address, sizeof (address)); + else + { + address[0] = 0xde; + address[1] = 0xad; + address[5] = instance; + } + + error = ethernet_register_interface + (vnm, + ethernet_simulated_device_class.index, + instance++, + address, + &hw_if_index, + /* flag change */ 0); + + if (error) + { + rv = VNET_API_ERROR_INVALID_REGISTRATION; + clib_error_report(error); + return rv; + } + + hw_if = vnet_get_hw_interface (vnm, hw_if_index); + slot = vlib_node_add_named_next_with_slot + (vm, hw_if->tx_node_index, + "ethernet-input", + VNET_SIMULATED_ETHERNET_TX_NEXT_ETHERNET_INPUT); + ASSERT (slot == VNET_SIMULATED_ETHERNET_TX_NEXT_ETHERNET_INPUT); + + { + vnet_sw_interface_t * si = vnet_get_hw_sw_interface (vnm, hw_if_index); + *sw_if_indexp = si->sw_if_index; + } + + return 0; +} + +static clib_error_t * +create_simulated_ethernet_interfaces (vlib_main_t * vm, + unformat_input_t * input, + vlib_cli_command_t * cmd) +{ + int rv; + u32 sw_if_index; + u8 mac_address[6]; + + memset (mac_address, 0, sizeof (mac_address)); + + while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) + { + if (unformat (input, "mac %U", unformat_ethernet_address, mac_address)) + ; + else + break; + } + + rv = vnet_create_loopback_interface (&sw_if_index, mac_address); + + if (rv) + return clib_error_return (0, "vnet_create_loopback_interface failed"); + + return 0; +} + +VLIB_CLI_COMMAND (create_simulated_ethernet_interface_command, static) = { + .path = "loopback create-interface", + .short_help = "Create Loopback ethernet interface [mac <mac-addr>]", + .function = create_simulated_ethernet_interfaces, +}; + +ethernet_interface_t * +ethernet_get_interface (ethernet_main_t * em, u32 hw_if_index) +{ + vnet_hw_interface_t * i = vnet_get_hw_interface (vnet_get_main(), hw_if_index); + return (i->hw_class_index == ethernet_hw_interface_class.index + ? pool_elt_at_index (em->interfaces, i->hw_instance) + : 0); +} + +int vnet_delete_loopback_interface (u32 sw_if_index) +{ + vnet_main_t * vnm = vnet_get_main(); + vnet_sw_interface_t * si; + + if (pool_is_free_index (vnm->interface_main.sw_interfaces, + sw_if_index)) + return VNET_API_ERROR_INVALID_SW_IF_INDEX; + + si = vnet_get_sw_interface (vnm, sw_if_index); + ethernet_delete_interface (vnm, si->hw_if_index); + + return 0; +} + +static clib_error_t * +delete_simulated_ethernet_interfaces (vlib_main_t * vm, + unformat_input_t * input, + vlib_cli_command_t * cmd) +{ + int rv; + u32 sw_if_index = ~0; + vnet_main_t * vnm = vnet_get_main(); + + while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) + { + if (unformat (input, "intfc %U", + unformat_vnet_sw_interface, vnm, &sw_if_index)) + ; + else + break; + } + + if (sw_if_index == ~0) + return clib_error_return (0, "interface not specified"); + + rv = vnet_delete_loopback_interface (sw_if_index); + + if (rv) + return clib_error_return (0, "vnet_delete_loopback_interface failed"); + + return 0; +} + +VLIB_CLI_COMMAND (delete_simulated_ethernet_interface_command, static) = { + .path = "loopback delete-interface", + .short_help = "Delete Loopback ethernet interface intfc <interface>", + .function = delete_simulated_ethernet_interfaces, +}; diff --git a/vnet/vnet/ethernet/mac_swap.c b/vnet/vnet/ethernet/mac_swap.c new file mode 100644 index 00000000000..6bb7f5b0b02 --- /dev/null +++ b/vnet/vnet/ethernet/mac_swap.c @@ -0,0 +1,380 @@ +/* + * Copyright (c) 2015 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include <vlib/vlib.h> +#include <vnet/pg/pg.h> +#include <vnet/ethernet/ethernet.h> +#include <vppinfra/error.h> +#include <vnet/devices/pci/ige.h> +#include <vnet/devices/pci/ixge.h> +#include <vnet/devices/pci/ixgev.h> + +typedef struct { + u32 cached_next_index; + u32 cached_sw_if_index; + + /* Hash table to map sw_if_index to next node index */ + uword * next_node_index_by_sw_if_index; + + /* convenience */ + vlib_main_t * vlib_main; + vnet_main_t * vnet_main; +} mac_swap_main_t; + +typedef struct { + u8 src[6]; + u8 dst[6]; + u32 sw_if_index; + u32 next_index; +} swap_trace_t; + +/* packet trace format function */ +static u8 * format_swap_trace (u8 * s, va_list * args) +{ + CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); + CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); + swap_trace_t * t = va_arg (*args, swap_trace_t *); + + s = format (s, "SWAP: dst now %U src now %U sw_if_index %d next_index %d", + format_ethernet_address, t->dst, + format_ethernet_address, t->src, + t->sw_if_index, + t->next_index); + return s; +} + +#define foreach_hw_driver_next \ + _(IP4) \ + _(IP6) \ + _(ETHERNET) + +mac_swap_main_t mac_swap_main; + +static vlib_node_registration_t mac_swap_node; + +#define foreach_mac_swap_error \ +_(SWAPS, "mac addresses swapped") + +typedef enum { +#define _(sym,str) MAC_SWAP_ERROR_##sym, + foreach_mac_swap_error +#undef _ + MAC_SWAP_N_ERROR, +} mac_swap_error_t; + +static char * mac_swap_error_strings[] = { +#define _(sym,string) string, + foreach_mac_swap_error +#undef _ +}; + +/* + * To drop a pkt and increment one of the previous counters: + * + * set b0->error = error_node->errors[RANDOM_ERROR_SAMPLE]; + * set next0 to a disposition index bound to "error-drop". + * + * To manually increment the specific counter MAC_SWAP_ERROR_SAMPLE: + * + * vlib_node_t *n = vlib_get_node (vm, mac_swap.index); + * u32 node_counter_base_index = n->error_heap_index; + * vlib_error_main_t * em = &vm->error_main; + * em->counters[node_counter_base_index + MAC_SWAP_ERROR_SAMPLE] += 1; + * + */ + +typedef enum { + MAC_SWAP_NEXT_DROP, + MAC_SWAP_N_NEXT, +} mac_swap_next_t; + +static uword +mac_swap_node_fn (vlib_main_t * vm, + vlib_node_runtime_t * node, + vlib_frame_t * frame) +{ + u32 n_left_from, * from, * to_next; + mac_swap_next_t next_index; + mac_swap_main_t * msm = &mac_swap_main; + vlib_node_t *n = vlib_get_node (vm, mac_swap_node.index); + u32 node_counter_base_index = n->error_heap_index; + vlib_error_main_t * em = &vm->error_main; + + from = vlib_frame_vector_args (frame); + n_left_from = frame->n_vectors; + next_index = node->cached_next_index; + + while (n_left_from > 0) + { + u32 n_left_to_next; + + vlib_get_next_frame (vm, node, next_index, + to_next, n_left_to_next); + + while (n_left_from >= 4 && n_left_to_next >= 2) + { + u32 bi0, bi1; + vlib_buffer_t * b0, * b1; + u32 next0, next1; + u32 sw_if_index0, sw_if_index1; + uword * p0, * p1; + u64 tmp0a, tmp0b; + u64 tmp1a, tmp1b; + ethernet_header_t * h0, *h1; + + + /* Prefetch next iteration. */ + { + vlib_buffer_t * p2, * p3; + + p2 = vlib_get_buffer (vm, from[2]); + p3 = vlib_get_buffer (vm, from[3]); + + vlib_prefetch_buffer_header (p2, LOAD); + vlib_prefetch_buffer_header (p3, LOAD); + + CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE); + CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE); + } + + to_next[0] = bi0 = from[0]; + to_next[1] = bi1 = from[1]; + from += 2; + to_next += 2; + n_left_from -= 2; + n_left_to_next -= 2; + + b0 = vlib_get_buffer (vm, bi0); + b1 = vlib_get_buffer (vm, bi1); + + sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX]; + next0 = msm->cached_next_index; + sw_if_index1 = vnet_buffer(b1)->sw_if_index[VLIB_RX]; + next1 = msm->cached_next_index; + + if (PREDICT_FALSE (msm->cached_sw_if_index != sw_if_index0)) + { + p0 = hash_get (msm->next_node_index_by_sw_if_index, sw_if_index0); + if (p0 == 0) + { + vnet_hw_interface_t *hw0; + + hw0 = vnet_get_sup_hw_interface (msm->vnet_main, + sw_if_index0); + + next0 = vlib_node_add_next (msm->vlib_main, + mac_swap_node.index, + hw0->output_node_index); + hash_set (msm->next_node_index_by_sw_if_index, + sw_if_index0, next0); + } + else + next0 = p0[0]; + msm->cached_sw_if_index = sw_if_index0; + msm->cached_next_index = next0; + next1 = next0; + } + if (PREDICT_FALSE (msm->cached_sw_if_index != sw_if_index1)) + { + p1 = hash_get (msm->next_node_index_by_sw_if_index, sw_if_index1); + if (p1 == 0) + { + vnet_hw_interface_t *hw1; + + hw1 = vnet_get_sup_hw_interface (msm->vnet_main, + sw_if_index1); + + next1 = vlib_node_add_next (msm->vlib_main, + mac_swap_node.index, + hw1->output_node_index); + hash_set (msm->next_node_index_by_sw_if_index, + sw_if_index1, next1); + } + else + next1 = p1[0]; + msm->cached_sw_if_index = sw_if_index1; + msm->cached_next_index = next1; + } + + em->counters[node_counter_base_index + MAC_SWAP_ERROR_SWAPS] += 2; + + /* reset buffer so we always point at the MAC hdr */ + vlib_buffer_reset (b0); + vlib_buffer_reset (b1); + h0 = vlib_buffer_get_current (b0); + h1 = vlib_buffer_get_current (b1); + + /* Swap 2 x src and dst mac addresses using 8-byte load/stores */ + tmp0a = clib_net_to_host_u64(((u64 *)(h0->dst_address))[0]); + tmp1a = clib_net_to_host_u64(((u64 *)(h1->dst_address))[0]); + tmp0b = clib_net_to_host_u64(((u64 *)(h0->src_address))[0]); + tmp1b = clib_net_to_host_u64(((u64 *)(h1->src_address))[0]); + ((u64 *)(h0->dst_address))[0] = clib_host_to_net_u64(tmp0b); + ((u64 *)(h1->dst_address))[0] = clib_host_to_net_u64(tmp1b); + /* Move the ethertype from "b" to "a" */ + tmp0a &= ~(0xFFFF); + tmp1a &= ~(0xFFFF); + tmp0a |= tmp0b & 0xFFFF; + ((u64 *)(h0->src_address))[0] = clib_host_to_net_u64(tmp0a); + tmp1a |= tmp1b & 0xFFFF; + ((u64 *)(h1->src_address))[0] = clib_host_to_net_u64(tmp1a); + + if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE))) + { + if (b0->flags & VLIB_BUFFER_IS_TRACED) + { + swap_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t)); + memcpy (t->src, h0->src_address, 6); + memcpy (t->dst, h0->dst_address, 6); + t->sw_if_index = sw_if_index0; + t->next_index = next0; + } + if (b1->flags & VLIB_BUFFER_IS_TRACED) + { + swap_trace_t *t = vlib_add_trace (vm, node, b1, sizeof (*t)); + memcpy (t->src, h1->src_address, 6); + memcpy (t->dst, h1->dst_address, 6); + t->sw_if_index = sw_if_index1; + t->next_index = next1; + } + } + + vlib_validate_buffer_enqueue_x2 (vm, node, next_index, + to_next, n_left_to_next, + bi0, bi1, next0, next1); + } + + while (n_left_from > 0 && n_left_to_next > 0) + { + u32 bi0; + vlib_buffer_t * b0; + u32 next0; + u32 sw_if_index0; + uword * p0; + u64 tmp0a, tmp0b; + ethernet_header_t * h0; + + bi0 = from[0]; + to_next[0] = bi0; + from += 1; + to_next += 1; + n_left_from -= 1; + n_left_to_next -= 1; + + b0 = vlib_get_buffer (vm, bi0); + + sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX]; + next0 = msm->cached_next_index; + + if (PREDICT_FALSE (msm->cached_sw_if_index != sw_if_index0)) + { + p0 = hash_get (msm->next_node_index_by_sw_if_index, sw_if_index0); + if (p0 == 0) + { + vnet_hw_interface_t *hw0; + + hw0 = vnet_get_sup_hw_interface (msm->vnet_main, + sw_if_index0); + + next0 = vlib_node_add_next (msm->vlib_main, + mac_swap_node.index, + hw0->output_node_index); + hash_set (msm->next_node_index_by_sw_if_index, + sw_if_index0, next0); + } + else + next0 = p0[0]; + msm->cached_sw_if_index = sw_if_index0; + msm->cached_next_index = next0; + } + + em->counters[node_counter_base_index + MAC_SWAP_ERROR_SWAPS] += 1; + + /* reset buffer so we always point at the MAC hdr */ + vlib_buffer_reset (b0); + h0 = vlib_buffer_get_current (b0); + + /* Exchange src and dst, preserve the ethertype */ + tmp0a = clib_net_to_host_u64(((u64 *)(h0->dst_address))[0]); + tmp0b = clib_net_to_host_u64(((u64 *)(h0->src_address))[0]); + ((u64 *)(h0->dst_address))[0] = clib_host_to_net_u64(tmp0b); + tmp0a &= ~(0xFFFF); + tmp0a |= tmp0b & 0xFFFF; + ((u64 *)(h0->src_address))[0] = clib_host_to_net_u64(tmp0a); + + /* ship it */ + if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE) + && (b0->flags & VLIB_BUFFER_IS_TRACED))) { + swap_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t)); + memcpy (t->src, h0->src_address, 6); + memcpy (t->dst, h0->dst_address, 6); + t->sw_if_index = sw_if_index0; + t->next_index = next0; + } + + vlib_validate_buffer_enqueue_x1 (vm, node, next_index, + to_next, n_left_to_next, + bi0, next0); + } + + vlib_put_next_frame (vm, node, next_index, n_left_to_next); + } + + return frame->n_vectors; +} + +VLIB_REGISTER_NODE (mac_swap_node,static) = { + .function = mac_swap_node_fn, + .name = "mac-swap", + .vector_size = sizeof (u32), + .format_trace = format_swap_trace, + .type = VLIB_NODE_TYPE_INTERNAL, + + .n_errors = ARRAY_LEN(mac_swap_error_strings), + .error_strings = mac_swap_error_strings, + + .n_next_nodes = MAC_SWAP_N_NEXT, + + /* edit / add dispositions here */ + .next_nodes = { + [MAC_SWAP_NEXT_DROP] = "error-drop", + }, +}; + +clib_error_t *mac_swap_init (vlib_main_t *vm) +{ + mac_swap_main_t * msm = &mac_swap_main; + + msm->next_node_index_by_sw_if_index = hash_create (0, sizeof (uword)); + msm->cached_next_index = (u32)~0; + msm->cached_sw_if_index = (u32)~0; + msm->vlib_main = vm; + msm->vnet_main = vnet_get_main(); + + /* Driver RX nodes send pkts here... */ +#define _(a) ixge_set_next_node (IXGE_RX_NEXT_##a##_INPUT, "mac-swap"); + foreach_hw_driver_next +#undef _ +#define _(a) ixgev_set_next_node (IXGEV_RX_NEXT_##a##_INPUT, "mac-swap"); + foreach_hw_driver_next +#undef _ +#define _(a) ige_set_next_node (IGE_RX_NEXT_##a##_INPUT, "mac-swap"); + foreach_hw_driver_next +#undef _ + + return 0; +} + +VLIB_INIT_FUNCTION (mac_swap_init); + diff --git a/vnet/vnet/ethernet/node.c b/vnet/vnet/ethernet/node.c new file mode 100644 index 00000000000..9c943992b68 --- /dev/null +++ b/vnet/vnet/ethernet/node.c @@ -0,0 +1,1112 @@ +/* + * Copyright (c) 2015 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/* + * ethernet_node.c: ethernet packet processing + * + * Copyright (c) 2008 Eliot Dresselhaus + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#include <vlib/vlib.h> +#include <vnet/pg/pg.h> +#include <vnet/ethernet/ethernet.h> +#include <vppinfra/sparse_vec.h> +#include <vnet/l2/l2_bvi.h> + + +#define foreach_ethernet_input_next \ + _ (PUNT, "error-punt") \ + _ (DROP, "error-drop") \ + _ (LLC, "llc-input") + +typedef enum { +#define _(s,n) ETHERNET_INPUT_NEXT_##s, + foreach_ethernet_input_next +#undef _ + ETHERNET_INPUT_N_NEXT, +} ethernet_input_next_t; + +typedef struct { + u8 packet_data[32]; +} ethernet_input_trace_t; + +static u8 * format_ethernet_input_trace (u8 * s, va_list * va) +{ + 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_input_trace_t * t = va_arg (*va, ethernet_input_trace_t *); + + s = format (s, "%U", format_ethernet_header, t->packet_data); + + return s; +} + +vlib_node_registration_t ethernet_input_node; + +typedef enum { + ETHERNET_INPUT_VARIANT_ETHERNET, + ETHERNET_INPUT_VARIANT_ETHERNET_TYPE, + ETHERNET_INPUT_VARIANT_VLAN, + ETHERNET_INPUT_VARIANT_NOT_L2, +} ethernet_input_variant_t; + + +// Compare two ethernet macs. Return 1 if they are the same, 0 if different +static_always_inline u32 +eth_mac_equal (u8 * mac1, u8 * mac2) { + return (*((u32 *)(mac1+0)) == *((u32 *)(mac2+0)) && + *((u32 *)(mac1+2)) == *((u32 *)(mac2+2))); +} + + +// Parse the ethernet header to extract vlan tags and innermost ethertype +static_always_inline void +parse_header (ethernet_input_variant_t variant, + vlib_buffer_t * b0, + u16 * type, + u16 * orig_type, + u16 * outer_id, + u16 * inner_id, + u32 * match_flags) { + + if (variant == ETHERNET_INPUT_VARIANT_ETHERNET + || variant == ETHERNET_INPUT_VARIANT_NOT_L2) { + ethernet_header_t * e0; + + e0 = (void *) (b0->data + b0->current_data); + + vnet_buffer (b0)->ethernet.start_of_ethernet_header = b0->current_data; + + vlib_buffer_advance (b0, sizeof (e0[0])); + + *type = clib_net_to_host_u16(e0->type); + } else if (variant == ETHERNET_INPUT_VARIANT_ETHERNET_TYPE) { + // here when prior node was LLC/SNAP processing + u16 * e0; + + e0 = (void *) (b0->data + b0->current_data); + + vlib_buffer_advance (b0, sizeof (e0[0])); + + *type = clib_net_to_host_u16(e0[0]); + } + + // save for distinguishing between dot1q and dot1ad later + *orig_type = *type; + + // default the tags to 0 (used if there is no corresponding tag) + *outer_id = 0; + *inner_id = 0; + + *match_flags = SUBINT_CONFIG_VALID | SUBINT_CONFIG_MATCH_0_TAG; + + // check for vlan encaps + if ((*type == ETHERNET_TYPE_VLAN) || + (*type == ETHERNET_TYPE_DOT1AD) || + (*type == ETHERNET_TYPE_VLAN_9100) || + (*type == ETHERNET_TYPE_VLAN_9200)) + { + ethernet_vlan_header_t * h0; + u16 tag; + + *match_flags = SUBINT_CONFIG_VALID | SUBINT_CONFIG_MATCH_1_TAG; + + h0 = (void *) (b0->data + b0->current_data); + + tag = clib_net_to_host_u16 (h0->priority_cfi_and_id); + + *outer_id = tag & 0xfff; + + *type = clib_net_to_host_u16(h0->type); + + vlib_buffer_advance (b0, sizeof (h0[0])); + + if (*type == ETHERNET_TYPE_VLAN) { + // Double tagged packet + *match_flags = SUBINT_CONFIG_VALID | SUBINT_CONFIG_MATCH_2_TAG; + + h0 = (void *) (b0->data + b0->current_data); + + tag = clib_net_to_host_u16 (h0->priority_cfi_and_id); + + *inner_id = tag & 0xfff; + + *type = clib_net_to_host_u16(h0->type); + + vlib_buffer_advance (b0, sizeof (h0[0])); + + if (*type == ETHERNET_TYPE_VLAN) { + // More than double tagged packet + *match_flags = SUBINT_CONFIG_VALID | SUBINT_CONFIG_MATCH_3_TAG; + } + } + } +} + +// Determine the subinterface for this packet, given the result of the +// vlan table lookups and vlan header parsing. Check the most specific +// matches first. +static_always_inline void +identify_subint (vnet_hw_interface_t * hi, + vlib_buffer_t * b0, + u32 match_flags, + main_intf_t * main_intf, + vlan_intf_t * vlan_intf, + qinq_intf_t * qinq_intf, + u32 * new_sw_if_index, + u8 * error0, + u32 * is_l2) +{ + u32 matched; + + matched = eth_identify_subint (hi, b0, match_flags, + main_intf, vlan_intf, qinq_intf, + new_sw_if_index, error0, is_l2); + + if (matched) { + + // Perform L3 my-mac filter + // A unicast packet arriving on an L3 interface must have a dmac matching the interface mac. + // This is required for promiscuous mode, else we will forward packets we aren't supposed to. + if (!(*is_l2)) { + ethernet_header_t * e0; + e0 = (void *) (b0->data + vnet_buffer (b0)->ethernet.start_of_ethernet_header); + + if (!(ethernet_address_cast(e0->dst_address))) { + if (!eth_mac_equal((u8 *)e0, hi->hw_address)) { + *error0 = ETHERNET_ERROR_L3_MAC_MISMATCH; + } + } + } + + // Check for down subinterface + *error0 = (*new_sw_if_index) != ~0 ? (*error0) : ETHERNET_ERROR_DOWN; + } +} + +static_always_inline void +determine_next_node (ethernet_main_t * em, + ethernet_input_variant_t variant, + u32 is_l20, + u32 type0, + vlib_buffer_t * b0, + u8 * error0, + u8 * next0) +{ + if (PREDICT_FALSE (*error0 != ETHERNET_ERROR_NONE)) { + // some error occurred + *next0 = ETHERNET_INPUT_NEXT_DROP; + } else if (is_l20) { + *next0 = em->l2_next; + // record the L2 len and reset the buffer so the L2 header is preserved + vnet_buffer(b0)->l2.l2_len = b0->current_data; + vlib_buffer_advance (b0, -(b0->current_data)); + + // check for common IP/MPLS ethertypes + } else if (type0 == ETHERNET_TYPE_IP4) { + *next0 = em->l3_next.input_next_ip4; + } else if (type0 == ETHERNET_TYPE_IP6) { + *next0 = em->l3_next.input_next_ip6; + } else if (type0 == ETHERNET_TYPE_MPLS_UNICAST) { + *next0 = em->l3_next.input_next_mpls; + + } else if (em->redirect_l3) { + // L3 Redirect is on, the cached common next nodes will be + // pointing to the redirect node, catch the uncommon types here + *next0 = em->redirect_l3_next; + } else { + // uncommon ethertype, check table + u32 i0; + i0 = sparse_vec_index (em->l3_next.input_next_by_type, type0); + *next0 = vec_elt (em->l3_next.input_next_by_type, i0); + *error0 = i0 == SPARSE_VEC_INVALID_INDEX ? ETHERNET_ERROR_UNKNOWN_TYPE : *error0; + + // The table is not populated with LLC values, so check that now. + // If variant is variant_ethernet then we came from LLC processing. Don't + // go back there; drop instead using by keeping the drop/bad table result. + if ((type0 < 0x600) && (variant == ETHERNET_INPUT_VARIANT_ETHERNET)) { + *next0 = ETHERNET_INPUT_NEXT_LLC; + } + } +} + +static_always_inline uword +ethernet_input_inline (vlib_main_t * vm, + vlib_node_runtime_t * node, + vlib_frame_t * from_frame, + ethernet_input_variant_t variant) +{ + vnet_main_t * vnm = vnet_get_main(); + ethernet_main_t * em = ðernet_main; + vlib_node_runtime_t * error_node; + u32 n_left_from, next_index, * from, * to_next; + u32 stats_sw_if_index, stats_n_packets, stats_n_bytes; + u32 cpu_index = os_get_cpu_number(); + + if (variant != ETHERNET_INPUT_VARIANT_ETHERNET) + error_node = vlib_node_get_runtime (vm, ethernet_input_node.index); + else + error_node = node; + + from = vlib_frame_vector_args (from_frame); + n_left_from = from_frame->n_vectors; + + if (node->flags & VLIB_NODE_FLAG_TRACE) + vlib_trace_frame_buffers_only (vm, node, + from, + n_left_from, + sizeof (from[0]), + sizeof (ethernet_input_trace_t)); + + next_index = node->cached_next_index; + stats_sw_if_index = node->runtime_data[0]; + stats_n_packets = stats_n_bytes = 0; + + while (n_left_from > 0) + { + u32 n_left_to_next; + + vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); + + while (n_left_from >= 4 && n_left_to_next >= 2) + { + u32 bi0, bi1; + vlib_buffer_t * b0, * b1; + u8 next0, next1, error0, error1; + u16 type0, orig_type0, type1, orig_type1; + u16 outer_id0, inner_id0, outer_id1, inner_id1; + u32 match_flags0, match_flags1; + u32 old_sw_if_index0, new_sw_if_index0, len0, old_sw_if_index1, new_sw_if_index1, len1; + vnet_hw_interface_t * hi0, * hi1; + main_intf_t * main_intf0, * main_intf1; + vlan_intf_t * vlan_intf0, * vlan_intf1; + qinq_intf_t * qinq_intf0, * qinq_intf1; + u32 is_l20, is_l21; + + /* Prefetch next iteration. */ + { + vlib_buffer_t * b2, * b3; + + b2 = vlib_get_buffer (vm, from[2]); + b3 = vlib_get_buffer (vm, from[3]); + + vlib_prefetch_buffer_header (b2, STORE); + vlib_prefetch_buffer_header (b3, STORE); + + CLIB_PREFETCH (b2->data, sizeof (ethernet_header_t), LOAD); + CLIB_PREFETCH (b3->data, sizeof (ethernet_header_t), LOAD); + } + + bi0 = from[0]; + bi1 = from[1]; + to_next[0] = bi0; + to_next[1] = bi1; + from += 2; + to_next += 2; + n_left_to_next -= 2; + n_left_from -= 2; + + b0 = vlib_get_buffer (vm, bi0); + b1 = vlib_get_buffer (vm, bi1); + + error0 = error1 = ETHERNET_ERROR_NONE; + + parse_header (variant, + b0, + &type0, + &orig_type0, + &outer_id0, + &inner_id0, + &match_flags0); + + parse_header (variant, + b1, + &type1, + &orig_type1, + &outer_id1, + &inner_id1, + &match_flags1); + + old_sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX]; + old_sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX]; + + eth_vlan_table_lookups (em, + vnm, + old_sw_if_index0, + orig_type0, + outer_id0, + inner_id0, + &hi0, + &main_intf0, + &vlan_intf0, + &qinq_intf0); + + eth_vlan_table_lookups (em, + vnm, + old_sw_if_index1, + orig_type1, + outer_id1, + inner_id1, + &hi1, + &main_intf1, + &vlan_intf1, + &qinq_intf1); + + identify_subint (hi0, + b0, + match_flags0, + main_intf0, + vlan_intf0, + qinq_intf0, + &new_sw_if_index0, + &error0, + &is_l20); + + identify_subint (hi1, + b1, + match_flags1, + main_intf1, + vlan_intf1, + qinq_intf1, + &new_sw_if_index1, + &error1, + &is_l21); + + // Save RX sw_if_index for later nodes + vnet_buffer (b0)->sw_if_index[VLIB_RX] = error0 != ETHERNET_ERROR_NONE ? old_sw_if_index0 : new_sw_if_index0; + vnet_buffer (b1)->sw_if_index[VLIB_RX] = error1 != ETHERNET_ERROR_NONE ? old_sw_if_index1 : new_sw_if_index1; + + // Check if there is a stat to take (valid and non-main sw_if_index for pkt 0 or pkt 1) + if (((new_sw_if_index0 != ~0) && (new_sw_if_index0 != old_sw_if_index0)) || + ((new_sw_if_index1 != ~0) && (new_sw_if_index1 != old_sw_if_index1))) { + + len0 = vlib_buffer_length_in_chain (vm, b0) + b0->current_data + - vnet_buffer (b0)->ethernet.start_of_ethernet_header; + len1 = vlib_buffer_length_in_chain (vm, b1) + b1->current_data + - vnet_buffer (b1)->ethernet.start_of_ethernet_header; + + stats_n_packets += 2; + stats_n_bytes += len0 + len1; + + if (PREDICT_FALSE (! (new_sw_if_index0 == stats_sw_if_index && new_sw_if_index1 == stats_sw_if_index))) + { + stats_n_packets -= 2; + stats_n_bytes -= len0 + len1; + + if (new_sw_if_index0 != old_sw_if_index0 && new_sw_if_index0 != ~0) + vlib_increment_combined_counter + (vnm->interface_main.combined_sw_if_counters + + VNET_INTERFACE_COUNTER_RX, + cpu_index, + new_sw_if_index0, + 1, + len0); + if (new_sw_if_index1 != old_sw_if_index1 && new_sw_if_index1 != ~0) + vlib_increment_combined_counter + (vnm->interface_main.combined_sw_if_counters + + VNET_INTERFACE_COUNTER_RX, + cpu_index, + new_sw_if_index1, + 1, + len1); + + if (new_sw_if_index0 == new_sw_if_index1) + { + if (stats_n_packets > 0) + { + vlib_increment_combined_counter + (vnm->interface_main.combined_sw_if_counters + + VNET_INTERFACE_COUNTER_RX, + cpu_index, + stats_sw_if_index, + stats_n_packets, + stats_n_bytes); + stats_n_packets = stats_n_bytes = 0; + } + stats_sw_if_index = new_sw_if_index0; + } + } + } + + if (variant == ETHERNET_INPUT_VARIANT_NOT_L2) + is_l20 = is_l21 = 0; + + determine_next_node(em, variant, is_l20, type0, b0, &error0, &next0); + determine_next_node(em, variant, is_l21, type1, b1, &error1, &next1); + + b0->error = error_node->errors[error0]; + b1->error = error_node->errors[error1]; + + // verify speculative enqueue + vlib_validate_buffer_enqueue_x2(vm,node,next_index,to_next,n_left_to_next,bi0,bi1,next0,next1); + } + + while (n_left_from > 0 && n_left_to_next > 0) + { + u32 bi0; + vlib_buffer_t * b0; + u8 error0, next0; + u16 type0, orig_type0; + u16 outer_id0, inner_id0; + u32 match_flags0; + u32 old_sw_if_index0, new_sw_if_index0, len0; + vnet_hw_interface_t * hi0; + main_intf_t * main_intf0; + vlan_intf_t * vlan_intf0; + qinq_intf_t * qinq_intf0; + u32 is_l20; + + // Prefetch next iteration + if (n_left_from > 1) { + vlib_buffer_t * p2; + + p2 = vlib_get_buffer (vm, from[1]); + vlib_prefetch_buffer_header (p2, STORE); + CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, LOAD); + } + + bi0 = from[0]; + to_next[0] = bi0; + from += 1; + to_next += 1; + n_left_from -= 1; + n_left_to_next -= 1; + + b0 = vlib_get_buffer (vm, bi0); + + error0 = ETHERNET_ERROR_NONE; + + parse_header (variant, + b0, + &type0, + &orig_type0, + &outer_id0, + &inner_id0, + &match_flags0); + + old_sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX]; + + eth_vlan_table_lookups (em, + vnm, + old_sw_if_index0, + orig_type0, + outer_id0, + inner_id0, + &hi0, + &main_intf0, + &vlan_intf0, + &qinq_intf0); + + identify_subint (hi0, + b0, + match_flags0, + main_intf0, + vlan_intf0, + qinq_intf0, + &new_sw_if_index0, + &error0, + &is_l20); + + // Save RX sw_if_index for later nodes + vnet_buffer (b0)->sw_if_index[VLIB_RX] = error0 != ETHERNET_ERROR_NONE ? old_sw_if_index0 : new_sw_if_index0; + + // Increment subinterface stats + // Note that interface-level counters have already been incremented + // prior to calling this function. Thus only subinterface counters + // are incremented here. + // + // Interface level counters include packets received on the main + // interface and all subinterfaces. Subinterface level counters + // include only those packets received on that subinterface + // Increment stats if the subint is valid and it is not the main intf + if ((new_sw_if_index0 != ~0) && (new_sw_if_index0 != old_sw_if_index0)) { + + len0 = vlib_buffer_length_in_chain (vm, b0) + b0->current_data + - vnet_buffer (b0)->ethernet.start_of_ethernet_header; + + stats_n_packets += 1; + stats_n_bytes += len0; + + // Batch stat increments from the same subinterface so counters + // don't need to be incremented for every packet. + if (PREDICT_FALSE (new_sw_if_index0 != stats_sw_if_index)) { + stats_n_packets -= 1; + stats_n_bytes -= len0; + + if (new_sw_if_index0 != ~0) + vlib_increment_combined_counter + (vnm->interface_main.combined_sw_if_counters + + VNET_INTERFACE_COUNTER_RX, + cpu_index, + new_sw_if_index0, + 1, + len0); + if (stats_n_packets > 0) { + vlib_increment_combined_counter + (vnm->interface_main.combined_sw_if_counters + + VNET_INTERFACE_COUNTER_RX, + cpu_index, + stats_sw_if_index, + stats_n_packets, + stats_n_bytes); + stats_n_packets = stats_n_bytes = 0; + } + stats_sw_if_index = new_sw_if_index0; + } + } + + if (variant == ETHERNET_INPUT_VARIANT_NOT_L2) + is_l20 = 0; + + determine_next_node(em, variant, is_l20, type0, b0, &error0, &next0); + + b0->error = error_node->errors[error0]; + + // verify speculative enqueue + vlib_validate_buffer_enqueue_x1 (vm, node, next_index, + to_next, n_left_to_next, + bi0, next0); + } + + vlib_put_next_frame (vm, node, next_index, n_left_to_next); + } + + // Increment any remaining batched stats + if (stats_n_packets > 0) + { + vlib_increment_combined_counter + (vnm->interface_main.combined_sw_if_counters + + VNET_INTERFACE_COUNTER_RX, + cpu_index, + stats_sw_if_index, + stats_n_packets, + stats_n_bytes); + node->runtime_data[0] = stats_sw_if_index; + } + + return from_frame->n_vectors; +} + +static uword +ethernet_input (vlib_main_t * vm, + vlib_node_runtime_t * node, + vlib_frame_t * from_frame) +{ return ethernet_input_inline (vm, node, from_frame, ETHERNET_INPUT_VARIANT_ETHERNET); } + +static uword +ethernet_input_type (vlib_main_t * vm, + vlib_node_runtime_t * node, + vlib_frame_t * from_frame) +{ return ethernet_input_inline (vm, node, from_frame, ETHERNET_INPUT_VARIANT_ETHERNET_TYPE); } + +static uword +ethernet_input_not_l2 (vlib_main_t * vm, + vlib_node_runtime_t * node, + vlib_frame_t * from_frame) +{ return ethernet_input_inline (vm, node, from_frame, ETHERNET_INPUT_VARIANT_NOT_L2); } + + +// Return the subinterface config struct for the given sw_if_index +// Also return via parameter the appropriate match flags for the +// configured number of tags. +// On error (unsupported or not ethernet) return 0. +static subint_config_t * +ethernet_sw_interface_get_config (vnet_main_t * vnm, + u32 sw_if_index, + u32 * flags, + u32 * unsupported) { + ethernet_main_t * em = ðernet_main; + vnet_hw_interface_t * hi; + vnet_sw_interface_t * si; + main_intf_t * main_intf; + vlan_table_t * vlan_table; + qinq_table_t * qinq_table; + subint_config_t * subint = 0; + + hi = vnet_get_sup_hw_interface (vnm, sw_if_index); + + if (!hi || (hi->hw_class_index != ethernet_hw_interface_class.index)) { + *unsupported = 0; + goto done; // non-ethernet interface + } + + // ensure there's an entry for the main intf (shouldn't really be necessary) + vec_validate (em->main_intfs, hi->hw_if_index); + main_intf = vec_elt_at_index (em->main_intfs, hi->hw_if_index); + + // Locate the subint for the given ethernet config + si = vnet_get_sw_interface (vnm, sw_if_index); + + if (si->sub.eth.flags.default_sub) { + subint = &main_intf->default_subint; + *flags = SUBINT_CONFIG_MATCH_0_TAG | + SUBINT_CONFIG_MATCH_1_TAG | + SUBINT_CONFIG_MATCH_2_TAG | + SUBINT_CONFIG_MATCH_3_TAG; + } else if ((si->sub.eth.flags.no_tags) || + (si->sub.eth.raw_flags == 0)) { + // if no flags are set then this is a main interface + // so treat as untagged + subint = &main_intf->untagged_subint; + *flags = SUBINT_CONFIG_MATCH_0_TAG; + } else { + // one or two tags + // first get the vlan table + if (si->sub.eth.flags.dot1ad) { + if (main_intf->dot1ad_vlans == 0) { + // Allocate a vlan table from the pool + pool_get(em->vlan_pool, vlan_table); + main_intf->dot1ad_vlans = vlan_table - em->vlan_pool; + } else { + // Get ptr to existing vlan table + vlan_table = vec_elt_at_index (em->vlan_pool, main_intf->dot1ad_vlans); + } + } else { // dot1q + if (main_intf->dot1q_vlans == 0) { + // Allocate a vlan table from the pool + pool_get(em->vlan_pool, vlan_table); + main_intf->dot1q_vlans = vlan_table - em->vlan_pool; + } else { + // Get ptr to existing vlan table + vlan_table = vec_elt_at_index (em->vlan_pool, main_intf->dot1q_vlans); + } + } + + if (si->sub.eth.flags.one_tag) { + *flags = si->sub.eth.flags.exact_match ? + SUBINT_CONFIG_MATCH_1_TAG : + (SUBINT_CONFIG_MATCH_1_TAG | + SUBINT_CONFIG_MATCH_2_TAG | + SUBINT_CONFIG_MATCH_3_TAG); + + if (si->sub.eth.flags.outer_vlan_id_any) { + // not implemented yet + *unsupported =1; + goto done; + } else { + // a single vlan, a common case + subint = &vlan_table->vlans[si->sub.eth.outer_vlan_id].single_tag_subint; + } + + } else { + // Two tags + *flags = si->sub.eth.flags.exact_match ? + SUBINT_CONFIG_MATCH_2_TAG : + (SUBINT_CONFIG_MATCH_2_TAG | + SUBINT_CONFIG_MATCH_3_TAG); + + if (si->sub.eth.flags.outer_vlan_id_any && si->sub.eth.flags.inner_vlan_id_any) { + // not implemented yet + *unsupported = 1; + goto done; + } + + if (si->sub.eth.flags.inner_vlan_id_any) { + // a specific outer and "any" inner + // don't need a qinq table for this + subint = &vlan_table->vlans[si->sub.eth.outer_vlan_id].inner_any_subint; + if (si->sub.eth.flags.exact_match) { + *flags = SUBINT_CONFIG_MATCH_2_TAG; + } else { + *flags = SUBINT_CONFIG_MATCH_2_TAG | + SUBINT_CONFIG_MATCH_3_TAG; + } + } else { + // a specific outer + specifc innner vlan id, a common case + + // get the qinq table + if (vlan_table->vlans[si->sub.eth.outer_vlan_id].qinqs == 0) { + // Allocate a qinq table from the pool + pool_get(em->qinq_pool, qinq_table); + vlan_table->vlans[si->sub.eth.outer_vlan_id].qinqs = qinq_table - em->qinq_pool; + } else { + // Get ptr to existing qinq table + qinq_table = vec_elt_at_index (em->qinq_pool, vlan_table->vlans[si->sub.eth.outer_vlan_id].qinqs); + } + subint = &qinq_table->vlans[si->sub.eth.inner_vlan_id].subint; + } + } + } + + done: + return subint; +} + +clib_error_t * +ethernet_sw_interface_up_down (vnet_main_t * vnm, + u32 sw_if_index, + u32 flags) +{ + subint_config_t * subint; + u32 dummy_flags; + u32 dummy_unsup; + clib_error_t * error = 0; + + // Find the config for this subinterface + subint = ethernet_sw_interface_get_config (vnm, sw_if_index, &dummy_flags, &dummy_unsup); + + if (subint == 0) { + // not implemented yet or not ethernet + goto done; + } + + subint->sw_if_index = + ((flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? sw_if_index : ~0); + + done: + return error; +} + +VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (ethernet_sw_interface_up_down); + + +// Set the L2/L3 mode for the subinterface +void +ethernet_sw_interface_set_l2_mode (vnet_main_t * vnm, + u32 sw_if_index, + u32 l2) +{ + subint_config_t *subint; + u32 dummy_flags; + u32 dummy_unsup; + int is_port; + vnet_sw_interface_t * sw = vnet_get_sw_interface (vnm, sw_if_index); + + is_port = !(sw->type == VNET_SW_INTERFACE_TYPE_SUB); + + // Find the config for this subinterface + subint = ethernet_sw_interface_get_config (vnm, sw_if_index, &dummy_flags, &dummy_unsup); + + if (subint == 0) { + // unimplemented or not ethernet + goto done; + } + + // Double check that the config we found is for our interface (or the interface is down) + ASSERT ((subint->sw_if_index == sw_if_index) | (subint->sw_if_index == ~0)); + + if (l2) { + subint->flags |= SUBINT_CONFIG_L2; + if (is_port) + subint->flags |= + SUBINT_CONFIG_MATCH_0_TAG | SUBINT_CONFIG_MATCH_1_TAG + | SUBINT_CONFIG_MATCH_2_TAG | SUBINT_CONFIG_MATCH_3_TAG; + } else { + subint->flags &= ~SUBINT_CONFIG_L2; + if (is_port) + subint->flags &= + ~(SUBINT_CONFIG_MATCH_1_TAG | SUBINT_CONFIG_MATCH_2_TAG + | SUBINT_CONFIG_MATCH_3_TAG); + } + + done: + return; +} + + +static clib_error_t * +ethernet_sw_interface_add_del (vnet_main_t * vnm, + u32 sw_if_index, + u32 is_create) +{ + clib_error_t * error = 0; + subint_config_t *subint; + u32 match_flags; + u32 unsupported; + + // Find the config for this subinterface + subint = ethernet_sw_interface_get_config (vnm, sw_if_index, &match_flags, &unsupported); + + if (subint == 0) { + // not implemented yet or not ethernet + if (unsupported) { + // this is the NYI case + error = clib_error_return (0, "not implemented yet"); + } + goto done; + } + + if (!is_create) { + subint->flags = 0; + return error; + } + + // Initialize the subint + if (subint->flags & SUBINT_CONFIG_VALID) { + // Error vlan already in use + error = clib_error_return (0, "vlan is already in use"); + } else { + // Note that config is L3 by defaulty + subint->flags = SUBINT_CONFIG_VALID | match_flags; + subint->sw_if_index = ~0; // because interfaces are initially down + } + + done: + return error; +} + +VNET_SW_INTERFACE_ADD_DEL_FUNCTION (ethernet_sw_interface_add_del); + +static char * ethernet_error_strings[] = { +#define ethernet_error(n,c,s) s, +#include "error.def" +#undef ethernet_error +}; + +VLIB_REGISTER_NODE (ethernet_input_node) = { + .function = ethernet_input, + .name = "ethernet-input", + /* Takes a vector of packets. */ + .vector_size = sizeof (u32), + + .n_errors = ETHERNET_N_ERROR, + .error_strings = ethernet_error_strings, + + .n_next_nodes = ETHERNET_INPUT_N_NEXT, + .next_nodes = { +#define _(s,n) [ETHERNET_INPUT_NEXT_##s] = n, + foreach_ethernet_input_next +#undef _ + }, + + .format_buffer = format_ethernet_header_with_length, + .format_trace = format_ethernet_input_trace, + .unformat_buffer = unformat_ethernet_header, +}; + +VLIB_REGISTER_NODE (ethernet_input_type_node,static) = { + .function = ethernet_input_type, + .name = "ethernet-input-type", + /* Takes a vector of packets. */ + .vector_size = sizeof (u32), + + .n_next_nodes = ETHERNET_INPUT_N_NEXT, + .next_nodes = { +#define _(s,n) [ETHERNET_INPUT_NEXT_##s] = n, + foreach_ethernet_input_next +#undef _ + }, +}; + +VLIB_REGISTER_NODE (ethernet_input_not_l2_node,static) = { + .function = ethernet_input_not_l2, + .name = "ethernet-input-not-l2", + /* Takes a vector of packets. */ + .vector_size = sizeof (u32), + + .n_next_nodes = ETHERNET_INPUT_N_NEXT, + .next_nodes = { +#define _(s,n) [ETHERNET_INPUT_NEXT_##s] = n, + foreach_ethernet_input_next +#undef _ + }, +}; + +void ethernet_set_rx_redirect (vnet_main_t * vnm, + vnet_hw_interface_t * hi, + u32 enable) +{ + // Insure all packets go to ethernet-input (i.e. untagged ipv4 packets + // don't go directly to ip4-input) + vnet_hw_interface_rx_redirect_to_node + (vnm, hi->hw_if_index, enable ? ethernet_input_node.index : ~0); +} + + +/* + * Initialization and registration for the next_by_ethernet structure + */ + +clib_error_t * next_by_ethertype_init (next_by_ethertype_t * l3_next) +{ + l3_next->input_next_by_type = sparse_vec_new + (/* elt bytes */ sizeof (l3_next->input_next_by_type[0]), + /* bits in index */ BITS (((ethernet_header_t *) 0)->type)); + + vec_validate (l3_next->sparse_index_by_input_next_index, ETHERNET_INPUT_NEXT_DROP); + vec_validate (l3_next->sparse_index_by_input_next_index, ETHERNET_INPUT_NEXT_PUNT); + l3_next->sparse_index_by_input_next_index[ETHERNET_INPUT_NEXT_DROP] + = SPARSE_VEC_INVALID_INDEX; + l3_next->sparse_index_by_input_next_index[ETHERNET_INPUT_NEXT_PUNT] + = SPARSE_VEC_INVALID_INDEX; + + return 0; +} + +// Add an ethertype -> next index mapping to the structure +clib_error_t * next_by_ethertype_register (next_by_ethertype_t * l3_next, + u32 ethertype, + u32 next_index) +{ + u32 i; + u16 * n; + ethernet_main_t * em = ðernet_main; + + /* Setup ethernet type -> next index sparse vector mapping. */ + n = sparse_vec_validate (l3_next->input_next_by_type, ethertype); + n[0] = next_index; + + /* Rebuild next index -> sparse index inverse mapping when sparse vector + is updated. */ + vec_validate (l3_next->sparse_index_by_input_next_index, next_index); + for (i = 1; i < vec_len (l3_next->input_next_by_type); i++) + l3_next->sparse_index_by_input_next_index[l3_next->input_next_by_type[i]] = i; + + // do not allow the cached next index's to be updated if L3 + // redirect is enabled, as it will have overwritten them + if (!em->redirect_l3) { + // Cache common ethertypes directly + if (ethertype == ETHERNET_TYPE_IP4) { + l3_next->input_next_ip4 = next_index; + } else if (ethertype == ETHERNET_TYPE_IP6) { + l3_next->input_next_ip6 = next_index; + } else if (ethertype == ETHERNET_TYPE_MPLS_UNICAST) { + l3_next->input_next_mpls = next_index; + } + } + return 0; +} + + +static clib_error_t * ethernet_input_init (vlib_main_t * vm) +{ + ethernet_main_t * em = ðernet_main; + __attribute__((unused)) vlan_table_t * invalid_vlan_table; + __attribute__((unused)) qinq_table_t * invalid_qinq_table; + + ethernet_setup_node (vm, ethernet_input_node.index); + ethernet_setup_node (vm, ethernet_input_type_node.index); + ethernet_setup_node (vm, ethernet_input_not_l2_node.index); + + next_by_ethertype_init (&em->l3_next); + + // Initialize pools and vector for vlan parsing + vec_validate (em->main_intfs, 10); // 10 main interfaces + pool_alloc(em->vlan_pool, 10); + pool_alloc(em->qinq_pool, 1); + + // The first vlan pool will always be reserved for an invalid table + pool_get(em->vlan_pool, invalid_vlan_table); // first id = 0 + // The first qinq pool will always be reserved for an invalid table + pool_get(em->qinq_pool, invalid_qinq_table); // first id = 0 + + return 0; +} + +VLIB_INIT_FUNCTION (ethernet_input_init); + +void +ethernet_register_input_type (vlib_main_t * vm, + ethernet_type_t type, + u32 node_index) +{ + ethernet_main_t * em = ðernet_main; + ethernet_type_info_t * ti; + u32 i; + + { + clib_error_t * error = vlib_call_init_function (vm, ethernet_init); + if (error) + clib_error_report (error); + } + + ti = ethernet_get_type_info (em, type); + ti->node_index = node_index; + ti->next_index = vlib_node_add_next (vm, + ethernet_input_node.index, + node_index); + i = vlib_node_add_next (vm, + ethernet_input_type_node.index, + node_index); + ASSERT (i == ti->next_index); + + i = vlib_node_add_next (vm, + ethernet_input_not_l2_node.index, + node_index); + ASSERT (i == ti->next_index); + + // Add the L3 node for this ethertype to the next nodes structure + next_by_ethertype_register (&em->l3_next, type, ti->next_index); + + // Call the registration functions for other nodes that want a mapping + l2bvi_register_input_type (vm, type, node_index); +} + +void +ethernet_register_l2_input (vlib_main_t * vm, + u32 node_index) +{ + ethernet_main_t * em = ðernet_main; + u32 i; + + em->l2_next = vlib_node_add_next (vm, ethernet_input_node.index, node_index); + + /* + * Even if we never use these arcs, we have to align the next indices... + */ + i = vlib_node_add_next (vm, ethernet_input_type_node.index, node_index); + + ASSERT (i == em->l2_next); + + i = vlib_node_add_next (vm, + ethernet_input_not_l2_node.index, + node_index); + ASSERT (i == em->l2_next); +} + +// Register a next node for L3 redirect, and enable L3 redirect +void +ethernet_register_l3_redirect (vlib_main_t * vm, + u32 node_index) +{ + ethernet_main_t * em = ðernet_main; + u32 i; + + em->redirect_l3 = 1; + em->redirect_l3_next = vlib_node_add_next(vm, + ethernet_input_node.index, + node_index); + /* + * Change the cached next nodes to the redirect node + */ + em->l3_next.input_next_ip4 = em->redirect_l3_next; + em->l3_next.input_next_ip6 = em->redirect_l3_next; + em->l3_next.input_next_mpls = em->redirect_l3_next; + + /* + * Even if we never use these arcs, we have to align the next indices... + */ + i = vlib_node_add_next (vm, ethernet_input_type_node.index, node_index); + + ASSERT (i == em->redirect_l3_next); +} diff --git a/vnet/vnet/ethernet/packet.h b/vnet/vnet/ethernet/packet.h new file mode 100644 index 00000000000..b5d1dcba931 --- /dev/null +++ b/vnet/vnet/ethernet/packet.h @@ -0,0 +1,95 @@ +/* + * Copyright (c) 2015 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/* + * ethernet/packet.h: ethernet packet format. + * + * Copyright (c) 2008 Eliot Dresselhaus + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#ifndef included_ethernet_packet_h +#define included_ethernet_packet_h + +typedef enum { +#define ethernet_type(n,s) ETHERNET_TYPE_##s = n, +#include <vnet/ethernet/types.def> +#undef ethernet_type +} ethernet_type_t; + +typedef struct { + /* Source/destination address. */ + u8 dst_address[6]; + u8 src_address[6]; + + /* Ethernet type. */ + u16 type; +} ethernet_header_t; + +#define ETHERNET_ADDRESS_UNICAST 0 +#define ETHERNET_ADDRESS_MULTICAST 1 + +/* I/G bit: individual (unicast)/group (broadcast/multicast). */ +always_inline uword +ethernet_address_cast (u8 * a) +{ return (a[0] >> 0) & 1; } + +always_inline uword +ethernet_address_is_locally_administered (u8 * a) +{ return (a[0] >> 1) & 1; } + +always_inline void +ethernet_address_set_locally_administered (u8 * a) +{ a[0] |= 1 << 1; } + +/* For VLAN ethernet type. */ +typedef struct { + /* 3 bit priority, 1 bit CFI and 12 bit vlan id. */ + u16 priority_cfi_and_id; + +#define ETHERNET_N_VLAN (1 << 12) + + /* Inner ethernet type. */ + u16 type; +} ethernet_vlan_header_t; + + +/* VLAN with ethertype first and vlan id second */ +typedef struct { + /* vlan type */ + u16 type; + + /* 3 bit priority, 1 bit CFI and 12 bit vlan id. */ + u16 priority_cfi_and_id; +} ethernet_vlan_header_tv_t; + +#endif /* included_ethernet_packet_h */ diff --git a/vnet/vnet/ethernet/pg.c b/vnet/vnet/ethernet/pg.c new file mode 100644 index 00000000000..838688db451 --- /dev/null +++ b/vnet/vnet/ethernet/pg.c @@ -0,0 +1,177 @@ +/* + * Copyright (c) 2015 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/* + * ethernet_pg.c: packet generator ethernet interface + * + * Copyright (c) 2008 Eliot Dresselhaus + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#include <vlib/vlib.h> +#include <vnet/pg/pg.h> +#include <vnet/ethernet/ethernet.h> + +typedef struct { + pg_edit_t type; + pg_edit_t src_address; + pg_edit_t dst_address; +} pg_ethernet_header_t; + +static inline void +pg_ethernet_header_init (pg_ethernet_header_t * e) +{ + pg_edit_init (&e->type, ethernet_header_t, type); + pg_edit_init (&e->src_address, ethernet_header_t, src_address); + pg_edit_init (&e->dst_address, ethernet_header_t, dst_address); +} + +typedef struct { + pg_edit_t type; + pg_edit_t id; + pg_edit_t cfi; + pg_edit_t priority; +} pg_ethernet_vlan_header_t; + +static inline void +pg_ethernet_vlan_header_init (pg_ethernet_vlan_header_t * v, + int vlan_index) +{ + ASSERT (vlan_index < ARRAY_LEN (((ethernet_max_header_t *) 0)->vlan)); + pg_edit_init (&v->type, ethernet_max_header_t, vlan[vlan_index].type); + + pg_edit_init_bitfield (&v->id, ethernet_max_header_t, + vlan[vlan_index].priority_cfi_and_id, + 0, 12); + pg_edit_init_bitfield (&v->cfi, ethernet_max_header_t, + vlan[vlan_index].priority_cfi_and_id, + 12, 1); + pg_edit_init_bitfield (&v->priority, ethernet_max_header_t, + vlan[vlan_index].priority_cfi_and_id, + 13, 3); +} + +uword +unformat_pg_ethernet_header (unformat_input_t * input, va_list * args) +{ + pg_stream_t * s = va_arg (*args, pg_stream_t *); + pg_ethernet_header_t * e; + pg_ethernet_vlan_header_t * v; + pg_edit_t * ether_type_edit; + u32 n_vlan, error, group_index; + + e = pg_create_edit_group (s, sizeof (e[0]), sizeof (ethernet_header_t), + &group_index); + pg_ethernet_header_init (e); + error = 1; + + if (! unformat (input, "%U: %U -> %U", + unformat_pg_edit, + unformat_ethernet_type_net_byte_order, &e->type, + unformat_pg_edit, + unformat_ethernet_address, &e->src_address, + unformat_pg_edit, + unformat_ethernet_address, &e->dst_address)) + goto done; + + n_vlan = 0; + while (unformat (input, "vlan")) + { + v = pg_add_edits (s, sizeof (v[0]), sizeof (ethernet_vlan_header_t), + group_index); + pg_ethernet_vlan_header_init (v, n_vlan); + + if (! unformat_user (input, unformat_pg_edit, + unformat_pg_number, &v->id)) + goto done; + + if (! unformat (input, "priority %U", unformat_pg_edit, + unformat_pg_number, &v->priority)) + pg_edit_set_fixed (&v->priority, 0); + + if (! unformat (input, "cfi %U", unformat_pg_edit, + unformat_pg_number, &v->cfi)) + pg_edit_set_fixed (&v->cfi, 0); + + /* Too many vlans given. */ + if (n_vlan >= 2) + goto done; + + n_vlan++; + } + + /* Address of e may have changed due to vlan edits being added */ + e = pg_get_edit_group (s, group_index); + v = (void *) (e + 1); + + /* Correct types for vlan packets. */ + ether_type_edit = &e->type; + if (n_vlan > 0) + { + int i; + + ether_type_edit = &v[n_vlan - 1].type; + pg_edit_copy_type_and_values (ether_type_edit, &e->type); + pg_edit_set_fixed (&e->type, ETHERNET_TYPE_VLAN); + + for (i = 0; i < n_vlan - 1; i++) + pg_edit_set_fixed (&v[i].type, ETHERNET_TYPE_VLAN); + } + + { + ethernet_main_t * em = ðernet_main; + ethernet_type_info_t * ti = 0; + pg_node_t * pg_node = 0; + + if (ether_type_edit->type == PG_EDIT_FIXED) + { + u16 t = *(u16 *) ether_type_edit->values[PG_EDIT_LO]; + ti = ethernet_get_type_info (em, clib_net_to_host_u16 (t)); + if (ti && ti->node_index != ~0) + pg_node = pg_get_node (ti->node_index); + } + + if (pg_node && pg_node->unformat_edit + && unformat_user (input, pg_node->unformat_edit, s)) + ; + else if (! unformat_user (input, unformat_pg_payload, s)) + goto done; + } + + error = 0; + + done: + if (error) + pg_free_edit_group (s); + return error == 0; +} + diff --git a/vnet/vnet/ethernet/types.def b/vnet/vnet/ethernet/types.def new file mode 100644 index 00000000000..37c97f62cdb --- /dev/null +++ b/vnet/vnet/ethernet/types.def @@ -0,0 +1,112 @@ +/* + * Copyright (c) 2015 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/* Emacs editing mode -*-C-*- Ethernet types. */ + +/* + * ethernet types + * + * Copyright (c) 2008 Eliot Dresselhaus + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +/* Types < 0x600 (1536) are LLC packet lengths. */ +ethernet_type (0x600, LLC_LENGTH) + +ethernet_type (0x600, XNS_IDP) +ethernet_type (0x800, IP4) +ethernet_type (0x806, ARP) +ethernet_type (0x0BAD, VINES_IP) +ethernet_type (0x0BAE, VINES_LOOPBACK) +ethernet_type (0x0BAF, VINES_ECHO) +ethernet_type (0x1984, TRAIN) +ethernet_type (0x2000, CDP) +ethernet_type (0x2001, CGMP) +ethernet_type (0x2007, SRP_CONTROL) +ethernet_type (0x2452, CENTRINO_PROMISC) +ethernet_type (0x6000, DECNET) +ethernet_type (0x6001, DECNET_DUMP_LOAD) +ethernet_type (0x6002, DECNET_REMOTE_CONSOLE) +ethernet_type (0x6003, DECNET_ROUTE) +ethernet_type (0x6004, DEC_LAT) +ethernet_type (0x6005, DEC_DIAGNOSTIC) +ethernet_type (0x6006, DEC_CUSTOMER) +ethernet_type (0x6007, DEC_SCA) +ethernet_type (0x6558, TRANSPARENT_BRIDGING) +ethernet_type (0x6559, RAW_FRAME_RELAY) +ethernet_type (0x8035, REVERSE_ARP) +ethernet_type (0x8038, DEC_LAN_BRIDGE) +ethernet_type (0x803D, DEC_ETHERNET_ENCRYPTION) +ethernet_type (0x803F, DEC_LAN_TRAFFIC_MONITOR) +ethernet_type (0x8041, DEC_LAST) +ethernet_type (0x809B, APPLETALK) +ethernet_type (0x80D5, IBM_SNA) +ethernet_type (0x80F3, APPLETALK_AARP) +ethernet_type (0x80FF, WELLFLEET_COMPRESSION) +ethernet_type (0x8100, VLAN) +ethernet_type (0x8137, IPX) +ethernet_type (0x814C, SNMP) +ethernet_type (0x81FD, CABLETRON_ISMP) +ethernet_type (0x81FF, CABLETRON_ISMP_TBFLOOD) +ethernet_type (0x86DD, IP6) +ethernet_type (0x86DF, ATOMIC) +ethernet_type (0x876B, TCP_IP_COMPRESSION) +ethernet_type (0x876C, IP_AUTONOMOUS_SYSTEMS) +ethernet_type (0x876D, SECURE_DATA) +ethernet_type (0x8808, MAC_CONTROL) +ethernet_type (0x8809, SLOW_PROTOCOLS) +ethernet_type (0x880B, PPP) +ethernet_type (0x8847, MPLS_UNICAST) +ethernet_type (0x8848, MPLS_MULTICAST) +ethernet_type (0x8863, PPPOE_DISCOVERY) +ethernet_type (0x8864, PPPOE_SESSION) +ethernet_type (0x886D, INTEL_ANS) +ethernet_type (0x886F, MICROSOFT_NLB_HEARTBEAT) +ethernet_type (0x8881, CDMA_2000) +ethernet_type (0x888e, 802_1X_AUTHENTICATION) +ethernet_type (0x8892, PROFINET) +ethernet_type (0x889a, HYPERSCSI) +ethernet_type (0x88a2, AOE) +ethernet_type (0x88a8, DOT1AD) +ethernet_type (0x88AE, BRDWALK) +ethernet_type (0x88B7, 802_OUI_EXTENDED) +ethernet_type (0x88c7, 802_11I_PRE_AUTHENTICATION) +ethernet_type (0x88cc, 802_1_LLDP) +ethernet_type (0x894f, VPATH_3) +ethernet_type (0x9000, LOOPBACK) +ethernet_type (0x9021, RTNET_MAC) +ethernet_type (0x9022, RTNET_CONFIG) +ethernet_type (0x9100, VLAN_9100) +ethernet_type (0x9200, VLAN_9200) +ethernet_type (0x9999, PGLAN) +ethernet_type (0xFEFE, SRP_ISIS) +ethernet_type (0xFFFF, RESERVED) |