summaryrefslogtreecommitdiffstats
path: root/java/jvpp-stats/jvpp_interface_stats.h
blob: 0cc592eff04dc7c6e9a6764189a8e7279f2b9cfc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/*
 * Copyright (c) 2019 Cisco and/or its affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * This file contains JNI bindings for jvpp Java API.
 */
#include <jvpp-common/jvpp_common.h>
#include <vpp-api/client/stat_client.h>

// JAVA class reference cache
jclass interfaceStatisticsDumpClass;
jclass interfaceStatisticsClass;
jclass interfaceStatisticsDetailsClass;
jclass interfaceNamesDumpClass;
jclass interfaceNameClass;
jclass interfaceNamesDetailsClass;
jclass callbackExceptionClass;

typedef struct interface_statistics {
    int context;
    int sw_if_index;
    int rx_errors;
    int rx_bytes;
    int rx_unicast_pkts;
    int rx_broadcast_pkts;
    int rx_multicast_pkts;
    int tx_errors;
    int tx_bytes;
    int tx_unicast_pkts;
    int tx_broadcast_pkts;
    int tx_multicast_pkts;

} vl_api_interface_statistics_details_t;

typedef struct interface_names {
    int context;
    int sw_if_index;
    char *name;
} vl_api_interface_names_details_t;

static int cache_class_references(JNIEnv *env) {

    interfaceStatisticsDumpClass = (jclass) (*env)->NewGlobalRef(env, (*env)->FindClass(env,
                                                                                        "io/fd/jvpp/stats/dto/InterfaceStatisticsDump"));
    if ((*env)->ExceptionCheck(env)) {
        (*env)->ExceptionDescribe(env);
        return JNI_ERR;
    }
    interfaceStatisticsDetailsClass = (jclass) (*env)->NewGlobalRef(env, (*env)->FindClass(env,
                                                                                           "io/fd/jvpp/stats/dto/InterfaceStatisticsDetails"));
    if ((*env)->ExceptionCheck(env)) {
        (*env)->ExceptionDescribe(env);
        return JNI_ERR;
    }
    interfaceStatisticsClass = (jclass) (*env)->NewGlobalRef(env, (*env)->FindClass(env,
                                                                                    "io/fd/jvpp/stats/dto/InterfaceStatistics"));
    if ((*env)->ExceptionCheck(env)) {
        (*env)->ExceptionDescribe(env);
        return JNI_ERR;
    }

    interfaceNamesDumpClass = (jclass) (*env)->NewGlobalRef(env, (*env)->FindClass(env,
                                                                                   "io/fd/jvpp/stats/dto/InterfaceNamesDump"));
    if ((*env)->ExceptionCheck(env)) {
        (*env)->ExceptionDescribe(env);
        return JNI_ERR;
    }
    interfaceNamesDetailsClass = (jclass) (*env)->NewGlobalRef(env, (*env)->FindClass(env,
                                                                                      "io/fd/jvpp/stats/dto/InterfaceNamesDetails"));
    if ((*env)->ExceptionCheck(env)) {
        (*env)->ExceptionDescribe(env);
        return JNI_ERR;
    }
    interfaceNameClass = (jclass) (*env)->NewGlobalRef(env, (*env)->FindClass(env,
                                                                               "io/fd/jvpp/stats/dto/InterfaceName"));
    if ((*env)->ExceptionCheck(env)) {
        (*env)->ExceptionDescribe(env);
        return JNI_ERR;
    }

    callbackExceptionClass = (jclass) (*env)->NewGlobalRef(env,
                                                           (*env)->FindClass(env, "io/fd/jvpp/VppCallbackException"));
    if ((*env)->ExceptionCheck(env)) {
        (*env)->ExceptionDescribe(env);
        return JNI_ERR;
    }
    return 0;
}

static void delete_class_references(JNIEnv *env) {

    if (interfaceStatisticsDumpClass) {
        (*env)->DeleteGlobalRef(env, interfaceStatisticsDumpClass);
    }
    if (interfaceStatisticsDetailsClass) {
        (*env)->DeleteGlobalRef(env, interfaceStatisticsDetailsClass);
    }
    if (interfaceStatisticsClass) {
        (*env)->DeleteGlobalRef(env, interfaceStatisticsClass);
    }
    if (interfaceNamesDumpClass) {
        (*env)->DeleteGlobalRef(env, interfaceNamesDumpClass);
    }
    if (interfaceNamesDetailsClass) {
        (*env)->DeleteGlobalRef(env, interfaceNamesDetailsClass);
    }
    if (interfaceNameClass) {
        (*env)->DeleteGlobalRef(env, interfaceNameClass);
    }
    if (callbackExceptionClass) {
        (*env)->DeleteGlobalRef(env, callbackExceptionClass);
    }
}

/**
 * Handler for interface_statistics_details message.
 */
static void
interface_statistics_details_handler(JNIEnv *env, vl_api_interface_statistics_details_t *ifc_stats, int ifc_count) {
    stats_main_t *plugin_main = &stats_main;
    jthrowable exc;
    if (CLIB_DEBUG > 1)
        clib_warning ("Received interface_statistics_details event message");

    jmethodID constructor = (*env)->GetMethodID(env, interfaceStatisticsDetailsClass, "<init>", "(II)V");

    // User does not have to provide callbacks for all VPP messages.
    // We are ignoring messages that are not supported by user.
    (*env)->ExceptionClear(env); // just in case exception occurred in different place and was not properly cleared
    jmethodID callbackMethod = (*env)->GetMethodID(env, plugin_main->callbackClass, "onInterfaceStatisticsDetails",
                                                   "(Lio/fd/jvpp/stats/dto/InterfaceStatisticsDetails;)V");
    exc = (*env)->ExceptionOccurred(env);
    if (exc) {
        clib_warning(
                "Unable to extract onInterfaceStatisticsDetails method reference from stats plugin's callbackClass. Ignoring message.\n");
        (*env)->ExceptionDescribe(env);
        (*env)->ExceptionClear(env);
        return;
    }
    jobject dto = (*env)->NewObject(env, interfaceStatisticsDetailsClass, constructor, ifc_count, ifc_stats->context);
    jfieldID interfaceStatisticsId = (*env)->GetFieldID(env, interfaceStatisticsDetailsClass, "interfaceStatistics",
                                                        "[Lio/fd/jvpp/stats/dto/InterfaceStatistics;");
    jobject stats_array = (*env)->GetObjectField(env, dto, interfaceStatisticsId);

    jmethodID ifc_stats_constructor = (*env)->GetMethodID(env, interfaceStatisticsClass, "<init>", "(IIIIIIIIIII)V");
    for (int i = 0; i < ifc_count; i++) {

        jobject element = (*env)->NewObject(env, interfaceStatisticsClass, ifc_stats_constructor,
                                            ifc_stats[i].sw_if_index,
                                            ifc_stats[i].tx_errors,
                                            ifc_stats[i].tx_multicast_pkts,
                                            ifc_stats[i].tx_unicast_pkts,
                                            ifc_stats[i].tx_broadcast_pkts,
                                            ifc_stats[i].tx_bytes,
                                            ifc_stats[i].rx_errors,
                                            ifc_stats[i].rx_multicast_pkts,
                                            ifc_stats[i].rx_unicast_pkts,
                                            ifc_stats[i].rx_broadcast_pkts,
                                            ifc_stats[i].rx_bytes);

        (*env)->SetObjectArrayElement(env, stats_array, i, element);
        if ((*env)->ExceptionOccurred(env)) {
            break;
        }
        (*env)->DeleteLocalRef(env, element);
    }
    (*env)->CallVoidMethod(env, plugin_main->callbackObject, callbackMethod, dto);
    if ((*env)->ExceptionOccurred(env)) {
        clib_warning("Unable to call callback for stats plugin's callbackClass.\n");
        (*env)->ExceptionDescribe(env);
        (*env)->ExceptionClear(env);
        return;
    }
    (*env)->DeleteLocalRef(env, stats_array);
    (*env)->DeleteLocalRef(env, dto);
}

/**
 * Handler for interface_names_details message.
 */
static void
interface_names_details_handler(JNIEnv *env, vl_api_interface_names_details_t *ifc_names, int ifc_count) {
    stats_main_t *plugin_main = &stats_main;
    jthrowable exc;
    if (CLIB_DEBUG > 1)
        clib_warning ("Received interface_names_details event message");

    jmethodID constructor = (*env)->GetMethodID(env, interfaceNamesDetailsClass, "<init>", "(II)V");

    // User does not have to provide callbacks for all VPP messages.
    // We are ignoring messages that are not supported by user.
    (*env)->ExceptionClear(env); // just in case exception occurred in different place and was not properly cleared
    jmethodID callbackMethod = (*env)->GetMethodID(env, plugin_main->callbackClass, "onInterfaceNamesDetails",
                                                   "(Lio/fd/jvpp/stats/dto/InterfaceNamesDetails;)V");
    exc = (*env)->ExceptionOccurred(env);
    if (exc) {
        clib_warning(
                "Unable to extract onInterfaceNamesDetails method reference from stats plugin's callbackClass. Ignoring message.\n");
        (*env)->ExceptionDescribe(env);
        (*env)->ExceptionClear(env);
        return;
    }
    jobject dto = (*env)->NewObject(env, interfaceNamesDetailsClass, constructor, ifc_count, ifc_names->context);
    jfieldID interfaceNamesId = (*env)->GetFieldID(env, interfaceNamesDetailsClass, "interfaceNames",
                                                        "[Lio/fd/jvpp/stats/dto/InterfaceName;");
    jobject names_array = (*env)->GetObjectField(env, dto, interfaceNamesId);

    jmethodID ifc_names_constructor = (*env)->GetMethodID(env, interfaceNameClass, "<init>", "(ILjava/lang/String;)V");
    for (int i = 0; i < ifc_count; i++) {
        jstring name = (*env)->NewStringUTF(env, (char *) ifc_names[i].name);
        jobject element = (*env)->NewObject(env, interfaceNameClass, ifc_names_constructor,
                                            ifc_names[i].sw_if_index,
                                            name);

        (*env)->SetObjectArrayElement(env, names_array, i, element);
        if ((*env)->ExceptionOccurred(env)) {
            break;
        }
        (*env)->DeleteLocalRef(env, element);
    }
    (*env)->CallVoidMethod(env, plugin_main->callbackObject, callbackMethod, dto);
    if ((*env)->ExceptionOccurred(env)) {
        clib_warning("Unable to call callback for stats plugin's callbackClass.\n");
        (*env)->ExceptionDescribe(env);
        (*env)->ExceptionClear(env);
        return;
    }
    (*env)->DeleteLocalRef(env, names_array);
    (*env)->DeleteLocalRef(env, dto);
}

static void
set_field(vl_api_interface_statistics_details_t *stats, int index, const char *field_name, int packets, int bytes) {
    if (strcmp(field_name, "/if/rx-error") == 0) {
        stats[index].rx_errors = packets;
    } else if (strcmp(field_name, "/if/tx-error") == 0) {
        stats[index].tx_errors = packets;
    } else if (strcmp(field_name, "/if/tx") == 0) {
        stats[index].tx_bytes = bytes;
    } else if (strcmp(field_name, "/if/tx-multicast") == 0) {
        stats[index].tx_multicast_pkts = packets;
    } else if (strcmp(field_name, "/if/tx-unicast") == 0) {
        stats[index].tx_unicast_pkts = packets;
    } else if (strcmp(field_name, "/if/tx-broadcast") == 0) {
        stats[index].tx_broadcast_pkts = packets;
    } else if (strcmp(field_name, "/if/rx") == 0) {
        stats[index].rx_bytes = bytes;
    } else if (strcmp(field_name, "/if/rx-multicast") == 0) {
        stats[index].rx_multicast_pkts = packets;
    } else if (strcmp(field_name, "/if/rx-unicast") == 0) {
        stats[index].rx_unicast_pkts = packets;
    } else if (strcmp(field_name, "/if/rx-broadcast") == 0) {
        stats[index].rx_broadcast_pkts = packets;
    }
    stats[index].sw_if_index = index;
}

static void
set_names_field(vl_api_interface_names_details_t *ifc_names, int index, const char *field_name, char *name) {
    if (strcmp(field_name, "/if/names") == 0) {
        ifc_names[index].name = name;
    }
    ifc_names[index].sw_if_index = index;
}

static stat_segment_data_t *get_ifc_statistics_dump() {
    u8 **patterns = 0;
    u32 *dir;
    vec_add1(patterns, (u8 *) "/if/rx");
    vec_add1(patterns, (u8 *) "/if/tx");
    dir = stat_segment_ls(patterns);
    return stat_segment_dump(dir);
}

static jint getInterfaceStatisticsDump(JNIEnv *env) {
    stat_segment_data_t *res;
    int i, j, k, interface_count = 0;
    u32 my_context_id = vppjni_get_context_id(&jvpp_main);
    res = get_ifc_statistics_dump();
    if (res == NULL) {
        clib_warning("Interface Statistics dump failed.\n");
        return -1;
    }

    if (vec_len (res) > 0) {
        if ((res[0].simple_counter_vec != 0) && (vec_len (res[0].simple_counter_vec) > 0)) {
            interface_count = vec_len (res[0].simple_counter_vec[0]);
        } else if ((res[0].combined_counter_vec != 0) && (vec_len (res[0].combined_counter_vec) > 0)) {
            interface_count = vec_len (res[0].combined_counter_vec[0]);
        }
    }
    vl_api_interface_statistics_details_t ifc_stats[interface_count];
    memset(ifc_stats, 0, interface_count * sizeof(vl_api_interface_statistics_details_t));
    ifc_stats->context = my_context_id;

    for (i = 0; i < vec_len (res); i++) {
        switch (res[i].type) {
            case STAT_DIR_TYPE_COUNTER_VECTOR_SIMPLE:
                if (res[i].simple_counter_vec == 0)
                    continue;
                for (k = 0; k < vec_len (res[i].simple_counter_vec); k++)
                    for (j = 0; j < vec_len (res[i].simple_counter_vec[k]); j++) {
                        set_field(ifc_stats, j, res[i].name, res[i].simple_counter_vec[k][j], 0);
                    }
                break;

            case STAT_DIR_TYPE_COUNTER_VECTOR_COMBINED:
                if (res[i].combined_counter_vec == 0)
                    continue;
                for (k = 0; k < vec_len (res[i].combined_counter_vec); k++)
                    for (j = 0; j < vec_len (res[i].combined_counter_vec[k]); j++) {
                        set_field(ifc_stats, j, res[i].name, res[i].combined_counter_vec[k][j].packets,
                                  res[i].combined_counter_vec[k][j].bytes);
                    }
                break;

            default:;
        }
    }
    stat_segment_data_free(res);
    interface_statistics_details_handler(env, ifc_stats, interface_count);
    return ifc_stats->context;
}

static stat_segment_data_t *get_ifc_names_dump() {
    u8 **patterns = 0;
    u32 *dir;
    vec_add1(patterns, (u8 *) "/if/names");
    dir = stat_segment_ls(patterns);
    return stat_segment_dump(dir);
}

static jint getInterfaceNamesDump(JNIEnv *env) {
    stat_segment_data_t *res;
    int i, k, interface_count = 0;
    u32 my_context_id = vppjni_get_context_id(&jvpp_main);
    res = get_ifc_names_dump();
    if (res == NULL) {
        clib_warning("Interface Names dump failed.\n");
        return -1;
    }

    if (vec_len (res) > 0) {
        if ((res[0].name_vector != 0) && (vec_len (res[0].name_vector) > 0)) {
            interface_count = vec_len (res[0].name_vector);
        }
    }
    vl_api_interface_names_details_t ifc_names[interface_count];
    memset(ifc_names, 0, interface_count * sizeof(vl_api_interface_names_details_t));
    int length = 0;
    for (i = 0; i < vec_len (res); i++) {
        switch (res[i].type) {
            case STAT_DIR_TYPE_NAME_VECTOR:
                if (res[i].name_vector == 0)
                    continue;
                for (k = 0; k < interface_count; k++)
                    if (res[i].name_vector[k]) {
                        set_names_field(ifc_names, k, res[i].name, (char *) res[i].name_vector[k]);
                        length ++;
                    }
                break;

            default:;
        }
    }
    vl_api_interface_names_details_t ifc_names_reduced[length];
    memset(ifc_names_reduced, 0, length * sizeof(vl_api_interface_names_details_t));
    ifc_names_reduced->context = my_context_id;
    // skip null entries
    int index = 0;
    for (int j = 0; j < interface_count; ++j) {
        if (ifc_names[j].name != NULL) {
            ifc_names_reduced[index].sw_if_index = ifc_names[j].sw_if_index;
            ifc_names_reduced[index].name = ifc_names[j].name;
            index ++;
        }
    }
    stat_segment_data_free(res);
    interface_names_details_handler(env, ifc_names_reduced, length);
    return ifc_names_reduced->context;
}