blob: 2408bb6bd126bf9f0a951d7c7291c0fde28ffb90 [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
Charles Chan5270ed02016-01-30 23:22:37 -080030import com.google.common.collect.ImmutableList;
Saurav Das822c4e22015-10-23 10:51:11 -070031import org.onlab.osgi.ServiceDirectory;
Saurav Das822c4e22015-10-23 10:51:11 -070032import org.onlab.packet.Ethernet;
Charles Chan14967c22015-12-07 11:11:50 -080033import org.onlab.packet.IpPrefix;
Saurav Das822c4e22015-10-23 10:51:11 -070034import org.onlab.packet.MacAddress;
Saurav Das822c4e22015-10-23 10:51:11 -070035import org.onlab.packet.VlanId;
36import org.onlab.util.KryoNamespace;
37import org.onosproject.core.ApplicationId;
38import org.onosproject.core.CoreService;
Charles Chan14967c22015-12-07 11:11:50 -080039import org.onosproject.driver.extensions.OfdpaMatchVlanVid;
40import org.onosproject.driver.extensions.OfdpaSetVlanVid;
Saurav Das822c4e22015-10-23 10:51:11 -070041import org.onosproject.net.DeviceId;
42import org.onosproject.net.Port;
43import org.onosproject.net.PortNumber;
44import org.onosproject.net.behaviour.NextGroup;
45import org.onosproject.net.behaviour.Pipeliner;
46import org.onosproject.net.behaviour.PipelinerContext;
47import org.onosproject.net.device.DeviceService;
48import org.onosproject.net.driver.AbstractHandlerBehaviour;
49import org.onosproject.net.flow.DefaultFlowRule;
50import org.onosproject.net.flow.DefaultTrafficSelector;
51import org.onosproject.net.flow.DefaultTrafficTreatment;
52import org.onosproject.net.flow.FlowRule;
53import org.onosproject.net.flow.FlowRuleOperations;
54import org.onosproject.net.flow.FlowRuleOperationsContext;
55import org.onosproject.net.flow.FlowRuleService;
56import org.onosproject.net.flow.TrafficSelector;
57import org.onosproject.net.flow.TrafficTreatment;
58import org.onosproject.net.flow.criteria.Criteria;
59import org.onosproject.net.flow.criteria.Criterion;
60import org.onosproject.net.flow.criteria.EthCriterion;
61import org.onosproject.net.flow.criteria.EthTypeCriterion;
Charles Chan14967c22015-12-07 11:11:50 -080062import org.onosproject.net.flow.criteria.ExtensionCriterion;
Saurav Das822c4e22015-10-23 10:51:11 -070063import org.onosproject.net.flow.criteria.IPCriterion;
Saurav Das8a0732e2015-11-20 15:27:53 -080064import org.onosproject.net.flow.criteria.MplsBosCriterion;
65import org.onosproject.net.flow.criteria.MplsCriterion;
Saurav Das822c4e22015-10-23 10:51:11 -070066import org.onosproject.net.flow.criteria.PortCriterion;
67import org.onosproject.net.flow.criteria.VlanIdCriterion;
68import org.onosproject.net.flow.instructions.Instruction;
69import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
70import org.onosproject.net.flow.instructions.L2ModificationInstruction;
Saurav Das8a0732e2015-11-20 15:27:53 -080071import org.onosproject.net.flow.instructions.L2ModificationInstruction.L2SubType;
Saurav Das822c4e22015-10-23 10:51:11 -070072import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanIdInstruction;
Charles Chan14967c22015-12-07 11:11:50 -080073import org.onosproject.net.flow.instructions.L3ModificationInstruction;
74import org.onosproject.net.flow.instructions.L3ModificationInstruction.L3SubType;
Saurav Das822c4e22015-10-23 10:51:11 -070075import org.onosproject.net.flowobjective.FilteringObjective;
76import org.onosproject.net.flowobjective.FlowObjectiveStore;
77import org.onosproject.net.flowobjective.ForwardingObjective;
78import org.onosproject.net.flowobjective.NextObjective;
79import org.onosproject.net.flowobjective.Objective;
80import org.onosproject.net.flowobjective.ObjectiveError;
Saurav Das822c4e22015-10-23 10:51:11 -070081import org.onosproject.net.group.DefaultGroupKey;
82import org.onosproject.net.group.Group;
Saurav Das822c4e22015-10-23 10:51:11 -070083import org.onosproject.net.group.GroupKey;
Saurav Das822c4e22015-10-23 10:51:11 -070084import org.onosproject.net.group.GroupService;
Saurav Das822c4e22015-10-23 10:51:11 -070085import org.onosproject.store.serializers.KryoNamespaces;
86import org.slf4j.Logger;
87
Saurav Das822c4e22015-10-23 10:51:11 -070088/**
89 * Driver for Broadcom's OF-DPA v2.0 TTP.
90 *
91 */
Charles Chan361154b2016-03-24 10:23:39 -070092public class Ofdpa2Pipeline extends AbstractHandlerBehaviour implements Pipeliner {
Saurav Das822c4e22015-10-23 10:51:11 -070093 protected static final int PORT_TABLE = 0;
94 protected static final int VLAN_TABLE = 10;
95 protected static final int TMAC_TABLE = 20;
96 protected static final int UNICAST_ROUTING_TABLE = 30;
97 protected static final int MULTICAST_ROUTING_TABLE = 40;
98 protected static final int MPLS_TABLE_0 = 23;
99 protected static final int MPLS_TABLE_1 = 24;
100 protected static final int BRIDGING_TABLE = 50;
101 protected static final int ACL_TABLE = 60;
102 protected static final int MAC_LEARNING_TABLE = 254;
103 protected static final long OFPP_MAX = 0xffffff00L;
104
Saurav Das52025962016-01-28 22:30:01 -0800105 protected static final int HIGHEST_PRIORITY = 0xffff;
Saurav Das2857f382015-11-03 14:39:27 -0800106 protected static final int DEFAULT_PRIORITY = 0x8000;
Saurav Das822c4e22015-10-23 10:51:11 -0700107 protected static final int LOWEST_PRIORITY = 0x0;
108
Saurav Das822c4e22015-10-23 10:51:11 -0700109 private final Logger log = getLogger(getClass());
110 private ServiceDirectory serviceDirectory;
111 protected FlowRuleService flowRuleService;
112 private CoreService coreService;
Saurav Das8a0732e2015-11-20 15:27:53 -0800113 protected GroupService groupService;
114 protected FlowObjectiveStore flowObjectiveStore;
Saurav Das822c4e22015-10-23 10:51:11 -0700115 protected DeviceId deviceId;
116 protected ApplicationId driverId;
Saurav Das822c4e22015-10-23 10:51:11 -0700117 protected DeviceService deviceService;
Charles Chan188ebf52015-12-23 00:15:11 -0800118 protected static KryoNamespace appKryo = new KryoNamespace.Builder()
Saurav Das822c4e22015-10-23 10:51:11 -0700119 .register(KryoNamespaces.API)
120 .register(GroupKey.class)
121 .register(DefaultGroupKey.class)
Charles Chan361154b2016-03-24 10:23:39 -0700122 .register(Ofdpa2GroupHandler.OfdpaNextGroup.class)
Saurav Das822c4e22015-10-23 10:51:11 -0700123 .register(byte[].class)
Saurav Das8a0732e2015-11-20 15:27:53 -0800124 .register(ArrayDeque.class)
Saurav Das822c4e22015-10-23 10:51:11 -0700125 .build();
126
Charles Chan361154b2016-03-24 10:23:39 -0700127 protected Ofdpa2GroupHandler ofdpa2GroupHandler;
Saurav Das822c4e22015-10-23 10:51:11 -0700128
Saurav Das52025962016-01-28 22:30:01 -0800129 protected Set<IPCriterion> sentIpFilters = Collections.newSetFromMap(
Charles Chan188ebf52015-12-23 00:15:11 -0800130 new ConcurrentHashMap<>());
Saurav Das4f980082015-11-05 13:39:15 -0800131
Saurav Das822c4e22015-10-23 10:51:11 -0700132 @Override
133 public void init(DeviceId deviceId, PipelinerContext context) {
134 this.serviceDirectory = context.directory();
135 this.deviceId = deviceId;
136
Charles Chan188ebf52015-12-23 00:15:11 -0800137 // Initialize OFDPA group handler
Charles Chan361154b2016-03-24 10:23:39 -0700138 ofdpa2GroupHandler = new Ofdpa2GroupHandler();
Charles Chan188ebf52015-12-23 00:15:11 -0800139 ofdpa2GroupHandler.init(deviceId, context);
Saurav Das822c4e22015-10-23 10:51:11 -0700140
141 coreService = serviceDirectory.get(CoreService.class);
142 flowRuleService = serviceDirectory.get(FlowRuleService.class);
143 groupService = serviceDirectory.get(GroupService.class);
144 flowObjectiveStore = context.store();
Saurav Das822c4e22015-10-23 10:51:11 -0700145 deviceService = serviceDirectory.get(DeviceService.class);
Saurav Das822c4e22015-10-23 10:51:11 -0700146
147 driverId = coreService.registerApplication(
148 "org.onosproject.driver.OFDPA2Pipeline");
149
Saurav Das822c4e22015-10-23 10:51:11 -0700150 initializePipeline();
Saurav Das822c4e22015-10-23 10:51:11 -0700151 }
152
153 protected void initializePipeline() {
Charles Chan188ebf52015-12-23 00:15:11 -0800154 // OF-DPA does not require initializing the pipeline as it puts default
155 // rules automatically in the hardware. However emulation of OFDPA in
156 // software switches does require table-miss-entries.
Saurav Das822c4e22015-10-23 10:51:11 -0700157 }
158
159 //////////////////////////////////////
160 // Flow Objectives
161 //////////////////////////////////////
162
163 @Override
164 public void filter(FilteringObjective filteringObjective) {
165 if (filteringObjective.type() == FilteringObjective.Type.PERMIT) {
166 processFilter(filteringObjective,
167 filteringObjective.op() == Objective.Operation.ADD,
168 filteringObjective.appId());
169 } else {
170 // Note that packets that don't match the PERMIT filter are
171 // automatically denied. The DENY filter is used to deny packets
172 // that are otherwise permitted by the PERMIT filter.
173 // Use ACL table flow rules here for DENY filtering objectives
174 log.debug("filter objective other than PERMIT currently not supported");
175 fail(filteringObjective, ObjectiveError.UNSUPPORTED);
176 }
177 }
178
179 @Override
180 public void forward(ForwardingObjective fwd) {
181 Collection<FlowRule> rules;
182 FlowRuleOperations.Builder flowOpsBuilder = FlowRuleOperations.builder();
183
184 rules = processForward(fwd);
185 switch (fwd.op()) {
186 case ADD:
187 rules.stream()
Sho SHIMIZU45906042016-01-13 23:05:54 -0800188 .filter(Objects::nonNull)
Saurav Das822c4e22015-10-23 10:51:11 -0700189 .forEach(flowOpsBuilder::add);
190 break;
191 case REMOVE:
192 rules.stream()
Sho SHIMIZU45906042016-01-13 23:05:54 -0800193 .filter(Objects::nonNull)
Saurav Das822c4e22015-10-23 10:51:11 -0700194 .forEach(flowOpsBuilder::remove);
195 break;
196 default:
197 fail(fwd, ObjectiveError.UNKNOWN);
198 log.warn("Unknown forwarding type {}", fwd.op());
199 }
200
Saurav Das822c4e22015-10-23 10:51:11 -0700201 flowRuleService.apply(flowOpsBuilder.build(new FlowRuleOperationsContext() {
202 @Override
203 public void onSuccess(FlowRuleOperations ops) {
204 pass(fwd);
205 }
206
207 @Override
208 public void onError(FlowRuleOperations ops) {
209 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
210 }
211 }));
Saurav Das822c4e22015-10-23 10:51:11 -0700212 }
213
214 @Override
215 public void next(NextObjective nextObjective) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800216 NextGroup nextGroup = flowObjectiveStore.getNextGroup(nextObjective.id());
217 switch (nextObjective.op()) {
218 case ADD:
Saurav Das4f980082015-11-05 13:39:15 -0800219 if (nextGroup != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800220 log.warn("Cannot add next {} that already exists in device {}",
221 nextObjective.id(), deviceId);
222 return;
223 }
224 log.debug("Processing NextObjective id{} in dev{} - add group",
225 nextObjective.id(), deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -0800226 ofdpa2GroupHandler.addGroup(nextObjective);
Saurav Das8a0732e2015-11-20 15:27:53 -0800227 break;
228 case ADD_TO_EXISTING:
229 if (nextGroup != null) {
230 log.debug("Processing NextObjective id{} in dev{} - add bucket",
231 nextObjective.id(), deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -0800232 ofdpa2GroupHandler.addBucketToGroup(nextObjective, nextGroup);
Saurav Das4f980082015-11-05 13:39:15 -0800233 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800234 // it is possible that group-chain has not been fully created yet
Saurav Das423fe2b2015-12-04 10:52:59 -0800235 log.debug("Waiting to add bucket to group for next-id:{} in dev:{}",
236 nextObjective.id(), deviceId);
237 // by design only one pending bucket is allowed for the group
Charles Chan188ebf52015-12-23 00:15:11 -0800238 ofdpa2GroupHandler.pendingBuckets.put(nextObjective.id(), nextObjective);
Saurav Das4f980082015-11-05 13:39:15 -0800239 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800240 break;
241 case REMOVE:
242 if (nextGroup == null) {
243 log.warn("Cannot remove next {} that does not exist in device {}",
244 nextObjective.id(), deviceId);
245 return;
246 }
247 log.debug("Processing NextObjective id{} in dev{} - remove group",
248 nextObjective.id(), deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -0800249 ofdpa2GroupHandler.removeGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800250 break;
251 case REMOVE_FROM_EXISTING:
252 if (nextGroup == null) {
253 log.warn("Cannot remove from next {} that does not exist in device {}",
254 nextObjective.id(), deviceId);
255 return;
256 }
257 log.debug("Processing NextObjective id{} in dev{} - remove bucket",
258 nextObjective.id(), deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -0800259 ofdpa2GroupHandler.removeBucketFromGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800260 break;
261 default:
Saurav Das4f980082015-11-05 13:39:15 -0800262 log.warn("Unsupported operation {}", nextObjective.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700263 }
264 }
265
266 //////////////////////////////////////
267 // Flow handling
268 //////////////////////////////////////
269
270 /**
271 * As per OFDPA 2.0 TTP, filtering of VLAN ids, MAC addresses (for routing)
272 * and IP addresses configured on switch ports happen in different tables.
273 * Note that IP filtering rules need to be added to the ACL table, as there
274 * is no mechanism to send to controller via IP table.
275 *
276 * @param filt the filtering objective
277 * @param install indicates whether to add or remove the objective
278 * @param applicationId the application that sent this objective
279 */
Saurav Das52025962016-01-28 22:30:01 -0800280 protected void processFilter(FilteringObjective filt,
281 boolean install, ApplicationId applicationId) {
Saurav Das822c4e22015-10-23 10:51:11 -0700282 // This driver only processes filtering criteria defined with switch
283 // ports as the key
284 PortCriterion portCriterion = null;
285 EthCriterion ethCriterion = null;
286 VlanIdCriterion vidCriterion = null;
287 Collection<IPCriterion> ips = new ArrayList<IPCriterion>();
288 if (!filt.key().equals(Criteria.dummy()) &&
289 filt.key().type() == Criterion.Type.IN_PORT) {
290 portCriterion = (PortCriterion) filt.key();
291 } else {
292 log.warn("No key defined in filtering objective from app: {}. Not"
293 + "processing filtering objective", applicationId);
294 fail(filt, ObjectiveError.UNKNOWN);
295 return;
296 }
297 // convert filtering conditions for switch-intfs into flowrules
298 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
299 for (Criterion criterion : filt.conditions()) {
300 if (criterion.type() == Criterion.Type.ETH_DST) {
301 ethCriterion = (EthCriterion) criterion;
302 } else if (criterion.type() == Criterion.Type.VLAN_VID) {
303 vidCriterion = (VlanIdCriterion) criterion;
304 } else if (criterion.type() == Criterion.Type.IPV4_DST) {
305 ips.add((IPCriterion) criterion);
306 } else {
307 log.error("Unsupported filter {}", criterion);
308 fail(filt, ObjectiveError.UNSUPPORTED);
309 return;
310 }
311 }
312
Saurav Das0e99e2b2015-10-28 12:39:42 -0700313 VlanId assignedVlan = null;
Charles Chane849c192016-01-11 18:28:54 -0800314 // For VLAN cross-connect packets, use the configured VLAN
315 if (vidCriterion != null) {
316 if (vidCriterion.vlanId() != VlanId.NONE) {
317 assignedVlan = vidCriterion.vlanId();
318
319 // For untagged packets, assign a VLAN ID
320 } else {
321 if (filt.meta() == null) {
322 log.error("Missing metadata in filtering objective required " +
323 "for vlan assignment in dev {}", deviceId);
324 fail(filt, ObjectiveError.BADPARAMS);
325 return;
Saurav Das0e99e2b2015-10-28 12:39:42 -0700326 }
Charles Chane849c192016-01-11 18:28:54 -0800327 for (Instruction i : filt.meta().allInstructions()) {
328 if (i instanceof ModVlanIdInstruction) {
329 assignedVlan = ((ModVlanIdInstruction) i).vlanId();
330 }
331 }
332 if (assignedVlan == null) {
333 log.error("Driver requires an assigned vlan-id to tag incoming "
334 + "untagged packets. Not processing vlan filters on "
335 + "device {}", deviceId);
336 fail(filt, ObjectiveError.BADPARAMS);
337 return;
338 }
Saurav Das0e99e2b2015-10-28 12:39:42 -0700339 }
340 }
341
Charles Chane849c192016-01-11 18:28:54 -0800342 if (ethCriterion == null || ethCriterion.mac().equals(MacAddress.NONE)) {
Saurav Das822c4e22015-10-23 10:51:11 -0700343 log.debug("filtering objective missing dstMac, cannot program TMAC table");
344 } else {
345 for (FlowRule tmacRule : processEthDstFilter(portCriterion, ethCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700346 vidCriterion, assignedVlan,
347 applicationId)) {
Saurav Das822c4e22015-10-23 10:51:11 -0700348 log.debug("adding MAC filtering rules in TMAC table: {} for dev: {}",
349 tmacRule, deviceId);
350 ops = install ? ops.add(tmacRule) : ops.remove(tmacRule);
351 }
352 }
353
354 if (ethCriterion == null || vidCriterion == null) {
Charles Chane849c192016-01-11 18:28:54 -0800355 log.debug("filtering objective missing dstMac or VLAN, "
356 + "cannot program VLAN Table");
Saurav Das822c4e22015-10-23 10:51:11 -0700357 } else {
Charles Chan14967c22015-12-07 11:11:50 -0800358 /*
359 * NOTE: Separate vlan filtering rules and assignment rules
360 * into different stage in order to guarantee that filtering rules
Saurav Das52025962016-01-28 22:30:01 -0800361 * always go first, as required by ofdpa.
Charles Chan14967c22015-12-07 11:11:50 -0800362 */
363 List<FlowRule> allRules = processVlanIdFilter(
364 portCriterion, vidCriterion, assignedVlan, applicationId);
365 List<FlowRule> filteringRules = new ArrayList<>();
366 List<FlowRule> assignmentRules = new ArrayList<>();
367
368 allRules.forEach(flowRule -> {
369 ExtensionCriterion extCriterion =
370 (ExtensionCriterion) flowRule.selector().getCriterion(Criterion.Type.EXTENSION);
371 VlanId vlanId = ((OfdpaMatchVlanVid) extCriterion.extensionSelector()).vlanId();
Charles Chanbe8aea42016-02-24 12:04:47 -0800372 if (!vlanId.equals(VlanId.NONE)) {
Charles Chan14967c22015-12-07 11:11:50 -0800373 filteringRules.add(flowRule);
374 } else {
375 assignmentRules.add(flowRule);
376 }
377 });
378
379 for (FlowRule filteringRule : filteringRules) {
Saurav Das822c4e22015-10-23 10:51:11 -0700380 log.debug("adding VLAN filtering rule in VLAN table: {} for dev: {}",
Charles Chan14967c22015-12-07 11:11:50 -0800381 filteringRule, deviceId);
382 ops = install ? ops.add(filteringRule) : ops.remove(filteringRule);
383 }
384
385 ops.newStage();
386
387 for (FlowRule assignmentRule : assignmentRules) {
388 log.debug("adding VLAN assignment rule in VLAN table: {} for dev: {}",
389 assignmentRule, deviceId);
390 ops = install ? ops.add(assignmentRule) : ops.remove(assignmentRule);
Saurav Das822c4e22015-10-23 10:51:11 -0700391 }
392 }
393
394 for (IPCriterion ipaddr : ips) {
395 // since we ignore port information for IP rules, and the same (gateway) IP
396 // can be configured on multiple ports, we make sure that we send
397 // only a single rule to the switch.
398 if (!sentIpFilters.contains(ipaddr)) {
399 sentIpFilters.add(ipaddr);
400 log.debug("adding IP filtering rules in ACL table {} for dev: {}",
401 ipaddr, deviceId);
402 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
403 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
404 selector.matchEthType(Ethernet.TYPE_IPV4);
405 selector.matchIPDst(ipaddr.ip());
406 treatment.setOutput(PortNumber.CONTROLLER);
407 FlowRule rule = DefaultFlowRule.builder()
408 .forDevice(deviceId)
409 .withSelector(selector.build())
410 .withTreatment(treatment.build())
411 .withPriority(HIGHEST_PRIORITY)
412 .fromApp(applicationId)
413 .makePermanent()
414 .forTable(ACL_TABLE).build();
415 ops = install ? ops.add(rule) : ops.remove(rule);
416 }
417 }
418
419 // apply filtering flow rules
420 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
421 @Override
422 public void onSuccess(FlowRuleOperations ops) {
423 log.info("Applied {} filtering rules in device {}",
424 ops.stages().get(0).size(), deviceId);
425 pass(filt);
426 }
427
428 @Override
429 public void onError(FlowRuleOperations ops) {
430 log.info("Failed to apply all filtering rules in dev {}", deviceId);
431 fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED);
432 }
433 }));
434
435 }
436
437 /**
438 * Allows untagged packets into pipeline by assigning a vlan id.
Saurav Das0e99e2b2015-10-28 12:39:42 -0700439 * Vlan assignment is done by the application.
Saurav Das822c4e22015-10-23 10:51:11 -0700440 * Allows tagged packets into pipeline as per configured port-vlan info.
Saurav Das0e99e2b2015-10-28 12:39:42 -0700441 *
Saurav Das822c4e22015-10-23 10:51:11 -0700442 * @param portCriterion port on device for which this filter is programmed
443 * @param vidCriterion vlan assigned to port, or NONE for untagged
Saurav Das0e99e2b2015-10-28 12:39:42 -0700444 * @param assignedVlan assigned vlan-id for untagged packets
Saurav Das822c4e22015-10-23 10:51:11 -0700445 * @param applicationId for application programming this filter
446 * @return list of FlowRule for port-vlan filters
447 */
448 protected List<FlowRule> processVlanIdFilter(PortCriterion portCriterion,
449 VlanIdCriterion vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700450 VlanId assignedVlan,
Saurav Das822c4e22015-10-23 10:51:11 -0700451 ApplicationId applicationId) {
Charles Chan14967c22015-12-07 11:11:50 -0800452 List<FlowRule> rules = new ArrayList<>();
Saurav Das822c4e22015-10-23 10:51:11 -0700453 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
454 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800455 TrafficSelector.Builder preSelector = null;
456 TrafficTreatment.Builder preTreatment = null;
457
458
Saurav Das4f980082015-11-05 13:39:15 -0800459 treatment.transition(TMAC_TABLE);
460
461 VlanId storeVlan = null;
Saurav Das822c4e22015-10-23 10:51:11 -0700462 if (vidCriterion.vlanId() == VlanId.NONE) {
463 // untagged packets are assigned vlans
Charles Chanbe8aea42016-02-24 12:04:47 -0800464 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(VlanId.NONE);
Charles Chan14967c22015-12-07 11:11:50 -0800465 selector.extension(ofdpaMatchVlanVid, deviceId);
466 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
467 treatment.extension(ofdpaSetVlanVid, deviceId);
Saurav Das52025962016-01-28 22:30:01 -0800468 // ofdpa requires an additional vlan match rule for the assigned vlan
469 // and it does not require the push when setting the assigned vlan.
470 // It also requires the extra rule to be sent to the switch before we
471 // send the untagged match rule.
472 // None of this in compliance with OF standard.
Saurav Das4f980082015-11-05 13:39:15 -0800473 storeVlan = assignedVlan;
Charles Chan14967c22015-12-07 11:11:50 -0800474
475 preSelector = DefaultTrafficSelector.builder();
476 OfdpaMatchVlanVid preOfdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
477 preSelector.extension(preOfdpaMatchVlanVid, deviceId);
478 preTreatment = DefaultTrafficTreatment.builder().transition(TMAC_TABLE);
479
Saurav Das4f980082015-11-05 13:39:15 -0800480 } else {
Charles Chan14967c22015-12-07 11:11:50 -0800481 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
482 selector.extension(ofdpaMatchVlanVid, deviceId);
Saurav Das4f980082015-11-05 13:39:15 -0800483 storeVlan = vidCriterion.vlanId();
Saurav Das822c4e22015-10-23 10:51:11 -0700484 }
Saurav Das822c4e22015-10-23 10:51:11 -0700485
486 // ofdpa cannot match on ALL portnumber, so we need to use separate
487 // rules for each port.
Charles Chan14967c22015-12-07 11:11:50 -0800488 List<PortNumber> portnums = new ArrayList<>();
Saurav Das822c4e22015-10-23 10:51:11 -0700489 if (portCriterion.port() == PortNumber.ALL) {
490 for (Port port : deviceService.getPorts(deviceId)) {
491 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
492 portnums.add(port.number());
493 }
494 }
495 } else {
496 portnums.add(portCriterion.port());
497 }
Saurav Das4f980082015-11-05 13:39:15 -0800498
Saurav Das822c4e22015-10-23 10:51:11 -0700499 for (PortNumber pnum : portnums) {
Saurav Das4f980082015-11-05 13:39:15 -0800500 // update storage
Charles Chan188ebf52015-12-23 00:15:11 -0800501 ofdpa2GroupHandler.port2Vlan.put(pnum, storeVlan);
502 Set<PortNumber> vlanPorts = ofdpa2GroupHandler.vlan2Port.get(storeVlan);
Saurav Das4f980082015-11-05 13:39:15 -0800503 if (vlanPorts == null) {
504 vlanPorts = Collections.newSetFromMap(
505 new ConcurrentHashMap<PortNumber, Boolean>());
506 vlanPorts.add(pnum);
Charles Chan188ebf52015-12-23 00:15:11 -0800507 ofdpa2GroupHandler.vlan2Port.put(storeVlan, vlanPorts);
Saurav Das4f980082015-11-05 13:39:15 -0800508 } else {
509 vlanPorts.add(pnum);
510 }
511 // create rest of flowrule
Saurav Das822c4e22015-10-23 10:51:11 -0700512 selector.matchInPort(pnum);
513 FlowRule rule = DefaultFlowRule.builder()
514 .forDevice(deviceId)
515 .withSelector(selector.build())
516 .withTreatment(treatment.build())
517 .withPriority(DEFAULT_PRIORITY)
518 .fromApp(applicationId)
519 .makePermanent()
520 .forTable(VLAN_TABLE).build();
Charles Chan14967c22015-12-07 11:11:50 -0800521
522 if (preSelector != null) {
523 preSelector.matchInPort(pnum);
524 FlowRule preRule = DefaultFlowRule.builder()
525 .forDevice(deviceId)
526 .withSelector(preSelector.build())
527 .withTreatment(preTreatment.build())
528 .withPriority(DEFAULT_PRIORITY)
529 .fromApp(applicationId)
530 .makePermanent()
531 .forTable(VLAN_TABLE).build();
532 rules.add(preRule);
533 }
534
Saurav Das822c4e22015-10-23 10:51:11 -0700535 rules.add(rule);
536 }
537 return rules;
538 }
539
540 /**
541 * Allows routed packets with correct destination MAC to be directed
542 * to unicast-IP routing table or MPLS forwarding table.
Saurav Das822c4e22015-10-23 10:51:11 -0700543 *
544 * @param portCriterion port on device for which this filter is programmed
545 * @param ethCriterion dstMac of device for which is filter is programmed
546 * @param vidCriterion vlan assigned to port, or NONE for untagged
Saurav Das0e99e2b2015-10-28 12:39:42 -0700547 * @param assignedVlan assigned vlan-id for untagged packets
Saurav Das822c4e22015-10-23 10:51:11 -0700548 * @param applicationId for application programming this filter
549 * @return list of FlowRule for port-vlan filters
550
551 */
552 protected List<FlowRule> processEthDstFilter(PortCriterion portCriterion,
553 EthCriterion ethCriterion,
554 VlanIdCriterion vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700555 VlanId assignedVlan,
Saurav Das822c4e22015-10-23 10:51:11 -0700556 ApplicationId applicationId) {
Charles Chan5270ed02016-01-30 23:22:37 -0800557 // Consider PortNumber.ANY as wildcard. Match ETH_DST only
558 if (portCriterion != null && portCriterion.port() == PortNumber.ANY) {
559 return processEthDstOnlyFilter(ethCriterion, applicationId);
560 }
561
Saurav Das822c4e22015-10-23 10:51:11 -0700562 //handling untagged packets via assigned VLAN
563 if (vidCriterion.vlanId() == VlanId.NONE) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700564 vidCriterion = (VlanIdCriterion) Criteria.matchVlanId(assignedVlan);
Saurav Das822c4e22015-10-23 10:51:11 -0700565 }
566 // ofdpa cannot match on ALL portnumber, so we need to use separate
567 // rules for each port.
568 List<PortNumber> portnums = new ArrayList<PortNumber>();
569 if (portCriterion.port() == PortNumber.ALL) {
570 for (Port port : deviceService.getPorts(deviceId)) {
571 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
572 portnums.add(port.number());
573 }
574 }
575 } else {
576 portnums.add(portCriterion.port());
577 }
578
579 List<FlowRule> rules = new ArrayList<FlowRule>();
580 for (PortNumber pnum : portnums) {
Charles Chan14967c22015-12-07 11:11:50 -0800581 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
Saurav Das822c4e22015-10-23 10:51:11 -0700582 // for unicast IP packets
583 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
584 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
585 selector.matchInPort(pnum);
Charles Chan14967c22015-12-07 11:11:50 -0800586 selector.extension(ofdpaMatchVlanVid, deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700587 selector.matchEthType(Ethernet.TYPE_IPV4);
588 selector.matchEthDst(ethCriterion.mac());
589 treatment.transition(UNICAST_ROUTING_TABLE);
590 FlowRule rule = DefaultFlowRule.builder()
591 .forDevice(deviceId)
592 .withSelector(selector.build())
593 .withTreatment(treatment.build())
594 .withPriority(DEFAULT_PRIORITY)
595 .fromApp(applicationId)
596 .makePermanent()
597 .forTable(TMAC_TABLE).build();
598 rules.add(rule);
599 //for MPLS packets
600 selector = DefaultTrafficSelector.builder();
601 treatment = DefaultTrafficTreatment.builder();
602 selector.matchInPort(pnum);
Charles Chan14967c22015-12-07 11:11:50 -0800603 selector.extension(ofdpaMatchVlanVid, deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700604 selector.matchEthType(Ethernet.MPLS_UNICAST);
605 selector.matchEthDst(ethCriterion.mac());
606 treatment.transition(MPLS_TABLE_0);
607 rule = DefaultFlowRule.builder()
608 .forDevice(deviceId)
609 .withSelector(selector.build())
610 .withTreatment(treatment.build())
611 .withPriority(DEFAULT_PRIORITY)
612 .fromApp(applicationId)
613 .makePermanent()
614 .forTable(TMAC_TABLE).build();
615 rules.add(rule);
616 }
617 return rules;
618 }
619
Charles Chan5270ed02016-01-30 23:22:37 -0800620 protected List<FlowRule> processEthDstOnlyFilter(EthCriterion ethCriterion,
621 ApplicationId applicationId) {
622 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
623 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
624 selector.matchEthType(Ethernet.TYPE_IPV4);
625 selector.matchEthDst(ethCriterion.mac());
626 treatment.transition(UNICAST_ROUTING_TABLE);
627 FlowRule rule = DefaultFlowRule.builder()
628 .forDevice(deviceId)
629 .withSelector(selector.build())
630 .withTreatment(treatment.build())
631 .withPriority(DEFAULT_PRIORITY)
632 .fromApp(applicationId)
633 .makePermanent()
634 .forTable(TMAC_TABLE).build();
635 return ImmutableList.<FlowRule>builder().add(rule).build();
636 }
637
Saurav Das822c4e22015-10-23 10:51:11 -0700638 private Collection<FlowRule> processForward(ForwardingObjective fwd) {
639 switch (fwd.flag()) {
640 case SPECIFIC:
641 return processSpecific(fwd);
642 case VERSATILE:
643 return processVersatile(fwd);
644 default:
645 fail(fwd, ObjectiveError.UNKNOWN);
646 log.warn("Unknown forwarding flag {}", fwd.flag());
647 }
648 return Collections.emptySet();
649 }
650
651 /**
652 * In the OF-DPA 2.0 pipeline, versatile forwarding objectives go to the
653 * ACL table.
654 * @param fwd the forwarding objective of type 'versatile'
655 * @return a collection of flow rules to be sent to the switch. An empty
656 * collection may be returned if there is a problem in processing
657 * the flow rule
658 */
Saurav Das52025962016-01-28 22:30:01 -0800659 protected Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
Saurav Das822c4e22015-10-23 10:51:11 -0700660 log.info("Processing versatile forwarding objective");
Saurav Das822c4e22015-10-23 10:51:11 -0700661
662 EthTypeCriterion ethType =
Saurav Das77b5e902016-01-27 17:01:59 -0800663 (EthTypeCriterion) fwd.selector().getCriterion(Criterion.Type.ETH_TYPE);
Saurav Das822c4e22015-10-23 10:51:11 -0700664 if (ethType == null) {
665 log.error("Versatile forwarding objective must include ethType");
666 fail(fwd, ObjectiveError.BADPARAMS);
667 return Collections.emptySet();
668 }
669 if (fwd.nextId() == null && fwd.treatment() == null) {
670 log.error("Forwarding objective {} from {} must contain "
671 + "nextId or Treatment", fwd.selector(), fwd.appId());
Zsolt Haraszti9faab752016-02-17 15:55:20 -0800672 fail(fwd, ObjectiveError.BADPARAMS);
Saurav Das822c4e22015-10-23 10:51:11 -0700673 return Collections.emptySet();
674 }
Saurav Das49cb5a12016-01-16 22:54:07 -0800675
Saurav Das77b5e902016-01-27 17:01:59 -0800676 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
677 fwd.selector().criteria().forEach(criterion -> {
678 if (criterion instanceof VlanIdCriterion) {
679 VlanId vlanId = ((VlanIdCriterion) criterion).vlanId();
680 // ensure that match does not include vlan = NONE as OF-DPA does not
681 // match untagged packets this way in the ACL table.
682 if (vlanId.equals(VlanId.NONE)) {
683 return;
684 }
685 OfdpaMatchVlanVid ofdpaMatchVlanVid =
686 new OfdpaMatchVlanVid(vlanId);
687 sbuilder.extension(ofdpaMatchVlanVid, deviceId);
688 } else {
689 sbuilder.add(criterion);
690 }
691 });
692
Saurav Das822c4e22015-10-23 10:51:11 -0700693 // XXX driver does not currently do type checking as per Tables 65-67 in
694 // OFDPA 2.0 spec. The only allowed treatment is a punt to the controller.
Saurav Das49cb5a12016-01-16 22:54:07 -0800695 TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder();
696 if (fwd.treatment() != null) {
697 for (Instruction ins : fwd.treatment().allInstructions()) {
698 if (ins instanceof OutputInstruction) {
699 OutputInstruction o = (OutputInstruction) ins;
700 if (o.port() == PortNumber.CONTROLLER) {
701 ttBuilder.add(o);
702 } else {
703 log.warn("Only allowed treatments in versatile forwarding "
704 + "objectives are punts to the controller");
705 }
706 } else {
707 log.warn("Cannot process instruction in versatile fwd {}", ins);
708 }
Saurav Das822c4e22015-10-23 10:51:11 -0700709 }
710 }
Saurav Das822c4e22015-10-23 10:51:11 -0700711 if (fwd.nextId() != null) {
Saurav Das49cb5a12016-01-16 22:54:07 -0800712 // overide case
713 NextGroup next = getGroupForNextObjective(fwd.nextId());
714 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
715 // we only need the top level group's key to point the flow to it
716 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
717 if (group == null) {
718 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
719 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
720 fail(fwd, ObjectiveError.GROUPMISSING);
721 return Collections.emptySet();
722 }
723 ttBuilder.deferred().group(group.id());
Saurav Das822c4e22015-10-23 10:51:11 -0700724 }
Saurav Das49cb5a12016-01-16 22:54:07 -0800725
726 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
727 .fromApp(fwd.appId())
728 .withPriority(fwd.priority())
729 .forDevice(deviceId)
Saurav Das77b5e902016-01-27 17:01:59 -0800730 .withSelector(sbuilder.build())
Saurav Das49cb5a12016-01-16 22:54:07 -0800731 .withTreatment(ttBuilder.build())
732 .makePermanent()
733 .forTable(ACL_TABLE);
734 return Collections.singletonList(ruleBuilder.build());
Saurav Das822c4e22015-10-23 10:51:11 -0700735 }
736
737 /**
738 * In the OF-DPA 2.0 pipeline, specific forwarding refers to the IP table
Saurav Das8a0732e2015-11-20 15:27:53 -0800739 * (unicast or multicast) or the L2 table (mac + vlan) or the MPLS table.
Saurav Das822c4e22015-10-23 10:51:11 -0700740 *
741 * @param fwd the forwarding objective of type 'specific'
742 * @return a collection of flow rules. Typically there will be only one
743 * for this type of forwarding objective. An empty set may be
744 * returned if there is an issue in processing the objective.
745 */
Saurav Das8a0732e2015-11-20 15:27:53 -0800746 protected Collection<FlowRule> processSpecific(ForwardingObjective fwd) {
Saurav Das4ce45962015-11-24 23:21:05 -0800747 log.trace("Processing specific fwd objective:{} in dev:{} with next:{}",
748 fwd.id(), deviceId, fwd.nextId());
749 boolean isEthTypeObj = isSupportedEthTypeObjective(fwd);
750 boolean isEthDstObj = isSupportedEthDstObjective(fwd);
751
752 if (isEthTypeObj) {
753 return processEthTypeSpecific(fwd);
754 } else if (isEthDstObj) {
755 return processEthDstSpecific(fwd);
756 } else {
757 log.warn("processSpecific: Unsupported forwarding objective "
758 + "criteria fwd:{} in dev:{}", fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700759 fail(fwd, ObjectiveError.UNSUPPORTED);
760 return Collections.emptySet();
761 }
Saurav Das4ce45962015-11-24 23:21:05 -0800762 }
763
764 private boolean isSupportedEthTypeObjective(ForwardingObjective fwd) {
765 TrafficSelector selector = fwd.selector();
766 EthTypeCriterion ethType = (EthTypeCriterion) selector
767 .getCriterion(Criterion.Type.ETH_TYPE);
Charles Chan188ebf52015-12-23 00:15:11 -0800768 return !((ethType == null) ||
Saurav Das4ce45962015-11-24 23:21:05 -0800769 ((ethType.ethType().toShort() != Ethernet.TYPE_IPV4) &&
Charles Chan188ebf52015-12-23 00:15:11 -0800770 (ethType.ethType().toShort() != Ethernet.MPLS_UNICAST)));
Saurav Das4ce45962015-11-24 23:21:05 -0800771 }
772
773 private boolean isSupportedEthDstObjective(ForwardingObjective fwd) {
774 TrafficSelector selector = fwd.selector();
775 EthCriterion ethDst = (EthCriterion) selector
776 .getCriterion(Criterion.Type.ETH_DST);
777 VlanIdCriterion vlanId = (VlanIdCriterion) selector
778 .getCriterion(Criterion.Type.VLAN_VID);
Charles Chan188ebf52015-12-23 00:15:11 -0800779 return !(ethDst == null && vlanId == null);
Saurav Das4ce45962015-11-24 23:21:05 -0800780 }
781
782 /**
783 * Handles forwarding rules to the IP and MPLS tables.
784 *
785 * @param fwd the forwarding objective
786 * @return A collection of flow rules, or an empty set
787 */
788 protected Collection<FlowRule> processEthTypeSpecific(ForwardingObjective fwd) {
789 TrafficSelector selector = fwd.selector();
790 EthTypeCriterion ethType =
791 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
Flavio Castroe10fa242016-01-15 12:43:51 -0800792 boolean defaultRule = false;
793 boolean popMpls = false;
Charles Chan188ebf52015-12-23 00:15:11 -0800794 int forTableId;
Saurav Das8a0732e2015-11-20 15:27:53 -0800795 TrafficSelector.Builder filteredSelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800796 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
Flavio Castroe10fa242016-01-15 12:43:51 -0800797 TrafficSelector.Builder complementarySelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800798
Flavio Castroe10fa242016-01-15 12:43:51 -0800799 /*
800 * NOTE: The switch does not support matching 0.0.0.0/0.
801 * Split it into 0.0.0.0/1 and 128.0.0.0/1
802 */
Saurav Das8a0732e2015-11-20 15:27:53 -0800803 if (ethType.ethType().toShort() == Ethernet.TYPE_IPV4) {
Flavio Castroe10fa242016-01-15 12:43:51 -0800804 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
805 if (ipv4Dst.prefixLength() > 0) {
806 filteredSelector.matchEthType(Ethernet.TYPE_IPV4)
807 .matchIPDst(ipv4Dst);
808 } else {
809 filteredSelector.matchEthType(Ethernet.TYPE_IPV4)
810 .matchIPDst(IpPrefix.valueOf("0.0.0.0/1"));
811 complementarySelector.matchEthType(Ethernet.TYPE_IPV4)
812 .matchIPDst(IpPrefix.valueOf("128.0.0.0/1"));
813 defaultRule = true;
814 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800815 forTableId = UNICAST_ROUTING_TABLE;
Saurav Das4ce45962015-11-24 23:21:05 -0800816 log.debug("processing IPv4 specific forwarding objective {} -> next:{}"
817 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Charles Chan14967c22015-12-07 11:11:50 -0800818
819 if (fwd.treatment() != null) {
820 for (Instruction instr : fwd.treatment().allInstructions()) {
821 if (instr instanceof L3ModificationInstruction &&
822 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
823 tb.deferred().add(instr);
824 }
825 }
826 }
827
Saurav Das8a0732e2015-11-20 15:27:53 -0800828 } else {
829 filteredSelector
830 .matchEthType(Ethernet.MPLS_UNICAST)
831 .matchMplsLabel(((MplsCriterion)
832 selector.getCriterion(Criterion.Type.MPLS_LABEL)).label());
833 MplsBosCriterion bos = (MplsBosCriterion) selector
834 .getCriterion(Criterion.Type.MPLS_BOS);
835 if (bos != null) {
836 filteredSelector.matchMplsBos(bos.mplsBos());
837 }
838 forTableId = MPLS_TABLE_1;
Saurav Das4ce45962015-11-24 23:21:05 -0800839 log.debug("processing MPLS specific forwarding objective {} -> next:{}"
840 + " in dev {}", fwd.id(), fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700841
Charles Chan14967c22015-12-07 11:11:50 -0800842 if (fwd.treatment() != null) {
843 for (Instruction instr : fwd.treatment().allInstructions()) {
844 if (instr instanceof L2ModificationInstruction &&
845 ((L2ModificationInstruction) instr).subtype() == L2SubType.MPLS_POP) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800846 popMpls = true;
Charles Chan14967c22015-12-07 11:11:50 -0800847 tb.immediate().add(instr);
848 }
849 if (instr instanceof L3ModificationInstruction &&
850 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
851 // FIXME Should modify the app to send the correct DEC_MPLS_TTL instruction
852 tb.immediate().decMplsTtl();
853 }
854 if (instr instanceof L3ModificationInstruction &&
855 ((L3ModificationInstruction) instr).subtype() == L3SubType.TTL_IN) {
856 tb.immediate().add(instr);
857 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800858 }
859 }
860 }
Saurav Das822c4e22015-10-23 10:51:11 -0700861
862 if (fwd.nextId() != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800863 if (forTableId == MPLS_TABLE_1 && !popMpls) {
864 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
865 + "is not implemented in OF-DPA yet. Aborting this flow "
866 + "in this device {}", deviceId);
867 // XXX We could convert to forwarding to a single-port, via a
868 // MPLS interface, or a MPLS SWAP (with-same) but that would
869 // have to be handled in the next-objective. Also the pop-mpls
870 // logic used here won't work in non-BoS case.
Saurav Das4ce45962015-11-24 23:21:05 -0800871 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
Saurav Das8a0732e2015-11-20 15:27:53 -0800872 return Collections.emptySet();
873 }
874
Saurav Das423fe2b2015-12-04 10:52:59 -0800875 NextGroup next = getGroupForNextObjective(fwd.nextId());
876 if (next != null) {
877 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
878 // we only need the top level group's key to point the flow to it
879 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
880 if (group == null) {
881 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
882 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
883 fail(fwd, ObjectiveError.GROUPMISSING);
884 return Collections.emptySet();
885 }
886 tb.deferred().group(group.id());
Saurav Das822c4e22015-10-23 10:51:11 -0700887 }
Saurav Das822c4e22015-10-23 10:51:11 -0700888 }
889 tb.transition(ACL_TABLE);
890 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
891 .fromApp(fwd.appId())
892 .withPriority(fwd.priority())
893 .forDevice(deviceId)
Saurav Das8a0732e2015-11-20 15:27:53 -0800894 .withSelector(filteredSelector.build())
895 .withTreatment(tb.build())
896 .forTable(forTableId);
Saurav Das822c4e22015-10-23 10:51:11 -0700897
898 if (fwd.permanent()) {
899 ruleBuilder.makePermanent();
900 } else {
901 ruleBuilder.makeTemporary(fwd.timeout());
902 }
Flavio Castroe10fa242016-01-15 12:43:51 -0800903 Collection<FlowRule> flowRuleCollection = new ArrayList<>();
904 flowRuleCollection.add(ruleBuilder.build());
905 if (defaultRule) {
906 FlowRule.Builder rule = DefaultFlowRule.builder()
907 .fromApp(fwd.appId())
908 .withPriority(fwd.priority())
909 .forDevice(deviceId)
910 .withSelector(complementarySelector.build())
911 .withTreatment(tb.build())
912 .forTable(forTableId);
913 if (fwd.permanent()) {
914 rule.makePermanent();
915 } else {
916 rule.makeTemporary(fwd.timeout());
917 }
918 flowRuleCollection.add(rule.build());
919 log.debug("Default rule 0.0.0.0/0 is being installed two rules");
920 }
Saurav Das822c4e22015-10-23 10:51:11 -0700921
Flavio Castroe10fa242016-01-15 12:43:51 -0800922 return flowRuleCollection;
Saurav Das822c4e22015-10-23 10:51:11 -0700923 }
924
Saurav Das4ce45962015-11-24 23:21:05 -0800925 /**
926 * Handles forwarding rules to the L2 bridging table. Flow actions are not
927 * allowed in the bridging table - instead we use L2 Interface group or
928 * L2 flood group
929 *
930 * @param fwd the forwarding objective
931 * @return A collection of flow rules, or an empty set
932 */
933 protected Collection<FlowRule> processEthDstSpecific(ForwardingObjective fwd) {
934 List<FlowRule> rules = new ArrayList<>();
935
936 // Build filtered selector
937 TrafficSelector selector = fwd.selector();
938 EthCriterion ethCriterion = (EthCriterion) selector
939 .getCriterion(Criterion.Type.ETH_DST);
940 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) selector
941 .getCriterion(Criterion.Type.VLAN_VID);
942
943 if (vlanIdCriterion == null) {
944 log.warn("Forwarding objective for bridging requires vlan. Not "
945 + "installing fwd:{} in dev:{}", fwd.id(), deviceId);
946 fail(fwd, ObjectiveError.BADPARAMS);
947 return Collections.emptySet();
948 }
949
950 TrafficSelector.Builder filteredSelectorBuilder =
951 DefaultTrafficSelector.builder();
952 // Do not match MacAddress for subnet broadcast entry
953 if (!ethCriterion.mac().equals(MacAddress.NONE)) {
954 filteredSelectorBuilder.matchEthDst(ethCriterion.mac());
955 log.debug("processing L2 forwarding objective:{} -> next:{} in dev:{}",
956 fwd.id(), fwd.nextId(), deviceId);
957 } else {
958 log.debug("processing L2 Broadcast forwarding objective:{} -> next:{} "
959 + "in dev:{} for vlan:{}",
960 fwd.id(), fwd.nextId(), deviceId, vlanIdCriterion.vlanId());
961 }
Charles Chan14967c22015-12-07 11:11:50 -0800962 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanIdCriterion.vlanId());
963 filteredSelectorBuilder.extension(ofdpaMatchVlanVid, deviceId);
Saurav Das4ce45962015-11-24 23:21:05 -0800964 TrafficSelector filteredSelector = filteredSelectorBuilder.build();
965
966 if (fwd.treatment() != null) {
967 log.warn("Ignoring traffic treatment in fwd rule {} meant for L2 table"
968 + "for dev:{}. Expecting only nextId", fwd.id(), deviceId);
969 }
970
971 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
972 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -0800973 NextGroup next = getGroupForNextObjective(fwd.nextId());
Saurav Das4ce45962015-11-24 23:21:05 -0800974 if (next != null) {
975 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
976 // we only need the top level group's key to point the flow to it
977 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
978 if (group != null) {
979 treatmentBuilder.deferred().group(group.id());
980 } else {
981 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
982 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
983 fail(fwd, ObjectiveError.GROUPMISSING);
984 return Collections.emptySet();
985 }
986 }
987 }
988 treatmentBuilder.immediate().transition(ACL_TABLE);
989 TrafficTreatment filteredTreatment = treatmentBuilder.build();
990
991 // Build bridging table entries
992 FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder();
993 flowRuleBuilder.fromApp(fwd.appId())
994 .withPriority(fwd.priority())
995 .forDevice(deviceId)
996 .withSelector(filteredSelector)
997 .withTreatment(filteredTreatment)
998 .forTable(BRIDGING_TABLE);
999 if (fwd.permanent()) {
1000 flowRuleBuilder.makePermanent();
1001 } else {
1002 flowRuleBuilder.makeTemporary(fwd.timeout());
1003 }
1004 rules.add(flowRuleBuilder.build());
1005 return rules;
1006 }
1007
Saurav Das423fe2b2015-12-04 10:52:59 -08001008 protected NextGroup getGroupForNextObjective(Integer nextId) {
1009 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
1010 if (next != null) {
1011 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1012 if (gkeys != null && !gkeys.isEmpty()) {
1013 return next;
1014 } else {
1015 log.warn("Empty next group found in FlowObjective store for "
1016 + "next-id:{} in dev:{}", nextId, deviceId);
1017 }
1018 } else {
1019 log.warn("next-id {} not found in Flow objective store for dev:{}",
1020 nextId, deviceId);
1021 }
1022 return null;
1023 }
1024
Charles Chan188ebf52015-12-23 00:15:11 -08001025 protected static void pass(Objective obj) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001026 obj.context().ifPresent(context -> context.onSuccess(obj));
Saurav Das822c4e22015-10-23 10:51:11 -07001027 }
1028
Charles Chan188ebf52015-12-23 00:15:11 -08001029 protected static void fail(Objective obj, ObjectiveError error) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001030 obj.context().ifPresent(context -> context.onError(obj, error));
Saurav Das822c4e22015-10-23 10:51:11 -07001031 }
Saurav Das24431192016-03-07 19:13:00 -08001032
Saurav Das24431192016-03-07 19:13:00 -08001033 @Override
1034 public List<String> getNextMappings(NextGroup nextGroup) {
1035 List<String> mappings = new ArrayList<>();
1036 List<Deque<GroupKey>> gkeys = appKryo.deserialize(nextGroup.data());
1037 for (Deque<GroupKey> gkd : gkeys) {
1038 Group lastGroup = null;
Saurav Das8be4e3a2016-03-11 17:19:07 -08001039 StringBuffer gchain = new StringBuffer();
Saurav Das24431192016-03-07 19:13:00 -08001040 for (GroupKey gk : gkd) {
1041 Group g = groupService.getGroup(deviceId, gk);
Saurav Das8be4e3a2016-03-11 17:19:07 -08001042 if (g == null) {
1043 gchain.append(" ERROR").append(" -->");
1044 continue;
1045 }
1046 gchain.append(" 0x").append(Integer.toHexString(g.id().id()))
1047 .append(" -->");
Saurav Das24431192016-03-07 19:13:00 -08001048 lastGroup = g;
1049 }
1050 // add port information for last group in group-chain
1051 for (Instruction i: lastGroup.buckets().buckets().get(0).treatment().allInstructions()) {
1052 if (i instanceof OutputInstruction) {
Saurav Das8be4e3a2016-03-11 17:19:07 -08001053 gchain.append(" port:").append(((OutputInstruction) i).port());
Saurav Das24431192016-03-07 19:13:00 -08001054 }
1055 }
Saurav Das8be4e3a2016-03-11 17:19:07 -08001056 mappings.add(gchain.toString());
Saurav Das24431192016-03-07 19:13:00 -08001057 }
1058 return mappings;
1059 }
Saurav Das822c4e22015-10-23 10:51:11 -07001060}