blob: f2970847d21665332c97c4b4ffbbcbf0a675c83c [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;
Amit Ghoshcbaf8672016-12-23 21:36:19 +000053import org.onosproject.net.flow.criteria.UdpPortCriterion;
alshabib2f74f2c2016-01-14 13:29:35 -080054import org.onosproject.net.flow.criteria.VlanIdCriterion;
alshabibfd430b62015-12-16 18:56:38 -080055import org.onosproject.net.flow.instructions.Instruction;
alshabib0ccde6d2015-05-30 18:22:36 -070056import org.onosproject.net.flow.instructions.Instructions;
alshabibfd430b62015-12-16 18:56:38 -080057import org.onosproject.net.flow.instructions.L2ModificationInstruction;
alshabib0ccde6d2015-05-30 18:22:36 -070058import org.onosproject.net.flowobjective.FilteringObjective;
alshabibd61b77b2016-02-01 23:30:53 -080059import org.onosproject.net.flowobjective.FlowObjectiveStore;
alshabib0ccde6d2015-05-30 18:22:36 -070060import org.onosproject.net.flowobjective.ForwardingObjective;
61import org.onosproject.net.flowobjective.NextObjective;
alshabibfd430b62015-12-16 18:56:38 -080062import org.onosproject.net.flowobjective.Objective;
alshabib0ccde6d2015-05-30 18:22:36 -070063import org.onosproject.net.flowobjective.ObjectiveError;
alshabibd61b77b2016-02-01 23:30:53 -080064import org.onosproject.net.group.DefaultGroupBucket;
65import org.onosproject.net.group.DefaultGroupDescription;
66import org.onosproject.net.group.DefaultGroupKey;
67import org.onosproject.net.group.Group;
68import org.onosproject.net.group.GroupBucket;
69import org.onosproject.net.group.GroupBuckets;
70import org.onosproject.net.group.GroupDescription;
71import org.onosproject.net.group.GroupEvent;
72import org.onosproject.net.group.GroupKey;
73import org.onosproject.net.group.GroupListener;
74import org.onosproject.net.group.GroupService;
75import org.onosproject.store.serializers.KryoNamespaces;
alshabib5ccbe3f2016-03-02 22:36:02 -080076import org.onosproject.store.service.StorageService;
alshabib0ccde6d2015-05-30 18:22:36 -070077import org.slf4j.Logger;
78
alshabibfd430b62015-12-16 18:56:38 -080079import java.util.Collection;
alshabibd61b77b2016-02-01 23:30:53 -080080import java.util.Collections;
alshabibfd430b62015-12-16 18:56:38 -080081import java.util.List;
82import java.util.Optional;
alshabibd61b77b2016-02-01 23:30:53 -080083import java.util.concurrent.TimeUnit;
alshabibfd430b62015-12-16 18:56:38 -080084import java.util.stream.Collectors;
85
alshabib0ccde6d2015-05-30 18:22:36 -070086import static org.slf4j.LoggerFactory.getLogger;
87
88/**
Jonathan Hart64da69d2015-07-15 15:10:28 -070089 * Pipeliner for OLT device.
alshabib0ccde6d2015-05-30 18:22:36 -070090 */
alshabibfd430b62015-12-16 18:56:38 -080091
Jonathan Hartb92cc512015-11-16 23:05:21 -080092public class OltPipeline extends AbstractHandlerBehaviour implements Pipeliner {
alshabib0ccde6d2015-05-30 18:22:36 -070093
alshabibfd430b62015-12-16 18:56:38 -080094 private static final Integer QQ_TABLE = 1;
alshabibd61b77b2016-02-01 23:30:53 -080095 private static final short MCAST_VLAN = 4000;
alshabib5ccbe3f2016-03-02 22:36:02 -080096 private static final String OLTCOOKIES = "olt-cookies-must-be-unique";
alshabib0ccde6d2015-05-30 18:22:36 -070097 private final Logger log = getLogger(getClass());
98
99 private ServiceDirectory serviceDirectory;
100 private FlowRuleService flowRuleService;
alshabibd61b77b2016-02-01 23:30:53 -0800101 private GroupService groupService;
Jonathan Hartdfc3b862015-07-01 14:49:56 -0700102 private CoreService coreService;
alshabib5ccbe3f2016-03-02 22:36:02 -0800103 private StorageService storageService;
Jonathan Hartdfc3b862015-07-01 14:49:56 -0700104
alshabibd61b77b2016-02-01 23:30:53 -0800105 private DeviceId deviceId;
Jonathan Hartdfc3b862015-07-01 14:49:56 -0700106 private ApplicationId appId;
alshabib83364472016-03-25 09:59:55 -0700107
alshabib0ccde6d2015-05-30 18:22:36 -0700108
alshabibd61b77b2016-02-01 23:30:53 -0800109 protected FlowObjectiveStore flowObjectiveStore;
110
111 private Cache<GroupKey, NextObjective> pendingGroups;
112
113 protected static KryoNamespace appKryo = new KryoNamespace.Builder()
114 .register(KryoNamespaces.API)
115 .register(GroupKey.class)
116 .register(DefaultGroupKey.class)
117 .register(OLTPipelineGroup.class)
Charles Chaneefdedf2016-05-23 16:45:45 -0700118 .build("OltPipeline");
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);
Amit Ghoshcbaf8672016-12-23 21:36:19 +0000188 } else if (ipProto.protocol() == IPv4.PROTOCOL_UDP) {
189 UdpPortCriterion udpSrcPort = (UdpPortCriterion)
190 filterForCriterion(filter.conditions(), Criterion.Type.UDP_SRC);
191
192 UdpPortCriterion udpDstPort = (UdpPortCriterion)
193 filterForCriterion(filter.conditions(), Criterion.Type.UDP_DST);
194
195 if (udpSrcPort.udpPort().toInt() != 68 || udpDstPort.udpPort().toInt() != 67) {
196 log.error("OLT can only filte DHCP, wrong UDP Src or Dst Port");
197 fail(filter, ObjectiveError.UNSUPPORTED);
198 }
199 provisionDhcp(filter, ethType, ipProto, udpSrcPort, udpDstPort, output);
alshabibbb424232016-01-15 12:20:25 -0800200 } else {
Amit Ghoshcbaf8672016-12-23 21:36:19 +0000201 log.error("OLT can only filter igmp and DHCP");
alshabibbb424232016-01-15 12:20:25 -0800202 fail(filter, ObjectiveError.UNSUPPORTED);
alshabibfd430b62015-12-16 18:56:38 -0800203 }
204 } else {
Amit Ghoshcbaf8672016-12-23 21:36:19 +0000205 log.error("OLT can only filter eapol igmp");
alshabibfd430b62015-12-16 18:56:38 -0800206 fail(filter, ObjectiveError.UNSUPPORTED);
207 }
208
209 }
210
211
212 @Override
213 public void forward(ForwardingObjective fwd) {
alshabibd61b77b2016-02-01 23:30:53 -0800214
215 if (checkForMulticast(fwd)) {
216 processMulticastRule(fwd);
217 return;
218 }
219
alshabib0ccde6d2015-05-30 18:22:36 -0700220 TrafficTreatment treatment = fwd.treatment();
alshabib0ccde6d2015-05-30 18:22:36 -0700221
alshabibfd430b62015-12-16 18:56:38 -0800222 List<Instruction> instructions = treatment.allInstructions();
alshabib0ccde6d2015-05-30 18:22:36 -0700223
alshabibfd430b62015-12-16 18:56:38 -0800224 Optional<Instruction> vlanIntruction = instructions.stream()
225 .filter(i -> i.type() == Instruction.Type.L2MODIFICATION)
226 .filter(i -> ((L2ModificationInstruction) i).subtype() ==
227 L2ModificationInstruction.L2SubType.VLAN_PUSH ||
228 ((L2ModificationInstruction) i).subtype() ==
229 L2ModificationInstruction.L2SubType.VLAN_POP)
230 .findAny();
231
232 if (!vlanIntruction.isPresent()) {
233 fail(fwd, ObjectiveError.BADPARAMS);
234 return;
235 }
236
237 L2ModificationInstruction vlanIns =
238 (L2ModificationInstruction) vlanIntruction.get();
239
240 if (vlanIns.subtype() == L2ModificationInstruction.L2SubType.VLAN_PUSH) {
241 installUpstreamRules(fwd);
242 } else if (vlanIns.subtype() == L2ModificationInstruction.L2SubType.VLAN_POP) {
243 installDownstreamRules(fwd);
alshabib0ccde6d2015-05-30 18:22:36 -0700244 } else {
alshabibfd430b62015-12-16 18:56:38 -0800245 log.error("Unknown OLT operation: {}", fwd);
246 fail(fwd, ObjectiveError.UNSUPPORTED);
247 return;
alshabib0ccde6d2015-05-30 18:22:36 -0700248 }
249
alshabibfd430b62015-12-16 18:56:38 -0800250 pass(fwd);
alshabib0ccde6d2015-05-30 18:22:36 -0700251
alshabib0ccde6d2015-05-30 18:22:36 -0700252 }
253
alshabibd61b77b2016-02-01 23:30:53 -0800254
alshabib0ccde6d2015-05-30 18:22:36 -0700255 @Override
256 public void next(NextObjective nextObjective) {
alshabibd61b77b2016-02-01 23:30:53 -0800257 if (nextObjective.type() != NextObjective.Type.BROADCAST) {
258 log.error("OLT only supports broadcast groups.");
259 fail(nextObjective, ObjectiveError.BADPARAMS);
260 }
261
262 if (nextObjective.next().size() != 1) {
263 log.error("OLT only supports singleton broadcast groups.");
264 fail(nextObjective, ObjectiveError.BADPARAMS);
265 }
266
267 TrafficTreatment treatment = nextObjective.next().stream().findFirst().get();
268
269
270 GroupBucket bucket = DefaultGroupBucket.createAllGroupBucket(treatment);
271 GroupKey key = new DefaultGroupKey(appKryo.serialize(nextObjective.id()));
272
alshabib1aa58142016-02-17 15:37:56 -0800273
alshabibd61b77b2016-02-01 23:30:53 -0800274 pendingGroups.put(key, nextObjective);
275
276 switch (nextObjective.op()) {
277 case ADD:
alshabib1aa58142016-02-17 15:37:56 -0800278 GroupDescription groupDesc =
279 new DefaultGroupDescription(deviceId,
280 GroupDescription.Type.ALL,
281 new GroupBuckets(Collections.singletonList(bucket)),
282 key,
283 null,
284 nextObjective.appId());
alshabibd61b77b2016-02-01 23:30:53 -0800285 groupService.addGroup(groupDesc);
286 break;
287 case REMOVE:
288 groupService.removeGroup(deviceId, key, nextObjective.appId());
289 break;
290 case ADD_TO_EXISTING:
alshabib1aa58142016-02-17 15:37:56 -0800291 groupService.addBucketsToGroup(deviceId, key,
292 new GroupBuckets(Collections.singletonList(bucket)),
293 key, nextObjective.appId());
294 break;
alshabibd61b77b2016-02-01 23:30:53 -0800295 case REMOVE_FROM_EXISTING:
alshabib1aa58142016-02-17 15:37:56 -0800296 groupService.removeBucketsFromGroup(deviceId, key,
alshabib56efe432016-02-25 17:57:24 -0500297 new GroupBuckets(Collections.singletonList(bucket)),
298 key, nextObjective.appId());
alshabibd61b77b2016-02-01 23:30:53 -0800299 break;
300 default:
301 log.warn("Unknown next objective operation: {}", nextObjective.op());
302 }
303
304
305 }
306
307 private void processMulticastRule(ForwardingObjective fwd) {
308 if (fwd.nextId() == null) {
309 log.error("Multicast objective does not have a next id");
310 fail(fwd, ObjectiveError.BADPARAMS);
311 }
312
alshabib1aa58142016-02-17 15:37:56 -0800313 GroupKey key = getGroupForNextObjective(fwd.nextId());
alshabibd61b77b2016-02-01 23:30:53 -0800314
alshabib1aa58142016-02-17 15:37:56 -0800315 if (key == null) {
alshabibd61b77b2016-02-01 23:30:53 -0800316 log.error("Group for forwarding objective missing: {}", fwd);
317 fail(fwd, ObjectiveError.GROUPMISSING);
318 }
319
alshabib1aa58142016-02-17 15:37:56 -0800320 Group group = groupService.getGroup(deviceId, key);
alshabibd61b77b2016-02-01 23:30:53 -0800321 TrafficTreatment treatment =
322 buildTreatment(Instructions.createGroup(group.id()));
323
324 FlowRule rule = DefaultFlowRule.builder()
alshabib83364472016-03-25 09:59:55 -0700325 .fromApp(fwd.appId())
alshabibd61b77b2016-02-01 23:30:53 -0800326 .forDevice(deviceId)
327 .forTable(0)
alshabibd61b77b2016-02-01 23:30:53 -0800328 .makePermanent()
329 .withPriority(fwd.priority())
330 .withSelector(fwd.selector())
331 .withTreatment(treatment)
332 .build();
333
334 FlowRuleOperations.Builder builder = FlowRuleOperations.builder();
335 switch (fwd.op()) {
336
337 case ADD:
338 builder.add(rule);
339 break;
340 case REMOVE:
341 builder.remove(rule);
342 break;
343 case ADD_TO_EXISTING:
344 case REMOVE_FROM_EXISTING:
345 break;
346 default:
347 log.warn("Unknown forwarding operation: {}", fwd.op());
348 }
349
350 applyFlowRules(builder, fwd);
351
352 }
353
354 private boolean checkForMulticast(ForwardingObjective fwd) {
355
alshabib1aa58142016-02-17 15:37:56 -0800356 IPCriterion ip = (IPCriterion) filterForCriterion(fwd.selector().criteria(),
alshabib56efe432016-02-25 17:57:24 -0500357 Criterion.Type.IPV4_DST);
alshabibd61b77b2016-02-01 23:30:53 -0800358
alshabib1aa58142016-02-17 15:37:56 -0800359 if (ip == null) {
360 return false;
361 }
362
Charles Chanaedabfd2016-02-26 09:31:48 -0800363 return ip.ip().isMulticast();
alshabibd61b77b2016-02-01 23:30:53 -0800364
365 }
366
alshabib1aa58142016-02-17 15:37:56 -0800367 private GroupKey getGroupForNextObjective(Integer nextId) {
alshabibd61b77b2016-02-01 23:30:53 -0800368 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
alshabib1aa58142016-02-17 15:37:56 -0800369 return appKryo.deserialize(next.data());
alshabibd61b77b2016-02-01 23:30:53 -0800370
alshabib0ccde6d2015-05-30 18:22:36 -0700371 }
372
alshabibfd430b62015-12-16 18:56:38 -0800373 private void installDownstreamRules(ForwardingObjective fwd) {
374 List<Pair<Instruction, Instruction>> vlanOps =
375 vlanOps(fwd,
376 L2ModificationInstruction.L2SubType.VLAN_POP);
377
378 if (vlanOps == null) {
379 return;
380 }
381
alshabibb3c14342016-03-04 17:05:01 -0800382 Instructions.OutputInstruction output = (Instructions.OutputInstruction) fetchOutput(fwd, "downstream");
alshabibfd430b62015-12-16 18:56:38 -0800383
384 if (output == null) {
385 return;
386 }
387
388 Pair<Instruction, Instruction> popAndRewrite = vlanOps.remove(0);
389
alshabibfa0dc662016-01-13 11:23:53 -0800390 TrafficSelector selector = fwd.selector();
391
392 Criterion outerVlan = selector.getCriterion(Criterion.Type.VLAN_VID);
393 Criterion innerVlan = selector.getCriterion(Criterion.Type.INNER_VLAN_VID);
394 Criterion inport = selector.getCriterion(Criterion.Type.IN_PORT);
alshabibb3c14342016-03-04 17:05:01 -0800395 Criterion bullshit = Criteria.matchMetadata(output.port().toLong());
alshabibfa0dc662016-01-13 11:23:53 -0800396
397 if (outerVlan == null || innerVlan == null || inport == null) {
398 log.error("Forwarding objective is underspecified: {}", fwd);
399 fail(fwd, ObjectiveError.BADPARAMS);
400 return;
401 }
402
alshabib2f74f2c2016-01-14 13:29:35 -0800403 Criterion innerVid = Criteria.matchVlanId(((VlanIdCriterion) innerVlan).vlanId());
404
alshabibfa0dc662016-01-13 11:23:53 -0800405 FlowRule.Builder outer = DefaultFlowRule.builder()
alshabib83364472016-03-25 09:59:55 -0700406 .fromApp(fwd.appId())
alshabibfd430b62015-12-16 18:56:38 -0800407 .forDevice(deviceId)
alshabibfd430b62015-12-16 18:56:38 -0800408 .makePermanent()
409 .withPriority(fwd.priority())
alshabibb3c14342016-03-04 17:05:01 -0800410 .withSelector(buildSelector(inport, outerVlan, bullshit))
alshabibfd430b62015-12-16 18:56:38 -0800411 .withTreatment(buildTreatment(popAndRewrite.getLeft(),
412 Instructions.transition(QQ_TABLE)));
alshabibfd430b62015-12-16 18:56:38 -0800413
alshabibfa0dc662016-01-13 11:23:53 -0800414 FlowRule.Builder inner = DefaultFlowRule.builder()
alshabib83364472016-03-25 09:59:55 -0700415 .fromApp(fwd.appId())
alshabibfd430b62015-12-16 18:56:38 -0800416 .forDevice(deviceId)
alshabibfd430b62015-12-16 18:56:38 -0800417 .forTable(QQ_TABLE)
418 .makePermanent()
419 .withPriority(fwd.priority())
alshabib2f74f2c2016-01-14 13:29:35 -0800420 .withSelector(buildSelector(inport, innerVid))
alshabibe5075842016-02-04 13:28:33 -0800421 .withTreatment(buildTreatment(popAndRewrite.getLeft(),
alshabibfd430b62015-12-16 18:56:38 -0800422 output));
423
424 applyRules(fwd, inner, outer);
425
426 }
427
428 private void installUpstreamRules(ForwardingObjective fwd) {
429 List<Pair<Instruction, Instruction>> vlanOps =
430 vlanOps(fwd,
431 L2ModificationInstruction.L2SubType.VLAN_PUSH);
432
433 if (vlanOps == null) {
434 return;
435 }
436
437 Instruction output = fetchOutput(fwd, "upstream");
438
439 if (output == null) {
440 return;
441 }
442
443 Pair<Instruction, Instruction> innerPair = vlanOps.remove(0);
444
445 Pair<Instruction, Instruction> outerPair = vlanOps.remove(0);
446
447 FlowRule.Builder inner = DefaultFlowRule.builder()
alshabib83364472016-03-25 09:59:55 -0700448 .fromApp(fwd.appId())
alshabibfd430b62015-12-16 18:56:38 -0800449 .forDevice(deviceId)
alshabibfd430b62015-12-16 18:56:38 -0800450 .makePermanent()
451 .withPriority(fwd.priority())
452 .withSelector(fwd.selector())
453 .withTreatment(buildTreatment(innerPair.getRight(),
454 Instructions.transition(QQ_TABLE)));
455
456 PortCriterion inPort = (PortCriterion)
457 fwd.selector().getCriterion(Criterion.Type.IN_PORT);
458
459 VlanId cVlanId = ((L2ModificationInstruction.ModVlanIdInstruction)
460 innerPair.getRight()).vlanId();
461
462 FlowRule.Builder outer = DefaultFlowRule.builder()
alshabib83364472016-03-25 09:59:55 -0700463 .fromApp(fwd.appId())
alshabibfd430b62015-12-16 18:56:38 -0800464 .forDevice(deviceId)
alshabibfd430b62015-12-16 18:56:38 -0800465 .forTable(QQ_TABLE)
466 .makePermanent()
467 .withPriority(fwd.priority())
468 .withSelector(buildSelector(inPort,
469 Criteria.matchVlanId(cVlanId)))
470 .withTreatment(buildTreatment(outerPair.getLeft(),
471 outerPair.getRight(),
472 output));
473
474 applyRules(fwd, inner, outer);
475
476 }
477
478 private Instruction fetchOutput(ForwardingObjective fwd, String direction) {
479 Instruction output = fwd.treatment().allInstructions().stream()
480 .filter(i -> i.type() == Instruction.Type.OUTPUT)
481 .findFirst().orElse(null);
482
483 if (output == null) {
484 log.error("OLT {} rule has no output", direction);
485 fail(fwd, ObjectiveError.BADPARAMS);
486 return null;
487 }
488 return output;
489 }
490
491 private List<Pair<Instruction, Instruction>> vlanOps(ForwardingObjective fwd,
492 L2ModificationInstruction.L2SubType type) {
493
494 List<Pair<Instruction, Instruction>> vlanOps = findVlanOps(
495 fwd.treatment().allInstructions(), type);
496
497 if (vlanOps == null) {
498 String direction = type == L2ModificationInstruction.L2SubType.VLAN_POP
499 ? "downstream" : "upstream";
500 log.error("Missing vlan operations in {} forwarding: {}", direction, fwd);
501 fail(fwd, ObjectiveError.BADPARAMS);
502 return null;
503 }
504 return vlanOps;
505 }
506
507
508 private List<Pair<Instruction, Instruction>> findVlanOps(List<Instruction> instructions,
alshabibd61b77b2016-02-01 23:30:53 -0800509 L2ModificationInstruction.L2SubType type) {
alshabibfd430b62015-12-16 18:56:38 -0800510
511 List<Instruction> vlanPushs = findL2Instructions(
512 type,
513 instructions);
514 List<Instruction> vlanSets = findL2Instructions(
515 L2ModificationInstruction.L2SubType.VLAN_ID,
516 instructions);
517
518 if (vlanPushs.size() != vlanSets.size()) {
519 return null;
520 }
521
522 List<Pair<Instruction, Instruction>> pairs = Lists.newArrayList();
523
524 for (int i = 0; i < vlanPushs.size(); i++) {
525 pairs.add(new ImmutablePair<>(vlanPushs.get(i), vlanSets.get(i)));
526 }
527 return pairs;
528 }
529
530 private List<Instruction> findL2Instructions(L2ModificationInstruction.L2SubType subType,
531 List<Instruction> actions) {
532 return actions.stream()
533 .filter(i -> i.type() == Instruction.Type.L2MODIFICATION)
534 .filter(i -> ((L2ModificationInstruction) i).subtype() == subType)
535 .collect(Collectors.toList());
536 }
537
538 private void provisionEapol(FilteringObjective filter,
539 EthTypeCriterion ethType,
540 Instructions.OutputInstruction output) {
541
542 TrafficSelector selector = buildSelector(filter.key(), ethType);
543 TrafficTreatment treatment = buildTreatment(output);
544 buildAndApplyRule(filter, selector, treatment);
545
546 }
547
Jonathan Hart51539b82015-10-29 09:53:04 -0700548 private void provisionIgmp(FilteringObjective filter, EthTypeCriterion ethType,
alshabibfd430b62015-12-16 18:56:38 -0800549 IPProtocolCriterion ipProto,
550 Instructions.OutputInstruction output) {
551 TrafficSelector selector = buildSelector(filter.key(), ethType, ipProto);
552 TrafficTreatment treatment = buildTreatment(output);
553 buildAndApplyRule(filter, selector, treatment);
554 }
555
Amit Ghoshcbaf8672016-12-23 21:36:19 +0000556 private void provisionDhcp(FilteringObjective filter, EthTypeCriterion ethType,
557 IPProtocolCriterion ipProto,
558 UdpPortCriterion udpSrcPort,
559 UdpPortCriterion udpDstPort,
560 Instructions.OutputInstruction output) {
561 TrafficSelector selector = buildSelector(filter.key(), ethType, ipProto, udpSrcPort, udpDstPort);
562 TrafficTreatment treatment = buildTreatment(output);
563 buildAndApplyRule(filter, selector, treatment);
564 }
alshabibfd430b62015-12-16 18:56:38 -0800565 private void buildAndApplyRule(FilteringObjective filter, TrafficSelector selector,
566 TrafficTreatment treatment) {
567 FlowRule rule = DefaultFlowRule.builder()
alshabib83364472016-03-25 09:59:55 -0700568 .fromApp(filter.appId())
alshabibfd430b62015-12-16 18:56:38 -0800569 .forDevice(deviceId)
570 .forTable(0)
alshabibfd430b62015-12-16 18:56:38 -0800571 .makePermanent()
572 .withSelector(selector)
573 .withTreatment(treatment)
alshabibbb424232016-01-15 12:20:25 -0800574 .withPriority(filter.priority())
alshabibfd430b62015-12-16 18:56:38 -0800575 .build();
576
577 FlowRuleOperations.Builder opsBuilder = FlowRuleOperations.builder();
578
579 switch (filter.type()) {
580 case PERMIT:
581 opsBuilder.add(rule);
582 break;
583 case DENY:
584 opsBuilder.remove(rule);
585 break;
586 default:
587 log.warn("Unknown filter type : {}", filter.type());
588 fail(filter, ObjectiveError.UNSUPPORTED);
589 }
590
591 applyFlowRules(opsBuilder, filter);
592 }
593
594 private void applyRules(ForwardingObjective fwd,
595 FlowRule.Builder inner, FlowRule.Builder outer) {
596 FlowRuleOperations.Builder builder = FlowRuleOperations.builder();
597 switch (fwd.op()) {
598 case ADD:
599 builder.add(inner.build()).add(outer.build());
600 break;
601 case REMOVE:
alshabibb05be2d2016-04-11 12:52:36 -0700602 builder.remove(inner.build()).remove(outer.build());
alshabibfd430b62015-12-16 18:56:38 -0800603 break;
604 case ADD_TO_EXISTING:
605 break;
606 case REMOVE_FROM_EXISTING:
607 break;
608 default:
609 log.warn("Unknown forwarding operation: {}", fwd.op());
610 }
611
612 applyFlowRules(builder, fwd);
613 }
614
615 private void applyFlowRules(FlowRuleOperations.Builder builder,
616 Objective objective) {
617 flowRuleService.apply(builder.build(new FlowRuleOperationsContext() {
618 @Override
619 public void onSuccess(FlowRuleOperations ops) {
620 pass(objective);
621 }
622
623 @Override
624 public void onError(FlowRuleOperations ops) {
625 fail(objective, ObjectiveError.FLOWINSTALLATIONFAILED);
626 }
627 }));
628 }
629
630 private Criterion filterForCriterion(Collection<Criterion> criteria, Criterion.Type type) {
631 return criteria.stream()
alshabibbb424232016-01-15 12:20:25 -0800632 .filter(c -> c.type().equals(type))
alshabibfd430b62015-12-16 18:56:38 -0800633 .limit(1)
634 .findFirst().orElse(null);
635 }
636
637 private TrafficSelector buildSelector(Criterion... criteria) {
638
639
640 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
641
642 for (Criterion c : criteria) {
643 sBuilder.add(c);
644 }
645
646 return sBuilder.build();
647 }
648
649 private TrafficTreatment buildTreatment(Instruction... instructions) {
650
651
652 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
653
654 for (Instruction i : instructions) {
655 tBuilder.add(i);
656 }
657
658 return tBuilder.build();
659 }
660
661
662 private void fail(Objective obj, ObjectiveError error) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -0800663 obj.context().ifPresent(context -> context.onError(obj, error));
alshabibfd430b62015-12-16 18:56:38 -0800664 }
665
666 private void pass(Objective obj) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -0800667 obj.context().ifPresent(context -> context.onSuccess(obj));
alshabibfd430b62015-12-16 18:56:38 -0800668 }
669
alshabib2cc73cb2015-06-30 20:26:56 -0700670
alshabibd61b77b2016-02-01 23:30:53 -0800671 private class InnerGroupListener implements GroupListener {
alshabib2cc73cb2015-06-30 20:26:56 -0700672 @Override
alshabibd61b77b2016-02-01 23:30:53 -0800673 public void event(GroupEvent event) {
ke hanf5086672016-08-12 11:09:17 +0800674 if (event.type() == GroupEvent.Type.GROUP_ADDED ||
675 event.type() == GroupEvent.Type.GROUP_UPDATED) {
alshabibd61b77b2016-02-01 23:30:53 -0800676 GroupKey key = event.subject().appCookie();
alshabib2cc73cb2015-06-30 20:26:56 -0700677
alshabibd61b77b2016-02-01 23:30:53 -0800678 NextObjective obj = pendingGroups.getIfPresent(key);
679 if (obj != null) {
680 flowObjectiveStore.putNextGroup(obj.id(), new OLTPipelineGroup(key));
681 pass(obj);
682 pendingGroups.invalidate(key);
683 }
684 }
alshabib2cc73cb2015-06-30 20:26:56 -0700685 }
686 }
687
alshabibd61b77b2016-02-01 23:30:53 -0800688 private static class OLTPipelineGroup implements NextGroup {
689
690 private final GroupKey key;
691
692 public OLTPipelineGroup(GroupKey key) {
693 this.key = key;
694 }
695
696 public GroupKey key() {
697 return key;
698 }
699
700 @Override
701 public byte[] data() {
702 return appKryo.serialize(key);
703 }
704
705 }
Saurav Das24431192016-03-07 19:13:00 -0800706
707 @Override
708 public List<String> getNextMappings(NextGroup nextGroup) {
709 // TODO Implementation deferred to vendor
710 return null;
711 }
alshabib0ccde6d2015-05-30 18:22:36 -0700712}