819561e78be168b2b1e06edb30eb7f388bfae125
[hc2vpp.git] /
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.ingress;
18
19 import com.google.common.primitives.Ints;
20 import io.fd.honeycomb.translate.vpp.util.Ipv4Translator;
21 import io.fd.vpp.jvpp.core.dto.ClassifyAddDelSession;
22 import io.fd.vpp.jvpp.core.dto.ClassifyAddDelTable;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.packet.fields.rev160708.AclIpHeaderFields;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.packet.fields.rev160708.AclIpv4HeaderFields;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.rev161214.InterfaceMode;
27 import org.slf4j.Logger;
28
29 public interface Ip4AclTranslator extends Ipv4Translator {
30     int ETHER_TYPE_OFFSET = 12; // first 14 bytes represent L2 header (2x6)
31     int DSCP_OFFSET = 15;
32     int DSCP_MASK = 0xfc;
33
34     int IP_PROTOCOL_OFFSET = ETHER_TYPE_OFFSET + 11;
35     int IP_PROTOCOL_MASK = 0xff;
36
37     int IP4_LEN = 4;
38     int IP4_MASK_BIT_LENGTH = 32;
39     int SRC_IP_OFFSET = ETHER_TYPE_OFFSET + 14;
40     int DST_IP_OFFSET = SRC_IP_OFFSET + IP4_LEN;
41
42     default boolean ip4Mask(final int baseOffset, final InterfaceMode mode, final AclIpHeaderFields header,
43                             final AclIpv4HeaderFields ip4, final ClassifyAddDelTable request, final Logger log) {
44         boolean aceIsEmpty = true;
45         if (InterfaceMode.L2.equals(mode)) {
46             // in L2 mode we need to match ether type
47             request.mask[baseOffset + ETHER_TYPE_OFFSET] = (byte) 0xff;
48             request.mask[baseOffset + ETHER_TYPE_OFFSET + 1] = (byte) 0xff;
49         }
50         if (header.getDscp() != null) {
51             aceIsEmpty = false;
52             request.mask[baseOffset + DSCP_OFFSET] = (byte) DSCP_MASK; // first 6 bits
53         }
54         if (header.getProtocol() != null) { // Internet Protocol number
55             aceIsEmpty = false;
56             request.mask[baseOffset + IP_PROTOCOL_OFFSET] = (byte) IP_PROTOCOL_MASK;
57         }
58         if (header.getSourcePortRange() != null) {
59             log.warn("L4 Header fields are not supported. Ignoring {}", header.getSourcePortRange());
60         }
61         if (header.getDestinationPortRange() != null) {
62             log.warn("L4 Header fields are not supported. Ignoring {}", header.getDestinationPortRange());
63         }
64         if (ip4.getSourceIpv4Network() != null) {
65             aceIsEmpty = false;
66             System.arraycopy(Impl.toByteMask(ip4.getSourceIpv4Network()), 0, request.mask,
67                 baseOffset + SRC_IP_OFFSET, IP4_LEN);
68         }
69         if (ip4.getDestinationIpv4Network() != null) {
70             aceIsEmpty = false;
71             System.arraycopy(Impl.toByteMask(ip4.getDestinationIpv4Network()), 0, request.mask,
72                 baseOffset + DST_IP_OFFSET, IP4_LEN);
73         }
74         return aceIsEmpty;
75     }
76
77     default boolean ip4Match(final int baseOffset, final InterfaceMode mode, final AclIpHeaderFields header,
78                              final AclIpv4HeaderFields ip4, final ClassifyAddDelSession request, final Logger log) {
79         boolean noMatch = true;
80         if (InterfaceMode.L2.equals(mode)) {
81             // match IP4 etherType (0x0800)
82             request.match[baseOffset + ETHER_TYPE_OFFSET] = 0x08;
83             request.match[baseOffset + ETHER_TYPE_OFFSET + 1] = 0x00;
84         }
85         if (header.getDscp() != null) {
86             noMatch = false;
87             request.match[baseOffset + DSCP_OFFSET] = (byte) (DSCP_MASK & (header.getDscp().getValue() << 2));
88         }
89         if (header.getProtocol() != null) { // Internet Protocol number
90             noMatch = false;
91             request.match[baseOffset + IP_PROTOCOL_OFFSET] = (byte) (IP_PROTOCOL_MASK & header.getProtocol());
92         }
93         if (header.getSourcePortRange() != null) {
94             log.warn("L4 Header fields are not supported. Ignoring {}", header.getSourcePortRange());
95         }
96         if (header.getDestinationPortRange() != null) {
97             log.warn("L4 Header fields are not supported. Ignoring {}", header.getDestinationPortRange());
98         }
99         if (ip4.getSourceIpv4Network() != null) {
100             noMatch = false;
101             System.arraycopy(Impl.toMatchValue(ip4.getSourceIpv4Network()), 0, request.match,
102                 baseOffset + SRC_IP_OFFSET, IP4_LEN);
103
104         }
105         if (ip4.getDestinationIpv4Network() != null) {
106             noMatch = false;
107             System.arraycopy(Impl.toMatchValue(ip4.getDestinationIpv4Network()), 0, request.match,
108                 baseOffset + DST_IP_OFFSET, IP4_LEN);
109
110         }
111         return noMatch;
112     }
113
114     class Impl {
115         private static byte[] toByteMask(final int prefixLength) {
116             final long mask = ((1L << prefixLength) - 1) << (IP4_MASK_BIT_LENGTH - prefixLength);
117             return Ints.toByteArray((int) mask);
118         }
119
120         private static byte[] toByteMask(final Ipv4Prefix ipv4Prefix) {
121             final int prefixLength = Byte.valueOf(ipv4Prefix.getValue().split("/")[1]);
122             return toByteMask(prefixLength);
123         }
124
125         private static byte[] toMatchValue(final Ipv4Prefix ipv4Prefix) {
126             final String[] split = ipv4Prefix.getValue().split("/");
127             final byte[] addressBytes = Ipv4Translator.INSTANCE.ipv4AddressNoZoneToArray(split[0]);
128             final byte[] mask = Impl.toByteMask(Byte.valueOf(split[1]));
129             for (int i = 0; i < addressBytes.length; ++i) {
130                 addressBytes[i] &= mask[i];
131             }
132             return addressBytes;
133         }
134     }
135 }