blob: 03a77bac562cef354fa41d1de122b0eee93b19ca [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
Jonathan Hartf8035d32016-06-16 16:23:26 -0700273 log.debug("Modifying Port/VLAN/MAC filtering rules in filter table: {}/{}/{}",
Saurav Dasdecd7a62015-05-16 22:39:47 -0700274 p.port(), v.vlanId(), e.mac());
275 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
276 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
277 selector.matchInPort(p.port());
Jonathan Hart6344f572015-12-15 08:26:25 -0800278
gauravadc90042016-05-09 23:17:05 +0530279 //Multicast MAC
280 if (e.mask() != null) {
281 selector.matchEthDstMasked(e.mac(), e.mask());
282 } else {
283 selector.matchEthDst(e.mac());
284 }
Jonathan Hart6344f572015-12-15 08:26:25 -0800285 selector.matchVlanId(v.vlanId());
Jonathan Hartca47cd72015-12-13 12:31:09 -0800286 if (!v.vlanId().equals(VlanId.NONE)) {
287 treatment.popVlan();
288 }
Jonathan Hart6344f572015-12-15 08:26:25 -0800289 treatment.transition(FIB_TABLE);
Saurav Dasdecd7a62015-05-16 22:39:47 -0700290 FlowRule rule = DefaultFlowRule.builder()
291 .forDevice(deviceId)
292 .withSelector(selector.build())
293 .withTreatment(treatment.build())
294 .withPriority(DEFAULT_PRIORITY)
295 .fromApp(applicationId)
296 .makePermanent()
297 .forTable(FILTER_TABLE).build();
Saurav Dasdecd7a62015-05-16 22:39:47 -0700298
Saurav Dasdecd7a62015-05-16 22:39:47 -0700299 ops = install ? ops.add(rule) : ops.remove(rule);
Vinayak Tejankar3a409c62017-01-12 02:20:53 +0530300
Saurav Dasdecd7a62015-05-16 22:39:47 -0700301 // apply filtering flow rules
302 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
303 @Override
304 public void onSuccess(FlowRuleOperations ops) {
305 log.info("Applied filtering rules");
306 pass(filt);
307 }
308
309 @Override
310 public void onError(FlowRuleOperations ops) {
311 log.info("Failed to apply filtering rules");
312 fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED);
313 }
314 }));
315 }
316
317 private Collection<FlowRule> processForward(ForwardingObjective fwd) {
318 switch (fwd.flag()) {
Vinayak Tejankar3a409c62017-01-12 02:20:53 +0530319 case SPECIFIC:
320 return processSpecific(fwd);
321 case VERSATILE:
322 return processVersatile(fwd);
323 default:
324 fail(fwd, ObjectiveError.UNKNOWN);
325 log.warn("Unknown forwarding flag {}", fwd.flag());
Saurav Dasdecd7a62015-05-16 22:39:47 -0700326 }
327 return Collections.emptySet();
328 }
329
330 /**
Saurav Das49cb5a12016-01-16 22:54:07 -0800331 * SoftRouter has a single versatile table - the filter table.
332 * This table can be used to filter entries that reach the next table (FIB table).
333 * It can also be used to punt packets to the controller and/or bypass
334 * the FIB table to forward out of a port.
Saurav Dasdecd7a62015-05-16 22:39:47 -0700335 *
336 * @param fwd The forwarding objective of type versatile
337 * @return A collection of flow rules meant to be delivered to the flowrule
338 * subsystem. May return empty collection in case of failures.
339 */
340 private Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
Saurav Das49cb5a12016-01-16 22:54:07 -0800341 log.debug("Received versatile fwd: to next:{}", fwd.nextId());
Jonathan Hart6344f572015-12-15 08:26:25 -0800342 Collection<FlowRule> flowrules = new ArrayList<>();
Saurav Das49cb5a12016-01-16 22:54:07 -0800343 if (fwd.nextId() == null && fwd.treatment() == null) {
344 log.error("Forwarding objective {} from {} must contain "
Vinayak Tejankar3a409c62017-01-12 02:20:53 +0530345 + "nextId or Treatment", fwd.selector(), fwd.appId());
Saurav Das49cb5a12016-01-16 22:54:07 -0800346 return Collections.emptySet();
347 }
Jonathan Hart63eeac32016-06-20 15:55:16 -0700348
349 int tableId = FILTER_TABLE;
350
351 // A punt rule for IP traffic should be directed to the FIB table
352 // so that it only takes effect if the packet misses the FIB rules
353 if (fwd.treatment() != null && containsPunt(fwd.treatment()) &&
Vinayak Tejankar3a409c62017-01-12 02:20:53 +0530354 fwd.selector() != null && matchesIp(fwd.selector()) &&
355 !matchesControlTraffic(fwd.selector())) {
Jonathan Hart63eeac32016-06-20 15:55:16 -0700356 tableId = FIB_TABLE;
357 }
358
Saurav Das49cb5a12016-01-16 22:54:07 -0800359 TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder();
360 if (fwd.treatment() != null) {
361 fwd.treatment().immediate().forEach(ins -> ttBuilder.add(ins));
362 }
363 //convert nextId to flow actions
364 if (fwd.nextId() != null) {
365 // only acceptable value is output to port
366 NextGroup next = flowObjectiveStore.getNextGroup(fwd.nextId());
367 if (next == null) {
368 log.error("next-id {} does not exist in store", fwd.nextId());
369 return Collections.emptySet();
370 }
371 TrafficTreatment nt = appKryo.deserialize(next.data());
372 if (nt == null) {
373 log.error("Error in deserializing next-id {}", fwd.nextId());
374 return Collections.emptySet();
375 }
376 for (Instruction ins : nt.allInstructions()) {
377 if (ins instanceof OutputInstruction) {
378 ttBuilder.add(ins);
379 }
380 }
381 }
Jonathan Hart6344f572015-12-15 08:26:25 -0800382
383 FlowRule rule = DefaultFlowRule.builder()
384 .withSelector(fwd.selector())
Saurav Das49cb5a12016-01-16 22:54:07 -0800385 .withTreatment(ttBuilder.build())
Jonathan Hart63eeac32016-06-20 15:55:16 -0700386 .forTable(tableId)
Jonathan Hart6344f572015-12-15 08:26:25 -0800387 .makePermanent()
388 .forDevice(deviceId)
389 .fromApp(fwd.appId())
390 .withPriority(fwd.priority())
391 .build();
392
393 flowrules.add(rule);
394
Saurav Dasdecd7a62015-05-16 22:39:47 -0700395 return flowrules;
396 }
397
Jonathan Hart63eeac32016-06-20 15:55:16 -0700398 private boolean containsPunt(TrafficTreatment treatment) {
399 return treatment.immediate().stream()
400 .anyMatch(i -> i.type().equals(Instruction.Type.OUTPUT)
401 && ((OutputInstruction) i).port().equals(PortNumber.CONTROLLER));
402 }
403
404 private boolean matchesIp(TrafficSelector selector) {
405 EthTypeCriterion c = (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
Vinayak Tejankar3a409c62017-01-12 02:20:53 +0530406 return c != null && (c.ethType().equals(EthType.EtherType.IPV4.ethType()) ||
407 c.ethType().equals(EthType.EtherType.IPV6.ethType()));
408 }
409
410 private boolean matchesControlTraffic(TrafficSelector selector) {
411 EthTypeCriterion c = (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
412 if (c != null && c.ethType().equals(EthType.EtherType.ARP.ethType())) {
413 return true;
414 } else if (c != null && c.ethType().equals(EthType.EtherType.IPV6.ethType())) {
415 IPProtocolCriterion i = (IPProtocolCriterion) selector.getCriterion(Criterion.Type.IP_PROTO);
416 if (i != null && i.protocol() == PROTOCOL_ICMP6) {
417 Icmpv6TypeCriterion ic = (Icmpv6TypeCriterion) selector.getCriterion(Criterion.Type.ICMPV6_TYPE);
418 if (ic.icmpv6Type() != ECHO_REQUEST && ic.icmpv6Type() != ECHO_REPLY) {
419 return true;
420 }
421 }
422 }
423 return false;
Jonathan Hart63eeac32016-06-20 15:55:16 -0700424 }
425
Saurav Dasdecd7a62015-05-16 22:39:47 -0700426 /**
427 * SoftRouter has a single specific table - the FIB Table. It emulates
428 * LPM matching of dstIP by using higher priority flows for longer prefixes.
429 * Flows are forwarded using flow-actions
430 *
431 * @param fwd The forwarding objective of type simple
432 * @return A collection of flow rules meant to be delivered to the flowrule
433 * subsystem. Typically the returned collection has a single flowrule.
434 * May return empty collection in case of failures.
435 *
436 */
437 private Collection<FlowRule> processSpecific(ForwardingObjective fwd) {
Saurav Das49cb5a12016-01-16 22:54:07 -0800438 log.debug("Processing specific forwarding objective to next:{}", fwd.nextId());
Saurav Dasdecd7a62015-05-16 22:39:47 -0700439 TrafficSelector selector = fwd.selector();
440 EthTypeCriterion ethType =
441 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
442 // XXX currently supporting only the L3 unicast table
Vinayak Tejankar3a409c62017-01-12 02:20:53 +0530443 if (ethType == null || (ethType.ethType().toShort() != TYPE_IPV4
444 && ethType.ethType().toShort() != Ethernet.TYPE_IPV6)) {
Saurav Dasdecd7a62015-05-16 22:39:47 -0700445 fail(fwd, ObjectiveError.UNSUPPORTED);
446 return Collections.emptySet();
447 }
Vinayak Tejankar3a409c62017-01-12 02:20:53 +0530448 //We build the selector according the eth type.
449 IpPrefix ipPrefix;
450 TrafficSelector.Builder filteredSelector;
451 if (ethType.ethType().toShort() == TYPE_IPV4) {
452 ipPrefix = ((IPCriterion)
453 selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
Saurav Dasdecd7a62015-05-16 22:39:47 -0700454
Vinayak Tejankar3a409c62017-01-12 02:20:53 +0530455 filteredSelector = DefaultTrafficSelector.builder()
456 .matchEthType(TYPE_IPV4);
457 } else {
458 ipPrefix = ((IPCriterion)
459 selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
Jonathan Hart29be97d2016-02-02 14:23:48 -0800460
Vinayak Tejankar3a409c62017-01-12 02:20:53 +0530461 filteredSelector = DefaultTrafficSelector.builder()
462 .matchEthType(Ethernet.TYPE_IPV6);
463 }
464 // If the prefix is different from the default via.
Jonathan Hart29be97d2016-02-02 14:23:48 -0800465 if (ipPrefix.prefixLength() != 0) {
Vinayak Tejankar3a409c62017-01-12 02:20:53 +0530466 if (ethType.ethType().toShort() == TYPE_IPV4) {
467 filteredSelector.matchIPDst(ipPrefix);
468 } else {
469 filteredSelector.matchIPv6Dst(ipPrefix);
470 }
Jonathan Hart29be97d2016-02-02 14:23:48 -0800471 }
Saurav Dasdecd7a62015-05-16 22:39:47 -0700472
473 TrafficTreatment tt = null;
474 if (fwd.nextId() != null) {
475 NextGroup next = flowObjectiveStore.getNextGroup(fwd.nextId());
476 if (next == null) {
477 log.error("next-id {} does not exist in store", fwd.nextId());
478 return Collections.emptySet();
479 }
480 tt = appKryo.deserialize(next.data());
481 if (tt == null) {
482 log.error("Error in deserializing next-id {}", fwd.nextId());
483 return Collections.emptySet();
484 }
485 }
486
487 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
488 .fromApp(fwd.appId())
489 .withPriority(fwd.priority())
490 .forDevice(deviceId)
Jonathan Hart29be97d2016-02-02 14:23:48 -0800491 .withSelector(filteredSelector.build());
Jonathan Hart3930f632015-10-19 12:12:51 -0700492
493 if (tt != null) {
494 ruleBuilder.withTreatment(tt);
495 }
Saurav Dasdecd7a62015-05-16 22:39:47 -0700496
497 if (fwd.permanent()) {
498 ruleBuilder.makePermanent();
499 } else {
500 ruleBuilder.makeTemporary(fwd.timeout());
501 }
502
503 ruleBuilder.forTable(FIB_TABLE);
504 return Collections.singletonList(ruleBuilder.build());
505 }
506
507 /**
508 * Next Objectives are stored as dummy groups for retrieval later
509 * when Forwarding Objectives reference the next objective id. At that point
510 * the dummy group is fetched from the distributed store and the enclosed
511 * treatment is applied as a flow rule action.
512 *
alshabibcaf1ca22015-06-25 15:18:16 -0700513 * @param nextObj the next objective of type simple
Saurav Dasdecd7a62015-05-16 22:39:47 -0700514 */
515 private void processSimpleNextObjective(NextObjective nextObj) {
516 // Simple next objective has a single treatment (not a collection)
Saurav Das49cb5a12016-01-16 22:54:07 -0800517 log.debug("Received nextObj {}", nextObj.id());
518 // delay processing to emulate group creation
519 delay(50);
Saurav Das6c44a632015-05-30 22:05:22 -0700520 TrafficTreatment treatment = nextObj.next().iterator().next();
Saurav Dasdecd7a62015-05-16 22:39:47 -0700521 flowObjectiveStore.putNextGroup(nextObj.id(),
Saurav Das6c44a632015-05-30 22:05:22 -0700522 new DummyGroup(treatment));
Saurav Dasdecd7a62015-05-16 22:39:47 -0700523 }
524
Saurav Dasdecd7a62015-05-16 22:39:47 -0700525 private class DummyGroup implements NextGroup {
526 TrafficTreatment nextActions;
527
528 public DummyGroup(TrafficTreatment next) {
529 this.nextActions = next;
530 }
531
532 @Override
533 public byte[] data() {
534 return appKryo.serialize(nextActions);
535 }
536
537 }
538
Saurav Das24431192016-03-07 19:13:00 -0800539 @Override
540 public List<String> getNextMappings(NextGroup nextGroup) {
541 // nextObjectives converted to flow-actions not groups
542 return Collections.emptyList();
543 }
544
Saurav Dasdecd7a62015-05-16 22:39:47 -0700545}