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 static org.opendaylight.genius.infra.Datastore.CONFIGURATION;
12 import com.google.common.collect.Lists;
13 import java.math.BigInteger;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.HashMap;
18 import java.util.HashSet;
19 import java.util.List;
21 import java.util.Map.Entry;
23 import java.util.SortedSet;
24 import java.util.stream.Collectors;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
27 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
28 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
29 import org.opendaylight.genius.mdsalutil.ActionInfo;
30 import org.opendaylight.genius.mdsalutil.FlowEntity;
31 import org.opendaylight.genius.mdsalutil.InstructionInfo;
32 import org.opendaylight.genius.mdsalutil.MDSALUtil;
33 import org.opendaylight.genius.mdsalutil.MatchInfoBase;
34 import org.opendaylight.genius.mdsalutil.NwConstants;
35 import org.opendaylight.genius.mdsalutil.actions.ActionNxConntrack;
36 import org.opendaylight.genius.mdsalutil.actions.ActionNxConntrack.NxCtAction;
37 import org.opendaylight.genius.mdsalutil.actions.ActionNxCtClear;
38 import org.opendaylight.genius.mdsalutil.actions.ActionNxResubmit;
39 import org.opendaylight.genius.mdsalutil.instructions.InstructionApplyActions;
40 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
41 import org.opendaylight.genius.mdsalutil.matches.MatchEthernetType;
42 import org.opendaylight.genius.mdsalutil.matches.MatchMetadata;
43 import org.opendaylight.genius.mdsalutil.nxmatches.NxMatchCtState;
44 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
45 import org.opendaylight.netvirt.aclservice.api.AclInterfaceCache;
46 import org.opendaylight.netvirt.aclservice.api.AclServiceListener;
47 import org.opendaylight.netvirt.aclservice.api.AclServiceManager.Action;
48 import org.opendaylight.netvirt.aclservice.api.utils.AclInterface;
49 import org.opendaylight.netvirt.aclservice.utils.AclConntrackClassifierType;
50 import org.opendaylight.netvirt.aclservice.utils.AclConstants;
51 import org.opendaylight.netvirt.aclservice.utils.AclDataUtil;
52 import org.opendaylight.netvirt.aclservice.utils.AclServiceOFFlowBuilder;
53 import org.opendaylight.netvirt.aclservice.utils.AclServiceUtils;
54 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.Acl;
55 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.acl.access.list.entries.Ace;
56 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.acl.access.list.entries.ace.Matches;
57 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.acl.access.list.entries.ace.matches.ace.type.AceIp;
58 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.acl.access.list.entries.ace.matches.ace.type.ace.ip.ace.ip.version.AceIpv4;
59 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.ServiceModeBase;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.ServiceModeEgress;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.DirectionBase;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.DirectionEgress;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.DirectionIngress;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.InterfaceAcl.InterfaceType;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.SecurityRuleAttr;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.interfaces._interface.AllowedAddressPairs;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.interfaces._interface.SubnetInfo;
69 import org.opendaylight.yangtools.yang.common.Uint64;
70 import org.slf4j.Logger;
71 import org.slf4j.LoggerFactory;
73 public abstract class AbstractAclServiceImpl implements AclServiceListener {
75 private static final Logger LOG = LoggerFactory.getLogger(AbstractAclServiceImpl.class);
77 protected final IMdsalApiManager mdsalManager;
78 protected final ManagedNewTransactionRunner txRunner;
79 protected final Class<? extends ServiceModeBase> serviceMode;
80 protected final AclDataUtil aclDataUtil;
81 protected final AclServiceUtils aclServiceUtils;
82 protected final JobCoordinator jobCoordinator;
83 protected final AclInterfaceCache aclInterfaceCache;
85 protected final Class<? extends DirectionBase> direction;
86 protected final String directionString;
89 * Initialize the member variables.
91 * @param serviceMode the service mode
92 * @param dataBroker the data broker instance.
93 * @param mdsalManager the mdsal manager instance.
94 * @param aclDataUtil the acl data util.
95 * @param aclServiceUtils the acl service util.
96 * @param jobCoordinator the job coordinator
97 * @param aclInterfaceCache the acl interface cache
99 public AbstractAclServiceImpl(Class<? extends ServiceModeBase> serviceMode, DataBroker dataBroker,
100 IMdsalApiManager mdsalManager, AclDataUtil aclDataUtil, AclServiceUtils aclServiceUtils,
101 JobCoordinator jobCoordinator, AclInterfaceCache aclInterfaceCache) {
102 this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
103 this.mdsalManager = mdsalManager;
104 this.serviceMode = serviceMode;
105 this.aclDataUtil = aclDataUtil;
106 this.aclServiceUtils = aclServiceUtils;
107 this.jobCoordinator = jobCoordinator;
108 this.aclInterfaceCache = aclInterfaceCache;
111 this.serviceMode.equals(ServiceModeEgress.class) ? DirectionIngress.class : DirectionEgress.class;
112 this.directionString = this.direction.equals(DirectionEgress.class) ? "Egress" : "Ingress";
116 public boolean applyAcl(AclInterface port) {
118 LOG.error("port cannot be null");
121 BigInteger dpId = port.getDpId();
122 String portId = port.getInterfaceId();
123 if (dpId == null || port.getLPortTag() == null) {
124 LOG.error("Unable to find DpId from ACL interface with id {}", portId);
128 LOG.debug("Applying ACL on port {} with DpId {}", port, dpId);
129 List<FlowEntity> flowEntries = new ArrayList<>();
130 if (port.getInterfaceType() == InterfaceType.DhcpService) {
131 programDhcpService(flowEntries, port, Action.ADD, NwConstants.ADD_FLOW);
133 if (port.getSecurityGroups() == null) {
134 LOG.info("Port {} without SGs", portId);
137 programAclWithAllowedAddress(flowEntries, port, port.getAllowedAddressPairs(),
138 Action.ADD, NwConstants.ADD_FLOW);
139 updateRemoteAclFilterTable(flowEntries, port, NwConstants.ADD_FLOW);
141 programFlows(AclConstants.ACL_JOB_KEY_PREFIX + portId, flowEntries, NwConstants.ADD_FLOW);
146 public boolean bindAcl(AclInterface port) {
148 LOG.error("Port cannot be null for binding ACL service");
156 public boolean unbindAcl(AclInterface port) {
158 LOG.error("Port cannot be null for unbinding ACL service");
161 if (port.getDpId() != null) {
168 public boolean updateAcl(AclInterface portBefore, AclInterface portAfter) {
169 // this check is to avoid situations of port update coming before interface state is up
170 if (portAfter.getDpId() == null || portAfter.getLPortTag() == null) {
171 LOG.debug("Unable to find DpId from ACL interface with id {} and lport {}", portAfter.getInterfaceId(),
172 portAfter.getLPortTag());
175 boolean result = true;
176 boolean isPortSecurityEnable = portAfter.isPortSecurityEnabled();
177 boolean isPortSecurityEnableBefore = portBefore.isPortSecurityEnabled();
178 // if port security is changed, apply/remove Acls
179 if (isPortSecurityEnableBefore != isPortSecurityEnable) {
180 LOG.debug("On ACL update, Port security is {} for {}", isPortSecurityEnable ? "Enabled" :
181 "Disabled", portAfter.getInterfaceId());
182 if (isPortSecurityEnable) {
183 result = applyAcl(portAfter);
185 result = removeAcl(portBefore);
187 } else if (isPortSecurityEnable) {
188 // Acls has been updated, find added/removed Acls and act accordingly.
189 processInterfaceUpdate(portBefore, portAfter);
190 LOG.debug("On ACL update, ACL has been updated for {}", portAfter.getInterfaceId());
191 } else if (portAfter.getInterfaceType() == InterfaceType.DhcpService) {
192 processDhcpServiceInterfaceUpdate(portBefore, portAfter);
198 private void processDhcpServiceInterfaceUpdate(AclInterface portBefore, AclInterface portAfter) {
199 List<FlowEntity> addFlowEntries = new ArrayList<>();
200 List<FlowEntity> deleteFlowEntries = new ArrayList<>();
201 List<AllowedAddressPairs> addedAaps = AclServiceUtils
202 .getUpdatedAllowedAddressPairs(portAfter.getAllowedAddressPairs(), portBefore.getAllowedAddressPairs());
203 List<AllowedAddressPairs> deletedAaps = AclServiceUtils
204 .getUpdatedAllowedAddressPairs(portBefore.getAllowedAddressPairs(), portAfter.getAllowedAddressPairs());
205 if (!deletedAaps.isEmpty()) {
206 processDhcpServiceUpdate(deleteFlowEntries, portBefore, deletedAaps, NwConstants.DEL_FLOW);
208 if (!addedAaps.isEmpty()) {
209 processDhcpServiceUpdate(addFlowEntries, portAfter, addedAaps, NwConstants.ADD_FLOW);
211 programFlows(AclConstants.ACL_JOB_KEY_PREFIX + portAfter.getInterfaceId(), deleteFlowEntries,
212 NwConstants.DEL_FLOW);
213 programFlows(AclConstants.ACL_JOB_KEY_PREFIX + portAfter.getInterfaceId(), addFlowEntries,
214 NwConstants.ADD_FLOW);
217 private void processInterfaceUpdate(AclInterface portBefore, AclInterface portAfter) {
218 List<FlowEntity> addFlowEntries = new ArrayList<>();
219 List<FlowEntity> deleteFlowEntries = new ArrayList<>();
220 List<AllowedAddressPairs> addedAaps = AclServiceUtils
221 .getUpdatedAllowedAddressPairs(portAfter.getAllowedAddressPairs(), portBefore.getAllowedAddressPairs());
222 List<AllowedAddressPairs> deletedAaps = AclServiceUtils
223 .getUpdatedAllowedAddressPairs(portBefore.getAllowedAddressPairs(), portAfter.getAllowedAddressPairs());
224 if (!deletedAaps.isEmpty()) {
225 programAclWithAllowedAddress(deleteFlowEntries, portBefore, deletedAaps, Action.UPDATE,
226 NwConstants.DEL_FLOW);
227 updateRemoteAclFilterTable(deleteFlowEntries, portBefore, portBefore.getSecurityGroups(), deletedAaps,
228 NwConstants.DEL_FLOW);
230 if (!addedAaps.isEmpty()) {
231 programAclWithAllowedAddress(addFlowEntries, portAfter, addedAaps, Action.UPDATE, NwConstants.ADD_FLOW);
232 updateRemoteAclFilterTable(addFlowEntries, portAfter, portAfter.getSecurityGroups(), addedAaps,
233 NwConstants.ADD_FLOW);
235 if (portAfter.getSubnetInfo() != null && portBefore.getSubnetInfo() == null) {
236 programBroadcastRules(addFlowEntries, portAfter, Action.UPDATE, NwConstants.ADD_FLOW);
238 handleSubnetChange(portBefore, portAfter, addFlowEntries, deleteFlowEntries);
240 List<Uuid> addedAcls = AclServiceUtils.getUpdatedAclList(portAfter.getSecurityGroups(),
241 portBefore.getSecurityGroups());
242 List<Uuid> deletedAcls = AclServiceUtils.getUpdatedAclList(portBefore.getSecurityGroups(),
243 portAfter.getSecurityGroups());
244 if (!deletedAcls.isEmpty() || !addedAcls.isEmpty()) {
245 handleAclChange(deleteFlowEntries, portBefore, deletedAcls, NwConstants.DEL_FLOW);
246 handleAclChange(addFlowEntries, portAfter, addedAcls, NwConstants.ADD_FLOW);
249 programFlows(AclConstants.ACL_JOB_KEY_PREFIX + portAfter.getInterfaceId(), deleteFlowEntries,
250 NwConstants.DEL_FLOW);
251 programFlows(AclConstants.ACL_JOB_KEY_PREFIX + portAfter.getInterfaceId(), addFlowEntries,
252 NwConstants.ADD_FLOW);
255 private void handleSubnetChange(AclInterface portBefore, AclInterface portAfter,
256 List<FlowEntity> addFlowEntries, List<FlowEntity> deleteFlowEntries) {
257 List<SubnetInfo> deletedSubnets =
258 AclServiceUtils.getSubnetDiff(portBefore.getSubnetInfo(), portAfter.getSubnetInfo());
259 List<SubnetInfo> addedSubnets =
260 AclServiceUtils.getSubnetDiff(portAfter.getSubnetInfo(), portBefore.getSubnetInfo());
262 if (deletedSubnets != null && !deletedSubnets.isEmpty()) {
263 programIcmpv6RARule(deleteFlowEntries, portAfter, deletedSubnets, NwConstants.DEL_FLOW);
264 programSubnetBroadcastRules(deleteFlowEntries, portAfter, deletedSubnets, NwConstants.DEL_FLOW);
266 if (addedSubnets != null && !addedSubnets.isEmpty()) {
267 programIcmpv6RARule(addFlowEntries, portAfter, addedSubnets, NwConstants.ADD_FLOW);
268 programSubnetBroadcastRules(addFlowEntries, portAfter, addedSubnets, NwConstants.ADD_FLOW);
272 private void handleAclChange(List<FlowEntity> flowEntries, AclInterface port, List<Uuid> aclList,
274 int operationForAclRules = addOrRemove == NwConstants.DEL_FLOW ? NwConstants.MOD_FLOW : addOrRemove;
275 programAclRules(flowEntries, port, aclList, operationForAclRules);
276 updateRemoteAclFilterTable(flowEntries, port, aclList, port.getAllowedAddressPairs(), addOrRemove);
277 programAclDispatcherTable(flowEntries, port, addOrRemove);
280 protected SortedSet<Integer> getRemoteAclTags(AclInterface port) {
281 return this.direction == DirectionIngress.class ? port.getIngressRemoteAclTags()
282 : port.getEgressRemoteAclTags();
285 protected void programAclDispatcherTable(List<FlowEntity> flowEntries, AclInterface port, int addOrRemove) {
286 SortedSet<Integer> remoteAclTags = getRemoteAclTags(port);
287 if (remoteAclTags.isEmpty()) {
288 LOG.debug("No {} rules with remote group id for port={}", this.directionString, port.getInterfaceId());
291 Integer firstRemoteAclTag = remoteAclTags.first();
292 Integer lastRemoteAclTag = remoteAclTags.last();
294 programFirstRemoteAclEntryInDispatcherTable(flowEntries, port, firstRemoteAclTag, addOrRemove);
295 programLastRemoteAclEntryInDispatcherTable(flowEntries, port, lastRemoteAclTag, addOrRemove);
297 Integer previousRemoteAclTag = firstRemoteAclTag;
298 for (Integer remoteAclTag : remoteAclTags) {
299 if (remoteAclTag.equals(firstRemoteAclTag)) {
302 List<MatchInfoBase> matches = new ArrayList<>();
303 matches.addAll(AclServiceUtils.buildMatchesForLPortTagAndRemoteAclTag(port.getLPortTag(),
304 previousRemoteAclTag, serviceMode));
305 String flowId = this.directionString + "_ACL_Dispatcher_" + port.getDpId() + "_" + port.getLPortTag() + "_"
308 List<InstructionInfo> instructions =
309 AclServiceOFFlowBuilder.getGotoInstructionInfo(getAclRuleBasedFilterTable());
310 instructions.add(AclServiceUtils.getWriteMetadataForRemoteAclTag(remoteAclTag));
311 addFlowEntryToList(flowEntries, Uint64.valueOf(port.getDpId()), getAclFilterCumDispatcherTable(), flowId,
312 AclConstants.ACE_GOTO_NEXT_REMOTE_ACL_PRIORITY, 0, 0, AclConstants.COOKIE_ACL_BASE, matches,
313 instructions, addOrRemove);
315 previousRemoteAclTag = remoteAclTag;
319 protected void programFirstRemoteAclEntryInDispatcherTable(List<FlowEntity> flowEntries, AclInterface port,
320 Integer firstRemoteAclTag, int addOrRemove) {
321 List<MatchInfoBase> matches = new ArrayList<>();
322 matches.add(AclServiceUtils.buildLPortTagMatch(port.getLPortTag(), serviceMode));
323 String flowId = this.directionString + "_ACL_Dispatcher_First_" + port.getDpId() + "_" + port.getLPortTag()
324 + "_" + firstRemoteAclTag;
326 List<InstructionInfo> instructions =
327 AclServiceOFFlowBuilder.getGotoInstructionInfo(getAclRuleBasedFilterTable());
328 instructions.add(AclServiceUtils.getWriteMetadataForRemoteAclTag(firstRemoteAclTag));
329 addFlowEntryToList(flowEntries, Uint64.valueOf(port.getDpId()), getAclFilterCumDispatcherTable(), flowId,
330 AclConstants.ACE_FIRST_REMOTE_ACL_PRIORITY, 0, 0, AclConstants.COOKIE_ACL_BASE, matches, instructions,
334 protected void programLastRemoteAclEntryInDispatcherTable(List<FlowEntity> flowEntries, AclInterface port,
335 Integer lastRemoteAclTag, int addOrRemove) {
336 List<MatchInfoBase> matches = new ArrayList<>();
337 matches.addAll(AclServiceUtils.buildMatchesForLPortTagAndRemoteAclTag(port.getLPortTag(), lastRemoteAclTag,
339 String flowId = this.directionString + "_ACL_Dispatcher_Last_" + port.getDpId() + "_" + port.getLPortTag() + "_"
342 List<InstructionInfo> instructions = AclServiceOFFlowBuilder.getDropInstructionInfo();
343 addFlowEntryToList(flowEntries, Uint64.valueOf(port.getDpId()), getAclFilterCumDispatcherTable(), flowId,
344 AclConstants.ACE_LAST_REMOTE_ACL_PRIORITY, 0, 0, AclServiceUtils.getDropFlowCookie(port.getLPortTag()),
345 matches, instructions, addOrRemove);
348 private void programAclWithAllowedAddress(List<FlowEntity> flowEntries, AclInterface port,
349 List<AllowedAddressPairs> allowedAddresses, Action action, int addOrRemove) {
350 Uint64 dpId = Uint64.valueOf(port.getDpId());
351 int lportTag = port.getLPortTag();
352 LOG.debug("Applying ACL Allowed Address on DpId {}, lportTag {}, Action {}", dpId, lportTag, action);
353 String portId = port.getInterfaceId();
354 programAntiSpoofingRules(flowEntries, port, allowedAddresses, action, addOrRemove);
355 programAclPortSpecificFixedRules(flowEntries, dpId, allowedAddresses, lportTag, portId, action, addOrRemove);
356 if (action == Action.ADD || action == Action.REMOVE) {
357 programAclRules(flowEntries, port, port.getSecurityGroups(), addOrRemove);
358 programAclDispatcherTable(flowEntries, port, addOrRemove);
363 * Programs the acl custom rules.
365 * @param flowEntries the flow entries
366 * @param port acl interface
367 * @param aclUuidList the list of acl uuid to be applied
368 * @param addOrRemove whether to delete or add flow
369 * @return program succeeded
371 protected boolean programAclRules(List<FlowEntity> flowEntries, AclInterface port, List<Uuid> aclUuidList,
373 BigInteger dpId = port.getDpId();
374 LOG.debug("Applying custom rules on DpId {}, lportTag {}", dpId, port.getLPortTag());
375 if (aclUuidList == null || dpId == null) {
376 LOG.warn("{} ACL parameters can not be null. dpId={}, aclUuidList={}", this.directionString, dpId,
380 for (Uuid aclUuid : aclUuidList) {
381 Acl acl = this.aclDataUtil.getAcl(aclUuid.getValue());
383 LOG.warn("The ACL {} not found in cache", aclUuid.getValue());
386 for (Ace ace : AclServiceUtils.aceList(acl)) {
387 programAceRule(flowEntries, port, aclUuid.getValue(), ace, addOrRemove);
394 * Programs the ace specific rule.
396 * @param flowEntries flow entries
397 * @param port acl interface
398 * @param aclName the acl name
399 * @param ace rule to be program
400 * @param addOrRemove whether to delete or add flow
402 protected void programAceRule(List<FlowEntity> flowEntries, AclInterface port, String aclName, Ace ace,
404 SecurityRuleAttr aceAttr = AclServiceUtils.getAccessListAttributes(ace);
405 if (aceAttr == null) {
406 LOG.error("Ace {} of Acl {} is either null or not having SecurityRuleAttr",
407 ace == null ? null : ace.getRuleName(), aclName);
410 if (addOrRemove == NwConstants.ADD_FLOW && aceAttr.isDeleted()) {
411 LOG.trace("Ignoring {} rule which is already deleted", ace.getRuleName());
414 if (!isValidDirection(aceAttr.getDirection())) {
415 LOG.trace("Ignoring {} direction while processing for {} ACE Rule {}", aceAttr.getDirection(),
416 this.directionString, ace.getRuleName());
419 LOG.debug("Program {} ACE rule for dpId={}, lportTag={}, addOrRemove={}, ace={}, portId={}",
420 this.directionString, port.getDpId(), port.getLPortTag(), addOrRemove, ace.getRuleName(),
421 port.getInterfaceId());
423 Matches matches = ace.getMatches();
424 if (matches != null && matches.getAceType() instanceof AceIp) {
425 Map<String, List<MatchInfoBase>> flowMap = AclServiceOFFlowBuilder.programIpFlow(matches);
426 if (!AclServiceUtils.doesAceHaveRemoteGroupId(aceAttr)) {
427 // programming for ACE which doesn't have any remote group Id
428 programForAceNotHavingRemoteAclId(flowEntries, port, aclName, ace, flowMap, addOrRemove);
430 Uuid remoteAclId = aceAttr.getRemoteGroupId();
431 // programming for ACE which have remote group Id
432 programAceSpecificFlows(flowEntries, port, aclName, ace, flowMap, remoteAclId, addOrRemove);
437 protected void programForAceNotHavingRemoteAclId(List<FlowEntity> flowEntries, AclInterface port, String aclName,
438 Ace ace, @Nullable Map<String, List<MatchInfoBase>> flowMap, int addOrRemove) {
439 if (null == flowMap) {
442 MatchInfoBase lportTagMatch = AclServiceUtils.buildLPortTagMatch(port.getLPortTag(), serviceMode);
443 List<InstructionInfo> instructions = AclServiceOFFlowBuilder.getGotoInstructionInfo(getAclCommitterTable());
444 Integer flowPriority = this.aclServiceUtils.getAceFlowPriority(aclName);
446 for (Entry<String, List<MatchInfoBase>> entry : flowMap.entrySet()) {
447 String flowName = entry.getKey();
448 List<MatchInfoBase> matches = entry.getValue();
449 matches.add(lportTagMatch);
450 String flowId = flowName + this.directionString + "_" + port.getDpId() + "_" + port.getLPortTag() + "_"
451 + ace.key().getRuleName();
453 int operation = addOrRemove == NwConstants.MOD_FLOW ? NwConstants.DEL_FLOW : addOrRemove;
454 addFlowEntryToList(flowEntries, Uint64.valueOf(port.getDpId()), getAclFilterCumDispatcherTable(),
455 flowId, flowPriority, 0, 0, AclConstants.COOKIE_ACL_BASE, matches, instructions, operation);
457 if (addOrRemove != NwConstants.DEL_FLOW) {
458 programAclForExistingTrafficTable(port, ace, addOrRemove, flowName, matches, flowPriority);
463 protected void programAceSpecificFlows(List<FlowEntity> flowEntries, AclInterface port, String aclName, Ace ace,
464 @Nullable Map<String, List<MatchInfoBase>> flowMap, Uuid remoteAclId, int addOrRemove) {
465 if (null == flowMap) {
468 Integer remoteAclTag = this.aclServiceUtils.getAclTag(remoteAclId);
469 if (remoteAclTag == null || remoteAclTag == AclConstants.INVALID_ACL_TAG) {
470 LOG.error("remoteAclTag={} is null or invalid for remoteAclId={}", remoteAclTag, remoteAclId);
473 List<MatchInfoBase> lportAndAclMatches =
474 AclServiceUtils.buildMatchesForLPortTagAndRemoteAclTag(port.getLPortTag(), remoteAclTag, serviceMode);
475 List<InstructionInfo> instructions = AclServiceOFFlowBuilder.getGotoInstructionInfo(getAclRemoteAclTable());
476 Integer flowPriority = this.aclServiceUtils.getAceFlowPriority(aclName);
478 for (Entry<String, List<MatchInfoBase>> entry : flowMap.entrySet()) {
479 String flowName = entry.getKey();
480 List<MatchInfoBase> matches = entry.getValue();
481 matches.addAll(lportAndAclMatches);
482 String flowId = flowName + this.directionString + "_" + port.getDpId() + "_" + port.getLPortTag() + "_"
483 + ace.key().getRuleName();
485 int operation = addOrRemove == NwConstants.MOD_FLOW ? NwConstants.DEL_FLOW : addOrRemove;
486 addFlowEntryToList(flowEntries, Uint64.valueOf(port.getDpId()), getAclRuleBasedFilterTable(), flowId,
487 flowPriority, 0, 0, AclConstants.COOKIE_ACL_BASE, matches, instructions, operation);
489 if (addOrRemove != NwConstants.DEL_FLOW) {
490 programAclForExistingTrafficTable(port, ace, addOrRemove, flowName, matches, flowPriority);
495 private void programAclForExistingTrafficTable(AclInterface port, Ace ace, int addOrRemove, String flowName,
496 List<MatchInfoBase> matches, Integer priority) {
497 if (port == null || port.getElanId() == null) {
498 LOG.debug("Acl interface or elan id is null, No need to update traffic flow table.");
502 AceIp acl = (AceIp) ace.getMatches().getAceType();
503 final String newFlowName = flowName + this.directionString + "_" + port.getDpId() + "_" + port.getLPortTag()
504 + "_" + (acl.getAceIpVersion() instanceof AceIpv4 ? "_IPv4" : "_IPv6") + "_FlowAfterRuleDeleted";
506 final List<MatchInfoBase> newMatches =
507 matches.stream().filter(obj -> !(obj instanceof NxMatchCtState || obj instanceof MatchMetadata))
508 .collect(Collectors.toList());
509 newMatches.add(AclServiceUtils.buildLPortTagMatch(port.getLPortTag(), serviceMode));
510 newMatches.add(new NxMatchCtState(AclConstants.TRACKED_RPL_CT_STATE, AclConstants.TRACKED_RPL_CT_STATE_MASK));
512 List<InstructionInfo> instructions =
513 AclServiceUtils.createCtMarkInstructionForNewState(getAclFilterCumDispatcherTable(), port.getElanId());
514 // Reversing the flow add/delete operation for this table.
515 List<FlowEntity> flowEntries = new ArrayList<>();
516 int operation = addOrRemove == NwConstants.ADD_FLOW ? NwConstants.DEL_FLOW : NwConstants.ADD_FLOW;
517 addFlowEntryToList(flowEntries, Uint64.valueOf(port.getDpId()), getAclForExistingTrafficTable(), newFlowName,
518 priority, 0, AclServiceUtils.getHardTimoutForApplyStatefulChangeOnExistingTraffic(ace, aclServiceUtils),
519 AclConstants.COOKIE_ACL_BASE, newMatches, instructions, operation);
520 programFlows(AclConstants.ACL_JOB_KEY_PREFIX + port.getInterfaceId(), flowEntries, operation);
524 public boolean removeAcl(AclInterface port) {
525 if (port.getDpId() == null) {
526 LOG.warn("Unable to find DP Id from ACL interface with id {}", port.getInterfaceId());
529 List<FlowEntity> flowEntries = new ArrayList<>();
530 if (port.getInterfaceType() == InterfaceType.DhcpService) {
531 programDhcpService(flowEntries, port, Action.REMOVE, NwConstants.DEL_FLOW);
533 programAclWithAllowedAddress(flowEntries, port, port.getAllowedAddressPairs(),
534 Action.REMOVE, NwConstants.DEL_FLOW);
535 updateRemoteAclFilterTable(flowEntries, port, NwConstants.DEL_FLOW);
537 programFlows(AclConstants.ACL_JOB_KEY_PREFIX + port.getInterfaceId(), flowEntries, NwConstants.DEL_FLOW);
542 public boolean applyAce(AclInterface port, String aclName, Ace ace) {
543 if (!port.isPortSecurityEnabled() || port.getDpId() == null) {
546 List<FlowEntity> flowEntries = new ArrayList<>();
547 programAceRule(flowEntries, port, aclName, ace, NwConstants.ADD_FLOW);
548 programFlows(AclConstants.ACL_JOB_KEY_PREFIX + port.getInterfaceId(), flowEntries, NwConstants.ADD_FLOW);
553 public boolean removeAce(AclInterface port, String aclName, Ace ace) {
554 if (!port.isPortSecurityEnabled() || port.getDpId() == null) {
557 List<FlowEntity> flowEntries = new ArrayList<>();
558 programAceRule(flowEntries, port, aclName, ace, NwConstants.MOD_FLOW);
559 programFlows(AclConstants.ACL_JOB_KEY_PREFIX + port.getInterfaceId(), flowEntries, NwConstants.DEL_FLOW);
564 public void updateRemoteAcl(Acl aclBefore, Acl aclAfter, Collection<AclInterface> portsBefore) {
565 handleRemoteAclUpdate(aclBefore, aclAfter, portsBefore);
571 * @param aclInterface the acl interface
573 public abstract void bindService(AclInterface aclInterface);
578 * @param aclInterface the acl interface
580 protected abstract void unbindService(AclInterface aclInterface);
583 * Programs DHCP Service flows.
585 * @param flowEntries the flow entries
586 * @param port the acl interface
587 * @param action add/modify/remove action
588 * @param addOrRemove addorRemove
590 protected abstract void programDhcpService(List<FlowEntity> flowEntries, AclInterface port,
591 Action action, int addOrRemove);
594 * Programs DHCP service flows.
596 * @param flowEntries the flow entries
597 * @param port the acl interface
598 * @param allowedAddresses the allowed addresses
599 * @param addOrRemove addorRemove
601 protected abstract void processDhcpServiceUpdate(List<FlowEntity> flowEntries, AclInterface port,
602 List<AllowedAddressPairs> allowedAddresses, int addOrRemove);
605 * Programs the anti-spoofing rules.
607 * @param flowEntries the flow entries
608 * @param port the acl interface
609 * @param allowedAddresses the allowed addresses
610 * @param action add/modify/remove action
611 * @param addOrRemove addorRemove
613 protected abstract void programAntiSpoofingRules(List<FlowEntity> flowEntries, AclInterface port,
614 List<AllowedAddressPairs> allowedAddresses, Action action, int addOrRemove);
617 * Programs broadcast rules.
619 * @param flowEntries the flow entries
620 * @param port the Acl Interface port
621 * @param addOrRemove whether to delete or add flow
623 protected abstract void programBroadcastRules(List<FlowEntity> flowEntries, AclInterface port, Action action,
627 * Programs broadcast rules.
629 * @param flowEntries the flow entries
630 * @param port the Acl Interface port
631 * @param subnetInfoList the port subnet info list
632 * @param addOrRemove whether to delete or add flow
634 protected abstract void programSubnetBroadcastRules(List<FlowEntity> flowEntries, AclInterface port,
635 List<SubnetInfo> subnetInfoList, int addOrRemove);
637 protected abstract void programIcmpv6RARule(List<FlowEntity> flowEntries, AclInterface port,
638 List<SubnetInfo> subnets, int addOrRemove);
658 * the list of matches to be writted
659 * @param instructions
660 * the list of instruction to be written.
662 * add or remove the entries.
664 protected void addFlowEntryToList(List<FlowEntity> flowEntries, Uint64 dpId, short tableId, String flowId,
665 int priority, int idleTimeOut, int hardTimeOut, Uint64 cookie, List<? extends MatchInfoBase> matches,
666 List<InstructionInfo> instructions, int addOrRemove) {
667 List<InstructionInfo> instructionInfos = null;
668 if (addOrRemove == NwConstants.ADD_FLOW) {
669 instructionInfos = instructions;
671 FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpId, tableId, flowId, priority,
672 flowId, idleTimeOut, hardTimeOut, cookie, matches, instructionInfos);
673 LOG.trace("Adding flow to list: DpnId {}, flowId {}", dpId, flowId);
674 flowEntries.add(flowEntity);
677 protected void programFlows(String jobName, List<FlowEntity> flowEntries, int addOrRemove) {
678 List<List<FlowEntity>> flowEntityParts = Lists.partition(flowEntries, AclConstants.FLOWS_PER_TRANSACTION);
679 for (List<FlowEntity> part : flowEntityParts) {
680 jobCoordinator.enqueueJob(jobName,
681 () -> Collections.singletonList(txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION,
683 if (addOrRemove == NwConstants.ADD_FLOW) {
684 for (FlowEntity flowEntity: part) {
685 mdsalManager.addFlow(tx, flowEntity);
688 for (FlowEntity flowEntity: part) {
689 mdsalManager.removeFlow(tx, flowEntity);
692 })), AclConstants.JOB_MAX_RETRIES);
696 protected List<InstructionInfo> getDispatcherTableResubmitInstructions() {
697 return getDispatcherTableResubmitInstructions(new ArrayList<>());
701 * Gets the dispatcher table resubmit instructions based on ingress/egress service mode w.r.t switch.
703 * @param actionsInfos
705 * @return the instructions for dispatcher table resubmit
707 protected List<InstructionInfo> getDispatcherTableResubmitInstructions(List<ActionInfo> actionsInfos) {
708 short dispatcherTableId = NwConstants.LPORT_DISPATCHER_TABLE;
709 if (ServiceModeEgress.class.equals(this.serviceMode)) {
710 dispatcherTableId = NwConstants.EGRESS_LPORT_DISPATCHER_TABLE;
713 List<InstructionInfo> instructions = new ArrayList<>();
714 actionsInfos.add(new ActionNxResubmit(dispatcherTableId));
715 instructions.add(new InstructionApplyActions(actionsInfos));
719 protected void handleRemoteAclUpdate(Acl aclBefore, Acl aclAfter, Collection<AclInterface> portsBefore) {
720 String aclName = aclAfter.getAclName();
721 Collection<AclInterface> interfaceList = aclDataUtil.getInterfaceList(new Uuid(aclName));
722 if (interfaceList.isEmpty()) {
723 LOG.trace("handleRemoteAclUpdate: No interfaces found with ACL={}", aclName);
726 Set<Uuid> remoteAclsBefore = AclServiceUtils.getRemoteAclIdsByDirection(aclBefore, this.direction);
727 Set<Uuid> remoteAclsAfter = AclServiceUtils.getRemoteAclIdsByDirection(aclAfter, this.direction);
729 Set<Uuid> remoteAclsAdded = new HashSet<>(remoteAclsAfter);
730 remoteAclsAdded.removeAll(remoteAclsBefore);
732 Set<Uuid> remoteAclsDeleted = new HashSet<>(remoteAclsBefore);
733 remoteAclsDeleted.removeAll(remoteAclsAfter);
735 List<FlowEntity> addFlowEntries = new ArrayList<>();
736 List<FlowEntity> deleteFlowEntries = new ArrayList<>();
737 if (!remoteAclsAdded.isEmpty() || !remoteAclsDeleted.isEmpty()) {
738 // delete and add flows in ACL dispatcher table for all applicable
740 for (AclInterface portBefore : portsBefore) {
741 programAclDispatcherTable(deleteFlowEntries, portBefore, NwConstants.DEL_FLOW);
743 for (AclInterface port : interfaceList) {
744 programAclDispatcherTable(addFlowEntries, port, NwConstants.ADD_FLOW);
747 Set<BigInteger> dpns = interfaceList.stream().map(AclInterface::getDpId).collect(Collectors.toSet());
749 programRemoteAclTable(deleteFlowEntries, aclName, remoteAclsDeleted, dpns, NwConstants.DEL_FLOW);
750 programRemoteAclTable(addFlowEntries, aclName, remoteAclsAdded, dpns, NwConstants.ADD_FLOW);
752 programFlows(aclName, deleteFlowEntries, NwConstants.DEL_FLOW);
753 programFlows(aclName, addFlowEntries, NwConstants.ADD_FLOW);
756 private void programRemoteAclTable(List<FlowEntity> flowEntries, String aclName, Set<Uuid> remoteAclIds,
757 Set<BigInteger> dpns, int addOrRemove) {
758 for (Uuid remoteAclId : remoteAclIds) {
759 Collection<AclInterface> remoteAclInterfaces = aclDataUtil.getInterfaceList(remoteAclId);
760 if (remoteAclInterfaces.isEmpty()) {
763 Set<AllowedAddressPairs> aaps =
764 remoteAclInterfaces.stream().map(AclInterface::getAllowedAddressPairs).flatMap(List::stream)
765 .filter(AclServiceUtils::isNotIpAllNetwork).collect(Collectors.toSet());
767 Integer aclTag = aclServiceUtils.getAclTag(remoteAclId);
768 if (addOrRemove == NwConstants.ADD_FLOW) {
769 for (BigInteger dpn : dpns) {
770 for (AllowedAddressPairs aap : aaps) {
771 programRemoteAclTableFlow(flowEntries, Uint64.valueOf(dpn), aclTag, aap, addOrRemove);
774 } else if (addOrRemove == NwConstants.DEL_FLOW) {
775 Set<BigInteger> remoteAclDpns = new HashSet<>();
776 Map<String, Set<AclInterface>> mapAclWithPortSet =
777 aclDataUtil.getRemoteAclInterfaces(remoteAclId, this.direction);
778 if (mapAclWithPortSet != null) {
779 Map<String, Set<AclInterface>> copyOfMapAclWithPortSet = new HashMap<>(mapAclWithPortSet);
780 copyOfMapAclWithPortSet.remove(aclName);
781 remoteAclDpns = collectDpns(copyOfMapAclWithPortSet);
783 Set<BigInteger> dpnsToOperate = new HashSet<>(dpns);
784 dpnsToOperate.removeAll(remoteAclDpns);
786 "Deleting flows in Remote ACL table for remoteAclId={}, direction={}, dpnsToOperate={}, "
787 + "remoteAclDpns={}, dpns={}",
788 remoteAclId.getValue(), directionString, dpnsToOperate, remoteAclDpns, dpns);
790 for (BigInteger dpn : dpnsToOperate) {
791 for (AllowedAddressPairs aap : aaps) {
792 programRemoteAclTableFlow(flowEntries, Uint64.valueOf(dpn), aclTag, aap, addOrRemove);
799 private void updateRemoteAclFilterTable(List<FlowEntity> flowEntries, AclInterface port, int addOrRemove) {
800 updateRemoteAclFilterTable(flowEntries, port, port.getSecurityGroups(), port.getAllowedAddressPairs(),
804 private void updateRemoteAclFilterTable(List<FlowEntity> flowEntries, AclInterface port, List<Uuid> aclList,
805 List<AllowedAddressPairs> aaps, int addOrRemove) {
806 if (aclList == null) {
807 LOG.debug("Port {} without SGs", port.getInterfaceId());
810 String portId = port.getInterfaceId();
811 LOG.trace("updateRemoteAclFilterTable for portId={}, aclList={}, aaps={}, addOrRemove={}", portId, aclList,
813 for (Uuid aclId : aclList) {
814 if (aclDataUtil.getRemoteAcl(aclId, this.direction) != null) {
815 Integer aclTag = aclServiceUtils.getAclTag(aclId);
816 if (addOrRemove == NwConstants.ADD_FLOW) {
817 syncRemoteAclTable(flowEntries, portId, aclId, aclTag, aaps, addOrRemove);
819 else if (addOrRemove == NwConstants.DEL_FLOW) {
820 jobCoordinator.enqueueJob(aclId.getValue(), () -> {
821 List<FlowEntity> remoteTableFlowEntries = new ArrayList<>();
822 syncRemoteAclTable(remoteTableFlowEntries, portId, aclId, aclTag, aaps, addOrRemove);
823 programFlows(AclConstants.ACL_JOB_KEY_PREFIX + aclId.getValue(),
824 remoteTableFlowEntries, NwConstants.DEL_FLOW);
825 return Collections.emptyList();
830 Set<Uuid> remoteAclIds = aclServiceUtils.getRemoteAclIdsByDirection(aclList, direction);
831 for (Uuid remoteAclId : remoteAclIds) {
832 syncRemoteAclTableFromOtherDpns(flowEntries, port, remoteAclId, addOrRemove);
836 private void syncRemoteAclTable(List<FlowEntity> flowEntries, String portId, Uuid acl, Integer aclTag,
837 List<AllowedAddressPairs> aaps, int addOrRemove) {
838 Map<String, Set<AclInterface>> mapAclWithPortSet = aclDataUtil.getRemoteAclInterfaces(acl, this.direction);
839 Set<BigInteger> dpns = collectDpns(mapAclWithPortSet);
840 for (AllowedAddressPairs aap : aaps) {
841 if (!AclServiceUtils.isNotIpAllNetwork(aap)) {
844 if (aclServiceUtils.skipDeleteInCaseOfOverlappingIP(portId, acl, aap.getIpAddress(),
846 LOG.debug("Skipping delete of IP={} in remote ACL table for remoteAclId={}, portId={}",
847 aap.getIpAddress(), portId, acl.getValue());
850 for (BigInteger dpId : dpns) {
851 programRemoteAclTableFlow(flowEntries, Uint64.valueOf(dpId), aclTag, aap, addOrRemove);
856 private void syncRemoteAclTableFromOtherDpns(List<FlowEntity> flowEntries, AclInterface port, Uuid remoteAclId,
858 Collection<AclInterface> aclInterfaces = aclDataUtil.getInterfaceList(remoteAclId);
860 if (!aclInterfaces.isEmpty() && isFirstPortInDpnWithRemoteAclId(port, remoteAclId)) {
861 Integer aclTag = aclServiceUtils.getAclTag(remoteAclId);
862 for (AclInterface aclInterface : aclInterfaces) {
863 if (port.getInterfaceId().equals(aclInterface.getInterfaceId())) {
866 for (AllowedAddressPairs aap : aclInterface.getAllowedAddressPairs()) {
867 if (AclServiceUtils.isNotIpAllNetwork(aap)) {
868 programRemoteAclTableFlow(flowEntries, Uint64.valueOf(port.getDpId()),
869 aclTag, aap, addOrRemove);
876 private boolean isFirstPortInDpnWithRemoteAclId(AclInterface port, Uuid remoteAclId) {
877 String portId = port.getInterfaceId();
878 BigInteger dpId = port.getDpId();
879 Map<String, Set<AclInterface>> remoteAclInterfacesMap =
880 aclDataUtil.getRemoteAclInterfaces(remoteAclId, direction);
881 if (remoteAclInterfacesMap != null) {
882 for (Set<AclInterface> interfaceSet : remoteAclInterfacesMap.values()) {
883 for (AclInterface aclInterface : interfaceSet) {
884 if (portId.equals(aclInterface.getInterfaceId())) {
887 if (dpId.equals(aclInterface.getDpId())) {
896 protected abstract void programRemoteAclTableFlow(List<FlowEntity> flowEntries, Uint64 dpId, Integer aclTag,
897 AllowedAddressPairs aap, int addOrRemove);
899 protected Set<BigInteger> collectDpns(@Nullable Map<String, Set<AclInterface>> mapAclWithPortSet) {
900 Set<BigInteger> dpns = new HashSet<>();
901 if (mapAclWithPortSet == null) {
904 for (Set<AclInterface> innerSet : mapAclWithPortSet.values()) {
905 if (innerSet == null) {
908 for (AclInterface inter : innerSet) {
909 dpns.add(inter.getDpId());
916 * Programs the port specific fixed rules.
918 * @param flowEntries the flow entries
919 * @param dpId the dp id
920 * @param allowedAddresses the allowed addresses
921 * @param lportTag the lport tag
922 * @param portId the portId
923 * @param action the action
924 * @param write whether to add or remove the flow.
926 protected void programAclPortSpecificFixedRules(List<FlowEntity> flowEntries, Uint64 dpId,
927 List<AllowedAddressPairs> allowedAddresses, int lportTag, String portId, Action action, int write) {
928 programGotoClassifierTableRules(flowEntries, dpId, allowedAddresses, lportTag, write);
929 if (action == Action.ADD || action == Action.REMOVE) {
930 programConntrackRecircRules(flowEntries, dpId, allowedAddresses, lportTag, portId, write);
931 programPortSpecificDropRules(flowEntries, dpId, lportTag, write);
932 programAclCommitRules(flowEntries, dpId, lportTag, portId, write);
934 LOG.info("programAclPortSpecificFixedRules: flows for dpId={}, lportId={}, action={}, write={}", dpId, lportTag,
938 protected abstract void programGotoClassifierTableRules(List<FlowEntity> flowEntries, Uint64 dpId,
939 List<AllowedAddressPairs> aaps, int lportTag, int addOrRemove);
942 * Adds the rule to send the packet to the netfilter to check whether it is a known packet.
944 * @param flowEntries the flow entries
945 * @param dpId the dpId
946 * @param aaps the allowed address pairs
947 * @param lportTag the lport tag
948 * @param portId the portId
949 * @param addOrRemove whether to add or remove the flow
951 protected void programConntrackRecircRules(List<FlowEntity> flowEntries, Uint64 dpId,
952 List<AllowedAddressPairs> aaps, int lportTag, String portId, int addOrRemove) {
953 if (AclServiceUtils.doesIpv4AddressExists(aaps)) {
954 programConntrackRecircRule(flowEntries, dpId, lportTag, portId, MatchEthernetType.IPV4, addOrRemove);
956 if (AclServiceUtils.doesIpv6AddressExists(aaps)) {
957 programConntrackRecircRule(flowEntries, dpId, lportTag, portId, MatchEthernetType.IPV6, addOrRemove);
961 protected void programConntrackRecircRule(List<FlowEntity> flowEntries, Uint64 dpId, int lportTag,
962 String portId, MatchEthernetType matchEtherType, int addOrRemove) {
963 List<MatchInfoBase> matches = new ArrayList<>();
964 matches.add(matchEtherType);
965 matches.add(AclServiceUtils.buildLPortTagMatch(lportTag, serviceMode));
967 List<InstructionInfo> instructions = new ArrayList<>();
968 if (addOrRemove == NwConstants.ADD_FLOW) {
969 Long elanTag = getElanIdFromAclInterface(portId);
970 if (elanTag == null) {
971 LOG.error("ElanId not found for portId={}; Context: dpId={}, lportTag={}, addOrRemove={},", portId,
972 dpId, lportTag, addOrRemove);
975 List<ActionInfo> actionsInfos = new ArrayList<>();
976 actionsInfos.add(new ActionNxConntrack(2, 0, 0, elanTag.intValue(), getAclForExistingTrafficTable()));
977 instructions.add(new InstructionApplyActions(actionsInfos));
981 this.directionString + "_Fixed_Conntrk_" + dpId + "_"
982 + lportTag + "_" + matchEtherType + "_Recirc";
983 addFlowEntryToList(flowEntries, dpId, getAclConntrackSenderTable(), flowName,
984 AclConstants.ACL_DEFAULT_PRIORITY, 0, 0, AclConstants.COOKIE_ACL_BASE, matches, instructions,
989 * Adds the rules to drop the unknown/invalid packets .
991 * @param flowEntries the flow entries
992 * @param dpId the dpId
993 * @param lportTag the lport tag
994 * @param addOrRemove whether to add or remove the flow
996 protected void programPortSpecificDropRules(List<FlowEntity> flowEntries, Uint64 dpId, int lportTag,
998 LOG.debug("Programming Drop Rules: DpId={}, lportTag={}, addOrRemove={}", dpId, lportTag, addOrRemove);
999 programConntrackInvalidDropRule(flowEntries, dpId, lportTag, addOrRemove);
1000 programAclRuleMissDropRule(flowEntries, dpId, lportTag, addOrRemove);
1004 * Adds the rule to drop the conntrack invalid packets .
1006 * @param flowEntries the flow entries
1007 * @param dpId the dpId
1008 * @param lportTag the lport tag
1009 * @param addOrRemove whether to add or remove the flow
1011 protected void programConntrackInvalidDropRule(List<FlowEntity> flowEntries, Uint64 dpId, int lportTag,
1013 List<MatchInfoBase> matches = AclServiceOFFlowBuilder.addLPortTagMatches(lportTag,
1014 AclConstants.TRACKED_INV_CT_STATE, AclConstants.TRACKED_INV_CT_STATE_MASK, serviceMode);
1015 List<InstructionInfo> instructions = AclServiceOFFlowBuilder.getDropInstructionInfo();
1017 String flowId = this.directionString + "_Fixed_Conntrk_Drop" + dpId.toString()
1018 + "_" + lportTag + "_Tracked_Invalid";
1019 addFlowEntryToList(flowEntries, dpId, getAclFilterCumDispatcherTable(), flowId,
1020 AclConstants.CT_STATE_TRACKED_INVALID_PRIORITY, 0, 0, AclServiceUtils.getDropFlowCookie(lportTag),
1021 matches, instructions, addOrRemove);
1025 * Program ACL rule miss drop rule for a port.
1027 * @param flowEntries the flow entries
1028 * @param dpId the dp id
1029 * @param lportTag the lport tag
1030 * @param addOrRemove the add or remove
1032 protected void programAclRuleMissDropRule(List<FlowEntity> flowEntries, Uint64 dpId, int lportTag,
1034 List<MatchInfoBase> matches = new ArrayList<>();
1035 matches.add(AclServiceUtils.buildLPortTagMatch(lportTag, serviceMode));
1036 List<InstructionInfo> instructions = AclServiceOFFlowBuilder.getDropInstructionInfo();
1038 String flowId = this.directionString + "_Fixed_Acl_Rule_Miss_Drop_" + dpId.toString() + "_" + lportTag;
1039 addFlowEntryToList(flowEntries, dpId, getAclFilterCumDispatcherTable(), flowId,
1040 AclConstants.ACL_PORT_SPECIFIC_DROP_PRIORITY, 0, 0, AclServiceUtils.getDropFlowCookie(lportTag),
1041 matches, instructions, addOrRemove);
1045 * Program acl commit rules.
1047 * @param flowEntries the flow entries
1048 * @param dpId the dp id
1049 * @param lportTag the lport tag
1050 * @param portId the port id
1051 * @param addOrRemove the add or remove
1053 protected void programAclCommitRules(List<FlowEntity> flowEntries, Uint64 dpId, int lportTag, String portId,
1055 programAclCommitRuleForConntrack(flowEntries, dpId, lportTag, portId, MatchEthernetType.IPV4, addOrRemove);
1056 programAclCommitRuleForConntrack(flowEntries, dpId, lportTag, portId, MatchEthernetType.IPV6, addOrRemove);
1057 programAclCommitRuleForNonConntrack(flowEntries, dpId, lportTag, addOrRemove);
1061 * Program acl commit rule for conntrack.
1063 * @param flowEntries the flow entries
1064 * @param dpId the dp id
1065 * @param lportTag the lport tag
1066 * @param portId the port id
1067 * @param matchEtherType the match ether type
1068 * @param addOrRemove the add or remove
1070 protected void programAclCommitRuleForConntrack(List<FlowEntity> flowEntries, Uint64 dpId, int lportTag,
1071 String portId, MatchEthernetType matchEtherType, int addOrRemove) {
1072 List<MatchInfoBase> matches = new ArrayList<>();
1073 matches.add(matchEtherType);
1074 matches.addAll(AclServiceUtils.buildMatchesForLPortTagAndConntrackClassifierType(lportTag,
1075 AclConntrackClassifierType.CONNTRACK_SUPPORTED, serviceMode));
1077 List<ActionInfo> actionsInfos = new ArrayList<>();
1078 if (addOrRemove == NwConstants.ADD_FLOW) {
1079 Long elanId = getElanIdFromAclInterface(portId);
1080 if (elanId == null) {
1081 LOG.error("ElanId not found for portId={}; Context: dpId={}, lportTag={}, addOrRemove={}", portId, dpId,
1082 lportTag, addOrRemove);
1085 List<NxCtAction> ctActionsList =
1086 Lists.newArrayList(new ActionNxConntrack.NxCtMark(AclConstants.CT_MARK_EST_STATE));
1087 actionsInfos.add(new ActionNxConntrack(2, 1, 0, elanId.intValue(), (short) 255, ctActionsList));
1088 actionsInfos.add(new ActionNxCtClear());
1090 List<InstructionInfo> instructions = getDispatcherTableResubmitInstructions(actionsInfos);
1092 String flowName = directionString + "_Acl_Commit_Conntrack_" + dpId.toString()
1093 + "_" + lportTag + "_" + matchEtherType;
1094 // Flow for conntrack traffic to commit and resubmit to dispatcher
1095 addFlowEntryToList(flowEntries, dpId, getAclCommitterTable(), flowName, AclConstants.ACL_DEFAULT_PRIORITY,
1096 0, 0, AclConstants.COOKIE_ACL_BASE, matches, instructions, addOrRemove);
1100 * Program acl commit rule for non conntrack.
1102 * @param flowEntries the flow entries
1103 * @param dpId the dp id
1104 * @param lportTag the lport tag
1105 * @param addOrRemove the add or remove
1107 protected void programAclCommitRuleForNonConntrack(List<FlowEntity> flowEntries, Uint64 dpId, int lportTag,
1109 List<MatchInfoBase> matches = new ArrayList<>();
1110 matches.addAll(AclServiceUtils.buildMatchesForLPortTagAndConntrackClassifierType(lportTag,
1111 AclConntrackClassifierType.NON_CONNTRACK_SUPPORTED, serviceMode));
1113 List<InstructionInfo> instructions = getDispatcherTableResubmitInstructions();
1114 String flowName = this.directionString + "_Acl_Commit_Non_Conntrack_" + dpId + "_" + lportTag;
1115 // Flow for non-conntrack traffic to resubmit to dispatcher
1116 addFlowEntryToList(flowEntries, dpId, getAclCommitterTable(), flowName, AclConstants.ACL_DEFAULT_PRIORITY,
1117 0, 0, AclConstants.COOKIE_ACL_BASE, matches, instructions, addOrRemove);
1121 protected Long getElanIdFromAclInterface(String elanInterfaceName) {
1122 AclInterface aclInterface = aclInterfaceCache.get(elanInterfaceName);
1123 if (null != aclInterface) {
1124 return aclInterface.getElanId();
1129 protected abstract boolean isValidDirection(Class<? extends DirectionBase> direction);
1131 protected abstract short getAclConntrackSenderTable();
1133 protected abstract short getAclForExistingTrafficTable();
1135 protected abstract short getAclFilterCumDispatcherTable();
1137 protected abstract short getAclRuleBasedFilterTable();
1139 protected abstract short getAclRemoteAclTable();
1141 protected abstract short getAclCommitterTable();