2 * Copyright (c) 2016 HPE, 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 com.google.common.util.concurrent.ListenableFuture;
11 import java.math.BigInteger;
12 import java.util.ArrayList;
13 import java.util.Collections;
14 import java.util.List;
17 import java.util.stream.Collectors;
18 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
19 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.genius.datastoreutils.DataStoreJobCoordinator;
22 import org.opendaylight.genius.mdsalutil.ActionInfo;
23 import org.opendaylight.genius.mdsalutil.InstructionInfo;
24 import org.opendaylight.genius.mdsalutil.MDSALUtil;
25 import org.opendaylight.genius.mdsalutil.MatchInfoBase;
26 import org.opendaylight.genius.mdsalutil.MetaDataUtil;
27 import org.opendaylight.genius.mdsalutil.NwConstants;
28 import org.opendaylight.genius.mdsalutil.instructions.InstructionGotoTable;
29 import org.opendaylight.genius.mdsalutil.instructions.InstructionWriteMetadata;
30 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
31 import org.opendaylight.genius.mdsalutil.matches.MatchArpSha;
32 import org.opendaylight.genius.mdsalutil.matches.MatchEthernetType;
33 import org.opendaylight.genius.utils.ServiceIndex;
34 import org.opendaylight.netvirt.aclservice.api.AclServiceManager.Action;
35 import org.opendaylight.netvirt.aclservice.api.utils.AclInterface;
36 import org.opendaylight.netvirt.aclservice.utils.AclConstants;
37 import org.opendaylight.netvirt.aclservice.utils.AclDataUtil;
38 import org.opendaylight.netvirt.aclservice.utils.AclServiceOFFlowBuilder;
39 import org.opendaylight.netvirt.aclservice.utils.AclServiceUtils;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.Acl;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.acl.AccessListEntries;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.acl.access.list.entries.Ace;
43 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;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.acl.access.list.entries.ace.actions.PacketHandling;
45 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;
46 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;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
48 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.ServiceModeEgress;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.ServiceModeIngress;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.services.info.BoundServices;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.DirectionEgress;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.SecurityRuleAttr;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.interfaces._interface.AllowedAddressPairs;
56 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
57 import org.slf4j.Logger;
58 import org.slf4j.LoggerFactory;
61 * Provides abstract implementation for egress (w.r.t VM) ACL service.
64 * Note: Table names used are w.r.t switch. Hence, switch ingress is VM egress
67 public abstract class AbstractEgressAclServiceImpl extends AbstractAclServiceImpl {
69 private static final Logger LOG = LoggerFactory.getLogger(AbstractEgressAclServiceImpl.class);
72 * Initialize the member variables.
74 * @param dataBroker the data broker instance.
75 * @param mdsalManager the mdsal manager instance.
78 * @param aclServiceUtils
79 * the acl service util.
81 public AbstractEgressAclServiceImpl(DataBroker dataBroker, IMdsalApiManager mdsalManager, AclDataUtil aclDataUtil,
82 AclServiceUtils aclServiceUtils) {
83 // Service mode is w.rt. switch
84 super(ServiceModeIngress.class, dataBroker, mdsalManager, aclDataUtil, aclServiceUtils);
90 * @param aclInterface the acl interface
93 public void bindService(AclInterface aclInterface) {
94 String interfaceName = aclInterface.getInterfaceId();
95 DataStoreJobCoordinator dataStoreCoordinator = DataStoreJobCoordinator.getInstance();
96 dataStoreCoordinator.enqueueJob(interfaceName,
98 int instructionKey = 0;
99 List<Instruction> instructions = new ArrayList<>();
100 Long vpnId = aclInterface.getVpnId();
102 instructions.add(MDSALUtil.buildAndGetWriteMetadaInstruction(MetaDataUtil.getVpnIdMetadata(vpnId),
103 MetaDataUtil.METADATA_MASK_VRFID, ++instructionKey));
105 Long elanTag = aclInterface.getElanId();
107 MDSALUtil.buildAndGetWriteMetadaInstruction(MetaDataUtil.getElanTagMetadata(elanTag),
108 MetaDataUtil.METADATA_MASK_SERVICE, ++instructionKey));
111 MDSALUtil.buildAndGetGotoTableInstruction(NwConstants.INGRESS_ACL_TABLE, ++instructionKey));
112 short serviceIndex = ServiceIndex.getIndex(NwConstants.ACL_SERVICE_NAME,
113 NwConstants.ACL_SERVICE_INDEX);
114 int flowPriority = AclConstants.EGRESS_ACL_DEFAULT_FLOW_PRIORITY;
115 BoundServices serviceInfo = AclServiceUtils.getBoundServices(
116 String.format("%s.%s.%s", "acl", "egressacl", interfaceName), serviceIndex, flowPriority,
117 AclConstants.COOKIE_ACL_BASE, instructions);
118 InstanceIdentifier<BoundServices> path =
119 AclServiceUtils.buildServiceId(interfaceName, serviceIndex, ServiceModeIngress.class);
121 WriteTransaction writeTxn = dataBroker.newWriteOnlyTransaction();
122 writeTxn.put(LogicalDatastoreType.CONFIGURATION, path, serviceInfo, true);
124 List<ListenableFuture<Void>> futures = new ArrayList<>();
125 futures.add(writeTxn.submit());
133 * @param aclInterface the acl interface
136 protected void unbindService(AclInterface aclInterface) {
137 String interfaceName = aclInterface.getInterfaceId();
138 InstanceIdentifier<BoundServices> path =
139 AclServiceUtils.buildServiceId(interfaceName,
140 ServiceIndex.getIndex(NwConstants.ACL_SERVICE_NAME, NwConstants.ACL_SERVICE_INDEX),
141 ServiceModeIngress.class);
143 DataStoreJobCoordinator dataStoreCoordinator = DataStoreJobCoordinator.getInstance();
144 dataStoreCoordinator.enqueueJob(interfaceName,
146 WriteTransaction writeTxn = dataBroker.newWriteOnlyTransaction();
147 writeTxn.delete(LogicalDatastoreType.CONFIGURATION, path);
149 List<ListenableFuture<Void>> futures = new ArrayList<>();
150 futures.add(writeTxn.submit());
156 protected void programGeneralFixedRules(BigInteger dpid, String dhcpMacAddress,
157 List<AllowedAddressPairs> allowedAddresses, int lportTag, Action action, int addOrRemove) {
158 LOG.info("programFixedRules : adding default rules.");
160 if (action == Action.ADD || action == Action.REMOVE) {
161 egressAclDhcpAllowClientTraffic(dpid, dhcpMacAddress, lportTag, addOrRemove);
162 egressAclDhcpv6AllowClientTraffic(dpid, dhcpMacAddress, lportTag, addOrRemove);
163 egressAclDhcpDropServerTraffic(dpid, dhcpMacAddress, lportTag, addOrRemove);
164 egressAclDhcpv6DropServerTraffic(dpid, dhcpMacAddress, lportTag, addOrRemove);
165 egressAclIcmpv6DropRouterAdvts(dpid, lportTag, addOrRemove);
166 egressAclIcmpv6AllowedList(dpid, lportTag, addOrRemove);
168 programArpRule(dpid, allowedAddresses, lportTag, addOrRemove);
173 protected void updateArpForAllowedAddressPairs(BigInteger dpId, int lportTag, List<AllowedAddressPairs> deletedAAP,
174 List<AllowedAddressPairs> addedAAP) {
175 Set<MacAddress> deletedAAPmacs =
176 deletedAAP.stream().map(aap -> aap.getMacAddress()).collect(Collectors.toSet());
177 Set<MacAddress> addedAAPmacs = addedAAP.stream().map(aap -> aap.getMacAddress()).collect(Collectors.toSet());
179 // Remove common macs to avoid delete and add of ARP flows having same MAC.
180 deletedAAPmacs.removeAll(addedAAPmacs);
182 programArpRule(dpId, deletedAAPmacs, lportTag, NwConstants.DEL_FLOW);
183 programArpRule(dpId, addedAAPmacs, lportTag, NwConstants.ADD_FLOW);
187 protected boolean programAclRules(AclInterface port, List<Uuid> aclUuidList, int addOrRemove) {
188 BigInteger dpId = port.getDpId();
189 LOG.trace("Applying custom rules DpId {}, lportTag {}", dpId, port.getLPortTag());
190 if (aclUuidList == null || dpId == null) {
191 LOG.warn("one of the egress acl parameters can not be null. sg {}, dpId {}",
195 for (Uuid sgUuid :aclUuidList) {
196 Acl acl = AclServiceUtils.getAcl(dataBroker, sgUuid.getValue());
198 LOG.warn("The ACL is empty");
201 AccessListEntries accessListEntries = acl.getAccessListEntries();
202 List<Ace> aceList = accessListEntries.getAce();
203 for (Ace ace: aceList) {
204 programAceRule(port, addOrRemove, acl.getAclName(), ace, null);
211 protected void programAceRule(AclInterface port, int addOrRemove, String aclName, Ace ace,
212 List<AllowedAddressPairs> syncAllowedAddresses) {
213 SecurityRuleAttr aceAttr = AclServiceUtils.getAccesssListAttributes(ace);
214 if (!aceAttr.getDirection().equals(DirectionEgress.class)) {
217 Matches matches = ace.getMatches();
218 AceType aceType = matches.getAceType();
219 Map<String,List<MatchInfoBase>> flowMap = null;
220 if (aceType instanceof AceIp) {
221 flowMap = AclServiceOFFlowBuilder.programIpFlow(matches);
222 if (syncAllowedAddresses != null) {
223 flowMap = AclServiceUtils.getFlowForAllowedAddresses(syncAllowedAddresses, flowMap, false);
224 } else if (aceAttr.getRemoteGroupId() != null) {
225 flowMap = aclServiceUtils.getFlowForRemoteAcl(port, aceAttr.getRemoteGroupId(), port.getInterfaceId(),
229 int lportTag = port.getLPortTag();
230 if (null == flowMap) {
231 LOG.error("Failed to apply ACL {} lportTag {}", ace.getKey(), lportTag);
234 for (String flowName : flowMap.keySet()) {
235 syncSpecificAclFlow(port.getDpId(), lportTag, addOrRemove, ace, port.getInterfaceId(), flowMap, flowName);
240 protected void updateRemoteAclTableForPort(AclInterface port, Uuid acl, int addOrRemove,
241 AllowedAddressPairs ip, BigInteger aclId, BigInteger dpId) {
242 Long elanTag = port.getElanId();
243 Long vpnId = port.getVpnId();
244 List<MatchInfoBase> flowMatches = new ArrayList<>();
245 flowMatches.addAll(AclServiceUtils.buildIpAndDstServiceMatch(elanTag, ip, dataBroker, vpnId));
247 List<InstructionInfo> instructions = new ArrayList<>();
249 InstructionWriteMetadata writeMetatdata =
250 new InstructionWriteMetadata(AclServiceUtils.getAclIdMetadata(aclId),
251 MetaDataUtil.METADATA_MASK_REMOTE_ACL_ID);
252 instructions.add(writeMetatdata);
253 instructions.add(new InstructionGotoTable(getEgressAclFilterTable()));
255 Long serviceTag = vpnId != null ? vpnId : elanTag;
256 String flowNameAdded = "Acl_Filter_Egress_" + new String(ip.getIpAddress().getValue()) + "_" + serviceTag;
258 syncFlow(dpId, getEgressAclRemoteAclTable(), flowNameAdded, AclConstants.NO_PRIORITY, "ACL", 0, 0,
259 AclConstants.COOKIE_ACL_BASE, flowMatches, instructions, addOrRemove);
262 protected short getEgressAclFilterTable() {
263 return NwConstants.INGRESS_ACL_FILTER_TABLE;
266 protected short getEgressAclRemoteAclTable() {
267 return NwConstants.INGRESS_ACL_REMOTE_ACL_TABLE;
270 protected abstract String syncSpecificAclFlow(BigInteger dpId, int lportTag, int addOrRemove, Ace ace,
271 String portId, Map<String, List<MatchInfoBase>> flowMap, String flowName);
274 * Anti-spoofing rule to block the Ipv4 DHCP server traffic from the port.
276 * @param dpId the dpId
277 * @param dhcpMacAddress the Dhcp mac address
278 * @param lportTag the lport tag
279 * @param addOrRemove add/remove the flow.
281 protected void egressAclDhcpDropServerTraffic(BigInteger dpId, String dhcpMacAddress, int lportTag,
283 List<MatchInfoBase> matches = AclServiceUtils.buildDhcpMatches(AclConstants.DHCP_SERVER_PORT_IPV4,
284 AclConstants.DHCP_CLIENT_PORT_IPV4, lportTag, ServiceModeEgress.class);
286 String flowName = "Egress_DHCP_Server_v4" + dpId + "_" + lportTag + "_" + dhcpMacAddress + "_Drop_";
287 syncFlow(dpId, NwConstants.INGRESS_ACL_TABLE, flowName,
288 AclConstants.PROTO_DHCP_CLIENT_TRAFFIC_MATCH_PRIORITY, "ACL", 0,
289 0, AclConstants.COOKIE_ACL_BASE, matches, Collections.emptyList(), addOrRemove);
293 * Anti-spoofing rule to block the Ipv6 DHCP server traffic from the port.
295 * @param dpId the dpId
296 * @param dhcpMacAddress the Dhcp mac address
297 * @param lportTag the lport tag
298 * @param addOrRemove add/remove the flow.
300 protected void egressAclDhcpv6DropServerTraffic(BigInteger dpId, String dhcpMacAddress, int lportTag,
302 List<MatchInfoBase> matches = AclServiceUtils.buildDhcpV6Matches(AclConstants.DHCP_SERVER_PORT_IPV6,
303 AclConstants.DHCP_CLIENT_PORT_IPV6, lportTag, ServiceModeEgress.class);
305 String flowName = "Egress_DHCP_Server_v6" + "_" + dpId + "_" + lportTag + "_" + dhcpMacAddress + "_Drop_";
306 syncFlow(dpId, NwConstants.INGRESS_ACL_TABLE, flowName,
307 AclConstants.PROTO_DHCP_CLIENT_TRAFFIC_MATCH_PRIORITY, "ACL", 0,
308 0, AclConstants.COOKIE_ACL_BASE, matches, Collections.emptyList(), addOrRemove);
312 * Anti-spoofing rule to block the Ipv6 Router Advts from the VM port.
314 * @param dpId the dpId
315 * @param lportTag the lport tag
316 * @param addOrRemove add/remove the flow.
318 private void egressAclIcmpv6DropRouterAdvts(BigInteger dpId, int lportTag, int addOrRemove) {
319 List<MatchInfoBase> matches = AclServiceUtils.buildIcmpV6Matches(AclConstants.ICMPV6_TYPE_RA, 0, lportTag,
320 ServiceModeEgress.class);
322 String flowName = "Egress_ICMPv6" + "_" + dpId + "_" + lportTag + "_" + AclConstants.ICMPV6_TYPE_RA + "_Drop_";
323 syncFlow(dpId, NwConstants.INGRESS_ACL_TABLE, flowName, AclConstants.PROTO_IPV6_DROP_PRIORITY, "ACL", 0,
324 0, AclConstants.COOKIE_ACL_BASE, matches, Collections.emptyList(), addOrRemove);
328 * Add rule to allow certain ICMPv6 traffic from VM ports.
330 * @param dpId the dpId
331 * @param lportTag the lport tag
332 * @param addOrRemove add/remove the flow.
334 private void egressAclIcmpv6AllowedList(BigInteger dpId, int lportTag, int addOrRemove) {
335 List<ActionInfo> actionsInfos = new ArrayList<>();
336 List<InstructionInfo> instructions = getDispatcherTableResubmitInstructions(actionsInfos);
338 for (Integer icmpv6Type: AclConstants.allowedIcmpv6NdList()) {
339 List<MatchInfoBase> matches = AclServiceUtils.buildIcmpV6Matches(icmpv6Type, 0, lportTag,
340 ServiceModeEgress.class);
341 String flowName = "Egress_ICMPv6" + "_" + dpId + "_" + lportTag + "_" + icmpv6Type + "_Permit_";
342 syncFlow(dpId, NwConstants.INGRESS_ACL_TABLE, flowName, AclConstants.PROTO_IPV6_ALLOWED_PRIORITY,
343 "ACL", 0, 0, AclConstants.COOKIE_ACL_BASE, matches, instructions, addOrRemove);
348 * Add rule to ensure only DHCP server traffic from the specified mac is
351 * @param dpId the dpid
352 * @param dhcpMacAddress the DHCP server mac address
353 * @param lportTag the lport tag
354 * @param addOrRemove whether to add or remove the flow
356 private void egressAclDhcpAllowClientTraffic(BigInteger dpId, String dhcpMacAddress, int lportTag,
358 final List<MatchInfoBase> matches = AclServiceUtils.buildDhcpMatches(AclConstants.DHCP_CLIENT_PORT_IPV4,
359 AclConstants.DHCP_SERVER_PORT_IPV4, lportTag, ServiceModeEgress.class);
361 List<ActionInfo> actionsInfos = new ArrayList<>();
362 List<InstructionInfo> instructions = getDispatcherTableResubmitInstructions(actionsInfos);
364 String flowName = "Egress_DHCP_Client_v4" + dpId + "_" + lportTag + "_" + dhcpMacAddress + "_Permit_";
365 syncFlow(dpId, NwConstants.INGRESS_ACL_TABLE, flowName, AclConstants.PROTO_DHCP_CLIENT_TRAFFIC_MATCH_PRIORITY,
366 "ACL", 0, 0, AclConstants.COOKIE_ACL_BASE, matches, instructions, addOrRemove);
370 * Add rule to ensure only DHCPv6 server traffic from the specified mac is
373 * @param dpId the dpid
374 * @param dhcpMacAddress the DHCP server mac address
375 * @param lportTag the lport tag
376 * @param addOrRemove whether to add or remove the flow
378 private void egressAclDhcpv6AllowClientTraffic(BigInteger dpId, String dhcpMacAddress, int lportTag,
380 final List<MatchInfoBase> matches = AclServiceUtils.buildDhcpV6Matches(AclConstants.DHCP_CLIENT_PORT_IPV6,
381 AclConstants.DHCP_SERVER_PORT_IPV6, lportTag, ServiceModeEgress.class);
383 List<ActionInfo> actionsInfos = new ArrayList<>();
384 List<InstructionInfo> instructions = getDispatcherTableResubmitInstructions(actionsInfos);
386 String flowName = "Egress_DHCP_Client_v6" + "_" + dpId + "_" + lportTag + "_" + dhcpMacAddress + "_Permit_";
387 syncFlow(dpId, NwConstants.INGRESS_ACL_TABLE, flowName, AclConstants.PROTO_DHCP_CLIENT_TRAFFIC_MATCH_PRIORITY,
388 "ACL", 0, 0, AclConstants.COOKIE_ACL_BASE, matches, instructions, addOrRemove);
394 * @param dpId the dp id
395 * @param allowedAddresses the allowed addresses
396 * @param lportTag the lport tag
397 * @param addOrRemove whether to add or remove the flow
399 protected void programArpRule(BigInteger dpId, List<AllowedAddressPairs> allowedAddresses, int lportTag,
401 // Collecting macs as a set to avoid duplicate
402 Set<MacAddress> macs = allowedAddresses.stream().map(aap -> aap.getMacAddress()).collect(Collectors.toSet());
403 programArpRule(dpId, macs, lportTag, addOrRemove);
407 * Adds the rule to allow arp packets.
409 * @param dpId the dpId
410 * @param macs the set of MACs
411 * @param lportTag the lport tag
412 * @param addOrRemove whether to add or remove the flow
414 protected void programArpRule(BigInteger dpId, Set<MacAddress> macs, int lportTag, int addOrRemove) {
415 for (MacAddress mac : macs) {
416 List<MatchInfoBase> matches = new ArrayList<>();
417 matches.add(MatchEthernetType.ARP);
418 matches.add(new MatchArpSha(mac));
419 matches.add(buildLPortTagMatch(lportTag));
421 List<InstructionInfo> instructions = getDispatcherTableResubmitInstructions(new ArrayList<>());
423 String flowName = "Egress_ARP_" + dpId + "_" + lportTag + "_" + mac.getValue();
424 syncFlow(dpId, NwConstants.INGRESS_ACL_TABLE, flowName,
425 AclConstants.PROTO_ARP_TRAFFIC_MATCH_PRIORITY, "ACL", 0, 0,
426 AclConstants.COOKIE_ACL_BASE, matches, instructions, addOrRemove);
430 protected MatchInfoBase buildLPortTagMatch(int lportTag) {
431 return AclServiceUtils.buildLPortTagMatch(lportTag, ServiceModeEgress.class);
434 protected int getEgressSpecificAclFlowPriority(BigInteger dpId, int addOrRemove, String flowName,
435 PacketHandling packetHandling) {
437 if (addOrRemove == NwConstants.DEL_FLOW) {
438 priority = aclServiceUtils.releaseAndRemoveFlowPriorityFromCache(dpId, NwConstants.INGRESS_ACL_FILTER_TABLE,
439 flowName, packetHandling);
441 priority = aclServiceUtils.allocateAndSaveFlowPriorityInCache(dpId, NwConstants.INGRESS_ACL_FILTER_TABLE,
442 flowName, packetHandling);