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.core.examples;
19 import io.fd.vpp.jvpp.JVppRegistry;
20 import io.fd.vpp.jvpp.JVppRegistryImpl;
21 import io.fd.vpp.jvpp.core.JVppCoreImpl;
22 import io.fd.vpp.jvpp.core.dto.ClassifyAddDelSession;
23 import io.fd.vpp.jvpp.core.dto.ClassifyAddDelSessionReply;
24 import io.fd.vpp.jvpp.core.dto.ClassifyAddDelTable;
25 import io.fd.vpp.jvpp.core.dto.ClassifyAddDelTableReply;
26 import io.fd.vpp.jvpp.core.dto.ClassifySessionDetailsReplyDump;
27 import io.fd.vpp.jvpp.core.dto.ClassifySessionDump;
28 import io.fd.vpp.jvpp.core.dto.ClassifyTableByInterface;
29 import io.fd.vpp.jvpp.core.dto.ClassifyTableByInterfaceReply;
30 import io.fd.vpp.jvpp.core.dto.ClassifyTableIds;
31 import io.fd.vpp.jvpp.core.dto.ClassifyTableIdsReply;
32 import io.fd.vpp.jvpp.core.dto.ClassifyTableInfo;
33 import io.fd.vpp.jvpp.core.dto.ClassifyTableInfoReply;
34 import io.fd.vpp.jvpp.core.dto.InputAclSetInterface;
35 import io.fd.vpp.jvpp.core.dto.InputAclSetInterfaceReply;
36 import io.fd.vpp.jvpp.core.future.FutureJVppCoreFacade;
37 import javax.xml.bind.DatatypeConverter;
40 * <p>Tests L2 ACL creation and read.<br> Equivalent to the following vppctl commands:<br>
43 * vppctl classify table mask l2 src
44 * vppctl classify session acl-hit-next deny opaque-index 0 table-index 0 match l2 src 01:02:03:04:05:06
45 * vppctl set int input acl intfc local0 l2-table 0
46 * vppctl sh class table verbose
50 public class L2AclExample {
52 private static final int LOCAL0_IFACE_ID = 0;
54 private static ClassifyAddDelTable createClassifyTable() {
55 ClassifyAddDelTable request = new ClassifyAddDelTable();
57 request.tableIndex = ~0; // default
59 request.memorySize = 2 << 20;
60 request.nextTableIndex = ~0; // default
61 request.missNextIndex = ~0; // default
62 request.skipNVectors = 0;
63 request.matchNVectors = 1;
65 new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
66 (byte) 0xff, (byte) 0xff, 0x00, 0x00, 0x00, 0x00};
70 private static ClassifyTableInfo createClassifyTableInfoRequest(final int tableId) {
71 ClassifyTableInfo request = new ClassifyTableInfo();
72 request.tableId = tableId;
76 private static ClassifyAddDelSession createClassifySession(final int tableIndex) {
77 ClassifyAddDelSession request = new ClassifyAddDelSession();
79 request.tableIndex = tableIndex;
80 request.hitNextIndex = 0; // deny
81 request.opaqueIndex = 0;
82 request.advance = 0; // default
83 // match 01:02:03:04:05:06 mac address
85 new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04,
86 (byte) 0x05, (byte) 0x06, 0x00, 0x00, 0x00, 0x00};
90 private static ClassifySessionDump createClassifySessionDumpRequest(final int newTableIndex) {
91 ClassifySessionDump request = new ClassifySessionDump();
92 request.tableId = newTableIndex;
96 private static InputAclSetInterface aclSetInterface() {
97 InputAclSetInterface request = new InputAclSetInterface();
99 request.swIfIndex = LOCAL0_IFACE_ID;
100 request.ip4TableIndex = ~0; // skip
101 request.ip6TableIndex = ~0; // skip
102 request.l2TableIndex = 0;
106 private static ClassifyTableByInterface createClassifyTableByInterfaceRequest() {
107 ClassifyTableByInterface request = new ClassifyTableByInterface();
108 request.swIfIndex = LOCAL0_IFACE_ID;
112 private static void print(ClassifyAddDelTableReply reply) {
113 System.out.printf("ClassifyAddDelTableReply: %s%n", reply);
116 private static void print(ClassifyTableIdsReply reply) {
117 System.out.printf("ClassifyTableIdsReply: %s%n", reply);
120 private static void print(final ClassifyTableInfoReply reply) {
121 System.out.println(reply);
123 System.out.println("Mask hex: " + DatatypeConverter.printHexBinary(reply.mask));
127 private static void print(ClassifyAddDelSessionReply reply) {
128 System.out.printf("ClassifyAddDelSessionReply: context=%s%n", reply);
131 private static void print(final ClassifySessionDetailsReplyDump reply) {
132 System.out.println(reply);
133 reply.classifySessionDetails.forEach(detail -> {
134 System.out.println(detail);
135 System.out.println("Match hex: " + DatatypeConverter.printHexBinary(detail.match));
139 private static void print(final InputAclSetInterfaceReply reply) {
140 System.out.printf("InputAclSetInterfaceReply: context=%s%n", reply);
143 private static void print(final ClassifyTableByInterfaceReply reply) {
144 System.out.printf("ClassifyAddDelTableReply: %s%n", reply);
147 private static void testL2Acl() throws Exception {
148 System.out.println("Testing L2 ACLs using Java callback API");
149 try (final JVppRegistry registry = new JVppRegistryImpl("L2AclExample");
150 final FutureJVppCoreFacade jvppFacade = new FutureJVppCoreFacade(registry, new JVppCoreImpl())) {
152 System.out.println("Successfully connected to VPP");
155 final ClassifyAddDelTableReply classifyAddDelTableReply =
156 jvppFacade.classifyAddDelTable(createClassifyTable()).toCompletableFuture().get();
157 print(classifyAddDelTableReply);
159 final ClassifyTableIdsReply classifyTableIdsReply =
160 jvppFacade.classifyTableIds(new ClassifyTableIds()).toCompletableFuture().get();
161 print(classifyTableIdsReply);
163 final ClassifyTableInfoReply classifyTableInfoReply =
164 jvppFacade.classifyTableInfo(createClassifyTableInfoRequest(classifyAddDelTableReply.newTableIndex))
165 .toCompletableFuture().get();
166 print(classifyTableInfoReply);
168 final ClassifyAddDelSessionReply classifyAddDelSessionReply =
169 jvppFacade.classifyAddDelSession(createClassifySession(classifyAddDelTableReply.newTableIndex))
170 .toCompletableFuture().get();
171 print(classifyAddDelSessionReply);
173 final ClassifySessionDetailsReplyDump classifySessionDetailsReplyDump =
174 jvppFacade.classifySessionDump(createClassifySessionDumpRequest(classifyAddDelTableReply.newTableIndex))
175 .toCompletableFuture().get();
176 print(classifySessionDetailsReplyDump);
178 final InputAclSetInterfaceReply inputAclSetInterfaceReply =
179 jvppFacade.inputAclSetInterface(aclSetInterface()).toCompletableFuture().get();
180 print(inputAclSetInterfaceReply);
182 final ClassifyTableByInterfaceReply classifyTableByInterfaceReply =
183 jvppFacade.classifyTableByInterface(createClassifyTableByInterfaceRequest()).toCompletableFuture()
185 print(classifyTableByInterfaceReply);
187 System.out.println("Disconnecting...");
192 public static void main(String[] args) throws Exception {