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