2 * Copyright © 2017 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.fibmanager;
10 import static java.util.stream.Collectors.toList;
11 import static org.opendaylight.genius.mdsalutil.NWUtil.isIpv4Address;
12 import static org.opendaylight.netvirt.fibmanager.FibUtil.nullToEmpty;
14 import com.google.common.base.Optional;
15 import java.math.BigInteger;
16 import java.net.Inet4Address;
17 import java.net.InetAddress;
18 import java.net.UnknownHostException;
19 import java.time.Duration;
20 import java.time.temporal.ChronoUnit;
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.List;
24 import javax.annotation.Nonnull;
25 import javax.annotation.Nullable;
26 import javax.inject.Inject;
27 import javax.inject.Singleton;
28 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
29 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
30 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
31 import org.opendaylight.genius.datastoreutils.listeners.DataTreeEventCallbackRegistrar;
32 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
33 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
34 import org.opendaylight.genius.mdsalutil.ActionInfo;
35 import org.opendaylight.genius.mdsalutil.FlowEntity;
36 import org.opendaylight.genius.mdsalutil.InstructionInfo;
37 import org.opendaylight.genius.mdsalutil.MDSALUtil;
38 import org.opendaylight.genius.mdsalutil.MatchInfo;
39 import org.opendaylight.genius.mdsalutil.MetaDataUtil;
40 import org.opendaylight.genius.mdsalutil.NwConstants;
41 import org.opendaylight.genius.mdsalutil.actions.ActionMoveSourceDestinationEth;
42 import org.opendaylight.genius.mdsalutil.actions.ActionMoveSourceDestinationIp;
43 import org.opendaylight.genius.mdsalutil.actions.ActionNxLoadInPort;
44 import org.opendaylight.genius.mdsalutil.actions.ActionNxResubmit;
45 import org.opendaylight.genius.mdsalutil.actions.ActionSetFieldEthernetDestination;
46 import org.opendaylight.genius.mdsalutil.actions.ActionSetFieldEthernetSource;
47 import org.opendaylight.genius.mdsalutil.actions.ActionSetFieldTunnelId;
48 import org.opendaylight.genius.mdsalutil.actions.ActionSetIcmpType;
49 import org.opendaylight.genius.mdsalutil.actions.ActionSetSourceIp;
50 import org.opendaylight.genius.mdsalutil.instructions.InstructionApplyActions;
51 import org.opendaylight.genius.mdsalutil.instructions.InstructionGotoTable;
52 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
53 import org.opendaylight.genius.mdsalutil.matches.MatchEthernetDestination;
54 import org.opendaylight.genius.mdsalutil.matches.MatchEthernetType;
55 import org.opendaylight.genius.mdsalutil.matches.MatchIcmpv4;
56 import org.opendaylight.genius.mdsalutil.matches.MatchIpProtocol;
57 import org.opendaylight.genius.mdsalutil.matches.MatchIpv4Destination;
58 import org.opendaylight.genius.mdsalutil.matches.MatchIpv6Destination;
59 import org.opendaylight.genius.mdsalutil.matches.MatchMetadata;
60 import org.opendaylight.genius.utils.batching.SubTransaction;
61 import org.opendaylight.genius.utils.batching.SubTransactionImpl;
62 import org.opendaylight.infrautils.utils.concurrent.ListenableFutures;
63 import org.opendaylight.netvirt.fibmanager.NexthopManager.AdjacencyResult;
64 import org.opendaylight.netvirt.fibmanager.api.FibHelper;
65 import org.opendaylight.netvirt.fibmanager.api.RouteOrigin;
66 import org.opendaylight.netvirt.vpnmanager.api.VpnExtraRouteHelper;
67 import org.opendaylight.serviceutils.upgrade.UpgradeState;
68 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
69 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.TunnelTypeBase;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.TunnelTypeMplsOverGre;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.TunnelTypeVxlan;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.fibmanager.rev150330.fibentries.VrfTablesKey;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.fibmanager.rev150330.vrfentries.VrfEntry;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.fibmanager.rev150330.vrfentrybase.RoutePaths;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3nexthop.rev150409.l3nexthop.vpnnexthops.VpnNexthop;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.VpnToExtraroutes;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.prefix.to._interface.vpn.ids.Prefixes;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn.instance.op.data.VpnInstanceOpDataEntry;
88 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn.to.extraroutes.Vpn;
89 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn.to.extraroutes.VpnKey;
90 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn.to.extraroutes.vpn.ExtraRoutes;
91 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn.to.extraroutes.vpn.ExtraRoutesKey;
92 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn.to.extraroutes.vpn.extra.routes.Routes;
93 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn.to.extraroutes.vpn.extra.routes.RoutesKey;
94 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
95 import org.slf4j.Logger;
96 import org.slf4j.LoggerFactory;
99 public class BaseVrfEntryHandler implements AutoCloseable {
101 private static final Logger LOG = LoggerFactory.getLogger(BaseVrfEntryHandler.class);
102 private static final BigInteger COOKIE_VM_FIB_TABLE = new BigInteger("8000003", 16);
103 private static final int DEFAULT_FIB_FLOW_PRIORITY = 10;
105 private final DataBroker dataBroker;
106 private final ManagedNewTransactionRunner txRunner;
107 private final NexthopManager nextHopManager;
108 private final IMdsalApiManager mdsalManager;
109 private final FibUtil fibUtil;
110 private final UpgradeState upgradeState;
111 private final DataTreeEventCallbackRegistrar eventCallbacks;
114 public BaseVrfEntryHandler(final DataBroker dataBroker,
115 final NexthopManager nexthopManager,
116 final IMdsalApiManager mdsalManager,
117 final FibUtil fibUtil,
118 final UpgradeState upgradeState,
119 final DataTreeEventCallbackRegistrar eventCallbacks) {
120 this.dataBroker = dataBroker;
121 this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
122 this.nextHopManager = nexthopManager;
123 this.mdsalManager = mdsalManager;
124 this.fibUtil = fibUtil;
125 this.upgradeState = upgradeState;
126 this.eventCallbacks = eventCallbacks;
130 public void close() {
131 LOG.info("{} closed", getClass().getSimpleName());
134 protected FibUtil getFibUtil() {
138 protected NexthopManager getNextHopManager() {
139 return nextHopManager;
142 private void addAdjacencyResultToList(List<AdjacencyResult> adjacencyList, AdjacencyResult adjacencyResult) {
143 if (adjacencyResult != null && !adjacencyList.contains(adjacencyResult)) {
144 adjacencyList.add(adjacencyResult);
148 protected void deleteLocalAdjacency(final BigInteger dpId, final long vpnId, final String ipAddress,
149 final String ipPrefixAddress) {
150 LOG.trace("deleteLocalAdjacency called with dpid {}, vpnId{}, primaryIpAddress {} currIpPrefix {}",
151 dpId, vpnId, ipAddress, ipPrefixAddress);
153 nextHopManager.removeLocalNextHop(dpId, vpnId, ipAddress, ipPrefixAddress);
154 } catch (NullPointerException e) {
160 protected List<AdjacencyResult> resolveAdjacency(final BigInteger remoteDpnId, final long vpnId,
161 final VrfEntry vrfEntry, String rd) {
162 List<RoutePaths> routePaths = nullToEmpty(vrfEntry.getRoutePaths());
163 FibHelper.sortIpAddress(routePaths);
164 List<AdjacencyResult> adjacencyList = new ArrayList<>();
165 List<String> prefixIpList;
166 LOG.trace("resolveAdjacency called with remotedDpnId {}, vpnId{}, VrfEntry {}",
167 remoteDpnId, vpnId, vrfEntry);
168 final Class<? extends TunnelTypeBase> tunnelType;
170 if (RouteOrigin.value(vrfEntry.getOrigin()) != RouteOrigin.BGP) {
171 tunnelType = TunnelTypeVxlan.class;
172 List<String> usedRds = VpnExtraRouteHelper.getUsedRds(dataBroker, vpnId, vrfEntry.getDestPrefix());
173 List<Routes> vpnExtraRoutes = VpnExtraRouteHelper.getAllVpnExtraRoutes(dataBroker,
174 fibUtil.getVpnNameFromId(vpnId), usedRds, vrfEntry.getDestPrefix());
175 if (vpnExtraRoutes.isEmpty()) {
176 Prefixes prefixInfo = fibUtil.getPrefixToInterface(vpnId, vrfEntry.getDestPrefix());
177 /* We don't want to provide an adjacencyList for
178 * (1) an extra-route-prefix or,
179 * (2) for a local route without prefix-to-interface.
180 * Allow only self-imported routes in such cases */
181 if (prefixInfo == null && FibHelper
182 .isControllerManagedNonSelfImportedRoute(RouteOrigin.value(vrfEntry.getOrigin()))) {
183 LOG.debug("The prefix {} in rd {} for vpn {} does not have a valid extra-route or"
184 + " prefix-to-interface entry in the data-store", vrfEntry.getDestPrefix(), rd, vpnId);
185 return adjacencyList;
187 prefixIpList = Collections.singletonList(vrfEntry.getDestPrefix());
189 List<String> prefixIpListLocal = new ArrayList<>();
190 vpnExtraRoutes.forEach(route -> nullToEmpty(route.getNexthopIpList()).forEach(extraRouteIp -> {
192 if (isIpv4Address(extraRouteIp)) {
193 ipPrefix = extraRouteIp + NwConstants.IPV4PREFIX;
195 ipPrefix = extraRouteIp + NwConstants.IPV6PREFIX;
197 prefixIpListLocal.add(ipPrefix);
199 prefixIpList = prefixIpListLocal;
202 prefixIpList = Collections.singletonList(vrfEntry.getDestPrefix());
203 if (vrfEntry.getEncapType() == VrfEntry.EncapType.Mplsgre) {
204 tunnelType = TunnelTypeMplsOverGre.class;
206 tunnelType = TunnelTypeVxlan.class;
210 for (String prefixIp : prefixIpList) {
211 if (routePaths == null || routePaths.isEmpty()) {
212 LOG.trace("Processing Destination IP {} without NextHop IP", prefixIp);
213 AdjacencyResult adjacencyResult = nextHopManager.getRemoteNextHopPointer(remoteDpnId, vpnId,
214 prefixIp, null, tunnelType);
215 addAdjacencyResultToList(adjacencyList, adjacencyResult);
218 adjacencyList.addAll(routePaths.stream()
220 LOG.debug("NextHop IP for destination {} is {}", prefixIp,
221 routePath.getNexthopAddress());
222 return nextHopManager.getRemoteNextHopPointer(remoteDpnId, vpnId,
223 prefixIp, routePath.getNexthopAddress(), tunnelType);
225 .filter(adjacencyResult -> adjacencyResult != null && !adjacencyList.contains(adjacencyResult))
229 } catch (NullPointerException e) {
232 return adjacencyList;
235 protected void makeConnectedRoute(BigInteger dpId, long vpnId, VrfEntry vrfEntry, String rd,
236 @Nullable List<InstructionInfo> instructions, int addOrRemove,
237 WriteTransaction tx, @Nullable List<SubTransaction> subTxns) {
239 ListenableFutures.addErrorLogging(txRunner.callWithNewWriteOnlyTransactionAndSubmit(
240 newTx -> makeConnectedRoute(dpId, vpnId, vrfEntry, rd, instructions, addOrRemove, newTx, subTxns)),
241 LOG, "Error making connected route");
245 LOG.trace("makeConnectedRoute: vrfEntry {}", vrfEntry);
246 String[] values = vrfEntry.getDestPrefix().split("/");
247 String ipAddress = values[0];
248 int prefixLength = values.length == 1 ? 0 : Integer.parseInt(values[1]);
249 if (addOrRemove == NwConstants.ADD_FLOW) {
250 LOG.debug("Adding route to DPN {} for rd {} prefix {} ", dpId, rd, vrfEntry.getDestPrefix());
252 LOG.debug("Removing route from DPN {} for rd {} prefix {}", dpId, rd, vrfEntry.getDestPrefix());
254 InetAddress destPrefix;
256 destPrefix = InetAddress.getByName(ipAddress);
257 } catch (UnknownHostException e) {
258 LOG.error("Failed to get destPrefix for prefix {} rd {} VpnId {} DPN {}",
259 vrfEntry.getDestPrefix(), rd, vpnId, dpId, e);
263 List<MatchInfo> matches = new ArrayList<>();
265 matches.add(new MatchMetadata(MetaDataUtil.getVpnIdMetadata(vpnId), MetaDataUtil.METADATA_MASK_VRFID));
267 if (destPrefix instanceof Inet4Address) {
268 matches.add(MatchEthernetType.IPV4);
269 if (prefixLength != 0) {
270 matches.add(new MatchIpv4Destination(destPrefix.getHostAddress(), Integer.toString(prefixLength)));
273 matches.add(MatchEthernetType.IPV6);
274 if (prefixLength != 0) {
275 matches.add(new MatchIpv6Destination(destPrefix.getHostAddress() + "/" + prefixLength));
279 int priority = DEFAULT_FIB_FLOW_PRIORITY + prefixLength;
280 String flowRef = FibUtil.getFlowRef(dpId, NwConstants.L3_FIB_TABLE, rd, priority, destPrefix);
281 FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpId, NwConstants.L3_FIB_TABLE, flowRef, priority,
283 COOKIE_VM_FIB_TABLE, matches, instructions);
284 Flow flow = flowEntity.getFlowBuilder().build();
285 String flowId = flowEntity.getFlowId();
286 FlowKey flowKey = new FlowKey(new FlowId(flowId));
287 Node nodeDpn = FibUtil.buildDpnNode(dpId);
289 InstanceIdentifier<Flow> flowInstanceId = InstanceIdentifier.builder(Nodes.class)
290 .child(Node.class, nodeDpn.key()).augmentation(FlowCapableNode.class)
291 .child(Table.class, new TableKey(flow.getTableId())).child(Flow.class, flowKey).build();
293 if (RouteOrigin.value(vrfEntry.getOrigin()) == RouteOrigin.BGP) {
294 SubTransaction subTransaction = new SubTransactionImpl();
295 if (addOrRemove == NwConstants.ADD_FLOW) {
296 subTransaction.setInstanceIdentifier(flowInstanceId);
297 subTransaction.setInstance(flow);
298 subTransaction.setAction(SubTransaction.CREATE);
300 subTransaction.setInstanceIdentifier(flowInstanceId);
301 subTransaction.setAction(SubTransaction.DELETE);
303 subTxns.add(subTransaction);
306 if (addOrRemove == NwConstants.ADD_FLOW) {
307 tx.put(LogicalDatastoreType.CONFIGURATION, flowInstanceId, flow, true);
309 tx.delete(LogicalDatastoreType.CONFIGURATION, flowInstanceId);
313 protected void addRewriteDstMacAction(long vpnId, VrfEntry vrfEntry, @Nullable Prefixes prefixInfo,
314 List<ActionInfo> actionInfos) {
315 if (vrfEntry.getMac() != null) {
316 actionInfos.add(new ActionSetFieldEthernetDestination(actionInfos.size(),
317 new MacAddress(vrfEntry.getMac())));
320 if (prefixInfo == null) {
321 prefixInfo = fibUtil.getPrefixToInterface(vpnId, vrfEntry.getDestPrefix());
322 //Checking PrefixtoInterface again as it is populated later in some cases
323 if (prefixInfo == null) {
324 LOG.debug("No prefix info found for prefix {}", vrfEntry.getDestPrefix());
328 String ipPrefix = prefixInfo.getIpAddress();
329 String ifName = prefixInfo.getVpnInterfaceName();
330 if (ifName == null) {
331 LOG.debug("Failed to get VPN interface for prefix {}", ipPrefix);
334 String vpnName = fibUtil.getVpnNameFromId(vpnId);
335 if (vpnName == null) {
336 LOG.debug("Failed to get VPN name for vpnId {}", vpnId);
339 String macAddress = null;
340 if (vrfEntry.getParentVpnRd() != null) {
341 // Handling iRT/eRT use-case for missing destination mac address in Remote FIB flow
342 Optional<VpnInstanceOpDataEntry> vpnInstance = fibUtil.getVpnInstanceOpData(vrfEntry.getParentVpnRd());
343 if (vpnInstance.isPresent()) {
344 macAddress = fibUtil.getMacAddressFromPrefix(ifName, vpnInstance.get().getVpnInstanceName(), ipPrefix);
346 LOG.warn("VpnInstance missing for Parent Rd {} value for prefix {}", vrfEntry.getParentVpnRd(),
347 vrfEntry.getDestPrefix());
350 macAddress = fibUtil.getMacAddressFromPrefix(ifName, vpnName, ipPrefix);
352 if (macAddress == null) {
353 LOG.warn("No MAC address found for VPN interface {} prefix {}", ifName, ipPrefix);
356 actionInfos.add(new ActionSetFieldEthernetDestination(actionInfos.size(), new MacAddress(macAddress)));
359 protected void addTunnelInterfaceActions(AdjacencyResult adjacencyResult, long vpnId, VrfEntry vrfEntry,
360 List<ActionInfo> actionInfos, String rd) {
361 Class<? extends TunnelTypeBase> tunnelType =
362 VpnExtraRouteHelper.getTunnelType(nextHopManager.getItmManager(), adjacencyResult.getInterfaceName());
363 if (tunnelType == null) {
364 LOG.debug("Tunnel type not found for vrfEntry {}", vrfEntry);
367 // TODO - For now have added routePath into adjacencyResult so that we know for which
368 // routePath this result is built for. If this is not possible construct a map which does
370 String nextHopIp = adjacencyResult.getNextHopIp();
371 java.util.Optional<Long> optionalLabel = FibUtil.getLabelForNextHop(vrfEntry, nextHopIp);
372 if (!optionalLabel.isPresent()) {
373 LOG.warn("NextHopIp {} not found in vrfEntry {}", nextHopIp, vrfEntry);
376 long label = optionalLabel.get();
377 BigInteger tunnelId = null;
378 Prefixes prefixInfo = null;
379 // FIXME vxlan vni bit set is not working properly with OVS.need to
381 if (tunnelType.equals(TunnelTypeVxlan.class)) {
382 if (FibHelper.isControllerManagedNonSelfImportedRoute(RouteOrigin.value(vrfEntry.getOrigin()))) {
383 prefixInfo = fibUtil.getPrefixToInterface(vpnId, vrfEntry.getDestPrefix());
384 //For extra route, the prefixInfo is fetched from the primary adjacency
385 if (prefixInfo == null) {
386 prefixInfo = fibUtil.getPrefixToInterface(vpnId, adjacencyResult.getPrefix());
389 //Imported Route. Get Prefix Info from parent RD
390 VpnInstanceOpDataEntry parentVpn = fibUtil.getVpnInstance(vrfEntry.getParentVpnRd());
391 prefixInfo = fibUtil.getPrefixToInterface(parentVpn.getVpnId(), adjacencyResult.getPrefix());
393 // Internet VPN VNI will be used as tun_id for NAT use-cases
394 if (Prefixes.PrefixCue.Nat.equals(prefixInfo.getPrefixCue())) {
395 if (vrfEntry.getL3vni() != null && vrfEntry.getL3vni() != 0) {
396 tunnelId = BigInteger.valueOf(vrfEntry.getL3vni());
399 if (FibUtil.isVxlanNetwork(prefixInfo.getNetworkType())) {
400 tunnelId = BigInteger.valueOf(prefixInfo.getSegmentationId());
402 LOG.warn("Network is not of type VXLAN for prefix {}."
403 + "Going with default Lport Tag.", prefixInfo.toString());
404 tunnelId = BigInteger.valueOf(label);
408 tunnelId = BigInteger.valueOf(label);
410 LOG.debug("adding set tunnel id action for label {}", label);
411 actionInfos.add(new ActionSetFieldTunnelId(tunnelId));
412 addRewriteDstMacAction(vpnId, vrfEntry, prefixInfo, actionInfos);
416 private InstanceIdentifier<Interface> getFirstAbsentInterfaceStateIid(List<AdjacencyResult> adjacencyResults) {
417 InstanceIdentifier<Interface> res = null;
418 for (AdjacencyResult adjacencyResult : adjacencyResults) {
419 String interfaceName = adjacencyResult.getInterfaceName();
420 if (null == fibUtil.getInterfaceStateFromOperDS(interfaceName)) {
421 res = fibUtil.buildStateInterfaceId(interfaceName);
429 public void programRemoteFib(final BigInteger remoteDpnId, final long vpnId,
430 final VrfEntry vrfEntry, WriteTransaction tx, String rd,
431 List<AdjacencyResult> adjacencyResults,
432 @Nullable List<SubTransaction> subTxns) {
433 if (upgradeState.isUpgradeInProgress()) {
434 InstanceIdentifier<Interface> absentInterfaceStateIid = getFirstAbsentInterfaceStateIid(adjacencyResults);
435 if (absentInterfaceStateIid != null) {
436 LOG.info("programRemoteFib: interface state for {} not yet present, waiting...",
437 absentInterfaceStateIid);
438 eventCallbacks.onAddOrUpdate(LogicalDatastoreType.OPERATIONAL,
439 absentInterfaceStateIid,
441 LOG.info("programRemoteFib: waited for and got interface state {}", absentInterfaceStateIid);
442 txRunner.callWithNewWriteOnlyTransactionAndSubmit(
443 (wtx) -> programRemoteFib(remoteDpnId, vpnId, vrfEntry, wtx, rd, adjacencyResults, null));
444 return DataTreeEventCallbackRegistrar.NextAction.UNREGISTER;
446 Duration.of(15, ChronoUnit.MINUTES),
448 LOG.error("programRemoteFib: timed out waiting for {}", absentInterfaceStateIid);
449 txRunner.callWithNewWriteOnlyTransactionAndSubmit(
450 (wtx) -> programRemoteFib(remoteDpnId, vpnId, vrfEntry, wtx, rd, adjacencyResults, null));
456 List<InstructionInfo> instructions = new ArrayList<>();
457 for (AdjacencyResult adjacencyResult : adjacencyResults) {
458 List<ActionInfo> actionInfos = new ArrayList<>();
459 String egressInterface = adjacencyResult.getInterfaceName();
460 if (FibUtil.isTunnelInterface(adjacencyResult)) {
461 addTunnelInterfaceActions(adjacencyResult, vpnId, vrfEntry, actionInfos, rd);
463 addRewriteDstMacAction(vpnId, vrfEntry, null, actionInfos);
465 List<ActionInfo> egressActions = nextHopManager.getEgressActionsForInterface(egressInterface,
466 actionInfos.size(), true);
467 if (egressActions.isEmpty()) {
469 "Failed to retrieve egress action for prefix {} route-paths {} interface {}. "
470 + "Aborting remote FIB entry creation.",
471 vrfEntry.getDestPrefix(), vrfEntry.getRoutePaths(), egressInterface);
474 actionInfos.addAll(egressActions);
475 instructions.add(new InstructionApplyActions(actionInfos));
477 makeConnectedRoute(remoteDpnId, vpnId, vrfEntry, rd, instructions, NwConstants.ADD_FLOW, tx, subTxns);
480 public boolean checkDpnDeleteFibEntry(VpnNexthop localNextHopInfo, BigInteger remoteDpnId, long vpnId,
481 VrfEntry vrfEntry, String rd,
482 WriteTransaction tx, @Nullable List<SubTransaction> subTxns) {
483 boolean isRemoteRoute = true;
484 if (localNextHopInfo != null) {
485 isRemoteRoute = !remoteDpnId.equals(localNextHopInfo.getDpnId());
488 makeConnectedRoute(remoteDpnId, vpnId, vrfEntry, rd, null, NwConstants.DEL_FLOW, tx, subTxns);
489 LOG.debug("Successfully delete FIB entry: vrfEntry={}, vpnId={}", vrfEntry.getDestPrefix(), vpnId);
492 LOG.debug("Did not delete FIB entry: rd={}, vrfEntry={}, as it is local to dpnId={}",
493 rd, vrfEntry.getDestPrefix(), remoteDpnId);
498 public void deleteRemoteRoute(@Nullable final BigInteger localDpnId, final BigInteger remoteDpnId,
499 final long vpnId, final VrfTablesKey vrfTableKey,
500 final VrfEntry vrfEntry, Optional<Routes> extraRouteOptional,
501 @Nullable WriteTransaction tx) {
503 ListenableFutures.addErrorLogging(txRunner.callWithNewWriteOnlyTransactionAndSubmit(
504 newTx -> deleteRemoteRoute(localDpnId, remoteDpnId, vpnId, vrfTableKey, vrfEntry,
505 extraRouteOptional, newTx)), LOG, "Error deleting remote route");
509 LOG.debug("deleting remote route: prefix={}, vpnId={} localDpnId {} remoteDpnId {}",
510 vrfEntry.getDestPrefix(), vpnId, localDpnId, remoteDpnId);
511 String rd = vrfTableKey.getRouteDistinguisher();
513 if (localDpnId != null && localDpnId != BigInteger.ZERO) {
514 // localDpnId is not known when clean up happens for last vm for a vpn on a dpn
515 if (extraRouteOptional.isPresent()) {
516 nextHopManager.deleteLoadBalancingNextHop(vpnId, remoteDpnId, vrfEntry.getDestPrefix());
518 makeConnectedRoute(remoteDpnId, vpnId, vrfEntry, rd, null, NwConstants.DEL_FLOW, tx, null);
519 LOG.debug("Successfully delete FIB entry: vrfEntry={}, vpnId={}", vrfEntry.getDestPrefix(), vpnId);
523 // below two reads are kept as is, until best way is found to identify dpnID
524 VpnNexthop localNextHopInfo = nextHopManager.getVpnNexthop(vpnId, vrfEntry.getDestPrefix());
525 if (extraRouteOptional.isPresent()) {
526 nextHopManager.deleteLoadBalancingNextHop(vpnId, remoteDpnId, vrfEntry.getDestPrefix());
528 checkDpnDeleteFibEntry(localNextHopInfo, remoteDpnId, vpnId, vrfEntry, rd, tx, null);
532 public static InstanceIdentifier<Routes> getVpnToExtrarouteIdentifier(String vpnName, String vrfId,
534 return InstanceIdentifier.builder(VpnToExtraroutes.class)
535 .child(Vpn.class, new VpnKey(vpnName)).child(ExtraRoutes.class,
536 new ExtraRoutesKey(vrfId)).child(Routes.class, new RoutesKey(ipPrefix)).build();
540 public Routes getVpnToExtraroute(Long vpnId, String vpnRd, String destPrefix) {
541 String optVpnName = fibUtil.getVpnNameFromId(vpnId);
542 if (optVpnName != null) {
543 InstanceIdentifier<Routes> vpnExtraRoutesId = getVpnToExtrarouteIdentifier(
544 optVpnName, vpnRd, destPrefix);
545 return MDSALUtil.read(dataBroker, LogicalDatastoreType.OPERATIONAL, vpnExtraRoutesId).orNull();
550 public FlowEntity buildL3vpnGatewayFlow(BigInteger dpId, String gwMacAddress, long vpnId) {
551 List<MatchInfo> mkMatches = new ArrayList<>();
552 mkMatches.add(new MatchMetadata(MetaDataUtil.getVpnIdMetadata(vpnId), MetaDataUtil.METADATA_MASK_VRFID));
553 mkMatches.add(new MatchEthernetDestination(new MacAddress(gwMacAddress)));
554 List<InstructionInfo> mkInstructions = new ArrayList<>();
555 mkInstructions.add(new InstructionGotoTable(NwConstants.L3_FIB_TABLE));
556 String flowId = FibUtil.getL3VpnGatewayFlowRef(NwConstants.L3_GW_MAC_TABLE, dpId, vpnId, gwMacAddress);
557 return MDSALUtil.buildFlowEntity(dpId, NwConstants.L3_GW_MAC_TABLE,
558 flowId, 20, flowId, 0, 0, NwConstants.COOKIE_L3_GW_MAC_TABLE, mkMatches, mkInstructions);
561 public void installPingResponderFlowEntry(BigInteger dpnId, long vpnId, String routerInternalIp,
562 MacAddress routerMac, long label, int addOrRemove) {
564 List<MatchInfo> matches = new ArrayList<>();
565 matches.add(MatchIpProtocol.ICMP);
566 matches.add(new MatchMetadata(MetaDataUtil.getVpnIdMetadata(vpnId), MetaDataUtil.METADATA_MASK_VRFID));
567 matches.add(new MatchIcmpv4((short) 8, (short) 0));
568 matches.add(MatchEthernetType.IPV4);
569 matches.add(new MatchIpv4Destination(routerInternalIp, "32"));
571 List<ActionInfo> actionsInfos = new ArrayList<>();
573 // Set Eth Src and Eth Dst
574 actionsInfos.add(new ActionMoveSourceDestinationEth());
575 actionsInfos.add(new ActionSetFieldEthernetSource(routerMac));
577 // Move Ip Src to Ip Dst
578 actionsInfos.add(new ActionMoveSourceDestinationIp());
579 actionsInfos.add(new ActionSetSourceIp(routerInternalIp, "32"));
581 // Set the ICMP type to 0 (echo reply)
582 actionsInfos.add(new ActionSetIcmpType((short) 0));
584 actionsInfos.add(new ActionNxLoadInPort(BigInteger.ZERO));
586 actionsInfos.add(new ActionNxResubmit(NwConstants.L3_FIB_TABLE));
588 List<InstructionInfo> instructions = new ArrayList<>();
590 instructions.add(new InstructionApplyActions(actionsInfos));
592 int priority = FibConstants.DEFAULT_FIB_FLOW_PRIORITY + FibConstants.DEFAULT_PREFIX_LENGTH;
593 String flowRef = FibUtil.getFlowRef(dpnId, NwConstants.L3_FIB_TABLE, label, priority);
595 FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpnId, NwConstants.L3_FIB_TABLE, flowRef, priority, flowRef,
596 0, 0, NwConstants.COOKIE_VM_FIB_TABLE, matches, instructions);
598 if (addOrRemove == NwConstants.ADD_FLOW) {
599 mdsalManager.syncInstallFlow(flowEntity);
601 mdsalManager.syncRemoveFlow(flowEntity);