blob: 2425607c582f261e516083325a275c881280379b [file] [log] [blame]
Marek Gradzki66ea26b2016-07-26 15:28:22 +02001/*
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 Perina63a46fc2017-07-20 15:35:19 +020017#include <vnet/api_errno.h>
Marek Gradzki66ea26b2016-07-26 15:28:22 +020018#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 Perina63a46fc2017-07-20 15:35:19 +020030#define _(error,errorCode,msg) \
31if (errorCode == code) \
32 message = msg; \
33else
34
35#define get_error_message(errno) \
36int code = errno; \
37foreach_vnet_api_error \
38 message = "Reason unknown";
39
Marek Gradzki66ea26b2016-07-26 15:28:22 +020040/* shared jvpp main structure */
41jvpp_main_t jvpp_main __attribute__((aligned (64)));
42
43void 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 Perina63a46fc2017-07-20 15:35:19 +020054 "<init>", "(Ljava/lang/String;Ljava/lang/String;II)V");
Marek Gradzki66ea26b2016-07-26 15:28:22 +020055 if (!excConstructor) {
56 DEBUG_LOG("CallOnError : excConstructor is null!\n");
57 return;
58 }
59 jmethodID callbackExcMethod = (*env)->GetMethodID(env, callbackClass,
Marek Gradzkie85581c2016-09-28 10:12:04 +020060 "onError", "(Lio/fd/vpp/jvpp/VppCallbackException;)V");
Marek Gradzki66ea26b2016-07-26 15:28:22 +020061 if (!callbackExcMethod) {
62 DEBUG_LOG("CallOnError : callbackExcMethod is null!\n");
63 return;
64 }
65
Matej Perina63a46fc2017-07-20 15:35:19 +020066 char *message;
67 get_error_message(clib_net_to_host_u32(retval));
Marek Gradzki66ea26b2016-07-26 15:28:22 +020068 jobject excObject = (*env)->NewObject(env, callbackExceptionClass,
69 excConstructor, (*env)->NewStringUTF(env, callName),
Matej Perina63a46fc2017-07-20 15:35:19 +020070 (*env)->NewStringUTF(env, message),
Marek Gradzki66ea26b2016-07-26 15:28:22 +020071 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 Perina63a46fc2017-07-20 15:35:19 +020080#undef _
Marek Gradzki4746a5d2017-01-27 08:57:40 +010081
82u32 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 Gradzkic7fc97a2017-10-16 12:33:12 +020086 char *msgBuf = clib_mem_alloc(strlen(key) + 70);
Marek Gradzki4746a5d2017-01-27 08:57:40 +010087 strcpy(msgBuf, "API mismatch detected: ");
88 strcat(msgBuf, key);
Marek Gradzkic7fc97a2017-10-16 12:33:12 +020089 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 Gradzki4746a5d2017-01-27 08:57:40 +010094 (*env)->ThrowNew(env, exClass, msgBuf);
95 clib_mem_free(msgBuf);
96 return 0;
97 }
98 return (u32) p[0];
99}