Workaround for C thread to Java thread attachment for plugins 71/2171/1
authorHongjun Ni <[email protected]>
Tue, 2 Aug 2016 11:14:37 +0000 (19:14 +0800)
committerHongjun Ni <[email protected]>
Tue, 2 Aug 2016 11:14:37 +0000 (19:14 +0800)
Change-Id: Ic33c37f63a30946ee451d3223db3bd1e3043f408
Signed-off-by: Hongjun Ni <[email protected]>
nsh-plugin/nsh/nsh.api
nsh-plugin/nsh/nsh.c
nsh-plugin/nsh/nsh_test.c

index 01292d1..0b42b58 100644 (file)
@@ -104,3 +104,25 @@ define nsh_map_details {
     u32 sw_if_index;
     u32 next_node;
 };
+
+/** \brief Control ping from client to api server request
+    @param client_index - opaque cookie to identify the sender
+    @param context - sender context, to match reply w/ request
+*/
+define control_ping {
+    u32 client_index;
+    u32 context;
+};
+
+/** \brief Control ping from the client to the server response
+    @param client_index - opaque cookie to identify the sender
+    @param context - sender context, to match reply w/ request
+    @param retval - return code for the request
+    @param vpe_pid - the pid of the vpe, returned by the server
+*/
+define control_ping_reply {
+    u32 context;
+    i32 retval;
+    u32 client_index;
+    u32 vpe_pid;
+};
index 19c8fa7..99e4ac3 100644 (file)
     vl_msg_api_send_shmem (q, (u8 *)&rmp);                      \
   } while(0);
 
+#define REPLY_MACRO2(t, 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));                     \
+    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);
+
+#define FINISH                                  \
+    vec_add1 (s, 0);                            \
+    vl_print (handle, (char *)s);               \
+    vec_free (s);                               \
+    return handle;
+
 /* List of message types that this plugin understands */
 
 #define foreach_nsh_plugin_api_msg             \
   _(NSH_ADD_DEL_ENTRY, nsh_add_del_entry)      \
   _(NSH_ENTRY_DUMP, nsh_entry_dump)             \
   _(NSH_ADD_DEL_MAP, nsh_add_del_map)           \
-  _(NSH_MAP_DUMP, nsh_map_dump)
+  _(NSH_MAP_DUMP, nsh_map_dump)                 \
+  _(CONTROL_PING, control_ping)
 
 clib_error_t *
 vlib_plugin_register (vlib_main_t * vm, vnet_plugin_handoff_t * h,
@@ -95,6 +118,19 @@ typedef struct {
   nsh_header_t nsh_header;
 } nsh_input_trace_t;
 
+
+static void vl_api_control_ping_t_handler
+(vl_api_control_ping_t *mp)
+{
+    vl_api_control_ping_reply_t * rmp;
+    int rv = 0;
+
+    REPLY_MACRO2(VL_API_CONTROL_PING_REPLY,
+    ({
+       rmp->vpe_pid = ntohl (getpid());
+    }));
+}
+
 u8 * format_nsh_header (u8 * s, va_list * args)
 {
   nsh_header_t * nsh = va_arg (*args, nsh_header_t *);
index d3dabee..85b48d7 100644 (file)
@@ -85,7 +85,8 @@ foreach_standard_reply_retval_handler;
 _(NSH_ADD_DEL_ENTRY_REPLY, nsh_add_del_entry_reply)                    \
 _(NSH_ENTRY_DETAILS, nsh_entry_details)                                 \
 _(NSH_ADD_DEL_MAP_REPLY, nsh_add_del_map_reply)                         \
-_(NSH_MAP_DETAILS, nsh_map_details)
+_(NSH_MAP_DETAILS, nsh_map_details)                                     \
+_(CONTROL_PING_REPLY, control_ping_reply)
 
 
 /* M: construct, but don't yet send a message */
@@ -124,6 +125,42 @@ do {                                            \
     return -99;                                 \
 } while(0);
 
+
+static void vl_api_control_ping_reply_t_handler
+(vl_api_control_ping_reply_t * mp)
+{
+    vat_main_t * vam = &vat_main;
+    i32 retval = ntohl(mp->retval);
+    if (vam->async_mode) {
+        vam->async_errors += (retval < 0);
+    } else {
+        vam->retval = retval;
+        vam->result_ready = 1;
+    }
+}
+
+static void vl_api_control_ping_reply_t_handler_json
+(vl_api_control_ping_reply_t * mp)
+{
+    vat_main_t * vam = &vat_main;
+    i32 retval = ntohl(mp->retval);
+
+    if (VAT_JSON_NONE != vam->json_tree.type) {
+        vat_json_print(vam->ofp, &vam->json_tree);
+        vat_json_free(&vam->json_tree);
+        vam->json_tree.type = VAT_JSON_NONE;
+    } else {
+        /* just print [] */
+        vat_json_init_array(&vam->json_tree);
+        vat_json_print(vam->ofp, &vam->json_tree);
+        vam->json_tree.type = VAT_JSON_NONE;
+    }
+
+    vam->retval = retval;
+    vam->result_ready = 1;
+}
+
+
 static int api_nsh_add_del_entry (vat_main_t * vam)
 {
     nsh_test_main_t * sm = &nsh_test_main;