sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.openstacknetworking.routing; |
| 17 | |
| 18 | import com.google.common.collect.Lists; |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame^] | 19 | import com.google.common.collect.Sets; |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 20 | import org.apache.felix.scr.annotations.Activate; |
| 21 | import org.apache.felix.scr.annotations.Component; |
| 22 | import org.apache.felix.scr.annotations.Deactivate; |
| 23 | import org.apache.felix.scr.annotations.Reference; |
| 24 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 25 | import org.apache.felix.scr.annotations.Service; |
| 26 | import org.onlab.packet.Ethernet; |
| 27 | import org.onlab.packet.IPv4; |
| 28 | import org.onlab.packet.Ip4Address; |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame^] | 29 | import org.onlab.packet.IpAddress; |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 30 | import org.onlab.packet.MacAddress; |
Hyunsun Moon | 0448ea0 | 2016-02-23 14:17:39 -0800 | [diff] [blame] | 31 | import org.onlab.packet.UDP; |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 32 | import org.onlab.util.KryoNamespace; |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 33 | import org.onosproject.core.ApplicationId; |
| 34 | import org.onosproject.core.CoreService; |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame^] | 35 | import org.onosproject.mastership.MastershipService; |
| 36 | import org.onosproject.net.DefaultAnnotations; |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 37 | import org.onosproject.net.DeviceId; |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame^] | 38 | import org.onosproject.net.Host; |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 39 | import org.onosproject.net.Port; |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 40 | import org.onosproject.net.device.DeviceService; |
| 41 | import org.onosproject.net.driver.DriverService; |
| 42 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame^] | 43 | import org.onosproject.net.host.DefaultHostDescription; |
| 44 | import org.onosproject.net.host.HostDescription; |
| 45 | import org.onosproject.net.host.HostEvent; |
| 46 | import org.onosproject.net.host.HostListener; |
| 47 | import org.onosproject.net.host.HostProvider; |
| 48 | import org.onosproject.net.host.HostProviderRegistry; |
| 49 | import org.onosproject.net.host.HostProviderService; |
| 50 | import org.onosproject.net.host.HostService; |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 51 | import org.onosproject.net.packet.InboundPacket; |
| 52 | import org.onosproject.net.packet.PacketContext; |
| 53 | import org.onosproject.net.packet.PacketProcessor; |
| 54 | import org.onosproject.net.packet.PacketService; |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame^] | 55 | import org.onosproject.net.provider.AbstractProvider; |
| 56 | import org.onosproject.net.provider.ProviderId; |
sangho | 93447f1 | 2016-02-24 00:33:22 +0900 | [diff] [blame] | 57 | import org.onosproject.openstackinterface.OpenstackFloatingIP; |
| 58 | import org.onosproject.openstackinterface.OpenstackInterfaceService; |
| 59 | import org.onosproject.openstackinterface.OpenstackPort; |
| 60 | import org.onosproject.openstackinterface.OpenstackRouter; |
| 61 | import org.onosproject.openstackinterface.OpenstackRouterInterface; |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 62 | import org.onosproject.openstacknetworking.OpenstackRoutingService; |
Kyuhwi Choi | 92d9ea4 | 2016-06-13 17:28:00 +0900 | [diff] [blame] | 63 | import org.onosproject.scalablegateway.api.ScalableGatewayService; |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame^] | 64 | import org.onosproject.openstacknetworking.routing.OpenstackFloatingIPHandler.Action; |
| 65 | import org.onosproject.openstacknetworking.Constants; |
| 66 | import org.onosproject.openstacknode.OpenstackNode; |
| 67 | import org.onosproject.openstacknode.OpenstackNodeEvent; |
| 68 | import org.onosproject.openstacknode.OpenstackNodeListener; |
| 69 | import org.onosproject.openstacknode.OpenstackNodeService; |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 70 | import org.onosproject.store.serializers.KryoNamespaces; |
| 71 | import org.onosproject.store.service.ConsistentMap; |
| 72 | import org.onosproject.store.service.Serializer; |
| 73 | import org.onosproject.store.service.StorageService; |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 74 | import org.slf4j.Logger; |
| 75 | import org.slf4j.LoggerFactory; |
| 76 | |
| 77 | import java.util.Collection; |
| 78 | import java.util.List; |
Daniel Park | 2319390 | 2016-03-24 18:17:19 +0900 | [diff] [blame] | 79 | import java.util.Optional; |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame^] | 80 | import java.util.Set; |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 81 | import java.util.concurrent.ExecutorService; |
| 82 | import java.util.concurrent.Executors; |
| 83 | import java.util.stream.Collectors; |
| 84 | |
| 85 | import static com.google.common.base.Preconditions.checkNotNull; |
| 86 | import static org.onlab.util.Tools.groupedThreads; |
| 87 | |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 88 | @Component(immediate = true) |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 89 | @Service |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 90 | /** |
| 91 | * Populates flow rules about L3 functionality for VMs in Openstack. |
| 92 | */ |
| 93 | public class OpenstackRoutingManager implements OpenstackRoutingService { |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 94 | |
| 95 | private final Logger log = LoggerFactory.getLogger(getClass()); |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 96 | |
| 97 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 98 | protected CoreService coreService; |
| 99 | |
| 100 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 101 | protected PacketService packetService; |
| 102 | |
| 103 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 104 | protected DeviceService deviceService; |
| 105 | |
| 106 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
sangho | 93447f1 | 2016-02-24 00:33:22 +0900 | [diff] [blame] | 107 | protected OpenstackInterfaceService openstackService; |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 108 | |
| 109 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 110 | protected FlowObjectiveService flowObjectiveService; |
| 111 | |
| 112 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 113 | protected DriverService driverService; |
| 114 | |
Daniel Park | 81a61a1 | 2016-02-26 08:24:44 +0900 | [diff] [blame] | 115 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 116 | protected StorageService storageService; |
Daniel Park | 81a61a1 | 2016-02-26 08:24:44 +0900 | [diff] [blame] | 117 | |
Kyuhwi Choi | 92d9ea4 | 2016-06-13 17:28:00 +0900 | [diff] [blame] | 118 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame^] | 119 | protected HostService hostService; |
| 120 | |
| 121 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 122 | protected HostProviderRegistry hostProviderRegistry; |
| 123 | |
| 124 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Kyuhwi Choi | 92d9ea4 | 2016-06-13 17:28:00 +0900 | [diff] [blame] | 125 | protected ScalableGatewayService gatewayService; |
Daniel Park | 2319390 | 2016-03-24 18:17:19 +0900 | [diff] [blame] | 126 | |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame^] | 127 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 128 | protected OpenstackNodeService nodeService; |
| 129 | |
| 130 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 131 | protected MastershipService mastershipService; |
| 132 | |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 133 | private ApplicationId appId; |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 134 | private ConsistentMap<Integer, String> tpPortNumMap; // Map<PortNum, allocated VM`s Mac & destionation Ip address> |
| 135 | private ConsistentMap<String, OpenstackFloatingIP> floatingIpMap; // Map<FloatingIp`s Id, FloatingIp object> |
Kyuhwi Choi | 5e65e2c | 2016-04-01 11:48:15 +0900 | [diff] [blame] | 136 | // Map<RouterInterface`s portId, Corresponded port`s network id> |
| 137 | private ConsistentMap<String, String> routerInterfaceMap; |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame^] | 138 | private static final ProviderId PID = new ProviderId("of", "org.onosproject.openstackroutering", true); |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 139 | private static final String APP_ID = "org.onosproject.openstackrouting"; |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 140 | private static final String PORT_NAME = "portName"; |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 141 | private static final String PORTNAME_PREFIX_VM = "tap"; |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 142 | private static final String DEVICE_OWNER_ROUTER_INTERFACE = "network:router_interface"; |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 143 | private static final String FLOATING_IP_MAP_NAME = "openstackrouting-floatingip"; |
Kyuhwi Choi | 5e65e2c | 2016-04-01 11:48:15 +0900 | [diff] [blame] | 144 | private static final String TP_PORT_MAP_NAME = "openstackrouting-tpportnum"; |
| 145 | private static final String ROUTER_INTERFACE_MAP_NAME = "openstackrouting-routerinterface"; |
Daniel Park | 2319390 | 2016-03-24 18:17:19 +0900 | [diff] [blame] | 146 | private static final String COLON = ":"; |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 147 | private static final int PNAT_PORT_EXPIRE_TIME = 1200 * 1000; |
| 148 | private static final int TP_PORT_MINIMUM_NUM = 1024; |
| 149 | private static final int TP_PORT_MAXIMUM_NUM = 65535; |
sangho | 4890754 | 2016-03-28 16:07:07 +0900 | [diff] [blame] | 150 | |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 151 | private static final KryoNamespace.Builder FLOATING_IP_SERIALIZER = KryoNamespace.newBuilder() |
| 152 | .register(KryoNamespaces.API) |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 153 | .register(OpenstackFloatingIP.FloatingIpStatus.class) |
HIGUCHI Yuta | c0f5045 | 2016-05-13 19:26:05 -0700 | [diff] [blame] | 154 | .register(OpenstackFloatingIP.class); |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 155 | |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 156 | private static final KryoNamespace.Builder NUMBER_SERIALIZER = KryoNamespace.newBuilder() |
HIGUCHI Yuta | c0f5045 | 2016-05-13 19:26:05 -0700 | [diff] [blame] | 157 | .register(KryoNamespaces.API); |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 158 | |
Kyuhwi Choi | 5e65e2c | 2016-04-01 11:48:15 +0900 | [diff] [blame] | 159 | private static final KryoNamespace.Builder ROUTER_INTERFACE_SERIALIZER = KryoNamespace.newBuilder() |
HIGUCHI Yuta | c0f5045 | 2016-05-13 19:26:05 -0700 | [diff] [blame] | 160 | .register(KryoNamespaces.API); |
Kyuhwi Choi | 5e65e2c | 2016-04-01 11:48:15 +0900 | [diff] [blame] | 161 | |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 162 | private InternalPacketProcessor internalPacketProcessor = new InternalPacketProcessor(); |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame^] | 163 | private InternalHostListener internalHostListener = new InternalHostListener(); |
| 164 | private InternalOpenstackNodeListener internalNodeListener = new InternalOpenstackNodeListener(); |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 165 | private ExecutorService l3EventExecutorService = |
| 166 | Executors.newSingleThreadExecutor(groupedThreads("onos/openstackrouting", "L3-event")); |
| 167 | private ExecutorService icmpEventExecutorService = |
| 168 | Executors.newSingleThreadExecutor(groupedThreads("onos/openstackrouting", "icmp-event")); |
Daniel Park | 81a61a1 | 2016-02-26 08:24:44 +0900 | [diff] [blame] | 169 | private ExecutorService arpEventExecutorService = |
| 170 | Executors.newSingleThreadExecutor(groupedThreads("onos/openstackrouting", "arp-event")); |
| 171 | private OpenstackIcmpHandler openstackIcmpHandler; |
| 172 | private OpenstackRoutingArpHandler openstackArpHandler; |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 173 | private OpenstackRoutingRulePopulator rulePopulator; |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame^] | 174 | |
| 175 | private HostProviderService hostProviderService; |
| 176 | private final HostProvider hostProvider = new InternalHostProvider(); |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 177 | |
| 178 | @Activate |
| 179 | protected void activate() { |
| 180 | appId = coreService.registerApplication(APP_ID); |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame^] | 181 | hostService.addListener(internalHostListener); |
| 182 | nodeService.addListener(internalNodeListener); |
| 183 | hostProviderService = hostProviderRegistry.register(hostProvider); |
Daniel Park | 81a61a1 | 2016-02-26 08:24:44 +0900 | [diff] [blame] | 184 | |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 185 | floatingIpMap = storageService.<String, OpenstackFloatingIP>consistentMapBuilder() |
| 186 | .withSerializer(Serializer.using(FLOATING_IP_SERIALIZER.build())) |
| 187 | .withName(FLOATING_IP_MAP_NAME) |
| 188 | .withApplicationId(appId) |
| 189 | .build(); |
| 190 | tpPortNumMap = storageService.<Integer, String>consistentMapBuilder() |
| 191 | .withSerializer(Serializer.using(NUMBER_SERIALIZER.build())) |
| 192 | .withName(TP_PORT_MAP_NAME) |
| 193 | .withApplicationId(appId) |
| 194 | .build(); |
Kyuhwi Choi | 5e65e2c | 2016-04-01 11:48:15 +0900 | [diff] [blame] | 195 | routerInterfaceMap = storageService.<String, String>consistentMapBuilder() |
| 196 | .withSerializer(Serializer.using(ROUTER_INTERFACE_SERIALIZER.build())) |
| 197 | .withName(ROUTER_INTERFACE_MAP_NAME) |
| 198 | .withApplicationId(appId) |
| 199 | .build(); |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 200 | |
Kyuhwi Choi | 5e65e2c | 2016-04-01 11:48:15 +0900 | [diff] [blame] | 201 | log.info("started"); |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | @Deactivate |
| 205 | protected void deactivate() { |
| 206 | packetService.removeProcessor(internalPacketProcessor); |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame^] | 207 | hostService.removeListener(internalHostListener); |
| 208 | nodeService.removeListener(internalNodeListener); |
| 209 | |
Daniel Park | 81a61a1 | 2016-02-26 08:24:44 +0900 | [diff] [blame] | 210 | l3EventExecutorService.shutdown(); |
| 211 | icmpEventExecutorService.shutdown(); |
| 212 | arpEventExecutorService.shutdown(); |
| 213 | |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 214 | floatingIpMap.clear(); |
| 215 | tpPortNumMap.clear(); |
Kyuhwi Choi | 5e65e2c | 2016-04-01 11:48:15 +0900 | [diff] [blame] | 216 | routerInterfaceMap.clear(); |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 217 | |
Kyuhwi Choi | 5e65e2c | 2016-04-01 11:48:15 +0900 | [diff] [blame] | 218 | log.info("stopped"); |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | |
| 222 | @Override |
Kyuhwi Choi | 5e65e2c | 2016-04-01 11:48:15 +0900 | [diff] [blame] | 223 | public void createFloatingIP(OpenstackFloatingIP openstackFloatingIp) { |
| 224 | floatingIpMap.put(openstackFloatingIp.id(), openstackFloatingIp); |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | @Override |
Kyuhwi Choi | 5e65e2c | 2016-04-01 11:48:15 +0900 | [diff] [blame] | 228 | public void updateFloatingIP(OpenstackFloatingIP openstackFloatingIp) { |
| 229 | if (!floatingIpMap.containsKey(openstackFloatingIp.id())) { |
| 230 | log.warn("There`s no information about {} in FloatingIpMap", openstackFloatingIp.id()); |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 231 | return; |
| 232 | } |
Kyuhwi Choi | 5e65e2c | 2016-04-01 11:48:15 +0900 | [diff] [blame] | 233 | if (openstackFloatingIp.portId() == null || openstackFloatingIp.portId().equals("null")) { |
| 234 | OpenstackFloatingIP floatingIp = floatingIpMap.get(openstackFloatingIp.id()).value(); |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame^] | 235 | // XXX When the VM has been removed, host information has been removed or not ??? |
| 236 | Optional<Host> host = hostService.getHostsByIp(openstackFloatingIp.fixedIpAddress().getIp4Address()) |
| 237 | .stream() |
| 238 | .findFirst(); |
| 239 | if (!host.isPresent()) { |
| 240 | log.warn("No Host info with the VM IP the Floating IP address {} is found", |
| 241 | openstackFloatingIp.floatingIpAddress()); |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 242 | return; |
| 243 | } |
| 244 | l3EventExecutorService.execute( |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame^] | 245 | new OpenstackFloatingIPHandler(rulePopulator, floatingIp, Action.DISSASSOCIATE, host.get())); |
Kyuhwi Choi | 5e65e2c | 2016-04-01 11:48:15 +0900 | [diff] [blame] | 246 | floatingIpMap.replace(floatingIp.id(), openstackFloatingIp); |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame^] | 247 | registerFloatingIpToHostService(openstackFloatingIp, Action.DISSASSOCIATE); |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 248 | } else { |
Kyuhwi Choi | 5e65e2c | 2016-04-01 11:48:15 +0900 | [diff] [blame] | 249 | floatingIpMap.put(openstackFloatingIp.id(), openstackFloatingIp); |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 250 | l3EventExecutorService.execute( |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame^] | 251 | new OpenstackFloatingIPHandler(rulePopulator, openstackFloatingIp, Action.ASSOCIATE, null)); |
| 252 | registerFloatingIpToHostService(openstackFloatingIp, Action.ASSOCIATE); |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 253 | } |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame^] | 254 | |
| 255 | |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | @Override |
| 259 | public void deleteFloatingIP(String id) { |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 260 | floatingIpMap.remove(id); |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | @Override |
| 264 | public void createRouter(OpenstackRouter openstackRouter) { |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | @Override |
| 268 | public void updateRouter(OpenstackRouter openstackRouter) { |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 269 | if (openstackRouter.gatewayExternalInfo().externalFixedIps().size() > 0) { |
sangho | efbc038 | 2016-04-06 13:35:38 +0900 | [diff] [blame] | 270 | checkExternalConnection(openstackRouter, getOpenstackRouterInterface(openstackRouter)); |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 271 | } else { |
| 272 | unsetExternalConnection(); |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | private void unsetExternalConnection() { |
| 277 | Collection<OpenstackRouter> internalRouters = getExternalRouter(false); |
| 278 | internalRouters.forEach(r -> |
| 279 | getOpenstackRouterInterface(r).forEach(i -> rulePopulator.removeExternalRules(i))); |
| 280 | } |
| 281 | |
| 282 | private Collection<OpenstackRouter> getExternalRouter(boolean externalConnection) { |
| 283 | List<OpenstackRouter> routers; |
| 284 | if (externalConnection) { |
| 285 | routers = openstackService.routers() |
| 286 | .stream() |
| 287 | .filter(r -> (r.gatewayExternalInfo().externalFixedIps().size() > 0)) |
| 288 | .collect(Collectors.toList()); |
| 289 | } else { |
| 290 | routers = openstackService.routers() |
| 291 | .stream() |
| 292 | .filter(r -> (r.gatewayExternalInfo().externalFixedIps().size() == 0)) |
| 293 | .collect(Collectors.toList()); |
| 294 | } |
| 295 | return routers; |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | @Override |
| 299 | public void deleteRouter(String id) { |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 300 | //TODO : In now, there`s nothing to do for deleteRouter process. It is reserved. |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | @Override |
| 304 | public void updateRouterInterface(OpenstackRouterInterface routerInterface) { |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 305 | List<OpenstackRouterInterface> routerInterfaces = Lists.newArrayList(); |
| 306 | routerInterfaces.add(routerInterface); |
Daniel Park | 81a61a1 | 2016-02-26 08:24:44 +0900 | [diff] [blame] | 307 | checkExternalConnection(getOpenstackRouter(routerInterface.id()), routerInterfaces); |
Kyuhwi Choi | 5e65e2c | 2016-04-01 11:48:15 +0900 | [diff] [blame] | 308 | setL3Connection(getOpenstackRouter(routerInterface.id()), null); |
| 309 | routerInterfaceMap.put(routerInterface.portId(), openstackService.port(routerInterface.portId()).networkId()); |
| 310 | } |
| 311 | |
sangho | d177f8f | 2016-06-29 21:52:23 +0900 | [diff] [blame] | 312 | /** |
| 313 | * Set flow rules for traffic between two different subnets when more than one subnets |
| 314 | * connected to a router. |
| 315 | * |
| 316 | * @param openstackRouter OpenstackRouter Info |
| 317 | * @param openstackPort OpenstackPort Info |
| 318 | */ |
Kyuhwi Choi | 5e65e2c | 2016-04-01 11:48:15 +0900 | [diff] [blame] | 319 | private void setL3Connection(OpenstackRouter openstackRouter, OpenstackPort openstackPort) { |
| 320 | Collection<OpenstackRouterInterface> interfaceList = getOpenstackRouterInterface(openstackRouter); |
sangho | d177f8f | 2016-06-29 21:52:23 +0900 | [diff] [blame] | 321 | |
Kyuhwi Choi | 5e65e2c | 2016-04-01 11:48:15 +0900 | [diff] [blame] | 322 | if (interfaceList.size() < 2) { |
| 323 | return; |
| 324 | } |
| 325 | if (openstackPort == null) { |
| 326 | interfaceList.forEach(i -> { |
| 327 | OpenstackPort interfacePort = openstackService.port(i.portId()); |
| 328 | openstackService.ports() |
| 329 | .stream() |
| 330 | .filter(p -> p.networkId().equals(interfacePort.networkId()) |
| 331 | && !p.deviceOwner().equals(DEVICE_OWNER_ROUTER_INTERFACE)) |
| 332 | .forEach(p -> rulePopulator.populateL3Rules(p, |
sangho | d177f8f | 2016-06-29 21:52:23 +0900 | [diff] [blame] | 333 | getL3ConnectionList(p.networkId(), interfaceList))); |
| 334 | |
Kyuhwi Choi | 5e65e2c | 2016-04-01 11:48:15 +0900 | [diff] [blame] | 335 | }); |
| 336 | } else { |
| 337 | rulePopulator.populateL3Rules(openstackPort, getL3ConnectionList(openstackPort.networkId(), interfaceList)); |
| 338 | } |
| 339 | |
| 340 | } |
| 341 | |
| 342 | private List<OpenstackRouterInterface> getL3ConnectionList(String networkId, |
| 343 | Collection<OpenstackRouterInterface> interfaceList) { |
| 344 | List<OpenstackRouterInterface> targetList = Lists.newArrayList(); |
| 345 | interfaceList.forEach(i -> { |
| 346 | OpenstackPort port = openstackService.port(i.portId()); |
| 347 | if (!port.networkId().equals(networkId)) { |
| 348 | targetList.add(i); |
| 349 | } |
| 350 | }); |
| 351 | return targetList; |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | @Override |
| 355 | public void removeRouterInterface(OpenstackRouterInterface routerInterface) { |
Kyuhwi Choi | 5e65e2c | 2016-04-01 11:48:15 +0900 | [diff] [blame] | 356 | OpenstackRouter router = openstackService.router(routerInterface.id()); |
| 357 | Collection<OpenstackRouterInterface> interfaceList = getOpenstackRouterInterface(router); |
| 358 | if (interfaceList.size() == 1) { |
| 359 | List<OpenstackRouterInterface> newList = Lists.newArrayList(); |
| 360 | newList.add(routerInterface); |
| 361 | interfaceList.forEach(i -> removeL3RulesForRouterInterface(i, router, newList)); |
| 362 | } |
| 363 | removeL3RulesForRouterInterface(routerInterface, router, null); |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 364 | rulePopulator.removeExternalRules(routerInterface); |
Kyuhwi Choi | 5e65e2c | 2016-04-01 11:48:15 +0900 | [diff] [blame] | 365 | routerInterfaceMap.remove(routerInterface.portId()); |
| 366 | } |
| 367 | |
| 368 | private void removeL3RulesForRouterInterface(OpenstackRouterInterface routerInterface, OpenstackRouter router, |
| 369 | List<OpenstackRouterInterface> newList) { |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame^] | 370 | if (!routerInterfaceMap.containsKey(routerInterface.portId())) { |
| 371 | log.warn("No router interface information found for {}", routerInterface.portId()); |
| 372 | return; |
| 373 | } |
Kyuhwi Choi | 5e65e2c | 2016-04-01 11:48:15 +0900 | [diff] [blame] | 374 | openstackService.ports(routerInterfaceMap.get(routerInterface.portId()).value()).forEach(p -> { |
| 375 | Ip4Address vmIp = (Ip4Address) p.fixedIps().values().toArray()[0]; |
| 376 | if (newList == null) { |
| 377 | rulePopulator.removeL3Rules(vmIp, |
| 378 | getL3ConnectionList(p.networkId(), getOpenstackRouterInterface(router))); |
| 379 | } else { |
| 380 | rulePopulator.removeL3Rules(vmIp, newList); |
| 381 | } |
| 382 | } |
| 383 | ); |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 384 | } |
| 385 | |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame^] | 386 | /* |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 387 | @Override |
| 388 | public void checkDisassociatedFloatingIp(String portId, OpenstackPortInfo portInfo) { |
| 389 | if (floatingIpMap.size() < 1) { |
| 390 | log.info("No information in FloatingIpMap"); |
| 391 | return; |
| 392 | } |
| 393 | OpenstackFloatingIP floatingIp = associatedFloatingIps() |
| 394 | .stream() |
| 395 | .filter(fIp -> fIp.portId().equals(portId)) |
| 396 | .findAny() |
| 397 | .orElse(null); |
| 398 | if (floatingIp != null && portInfo != null) { |
| 399 | l3EventExecutorService.execute( |
| 400 | new OpenstackFloatingIPHandler(rulePopulator, floatingIp, false, portInfo)); |
| 401 | OpenstackFloatingIP.Builder fBuilder = new OpenstackFloatingIP.Builder() |
| 402 | .floatingIpAddress(floatingIp.floatingIpAddress()) |
| 403 | .id(floatingIp.id()) |
| 404 | .networkId(floatingIp.networkId()) |
| 405 | .status(floatingIp.status()) |
| 406 | .tenantId(floatingIp.tenantId()); |
| 407 | floatingIpMap.replace(floatingIp.id(), fBuilder.build()); |
| 408 | } else if (portInfo == null) { |
| 409 | log.warn("portInfo is null as timing issue between ovs port update event and openstack deletePort event"); |
| 410 | } |
| 411 | } |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame^] | 412 | */ |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 413 | |
Kyuhwi Choi | 5e65e2c | 2016-04-01 11:48:15 +0900 | [diff] [blame] | 414 | @Override |
| 415 | public String networkIdForRouterInterface(String portId) { |
| 416 | return routerInterfaceMap.get(portId).value(); |
| 417 | } |
| 418 | |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 419 | private Collection<OpenstackFloatingIP> associatedFloatingIps() { |
| 420 | List<OpenstackFloatingIP> fIps = Lists.newArrayList(); |
| 421 | floatingIpMap.values() |
| 422 | .stream() |
| 423 | .filter(fIp -> fIp.value().portId() != null) |
| 424 | .forEach(fIp -> fIps.add(fIp.value())); |
| 425 | return fIps; |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 426 | } |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 427 | |
| 428 | private void reloadInitL3Rules() { |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 429 | |
sangho | d177f8f | 2016-06-29 21:52:23 +0900 | [diff] [blame] | 430 | l3EventExecutorService.execute(() -> |
| 431 | openstackService.ports() |
| 432 | .stream() |
| 433 | .forEach(p -> |
| 434 | { |
| 435 | if (p.deviceOwner().equals(DEVICE_OWNER_ROUTER_INTERFACE)) { |
| 436 | updateRouterInterface(portToRouterInterface(p)); |
| 437 | } else { |
| 438 | Optional<Ip4Address> vmIp = p.fixedIps().values().stream().findAny(); |
| 439 | if (vmIp.isPresent()) { |
| 440 | OpenstackFloatingIP floatingIP = getOpenstackFloatingIp(vmIp.get()); |
| 441 | if (floatingIP != null) { |
| 442 | updateFloatingIP(floatingIP); |
| 443 | } |
| 444 | } |
| 445 | } |
| 446 | }) |
| 447 | ); |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | private OpenstackRouterInterface portToRouterInterface(OpenstackPort p) { |
| 451 | OpenstackRouterInterface.Builder osBuilder = new OpenstackRouterInterface.Builder() |
Daniel Park | 81a61a1 | 2016-02-26 08:24:44 +0900 | [diff] [blame] | 452 | .id(checkNotNull(p.deviceId())) |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 453 | .tenantId(checkNotNull(openstackService.network(p.networkId()).tenantId())) |
| 454 | .subnetId(checkNotNull(p.fixedIps().keySet().stream().findFirst().orElse(null)).toString()) |
Daniel Park | 81a61a1 | 2016-02-26 08:24:44 +0900 | [diff] [blame] | 455 | .portId(checkNotNull(p.id())); |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 456 | |
| 457 | return osBuilder.build(); |
| 458 | } |
| 459 | |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 460 | private class InternalPacketProcessor implements PacketProcessor { |
| 461 | |
| 462 | @Override |
| 463 | public void process(PacketContext context) { |
| 464 | |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame^] | 465 | DeviceId senderDeviceId = context.inPacket().receivedFrom().deviceId(); |
| 466 | if (!nodeService.routerBridge(senderDeviceId).isPresent()) { |
| 467 | log.warn("No router bridge for {} is found.", senderDeviceId); |
| 468 | return; |
| 469 | } |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 470 | if (context.isHandled()) { |
| 471 | return; |
Kyuhwi Choi | 92d9ea4 | 2016-06-13 17:28:00 +0900 | [diff] [blame] | 472 | } else if (!checkGatewayNode(context.inPacket().receivedFrom().deviceId())) { |
Daniel Park | 81a61a1 | 2016-02-26 08:24:44 +0900 | [diff] [blame] | 473 | return; |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | InboundPacket pkt = context.inPacket(); |
| 477 | Ethernet ethernet = pkt.parsed(); |
| 478 | |
Daniel Park | 81a61a1 | 2016-02-26 08:24:44 +0900 | [diff] [blame] | 479 | //TODO: Considers IPv6 later. |
| 480 | if (ethernet == null) { |
| 481 | return; |
| 482 | } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV4) { |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 483 | IPv4 iPacket = (IPv4) ethernet.getPayload(); |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 484 | switch (iPacket.getProtocol()) { |
| 485 | case IPv4.PROTOCOL_ICMP: |
Daniel Park | 81a61a1 | 2016-02-26 08:24:44 +0900 | [diff] [blame] | 486 | |
Daniel Park | 2319390 | 2016-03-24 18:17:19 +0900 | [diff] [blame] | 487 | icmpEventExecutorService.execute(() -> |
Daniel Park | 81a61a1 | 2016-02-26 08:24:44 +0900 | [diff] [blame] | 488 | openstackIcmpHandler.processIcmpPacket(context, ethernet)); |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 489 | break; |
Hyunsun Moon | 0448ea0 | 2016-02-23 14:17:39 -0800 | [diff] [blame] | 490 | case IPv4.PROTOCOL_UDP: |
| 491 | // don't process DHCP |
| 492 | UDP udpPacket = (UDP) iPacket.getPayload(); |
| 493 | if (udpPacket.getDestinationPort() == UDP.DHCP_SERVER_PORT && |
| 494 | udpPacket.getSourcePort() == UDP.DHCP_CLIENT_PORT) { |
| 495 | break; |
| 496 | } |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 497 | default: |
| 498 | int portNum = getPortNum(ethernet.getSourceMAC(), iPacket.getDestinationAddress()); |
Kyuhwi Choi | 92d9ea4 | 2016-06-13 17:28:00 +0900 | [diff] [blame] | 499 | DeviceId deviceId = pkt.receivedFrom().deviceId(); |
| 500 | Port port = null; |
| 501 | port = deviceService.getPort(deviceId, |
| 502 | gatewayService.getGatewayExternalPorts(deviceId).get(0)); |
| 503 | if (port != null) { |
Daniel Park | 2319390 | 2016-03-24 18:17:19 +0900 | [diff] [blame] | 504 | OpenstackPort openstackPort = getOpenstackPort(ethernet.getSourceMAC(), |
| 505 | Ip4Address.valueOf(iPacket.getSourceAddress())); |
| 506 | l3EventExecutorService.execute(new OpenstackPnatHandler(rulePopulator, context, |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame^] | 507 | portNum, openstackPort, port)); |
| 508 | |
Kyuhwi Choi | 5e65e2c | 2016-04-01 11:48:15 +0900 | [diff] [blame] | 509 | } else { |
| 510 | log.warn("There`s no external interface"); |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 511 | } |
Kyuhwi Choi | 92d9ea4 | 2016-06-13 17:28:00 +0900 | [diff] [blame] | 512 | |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 513 | break; |
| 514 | } |
Daniel Park | 81a61a1 | 2016-02-26 08:24:44 +0900 | [diff] [blame] | 515 | } else if (ethernet.getEtherType() == Ethernet.TYPE_ARP) { |
Daniel Park | 2319390 | 2016-03-24 18:17:19 +0900 | [diff] [blame] | 516 | arpEventExecutorService.execute(() -> |
Daniel Park | 81a61a1 | 2016-02-26 08:24:44 +0900 | [diff] [blame] | 517 | openstackArpHandler.processArpPacketFromRouter(context, ethernet)); |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 518 | } |
| 519 | } |
| 520 | |
| 521 | private int getPortNum(MacAddress sourceMac, int destinationAddress) { |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 522 | int portNum = findUnusedPortNum(); |
| 523 | if (portNum == 0) { |
| 524 | clearPortNumMap(); |
| 525 | portNum = findUnusedPortNum(); |
| 526 | } |
Daniel Park | 2319390 | 2016-03-24 18:17:19 +0900 | [diff] [blame] | 527 | tpPortNumMap.put(portNum, sourceMac.toString().concat(COLON).concat(String.valueOf(destinationAddress))); |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 528 | return portNum; |
| 529 | } |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 530 | |
| 531 | private int findUnusedPortNum() { |
| 532 | for (int i = TP_PORT_MINIMUM_NUM; i < TP_PORT_MAXIMUM_NUM; i++) { |
| 533 | if (!tpPortNumMap.containsKey(i)) { |
| 534 | return i; |
| 535 | } |
| 536 | } |
| 537 | return 0; |
| 538 | } |
| 539 | |
| 540 | } |
| 541 | |
Kyuhwi Choi | 92d9ea4 | 2016-06-13 17:28:00 +0900 | [diff] [blame] | 542 | private boolean checkGatewayNode(DeviceId deviceId) { |
| 543 | return gatewayService.getGatewayDeviceIds().contains(deviceId); |
| 544 | } |
| 545 | |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 546 | private void clearPortNumMap() { |
| 547 | tpPortNumMap.entrySet().forEach(e -> { |
| 548 | if (System.currentTimeMillis() - e.getValue().creationTime() > PNAT_PORT_EXPIRE_TIME) { |
| 549 | tpPortNumMap.remove(e.getKey()); |
| 550 | } |
| 551 | }); |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 552 | } |
| 553 | |
Daniel Park | 2319390 | 2016-03-24 18:17:19 +0900 | [diff] [blame] | 554 | private Optional<Port> getExternalPort(DeviceId deviceId, String interfaceName) { |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 555 | return deviceService.getPorts(deviceId) |
| 556 | .stream() |
| 557 | .filter(p -> p.annotations().value(PORT_NAME).equals(interfaceName)) |
Daniel Park | 2319390 | 2016-03-24 18:17:19 +0900 | [diff] [blame] | 558 | .findAny(); |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 559 | } |
| 560 | |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 561 | private void checkExternalConnection(OpenstackRouter router, |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 562 | Collection<OpenstackRouterInterface> interfaces) { |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 563 | checkNotNull(router, "Router can not be null"); |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 564 | checkNotNull(interfaces, "routerInterfaces can not be null"); |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 565 | Ip4Address externalIp = router.gatewayExternalInfo().externalFixedIps() |
| 566 | .values().stream().findFirst().orElse(null); |
| 567 | if ((externalIp == null) || (!router.gatewayExternalInfo().isEnablePnat())) { |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 568 | log.debug("Not satisfied to set pnat configuration"); |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 569 | return; |
| 570 | } |
Daniel Park | 2319390 | 2016-03-24 18:17:19 +0900 | [diff] [blame] | 571 | interfaces.forEach(this::initiateL3Rule); |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 572 | } |
| 573 | |
Daniel Park | 2319390 | 2016-03-24 18:17:19 +0900 | [diff] [blame] | 574 | private Optional<OpenstackRouter> getRouterfromExternalIp(Ip4Address externalIp) { |
| 575 | return getExternalRouter(true) |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 576 | .stream() |
| 577 | .filter(r -> r.gatewayExternalInfo() |
| 578 | .externalFixedIps() |
| 579 | .values() |
| 580 | .stream() |
Daniel Park | 2319390 | 2016-03-24 18:17:19 +0900 | [diff] [blame] | 581 | .findAny() |
| 582 | .get() |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 583 | .equals(externalIp)) |
Daniel Park | 2319390 | 2016-03-24 18:17:19 +0900 | [diff] [blame] | 584 | .findAny(); |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 585 | } |
| 586 | |
Daniel Park | 2319390 | 2016-03-24 18:17:19 +0900 | [diff] [blame] | 587 | private void initiateL3Rule(OpenstackRouterInterface routerInterface) { |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 588 | long vni = Long.parseLong(openstackService.network(openstackService |
Daniel Park | 81a61a1 | 2016-02-26 08:24:44 +0900 | [diff] [blame] | 589 | .port(routerInterface.portId()).networkId()).segmentId()); |
Daniel Park | 2319390 | 2016-03-24 18:17:19 +0900 | [diff] [blame] | 590 | rulePopulator.populateExternalRules(vni); |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | private Collection<OpenstackRouterInterface> getOpenstackRouterInterface(OpenstackRouter router) { |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 594 | List<OpenstackRouterInterface> interfaces = Lists.newArrayList(); |
| 595 | openstackService.ports() |
| 596 | .stream() |
Kyuhwi Choi | 5e65e2c | 2016-04-01 11:48:15 +0900 | [diff] [blame] | 597 | .filter(p -> p.deviceOwner().equals(DEVICE_OWNER_ROUTER_INTERFACE) |
| 598 | && p.deviceId().equals(router.id())) |
| 599 | .forEach(p -> interfaces.add(portToRouterInterface(p))); |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 600 | return interfaces; |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 601 | } |
| 602 | |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 603 | private OpenstackRouter getOpenstackRouter(String id) { |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 604 | return openstackService.routers().stream().filter(r -> |
Kyuhwi Choi | 5e65e2c | 2016-04-01 11:48:15 +0900 | [diff] [blame] | 605 | r.id().equals(id)).iterator().next(); |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | private OpenstackPort getOpenstackPort(MacAddress sourceMac, Ip4Address ip4Address) { |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 609 | OpenstackPort openstackPort = openstackService.ports().stream() |
Kyuhwi Choi | 5e65e2c | 2016-04-01 11:48:15 +0900 | [diff] [blame] | 610 | .filter(p -> p.macAddress().equals(sourceMac)).iterator().next(); |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 611 | return openstackPort.fixedIps().values().stream().filter(ip -> |
| 612 | ip.equals(ip4Address)).count() > 0 ? openstackPort : null; |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 613 | } |
| 614 | |
sangho | d177f8f | 2016-06-29 21:52:23 +0900 | [diff] [blame] | 615 | private OpenstackFloatingIP getOpenstackFloatingIp(Ip4Address vmIp) { |
| 616 | Optional<OpenstackFloatingIP> floatingIp = floatingIpMap.asJavaMap().values().stream() |
| 617 | .filter(f -> f.portId() != null && f.fixedIpAddress().equals(vmIp)) |
| 618 | .findAny(); |
| 619 | |
| 620 | if (floatingIp.isPresent()) { |
| 621 | return floatingIp.get(); |
| 622 | } |
| 623 | log.debug("There is no floating IP information for VM IP {}", vmIp); |
| 624 | |
| 625 | return null; |
| 626 | } |
| 627 | |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame^] | 628 | private Optional<OpenstackPort> getRouterInterfacePort(String networkId) { |
| 629 | |
| 630 | return openstackService.ports() |
| 631 | .stream() |
| 632 | .filter(p -> p.deviceOwner().equals(DEVICE_OWNER_ROUTER_INTERFACE) |
| 633 | && p.networkId().equals(networkId)) |
| 634 | .findAny(); |
| 635 | } |
| 636 | |
| 637 | // TODO: Remove the function and the related codes when vRouter is running on different ONOS instance. |
| 638 | private void registerFloatingIpToHostService(OpenstackFloatingIP openstackFloatingIp, Action action) { |
| 639 | |
| 640 | Optional<Host> hostOptional = hostService.getHostsByIp(openstackFloatingIp.fixedIpAddress()) |
| 641 | .stream() |
| 642 | .findFirst(); |
| 643 | if (!hostOptional.isPresent()) { |
| 644 | log.warn("No host with IP {} is registered and cannot add the floating IP. ", |
| 645 | openstackFloatingIp.floatingIpAddress()); |
Daniel Park | 81a61a1 | 2016-02-26 08:24:44 +0900 | [diff] [blame] | 646 | return; |
| 647 | } |
| 648 | |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame^] | 649 | Host host = hostOptional.get(); |
| 650 | Set<IpAddress> ipAddresses = Sets.newHashSet(); |
| 651 | if (action == Action.ASSOCIATE) { |
| 652 | ipAddresses.add(openstackFloatingIp.floatingIpAddress()); |
| 653 | } |
sangho | d177f8f | 2016-06-29 21:52:23 +0900 | [diff] [blame] | 654 | |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame^] | 655 | HostDescription hostDescription = |
| 656 | new DefaultHostDescription(host.mac(), host.vlan(), host.location(), ipAddresses, |
| 657 | (DefaultAnnotations) host.annotations()); |
sangho | d177f8f | 2016-06-29 21:52:23 +0900 | [diff] [blame] | 658 | |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame^] | 659 | hostProviderService.hostDetected(host.id(), hostDescription, false); |
Daniel Park | 81a61a1 | 2016-02-26 08:24:44 +0900 | [diff] [blame] | 660 | } |
| 661 | |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame^] | 662 | private class InternalHostListener implements HostListener { |
Daniel Park | 81a61a1 | 2016-02-26 08:24:44 +0900 | [diff] [blame] | 663 | |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame^] | 664 | private void hostDetected(Host host) { |
| 665 | |
| 666 | String portId = host.annotations().value(Constants.PORT_ID); |
| 667 | OpenstackPort openstackPort = openstackService.port(portId); |
| 668 | if (openstackPort == null) { |
| 669 | log.warn("No OpenstackPort information found from OpenStack for port ID {}", portId); |
Daniel Park | 81a61a1 | 2016-02-26 08:24:44 +0900 | [diff] [blame] | 670 | return; |
| 671 | } |
| 672 | |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame^] | 673 | Optional<OpenstackPort> routerPort = getRouterInterfacePort(openstackPort.networkId()); |
| 674 | if (routerPort.isPresent()) { |
| 675 | OpenstackRouterInterface routerInterface = portToRouterInterface(routerPort.get()); |
| 676 | l3EventExecutorService.execute(() -> |
| 677 | setL3Connection(getOpenstackRouter(routerInterface.id()), openstackPort)); |
| 678 | |
| 679 | } |
| 680 | } |
| 681 | |
| 682 | private void hostRemoved(Host host) { |
| 683 | String portId = host.annotations().value(Constants.PORT_ID); |
| 684 | OpenstackPort openstackPort = openstackService.port(portId); |
| 685 | if (openstackPort == null) { |
| 686 | log.warn("No OpenstackPort information found from OpenStack for port ID {}", portId); |
| 687 | return; |
| 688 | } |
| 689 | |
| 690 | Optional<OpenstackPort> routerPort = getRouterInterfacePort(openstackPort.networkId()); |
| 691 | if (routerPort.isPresent()) { |
| 692 | OpenstackRouterInterface routerInterface = portToRouterInterface(routerPort.get()); |
| 693 | IpAddress ipAddress = host.ipAddresses().stream().findFirst().get(); |
| 694 | l3EventExecutorService.execute(() -> rulePopulator.removeL3Rules(ipAddress.getIp4Address(), |
| 695 | getL3ConnectionList(host.annotations().value(Constants.NETWORK_ID), |
| 696 | getOpenstackRouterInterface(getOpenstackRouter(routerInterface.id()))))); |
| 697 | } |
| 698 | } |
| 699 | |
| 700 | private boolean isValidHost(Host host) { |
| 701 | return !host.ipAddresses().isEmpty() && |
| 702 | host.annotations().value(Constants.VXLAN_ID) != null && |
| 703 | host.annotations().value(Constants.NETWORK_ID) != null && |
| 704 | host.annotations().value(Constants.TENANT_ID) != null && |
| 705 | host.annotations().value(Constants.PORT_ID) != null; |
| 706 | } |
| 707 | |
| 708 | @Override |
| 709 | public void event(HostEvent event) { |
| 710 | Host host = event.subject(); |
| 711 | if (!mastershipService.isLocalMaster(host.location().deviceId())) { |
| 712 | // do not allow to proceed without mastership |
| 713 | return; |
| 714 | } |
| 715 | |
| 716 | if (!isValidHost(host)) { |
| 717 | log.debug("Invalid host event, ignore it {}", host); |
| 718 | return; |
| 719 | } |
| 720 | |
| 721 | switch (event.type()) { |
| 722 | case HOST_UPDATED: |
| 723 | case HOST_ADDED: |
| 724 | l3EventExecutorService.execute(() -> hostDetected(host)); |
| 725 | break; |
| 726 | case HOST_REMOVED: |
| 727 | l3EventExecutorService.execute(() -> hostRemoved(host)); |
| 728 | break; |
| 729 | default: |
| 730 | break; |
Daniel Park | 81a61a1 | 2016-02-26 08:24:44 +0900 | [diff] [blame] | 731 | } |
| 732 | } |
| 733 | } |
Kyuhwi Choi | 5e65e2c | 2016-04-01 11:48:15 +0900 | [diff] [blame] | 734 | |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame^] | 735 | private class InternalOpenstackNodeListener implements OpenstackNodeListener { |
| 736 | |
| 737 | private void nodeComplete() { |
| 738 | |
| 739 | rulePopulator = new OpenstackRoutingRulePopulator(appId, openstackService, flowObjectiveService, |
| 740 | deviceService, driverService, nodeService, gatewayService); |
| 741 | openstackIcmpHandler = new OpenstackIcmpHandler(packetService, deviceService, hostService, |
| 742 | openstackService, nodeService, gatewayService); |
| 743 | openstackArpHandler = new OpenstackRoutingArpHandler(packetService, openstackService, nodeService, |
| 744 | gatewayService); |
| 745 | |
| 746 | // Packet handlers must be started AFTER all initialization processes. |
| 747 | packetService.addProcessor(internalPacketProcessor, PacketProcessor.director(1)); |
| 748 | |
| 749 | openstackIcmpHandler.requestPacket(appId); |
| 750 | openstackArpHandler.requestPacket(appId); |
| 751 | |
| 752 | openstackService.floatingIps().stream() |
| 753 | .forEach(f -> floatingIpMap.put(f.id(), f)); |
| 754 | |
| 755 | reloadInitL3Rules(); |
| 756 | } |
Kyuhwi Choi | 5e65e2c | 2016-04-01 11:48:15 +0900 | [diff] [blame] | 757 | |
| 758 | @Override |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame^] | 759 | public void event(OpenstackNodeEvent event) { |
| 760 | OpenstackNode node = event.node(); |
| 761 | |
| 762 | switch (event.type()) { |
| 763 | case COMPLETE: |
| 764 | log.info("COMPLETE node {} detected", node.hostname()); |
| 765 | l3EventExecutorService.execute(() -> nodeComplete()); |
| 766 | break; |
| 767 | case INCOMPLETE: |
| 768 | break; |
| 769 | default: |
| 770 | break; |
Kyuhwi Choi | 5e65e2c | 2016-04-01 11:48:15 +0900 | [diff] [blame] | 771 | } |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame^] | 772 | |
| 773 | } |
| 774 | } |
| 775 | |
| 776 | private class InternalHostProvider extends AbstractProvider implements HostProvider { |
| 777 | |
| 778 | /** |
| 779 | * Creates a provider with the supplier identifier. |
| 780 | */ |
| 781 | protected InternalHostProvider() { |
| 782 | super(PID); |
| 783 | } |
| 784 | |
| 785 | @Override |
| 786 | public void triggerProbe(Host host) { |
| 787 | // nothing to do |
Kyuhwi Choi | 5e65e2c | 2016-04-01 11:48:15 +0900 | [diff] [blame] | 788 | } |
| 789 | } |
| 790 | |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 791 | } |