Marek Gradzki | 66ea26b | 2016-07-26 15:28:22 +0200 | [diff] [blame] | 1 | /* |
| 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: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 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. |
| 14 | */ |
| 15 | #define _GNU_SOURCE /* for strcasestr(3) */ |
| 16 | |
Matej Perina | 63a46fc | 2017-07-20 15:35:19 +0200 | [diff] [blame] | 17 | #include <vnet/api_errno.h> |
Marek Gradzki | 66ea26b | 2016-07-26 15:28:22 +0200 | [diff] [blame] | 18 | #include "jvpp_common.h" |
| 19 | |
| 20 | #ifndef JVPP_DEBUG |
| 21 | #define JVPP_DEBUG 0 |
| 22 | #endif |
| 23 | |
| 24 | #if JVPP_DEBUG == 1 |
| 25 | #define DEBUG_LOG(...) clib_warning(__VA_ARGS__) |
| 26 | #else |
| 27 | #define DEBUG_LOG(...) |
| 28 | #endif |
| 29 | |
Matej Perina | 63a46fc | 2017-07-20 15:35:19 +0200 | [diff] [blame] | 30 | #define _(error,errorCode,msg) \ |
| 31 | if (errorCode == code) \ |
| 32 | message = msg; \ |
| 33 | else |
| 34 | |
| 35 | #define get_error_message(errno) \ |
| 36 | int code = errno; \ |
| 37 | foreach_vnet_api_error \ |
| 38 | message = "Reason unknown"; |
| 39 | |
Marek Gradzki | 66ea26b | 2016-07-26 15:28:22 +0200 | [diff] [blame] | 40 | /* shared jvpp main structure */ |
| 41 | jvpp_main_t jvpp_main __attribute__((aligned (64))); |
| 42 | |
| 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; |
| 49 | if (!callbackClass) { |
| 50 | DEBUG_LOG("CallOnError : jm->callbackClass is null!\n"); |
| 51 | return; |
| 52 | } |
| 53 | jmethodID excConstructor = (*env)->GetMethodID(env, callbackExceptionClass, |
Matej Perina | 63a46fc | 2017-07-20 15:35:19 +0200 | [diff] [blame] | 54 | "<init>", "(Ljava/lang/String;Ljava/lang/String;II)V"); |
Marek Gradzki | 66ea26b | 2016-07-26 15:28:22 +0200 | [diff] [blame] | 55 | if (!excConstructor) { |
| 56 | DEBUG_LOG("CallOnError : excConstructor is null!\n"); |
| 57 | return; |
| 58 | } |
| 59 | jmethodID callbackExcMethod = (*env)->GetMethodID(env, callbackClass, |
Marek Gradzki | e85581c | 2016-09-28 10:12:04 +0200 | [diff] [blame] | 60 | "onError", "(Lio/fd/vpp/jvpp/VppCallbackException;)V"); |
Marek Gradzki | 66ea26b | 2016-07-26 15:28:22 +0200 | [diff] [blame] | 61 | if (!callbackExcMethod) { |
| 62 | DEBUG_LOG("CallOnError : callbackExcMethod is null!\n"); |
| 63 | return; |
| 64 | } |
| 65 | |
Matej Perina | 63a46fc | 2017-07-20 15:35:19 +0200 | [diff] [blame] | 66 | char *message; |
| 67 | get_error_message(clib_net_to_host_u32(retval)); |
Marek Gradzki | 66ea26b | 2016-07-26 15:28:22 +0200 | [diff] [blame] | 68 | jobject excObject = (*env)->NewObject(env, callbackExceptionClass, |
| 69 | excConstructor, (*env)->NewStringUTF(env, callName), |
Matej Perina | 63a46fc | 2017-07-20 15:35:19 +0200 | [diff] [blame] | 70 | (*env)->NewStringUTF(env, message), |
Marek Gradzki | 66ea26b | 2016-07-26 15:28:22 +0200 | [diff] [blame] | 71 | clib_net_to_host_u32(contextId), clib_net_to_host_u32(retval)); |
| 72 | if (!excObject) { |
| 73 | DEBUG_LOG("CallOnError : excObject is null!\n"); |
| 74 | return; |
| 75 | } |
| 76 | |
| 77 | (*env)->CallVoidMethod(env, callbackObject, callbackExcMethod, excObject); |
| 78 | DEBUG_LOG("CallOnError : Response sent\n"); |
| 79 | } |
Matej Perina | 63a46fc | 2017-07-20 15:35:19 +0200 | [diff] [blame] | 80 | #undef _ |
Marek Gradzki | 4746a5d | 2017-01-27 08:57:40 +0100 | [diff] [blame] | 81 | |
| 82 | u32 get_message_id(JNIEnv *env, const char *key) { |
| 83 | uword *p = hash_get(jvpp_main.messages_hash, key); |
| 84 | if (!p) { |
| 85 | jclass exClass = (*env)->FindClass(env, "java/lang/IllegalStateException"); |
Marek Gradzki | c7fc97a | 2017-10-16 12:33:12 +0200 | [diff] [blame] | 86 | char *msgBuf = clib_mem_alloc(strlen(key) + 70); |
Marek Gradzki | 4746a5d | 2017-01-27 08:57:40 +0100 | [diff] [blame] | 87 | strcpy(msgBuf, "API mismatch detected: "); |
| 88 | strcat(msgBuf, key); |
Marek Gradzki | c7fc97a | 2017-10-16 12:33:12 +0200 | [diff] [blame] | 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"); |
Marek Gradzki | 4746a5d | 2017-01-27 08:57:40 +0100 | [diff] [blame] | 94 | (*env)->ThrowNew(env, exClass, msgBuf); |
| 95 | clib_mem_free(msgBuf); |
| 96 | return 0; |
| 97 | } |
| 98 | return (u32) p[0]; |
| 99 | } |