Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +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.impl; |
| 17 | |
| 18 | import com.google.common.base.Strings; |
Jian Li | 1064e4f | 2018-05-29 16:16:53 +0900 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableSet; |
Jian Li | 99892e9 | 2018-04-13 14:59:39 +0900 | [diff] [blame] | 20 | import com.google.common.collect.Maps; |
Jian Li | 1064e4f | 2018-05-29 16:16:53 +0900 | [diff] [blame] | 21 | import com.google.common.collect.Sets; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 22 | import org.apache.felix.scr.annotations.Activate; |
| 23 | import org.apache.felix.scr.annotations.Component; |
| 24 | import org.apache.felix.scr.annotations.Deactivate; |
| 25 | import org.apache.felix.scr.annotations.Reference; |
| 26 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 27 | import org.onlab.packet.Ethernet; |
| 28 | import org.onlab.packet.IpAddress; |
| 29 | import org.onlab.packet.MacAddress; |
daniel park | ee8700b | 2017-05-11 15:50:03 +0900 | [diff] [blame] | 30 | import org.onlab.packet.VlanId; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 31 | import org.onosproject.cluster.ClusterService; |
| 32 | import org.onosproject.cluster.LeadershipService; |
| 33 | import org.onosproject.cluster.NodeId; |
| 34 | import org.onosproject.core.ApplicationId; |
| 35 | import org.onosproject.core.CoreService; |
Jian Li | 24ec59f | 2018-05-23 19:01:25 +0900 | [diff] [blame^] | 36 | import org.onosproject.net.DeviceId; |
Jian Li | 99892e9 | 2018-04-13 14:59:39 +0900 | [diff] [blame] | 37 | import org.onosproject.net.Host; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 38 | import org.onosproject.net.PortNumber; |
| 39 | import org.onosproject.net.device.DeviceService; |
| 40 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 41 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 42 | import org.onosproject.net.flow.TrafficSelector; |
| 43 | import org.onosproject.net.flow.TrafficTreatment; |
Jian Li | 99892e9 | 2018-04-13 14:59:39 +0900 | [diff] [blame] | 44 | import org.onosproject.net.host.HostEvent; |
| 45 | import org.onosproject.net.host.HostListener; |
| 46 | import org.onosproject.net.host.HostService; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 47 | import org.onosproject.openstacknetworking.api.Constants; |
daniel park | 32b4220 | 2018-03-14 16:53:44 +0900 | [diff] [blame] | 48 | import org.onosproject.openstacknetworking.api.ExternalPeerRouter; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 49 | import org.onosproject.openstacknetworking.api.InstancePort; |
Jian Li | 24ec59f | 2018-05-23 19:01:25 +0900 | [diff] [blame^] | 50 | import org.onosproject.openstacknetworking.api.InstancePortEvent; |
| 51 | import org.onosproject.openstacknetworking.api.InstancePortListener; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 52 | import org.onosproject.openstacknetworking.api.InstancePortService; |
sangho | dc37537 | 2017-06-08 10:41:30 +0900 | [diff] [blame] | 53 | import org.onosproject.openstacknetworking.api.OpenstackFlowRuleService; |
| 54 | import org.onosproject.openstacknetworking.api.OpenstackNetworkService; |
Jian Li | f3a28b0 | 2018-06-11 21:29:13 +0900 | [diff] [blame] | 55 | import org.onosproject.openstacknetworking.api.OpenstackRouterAdminService; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 56 | import org.onosproject.openstacknetworking.api.OpenstackRouterEvent; |
| 57 | import org.onosproject.openstacknetworking.api.OpenstackRouterListener; |
Hyunsun Moon | 0d45736 | 2017-06-27 17:19:41 +0900 | [diff] [blame] | 58 | import org.onosproject.openstacknode.api.OpenstackNode; |
| 59 | import org.onosproject.openstacknode.api.OpenstackNodeEvent; |
| 60 | import org.onosproject.openstacknode.api.OpenstackNodeListener; |
| 61 | import org.onosproject.openstacknode.api.OpenstackNodeService; |
daniel park | eeb8e04 | 2018-02-21 14:06:58 +0900 | [diff] [blame] | 62 | import org.openstack4j.model.network.ExternalGateway; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 63 | import org.openstack4j.model.network.NetFloatingIP; |
| 64 | import org.openstack4j.model.network.Network; |
daniel park | ee8700b | 2017-05-11 15:50:03 +0900 | [diff] [blame] | 65 | import org.openstack4j.model.network.NetworkType; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 66 | import org.openstack4j.model.network.Port; |
daniel park | eeb8e04 | 2018-02-21 14:06:58 +0900 | [diff] [blame] | 67 | import org.openstack4j.model.network.Router; |
| 68 | import org.openstack4j.model.network.RouterInterface; |
| 69 | import org.openstack4j.model.network.Subnet; |
Jian Li | f3a28b0 | 2018-06-11 21:29:13 +0900 | [diff] [blame] | 70 | import org.openstack4j.openstack.networking.domain.NeutronFloatingIP; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 71 | import org.slf4j.Logger; |
| 72 | import org.slf4j.LoggerFactory; |
| 73 | |
Jian Li | 99892e9 | 2018-04-13 14:59:39 +0900 | [diff] [blame] | 74 | import java.util.Map; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 75 | import java.util.Objects; |
Jian Li | 99892e9 | 2018-04-13 14:59:39 +0900 | [diff] [blame] | 76 | import java.util.Set; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 77 | import java.util.concurrent.ExecutorService; |
| 78 | |
| 79 | import static java.util.concurrent.Executors.newSingleThreadExecutor; |
| 80 | import static org.onlab.util.Tools.groupedThreads; |
sangho | dc37537 | 2017-06-08 10:41:30 +0900 | [diff] [blame] | 81 | import static org.onosproject.openstacknetworking.api.Constants.GW_COMMON_TABLE; |
| 82 | import static org.onosproject.openstacknetworking.api.Constants.OPENSTACK_NETWORKING_APP_ID; |
daniel park | eeb8e04 | 2018-02-21 14:06:58 +0900 | [diff] [blame] | 83 | import static org.onosproject.openstacknetworking.api.Constants.PRIORITY_EXTERNAL_FLOATING_ROUTING_RULE; |
sangho | dc37537 | 2017-06-08 10:41:30 +0900 | [diff] [blame] | 84 | import static org.onosproject.openstacknetworking.api.Constants.PRIORITY_FLOATING_EXTERNAL; |
| 85 | import static org.onosproject.openstacknetworking.api.Constants.PRIORITY_FLOATING_INTERNAL; |
daniel park | eeb8e04 | 2018-02-21 14:06:58 +0900 | [diff] [blame] | 86 | import static org.onosproject.openstacknetworking.api.Constants.ROUTING_TABLE; |
Jian Li | 99892e9 | 2018-04-13 14:59:39 +0900 | [diff] [blame] | 87 | import static org.onosproject.openstacknetworking.impl.HostBasedInstancePort.ANNOTATION_NETWORK_ID; |
| 88 | import static org.onosproject.openstacknetworking.impl.HostBasedInstancePort.ANNOTATION_PORT_ID; |
Jian Li | 24ec59f | 2018-05-23 19:01:25 +0900 | [diff] [blame^] | 89 | import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.associatedFloatingIp; |
Jian Li | 1064e4f | 2018-05-29 16:16:53 +0900 | [diff] [blame] | 90 | import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.getGwByComputeDevId; |
Jian Li | 24ec59f | 2018-05-23 19:01:25 +0900 | [diff] [blame^] | 91 | import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.isAssociatedWithVM; |
Jian Li | 2694976 | 2018-03-30 15:46:37 +0900 | [diff] [blame] | 92 | import static org.onosproject.openstacknetworking.util.RulePopulatorUtil.buildExtension; |
Hyunsun Moon | 0d45736 | 2017-06-27 17:19:41 +0900 | [diff] [blame] | 93 | import static org.onosproject.openstacknode.api.OpenstackNode.NodeType.GATEWAY; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 94 | |
| 95 | /** |
| 96 | * Handles OpenStack floating IP events. |
| 97 | */ |
| 98 | @Component(immediate = true) |
| 99 | public class OpenstackRoutingFloatingIpHandler { |
| 100 | |
| 101 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 102 | |
| 103 | private static final String ERR_FLOW = "Failed set flows for floating IP %s: "; |
Ray Milkey | c6c9b17 | 2018-02-26 09:36:31 -0800 | [diff] [blame] | 104 | private static final String ERR_UNSUPPORTED_NET_TYPE = "Unsupported network type %s"; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 105 | |
| 106 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 107 | protected CoreService coreService; |
| 108 | |
| 109 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 110 | protected DeviceService deviceService; |
| 111 | |
| 112 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 113 | protected LeadershipService leadershipService; |
| 114 | |
| 115 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 116 | protected ClusterService clusterService; |
| 117 | |
| 118 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Jian Li | 99892e9 | 2018-04-13 14:59:39 +0900 | [diff] [blame] | 119 | protected HostService hostService; |
| 120 | |
| 121 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 122 | protected OpenstackNodeService osNodeService; |
| 123 | |
| 124 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 125 | protected InstancePortService instancePortService; |
| 126 | |
| 127 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Jian Li | f3a28b0 | 2018-06-11 21:29:13 +0900 | [diff] [blame] | 128 | protected OpenstackRouterAdminService osRouterAdminService; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 129 | |
| 130 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 131 | protected OpenstackNetworkService osNetworkService; |
| 132 | |
sangho | dc37537 | 2017-06-08 10:41:30 +0900 | [diff] [blame] | 133 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 134 | protected OpenstackFlowRuleService osFlowRuleService; |
| 135 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 136 | private final ExecutorService eventExecutor = newSingleThreadExecutor( |
| 137 | groupedThreads(this.getClass().getSimpleName(), "event-handler", log)); |
Jian Li | 24ec59f | 2018-05-23 19:01:25 +0900 | [diff] [blame^] | 138 | private final OpenstackRouterListener floatingIpListener = new InternalFloatingIpListener(); |
| 139 | private final InstancePortListener instancePortListener = new InternalInstancePortListener(); |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 140 | private final OpenstackNodeListener osNodeListener = new InternalNodeListener(); |
Jian Li | 99892e9 | 2018-04-13 14:59:39 +0900 | [diff] [blame] | 141 | private final HostListener hostListener = new InternalHostListener(); |
| 142 | private Map<MacAddress, InstancePort> removedPorts = Maps.newConcurrentMap(); |
Jian Li | 24ec59f | 2018-05-23 19:01:25 +0900 | [diff] [blame^] | 143 | private Map<String, DeviceId> migrationPool = Maps.newConcurrentMap(); |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 144 | |
| 145 | private ApplicationId appId; |
| 146 | private NodeId localNodeId; |
| 147 | |
| 148 | @Activate |
| 149 | protected void activate() { |
| 150 | appId = coreService.registerApplication(OPENSTACK_NETWORKING_APP_ID); |
| 151 | localNodeId = clusterService.getLocalNode().id(); |
| 152 | leadershipService.runForLeadership(appId.name()); |
Jian Li | 99892e9 | 2018-04-13 14:59:39 +0900 | [diff] [blame] | 153 | hostService.addListener(hostListener); |
Jian Li | 24ec59f | 2018-05-23 19:01:25 +0900 | [diff] [blame^] | 154 | osRouterAdminService.addListener(floatingIpListener); |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 155 | osNodeService.addListener(osNodeListener); |
Jian Li | 24ec59f | 2018-05-23 19:01:25 +0900 | [diff] [blame^] | 156 | instancePortService.addListener(instancePortListener); |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 157 | |
| 158 | log.info("Started"); |
| 159 | } |
| 160 | |
| 161 | @Deactivate |
| 162 | protected void deactivate() { |
Jian Li | 24ec59f | 2018-05-23 19:01:25 +0900 | [diff] [blame^] | 163 | instancePortService.removeListener(instancePortListener); |
Jian Li | 99892e9 | 2018-04-13 14:59:39 +0900 | [diff] [blame] | 164 | hostService.removeListener(hostListener); |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 165 | osNodeService.removeListener(osNodeListener); |
Jian Li | 24ec59f | 2018-05-23 19:01:25 +0900 | [diff] [blame^] | 166 | osRouterAdminService.removeListener(floatingIpListener); |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 167 | leadershipService.withdraw(appId.name()); |
| 168 | eventExecutor.shutdown(); |
| 169 | |
| 170 | log.info("Stopped"); |
| 171 | } |
| 172 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 173 | private void setFloatingIpRules(NetFloatingIP floatingIp, Port osPort, |
Jian Li | 1064e4f | 2018-05-29 16:16:53 +0900 | [diff] [blame] | 174 | OpenstackNode gateway, boolean install) { |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 175 | Network osNet = osNetworkService.network(osPort.getNetworkId()); |
| 176 | if (osNet == null) { |
Jian Li | 71670d1 | 2018-03-02 21:31:07 +0900 | [diff] [blame] | 177 | final String errorFormat = ERR_FLOW + "no network(%s) exists"; |
| 178 | final String error = String.format(errorFormat, |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 179 | floatingIp.getFloatingIpAddress(), |
| 180 | osPort.getNetworkId()); |
| 181 | throw new IllegalStateException(error); |
| 182 | } |
| 183 | |
| 184 | MacAddress srcMac = MacAddress.valueOf(osPort.getMacAddress()); |
daniel park | c2a2ed6 | 2018-04-10 15:17:42 +0900 | [diff] [blame] | 185 | log.trace("Mac address of openstack port: {}", srcMac); |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 186 | InstancePort instPort = instancePortService.instancePort(srcMac); |
Jian Li | 99892e9 | 2018-04-13 14:59:39 +0900 | [diff] [blame] | 187 | |
| 188 | // sweep through removed port map |
| 189 | if (instPort == null) { |
| 190 | instPort = removedPorts.get(srcMac); |
| 191 | removedPorts.remove(srcMac); |
| 192 | } |
| 193 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 194 | if (instPort == null) { |
Jian Li | 71670d1 | 2018-03-02 21:31:07 +0900 | [diff] [blame] | 195 | final String errorFormat = ERR_FLOW + "no host(MAC:%s) found"; |
| 196 | final String error = String.format(errorFormat, |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 197 | floatingIp.getFloatingIpAddress(), srcMac); |
| 198 | throw new IllegalStateException(error); |
| 199 | } |
| 200 | |
daniel park | 32b4220 | 2018-03-14 16:53:44 +0900 | [diff] [blame] | 201 | ExternalPeerRouter externalPeerRouter = externalPeerRouter(osNet); |
| 202 | if (externalPeerRouter == null) { |
daniel park | c2a2ed6 | 2018-04-10 15:17:42 +0900 | [diff] [blame] | 203 | final String errorFormat = ERR_FLOW + "no external peer router found"; |
| 204 | throw new IllegalStateException(errorFormat); |
daniel park | 32b4220 | 2018-03-14 16:53:44 +0900 | [diff] [blame] | 205 | } |
| 206 | |
Jian Li | de67978 | 2018-06-05 01:41:29 +0900 | [diff] [blame] | 207 | updateComputeNodeRules(instPort, osNet, gateway, install); |
| 208 | updateGatewayNodeRules(floatingIp, instPort, osNet, externalPeerRouter, gateway, install); |
| 209 | |
| 210 | // FIXME: downstream internal rules are still duplicated in all gateway nodes |
| 211 | // need to make the internal rules de-duplicated sooner or later |
| 212 | setDownstreamInternalRules(floatingIp, osNet, instPort, install); |
| 213 | |
| 214 | // TODO: need to refactor setUpstreamRules if possible |
daniel park | 32b4220 | 2018-03-14 16:53:44 +0900 | [diff] [blame] | 215 | setUpstreamRules(floatingIp, osNet, instPort, externalPeerRouter, install); |
daniel park | c2a2ed6 | 2018-04-10 15:17:42 +0900 | [diff] [blame] | 216 | log.trace("Succeeded to set flow rules for floating ip {}:{} and install: {}", |
| 217 | floatingIp.getFloatingIpAddress(), |
| 218 | floatingIp.getFixedIpAddress(), |
| 219 | install); |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 220 | } |
| 221 | |
Jian Li | de67978 | 2018-06-05 01:41:29 +0900 | [diff] [blame] | 222 | private synchronized void updateGatewayNodeRules(NetFloatingIP fip, |
| 223 | InstancePort instPort, |
| 224 | Network osNet, |
| 225 | ExternalPeerRouter router, |
| 226 | OpenstackNode gateway, |
| 227 | boolean install) { |
| 228 | |
| 229 | Set<OpenstackNode> completedGws = osNodeService.completeNodes(GATEWAY); |
| 230 | Set<OpenstackNode> finalGws = Sets.newConcurrentHashSet(); |
| 231 | finalGws.addAll(ImmutableSet.copyOf(completedGws)); |
| 232 | |
Jian Li | a171a43 | 2018-06-11 11:52:11 +0900 | [diff] [blame] | 233 | |
| 234 | if (gateway == null) { |
| 235 | // these are floating IP related cases... |
| 236 | setDownstreamExternalRulesHelper(fip, osNet, instPort, router, |
| 237 | ImmutableSet.copyOf(finalGws), install); |
| 238 | |
Jian Li | de67978 | 2018-06-05 01:41:29 +0900 | [diff] [blame] | 239 | } else { |
Jian Li | a171a43 | 2018-06-11 11:52:11 +0900 | [diff] [blame] | 240 | // these are openstack node related cases... |
| 241 | if (install) { |
| 242 | if (completedGws.contains(gateway)) { |
| 243 | if (completedGws.size() > 1) { |
| 244 | finalGws.remove(gateway); |
| 245 | if (fip.getPortId() != null) { |
| 246 | setDownstreamExternalRulesHelper(fip, osNet, instPort, router, |
| 247 | ImmutableSet.copyOf(finalGws), false); |
| 248 | finalGws.add(gateway); |
| 249 | } |
| 250 | } |
Jian Li | de67978 | 2018-06-05 01:41:29 +0900 | [diff] [blame] | 251 | if (fip.getPortId() != null) { |
| 252 | setDownstreamExternalRulesHelper(fip, osNet, instPort, router, |
| 253 | ImmutableSet.copyOf(finalGws), true); |
| 254 | } |
Jian Li | a171a43 | 2018-06-11 11:52:11 +0900 | [diff] [blame] | 255 | } else { |
| 256 | log.warn("Detected node should be included in completed gateway set"); |
Jian Li | de67978 | 2018-06-05 01:41:29 +0900 | [diff] [blame] | 257 | } |
| 258 | } else { |
Jian Li | a171a43 | 2018-06-11 11:52:11 +0900 | [diff] [blame] | 259 | if (!completedGws.contains(gateway)) { |
| 260 | if (completedGws.size() >= 1) { |
| 261 | if (fip.getPortId() != null) { |
| 262 | setDownstreamExternalRulesHelper(fip, osNet, instPort, router, |
| 263 | ImmutableSet.copyOf(finalGws), true); |
| 264 | } |
| 265 | } |
| 266 | } else { |
| 267 | log.warn("Detected node should NOT be included in completed gateway set"); |
| 268 | } |
Jian Li | de67978 | 2018-06-05 01:41:29 +0900 | [diff] [blame] | 269 | } |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | private synchronized void updateComputeNodeRules(InstancePort instPort, |
| 274 | Network osNet, |
| 275 | OpenstackNode gateway, |
| 276 | boolean install) { |
Jian Li | 1064e4f | 2018-05-29 16:16:53 +0900 | [diff] [blame] | 277 | |
| 278 | Set<OpenstackNode> completedGws = osNodeService.completeNodes(GATEWAY); |
| 279 | Set<OpenstackNode> finalGws = Sets.newConcurrentHashSet(); |
| 280 | finalGws.addAll(ImmutableSet.copyOf(completedGws)); |
| 281 | |
| 282 | if (gateway == null) { |
| 283 | // these are floating IP related cases... |
| 284 | setComputeNodeToGatewayHelper(instPort, osNet, |
| 285 | ImmutableSet.copyOf(finalGws), install); |
Jian Li | de67978 | 2018-06-05 01:41:29 +0900 | [diff] [blame] | 286 | |
Jian Li | 1064e4f | 2018-05-29 16:16:53 +0900 | [diff] [blame] | 287 | } else { |
| 288 | // these are openstack node related cases... |
| 289 | if (install) { |
| 290 | if (completedGws.contains(gateway)) { |
| 291 | if (completedGws.size() > 1) { |
| 292 | finalGws.remove(gateway); |
| 293 | setComputeNodeToGatewayHelper(instPort, osNet, |
| 294 | ImmutableSet.copyOf(finalGws), false); |
| 295 | finalGws.add(gateway); |
| 296 | } |
| 297 | |
| 298 | setComputeNodeToGatewayHelper(instPort, osNet, |
| 299 | ImmutableSet.copyOf(finalGws), true); |
| 300 | } else { |
| 301 | log.warn("Detected node should be included in completed gateway set"); |
| 302 | } |
| 303 | } else { |
| 304 | if (!completedGws.contains(gateway)) { |
| 305 | finalGws.add(gateway); |
| 306 | setComputeNodeToGatewayHelper(instPort, osNet, |
| 307 | ImmutableSet.copyOf(finalGws), false); |
| 308 | finalGws.remove(gateway); |
| 309 | if (completedGws.size() >= 1) { |
| 310 | setComputeNodeToGatewayHelper(instPort, osNet, |
| 311 | ImmutableSet.copyOf(finalGws), true); |
| 312 | } |
| 313 | } else { |
| 314 | log.warn("Detected node should NOT be included in completed gateway set"); |
| 315 | } |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | // a helper method |
| 321 | private void setComputeNodeToGatewayHelper(InstancePort instPort, |
| 322 | Network osNet, |
| 323 | Set<OpenstackNode> gateways, |
| 324 | boolean install) { |
daniel park | eeb8e04 | 2018-02-21 14:06:58 +0900 | [diff] [blame] | 325 | TrafficTreatment treatment; |
| 326 | |
| 327 | TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder() |
| 328 | .matchEthType(Ethernet.TYPE_IPV4) |
| 329 | .matchIPSrc(instPort.ipAddress().toIpPrefix()) |
| 330 | .matchEthDst(Constants.DEFAULT_GATEWAY_MAC); |
| 331 | |
| 332 | switch (osNet.getNetworkType()) { |
| 333 | case VXLAN: |
| 334 | sBuilder.matchTunnelId(Long.parseLong(osNet.getProviderSegID())); |
| 335 | break; |
| 336 | case VLAN: |
| 337 | sBuilder.matchVlanId(VlanId.vlanId(osNet.getProviderSegID())); |
| 338 | break; |
| 339 | default: |
| 340 | final String error = String.format( |
Ray Milkey | c6c9b17 | 2018-02-26 09:36:31 -0800 | [diff] [blame] | 341 | ERR_UNSUPPORTED_NET_TYPE, |
daniel park | eeb8e04 | 2018-02-21 14:06:58 +0900 | [diff] [blame] | 342 | osNet.getNetworkType().toString()); |
| 343 | throw new IllegalStateException(error); |
| 344 | } |
| 345 | |
Jian Li | 1064e4f | 2018-05-29 16:16:53 +0900 | [diff] [blame] | 346 | OpenstackNode selectedGatewayNode = getGwByComputeDevId(gateways, instPort.deviceId()); |
| 347 | |
daniel park | eeb8e04 | 2018-02-21 14:06:58 +0900 | [diff] [blame] | 348 | if (selectedGatewayNode == null) { |
daniel park | c2a2ed6 | 2018-04-10 15:17:42 +0900 | [diff] [blame] | 349 | final String errorFormat = ERR_FLOW + "no gateway node selected"; |
| 350 | throw new IllegalStateException(errorFormat); |
daniel park | eeb8e04 | 2018-02-21 14:06:58 +0900 | [diff] [blame] | 351 | } |
| 352 | treatment = DefaultTrafficTreatment.builder() |
| 353 | .extension(buildExtension( |
| 354 | deviceService, |
| 355 | instPort.deviceId(), |
| 356 | selectedGatewayNode.dataIp().getIp4Address()), |
| 357 | instPort.deviceId()) |
| 358 | .setOutput(osNodeService.node(instPort.deviceId()).tunnelPortNum()) |
| 359 | .build(); |
| 360 | |
| 361 | osFlowRuleService.setRule( |
| 362 | appId, |
| 363 | instPort.deviceId(), |
| 364 | sBuilder.build(), |
| 365 | treatment, |
| 366 | PRIORITY_EXTERNAL_FLOATING_ROUTING_RULE, |
| 367 | ROUTING_TABLE, |
| 368 | install); |
daniel park | c2a2ed6 | 2018-04-10 15:17:42 +0900 | [diff] [blame] | 369 | log.trace("Succeeded to set flow rules from compute node to gateway on compute node"); |
daniel park | eeb8e04 | 2018-02-21 14:06:58 +0900 | [diff] [blame] | 370 | } |
| 371 | |
Jian Li | de67978 | 2018-06-05 01:41:29 +0900 | [diff] [blame] | 372 | private void setDownstreamInternalRules(NetFloatingIP floatingIp, |
| 373 | Network osNet, |
| 374 | InstancePort instPort, |
| 375 | boolean install) { |
Hyunsun Moon | 0d45736 | 2017-06-27 17:19:41 +0900 | [diff] [blame] | 376 | OpenstackNode cNode = osNodeService.node(instPort.deviceId()); |
| 377 | if (cNode == null) { |
| 378 | final String error = String.format("Cannot find openstack node for device %s", |
| 379 | instPort.deviceId()); |
| 380 | throw new IllegalStateException(error); |
| 381 | } |
| 382 | if (osNet.getNetworkType() == NetworkType.VXLAN && cNode.dataIp() == null) { |
Jian Li | 71670d1 | 2018-03-02 21:31:07 +0900 | [diff] [blame] | 383 | final String errorFormat = ERR_FLOW + "VXLAN mode is not ready for %s"; |
| 384 | final String error = String.format(errorFormat, floatingIp, cNode.hostname()); |
Hyunsun Moon | 0d45736 | 2017-06-27 17:19:41 +0900 | [diff] [blame] | 385 | throw new IllegalStateException(error); |
| 386 | } |
| 387 | if (osNet.getNetworkType() == NetworkType.VLAN && cNode.vlanIntf() == null) { |
Jian Li | 71670d1 | 2018-03-02 21:31:07 +0900 | [diff] [blame] | 388 | final String errorFormat = ERR_FLOW + "VLAN mode is not ready for %s"; |
| 389 | final String error = String.format(errorFormat, floatingIp, cNode.hostname()); |
Hyunsun Moon | 0d45736 | 2017-06-27 17:19:41 +0900 | [diff] [blame] | 390 | throw new IllegalStateException(error); |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | IpAddress floating = IpAddress.valueOf(floatingIp.getFloatingIpAddress()); |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 394 | |
Jian Li | de67978 | 2018-06-05 01:41:29 +0900 | [diff] [blame] | 395 | // TODO: following code snippet will be refactored sooner or later |
Hyunsun Moon | 0d45736 | 2017-06-27 17:19:41 +0900 | [diff] [blame] | 396 | osNodeService.completeNodes(GATEWAY).forEach(gNode -> { |
Hyunsun Moon | 0d45736 | 2017-06-27 17:19:41 +0900 | [diff] [blame] | 397 | // access from one VM to the others via floating IP |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 398 | TrafficSelector internalSelector = DefaultTrafficSelector.builder() |
| 399 | .matchEthType(Ethernet.TYPE_IPV4) |
| 400 | .matchIPDst(floating.toIpPrefix()) |
Hyunsun Moon | 0d45736 | 2017-06-27 17:19:41 +0900 | [diff] [blame] | 401 | .matchInPort(gNode.tunnelPortNum()) |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 402 | .build(); |
| 403 | |
daniel park | ee8700b | 2017-05-11 15:50:03 +0900 | [diff] [blame] | 404 | TrafficTreatment.Builder internalBuilder = DefaultTrafficTreatment.builder() |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 405 | .setEthSrc(Constants.DEFAULT_GATEWAY_MAC) |
| 406 | .setEthDst(instPort.macAddress()) |
daniel park | ee8700b | 2017-05-11 15:50:03 +0900 | [diff] [blame] | 407 | .setIpDst(instPort.ipAddress().getIp4Address()); |
| 408 | |
| 409 | switch (osNet.getNetworkType()) { |
| 410 | case VXLAN: |
| 411 | internalBuilder.setTunnelId(Long.valueOf(osNet.getProviderSegID())) |
| 412 | .extension(buildExtension( |
| 413 | deviceService, |
Hyunsun Moon | 0d45736 | 2017-06-27 17:19:41 +0900 | [diff] [blame] | 414 | gNode.intgBridge(), |
| 415 | cNode.dataIp().getIp4Address()), |
| 416 | gNode.intgBridge()) |
daniel park | ee8700b | 2017-05-11 15:50:03 +0900 | [diff] [blame] | 417 | .setOutput(PortNumber.IN_PORT); |
| 418 | break; |
| 419 | case VLAN: |
| 420 | internalBuilder.pushVlan() |
| 421 | .setVlanId(VlanId.vlanId(osNet.getProviderSegID())) |
| 422 | .setOutput(PortNumber.IN_PORT); |
| 423 | break; |
| 424 | default: |
Ray Milkey | c6c9b17 | 2018-02-26 09:36:31 -0800 | [diff] [blame] | 425 | final String error = String.format(ERR_UNSUPPORTED_NET_TYPE, |
Hyunsun Moon | 0d45736 | 2017-06-27 17:19:41 +0900 | [diff] [blame] | 426 | osNet.getNetworkType()); |
daniel park | ee8700b | 2017-05-11 15:50:03 +0900 | [diff] [blame] | 427 | throw new IllegalStateException(error); |
| 428 | } |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 429 | |
sangho | dc37537 | 2017-06-08 10:41:30 +0900 | [diff] [blame] | 430 | osFlowRuleService.setRule( |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 431 | appId, |
Hyunsun Moon | 0d45736 | 2017-06-27 17:19:41 +0900 | [diff] [blame] | 432 | gNode.intgBridge(), |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 433 | internalSelector, |
daniel park | ee8700b | 2017-05-11 15:50:03 +0900 | [diff] [blame] | 434 | internalBuilder.build(), |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 435 | PRIORITY_FLOATING_INTERNAL, |
sangho | dc37537 | 2017-06-08 10:41:30 +0900 | [diff] [blame] | 436 | GW_COMMON_TABLE, |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 437 | install); |
| 438 | }); |
daniel park | c2a2ed6 | 2018-04-10 15:17:42 +0900 | [diff] [blame] | 439 | log.trace("Succeeded to set flow rules for downstream on gateway nodes"); |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 440 | } |
| 441 | |
Jian Li | de67978 | 2018-06-05 01:41:29 +0900 | [diff] [blame] | 442 | private void setDownstreamExternalRulesHelper(NetFloatingIP floatingIp, |
| 443 | Network osNet, |
| 444 | InstancePort instPort, |
| 445 | ExternalPeerRouter externalPeerRouter, |
| 446 | Set<OpenstackNode> gateways, boolean install) { |
| 447 | OpenstackNode cNode = osNodeService.node(instPort.deviceId()); |
| 448 | if (cNode == null) { |
| 449 | final String error = String.format("Cannot find openstack node for device %s", |
| 450 | instPort.deviceId()); |
| 451 | throw new IllegalStateException(error); |
| 452 | } |
| 453 | if (osNet.getNetworkType() == NetworkType.VXLAN && cNode.dataIp() == null) { |
| 454 | final String errorFormat = ERR_FLOW + "VXLAN mode is not ready for %s"; |
| 455 | final String error = String.format(errorFormat, floatingIp, cNode.hostname()); |
| 456 | throw new IllegalStateException(error); |
| 457 | } |
| 458 | if (osNet.getNetworkType() == NetworkType.VLAN && cNode.vlanIntf() == null) { |
| 459 | final String errorFormat = ERR_FLOW + "VLAN mode is not ready for %s"; |
| 460 | final String error = String.format(errorFormat, floatingIp, cNode.hostname()); |
| 461 | throw new IllegalStateException(error); |
| 462 | } |
| 463 | |
| 464 | IpAddress floating = IpAddress.valueOf(floatingIp.getFloatingIpAddress()); |
| 465 | |
| 466 | OpenstackNode selectedGatewayNode = getGwByComputeDevId(gateways, instPort.deviceId()); |
| 467 | |
| 468 | if (selectedGatewayNode == null) { |
| 469 | final String errorFormat = ERR_FLOW + "no gateway node selected"; |
| 470 | throw new IllegalStateException(errorFormat); |
| 471 | } |
| 472 | |
| 473 | TrafficSelector.Builder externalSelectorBuilder = DefaultTrafficSelector.builder() |
| 474 | .matchEthType(Ethernet.TYPE_IPV4) |
| 475 | .matchIPDst(floating.toIpPrefix()); |
| 476 | |
| 477 | TrafficTreatment.Builder externalTreatmentBuilder = DefaultTrafficTreatment.builder() |
| 478 | .setEthSrc(Constants.DEFAULT_GATEWAY_MAC) |
| 479 | .setEthDst(instPort.macAddress()) |
| 480 | .setIpDst(instPort.ipAddress().getIp4Address()); |
| 481 | |
| 482 | if (!externalPeerRouter.externalPeerRouterVlanId().equals(VlanId.NONE)) { |
| 483 | externalSelectorBuilder.matchVlanId(externalPeerRouter.externalPeerRouterVlanId()).build(); |
| 484 | externalTreatmentBuilder.popVlan(); |
| 485 | } |
| 486 | |
| 487 | switch (osNet.getNetworkType()) { |
| 488 | case VXLAN: |
| 489 | externalTreatmentBuilder.setTunnelId(Long.valueOf(osNet.getProviderSegID())) |
| 490 | .extension(buildExtension( |
| 491 | deviceService, |
| 492 | selectedGatewayNode.intgBridge(), |
| 493 | cNode.dataIp().getIp4Address()), |
| 494 | selectedGatewayNode.intgBridge()) |
| 495 | .setOutput(selectedGatewayNode.tunnelPortNum()); |
| 496 | break; |
| 497 | case VLAN: |
| 498 | externalTreatmentBuilder.pushVlan() |
| 499 | .setVlanId(VlanId.vlanId(osNet.getProviderSegID())) |
| 500 | .setOutput(selectedGatewayNode.vlanPortNum()); |
| 501 | break; |
| 502 | default: |
| 503 | final String error = String.format(ERR_UNSUPPORTED_NET_TYPE, |
| 504 | osNet.getNetworkType()); |
| 505 | throw new IllegalStateException(error); |
| 506 | } |
| 507 | |
| 508 | osFlowRuleService.setRule( |
| 509 | appId, |
| 510 | selectedGatewayNode.intgBridge(), |
| 511 | externalSelectorBuilder.build(), |
| 512 | externalTreatmentBuilder.build(), |
| 513 | PRIORITY_FLOATING_EXTERNAL, |
| 514 | GW_COMMON_TABLE, |
| 515 | install); |
| 516 | } |
| 517 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 518 | private void setUpstreamRules(NetFloatingIP floatingIp, Network osNet, |
daniel park | 32b4220 | 2018-03-14 16:53:44 +0900 | [diff] [blame] | 519 | InstancePort instPort, ExternalPeerRouter externalPeerRouter, |
| 520 | boolean install) { |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 521 | IpAddress floating = IpAddress.valueOf(floatingIp.getFloatingIpAddress()); |
daniel park | ee8700b | 2017-05-11 15:50:03 +0900 | [diff] [blame] | 522 | TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder() |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 523 | .matchEthType(Ethernet.TYPE_IPV4) |
daniel park | ee8700b | 2017-05-11 15:50:03 +0900 | [diff] [blame] | 524 | .matchIPSrc(instPort.ipAddress().toIpPrefix()); |
| 525 | |
| 526 | switch (osNet.getNetworkType()) { |
| 527 | case VXLAN: |
| 528 | sBuilder.matchTunnelId(Long.valueOf(osNet.getProviderSegID())); |
| 529 | break; |
| 530 | case VLAN: |
| 531 | sBuilder.matchVlanId(VlanId.vlanId(osNet.getProviderSegID())); |
| 532 | break; |
| 533 | default: |
Ray Milkey | c6c9b17 | 2018-02-26 09:36:31 -0800 | [diff] [blame] | 534 | final String error = String.format(ERR_UNSUPPORTED_NET_TYPE, |
Hyunsun Moon | 0d45736 | 2017-06-27 17:19:41 +0900 | [diff] [blame] | 535 | osNet.getNetworkType()); |
daniel park | ee8700b | 2017-05-11 15:50:03 +0900 | [diff] [blame] | 536 | throw new IllegalStateException(error); |
| 537 | } |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 538 | |
Daniel Park | 75e3d7f | 2018-05-29 14:43:53 +0900 | [diff] [blame] | 539 | TrafficSelector selector = sBuilder.build(); |
daniel park | eeb8e04 | 2018-02-21 14:06:58 +0900 | [diff] [blame] | 540 | |
Hyunsun Moon | 0d45736 | 2017-06-27 17:19:41 +0900 | [diff] [blame] | 541 | osNodeService.completeNodes(GATEWAY).forEach(gNode -> { |
Daniel Park | 75e3d7f | 2018-05-29 14:43:53 +0900 | [diff] [blame] | 542 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder() |
| 543 | .setIpSrc(floating.getIp4Address()) |
| 544 | .setEthSrc(instPort.macAddress()) |
| 545 | .setEthDst(externalPeerRouter.externalPeerRouterMac()); |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 546 | |
Daniel Park | 75e3d7f | 2018-05-29 14:43:53 +0900 | [diff] [blame] | 547 | if (osNet.getNetworkType().equals(NetworkType.VLAN)) { |
| 548 | tBuilder.popVlan(); |
| 549 | } |
| 550 | |
| 551 | if (!externalPeerRouter.externalPeerRouterVlanId().equals(VlanId.NONE)) { |
| 552 | tBuilder.pushVlan().setVlanId(externalPeerRouter.externalPeerRouterVlanId()); |
| 553 | } |
sangho | dc37537 | 2017-06-08 10:41:30 +0900 | [diff] [blame] | 554 | osFlowRuleService.setRule( |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 555 | appId, |
Hyunsun Moon | 0d45736 | 2017-06-27 17:19:41 +0900 | [diff] [blame] | 556 | gNode.intgBridge(), |
Daniel Park | 75e3d7f | 2018-05-29 14:43:53 +0900 | [diff] [blame] | 557 | selector, |
daniel park | eeb8e04 | 2018-02-21 14:06:58 +0900 | [diff] [blame] | 558 | tBuilder.setOutput(gNode.uplinkPortNum()).build(), |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 559 | PRIORITY_FLOATING_EXTERNAL, |
sangho | dc37537 | 2017-06-08 10:41:30 +0900 | [diff] [blame] | 560 | GW_COMMON_TABLE, |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 561 | install); |
Daniel Park | 75e3d7f | 2018-05-29 14:43:53 +0900 | [diff] [blame] | 562 | }); |
daniel park | c2a2ed6 | 2018-04-10 15:17:42 +0900 | [diff] [blame] | 563 | log.trace("Succeeded to set flow rules for upstream on gateway nodes"); |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 564 | } |
| 565 | |
daniel park | 32b4220 | 2018-03-14 16:53:44 +0900 | [diff] [blame] | 566 | private ExternalPeerRouter externalPeerRouter(Network network) { |
daniel park | eeb8e04 | 2018-02-21 14:06:58 +0900 | [diff] [blame] | 567 | if (network == null) { |
| 568 | return null; |
| 569 | } |
| 570 | |
| 571 | Subnet subnet = osNetworkService.subnets(network.getId()).stream().findAny().orElse(null); |
| 572 | |
| 573 | if (subnet == null) { |
| 574 | return null; |
| 575 | } |
| 576 | |
Jian Li | f3a28b0 | 2018-06-11 21:29:13 +0900 | [diff] [blame] | 577 | RouterInterface osRouterIface = osRouterAdminService.routerInterfaces().stream() |
daniel park | eeb8e04 | 2018-02-21 14:06:58 +0900 | [diff] [blame] | 578 | .filter(i -> Objects.equals(i.getSubnetId(), subnet.getId())) |
| 579 | .findAny().orElse(null); |
| 580 | if (osRouterIface == null) { |
| 581 | return null; |
| 582 | } |
| 583 | |
Jian Li | f3a28b0 | 2018-06-11 21:29:13 +0900 | [diff] [blame] | 584 | Router osRouter = osRouterAdminService.router(osRouterIface.getId()); |
daniel park | eeb8e04 | 2018-02-21 14:06:58 +0900 | [diff] [blame] | 585 | if (osRouter == null) { |
| 586 | return null; |
| 587 | } |
| 588 | if (osRouter.getExternalGatewayInfo() == null) { |
| 589 | return null; |
| 590 | } |
| 591 | |
| 592 | ExternalGateway exGatewayInfo = osRouter.getExternalGatewayInfo(); |
daniel park | 65e1c20 | 2018-04-03 13:15:28 +0900 | [diff] [blame] | 593 | return osNetworkService.externalPeerRouter(exGatewayInfo); |
daniel park | eeb8e04 | 2018-02-21 14:06:58 +0900 | [diff] [blame] | 594 | } |
daniel park | 65e1c20 | 2018-04-03 13:15:28 +0900 | [diff] [blame] | 595 | |
Jian Li | 99892e9 | 2018-04-13 14:59:39 +0900 | [diff] [blame] | 596 | private void associateFloatingIp(NetFloatingIP osFip) { |
| 597 | Port osPort = osNetworkService.port(osFip.getPortId()); |
| 598 | if (osPort == null) { |
| 599 | final String errorFormat = ERR_FLOW + "port(%s) not found"; |
| 600 | final String error = String.format(errorFormat, |
| 601 | osFip.getFloatingIpAddress(), osFip.getPortId()); |
| 602 | throw new IllegalStateException(error); |
| 603 | } |
| 604 | // set floating IP rules only if the port is associated to a VM |
| 605 | if (!Strings.isNullOrEmpty(osPort.getDeviceId())) { |
Jian Li | 1064e4f | 2018-05-29 16:16:53 +0900 | [diff] [blame] | 606 | setFloatingIpRules(osFip, osPort, null, true); |
Jian Li | 99892e9 | 2018-04-13 14:59:39 +0900 | [diff] [blame] | 607 | } |
| 608 | } |
| 609 | |
| 610 | private void disassociateFloatingIp(NetFloatingIP osFip, String portId) { |
| 611 | Port osPort = osNetworkService.port(portId); |
| 612 | if (osPort == null) { |
| 613 | // FIXME when a port with floating IP removed without |
| 614 | // disassociation step, it can reach here |
| 615 | return; |
| 616 | } |
| 617 | // set floating IP rules only if the port is associated to a VM |
| 618 | if (!Strings.isNullOrEmpty(osPort.getDeviceId())) { |
Jian Li | 1064e4f | 2018-05-29 16:16:53 +0900 | [diff] [blame] | 619 | setFloatingIpRules(osFip, osPort, null, false); |
Jian Li | 99892e9 | 2018-04-13 14:59:39 +0900 | [diff] [blame] | 620 | } |
| 621 | } |
| 622 | |
Hyunsun Moon | 0d45736 | 2017-06-27 17:19:41 +0900 | [diff] [blame] | 623 | private class InternalFloatingIpListener implements OpenstackRouterListener { |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 624 | |
| 625 | @Override |
| 626 | public boolean isRelevant(OpenstackRouterEvent event) { |
| 627 | // do not allow to proceed without leadership |
| 628 | NodeId leader = leadershipService.getLeader(appId.name()); |
| 629 | if (!Objects.equals(localNodeId, leader)) { |
| 630 | return false; |
| 631 | } |
| 632 | return event.floatingIp() != null; |
| 633 | } |
| 634 | |
| 635 | @Override |
| 636 | public void event(OpenstackRouterEvent event) { |
| 637 | switch (event.type()) { |
| 638 | case OPENSTACK_FLOATING_IP_ASSOCIATED: |
Hyunsun Moon | 7a5f904 | 2017-05-11 18:19:01 +0900 | [diff] [blame] | 639 | eventExecutor.execute(() -> { |
Hyunsun Moon | b720e63 | 2017-05-16 15:41:36 +0900 | [diff] [blame] | 640 | NetFloatingIP osFip = event.floatingIp(); |
| 641 | associateFloatingIp(osFip); |
| 642 | log.info("Associated floating IP {}:{}", |
| 643 | osFip.getFloatingIpAddress(), osFip.getFixedIpAddress()); |
Hyunsun Moon | 7a5f904 | 2017-05-11 18:19:01 +0900 | [diff] [blame] | 644 | }); |
| 645 | break; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 646 | case OPENSTACK_FLOATING_IP_DISASSOCIATED: |
| 647 | eventExecutor.execute(() -> { |
Hyunsun Moon | b720e63 | 2017-05-16 15:41:36 +0900 | [diff] [blame] | 648 | NetFloatingIP osFip = event.floatingIp(); |
| 649 | disassociateFloatingIp(osFip, event.portId()); |
| 650 | log.info("Disassociated floating IP {}:{}", |
| 651 | osFip.getFloatingIpAddress(), osFip.getFixedIpAddress()); |
Hyunsun Moon | 7a5f904 | 2017-05-11 18:19:01 +0900 | [diff] [blame] | 652 | }); |
| 653 | break; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 654 | case OPENSTACK_FLOATING_IP_CREATED: |
Hyunsun Moon | b720e63 | 2017-05-16 15:41:36 +0900 | [diff] [blame] | 655 | eventExecutor.execute(() -> { |
| 656 | NetFloatingIP osFip = event.floatingIp(); |
| 657 | if (!Strings.isNullOrEmpty(osFip.getPortId())) { |
| 658 | associateFloatingIp(event.floatingIp()); |
| 659 | } |
| 660 | log.info("Created floating IP {}", osFip.getFloatingIpAddress()); |
| 661 | }); |
| 662 | break; |
Jian Li | a171a43 | 2018-06-11 11:52:11 +0900 | [diff] [blame] | 663 | case OPENSTACK_FLOATING_IP_REMOVED: |
| 664 | eventExecutor.execute(() -> { |
| 665 | NetFloatingIP osFip = event.floatingIp(); |
| 666 | if (!Strings.isNullOrEmpty(osFip.getPortId())) { |
| 667 | disassociateFloatingIp(osFip, osFip.getPortId()); |
| 668 | } |
| 669 | log.info("Removed floating IP {}", osFip.getFloatingIpAddress()); |
| 670 | }); |
| 671 | break; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 672 | case OPENSTACK_FLOATING_IP_UPDATED: |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 673 | case OPENSTACK_ROUTER_CREATED: |
| 674 | case OPENSTACK_ROUTER_UPDATED: |
| 675 | case OPENSTACK_ROUTER_REMOVED: |
| 676 | case OPENSTACK_ROUTER_INTERFACE_ADDED: |
| 677 | case OPENSTACK_ROUTER_INTERFACE_UPDATED: |
| 678 | case OPENSTACK_ROUTER_INTERFACE_REMOVED: |
| 679 | default: |
| 680 | // do nothing for the other events |
| 681 | break; |
| 682 | } |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | private class InternalNodeListener implements OpenstackNodeListener { |
| 687 | |
| 688 | @Override |
| 689 | public boolean isRelevant(OpenstackNodeEvent event) { |
| 690 | // do not allow to proceed without leadership |
| 691 | NodeId leader = leadershipService.getLeader(appId.name()); |
| 692 | if (!Objects.equals(localNodeId, leader)) { |
| 693 | return false; |
| 694 | } |
| 695 | return event.subject().type() == GATEWAY; |
| 696 | } |
| 697 | |
| 698 | @Override |
| 699 | public void event(OpenstackNodeEvent event) { |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 700 | |
| 701 | switch (event.type()) { |
Hyunsun Moon | 0d45736 | 2017-06-27 17:19:41 +0900 | [diff] [blame] | 702 | case OPENSTACK_NODE_COMPLETE: |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 703 | eventExecutor.execute(() -> { |
Jian Li | f3a28b0 | 2018-06-11 21:29:13 +0900 | [diff] [blame] | 704 | for (NetFloatingIP fip : osRouterAdminService.floatingIps()) { |
Hyunsun Moon | 7a5f904 | 2017-05-11 18:19:01 +0900 | [diff] [blame] | 705 | if (Strings.isNullOrEmpty(fip.getPortId())) { |
| 706 | continue; |
| 707 | } |
| 708 | Port osPort = osNetworkService.port(fip.getPortId()); |
| 709 | if (osPort == null) { |
| 710 | log.warn("Failed to set floating IP {}", fip.getId()); |
| 711 | continue; |
| 712 | } |
Jian Li | 1064e4f | 2018-05-29 16:16:53 +0900 | [diff] [blame] | 713 | setFloatingIpRules(fip, osPort, event.subject(), true); |
Hyunsun Moon | 7a5f904 | 2017-05-11 18:19:01 +0900 | [diff] [blame] | 714 | } |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 715 | }); |
| 716 | break; |
Hyunsun Moon | 0d45736 | 2017-06-27 17:19:41 +0900 | [diff] [blame] | 717 | case OPENSTACK_NODE_INCOMPLETE: |
Jian Li | 1064e4f | 2018-05-29 16:16:53 +0900 | [diff] [blame] | 718 | eventExecutor.execute(() -> { |
Jian Li | f3a28b0 | 2018-06-11 21:29:13 +0900 | [diff] [blame] | 719 | for (NetFloatingIP fip : osRouterAdminService.floatingIps()) { |
Jian Li | 1064e4f | 2018-05-29 16:16:53 +0900 | [diff] [blame] | 720 | if (Strings.isNullOrEmpty(fip.getPortId())) { |
| 721 | continue; |
| 722 | } |
| 723 | Port osPort = osNetworkService.port(fip.getPortId()); |
| 724 | if (osPort == null) { |
| 725 | log.warn("Failed to set floating IP {}", fip.getId()); |
| 726 | continue; |
| 727 | } |
| 728 | Network osNet = osNetworkService.network(osPort.getNetworkId()); |
| 729 | if (osNet == null) { |
| 730 | final String errorFormat = ERR_FLOW + "no network(%s) exists"; |
| 731 | final String error = String.format(errorFormat, |
| 732 | fip.getFloatingIpAddress(), |
| 733 | osPort.getNetworkId()); |
| 734 | throw new IllegalStateException(error); |
| 735 | } |
| 736 | MacAddress srcMac = MacAddress.valueOf(osPort.getMacAddress()); |
| 737 | log.trace("Mac address of openstack port: {}", srcMac); |
| 738 | InstancePort instPort = instancePortService.instancePort(srcMac); |
| 739 | |
| 740 | if (instPort == null) { |
| 741 | final String errorFormat = ERR_FLOW + "no host(MAC:%s) found"; |
| 742 | final String error = String.format(errorFormat, |
| 743 | fip.getFloatingIpAddress(), srcMac); |
| 744 | throw new IllegalStateException(error); |
| 745 | } |
| 746 | |
Jian Li | de67978 | 2018-06-05 01:41:29 +0900 | [diff] [blame] | 747 | ExternalPeerRouter externalPeerRouter = externalPeerRouter(osNet); |
| 748 | if (externalPeerRouter == null) { |
| 749 | final String errorFormat = ERR_FLOW + "no external peer router found"; |
| 750 | throw new IllegalStateException(errorFormat); |
| 751 | } |
| 752 | |
| 753 | updateComputeNodeRules(instPort, osNet, event.subject(), false); |
| 754 | updateGatewayNodeRules(fip, instPort, osNet, |
| 755 | externalPeerRouter, event.subject(), false); |
Jian Li | 1064e4f | 2018-05-29 16:16:53 +0900 | [diff] [blame] | 756 | } |
| 757 | }); |
| 758 | break; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 759 | default: |
Hyunsun Moon | 0d45736 | 2017-06-27 17:19:41 +0900 | [diff] [blame] | 760 | // do nothing |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 761 | break; |
| 762 | } |
| 763 | } |
| 764 | } |
Jian Li | 99892e9 | 2018-04-13 14:59:39 +0900 | [diff] [blame] | 765 | |
| 766 | private class InternalHostListener implements HostListener { |
| 767 | |
| 768 | @Override |
| 769 | public boolean isRelevant(HostEvent event) { |
| 770 | Host host = event.subject(); |
| 771 | if (!isValidHost(host)) { |
| 772 | log.debug("Invalid host detected, ignore it {}", host); |
| 773 | return false; |
| 774 | } |
| 775 | return true; |
| 776 | } |
| 777 | |
| 778 | @Override |
| 779 | public void event(HostEvent event) { |
| 780 | InstancePort instPort = HostBasedInstancePort.of(event.subject()); |
| 781 | switch (event.type()) { |
| 782 | case HOST_REMOVED: |
| 783 | storeTempInstPort(instPort); |
| 784 | break; |
| 785 | case HOST_UPDATED: |
| 786 | case HOST_ADDED: |
| 787 | default: |
| 788 | break; |
| 789 | } |
| 790 | } |
| 791 | |
| 792 | private void storeTempInstPort(InstancePort port) { |
Jian Li | f3a28b0 | 2018-06-11 21:29:13 +0900 | [diff] [blame] | 793 | Set<NetFloatingIP> ips = osRouterAdminService.floatingIps(); |
Jian Li | 99892e9 | 2018-04-13 14:59:39 +0900 | [diff] [blame] | 794 | for (NetFloatingIP fip : ips) { |
| 795 | if (Strings.isNullOrEmpty(fip.getFixedIpAddress())) { |
| 796 | continue; |
| 797 | } |
| 798 | if (Strings.isNullOrEmpty(fip.getFloatingIpAddress())) { |
| 799 | continue; |
| 800 | } |
| 801 | if (fip.getFixedIpAddress().equals(port.ipAddress().toString())) { |
| 802 | removedPorts.put(port.macAddress(), port); |
Jian Li | f3a28b0 | 2018-06-11 21:29:13 +0900 | [diff] [blame] | 803 | NeutronFloatingIP neutronFip = (NeutronFloatingIP) fip; |
| 804 | // invalidate bound fixed IP and port |
| 805 | neutronFip.setFixedIpAddress(null); |
| 806 | neutronFip.setPortId(null); |
| 807 | osRouterAdminService.updateFloatingIp(neutronFip); |
| 808 | log.info("Updated floating IP {}, due to host removal", |
| 809 | neutronFip.getFloatingIpAddress()); |
Jian Li | 99892e9 | 2018-04-13 14:59:39 +0900 | [diff] [blame] | 810 | } |
| 811 | } |
| 812 | } |
| 813 | |
| 814 | private boolean isValidHost(Host host) { |
| 815 | return !host.ipAddresses().isEmpty() && |
| 816 | host.annotations().value(ANNOTATION_NETWORK_ID) != null && |
| 817 | host.annotations().value(ANNOTATION_PORT_ID) != null; |
| 818 | } |
| 819 | } |
Jian Li | 24ec59f | 2018-05-23 19:01:25 +0900 | [diff] [blame^] | 820 | |
| 821 | private class InternalInstancePortListener implements InstancePortListener { |
| 822 | |
| 823 | @Override |
| 824 | public boolean isRelevant(InstancePortEvent event) { |
| 825 | Set<NetFloatingIP> ips = osRouterAdminService.floatingIps(); |
| 826 | NetFloatingIP fip = associatedFloatingIp(event.subject(), ips); |
| 827 | |
| 828 | return fip != null && isAssociatedWithVM(osNetworkService, fip); |
| 829 | } |
| 830 | |
| 831 | @Override |
| 832 | public void event(InstancePortEvent event) { |
| 833 | |
| 834 | Set<NetFloatingIP> ips = osRouterAdminService.floatingIps(); |
| 835 | NetFloatingIP fip = associatedFloatingIp(event.subject(), ips); |
| 836 | Port osPort = osNetworkService.port(fip.getPortId()); |
| 837 | Network osNet = osNetworkService.network(osPort.getNetworkId()); |
| 838 | Set<OpenstackNode> gateways = osNodeService.completeNodes(GATEWAY); |
| 839 | |
| 840 | ExternalPeerRouter externalPeerRouter = externalPeerRouter(osNet); |
| 841 | if (externalPeerRouter == null) { |
| 842 | final String errorFormat = ERR_FLOW + "no external peer router found"; |
| 843 | throw new IllegalStateException(errorFormat); |
| 844 | } |
| 845 | |
| 846 | switch (event.type()) { |
| 847 | case OPENSTACK_INSTANCE_MIGRATION_STARTED: |
| 848 | eventExecutor.execute(() -> { |
| 849 | |
| 850 | // since downstream internal rules are located in all gateway |
| 851 | // nodes, therefore, we simply update the rules with new compute node info |
| 852 | setDownstreamInternalRules(fip, osNet, event.subject(), true); |
| 853 | |
| 854 | // since DownstreamExternal rules should only be placed in |
| 855 | // corresponding gateway node, we need to install new rule to |
| 856 | // the corresponding gateway node |
| 857 | setDownstreamExternalRulesHelper(fip, osNet, |
| 858 | event.subject(), externalPeerRouter, gateways, true); |
| 859 | |
| 860 | // since ComputeNodeToGateway rules should only be placed in |
| 861 | // corresponding compute node, we need to install new rule to |
| 862 | // the target compute node, and remove rules from original node |
| 863 | setComputeNodeToGatewayHelper(event.subject(), osNet, gateways, true); |
| 864 | |
| 865 | migrationPool.put(fip.getFloatingIpAddress(), event.subject().deviceId()); |
| 866 | |
| 867 | }); |
| 868 | break; |
| 869 | case OPENSTACK_INSTANCE_MIGRATION_ENDED: |
| 870 | |
| 871 | // if we only have one gateway, we simply do not remove any |
| 872 | // flow rules from either gateway or compute node |
| 873 | if (gateways.size() == 1) { |
| 874 | return; |
| 875 | } |
| 876 | |
| 877 | // checks whether the destination compute node's device id |
| 878 | // has identical gateway hash or not |
| 879 | // if it is true, we simply do not remove the rules, as |
| 880 | // it has been overwritten at port detention event |
| 881 | // if it is false, we will remove the rules |
| 882 | DeviceId newDeviceId = migrationPool.get(fip.getFloatingIpAddress()); |
| 883 | DeviceId oldDeviceId = event.subject().deviceId(); |
| 884 | migrationPool.remove(fip.getFloatingIpAddress()); |
| 885 | |
| 886 | OpenstackNode oldGateway = getGwByComputeDevId(gateways, oldDeviceId); |
| 887 | OpenstackNode newGateway = getGwByComputeDevId(gateways, newDeviceId); |
| 888 | |
| 889 | if (oldGateway != null && oldGateway.equals(newGateway)) { |
| 890 | return; |
| 891 | } |
| 892 | |
| 893 | eventExecutor.execute(() -> { |
| 894 | |
| 895 | // we need to remove the old ComputeNodeToGateway rules from |
| 896 | // original compute node |
| 897 | setComputeNodeToGatewayHelper(event.subject(), osNet, gateways, false); |
| 898 | |
| 899 | // since DownstreamExternal rules should only be placed in |
| 900 | // corresponding gateway node, we need to remove old rule from |
| 901 | // the corresponding gateway node |
| 902 | setDownstreamExternalRulesHelper(fip, osNet, |
| 903 | event.subject(), externalPeerRouter, gateways, false); |
| 904 | }); |
| 905 | break; |
| 906 | default: |
| 907 | break; |
| 908 | } |
| 909 | } |
| 910 | } |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 911 | } |