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 | |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableSet; |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 19 | import org.apache.felix.scr.annotations.Activate; |
| 20 | import org.apache.felix.scr.annotations.Component; |
| 21 | import org.apache.felix.scr.annotations.Deactivate; |
| 22 | import org.apache.felix.scr.annotations.Reference; |
| 23 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 24 | import org.apache.felix.scr.annotations.Service; |
| 25 | import org.onlab.packet.Ethernet; |
Daniel Park | a8d896c | 2016-08-26 14:31:33 +0900 | [diff] [blame] | 26 | import org.onlab.packet.IPv4; |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 27 | import org.onlab.packet.Ip4Address; |
sangho | 3d2bf9c | 2016-08-26 17:19:55 +0900 | [diff] [blame] | 28 | import org.onlab.packet.IpPrefix; |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 29 | import org.onlab.packet.MacAddress; |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 30 | import org.onlab.util.Tools; |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 31 | import org.onosproject.core.ApplicationId; |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 32 | import org.onosproject.core.GroupId; |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 33 | import org.onosproject.net.DeviceId; |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame] | 34 | import org.onosproject.net.Host; |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 35 | import org.onosproject.net.PortNumber; |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 36 | import org.onosproject.net.device.DeviceService; |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 37 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 38 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 39 | import org.onosproject.net.flow.TrafficSelector; |
| 40 | import org.onosproject.net.flow.TrafficTreatment; |
| 41 | import org.onosproject.net.flowobjective.DefaultForwardingObjective; |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 42 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 43 | import org.onosproject.net.flowobjective.ForwardingObjective; |
sangho | 93447f1 | 2016-02-24 00:33:22 +0900 | [diff] [blame] | 44 | import org.onosproject.openstackinterface.OpenstackInterfaceService; |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 45 | import org.onosproject.openstackinterface.OpenstackNetwork; |
sangho | 93447f1 | 2016-02-24 00:33:22 +0900 | [diff] [blame] | 46 | import org.onosproject.openstackinterface.OpenstackPort; |
| 47 | import org.onosproject.openstackinterface.OpenstackRouter; |
| 48 | import org.onosproject.openstackinterface.OpenstackRouterInterface; |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 49 | import org.onosproject.openstackinterface.OpenstackSubnet; |
| 50 | import org.onosproject.openstacknetworking.AbstractVmHandler; |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame] | 51 | import org.onosproject.openstacknetworking.Constants; |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 52 | import org.onosproject.openstacknetworking.OpenstackRoutingService; |
| 53 | import org.onosproject.openstacknetworking.RulePopulatorUtil; |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame] | 54 | import org.onosproject.openstacknode.OpenstackNode; |
| 55 | import org.onosproject.openstacknode.OpenstackNodeEvent; |
| 56 | import org.onosproject.openstacknode.OpenstackNodeListener; |
| 57 | import org.onosproject.openstacknode.OpenstackNodeService; |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 58 | import org.onosproject.scalablegateway.api.GatewayNode; |
| 59 | import org.onosproject.scalablegateway.api.ScalableGatewayService; |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 60 | import org.slf4j.Logger; |
| 61 | import org.slf4j.LoggerFactory; |
| 62 | |
sangho | 3d2bf9c | 2016-08-26 17:19:55 +0900 | [diff] [blame] | 63 | import java.util.HashMap; |
| 64 | import java.util.Map; |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 65 | import java.util.Objects; |
Daniel Park | 2319390 | 2016-03-24 18:17:19 +0900 | [diff] [blame] | 66 | import java.util.Optional; |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame] | 67 | import java.util.Set; |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 68 | import java.util.concurrent.ExecutorService; |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 69 | import java.util.stream.Collectors; |
| 70 | |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 71 | import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor; |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 72 | import static org.onlab.util.Tools.groupedThreads; |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 73 | import static org.onosproject.openstacknetworking.Constants.*; |
| 74 | import static org.onosproject.openstacknetworking.RulePopulatorUtil.buildExtension; |
| 75 | import static org.onosproject.openstacknode.OpenstackNodeService.NodeType.COMPUTE; |
| 76 | import static org.onosproject.openstacknode.OpenstackNodeService.NodeType.GATEWAY; |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 77 | |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 78 | @Component(immediate = true) |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 79 | @Service |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 80 | public class OpenstackRoutingManager extends AbstractVmHandler implements OpenstackRoutingService { |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 81 | |
| 82 | private final Logger log = LoggerFactory.getLogger(getClass()); |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 83 | |
| 84 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 85 | protected FlowObjectiveService flowObjectiveService; |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 86 | |
| 87 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 88 | protected DeviceService deviceService; |
| 89 | |
| 90 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
sangho | 93447f1 | 2016-02-24 00:33:22 +0900 | [diff] [blame] | 91 | protected OpenstackInterfaceService openstackService; |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 92 | |
| 93 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 94 | protected OpenstackNodeService nodeService; |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame] | 95 | |
| 96 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Kyuhwi Choi | 92d9ea4 | 2016-06-13 17:28:00 +0900 | [diff] [blame] | 97 | protected ScalableGatewayService gatewayService; |
Daniel Park | 2319390 | 2016-03-24 18:17:19 +0900 | [diff] [blame] | 98 | |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 99 | private final ExecutorService eventExecutor = newSingleThreadScheduledExecutor( |
| 100 | groupedThreads(this.getClass().getSimpleName(), "event-handler", log)); |
| 101 | private final InternalNodeListener nodeListener = new InternalNodeListener(); |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame] | 102 | |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 103 | private ApplicationId appId; |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 104 | |
| 105 | @Activate |
| 106 | protected void activate() { |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 107 | super.activate(); |
| 108 | appId = coreService.registerApplication(ROUTING_APP_ID); |
| 109 | nodeService.addListener(nodeListener); |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | @Deactivate |
| 113 | protected void deactivate() { |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 114 | nodeService.removeListener(nodeListener); |
Kyuhwi Choi | 5e65e2c | 2016-04-01 11:48:15 +0900 | [diff] [blame] | 115 | log.info("stopped"); |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 116 | } |
| 117 | |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 118 | @Override |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 119 | public void createRouter(OpenstackRouter osRouter) { |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | @Override |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 123 | public void updateRouter(OpenstackRouter osRouter) { |
| 124 | if (osRouter.gatewayExternalInfo().externalFixedIps().size() > 0) { |
| 125 | openstackService.ports().stream() |
| 126 | .filter(osPort -> osPort.deviceOwner().equals(DEVICE_OWNER_ROUTER_INTERFACE) && |
| 127 | osPort.deviceId().equals(osRouter.id())) |
sangho | 3d2bf9c | 2016-08-26 17:19:55 +0900 | [diff] [blame] | 128 | .forEach(osPort -> { |
| 129 | String subnetId = osPort.fixedIps().keySet().stream().findFirst().get(); |
| 130 | setExternalConnection(osRouter, subnetId); |
| 131 | }); |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 132 | |
| 133 | log.info("Connected external gateway {} to router {}", |
| 134 | osRouter.gatewayExternalInfo().externalFixedIps(), |
| 135 | osRouter.name()); |
| 136 | } else { |
| 137 | openstackService.ports().stream() |
| 138 | .filter(osPort -> osPort.deviceOwner().equals(DEVICE_OWNER_ROUTER_INTERFACE) && |
| 139 | osPort.deviceId().equals(osRouter.id())) |
jskim | 3d66aca | 2016-09-22 16:45:10 +0900 | [diff] [blame] | 140 | .forEach(osPort -> { |
| 141 | String subnetId = osPort.fixedIps().keySet().stream().findFirst().get(); |
| 142 | OpenstackSubnet osSubNet = openstackService.subnet(subnetId); |
| 143 | unsetExternalConnection(osRouter, osPort.networkId(), osSubNet.cidr()); |
| 144 | }); |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 145 | |
| 146 | log.info("Disconnected external gateway from router {}", |
| 147 | osRouter.name()); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public void removeRouter(String osRouterId) { |
| 153 | // TODO implement this |
| 154 | } |
| 155 | |
| 156 | @Override |
| 157 | public void addRouterInterface(OpenstackRouterInterface routerIface) { |
| 158 | OpenstackRouter osRouter = openstackRouter(routerIface.id()); |
| 159 | OpenstackPort osPort = openstackService.port(routerIface.portId()); |
| 160 | if (osRouter == null || osPort == null) { |
| 161 | log.warn("Failed to add router interface {}", routerIface); |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 162 | return; |
| 163 | } |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 164 | |
Daniel Park | a8d896c | 2016-08-26 14:31:33 +0900 | [diff] [blame] | 165 | setGatewayIcmp(Ip4Address.valueOf(openstackService.subnet(routerIface.subnetId()).gatewayIp())); |
| 166 | |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 167 | setRoutes(osRouter, Optional.empty()); |
| 168 | if (osRouter.gatewayExternalInfo().externalFixedIps().size() > 0) { |
sangho | 3d2bf9c | 2016-08-26 17:19:55 +0900 | [diff] [blame] | 169 | String subnetId = osPort.fixedIps().keySet().stream().findFirst().get(); |
| 170 | setExternalConnection(osRouter, subnetId); |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 171 | } |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 172 | log.info("Connected {} to router {}", osPort.fixedIps(), osRouter.name()); |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | @Override |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 176 | public void removeRouterInterface(OpenstackRouterInterface routerIface) { |
| 177 | OpenstackRouter osRouter = openstackService.router(routerIface.id()); |
| 178 | if (osRouter == null) { |
| 179 | log.warn("Failed to remove router interface {}", routerIface); |
Kyuhwi Choi | 5e65e2c | 2016-04-01 11:48:15 +0900 | [diff] [blame] | 180 | return; |
| 181 | } |
sangho | d177f8f | 2016-06-29 21:52:23 +0900 | [diff] [blame] | 182 | |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 183 | OpenstackSubnet osSubnet = openstackService.subnet(routerIface.subnetId()); |
| 184 | OpenstackNetwork osNet = openstackService.network(osSubnet.networkId()); |
| 185 | |
Daniel Park | a8d896c | 2016-08-26 14:31:33 +0900 | [diff] [blame] | 186 | unsetGatewayIcmp(Ip4Address.valueOf(openstackService.subnet(routerIface.subnetId()).gatewayIp())); |
| 187 | |
sangho | 3d2bf9c | 2016-08-26 17:19:55 +0900 | [diff] [blame] | 188 | unsetRoutes(osRouter, osSubnet); |
Daniel Park | a8d896c | 2016-08-26 14:31:33 +0900 | [diff] [blame] | 189 | |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 190 | if (osRouter.gatewayExternalInfo().externalFixedIps().size() > 0) { |
jskim | 3d66aca | 2016-09-22 16:45:10 +0900 | [diff] [blame] | 191 | unsetExternalConnection(osRouter, osNet.id(), osSubnet.cidr()); |
Kyuhwi Choi | 5e65e2c | 2016-04-01 11:48:15 +0900 | [diff] [blame] | 192 | } |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 193 | log.info("Disconnected {} from router {}", osSubnet.cidr(), osRouter.name()); |
Kyuhwi Choi | 5e65e2c | 2016-04-01 11:48:15 +0900 | [diff] [blame] | 194 | } |
| 195 | |
Daniel Park | a8d896c | 2016-08-26 14:31:33 +0900 | [diff] [blame] | 196 | private void setGatewayIcmp(Ip4Address gatewayIp) { |
| 197 | if (gatewayIp == null) { |
| 198 | return; |
| 199 | } |
Sho SHIMIZU | 8ebb04a | 2016-10-06 15:58:29 -0700 | [diff] [blame] | 200 | gatewayService.getGatewayDeviceIds().forEach(deviceId -> populateGatewayIcmpRule(gatewayIp, deviceId)); |
Daniel Park | a8d896c | 2016-08-26 14:31:33 +0900 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | private void populateGatewayIcmpRule(Ip4Address gatewayIp, DeviceId deviceId) { |
| 204 | TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder(); |
| 205 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 206 | |
| 207 | sBuilder.matchEthType(Ethernet.TYPE_IPV4) |
| 208 | .matchIPProtocol(IPv4.PROTOCOL_ICMP) |
| 209 | .matchIPDst(gatewayIp.toIpPrefix()); |
| 210 | |
| 211 | tBuilder.setOutput(PortNumber.CONTROLLER); |
| 212 | |
| 213 | ForwardingObjective fo = DefaultForwardingObjective.builder() |
| 214 | .withSelector(sBuilder.build()) |
| 215 | .withTreatment(tBuilder.build()) |
| 216 | .withPriority(GATEWAY_ICMP_PRIORITY) |
| 217 | .withFlag(ForwardingObjective.Flag.VERSATILE) |
| 218 | .fromApp(appId) |
| 219 | .add(); |
| 220 | |
| 221 | flowObjectiveService.forward(deviceId, fo); |
| 222 | } |
| 223 | |
| 224 | private void unsetGatewayIcmp(Ip4Address gatewayIp) { |
| 225 | if (gatewayIp == null) { |
| 226 | return; |
| 227 | } |
| 228 | gatewayService.getGatewayDeviceIds().forEach(deviceId -> { |
| 229 | removeGatewayIcmpRule(gatewayIp, deviceId); |
| 230 | }); |
| 231 | } |
| 232 | |
| 233 | private void removeGatewayIcmpRule(Ip4Address gatewayIp, DeviceId deviceId) { |
| 234 | TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder(); |
| 235 | |
| 236 | sBuilder.matchEthType(Ethernet.TYPE_IPV4) |
| 237 | .matchIPProtocol(IPv4.PROTOCOL_ICMP) |
| 238 | .matchIPDst(gatewayIp.toIpPrefix()); |
| 239 | |
| 240 | RulePopulatorUtil.removeRule(flowObjectiveService, appId, deviceId, sBuilder.build(), |
| 241 | ForwardingObjective.Flag.VERSATILE, GATEWAY_ICMP_PRIORITY); |
| 242 | |
| 243 | } |
sangho | 3d2bf9c | 2016-08-26 17:19:55 +0900 | [diff] [blame] | 244 | private void setExternalConnection(OpenstackRouter osRouter, String osSubNetId) { |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 245 | if (!osRouter.gatewayExternalInfo().isEnablePnat()) { |
| 246 | log.debug("Source NAT is disabled"); |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame] | 247 | return; |
| 248 | } |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 249 | |
sangho | 3d2bf9c | 2016-08-26 17:19:55 +0900 | [diff] [blame] | 250 | OpenstackSubnet osSubNet = openstackService.subnet(osSubNetId); |
| 251 | OpenstackNetwork osNet = openstackService.network(osSubNet.networkId()); |
| 252 | populateExternalRules(osNet, osSubNet); |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 253 | } |
| 254 | |
jskim | 3d66aca | 2016-09-22 16:45:10 +0900 | [diff] [blame] | 255 | private void unsetExternalConnection(OpenstackRouter osRouter, String osNetId, String subNetCidr) { |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 256 | if (!osRouter.gatewayExternalInfo().isEnablePnat()) { |
| 257 | log.debug("Source NAT is disabled"); |
| 258 | return; |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 259 | } |
| 260 | |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 261 | // FIXME router interface is subnet specific, not network |
| 262 | OpenstackNetwork osNet = openstackService.network(osNetId); |
jskim | 3d66aca | 2016-09-22 16:45:10 +0900 | [diff] [blame] | 263 | removeExternalRules(osNet, subNetCidr); |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 264 | } |
| 265 | |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 266 | private void setRoutes(OpenstackRouter osRouter, Optional<Host> host) { |
sangho | 3d2bf9c | 2016-08-26 17:19:55 +0900 | [diff] [blame] | 267 | Set<OpenstackSubnet> routableSubNets = routableSubNets(osRouter.id()); |
| 268 | if (routableSubNets.size() < 2) { |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 269 | // no other subnet interface is connected to this router, do nothing |
| 270 | return; |
| 271 | } |
| 272 | |
sangho | 3d2bf9c | 2016-08-26 17:19:55 +0900 | [diff] [blame] | 273 | Set<String> routableSubNetIds = routableSubNets.stream() |
| 274 | .map(OpenstackSubnet::id) |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 275 | .collect(Collectors.toSet()); |
| 276 | |
jskim | 8f27c4e | 2016-09-27 18:43:31 +0900 | [diff] [blame] | 277 | if (host.isPresent()) { |
| 278 | if (!routableSubNetIds.contains(host.get().annotations().value(SUBNET_ID))) { |
| 279 | // subnet of host is not connected to this router, do nothing. |
| 280 | return; |
| 281 | } |
| 282 | } |
sangho | 3d2bf9c | 2016-08-26 17:19:55 +0900 | [diff] [blame] | 283 | |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 284 | Set<Host> hosts = host.isPresent() ? ImmutableSet.of(host.get()) : |
| 285 | Tools.stream(hostService.getHosts()) |
sangho | 3d2bf9c | 2016-08-26 17:19:55 +0900 | [diff] [blame] | 286 | .filter(h -> routableSubNetIds.contains(h.annotations().value(SUBNET_ID))) |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 287 | .collect(Collectors.toSet()); |
| 288 | |
sangho | 3d2bf9c | 2016-08-26 17:19:55 +0900 | [diff] [blame] | 289 | hosts.forEach(h -> populateRoutingRules(h, routableSubNets)); |
Kyuhwi Choi | 92d9ea4 | 2016-06-13 17:28:00 +0900 | [diff] [blame] | 290 | } |
| 291 | |
sangho | 3d2bf9c | 2016-08-26 17:19:55 +0900 | [diff] [blame] | 292 | private void unsetRoutes(OpenstackRouter osRouter, OpenstackSubnet osSubNet) { |
| 293 | Set<OpenstackSubnet> routableSubNets = routableSubNets(osRouter.id()); |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 294 | Tools.stream(hostService.getHosts()) |
| 295 | .filter(h -> Objects.equals( |
sangho | 3d2bf9c | 2016-08-26 17:19:55 +0900 | [diff] [blame] | 296 | h.annotations().value(NETWORK_ID), osSubNet.id())) |
| 297 | .forEach(h -> removeRoutingRules(h, routableSubNets)); |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 298 | |
sangho | 3d2bf9c | 2016-08-26 17:19:55 +0900 | [diff] [blame] | 299 | routableSubNets.forEach(n -> { |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 300 | Tools.stream(hostService.getHosts()) |
| 301 | .filter(h -> Objects.equals( |
sangho | 3d2bf9c | 2016-08-26 17:19:55 +0900 | [diff] [blame] | 302 | h.annotations().value(SUBNET_ID), |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 303 | n.id())) |
sangho | 3d2bf9c | 2016-08-26 17:19:55 +0900 | [diff] [blame] | 304 | .forEach(h -> removeRoutingRules(h, ImmutableSet.of(osSubNet))); |
| 305 | log.debug("Removed between {} to {}", n.name(), osSubNet.name()); |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 306 | }); |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 307 | } |
| 308 | |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 309 | private OpenstackRouter openstackRouter(String routerId) { |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 310 | return openstackService.routers().stream().filter(r -> |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 311 | r.id().equals(routerId)).iterator().next(); |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 312 | } |
| 313 | |
jskim | 8f27c4e | 2016-09-27 18:43:31 +0900 | [diff] [blame] | 314 | private Optional<OpenstackPort> routerIfacePort(String osNetId, String osSubNetId) { |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 315 | // FIXME router interface is subnet specific, not network |
| 316 | return openstackService.ports().stream() |
| 317 | .filter(p -> p.deviceOwner().equals(DEVICE_OWNER_ROUTER_INTERFACE) && |
jskim | 8f27c4e | 2016-09-27 18:43:31 +0900 | [diff] [blame] | 318 | p.networkId().equals(osNetId) && |
| 319 | p.fixedIps().containsKey(osSubNetId)) |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame] | 320 | .findAny(); |
| 321 | } |
| 322 | |
sangho | 3d2bf9c | 2016-08-26 17:19:55 +0900 | [diff] [blame] | 323 | private Set<OpenstackSubnet> routableSubNets(String osRouterId) { |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 324 | return openstackService.ports().stream() |
| 325 | .filter(p -> p.deviceOwner().equals(DEVICE_OWNER_ROUTER_INTERFACE) && |
| 326 | p.deviceId().equals(osRouterId)) |
sangho | 3d2bf9c | 2016-08-26 17:19:55 +0900 | [diff] [blame] | 327 | .map(p -> openstackService.subnet(p.fixedIps().keySet().stream().findFirst().get())) |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 328 | .collect(Collectors.toSet()); |
| 329 | } |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame] | 330 | |
sangho | 3d2bf9c | 2016-08-26 17:19:55 +0900 | [diff] [blame] | 331 | private void populateExternalRules(OpenstackNetwork osNet, OpenstackSubnet osSubNet) { |
| 332 | populateCnodeToGateway(Long.valueOf(osNet.segmentId()), osSubNet.cidr()); |
| 333 | populateGatewayToController(Long.valueOf(osNet.segmentId()), osSubNet.cidr()); |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 334 | } |
| 335 | |
jskim | 3d66aca | 2016-09-22 16:45:10 +0900 | [diff] [blame] | 336 | private void removeExternalRules(OpenstackNetwork osNet, String subNetCidr) { |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 337 | TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder(); |
| 338 | sBuilder.matchEthType(Ethernet.TYPE_IPV4) |
| 339 | .matchTunnelId(Long.valueOf(osNet.segmentId())) |
jskim | 3d66aca | 2016-09-22 16:45:10 +0900 | [diff] [blame] | 340 | .matchIPSrc(IpPrefix.valueOf(subNetCidr)) |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 341 | .matchEthDst(Constants.DEFAULT_GATEWAY_MAC); |
| 342 | |
Sho SHIMIZU | a09e1bb | 2016-08-01 14:25:25 -0700 | [diff] [blame] | 343 | nodeService.completeNodes().forEach(node -> { |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 344 | ForwardingObjective.Flag flag = node.type().equals(GATEWAY) ? |
| 345 | ForwardingObjective.Flag.VERSATILE : |
| 346 | ForwardingObjective.Flag.SPECIFIC; |
| 347 | |
| 348 | RulePopulatorUtil.removeRule( |
| 349 | flowObjectiveService, |
| 350 | appId, |
| 351 | node.intBridge(), |
| 352 | sBuilder.build(), |
| 353 | flag, |
| 354 | ROUTING_RULE_PRIORITY); |
| 355 | }); |
| 356 | } |
| 357 | |
sangho | 3d2bf9c | 2016-08-26 17:19:55 +0900 | [diff] [blame] | 358 | private void populateRoutingRules(Host host, Set<OpenstackSubnet> osSubNets) { |
| 359 | String osSubNetId = host.annotations().value(SUBNET_ID); |
| 360 | if (osSubNetId == null) { |
Daniel Park | 81a61a1 | 2016-02-26 08:24:44 +0900 | [diff] [blame] | 361 | return; |
| 362 | } |
| 363 | |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 364 | DeviceId localDevice = host.location().deviceId(); |
| 365 | PortNumber localPort = host.location().port(); |
| 366 | if (!nodeService.dataIp(localDevice).isPresent()) { |
| 367 | log.warn("Failed to populate L3 rules"); |
| 368 | return; |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame] | 369 | } |
sangho | d177f8f | 2016-06-29 21:52:23 +0900 | [diff] [blame] | 370 | |
sangho | 3d2bf9c | 2016-08-26 17:19:55 +0900 | [diff] [blame] | 371 | Map<String, String> vniMap = new HashMap<>(); |
Sho SHIMIZU | 8ebb04a | 2016-10-06 15:58:29 -0700 | [diff] [blame] | 372 | openstackService.networks().forEach(n -> vniMap.put(n.id(), n.segmentId())); |
sangho | 3d2bf9c | 2016-08-26 17:19:55 +0900 | [diff] [blame] | 373 | |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 374 | // TODO improve pipeline, do we have to install access rules between networks |
| 375 | // for every single VMs? |
sangho | 3d2bf9c | 2016-08-26 17:19:55 +0900 | [diff] [blame] | 376 | osSubNets.stream().filter(osSubNet -> !osSubNet.id().equals(osSubNetId)).forEach(osSubNet -> { |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 377 | populateRoutingRulestoSameNode( |
| 378 | host.ipAddresses().stream().findFirst().get().getIp4Address(), |
| 379 | host.mac(), |
| 380 | localPort, localDevice, |
sangho | 3d2bf9c | 2016-08-26 17:19:55 +0900 | [diff] [blame] | 381 | Long.valueOf(vniMap.get(osSubNet.networkId())), |
| 382 | osSubNet.cidr()); |
sangho | d177f8f | 2016-06-29 21:52:23 +0900 | [diff] [blame] | 383 | |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 384 | nodeService.completeNodes().stream() |
| 385 | .filter(node -> node.type().equals(COMPUTE)) |
| 386 | .filter(node -> !node.intBridge().equals(localDevice)) |
| 387 | .forEach(node -> populateRoutingRulestoDifferentNode( |
| 388 | host.ipAddresses().stream().findFirst().get().getIp4Address(), |
sangho | 3d2bf9c | 2016-08-26 17:19:55 +0900 | [diff] [blame] | 389 | Long.valueOf(vniMap.get(osSubNet.networkId())), |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 390 | node.intBridge(), |
sangho | 3d2bf9c | 2016-08-26 17:19:55 +0900 | [diff] [blame] | 391 | nodeService.dataIp(localDevice).get().getIp4Address(), |
| 392 | osSubNet.cidr())); |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 393 | }); |
Daniel Park | 81a61a1 | 2016-02-26 08:24:44 +0900 | [diff] [blame] | 394 | } |
| 395 | |
sangho | 3d2bf9c | 2016-08-26 17:19:55 +0900 | [diff] [blame] | 396 | private void removeRoutingRules(Host host, Set<OpenstackSubnet> osSubNets) { |
| 397 | String osSubNetId = host.annotations().value(SUBNET_ID); |
| 398 | if (osSubNetId == null) { |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 399 | return; |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame] | 400 | } |
| 401 | |
sangho | 3d2bf9c | 2016-08-26 17:19:55 +0900 | [diff] [blame] | 402 | Map<String, String> vniMap = new HashMap<>(); |
Sho SHIMIZU | 8ebb04a | 2016-10-06 15:58:29 -0700 | [diff] [blame] | 403 | openstackService.networks().forEach(n -> vniMap.put(n.id(), n.segmentId())); |
sangho | 3d2bf9c | 2016-08-26 17:19:55 +0900 | [diff] [blame] | 404 | |
| 405 | osSubNets.stream().filter(osSubNet -> !osSubNet.id().equals(osSubNetId)).forEach(osSubNet -> { |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 406 | TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder(); |
| 407 | sBuilder.matchEthType(Ethernet.TYPE_IPV4) |
| 408 | .matchIPDst(host.ipAddresses().stream().findFirst().get().toIpPrefix()) |
sangho | 3d2bf9c | 2016-08-26 17:19:55 +0900 | [diff] [blame] | 409 | .matchIPSrc(IpPrefix.valueOf(osSubNet.cidr())) |
| 410 | .matchTunnelId(Long.valueOf(vniMap.get(osSubNet.networkId()))); |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame] | 411 | |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 412 | nodeService.completeNodes().stream() |
| 413 | .filter(node -> node.type().equals(COMPUTE)) |
| 414 | .forEach(node -> RulePopulatorUtil.removeRule( |
| 415 | flowObjectiveService, |
| 416 | appId, |
| 417 | node.intBridge(), |
| 418 | sBuilder.build(), |
| 419 | ForwardingObjective.Flag.SPECIFIC, |
jskim | e4e99fa | 2016-09-13 13:11:53 +0900 | [diff] [blame] | 420 | EW_ROUTING_RULE_PRIORITY)); |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 421 | }); |
sangho | 3d2bf9c | 2016-08-26 17:19:55 +0900 | [diff] [blame] | 422 | log.debug("Removed routing rule from {} to {}", host, osSubNets); |
Daniel Park | 81a61a1 | 2016-02-26 08:24:44 +0900 | [diff] [blame] | 423 | } |
Kyuhwi Choi | 5e65e2c | 2016-04-01 11:48:15 +0900 | [diff] [blame] | 424 | |
sangho | 3d2bf9c | 2016-08-26 17:19:55 +0900 | [diff] [blame] | 425 | private void populateGatewayToController(long vni, String subNetCidr) { |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 426 | TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder(); |
| 427 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame] | 428 | |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 429 | sBuilder.matchEthType(Ethernet.TYPE_IPV4) |
| 430 | .matchTunnelId(vni) |
sangho | 3d2bf9c | 2016-08-26 17:19:55 +0900 | [diff] [blame] | 431 | .matchIPSrc(IpPrefix.valueOf(subNetCidr)) |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 432 | .matchEthDst(Constants.DEFAULT_GATEWAY_MAC); |
| 433 | tBuilder.setOutput(PortNumber.CONTROLLER); |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame] | 434 | |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 435 | ForwardingObjective fo = DefaultForwardingObjective.builder() |
| 436 | .withSelector(sBuilder.build()) |
| 437 | .withTreatment(tBuilder.build()) |
| 438 | .withFlag(ForwardingObjective.Flag.VERSATILE) |
| 439 | .withPriority(ROUTING_RULE_PRIORITY) |
| 440 | .fromApp(appId) |
| 441 | .add(); |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame] | 442 | |
Sho SHIMIZU | a09e1bb | 2016-08-01 14:25:25 -0700 | [diff] [blame] | 443 | gatewayService.getGatewayDeviceIds().forEach(deviceId -> flowObjectiveService.forward(deviceId, fo)); |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 444 | } |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame] | 445 | |
sangho | 3d2bf9c | 2016-08-26 17:19:55 +0900 | [diff] [blame] | 446 | private void populateCnodeToGateway(long vni, String subnetCidr) { |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 447 | nodeService.completeNodes().stream() |
| 448 | .filter(node -> node.type().equals(COMPUTE)) |
| 449 | .forEach(node -> populateRuleToGateway( |
| 450 | node.intBridge(), |
| 451 | gatewayService.getGatewayGroupId(node.intBridge()), |
sangho | 3d2bf9c | 2016-08-26 17:19:55 +0900 | [diff] [blame] | 452 | vni, subnetCidr)); |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 453 | } |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame] | 454 | |
sangho | 3d2bf9c | 2016-08-26 17:19:55 +0900 | [diff] [blame] | 455 | private void populateRuleToGateway(DeviceId deviceId, GroupId groupId, long vni, String cidr) { |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 456 | TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder(); |
| 457 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame] | 458 | |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 459 | sBuilder.matchEthType(Ethernet.TYPE_IPV4) |
| 460 | .matchTunnelId(vni) |
sangho | 3d2bf9c | 2016-08-26 17:19:55 +0900 | [diff] [blame] | 461 | .matchIPSrc(IpPrefix.valueOf(cidr)) |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 462 | .matchEthDst(Constants.DEFAULT_GATEWAY_MAC); |
| 463 | |
| 464 | tBuilder.group(groupId); |
| 465 | ForwardingObjective fo = DefaultForwardingObjective.builder() |
| 466 | .withSelector(sBuilder.build()) |
| 467 | .withTreatment(tBuilder.build()) |
| 468 | .withFlag(ForwardingObjective.Flag.SPECIFIC) |
| 469 | .withPriority(ROUTING_RULE_PRIORITY) |
| 470 | .fromApp(appId) |
| 471 | .add(); |
| 472 | |
| 473 | flowObjectiveService.forward(deviceId, fo); |
| 474 | } |
| 475 | |
| 476 | private void populateRoutingRulestoDifferentNode(Ip4Address vmIp, long vni, |
sangho | 3d2bf9c | 2016-08-26 17:19:55 +0900 | [diff] [blame] | 477 | DeviceId deviceId, Ip4Address hostIp, |
| 478 | String cidr) { |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 479 | TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder(); |
| 480 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 481 | |
| 482 | sBuilder.matchEthType(Ethernet.TYPE_IPV4) |
| 483 | .matchTunnelId(vni) |
sangho | 3d2bf9c | 2016-08-26 17:19:55 +0900 | [diff] [blame] | 484 | .matchIPSrc(IpPrefix.valueOf(cidr)) |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 485 | .matchIPDst(vmIp.toIpPrefix()); |
| 486 | tBuilder.extension(buildExtension(deviceService, deviceId, hostIp), deviceId) |
| 487 | .setOutput(nodeService.tunnelPort(deviceId).get()); |
| 488 | |
| 489 | ForwardingObjective fo = DefaultForwardingObjective.builder() |
| 490 | .withSelector(sBuilder.build()) |
| 491 | .withTreatment(tBuilder.build()) |
sangho | 3d2bf9c | 2016-08-26 17:19:55 +0900 | [diff] [blame] | 492 | .withPriority(EW_ROUTING_RULE_PRIORITY) |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 493 | .withFlag(ForwardingObjective.Flag.SPECIFIC) |
| 494 | .fromApp(appId) |
| 495 | .add(); |
| 496 | |
| 497 | flowObjectiveService.forward(deviceId, fo); |
| 498 | } |
| 499 | |
| 500 | private void populateRoutingRulestoSameNode(Ip4Address vmIp, MacAddress vmMac, |
sangho | 3d2bf9c | 2016-08-26 17:19:55 +0900 | [diff] [blame] | 501 | PortNumber port, DeviceId deviceId, long vni, |
| 502 | String cidr) { |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 503 | TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder(); |
| 504 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 505 | |
sangho | 3d2bf9c | 2016-08-26 17:19:55 +0900 | [diff] [blame] | 506 | // FIXME: we need to check the VNI of the dest IP also just in case... |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 507 | sBuilder.matchEthType(Ethernet.TYPE_IPV4) |
| 508 | .matchIPDst(vmIp.toIpPrefix()) |
sangho | 3d2bf9c | 2016-08-26 17:19:55 +0900 | [diff] [blame] | 509 | .matchIPSrc(IpPrefix.valueOf(cidr)) |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 510 | .matchTunnelId(vni); |
| 511 | |
| 512 | tBuilder.setEthDst(vmMac) |
| 513 | .setOutput(port); |
| 514 | |
| 515 | ForwardingObjective fo = DefaultForwardingObjective.builder() |
| 516 | .withSelector(sBuilder.build()) |
| 517 | .withTreatment(tBuilder.build()) |
sangho | 3d2bf9c | 2016-08-26 17:19:55 +0900 | [diff] [blame] | 518 | .withPriority(EW_ROUTING_RULE_PRIORITY) |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 519 | .withFlag(ForwardingObjective.Flag.SPECIFIC) |
| 520 | .fromApp(appId) |
| 521 | .add(); |
| 522 | |
| 523 | flowObjectiveService.forward(deviceId, fo); |
| 524 | } |
| 525 | |
| 526 | private void reloadRoutingRules() { |
| 527 | eventExecutor.execute(() -> openstackService.ports().stream() |
| 528 | .filter(osPort -> osPort.deviceOwner().equals(DEVICE_OWNER_ROUTER_INTERFACE)) |
| 529 | .forEach(osPort -> { |
| 530 | OpenstackRouter osRouter = openstackRouter(osPort.deviceId()); |
Daniel Park | a8d896c | 2016-08-26 14:31:33 +0900 | [diff] [blame] | 531 | |
| 532 | setGatewayIcmp(Ip4Address.valueOf(openstackService |
| 533 | .subnet(osPort.fixedIps().keySet().stream().findAny().get()).gatewayIp())); |
| 534 | |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 535 | setRoutes(osRouter, Optional.empty()); |
| 536 | if (osRouter.gatewayExternalInfo().externalFixedIps().size() > 0) { |
sangho | 3d2bf9c | 2016-08-26 17:19:55 +0900 | [diff] [blame] | 537 | String subnetId = osPort.fixedIps().keySet().stream().findFirst().get(); |
| 538 | setExternalConnection(osRouter, subnetId); |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 539 | } |
| 540 | })); |
| 541 | } |
| 542 | |
| 543 | @Override |
| 544 | protected void hostDetected(Host host) { |
| 545 | String osNetId = host.annotations().value(NETWORK_ID); |
jskim | 8f27c4e | 2016-09-27 18:43:31 +0900 | [diff] [blame] | 546 | String osSubNetId = host.annotations().value(SUBNET_ID); |
| 547 | Optional<OpenstackPort> routerIface = routerIfacePort(osNetId, osSubNetId); |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 548 | if (!routerIface.isPresent()) { |
| 549 | return; |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame] | 550 | } |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 551 | eventExecutor.execute(() -> setRoutes( |
| 552 | openstackRouter(routerIface.get().deviceId()), |
| 553 | Optional.of(host))); |
| 554 | } |
| 555 | |
| 556 | @Override |
| 557 | protected void hostRemoved(Host host) { |
| 558 | String osNetId = host.annotations().value(NETWORK_ID); |
jskim | 8f27c4e | 2016-09-27 18:43:31 +0900 | [diff] [blame] | 559 | String osSubNetId = host.annotations().value(SUBNET_ID); |
| 560 | Optional<OpenstackPort> routerIface = routerIfacePort(osNetId, osSubNetId); |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 561 | if (!routerIface.isPresent()) { |
| 562 | return; |
| 563 | } |
sangho | 3d2bf9c | 2016-08-26 17:19:55 +0900 | [diff] [blame] | 564 | Set<OpenstackSubnet> routableSubNets = routableSubNets(routerIface.get().deviceId()); |
| 565 | eventExecutor.execute(() -> removeRoutingRules(host, routableSubNets)); |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 566 | } |
| 567 | |
jskim | aa85193 | 2016-10-27 17:45:30 +0900 | [diff] [blame] | 568 | @Override |
| 569 | public void reinstallVmFlow(Host host) { |
| 570 | if (host == null) { |
| 571 | hostService.getHosts().forEach(h -> { |
| 572 | hostDetected(h); |
| 573 | log.info("Re-Install data plane flow of virtual machine {}", h); |
| 574 | }); |
| 575 | } else { |
| 576 | hostDetected(host); |
| 577 | log.info("Re-Install data plane flow of virtual machine {}", host); |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | @Override |
| 582 | public void purgeVmFlow(Host host) { |
| 583 | if (host == null) { |
| 584 | hostService.getHosts().forEach(h -> { |
| 585 | hostRemoved(h); |
| 586 | log.info("Purge data plane flow of virtual machine {}", h); |
| 587 | }); |
| 588 | } else { |
| 589 | hostRemoved(host); |
| 590 | log.info("Purge data plane flow of virtual machine {}", host); |
| 591 | } |
| 592 | } |
| 593 | |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 594 | private class InternalNodeListener implements OpenstackNodeListener { |
Kyuhwi Choi | 5e65e2c | 2016-04-01 11:48:15 +0900 | [diff] [blame] | 595 | |
| 596 | @Override |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame] | 597 | public void event(OpenstackNodeEvent event) { |
| 598 | OpenstackNode node = event.node(); |
| 599 | |
| 600 | switch (event.type()) { |
| 601 | case COMPLETE: |
| 602 | log.info("COMPLETE node {} detected", node.hostname()); |
Hyunsun Moon | 5aa480b | 2016-08-03 12:23:14 -0700 | [diff] [blame] | 603 | eventExecutor.execute(() -> { |
| 604 | if (node.type() == GATEWAY) { |
| 605 | GatewayNode gnode = GatewayNode.builder() |
| 606 | .gatewayDeviceId(node.intBridge()) |
| 607 | .dataIpAddress(node.dataIp().getIp4Address()) |
| 608 | .uplinkIntf(node.externalPortName().get()) |
| 609 | .build(); |
| 610 | gatewayService.addGatewayNode(gnode); |
| 611 | } |
| 612 | reloadRoutingRules(); |
| 613 | }); |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame] | 614 | break; |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 615 | case INIT: |
| 616 | case DEVICE_CREATED: |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame] | 617 | case INCOMPLETE: |
Daniel Park | f1af068 | 2016-08-09 14:39:57 +0900 | [diff] [blame] | 618 | eventExecutor.execute(() -> { |
| 619 | if (node.type() == GATEWAY) { |
| 620 | GatewayNode gnode = GatewayNode.builder() |
| 621 | .gatewayDeviceId(node.intBridge()) |
| 622 | .dataIpAddress(node.dataIp().getIp4Address()) |
| 623 | .uplinkIntf(node.externalPortName().get()) |
| 624 | .build(); |
| 625 | gatewayService.deleteGatewayNode(gnode); |
| 626 | } |
| 627 | reloadRoutingRules(); |
| 628 | }); |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame] | 629 | default: |
| 630 | break; |
Kyuhwi Choi | 5e65e2c | 2016-04-01 11:48:15 +0900 | [diff] [blame] | 631 | } |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame] | 632 | } |
| 633 | } |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 634 | } |