2 * Copyright (c) 2016 Cisco and/or its affiliates.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package io.fd.vpp.jvpp;
19 import static io.fd.vpp.jvpp.NativeLibraryLoader.loadLibrary;
20 import static java.lang.String.format;
22 import java.io.IOException;
23 import java.nio.file.Files;
24 import java.nio.file.Paths;
25 import java.util.HashMap;
27 import java.util.Objects;
28 import java.util.logging.Level;
29 import java.util.logging.Logger;
32 * JNI based representation of a management connection to VPP.
34 public final class VppJNIConnection implements VppConnection {
35 private static final Logger LOG = Logger.getLogger(VppJNIConnection.class.getName());
36 private static final String DEFAULT_SHM_PREFIX = "/vpe-api";
39 final String libName = "libjvpp_registry.so";
41 loadLibrary(libName, VppJNIConnection.class);
42 } catch (IOException e) {
43 LOG.log(Level.SEVERE, format("Can't find vpp jni library: %s", libName), e);
44 throw new ExceptionInInitializerError(e);
48 private ConnectionInfo connectionInfo;
50 private final String clientName;
51 private final String shmPrefix;
52 private volatile boolean disconnected = false;
55 * Create VPPJNIConnection instance for client connecting to VPP.
57 * @param clientName client name instance to be used for communication. Single connection per clientName is
60 public VppJNIConnection(final String clientName) {
61 this.clientName = Objects.requireNonNull(clientName, "Null clientName");
62 this.shmPrefix = DEFAULT_SHM_PREFIX;
65 public VppJNIConnection(final String clientName, final String shmPrefix) {
66 this.clientName = Objects.requireNonNull(clientName, "Null clientName");
67 this.shmPrefix = Objects.requireNonNull(shmPrefix, "Null shmPrefix");
71 * Guarded by VppJNIConnection.class
73 private static final Map<String, VppJNIConnection> connections = new HashMap<>();
76 * Initiate VPP connection for current instance
78 * Multiple instances are allowed since this class is not a singleton (VPP allows multiple management connections).
80 * However only a single connection per clientName is allowed.
82 * @throws IOException in case the connection could not be established
86 public void connect() throws IOException {
90 private void _connect(final String shmPrefix) throws IOException {
91 Objects.requireNonNull(shmPrefix, "Shared memory prefix must be defined");
93 synchronized (VppJNIConnection.class) {
94 if (connections.containsKey(clientName)) {
95 throw new IOException("Client " + clientName + " already connected");
98 connectionInfo = clientConnect(shmPrefix, clientName);
99 if (connectionInfo.status != 0) {
100 throw new IOException("Connection returned error " + connectionInfo.status);
102 connections.put(clientName, this);
107 public final void checkActive() {
109 throw new IllegalStateException("Disconnected client " + clientName);
114 public final synchronized void close() {
120 synchronized (VppJNIConnection.class) {
121 connections.remove(clientName);
127 public ConnectionInfo getConnectionInfo() {
128 return connectionInfo;
132 * VPP connection information used by plugins to reuse the connection.
134 public static final class ConnectionInfo {
135 public final long queueAddress;
136 public final int clientIndex;
137 public final int status; // FIXME throw exception instead
138 public final int pid;
140 public ConnectionInfo(long queueAddress, int clientIndex, int status, int pid) {
141 this.queueAddress = queueAddress;
142 this.clientIndex = clientIndex;
143 this.status = status;
148 private static native ConnectionInfo clientConnect(String shmPrefix, String clientName);
150 private static native void clientDisconnect();