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