policer: add api to bind policer to worker

Add a new api to allow a policer to be bound to
a specific worker thread for thread handoff.

Type: improvement
Signed-off-by: Brian Russell <brian@graphiant.com>
Change-Id: I2623a6827843c3d93c0d7b4ad7c2e13611ec1696
diff --git a/src/vnet/policer/policer.api b/src/vnet/policer/policer.api
index 946cc35..c3b8d7c 100644
--- a/src/vnet/policer/policer.api
+++ b/src/vnet/policer/policer.api
@@ -17,6 +17,23 @@
 
 import "vnet/policer/policer_types.api";
 
+/** \brief policer bind: Associate/disassociate a policer with a worker thread.
+    @param client_index - opaque cookie to identify the sender
+    @param context - sender context, to match reply w/ request
+    @param name - policer name to bind
+    @param worker_index - the worker thread to bind to
+    @param bind_enable - Associate/disassociate
+*/
+autoreply define policer_bind
+{
+  u32 client_index;
+  u32 context;
+
+  string name[64];
+  u32 worker_index;
+  bool bind_enable;
+};
+
 /** \brief Add/del policer
     @param client_index - opaque cookie to identify the sender
     @param context - sender context, to match reply w/ request
diff --git a/src/vnet/policer/policer.c b/src/vnet/policer/policer.c
index 2c05ae2..8146d4b 100644
--- a/src/vnet/policer/policer.c
+++ b/src/vnet/policer/policer.c
@@ -13,6 +13,7 @@
  * limitations under the License.
  */
 #include <stdint.h>
+#include <stdbool.h>
 #include <vnet/policer/policer.h>
 #include <vnet/policer/police_inlines.h>
 #include <vnet/classify/vnet_classify.h>
@@ -133,6 +134,37 @@
   return 0;
 }
 
+int
+policer_bind_worker (u8 *name, u32 worker, bool bind)
+{
+  vnet_policer_main_t *pm = &vnet_policer_main;
+  policer_read_response_type_st *policer;
+  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]];
+
+  if (bind)
+    {
+      if (worker >= vlib_num_workers ())
+	{
+	  return VNET_API_ERROR_INVALID_WORKER;
+	}
+
+      policer->thread_index = vlib_get_worker_thread_index (worker);
+    }
+  else
+    {
+      policer->thread_index = ~0;
+    }
+  return 0;
+}
+
 u8 *
 format_policer_instance (u8 * s, va_list * va)
 {
diff --git a/src/vnet/policer/policer.h b/src/vnet/policer/policer.h
index 4d253f7..4c2c741 100644
--- a/src/vnet/policer/policer.h
+++ b/src/vnet/policer/policer.h
@@ -15,6 +15,8 @@
 #ifndef __included_policer_h__
 #define __included_policer_h__
 
+#include <stdbool.h>
+
 #include <vlib/vlib.h>
 #include <vnet/vnet.h>
 
@@ -68,6 +70,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);
 
 #endif /* __included_policer_h__ */
 
diff --git a/src/vnet/policer/policer_api.c b/src/vnet/policer/policer_api.c
index fb66667..17ed737 100644
--- a/src/vnet/policer/policer_api.c
+++ b/src/vnet/policer/policer_api.c
@@ -42,9 +42,10 @@
 
 #include <vlibapi/api_helper_macros.h>
 
-#define foreach_vpe_api_msg                             \
-_(POLICER_ADD_DEL, policer_add_del)                     \
-_(POLICER_DUMP, policer_dump)
+#define foreach_vpe_api_msg                                                   \
+  _ (POLICER_ADD_DEL, policer_add_del)                                        \
+  _ (POLICER_BIND, policer_bind)                                              \
+  _ (POLICER_DUMP, policer_dump)
 
 static void
 vl_api_policer_add_del_t_handler (vl_api_policer_add_del_t * mp)
@@ -96,6 +97,26 @@
 }
 
 static void
+vl_api_policer_bind_t_handler (vl_api_policer_bind_t *mp)
+{
+  vl_api_policer_bind_reply_t *rmp;
+  u8 *name;
+  u32 worker_index;
+  u8 bind_enable;
+  int rv;
+
+  name = format (0, "%s", mp->name);
+  vec_terminate_c_string (name);
+
+  worker_index = ntohl (mp->worker_index);
+  bind_enable = mp->bind_enable;
+
+  rv = policer_bind_worker (name, worker_index, bind_enable);
+  vec_free (name);
+  REPLY_MACRO (VL_API_POLICER_BIND_REPLY);
+}
+
+static void
 send_policer_details (u8 *name, qos_pol_cfg_params_st *config,
 		      policer_read_response_type_st *templ,
 		      vl_api_registration_t *reg, u32 context)