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.onlab.packet.MacAddress; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 22 | import org.onosproject.core.ApplicationId; |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 23 | import org.onosproject.net.Device; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 24 | import org.onosproject.net.DeviceId; |
| 25 | import org.onosproject.net.Port; |
| 26 | import org.onosproject.net.PortNumber; |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 27 | import org.onosproject.net.behaviour.ExtensionTreatmentResolver; |
| 28 | import org.onosproject.net.device.DeviceService; |
| 29 | import org.onosproject.net.driver.DefaultDriverData; |
| 30 | import org.onosproject.net.driver.DefaultDriverHandler; |
| 31 | import org.onosproject.net.driver.Driver; |
| 32 | import org.onosproject.net.driver.DriverHandler; |
| 33 | import org.onosproject.net.driver.DriverService; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 34 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 35 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 36 | import org.onosproject.net.flow.TrafficSelector; |
| 37 | import org.onosproject.net.flow.TrafficTreatment; |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 38 | import org.onosproject.net.flow.instructions.ExtensionTreatment; |
| 39 | import org.onosproject.net.flow.instructions.ExtensionPropertyException; |
| 40 | import org.onosproject.net.flow.instructions.ExtensionTreatmentType; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 41 | import org.onosproject.net.flowobjective.DefaultForwardingObjective; |
| 42 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
| 43 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 44 | import org.slf4j.Logger; |
| 45 | import org.slf4j.LoggerFactory; |
| 46 | |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 47 | import java.util.Collection; |
| 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); |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 56 | private static final int SWITCHING_RULE_PRIORITY = 50000; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 57 | |
| 58 | private FlowObjectiveService flowObjectiveService; |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 59 | private DriverService driverService; |
| 60 | private DeviceService deviceService; |
| 61 | private OpenstackRestHandler restHandler; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 62 | private ApplicationId appId; |
| 63 | |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 64 | private Collection<OpenstackNetwork> openstackNetworkList; |
| 65 | private Collection<OpenstackPort> openstackPortList; |
| 66 | |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 67 | /** |
Ray Milkey | 87b16b0 | 2015-10-30 11:50:00 -0700 | [diff] [blame] | 68 | * Creates OpenstackSwitchingRulPopulator. |
| 69 | * |
| 70 | * @param appId application id |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 71 | * @param flowObjectiveService FlowObjectiveService reference |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 72 | * @param deviceService DeviceService reference |
| 73 | * @param driverService DriverService reference |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 74 | */ |
| 75 | public OpenstackSwitchingRulePopulator(ApplicationId appId, |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 76 | FlowObjectiveService flowObjectiveService, |
| 77 | DeviceService deviceService, |
| 78 | OpenstackRestHandler restHandler, |
| 79 | DriverService driverService) { |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 80 | this.flowObjectiveService = flowObjectiveService; |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 81 | this.deviceService = deviceService; |
| 82 | this.driverService = driverService; |
| 83 | this.restHandler = restHandler; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 84 | this.appId = appId; |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 85 | |
| 86 | openstackNetworkList = restHandler.getNetworks(); |
| 87 | openstackPortList = restHandler.getPorts(); |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | /** |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 91 | * Populates flow rules for the VM created. |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 92 | * |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 93 | * @param device device to populate rules to |
| 94 | * @param port port 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 | public void populateSwitchingRules(Device device, Port port) { |
| 97 | populateFlowRulesForTrafficToSameCnode(device, port); |
| 98 | populateFlowRulesForTrafficToDifferentCnode(device, port); |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | /** |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 102 | * 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] | 103 | * |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 104 | * @param device device to put the rules |
| 105 | * @param port port info of the VM |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 106 | */ |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 107 | private void populateFlowRulesForTrafficToSameCnode(Device device, Port port) { |
| 108 | Ip4Address vmIp = getFixedIpAddressForPort(port.annotations().value("portName")); |
| 109 | if (vmIp != null) { |
| 110 | setFlowRuleForVMsInSameCnode(vmIp, device.id(), port); |
| 111 | } |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | /** |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 115 | * Populates the flow rules for traffic to VMs in different Cnode using |
| 116 | * Nicira extention. |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 117 | * |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 118 | * @param device device to put rules |
| 119 | * @param port port information of the VM |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 120 | */ |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 121 | private void populateFlowRulesForTrafficToDifferentCnode(Device device, Port port) { |
| 122 | String portName = port.annotations().value("portName"); |
| 123 | String channelId = device.annotations().value("channelId"); |
| 124 | Ip4Address hostIpAddress = Ip4Address.valueOf(channelId.split(":")[0]); |
| 125 | Ip4Address fixedIp = getFixedIpAddressForPort(portName); |
| 126 | MacAddress vmMac = getVmMacAddressForPort(portName); |
| 127 | String vni = getVniForPort(portName); |
| 128 | deviceService.getAvailableDevices().forEach(d -> { |
| 129 | if (!d.equals(device)) { |
| 130 | deviceService.getPorts(d.id()).forEach(p -> { |
| 131 | String pName = p.annotations().value("portName"); |
| 132 | if (!p.equals(port) && vni.equals(getVniForPort(pName))) { |
| 133 | String cidx = d.annotations().value("channelId"); |
| 134 | Ip4Address hostIpx = Ip4Address.valueOf(cidx.split(":")[0]); |
| 135 | MacAddress vmMacx = getVmMacAddressForPort(pName); |
| 136 | Ip4Address fixedIpx = getFixedIpAddressForPort(pName); |
| 137 | |
| 138 | setVxLanFlowRule(vni, device.id(), hostIpx, fixedIpx, vmMacx); |
| 139 | setVxLanFlowRule(vni, d.id(), hostIpAddress, fixedIp, vmMac); |
| 140 | } |
| 141 | }); |
| 142 | } |
| 143 | }); |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | /** |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 147 | * Returns the VNI of the VM of the port. |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 148 | * |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 149 | * @param portName VM port |
| 150 | * @return VNI |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 151 | */ |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 152 | private String getVniForPort(String portName) { |
| 153 | String uuid = portName.substring(3); |
| 154 | OpenstackPort port = openstackPortList.stream() |
| 155 | .filter(p -> p.id().startsWith(uuid)) |
| 156 | .findAny().orElse(null); |
| 157 | if (port == null) { |
| 158 | log.warn("No port information for port {}", portName); |
| 159 | return null; |
| 160 | } |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 161 | |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 162 | OpenstackNetwork network = openstackNetworkList.stream() |
| 163 | .filter(n -> n.id().equals(port.networkId())) |
| 164 | .findAny().orElse(null); |
| 165 | if (network == null) { |
Satish K | 2e2d635 | 2015-11-27 12:02:15 +0530 | [diff] [blame^] | 166 | log.warn("No VNI information for network {}", port.networkId()); |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 167 | return null; |
| 168 | } |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 169 | |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 170 | return network.segmentId(); |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | /** |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 174 | * Returns the Fixed IP address of the VM. |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 175 | * |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 176 | * @param portName VM port info |
| 177 | * @return IP address of the VM |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 178 | */ |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 179 | private Ip4Address getFixedIpAddressForPort(String portName) { |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 180 | |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 181 | String uuid = portName.substring(3); |
| 182 | OpenstackPort port = openstackPortList.stream() |
| 183 | .filter(p -> p.id().startsWith(uuid)) |
| 184 | .findFirst().orElse(null); |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 185 | |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 186 | if (port == null) { |
| 187 | log.error("There is no port information for port name {}", portName); |
| 188 | return null; |
| 189 | } |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 190 | |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 191 | if (port.fixedIps().isEmpty()) { |
| 192 | log.error("There is no fixed IP info in the port information"); |
| 193 | return null; |
| 194 | } |
| 195 | |
| 196 | return (Ip4Address) port.fixedIps().values().toArray()[0]; |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Returns the MAC address of the VM of the port. |
| 201 | * |
| 202 | * @param portName VM port |
| 203 | * @return MAC address of the VM |
| 204 | */ |
| 205 | private MacAddress getVmMacAddressForPort(String portName) { |
| 206 | |
| 207 | String uuid = portName.substring(3); |
| 208 | OpenstackPort port = openstackPortList.stream() |
| 209 | .filter(p -> p.id().startsWith(uuid)) |
| 210 | .findFirst().orElse(null); |
| 211 | |
| 212 | if (port == null) { |
| 213 | log.error("There is port information for port name {}", portName); |
| 214 | return null; |
| 215 | } |
| 216 | |
| 217 | return port.macAddress(); |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Sets the flow rules for traffic between VMs in the same Cnode. |
| 222 | * |
| 223 | * @param ip4Address VM IP address |
| 224 | * @param id device ID to put rules |
| 225 | * @param port VM port |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 226 | */ |
| 227 | private void setFlowRuleForVMsInSameCnode(Ip4Address ip4Address, DeviceId id, |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 228 | Port port) { |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 229 | TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder(); |
| 230 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 231 | |
| 232 | sBuilder.matchEthType(Ethernet.TYPE_IPV4) |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 233 | .matchIPDst(ip4Address.toIpPrefix()); |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 234 | tBuilder.setOutput(port.number()); |
| 235 | |
| 236 | ForwardingObjective fo = DefaultForwardingObjective.builder() |
| 237 | .withSelector(sBuilder.build()) |
| 238 | .withTreatment(tBuilder.build()) |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 239 | .withPriority(SWITCHING_RULE_PRIORITY) |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 240 | .withFlag(ForwardingObjective.Flag.VERSATILE) |
| 241 | .fromApp(appId) |
| 242 | .add(); |
| 243 | |
| 244 | flowObjectiveService.forward(id, fo); |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Sets the flow rules between traffic from VMs in different Cnode. |
| 249 | * |
| 250 | * @param vni VNI |
| 251 | * @param id device ID |
| 252 | * @param hostIp host IP of the VM |
| 253 | * @param vmIp fixed IP of the VM |
| 254 | * @param vmMac MAC address of the VM |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 255 | */ |
| 256 | private void setVxLanFlowRule(String vni, DeviceId id, Ip4Address hostIp, |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 257 | Ip4Address vmIp, MacAddress vmMac) { |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 258 | TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder(); |
| 259 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 260 | |
| 261 | sBuilder.matchEthType(Ethernet.TYPE_IPV4) |
| 262 | .matchIPDst(vmIp.toIpPrefix()); |
| 263 | tBuilder.setTunnelId(Long.parseLong(vni)) |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 264 | .extension(buildNiciraExtenstion(id, hostIp), id) |
| 265 | .setOutput(getTunnelPort(id)); |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 266 | |
| 267 | ForwardingObjective fo = DefaultForwardingObjective.builder() |
| 268 | .withSelector(sBuilder.build()) |
| 269 | .withTreatment(tBuilder.build()) |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 270 | .withPriority(SWITCHING_RULE_PRIORITY) |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 271 | .withFlag(ForwardingObjective.Flag.VERSATILE) |
| 272 | .fromApp(appId) |
| 273 | .add(); |
| 274 | |
| 275 | flowObjectiveService.forward(id, fo); |
| 276 | } |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 277 | |
| 278 | private ExtensionTreatment buildNiciraExtenstion(DeviceId id, Ip4Address hostIp) { |
| 279 | Driver driver = driverService.getDriver(id); |
| 280 | DriverHandler driverHandler = new DefaultDriverHandler(new DefaultDriverData(driver, id)); |
| 281 | ExtensionTreatmentResolver resolver = driverHandler.behaviour(ExtensionTreatmentResolver.class); |
| 282 | |
| 283 | ExtensionTreatment extensionInstruction = |
| 284 | resolver.getExtensionInstruction( |
| 285 | ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_TUNNEL_DST.type()); |
| 286 | |
| 287 | try { |
| 288 | extensionInstruction.setPropertyValue("tunnelDst", hostIp); |
| 289 | } catch (ExtensionPropertyException e) { |
| 290 | log.error("Error setting Nicira extension setting {}", e); |
| 291 | } |
| 292 | |
| 293 | return extensionInstruction; |
| 294 | } |
| 295 | |
| 296 | private PortNumber getTunnelPort(DeviceId id) { |
| 297 | Port port = deviceService.getPorts(id).stream() |
| 298 | .filter(p -> p.annotations().value("portName").equals("vxlan")) |
| 299 | .findAny().orElse(null); |
| 300 | |
| 301 | if (port == null) { |
| 302 | log.error("No TunnelPort was created."); |
| 303 | return null; |
| 304 | } |
| 305 | return port.number(); |
| 306 | } |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 307 | } |