/*
* Copyright (c) 2016 Red Hat, Inc. and others. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*/
package org.opendaylight.netvirt.aclservice;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.opendaylight.controller.md.sal.binding.api.DataBroker;
import org.opendaylight.genius.mdsalutil.ActionInfo;
import org.opendaylight.genius.mdsalutil.InstructionInfo;
import org.opendaylight.genius.mdsalutil.MatchInfoBase;
import org.opendaylight.genius.mdsalutil.NwConstants;
import org.opendaylight.genius.mdsalutil.NxMatchFieldType;
import org.opendaylight.genius.mdsalutil.NxMatchInfo;
import org.opendaylight.genius.mdsalutil.actions.ActionNxConntrack;
import org.opendaylight.genius.mdsalutil.instructions.InstructionApplyActions;
import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
import org.opendaylight.genius.mdsalutil.matches.MatchEthernetDestination;
import org.opendaylight.genius.mdsalutil.matches.MatchEthernetType;
import org.opendaylight.netvirt.aclservice.api.AclServiceManager.Action;
import org.opendaylight.netvirt.aclservice.api.AclServiceManager.MatchCriteria;
import org.opendaylight.netvirt.aclservice.utils.AclConstants;
import org.opendaylight.netvirt.aclservice.utils.AclDataUtil;
import org.opendaylight.netvirt.aclservice.utils.AclServiceOFFlowBuilder;
import org.opendaylight.netvirt.aclservice.utils.AclServiceUtils;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.acl.access.list.entries.Ace;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.IpPrefixOrAddress;
import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.interfaces._interface.AllowedAddressPairs;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Provides the stateful implementation for ingress (w.r.t VM) ACL service.
*
*
* Note: Table names used are w.r.t switch. Hence, switch ingress is VM egress
* and vice versa.
*/
public class StatefulIngressAclServiceImpl extends AbstractIngressAclServiceImpl {
private static final Logger LOG = LoggerFactory.getLogger(StatefulIngressAclServiceImpl.class);
/**
* Initialize the member variables.
*
* @param dataBroker the data broker instance.
* @param mdsalManager the mdsal manager.
* @param aclDataUtil
* the acl data util.
* @param aclServiceUtils
* the acl service util.
*/
public StatefulIngressAclServiceImpl(DataBroker dataBroker, IMdsalApiManager mdsalManager, AclDataUtil aclDataUtil,
AclServiceUtils aclServiceUtils) {
// Service mode is w.rt. switch
super(dataBroker, mdsalManager, aclDataUtil, aclServiceUtils);
}
/**
* Program conntrack rules.
*
* @param dpid the dpid
* @param dhcpMacAddress the dhcp mac address.
* @param allowedAddresses the allowed addresses
* @param lportTag the lport tag
* @param addOrRemove add or remove the flow
*/
@Override
protected void programSpecificFixedRules(BigInteger dpid, String dhcpMacAddress,
List allowedAddresses, int lportTag, String portId, Action action, int addOrRemove) {
programIngressAclFixedConntrackRule(dpid, lportTag, allowedAddresses, portId, action, addOrRemove);
}
@Override
protected String syncSpecificAclFlow(BigInteger dpId, int lportTag, int addOrRemove, Ace ace, String portId,
Map> flowMap, String flowName) {
List matches = flowMap.get(flowName);
flowName += "Ingress" + lportTag + ace.getKey().getRuleName();
matches.add(AclServiceUtils.buildLPortTagMatch(lportTag));
matches.add(new NxMatchInfo(NxMatchFieldType.ct_state,
new long[] {AclConstants.TRACKED_NEW_CT_STATE, AclConstants.TRACKED_NEW_CT_STATE_MASK}));
Long elanTag = AclServiceUtils.getElanIdFromInterface(portId, dataBroker);
List actionsInfos = new ArrayList<>();
actionsInfos.add(new ActionNxConntrack(2, 1, 0, elanTag.intValue(), (short) 255));
List instructions = getDispatcherTableResubmitInstructions(actionsInfos);
// For flows related remote ACL, unique flow priority is used for
// each flow to avoid overlapping flows
int priority = getIngressSpecificAclFlowPriority(dpId, addOrRemove, flowName);
syncFlow(dpId, NwConstants.EGRESS_ACL_FILTER_TABLE, flowName, priority, "ACL", 0, 0,
AclConstants.COOKIE_ACL_BASE, matches, instructions, addOrRemove);
return flowName;
}
private int getIngressSpecificAclFlowPriority(BigInteger dpId, int addOrRemove, String flowName) {
int priority;
if (addOrRemove == NwConstants.DEL_FLOW) {
priority = aclServiceUtils.releaseAndRemoveFlowPriorityFromCache(dpId, NwConstants.EGRESS_ACL_FILTER_TABLE,
flowName);
} else {
priority = aclServiceUtils.allocateAndSaveFlowPriorityInCache(dpId, NwConstants.EGRESS_ACL_FILTER_TABLE,
flowName);
}
return priority;
}
/**
* Adds the rule to send the packet to the netfilter to check whether it is
* a known packet.
*
* @param dpId the dpId
* @param allowedAddresses the allowed addresses
* @param priority the priority of the flow
* @param flowId the flowId
* @param conntrackState the conntrack state of the packets thats should be
* send
* @param conntrackMask the conntrack mask
* @param portId the portId
* @param addOrRemove whether to add or remove the flow
*/
private void programConntrackRecircRules(BigInteger dpId, List allowedAddresses,
Integer priority, String flowId, String portId, int addOrRemove) {
for (AllowedAddressPairs allowedAddress : allowedAddresses) {
IpPrefixOrAddress attachIp = allowedAddress.getIpAddress();
MacAddress attachMac = allowedAddress.getMacAddress();
List matches = new ArrayList<>();
matches.add(MatchEthernetType.IPV4);
matches.add(new MatchEthernetDestination(attachMac));
matches.addAll(AclServiceUtils.buildIpMatches(attachIp, MatchCriteria.MATCH_DESTINATION));
List instructions = new ArrayList<>();
List actionsInfos = new ArrayList<>();
Long elanTag = AclServiceUtils.getElanIdFromInterface(portId, dataBroker);
actionsInfos.add(new ActionNxConntrack(2, 0, 0, elanTag.intValue(),
NwConstants.EGRESS_ACL_REMOTE_ACL_TABLE));
instructions.add(new InstructionApplyActions(actionsInfos));
String flowName = "Ingress_Fixed_Conntrk_" + dpId + "_" + attachMac.getValue() + "_"
+ String.valueOf(attachIp.getValue()) + "_" + flowId;
syncFlow(dpId, NwConstants.EGRESS_ACL_TABLE, flowName, AclConstants.PROTO_MATCH_PRIORITY, "ACL", 0, 0,
AclConstants.COOKIE_ACL_BASE, matches, instructions, addOrRemove);
}
}
/**
* Programs the default connection tracking rules.
*
* @param dpid the dp id
* @param lportTag the lport tag
* @param allowedAddresses the allowed addresses
* @param portId the portId
* @param write whether to add or remove the flow.
*/
private void programIngressAclFixedConntrackRule(BigInteger dpid, int lportTag,
List allowedAddresses, String portId, Action action, int write) {
programConntrackRecircRules(dpid, allowedAddresses, AclConstants.CT_STATE_UNTRACKED_PRIORITY,
"Recirc",portId, write);
programIngressConntrackDropRules(dpid, lportTag, write);
LOG.info("programIngressAclFixedConntrackRule : default connection tracking rule are added.");
}
/**
* Adds the rule to drop the unknown/invalid packets .
*
* @param dpId the dpId
* @param lportTag the lport tag
* @param priority the priority of the flow
* @param flowId the flowId
* @param conntrackState the conntrack state of the packets thats should be
* send
* @param conntrackMask the conntrack mask
* @param tableId table id
* @param addOrRemove whether to add or remove the flow
*/
private void programConntrackDropRule(BigInteger dpId, int lportTag, Integer priority, String flowId,
int conntrackState, int conntrackMask, int addOrRemove) {
List matches = AclServiceOFFlowBuilder.addLPortTagMatches(lportTag, conntrackState,
conntrackMask);
List instructions = AclServiceOFFlowBuilder.getDropInstructionInfo();
flowId = "Ingress_Fixed_Conntrk_Drop" + dpId + "_" + lportTag + "_" + flowId;
syncFlow(dpId, NwConstants.INGRESS_ACL_FILTER_TABLE, flowId, priority, "ACL", 0, 0,
AclConstants.COOKIE_ACL_DROP_FLOW, matches, instructions, addOrRemove);
}
/**
* Adds the rules to drop the unknown/invalid packets .
*
* @param dpId the dpId
* @param lportTag the lport tag
* @param addOrRemove whether to add or remove the flow
*/
private void programIngressConntrackDropRules(BigInteger dpId, int lportTag, int addOrRemove) {
programConntrackDropRule(dpId, lportTag, AclConstants.CT_STATE_TRACKED_NEW_DROP_PRIORITY, "Tracked_New",
AclConstants.TRACKED_NEW_CT_STATE, AclConstants.TRACKED_NEW_CT_STATE_MASK, addOrRemove);
programConntrackDropRule(dpId, lportTag, AclConstants.CT_STATE_TRACKED_INVALID_PRIORITY, "Tracked_Invalid",
AclConstants.TRACKED_INV_CT_STATE, AclConstants.TRACKED_INV_CT_STATE_MASK, addOrRemove);
}
}