sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | |
| 17 | package org.onosproject.openstackswitching; |
| 18 | |
| 19 | import org.onlab.packet.Ethernet; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 20 | import org.onlab.packet.Ip4Address; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 21 | import org.onosproject.core.ApplicationId; |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 22 | import org.onosproject.net.Device; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 23 | import org.onosproject.net.DeviceId; |
| 24 | import org.onosproject.net.Port; |
| 25 | import org.onosproject.net.PortNumber; |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 26 | import org.onosproject.net.behaviour.ExtensionTreatmentResolver; |
| 27 | import org.onosproject.net.device.DeviceService; |
| 28 | import org.onosproject.net.driver.DefaultDriverData; |
| 29 | import org.onosproject.net.driver.DefaultDriverHandler; |
| 30 | import org.onosproject.net.driver.Driver; |
| 31 | import org.onosproject.net.driver.DriverHandler; |
| 32 | import org.onosproject.net.driver.DriverService; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 33 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 34 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 35 | import org.onosproject.net.flow.TrafficSelector; |
| 36 | import org.onosproject.net.flow.TrafficTreatment; |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 37 | import org.onosproject.net.flow.instructions.ExtensionTreatment; |
| 38 | import org.onosproject.net.flow.instructions.ExtensionPropertyException; |
| 39 | import org.onosproject.net.flow.instructions.ExtensionTreatmentType; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 40 | import org.onosproject.net.flowobjective.DefaultForwardingObjective; |
| 41 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
| 42 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 43 | import org.slf4j.Logger; |
| 44 | import org.slf4j.LoggerFactory; |
| 45 | |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 46 | import java.util.Collection; |
Daniel Park | 5df6518 | 2016-01-09 00:12:03 +0900 | [diff] [blame] | 47 | import java.util.Map; |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 48 | |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 49 | /** |
sanghoshin | 46297d2 | 2015-11-03 17:51:24 +0900 | [diff] [blame] | 50 | * Populates switching flow rules. |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 51 | */ |
| 52 | public class OpenstackSwitchingRulePopulator { |
| 53 | |
| 54 | private static Logger log = LoggerFactory |
| 55 | .getLogger(OpenstackSwitchingRulePopulator.class); |
daniel | 2a2bd7b | 2015-12-02 13:53:58 +0900 | [diff] [blame] | 56 | private static final int SWITCHING_RULE_PRIORITY = 30000; |
| 57 | private static final int EAST_WEST_ROUTING_RULE_PRIORITY = 29000; |
| 58 | private static final int TUNNELTAG_RULE_PRIORITY = 30000; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 59 | |
| 60 | private FlowObjectiveService flowObjectiveService; |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 61 | private DriverService driverService; |
| 62 | private DeviceService deviceService; |
| 63 | private OpenstackRestHandler restHandler; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 64 | private ApplicationId appId; |
| 65 | |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 66 | private Collection<OpenstackNetwork> openstackNetworkList; |
| 67 | private Collection<OpenstackPort> openstackPortList; |
| 68 | |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 69 | /** |
Ray Milkey | 87b16b0 | 2015-10-30 11:50:00 -0700 | [diff] [blame] | 70 | * Creates OpenstackSwitchingRulPopulator. |
| 71 | * |
| 72 | * @param appId application id |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 73 | * @param flowObjectiveService FlowObjectiveService reference |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 74 | * @param deviceService DeviceService reference |
Jian Li | dfba739 | 2016-01-22 16:46:58 -0800 | [diff] [blame^] | 75 | * @param restHandler OpenstackRestHandler reference |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 76 | * @param driverService DriverService reference |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 77 | */ |
| 78 | public OpenstackSwitchingRulePopulator(ApplicationId appId, |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 79 | FlowObjectiveService flowObjectiveService, |
| 80 | DeviceService deviceService, |
| 81 | OpenstackRestHandler restHandler, |
| 82 | DriverService driverService) { |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 83 | this.flowObjectiveService = flowObjectiveService; |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 84 | this.deviceService = deviceService; |
| 85 | this.driverService = driverService; |
| 86 | this.restHandler = restHandler; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 87 | this.appId = appId; |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 88 | |
| 89 | openstackNetworkList = restHandler.getNetworks(); |
| 90 | openstackPortList = restHandler.getPorts(); |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 91 | } |
| 92 | |
daniel | 2a2bd7b | 2015-12-02 13:53:58 +0900 | [diff] [blame] | 93 | |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 94 | /** |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 95 | * Populates flow rules for the VM created. |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 96 | * |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 97 | * @param device device to populate rules to |
| 98 | * @param port port for the VM created |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 99 | */ |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 100 | public void populateSwitchingRules(Device device, Port port) { |
Hyunsun Moon | 74ec406 | 2015-12-09 10:58:32 -0800 | [diff] [blame] | 101 | populateFlowRulesForTunnelTag(device, port); |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 102 | populateFlowRulesForTrafficToSameCnode(device, port); |
| 103 | populateFlowRulesForTrafficToDifferentCnode(device, port); |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | /** |
daniel | 2a2bd7b | 2015-12-02 13:53:58 +0900 | [diff] [blame] | 107 | * Populate the flow rules for tagging tunnelId according to which inport is came from. |
| 108 | * |
| 109 | * @param device device to put the rules |
| 110 | * @param port port info of the VM |
| 111 | */ |
Hyunsun Moon | 74ec406 | 2015-12-09 10:58:32 -0800 | [diff] [blame] | 112 | private void populateFlowRulesForTunnelTag(Device device, Port port) { |
daniel | 2a2bd7b | 2015-12-02 13:53:58 +0900 | [diff] [blame] | 113 | Ip4Address vmIp = getFixedIpAddressForPort(port.annotations().value("portName")); |
| 114 | String portName = port.annotations().value("portName"); |
| 115 | String vni = getVniForPort(portName); |
| 116 | |
| 117 | if (vmIp != null) { |
Hyunsun Moon | 74ec406 | 2015-12-09 10:58:32 -0800 | [diff] [blame] | 118 | setFlowRuleForTunnelTag(device.id(), port, vni); |
daniel | 2a2bd7b | 2015-12-02 13:53:58 +0900 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | |
Daniel Park | 5df6518 | 2016-01-09 00:12:03 +0900 | [diff] [blame] | 122 | private void setFlowRuleForTunnelTag(DeviceId deviceId, Port port, String vni) { |
sanghoshin | 075e3e7 | 2015-11-25 16:34:29 +0900 | [diff] [blame] | 123 | |
Daniel Park | 5df6518 | 2016-01-09 00:12:03 +0900 | [diff] [blame] | 124 | TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder(); |
| 125 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 126 | |
| 127 | sBuilder.matchEthType(Ethernet.TYPE_IPV4) |
| 128 | .matchInPort(port.number()); |
| 129 | |
| 130 | tBuilder.setTunnelId(Long.parseLong(vni)); |
| 131 | |
| 132 | ForwardingObjective fo = DefaultForwardingObjective.builder() |
| 133 | .withSelector(sBuilder.build()) |
| 134 | .withTreatment(tBuilder.build()) |
| 135 | .withPriority(TUNNELTAG_RULE_PRIORITY) |
| 136 | .withFlag(ForwardingObjective.Flag.SPECIFIC) |
| 137 | .fromApp(appId) |
| 138 | .add(); |
| 139 | |
| 140 | flowObjectiveService.forward(deviceId, fo); |
sanghoshin | 65723ae | 2015-11-17 22:07:21 +0900 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | /** |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 144 | * Populates the flow rules for traffic to VMs in the same Cnode as the sender. |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 145 | * |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 146 | * @param device device to put the rules |
| 147 | * @param port port info of the VM |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 148 | */ |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 149 | private void populateFlowRulesForTrafficToSameCnode(Device device, Port port) { |
| 150 | Ip4Address vmIp = getFixedIpAddressForPort(port.annotations().value("portName")); |
daniel | 2a2bd7b | 2015-12-02 13:53:58 +0900 | [diff] [blame] | 151 | String portName = port.annotations().value("portName"); |
| 152 | String vni = getVniForPort(portName); |
daniel | 2a2bd7b | 2015-12-02 13:53:58 +0900 | [diff] [blame] | 153 | |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 154 | if (vmIp != null) { |
Daniel Park | 5df6518 | 2016-01-09 00:12:03 +0900 | [diff] [blame] | 155 | setFlowRuleForVMsInSameCnode(vmIp, device.id(), port, vni); |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 156 | } |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | /** |
Daniel Park | 5df6518 | 2016-01-09 00:12:03 +0900 | [diff] [blame] | 160 | * Sets the flow rules for traffic between VMs in the same Cnode. |
| 161 | * |
| 162 | * @param ip4Address VM IP address |
| 163 | * @param id device ID to put rules |
| 164 | * @param port VM port |
| 165 | * @param vni VM VNI |
| 166 | */ |
| 167 | private void setFlowRuleForVMsInSameCnode(Ip4Address ip4Address, DeviceId id, |
| 168 | Port port, String vni) { |
| 169 | |
| 170 | //For L2 Switching Case |
| 171 | TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder(); |
| 172 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 173 | |
| 174 | sBuilder.matchEthType(Ethernet.TYPE_IPV4) |
| 175 | .matchIPDst(ip4Address.toIpPrefix()) |
| 176 | .matchTunnelId(Long.parseLong(vni)); |
| 177 | |
| 178 | tBuilder.setOutput(port.number()); |
| 179 | |
| 180 | ForwardingObjective fo = DefaultForwardingObjective.builder() |
| 181 | .withSelector(sBuilder.build()) |
| 182 | .withTreatment(tBuilder.build()) |
| 183 | .withPriority(SWITCHING_RULE_PRIORITY) |
| 184 | .withFlag(ForwardingObjective.Flag.SPECIFIC) |
| 185 | .fromApp(appId) |
| 186 | .add(); |
| 187 | |
| 188 | flowObjectiveService.forward(id, fo); |
| 189 | } |
| 190 | |
| 191 | /** |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 192 | * Populates the flow rules for traffic to VMs in different Cnode using |
| 193 | * Nicira extention. |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 194 | * |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 195 | * @param device device to put rules |
| 196 | * @param port port information of the VM |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 197 | */ |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 198 | private void populateFlowRulesForTrafficToDifferentCnode(Device device, Port port) { |
| 199 | String portName = port.annotations().value("portName"); |
| 200 | String channelId = device.annotations().value("channelId"); |
| 201 | Ip4Address hostIpAddress = Ip4Address.valueOf(channelId.split(":")[0]); |
| 202 | Ip4Address fixedIp = getFixedIpAddressForPort(portName); |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 203 | String vni = getVniForPort(portName); |
| 204 | deviceService.getAvailableDevices().forEach(d -> { |
| 205 | if (!d.equals(device)) { |
| 206 | deviceService.getPorts(d.id()).forEach(p -> { |
| 207 | String pName = p.annotations().value("portName"); |
| 208 | if (!p.equals(port) && vni.equals(getVniForPort(pName))) { |
| 209 | String cidx = d.annotations().value("channelId"); |
| 210 | Ip4Address hostIpx = Ip4Address.valueOf(cidx.split(":")[0]); |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 211 | Ip4Address fixedIpx = getFixedIpAddressForPort(pName); |
sanghoshin | 46c6e3e | 2015-12-18 18:42:17 +0900 | [diff] [blame] | 212 | if (port.isEnabled() || |
| 213 | port.annotations().value("portName").startsWith( |
| 214 | OpenstackSwitchingManager.PORTNAME_PREFIX_ROUTER)) { |
Daniel Park | 5df6518 | 2016-01-09 00:12:03 +0900 | [diff] [blame] | 215 | setVxLanFlowRule(vni, device.id(), hostIpx, fixedIpx); |
| 216 | setVxLanFlowRule(vni, d.id(), hostIpAddress, fixedIp); |
sanghoshin | 65723ae | 2015-11-17 22:07:21 +0900 | [diff] [blame] | 217 | } |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 218 | } |
| 219 | }); |
| 220 | } |
| 221 | }); |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | /** |
Daniel Park | 5df6518 | 2016-01-09 00:12:03 +0900 | [diff] [blame] | 225 | * Sets the flow rules between traffic from VMs in different Cnode. |
| 226 | * |
| 227 | * @param vni VNI |
| 228 | * @param deviceId device ID |
| 229 | * @param hostIp host IP of the VM |
| 230 | * @param vmIp fixed IP of the VM |
| 231 | */ |
| 232 | private void setVxLanFlowRule(String vni, DeviceId deviceId, Ip4Address hostIp, |
| 233 | Ip4Address vmIp) { |
| 234 | TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder(); |
| 235 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 236 | |
| 237 | sBuilder.matchEthType(Ethernet.TYPE_IPV4) |
| 238 | .matchTunnelId(Long.parseLong(vni)) |
| 239 | .matchIPDst(vmIp.toIpPrefix()); |
| 240 | tBuilder.extension(buildNiciraExtenstion(deviceId, hostIp), deviceId) |
| 241 | .setOutput(getTunnelPort(deviceId)); |
| 242 | |
| 243 | ForwardingObjective fo = DefaultForwardingObjective.builder() |
| 244 | .withSelector(sBuilder.build()) |
| 245 | .withTreatment(tBuilder.build()) |
| 246 | .withPriority(SWITCHING_RULE_PRIORITY) |
| 247 | .withFlag(ForwardingObjective.Flag.SPECIFIC) |
| 248 | .fromApp(appId) |
| 249 | .add(); |
| 250 | |
| 251 | flowObjectiveService.forward(deviceId, fo); |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Returns OpenstackPort object for the Port reference given. |
| 256 | * |
| 257 | * @param port Port object |
| 258 | * @return OpenstackPort reference, or null when not found |
| 259 | */ |
| 260 | public OpenstackPort openstackPort(Port port) { |
| 261 | String uuid = port.annotations().value("portName").substring(3); |
| 262 | return openstackPortList.stream().filter(p -> p.id().startsWith(uuid)) |
| 263 | .findAny().orElse(null); |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * Remove flows rules for the removed VM. |
| 268 | * |
| 269 | * @param removedPort removedport info |
| 270 | * @param openstackPortInfoMap openstackPortInfoMap |
| 271 | */ |
| 272 | public void removeSwitchingRules(Port removedPort, Map<String, OpenstackPortInfo> openstackPortInfoMap) { |
| 273 | |
| 274 | OpenstackPortInfo openstackPortInfo = openstackPortInfoMap |
| 275 | .get(removedPort.annotations().value("portName")); |
| 276 | |
| 277 | DeviceId deviceId = openstackPortInfo.deviceId(); |
| 278 | Ip4Address vmIp = openstackPortInfo.ip(); |
| 279 | PortNumber portNumber = removedPort.number(); |
| 280 | long vni = openstackPortInfo.vni(); |
| 281 | |
| 282 | removeFlowRuleForTunnelTag(deviceId, portNumber); |
| 283 | removeFlowRuleForVMsInSameCnode(deviceId, vmIp, vni); |
| 284 | removeFlowRuleForVMsInDiffrentCnode(removedPort, deviceId, vmIp, vni, openstackPortInfoMap); |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * Removes flow rules for tagging tunnelId. |
| 289 | * |
| 290 | * @param deviceId device id |
| 291 | * @param portNumber port number |
| 292 | */ |
| 293 | private void removeFlowRuleForTunnelTag(DeviceId deviceId, PortNumber portNumber) { |
| 294 | TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder(); |
| 295 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 296 | |
| 297 | sBuilder.matchEthType(Ethernet.TYPE_IPV4) |
| 298 | .matchInPort(portNumber); |
| 299 | |
| 300 | ForwardingObjective fo = DefaultForwardingObjective.builder() |
| 301 | .withSelector(sBuilder.build()) |
| 302 | .withTreatment(tBuilder.build()) |
| 303 | .withPriority(TUNNELTAG_RULE_PRIORITY) |
| 304 | .withFlag(ForwardingObjective.Flag.SPECIFIC) |
| 305 | .fromApp(appId) |
| 306 | .remove(); |
| 307 | |
| 308 | flowObjectiveService.forward(deviceId, fo); |
| 309 | } |
| 310 | |
| 311 | /** |
| 312 | * Removes the flow rules for traffic between VMs in the same Cnode. |
| 313 | * |
| 314 | * @param deviceId device id on which removed VM was run |
| 315 | * @param vmIp ip of the removed VM |
| 316 | * @param vni vni which removed VM was belonged |
| 317 | */ |
| 318 | private void removeFlowRuleForVMsInSameCnode(DeviceId deviceId, Ip4Address vmIp, long vni) { |
| 319 | TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder(); |
| 320 | |
| 321 | sBuilder.matchEthType(Ethernet.TYPE_IPV4) |
| 322 | .matchIPDst(vmIp.toIpPrefix()) |
| 323 | .matchTunnelId(vni); |
| 324 | |
| 325 | ForwardingObjective fo = DefaultForwardingObjective.builder() |
| 326 | .withSelector(sBuilder.build()) |
| 327 | .withTreatment(DefaultTrafficTreatment.builder().build()) |
| 328 | .withFlag(ForwardingObjective.Flag.SPECIFIC) |
| 329 | .withPriority(SWITCHING_RULE_PRIORITY) |
| 330 | .fromApp(appId) |
| 331 | .remove(); |
| 332 | |
| 333 | flowObjectiveService.forward(deviceId, fo); |
| 334 | } |
| 335 | |
| 336 | /** |
| 337 | * Removes the flow rules for traffic between VMs in the different Cnode. |
| 338 | * |
| 339 | * @param removedPort removedport info |
| 340 | * @param deviceId device id on which removed VM was run |
| 341 | * @param vmIp ip of the removed VM |
| 342 | * @param vni vni which removed VM was belonged |
| 343 | * @param openstackPortInfoMap openstackPortInfoMap |
| 344 | */ |
| 345 | private void removeFlowRuleForVMsInDiffrentCnode(Port removedPort, DeviceId deviceId, Ip4Address vmIp, |
| 346 | long vni, Map<String, OpenstackPortInfo> openstackPortInfoMap) { |
| 347 | |
| 348 | final boolean anyPortRemainedInSameCnode |
| 349 | = checkIfAnyPortRemainedInSameCnode(removedPort, deviceId, vni, openstackPortInfoMap); |
| 350 | |
| 351 | openstackPortInfoMap.forEach((port, portInfo) -> { |
| 352 | if (portInfo.vni() == vni && !portInfo.deviceId().equals(deviceId)) { |
| 353 | removeVxLanFlowRule(portInfo.deviceId(), vmIp, vni); |
| 354 | if (!anyPortRemainedInSameCnode) { |
| 355 | removeVxLanFlowRule(deviceId, portInfo.ip(), vni); |
| 356 | } |
| 357 | } |
| 358 | }); |
| 359 | } |
| 360 | |
| 361 | /** |
| 362 | * Removes the flow rules between traffic from VMs in different Cnode. |
| 363 | * |
| 364 | * @param deviceId device id |
| 365 | * @param vmIp ip |
| 366 | * @param vni vni which removed VM was belonged |
| 367 | */ |
| 368 | private void removeVxLanFlowRule(DeviceId deviceId, Ip4Address vmIp, long vni) { |
| 369 | TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder(); |
| 370 | |
| 371 | sBuilder.matchEthType(Ethernet.TYPE_IPV4) |
| 372 | .matchTunnelId(vni) |
| 373 | .matchIPDst(vmIp.toIpPrefix()); |
| 374 | |
| 375 | ForwardingObjective fo = DefaultForwardingObjective.builder() |
| 376 | .withSelector(sBuilder.build()) |
| 377 | .withTreatment(DefaultTrafficTreatment.builder().build()) |
| 378 | .withFlag(ForwardingObjective.Flag.SPECIFIC) |
| 379 | .withPriority(SWITCHING_RULE_PRIORITY) |
| 380 | .fromApp(appId) |
| 381 | .remove(); |
| 382 | |
| 383 | flowObjectiveService.forward(deviceId, fo); |
| 384 | } |
| 385 | |
| 386 | /** |
| 387 | * Checks if there is any port remained with same vni at the device, on which the removed VM was run. |
| 388 | * |
| 389 | * @param removedPort removedport info |
| 390 | * @param deviceId device id on which removed VM was run |
| 391 | * @param vni vni which removed VM was belonged |
| 392 | * @param openstackPortInfoMap openstackPortInfoMap |
| 393 | * @return true if there is, false otherwise |
| 394 | */ |
| 395 | private boolean checkIfAnyPortRemainedInSameCnode(Port removedPort, DeviceId deviceId, long vni, |
| 396 | Map<String, OpenstackPortInfo> openstackPortInfoMap) { |
| 397 | |
| 398 | for (Map.Entry<String, OpenstackPortInfo> entry : openstackPortInfoMap.entrySet()) { |
| 399 | if (!removedPort.annotations().value("portName").equals(entry.getKey())) { |
| 400 | if (entry.getValue().vni() == vni && entry.getValue().deviceId().equals(deviceId)) { |
| 401 | return true; |
| 402 | } |
| 403 | } |
| 404 | } |
| 405 | return false; |
| 406 | } |
| 407 | |
| 408 | /** |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 409 | * Returns the VNI of the VM of the port. |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 410 | * |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 411 | * @param portName VM port |
| 412 | * @return VNI |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 413 | */ |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 414 | private String getVniForPort(String portName) { |
| 415 | String uuid = portName.substring(3); |
| 416 | OpenstackPort port = openstackPortList.stream() |
| 417 | .filter(p -> p.id().startsWith(uuid)) |
| 418 | .findAny().orElse(null); |
| 419 | if (port == null) { |
daniel | 2a2bd7b | 2015-12-02 13:53:58 +0900 | [diff] [blame] | 420 | log.debug("No port information for port {}", portName); |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 421 | return null; |
| 422 | } |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 423 | |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 424 | OpenstackNetwork network = openstackNetworkList.stream() |
| 425 | .filter(n -> n.id().equals(port.networkId())) |
| 426 | .findAny().orElse(null); |
| 427 | if (network == null) { |
Satish K | 2e2d635 | 2015-11-27 12:02:15 +0530 | [diff] [blame] | 428 | log.warn("No VNI information for network {}", port.networkId()); |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 429 | return null; |
| 430 | } |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 431 | |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 432 | return network.segmentId(); |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | /** |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 436 | * Returns the Fixed IP address of the VM. |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 437 | * |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 438 | * @param portName VM port info |
| 439 | * @return IP address of the VM |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 440 | */ |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 441 | private Ip4Address getFixedIpAddressForPort(String portName) { |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 442 | |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 443 | String uuid = portName.substring(3); |
| 444 | OpenstackPort port = openstackPortList.stream() |
| 445 | .filter(p -> p.id().startsWith(uuid)) |
| 446 | .findFirst().orElse(null); |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 447 | |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 448 | if (port == null) { |
| 449 | log.error("There is no port information for port name {}", portName); |
| 450 | return null; |
| 451 | } |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 452 | |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 453 | if (port.fixedIps().isEmpty()) { |
| 454 | log.error("There is no fixed IP info in the port information"); |
| 455 | return null; |
| 456 | } |
| 457 | |
| 458 | return (Ip4Address) port.fixedIps().values().toArray()[0]; |
| 459 | } |
| 460 | |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 461 | private ExtensionTreatment buildNiciraExtenstion(DeviceId id, Ip4Address hostIp) { |
| 462 | Driver driver = driverService.getDriver(id); |
| 463 | DriverHandler driverHandler = new DefaultDriverHandler(new DefaultDriverData(driver, id)); |
| 464 | ExtensionTreatmentResolver resolver = driverHandler.behaviour(ExtensionTreatmentResolver.class); |
| 465 | |
| 466 | ExtensionTreatment extensionInstruction = |
| 467 | resolver.getExtensionInstruction( |
| 468 | ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_TUNNEL_DST.type()); |
| 469 | |
| 470 | try { |
| 471 | extensionInstruction.setPropertyValue("tunnelDst", hostIp); |
| 472 | } catch (ExtensionPropertyException e) { |
| 473 | log.error("Error setting Nicira extension setting {}", e); |
| 474 | } |
| 475 | |
| 476 | return extensionInstruction; |
| 477 | } |
| 478 | |
Daniel Park | 5df6518 | 2016-01-09 00:12:03 +0900 | [diff] [blame] | 479 | private PortNumber getTunnelPort(DeviceId deviceId) { |
| 480 | Port port = deviceService.getPorts(deviceId).stream() |
sanghoshin | 46c6e3e | 2015-12-18 18:42:17 +0900 | [diff] [blame] | 481 | .filter(p -> p.annotations().value("portName").equals( |
| 482 | OpenstackSwitchingManager.PORTNAME_PREFIX_TUNNEL)) |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 483 | .findAny().orElse(null); |
| 484 | |
| 485 | if (port == null) { |
| 486 | log.error("No TunnelPort was created."); |
| 487 | return null; |
| 488 | } |
| 489 | return port.number(); |
| 490 | } |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 491 | } |