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.honeycomb.translate.v3po.interfaces.acl;
19 import static com.google.common.base.Preconditions.checkArgument;
21 import com.google.common.annotations.VisibleForTesting;
22 import java.net.InetAddress;
23 import java.net.UnknownHostException;
24 import java.util.BitSet;
25 import javax.annotation.Nonnull;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.acl.access.list.entries.ace.actions.PacketHandling;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.acl.access.list.entries.ace.matches.ace.type.AceIp;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.acl.access.list.entries.ace.matches.ace.type.ace.ip.ace.ip.version.AceIpv6;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Prefix;
30 import org.openvpp.jvpp.core.dto.ClassifyAddDelSession;
31 import org.openvpp.jvpp.core.dto.ClassifyAddDelTable;
32 import org.openvpp.jvpp.core.dto.InputAclSetInterface;
33 import org.openvpp.jvpp.core.future.FutureJVppCore;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
37 final class AceIp6Writer extends AbstractAceWriter<AceIp> {
40 static final int MATCH_N_VECTORS = 4; // number of 16B vectors
41 private static final Logger LOG = LoggerFactory.getLogger(AceIp6Writer.class);
42 private static final int TABLE_MASK_LENGTH = 64;
43 private static final int IP6_MASK_BIT_LENGTH = 128;
45 private static final int IP_VERSION_OFFSET = 14; // first 14 bytes represent L2 header (2x6 + etherType(2))
46 private static final int IP_VERSION_MASK = 0xf0;
47 private static final int DSCP_MASK1 = 0x0f;
48 private static final int DSCP_MASK2 = 0xc0;
49 private static final int IP6_LEN = 16;
50 private static final int SRC_IP_OFFSET = IP_VERSION_OFFSET + 8;
51 private static final int DST_IP_OFFSET = SRC_IP_OFFSET + IP6_LEN;
53 public AceIp6Writer(@Nonnull final FutureJVppCore futureJVppCore) {
54 super(futureJVppCore);
57 private static byte[] toByteMask(final int prefixLength) {
58 final BitSet mask = new BitSet(IP6_MASK_BIT_LENGTH);
59 mask.set(0, prefixLength, true);
60 if (prefixLength < IP6_MASK_BIT_LENGTH) {
61 mask.set(prefixLength, IP6_MASK_BIT_LENGTH, false);
63 return mask.toByteArray();
66 private static byte[] toByteMask(final Ipv6Prefix ipv6Prefix) {
67 final int prefixLength = Short.valueOf(ipv6Prefix.getValue().split("/")[1]);
68 return toByteMask(prefixLength);
71 private static byte[] toMatchValue(final Ipv6Prefix ipv6Prefix) {
72 final String[] split = ipv6Prefix.getValue().split("/");
73 final byte[] addressBytes;
75 addressBytes = InetAddress.getByName(split[0]).getAddress();
76 } catch (UnknownHostException e) {
77 throw new IllegalArgumentException("Invalid IP6 address", e);
79 final byte[] mask = toByteMask(Short.valueOf(split[1]));
81 for (; pos < mask.length; ++pos) {
82 addressBytes[pos] &= mask[pos];
84 // mask can be shorter that address, so we need to clear rest of the address:
85 for (; pos < addressBytes.length; ++pos) {
86 addressBytes[pos] = 0;
92 public ClassifyAddDelTable createClassifyTable(@Nonnull final PacketHandling action,
93 @Nonnull final AceIp aceIp,
94 final int nextTableIndex,
96 checkArgument(aceIp.getAceIpVersion() instanceof AceIpv6, "Expected AceIpv6 version, but was %", aceIp);
97 final AceIpv6 ipVersion = (AceIpv6) aceIp.getAceIpVersion();
99 final ClassifyAddDelTable request = createClassifyTable(action, nextTableIndex);
100 request.skipNVectors = 0; // match entire L2 and L3 header
101 request.matchNVectors = MATCH_N_VECTORS;
103 boolean aceIsEmpty = true;
104 request.mask = new byte[TABLE_MASK_LENGTH];
106 final int baseOffset = getVlanTagsLen(vlanTags);
108 if (aceIp.getProtocol() != null) {
109 request.mask[baseOffset + IP_VERSION_OFFSET] |= IP_VERSION_MASK;
112 if (aceIp.getDscp() != null) {
114 // DCSP (bits 4-9 of IP6 header)
115 request.mask[baseOffset + IP_VERSION_OFFSET] |= DSCP_MASK1;
116 request.mask[baseOffset + IP_VERSION_OFFSET + 1] |= DSCP_MASK2;
119 if (aceIp.getSourcePortRange() != null) {
120 LOG.warn("L4 Header fields are not supported. Ignoring {}", aceIp.getSourcePortRange());
123 if (aceIp.getDestinationPortRange() != null) {
124 LOG.warn("L4 Header fields are not supported. Ignoring {}", aceIp.getDestinationPortRange());
127 if (ipVersion.getFlowLabel() != null) {
130 request.mask[baseOffset + IP_VERSION_OFFSET + 1] |= (byte) 0x0f;
131 request.mask[baseOffset + IP_VERSION_OFFSET + 2] = (byte) 0xff;
132 request.mask[baseOffset + IP_VERSION_OFFSET + 3] = (byte) 0xff;
135 if (ipVersion.getSourceIpv6Network() != null) {
137 final byte[] mask = toByteMask(ipVersion.getSourceIpv6Network());
138 System.arraycopy(mask, 0, request.mask, baseOffset + SRC_IP_OFFSET, mask.length);
141 if (ipVersion.getDestinationIpv6Network() != null) {
143 final byte[] mask = toByteMask(ipVersion.getDestinationIpv6Network());
144 System.arraycopy(mask, 0, request.mask, baseOffset + DST_IP_OFFSET, mask.length);
148 throw new IllegalArgumentException(
149 String.format("Ace %s does not define packet field match values", aceIp.toString()));
152 LOG.debug("ACE action={}, rule={} translated to table={}.", action, aceIp, request);
157 public ClassifyAddDelSession createClassifySession(@Nonnull final PacketHandling action,
158 @Nonnull final AceIp aceIp,
159 final int tableIndex,
160 final int vlanTags) {
161 checkArgument(aceIp.getAceIpVersion() instanceof AceIpv6, "Expected AceIpv6 version, but was %", aceIp);
162 final AceIpv6 ipVersion = (AceIpv6) aceIp.getAceIpVersion();
164 final ClassifyAddDelSession request = createClassifySession(action, tableIndex);
165 request.match = new byte[TABLE_MASK_LENGTH];
166 boolean noMatch = true;
168 final int baseOffset = getVlanTagsLen(vlanTags);
170 if (aceIp.getProtocol() != null) {
171 request.match[baseOffset + IP_VERSION_OFFSET] |=
172 (byte) (IP_VERSION_MASK & (aceIp.getProtocol().intValue() << 4));
175 if (aceIp.getDscp() != null) {
177 final int dscp = aceIp.getDscp().getValue();
178 // set bits 4-9 of IP6 header:
179 request.match[baseOffset + IP_VERSION_OFFSET] |= (byte) (DSCP_MASK1 & (dscp >> 2));
180 request.match[baseOffset + IP_VERSION_OFFSET + 1] |= (byte) (DSCP_MASK2 & (dscp << 6));
183 if (aceIp.getSourcePortRange() != null) {
184 LOG.warn("L4 Header fields are not supported. Ignoring {}", aceIp.getSourcePortRange());
187 if (aceIp.getDestinationPortRange() != null) {
188 LOG.warn("L4 Header fields are not supported. Ignoring {}", aceIp.getDestinationPortRange());
191 if (ipVersion.getFlowLabel() != null) {
193 final int flowLabel = ipVersion.getFlowLabel().getValue().intValue();
195 request.match[baseOffset + IP_VERSION_OFFSET + 1] |= (byte) (0x0f & (flowLabel >> 16));
196 request.match[baseOffset + IP_VERSION_OFFSET + 2] = (byte) (0xff & (flowLabel >> 8));
197 request.match[baseOffset + IP_VERSION_OFFSET + 3] = (byte) (0xff & flowLabel);
200 if (ipVersion.getSourceIpv6Network() != null) {
202 final byte[] match = toMatchValue(ipVersion.getSourceIpv6Network());
203 System.arraycopy(match, 0, request.match, baseOffset + SRC_IP_OFFSET, IP6_LEN);
206 if (ipVersion.getDestinationIpv6Network() != null) {
208 final byte[] match = toMatchValue(ipVersion.getDestinationIpv6Network());
209 System.arraycopy(match, 0, request.match, baseOffset + DST_IP_OFFSET, IP6_LEN);
213 throw new IllegalArgumentException(
214 String.format("Ace %s does not define packet field match values", aceIp.toString()));
217 LOG.debug("ACE action={}, rule={} translated to session={}.", action, aceIp, request);
222 protected void setClassifyTable(@Nonnull final InputAclSetInterface request, final int tableIndex) {
223 request.ip6TableIndex = tableIndex;