2 * Copyright (c) 2016 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 com.google.common.util.concurrent.ListenableFuture;
13 import java.math.BigInteger;
14 import java.util.ArrayList;
15 import java.util.Collections;
16 import java.util.List;
17 import org.opendaylight.controller.liblldp.NetUtils;
18 import org.opendaylight.controller.liblldp.PacketException;
19 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
20 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
21 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
22 import org.opendaylight.genius.datastoreutils.DataStoreJobCoordinator;
23 import org.opendaylight.genius.interfacemanager.globals.InterfaceInfo;
24 import org.opendaylight.genius.interfacemanager.interfaces.IInterfaceManager;
25 import org.opendaylight.genius.mdsalutil.MetaDataUtil;
26 import org.opendaylight.genius.mdsalutil.NWUtil;
27 import org.opendaylight.genius.mdsalutil.NwConstants;
28 import org.opendaylight.genius.mdsalutil.packet.Ethernet;
29 import org.opendaylight.netvirt.elan.l2gw.utils.ElanL2GatewayUtils;
30 import org.opendaylight.netvirt.elan.utils.ElanUtils;
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.instances.ElanInstance;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.tag.name.map.ElanTagName;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.forwarding.entries.MacEntry;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.forwarding.entries.MacEntryBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.forwarding.entries.MacEntryKey;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.NoMatch;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketInReason;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketProcessingListener;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived;
42 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
46 @SuppressWarnings("deprecation")
47 public class ElanPacketInHandler implements PacketProcessingListener {
49 private static final Logger LOG = LoggerFactory.getLogger(ElanPacketInHandler.class);
51 private final DataBroker broker;
52 private final IInterfaceManager interfaceManager;
53 private final ElanUtils elanUtils;
54 private final ElanL2GatewayUtils elanL2GatewayUtils;
56 public ElanPacketInHandler(DataBroker dataBroker, final IInterfaceManager interfaceManager, ElanUtils elanUtils) {
58 this.interfaceManager = interfaceManager;
59 this.elanUtils = elanUtils;
60 this.elanL2GatewayUtils = elanUtils.getElanL2GatewayUtils();
64 public void onPacketReceived(PacketReceived notification) {
65 Class<? extends PacketInReason> pktInReason = notification.getPacketInReason();
66 short tableId = notification.getTableId().getValue();
67 if (pktInReason == NoMatch.class && tableId == NwConstants.ELAN_SMAC_TABLE) {
68 ElanManagerCounters.unknown_smac_pktin_rcv.inc();
70 byte[] data = notification.getPayload();
71 Ethernet res = new Ethernet();
73 res.deserialize(data, 0, data.length * NetUtils.NumBitsInAByte);
75 byte[] srcMac = res.getSourceMACAddress();
76 final String macAddress = NWUtil.toStringMacAddress(srcMac);
77 final BigInteger metadata = notification.getMatch().getMetadata().getMetadata();
78 final long elanTag = MetaDataUtil.getElanTagFromMetadata(metadata);
80 long portTag = MetaDataUtil.getLportFromMetadata(metadata).intValue();
82 Optional<IfIndexInterface> interfaceInfoOp = elanUtils.getInterfaceInfoByInterfaceTag(portTag);
83 if (!interfaceInfoOp.isPresent()) {
84 LOG.warn("There is no interface for given portTag {}", portTag);
87 String interfaceName = interfaceInfoOp.get().getInterfaceName();
88 LOG.debug("Received a packet with srcMac: {} ElanTag: {} PortTag: {} InterfaceName: {}", macAddress,
89 elanTag, portTag, interfaceName);
90 ElanTagName elanTagName = elanUtils.getElanInfoByElanTag(elanTag);
91 if (elanTagName == null) {
92 LOG.warn("not able to find elanTagName in elan-tag-name-map for elan tag {}", elanTag);
95 String elanName = elanTagName.getName();
96 PhysAddress physAddress = new PhysAddress(macAddress);
97 ElanInstance elanInstance = ElanUtils.getElanInstanceByName(broker, elanName);
98 InterfaceInfo interfaceInfo = interfaceManager.getInterfaceInfo(interfaceName);
99 MacEntry oldMacEntry = elanUtils.getMacEntryForElanInstance(elanName, physAddress).orNull();
100 boolean isVlanOrFlatProviderIface = interfaceManager.isExternalInterface(interfaceName);
102 BigInteger timeStamp = new BigInteger(String.valueOf(System.currentTimeMillis()));
103 MacEntry newMacEntry = new MacEntryBuilder().setInterface(interfaceName).setMacAddress(physAddress)
104 .setKey(new MacEntryKey(physAddress)).setControllerLearnedForwardingEntryTimestamp(timeStamp)
105 .setIsStaticAddress(false).build();
107 final DataStoreJobCoordinator portDataStoreCoordinator = DataStoreJobCoordinator.getInstance();
108 enqueueJobForMacSpecificTasks(macAddress, elanTag, interfaceName, elanName, physAddress, oldMacEntry,
109 newMacEntry, isVlanOrFlatProviderIface, portDataStoreCoordinator);
111 enqueueJobForDPNSpecificTasks(macAddress, elanTag, interfaceName, physAddress, elanInstance,
112 interfaceInfo, oldMacEntry, newMacEntry, isVlanOrFlatProviderIface, portDataStoreCoordinator);
115 } catch (PacketException e) {
116 LOG.error("Failed to decode packet: {}", notification, e);
121 private void enqueueJobForMacSpecificTasks(final String macAddress, final long elanTag, String interfaceName,
122 String elanName, PhysAddress physAddress, MacEntry oldMacEntry, MacEntry newMacEntry,
123 final boolean isVlanOrFlatProviderIface, final DataStoreJobCoordinator portDataStoreCoordinator) {
124 portDataStoreCoordinator.enqueueJob(ElanUtils.getElanMacKey(elanTag, macAddress), () -> {
125 WriteTransaction writeTx = broker.newWriteOnlyTransaction();
126 if (oldMacEntry != null && oldMacEntry.getInterface().equals(interfaceName)) {
127 // This should never occur because of ovs temporary mac learning
128 ElanManagerCounters.unknown_smac_pktin_forwarding_entries_removed.inc();
129 } else if (oldMacEntry != null && !isVlanOrFlatProviderIface) {
130 long macTimeStamp = oldMacEntry.getControllerLearnedForwardingEntryTimestamp().longValue();
131 if (System.currentTimeMillis() > macTimeStamp + 1000) {
132 InstanceIdentifier<MacEntry> macEntryId = ElanUtils
133 .getInterfaceMacEntriesIdentifierOperationalDataPath(interfaceName,
135 writeTx.delete(LogicalDatastoreType.OPERATIONAL, macEntryId);
137 // New FEs flood their packets on all interfaces. This
139 // to many contradicting packet_ins. Ignore all packets
141 // within 1s after the first packet_in
142 ElanManagerCounters.unknown_smac_pktin_mac_migration_ignored_due_to_protection.inc();
144 } else if (oldMacEntry != null) {
145 ElanManagerCounters.unknown_smac_pktin_removed_for_relearned.inc();
147 // This check is required only to update elan-forwarding-tables when mac is learned
148 // in ports (example: VM interfaces) other than on vlan provider port.
149 if (!isVlanOrFlatProviderIface && oldMacEntry == null) {
150 InstanceIdentifier<MacEntry> elanMacEntryId =
151 ElanUtils.getMacEntryOperationalDataPath(elanName, physAddress);
152 writeTx.put(LogicalDatastoreType.OPERATIONAL, elanMacEntryId, newMacEntry, true);
154 List<ListenableFuture<Void>> futures = new ArrayList<>();
155 futures.add(writeTx.submit());
160 private void enqueueJobForDPNSpecificTasks(final String macAddress, final long elanTag, String interfaceName,
161 PhysAddress physAddress, ElanInstance elanInstance, InterfaceInfo interfaceInfo,
162 MacEntry oldMacEntry, MacEntry newMacEntry, boolean isVlanOrFlatProviderIface,
163 final DataStoreJobCoordinator portDataStoreCoordinator) {
164 portDataStoreCoordinator
165 .enqueueJob(ElanUtils.getElanMacDPNKey(elanTag, macAddress, interfaceInfo.getDpId()), () -> {
166 macMigrationFlowsCleanup(interfaceName, elanInstance, oldMacEntry, isVlanOrFlatProviderIface);
167 BigInteger dpId = interfaceManager.getDpnForInterface(interfaceName);
168 elanL2GatewayUtils.scheduleAddDpnMacInExtDevices(elanInstance.getElanInstanceName(), dpId,
169 Collections.singletonList(physAddress));
170 ElanManagerCounters.unknown_smac_pktin_learned.inc();
171 WriteTransaction flowWritetx = broker.newWriteOnlyTransaction();
172 elanUtils.setupMacFlows(elanInstance, interfaceInfo, elanInstance.getMacTimeout(),
173 macAddress, !isVlanOrFlatProviderIface, flowWritetx);
174 InstanceIdentifier<MacEntry> macEntryId =
175 ElanUtils.getInterfaceMacEntriesIdentifierOperationalDataPath(interfaceName, physAddress);
176 flowWritetx.put(LogicalDatastoreType.OPERATIONAL, macEntryId, newMacEntry, true);
177 List<ListenableFuture<Void>> futures = new ArrayList<>();
178 futures.add(flowWritetx.submit());
183 private void macMigrationFlowsCleanup(String interfaceName, ElanInstance elanInstance, MacEntry macEntry,
184 boolean isVlanOrFlatProviderIface) {
185 if (macEntry != null && !macEntry.getInterface().equals(interfaceName)
186 && !isVlanOrFlatProviderIface) {
187 tryAndRemoveInvalidMacEntry(elanInstance.getElanInstanceName(), macEntry);
188 ElanManagerCounters.unknown_smac_pktin_flows_removed_for_relearned.inc();
193 * Though this method is a little costlier because it uses try-catch
194 * construct, it is used only in rare scenarios like MAC movement or invalid
195 * Static MAC having been added on a wrong ELAN.
197 private void tryAndRemoveInvalidMacEntry(String elanName, MacEntry macEntry) {
198 ElanInstance elanInfo = ElanUtils.getElanInstanceByName(broker, elanName);
199 if (elanInfo == null) {
200 LOG.warn("MAC {} is been added (either statically or dynamically) for an invalid Elan {}. "
201 + "Manual cleanup may be necessary", macEntry.getMacAddress(), elanName);
205 InterfaceInfo oldInterfaceLport = interfaceManager.getInterfaceInfo(macEntry.getInterface());
206 if (oldInterfaceLport == null) {
207 LOG.warn("MAC {} is been added (either statically or dynamically) on an invalid Logical Port {}. "
208 + "Manual cleanup may be necessary",
209 macEntry.getMacAddress(), macEntry.getInterface());
212 WriteTransaction flowDeletetx = broker.newWriteOnlyTransaction();
213 elanUtils.deleteMacFlows(elanInfo, oldInterfaceLport, macEntry, flowDeletetx);
214 flowDeletetx.submit();
215 elanL2GatewayUtils.removeMacsFromElanExternalDevices(elanInfo,
216 Collections.singletonList(macEntry.getMacAddress()));