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.InstructionInfo;
18 import org.opendaylight.genius.mdsalutil.MatchFieldType;
19 import org.opendaylight.genius.mdsalutil.MatchInfo;
20 import org.opendaylight.genius.mdsalutil.MatchInfoBase;
21 import org.opendaylight.genius.mdsalutil.NwConstants;
22 import org.opendaylight.genius.mdsalutil.NxMatchFieldType;
23 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
24 import org.opendaylight.netvirt.aclservice.api.AclServiceManager.Action;
25 import org.opendaylight.netvirt.aclservice.utils.AclConstants;
26 import org.opendaylight.netvirt.aclservice.utils.AclDataUtil;
27 import org.opendaylight.netvirt.aclservice.utils.AclServiceOFFlowBuilder;
28 import org.opendaylight.netvirt.aclservice.utils.AclServiceUtils;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.acl.access.list.entries.Ace;
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.Matches;
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.matches.AceType;
32 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;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.DirectionEgress;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.SecurityRuleAttr;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.interfaces._interface.AllowedAddressPairs;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
40 * Provides the stateless implementation for egress (w.r.t VM) ACL service.
43 * Note: Table names used are w.r.t switch. Hence, switch ingress is VM egress
46 public class StatelessEgressAclServiceImpl extends AbstractEgressAclServiceImpl {
48 private static final Logger LOG = LoggerFactory.getLogger(StatelessEgressAclServiceImpl.class);
50 public StatelessEgressAclServiceImpl(DataBroker dataBroker, IMdsalApiManager mdsalManager, AclDataUtil aclDataUtil,
51 AclServiceUtils aclServiceUtils) {
52 super(dataBroker, mdsalManager, aclDataUtil, aclServiceUtils);
56 protected void programSpecificFixedRules(BigInteger dpid, String dhcpMacAddress,
57 List<AllowedAddressPairs> allowedAddresses, int lportTag, String portId, Action action, int addOrRemove) {
61 protected String syncSpecificAclFlow(BigInteger dpId, int lportTag, int addOrRemove, int priority, Ace ace,
62 String portId, Map<String, List<MatchInfoBase>> flowMap, String flowName) {
63 // Not in use here. programAceRule function is overridden.
68 protected void programAceRule(BigInteger dpId, int lportTag, int addOrRemove, String aclName, Ace ace,
69 String portId, List<AllowedAddressPairs> syncAllowedAddresses) {
70 SecurityRuleAttr aceAttr = AclServiceUtils.getAccesssListAttributes(ace);
71 if (!aceAttr.getDirection().equals(DirectionEgress.class)) {
74 Matches matches = ace.getMatches();
75 AceType aceType = matches.getAceType();
76 Map<String, List<MatchInfoBase>> flowMap = null;
77 Short protocol = null;
79 if (aceType instanceof AceIp) {
80 protocol = ((AceIp)aceType).getProtocol();
81 flowMap = AclServiceOFFlowBuilder.programIpFlow(matches);
83 if (null == flowMap) {
84 LOG.error("Failed to apply ACL {}", ace.getKey());
87 for (Map.Entry<String, List<MatchInfoBase>> flow : flowMap.entrySet()) {
88 String flowName = flow.getKey();
89 List<MatchInfoBase> flowMatches = flow.getValue();
90 boolean hasTcpMatch = AclServiceUtils.containsMatchFieldType(flowMatches,
91 NxMatchFieldType.nx_tcp_dst_with_mask) || AclServiceUtils.containsMatchFieldType(flowMatches,
92 NxMatchFieldType.nx_tcp_src_with_mask);
93 if (hasTcpMatch || protocol == null) {
94 flowName += "Egress" + lportTag + ace.getKey().getRuleName();
95 flowMatches.add(AclServiceUtils.buildLPortTagMatch(lportTag));
97 programAllowSynRules(dpId, flowName, flowMatches, addOrRemove, protocol);
102 private void programAllowSynRules(BigInteger dpId, String origFlowName,
103 List<MatchInfoBase> origFlowMatches, int addFlow, Short protocol) {
104 List<MatchInfoBase> flowMatches = new ArrayList<>();
105 flowMatches.addAll(origFlowMatches);
106 if (new Short((short) NwConstants.IP_PROT_TCP).equals(protocol)) {
107 flowMatches.add(new MatchInfo(MatchFieldType.tcp_flags, new long[] { AclConstants.TCP_FLAG_SYN }));
110 List<ActionInfo> actionsInfos = new ArrayList<>();
111 List<InstructionInfo> instructions = getDispatcherTableResubmitInstructions(actionsInfos);
113 String flowName = "SYN_" + origFlowName;
114 syncFlow(dpId, NwConstants.INGRESS_ACL_TABLE, flowName, AclConstants.PROTO_MATCH_SYN_ALLOW_PRIORITY,
115 "ACL_SYN_", 0, 0, AclConstants.COOKIE_ACL_BASE, flowMatches, instructions, addFlow);
116 String oper = getOperAsString(addFlow);
117 LOG.debug("{} allow syn packet flow {}", oper, flowName);