Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present 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 | */ |
alshabib | b4d3171 | 2016-06-01 18:51:03 -0700 | [diff] [blame] | 16 | package org.opencord.cordvtn.impl; |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 17 | |
| 18 | import org.apache.felix.scr.annotations.Activate; |
| 19 | import org.apache.felix.scr.annotations.Component; |
| 20 | import org.apache.felix.scr.annotations.Deactivate; |
| 21 | import org.apache.felix.scr.annotations.Reference; |
| 22 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 23 | import org.apache.felix.scr.annotations.Service; |
| 24 | import org.onlab.packet.Ethernet; |
| 25 | import org.onlab.packet.IPv4; |
| 26 | import org.onlab.packet.Ip4Address; |
| 27 | import org.onlab.packet.IpAddress; |
| 28 | import org.onlab.packet.TpPort; |
| 29 | import org.onlab.packet.VlanId; |
Hyunsun Moon | c031d9b | 2016-08-04 13:57:22 -0700 | [diff] [blame] | 30 | import org.onosproject.net.AnnotationKeys; |
| 31 | import org.onosproject.net.Port; |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 32 | import org.opencord.cordvtn.api.Constants; |
alshabib | b4d3171 | 2016-06-01 18:51:03 -0700 | [diff] [blame] | 33 | import org.opencord.cordvtn.api.CordVtnNode; |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 34 | import org.onosproject.core.ApplicationId; |
| 35 | import org.onosproject.core.CoreService; |
| 36 | import org.onosproject.net.Device; |
| 37 | import org.onosproject.net.DeviceId; |
| 38 | import org.onosproject.net.PortNumber; |
| 39 | import org.onosproject.net.behaviour.ExtensionTreatmentResolver; |
| 40 | import org.onosproject.net.device.DeviceService; |
| 41 | import org.onosproject.net.flow.DefaultFlowRule; |
| 42 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 43 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 44 | import org.onosproject.net.flow.FlowRule; |
| 45 | import org.onosproject.net.flow.FlowRuleOperations; |
| 46 | import org.onosproject.net.flow.FlowRuleOperationsContext; |
| 47 | import org.onosproject.net.flow.FlowRuleService; |
| 48 | import org.onosproject.net.flow.TrafficSelector; |
| 49 | import org.onosproject.net.flow.TrafficTreatment; |
| 50 | import org.onosproject.net.flow.instructions.ExtensionPropertyException; |
| 51 | import org.onosproject.net.flow.instructions.ExtensionTreatment; |
| 52 | import org.slf4j.Logger; |
| 53 | |
Hyunsun Moon | c031d9b | 2016-08-04 13:57:22 -0700 | [diff] [blame] | 54 | import java.util.Optional; |
| 55 | |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 56 | import static com.google.common.base.Preconditions.checkNotNull; |
| 57 | import static org.onosproject.net.flow.instructions.ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_TUNNEL_DST; |
Hyunsun Moon | c031d9b | 2016-08-04 13:57:22 -0700 | [diff] [blame] | 58 | import static org.opencord.cordvtn.api.Constants.DEFAULT_TUNNEL; |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 59 | import static org.slf4j.LoggerFactory.getLogger; |
| 60 | |
| 61 | /** |
| 62 | * Provides CORD VTN pipeline. |
| 63 | */ |
| 64 | @Component(immediate = true) |
| 65 | @Service(value = CordVtnPipeline.class) |
| 66 | public final class CordVtnPipeline { |
| 67 | |
| 68 | protected final Logger log = getLogger(getClass()); |
| 69 | |
| 70 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 71 | protected CoreService coreService; |
| 72 | |
| 73 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 74 | protected FlowRuleService flowRuleService; |
| 75 | |
| 76 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 77 | protected DeviceService deviceService; |
| 78 | |
| 79 | // tables |
| 80 | public static final int TABLE_ZERO = 0; |
| 81 | public static final int TABLE_IN_PORT = 1; |
Hyunsun Moon | 1e88fef | 2016-08-04 14:00:35 -0700 | [diff] [blame] | 82 | public static final int TABLE_ACCESS = 2; |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 83 | public static final int TABLE_IN_SERVICE = 3; |
Hyunsun Moon | 1e88fef | 2016-08-04 14:00:35 -0700 | [diff] [blame] | 84 | public static final int TABLE_DST = 4; |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 85 | public static final int TABLE_TUNNEL_IN = 5; |
| 86 | public static final int TABLE_VLAN = 6; |
| 87 | |
| 88 | // priorities |
| 89 | public static final int PRIORITY_MANAGEMENT = 55000; |
| 90 | public static final int PRIORITY_HIGH = 50000; |
| 91 | public static final int PRIORITY_DEFAULT = 5000; |
| 92 | public static final int PRIORITY_LOW = 4000; |
| 93 | public static final int PRIORITY_ZERO = 0; |
| 94 | |
| 95 | public static final int VXLAN_UDP_PORT = 4789; |
| 96 | public static final VlanId VLAN_WAN = VlanId.vlanId((short) 500); |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 97 | |
Hyunsun Moon | c031d9b | 2016-08-04 13:57:22 -0700 | [diff] [blame] | 98 | public static final String PROPERTY_TUNNEL_DST = "tunnelDst"; |
| 99 | |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 100 | private ApplicationId appId; |
| 101 | |
| 102 | @Activate |
| 103 | protected void activate() { |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 104 | appId = coreService.registerApplication(Constants.CORDVTN_APP_ID); |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 105 | log.info("Started"); |
| 106 | } |
| 107 | |
| 108 | @Deactivate |
| 109 | protected void deactivate() { |
| 110 | log.info("Stopped"); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Flush flows installed by this application. |
| 115 | */ |
| 116 | public void flushRules() { |
| 117 | flowRuleService.getFlowRulesById(appId).forEach(flowRule -> processFlowRule(false, flowRule)); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Installs table miss rule to a give device. |
| 122 | * |
| 123 | * @param node cordvtn node |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 124 | */ |
Hyunsun Moon | c031d9b | 2016-08-04 13:57:22 -0700 | [diff] [blame] | 125 | public void initPipeline(CordVtnNode node) { |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 126 | checkNotNull(node); |
| 127 | |
Hyunsun Moon | c031d9b | 2016-08-04 13:57:22 -0700 | [diff] [blame] | 128 | Optional<PortNumber> dataPort = getPortNumber(node.integrationBridgeId(), node.dataIface()); |
| 129 | Optional<PortNumber> tunnelPort = getPortNumber(node.integrationBridgeId(), DEFAULT_TUNNEL); |
| 130 | if (!dataPort.isPresent() || !tunnelPort.isPresent()) { |
| 131 | log.warn("Node is not in COMPLETE state"); |
| 132 | return; |
| 133 | } |
| 134 | |
| 135 | Optional<PortNumber> hostMgmtPort = Optional.empty(); |
| 136 | if (node.hostMgmtIface().isPresent()) { |
| 137 | hostMgmtPort = getPortNumber(node.integrationBridgeId(), node.hostMgmtIface().get()); |
| 138 | } |
| 139 | |
| 140 | processTableZero(node.integrationBridgeId(), |
| 141 | dataPort.get(), |
| 142 | node.dataIp().ip(), |
| 143 | node.localMgmtIp().ip()); |
| 144 | |
| 145 | processInPortTable(node.integrationBridgeId(), |
| 146 | tunnelPort.get(), |
| 147 | dataPort.get(), |
| 148 | hostMgmtPort); |
| 149 | |
| 150 | processAccessTypeTable(node.integrationBridgeId(), dataPort.get()); |
| 151 | processVlanTable(node.integrationBridgeId(), dataPort.get()); |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Hyunsun Moon | c031d9b | 2016-08-04 13:57:22 -0700 | [diff] [blame] | 154 | private void processTableZero(DeviceId deviceId, PortNumber dataPort, IpAddress dataIp, |
| 155 | IpAddress localMgmtIp) { |
| 156 | vxlanShuttleRule(deviceId, dataPort, dataIp); |
| 157 | localManagementBaseRule(deviceId, localMgmtIp.getIp4Address()); |
| 158 | |
| 159 | // take all vlan tagged packet to the VLAN table |
| 160 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 161 | .matchVlanId(VlanId.ANY) |
| 162 | .build(); |
| 163 | |
| 164 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 165 | .transition(TABLE_VLAN) |
| 166 | .build(); |
| 167 | |
| 168 | FlowRule flowRule = DefaultFlowRule.builder() |
| 169 | .fromApp(appId) |
| 170 | .withSelector(selector) |
| 171 | .withTreatment(treatment) |
| 172 | .withPriority(PRIORITY_MANAGEMENT) |
| 173 | .forDevice(deviceId) |
| 174 | .forTable(TABLE_ZERO) |
| 175 | .makePermanent() |
| 176 | .build(); |
| 177 | |
| 178 | processFlowRule(true, flowRule); |
| 179 | |
| 180 | // take all other packets to the next table |
| 181 | selector = DefaultTrafficSelector.builder() |
| 182 | .build(); |
| 183 | |
| 184 | treatment = DefaultTrafficTreatment.builder() |
| 185 | .transition(TABLE_IN_PORT) |
| 186 | .build(); |
| 187 | |
| 188 | flowRule = DefaultFlowRule.builder() |
| 189 | .fromApp(appId) |
| 190 | .withSelector(selector) |
| 191 | .withTreatment(treatment) |
| 192 | .withPriority(PRIORITY_ZERO) |
| 193 | .forDevice(deviceId) |
| 194 | .forTable(TABLE_ZERO) |
| 195 | .makePermanent() |
| 196 | .build(); |
| 197 | |
| 198 | processFlowRule(true, flowRule); |
| 199 | } |
| 200 | |
| 201 | private void vxlanShuttleRule(DeviceId deviceId, PortNumber dataPort, IpAddress dataIp) { |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 202 | // take vxlan packet out onto the physical port |
| 203 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 204 | .matchInPort(PortNumber.LOCAL) |
| 205 | .build(); |
| 206 | |
| 207 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 208 | .setOutput(dataPort) |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 209 | .build(); |
| 210 | |
| 211 | FlowRule flowRule = DefaultFlowRule.builder() |
| 212 | .fromApp(appId) |
| 213 | .withSelector(selector) |
| 214 | .withTreatment(treatment) |
| 215 | .withPriority(PRIORITY_HIGH) |
| 216 | .forDevice(deviceId) |
| 217 | .forTable(TABLE_ZERO) |
| 218 | .makePermanent() |
| 219 | .build(); |
| 220 | |
| 221 | processFlowRule(true, flowRule); |
| 222 | |
| 223 | // take a vxlan encap'd packet through the Linux stack |
| 224 | selector = DefaultTrafficSelector.builder() |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 225 | .matchInPort(dataPort) |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 226 | .matchEthType(Ethernet.TYPE_IPV4) |
| 227 | .matchIPProtocol(IPv4.PROTOCOL_UDP) |
| 228 | .matchUdpDst(TpPort.tpPort(VXLAN_UDP_PORT)) |
| 229 | .build(); |
| 230 | |
| 231 | treatment = DefaultTrafficTreatment.builder() |
| 232 | .setOutput(PortNumber.LOCAL) |
| 233 | .build(); |
| 234 | |
| 235 | flowRule = DefaultFlowRule.builder() |
| 236 | .fromApp(appId) |
| 237 | .withSelector(selector) |
| 238 | .withTreatment(treatment) |
| 239 | .withPriority(PRIORITY_HIGH) |
| 240 | .forDevice(deviceId) |
| 241 | .forTable(TABLE_ZERO) |
| 242 | .makePermanent() |
| 243 | .build(); |
| 244 | |
| 245 | processFlowRule(true, flowRule); |
| 246 | |
| 247 | // take a packet to the data plane ip through Linux stack |
| 248 | selector = DefaultTrafficSelector.builder() |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 249 | .matchInPort(dataPort) |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 250 | .matchEthType(Ethernet.TYPE_IPV4) |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 251 | .matchIPDst(dataIp.toIpPrefix()) |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 252 | .build(); |
| 253 | |
| 254 | treatment = DefaultTrafficTreatment.builder() |
| 255 | .setOutput(PortNumber.LOCAL) |
| 256 | .build(); |
| 257 | |
| 258 | flowRule = DefaultFlowRule.builder() |
| 259 | .fromApp(appId) |
| 260 | .withSelector(selector) |
| 261 | .withTreatment(treatment) |
| 262 | .withPriority(PRIORITY_HIGH) |
| 263 | .forDevice(deviceId) |
| 264 | .forTable(TABLE_ZERO) |
| 265 | .makePermanent() |
| 266 | .build(); |
| 267 | |
| 268 | processFlowRule(true, flowRule); |
| 269 | |
| 270 | // take an arp packet from physical through Linux stack |
| 271 | selector = DefaultTrafficSelector.builder() |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 272 | .matchInPort(dataPort) |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 273 | .matchEthType(Ethernet.TYPE_ARP) |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 274 | .matchArpTpa(dataIp.getIp4Address()) |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 275 | .build(); |
| 276 | |
| 277 | treatment = DefaultTrafficTreatment.builder() |
| 278 | .setOutput(PortNumber.LOCAL) |
| 279 | .build(); |
| 280 | |
| 281 | flowRule = DefaultFlowRule.builder() |
| 282 | .fromApp(appId) |
| 283 | .withSelector(selector) |
| 284 | .withTreatment(treatment) |
| 285 | .withPriority(PRIORITY_HIGH) |
| 286 | .forDevice(deviceId) |
| 287 | .forTable(TABLE_ZERO) |
| 288 | .makePermanent() |
| 289 | .build(); |
| 290 | |
| 291 | processFlowRule(true, flowRule); |
Hyunsun Moon | c031d9b | 2016-08-04 13:57:22 -0700 | [diff] [blame] | 292 | } |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 293 | |
Hyunsun Moon | c031d9b | 2016-08-04 13:57:22 -0700 | [diff] [blame] | 294 | private void localManagementBaseRule(DeviceId deviceId, Ip4Address localMgmtIp) { |
| 295 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 296 | .matchEthType(Ethernet.TYPE_ARP) |
| 297 | .matchArpTpa(localMgmtIp) |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 298 | .build(); |
| 299 | |
Hyunsun Moon | c031d9b | 2016-08-04 13:57:22 -0700 | [diff] [blame] | 300 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 301 | .setOutput(PortNumber.LOCAL) |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 302 | .build(); |
| 303 | |
Hyunsun Moon | c031d9b | 2016-08-04 13:57:22 -0700 | [diff] [blame] | 304 | FlowRule flowRule = DefaultFlowRule.builder() |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 305 | .fromApp(appId) |
| 306 | .withSelector(selector) |
| 307 | .withTreatment(treatment) |
Hyunsun Moon | c031d9b | 2016-08-04 13:57:22 -0700 | [diff] [blame] | 308 | .withPriority(CordVtnPipeline.PRIORITY_MANAGEMENT) |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 309 | .forDevice(deviceId) |
Hyunsun Moon | c031d9b | 2016-08-04 13:57:22 -0700 | [diff] [blame] | 310 | .forTable(CordVtnPipeline.TABLE_ZERO) |
Hyunsun Moon | acbc8ef | 2016-06-21 17:58:45 -0700 | [diff] [blame] | 311 | .makePermanent() |
| 312 | .build(); |
| 313 | |
| 314 | processFlowRule(true, flowRule); |
| 315 | |
| 316 | selector = DefaultTrafficSelector.builder() |
Hyunsun Moon | c031d9b | 2016-08-04 13:57:22 -0700 | [diff] [blame] | 317 | .matchInPort(PortNumber.LOCAL) |
| 318 | .matchEthType(Ethernet.TYPE_IPV4) |
| 319 | .matchIPSrc(localMgmtIp.toIpPrefix()) |
Hyunsun Moon | acbc8ef | 2016-06-21 17:58:45 -0700 | [diff] [blame] | 320 | .build(); |
| 321 | |
| 322 | treatment = DefaultTrafficTreatment.builder() |
Hyunsun Moon | 1e88fef | 2016-08-04 14:00:35 -0700 | [diff] [blame] | 323 | .transition(CordVtnPipeline.TABLE_DST) |
Hyunsun Moon | acbc8ef | 2016-06-21 17:58:45 -0700 | [diff] [blame] | 324 | .build(); |
| 325 | |
| 326 | flowRule = DefaultFlowRule.builder() |
| 327 | .fromApp(appId) |
| 328 | .withSelector(selector) |
| 329 | .withTreatment(treatment) |
Hyunsun Moon | c031d9b | 2016-08-04 13:57:22 -0700 | [diff] [blame] | 330 | .withPriority(CordVtnPipeline.PRIORITY_MANAGEMENT) |
Hyunsun Moon | acbc8ef | 2016-06-21 17:58:45 -0700 | [diff] [blame] | 331 | .forDevice(deviceId) |
Hyunsun Moon | c031d9b | 2016-08-04 13:57:22 -0700 | [diff] [blame] | 332 | .forTable(CordVtnPipeline.TABLE_ZERO) |
| 333 | .makePermanent() |
| 334 | .build(); |
| 335 | |
| 336 | processFlowRule(true, flowRule); |
| 337 | |
| 338 | selector = DefaultTrafficSelector.builder() |
| 339 | .matchEthType(Ethernet.TYPE_IPV4) |
| 340 | .matchIPDst(localMgmtIp.toIpPrefix()) |
| 341 | .build(); |
| 342 | |
| 343 | treatment = DefaultTrafficTreatment.builder() |
| 344 | .setOutput(PortNumber.LOCAL) |
| 345 | .build(); |
| 346 | |
| 347 | flowRule = DefaultFlowRule.builder() |
| 348 | .fromApp(appId) |
| 349 | .withSelector(selector) |
| 350 | .withTreatment(treatment) |
| 351 | .withPriority(CordVtnPipeline.PRIORITY_MANAGEMENT) |
| 352 | .forDevice(deviceId) |
| 353 | .forTable(CordVtnPipeline.TABLE_ZERO) |
| 354 | .makePermanent() |
| 355 | .build(); |
| 356 | |
| 357 | processFlowRule(true, flowRule); |
| 358 | |
| 359 | selector = DefaultTrafficSelector.builder() |
| 360 | .matchInPort(PortNumber.LOCAL) |
| 361 | .matchEthType(Ethernet.TYPE_ARP) |
| 362 | .matchArpSpa(localMgmtIp) |
| 363 | .build(); |
| 364 | |
| 365 | treatment = DefaultTrafficTreatment.builder() |
| 366 | .setOutput(PortNumber.CONTROLLER) |
| 367 | .build(); |
| 368 | |
| 369 | flowRule = DefaultFlowRule.builder() |
| 370 | .fromApp(appId) |
| 371 | .withSelector(selector) |
| 372 | .withTreatment(treatment) |
| 373 | .withPriority(CordVtnPipeline.PRIORITY_MANAGEMENT) |
| 374 | .forDevice(deviceId) |
| 375 | .forTable(CordVtnPipeline.TABLE_ZERO) |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 376 | .makePermanent() |
| 377 | .build(); |
| 378 | |
| 379 | processFlowRule(true, flowRule); |
| 380 | } |
| 381 | |
Hyunsun Moon | c031d9b | 2016-08-04 13:57:22 -0700 | [diff] [blame] | 382 | private void processInPortTable(DeviceId deviceId, PortNumber tunnelPort, PortNumber dataPort, |
| 383 | Optional<PortNumber> hostMgmtPort) { |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 384 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 385 | .matchInPort(tunnelPort) |
| 386 | .build(); |
| 387 | |
| 388 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 389 | .transition(TABLE_TUNNEL_IN) |
| 390 | .build(); |
| 391 | |
| 392 | FlowRule flowRule = DefaultFlowRule.builder() |
| 393 | .fromApp(appId) |
| 394 | .withSelector(selector) |
| 395 | .withTreatment(treatment) |
| 396 | .withPriority(PRIORITY_DEFAULT) |
| 397 | .forDevice(deviceId) |
| 398 | .forTable(TABLE_IN_PORT) |
| 399 | .makePermanent() |
| 400 | .build(); |
| 401 | |
| 402 | processFlowRule(true, flowRule); |
| 403 | |
| 404 | selector = DefaultTrafficSelector.builder() |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 405 | .matchInPort(dataPort) |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 406 | .build(); |
| 407 | |
| 408 | treatment = DefaultTrafficTreatment.builder() |
Hyunsun Moon | 1e88fef | 2016-08-04 14:00:35 -0700 | [diff] [blame] | 409 | .transition(TABLE_DST) |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 410 | .build(); |
| 411 | |
| 412 | flowRule = DefaultFlowRule.builder() |
| 413 | .fromApp(appId) |
| 414 | .withSelector(selector) |
| 415 | .withTreatment(treatment) |
| 416 | .withPriority(PRIORITY_DEFAULT) |
| 417 | .forDevice(deviceId) |
| 418 | .forTable(TABLE_IN_PORT) |
| 419 | .makePermanent() |
| 420 | .build(); |
| 421 | |
| 422 | processFlowRule(true, flowRule); |
Hyunsun Moon | c031d9b | 2016-08-04 13:57:22 -0700 | [diff] [blame] | 423 | |
| 424 | if (hostMgmtPort.isPresent()) { |
| 425 | selector = DefaultTrafficSelector.builder() |
| 426 | .matchInPort(hostMgmtPort.get()) |
| 427 | .build(); |
| 428 | |
| 429 | treatment = DefaultTrafficTreatment.builder() |
Hyunsun Moon | 1e88fef | 2016-08-04 14:00:35 -0700 | [diff] [blame] | 430 | .transition(TABLE_DST) |
Hyunsun Moon | c031d9b | 2016-08-04 13:57:22 -0700 | [diff] [blame] | 431 | .build(); |
| 432 | |
| 433 | flowRule = DefaultFlowRule.builder() |
| 434 | .fromApp(appId) |
| 435 | .withSelector(selector) |
| 436 | .withTreatment(treatment) |
| 437 | .withPriority(PRIORITY_DEFAULT) |
| 438 | .forDevice(deviceId) |
| 439 | .forTable(TABLE_IN_PORT) |
| 440 | .makePermanent() |
| 441 | .build(); |
| 442 | |
| 443 | processFlowRule(true, flowRule); |
| 444 | } |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 445 | } |
| 446 | |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 447 | private void processAccessTypeTable(DeviceId deviceId, PortNumber dataPort) { |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 448 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 449 | .build(); |
| 450 | |
| 451 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 452 | .setOutput(dataPort) |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 453 | .build(); |
| 454 | |
| 455 | FlowRule flowRule = DefaultFlowRule.builder() |
| 456 | .fromApp(appId) |
| 457 | .withSelector(selector) |
| 458 | .withTreatment(treatment) |
| 459 | .withPriority(PRIORITY_ZERO) |
| 460 | .forDevice(deviceId) |
Hyunsun Moon | 1e88fef | 2016-08-04 14:00:35 -0700 | [diff] [blame] | 461 | .forTable(TABLE_ACCESS) |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 462 | .makePermanent() |
| 463 | .build(); |
| 464 | |
| 465 | processFlowRule(true, flowRule); |
| 466 | } |
| 467 | |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 468 | private void processVlanTable(DeviceId deviceId, PortNumber dataPort) { |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 469 | // for traffic going out to WAN, strip vid 500 and take through data plane interface |
| 470 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 471 | .matchVlanId(VLAN_WAN) |
| 472 | .build(); |
| 473 | |
| 474 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 475 | .popVlan() |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 476 | .setOutput(dataPort) |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 477 | .build(); |
| 478 | |
| 479 | FlowRule flowRule = DefaultFlowRule.builder() |
| 480 | .fromApp(appId) |
| 481 | .withSelector(selector) |
| 482 | .withTreatment(treatment) |
| 483 | .withPriority(PRIORITY_DEFAULT) |
| 484 | .forDevice(deviceId) |
| 485 | .forTable(TABLE_VLAN) |
| 486 | .makePermanent() |
| 487 | .build(); |
| 488 | |
| 489 | processFlowRule(true, flowRule); |
| 490 | |
| 491 | selector = DefaultTrafficSelector.builder() |
| 492 | .matchVlanId(VLAN_WAN) |
| 493 | .matchEthType(Ethernet.TYPE_ARP) |
| 494 | .build(); |
| 495 | |
| 496 | treatment = DefaultTrafficTreatment.builder() |
| 497 | .setOutput(PortNumber.CONTROLLER) |
| 498 | .build(); |
| 499 | |
| 500 | flowRule = DefaultFlowRule.builder() |
| 501 | .fromApp(appId) |
| 502 | .withSelector(selector) |
| 503 | .withTreatment(treatment) |
| 504 | .withPriority(PRIORITY_HIGH) |
| 505 | .forDevice(deviceId) |
| 506 | .forTable(TABLE_VLAN) |
| 507 | .makePermanent() |
| 508 | .build(); |
| 509 | |
| 510 | processFlowRule(true, flowRule); |
| 511 | } |
| 512 | |
| 513 | public void processFlowRule(boolean install, FlowRule rule) { |
| 514 | FlowRuleOperations.Builder oBuilder = FlowRuleOperations.builder(); |
| 515 | oBuilder = install ? oBuilder.add(rule) : oBuilder.remove(rule); |
| 516 | |
| 517 | flowRuleService.apply(oBuilder.build(new FlowRuleOperationsContext() { |
| 518 | @Override |
| 519 | public void onError(FlowRuleOperations ops) { |
| 520 | log.error(String.format("Failed %s, %s", ops.toString(), rule.toString())); |
| 521 | } |
| 522 | })); |
| 523 | } |
| 524 | |
| 525 | public ExtensionTreatment tunnelDstTreatment(DeviceId deviceId, Ip4Address remoteIp) { |
Hyunsun Moon | c031d9b | 2016-08-04 13:57:22 -0700 | [diff] [blame] | 526 | Device device = deviceService.getDevice(deviceId); |
| 527 | if (device != null && !device.is(ExtensionTreatmentResolver.class)) { |
| 528 | log.error("The extension treatment is not supported"); |
Hyunsun Moon | acbc8ef | 2016-06-21 17:58:45 -0700 | [diff] [blame] | 529 | return null; |
| 530 | } |
Hyunsun Moon | c031d9b | 2016-08-04 13:57:22 -0700 | [diff] [blame] | 531 | |
| 532 | ExtensionTreatmentResolver resolver = device.as(ExtensionTreatmentResolver.class); |
| 533 | ExtensionTreatment treatment = resolver.getExtensionInstruction(NICIRA_SET_TUNNEL_DST.type()); |
| 534 | try { |
| 535 | treatment.setPropertyValue(PROPERTY_TUNNEL_DST, remoteIp); |
| 536 | return treatment; |
| 537 | } catch (ExtensionPropertyException e) { |
| 538 | log.warn("Failed to get tunnelDst extension treatment for {}", deviceId); |
| 539 | return null; |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | private Optional<PortNumber> getPortNumber(DeviceId deviceId, String portName) { |
| 544 | PortNumber port = deviceService.getPorts(deviceId).stream() |
| 545 | .filter(p -> p.annotations().value(AnnotationKeys.PORT_NAME).equals(portName) && |
| 546 | p.isEnabled()) |
| 547 | .map(Port::number) |
| 548 | .findAny() |
| 549 | .orElse(null); |
| 550 | return Optional.ofNullable(port); |
Hyunsun Moon | acbc8ef | 2016-06-21 17:58:45 -0700 | [diff] [blame] | 551 | } |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 552 | } |