blob: 86e8200dd456495ef11c50495a6dd49d937b5553 [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
454 FlowRule.Builder inner = DefaultFlowRule.builder()
alshabib83364472016-03-25 09:59:55 -0700455 .fromApp(fwd.appId())
alshabibfd430b62015-12-16 18:56:38 -0800456 .forDevice(deviceId)
alshabibfd430b62015-12-16 18:56:38 -0800457 .makePermanent()
458 .withPriority(fwd.priority())
459 .withSelector(fwd.selector())
460 .withTreatment(buildTreatment(innerPair.getRight(),
461 Instructions.transition(QQ_TABLE)));
462
463 PortCriterion inPort = (PortCriterion)
464 fwd.selector().getCriterion(Criterion.Type.IN_PORT);
465
466 VlanId cVlanId = ((L2ModificationInstruction.ModVlanIdInstruction)
467 innerPair.getRight()).vlanId();
468
469 FlowRule.Builder outer = 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 .forTable(QQ_TABLE)
473 .makePermanent()
474 .withPriority(fwd.priority())
475 .withSelector(buildSelector(inPort,
476 Criteria.matchVlanId(cVlanId)))
477 .withTreatment(buildTreatment(outerPair.getLeft(),
478 outerPair.getRight(),
479 output));
480
481 applyRules(fwd, inner, outer);
482
483 }
484
485 private Instruction fetchOutput(ForwardingObjective fwd, String direction) {
486 Instruction output = fwd.treatment().allInstructions().stream()
487 .filter(i -> i.type() == Instruction.Type.OUTPUT)
488 .findFirst().orElse(null);
489
490 if (output == null) {
491 log.error("OLT {} rule has no output", direction);
492 fail(fwd, ObjectiveError.BADPARAMS);
493 return null;
494 }
495 return output;
496 }
497
498 private List<Pair<Instruction, Instruction>> vlanOps(ForwardingObjective fwd,
499 L2ModificationInstruction.L2SubType type) {
500
501 List<Pair<Instruction, Instruction>> vlanOps = findVlanOps(
502 fwd.treatment().allInstructions(), type);
503
504 if (vlanOps == null) {
505 String direction = type == L2ModificationInstruction.L2SubType.VLAN_POP
506 ? "downstream" : "upstream";
507 log.error("Missing vlan operations in {} forwarding: {}", direction, fwd);
508 fail(fwd, ObjectiveError.BADPARAMS);
509 return null;
510 }
511 return vlanOps;
512 }
513
514
515 private List<Pair<Instruction, Instruction>> findVlanOps(List<Instruction> instructions,
alshabibd61b77b2016-02-01 23:30:53 -0800516 L2ModificationInstruction.L2SubType type) {
alshabibfd430b62015-12-16 18:56:38 -0800517
518 List<Instruction> vlanPushs = findL2Instructions(
519 type,
520 instructions);
521 List<Instruction> vlanSets = findL2Instructions(
522 L2ModificationInstruction.L2SubType.VLAN_ID,
523 instructions);
524
525 if (vlanPushs.size() != vlanSets.size()) {
526 return null;
527 }
528
529 List<Pair<Instruction, Instruction>> pairs = Lists.newArrayList();
530
531 for (int i = 0; i < vlanPushs.size(); i++) {
532 pairs.add(new ImmutablePair<>(vlanPushs.get(i), vlanSets.get(i)));
533 }
534 return pairs;
535 }
536
537 private List<Instruction> findL2Instructions(L2ModificationInstruction.L2SubType subType,
538 List<Instruction> actions) {
539 return actions.stream()
540 .filter(i -> i.type() == Instruction.Type.L2MODIFICATION)
541 .filter(i -> ((L2ModificationInstruction) i).subtype() == subType)
542 .collect(Collectors.toList());
543 }
544
545 private void provisionEapol(FilteringObjective filter,
546 EthTypeCriterion ethType,
547 Instructions.OutputInstruction output) {
548
549 TrafficSelector selector = buildSelector(filter.key(), ethType);
550 TrafficTreatment treatment = buildTreatment(output);
551 buildAndApplyRule(filter, selector, treatment);
552
553 }
554
Jonathan Hart51539b82015-10-29 09:53:04 -0700555 private void provisionIgmp(FilteringObjective filter, EthTypeCriterion ethType,
alshabibfd430b62015-12-16 18:56:38 -0800556 IPProtocolCriterion ipProto,
557 Instructions.OutputInstruction output) {
558 TrafficSelector selector = buildSelector(filter.key(), ethType, ipProto);
559 TrafficTreatment treatment = buildTreatment(output);
560 buildAndApplyRule(filter, selector, treatment);
561 }
562
Amit Ghoshcbaf8672016-12-23 21:36:19 +0000563 private void provisionDhcp(FilteringObjective filter, EthTypeCriterion ethType,
564 IPProtocolCriterion ipProto,
565 UdpPortCriterion udpSrcPort,
566 UdpPortCriterion udpDstPort,
567 Instructions.OutputInstruction output) {
568 TrafficSelector selector = buildSelector(filter.key(), ethType, ipProto, udpSrcPort, udpDstPort);
569 TrafficTreatment treatment = buildTreatment(output);
570 buildAndApplyRule(filter, selector, treatment);
571 }
alshabibfd430b62015-12-16 18:56:38 -0800572 private void buildAndApplyRule(FilteringObjective filter, TrafficSelector selector,
573 TrafficTreatment treatment) {
574 FlowRule rule = DefaultFlowRule.builder()
alshabib83364472016-03-25 09:59:55 -0700575 .fromApp(filter.appId())
alshabibfd430b62015-12-16 18:56:38 -0800576 .forDevice(deviceId)
577 .forTable(0)
alshabibfd430b62015-12-16 18:56:38 -0800578 .makePermanent()
579 .withSelector(selector)
580 .withTreatment(treatment)
alshabibbb424232016-01-15 12:20:25 -0800581 .withPriority(filter.priority())
alshabibfd430b62015-12-16 18:56:38 -0800582 .build();
583
584 FlowRuleOperations.Builder opsBuilder = FlowRuleOperations.builder();
585
586 switch (filter.type()) {
587 case PERMIT:
588 opsBuilder.add(rule);
589 break;
590 case DENY:
591 opsBuilder.remove(rule);
592 break;
593 default:
594 log.warn("Unknown filter type : {}", filter.type());
595 fail(filter, ObjectiveError.UNSUPPORTED);
596 }
597
598 applyFlowRules(opsBuilder, filter);
599 }
600
601 private void applyRules(ForwardingObjective fwd,
602 FlowRule.Builder inner, FlowRule.Builder outer) {
603 FlowRuleOperations.Builder builder = FlowRuleOperations.builder();
604 switch (fwd.op()) {
605 case ADD:
606 builder.add(inner.build()).add(outer.build());
607 break;
608 case REMOVE:
alshabibb05be2d2016-04-11 12:52:36 -0700609 builder.remove(inner.build()).remove(outer.build());
alshabibfd430b62015-12-16 18:56:38 -0800610 break;
611 case ADD_TO_EXISTING:
612 break;
613 case REMOVE_FROM_EXISTING:
614 break;
615 default:
616 log.warn("Unknown forwarding operation: {}", fwd.op());
617 }
618
619 applyFlowRules(builder, fwd);
620 }
621
622 private void applyFlowRules(FlowRuleOperations.Builder builder,
623 Objective objective) {
624 flowRuleService.apply(builder.build(new FlowRuleOperationsContext() {
625 @Override
626 public void onSuccess(FlowRuleOperations ops) {
627 pass(objective);
628 }
629
630 @Override
631 public void onError(FlowRuleOperations ops) {
632 fail(objective, ObjectiveError.FLOWINSTALLATIONFAILED);
633 }
634 }));
635 }
636
637 private Criterion filterForCriterion(Collection<Criterion> criteria, Criterion.Type type) {
638 return criteria.stream()
alshabibbb424232016-01-15 12:20:25 -0800639 .filter(c -> c.type().equals(type))
alshabibfd430b62015-12-16 18:56:38 -0800640 .limit(1)
641 .findFirst().orElse(null);
642 }
643
644 private TrafficSelector buildSelector(Criterion... criteria) {
645
646
647 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
648
649 for (Criterion c : criteria) {
650 sBuilder.add(c);
651 }
652
653 return sBuilder.build();
654 }
655
656 private TrafficTreatment buildTreatment(Instruction... instructions) {
657
658
659 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
660
661 for (Instruction i : instructions) {
662 tBuilder.add(i);
663 }
664
665 return tBuilder.build();
666 }
667
668
669 private void fail(Objective obj, ObjectiveError error) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -0800670 obj.context().ifPresent(context -> context.onError(obj, error));
alshabibfd430b62015-12-16 18:56:38 -0800671 }
672
673 private void pass(Objective obj) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -0800674 obj.context().ifPresent(context -> context.onSuccess(obj));
alshabibfd430b62015-12-16 18:56:38 -0800675 }
676
alshabib2cc73cb2015-06-30 20:26:56 -0700677
alshabibd61b77b2016-02-01 23:30:53 -0800678 private class InnerGroupListener implements GroupListener {
alshabib2cc73cb2015-06-30 20:26:56 -0700679 @Override
alshabibd61b77b2016-02-01 23:30:53 -0800680 public void event(GroupEvent event) {
ke hanf5086672016-08-12 11:09:17 +0800681 if (event.type() == GroupEvent.Type.GROUP_ADDED ||
682 event.type() == GroupEvent.Type.GROUP_UPDATED) {
alshabibd61b77b2016-02-01 23:30:53 -0800683 GroupKey key = event.subject().appCookie();
alshabib2cc73cb2015-06-30 20:26:56 -0700684
alshabibd61b77b2016-02-01 23:30:53 -0800685 NextObjective obj = pendingGroups.getIfPresent(key);
686 if (obj != null) {
687 flowObjectiveStore.putNextGroup(obj.id(), new OLTPipelineGroup(key));
688 pass(obj);
689 pendingGroups.invalidate(key);
690 }
691 }
alshabib2cc73cb2015-06-30 20:26:56 -0700692 }
693 }
694
alshabibd61b77b2016-02-01 23:30:53 -0800695 private static class OLTPipelineGroup implements NextGroup {
696
697 private final GroupKey key;
698
699 public OLTPipelineGroup(GroupKey key) {
700 this.key = key;
701 }
702
703 public GroupKey key() {
704 return key;
705 }
706
707 @Override
708 public byte[] data() {
709 return appKryo.serialize(key);
710 }
711
712 }
Saurav Das24431192016-03-07 19:13:00 -0800713
714 @Override
715 public List<String> getNextMappings(NextGroup nextGroup) {
716 // TODO Implementation deferred to vendor
717 return null;
718 }
alshabib0ccde6d2015-05-30 18:22:36 -0700719}