blob: cd25643951627a9cb47f4c583626ae08e6a2979f [file] [log] [blame]
Saurav Das822c4e22015-10-23 10:51:11 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Saurav Das822c4e22015-10-23 10:51:11 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.driver.pipeline;
17
Charles Chan5270ed02016-01-30 23:22:37 -080018import com.google.common.collect.ImmutableList;
Yi Tseng47f82dc2017-03-05 22:48:39 -080019import com.google.common.collect.Sets;
Saurav Das822c4e22015-10-23 10:51:11 -070020import org.onlab.osgi.ServiceDirectory;
Saurav Das822c4e22015-10-23 10:51:11 -070021import org.onlab.packet.Ethernet;
Charles Chan14967c22015-12-07 11:11:50 -080022import org.onlab.packet.IpPrefix;
Saurav Das822c4e22015-10-23 10:51:11 -070023import org.onlab.packet.VlanId;
24import org.onlab.util.KryoNamespace;
25import org.onosproject.core.ApplicationId;
26import org.onosproject.core.CoreService;
Charles Chancad338a2016-09-16 18:03:11 -070027import org.onosproject.driver.extensions.Ofdpa3MplsType;
28import org.onosproject.driver.extensions.Ofdpa3SetMplsType;
Charles Chan14967c22015-12-07 11:11:50 -080029import org.onosproject.driver.extensions.OfdpaMatchVlanVid;
30import org.onosproject.driver.extensions.OfdpaSetVlanVid;
Saurav Das822c4e22015-10-23 10:51:11 -070031import org.onosproject.net.DeviceId;
32import org.onosproject.net.Port;
33import org.onosproject.net.PortNumber;
34import org.onosproject.net.behaviour.NextGroup;
35import org.onosproject.net.behaviour.Pipeliner;
36import org.onosproject.net.behaviour.PipelinerContext;
37import org.onosproject.net.device.DeviceService;
38import org.onosproject.net.driver.AbstractHandlerBehaviour;
39import org.onosproject.net.flow.DefaultFlowRule;
40import org.onosproject.net.flow.DefaultTrafficSelector;
41import org.onosproject.net.flow.DefaultTrafficTreatment;
42import org.onosproject.net.flow.FlowRule;
43import org.onosproject.net.flow.FlowRuleOperations;
44import org.onosproject.net.flow.FlowRuleOperationsContext;
45import org.onosproject.net.flow.FlowRuleService;
46import org.onosproject.net.flow.TrafficSelector;
47import org.onosproject.net.flow.TrafficTreatment;
48import org.onosproject.net.flow.criteria.Criteria;
49import org.onosproject.net.flow.criteria.Criterion;
50import org.onosproject.net.flow.criteria.EthCriterion;
51import org.onosproject.net.flow.criteria.EthTypeCriterion;
Charles Chan14967c22015-12-07 11:11:50 -080052import org.onosproject.net.flow.criteria.ExtensionCriterion;
Saurav Das822c4e22015-10-23 10:51:11 -070053import org.onosproject.net.flow.criteria.IPCriterion;
Pier Ventree0ae7a32016-11-23 09:57:42 -080054import org.onosproject.net.flow.criteria.Icmpv6CodeCriterion;
55import org.onosproject.net.flow.criteria.Icmpv6TypeCriterion;
Saurav Das8a0732e2015-11-20 15:27:53 -080056import org.onosproject.net.flow.criteria.MplsBosCriterion;
57import org.onosproject.net.flow.criteria.MplsCriterion;
Saurav Das822c4e22015-10-23 10:51:11 -070058import org.onosproject.net.flow.criteria.PortCriterion;
59import org.onosproject.net.flow.criteria.VlanIdCriterion;
60import org.onosproject.net.flow.instructions.Instruction;
61import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
62import org.onosproject.net.flow.instructions.L2ModificationInstruction;
Saurav Das8a0732e2015-11-20 15:27:53 -080063import org.onosproject.net.flow.instructions.L2ModificationInstruction.L2SubType;
Saurav Das822c4e22015-10-23 10:51:11 -070064import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanIdInstruction;
Charles Chan14967c22015-12-07 11:11:50 -080065import org.onosproject.net.flow.instructions.L3ModificationInstruction;
66import org.onosproject.net.flow.instructions.L3ModificationInstruction.L3SubType;
Saurav Das822c4e22015-10-23 10:51:11 -070067import org.onosproject.net.flowobjective.FilteringObjective;
68import org.onosproject.net.flowobjective.FlowObjectiveStore;
69import org.onosproject.net.flowobjective.ForwardingObjective;
70import org.onosproject.net.flowobjective.NextObjective;
71import org.onosproject.net.flowobjective.Objective;
72import org.onosproject.net.flowobjective.ObjectiveError;
Saurav Das822c4e22015-10-23 10:51:11 -070073import org.onosproject.net.group.DefaultGroupKey;
74import org.onosproject.net.group.Group;
Saurav Das822c4e22015-10-23 10:51:11 -070075import org.onosproject.net.group.GroupKey;
Saurav Das822c4e22015-10-23 10:51:11 -070076import org.onosproject.net.group.GroupService;
Saurav Das822c4e22015-10-23 10:51:11 -070077import org.onosproject.store.serializers.KryoNamespaces;
78import org.slf4j.Logger;
79
Pier Ventree0ae7a32016-11-23 09:57:42 -080080import java.util.ArrayDeque;
81import java.util.ArrayList;
82import java.util.Collection;
83import java.util.Collections;
84import java.util.Deque;
85import java.util.List;
86import java.util.Objects;
Pier Ventree0ae7a32016-11-23 09:57:42 -080087import java.util.concurrent.ScheduledExecutorService;
88import java.util.concurrent.TimeUnit;
89
90import static java.util.concurrent.Executors.newScheduledThreadPool;
Yi Tseng3a77b4f2017-02-06 15:02:17 -080091import static org.onlab.packet.MacAddress.BROADCAST;
92import static org.onlab.packet.MacAddress.NONE;
Pier Ventree0ae7a32016-11-23 09:57:42 -080093import static org.onlab.util.Tools.groupedThreads;
94import static org.slf4j.LoggerFactory.getLogger;
Pier Ventre140a8942016-11-02 07:26:38 -070095import static org.onosproject.net.flow.criteria.Criterion.Type.MPLS_BOS;
96import static org.onosproject.net.flowobjective.NextObjective.Type.HASHED;
Pier Ventree0ae7a32016-11-23 09:57:42 -080097
Saurav Das822c4e22015-10-23 10:51:11 -070098/**
99 * Driver for Broadcom's OF-DPA v2.0 TTP.
100 *
101 */
Charles Chan361154b2016-03-24 10:23:39 -0700102public class Ofdpa2Pipeline extends AbstractHandlerBehaviour implements Pipeliner {
Pier Ventre42287df2016-11-09 14:17:26 -0800103
Saurav Das822c4e22015-10-23 10:51:11 -0700104 protected static final int PORT_TABLE = 0;
105 protected static final int VLAN_TABLE = 10;
Pier Ventre42287df2016-11-09 14:17:26 -0800106 protected static final int VLAN_1_TABLE = 11;
107 protected static final int MPLS_L2_PORT_FLOW_TABLE = 13;
108 protected static final int MPLS_L2_PORT_PCP_TRUST_FLOW_TABLE = 16;
Saurav Das822c4e22015-10-23 10:51:11 -0700109 protected static final int TMAC_TABLE = 20;
110 protected static final int UNICAST_ROUTING_TABLE = 30;
111 protected static final int MULTICAST_ROUTING_TABLE = 40;
112 protected static final int MPLS_TABLE_0 = 23;
113 protected static final int MPLS_TABLE_1 = 24;
Pier Ventre140a8942016-11-02 07:26:38 -0700114 protected static final int MPLS_L3_TYPE_TABLE = 27;
115 protected static final int MPLS_TYPE_TABLE = 29;
Saurav Das822c4e22015-10-23 10:51:11 -0700116 protected static final int BRIDGING_TABLE = 50;
117 protected static final int ACL_TABLE = 60;
118 protected static final int MAC_LEARNING_TABLE = 254;
119 protected static final long OFPP_MAX = 0xffffff00L;
120
Saurav Das52025962016-01-28 22:30:01 -0800121 protected static final int HIGHEST_PRIORITY = 0xffff;
Saurav Das2857f382015-11-03 14:39:27 -0800122 protected static final int DEFAULT_PRIORITY = 0x8000;
Saurav Das822c4e22015-10-23 10:51:11 -0700123 protected static final int LOWEST_PRIORITY = 0x0;
124
Pier Ventre42287df2016-11-09 14:17:26 -0800125 protected static final int MPLS_L2_PORT_PRIORITY = 2;
126
127 protected static final int MPLS_TUNNEL_ID_BASE = 0x10000;
128 protected static final int MPLS_TUNNEL_ID_MAX = 0x1FFFF;
129
Pier Ventre70d53ba2016-11-17 22:26:29 -0800130 protected static final int MPLS_UNI_PORT_MAX = 0x0000FFFF;
131
132 protected static final int MPLS_NNI_PORT_BASE = 0x00020000;
133 protected static final int MPLS_NNI_PORT_MAX = 0x0002FFFF;
134
Saurav Das822c4e22015-10-23 10:51:11 -0700135 private final Logger log = getLogger(getClass());
Charles Chan425854b2016-04-11 15:32:12 -0700136 protected ServiceDirectory serviceDirectory;
Saurav Das822c4e22015-10-23 10:51:11 -0700137 protected FlowRuleService flowRuleService;
Charles Chan425854b2016-04-11 15:32:12 -0700138 protected CoreService coreService;
Saurav Das8a0732e2015-11-20 15:27:53 -0800139 protected GroupService groupService;
140 protected FlowObjectiveStore flowObjectiveStore;
Saurav Das822c4e22015-10-23 10:51:11 -0700141 protected DeviceId deviceId;
142 protected ApplicationId driverId;
Saurav Das822c4e22015-10-23 10:51:11 -0700143 protected DeviceService deviceService;
Charles Chan188ebf52015-12-23 00:15:11 -0800144 protected static KryoNamespace appKryo = new KryoNamespace.Builder()
Saurav Das822c4e22015-10-23 10:51:11 -0700145 .register(KryoNamespaces.API)
146 .register(GroupKey.class)
147 .register(DefaultGroupKey.class)
Charles Chan361154b2016-03-24 10:23:39 -0700148 .register(Ofdpa2GroupHandler.OfdpaNextGroup.class)
Saurav Das8a0732e2015-11-20 15:27:53 -0800149 .register(ArrayDeque.class)
Charles Chaneefdedf2016-05-23 16:45:45 -0700150 .build("Ofdpa2Pipeline");
Saurav Das822c4e22015-10-23 10:51:11 -0700151
Charles Chan425854b2016-04-11 15:32:12 -0700152 protected Ofdpa2GroupHandler groupHandler;
Saurav Das822c4e22015-10-23 10:51:11 -0700153
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700154 // flows installations to be retried
155 protected ScheduledExecutorService executorService
156 = newScheduledThreadPool(5, groupedThreads("OfdpaPipeliner", "retry-%d", log));
157 protected static final int MAX_RETRY_ATTEMPTS = 10;
158 protected static final int RETRY_MS = 1000;
Saurav Das4f980082015-11-05 13:39:15 -0800159
Saurav Das822c4e22015-10-23 10:51:11 -0700160 @Override
161 public void init(DeviceId deviceId, PipelinerContext context) {
Saurav Das822c4e22015-10-23 10:51:11 -0700162 this.deviceId = deviceId;
163
Charles Chan425854b2016-04-11 15:32:12 -0700164 serviceDirectory = context.directory();
Saurav Das822c4e22015-10-23 10:51:11 -0700165 coreService = serviceDirectory.get(CoreService.class);
166 flowRuleService = serviceDirectory.get(FlowRuleService.class);
167 groupService = serviceDirectory.get(GroupService.class);
168 flowObjectiveStore = context.store();
Saurav Das822c4e22015-10-23 10:51:11 -0700169 deviceService = serviceDirectory.get(DeviceService.class);
Saurav Das822c4e22015-10-23 10:51:11 -0700170
Charles Chan40132b32017-01-22 00:19:37 -0800171 initDriverId();
172 initGroupHander(context);
Saurav Das822c4e22015-10-23 10:51:11 -0700173
Saurav Das822c4e22015-10-23 10:51:11 -0700174 initializePipeline();
Saurav Das822c4e22015-10-23 10:51:11 -0700175 }
176
Charles Chan40132b32017-01-22 00:19:37 -0800177 protected void initDriverId() {
178 driverId = coreService.registerApplication(
179 "org.onosproject.driver.Ofdpa2Pipeline");
180 }
181
182 protected void initGroupHander(PipelinerContext context) {
183 groupHandler = new Ofdpa2GroupHandler();
184 groupHandler.init(deviceId, context);
185 }
186
Saurav Das822c4e22015-10-23 10:51:11 -0700187 protected void initializePipeline() {
Charles Chan188ebf52015-12-23 00:15:11 -0800188 // OF-DPA does not require initializing the pipeline as it puts default
189 // rules automatically in the hardware. However emulation of OFDPA in
190 // software switches does require table-miss-entries.
Saurav Das822c4e22015-10-23 10:51:11 -0700191 }
192
Charles Chand1172632017-03-15 17:33:09 -0700193 /**
194 * Determines whether this pipeline requires OFDPA match and set VLAN extensions.
195 *
196 * @return true to use the extensions
197 */
198 protected boolean requireVlanExtensions() {
199 return true;
200 }
201
Saurav Das822c4e22015-10-23 10:51:11 -0700202 //////////////////////////////////////
203 // Flow Objectives
204 //////////////////////////////////////
205
206 @Override
207 public void filter(FilteringObjective filteringObjective) {
208 if (filteringObjective.type() == FilteringObjective.Type.PERMIT) {
209 processFilter(filteringObjective,
210 filteringObjective.op() == Objective.Operation.ADD,
211 filteringObjective.appId());
212 } else {
213 // Note that packets that don't match the PERMIT filter are
214 // automatically denied. The DENY filter is used to deny packets
215 // that are otherwise permitted by the PERMIT filter.
216 // Use ACL table flow rules here for DENY filtering objectives
217 log.debug("filter objective other than PERMIT currently not supported");
218 fail(filteringObjective, ObjectiveError.UNSUPPORTED);
219 }
220 }
221
222 @Override
223 public void forward(ForwardingObjective fwd) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700224 Collection<FlowRule> rules = processForward(fwd);
Saurav Das25190812016-05-27 13:54:07 -0700225 if (rules == null || rules.isEmpty()) {
226 // Assumes fail message has already been generated to the objective
227 // context. Returning here prevents spurious pass message to be
228 // generated by FlowRule service for empty flowOps.
229 return;
230 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700231 sendForward(fwd, rules);
232 }
233
234 protected void sendForward(ForwardingObjective fwd, Collection<FlowRule> rules) {
235 FlowRuleOperations.Builder flowOpsBuilder = FlowRuleOperations.builder();
Saurav Das822c4e22015-10-23 10:51:11 -0700236 switch (fwd.op()) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700237 case ADD:
238 rules.stream()
239 .filter(Objects::nonNull)
240 .forEach(flowOpsBuilder::add);
Saurav Dasd2fded02016-12-02 15:43:47 -0800241 log.debug("Applying a add fwd-obj {} to sw:{}", fwd.id(), deviceId);
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700242 break;
243 case REMOVE:
244 rules.stream()
245 .filter(Objects::nonNull)
246 .forEach(flowOpsBuilder::remove);
Pier Ventre42287df2016-11-09 14:17:26 -0800247 log.debug("Deleting a flow rule to sw:{}", deviceId);
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700248 break;
249 default:
250 fail(fwd, ObjectiveError.UNKNOWN);
251 log.warn("Unknown forwarding type {}", fwd.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700252 }
253
Saurav Das822c4e22015-10-23 10:51:11 -0700254 flowRuleService.apply(flowOpsBuilder.build(new FlowRuleOperationsContext() {
255 @Override
256 public void onSuccess(FlowRuleOperations ops) {
257 pass(fwd);
258 }
259
260 @Override
261 public void onError(FlowRuleOperations ops) {
262 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
263 }
264 }));
Saurav Das822c4e22015-10-23 10:51:11 -0700265 }
266
267 @Override
268 public void next(NextObjective nextObjective) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800269 NextGroup nextGroup = flowObjectiveStore.getNextGroup(nextObjective.id());
270 switch (nextObjective.op()) {
271 case ADD:
Saurav Das4f980082015-11-05 13:39:15 -0800272 if (nextGroup != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800273 log.warn("Cannot add next {} that already exists in device {}",
274 nextObjective.id(), deviceId);
275 return;
276 }
277 log.debug("Processing NextObjective id{} in dev{} - add group",
278 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700279 groupHandler.addGroup(nextObjective);
Saurav Das8a0732e2015-11-20 15:27:53 -0800280 break;
281 case ADD_TO_EXISTING:
282 if (nextGroup != null) {
283 log.debug("Processing NextObjective id{} in dev{} - add bucket",
284 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700285 groupHandler.addBucketToGroup(nextObjective, nextGroup);
Saurav Das4f980082015-11-05 13:39:15 -0800286 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800287 // it is possible that group-chain has not been fully created yet
Saurav Das423fe2b2015-12-04 10:52:59 -0800288 log.debug("Waiting to add bucket to group for next-id:{} in dev:{}",
289 nextObjective.id(), deviceId);
Yi Tseng47f82dc2017-03-05 22:48:39 -0800290
291 // by design multiple pending bucket is allowed for the group
292 groupHandler.pendingBuckets.compute(nextObjective.id(), (nextId, pendBkts) -> {
293 if (pendBkts == null) {
294 pendBkts = Sets.newHashSet();
295 }
296 pendBkts.add(nextObjective);
297 return pendBkts;
298 });
Saurav Das4f980082015-11-05 13:39:15 -0800299 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800300 break;
301 case REMOVE:
302 if (nextGroup == null) {
303 log.warn("Cannot remove next {} that does not exist in device {}",
304 nextObjective.id(), deviceId);
305 return;
306 }
307 log.debug("Processing NextObjective id{} in dev{} - remove group",
308 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700309 groupHandler.removeGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800310 break;
311 case REMOVE_FROM_EXISTING:
312 if (nextGroup == null) {
313 log.warn("Cannot remove from next {} that does not exist in device {}",
314 nextObjective.id(), deviceId);
315 return;
316 }
317 log.debug("Processing NextObjective id{} in dev{} - remove bucket",
318 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700319 groupHandler.removeBucketFromGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800320 break;
321 default:
Saurav Das4f980082015-11-05 13:39:15 -0800322 log.warn("Unsupported operation {}", nextObjective.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700323 }
324 }
325
326 //////////////////////////////////////
327 // Flow handling
328 //////////////////////////////////////
329
330 /**
Yi Tseng0fdc07e2017-04-24 19:17:08 -0700331 * As per OFDPA 2.0 TTP, filtering of VLAN ids and MAC addresses (for routing)
332 * configured on switch ports happen in different tables.
Saurav Das822c4e22015-10-23 10:51:11 -0700333 *
334 * @param filt the filtering objective
335 * @param install indicates whether to add or remove the objective
336 * @param applicationId the application that sent this objective
337 */
Saurav Das52025962016-01-28 22:30:01 -0800338 protected void processFilter(FilteringObjective filt,
339 boolean install, ApplicationId applicationId) {
Saurav Das822c4e22015-10-23 10:51:11 -0700340 // This driver only processes filtering criteria defined with switch
341 // ports as the key
Yi Tseng0fdc07e2017-04-24 19:17:08 -0700342 PortCriterion portCriterion;
Saurav Das822c4e22015-10-23 10:51:11 -0700343 EthCriterion ethCriterion = null;
344 VlanIdCriterion vidCriterion = null;
Saurav Das822c4e22015-10-23 10:51:11 -0700345 if (!filt.key().equals(Criteria.dummy()) &&
346 filt.key().type() == Criterion.Type.IN_PORT) {
347 portCriterion = (PortCriterion) filt.key();
348 } else {
349 log.warn("No key defined in filtering objective from app: {}. Not"
350 + "processing filtering objective", applicationId);
Saurav Das59232cf2016-04-27 18:35:50 -0700351 fail(filt, ObjectiveError.BADPARAMS);
Saurav Das822c4e22015-10-23 10:51:11 -0700352 return;
353 }
Saurav Das59232cf2016-04-27 18:35:50 -0700354 log.debug("Received filtering objective for dev/port: {}/{}", deviceId,
355 portCriterion.port());
Saurav Das822c4e22015-10-23 10:51:11 -0700356 // convert filtering conditions for switch-intfs into flowrules
357 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
358 for (Criterion criterion : filt.conditions()) {
Yi Tseng0fdc07e2017-04-24 19:17:08 -0700359 switch (criterion.type()) {
360 case ETH_DST:
361 case ETH_DST_MASKED:
362 ethCriterion = (EthCriterion) criterion;
363 break;
364 case VLAN_VID:
365 vidCriterion = (VlanIdCriterion) criterion;
366 break;
367 default:
368 log.warn("Unsupported filter {}", criterion);
369 fail(filt, ObjectiveError.UNSUPPORTED);
370 return;
Saurav Das822c4e22015-10-23 10:51:11 -0700371 }
372 }
373
Saurav Das0e99e2b2015-10-28 12:39:42 -0700374 VlanId assignedVlan = null;
Charles Chane849c192016-01-11 18:28:54 -0800375 if (vidCriterion != null) {
Charles Chand55e84d2016-03-30 17:54:24 -0700376 // Use the VLAN in metadata whenever a metadata is provided
377 if (filt.meta() != null) {
378 assignedVlan = readVlanFromTreatment(filt.meta());
379 // Use the VLAN in criterion if metadata is not present and the traffic is tagged
380 } else if (!vidCriterion.vlanId().equals(VlanId.NONE)) {
Charles Chane849c192016-01-11 18:28:54 -0800381 assignedVlan = vidCriterion.vlanId();
Charles Chand55e84d2016-03-30 17:54:24 -0700382 }
Charles Chane849c192016-01-11 18:28:54 -0800383
Charles Chand55e84d2016-03-30 17:54:24 -0700384 if (assignedVlan == null) {
385 log.error("Driver fails to extract VLAN information. "
386 + "Not proccessing VLAN filters on device {}.", deviceId);
387 log.debug("VLAN ID in criterion={}, metadata={}",
388 readVlanFromTreatment(filt.meta()), vidCriterion.vlanId());
389 fail(filt, ObjectiveError.BADPARAMS);
390 return;
Saurav Das0e99e2b2015-10-28 12:39:42 -0700391 }
392 }
393
Yi Tseng3a77b4f2017-02-06 15:02:17 -0800394 if (ethCriterion == null || ethCriterion.mac().equals(NONE)) {
Charles Chan985b12e2016-05-11 19:47:22 -0700395 // NOTE: it is possible that a filtering objective only has vidCriterion
Saurav Das961beb22017-03-29 19:09:17 -0700396 log.warn("filtering objective missing dstMac, cannot program TMAC table");
Saurav Das822c4e22015-10-23 10:51:11 -0700397 } else {
398 for (FlowRule tmacRule : processEthDstFilter(portCriterion, ethCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700399 vidCriterion, assignedVlan,
400 applicationId)) {
Saurav Das961beb22017-03-29 19:09:17 -0700401 log.trace("{} MAC filtering rules in TMAC table: {} for dev: {}",
Saurav Das018605f2017-02-18 14:05:44 -0800402 (install) ? "adding" : "removing", tmacRule, deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700403 ops = install ? ops.add(tmacRule) : ops.remove(tmacRule);
404 }
405 }
406
Charles Chan985b12e2016-05-11 19:47:22 -0700407 if (vidCriterion == null) {
408 // NOTE: it is possible that a filtering objective only has ethCriterion
Charles Chan053b1cb2017-03-22 16:56:35 -0700409 log.debug("filtering objective missing VLAN, cannot program VLAN Table");
Saurav Das822c4e22015-10-23 10:51:11 -0700410 } else {
Charles Chan14967c22015-12-07 11:11:50 -0800411 /*
412 * NOTE: Separate vlan filtering rules and assignment rules
413 * into different stage in order to guarantee that filtering rules
Saurav Das52025962016-01-28 22:30:01 -0800414 * always go first, as required by ofdpa.
Charles Chan14967c22015-12-07 11:11:50 -0800415 */
416 List<FlowRule> allRules = processVlanIdFilter(
417 portCriterion, vidCriterion, assignedVlan, applicationId);
418 List<FlowRule> filteringRules = new ArrayList<>();
419 List<FlowRule> assignmentRules = new ArrayList<>();
420
421 allRules.forEach(flowRule -> {
Charles Chand1172632017-03-15 17:33:09 -0700422 VlanId vlanId;
423 if (requireVlanExtensions()) {
424 ExtensionCriterion extCriterion =
425 (ExtensionCriterion) flowRule.selector().getCriterion(Criterion.Type.EXTENSION);
426 vlanId = ((OfdpaMatchVlanVid) extCriterion.extensionSelector()).vlanId();
427 } else {
428 VlanIdCriterion vlanIdCriterion =
429 (VlanIdCriterion) flowRule.selector().getCriterion(Criterion.Type.VLAN_VID);
430 vlanId = vlanIdCriterion.vlanId();
431 }
Charles Chanbe8aea42016-02-24 12:04:47 -0800432 if (!vlanId.equals(VlanId.NONE)) {
Charles Chan14967c22015-12-07 11:11:50 -0800433 filteringRules.add(flowRule);
434 } else {
435 assignmentRules.add(flowRule);
436 }
437 });
438
439 for (FlowRule filteringRule : filteringRules) {
Saurav Das961beb22017-03-29 19:09:17 -0700440 log.trace("{} VLAN filtering rule in VLAN table: {} for dev: {}",
Saurav Das018605f2017-02-18 14:05:44 -0800441 (install) ? "adding" : "removing", filteringRule, deviceId);
Charles Chan14967c22015-12-07 11:11:50 -0800442 ops = install ? ops.add(filteringRule) : ops.remove(filteringRule);
443 }
444
445 ops.newStage();
446
447 for (FlowRule assignmentRule : assignmentRules) {
Saurav Das961beb22017-03-29 19:09:17 -0700448 log.trace("{} VLAN assignment rule in VLAN table: {} for dev: {}",
Saurav Das018605f2017-02-18 14:05:44 -0800449 (install) ? "adding" : "removing", assignmentRule, deviceId);
Charles Chan14967c22015-12-07 11:11:50 -0800450 ops = install ? ops.add(assignmentRule) : ops.remove(assignmentRule);
Saurav Das822c4e22015-10-23 10:51:11 -0700451 }
452 }
453
Saurav Das822c4e22015-10-23 10:51:11 -0700454 // apply filtering flow rules
455 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
456 @Override
457 public void onSuccess(FlowRuleOperations ops) {
Saurav Das018605f2017-02-18 14:05:44 -0800458 log.debug("Applied {} filtering rules in device {}",
Saurav Das822c4e22015-10-23 10:51:11 -0700459 ops.stages().get(0).size(), deviceId);
460 pass(filt);
461 }
462
463 @Override
464 public void onError(FlowRuleOperations ops) {
465 log.info("Failed to apply all filtering rules in dev {}", deviceId);
466 fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED);
467 }
468 }));
469
470 }
471
472 /**
Charles Chand1172632017-03-15 17:33:09 -0700473 * Internal implementation of processVlanIdFilter.
474 * <p>
475 * The is_present bit in set_vlan_vid action is required to be 0 in OFDPA i12.
476 * Since it is non-OF spec, we need an extension treatment for that.
477 * The useVlanExtension must be set to false for OFDPA i12.
478 * </p>
Saurav Das0e99e2b2015-10-28 12:39:42 -0700479 *
Charles Chanf9e98652016-09-07 16:54:23 -0700480 * @param portCriterion port on device for which this filter is programmed
481 * @param vidCriterion vlan assigned to port, or NONE for untagged
482 * @param assignedVlan assigned vlan-id for untagged packets
483 * @param applicationId for application programming this filter
Saurav Das822c4e22015-10-23 10:51:11 -0700484 * @return list of FlowRule for port-vlan filters
485 */
486 protected List<FlowRule> processVlanIdFilter(PortCriterion portCriterion,
Charles Chanf9e98652016-09-07 16:54:23 -0700487 VlanIdCriterion vidCriterion,
488 VlanId assignedVlan,
Charles Chand1172632017-03-15 17:33:09 -0700489 ApplicationId applicationId) {
Charles Chan14967c22015-12-07 11:11:50 -0800490 List<FlowRule> rules = new ArrayList<>();
Saurav Das822c4e22015-10-23 10:51:11 -0700491 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
492 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800493 TrafficSelector.Builder preSelector = null;
494 TrafficTreatment.Builder preTreatment = null;
495
Saurav Das4f980082015-11-05 13:39:15 -0800496 treatment.transition(TMAC_TABLE);
497
Saurav Das822c4e22015-10-23 10:51:11 -0700498 if (vidCriterion.vlanId() == VlanId.NONE) {
499 // untagged packets are assigned vlans
Charles Chand1172632017-03-15 17:33:09 -0700500 preSelector = DefaultTrafficSelector.builder();
501 if (requireVlanExtensions()) {
502 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(VlanId.NONE);
503 selector.extension(ofdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700504 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
505 treatment.extension(ofdpaSetVlanVid, deviceId);
Charles Chand1172632017-03-15 17:33:09 -0700506
507 OfdpaMatchVlanVid preOfdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
508 preSelector.extension(preOfdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700509 } else {
Charles Chand1172632017-03-15 17:33:09 -0700510 selector.matchVlanId(VlanId.NONE);
Charles Chanf9e98652016-09-07 16:54:23 -0700511 treatment.setVlanId(assignedVlan);
Charles Chand1172632017-03-15 17:33:09 -0700512
513 preSelector.matchVlanId(assignedVlan);
514 }
515 preTreatment = DefaultTrafficTreatment.builder().transition(TMAC_TABLE);
516 } else {
517 if (requireVlanExtensions()) {
518 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
519 selector.extension(ofdpaMatchVlanVid, deviceId);
520 } else {
521 selector.matchVlanId(vidCriterion.vlanId());
Charles Chanf9e98652016-09-07 16:54:23 -0700522 }
Charles Chan14967c22015-12-07 11:11:50 -0800523
Charles Chand55e84d2016-03-30 17:54:24 -0700524 if (!assignedVlan.equals(vidCriterion.vlanId())) {
Charles Chand1172632017-03-15 17:33:09 -0700525 if (requireVlanExtensions()) {
Charles Chanc03782d2017-01-31 13:57:55 -0800526 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
527 treatment.extension(ofdpaSetVlanVid, deviceId);
528 } else {
529 treatment.setVlanId(assignedVlan);
530 }
Charles Chand55e84d2016-03-30 17:54:24 -0700531 }
Saurav Das822c4e22015-10-23 10:51:11 -0700532 }
Saurav Das822c4e22015-10-23 10:51:11 -0700533
534 // ofdpa cannot match on ALL portnumber, so we need to use separate
535 // rules for each port.
Charles Chan14967c22015-12-07 11:11:50 -0800536 List<PortNumber> portnums = new ArrayList<>();
Saurav Das822c4e22015-10-23 10:51:11 -0700537 if (portCriterion.port() == PortNumber.ALL) {
538 for (Port port : deviceService.getPorts(deviceId)) {
539 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
540 portnums.add(port.number());
541 }
542 }
543 } else {
544 portnums.add(portCriterion.port());
545 }
Saurav Das4f980082015-11-05 13:39:15 -0800546
Saurav Das822c4e22015-10-23 10:51:11 -0700547 for (PortNumber pnum : portnums) {
Saurav Das4f980082015-11-05 13:39:15 -0800548 // create rest of flowrule
Saurav Das822c4e22015-10-23 10:51:11 -0700549 selector.matchInPort(pnum);
550 FlowRule rule = DefaultFlowRule.builder()
551 .forDevice(deviceId)
552 .withSelector(selector.build())
553 .withTreatment(treatment.build())
554 .withPriority(DEFAULT_PRIORITY)
555 .fromApp(applicationId)
556 .makePermanent()
557 .forTable(VLAN_TABLE).build();
Charles Chan14967c22015-12-07 11:11:50 -0800558
559 if (preSelector != null) {
560 preSelector.matchInPort(pnum);
561 FlowRule preRule = DefaultFlowRule.builder()
562 .forDevice(deviceId)
563 .withSelector(preSelector.build())
564 .withTreatment(preTreatment.build())
565 .withPriority(DEFAULT_PRIORITY)
566 .fromApp(applicationId)
567 .makePermanent()
568 .forTable(VLAN_TABLE).build();
569 rules.add(preRule);
570 }
571
Saurav Das822c4e22015-10-23 10:51:11 -0700572 rules.add(rule);
573 }
574 return rules;
575 }
576
577 /**
578 * Allows routed packets with correct destination MAC to be directed
579 * to unicast-IP routing table or MPLS forwarding table.
Saurav Das822c4e22015-10-23 10:51:11 -0700580 *
581 * @param portCriterion port on device for which this filter is programmed
582 * @param ethCriterion dstMac of device for which is filter is programmed
583 * @param vidCriterion vlan assigned to port, or NONE for untagged
Saurav Das0e99e2b2015-10-28 12:39:42 -0700584 * @param assignedVlan assigned vlan-id for untagged packets
Saurav Das822c4e22015-10-23 10:51:11 -0700585 * @param applicationId for application programming this filter
586 * @return list of FlowRule for port-vlan filters
587
588 */
589 protected List<FlowRule> processEthDstFilter(PortCriterion portCriterion,
590 EthCriterion ethCriterion,
591 VlanIdCriterion vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700592 VlanId assignedVlan,
Saurav Das822c4e22015-10-23 10:51:11 -0700593 ApplicationId applicationId) {
Charles Chan5270ed02016-01-30 23:22:37 -0800594 // Consider PortNumber.ANY as wildcard. Match ETH_DST only
595 if (portCriterion != null && portCriterion.port() == PortNumber.ANY) {
596 return processEthDstOnlyFilter(ethCriterion, applicationId);
597 }
598
Charles Chan5b9df8d2016-03-28 22:21:40 -0700599 // Multicast MAC
600 if (ethCriterion.mask() != null) {
601 return processMcastEthDstFilter(ethCriterion, applicationId);
602 }
603
Saurav Das822c4e22015-10-23 10:51:11 -0700604 //handling untagged packets via assigned VLAN
605 if (vidCriterion.vlanId() == VlanId.NONE) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700606 vidCriterion = (VlanIdCriterion) Criteria.matchVlanId(assignedVlan);
Saurav Das822c4e22015-10-23 10:51:11 -0700607 }
608 // ofdpa cannot match on ALL portnumber, so we need to use separate
609 // rules for each port.
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -0700610 List<PortNumber> portnums = new ArrayList<>();
Saurav Das822c4e22015-10-23 10:51:11 -0700611 if (portCriterion.port() == PortNumber.ALL) {
612 for (Port port : deviceService.getPorts(deviceId)) {
613 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
614 portnums.add(port.number());
615 }
616 }
617 } else {
618 portnums.add(portCriterion.port());
619 }
620
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -0700621 List<FlowRule> rules = new ArrayList<>();
Saurav Das822c4e22015-10-23 10:51:11 -0700622 for (PortNumber pnum : portnums) {
Charles Chan14967c22015-12-07 11:11:50 -0800623 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
Saurav Das822c4e22015-10-23 10:51:11 -0700624 // for unicast IP packets
625 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
626 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
627 selector.matchInPort(pnum);
Charles Chand1172632017-03-15 17:33:09 -0700628 if (requireVlanExtensions()) {
629 selector.extension(ofdpaMatchVlanVid, deviceId);
630 } else {
631 selector.matchVlanId(vidCriterion.vlanId());
632 }
Saurav Das822c4e22015-10-23 10:51:11 -0700633 selector.matchEthType(Ethernet.TYPE_IPV4);
634 selector.matchEthDst(ethCriterion.mac());
635 treatment.transition(UNICAST_ROUTING_TABLE);
636 FlowRule rule = DefaultFlowRule.builder()
637 .forDevice(deviceId)
638 .withSelector(selector.build())
639 .withTreatment(treatment.build())
640 .withPriority(DEFAULT_PRIORITY)
641 .fromApp(applicationId)
642 .makePermanent()
643 .forTable(TMAC_TABLE).build();
644 rules.add(rule);
645 //for MPLS packets
646 selector = DefaultTrafficSelector.builder();
647 treatment = DefaultTrafficTreatment.builder();
648 selector.matchInPort(pnum);
Charles Chand1172632017-03-15 17:33:09 -0700649 if (requireVlanExtensions()) {
650 selector.extension(ofdpaMatchVlanVid, deviceId);
651 } else {
652 selector.matchVlanId(vidCriterion.vlanId());
653 }
Saurav Das822c4e22015-10-23 10:51:11 -0700654 selector.matchEthType(Ethernet.MPLS_UNICAST);
655 selector.matchEthDst(ethCriterion.mac());
656 treatment.transition(MPLS_TABLE_0);
657 rule = DefaultFlowRule.builder()
658 .forDevice(deviceId)
659 .withSelector(selector.build())
660 .withTreatment(treatment.build())
661 .withPriority(DEFAULT_PRIORITY)
662 .fromApp(applicationId)
663 .makePermanent()
664 .forTable(TMAC_TABLE).build();
665 rules.add(rule);
Pier Ventree0ae7a32016-11-23 09:57:42 -0800666 /*
667 * TMAC rules for IPv6 packets
668 */
669 selector = DefaultTrafficSelector.builder();
670 treatment = DefaultTrafficTreatment.builder();
671 selector.matchInPort(pnum);
Charles Chand1172632017-03-15 17:33:09 -0700672 if (requireVlanExtensions()) {
673 selector.extension(ofdpaMatchVlanVid, deviceId);
674 } else {
675 selector.matchVlanId(vidCriterion.vlanId());
676 }
Pier Ventree0ae7a32016-11-23 09:57:42 -0800677 selector.matchEthType(Ethernet.TYPE_IPV6);
678 selector.matchEthDst(ethCriterion.mac());
679 treatment.transition(UNICAST_ROUTING_TABLE);
680 rule = DefaultFlowRule.builder()
681 .forDevice(deviceId)
682 .withSelector(selector.build())
683 .withTreatment(treatment.build())
684 .withPriority(DEFAULT_PRIORITY)
685 .fromApp(applicationId)
686 .makePermanent()
687 .forTable(TMAC_TABLE).build();
688 rules.add(rule);
Saurav Das822c4e22015-10-23 10:51:11 -0700689 }
690 return rules;
691 }
692
Charles Chan5270ed02016-01-30 23:22:37 -0800693 protected List<FlowRule> processEthDstOnlyFilter(EthCriterion ethCriterion,
694 ApplicationId applicationId) {
Pier Luigi0e358632017-01-31 09:35:05 -0800695 ImmutableList.Builder<FlowRule> builder = ImmutableList.builder();
696
Charles Chan5270ed02016-01-30 23:22:37 -0800697 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
698 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
699 selector.matchEthType(Ethernet.TYPE_IPV4);
700 selector.matchEthDst(ethCriterion.mac());
701 treatment.transition(UNICAST_ROUTING_TABLE);
702 FlowRule rule = DefaultFlowRule.builder()
703 .forDevice(deviceId)
704 .withSelector(selector.build())
705 .withTreatment(treatment.build())
706 .withPriority(DEFAULT_PRIORITY)
707 .fromApp(applicationId)
708 .makePermanent()
709 .forTable(TMAC_TABLE).build();
Pier Luigi0e358632017-01-31 09:35:05 -0800710 builder.add(rule);
711
712 selector = DefaultTrafficSelector.builder();
713 treatment = DefaultTrafficTreatment.builder();
714 selector.matchEthType(Ethernet.TYPE_IPV6);
715 selector.matchEthDst(ethCriterion.mac());
716 treatment.transition(UNICAST_ROUTING_TABLE);
717 rule = DefaultFlowRule.builder()
718 .forDevice(deviceId)
719 .withSelector(selector.build())
720 .withTreatment(treatment.build())
721 .withPriority(DEFAULT_PRIORITY)
722 .fromApp(applicationId)
723 .makePermanent()
724 .forTable(TMAC_TABLE).build();
725 return builder.add(rule).build();
Charles Chan5270ed02016-01-30 23:22:37 -0800726 }
727
Charles Chan5b9df8d2016-03-28 22:21:40 -0700728 protected List<FlowRule> processMcastEthDstFilter(EthCriterion ethCriterion,
729 ApplicationId applicationId) {
730 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
731 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
732 selector.matchEthType(Ethernet.TYPE_IPV4);
733 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
734 treatment.transition(MULTICAST_ROUTING_TABLE);
735 FlowRule rule = DefaultFlowRule.builder()
736 .forDevice(deviceId)
737 .withSelector(selector.build())
738 .withTreatment(treatment.build())
739 .withPriority(DEFAULT_PRIORITY)
740 .fromApp(applicationId)
741 .makePermanent()
742 .forTable(TMAC_TABLE).build();
743 return ImmutableList.<FlowRule>builder().add(rule).build();
744 }
745
Saurav Das822c4e22015-10-23 10:51:11 -0700746 private Collection<FlowRule> processForward(ForwardingObjective fwd) {
747 switch (fwd.flag()) {
748 case SPECIFIC:
749 return processSpecific(fwd);
750 case VERSATILE:
751 return processVersatile(fwd);
752 default:
753 fail(fwd, ObjectiveError.UNKNOWN);
754 log.warn("Unknown forwarding flag {}", fwd.flag());
755 }
756 return Collections.emptySet();
757 }
758
759 /**
760 * In the OF-DPA 2.0 pipeline, versatile forwarding objectives go to the
761 * ACL table.
762 * @param fwd the forwarding objective of type 'versatile'
763 * @return a collection of flow rules to be sent to the switch. An empty
764 * collection may be returned if there is a problem in processing
765 * the flow rule
766 */
Saurav Das52025962016-01-28 22:30:01 -0800767 protected Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
Saurav Das018605f2017-02-18 14:05:44 -0800768 log.debug("Processing versatile forwarding objective:{} in dev:{}",
Saurav Dasd2fded02016-12-02 15:43:47 -0800769 fwd.id(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700770
771 EthTypeCriterion ethType =
Saurav Das77b5e902016-01-27 17:01:59 -0800772 (EthTypeCriterion) fwd.selector().getCriterion(Criterion.Type.ETH_TYPE);
Saurav Das822c4e22015-10-23 10:51:11 -0700773 if (ethType == null) {
Saurav Dasd2fded02016-12-02 15:43:47 -0800774 log.error("Versatile forwarding objective:{} must include ethType",
775 fwd.id());
Saurav Das822c4e22015-10-23 10:51:11 -0700776 fail(fwd, ObjectiveError.BADPARAMS);
777 return Collections.emptySet();
778 }
779 if (fwd.nextId() == null && fwd.treatment() == null) {
780 log.error("Forwarding objective {} from {} must contain "
781 + "nextId or Treatment", fwd.selector(), fwd.appId());
Zsolt Haraszti9faab752016-02-17 15:55:20 -0800782 fail(fwd, ObjectiveError.BADPARAMS);
Saurav Das822c4e22015-10-23 10:51:11 -0700783 return Collections.emptySet();
784 }
Saurav Das49cb5a12016-01-16 22:54:07 -0800785
Saurav Das77b5e902016-01-27 17:01:59 -0800786 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
787 fwd.selector().criteria().forEach(criterion -> {
788 if (criterion instanceof VlanIdCriterion) {
789 VlanId vlanId = ((VlanIdCriterion) criterion).vlanId();
790 // ensure that match does not include vlan = NONE as OF-DPA does not
791 // match untagged packets this way in the ACL table.
792 if (vlanId.equals(VlanId.NONE)) {
793 return;
794 }
Charles Chand1172632017-03-15 17:33:09 -0700795 if (requireVlanExtensions()) {
796 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanId);
797 sbuilder.extension(ofdpaMatchVlanVid, deviceId);
798 } else {
799 sbuilder.matchVlanId(vlanId);
800 }
Pier Ventree0ae7a32016-11-23 09:57:42 -0800801 } else if (criterion instanceof Icmpv6TypeCriterion ||
802 criterion instanceof Icmpv6CodeCriterion) {
803 /*
804 * We silenty discard these criterions, our current
805 * OFDPA platform does not support these matches on
806 * the ACL table.
807 */
808 log.warn("ICMPv6 Type and ICMPv6 Code are not supported");
Saurav Das77b5e902016-01-27 17:01:59 -0800809 } else {
810 sbuilder.add(criterion);
811 }
812 });
813
Saurav Das822c4e22015-10-23 10:51:11 -0700814 // XXX driver does not currently do type checking as per Tables 65-67 in
815 // OFDPA 2.0 spec. The only allowed treatment is a punt to the controller.
Saurav Das49cb5a12016-01-16 22:54:07 -0800816 TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder();
817 if (fwd.treatment() != null) {
818 for (Instruction ins : fwd.treatment().allInstructions()) {
819 if (ins instanceof OutputInstruction) {
820 OutputInstruction o = (OutputInstruction) ins;
821 if (o.port() == PortNumber.CONTROLLER) {
822 ttBuilder.add(o);
823 } else {
824 log.warn("Only allowed treatments in versatile forwarding "
825 + "objectives are punts to the controller");
826 }
827 } else {
828 log.warn("Cannot process instruction in versatile fwd {}", ins);
829 }
Saurav Das822c4e22015-10-23 10:51:11 -0700830 }
Charles Chan2df0e8a2017-01-09 11:45:08 -0800831 if (fwd.treatment().clearedDeferred()) {
832 ttBuilder.wipeDeferred();
833 }
Saurav Das822c4e22015-10-23 10:51:11 -0700834 }
Saurav Das822c4e22015-10-23 10:51:11 -0700835 if (fwd.nextId() != null) {
Saurav Das49cb5a12016-01-16 22:54:07 -0800836 // overide case
837 NextGroup next = getGroupForNextObjective(fwd.nextId());
838 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
839 // we only need the top level group's key to point the flow to it
840 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
841 if (group == null) {
842 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
843 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
844 fail(fwd, ObjectiveError.GROUPMISSING);
845 return Collections.emptySet();
846 }
847 ttBuilder.deferred().group(group.id());
Saurav Das822c4e22015-10-23 10:51:11 -0700848 }
Saurav Das49cb5a12016-01-16 22:54:07 -0800849
850 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
851 .fromApp(fwd.appId())
852 .withPriority(fwd.priority())
853 .forDevice(deviceId)
Saurav Das77b5e902016-01-27 17:01:59 -0800854 .withSelector(sbuilder.build())
Saurav Das49cb5a12016-01-16 22:54:07 -0800855 .withTreatment(ttBuilder.build())
856 .makePermanent()
857 .forTable(ACL_TABLE);
858 return Collections.singletonList(ruleBuilder.build());
Saurav Das822c4e22015-10-23 10:51:11 -0700859 }
860
861 /**
862 * In the OF-DPA 2.0 pipeline, specific forwarding refers to the IP table
Saurav Das8a0732e2015-11-20 15:27:53 -0800863 * (unicast or multicast) or the L2 table (mac + vlan) or the MPLS table.
Saurav Das822c4e22015-10-23 10:51:11 -0700864 *
865 * @param fwd the forwarding objective of type 'specific'
866 * @return a collection of flow rules. Typically there will be only one
867 * for this type of forwarding objective. An empty set may be
868 * returned if there is an issue in processing the objective.
869 */
Saurav Das8a0732e2015-11-20 15:27:53 -0800870 protected Collection<FlowRule> processSpecific(ForwardingObjective fwd) {
Saurav Das25190812016-05-27 13:54:07 -0700871 log.debug("Processing specific fwd objective:{} in dev:{} with next:{}",
Saurav Das4ce45962015-11-24 23:21:05 -0800872 fwd.id(), deviceId, fwd.nextId());
873 boolean isEthTypeObj = isSupportedEthTypeObjective(fwd);
874 boolean isEthDstObj = isSupportedEthDstObjective(fwd);
875
876 if (isEthTypeObj) {
877 return processEthTypeSpecific(fwd);
878 } else if (isEthDstObj) {
879 return processEthDstSpecific(fwd);
880 } else {
881 log.warn("processSpecific: Unsupported forwarding objective "
882 + "criteria fwd:{} in dev:{}", fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700883 fail(fwd, ObjectiveError.UNSUPPORTED);
884 return Collections.emptySet();
885 }
Saurav Das4ce45962015-11-24 23:21:05 -0800886 }
887
Saurav Das4ce45962015-11-24 23:21:05 -0800888 /**
889 * Handles forwarding rules to the IP and MPLS tables.
890 *
891 * @param fwd the forwarding objective
892 * @return A collection of flow rules, or an empty set
893 */
894 protected Collection<FlowRule> processEthTypeSpecific(ForwardingObjective fwd) {
Charles Chancad338a2016-09-16 18:03:11 -0700895 return processEthTypeSpecificInternal(fwd, false, ACL_TABLE);
Charles Chanf9e98652016-09-07 16:54:23 -0700896 }
897
898 /**
899 * Internal implementation of processEthTypeSpecific.
900 * <p>
901 * Wildcarded IPv4_DST is not supported in OFDPA i12. Therefore, we break
902 * the rule into 0.0.0.0/1 and 128.0.0.0/1.
903 * The allowDefaultRoute must be set to false for OFDPA i12.
904 * </p>
905 *
906 * @param fwd the forwarding objective
907 * @param allowDefaultRoute allow wildcarded IPv4_DST or not
Ray Milkey0bb1e102016-11-10 14:51:27 -0800908 * @param mplsNextTable next MPLS table
Charles Chanf9e98652016-09-07 16:54:23 -0700909 * @return A collection of flow rules, or an empty set
910 */
911 protected Collection<FlowRule> processEthTypeSpecificInternal(ForwardingObjective fwd,
Charles Chancad338a2016-09-16 18:03:11 -0700912 boolean allowDefaultRoute,
913 int mplsNextTable) {
Saurav Das4ce45962015-11-24 23:21:05 -0800914 TrafficSelector selector = fwd.selector();
915 EthTypeCriterion ethType =
916 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
Flavio Castroe10fa242016-01-15 12:43:51 -0800917 boolean popMpls = false;
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700918 boolean emptyGroup = false;
Charles Chan188ebf52015-12-23 00:15:11 -0800919 int forTableId;
Saurav Das8a0732e2015-11-20 15:27:53 -0800920 TrafficSelector.Builder filteredSelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800921 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
Flavio Castroe10fa242016-01-15 12:43:51 -0800922 TrafficSelector.Builder complementarySelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800923
Saurav Das8a0732e2015-11-20 15:27:53 -0800924 if (ethType.ethType().toShort() == Ethernet.TYPE_IPV4) {
Pier Luigi3bfe32c2017-01-30 09:47:36 -0800925 if (buildIpv4Selector(filteredSelector, complementarySelector, fwd, allowDefaultRoute) < 0) {
926 return Collections.emptyList();
927 }
928 // We need to set properly the next table
Flavio Castroe10fa242016-01-15 12:43:51 -0800929 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700930 if (ipv4Dst.isMulticast()) {
Charles Chan5b9df8d2016-03-28 22:21:40 -0700931 forTableId = MULTICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -0800932 } else {
Charles Chan5b9df8d2016-03-28 22:21:40 -0700933 forTableId = UNICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -0800934 }
Charles Chan14967c22015-12-07 11:11:50 -0800935 if (fwd.treatment() != null) {
936 for (Instruction instr : fwd.treatment().allInstructions()) {
937 if (instr instanceof L3ModificationInstruction &&
938 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
Saurav Das9c705342017-01-06 11:36:01 -0800939 // XXX decrementing IP ttl is done automatically for routing, this
940 // action is ignored or rejected in ofdpa as it is not fully implemented
941 //tb.deferred().add(instr);
Charles Chan14967c22015-12-07 11:11:50 -0800942 }
943 }
944 }
945
Pier Ventree0ae7a32016-11-23 09:57:42 -0800946 } else if (ethType.ethType().toShort() == Ethernet.TYPE_IPV6) {
947 if (buildIpv6Selector(filteredSelector, fwd) < 0) {
948 return Collections.emptyList();
949 }
950 forTableId = UNICAST_ROUTING_TABLE;
951 if (fwd.treatment() != null) {
952 for (Instruction instr : fwd.treatment().allInstructions()) {
953 if (instr instanceof L3ModificationInstruction &&
954 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
955 // XXX decrementing IP ttl is done automatically for routing, this
956 // action is ignored or rejected in ofdpa as it is not fully implemented
957 //tb.deferred().add(instr);
958 }
959 }
960 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800961 } else {
962 filteredSelector
963 .matchEthType(Ethernet.MPLS_UNICAST)
964 .matchMplsLabel(((MplsCriterion)
965 selector.getCriterion(Criterion.Type.MPLS_LABEL)).label());
966 MplsBosCriterion bos = (MplsBosCriterion) selector
Pier Ventre140a8942016-11-02 07:26:38 -0700967 .getCriterion(MPLS_BOS);
Saurav Das8a0732e2015-11-20 15:27:53 -0800968 if (bos != null) {
969 filteredSelector.matchMplsBos(bos.mplsBos());
970 }
971 forTableId = MPLS_TABLE_1;
Saurav Das4ce45962015-11-24 23:21:05 -0800972 log.debug("processing MPLS specific forwarding objective {} -> next:{}"
973 + " in dev {}", fwd.id(), fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700974
Charles Chan14967c22015-12-07 11:11:50 -0800975 if (fwd.treatment() != null) {
976 for (Instruction instr : fwd.treatment().allInstructions()) {
977 if (instr instanceof L2ModificationInstruction &&
978 ((L2ModificationInstruction) instr).subtype() == L2SubType.MPLS_POP) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800979 popMpls = true;
Pier Luigi3bfe32c2017-01-30 09:47:36 -0800980 // OF-DPA does not pop in MPLS table in some cases. For the L3 VPN, it requires
Saurav Das9c705342017-01-06 11:36:01 -0800981 // setting the MPLS_TYPE so pop can happen down the pipeline
Pier Luigi3bfe32c2017-01-30 09:47:36 -0800982 if (mplsNextTable == MPLS_TYPE_TABLE && isNotMplsBos(selector)) {
983 tb.immediate().popMpls();
984 }
Charles Chan14967c22015-12-07 11:11:50 -0800985 }
986 if (instr instanceof L3ModificationInstruction &&
987 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
988 // FIXME Should modify the app to send the correct DEC_MPLS_TTL instruction
989 tb.immediate().decMplsTtl();
990 }
991 if (instr instanceof L3ModificationInstruction &&
992 ((L3ModificationInstruction) instr).subtype() == L3SubType.TTL_IN) {
993 tb.immediate().add(instr);
994 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800995 }
996 }
997 }
Saurav Das822c4e22015-10-23 10:51:11 -0700998
999 if (fwd.nextId() != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -08001000 if (forTableId == MPLS_TABLE_1 && !popMpls) {
1001 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
Saurav Das25190812016-05-27 13:54:07 -07001002 + "is not implemented in OF-DPA yet. Aborting this flow {} -> next:{}"
1003 + "in this device {}", fwd.id(), fwd.nextId(), deviceId);
Pier Ventree0ae7a32016-11-23 09:57:42 -08001004 // XXX We could convert to forwarding to a single-port, via a MPLS interface,
1005 // or a MPLS SWAP (with-same) but that would have to be handled in the next-objective.
1006 // Also the pop-mpls logic used here won't work in non-BoS case.
Saurav Das4ce45962015-11-24 23:21:05 -08001007 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
Saurav Das8a0732e2015-11-20 15:27:53 -08001008 return Collections.emptySet();
1009 }
1010
Saurav Das423fe2b2015-12-04 10:52:59 -08001011 NextGroup next = getGroupForNextObjective(fwd.nextId());
1012 if (next != null) {
1013 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1014 // we only need the top level group's key to point the flow to it
1015 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
Pier Ventre140a8942016-11-02 07:26:38 -07001016 if (isNotMplsBos(selector) && group.type().equals(HASHED)) {
1017 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
1018 + "is not implemented in OF-DPA yet. Aborting this flow {} -> next:{}"
1019 + "in this device {}", fwd.id(), fwd.nextId(), deviceId);
1020 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1021 return Collections.emptySet();
1022 }
Saurav Das423fe2b2015-12-04 10:52:59 -08001023 if (group == null) {
1024 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1025 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1026 fail(fwd, ObjectiveError.GROUPMISSING);
1027 return Collections.emptySet();
1028 }
1029 tb.deferred().group(group.id());
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001030 // check if group is empty
1031 if (gkeys.size() == 1 && gkeys.get(0).size() == 1) {
1032 log.warn("Found empty group 0x{} in dev:{} .. will retry fwd:{}",
1033 Integer.toHexString(group.id().id()), deviceId, fwd.id());
1034 emptyGroup = true;
1035 }
Saurav Das25190812016-05-27 13:54:07 -07001036 } else {
1037 log.warn("Cannot find group for nextId:{} in dev:{}. Aborting fwd:{}",
1038 fwd.nextId(), deviceId, fwd.id());
1039 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1040 return Collections.emptySet();
Saurav Das822c4e22015-10-23 10:51:11 -07001041 }
Saurav Das822c4e22015-10-23 10:51:11 -07001042 }
Charles Chancad338a2016-09-16 18:03:11 -07001043
1044 if (forTableId == MPLS_TABLE_1) {
Pier Ventre140a8942016-11-02 07:26:38 -07001045 if (mplsNextTable == MPLS_L3_TYPE_TABLE) {
Charles Chancad338a2016-09-16 18:03:11 -07001046 Ofdpa3SetMplsType setMplsType = new Ofdpa3SetMplsType(Ofdpa3MplsType.L3_PHP);
Saurav Das9c705342017-01-06 11:36:01 -08001047 // set mpls type as apply_action
1048 tb.immediate().extension(setMplsType, deviceId);
Charles Chancad338a2016-09-16 18:03:11 -07001049 }
1050 tb.transition(mplsNextTable);
1051 } else {
1052 tb.transition(ACL_TABLE);
1053 }
1054
Saurav Das822c4e22015-10-23 10:51:11 -07001055 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1056 .fromApp(fwd.appId())
1057 .withPriority(fwd.priority())
1058 .forDevice(deviceId)
Saurav Das8a0732e2015-11-20 15:27:53 -08001059 .withSelector(filteredSelector.build())
1060 .withTreatment(tb.build())
1061 .forTable(forTableId);
Saurav Das822c4e22015-10-23 10:51:11 -07001062
1063 if (fwd.permanent()) {
1064 ruleBuilder.makePermanent();
1065 } else {
1066 ruleBuilder.makeTemporary(fwd.timeout());
1067 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001068 Collection<FlowRule> flowRuleCollection = new ArrayList<>();
1069 flowRuleCollection.add(ruleBuilder.build());
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001070 if (!allowDefaultRoute) {
Pier Ventree0ae7a32016-11-23 09:57:42 -08001071 flowRuleCollection.add(
1072 defaultRoute(fwd, complementarySelector, forTableId, tb)
1073 );
Flavio Castroe10fa242016-01-15 12:43:51 -08001074 log.debug("Default rule 0.0.0.0/0 is being installed two rules");
1075 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001076 // XXX retrying flows may be necessary due to bug CORD-554
1077 if (emptyGroup) {
1078 executorService.schedule(new RetryFlows(fwd, flowRuleCollection),
1079 RETRY_MS, TimeUnit.MILLISECONDS);
1080 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001081 return flowRuleCollection;
Saurav Das822c4e22015-10-23 10:51:11 -07001082 }
1083
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001084 protected int buildIpv4Selector(TrafficSelector.Builder builderToUpdate,
1085 TrafficSelector.Builder extBuilder,
1086 ForwardingObjective fwd,
1087 boolean allowDefaultRoute) {
1088 TrafficSelector selector = fwd.selector();
1089
1090 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
1091 if (ipv4Dst.isMulticast()) {
1092 if (ipv4Dst.prefixLength() != 32) {
1093 log.warn("Multicast specific forwarding objective can only be /32");
1094 fail(fwd, ObjectiveError.BADPARAMS);
1095 return -1;
1096 }
1097 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
1098 if (assignedVlan == null) {
1099 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
1100 fail(fwd, ObjectiveError.BADPARAMS);
1101 return -1;
1102 }
Charles Chand1172632017-03-15 17:33:09 -07001103 if (requireVlanExtensions()) {
1104 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
1105 builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
1106 } else {
1107 builderToUpdate.matchVlanId(assignedVlan);
1108 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001109 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1110 log.debug("processing IPv4 multicast specific forwarding objective {} -> next:{}"
1111 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1112 } else {
1113 if (ipv4Dst.prefixLength() == 0) {
1114 if (allowDefaultRoute) {
1115 // The entire IPV4_DST field is wildcarded intentionally
1116 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4);
1117 } else {
1118 // NOTE: The switch does not support matching 0.0.0.0/0
1119 // Split it into 0.0.0.0/1 and 128.0.0.0/1
1120 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4)
1121 .matchIPDst(IpPrefix.valueOf("0.0.0.0/1"));
1122 extBuilder.matchEthType(Ethernet.TYPE_IPV4)
1123 .matchIPDst(IpPrefix.valueOf("128.0.0.0/1"));
1124 }
1125 } else {
1126 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1127 }
1128 log.debug("processing IPv4 unicast specific forwarding objective {} -> next:{}"
1129 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1130 }
1131 return 0;
1132 }
1133
1134 /**
1135 * Helper method to build Ipv6 selector using the selector provided by
1136 * a forwarding objective.
1137 *
1138 * @param builderToUpdate the builder to update
1139 * @param fwd the selector to read
1140 * @return 0 if the update ends correctly. -1 if the matches
1141 * are not yet supported
1142 */
1143 protected int buildIpv6Selector(TrafficSelector.Builder builderToUpdate,
1144 ForwardingObjective fwd) {
1145
1146 TrafficSelector selector = fwd.selector();
1147
1148 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
1149 if (ipv6Dst.isMulticast()) {
1150 log.warn("IPv6 Multicast is currently not supported");
1151 fail(fwd, ObjectiveError.BADPARAMS);
1152 return -1;
1153 }
1154 if (ipv6Dst.prefixLength() != 0) {
1155 builderToUpdate.matchIPv6Dst(ipv6Dst);
1156 }
1157 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6);
1158 log.debug("processing IPv6 unicast specific forwarding objective {} -> next:{}"
1159 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1160 return 0;
1161 }
1162
Pier Ventree0ae7a32016-11-23 09:57:42 -08001163 protected FlowRule defaultRoute(ForwardingObjective fwd,
1164 TrafficSelector.Builder complementarySelector,
1165 int forTableId,
1166 TrafficTreatment.Builder tb) {
1167 FlowRule.Builder rule = DefaultFlowRule.builder()
1168 .fromApp(fwd.appId())
1169 .withPriority(fwd.priority())
1170 .forDevice(deviceId)
1171 .withSelector(complementarySelector.build())
1172 .withTreatment(tb.build())
1173 .forTable(forTableId);
1174 if (fwd.permanent()) {
1175 rule.makePermanent();
1176 } else {
1177 rule.makeTemporary(fwd.timeout());
1178 }
1179 return rule.build();
1180 }
1181
Saurav Das4ce45962015-11-24 23:21:05 -08001182 /**
1183 * Handles forwarding rules to the L2 bridging table. Flow actions are not
1184 * allowed in the bridging table - instead we use L2 Interface group or
1185 * L2 flood group
1186 *
1187 * @param fwd the forwarding objective
1188 * @return A collection of flow rules, or an empty set
1189 */
1190 protected Collection<FlowRule> processEthDstSpecific(ForwardingObjective fwd) {
1191 List<FlowRule> rules = new ArrayList<>();
1192
1193 // Build filtered selector
1194 TrafficSelector selector = fwd.selector();
1195 EthCriterion ethCriterion = (EthCriterion) selector
1196 .getCriterion(Criterion.Type.ETH_DST);
1197 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) selector
1198 .getCriterion(Criterion.Type.VLAN_VID);
1199
1200 if (vlanIdCriterion == null) {
1201 log.warn("Forwarding objective for bridging requires vlan. Not "
1202 + "installing fwd:{} in dev:{}", fwd.id(), deviceId);
1203 fail(fwd, ObjectiveError.BADPARAMS);
1204 return Collections.emptySet();
1205 }
1206
1207 TrafficSelector.Builder filteredSelectorBuilder =
1208 DefaultTrafficSelector.builder();
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001209
1210 if (!ethCriterion.mac().equals(NONE) &&
1211 !ethCriterion.mac().equals(BROADCAST)) {
Saurav Das4ce45962015-11-24 23:21:05 -08001212 filteredSelectorBuilder.matchEthDst(ethCriterion.mac());
1213 log.debug("processing L2 forwarding objective:{} -> next:{} in dev:{}",
1214 fwd.id(), fwd.nextId(), deviceId);
1215 } else {
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001216 // Use wildcard DST_MAC if the MacAddress is None or Broadcast
Saurav Das4ce45962015-11-24 23:21:05 -08001217 log.debug("processing L2 Broadcast forwarding objective:{} -> next:{} "
1218 + "in dev:{} for vlan:{}",
1219 fwd.id(), fwd.nextId(), deviceId, vlanIdCriterion.vlanId());
1220 }
Charles Chand1172632017-03-15 17:33:09 -07001221 if (requireVlanExtensions()) {
1222 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanIdCriterion.vlanId());
1223 filteredSelectorBuilder.extension(ofdpaMatchVlanVid, deviceId);
1224 } else {
1225 filteredSelectorBuilder.matchVlanId(vlanIdCriterion.vlanId());
1226 }
Saurav Das4ce45962015-11-24 23:21:05 -08001227 TrafficSelector filteredSelector = filteredSelectorBuilder.build();
1228
1229 if (fwd.treatment() != null) {
1230 log.warn("Ignoring traffic treatment in fwd rule {} meant for L2 table"
1231 + "for dev:{}. Expecting only nextId", fwd.id(), deviceId);
1232 }
1233
1234 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1235 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001236 NextGroup next = getGroupForNextObjective(fwd.nextId());
Saurav Das4ce45962015-11-24 23:21:05 -08001237 if (next != null) {
1238 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1239 // we only need the top level group's key to point the flow to it
1240 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1241 if (group != null) {
1242 treatmentBuilder.deferred().group(group.id());
1243 } else {
1244 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1245 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1246 fail(fwd, ObjectiveError.GROUPMISSING);
1247 return Collections.emptySet();
1248 }
1249 }
1250 }
1251 treatmentBuilder.immediate().transition(ACL_TABLE);
1252 TrafficTreatment filteredTreatment = treatmentBuilder.build();
1253
1254 // Build bridging table entries
1255 FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder();
1256 flowRuleBuilder.fromApp(fwd.appId())
1257 .withPriority(fwd.priority())
1258 .forDevice(deviceId)
1259 .withSelector(filteredSelector)
1260 .withTreatment(filteredTreatment)
1261 .forTable(BRIDGING_TABLE);
1262 if (fwd.permanent()) {
1263 flowRuleBuilder.makePermanent();
1264 } else {
1265 flowRuleBuilder.makeTemporary(fwd.timeout());
1266 }
1267 rules.add(flowRuleBuilder.build());
1268 return rules;
1269 }
1270
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001271 //////////////////////////////////////
1272 // Helper Methods and Classes
1273 //////////////////////////////////////
1274
1275 private boolean isSupportedEthTypeObjective(ForwardingObjective fwd) {
1276 TrafficSelector selector = fwd.selector();
1277 EthTypeCriterion ethType = (EthTypeCriterion) selector
1278 .getCriterion(Criterion.Type.ETH_TYPE);
1279 return !((ethType == null) ||
1280 ((ethType.ethType().toShort() != Ethernet.TYPE_IPV4) &&
Pier Ventree0ae7a32016-11-23 09:57:42 -08001281 (ethType.ethType().toShort() != Ethernet.MPLS_UNICAST)) &&
1282 (ethType.ethType().toShort() != Ethernet.TYPE_IPV6));
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001283 }
1284
1285 private boolean isSupportedEthDstObjective(ForwardingObjective fwd) {
1286 TrafficSelector selector = fwd.selector();
1287 EthCriterion ethDst = (EthCriterion) selector
1288 .getCriterion(Criterion.Type.ETH_DST);
1289 VlanIdCriterion vlanId = (VlanIdCriterion) selector
1290 .getCriterion(Criterion.Type.VLAN_VID);
1291 return !(ethDst == null && vlanId == null);
1292 }
1293
Saurav Das423fe2b2015-12-04 10:52:59 -08001294 protected NextGroup getGroupForNextObjective(Integer nextId) {
1295 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
1296 if (next != null) {
1297 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1298 if (gkeys != null && !gkeys.isEmpty()) {
1299 return next;
1300 } else {
1301 log.warn("Empty next group found in FlowObjective store for "
1302 + "next-id:{} in dev:{}", nextId, deviceId);
1303 }
1304 } else {
1305 log.warn("next-id {} not found in Flow objective store for dev:{}",
1306 nextId, deviceId);
1307 }
1308 return null;
1309 }
1310
Charles Chan188ebf52015-12-23 00:15:11 -08001311 protected static void pass(Objective obj) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001312 obj.context().ifPresent(context -> context.onSuccess(obj));
Saurav Das822c4e22015-10-23 10:51:11 -07001313 }
1314
Charles Chan188ebf52015-12-23 00:15:11 -08001315 protected static void fail(Objective obj, ObjectiveError error) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001316 obj.context().ifPresent(context -> context.onError(obj, error));
Saurav Das822c4e22015-10-23 10:51:11 -07001317 }
Saurav Das24431192016-03-07 19:13:00 -08001318
Saurav Das24431192016-03-07 19:13:00 -08001319 @Override
1320 public List<String> getNextMappings(NextGroup nextGroup) {
1321 List<String> mappings = new ArrayList<>();
1322 List<Deque<GroupKey>> gkeys = appKryo.deserialize(nextGroup.data());
1323 for (Deque<GroupKey> gkd : gkeys) {
1324 Group lastGroup = null;
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -07001325 StringBuilder gchain = new StringBuilder();
Saurav Das24431192016-03-07 19:13:00 -08001326 for (GroupKey gk : gkd) {
1327 Group g = groupService.getGroup(deviceId, gk);
Saurav Das8be4e3a2016-03-11 17:19:07 -08001328 if (g == null) {
Saurav Das25190812016-05-27 13:54:07 -07001329 gchain.append(" NoGrp").append(" -->");
Saurav Das8be4e3a2016-03-11 17:19:07 -08001330 continue;
1331 }
1332 gchain.append(" 0x").append(Integer.toHexString(g.id().id()))
1333 .append(" -->");
Saurav Das24431192016-03-07 19:13:00 -08001334 lastGroup = g;
1335 }
1336 // add port information for last group in group-chain
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -07001337 List<Instruction> lastGroupIns = new ArrayList<>();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001338 if (lastGroup != null && !lastGroup.buckets().buckets().isEmpty()) {
Saurav Das25190812016-05-27 13:54:07 -07001339 lastGroupIns = lastGroup.buckets().buckets().get(0)
1340 .treatment().allInstructions();
1341 }
1342 for (Instruction i: lastGroupIns) {
Saurav Das24431192016-03-07 19:13:00 -08001343 if (i instanceof OutputInstruction) {
Saurav Das8be4e3a2016-03-11 17:19:07 -08001344 gchain.append(" port:").append(((OutputInstruction) i).port());
Saurav Das24431192016-03-07 19:13:00 -08001345 }
1346 }
Saurav Das8be4e3a2016-03-11 17:19:07 -08001347 mappings.add(gchain.toString());
Saurav Das24431192016-03-07 19:13:00 -08001348 }
1349 return mappings;
1350 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001351
Pier Ventre140a8942016-11-02 07:26:38 -07001352 static boolean isMplsBos(TrafficSelector selector) {
1353 MplsBosCriterion bosCriterion = (MplsBosCriterion) selector.getCriterion(MPLS_BOS);
1354 return bosCriterion != null && bosCriterion.mplsBos();
1355 }
1356
1357 static boolean isNotMplsBos(TrafficSelector selector) {
1358 MplsBosCriterion bosCriterion = (MplsBosCriterion) selector.getCriterion(MPLS_BOS);
1359 return bosCriterion != null && !bosCriterion.mplsBos();
1360 }
1361
Charles Chan5b9df8d2016-03-28 22:21:40 -07001362 protected static VlanId readVlanFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001363 if (selector == null) {
1364 return null;
1365 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001366 Criterion criterion = selector.getCriterion(Criterion.Type.VLAN_VID);
1367 return (criterion == null)
1368 ? null : ((VlanIdCriterion) criterion).vlanId();
1369 }
1370
1371 protected static IpPrefix readIpDstFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001372 if (selector == null) {
1373 return null;
1374 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001375 Criterion criterion = selector.getCriterion(Criterion.Type.IPV4_DST);
1376 return (criterion == null) ? null : ((IPCriterion) criterion).ip();
1377 }
Charles Chand55e84d2016-03-30 17:54:24 -07001378
1379 private static VlanId readVlanFromTreatment(TrafficTreatment treatment) {
Saurav Das59232cf2016-04-27 18:35:50 -07001380 if (treatment == null) {
1381 return null;
1382 }
Charles Chand55e84d2016-03-30 17:54:24 -07001383 for (Instruction i : treatment.allInstructions()) {
1384 if (i instanceof ModVlanIdInstruction) {
1385 return ((ModVlanIdInstruction) i).vlanId();
1386 }
1387 }
1388 return null;
1389 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001390
1391 /**
1392 * Utility class that retries sending flows a fixed number of times, even if
1393 * some of the attempts are successful. Used only for forwarding objectives.
1394 */
1395 protected final class RetryFlows implements Runnable {
1396 int attempts = MAX_RETRY_ATTEMPTS;
1397 private Collection<FlowRule> retryFlows;
1398 private ForwardingObjective fwd;
1399
1400 RetryFlows(ForwardingObjective fwd, Collection<FlowRule> retryFlows) {
1401 this.fwd = fwd;
1402 this.retryFlows = retryFlows;
1403 }
1404
1405 @Override
1406 public void run() {
1407 log.info("RETRY FLOWS ATTEMPT# {} for fwd:{} rules:{}",
1408 MAX_RETRY_ATTEMPTS - attempts, fwd.id(), retryFlows.size());
1409 sendForward(fwd, retryFlows);
1410 if (--attempts > 0) {
1411 executorService.schedule(this, RETRY_MS, TimeUnit.MILLISECONDS);
1412 }
1413 }
1414 }
1415
Saurav Das822c4e22015-10-23 10:51:11 -07001416}