blob: 7ef72fa11f139702374831b30866f3530b49387f [file] [log] [blame]
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -07001/*
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 */
16package org.onosproject.driver.pipeline;
17
18import static org.onlab.util.Tools.groupedThreads;
19import static org.slf4j.LoggerFactory.getLogger;
20
21import com.google.common.cache.Cache;
22import com.google.common.cache.CacheBuilder;
23import com.google.common.cache.RemovalCause;
24import com.google.common.cache.RemovalNotification;
25
26import org.onlab.osgi.ServiceDirectory;
27import org.onlab.packet.Ethernet;
28import org.onlab.packet.VlanId;
29import org.onlab.util.KryoNamespace;
30import org.onosproject.core.ApplicationId;
31import org.onosproject.core.CoreService;
32import org.onosproject.net.DeviceId;
33import org.onosproject.net.PortNumber;
34import org.onosproject.net.behaviour.NextGroup;
35import org.onosproject.net.behaviour.Pipeliner;
36import org.onosproject.net.behaviour.PipelinerContext;
37import org.onosproject.net.driver.AbstractHandlerBehaviour;
38import org.onosproject.net.flow.DefaultFlowRule;
39import org.onosproject.net.flow.DefaultTrafficSelector;
40import org.onosproject.net.flow.DefaultTrafficTreatment;
41import org.onosproject.net.flow.FlowRule;
42import org.onosproject.net.flow.FlowRuleOperations;
43import org.onosproject.net.flow.FlowRuleOperationsContext;
44import org.onosproject.net.flow.FlowRuleService;
45import org.onosproject.net.flow.TrafficSelector;
46import org.onosproject.net.flow.TrafficTreatment;
47import org.onosproject.net.flow.criteria.Criteria;
48import org.onosproject.net.flow.criteria.Criterion;
49import org.onosproject.net.flow.criteria.EthCriterion;
50import org.onosproject.net.flow.criteria.EthTypeCriterion;
51import org.onosproject.net.flow.criteria.IPCriterion;
52import org.onosproject.net.flow.criteria.MplsCriterion;
53import org.onosproject.net.flow.criteria.PortCriterion;
54import org.onosproject.net.flow.criteria.VlanIdCriterion;
55import org.onosproject.net.flow.instructions.Instruction;
Saurav Das822c4e22015-10-23 10:51:11 -070056import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070057import org.onosproject.net.flowobjective.FilteringObjective;
58import org.onosproject.net.flowobjective.FlowObjectiveStore;
59import org.onosproject.net.flowobjective.ForwardingObjective;
60import org.onosproject.net.flowobjective.NextObjective;
61import org.onosproject.net.flowobjective.Objective;
62import org.onosproject.net.flowobjective.ObjectiveError;
63import org.onosproject.net.group.DefaultGroupBucket;
64import org.onosproject.net.group.DefaultGroupDescription;
65import org.onosproject.net.group.DefaultGroupKey;
66import org.onosproject.net.group.Group;
67import org.onosproject.net.group.GroupBucket;
68import org.onosproject.net.group.GroupBuckets;
69import org.onosproject.net.group.GroupDescription;
70import org.onosproject.net.group.GroupEvent;
71import org.onosproject.net.group.GroupKey;
72import org.onosproject.net.group.GroupListener;
73import org.onosproject.net.group.GroupService;
74import org.slf4j.Logger;
75
76import java.util.ArrayList;
77import java.util.Collection;
78import java.util.Collections;
79import java.util.List;
80import java.util.Set;
81import java.util.concurrent.Executors;
82import java.util.concurrent.ScheduledExecutorService;
83import java.util.concurrent.TimeUnit;
84import java.util.stream.Collectors;
85
86/**
87 * Driver for SPRING-OPEN pipeline.
88 */
89public class SpringOpenTTP extends AbstractHandlerBehaviour
90 implements Pipeliner {
91
92 // Default table ID - compatible with CpqD switch
93 private static final int TABLE_VLAN = 0;
94 private static final int TABLE_TMAC = 1;
95 private static final int TABLE_IPV4_UNICAST = 2;
96 private static final int TABLE_MPLS = 3;
97 private static final int TABLE_ACL = 5;
98
99 /**
100 * Set the default values. These variables will get overwritten based on the
101 * switch vendor type
102 */
103 protected int vlanTableId = TABLE_VLAN;
104 protected int tmacTableId = TABLE_TMAC;
105 protected int ipv4UnicastTableId = TABLE_IPV4_UNICAST;
106 protected int mplsTableId = TABLE_MPLS;
107 protected int aclTableId = TABLE_ACL;
108
109 protected final Logger log = getLogger(getClass());
110
111 private ServiceDirectory serviceDirectory;
112 private FlowRuleService flowRuleService;
113 private CoreService coreService;
114 protected GroupService groupService;
115 protected FlowObjectiveStore flowObjectiveStore;
116 protected DeviceId deviceId;
117 private ApplicationId appId;
118
119 private Cache<GroupKey, NextObjective> pendingGroups;
120
121 private ScheduledExecutorService groupChecker = Executors
122 .newScheduledThreadPool(2,
123 groupedThreads("onos/pipeliner",
124 "spring-open-%d"));
125 protected KryoNamespace appKryo = new KryoNamespace.Builder()
126 .register(GroupKey.class).register(DefaultGroupKey.class)
127 .register(SegmentRoutingGroup.class).register(byte[].class).build();
128
129 @Override
130 public void init(DeviceId deviceId, PipelinerContext context) {
131 this.serviceDirectory = context.directory();
132 this.deviceId = deviceId;
133
134 pendingGroups = CacheBuilder
135 .newBuilder()
136 .expireAfterWrite(20, TimeUnit.SECONDS)
137 .removalListener((RemovalNotification<GroupKey, NextObjective> notification) -> {
138 if (notification.getCause() == RemovalCause.EXPIRED) {
139 fail(notification.getValue(),
140 ObjectiveError.GROUPINSTALLATIONFAILED);
141 }
142 }).build();
143
144 groupChecker.scheduleAtFixedRate(new GroupChecker(), 0, 500,
145 TimeUnit.MILLISECONDS);
146
147 coreService = serviceDirectory.get(CoreService.class);
148 flowRuleService = serviceDirectory.get(FlowRuleService.class);
149 groupService = serviceDirectory.get(GroupService.class);
150 flowObjectiveStore = context.store();
151
152 groupService.addListener(new InnerGroupListener());
153
154 appId = coreService
155 .registerApplication("org.onosproject.driver.SpringOpenTTP");
156
157 setTableMissEntries();
158 log.info("Spring Open TTP driver initialized");
159 }
160
161 @Override
162 public void filter(FilteringObjective filteringObjective) {
163 if (filteringObjective.type() == FilteringObjective.Type.PERMIT) {
164 log.debug("processing PERMIT filter objective");
165 processFilter(filteringObjective,
166 filteringObjective.op() == Objective.Operation.ADD,
167 filteringObjective.appId());
168 } else {
169 log.debug("filter objective other than PERMIT not supported");
170 fail(filteringObjective, ObjectiveError.UNSUPPORTED);
171 }
172 }
173
174 @Override
175 public void forward(ForwardingObjective fwd) {
176 Collection<FlowRule> rules;
177 FlowRuleOperations.Builder flowBuilder = FlowRuleOperations.builder();
178
179 rules = processForward(fwd);
180 switch (fwd.op()) {
181 case ADD:
182 rules.stream().filter(rule -> rule != null)
183 .forEach(flowBuilder::add);
184 break;
185 case REMOVE:
186 rules.stream().filter(rule -> rule != null)
187 .forEach(flowBuilder::remove);
188 break;
189 default:
190 fail(fwd, ObjectiveError.UNKNOWN);
191 log.warn("Unknown forwarding type {}", fwd.op());
192 }
193
194 flowRuleService.apply(flowBuilder
195 .build(new FlowRuleOperationsContext() {
196 @Override
197 public void onSuccess(FlowRuleOperations ops) {
198 pass(fwd);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700199 log.debug("Provisioned tables in {} with "
200 + "forwarding rules for segment "
201 + "router", deviceId);
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700202 }
203
204 @Override
205 public void onError(FlowRuleOperations ops) {
206 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700207 log.warn("Failed to provision tables in {} with "
208 + "forwarding rules for segment router",
209 deviceId);
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700210 }
211 }));
212
213 }
214
215 @Override
216 public void next(NextObjective nextObjective) {
sangho834e4b02015-05-01 09:38:25 -0700217
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700218 log.debug("Processing NextObjective id{} op{}", nextObjective.id(),
219 nextObjective.op());
sangho834e4b02015-05-01 09:38:25 -0700220 if (nextObjective.op() == Objective.Operation.REMOVE) {
sangho1e575652015-05-14 00:39:53 -0700221 if (nextObjective.next().isEmpty()) {
sangho834e4b02015-05-01 09:38:25 -0700222 removeGroup(nextObjective);
223 } else {
224 removeBucketFromGroup(nextObjective);
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700225 }
sangho834e4b02015-05-01 09:38:25 -0700226 } else if (nextObjective.op() == Objective.Operation.ADD) {
227 NextGroup nextGroup = flowObjectiveStore.getNextGroup(nextObjective.id());
228 if (nextGroup != null) {
229 addBucketToGroup(nextObjective);
230 } else {
231 addGroup(nextObjective);
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700232 }
sangho834e4b02015-05-01 09:38:25 -0700233 } else {
234 log.warn("Unsupported operation {}", nextObjective.op());
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700235 }
236
237 }
238
sangho834e4b02015-05-01 09:38:25 -0700239 private void removeGroup(NextObjective nextObjective) {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700240 log.debug("removeGroup in {}: for next objective id {}",
241 deviceId, nextObjective.id());
sangho834e4b02015-05-01 09:38:25 -0700242 final GroupKey key = new DefaultGroupKey(
243 appKryo.serialize(nextObjective.id()));
244 groupService.removeGroup(deviceId, key, appId);
245 }
246
247 private void addGroup(NextObjective nextObjective) {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700248 log.debug("addGroup with type{} for nextObjective id {}",
249 nextObjective.type(), nextObjective.id());
sangho834e4b02015-05-01 09:38:25 -0700250 switch (nextObjective.type()) {
251 case SIMPLE:
252 log.debug("processing SIMPLE next objective");
253 Collection<TrafficTreatment> treatments = nextObjective.next();
254 if (treatments.size() == 1) {
255 TrafficTreatment treatment = treatments.iterator().next();
256 GroupBucket bucket = DefaultGroupBucket
257 .createIndirectGroupBucket(treatment);
258 final GroupKey key = new DefaultGroupKey(
259 appKryo.serialize(nextObjective
260 .id()));
261 GroupDescription groupDescription = new DefaultGroupDescription(
262 deviceId,
263 GroupDescription.Type.INDIRECT,
264 new GroupBuckets(
265 Collections.singletonList(bucket)),
266 key,
Saurav Das100e3b82015-04-30 11:12:10 -0700267 null,
sangho834e4b02015-05-01 09:38:25 -0700268 nextObjective.appId());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700269 log.debug("Creating SIMPLE group for next objective id {}",
270 nextObjective.id());
sangho834e4b02015-05-01 09:38:25 -0700271 groupService.addGroup(groupDescription);
272 pendingGroups.put(key, nextObjective);
273 }
274 break;
275 case HASHED:
276 log.debug("processing HASHED next objective");
277 List<GroupBucket> buckets = nextObjective
278 .next()
279 .stream()
280 .map((treatment) -> DefaultGroupBucket
281 .createSelectGroupBucket(treatment))
282 .collect(Collectors.toList());
283 if (!buckets.isEmpty()) {
284 final GroupKey key = new DefaultGroupKey(
285 appKryo.serialize(nextObjective
286 .id()));
287 GroupDescription groupDescription = new DefaultGroupDescription(
288 deviceId,
289 GroupDescription.Type.SELECT,
290 new GroupBuckets(buckets),
291 key,
Saurav Das100e3b82015-04-30 11:12:10 -0700292 null,
sangho834e4b02015-05-01 09:38:25 -0700293 nextObjective.appId());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700294 log.debug("Creating HASHED group for next objective id {}",
295 nextObjective.id());
sangho834e4b02015-05-01 09:38:25 -0700296 groupService.addGroup(groupDescription);
297 pendingGroups.put(key, nextObjective);
298 }
299 break;
300 case BROADCAST:
301 case FAILOVER:
302 log.debug("BROADCAST and FAILOVER next objectives not supported");
303 fail(nextObjective, ObjectiveError.UNSUPPORTED);
304 log.warn("Unsupported next objective type {}", nextObjective.type());
305 break;
306 default:
307 fail(nextObjective, ObjectiveError.UNKNOWN);
308 log.warn("Unknown next objective type {}", nextObjective.type());
309 }
310 }
311
312 private void addBucketToGroup(NextObjective nextObjective) {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700313 log.debug("addBucketToGroup in {}: for next objective id {}",
314 deviceId, nextObjective.id());
sangho834e4b02015-05-01 09:38:25 -0700315 Collection<TrafficTreatment> treatments = nextObjective.next();
316 TrafficTreatment treatment = treatments.iterator().next();
317 final GroupKey key = new DefaultGroupKey(
318 appKryo.serialize(nextObjective
319 .id()));
320 Group group = groupService.getGroup(deviceId, key);
321 if (group == null) {
322 log.warn("Group is not found in {} for {}", deviceId, key);
323 return;
324 }
325 GroupBucket bucket;
326 if (group.type() == GroupDescription.Type.INDIRECT) {
327 bucket = DefaultGroupBucket.createIndirectGroupBucket(treatment);
328 } else if (group.type() == GroupDescription.Type.SELECT) {
329 bucket = DefaultGroupBucket.createSelectGroupBucket(treatment);
330 } else {
331 log.warn("Unsupported Group type {}", group.type());
332 return;
333 }
Sho SHIMIZU98ffca82015-05-11 08:39:24 -0700334 GroupBuckets bucketsToAdd = new GroupBuckets(Collections.singletonList(bucket));
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700335 log.debug("Adding buckets to group id {} of next objective id {} in device {}",
336 group.id(), nextObjective.id(), deviceId);
sangho834e4b02015-05-01 09:38:25 -0700337 groupService.addBucketsToGroup(deviceId, key, bucketsToAdd, key, appId);
338 }
339
340 private void removeBucketFromGroup(NextObjective nextObjective) {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700341 log.debug("removeBucketFromGroup in {}: for next objective id {}",
342 deviceId, nextObjective.id());
sangho834e4b02015-05-01 09:38:25 -0700343 NextGroup nextGroup = flowObjectiveStore.getNextGroup(nextObjective.id());
344 if (nextGroup != null) {
345 Collection<TrafficTreatment> treatments = nextObjective.next();
346 TrafficTreatment treatment = treatments.iterator().next();
347 final GroupKey key = new DefaultGroupKey(
348 appKryo.serialize(nextObjective
349 .id()));
350 Group group = groupService.getGroup(deviceId, key);
351 if (group == null) {
352 log.warn("Group is not found in {} for {}", deviceId, key);
353 return;
354 }
355 GroupBucket bucket;
356 if (group.type() == GroupDescription.Type.INDIRECT) {
357 bucket = DefaultGroupBucket.createIndirectGroupBucket(treatment);
358 } else if (group.type() == GroupDescription.Type.SELECT) {
359 bucket = DefaultGroupBucket.createSelectGroupBucket(treatment);
360 } else {
361 log.warn("Unsupported Group type {}", group.type());
362 return;
363 }
Sho SHIMIZU98ffca82015-05-11 08:39:24 -0700364 GroupBuckets removeBuckets = new GroupBuckets(Collections.singletonList(bucket));
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700365 log.debug("Removing buckets from group id {} of next objective id {} in device {}",
366 group.id(), nextObjective.id(), deviceId);
sangho834e4b02015-05-01 09:38:25 -0700367 groupService.removeBucketsFromGroup(deviceId, key, removeBuckets, key, appId);
368 }
369 }
370
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700371 private Collection<FlowRule> processForward(ForwardingObjective fwd) {
372 switch (fwd.flag()) {
373 case SPECIFIC:
374 return processSpecific(fwd);
375 case VERSATILE:
376 return processVersatile(fwd);
377 default:
378 fail(fwd, ObjectiveError.UNKNOWN);
379 log.warn("Unknown forwarding flag {}", fwd.flag());
380 }
381 return Collections.emptySet();
382 }
383
384 private Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
sangho1e575652015-05-14 00:39:53 -0700385 log.debug("Processing versatile forwarding objective");
386 TrafficSelector selector = fwd.selector();
Saurav Das822c4e22015-10-23 10:51:11 -0700387 TrafficTreatment treatment = null;
sangho1e575652015-05-14 00:39:53 -0700388 EthTypeCriterion ethType =
389 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
390 if (ethType == null) {
391 log.error("Versatile forwarding objective must include ethType");
392 fail(fwd, ObjectiveError.UNKNOWN);
393 return Collections.emptySet();
394 }
395
sangho1e575652015-05-14 00:39:53 -0700396 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment
397 .builder();
398 treatmentBuilder.wipeDeferred();
399
400 if (fwd.nextId() != null) {
401 NextGroup next = flowObjectiveStore.getNextGroup(fwd.nextId());
402
403 if (next != null) {
404 GroupKey key = appKryo.deserialize(next.data());
405
406 Group group = groupService.getGroup(deviceId, key);
407
408 if (group == null) {
409 log.warn("The group left!");
410 fail(fwd, ObjectiveError.GROUPMISSING);
411 return Collections.emptySet();
412 }
413 treatmentBuilder.deferred().group(group.id());
Saurav Das822c4e22015-10-23 10:51:11 -0700414 treatment = treatmentBuilder.build();
sangho1e575652015-05-14 00:39:53 -0700415 log.debug("Adding OUTGROUP action");
416 }
Saurav Das822c4e22015-10-23 10:51:11 -0700417 } else if (fwd.treatment() != null) {
418 if (fwd.treatment().allInstructions().size() == 1 &&
419 fwd.treatment().allInstructions().get(0).type() == Instruction.Type.OUTPUT) {
420 OutputInstruction o = (OutputInstruction) fwd.treatment().allInstructions().get(0);
421 if (o.port() == PortNumber.CONTROLLER) {
422 log.warn("Punts to the controller are handled by misses in"
423 + " the TMAC, IP and MPLS tables.");
424 return Collections.emptySet();
425 }
426 }
427 treatment = fwd.treatment();
sangho1e575652015-05-14 00:39:53 -0700428 } else {
Saurav Das822c4e22015-10-23 10:51:11 -0700429 log.warn("VERSATILE forwarding objective needs next objective ID "
430 + "or treatment.");
sangho1e575652015-05-14 00:39:53 -0700431 return Collections.emptySet();
432 }
433
sangho1e575652015-05-14 00:39:53 -0700434 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
435 .fromApp(fwd.appId()).withPriority(fwd.priority())
sanghof9d0bf12015-05-19 11:57:42 -0700436 .forDevice(deviceId).withSelector(fwd.selector())
sangho1e575652015-05-14 00:39:53 -0700437 .withTreatment(treatment);
438
439 if (fwd.permanent()) {
440 ruleBuilder.makePermanent();
441 } else {
442 ruleBuilder.makeTemporary(fwd.timeout());
443 }
444
445 ruleBuilder.forTable(aclTableId);
446 return Collections.singletonList(ruleBuilder.build());
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700447 }
448
449 protected Collection<FlowRule> processSpecific(ForwardingObjective fwd) {
450 log.debug("Processing specific");
451 TrafficSelector selector = fwd.selector();
452 EthTypeCriterion ethType = (EthTypeCriterion) selector
453 .getCriterion(Criterion.Type.ETH_TYPE);
454 if ((ethType == null) ||
alshabibcaf1ca22015-06-25 15:18:16 -0700455 (ethType.ethType().toShort() != Ethernet.TYPE_IPV4) &&
456 (ethType.ethType().toShort() != Ethernet.MPLS_UNICAST)) {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700457 log.warn("processSpecific: Unsupported "
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700458 + "forwarding objective criteraia");
459 fail(fwd, ObjectiveError.UNSUPPORTED);
460 return Collections.emptySet();
461 }
462
463 TrafficSelector.Builder filteredSelectorBuilder =
464 DefaultTrafficSelector.builder();
465 int forTableId = -1;
alshabibcaf1ca22015-06-25 15:18:16 -0700466 if (ethType.ethType().toShort() == Ethernet.TYPE_IPV4) {
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700467 filteredSelectorBuilder = filteredSelectorBuilder
468 .matchEthType(Ethernet.TYPE_IPV4)
469 .matchIPDst(((IPCriterion) selector
470 .getCriterion(Criterion.Type.IPV4_DST))
471 .ip());
472 forTableId = ipv4UnicastTableId;
473 log.debug("processing IPv4 specific forwarding objective");
474 } else {
475 filteredSelectorBuilder = filteredSelectorBuilder
476 .matchEthType(Ethernet.MPLS_UNICAST)
477 .matchMplsLabel(((MplsCriterion)
478 selector.getCriterion(Criterion.Type.MPLS_LABEL)).label());
479 //TODO: Add Match for BoS
480 //if (selector.getCriterion(Criterion.Type.MPLS_BOS) != null) {
481 //}
482 forTableId = mplsTableId;
483 log.debug("processing MPLS specific forwarding objective");
484 }
485
486 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment
487 .builder();
488 if (fwd.treatment() != null) {
489 for (Instruction i : fwd.treatment().allInstructions()) {
490 treatmentBuilder.add(i);
491 }
492 }
493
494 //TODO: Analyze the forwarding objective here to make
495 //device specific decision such as no ECMP groups in Dell
496 //switches.
497 if (fwd.nextId() != null) {
498 NextGroup next = flowObjectiveStore.getNextGroup(fwd.nextId());
499
500 if (next != null) {
501 GroupKey key = appKryo.deserialize(next.data());
502
503 Group group = groupService.getGroup(deviceId, key);
504
505 if (group == null) {
506 log.warn("The group left!");
507 fail(fwd, ObjectiveError.GROUPMISSING);
508 return Collections.emptySet();
509 }
sangho1e575652015-05-14 00:39:53 -0700510 treatmentBuilder.deferred().group(group.id());
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700511 log.debug("Adding OUTGROUP action");
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700512 } else {
513 log.warn("processSpecific: No associated next objective object");
514 fail(fwd, ObjectiveError.GROUPMISSING);
515 return Collections.emptySet();
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700516 }
517 }
518
519 TrafficSelector filteredSelector = filteredSelectorBuilder.build();
520 TrafficTreatment treatment = treatmentBuilder.transition(aclTableId)
521 .build();
522
523 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
524 .fromApp(fwd.appId()).withPriority(fwd.priority())
525 .forDevice(deviceId).withSelector(filteredSelector)
526 .withTreatment(treatment);
527
528 if (fwd.permanent()) {
529 ruleBuilder.makePermanent();
530 } else {
531 ruleBuilder.makeTemporary(fwd.timeout());
532 }
533
534 ruleBuilder.forTable(forTableId);
535 return Collections.singletonList(ruleBuilder.build());
536
537 }
538
539 protected List<FlowRule> processEthDstFilter(Criterion c,
540 FilteringObjective filt,
541 ApplicationId applicationId) {
542 List<FlowRule> rules = new ArrayList<FlowRule>();
543 EthCriterion e = (EthCriterion) c;
544 TrafficSelector.Builder selectorIp = DefaultTrafficSelector
545 .builder();
546 TrafficTreatment.Builder treatmentIp = DefaultTrafficTreatment
547 .builder();
548 selectorIp.matchEthDst(e.mac());
549 selectorIp.matchEthType(Ethernet.TYPE_IPV4);
550 treatmentIp.transition(ipv4UnicastTableId);
551 FlowRule ruleIp = DefaultFlowRule.builder().forDevice(deviceId)
552 .withSelector(selectorIp.build())
553 .withTreatment(treatmentIp.build())
554 .withPriority(filt.priority()).fromApp(applicationId)
555 .makePermanent().forTable(tmacTableId).build();
556 log.debug("adding IP ETH rule for MAC: {}", e.mac());
557 rules.add(ruleIp);
558
559 TrafficSelector.Builder selectorMpls = DefaultTrafficSelector
560 .builder();
561 TrafficTreatment.Builder treatmentMpls = DefaultTrafficTreatment
562 .builder();
563 selectorMpls.matchEthDst(e.mac());
564 selectorMpls.matchEthType(Ethernet.MPLS_UNICAST);
565 treatmentMpls.transition(mplsTableId);
566 FlowRule ruleMpls = DefaultFlowRule.builder()
567 .forDevice(deviceId).withSelector(selectorMpls.build())
568 .withTreatment(treatmentMpls.build())
569 .withPriority(filt.priority()).fromApp(applicationId)
570 .makePermanent().forTable(tmacTableId).build();
571 log.debug("adding MPLS ETH rule for MAC: {}", e.mac());
572 rules.add(ruleMpls);
573
574 return rules;
575 }
576
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700577 protected List<FlowRule> processVlanIdFilter(Criterion c,
578 FilteringObjective filt,
579 ApplicationId applicationId) {
580 List<FlowRule> rules = new ArrayList<FlowRule>();
581 VlanIdCriterion v = (VlanIdCriterion) c;
582 log.debug("adding rule for VLAN: {}", v.vlanId());
583 TrafficSelector.Builder selector = DefaultTrafficSelector
584 .builder();
585 TrafficTreatment.Builder treatment = DefaultTrafficTreatment
586 .builder();
587 PortCriterion p = (PortCriterion) filt.key();
588 if (v.vlanId() != VlanId.NONE) {
589 selector.matchVlanId(v.vlanId());
590 selector.matchInPort(p.port());
591 treatment.deferred().popVlan();
592 }
593 treatment.transition(tmacTableId);
594 FlowRule rule = DefaultFlowRule.builder().forDevice(deviceId)
595 .withSelector(selector.build())
596 .withTreatment(treatment.build())
597 .withPriority(filt.priority()).fromApp(applicationId)
598 .makePermanent().forTable(vlanTableId).build();
599 rules.add(rule);
600
601 return rules;
602 }
603
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700604 private void processFilter(FilteringObjective filt, boolean install,
605 ApplicationId applicationId) {
606 // This driver only processes filtering criteria defined with switch
607 // ports as the key
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700608 if (filt.key().equals(Criteria.dummy())
609 || filt.key().type() != Criterion.Type.IN_PORT) {
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700610 log.warn("No key defined in filtering objective from app: {}. Not"
611 + "processing filtering objective", applicationId);
612 fail(filt, ObjectiveError.UNKNOWN);
613 return;
614 }
615 // convert filtering conditions for switch-intfs into flowrules
616 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
617 for (Criterion c : filt.conditions()) {
618 if (c.type() == Criterion.Type.ETH_DST) {
619 for (FlowRule rule : processEthDstFilter(c,
620 filt,
621 applicationId)) {
622 ops = install ? ops.add(rule) : ops.remove(rule);
623 }
624 } else if (c.type() == Criterion.Type.VLAN_VID) {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700625 for (FlowRule rule : processVlanIdFilter(c,
626 filt,
627 applicationId)) {
628 ops = install ? ops.add(rule) : ops.remove(rule);
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700629 }
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700630 } else if (c.type() == Criterion.Type.IPV4_DST) {
Saurav Das822c4e22015-10-23 10:51:11 -0700631 log.debug("driver does not process IP filtering rules as it " +
632 "sends all misses in the IP table to the controller");
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700633 } else {
634 log.warn("Driver does not currently process filtering condition"
635 + " of type: {}", c.type());
636 fail(filt, ObjectiveError.UNSUPPORTED);
637 }
638 }
639 // apply filtering flow rules
640 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
641 @Override
642 public void onSuccess(FlowRuleOperations ops) {
643 pass(filt);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700644 log.debug("Provisioned tables in {} with fitering "
645 + "rules for segment router", deviceId);
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700646 }
647
648 @Override
649 public void onError(FlowRuleOperations ops) {
650 fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700651 log.warn("Failed to provision tables in {} with "
652 + "fitering rules for segment router", deviceId);
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700653 }
654 }));
655 }
656
657 protected void setTableMissEntries() {
658 // set all table-miss-entries
659 populateTableMissEntry(vlanTableId, true, false, false, -1);
660 populateTableMissEntry(tmacTableId, true, false, false, -1);
661 populateTableMissEntry(ipv4UnicastTableId, false, true, true,
662 aclTableId);
663 populateTableMissEntry(mplsTableId, false, true, true, aclTableId);
664 populateTableMissEntry(aclTableId, false, false, false, -1);
665 }
666
667 protected void populateTableMissEntry(int tableToAdd,
668 boolean toControllerNow,
669 boolean toControllerWrite,
670 boolean toTable, int tableToSend) {
671 TrafficSelector selector = DefaultTrafficSelector.builder().build();
672 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
673
674 if (toControllerNow) {
675 tBuilder.setOutput(PortNumber.CONTROLLER);
676 }
677
678 if (toControllerWrite) {
679 tBuilder.deferred().setOutput(PortNumber.CONTROLLER);
680 }
681
682 if (toTable) {
683 tBuilder.transition(tableToSend);
684 }
685
686 FlowRule flow = DefaultFlowRule.builder().forDevice(deviceId)
687 .withSelector(selector).withTreatment(tBuilder.build())
688 .withPriority(0).fromApp(appId).makePermanent()
689 .forTable(tableToAdd).build();
690
691 flowRuleService.applyFlowRules(flow);
692 }
693
694 private void pass(Objective obj) {
695 if (obj.context().isPresent()) {
696 obj.context().get().onSuccess(obj);
697 }
698 }
699
700 protected void fail(Objective obj, ObjectiveError error) {
701 if (obj.context().isPresent()) {
702 obj.context().get().onError(obj, error);
703 }
704 }
705
706 private class InnerGroupListener implements GroupListener {
707 @Override
708 public void event(GroupEvent event) {
709 if (event.type() == GroupEvent.Type.GROUP_ADDED) {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700710 log.debug("InnerGroupListener: Group ADDED "
711 + "event received in device {}", deviceId);
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700712 GroupKey key = event.subject().appCookie();
713
714 NextObjective obj = pendingGroups.getIfPresent(key);
715 if (obj != null) {
716 flowObjectiveStore
717 .putNextGroup(obj.id(),
718 new SegmentRoutingGroup(key));
719 pass(obj);
720 pendingGroups.invalidate(key);
721 }
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700722 } else if (event.type() == GroupEvent.Type.GROUP_ADD_FAILED) {
723 log.warn("InnerGroupListener: Group ADD "
724 + "failed event received in device {}", deviceId);
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700725 }
726 }
727 }
728
729 private class GroupChecker implements Runnable {
730
731 @Override
732 public void run() {
733 Set<GroupKey> keys = pendingGroups
734 .asMap()
735 .keySet()
736 .stream()
737 .filter(key -> groupService.getGroup(deviceId, key) != null)
738 .collect(Collectors.toSet());
739
740 keys.stream()
741 .forEach(key -> {
742 NextObjective obj = pendingGroups
743 .getIfPresent(key);
744 if (obj == null) {
745 return;
746 }
747 pass(obj);
748 pendingGroups.invalidate(key);
749 flowObjectiveStore.putNextGroup(obj.id(),
750 new SegmentRoutingGroup(
751 key));
752 });
753 }
754 }
755
756 private class SegmentRoutingGroup implements NextGroup {
757
758 private final GroupKey key;
759
760 public SegmentRoutingGroup(GroupKey key) {
761 this.key = key;
762 }
763
Saurav Das822c4e22015-10-23 10:51:11 -0700764 @SuppressWarnings("unused")
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700765 public GroupKey key() {
766 return key;
767 }
768
769 @Override
770 public byte[] data() {
771 return appKryo.serialize(key);
772 }
773
774 }
775}