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 | |
Michele Santuari | d2c8b15 | 2016-03-30 17:57:56 -0700 | [diff] [blame] | 18 | import com.google.common.cache.Cache; |
| 19 | import com.google.common.cache.CacheBuilder; |
| 20 | import com.google.common.cache.RemovalCause; |
| 21 | import com.google.common.cache.RemovalNotification; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 22 | import org.onlab.osgi.ServiceDirectory; |
Michele Santuari | d2c8b15 | 2016-03-30 17:57:56 -0700 | [diff] [blame] | 23 | import org.onlab.util.KryoNamespace; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 24 | import org.onosproject.net.DeviceId; |
Saurav Das | 2443119 | 2016-03-07 19:13:00 -0800 | [diff] [blame] | 25 | import org.onosproject.net.behaviour.NextGroup; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 26 | import org.onosproject.net.behaviour.Pipeliner; |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 27 | import org.onosproject.net.behaviour.PipelinerContext; |
Thomas Vachuska | facc3f5 | 2015-04-10 08:58:36 -0700 | [diff] [blame] | 28 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 29 | import org.onosproject.net.flow.DefaultFlowRule; |
alshabib | c61e18c | 2016-02-02 23:05:25 -0800 | [diff] [blame] | 30 | import org.onosproject.net.flow.DefaultTrafficSelector; |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 31 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 32 | import org.onosproject.net.flow.FlowRule; |
| 33 | import org.onosproject.net.flow.FlowRuleOperations; |
| 34 | import org.onosproject.net.flow.FlowRuleOperationsContext; |
| 35 | import org.onosproject.net.flow.FlowRuleService; |
| 36 | import org.onosproject.net.flow.TrafficSelector; |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 37 | import org.onosproject.net.flow.TrafficTreatment; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 38 | import org.onosproject.net.flowobjective.FilteringObjective; |
Michele Santuari | d2c8b15 | 2016-03-30 17:57:56 -0700 | [diff] [blame] | 39 | import org.onosproject.net.flowobjective.FlowObjectiveStore; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 40 | import org.onosproject.net.flowobjective.ForwardingObjective; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 41 | import org.onosproject.net.flowobjective.NextObjective; |
alshabib | c61e18c | 2016-02-02 23:05:25 -0800 | [diff] [blame] | 42 | import org.onosproject.net.flowobjective.Objective; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 43 | import org.onosproject.net.flowobjective.ObjectiveError; |
Pier Luigi | b69b6cc | 2017-01-10 15:13:14 -0800 | [diff] [blame] | 44 | import org.onosproject.store.serializers.KryoNamespaces; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 45 | import org.slf4j.Logger; |
| 46 | |
Pier Luigi | b69b6cc | 2017-01-10 15:13:14 -0800 | [diff] [blame] | 47 | import java.util.Collections; |
Saurav Das | 2443119 | 2016-03-07 19:13:00 -0800 | [diff] [blame] | 48 | import java.util.List; |
Michele Santuari | d2c8b15 | 2016-03-30 17:57:56 -0700 | [diff] [blame] | 49 | import java.util.concurrent.TimeUnit; |
| 50 | |
Pier Luigi | b69b6cc | 2017-01-10 15:13:14 -0800 | [diff] [blame] | 51 | import static org.onosproject.net.flowobjective.Objective.Operation.ADD; |
Michele Santuari | d2c8b15 | 2016-03-30 17:57:56 -0700 | [diff] [blame] | 52 | import static org.slf4j.LoggerFactory.getLogger; |
Saurav Das | 2443119 | 2016-03-07 19:13:00 -0800 | [diff] [blame] | 53 | |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 54 | /** |
| 55 | * Simple single table pipeline abstraction. |
| 56 | */ |
Thomas Vachuska | facc3f5 | 2015-04-10 08:58:36 -0700 | [diff] [blame] | 57 | public class DefaultSingleTablePipeline extends AbstractHandlerBehaviour implements Pipeliner { |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 58 | |
| 59 | private final Logger log = getLogger(getClass()); |
| 60 | |
| 61 | private ServiceDirectory serviceDirectory; |
| 62 | private FlowRuleService flowRuleService; |
Michele Santuari | d2c8b15 | 2016-03-30 17:57:56 -0700 | [diff] [blame] | 63 | private FlowObjectiveStore flowObjectiveStore; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 64 | private DeviceId deviceId; |
| 65 | |
Michele Santuari | d2c8b15 | 2016-03-30 17:57:56 -0700 | [diff] [blame] | 66 | private KryoNamespace appKryo = new KryoNamespace.Builder() |
Pier Luigi | b69b6cc | 2017-01-10 15:13:14 -0800 | [diff] [blame] | 67 | .register(KryoNamespaces.API) |
Michele Santuari | d2c8b15 | 2016-03-30 17:57:56 -0700 | [diff] [blame] | 68 | .register(SingleGroup.class) |
Charles Chan | eefdedf | 2016-05-23 16:45:45 -0700 | [diff] [blame] | 69 | .build("DefaultSingleTablePipeline"); |
Michele Santuari | d2c8b15 | 2016-03-30 17:57:56 -0700 | [diff] [blame] | 70 | |
Pier Luigi | b69b6cc | 2017-01-10 15:13:14 -0800 | [diff] [blame] | 71 | // Fast path for the installation. If we don't find the nextobjective in |
| 72 | // the cache, as fallback mechanism we will try to retrieve the treatments |
| 73 | // from the store. It is safe to use this cache for the addition, while it |
| 74 | // should be avoided for the removal. This cache from Guava does not offer |
| 75 | // thread-safe read (the read does not take the lock). |
| 76 | private Cache<Integer, NextObjective> pendingAddNext = CacheBuilder.newBuilder() |
| 77 | .expireAfterWrite(20, TimeUnit.SECONDS) |
| 78 | .removalListener((RemovalNotification<Integer, NextObjective> notification) -> { |
| 79 | if (notification.getCause() == RemovalCause.EXPIRED) { |
| 80 | notification.getValue().context() |
| 81 | .ifPresent(c -> c.onError(notification.getValue(), |
| 82 | ObjectiveError.FLOWINSTALLATIONFAILED)); |
| 83 | } |
| 84 | }).build(); |
| 85 | |
Michele Santuari | d2c8b15 | 2016-03-30 17:57:56 -0700 | [diff] [blame] | 86 | |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 87 | @Override |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 88 | public void init(DeviceId deviceId, PipelinerContext context) { |
| 89 | this.serviceDirectory = context.directory(); |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 90 | this.deviceId = deviceId; |
| 91 | |
| 92 | flowRuleService = serviceDirectory.get(FlowRuleService.class); |
Michele Santuari | d2c8b15 | 2016-03-30 17:57:56 -0700 | [diff] [blame] | 93 | flowObjectiveStore = serviceDirectory.get(FlowObjectiveStore.class); |
| 94 | |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | @Override |
alshabib | c61e18c | 2016-02-02 23:05:25 -0800 | [diff] [blame] | 98 | public void filter(FilteringObjective filter) { |
| 99 | |
| 100 | TrafficTreatment.Builder actions; |
| 101 | switch (filter.type()) { |
| 102 | case PERMIT: |
| 103 | actions = (filter.meta() == null) ? |
| 104 | DefaultTrafficTreatment.builder().punt() : |
| 105 | DefaultTrafficTreatment.builder(filter.meta()); |
| 106 | break; |
| 107 | case DENY: |
| 108 | actions = (filter.meta() == null) ? |
| 109 | DefaultTrafficTreatment.builder() : |
| 110 | DefaultTrafficTreatment.builder(filter.meta()); |
| 111 | actions.drop(); |
| 112 | break; |
| 113 | default: |
| 114 | log.warn("Unknown filter type: {}", filter.type()); |
| 115 | actions = DefaultTrafficTreatment.builder().drop(); |
| 116 | } |
| 117 | |
| 118 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 119 | |
Sho SHIMIZU | a09e1bb | 2016-08-01 14:25:25 -0700 | [diff] [blame] | 120 | filter.conditions().forEach(selector::add); |
alshabib | c61e18c | 2016-02-02 23:05:25 -0800 | [diff] [blame] | 121 | |
| 122 | if (filter.key() != null) { |
| 123 | selector.add(filter.key()); |
| 124 | } |
| 125 | |
| 126 | FlowRule.Builder ruleBuilder = DefaultFlowRule.builder() |
| 127 | .forDevice(deviceId) |
| 128 | .withSelector(selector.build()) |
| 129 | .withTreatment(actions.build()) |
| 130 | .fromApp(filter.appId()) |
| 131 | .withPriority(filter.priority()); |
| 132 | |
| 133 | if (filter.permanent()) { |
| 134 | ruleBuilder.makePermanent(); |
| 135 | } else { |
| 136 | ruleBuilder.makeTemporary(filter.timeout()); |
| 137 | } |
| 138 | |
| 139 | installObjective(ruleBuilder, filter); |
| 140 | |
| 141 | } |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 142 | |
| 143 | @Override |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 144 | public void forward(ForwardingObjective fwd) { |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 145 | TrafficSelector selector = fwd.selector(); |
Michele Santuari | d2c8b15 | 2016-03-30 17:57:56 -0700 | [diff] [blame] | 146 | if (fwd.treatment() != null) { |
| 147 | // Deal with SPECIFIC and VERSATILE in the same manner. |
| 148 | FlowRule.Builder ruleBuilder = DefaultFlowRule.builder() |
| 149 | .forDevice(deviceId) |
| 150 | .withSelector(selector) |
| 151 | .fromApp(fwd.appId()) |
| 152 | .withPriority(fwd.priority()) |
| 153 | .withTreatment(fwd.treatment()); |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 154 | |
Michele Santuari | d2c8b15 | 2016-03-30 17:57:56 -0700 | [diff] [blame] | 155 | if (fwd.permanent()) { |
| 156 | ruleBuilder.makePermanent(); |
| 157 | } else { |
| 158 | ruleBuilder.makeTemporary(fwd.timeout()); |
| 159 | } |
| 160 | installObjective(ruleBuilder, fwd); |
| 161 | |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 162 | } else { |
Pier Luigi | b69b6cc | 2017-01-10 15:13:14 -0800 | [diff] [blame] | 163 | NextObjective nextObjective; |
| 164 | NextGroup next; |
| 165 | TrafficTreatment treatment; |
| 166 | if (fwd.op() == ADD) { |
| 167 | // Give a try to the cache. Doing an operation |
| 168 | // on the store seems to be very expensive. |
| 169 | nextObjective = pendingAddNext.getIfPresent(fwd.nextId()); |
| 170 | // If the next objective is not present |
| 171 | // We will try with the store |
| 172 | if (nextObjective == null) { |
| 173 | next = flowObjectiveStore.getNextGroup(fwd.nextId()); |
| 174 | // We verify that next was in the store and then de-serialize |
| 175 | // the treatment in order to re-build the flow rule. |
| 176 | if (next == null) { |
| 177 | fwd.context().ifPresent(c -> c.onError(fwd, ObjectiveError.GROUPMISSING)); |
| 178 | return; |
Michele Santuari | d2c8b15 | 2016-03-30 17:57:56 -0700 | [diff] [blame] | 179 | } |
Pier Luigi | b69b6cc | 2017-01-10 15:13:14 -0800 | [diff] [blame] | 180 | treatment = appKryo.deserialize(next.data()); |
| 181 | } else { |
| 182 | pendingAddNext.invalidate(fwd.nextId()); |
| 183 | treatment = nextObjective.next().iterator().next(); |
| 184 | } |
Michele Santuari | d2c8b15 | 2016-03-30 17:57:56 -0700 | [diff] [blame] | 185 | } else { |
Pier Luigi | b69b6cc | 2017-01-10 15:13:14 -0800 | [diff] [blame] | 186 | // We get the NextGroup from the remove operation. |
| 187 | // Doing an operation on the store seems to be very expensive. |
| 188 | next = flowObjectiveStore.removeNextGroup(fwd.nextId()); |
| 189 | if (next == null) { |
| 190 | fwd.context().ifPresent(c -> c.onError(fwd, ObjectiveError.GROUPMISSING)); |
| 191 | return; |
| 192 | } |
| 193 | treatment = appKryo.deserialize(next.data()); |
Michele Santuari | d2c8b15 | 2016-03-30 17:57:56 -0700 | [diff] [blame] | 194 | } |
Pier Luigi | b69b6cc | 2017-01-10 15:13:14 -0800 | [diff] [blame] | 195 | // If the treatment is null we cannot re-build the original flow |
| 196 | if (treatment == null) { |
| 197 | fwd.context().ifPresent(c -> c.onError(fwd, ObjectiveError.GROUPMISSING)); |
| 198 | return; |
| 199 | } |
| 200 | // Finally we build the flow rule and push to the flowrule subsystem. |
| 201 | FlowRule.Builder ruleBuilder = DefaultFlowRule.builder() |
| 202 | .forDevice(deviceId) |
| 203 | .withSelector(selector) |
| 204 | .fromApp(fwd.appId()) |
| 205 | .withPriority(fwd.priority()) |
| 206 | .withTreatment(treatment); |
| 207 | if (fwd.permanent()) { |
| 208 | ruleBuilder.makePermanent(); |
| 209 | } else { |
| 210 | ruleBuilder.makeTemporary(fwd.timeout()); |
| 211 | } |
| 212 | installObjective(ruleBuilder, fwd); |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 213 | } |
alshabib | c61e18c | 2016-02-02 23:05:25 -0800 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | private void installObjective(FlowRule.Builder ruleBuilder, Objective objective) { |
| 217 | FlowRuleOperations.Builder flowBuilder = FlowRuleOperations.builder(); |
| 218 | switch (objective.op()) { |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 219 | |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 220 | case ADD: |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 221 | flowBuilder.add(ruleBuilder.build()); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 222 | break; |
| 223 | case REMOVE: |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 224 | flowBuilder.remove(ruleBuilder.build()); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 225 | break; |
| 226 | default: |
alshabib | c61e18c | 2016-02-02 23:05:25 -0800 | [diff] [blame] | 227 | log.warn("Unknown operation {}", objective.op()); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 228 | } |
| 229 | |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 230 | flowRuleService.apply(flowBuilder.build(new FlowRuleOperationsContext() { |
| 231 | @Override |
| 232 | public void onSuccess(FlowRuleOperations ops) { |
Sho SHIMIZU | ef7e290 | 2016-02-12 18:38:29 -0800 | [diff] [blame] | 233 | objective.context().ifPresent(context -> context.onSuccess(objective)); |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | @Override |
| 237 | public void onError(FlowRuleOperations ops) { |
Sho SHIMIZU | ef7e290 | 2016-02-12 18:38:29 -0800 | [diff] [blame] | 238 | objective.context() |
| 239 | .ifPresent(context -> context.onError(objective, ObjectiveError.FLOWINSTALLATIONFAILED)); |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 240 | } |
| 241 | })); |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 242 | } |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 243 | |
| 244 | @Override |
alshabib | c61e18c | 2016-02-02 23:05:25 -0800 | [diff] [blame] | 245 | public void next(NextObjective nextObjective) { |
Pier Luigi | b69b6cc | 2017-01-10 15:13:14 -0800 | [diff] [blame] | 246 | switch (nextObjective.op()) { |
| 247 | case ADD: |
| 248 | // We insert the value in the cache |
| 249 | pendingAddNext.put(nextObjective.id(), nextObjective); |
| 250 | // Then in the store, this will unblock the queued fwd obj |
| 251 | flowObjectiveStore.putNextGroup( |
| 252 | nextObjective.id(), |
| 253 | new SingleGroup(nextObjective.next().iterator().next()) |
| 254 | ); |
| 255 | break; |
| 256 | case REMOVE: |
| 257 | break; |
| 258 | default: |
| 259 | log.warn("Unsupported operation {}", nextObjective.op()); |
| 260 | } |
Ray Milkey | ce7db1b | 2016-05-24 13:33:48 -0700 | [diff] [blame] | 261 | nextObjective.context().ifPresent(context -> context.onSuccess(nextObjective)); |
alshabib | c61e18c | 2016-02-02 23:05:25 -0800 | [diff] [blame] | 262 | } |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 263 | |
Saurav Das | 2443119 | 2016-03-07 19:13:00 -0800 | [diff] [blame] | 264 | @Override |
| 265 | public List<String> getNextMappings(NextGroup nextGroup) { |
| 266 | // Default single table pipeline does not use nextObjectives or groups |
Pier Luigi | b69b6cc | 2017-01-10 15:13:14 -0800 | [diff] [blame] | 267 | return Collections.emptyList(); |
Saurav Das | 2443119 | 2016-03-07 19:13:00 -0800 | [diff] [blame] | 268 | } |
| 269 | |
Michele Santuari | d2c8b15 | 2016-03-30 17:57:56 -0700 | [diff] [blame] | 270 | private class SingleGroup implements NextGroup { |
| 271 | |
Pier Luigi | b69b6cc | 2017-01-10 15:13:14 -0800 | [diff] [blame] | 272 | private TrafficTreatment nextActions; |
Michele Santuari | d2c8b15 | 2016-03-30 17:57:56 -0700 | [diff] [blame] | 273 | |
Pier Luigi | b69b6cc | 2017-01-10 15:13:14 -0800 | [diff] [blame] | 274 | SingleGroup(TrafficTreatment next) { |
| 275 | this.nextActions = next; |
Michele Santuari | d2c8b15 | 2016-03-30 17:57:56 -0700 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | @Override |
| 279 | public byte[] data() { |
Pier Luigi | b69b6cc | 2017-01-10 15:13:14 -0800 | [diff] [blame] | 280 | return appKryo.serialize(nextActions); |
| 281 | } |
| 282 | |
| 283 | public TrafficTreatment treatment() { |
| 284 | return nextActions; |
Michele Santuari | d2c8b15 | 2016-03-30 17:57:56 -0700 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | } |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 288 | } |