blob: 01395736e20019aa2eeef791776a64789fe24f90 [file] [log] [blame]
alshabib0ccde6d2015-05-30 18:22:36 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present 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;
alshabib5ccbe3f2016-03-02 22:36:02 -080075import org.onosproject.store.service.StorageService;
alshabib0ccde6d2015-05-30 18:22:36 -070076import org.slf4j.Logger;
77
alshabibfd430b62015-12-16 18:56:38 -080078import java.util.Collection;
alshabibd61b77b2016-02-01 23:30:53 -080079import java.util.Collections;
alshabibfd430b62015-12-16 18:56:38 -080080import java.util.List;
81import java.util.Optional;
alshabibd61b77b2016-02-01 23:30:53 -080082import java.util.concurrent.TimeUnit;
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;
alshabib5ccbe3f2016-03-02 22:36:02 -080095 private static final String OLTCOOKIES = "olt-cookies-must-be-unique";
alshabib0ccde6d2015-05-30 18:22:36 -070096 private final Logger log = getLogger(getClass());
97
98 private ServiceDirectory serviceDirectory;
99 private FlowRuleService flowRuleService;
alshabibd61b77b2016-02-01 23:30:53 -0800100 private GroupService groupService;
Jonathan Hartdfc3b862015-07-01 14:49:56 -0700101 private CoreService coreService;
alshabib5ccbe3f2016-03-02 22:36:02 -0800102 private StorageService storageService;
Jonathan Hartdfc3b862015-07-01 14:49:56 -0700103
alshabibd61b77b2016-02-01 23:30:53 -0800104 private DeviceId deviceId;
Jonathan Hartdfc3b862015-07-01 14:49:56 -0700105 private ApplicationId appId;
alshabib83364472016-03-25 09:59:55 -0700106
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();
alshabib5ccbe3f2016-03-02 22:36:02 -0800130 storageService = serviceDirectory.get(StorageService.class);
131
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()
alshabib83364472016-03-25 09:59:55 -0700313 .fromApp(fwd.appId())
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
alshabibb3c14342016-03-04 17:05:01 -0800370 Instructions.OutputInstruction output = (Instructions.OutputInstruction) fetchOutput(fwd, "downstream");
alshabibfd430b62015-12-16 18:56:38 -0800371
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);
alshabibb3c14342016-03-04 17:05:01 -0800383 Criterion bullshit = Criteria.matchMetadata(output.port().toLong());
alshabibfa0dc662016-01-13 11:23:53 -0800384
385 if (outerVlan == null || innerVlan == null || inport == null) {
386 log.error("Forwarding objective is underspecified: {}", fwd);
387 fail(fwd, ObjectiveError.BADPARAMS);
388 return;
389 }
390
alshabib2f74f2c2016-01-14 13:29:35 -0800391 Criterion innerVid = Criteria.matchVlanId(((VlanIdCriterion) innerVlan).vlanId());
392
alshabibfa0dc662016-01-13 11:23:53 -0800393 FlowRule.Builder outer = DefaultFlowRule.builder()
alshabib83364472016-03-25 09:59:55 -0700394 .fromApp(fwd.appId())
alshabibfd430b62015-12-16 18:56:38 -0800395 .forDevice(deviceId)
alshabibfd430b62015-12-16 18:56:38 -0800396 .makePermanent()
397 .withPriority(fwd.priority())
alshabibb3c14342016-03-04 17:05:01 -0800398 .withSelector(buildSelector(inport, outerVlan, bullshit))
alshabibfd430b62015-12-16 18:56:38 -0800399 .withTreatment(buildTreatment(popAndRewrite.getLeft(),
400 Instructions.transition(QQ_TABLE)));
alshabibfd430b62015-12-16 18:56:38 -0800401
alshabibfa0dc662016-01-13 11:23:53 -0800402 FlowRule.Builder inner = DefaultFlowRule.builder()
alshabib83364472016-03-25 09:59:55 -0700403 .fromApp(fwd.appId())
alshabibfd430b62015-12-16 18:56:38 -0800404 .forDevice(deviceId)
alshabibfd430b62015-12-16 18:56:38 -0800405 .forTable(QQ_TABLE)
406 .makePermanent()
407 .withPriority(fwd.priority())
alshabib2f74f2c2016-01-14 13:29:35 -0800408 .withSelector(buildSelector(inport, innerVid))
alshabibe5075842016-02-04 13:28:33 -0800409 .withTreatment(buildTreatment(popAndRewrite.getLeft(),
alshabibfd430b62015-12-16 18:56:38 -0800410 output));
411
412 applyRules(fwd, inner, outer);
413
414 }
415
416 private void installUpstreamRules(ForwardingObjective fwd) {
417 List<Pair<Instruction, Instruction>> vlanOps =
418 vlanOps(fwd,
419 L2ModificationInstruction.L2SubType.VLAN_PUSH);
420
421 if (vlanOps == null) {
422 return;
423 }
424
425 Instruction output = fetchOutput(fwd, "upstream");
426
427 if (output == null) {
428 return;
429 }
430
431 Pair<Instruction, Instruction> innerPair = vlanOps.remove(0);
432
433 Pair<Instruction, Instruction> outerPair = vlanOps.remove(0);
434
435 FlowRule.Builder inner = DefaultFlowRule.builder()
alshabib83364472016-03-25 09:59:55 -0700436 .fromApp(fwd.appId())
alshabibfd430b62015-12-16 18:56:38 -0800437 .forDevice(deviceId)
alshabibfd430b62015-12-16 18:56:38 -0800438 .makePermanent()
439 .withPriority(fwd.priority())
440 .withSelector(fwd.selector())
441 .withTreatment(buildTreatment(innerPair.getRight(),
442 Instructions.transition(QQ_TABLE)));
443
444 PortCriterion inPort = (PortCriterion)
445 fwd.selector().getCriterion(Criterion.Type.IN_PORT);
446
447 VlanId cVlanId = ((L2ModificationInstruction.ModVlanIdInstruction)
448 innerPair.getRight()).vlanId();
449
450 FlowRule.Builder outer = DefaultFlowRule.builder()
alshabib83364472016-03-25 09:59:55 -0700451 .fromApp(fwd.appId())
alshabibfd430b62015-12-16 18:56:38 -0800452 .forDevice(deviceId)
alshabibfd430b62015-12-16 18:56:38 -0800453 .forTable(QQ_TABLE)
454 .makePermanent()
455 .withPriority(fwd.priority())
456 .withSelector(buildSelector(inPort,
457 Criteria.matchVlanId(cVlanId)))
458 .withTreatment(buildTreatment(outerPair.getLeft(),
459 outerPair.getRight(),
460 output));
461
462 applyRules(fwd, inner, outer);
463
464 }
465
466 private Instruction fetchOutput(ForwardingObjective fwd, String direction) {
467 Instruction output = fwd.treatment().allInstructions().stream()
468 .filter(i -> i.type() == Instruction.Type.OUTPUT)
469 .findFirst().orElse(null);
470
471 if (output == null) {
472 log.error("OLT {} rule has no output", direction);
473 fail(fwd, ObjectiveError.BADPARAMS);
474 return null;
475 }
476 return output;
477 }
478
479 private List<Pair<Instruction, Instruction>> vlanOps(ForwardingObjective fwd,
480 L2ModificationInstruction.L2SubType type) {
481
482 List<Pair<Instruction, Instruction>> vlanOps = findVlanOps(
483 fwd.treatment().allInstructions(), type);
484
485 if (vlanOps == null) {
486 String direction = type == L2ModificationInstruction.L2SubType.VLAN_POP
487 ? "downstream" : "upstream";
488 log.error("Missing vlan operations in {} forwarding: {}", direction, fwd);
489 fail(fwd, ObjectiveError.BADPARAMS);
490 return null;
491 }
492 return vlanOps;
493 }
494
495
496 private List<Pair<Instruction, Instruction>> findVlanOps(List<Instruction> instructions,
alshabibd61b77b2016-02-01 23:30:53 -0800497 L2ModificationInstruction.L2SubType type) {
alshabibfd430b62015-12-16 18:56:38 -0800498
499 List<Instruction> vlanPushs = findL2Instructions(
500 type,
501 instructions);
502 List<Instruction> vlanSets = findL2Instructions(
503 L2ModificationInstruction.L2SubType.VLAN_ID,
504 instructions);
505
506 if (vlanPushs.size() != vlanSets.size()) {
507 return null;
508 }
509
510 List<Pair<Instruction, Instruction>> pairs = Lists.newArrayList();
511
512 for (int i = 0; i < vlanPushs.size(); i++) {
513 pairs.add(new ImmutablePair<>(vlanPushs.get(i), vlanSets.get(i)));
514 }
515 return pairs;
516 }
517
518 private List<Instruction> findL2Instructions(L2ModificationInstruction.L2SubType subType,
519 List<Instruction> actions) {
520 return actions.stream()
521 .filter(i -> i.type() == Instruction.Type.L2MODIFICATION)
522 .filter(i -> ((L2ModificationInstruction) i).subtype() == subType)
523 .collect(Collectors.toList());
524 }
525
526 private void provisionEapol(FilteringObjective filter,
527 EthTypeCriterion ethType,
528 Instructions.OutputInstruction output) {
529
530 TrafficSelector selector = buildSelector(filter.key(), ethType);
531 TrafficTreatment treatment = buildTreatment(output);
532 buildAndApplyRule(filter, selector, treatment);
533
534 }
535
Jonathan Hart51539b82015-10-29 09:53:04 -0700536 private void provisionIgmp(FilteringObjective filter, EthTypeCriterion ethType,
alshabibfd430b62015-12-16 18:56:38 -0800537 IPProtocolCriterion ipProto,
538 Instructions.OutputInstruction output) {
539 TrafficSelector selector = buildSelector(filter.key(), ethType, ipProto);
540 TrafficTreatment treatment = buildTreatment(output);
541 buildAndApplyRule(filter, selector, treatment);
542 }
543
544 private void buildAndApplyRule(FilteringObjective filter, TrafficSelector selector,
545 TrafficTreatment treatment) {
546 FlowRule rule = DefaultFlowRule.builder()
alshabib83364472016-03-25 09:59:55 -0700547 .fromApp(filter.appId())
alshabibfd430b62015-12-16 18:56:38 -0800548 .forDevice(deviceId)
549 .forTable(0)
alshabibfd430b62015-12-16 18:56:38 -0800550 .makePermanent()
551 .withSelector(selector)
552 .withTreatment(treatment)
alshabibbb424232016-01-15 12:20:25 -0800553 .withPriority(filter.priority())
alshabibfd430b62015-12-16 18:56:38 -0800554 .build();
555
556 FlowRuleOperations.Builder opsBuilder = FlowRuleOperations.builder();
557
558 switch (filter.type()) {
559 case PERMIT:
560 opsBuilder.add(rule);
561 break;
562 case DENY:
563 opsBuilder.remove(rule);
564 break;
565 default:
566 log.warn("Unknown filter type : {}", filter.type());
567 fail(filter, ObjectiveError.UNSUPPORTED);
568 }
569
570 applyFlowRules(opsBuilder, filter);
571 }
572
573 private void applyRules(ForwardingObjective fwd,
574 FlowRule.Builder inner, FlowRule.Builder outer) {
575 FlowRuleOperations.Builder builder = FlowRuleOperations.builder();
576 switch (fwd.op()) {
577 case ADD:
578 builder.add(inner.build()).add(outer.build());
579 break;
580 case REMOVE:
alshabibb05be2d2016-04-11 12:52:36 -0700581 builder.remove(inner.build()).remove(outer.build());
alshabibfd430b62015-12-16 18:56:38 -0800582 break;
583 case ADD_TO_EXISTING:
584 break;
585 case REMOVE_FROM_EXISTING:
586 break;
587 default:
588 log.warn("Unknown forwarding operation: {}", fwd.op());
589 }
590
591 applyFlowRules(builder, fwd);
592 }
593
594 private void applyFlowRules(FlowRuleOperations.Builder builder,
595 Objective objective) {
596 flowRuleService.apply(builder.build(new FlowRuleOperationsContext() {
597 @Override
598 public void onSuccess(FlowRuleOperations ops) {
599 pass(objective);
600 }
601
602 @Override
603 public void onError(FlowRuleOperations ops) {
604 fail(objective, ObjectiveError.FLOWINSTALLATIONFAILED);
605 }
606 }));
607 }
608
609 private Criterion filterForCriterion(Collection<Criterion> criteria, Criterion.Type type) {
610 return criteria.stream()
alshabibbb424232016-01-15 12:20:25 -0800611 .filter(c -> c.type().equals(type))
alshabibfd430b62015-12-16 18:56:38 -0800612 .limit(1)
613 .findFirst().orElse(null);
614 }
615
616 private TrafficSelector buildSelector(Criterion... criteria) {
617
618
619 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
620
621 for (Criterion c : criteria) {
622 sBuilder.add(c);
623 }
624
625 return sBuilder.build();
626 }
627
628 private TrafficTreatment buildTreatment(Instruction... instructions) {
629
630
631 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
632
633 for (Instruction i : instructions) {
634 tBuilder.add(i);
635 }
636
637 return tBuilder.build();
638 }
639
640
641 private void fail(Objective obj, ObjectiveError error) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -0800642 obj.context().ifPresent(context -> context.onError(obj, error));
alshabibfd430b62015-12-16 18:56:38 -0800643 }
644
645 private void pass(Objective obj) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -0800646 obj.context().ifPresent(context -> context.onSuccess(obj));
alshabibfd430b62015-12-16 18:56:38 -0800647 }
648
alshabib2cc73cb2015-06-30 20:26:56 -0700649
alshabibd61b77b2016-02-01 23:30:53 -0800650 private class InnerGroupListener implements GroupListener {
alshabib2cc73cb2015-06-30 20:26:56 -0700651 @Override
alshabibd61b77b2016-02-01 23:30:53 -0800652 public void event(GroupEvent event) {
653 if (event.type() == GroupEvent.Type.GROUP_ADDED) {
654 GroupKey key = event.subject().appCookie();
alshabib2cc73cb2015-06-30 20:26:56 -0700655
alshabibd61b77b2016-02-01 23:30:53 -0800656 NextObjective obj = pendingGroups.getIfPresent(key);
657 if (obj != null) {
658 flowObjectiveStore.putNextGroup(obj.id(), new OLTPipelineGroup(key));
659 pass(obj);
660 pendingGroups.invalidate(key);
661 }
662 }
alshabib2cc73cb2015-06-30 20:26:56 -0700663 }
664 }
665
alshabibd61b77b2016-02-01 23:30:53 -0800666 private static class OLTPipelineGroup implements NextGroup {
667
668 private final GroupKey key;
669
670 public OLTPipelineGroup(GroupKey key) {
671 this.key = key;
672 }
673
674 public GroupKey key() {
675 return key;
676 }
677
678 @Override
679 public byte[] data() {
680 return appKryo.serialize(key);
681 }
682
683 }
Saurav Das24431192016-03-07 19:13:00 -0800684
685 @Override
686 public List<String> getNextMappings(NextGroup nextGroup) {
687 // TODO Implementation deferred to vendor
688 return null;
689 }
alshabib0ccde6d2015-05-30 18:22:36 -0700690}