diff options
Diffstat (limited to 'nsh-plugin/java/jvpp')
-rw-r--r-- | nsh-plugin/java/jvpp/jvpp_nsh.c | 114 | ||||
-rw-r--r-- | nsh-plugin/java/jvpp/jvpp_nsh.h | 44 | ||||
-rw-r--r-- | nsh-plugin/java/jvpp/pom.xml | 62 | ||||
-rw-r--r-- | nsh-plugin/java/jvpp/test/JVppNshTest.java | 94 | ||||
-rw-r--r-- | nsh-plugin/java/jvpp/test/NshFutureApiTest.java | 89 |
5 files changed, 403 insertions, 0 deletions
diff --git a/nsh-plugin/java/jvpp/jvpp_nsh.c b/nsh-plugin/java/jvpp/jvpp_nsh.c new file mode 100644 index 0000000..a84e02e --- /dev/null +++ b/nsh-plugin/java/jvpp/jvpp_nsh.c @@ -0,0 +1,114 @@ +/* + * Copyright (c) 2016 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. + */ + +#include <vnet/vnet.h> + +#include <vpp-api/nsh_msg_enum.h> +#define vl_typedefs /* define message structures */ +#include <vpp-api/nsh_all_api_h.h> +#undef vl_typedefs + +#define vl_endianfun +#include <vpp-api/nsh_all_api_h.h> +#undef vl_endianfun + +#define vl_print(handle, ...) +#define vl_printfun +#include <vpp-api/nsh_all_api_h.h> +#undef vl_printfun + +/* Get the API version number */ +#define vl_api_version(n,v) static u32 api_version=(v); +#include <vpp-api/nsh_all_api_h.h> +#undef vl_api_version + +#include <vnet/api_errno.h> +#include <vlibapi/api.h> +#include <vlibmemory/api.h> + +#include <jvpp-common/jvpp_common.h> + +// FIXME use less fragile path +#include "../build/java/jvpp/org_openvpp_jvpp_nsh_JVppNshImpl.h" +#include "jvpp_nsh.h" +#include "./build/java/jvpp/jvpp_nsh_gen.h" + +/* + * Class: org_openvpp_jvpp_nsh_JVppNshImpl + * Method: init0 + * Signature: (JI)V + */ +JNIEXPORT void JNICALL Java_org_openvpp_jvpp_nsh_JVppNshImpl_init0 + (JNIEnv *env, jclass clazz, jobject callback, jlong queue_address, jint my_client_index) { + nsh_main_t * plugin_main = &nsh_main; + u8 * name; + clib_warning ("Java_org_openvpp_jvpp_nsh_JVppNshImpl_init0"); + + plugin_main->my_client_index = my_client_index; + plugin_main->vl_input_queue = (unix_shared_memory_queue_t *)queue_address; + + name = format (0, "nsh_%08x%c", api_version, 0); + plugin_main->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name); + + plugin_main->callbackObject = (*env)->NewGlobalRef(env, callback); + plugin_main->callbackClass = (jclass)(*env)->NewGlobalRef(env, (*env)->GetObjectClass(env, callback)); + + #define _(N,n) \ + vl_msg_api_set_handlers(VL_API_##N + plugin_main->msg_id_base, #n, \ + vl_api_##n##_t_handler, \ + vl_noop_handler, \ + vl_api_##n##_t_endian, \ + vl_api_##n##_t_print, \ + sizeof(vl_api_##n##_t), 1); + foreach_api_reply_handler; + #undef _ +} + +JNIEXPORT void JNICALL Java_org_openvpp_jvpp_nsh_JVppNshImpl_close0 +(JNIEnv *env, jclass clazz) { + nsh_main_t * plugin_main = &nsh_main; + + // cleanup: + (*env)->DeleteGlobalRef(env, plugin_main->callbackClass); + (*env)->DeleteGlobalRef(env, plugin_main->callbackObject); + + plugin_main->callbackClass = NULL; + plugin_main->callbackObject = NULL; +} + +jint JNI_OnLoad(JavaVM *vm, void *reserved) { + JNIEnv* env; + + if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_8) != JNI_OK) { + return JNI_EVERSION; + } + + if (cache_class_references(env) != 0) { + clib_warning ("Failed to cache class references\n"); + return JNI_ERR; + } + + return JNI_VERSION_1_8; +} + +void JNI_OnUnload(JavaVM *vm, void *reserved) { + JNIEnv* env; + if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_8) != JNI_OK) { + return; + } + delete_class_references(env); +} + + diff --git a/nsh-plugin/java/jvpp/jvpp_nsh.h b/nsh-plugin/java/jvpp/jvpp_nsh.h new file mode 100644 index 0000000..1f9ab58 --- /dev/null +++ b/nsh-plugin/java/jvpp/jvpp_nsh.h @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2016 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. + */ +#ifndef __included_jvpp_nsh_h__ +#define __included_jvpp_nsh_h__ + +#include <vnet/vnet.h> +#include <vnet/ip/ip.h> +#include <vnet/api_errno.h> +#include <vlibapi/api.h> +#include <vlibmemory/api.h> +#include <jni.h> + +typedef struct { + /* Base message index for the nsh plugin */ + u16 msg_id_base; + + /* Pointer to shared memory queue */ + unix_shared_memory_queue_t * vl_input_queue; + + /* VPP api client index */ + u32 my_client_index; + + /* Callback object and class references enabling asynchronous Java calls */ + jobject callbackObject; + jclass callbackClass; + +} nsh_main_t; + +nsh_main_t nsh_main __attribute__((aligned (64))); + + +#endif /* __included_jvpp_nsh_h__ */ diff --git a/nsh-plugin/java/jvpp/pom.xml b/nsh-plugin/java/jvpp/pom.xml new file mode 100644 index 0000000..7c59045 --- /dev/null +++ b/nsh-plugin/java/jvpp/pom.xml @@ -0,0 +1,62 @@ +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <groupId>io.fd.vpp</groupId> + <artifactId>nsh-sfc</artifactId> + <version>16.09-SNAPSHOT</version> + <packaging>jar</packaging> + + <name>nsh-sfc</name> + <url>https://wiki.fd.io/view/NSH_SFC</url> + + <dependencies> + <dependency> + <groupId>io.fd.vpp</groupId> + <artifactId>jvpp-registry</artifactId> + <version>16.09-SNAPSHOT</version> + </dependency> + </dependencies> + + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-compiler-plugin</artifactId> + <version>3.5.1</version> + <configuration> + <source>1.8</source> + <target>1.8</target> + </configuration> + </plugin> + + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-antrun-plugin</artifactId> + <version>1.8</version> + <executions> + <execution> + <phase>process-classes</phase> + <goals> + <goal>run</goal> + </goals> + <configuration> + <exportAntProperties>true</exportAntProperties> + <target> + <property name="runtime_classpath" refid="maven.compile.classpath"/> + <exec executable="javah"> + <arg value="-cp"/> + <arg value="${runtime_classpath}"/> + <arg value="-d"/> + <arg value="${basedir}"/> + <arg value="org.openvpp.jvpp.nsh.JVppNshImpl"/> + </exec> + </target> + </configuration> + </execution> + </executions> + </plugin> + </plugins> + </build> + +</project> diff --git a/nsh-plugin/java/jvpp/test/JVppNshTest.java b/nsh-plugin/java/jvpp/test/JVppNshTest.java new file mode 100644 index 0000000..2a12c56 --- /dev/null +++ b/nsh-plugin/java/jvpp/test/JVppNshTest.java @@ -0,0 +1,94 @@ +/* + * Copyright (c) 2016 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. + */ + +package org.openvpp.jvpp.nsh.test; + +import org.openvpp.jvpp.JVpp; +import org.openvpp.jvpp.JVppRegistry; +import org.openvpp.jvpp.JVppRegistryImpl; +import org.openvpp.jvpp.VppCallbackException; +import org.openvpp.jvpp.VppJNIConnection; + +import org.openvpp.jvpp.nsh.dto.*; +import org.openvpp.jvpp.nsh.callback.*; +import org.openvpp.jvpp.nsh.*; + +/** + * Tests the Nsh Jvpp wrapper (callback API). Run using: + * + * sudo java -cp path/to/jvpp-registry-16.09-SNAPSHOT.jar:path/to/nsh-sfc-16.09-SNAPSHOT.jar org.openvpp.jvpp.nsh.test.JVppNshTest + */ +public class JVppNshTest { + + static class TestCallback implements NshAddDelEntryCallback, NshEntryCallback { + + @Override + public void onNshAddDelEntryReply(final NshAddDelEntryReply msg) { + System.out.printf("Received NshAddDelEntryReply: context=%d\n", msg.context); + } + @Override + public void onNshEntryDetails(final NshEntryDetails msg) { + System.out.printf("Received NshEntryDetails: context=%d, nspNsi=%d, mdType=%d, verOC=%d, length=%d, nextProtocol=%d\n", + msg.context, msg.nspNsi, msg.mdType, msg.verOC, msg.length, msg.nextProtocol); + } + + @Override + public void onError(VppCallbackException ex) { + System.out.printf("Received onError exception: call=%s, context=%d, retval=%d\n", ex.getMethodName(), ex.getCtxId(), ex.getErrorCode()); + } + } + + public static void main(String[] args) throws Exception { + testJVppNsh(); + } + + private static void testJVppNsh() throws Exception { + System.out.println("Testing Java callback API with JVppRegistry"); + JVppRegistry registry = new JVppRegistryImpl("NshTest"); + JVpp jvpp = new JVppNshImpl(); + + registry.register(jvpp, new TestCallback()); + + System.out.println("Sending NshAddDelEntryReply request..."); + final NshAddDelEntry request = new NshAddDelEntry(); + + request.isAdd = 1; + int nsp = 1; + int nsi = 2; + request.nspNsi = (nsp<<8) | nsi; + request.mdType = 1; + request.verOC = 0; + request.length = 6; + request.nextProtocol = 1; + + int result = jvpp.send(request); + System.out.printf("NshAddDelEntryReply send result = %d\n", result); + + Thread.sleep(2000); + + + System.out.println("Sending NshEntryDump request..."); + result = jvpp.send(new NshEntryDump()); + System.out.printf("NshEntryDump send result = %d\n", result); + + + Thread.sleep(2000); + + System.out.println("Disconnecting..."); + registry.close(); + Thread.sleep(1000); + } +} diff --git a/nsh-plugin/java/jvpp/test/NshFutureApiTest.java b/nsh-plugin/java/jvpp/test/NshFutureApiTest.java new file mode 100644 index 0000000..26f4276 --- /dev/null +++ b/nsh-plugin/java/jvpp/test/NshFutureApiTest.java @@ -0,0 +1,89 @@ +/* + * Copyright (c) 2016 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. + */ + +package org.openvpp.jvpp.nsh.test; + +import java.util.concurrent.Future; +import java.util.Objects; + +import org.openvpp.jvpp.JVpp; +import org.openvpp.jvpp.JVppRegistry; +import org.openvpp.jvpp.JVppRegistryImpl; +import org.openvpp.jvpp.VppCallbackException; +import org.openvpp.jvpp.VppJNIConnection; + +import org.openvpp.jvpp.nsh.dto.*; +import org.openvpp.jvpp.nsh.callback.*; +import org.openvpp.jvpp.nsh.*; +import org.openvpp.jvpp.nsh.future.FutureJVppNshFacade; + +/** + * Tests the Nsh Jvpp wrapper (future API). Run using: + * + * sudo java -cp path/to/jvpp-registry-16.09-SNAPSHOT.jar:path/to/nsh-sfc-16.09-SNAPSHOT.jar org.openvpp.jvpp.nsh.test.NshFutureApiTest + */ +public class NshFutureApiTest { + + public static void main(String[] args) throws Exception { + testFutureApi(); + } + + private static void testFutureApi() throws Exception { + System.out.println("Testing Java future API for NSH plugin"); + JVppRegistry registry = new JVppRegistryImpl("NshFutureApiTest"); + JVpp jvpp = new JVppNshImpl(); + FutureJVppNshFacade jvppFacade = new FutureJVppNshFacade(registry, jvpp); + + + testNshAddDelEntry(jvppFacade); + testNshEntryDump(jvppFacade); + + System.out.println("Disconnecting..."); + registry.close(); + Thread.sleep(1000); + } + + private static void testNshAddDelEntry(final FutureJVppNshFacade jvppFacade) throws Exception { + System.out.println("Sending NshAddDelEntry request..."); + final NshAddDelEntry request = new NshAddDelEntry(); + + request.isAdd = 1; + int nsp = 1; + int nsi = 2; + request.nspNsi = (nsp<<8) | nsi; + request.mdType = 1; + request.verOC = 0; + request.length = 6; + request.nextProtocol = 1; + + final Future<NshAddDelEntryReply> replyFuture = jvppFacade.nshAddDelEntry(request).toCompletableFuture(); + final NshAddDelEntryReply reply = replyFuture.get(); + + System.out.printf("Received NshAddDelEntryReply: context=%d\n", reply.context); + } + + private static void testNshEntryDump(final FutureJVppNshFacade jvppFacade) throws Exception { + System.out.println("Sending NshEntryDump request..."); + final Future<NshEntryDetailsReplyDump> replyFuture = jvppFacade.nshEntryDump(new NshEntryDump()).toCompletableFuture(); + final NshEntryDetailsReplyDump reply = replyFuture.get(); + for (NshEntryDetails details : reply.nshEntryDetails) { + Objects.requireNonNull(details, "reply.nshEntryDetails contains null element!"); + System.out.printf("Received NshEntryDetails: context=%d, nspNsi=%d, mdType=%d, verOC=%d, length=%d, nextProtocol=%d\n", + details.context, details.nspNsi, details.mdType, details.verOC, details.length, details.nextProtocol); + } + } + +} |