blob: 6e7feca7c46d8ee89a675f157316f8b015494223 [file] [log] [blame]
Saurav Das822c4e22015-10-23 10:51:11 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
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 */
Yi Tsengef19de12017-04-24 11:33:05 -070016package org.onosproject.driver.pipeline.ofdpa;
Saurav Das822c4e22015-10-23 10:51:11 -070017
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;
Jonghwan Hyun800d9d02018-04-09 09:40:50 -070021import org.onlab.packet.EthType;
Saurav Das822c4e22015-10-23 10:51:11 -070022import org.onlab.packet.Ethernet;
Alex Yashchuk4caa8e82017-12-08 17:40:05 +020023import org.onlab.packet.EthType.EtherType;
Julia Ferguson65428c32017-08-10 18:15:24 +000024import org.onlab.packet.IpAddress;
Charles Chan14967c22015-12-07 11:11:50 -080025import org.onlab.packet.IpPrefix;
Charles Chan45b69ab2018-03-02 15:41:41 -080026import org.onlab.packet.MacAddress;
Saurav Das822c4e22015-10-23 10:51:11 -070027import org.onlab.packet.VlanId;
28import org.onlab.util.KryoNamespace;
29import org.onosproject.core.ApplicationId;
30import org.onosproject.core.CoreService;
Jonghwan Hyun800d9d02018-04-09 09:40:50 -070031import org.onosproject.driver.extensions.Ofdpa3CopyField;
Charles Chancad338a2016-09-16 18:03:11 -070032import org.onosproject.driver.extensions.Ofdpa3MplsType;
33import org.onosproject.driver.extensions.Ofdpa3SetMplsType;
Jonghwan Hyun800d9d02018-04-09 09:40:50 -070034import org.onosproject.driver.extensions.OfdpaMatchActsetOutput;
35import org.onosproject.driver.extensions.OfdpaMatchAllowVlanTranslation;
Charles Chan14967c22015-12-07 11:11:50 -080036import org.onosproject.driver.extensions.OfdpaMatchVlanVid;
37import org.onosproject.driver.extensions.OfdpaSetVlanVid;
Saurav Das822c4e22015-10-23 10:51:11 -070038import org.onosproject.net.DeviceId;
39import org.onosproject.net.Port;
40import org.onosproject.net.PortNumber;
41import org.onosproject.net.behaviour.NextGroup;
42import org.onosproject.net.behaviour.Pipeliner;
43import org.onosproject.net.behaviour.PipelinerContext;
44import org.onosproject.net.device.DeviceService;
45import org.onosproject.net.driver.AbstractHandlerBehaviour;
46import org.onosproject.net.flow.DefaultFlowRule;
47import org.onosproject.net.flow.DefaultTrafficSelector;
48import org.onosproject.net.flow.DefaultTrafficTreatment;
49import org.onosproject.net.flow.FlowRule;
50import org.onosproject.net.flow.FlowRuleOperations;
51import org.onosproject.net.flow.FlowRuleOperationsContext;
52import org.onosproject.net.flow.FlowRuleService;
53import org.onosproject.net.flow.TrafficSelector;
54import org.onosproject.net.flow.TrafficTreatment;
55import org.onosproject.net.flow.criteria.Criteria;
56import org.onosproject.net.flow.criteria.Criterion;
57import org.onosproject.net.flow.criteria.EthCriterion;
58import org.onosproject.net.flow.criteria.EthTypeCriterion;
59import org.onosproject.net.flow.criteria.IPCriterion;
Pier Ventree0ae7a32016-11-23 09:57:42 -080060import org.onosproject.net.flow.criteria.Icmpv6CodeCriterion;
61import org.onosproject.net.flow.criteria.Icmpv6TypeCriterion;
Saurav Das8a0732e2015-11-20 15:27:53 -080062import org.onosproject.net.flow.criteria.MplsBosCriterion;
63import org.onosproject.net.flow.criteria.MplsCriterion;
Saurav Das822c4e22015-10-23 10:51:11 -070064import org.onosproject.net.flow.criteria.PortCriterion;
Charles Chand9e47c62017-10-05 15:17:15 -070065import org.onosproject.net.flow.criteria.TcpPortCriterion;
66import org.onosproject.net.flow.criteria.UdpPortCriterion;
Saurav Das822c4e22015-10-23 10:51:11 -070067import org.onosproject.net.flow.criteria.VlanIdCriterion;
68import org.onosproject.net.flow.instructions.Instruction;
69import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
70import org.onosproject.net.flow.instructions.L2ModificationInstruction;
Saurav Das8a0732e2015-11-20 15:27:53 -080071import org.onosproject.net.flow.instructions.L2ModificationInstruction.L2SubType;
Saurav Das822c4e22015-10-23 10:51:11 -070072import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanIdInstruction;
Charles Chan45b69ab2018-03-02 15:41:41 -080073import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModEtherInstruction;
Alex Yashchuk4caa8e82017-12-08 17:40:05 +020074import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModMplsHeaderInstruction;
Charles Chan14967c22015-12-07 11:11:50 -080075import org.onosproject.net.flow.instructions.L3ModificationInstruction;
76import org.onosproject.net.flow.instructions.L3ModificationInstruction.L3SubType;
Saurav Das822c4e22015-10-23 10:51:11 -070077import org.onosproject.net.flowobjective.FilteringObjective;
78import org.onosproject.net.flowobjective.FlowObjectiveStore;
79import org.onosproject.net.flowobjective.ForwardingObjective;
80import org.onosproject.net.flowobjective.NextObjective;
81import org.onosproject.net.flowobjective.Objective;
82import org.onosproject.net.flowobjective.ObjectiveError;
Saurav Das822c4e22015-10-23 10:51:11 -070083import org.onosproject.net.group.DefaultGroupKey;
84import org.onosproject.net.group.Group;
Saurav Das822c4e22015-10-23 10:51:11 -070085import org.onosproject.net.group.GroupKey;
Saurav Das822c4e22015-10-23 10:51:11 -070086import org.onosproject.net.group.GroupService;
Saurav Das822c4e22015-10-23 10:51:11 -070087import org.onosproject.store.serializers.KryoNamespaces;
88import org.slf4j.Logger;
89
Pier Ventree0ae7a32016-11-23 09:57:42 -080090import java.util.ArrayDeque;
91import java.util.ArrayList;
92import java.util.Collection;
93import java.util.Collections;
94import java.util.Deque;
95import java.util.List;
96import java.util.Objects;
Jonghwan Hyun800d9d02018-04-09 09:40:50 -070097import java.util.Optional;
Pier Ventree0ae7a32016-11-23 09:57:42 -080098import java.util.concurrent.ScheduledExecutorService;
99import java.util.concurrent.TimeUnit;
100
101import static java.util.concurrent.Executors.newScheduledThreadPool;
Yi Tseng3a77b4f2017-02-06 15:02:17 -0800102import static org.onlab.packet.MacAddress.BROADCAST;
Charles Chanb4879a52017-10-20 19:09:16 -0700103import static org.onlab.packet.MacAddress.IPV4_MULTICAST;
104import static org.onlab.packet.MacAddress.IPV6_MULTICAST;
Yi Tseng3a77b4f2017-02-06 15:02:17 -0800105import static org.onlab.packet.MacAddress.NONE;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800106import static org.onlab.util.Tools.groupedThreads;
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700107import static org.onosproject.driver.extensions.Ofdpa3CopyField.OXM_ID_PACKET_REG_1;
108import static org.onosproject.driver.extensions.Ofdpa3CopyField.OXM_ID_VLAN_VID;
Yi Tsengef19de12017-04-24 11:33:05 -0700109import static org.onosproject.driver.pipeline.ofdpa.OfdpaGroupHandlerUtility.*;
Charles Chand9e47c62017-10-05 15:17:15 -0700110import static org.onosproject.net.flow.criteria.Criterion.Type.ETH_TYPE;
Pier Luigi075f1012018-02-01 10:23:12 +0100111import static org.onosproject.net.group.GroupDescription.Type.SELECT;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800112import static org.slf4j.LoggerFactory.getLogger;
Pier Ventre140a8942016-11-02 07:26:38 -0700113import static org.onosproject.net.flow.criteria.Criterion.Type.MPLS_BOS;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800114
Saurav Das822c4e22015-10-23 10:51:11 -0700115/**
116 * Driver for Broadcom's OF-DPA v2.0 TTP.
Saurav Das822c4e22015-10-23 10:51:11 -0700117 */
Charles Chan361154b2016-03-24 10:23:39 -0700118public class Ofdpa2Pipeline extends AbstractHandlerBehaviour implements Pipeliner {
Saurav Das822c4e22015-10-23 10:51:11 -0700119 protected static final int PORT_TABLE = 0;
120 protected static final int VLAN_TABLE = 10;
Pier Ventre42287df2016-11-09 14:17:26 -0800121 protected static final int VLAN_1_TABLE = 11;
122 protected static final int MPLS_L2_PORT_FLOW_TABLE = 13;
123 protected static final int MPLS_L2_PORT_PCP_TRUST_FLOW_TABLE = 16;
Saurav Das822c4e22015-10-23 10:51:11 -0700124 protected static final int TMAC_TABLE = 20;
125 protected static final int UNICAST_ROUTING_TABLE = 30;
126 protected static final int MULTICAST_ROUTING_TABLE = 40;
127 protected static final int MPLS_TABLE_0 = 23;
128 protected static final int MPLS_TABLE_1 = 24;
Pier Ventre140a8942016-11-02 07:26:38 -0700129 protected static final int MPLS_L3_TYPE_TABLE = 27;
130 protected static final int MPLS_TYPE_TABLE = 29;
Saurav Das822c4e22015-10-23 10:51:11 -0700131 protected static final int BRIDGING_TABLE = 50;
132 protected static final int ACL_TABLE = 60;
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700133 protected static final int EGRESS_VLAN_FLOW_TABLE = 210;
134 protected static final int EGRESS_DSCP_PCP_REMARK_FLOW_TABLE = 230;
135 protected static final int EGRESS_TPID_FLOW_TABLE = 235;
Saurav Das822c4e22015-10-23 10:51:11 -0700136 protected static final int MAC_LEARNING_TABLE = 254;
137 protected static final long OFPP_MAX = 0xffffff00L;
138
Saurav Das52025962016-01-28 22:30:01 -0800139 protected static final int HIGHEST_PRIORITY = 0xffff;
Saurav Das2857f382015-11-03 14:39:27 -0800140 protected static final int DEFAULT_PRIORITY = 0x8000;
Andrea Campanellab955fec2018-04-27 14:44:15 +0200141 // Priority Introduced for Mcast Drop Rules
142 protected static final int MIDDLE_PRIORITY = 0x4000;
Saurav Das822c4e22015-10-23 10:51:11 -0700143 protected static final int LOWEST_PRIORITY = 0x0;
144
Pier Ventre42287df2016-11-09 14:17:26 -0800145 protected static final int MPLS_L2_PORT_PRIORITY = 2;
Pier Ventre42287df2016-11-09 14:17:26 -0800146 protected static final int MPLS_TUNNEL_ID_BASE = 0x10000;
147 protected static final int MPLS_TUNNEL_ID_MAX = 0x1FFFF;
148
Pier Ventre70d53ba2016-11-17 22:26:29 -0800149 protected static final int MPLS_UNI_PORT_MAX = 0x0000FFFF;
Pier Ventre70d53ba2016-11-17 22:26:29 -0800150 protected static final int MPLS_NNI_PORT_BASE = 0x00020000;
151 protected static final int MPLS_NNI_PORT_MAX = 0x0002FFFF;
152
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700153 protected static final short ALLOW_VLAN_TRANSLATION = 1;
154 protected static final int COPY_FIELD_NBITS = 12;
155 protected static final int COPY_FIELD_OFFSET = 0;
156
Saurav Das822c4e22015-10-23 10:51:11 -0700157 private final Logger log = getLogger(getClass());
Charles Chan425854b2016-04-11 15:32:12 -0700158 protected ServiceDirectory serviceDirectory;
Saurav Das822c4e22015-10-23 10:51:11 -0700159 protected FlowRuleService flowRuleService;
Charles Chan425854b2016-04-11 15:32:12 -0700160 protected CoreService coreService;
Saurav Das8a0732e2015-11-20 15:27:53 -0800161 protected GroupService groupService;
162 protected FlowObjectiveStore flowObjectiveStore;
Saurav Das822c4e22015-10-23 10:51:11 -0700163 protected DeviceId deviceId;
164 protected ApplicationId driverId;
Saurav Das822c4e22015-10-23 10:51:11 -0700165 protected DeviceService deviceService;
Charles Chan188ebf52015-12-23 00:15:11 -0800166 protected static KryoNamespace appKryo = new KryoNamespace.Builder()
Yi Tsengef19de12017-04-24 11:33:05 -0700167 .register(KryoNamespaces.API)
168 .register(GroupKey.class)
169 .register(DefaultGroupKey.class)
170 .register(OfdpaNextGroup.class)
171 .register(ArrayDeque.class)
172 .build("Ofdpa2Pipeline");
Saurav Das822c4e22015-10-23 10:51:11 -0700173
Charles Chan425854b2016-04-11 15:32:12 -0700174 protected Ofdpa2GroupHandler groupHandler;
Saurav Das822c4e22015-10-23 10:51:11 -0700175
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700176 // flows installations to be retried
Charles Chan50d900c2018-03-02 13:26:22 -0800177 private ScheduledExecutorService executorService
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700178 = newScheduledThreadPool(5, groupedThreads("OfdpaPipeliner", "retry-%d", log));
Charles Chan50d900c2018-03-02 13:26:22 -0800179 private static final int MAX_RETRY_ATTEMPTS = 10;
180 private static final int RETRY_MS = 1000;
Saurav Das4f980082015-11-05 13:39:15 -0800181
Saurav Das822c4e22015-10-23 10:51:11 -0700182 @Override
183 public void init(DeviceId deviceId, PipelinerContext context) {
Saurav Das822c4e22015-10-23 10:51:11 -0700184 this.deviceId = deviceId;
185
Charles Chan425854b2016-04-11 15:32:12 -0700186 serviceDirectory = context.directory();
Saurav Das822c4e22015-10-23 10:51:11 -0700187 coreService = serviceDirectory.get(CoreService.class);
188 flowRuleService = serviceDirectory.get(FlowRuleService.class);
189 groupService = serviceDirectory.get(GroupService.class);
190 flowObjectiveStore = context.store();
Saurav Das822c4e22015-10-23 10:51:11 -0700191 deviceService = serviceDirectory.get(DeviceService.class);
Saurav Das822c4e22015-10-23 10:51:11 -0700192
Charles Chan40132b32017-01-22 00:19:37 -0800193 initDriverId();
194 initGroupHander(context);
Saurav Das822c4e22015-10-23 10:51:11 -0700195
Saurav Das822c4e22015-10-23 10:51:11 -0700196 initializePipeline();
Saurav Das822c4e22015-10-23 10:51:11 -0700197 }
198
Charles Chan40132b32017-01-22 00:19:37 -0800199 protected void initDriverId() {
200 driverId = coreService.registerApplication(
201 "org.onosproject.driver.Ofdpa2Pipeline");
202 }
203
204 protected void initGroupHander(PipelinerContext context) {
205 groupHandler = new Ofdpa2GroupHandler();
206 groupHandler.init(deviceId, context);
207 }
208
Saurav Das822c4e22015-10-23 10:51:11 -0700209 protected void initializePipeline() {
Charles Chan188ebf52015-12-23 00:15:11 -0800210 // OF-DPA does not require initializing the pipeline as it puts default
211 // rules automatically in the hardware. However emulation of OFDPA in
212 // software switches does require table-miss-entries.
Saurav Das822c4e22015-10-23 10:51:11 -0700213 }
214
Charles Chand1172632017-03-15 17:33:09 -0700215 /**
Alex Yashchuk4caa8e82017-12-08 17:40:05 +0200216 * Determines whether this pipeline requires MPLS POP instruction.
217 *
218 * @return true to use MPLS POP instruction
219 */
220 public boolean requireMplsPop() {
221 return true;
222 }
223
224 /**
225 * Determines whether this pipeline requires MPLS BOS match.
226 *
227 * @return true to use MPLS BOS match
228 */
229 public boolean requireMplsBosMatch() {
230 return true;
231 }
232
233 /**
234 * Determines whether this pipeline requires MPLS TTL decrement and copy.
235 *
236 * @return true to use MPLS TTL decrement and copy
237 */
238 public boolean requireMplsTtlModification() {
239 return true;
240 }
241
242 /**
Charles Chand1172632017-03-15 17:33:09 -0700243 * Determines whether this pipeline requires OFDPA match and set VLAN extensions.
244 *
245 * @return true to use the extensions
246 */
247 protected boolean requireVlanExtensions() {
248 return true;
249 }
250
Saurav Das86d13e82017-04-28 17:03:48 -0700251 /**
252 * Determines whether in-port should be matched on in TMAC table rules.
253 *
254 * @return true if match on in-port should be programmed
255 */
256 protected boolean matchInPortTmacTable() {
257 return true;
258 }
259
Charles Chand9e47c62017-10-05 15:17:15 -0700260 /**
261 * Determines whether matching L4 destination port on IPv6 packets is supported in ACL table.
262 *
263 * @return true if matching L4 destination port on IPv6 packets is supported in ACL table.
264 */
265 protected boolean supportIpv6L4Dst() {
266 return true;
267 }
268
Saurav Dasc568c342018-01-25 09:49:01 -0800269 /**
270 * Determines whether this driver should continue to retry flows that point
271 * to empty groups. See CORD-554.
272 *
273 * @return true if the driver should retry flows
274 */
275 protected boolean shouldRetry() {
276 return true;
277 }
278
Charles Chan45b69ab2018-03-02 15:41:41 -0800279 /**
280 * Determines whether this driver requires unicast flow to be installed before multicast flow
281 * in TMAC table.
282 *
283 * @return true if required
284 */
285 protected boolean requireUnicastBeforeMulticast() {
286 return false;
287 }
288
Andrea Campanellad980c6d2018-04-30 11:48:55 +0200289 /**
290 * Determines whether this driver supports installing a clearDeferred action on table 30.
291 *
292 * @return true if required
293 */
294 protected boolean supportsUnicastBlackHole() {
295 return true;
296 }
297
Saurav Das822c4e22015-10-23 10:51:11 -0700298 //////////////////////////////////////
299 // Flow Objectives
300 //////////////////////////////////////
301
302 @Override
303 public void filter(FilteringObjective filteringObjective) {
304 if (filteringObjective.type() == FilteringObjective.Type.PERMIT) {
305 processFilter(filteringObjective,
306 filteringObjective.op() == Objective.Operation.ADD,
307 filteringObjective.appId());
308 } else {
309 // Note that packets that don't match the PERMIT filter are
310 // automatically denied. The DENY filter is used to deny packets
311 // that are otherwise permitted by the PERMIT filter.
312 // Use ACL table flow rules here for DENY filtering objectives
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530313 log.warn("filter objective other than PERMIT currently not supported");
Saurav Das822c4e22015-10-23 10:51:11 -0700314 fail(filteringObjective, ObjectiveError.UNSUPPORTED);
315 }
316 }
317
318 @Override
319 public void forward(ForwardingObjective fwd) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700320 Collection<FlowRule> rules = processForward(fwd);
Saurav Das25190812016-05-27 13:54:07 -0700321 if (rules == null || rules.isEmpty()) {
322 // Assumes fail message has already been generated to the objective
323 // context. Returning here prevents spurious pass message to be
324 // generated by FlowRule service for empty flowOps.
325 return;
326 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700327 sendForward(fwd, rules);
328 }
329
Charles Chan50d900c2018-03-02 13:26:22 -0800330 private void sendForward(ForwardingObjective fwd, Collection<FlowRule> rules) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700331 FlowRuleOperations.Builder flowOpsBuilder = FlowRuleOperations.builder();
Saurav Das822c4e22015-10-23 10:51:11 -0700332 switch (fwd.op()) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700333 case ADD:
334 rules.stream()
335 .filter(Objects::nonNull)
336 .forEach(flowOpsBuilder::add);
Saurav Dasd2fded02016-12-02 15:43:47 -0800337 log.debug("Applying a add fwd-obj {} to sw:{}", fwd.id(), deviceId);
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700338 break;
339 case REMOVE:
340 rules.stream()
341 .filter(Objects::nonNull)
342 .forEach(flowOpsBuilder::remove);
Pier Ventre42287df2016-11-09 14:17:26 -0800343 log.debug("Deleting a flow rule to sw:{}", deviceId);
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700344 break;
345 default:
346 fail(fwd, ObjectiveError.UNKNOWN);
347 log.warn("Unknown forwarding type {}", fwd.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700348 }
349
Saurav Das822c4e22015-10-23 10:51:11 -0700350 flowRuleService.apply(flowOpsBuilder.build(new FlowRuleOperationsContext() {
351 @Override
352 public void onSuccess(FlowRuleOperations ops) {
353 pass(fwd);
354 }
355
356 @Override
357 public void onError(FlowRuleOperations ops) {
358 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
359 }
360 }));
Saurav Das822c4e22015-10-23 10:51:11 -0700361 }
362
363 @Override
364 public void next(NextObjective nextObjective) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800365 NextGroup nextGroup = flowObjectiveStore.getNextGroup(nextObjective.id());
366 switch (nextObjective.op()) {
367 case ADD:
Saurav Das4f980082015-11-05 13:39:15 -0800368 if (nextGroup != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800369 log.warn("Cannot add next {} that already exists in device {}",
370 nextObjective.id(), deviceId);
371 return;
372 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700373 log.debug("Processing NextObjective id {} in dev {} - add group",
Saurav Das8a0732e2015-11-20 15:27:53 -0800374 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700375 groupHandler.addGroup(nextObjective);
Saurav Das8a0732e2015-11-20 15:27:53 -0800376 break;
377 case ADD_TO_EXISTING:
378 if (nextGroup != null) {
Saurav Das1547b3f2017-05-05 17:01:08 -0700379 log.debug("Processing NextObjective id {} in dev {} - add bucket",
Saurav Das8a0732e2015-11-20 15:27:53 -0800380 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700381 groupHandler.addBucketToGroup(nextObjective, nextGroup);
Saurav Das4f980082015-11-05 13:39:15 -0800382 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800383 // it is possible that group-chain has not been fully created yet
Saurav Das423fe2b2015-12-04 10:52:59 -0800384 log.debug("Waiting to add bucket to group for next-id:{} in dev:{}",
385 nextObjective.id(), deviceId);
Yi Tseng47f82dc2017-03-05 22:48:39 -0800386
387 // by design multiple pending bucket is allowed for the group
388 groupHandler.pendingBuckets.compute(nextObjective.id(), (nextId, pendBkts) -> {
389 if (pendBkts == null) {
390 pendBkts = Sets.newHashSet();
391 }
392 pendBkts.add(nextObjective);
393 return pendBkts;
394 });
Saurav Das4f980082015-11-05 13:39:15 -0800395 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800396 break;
397 case REMOVE:
398 if (nextGroup == null) {
399 log.warn("Cannot remove next {} that does not exist in device {}",
400 nextObjective.id(), deviceId);
401 return;
402 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700403 log.debug("Processing NextObjective id {} in dev {} - remove group",
Saurav Das8a0732e2015-11-20 15:27:53 -0800404 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700405 groupHandler.removeGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800406 break;
407 case REMOVE_FROM_EXISTING:
408 if (nextGroup == null) {
409 log.warn("Cannot remove from next {} that does not exist in device {}",
410 nextObjective.id(), deviceId);
411 return;
412 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700413 log.debug("Processing NextObjective id {} in dev {} - remove bucket",
Saurav Das8a0732e2015-11-20 15:27:53 -0800414 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700415 groupHandler.removeBucketFromGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800416 break;
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +0900417 case MODIFY:
418 if (nextGroup == null) {
419 log.warn("Cannot modify next {} that does not exist in device {}",
420 nextObjective.id(), deviceId);
421 return;
422 }
423 log.debug("Processing NextObjective id {} in dev {} - modify bucket",
424 nextObjective.id(), deviceId);
425 groupHandler.modifyBucketFromGroup(nextObjective, nextGroup);
426 break;
Saurav Dasceccf242017-08-03 18:30:35 -0700427 case VERIFY:
428 if (nextGroup == null) {
429 log.warn("Cannot verify next {} that does not exist in device {}",
430 nextObjective.id(), deviceId);
431 return;
432 }
433 log.debug("Processing NextObjective id {} in dev {} - verify",
434 nextObjective.id(), deviceId);
435 groupHandler.verifyGroup(nextObjective, nextGroup);
436 break;
Saurav Das8a0732e2015-11-20 15:27:53 -0800437 default:
Saurav Das4f980082015-11-05 13:39:15 -0800438 log.warn("Unsupported operation {}", nextObjective.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700439 }
440 }
441
442 //////////////////////////////////////
443 // Flow handling
444 //////////////////////////////////////
445
446 /**
Yi Tseng0fdc07e2017-04-24 19:17:08 -0700447 * As per OFDPA 2.0 TTP, filtering of VLAN ids and MAC addresses (for routing)
448 * configured on switch ports happen in different tables.
Saurav Das822c4e22015-10-23 10:51:11 -0700449 *
450 * @param filt the filtering objective
451 * @param install indicates whether to add or remove the objective
452 * @param applicationId the application that sent this objective
453 */
Saurav Das52025962016-01-28 22:30:01 -0800454 protected void processFilter(FilteringObjective filt,
455 boolean install, ApplicationId applicationId) {
Saurav Das822c4e22015-10-23 10:51:11 -0700456 // This driver only processes filtering criteria defined with switch
457 // ports as the key
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530458 PortCriterion portCriterion = null;
Saurav Das822c4e22015-10-23 10:51:11 -0700459 EthCriterion ethCriterion = null;
460 VlanIdCriterion vidCriterion = null;
Saurav Das822c4e22015-10-23 10:51:11 -0700461 if (!filt.key().equals(Criteria.dummy()) &&
462 filt.key().type() == Criterion.Type.IN_PORT) {
463 portCriterion = (PortCriterion) filt.key();
Saurav Das822c4e22015-10-23 10:51:11 -0700464 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530465 if (portCriterion == null) {
466 log.debug("No IN_PORT defined in filtering objective from app: {}",
467 applicationId);
468 } else {
469 log.debug("Received filtering objective for dev/port: {}/{}", deviceId,
470 portCriterion.port());
471 }
Saurav Das822c4e22015-10-23 10:51:11 -0700472 // convert filtering conditions for switch-intfs into flowrules
473 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
474 for (Criterion criterion : filt.conditions()) {
Yi Tseng0fdc07e2017-04-24 19:17:08 -0700475 switch (criterion.type()) {
476 case ETH_DST:
477 case ETH_DST_MASKED:
478 ethCriterion = (EthCriterion) criterion;
479 break;
480 case VLAN_VID:
481 vidCriterion = (VlanIdCriterion) criterion;
482 break;
483 default:
484 log.warn("Unsupported filter {}", criterion);
485 fail(filt, ObjectiveError.UNSUPPORTED);
486 return;
Saurav Das822c4e22015-10-23 10:51:11 -0700487 }
488 }
489
Saurav Das0e99e2b2015-10-28 12:39:42 -0700490 VlanId assignedVlan = null;
Charles Chane849c192016-01-11 18:28:54 -0800491 if (vidCriterion != null) {
Charles Chand55e84d2016-03-30 17:54:24 -0700492 // Use the VLAN in criterion if metadata is not present and the traffic is tagged
Charles Chanc550f2e2017-12-19 19:55:57 -0800493 if (!vidCriterion.vlanId().equals(VlanId.NONE)) {
Charles Chane849c192016-01-11 18:28:54 -0800494 assignedVlan = vidCriterion.vlanId();
Piere5bff482018-03-07 11:42:50 +0100495 }
496 // If the meta VLAN is present let's update the assigned vlan
497 if (filt.meta() != null) {
498 VlanId metaVlan = readVlanFromTreatment(filt.meta());
499 if (metaVlan != null) {
500 assignedVlan = metaVlan;
501 }
Charles Chand55e84d2016-03-30 17:54:24 -0700502 }
Charles Chane849c192016-01-11 18:28:54 -0800503
Charles Chand55e84d2016-03-30 17:54:24 -0700504 if (assignedVlan == null) {
505 log.error("Driver fails to extract VLAN information. "
Frank Wangd8ab0962017-08-11 11:09:30 +0800506 + "Not processing VLAN filters on device {}.", deviceId);
Charles Chand55e84d2016-03-30 17:54:24 -0700507 log.debug("VLAN ID in criterion={}, metadata={}",
508 readVlanFromTreatment(filt.meta()), vidCriterion.vlanId());
509 fail(filt, ObjectiveError.BADPARAMS);
510 return;
Saurav Das0e99e2b2015-10-28 12:39:42 -0700511 }
512 }
513
Yi Tseng3a77b4f2017-02-06 15:02:17 -0800514 if (ethCriterion == null || ethCriterion.mac().equals(NONE)) {
Charles Chan985b12e2016-05-11 19:47:22 -0700515 // NOTE: it is possible that a filtering objective only has vidCriterion
Daniel Ginsburgc1d47e92018-04-30 19:27:19 -0400516 log.debug("filtering objective missing dstMac, won't program TMAC table");
Saurav Das822c4e22015-10-23 10:51:11 -0700517 } else {
Charles Chan45b69ab2018-03-02 15:41:41 -0800518 MacAddress unicastMac = readEthDstFromTreatment(filt.meta());
Charles Chan66291502018-03-02 16:43:28 -0800519 List<List<FlowRule>> allStages = processEthDstFilter(portCriterion, ethCriterion,
Charles Chan45b69ab2018-03-02 15:41:41 -0800520 vidCriterion, assignedVlan, unicastMac, applicationId);
Charles Chan66291502018-03-02 16:43:28 -0800521 for (List<FlowRule> flowRules : allStages) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800522 log.trace("Starting a new flow rule stage for TMAC table flow");
Charles Chan66291502018-03-02 16:43:28 -0800523 ops.newStage();
Charles Chanc550f2e2017-12-19 19:55:57 -0800524
Charles Chan66291502018-03-02 16:43:28 -0800525 for (FlowRule flowRule : flowRules) {
Andrea Campanellab955fec2018-04-27 14:44:15 +0200526 log.debug(" {} flow rules in TMAC table: {} for dev: {}",
Charles Chan66291502018-03-02 16:43:28 -0800527 (install) ? "adding" : "removing", flowRules, deviceId);
Charles Chan66291502018-03-02 16:43:28 -0800528 if (install) {
529 ops = ops.add(flowRule);
Charles Chanc550f2e2017-12-19 19:55:57 -0800530 } else {
Charles Chan66291502018-03-02 16:43:28 -0800531 // NOTE: Only remove TMAC flow when there is no more enabled port within the
532 // same VLAN on this device if TMAC doesn't support matching on in_port.
533 if (matchInPortTmacTable() || (filt.meta() != null && filt.meta().clearedDeferred())) {
534 ops = ops.remove(flowRule);
535 } else {
536 log.debug("Abort TMAC flow removal on {}. Some other ports still share this TMAC flow");
537 }
Charles Chanc550f2e2017-12-19 19:55:57 -0800538 }
539 }
Saurav Das822c4e22015-10-23 10:51:11 -0700540 }
541 }
542
Charles Chan985b12e2016-05-11 19:47:22 -0700543 if (vidCriterion == null) {
544 // NOTE: it is possible that a filtering objective only has ethCriterion
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530545 log.info("filtering objective missing VLAN, cannot program VLAN Table");
Saurav Das822c4e22015-10-23 10:51:11 -0700546 } else {
Charles Chan66291502018-03-02 16:43:28 -0800547 List<List<FlowRule>> allStages = processVlanIdFilter(
Charles Chan14967c22015-12-07 11:11:50 -0800548 portCriterion, vidCriterion, assignedVlan, applicationId);
Charles Chan66291502018-03-02 16:43:28 -0800549 for (List<FlowRule> flowRules : allStages) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800550 log.trace("Starting a new flow rule stage for VLAN table flow");
Charles Chan66291502018-03-02 16:43:28 -0800551 ops.newStage();
Charles Chan14967c22015-12-07 11:11:50 -0800552
Charles Chan66291502018-03-02 16:43:28 -0800553 for (FlowRule flowRule : flowRules) {
554 log.trace("{} flow rules in VLAN table: {} for dev: {}",
555 (install) ? "adding" : "removing", flowRule, deviceId);
556 ops = install ? ops.add(flowRule) : ops.remove(flowRule);
Charles Chand1172632017-03-15 17:33:09 -0700557 }
Saurav Das822c4e22015-10-23 10:51:11 -0700558 }
559 }
560
Saurav Das822c4e22015-10-23 10:51:11 -0700561 // apply filtering flow rules
562 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
563 @Override
564 public void onSuccess(FlowRuleOperations ops) {
Saurav Das018605f2017-02-18 14:05:44 -0800565 log.debug("Applied {} filtering rules in device {}",
Saurav Das822c4e22015-10-23 10:51:11 -0700566 ops.stages().get(0).size(), deviceId);
567 pass(filt);
568 }
569
570 @Override
571 public void onError(FlowRuleOperations ops) {
572 log.info("Failed to apply all filtering rules in dev {}", deviceId);
573 fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED);
574 }
575 }));
576
577 }
578
579 /**
Charles Chand1172632017-03-15 17:33:09 -0700580 * Internal implementation of processVlanIdFilter.
581 * <p>
582 * The is_present bit in set_vlan_vid action is required to be 0 in OFDPA i12.
583 * Since it is non-OF spec, we need an extension treatment for that.
584 * The useVlanExtension must be set to false for OFDPA i12.
585 * </p>
Charles Chan66291502018-03-02 16:43:28 -0800586 * <p>
587 * NOTE: Separate VLAN filtering rules and assignment rules
588 * into different stages in order to guarantee that filtering rules
589 * always go first, as required by OFDPA.
590 * </p>
Saurav Das0e99e2b2015-10-28 12:39:42 -0700591 *
Charles Chanf9e98652016-09-07 16:54:23 -0700592 * @param portCriterion port on device for which this filter is programmed
593 * @param vidCriterion vlan assigned to port, or NONE for untagged
594 * @param assignedVlan assigned vlan-id for untagged packets
595 * @param applicationId for application programming this filter
Charles Chan66291502018-03-02 16:43:28 -0800596 * @return stages of flow rules for port-vlan filters
Saurav Das822c4e22015-10-23 10:51:11 -0700597 */
Charles Chan66291502018-03-02 16:43:28 -0800598 protected List<List<FlowRule>> processVlanIdFilter(PortCriterion portCriterion,
Yi Tsengef19de12017-04-24 11:33:05 -0700599 VlanIdCriterion vidCriterion,
600 VlanId assignedVlan,
601 ApplicationId applicationId) {
Charles Chan66291502018-03-02 16:43:28 -0800602 List<FlowRule> filteringRules = new ArrayList<>();
603 List<FlowRule> assignmentRules = new ArrayList<>();
Saurav Das822c4e22015-10-23 10:51:11 -0700604 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
605 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800606 TrafficSelector.Builder preSelector = null;
607 TrafficTreatment.Builder preTreatment = null;
608
Saurav Das4f980082015-11-05 13:39:15 -0800609 treatment.transition(TMAC_TABLE);
610
Saurav Das822c4e22015-10-23 10:51:11 -0700611 if (vidCriterion.vlanId() == VlanId.NONE) {
612 // untagged packets are assigned vlans
Charles Chand1172632017-03-15 17:33:09 -0700613 preSelector = DefaultTrafficSelector.builder();
614 if (requireVlanExtensions()) {
615 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(VlanId.NONE);
616 selector.extension(ofdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700617 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
618 treatment.extension(ofdpaSetVlanVid, deviceId);
Charles Chand1172632017-03-15 17:33:09 -0700619
620 OfdpaMatchVlanVid preOfdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
621 preSelector.extension(preOfdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700622 } else {
Charles Chand1172632017-03-15 17:33:09 -0700623 selector.matchVlanId(VlanId.NONE);
Charles Chanf9e98652016-09-07 16:54:23 -0700624 treatment.setVlanId(assignedVlan);
Charles Chand1172632017-03-15 17:33:09 -0700625
626 preSelector.matchVlanId(assignedVlan);
627 }
628 preTreatment = DefaultTrafficTreatment.builder().transition(TMAC_TABLE);
629 } else {
630 if (requireVlanExtensions()) {
631 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
632 selector.extension(ofdpaMatchVlanVid, deviceId);
633 } else {
634 selector.matchVlanId(vidCriterion.vlanId());
Charles Chanf9e98652016-09-07 16:54:23 -0700635 }
Charles Chan14967c22015-12-07 11:11:50 -0800636
Charles Chand55e84d2016-03-30 17:54:24 -0700637 if (!assignedVlan.equals(vidCriterion.vlanId())) {
Charles Chand1172632017-03-15 17:33:09 -0700638 if (requireVlanExtensions()) {
Charles Chanc03782d2017-01-31 13:57:55 -0800639 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
640 treatment.extension(ofdpaSetVlanVid, deviceId);
641 } else {
642 treatment.setVlanId(assignedVlan);
643 }
Charles Chand55e84d2016-03-30 17:54:24 -0700644 }
Saurav Das822c4e22015-10-23 10:51:11 -0700645 }
Saurav Das822c4e22015-10-23 10:51:11 -0700646
647 // ofdpa cannot match on ALL portnumber, so we need to use separate
648 // rules for each port.
Charles Chan14967c22015-12-07 11:11:50 -0800649 List<PortNumber> portnums = new ArrayList<>();
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530650 if (portCriterion != null) {
651 if (PortNumber.ALL.equals(portCriterion.port())) {
652 for (Port port : deviceService.getPorts(deviceId)) {
653 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
654 portnums.add(port.number());
655 }
Saurav Das822c4e22015-10-23 10:51:11 -0700656 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530657 } else {
658 portnums.add(portCriterion.port());
Saurav Das822c4e22015-10-23 10:51:11 -0700659 }
660 } else {
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530661 log.warn("Filtering Objective missing Port Criterion . " +
Charles Chan66291502018-03-02 16:43:28 -0800662 "VLAN Table cannot be programmed for {}", deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700663 }
Saurav Das4f980082015-11-05 13:39:15 -0800664
Saurav Das822c4e22015-10-23 10:51:11 -0700665 for (PortNumber pnum : portnums) {
Saurav Das4f980082015-11-05 13:39:15 -0800666 // create rest of flowrule
Saurav Das822c4e22015-10-23 10:51:11 -0700667 selector.matchInPort(pnum);
668 FlowRule rule = DefaultFlowRule.builder()
669 .forDevice(deviceId)
670 .withSelector(selector.build())
671 .withTreatment(treatment.build())
672 .withPriority(DEFAULT_PRIORITY)
673 .fromApp(applicationId)
674 .makePermanent()
675 .forTable(VLAN_TABLE).build();
Charles Chan66291502018-03-02 16:43:28 -0800676 assignmentRules.add(rule);
Charles Chan14967c22015-12-07 11:11:50 -0800677
678 if (preSelector != null) {
679 preSelector.matchInPort(pnum);
680 FlowRule preRule = DefaultFlowRule.builder()
681 .forDevice(deviceId)
682 .withSelector(preSelector.build())
683 .withTreatment(preTreatment.build())
684 .withPriority(DEFAULT_PRIORITY)
685 .fromApp(applicationId)
686 .makePermanent()
687 .forTable(VLAN_TABLE).build();
Charles Chan66291502018-03-02 16:43:28 -0800688 filteringRules.add(preRule);
Charles Chan14967c22015-12-07 11:11:50 -0800689 }
Saurav Das822c4e22015-10-23 10:51:11 -0700690 }
Charles Chan2686dd72018-03-06 22:10:15 -0800691 return ImmutableList.of(filteringRules, assignmentRules);
Saurav Das822c4e22015-10-23 10:51:11 -0700692 }
693
694 /**
695 * Allows routed packets with correct destination MAC to be directed
Charles Chan45b69ab2018-03-02 15:41:41 -0800696 * to unicast routing table, multicast routing table or MPLS forwarding table.
Saurav Das822c4e22015-10-23 10:51:11 -0700697 *
698 * @param portCriterion port on device for which this filter is programmed
699 * @param ethCriterion dstMac of device for which is filter is programmed
700 * @param vidCriterion vlan assigned to port, or NONE for untagged
Saurav Das0e99e2b2015-10-28 12:39:42 -0700701 * @param assignedVlan assigned vlan-id for untagged packets
Charles Chan45b69ab2018-03-02 15:41:41 -0800702 * @param unicastMac some switches require a unicast TMAC flow to be programmed before multicast
703 * TMAC flow. This MAC address will be used for the unicast TMAC flow.
704 * This is unused if the filtering objective is a unicast.
Saurav Das822c4e22015-10-23 10:51:11 -0700705 * @param applicationId for application programming this filter
Charles Chan66291502018-03-02 16:43:28 -0800706 * @return stages of flow rules for port-vlan filters
Saurav Das822c4e22015-10-23 10:51:11 -0700707
708 */
Charles Chan66291502018-03-02 16:43:28 -0800709 protected List<List<FlowRule>> processEthDstFilter(PortCriterion portCriterion,
Saurav Das822c4e22015-10-23 10:51:11 -0700710 EthCriterion ethCriterion,
711 VlanIdCriterion vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700712 VlanId assignedVlan,
Charles Chan45b69ab2018-03-02 15:41:41 -0800713 MacAddress unicastMac,
Saurav Das822c4e22015-10-23 10:51:11 -0700714 ApplicationId applicationId) {
Charles Chan5270ed02016-01-30 23:22:37 -0800715 // Consider PortNumber.ANY as wildcard. Match ETH_DST only
Sivachidambaram Subramanianc598d062017-05-16 22:42:06 +0530716 if (portCriterion != null && PortNumber.ANY.equals(portCriterion.port())) {
Charles Chan5270ed02016-01-30 23:22:37 -0800717 return processEthDstOnlyFilter(ethCriterion, applicationId);
718 }
719
Charles Chan5b9df8d2016-03-28 22:21:40 -0700720 // Multicast MAC
721 if (ethCriterion.mask() != null) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800722 return processMcastEthDstFilter(ethCriterion, assignedVlan, unicastMac, applicationId);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700723 }
724
Saurav Das822c4e22015-10-23 10:51:11 -0700725 //handling untagged packets via assigned VLAN
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530726 if (vidCriterion != null && vidCriterion.vlanId() == VlanId.NONE) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700727 vidCriterion = (VlanIdCriterion) Criteria.matchVlanId(assignedVlan);
Saurav Das822c4e22015-10-23 10:51:11 -0700728 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530729 List<FlowRule> rules = new ArrayList<>();
730 OfdpaMatchVlanVid ofdpaMatchVlanVid = null;
731 if (vidCriterion != null && requireVlanExtensions()) {
732 ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
733 }
Saurav Das822c4e22015-10-23 10:51:11 -0700734 // ofdpa cannot match on ALL portnumber, so we need to use separate
735 // rules for each port.
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -0700736 List<PortNumber> portnums = new ArrayList<>();
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530737 if (portCriterion != null) {
738 if (PortNumber.ALL.equals(portCriterion.port())) {
739 for (Port port : deviceService.getPorts(deviceId)) {
740 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
741 portnums.add(port.number());
742 }
Saurav Das822c4e22015-10-23 10:51:11 -0700743 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530744 } else {
745 portnums.add(portCriterion.port());
746 }
747 for (PortNumber pnum : portnums) {
748 rules.add(buildTmacRuleForIpv4(ethCriterion,
749 vidCriterion,
750 ofdpaMatchVlanVid,
751 applicationId,
752 pnum));
753 rules.add(buildTmacRuleForMpls(ethCriterion,
754 vidCriterion,
755 ofdpaMatchVlanVid,
756 applicationId,
757 pnum));
758 rules.add(buildTmacRuleForIpv6(ethCriterion,
759 vidCriterion,
760 ofdpaMatchVlanVid,
761 applicationId,
762 pnum));
Saurav Das822c4e22015-10-23 10:51:11 -0700763 }
764 } else {
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530765 rules.add(buildTmacRuleForIpv4(ethCriterion,
766 vidCriterion,
767 ofdpaMatchVlanVid,
768 applicationId,
769 null));
770 rules.add(buildTmacRuleForMpls(ethCriterion,
771 vidCriterion,
772 ofdpaMatchVlanVid,
773 applicationId,
774 null));
775 rules.add(buildTmacRuleForIpv6(ethCriterion,
776 vidCriterion,
777 ofdpaMatchVlanVid,
778 applicationId,
779 null));
Saurav Das822c4e22015-10-23 10:51:11 -0700780 }
Charles Chan66291502018-03-02 16:43:28 -0800781 return ImmutableList.of(rules);
Saurav Das822c4e22015-10-23 10:51:11 -0700782 }
783
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530784 /**
785 * Builds TMAC rules for IPv4 packets.
786 *
Charles Chanc550f2e2017-12-19 19:55:57 -0800787 * @param ethCriterion dst mac matching
788 * @param vidCriterion vlan id assigned to the port
789 * @param ofdpaMatchVlanVid OFDPA vlan id matching
790 * @param applicationId application id
791 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530792 * @return TMAC rule for IPV4 packets
793 */
794 private FlowRule buildTmacRuleForIpv4(EthCriterion ethCriterion,
795 VlanIdCriterion vidCriterion,
796 OfdpaMatchVlanVid ofdpaMatchVlanVid,
797 ApplicationId applicationId,
798 PortNumber pnum) {
799 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
800 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
801 if (pnum != null) {
802 if (matchInPortTmacTable()) {
803 selector.matchInPort(pnum);
804 } else {
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800805 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530806 "ignoring the IN_PORT criteria");
807 }
808 }
809 if (vidCriterion != null) {
810 if (requireVlanExtensions()) {
811 selector.extension(ofdpaMatchVlanVid, deviceId);
812 } else {
813 selector.matchVlanId(vidCriterion.vlanId());
814 }
815 }
816 selector.matchEthType(Ethernet.TYPE_IPV4);
817 selector.matchEthDst(ethCriterion.mac());
818 treatment.transition(UNICAST_ROUTING_TABLE);
819 return DefaultFlowRule.builder()
820 .forDevice(deviceId)
821 .withSelector(selector.build())
822 .withTreatment(treatment.build())
823 .withPriority(DEFAULT_PRIORITY)
824 .fromApp(applicationId)
825 .makePermanent()
826 .forTable(TMAC_TABLE).build();
827 }
828
829 /**
830 * Builds TMAC rule for MPLS packets.
831 *
Charles Chanc550f2e2017-12-19 19:55:57 -0800832 * @param ethCriterion dst mac matching
833 * @param vidCriterion vlan id assigned to the port
834 * @param ofdpaMatchVlanVid OFDPA vlan id matching
835 * @param applicationId application id
836 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530837 * @return TMAC rule for MPLS packets
838 */
839 private FlowRule buildTmacRuleForMpls(EthCriterion ethCriterion,
840 VlanIdCriterion vidCriterion,
841 OfdpaMatchVlanVid ofdpaMatchVlanVid,
842 ApplicationId applicationId,
843 PortNumber pnum) {
844 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
845 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
846 if (pnum != null) {
847 if (matchInPortTmacTable()) {
848 selector.matchInPort(pnum);
849 } else {
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800850 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530851 "ignoring the IN_PORT criteria");
852 }
853 }
854 if (vidCriterion != null) {
855 if (requireVlanExtensions()) {
856 selector.extension(ofdpaMatchVlanVid, deviceId);
857 } else {
858 selector.matchVlanId(vidCriterion.vlanId());
859 }
860 }
861 selector.matchEthType(Ethernet.MPLS_UNICAST);
862 selector.matchEthDst(ethCriterion.mac());
863 treatment.transition(MPLS_TABLE_0);
864 return DefaultFlowRule.builder()
865 .forDevice(deviceId)
866 .withSelector(selector.build())
867 .withTreatment(treatment.build())
868 .withPriority(DEFAULT_PRIORITY)
869 .fromApp(applicationId)
870 .makePermanent()
871 .forTable(TMAC_TABLE).build();
872 }
873
874 /**
875 * Builds TMAC rules for IPv6 packets.
876 *
Charles Chanc550f2e2017-12-19 19:55:57 -0800877 * @param ethCriterion dst mac matching
878 * @param vidCriterion vlan id assigned to the port
879 * @param ofdpaMatchVlanVid OFDPA vlan id matching
880 * @param applicationId application id
881 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530882 * @return TMAC rule for IPV6 packets
883 */
884 private FlowRule buildTmacRuleForIpv6(EthCriterion ethCriterion,
885 VlanIdCriterion vidCriterion,
886 OfdpaMatchVlanVid ofdpaMatchVlanVid,
887 ApplicationId applicationId,
888 PortNumber pnum) {
889 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
890 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
891 if (pnum != null) {
892 if (matchInPortTmacTable()) {
893 selector.matchInPort(pnum);
894 } else {
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800895 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530896 "ignoring the IN_PORT criteria");
897 }
898 }
899 if (vidCriterion != null) {
900 if (requireVlanExtensions()) {
901 selector.extension(ofdpaMatchVlanVid, deviceId);
902 } else {
903 selector.matchVlanId(vidCriterion.vlanId());
904 }
905 }
906 selector.matchEthType(Ethernet.TYPE_IPV6);
907 selector.matchEthDst(ethCriterion.mac());
908 treatment.transition(UNICAST_ROUTING_TABLE);
909 return DefaultFlowRule.builder()
910 .forDevice(deviceId)
911 .withSelector(selector.build())
912 .withTreatment(treatment.build())
913 .withPriority(DEFAULT_PRIORITY)
914 .fromApp(applicationId)
915 .makePermanent()
916 .forTable(TMAC_TABLE).build();
917 }
918
Charles Chan66291502018-03-02 16:43:28 -0800919 protected List<List<FlowRule>> processEthDstOnlyFilter(EthCriterion ethCriterion,
Yi Tsengef19de12017-04-24 11:33:05 -0700920 ApplicationId applicationId) {
Pier Luigi0e358632017-01-31 09:35:05 -0800921 ImmutableList.Builder<FlowRule> builder = ImmutableList.builder();
922
Charles Chan5270ed02016-01-30 23:22:37 -0800923 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
924 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
925 selector.matchEthType(Ethernet.TYPE_IPV4);
926 selector.matchEthDst(ethCriterion.mac());
927 treatment.transition(UNICAST_ROUTING_TABLE);
928 FlowRule rule = DefaultFlowRule.builder()
929 .forDevice(deviceId)
930 .withSelector(selector.build())
931 .withTreatment(treatment.build())
932 .withPriority(DEFAULT_PRIORITY)
933 .fromApp(applicationId)
934 .makePermanent()
935 .forTable(TMAC_TABLE).build();
Pier Luigi0e358632017-01-31 09:35:05 -0800936 builder.add(rule);
937
938 selector = DefaultTrafficSelector.builder();
939 treatment = DefaultTrafficTreatment.builder();
940 selector.matchEthType(Ethernet.TYPE_IPV6);
941 selector.matchEthDst(ethCriterion.mac());
942 treatment.transition(UNICAST_ROUTING_TABLE);
943 rule = DefaultFlowRule.builder()
944 .forDevice(deviceId)
945 .withSelector(selector.build())
946 .withTreatment(treatment.build())
947 .withPriority(DEFAULT_PRIORITY)
948 .fromApp(applicationId)
949 .makePermanent()
950 .forTable(TMAC_TABLE).build();
Charles Chan66291502018-03-02 16:43:28 -0800951 return ImmutableList.of(builder.add(rule).build());
Charles Chan5270ed02016-01-30 23:22:37 -0800952 }
953
Charles Chan66291502018-03-02 16:43:28 -0800954 List<List<FlowRule>> processMcastEthDstFilter(EthCriterion ethCriterion,
Charles Chanb4879a52017-10-20 19:09:16 -0700955 VlanId assignedVlan,
Charles Chan45b69ab2018-03-02 15:41:41 -0800956 MacAddress unicastMac,
Yi Tsengef19de12017-04-24 11:33:05 -0700957 ApplicationId applicationId) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800958 ImmutableList.Builder<FlowRule> unicastFlows = ImmutableList.builder();
959 ImmutableList.Builder<FlowRule> multicastFlows = ImmutableList.builder();
960 TrafficSelector.Builder selector;
961 TrafficTreatment.Builder treatment;
Charles Chanb4879a52017-10-20 19:09:16 -0700962 FlowRule rule;
Julia Ferguson65428c32017-08-10 18:15:24 +0000963
Andrea Campanellab955fec2018-04-27 14:44:15 +0200964 int priority = (assignedVlan == null || assignedVlan == VlanId.ANY) ? MIDDLE_PRIORITY : DEFAULT_PRIORITY;
965
Charles Chanb4879a52017-10-20 19:09:16 -0700966 if (IPV4_MULTICAST.equals(ethCriterion.mac())) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800967 if (requireUnicastBeforeMulticast()) {
968 selector = DefaultTrafficSelector.builder();
969 treatment = DefaultTrafficTreatment.builder();
970 selector.matchEthType(Ethernet.TYPE_IPV4);
971 selector.matchEthDst(unicastMac);
Andrea Campanellab955fec2018-04-27 14:44:15 +0200972 if (assignedVlan != null) {
973 selector.matchVlanId(assignedVlan);
974 }
Charles Chan45b69ab2018-03-02 15:41:41 -0800975 treatment.transition(UNICAST_ROUTING_TABLE);
976 rule = DefaultFlowRule.builder()
977 .forDevice(deviceId)
978 .withSelector(selector.build())
979 .withTreatment(treatment.build())
Andrea Campanellab955fec2018-04-27 14:44:15 +0200980 .withPriority(priority)
Charles Chan45b69ab2018-03-02 15:41:41 -0800981 .fromApp(applicationId)
982 .makePermanent()
983 .forTable(TMAC_TABLE).build();
984 unicastFlows.add(rule);
985 }
986
987 selector = DefaultTrafficSelector.builder();
988 treatment = DefaultTrafficTreatment.builder();
Charles Chanb4879a52017-10-20 19:09:16 -0700989 selector.matchEthType(Ethernet.TYPE_IPV4);
990 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
Andrea Campanellab955fec2018-04-27 14:44:15 +0200991 if (assignedVlan != null) {
992 selector.matchVlanId(assignedVlan);
993 }
Charles Chanb4879a52017-10-20 19:09:16 -0700994 treatment.transition(MULTICAST_ROUTING_TABLE);
995 rule = DefaultFlowRule.builder()
996 .forDevice(deviceId)
997 .withSelector(selector.build())
998 .withTreatment(treatment.build())
Andrea Campanellab955fec2018-04-27 14:44:15 +0200999 .withPriority(priority)
Charles Chanb4879a52017-10-20 19:09:16 -07001000 .fromApp(applicationId)
1001 .makePermanent()
1002 .forTable(TMAC_TABLE).build();
Charles Chan45b69ab2018-03-02 15:41:41 -08001003 multicastFlows.add(rule);
Charles Chanb4879a52017-10-20 19:09:16 -07001004 }
1005
1006 if (IPV6_MULTICAST.equals(ethCriterion.mac())) {
Charles Chan45b69ab2018-03-02 15:41:41 -08001007 if (requireUnicastBeforeMulticast()) {
1008 selector = DefaultTrafficSelector.builder();
1009 treatment = DefaultTrafficTreatment.builder();
1010 selector.matchEthType(Ethernet.TYPE_IPV6);
1011 selector.matchEthDst(unicastMac);
Andrea Campanellab955fec2018-04-27 14:44:15 +02001012 if (assignedVlan != null) {
1013 selector.matchVlanId(assignedVlan);
1014 }
Charles Chan45b69ab2018-03-02 15:41:41 -08001015 treatment.transition(UNICAST_ROUTING_TABLE);
1016 rule = DefaultFlowRule.builder()
1017 .forDevice(deviceId)
1018 .withSelector(selector.build())
1019 .withTreatment(treatment.build())
Andrea Campanellab955fec2018-04-27 14:44:15 +02001020 .withPriority(priority)
Charles Chan45b69ab2018-03-02 15:41:41 -08001021 .fromApp(applicationId)
1022 .makePermanent()
1023 .forTable(TMAC_TABLE).build();
1024 unicastFlows.add(rule);
1025 }
1026
Charles Chanb4879a52017-10-20 19:09:16 -07001027 selector = DefaultTrafficSelector.builder();
1028 treatment = DefaultTrafficTreatment.builder();
1029 selector.matchEthType(Ethernet.TYPE_IPV6);
1030 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
Andrea Campanellab955fec2018-04-27 14:44:15 +02001031 if (assignedVlan != null) {
1032 selector.matchVlanId(assignedVlan);
1033 }
Charles Chanb4879a52017-10-20 19:09:16 -07001034 treatment.transition(MULTICAST_ROUTING_TABLE);
1035 rule = DefaultFlowRule.builder()
1036 .forDevice(deviceId)
1037 .withSelector(selector.build())
1038 .withTreatment(treatment.build())
Andrea Campanellab955fec2018-04-27 14:44:15 +02001039 .withPriority(priority)
Charles Chanb4879a52017-10-20 19:09:16 -07001040 .fromApp(applicationId)
1041 .makePermanent()
1042 .forTable(TMAC_TABLE).build();
Charles Chan45b69ab2018-03-02 15:41:41 -08001043 multicastFlows.add(rule);
Charles Chanb4879a52017-10-20 19:09:16 -07001044 }
Charles Chan45b69ab2018-03-02 15:41:41 -08001045 return ImmutableList.of(unicastFlows.build(), multicastFlows.build());
Charles Chan5b9df8d2016-03-28 22:21:40 -07001046 }
1047
Saurav Das822c4e22015-10-23 10:51:11 -07001048 private Collection<FlowRule> processForward(ForwardingObjective fwd) {
1049 switch (fwd.flag()) {
1050 case SPECIFIC:
1051 return processSpecific(fwd);
1052 case VERSATILE:
1053 return processVersatile(fwd);
Jonghwan Hyun800d9d02018-04-09 09:40:50 -07001054 case EGRESS:
1055 return processEgress(fwd);
Saurav Das822c4e22015-10-23 10:51:11 -07001056 default:
1057 fail(fwd, ObjectiveError.UNKNOWN);
1058 log.warn("Unknown forwarding flag {}", fwd.flag());
1059 }
1060 return Collections.emptySet();
1061 }
1062
1063 /**
Jonghwan Hyun800d9d02018-04-09 09:40:50 -07001064 * In the OF-DPA 2.0 pipeline, egress forwarding objectives go to the
1065 * egress tables.
1066 * @param fwd the forwarding objective of type 'egress'
1067 * @return a collection of flow rules to be sent to the switch. An empty
1068 * collection may be returned if there is a problem in processing
1069 * the flow rule
1070 */
1071 protected Collection<FlowRule> processEgress(ForwardingObjective fwd) {
1072 log.debug("Processing egress forwarding objective:{} in dev:{}",
1073 fwd, deviceId);
1074
1075 List<FlowRule> rules = new ArrayList<>();
1076
1077 // Build selector
1078 TrafficSelector.Builder sb = DefaultTrafficSelector.builder();
1079 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) fwd.selector().getCriterion(Criterion.Type.VLAN_VID);
1080 if (vlanIdCriterion == null) {
1081 log.error("Egress forwarding objective:{} must include vlanId", fwd.id());
1082 fail(fwd, ObjectiveError.BADPARAMS);
1083 return rules;
1084 }
1085
1086 Optional<Instruction> outInstr = fwd.treatment().allInstructions().stream()
1087 .filter(instruction -> instruction instanceof OutputInstruction).findFirst();
1088 if (!outInstr.isPresent()) {
1089 log.error("Egress forwarding objective:{} must include output port", fwd.id());
1090 fail(fwd, ObjectiveError.BADPARAMS);
1091 return rules;
1092 }
1093
1094 PortNumber portNumber = ((OutputInstruction) outInstr.get()).port();
1095
1096 sb.matchVlanId(vlanIdCriterion.vlanId());
1097 OfdpaMatchActsetOutput actsetOutput = new OfdpaMatchActsetOutput(portNumber);
1098 sb.extension(actsetOutput, deviceId);
1099
1100 sb.extension(new OfdpaMatchAllowVlanTranslation(ALLOW_VLAN_TRANSLATION), deviceId);
1101
1102 // Build a flow rule for Egress VLAN Flow table
1103 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
1104 tb.transition(EGRESS_DSCP_PCP_REMARK_FLOW_TABLE);
1105 if (fwd.treatment() != null) {
1106 for (Instruction instr : fwd.treatment().allInstructions()) {
1107 if (instr instanceof L2ModificationInstruction &&
1108 ((L2ModificationInstruction) instr).subtype() == L2SubType.VLAN_ID) {
1109 tb.immediate().add(instr);
1110 }
1111 if (instr instanceof L2ModificationInstruction &&
1112 ((L2ModificationInstruction) instr).subtype() == L2SubType.VLAN_PUSH) {
1113 tb.immediate().pushVlan();
1114 EthType ethType = ((L2ModificationInstruction.ModVlanHeaderInstruction) instr).ethernetType();
1115 if (ethType.equals(EtherType.QINQ.ethType())) {
1116 // Build a flow rule for Egress TPID Flow table
1117 TrafficSelector tpidSelector = DefaultTrafficSelector.builder()
1118 .extension(actsetOutput, deviceId)
1119 .matchVlanId(VlanId.ANY).build();
1120
1121 TrafficTreatment tpidTreatment = DefaultTrafficTreatment.builder()
1122 .extension(new Ofdpa3CopyField(COPY_FIELD_NBITS, COPY_FIELD_OFFSET,
1123 COPY_FIELD_OFFSET, OXM_ID_VLAN_VID,
1124 OXM_ID_PACKET_REG_1),
1125 deviceId)
1126 .popVlan()
1127 .pushVlan(EtherType.QINQ.ethType())
1128 .extension(new Ofdpa3CopyField(COPY_FIELD_NBITS, COPY_FIELD_OFFSET,
1129 COPY_FIELD_OFFSET, OXM_ID_PACKET_REG_1,
1130 OXM_ID_VLAN_VID),
1131 deviceId)
1132 .build();
1133
1134 FlowRule.Builder tpidRuleBuilder = DefaultFlowRule.builder()
1135 .fromApp(fwd.appId())
1136 .withPriority(fwd.priority())
1137 .forDevice(deviceId)
1138 .withSelector(tpidSelector)
1139 .withTreatment(tpidTreatment)
1140 .makePermanent()
1141 .forTable(EGRESS_TPID_FLOW_TABLE);
1142 rules.add(tpidRuleBuilder.build());
1143 }
1144 }
1145 }
1146 }
1147
1148 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1149 .fromApp(fwd.appId())
1150 .withPriority(fwd.priority())
1151 .forDevice(deviceId)
1152 .withSelector(sb.build())
1153 .withTreatment(tb.build())
1154 .makePermanent()
1155 .forTable(EGRESS_VLAN_FLOW_TABLE);
1156 rules.add(ruleBuilder.build());
1157 return rules;
1158 }
1159
1160 /**
Saurav Das822c4e22015-10-23 10:51:11 -07001161 * In the OF-DPA 2.0 pipeline, versatile forwarding objectives go to the
1162 * ACL table.
1163 * @param fwd the forwarding objective of type 'versatile'
1164 * @return a collection of flow rules to be sent to the switch. An empty
1165 * collection may be returned if there is a problem in processing
1166 * the flow rule
1167 */
Saurav Das52025962016-01-28 22:30:01 -08001168 protected Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
Saurav Das018605f2017-02-18 14:05:44 -08001169 log.debug("Processing versatile forwarding objective:{} in dev:{}",
Saurav Dasd2fded02016-12-02 15:43:47 -08001170 fwd.id(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001171
1172 EthTypeCriterion ethType =
Saurav Das77b5e902016-01-27 17:01:59 -08001173 (EthTypeCriterion) fwd.selector().getCriterion(Criterion.Type.ETH_TYPE);
Saurav Das822c4e22015-10-23 10:51:11 -07001174 if (ethType == null) {
Saurav Dasd2fded02016-12-02 15:43:47 -08001175 log.error("Versatile forwarding objective:{} must include ethType",
1176 fwd.id());
Saurav Das822c4e22015-10-23 10:51:11 -07001177 fail(fwd, ObjectiveError.BADPARAMS);
1178 return Collections.emptySet();
1179 }
1180 if (fwd.nextId() == null && fwd.treatment() == null) {
1181 log.error("Forwarding objective {} from {} must contain "
1182 + "nextId or Treatment", fwd.selector(), fwd.appId());
Zsolt Haraszti9faab752016-02-17 15:55:20 -08001183 fail(fwd, ObjectiveError.BADPARAMS);
Saurav Das822c4e22015-10-23 10:51:11 -07001184 return Collections.emptySet();
1185 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001186
Saurav Das77b5e902016-01-27 17:01:59 -08001187 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
1188 fwd.selector().criteria().forEach(criterion -> {
1189 if (criterion instanceof VlanIdCriterion) {
1190 VlanId vlanId = ((VlanIdCriterion) criterion).vlanId();
1191 // ensure that match does not include vlan = NONE as OF-DPA does not
1192 // match untagged packets this way in the ACL table.
1193 if (vlanId.equals(VlanId.NONE)) {
1194 return;
1195 }
Charles Chand1172632017-03-15 17:33:09 -07001196 if (requireVlanExtensions()) {
1197 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanId);
1198 sbuilder.extension(ofdpaMatchVlanVid, deviceId);
1199 } else {
1200 sbuilder.matchVlanId(vlanId);
1201 }
Charles Chan09bf2692018-01-11 11:48:18 -08001202 } else if (criterion instanceof Icmpv6TypeCriterion) {
1203 byte icmpv6Type = (byte) ((Icmpv6TypeCriterion) criterion).icmpv6Type();
1204 sbuilder.matchIcmpv6Type(icmpv6Type);
1205 } else if (criterion instanceof Icmpv6CodeCriterion) {
1206 byte icmpv6Code = (byte) ((Icmpv6CodeCriterion) criterion).icmpv6Code();
1207 sbuilder.matchIcmpv6Type(icmpv6Code);
Charles Chand9e47c62017-10-05 15:17:15 -07001208 } else if (criterion instanceof TcpPortCriterion || criterion instanceof UdpPortCriterion) {
1209 // FIXME: QMX switches do not support L4 dst port matching in ACL table.
1210 // Currently L4 dst port matching is only used by DHCP relay feature
1211 // and therefore is safe to be replaced with L4 src port matching.
1212 // We need to revisit this if L4 dst port is used for other purpose in the future.
1213 if (!supportIpv6L4Dst() && isIpv6(fwd.selector())) {
Charles Chand9e47c62017-10-05 15:17:15 -07001214 switch (criterion.type()) {
1215 case UDP_DST:
Charles Chand9e47c62017-10-05 15:17:15 -07001216 case UDP_DST_MASKED:
Charles Chand9e47c62017-10-05 15:17:15 -07001217 case TCP_DST:
Charles Chand9e47c62017-10-05 15:17:15 -07001218 case TCP_DST_MASKED:
Charles Chand9e47c62017-10-05 15:17:15 -07001219 break;
1220 default:
1221 sbuilder.add(criterion);
1222 }
1223 } else {
1224 sbuilder.add(criterion);
1225 }
Saurav Das77b5e902016-01-27 17:01:59 -08001226 } else {
1227 sbuilder.add(criterion);
1228 }
1229 });
1230
Saurav Das822c4e22015-10-23 10:51:11 -07001231 // XXX driver does not currently do type checking as per Tables 65-67 in
1232 // OFDPA 2.0 spec. The only allowed treatment is a punt to the controller.
Saurav Das49cb5a12016-01-16 22:54:07 -08001233 TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder();
1234 if (fwd.treatment() != null) {
1235 for (Instruction ins : fwd.treatment().allInstructions()) {
1236 if (ins instanceof OutputInstruction) {
1237 OutputInstruction o = (OutputInstruction) ins;
Ray Milkeyfe0e0852018-01-18 11:14:05 -08001238 if (PortNumber.CONTROLLER.equals(o.port())) {
Saurav Das49cb5a12016-01-16 22:54:07 -08001239 ttBuilder.add(o);
1240 } else {
1241 log.warn("Only allowed treatments in versatile forwarding "
1242 + "objectives are punts to the controller");
1243 }
1244 } else {
1245 log.warn("Cannot process instruction in versatile fwd {}", ins);
1246 }
Saurav Das822c4e22015-10-23 10:51:11 -07001247 }
Charles Chan2df0e8a2017-01-09 11:45:08 -08001248 if (fwd.treatment().clearedDeferred()) {
1249 ttBuilder.wipeDeferred();
1250 }
Saurav Das822c4e22015-10-23 10:51:11 -07001251 }
Saurav Das822c4e22015-10-23 10:51:11 -07001252 if (fwd.nextId() != null) {
Saurav Das49cb5a12016-01-16 22:54:07 -08001253 // overide case
1254 NextGroup next = getGroupForNextObjective(fwd.nextId());
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +05301255 if (next == null) {
1256 fail(fwd, ObjectiveError.BADPARAMS);
1257 return Collections.emptySet();
1258 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001259 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1260 // we only need the top level group's key to point the flow to it
1261 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1262 if (group == null) {
1263 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1264 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1265 fail(fwd, ObjectiveError.GROUPMISSING);
1266 return Collections.emptySet();
1267 }
1268 ttBuilder.deferred().group(group.id());
Saurav Das822c4e22015-10-23 10:51:11 -07001269 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001270
1271 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1272 .fromApp(fwd.appId())
1273 .withPriority(fwd.priority())
1274 .forDevice(deviceId)
Saurav Das77b5e902016-01-27 17:01:59 -08001275 .withSelector(sbuilder.build())
Saurav Das49cb5a12016-01-16 22:54:07 -08001276 .withTreatment(ttBuilder.build())
1277 .makePermanent()
1278 .forTable(ACL_TABLE);
1279 return Collections.singletonList(ruleBuilder.build());
Saurav Das822c4e22015-10-23 10:51:11 -07001280 }
1281
1282 /**
1283 * In the OF-DPA 2.0 pipeline, specific forwarding refers to the IP table
Saurav Das8a0732e2015-11-20 15:27:53 -08001284 * (unicast or multicast) or the L2 table (mac + vlan) or the MPLS table.
Saurav Das822c4e22015-10-23 10:51:11 -07001285 *
1286 * @param fwd the forwarding objective of type 'specific'
1287 * @return a collection of flow rules. Typically there will be only one
1288 * for this type of forwarding objective. An empty set may be
1289 * returned if there is an issue in processing the objective.
1290 */
Charles Chan50d900c2018-03-02 13:26:22 -08001291 private Collection<FlowRule> processSpecific(ForwardingObjective fwd) {
Saurav Das25190812016-05-27 13:54:07 -07001292 log.debug("Processing specific fwd objective:{} in dev:{} with next:{}",
Saurav Das4ce45962015-11-24 23:21:05 -08001293 fwd.id(), deviceId, fwd.nextId());
1294 boolean isEthTypeObj = isSupportedEthTypeObjective(fwd);
1295 boolean isEthDstObj = isSupportedEthDstObjective(fwd);
1296
1297 if (isEthTypeObj) {
1298 return processEthTypeSpecific(fwd);
1299 } else if (isEthDstObj) {
1300 return processEthDstSpecific(fwd);
1301 } else {
1302 log.warn("processSpecific: Unsupported forwarding objective "
1303 + "criteria fwd:{} in dev:{}", fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001304 fail(fwd, ObjectiveError.UNSUPPORTED);
1305 return Collections.emptySet();
1306 }
Saurav Das4ce45962015-11-24 23:21:05 -08001307 }
1308
Saurav Das4ce45962015-11-24 23:21:05 -08001309 /**
1310 * Handles forwarding rules to the IP and MPLS tables.
1311 *
1312 * @param fwd the forwarding objective
1313 * @return A collection of flow rules, or an empty set
1314 */
1315 protected Collection<FlowRule> processEthTypeSpecific(ForwardingObjective fwd) {
Charles Chancad338a2016-09-16 18:03:11 -07001316 return processEthTypeSpecificInternal(fwd, false, ACL_TABLE);
Charles Chanf9e98652016-09-07 16:54:23 -07001317 }
1318
1319 /**
1320 * Internal implementation of processEthTypeSpecific.
1321 * <p>
1322 * Wildcarded IPv4_DST is not supported in OFDPA i12. Therefore, we break
1323 * the rule into 0.0.0.0/1 and 128.0.0.0/1.
1324 * The allowDefaultRoute must be set to false for OFDPA i12.
1325 * </p>
1326 *
1327 * @param fwd the forwarding objective
1328 * @param allowDefaultRoute allow wildcarded IPv4_DST or not
Ray Milkey0bb1e102016-11-10 14:51:27 -08001329 * @param mplsNextTable next MPLS table
Charles Chanf9e98652016-09-07 16:54:23 -07001330 * @return A collection of flow rules, or an empty set
1331 */
1332 protected Collection<FlowRule> processEthTypeSpecificInternal(ForwardingObjective fwd,
Charles Chancad338a2016-09-16 18:03:11 -07001333 boolean allowDefaultRoute,
1334 int mplsNextTable) {
Saurav Das4ce45962015-11-24 23:21:05 -08001335 TrafficSelector selector = fwd.selector();
1336 EthTypeCriterion ethType =
1337 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001338 boolean emptyGroup = false;
Charles Chan188ebf52015-12-23 00:15:11 -08001339 int forTableId;
Saurav Das8a0732e2015-11-20 15:27:53 -08001340 TrafficSelector.Builder filteredSelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -08001341 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
Flavio Castroe10fa242016-01-15 12:43:51 -08001342 TrafficSelector.Builder complementarySelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -08001343
Saurav Das8a0732e2015-11-20 15:27:53 -08001344 if (ethType.ethType().toShort() == Ethernet.TYPE_IPV4) {
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001345 if (buildIpv4Selector(filteredSelector, complementarySelector, fwd, allowDefaultRoute) < 0) {
1346 return Collections.emptyList();
1347 }
1348 // We need to set properly the next table
Flavio Castroe10fa242016-01-15 12:43:51 -08001349 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
Charles Chan5b9df8d2016-03-28 22:21:40 -07001350 if (ipv4Dst.isMulticast()) {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001351 forTableId = MULTICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -08001352 } else {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001353 forTableId = UNICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -08001354 }
Charles Chan50d900c2018-03-02 13:26:22 -08001355
Charles Chan236653d2018-03-05 11:28:23 -08001356 // TODO decrementing IP ttl is done automatically for routing, this
Charles Chan50d900c2018-03-02 13:26:22 -08001357 // action is ignored or rejected in ofdpa as it is not fully implemented
Charles Chan236653d2018-03-05 11:28:23 -08001358 // if (fwd.treatment() != null) {
1359 // for (Instruction instr : fwd.treatment().allInstructions()) {
1360 // if (instr instanceof L3ModificationInstruction &&
1361 // ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1362 // tb.deferred().add(instr);
1363 // }
1364 // }
1365 // }
1366
Pier Ventree0ae7a32016-11-23 09:57:42 -08001367 } else if (ethType.ethType().toShort() == Ethernet.TYPE_IPV6) {
1368 if (buildIpv6Selector(filteredSelector, fwd) < 0) {
1369 return Collections.emptyList();
1370 }
Kalhee Kim3d26bf02017-10-04 14:59:11 +00001371 //We need to set the proper next table
1372 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
1373 if (ipv6Dst.isMulticast()) {
1374 forTableId = MULTICAST_ROUTING_TABLE;
1375 } else {
1376 forTableId = UNICAST_ROUTING_TABLE;
1377 }
1378
Charles Chan236653d2018-03-05 11:28:23 -08001379 // TODO decrementing IP ttl is done automatically for routing, this
Charles Chan50d900c2018-03-02 13:26:22 -08001380 // action is ignored or rejected in ofdpa as it is not fully implemented
Charles Chan236653d2018-03-05 11:28:23 -08001381 // if (fwd.treatment() != null) {
1382 // for (Instruction instr : fwd.treatment().allInstructions()) {
1383 // if (instr instanceof L3ModificationInstruction &&
1384 // ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1385 // tb.deferred().add(instr);
1386 // }
1387 // }
1388 // }
Saurav Das8a0732e2015-11-20 15:27:53 -08001389 } else {
1390 filteredSelector
1391 .matchEthType(Ethernet.MPLS_UNICAST)
1392 .matchMplsLabel(((MplsCriterion)
1393 selector.getCriterion(Criterion.Type.MPLS_LABEL)).label());
1394 MplsBosCriterion bos = (MplsBosCriterion) selector
Pier Ventre140a8942016-11-02 07:26:38 -07001395 .getCriterion(MPLS_BOS);
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001396 if (bos != null && requireMplsBosMatch()) {
Saurav Das8a0732e2015-11-20 15:27:53 -08001397 filteredSelector.matchMplsBos(bos.mplsBos());
1398 }
1399 forTableId = MPLS_TABLE_1;
Saurav Das4ce45962015-11-24 23:21:05 -08001400 log.debug("processing MPLS specific forwarding objective {} -> next:{}"
1401 + " in dev {}", fwd.id(), fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001402
Charles Chan14967c22015-12-07 11:11:50 -08001403 if (fwd.treatment() != null) {
1404 for (Instruction instr : fwd.treatment().allInstructions()) {
1405 if (instr instanceof L2ModificationInstruction &&
1406 ((L2ModificationInstruction) instr).subtype() == L2SubType.MPLS_POP) {
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001407 // OF-DPA does not pop in MPLS table in some cases. For the L3 VPN, it requires
Saurav Das9c705342017-01-06 11:36:01 -08001408 // setting the MPLS_TYPE so pop can happen down the pipeline
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001409 if (requireMplsPop()) {
1410 if (mplsNextTable == MPLS_TYPE_TABLE && isNotMplsBos(selector)) {
1411 tb.immediate().popMpls();
1412 }
1413 } else {
1414 // Skip mpls pop action for mpls_unicast label
1415 if (instr instanceof ModMplsHeaderInstruction &&
1416 !((ModMplsHeaderInstruction) instr).ethernetType()
1417 .equals(EtherType.MPLS_UNICAST.ethType())) {
1418 tb.immediate().add(instr);
1419 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001420 }
Charles Chan14967c22015-12-07 11:11:50 -08001421 }
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001422
1423 if (requireMplsTtlModification()) {
1424 if (instr instanceof L3ModificationInstruction &&
1425 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1426 // FIXME Should modify the app to send the correct DEC_MPLS_TTL instruction
1427 tb.immediate().decMplsTtl();
1428 }
1429 if (instr instanceof L3ModificationInstruction &&
1430 ((L3ModificationInstruction) instr).subtype() == L3SubType.TTL_IN) {
1431 tb.immediate().add(instr);
1432 }
Charles Chan14967c22015-12-07 11:11:50 -08001433 }
Saurav Das8a0732e2015-11-20 15:27:53 -08001434 }
1435 }
1436 }
Saurav Das822c4e22015-10-23 10:51:11 -07001437
1438 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001439 NextGroup next = getGroupForNextObjective(fwd.nextId());
1440 if (next != null) {
1441 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1442 // we only need the top level group's key to point the flow to it
1443 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1444 if (group == null) {
1445 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1446 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1447 fail(fwd, ObjectiveError.GROUPMISSING);
1448 return Collections.emptySet();
1449 }
Saurav Dasa4020382018-02-14 14:14:54 -08001450 if (isNotMplsBos(selector) && group.type().equals(SELECT)) {
1451 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
1452 + "is not implemented in OF-DPA yet. Aborting flow {} -> next:{} "
1453 + "in this device {}", fwd.id(), fwd.nextId(), deviceId);
1454 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1455 return Collections.emptySet();
1456 }
Saurav Das423fe2b2015-12-04 10:52:59 -08001457 tb.deferred().group(group.id());
Saurav Dasc568c342018-01-25 09:49:01 -08001458 // retrying flows may be necessary due to bug CORD-554
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001459 if (gkeys.size() == 1 && gkeys.get(0).size() == 1) {
Saurav Dasc568c342018-01-25 09:49:01 -08001460 if (shouldRetry()) {
1461 log.warn("Found empty group 0x{} in dev:{} .. will retry fwd:{}",
1462 Integer.toHexString(group.id().id()), deviceId,
1463 fwd.id());
1464 emptyGroup = true;
1465 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001466 }
Saurav Das25190812016-05-27 13:54:07 -07001467 } else {
1468 log.warn("Cannot find group for nextId:{} in dev:{}. Aborting fwd:{}",
1469 fwd.nextId(), deviceId, fwd.id());
1470 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1471 return Collections.emptySet();
Saurav Das822c4e22015-10-23 10:51:11 -07001472 }
Saurav Das822c4e22015-10-23 10:51:11 -07001473 }
Charles Chancad338a2016-09-16 18:03:11 -07001474
1475 if (forTableId == MPLS_TABLE_1) {
Pier Ventre140a8942016-11-02 07:26:38 -07001476 if (mplsNextTable == MPLS_L3_TYPE_TABLE) {
Charles Chancad338a2016-09-16 18:03:11 -07001477 Ofdpa3SetMplsType setMplsType = new Ofdpa3SetMplsType(Ofdpa3MplsType.L3_PHP);
Saurav Das9c705342017-01-06 11:36:01 -08001478 // set mpls type as apply_action
1479 tb.immediate().extension(setMplsType, deviceId);
Charles Chancad338a2016-09-16 18:03:11 -07001480 }
1481 tb.transition(mplsNextTable);
1482 } else {
1483 tb.transition(ACL_TABLE);
1484 }
1485
Andrea Campanellad980c6d2018-04-30 11:48:55 +02001486 if (fwd.treatment() != null && fwd.treatment().clearedDeferred()) {
1487 if (supportsUnicastBlackHole()) {
1488 tb.wipeDeferred();
1489 } else {
1490 log.warn("Clear Deferred is not supported Unicast Routing Table on device {}", deviceId);
1491 return Collections.emptySet();
1492 }
1493 }
1494
Saurav Das822c4e22015-10-23 10:51:11 -07001495 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1496 .fromApp(fwd.appId())
1497 .withPriority(fwd.priority())
1498 .forDevice(deviceId)
Saurav Das8a0732e2015-11-20 15:27:53 -08001499 .withSelector(filteredSelector.build())
1500 .withTreatment(tb.build())
1501 .forTable(forTableId);
Saurav Das822c4e22015-10-23 10:51:11 -07001502
1503 if (fwd.permanent()) {
1504 ruleBuilder.makePermanent();
1505 } else {
1506 ruleBuilder.makeTemporary(fwd.timeout());
1507 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001508 Collection<FlowRule> flowRuleCollection = new ArrayList<>();
1509 flowRuleCollection.add(ruleBuilder.build());
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001510 if (!allowDefaultRoute) {
Pier Ventree0ae7a32016-11-23 09:57:42 -08001511 flowRuleCollection.add(
1512 defaultRoute(fwd, complementarySelector, forTableId, tb)
1513 );
Flavio Castroe10fa242016-01-15 12:43:51 -08001514 log.debug("Default rule 0.0.0.0/0 is being installed two rules");
1515 }
Saurav Dasc568c342018-01-25 09:49:01 -08001516
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001517 if (emptyGroup) {
1518 executorService.schedule(new RetryFlows(fwd, flowRuleCollection),
1519 RETRY_MS, TimeUnit.MILLISECONDS);
1520 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001521 return flowRuleCollection;
Saurav Das822c4e22015-10-23 10:51:11 -07001522 }
1523
Charles Chan50d900c2018-03-02 13:26:22 -08001524 private int buildIpv4Selector(TrafficSelector.Builder builderToUpdate,
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001525 TrafficSelector.Builder extBuilder,
1526 ForwardingObjective fwd,
1527 boolean allowDefaultRoute) {
1528 TrafficSelector selector = fwd.selector();
1529
1530 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
1531 if (ipv4Dst.isMulticast()) {
1532 if (ipv4Dst.prefixLength() != 32) {
1533 log.warn("Multicast specific forwarding objective can only be /32");
1534 fail(fwd, ObjectiveError.BADPARAMS);
1535 return -1;
1536 }
1537 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
1538 if (assignedVlan == null) {
1539 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
1540 fail(fwd, ObjectiveError.BADPARAMS);
1541 return -1;
1542 }
Charles Chand1172632017-03-15 17:33:09 -07001543 if (requireVlanExtensions()) {
1544 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
1545 builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
1546 } else {
1547 builderToUpdate.matchVlanId(assignedVlan);
1548 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001549 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1550 log.debug("processing IPv4 multicast specific forwarding objective {} -> next:{}"
1551 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1552 } else {
1553 if (ipv4Dst.prefixLength() == 0) {
1554 if (allowDefaultRoute) {
1555 // The entire IPV4_DST field is wildcarded intentionally
1556 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4);
1557 } else {
1558 // NOTE: The switch does not support matching 0.0.0.0/0
1559 // Split it into 0.0.0.0/1 and 128.0.0.0/1
1560 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4)
1561 .matchIPDst(IpPrefix.valueOf("0.0.0.0/1"));
1562 extBuilder.matchEthType(Ethernet.TYPE_IPV4)
1563 .matchIPDst(IpPrefix.valueOf("128.0.0.0/1"));
1564 }
1565 } else {
1566 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1567 }
1568 log.debug("processing IPv4 unicast specific forwarding objective {} -> next:{}"
1569 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1570 }
1571 return 0;
1572 }
1573
1574 /**
1575 * Helper method to build Ipv6 selector using the selector provided by
1576 * a forwarding objective.
1577 *
1578 * @param builderToUpdate the builder to update
1579 * @param fwd the selector to read
1580 * @return 0 if the update ends correctly. -1 if the matches
1581 * are not yet supported
1582 */
Charles Chan50d900c2018-03-02 13:26:22 -08001583 int buildIpv6Selector(TrafficSelector.Builder builderToUpdate,
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001584 ForwardingObjective fwd) {
1585
1586 TrafficSelector selector = fwd.selector();
1587
1588 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
1589 if (ipv6Dst.isMulticast()) {
Julia Ferguson65428c32017-08-10 18:15:24 +00001590 if (ipv6Dst.prefixLength() != IpAddress.INET6_BIT_LENGTH) {
1591 log.warn("Multicast specific forwarding objective can only be /128");
1592 fail(fwd, ObjectiveError.BADPARAMS);
1593 return -1;
1594 }
1595 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
1596 if (assignedVlan == null) {
1597 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
1598 fail(fwd, ObjectiveError.BADPARAMS);
1599 return -1;
1600 }
1601 if (requireVlanExtensions()) {
1602 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
1603 builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
1604 } else {
1605 builderToUpdate.matchVlanId(assignedVlan);
1606 }
1607 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6).matchIPv6Dst(ipv6Dst);
1608 log.debug("processing IPv6 multicast specific forwarding objective {} -> next:{}"
1609 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1610 } else {
1611 if (ipv6Dst.prefixLength() != 0) {
1612 builderToUpdate.matchIPv6Dst(ipv6Dst);
1613 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001614 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6);
1615 log.debug("processing IPv6 unicast specific forwarding objective {} -> next:{}"
1616 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Julia Ferguson65428c32017-08-10 18:15:24 +00001617 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001618 return 0;
1619 }
1620
Charles Chan50d900c2018-03-02 13:26:22 -08001621 FlowRule defaultRoute(ForwardingObjective fwd,
Pier Ventree0ae7a32016-11-23 09:57:42 -08001622 TrafficSelector.Builder complementarySelector,
1623 int forTableId,
1624 TrafficTreatment.Builder tb) {
1625 FlowRule.Builder rule = DefaultFlowRule.builder()
1626 .fromApp(fwd.appId())
1627 .withPriority(fwd.priority())
1628 .forDevice(deviceId)
1629 .withSelector(complementarySelector.build())
1630 .withTreatment(tb.build())
1631 .forTable(forTableId);
1632 if (fwd.permanent()) {
1633 rule.makePermanent();
1634 } else {
1635 rule.makeTemporary(fwd.timeout());
1636 }
1637 return rule.build();
1638 }
1639
Saurav Das4ce45962015-11-24 23:21:05 -08001640 /**
1641 * Handles forwarding rules to the L2 bridging table. Flow actions are not
1642 * allowed in the bridging table - instead we use L2 Interface group or
1643 * L2 flood group
1644 *
1645 * @param fwd the forwarding objective
1646 * @return A collection of flow rules, or an empty set
1647 */
1648 protected Collection<FlowRule> processEthDstSpecific(ForwardingObjective fwd) {
1649 List<FlowRule> rules = new ArrayList<>();
1650
1651 // Build filtered selector
1652 TrafficSelector selector = fwd.selector();
1653 EthCriterion ethCriterion = (EthCriterion) selector
1654 .getCriterion(Criterion.Type.ETH_DST);
1655 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) selector
1656 .getCriterion(Criterion.Type.VLAN_VID);
1657
1658 if (vlanIdCriterion == null) {
1659 log.warn("Forwarding objective for bridging requires vlan. Not "
1660 + "installing fwd:{} in dev:{}", fwd.id(), deviceId);
1661 fail(fwd, ObjectiveError.BADPARAMS);
1662 return Collections.emptySet();
1663 }
1664
1665 TrafficSelector.Builder filteredSelectorBuilder =
1666 DefaultTrafficSelector.builder();
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001667
1668 if (!ethCriterion.mac().equals(NONE) &&
1669 !ethCriterion.mac().equals(BROADCAST)) {
Saurav Das4ce45962015-11-24 23:21:05 -08001670 filteredSelectorBuilder.matchEthDst(ethCriterion.mac());
1671 log.debug("processing L2 forwarding objective:{} -> next:{} in dev:{}",
1672 fwd.id(), fwd.nextId(), deviceId);
1673 } else {
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001674 // Use wildcard DST_MAC if the MacAddress is None or Broadcast
Saurav Das4ce45962015-11-24 23:21:05 -08001675 log.debug("processing L2 Broadcast forwarding objective:{} -> next:{} "
1676 + "in dev:{} for vlan:{}",
1677 fwd.id(), fwd.nextId(), deviceId, vlanIdCriterion.vlanId());
1678 }
Charles Chand1172632017-03-15 17:33:09 -07001679 if (requireVlanExtensions()) {
1680 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanIdCriterion.vlanId());
1681 filteredSelectorBuilder.extension(ofdpaMatchVlanVid, deviceId);
1682 } else {
1683 filteredSelectorBuilder.matchVlanId(vlanIdCriterion.vlanId());
1684 }
Saurav Das4ce45962015-11-24 23:21:05 -08001685 TrafficSelector filteredSelector = filteredSelectorBuilder.build();
1686
1687 if (fwd.treatment() != null) {
1688 log.warn("Ignoring traffic treatment in fwd rule {} meant for L2 table"
1689 + "for dev:{}. Expecting only nextId", fwd.id(), deviceId);
1690 }
1691
1692 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1693 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001694 NextGroup next = getGroupForNextObjective(fwd.nextId());
Saurav Das4ce45962015-11-24 23:21:05 -08001695 if (next != null) {
1696 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1697 // we only need the top level group's key to point the flow to it
1698 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1699 if (group != null) {
1700 treatmentBuilder.deferred().group(group.id());
1701 } else {
1702 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1703 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1704 fail(fwd, ObjectiveError.GROUPMISSING);
1705 return Collections.emptySet();
1706 }
1707 }
1708 }
1709 treatmentBuilder.immediate().transition(ACL_TABLE);
1710 TrafficTreatment filteredTreatment = treatmentBuilder.build();
1711
1712 // Build bridging table entries
1713 FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder();
1714 flowRuleBuilder.fromApp(fwd.appId())
1715 .withPriority(fwd.priority())
1716 .forDevice(deviceId)
1717 .withSelector(filteredSelector)
1718 .withTreatment(filteredTreatment)
1719 .forTable(BRIDGING_TABLE);
1720 if (fwd.permanent()) {
1721 flowRuleBuilder.makePermanent();
1722 } else {
1723 flowRuleBuilder.makeTemporary(fwd.timeout());
1724 }
1725 rules.add(flowRuleBuilder.build());
1726 return rules;
1727 }
1728
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001729 //////////////////////////////////////
1730 // Helper Methods and Classes
1731 //////////////////////////////////////
1732
1733 private boolean isSupportedEthTypeObjective(ForwardingObjective fwd) {
1734 TrafficSelector selector = fwd.selector();
1735 EthTypeCriterion ethType = (EthTypeCriterion) selector
1736 .getCriterion(Criterion.Type.ETH_TYPE);
1737 return !((ethType == null) ||
1738 ((ethType.ethType().toShort() != Ethernet.TYPE_IPV4) &&
Pier Ventree0ae7a32016-11-23 09:57:42 -08001739 (ethType.ethType().toShort() != Ethernet.MPLS_UNICAST)) &&
1740 (ethType.ethType().toShort() != Ethernet.TYPE_IPV6));
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001741 }
1742
1743 private boolean isSupportedEthDstObjective(ForwardingObjective fwd) {
1744 TrafficSelector selector = fwd.selector();
1745 EthCriterion ethDst = (EthCriterion) selector
1746 .getCriterion(Criterion.Type.ETH_DST);
1747 VlanIdCriterion vlanId = (VlanIdCriterion) selector
1748 .getCriterion(Criterion.Type.VLAN_VID);
1749 return !(ethDst == null && vlanId == null);
1750 }
1751
Charles Chan50d900c2018-03-02 13:26:22 -08001752 NextGroup getGroupForNextObjective(Integer nextId) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001753 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
1754 if (next != null) {
1755 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1756 if (gkeys != null && !gkeys.isEmpty()) {
1757 return next;
1758 } else {
1759 log.warn("Empty next group found in FlowObjective store for "
1760 + "next-id:{} in dev:{}", nextId, deviceId);
1761 }
1762 } else {
1763 log.warn("next-id {} not found in Flow objective store for dev:{}",
1764 nextId, deviceId);
1765 }
1766 return null;
1767 }
1768
Charles Chan188ebf52015-12-23 00:15:11 -08001769 protected static void pass(Objective obj) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001770 obj.context().ifPresent(context -> context.onSuccess(obj));
Saurav Das822c4e22015-10-23 10:51:11 -07001771 }
1772
Charles Chan188ebf52015-12-23 00:15:11 -08001773 protected static void fail(Objective obj, ObjectiveError error) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001774 obj.context().ifPresent(context -> context.onError(obj, error));
Saurav Das822c4e22015-10-23 10:51:11 -07001775 }
Saurav Das24431192016-03-07 19:13:00 -08001776
Saurav Das24431192016-03-07 19:13:00 -08001777 @Override
1778 public List<String> getNextMappings(NextGroup nextGroup) {
1779 List<String> mappings = new ArrayList<>();
1780 List<Deque<GroupKey>> gkeys = appKryo.deserialize(nextGroup.data());
1781 for (Deque<GroupKey> gkd : gkeys) {
1782 Group lastGroup = null;
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -07001783 StringBuilder gchain = new StringBuilder();
Saurav Das24431192016-03-07 19:13:00 -08001784 for (GroupKey gk : gkd) {
1785 Group g = groupService.getGroup(deviceId, gk);
Saurav Das8be4e3a2016-03-11 17:19:07 -08001786 if (g == null) {
Saurav Das25190812016-05-27 13:54:07 -07001787 gchain.append(" NoGrp").append(" -->");
Saurav Das8be4e3a2016-03-11 17:19:07 -08001788 continue;
1789 }
1790 gchain.append(" 0x").append(Integer.toHexString(g.id().id()))
1791 .append(" -->");
Saurav Das24431192016-03-07 19:13:00 -08001792 lastGroup = g;
1793 }
1794 // add port information for last group in group-chain
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -07001795 List<Instruction> lastGroupIns = new ArrayList<>();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001796 if (lastGroup != null && !lastGroup.buckets().buckets().isEmpty()) {
Saurav Das25190812016-05-27 13:54:07 -07001797 lastGroupIns = lastGroup.buckets().buckets().get(0)
1798 .treatment().allInstructions();
1799 }
1800 for (Instruction i: lastGroupIns) {
Saurav Das24431192016-03-07 19:13:00 -08001801 if (i instanceof OutputInstruction) {
Saurav Das8be4e3a2016-03-11 17:19:07 -08001802 gchain.append(" port:").append(((OutputInstruction) i).port());
Saurav Das24431192016-03-07 19:13:00 -08001803 }
1804 }
Saurav Das8be4e3a2016-03-11 17:19:07 -08001805 mappings.add(gchain.toString());
Saurav Das24431192016-03-07 19:13:00 -08001806 }
1807 return mappings;
1808 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001809
Saurav Dasa4020382018-02-14 14:14:54 -08001810 /**
1811 * Returns true iff the given selector matches on BOS==true, indicating that
1812 * the selector is trying to match on a label that is bottom-of-stack.
1813 *
1814 * @param selector the given match
1815 * @return true iff BoS==true; false if BOS==false, or BOS matching is not
1816 * expressed in the given selector
1817 */
Pier Ventre140a8942016-11-02 07:26:38 -07001818 static boolean isMplsBos(TrafficSelector selector) {
1819 MplsBosCriterion bosCriterion = (MplsBosCriterion) selector.getCriterion(MPLS_BOS);
1820 return bosCriterion != null && bosCriterion.mplsBos();
1821 }
1822
Saurav Dasa4020382018-02-14 14:14:54 -08001823 /**
1824 * Returns true iff the given selector matches on BOS==false, indicating
1825 * that the selector is trying to match on a label that is not the
1826 * bottom-of-stack label.
1827 *
1828 * @param selector the given match
1829 * @return true iff BoS==false;
1830 * false if BOS==true, or BOS matching is not expressed in the given selector
1831 */
Pier Ventre140a8942016-11-02 07:26:38 -07001832 static boolean isNotMplsBos(TrafficSelector selector) {
1833 MplsBosCriterion bosCriterion = (MplsBosCriterion) selector.getCriterion(MPLS_BOS);
1834 return bosCriterion != null && !bosCriterion.mplsBos();
1835 }
1836
Saurav Dasa4020382018-02-14 14:14:54 -08001837 /**
1838 * Returns true iff the forwarding objective includes a treatment to pop the
1839 * MPLS label.
1840 *
1841 * @param fwd the given forwarding objective
1842 * @return true iff mpls pop treatment exists
1843 */
1844 static boolean isMplsPop(ForwardingObjective fwd) {
1845 if (fwd.treatment() != null) {
1846 for (Instruction instr : fwd.treatment().allInstructions()) {
1847 if (instr instanceof L2ModificationInstruction
1848 && ((L2ModificationInstruction) instr)
1849 .subtype() == L2SubType.MPLS_POP) {
1850 return true;
1851 }
1852 }
1853 }
1854 return false;
1855 }
1856
Charles Chand9e47c62017-10-05 15:17:15 -07001857 private static boolean isIpv6(TrafficSelector selector) {
1858 EthTypeCriterion ethTypeCriterion = (EthTypeCriterion) selector.getCriterion(ETH_TYPE);
1859 return ethTypeCriterion != null && ethTypeCriterion.ethType().toShort() == Ethernet.TYPE_IPV6;
1860 }
1861
Charles Chan50d900c2018-03-02 13:26:22 -08001862 static VlanId readVlanFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001863 if (selector == null) {
1864 return null;
1865 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001866 Criterion criterion = selector.getCriterion(Criterion.Type.VLAN_VID);
1867 return (criterion == null)
1868 ? null : ((VlanIdCriterion) criterion).vlanId();
1869 }
1870
Charles Chand9e47c62017-10-05 15:17:15 -07001871 static IpPrefix readIpDstFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001872 if (selector == null) {
1873 return null;
1874 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001875 Criterion criterion = selector.getCriterion(Criterion.Type.IPV4_DST);
1876 return (criterion == null) ? null : ((IPCriterion) criterion).ip();
1877 }
Charles Chand55e84d2016-03-30 17:54:24 -07001878
1879 private static VlanId readVlanFromTreatment(TrafficTreatment treatment) {
Saurav Das59232cf2016-04-27 18:35:50 -07001880 if (treatment == null) {
1881 return null;
1882 }
Charles Chand55e84d2016-03-30 17:54:24 -07001883 for (Instruction i : treatment.allInstructions()) {
1884 if (i instanceof ModVlanIdInstruction) {
1885 return ((ModVlanIdInstruction) i).vlanId();
1886 }
1887 }
1888 return null;
1889 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001890
Jonghwan Hyun800d9d02018-04-09 09:40:50 -07001891 protected static MacAddress readEthDstFromTreatment(TrafficTreatment treatment) {
Charles Chan45b69ab2018-03-02 15:41:41 -08001892 if (treatment == null) {
1893 return null;
1894 }
1895 for (Instruction i : treatment.allInstructions()) {
1896 if (i instanceof ModEtherInstruction) {
1897 ModEtherInstruction modEtherInstruction = (ModEtherInstruction) i;
1898 if (modEtherInstruction.subtype() == L2SubType.ETH_DST) {
1899 return modEtherInstruction.mac();
1900 }
1901 }
1902 }
1903 return null;
1904 }
1905
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001906 /**
1907 * Utility class that retries sending flows a fixed number of times, even if
1908 * some of the attempts are successful. Used only for forwarding objectives.
1909 */
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001910 public final class RetryFlows implements Runnable {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001911 int attempts = MAX_RETRY_ATTEMPTS;
1912 private Collection<FlowRule> retryFlows;
1913 private ForwardingObjective fwd;
1914
Charles Chan50d900c2018-03-02 13:26:22 -08001915 RetryFlows(ForwardingObjective fwd, Collection<FlowRule> retryFlows) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001916 this.fwd = fwd;
1917 this.retryFlows = retryFlows;
1918 }
1919
1920 @Override
1921 public void run() {
1922 log.info("RETRY FLOWS ATTEMPT# {} for fwd:{} rules:{}",
1923 MAX_RETRY_ATTEMPTS - attempts, fwd.id(), retryFlows.size());
1924 sendForward(fwd, retryFlows);
1925 if (--attempts > 0) {
1926 executorService.schedule(this, RETRY_MS, TimeUnit.MILLISECONDS);
1927 }
1928 }
1929 }
1930
Saurav Das822c4e22015-10-23 10:51:11 -07001931}