blob: 426f36c8a1113f8e848a4936f0cefcb7bb64d022 [file] [log] [blame]
alshabibfaa1e362015-04-02 15:01:54 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
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;
alshabibfaa1e362015-04-02 15:01:54 -070024import org.onosproject.net.DeviceId;
Saurav Das24431192016-03-07 19:13:00 -080025import org.onosproject.net.behaviour.NextGroup;
alshabibfaa1e362015-04-02 15:01:54 -070026import org.onosproject.net.behaviour.Pipeliner;
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070027import org.onosproject.net.behaviour.PipelinerContext;
Thomas Vachuskafacc3f52015-04-10 08:58:36 -070028import org.onosproject.net.driver.AbstractHandlerBehaviour;
alshabibfaa1e362015-04-02 15:01:54 -070029import org.onosproject.net.flow.DefaultFlowRule;
alshabibc61e18c2016-02-02 23:05:25 -080030import org.onosproject.net.flow.DefaultTrafficSelector;
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070031import org.onosproject.net.flow.DefaultTrafficTreatment;
alshabibfaa1e362015-04-02 15:01:54 -070032import org.onosproject.net.flow.FlowRule;
33import org.onosproject.net.flow.FlowRuleOperations;
34import org.onosproject.net.flow.FlowRuleOperationsContext;
35import org.onosproject.net.flow.FlowRuleService;
36import org.onosproject.net.flow.TrafficSelector;
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070037import org.onosproject.net.flow.TrafficTreatment;
alshabibfaa1e362015-04-02 15:01:54 -070038import org.onosproject.net.flowobjective.FilteringObjective;
Michele Santuarid2c8b152016-03-30 17:57:56 -070039import org.onosproject.net.flowobjective.FlowObjectiveStore;
alshabibfaa1e362015-04-02 15:01:54 -070040import org.onosproject.net.flowobjective.ForwardingObjective;
alshabib77b88482015-04-07 15:47:50 -070041import org.onosproject.net.flowobjective.NextObjective;
alshabibc61e18c2016-02-02 23:05:25 -080042import org.onosproject.net.flowobjective.Objective;
alshabib2a441c62015-04-13 18:39:38 -070043import org.onosproject.net.flowobjective.ObjectiveError;
Pier Luigib69b6cc2017-01-10 15:13:14 -080044import org.onosproject.store.serializers.KryoNamespaces;
alshabibfaa1e362015-04-02 15:01:54 -070045import org.slf4j.Logger;
46
Pier Luigib69b6cc2017-01-10 15:13:14 -080047import java.util.Collections;
Saurav Das24431192016-03-07 19:13:00 -080048import java.util.List;
Michele Santuarid2c8b152016-03-30 17:57:56 -070049import java.util.concurrent.TimeUnit;
50
Pier Luigib69b6cc2017-01-10 15:13:14 -080051import static org.onosproject.net.flowobjective.Objective.Operation.ADD;
Michele Santuarid2c8b152016-03-30 17:57:56 -070052import static org.slf4j.LoggerFactory.getLogger;
Saurav Das24431192016-03-07 19:13:00 -080053
alshabibfaa1e362015-04-02 15:01:54 -070054/**
55 * Simple single table pipeline abstraction.
56 */
Thomas Vachuskafacc3f52015-04-10 08:58:36 -070057public class DefaultSingleTablePipeline extends AbstractHandlerBehaviour implements Pipeliner {
alshabibfaa1e362015-04-02 15:01:54 -070058
59 private final Logger log = getLogger(getClass());
60
61 private ServiceDirectory serviceDirectory;
62 private FlowRuleService flowRuleService;
Michele Santuarid2c8b152016-03-30 17:57:56 -070063 private FlowObjectiveStore flowObjectiveStore;
alshabibfaa1e362015-04-02 15:01:54 -070064 private DeviceId deviceId;
65
Michele Santuarid2c8b152016-03-30 17:57:56 -070066 private KryoNamespace appKryo = new KryoNamespace.Builder()
Pier Luigib69b6cc2017-01-10 15:13:14 -080067 .register(KryoNamespaces.API)
Michele Santuarid2c8b152016-03-30 17:57:56 -070068 .register(SingleGroup.class)
Charles Chaneefdedf2016-05-23 16:45:45 -070069 .build("DefaultSingleTablePipeline");
Michele Santuarid2c8b152016-03-30 17:57:56 -070070
Pier Luigib69b6cc2017-01-10 15:13:14 -080071 // 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 Santuarid2c8b152016-03-30 17:57:56 -070086
alshabibfaa1e362015-04-02 15:01:54 -070087 @Override
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070088 public void init(DeviceId deviceId, PipelinerContext context) {
89 this.serviceDirectory = context.directory();
alshabibfaa1e362015-04-02 15:01:54 -070090 this.deviceId = deviceId;
91
92 flowRuleService = serviceDirectory.get(FlowRuleService.class);
Michele Santuarid2c8b152016-03-30 17:57:56 -070093 flowObjectiveStore = serviceDirectory.get(FlowObjectiveStore.class);
94
alshabibfaa1e362015-04-02 15:01:54 -070095 }
96
97 @Override
alshabibc61e18c2016-02-02 23:05:25 -080098 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 SHIMIZUa09e1bb2016-08-01 14:25:25 -0700120 filter.conditions().forEach(selector::add);
alshabibc61e18c2016-02-02 23:05:25 -0800121
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 }
alshabibfaa1e362015-04-02 15:01:54 -0700142
143 @Override
alshabib2a441c62015-04-13 18:39:38 -0700144 public void forward(ForwardingObjective fwd) {
alshabib2a441c62015-04-13 18:39:38 -0700145 TrafficSelector selector = fwd.selector();
Michele Santuarid2c8b152016-03-30 17:57:56 -0700146 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());
alshabibb452fd72015-04-22 20:46:20 -0700154
Michele Santuarid2c8b152016-03-30 17:57:56 -0700155 if (fwd.permanent()) {
156 ruleBuilder.makePermanent();
157 } else {
158 ruleBuilder.makeTemporary(fwd.timeout());
159 }
160 installObjective(ruleBuilder, fwd);
161
alshabibb452fd72015-04-22 20:46:20 -0700162 } else {
Pier Luigib69b6cc2017-01-10 15:13:14 -0800163 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 Santuarid2c8b152016-03-30 17:57:56 -0700179 }
Pier Luigib69b6cc2017-01-10 15:13:14 -0800180 treatment = appKryo.deserialize(next.data());
181 } else {
182 pendingAddNext.invalidate(fwd.nextId());
183 treatment = nextObjective.next().iterator().next();
184 }
Michele Santuarid2c8b152016-03-30 17:57:56 -0700185 } else {
Pier Luigib69b6cc2017-01-10 15:13:14 -0800186 // 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 Santuarid2c8b152016-03-30 17:57:56 -0700194 }
Pier Luigib69b6cc2017-01-10 15:13:14 -0800195 // 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);
alshabibb452fd72015-04-22 20:46:20 -0700213 }
alshabibc61e18c2016-02-02 23:05:25 -0800214 }
215
216 private void installObjective(FlowRule.Builder ruleBuilder, Objective objective) {
217 FlowRuleOperations.Builder flowBuilder = FlowRuleOperations.builder();
218 switch (objective.op()) {
alshabibfaa1e362015-04-02 15:01:54 -0700219
alshabib2a441c62015-04-13 18:39:38 -0700220 case ADD:
alshabibb452fd72015-04-22 20:46:20 -0700221 flowBuilder.add(ruleBuilder.build());
alshabib2a441c62015-04-13 18:39:38 -0700222 break;
223 case REMOVE:
alshabibb452fd72015-04-22 20:46:20 -0700224 flowBuilder.remove(ruleBuilder.build());
alshabib2a441c62015-04-13 18:39:38 -0700225 break;
226 default:
alshabibc61e18c2016-02-02 23:05:25 -0800227 log.warn("Unknown operation {}", objective.op());
alshabib2a441c62015-04-13 18:39:38 -0700228 }
229
alshabibfaa1e362015-04-02 15:01:54 -0700230 flowRuleService.apply(flowBuilder.build(new FlowRuleOperationsContext() {
231 @Override
232 public void onSuccess(FlowRuleOperations ops) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -0800233 objective.context().ifPresent(context -> context.onSuccess(objective));
alshabibfaa1e362015-04-02 15:01:54 -0700234 }
235
236 @Override
237 public void onError(FlowRuleOperations ops) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -0800238 objective.context()
239 .ifPresent(context -> context.onError(objective, ObjectiveError.FLOWINSTALLATIONFAILED));
alshabibfaa1e362015-04-02 15:01:54 -0700240 }
241 }));
alshabibfaa1e362015-04-02 15:01:54 -0700242 }
alshabib77b88482015-04-07 15:47:50 -0700243
244 @Override
alshabibc61e18c2016-02-02 23:05:25 -0800245 public void next(NextObjective nextObjective) {
Pier Luigib69b6cc2017-01-10 15:13:14 -0800246 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 Milkeyce7db1b2016-05-24 13:33:48 -0700261 nextObjective.context().ifPresent(context -> context.onSuccess(nextObjective));
alshabibc61e18c2016-02-02 23:05:25 -0800262 }
alshabib77b88482015-04-07 15:47:50 -0700263
Saurav Das24431192016-03-07 19:13:00 -0800264 @Override
265 public List<String> getNextMappings(NextGroup nextGroup) {
266 // Default single table pipeline does not use nextObjectives or groups
Pier Luigib69b6cc2017-01-10 15:13:14 -0800267 return Collections.emptyList();
Saurav Das24431192016-03-07 19:13:00 -0800268 }
269
Michele Santuarid2c8b152016-03-30 17:57:56 -0700270 private class SingleGroup implements NextGroup {
271
Pier Luigib69b6cc2017-01-10 15:13:14 -0800272 private TrafficTreatment nextActions;
Michele Santuarid2c8b152016-03-30 17:57:56 -0700273
Pier Luigib69b6cc2017-01-10 15:13:14 -0800274 SingleGroup(TrafficTreatment next) {
275 this.nextActions = next;
Michele Santuarid2c8b152016-03-30 17:57:56 -0700276 }
277
278 @Override
279 public byte[] data() {
Pier Luigib69b6cc2017-01-10 15:13:14 -0800280 return appKryo.serialize(nextActions);
281 }
282
283 public TrafficTreatment treatment() {
284 return nextActions;
Michele Santuarid2c8b152016-03-30 17:57:56 -0700285 }
286
287 }
alshabibfaa1e362015-04-02 15:01:54 -0700288}