blob: 111a6c6f15a7a8e1919ec05b97ae53fc71218b11 [file] [log] [blame]
Saurav Das822c4e22015-10-23 10:51:11 -07001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
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
Saurav Das822c4e22015-10-23 10:51:11 -070018import static org.slf4j.LoggerFactory.getLogger;
19
Saurav Das8a0732e2015-11-20 15:27:53 -080020import java.util.ArrayDeque;
Saurav Das822c4e22015-10-23 10:51:11 -070021import java.util.ArrayList;
22import java.util.Collection;
23import java.util.Collections;
Saurav Das8a0732e2015-11-20 15:27:53 -080024import java.util.Deque;
Saurav Das822c4e22015-10-23 10:51:11 -070025import java.util.List;
Sho SHIMIZU45906042016-01-13 23:05:54 -080026import java.util.Objects;
Saurav Das822c4e22015-10-23 10:51:11 -070027import java.util.Set;
28import java.util.concurrent.ConcurrentHashMap;
Saurav Das822c4e22015-10-23 10:51:11 -070029
30import org.onlab.osgi.ServiceDirectory;
Saurav Das822c4e22015-10-23 10:51:11 -070031import org.onlab.packet.Ethernet;
Charles Chan14967c22015-12-07 11:11:50 -080032import org.onlab.packet.IpPrefix;
Saurav Das822c4e22015-10-23 10:51:11 -070033import org.onlab.packet.MacAddress;
Saurav Das822c4e22015-10-23 10:51:11 -070034import org.onlab.packet.VlanId;
35import org.onlab.util.KryoNamespace;
36import org.onosproject.core.ApplicationId;
37import org.onosproject.core.CoreService;
Charles Chan14967c22015-12-07 11:11:50 -080038import org.onosproject.driver.extensions.OfdpaMatchVlanVid;
39import org.onosproject.driver.extensions.OfdpaSetVlanVid;
Saurav Das822c4e22015-10-23 10:51:11 -070040import org.onosproject.net.DeviceId;
41import org.onosproject.net.Port;
42import org.onosproject.net.PortNumber;
43import org.onosproject.net.behaviour.NextGroup;
44import org.onosproject.net.behaviour.Pipeliner;
45import org.onosproject.net.behaviour.PipelinerContext;
46import org.onosproject.net.device.DeviceService;
47import org.onosproject.net.driver.AbstractHandlerBehaviour;
48import org.onosproject.net.flow.DefaultFlowRule;
49import org.onosproject.net.flow.DefaultTrafficSelector;
50import org.onosproject.net.flow.DefaultTrafficTreatment;
51import org.onosproject.net.flow.FlowRule;
52import org.onosproject.net.flow.FlowRuleOperations;
53import org.onosproject.net.flow.FlowRuleOperationsContext;
54import org.onosproject.net.flow.FlowRuleService;
55import org.onosproject.net.flow.TrafficSelector;
56import org.onosproject.net.flow.TrafficTreatment;
57import org.onosproject.net.flow.criteria.Criteria;
58import org.onosproject.net.flow.criteria.Criterion;
59import org.onosproject.net.flow.criteria.EthCriterion;
60import org.onosproject.net.flow.criteria.EthTypeCriterion;
Charles Chan14967c22015-12-07 11:11:50 -080061import org.onosproject.net.flow.criteria.ExtensionCriterion;
Saurav Das822c4e22015-10-23 10:51:11 -070062import org.onosproject.net.flow.criteria.IPCriterion;
Saurav Das8a0732e2015-11-20 15:27:53 -080063import org.onosproject.net.flow.criteria.MplsBosCriterion;
64import org.onosproject.net.flow.criteria.MplsCriterion;
Saurav Das822c4e22015-10-23 10:51:11 -070065import org.onosproject.net.flow.criteria.PortCriterion;
66import org.onosproject.net.flow.criteria.VlanIdCriterion;
67import org.onosproject.net.flow.instructions.Instruction;
68import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
69import org.onosproject.net.flow.instructions.L2ModificationInstruction;
Saurav Das8a0732e2015-11-20 15:27:53 -080070import org.onosproject.net.flow.instructions.L2ModificationInstruction.L2SubType;
Saurav Das822c4e22015-10-23 10:51:11 -070071import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanIdInstruction;
Charles Chan14967c22015-12-07 11:11:50 -080072import org.onosproject.net.flow.instructions.L3ModificationInstruction;
73import org.onosproject.net.flow.instructions.L3ModificationInstruction.L3SubType;
Saurav Das822c4e22015-10-23 10:51:11 -070074import org.onosproject.net.flowobjective.FilteringObjective;
75import org.onosproject.net.flowobjective.FlowObjectiveStore;
76import org.onosproject.net.flowobjective.ForwardingObjective;
77import org.onosproject.net.flowobjective.NextObjective;
78import org.onosproject.net.flowobjective.Objective;
79import org.onosproject.net.flowobjective.ObjectiveError;
Saurav Das822c4e22015-10-23 10:51:11 -070080import org.onosproject.net.group.DefaultGroupKey;
81import org.onosproject.net.group.Group;
Saurav Das822c4e22015-10-23 10:51:11 -070082import org.onosproject.net.group.GroupKey;
Saurav Das822c4e22015-10-23 10:51:11 -070083import org.onosproject.net.group.GroupService;
Saurav Das822c4e22015-10-23 10:51:11 -070084import org.onosproject.store.serializers.KryoNamespaces;
85import org.slf4j.Logger;
86
Saurav Das822c4e22015-10-23 10:51:11 -070087/**
88 * Driver for Broadcom's OF-DPA v2.0 TTP.
89 *
90 */
91public class OFDPA2Pipeline extends AbstractHandlerBehaviour implements Pipeliner {
Saurav Das822c4e22015-10-23 10:51:11 -070092 protected static final int PORT_TABLE = 0;
93 protected static final int VLAN_TABLE = 10;
94 protected static final int TMAC_TABLE = 20;
95 protected static final int UNICAST_ROUTING_TABLE = 30;
96 protected static final int MULTICAST_ROUTING_TABLE = 40;
97 protected static final int MPLS_TABLE_0 = 23;
98 protected static final int MPLS_TABLE_1 = 24;
99 protected static final int BRIDGING_TABLE = 50;
100 protected static final int ACL_TABLE = 60;
101 protected static final int MAC_LEARNING_TABLE = 254;
102 protected static final long OFPP_MAX = 0xffffff00L;
103
104 private static final int HIGHEST_PRIORITY = 0xffff;
Saurav Das2857f382015-11-03 14:39:27 -0800105 protected static final int DEFAULT_PRIORITY = 0x8000;
Saurav Das822c4e22015-10-23 10:51:11 -0700106 protected static final int LOWEST_PRIORITY = 0x0;
107
Saurav Das822c4e22015-10-23 10:51:11 -0700108 private final Logger log = getLogger(getClass());
109 private ServiceDirectory serviceDirectory;
110 protected FlowRuleService flowRuleService;
111 private CoreService coreService;
Saurav Das8a0732e2015-11-20 15:27:53 -0800112 protected GroupService groupService;
113 protected FlowObjectiveStore flowObjectiveStore;
Saurav Das822c4e22015-10-23 10:51:11 -0700114 protected DeviceId deviceId;
115 protected ApplicationId driverId;
Saurav Das822c4e22015-10-23 10:51:11 -0700116 protected DeviceService deviceService;
Charles Chan188ebf52015-12-23 00:15:11 -0800117 protected static KryoNamespace appKryo = new KryoNamespace.Builder()
Saurav Das822c4e22015-10-23 10:51:11 -0700118 .register(KryoNamespaces.API)
119 .register(GroupKey.class)
120 .register(DefaultGroupKey.class)
Andrea Campanella238d96e2016-01-20 11:52:02 -0800121 .register(OFDPA2GroupHandler.OfdpaNextGroup.class)
Saurav Das822c4e22015-10-23 10:51:11 -0700122 .register(byte[].class)
Saurav Das8a0732e2015-11-20 15:27:53 -0800123 .register(ArrayDeque.class)
Saurav Das822c4e22015-10-23 10:51:11 -0700124 .build();
125
Charles Chan188ebf52015-12-23 00:15:11 -0800126 protected OFDPA2GroupHandler ofdpa2GroupHandler;
Saurav Das822c4e22015-10-23 10:51:11 -0700127
Saurav Das822c4e22015-10-23 10:51:11 -0700128 private Set<IPCriterion> sentIpFilters = Collections.newSetFromMap(
Charles Chan188ebf52015-12-23 00:15:11 -0800129 new ConcurrentHashMap<>());
Saurav Das4f980082015-11-05 13:39:15 -0800130
Saurav Das822c4e22015-10-23 10:51:11 -0700131 @Override
132 public void init(DeviceId deviceId, PipelinerContext context) {
133 this.serviceDirectory = context.directory();
134 this.deviceId = deviceId;
135
Charles Chan188ebf52015-12-23 00:15:11 -0800136 // Initialize OFDPA group handler
137 ofdpa2GroupHandler = new OFDPA2GroupHandler();
138 ofdpa2GroupHandler.init(deviceId, context);
Saurav Das822c4e22015-10-23 10:51:11 -0700139
140 coreService = serviceDirectory.get(CoreService.class);
141 flowRuleService = serviceDirectory.get(FlowRuleService.class);
142 groupService = serviceDirectory.get(GroupService.class);
143 flowObjectiveStore = context.store();
Saurav Das822c4e22015-10-23 10:51:11 -0700144 deviceService = serviceDirectory.get(DeviceService.class);
Saurav Das822c4e22015-10-23 10:51:11 -0700145
146 driverId = coreService.registerApplication(
147 "org.onosproject.driver.OFDPA2Pipeline");
148
Saurav Das822c4e22015-10-23 10:51:11 -0700149 initializePipeline();
Saurav Das822c4e22015-10-23 10:51:11 -0700150 }
151
152 protected void initializePipeline() {
Charles Chan188ebf52015-12-23 00:15:11 -0800153 // OF-DPA does not require initializing the pipeline as it puts default
154 // rules automatically in the hardware. However emulation of OFDPA in
155 // software switches does require table-miss-entries.
Saurav Das822c4e22015-10-23 10:51:11 -0700156 }
157
158 //////////////////////////////////////
159 // Flow Objectives
160 //////////////////////////////////////
161
162 @Override
163 public void filter(FilteringObjective filteringObjective) {
164 if (filteringObjective.type() == FilteringObjective.Type.PERMIT) {
165 processFilter(filteringObjective,
166 filteringObjective.op() == Objective.Operation.ADD,
167 filteringObjective.appId());
168 } else {
169 // Note that packets that don't match the PERMIT filter are
170 // automatically denied. The DENY filter is used to deny packets
171 // that are otherwise permitted by the PERMIT filter.
172 // Use ACL table flow rules here for DENY filtering objectives
173 log.debug("filter objective other than PERMIT currently not supported");
174 fail(filteringObjective, ObjectiveError.UNSUPPORTED);
175 }
176 }
177
178 @Override
179 public void forward(ForwardingObjective fwd) {
180 Collection<FlowRule> rules;
181 FlowRuleOperations.Builder flowOpsBuilder = FlowRuleOperations.builder();
182
183 rules = processForward(fwd);
184 switch (fwd.op()) {
185 case ADD:
186 rules.stream()
Sho SHIMIZU45906042016-01-13 23:05:54 -0800187 .filter(Objects::nonNull)
Saurav Das822c4e22015-10-23 10:51:11 -0700188 .forEach(flowOpsBuilder::add);
189 break;
190 case REMOVE:
191 rules.stream()
Sho SHIMIZU45906042016-01-13 23:05:54 -0800192 .filter(Objects::nonNull)
Saurav Das822c4e22015-10-23 10:51:11 -0700193 .forEach(flowOpsBuilder::remove);
194 break;
195 default:
196 fail(fwd, ObjectiveError.UNKNOWN);
197 log.warn("Unknown forwarding type {}", fwd.op());
198 }
199
Saurav Das822c4e22015-10-23 10:51:11 -0700200 flowRuleService.apply(flowOpsBuilder.build(new FlowRuleOperationsContext() {
201 @Override
202 public void onSuccess(FlowRuleOperations ops) {
203 pass(fwd);
204 }
205
206 @Override
207 public void onError(FlowRuleOperations ops) {
208 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
209 }
210 }));
Saurav Das822c4e22015-10-23 10:51:11 -0700211 }
212
213 @Override
214 public void next(NextObjective nextObjective) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800215 NextGroup nextGroup = flowObjectiveStore.getNextGroup(nextObjective.id());
216 switch (nextObjective.op()) {
217 case ADD:
Saurav Das4f980082015-11-05 13:39:15 -0800218 if (nextGroup != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800219 log.warn("Cannot add next {} that already exists in device {}",
220 nextObjective.id(), deviceId);
221 return;
222 }
223 log.debug("Processing NextObjective id{} in dev{} - add group",
224 nextObjective.id(), deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -0800225 ofdpa2GroupHandler.addGroup(nextObjective);
Saurav Das8a0732e2015-11-20 15:27:53 -0800226 break;
227 case ADD_TO_EXISTING:
228 if (nextGroup != null) {
229 log.debug("Processing NextObjective id{} in dev{} - add bucket",
230 nextObjective.id(), deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -0800231 ofdpa2GroupHandler.addBucketToGroup(nextObjective, nextGroup);
Saurav Das4f980082015-11-05 13:39:15 -0800232 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800233 // it is possible that group-chain has not been fully created yet
Saurav Das423fe2b2015-12-04 10:52:59 -0800234 log.debug("Waiting to add bucket to group for next-id:{} in dev:{}",
235 nextObjective.id(), deviceId);
236 // by design only one pending bucket is allowed for the group
Charles Chan188ebf52015-12-23 00:15:11 -0800237 ofdpa2GroupHandler.pendingBuckets.put(nextObjective.id(), nextObjective);
Saurav Das4f980082015-11-05 13:39:15 -0800238 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800239 break;
240 case REMOVE:
241 if (nextGroup == null) {
242 log.warn("Cannot remove next {} that does not exist in device {}",
243 nextObjective.id(), deviceId);
244 return;
245 }
246 log.debug("Processing NextObjective id{} in dev{} - remove group",
247 nextObjective.id(), deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -0800248 ofdpa2GroupHandler.removeGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800249 break;
250 case REMOVE_FROM_EXISTING:
251 if (nextGroup == null) {
252 log.warn("Cannot remove from next {} that does not exist in device {}",
253 nextObjective.id(), deviceId);
254 return;
255 }
256 log.debug("Processing NextObjective id{} in dev{} - remove bucket",
257 nextObjective.id(), deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -0800258 ofdpa2GroupHandler.removeBucketFromGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800259 break;
260 default:
Saurav Das4f980082015-11-05 13:39:15 -0800261 log.warn("Unsupported operation {}", nextObjective.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700262 }
263 }
264
265 //////////////////////////////////////
266 // Flow handling
267 //////////////////////////////////////
268
269 /**
270 * As per OFDPA 2.0 TTP, filtering of VLAN ids, MAC addresses (for routing)
271 * and IP addresses configured on switch ports happen in different tables.
272 * Note that IP filtering rules need to be added to the ACL table, as there
273 * is no mechanism to send to controller via IP table.
274 *
275 * @param filt the filtering objective
276 * @param install indicates whether to add or remove the objective
277 * @param applicationId the application that sent this objective
278 */
279 private void processFilter(FilteringObjective filt,
280 boolean install, ApplicationId applicationId) {
281 // This driver only processes filtering criteria defined with switch
282 // ports as the key
283 PortCriterion portCriterion = null;
284 EthCriterion ethCriterion = null;
285 VlanIdCriterion vidCriterion = null;
286 Collection<IPCriterion> ips = new ArrayList<IPCriterion>();
287 if (!filt.key().equals(Criteria.dummy()) &&
288 filt.key().type() == Criterion.Type.IN_PORT) {
289 portCriterion = (PortCriterion) filt.key();
290 } else {
291 log.warn("No key defined in filtering objective from app: {}. Not"
292 + "processing filtering objective", applicationId);
293 fail(filt, ObjectiveError.UNKNOWN);
294 return;
295 }
296 // convert filtering conditions for switch-intfs into flowrules
297 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
298 for (Criterion criterion : filt.conditions()) {
299 if (criterion.type() == Criterion.Type.ETH_DST) {
300 ethCriterion = (EthCriterion) criterion;
301 } else if (criterion.type() == Criterion.Type.VLAN_VID) {
302 vidCriterion = (VlanIdCriterion) criterion;
303 } else if (criterion.type() == Criterion.Type.IPV4_DST) {
304 ips.add((IPCriterion) criterion);
305 } else {
306 log.error("Unsupported filter {}", criterion);
307 fail(filt, ObjectiveError.UNSUPPORTED);
308 return;
309 }
310 }
311
Saurav Das0e99e2b2015-10-28 12:39:42 -0700312 VlanId assignedVlan = null;
Charles Chane849c192016-01-11 18:28:54 -0800313 // For VLAN cross-connect packets, use the configured VLAN
314 if (vidCriterion != null) {
315 if (vidCriterion.vlanId() != VlanId.NONE) {
316 assignedVlan = vidCriterion.vlanId();
317
318 // For untagged packets, assign a VLAN ID
319 } else {
320 if (filt.meta() == null) {
321 log.error("Missing metadata in filtering objective required " +
322 "for vlan assignment in dev {}", deviceId);
323 fail(filt, ObjectiveError.BADPARAMS);
324 return;
Saurav Das0e99e2b2015-10-28 12:39:42 -0700325 }
Charles Chane849c192016-01-11 18:28:54 -0800326 for (Instruction i : filt.meta().allInstructions()) {
327 if (i instanceof ModVlanIdInstruction) {
328 assignedVlan = ((ModVlanIdInstruction) i).vlanId();
329 }
330 }
331 if (assignedVlan == null) {
332 log.error("Driver requires an assigned vlan-id to tag incoming "
333 + "untagged packets. Not processing vlan filters on "
334 + "device {}", deviceId);
335 fail(filt, ObjectiveError.BADPARAMS);
336 return;
337 }
Saurav Das0e99e2b2015-10-28 12:39:42 -0700338 }
339 }
340
Charles Chane849c192016-01-11 18:28:54 -0800341 if (ethCriterion == null || ethCriterion.mac().equals(MacAddress.NONE)) {
Saurav Das822c4e22015-10-23 10:51:11 -0700342 log.debug("filtering objective missing dstMac, cannot program TMAC table");
343 } else {
344 for (FlowRule tmacRule : processEthDstFilter(portCriterion, ethCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700345 vidCriterion, assignedVlan,
346 applicationId)) {
Saurav Das822c4e22015-10-23 10:51:11 -0700347 log.debug("adding MAC filtering rules in TMAC table: {} for dev: {}",
348 tmacRule, deviceId);
349 ops = install ? ops.add(tmacRule) : ops.remove(tmacRule);
350 }
351 }
352
353 if (ethCriterion == null || vidCriterion == null) {
Charles Chane849c192016-01-11 18:28:54 -0800354 log.debug("filtering objective missing dstMac or VLAN, "
355 + "cannot program VLAN Table");
Saurav Das822c4e22015-10-23 10:51:11 -0700356 } else {
Charles Chan14967c22015-12-07 11:11:50 -0800357 /*
358 * NOTE: Separate vlan filtering rules and assignment rules
359 * into different stage in order to guarantee that filtering rules
360 * always go first.
361 */
362 List<FlowRule> allRules = processVlanIdFilter(
363 portCriterion, vidCriterion, assignedVlan, applicationId);
364 List<FlowRule> filteringRules = new ArrayList<>();
365 List<FlowRule> assignmentRules = new ArrayList<>();
366
367 allRules.forEach(flowRule -> {
368 ExtensionCriterion extCriterion =
369 (ExtensionCriterion) flowRule.selector().getCriterion(Criterion.Type.EXTENSION);
370 VlanId vlanId = ((OfdpaMatchVlanVid) extCriterion.extensionSelector()).vlanId();
371 if (vlanId.toShort() != (short) 0) {
372 filteringRules.add(flowRule);
373 } else {
374 assignmentRules.add(flowRule);
375 }
376 });
377
378 for (FlowRule filteringRule : filteringRules) {
Saurav Das822c4e22015-10-23 10:51:11 -0700379 log.debug("adding VLAN filtering rule in VLAN table: {} for dev: {}",
Charles Chan14967c22015-12-07 11:11:50 -0800380 filteringRule, deviceId);
381 ops = install ? ops.add(filteringRule) : ops.remove(filteringRule);
382 }
383
384 ops.newStage();
385
386 for (FlowRule assignmentRule : assignmentRules) {
387 log.debug("adding VLAN assignment rule in VLAN table: {} for dev: {}",
388 assignmentRule, deviceId);
389 ops = install ? ops.add(assignmentRule) : ops.remove(assignmentRule);
Saurav Das822c4e22015-10-23 10:51:11 -0700390 }
391 }
392
393 for (IPCriterion ipaddr : ips) {
394 // since we ignore port information for IP rules, and the same (gateway) IP
395 // can be configured on multiple ports, we make sure that we send
396 // only a single rule to the switch.
397 if (!sentIpFilters.contains(ipaddr)) {
398 sentIpFilters.add(ipaddr);
399 log.debug("adding IP filtering rules in ACL table {} for dev: {}",
400 ipaddr, deviceId);
401 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
402 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
403 selector.matchEthType(Ethernet.TYPE_IPV4);
404 selector.matchIPDst(ipaddr.ip());
405 treatment.setOutput(PortNumber.CONTROLLER);
406 FlowRule rule = DefaultFlowRule.builder()
407 .forDevice(deviceId)
408 .withSelector(selector.build())
409 .withTreatment(treatment.build())
410 .withPriority(HIGHEST_PRIORITY)
411 .fromApp(applicationId)
412 .makePermanent()
413 .forTable(ACL_TABLE).build();
414 ops = install ? ops.add(rule) : ops.remove(rule);
415 }
416 }
417
418 // apply filtering flow rules
419 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
420 @Override
421 public void onSuccess(FlowRuleOperations ops) {
422 log.info("Applied {} filtering rules in device {}",
423 ops.stages().get(0).size(), deviceId);
424 pass(filt);
425 }
426
427 @Override
428 public void onError(FlowRuleOperations ops) {
429 log.info("Failed to apply all filtering rules in dev {}", deviceId);
430 fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED);
431 }
432 }));
433
434 }
435
436 /**
437 * Allows untagged packets into pipeline by assigning a vlan id.
Saurav Das0e99e2b2015-10-28 12:39:42 -0700438 * Vlan assignment is done by the application.
Saurav Das822c4e22015-10-23 10:51:11 -0700439 * Allows tagged packets into pipeline as per configured port-vlan info.
Saurav Das0e99e2b2015-10-28 12:39:42 -0700440 *
Saurav Das822c4e22015-10-23 10:51:11 -0700441 * @param portCriterion port on device for which this filter is programmed
442 * @param vidCriterion vlan assigned to port, or NONE for untagged
Saurav Das0e99e2b2015-10-28 12:39:42 -0700443 * @param assignedVlan assigned vlan-id for untagged packets
Saurav Das822c4e22015-10-23 10:51:11 -0700444 * @param applicationId for application programming this filter
445 * @return list of FlowRule for port-vlan filters
446 */
447 protected List<FlowRule> processVlanIdFilter(PortCriterion portCriterion,
448 VlanIdCriterion vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700449 VlanId assignedVlan,
Saurav Das822c4e22015-10-23 10:51:11 -0700450 ApplicationId applicationId) {
Charles Chan14967c22015-12-07 11:11:50 -0800451 List<FlowRule> rules = new ArrayList<>();
Saurav Das822c4e22015-10-23 10:51:11 -0700452 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
453 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800454 TrafficSelector.Builder preSelector = null;
455 TrafficTreatment.Builder preTreatment = null;
456
457
Saurav Das4f980082015-11-05 13:39:15 -0800458 treatment.transition(TMAC_TABLE);
459
460 VlanId storeVlan = null;
Saurav Das822c4e22015-10-23 10:51:11 -0700461 if (vidCriterion.vlanId() == VlanId.NONE) {
462 // untagged packets are assigned vlans
Charles Chan14967c22015-12-07 11:11:50 -0800463 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(VlanId.vlanId((short) 0));
464 selector.extension(ofdpaMatchVlanVid, deviceId);
465 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
466 treatment.extension(ofdpaSetVlanVid, deviceId);
Saurav Das2857f382015-11-03 14:39:27 -0800467 // XXX ofdpa will require an additional vlan match on the assigned vlan
468 // and it may not require the push. This is not in compliance with OF
469 // standard. Waiting on what the exact flows are going to look like.
Saurav Das4f980082015-11-05 13:39:15 -0800470 storeVlan = assignedVlan;
Charles Chan14967c22015-12-07 11:11:50 -0800471
472 preSelector = DefaultTrafficSelector.builder();
473 OfdpaMatchVlanVid preOfdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
474 preSelector.extension(preOfdpaMatchVlanVid, deviceId);
475 preTreatment = DefaultTrafficTreatment.builder().transition(TMAC_TABLE);
476
Saurav Das4f980082015-11-05 13:39:15 -0800477 } else {
Charles Chan14967c22015-12-07 11:11:50 -0800478 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
479 selector.extension(ofdpaMatchVlanVid, deviceId);
Saurav Das4f980082015-11-05 13:39:15 -0800480 storeVlan = vidCriterion.vlanId();
Saurav Das822c4e22015-10-23 10:51:11 -0700481 }
Saurav Das822c4e22015-10-23 10:51:11 -0700482
483 // ofdpa cannot match on ALL portnumber, so we need to use separate
484 // rules for each port.
Charles Chan14967c22015-12-07 11:11:50 -0800485 List<PortNumber> portnums = new ArrayList<>();
Saurav Das822c4e22015-10-23 10:51:11 -0700486 if (portCriterion.port() == PortNumber.ALL) {
487 for (Port port : deviceService.getPorts(deviceId)) {
488 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
489 portnums.add(port.number());
490 }
491 }
492 } else {
493 portnums.add(portCriterion.port());
494 }
Saurav Das4f980082015-11-05 13:39:15 -0800495
Saurav Das822c4e22015-10-23 10:51:11 -0700496 for (PortNumber pnum : portnums) {
Saurav Das4f980082015-11-05 13:39:15 -0800497 // update storage
Charles Chan188ebf52015-12-23 00:15:11 -0800498 ofdpa2GroupHandler.port2Vlan.put(pnum, storeVlan);
499 Set<PortNumber> vlanPorts = ofdpa2GroupHandler.vlan2Port.get(storeVlan);
Saurav Das4f980082015-11-05 13:39:15 -0800500 if (vlanPorts == null) {
501 vlanPorts = Collections.newSetFromMap(
502 new ConcurrentHashMap<PortNumber, Boolean>());
503 vlanPorts.add(pnum);
Charles Chan188ebf52015-12-23 00:15:11 -0800504 ofdpa2GroupHandler.vlan2Port.put(storeVlan, vlanPorts);
Saurav Das4f980082015-11-05 13:39:15 -0800505 } else {
506 vlanPorts.add(pnum);
507 }
508 // create rest of flowrule
Saurav Das822c4e22015-10-23 10:51:11 -0700509 selector.matchInPort(pnum);
510 FlowRule rule = DefaultFlowRule.builder()
511 .forDevice(deviceId)
512 .withSelector(selector.build())
513 .withTreatment(treatment.build())
514 .withPriority(DEFAULT_PRIORITY)
515 .fromApp(applicationId)
516 .makePermanent()
517 .forTable(VLAN_TABLE).build();
Charles Chan14967c22015-12-07 11:11:50 -0800518
519 if (preSelector != null) {
520 preSelector.matchInPort(pnum);
521 FlowRule preRule = DefaultFlowRule.builder()
522 .forDevice(deviceId)
523 .withSelector(preSelector.build())
524 .withTreatment(preTreatment.build())
525 .withPriority(DEFAULT_PRIORITY)
526 .fromApp(applicationId)
527 .makePermanent()
528 .forTable(VLAN_TABLE).build();
529 rules.add(preRule);
530 }
531
Saurav Das822c4e22015-10-23 10:51:11 -0700532 rules.add(rule);
533 }
534 return rules;
535 }
536
537 /**
538 * Allows routed packets with correct destination MAC to be directed
539 * to unicast-IP routing table or MPLS forwarding table.
Saurav Das822c4e22015-10-23 10:51:11 -0700540 *
541 * @param portCriterion port on device for which this filter is programmed
542 * @param ethCriterion dstMac of device for which is filter is programmed
543 * @param vidCriterion vlan assigned to port, or NONE for untagged
Saurav Das0e99e2b2015-10-28 12:39:42 -0700544 * @param assignedVlan assigned vlan-id for untagged packets
Saurav Das822c4e22015-10-23 10:51:11 -0700545 * @param applicationId for application programming this filter
546 * @return list of FlowRule for port-vlan filters
547
548 */
549 protected List<FlowRule> processEthDstFilter(PortCriterion portCriterion,
550 EthCriterion ethCriterion,
551 VlanIdCriterion vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700552 VlanId assignedVlan,
Saurav Das822c4e22015-10-23 10:51:11 -0700553 ApplicationId applicationId) {
554 //handling untagged packets via assigned VLAN
555 if (vidCriterion.vlanId() == VlanId.NONE) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700556 vidCriterion = (VlanIdCriterion) Criteria.matchVlanId(assignedVlan);
Saurav Das822c4e22015-10-23 10:51:11 -0700557 }
558 // ofdpa cannot match on ALL portnumber, so we need to use separate
559 // rules for each port.
560 List<PortNumber> portnums = new ArrayList<PortNumber>();
561 if (portCriterion.port() == PortNumber.ALL) {
562 for (Port port : deviceService.getPorts(deviceId)) {
563 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
564 portnums.add(port.number());
565 }
566 }
567 } else {
568 portnums.add(portCriterion.port());
569 }
570
571 List<FlowRule> rules = new ArrayList<FlowRule>();
572 for (PortNumber pnum : portnums) {
Charles Chan14967c22015-12-07 11:11:50 -0800573 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
Saurav Das822c4e22015-10-23 10:51:11 -0700574 // for unicast IP packets
575 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
576 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
577 selector.matchInPort(pnum);
Charles Chan14967c22015-12-07 11:11:50 -0800578 selector.extension(ofdpaMatchVlanVid, deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700579 selector.matchEthType(Ethernet.TYPE_IPV4);
580 selector.matchEthDst(ethCriterion.mac());
581 treatment.transition(UNICAST_ROUTING_TABLE);
582 FlowRule rule = DefaultFlowRule.builder()
583 .forDevice(deviceId)
584 .withSelector(selector.build())
585 .withTreatment(treatment.build())
586 .withPriority(DEFAULT_PRIORITY)
587 .fromApp(applicationId)
588 .makePermanent()
589 .forTable(TMAC_TABLE).build();
590 rules.add(rule);
591 //for MPLS packets
592 selector = DefaultTrafficSelector.builder();
593 treatment = DefaultTrafficTreatment.builder();
594 selector.matchInPort(pnum);
Charles Chan14967c22015-12-07 11:11:50 -0800595 selector.extension(ofdpaMatchVlanVid, deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700596 selector.matchEthType(Ethernet.MPLS_UNICAST);
597 selector.matchEthDst(ethCriterion.mac());
598 treatment.transition(MPLS_TABLE_0);
599 rule = DefaultFlowRule.builder()
600 .forDevice(deviceId)
601 .withSelector(selector.build())
602 .withTreatment(treatment.build())
603 .withPriority(DEFAULT_PRIORITY)
604 .fromApp(applicationId)
605 .makePermanent()
606 .forTable(TMAC_TABLE).build();
607 rules.add(rule);
608 }
609 return rules;
610 }
611
612 private Collection<FlowRule> processForward(ForwardingObjective fwd) {
613 switch (fwd.flag()) {
614 case SPECIFIC:
615 return processSpecific(fwd);
616 case VERSATILE:
617 return processVersatile(fwd);
618 default:
619 fail(fwd, ObjectiveError.UNKNOWN);
620 log.warn("Unknown forwarding flag {}", fwd.flag());
621 }
622 return Collections.emptySet();
623 }
624
625 /**
626 * In the OF-DPA 2.0 pipeline, versatile forwarding objectives go to the
627 * ACL table.
628 * @param fwd the forwarding objective of type 'versatile'
629 * @return a collection of flow rules to be sent to the switch. An empty
630 * collection may be returned if there is a problem in processing
631 * the flow rule
632 */
633 private Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
634 log.info("Processing versatile forwarding objective");
635 TrafficSelector selector = fwd.selector();
636
637 EthTypeCriterion ethType =
638 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
639 if (ethType == null) {
640 log.error("Versatile forwarding objective must include ethType");
641 fail(fwd, ObjectiveError.BADPARAMS);
642 return Collections.emptySet();
643 }
644 if (fwd.nextId() == null && fwd.treatment() == null) {
645 log.error("Forwarding objective {} from {} must contain "
646 + "nextId or Treatment", fwd.selector(), fwd.appId());
647 return Collections.emptySet();
648 }
Saurav Das49cb5a12016-01-16 22:54:07 -0800649
Saurav Das822c4e22015-10-23 10:51:11 -0700650 // XXX driver does not currently do type checking as per Tables 65-67 in
651 // OFDPA 2.0 spec. The only allowed treatment is a punt to the controller.
Saurav Das49cb5a12016-01-16 22:54:07 -0800652 TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder();
653 if (fwd.treatment() != null) {
654 for (Instruction ins : fwd.treatment().allInstructions()) {
655 if (ins instanceof OutputInstruction) {
656 OutputInstruction o = (OutputInstruction) ins;
657 if (o.port() == PortNumber.CONTROLLER) {
658 ttBuilder.add(o);
659 } else {
660 log.warn("Only allowed treatments in versatile forwarding "
661 + "objectives are punts to the controller");
662 }
663 } else {
664 log.warn("Cannot process instruction in versatile fwd {}", ins);
665 }
Saurav Das822c4e22015-10-23 10:51:11 -0700666 }
667 }
Saurav Das822c4e22015-10-23 10:51:11 -0700668 if (fwd.nextId() != null) {
Saurav Das49cb5a12016-01-16 22:54:07 -0800669 // overide case
670 NextGroup next = getGroupForNextObjective(fwd.nextId());
671 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
672 // we only need the top level group's key to point the flow to it
673 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
674 if (group == null) {
675 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
676 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
677 fail(fwd, ObjectiveError.GROUPMISSING);
678 return Collections.emptySet();
679 }
680 ttBuilder.deferred().group(group.id());
Saurav Das822c4e22015-10-23 10:51:11 -0700681 }
Saurav Das49cb5a12016-01-16 22:54:07 -0800682 // ensure that match does not include vlan = NONE as OF-DPA does not
683 // match untagged packets this way in the ACL table.
684 TrafficSelector.Builder selBuilder = DefaultTrafficSelector.builder();
685 for (Criterion criterion : fwd.selector().criteria()) {
686 if (criterion instanceof VlanIdCriterion &&
687 ((VlanIdCriterion) criterion).vlanId() == VlanId.NONE) {
688 continue;
689 }
690 selBuilder.add(criterion);
691 }
692
693 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
694 .fromApp(fwd.appId())
695 .withPriority(fwd.priority())
696 .forDevice(deviceId)
697 .withSelector(selBuilder.build())
698 .withTreatment(ttBuilder.build())
699 .makePermanent()
700 .forTable(ACL_TABLE);
701 return Collections.singletonList(ruleBuilder.build());
Saurav Das822c4e22015-10-23 10:51:11 -0700702 }
703
704 /**
705 * In the OF-DPA 2.0 pipeline, specific forwarding refers to the IP table
Saurav Das8a0732e2015-11-20 15:27:53 -0800706 * (unicast or multicast) or the L2 table (mac + vlan) or the MPLS table.
Saurav Das822c4e22015-10-23 10:51:11 -0700707 *
708 * @param fwd the forwarding objective of type 'specific'
709 * @return a collection of flow rules. Typically there will be only one
710 * for this type of forwarding objective. An empty set may be
711 * returned if there is an issue in processing the objective.
712 */
Saurav Das8a0732e2015-11-20 15:27:53 -0800713 protected Collection<FlowRule> processSpecific(ForwardingObjective fwd) {
Saurav Das4ce45962015-11-24 23:21:05 -0800714 log.trace("Processing specific fwd objective:{} in dev:{} with next:{}",
715 fwd.id(), deviceId, fwd.nextId());
716 boolean isEthTypeObj = isSupportedEthTypeObjective(fwd);
717 boolean isEthDstObj = isSupportedEthDstObjective(fwd);
718
719 if (isEthTypeObj) {
720 return processEthTypeSpecific(fwd);
721 } else if (isEthDstObj) {
722 return processEthDstSpecific(fwd);
723 } else {
724 log.warn("processSpecific: Unsupported forwarding objective "
725 + "criteria fwd:{} in dev:{}", fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700726 fail(fwd, ObjectiveError.UNSUPPORTED);
727 return Collections.emptySet();
728 }
Saurav Das4ce45962015-11-24 23:21:05 -0800729 }
730
731 private boolean isSupportedEthTypeObjective(ForwardingObjective fwd) {
732 TrafficSelector selector = fwd.selector();
733 EthTypeCriterion ethType = (EthTypeCriterion) selector
734 .getCriterion(Criterion.Type.ETH_TYPE);
Charles Chan188ebf52015-12-23 00:15:11 -0800735 return !((ethType == null) ||
Saurav Das4ce45962015-11-24 23:21:05 -0800736 ((ethType.ethType().toShort() != Ethernet.TYPE_IPV4) &&
Charles Chan188ebf52015-12-23 00:15:11 -0800737 (ethType.ethType().toShort() != Ethernet.MPLS_UNICAST)));
Saurav Das4ce45962015-11-24 23:21:05 -0800738 }
739
740 private boolean isSupportedEthDstObjective(ForwardingObjective fwd) {
741 TrafficSelector selector = fwd.selector();
742 EthCriterion ethDst = (EthCriterion) selector
743 .getCriterion(Criterion.Type.ETH_DST);
744 VlanIdCriterion vlanId = (VlanIdCriterion) selector
745 .getCriterion(Criterion.Type.VLAN_VID);
Charles Chan188ebf52015-12-23 00:15:11 -0800746 return !(ethDst == null && vlanId == null);
Saurav Das4ce45962015-11-24 23:21:05 -0800747 }
748
749 /**
750 * Handles forwarding rules to the IP and MPLS tables.
751 *
752 * @param fwd the forwarding objective
753 * @return A collection of flow rules, or an empty set
754 */
755 protected Collection<FlowRule> processEthTypeSpecific(ForwardingObjective fwd) {
756 TrafficSelector selector = fwd.selector();
757 EthTypeCriterion ethType =
758 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
Saurav Das822c4e22015-10-23 10:51:11 -0700759
Charles Chan188ebf52015-12-23 00:15:11 -0800760 int forTableId;
Saurav Das8a0732e2015-11-20 15:27:53 -0800761 TrafficSelector.Builder filteredSelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800762 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
763 boolean popMpls = false;
764
Saurav Das8a0732e2015-11-20 15:27:53 -0800765 if (ethType.ethType().toShort() == Ethernet.TYPE_IPV4) {
Charles Chan14967c22015-12-07 11:11:50 -0800766 IpPrefix ipPrefix = ((IPCriterion)
767 selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
Saurav Das8a0732e2015-11-20 15:27:53 -0800768 filteredSelector.matchEthType(Ethernet.TYPE_IPV4)
Charles Chan14967c22015-12-07 11:11:50 -0800769 .matchIPDst(ipPrefix);
Saurav Das8a0732e2015-11-20 15:27:53 -0800770 forTableId = UNICAST_ROUTING_TABLE;
Saurav Das4ce45962015-11-24 23:21:05 -0800771 log.debug("processing IPv4 specific forwarding objective {} -> next:{}"
772 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Charles Chan14967c22015-12-07 11:11:50 -0800773
774 if (fwd.treatment() != null) {
775 for (Instruction instr : fwd.treatment().allInstructions()) {
776 if (instr instanceof L3ModificationInstruction &&
777 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
778 tb.deferred().add(instr);
779 }
780 }
781 }
782
Saurav Das8a0732e2015-11-20 15:27:53 -0800783 } else {
784 filteredSelector
785 .matchEthType(Ethernet.MPLS_UNICAST)
786 .matchMplsLabel(((MplsCriterion)
787 selector.getCriterion(Criterion.Type.MPLS_LABEL)).label());
788 MplsBosCriterion bos = (MplsBosCriterion) selector
789 .getCriterion(Criterion.Type.MPLS_BOS);
790 if (bos != null) {
791 filteredSelector.matchMplsBos(bos.mplsBos());
792 }
793 forTableId = MPLS_TABLE_1;
Saurav Das4ce45962015-11-24 23:21:05 -0800794 log.debug("processing MPLS specific forwarding objective {} -> next:{}"
795 + " in dev {}", fwd.id(), fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700796
Charles Chan14967c22015-12-07 11:11:50 -0800797 if (fwd.treatment() != null) {
798 for (Instruction instr : fwd.treatment().allInstructions()) {
799 if (instr instanceof L2ModificationInstruction &&
800 ((L2ModificationInstruction) instr).subtype() == L2SubType.MPLS_POP) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800801 popMpls = true;
Charles Chan14967c22015-12-07 11:11:50 -0800802 tb.immediate().add(instr);
803 }
804 if (instr instanceof L3ModificationInstruction &&
805 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
806 // FIXME Should modify the app to send the correct DEC_MPLS_TTL instruction
807 tb.immediate().decMplsTtl();
808 }
809 if (instr instanceof L3ModificationInstruction &&
810 ((L3ModificationInstruction) instr).subtype() == L3SubType.TTL_IN) {
811 tb.immediate().add(instr);
812 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800813 }
814 }
815 }
Saurav Das822c4e22015-10-23 10:51:11 -0700816
817 if (fwd.nextId() != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800818 if (forTableId == MPLS_TABLE_1 && !popMpls) {
819 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
820 + "is not implemented in OF-DPA yet. Aborting this flow "
821 + "in this device {}", deviceId);
822 // XXX We could convert to forwarding to a single-port, via a
823 // MPLS interface, or a MPLS SWAP (with-same) but that would
824 // have to be handled in the next-objective. Also the pop-mpls
825 // logic used here won't work in non-BoS case.
Saurav Das4ce45962015-11-24 23:21:05 -0800826 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
Saurav Das8a0732e2015-11-20 15:27:53 -0800827 return Collections.emptySet();
828 }
829
Saurav Das423fe2b2015-12-04 10:52:59 -0800830 NextGroup next = getGroupForNextObjective(fwd.nextId());
831 if (next != null) {
832 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
833 // we only need the top level group's key to point the flow to it
834 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
835 if (group == null) {
836 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
837 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
838 fail(fwd, ObjectiveError.GROUPMISSING);
839 return Collections.emptySet();
840 }
841 tb.deferred().group(group.id());
Saurav Das822c4e22015-10-23 10:51:11 -0700842 }
Saurav Das822c4e22015-10-23 10:51:11 -0700843 }
844 tb.transition(ACL_TABLE);
845 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
846 .fromApp(fwd.appId())
847 .withPriority(fwd.priority())
848 .forDevice(deviceId)
Saurav Das8a0732e2015-11-20 15:27:53 -0800849 .withSelector(filteredSelector.build())
850 .withTreatment(tb.build())
851 .forTable(forTableId);
Saurav Das822c4e22015-10-23 10:51:11 -0700852
853 if (fwd.permanent()) {
854 ruleBuilder.makePermanent();
855 } else {
856 ruleBuilder.makeTemporary(fwd.timeout());
857 }
858
Saurav Das822c4e22015-10-23 10:51:11 -0700859 return Collections.singletonList(ruleBuilder.build());
860 }
861
Saurav Das4ce45962015-11-24 23:21:05 -0800862 /**
863 * Handles forwarding rules to the L2 bridging table. Flow actions are not
864 * allowed in the bridging table - instead we use L2 Interface group or
865 * L2 flood group
866 *
867 * @param fwd the forwarding objective
868 * @return A collection of flow rules, or an empty set
869 */
870 protected Collection<FlowRule> processEthDstSpecific(ForwardingObjective fwd) {
871 List<FlowRule> rules = new ArrayList<>();
872
873 // Build filtered selector
874 TrafficSelector selector = fwd.selector();
875 EthCriterion ethCriterion = (EthCriterion) selector
876 .getCriterion(Criterion.Type.ETH_DST);
877 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) selector
878 .getCriterion(Criterion.Type.VLAN_VID);
879
880 if (vlanIdCriterion == null) {
881 log.warn("Forwarding objective for bridging requires vlan. Not "
882 + "installing fwd:{} in dev:{}", fwd.id(), deviceId);
883 fail(fwd, ObjectiveError.BADPARAMS);
884 return Collections.emptySet();
885 }
886
887 TrafficSelector.Builder filteredSelectorBuilder =
888 DefaultTrafficSelector.builder();
889 // Do not match MacAddress for subnet broadcast entry
890 if (!ethCriterion.mac().equals(MacAddress.NONE)) {
891 filteredSelectorBuilder.matchEthDst(ethCriterion.mac());
892 log.debug("processing L2 forwarding objective:{} -> next:{} in dev:{}",
893 fwd.id(), fwd.nextId(), deviceId);
894 } else {
895 log.debug("processing L2 Broadcast forwarding objective:{} -> next:{} "
896 + "in dev:{} for vlan:{}",
897 fwd.id(), fwd.nextId(), deviceId, vlanIdCriterion.vlanId());
898 }
Charles Chan14967c22015-12-07 11:11:50 -0800899 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanIdCriterion.vlanId());
900 filteredSelectorBuilder.extension(ofdpaMatchVlanVid, deviceId);
Saurav Das4ce45962015-11-24 23:21:05 -0800901 TrafficSelector filteredSelector = filteredSelectorBuilder.build();
902
903 if (fwd.treatment() != null) {
904 log.warn("Ignoring traffic treatment in fwd rule {} meant for L2 table"
905 + "for dev:{}. Expecting only nextId", fwd.id(), deviceId);
906 }
907
908 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
909 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -0800910 NextGroup next = getGroupForNextObjective(fwd.nextId());
Saurav Das4ce45962015-11-24 23:21:05 -0800911 if (next != null) {
912 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
913 // we only need the top level group's key to point the flow to it
914 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
915 if (group != null) {
916 treatmentBuilder.deferred().group(group.id());
917 } else {
918 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
919 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
920 fail(fwd, ObjectiveError.GROUPMISSING);
921 return Collections.emptySet();
922 }
923 }
924 }
925 treatmentBuilder.immediate().transition(ACL_TABLE);
926 TrafficTreatment filteredTreatment = treatmentBuilder.build();
927
928 // Build bridging table entries
929 FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder();
930 flowRuleBuilder.fromApp(fwd.appId())
931 .withPriority(fwd.priority())
932 .forDevice(deviceId)
933 .withSelector(filteredSelector)
934 .withTreatment(filteredTreatment)
935 .forTable(BRIDGING_TABLE);
936 if (fwd.permanent()) {
937 flowRuleBuilder.makePermanent();
938 } else {
939 flowRuleBuilder.makeTemporary(fwd.timeout());
940 }
941 rules.add(flowRuleBuilder.build());
942 return rules;
943 }
944
Saurav Das423fe2b2015-12-04 10:52:59 -0800945 protected NextGroup getGroupForNextObjective(Integer nextId) {
946 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
947 if (next != null) {
948 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
949 if (gkeys != null && !gkeys.isEmpty()) {
950 return next;
951 } else {
952 log.warn("Empty next group found in FlowObjective store for "
953 + "next-id:{} in dev:{}", nextId, deviceId);
954 }
955 } else {
956 log.warn("next-id {} not found in Flow objective store for dev:{}",
957 nextId, deviceId);
958 }
959 return null;
960 }
961
Charles Chan188ebf52015-12-23 00:15:11 -0800962 protected static void pass(Objective obj) {
Saurav Das822c4e22015-10-23 10:51:11 -0700963 if (obj.context().isPresent()) {
964 obj.context().get().onSuccess(obj);
965 }
966 }
967
Charles Chan188ebf52015-12-23 00:15:11 -0800968 protected static void fail(Objective obj, ObjectiveError error) {
Saurav Das822c4e22015-10-23 10:51:11 -0700969 if (obj.context().isPresent()) {
970 obj.context().get().onError(obj, error);
971 }
972 }
Saurav Das822c4e22015-10-23 10:51:11 -0700973}