blob: b995c2bfccf1afb41ecd19f7663154c86f88f82c [file] [log] [blame]
alshabib0ccde6d2015-05-30 18:22:36 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
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)) {
dvaddire8e6b89a2017-08-31 21:54:03 +0530159 log.warn("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);
dvaddire8e6b89a2017-08-31 21:54:03 +0530186 if (ipProto == null) {
187 log.warn("OLT can only filter IGMP and DHCP");
188 fail(filter, ObjectiveError.UNSUPPORTED);
189 return;
190 }
alshabibfd430b62015-12-16 18:56:38 -0800191 if (ipProto.protocol() == IPv4.PROTOCOL_IGMP) {
Jonathan Hart51539b82015-10-29 09:53:04 -0700192 provisionIgmp(filter, ethType, ipProto, output);
Amit Ghoshcbaf8672016-12-23 21:36:19 +0000193 } else if (ipProto.protocol() == IPv4.PROTOCOL_UDP) {
194 UdpPortCriterion udpSrcPort = (UdpPortCriterion)
195 filterForCriterion(filter.conditions(), Criterion.Type.UDP_SRC);
196
197 UdpPortCriterion udpDstPort = (UdpPortCriterion)
198 filterForCriterion(filter.conditions(), Criterion.Type.UDP_DST);
199
200 if (udpSrcPort.udpPort().toInt() != 68 || udpDstPort.udpPort().toInt() != 67) {
dvaddire8e6b89a2017-08-31 21:54:03 +0530201 log.warn("OLT can only filter DHCP, wrong UDP Src or Dst Port");
Amit Ghoshcbaf8672016-12-23 21:36:19 +0000202 fail(filter, ObjectiveError.UNSUPPORTED);
203 }
204 provisionDhcp(filter, ethType, ipProto, udpSrcPort, udpDstPort, output);
alshabibbb424232016-01-15 12:20:25 -0800205 } else {
dvaddire8e6b89a2017-08-31 21:54:03 +0530206 log.warn("OLT can only filter IGMP and DHCP");
alshabibbb424232016-01-15 12:20:25 -0800207 fail(filter, ObjectiveError.UNSUPPORTED);
alshabibfd430b62015-12-16 18:56:38 -0800208 }
209 } else {
dvaddire8e6b89a2017-08-31 21:54:03 +0530210 log.warn("\nOnly the following are Supported in OLT for filter ->\n"
211 + "ETH TYPE : EAPOL and IPV4\n"
212 + "IPV4 TYPE: IGMP and UDP (for DHCP)");
alshabibfd430b62015-12-16 18:56:38 -0800213 fail(filter, ObjectiveError.UNSUPPORTED);
214 }
215
216 }
217
218
219 @Override
220 public void forward(ForwardingObjective fwd) {
alshabibd61b77b2016-02-01 23:30:53 -0800221
222 if (checkForMulticast(fwd)) {
223 processMulticastRule(fwd);
224 return;
225 }
226
alshabib0ccde6d2015-05-30 18:22:36 -0700227 TrafficTreatment treatment = fwd.treatment();
alshabib0ccde6d2015-05-30 18:22:36 -0700228
alshabibfd430b62015-12-16 18:56:38 -0800229 List<Instruction> instructions = treatment.allInstructions();
alshabib0ccde6d2015-05-30 18:22:36 -0700230
alshabibfd430b62015-12-16 18:56:38 -0800231 Optional<Instruction> vlanIntruction = instructions.stream()
232 .filter(i -> i.type() == Instruction.Type.L2MODIFICATION)
233 .filter(i -> ((L2ModificationInstruction) i).subtype() ==
234 L2ModificationInstruction.L2SubType.VLAN_PUSH ||
235 ((L2ModificationInstruction) i).subtype() ==
236 L2ModificationInstruction.L2SubType.VLAN_POP)
237 .findAny();
238
239 if (!vlanIntruction.isPresent()) {
240 fail(fwd, ObjectiveError.BADPARAMS);
241 return;
242 }
243
244 L2ModificationInstruction vlanIns =
245 (L2ModificationInstruction) vlanIntruction.get();
246
247 if (vlanIns.subtype() == L2ModificationInstruction.L2SubType.VLAN_PUSH) {
248 installUpstreamRules(fwd);
249 } else if (vlanIns.subtype() == L2ModificationInstruction.L2SubType.VLAN_POP) {
250 installDownstreamRules(fwd);
alshabib0ccde6d2015-05-30 18:22:36 -0700251 } else {
alshabibfd430b62015-12-16 18:56:38 -0800252 log.error("Unknown OLT operation: {}", fwd);
253 fail(fwd, ObjectiveError.UNSUPPORTED);
254 return;
alshabib0ccde6d2015-05-30 18:22:36 -0700255 }
256
alshabibfd430b62015-12-16 18:56:38 -0800257 pass(fwd);
alshabib0ccde6d2015-05-30 18:22:36 -0700258
alshabib0ccde6d2015-05-30 18:22:36 -0700259 }
260
alshabibd61b77b2016-02-01 23:30:53 -0800261
alshabib0ccde6d2015-05-30 18:22:36 -0700262 @Override
263 public void next(NextObjective nextObjective) {
alshabibd61b77b2016-02-01 23:30:53 -0800264 if (nextObjective.type() != NextObjective.Type.BROADCAST) {
265 log.error("OLT only supports broadcast groups.");
266 fail(nextObjective, ObjectiveError.BADPARAMS);
267 }
268
269 if (nextObjective.next().size() != 1) {
270 log.error("OLT only supports singleton broadcast groups.");
271 fail(nextObjective, ObjectiveError.BADPARAMS);
272 }
273
274 TrafficTreatment treatment = nextObjective.next().stream().findFirst().get();
275
276
277 GroupBucket bucket = DefaultGroupBucket.createAllGroupBucket(treatment);
278 GroupKey key = new DefaultGroupKey(appKryo.serialize(nextObjective.id()));
279
alshabib1aa58142016-02-17 15:37:56 -0800280
alshabibd61b77b2016-02-01 23:30:53 -0800281 pendingGroups.put(key, nextObjective);
282
283 switch (nextObjective.op()) {
284 case ADD:
alshabib1aa58142016-02-17 15:37:56 -0800285 GroupDescription groupDesc =
286 new DefaultGroupDescription(deviceId,
287 GroupDescription.Type.ALL,
288 new GroupBuckets(Collections.singletonList(bucket)),
289 key,
290 null,
291 nextObjective.appId());
alshabibd61b77b2016-02-01 23:30:53 -0800292 groupService.addGroup(groupDesc);
293 break;
294 case REMOVE:
295 groupService.removeGroup(deviceId, key, nextObjective.appId());
296 break;
297 case ADD_TO_EXISTING:
alshabib1aa58142016-02-17 15:37:56 -0800298 groupService.addBucketsToGroup(deviceId, key,
299 new GroupBuckets(Collections.singletonList(bucket)),
300 key, nextObjective.appId());
301 break;
alshabibd61b77b2016-02-01 23:30:53 -0800302 case REMOVE_FROM_EXISTING:
alshabib1aa58142016-02-17 15:37:56 -0800303 groupService.removeBucketsFromGroup(deviceId, key,
alshabib56efe432016-02-25 17:57:24 -0500304 new GroupBuckets(Collections.singletonList(bucket)),
305 key, nextObjective.appId());
alshabibd61b77b2016-02-01 23:30:53 -0800306 break;
307 default:
308 log.warn("Unknown next objective operation: {}", nextObjective.op());
309 }
310
311
312 }
313
314 private void processMulticastRule(ForwardingObjective fwd) {
315 if (fwd.nextId() == null) {
316 log.error("Multicast objective does not have a next id");
317 fail(fwd, ObjectiveError.BADPARAMS);
318 }
319
alshabib1aa58142016-02-17 15:37:56 -0800320 GroupKey key = getGroupForNextObjective(fwd.nextId());
alshabibd61b77b2016-02-01 23:30:53 -0800321
alshabib1aa58142016-02-17 15:37:56 -0800322 if (key == null) {
alshabibd61b77b2016-02-01 23:30:53 -0800323 log.error("Group for forwarding objective missing: {}", fwd);
324 fail(fwd, ObjectiveError.GROUPMISSING);
325 }
326
alshabib1aa58142016-02-17 15:37:56 -0800327 Group group = groupService.getGroup(deviceId, key);
alshabibd61b77b2016-02-01 23:30:53 -0800328 TrafficTreatment treatment =
329 buildTreatment(Instructions.createGroup(group.id()));
330
331 FlowRule rule = DefaultFlowRule.builder()
alshabib83364472016-03-25 09:59:55 -0700332 .fromApp(fwd.appId())
alshabibd61b77b2016-02-01 23:30:53 -0800333 .forDevice(deviceId)
334 .forTable(0)
alshabibd61b77b2016-02-01 23:30:53 -0800335 .makePermanent()
336 .withPriority(fwd.priority())
337 .withSelector(fwd.selector())
338 .withTreatment(treatment)
339 .build();
340
341 FlowRuleOperations.Builder builder = FlowRuleOperations.builder();
342 switch (fwd.op()) {
343
344 case ADD:
345 builder.add(rule);
346 break;
347 case REMOVE:
348 builder.remove(rule);
349 break;
350 case ADD_TO_EXISTING:
351 case REMOVE_FROM_EXISTING:
352 break;
353 default:
354 log.warn("Unknown forwarding operation: {}", fwd.op());
355 }
356
357 applyFlowRules(builder, fwd);
358
359 }
360
361 private boolean checkForMulticast(ForwardingObjective fwd) {
362
alshabib1aa58142016-02-17 15:37:56 -0800363 IPCriterion ip = (IPCriterion) filterForCriterion(fwd.selector().criteria(),
alshabib56efe432016-02-25 17:57:24 -0500364 Criterion.Type.IPV4_DST);
alshabibd61b77b2016-02-01 23:30:53 -0800365
alshabib1aa58142016-02-17 15:37:56 -0800366 if (ip == null) {
367 return false;
368 }
369
Charles Chanaedabfd2016-02-26 09:31:48 -0800370 return ip.ip().isMulticast();
alshabibd61b77b2016-02-01 23:30:53 -0800371
372 }
373
alshabib1aa58142016-02-17 15:37:56 -0800374 private GroupKey getGroupForNextObjective(Integer nextId) {
alshabibd61b77b2016-02-01 23:30:53 -0800375 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
alshabib1aa58142016-02-17 15:37:56 -0800376 return appKryo.deserialize(next.data());
alshabibd61b77b2016-02-01 23:30:53 -0800377
alshabib0ccde6d2015-05-30 18:22:36 -0700378 }
379
alshabibfd430b62015-12-16 18:56:38 -0800380 private void installDownstreamRules(ForwardingObjective fwd) {
381 List<Pair<Instruction, Instruction>> vlanOps =
382 vlanOps(fwd,
383 L2ModificationInstruction.L2SubType.VLAN_POP);
384
385 if (vlanOps == null) {
386 return;
387 }
388
alshabibb3c14342016-03-04 17:05:01 -0800389 Instructions.OutputInstruction output = (Instructions.OutputInstruction) fetchOutput(fwd, "downstream");
alshabibfd430b62015-12-16 18:56:38 -0800390
391 if (output == null) {
392 return;
393 }
394
395 Pair<Instruction, Instruction> popAndRewrite = vlanOps.remove(0);
396
alshabibfa0dc662016-01-13 11:23:53 -0800397 TrafficSelector selector = fwd.selector();
398
399 Criterion outerVlan = selector.getCriterion(Criterion.Type.VLAN_VID);
400 Criterion innerVlan = selector.getCriterion(Criterion.Type.INNER_VLAN_VID);
401 Criterion inport = selector.getCriterion(Criterion.Type.IN_PORT);
alshabibb3c14342016-03-04 17:05:01 -0800402 Criterion bullshit = Criteria.matchMetadata(output.port().toLong());
alshabibfa0dc662016-01-13 11:23:53 -0800403
404 if (outerVlan == null || innerVlan == null || inport == null) {
405 log.error("Forwarding objective is underspecified: {}", fwd);
406 fail(fwd, ObjectiveError.BADPARAMS);
407 return;
408 }
409
alshabib2f74f2c2016-01-14 13:29:35 -0800410 Criterion innerVid = Criteria.matchVlanId(((VlanIdCriterion) innerVlan).vlanId());
411
alshabibfa0dc662016-01-13 11:23:53 -0800412 FlowRule.Builder outer = DefaultFlowRule.builder()
alshabib83364472016-03-25 09:59:55 -0700413 .fromApp(fwd.appId())
alshabibfd430b62015-12-16 18:56:38 -0800414 .forDevice(deviceId)
alshabibfd430b62015-12-16 18:56:38 -0800415 .makePermanent()
416 .withPriority(fwd.priority())
alshabibb3c14342016-03-04 17:05:01 -0800417 .withSelector(buildSelector(inport, outerVlan, bullshit))
alshabibfd430b62015-12-16 18:56:38 -0800418 .withTreatment(buildTreatment(popAndRewrite.getLeft(),
419 Instructions.transition(QQ_TABLE)));
alshabibfd430b62015-12-16 18:56:38 -0800420
alshabibfa0dc662016-01-13 11:23:53 -0800421 FlowRule.Builder inner = DefaultFlowRule.builder()
alshabib83364472016-03-25 09:59:55 -0700422 .fromApp(fwd.appId())
alshabibfd430b62015-12-16 18:56:38 -0800423 .forDevice(deviceId)
alshabibfd430b62015-12-16 18:56:38 -0800424 .forTable(QQ_TABLE)
425 .makePermanent()
426 .withPriority(fwd.priority())
alshabib2f74f2c2016-01-14 13:29:35 -0800427 .withSelector(buildSelector(inport, innerVid))
alshabibe5075842016-02-04 13:28:33 -0800428 .withTreatment(buildTreatment(popAndRewrite.getLeft(),
alshabibfd430b62015-12-16 18:56:38 -0800429 output));
430
431 applyRules(fwd, inner, outer);
432
433 }
434
435 private void installUpstreamRules(ForwardingObjective fwd) {
436 List<Pair<Instruction, Instruction>> vlanOps =
437 vlanOps(fwd,
438 L2ModificationInstruction.L2SubType.VLAN_PUSH);
439
440 if (vlanOps == null) {
441 return;
442 }
443
444 Instruction output = fetchOutput(fwd, "upstream");
445
446 if (output == null) {
447 return;
448 }
449
450 Pair<Instruction, Instruction> innerPair = vlanOps.remove(0);
451
452 Pair<Instruction, Instruction> outerPair = vlanOps.remove(0);
453
Jonathan Hart3333cc62018-08-15 18:15:45 -0700454 // Add the VLAN_PUSH treatment if we're matching on VlanId.NONE
455 Criterion vlanMatchCriterion = filterForCriterion(fwd.selector().criteria(), Criterion.Type.VLAN_VID);
456 boolean push = false;
457 if (vlanMatchCriterion != null) {
458 push = ((VlanIdCriterion) vlanMatchCriterion).vlanId().equals(VlanId.NONE);
459 }
460
461 TrafficTreatment treatment;
462 if (push) {
463 treatment = buildTreatment(innerPair.getLeft(), innerPair.getRight(),
464 Instructions.transition(QQ_TABLE));
465 } else {
466 treatment = buildTreatment(innerPair.getRight(), Instructions.transition(QQ_TABLE));
467 }
468
alshabibfd430b62015-12-16 18:56:38 -0800469 FlowRule.Builder inner = DefaultFlowRule.builder()
alshabib83364472016-03-25 09:59:55 -0700470 .fromApp(fwd.appId())
alshabibfd430b62015-12-16 18:56:38 -0800471 .forDevice(deviceId)
alshabibfd430b62015-12-16 18:56:38 -0800472 .makePermanent()
473 .withPriority(fwd.priority())
474 .withSelector(fwd.selector())
Jonathan Hart3333cc62018-08-15 18:15:45 -0700475 .withTreatment(treatment);
alshabibfd430b62015-12-16 18:56:38 -0800476
477 PortCriterion inPort = (PortCriterion)
478 fwd.selector().getCriterion(Criterion.Type.IN_PORT);
479
480 VlanId cVlanId = ((L2ModificationInstruction.ModVlanIdInstruction)
481 innerPair.getRight()).vlanId();
482
483 FlowRule.Builder outer = DefaultFlowRule.builder()
alshabib83364472016-03-25 09:59:55 -0700484 .fromApp(fwd.appId())
alshabibfd430b62015-12-16 18:56:38 -0800485 .forDevice(deviceId)
alshabibfd430b62015-12-16 18:56:38 -0800486 .forTable(QQ_TABLE)
487 .makePermanent()
488 .withPriority(fwd.priority())
489 .withSelector(buildSelector(inPort,
490 Criteria.matchVlanId(cVlanId)))
491 .withTreatment(buildTreatment(outerPair.getLeft(),
492 outerPair.getRight(),
493 output));
494
495 applyRules(fwd, inner, outer);
496
497 }
498
499 private Instruction fetchOutput(ForwardingObjective fwd, String direction) {
500 Instruction output = fwd.treatment().allInstructions().stream()
501 .filter(i -> i.type() == Instruction.Type.OUTPUT)
502 .findFirst().orElse(null);
503
504 if (output == null) {
505 log.error("OLT {} rule has no output", direction);
506 fail(fwd, ObjectiveError.BADPARAMS);
507 return null;
508 }
509 return output;
510 }
511
512 private List<Pair<Instruction, Instruction>> vlanOps(ForwardingObjective fwd,
513 L2ModificationInstruction.L2SubType type) {
514
515 List<Pair<Instruction, Instruction>> vlanOps = findVlanOps(
516 fwd.treatment().allInstructions(), type);
517
518 if (vlanOps == null) {
519 String direction = type == L2ModificationInstruction.L2SubType.VLAN_POP
520 ? "downstream" : "upstream";
521 log.error("Missing vlan operations in {} forwarding: {}", direction, fwd);
522 fail(fwd, ObjectiveError.BADPARAMS);
523 return null;
524 }
525 return vlanOps;
526 }
527
528
529 private List<Pair<Instruction, Instruction>> findVlanOps(List<Instruction> instructions,
alshabibd61b77b2016-02-01 23:30:53 -0800530 L2ModificationInstruction.L2SubType type) {
alshabibfd430b62015-12-16 18:56:38 -0800531
532 List<Instruction> vlanPushs = findL2Instructions(
533 type,
534 instructions);
535 List<Instruction> vlanSets = findL2Instructions(
536 L2ModificationInstruction.L2SubType.VLAN_ID,
537 instructions);
538
539 if (vlanPushs.size() != vlanSets.size()) {
540 return null;
541 }
542
543 List<Pair<Instruction, Instruction>> pairs = Lists.newArrayList();
544
545 for (int i = 0; i < vlanPushs.size(); i++) {
546 pairs.add(new ImmutablePair<>(vlanPushs.get(i), vlanSets.get(i)));
547 }
548 return pairs;
549 }
550
551 private List<Instruction> findL2Instructions(L2ModificationInstruction.L2SubType subType,
552 List<Instruction> actions) {
553 return actions.stream()
554 .filter(i -> i.type() == Instruction.Type.L2MODIFICATION)
555 .filter(i -> ((L2ModificationInstruction) i).subtype() == subType)
556 .collect(Collectors.toList());
557 }
558
559 private void provisionEapol(FilteringObjective filter,
560 EthTypeCriterion ethType,
561 Instructions.OutputInstruction output) {
562
563 TrafficSelector selector = buildSelector(filter.key(), ethType);
564 TrafficTreatment treatment = buildTreatment(output);
565 buildAndApplyRule(filter, selector, treatment);
566
567 }
568
Jonathan Hart51539b82015-10-29 09:53:04 -0700569 private void provisionIgmp(FilteringObjective filter, EthTypeCriterion ethType,
alshabibfd430b62015-12-16 18:56:38 -0800570 IPProtocolCriterion ipProto,
571 Instructions.OutputInstruction output) {
572 TrafficSelector selector = buildSelector(filter.key(), ethType, ipProto);
573 TrafficTreatment treatment = buildTreatment(output);
574 buildAndApplyRule(filter, selector, treatment);
575 }
576
Amit Ghoshcbaf8672016-12-23 21:36:19 +0000577 private void provisionDhcp(FilteringObjective filter, EthTypeCriterion ethType,
578 IPProtocolCriterion ipProto,
579 UdpPortCriterion udpSrcPort,
580 UdpPortCriterion udpDstPort,
581 Instructions.OutputInstruction output) {
582 TrafficSelector selector = buildSelector(filter.key(), ethType, ipProto, udpSrcPort, udpDstPort);
583 TrafficTreatment treatment = buildTreatment(output);
584 buildAndApplyRule(filter, selector, treatment);
585 }
alshabibfd430b62015-12-16 18:56:38 -0800586 private void buildAndApplyRule(FilteringObjective filter, TrafficSelector selector,
587 TrafficTreatment treatment) {
588 FlowRule rule = DefaultFlowRule.builder()
alshabib83364472016-03-25 09:59:55 -0700589 .fromApp(filter.appId())
alshabibfd430b62015-12-16 18:56:38 -0800590 .forDevice(deviceId)
591 .forTable(0)
alshabibfd430b62015-12-16 18:56:38 -0800592 .makePermanent()
593 .withSelector(selector)
594 .withTreatment(treatment)
alshabibbb424232016-01-15 12:20:25 -0800595 .withPriority(filter.priority())
alshabibfd430b62015-12-16 18:56:38 -0800596 .build();
597
598 FlowRuleOperations.Builder opsBuilder = FlowRuleOperations.builder();
599
600 switch (filter.type()) {
601 case PERMIT:
602 opsBuilder.add(rule);
603 break;
604 case DENY:
605 opsBuilder.remove(rule);
606 break;
607 default:
608 log.warn("Unknown filter type : {}", filter.type());
609 fail(filter, ObjectiveError.UNSUPPORTED);
610 }
611
612 applyFlowRules(opsBuilder, filter);
613 }
614
615 private void applyRules(ForwardingObjective fwd,
616 FlowRule.Builder inner, FlowRule.Builder outer) {
617 FlowRuleOperations.Builder builder = FlowRuleOperations.builder();
618 switch (fwd.op()) {
619 case ADD:
620 builder.add(inner.build()).add(outer.build());
621 break;
622 case REMOVE:
alshabibb05be2d2016-04-11 12:52:36 -0700623 builder.remove(inner.build()).remove(outer.build());
alshabibfd430b62015-12-16 18:56:38 -0800624 break;
625 case ADD_TO_EXISTING:
626 break;
627 case REMOVE_FROM_EXISTING:
628 break;
629 default:
630 log.warn("Unknown forwarding operation: {}", fwd.op());
631 }
632
633 applyFlowRules(builder, fwd);
634 }
635
636 private void applyFlowRules(FlowRuleOperations.Builder builder,
637 Objective objective) {
638 flowRuleService.apply(builder.build(new FlowRuleOperationsContext() {
639 @Override
640 public void onSuccess(FlowRuleOperations ops) {
641 pass(objective);
642 }
643
644 @Override
645 public void onError(FlowRuleOperations ops) {
646 fail(objective, ObjectiveError.FLOWINSTALLATIONFAILED);
647 }
648 }));
649 }
650
651 private Criterion filterForCriterion(Collection<Criterion> criteria, Criterion.Type type) {
652 return criteria.stream()
alshabibbb424232016-01-15 12:20:25 -0800653 .filter(c -> c.type().equals(type))
alshabibfd430b62015-12-16 18:56:38 -0800654 .limit(1)
655 .findFirst().orElse(null);
656 }
657
658 private TrafficSelector buildSelector(Criterion... criteria) {
659
660
661 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
662
663 for (Criterion c : criteria) {
664 sBuilder.add(c);
665 }
666
667 return sBuilder.build();
668 }
669
670 private TrafficTreatment buildTreatment(Instruction... instructions) {
671
672
673 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
674
675 for (Instruction i : instructions) {
676 tBuilder.add(i);
677 }
678
679 return tBuilder.build();
680 }
681
682
683 private void fail(Objective obj, ObjectiveError error) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -0800684 obj.context().ifPresent(context -> context.onError(obj, error));
alshabibfd430b62015-12-16 18:56:38 -0800685 }
686
687 private void pass(Objective obj) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -0800688 obj.context().ifPresent(context -> context.onSuccess(obj));
alshabibfd430b62015-12-16 18:56:38 -0800689 }
690
alshabib2cc73cb2015-06-30 20:26:56 -0700691
alshabibd61b77b2016-02-01 23:30:53 -0800692 private class InnerGroupListener implements GroupListener {
alshabib2cc73cb2015-06-30 20:26:56 -0700693 @Override
alshabibd61b77b2016-02-01 23:30:53 -0800694 public void event(GroupEvent event) {
ke hanf5086672016-08-12 11:09:17 +0800695 if (event.type() == GroupEvent.Type.GROUP_ADDED ||
696 event.type() == GroupEvent.Type.GROUP_UPDATED) {
alshabibd61b77b2016-02-01 23:30:53 -0800697 GroupKey key = event.subject().appCookie();
alshabib2cc73cb2015-06-30 20:26:56 -0700698
alshabibd61b77b2016-02-01 23:30:53 -0800699 NextObjective obj = pendingGroups.getIfPresent(key);
700 if (obj != null) {
701 flowObjectiveStore.putNextGroup(obj.id(), new OLTPipelineGroup(key));
702 pass(obj);
703 pendingGroups.invalidate(key);
704 }
705 }
alshabib2cc73cb2015-06-30 20:26:56 -0700706 }
707 }
708
alshabibd61b77b2016-02-01 23:30:53 -0800709 private static class OLTPipelineGroup implements NextGroup {
710
711 private final GroupKey key;
712
713 public OLTPipelineGroup(GroupKey key) {
714 this.key = key;
715 }
716
717 public GroupKey key() {
718 return key;
719 }
720
721 @Override
722 public byte[] data() {
723 return appKryo.serialize(key);
724 }
725
726 }
Saurav Das24431192016-03-07 19:13:00 -0800727
728 @Override
729 public List<String> getNextMappings(NextGroup nextGroup) {
730 // TODO Implementation deferred to vendor
731 return null;
732 }
alshabib0ccde6d2015-05-30 18:22:36 -0700733}