blob: b9377625b61e4c71d9e928c0daac4d1ec2d159a0 [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;
87import java.util.Set;
88import java.util.concurrent.ConcurrentHashMap;
89import java.util.concurrent.ScheduledExecutorService;
90import java.util.concurrent.TimeUnit;
91
92import static java.util.concurrent.Executors.newScheduledThreadPool;
Yi Tseng3a77b4f2017-02-06 15:02:17 -080093import static org.onlab.packet.MacAddress.BROADCAST;
94import static org.onlab.packet.MacAddress.NONE;
Pier Ventree0ae7a32016-11-23 09:57:42 -080095import static org.onlab.util.Tools.groupedThreads;
96import static org.slf4j.LoggerFactory.getLogger;
Pier Ventre140a8942016-11-02 07:26:38 -070097import static org.onosproject.net.flow.criteria.Criterion.Type.MPLS_BOS;
98import static org.onosproject.net.flowobjective.NextObjective.Type.HASHED;
Pier Ventree0ae7a32016-11-23 09:57:42 -080099
Saurav Das822c4e22015-10-23 10:51:11 -0700100/**
101 * Driver for Broadcom's OF-DPA v2.0 TTP.
102 *
103 */
Charles Chan361154b2016-03-24 10:23:39 -0700104public class Ofdpa2Pipeline extends AbstractHandlerBehaviour implements Pipeliner {
Pier Ventre42287df2016-11-09 14:17:26 -0800105
Saurav Das822c4e22015-10-23 10:51:11 -0700106 protected static final int PORT_TABLE = 0;
107 protected static final int VLAN_TABLE = 10;
Pier Ventre42287df2016-11-09 14:17:26 -0800108 protected static final int VLAN_1_TABLE = 11;
109 protected static final int MPLS_L2_PORT_FLOW_TABLE = 13;
110 protected static final int MPLS_L2_PORT_PCP_TRUST_FLOW_TABLE = 16;
Saurav Das822c4e22015-10-23 10:51:11 -0700111 protected static final int TMAC_TABLE = 20;
112 protected static final int UNICAST_ROUTING_TABLE = 30;
113 protected static final int MULTICAST_ROUTING_TABLE = 40;
114 protected static final int MPLS_TABLE_0 = 23;
115 protected static final int MPLS_TABLE_1 = 24;
Pier Ventre140a8942016-11-02 07:26:38 -0700116 protected static final int MPLS_L3_TYPE_TABLE = 27;
117 protected static final int MPLS_TYPE_TABLE = 29;
Saurav Das822c4e22015-10-23 10:51:11 -0700118 protected static final int BRIDGING_TABLE = 50;
119 protected static final int ACL_TABLE = 60;
120 protected static final int MAC_LEARNING_TABLE = 254;
121 protected static final long OFPP_MAX = 0xffffff00L;
122
Saurav Das52025962016-01-28 22:30:01 -0800123 protected static final int HIGHEST_PRIORITY = 0xffff;
Saurav Das2857f382015-11-03 14:39:27 -0800124 protected static final int DEFAULT_PRIORITY = 0x8000;
Saurav Das822c4e22015-10-23 10:51:11 -0700125 protected static final int LOWEST_PRIORITY = 0x0;
126
Pier Ventre42287df2016-11-09 14:17:26 -0800127 protected static final int MPLS_L2_PORT_PRIORITY = 2;
128
129 protected static final int MPLS_TUNNEL_ID_BASE = 0x10000;
130 protected static final int MPLS_TUNNEL_ID_MAX = 0x1FFFF;
131
Pier Ventre70d53ba2016-11-17 22:26:29 -0800132 protected static final int MPLS_UNI_PORT_MAX = 0x0000FFFF;
133
134 protected static final int MPLS_NNI_PORT_BASE = 0x00020000;
135 protected static final int MPLS_NNI_PORT_MAX = 0x0002FFFF;
136
Saurav Das822c4e22015-10-23 10:51:11 -0700137 private final Logger log = getLogger(getClass());
Charles Chan425854b2016-04-11 15:32:12 -0700138 protected ServiceDirectory serviceDirectory;
Saurav Das822c4e22015-10-23 10:51:11 -0700139 protected FlowRuleService flowRuleService;
Charles Chan425854b2016-04-11 15:32:12 -0700140 protected CoreService coreService;
Saurav Das8a0732e2015-11-20 15:27:53 -0800141 protected GroupService groupService;
142 protected FlowObjectiveStore flowObjectiveStore;
Saurav Das822c4e22015-10-23 10:51:11 -0700143 protected DeviceId deviceId;
144 protected ApplicationId driverId;
Saurav Das822c4e22015-10-23 10:51:11 -0700145 protected DeviceService deviceService;
Charles Chan188ebf52015-12-23 00:15:11 -0800146 protected static KryoNamespace appKryo = new KryoNamespace.Builder()
Saurav Das822c4e22015-10-23 10:51:11 -0700147 .register(KryoNamespaces.API)
148 .register(GroupKey.class)
149 .register(DefaultGroupKey.class)
Charles Chan361154b2016-03-24 10:23:39 -0700150 .register(Ofdpa2GroupHandler.OfdpaNextGroup.class)
Saurav Das8a0732e2015-11-20 15:27:53 -0800151 .register(ArrayDeque.class)
Charles Chaneefdedf2016-05-23 16:45:45 -0700152 .build("Ofdpa2Pipeline");
Saurav Das822c4e22015-10-23 10:51:11 -0700153
Charles Chan425854b2016-04-11 15:32:12 -0700154 protected Ofdpa2GroupHandler groupHandler;
Saurav Das822c4e22015-10-23 10:51:11 -0700155
Saurav Das52025962016-01-28 22:30:01 -0800156 protected Set<IPCriterion> sentIpFilters = Collections.newSetFromMap(
Charles Chan188ebf52015-12-23 00:15:11 -0800157 new ConcurrentHashMap<>());
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700158 // flows installations to be retried
159 protected ScheduledExecutorService executorService
160 = newScheduledThreadPool(5, groupedThreads("OfdpaPipeliner", "retry-%d", log));
161 protected static final int MAX_RETRY_ATTEMPTS = 10;
162 protected static final int RETRY_MS = 1000;
Saurav Das4f980082015-11-05 13:39:15 -0800163
Saurav Das822c4e22015-10-23 10:51:11 -0700164 @Override
165 public void init(DeviceId deviceId, PipelinerContext context) {
Saurav Das822c4e22015-10-23 10:51:11 -0700166 this.deviceId = deviceId;
167
Charles Chan425854b2016-04-11 15:32:12 -0700168 serviceDirectory = context.directory();
Saurav Das822c4e22015-10-23 10:51:11 -0700169 coreService = serviceDirectory.get(CoreService.class);
170 flowRuleService = serviceDirectory.get(FlowRuleService.class);
171 groupService = serviceDirectory.get(GroupService.class);
172 flowObjectiveStore = context.store();
Saurav Das822c4e22015-10-23 10:51:11 -0700173 deviceService = serviceDirectory.get(DeviceService.class);
Saurav Das822c4e22015-10-23 10:51:11 -0700174
Charles Chan40132b32017-01-22 00:19:37 -0800175 initDriverId();
176 initGroupHander(context);
Saurav Das822c4e22015-10-23 10:51:11 -0700177
Saurav Das822c4e22015-10-23 10:51:11 -0700178 initializePipeline();
Saurav Das822c4e22015-10-23 10:51:11 -0700179 }
180
Charles Chan40132b32017-01-22 00:19:37 -0800181 protected void initDriverId() {
182 driverId = coreService.registerApplication(
183 "org.onosproject.driver.Ofdpa2Pipeline");
184 }
185
186 protected void initGroupHander(PipelinerContext context) {
187 groupHandler = new Ofdpa2GroupHandler();
188 groupHandler.init(deviceId, context);
189 }
190
Saurav Das822c4e22015-10-23 10:51:11 -0700191 protected void initializePipeline() {
Charles Chan188ebf52015-12-23 00:15:11 -0800192 // OF-DPA does not require initializing the pipeline as it puts default
193 // rules automatically in the hardware. However emulation of OFDPA in
194 // software switches does require table-miss-entries.
Saurav Das822c4e22015-10-23 10:51:11 -0700195 }
196
Charles Chand1172632017-03-15 17:33:09 -0700197 /**
198 * Determines whether this pipeline requires OFDPA match and set VLAN extensions.
199 *
200 * @return true to use the extensions
201 */
202 protected boolean requireVlanExtensions() {
203 return true;
204 }
205
Saurav Das822c4e22015-10-23 10:51:11 -0700206 //////////////////////////////////////
207 // Flow Objectives
208 //////////////////////////////////////
209
210 @Override
211 public void filter(FilteringObjective filteringObjective) {
212 if (filteringObjective.type() == FilteringObjective.Type.PERMIT) {
213 processFilter(filteringObjective,
214 filteringObjective.op() == Objective.Operation.ADD,
215 filteringObjective.appId());
216 } else {
217 // Note that packets that don't match the PERMIT filter are
218 // automatically denied. The DENY filter is used to deny packets
219 // that are otherwise permitted by the PERMIT filter.
220 // Use ACL table flow rules here for DENY filtering objectives
221 log.debug("filter objective other than PERMIT currently not supported");
222 fail(filteringObjective, ObjectiveError.UNSUPPORTED);
223 }
224 }
225
226 @Override
227 public void forward(ForwardingObjective fwd) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700228 Collection<FlowRule> rules = processForward(fwd);
Saurav Das25190812016-05-27 13:54:07 -0700229 if (rules == null || rules.isEmpty()) {
230 // Assumes fail message has already been generated to the objective
231 // context. Returning here prevents spurious pass message to be
232 // generated by FlowRule service for empty flowOps.
233 return;
234 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700235 sendForward(fwd, rules);
236 }
237
238 protected void sendForward(ForwardingObjective fwd, Collection<FlowRule> rules) {
239 FlowRuleOperations.Builder flowOpsBuilder = FlowRuleOperations.builder();
Saurav Das822c4e22015-10-23 10:51:11 -0700240 switch (fwd.op()) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700241 case ADD:
242 rules.stream()
243 .filter(Objects::nonNull)
244 .forEach(flowOpsBuilder::add);
Saurav Dasd2fded02016-12-02 15:43:47 -0800245 log.debug("Applying a add fwd-obj {} to sw:{}", fwd.id(), deviceId);
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700246 break;
247 case REMOVE:
248 rules.stream()
249 .filter(Objects::nonNull)
250 .forEach(flowOpsBuilder::remove);
Pier Ventre42287df2016-11-09 14:17:26 -0800251 log.debug("Deleting a flow rule to sw:{}", deviceId);
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700252 break;
253 default:
254 fail(fwd, ObjectiveError.UNKNOWN);
255 log.warn("Unknown forwarding type {}", fwd.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700256 }
257
Saurav Das822c4e22015-10-23 10:51:11 -0700258 flowRuleService.apply(flowOpsBuilder.build(new FlowRuleOperationsContext() {
259 @Override
260 public void onSuccess(FlowRuleOperations ops) {
261 pass(fwd);
262 }
263
264 @Override
265 public void onError(FlowRuleOperations ops) {
266 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
267 }
268 }));
Saurav Das822c4e22015-10-23 10:51:11 -0700269 }
270
271 @Override
272 public void next(NextObjective nextObjective) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800273 NextGroup nextGroup = flowObjectiveStore.getNextGroup(nextObjective.id());
274 switch (nextObjective.op()) {
275 case ADD:
Saurav Das4f980082015-11-05 13:39:15 -0800276 if (nextGroup != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800277 log.warn("Cannot add next {} that already exists in device {}",
278 nextObjective.id(), deviceId);
279 return;
280 }
281 log.debug("Processing NextObjective id{} in dev{} - add group",
282 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700283 groupHandler.addGroup(nextObjective);
Saurav Das8a0732e2015-11-20 15:27:53 -0800284 break;
285 case ADD_TO_EXISTING:
286 if (nextGroup != null) {
287 log.debug("Processing NextObjective id{} in dev{} - add bucket",
288 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700289 groupHandler.addBucketToGroup(nextObjective, nextGroup);
Saurav Das4f980082015-11-05 13:39:15 -0800290 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800291 // it is possible that group-chain has not been fully created yet
Saurav Das423fe2b2015-12-04 10:52:59 -0800292 log.debug("Waiting to add bucket to group for next-id:{} in dev:{}",
293 nextObjective.id(), deviceId);
Yi Tseng47f82dc2017-03-05 22:48:39 -0800294
295 // by design multiple pending bucket is allowed for the group
296 groupHandler.pendingBuckets.compute(nextObjective.id(), (nextId, pendBkts) -> {
297 if (pendBkts == null) {
298 pendBkts = Sets.newHashSet();
299 }
300 pendBkts.add(nextObjective);
301 return pendBkts;
302 });
Saurav Das4f980082015-11-05 13:39:15 -0800303 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800304 break;
305 case REMOVE:
306 if (nextGroup == null) {
307 log.warn("Cannot remove next {} that does not exist in device {}",
308 nextObjective.id(), deviceId);
309 return;
310 }
311 log.debug("Processing NextObjective id{} in dev{} - remove group",
312 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700313 groupHandler.removeGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800314 break;
315 case REMOVE_FROM_EXISTING:
316 if (nextGroup == null) {
317 log.warn("Cannot remove from next {} that does not exist in device {}",
318 nextObjective.id(), deviceId);
319 return;
320 }
321 log.debug("Processing NextObjective id{} in dev{} - remove bucket",
322 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700323 groupHandler.removeBucketFromGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800324 break;
325 default:
Saurav Das4f980082015-11-05 13:39:15 -0800326 log.warn("Unsupported operation {}", nextObjective.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700327 }
328 }
329
330 //////////////////////////////////////
331 // Flow handling
332 //////////////////////////////////////
333
334 /**
335 * As per OFDPA 2.0 TTP, filtering of VLAN ids, MAC addresses (for routing)
336 * and IP addresses configured on switch ports happen in different tables.
337 * Note that IP filtering rules need to be added to the ACL table, as there
338 * is no mechanism to send to controller via IP table.
339 *
340 * @param filt the filtering objective
341 * @param install indicates whether to add or remove the objective
342 * @param applicationId the application that sent this objective
343 */
Saurav Das52025962016-01-28 22:30:01 -0800344 protected void processFilter(FilteringObjective filt,
345 boolean install, ApplicationId applicationId) {
Saurav Das822c4e22015-10-23 10:51:11 -0700346 // This driver only processes filtering criteria defined with switch
347 // ports as the key
348 PortCriterion portCriterion = null;
349 EthCriterion ethCriterion = null;
350 VlanIdCriterion vidCriterion = null;
351 Collection<IPCriterion> ips = new ArrayList<IPCriterion>();
352 if (!filt.key().equals(Criteria.dummy()) &&
353 filt.key().type() == Criterion.Type.IN_PORT) {
354 portCriterion = (PortCriterion) filt.key();
355 } else {
356 log.warn("No key defined in filtering objective from app: {}. Not"
357 + "processing filtering objective", applicationId);
Saurav Das59232cf2016-04-27 18:35:50 -0700358 fail(filt, ObjectiveError.BADPARAMS);
Saurav Das822c4e22015-10-23 10:51:11 -0700359 return;
360 }
Saurav Das59232cf2016-04-27 18:35:50 -0700361 log.debug("Received filtering objective for dev/port: {}/{}", deviceId,
362 portCriterion.port());
Saurav Das822c4e22015-10-23 10:51:11 -0700363 // convert filtering conditions for switch-intfs into flowrules
364 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
365 for (Criterion criterion : filt.conditions()) {
Charles Chan5b9df8d2016-03-28 22:21:40 -0700366 if (criterion.type() == Criterion.Type.ETH_DST ||
367 criterion.type() == Criterion.Type.ETH_DST_MASKED) {
Saurav Das822c4e22015-10-23 10:51:11 -0700368 ethCriterion = (EthCriterion) criterion;
369 } else if (criterion.type() == Criterion.Type.VLAN_VID) {
370 vidCriterion = (VlanIdCriterion) criterion;
371 } else if (criterion.type() == Criterion.Type.IPV4_DST) {
372 ips.add((IPCriterion) criterion);
373 } else {
374 log.error("Unsupported filter {}", criterion);
375 fail(filt, ObjectiveError.UNSUPPORTED);
376 return;
377 }
378 }
379
Saurav Das0e99e2b2015-10-28 12:39:42 -0700380 VlanId assignedVlan = null;
Charles Chane849c192016-01-11 18:28:54 -0800381 if (vidCriterion != null) {
Charles Chand55e84d2016-03-30 17:54:24 -0700382 // Use the VLAN in metadata whenever a metadata is provided
383 if (filt.meta() != null) {
384 assignedVlan = readVlanFromTreatment(filt.meta());
385 // Use the VLAN in criterion if metadata is not present and the traffic is tagged
386 } else if (!vidCriterion.vlanId().equals(VlanId.NONE)) {
Charles Chane849c192016-01-11 18:28:54 -0800387 assignedVlan = vidCriterion.vlanId();
Charles Chand55e84d2016-03-30 17:54:24 -0700388 }
Charles Chane849c192016-01-11 18:28:54 -0800389
Charles Chand55e84d2016-03-30 17:54:24 -0700390 if (assignedVlan == null) {
391 log.error("Driver fails to extract VLAN information. "
392 + "Not proccessing VLAN filters on device {}.", deviceId);
393 log.debug("VLAN ID in criterion={}, metadata={}",
394 readVlanFromTreatment(filt.meta()), vidCriterion.vlanId());
395 fail(filt, ObjectiveError.BADPARAMS);
396 return;
Saurav Das0e99e2b2015-10-28 12:39:42 -0700397 }
398 }
399
Yi Tseng3a77b4f2017-02-06 15:02:17 -0800400 if (ethCriterion == null || ethCriterion.mac().equals(NONE)) {
Charles Chan985b12e2016-05-11 19:47:22 -0700401 // NOTE: it is possible that a filtering objective only has vidCriterion
402 log.debug("filtering objective missing dstMac, cannot program TMAC table");
Saurav Das822c4e22015-10-23 10:51:11 -0700403 } else {
404 for (FlowRule tmacRule : processEthDstFilter(portCriterion, ethCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700405 vidCriterion, assignedVlan,
406 applicationId)) {
Saurav Das018605f2017-02-18 14:05:44 -0800407 log.debug("{} MAC filtering rules in TMAC table: {} for dev: {}",
408 (install) ? "adding" : "removing", tmacRule, deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700409 ops = install ? ops.add(tmacRule) : ops.remove(tmacRule);
410 }
411 }
412
Charles Chan985b12e2016-05-11 19:47:22 -0700413 if (vidCriterion == null) {
414 // NOTE: it is possible that a filtering objective only has ethCriterion
Charles Chan053b1cb2017-03-22 16:56:35 -0700415 log.debug("filtering objective missing VLAN, cannot program VLAN Table");
Saurav Das822c4e22015-10-23 10:51:11 -0700416 } else {
Charles Chan14967c22015-12-07 11:11:50 -0800417 /*
418 * NOTE: Separate vlan filtering rules and assignment rules
419 * into different stage in order to guarantee that filtering rules
Saurav Das52025962016-01-28 22:30:01 -0800420 * always go first, as required by ofdpa.
Charles Chan14967c22015-12-07 11:11:50 -0800421 */
422 List<FlowRule> allRules = processVlanIdFilter(
423 portCriterion, vidCriterion, assignedVlan, applicationId);
424 List<FlowRule> filteringRules = new ArrayList<>();
425 List<FlowRule> assignmentRules = new ArrayList<>();
426
427 allRules.forEach(flowRule -> {
Charles Chand1172632017-03-15 17:33:09 -0700428 VlanId vlanId;
429 if (requireVlanExtensions()) {
430 ExtensionCriterion extCriterion =
431 (ExtensionCriterion) flowRule.selector().getCriterion(Criterion.Type.EXTENSION);
432 vlanId = ((OfdpaMatchVlanVid) extCriterion.extensionSelector()).vlanId();
433 } else {
434 VlanIdCriterion vlanIdCriterion =
435 (VlanIdCriterion) flowRule.selector().getCriterion(Criterion.Type.VLAN_VID);
436 vlanId = vlanIdCriterion.vlanId();
437 }
Charles Chanbe8aea42016-02-24 12:04:47 -0800438 if (!vlanId.equals(VlanId.NONE)) {
Charles Chan14967c22015-12-07 11:11:50 -0800439 filteringRules.add(flowRule);
440 } else {
441 assignmentRules.add(flowRule);
442 }
443 });
444
445 for (FlowRule filteringRule : filteringRules) {
Saurav Das018605f2017-02-18 14:05:44 -0800446 log.debug("{} VLAN filtering rule in VLAN table: {} for dev: {}",
447 (install) ? "adding" : "removing", filteringRule, deviceId);
Charles Chan14967c22015-12-07 11:11:50 -0800448 ops = install ? ops.add(filteringRule) : ops.remove(filteringRule);
449 }
450
451 ops.newStage();
452
453 for (FlowRule assignmentRule : assignmentRules) {
Saurav Das018605f2017-02-18 14:05:44 -0800454 log.debug("{} VLAN assignment rule in VLAN table: {} for dev: {}",
455 (install) ? "adding" : "removing", assignmentRule, deviceId);
Charles Chan14967c22015-12-07 11:11:50 -0800456 ops = install ? ops.add(assignmentRule) : ops.remove(assignmentRule);
Saurav Das822c4e22015-10-23 10:51:11 -0700457 }
458 }
459
Saurav Das822c4e22015-10-23 10:51:11 -0700460 // apply filtering flow rules
461 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
462 @Override
463 public void onSuccess(FlowRuleOperations ops) {
Saurav Das018605f2017-02-18 14:05:44 -0800464 log.debug("Applied {} filtering rules in device {}",
Saurav Das822c4e22015-10-23 10:51:11 -0700465 ops.stages().get(0).size(), deviceId);
466 pass(filt);
467 }
468
469 @Override
470 public void onError(FlowRuleOperations ops) {
471 log.info("Failed to apply all filtering rules in dev {}", deviceId);
472 fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED);
473 }
474 }));
475
476 }
477
478 /**
Charles Chand1172632017-03-15 17:33:09 -0700479 * Internal implementation of processVlanIdFilter.
480 * <p>
481 * The is_present bit in set_vlan_vid action is required to be 0 in OFDPA i12.
482 * Since it is non-OF spec, we need an extension treatment for that.
483 * The useVlanExtension must be set to false for OFDPA i12.
484 * </p>
Saurav Das0e99e2b2015-10-28 12:39:42 -0700485 *
Charles Chanf9e98652016-09-07 16:54:23 -0700486 * @param portCriterion port on device for which this filter is programmed
487 * @param vidCriterion vlan assigned to port, or NONE for untagged
488 * @param assignedVlan assigned vlan-id for untagged packets
489 * @param applicationId for application programming this filter
Saurav Das822c4e22015-10-23 10:51:11 -0700490 * @return list of FlowRule for port-vlan filters
491 */
492 protected List<FlowRule> processVlanIdFilter(PortCriterion portCriterion,
Charles Chanf9e98652016-09-07 16:54:23 -0700493 VlanIdCriterion vidCriterion,
494 VlanId assignedVlan,
Charles Chand1172632017-03-15 17:33:09 -0700495 ApplicationId applicationId) {
Charles Chan14967c22015-12-07 11:11:50 -0800496 List<FlowRule> rules = new ArrayList<>();
Saurav Das822c4e22015-10-23 10:51:11 -0700497 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
498 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800499 TrafficSelector.Builder preSelector = null;
500 TrafficTreatment.Builder preTreatment = null;
501
Saurav Das4f980082015-11-05 13:39:15 -0800502 treatment.transition(TMAC_TABLE);
503
Saurav Das822c4e22015-10-23 10:51:11 -0700504 if (vidCriterion.vlanId() == VlanId.NONE) {
505 // untagged packets are assigned vlans
Charles Chand1172632017-03-15 17:33:09 -0700506 preSelector = DefaultTrafficSelector.builder();
507 if (requireVlanExtensions()) {
508 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(VlanId.NONE);
509 selector.extension(ofdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700510 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
511 treatment.extension(ofdpaSetVlanVid, deviceId);
Charles Chand1172632017-03-15 17:33:09 -0700512
513 OfdpaMatchVlanVid preOfdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
514 preSelector.extension(preOfdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700515 } else {
Charles Chand1172632017-03-15 17:33:09 -0700516 selector.matchVlanId(VlanId.NONE);
Charles Chanf9e98652016-09-07 16:54:23 -0700517 treatment.setVlanId(assignedVlan);
Charles Chand1172632017-03-15 17:33:09 -0700518
519 preSelector.matchVlanId(assignedVlan);
520 }
521 preTreatment = DefaultTrafficTreatment.builder().transition(TMAC_TABLE);
522 } else {
523 if (requireVlanExtensions()) {
524 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
525 selector.extension(ofdpaMatchVlanVid, deviceId);
526 } else {
527 selector.matchVlanId(vidCriterion.vlanId());
Charles Chanf9e98652016-09-07 16:54:23 -0700528 }
Charles Chan14967c22015-12-07 11:11:50 -0800529
Charles Chand55e84d2016-03-30 17:54:24 -0700530 if (!assignedVlan.equals(vidCriterion.vlanId())) {
Charles Chand1172632017-03-15 17:33:09 -0700531 if (requireVlanExtensions()) {
Charles Chanc03782d2017-01-31 13:57:55 -0800532 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
533 treatment.extension(ofdpaSetVlanVid, deviceId);
534 } else {
535 treatment.setVlanId(assignedVlan);
536 }
Charles Chand55e84d2016-03-30 17:54:24 -0700537 }
Saurav Das822c4e22015-10-23 10:51:11 -0700538 }
Saurav Das822c4e22015-10-23 10:51:11 -0700539
540 // ofdpa cannot match on ALL portnumber, so we need to use separate
541 // rules for each port.
Charles Chan14967c22015-12-07 11:11:50 -0800542 List<PortNumber> portnums = new ArrayList<>();
Saurav Das822c4e22015-10-23 10:51:11 -0700543 if (portCriterion.port() == PortNumber.ALL) {
544 for (Port port : deviceService.getPorts(deviceId)) {
545 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
546 portnums.add(port.number());
547 }
548 }
549 } else {
550 portnums.add(portCriterion.port());
551 }
Saurav Das4f980082015-11-05 13:39:15 -0800552
Saurav Das822c4e22015-10-23 10:51:11 -0700553 for (PortNumber pnum : portnums) {
Saurav Das4f980082015-11-05 13:39:15 -0800554 // create rest of flowrule
Saurav Das822c4e22015-10-23 10:51:11 -0700555 selector.matchInPort(pnum);
556 FlowRule rule = DefaultFlowRule.builder()
557 .forDevice(deviceId)
558 .withSelector(selector.build())
559 .withTreatment(treatment.build())
560 .withPriority(DEFAULT_PRIORITY)
561 .fromApp(applicationId)
562 .makePermanent()
563 .forTable(VLAN_TABLE).build();
Charles Chan14967c22015-12-07 11:11:50 -0800564
565 if (preSelector != null) {
566 preSelector.matchInPort(pnum);
567 FlowRule preRule = DefaultFlowRule.builder()
568 .forDevice(deviceId)
569 .withSelector(preSelector.build())
570 .withTreatment(preTreatment.build())
571 .withPriority(DEFAULT_PRIORITY)
572 .fromApp(applicationId)
573 .makePermanent()
574 .forTable(VLAN_TABLE).build();
575 rules.add(preRule);
576 }
577
Saurav Das822c4e22015-10-23 10:51:11 -0700578 rules.add(rule);
579 }
580 return rules;
581 }
582
583 /**
584 * Allows routed packets with correct destination MAC to be directed
585 * to unicast-IP routing table or MPLS forwarding table.
Saurav Das822c4e22015-10-23 10:51:11 -0700586 *
587 * @param portCriterion port on device for which this filter is programmed
588 * @param ethCriterion dstMac of device for which is filter is programmed
589 * @param vidCriterion vlan assigned to port, or NONE for untagged
Saurav Das0e99e2b2015-10-28 12:39:42 -0700590 * @param assignedVlan assigned vlan-id for untagged packets
Saurav Das822c4e22015-10-23 10:51:11 -0700591 * @param applicationId for application programming this filter
592 * @return list of FlowRule for port-vlan filters
593
594 */
595 protected List<FlowRule> processEthDstFilter(PortCriterion portCriterion,
596 EthCriterion ethCriterion,
597 VlanIdCriterion vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700598 VlanId assignedVlan,
Saurav Das822c4e22015-10-23 10:51:11 -0700599 ApplicationId applicationId) {
Charles Chan5270ed02016-01-30 23:22:37 -0800600 // Consider PortNumber.ANY as wildcard. Match ETH_DST only
601 if (portCriterion != null && portCriterion.port() == PortNumber.ANY) {
602 return processEthDstOnlyFilter(ethCriterion, applicationId);
603 }
604
Charles Chan5b9df8d2016-03-28 22:21:40 -0700605 // Multicast MAC
606 if (ethCriterion.mask() != null) {
607 return processMcastEthDstFilter(ethCriterion, applicationId);
608 }
609
Saurav Das822c4e22015-10-23 10:51:11 -0700610 //handling untagged packets via assigned VLAN
611 if (vidCriterion.vlanId() == VlanId.NONE) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700612 vidCriterion = (VlanIdCriterion) Criteria.matchVlanId(assignedVlan);
Saurav Das822c4e22015-10-23 10:51:11 -0700613 }
614 // ofdpa cannot match on ALL portnumber, so we need to use separate
615 // rules for each port.
616 List<PortNumber> portnums = new ArrayList<PortNumber>();
617 if (portCriterion.port() == PortNumber.ALL) {
618 for (Port port : deviceService.getPorts(deviceId)) {
619 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
620 portnums.add(port.number());
621 }
622 }
623 } else {
624 portnums.add(portCriterion.port());
625 }
626
627 List<FlowRule> rules = new ArrayList<FlowRule>();
628 for (PortNumber pnum : portnums) {
Charles Chan14967c22015-12-07 11:11:50 -0800629 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
Saurav Das822c4e22015-10-23 10:51:11 -0700630 // for unicast IP packets
631 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
632 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
633 selector.matchInPort(pnum);
Charles Chand1172632017-03-15 17:33:09 -0700634 if (requireVlanExtensions()) {
635 selector.extension(ofdpaMatchVlanVid, deviceId);
636 } else {
637 selector.matchVlanId(vidCriterion.vlanId());
638 }
Saurav Das822c4e22015-10-23 10:51:11 -0700639 selector.matchEthType(Ethernet.TYPE_IPV4);
640 selector.matchEthDst(ethCriterion.mac());
641 treatment.transition(UNICAST_ROUTING_TABLE);
642 FlowRule rule = DefaultFlowRule.builder()
643 .forDevice(deviceId)
644 .withSelector(selector.build())
645 .withTreatment(treatment.build())
646 .withPriority(DEFAULT_PRIORITY)
647 .fromApp(applicationId)
648 .makePermanent()
649 .forTable(TMAC_TABLE).build();
650 rules.add(rule);
651 //for MPLS packets
652 selector = DefaultTrafficSelector.builder();
653 treatment = DefaultTrafficTreatment.builder();
654 selector.matchInPort(pnum);
Charles Chand1172632017-03-15 17:33:09 -0700655 if (requireVlanExtensions()) {
656 selector.extension(ofdpaMatchVlanVid, deviceId);
657 } else {
658 selector.matchVlanId(vidCriterion.vlanId());
659 }
Saurav Das822c4e22015-10-23 10:51:11 -0700660 selector.matchEthType(Ethernet.MPLS_UNICAST);
661 selector.matchEthDst(ethCriterion.mac());
662 treatment.transition(MPLS_TABLE_0);
663 rule = DefaultFlowRule.builder()
664 .forDevice(deviceId)
665 .withSelector(selector.build())
666 .withTreatment(treatment.build())
667 .withPriority(DEFAULT_PRIORITY)
668 .fromApp(applicationId)
669 .makePermanent()
670 .forTable(TMAC_TABLE).build();
671 rules.add(rule);
Pier Ventree0ae7a32016-11-23 09:57:42 -0800672 /*
673 * TMAC rules for IPv6 packets
674 */
675 selector = DefaultTrafficSelector.builder();
676 treatment = DefaultTrafficTreatment.builder();
677 selector.matchInPort(pnum);
Charles Chand1172632017-03-15 17:33:09 -0700678 if (requireVlanExtensions()) {
679 selector.extension(ofdpaMatchVlanVid, deviceId);
680 } else {
681 selector.matchVlanId(vidCriterion.vlanId());
682 }
Pier Ventree0ae7a32016-11-23 09:57:42 -0800683 selector.matchEthType(Ethernet.TYPE_IPV6);
684 selector.matchEthDst(ethCriterion.mac());
685 treatment.transition(UNICAST_ROUTING_TABLE);
686 rule = DefaultFlowRule.builder()
687 .forDevice(deviceId)
688 .withSelector(selector.build())
689 .withTreatment(treatment.build())
690 .withPriority(DEFAULT_PRIORITY)
691 .fromApp(applicationId)
692 .makePermanent()
693 .forTable(TMAC_TABLE).build();
694 rules.add(rule);
Saurav Das822c4e22015-10-23 10:51:11 -0700695 }
696 return rules;
697 }
698
Charles Chan5270ed02016-01-30 23:22:37 -0800699 protected List<FlowRule> processEthDstOnlyFilter(EthCriterion ethCriterion,
700 ApplicationId applicationId) {
Pier Luigi0e358632017-01-31 09:35:05 -0800701 ImmutableList.Builder<FlowRule> builder = ImmutableList.builder();
702
Charles Chan5270ed02016-01-30 23:22:37 -0800703 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
704 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
705 selector.matchEthType(Ethernet.TYPE_IPV4);
706 selector.matchEthDst(ethCriterion.mac());
707 treatment.transition(UNICAST_ROUTING_TABLE);
708 FlowRule rule = DefaultFlowRule.builder()
709 .forDevice(deviceId)
710 .withSelector(selector.build())
711 .withTreatment(treatment.build())
712 .withPriority(DEFAULT_PRIORITY)
713 .fromApp(applicationId)
714 .makePermanent()
715 .forTable(TMAC_TABLE).build();
Pier Luigi0e358632017-01-31 09:35:05 -0800716 builder.add(rule);
717
718 selector = DefaultTrafficSelector.builder();
719 treatment = DefaultTrafficTreatment.builder();
720 selector.matchEthType(Ethernet.TYPE_IPV6);
721 selector.matchEthDst(ethCriterion.mac());
722 treatment.transition(UNICAST_ROUTING_TABLE);
723 rule = DefaultFlowRule.builder()
724 .forDevice(deviceId)
725 .withSelector(selector.build())
726 .withTreatment(treatment.build())
727 .withPriority(DEFAULT_PRIORITY)
728 .fromApp(applicationId)
729 .makePermanent()
730 .forTable(TMAC_TABLE).build();
731 return builder.add(rule).build();
Charles Chan5270ed02016-01-30 23:22:37 -0800732 }
733
Charles Chan5b9df8d2016-03-28 22:21:40 -0700734 protected List<FlowRule> processMcastEthDstFilter(EthCriterion ethCriterion,
735 ApplicationId applicationId) {
736 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
737 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
738 selector.matchEthType(Ethernet.TYPE_IPV4);
739 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
740 treatment.transition(MULTICAST_ROUTING_TABLE);
741 FlowRule rule = DefaultFlowRule.builder()
742 .forDevice(deviceId)
743 .withSelector(selector.build())
744 .withTreatment(treatment.build())
745 .withPriority(DEFAULT_PRIORITY)
746 .fromApp(applicationId)
747 .makePermanent()
748 .forTable(TMAC_TABLE).build();
749 return ImmutableList.<FlowRule>builder().add(rule).build();
750 }
751
Saurav Das822c4e22015-10-23 10:51:11 -0700752 private Collection<FlowRule> processForward(ForwardingObjective fwd) {
753 switch (fwd.flag()) {
754 case SPECIFIC:
755 return processSpecific(fwd);
756 case VERSATILE:
757 return processVersatile(fwd);
758 default:
759 fail(fwd, ObjectiveError.UNKNOWN);
760 log.warn("Unknown forwarding flag {}", fwd.flag());
761 }
762 return Collections.emptySet();
763 }
764
765 /**
766 * In the OF-DPA 2.0 pipeline, versatile forwarding objectives go to the
767 * ACL table.
768 * @param fwd the forwarding objective of type 'versatile'
769 * @return a collection of flow rules to be sent to the switch. An empty
770 * collection may be returned if there is a problem in processing
771 * the flow rule
772 */
Saurav Das52025962016-01-28 22:30:01 -0800773 protected Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
Saurav Das018605f2017-02-18 14:05:44 -0800774 log.debug("Processing versatile forwarding objective:{} in dev:{}",
Saurav Dasd2fded02016-12-02 15:43:47 -0800775 fwd.id(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700776
777 EthTypeCriterion ethType =
Saurav Das77b5e902016-01-27 17:01:59 -0800778 (EthTypeCriterion) fwd.selector().getCriterion(Criterion.Type.ETH_TYPE);
Saurav Das822c4e22015-10-23 10:51:11 -0700779 if (ethType == null) {
Saurav Dasd2fded02016-12-02 15:43:47 -0800780 log.error("Versatile forwarding objective:{} must include ethType",
781 fwd.id());
Saurav Das822c4e22015-10-23 10:51:11 -0700782 fail(fwd, ObjectiveError.BADPARAMS);
783 return Collections.emptySet();
784 }
785 if (fwd.nextId() == null && fwd.treatment() == null) {
786 log.error("Forwarding objective {} from {} must contain "
787 + "nextId or Treatment", fwd.selector(), fwd.appId());
Zsolt Haraszti9faab752016-02-17 15:55:20 -0800788 fail(fwd, ObjectiveError.BADPARAMS);
Saurav Das822c4e22015-10-23 10:51:11 -0700789 return Collections.emptySet();
790 }
Saurav Das49cb5a12016-01-16 22:54:07 -0800791
Saurav Das77b5e902016-01-27 17:01:59 -0800792 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
793 fwd.selector().criteria().forEach(criterion -> {
794 if (criterion instanceof VlanIdCriterion) {
795 VlanId vlanId = ((VlanIdCriterion) criterion).vlanId();
796 // ensure that match does not include vlan = NONE as OF-DPA does not
797 // match untagged packets this way in the ACL table.
798 if (vlanId.equals(VlanId.NONE)) {
799 return;
800 }
Charles Chand1172632017-03-15 17:33:09 -0700801 if (requireVlanExtensions()) {
802 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanId);
803 sbuilder.extension(ofdpaMatchVlanVid, deviceId);
804 } else {
805 sbuilder.matchVlanId(vlanId);
806 }
Pier Ventree0ae7a32016-11-23 09:57:42 -0800807 } else if (criterion instanceof Icmpv6TypeCriterion ||
808 criterion instanceof Icmpv6CodeCriterion) {
809 /*
810 * We silenty discard these criterions, our current
811 * OFDPA platform does not support these matches on
812 * the ACL table.
813 */
814 log.warn("ICMPv6 Type and ICMPv6 Code are not supported");
Saurav Das77b5e902016-01-27 17:01:59 -0800815 } else {
816 sbuilder.add(criterion);
817 }
818 });
819
Saurav Das822c4e22015-10-23 10:51:11 -0700820 // XXX driver does not currently do type checking as per Tables 65-67 in
821 // OFDPA 2.0 spec. The only allowed treatment is a punt to the controller.
Saurav Das49cb5a12016-01-16 22:54:07 -0800822 TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder();
823 if (fwd.treatment() != null) {
824 for (Instruction ins : fwd.treatment().allInstructions()) {
825 if (ins instanceof OutputInstruction) {
826 OutputInstruction o = (OutputInstruction) ins;
827 if (o.port() == PortNumber.CONTROLLER) {
828 ttBuilder.add(o);
829 } else {
830 log.warn("Only allowed treatments in versatile forwarding "
831 + "objectives are punts to the controller");
832 }
833 } else {
834 log.warn("Cannot process instruction in versatile fwd {}", ins);
835 }
Saurav Das822c4e22015-10-23 10:51:11 -0700836 }
Charles Chan2df0e8a2017-01-09 11:45:08 -0800837 if (fwd.treatment().clearedDeferred()) {
838 ttBuilder.wipeDeferred();
839 }
Saurav Das822c4e22015-10-23 10:51:11 -0700840 }
Saurav Das822c4e22015-10-23 10:51:11 -0700841 if (fwd.nextId() != null) {
Saurav Das49cb5a12016-01-16 22:54:07 -0800842 // overide case
843 NextGroup next = getGroupForNextObjective(fwd.nextId());
844 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
845 // we only need the top level group's key to point the flow to it
846 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
847 if (group == null) {
848 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
849 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
850 fail(fwd, ObjectiveError.GROUPMISSING);
851 return Collections.emptySet();
852 }
853 ttBuilder.deferred().group(group.id());
Saurav Das822c4e22015-10-23 10:51:11 -0700854 }
Saurav Das49cb5a12016-01-16 22:54:07 -0800855
856 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
857 .fromApp(fwd.appId())
858 .withPriority(fwd.priority())
859 .forDevice(deviceId)
Saurav Das77b5e902016-01-27 17:01:59 -0800860 .withSelector(sbuilder.build())
Saurav Das49cb5a12016-01-16 22:54:07 -0800861 .withTreatment(ttBuilder.build())
862 .makePermanent()
863 .forTable(ACL_TABLE);
864 return Collections.singletonList(ruleBuilder.build());
Saurav Das822c4e22015-10-23 10:51:11 -0700865 }
866
867 /**
868 * In the OF-DPA 2.0 pipeline, specific forwarding refers to the IP table
Saurav Das8a0732e2015-11-20 15:27:53 -0800869 * (unicast or multicast) or the L2 table (mac + vlan) or the MPLS table.
Saurav Das822c4e22015-10-23 10:51:11 -0700870 *
871 * @param fwd the forwarding objective of type 'specific'
872 * @return a collection of flow rules. Typically there will be only one
873 * for this type of forwarding objective. An empty set may be
874 * returned if there is an issue in processing the objective.
875 */
Saurav Das8a0732e2015-11-20 15:27:53 -0800876 protected Collection<FlowRule> processSpecific(ForwardingObjective fwd) {
Saurav Das25190812016-05-27 13:54:07 -0700877 log.debug("Processing specific fwd objective:{} in dev:{} with next:{}",
Saurav Das4ce45962015-11-24 23:21:05 -0800878 fwd.id(), deviceId, fwd.nextId());
879 boolean isEthTypeObj = isSupportedEthTypeObjective(fwd);
880 boolean isEthDstObj = isSupportedEthDstObjective(fwd);
881
882 if (isEthTypeObj) {
883 return processEthTypeSpecific(fwd);
884 } else if (isEthDstObj) {
885 return processEthDstSpecific(fwd);
886 } else {
887 log.warn("processSpecific: Unsupported forwarding objective "
888 + "criteria fwd:{} in dev:{}", fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700889 fail(fwd, ObjectiveError.UNSUPPORTED);
890 return Collections.emptySet();
891 }
Saurav Das4ce45962015-11-24 23:21:05 -0800892 }
893
Saurav Das4ce45962015-11-24 23:21:05 -0800894 /**
895 * Handles forwarding rules to the IP and MPLS tables.
896 *
897 * @param fwd the forwarding objective
898 * @return A collection of flow rules, or an empty set
899 */
900 protected Collection<FlowRule> processEthTypeSpecific(ForwardingObjective fwd) {
Charles Chancad338a2016-09-16 18:03:11 -0700901 return processEthTypeSpecificInternal(fwd, false, ACL_TABLE);
Charles Chanf9e98652016-09-07 16:54:23 -0700902 }
903
904 /**
905 * Internal implementation of processEthTypeSpecific.
906 * <p>
907 * Wildcarded IPv4_DST is not supported in OFDPA i12. Therefore, we break
908 * the rule into 0.0.0.0/1 and 128.0.0.0/1.
909 * The allowDefaultRoute must be set to false for OFDPA i12.
910 * </p>
911 *
912 * @param fwd the forwarding objective
913 * @param allowDefaultRoute allow wildcarded IPv4_DST or not
Ray Milkey0bb1e102016-11-10 14:51:27 -0800914 * @param mplsNextTable next MPLS table
Charles Chanf9e98652016-09-07 16:54:23 -0700915 * @return A collection of flow rules, or an empty set
916 */
917 protected Collection<FlowRule> processEthTypeSpecificInternal(ForwardingObjective fwd,
Charles Chancad338a2016-09-16 18:03:11 -0700918 boolean allowDefaultRoute,
919 int mplsNextTable) {
Saurav Das4ce45962015-11-24 23:21:05 -0800920 TrafficSelector selector = fwd.selector();
921 EthTypeCriterion ethType =
922 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
Flavio Castroe10fa242016-01-15 12:43:51 -0800923 boolean popMpls = false;
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700924 boolean emptyGroup = false;
Charles Chan188ebf52015-12-23 00:15:11 -0800925 int forTableId;
Saurav Das8a0732e2015-11-20 15:27:53 -0800926 TrafficSelector.Builder filteredSelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800927 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
Flavio Castroe10fa242016-01-15 12:43:51 -0800928 TrafficSelector.Builder complementarySelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800929
Saurav Das8a0732e2015-11-20 15:27:53 -0800930 if (ethType.ethType().toShort() == Ethernet.TYPE_IPV4) {
Pier Luigi3bfe32c2017-01-30 09:47:36 -0800931 if (buildIpv4Selector(filteredSelector, complementarySelector, fwd, allowDefaultRoute) < 0) {
932 return Collections.emptyList();
933 }
934 // We need to set properly the next table
Flavio Castroe10fa242016-01-15 12:43:51 -0800935 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700936 if (ipv4Dst.isMulticast()) {
Charles Chan5b9df8d2016-03-28 22:21:40 -0700937 forTableId = MULTICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -0800938 } else {
Charles Chan5b9df8d2016-03-28 22:21:40 -0700939 forTableId = UNICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -0800940 }
Charles Chan14967c22015-12-07 11:11:50 -0800941 if (fwd.treatment() != null) {
942 for (Instruction instr : fwd.treatment().allInstructions()) {
943 if (instr instanceof L3ModificationInstruction &&
944 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
Saurav Das9c705342017-01-06 11:36:01 -0800945 // XXX decrementing IP ttl is done automatically for routing, this
946 // action is ignored or rejected in ofdpa as it is not fully implemented
947 //tb.deferred().add(instr);
Charles Chan14967c22015-12-07 11:11:50 -0800948 }
949 }
950 }
951
Pier Ventree0ae7a32016-11-23 09:57:42 -0800952 } else if (ethType.ethType().toShort() == Ethernet.TYPE_IPV6) {
953 if (buildIpv6Selector(filteredSelector, fwd) < 0) {
954 return Collections.emptyList();
955 }
956 forTableId = UNICAST_ROUTING_TABLE;
957 if (fwd.treatment() != null) {
958 for (Instruction instr : fwd.treatment().allInstructions()) {
959 if (instr instanceof L3ModificationInstruction &&
960 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
961 // XXX decrementing IP ttl is done automatically for routing, this
962 // action is ignored or rejected in ofdpa as it is not fully implemented
963 //tb.deferred().add(instr);
964 }
965 }
966 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800967 } else {
968 filteredSelector
969 .matchEthType(Ethernet.MPLS_UNICAST)
970 .matchMplsLabel(((MplsCriterion)
971 selector.getCriterion(Criterion.Type.MPLS_LABEL)).label());
972 MplsBosCriterion bos = (MplsBosCriterion) selector
Pier Ventre140a8942016-11-02 07:26:38 -0700973 .getCriterion(MPLS_BOS);
Saurav Das8a0732e2015-11-20 15:27:53 -0800974 if (bos != null) {
975 filteredSelector.matchMplsBos(bos.mplsBos());
976 }
977 forTableId = MPLS_TABLE_1;
Saurav Das4ce45962015-11-24 23:21:05 -0800978 log.debug("processing MPLS specific forwarding objective {} -> next:{}"
979 + " in dev {}", fwd.id(), fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700980
Charles Chan14967c22015-12-07 11:11:50 -0800981 if (fwd.treatment() != null) {
982 for (Instruction instr : fwd.treatment().allInstructions()) {
983 if (instr instanceof L2ModificationInstruction &&
984 ((L2ModificationInstruction) instr).subtype() == L2SubType.MPLS_POP) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800985 popMpls = true;
Pier Luigi3bfe32c2017-01-30 09:47:36 -0800986 // OF-DPA does not pop in MPLS table in some cases. For the L3 VPN, it requires
Saurav Das9c705342017-01-06 11:36:01 -0800987 // setting the MPLS_TYPE so pop can happen down the pipeline
Pier Luigi3bfe32c2017-01-30 09:47:36 -0800988 if (mplsNextTable == MPLS_TYPE_TABLE && isNotMplsBos(selector)) {
989 tb.immediate().popMpls();
990 }
Charles Chan14967c22015-12-07 11:11:50 -0800991 }
992 if (instr instanceof L3ModificationInstruction &&
993 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
994 // FIXME Should modify the app to send the correct DEC_MPLS_TTL instruction
995 tb.immediate().decMplsTtl();
996 }
997 if (instr instanceof L3ModificationInstruction &&
998 ((L3ModificationInstruction) instr).subtype() == L3SubType.TTL_IN) {
999 tb.immediate().add(instr);
1000 }
Saurav Das8a0732e2015-11-20 15:27:53 -08001001 }
1002 }
1003 }
Saurav Das822c4e22015-10-23 10:51:11 -07001004
1005 if (fwd.nextId() != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -08001006 if (forTableId == MPLS_TABLE_1 && !popMpls) {
1007 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
Saurav Das25190812016-05-27 13:54:07 -07001008 + "is not implemented in OF-DPA yet. Aborting this flow {} -> next:{}"
1009 + "in this device {}", fwd.id(), fwd.nextId(), deviceId);
Pier Ventree0ae7a32016-11-23 09:57:42 -08001010 // XXX We could convert to forwarding to a single-port, via a MPLS interface,
1011 // or a MPLS SWAP (with-same) but that would have to be handled in the next-objective.
1012 // Also the pop-mpls logic used here won't work in non-BoS case.
Saurav Das4ce45962015-11-24 23:21:05 -08001013 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
Saurav Das8a0732e2015-11-20 15:27:53 -08001014 return Collections.emptySet();
1015 }
1016
Saurav Das423fe2b2015-12-04 10:52:59 -08001017 NextGroup next = getGroupForNextObjective(fwd.nextId());
1018 if (next != null) {
1019 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1020 // we only need the top level group's key to point the flow to it
1021 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
Pier Ventre140a8942016-11-02 07:26:38 -07001022 if (isNotMplsBos(selector) && group.type().equals(HASHED)) {
1023 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
1024 + "is not implemented in OF-DPA yet. Aborting this flow {} -> next:{}"
1025 + "in this device {}", fwd.id(), fwd.nextId(), deviceId);
1026 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1027 return Collections.emptySet();
1028 }
Saurav Das423fe2b2015-12-04 10:52:59 -08001029 if (group == null) {
1030 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1031 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1032 fail(fwd, ObjectiveError.GROUPMISSING);
1033 return Collections.emptySet();
1034 }
1035 tb.deferred().group(group.id());
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001036 // check if group is empty
1037 if (gkeys.size() == 1 && gkeys.get(0).size() == 1) {
1038 log.warn("Found empty group 0x{} in dev:{} .. will retry fwd:{}",
1039 Integer.toHexString(group.id().id()), deviceId, fwd.id());
1040 emptyGroup = true;
1041 }
Saurav Das25190812016-05-27 13:54:07 -07001042 } else {
1043 log.warn("Cannot find group for nextId:{} in dev:{}. Aborting fwd:{}",
1044 fwd.nextId(), deviceId, fwd.id());
1045 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1046 return Collections.emptySet();
Saurav Das822c4e22015-10-23 10:51:11 -07001047 }
Saurav Das822c4e22015-10-23 10:51:11 -07001048 }
Charles Chancad338a2016-09-16 18:03:11 -07001049
1050 if (forTableId == MPLS_TABLE_1) {
Pier Ventre140a8942016-11-02 07:26:38 -07001051 if (mplsNextTable == MPLS_L3_TYPE_TABLE) {
Charles Chancad338a2016-09-16 18:03:11 -07001052 Ofdpa3SetMplsType setMplsType = new Ofdpa3SetMplsType(Ofdpa3MplsType.L3_PHP);
Saurav Das9c705342017-01-06 11:36:01 -08001053 // set mpls type as apply_action
1054 tb.immediate().extension(setMplsType, deviceId);
Charles Chancad338a2016-09-16 18:03:11 -07001055 }
1056 tb.transition(mplsNextTable);
1057 } else {
1058 tb.transition(ACL_TABLE);
1059 }
1060
Saurav Das822c4e22015-10-23 10:51:11 -07001061 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1062 .fromApp(fwd.appId())
1063 .withPriority(fwd.priority())
1064 .forDevice(deviceId)
Saurav Das8a0732e2015-11-20 15:27:53 -08001065 .withSelector(filteredSelector.build())
1066 .withTreatment(tb.build())
1067 .forTable(forTableId);
Saurav Das822c4e22015-10-23 10:51:11 -07001068
1069 if (fwd.permanent()) {
1070 ruleBuilder.makePermanent();
1071 } else {
1072 ruleBuilder.makeTemporary(fwd.timeout());
1073 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001074 Collection<FlowRule> flowRuleCollection = new ArrayList<>();
1075 flowRuleCollection.add(ruleBuilder.build());
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001076 if (!allowDefaultRoute) {
Pier Ventree0ae7a32016-11-23 09:57:42 -08001077 flowRuleCollection.add(
1078 defaultRoute(fwd, complementarySelector, forTableId, tb)
1079 );
Flavio Castroe10fa242016-01-15 12:43:51 -08001080 log.debug("Default rule 0.0.0.0/0 is being installed two rules");
1081 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001082 // XXX retrying flows may be necessary due to bug CORD-554
1083 if (emptyGroup) {
1084 executorService.schedule(new RetryFlows(fwd, flowRuleCollection),
1085 RETRY_MS, TimeUnit.MILLISECONDS);
1086 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001087 return flowRuleCollection;
Saurav Das822c4e22015-10-23 10:51:11 -07001088 }
1089
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001090 protected int buildIpv4Selector(TrafficSelector.Builder builderToUpdate,
1091 TrafficSelector.Builder extBuilder,
1092 ForwardingObjective fwd,
1093 boolean allowDefaultRoute) {
1094 TrafficSelector selector = fwd.selector();
1095
1096 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
1097 if (ipv4Dst.isMulticast()) {
1098 if (ipv4Dst.prefixLength() != 32) {
1099 log.warn("Multicast specific forwarding objective can only be /32");
1100 fail(fwd, ObjectiveError.BADPARAMS);
1101 return -1;
1102 }
1103 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
1104 if (assignedVlan == null) {
1105 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
1106 fail(fwd, ObjectiveError.BADPARAMS);
1107 return -1;
1108 }
Charles Chand1172632017-03-15 17:33:09 -07001109 if (requireVlanExtensions()) {
1110 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
1111 builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
1112 } else {
1113 builderToUpdate.matchVlanId(assignedVlan);
1114 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001115 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1116 log.debug("processing IPv4 multicast specific forwarding objective {} -> next:{}"
1117 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1118 } else {
1119 if (ipv4Dst.prefixLength() == 0) {
1120 if (allowDefaultRoute) {
1121 // The entire IPV4_DST field is wildcarded intentionally
1122 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4);
1123 } else {
1124 // NOTE: The switch does not support matching 0.0.0.0/0
1125 // Split it into 0.0.0.0/1 and 128.0.0.0/1
1126 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4)
1127 .matchIPDst(IpPrefix.valueOf("0.0.0.0/1"));
1128 extBuilder.matchEthType(Ethernet.TYPE_IPV4)
1129 .matchIPDst(IpPrefix.valueOf("128.0.0.0/1"));
1130 }
1131 } else {
1132 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1133 }
1134 log.debug("processing IPv4 unicast specific forwarding objective {} -> next:{}"
1135 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1136 }
1137 return 0;
1138 }
1139
1140 /**
1141 * Helper method to build Ipv6 selector using the selector provided by
1142 * a forwarding objective.
1143 *
1144 * @param builderToUpdate the builder to update
1145 * @param fwd the selector to read
1146 * @return 0 if the update ends correctly. -1 if the matches
1147 * are not yet supported
1148 */
1149 protected int buildIpv6Selector(TrafficSelector.Builder builderToUpdate,
1150 ForwardingObjective fwd) {
1151
1152 TrafficSelector selector = fwd.selector();
1153
1154 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
1155 if (ipv6Dst.isMulticast()) {
1156 log.warn("IPv6 Multicast is currently not supported");
1157 fail(fwd, ObjectiveError.BADPARAMS);
1158 return -1;
1159 }
1160 if (ipv6Dst.prefixLength() != 0) {
1161 builderToUpdate.matchIPv6Dst(ipv6Dst);
1162 }
1163 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6);
1164 log.debug("processing IPv6 unicast specific forwarding objective {} -> next:{}"
1165 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1166 return 0;
1167 }
1168
Pier Ventree0ae7a32016-11-23 09:57:42 -08001169 protected FlowRule defaultRoute(ForwardingObjective fwd,
1170 TrafficSelector.Builder complementarySelector,
1171 int forTableId,
1172 TrafficTreatment.Builder tb) {
1173 FlowRule.Builder rule = DefaultFlowRule.builder()
1174 .fromApp(fwd.appId())
1175 .withPriority(fwd.priority())
1176 .forDevice(deviceId)
1177 .withSelector(complementarySelector.build())
1178 .withTreatment(tb.build())
1179 .forTable(forTableId);
1180 if (fwd.permanent()) {
1181 rule.makePermanent();
1182 } else {
1183 rule.makeTemporary(fwd.timeout());
1184 }
1185 return rule.build();
1186 }
1187
Saurav Das4ce45962015-11-24 23:21:05 -08001188 /**
1189 * Handles forwarding rules to the L2 bridging table. Flow actions are not
1190 * allowed in the bridging table - instead we use L2 Interface group or
1191 * L2 flood group
1192 *
1193 * @param fwd the forwarding objective
1194 * @return A collection of flow rules, or an empty set
1195 */
1196 protected Collection<FlowRule> processEthDstSpecific(ForwardingObjective fwd) {
1197 List<FlowRule> rules = new ArrayList<>();
1198
1199 // Build filtered selector
1200 TrafficSelector selector = fwd.selector();
1201 EthCriterion ethCriterion = (EthCriterion) selector
1202 .getCriterion(Criterion.Type.ETH_DST);
1203 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) selector
1204 .getCriterion(Criterion.Type.VLAN_VID);
1205
1206 if (vlanIdCriterion == null) {
1207 log.warn("Forwarding objective for bridging requires vlan. Not "
1208 + "installing fwd:{} in dev:{}", fwd.id(), deviceId);
1209 fail(fwd, ObjectiveError.BADPARAMS);
1210 return Collections.emptySet();
1211 }
1212
1213 TrafficSelector.Builder filteredSelectorBuilder =
1214 DefaultTrafficSelector.builder();
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001215
1216 if (!ethCriterion.mac().equals(NONE) &&
1217 !ethCriterion.mac().equals(BROADCAST)) {
Saurav Das4ce45962015-11-24 23:21:05 -08001218 filteredSelectorBuilder.matchEthDst(ethCriterion.mac());
1219 log.debug("processing L2 forwarding objective:{} -> next:{} in dev:{}",
1220 fwd.id(), fwd.nextId(), deviceId);
1221 } else {
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001222 // Use wildcard DST_MAC if the MacAddress is None or Broadcast
Saurav Das4ce45962015-11-24 23:21:05 -08001223 log.debug("processing L2 Broadcast forwarding objective:{} -> next:{} "
1224 + "in dev:{} for vlan:{}",
1225 fwd.id(), fwd.nextId(), deviceId, vlanIdCriterion.vlanId());
1226 }
Charles Chand1172632017-03-15 17:33:09 -07001227 if (requireVlanExtensions()) {
1228 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanIdCriterion.vlanId());
1229 filteredSelectorBuilder.extension(ofdpaMatchVlanVid, deviceId);
1230 } else {
1231 filteredSelectorBuilder.matchVlanId(vlanIdCriterion.vlanId());
1232 }
Saurav Das4ce45962015-11-24 23:21:05 -08001233 TrafficSelector filteredSelector = filteredSelectorBuilder.build();
1234
1235 if (fwd.treatment() != null) {
1236 log.warn("Ignoring traffic treatment in fwd rule {} meant for L2 table"
1237 + "for dev:{}. Expecting only nextId", fwd.id(), deviceId);
1238 }
1239
1240 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1241 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001242 NextGroup next = getGroupForNextObjective(fwd.nextId());
Saurav Das4ce45962015-11-24 23:21:05 -08001243 if (next != null) {
1244 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1245 // we only need the top level group's key to point the flow to it
1246 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1247 if (group != null) {
1248 treatmentBuilder.deferred().group(group.id());
1249 } else {
1250 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1251 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1252 fail(fwd, ObjectiveError.GROUPMISSING);
1253 return Collections.emptySet();
1254 }
1255 }
1256 }
1257 treatmentBuilder.immediate().transition(ACL_TABLE);
1258 TrafficTreatment filteredTreatment = treatmentBuilder.build();
1259
1260 // Build bridging table entries
1261 FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder();
1262 flowRuleBuilder.fromApp(fwd.appId())
1263 .withPriority(fwd.priority())
1264 .forDevice(deviceId)
1265 .withSelector(filteredSelector)
1266 .withTreatment(filteredTreatment)
1267 .forTable(BRIDGING_TABLE);
1268 if (fwd.permanent()) {
1269 flowRuleBuilder.makePermanent();
1270 } else {
1271 flowRuleBuilder.makeTemporary(fwd.timeout());
1272 }
1273 rules.add(flowRuleBuilder.build());
1274 return rules;
1275 }
1276
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001277 //////////////////////////////////////
1278 // Helper Methods and Classes
1279 //////////////////////////////////////
1280
1281 private boolean isSupportedEthTypeObjective(ForwardingObjective fwd) {
1282 TrafficSelector selector = fwd.selector();
1283 EthTypeCriterion ethType = (EthTypeCriterion) selector
1284 .getCriterion(Criterion.Type.ETH_TYPE);
1285 return !((ethType == null) ||
1286 ((ethType.ethType().toShort() != Ethernet.TYPE_IPV4) &&
Pier Ventree0ae7a32016-11-23 09:57:42 -08001287 (ethType.ethType().toShort() != Ethernet.MPLS_UNICAST)) &&
1288 (ethType.ethType().toShort() != Ethernet.TYPE_IPV6));
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001289 }
1290
1291 private boolean isSupportedEthDstObjective(ForwardingObjective fwd) {
1292 TrafficSelector selector = fwd.selector();
1293 EthCriterion ethDst = (EthCriterion) selector
1294 .getCriterion(Criterion.Type.ETH_DST);
1295 VlanIdCriterion vlanId = (VlanIdCriterion) selector
1296 .getCriterion(Criterion.Type.VLAN_VID);
1297 return !(ethDst == null && vlanId == null);
1298 }
1299
Saurav Das423fe2b2015-12-04 10:52:59 -08001300 protected NextGroup getGroupForNextObjective(Integer nextId) {
1301 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
1302 if (next != null) {
1303 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1304 if (gkeys != null && !gkeys.isEmpty()) {
1305 return next;
1306 } else {
1307 log.warn("Empty next group found in FlowObjective store for "
1308 + "next-id:{} in dev:{}", nextId, deviceId);
1309 }
1310 } else {
1311 log.warn("next-id {} not found in Flow objective store for dev:{}",
1312 nextId, deviceId);
1313 }
1314 return null;
1315 }
1316
Charles Chan188ebf52015-12-23 00:15:11 -08001317 protected static void pass(Objective obj) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001318 obj.context().ifPresent(context -> context.onSuccess(obj));
Saurav Das822c4e22015-10-23 10:51:11 -07001319 }
1320
Charles Chan188ebf52015-12-23 00:15:11 -08001321 protected static void fail(Objective obj, ObjectiveError error) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001322 obj.context().ifPresent(context -> context.onError(obj, error));
Saurav Das822c4e22015-10-23 10:51:11 -07001323 }
Saurav Das24431192016-03-07 19:13:00 -08001324
Saurav Das24431192016-03-07 19:13:00 -08001325 @Override
1326 public List<String> getNextMappings(NextGroup nextGroup) {
1327 List<String> mappings = new ArrayList<>();
1328 List<Deque<GroupKey>> gkeys = appKryo.deserialize(nextGroup.data());
1329 for (Deque<GroupKey> gkd : gkeys) {
1330 Group lastGroup = null;
Saurav Das8be4e3a2016-03-11 17:19:07 -08001331 StringBuffer gchain = new StringBuffer();
Saurav Das24431192016-03-07 19:13:00 -08001332 for (GroupKey gk : gkd) {
1333 Group g = groupService.getGroup(deviceId, gk);
Saurav Das8be4e3a2016-03-11 17:19:07 -08001334 if (g == null) {
Saurav Das25190812016-05-27 13:54:07 -07001335 gchain.append(" NoGrp").append(" -->");
Saurav Das8be4e3a2016-03-11 17:19:07 -08001336 continue;
1337 }
1338 gchain.append(" 0x").append(Integer.toHexString(g.id().id()))
1339 .append(" -->");
Saurav Das24431192016-03-07 19:13:00 -08001340 lastGroup = g;
1341 }
1342 // add port information for last group in group-chain
Saurav Das25190812016-05-27 13:54:07 -07001343 List<Instruction> lastGroupIns = new ArrayList<Instruction>();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001344 if (lastGroup != null && !lastGroup.buckets().buckets().isEmpty()) {
Saurav Das25190812016-05-27 13:54:07 -07001345 lastGroupIns = lastGroup.buckets().buckets().get(0)
1346 .treatment().allInstructions();
1347 }
1348 for (Instruction i: lastGroupIns) {
Saurav Das24431192016-03-07 19:13:00 -08001349 if (i instanceof OutputInstruction) {
Saurav Das8be4e3a2016-03-11 17:19:07 -08001350 gchain.append(" port:").append(((OutputInstruction) i).port());
Saurav Das24431192016-03-07 19:13:00 -08001351 }
1352 }
Saurav Das8be4e3a2016-03-11 17:19:07 -08001353 mappings.add(gchain.toString());
Saurav Das24431192016-03-07 19:13:00 -08001354 }
1355 return mappings;
1356 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001357
Pier Ventre140a8942016-11-02 07:26:38 -07001358 static boolean isMplsBos(TrafficSelector selector) {
1359 MplsBosCriterion bosCriterion = (MplsBosCriterion) selector.getCriterion(MPLS_BOS);
1360 return bosCriterion != null && bosCriterion.mplsBos();
1361 }
1362
1363 static boolean isNotMplsBos(TrafficSelector selector) {
1364 MplsBosCriterion bosCriterion = (MplsBosCriterion) selector.getCriterion(MPLS_BOS);
1365 return bosCriterion != null && !bosCriterion.mplsBos();
1366 }
1367
Charles Chan5b9df8d2016-03-28 22:21:40 -07001368 protected static VlanId readVlanFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001369 if (selector == null) {
1370 return null;
1371 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001372 Criterion criterion = selector.getCriterion(Criterion.Type.VLAN_VID);
1373 return (criterion == null)
1374 ? null : ((VlanIdCriterion) criterion).vlanId();
1375 }
1376
1377 protected static IpPrefix readIpDstFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001378 if (selector == null) {
1379 return null;
1380 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001381 Criterion criterion = selector.getCriterion(Criterion.Type.IPV4_DST);
1382 return (criterion == null) ? null : ((IPCriterion) criterion).ip();
1383 }
Charles Chand55e84d2016-03-30 17:54:24 -07001384
1385 private static VlanId readVlanFromTreatment(TrafficTreatment treatment) {
Saurav Das59232cf2016-04-27 18:35:50 -07001386 if (treatment == null) {
1387 return null;
1388 }
Charles Chand55e84d2016-03-30 17:54:24 -07001389 for (Instruction i : treatment.allInstructions()) {
1390 if (i instanceof ModVlanIdInstruction) {
1391 return ((ModVlanIdInstruction) i).vlanId();
1392 }
1393 }
1394 return null;
1395 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001396
1397 /**
1398 * Utility class that retries sending flows a fixed number of times, even if
1399 * some of the attempts are successful. Used only for forwarding objectives.
1400 */
1401 protected final class RetryFlows implements Runnable {
1402 int attempts = MAX_RETRY_ATTEMPTS;
1403 private Collection<FlowRule> retryFlows;
1404 private ForwardingObjective fwd;
1405
1406 RetryFlows(ForwardingObjective fwd, Collection<FlowRule> retryFlows) {
1407 this.fwd = fwd;
1408 this.retryFlows = retryFlows;
1409 }
1410
1411 @Override
1412 public void run() {
1413 log.info("RETRY FLOWS ATTEMPT# {} for fwd:{} rules:{}",
1414 MAX_RETRY_ATTEMPTS - attempts, fwd.id(), retryFlows.size());
1415 sendForward(fwd, retryFlows);
1416 if (--attempts > 0) {
1417 executorService.schedule(this, RETRY_MS, TimeUnit.MILLISECONDS);
1418 }
1419 }
1420 }
1421
Saurav Das822c4e22015-10-23 10:51:11 -07001422}