blob: 2ba76511bb4de91b999e48d2ea75860519ab2c8b [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
Saurav Das52025962016-01-28 22:30:01 -0800104 protected 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 Das52025962016-01-28 22:30:01 -0800128 protected 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 */
Saurav Das52025962016-01-28 22:30:01 -0800279 protected void processFilter(FilteringObjective filt,
280 boolean install, ApplicationId applicationId) {
Saurav Das822c4e22015-10-23 10:51:11 -0700281 // 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
Saurav Das52025962016-01-28 22:30:01 -0800360 * always go first, as required by ofdpa.
Charles Chan14967c22015-12-07 11:11:50 -0800361 */
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 Das52025962016-01-28 22:30:01 -0800467 // ofdpa requires an additional vlan match rule for the assigned vlan
468 // and it does not require the push when setting the assigned vlan.
469 // It also requires the extra rule to be sent to the switch before we
470 // send the untagged match rule.
471 // None of this in compliance with OF standard.
Saurav Das4f980082015-11-05 13:39:15 -0800472 storeVlan = assignedVlan;
Charles Chan14967c22015-12-07 11:11:50 -0800473
474 preSelector = DefaultTrafficSelector.builder();
475 OfdpaMatchVlanVid preOfdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
476 preSelector.extension(preOfdpaMatchVlanVid, deviceId);
477 preTreatment = DefaultTrafficTreatment.builder().transition(TMAC_TABLE);
478
Saurav Das4f980082015-11-05 13:39:15 -0800479 } else {
Charles Chan14967c22015-12-07 11:11:50 -0800480 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
481 selector.extension(ofdpaMatchVlanVid, deviceId);
Saurav Das4f980082015-11-05 13:39:15 -0800482 storeVlan = vidCriterion.vlanId();
Saurav Das822c4e22015-10-23 10:51:11 -0700483 }
Saurav Das822c4e22015-10-23 10:51:11 -0700484
485 // ofdpa cannot match on ALL portnumber, so we need to use separate
486 // rules for each port.
Charles Chan14967c22015-12-07 11:11:50 -0800487 List<PortNumber> portnums = new ArrayList<>();
Saurav Das822c4e22015-10-23 10:51:11 -0700488 if (portCriterion.port() == PortNumber.ALL) {
489 for (Port port : deviceService.getPorts(deviceId)) {
490 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
491 portnums.add(port.number());
492 }
493 }
494 } else {
495 portnums.add(portCriterion.port());
496 }
Saurav Das4f980082015-11-05 13:39:15 -0800497
Saurav Das822c4e22015-10-23 10:51:11 -0700498 for (PortNumber pnum : portnums) {
Saurav Das4f980082015-11-05 13:39:15 -0800499 // update storage
Charles Chan188ebf52015-12-23 00:15:11 -0800500 ofdpa2GroupHandler.port2Vlan.put(pnum, storeVlan);
501 Set<PortNumber> vlanPorts = ofdpa2GroupHandler.vlan2Port.get(storeVlan);
Saurav Das4f980082015-11-05 13:39:15 -0800502 if (vlanPorts == null) {
503 vlanPorts = Collections.newSetFromMap(
504 new ConcurrentHashMap<PortNumber, Boolean>());
505 vlanPorts.add(pnum);
Charles Chan188ebf52015-12-23 00:15:11 -0800506 ofdpa2GroupHandler.vlan2Port.put(storeVlan, vlanPorts);
Saurav Das4f980082015-11-05 13:39:15 -0800507 } else {
508 vlanPorts.add(pnum);
509 }
510 // create rest of flowrule
Saurav Das822c4e22015-10-23 10:51:11 -0700511 selector.matchInPort(pnum);
512 FlowRule rule = DefaultFlowRule.builder()
513 .forDevice(deviceId)
514 .withSelector(selector.build())
515 .withTreatment(treatment.build())
516 .withPriority(DEFAULT_PRIORITY)
517 .fromApp(applicationId)
518 .makePermanent()
519 .forTable(VLAN_TABLE).build();
Charles Chan14967c22015-12-07 11:11:50 -0800520
521 if (preSelector != null) {
522 preSelector.matchInPort(pnum);
523 FlowRule preRule = DefaultFlowRule.builder()
524 .forDevice(deviceId)
525 .withSelector(preSelector.build())
526 .withTreatment(preTreatment.build())
527 .withPriority(DEFAULT_PRIORITY)
528 .fromApp(applicationId)
529 .makePermanent()
530 .forTable(VLAN_TABLE).build();
531 rules.add(preRule);
532 }
533
Saurav Das822c4e22015-10-23 10:51:11 -0700534 rules.add(rule);
535 }
536 return rules;
537 }
538
539 /**
540 * Allows routed packets with correct destination MAC to be directed
541 * to unicast-IP routing table or MPLS forwarding table.
Saurav Das822c4e22015-10-23 10:51:11 -0700542 *
543 * @param portCriterion port on device for which this filter is programmed
544 * @param ethCriterion dstMac of device for which is filter is programmed
545 * @param vidCriterion vlan assigned to port, or NONE for untagged
Saurav Das0e99e2b2015-10-28 12:39:42 -0700546 * @param assignedVlan assigned vlan-id for untagged packets
Saurav Das822c4e22015-10-23 10:51:11 -0700547 * @param applicationId for application programming this filter
548 * @return list of FlowRule for port-vlan filters
549
550 */
551 protected List<FlowRule> processEthDstFilter(PortCriterion portCriterion,
552 EthCriterion ethCriterion,
553 VlanIdCriterion vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700554 VlanId assignedVlan,
Saurav Das822c4e22015-10-23 10:51:11 -0700555 ApplicationId applicationId) {
556 //handling untagged packets via assigned VLAN
557 if (vidCriterion.vlanId() == VlanId.NONE) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700558 vidCriterion = (VlanIdCriterion) Criteria.matchVlanId(assignedVlan);
Saurav Das822c4e22015-10-23 10:51:11 -0700559 }
560 // ofdpa cannot match on ALL portnumber, so we need to use separate
561 // rules for each port.
562 List<PortNumber> portnums = new ArrayList<PortNumber>();
563 if (portCriterion.port() == PortNumber.ALL) {
564 for (Port port : deviceService.getPorts(deviceId)) {
565 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
566 portnums.add(port.number());
567 }
568 }
569 } else {
570 portnums.add(portCriterion.port());
571 }
572
573 List<FlowRule> rules = new ArrayList<FlowRule>();
574 for (PortNumber pnum : portnums) {
Charles Chan14967c22015-12-07 11:11:50 -0800575 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
Saurav Das822c4e22015-10-23 10:51:11 -0700576 // for unicast IP packets
577 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
578 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
579 selector.matchInPort(pnum);
Charles Chan14967c22015-12-07 11:11:50 -0800580 selector.extension(ofdpaMatchVlanVid, deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700581 selector.matchEthType(Ethernet.TYPE_IPV4);
582 selector.matchEthDst(ethCriterion.mac());
583 treatment.transition(UNICAST_ROUTING_TABLE);
584 FlowRule rule = DefaultFlowRule.builder()
585 .forDevice(deviceId)
586 .withSelector(selector.build())
587 .withTreatment(treatment.build())
588 .withPriority(DEFAULT_PRIORITY)
589 .fromApp(applicationId)
590 .makePermanent()
591 .forTable(TMAC_TABLE).build();
592 rules.add(rule);
593 //for MPLS packets
594 selector = DefaultTrafficSelector.builder();
595 treatment = DefaultTrafficTreatment.builder();
596 selector.matchInPort(pnum);
Charles Chan14967c22015-12-07 11:11:50 -0800597 selector.extension(ofdpaMatchVlanVid, deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700598 selector.matchEthType(Ethernet.MPLS_UNICAST);
599 selector.matchEthDst(ethCriterion.mac());
600 treatment.transition(MPLS_TABLE_0);
601 rule = DefaultFlowRule.builder()
602 .forDevice(deviceId)
603 .withSelector(selector.build())
604 .withTreatment(treatment.build())
605 .withPriority(DEFAULT_PRIORITY)
606 .fromApp(applicationId)
607 .makePermanent()
608 .forTable(TMAC_TABLE).build();
609 rules.add(rule);
610 }
611 return rules;
612 }
613
614 private Collection<FlowRule> processForward(ForwardingObjective fwd) {
615 switch (fwd.flag()) {
616 case SPECIFIC:
617 return processSpecific(fwd);
618 case VERSATILE:
619 return processVersatile(fwd);
620 default:
621 fail(fwd, ObjectiveError.UNKNOWN);
622 log.warn("Unknown forwarding flag {}", fwd.flag());
623 }
624 return Collections.emptySet();
625 }
626
627 /**
628 * In the OF-DPA 2.0 pipeline, versatile forwarding objectives go to the
629 * ACL table.
630 * @param fwd the forwarding objective of type 'versatile'
631 * @return a collection of flow rules to be sent to the switch. An empty
632 * collection may be returned if there is a problem in processing
633 * the flow rule
634 */
Saurav Das52025962016-01-28 22:30:01 -0800635 protected Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
Saurav Das822c4e22015-10-23 10:51:11 -0700636 log.info("Processing versatile forwarding objective");
Saurav Das822c4e22015-10-23 10:51:11 -0700637
638 EthTypeCriterion ethType =
Saurav Das77b5e902016-01-27 17:01:59 -0800639 (EthTypeCriterion) fwd.selector().getCriterion(Criterion.Type.ETH_TYPE);
Saurav Das822c4e22015-10-23 10:51:11 -0700640 if (ethType == null) {
641 log.error("Versatile forwarding objective must include ethType");
642 fail(fwd, ObjectiveError.BADPARAMS);
643 return Collections.emptySet();
644 }
645 if (fwd.nextId() == null && fwd.treatment() == null) {
646 log.error("Forwarding objective {} from {} must contain "
647 + "nextId or Treatment", fwd.selector(), fwd.appId());
648 return Collections.emptySet();
649 }
Saurav Das49cb5a12016-01-16 22:54:07 -0800650
Saurav Das77b5e902016-01-27 17:01:59 -0800651 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
652 fwd.selector().criteria().forEach(criterion -> {
653 if (criterion instanceof VlanIdCriterion) {
654 VlanId vlanId = ((VlanIdCriterion) criterion).vlanId();
655 // ensure that match does not include vlan = NONE as OF-DPA does not
656 // match untagged packets this way in the ACL table.
657 if (vlanId.equals(VlanId.NONE)) {
658 return;
659 }
660 OfdpaMatchVlanVid ofdpaMatchVlanVid =
661 new OfdpaMatchVlanVid(vlanId);
662 sbuilder.extension(ofdpaMatchVlanVid, deviceId);
663 } else {
664 sbuilder.add(criterion);
665 }
666 });
667
Saurav Das822c4e22015-10-23 10:51:11 -0700668 // XXX driver does not currently do type checking as per Tables 65-67 in
669 // OFDPA 2.0 spec. The only allowed treatment is a punt to the controller.
Saurav Das49cb5a12016-01-16 22:54:07 -0800670 TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder();
671 if (fwd.treatment() != null) {
672 for (Instruction ins : fwd.treatment().allInstructions()) {
673 if (ins instanceof OutputInstruction) {
674 OutputInstruction o = (OutputInstruction) ins;
675 if (o.port() == PortNumber.CONTROLLER) {
676 ttBuilder.add(o);
677 } else {
678 log.warn("Only allowed treatments in versatile forwarding "
679 + "objectives are punts to the controller");
680 }
681 } else {
682 log.warn("Cannot process instruction in versatile fwd {}", ins);
683 }
Saurav Das822c4e22015-10-23 10:51:11 -0700684 }
685 }
Saurav Das822c4e22015-10-23 10:51:11 -0700686 if (fwd.nextId() != null) {
Saurav Das49cb5a12016-01-16 22:54:07 -0800687 // overide case
688 NextGroup next = getGroupForNextObjective(fwd.nextId());
689 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
690 // we only need the top level group's key to point the flow to it
691 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
692 if (group == null) {
693 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
694 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
695 fail(fwd, ObjectiveError.GROUPMISSING);
696 return Collections.emptySet();
697 }
698 ttBuilder.deferred().group(group.id());
Saurav Das822c4e22015-10-23 10:51:11 -0700699 }
Saurav Das49cb5a12016-01-16 22:54:07 -0800700
701 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
702 .fromApp(fwd.appId())
703 .withPriority(fwd.priority())
704 .forDevice(deviceId)
Saurav Das77b5e902016-01-27 17:01:59 -0800705 .withSelector(sbuilder.build())
Saurav Das49cb5a12016-01-16 22:54:07 -0800706 .withTreatment(ttBuilder.build())
707 .makePermanent()
708 .forTable(ACL_TABLE);
709 return Collections.singletonList(ruleBuilder.build());
Saurav Das822c4e22015-10-23 10:51:11 -0700710 }
711
712 /**
713 * In the OF-DPA 2.0 pipeline, specific forwarding refers to the IP table
Saurav Das8a0732e2015-11-20 15:27:53 -0800714 * (unicast or multicast) or the L2 table (mac + vlan) or the MPLS table.
Saurav Das822c4e22015-10-23 10:51:11 -0700715 *
716 * @param fwd the forwarding objective of type 'specific'
717 * @return a collection of flow rules. Typically there will be only one
718 * for this type of forwarding objective. An empty set may be
719 * returned if there is an issue in processing the objective.
720 */
Saurav Das8a0732e2015-11-20 15:27:53 -0800721 protected Collection<FlowRule> processSpecific(ForwardingObjective fwd) {
Saurav Das4ce45962015-11-24 23:21:05 -0800722 log.trace("Processing specific fwd objective:{} in dev:{} with next:{}",
723 fwd.id(), deviceId, fwd.nextId());
724 boolean isEthTypeObj = isSupportedEthTypeObjective(fwd);
725 boolean isEthDstObj = isSupportedEthDstObjective(fwd);
726
727 if (isEthTypeObj) {
728 return processEthTypeSpecific(fwd);
729 } else if (isEthDstObj) {
730 return processEthDstSpecific(fwd);
731 } else {
732 log.warn("processSpecific: Unsupported forwarding objective "
733 + "criteria fwd:{} in dev:{}", fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700734 fail(fwd, ObjectiveError.UNSUPPORTED);
735 return Collections.emptySet();
736 }
Saurav Das4ce45962015-11-24 23:21:05 -0800737 }
738
739 private boolean isSupportedEthTypeObjective(ForwardingObjective fwd) {
740 TrafficSelector selector = fwd.selector();
741 EthTypeCriterion ethType = (EthTypeCriterion) selector
742 .getCriterion(Criterion.Type.ETH_TYPE);
Charles Chan188ebf52015-12-23 00:15:11 -0800743 return !((ethType == null) ||
Saurav Das4ce45962015-11-24 23:21:05 -0800744 ((ethType.ethType().toShort() != Ethernet.TYPE_IPV4) &&
Charles Chan188ebf52015-12-23 00:15:11 -0800745 (ethType.ethType().toShort() != Ethernet.MPLS_UNICAST)));
Saurav Das4ce45962015-11-24 23:21:05 -0800746 }
747
748 private boolean isSupportedEthDstObjective(ForwardingObjective fwd) {
749 TrafficSelector selector = fwd.selector();
750 EthCriterion ethDst = (EthCriterion) selector
751 .getCriterion(Criterion.Type.ETH_DST);
752 VlanIdCriterion vlanId = (VlanIdCriterion) selector
753 .getCriterion(Criterion.Type.VLAN_VID);
Charles Chan188ebf52015-12-23 00:15:11 -0800754 return !(ethDst == null && vlanId == null);
Saurav Das4ce45962015-11-24 23:21:05 -0800755 }
756
757 /**
758 * Handles forwarding rules to the IP and MPLS tables.
759 *
760 * @param fwd the forwarding objective
761 * @return A collection of flow rules, or an empty set
762 */
763 protected Collection<FlowRule> processEthTypeSpecific(ForwardingObjective fwd) {
764 TrafficSelector selector = fwd.selector();
765 EthTypeCriterion ethType =
766 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
Saurav Das822c4e22015-10-23 10:51:11 -0700767
Charles Chan188ebf52015-12-23 00:15:11 -0800768 int forTableId;
Saurav Das8a0732e2015-11-20 15:27:53 -0800769 TrafficSelector.Builder filteredSelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800770 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
771 boolean popMpls = false;
772
Saurav Das8a0732e2015-11-20 15:27:53 -0800773 if (ethType.ethType().toShort() == Ethernet.TYPE_IPV4) {
Charles Chan14967c22015-12-07 11:11:50 -0800774 IpPrefix ipPrefix = ((IPCriterion)
775 selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
Saurav Das8a0732e2015-11-20 15:27:53 -0800776 filteredSelector.matchEthType(Ethernet.TYPE_IPV4)
Charles Chan14967c22015-12-07 11:11:50 -0800777 .matchIPDst(ipPrefix);
Saurav Das8a0732e2015-11-20 15:27:53 -0800778 forTableId = UNICAST_ROUTING_TABLE;
Saurav Das4ce45962015-11-24 23:21:05 -0800779 log.debug("processing IPv4 specific forwarding objective {} -> next:{}"
780 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Charles Chan14967c22015-12-07 11:11:50 -0800781
782 if (fwd.treatment() != null) {
783 for (Instruction instr : fwd.treatment().allInstructions()) {
784 if (instr instanceof L3ModificationInstruction &&
785 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
786 tb.deferred().add(instr);
787 }
788 }
789 }
790
Saurav Das8a0732e2015-11-20 15:27:53 -0800791 } else {
792 filteredSelector
793 .matchEthType(Ethernet.MPLS_UNICAST)
794 .matchMplsLabel(((MplsCriterion)
795 selector.getCriterion(Criterion.Type.MPLS_LABEL)).label());
796 MplsBosCriterion bos = (MplsBosCriterion) selector
797 .getCriterion(Criterion.Type.MPLS_BOS);
798 if (bos != null) {
799 filteredSelector.matchMplsBos(bos.mplsBos());
800 }
801 forTableId = MPLS_TABLE_1;
Saurav Das4ce45962015-11-24 23:21:05 -0800802 log.debug("processing MPLS specific forwarding objective {} -> next:{}"
803 + " in dev {}", fwd.id(), fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700804
Charles Chan14967c22015-12-07 11:11:50 -0800805 if (fwd.treatment() != null) {
806 for (Instruction instr : fwd.treatment().allInstructions()) {
807 if (instr instanceof L2ModificationInstruction &&
808 ((L2ModificationInstruction) instr).subtype() == L2SubType.MPLS_POP) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800809 popMpls = true;
Charles Chan14967c22015-12-07 11:11:50 -0800810 tb.immediate().add(instr);
811 }
812 if (instr instanceof L3ModificationInstruction &&
813 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
814 // FIXME Should modify the app to send the correct DEC_MPLS_TTL instruction
815 tb.immediate().decMplsTtl();
816 }
817 if (instr instanceof L3ModificationInstruction &&
818 ((L3ModificationInstruction) instr).subtype() == L3SubType.TTL_IN) {
819 tb.immediate().add(instr);
820 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800821 }
822 }
823 }
Saurav Das822c4e22015-10-23 10:51:11 -0700824
825 if (fwd.nextId() != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800826 if (forTableId == MPLS_TABLE_1 && !popMpls) {
827 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
828 + "is not implemented in OF-DPA yet. Aborting this flow "
829 + "in this device {}", deviceId);
830 // XXX We could convert to forwarding to a single-port, via a
831 // MPLS interface, or a MPLS SWAP (with-same) but that would
832 // have to be handled in the next-objective. Also the pop-mpls
833 // logic used here won't work in non-BoS case.
Saurav Das4ce45962015-11-24 23:21:05 -0800834 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
Saurav Das8a0732e2015-11-20 15:27:53 -0800835 return Collections.emptySet();
836 }
837
Saurav Das423fe2b2015-12-04 10:52:59 -0800838 NextGroup next = getGroupForNextObjective(fwd.nextId());
839 if (next != null) {
840 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
841 // we only need the top level group's key to point the flow to it
842 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
843 if (group == null) {
844 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
845 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
846 fail(fwd, ObjectiveError.GROUPMISSING);
847 return Collections.emptySet();
848 }
849 tb.deferred().group(group.id());
Saurav Das822c4e22015-10-23 10:51:11 -0700850 }
Saurav Das822c4e22015-10-23 10:51:11 -0700851 }
852 tb.transition(ACL_TABLE);
853 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
854 .fromApp(fwd.appId())
855 .withPriority(fwd.priority())
856 .forDevice(deviceId)
Saurav Das8a0732e2015-11-20 15:27:53 -0800857 .withSelector(filteredSelector.build())
858 .withTreatment(tb.build())
859 .forTable(forTableId);
Saurav Das822c4e22015-10-23 10:51:11 -0700860
861 if (fwd.permanent()) {
862 ruleBuilder.makePermanent();
863 } else {
864 ruleBuilder.makeTemporary(fwd.timeout());
865 }
866
Saurav Das822c4e22015-10-23 10:51:11 -0700867 return Collections.singletonList(ruleBuilder.build());
868 }
869
Saurav Das4ce45962015-11-24 23:21:05 -0800870 /**
871 * Handles forwarding rules to the L2 bridging table. Flow actions are not
872 * allowed in the bridging table - instead we use L2 Interface group or
873 * L2 flood group
874 *
875 * @param fwd the forwarding objective
876 * @return A collection of flow rules, or an empty set
877 */
878 protected Collection<FlowRule> processEthDstSpecific(ForwardingObjective fwd) {
879 List<FlowRule> rules = new ArrayList<>();
880
881 // Build filtered selector
882 TrafficSelector selector = fwd.selector();
883 EthCriterion ethCriterion = (EthCriterion) selector
884 .getCriterion(Criterion.Type.ETH_DST);
885 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) selector
886 .getCriterion(Criterion.Type.VLAN_VID);
887
888 if (vlanIdCriterion == null) {
889 log.warn("Forwarding objective for bridging requires vlan. Not "
890 + "installing fwd:{} in dev:{}", fwd.id(), deviceId);
891 fail(fwd, ObjectiveError.BADPARAMS);
892 return Collections.emptySet();
893 }
894
895 TrafficSelector.Builder filteredSelectorBuilder =
896 DefaultTrafficSelector.builder();
897 // Do not match MacAddress for subnet broadcast entry
898 if (!ethCriterion.mac().equals(MacAddress.NONE)) {
899 filteredSelectorBuilder.matchEthDst(ethCriterion.mac());
900 log.debug("processing L2 forwarding objective:{} -> next:{} in dev:{}",
901 fwd.id(), fwd.nextId(), deviceId);
902 } else {
903 log.debug("processing L2 Broadcast forwarding objective:{} -> next:{} "
904 + "in dev:{} for vlan:{}",
905 fwd.id(), fwd.nextId(), deviceId, vlanIdCriterion.vlanId());
906 }
Charles Chan14967c22015-12-07 11:11:50 -0800907 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanIdCriterion.vlanId());
908 filteredSelectorBuilder.extension(ofdpaMatchVlanVid, deviceId);
Saurav Das4ce45962015-11-24 23:21:05 -0800909 TrafficSelector filteredSelector = filteredSelectorBuilder.build();
910
911 if (fwd.treatment() != null) {
912 log.warn("Ignoring traffic treatment in fwd rule {} meant for L2 table"
913 + "for dev:{}. Expecting only nextId", fwd.id(), deviceId);
914 }
915
916 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
917 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -0800918 NextGroup next = getGroupForNextObjective(fwd.nextId());
Saurav Das4ce45962015-11-24 23:21:05 -0800919 if (next != null) {
920 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
921 // we only need the top level group's key to point the flow to it
922 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
923 if (group != null) {
924 treatmentBuilder.deferred().group(group.id());
925 } else {
926 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
927 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
928 fail(fwd, ObjectiveError.GROUPMISSING);
929 return Collections.emptySet();
930 }
931 }
932 }
933 treatmentBuilder.immediate().transition(ACL_TABLE);
934 TrafficTreatment filteredTreatment = treatmentBuilder.build();
935
936 // Build bridging table entries
937 FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder();
938 flowRuleBuilder.fromApp(fwd.appId())
939 .withPriority(fwd.priority())
940 .forDevice(deviceId)
941 .withSelector(filteredSelector)
942 .withTreatment(filteredTreatment)
943 .forTable(BRIDGING_TABLE);
944 if (fwd.permanent()) {
945 flowRuleBuilder.makePermanent();
946 } else {
947 flowRuleBuilder.makeTemporary(fwd.timeout());
948 }
949 rules.add(flowRuleBuilder.build());
950 return rules;
951 }
952
Saurav Das423fe2b2015-12-04 10:52:59 -0800953 protected NextGroup getGroupForNextObjective(Integer nextId) {
954 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
955 if (next != null) {
956 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
957 if (gkeys != null && !gkeys.isEmpty()) {
958 return next;
959 } else {
960 log.warn("Empty next group found in FlowObjective store for "
961 + "next-id:{} in dev:{}", nextId, deviceId);
962 }
963 } else {
964 log.warn("next-id {} not found in Flow objective store for dev:{}",
965 nextId, deviceId);
966 }
967 return null;
968 }
969
Charles Chan188ebf52015-12-23 00:15:11 -0800970 protected static void pass(Objective obj) {
Saurav Das822c4e22015-10-23 10:51:11 -0700971 if (obj.context().isPresent()) {
972 obj.context().get().onSuccess(obj);
973 }
974 }
975
Charles Chan188ebf52015-12-23 00:15:11 -0800976 protected static void fail(Objective obj, ObjectiveError error) {
Saurav Das822c4e22015-10-23 10:51:11 -0700977 if (obj.context().isPresent()) {
978 obj.context().get().onError(obj, error);
979 }
980 }
Saurav Das822c4e22015-10-23 10:51:11 -0700981}