Add config option to use dlmalloc instead of mheap
[vpp.git] / src / vpp-api / java / jvpp-registry / jvpp_registry.c
index cbd5e0a..bbe9719 100644 (file)
@@ -52,22 +52,28 @@ void __stack_chk_guard(void) __attribute__((weak));
 void __stack_chk_guard(void) {
 }
 
+#define CONTROL_PING_MESSAGE "control_ping"
+#define CONTROL_PING_REPLY_MESSAGE "control_ping_reply"
+
 typedef struct {
     /* UThread attachment */
     volatile u32 control_ping_result_ready;
     volatile i32 control_ping_retval;
 
-    /* Control poing callback */
+    /* Control ping callback */
     jobject registryObject;
     jclass registryClass;
     jclass controlPingReplyClass;
     jclass callbackExceptionClass;
+    int control_ping_msg_id;
+    int control_ping_reply_msg_id;
 
     /* Thread cleanup */
     pthread_key_t cleanup_rx_thread_key;
 
     /* Connected indication */
     volatile u8 is_connected;
+    u32 vpe_pid;
 } jvpp_registry_main_t;
 
 jvpp_registry_main_t jvpp_registry_main __attribute__((aligned (64)));
@@ -165,7 +171,43 @@ static void vl_api_control_ping_reply_t_handler(
         }
     }
 
-    out: rm->control_ping_result_ready = 1;
+    out: rm->vpe_pid = clib_net_to_host_u32(mp->vpe_pid);
+    rm->control_ping_result_ready = 1;
+}
+
+static int find_ping_id() {
+    int rv = 0;
+    jvpp_main_t * jm = &jvpp_main;
+    jvpp_registry_main_t * rm = &jvpp_registry_main;
+    api_main_t *am = &api_main;
+    hash_pair_t *hp;
+    jm->messages_hash = am->msg_index_by_name_and_crc;
+
+    rm->control_ping_msg_id = -1;
+    rm->control_ping_reply_msg_id = -1;
+
+    hash_foreach_pair (hp, jm->messages_hash,
+    ({
+        char *key = (char *)hp->key; // key format: name_crc
+        int msg_name_len = strlen(key) - 9; // ignore crc
+        if (strlen(CONTROL_PING_MESSAGE) == msg_name_len &&
+                strncmp(CONTROL_PING_MESSAGE, (char *)hp->key, msg_name_len) == 0) {
+            rm->control_ping_msg_id = (u32)hp->value[0];
+        }
+        if (strlen(CONTROL_PING_REPLY_MESSAGE) == msg_name_len &&
+                strncmp(CONTROL_PING_REPLY_MESSAGE, (char *)hp->key, msg_name_len) == 0) {
+            rm->control_ping_reply_msg_id  = (u32)hp->value[0];
+        }
+    }));
+    if (rm->control_ping_msg_id == -1) {
+        clib_warning("failed to find id for %s", CONTROL_PING_MESSAGE);
+        rv = -1;
+    }
+    if (rm->control_ping_reply_msg_id == -1) {
+        clib_warning("failed to find id for %s", CONTROL_PING_REPLY_MESSAGE);
+        rv = -1;
+    }
+    return rv;
 }
 
 static int send_initial_control_ping() {
@@ -180,7 +222,7 @@ static int send_initial_control_ping() {
     rm->control_ping_result_ready = 0;
     mp = vl_msg_api_alloc(sizeof(*mp));
     memset(mp, 0, sizeof(*mp));
-    mp->_vl_msg_id = ntohs(VL_API_CONTROL_PING);
+    mp->_vl_msg_id = ntohs(rm->control_ping_msg_id);
     mp->client_index = jm->my_client_index;
 
     // send message:
@@ -197,24 +239,35 @@ static int send_initial_control_ping() {
     }
 
     if (rv != 0) {
-        clib_warning("common: first control ping failed: %d", rv);
+        vl_msg_api_clean_handlers(rm->control_ping_reply_msg_id);
+        clib_warning("first control ping failed: %d", rv);
     }
-
     return rv;
 }
 
-static int connect_to_vpe(char *name) {
+#if USE_DLMALLOC == 1
+void * __jvpp_heap;
+#endif
+
+static int connect_to_vpe(char *shm_prefix, char *name) {
     jvpp_main_t * jm = &jvpp_main;
     api_main_t * am = &api_main;
+    jvpp_registry_main_t * rm = &jvpp_registry_main;
 
-    if (vl_client_connect_to_vlib("/vpe-api", name, 32) < 0)
-        return -1;
+#if USE_DLMALLOC == 1
+    __jvpp_heap = clib_mem_init (0, 1<<20);
+#endif
 
+    if (vl_client_connect_to_vlib(shm_prefix, name, 32) < 0)
+        return -1;
     jm->my_client_index = am->my_client_index;
 
     jm->vl_input_queue = am->shmem_hdr->vl_input_queue;
 
-    vl_msg_api_set_handlers(VL_API_CONTROL_PING_REPLY, "control_ping_reply",
+    if (find_ping_id() < 0)
+        return -1;
+
+    vl_msg_api_set_handlers(rm->control_ping_reply_msg_id, CONTROL_PING_REPLY_MESSAGE,
             vl_api_control_ping_reply_t_handler, vl_noop_handler,
             vl_api_control_ping_reply_t_endian,
             vl_api_control_ping_reply_t_print,
@@ -224,9 +277,15 @@ static int connect_to_vpe(char *name) {
 }
 
 JNIEXPORT jobject JNICALL Java_io_fd_vpp_jvpp_VppJNIConnection_clientConnect(
-        JNIEnv *env, jclass obj, jstring clientName) {
+        JNIEnv *env, jclass obj, jstring shmPrefix, jstring clientName) {
+    /*
+     * TODO introducing memory prefix as variable can be used in hc2vpp
+     * to be able to run without root privileges
+     * https://jira.fd.io/browse/HC2VPP-176
+     */
     int rv;
     const char *client_name;
+    const char *shm_prefix;
     void vl_msg_reply_handler_hookup(void);
     jvpp_main_t * jm = &jvpp_main;
     jvpp_registry_main_t * rm = &jvpp_registry_main;
@@ -234,39 +293,38 @@ JNIEXPORT jobject JNICALL Java_io_fd_vpp_jvpp_VppJNIConnection_clientConnect(
     jclass connectionInfoClass = (*env)->FindClass(env,
             "io/fd/vpp/jvpp/VppJNIConnection$ConnectionInfo");
     jmethodID connectionInfoConstructor = (*env)->GetMethodID(env,
-            connectionInfoClass, "<init>", "(JII)V");
-
-    /*
-     * Bail out now if we're not running as root
-     */
-    if (geteuid() != 0) {
-        return (*env)->NewObject(env, connectionInfoClass,
-                connectionInfoConstructor, 0, 0,
-                VNET_API_ERROR_NOT_RUNNING_AS_ROOT);
-    }
+            connectionInfoClass, "<init>", "(JIII)V");
 
     if (rm->is_connected) {
         return (*env)->NewObject(env, connectionInfoClass,
                 connectionInfoConstructor, 0, 0,
-                VNET_API_ERROR_ALREADY_CONNECTED);
+                VNET_API_ERROR_ALREADY_CONNECTED, 0);
     }
 
     client_name = (*env)->GetStringUTFChars(env, clientName, 0);
+    shm_prefix = (*env)->GetStringUTFChars(env, shmPrefix, 0);
+
     if (!client_name) {
         return (*env)->NewObject(env, connectionInfoClass,
-                connectionInfoConstructor, 0, 0, VNET_API_ERROR_INVALID_VALUE);
+                connectionInfoConstructor, 0, 0, VNET_API_ERROR_INVALID_VALUE, 0, shmPrefix);
+    }
+
+    if (!shm_prefix) {
+        return (*env)->NewObject(env, connectionInfoClass,
+                connectionInfoConstructor, 0, 0, VNET_API_ERROR_INVALID_VALUE, 0, shmPrefix);
     }
 
-    rv = connect_to_vpe((char *) client_name);
+    rv = connect_to_vpe((char *) shm_prefix, (char *) client_name);
 
     if (rv < 0)
         clib_warning("connection failed, rv %d", rv);
 
     (*env)->ReleaseStringUTFChars(env, clientName, client_name);
+    (*env)->ReleaseStringUTFChars(env, shmPrefix, shm_prefix);
 
     return (*env)->NewObject(env, connectionInfoClass,
-            connectionInfoConstructor, (jlong) jm->vl_input_queue,
-            (jint) jm->my_client_index, (jint) rv);
+            connectionInfoConstructor, (jlong) pointer_to_uword (jm->vl_input_queue),
+            (jint) jm->my_client_index, (jint) rv, (jint) rm->vpe_pid);
 }
 
 JNIEXPORT jint JNICALL Java_io_fd_vpp_jvpp_JVppRegistryImpl_controlPing0(
@@ -286,7 +344,7 @@ JNIEXPORT jint JNICALL Java_io_fd_vpp_jvpp_JVppRegistryImpl_controlPing0(
 
     mp = vl_msg_api_alloc(sizeof(*mp));
     memset(mp, 0, sizeof(*mp));
-    mp->_vl_msg_id = ntohs(VL_API_CONTROL_PING);
+    mp->_vl_msg_id = ntohs(rm->control_ping_msg_id);
     mp->client_index = jm->my_client_index;
     mp->context = clib_host_to_net_u32(my_context_id);