1e222ec6ae41e0b4983ec1ccd0ea70228e58b8cc
[honeycomb.git] / v3po / v3po2vpp / src / main / java / io / fd / honeycomb / translate / v3po / interfaces / acl / AceIp6Writer.java
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  *
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:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package io.fd.honeycomb.translate.v3po.interfaces.acl;
18
19 import static com.google.common.base.Preconditions.checkArgument;
20
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;
36
37 final class AceIp6Writer extends AbstractAceWriter<AceIp> {
38
39     @VisibleForTesting
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;
44
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;
52
53     public AceIp6Writer(@Nonnull final FutureJVppCore futureJVppCore) {
54         super(futureJVppCore);
55     }
56
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);
62         }
63         return mask.toByteArray();
64     }
65
66     private static byte[] toByteMask(final Ipv6Prefix ipv6Prefix) {
67         final int prefixLength = Short.valueOf(ipv6Prefix.getValue().split("/")[1]);
68         return toByteMask(prefixLength);
69     }
70
71     private static byte[] toMatchValue(final Ipv6Prefix ipv6Prefix) {
72         final String[] split = ipv6Prefix.getValue().split("/");
73         final byte[] addressBytes;
74         try {
75             addressBytes = InetAddress.getByName(split[0]).getAddress();
76         } catch (UnknownHostException e) {
77             throw new IllegalArgumentException("Invalid IP6 address", e);
78         }
79         final byte[] mask = toByteMask(Short.valueOf(split[1]));
80         int pos = 0;
81         for (; pos < mask.length; ++pos) {
82             addressBytes[pos] &= mask[pos];
83         }
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;
87         }
88         return addressBytes;
89     }
90
91     @Override
92     public ClassifyAddDelTable createClassifyTable(@Nonnull final PacketHandling action,
93                                                    @Nonnull final AceIp aceIp,
94                                                    final int nextTableIndex,
95                                                    final int vlanTags) {
96         checkArgument(aceIp.getAceIpVersion() instanceof AceIpv6, "Expected AceIpv6 version, but was %", aceIp);
97         final AceIpv6 ipVersion = (AceIpv6) aceIp.getAceIpVersion();
98
99         final ClassifyAddDelTable request = createClassifyTable(action, nextTableIndex);
100         request.skipNVectors = 0; // match entire L2 and L3 header
101         request.matchNVectors = MATCH_N_VECTORS;
102
103         boolean aceIsEmpty = true;
104         request.mask = new byte[TABLE_MASK_LENGTH];
105
106         final int baseOffset = getVlanTagsLen(vlanTags);
107
108         if (aceIp.getProtocol() != null) {
109             request.mask[baseOffset + IP_VERSION_OFFSET] |= IP_VERSION_MASK;
110         }
111
112         if (aceIp.getDscp() != null) {
113             aceIsEmpty = false;
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;
117         }
118
119         if (aceIp.getSourcePortRange() != null) {
120             LOG.warn("L4 Header fields are not supported. Ignoring {}", aceIp.getSourcePortRange());
121         }
122
123         if (aceIp.getDestinationPortRange() != null) {
124             LOG.warn("L4 Header fields are not supported. Ignoring {}", aceIp.getDestinationPortRange());
125         }
126
127         if (ipVersion.getFlowLabel() != null) {
128             aceIsEmpty = false;
129             // bits 12-31
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;
133         }
134
135         if (ipVersion.getSourceIpv6Network() != null) {
136             aceIsEmpty = false;
137             final byte[] mask = toByteMask(ipVersion.getSourceIpv6Network());
138             System.arraycopy(mask, 0, request.mask, baseOffset + SRC_IP_OFFSET, mask.length);
139         }
140
141         if (ipVersion.getDestinationIpv6Network() != null) {
142             aceIsEmpty = false;
143             final byte[] mask = toByteMask(ipVersion.getDestinationIpv6Network());
144             System.arraycopy(mask, 0, request.mask, baseOffset + DST_IP_OFFSET, mask.length);
145         }
146
147         if (aceIsEmpty) {
148             throw new IllegalArgumentException(
149                 String.format("Ace %s does not define packet field match values", aceIp.toString()));
150         }
151
152         LOG.debug("ACE action={}, rule={} translated to table={}.", action, aceIp, request);
153         return request;
154     }
155
156     @Override
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();
163
164         final ClassifyAddDelSession request = createClassifySession(action, tableIndex);
165         request.match = new byte[TABLE_MASK_LENGTH];
166         boolean noMatch = true;
167
168         final int baseOffset = getVlanTagsLen(vlanTags);
169
170         if (aceIp.getProtocol() != null) {
171             request.match[baseOffset + IP_VERSION_OFFSET] |=
172                 (byte) (IP_VERSION_MASK & (aceIp.getProtocol().intValue() << 4));
173         }
174
175         if (aceIp.getDscp() != null) {
176             noMatch = false;
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));
181         }
182
183         if (aceIp.getSourcePortRange() != null) {
184             LOG.warn("L4 Header fields are not supported. Ignoring {}", aceIp.getSourcePortRange());
185         }
186
187         if (aceIp.getDestinationPortRange() != null) {
188             LOG.warn("L4 Header fields are not supported. Ignoring {}", aceIp.getDestinationPortRange());
189         }
190
191         if (ipVersion.getFlowLabel() != null) {
192             noMatch = false;
193             final int flowLabel = ipVersion.getFlowLabel().getValue().intValue();
194             // bits 12-31
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);
198         }
199
200         if (ipVersion.getSourceIpv6Network() != null) {
201             noMatch = false;
202             final byte[] match = toMatchValue(ipVersion.getSourceIpv6Network());
203             System.arraycopy(match, 0, request.match, baseOffset + SRC_IP_OFFSET, IP6_LEN);
204         }
205
206         if (ipVersion.getDestinationIpv6Network() != null) {
207             noMatch = false;
208             final byte[] match = toMatchValue(ipVersion.getDestinationIpv6Network());
209             System.arraycopy(match, 0, request.match, baseOffset + DST_IP_OFFSET, IP6_LEN);
210         }
211
212         if (noMatch) {
213             throw new IllegalArgumentException(
214                 String.format("Ace %s does not define packet field match values", aceIp.toString()));
215         }
216
217         LOG.debug("ACE action={}, rule={} translated to session={}.", action, aceIp, request);
218         return request;
219     }
220
221     @Override
222     protected void setClassifyTable(@Nonnull final InputAclSetInterface request, final int tableIndex) {
223         request.ip6TableIndex = tableIndex;
224     }
225 }