blob: e66adb9c392c7e069b5e642bfd3dd8152eb15364 [file] [log] [blame]
alshabibfaa1e362015-04-02 15:01:54 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
alshabibfaa1e362015-04-02 15:01:54 -07003 *
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 */
16package org.onosproject.driver.pipeline;
17
Michele Santuarid2c8b152016-03-30 17:57:56 -070018import com.google.common.cache.Cache;
19import com.google.common.cache.CacheBuilder;
20import com.google.common.cache.RemovalCause;
21import com.google.common.cache.RemovalNotification;
alshabibfaa1e362015-04-02 15:01:54 -070022import org.onlab.osgi.ServiceDirectory;
Michele Santuarid2c8b152016-03-30 17:57:56 -070023import org.onlab.util.KryoNamespace;
Daniele Moro607fd0b2021-07-19 22:39:22 +020024import org.onosproject.core.ApplicationId;
alshabibfaa1e362015-04-02 15:01:54 -070025import org.onosproject.net.DeviceId;
Saurav Das24431192016-03-07 19:13:00 -080026import org.onosproject.net.behaviour.NextGroup;
alshabibfaa1e362015-04-02 15:01:54 -070027import org.onosproject.net.behaviour.Pipeliner;
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070028import org.onosproject.net.behaviour.PipelinerContext;
Thomas Vachuskafacc3f52015-04-10 08:58:36 -070029import org.onosproject.net.driver.AbstractHandlerBehaviour;
alshabibfaa1e362015-04-02 15:01:54 -070030import org.onosproject.net.flow.DefaultFlowRule;
alshabibc61e18c2016-02-02 23:05:25 -080031import org.onosproject.net.flow.DefaultTrafficSelector;
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070032import org.onosproject.net.flow.DefaultTrafficTreatment;
alshabibfaa1e362015-04-02 15:01:54 -070033import org.onosproject.net.flow.FlowRule;
34import org.onosproject.net.flow.FlowRuleOperations;
35import org.onosproject.net.flow.FlowRuleOperationsContext;
36import org.onosproject.net.flow.FlowRuleService;
37import org.onosproject.net.flow.TrafficSelector;
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070038import org.onosproject.net.flow.TrafficTreatment;
alshabibfaa1e362015-04-02 15:01:54 -070039import org.onosproject.net.flowobjective.FilteringObjective;
Michele Santuarid2c8b152016-03-30 17:57:56 -070040import org.onosproject.net.flowobjective.FlowObjectiveStore;
alshabibfaa1e362015-04-02 15:01:54 -070041import org.onosproject.net.flowobjective.ForwardingObjective;
alshabib77b88482015-04-07 15:47:50 -070042import org.onosproject.net.flowobjective.NextObjective;
alshabibc61e18c2016-02-02 23:05:25 -080043import org.onosproject.net.flowobjective.Objective;
alshabib2a441c62015-04-13 18:39:38 -070044import org.onosproject.net.flowobjective.ObjectiveError;
Pier Luigib69b6cc2017-01-10 15:13:14 -080045import org.onosproject.store.serializers.KryoNamespaces;
alshabibfaa1e362015-04-02 15:01:54 -070046import org.slf4j.Logger;
47
Yi Tsenge935a642017-06-19 15:19:07 -070048import java.util.Collection;
Pier Luigib69b6cc2017-01-10 15:13:14 -080049import java.util.Collections;
Saurav Das24431192016-03-07 19:13:00 -080050import java.util.List;
Michele Santuarid2c8b152016-03-30 17:57:56 -070051import java.util.concurrent.TimeUnit;
52
Pier Luigib69b6cc2017-01-10 15:13:14 -080053import static org.onosproject.net.flowobjective.Objective.Operation.ADD;
Michele Santuarid2c8b152016-03-30 17:57:56 -070054import static org.slf4j.LoggerFactory.getLogger;
Saurav Das24431192016-03-07 19:13:00 -080055
alshabibfaa1e362015-04-02 15:01:54 -070056/**
57 * Simple single table pipeline abstraction.
58 */
Thomas Vachuskafacc3f52015-04-10 08:58:36 -070059public class DefaultSingleTablePipeline extends AbstractHandlerBehaviour implements Pipeliner {
alshabibfaa1e362015-04-02 15:01:54 -070060
61 private final Logger log = getLogger(getClass());
62
63 private ServiceDirectory serviceDirectory;
64 private FlowRuleService flowRuleService;
Michele Santuarid2c8b152016-03-30 17:57:56 -070065 private FlowObjectiveStore flowObjectiveStore;
alshabibfaa1e362015-04-02 15:01:54 -070066 private DeviceId deviceId;
67
Michele Santuarid2c8b152016-03-30 17:57:56 -070068 private KryoNamespace appKryo = new KryoNamespace.Builder()
Pier Luigib69b6cc2017-01-10 15:13:14 -080069 .register(KryoNamespaces.API)
Michele Santuarid2c8b152016-03-30 17:57:56 -070070 .register(SingleGroup.class)
Charles Chaneefdedf2016-05-23 16:45:45 -070071 .build("DefaultSingleTablePipeline");
Michele Santuarid2c8b152016-03-30 17:57:56 -070072
Pier Luigib69b6cc2017-01-10 15:13:14 -080073 // Fast path for the installation. If we don't find the nextobjective in
74 // the cache, as fallback mechanism we will try to retrieve the treatments
75 // from the store. It is safe to use this cache for the addition, while it
76 // should be avoided for the removal. This cache from Guava does not offer
77 // thread-safe read (the read does not take the lock).
78 private Cache<Integer, NextObjective> pendingAddNext = CacheBuilder.newBuilder()
79 .expireAfterWrite(20, TimeUnit.SECONDS)
80 .removalListener((RemovalNotification<Integer, NextObjective> notification) -> {
81 if (notification.getCause() == RemovalCause.EXPIRED) {
82 notification.getValue().context()
83 .ifPresent(c -> c.onError(notification.getValue(),
84 ObjectiveError.FLOWINSTALLATIONFAILED));
85 }
86 }).build();
87
Michele Santuarid2c8b152016-03-30 17:57:56 -070088
alshabibfaa1e362015-04-02 15:01:54 -070089 @Override
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070090 public void init(DeviceId deviceId, PipelinerContext context) {
91 this.serviceDirectory = context.directory();
alshabibfaa1e362015-04-02 15:01:54 -070092 this.deviceId = deviceId;
93
94 flowRuleService = serviceDirectory.get(FlowRuleService.class);
Michele Santuarid2c8b152016-03-30 17:57:56 -070095 flowObjectiveStore = serviceDirectory.get(FlowObjectiveStore.class);
96
alshabibfaa1e362015-04-02 15:01:54 -070097 }
98
99 @Override
alshabibc61e18c2016-02-02 23:05:25 -0800100 public void filter(FilteringObjective filter) {
alshabibc61e18c2016-02-02 23:05:25 -0800101 TrafficTreatment.Builder actions;
102 switch (filter.type()) {
103 case PERMIT:
104 actions = (filter.meta() == null) ?
105 DefaultTrafficTreatment.builder().punt() :
106 DefaultTrafficTreatment.builder(filter.meta());
107 break;
108 case DENY:
109 actions = (filter.meta() == null) ?
110 DefaultTrafficTreatment.builder() :
111 DefaultTrafficTreatment.builder(filter.meta());
112 actions.drop();
113 break;
114 default:
115 log.warn("Unknown filter type: {}", filter.type());
116 actions = DefaultTrafficTreatment.builder().drop();
117 }
118
119 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
120
Sho SHIMIZUa09e1bb2016-08-01 14:25:25 -0700121 filter.conditions().forEach(selector::add);
alshabibc61e18c2016-02-02 23:05:25 -0800122
123 if (filter.key() != null) {
124 selector.add(filter.key());
125 }
126
127 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
128 .forDevice(deviceId)
129 .withSelector(selector.build())
130 .withTreatment(actions.build())
131 .fromApp(filter.appId())
132 .withPriority(filter.priority());
133
134 if (filter.permanent()) {
135 ruleBuilder.makePermanent();
136 } else {
137 ruleBuilder.makeTemporary(filter.timeout());
138 }
alshabibc61e18c2016-02-02 23:05:25 -0800139 installObjective(ruleBuilder, filter);
alshabibc61e18c2016-02-02 23:05:25 -0800140 }
alshabibfaa1e362015-04-02 15:01:54 -0700141
142 @Override
alshabib2a441c62015-04-13 18:39:38 -0700143 public void forward(ForwardingObjective fwd) {
alshabib2a441c62015-04-13 18:39:38 -0700144 TrafficSelector selector = fwd.selector();
Michele Santuarid2c8b152016-03-30 17:57:56 -0700145 if (fwd.treatment() != null) {
146 // Deal with SPECIFIC and VERSATILE in the same manner.
147 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
148 .forDevice(deviceId)
149 .withSelector(selector)
150 .fromApp(fwd.appId())
151 .withPriority(fwd.priority())
152 .withTreatment(fwd.treatment());
alshabibb452fd72015-04-22 20:46:20 -0700153
Michele Santuarid2c8b152016-03-30 17:57:56 -0700154 if (fwd.permanent()) {
155 ruleBuilder.makePermanent();
156 } else {
157 ruleBuilder.makeTemporary(fwd.timeout());
158 }
159 installObjective(ruleBuilder, fwd);
160
alshabibb452fd72015-04-22 20:46:20 -0700161 } else {
Pier Luigib69b6cc2017-01-10 15:13:14 -0800162 NextObjective nextObjective;
163 NextGroup next;
164 TrafficTreatment treatment;
165 if (fwd.op() == ADD) {
166 // Give a try to the cache. Doing an operation
167 // on the store seems to be very expensive.
168 nextObjective = pendingAddNext.getIfPresent(fwd.nextId());
169 // If the next objective is not present
170 // We will try with the store
171 if (nextObjective == null) {
172 next = flowObjectiveStore.getNextGroup(fwd.nextId());
173 // We verify that next was in the store and then de-serialize
174 // the treatment in order to re-build the flow rule.
175 if (next == null) {
176 fwd.context().ifPresent(c -> c.onError(fwd, ObjectiveError.GROUPMISSING));
177 return;
Michele Santuarid2c8b152016-03-30 17:57:56 -0700178 }
Pier Luigib69b6cc2017-01-10 15:13:14 -0800179 treatment = appKryo.deserialize(next.data());
180 } else {
181 pendingAddNext.invalidate(fwd.nextId());
Yi Tsenge935a642017-06-19 15:19:07 -0700182 treatment = getTreatment(nextObjective);
183 if (treatment == null) {
184 fwd.context().ifPresent(c -> c.onError(fwd, ObjectiveError.UNSUPPORTED));
185 return;
186 }
Pier Luigib69b6cc2017-01-10 15:13:14 -0800187 }
Michele Santuarid2c8b152016-03-30 17:57:56 -0700188 } else {
Pier Luigib69b6cc2017-01-10 15:13:14 -0800189 // We get the NextGroup from the remove operation.
190 // Doing an operation on the store seems to be very expensive.
Yi Tseng32c053c2017-05-15 14:17:34 -0700191 next = flowObjectiveStore.getNextGroup(fwd.nextId());
Yi Tsenge935a642017-06-19 15:19:07 -0700192 treatment = (next != null) ? appKryo.deserialize(next.data()) : null;
Michele Santuarid2c8b152016-03-30 17:57:56 -0700193 }
Pier Luigib69b6cc2017-01-10 15:13:14 -0800194 // If the treatment is null we cannot re-build the original flow
195 if (treatment == null) {
196 fwd.context().ifPresent(c -> c.onError(fwd, ObjectiveError.GROUPMISSING));
197 return;
198 }
Yi Tsenge935a642017-06-19 15:19:07 -0700199 // Finally we build the flow rule and push to the flow rule subsystem.
Pier Luigib69b6cc2017-01-10 15:13:14 -0800200 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
201 .forDevice(deviceId)
202 .withSelector(selector)
203 .fromApp(fwd.appId())
204 .withPriority(fwd.priority())
205 .withTreatment(treatment);
206 if (fwd.permanent()) {
207 ruleBuilder.makePermanent();
208 } else {
209 ruleBuilder.makeTemporary(fwd.timeout());
210 }
211 installObjective(ruleBuilder, fwd);
alshabibb452fd72015-04-22 20:46:20 -0700212 }
alshabibc61e18c2016-02-02 23:05:25 -0800213 }
214
215 private void installObjective(FlowRule.Builder ruleBuilder, Objective objective) {
216 FlowRuleOperations.Builder flowBuilder = FlowRuleOperations.builder();
217 switch (objective.op()) {
alshabib2a441c62015-04-13 18:39:38 -0700218 case ADD:
alshabibb452fd72015-04-22 20:46:20 -0700219 flowBuilder.add(ruleBuilder.build());
alshabib2a441c62015-04-13 18:39:38 -0700220 break;
221 case REMOVE:
alshabibb452fd72015-04-22 20:46:20 -0700222 flowBuilder.remove(ruleBuilder.build());
alshabib2a441c62015-04-13 18:39:38 -0700223 break;
224 default:
alshabibc61e18c2016-02-02 23:05:25 -0800225 log.warn("Unknown operation {}", objective.op());
alshabib2a441c62015-04-13 18:39:38 -0700226 }
227
alshabibfaa1e362015-04-02 15:01:54 -0700228 flowRuleService.apply(flowBuilder.build(new FlowRuleOperationsContext() {
229 @Override
230 public void onSuccess(FlowRuleOperations ops) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -0800231 objective.context().ifPresent(context -> context.onSuccess(objective));
alshabibfaa1e362015-04-02 15:01:54 -0700232 }
233
234 @Override
235 public void onError(FlowRuleOperations ops) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -0800236 objective.context()
237 .ifPresent(context -> context.onError(objective, ObjectiveError.FLOWINSTALLATIONFAILED));
alshabibfaa1e362015-04-02 15:01:54 -0700238 }
239 }));
alshabibfaa1e362015-04-02 15:01:54 -0700240 }
alshabib77b88482015-04-07 15:47:50 -0700241
242 @Override
alshabibc61e18c2016-02-02 23:05:25 -0800243 public void next(NextObjective nextObjective) {
Pier Luigib69b6cc2017-01-10 15:13:14 -0800244 switch (nextObjective.op()) {
245 case ADD:
Yi Tsenge935a642017-06-19 15:19:07 -0700246 // Check next objective
247 TrafficTreatment treatment = getTreatment(nextObjective);
248 if (treatment == null) {
249 // unsupported next objective
250 nextObjective.context().ifPresent(context -> context.onError(nextObjective,
251 ObjectiveError.UNSUPPORTED));
252 return;
253 }
Pier Luigib69b6cc2017-01-10 15:13:14 -0800254 // We insert the value in the cache
255 pendingAddNext.put(nextObjective.id(), nextObjective);
Yi Tsenge935a642017-06-19 15:19:07 -0700256
Pier Luigib69b6cc2017-01-10 15:13:14 -0800257 // Then in the store, this will unblock the queued fwd obj
258 flowObjectiveStore.putNextGroup(
259 nextObjective.id(),
Yi Tsenge935a642017-06-19 15:19:07 -0700260 new SingleGroup(treatment)
Pier Luigib69b6cc2017-01-10 15:13:14 -0800261 );
262 break;
263 case REMOVE:
Yi Tseng32c053c2017-05-15 14:17:34 -0700264 NextGroup next = flowObjectiveStore.removeNextGroup(nextObjective.id());
265 if (next == null) {
266 nextObjective.context().ifPresent(context -> context.onError(nextObjective,
267 ObjectiveError.GROUPMISSING));
268 return;
269 }
Pier Luigib69b6cc2017-01-10 15:13:14 -0800270 break;
271 default:
272 log.warn("Unsupported operation {}", nextObjective.op());
273 }
Ray Milkeyce7db1b2016-05-24 13:33:48 -0700274 nextObjective.context().ifPresent(context -> context.onSuccess(nextObjective));
alshabibc61e18c2016-02-02 23:05:25 -0800275 }
alshabib77b88482015-04-07 15:47:50 -0700276
Saurav Das24431192016-03-07 19:13:00 -0800277 @Override
Daniele Moro607fd0b2021-07-19 22:39:22 +0200278 public void purgeAll(ApplicationId appId) {
279 flowRuleService.purgeFlowRules(deviceId, appId);
280 }
281
282 @Override
Saurav Das24431192016-03-07 19:13:00 -0800283 public List<String> getNextMappings(NextGroup nextGroup) {
284 // Default single table pipeline does not use nextObjectives or groups
Pier Luigib69b6cc2017-01-10 15:13:14 -0800285 return Collections.emptyList();
Saurav Das24431192016-03-07 19:13:00 -0800286 }
287
Yi Tsenge935a642017-06-19 15:19:07 -0700288 /**
289 * Gets traffic treatment from a next objective.
290 * Merge traffic treatments from next objective if the next objective is
291 * BROADCAST type and contains multiple traffic treatments.
292 * Returns first treatment from next objective if the next objective is
293 * SIMPLE type and it contains only one treatment.
294 *
295 * @param nextObjective the next objective
296 * @return the treatment from next objective; null if not supported
297 */
298 private TrafficTreatment getTreatment(NextObjective nextObjective) {
299 Collection<TrafficTreatment> treatments = nextObjective.next();
300 switch (nextObjective.type()) {
301 case SIMPLE:
302 if (treatments.size() != 1) {
303 log.error("Next Objectives of type SIMPLE should have only " +
304 "one traffic treatment. NexObjective: {}",
305 nextObjective.toString());
306 return null;
307 }
308 return treatments.iterator().next();
309 case BROADCAST:
310 TrafficTreatment.Builder builder = DefaultTrafficTreatment.builder();
311 treatments.forEach(builder::addTreatment);
312 return builder.build();
313 default:
314 log.error("Unsupported next objective type {}.", nextObjective.type());
315 return null;
316 }
317 }
318
Michele Santuarid2c8b152016-03-30 17:57:56 -0700319 private class SingleGroup implements NextGroup {
320
Pier Luigib69b6cc2017-01-10 15:13:14 -0800321 private TrafficTreatment nextActions;
Michele Santuarid2c8b152016-03-30 17:57:56 -0700322
Pier Luigib69b6cc2017-01-10 15:13:14 -0800323 SingleGroup(TrafficTreatment next) {
324 this.nextActions = next;
Michele Santuarid2c8b152016-03-30 17:57:56 -0700325 }
326
327 @Override
328 public byte[] data() {
Pier Luigib69b6cc2017-01-10 15:13:14 -0800329 return appKryo.serialize(nextActions);
330 }
331
332 public TrafficTreatment treatment() {
333 return nextActions;
Michele Santuarid2c8b152016-03-30 17:57:56 -0700334 }
335
336 }
alshabibfaa1e362015-04-02 15:01:54 -0700337}