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