2 * Copyright (c) 2016 Hewlett Packard Enterprise, Co. 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
9 package org.opendaylight.netvirt.neutronvpn;
11 import com.google.common.util.concurrent.JdkFutureAdapters;
12 import com.google.common.util.concurrent.ThreadFactoryBuilder;
13 import java.math.BigInteger;
14 import java.util.ArrayList;
15 import java.util.Collections;
16 import java.util.List;
17 import java.util.concurrent.ExecutionException;
18 import java.util.concurrent.Executors;
19 import java.util.concurrent.Future;
20 import java.util.concurrent.ScheduledExecutorService;
21 import java.util.concurrent.TimeUnit;
22 import javax.annotation.Nullable;
23 import javax.annotation.PostConstruct;
24 import javax.annotation.PreDestroy;
25 import javax.inject.Inject;
26 import javax.inject.Singleton;
27 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
28 import org.opendaylight.genius.arputil.api.ArpConstants;
29 import org.opendaylight.genius.mdsalutil.NWUtil;
30 import org.opendaylight.infrautils.utils.concurrent.ListenableFutures;
31 import org.opendaylight.netvirt.elanmanager.api.IElanService;
32 import org.opendaylight.netvirt.vpnmanager.api.ICentralizedSwitchProvider;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Address;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.arputil.rev160406.OdlArputilService;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.arputil.rev160406.SendArpRequestInput;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.arputil.rev160406.SendArpRequestInputBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.arputil.rev160406.interfaces.InterfaceAddress;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.arputil.rev160406.interfaces.InterfaceAddressBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.ipv6.nd.util.rev170210.Ipv6NdUtilService;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.ipv6.nd.util.rev170210.SendNeighborSolicitationInput;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.ipv6.nd.util.rev170210.SendNeighborSolicitationInputBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.ipv6.nd.util.rev170210.SendNeighborSolicitationOutput;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.routers.attributes.routers.Router;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.routers.attributes.routers.router.ExternalGatewayInfo;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.port.attributes.FixedIps;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.ports.Port;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.subnets.Subnet;
52 import org.opendaylight.yangtools.yang.common.RpcResult;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
57 public class NeutronSubnetGwMacResolver {
58 private static final Logger LOG = LoggerFactory.getLogger(NeutronSubnetGwMacResolver.class);
59 private static final long L3_INSTALL_DELAY_MILLIS = 5000;
61 private final DataBroker broker;
62 private final OdlArputilService arpUtilService;
63 private final IElanService elanService;
64 private final ICentralizedSwitchProvider cswitchProvider;
65 private final NeutronvpnUtils neutronvpnUtils;
66 private final ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor(
67 new ThreadFactoryBuilder().setNameFormat("Gw-Mac-Res").build());
68 private final Ipv6NdUtilService ipv6NdUtilService;
71 public NeutronSubnetGwMacResolver(final DataBroker broker,
72 final OdlArputilService arputilService, final IElanService elanService,
73 final ICentralizedSwitchProvider cswitchProvider, final NeutronvpnUtils neutronvpnUtils,
74 final Ipv6NdUtilService ipv6NdUtilService) {
76 this.arpUtilService = arputilService;
77 this.elanService = elanService;
78 this.cswitchProvider = cswitchProvider;
79 this.neutronvpnUtils = neutronvpnUtils;
80 this.ipv6NdUtilService = ipv6NdUtilService;
83 // TODO Clean up the exception handling
84 @SuppressWarnings("checkstyle:IllegalCatch")
87 LOG.info("{} init", getClass().getSimpleName());
89 executorService.scheduleAtFixedRate(() -> {
91 sendArpRequestsToExtGateways();
92 } catch (Exception e) {
93 LOG.warn("Failed to send ARP request to GW ips", e);
95 }, 0, ArpConstants.ARP_CACHE_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
100 public void close() {
101 executorService.shutdownNow();
104 public void sendArpRequestsToExtGateways(Router router) {
105 // Let the FIB flows a chance to be installed
106 // otherwise the ARP response will be routed straight to L2
107 // and bypasses L3 arp cache
108 executorService.schedule(() -> sendArpRequestsToExtGatewayTask(router), L3_INSTALL_DELAY_MILLIS,
109 TimeUnit.MILLISECONDS);
112 public void sendArpRequestsToExtGateways() {
113 LOG.trace("Sending ARP requests to external gateways");
114 for (Router router : neutronvpnUtils.getAllRouters()) {
115 sendArpRequestsToExtGateways(router);
119 private void sendArpRequestsToExtGatewayTask(Router router) {
120 LOG.trace("Send ARP requests to external GW for router {}", router);
121 Port extPort = getRouterExtGatewayPort(router);
122 if (extPort == null) {
123 LOG.trace("External GW port for router {} is missing", router.getUuid().getValue());
127 String extInterface = getExternalInterface(router);
128 if (extInterface == null) {
129 LOG.trace("No external interface defined for router {}", router.getUuid().getValue());
133 List<FixedIps> fixedIps = extPort.getFixedIps();
134 if (fixedIps == null || fixedIps.isEmpty()) {
135 LOG.trace("External GW port {} for router {} has no fixed IPs", extPort.getUuid().getValue(),
136 router.getUuid().getValue());
140 MacAddress macAddress = extPort.getMacAddress();
141 if (macAddress == null) {
142 LOG.trace("External GW port {} for router {} has no mac address", extPort.getUuid().getValue(),
143 router.getUuid().getValue());
147 for (FixedIps fixIp : fixedIps) {
148 Uuid subnetId = fixIp.getSubnetId();
149 IpAddress srcIpAddress = fixIp.getIpAddress();
150 IpAddress dstIpAddress = getExternalGwIpAddress(subnetId);
151 String srcIpAddressString = srcIpAddress.stringValue();
152 String dstIpAddressString = dstIpAddress.stringValue();
153 if (NWUtil.isIpv4Address(srcIpAddressString)) {
154 sendArpRequest(srcIpAddress, dstIpAddress, macAddress, extInterface);
156 sendNeighborSolication(new Ipv6Address(srcIpAddressString),macAddress,
157 new Ipv6Address(dstIpAddressString), extInterface);
163 // TODO Clean up the exception handling
164 @SuppressWarnings("checkstyle:IllegalCatch")
165 private void sendArpRequest(IpAddress srcIpAddress, IpAddress dstIpAddress, MacAddress srcMacAddress,
166 String interfaceName) {
167 if (srcIpAddress == null || dstIpAddress == null) {
168 LOG.trace("Skip sending ARP to external GW srcIp {} dstIp {}", srcIpAddress, dstIpAddress);
172 PhysAddress srcMacPhysAddress = new PhysAddress(srcMacAddress.getValue());
174 InterfaceAddress interfaceAddress = new InterfaceAddressBuilder().setInterface(interfaceName)
175 .setIpAddress(srcIpAddress).setMacaddress(srcMacPhysAddress).build();
177 SendArpRequestInput sendArpRequestInput = new SendArpRequestInputBuilder().setIpaddress(dstIpAddress)
178 .setInterfaceAddress(Collections.singletonList(interfaceAddress)).build();
180 ListenableFutures.addErrorLogging(JdkFutureAdapters.listenInPoolThread(
181 arpUtilService.sendArpRequest(sendArpRequestInput)), LOG, "Send ARP request");
182 } catch (Exception e) {
183 LOG.error("Failed to send ARP request to external GW {} from interface {}",
184 dstIpAddress.getIpv4Address().getValue(), interfaceName, e);
188 private void sendNeighborSolication(Ipv6Address srcIpv6Address,
189 MacAddress srcMac, Ipv6Address dstIpv6Address, String interfaceName) {
190 List<org.opendaylight.yang.gen.v1.urn.opendaylight.genius.ipv6
191 .nd.util.rev170210.interfaces.InterfaceAddress> interfaceAddresses = new ArrayList<>();
192 interfaceAddresses.add(new org.opendaylight.yang.gen.v1.urn.opendaylight.genius.ipv6
193 .nd.util.rev170210.interfaces.InterfaceAddressBuilder()
194 .setInterface(interfaceName)
195 .setSrcIpAddress(srcIpv6Address)
196 .setSrcMacAddress(new PhysAddress(srcMac.getValue())).build());
197 SendNeighborSolicitationInput input = new SendNeighborSolicitationInputBuilder()
198 .setInterfaceAddress(interfaceAddresses).setTargetIpAddress(dstIpv6Address)
201 Future<RpcResult<SendNeighborSolicitationOutput>> result = ipv6NdUtilService
202 .sendNeighborSolicitation(input);
203 RpcResult<SendNeighborSolicitationOutput> rpcResult = result.get();
204 if (!rpcResult.isSuccessful()) {
205 LOG.error("sendNeighborSolicitationToOfGroup: RPC Call failed for input={} and Errors={}", input,
206 rpcResult.getErrors());
208 } catch (InterruptedException | ExecutionException e) {
209 LOG.error("Failed to send NS packet to ELAN group, input={}", input, e);
214 private Port getRouterExtGatewayPort(Router router) {
215 if (router == null) {
216 LOG.trace("Router is null");
220 Uuid extPortId = router.getGatewayPortId();
221 if (extPortId == null) {
222 LOG.trace("Router {} is not associated with any external GW port", router.getUuid().getValue());
226 return neutronvpnUtils.getNeutronPort(extPortId);
230 private String getExternalInterface(Router router) {
231 ExternalGatewayInfo extGatewayInfo = router.getExternalGatewayInfo();
232 String routerName = router.getUuid().getValue();
233 if (extGatewayInfo == null) {
234 LOG.warn("External GW info missing for router {}", routerName);
238 Uuid extNetworkId = extGatewayInfo.getExternalNetworkId();
239 if (extNetworkId == null) {
240 LOG.warn("External network id missing for router {}", routerName);
244 BigInteger primarySwitch = cswitchProvider.getPrimarySwitchForRouter(routerName);
245 if (primarySwitch == null || BigInteger.ZERO.equals(primarySwitch)) {
246 LOG.warn("Primary switch has not been allocated for router {}", routerName);
250 return elanService.getExternalElanInterface(extNetworkId.getValue(), primarySwitch);
254 private IpAddress getExternalGwIpAddress(Uuid subnetId) {
255 if (subnetId == null) {
256 LOG.error("Subnet id is null");
260 Subnet subnet = neutronvpnUtils.getNeutronSubnet(subnetId);
261 return subnet != null ? subnet.getGatewayIp() : null;