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); |
daniel | 2a2bd7b | 2015-12-02 13:53:58 +0900 | [diff] [blame] | 56 | private static final int SWITCHING_RULE_PRIORITY = 30000; |
| 57 | private static final int EAST_WEST_ROUTING_RULE_PRIORITY = 29000; |
| 58 | private static final int TUNNELTAG_RULE_PRIORITY = 30000; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 59 | |
| 60 | private FlowObjectiveService flowObjectiveService; |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 61 | private DriverService driverService; |
| 62 | private DeviceService deviceService; |
| 63 | private OpenstackRestHandler restHandler; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 64 | private ApplicationId appId; |
| 65 | |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 66 | private Collection<OpenstackNetwork> openstackNetworkList; |
| 67 | private Collection<OpenstackPort> openstackPortList; |
| 68 | |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 69 | /** |
Ray Milkey | 87b16b0 | 2015-10-30 11:50:00 -0700 | [diff] [blame] | 70 | * Creates OpenstackSwitchingRulPopulator. |
| 71 | * |
| 72 | * @param appId application id |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 73 | * @param flowObjectiveService FlowObjectiveService reference |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 74 | * @param deviceService DeviceService reference |
| 75 | * @param driverService DriverService reference |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 76 | */ |
| 77 | public OpenstackSwitchingRulePopulator(ApplicationId appId, |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 78 | FlowObjectiveService flowObjectiveService, |
| 79 | DeviceService deviceService, |
| 80 | OpenstackRestHandler restHandler, |
| 81 | DriverService driverService) { |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 82 | this.flowObjectiveService = flowObjectiveService; |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 83 | this.deviceService = deviceService; |
| 84 | this.driverService = driverService; |
| 85 | this.restHandler = restHandler; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 86 | this.appId = appId; |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 87 | |
| 88 | openstackNetworkList = restHandler.getNetworks(); |
| 89 | openstackPortList = restHandler.getPorts(); |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 90 | } |
| 91 | |
daniel | 2a2bd7b | 2015-12-02 13:53:58 +0900 | [diff] [blame] | 92 | |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 93 | /** |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 94 | * Populates flow rules 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 | * @param device device to populate rules to |
| 97 | * @param port port for the VM created |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 98 | */ |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 99 | public void populateSwitchingRules(Device device, Port port) { |
Hyunsun Moon | 74ec406 | 2015-12-09 10:58:32 -0800 | [diff] [blame] | 100 | populateFlowRulesForTunnelTag(device, port); |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 101 | populateFlowRulesForTrafficToSameCnode(device, port); |
| 102 | populateFlowRulesForTrafficToDifferentCnode(device, port); |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /** |
daniel | 2a2bd7b | 2015-12-02 13:53:58 +0900 | [diff] [blame] | 106 | * Populate the flow rules for tagging tunnelId according to which inport is came from. |
| 107 | * |
| 108 | * @param device device to put the rules |
| 109 | * @param port port info of the VM |
| 110 | */ |
Hyunsun Moon | 74ec406 | 2015-12-09 10:58:32 -0800 | [diff] [blame] | 111 | private void populateFlowRulesForTunnelTag(Device device, Port port) { |
daniel | 2a2bd7b | 2015-12-02 13:53:58 +0900 | [diff] [blame] | 112 | Ip4Address vmIp = getFixedIpAddressForPort(port.annotations().value("portName")); |
| 113 | String portName = port.annotations().value("portName"); |
| 114 | String vni = getVniForPort(portName); |
| 115 | |
| 116 | if (vmIp != null) { |
Hyunsun Moon | 74ec406 | 2015-12-09 10:58:32 -0800 | [diff] [blame] | 117 | setFlowRuleForTunnelTag(device.id(), port, vni); |
daniel | 2a2bd7b | 2015-12-02 13:53:58 +0900 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | |
| 121 | /** |
sanghoshin | 075e3e7 | 2015-11-25 16:34:29 +0900 | [diff] [blame] | 122 | * Returns OpenstackPort object for the Port reference given. |
| 123 | * |
| 124 | * @param port Port object |
| 125 | * @return OpenstackPort reference, or null when not found |
| 126 | */ |
| 127 | public OpenstackPort openstackPort(Port port) { |
| 128 | String uuid = port.annotations().value("portName").substring(3); |
| 129 | return openstackPortList.stream().filter(p -> p.id().startsWith(uuid)) |
| 130 | .findAny().orElse(null); |
| 131 | } |
| 132 | |
| 133 | /** |
sanghoshin | 65723ae | 2015-11-17 22:07:21 +0900 | [diff] [blame] | 134 | * Remove flows rules for the VM removed. |
| 135 | * |
| 136 | * @param deviceId device to remove rules |
| 137 | * @param vmIp IP address of the VM removed |
| 138 | */ |
| 139 | public void removeSwitchingRules(DeviceId deviceId, Ip4Address vmIp) { |
| 140 | removeFlowRuleForVMsInSameCnode(deviceId, vmIp); |
| 141 | deviceService.getAvailableDevices().forEach(device -> { |
| 142 | if (!device.id().equals(deviceId)) { |
| 143 | removeVxLanFlowRule(device.id(), vmIp); |
| 144 | } |
| 145 | }); |
| 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); |
| 158 | MacAddress vmMacAddress = getVmMacAddressForPort(portName); |
| 159 | |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 160 | if (vmIp != null) { |
daniel | 2a2bd7b | 2015-12-02 13:53:58 +0900 | [diff] [blame] | 161 | setFlowRuleForVMsInSameCnode(vmIp, device.id(), port, vni, vmMacAddress); |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 162 | } |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | /** |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 166 | * Populates the flow rules for traffic to VMs in different Cnode using |
| 167 | * Nicira extention. |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 168 | * |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 169 | * @param device device to put rules |
| 170 | * @param port port information of the VM |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 171 | */ |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 172 | private void populateFlowRulesForTrafficToDifferentCnode(Device device, Port port) { |
| 173 | String portName = port.annotations().value("portName"); |
| 174 | String channelId = device.annotations().value("channelId"); |
| 175 | Ip4Address hostIpAddress = Ip4Address.valueOf(channelId.split(":")[0]); |
| 176 | Ip4Address fixedIp = getFixedIpAddressForPort(portName); |
| 177 | MacAddress vmMac = getVmMacAddressForPort(portName); |
| 178 | String vni = getVniForPort(portName); |
| 179 | deviceService.getAvailableDevices().forEach(d -> { |
| 180 | if (!d.equals(device)) { |
| 181 | deviceService.getPorts(d.id()).forEach(p -> { |
| 182 | String pName = p.annotations().value("portName"); |
| 183 | if (!p.equals(port) && vni.equals(getVniForPort(pName))) { |
| 184 | String cidx = d.annotations().value("channelId"); |
| 185 | Ip4Address hostIpx = Ip4Address.valueOf(cidx.split(":")[0]); |
| 186 | MacAddress vmMacx = getVmMacAddressForPort(pName); |
| 187 | Ip4Address fixedIpx = getFixedIpAddressForPort(pName); |
sanghoshin | 65723ae | 2015-11-17 22:07:21 +0900 | [diff] [blame] | 188 | if (port.isEnabled()) { |
| 189 | setVxLanFlowRule(vni, device.id(), hostIpx, fixedIpx, vmMacx); |
| 190 | setVxLanFlowRule(vni, d.id(), hostIpAddress, fixedIp, vmMac); |
| 191 | } |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 192 | } |
| 193 | }); |
| 194 | } |
| 195 | }); |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | /** |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 199 | * Returns the VNI of the VM of the port. |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 200 | * |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 201 | * @param portName VM port |
| 202 | * @return VNI |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 203 | */ |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 204 | private String getVniForPort(String portName) { |
| 205 | String uuid = portName.substring(3); |
| 206 | OpenstackPort port = openstackPortList.stream() |
| 207 | .filter(p -> p.id().startsWith(uuid)) |
| 208 | .findAny().orElse(null); |
| 209 | if (port == null) { |
daniel | 2a2bd7b | 2015-12-02 13:53:58 +0900 | [diff] [blame] | 210 | log.debug("No port information for port {}", portName); |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 211 | return null; |
| 212 | } |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 213 | |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 214 | OpenstackNetwork network = openstackNetworkList.stream() |
| 215 | .filter(n -> n.id().equals(port.networkId())) |
| 216 | .findAny().orElse(null); |
| 217 | if (network == null) { |
Satish K | 2e2d635 | 2015-11-27 12:02:15 +0530 | [diff] [blame] | 218 | log.warn("No VNI information for network {}", port.networkId()); |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 219 | return null; |
| 220 | } |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 221 | |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 222 | return network.segmentId(); |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | /** |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 226 | * Returns the Fixed IP address of the VM. |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 227 | * |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 228 | * @param portName VM port info |
| 229 | * @return IP address of the VM |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 230 | */ |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 231 | private Ip4Address getFixedIpAddressForPort(String portName) { |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 232 | |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 233 | String uuid = portName.substring(3); |
| 234 | OpenstackPort port = openstackPortList.stream() |
| 235 | .filter(p -> p.id().startsWith(uuid)) |
| 236 | .findFirst().orElse(null); |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 237 | |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 238 | if (port == null) { |
| 239 | log.error("There is no port information for port name {}", portName); |
| 240 | return null; |
| 241 | } |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 242 | |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 243 | if (port.fixedIps().isEmpty()) { |
| 244 | log.error("There is no fixed IP info in the port information"); |
| 245 | return null; |
| 246 | } |
| 247 | |
| 248 | return (Ip4Address) port.fixedIps().values().toArray()[0]; |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Returns the MAC address of the VM of the port. |
| 253 | * |
| 254 | * @param portName VM port |
| 255 | * @return MAC address of the VM |
| 256 | */ |
| 257 | private MacAddress getVmMacAddressForPort(String portName) { |
| 258 | |
| 259 | String uuid = portName.substring(3); |
| 260 | OpenstackPort port = openstackPortList.stream() |
| 261 | .filter(p -> p.id().startsWith(uuid)) |
| 262 | .findFirst().orElse(null); |
| 263 | |
| 264 | if (port == null) { |
sanghoshin | 65723ae | 2015-11-17 22:07:21 +0900 | [diff] [blame] | 265 | log.error("There is no port information for port name {}", portName); |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 266 | return null; |
| 267 | } |
| 268 | |
| 269 | return port.macAddress(); |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 270 | } |
| 271 | |
Hyunsun Moon | 74ec406 | 2015-12-09 10:58:32 -0800 | [diff] [blame] | 272 | private void setFlowRuleForTunnelTag(DeviceId deviceId, Port port, String vni) { |
daniel | 2a2bd7b | 2015-12-02 13:53:58 +0900 | [diff] [blame] | 273 | |
| 274 | TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder(); |
| 275 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 276 | |
| 277 | sBuilder.matchEthType(Ethernet.TYPE_IPV4) |
| 278 | .matchInPort(port.number()); |
| 279 | |
| 280 | tBuilder.setTunnelId(Long.parseLong(vni)); |
| 281 | |
| 282 | ForwardingObjective fo = DefaultForwardingObjective.builder() |
| 283 | .withSelector(sBuilder.build()) |
| 284 | .withTreatment(tBuilder.build()) |
| 285 | .withPriority(TUNNELTAG_RULE_PRIORITY) |
| 286 | .withFlag(ForwardingObjective.Flag.SPECIFIC) |
| 287 | .fromApp(appId) |
| 288 | .add(); |
| 289 | |
| 290 | flowObjectiveService.forward(deviceId, fo); |
| 291 | } |
| 292 | |
| 293 | |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 294 | /** |
| 295 | * Sets the flow rules for traffic between VMs in the same Cnode. |
| 296 | * |
| 297 | * @param ip4Address VM IP address |
| 298 | * @param id device ID to put rules |
| 299 | * @param port VM port |
daniel | 2a2bd7b | 2015-12-02 13:53:58 +0900 | [diff] [blame] | 300 | * @param vni VM VNI |
| 301 | * @param vmMacAddress VM MAC address |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 302 | */ |
| 303 | private void setFlowRuleForVMsInSameCnode(Ip4Address ip4Address, DeviceId id, |
daniel | 2a2bd7b | 2015-12-02 13:53:58 +0900 | [diff] [blame] | 304 | Port port, String vni, MacAddress vmMacAddress) { |
| 305 | |
| 306 | //For L2 Switching Case |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 307 | TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder(); |
| 308 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 309 | |
| 310 | sBuilder.matchEthType(Ethernet.TYPE_IPV4) |
daniel | 2a2bd7b | 2015-12-02 13:53:58 +0900 | [diff] [blame] | 311 | .matchIPDst(ip4Address.toIpPrefix()) |
| 312 | .matchTunnelId(Long.parseLong(vni)); |
| 313 | |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 314 | tBuilder.setOutput(port.number()); |
| 315 | |
| 316 | ForwardingObjective fo = DefaultForwardingObjective.builder() |
| 317 | .withSelector(sBuilder.build()) |
| 318 | .withTreatment(tBuilder.build()) |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 319 | .withPriority(SWITCHING_RULE_PRIORITY) |
daniel | 2a2bd7b | 2015-12-02 13:53:58 +0900 | [diff] [blame] | 320 | .withFlag(ForwardingObjective.Flag.SPECIFIC) |
| 321 | .fromApp(appId) |
| 322 | .add(); |
| 323 | |
| 324 | flowObjectiveService.forward(id, fo); |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 325 | } |
| 326 | |
daniel | 2a2bd7b | 2015-12-02 13:53:58 +0900 | [diff] [blame] | 327 | |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 328 | /** |
| 329 | * Sets the flow rules between traffic from VMs in different Cnode. |
| 330 | * |
| 331 | * @param vni VNI |
| 332 | * @param id device ID |
| 333 | * @param hostIp host IP of the VM |
| 334 | * @param vmIp fixed IP of the VM |
| 335 | * @param vmMac MAC address of the VM |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 336 | */ |
| 337 | private void setVxLanFlowRule(String vni, DeviceId id, Ip4Address hostIp, |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 338 | Ip4Address vmIp, MacAddress vmMac) { |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 339 | TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder(); |
| 340 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 341 | |
| 342 | sBuilder.matchEthType(Ethernet.TYPE_IPV4) |
daniel | 2a2bd7b | 2015-12-02 13:53:58 +0900 | [diff] [blame] | 343 | .matchTunnelId(Long.parseLong(vni)) |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 344 | .matchIPDst(vmIp.toIpPrefix()); |
daniel | 2a2bd7b | 2015-12-02 13:53:58 +0900 | [diff] [blame] | 345 | |
| 346 | tBuilder.extension(buildNiciraExtenstion(id, hostIp), id) |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 347 | .setOutput(getTunnelPort(id)); |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 348 | |
| 349 | ForwardingObjective fo = DefaultForwardingObjective.builder() |
| 350 | .withSelector(sBuilder.build()) |
| 351 | .withTreatment(tBuilder.build()) |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 352 | .withPriority(SWITCHING_RULE_PRIORITY) |
daniel | 2a2bd7b | 2015-12-02 13:53:58 +0900 | [diff] [blame] | 353 | .withFlag(ForwardingObjective.Flag.SPECIFIC) |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 354 | .fromApp(appId) |
| 355 | .add(); |
| 356 | |
| 357 | flowObjectiveService.forward(id, fo); |
| 358 | } |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 359 | |
sanghoshin | 65723ae | 2015-11-17 22:07:21 +0900 | [diff] [blame] | 360 | private void removeFlowRuleForVMsInSameCnode(DeviceId id, Ip4Address vmIp) { |
| 361 | TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder(); |
| 362 | |
| 363 | sBuilder.matchEthType(Ethernet.TYPE_IPV4) |
| 364 | .matchIPDst(vmIp.toIpPrefix()); |
| 365 | |
| 366 | ForwardingObjective fo = DefaultForwardingObjective.builder() |
| 367 | .withSelector(sBuilder.build()) |
| 368 | .withTreatment(DefaultTrafficTreatment.builder().build()) |
| 369 | .withFlag(ForwardingObjective.Flag.VERSATILE) |
| 370 | .withPriority(SWITCHING_RULE_PRIORITY) |
| 371 | .fromApp(appId) |
| 372 | .remove(); |
| 373 | |
| 374 | flowObjectiveService.forward(id, fo); |
| 375 | } |
| 376 | |
| 377 | private void removeVxLanFlowRule(DeviceId id, Ip4Address vmIp) { |
| 378 | TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder(); |
| 379 | // XXX: Later, more matches will be added when multiple table is implemented. |
| 380 | sBuilder.matchEthType(Ethernet.TYPE_IPV4) |
| 381 | .matchIPDst(vmIp.toIpPrefix()); |
| 382 | |
| 383 | ForwardingObjective fo = DefaultForwardingObjective.builder() |
| 384 | .withSelector(sBuilder.build()) |
| 385 | .withTreatment(DefaultTrafficTreatment.builder().build()) |
| 386 | .withFlag(ForwardingObjective.Flag.VERSATILE) |
| 387 | .withPriority(SWITCHING_RULE_PRIORITY) |
| 388 | .fromApp(appId) |
| 389 | .remove(); |
| 390 | |
| 391 | flowObjectiveService.forward(id, fo); |
| 392 | } |
| 393 | |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 394 | private ExtensionTreatment buildNiciraExtenstion(DeviceId id, Ip4Address hostIp) { |
| 395 | Driver driver = driverService.getDriver(id); |
| 396 | DriverHandler driverHandler = new DefaultDriverHandler(new DefaultDriverData(driver, id)); |
| 397 | ExtensionTreatmentResolver resolver = driverHandler.behaviour(ExtensionTreatmentResolver.class); |
| 398 | |
| 399 | ExtensionTreatment extensionInstruction = |
| 400 | resolver.getExtensionInstruction( |
| 401 | ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_TUNNEL_DST.type()); |
| 402 | |
| 403 | try { |
| 404 | extensionInstruction.setPropertyValue("tunnelDst", hostIp); |
| 405 | } catch (ExtensionPropertyException e) { |
| 406 | log.error("Error setting Nicira extension setting {}", e); |
| 407 | } |
| 408 | |
| 409 | return extensionInstruction; |
| 410 | } |
| 411 | |
| 412 | private PortNumber getTunnelPort(DeviceId id) { |
| 413 | Port port = deviceService.getPorts(id).stream() |
| 414 | .filter(p -> p.annotations().value("portName").equals("vxlan")) |
| 415 | .findAny().orElse(null); |
| 416 | |
| 417 | if (port == null) { |
| 418 | log.error("No TunnelPort was created."); |
| 419 | return null; |
| 420 | } |
| 421 | return port.number(); |
| 422 | } |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 423 | } |