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.interfacemanager.globals.InterfaceInfo;
19 import org.opendaylight.genius.interfacemanager.interfaces.IInterfaceManager;
20 import org.opendaylight.genius.mdsalutil.MetaDataUtil;
21 import org.opendaylight.genius.mdsalutil.NWUtil;
22 import org.opendaylight.genius.mdsalutil.NwConstants;
23 import org.opendaylight.genius.mdsalutil.packet.Ethernet;
24 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
25 import org.opendaylight.netvirt.elan.evpn.utils.EvpnUtils;
26 import org.opendaylight.netvirt.elan.l2gw.utils.ElanL2GatewayUtils;
27 import org.opendaylight.netvirt.elan.utils.ElanUtils;
28 import org.opendaylight.openflowplugin.libraries.liblldp.NetUtils;
29 import org.opendaylight.openflowplugin.libraries.liblldp.PacketException;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.meta.rev160406._if.indexes._interface.map.IfIndexInterface;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan._interface.forwarding.entries.ElanInterfaceMac;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.tag.name.map.ElanTagName;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.forwarding.entries.MacEntry;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.forwarding.entries.MacEntryBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.forwarding.entries.MacEntryKey;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.NoMatch;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketInReason;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketProcessingListener;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived;
43 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
48 public class ElanPacketInHandler implements PacketProcessingListener {
50 private static final Logger LOG = LoggerFactory.getLogger(ElanPacketInHandler.class);
52 private final DataBroker broker;
53 private final IInterfaceManager interfaceManager;
54 private final ElanUtils elanUtils;
55 private final ElanL2GatewayUtils elanL2GatewayUtils;
56 private final EvpnUtils evpnUtils;
57 private final JobCoordinator jobCoordinator;
60 public ElanPacketInHandler(DataBroker dataBroker, final IInterfaceManager interfaceManager, ElanUtils elanUtils,
61 EvpnUtils evpnUtils, ElanL2GatewayUtils elanL2GatewayUtils, JobCoordinator jobCoordinator) {
63 this.interfaceManager = interfaceManager;
64 this.elanUtils = elanUtils;
65 this.elanL2GatewayUtils = elanL2GatewayUtils;
66 this.evpnUtils = evpnUtils;
67 this.jobCoordinator = jobCoordinator;
71 public void onPacketReceived(PacketReceived notification) {
72 Class<? extends PacketInReason> pktInReason = notification.getPacketInReason();
73 short tableId = notification.getTableId().getValue();
74 if (pktInReason == NoMatch.class && tableId == NwConstants.ELAN_SMAC_TABLE) {
75 ElanManagerCounters.unknown_smac_pktin_rcv.inc();
77 byte[] data = notification.getPayload();
78 Ethernet res = new Ethernet();
80 res.deserialize(data, 0, data.length * NetUtils.NUM_BITS_IN_A_BYTE);
82 byte[] srcMac = res.getSourceMACAddress();
83 final String macAddress = NWUtil.toStringMacAddress(srcMac);
84 final BigInteger metadata = notification.getMatch().getMetadata().getMetadata();
85 final long elanTag = MetaDataUtil.getElanTagFromMetadata(metadata);
87 long portTag = MetaDataUtil.getLportFromMetadata(metadata).intValue();
89 Optional<IfIndexInterface> interfaceInfoOp = elanUtils.getInterfaceInfoByInterfaceTag(portTag);
90 if (!interfaceInfoOp.isPresent()) {
91 LOG.warn("There is no interface for given portTag {}", portTag);
94 String interfaceName = interfaceInfoOp.get().getInterfaceName();
95 LOG.debug("Received a packet with srcMac: {} ElanTag: {} PortTag: {} InterfaceName: {}", macAddress,
96 elanTag, portTag, interfaceName);
97 ElanTagName elanTagName = elanUtils.getElanInfoByElanTag(elanTag);
98 if (elanTagName == null) {
99 LOG.warn("not able to find elanTagName in elan-tag-name-map for elan tag {}", elanTag);
102 ElanInterfaceMac elanInterfaceMac = elanUtils.getElanInterfaceMacByInterfaceName(interfaceName);
103 if (elanInterfaceMac == null) {
104 LOG.info("There is no ElanInterfaceForwardingEntryDS created for interface :{}", interfaceName);
107 String elanName = elanTagName.getName();
108 PhysAddress physAddress = new PhysAddress(macAddress);
109 MacEntry oldMacEntry = elanUtils.getMacEntryForElanInstance(elanName, physAddress).orNull();
110 boolean isVlanOrFlatProviderIface = interfaceManager.isExternalInterface(interfaceName);
112 Optional<IpAddress> srcIpAddress = elanUtils.getSourceIpAddress(res);
113 MacEntry newMacEntry = null;
114 BigInteger timeStamp = new BigInteger(String.valueOf(System.currentTimeMillis()));
115 if (!srcIpAddress.isPresent()) {
116 newMacEntry = new MacEntryBuilder().setInterface(interfaceName).setMacAddress(physAddress)
117 .setKey(new MacEntryKey(physAddress))
118 .setControllerLearnedForwardingEntryTimestamp(timeStamp)
119 .setIsStaticAddress(false).build();
121 newMacEntry = new MacEntryBuilder().setInterface(interfaceName).setMacAddress(physAddress)
122 .setIpPrefix(srcIpAddress.get()).setKey(new MacEntryKey(physAddress))
123 .setControllerLearnedForwardingEntryTimestamp(timeStamp)
124 .setIsStaticAddress(false).build();
126 if (srcIpAddress.isPresent()) {
127 String prefix = srcIpAddress.get().getIpv4Address().getValue();
128 InterfaceInfo interfaceInfo = interfaceManager.getInterfaceInfo(interfaceName);
129 ElanInstance elanInstance = ElanUtils.getElanInstanceByName(broker, elanName);
130 evpnUtils.advertisePrefix(elanInstance, macAddress, prefix, interfaceName, interfaceInfo.getDpId());
132 enqueueJobForMacSpecificTasks(macAddress, elanTag, interfaceName, elanName, physAddress, oldMacEntry,
133 newMacEntry, isVlanOrFlatProviderIface);
135 ElanInstance elanInstance = ElanUtils.getElanInstanceByName(broker, elanName);
136 InterfaceInfo interfaceInfo = interfaceManager.getInterfaceInfo(interfaceName);
137 if (interfaceInfo == null) {
138 LOG.trace("Interface:{} is not present under Config DS", interfaceName);
141 enqueueJobForDPNSpecificTasks(macAddress, elanTag, interfaceName, physAddress, elanInstance,
142 interfaceInfo, oldMacEntry, newMacEntry, isVlanOrFlatProviderIface);
145 } catch (PacketException e) {
146 LOG.error("Failed to decode packet: {}", notification, e);
151 private void enqueueJobForMacSpecificTasks(final String macAddress, final long elanTag, String interfaceName,
152 String elanName, PhysAddress physAddress,
153 MacEntry oldMacEntry, MacEntry newMacEntry,
154 final boolean isVlanOrFlatProviderIface) {
155 jobCoordinator.enqueueJob(ElanUtils.getElanMacKey(elanTag, macAddress), () -> {
156 WriteTransaction writeTx = broker.newWriteOnlyTransaction();
157 if (oldMacEntry != null && oldMacEntry.getInterface().equals(interfaceName)) {
158 // This should never occur because of ovs temporary mac learning
159 ElanManagerCounters.unknown_smac_pktin_forwarding_entries_removed.inc();
160 } else if (oldMacEntry != null && !isVlanOrFlatProviderIface) {
161 long macTimeStamp = oldMacEntry.getControllerLearnedForwardingEntryTimestamp().longValue();
162 if (System.currentTimeMillis() > macTimeStamp + 1000) {
163 InstanceIdentifier<MacEntry> macEntryId = ElanUtils
164 .getInterfaceMacEntriesIdentifierOperationalDataPath(interfaceName,
166 writeTx.delete(LogicalDatastoreType.OPERATIONAL, macEntryId);
168 // New FEs flood their packets on all interfaces. This
170 // to many contradicting packet_ins. Ignore all packets
172 // within 1s after the first packet_in
173 ElanManagerCounters.unknown_smac_pktin_mac_migration_ignored_due_to_protection.inc();
175 } else if (oldMacEntry != null) {
176 ElanManagerCounters.unknown_smac_pktin_removed_for_relearned.inc();
178 // This check is required only to update elan-forwarding-tables when mac is learned
179 // in ports (example: VM interfaces) other than on vlan provider port.
180 if (!isVlanOrFlatProviderIface && oldMacEntry == null) {
181 InstanceIdentifier<MacEntry> elanMacEntryId =
182 ElanUtils.getMacEntryOperationalDataPath(elanName, physAddress);
183 writeTx.put(LogicalDatastoreType.OPERATIONAL, elanMacEntryId, newMacEntry,
184 WriteTransaction.CREATE_MISSING_PARENTS);
186 return Collections.singletonList(writeTx.submit());
190 private void enqueueJobForDPNSpecificTasks(final String macAddress, final long elanTag, String interfaceName,
191 PhysAddress physAddress, ElanInstance elanInstance,
192 InterfaceInfo interfaceInfo, MacEntry oldMacEntry,
193 MacEntry newMacEntry, boolean isVlanOrFlatProviderIface) {
194 jobCoordinator.enqueueJob(ElanUtils.getElanMacDPNKey(elanTag, macAddress, interfaceInfo.getDpId()), () -> {
195 macMigrationFlowsCleanup(interfaceName, elanInstance, oldMacEntry, isVlanOrFlatProviderIface);
196 BigInteger dpId = interfaceManager.getDpnForInterface(interfaceName);
197 elanL2GatewayUtils.scheduleAddDpnMacInExtDevices(elanInstance.getElanInstanceName(), dpId,
198 Collections.singletonList(physAddress));
199 ElanManagerCounters.unknown_smac_pktin_learned.inc();
200 WriteTransaction flowWritetx = broker.newWriteOnlyTransaction();
201 elanUtils.setupMacFlows(elanInstance, interfaceInfo, elanInstance.getMacTimeout(),
202 macAddress, !isVlanOrFlatProviderIface, flowWritetx);
203 InstanceIdentifier<MacEntry> macEntryId =
204 ElanUtils.getInterfaceMacEntriesIdentifierOperationalDataPath(interfaceName, physAddress);
205 flowWritetx.put(LogicalDatastoreType.OPERATIONAL, macEntryId, newMacEntry,
206 WriteTransaction.CREATE_MISSING_PARENTS);
207 return Collections.singletonList(flowWritetx.submit());
211 private void macMigrationFlowsCleanup(String interfaceName, ElanInstance elanInstance, MacEntry macEntry,
212 boolean isVlanOrFlatProviderIface) {
213 if (macEntry != null && !macEntry.getInterface().equals(interfaceName)
214 && !isVlanOrFlatProviderIface) {
215 tryAndRemoveInvalidMacEntry(elanInstance.getElanInstanceName(), macEntry);
216 ElanManagerCounters.unknown_smac_pktin_flows_removed_for_relearned.inc();
221 * Though this method is a little costlier because it uses try-catch
222 * construct, it is used only in rare scenarios like MAC movement or invalid
223 * Static MAC having been added on a wrong ELAN.
225 private void tryAndRemoveInvalidMacEntry(String elanName, MacEntry macEntry) {
226 ElanInstance elanInfo = ElanUtils.getElanInstanceByName(broker, elanName);
227 if (elanInfo == null) {
228 LOG.warn("MAC {} is been added (either statically or dynamically) for an invalid Elan {}. "
229 + "Manual cleanup may be necessary", macEntry.getMacAddress(), elanName);
233 InterfaceInfo oldInterfaceLport = interfaceManager.getInterfaceInfo(macEntry.getInterface());
234 if (oldInterfaceLport == null) {
235 LOG.warn("MAC {} is been added (either statically or dynamically) on an invalid Logical Port {}. "
236 + "Manual cleanup may be necessary",
237 macEntry.getMacAddress(), macEntry.getInterface());
240 WriteTransaction flowDeletetx = broker.newWriteOnlyTransaction();
241 elanUtils.deleteMacFlows(elanInfo, oldInterfaceLport, macEntry, flowDeletetx);
242 flowDeletetx.submit();
243 elanL2GatewayUtils.removeMacsFromElanExternalDevices(elanInfo,
244 Collections.singletonList(macEntry.getMacAddress()));