blob: f6d16f4855bfa414e24c2ac3fa3992aea2a10a89 [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
Jonathan Hart248e7cd2018-08-21 17:53:02 -0700181 if (ethType.ethType().equals(EthType.EtherType.EAPOL.ethType()) ||
182 ethType.ethType().equals(EthType.EtherType.LLDP.ethType())) {
183 provisionEthtypeTrap(filter, ethType, output);
alshabibbb424232016-01-15 12:20:25 -0800184 } else if (ethType.ethType().equals(EthType.EtherType.IPV4.ethType())) {
alshabibfd430b62015-12-16 18:56:38 -0800185 IPProtocolCriterion ipProto = (IPProtocolCriterion)
186 filterForCriterion(filter.conditions(), Criterion.Type.IP_PROTO);
dvaddire8e6b89a2017-08-31 21:54:03 +0530187 if (ipProto == null) {
188 log.warn("OLT can only filter IGMP and DHCP");
189 fail(filter, ObjectiveError.UNSUPPORTED);
190 return;
191 }
alshabibfd430b62015-12-16 18:56:38 -0800192 if (ipProto.protocol() == IPv4.PROTOCOL_IGMP) {
Jonathan Hart51539b82015-10-29 09:53:04 -0700193 provisionIgmp(filter, ethType, ipProto, output);
Amit Ghoshcbaf8672016-12-23 21:36:19 +0000194 } else if (ipProto.protocol() == IPv4.PROTOCOL_UDP) {
195 UdpPortCriterion udpSrcPort = (UdpPortCriterion)
196 filterForCriterion(filter.conditions(), Criterion.Type.UDP_SRC);
197
198 UdpPortCriterion udpDstPort = (UdpPortCriterion)
199 filterForCriterion(filter.conditions(), Criterion.Type.UDP_DST);
200
201 if (udpSrcPort.udpPort().toInt() != 68 || udpDstPort.udpPort().toInt() != 67) {
dvaddire8e6b89a2017-08-31 21:54:03 +0530202 log.warn("OLT can only filter DHCP, wrong UDP Src or Dst Port");
Amit Ghoshcbaf8672016-12-23 21:36:19 +0000203 fail(filter, ObjectiveError.UNSUPPORTED);
204 }
205 provisionDhcp(filter, ethType, ipProto, udpSrcPort, udpDstPort, output);
alshabibbb424232016-01-15 12:20:25 -0800206 } else {
dvaddire8e6b89a2017-08-31 21:54:03 +0530207 log.warn("OLT can only filter IGMP and DHCP");
alshabibbb424232016-01-15 12:20:25 -0800208 fail(filter, ObjectiveError.UNSUPPORTED);
alshabibfd430b62015-12-16 18:56:38 -0800209 }
210 } else {
dvaddire8e6b89a2017-08-31 21:54:03 +0530211 log.warn("\nOnly the following are Supported in OLT for filter ->\n"
Jonathan Hart248e7cd2018-08-21 17:53:02 -0700212 + "ETH TYPE : EAPOL, LLDP and IPV4\n"
dvaddire8e6b89a2017-08-31 21:54:03 +0530213 + "IPV4 TYPE: IGMP and UDP (for DHCP)");
alshabibfd430b62015-12-16 18:56:38 -0800214 fail(filter, ObjectiveError.UNSUPPORTED);
215 }
216
217 }
218
219
220 @Override
221 public void forward(ForwardingObjective fwd) {
alshabibd61b77b2016-02-01 23:30:53 -0800222
223 if (checkForMulticast(fwd)) {
224 processMulticastRule(fwd);
225 return;
226 }
227
alshabib0ccde6d2015-05-30 18:22:36 -0700228 TrafficTreatment treatment = fwd.treatment();
alshabib0ccde6d2015-05-30 18:22:36 -0700229
alshabibfd430b62015-12-16 18:56:38 -0800230 List<Instruction> instructions = treatment.allInstructions();
alshabib0ccde6d2015-05-30 18:22:36 -0700231
alshabibfd430b62015-12-16 18:56:38 -0800232 Optional<Instruction> vlanIntruction = instructions.stream()
233 .filter(i -> i.type() == Instruction.Type.L2MODIFICATION)
234 .filter(i -> ((L2ModificationInstruction) i).subtype() ==
235 L2ModificationInstruction.L2SubType.VLAN_PUSH ||
236 ((L2ModificationInstruction) i).subtype() ==
237 L2ModificationInstruction.L2SubType.VLAN_POP)
238 .findAny();
239
240 if (!vlanIntruction.isPresent()) {
241 fail(fwd, ObjectiveError.BADPARAMS);
242 return;
243 }
244
245 L2ModificationInstruction vlanIns =
246 (L2ModificationInstruction) vlanIntruction.get();
247
248 if (vlanIns.subtype() == L2ModificationInstruction.L2SubType.VLAN_PUSH) {
249 installUpstreamRules(fwd);
250 } else if (vlanIns.subtype() == L2ModificationInstruction.L2SubType.VLAN_POP) {
251 installDownstreamRules(fwd);
alshabib0ccde6d2015-05-30 18:22:36 -0700252 } else {
alshabibfd430b62015-12-16 18:56:38 -0800253 log.error("Unknown OLT operation: {}", fwd);
254 fail(fwd, ObjectiveError.UNSUPPORTED);
255 return;
alshabib0ccde6d2015-05-30 18:22:36 -0700256 }
257
alshabibfd430b62015-12-16 18:56:38 -0800258 pass(fwd);
alshabib0ccde6d2015-05-30 18:22:36 -0700259
alshabib0ccde6d2015-05-30 18:22:36 -0700260 }
261
alshabibd61b77b2016-02-01 23:30:53 -0800262
alshabib0ccde6d2015-05-30 18:22:36 -0700263 @Override
264 public void next(NextObjective nextObjective) {
alshabibd61b77b2016-02-01 23:30:53 -0800265 if (nextObjective.type() != NextObjective.Type.BROADCAST) {
266 log.error("OLT only supports broadcast groups.");
267 fail(nextObjective, ObjectiveError.BADPARAMS);
268 }
269
270 if (nextObjective.next().size() != 1) {
271 log.error("OLT only supports singleton broadcast groups.");
272 fail(nextObjective, ObjectiveError.BADPARAMS);
273 }
274
275 TrafficTreatment treatment = nextObjective.next().stream().findFirst().get();
276
277
278 GroupBucket bucket = DefaultGroupBucket.createAllGroupBucket(treatment);
279 GroupKey key = new DefaultGroupKey(appKryo.serialize(nextObjective.id()));
280
alshabib1aa58142016-02-17 15:37:56 -0800281
alshabibd61b77b2016-02-01 23:30:53 -0800282 pendingGroups.put(key, nextObjective);
283
284 switch (nextObjective.op()) {
285 case ADD:
alshabib1aa58142016-02-17 15:37:56 -0800286 GroupDescription groupDesc =
287 new DefaultGroupDescription(deviceId,
288 GroupDescription.Type.ALL,
289 new GroupBuckets(Collections.singletonList(bucket)),
290 key,
291 null,
292 nextObjective.appId());
alshabibd61b77b2016-02-01 23:30:53 -0800293 groupService.addGroup(groupDesc);
294 break;
295 case REMOVE:
296 groupService.removeGroup(deviceId, key, nextObjective.appId());
297 break;
298 case ADD_TO_EXISTING:
alshabib1aa58142016-02-17 15:37:56 -0800299 groupService.addBucketsToGroup(deviceId, key,
300 new GroupBuckets(Collections.singletonList(bucket)),
301 key, nextObjective.appId());
302 break;
alshabibd61b77b2016-02-01 23:30:53 -0800303 case REMOVE_FROM_EXISTING:
alshabib1aa58142016-02-17 15:37:56 -0800304 groupService.removeBucketsFromGroup(deviceId, key,
alshabib56efe432016-02-25 17:57:24 -0500305 new GroupBuckets(Collections.singletonList(bucket)),
306 key, nextObjective.appId());
alshabibd61b77b2016-02-01 23:30:53 -0800307 break;
308 default:
309 log.warn("Unknown next objective operation: {}", nextObjective.op());
310 }
311
312
313 }
314
315 private void processMulticastRule(ForwardingObjective fwd) {
316 if (fwd.nextId() == null) {
317 log.error("Multicast objective does not have a next id");
318 fail(fwd, ObjectiveError.BADPARAMS);
319 }
320
alshabib1aa58142016-02-17 15:37:56 -0800321 GroupKey key = getGroupForNextObjective(fwd.nextId());
alshabibd61b77b2016-02-01 23:30:53 -0800322
alshabib1aa58142016-02-17 15:37:56 -0800323 if (key == null) {
alshabibd61b77b2016-02-01 23:30:53 -0800324 log.error("Group for forwarding objective missing: {}", fwd);
325 fail(fwd, ObjectiveError.GROUPMISSING);
326 }
327
alshabib1aa58142016-02-17 15:37:56 -0800328 Group group = groupService.getGroup(deviceId, key);
alshabibd61b77b2016-02-01 23:30:53 -0800329 TrafficTreatment treatment =
330 buildTreatment(Instructions.createGroup(group.id()));
331
332 FlowRule rule = DefaultFlowRule.builder()
alshabib83364472016-03-25 09:59:55 -0700333 .fromApp(fwd.appId())
alshabibd61b77b2016-02-01 23:30:53 -0800334 .forDevice(deviceId)
335 .forTable(0)
alshabibd61b77b2016-02-01 23:30:53 -0800336 .makePermanent()
337 .withPriority(fwd.priority())
338 .withSelector(fwd.selector())
339 .withTreatment(treatment)
340 .build();
341
342 FlowRuleOperations.Builder builder = FlowRuleOperations.builder();
343 switch (fwd.op()) {
344
345 case ADD:
346 builder.add(rule);
347 break;
348 case REMOVE:
349 builder.remove(rule);
350 break;
351 case ADD_TO_EXISTING:
352 case REMOVE_FROM_EXISTING:
353 break;
354 default:
355 log.warn("Unknown forwarding operation: {}", fwd.op());
356 }
357
358 applyFlowRules(builder, fwd);
359
360 }
361
362 private boolean checkForMulticast(ForwardingObjective fwd) {
363
alshabib1aa58142016-02-17 15:37:56 -0800364 IPCriterion ip = (IPCriterion) filterForCriterion(fwd.selector().criteria(),
alshabib56efe432016-02-25 17:57:24 -0500365 Criterion.Type.IPV4_DST);
alshabibd61b77b2016-02-01 23:30:53 -0800366
alshabib1aa58142016-02-17 15:37:56 -0800367 if (ip == null) {
368 return false;
369 }
370
Charles Chanaedabfd2016-02-26 09:31:48 -0800371 return ip.ip().isMulticast();
alshabibd61b77b2016-02-01 23:30:53 -0800372
373 }
374
alshabib1aa58142016-02-17 15:37:56 -0800375 private GroupKey getGroupForNextObjective(Integer nextId) {
alshabibd61b77b2016-02-01 23:30:53 -0800376 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
alshabib1aa58142016-02-17 15:37:56 -0800377 return appKryo.deserialize(next.data());
alshabibd61b77b2016-02-01 23:30:53 -0800378
alshabib0ccde6d2015-05-30 18:22:36 -0700379 }
380
alshabibfd430b62015-12-16 18:56:38 -0800381 private void installDownstreamRules(ForwardingObjective fwd) {
382 List<Pair<Instruction, Instruction>> vlanOps =
383 vlanOps(fwd,
384 L2ModificationInstruction.L2SubType.VLAN_POP);
385
386 if (vlanOps == null) {
387 return;
388 }
389
alshabibb3c14342016-03-04 17:05:01 -0800390 Instructions.OutputInstruction output = (Instructions.OutputInstruction) fetchOutput(fwd, "downstream");
alshabibfd430b62015-12-16 18:56:38 -0800391
392 if (output == null) {
393 return;
394 }
395
396 Pair<Instruction, Instruction> popAndRewrite = vlanOps.remove(0);
397
alshabibfa0dc662016-01-13 11:23:53 -0800398 TrafficSelector selector = fwd.selector();
399
400 Criterion outerVlan = selector.getCriterion(Criterion.Type.VLAN_VID);
401 Criterion innerVlan = selector.getCriterion(Criterion.Type.INNER_VLAN_VID);
402 Criterion inport = selector.getCriterion(Criterion.Type.IN_PORT);
alshabibb3c14342016-03-04 17:05:01 -0800403 Criterion bullshit = Criteria.matchMetadata(output.port().toLong());
alshabibfa0dc662016-01-13 11:23:53 -0800404
405 if (outerVlan == null || innerVlan == null || inport == null) {
406 log.error("Forwarding objective is underspecified: {}", fwd);
407 fail(fwd, ObjectiveError.BADPARAMS);
408 return;
409 }
410
alshabib2f74f2c2016-01-14 13:29:35 -0800411 Criterion innerVid = Criteria.matchVlanId(((VlanIdCriterion) innerVlan).vlanId());
412
alshabibfa0dc662016-01-13 11:23:53 -0800413 FlowRule.Builder outer = DefaultFlowRule.builder()
alshabib83364472016-03-25 09:59:55 -0700414 .fromApp(fwd.appId())
alshabibfd430b62015-12-16 18:56:38 -0800415 .forDevice(deviceId)
alshabibfd430b62015-12-16 18:56:38 -0800416 .makePermanent()
417 .withPriority(fwd.priority())
alshabibb3c14342016-03-04 17:05:01 -0800418 .withSelector(buildSelector(inport, outerVlan, bullshit))
alshabibfd430b62015-12-16 18:56:38 -0800419 .withTreatment(buildTreatment(popAndRewrite.getLeft(),
420 Instructions.transition(QQ_TABLE)));
alshabibfd430b62015-12-16 18:56:38 -0800421
alshabibfa0dc662016-01-13 11:23:53 -0800422 FlowRule.Builder inner = DefaultFlowRule.builder()
alshabib83364472016-03-25 09:59:55 -0700423 .fromApp(fwd.appId())
alshabibfd430b62015-12-16 18:56:38 -0800424 .forDevice(deviceId)
alshabibfd430b62015-12-16 18:56:38 -0800425 .forTable(QQ_TABLE)
426 .makePermanent()
427 .withPriority(fwd.priority())
alshabib2f74f2c2016-01-14 13:29:35 -0800428 .withSelector(buildSelector(inport, innerVid))
alshabibe5075842016-02-04 13:28:33 -0800429 .withTreatment(buildTreatment(popAndRewrite.getLeft(),
alshabibfd430b62015-12-16 18:56:38 -0800430 output));
431
432 applyRules(fwd, inner, outer);
433
434 }
435
436 private void installUpstreamRules(ForwardingObjective fwd) {
437 List<Pair<Instruction, Instruction>> vlanOps =
438 vlanOps(fwd,
439 L2ModificationInstruction.L2SubType.VLAN_PUSH);
440
441 if (vlanOps == null) {
442 return;
443 }
444
445 Instruction output = fetchOutput(fwd, "upstream");
446
447 if (output == null) {
448 return;
449 }
450
451 Pair<Instruction, Instruction> innerPair = vlanOps.remove(0);
452
453 Pair<Instruction, Instruction> outerPair = vlanOps.remove(0);
454
Jonathan Hart3333cc62018-08-15 18:15:45 -0700455 // Add the VLAN_PUSH treatment if we're matching on VlanId.NONE
456 Criterion vlanMatchCriterion = filterForCriterion(fwd.selector().criteria(), Criterion.Type.VLAN_VID);
457 boolean push = false;
458 if (vlanMatchCriterion != null) {
459 push = ((VlanIdCriterion) vlanMatchCriterion).vlanId().equals(VlanId.NONE);
460 }
461
462 TrafficTreatment treatment;
463 if (push) {
464 treatment = buildTreatment(innerPair.getLeft(), innerPair.getRight(),
465 Instructions.transition(QQ_TABLE));
466 } else {
467 treatment = buildTreatment(innerPair.getRight(), Instructions.transition(QQ_TABLE));
468 }
469
alshabibfd430b62015-12-16 18:56:38 -0800470 FlowRule.Builder inner = DefaultFlowRule.builder()
alshabib83364472016-03-25 09:59:55 -0700471 .fromApp(fwd.appId())
alshabibfd430b62015-12-16 18:56:38 -0800472 .forDevice(deviceId)
alshabibfd430b62015-12-16 18:56:38 -0800473 .makePermanent()
474 .withPriority(fwd.priority())
475 .withSelector(fwd.selector())
Jonathan Hart3333cc62018-08-15 18:15:45 -0700476 .withTreatment(treatment);
alshabibfd430b62015-12-16 18:56:38 -0800477
478 PortCriterion inPort = (PortCriterion)
479 fwd.selector().getCriterion(Criterion.Type.IN_PORT);
480
481 VlanId cVlanId = ((L2ModificationInstruction.ModVlanIdInstruction)
482 innerPair.getRight()).vlanId();
483
484 FlowRule.Builder outer = DefaultFlowRule.builder()
alshabib83364472016-03-25 09:59:55 -0700485 .fromApp(fwd.appId())
alshabibfd430b62015-12-16 18:56:38 -0800486 .forDevice(deviceId)
alshabibfd430b62015-12-16 18:56:38 -0800487 .forTable(QQ_TABLE)
488 .makePermanent()
489 .withPriority(fwd.priority())
490 .withSelector(buildSelector(inPort,
491 Criteria.matchVlanId(cVlanId)))
492 .withTreatment(buildTreatment(outerPair.getLeft(),
493 outerPair.getRight(),
494 output));
495
496 applyRules(fwd, inner, outer);
497
498 }
499
500 private Instruction fetchOutput(ForwardingObjective fwd, String direction) {
501 Instruction output = fwd.treatment().allInstructions().stream()
502 .filter(i -> i.type() == Instruction.Type.OUTPUT)
503 .findFirst().orElse(null);
504
505 if (output == null) {
506 log.error("OLT {} rule has no output", direction);
507 fail(fwd, ObjectiveError.BADPARAMS);
508 return null;
509 }
510 return output;
511 }
512
513 private List<Pair<Instruction, Instruction>> vlanOps(ForwardingObjective fwd,
514 L2ModificationInstruction.L2SubType type) {
515
516 List<Pair<Instruction, Instruction>> vlanOps = findVlanOps(
517 fwd.treatment().allInstructions(), type);
518
519 if (vlanOps == null) {
520 String direction = type == L2ModificationInstruction.L2SubType.VLAN_POP
521 ? "downstream" : "upstream";
522 log.error("Missing vlan operations in {} forwarding: {}", direction, fwd);
523 fail(fwd, ObjectiveError.BADPARAMS);
524 return null;
525 }
526 return vlanOps;
527 }
528
529
530 private List<Pair<Instruction, Instruction>> findVlanOps(List<Instruction> instructions,
alshabibd61b77b2016-02-01 23:30:53 -0800531 L2ModificationInstruction.L2SubType type) {
alshabibfd430b62015-12-16 18:56:38 -0800532
533 List<Instruction> vlanPushs = findL2Instructions(
534 type,
535 instructions);
536 List<Instruction> vlanSets = findL2Instructions(
537 L2ModificationInstruction.L2SubType.VLAN_ID,
538 instructions);
539
540 if (vlanPushs.size() != vlanSets.size()) {
541 return null;
542 }
543
544 List<Pair<Instruction, Instruction>> pairs = Lists.newArrayList();
545
546 for (int i = 0; i < vlanPushs.size(); i++) {
547 pairs.add(new ImmutablePair<>(vlanPushs.get(i), vlanSets.get(i)));
548 }
549 return pairs;
550 }
551
552 private List<Instruction> findL2Instructions(L2ModificationInstruction.L2SubType subType,
553 List<Instruction> actions) {
554 return actions.stream()
555 .filter(i -> i.type() == Instruction.Type.L2MODIFICATION)
556 .filter(i -> ((L2ModificationInstruction) i).subtype() == subType)
557 .collect(Collectors.toList());
558 }
559
Jonathan Hart248e7cd2018-08-21 17:53:02 -0700560 private void provisionEthtypeTrap(FilteringObjective filter,
561 EthTypeCriterion ethType,
562 Instructions.OutputInstruction output) {
alshabibfd430b62015-12-16 18:56:38 -0800563
564 TrafficSelector selector = buildSelector(filter.key(), ethType);
565 TrafficTreatment treatment = buildTreatment(output);
566 buildAndApplyRule(filter, selector, treatment);
567
568 }
569
Jonathan Hart51539b82015-10-29 09:53:04 -0700570 private void provisionIgmp(FilteringObjective filter, EthTypeCriterion ethType,
alshabibfd430b62015-12-16 18:56:38 -0800571 IPProtocolCriterion ipProto,
572 Instructions.OutputInstruction output) {
573 TrafficSelector selector = buildSelector(filter.key(), ethType, ipProto);
574 TrafficTreatment treatment = buildTreatment(output);
575 buildAndApplyRule(filter, selector, treatment);
576 }
577
Amit Ghoshcbaf8672016-12-23 21:36:19 +0000578 private void provisionDhcp(FilteringObjective filter, EthTypeCriterion ethType,
579 IPProtocolCriterion ipProto,
580 UdpPortCriterion udpSrcPort,
581 UdpPortCriterion udpDstPort,
582 Instructions.OutputInstruction output) {
583 TrafficSelector selector = buildSelector(filter.key(), ethType, ipProto, udpSrcPort, udpDstPort);
584 TrafficTreatment treatment = buildTreatment(output);
585 buildAndApplyRule(filter, selector, treatment);
586 }
alshabibfd430b62015-12-16 18:56:38 -0800587 private void buildAndApplyRule(FilteringObjective filter, TrafficSelector selector,
588 TrafficTreatment treatment) {
589 FlowRule rule = DefaultFlowRule.builder()
alshabib83364472016-03-25 09:59:55 -0700590 .fromApp(filter.appId())
alshabibfd430b62015-12-16 18:56:38 -0800591 .forDevice(deviceId)
592 .forTable(0)
alshabibfd430b62015-12-16 18:56:38 -0800593 .makePermanent()
594 .withSelector(selector)
595 .withTreatment(treatment)
alshabibbb424232016-01-15 12:20:25 -0800596 .withPriority(filter.priority())
alshabibfd430b62015-12-16 18:56:38 -0800597 .build();
598
599 FlowRuleOperations.Builder opsBuilder = FlowRuleOperations.builder();
600
601 switch (filter.type()) {
602 case PERMIT:
603 opsBuilder.add(rule);
604 break;
605 case DENY:
606 opsBuilder.remove(rule);
607 break;
608 default:
609 log.warn("Unknown filter type : {}", filter.type());
610 fail(filter, ObjectiveError.UNSUPPORTED);
611 }
612
613 applyFlowRules(opsBuilder, filter);
614 }
615
616 private void applyRules(ForwardingObjective fwd,
617 FlowRule.Builder inner, FlowRule.Builder outer) {
618 FlowRuleOperations.Builder builder = FlowRuleOperations.builder();
619 switch (fwd.op()) {
620 case ADD:
621 builder.add(inner.build()).add(outer.build());
622 break;
623 case REMOVE:
alshabibb05be2d2016-04-11 12:52:36 -0700624 builder.remove(inner.build()).remove(outer.build());
alshabibfd430b62015-12-16 18:56:38 -0800625 break;
626 case ADD_TO_EXISTING:
627 break;
628 case REMOVE_FROM_EXISTING:
629 break;
630 default:
631 log.warn("Unknown forwarding operation: {}", fwd.op());
632 }
633
634 applyFlowRules(builder, fwd);
635 }
636
637 private void applyFlowRules(FlowRuleOperations.Builder builder,
638 Objective objective) {
639 flowRuleService.apply(builder.build(new FlowRuleOperationsContext() {
640 @Override
641 public void onSuccess(FlowRuleOperations ops) {
642 pass(objective);
643 }
644
645 @Override
646 public void onError(FlowRuleOperations ops) {
647 fail(objective, ObjectiveError.FLOWINSTALLATIONFAILED);
648 }
649 }));
650 }
651
652 private Criterion filterForCriterion(Collection<Criterion> criteria, Criterion.Type type) {
653 return criteria.stream()
alshabibbb424232016-01-15 12:20:25 -0800654 .filter(c -> c.type().equals(type))
alshabibfd430b62015-12-16 18:56:38 -0800655 .limit(1)
656 .findFirst().orElse(null);
657 }
658
659 private TrafficSelector buildSelector(Criterion... criteria) {
660
661
662 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
663
664 for (Criterion c : criteria) {
665 sBuilder.add(c);
666 }
667
668 return sBuilder.build();
669 }
670
671 private TrafficTreatment buildTreatment(Instruction... instructions) {
672
673
674 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
675
676 for (Instruction i : instructions) {
677 tBuilder.add(i);
678 }
679
680 return tBuilder.build();
681 }
682
683
684 private void fail(Objective obj, ObjectiveError error) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -0800685 obj.context().ifPresent(context -> context.onError(obj, error));
alshabibfd430b62015-12-16 18:56:38 -0800686 }
687
688 private void pass(Objective obj) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -0800689 obj.context().ifPresent(context -> context.onSuccess(obj));
alshabibfd430b62015-12-16 18:56:38 -0800690 }
691
alshabib2cc73cb2015-06-30 20:26:56 -0700692
alshabibd61b77b2016-02-01 23:30:53 -0800693 private class InnerGroupListener implements GroupListener {
alshabib2cc73cb2015-06-30 20:26:56 -0700694 @Override
alshabibd61b77b2016-02-01 23:30:53 -0800695 public void event(GroupEvent event) {
ke hanf5086672016-08-12 11:09:17 +0800696 if (event.type() == GroupEvent.Type.GROUP_ADDED ||
697 event.type() == GroupEvent.Type.GROUP_UPDATED) {
alshabibd61b77b2016-02-01 23:30:53 -0800698 GroupKey key = event.subject().appCookie();
alshabib2cc73cb2015-06-30 20:26:56 -0700699
alshabibd61b77b2016-02-01 23:30:53 -0800700 NextObjective obj = pendingGroups.getIfPresent(key);
701 if (obj != null) {
702 flowObjectiveStore.putNextGroup(obj.id(), new OLTPipelineGroup(key));
703 pass(obj);
704 pendingGroups.invalidate(key);
705 }
706 }
alshabib2cc73cb2015-06-30 20:26:56 -0700707 }
708 }
709
alshabibd61b77b2016-02-01 23:30:53 -0800710 private static class OLTPipelineGroup implements NextGroup {
711
712 private final GroupKey key;
713
714 public OLTPipelineGroup(GroupKey key) {
715 this.key = key;
716 }
717
718 public GroupKey key() {
719 return key;
720 }
721
722 @Override
723 public byte[] data() {
724 return appKryo.serialize(key);
725 }
726
727 }
Saurav Das24431192016-03-07 19:13:00 -0800728
729 @Override
730 public List<String> getNextMappings(NextGroup nextGroup) {
731 // TODO Implementation deferred to vendor
732 return null;
733 }
alshabib0ccde6d2015-05-30 18:22:36 -0700734}