1ef56ea6ad9b536c9cf227d020fa066bb48d707a
[netvirt.git] / vpnservice / aclservice / impl / src / main / java / org / opendaylight / netvirt / aclservice / StatefulEgressAclServiceImpl.java
1 /*
2  * Copyright (c) 2016 Red Hat, Inc. and others. All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.netvirt.aclservice;
9
10 import java.math.BigInteger;
11 import java.util.ArrayList;
12 import java.util.List;
13 import java.util.Map;
14
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.genius.mdsalutil.ActionInfo;
17 import org.opendaylight.genius.mdsalutil.ActionType;
18 import org.opendaylight.genius.mdsalutil.InstructionInfo;
19 import org.opendaylight.genius.mdsalutil.InstructionType;
20 import org.opendaylight.genius.mdsalutil.MatchFieldType;
21 import org.opendaylight.genius.mdsalutil.MatchInfo;
22 import org.opendaylight.genius.mdsalutil.MatchInfoBase;
23 import org.opendaylight.genius.mdsalutil.NwConstants;
24 import org.opendaylight.genius.mdsalutil.NxMatchFieldType;
25 import org.opendaylight.genius.mdsalutil.NxMatchInfo;
26 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
27 import org.opendaylight.netvirt.aclservice.api.AclServiceManager.Action;
28 import org.opendaylight.netvirt.aclservice.utils.AclConstants;
29 import org.opendaylight.netvirt.aclservice.utils.AclServiceUtils;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.acl.access.list.entries.Ace;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.IpPrefixOrAddress;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.interfaces._interface.AllowedAddressPairs;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 /**
37  * Provides the stateful implementation for egress (w.r.t VM) ACL service.
38  *
39  * <p>
40  * Note: Table names used are w.r.t switch. Hence, switch ingress is VM egress
41  * and vice versa.
42  */
43 public class StatefulEgressAclServiceImpl extends AbstractEgressAclServiceImpl {
44
45     public StatefulEgressAclServiceImpl(DataBroker dataBroker, IMdsalApiManager mdsalManager) {
46         super(dataBroker, mdsalManager);
47     }
48
49     private static final Logger LOG = LoggerFactory.getLogger(StatefulEgressAclServiceImpl.class);
50
51
52     /**
53      * Program conntrack rules.
54      *
55      * @param dpid the dpid
56      * @param dhcpMacAddress the dhcp mac address.
57      * @param allowedAddresses the allowed addresses
58      * @param lportTag the lport tag
59      * @param addOrRemove addorRemove
60      */
61     @Override
62     protected void programSpecificFixedRules(BigInteger dpid, String dhcpMacAddress,
63             List<AllowedAddressPairs> allowedAddresses, int lportTag, String portId, Action action, int addOrRemove) {
64         programEgressAclFixedConntrackRule(dpid, allowedAddresses, lportTag, portId, action, addOrRemove);
65     }
66
67     @Override
68     protected String syncSpecificAclFlow(BigInteger dpId, int lportTag, int addOrRemove, Ace ace, String portId,
69             Map<String, List<MatchInfoBase>> flowMap, String flowName) {
70         List<MatchInfoBase> flows = flowMap.get(flowName);
71         flowName += "Egress" + lportTag + ace.getKey().getRuleName();
72         flows.add(AclServiceUtils.buildLPortTagMatch(lportTag));
73         flows.add(new NxMatchInfo(NxMatchFieldType.ct_state,
74             new long[] {AclConstants.TRACKED_NEW_CT_STATE, AclConstants.TRACKED_NEW_CT_STATE_MASK}));
75
76         Long elanId = AclServiceUtils.getElanIdFromInterface(portId, dataBroker);
77         List<ActionInfo> actionsInfos = new ArrayList<>();
78         actionsInfos.add(new ActionInfo(ActionType.nx_conntrack,
79             new String[] {"1", "0", elanId.toString(), "255"}, 2));
80         List<InstructionInfo> instructions = getDispatcherTableResubmitInstructions(actionsInfos);
81
82         syncFlow(dpId, NwConstants.INGRESS_ACL_FILTER_TABLE, flowName, AclConstants.PROTO_MATCH_PRIORITY,
83             "ACL", 0, 0, AclConstants.COOKIE_ACL_BASE, flows, instructions, addOrRemove);
84         return flowName;
85     }
86
87     /**
88      * Adds the rule to send the packet to the netfilter to check whether it is
89      * a known packet.
90      *
91      * @param dpId the dpId
92      * @param allowedAddresses the allowed addresses
93      * @param priority the priority of the flow
94      * @param flowId the flowId
95      * @param conntrackState the conntrack state of the packets thats should be
96      *        send
97      * @param conntrackMask the conntrack mask
98      * @param portId the portId
99      * @param addOrRemove whether to add or remove the flow
100      */
101     private void programConntrackRecircRules(BigInteger dpId, List<AllowedAddressPairs> allowedAddresses,
102             Integer priority, String flowId, int conntrackState, int conntrackMask, String portId, int addOrRemove) {
103         for (AllowedAddressPairs allowedAddress : allowedAddresses) {
104             IpPrefixOrAddress attachIp = allowedAddress.getIpAddress();
105             String attachMac = allowedAddress.getMacAddress().getValue();
106
107             List<MatchInfoBase> matches = new ArrayList<>();
108             matches.add(new MatchInfo(MatchFieldType.eth_type, new long[] {NwConstants.ETHTYPE_IPV4}));
109             matches.add(new NxMatchInfo(NxMatchFieldType.ct_state, new long[] {conntrackState, conntrackMask}));
110             matches.add(new MatchInfo(MatchFieldType.eth_src, new String[] {attachMac}));
111             matches.addAll(AclServiceUtils.buildIpMatches(attachIp, MatchFieldType.ipv4_source));
112
113             Long elanTag = AclServiceUtils.getElanIdFromInterface(portId, dataBroker);
114             List<InstructionInfo> instructions = new ArrayList<>();
115             List<ActionInfo> actionsInfos = new ArrayList<>();
116             actionsInfos.add(new ActionInfo(ActionType.nx_conntrack,
117                     new String[] {"0", "0", elanTag.toString(), Short.toString(
118                         NwConstants.INGRESS_ACL_FILTER_TABLE)}, 2));
119             instructions.add(new InstructionInfo(InstructionType.apply_actions, actionsInfos));
120
121             String flowName = "Egress_Fixed_Conntrk_Untrk_" + dpId + "_" + attachMac + "_"
122                     + String.valueOf(attachIp.getValue()) + "_" + flowId;
123             syncFlow(dpId, NwConstants.INGRESS_ACL_TABLE, flowName, AclConstants.PROTO_MATCH_PRIORITY, "ACL", 0, 0,
124                     AclConstants.COOKIE_ACL_BASE, matches, instructions, addOrRemove);
125         }
126     }
127
128     /**
129      * Programs the default connection tracking rules.
130      *
131      * @param dpid the dp id
132      * @param allowedAddresses the allowed addresses
133      * @param lportTag the lport tag
134      * @param portId the portId
135      * @param action the action
136      * @param write whether to add or remove the flow.
137      */
138     private void programEgressAclFixedConntrackRule(BigInteger dpid, List<AllowedAddressPairs> allowedAddresses,
139             int lportTag, String portId, Action action, int write) {
140         programConntrackRecircRules(dpid, allowedAddresses, AclConstants.CT_STATE_UNTRACKED_PRIORITY,
141             "Untracked",AclConstants.UNTRACKED_CT_STATE,AclConstants.UNTRACKED_CT_STATE_MASK, portId, write );
142         LOG.info("programEgressAclFixedConntrackRule :  default connection tracking rule are added.");
143     }
144 }