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.jvpp.core.examples;
19 import io.fd.jvpp.JVppRegistry;
20 import io.fd.jvpp.JVppRegistryImpl;
21 import io.fd.jvpp.core.JVppCoreImpl;
22 import io.fd.jvpp.core.dto.ClassifyAddDelSession;
23 import io.fd.jvpp.core.dto.ClassifyAddDelSessionReply;
24 import io.fd.jvpp.core.dto.ClassifyAddDelTable;
25 import io.fd.jvpp.core.dto.ClassifyAddDelTableReply;
26 import io.fd.jvpp.core.dto.ClassifySessionDetailsReplyDump;
27 import io.fd.jvpp.core.dto.ClassifySessionDump;
28 import io.fd.jvpp.core.dto.ClassifyTableByInterface;
29 import io.fd.jvpp.core.dto.ClassifyTableByInterfaceReply;
30 import io.fd.jvpp.core.dto.ClassifyTableIds;
31 import io.fd.jvpp.core.dto.ClassifyTableIdsReply;
32 import io.fd.jvpp.core.dto.ClassifyTableInfo;
33 import io.fd.jvpp.core.dto.ClassifyTableInfoReply;
34 import io.fd.jvpp.core.dto.InputAclSetInterface;
35 import io.fd.jvpp.core.dto.InputAclSetInterfaceReply;
36 import io.fd.jvpp.core.future.FutureJVppCoreFacade;
39 * <p>Tests L2 ACL creation and read.<br> Equivalent to the following vppctl commands:<br>
42 * vppctl classify table mask l2 src
43 * vppctl classify session acl-hit-next deny opaque-index 0 table-index 0 match l2 src 01:02:03:04:05:06
44 * vppctl set int input acl intfc local0 l2-table 0
45 * vppctl sh class table verbose
49 public class L2AclExample {
51 private static final int LOCAL0_IFACE_ID = 0;
52 private static final char[] hexArray = "0123456789ABCDEF".toCharArray();
55 private static ClassifyAddDelTable createClassifyTable() {
56 ClassifyAddDelTable request = new ClassifyAddDelTable();
58 request.tableIndex = ~0; // default
60 request.memorySize = 2 << 20;
61 request.nextTableIndex = ~0; // default
62 request.missNextIndex = ~0; // default
63 request.skipNVectors = 0;
64 request.matchNVectors = 1;
66 new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
67 (byte) 0xff, (byte) 0xff, 0x00, 0x00, 0x00, 0x00};
71 private static String bytesToHex(byte[] bytes) {
72 char[] hexChars = new char[bytes.length * 2];
73 for ( int j = 0; j < bytes.length; j++ ) {
74 int v = bytes[j] & 0xFF;
75 hexChars[j * 2] = hexArray[v >>> 4];
76 hexChars[j * 2 + 1] = hexArray[v & 0x0F];
78 return new java.lang.String(hexChars);
81 private static ClassifyTableInfo createClassifyTableInfoRequest(final int tableId) {
82 ClassifyTableInfo request = new ClassifyTableInfo();
83 request.tableId = tableId;
87 private static ClassifyAddDelSession createClassifySession(final int tableIndex) {
88 ClassifyAddDelSession request = new ClassifyAddDelSession();
90 request.tableIndex = tableIndex;
91 request.hitNextIndex = 0; // deny
92 request.opaqueIndex = 0;
93 request.advance = 0; // default
94 // match 01:02:03:04:05:06 mac address
96 new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04,
97 (byte) 0x05, (byte) 0x06, 0x00, 0x00, 0x00, 0x00};
101 private static ClassifySessionDump createClassifySessionDumpRequest(final int newTableIndex) {
102 ClassifySessionDump request = new ClassifySessionDump();
103 request.tableId = newTableIndex;
107 private static InputAclSetInterface aclSetInterface() {
108 InputAclSetInterface request = new InputAclSetInterface();
110 request.swIfIndex = LOCAL0_IFACE_ID;
111 request.ip4TableIndex = ~0; // skip
112 request.ip6TableIndex = ~0; // skip
113 request.l2TableIndex = 0;
117 private static ClassifyTableByInterface createClassifyTableByInterfaceRequest() {
118 ClassifyTableByInterface request = new ClassifyTableByInterface();
119 request.swIfIndex = LOCAL0_IFACE_ID;
123 private static void print(ClassifyAddDelTableReply reply) {
124 System.out.printf("ClassifyAddDelTableReply: %s%n", reply);
127 private static void print(ClassifyTableIdsReply reply) {
128 System.out.printf("ClassifyTableIdsReply: %s%n", reply);
131 private static void print(final ClassifyTableInfoReply reply) {
132 System.out.println(reply);
134 System.out.println("Mask hex: " + bytesToHex(reply.mask));
138 private static void print(ClassifyAddDelSessionReply reply) {
139 System.out.printf("ClassifyAddDelSessionReply: context=%s%n", reply);
142 private static void print(final ClassifySessionDetailsReplyDump reply) {
143 System.out.println(reply);
144 reply.classifySessionDetails.forEach(detail -> {
145 System.out.println(detail);
146 System.out.println("Match hex: " + bytesToHex(detail.match));
150 private static void print(final InputAclSetInterfaceReply reply) {
151 System.out.printf("InputAclSetInterfaceReply: context=%s%n", reply);
154 private static void print(final ClassifyTableByInterfaceReply reply) {
155 System.out.printf("ClassifyAddDelTableReply: %s%n", reply);
158 private static void testL2Acl() throws Exception {
159 System.out.println("Testing L2 ACLs using Java callback API");
160 try (final JVppRegistry registry = new JVppRegistryImpl("L2AclExample");
161 final FutureJVppCoreFacade jvppFacade = new FutureJVppCoreFacade(registry, new JVppCoreImpl())) {
163 System.out.println("Successfully connected to VPP");
166 final ClassifyAddDelTableReply classifyAddDelTableReply =
167 jvppFacade.classifyAddDelTable(createClassifyTable()).toCompletableFuture().get();
168 print(classifyAddDelTableReply);
170 final ClassifyTableIdsReply classifyTableIdsReply =
171 jvppFacade.classifyTableIds(new ClassifyTableIds()).toCompletableFuture().get();
172 print(classifyTableIdsReply);
174 final ClassifyTableInfoReply classifyTableInfoReply =
175 jvppFacade.classifyTableInfo(createClassifyTableInfoRequest(classifyAddDelTableReply.newTableIndex))
176 .toCompletableFuture().get();
177 print(classifyTableInfoReply);
179 final ClassifyAddDelSessionReply classifyAddDelSessionReply =
180 jvppFacade.classifyAddDelSession(createClassifySession(classifyAddDelTableReply.newTableIndex))
181 .toCompletableFuture().get();
182 print(classifyAddDelSessionReply);
184 final ClassifySessionDetailsReplyDump classifySessionDetailsReplyDump =
185 jvppFacade.classifySessionDump(createClassifySessionDumpRequest(classifyAddDelTableReply.newTableIndex))
186 .toCompletableFuture().get();
187 print(classifySessionDetailsReplyDump);
189 final InputAclSetInterfaceReply inputAclSetInterfaceReply =
190 jvppFacade.inputAclSetInterface(aclSetInterface()).toCompletableFuture().get();
191 print(inputAclSetInterfaceReply);
193 final ClassifyTableByInterfaceReply classifyTableByInterfaceReply =
194 jvppFacade.classifyTableByInterface(createClassifyTableByInterfaceRequest()).toCompletableFuture()
196 print(classifyTableByInterfaceReply);
198 System.out.println("Disconnecting...");
203 public static void main(String[] args) throws Exception {