blob: 9ca9420a6562c76f7daedba9c40362a5657e5ff7 [file] [log] [blame]
Thomas Vachuska58de4162015-09-10 16:15:33 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Thomas Vachuska58de4162015-09-10 16:15:33 -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 */
Saurav Dasdecd7a62015-05-16 22:39:47 -070016package org.onosproject.driver.pipeline;
17
Saurav Dasdecd7a62015-05-16 22:39:47 -070018import org.onlab.osgi.ServiceDirectory;
Jonathan Hart63eeac32016-06-20 15:55:16 -070019import org.onlab.packet.EthType;
Saurav Dasdecd7a62015-05-16 22:39:47 -070020import org.onlab.packet.Ethernet;
Jonathan Hart29be97d2016-02-02 14:23:48 -080021import org.onlab.packet.IpPrefix;
Saurav Dasdecd7a62015-05-16 22:39:47 -070022import org.onlab.packet.VlanId;
23import org.onlab.util.KryoNamespace;
24import org.onosproject.core.ApplicationId;
25import org.onosproject.core.CoreService;
26import org.onosproject.net.DeviceId;
Jonathan Hart63eeac32016-06-20 15:55:16 -070027import org.onosproject.net.PortNumber;
Saurav Dasdecd7a62015-05-16 22:39:47 -070028import org.onosproject.net.behaviour.NextGroup;
29import org.onosproject.net.behaviour.Pipeliner;
30import org.onosproject.net.behaviour.PipelinerContext;
31import org.onosproject.net.driver.AbstractHandlerBehaviour;
32import org.onosproject.net.flow.DefaultFlowRule;
33import org.onosproject.net.flow.DefaultTrafficSelector;
34import org.onosproject.net.flow.DefaultTrafficTreatment;
35import org.onosproject.net.flow.FlowRule;
36import org.onosproject.net.flow.FlowRuleOperations;
37import org.onosproject.net.flow.FlowRuleOperationsContext;
38import org.onosproject.net.flow.FlowRuleService;
39import org.onosproject.net.flow.TrafficSelector;
40import org.onosproject.net.flow.TrafficTreatment;
41import org.onosproject.net.flow.criteria.Criteria;
42import org.onosproject.net.flow.criteria.Criterion;
43import org.onosproject.net.flow.criteria.EthCriterion;
44import org.onosproject.net.flow.criteria.EthTypeCriterion;
45import org.onosproject.net.flow.criteria.IPCriterion;
Vinayak Tejankar3a409c62017-01-12 02:20:53 +053046import org.onosproject.net.flow.criteria.IPProtocolCriterion;
47import org.onosproject.net.flow.criteria.Icmpv6TypeCriterion;
Saurav Dasdecd7a62015-05-16 22:39:47 -070048import org.onosproject.net.flow.criteria.PortCriterion;
49import org.onosproject.net.flow.criteria.VlanIdCriterion;
Saurav Das49cb5a12016-01-16 22:54:07 -080050import org.onosproject.net.flow.instructions.Instruction;
51import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
Saurav Dasdecd7a62015-05-16 22:39:47 -070052import org.onosproject.net.flowobjective.FilteringObjective;
53import org.onosproject.net.flowobjective.FlowObjectiveStore;
54import org.onosproject.net.flowobjective.ForwardingObjective;
55import org.onosproject.net.flowobjective.NextObjective;
56import org.onosproject.net.flowobjective.Objective;
57import org.onosproject.net.flowobjective.ObjectiveError;
Saurav Dasdecd7a62015-05-16 22:39:47 -070058import org.onosproject.store.serializers.KryoNamespaces;
Jonathan Harte54bdbf2015-11-17 11:29:37 -080059import org.slf4j.Logger;
60
61import java.util.ArrayList;
62import java.util.Collection;
63import java.util.Collections;
Saurav Das24431192016-03-07 19:13:00 -080064import java.util.List;
Sho SHIMIZU45906042016-01-13 23:05:54 -080065import java.util.Objects;
Jonathan Harte54bdbf2015-11-17 11:29:37 -080066
Vinayak Tejankar3a409c62017-01-12 02:20:53 +053067import static org.onlab.packet.Ethernet.TYPE_IPV4;
68import static org.onlab.packet.ICMP6.ECHO_REPLY;
69import static org.onlab.packet.ICMP6.ECHO_REQUEST;
70import static org.onlab.packet.IPv6.PROTOCOL_ICMP6;
Jonathan Hart29be97d2016-02-02 14:23:48 -080071import static org.onlab.util.Tools.delay;
Jonathan Harte54bdbf2015-11-17 11:29:37 -080072import static org.slf4j.LoggerFactory.getLogger;
Saurav Dasdecd7a62015-05-16 22:39:47 -070073
74/**
75 * Simple 2-Table Pipeline for Software/NPU based routers. This pipeline
76 * does not forward IP traffic to next-hop groups. Instead it forwards traffic
77 * using OF FlowMod actions.
78 */
79public class SoftRouterPipeline extends AbstractHandlerBehaviour implements Pipeliner {
80
81 protected static final int FILTER_TABLE = 0;
82 protected static final int FIB_TABLE = 1;
83
84 private static final int DROP_PRIORITY = 0;
85 private static final int DEFAULT_PRIORITY = 0x8000;
86 private static final int HIGHEST_PRIORITY = 0xffff;
87
88 private ServiceDirectory serviceDirectory;
89 protected FlowRuleService flowRuleService;
90 private CoreService coreService;
91 private FlowObjectiveStore flowObjectiveStore;
92 protected DeviceId deviceId;
93 protected ApplicationId appId;
94 private ApplicationId driverId;
Saurav Dasdecd7a62015-05-16 22:39:47 -070095
96 private KryoNamespace appKryo = new KryoNamespace.Builder()
Jonathan Hart888eeda2016-05-20 13:42:26 -070097 .register(KryoNamespaces.API)
98 .register(DummyGroup.class)
99 .build();
Saurav Dasdecd7a62015-05-16 22:39:47 -0700100
101 private final Logger log = getLogger(getClass());
102
103 @Override
104 public void init(DeviceId deviceId, PipelinerContext context) {
105 this.serviceDirectory = context.directory();
106 this.deviceId = deviceId;
107 coreService = serviceDirectory.get(CoreService.class);
108 flowRuleService = serviceDirectory.get(FlowRuleService.class);
109 flowObjectiveStore = context.store();
110 driverId = coreService.registerApplication(
Jonathan Harte54bdbf2015-11-17 11:29:37 -0800111 "org.onosproject.driver.SoftRouterPipeline");
Jonathan Hart6344f572015-12-15 08:26:25 -0800112
Saurav Dasdecd7a62015-05-16 22:39:47 -0700113 initializePipeline();
114 }
115
116 @Override
117 public void filter(FilteringObjective filteringObjective) {
118 if (filteringObjective.type() == FilteringObjective.Type.PERMIT) {
119 processFilter(filteringObjective,
120 filteringObjective.op() == Objective.Operation.ADD,
121 filteringObjective.appId());
122 } else {
123 fail(filteringObjective, ObjectiveError.UNSUPPORTED);
124 }
125 }
126
127 @Override
128 public void forward(ForwardingObjective fwd) {
129 Collection<FlowRule> rules;
130 FlowRuleOperations.Builder flowOpsBuilder = FlowRuleOperations.builder();
131
132 rules = processForward(fwd);
133 switch (fwd.op()) {
134 case ADD:
135 rules.stream()
Sho SHIMIZU45906042016-01-13 23:05:54 -0800136 .filter(Objects::nonNull)
Saurav Dasdecd7a62015-05-16 22:39:47 -0700137 .forEach(flowOpsBuilder::add);
138 break;
139 case REMOVE:
140 rules.stream()
Sho SHIMIZU45906042016-01-13 23:05:54 -0800141 .filter(Objects::nonNull)
Saurav Dasdecd7a62015-05-16 22:39:47 -0700142 .forEach(flowOpsBuilder::remove);
143 break;
144 default:
145 fail(fwd, ObjectiveError.UNKNOWN);
146 log.warn("Unknown forwarding type {}", fwd.op());
147 }
148
149
150 flowRuleService.apply(flowOpsBuilder.build(new FlowRuleOperationsContext() {
151 @Override
152 public void onSuccess(FlowRuleOperations ops) {
153 pass(fwd);
154 }
155
156 @Override
157 public void onError(FlowRuleOperations ops) {
158 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
159 }
160 }));
161
162 }
163
164 @Override
165 public void next(NextObjective nextObjective) {
166 switch (nextObjective.type()) {
Vinayak Tejankar3a409c62017-01-12 02:20:53 +0530167 case SIMPLE:
168 Collection<TrafficTreatment> treatments = nextObjective.next();
169 if (treatments.size() != 1) {
170 log.error("Next Objectives of type Simple should only have a "
171 + "single Traffic Treatment. Next Objective Id:{}", nextObjective.id());
172 fail(nextObjective, ObjectiveError.BADPARAMS);
173 return;
174 }
175 processSimpleNextObjective(nextObjective);
176 break;
177 case HASHED:
178 case BROADCAST:
179 case FAILOVER:
180 fail(nextObjective, ObjectiveError.UNSUPPORTED);
181 log.warn("Unsupported next objective type {}", nextObjective.type());
182 break;
183 default:
184 fail(nextObjective, ObjectiveError.UNKNOWN);
185 log.warn("Unknown next objective type {}", nextObjective.type());
Saurav Dasdecd7a62015-05-16 22:39:47 -0700186 }
187 }
188
189 private void pass(Objective obj) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -0800190 obj.context().ifPresent(context -> context.onSuccess(obj));
Saurav Dasdecd7a62015-05-16 22:39:47 -0700191 }
192
193 private void fail(Objective obj, ObjectiveError error) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -0800194 obj.context().ifPresent(context -> context.onError(obj, error));
Saurav Dasdecd7a62015-05-16 22:39:47 -0700195 }
196
Saurav Dasdecd7a62015-05-16 22:39:47 -0700197 private void initializePipeline() {
198 //Drop rules for both tables
199 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
200 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
201 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
202
203 treatment.drop();
204
205 FlowRule rule = DefaultFlowRule.builder()
206 .forDevice(deviceId)
207 .withSelector(selector.build())
208 .withTreatment(treatment.build())
209 .withPriority(DROP_PRIORITY)
210 .fromApp(driverId)
211 .makePermanent()
212 .forTable(FILTER_TABLE)
213 .build();
214 ops = ops.add(rule);
215
216 rule = DefaultFlowRule.builder().forDevice(deviceId)
217 .withSelector(selector.build())
218 .withTreatment(treatment.build())
219 .withPriority(DROP_PRIORITY)
220 .fromApp(driverId)
221 .makePermanent()
222 .forTable(FIB_TABLE)
223 .build();
224 ops = ops.add(rule);
225
226 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
227 @Override
228 public void onSuccess(FlowRuleOperations ops) {
229 log.info("Provisioned drop rules in both tables");
230 }
231
232 @Override
233 public void onError(FlowRuleOperations ops) {
234 log.info("Failed to provision drop rules");
235 }
236 }));
237 }
238
239 private void processFilter(FilteringObjective filt, boolean install,
240 ApplicationId applicationId) {
241 // This driver only processes filtering criteria defined with switch
242 // ports as the key
Jonathan Hart6344f572015-12-15 08:26:25 -0800243 PortCriterion p;
244 EthCriterion e = null;
245 VlanIdCriterion v = null;
246
Saurav Dasdecd7a62015-05-16 22:39:47 -0700247 if (!filt.key().equals(Criteria.dummy()) &&
248 filt.key().type() == Criterion.Type.IN_PORT) {
249 p = (PortCriterion) filt.key();
250 } else {
251 log.warn("No key defined in filtering objective from app: {}. Not"
Vinayak Tejankar3a409c62017-01-12 02:20:53 +0530252 + "processing filtering objective", applicationId);
Saurav Dasdecd7a62015-05-16 22:39:47 -0700253 fail(filt, ObjectiveError.UNKNOWN);
254 return;
255 }
256
257 // convert filtering conditions for switch-intfs into flowrules
258 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
259 for (Criterion c : filt.conditions()) {
gauravadc90042016-05-09 23:17:05 +0530260 if (c.type() == Criterion.Type.ETH_DST ||
261 c.type() == Criterion.Type.ETH_DST_MASKED) {
Saurav Dasdecd7a62015-05-16 22:39:47 -0700262 e = (EthCriterion) c;
263 } else if (c.type() == Criterion.Type.VLAN_VID) {
264 v = (VlanIdCriterion) c;
Saurav Dasdecd7a62015-05-16 22:39:47 -0700265 } else {
266 log.error("Unsupported filter {}", c);
267 fail(filt, ObjectiveError.UNSUPPORTED);
268 return;
269 }
Vinayak Tejankar3a409c62017-01-12 02:20:53 +0530270
Saurav Dasdecd7a62015-05-16 22:39:47 -0700271 }
272
Ray Milkeyfd4f8d32018-01-17 15:24:52 -0800273 if (v == null || e == null) {
274 log.warn("Soft Router Pipeline ETH_DST and/or VLAN_ID not specified");
275 fail(filt, ObjectiveError.BADPARAMS);
276 return;
277 }
278
Jonathan Hartf8035d32016-06-16 16:23:26 -0700279 log.debug("Modifying Port/VLAN/MAC filtering rules in filter table: {}/{}/{}",
Saurav Dasdecd7a62015-05-16 22:39:47 -0700280 p.port(), v.vlanId(), e.mac());
281 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
282 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
283 selector.matchInPort(p.port());
Jonathan Hart6344f572015-12-15 08:26:25 -0800284
gauravadc90042016-05-09 23:17:05 +0530285 //Multicast MAC
286 if (e.mask() != null) {
287 selector.matchEthDstMasked(e.mac(), e.mask());
288 } else {
289 selector.matchEthDst(e.mac());
290 }
Jonathan Hart6344f572015-12-15 08:26:25 -0800291 selector.matchVlanId(v.vlanId());
Jonathan Hartca47cd72015-12-13 12:31:09 -0800292 if (!v.vlanId().equals(VlanId.NONE)) {
293 treatment.popVlan();
294 }
Jonathan Hart6344f572015-12-15 08:26:25 -0800295 treatment.transition(FIB_TABLE);
Saurav Dasdecd7a62015-05-16 22:39:47 -0700296 FlowRule rule = DefaultFlowRule.builder()
297 .forDevice(deviceId)
298 .withSelector(selector.build())
299 .withTreatment(treatment.build())
300 .withPriority(DEFAULT_PRIORITY)
301 .fromApp(applicationId)
302 .makePermanent()
303 .forTable(FILTER_TABLE).build();
Saurav Dasdecd7a62015-05-16 22:39:47 -0700304
Saurav Dasdecd7a62015-05-16 22:39:47 -0700305 ops = install ? ops.add(rule) : ops.remove(rule);
Vinayak Tejankar3a409c62017-01-12 02:20:53 +0530306
Saurav Dasdecd7a62015-05-16 22:39:47 -0700307 // apply filtering flow rules
308 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
309 @Override
310 public void onSuccess(FlowRuleOperations ops) {
311 log.info("Applied filtering rules");
312 pass(filt);
313 }
314
315 @Override
316 public void onError(FlowRuleOperations ops) {
317 log.info("Failed to apply filtering rules");
318 fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED);
319 }
320 }));
321 }
322
323 private Collection<FlowRule> processForward(ForwardingObjective fwd) {
324 switch (fwd.flag()) {
Vinayak Tejankar3a409c62017-01-12 02:20:53 +0530325 case SPECIFIC:
326 return processSpecific(fwd);
327 case VERSATILE:
328 return processVersatile(fwd);
329 default:
330 fail(fwd, ObjectiveError.UNKNOWN);
331 log.warn("Unknown forwarding flag {}", fwd.flag());
Saurav Dasdecd7a62015-05-16 22:39:47 -0700332 }
333 return Collections.emptySet();
334 }
335
336 /**
Saurav Das49cb5a12016-01-16 22:54:07 -0800337 * SoftRouter has a single versatile table - the filter table.
338 * This table can be used to filter entries that reach the next table (FIB table).
339 * It can also be used to punt packets to the controller and/or bypass
340 * the FIB table to forward out of a port.
Saurav Dasdecd7a62015-05-16 22:39:47 -0700341 *
342 * @param fwd The forwarding objective of type versatile
343 * @return A collection of flow rules meant to be delivered to the flowrule
344 * subsystem. May return empty collection in case of failures.
345 */
346 private Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
Saurav Das49cb5a12016-01-16 22:54:07 -0800347 log.debug("Received versatile fwd: to next:{}", fwd.nextId());
Jonathan Hart6344f572015-12-15 08:26:25 -0800348 Collection<FlowRule> flowrules = new ArrayList<>();
Saurav Das49cb5a12016-01-16 22:54:07 -0800349 if (fwd.nextId() == null && fwd.treatment() == null) {
350 log.error("Forwarding objective {} from {} must contain "
Vinayak Tejankar3a409c62017-01-12 02:20:53 +0530351 + "nextId or Treatment", fwd.selector(), fwd.appId());
Saurav Das49cb5a12016-01-16 22:54:07 -0800352 return Collections.emptySet();
353 }
Jonathan Hart63eeac32016-06-20 15:55:16 -0700354
355 int tableId = FILTER_TABLE;
356
357 // A punt rule for IP traffic should be directed to the FIB table
358 // so that it only takes effect if the packet misses the FIB rules
359 if (fwd.treatment() != null && containsPunt(fwd.treatment()) &&
Vinayak Tejankar3a409c62017-01-12 02:20:53 +0530360 fwd.selector() != null && matchesIp(fwd.selector()) &&
361 !matchesControlTraffic(fwd.selector())) {
Jonathan Hart63eeac32016-06-20 15:55:16 -0700362 tableId = FIB_TABLE;
363 }
364
Saurav Das49cb5a12016-01-16 22:54:07 -0800365 TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder();
366 if (fwd.treatment() != null) {
367 fwd.treatment().immediate().forEach(ins -> ttBuilder.add(ins));
368 }
369 //convert nextId to flow actions
370 if (fwd.nextId() != null) {
371 // only acceptable value is output to port
372 NextGroup next = flowObjectiveStore.getNextGroup(fwd.nextId());
373 if (next == null) {
374 log.error("next-id {} does not exist in store", fwd.nextId());
375 return Collections.emptySet();
376 }
377 TrafficTreatment nt = appKryo.deserialize(next.data());
378 if (nt == null) {
379 log.error("Error in deserializing next-id {}", fwd.nextId());
380 return Collections.emptySet();
381 }
382 for (Instruction ins : nt.allInstructions()) {
383 if (ins instanceof OutputInstruction) {
384 ttBuilder.add(ins);
385 }
386 }
387 }
Jonathan Hart6344f572015-12-15 08:26:25 -0800388
389 FlowRule rule = DefaultFlowRule.builder()
390 .withSelector(fwd.selector())
Saurav Das49cb5a12016-01-16 22:54:07 -0800391 .withTreatment(ttBuilder.build())
Jonathan Hart63eeac32016-06-20 15:55:16 -0700392 .forTable(tableId)
Jonathan Hart6344f572015-12-15 08:26:25 -0800393 .makePermanent()
394 .forDevice(deviceId)
395 .fromApp(fwd.appId())
396 .withPriority(fwd.priority())
397 .build();
398
399 flowrules.add(rule);
400
Saurav Dasdecd7a62015-05-16 22:39:47 -0700401 return flowrules;
402 }
403
Jonathan Hart63eeac32016-06-20 15:55:16 -0700404 private boolean containsPunt(TrafficTreatment treatment) {
405 return treatment.immediate().stream()
406 .anyMatch(i -> i.type().equals(Instruction.Type.OUTPUT)
407 && ((OutputInstruction) i).port().equals(PortNumber.CONTROLLER));
408 }
409
410 private boolean matchesIp(TrafficSelector selector) {
411 EthTypeCriterion c = (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
Vinayak Tejankar3a409c62017-01-12 02:20:53 +0530412 return c != null && (c.ethType().equals(EthType.EtherType.IPV4.ethType()) ||
413 c.ethType().equals(EthType.EtherType.IPV6.ethType()));
414 }
415
416 private boolean matchesControlTraffic(TrafficSelector selector) {
417 EthTypeCriterion c = (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
418 if (c != null && c.ethType().equals(EthType.EtherType.ARP.ethType())) {
419 return true;
420 } else if (c != null && c.ethType().equals(EthType.EtherType.IPV6.ethType())) {
421 IPProtocolCriterion i = (IPProtocolCriterion) selector.getCriterion(Criterion.Type.IP_PROTO);
422 if (i != null && i.protocol() == PROTOCOL_ICMP6) {
423 Icmpv6TypeCriterion ic = (Icmpv6TypeCriterion) selector.getCriterion(Criterion.Type.ICMPV6_TYPE);
424 if (ic.icmpv6Type() != ECHO_REQUEST && ic.icmpv6Type() != ECHO_REPLY) {
425 return true;
426 }
427 }
428 }
429 return false;
Jonathan Hart63eeac32016-06-20 15:55:16 -0700430 }
431
Saurav Dasdecd7a62015-05-16 22:39:47 -0700432 /**
433 * SoftRouter has a single specific table - the FIB Table. It emulates
434 * LPM matching of dstIP by using higher priority flows for longer prefixes.
435 * Flows are forwarded using flow-actions
436 *
437 * @param fwd The forwarding objective of type simple
438 * @return A collection of flow rules meant to be delivered to the flowrule
439 * subsystem. Typically the returned collection has a single flowrule.
440 * May return empty collection in case of failures.
441 *
442 */
443 private Collection<FlowRule> processSpecific(ForwardingObjective fwd) {
Saurav Das49cb5a12016-01-16 22:54:07 -0800444 log.debug("Processing specific forwarding objective to next:{}", fwd.nextId());
Saurav Dasdecd7a62015-05-16 22:39:47 -0700445 TrafficSelector selector = fwd.selector();
446 EthTypeCriterion ethType =
447 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
448 // XXX currently supporting only the L3 unicast table
Vinayak Tejankar3a409c62017-01-12 02:20:53 +0530449 if (ethType == null || (ethType.ethType().toShort() != TYPE_IPV4
450 && ethType.ethType().toShort() != Ethernet.TYPE_IPV6)) {
Saurav Dasdecd7a62015-05-16 22:39:47 -0700451 fail(fwd, ObjectiveError.UNSUPPORTED);
452 return Collections.emptySet();
453 }
Vinayak Tejankar3a409c62017-01-12 02:20:53 +0530454 //We build the selector according the eth type.
455 IpPrefix ipPrefix;
456 TrafficSelector.Builder filteredSelector;
457 if (ethType.ethType().toShort() == TYPE_IPV4) {
458 ipPrefix = ((IPCriterion)
459 selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
Saurav Dasdecd7a62015-05-16 22:39:47 -0700460
Vinayak Tejankar3a409c62017-01-12 02:20:53 +0530461 filteredSelector = DefaultTrafficSelector.builder()
462 .matchEthType(TYPE_IPV4);
463 } else {
464 ipPrefix = ((IPCriterion)
465 selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
Jonathan Hart29be97d2016-02-02 14:23:48 -0800466
Vinayak Tejankar3a409c62017-01-12 02:20:53 +0530467 filteredSelector = DefaultTrafficSelector.builder()
468 .matchEthType(Ethernet.TYPE_IPV6);
469 }
470 // If the prefix is different from the default via.
Jonathan Hart29be97d2016-02-02 14:23:48 -0800471 if (ipPrefix.prefixLength() != 0) {
Vinayak Tejankar3a409c62017-01-12 02:20:53 +0530472 if (ethType.ethType().toShort() == TYPE_IPV4) {
473 filteredSelector.matchIPDst(ipPrefix);
474 } else {
475 filteredSelector.matchIPv6Dst(ipPrefix);
476 }
Jonathan Hart29be97d2016-02-02 14:23:48 -0800477 }
Saurav Dasdecd7a62015-05-16 22:39:47 -0700478
479 TrafficTreatment tt = null;
480 if (fwd.nextId() != null) {
481 NextGroup next = flowObjectiveStore.getNextGroup(fwd.nextId());
482 if (next == null) {
483 log.error("next-id {} does not exist in store", fwd.nextId());
484 return Collections.emptySet();
485 }
486 tt = appKryo.deserialize(next.data());
487 if (tt == null) {
488 log.error("Error in deserializing next-id {}", fwd.nextId());
489 return Collections.emptySet();
490 }
491 }
492
493 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
494 .fromApp(fwd.appId())
495 .withPriority(fwd.priority())
496 .forDevice(deviceId)
Jonathan Hart29be97d2016-02-02 14:23:48 -0800497 .withSelector(filteredSelector.build());
Jonathan Hart3930f632015-10-19 12:12:51 -0700498
499 if (tt != null) {
500 ruleBuilder.withTreatment(tt);
501 }
Saurav Dasdecd7a62015-05-16 22:39:47 -0700502
503 if (fwd.permanent()) {
504 ruleBuilder.makePermanent();
505 } else {
506 ruleBuilder.makeTemporary(fwd.timeout());
507 }
508
509 ruleBuilder.forTable(FIB_TABLE);
510 return Collections.singletonList(ruleBuilder.build());
511 }
512
513 /**
514 * Next Objectives are stored as dummy groups for retrieval later
515 * when Forwarding Objectives reference the next objective id. At that point
516 * the dummy group is fetched from the distributed store and the enclosed
517 * treatment is applied as a flow rule action.
518 *
alshabibcaf1ca22015-06-25 15:18:16 -0700519 * @param nextObj the next objective of type simple
Saurav Dasdecd7a62015-05-16 22:39:47 -0700520 */
521 private void processSimpleNextObjective(NextObjective nextObj) {
522 // Simple next objective has a single treatment (not a collection)
Saurav Das49cb5a12016-01-16 22:54:07 -0800523 log.debug("Received nextObj {}", nextObj.id());
524 // delay processing to emulate group creation
525 delay(50);
Saurav Das6c44a632015-05-30 22:05:22 -0700526 TrafficTreatment treatment = nextObj.next().iterator().next();
Saurav Dasdecd7a62015-05-16 22:39:47 -0700527 flowObjectiveStore.putNextGroup(nextObj.id(),
Saurav Das6c44a632015-05-30 22:05:22 -0700528 new DummyGroup(treatment));
Saurav Dasdecd7a62015-05-16 22:39:47 -0700529 }
530
Saurav Dasdecd7a62015-05-16 22:39:47 -0700531 private class DummyGroup implements NextGroup {
532 TrafficTreatment nextActions;
533
534 public DummyGroup(TrafficTreatment next) {
535 this.nextActions = next;
536 }
537
538 @Override
539 public byte[] data() {
540 return appKryo.serialize(nextActions);
541 }
542
543 }
544
Saurav Das24431192016-03-07 19:13:00 -0800545 @Override
546 public List<String> getNextMappings(NextGroup nextGroup) {
547 // nextObjectives converted to flow-actions not groups
548 return Collections.emptyList();
549 }
550
Saurav Dasdecd7a62015-05-16 22:39:47 -0700551}