blob: 810ff1c35c2b067b118bafb1d6ccb1aa47a0bfeb [file] [log] [blame]
alshabib0ccde6d2015-05-30 18:22:36 -07001/*
Charles Chanaedabfd2016-02-26 09:31:48 -08002 * Copyright 2015-2016 Open Networking Laboratory
alshabib0ccde6d2015-05-30 18:22:36 -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
alshabibd61b77b2016-02-01 23:30:53 -080018import com.google.common.cache.Cache;
19import com.google.common.cache.CacheBuilder;
20import com.google.common.cache.RemovalCause;
21import com.google.common.cache.RemovalNotification;
alshabibfd430b62015-12-16 18:56:38 -080022import com.google.common.collect.Lists;
23import org.apache.commons.lang3.tuple.ImmutablePair;
24import org.apache.commons.lang3.tuple.Pair;
alshabib0ccde6d2015-05-30 18:22:36 -070025import org.onlab.osgi.ServiceDirectory;
Jonathan Hartdfc3b862015-07-01 14:49:56 -070026import org.onlab.packet.EthType;
alshabibfd430b62015-12-16 18:56:38 -080027import org.onlab.packet.IPv4;
28import org.onlab.packet.VlanId;
alshabibd61b77b2016-02-01 23:30:53 -080029import org.onlab.util.KryoNamespace;
Jonathan Hartdfc3b862015-07-01 14:49:56 -070030import org.onosproject.core.ApplicationId;
31import org.onosproject.core.CoreService;
alshabib0ccde6d2015-05-30 18:22:36 -070032import org.onosproject.net.DeviceId;
33import org.onosproject.net.PortNumber;
alshabibd61b77b2016-02-01 23:30:53 -080034import org.onosproject.net.behaviour.NextGroup;
alshabib0ccde6d2015-05-30 18:22:36 -070035import org.onosproject.net.behaviour.Pipeliner;
36import org.onosproject.net.behaviour.PipelinerContext;
37import org.onosproject.net.driver.AbstractHandlerBehaviour;
38import org.onosproject.net.flow.DefaultFlowRule;
Jonathan Hartdfc3b862015-07-01 14:49:56 -070039import org.onosproject.net.flow.DefaultTrafficSelector;
alshabib0ccde6d2015-05-30 18:22:36 -070040import 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;
alshabibfd430b62015-12-16 18:56:38 -080047import org.onosproject.net.flow.criteria.Criteria;
48import org.onosproject.net.flow.criteria.Criterion;
49import org.onosproject.net.flow.criteria.EthTypeCriterion;
alshabib1aa58142016-02-17 15:37:56 -080050import org.onosproject.net.flow.criteria.IPCriterion;
alshabibfd430b62015-12-16 18:56:38 -080051import org.onosproject.net.flow.criteria.IPProtocolCriterion;
52import org.onosproject.net.flow.criteria.PortCriterion;
alshabib2f74f2c2016-01-14 13:29:35 -080053import org.onosproject.net.flow.criteria.VlanIdCriterion;
alshabibfd430b62015-12-16 18:56:38 -080054import org.onosproject.net.flow.instructions.Instruction;
alshabib0ccde6d2015-05-30 18:22:36 -070055import org.onosproject.net.flow.instructions.Instructions;
alshabibfd430b62015-12-16 18:56:38 -080056import org.onosproject.net.flow.instructions.L2ModificationInstruction;
alshabib0ccde6d2015-05-30 18:22:36 -070057import org.onosproject.net.flowobjective.FilteringObjective;
alshabibd61b77b2016-02-01 23:30:53 -080058import org.onosproject.net.flowobjective.FlowObjectiveStore;
alshabib0ccde6d2015-05-30 18:22:36 -070059import org.onosproject.net.flowobjective.ForwardingObjective;
60import org.onosproject.net.flowobjective.NextObjective;
alshabibfd430b62015-12-16 18:56:38 -080061import org.onosproject.net.flowobjective.Objective;
alshabib0ccde6d2015-05-30 18:22:36 -070062import org.onosproject.net.flowobjective.ObjectiveError;
alshabibd61b77b2016-02-01 23:30:53 -080063import 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.onosproject.store.serializers.KryoNamespaces;
alshabib0ccde6d2015-05-30 18:22:36 -070075import org.slf4j.Logger;
76
alshabibfd430b62015-12-16 18:56:38 -080077import java.util.Collection;
alshabibd61b77b2016-02-01 23:30:53 -080078import java.util.Collections;
alshabibfd430b62015-12-16 18:56:38 -080079import java.util.List;
80import java.util.Optional;
alshabibd61b77b2016-02-01 23:30:53 -080081import java.util.concurrent.TimeUnit;
alshabib56efe432016-02-25 17:57:24 -050082import java.util.concurrent.atomic.AtomicLong;
alshabibfd430b62015-12-16 18:56:38 -080083import java.util.stream.Collectors;
84
alshabib0ccde6d2015-05-30 18:22:36 -070085import static org.slf4j.LoggerFactory.getLogger;
86
87/**
Jonathan Hart64da69d2015-07-15 15:10:28 -070088 * Pipeliner for OLT device.
alshabib0ccde6d2015-05-30 18:22:36 -070089 */
alshabibfd430b62015-12-16 18:56:38 -080090
Jonathan Hartb92cc512015-11-16 23:05:21 -080091public class OltPipeline extends AbstractHandlerBehaviour implements Pipeliner {
alshabib0ccde6d2015-05-30 18:22:36 -070092
alshabibfd430b62015-12-16 18:56:38 -080093 private static final Integer QQ_TABLE = 1;
alshabibd61b77b2016-02-01 23:30:53 -080094 private static final short MCAST_VLAN = 4000;
alshabib0ccde6d2015-05-30 18:22:36 -070095 private final Logger log = getLogger(getClass());
96
97 private ServiceDirectory serviceDirectory;
98 private FlowRuleService flowRuleService;
alshabibd61b77b2016-02-01 23:30:53 -080099 private GroupService groupService;
Jonathan Hartdfc3b862015-07-01 14:49:56 -0700100 private CoreService coreService;
101
alshabibd61b77b2016-02-01 23:30:53 -0800102 private DeviceId deviceId;
Jonathan Hartdfc3b862015-07-01 14:49:56 -0700103 private ApplicationId appId;
Charles Chanaedabfd2016-02-26 09:31:48 -0800104 // NOTE: OLT currently has some issue with cookie 0. Pick something larger
105 // to avoid collision
alshabib56efe432016-02-25 17:57:24 -0500106 private AtomicLong counter = new AtomicLong(123);
alshabib0ccde6d2015-05-30 18:22:36 -0700107
alshabibd61b77b2016-02-01 23:30:53 -0800108 protected FlowObjectiveStore flowObjectiveStore;
109
110 private Cache<GroupKey, NextObjective> pendingGroups;
111
112 protected static KryoNamespace appKryo = new KryoNamespace.Builder()
113 .register(KryoNamespaces.API)
114 .register(GroupKey.class)
115 .register(DefaultGroupKey.class)
116 .register(OLTPipelineGroup.class)
117 .register(byte[].class)
118 .build();
alshabib2cc73cb2015-06-30 20:26:56 -0700119
alshabib0ccde6d2015-05-30 18:22:36 -0700120 @Override
121 public void init(DeviceId deviceId, PipelinerContext context) {
alshabibfd430b62015-12-16 18:56:38 -0800122 log.debug("Initiate OLT pipeline");
alshabib0ccde6d2015-05-30 18:22:36 -0700123 this.serviceDirectory = context.directory();
124 this.deviceId = deviceId;
alshabibd61b77b2016-02-01 23:30:53 -0800125
alshabib0ccde6d2015-05-30 18:22:36 -0700126 flowRuleService = serviceDirectory.get(FlowRuleService.class);
Jonathan Hartdfc3b862015-07-01 14:49:56 -0700127 coreService = serviceDirectory.get(CoreService.class);
alshabibd61b77b2016-02-01 23:30:53 -0800128 groupService = serviceDirectory.get(GroupService.class);
129 flowObjectiveStore = context.store();
130
alshabibb32cefe2015-06-08 18:15:05 -0700131
Jonathan Hartdfc3b862015-07-01 14:49:56 -0700132 appId = coreService.registerApplication(
133 "org.onosproject.driver.OLTPipeline");
134
alshabibd61b77b2016-02-01 23:30:53 -0800135
136 pendingGroups = CacheBuilder.newBuilder()
137 .expireAfterWrite(20, TimeUnit.SECONDS)
138 .removalListener((RemovalNotification<GroupKey, NextObjective> notification) -> {
139 if (notification.getCause() == RemovalCause.EXPIRED) {
140 fail(notification.getValue(), ObjectiveError.GROUPINSTALLATIONFAILED);
141 }
142 }).build();
143
144 groupService.addListener(new InnerGroupListener());
145
alshabibb32cefe2015-06-08 18:15:05 -0700146 }
147
alshabib0ccde6d2015-05-30 18:22:36 -0700148 @Override
149 public void filter(FilteringObjective filter) {
alshabibfd430b62015-12-16 18:56:38 -0800150 Instructions.OutputInstruction output;
alshabib0ccde6d2015-05-30 18:22:36 -0700151
alshabibfd430b62015-12-16 18:56:38 -0800152 if (filter.meta() != null && !filter.meta().immediate().isEmpty()) {
153 output = (Instructions.OutputInstruction) filter.meta().immediate().stream()
154 .filter(t -> t.type().equals(Instruction.Type.OUTPUT))
155 .limit(1)
156 .findFirst().get();
alshabib0ccde6d2015-05-30 18:22:36 -0700157
alshabibbb424232016-01-15 12:20:25 -0800158 if (output == null || !output.port().equals(PortNumber.CONTROLLER)) {
159 log.error("OLT can only filter packet to controller");
alshabibfd430b62015-12-16 18:56:38 -0800160 fail(filter, ObjectiveError.UNSUPPORTED);
161 return;
alshabib0ccde6d2015-05-30 18:22:36 -0700162 }
alshabibfd430b62015-12-16 18:56:38 -0800163 } else {
164 fail(filter, ObjectiveError.BADPARAMS);
alshabib0ccde6d2015-05-30 18:22:36 -0700165 return;
166 }
167
alshabibfd430b62015-12-16 18:56:38 -0800168 if (filter.key().type() != Criterion.Type.IN_PORT) {
169 fail(filter, ObjectiveError.BADPARAMS);
170 return;
171 }
172
173 EthTypeCriterion ethType = (EthTypeCriterion)
174 filterForCriterion(filter.conditions(), Criterion.Type.ETH_TYPE);
175
176 if (ethType == null) {
177 fail(filter, ObjectiveError.BADPARAMS);
178 return;
179 }
180
alshabibbb424232016-01-15 12:20:25 -0800181 if (ethType.ethType().equals(EthType.EtherType.EAPOL.ethType())) {
alshabibfd430b62015-12-16 18:56:38 -0800182 provisionEapol(filter, ethType, output);
alshabibbb424232016-01-15 12:20:25 -0800183 } else if (ethType.ethType().equals(EthType.EtherType.IPV4.ethType())) {
alshabibfd430b62015-12-16 18:56:38 -0800184 IPProtocolCriterion ipProto = (IPProtocolCriterion)
185 filterForCriterion(filter.conditions(), Criterion.Type.IP_PROTO);
186 if (ipProto.protocol() == IPv4.PROTOCOL_IGMP) {
Jonathan Hart51539b82015-10-29 09:53:04 -0700187 provisionIgmp(filter, ethType, ipProto, output);
alshabibbb424232016-01-15 12:20:25 -0800188 } else {
189 log.error("OLT can only filter igmp");
190 fail(filter, ObjectiveError.UNSUPPORTED);
alshabibfd430b62015-12-16 18:56:38 -0800191 }
192 } else {
alshabibbb424232016-01-15 12:20:25 -0800193 log.error("OLT can only filter eapol and igmp");
alshabibfd430b62015-12-16 18:56:38 -0800194 fail(filter, ObjectiveError.UNSUPPORTED);
195 }
196
197 }
198
199
200 @Override
201 public void forward(ForwardingObjective fwd) {
alshabibd61b77b2016-02-01 23:30:53 -0800202
203 if (checkForMulticast(fwd)) {
204 processMulticastRule(fwd);
205 return;
206 }
207
alshabib0ccde6d2015-05-30 18:22:36 -0700208 TrafficTreatment treatment = fwd.treatment();
alshabib0ccde6d2015-05-30 18:22:36 -0700209
alshabibfd430b62015-12-16 18:56:38 -0800210 List<Instruction> instructions = treatment.allInstructions();
alshabib0ccde6d2015-05-30 18:22:36 -0700211
alshabibfd430b62015-12-16 18:56:38 -0800212 Optional<Instruction> vlanIntruction = instructions.stream()
213 .filter(i -> i.type() == Instruction.Type.L2MODIFICATION)
214 .filter(i -> ((L2ModificationInstruction) i).subtype() ==
215 L2ModificationInstruction.L2SubType.VLAN_PUSH ||
216 ((L2ModificationInstruction) i).subtype() ==
217 L2ModificationInstruction.L2SubType.VLAN_POP)
218 .findAny();
219
220 if (!vlanIntruction.isPresent()) {
221 fail(fwd, ObjectiveError.BADPARAMS);
222 return;
223 }
224
225 L2ModificationInstruction vlanIns =
226 (L2ModificationInstruction) vlanIntruction.get();
227
228 if (vlanIns.subtype() == L2ModificationInstruction.L2SubType.VLAN_PUSH) {
229 installUpstreamRules(fwd);
230 } else if (vlanIns.subtype() == L2ModificationInstruction.L2SubType.VLAN_POP) {
231 installDownstreamRules(fwd);
alshabib0ccde6d2015-05-30 18:22:36 -0700232 } else {
alshabibfd430b62015-12-16 18:56:38 -0800233 log.error("Unknown OLT operation: {}", fwd);
234 fail(fwd, ObjectiveError.UNSUPPORTED);
235 return;
alshabib0ccde6d2015-05-30 18:22:36 -0700236 }
237
alshabibfd430b62015-12-16 18:56:38 -0800238 pass(fwd);
alshabib0ccde6d2015-05-30 18:22:36 -0700239
alshabib0ccde6d2015-05-30 18:22:36 -0700240 }
241
alshabibd61b77b2016-02-01 23:30:53 -0800242
alshabib0ccde6d2015-05-30 18:22:36 -0700243 @Override
244 public void next(NextObjective nextObjective) {
alshabibd61b77b2016-02-01 23:30:53 -0800245 if (nextObjective.type() != NextObjective.Type.BROADCAST) {
246 log.error("OLT only supports broadcast groups.");
247 fail(nextObjective, ObjectiveError.BADPARAMS);
248 }
249
250 if (nextObjective.next().size() != 1) {
251 log.error("OLT only supports singleton broadcast groups.");
252 fail(nextObjective, ObjectiveError.BADPARAMS);
253 }
254
255 TrafficTreatment treatment = nextObjective.next().stream().findFirst().get();
256
257
258 GroupBucket bucket = DefaultGroupBucket.createAllGroupBucket(treatment);
259 GroupKey key = new DefaultGroupKey(appKryo.serialize(nextObjective.id()));
260
alshabib1aa58142016-02-17 15:37:56 -0800261
alshabibd61b77b2016-02-01 23:30:53 -0800262 pendingGroups.put(key, nextObjective);
263
264 switch (nextObjective.op()) {
265 case ADD:
alshabib1aa58142016-02-17 15:37:56 -0800266 GroupDescription groupDesc =
267 new DefaultGroupDescription(deviceId,
268 GroupDescription.Type.ALL,
269 new GroupBuckets(Collections.singletonList(bucket)),
270 key,
271 null,
272 nextObjective.appId());
alshabibd61b77b2016-02-01 23:30:53 -0800273 groupService.addGroup(groupDesc);
274 break;
275 case REMOVE:
276 groupService.removeGroup(deviceId, key, nextObjective.appId());
277 break;
278 case ADD_TO_EXISTING:
alshabib1aa58142016-02-17 15:37:56 -0800279 groupService.addBucketsToGroup(deviceId, key,
280 new GroupBuckets(Collections.singletonList(bucket)),
281 key, nextObjective.appId());
282 break;
alshabibd61b77b2016-02-01 23:30:53 -0800283 case REMOVE_FROM_EXISTING:
alshabib1aa58142016-02-17 15:37:56 -0800284 groupService.removeBucketsFromGroup(deviceId, key,
alshabib56efe432016-02-25 17:57:24 -0500285 new GroupBuckets(Collections.singletonList(bucket)),
286 key, nextObjective.appId());
alshabibd61b77b2016-02-01 23:30:53 -0800287 break;
288 default:
289 log.warn("Unknown next objective operation: {}", nextObjective.op());
290 }
291
292
293 }
294
295 private void processMulticastRule(ForwardingObjective fwd) {
296 if (fwd.nextId() == null) {
297 log.error("Multicast objective does not have a next id");
298 fail(fwd, ObjectiveError.BADPARAMS);
299 }
300
alshabib1aa58142016-02-17 15:37:56 -0800301 GroupKey key = getGroupForNextObjective(fwd.nextId());
alshabibd61b77b2016-02-01 23:30:53 -0800302
alshabib1aa58142016-02-17 15:37:56 -0800303 if (key == null) {
alshabibd61b77b2016-02-01 23:30:53 -0800304 log.error("Group for forwarding objective missing: {}", fwd);
305 fail(fwd, ObjectiveError.GROUPMISSING);
306 }
307
alshabib1aa58142016-02-17 15:37:56 -0800308 Group group = groupService.getGroup(deviceId, key);
alshabibd61b77b2016-02-01 23:30:53 -0800309 TrafficTreatment treatment =
310 buildTreatment(Instructions.createGroup(group.id()));
311
312 FlowRule rule = DefaultFlowRule.builder()
alshabib56efe432016-02-25 17:57:24 -0500313 .withCookie(counter.getAndIncrement())
alshabibd61b77b2016-02-01 23:30:53 -0800314 .forDevice(deviceId)
315 .forTable(0)
alshabibd61b77b2016-02-01 23:30:53 -0800316 .makePermanent()
317 .withPriority(fwd.priority())
318 .withSelector(fwd.selector())
319 .withTreatment(treatment)
320 .build();
321
322 FlowRuleOperations.Builder builder = FlowRuleOperations.builder();
323 switch (fwd.op()) {
324
325 case ADD:
326 builder.add(rule);
327 break;
328 case REMOVE:
329 builder.remove(rule);
330 break;
331 case ADD_TO_EXISTING:
332 case REMOVE_FROM_EXISTING:
333 break;
334 default:
335 log.warn("Unknown forwarding operation: {}", fwd.op());
336 }
337
338 applyFlowRules(builder, fwd);
339
340 }
341
342 private boolean checkForMulticast(ForwardingObjective fwd) {
343
alshabib1aa58142016-02-17 15:37:56 -0800344 IPCriterion ip = (IPCriterion) filterForCriterion(fwd.selector().criteria(),
alshabib56efe432016-02-25 17:57:24 -0500345 Criterion.Type.IPV4_DST);
alshabibd61b77b2016-02-01 23:30:53 -0800346
alshabib1aa58142016-02-17 15:37:56 -0800347 if (ip == null) {
348 return false;
349 }
350
Charles Chanaedabfd2016-02-26 09:31:48 -0800351 return ip.ip().isMulticast();
alshabibd61b77b2016-02-01 23:30:53 -0800352
353 }
354
alshabib1aa58142016-02-17 15:37:56 -0800355 private GroupKey getGroupForNextObjective(Integer nextId) {
alshabibd61b77b2016-02-01 23:30:53 -0800356 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
alshabib1aa58142016-02-17 15:37:56 -0800357 return appKryo.deserialize(next.data());
alshabibd61b77b2016-02-01 23:30:53 -0800358
alshabib0ccde6d2015-05-30 18:22:36 -0700359 }
360
alshabibfd430b62015-12-16 18:56:38 -0800361 private void installDownstreamRules(ForwardingObjective fwd) {
362 List<Pair<Instruction, Instruction>> vlanOps =
363 vlanOps(fwd,
364 L2ModificationInstruction.L2SubType.VLAN_POP);
365
366 if (vlanOps == null) {
367 return;
368 }
369
370 Instruction output = fetchOutput(fwd, "downstream");
371
372 if (output == null) {
373 return;
374 }
375
376 Pair<Instruction, Instruction> popAndRewrite = vlanOps.remove(0);
377
alshabibfa0dc662016-01-13 11:23:53 -0800378 TrafficSelector selector = fwd.selector();
379
380 Criterion outerVlan = selector.getCriterion(Criterion.Type.VLAN_VID);
381 Criterion innerVlan = selector.getCriterion(Criterion.Type.INNER_VLAN_VID);
382 Criterion inport = selector.getCriterion(Criterion.Type.IN_PORT);
383
384 if (outerVlan == null || innerVlan == null || inport == null) {
385 log.error("Forwarding objective is underspecified: {}", fwd);
386 fail(fwd, ObjectiveError.BADPARAMS);
387 return;
388 }
389
alshabib2f74f2c2016-01-14 13:29:35 -0800390 Criterion innerVid = Criteria.matchVlanId(((VlanIdCriterion) innerVlan).vlanId());
391
alshabibfa0dc662016-01-13 11:23:53 -0800392 FlowRule.Builder outer = DefaultFlowRule.builder()
alshabib56efe432016-02-25 17:57:24 -0500393 .withCookie(counter.getAndIncrement())
alshabibfd430b62015-12-16 18:56:38 -0800394 .forDevice(deviceId)
alshabibfd430b62015-12-16 18:56:38 -0800395 .makePermanent()
396 .withPriority(fwd.priority())
alshabibfa0dc662016-01-13 11:23:53 -0800397 .withSelector(buildSelector(inport, outerVlan))
alshabibfd430b62015-12-16 18:56:38 -0800398 .withTreatment(buildTreatment(popAndRewrite.getLeft(),
399 Instructions.transition(QQ_TABLE)));
alshabibfd430b62015-12-16 18:56:38 -0800400
alshabibfa0dc662016-01-13 11:23:53 -0800401 FlowRule.Builder inner = DefaultFlowRule.builder()
alshabib56efe432016-02-25 17:57:24 -0500402 .withCookie(counter.getAndIncrement())
alshabibfd430b62015-12-16 18:56:38 -0800403 .forDevice(deviceId)
alshabibfd430b62015-12-16 18:56:38 -0800404 .forTable(QQ_TABLE)
405 .makePermanent()
406 .withPriority(fwd.priority())
alshabib2f74f2c2016-01-14 13:29:35 -0800407 .withSelector(buildSelector(inport, innerVid))
alshabibe5075842016-02-04 13:28:33 -0800408 .withTreatment(buildTreatment(popAndRewrite.getLeft(),
alshabibfd430b62015-12-16 18:56:38 -0800409 output));
410
411 applyRules(fwd, inner, outer);
412
413 }
414
415 private void installUpstreamRules(ForwardingObjective fwd) {
416 List<Pair<Instruction, Instruction>> vlanOps =
417 vlanOps(fwd,
418 L2ModificationInstruction.L2SubType.VLAN_PUSH);
419
420 if (vlanOps == null) {
421 return;
422 }
423
424 Instruction output = fetchOutput(fwd, "upstream");
425
426 if (output == null) {
427 return;
428 }
429
430 Pair<Instruction, Instruction> innerPair = vlanOps.remove(0);
431
432 Pair<Instruction, Instruction> outerPair = vlanOps.remove(0);
433
434 FlowRule.Builder inner = DefaultFlowRule.builder()
alshabib56efe432016-02-25 17:57:24 -0500435 .withCookie(counter.getAndIncrement())
alshabibfd430b62015-12-16 18:56:38 -0800436 .forDevice(deviceId)
alshabibfd430b62015-12-16 18:56:38 -0800437 .makePermanent()
438 .withPriority(fwd.priority())
439 .withSelector(fwd.selector())
440 .withTreatment(buildTreatment(innerPair.getRight(),
441 Instructions.transition(QQ_TABLE)));
442
443 PortCriterion inPort = (PortCriterion)
444 fwd.selector().getCriterion(Criterion.Type.IN_PORT);
445
446 VlanId cVlanId = ((L2ModificationInstruction.ModVlanIdInstruction)
447 innerPair.getRight()).vlanId();
448
449 FlowRule.Builder outer = DefaultFlowRule.builder()
alshabib56efe432016-02-25 17:57:24 -0500450 .withCookie(counter.getAndIncrement())
alshabibfd430b62015-12-16 18:56:38 -0800451 .forDevice(deviceId)
alshabibfd430b62015-12-16 18:56:38 -0800452 .forTable(QQ_TABLE)
453 .makePermanent()
454 .withPriority(fwd.priority())
455 .withSelector(buildSelector(inPort,
456 Criteria.matchVlanId(cVlanId)))
457 .withTreatment(buildTreatment(outerPair.getLeft(),
458 outerPair.getRight(),
459 output));
460
461 applyRules(fwd, inner, outer);
462
463 }
464
465 private Instruction fetchOutput(ForwardingObjective fwd, String direction) {
466 Instruction output = fwd.treatment().allInstructions().stream()
467 .filter(i -> i.type() == Instruction.Type.OUTPUT)
468 .findFirst().orElse(null);
469
470 if (output == null) {
471 log.error("OLT {} rule has no output", direction);
472 fail(fwd, ObjectiveError.BADPARAMS);
473 return null;
474 }
475 return output;
476 }
477
478 private List<Pair<Instruction, Instruction>> vlanOps(ForwardingObjective fwd,
479 L2ModificationInstruction.L2SubType type) {
480
481 List<Pair<Instruction, Instruction>> vlanOps = findVlanOps(
482 fwd.treatment().allInstructions(), type);
483
484 if (vlanOps == null) {
485 String direction = type == L2ModificationInstruction.L2SubType.VLAN_POP
486 ? "downstream" : "upstream";
487 log.error("Missing vlan operations in {} forwarding: {}", direction, fwd);
488 fail(fwd, ObjectiveError.BADPARAMS);
489 return null;
490 }
491 return vlanOps;
492 }
493
494
495 private List<Pair<Instruction, Instruction>> findVlanOps(List<Instruction> instructions,
alshabibd61b77b2016-02-01 23:30:53 -0800496 L2ModificationInstruction.L2SubType type) {
alshabibfd430b62015-12-16 18:56:38 -0800497
498 List<Instruction> vlanPushs = findL2Instructions(
499 type,
500 instructions);
501 List<Instruction> vlanSets = findL2Instructions(
502 L2ModificationInstruction.L2SubType.VLAN_ID,
503 instructions);
504
505 if (vlanPushs.size() != vlanSets.size()) {
506 return null;
507 }
508
509 List<Pair<Instruction, Instruction>> pairs = Lists.newArrayList();
510
511 for (int i = 0; i < vlanPushs.size(); i++) {
512 pairs.add(new ImmutablePair<>(vlanPushs.get(i), vlanSets.get(i)));
513 }
514 return pairs;
515 }
516
517 private List<Instruction> findL2Instructions(L2ModificationInstruction.L2SubType subType,
518 List<Instruction> actions) {
519 return actions.stream()
520 .filter(i -> i.type() == Instruction.Type.L2MODIFICATION)
521 .filter(i -> ((L2ModificationInstruction) i).subtype() == subType)
522 .collect(Collectors.toList());
523 }
524
525 private void provisionEapol(FilteringObjective filter,
526 EthTypeCriterion ethType,
527 Instructions.OutputInstruction output) {
528
529 TrafficSelector selector = buildSelector(filter.key(), ethType);
530 TrafficTreatment treatment = buildTreatment(output);
531 buildAndApplyRule(filter, selector, treatment);
532
533 }
534
Jonathan Hart51539b82015-10-29 09:53:04 -0700535 private void provisionIgmp(FilteringObjective filter, EthTypeCriterion ethType,
alshabibfd430b62015-12-16 18:56:38 -0800536 IPProtocolCriterion ipProto,
537 Instructions.OutputInstruction output) {
538 TrafficSelector selector = buildSelector(filter.key(), ethType, ipProto);
539 TrafficTreatment treatment = buildTreatment(output);
540 buildAndApplyRule(filter, selector, treatment);
541 }
542
543 private void buildAndApplyRule(FilteringObjective filter, TrafficSelector selector,
544 TrafficTreatment treatment) {
545 FlowRule rule = DefaultFlowRule.builder()
alshabib56efe432016-02-25 17:57:24 -0500546 .withCookie(counter.getAndIncrement())
alshabibfd430b62015-12-16 18:56:38 -0800547 .forDevice(deviceId)
548 .forTable(0)
alshabibfd430b62015-12-16 18:56:38 -0800549 .makePermanent()
550 .withSelector(selector)
551 .withTreatment(treatment)
alshabibbb424232016-01-15 12:20:25 -0800552 .withPriority(filter.priority())
alshabibfd430b62015-12-16 18:56:38 -0800553 .build();
554
555 FlowRuleOperations.Builder opsBuilder = FlowRuleOperations.builder();
556
557 switch (filter.type()) {
558 case PERMIT:
559 opsBuilder.add(rule);
560 break;
561 case DENY:
562 opsBuilder.remove(rule);
563 break;
564 default:
565 log.warn("Unknown filter type : {}", filter.type());
566 fail(filter, ObjectiveError.UNSUPPORTED);
567 }
568
569 applyFlowRules(opsBuilder, filter);
570 }
571
572 private void applyRules(ForwardingObjective fwd,
573 FlowRule.Builder inner, FlowRule.Builder outer) {
574 FlowRuleOperations.Builder builder = FlowRuleOperations.builder();
575 switch (fwd.op()) {
576 case ADD:
577 builder.add(inner.build()).add(outer.build());
578 break;
579 case REMOVE:
580 builder.remove(inner.build()).remove(outer.build());
581 break;
582 case ADD_TO_EXISTING:
583 break;
584 case REMOVE_FROM_EXISTING:
585 break;
586 default:
587 log.warn("Unknown forwarding operation: {}", fwd.op());
588 }
589
590 applyFlowRules(builder, fwd);
591 }
592
593 private void applyFlowRules(FlowRuleOperations.Builder builder,
594 Objective objective) {
595 flowRuleService.apply(builder.build(new FlowRuleOperationsContext() {
596 @Override
597 public void onSuccess(FlowRuleOperations ops) {
598 pass(objective);
599 }
600
601 @Override
602 public void onError(FlowRuleOperations ops) {
603 fail(objective, ObjectiveError.FLOWINSTALLATIONFAILED);
604 }
605 }));
606 }
607
608 private Criterion filterForCriterion(Collection<Criterion> criteria, Criterion.Type type) {
609 return criteria.stream()
alshabibbb424232016-01-15 12:20:25 -0800610 .filter(c -> c.type().equals(type))
alshabibfd430b62015-12-16 18:56:38 -0800611 .limit(1)
612 .findFirst().orElse(null);
613 }
614
615 private TrafficSelector buildSelector(Criterion... criteria) {
616
617
618 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
619
620 for (Criterion c : criteria) {
621 sBuilder.add(c);
622 }
623
624 return sBuilder.build();
625 }
626
627 private TrafficTreatment buildTreatment(Instruction... instructions) {
628
629
630 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
631
632 for (Instruction i : instructions) {
633 tBuilder.add(i);
634 }
635
636 return tBuilder.build();
637 }
638
639
640 private void fail(Objective obj, ObjectiveError error) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -0800641 obj.context().ifPresent(context -> context.onError(obj, error));
alshabibfd430b62015-12-16 18:56:38 -0800642 }
643
644 private void pass(Objective obj) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -0800645 obj.context().ifPresent(context -> context.onSuccess(obj));
alshabibfd430b62015-12-16 18:56:38 -0800646 }
647
alshabib2cc73cb2015-06-30 20:26:56 -0700648
alshabibd61b77b2016-02-01 23:30:53 -0800649 private class InnerGroupListener implements GroupListener {
alshabib2cc73cb2015-06-30 20:26:56 -0700650 @Override
alshabibd61b77b2016-02-01 23:30:53 -0800651 public void event(GroupEvent event) {
652 if (event.type() == GroupEvent.Type.GROUP_ADDED) {
653 GroupKey key = event.subject().appCookie();
alshabib2cc73cb2015-06-30 20:26:56 -0700654
alshabibd61b77b2016-02-01 23:30:53 -0800655 NextObjective obj = pendingGroups.getIfPresent(key);
656 if (obj != null) {
657 flowObjectiveStore.putNextGroup(obj.id(), new OLTPipelineGroup(key));
658 pass(obj);
659 pendingGroups.invalidate(key);
660 }
661 }
alshabib2cc73cb2015-06-30 20:26:56 -0700662 }
663 }
664
alshabibd61b77b2016-02-01 23:30:53 -0800665 private static class OLTPipelineGroup implements NextGroup {
666
667 private final GroupKey key;
668
669 public OLTPipelineGroup(GroupKey key) {
670 this.key = key;
671 }
672
673 public GroupKey key() {
674 return key;
675 }
676
677 @Override
678 public byte[] data() {
679 return appKryo.serialize(key);
680 }
681
682 }
alshabib0ccde6d2015-05-30 18:22:36 -0700683}