From c27213a30f4d6b5395ba70f011615ae9c7be93ce Mon Sep 17 00:00:00 2001
From: Ole Troan <ot@cisco.com>
Date: Wed, 31 Aug 2016 14:50:49 +0200
Subject: Add in-message cli_request/cli_reply API

This new CLI API is meant to replace the
cli_request/cli_reply that uses shared memory.

PS: checkstyle -- *hate*

Change-Id: I6318f8f6b9be2c2398b49dac9e2193c1998ea724
Signed-off-by: Ole Troan <ot@cisco.com>
---
 vpp/vpp-api/api.c         | 57 +++++++++++++++++++++++++++++++++++++++++++++++
 vpp/vpp-api/custom_dump.c | 11 +++++++++
 vpp/vpp-api/vpe.api       | 14 ++++++++++++
 3 files changed, 82 insertions(+)

(limited to 'vpp')

diff --git a/vpp/vpp-api/api.c b/vpp/vpp-api/api.c
index fbebfa6b..ead5f0cd 100644
--- a/vpp/vpp-api/api.c
+++ b/vpp/vpp-api/api.c
@@ -155,6 +155,22 @@ do {                                                            \
     vl_msg_api_send_shmem (q, (u8 *)&rmp);                      \
 } while(0);
 
+#define REPLY_MACRO3(t, n, body)				\
+do {                                                            \
+    unix_shared_memory_queue_t * q;                             \
+    rv = vl_msg_api_pd_handler (mp, rv);                        \
+    q = vl_api_client_index_to_input_queue (mp->client_index);  \
+    if (!q)                                                     \
+        return;                                                 \
+                                                                \
+    rmp = vl_msg_api_alloc (sizeof (*rmp) + n);                 \
+    rmp->_vl_msg_id = ntohs((t));                               \
+    rmp->context = mp->context;                                 \
+    rmp->retval = ntohl(rv);                                    \
+    do {body;} while (0);                                       \
+    vl_msg_api_send_shmem (q, (u8 *)&rmp);                      \
+} while(0);
+
 #if (1 || CLIB_DEBUG > 0)	/* "trust, but verify" */
 
 #define VALIDATE_SW_IF_INDEX(mp)				\
@@ -268,6 +284,7 @@ _(CREATE_LOOPBACK, create_loopback)					\
 _(CONTROL_PING, control_ping)                                           \
 _(NOPRINT_CONTROL_PING, noprint_control_ping)                           \
 _(CLI_REQUEST, cli_request)                                             \
+_(CLI_INBAND, cli_inband)						\
 _(SET_ARP_NEIGHBOR_LIMIT, set_arp_neighbor_limit)			\
 _(L2_PATCH_ADD_DEL, l2_patch_add_del)					\
 _(CLASSIFY_ADD_DEL_TABLE, classify_add_del_table)			\
@@ -3692,6 +3709,46 @@ vl_api_cli_request_t_handler (vl_api_cli_request_t * mp)
   vl_msg_api_send_shmem (q, (u8 *) & rp);
 }
 
+static void
+inband_cli_output (uword arg, u8 * buffer, uword buffer_bytes)
+{
+  u8 **mem_vecp = (u8 **) arg;
+  u8 *mem_vec = *mem_vecp;
+  u32 offset = vec_len (mem_vec);
+
+  vec_validate (mem_vec, offset + buffer_bytes - 1);
+  clib_memcpy (mem_vec + offset, buffer, buffer_bytes);
+  *mem_vecp = mem_vec;
+}
+
+static void
+vl_api_cli_inband_t_handler (vl_api_cli_inband_t * mp)
+{
+  vl_api_cli_inband_reply_t *rmp;
+  int rv = 0;
+  unix_shared_memory_queue_t *q;
+  vlib_main_t *vm = vlib_get_main ();
+  unformat_input_t input;
+  u8 *out_vec = 0;
+
+  q = vl_api_client_index_to_input_queue (mp->client_index);
+  if (!q)
+    return;
+
+  unformat_init_string (&input, (char *) mp->cmd, ntohl (mp->length));
+  vlib_cli_input (vm, &input, inband_cli_output, (uword) & out_vec);
+
+  u32 len = vec_len (out_vec);
+  /* *INDENT-OFF* */
+  REPLY_MACRO3(VL_API_CLI_INBAND_REPLY, len,
+  ({
+    rmp->length = htonl (len);
+    clib_memcpy (rmp->reply, out_vec, len);
+  }));
+  /* *INDENT-ON* */
+  vec_free (out_vec);
+}
+
 static void
 vl_api_set_arp_neighbor_limit_t_handler (vl_api_set_arp_neighbor_limit_t * mp)
 {
diff --git a/vpp/vpp-api/custom_dump.c b/vpp/vpp-api/custom_dump.c
index 566bcb9f..5ca7ccc9 100644
--- a/vpp/vpp-api/custom_dump.c
+++ b/vpp/vpp-api/custom_dump.c
@@ -1621,6 +1621,16 @@ static void *vl_api_cli_request_t_print
   FINISH;
 }
 
+static void *vl_api_cli_inband_t_print
+  (vl_api_cli_inband_t * mp, void *handle)
+{
+  u8 *s;
+
+  s = format (0, "SCRIPT: cli_inband ");
+
+  FINISH;
+}
+
 static void *vl_api_memclnt_create_t_print
   (vl_api_memclnt_create_t * mp, void *handle)
 {
@@ -2695,6 +2705,7 @@ _(SW_INTERFACE_DUMP, sw_interface_dump)					\
 _(CONTROL_PING, control_ping)						\
 _(WANT_INTERFACE_EVENTS, want_interface_events)				\
 _(CLI_REQUEST, cli_request)						\
+_(CLI_INBAND, cli_inband)						\
 _(MEMCLNT_CREATE, memclnt_create)					\
 _(SW_INTERFACE_VHOST_USER_DUMP, sw_interface_vhost_user_dump)           \
 _(SHOW_VERSION, show_version)                                           \
diff --git a/vpp/vpp-api/vpe.api b/vpp/vpp-api/vpe.api
index 1f26d551..51862f74 100644
--- a/vpp/vpp-api/vpe.api
+++ b/vpp/vpp-api/vpe.api
@@ -1183,6 +1183,13 @@ define cli_request
   u32 context;
   u64 cmd_in_shmem;
 };
+define cli_inband
+{
+  u32 client_index;
+  u32 context;
+  u32 length;
+  u8 cmd[length];
+};
 
 /** \brief vpe parser cli string response
     @param context - sender context, to match reply w/ request
@@ -1195,6 +1202,13 @@ define cli_reply
   i32 retval;
   u64 reply_in_shmem;
 };
+define cli_inband_reply
+{
+  u32 context;
+  i32 retval;
+  u32 length;
+  u8 reply[length];
+};
 
 /** \brief Set max allowed ARP or ip6 neighbor entries request
     @param client_index - opaque cookie to identify the sender
-- 
cgit