2 * Copyright (c) 2016, 2018 Ericsson India Global Services Pvt Ltd. 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.elan.internal;
10 import com.google.common.base.Optional;
11 import java.math.BigInteger;
12 import java.util.Collections;
13 import javax.inject.Inject;
14 import javax.inject.Singleton;
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
17 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
19 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
20 import org.opendaylight.genius.interfacemanager.globals.InterfaceInfo;
21 import org.opendaylight.genius.interfacemanager.interfaces.IInterfaceManager;
22 import org.opendaylight.genius.mdsalutil.MetaDataUtil;
23 import org.opendaylight.genius.mdsalutil.NWUtil;
24 import org.opendaylight.genius.mdsalutil.NwConstants;
25 import org.opendaylight.genius.mdsalutil.packet.Ethernet;
26 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
27 import org.opendaylight.infrautils.utils.concurrent.ListenableFutures;
28 import org.opendaylight.netvirt.elan.cache.ElanInstanceCache;
29 import org.opendaylight.netvirt.elan.evpn.utils.EvpnUtils;
30 import org.opendaylight.netvirt.elan.l2gw.utils.ElanL2GatewayUtils;
31 import org.opendaylight.netvirt.elan.utils.ElanUtils;
32 import org.opendaylight.openflowplugin.libraries.liblldp.NetUtils;
33 import org.opendaylight.openflowplugin.libraries.liblldp.PacketException;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.meta.rev160406._if.indexes._interface.map.IfIndexInterface;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan._interface.forwarding.entries.ElanInterfaceMac;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.tag.name.map.ElanTagName;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.forwarding.entries.MacEntry;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.forwarding.entries.MacEntryBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.forwarding.entries.MacEntryKey;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.NoMatch;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketInReason;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketProcessingListener;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived;
47 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
52 public class ElanPacketInHandler implements PacketProcessingListener {
54 private static final Logger LOG = LoggerFactory.getLogger(ElanPacketInHandler.class);
56 private final ManagedNewTransactionRunner txRunner;
57 private final IInterfaceManager interfaceManager;
58 private final ElanUtils elanUtils;
59 private final ElanL2GatewayUtils elanL2GatewayUtils;
60 private final EvpnUtils evpnUtils;
61 private final JobCoordinator jobCoordinator;
62 private final ElanInstanceCache elanInstanceCache;
65 public ElanPacketInHandler(DataBroker dataBroker, final IInterfaceManager interfaceManager, ElanUtils elanUtils,
66 EvpnUtils evpnUtils, ElanL2GatewayUtils elanL2GatewayUtils, JobCoordinator jobCoordinator,
67 ElanInstanceCache elanInstanceCache) {
68 this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
69 this.interfaceManager = interfaceManager;
70 this.elanUtils = elanUtils;
71 this.elanL2GatewayUtils = elanL2GatewayUtils;
72 this.evpnUtils = evpnUtils;
73 this.jobCoordinator = jobCoordinator;
74 this.elanInstanceCache = elanInstanceCache;
78 public void onPacketReceived(PacketReceived notification) {
79 Class<? extends PacketInReason> pktInReason = notification.getPacketInReason();
80 short tableId = notification.getTableId().getValue();
81 if (pktInReason == NoMatch.class && tableId == NwConstants.ELAN_SMAC_TABLE) {
82 ElanManagerCounters.unknown_smac_pktin_rcv.inc();
84 byte[] data = notification.getPayload();
85 Ethernet res = new Ethernet();
87 res.deserialize(data, 0, data.length * NetUtils.NUM_BITS_IN_A_BYTE);
89 byte[] srcMac = res.getSourceMACAddress();
90 final String macAddress = NWUtil.toStringMacAddress(srcMac);
91 final BigInteger metadata = notification.getMatch().getMetadata().getMetadata();
92 final long elanTag = MetaDataUtil.getElanTagFromMetadata(metadata);
94 long portTag = MetaDataUtil.getLportFromMetadata(metadata).intValue();
96 Optional<IfIndexInterface> interfaceInfoOp = elanUtils.getInterfaceInfoByInterfaceTag(portTag);
97 if (!interfaceInfoOp.isPresent()) {
98 LOG.warn("There is no interface for given portTag {}", portTag);
101 String interfaceName = interfaceInfoOp.get().getInterfaceName();
102 LOG.debug("Received a packet with srcMac: {} ElanTag: {} PortTag: {} InterfaceName: {}", macAddress,
103 elanTag, portTag, interfaceName);
104 ElanTagName elanTagName = elanUtils.getElanInfoByElanTag(elanTag);
105 if (elanTagName == null) {
106 LOG.warn("not able to find elanTagName in elan-tag-name-map for elan tag {}", elanTag);
109 ElanInterfaceMac elanInterfaceMac = elanUtils.getElanInterfaceMacByInterfaceName(interfaceName);
110 if (elanInterfaceMac == null) {
111 LOG.info("There is no ElanInterfaceForwardingEntryDS created for interface :{}", interfaceName);
114 String elanName = elanTagName.getName();
115 PhysAddress physAddress = new PhysAddress(macAddress);
116 MacEntry oldMacEntry = elanUtils.getMacEntryForElanInstance(elanName, physAddress).orNull();
117 boolean isVlanOrFlatProviderIface = interfaceManager.isExternalInterface(interfaceName);
119 Optional<IpAddress> srcIpAddress = elanUtils.getSourceIpAddress(res);
120 MacEntry newMacEntry = null;
121 BigInteger timeStamp = new BigInteger(String.valueOf(System.currentTimeMillis()));
122 if (!srcIpAddress.isPresent()) {
123 newMacEntry = new MacEntryBuilder().setInterface(interfaceName).setMacAddress(physAddress)
124 .setKey(new MacEntryKey(physAddress))
125 .setControllerLearnedForwardingEntryTimestamp(timeStamp)
126 .setIsStaticAddress(false).build();
128 newMacEntry = new MacEntryBuilder().setInterface(interfaceName).setMacAddress(physAddress)
129 .setIpPrefix(srcIpAddress.get()).setKey(new MacEntryKey(physAddress))
130 .setControllerLearnedForwardingEntryTimestamp(timeStamp)
131 .setIsStaticAddress(false).build();
133 if (srcIpAddress.isPresent()) {
134 String prefix = srcIpAddress.get().getIpv4Address().getValue();
135 InterfaceInfo interfaceInfo = interfaceManager.getInterfaceInfo(interfaceName);
136 ElanInstance elanInstance = elanInstanceCache.get(elanName).orNull();
137 evpnUtils.advertisePrefix(elanInstance, macAddress, prefix, interfaceName, interfaceInfo.getDpId());
139 enqueueJobForMacSpecificTasks(macAddress, elanTag, interfaceName, elanName, physAddress, oldMacEntry,
140 newMacEntry, isVlanOrFlatProviderIface);
142 ElanInstance elanInstance = elanInstanceCache.get(elanName).orNull();
143 InterfaceInfo interfaceInfo = interfaceManager.getInterfaceInfo(interfaceName);
144 if (interfaceInfo == null) {
145 LOG.trace("Interface:{} is not present under Config DS", interfaceName);
148 enqueueJobForDPNSpecificTasks(macAddress, elanTag, interfaceName, physAddress, elanInstance,
149 interfaceInfo, oldMacEntry, newMacEntry, isVlanOrFlatProviderIface);
152 } catch (PacketException e) {
153 LOG.error("Failed to decode packet: {}", notification, e);
158 private void enqueueJobForMacSpecificTasks(final String macAddress, final long elanTag, String interfaceName,
159 String elanName, PhysAddress physAddress,
160 MacEntry oldMacEntry, MacEntry newMacEntry,
161 final boolean isVlanOrFlatProviderIface) {
162 jobCoordinator.enqueueJob(ElanUtils.getElanMacKey(elanTag, macAddress),
163 () -> Collections.singletonList(txRunner.callWithNewWriteOnlyTransactionAndSubmit(tx -> {
164 if (oldMacEntry != null && oldMacEntry.getInterface().equals(interfaceName)) {
165 // This should never occur because of ovs temporary mac learning
166 ElanManagerCounters.unknown_smac_pktin_forwarding_entries_removed.inc();
167 } else if (oldMacEntry != null && !isVlanOrFlatProviderIface) {
168 long macTimeStamp = oldMacEntry.getControllerLearnedForwardingEntryTimestamp().longValue();
169 if (System.currentTimeMillis() > macTimeStamp + 1000) {
170 InstanceIdentifier<MacEntry> macEntryId = ElanUtils
171 .getInterfaceMacEntriesIdentifierOperationalDataPath(interfaceName,
173 tx.delete(LogicalDatastoreType.OPERATIONAL, macEntryId);
175 // New FEs flood their packets on all interfaces. This can lead
176 // to many contradicting packet_ins. Ignore all packets received
177 // within 1s after the first packet_in
178 ElanManagerCounters.unknown_smac_pktin_mac_migration_ignored_due_to_protection.inc();
180 } else if (oldMacEntry != null) {
181 ElanManagerCounters.unknown_smac_pktin_removed_for_relearned.inc();
183 // This check is required only to update elan-forwarding-tables when mac is learned
184 // in ports (example: VM interfaces) other than on vlan provider port.
185 if (!isVlanOrFlatProviderIface && oldMacEntry == null) {
186 InstanceIdentifier<MacEntry> elanMacEntryId =
187 ElanUtils.getMacEntryOperationalDataPath(elanName, physAddress);
188 tx.put(LogicalDatastoreType.OPERATIONAL, elanMacEntryId, newMacEntry,
189 WriteTransaction.CREATE_MISSING_PARENTS);
194 private void enqueueJobForDPNSpecificTasks(final String macAddress, final long elanTag, String interfaceName,
195 PhysAddress physAddress, ElanInstance elanInstance,
196 InterfaceInfo interfaceInfo, MacEntry oldMacEntry,
197 MacEntry newMacEntry, boolean isVlanOrFlatProviderIface) {
198 jobCoordinator.enqueueJob(ElanUtils.getElanMacDPNKey(elanTag, macAddress, interfaceInfo.getDpId()), () -> {
199 macMigrationFlowsCleanup(interfaceName, elanInstance, oldMacEntry, isVlanOrFlatProviderIface);
200 BigInteger dpId = interfaceManager.getDpnForInterface(interfaceName);
201 elanL2GatewayUtils.scheduleAddDpnMacInExtDevices(elanInstance.getElanInstanceName(), dpId,
202 Collections.singletonList(physAddress));
203 ElanManagerCounters.unknown_smac_pktin_learned.inc();
204 return Collections.singletonList(txRunner.callWithNewWriteOnlyTransactionAndSubmit(tx -> {
205 elanUtils.setupMacFlows(elanInstance, interfaceInfo, elanInstance.getMacTimeout(),
206 macAddress, !isVlanOrFlatProviderIface, tx);
207 InstanceIdentifier<MacEntry> macEntryId =
208 ElanUtils.getInterfaceMacEntriesIdentifierOperationalDataPath(interfaceName, physAddress);
209 tx.put(LogicalDatastoreType.OPERATIONAL, macEntryId, newMacEntry,
210 WriteTransaction.CREATE_MISSING_PARENTS);
215 private void macMigrationFlowsCleanup(String interfaceName, ElanInstance elanInstance, MacEntry macEntry,
216 boolean isVlanOrFlatProviderIface) {
217 if (macEntry != null && !macEntry.getInterface().equals(interfaceName)
218 && !isVlanOrFlatProviderIface) {
219 tryAndRemoveInvalidMacEntry(elanInstance.getElanInstanceName(), macEntry);
220 ElanManagerCounters.unknown_smac_pktin_flows_removed_for_relearned.inc();
225 * Though this method is a little costlier because it uses try-catch
226 * construct, it is used only in rare scenarios like MAC movement or invalid
227 * Static MAC having been added on a wrong ELAN.
229 private void tryAndRemoveInvalidMacEntry(String elanName, MacEntry macEntry) {
230 ElanInstance elanInfo = elanInstanceCache.get(elanName).orNull();
231 if (elanInfo == null) {
232 LOG.warn("MAC {} is been added (either statically or dynamically) for an invalid Elan {}. "
233 + "Manual cleanup may be necessary", macEntry.getMacAddress(), elanName);
237 InterfaceInfo oldInterfaceLport = interfaceManager.getInterfaceInfo(macEntry.getInterface());
238 if (oldInterfaceLport == null) {
239 LOG.warn("MAC {} is been added (either statically or dynamically) on an invalid Logical Port {}. "
240 + "Manual cleanup may be necessary",
241 macEntry.getMacAddress(), macEntry.getInterface());
244 ListenableFutures.addErrorLogging(txRunner.callWithNewWriteOnlyTransactionAndSubmit(
245 tx -> elanUtils.deleteMacFlows(elanInfo, oldInterfaceLport, macEntry, tx)), LOG,
246 "Error deleting invalid MAC entry");
247 elanL2GatewayUtils.removeMacsFromElanExternalDevices(elanInfo,
248 Collections.singletonList(macEntry.getMacAddress()));