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