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 | |
Kyuhwi Choi | 92d9ea4 | 2016-06-13 17:28:00 +0900 | [diff] [blame] | 18 | import com.google.common.collect.Lists; |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 19 | import org.onlab.packet.Ethernet; |
| 20 | import org.onlab.packet.IPv4; |
| 21 | import org.onlab.packet.Ip4Address; |
| 22 | import org.onlab.packet.IpAddress; |
| 23 | import org.onlab.packet.IpPrefix; |
| 24 | import org.onlab.packet.MacAddress; |
| 25 | import org.onlab.packet.TCP; |
| 26 | import org.onlab.packet.TpPort; |
| 27 | import org.onlab.packet.UDP; |
| 28 | import org.onosproject.core.ApplicationId; |
Kyuhwi Choi | 92d9ea4 | 2016-06-13 17:28:00 +0900 | [diff] [blame] | 29 | import org.onosproject.core.GroupId; |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 30 | import org.onosproject.net.Device; |
| 31 | import org.onosproject.net.DeviceId; |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame^] | 32 | import org.onosproject.net.Host; |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 33 | import org.onosproject.net.Port; |
| 34 | import org.onosproject.net.PortNumber; |
| 35 | import org.onosproject.net.behaviour.ExtensionTreatmentResolver; |
| 36 | import org.onosproject.net.device.DeviceService; |
| 37 | import org.onosproject.net.driver.DefaultDriverData; |
| 38 | import org.onosproject.net.driver.DefaultDriverHandler; |
| 39 | import org.onosproject.net.driver.Driver; |
| 40 | import org.onosproject.net.driver.DriverHandler; |
| 41 | import org.onosproject.net.driver.DriverService; |
| 42 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 43 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 44 | import org.onosproject.net.flow.TrafficSelector; |
| 45 | import org.onosproject.net.flow.TrafficTreatment; |
| 46 | import org.onosproject.net.flow.instructions.ExtensionPropertyException; |
| 47 | import org.onosproject.net.flow.instructions.ExtensionTreatment; |
| 48 | import org.onosproject.net.flow.instructions.ExtensionTreatmentType; |
| 49 | import org.onosproject.net.flowobjective.DefaultForwardingObjective; |
| 50 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
| 51 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 52 | import org.onosproject.net.packet.InboundPacket; |
sangho | 93447f1 | 2016-02-24 00:33:22 +0900 | [diff] [blame] | 53 | import org.onosproject.openstackinterface.OpenstackInterfaceService; |
| 54 | import org.onosproject.openstackinterface.OpenstackPort; |
sangho | 93447f1 | 2016-02-24 00:33:22 +0900 | [diff] [blame] | 55 | import org.onosproject.openstackinterface.OpenstackRouterInterface; |
| 56 | import org.onosproject.openstackinterface.OpenstackSubnet; |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 57 | import org.onosproject.openstackinterface.OpenstackFloatingIP; |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame^] | 58 | import org.onosproject.openstacknetworking.Constants; |
Kyuhwi Choi | 5e65e2c | 2016-04-01 11:48:15 +0900 | [diff] [blame] | 59 | import org.onosproject.openstacknetworking.OpenstackRoutingService; |
Kyuhwi Choi | 92d9ea4 | 2016-06-13 17:28:00 +0900 | [diff] [blame] | 60 | import org.onosproject.scalablegateway.api.ScalableGatewayService; |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame^] | 61 | import org.onosproject.openstacknode.OpenstackNode; |
| 62 | import org.onosproject.openstacknode.OpenstackNodeService; |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 63 | import org.slf4j.Logger; |
| 64 | import org.slf4j.LoggerFactory; |
| 65 | |
Kyuhwi Choi | 5e65e2c | 2016-04-01 11:48:15 +0900 | [diff] [blame] | 66 | import java.util.List; |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame^] | 67 | import java.util.Optional; |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 68 | import java.util.stream.StreamSupport; |
| 69 | |
| 70 | import static com.google.common.base.Preconditions.checkNotNull; |
Kyuhwi Choi | 5e65e2c | 2016-04-01 11:48:15 +0900 | [diff] [blame] | 71 | import static org.onlab.osgi.DefaultServiceDirectory.getService; |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 72 | |
| 73 | /** |
| 74 | * Populates Routing Flow Rules. |
| 75 | */ |
| 76 | public class OpenstackRoutingRulePopulator { |
| 77 | |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 78 | private final Logger log = LoggerFactory.getLogger(getClass()); |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 79 | |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 80 | private final ApplicationId appId; |
| 81 | private final FlowObjectiveService flowObjectiveService; |
sangho | 93447f1 | 2016-02-24 00:33:22 +0900 | [diff] [blame] | 82 | private final OpenstackInterfaceService openstackService; |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 83 | private final DeviceService deviceService; |
| 84 | private final DriverService driverService; |
Kyuhwi Choi | 92d9ea4 | 2016-06-13 17:28:00 +0900 | [diff] [blame] | 85 | private final ScalableGatewayService gatewayService; |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame^] | 86 | private final OpenstackNodeService nodeService; |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 87 | |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 88 | private static final String PORTNAME_PREFIX_TUNNEL = "vxlan"; |
| 89 | private static final String PORTNAME = "portName"; |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 90 | private static final String PORTNAME_PREFIX_VM = "tap"; |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 91 | |
| 92 | private static final String PORTNOTNULL = "Port can not be null"; |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 93 | private static final String DEVICENOTNULL = "Device can not be null"; |
Kyuhwi Choi | 92d9ea4 | 2016-06-13 17:28:00 +0900 | [diff] [blame] | 94 | private static final String EXTPORTNOTNULL = "External port can not be null"; |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 95 | private static final String TUNNEL_DESTINATION = "tunnelDst"; |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 96 | private static final int ROUTING_RULE_PRIORITY = 25000; |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 97 | private static final int FLOATING_RULE_PRIORITY = 42000; |
| 98 | private static final int PNAT_RULE_PRIORITY = 26000; |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 99 | private static final int PNAT_TIMEOUT = 120; |
Daniel Park | 2319390 | 2016-03-24 18:17:19 +0900 | [diff] [blame] | 100 | private static final int PREFIX_LENGTH = 32; |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 101 | |
| 102 | private InboundPacket inboundPacket; |
| 103 | private OpenstackPort openstackPort; |
| 104 | private int portNum; |
| 105 | private MacAddress externalInterface; |
| 106 | private MacAddress externalRouter; |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 107 | |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 108 | /** |
| 109 | * The constructor of openstackRoutingRulePopulator. |
| 110 | * |
| 111 | * @param appId Caller`s appId |
Kyuhwi Choi | 5e65e2c | 2016-04-01 11:48:15 +0900 | [diff] [blame] | 112 | * @param openstackService Opestack REST request handler |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 113 | * @param flowObjectiveService FlowObjectiveService |
| 114 | * @param deviceService DeviceService |
| 115 | * @param driverService DriverService |
Kyuhwi Choi | 92d9ea4 | 2016-06-13 17:28:00 +0900 | [diff] [blame] | 116 | * @param gatewayService scalable gateway service |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 117 | */ |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame^] | 118 | public OpenstackRoutingRulePopulator(ApplicationId appId, |
| 119 | OpenstackInterfaceService openstackService, |
| 120 | FlowObjectiveService flowObjectiveService, |
| 121 | DeviceService deviceService, |
| 122 | DriverService driverService, |
| 123 | OpenstackNodeService nodeService, |
Kyuhwi Choi | 92d9ea4 | 2016-06-13 17:28:00 +0900 | [diff] [blame] | 124 | ScalableGatewayService gatewayService) { |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 125 | this.appId = appId; |
| 126 | this.flowObjectiveService = flowObjectiveService; |
Daniel Park | 81a61a1 | 2016-02-26 08:24:44 +0900 | [diff] [blame] | 127 | this.openstackService = checkNotNull(openstackService); |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 128 | this.deviceService = deviceService; |
| 129 | this.driverService = driverService; |
Kyuhwi Choi | 92d9ea4 | 2016-06-13 17:28:00 +0900 | [diff] [blame] | 130 | this.gatewayService = gatewayService; |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame^] | 131 | this.nodeService = nodeService; |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 132 | } |
| 133 | |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 134 | /** |
| 135 | * Populates flow rules for Pnat configurations. |
Daniel Park | 81a61a1 | 2016-02-26 08:24:44 +0900 | [diff] [blame] | 136 | * |
| 137 | * @param inboundPacket Packet-in event packet |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 138 | * @param openstackPort Target VM information |
| 139 | * @param portNum Pnat port number |
Hyunsun Moon | 0dba61f | 2016-03-03 14:05:21 -0800 | [diff] [blame] | 140 | * @param externalIp external ip address |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 141 | * @param externalInterfaceMacAddress Gateway external interface macaddress |
| 142 | * @param externalRouterMacAddress Outer(physical) router`s macaddress |
| 143 | */ |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 144 | public void populatePnatFlowRules(InboundPacket inboundPacket, OpenstackPort openstackPort, int portNum, |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 145 | Ip4Address externalIp, MacAddress externalInterfaceMacAddress, |
| 146 | MacAddress externalRouterMacAddress) { |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 147 | this.inboundPacket = inboundPacket; |
| 148 | this.openstackPort = openstackPort; |
| 149 | this.portNum = portNum; |
| 150 | this.externalInterface = externalInterfaceMacAddress; |
| 151 | this.externalRouter = externalRouterMacAddress; |
| 152 | |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 153 | long vni = getVni(openstackPort.networkId()); |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 154 | |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 155 | populatePnatIncomingFlowRules(vni, externalIp); |
| 156 | populatePnatOutgoingFlowRules(vni, externalIp); |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 157 | } |
| 158 | |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 159 | private void populatePnatOutgoingFlowRules(long vni, Ip4Address externalIp) { |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 160 | IPv4 iPacket = (IPv4) inboundPacket.parsed().getPayload(); |
| 161 | |
| 162 | TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder(); |
| 163 | sBuilder.matchEthType(Ethernet.TYPE_IPV4) |
| 164 | .matchIPProtocol(iPacket.getProtocol()) |
| 165 | .matchTunnelId(vni) |
| 166 | .matchIPSrc(IpPrefix.valueOf(iPacket.getSourceAddress(), 32)) |
| 167 | .matchIPDst(IpPrefix.valueOf(iPacket.getDestinationAddress(), 32)); |
| 168 | |
| 169 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 170 | |
| 171 | switch (iPacket.getProtocol()) { |
| 172 | case IPv4.PROTOCOL_TCP: |
| 173 | TCP tcpPacket = (TCP) iPacket.getPayload(); |
| 174 | sBuilder.matchTcpSrc(TpPort.tpPort(tcpPacket.getSourcePort())) |
| 175 | .matchTcpDst(TpPort.tpPort(tcpPacket.getDestinationPort())); |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 176 | tBuilder.setTcpSrc(TpPort.tpPort(portNum)); |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 177 | break; |
| 178 | case IPv4.PROTOCOL_UDP: |
| 179 | UDP udpPacket = (UDP) iPacket.getPayload(); |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 180 | sBuilder.matchUdpSrc(TpPort.tpPort(udpPacket.getSourcePort())) |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 181 | .matchUdpDst(TpPort.tpPort(udpPacket.getDestinationPort())); |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 182 | tBuilder.setUdpSrc(TpPort.tpPort(portNum)); |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 183 | break; |
| 184 | default: |
Daniel Park | 81a61a1 | 2016-02-26 08:24:44 +0900 | [diff] [blame] | 185 | log.debug("Unsupported IPv4 protocol {}"); |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 186 | break; |
| 187 | } |
| 188 | |
Kyuhwi Choi | 92d9ea4 | 2016-06-13 17:28:00 +0900 | [diff] [blame] | 189 | tBuilder.setIpSrc(externalIp); |
| 190 | gatewayService.getGatewayNodes().forEach(node -> { |
| 191 | tBuilder.setOutput(gatewayService.getGatewayExternalPorts(node.getGatewayDeviceId()).get(0)); |
| 192 | ForwardingObjective fo = DefaultForwardingObjective.builder() |
| 193 | .withSelector(sBuilder.build()) |
| 194 | .withTreatment(tBuilder.build()) |
| 195 | .withFlag(ForwardingObjective.Flag.VERSATILE) |
| 196 | .withPriority(PNAT_RULE_PRIORITY) |
| 197 | .makeTemporary(PNAT_TIMEOUT) |
| 198 | .fromApp(appId) |
| 199 | .add(); |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 200 | |
Kyuhwi Choi | 92d9ea4 | 2016-06-13 17:28:00 +0900 | [diff] [blame] | 201 | flowObjectiveService.forward(node.getGatewayDeviceId(), fo); |
| 202 | }); |
Kyuhwi Choi | 5e65e2c | 2016-04-01 11:48:15 +0900 | [diff] [blame] | 203 | |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 204 | } |
| 205 | |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 206 | private Port getPortOfExternalInterface() { |
| 207 | return deviceService.getPorts(getGatewayNode().id()).stream() |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame^] | 208 | .filter(p -> p.annotations().value(PORTNAME) |
| 209 | .equals(org.onosproject.openstacknode.Constants.PATCH_INTG_BRIDGE)) |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 210 | .findAny().orElse(null); |
| 211 | } |
| 212 | |
| 213 | |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 214 | private void populatePnatIncomingFlowRules(long vni, Ip4Address externalIp) { |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 215 | IPv4 iPacket = (IPv4) inboundPacket.parsed().getPayload(); |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 216 | |
| 217 | TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder(); |
| 218 | sBuilder.matchEthType(Ethernet.TYPE_IPV4) |
| 219 | .matchIPProtocol(iPacket.getProtocol()) |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 220 | .matchIPDst(IpPrefix.valueOf(externalIp, 32)) |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 221 | .matchIPSrc(IpPrefix.valueOf(iPacket.getDestinationAddress(), 32)); |
| 222 | |
| 223 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 224 | tBuilder.setTunnelId(vni) |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 225 | .setEthDst(inboundPacket.parsed().getSourceMAC()) |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 226 | .setIpDst(IpAddress.valueOf(iPacket.getSourceAddress())); |
| 227 | |
| 228 | switch (iPacket.getProtocol()) { |
| 229 | case IPv4.PROTOCOL_TCP: |
| 230 | TCP tcpPacket = (TCP) iPacket.getPayload(); |
| 231 | sBuilder.matchTcpSrc(TpPort.tpPort(tcpPacket.getDestinationPort())) |
| 232 | .matchTcpDst(TpPort.tpPort(portNum)); |
| 233 | tBuilder.setTcpDst(TpPort.tpPort(tcpPacket.getSourcePort())); |
| 234 | break; |
| 235 | case IPv4.PROTOCOL_UDP: |
| 236 | UDP udpPacket = (UDP) iPacket.getPayload(); |
| 237 | sBuilder.matchUdpSrc(TpPort.tpPort(udpPacket.getDestinationPort())) |
| 238 | .matchUdpDst(TpPort.tpPort(portNum)); |
| 239 | tBuilder.setUdpDst(TpPort.tpPort(udpPacket.getSourcePort())); |
| 240 | break; |
| 241 | default: |
| 242 | break; |
| 243 | } |
| 244 | |
Kyuhwi Choi | 92d9ea4 | 2016-06-13 17:28:00 +0900 | [diff] [blame] | 245 | getGatewayNodeList().forEach(node -> { |
| 246 | DeviceId deviceId = node.id(); |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame^] | 247 | tBuilder.extension(buildNiciraExtenstion(deviceId, |
| 248 | getHostIpfromOpenstackPort(openstackPort).getIp4Address()), deviceId) |
Kyuhwi Choi | 92d9ea4 | 2016-06-13 17:28:00 +0900 | [diff] [blame] | 249 | .setOutput(getTunnelPort(deviceId)); |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 250 | |
Kyuhwi Choi | 92d9ea4 | 2016-06-13 17:28:00 +0900 | [diff] [blame] | 251 | ForwardingObjective fo = DefaultForwardingObjective.builder() |
| 252 | .withSelector(sBuilder.build()) |
| 253 | .withTreatment(tBuilder.build()) |
| 254 | .withFlag(ForwardingObjective.Flag.VERSATILE) |
| 255 | .withPriority(PNAT_RULE_PRIORITY) |
| 256 | .makeTemporary(PNAT_TIMEOUT) |
| 257 | .fromApp(appId) |
| 258 | .add(); |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 259 | |
Kyuhwi Choi | 92d9ea4 | 2016-06-13 17:28:00 +0900 | [diff] [blame] | 260 | flowObjectiveService.forward(deviceId, fo); |
| 261 | }); |
| 262 | } |
| 263 | |
| 264 | private List<Device> getGatewayNodeList() { |
| 265 | List<Device> devices = Lists.newArrayList(); |
| 266 | gatewayService.getGatewayDeviceIds().forEach(deviceId -> |
| 267 | devices.add(checkNotNull(deviceService.getDevice(deviceId)))); |
| 268 | return devices; |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 269 | } |
| 270 | |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame^] | 271 | private IpAddress getHostIpfromOpenstackPort(OpenstackPort openstackPort) { |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 272 | Device device = getDevicefromOpenstackPort(openstackPort); |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame^] | 273 | |
| 274 | Optional<IpAddress> ipAddress = nodeService.dataIp(device.id()); |
| 275 | if (!ipAddress.isPresent()) { |
| 276 | log.warn("No IP address found for device {}", device.id()); |
| 277 | return null; |
| 278 | } |
| 279 | |
| 280 | return ipAddress.get(); |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 281 | } |
Daniel Park | 81a61a1 | 2016-02-26 08:24:44 +0900 | [diff] [blame] | 282 | |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 283 | private Device getDevicefromOpenstackPort(OpenstackPort openstackPort) { |
| 284 | String openstackPortName = PORTNAME_PREFIX_VM + openstackPort.id().substring(0, 11); |
| 285 | Device device = StreamSupport.stream(deviceService.getDevices().spliterator(), false) |
Kyuhwi Choi | 92d9ea4 | 2016-06-13 17:28:00 +0900 | [diff] [blame] | 286 | .filter(d -> findPortinDevice(d.id(), openstackPortName)) |
Kyuhwi Choi | 5e65e2c | 2016-04-01 11:48:15 +0900 | [diff] [blame] | 287 | .iterator() |
| 288 | .next(); |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 289 | checkNotNull(device, DEVICENOTNULL); |
| 290 | return device; |
| 291 | } |
| 292 | |
Kyuhwi Choi | 92d9ea4 | 2016-06-13 17:28:00 +0900 | [diff] [blame] | 293 | private boolean findPortinDevice(DeviceId deviceId, String openstackPortName) { |
| 294 | Port port = deviceService.getPorts(deviceId) |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 295 | .stream() |
| 296 | .filter(p -> p.isEnabled() && p.annotations().value(PORTNAME).equals(openstackPortName)) |
| 297 | .findAny() |
| 298 | .orElse(null); |
Daniel Park | 2319390 | 2016-03-24 18:17:19 +0900 | [diff] [blame] | 299 | return port != null; |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | /** |
Kyuhwi Choi | 5e65e2c | 2016-04-01 11:48:15 +0900 | [diff] [blame] | 303 | * Builds Nicira extension for tagging remoteIp of vxlan. |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 304 | * |
Kyuhwi Choi | 92d9ea4 | 2016-06-13 17:28:00 +0900 | [diff] [blame] | 305 | * @param deviceId Device Id of vxlan source device |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 306 | * @param hostIp Remote Ip of vxlan destination device |
| 307 | * @return NiciraExtension Treatment |
| 308 | */ |
Kyuhwi Choi | 92d9ea4 | 2016-06-13 17:28:00 +0900 | [diff] [blame] | 309 | public ExtensionTreatment buildNiciraExtenstion(DeviceId deviceId, Ip4Address hostIp) { |
| 310 | Driver driver = driverService.getDriver(deviceId); |
| 311 | DriverHandler driverHandler = new DefaultDriverHandler(new DefaultDriverData(driver, deviceId)); |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 312 | ExtensionTreatmentResolver resolver = driverHandler.behaviour(ExtensionTreatmentResolver.class); |
| 313 | |
| 314 | ExtensionTreatment extensionInstruction = |
| 315 | resolver.getExtensionInstruction( |
| 316 | ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_TUNNEL_DST.type()); |
| 317 | |
| 318 | try { |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 319 | extensionInstruction.setPropertyValue(TUNNEL_DESTINATION, hostIp); |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 320 | } catch (ExtensionPropertyException e) { |
| 321 | log.error("Error setting Nicira extension setting {}", e); |
| 322 | } |
| 323 | |
| 324 | return extensionInstruction; |
| 325 | } |
| 326 | |
Daniel Park | 81a61a1 | 2016-02-26 08:24:44 +0900 | [diff] [blame] | 327 | /** |
| 328 | * Returns port number of vxlan tunnel. |
| 329 | * |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 330 | * @param deviceId Target Device Id |
| 331 | * @return PortNumber |
Daniel Park | 81a61a1 | 2016-02-26 08:24:44 +0900 | [diff] [blame] | 332 | */ |
| 333 | public PortNumber getTunnelPort(DeviceId deviceId) { |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 334 | Port port = deviceService.getPorts(deviceId).stream() |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 335 | .filter(p -> p.annotations().value(PORTNAME).equals(PORTNAME_PREFIX_TUNNEL)) |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 336 | .findAny().orElse(null); |
| 337 | |
| 338 | if (port == null) { |
| 339 | log.error("No TunnelPort was created."); |
| 340 | return null; |
| 341 | } |
| 342 | return port.number(); |
| 343 | |
| 344 | } |
| 345 | |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 346 | /** |
| 347 | * Populates flow rules from openstackComputeNode to GatewayNode. |
| 348 | * |
| 349 | * @param vni Target network |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 350 | */ |
Daniel Park | 2319390 | 2016-03-24 18:17:19 +0900 | [diff] [blame] | 351 | public void populateExternalRules(long vni) { |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 352 | |
| 353 | // 1. computeNode to gateway |
| 354 | populateComputeNodeRules(vni); |
| 355 | // 2. gatewayNode to controller |
| 356 | populateRuleGatewaytoController(vni); |
| 357 | } |
| 358 | |
| 359 | private void populateRuleGatewaytoController(long vni) { |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 360 | TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder(); |
| 361 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 362 | |
| 363 | sBuilder.matchEthType(Ethernet.TYPE_IPV4) |
| 364 | .matchTunnelId(vni) |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame^] | 365 | .matchEthDst(Constants.GATEWAY_MAC); |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 366 | tBuilder.setOutput(PortNumber.CONTROLLER); |
| 367 | |
| 368 | ForwardingObjective fo = DefaultForwardingObjective.builder() |
| 369 | .withSelector(sBuilder.build()) |
| 370 | .withTreatment(tBuilder.build()) |
| 371 | .withFlag(ForwardingObjective.Flag.VERSATILE) |
| 372 | .withPriority(ROUTING_RULE_PRIORITY) |
| 373 | .fromApp(appId) |
| 374 | .add(); |
| 375 | |
Kyuhwi Choi | 92d9ea4 | 2016-06-13 17:28:00 +0900 | [diff] [blame] | 376 | getGatewayNodeList().forEach(device -> flowObjectiveService.forward(device.id(), fo)); |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | private void populateComputeNodeRules(long vni) { |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 380 | StreamSupport.stream(deviceService.getDevices().spliterator(), false) |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame^] | 381 | .filter(d -> isTypeOf(d.id(), OpenstackNodeService.NodeType.COMPUTE)) |
Kyuhwi Choi | 92d9ea4 | 2016-06-13 17:28:00 +0900 | [diff] [blame] | 382 | .forEach(d -> populateRuleToGatewayBySgw(d.id(), |
| 383 | gatewayService.getGroupIdForGatewayLoadBalance(d.id()), vni)); |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 384 | } |
| 385 | |
Kyuhwi Choi | 92d9ea4 | 2016-06-13 17:28:00 +0900 | [diff] [blame] | 386 | private void populateRuleToGatewayBySgw(DeviceId deviceId, GroupId groupId, long vni) { |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 387 | TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder(); |
| 388 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 389 | |
| 390 | sBuilder.matchEthType(Ethernet.TYPE_IPV4) |
| 391 | .matchTunnelId(vni) |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame^] | 392 | .matchEthDst(Constants.GATEWAY_MAC); |
| 393 | |
Kyuhwi Choi | 92d9ea4 | 2016-06-13 17:28:00 +0900 | [diff] [blame] | 394 | tBuilder.group(groupId); |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 395 | |
| 396 | ForwardingObjective fo = DefaultForwardingObjective.builder() |
| 397 | .withSelector(sBuilder.build()) |
| 398 | .withTreatment(tBuilder.build()) |
| 399 | .withFlag(ForwardingObjective.Flag.SPECIFIC) |
| 400 | .withPriority(ROUTING_RULE_PRIORITY) |
| 401 | .fromApp(appId) |
| 402 | .add(); |
| 403 | |
Kyuhwi Choi | 92d9ea4 | 2016-06-13 17:28:00 +0900 | [diff] [blame] | 404 | flowObjectiveService.forward(deviceId, fo); |
| 405 | } |
| 406 | |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame^] | 407 | /* |
Kyuhwi Choi | 92d9ea4 | 2016-06-13 17:28:00 +0900 | [diff] [blame] | 408 | private void populateRuleToGateway(DeviceId deviceId, Device gatewayDevice, long vni) { |
| 409 | TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder(); |
| 410 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 411 | |
| 412 | sBuilder.matchEthType(Ethernet.TYPE_IPV4) |
| 413 | .matchTunnelId(vni) |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame^] | 414 | .matchEthDst(Constants.GATEWAY_MAC); |
| 415 | tBuilder.extension(buildNiciraExtenstion(deviceId, nodeService.nodes().get(gatewayDevice.id())), deviceId) |
Kyuhwi Choi | 92d9ea4 | 2016-06-13 17:28:00 +0900 | [diff] [blame] | 416 | .setOutput(getTunnelPort(deviceId)); |
| 417 | |
| 418 | ForwardingObjective fo = DefaultForwardingObjective.builder() |
| 419 | .withSelector(sBuilder.build()) |
| 420 | .withTreatment(tBuilder.build()) |
| 421 | .withFlag(ForwardingObjective.Flag.SPECIFIC) |
| 422 | .withPriority(ROUTING_RULE_PRIORITY) |
| 423 | .fromApp(appId) |
| 424 | .add(); |
| 425 | |
| 426 | flowObjectiveService.forward(deviceId, fo); |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 427 | } |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame^] | 428 | */ |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 429 | |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 430 | private Device getGatewayNode() { |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame^] | 431 | |
| 432 | // TODO Return the correct gateway node |
| 433 | Optional<OpenstackNode> gwNode = nodeService.nodes().stream() |
| 434 | .filter(n -> n.type().equals(OpenstackNodeService.NodeType.GATEWAY)) |
| 435 | .findFirst(); |
| 436 | |
| 437 | if (!gwNode.isPresent()) { |
| 438 | log.warn("No Gateway is defined."); |
| 439 | return null; |
| 440 | } |
| 441 | |
| 442 | return deviceService.getDevice(gwNode.get().intBridge()); |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 443 | } |
| 444 | |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame^] | 445 | private boolean isTypeOf(DeviceId deviceId, OpenstackNodeService.NodeType type) { |
| 446 | |
| 447 | Optional<OpenstackNode> node = nodeService.nodes().stream() |
| 448 | .filter(n -> n.intBridge().equals(deviceId) || |
| 449 | (n.routerBridge().isPresent() && n.routerBridge().get().equals(deviceId))) |
| 450 | .filter(n -> n.type().equals(type)) |
| 451 | .findFirst(); |
| 452 | |
| 453 | if (node.isPresent()) { |
| 454 | return true; |
| 455 | } |
| 456 | |
| 457 | return false; |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 458 | } |
| 459 | |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 460 | private long getVni(String netId) { |
| 461 | return Long.parseLong(openstackService.network(netId).segmentId()); |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | /** |
| 465 | * Remove flow rules for external connection. |
| 466 | * |
| 467 | * @param routerInterface Corresponding routerInterface |
| 468 | */ |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 469 | public void removeExternalRules(OpenstackRouterInterface routerInterface) { |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 470 | OpenstackSubnet openstackSubnet = openstackService.subnet(routerInterface.subnetId()); |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 471 | TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder(); |
| 472 | sBuilder.matchEthType(Ethernet.TYPE_IPV4) |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 473 | .matchTunnelId(getVni(openstackSubnet.networkId())) |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame^] | 474 | .matchEthDst(Constants.GATEWAY_MAC); |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 475 | |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 476 | StreamSupport.stream(deviceService.getDevices().spliterator(), false) |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 477 | .forEach(d -> { |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame^] | 478 | ForwardingObjective.Flag flag = isTypeOf(d.id(), OpenstackNodeService.NodeType.GATEWAY) ? |
Kyuhwi Choi | 5e65e2c | 2016-04-01 11:48:15 +0900 | [diff] [blame] | 479 | ForwardingObjective.Flag.VERSATILE : |
| 480 | ForwardingObjective.Flag.SPECIFIC; |
Daniel Park | 2319390 | 2016-03-24 18:17:19 +0900 | [diff] [blame] | 481 | removeRule(d.id(), sBuilder, flag, ROUTING_RULE_PRIORITY); |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 482 | }); |
| 483 | |
| 484 | } |
| 485 | |
Kyuhwi Choi | 92d9ea4 | 2016-06-13 17:28:00 +0900 | [diff] [blame] | 486 | private void removeRule(DeviceId deviceId, TrafficSelector.Builder sBuilder, |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 487 | ForwardingObjective.Flag flag, int priority) { |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 488 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 489 | |
| 490 | ForwardingObjective fo = DefaultForwardingObjective.builder() |
| 491 | .withSelector(sBuilder.build()) |
| 492 | .withTreatment(tBuilder.build()) |
| 493 | .withFlag(flag) |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 494 | .withPriority(priority) |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 495 | .fromApp(appId) |
| 496 | .remove(); |
| 497 | |
Kyuhwi Choi | 92d9ea4 | 2016-06-13 17:28:00 +0900 | [diff] [blame] | 498 | flowObjectiveService.forward(deviceId, fo); |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 499 | } |
| 500 | |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 501 | /** |
| 502 | * Populates flow rules for floatingIp configuration. |
| 503 | * |
| 504 | * @param floatingIP Corresponding floating ip information |
| 505 | */ |
| 506 | public void populateFloatingIpRules(OpenstackFloatingIP floatingIP) { |
| 507 | OpenstackPort port = openstackService.port(floatingIP.portId()); |
| 508 | //1. incoming rules |
| 509 | populateFloatingIpIncomingRules(floatingIP, port); |
| 510 | //2. outgoing rules |
| 511 | populateFloatingIpOutgoingRules(floatingIP, port); |
| 512 | } |
| 513 | |
| 514 | private void populateFloatingIpIncomingRules(OpenstackFloatingIP floatingIP, OpenstackPort port) { |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 515 | TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder(); |
| 516 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 517 | |
| 518 | sBuilder.matchEthType(Ethernet.TYPE_IPV4) |
Daniel Park | 2319390 | 2016-03-24 18:17:19 +0900 | [diff] [blame] | 519 | .matchIPDst(IpPrefix.valueOf(floatingIP.floatingIpAddress(), PREFIX_LENGTH)); |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 520 | |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame^] | 521 | DeviceId gatewayDeviceId = DeviceId.deviceId(port.deviceId()); |
| 522 | Optional<IpAddress> ipAddress = nodeService.dataIp(gatewayDeviceId); |
| 523 | if (!ipAddress.isPresent()) { |
| 524 | log.warn("No IP address found for device {}", port.deviceId()); |
| 525 | return; |
| 526 | } |
| 527 | tBuilder.setEthSrc(Constants.GATEWAY_MAC) |
| 528 | .setEthDst(port.macAddress()) |
| 529 | .setIpDst(floatingIP.fixedIpAddress()) |
| 530 | .setTunnelId(getVni(port.networkId())) |
| 531 | .extension(buildNiciraExtenstion(gatewayDeviceId, |
| 532 | ipAddress.get().getIp4Address()), gatewayDeviceId) |
| 533 | .setOutput(getTunnelPort(gatewayDeviceId)); |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 534 | |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame^] | 535 | ForwardingObjective fo = DefaultForwardingObjective.builder() |
| 536 | .withSelector(sBuilder.build()) |
| 537 | .withTreatment(tBuilder.build()) |
| 538 | .withFlag(ForwardingObjective.Flag.VERSATILE) |
| 539 | .withPriority(FLOATING_RULE_PRIORITY) |
| 540 | .fromApp(appId) |
| 541 | .add(); |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 542 | |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame^] | 543 | flowObjectiveService.forward(getGatewayNode().id(), fo); |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | private void populateFloatingIpOutgoingRules(OpenstackFloatingIP floatingIP, OpenstackPort port) { |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 547 | TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder(); |
| 548 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 549 | |
| 550 | sBuilder.matchEthType(Ethernet.TYPE_IPV4) |
| 551 | .matchTunnelId(getVni(port.networkId())) |
| 552 | .matchIPSrc(IpPrefix.valueOf(floatingIP.fixedIpAddress(), 32)); |
| 553 | |
Kyuhwi Choi | 92d9ea4 | 2016-06-13 17:28:00 +0900 | [diff] [blame] | 554 | getGatewayNodeList().forEach(device -> { |
| 555 | DeviceId deviceId = device.id(); |
| 556 | tBuilder.setIpSrc(floatingIP.floatingIpAddress()) |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame^] | 557 | .setEthSrc(Constants.GW_EXT_INT_MAC) |
| 558 | .setEthDst(Constants.PHY_ROUTER_MAC) |
Kyuhwi Choi | 92d9ea4 | 2016-06-13 17:28:00 +0900 | [diff] [blame] | 559 | .setOutput(getExternalPortNum(deviceId)); |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 560 | |
Kyuhwi Choi | 92d9ea4 | 2016-06-13 17:28:00 +0900 | [diff] [blame] | 561 | ForwardingObjective fo = DefaultForwardingObjective.builder() |
| 562 | .withSelector(sBuilder.build()) |
| 563 | .withTreatment(tBuilder.build()) |
| 564 | .withFlag(ForwardingObjective.Flag.VERSATILE) |
| 565 | .withPriority(FLOATING_RULE_PRIORITY) |
| 566 | .fromApp(appId) |
| 567 | .add(); |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 568 | |
Kyuhwi Choi | 92d9ea4 | 2016-06-13 17:28:00 +0900 | [diff] [blame] | 569 | flowObjectiveService.forward(deviceId, fo); |
| 570 | }); |
| 571 | } |
| 572 | |
| 573 | private PortNumber getExternalPortNum(DeviceId deviceId) { |
| 574 | return checkNotNull(gatewayService.getGatewayExternalPorts(deviceId).get(0), PORTNOTNULL); |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | /** |
| 578 | * Removes flow rules for floating ip configuration. |
| 579 | * |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame^] | 580 | * @param floatingIp Corresponding floating ip information |
| 581 | * @param host host information for vm to remove |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 582 | */ |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame^] | 583 | public void removeFloatingIpRules(OpenstackFloatingIP floatingIp, Host host) { |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 584 | TrafficSelector.Builder sOutgoingBuilder = DefaultTrafficSelector.builder(); |
| 585 | TrafficSelector.Builder sIncomingBuilder = DefaultTrafficSelector.builder(); |
| 586 | |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame^] | 587 | // XXX FloatingIp.tenant_id() == host.vxlan_id ??? |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 588 | sOutgoingBuilder.matchEthType(Ethernet.TYPE_IPV4) |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame^] | 589 | .matchTunnelId(Integer.parseInt(host.annotations().value(Constants.VXLAN_ID))) |
| 590 | .matchIPSrc(IpPrefix.valueOf(floatingIp.fixedIpAddress(), PREFIX_LENGTH)); |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 591 | |
| 592 | sIncomingBuilder.matchEthType(Ethernet.TYPE_IPV4) |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame^] | 593 | .matchIPDst(IpPrefix.valueOf(floatingIp.floatingIpAddress(), PREFIX_LENGTH)); |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 594 | |
Kyuhwi Choi | 92d9ea4 | 2016-06-13 17:28:00 +0900 | [diff] [blame] | 595 | getGatewayNodeList().forEach(device -> { |
| 596 | removeRule(device.id(), sOutgoingBuilder, ForwardingObjective.Flag.VERSATILE, FLOATING_RULE_PRIORITY); |
| 597 | removeRule(device.id(), sIncomingBuilder, ForwardingObjective.Flag.VERSATILE, FLOATING_RULE_PRIORITY); |
| 598 | }); |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 599 | } |
| 600 | |
Kyuhwi Choi | 5e65e2c | 2016-04-01 11:48:15 +0900 | [diff] [blame] | 601 | /** |
| 602 | * Populates L3 rules for east to west traffic. |
| 603 | * |
Kyuhwi Choi | 92d9ea4 | 2016-06-13 17:28:00 +0900 | [diff] [blame] | 604 | * @param openstackPort target VM |
Kyuhwi Choi | 5e65e2c | 2016-04-01 11:48:15 +0900 | [diff] [blame] | 605 | * @param targetList target openstackRouterInterfaces |
| 606 | */ |
Kyuhwi Choi | 92d9ea4 | 2016-06-13 17:28:00 +0900 | [diff] [blame] | 607 | public void populateL3Rules(OpenstackPort openstackPort, List<OpenstackRouterInterface> targetList) { |
| 608 | Device device = getDevicefromOpenstackPort(openstackPort); |
| 609 | Port port = getPortFromOpenstackPort(device, openstackPort); |
| 610 | Ip4Address vmIp = openstackPort.fixedIps().values().iterator().next(); |
Kyuhwi Choi | 5e65e2c | 2016-04-01 11:48:15 +0900 | [diff] [blame] | 611 | |
| 612 | if (port == null) { |
| 613 | return; |
| 614 | } |
| 615 | |
| 616 | targetList.forEach(routerInterface -> { |
Kyuhwi Choi | 92d9ea4 | 2016-06-13 17:28:00 +0900 | [diff] [blame] | 617 | long vni = getVni(openstackService.port(routerInterface.portId()).networkId()); |
Kyuhwi Choi | 5e65e2c | 2016-04-01 11:48:15 +0900 | [diff] [blame] | 618 | |
| 619 | if (vmIp == null) { |
| 620 | return; |
| 621 | } |
| 622 | |
Kyuhwi Choi | 92d9ea4 | 2016-06-13 17:28:00 +0900 | [diff] [blame] | 623 | populateL3RulestoSameNode(vmIp, openstackPort, port, device, vni); |
Kyuhwi Choi | 5e65e2c | 2016-04-01 11:48:15 +0900 | [diff] [blame] | 624 | |
| 625 | deviceService.getAvailableDevices().forEach(d -> { |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame^] | 626 | if (!d.equals(device) && !d.equals(getGatewayNode())) { |
| 627 | populateL3RulestoDifferentNode(vmIp, vni, d.id(), |
| 628 | getHostIpfromOpenstackPort(openstackPort).getIp4Address()); |
Kyuhwi Choi | 5e65e2c | 2016-04-01 11:48:15 +0900 | [diff] [blame] | 629 | } |
| 630 | }); |
| 631 | |
| 632 | }); |
| 633 | } |
| 634 | |
| 635 | private void populateL3RulestoDifferentNode(Ip4Address vmIp, long vni, DeviceId deviceId, Ip4Address hostIp) { |
| 636 | TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder(); |
| 637 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 638 | |
| 639 | sBuilder.matchEthType(Ethernet.TYPE_IPV4) |
| 640 | .matchTunnelId(vni) |
| 641 | .matchIPDst(vmIp.toIpPrefix()); |
| 642 | tBuilder.extension(buildNiciraExtenstion(deviceId, hostIp), deviceId) |
| 643 | .setOutput(getTunnelPort(deviceId)); |
| 644 | |
| 645 | ForwardingObjective fo = DefaultForwardingObjective.builder() |
| 646 | .withSelector(sBuilder.build()) |
| 647 | .withTreatment(tBuilder.build()) |
| 648 | .withPriority(ROUTING_RULE_PRIORITY) |
| 649 | .withFlag(ForwardingObjective.Flag.SPECIFIC) |
| 650 | .fromApp(appId) |
| 651 | .add(); |
| 652 | |
| 653 | flowObjectiveService.forward(deviceId, fo); |
| 654 | } |
| 655 | |
| 656 | private void populateL3RulestoSameNode(Ip4Address vmIp, OpenstackPort p, Port port, Device device, long vni) { |
| 657 | TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder(); |
| 658 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 659 | |
| 660 | sBuilder.matchEthType(Ethernet.TYPE_IPV4) |
| 661 | .matchIPDst(vmIp.toIpPrefix()) |
| 662 | .matchTunnelId(vni); |
| 663 | |
| 664 | tBuilder.setEthDst(p.macAddress()) |
| 665 | .setOutput(port.number()); |
| 666 | |
| 667 | ForwardingObjective fo = DefaultForwardingObjective.builder() |
| 668 | .withSelector(sBuilder.build()) |
| 669 | .withTreatment(tBuilder.build()) |
| 670 | .withPriority(ROUTING_RULE_PRIORITY) |
| 671 | .withFlag(ForwardingObjective.Flag.SPECIFIC) |
| 672 | .fromApp(appId) |
| 673 | .add(); |
| 674 | |
| 675 | flowObjectiveService.forward(device.id(), fo); |
| 676 | } |
| 677 | |
| 678 | private Port getPortFromOpenstackPort(Device device, OpenstackPort p) { |
| 679 | String openstackPortName = PORTNAME_PREFIX_VM + p.id().substring(0, 11); |
| 680 | return deviceService.getPorts(device.id()) |
| 681 | .stream() |
| 682 | .filter(pt -> pt.annotations().value(PORTNAME).equals(openstackPortName)) |
| 683 | .findAny() |
| 684 | .orElse(null); |
| 685 | } |
| 686 | |
| 687 | /** |
| 688 | * Removes L3 rules for routerInterface events. |
| 689 | * |
| 690 | * @param vmIp Corresponding Vm ip |
| 691 | * @param routerInterfaces Corresponding routerInterfaces |
| 692 | */ |
| 693 | public void removeL3Rules(Ip4Address vmIp, List<OpenstackRouterInterface> routerInterfaces) { |
| 694 | if (vmIp == null) { |
| 695 | return; |
| 696 | } |
| 697 | |
| 698 | OpenstackRoutingService routingService = getService(OpenstackRoutingService.class); |
| 699 | |
| 700 | deviceService.getAvailableDevices().forEach(d -> { |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame^] | 701 | if (isTypeOf(d.id(), OpenstackNodeService.NodeType.COMPUTE)) { |
Kyuhwi Choi | 5e65e2c | 2016-04-01 11:48:15 +0900 | [diff] [blame] | 702 | routerInterfaces.forEach(routerInterface -> { |
| 703 | String networkId = routingService.networkIdForRouterInterface(routerInterface.portId()); |
| 704 | long vni = getVni(networkId); |
| 705 | |
| 706 | TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder(); |
| 707 | |
| 708 | sBuilder.matchEthType(Ethernet.TYPE_IPV4) |
| 709 | .matchIPDst(vmIp.toIpPrefix()) |
| 710 | .matchTunnelId(vni); |
| 711 | |
| 712 | removeRule(d.id(), sBuilder, ForwardingObjective.Flag.SPECIFIC, ROUTING_RULE_PRIORITY); |
| 713 | }); |
| 714 | } |
| 715 | }); |
| 716 | } |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 717 | } |