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.ingress;
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;
29 public interface Ip4AclTranslator extends Ipv4Translator {
30 int ETHER_TYPE_OFFSET = 12; // first 14 bytes represent L2 header (2x6)
34 int IP_PROTOCOL_OFFSET = ETHER_TYPE_OFFSET + 11;
35 int IP_PROTOCOL_MASK = 0xff;
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;
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;
50 if (header.getDscp() != null) {
52 request.mask[baseOffset + DSCP_OFFSET] = (byte) DSCP_MASK; // first 6 bits
54 if (header.getProtocol() != null) { // Internet Protocol number
56 request.mask[baseOffset + IP_PROTOCOL_OFFSET] = (byte) IP_PROTOCOL_MASK;
58 if (header.getSourcePortRange() != null) {
59 log.warn("L4 Header fields are not supported. Ignoring {}", header.getSourcePortRange());
61 if (header.getDestinationPortRange() != null) {
62 log.warn("L4 Header fields are not supported. Ignoring {}", header.getDestinationPortRange());
64 if (ip4.getSourceIpv4Network() != null) {
66 System.arraycopy(Impl.toByteMask(ip4.getSourceIpv4Network()), 0, request.mask,
67 baseOffset + SRC_IP_OFFSET, IP4_LEN);
69 if (ip4.getDestinationIpv4Network() != null) {
71 System.arraycopy(Impl.toByteMask(ip4.getDestinationIpv4Network()), 0, request.mask,
72 baseOffset + DST_IP_OFFSET, IP4_LEN);
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;
85 if (header.getDscp() != null) {
87 request.match[baseOffset + DSCP_OFFSET] = (byte) (DSCP_MASK & (header.getDscp().getValue() << 2));
89 if (header.getProtocol() != null) { // Internet Protocol number
91 request.match[baseOffset + IP_PROTOCOL_OFFSET] = (byte) (IP_PROTOCOL_MASK & header.getProtocol());
93 if (header.getSourcePortRange() != null) {
94 log.warn("L4 Header fields are not supported. Ignoring {}", header.getSourcePortRange());
96 if (header.getDestinationPortRange() != null) {
97 log.warn("L4 Header fields are not supported. Ignoring {}", header.getDestinationPortRange());
99 if (ip4.getSourceIpv4Network() != null) {
101 System.arraycopy(Impl.toMatchValue(ip4.getSourceIpv4Network()), 0, request.match,
102 baseOffset + SRC_IP_OFFSET, IP4_LEN);
105 if (ip4.getDestinationIpv4Network() != null) {
107 System.arraycopy(Impl.toMatchValue(ip4.getDestinationIpv4Network()), 0, request.match,
108 baseOffset + DST_IP_OFFSET, IP4_LEN);
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);
120 private static byte[] toByteMask(final Ipv4Prefix ipv4Prefix) {
121 final int prefixLength = Byte.valueOf(ipv4Prefix.getValue().split("/")[1]);
122 return toByteMask(prefixLength);
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];