Use SSE instructions for matching tagged frames by ethertype 38/2938/4
authorDamjan Marion <damarion@cisco.com>
Mon, 19 Sep 2016 09:32:03 +0000 (11:32 +0200)
committerJohn Lo <loj@cisco.com>
Tue, 20 Sep 2016 17:37:02 +0000 (17:37 +0000)
This change improves performance in l2 scenarios for about 1-2%.

Change-Id: Ie35ed8ad70a3b95dd78c0d158c485a10dabaf0eb
Signed-off-by: Damjan Marion <damarion@cisco.com>
vnet/vnet/ethernet/ethernet.h
vnet/vnet/ethernet/node.c
vnet/vnet/l2/l2_input.h

index b5c4ee9..8a1369c 100644 (file)
@@ -60,6 +60,33 @@ ethernet_mac_address_is_multicast_u64 (u64 a)
   return (a & (1ULL << (5 * 8))) != 0;
 }
 
+static_always_inline int
+ethernet_frame_is_tagged (u16 type)
+{
+#if __SSE4_2__
+  const __m128i ethertype_mask = _mm_set_epi16 (ETHERNET_TYPE_VLAN,
+                                               ETHERNET_TYPE_DOT1AD,
+                                               ETHERNET_TYPE_VLAN_9100,
+                                               ETHERNET_TYPE_VLAN_9200,
+                                               /* duplicate last one to
+                                                  fill register */
+                                               ETHERNET_TYPE_VLAN_9200,
+                                               ETHERNET_TYPE_VLAN_9200,
+                                               ETHERNET_TYPE_VLAN_9200,
+                                               ETHERNET_TYPE_VLAN_9200);
+
+  __m128i r = _mm_set1_epi16 (type);
+  r = _mm_cmpeq_epi16 (ethertype_mask, r);
+  return !_mm_test_all_zeros (r, r);
+#else
+  if ((type == ETHERNET_TYPE_VLAN) ||
+      (type == ETHERNET_TYPE_DOT1AD) ||
+      (type == ETHERNET_TYPE_VLAN_9100) || (type == ETHERNET_TYPE_VLAN_9200))
+    return 1;
+#endif
+  return 0;
+}
+
 /* Max. sized ethernet/vlan header for parsing. */
 typedef struct
 {
index a8a4fcd..aaa0434 100644 (file)
@@ -130,10 +130,7 @@ parse_header (ethernet_input_variant_t variant,
   vlan_count = 0;
 
   // check for vlan encaps
-  if ((*type == ETHERNET_TYPE_VLAN) ||
-      (*type == ETHERNET_TYPE_DOT1AD) ||
-      (*type == ETHERNET_TYPE_VLAN_9100) ||
-      (*type == ETHERNET_TYPE_VLAN_9200))
+  if (ethernet_frame_is_tagged (*type))
     {
       ethernet_vlan_header_t *h0;
       u16 tag;
index 48597ce..c1c9eb1 100644 (file)
@@ -244,10 +244,7 @@ vnet_update_l2_len (vlib_buffer_t * b)
    */
   vnet_buffer (b)->l2.l2_len = sizeof (ethernet_header_t);
   ethertype = clib_net_to_host_u16 (eth->type);
-  if ((ethertype == ETHERNET_TYPE_VLAN) ||
-      (ethertype == ETHERNET_TYPE_DOT1AD) ||
-      (ethertype == ETHERNET_TYPE_VLAN_9100) ||
-      (ethertype == ETHERNET_TYPE_VLAN_9200))
+  if (ethernet_frame_is_tagged (ethertype))
     {
       ethernet_vlan_header_t *vlan;
       vnet_buffer (b)->l2.l2_len += sizeof (*vlan);