Marek Gradzki | d85036f | 2016-04-26 12:09:05 +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 | #ifndef __included_vppjni_h__ |
| 16 | #define __included_vppjni_h__ |
| 17 | |
| 18 | #include <vnet/vnet.h> |
| 19 | #include <vnet/ip/ip.h> |
| 20 | #include <vnet/api_errno.h> |
| 21 | #include <vlibapi/api.h> |
| 22 | #include <vlibmemory/api.h> |
| 23 | #include <jni.h> |
| 24 | |
| 25 | typedef struct { |
| 26 | /* Unique identifier used for matching replays with requests */ |
| 27 | volatile u32 context_id; |
| 28 | |
| 29 | /* Spinlock */ |
| 30 | volatile u32 lock; |
| 31 | u32 tag; |
| 32 | |
| 33 | /* Used for first control ping */ |
| 34 | // TODO better names? |
| 35 | volatile u32 result_ready; |
| 36 | volatile i32 retval; |
| 37 | |
| 38 | /* JNI Native Method Interface pointer for message handlers */ |
| 39 | JNIEnv *jenv; |
| 40 | |
| 41 | /* thread cleanup */ |
| 42 | pthread_key_t cleanup_rx_thread_key; |
| 43 | |
| 44 | /* JNI Invoke Interface pointer for attachment of rx thread to java thread */ |
| 45 | JavaVM *jvm; |
| 46 | |
| 47 | /* Callback object and class references enabling asynchronous Java calls */ |
| 48 | jobject callback; |
| 49 | jclass callbackClass; |
| 50 | |
| 51 | /* Connected indication */ |
| 52 | volatile u8 is_connected; |
| 53 | |
| 54 | /* Convenience */ |
| 55 | unix_shared_memory_queue_t * vl_input_queue; |
| 56 | u32 my_client_index; |
| 57 | |
| 58 | } vppjni_main_t; |
| 59 | |
| 60 | vppjni_main_t vppjni_main __attribute__((aligned (64))); |
| 61 | |
| 62 | static inline u32 vppjni_get_context_id (vppjni_main_t * jm) |
| 63 | { |
| 64 | return __sync_add_and_fetch (&jm->context_id, 1); |
| 65 | } |
| 66 | |
| 67 | static inline void vppjni_lock (vppjni_main_t * jm, u32 tag) |
| 68 | { |
| 69 | while (__sync_lock_test_and_set (&jm->lock, 1)) |
| 70 | ; |
| 71 | jm->tag = tag; |
| 72 | } |
| 73 | |
| 74 | static inline void vppjni_unlock (vppjni_main_t * jm) |
| 75 | { |
| 76 | jm->tag = 0; |
| 77 | CLIB_MEMORY_BARRIER(); |
| 78 | jm->lock = 0; |
| 79 | } |
| 80 | |
| 81 | static inline int vppjni_sanity_check (vppjni_main_t * jm) |
| 82 | { |
| 83 | if (!jm->is_connected) |
| 84 | return VNET_API_ERROR_NOT_CONNECTED; |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | // TODO remove macros (code is now fully autogenerated) |
| 89 | |
| 90 | /* M: construct, but don't yet send a message */ |
| 91 | #define M(T,t) \ |
| 92 | do { \ |
| 93 | jm->result_ready = 0; \ |
| 94 | mp = vl_msg_api_alloc(sizeof(*mp)); \ |
| 95 | memset (mp, 0, sizeof (*mp)); \ |
| 96 | mp->_vl_msg_id = ntohs (VL_API_##T); \ |
| 97 | mp->client_index = jm->my_client_index; \ |
| 98 | } while(0); |
| 99 | |
| 100 | #define M2(T,t,n) \ |
| 101 | do { \ |
| 102 | jm->result_ready = 0; \ |
| 103 | mp = vl_msg_api_alloc(sizeof(*mp)+(n)); \ |
| 104 | memset (mp, 0, sizeof (*mp)); \ |
| 105 | mp->_vl_msg_id = ntohs (VL_API_##T); \ |
| 106 | mp->client_index = jm->my_client_index; \ |
| 107 | } while(0); |
| 108 | |
| 109 | /* S: send a message */ |
| 110 | #define S (vl_msg_api_send_shmem (jm->vl_input_queue, (u8 *)&mp)) |
| 111 | |
| 112 | #endif /* __included_vppjni_h__ */ |