2 * Copyright (c) 2014, 2015 Red Hat, Inc. 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.ovsdb.openstack.netvirt.impl;
11 import org.apache.commons.lang3.tuple.ImmutablePair;
12 import org.apache.commons.lang3.tuple.Pair;
13 import org.opendaylight.neutron.spi.INeutronNetworkCRUD;
14 import org.opendaylight.neutron.spi.INeutronPortCRUD;
15 import org.opendaylight.neutron.spi.INeutronSubnetCRUD;
16 import org.opendaylight.neutron.spi.NeutronFloatingIP;
17 import org.opendaylight.neutron.spi.NeutronNetwork;
18 import org.opendaylight.neutron.spi.NeutronPort;
19 import org.opendaylight.neutron.spi.NeutronRouter;
20 import org.opendaylight.neutron.spi.NeutronRouter_Interface;
21 import org.opendaylight.neutron.spi.NeutronSubnet;
22 import org.opendaylight.neutron.spi.Neutron_IPs;
23 import org.opendaylight.ovsdb.openstack.netvirt.ConfigInterface;
24 import org.opendaylight.ovsdb.openstack.netvirt.api.*;
25 import org.opendaylight.ovsdb.utils.servicehelper.ServiceHelper;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbTerminationPointAugmentation;
30 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
32 import com.google.common.base.Preconditions;
33 import com.google.common.util.concurrent.FutureCallback;
34 import com.google.common.util.concurrent.Futures;
35 import com.google.common.util.concurrent.ListenableFuture;
37 import org.osgi.framework.BundleContext;
38 import org.osgi.framework.ServiceReference;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
42 import java.net.InetAddress;
43 import java.net.UnknownHostException;
44 import java.util.HashMap;
45 import java.util.HashSet;
46 import java.util.ArrayList;
47 import java.util.List;
50 import java.util.concurrent.ExecutorService;
51 import java.util.concurrent.Executors;
54 * Neutron L3 Adapter implements a hub-like adapter for the various Neutron events. Based on
55 * these events, the abstract router callbacks can be generated to the multi-tenant aware router,
56 * as well as the multi-tenant router forwarding provider.
58 public class NeutronL3Adapter implements ConfigInterface {
59 private static final Logger LOG = LoggerFactory.getLogger(NeutronL3Adapter.class);
61 // The implementation for each of these services is resolved by the OSGi Service Manager
62 private volatile ConfigurationService configurationService;
63 private volatile TenantNetworkManager tenantNetworkManager;
64 private volatile NodeCacheManager nodeCacheManager;
65 private volatile INeutronNetworkCRUD neutronNetworkCache;
66 private volatile INeutronSubnetCRUD neutronSubnetCache;
67 private volatile INeutronPortCRUD neutronPortCache;
68 private volatile L3ForwardingProvider l3ForwardingProvider;
69 private volatile InboundNatProvider inboundNatProvider;
70 private volatile OutboundNatProvider outboundNatProvider;
71 private volatile ArpProvider arpProvider;
72 private volatile RoutingProvider routingProvider;
73 private volatile GatewayMacResolver gatewayMacResolver;
75 private class FloatIpData {
76 private final Long dpid; // br-int of node where floating ip is associated with tenant port
77 private final Long ofPort; // patch port in br-int used to reach br-ex
78 private final String segId; // segmentation id of the net where fixed ip is instantiated
79 private final String macAddress; // mac address assigned to neutron port of floating ip
80 private final String floatingIpAddress;
81 private final String fixedIpAddress; // ip address given to tenant vm
82 private final String neutronRouterMac;
84 FloatIpData(final Long dpid, final Long ofPort, final String segId, final String macAddress,
85 final String floatingIpAddress, final String fixedIpAddress, final String neutronRouterMac) {
89 this.macAddress = macAddress;
90 this.floatingIpAddress = floatingIpAddress;
91 this.fixedIpAddress = fixedIpAddress;
92 this.neutronRouterMac = neutronRouterMac;
96 private Set<String> inboundIpRewriteCache;
97 private Set<String> outboundIpRewriteCache;
98 private Set<String> outboundIpRewriteExclusionCache;
99 private Set<String> routerInterfacesCache;
100 private Set<String> staticArpEntryCache;
101 private Set<String> l3ForwardingCache;
102 private Map<String, String> networkIdToRouterMacCache;
103 private Map<String, List<Neutron_IPs>> networkIdToRouterIpListCache;
104 private Map<String, NeutronRouter_Interface> subnetIdToRouterInterfaceCache;
105 private Map<String, Pair<Long, Uuid>> neutronPortToDpIdCache;
106 private Map<String, FloatIpData> floatIpDataMapCache;
107 private String externalRouterMac;
108 private Boolean enabled = false;
109 private Boolean flgDistributedARPEnabled = true;
110 private Southbound southbound;
111 private final ExecutorService gatewayMacResolverPool = Executors.newFixedThreadPool(5);
113 private static final String OWNER_ROUTER_INTERFACE = "network:router_interface";
114 private static final String OWNER_ROUTER_INTERFACE_DISTRIBUTED = "network:router_interface_distributed";
115 private static final String OWNER_ROUTER_GATEWAY = "network:router_gateway";
116 private static final String OWNER_FLOATING_IP = "network:floatingip";
117 private static final String DEFAULT_EXT_RTR_MAC = "00:00:5E:00:01:01";
119 public NeutronL3Adapter() {
120 LOG.info(">>>>>> NeutronL3Adapter constructor {}", this.getClass());
123 private void initL3AdapterMembers() {
124 Preconditions.checkNotNull(configurationService);
126 if (configurationService.isL3ForwardingEnabled()) {
127 this.inboundIpRewriteCache = new HashSet<>();
128 this.outboundIpRewriteCache = new HashSet<>();
129 this.outboundIpRewriteExclusionCache = new HashSet<>();
130 this.routerInterfacesCache = new HashSet<>();
131 this.staticArpEntryCache = new HashSet<>();
132 this.l3ForwardingCache = new HashSet<>();
133 this.networkIdToRouterMacCache = new HashMap<>();
134 this.networkIdToRouterIpListCache = new HashMap<>();
135 this.subnetIdToRouterInterfaceCache = new HashMap<>();
136 this.neutronPortToDpIdCache = new HashMap<>();
137 this.floatIpDataMapCache = new HashMap<>();
139 this.externalRouterMac = configurationService.getDefaultGatewayMacAddress(null);
140 if (this.externalRouterMac == null) {
141 this.externalRouterMac = DEFAULT_EXT_RTR_MAC;
145 LOG.info("OVSDB L3 forwarding is enabled");
146 if (configurationService.isDistributedArpDisabled()) {
147 this.flgDistributedARPEnabled = false;
148 LOG.info("Distributed ARP responder is disabled");
150 LOG.debug("Distributed ARP responder is enabled");
153 LOG.debug("OVSDB L3 forwarding is disabled");
158 // Callbacks from OVSDB's northbound handlers
162 * Invoked to configure the mac address for the external gateway in br-ex. ovsdb netvirt needs help in getting
163 * mac for given ip in br-ex (bug 3378). For example, since ovsdb has no real arp, it needs a service in can
164 * subscribe so that the mac address associated to the gateway ip address is available.
166 * @param externalRouterMacUpdate The mac address to be associated to the gateway.
168 public void updateExternalRouterMac(final String externalRouterMacUpdate) {
169 Preconditions.checkNotNull(externalRouterMacUpdate);
171 flushExistingIpRewrite();
172 this.externalRouterMac = externalRouterMacUpdate;
173 rebuildExistingIpRewrite();
179 * @param action the {@link org.opendaylight.ovsdb.openstack.netvirt.api.Action} action to be handled.
180 * @param subnet An instance of NeutronSubnet object.
182 public void handleNeutronSubnetEvent(final NeutronSubnet subnet, Action action) {
183 LOG.debug("Neutron subnet {} event : {}", action, subnet.toString());
187 * Process the port event as a router interface event.
188 * For a not delete action, since a port is only create when the tennat uses the subnet, it is required to
189 * verify if all routers across all nodes have the interface for the port's subnet(s) configured.
191 * @param action the {@link org.opendaylight.ovsdb.openstack.netvirt.api.Action} action to be handled.
192 * @param neutronPort An instance of NeutronPort object.
194 public void handleNeutronPortEvent(final NeutronPort neutronPort, Action action) {
195 LOG.debug("Neutron port {} event : {}", action, neutronPort.toString());
200 final boolean isDelete = action == Action.DELETE;
202 if (neutronPort.getDeviceOwner().equalsIgnoreCase(OWNER_ROUTER_GATEWAY)){
204 Node externalBridgeNode = getExternalBridgeNode();
205 if(externalBridgeNode != null){
206 LOG.info("Port {} is network router gateway interface, "
207 + "triggering gateway resolution for the attached external network on node {}", neutronPort, externalBridgeNode);
208 this.triggerGatewayMacResolver(externalBridgeNode, neutronPort);
210 LOG.error("Did not find Node that has external bridge (br-ex), Gateway resolution failed");
213 NeutronNetwork externalNetwork = neutronNetworkCache.getNetwork(neutronPort.getNetworkUUID());
215 if(externalNetwork != null){
216 if(externalNetwork.isRouterExternal()){
217 final NeutronSubnet externalSubnet = getExternalNetworkSubnet(neutronPort);
218 if(externalSubnet != null){
219 gatewayMacResolver.stopPeriodicReferesh(new Ipv4Address(externalSubnet.getGatewayIP()));
226 // Treat the port event as a router interface event if the port belongs to router. This is a
227 // helper for handling cases when handleNeutronRouterInterfaceEvent is not available
229 if (neutronPort.getDeviceOwner().equalsIgnoreCase(OWNER_ROUTER_INTERFACE) ||
230 neutronPort.getDeviceOwner().equalsIgnoreCase(OWNER_ROUTER_INTERFACE_DISTRIBUTED)) {
232 for (Neutron_IPs neutronIP : neutronPort.getFixedIPs()) {
233 NeutronRouter_Interface neutronRouterInterface =
234 new NeutronRouter_Interface(neutronIP.getSubnetUUID(), neutronPort.getPortUUID());
235 neutronRouterInterface.setID(neutronIP.getSubnetUUID()); // id of router interface to be same as subnet
236 neutronRouterInterface.setTenantID(neutronPort.getTenantID());
238 this.handleNeutronRouterInterfaceEvent(null /*neutronRouter*/, neutronRouterInterface, action);
241 // We made it here, port is not used as a router interface. If this is not a delete action, make sure that
242 // all nodes that are supposed to have a router interface for the port's subnet(s), have it configured. We
243 // need to do this check here because a router interface is not added to a node until tenant becomes needed
247 for (Neutron_IPs neutronIP : neutronPort.getFixedIPs()) {
248 NeutronRouter_Interface neutronRouterInterface =
249 subnetIdToRouterInterfaceCache.get(neutronIP.getSubnetUUID());
250 if (neutronRouterInterface != null) {
251 this.handleNeutronRouterInterfaceEvent(null /*neutronRouter*/, neutronRouterInterface, action);
255 this.updateL3ForNeutronPort(neutronPort, isDelete);
262 * @param action the {@link org.opendaylight.ovsdb.openstack.netvirt.api.Action} action to be handled.
263 * @param neutronRouter An instance of NeutronRouter object.
265 public void handleNeutronRouterEvent(final NeutronRouter neutronRouter, Action action) {
266 LOG.debug("Neutron router {} event : {}", action, neutronRouter.toString());
270 * Process the event enforcing actions and verifying dependencies between all router's interface. For example,
271 * delete the ports on the same subnet.
273 * @param action the {@link org.opendaylight.ovsdb.openstack.netvirt.api.Action} action to be handled.
274 * @param neutronRouter An instance of NeutronRouter object.
275 * @param neutronRouterInterface An instance of NeutronRouter_Interface object.
277 public void handleNeutronRouterInterfaceEvent(final NeutronRouter neutronRouter,
278 final NeutronRouter_Interface neutronRouterInterface,
280 LOG.debug("Router interface {} got event {}. Subnet {}",
281 neutronRouterInterface.getPortUUID(),
283 neutronRouterInterface.getSubnetUUID());
288 final boolean isDelete = action == Action.DELETE;
290 this.programFlowsForNeutronRouterInterface(neutronRouterInterface, isDelete);
292 // As neutron router interface is added/removed, we need to iterate through all the neutron ports and
293 // see if they are affected by l3
295 for (NeutronPort neutronPort : neutronPortCache.getAllPorts()) {
296 boolean currPortShouldBeDeleted = false;
297 // Note: delete in this case only applies to 1)router interface delete and 2)ports on the same subnet
299 for (Neutron_IPs neutronIP : neutronPort.getFixedIPs()) {
300 if (neutronRouterInterface.getSubnetUUID().equalsIgnoreCase(neutronIP.getSubnetUUID())) {
301 currPortShouldBeDeleted = true;
306 this.updateL3ForNeutronPort(neutronPort, currPortShouldBeDeleted);
311 * Invoked when a neutron message regarding the floating ip association is sent to odl via ml2. If the action is
312 * a creation, it will first add ARP rules for the given floating ip and then configure the DNAT (rewrite the
313 * packets from the floating IP address to the internal fixed ip) rules on OpenFlow Table 30 and SNAT rules (other
314 * way around) on OpenFlow Table 100.
316 * @param actionIn the {@link org.opendaylight.ovsdb.openstack.netvirt.api.Action} action to be handled.
317 * @param neutronFloatingIP An {@link org.opendaylight.neutron.spi.NeutronFloatingIP} instance of NeutronFloatingIP object.
319 public void handleNeutronFloatingIPEvent(final NeutronFloatingIP neutronFloatingIP,
321 Preconditions.checkNotNull(neutronFloatingIP);
323 LOG.debug(" Floating IP {} {}<->{}, network uuid {}", actionIn,
324 neutronFloatingIP.getFixedIPAddress(),
325 neutronFloatingIP.getFloatingIPAddress(),
326 neutronFloatingIP.getFloatingNetworkUUID());
333 // Consider action to be delete if getFixedIPAddress is null
335 if (neutronFloatingIP.getFixedIPAddress() == null) {
336 action = Action.DELETE;
341 // this.programFlowsForFloatingIP(neutronFloatingIP, action == Action.DELETE);
343 if (action != Action.DELETE) {
344 programFlowsForFloatingIPArpAdd(neutronFloatingIP); // must be first, as it updates floatIpDataMapCache
346 programFlowsForFloatingIPInbound(neutronFloatingIP, Action.ADD);
347 programFlowsForFloatingIPOutbound(neutronFloatingIP, Action.ADD);
349 programFlowsForFloatingIPOutbound(neutronFloatingIP, Action.DELETE);
350 programFlowsForFloatingIPInbound(neutronFloatingIP, Action.DELETE);
352 programFlowsForFloatingIPArpDelete(neutronFloatingIP.getID()); // must be last, as it updates floatIpDataMapCache
357 * This method performs creation or deletion of in-bound rules into Table 30 for a existing available floating
358 * ip, otherwise for newer one.
360 private void programFlowsForFloatingIPInbound(final NeutronFloatingIP neutronFloatingIP, final Action action) {
361 Preconditions.checkNotNull(neutronFloatingIP);
363 final FloatIpData fid = floatIpDataMapCache.get(neutronFloatingIP.getID());
365 LOG.trace("programFlowsForFloatingIPInboundAdd {} for {} uuid {} not in local cache",
366 action, neutronFloatingIP.getFloatingIPAddress(), neutronFloatingIP.getID());
369 programInboundIpRewriteStage1(fid.dpid, fid.ofPort, fid.segId, fid.floatingIpAddress, fid.fixedIpAddress,
374 * This method performs creation or deletion of out-bound rules into Table 100 for a existing available floating
375 * ip, otherwise for newer one.
377 private void programFlowsForFloatingIPOutbound(final NeutronFloatingIP neutronFloatingIP, final Action action) {
378 Preconditions.checkNotNull(neutronFloatingIP);
380 final FloatIpData fid = floatIpDataMapCache.get(neutronFloatingIP.getID());
382 LOG.trace("programFlowsForFloatingIPOutbound {} for {} uuid {} not in local cache",
383 action, neutronFloatingIP.getFloatingIPAddress(), neutronFloatingIP.getID());
386 programOutboundIpRewriteStage1(fid, action);
389 private void flushExistingIpRewrite() {
390 for (FloatIpData fid : floatIpDataMapCache.values()) {
391 programOutboundIpRewriteStage1(fid, Action.DELETE);
395 private void rebuildExistingIpRewrite() {
396 for (FloatIpData fid : floatIpDataMapCache.values()) {
397 programOutboundIpRewriteStage1(fid, Action.ADD);
402 * This method creates ARP response rules into OpenFlow Table 30 for a given floating ip. In order to connect
403 * to br-ex from br-int, a patch-port is used. Thus, the patch-port will be responsible to respond the ARP
406 private void programFlowsForFloatingIPArpAdd(final NeutronFloatingIP neutronFloatingIP) {
407 Preconditions.checkNotNull(neutronFloatingIP);
408 Preconditions.checkNotNull(neutronFloatingIP.getFixedIPAddress());
409 Preconditions.checkNotNull(neutronFloatingIP.getFloatingIPAddress());
411 if (floatIpDataMapCache.get(neutronFloatingIP.getID()) != null) {
412 LOG.trace("programFlowsForFloatingIPArpAdd for neutronFloatingIP {} uuid {} is already done",
413 neutronFloatingIP.getFloatingIPAddress(), neutronFloatingIP.getID());
417 // find bridge Node where floating ip is configured by looking up cache for its port
418 final NeutronPort neutronPortForFloatIp = findNeutronPortForFloatingIp(neutronFloatingIP.getID());
419 final String neutronTenantPortUuid = neutronFloatingIP.getPortUUID();
420 final Pair<Long, Uuid> nodeIfPair = neutronPortToDpIdCache.get(neutronTenantPortUuid);
421 final String floatingIpMac = neutronPortForFloatIp == null ? null : neutronPortForFloatIp.getMacAddress();
422 final String fixedIpAddress = neutronFloatingIP.getFixedIPAddress();
423 final String floatingIpAddress = neutronFloatingIP.getFloatingIPAddress();
425 final NeutronPort tenantNeutronPort = neutronPortCache.getPort(neutronTenantPortUuid);
426 final NeutronNetwork tenantNeutronNetwork = tenantNeutronPort != null ?
427 neutronNetworkCache.getNetwork(tenantNeutronPort.getNetworkUUID()) : null;
428 final String providerSegmentationId = tenantNeutronNetwork != null ?
429 tenantNeutronNetwork.getProviderSegmentationID() : null;
430 final String neutronRouterMac = tenantNeutronNetwork != null ?
431 networkIdToRouterMacCache.get(tenantNeutronNetwork.getID()) : null;
433 if (nodeIfPair == null || neutronTenantPortUuid == null ||
434 providerSegmentationId == null || providerSegmentationId.isEmpty() ||
435 floatingIpMac == null || floatingIpMac.isEmpty() ||
436 neutronRouterMac == null || neutronRouterMac.isEmpty()) {
437 LOG.trace("Floating IP {}<->{}, incomplete floatPort {} tenantPortUuid {} seg {} mac {} rtrMac {}",
440 neutronPortForFloatIp,
441 neutronTenantPortUuid,
442 providerSegmentationId,
448 // get ofport for patch port in br-int
449 final Long dpId = nodeIfPair.getLeft();
450 final Long ofPort = findOFPortForExtPatch(dpId);
451 if (ofPort == null) {
452 LOG.warn("Unable to locate OF port of patch port to connect floating ip to external bridge. dpid {}",
457 // Respond to ARPs for the floating ip address by default, via the patch port that connects br-int to br-ex
459 if (programStaticArpStage1(dpId, encodeExcplicitOFPort(ofPort), floatingIpMac, floatingIpAddress,
461 final FloatIpData floatIpData = new FloatIpData(dpId, ofPort, providerSegmentationId, floatingIpMac,
462 floatingIpAddress, fixedIpAddress, neutronRouterMac);
463 floatIpDataMapCache.put(neutronFloatingIP.getID(), floatIpData);
464 LOG.info("Floating IP {}<->{} programmed ARP mac {} on OFport {} seg {} dpid {}",
465 neutronFloatingIP.getFixedIPAddress(), neutronFloatingIP.getFloatingIPAddress(),
466 floatingIpMac, ofPort, providerSegmentationId, dpId);
470 private void programFlowsForFloatingIPArpDelete(final String neutronFloatingIPUuid) {
471 final FloatIpData floatIpData = floatIpDataMapCache.get(neutronFloatingIPUuid);
472 if (floatIpData == null) {
473 LOG.trace("programFlowsForFloatingIPArpDelete for uuid {} is not needed", neutronFloatingIPUuid);
477 if (programStaticArpStage1(floatIpData.dpid, encodeExcplicitOFPort(floatIpData.ofPort), floatIpData.macAddress,
478 floatIpData.floatingIpAddress, Action.DELETE)) {
479 floatIpDataMapCache.remove(neutronFloatingIPUuid);
480 LOG.info("Floating IP {} un-programmed ARP mac {} on {} dpid {}",
481 floatIpData.floatingIpAddress, floatIpData.macAddress, floatIpData.ofPort, floatIpData.dpid);
485 private NeutronPort findNeutronPortForFloatingIp(final String floatingIpUuid) {
486 for (NeutronPort neutronPort : neutronPortCache.getAllPorts()) {
487 if (neutronPort.getDeviceOwner().equals(OWNER_FLOATING_IP) &&
488 neutronPort.getDeviceID().equals(floatingIpUuid)) {
495 private Long findOFPortForExtPatch(Long dpId) {
496 final String brInt = configurationService.getIntegrationBridgeName();
497 final String brExt = configurationService.getExternalBridgeName();
498 final String portNameInt = configurationService.getPatchPortName(new ImmutablePair<>(brInt, brExt));
500 Preconditions.checkNotNull(dpId);
501 Preconditions.checkNotNull(portNameInt);
503 final long dpidPrimitive = dpId;
504 for (Node node : nodeCacheManager.getBridgeNodes()) {
505 if (dpidPrimitive == southbound.getDataPathId(node)) {
506 final OvsdbTerminationPointAugmentation terminationPointOfBridge =
507 southbound.getTerminationPointOfBridge(node, portNameInt);
508 return terminationPointOfBridge == null ? null : terminationPointOfBridge.getOfport();
517 * @param action the {@link org.opendaylight.ovsdb.openstack.netvirt.api.Action} action to be handled.
518 * @param neutronNetwork An {@link org.opendaylight.neutron.spi.NeutronNetwork} instance of NeutronFloatingIP object.
520 public void handleNeutronNetworkEvent(final NeutronNetwork neutronNetwork, Action action) {
521 LOG.debug("neutronNetwork {}: network: {}", action, neutronNetwork);
525 // Callbacks from OVSDB's southbound handler
530 * @param bridgeNode An instance of Node object.
531 * @param intf An {@link org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105
532 * .OvsdbTerminationPointAugmentation} instance of OvsdbTerminationPointAugmentation object.
533 * @param neutronNetwork An {@link org.opendaylight.neutron.spi.NeutronNetwork} instance of NeutronNetwork
535 * @param action the {@link org.opendaylight.ovsdb.openstack.netvirt.api.Action} action to be handled.
537 public void handleInterfaceEvent(final Node bridgeNode, final OvsdbTerminationPointAugmentation intf,
538 final NeutronNetwork neutronNetwork, Action action) {
539 LOG.debug("southbound interface {} node:{} interface:{}, neutronNetwork:{}",
540 action, bridgeNode.getNodeId().getValue(), intf.getName(), neutronNetwork);
545 final NeutronPort neutronPort = tenantNetworkManager.getTenantPort(intf);
546 final Long dpId = getDpidForIntegrationBridge(bridgeNode);
547 final Uuid interfaceUuid = intf.getInterfaceUuid();
549 LOG.trace("southbound interface {} node:{} interface:{}, neutronNetwork:{} port:{} dpid:{} intfUuid:{}",
550 action, bridgeNode.getNodeId().getValue(), intf.getName(), neutronNetwork, neutronPort, dpId, interfaceUuid);
552 if (neutronPort != null) {
553 final String neutronPortUuid = neutronPort.getPortUUID();
555 if (action != Action.DELETE && neutronPortToDpIdCache.get(neutronPortUuid) == null &&
556 dpId != null && interfaceUuid != null) {
557 handleInterfaceEventAdd(neutronPortUuid, dpId, interfaceUuid);
560 handleNeutronPortEvent(neutronPort, action);
563 if (action == Action.DELETE && interfaceUuid != null) {
564 handleInterfaceEventDelete(intf, dpId);
568 private void handleInterfaceEventAdd(final String neutronPortUuid, Long dpId, final Uuid interfaceUuid) {
569 neutronPortToDpIdCache.put(neutronPortUuid, new ImmutablePair<>(dpId, interfaceUuid));
570 LOG.debug("handleInterfaceEvent add cache entry NeutronPortUuid {} : dpid {}, ifUuid {}",
571 neutronPortUuid, dpId, interfaceUuid.getValue());
574 private void handleInterfaceEventDelete(final OvsdbTerminationPointAugmentation intf, final Long dpId) {
575 // Remove entry from neutronPortToDpIdCache based on interface uuid
576 for (Map.Entry<String, Pair<Long, Uuid>> entry : neutronPortToDpIdCache.entrySet()) {
577 final String currPortUuid = entry.getKey();
578 if (intf.getInterfaceUuid().equals(entry.getValue().getRight())) {
579 LOG.debug("handleInterfaceEventDelete remove cache entry NeutronPortUuid {} : dpid {}, ifUuid {}",
580 currPortUuid, dpId, intf.getInterfaceUuid().getValue());
581 neutronPortToDpIdCache.remove(currPortUuid);
590 private void updateL3ForNeutronPort(final NeutronPort neutronPort, final boolean isDelete) {
592 final String networkUUID = neutronPort.getNetworkUUID();
593 final String routerMacAddress = networkIdToRouterMacCache.get(networkUUID);
595 // If there is no router interface handling the networkUUID, we are done
596 if (routerMacAddress == null || routerMacAddress.isEmpty()) {
600 // If this is the neutron port for the router interface itself, ignore it as well. Ports that represent the
601 // router interface are handled via handleNeutronRouterInterfaceEvent.
602 if (routerMacAddress.equalsIgnoreCase(neutronPort.getMacAddress())) {
606 final NeutronNetwork neutronNetwork = neutronNetworkCache.getNetwork(networkUUID);
607 final String providerSegmentationId = neutronNetwork != null ?
608 neutronNetwork.getProviderSegmentationID() : null;
609 final String tenantMac = neutronPort.getMacAddress();
611 if (providerSegmentationId == null || providerSegmentationId.isEmpty() ||
612 tenantMac == null || tenantMac.isEmpty()) {
613 // done: go no further w/out all the info needed...
617 final Action action = isDelete ? Action.DELETE : Action.ADD;
618 List<Node> nodes = nodeCacheManager.getBridgeNodes();
619 if (nodes.isEmpty()) {
620 LOG.trace("updateL3ForNeutronPort has no nodes to work with");
622 for (Node node : nodes) {
623 final Long dpid = getDpidForIntegrationBridge(node);
628 for (Neutron_IPs neutronIP : neutronPort.getFixedIPs()) {
629 final String tenantIpStr = neutronIP.getIpAddress();
630 if (tenantIpStr.isEmpty()) {
634 // Configure L3 fwd. We do that regardless of tenant network present, because these rules are
635 // still needed when routing to subnets non-local to node (bug 2076).
636 programL3ForwardingStage1(node, dpid, providerSegmentationId, tenantMac, tenantIpStr, action);
638 // Configure distributed ARP responder
639 if (flgDistributedARPEnabled) {
640 programStaticArpStage1(dpid, providerSegmentationId, tenantMac, tenantIpStr, action);
646 private void programL3ForwardingStage1(Node node, Long dpid, String providerSegmentationId,
647 String macAddress, String ipStr,
648 Action actionForNode) {
649 // Based on the local cache, figure out whether programming needs to occur. To do this, we
650 // will look at desired action for node.
652 final String cacheKey = node.getNodeId().getValue() + ":" + providerSegmentationId + ":" + ipStr;
653 final Boolean isProgrammed = l3ForwardingCache.contains(cacheKey);
655 if (actionForNode == Action.DELETE && isProgrammed == Boolean.FALSE) {
656 LOG.trace("programL3ForwardingStage1 for node {} providerId {} mac {} ip {} action {} is already done",
657 node.getNodeId().getValue(), providerSegmentationId, macAddress, ipStr, actionForNode);
660 if (actionForNode == Action.ADD && isProgrammed == Boolean.TRUE) {
661 LOG.trace("programL3ForwardingStage1 for node {} providerId {} mac {} ip {} action {} is already done",
662 node.getNodeId().getValue(), providerSegmentationId, macAddress, ipStr, actionForNode);
666 Status status = this.programL3ForwardingStage2(node, dpid, providerSegmentationId,
667 macAddress, ipStr, actionForNode);
668 if (status.isSuccess()) {
670 if (actionForNode == Action.ADD) {
671 l3ForwardingCache.add(cacheKey);
673 l3ForwardingCache.remove(cacheKey);
678 private Status programL3ForwardingStage2(Node node, Long dpid, String providerSegmentationId,
681 Action actionForNode) {
684 InetAddress inetAddress = InetAddress.getByName(address);
685 status = l3ForwardingProvider == null ?
686 new Status(StatusCode.SUCCESS) :
687 l3ForwardingProvider.programForwardingTableEntry(dpid, providerSegmentationId,
688 inetAddress, macAddress, actionForNode);
689 } catch (UnknownHostException e) {
690 status = new Status(StatusCode.BADREQUEST);
693 if (status.isSuccess()) {
694 LOG.debug("ProgramL3Forwarding {} for mac:{} addr:{} node:{} action:{}",
695 l3ForwardingProvider == null ? "skipped" : "programmed",
696 macAddress, address, node.getNodeId().getValue(), actionForNode);
698 LOG.error("ProgramL3Forwarding failed for mac:{} addr:{} node:{} action:{} status:{}",
699 macAddress, address, node.getNodeId().getValue(), actionForNode, status);
706 private void programFlowsForNeutronRouterInterface(final NeutronRouter_Interface destNeutronRouterInterface,
708 Preconditions.checkNotNull(destNeutronRouterInterface);
710 final NeutronPort neutronPort = neutronPortCache.getPort(destNeutronRouterInterface.getPortUUID());
711 String macAddress = neutronPort != null ? neutronPort.getMacAddress() : null;
712 List<Neutron_IPs> ipList = neutronPort != null ? neutronPort.getFixedIPs() : null;
713 final NeutronSubnet subnet = neutronSubnetCache.getSubnet(destNeutronRouterInterface.getSubnetUUID());
714 final NeutronNetwork neutronNetwork = subnet != null ?
715 neutronNetworkCache.getNetwork(subnet.getNetworkUUID()) : null;
716 final String destinationSegmentationId = neutronNetwork != null ?
717 neutronNetwork.getProviderSegmentationID() : null;
718 final Boolean isExternal = neutronNetwork != null ? neutronNetwork.getRouterExternal() : Boolean.TRUE;
719 final String cidr = subnet != null ? subnet.getCidr() : null;
720 final int mask = getMaskLenFromCidr(cidr);
722 LOG.trace("programFlowsForNeutronRouterInterface called for interface {} isDelete {}",
723 destNeutronRouterInterface, isDelete);
725 // in delete path, mac address as well as ip address are not provided. Being so, let's find them from
727 if (neutronNetwork != null) {
728 if (macAddress == null || macAddress.isEmpty()) {
729 macAddress = networkIdToRouterMacCache.get(neutronNetwork.getNetworkUUID());
731 if (ipList == null || ipList.isEmpty()) {
732 ipList = networkIdToRouterIpListCache.get(neutronNetwork.getNetworkUUID());
736 if (destinationSegmentationId == null || destinationSegmentationId.isEmpty() ||
737 cidr == null || cidr.isEmpty() ||
738 macAddress == null || macAddress.isEmpty() ||
739 ipList == null || ipList.isEmpty()) {
740 LOG.debug("programFlowsForNeutronRouterInterface is bailing seg:{} cidr:{} mac:{} ip:{}",
741 destinationSegmentationId, cidr, macAddress, ipList);
742 // done: go no further w/out all the info needed...
746 final Action actionForNode = isDelete ? Action.DELETE : Action.ADD;
748 // Keep cache for finding router's mac from network uuid -- add
751 networkIdToRouterMacCache.put(neutronNetwork.getNetworkUUID(), macAddress);
752 networkIdToRouterIpListCache.put(neutronNetwork.getNetworkUUID(), new ArrayList<>(ipList));
753 subnetIdToRouterInterfaceCache.put(subnet.getSubnetUUID(), destNeutronRouterInterface);
756 List<Node> nodes = nodeCacheManager.getBridgeNodes();
757 if (nodes.isEmpty()) {
758 LOG.trace("programFlowsForNeutronRouterInterface has no nodes to work with");
760 for (Node node : nodes) {
761 final Long dpid = getDpidForIntegrationBridge(node);
766 for (Neutron_IPs neutronIP : ipList) {
767 final String ipStr = neutronIP.getIpAddress();
768 if (ipStr.isEmpty()) {
769 LOG.debug("programFlowsForNeutronRouterInterface is skipping node {} ip {}",
770 node.getNodeId().getValue(), ipStr);
774 // Iterate through all other interfaces and add/remove reflexive flows to this interface
776 for (NeutronRouter_Interface srcNeutronRouterInterface : subnetIdToRouterInterfaceCache.values()) {
777 programFlowsForNeutronRouterInterfacePair(node, dpid,
778 srcNeutronRouterInterface, destNeutronRouterInterface,
779 neutronNetwork, destinationSegmentationId,
780 macAddress, ipStr, mask, actionForNode,
781 true /*isReflexsive*/);
785 programFlowForNetworkFromExternal(node, dpid, destinationSegmentationId, macAddress, ipStr, mask,
788 // Enable ARP responder by default, because router interface needs to be responded always.
789 programStaticArpStage1(dpid, destinationSegmentationId, macAddress, ipStr, actionForNode);
792 // Compute action to be programmed. In the case of rewrite exclusions, we must never program rules
793 // for the external neutron networks.
796 final Action actionForRewriteExclusion = isExternal ? Action.DELETE : actionForNode;
797 programIpRewriteExclusionStage1(node, dpid, destinationSegmentationId, cidr, actionForRewriteExclusion);
801 // Keep cache for finding router's mac from network uuid -- remove
804 networkIdToRouterMacCache.remove(neutronNetwork.getNetworkUUID());
805 networkIdToRouterIpListCache.remove(neutronNetwork.getNetworkUUID());
806 subnetIdToRouterInterfaceCache.remove(subnet.getSubnetUUID());
810 private void programFlowForNetworkFromExternal(final Node node,
812 final String destinationSegmentationId,
813 final String dstMacAddress,
814 final String destIpStr,
816 final Action actionForNode) {
817 programRouterInterfaceStage1(node, dpid, Constants.EXTERNAL_NETWORK, destinationSegmentationId,
818 dstMacAddress, destIpStr, destMask, actionForNode);
821 private void programFlowsForNeutronRouterInterfacePair(final Node node,
823 final NeutronRouter_Interface srcNeutronRouterInterface,
824 final NeutronRouter_Interface dstNeutronRouterInterface,
825 final NeutronNetwork dstNeutronNetwork,
826 final String destinationSegmentationId,
827 final String dstMacAddress,
828 final String destIpStr,
830 final Action actionForNode,
831 Boolean isReflexsive) {
832 Preconditions.checkNotNull(srcNeutronRouterInterface);
833 Preconditions.checkNotNull(dstNeutronRouterInterface);
835 final String sourceSubnetId = srcNeutronRouterInterface.getSubnetUUID();
836 if (sourceSubnetId == null) {
837 LOG.error("Could not get provider Subnet ID from router interface {}",
838 srcNeutronRouterInterface.getID());
842 final NeutronSubnet sourceSubnet = neutronSubnetCache.getSubnet(sourceSubnetId);
843 final String sourceNetworkId = sourceSubnet == null ? null : sourceSubnet.getNetworkUUID();
844 if (sourceNetworkId == null) {
845 LOG.error("Could not get provider Network ID from subnet {}", sourceSubnetId);
849 final NeutronNetwork sourceNetwork = neutronNetworkCache.getNetwork(sourceNetworkId);
850 if (sourceNetwork == null) {
851 LOG.error("Could not get provider Network for Network ID {}", sourceNetworkId);
855 if (! sourceNetwork.getTenantID().equals(dstNeutronNetwork.getTenantID())) {
856 // Isolate subnets from different tenants within the same router
859 final String sourceSegmentationId = sourceNetwork.getProviderSegmentationID();
860 if (sourceSegmentationId == null) {
861 LOG.error("Could not get provider Segmentation ID for Subnet {}", sourceSubnetId);
864 if (sourceSegmentationId.equals(destinationSegmentationId)) {
869 programRouterInterfaceStage1(node, dpid, sourceSegmentationId, destinationSegmentationId,
870 dstMacAddress, destIpStr, destMask, actionForNode);
872 // Flip roles src->dst; dst->src
874 final NeutronPort sourceNeutronPort = neutronPortCache.getPort(srcNeutronRouterInterface.getPortUUID());
875 final String macAddress2 = sourceNeutronPort != null ? sourceNeutronPort.getMacAddress() : null;
876 final List<Neutron_IPs> ipList2 = sourceNeutronPort != null ? sourceNeutronPort.getFixedIPs() : null;
877 final String cidr2 = sourceSubnet.getCidr();
878 final int mask2 = getMaskLenFromCidr(cidr2);
880 if (cidr2 == null || cidr2.isEmpty() ||
881 macAddress2 == null || macAddress2.isEmpty() ||
882 ipList2 == null || ipList2.isEmpty()) {
883 LOG.trace("programFlowsForNeutronRouterInterfacePair reflexive is bailing seg:{} cidr:{} mac:{} ip:{}",
884 sourceSegmentationId, cidr2, macAddress2, ipList2);
885 // done: go no further w/out all the info needed...
889 for (Neutron_IPs neutronIP2 : ipList2) {
890 final String ipStr2 = neutronIP2.getIpAddress();
891 if (ipStr2.isEmpty()) {
894 programFlowsForNeutronRouterInterfacePair(node, dpid, dstNeutronRouterInterface,
895 srcNeutronRouterInterface,
896 sourceNetwork, sourceSegmentationId,
897 macAddress2, ipStr2, mask2, actionForNode,
898 false /*isReflexsive*/);
903 private void programRouterInterfaceStage1(Node node, Long dpid, String sourceSegmentationId,
904 String destinationSegmentationId,
905 String macAddress, String ipStr, int mask,
906 Action actionForNode) {
907 // Based on the local cache, figure out whether programming needs to occur. To do this, we
908 // will look at desired action for node.
910 final String cacheKey = node.getNodeId().getValue() + ":" +
911 sourceSegmentationId + ":" + destinationSegmentationId + ":" +
912 ipStr + "/" + Integer.toString(mask);
913 final Boolean isProgrammed = routerInterfacesCache.contains(cacheKey);
915 if (actionForNode == Action.DELETE && isProgrammed == Boolean.FALSE) {
916 LOG.trace("programRouterInterfaceStage1 for node {} sourceSegId {} destSegId {} mac {} ip {} mask {}" +
917 " action {} is already done",
918 node.getNodeId().getValue(), sourceSegmentationId, destinationSegmentationId,
919 macAddress, ipStr, mask, actionForNode);
922 if (actionForNode == Action.ADD && isProgrammed == Boolean.TRUE) {
923 LOG.trace("programRouterInterfaceStage1 for node {} sourceSegId {} destSegId {} mac {} ip {} mask {}" +
924 " action {} is already done",
925 node.getNodeId().getValue(), sourceSegmentationId, destinationSegmentationId,
926 macAddress, ipStr, mask, actionForNode);
930 Status status = this.programRouterInterfaceStage2(node, dpid, sourceSegmentationId, destinationSegmentationId,
931 macAddress, ipStr, mask, actionForNode);
932 if (status.isSuccess()) {
934 if (actionForNode == Action.ADD) {
935 // TODO: multiTenantAwareRouter.addInterface(UUID.fromString(tenant), ...);
936 routerInterfacesCache.add(cacheKey);
938 // TODO: multiTenantAwareRouter.removeInterface(...);
939 routerInterfacesCache.remove(cacheKey);
944 private Status programRouterInterfaceStage2(Node node, Long dpid, String sourceSegmentationId,
945 String destinationSegmentationId,
947 String address, int mask,
948 Action actionForNode) {
951 InetAddress inetAddress = InetAddress.getByName(address);
952 status = routingProvider == null ?
953 new Status(StatusCode.SUCCESS) :
954 routingProvider.programRouterInterface(dpid, sourceSegmentationId, destinationSegmentationId,
955 macAddress, inetAddress, mask, actionForNode);
956 } catch (UnknownHostException e) {
957 status = new Status(StatusCode.BADREQUEST);
960 if (status.isSuccess()) {
961 LOG.debug("ProgramRouterInterface {} for mac:{} addr:{}/{} node:{} srcTunId:{} destTunId:{} action:{}",
962 routingProvider == null ? "skipped" : "programmed",
963 macAddress, address, mask, node.getNodeId().getValue(), sourceSegmentationId, destinationSegmentationId,
966 LOG.error("ProgramRouterInterface failed for mac:{} addr:{}/{} node:{} srcTunId:{} destTunId:{} action:{} status:{}",
967 macAddress, address, mask, node.getNodeId().getValue(), sourceSegmentationId, destinationSegmentationId,
968 actionForNode, status);
973 private boolean programStaticArpStage1(Long dpid, String segOrOfPort,
974 String macAddress, String ipStr,
976 // Based on the local cache, figure out whether programming needs to occur. To do this, we
977 // will look at desired action for node.
979 final String cacheKey = dpid + ":" + segOrOfPort + ":" + ipStr;
980 final Boolean isProgrammed = staticArpEntryCache.contains(cacheKey);
982 if (action == Action.DELETE && isProgrammed == Boolean.FALSE) {
983 LOG.trace("programStaticArpStage1 dpid {} segOrOfPort {} mac {} ip {} action {} is already done",
984 dpid, segOrOfPort, macAddress, ipStr, action);
987 if (action == Action.ADD && isProgrammed == Boolean.TRUE) {
988 LOG.trace("programStaticArpStage1 dpid {} segOrOfPort {} mac {} ip {} action {} is already done",
989 dpid, segOrOfPort, macAddress, ipStr, action);
993 Status status = this.programStaticArpStage2(dpid, segOrOfPort, macAddress, ipStr, action);
994 if (status.isSuccess()) {
996 if (action == Action.ADD) {
997 staticArpEntryCache.add(cacheKey);
999 staticArpEntryCache.remove(cacheKey);
1006 private Status programStaticArpStage2(Long dpid,
1013 InetAddress inetAddress = InetAddress.getByName(address);
1014 status = arpProvider == null ?
1015 new Status(StatusCode.SUCCESS) :
1016 arpProvider.programStaticArpEntry(dpid, segOrOfPort,
1017 macAddress, inetAddress, action);
1018 } catch (UnknownHostException e) {
1019 status = new Status(StatusCode.BADREQUEST);
1022 if (status.isSuccess()) {
1023 LOG.debug("ProgramStaticArp {} for mac:{} addr:{} dpid:{} segOrOfPort:{} action:{}",
1024 arpProvider == null ? "skipped" : "programmed",
1025 macAddress, address, dpid, segOrOfPort, action);
1027 LOG.error("ProgramStaticArp failed for mac:{} addr:{} dpid:{} segOrOfPort:{} action:{} status:{}",
1028 macAddress, address, dpid, segOrOfPort, action, status);
1033 private boolean programInboundIpRewriteStage1(Long dpid, Long inboundOFPort, String providerSegmentationId,
1034 String matchAddress, String rewriteAddress,
1036 // Based on the local cache, figure out whether programming needs to occur. To do this, we
1037 // will look at desired action for node.
1039 final String cacheKey = dpid + ":" + inboundOFPort + ":" + providerSegmentationId + ":" + matchAddress;
1040 final Boolean isProgrammed = inboundIpRewriteCache.contains(cacheKey);
1042 if (action == Action.DELETE && isProgrammed == Boolean.FALSE) {
1043 LOG.trace("programInboundIpRewriteStage1 dpid {} OFPort {} seg {} matchAddress {} rewriteAddress {}" +
1044 " action {} is already done",
1045 dpid, inboundOFPort, providerSegmentationId, matchAddress, rewriteAddress, action);
1048 if (action == Action.ADD && isProgrammed == Boolean.TRUE) {
1049 LOG.trace("programInboundIpRewriteStage1 dpid {} OFPort {} seg {} matchAddress {} rewriteAddress {}" +
1050 " action is already done",
1051 dpid, inboundOFPort, providerSegmentationId, matchAddress, rewriteAddress, action);
1055 Status status = programInboundIpRewriteStage2(dpid, inboundOFPort, providerSegmentationId, matchAddress,
1056 rewriteAddress, action);
1057 if (status.isSuccess()) {
1059 if (action == Action.ADD) {
1060 inboundIpRewriteCache.add(cacheKey);
1062 inboundIpRewriteCache.remove(cacheKey);
1069 private Status programInboundIpRewriteStage2(Long dpid, Long inboundOFPort, String providerSegmentationId,
1070 String matchAddress, String rewriteAddress,
1074 InetAddress inetMatchAddress = InetAddress.getByName(matchAddress);
1075 InetAddress inetRewriteAddress = InetAddress.getByName(rewriteAddress);
1076 status = inboundNatProvider == null ?
1077 new Status(StatusCode.SUCCESS) :
1078 inboundNatProvider.programIpRewriteRule(dpid, inboundOFPort, providerSegmentationId,
1079 inetMatchAddress, inetRewriteAddress,
1081 } catch (UnknownHostException e) {
1082 status = new Status(StatusCode.BADREQUEST);
1085 if (status.isSuccess()) {
1086 final boolean isSkipped = inboundNatProvider == null;
1087 LOG.debug("programInboundIpRewriteStage2 {} for dpid:{} ofPort:{} seg:{} match:{} rewrite:{} action:{}",
1088 isSkipped ? "skipped" : "programmed",
1089 dpid, inboundOFPort, providerSegmentationId, matchAddress, rewriteAddress, action);
1091 LOG.error("programInboundIpRewriteStage2 failed for dpid:{} ofPort:{} seg:{} match:{} rewrite:{} action:{}" +
1093 dpid, inboundOFPort, providerSegmentationId, matchAddress, rewriteAddress, action,
1099 private void programIpRewriteExclusionStage1(Node node, Long dpid, String providerSegmentationId, String cidr,
1100 Action actionForRewriteExclusion) {
1101 // Based on the local cache, figure out whether programming needs to occur. To do this, we
1102 // will look at desired action for node.
1104 final String cacheKey = node.getNodeId().getValue() + ":" + providerSegmentationId + ":" + cidr;
1105 final Boolean isProgrammed = outboundIpRewriteExclusionCache.contains(cacheKey);
1107 if (actionForRewriteExclusion == Action.DELETE && isProgrammed == Boolean.FALSE) {
1108 LOG.trace("programIpRewriteExclusionStage1 node {} providerId {} cidr {} action {} is already done",
1109 node.getNodeId().getValue(), providerSegmentationId, cidr, actionForRewriteExclusion);
1112 if (actionForRewriteExclusion == Action.ADD && isProgrammed == Boolean.TRUE) {
1113 LOG.trace("programIpRewriteExclusionStage1 node {} providerId {} cidr {} action {} is already done",
1114 node.getNodeId().getValue(), providerSegmentationId, cidr, actionForRewriteExclusion);
1118 Status status = this.programIpRewriteExclusionStage2(node, dpid, providerSegmentationId, cidr,
1119 actionForRewriteExclusion);
1120 if (status.isSuccess()) {
1122 if (actionForRewriteExclusion == Action.ADD) {
1123 outboundIpRewriteExclusionCache.add(cacheKey);
1125 outboundIpRewriteExclusionCache.remove(cacheKey);
1130 private Status programIpRewriteExclusionStage2(Node node, Long dpid, String providerSegmentationId, String cidr,
1131 Action actionForNode) {
1132 final Status status = outboundNatProvider == null ? new Status(StatusCode.SUCCESS) :
1133 outboundNatProvider.programIpRewriteExclusion(dpid, providerSegmentationId, cidr, actionForNode);
1135 if (status.isSuccess()) {
1136 final boolean isSkipped = outboundNatProvider == null;
1137 LOG.debug("IpRewriteExclusion {} for cidr:{} node:{} action:{}",
1138 isSkipped ? "skipped" : "programmed",
1139 cidr, node.getNodeId().getValue(), actionForNode);
1141 LOG.error("IpRewriteExclusion failed for cidr:{} node:{} action:{} status:{}",
1142 cidr, node.getNodeId().getValue(), actionForNode, status);
1147 private void programOutboundIpRewriteStage1(FloatIpData fid, Action action) {
1148 // Based on the local cache, figure out whether programming needs to occur. To do this, we
1149 // will look at desired action for node.
1151 final String cacheKey = fid.dpid + ":" + fid.segId + ":" + fid.fixedIpAddress;
1152 final Boolean isProgrammed = outboundIpRewriteCache.contains(cacheKey);
1154 if (action == Action.DELETE && isProgrammed == Boolean.FALSE) {
1155 LOG.trace("programOutboundIpRewriteStage1 dpid {} seg {} fixedIpAddress {} floatIp {} action {} " +
1157 fid.dpid, fid.segId, fid.fixedIpAddress, fid.floatingIpAddress, action);
1160 if (action == Action.ADD && isProgrammed == Boolean.TRUE) {
1161 LOG.trace("programOutboundIpRewriteStage1 dpid {} seg {} fixedIpAddress {} floatIp {} action {} " +
1163 fid.dpid, fid.segId, fid.fixedIpAddress, fid.floatingIpAddress, action);
1167 Status status = this.programOutboundIpRewriteStage2(fid, action);
1168 if (status.isSuccess()) {
1170 if (action == Action.ADD) {
1171 outboundIpRewriteCache.add(cacheKey);
1173 outboundIpRewriteCache.remove(cacheKey);
1178 private Status programOutboundIpRewriteStage2(FloatIpData fid, Action action) {
1181 InetAddress matchSrcAddress = InetAddress.getByName(fid.fixedIpAddress);
1182 InetAddress rewriteSrcAddress = InetAddress.getByName(fid.floatingIpAddress);
1183 status = outboundNatProvider == null ?
1184 new Status(StatusCode.SUCCESS) :
1185 outboundNatProvider.programIpRewriteRule(
1186 fid.dpid, fid.segId, fid.neutronRouterMac, matchSrcAddress, fid.macAddress,
1187 this.externalRouterMac, rewriteSrcAddress, fid.ofPort, action);
1188 } catch (UnknownHostException e) {
1189 status = new Status(StatusCode.BADREQUEST);
1192 if (status.isSuccess()) {
1193 final boolean isSkipped = outboundNatProvider == null;
1194 LOG.debug("programOutboundIpRewriteStage2 {} for dpid {} seg {} fixedIpAddress {} floatIp {}" +
1196 isSkipped ? "skipped" : "programmed",
1197 fid.dpid, fid.segId, fid.fixedIpAddress, fid.floatingIpAddress, action);
1199 LOG.error("programOutboundIpRewriteStage2 failed for dpid {} seg {} fixedIpAddress {} floatIp {}" +
1200 " action {} status:{}",
1201 fid.dpid, fid.segId, fid.fixedIpAddress, fid.floatingIpAddress, action, status);
1206 private int getMaskLenFromCidr(String cidr) {
1210 String[] splits = cidr.split("/");
1211 if (splits.length != 2) {
1217 result = Integer.parseInt(splits[1].trim());
1218 } catch (NumberFormatException nfe) {
1224 private Long getDpidForIntegrationBridge(Node node) {
1225 // Check if node is integration bridge; and only then return its dpid
1226 if (southbound.getBridge(node, configurationService.getIntegrationBridgeName()) != null) {
1227 return southbound.getDataPathId(node);
1232 private Long getDpidForExternalBridge(Node node) {
1233 // Check if node is integration bridge; and only then return its dpid
1234 if (southbound.getBridge(node, configurationService.getExternalBridgeName()) != null) {
1235 return southbound.getDataPathId(node);
1240 private Node getExternalBridgeNode(){
1241 //Pickup the first node that has external bridge (br-ex).
1242 //NOTE: We are assuming that all the br-ex are serving one external network and gateway ip of
1243 //the external network is reachable from every br-ex
1244 // TODO: Consider other deployment scenario, and thing of better solution.
1245 List<Node> allBridges = nodeCacheManager.getBridgeNodes();
1246 for(Node node : allBridges){
1247 if (southbound.getBridge(node, configurationService.getExternalBridgeName()) != null) {
1254 private NeutronSubnet getExternalNetworkSubnet(NeutronPort gatewayPort){
1255 NeutronSubnet extSubnet = null;
1256 for (NeutronSubnet subnet : neutronSubnetCache.getAllSubnets()){
1257 if(subnet.getPortsInSubnet().contains(gatewayPort)){
1265 public void triggerGatewayMacResolver(final Node node, final NeutronPort gatewayPort ){
1267 Preconditions.checkNotNull(node);
1268 Preconditions.checkNotNull(gatewayPort);
1269 NeutronNetwork externalNetwork = neutronNetworkCache.getNetwork(gatewayPort.getNetworkUUID());
1271 if(externalNetwork != null){
1272 if(externalNetwork.isRouterExternal()){
1273 final NeutronSubnet externalSubnet = getExternalNetworkSubnet(gatewayPort);
1274 if(externalSubnet != null){
1275 if(externalSubnet.getGatewayIP() != null){
1276 LOG.info("Trigger MAC resolution for gateway ip {} on Node {}", externalSubnet.getGatewayIP(), node.getNodeId());
1278 ListenableFuture<MacAddress> gatewayMacAddress =
1279 gatewayMacResolver.resolveMacAddress(getDpidForExternalBridge(node),
1280 new Ipv4Address(externalSubnet.getGatewayIP()),
1281 new Ipv4Address(gatewayPort.getFixedIPs().get(0).getIpAddress()),
1282 new MacAddress(gatewayPort.getMacAddress()),
1284 if(gatewayMacAddress != null){
1285 Futures.addCallback(gatewayMacAddress, new FutureCallback<MacAddress>(){
1287 public void onSuccess(MacAddress result) {
1289 updateExternalRouterMac(result.getValue());
1290 LOG.info("Resolved MAC address for gateway IP {} is {}", externalSubnet.getGatewayIP(), result.getValue());
1292 LOG.warn("MAC address resolution failed for gateway IP {}", externalSubnet.getGatewayIP());
1297 public void onFailure(Throwable t) {
1298 LOG.warn("MAC address resolution failed for gateway IP {}", externalSubnet.getGatewayIP());
1300 }, gatewayMacResolverPool);
1303 LOG.warn("No gateway IP address found for external subnet {}", externalSubnet);
1306 LOG.warn("Neutron subnet not found for external network {}", externalNetwork);
1310 LOG.warn("Neutron network not found for router interface {}", gatewayPort);
1315 * Return String that represents OF port with marker explicitly provided (reverse of MatchUtils:parseExplicitOFPort)
1317 * @param ofPort the OF port number
1318 * @return the string with encoded OF port (example format "OFPort|999")
1320 public static String encodeExcplicitOFPort(Long ofPort) {
1321 return "OFPort|" + ofPort.toString();
1325 public void setDependencies(BundleContext bundleContext, ServiceReference serviceReference) {
1326 tenantNetworkManager =
1327 (TenantNetworkManager) ServiceHelper.getGlobalInstance(TenantNetworkManager.class, this);
1328 configurationService =
1329 (ConfigurationService) ServiceHelper.getGlobalInstance(ConfigurationService.class, this);
1331 (ArpProvider) ServiceHelper.getGlobalInstance(ArpProvider.class, this);
1332 inboundNatProvider =
1333 (InboundNatProvider) ServiceHelper.getGlobalInstance(InboundNatProvider.class, this);
1334 outboundNatProvider =
1335 (OutboundNatProvider) ServiceHelper.getGlobalInstance(OutboundNatProvider.class, this);
1337 (RoutingProvider) ServiceHelper.getGlobalInstance(RoutingProvider.class, this);
1338 l3ForwardingProvider =
1339 (L3ForwardingProvider) ServiceHelper.getGlobalInstance(L3ForwardingProvider.class, this);
1341 (NodeCacheManager) ServiceHelper.getGlobalInstance(NodeCacheManager.class, this);
1343 (Southbound) ServiceHelper.getGlobalInstance(Southbound.class, this);
1344 gatewayMacResolver =
1345 (GatewayMacResolver) ServiceHelper.getGlobalInstance(GatewayMacResolver.class, this);
1346 initL3AdapterMembers();
1350 public void setDependencies(Object impl) {
1351 if (impl instanceof INeutronNetworkCRUD) {
1352 neutronNetworkCache = (INeutronNetworkCRUD)impl;
1353 } else if (impl instanceof INeutronPortCRUD) {
1354 neutronPortCache = (INeutronPortCRUD)impl;
1355 } else if (impl instanceof INeutronSubnetCRUD) {
1356 neutronSubnetCache = (INeutronSubnetCRUD)impl;
1357 } else if (impl instanceof ArpProvider) {
1358 arpProvider = (ArpProvider)impl;
1359 } else if (impl instanceof InboundNatProvider) {
1360 inboundNatProvider = (InboundNatProvider)impl;
1361 } else if (impl instanceof OutboundNatProvider) {
1362 outboundNatProvider = (OutboundNatProvider)impl;
1363 } else if (impl instanceof RoutingProvider) {
1364 routingProvider = (RoutingProvider)impl;
1365 } else if (impl instanceof L3ForwardingProvider) {
1366 l3ForwardingProvider = (L3ForwardingProvider)impl;
1367 }else if (impl instanceof GatewayMacResolver) {
1368 gatewayMacResolver = (GatewayMacResolver)impl;