summaryrefslogtreecommitdiffstats
path: root/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/translate/v3po/interfaces/acl/ingress/Ip4AclTranslator.java
blob: 819561e78be168b2b1e06edb30eb7f388bfae125 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
 * Copyright (c) 2016 Cisco and/or its affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.fd.honeycomb.translate.v3po.interfaces.acl.ingress;

import com.google.common.primitives.Ints;
import io.fd.honeycomb.translate.vpp.util.Ipv4Translator;
import io.fd.vpp.jvpp.core.dto.ClassifyAddDelSession;
import io.fd.vpp.jvpp.core.dto.ClassifyAddDelTable;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.packet.fields.rev160708.AclIpHeaderFields;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.packet.fields.rev160708.AclIpv4HeaderFields;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.rev161214.InterfaceMode;
import org.slf4j.Logger;

public interface Ip4AclTranslator extends Ipv4Translator {
    int ETHER_TYPE_OFFSET = 12; // first 14 bytes represent L2 header (2x6)
    int DSCP_OFFSET = 15;
    int DSCP_MASK = 0xfc;

    int IP_PROTOCOL_OFFSET = ETHER_TYPE_OFFSET + 11;
    int IP_PROTOCOL_MASK = 0xff;

    int IP4_LEN = 4;
    int IP4_MASK_BIT_LENGTH = 32;
    int SRC_IP_OFFSET = ETHER_TYPE_OFFSET + 14;
    int DST_IP_OFFSET = SRC_IP_OFFSET + IP4_LEN;

    default boolean ip4Mask(final int baseOffset, final InterfaceMode mode, final AclIpHeaderFields header,
                            final AclIpv4HeaderFields ip4, final ClassifyAddDelTable request, final Logger log) {
        boolean aceIsEmpty = true;
        if (InterfaceMode.L2.equals(mode)) {
            // in L2 mode we need to match ether type
            request.mask[baseOffset + ETHER_TYPE_OFFSET] = (byte) 0xff;
            request.mask[baseOffset + ETHER_TYPE_OFFSET + 1] = (byte) 0xff;
        }
        if (header.getDscp() != null) {
            aceIsEmpty = false;
            request.mask[baseOffset + DSCP_OFFSET] = (byte) DSCP_MASK; // first 6 bits
        }
        if (header.getProtocol() != null) { // Internet Protocol number
            aceIsEmpty = false;
            request.mask[baseOffset + IP_PROTOCOL_OFFSET] = (byte) IP_PROTOCOL_MASK;
        }
        if (header.getSourcePortRange() != null) {
            log.warn("L4 Header fields are not supported. Ignoring {}", header.getSourcePortRange());
        }
        if (header.getDestinationPortRange() != null) {
            log.warn("L4 Header fields are not supported. Ignoring {}", header.getDestinationPortRange());
        }
        if (ip4.getSourceIpv4Network() != null) {
            aceIsEmpty = false;
            System.arraycopy(Impl.toByteMask(ip4.getSourceIpv4Network()), 0, request.mask,
                baseOffset + SRC_IP_OFFSET, IP4_LEN);
        }
        if (ip4.getDestinationIpv4Network() != null) {
            aceIsEmpty = false;
            System.arraycopy(Impl.toByteMask(ip4.getDestinationIpv4Network()), 0, request.mask,
                baseOffset + DST_IP_OFFSET, IP4_LEN);
        }
        return aceIsEmpty;
    }

    default boolean ip4Match(final int baseOffset, final InterfaceMode mode, final AclIpHeaderFields header,
                             final AclIpv4HeaderFields ip4, final ClassifyAddDelSession request, final Logger log) {
        boolean noMatch = true;
        if (InterfaceMode.L2.equals(mode)) {
            // match IP4 etherType (0x0800)
            request.match[baseOffset + ETHER_TYPE_OFFSET] = 0x08;
            request.match[baseOffset + ETHER_TYPE_OFFSET + 1] = 0x00;
        }
        if (header.getDscp() != null) {
            noMatch = false;
            request.match[baseOffset + DSCP_OFFSET] = (byte) (DSCP_MASK & (header.getDscp().getValue() << 2));
        }
        if (header.getProtocol() != null) { // Internet Protocol number
            noMatch = false;
            request.match[baseOffset + IP_PROTOCOL_OFFSET] = (byte) (IP_PROTOCOL_MASK & header.getProtocol());
        }
        if (header.getSourcePortRange() != null) {
            log.warn("L4 Header fields are not supported. Ignoring {}", header.getSourcePortRange());
        }
        if (header.getDestinationPortRange() != null) {
            log.warn("L4 Header fields are not supported. Ignoring {}", header.getDestinationPortRange());
        }
        if (ip4.getSourceIpv4Network() != null) {
            noMatch = false;
            System.arraycopy(Impl.toMatchValue(ip4.getSourceIpv4Network()), 0, request.match,
                baseOffset + SRC_IP_OFFSET, IP4_LEN);

        }
        if (ip4.getDestinationIpv4Network() != null) {
            noMatch = false;
            System.arraycopy(Impl.toMatchValue(ip4.getDestinationIpv4Network()), 0, request.match,
                baseOffset + DST_IP_OFFSET, IP4_LEN);

        }
        return noMatch;
    }

    class Impl {
        private static byte[] toByteMask(final int prefixLength) {
            final long mask = ((1L << prefixLength) - 1) << (IP4_MASK_BIT_LENGTH - prefixLength);
            return Ints.toByteArray((int) mask);
        }

        private static byte[] toByteMask(final Ipv4Prefix ipv4Prefix) {
            final int prefixLength = Byte.valueOf(ipv4Prefix.getValue().split("/")[1]);
            return toByteMask(prefixLength);
        }

        private static byte[] toMatchValue(final Ipv4Prefix ipv4Prefix) {
            final String[] split = ipv4Prefix.getValue().split("/");
            final byte[] addressBytes = Ipv4Translator.INSTANCE.ipv4AddressNoZoneToArray(split[0]);
            final byte[] mask = Impl.toByteMask(Byte.valueOf(split[1]));
            for (int i = 0; i < addressBytes.length; ++i) {
                addressBytes[i] &= mask[i];
            }
            return addressBytes;
        }
    }
}