blob: bd0683ae2c5c66ec938a995a178406bddca926f9 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * Copyright (c) 2015 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#include <japi/vppjni_bridge_domain.h>
25
26typedef struct {
27 u8 * name;
28 u32 value;
29} name_sort_t;
30
31typedef struct {
32 u8 valid; // used in a vector of sw_interface_details_t
33
34 u8 interface_name[64];
35 u32 sw_if_index;
36 u32 sup_sw_if_index;
37 u32 l2_address_length;
38 u8 l2_address[8];
39 u8 admin_up_down;
40 u8 link_up_down;
41 u8 link_duplex;
42 u8 link_speed;
Pavel84e4ffe2016-02-17 15:10:04 +010043 u16 link_mtu;
Ed Warnickecb9cada2015-12-08 15:45:58 -070044 u32 sub_id;
45 u8 sub_dot1ad;
46 u8 sub_number_of_tags;
47 u16 sub_outer_vlan_id;
48 u16 sub_inner_vlan_id;
49 u8 sub_exact_match;
50 u8 sub_default;
51 u8 sub_outer_vlan_id_any;
52 u8 sub_inner_vlan_id_any;
53 u32 vtr_op;
54 u32 vtr_push_dot1q;
55 u32 vtr_tag1;
56 u32 vtr_tag2;
57} sw_interface_details_t;
58
59typedef struct {
60 u8 * interface_name;
61 u32 sw_if_index;
62 /*
63 * Subinterface ID. A number 0-N to uniquely identify
64 * this subinterface under the super interface
65 */
66 u32 sub_id;
67
68 /* 0 = dot1q, 1=dot1ad */
69 u8 sub_dot1ad;
70
71 /* Number of tags 0-2 */
72 u8 sub_number_of_tags;
73 u16 sub_outer_vlan_id;
74 u16 sub_inner_vlan_id;
75 u8 sub_exact_match;
76 u8 sub_default;
77 u8 sub_outer_vlan_id_any;
78 u8 sub_inner_vlan_id_any;
79
80 /* vlan tag rewrite */
81 u32 vtr_op;
82 u32 vtr_push_dot1q;
83 u32 vtr_tag1;
84 u32 vtr_tag2;
85} sw_interface_subif_t;
86
87typedef struct {
Dave Wallacebf8c15e2015-12-17 20:54:54 -050088 u8 *desc;
89} sw_if_config_t;
90
91typedef struct {
92 u32 ip;
93 u8 prefix_length;
94} ipv4_address_t;
95
96typedef struct {
97 u8 ip[16];
98 u8 prefix_length;
99} ipv6_address_t;
100
101typedef struct {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700102 u64 ip4;
103 u64 ip6;
104 u64 unicast;
105 u64 multicast;
106 u64 broadcast;
107 u64 discard;
108 u64 fifo_full;
109 u64 error;
110 u64 unknown_proto;
111 u64 miss;
112} packet_counters_t;
113
114typedef struct {
115 u64 octets;
116 packet_counters_t pkts;
117} if_counters_t;
118
119typedef struct {
120 u8 valid;
121 u32 sw_if_index;
122 if_counters_t rx;
123 if_counters_t tx;
124} sw_interface_stats_t;
125
Dave Wallacebf8c15e2015-12-17 20:54:54 -0500126typedef struct {
127 u32 src_address;
128 u32 dst_address;
129 u32 encap_vrf_id;
130 u32 vni;
131 u32 decap_next_index;
132} vxlan_tunnel_details_t;
133
Ed Warnickecb9cada2015-12-08 15:45:58 -0700134
135typedef struct {
136 /* Context IDs */
137 volatile u32 context_id_sent;
138 volatile u32 context_id_received;
139
140 /* Spinlock */
141 volatile u32 lock;
142 u32 tag;
143
Damjan Marion08ff7e02016-01-20 13:45:36 +0100144 /* To recycle pseudo-synchronous message code from vpp_api_test... */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700145 volatile u32 result_ready;
146 volatile i32 retval;
147 volatile u8 *shmem_result;
148
149 /* thread cleanup */
150 pthread_key_t cleanup_rx_thread_key;
151 /* attachment of rx thread to java thread */
152 JNIEnv *jenv;
153 JavaVM *jvm;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700154 uword *callback_hash; // map context_id => jobject
155 uword *ping_hash; // map ping context_id => msg type called
156
157 /* Timestamp */
158 clib_time_t clib_time;
159
160 /* connected indication */
161 u8 is_connected;
162
163 /* context -> non-trivial reply hash */
164 uword * reply_hash;
165 u32 saved_reply_count;
166
167 /* interface name map */
168 uword * sw_if_index_by_interface_name;
169
170 /* interface counters */
171 sw_interface_stats_t * sw_if_stats_by_sw_if_index;
172
173 /* interface table */
174 sw_interface_details_t * sw_if_table;
175
Dave Wallacebf8c15e2015-12-17 20:54:54 -0500176 uword * sw_if_config_by_sw_if_index;
177
Ed Warnickecb9cada2015-12-08 15:45:58 -0700178 /* interface indices of responses to one sw_if_dump request */
179 u8 collect_indices;
180 u32 * sw_if_dump_if_indices;
181
Damjan Mariona0d4a1a2015-12-12 14:40:59 +0100182 /* program name, build_dir, version */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700183 u8 program_name[32];
184 u8 build_directory[256];
Damjan Mariona0d4a1a2015-12-12 14:40:59 +0100185 u8 version[32];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700186 u8 build_date[32];
187
188 /* subinterface table */
189 sw_interface_subif_t * sw_if_subif_table;
190
Dave Wallacebf8c15e2015-12-17 20:54:54 -0500191 /* used in ip_address_dump request and response handling */
192 ipv4_address_t *ipv4_addresses;
193 ipv6_address_t *ipv6_addresses;
194 u8 is_ipv6;
195
196 /* used in vxlan_tunnel_dump request and response handling */
197 vxlan_tunnel_details_t *vxlan_tunnel_details;
198
Ed Warnickecb9cada2015-12-08 15:45:58 -0700199 /* main heap */
200 u8 * heap;
201
202 /* convenience */
203 unix_shared_memory_queue_t * vl_input_queue;
204 api_main_t * api_main;
205 u32 my_client_index;
206
207 vjbd_main_t vjbd_main;
208} vppjni_main_t;
209
210vppjni_main_t vppjni_main __attribute__((aligned (64)));
211
212
213static inline u32 vppjni_get_context_id (vppjni_main_t * jm)
214{
215 u32 my_context_id;
216 my_context_id = __sync_add_and_fetch (&jm->context_id_sent, 1);
217 return my_context_id;
218}
219
220static inline void vppjni_lock (vppjni_main_t * jm, u32 tag)
221{
222 while (__sync_lock_test_and_set (&jm->lock, 1))
223 ;
224 jm->tag = tag;
225}
226
227static inline void vppjni_unlock (vppjni_main_t * jm)
228{
229 jm->tag = 0;
230 CLIB_MEMORY_BARRIER();
231 jm->lock = 0;
232}
233
234static inline f64 vppjni_time_now (vppjni_main_t *jm)
235{
236 return clib_time_now (&jm->clib_time);
237}
238
239static inline int vppjni_sanity_check (vppjni_main_t * jm)
240{
241 if (!jm->is_connected)
242 return VNET_API_ERROR_NOT_CONNECTED;
243 return 0;
244}
245
246#define __PACKED(x) x __attribute__((packed))
247
248typedef __PACKED(struct _vl_api_generic_reply {
249 u16 _vl_msg_id;
250 u32 context;
251 i32 retval;
252 u8 data[0];
253}) vl_api_generic_reply_t;
254
255void vl_api_generic_reply_handler (vl_api_generic_reply_t *mp);
256
257/* M: construct, but don't yet send a message */
258
259#define M(T,t) \
260do { \
261 jm->result_ready = 0; \
262 mp = vl_msg_api_alloc(sizeof(*mp)); \
263 memset (mp, 0, sizeof (*mp)); \
264 mp->_vl_msg_id = ntohs (VL_API_##T); \
265 mp->client_index = jm->my_client_index; \
266 } while(0);
267
268#define M2(T,t,n) \
269do { \
270 jm->result_ready = 0; \
271 mp = vl_msg_api_alloc(sizeof(*mp)+(n)); \
272 memset (mp, 0, sizeof (*mp)); \
273 mp->_vl_msg_id = ntohs (VL_API_##T); \
274 mp->client_index = jm->my_client_index; \
275 } while(0);
276
277
278/* S: send a message */
279#define S (vl_msg_api_send_shmem (jm->vl_input_queue, (u8 *)&mp))
280
281/* W: wait for results, with timeout */
282#define W \
283 do { \
284 timeout = vppjni_time_now (jm) + 1.0; \
285 \
286 while (vppjni_time_now (jm) < timeout) { \
287 if (jm->result_ready == 1) { \
288 return (jm->retval); \
289 } \
290 } \
291 return -99; \
292} while(0);
293
294/* WNR: wait for results, with timeout (without returning) */
295#define WNR \
296 do { \
297 timeout = vppjni_time_now (jm) + 1.0; \
298 \
299 rv = -99; \
300 while (vppjni_time_now (jm) < timeout) { \
301 if (jm->result_ready == 1) { \
302 rv = (jm->retval); \
303 break; \
304 } \
305 } \
306} while(0);
307
308#endif /* __included_vppjni_h__ */