sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.openstacknetworking.routing; |
| 17 | |
| 18 | import org.onlab.packet.Ethernet; |
| 19 | import org.onlab.packet.IPv4; |
| 20 | import org.onlab.packet.Ip4Address; |
| 21 | import org.onlab.packet.IpAddress; |
| 22 | import org.onlab.packet.IpPrefix; |
| 23 | import org.onlab.packet.MacAddress; |
| 24 | import org.onlab.packet.TCP; |
| 25 | import org.onlab.packet.TpPort; |
| 26 | import org.onlab.packet.UDP; |
| 27 | import org.onosproject.core.ApplicationId; |
| 28 | import org.onosproject.net.Device; |
| 29 | import org.onosproject.net.DeviceId; |
| 30 | import org.onosproject.net.Port; |
| 31 | import org.onosproject.net.PortNumber; |
| 32 | import org.onosproject.net.behaviour.ExtensionTreatmentResolver; |
| 33 | import org.onosproject.net.device.DeviceService; |
| 34 | import org.onosproject.net.driver.DefaultDriverData; |
| 35 | import org.onosproject.net.driver.DefaultDriverHandler; |
| 36 | import org.onosproject.net.driver.Driver; |
| 37 | import org.onosproject.net.driver.DriverHandler; |
| 38 | import org.onosproject.net.driver.DriverService; |
| 39 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 40 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 41 | import org.onosproject.net.flow.TrafficSelector; |
| 42 | import org.onosproject.net.flow.TrafficTreatment; |
| 43 | import org.onosproject.net.flow.instructions.ExtensionPropertyException; |
| 44 | import org.onosproject.net.flow.instructions.ExtensionTreatment; |
| 45 | import org.onosproject.net.flow.instructions.ExtensionTreatmentType; |
| 46 | import org.onosproject.net.flowobjective.DefaultForwardingObjective; |
| 47 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
| 48 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 49 | import org.onosproject.net.packet.InboundPacket; |
sangho | 93447f1 | 2016-02-24 00:33:22 +0900 | [diff] [blame] | 50 | import org.onosproject.openstackinterface.OpenstackInterfaceService; |
| 51 | import org.onosproject.openstackinterface.OpenstackPort; |
| 52 | import org.onosproject.openstackinterface.OpenstackRouter; |
| 53 | import org.onosproject.openstackinterface.OpenstackRouterInterface; |
| 54 | import org.onosproject.openstackinterface.OpenstackSubnet; |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame^] | 55 | import org.onosproject.openstackinterface.OpenstackFloatingIP; |
| 56 | import org.onosproject.openstacknetworking.OpenstackPortInfo; |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 57 | import org.slf4j.Logger; |
| 58 | import org.slf4j.LoggerFactory; |
| 59 | |
| 60 | import java.util.stream.StreamSupport; |
| 61 | |
| 62 | import static com.google.common.base.Preconditions.checkNotNull; |
| 63 | |
| 64 | /** |
| 65 | * Populates Routing Flow Rules. |
| 66 | */ |
| 67 | public class OpenstackRoutingRulePopulator { |
| 68 | |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 69 | private final Logger log = LoggerFactory.getLogger(getClass()); |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 70 | |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 71 | private final ApplicationId appId; |
| 72 | private final FlowObjectiveService flowObjectiveService; |
sangho | 93447f1 | 2016-02-24 00:33:22 +0900 | [diff] [blame] | 73 | private final OpenstackInterfaceService openstackService; |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 74 | private final DeviceService deviceService; |
| 75 | private final DriverService driverService; |
Daniel Park | 81a61a1 | 2016-02-26 08:24:44 +0900 | [diff] [blame] | 76 | private final OpenstackRoutingConfig config; |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 77 | |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 78 | private static final String PORTNAME_PREFIX_TUNNEL = "vxlan"; |
| 79 | private static final String PORTNAME = "portName"; |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame^] | 80 | private static final String PORTNAME_PREFIX_VM = "tap"; |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 81 | |
| 82 | private static final String PORTNOTNULL = "Port can not be null"; |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame^] | 83 | private static final String DEVICENOTNULL = "Device can not be null"; |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 84 | private static final String TUNNEL_DESTINATION = "tunnelDst"; |
| 85 | private static final String DEVICE_ANNOTATION_CHANNELID = "channelId"; |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 86 | private static final int ROUTING_RULE_PRIORITY = 25000; |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame^] | 87 | private static final int FLOATING_RULE_PRIORITY = 42000; |
| 88 | private static final int PNAT_RULE_PRIORITY = 26000; |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 89 | private static final int PNAT_TIMEOUT = 120; |
| 90 | private static final MacAddress GATEWAYMAC = MacAddress.valueOf("1f:1f:1f:1f:1f:1f"); |
| 91 | |
| 92 | private InboundPacket inboundPacket; |
| 93 | private OpenstackPort openstackPort; |
| 94 | private int portNum; |
| 95 | private MacAddress externalInterface; |
| 96 | private MacAddress externalRouter; |
| 97 | private OpenstackRouter router; |
| 98 | private OpenstackRouterInterface routerInterface; |
| 99 | |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 100 | /** |
| 101 | * The constructor of openstackRoutingRulePopulator. |
| 102 | * |
| 103 | * @param appId Caller`s appId |
| 104 | * @param openstackService OpenstackNetworkingService |
| 105 | * @param flowObjectiveService FlowObjectiveService |
| 106 | * @param deviceService DeviceService |
| 107 | * @param driverService DriverService |
Daniel Park | 81a61a1 | 2016-02-26 08:24:44 +0900 | [diff] [blame] | 108 | * @param config OpenstackRoutingConfig |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 109 | */ |
sangho | 93447f1 | 2016-02-24 00:33:22 +0900 | [diff] [blame] | 110 | public OpenstackRoutingRulePopulator(ApplicationId appId, OpenstackInterfaceService openstackService, |
Daniel Park | 81a61a1 | 2016-02-26 08:24:44 +0900 | [diff] [blame] | 111 | FlowObjectiveService flowObjectiveService, DeviceService deviceService, |
| 112 | DriverService driverService, OpenstackRoutingConfig config) { |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 113 | this.appId = appId; |
| 114 | this.flowObjectiveService = flowObjectiveService; |
Daniel Park | 81a61a1 | 2016-02-26 08:24:44 +0900 | [diff] [blame] | 115 | this.openstackService = checkNotNull(openstackService); |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 116 | this.deviceService = deviceService; |
| 117 | this.driverService = driverService; |
Daniel Park | 81a61a1 | 2016-02-26 08:24:44 +0900 | [diff] [blame] | 118 | this.config = config; |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 119 | } |
| 120 | |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 121 | /** |
| 122 | * Populates flow rules for Pnat configurations. |
Daniel Park | 81a61a1 | 2016-02-26 08:24:44 +0900 | [diff] [blame] | 123 | * |
| 124 | * @param inboundPacket Packet-in event packet |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 125 | * @param openstackPort Target VM information |
| 126 | * @param portNum Pnat port number |
Hyunsun Moon | 0dba61f | 2016-03-03 14:05:21 -0800 | [diff] [blame] | 127 | * @param externalIp external ip address |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 128 | * @param externalInterfaceMacAddress Gateway external interface macaddress |
| 129 | * @param externalRouterMacAddress Outer(physical) router`s macaddress |
| 130 | */ |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 131 | public void populatePnatFlowRules(InboundPacket inboundPacket, OpenstackPort openstackPort, int portNum, |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 132 | Ip4Address externalIp, MacAddress externalInterfaceMacAddress, |
| 133 | MacAddress externalRouterMacAddress) { |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 134 | this.inboundPacket = inboundPacket; |
| 135 | this.openstackPort = openstackPort; |
| 136 | this.portNum = portNum; |
| 137 | this.externalInterface = externalInterfaceMacAddress; |
| 138 | this.externalRouter = externalRouterMacAddress; |
| 139 | |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame^] | 140 | long vni = getVni(openstackPort.networkId()); |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 141 | |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 142 | populatePnatIncomingFlowRules(vni, externalIp); |
| 143 | populatePnatOutgoingFlowRules(vni, externalIp); |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 144 | } |
| 145 | |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 146 | private void populatePnatOutgoingFlowRules(long vni, Ip4Address externalIp) { |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 147 | IPv4 iPacket = (IPv4) inboundPacket.parsed().getPayload(); |
| 148 | |
| 149 | TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder(); |
| 150 | sBuilder.matchEthType(Ethernet.TYPE_IPV4) |
| 151 | .matchIPProtocol(iPacket.getProtocol()) |
| 152 | .matchTunnelId(vni) |
| 153 | .matchIPSrc(IpPrefix.valueOf(iPacket.getSourceAddress(), 32)) |
| 154 | .matchIPDst(IpPrefix.valueOf(iPacket.getDestinationAddress(), 32)); |
| 155 | |
| 156 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 157 | tBuilder.setEthSrc(externalInterface) |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 158 | .setEthDst(externalRouter) |
| 159 | .setIpSrc(externalIp); |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 160 | |
| 161 | switch (iPacket.getProtocol()) { |
| 162 | case IPv4.PROTOCOL_TCP: |
| 163 | TCP tcpPacket = (TCP) iPacket.getPayload(); |
| 164 | sBuilder.matchTcpSrc(TpPort.tpPort(tcpPacket.getSourcePort())) |
| 165 | .matchTcpDst(TpPort.tpPort(tcpPacket.getDestinationPort())); |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 166 | tBuilder.setTcpSrc(TpPort.tpPort(portNum)); |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 167 | break; |
| 168 | case IPv4.PROTOCOL_UDP: |
| 169 | UDP udpPacket = (UDP) iPacket.getPayload(); |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 170 | sBuilder.matchUdpSrc(TpPort.tpPort(udpPacket.getSourcePort())) |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 171 | .matchUdpDst(TpPort.tpPort(udpPacket.getDestinationPort())); |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 172 | tBuilder.setUdpSrc(TpPort.tpPort(portNum)); |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 173 | break; |
| 174 | default: |
Daniel Park | 81a61a1 | 2016-02-26 08:24:44 +0900 | [diff] [blame] | 175 | log.debug("Unsupported IPv4 protocol {}"); |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 176 | break; |
| 177 | } |
| 178 | |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame^] | 179 | Port port = checkNotNull(getPortOfExternalInterface(), PORTNOTNULL); |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 180 | tBuilder.setOutput(port.number()); |
| 181 | |
| 182 | ForwardingObjective fo = DefaultForwardingObjective.builder() |
| 183 | .withSelector(sBuilder.build()) |
| 184 | .withTreatment(tBuilder.build()) |
| 185 | .withFlag(ForwardingObjective.Flag.VERSATILE) |
| 186 | .withPriority(PNAT_RULE_PRIORITY) |
| 187 | .makeTemporary(PNAT_TIMEOUT) |
| 188 | .fromApp(appId) |
| 189 | .add(); |
| 190 | |
| 191 | flowObjectiveService.forward(inboundPacket.receivedFrom().deviceId(), fo); |
| 192 | } |
| 193 | |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame^] | 194 | private Port getPortOfExternalInterface() { |
| 195 | return deviceService.getPorts(getGatewayNode().id()).stream() |
Daniel Park | 81a61a1 | 2016-02-26 08:24:44 +0900 | [diff] [blame] | 196 | .filter(p -> p.annotations().value(PORTNAME).equals(config.gatewayExternalInterfaceName())) |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 197 | .findAny().orElse(null); |
| 198 | } |
| 199 | |
| 200 | |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 201 | private void populatePnatIncomingFlowRules(long vni, Ip4Address externalIp) { |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 202 | IPv4 iPacket = (IPv4) inboundPacket.parsed().getPayload(); |
| 203 | DeviceId deviceId = inboundPacket.receivedFrom().deviceId(); |
| 204 | |
| 205 | TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder(); |
| 206 | sBuilder.matchEthType(Ethernet.TYPE_IPV4) |
| 207 | .matchIPProtocol(iPacket.getProtocol()) |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 208 | .matchIPDst(IpPrefix.valueOf(externalIp, 32)) |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 209 | .matchIPSrc(IpPrefix.valueOf(iPacket.getDestinationAddress(), 32)); |
| 210 | |
| 211 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 212 | tBuilder.setTunnelId(vni) |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame^] | 213 | .setEthDst(inboundPacket.parsed().getSourceMAC()) |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 214 | .setIpDst(IpAddress.valueOf(iPacket.getSourceAddress())); |
| 215 | |
| 216 | switch (iPacket.getProtocol()) { |
| 217 | case IPv4.PROTOCOL_TCP: |
| 218 | TCP tcpPacket = (TCP) iPacket.getPayload(); |
| 219 | sBuilder.matchTcpSrc(TpPort.tpPort(tcpPacket.getDestinationPort())) |
| 220 | .matchTcpDst(TpPort.tpPort(portNum)); |
| 221 | tBuilder.setTcpDst(TpPort.tpPort(tcpPacket.getSourcePort())); |
| 222 | break; |
| 223 | case IPv4.PROTOCOL_UDP: |
| 224 | UDP udpPacket = (UDP) iPacket.getPayload(); |
| 225 | sBuilder.matchUdpSrc(TpPort.tpPort(udpPacket.getDestinationPort())) |
| 226 | .matchUdpDst(TpPort.tpPort(portNum)); |
| 227 | tBuilder.setUdpDst(TpPort.tpPort(udpPacket.getSourcePort())); |
| 228 | break; |
| 229 | default: |
| 230 | break; |
| 231 | } |
| 232 | |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame^] | 233 | tBuilder.extension(buildNiciraExtenstion(deviceId, getHostIpfromOpenstackPort(openstackPort)), deviceId) |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 234 | .setOutput(getTunnelPort(deviceId)); |
| 235 | |
| 236 | ForwardingObjective fo = DefaultForwardingObjective.builder() |
| 237 | .withSelector(sBuilder.build()) |
| 238 | .withTreatment(tBuilder.build()) |
| 239 | .withFlag(ForwardingObjective.Flag.VERSATILE) |
| 240 | .withPriority(PNAT_RULE_PRIORITY) |
| 241 | .makeTemporary(PNAT_TIMEOUT) |
| 242 | .fromApp(appId) |
| 243 | .add(); |
| 244 | |
| 245 | flowObjectiveService.forward(inboundPacket.receivedFrom().deviceId(), fo); |
| 246 | } |
| 247 | |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame^] | 248 | private Ip4Address getHostIpfromOpenstackPort(OpenstackPort openstackPort) { |
| 249 | Device device = getDevicefromOpenstackPort(openstackPort); |
| 250 | return getIPAddressforDevice(device); |
| 251 | } |
Daniel Park | 81a61a1 | 2016-02-26 08:24:44 +0900 | [diff] [blame] | 252 | |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame^] | 253 | private Device getDevicefromOpenstackPort(OpenstackPort openstackPort) { |
| 254 | String openstackPortName = PORTNAME_PREFIX_VM + openstackPort.id().substring(0, 11); |
| 255 | Device device = StreamSupport.stream(deviceService.getDevices().spliterator(), false) |
| 256 | .filter(d -> findPortinDevice(d, openstackPortName)) |
| 257 | .findAny() |
| 258 | .orElse(null); |
| 259 | checkNotNull(device, DEVICENOTNULL); |
| 260 | return device; |
| 261 | } |
| 262 | |
| 263 | private boolean findPortinDevice(Device d, String openstackPortName) { |
| 264 | Port port = deviceService.getPorts(d.id()) |
| 265 | .stream() |
| 266 | .filter(p -> p.isEnabled() && p.annotations().value(PORTNAME).equals(openstackPortName)) |
| 267 | .findAny() |
| 268 | .orElse(null); |
| 269 | return port != null ? true : false; |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * Builds NiciraExtension for tagging remoteIp of vxlan. |
| 274 | * |
| 275 | * @param id Device Id of vxlan source device |
| 276 | * @param hostIp Remote Ip of vxlan destination device |
| 277 | * @return NiciraExtension Treatment |
| 278 | */ |
Daniel Park | 81a61a1 | 2016-02-26 08:24:44 +0900 | [diff] [blame] | 279 | public ExtensionTreatment buildNiciraExtenstion(DeviceId id, Ip4Address hostIp) { |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 280 | Driver driver = driverService.getDriver(id); |
| 281 | DriverHandler driverHandler = new DefaultDriverHandler(new DefaultDriverData(driver, id)); |
| 282 | ExtensionTreatmentResolver resolver = driverHandler.behaviour(ExtensionTreatmentResolver.class); |
| 283 | |
| 284 | ExtensionTreatment extensionInstruction = |
| 285 | resolver.getExtensionInstruction( |
| 286 | ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_TUNNEL_DST.type()); |
| 287 | |
| 288 | try { |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 289 | extensionInstruction.setPropertyValue(TUNNEL_DESTINATION, hostIp); |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 290 | } catch (ExtensionPropertyException e) { |
| 291 | log.error("Error setting Nicira extension setting {}", e); |
| 292 | } |
| 293 | |
| 294 | return extensionInstruction; |
| 295 | } |
| 296 | |
Daniel Park | 81a61a1 | 2016-02-26 08:24:44 +0900 | [diff] [blame] | 297 | /** |
| 298 | * Returns port number of vxlan tunnel. |
| 299 | * |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame^] | 300 | * @param deviceId Target Device Id |
| 301 | * @return PortNumber |
Daniel Park | 81a61a1 | 2016-02-26 08:24:44 +0900 | [diff] [blame] | 302 | */ |
| 303 | public PortNumber getTunnelPort(DeviceId deviceId) { |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 304 | Port port = deviceService.getPorts(deviceId).stream() |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 305 | .filter(p -> p.annotations().value(PORTNAME).equals(PORTNAME_PREFIX_TUNNEL)) |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 306 | .findAny().orElse(null); |
| 307 | |
| 308 | if (port == null) { |
| 309 | log.error("No TunnelPort was created."); |
| 310 | return null; |
| 311 | } |
| 312 | return port.number(); |
| 313 | |
| 314 | } |
| 315 | |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 316 | /** |
| 317 | * Populates flow rules from openstackComputeNode to GatewayNode. |
| 318 | * |
| 319 | * @param vni Target network |
| 320 | * @param router corresponding router |
| 321 | * @param routerInterface corresponding routerInterface |
| 322 | */ |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 323 | public void populateExternalRules(long vni, OpenstackRouter router, |
| 324 | OpenstackRouterInterface routerInterface) { |
| 325 | this.router = router; |
| 326 | this.routerInterface = routerInterface; |
| 327 | |
| 328 | // 1. computeNode to gateway |
| 329 | populateComputeNodeRules(vni); |
| 330 | // 2. gatewayNode to controller |
| 331 | populateRuleGatewaytoController(vni); |
| 332 | } |
| 333 | |
| 334 | private void populateRuleGatewaytoController(long vni) { |
| 335 | Device gatewayDevice = getGatewayNode(); |
| 336 | TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder(); |
| 337 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 338 | |
| 339 | sBuilder.matchEthType(Ethernet.TYPE_IPV4) |
| 340 | .matchTunnelId(vni) |
| 341 | .matchEthDst(GATEWAYMAC); |
| 342 | tBuilder.setOutput(PortNumber.CONTROLLER); |
| 343 | |
| 344 | ForwardingObjective fo = DefaultForwardingObjective.builder() |
| 345 | .withSelector(sBuilder.build()) |
| 346 | .withTreatment(tBuilder.build()) |
| 347 | .withFlag(ForwardingObjective.Flag.VERSATILE) |
| 348 | .withPriority(ROUTING_RULE_PRIORITY) |
| 349 | .fromApp(appId) |
| 350 | .add(); |
| 351 | |
| 352 | flowObjectiveService.forward(gatewayDevice.id(), fo); |
| 353 | } |
| 354 | |
| 355 | private void populateComputeNodeRules(long vni) { |
| 356 | Device gatewayDevice = getGatewayNode(); |
| 357 | |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame^] | 358 | StreamSupport.stream(deviceService.getDevices().spliterator(), false) |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 359 | .filter(d -> !checkGatewayNode(d.id())) |
| 360 | .forEach(d -> populateRuleToGateway(d, gatewayDevice, vni)); |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | private void populateRuleToGateway(Device d, Device gatewayDevice, long vni) { |
| 364 | TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder(); |
| 365 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 366 | |
| 367 | sBuilder.matchEthType(Ethernet.TYPE_IPV4) |
| 368 | .matchTunnelId(vni) |
| 369 | .matchEthDst(GATEWAYMAC); |
| 370 | tBuilder.extension(buildNiciraExtenstion(d.id(), getIPAddressforDevice(gatewayDevice)), d.id()) |
| 371 | .setOutput(getTunnelPort(d.id())); |
| 372 | |
| 373 | ForwardingObjective fo = DefaultForwardingObjective.builder() |
| 374 | .withSelector(sBuilder.build()) |
| 375 | .withTreatment(tBuilder.build()) |
| 376 | .withFlag(ForwardingObjective.Flag.SPECIFIC) |
| 377 | .withPriority(ROUTING_RULE_PRIORITY) |
| 378 | .fromApp(appId) |
| 379 | .add(); |
| 380 | |
| 381 | flowObjectiveService.forward(d.id(), fo); |
| 382 | } |
| 383 | |
| 384 | private Ip4Address getIPAddressforDevice(Device device) { |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 385 | return Ip4Address.valueOf(device.annotations().value(DEVICE_ANNOTATION_CHANNELID).split(":")[0]); |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | private Device getGatewayNode() { |
Daniel Park | 81a61a1 | 2016-02-26 08:24:44 +0900 | [diff] [blame] | 389 | return checkNotNull(deviceService.getDevice(DeviceId.deviceId(config.gatewayBridgeId()))); |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | private boolean checkGatewayNode(DeviceId deviceId) { |
Daniel Park | 81a61a1 | 2016-02-26 08:24:44 +0900 | [diff] [blame] | 393 | return deviceId.toString().equals(config.gatewayBridgeId()); |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 394 | } |
| 395 | |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame^] | 396 | private long getVni(String netId) { |
| 397 | return Long.parseLong(openstackService.network(netId).segmentId()); |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | /** |
| 401 | * Remove flow rules for external connection. |
| 402 | * |
| 403 | * @param routerInterface Corresponding routerInterface |
| 404 | */ |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 405 | public void removeExternalRules(OpenstackRouterInterface routerInterface) { |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 406 | OpenstackSubnet openstackSubnet = openstackService.subnet(routerInterface.subnetId()); |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 407 | TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder(); |
| 408 | sBuilder.matchEthType(Ethernet.TYPE_IPV4) |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame^] | 409 | .matchTunnelId(getVni(openstackSubnet.networkId())) |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 410 | .matchEthDst(GATEWAYMAC); |
| 411 | |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame^] | 412 | StreamSupport.stream(deviceService.getDevices().spliterator(), false) |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 413 | .forEach(d -> { |
| 414 | if (checkGatewayNode(d.id())) { |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame^] | 415 | removeRule(d.id(), sBuilder, ForwardingObjective.Flag.VERSATILE, ROUTING_RULE_PRIORITY); |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 416 | } else { |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame^] | 417 | removeRule(d.id(), sBuilder, ForwardingObjective.Flag.SPECIFIC, ROUTING_RULE_PRIORITY); |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 418 | } |
| 419 | }); |
| 420 | |
| 421 | } |
| 422 | |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame^] | 423 | private void removeRule(DeviceId id, TrafficSelector.Builder sBuilder, |
| 424 | ForwardingObjective.Flag flag, int priority) { |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 425 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 426 | |
| 427 | ForwardingObjective fo = DefaultForwardingObjective.builder() |
| 428 | .withSelector(sBuilder.build()) |
| 429 | .withTreatment(tBuilder.build()) |
| 430 | .withFlag(flag) |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame^] | 431 | .withPriority(priority) |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 432 | .fromApp(appId) |
| 433 | .remove(); |
| 434 | |
| 435 | flowObjectiveService.forward(id, fo); |
| 436 | } |
| 437 | |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame^] | 438 | /** |
| 439 | * Populates flow rules for floatingIp configuration. |
| 440 | * |
| 441 | * @param floatingIP Corresponding floating ip information |
| 442 | */ |
| 443 | public void populateFloatingIpRules(OpenstackFloatingIP floatingIP) { |
| 444 | OpenstackPort port = openstackService.port(floatingIP.portId()); |
| 445 | //1. incoming rules |
| 446 | populateFloatingIpIncomingRules(floatingIP, port); |
| 447 | //2. outgoing rules |
| 448 | populateFloatingIpOutgoingRules(floatingIP, port); |
| 449 | } |
| 450 | |
| 451 | private void populateFloatingIpIncomingRules(OpenstackFloatingIP floatingIP, OpenstackPort port) { |
| 452 | DeviceId portDeviceId = getDevicefromOpenstackPort(port).id(); |
| 453 | Device gatewayNode = getGatewayNode(); |
| 454 | Device portNode = deviceService.getDevice(portDeviceId); |
| 455 | |
| 456 | TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder(); |
| 457 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 458 | |
| 459 | sBuilder.matchEthType(Ethernet.TYPE_IPV4) |
| 460 | .matchIPDst(IpPrefix.valueOf(floatingIP.floatingIpAddress(), 32)); |
| 461 | |
| 462 | tBuilder.setEthSrc(MacAddress.valueOf(config.gatewayExternalInterfaceMac())) |
| 463 | .setEthDst(port.macAddress()) |
| 464 | .setIpDst(floatingIP.fixedIpAddress()) |
| 465 | .setTunnelId(getVni(port.networkId())) |
| 466 | .extension(buildNiciraExtenstion(gatewayNode.id(), getIPAddressforDevice(portNode)), gatewayNode.id()) |
| 467 | .setOutput(getTunnelPort(gatewayNode.id())); |
| 468 | |
| 469 | ForwardingObjective fo = DefaultForwardingObjective.builder() |
| 470 | .withSelector(sBuilder.build()) |
| 471 | .withTreatment(tBuilder.build()) |
| 472 | .withFlag(ForwardingObjective.Flag.VERSATILE) |
| 473 | .withPriority(FLOATING_RULE_PRIORITY) |
| 474 | .fromApp(appId) |
| 475 | .add(); |
| 476 | |
| 477 | flowObjectiveService.forward(getGatewayNode().id(), fo); |
| 478 | |
| 479 | } |
| 480 | |
| 481 | private void populateFloatingIpOutgoingRules(OpenstackFloatingIP floatingIP, OpenstackPort port) { |
| 482 | Port outputPort = checkNotNull(getPortOfExternalInterface(), PORTNOTNULL); |
| 483 | |
| 484 | TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder(); |
| 485 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 486 | |
| 487 | sBuilder.matchEthType(Ethernet.TYPE_IPV4) |
| 488 | .matchTunnelId(getVni(port.networkId())) |
| 489 | .matchIPSrc(IpPrefix.valueOf(floatingIP.fixedIpAddress(), 32)); |
| 490 | |
| 491 | tBuilder.setIpSrc(floatingIP.floatingIpAddress()) |
| 492 | .setEthSrc(MacAddress.valueOf(config.gatewayExternalInterfaceMac())) |
| 493 | .setEthDst(MacAddress.valueOf(config.physicalRouterMac())) |
| 494 | .setOutput(outputPort.number()); |
| 495 | |
| 496 | ForwardingObjective fo = DefaultForwardingObjective.builder() |
| 497 | .withSelector(sBuilder.build()) |
| 498 | .withTreatment(tBuilder.build()) |
| 499 | .withFlag(ForwardingObjective.Flag.VERSATILE) |
| 500 | .withPriority(FLOATING_RULE_PRIORITY) |
| 501 | .fromApp(appId) |
| 502 | .add(); |
| 503 | |
| 504 | flowObjectiveService.forward(getGatewayNode().id(), fo); |
| 505 | } |
| 506 | |
| 507 | /** |
| 508 | * Removes flow rules for floating ip configuration. |
| 509 | * |
| 510 | * @param floatingIP Corresponding floating ip information |
| 511 | */ |
| 512 | public void removeFloatingIpRules(OpenstackFloatingIP floatingIP, OpenstackPortInfo portInfo) { |
| 513 | TrafficSelector.Builder sOutgoingBuilder = DefaultTrafficSelector.builder(); |
| 514 | TrafficSelector.Builder sIncomingBuilder = DefaultTrafficSelector.builder(); |
| 515 | |
| 516 | sOutgoingBuilder.matchEthType(Ethernet.TYPE_IPV4) |
| 517 | .matchTunnelId(portInfo.vni()) |
| 518 | .matchIPSrc(IpPrefix.valueOf(portInfo.ip(), 32)); |
| 519 | |
| 520 | sIncomingBuilder.matchEthType(Ethernet.TYPE_IPV4) |
| 521 | .matchIPDst(IpPrefix.valueOf(floatingIP.floatingIpAddress(), 32)); |
| 522 | |
| 523 | removeRule(getGatewayNode().id(), sOutgoingBuilder, ForwardingObjective.Flag.VERSATILE, FLOATING_RULE_PRIORITY); |
| 524 | removeRule(getGatewayNode().id(), sIncomingBuilder, ForwardingObjective.Flag.VERSATILE, FLOATING_RULE_PRIORITY); |
| 525 | } |
| 526 | |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 527 | } |