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