hash: add support for hashing infra 40/32940/28
authorMohsin Kazmi <sykazmi@cisco.com>
Wed, 30 Jun 2021 18:26:25 +0000 (18:26 +0000)
committerDamjan Marion <dmarion@me.com>
Mon, 20 Sep 2021 11:11:02 +0000 (11:11 +0000)
Type: feature

Signed-off-by: Mohsin Kazmi <sykazmi@cisco.com>
Change-Id: I3652ae275385d9b1eb1b11f418e3a7e5fef2f556

MAINTAINERS
src/plugins/unittest/CMakeLists.txt
src/plugins/unittest/hash_test.c [new file with mode: 0644]
src/vnet/CMakeLists.txt
src/vnet/hash/FEATURE.yaml [new file with mode: 0644]
src/vnet/hash/cli.c [new file with mode: 0644]
src/vnet/hash/crc32_5tuple.c [new file with mode: 0644]
src/vnet/hash/hash.c [new file with mode: 0644]
src/vnet/hash/hash.h [new file with mode: 0644]

index 92b6c6d..5a5ee84 100644 (file)
@@ -361,6 +361,12 @@ I: flow
 M:     Damjan Marion <damarion@cisco.com>
 F:     src/vnet/flow/
 
+VNET Hash
+I:     hash
+M:     Mohsin Kazmi <sykazmi@cisco.com>
+M:     Damjan Marion <damarion@cisco.com>
+F:     src/vnet/hash/
+
 VPP Main App
 I:     vpp
 M:     Dave Barach <vpp@barachs.net>
index baf3a3c..e39a240 100644 (file)
@@ -32,6 +32,7 @@ add_vpp_plugin(unittest
   crypto/sha.c
   crypto_test.c
   fib_test.c
+  hash_test.c
   interface_test.c
   ipsec_test.c
   llist_test.c
diff --git a/src/plugins/unittest/hash_test.c b/src/plugins/unittest/hash_test.c
new file mode 100644 (file)
index 0000000..3b0a3cf
--- /dev/null
@@ -0,0 +1,331 @@
+/*
+ * SPDX-License-Identifier: Apache-2.0
+ * Copyright(c) 2021 Cisco Systems, Inc.
+ */
+
+#include <vlib/vlib.h>
+#include <vppinfra/time.h>
+#include <vppinfra/cache.h>
+#include <vppinfra/error.h>
+#include <vnet/hash/hash.h>
+#include <vnet/ethernet/ethernet.h>
+
+#define HASH_TEST_DATA_SIZE 2048
+
+typedef struct _hash_test_data
+{
+  const char *name;
+  const char *description;
+  u8 *data;
+  u32 data_size;
+  vnet_hash_fn_type_t ftype;
+  struct _hash_test_data *next;
+} hash_test_data_t;
+
+typedef struct
+{
+  int verbose;
+
+  char *hash_name;
+  u32 warmup_rounds;
+  u32 rounds;
+  u32 n_buffers;
+
+  hash_test_data_t *hash_test_data;
+} hash_test_main_t;
+
+hash_test_main_t hash_test_main;
+
+#define HASH_TEST_REGISTER_DATA(x, ...)                                       \
+  __VA_ARGS__ hash_test_data_t __hash_test_data_##x;                          \
+  static void __clib_constructor __hash_test_data_fn_##x (void)               \
+  {                                                                           \
+    hash_test_main_t *htm = &hash_test_main;                                  \
+    __hash_test_data_##x.next = htm->hash_test_data;                          \
+    htm->hash_test_data = &__hash_test_data_##x;                              \
+  }                                                                           \
+  __VA_ARGS__ hash_test_data_t __hash_test_data_##x
+
+// qinq
+u8 eth_qinq_ipv4_tcp_data[72] = {
+  0x02, 0xfe, 0x39, 0xe5, 0x09, 0x8f, 0x02, 0xfe, 0x2d, 0x18, 0x63, 0x18,
+  0x88, 0xa8, 0x03, 0xe8, 0x81, 0x00, 0x03, 0xe8, 0x08, 0x00, 0x45, 0x00,
+  0x05, 0xdc, 0xdb, 0x42, 0x40, 0x00, 0x40, 0x06, 0xc4, 0x85, 0xc0, 0xa8,
+  0x0a, 0x02, 0xc0, 0xa8, 0x0a, 0x01, 0xd8, 0xde, 0x14, 0x51, 0x34, 0x93,
+  0xa8, 0x1b, 0x7b, 0xef, 0x2e, 0x7e, 0x80, 0x10, 0x00, 0xe5, 0xc7, 0x03,
+  0x00, 0x00, 0x01, 0x01, 0x08, 0x0a, 0xce, 0xaa, 0x00, 0x2f, 0xf2, 0xc3
+};
+
+HASH_TEST_REGISTER_DATA (eth_qinq_ipv4_tcp, static) = {
+  .name = "eth-qinq-ipv4-tcp",
+  .description = "Ethernet QinQ IPv4 TCP",
+  .data = eth_qinq_ipv4_tcp_data,
+  .data_size = sizeof (eth_qinq_ipv4_tcp_data),
+  .ftype = VNET_HASH_FN_TYPE_ETHERNET,
+};
+
+// vlan
+u8 eth_vlan_ipv4_tcp_data[68] = {
+  0x02, 0xfe, 0x39, 0xe5, 0x09, 0x8f, 0x02, 0xfe, 0x2d, 0x18, 0x63, 0x18,
+  0x81, 0x00, 0x03, 0xe8, 0x08, 0x00, 0x45, 0x00, 0x05, 0xdc, 0xdb, 0x42,
+  0x40, 0x00, 0x40, 0x06, 0xc4, 0x85, 0xc0, 0xa8, 0x0a, 0x02, 0xc0, 0xa8,
+  0x0a, 0x01, 0xd8, 0xde, 0x14, 0x51, 0x34, 0x93, 0xa8, 0x1b, 0x7b, 0xef,
+  0x2e, 0x7e, 0x80, 0x10, 0x00, 0xe5, 0xc7, 0x03, 0x00, 0x00, 0x01, 0x01,
+  0x08, 0x0a, 0xce, 0xaa, 0x00, 0x2f, 0xf2, 0xc3
+};
+
+HASH_TEST_REGISTER_DATA (eth_vlan_ipv4_tcp, static) = {
+  .name = "eth-vlan-ipv4-tcp",
+  .description = "Ethernet Vlan IPv4 TCP",
+  .data = eth_vlan_ipv4_tcp_data,
+  .data_size = sizeof (eth_vlan_ipv4_tcp_data),
+  .ftype = VNET_HASH_FN_TYPE_ETHERNET,
+};
+
+// ethernet
+u8 eth_ipv4_tcp_data[64] = {
+  0x02, 0xfe, 0x39, 0xe5, 0x09, 0x8f, 0x02, 0xfe, 0x2d, 0x18, 0x63, 0x18, 0x08,
+  0x00, 0x45, 0x00, 0x05, 0xdc, 0xdb, 0x42, 0x40, 0x00, 0x40, 0x06, 0xc4, 0x85,
+  0xc0, 0xa8, 0x0a, 0x02, 0xc0, 0xa8, 0x0a, 0x01, 0xd8, 0xde, 0x14, 0x51, 0x34,
+  0x93, 0xa8, 0x1b, 0x7b, 0xef, 0x2e, 0x7e, 0x80, 0x10, 0x00, 0xe5, 0xc7, 0x03,
+  0x00, 0x00, 0x01, 0x01, 0x08, 0x0a, 0xce, 0xaa, 0x00, 0x2f, 0xf2, 0xc3
+};
+
+HASH_TEST_REGISTER_DATA (eth_ipv4_tcp, static) = {
+  .name = "eth-ipv4-tcp",
+  .description = "Ethernet IPv4 TCP",
+  .data = eth_ipv4_tcp_data,
+  .data_size = sizeof (eth_ipv4_tcp_data),
+  .ftype = VNET_HASH_FN_TYPE_ETHERNET,
+};
+
+// udp
+u8 eth_ipv4_udp_data[42] = { 0x62, 0x36, 0xbe, 0xff, 0x91, 0x20, 0x5e,
+                            0x2c, 0xaf, 0x2e, 0x1e, 0x51, 0x08, 0x00,
+                            0x45, 0x00, 0x05, 0xc4, 0x9d, 0xc3, 0x40,
+                            0x00, 0x33, 0x11, 0x49, 0x61, 0x3e, 0xd2,
+                            0x12, 0x28, 0x0a, 0x09, 0x00, 0x02, 0x14,
+                            0x58, 0xc0, 0xd8, 0x05, 0xb0, 0x75, 0xbd };
+
+HASH_TEST_REGISTER_DATA (eth_ipv4_udp, static) = {
+  .name = "eth-ipv4-udp",
+  .description = "Ethernet IPv4 UDP",
+  .data = eth_ipv4_udp_data,
+  .data_size = sizeof (eth_ipv4_udp_data),
+  .ftype = VNET_HASH_FN_TYPE_ETHERNET,
+};
+
+// ipv4
+u8 ipv4_tcp_data[50] = { 0x45, 0x00, 0x05, 0xdc, 0xdb, 0x42, 0x40, 0x00, 0x40,
+                        0x06, 0xc4, 0x85, 0xc0, 0xa8, 0x0a, 0x02, 0xc0, 0xa8,
+                        0x0a, 0x01, 0xd8, 0xde, 0x14, 0x51, 0x34, 0x93, 0xa8,
+                        0x1b, 0x7b, 0xef, 0x2e, 0x7e, 0x80, 0x10, 0x00, 0xe5,
+                        0xc7, 0x03, 0x00, 0x00, 0x01, 0x01, 0x08, 0x0a, 0xce,
+                        0xaa, 0x00, 0x2f, 0xf2, 0xc3 };
+
+HASH_TEST_REGISTER_DATA (ipv4_tcp, static) = {
+  .name = "ipv4-tcp",
+  .description = "IPv4 TCP",
+  .data = ipv4_tcp_data,
+  .data_size = sizeof (ipv4_tcp_data),
+  .ftype = VNET_HASH_FN_TYPE_IP,
+};
+
+u8 ipv4_icmp_data[84] = {
+  0x45, 0x00, 0x00, 0x54, 0xb7, 0xe6, 0x40, 0x00, 0x40, 0x01, 0xed, 0x6e,
+  0xc0, 0xa8, 0x0a, 0x01, 0xc0, 0xa8, 0x0a, 0x02, 0x08, 0x00, 0xc7, 0x84,
+  0x00, 0x16, 0x00, 0x92, 0xfd, 0xdb, 0xd9, 0x60, 0x00, 0x00, 0x00, 0x00,
+  0x91, 0xc3, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x11, 0x12, 0x13,
+  0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
+  0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b,
+  0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
+
+};
+
+HASH_TEST_REGISTER_DATA (ipv4_icmp, static) = {
+  .name = "ipv4-icmp",
+  .description = "IPv4 ICMP",
+  .data = ipv4_icmp_data,
+  .data_size = sizeof (ipv4_icmp_data),
+  .ftype = VNET_HASH_FN_TYPE_IP,
+};
+
+// ip6
+u8 ipv6_icmp6_data[104] = {
+  0x60, 0x0d, 0xf4, 0x97, 0x00, 0x40, 0x3a, 0x40, 0xfd, 0x01, 0x00, 0x00, 0x00,
+  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0xfd, 0x01,
+  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,
+  0x01, 0x80, 0x00, 0x10, 0x84, 0xb1, 0x25, 0x00, 0x01, 0x22, 0x57, 0xf0, 0x60,
+  0x00, 0x00, 0x00, 0x00, 0xcb, 0x4a, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,
+  0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d,
+  0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a,
+  0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
+};
+
+HASH_TEST_REGISTER_DATA (ipv6_icmp6, static) = {
+  .name = "ipv6-icmp6",
+  .description = "IPv6 ICMP6",
+  .data = ipv6_icmp6_data,
+  .data_size = sizeof (ipv6_icmp6_data),
+  .ftype = VNET_HASH_FN_TYPE_IP,
+};
+
+void
+fill_buffers (vlib_main_t *vm, u32 *buffer_indices, u8 *data, u32 data_size,
+             u32 n_buffers)
+{
+  int i, j;
+  u64 seed = clib_cpu_time_now ();
+  for (i = 0; i < n_buffers; i++)
+    {
+      vlib_buffer_t *b = vlib_get_buffer (vm, buffer_indices[i]);
+      clib_memcpy_fast (b->data, data, data_size);
+      b->current_data = 0;
+      for (j = data_size; j < HASH_TEST_DATA_SIZE; j += 8)
+       *(u64 *) (b->data + j) = 1 + random_u64 (&seed);
+      b->current_length = HASH_TEST_DATA_SIZE;
+    }
+}
+
+static clib_error_t *
+test_hash_perf (vlib_main_t *vm, hash_test_main_t *htm)
+{
+  clib_error_t *err = 0;
+  u32 n_buffers, n_alloc = 0, warmup_rounds, rounds;
+  u32 *buffer_indices = 0;
+  u64 t0[5], t1[5];
+  vnet_hash_fn_t hf;
+  hash_test_data_t *hash_test_data = htm->hash_test_data;
+  void **p = 0;
+  int i, j;
+
+  rounds = htm->rounds ? htm->rounds : 100;
+  n_buffers = htm->n_buffers ? htm->n_buffers : 256;
+  warmup_rounds = htm->warmup_rounds ? htm->warmup_rounds : 100;
+
+  vec_validate_aligned (p, n_buffers - 1, CLIB_CACHE_LINE_BYTES);
+  vec_validate_aligned (buffer_indices, n_buffers - 1, CLIB_CACHE_LINE_BYTES);
+  n_alloc = vlib_buffer_alloc (vm, buffer_indices, n_buffers);
+  if (n_alloc != n_buffers)
+    {
+      err = clib_error_return (0, "buffer alloc failure");
+      goto done;
+    }
+
+  vlib_cli_output (vm,
+                  "%s: n_buffers %u rounds %u "
+                  "warmup-rounds %u",
+                  htm->hash_name, n_buffers, rounds, warmup_rounds);
+  vlib_cli_output (vm, "   cpu-freq %.2f GHz",
+                  (f64) vm->clib_time.clocks_per_second * 1e-9);
+
+  while (hash_test_data)
+    {
+      fill_buffers (vm, buffer_indices, hash_test_data->data,
+                   hash_test_data->data_size, n_buffers);
+
+      for (i = 0; i < n_buffers; i++)
+       {
+         vlib_buffer_t *b = vlib_get_buffer (vm, buffer_indices[i]);
+         p[i] = vlib_buffer_get_current (b);
+       }
+
+      hf =
+       vnet_hash_function_from_name (htm->hash_name, hash_test_data->ftype);
+
+      if (!hf)
+       {
+         err = clib_error_return (0, "wrong hash name");
+         goto done;
+       }
+
+      for (i = 0; i < 5; i++)
+       {
+         u32 h[n_buffers];
+         for (j = 0; j < warmup_rounds; j++)
+           {
+             hf (p, h, n_buffers);
+           }
+
+         t0[i] = clib_cpu_time_now ();
+         for (j = 0; j < rounds; j++)
+           hf (p, h, n_buffers);
+         t1[i] = clib_cpu_time_now ();
+       }
+
+      vlib_cli_output (
+       vm, "===========================================================");
+      vlib_cli_output (vm, " Test: %s", hash_test_data->description);
+      vlib_cli_output (
+       vm, "===========================================================");
+      for (i = 0; i < 5; i++)
+       {
+         f64 tpp1 = (f64) (t1[i] - t0[i]) / (n_buffers * rounds);
+         f64 Mpps1 = vm->clib_time.clocks_per_second * 1e-6 / tpp1;
+
+         vlib_cli_output (vm, "%-2u: %.03f ticks/packet, %.02f Mpps\n", i + 1,
+                          tpp1, Mpps1);
+       }
+      hash_test_data = hash_test_data->next;
+    }
+
+done:
+  if (n_alloc)
+    vlib_buffer_free (vm, buffer_indices, n_alloc);
+
+  vec_free (p);
+  vec_free (buffer_indices);
+  return err;
+}
+
+static clib_error_t *
+test_hash_command_fn (vlib_main_t *vm, unformat_input_t *input,
+                     vlib_cli_command_t *cmd)
+{
+  hash_test_main_t *tm = &hash_test_main;
+  clib_error_t *err = 0;
+
+  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
+    {
+      if (unformat (input, "verbose"))
+       tm->verbose = 1;
+      else if (unformat (input, "detail"))
+       tm->verbose = 2;
+      else if (unformat (input, "perf %s", &tm->hash_name))
+       ;
+      else if (unformat (input, "buffers %u", &tm->n_buffers))
+       ;
+      else if (unformat (input, "rounds %u", &tm->rounds))
+       ;
+      else if (unformat (input, "warmup-rounds %u", &tm->warmup_rounds))
+       ;
+      else
+       {
+         err = clib_error_return (0, "unknown input '%U'",
+                                  format_unformat_error, input);
+         goto error;
+       }
+    }
+
+  err = test_hash_perf (vm, tm);
+
+error:
+  vec_free (tm->hash_name);
+
+  return err;
+}
+
+VLIB_CLI_COMMAND (test_hash_command, static) = {
+  .path = "test hash",
+  .short_help = "test hash [perf <hash-name>] [buffers <n>] [rounds <n>] "
+               "[warmup-rounds <n>]",
+  .function = test_hash_command_fn,
+};
+
+static clib_error_t *
+hash_test_init (vlib_main_t *vm)
+{
+  return (0);
+}
+
+VLIB_INIT_FUNCTION (hash_test_init);
index 78a2ba7..6813330 100644 (file)
@@ -881,6 +881,19 @@ list(APPEND VNET_HEADERS
 
 list(APPEND VNET_API_FILES ipfix-export/ipfix_export.api)
 
+##############################################################################
+# HASH
+##############################################################################
+list(APPEND VNET_SOURCES
+  hash/hash.c
+  hash/cli.c
+  hash/crc32_5tuple.c
+)
+
+list(APPEND VNET_HEADERS
+  hash/hash.h
+)
+
 ##############################################################################
 # GSO
 ##############################################################################
diff --git a/src/vnet/hash/FEATURE.yaml b/src/vnet/hash/FEATURE.yaml
new file mode 100644 (file)
index 0000000..1e3d23e
--- /dev/null
@@ -0,0 +1,9 @@
+---
+name: Hash infrastructure
+maintainer: Mohsin Kazmi <sykazmi@cisco.com>, Damjan Marion <damarion@cisco.com>
+features:
+  - Ethernet
+  - IP
+description: "Hash infrastructure"
+state: development
+properties: [CLI]
diff --git a/src/vnet/hash/cli.c b/src/vnet/hash/cli.c
new file mode 100644 (file)
index 0000000..47d33b9
--- /dev/null
@@ -0,0 +1,33 @@
+/*
+ * SPDX-License-Identifier: Apache-2.0
+ * Copyright(c) 2021 Cisco Systems, Inc.
+ */
+
+#include <vnet/vnet.h>
+#include <vnet/hash/hash.h>
+
+static clib_error_t *
+show_hash (vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
+{
+  clib_error_t *error = 0;
+  vnet_hash_main_t *hm = &vnet_hash_main;
+  vnet_hash_function_registration_t *hash;
+
+  hash = hm->hash_registrations;
+
+  vlib_cli_output (vm, "%-25s%-8s%s", "Name", "Prio", "Description");
+  while (hash)
+    {
+      vlib_cli_output (vm, "%-25s%-8u%s", hash->name, hash->priority,
+                      hash->description);
+      hash = hash->next;
+    }
+
+  return (error);
+}
+
+VLIB_CLI_COMMAND (cmd_show_hash, static) = {
+  .path = "show hash",
+  .short_help = "show hash",
+  .function = show_hash,
+};
diff --git a/src/vnet/hash/crc32_5tuple.c b/src/vnet/hash/crc32_5tuple.c
new file mode 100644 (file)
index 0000000..69ee63a
--- /dev/null
@@ -0,0 +1,203 @@
+/*
+ * SPDX-License-Identifier: Apache-2.0
+ * Copyright(c) 2021 Cisco Systems, Inc.
+ */
+
+#include <vnet/vnet.h>
+#include <vnet/ethernet/ethernet.h>
+#include <vnet/ip/ip4_packet.h>
+#include <vnet/ip/ip6_packet.h>
+#include <vnet/ip/ip46_address.h>
+#include <vnet/udp/udp_packet.h>
+#include <vnet/hash/hash.h>
+#include <vppinfra/crc32.h>
+
+typedef union
+{
+  struct
+  {
+    ip46_address_t src_address;
+    ip46_address_t dst_address;
+    union
+    {
+      struct
+      {
+       u16 src_port;
+       u16 dst_port;
+      };
+      u32 l4_hdr;
+    };
+  };
+  u8 as_u8[36];
+} crc32c_5tuple_key_t;
+
+static const u8 l4_mask_bits[256] = {
+  [IP_PROTOCOL_ICMP] = 16,     [IP_PROTOCOL_IGMP] = 8,
+  [IP_PROTOCOL_TCP] = 32,      [IP_PROTOCOL_UDP] = 32,
+  [IP_PROTOCOL_IPSEC_ESP] = 32, [IP_PROTOCOL_IPSEC_AH] = 32,
+  [IP_PROTOCOL_ICMP6] = 16,
+};
+
+static_always_inline void
+compute_ip6_key (ip6_header_t *ip, crc32c_5tuple_key_t *k)
+{
+  u8 pr;
+
+  /* copy 32 bytes of ip6 src and dst addresses into hash_key_t */
+  clib_memcpy_fast ((u8 *) k, (u8 *) ip + 8, sizeof (ip6_address_t) * 2);
+  pr = ip->protocol;
+  /* write l4 header */
+  k->l4_hdr = *(u32 *) ip6_next_header (ip) & pow2_mask (l4_mask_bits[pr]);
+}
+
+static_always_inline void
+compute_ip4_key (ip4_header_t *ip, crc32c_5tuple_key_t *k)
+{
+  u8 pr;
+  u64 *key = (u64 *) k;
+  /* copy 8 bytes of ip src and dst addresses into hash_key_t */
+  key[0] = 0;
+  key[1] = 0;
+  key[2] = 0;
+  key[3] = *(u64 *) ((u8 *) ip + 12);
+  pr = ip->protocol;
+  /* write l4 header */
+  k->l4_hdr = *(u32 *) ip4_next_header (ip) & pow2_mask (l4_mask_bits[pr]);
+}
+static_always_inline void
+compute_ip_key (void *p, crc32c_5tuple_key_t *key)
+{
+  if ((((u8 *) p)[0] & 0xf0) == 0x40)
+    compute_ip4_key (p, key);
+  else if ((((u8 *) p)[0] & 0xf0) == 0x60)
+    compute_ip6_key (p, key);
+}
+
+void
+vnet_crc32c_5tuple_ip_func (void **p, u32 *hash, u32 n_packets)
+{
+  u32 n_left_from = n_packets;
+
+  while (n_left_from >= 8)
+    {
+      crc32c_5tuple_key_t key[4];
+
+      clib_prefetch_load (p[4]);
+      clib_prefetch_load (p[5]);
+      clib_prefetch_load (p[6]);
+      clib_prefetch_load (p[7]);
+
+      compute_ip_key (p[0], &key[0]);
+      compute_ip_key (p[1], &key[1]);
+      compute_ip_key (p[2], &key[2]);
+      compute_ip_key (p[3], &key[3]);
+
+      hash[0] = clib_crc32c (key[0].as_u8, sizeof (key[0]));
+      hash[1] = clib_crc32c (key[1].as_u8, sizeof (key[1]));
+      hash[2] = clib_crc32c (key[2].as_u8, sizeof (key[2]));
+      hash[3] = clib_crc32c (key[3].as_u8, sizeof (key[3]));
+
+      hash += 4;
+      n_left_from -= 4;
+      p += 4;
+    }
+
+  while (n_left_from > 0)
+    {
+      crc32c_5tuple_key_t key;
+
+      compute_ip_key (p[0], &key);
+
+      hash[0] = clib_crc32c (key.as_u8, sizeof (key));
+
+      hash += 1;
+      n_left_from -= 1;
+      p += 1;
+    }
+}
+
+static_always_inline void
+compute_ethernet_key (void *p, crc32c_5tuple_key_t *key)
+{
+  u16 ethertype = 0, l2hdr_sz = 0;
+
+  ethernet_header_t *eh = (ethernet_header_t *) p;
+  ethertype = clib_net_to_host_u16 (eh->type);
+  l2hdr_sz = sizeof (ethernet_header_t);
+
+  if (ethernet_frame_is_tagged (ethertype))
+    {
+      ethernet_vlan_header_t *vlan = (ethernet_vlan_header_t *) (eh + 1);
+
+      ethertype = clib_net_to_host_u16 (vlan->type);
+      l2hdr_sz += sizeof (*vlan);
+      while (ethernet_frame_is_tagged (ethertype))
+       {
+         vlan++;
+         ethertype = clib_net_to_host_u16 (vlan->type);
+         l2hdr_sz += sizeof (*vlan);
+       }
+    }
+
+  if (ethertype == ETHERNET_TYPE_IP4)
+    {
+      ip4_header_t *ip4 = (ip4_header_t *) (p + l2hdr_sz);
+      compute_ip4_key (ip4, key);
+    }
+  else if (ethertype == ETHERNET_TYPE_IP6)
+    {
+      ip6_header_t *ip6 = (ip6_header_t *) (p + l2hdr_sz);
+      compute_ip6_key (ip6, key);
+    }
+}
+
+void
+vnet_crc32c_5tuple_ethernet_func (void **p, u32 *hash, u32 n_packets)
+{
+  u32 n_left_from = n_packets;
+
+  while (n_left_from >= 8)
+    {
+      crc32c_5tuple_key_t key[4];
+
+      clib_prefetch_load (p[4]);
+      clib_prefetch_load (p[5]);
+      clib_prefetch_load (p[6]);
+      clib_prefetch_load (p[7]);
+
+      compute_ethernet_key (p[0], &key[0]);
+      compute_ethernet_key (p[1], &key[1]);
+      compute_ethernet_key (p[2], &key[2]);
+      compute_ethernet_key (p[3], &key[3]);
+
+      hash[0] = clib_crc32c (key[0].as_u8, sizeof (key[0]));
+      hash[1] = clib_crc32c (key[1].as_u8, sizeof (key[1]));
+      hash[2] = clib_crc32c (key[2].as_u8, sizeof (key[2]));
+      hash[3] = clib_crc32c (key[3].as_u8, sizeof (key[3]));
+
+      hash += 4;
+      n_left_from -= 4;
+      p += 4;
+    }
+
+  while (n_left_from > 0)
+    {
+      crc32c_5tuple_key_t key;
+
+      compute_ethernet_key (p[0], &key);
+
+      hash[0] = clib_crc32c (key.as_u8, sizeof (key));
+
+      hash += 1;
+      n_left_from -= 1;
+      p += 1;
+    }
+}
+
+VNET_REGISTER_HASH_FUNCTION (crc32c_5tuple, static) = {
+  .name = "crc32c-5tuple",
+  .description = "IPv4/IPv6 header and TCP/UDP ports",
+  .priority = 50,
+  .function[VNET_HASH_FN_TYPE_ETHERNET] = vnet_crc32c_5tuple_ethernet_func,
+  .function[VNET_HASH_FN_TYPE_IP] = vnet_crc32c_5tuple_ip_func,
+};
diff --git a/src/vnet/hash/hash.c b/src/vnet/hash/hash.c
new file mode 100644 (file)
index 0000000..31693c9
--- /dev/null
@@ -0,0 +1,76 @@
+/*
+ * SPDX-License-Identifier: Apache-2.0
+ * Copyright(c) 2021 Cisco Systems, Inc.
+ */
+
+#include <vlib/vlib.h>
+#include <vnet/vnet.h>
+#include <vnet/interface.h>
+#include <vnet/hash/hash.h>
+
+vnet_hash_main_t vnet_hash_main;
+
+u8 *
+format_vnet_hash (u8 *s, va_list *args)
+{
+  vnet_hash_function_registration_t *hash =
+    va_arg (*args, vnet_hash_function_registration_t *);
+
+  s = format (s, "[name: %s ", hash->name);
+  s = format (s, "priority: %u ", hash->priority);
+  s = format (s, "description: %s]", hash->description);
+  return s;
+}
+
+/**
+ * select hash func with highest priority
+ */
+vnet_hash_fn_t
+vnet_hash_default_function (vnet_hash_fn_type_t ftype)
+{
+  vnet_hash_function_registration_t *hash = vnet_hash_main.hash_registrations;
+  vnet_hash_function_registration_t *tmp_hash = hash;
+  while (hash)
+    {
+      if (hash->priority > tmp_hash->priority)
+       tmp_hash = hash;
+      hash = hash->next;
+    }
+  return tmp_hash->function[ftype];
+}
+
+vnet_hash_fn_t
+vnet_hash_function_from_name (const char *name, vnet_hash_fn_type_t ftype)
+{
+  vnet_hash_function_registration_t *hash = vnet_hash_main.hash_registrations;
+  while (hash)
+    {
+      if (strcmp (hash->name, name) == 0)
+       break;
+      hash = hash->next;
+    }
+  if (!hash)
+    return (0);
+  return hash->function[ftype];
+}
+
+vnet_hash_function_registration_t *
+vnet_hash_function_from_func (vnet_hash_fn_t fn, vnet_hash_fn_type_t ftype)
+{
+  vnet_hash_function_registration_t *hash = vnet_hash_main.hash_registrations;
+  while (hash)
+    {
+      if (hash->function[ftype] == fn)
+       break;
+      hash = hash->next;
+    }
+  return hash;
+}
+
+static clib_error_t *
+vnet_hash_init (vlib_main_t *vm)
+{
+  return (0);
+}
+
+VLIB_INIT_FUNCTION (vnet_hash_init);
diff --git a/src/vnet/hash/hash.h b/src/vnet/hash/hash.h
new file mode 100644 (file)
index 0000000..c1eb947
--- /dev/null
@@ -0,0 +1,59 @@
+/*
+ * SPDX-License-Identifier: Apache-2.0
+ * Copyright(c) 2021 Cisco Systems, Inc.
+ */
+
+#ifndef __VNET_HASH_H__
+#define __VNET_HASH_H__
+
+#include <vlib/vlib.h>
+
+#define foreach_vnet_hash_fn_types                                            \
+  _ (ETHERNET, 0, "hash-fn-ethernet")                                         \
+  _ (IP, 1, "hash-fn-ip")
+
+typedef enum
+{
+#define _(f, n, s) VNET_HASH_FN_TYPE_##f,
+  foreach_vnet_hash_fn_types
+#undef _
+    VNET_HASH_FN_TYPE_N,
+} vnet_hash_fn_type_t;
+
+typedef void (*vnet_hash_fn_t) (void **p, u32 *h, u32 n_packets);
+
+typedef struct vnet_hash_function_registration
+{
+  const char *name;
+  const char *description;
+  int priority;
+  vnet_hash_fn_t function[VNET_HASH_FN_TYPE_N];
+
+  struct vnet_hash_function_registration *next;
+} vnet_hash_function_registration_t;
+
+typedef struct
+{
+  vnet_hash_function_registration_t *hash_registrations;
+} vnet_hash_main_t;
+
+extern vnet_hash_main_t vnet_hash_main;
+
+#define VNET_REGISTER_HASH_FUNCTION(x, ...)                                   \
+  __VA_ARGS__ vnet_hash_function_registration_t __vnet_hash_function_##x;     \
+  static void __clib_constructor __vnet_hash_function_registration_##x (void) \
+  {                                                                           \
+    vnet_hash_main_t *hm = &vnet_hash_main;                                   \
+    __vnet_hash_function_##x.next = hm->hash_registrations;                   \
+    hm->hash_registrations = &__vnet_hash_function_##x;                       \
+  }                                                                           \
+  __VA_ARGS__ vnet_hash_function_registration_t __vnet_hash_function_##x
+
+vnet_hash_fn_t vnet_hash_default_function (vnet_hash_fn_type_t ftype);
+vnet_hash_fn_t vnet_hash_function_from_name (const char *name,
+                                            vnet_hash_fn_type_t ftype);
+vnet_hash_function_registration_t *
+vnet_hash_function_from_func (vnet_hash_fn_t fn, vnet_hash_fn_type_t ftype);
+format_function_t format_vnet_hash;
+
+#endif