alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 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 | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 18 | import org.onlab.osgi.ServiceDirectory; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 19 | import org.onosproject.net.DeviceId; |
Saurav Das | 2443119 | 2016-03-07 19:13:00 -0800 | [diff] [blame] | 20 | import org.onosproject.net.behaviour.NextGroup; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 21 | import org.onosproject.net.behaviour.Pipeliner; |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 22 | import org.onosproject.net.behaviour.PipelinerContext; |
Thomas Vachuska | facc3f5 | 2015-04-10 08:58:36 -0700 | [diff] [blame] | 23 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 24 | import org.onosproject.net.flow.DefaultFlowRule; |
alshabib | c61e18c | 2016-02-02 23:05:25 -0800 | [diff] [blame] | 25 | import org.onosproject.net.flow.DefaultTrafficSelector; |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 26 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 27 | import org.onosproject.net.flow.FlowRule; |
| 28 | import org.onosproject.net.flow.FlowRuleOperations; |
| 29 | import org.onosproject.net.flow.FlowRuleOperationsContext; |
| 30 | import org.onosproject.net.flow.FlowRuleService; |
| 31 | import org.onosproject.net.flow.TrafficSelector; |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 32 | import org.onosproject.net.flow.TrafficTreatment; |
| 33 | import org.onosproject.net.flow.instructions.Instructions; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 34 | import org.onosproject.net.flowobjective.FilteringObjective; |
| 35 | import org.onosproject.net.flowobjective.ForwardingObjective; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 36 | import org.onosproject.net.flowobjective.NextObjective; |
alshabib | c61e18c | 2016-02-02 23:05:25 -0800 | [diff] [blame] | 37 | import org.onosproject.net.flowobjective.Objective; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 38 | import org.onosproject.net.flowobjective.ObjectiveError; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 39 | import org.slf4j.Logger; |
| 40 | |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 41 | import static org.slf4j.LoggerFactory.getLogger; |
| 42 | |
Saurav Das | 2443119 | 2016-03-07 19:13:00 -0800 | [diff] [blame] | 43 | import java.util.List; |
| 44 | |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 45 | /** |
| 46 | * Simple single table pipeline abstraction. |
| 47 | */ |
Thomas Vachuska | facc3f5 | 2015-04-10 08:58:36 -0700 | [diff] [blame] | 48 | public class DefaultSingleTablePipeline extends AbstractHandlerBehaviour implements Pipeliner { |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 49 | |
| 50 | private final Logger log = getLogger(getClass()); |
| 51 | |
| 52 | private ServiceDirectory serviceDirectory; |
| 53 | private FlowRuleService flowRuleService; |
| 54 | private DeviceId deviceId; |
| 55 | |
| 56 | @Override |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 57 | public void init(DeviceId deviceId, PipelinerContext context) { |
| 58 | this.serviceDirectory = context.directory(); |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 59 | this.deviceId = deviceId; |
| 60 | |
| 61 | flowRuleService = serviceDirectory.get(FlowRuleService.class); |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | @Override |
alshabib | c61e18c | 2016-02-02 23:05:25 -0800 | [diff] [blame] | 65 | public void filter(FilteringObjective filter) { |
| 66 | |
| 67 | TrafficTreatment.Builder actions; |
| 68 | switch (filter.type()) { |
| 69 | case PERMIT: |
| 70 | actions = (filter.meta() == null) ? |
| 71 | DefaultTrafficTreatment.builder().punt() : |
| 72 | DefaultTrafficTreatment.builder(filter.meta()); |
| 73 | break; |
| 74 | case DENY: |
| 75 | actions = (filter.meta() == null) ? |
| 76 | DefaultTrafficTreatment.builder() : |
| 77 | DefaultTrafficTreatment.builder(filter.meta()); |
| 78 | actions.drop(); |
| 79 | break; |
| 80 | default: |
| 81 | log.warn("Unknown filter type: {}", filter.type()); |
| 82 | actions = DefaultTrafficTreatment.builder().drop(); |
| 83 | } |
| 84 | |
| 85 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 86 | |
| 87 | filter.conditions().stream().forEach(selector::add); |
| 88 | |
| 89 | if (filter.key() != null) { |
| 90 | selector.add(filter.key()); |
| 91 | } |
| 92 | |
| 93 | FlowRule.Builder ruleBuilder = DefaultFlowRule.builder() |
| 94 | .forDevice(deviceId) |
| 95 | .withSelector(selector.build()) |
| 96 | .withTreatment(actions.build()) |
| 97 | .fromApp(filter.appId()) |
| 98 | .withPriority(filter.priority()); |
| 99 | |
| 100 | if (filter.permanent()) { |
| 101 | ruleBuilder.makePermanent(); |
| 102 | } else { |
| 103 | ruleBuilder.makeTemporary(filter.timeout()); |
| 104 | } |
| 105 | |
| 106 | installObjective(ruleBuilder, filter); |
| 107 | |
| 108 | } |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 109 | |
| 110 | @Override |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 111 | public void forward(ForwardingObjective fwd) { |
Thomas Vachuska | 8378ccf | 2016-03-02 18:02:17 -0800 | [diff] [blame] | 112 | // Deal with SPECIFIC and VERSATILE in the same manner. |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 113 | TrafficSelector selector = fwd.selector(); |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 114 | TrafficTreatment treatment = fwd.treatment(); |
| 115 | if ((fwd.treatment().deferred().size() == 0) && |
| 116 | (fwd.treatment().immediate().size() == 0) && |
| 117 | (fwd.treatment().tableTransition() == null) && |
| 118 | (!fwd.treatment().clearedDeferred())) { |
| 119 | TrafficTreatment.Builder flowTreatment = DefaultTrafficTreatment.builder(); |
Ray Milkey | 2be39ed | 2016-02-22 15:54:19 -0800 | [diff] [blame] | 120 | flowTreatment.add(Instructions.createNoAction()); |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 121 | treatment = flowTreatment.build(); |
| 122 | } |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 123 | |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 124 | FlowRule.Builder ruleBuilder = DefaultFlowRule.builder() |
| 125 | .forDevice(deviceId) |
| 126 | .withSelector(selector) |
Charles M.C. Chan | ef3f39e | 2015-07-14 22:21:46 +0800 | [diff] [blame] | 127 | .withTreatment(treatment) |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 128 | .fromApp(fwd.appId()) |
| 129 | .withPriority(fwd.priority()); |
| 130 | |
| 131 | if (fwd.permanent()) { |
| 132 | ruleBuilder.makePermanent(); |
| 133 | } else { |
| 134 | ruleBuilder.makeTemporary(fwd.timeout()); |
| 135 | } |
| 136 | |
alshabib | c61e18c | 2016-02-02 23:05:25 -0800 | [diff] [blame] | 137 | installObjective(ruleBuilder, fwd); |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 138 | |
alshabib | c61e18c | 2016-02-02 23:05:25 -0800 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | private void installObjective(FlowRule.Builder ruleBuilder, Objective objective) { |
| 142 | FlowRuleOperations.Builder flowBuilder = FlowRuleOperations.builder(); |
| 143 | switch (objective.op()) { |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 144 | |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 145 | case ADD: |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 146 | flowBuilder.add(ruleBuilder.build()); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 147 | break; |
| 148 | case REMOVE: |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 149 | flowBuilder.remove(ruleBuilder.build()); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 150 | break; |
| 151 | default: |
alshabib | c61e18c | 2016-02-02 23:05:25 -0800 | [diff] [blame] | 152 | log.warn("Unknown operation {}", objective.op()); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 153 | } |
| 154 | |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 155 | flowRuleService.apply(flowBuilder.build(new FlowRuleOperationsContext() { |
| 156 | @Override |
| 157 | public void onSuccess(FlowRuleOperations ops) { |
Sho SHIMIZU | ef7e290 | 2016-02-12 18:38:29 -0800 | [diff] [blame] | 158 | objective.context().ifPresent(context -> context.onSuccess(objective)); |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | @Override |
| 162 | public void onError(FlowRuleOperations ops) { |
Sho SHIMIZU | ef7e290 | 2016-02-12 18:38:29 -0800 | [diff] [blame] | 163 | objective.context() |
| 164 | .ifPresent(context -> context.onError(objective, ObjectiveError.FLOWINSTALLATIONFAILED)); |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 165 | } |
| 166 | })); |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 167 | } |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 168 | |
| 169 | @Override |
alshabib | c61e18c | 2016-02-02 23:05:25 -0800 | [diff] [blame] | 170 | public void next(NextObjective nextObjective) { |
| 171 | } |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 172 | |
Saurav Das | 2443119 | 2016-03-07 19:13:00 -0800 | [diff] [blame] | 173 | @Override |
| 174 | public List<String> getNextMappings(NextGroup nextGroup) { |
| 175 | // Default single table pipeline does not use nextObjectives or groups |
| 176 | return null; |
| 177 | } |
| 178 | |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 179 | } |