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 | 075e3e7 | 2015-11-25 16:34:29 +0900 | [diff] [blame^] | 102 | * Returns OpenstackPort object for the Port reference given. |
| 103 | * |
| 104 | * @param port Port object |
| 105 | * @return OpenstackPort reference, or null when not found |
| 106 | */ |
| 107 | public OpenstackPort openstackPort(Port port) { |
| 108 | String uuid = port.annotations().value("portName").substring(3); |
| 109 | return openstackPortList.stream().filter(p -> p.id().startsWith(uuid)) |
| 110 | .findAny().orElse(null); |
| 111 | } |
| 112 | |
| 113 | /** |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 114 | * 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] | 115 | * |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 116 | * @param device device to put the rules |
| 117 | * @param port port info of the VM |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 118 | */ |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 119 | private void populateFlowRulesForTrafficToSameCnode(Device device, Port port) { |
| 120 | Ip4Address vmIp = getFixedIpAddressForPort(port.annotations().value("portName")); |
| 121 | if (vmIp != null) { |
| 122 | setFlowRuleForVMsInSameCnode(vmIp, device.id(), port); |
| 123 | } |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | /** |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 127 | * Populates the flow rules for traffic to VMs in different Cnode using |
| 128 | * Nicira extention. |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 129 | * |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 130 | * @param device device to put rules |
| 131 | * @param port port information of the VM |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 132 | */ |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 133 | private void populateFlowRulesForTrafficToDifferentCnode(Device device, Port port) { |
| 134 | String portName = port.annotations().value("portName"); |
| 135 | String channelId = device.annotations().value("channelId"); |
| 136 | Ip4Address hostIpAddress = Ip4Address.valueOf(channelId.split(":")[0]); |
| 137 | Ip4Address fixedIp = getFixedIpAddressForPort(portName); |
| 138 | MacAddress vmMac = getVmMacAddressForPort(portName); |
| 139 | String vni = getVniForPort(portName); |
| 140 | deviceService.getAvailableDevices().forEach(d -> { |
| 141 | if (!d.equals(device)) { |
| 142 | deviceService.getPorts(d.id()).forEach(p -> { |
| 143 | String pName = p.annotations().value("portName"); |
| 144 | if (!p.equals(port) && vni.equals(getVniForPort(pName))) { |
| 145 | String cidx = d.annotations().value("channelId"); |
| 146 | Ip4Address hostIpx = Ip4Address.valueOf(cidx.split(":")[0]); |
| 147 | MacAddress vmMacx = getVmMacAddressForPort(pName); |
| 148 | Ip4Address fixedIpx = getFixedIpAddressForPort(pName); |
| 149 | |
| 150 | setVxLanFlowRule(vni, device.id(), hostIpx, fixedIpx, vmMacx); |
| 151 | setVxLanFlowRule(vni, d.id(), hostIpAddress, fixedIp, vmMac); |
| 152 | } |
| 153 | }); |
| 154 | } |
| 155 | }); |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | /** |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 159 | * Returns the VNI of the VM of the port. |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 160 | * |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 161 | * @param portName VM port |
| 162 | * @return VNI |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 163 | */ |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 164 | private String getVniForPort(String portName) { |
| 165 | String uuid = portName.substring(3); |
| 166 | OpenstackPort port = openstackPortList.stream() |
| 167 | .filter(p -> p.id().startsWith(uuid)) |
| 168 | .findAny().orElse(null); |
| 169 | if (port == null) { |
| 170 | log.warn("No port information for port {}", portName); |
| 171 | return null; |
| 172 | } |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 173 | |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 174 | OpenstackNetwork network = openstackNetworkList.stream() |
| 175 | .filter(n -> n.id().equals(port.networkId())) |
| 176 | .findAny().orElse(null); |
| 177 | if (network == null) { |
Satish K | 2e2d635 | 2015-11-27 12:02:15 +0530 | [diff] [blame] | 178 | log.warn("No VNI information for network {}", port.networkId()); |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 179 | return null; |
| 180 | } |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 181 | |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 182 | return network.segmentId(); |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | /** |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 186 | * Returns the Fixed IP address of the VM. |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 187 | * |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 188 | * @param portName VM port info |
| 189 | * @return IP address of the VM |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 190 | */ |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 191 | private Ip4Address getFixedIpAddressForPort(String portName) { |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 192 | |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 193 | String uuid = portName.substring(3); |
| 194 | OpenstackPort port = openstackPortList.stream() |
| 195 | .filter(p -> p.id().startsWith(uuid)) |
| 196 | .findFirst().orElse(null); |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 197 | |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 198 | if (port == null) { |
| 199 | log.error("There is no port information for port name {}", portName); |
| 200 | return null; |
| 201 | } |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 202 | |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 203 | if (port.fixedIps().isEmpty()) { |
| 204 | log.error("There is no fixed IP info in the port information"); |
| 205 | return null; |
| 206 | } |
| 207 | |
| 208 | return (Ip4Address) port.fixedIps().values().toArray()[0]; |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Returns the MAC address of the VM of the port. |
| 213 | * |
| 214 | * @param portName VM port |
| 215 | * @return MAC address of the VM |
| 216 | */ |
| 217 | private MacAddress getVmMacAddressForPort(String portName) { |
| 218 | |
| 219 | String uuid = portName.substring(3); |
| 220 | OpenstackPort port = openstackPortList.stream() |
| 221 | .filter(p -> p.id().startsWith(uuid)) |
| 222 | .findFirst().orElse(null); |
| 223 | |
| 224 | if (port == null) { |
| 225 | log.error("There is port information for port name {}", portName); |
| 226 | return null; |
| 227 | } |
| 228 | |
| 229 | return port.macAddress(); |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Sets the flow rules for traffic between VMs in the same Cnode. |
| 234 | * |
| 235 | * @param ip4Address VM IP address |
| 236 | * @param id device ID to put rules |
| 237 | * @param port VM port |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 238 | */ |
| 239 | private void setFlowRuleForVMsInSameCnode(Ip4Address ip4Address, DeviceId id, |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 240 | Port port) { |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 241 | TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder(); |
| 242 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 243 | |
| 244 | sBuilder.matchEthType(Ethernet.TYPE_IPV4) |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 245 | .matchIPDst(ip4Address.toIpPrefix()); |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 246 | tBuilder.setOutput(port.number()); |
| 247 | |
| 248 | ForwardingObjective fo = DefaultForwardingObjective.builder() |
| 249 | .withSelector(sBuilder.build()) |
| 250 | .withTreatment(tBuilder.build()) |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 251 | .withPriority(SWITCHING_RULE_PRIORITY) |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 252 | .withFlag(ForwardingObjective.Flag.VERSATILE) |
| 253 | .fromApp(appId) |
| 254 | .add(); |
| 255 | |
| 256 | flowObjectiveService.forward(id, fo); |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * Sets the flow rules between traffic from VMs in different Cnode. |
| 261 | * |
| 262 | * @param vni VNI |
| 263 | * @param id device ID |
| 264 | * @param hostIp host IP of the VM |
| 265 | * @param vmIp fixed IP of the VM |
| 266 | * @param vmMac MAC address of the VM |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 267 | */ |
| 268 | private void setVxLanFlowRule(String vni, DeviceId id, Ip4Address hostIp, |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 269 | Ip4Address vmIp, MacAddress vmMac) { |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 270 | TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder(); |
| 271 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 272 | |
| 273 | sBuilder.matchEthType(Ethernet.TYPE_IPV4) |
| 274 | .matchIPDst(vmIp.toIpPrefix()); |
| 275 | tBuilder.setTunnelId(Long.parseLong(vni)) |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 276 | .extension(buildNiciraExtenstion(id, hostIp), id) |
| 277 | .setOutput(getTunnelPort(id)); |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 278 | |
| 279 | ForwardingObjective fo = DefaultForwardingObjective.builder() |
| 280 | .withSelector(sBuilder.build()) |
| 281 | .withTreatment(tBuilder.build()) |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 282 | .withPriority(SWITCHING_RULE_PRIORITY) |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 283 | .withFlag(ForwardingObjective.Flag.VERSATILE) |
| 284 | .fromApp(appId) |
| 285 | .add(); |
| 286 | |
| 287 | flowObjectiveService.forward(id, fo); |
| 288 | } |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 289 | |
| 290 | private ExtensionTreatment buildNiciraExtenstion(DeviceId id, Ip4Address hostIp) { |
| 291 | Driver driver = driverService.getDriver(id); |
| 292 | DriverHandler driverHandler = new DefaultDriverHandler(new DefaultDriverData(driver, id)); |
| 293 | ExtensionTreatmentResolver resolver = driverHandler.behaviour(ExtensionTreatmentResolver.class); |
| 294 | |
| 295 | ExtensionTreatment extensionInstruction = |
| 296 | resolver.getExtensionInstruction( |
| 297 | ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_TUNNEL_DST.type()); |
| 298 | |
| 299 | try { |
| 300 | extensionInstruction.setPropertyValue("tunnelDst", hostIp); |
| 301 | } catch (ExtensionPropertyException e) { |
| 302 | log.error("Error setting Nicira extension setting {}", e); |
| 303 | } |
| 304 | |
| 305 | return extensionInstruction; |
| 306 | } |
| 307 | |
| 308 | private PortNumber getTunnelPort(DeviceId id) { |
| 309 | Port port = deviceService.getPorts(id).stream() |
| 310 | .filter(p -> p.annotations().value("portName").equals("vxlan")) |
| 311 | .findAny().orElse(null); |
| 312 | |
| 313 | if (port == null) { |
| 314 | log.error("No TunnelPort was created."); |
| 315 | return null; |
| 316 | } |
| 317 | return port.number(); |
| 318 | } |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 319 | } |