alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [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 | package org.onosproject.driver.pipeline; |
| 17 | |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame^] | 18 | import com.google.common.collect.Sets; |
| 19 | import com.google.common.util.concurrent.SettableFuture; |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 20 | import org.onlab.osgi.ServiceDirectory; |
| 21 | import org.onlab.packet.Ethernet; |
| 22 | import org.onlab.packet.MacAddress; |
| 23 | import org.onlab.packet.VlanId; |
| 24 | import org.onosproject.core.ApplicationId; |
| 25 | import org.onosproject.core.CoreService; |
| 26 | import org.onosproject.net.DeviceId; |
| 27 | import org.onosproject.net.behaviour.Pipeliner; |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 28 | import org.onosproject.net.behaviour.PipelinerContext; |
Thomas Vachuska | 5c2f813 | 2015-04-08 23:09:08 -0700 | [diff] [blame] | 29 | import org.onosproject.net.driver.AbstractBehaviour; |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 30 | import org.onosproject.net.flow.DefaultFlowRule; |
| 31 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 32 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 33 | import org.onosproject.net.flow.FlowRule; |
| 34 | import org.onosproject.net.flow.FlowRuleOperations; |
| 35 | import org.onosproject.net.flow.FlowRuleOperationsContext; |
| 36 | import org.onosproject.net.flow.FlowRuleService; |
| 37 | import org.onosproject.net.flow.TrafficSelector; |
| 38 | import org.onosproject.net.flow.TrafficTreatment; |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame^] | 39 | import org.onosproject.net.flow.criteria.Criteria; |
| 40 | import org.onosproject.net.flow.criteria.Criterion; |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 41 | import org.onosproject.net.flowobjective.FilteringObjective; |
| 42 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 43 | import org.onosproject.net.flowobjective.NextObjective; |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame^] | 44 | import org.onosproject.net.flowobjective.Objective; |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 45 | import org.slf4j.Logger; |
| 46 | |
| 47 | import java.util.Collection; |
| 48 | import java.util.concurrent.Future; |
| 49 | |
| 50 | import static org.slf4j.LoggerFactory.getLogger; |
| 51 | |
| 52 | /** |
Thomas Vachuska | 5c2f813 | 2015-04-08 23:09:08 -0700 | [diff] [blame] | 53 | * Corsa pipeline handler. |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 54 | */ |
Thomas Vachuska | 5c2f813 | 2015-04-08 23:09:08 -0700 | [diff] [blame] | 55 | public class OVSCorsaPipeline extends AbstractBehaviour implements Pipeliner { |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 56 | |
| 57 | private static final int CONTROLLER_PRIORITY = 255; |
| 58 | private static final int DROP_PRIORITY = 0; |
| 59 | private static final int HIGHEST_PRIORITY = 0xffff; |
| 60 | |
| 61 | private final Logger log = getLogger(getClass()); |
| 62 | |
| 63 | private ServiceDirectory serviceDirectory; |
| 64 | private FlowRuleService flowRuleService; |
| 65 | private CoreService coreService; |
| 66 | private DeviceId deviceId; |
| 67 | private ApplicationId appId; |
| 68 | |
| 69 | @Override |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 70 | public void init(DeviceId deviceId, PipelinerContext context) { |
| 71 | this.serviceDirectory = context.directory(); |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 72 | this.deviceId = deviceId; |
| 73 | |
| 74 | |
| 75 | coreService = serviceDirectory.get(CoreService.class); |
| 76 | flowRuleService = serviceDirectory.get(FlowRuleService.class); |
| 77 | |
| 78 | appId = coreService.registerApplication( |
| 79 | "org.onosproject.driver.OVSCorsaPipeline"); |
| 80 | |
| 81 | pushDefaultRules(); |
| 82 | |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public Future<Boolean> filter(Collection<FilteringObjective> filteringObjectives) { |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame^] | 87 | Collection<Future<Boolean>> results = |
| 88 | Sets.newHashSet(); |
| 89 | filteringObjectives.stream() |
| 90 | .filter(obj -> obj.type() == FilteringObjective.Type.PERMIT) |
| 91 | .forEach(obj -> obj.conditions() |
| 92 | .forEach(condition -> |
| 93 | results.add(processCondition(condition, |
| 94 | obj.op() == Objective.Operation.ADD, |
| 95 | obj.appId())) |
| 96 | )); |
| 97 | |
| 98 | //TODO: return something more helpful/sensible in the future (no pun intended) |
| 99 | return results.iterator().next(); |
| 100 | |
| 101 | } |
| 102 | |
| 103 | private Future<Boolean> processCondition(Criterion c, boolean install, |
| 104 | ApplicationId applicationId) { |
| 105 | SettableFuture<Boolean> result = SettableFuture.create(); |
| 106 | if (c.type() == Criterion.Type.ETH_DST) { |
| 107 | Criteria.EthCriterion e = (Criteria.EthCriterion) c; |
| 108 | log.debug("adding rule for MAC: {}", e.mac()); |
| 109 | |
| 110 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 111 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
| 112 | selector.matchEthDst(e.mac()); |
| 113 | treatment.transition(FlowRule.Type.VLAN_MPLS); |
| 114 | FlowRule rule = new DefaultFlowRule(deviceId, selector.build(), |
| 115 | treatment.build(), |
| 116 | CONTROLLER_PRIORITY, applicationId, 0, |
| 117 | true, FlowRule.Type.FIRST); |
| 118 | FlowRuleOperations.Builder ops = FlowRuleOperations.builder(); |
| 119 | ops = install ? ops.add(rule) : ops.remove(rule); |
| 120 | flowRuleService.apply(ops.build(new FlowRuleOperationsContext() { |
| 121 | @Override |
| 122 | public void onSuccess(FlowRuleOperations ops) { |
| 123 | result.set(true); |
| 124 | log.info("Provisioned default table for bgp router"); |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public void onError(FlowRuleOperations ops) { |
| 129 | result.set(false); |
| 130 | log.info("Failed to provision default table for bgp router"); |
| 131 | } |
| 132 | })); |
| 133 | } |
| 134 | return result; |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public Future<Boolean> forward(Collection<ForwardingObjective> forwardObjectives) { |
| 139 | return null; |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public Future<Boolean> next(Collection<NextObjective> nextObjectives) { |
| 144 | return null; |
| 145 | } |
| 146 | |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 147 | private void pushDefaultRules() { |
| 148 | boolean install = true; |
| 149 | processTableZero(install); |
| 150 | processTableOne(install); |
| 151 | processTableTwo(install); |
| 152 | processTableFour(install); |
| 153 | processTableFive(install); |
| 154 | processTableSix(install); |
| 155 | processTableNine(install); |
| 156 | } |
| 157 | |
| 158 | private void processTableZero(boolean install) { |
| 159 | TrafficSelector.Builder selector; |
| 160 | TrafficTreatment.Builder treatment; |
| 161 | |
| 162 | // Bcast rule |
| 163 | selector = DefaultTrafficSelector.builder(); |
| 164 | treatment = DefaultTrafficTreatment.builder(); |
| 165 | |
| 166 | selector.matchEthDst(MacAddress.BROADCAST); |
| 167 | treatment.transition(FlowRule.Type.VLAN_MPLS); |
| 168 | |
| 169 | FlowRule rule = new DefaultFlowRule(deviceId, selector.build(), |
| 170 | treatment.build(), |
| 171 | CONTROLLER_PRIORITY, appId, 0, |
| 172 | true, FlowRule.Type.FIRST); |
| 173 | |
| 174 | FlowRuleOperations.Builder ops = FlowRuleOperations.builder(); |
| 175 | |
| 176 | ops = install ? ops.add(rule) : ops.remove(rule); |
| 177 | |
| 178 | |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 179 | //Drop rule |
| 180 | selector = DefaultTrafficSelector.builder(); |
| 181 | treatment = DefaultTrafficTreatment.builder(); |
| 182 | |
| 183 | treatment.drop(); |
| 184 | |
| 185 | rule = new DefaultFlowRule(deviceId, selector.build(), |
| 186 | treatment.build(), DROP_PRIORITY, appId, |
| 187 | 0, true, FlowRule.Type.FIRST); |
| 188 | |
| 189 | ops = install ? ops.add(rule) : ops.remove(rule); |
| 190 | |
| 191 | flowRuleService.apply(ops.build(new FlowRuleOperationsContext() { |
| 192 | @Override |
| 193 | public void onSuccess(FlowRuleOperations ops) { |
| 194 | log.info("Provisioned default table for bgp router"); |
| 195 | } |
| 196 | |
| 197 | @Override |
| 198 | public void onError(FlowRuleOperations ops) { |
| 199 | log.info("Failed to provision default table for bgp router"); |
| 200 | } |
| 201 | })); |
| 202 | |
| 203 | } |
| 204 | |
| 205 | private void processTableOne(boolean install) { |
| 206 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 207 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment |
| 208 | .builder(); |
| 209 | FlowRuleOperations.Builder ops = FlowRuleOperations.builder(); |
| 210 | FlowRule rule; |
| 211 | |
| 212 | selector.matchVlanId(VlanId.ANY); |
| 213 | treatment.transition(FlowRule.Type.VLAN); |
| 214 | |
| 215 | rule = new DefaultFlowRule(deviceId, selector.build(), |
| 216 | treatment.build(), CONTROLLER_PRIORITY, |
| 217 | appId, 0, true, FlowRule.Type.VLAN_MPLS); |
| 218 | |
| 219 | ops = install ? ops.add(rule) : ops.remove(rule); |
| 220 | |
| 221 | flowRuleService.apply(ops.build(new FlowRuleOperationsContext() { |
| 222 | @Override |
| 223 | public void onSuccess(FlowRuleOperations ops) { |
| 224 | log.info("Provisioned vlan/mpls table for bgp router"); |
| 225 | } |
| 226 | |
| 227 | @Override |
| 228 | public void onError(FlowRuleOperations ops) { |
| 229 | log.info( |
| 230 | "Failed to provision vlan/mpls table for bgp router"); |
| 231 | } |
| 232 | })); |
| 233 | |
| 234 | } |
| 235 | |
| 236 | private void processTableTwo(boolean install) { |
| 237 | TrafficSelector.Builder selector; |
| 238 | TrafficTreatment.Builder treatment; |
| 239 | FlowRuleOperations.Builder ops = FlowRuleOperations.builder(); |
| 240 | FlowRule rule; |
| 241 | |
| 242 | |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 243 | //Drop rule |
| 244 | selector = DefaultTrafficSelector.builder(); |
| 245 | treatment = DefaultTrafficTreatment.builder(); |
| 246 | |
| 247 | treatment.drop(); |
| 248 | |
| 249 | rule = new DefaultFlowRule(deviceId, selector.build(), |
| 250 | treatment.build(), DROP_PRIORITY, appId, |
| 251 | 0, true, FlowRule.Type.VLAN); |
| 252 | |
| 253 | ops = install ? ops.add(rule) : ops.remove(rule); |
| 254 | |
| 255 | flowRuleService.apply(ops.build(new FlowRuleOperationsContext() { |
| 256 | @Override |
| 257 | public void onSuccess(FlowRuleOperations ops) { |
| 258 | log.info("Provisioned vlan table for bgp router"); |
| 259 | } |
| 260 | |
| 261 | @Override |
| 262 | public void onError(FlowRuleOperations ops) { |
| 263 | log.info("Failed to provision vlan table for bgp router"); |
| 264 | } |
| 265 | })); |
| 266 | } |
| 267 | |
| 268 | private void processTableFour(boolean install) { |
| 269 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 270 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment |
| 271 | .builder(); |
| 272 | FlowRuleOperations.Builder ops = FlowRuleOperations.builder(); |
| 273 | FlowRule rule; |
| 274 | |
| 275 | selector.matchEthType(Ethernet.TYPE_ARP); |
| 276 | treatment.punt(); |
| 277 | |
| 278 | rule = new DefaultFlowRule(deviceId, selector.build(), |
| 279 | treatment.build(), CONTROLLER_PRIORITY, |
| 280 | appId, 0, true, FlowRule.Type.ETHER); |
| 281 | |
| 282 | ops = install ? ops.add(rule) : ops.remove(rule); |
| 283 | |
| 284 | selector = DefaultTrafficSelector.builder(); |
| 285 | treatment = DefaultTrafficTreatment.builder(); |
| 286 | |
| 287 | selector.matchEthType(Ethernet.TYPE_IPV4); |
| 288 | treatment.transition(FlowRule.Type.COS); |
| 289 | |
| 290 | rule = new DefaultFlowRule(deviceId, selector.build(), |
| 291 | treatment.build(), CONTROLLER_PRIORITY, |
| 292 | appId, 0, true, FlowRule.Type.ETHER); |
| 293 | |
| 294 | ops = install ? ops.add(rule) : ops.remove(rule); |
| 295 | |
| 296 | //Drop rule |
| 297 | selector = DefaultTrafficSelector.builder(); |
| 298 | treatment = DefaultTrafficTreatment.builder(); |
| 299 | |
| 300 | treatment.drop(); |
| 301 | |
| 302 | rule = new DefaultFlowRule(deviceId, selector.build(), |
| 303 | treatment.build(), DROP_PRIORITY, appId, |
| 304 | 0, true, FlowRule.Type.ETHER); |
| 305 | |
| 306 | ops = install ? ops.add(rule) : ops.remove(rule); |
| 307 | |
| 308 | flowRuleService.apply(ops.build(new FlowRuleOperationsContext() { |
| 309 | @Override |
| 310 | public void onSuccess(FlowRuleOperations ops) { |
| 311 | log.info("Provisioned ether table for bgp router"); |
| 312 | } |
| 313 | |
| 314 | @Override |
| 315 | public void onError(FlowRuleOperations ops) { |
| 316 | log.info("Failed to provision ether table for bgp router"); |
| 317 | } |
| 318 | })); |
| 319 | |
| 320 | } |
| 321 | |
| 322 | private void processTableFive(boolean install) { |
| 323 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 324 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment |
| 325 | .builder(); |
| 326 | FlowRuleOperations.Builder ops = FlowRuleOperations.builder(); |
| 327 | FlowRule rule; |
| 328 | |
| 329 | treatment.transition(FlowRule.Type.IP); |
| 330 | |
| 331 | rule = new DefaultFlowRule(deviceId, selector.build(), |
| 332 | treatment.build(), DROP_PRIORITY, appId, |
| 333 | 0, true, FlowRule.Type.COS); |
| 334 | |
| 335 | ops = install ? ops.add(rule) : ops.remove(rule); |
| 336 | |
| 337 | flowRuleService.apply(ops.build(new FlowRuleOperationsContext() { |
| 338 | @Override |
| 339 | public void onSuccess(FlowRuleOperations ops) { |
| 340 | log.info("Provisioned cos table for bgp router"); |
| 341 | } |
| 342 | |
| 343 | @Override |
| 344 | public void onError(FlowRuleOperations ops) { |
| 345 | log.info("Failed to provision cos table for bgp router"); |
| 346 | } |
| 347 | })); |
| 348 | |
| 349 | } |
| 350 | |
| 351 | private void processTableSix(boolean install) { |
| 352 | TrafficSelector.Builder selector; |
| 353 | TrafficTreatment.Builder treatment; |
| 354 | FlowRuleOperations.Builder ops = FlowRuleOperations.builder(); |
| 355 | FlowRule rule; |
| 356 | |
| 357 | //Drop rule |
| 358 | selector = DefaultTrafficSelector.builder(); |
| 359 | treatment = DefaultTrafficTreatment.builder(); |
| 360 | |
| 361 | treatment.drop(); |
| 362 | |
| 363 | rule = new DefaultFlowRule(deviceId, selector.build(), |
| 364 | treatment.build(), DROP_PRIORITY, appId, |
| 365 | 0, true, FlowRule.Type.IP); |
| 366 | |
| 367 | ops = install ? ops.add(rule) : ops.remove(rule); |
| 368 | |
| 369 | flowRuleService.apply(ops.build(new FlowRuleOperationsContext() { |
| 370 | @Override |
| 371 | public void onSuccess(FlowRuleOperations ops) { |
| 372 | log.info("Provisioned FIB table for bgp router"); |
| 373 | } |
| 374 | |
| 375 | @Override |
| 376 | public void onError(FlowRuleOperations ops) { |
| 377 | log.info("Failed to provision FIB table for bgp router"); |
| 378 | } |
| 379 | })); |
| 380 | } |
| 381 | |
| 382 | private void processTableNine(boolean install) { |
| 383 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 384 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment |
| 385 | .builder(); |
| 386 | FlowRuleOperations.Builder ops = FlowRuleOperations.builder(); |
| 387 | FlowRule rule; |
| 388 | |
| 389 | treatment.punt(); |
| 390 | |
| 391 | rule = new DefaultFlowRule(deviceId, selector.build(), |
| 392 | treatment.build(), CONTROLLER_PRIORITY, |
| 393 | appId, 0, true, FlowRule.Type.DEFAULT); |
| 394 | |
| 395 | ops = install ? ops.add(rule) : ops.remove(rule); |
| 396 | |
| 397 | flowRuleService.apply(ops.build(new FlowRuleOperationsContext() { |
| 398 | @Override |
| 399 | public void onSuccess(FlowRuleOperations ops) { |
| 400 | log.info("Provisioned Local table for bgp router"); |
| 401 | } |
| 402 | |
| 403 | @Override |
| 404 | public void onError(FlowRuleOperations ops) { |
| 405 | log.info("Failed to provision Local table for bgp router"); |
| 406 | } |
| 407 | })); |
| 408 | } |
| 409 | |
| 410 | } |