2 * Copyright (c) 2016 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
15 #define _GNU_SOURCE /* for strcasestr(3) */
17 #include <vnet/api_errno.h>
18 #include "jvpp_common.h"
25 #define DEBUG_LOG(...) clib_warning(__VA_ARGS__)
27 #define DEBUG_LOG(...)
30 #define _(error,errorCode,msg) \
31 if (errorCode == code) \
35 #define get_error_message(errno) \
37 foreach_vnet_api_error \
38 message = "Reason unknown";
40 /* shared jvpp main structure */
41 jvpp_main_t jvpp_main __attribute__((aligned (64)));
43 void call_on_error(const char* callName, int contextId, int retval,
44 jclass callbackClass, jobject callbackObject,
45 jclass callbackExceptionClass) {
46 DEBUG_LOG("\nCallOnError : callback=%s, retval=%d, context=%d\n", callName,
47 clib_net_to_host_u32(retval), clib_net_to_host_u32(context));
48 JNIEnv *env = jvpp_main.jenv;
50 DEBUG_LOG("CallOnError : jm->callbackClass is null!\n");
53 jmethodID excConstructor = (*env)->GetMethodID(env, callbackExceptionClass,
54 "<init>", "(Ljava/lang/String;Ljava/lang/String;II)V");
55 if (!excConstructor) {
56 DEBUG_LOG("CallOnError : excConstructor is null!\n");
59 jmethodID callbackExcMethod = (*env)->GetMethodID(env, callbackClass,
60 "onError", "(Lio/fd/vpp/jvpp/VppCallbackException;)V");
61 if (!callbackExcMethod) {
62 DEBUG_LOG("CallOnError : callbackExcMethod is null!\n");
67 get_error_message(clib_net_to_host_u32(retval));
68 jobject excObject = (*env)->NewObject(env, callbackExceptionClass,
69 excConstructor, (*env)->NewStringUTF(env, callName),
70 (*env)->NewStringUTF(env, message),
71 clib_net_to_host_u32(contextId), clib_net_to_host_u32(retval));
73 DEBUG_LOG("CallOnError : excObject is null!\n");
77 (*env)->CallVoidMethod(env, callbackObject, callbackExcMethod, excObject);
78 DEBUG_LOG("CallOnError : Response sent\n");
82 u32 get_message_id(JNIEnv *env, const char *key) {
83 uword *p = hash_get(jvpp_main.messages_hash, key);
85 jclass exClass = (*env)->FindClass(env, "java/lang/IllegalStateException");
86 char *msgBuf = clib_mem_alloc(strlen(key) + 70);
87 strcpy(msgBuf, "API mismatch detected: ");
89 strcat(msgBuf, " is missing in global name_crc hash table.");
90 DEBUG_LOG("%s", msgBuf);
91 DEBUG_LOG("Possible reasons:");
92 DEBUG_LOG("1) incompatible VPP version used");
93 DEBUG_LOG("2) message present in JSON file but not in global name_crc table");
94 (*env)->ThrowNew(env, exClass, msgBuf);
95 clib_mem_free(msgBuf);