/* SPDX-License-Identifier: Apache-2.0
 * Copyright(c) 2021 Cisco Systems, Inc.
 */

#include <vppinfra/format.h>
#include <vppinfra/test/test.h>
#include <vppinfra/vector/ip_csum.h>

typedef struct
{
  struct
  {
    u8 *src;
    u32 count;
  } chunk[5];
  u16 result;
} ip_csum_test_t;

static u8 test1[] = { 0x45, 0x00, 0x00, 0x73, 0x00, 0x00, 0x40,
		      0x00, 0x40, 0x11, 0x00, 0x00, 0xc0, 0xa8,
		      0x00, 0x01, 0xc0, 0xa8, 0x00, 0xc7, 0x00 };
#define TEST_LEN(x) (ARRAY_LEN (x) - 1)

static ip_csum_test_t tests[] = { {
				    .chunk[0].src = test1,
				    .chunk[0].count = TEST_LEN (test1),
				    .result = 0x61b8,
				  },
				  {
				    .chunk[0].src = test1,
				    .chunk[0].count = 1,
				    .chunk[1].src = test1 + 1,
				    .chunk[1].count = 2,
				    .chunk[2].src = test1 + 3,
				    .chunk[2].count = 3,
				    .chunk[3].src = test1 + 6,
				    .chunk[3].count = 4,
				    .chunk[4].src = test1 + 10,
				    .chunk[4].count = TEST_LEN (test1) - 10,
				    .result = 0x61b8,
				  },
				  {
				    .chunk[0].count = 1,
				    .result = 0xff0f,
				  },
				  {
				    .chunk[0].count = 2,
				    .result = 0x080f,
				  },
				  {
				    .chunk[0].count = 3,
				    .result = 0x0711,
				  },
				  {
				    .chunk[0].count = 4,
				    .result = 0x1210,
				  },
				  {
				    .chunk[0].count = 63,
				    .result = 0xda01,
				  },
				  {
				    .chunk[0].count = 64,
				    .result = 0xe100,
				  },
				  {
				    .chunk[0].count = 65,
				    .result = 0xe010,
				  },
				  {
				    .chunk[0].count = 65535,
				    .result = 0xfc84,
				  },
				  {
				    .chunk[0].count = 65536,
				    .result = 0xffff,
				  } };

static clib_error_t *
test_clib_ip_csum (clib_error_t *err)
{
  u8 *buf;
  buf = test_mem_alloc (65536);
  for (int i = 0; i < 65536; i++)
    buf[i] = 0xf0 + ((i * 7) & 0xf);

  for (int i = 0; i < ARRAY_LEN (tests); i++)
    {
      clib_ip_csum_t c = {};
      ip_csum_test_t *t = tests + i;
      u16 rv;

      for (int j = 0; j < ARRAY_LEN (((ip_csum_test_t *) 0)->chunk); j++)
	if (t->chunk[j].count > 0)
	  {
	    if (t->chunk[j].src == 0)
	      clib_ip_csum_chunk (&c, buf, t->chunk[j].count);
	    else
	      clib_ip_csum_chunk (&c, t->chunk[j].src, t->chunk[j].count);
	  }
      rv = clib_ip_csum_fold (&c);

      if (rv != tests[i].result)
	{
	  err = clib_error_return (err,
				   "bad checksum in test case %u (expected "
				   "0x%04x, calculated 0x%04x)",
				   i, tests[i].result, rv);
	  goto done;
	}
    }
done:
  return err;
}

void __test_perf_fn
perftest_ip4_hdr (test_perf_t *tp)
{
  u32 n = tp->n_ops;
  u8 *data = test_mem_alloc_and_splat (20, n, (void *) &test1);
  u16 *res = test_mem_alloc (n * sizeof (u16));

  test_perf_event_enable (tp);
  for (int i = 0; i < n; i++)
    res[i] = clib_ip_csum (data + i * 20, 20);
  test_perf_event_disable (tp);
}

void __test_perf_fn
perftest_tcp_payload (test_perf_t *tp)
{
  u32 n = tp->n_ops;
  volatile uword *lenp = &tp->arg0;
  u8 *data = test_mem_alloc_and_splat (20, n, (void *) &test1);
  u16 *res = test_mem_alloc (n * sizeof (u16));

  test_perf_event_enable (tp);
  for (int i = 0; i < n; i++)
    res[i] = clib_ip_csum (data + i * lenp[0], lenp[0]);
  test_perf_event_disable (tp);
}

void __test_perf_fn
perftest_byte (test_perf_t *tp)
{
  volatile uword *np = &tp->n_ops;
  u8 *data = test_mem_alloc_and_fill_inc_u8 (*np, 0, 0);
  u16 *res = test_mem_alloc (sizeof (u16));

  test_perf_event_enable (tp);
  res[0] = clib_ip_csum (data, np[0]);
  test_perf_event_disable (tp);
}

REGISTER_TEST (clib_ip_csum) = {
  .name = "clib_ip_csum",
  .fn = test_clib_ip_csum,
  .perf_tests = PERF_TESTS (
    { .name = "fixed size (per IPv4 Header)",
      .n_ops = 1024,
      .fn = perftest_ip4_hdr },
    { .name = "fixed size (per 1460 byte block)",
      .n_ops = 16,
      .arg0 = 1460,
      .fn = perftest_tcp_payload },
    { .name = "variable size (per byte)", .n_ops = 16384, .fn = perftest_byte }

    ),
};