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