2 * Copyright (c) 2016 Red Hat, Inc. and others. All rights reserved.
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
8 package org.opendaylight.netvirt.aclservice;
10 import java.math.BigInteger;
11 import java.util.ArrayList;
12 import java.util.List;
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.api.AclServiceManager.MatchCriteria;
29 import org.opendaylight.netvirt.aclservice.utils.AclConstants;
30 import org.opendaylight.netvirt.aclservice.utils.AclServiceUtils;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.acl.access.list.entries.Ace;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.IpPrefixOrAddress;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.interfaces._interface.AllowedAddressPairs;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 * Provides the stateful implementation for egress (w.r.t VM) ACL service.
41 * Note: Table names used are w.r.t switch. Hence, switch ingress is VM egress
44 public class StatefulEgressAclServiceImpl extends AbstractEgressAclServiceImpl {
46 public StatefulEgressAclServiceImpl(DataBroker dataBroker, IMdsalApiManager mdsalManager) {
47 super(dataBroker, mdsalManager);
50 private static final Logger LOG = LoggerFactory.getLogger(StatefulEgressAclServiceImpl.class);
54 * Program conntrack rules.
56 * @param dpid the dpid
57 * @param dhcpMacAddress the dhcp mac address.
58 * @param allowedAddresses the allowed addresses
59 * @param lportTag the lport tag
60 * @param addOrRemove addorRemove
63 protected void programSpecificFixedRules(BigInteger dpid, String dhcpMacAddress,
64 List<AllowedAddressPairs> allowedAddresses, int lportTag, String portId, Action action, int addOrRemove) {
65 programEgressAclFixedConntrackRule(dpid, allowedAddresses, lportTag, portId, action, addOrRemove);
69 protected String syncSpecificAclFlow(BigInteger dpId, int lportTag, int addOrRemove, Ace ace, String portId,
70 Map<String, List<MatchInfoBase>> flowMap, String flowName) {
71 List<MatchInfoBase> flows = flowMap.get(flowName);
72 flowName += "Egress" + lportTag + ace.getKey().getRuleName();
73 flows.add(AclServiceUtils.buildLPortTagMatch(lportTag));
74 flows.add(new NxMatchInfo(NxMatchFieldType.ct_state,
75 new long[] {AclConstants.TRACKED_NEW_CT_STATE, AclConstants.TRACKED_NEW_CT_STATE_MASK}));
77 Long elanId = AclServiceUtils.getElanIdFromInterface(portId, dataBroker);
78 List<ActionInfo> actionsInfos = new ArrayList<>();
79 actionsInfos.add(new ActionInfo(ActionType.nx_conntrack,
80 new String[] {"1", "0", elanId.toString(), "255"}, 2));
81 List<InstructionInfo> instructions = getDispatcherTableResubmitInstructions(actionsInfos);
83 syncFlow(dpId, NwConstants.INGRESS_ACL_FILTER_TABLE, flowName, AclConstants.PROTO_MATCH_PRIORITY,
84 "ACL", 0, 0, AclConstants.COOKIE_ACL_BASE, flows, instructions, addOrRemove);
89 * Adds the rule to send the packet to the netfilter to check whether it is
92 * @param dpId the dpId
93 * @param allowedAddresses the allowed addresses
94 * @param priority the priority of the flow
95 * @param flowId the flowId
96 * @param conntrackState the conntrack state of the packets thats should be
98 * @param conntrackMask the conntrack mask
99 * @param portId the portId
100 * @param addOrRemove whether to add or remove the flow
102 private void programConntrackRecircRules(BigInteger dpId, List<AllowedAddressPairs> allowedAddresses,
103 Integer priority, String flowId, int conntrackState, int conntrackMask, String portId, int addOrRemove) {
104 for (AllowedAddressPairs allowedAddress : allowedAddresses) {
105 IpPrefixOrAddress attachIp = allowedAddress.getIpAddress();
106 String attachMac = allowedAddress.getMacAddress().getValue();
108 List<MatchInfoBase> matches = new ArrayList<>();
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, MatchCriteria.MATCH_SOURCE));
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));
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);
129 * Programs the default connection tracking rules.
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.
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.");