/*
 *------------------------------------------------------------------
 * policer_api.c - policer api
 *
 * Copyright (c) 2016 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/vnet.h>
#include <vlibmemory/api.h>

#include <vnet/interface.h>
#include <vnet/api_errno.h>
#include <vnet/policer/policer.h>

#include <vnet/format_fns.h>
#include <vnet/policer/policer.api_enum.h>
#include <vnet/policer/policer.api_types.h>

#define REPLY_MSG_ID_BASE vnet_policer_main.msg_id_base
#include <vlibapi/api_helper_macros.h>

static void
vl_api_policer_add_del_t_handler (vl_api_policer_add_del_t * mp)
{
  vlib_main_t *vm = vlib_get_main ();
  vnet_policer_main_t *pm = &vnet_policer_main;
  vl_api_policer_add_del_reply_t *rmp;
  int rv = 0;
  uword *p;
  char name[sizeof (mp->name) + 1];
  qos_pol_cfg_params_st cfg;
  u32 policer_index;

  snprintf (name, sizeof (name), "%s", mp->name);

  if (mp->is_add)
    {
      clib_memset (&cfg, 0, sizeof (cfg));
      cfg.rfc = (qos_policer_type_en) mp->type;
      cfg.rnd_type = (qos_round_type_en) mp->round_type;
      cfg.rate_type = (qos_rate_type_en) mp->rate_type;
      cfg.rb.kbps.cir_kbps = ntohl (mp->cir);
      cfg.rb.kbps.eir_kbps = ntohl (mp->eir);
      cfg.rb.kbps.cb_bytes = clib_net_to_host_u64 (mp->cb);
      cfg.rb.kbps.eb_bytes = clib_net_to_host_u64 (mp->eb);
      cfg.conform_action.action_type =
	(qos_action_type_en) mp->conform_action.type;
      cfg.conform_action.dscp = mp->conform_action.dscp;
      cfg.exceed_action.action_type =
	(qos_action_type_en) mp->exceed_action.type;
      cfg.exceed_action.dscp = mp->exceed_action.dscp;
      cfg.violate_action.action_type =
	(qos_action_type_en) mp->violate_action.type;
      cfg.violate_action.dscp = mp->violate_action.dscp;
      cfg.color_aware = mp->color_aware;

      rv = policer_add (vm, (u8 *) name, &cfg, &policer_index);
    }
  else
    {
      p = hash_get_mem (pm->policer_index_by_name, name);

      rv = VNET_API_ERROR_NO_SUCH_ENTRY;
      if (p != NULL)
	rv = policer_del (vm, p[0]);
    }

  REPLY_MACRO2 (VL_API_POLICER_ADD_DEL_REPLY, ({
		  if (rv == 0 && mp->is_add)
		    rmp->policer_index = htonl (policer_index);
		  else
		    rmp->policer_index = ~0;
		}));
}

static_always_inline void
policer_set_configuration (qos_pol_cfg_params_st *cfg,
			   vl_api_policer_config_t *infos)
{
  clib_memset (cfg, 0, sizeof (*cfg));
  cfg->rfc = (qos_policer_type_en) infos->type;
  cfg->rnd_type = (qos_round_type_en) infos->round_type;
  cfg->rate_type = (qos_rate_type_en) infos->rate_type;
  cfg->rb.kbps.cir_kbps = ntohl (infos->cir);
  cfg->rb.kbps.eir_kbps = ntohl (infos->eir);
  cfg->rb.kbps.cb_bytes = clib_net_to_host_u64 (infos->cb);
  cfg->rb.kbps.eb_bytes = clib_net_to_host_u64 (infos->eb);
  cfg->conform_action.action_type =
    (qos_action_type_en) infos->conform_action.type;
  cfg->conform_action.dscp = infos->conform_action.dscp;
  cfg->exceed_action.action_type =
    (qos_action_type_en) infos->exceed_action.type;
  cfg->exceed_action.dscp = infos->exceed_action.dscp;
  cfg->violate_action.action_type =
    (qos_action_type_en) infos->violate_action.type;
  cfg->violate_action.dscp = infos->violate_action.dscp;
  cfg->color_aware = infos->color_aware;
}

static void
vl_api_policer_add_t_handler (vl_api_policer_add_t *mp)
{
  vlib_main_t *vm = vlib_get_main ();
  vl_api_policer_add_reply_t *rmp;
  int rv = 0;
  char name[sizeof (mp->name) + 1];
  qos_pol_cfg_params_st cfg;
  u32 policer_index;

  snprintf (name, sizeof (name), "%s", mp->name);

  policer_set_configuration (&cfg, &mp->infos);

  rv = policer_add (vm, (u8 *) name, &cfg, &policer_index);

  REPLY_MACRO2 (VL_API_POLICER_ADD_REPLY, ({
		  if (rv == 0)
		    rmp->policer_index = htonl (policer_index);
		  else
		    rmp->policer_index = ~0;
		}));
}

static void
vl_api_policer_del_t_handler (vl_api_policer_del_t *mp)
{
  vlib_main_t *vm = vlib_get_main ();
  vl_api_policer_del_reply_t *rmp;
  u32 policer_index;
  int rv = 0;

  policer_index = ntohl (mp->policer_index);
  rv = policer_del (vm, policer_index);

  REPLY_MACRO (VL_API_POLICER_DEL_REPLY);
}

static void
vl_api_policer_update_t_handler (vl_api_policer_update_t *mp)
{
  vlib_main_t *vm = vlib_get_main ();
  vl_api_policer_update_reply_t *rmp;
  int rv = 0;
  qos_pol_cfg_params_st cfg;
  u32 policer_index;

  policer_set_configuration (&cfg, &mp->infos);

  policer_index = ntohl (mp->policer_index);
  rv = policer_update (vm, policer_index, &cfg);

  REPLY_MACRO (VL_API_POLICER_UPDATE_REPLY);
}

static void
vl_api_policer_reset_t_handler (vl_api_policer_reset_t *mp)
{
  vlib_main_t *vm = vlib_get_main ();
  vl_api_policer_reset_reply_t *rmp;
  u32 policer_index;
  int rv = 0;

  policer_index = ntohl (mp->policer_index);
  rv = policer_reset (vm, policer_index);

  REPLY_MACRO (VL_API_POLICER_RESET_REPLY);
}

static void
vl_api_policer_bind_t_handler (vl_api_policer_bind_t *mp)
{
  vl_api_policer_bind_reply_t *rmp;
  vnet_policer_main_t *pm = &vnet_policer_main;
  char name[sizeof (mp->name) + 1];
  uword *p;
  u32 worker_index;
  u8 bind_enable;
  int rv;

  snprintf (name, sizeof (name), "%s", mp->name);

  worker_index = ntohl (mp->worker_index);
  bind_enable = mp->bind_enable;

  p = hash_get_mem (pm->policer_index_by_name, name);

  rv = VNET_API_ERROR_NO_SUCH_ENTRY;
  if (p != NULL)
    rv = policer_bind_worker (p[0], worker_index, bind_enable);

  REPLY_MACRO (VL_API_POLICER_BIND_REPLY);
}

static void
vl_api_policer_bind_v2_t_handler (vl_api_policer_bind_v2_t *mp)
{
  vl_api_policer_bind_v2_reply_t *rmp;
  u32 policer_index;
  u32 worker_index;
  u8 bind_enable;
  int rv;

  policer_index = ntohl (mp->policer_index);
  worker_index = ntohl (mp->worker_index);
  bind_enable = mp->bind_enable;

  rv = policer_bind_worker (policer_index, worker_index, bind_enable);

  REPLY_MACRO (VL_API_POLICER_BIND_V2_REPLY);
}

static void
vl_api_policer_input_t_handler (vl_api_policer_input_t *mp)
{
  vl_api_policer_input_reply_t *rmp;
  vnet_policer_main_t *pm = &vnet_policer_main;
  char name[sizeof (mp->name) + 1];
  uword *p;
  u32 sw_if_index;
  u8 apply;
  int rv;

  VALIDATE_SW_IF_INDEX (mp);

  snprintf (name, sizeof (name), "%s", mp->name);

  sw_if_index = ntohl (mp->sw_if_index);
  apply = mp->apply;

  p = hash_get_mem (pm->policer_index_by_name, name);

  rv = VNET_API_ERROR_NO_SUCH_ENTRY;
  if (p != NULL)
    rv = policer_input (p[0], sw_if_index, VLIB_RX, apply);

  BAD_SW_IF_INDEX_LABEL;
  REPLY_MACRO (VL_API_POLICER_INPUT_REPLY);
}

static void
vl_api_policer_input_v2_t_handler (vl_api_policer_input_v2_t *mp)
{
  vl_api_policer_input_v2_reply_t *rmp;
  u32 policer_index;
  u32 sw_if_index;
  u8 apply;
  int rv;

  VALIDATE_SW_IF_INDEX (mp);

  policer_index = ntohl (mp->policer_index);
  sw_if_index = ntohl (mp->sw_if_index);
  apply = mp->apply;

  rv = policer_input (policer_index, sw_if_index, VLIB_RX, apply);

  BAD_SW_IF_INDEX_LABEL;
  REPLY_MACRO (VL_API_POLICER_INPUT_REPLY);
}

static void
vl_api_policer_output_t_handler (vl_api_policer_output_t *mp)
{
  vl_api_policer_output_reply_t *rmp;
  vnet_policer_main_t *pm = &vnet_policer_main;
  char name[sizeof (mp->name) + 1];
  uword *p;
  u32 sw_if_index;
  u8 apply;
  int rv;

  VALIDATE_SW_IF_INDEX (mp);

  snprintf (name, sizeof (name), "%s", mp->name);

  sw_if_index = ntohl (mp->sw_if_index);
  apply = mp->apply;

  p = hash_get_mem (pm->policer_index_by_name, name);

  rv = VNET_API_ERROR_NO_SUCH_ENTRY;
  if (p != NULL)
    rv = policer_input (p[0], sw_if_index, VLIB_TX, apply);

  BAD_SW_IF_INDEX_LABEL;
  REPLY_MACRO (VL_API_POLICER_OUTPUT_REPLY);
}

static void
vl_api_policer_output_v2_t_handler (vl_api_policer_output_v2_t *mp)
{
  vl_api_policer_output_reply_t *rmp;
  u32 policer_index;
  u32 sw_if_index;
  u8 apply;
  int rv;

  VALIDATE_SW_IF_INDEX (mp);

  policer_index = ntohl (mp->policer_index);
  sw_if_index = ntohl (mp->sw_if_index);
  apply = mp->apply;

  rv = policer_input (policer_index, sw_if_index, VLIB_TX, apply);

  BAD_SW_IF_INDEX_LABEL;
  REPLY_MACRO (VL_API_POLICER_OUTPUT_REPLY);
}

static void
send_policer_details (qos_pol_cfg_params_st *config, policer_t *policer,
		      vl_api_registration_t *reg, u32 context)
{
  vl_api_policer_details_t *mp;

  mp = vl_msg_api_alloc (sizeof (*mp));
  clib_memset (mp, 0, sizeof (*mp));
  mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_POLICER_DETAILS);
  mp->context = context;
  mp->cir = htonl (config->rb.kbps.cir_kbps);
  mp->eir = htonl (config->rb.kbps.eir_kbps);
  mp->cb = clib_host_to_net_u64 (config->rb.kbps.cb_bytes);
  mp->eb = clib_host_to_net_u64 (config->rb.kbps.eb_bytes);
  mp->rate_type = (vl_api_sse2_qos_rate_type_t) config->rate_type;
  mp->round_type = (vl_api_sse2_qos_round_type_t) config->rnd_type;
  mp->type = (vl_api_sse2_qos_policer_type_t) config->rfc;
  mp->conform_action.type =
    (vl_api_sse2_qos_action_type_t) policer->action[POLICE_CONFORM];
  mp->conform_action.dscp = policer->mark_dscp[POLICE_CONFORM];
  mp->exceed_action.type =
    (vl_api_sse2_qos_action_type_t) policer->action[POLICE_EXCEED];
  mp->exceed_action.dscp = policer->mark_dscp[POLICE_EXCEED];
  mp->violate_action.type =
    (vl_api_sse2_qos_action_type_t) policer->action[POLICE_VIOLATE];
  mp->violate_action.dscp = policer->mark_dscp[POLICE_VIOLATE];
  mp->single_rate = policer->single_rate ? 1 : 0;
  mp->color_aware = policer->color_aware ? 1 : 0;
  mp->scale = htonl (policer->scale);
  mp->cir_tokens_per_period = htonl (policer->cir_tokens_per_period);
  mp->pir_tokens_per_period = htonl (policer->pir_tokens_per_period);
  mp->current_limit = htonl (policer->current_limit);
  mp->current_bucket = htonl (policer->current_bucket);
  mp->extended_limit = htonl (policer->extended_limit);
  mp->extended_bucket = htonl (policer->extended_bucket);
  mp->last_update_time = clib_host_to_net_u64 (policer->last_update_time);

  strncpy ((char *) mp->name, (char *) policer->name,
	   ARRAY_LEN (mp->name) - 1);

  vl_api_send_msg (reg, (u8 *) mp);
}

static void
vl_api_policer_dump_t_handler (vl_api_policer_dump_t * mp)
{
  vl_api_registration_t *reg;
  vnet_policer_main_t *pm = &vnet_policer_main;
  uword *p, *pi;
  u32 pool_index, policer_index;
  u8 *match_name = 0;
  qos_pol_cfg_params_st *config;
  policer_t *policer;

  reg = vl_api_client_index_to_registration (mp->client_index);
  if (!reg)
    return;

  if (mp->match_name_valid)
    {
      match_name = format (0, "%s%c", mp->match_name, 0);
      vec_terminate_c_string (match_name);
    }

  if (mp->match_name_valid)
    {
      p = hash_get_mem (pm->policer_config_by_name, match_name);
      pi = hash_get_mem (pm->policer_index_by_name, match_name);
      if (0 == p || 0 == pi)
	return;

      pool_index = p[0];
      policer_index = pi[0];
      config = pool_elt_at_index (pm->configs, pool_index);
      policer = pool_elt_at_index (pm->policers, policer_index);
      send_policer_details (config, policer, reg, mp->context);
    }
  else
    {
      pool_foreach (policer, pm->policers)
	{
	  p = hash_get_mem (pm->policer_config_by_name, policer->name);
	  if (0 == p)
	    continue;

	  pool_index = p[0];
	  config = pool_elt_at_index (pm->configs, pool_index);
	  send_policer_details (config, policer, reg, mp->context);
	};
    }
}

static void
vl_api_policer_dump_v2_t_handler (vl_api_policer_dump_v2_t *mp)
{
  vl_api_registration_t *reg;
  vnet_policer_main_t *pm = &vnet_policer_main;
  qos_pol_cfg_params_st *config;
  u32 policer_index, pool_index;
  policer_t *policer;
  uword *p;

  reg = vl_api_client_index_to_registration (mp->client_index);
  if (!reg)
    return;

  policer_index = ntohl (mp->policer_index);

  if (~0 == policer_index)
    {
      pool_foreach (policer, pm->policers)
	{
	  p = hash_get_mem (pm->policer_config_by_name, policer->name);
	  pool_index = p[0];
	  config = pool_elt_at_index (pm->configs, pool_index);
	  send_policer_details (config, policer, reg, mp->context);
	};
    }
  else
    {
      if (pool_is_free_index (pm->policers, policer_index))
	return;

      policer = &pm->policers[policer_index];
      p = hash_get_mem (pm->policer_config_by_name, policer->name);
      pool_index = p[0];
      config = pool_elt_at_index (pm->configs, pool_index);
      send_policer_details (config, policer, reg, mp->context);
    }
}

#include <vnet/policer/policer.api.c>
static clib_error_t *
policer_api_hookup (vlib_main_t * vm)
{
  /*
   * Set up the (msg_name, crc, message-id) table
   */
  REPLY_MSG_ID_BASE = setup_message_id_table ();

  return 0;
}

VLIB_API_INIT_FUNCTION (policer_api_hookup);

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */