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