policer: add api to configure input policing 43/31343/3
authorBrian Russell <brian@graphiant.com>
Wed, 17 Feb 2021 15:51:45 +0000 (15:51 +0000)
committerNeale Ranns <neale@graphiant.com>
Fri, 19 Feb 2021 10:46:58 +0000 (10:46 +0000)
Add a new API to apply a policer to an input interface.

Type: improvement
Signed-off-by: Brian Russell <brian@graphiant.com>
Change-Id: Ie8aff9120149b63d85363a9a5afdcaed60a93700

src/vnet/policer/policer.api
src/vnet/policer/policer.c
src/vnet/policer/policer.h
src/vnet/policer/policer_api.c

index c3b8d7c..a664ab0 100644 (file)
@@ -15,6 +15,7 @@
 
 option version = "2.0.0";
 
+import "vnet/interface_types.api";
 import "vnet/policer/policer_types.api";
 
 /** \brief policer bind: Associate/disassociate a policer with a worker thread.
@@ -34,6 +35,23 @@ autoreply define policer_bind
   bool bind_enable;
 };
 
+/** \brief policer input: Apply policer as an input feature.
+    @param client_index - opaque cookie to identify the sender
+    @param context - sender context, to match reply w/ request
+    @param name - policer name
+    @param sw_if_index - interface to apply the policer
+    @param apply - Apply/remove
+*/
+autoreply define policer_input
+{
+  u32 client_index;
+  u32 context;
+
+  string name[64];
+  vl_api_interface_index_t sw_if_index;
+  bool apply;
+};
+
 /** \brief Add/del policer
     @param client_index - opaque cookie to identify the sender
     @param context - sender context, to match reply w/ request
index fb2ce75..678e588 100644 (file)
@@ -165,6 +165,38 @@ policer_bind_worker (u8 *name, u32 worker, bool bind)
   return 0;
 }
 
+int
+policer_input (u8 *name, u32 sw_if_index, bool apply)
+{
+  vnet_policer_main_t *pm = &vnet_policer_main;
+  policer_t *policer;
+  u32 policer_index;
+  uword *p;
+
+  p = hash_get_mem (pm->policer_index_by_name, name);
+  if (p == 0)
+    {
+      return VNET_API_ERROR_NO_SUCH_ENTRY;
+    }
+
+  policer = &pm->policers[p[0]];
+  policer_index = policer - pm->policers;
+
+  if (apply)
+    {
+      vec_validate (pm->policer_index_by_sw_if_index, sw_if_index);
+      pm->policer_index_by_sw_if_index[sw_if_index] = policer_index;
+    }
+  else
+    {
+      pm->policer_index_by_sw_if_index[sw_if_index] = ~0;
+    }
+
+  vnet_feature_enable_disable ("device-input", "policer-input", sw_if_index,
+                              apply, 0, 0);
+  return 0;
+}
+
 u8 *
 format_policer_instance (u8 * s, va_list * va)
 {
index 9a6de14..9f090b1 100644 (file)
@@ -63,6 +63,7 @@ clib_error_t *policer_add_del (vlib_main_t *vm, u8 *name,
                               qos_pol_cfg_params_st *cfg, u32 *policer_index,
                               u8 is_add);
 int policer_bind_worker (u8 *name, u32 worker, bool bind);
+int policer_input (u8 *name, u32 sw_if_index, bool apply);
 
 #endif /* __included_policer_h__ */
 
index eff02fb..c3a9800 100644 (file)
@@ -45,6 +45,7 @@
 #define foreach_vpe_api_msg                                                   \
   _ (POLICER_ADD_DEL, policer_add_del)                                        \
   _ (POLICER_BIND, policer_bind)                                              \
+  _ (POLICER_INPUT, policer_input)                                            \
   _ (POLICER_DUMP, policer_dump)
 
 static void
@@ -116,6 +117,30 @@ vl_api_policer_bind_t_handler (vl_api_policer_bind_t *mp)
   REPLY_MACRO (VL_API_POLICER_BIND_REPLY);
 }
 
+static void
+vl_api_policer_input_t_handler (vl_api_policer_input_t *mp)
+{
+  vl_api_policer_bind_reply_t *rmp;
+  u8 *name;
+  u32 sw_if_index;
+  u8 apply;
+  int rv;
+
+  VALIDATE_SW_IF_INDEX (mp);
+
+  name = format (0, "%s", mp->name);
+  vec_terminate_c_string (name);
+
+  sw_if_index = ntohl (mp->sw_if_index);
+  apply = mp->apply;
+
+  rv = policer_input (name, sw_if_index, apply);
+  vec_free (name);
+
+  BAD_SW_IF_INDEX_LABEL;
+  REPLY_MACRO (VL_API_POLICER_INPUT_REPLY);
+}
+
 static void
 send_policer_details (u8 *name, qos_pol_cfg_params_st *config,
                      policer_t *templ, vl_api_registration_t *reg,