+/* process frame of buffers, store ethertype into array and update
+ buffer metadata fields depending on interface being l2 or l3 assuming that
+ packets are untagged. For tagged packets those fields are updated later.
+ Optionally store Destionation MAC address and tag data into arrays
+ for further processing */
+
+STATIC_ASSERT (VLIB_FRAME_SIZE % 8 == 0,
+ "VLIB_FRAME_SIZE must be power of 8");
+static_always_inline void
+eth_input_process_frame (vlib_main_t * vm, vlib_node_runtime_t * node,
+ vnet_hw_interface_t * hi,
+ u32 * buffer_indices, u32 n_packets, int main_is_l3,
+ int ip4_cksum_ok, int dmac_check)
+{
+ ethernet_main_t *em = ðernet_main;
+ u16 nexts[VLIB_FRAME_SIZE], *next;
+ u16 etypes[VLIB_FRAME_SIZE], *etype = etypes;
+ u64 dmacs[VLIB_FRAME_SIZE], *dmac = dmacs;
+ u8 dmacs_bad[VLIB_FRAME_SIZE];
+ u64 tags[VLIB_FRAME_SIZE], *tag = tags;
+ u16 slowpath_indices[VLIB_FRAME_SIZE];
+ u16 n_slowpath, i;
+ u16 next_ip4, next_ip6, next_mpls, next_l2;
+ u16 et_ip4 = clib_host_to_net_u16 (ETHERNET_TYPE_IP4);
+ u16 et_ip6 = clib_host_to_net_u16 (ETHERNET_TYPE_IP6);
+ u16 et_mpls = clib_host_to_net_u16 (ETHERNET_TYPE_MPLS);
+ u16 et_vlan = clib_host_to_net_u16 (ETHERNET_TYPE_VLAN);
+ u16 et_dot1ad = clib_host_to_net_u16 (ETHERNET_TYPE_DOT1AD);
+ i32 n_left = n_packets;
+ vlib_buffer_t *b[20];
+ u32 *from;
+
+ from = buffer_indices;
+
+ while (n_left >= 20)
+ {
+ vlib_buffer_t **ph = b + 16, **pd = b + 8;
+ vlib_get_buffers (vm, from, b, 4);
+ vlib_get_buffers (vm, from + 8, pd, 4);
+ vlib_get_buffers (vm, from + 16, ph, 4);
+
+ vlib_prefetch_buffer_header (ph[0], LOAD);
+ vlib_prefetch_buffer_data (pd[0], LOAD);
+ eth_input_get_etype_and_tags (b, etype, tag, dmac, 0, dmac_check);
+
+ vlib_prefetch_buffer_header (ph[1], LOAD);
+ vlib_prefetch_buffer_data (pd[1], LOAD);
+ eth_input_get_etype_and_tags (b, etype, tag, dmac, 1, dmac_check);
+
+ vlib_prefetch_buffer_header (ph[2], LOAD);
+ vlib_prefetch_buffer_data (pd[2], LOAD);
+ eth_input_get_etype_and_tags (b, etype, tag, dmac, 2, dmac_check);
+
+ vlib_prefetch_buffer_header (ph[3], LOAD);
+ vlib_prefetch_buffer_data (pd[3], LOAD);
+ eth_input_get_etype_and_tags (b, etype, tag, dmac, 3, dmac_check);
+
+ eth_input_adv_and_flags_x4 (b, main_is_l3);
+
+ /* next */
+ n_left -= 4;
+ etype += 4;
+ tag += 4;
+ dmac += 4;
+ from += 4;
+ }
+ while (n_left >= 4)
+ {
+ vlib_get_buffers (vm, from, b, 4);
+ eth_input_get_etype_and_tags (b, etype, tag, dmac, 0, dmac_check);
+ eth_input_get_etype_and_tags (b, etype, tag, dmac, 1, dmac_check);
+ eth_input_get_etype_and_tags (b, etype, tag, dmac, 2, dmac_check);
+ eth_input_get_etype_and_tags (b, etype, tag, dmac, 3, dmac_check);
+ eth_input_adv_and_flags_x4 (b, main_is_l3);
+
+ /* next */
+ n_left -= 4;
+ etype += 4;
+ tag += 4;
+ dmac += 4;
+ from += 4;
+ }
+ while (n_left)
+ {
+ vlib_get_buffers (vm, from, b, 1);
+ eth_input_get_etype_and_tags (b, etype, tag, dmac, 0, dmac_check);
+ eth_input_adv_and_flags_x1 (b, main_is_l3);
+
+ /* next */
+ n_left -= 1;
+ etype += 1;
+ tag += 1;
+ dmac += 4;
+ from += 1;
+ }
+
+ if (dmac_check)
+ {
+ u64 mask = clib_net_to_host_u64 (0xFFFFFFFFFFFF0000);
+ u64 igbit = clib_net_to_host_u64 (0x0100000000000000);
+ u64 hwaddr = (*(u64 *) hi->hw_address) & mask;
+ u64 *dmac = dmacs;
+ u8 *dmac_bad = dmacs_bad;
+
+ n_left = n_packets;
+
+#ifdef CLIB_HAVE_VEC256
+ u64x4 igbit4 = u64x4_splat (igbit);
+ u64x4 mask4 = u64x4_splat (mask);
+ u64x4 hwaddr4 = u64x4_splat (hwaddr);
+ while (n_left >= 0)
+ {
+ u64x4 r0, r1;
+ r0 = u64x4_load_unaligned (dmac + 0) & mask4;
+ r1 = u64x4_load_unaligned (dmac + 4) & mask4;
+
+ r0 = (r0 != hwaddr4) & ((r0 & igbit4) == 0);
+ r1 = (r1 != hwaddr4) & ((r1 & igbit4) == 0);
+
+ *(u32 *) (dmac_bad + 0) = u8x32_msb_mask ((u8x32) (r0));
+ *(u32 *) (dmac_bad + 4) = u8x32_msb_mask ((u8x32) (r1));
+
+ /* next */
+ dmac += 8;
+ dmac_bad += 8;
+ n_left -= 8;
+ }
+#else
+ while (n_left > 0)
+ {
+ u64 r0, r1, r2, r3;
+ r0 = dmac[0] & mask;
+ r1 = dmac[1] & mask;
+ r2 = dmac[2] & mask;
+ r3 = dmac[3] & mask;
+
+ r0 = (r0 != hwaddr) && ((r0 & igbit) == 0);
+ r1 = (r1 != hwaddr) && ((r1 & igbit) == 0);
+ r2 = (r2 != hwaddr) && ((r2 & igbit) == 0);
+ r3 = (r3 != hwaddr) && ((r3 & igbit) == 0);
+
+ dmac_bad[0] = r0;
+ dmac_bad[1] = r1;
+ dmac_bad[2] = r2;
+ dmac_bad[3] = r3;
+
+ /* next */
+ dmac += 4;
+ dmac_bad += 4;
+ n_left -= 4;
+ }
+#endif
+ }
+
+ next_ip4 = em->l3_next.input_next_ip4;
+ next_ip6 = em->l3_next.input_next_ip6;
+ next_mpls = em->l3_next.input_next_mpls;
+ next_l2 = em->l2_next;
+
+ if (next_ip4 == ETHERNET_INPUT_NEXT_IP4_INPUT && ip4_cksum_ok)
+ next_ip4 = ETHERNET_INPUT_NEXT_IP4_INPUT_NCS;
+
+#ifdef CLIB_HAVE_VEC256
+ u16x16 et16_ip4 = u16x16_splat (et_ip4);
+ u16x16 et16_ip6 = u16x16_splat (et_ip6);
+ u16x16 et16_mpls = u16x16_splat (et_mpls);
+ u16x16 et16_vlan = u16x16_splat (et_vlan);
+ u16x16 et16_dot1ad = u16x16_splat (et_dot1ad);
+ u16x16 next16_ip4 = u16x16_splat (next_ip4);
+ u16x16 next16_ip6 = u16x16_splat (next_ip6);
+ u16x16 next16_mpls = u16x16_splat (next_mpls);
+ u16x16 next16_l2 = u16x16_splat (next_l2);
+ u16x16 zero = { 0 };
+ u16x16 stairs = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
+#endif
+
+ etype = etypes;
+ n_left = n_packets;
+ next = nexts;
+ n_slowpath = 0;
+ i = 0;
+
+ /* fastpath - in l3 mode hadles ip4, ip6 and mpls packets, other packets
+ are considered as slowpath, in l2 mode all untagged packets are
+ considered as fastpath */
+ while (n_left > 0)
+ {
+#ifdef CLIB_HAVE_VEC256
+ if (n_left >= 16)
+ {
+ u16x16 r = zero;
+ u16x16 e16 = u16x16_load_unaligned (etype);
+ if (main_is_l3)
+ {
+ r += (e16 == et16_ip4) & next16_ip4;
+ r += (e16 == et16_ip6) & next16_ip6;
+ r += (e16 == et16_mpls) & next16_mpls;
+ }
+ else
+ r = ((e16 != et16_vlan) & (e16 != et16_dot1ad)) & next16_l2;
+ u16x16_store_unaligned (r, next);
+
+ if (!u16x16_is_all_zero (r == zero))
+ {
+ if (u16x16_is_all_zero (r))
+ {
+ u16x16_store_unaligned (u16x16_splat (i) + stairs,
+ slowpath_indices + n_slowpath);
+ n_slowpath += 16;
+ }
+ else
+ {
+ for (int j = 0; j < 16; j++)
+ if (next[j] == 0)
+ slowpath_indices[n_slowpath++] = i + j;
+ }
+ }
+
+ etype += 16;
+ next += 16;
+ n_left -= 16;
+ i += 16;
+ continue;
+ }
+#endif
+ if (main_is_l3 && etype[0] == et_ip4)
+ next[0] = next_ip4;
+ else if (main_is_l3 && etype[0] == et_ip6)
+ next[0] = next_ip6;
+ else if (main_is_l3 && etype[0] == et_mpls)
+ next[0] = next_mpls;
+ else if (main_is_l3 == 0 &&
+ etype[0] != et_vlan && etype[0] != et_dot1ad)
+ next[0] = next_l2;
+ else
+ {
+ next[0] = 0;
+ slowpath_indices[n_slowpath++] = i;
+ }
+
+ etype += 1;
+ next += 1;
+ n_left -= 1;
+ i += 1;
+ }
+
+ if (n_slowpath)
+ {
+ vnet_main_t *vnm = vnet_get_main ();
+ n_left = n_slowpath;
+ u16 *si = slowpath_indices;
+ u32 last_unknown_etype = ~0;
+ u32 last_unknown_next = ~0;
+ eth_input_tag_lookup_t dot1ad_lookup, dot1q_lookup = {
+ .mask = -1LL,
+ .tag = tags[si[0]] ^ -1LL,
+ .sw_if_index = ~0
+ };
+
+ clib_memcpy_fast (&dot1ad_lookup, &dot1q_lookup, sizeof (dot1q_lookup));
+
+ while (n_left)
+ {
+ i = si[0];
+ u16 etype = etypes[i];
+
+ if (etype == et_vlan)
+ {
+ vlib_buffer_t *b = vlib_get_buffer (vm, buffer_indices[i]);
+ eth_input_tag_lookup (vm, vnm, node, hi, tags[i], nexts + i, b,
+ &dot1q_lookup, dmacs_bad[i], 0,
+ main_is_l3, dmac_check);
+
+ }
+ else if (etype == et_dot1ad)
+ {
+ vlib_buffer_t *b = vlib_get_buffer (vm, buffer_indices[i]);
+ eth_input_tag_lookup (vm, vnm, node, hi, tags[i], nexts + i, b,
+ &dot1ad_lookup, dmacs_bad[i], 1,
+ main_is_l3, dmac_check);
+ }
+ else
+ {
+ /* untagged packet with not well known etyertype */
+ if (last_unknown_etype != etype)
+ {
+ last_unknown_etype = etype;
+ etype = clib_host_to_net_u16 (etype);
+ last_unknown_next = eth_input_next_by_type (etype);
+ }
+ if (dmac_check && main_is_l3 && dmacs_bad[i])
+ {
+ vlib_buffer_t *b = vlib_get_buffer (vm, buffer_indices[i]);
+ b->error = node->errors[ETHERNET_ERROR_L3_MAC_MISMATCH];
+ nexts[i] = ETHERNET_INPUT_NEXT_PUNT;
+ }
+ else
+ nexts[i] = last_unknown_next;
+ }
+
+ /* next */
+ n_left--;
+ si++;
+ }
+
+ eth_input_update_if_counters (vm, vnm, &dot1q_lookup);
+ eth_input_update_if_counters (vm, vnm, &dot1ad_lookup);
+ }
+
+ vlib_buffer_enqueue_to_next (vm, node, buffer_indices, nexts, n_packets);
+}
+
+static_always_inline void
+eth_input_single_int (vlib_main_t * vm, vlib_node_runtime_t * node,
+ vnet_hw_interface_t * hi, u32 * from, u32 n_pkts,
+ int ip4_cksum_ok)
+{
+ ethernet_main_t *em = ðernet_main;
+ ethernet_interface_t *ei;
+ ei = pool_elt_at_index (em->interfaces, hi->hw_instance);
+ main_intf_t *intf0 = vec_elt_at_index (em->main_intfs, hi->hw_if_index);
+ subint_config_t *subint0 = &intf0->untagged_subint;
+
+ int main_is_l3 = (subint0->flags & SUBINT_CONFIG_L2) == 0;
+ int promisc = (ei->flags & ETHERNET_INTERFACE_FLAG_ACCEPT_ALL) != 0;
+
+ if (main_is_l3)
+ {
+ /* main interface is L3, we dont expect tagged packets and interface
+ is not in promisc node, so we dont't need to check DMAC */
+ int is_l3 = 1;
+
+ if (promisc == 0)
+ eth_input_process_frame (vm, node, hi, from, n_pkts, is_l3,
+ ip4_cksum_ok, 0);
+ else
+ /* subinterfaces and promisc mode so DMAC check is needed */
+ eth_input_process_frame (vm, node, hi, from, n_pkts, is_l3,
+ ip4_cksum_ok, 1);
+ return;
+ }
+ else
+ {
+ /* untagged packets are treated as L2 */
+ int is_l3 = 0;
+ eth_input_process_frame (vm, node, hi, from, n_pkts, is_l3,
+ ip4_cksum_ok, 1);
+ return;
+ }
+}
+
+static_always_inline void
+ethernet_input_trace (vlib_main_t * vm, vlib_node_runtime_t * node,
+ vlib_frame_t * from_frame)
+{
+ u32 *from, n_left;
+ if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
+ {
+ from = vlib_frame_vector_args (from_frame);
+ n_left = from_frame->n_vectors;
+
+ while (n_left)
+ {
+ ethernet_input_trace_t *t0;
+ vlib_buffer_t *b0 = vlib_get_buffer (vm, from[0]);
+
+ if (b0->flags & VLIB_BUFFER_IS_TRACED)
+ {
+ t0 = vlib_add_trace (vm, node, b0,
+ sizeof (ethernet_input_trace_t));
+ clib_memcpy_fast (t0->packet_data, b0->data + b0->current_data,
+ sizeof (t0->packet_data));
+ t0->frame_flags = from_frame->flags;
+ clib_memcpy_fast (&t0->frame_data,
+ vlib_frame_scalar_args (from_frame),
+ sizeof (ethernet_input_frame_t));
+ }
+ from += 1;
+ n_left -= 1;
+ }
+ }
+
+ /* rx pcap capture if enabled */
+ if (PREDICT_FALSE (vlib_global_main.pcap[VLIB_RX].pcap_enable))
+ {
+ u32 bi0;
+
+ from = vlib_frame_vector_args (from_frame);
+ n_left = from_frame->n_vectors;
+ while (n_left > 0)
+ {
+ vlib_buffer_t *b0;
+ bi0 = from[0];
+ from++;
+ b0 = vlib_get_buffer (vm, bi0);
+
+ if (vlib_global_main.pcap[VLIB_RX].pcap_sw_if_index == 0 ||
+ vlib_global_main.pcap[VLIB_RX].pcap_sw_if_index
+ == vnet_buffer (b0)->sw_if_index[VLIB_RX])
+ {
+ pcap_add_buffer (&vlib_global_main.pcap[VLIB_RX].pcap_main, vm,
+ bi0, 512);
+ }
+ n_left--;
+ }
+ }
+}
+
+static_always_inline void