f14a24a3feb6a67399d31065eedbf56173264259
[hc2vpp.git] /
1 /*
2  * Copyright (c) 2017 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.hc2vpp.vpp.classifier.write.acl.common;
18
19 import static com.google.common.base.Preconditions.checkArgument;
20
21 import io.fd.vpp.jvpp.core.dto.ClassifyAddDelSession;
22 import io.fd.vpp.jvpp.core.dto.ClassifyAddDelTable;
23 import java.util.ArrayList;
24 import java.util.List;
25 import javax.annotation.Nonnull;
26 import javax.annotation.Nullable;
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.actions.PacketHandling;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.acl.rev170315.InterfaceMode;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.acl.rev170315.access.lists.acl.access.list.entries.ace.matches.ace.type.AceIpAndEth;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.acl.rev170315.access.lists.acl.access.list.entries.ace.matches.ace.type.ace.ip.and.eth.AceIpAndEthNodes;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.acl.rev170315.access.lists.acl.access.list.entries.ace.matches.ace.type.ace.ip.and.eth.ace.ip.and.eth.nodes.AceIpVersion;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.acl.rev170315.access.lists.acl.access.list.entries.ace.matches.ace.type.ace.ip.and.eth.ace.ip.and.eth.nodes.ace.ip.version.AceIpv4;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.acl.rev170315.access.lists.acl.access.list.entries.ace.matches.ace.type.ace.ip.and.eth.ace.ip.and.eth.nodes.ace.ip.version.AceIpv6;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 public final class AceIpAndEthWriter
38     implements AceWriter<AceIpAndEth>, AclTranslator, L2AclTranslator, Ip4AclTranslator, Ip6AclTranslator {
39
40     private static final Logger LOG = LoggerFactory.getLogger(AceIpAndEthWriter.class);
41
42     private static int maskLength(@Nonnull final AceIpAndEth ace, final int vlanTags) {
43         if (ace.getAceIpAndEthNodes().getAceIpVersion() != null) {
44             if (ace.getAceIpAndEthNodes().getAceIpVersion() instanceof AceIpv4) {
45                 return 48;
46             } else {
47                 return vlanTags == 2
48                     ? 80
49                     : 64;
50             }
51         }
52         return 16;
53     }
54
55     @Override
56     public ClassifyAddDelTable createTable(@Nonnull final AceIpAndEth ace, @Nullable final InterfaceMode mode,
57                                            final int nextTableIndex, final int vlanTags) {
58         final AceIpAndEthNodes nodes = ace.getAceIpAndEthNodes();
59         final int numberOfSessions = PortPair.fromRange(nodes.getSourcePortRange(), nodes.getDestinationPortRange()).size();
60         final ClassifyAddDelTable request = createTable(nextTableIndex, numberOfSessions);
61         final int maskLength = maskLength(ace, vlanTags);
62         request.mask = new byte[maskLength];
63         request.skipNVectors = 0;
64         request.matchNVectors = maskLength / 16;
65
66         boolean aceIsEmpty =
67             destinationMacAddressMask(nodes.getDestinationMacAddressMask(), nodes.getDestinationMacAddress(), request);
68         aceIsEmpty &= sourceMacAddressMask(nodes.getSourceMacAddressMask(), nodes.getSourceMacAddress(), request);
69
70         // if we use classifier API, we need to know ip version (fields common for ip4 and ip6 have different offsets):
71         final AceIpVersion aceIpVersion = nodes.getAceIpVersion();
72         checkArgument(aceIpVersion != null, "AceIpAndEth have to define IpVersion");
73
74         final int baseOffset = getVlanTagsLen(vlanTags);
75         if (aceIpVersion instanceof AceIpv4) {
76             final AceIpv4 ipVersion = (AceIpv4) aceIpVersion;
77             aceIsEmpty &= ip4Mask(baseOffset, mode, nodes, ipVersion, request);
78         } else if (aceIpVersion instanceof AceIpv6) {
79             final AceIpv6 ipVersion = (AceIpv6) aceIpVersion;
80             aceIsEmpty &= ip6Mask(baseOffset, mode, nodes, ipVersion, request);
81         } else {
82             throw new IllegalArgumentException(String.format("Unsupported IP version %s", aceIpVersion));
83         }
84
85         if (aceIsEmpty) {
86             throw new IllegalArgumentException(
87                 String.format("Ace %s does not define packet field match values", ace.toString()));
88         }
89
90         LOG.debug("ACE rule={} translated to table={}.", ace, request);
91         return request;
92     }
93
94     @Override
95     public List<ClassifyAddDelSession> createSession(@Nonnull final PacketHandling action,
96                                                      @Nonnull final AceIpAndEth ace,
97                                                      @Nullable final InterfaceMode mode, final int tableIndex,
98                                                      final int vlanTags) {
99         final AceIpAndEthNodes nodes = ace.getAceIpAndEthNodes();
100         final List<PortPair> portPairs = PortPair.fromRange(nodes.getSourcePortRange(), nodes.getDestinationPortRange());
101         final List<ClassifyAddDelSession> requests = new ArrayList<>(portPairs.size());
102         for (final PortPair pair : portPairs) {
103             final ClassifyAddDelSession request = createSession(action, tableIndex);
104             request.match = new byte[maskLength(ace, vlanTags)];
105
106             boolean noMatch = destinationMacAddressMatch(nodes.getDestinationMacAddress(), request);
107             noMatch &= sourceMacAddressMatch(nodes.getSourceMacAddress(), request);
108
109             final AceIpVersion aceIpVersion = nodes.getAceIpVersion();
110             checkArgument(aceIpVersion != null, "AceIpAndEth have to define IpVersion");
111
112             final int baseOffset = getVlanTagsLen(vlanTags);
113             if (aceIpVersion instanceof AceIpv4) {
114                 final AceIpv4 ipVersion = (AceIpv4) aceIpVersion;
115                 noMatch &= ip4Match(baseOffset, mode, nodes, ipVersion, pair.getSrc(), pair.getDst(), request);
116             } else if (aceIpVersion instanceof AceIpv6) {
117                 final AceIpv6 ipVersion = (AceIpv6) aceIpVersion;
118                 noMatch &= ip6Match(baseOffset, mode, nodes, ipVersion, pair.getSrc(), pair.getDst(), request);
119             } else {
120                 throw new IllegalArgumentException(String.format("Unsupported IP version %s", aceIpVersion));
121             }
122
123             if (noMatch) {
124                 throw new IllegalArgumentException(
125                     String.format("Ace %s does not define packet field match values", ace.toString()));
126             }
127             LOG.debug("ACE action={}, rule={} translated to session={}.", action, ace, request);
128             requests.add(request);
129         }
130         return requests;
131     }
132 }