blob: 851b24bfa0220b9db959766713e9c8ddb6b06780 [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;
jayakumarthazhath655b9a82018-10-01 00:51:54 +053059import org.onosproject.net.flow.criteria.ExtensionCriterion;
60import org.onosproject.net.flow.criteria.ExtensionSelector;
Saurav Das822c4e22015-10-23 10:51:11 -070061import org.onosproject.net.flow.criteria.IPCriterion;
Pier Ventree0ae7a32016-11-23 09:57:42 -080062import org.onosproject.net.flow.criteria.Icmpv6CodeCriterion;
63import org.onosproject.net.flow.criteria.Icmpv6TypeCriterion;
Saurav Das8a0732e2015-11-20 15:27:53 -080064import org.onosproject.net.flow.criteria.MplsBosCriterion;
65import org.onosproject.net.flow.criteria.MplsCriterion;
Saurav Das822c4e22015-10-23 10:51:11 -070066import org.onosproject.net.flow.criteria.PortCriterion;
Charles Chand9e47c62017-10-05 15:17:15 -070067import org.onosproject.net.flow.criteria.TcpPortCriterion;
68import org.onosproject.net.flow.criteria.UdpPortCriterion;
Saurav Das822c4e22015-10-23 10:51:11 -070069import org.onosproject.net.flow.criteria.VlanIdCriterion;
70import org.onosproject.net.flow.instructions.Instruction;
71import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
Charles Chanf76de302018-06-15 18:54:18 -070072import org.onosproject.net.flow.instructions.Instructions.NoActionInstruction;
Saurav Das822c4e22015-10-23 10:51:11 -070073import org.onosproject.net.flow.instructions.L2ModificationInstruction;
Saurav Das8a0732e2015-11-20 15:27:53 -080074import org.onosproject.net.flow.instructions.L2ModificationInstruction.L2SubType;
Saurav Das822c4e22015-10-23 10:51:11 -070075import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanIdInstruction;
Charles Chan45b69ab2018-03-02 15:41:41 -080076import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModEtherInstruction;
Alex Yashchuk4caa8e82017-12-08 17:40:05 +020077import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModMplsHeaderInstruction;
Charles Chan14967c22015-12-07 11:11:50 -080078import org.onosproject.net.flow.instructions.L3ModificationInstruction;
79import org.onosproject.net.flow.instructions.L3ModificationInstruction.L3SubType;
Saurav Das822c4e22015-10-23 10:51:11 -070080import org.onosproject.net.flowobjective.FilteringObjective;
81import org.onosproject.net.flowobjective.FlowObjectiveStore;
82import org.onosproject.net.flowobjective.ForwardingObjective;
83import org.onosproject.net.flowobjective.NextObjective;
84import org.onosproject.net.flowobjective.Objective;
85import org.onosproject.net.flowobjective.ObjectiveError;
Saurav Das822c4e22015-10-23 10:51:11 -070086import org.onosproject.net.group.DefaultGroupKey;
87import org.onosproject.net.group.Group;
Saurav Das822c4e22015-10-23 10:51:11 -070088import org.onosproject.net.group.GroupKey;
Saurav Das822c4e22015-10-23 10:51:11 -070089import org.onosproject.net.group.GroupService;
Saurav Das822c4e22015-10-23 10:51:11 -070090import org.onosproject.store.serializers.KryoNamespaces;
91import org.slf4j.Logger;
92
Pier Ventree0ae7a32016-11-23 09:57:42 -080093import java.util.ArrayDeque;
94import java.util.ArrayList;
95import java.util.Collection;
96import java.util.Collections;
97import java.util.Deque;
98import java.util.List;
99import java.util.Objects;
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700100import java.util.Optional;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800101import java.util.concurrent.ScheduledExecutorService;
102import java.util.concurrent.TimeUnit;
103
104import static java.util.concurrent.Executors.newScheduledThreadPool;
Yi Tseng3a77b4f2017-02-06 15:02:17 -0800105import static org.onlab.packet.MacAddress.BROADCAST;
Charles Chanb4879a52017-10-20 19:09:16 -0700106import static org.onlab.packet.MacAddress.IPV4_MULTICAST;
107import static org.onlab.packet.MacAddress.IPV6_MULTICAST;
Yi Tseng3a77b4f2017-02-06 15:02:17 -0800108import static org.onlab.packet.MacAddress.NONE;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800109import static org.onlab.util.Tools.groupedThreads;
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700110import static org.onosproject.driver.extensions.Ofdpa3CopyField.OXM_ID_PACKET_REG_1;
111import static org.onosproject.driver.extensions.Ofdpa3CopyField.OXM_ID_VLAN_VID;
Yi Tsengef19de12017-04-24 11:33:05 -0700112import static org.onosproject.driver.pipeline.ofdpa.OfdpaGroupHandlerUtility.*;
Charles Chand9e47c62017-10-05 15:17:15 -0700113import static org.onosproject.net.flow.criteria.Criterion.Type.ETH_TYPE;
Pier Luigi075f1012018-02-01 10:23:12 +0100114import static org.onosproject.net.group.GroupDescription.Type.SELECT;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800115import static org.slf4j.LoggerFactory.getLogger;
Pier Ventre140a8942016-11-02 07:26:38 -0700116import static org.onosproject.net.flow.criteria.Criterion.Type.MPLS_BOS;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800117
Saurav Das822c4e22015-10-23 10:51:11 -0700118/**
119 * Driver for Broadcom's OF-DPA v2.0 TTP.
Saurav Das822c4e22015-10-23 10:51:11 -0700120 */
Charles Chan361154b2016-03-24 10:23:39 -0700121public class Ofdpa2Pipeline extends AbstractHandlerBehaviour implements Pipeliner {
Saurav Das822c4e22015-10-23 10:51:11 -0700122 protected static final int PORT_TABLE = 0;
123 protected static final int VLAN_TABLE = 10;
Pier Ventre42287df2016-11-09 14:17:26 -0800124 protected static final int VLAN_1_TABLE = 11;
125 protected static final int MPLS_L2_PORT_FLOW_TABLE = 13;
126 protected static final int MPLS_L2_PORT_PCP_TRUST_FLOW_TABLE = 16;
Saurav Das822c4e22015-10-23 10:51:11 -0700127 protected static final int TMAC_TABLE = 20;
128 protected static final int UNICAST_ROUTING_TABLE = 30;
129 protected static final int MULTICAST_ROUTING_TABLE = 40;
130 protected static final int MPLS_TABLE_0 = 23;
131 protected static final int MPLS_TABLE_1 = 24;
Pier Ventre140a8942016-11-02 07:26:38 -0700132 protected static final int MPLS_L3_TYPE_TABLE = 27;
133 protected static final int MPLS_TYPE_TABLE = 29;
Saurav Das822c4e22015-10-23 10:51:11 -0700134 protected static final int BRIDGING_TABLE = 50;
135 protected static final int ACL_TABLE = 60;
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700136 protected static final int EGRESS_VLAN_FLOW_TABLE = 210;
137 protected static final int EGRESS_DSCP_PCP_REMARK_FLOW_TABLE = 230;
138 protected static final int EGRESS_TPID_FLOW_TABLE = 235;
Saurav Das822c4e22015-10-23 10:51:11 -0700139 protected static final int MAC_LEARNING_TABLE = 254;
140 protected static final long OFPP_MAX = 0xffffff00L;
141
Saurav Das52025962016-01-28 22:30:01 -0800142 protected static final int HIGHEST_PRIORITY = 0xffff;
Saurav Das2857f382015-11-03 14:39:27 -0800143 protected static final int DEFAULT_PRIORITY = 0x8000;
Saurav Das822c4e22015-10-23 10:51:11 -0700144 protected static final int LOWEST_PRIORITY = 0x0;
145
Pier Ventre42287df2016-11-09 14:17:26 -0800146 protected static final int MPLS_L2_PORT_PRIORITY = 2;
Pier Ventre42287df2016-11-09 14:17:26 -0800147 protected static final int MPLS_TUNNEL_ID_BASE = 0x10000;
148 protected static final int MPLS_TUNNEL_ID_MAX = 0x1FFFF;
149
Pier Ventre70d53ba2016-11-17 22:26:29 -0800150 protected static final int MPLS_UNI_PORT_MAX = 0x0000FFFF;
Pier Ventre70d53ba2016-11-17 22:26:29 -0800151 protected static final int MPLS_NNI_PORT_BASE = 0x00020000;
152 protected static final int MPLS_NNI_PORT_MAX = 0x0002FFFF;
153
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700154 protected static final short ALLOW_VLAN_TRANSLATION = 1;
155 protected static final int COPY_FIELD_NBITS = 12;
156 protected static final int COPY_FIELD_OFFSET = 0;
157
Saurav Das822c4e22015-10-23 10:51:11 -0700158 private final Logger log = getLogger(getClass());
Charles Chan425854b2016-04-11 15:32:12 -0700159 protected ServiceDirectory serviceDirectory;
Saurav Das822c4e22015-10-23 10:51:11 -0700160 protected FlowRuleService flowRuleService;
Charles Chan425854b2016-04-11 15:32:12 -0700161 protected CoreService coreService;
Saurav Das8a0732e2015-11-20 15:27:53 -0800162 protected GroupService groupService;
163 protected FlowObjectiveStore flowObjectiveStore;
Saurav Das822c4e22015-10-23 10:51:11 -0700164 protected DeviceId deviceId;
165 protected ApplicationId driverId;
Saurav Das822c4e22015-10-23 10:51:11 -0700166 protected DeviceService deviceService;
Charles Chan188ebf52015-12-23 00:15:11 -0800167 protected static KryoNamespace appKryo = new KryoNamespace.Builder()
Yi Tsengef19de12017-04-24 11:33:05 -0700168 .register(KryoNamespaces.API)
169 .register(GroupKey.class)
170 .register(DefaultGroupKey.class)
171 .register(OfdpaNextGroup.class)
172 .register(ArrayDeque.class)
173 .build("Ofdpa2Pipeline");
Saurav Das822c4e22015-10-23 10:51:11 -0700174
Charles Chan425854b2016-04-11 15:32:12 -0700175 protected Ofdpa2GroupHandler groupHandler;
Saurav Das822c4e22015-10-23 10:51:11 -0700176
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700177 // flows installations to be retried
Charles Chan50d900c2018-03-02 13:26:22 -0800178 private ScheduledExecutorService executorService
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700179 = newScheduledThreadPool(5, groupedThreads("OfdpaPipeliner", "retry-%d", log));
Charles Chan50d900c2018-03-02 13:26:22 -0800180 private static final int MAX_RETRY_ATTEMPTS = 10;
181 private static final int RETRY_MS = 1000;
Saurav Das4f980082015-11-05 13:39:15 -0800182
Saurav Das822c4e22015-10-23 10:51:11 -0700183 @Override
184 public void init(DeviceId deviceId, PipelinerContext context) {
Saurav Das822c4e22015-10-23 10:51:11 -0700185 this.deviceId = deviceId;
186
Charles Chan425854b2016-04-11 15:32:12 -0700187 serviceDirectory = context.directory();
Saurav Das822c4e22015-10-23 10:51:11 -0700188 coreService = serviceDirectory.get(CoreService.class);
189 flowRuleService = serviceDirectory.get(FlowRuleService.class);
190 groupService = serviceDirectory.get(GroupService.class);
191 flowObjectiveStore = context.store();
Saurav Das822c4e22015-10-23 10:51:11 -0700192 deviceService = serviceDirectory.get(DeviceService.class);
Saurav Das822c4e22015-10-23 10:51:11 -0700193
Charles Chan40132b32017-01-22 00:19:37 -0800194 initDriverId();
195 initGroupHander(context);
Saurav Das822c4e22015-10-23 10:51:11 -0700196
Saurav Das822c4e22015-10-23 10:51:11 -0700197 initializePipeline();
Saurav Das822c4e22015-10-23 10:51:11 -0700198 }
199
Charles Chan40132b32017-01-22 00:19:37 -0800200 protected void initDriverId() {
201 driverId = coreService.registerApplication(
202 "org.onosproject.driver.Ofdpa2Pipeline");
203 }
204
205 protected void initGroupHander(PipelinerContext context) {
206 groupHandler = new Ofdpa2GroupHandler();
207 groupHandler.init(deviceId, context);
208 }
209
Saurav Das822c4e22015-10-23 10:51:11 -0700210 protected void initializePipeline() {
Charles Chan188ebf52015-12-23 00:15:11 -0800211 // OF-DPA does not require initializing the pipeline as it puts default
212 // rules automatically in the hardware. However emulation of OFDPA in
213 // software switches does require table-miss-entries.
Saurav Das822c4e22015-10-23 10:51:11 -0700214 }
215
Charles Chand1172632017-03-15 17:33:09 -0700216 /**
Alex Yashchuk4caa8e82017-12-08 17:40:05 +0200217 * Determines whether this pipeline requires MPLS POP instruction.
218 *
219 * @return true to use MPLS POP instruction
220 */
221 public boolean requireMplsPop() {
222 return true;
223 }
224
225 /**
226 * Determines whether this pipeline requires MPLS BOS match.
227 *
228 * @return true to use MPLS BOS match
229 */
230 public boolean requireMplsBosMatch() {
231 return true;
232 }
233
234 /**
235 * Determines whether this pipeline requires MPLS TTL decrement and copy.
236 *
237 * @return true to use MPLS TTL decrement and copy
238 */
239 public boolean requireMplsTtlModification() {
240 return true;
241 }
242
243 /**
Charles Chand1172632017-03-15 17:33:09 -0700244 * Determines whether this pipeline requires OFDPA match and set VLAN extensions.
245 *
246 * @return true to use the extensions
247 */
248 protected boolean requireVlanExtensions() {
249 return true;
250 }
251
Saurav Das86d13e82017-04-28 17:03:48 -0700252 /**
253 * Determines whether in-port should be matched on in TMAC table rules.
254 *
255 * @return true if match on in-port should be programmed
256 */
257 protected boolean matchInPortTmacTable() {
258 return true;
259 }
260
Charles Chand9e47c62017-10-05 15:17:15 -0700261 /**
262 * Determines whether matching L4 destination port on IPv6 packets is supported in ACL table.
263 *
264 * @return true if matching L4 destination port on IPv6 packets is supported in ACL table.
265 */
266 protected boolean supportIpv6L4Dst() {
267 return true;
268 }
269
Saurav Dasc568c342018-01-25 09:49:01 -0800270 /**
271 * Determines whether this driver should continue to retry flows that point
272 * to empty groups. See CORD-554.
273 *
274 * @return true if the driver should retry flows
275 */
276 protected boolean shouldRetry() {
277 return true;
278 }
279
Charles Chan45b69ab2018-03-02 15:41:41 -0800280 /**
281 * Determines whether this driver requires unicast flow to be installed before multicast flow
282 * in TMAC table.
283 *
284 * @return true if required
285 */
286 protected boolean requireUnicastBeforeMulticast() {
287 return false;
288 }
289
Andrea Campanellad980c6d2018-04-30 11:48:55 +0200290 /**
291 * Determines whether this driver supports installing a clearDeferred action on table 30.
292 *
293 * @return true if required
294 */
295 protected boolean supportsUnicastBlackHole() {
296 return true;
297 }
298
Charles Chanab591602019-01-22 17:25:04 -0800299 protected boolean requirePuntTable() {
300 return false;
301 }
302
Saurav Das822c4e22015-10-23 10:51:11 -0700303 //////////////////////////////////////
304 // Flow Objectives
305 //////////////////////////////////////
306
307 @Override
308 public void filter(FilteringObjective filteringObjective) {
309 if (filteringObjective.type() == FilteringObjective.Type.PERMIT) {
310 processFilter(filteringObjective,
311 filteringObjective.op() == Objective.Operation.ADD,
312 filteringObjective.appId());
313 } else {
314 // Note that packets that don't match the PERMIT filter are
315 // automatically denied. The DENY filter is used to deny packets
316 // that are otherwise permitted by the PERMIT filter.
317 // Use ACL table flow rules here for DENY filtering objectives
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530318 log.warn("filter objective other than PERMIT currently not supported");
Saurav Das822c4e22015-10-23 10:51:11 -0700319 fail(filteringObjective, ObjectiveError.UNSUPPORTED);
320 }
321 }
322
323 @Override
324 public void forward(ForwardingObjective fwd) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700325 Collection<FlowRule> rules = processForward(fwd);
Saurav Das25190812016-05-27 13:54:07 -0700326 if (rules == null || rules.isEmpty()) {
327 // Assumes fail message has already been generated to the objective
328 // context. Returning here prevents spurious pass message to be
329 // generated by FlowRule service for empty flowOps.
330 return;
331 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700332 sendForward(fwd, rules);
333 }
334
Charles Chan50d900c2018-03-02 13:26:22 -0800335 private void sendForward(ForwardingObjective fwd, Collection<FlowRule> rules) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700336 FlowRuleOperations.Builder flowOpsBuilder = FlowRuleOperations.builder();
Saurav Das822c4e22015-10-23 10:51:11 -0700337 switch (fwd.op()) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700338 case ADD:
339 rules.stream()
340 .filter(Objects::nonNull)
341 .forEach(flowOpsBuilder::add);
Saurav Dasd2fded02016-12-02 15:43:47 -0800342 log.debug("Applying a add fwd-obj {} to sw:{}", fwd.id(), deviceId);
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700343 break;
344 case REMOVE:
345 rules.stream()
346 .filter(Objects::nonNull)
347 .forEach(flowOpsBuilder::remove);
Pier Ventre42287df2016-11-09 14:17:26 -0800348 log.debug("Deleting a flow rule to sw:{}", deviceId);
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700349 break;
350 default:
351 fail(fwd, ObjectiveError.UNKNOWN);
352 log.warn("Unknown forwarding type {}", fwd.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700353 }
354
Saurav Das822c4e22015-10-23 10:51:11 -0700355 flowRuleService.apply(flowOpsBuilder.build(new FlowRuleOperationsContext() {
356 @Override
357 public void onSuccess(FlowRuleOperations ops) {
358 pass(fwd);
359 }
360
361 @Override
362 public void onError(FlowRuleOperations ops) {
363 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
364 }
365 }));
Saurav Das822c4e22015-10-23 10:51:11 -0700366 }
367
368 @Override
369 public void next(NextObjective nextObjective) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800370 NextGroup nextGroup = flowObjectiveStore.getNextGroup(nextObjective.id());
371 switch (nextObjective.op()) {
372 case ADD:
Saurav Das4f980082015-11-05 13:39:15 -0800373 if (nextGroup != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800374 log.warn("Cannot add next {} that already exists in device {}",
375 nextObjective.id(), deviceId);
376 return;
377 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700378 log.debug("Processing NextObjective id {} in dev {} - add group",
Saurav Das8a0732e2015-11-20 15:27:53 -0800379 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700380 groupHandler.addGroup(nextObjective);
Saurav Das8a0732e2015-11-20 15:27:53 -0800381 break;
382 case ADD_TO_EXISTING:
383 if (nextGroup != null) {
Saurav Das1547b3f2017-05-05 17:01:08 -0700384 log.debug("Processing NextObjective id {} in dev {} - add bucket",
Saurav Das8a0732e2015-11-20 15:27:53 -0800385 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700386 groupHandler.addBucketToGroup(nextObjective, nextGroup);
Saurav Das4f980082015-11-05 13:39:15 -0800387 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800388 // it is possible that group-chain has not been fully created yet
Saurav Das423fe2b2015-12-04 10:52:59 -0800389 log.debug("Waiting to add bucket to group for next-id:{} in dev:{}",
390 nextObjective.id(), deviceId);
Yi Tseng47f82dc2017-03-05 22:48:39 -0800391
392 // by design multiple pending bucket is allowed for the group
393 groupHandler.pendingBuckets.compute(nextObjective.id(), (nextId, pendBkts) -> {
394 if (pendBkts == null) {
395 pendBkts = Sets.newHashSet();
396 }
397 pendBkts.add(nextObjective);
398 return pendBkts;
399 });
Saurav Das4f980082015-11-05 13:39:15 -0800400 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800401 break;
402 case REMOVE:
403 if (nextGroup == null) {
404 log.warn("Cannot remove next {} that does not exist in device {}",
405 nextObjective.id(), deviceId);
406 return;
407 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700408 log.debug("Processing NextObjective id {} in dev {} - remove group",
Saurav Das8a0732e2015-11-20 15:27:53 -0800409 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700410 groupHandler.removeGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800411 break;
412 case REMOVE_FROM_EXISTING:
413 if (nextGroup == null) {
414 log.warn("Cannot remove from next {} that does not exist in device {}",
415 nextObjective.id(), deviceId);
416 return;
417 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700418 log.debug("Processing NextObjective id {} in dev {} - remove bucket",
Saurav Das8a0732e2015-11-20 15:27:53 -0800419 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700420 groupHandler.removeBucketFromGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800421 break;
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +0900422 case MODIFY:
423 if (nextGroup == null) {
424 log.warn("Cannot modify next {} that does not exist in device {}",
425 nextObjective.id(), deviceId);
426 return;
427 }
Ruchi Sahotaef0761c2019-01-28 01:08:18 +0000428 log.debug("Processing NextObjective id {} in dev {} group {} - modify bucket",
429 nextObjective.id(), deviceId, nextGroup);
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +0900430 groupHandler.modifyBucketFromGroup(nextObjective, nextGroup);
431 break;
Saurav Dasceccf242017-08-03 18:30:35 -0700432 case VERIFY:
433 if (nextGroup == null) {
434 log.warn("Cannot verify next {} that does not exist in device {}",
435 nextObjective.id(), deviceId);
436 return;
437 }
438 log.debug("Processing NextObjective id {} in dev {} - verify",
439 nextObjective.id(), deviceId);
440 groupHandler.verifyGroup(nextObjective, nextGroup);
441 break;
Saurav Das8a0732e2015-11-20 15:27:53 -0800442 default:
Saurav Das4f980082015-11-05 13:39:15 -0800443 log.warn("Unsupported operation {}", nextObjective.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700444 }
445 }
446
447 //////////////////////////////////////
448 // Flow handling
449 //////////////////////////////////////
450
451 /**
Yi Tseng0fdc07e2017-04-24 19:17:08 -0700452 * As per OFDPA 2.0 TTP, filtering of VLAN ids and MAC addresses (for routing)
453 * configured on switch ports happen in different tables.
Saurav Das822c4e22015-10-23 10:51:11 -0700454 *
455 * @param filt the filtering objective
456 * @param install indicates whether to add or remove the objective
457 * @param applicationId the application that sent this objective
458 */
Saurav Das52025962016-01-28 22:30:01 -0800459 protected void processFilter(FilteringObjective filt,
460 boolean install, ApplicationId applicationId) {
Saurav Das822c4e22015-10-23 10:51:11 -0700461 // This driver only processes filtering criteria defined with switch
462 // ports as the key
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530463 PortCriterion portCriterion = null;
Saurav Das822c4e22015-10-23 10:51:11 -0700464 EthCriterion ethCriterion = null;
465 VlanIdCriterion vidCriterion = null;
Saurav Das822c4e22015-10-23 10:51:11 -0700466 if (!filt.key().equals(Criteria.dummy()) &&
467 filt.key().type() == Criterion.Type.IN_PORT) {
468 portCriterion = (PortCriterion) filt.key();
Saurav Das822c4e22015-10-23 10:51:11 -0700469 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530470 if (portCriterion == null) {
471 log.debug("No IN_PORT defined in filtering objective from app: {}",
472 applicationId);
473 } else {
474 log.debug("Received filtering objective for dev/port: {}/{}", deviceId,
475 portCriterion.port());
476 }
Saurav Das822c4e22015-10-23 10:51:11 -0700477 // convert filtering conditions for switch-intfs into flowrules
478 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
479 for (Criterion criterion : filt.conditions()) {
Yi Tseng0fdc07e2017-04-24 19:17:08 -0700480 switch (criterion.type()) {
481 case ETH_DST:
482 case ETH_DST_MASKED:
483 ethCriterion = (EthCriterion) criterion;
484 break;
485 case VLAN_VID:
486 vidCriterion = (VlanIdCriterion) criterion;
487 break;
488 default:
489 log.warn("Unsupported filter {}", criterion);
490 fail(filt, ObjectiveError.UNSUPPORTED);
491 return;
Saurav Das822c4e22015-10-23 10:51:11 -0700492 }
493 }
494
Saurav Das0e99e2b2015-10-28 12:39:42 -0700495 VlanId assignedVlan = null;
Charles Chane849c192016-01-11 18:28:54 -0800496 if (vidCriterion != null) {
Charles Chand55e84d2016-03-30 17:54:24 -0700497 // Use the VLAN in criterion if metadata is not present and the traffic is tagged
Charles Chanc550f2e2017-12-19 19:55:57 -0800498 if (!vidCriterion.vlanId().equals(VlanId.NONE)) {
Charles Chane849c192016-01-11 18:28:54 -0800499 assignedVlan = vidCriterion.vlanId();
Piere5bff482018-03-07 11:42:50 +0100500 }
501 // If the meta VLAN is present let's update the assigned vlan
502 if (filt.meta() != null) {
503 VlanId metaVlan = readVlanFromTreatment(filt.meta());
504 if (metaVlan != null) {
505 assignedVlan = metaVlan;
506 }
Charles Chand55e84d2016-03-30 17:54:24 -0700507 }
Charles Chane849c192016-01-11 18:28:54 -0800508
Charles Chand55e84d2016-03-30 17:54:24 -0700509 if (assignedVlan == null) {
510 log.error("Driver fails to extract VLAN information. "
Frank Wangd8ab0962017-08-11 11:09:30 +0800511 + "Not processing VLAN filters on device {}.", deviceId);
Charles Chand55e84d2016-03-30 17:54:24 -0700512 log.debug("VLAN ID in criterion={}, metadata={}",
513 readVlanFromTreatment(filt.meta()), vidCriterion.vlanId());
514 fail(filt, ObjectiveError.BADPARAMS);
515 return;
Saurav Das0e99e2b2015-10-28 12:39:42 -0700516 }
517 }
518
Yi Tseng3a77b4f2017-02-06 15:02:17 -0800519 if (ethCriterion == null || ethCriterion.mac().equals(NONE)) {
Charles Chan985b12e2016-05-11 19:47:22 -0700520 // NOTE: it is possible that a filtering objective only has vidCriterion
Daniel Ginsburgc1d47e92018-04-30 19:27:19 -0400521 log.debug("filtering objective missing dstMac, won't program TMAC table");
Saurav Das822c4e22015-10-23 10:51:11 -0700522 } else {
Charles Chan45b69ab2018-03-02 15:41:41 -0800523 MacAddress unicastMac = readEthDstFromTreatment(filt.meta());
Charles Chan66291502018-03-02 16:43:28 -0800524 List<List<FlowRule>> allStages = processEthDstFilter(portCriterion, ethCriterion,
Charles Chan45b69ab2018-03-02 15:41:41 -0800525 vidCriterion, assignedVlan, unicastMac, applicationId);
Charles Chan66291502018-03-02 16:43:28 -0800526 for (List<FlowRule> flowRules : allStages) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800527 log.trace("Starting a new flow rule stage for TMAC table flow");
Charles Chan66291502018-03-02 16:43:28 -0800528 ops.newStage();
Charles Chanc550f2e2017-12-19 19:55:57 -0800529
Charles Chan66291502018-03-02 16:43:28 -0800530 for (FlowRule flowRule : flowRules) {
Charles Chan056e0c12018-05-10 22:19:49 +0000531 log.trace("{} flow rules in TMAC table: {} for dev: {}",
Charles Chan66291502018-03-02 16:43:28 -0800532 (install) ? "adding" : "removing", flowRules, deviceId);
Charles Chan66291502018-03-02 16:43:28 -0800533 if (install) {
534 ops = ops.add(flowRule);
Charles Chanc550f2e2017-12-19 19:55:57 -0800535 } else {
Charles Chan66291502018-03-02 16:43:28 -0800536 // NOTE: Only remove TMAC flow when there is no more enabled port within the
537 // same VLAN on this device if TMAC doesn't support matching on in_port.
538 if (matchInPortTmacTable() || (filt.meta() != null && filt.meta().clearedDeferred())) {
539 ops = ops.remove(flowRule);
540 } else {
541 log.debug("Abort TMAC flow removal on {}. Some other ports still share this TMAC flow");
542 }
Charles Chanc550f2e2017-12-19 19:55:57 -0800543 }
544 }
Saurav Das822c4e22015-10-23 10:51:11 -0700545 }
546 }
547
Charles Chan985b12e2016-05-11 19:47:22 -0700548 if (vidCriterion == null) {
549 // NOTE: it is possible that a filtering objective only has ethCriterion
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530550 log.info("filtering objective missing VLAN, cannot program VLAN Table");
Saurav Das822c4e22015-10-23 10:51:11 -0700551 } else {
Charles Chan66291502018-03-02 16:43:28 -0800552 List<List<FlowRule>> allStages = processVlanIdFilter(
Charles Chan14967c22015-12-07 11:11:50 -0800553 portCriterion, vidCriterion, assignedVlan, applicationId);
Charles Chan66291502018-03-02 16:43:28 -0800554 for (List<FlowRule> flowRules : allStages) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800555 log.trace("Starting a new flow rule stage for VLAN table flow");
Charles Chan66291502018-03-02 16:43:28 -0800556 ops.newStage();
Charles Chan14967c22015-12-07 11:11:50 -0800557
Charles Chan66291502018-03-02 16:43:28 -0800558 for (FlowRule flowRule : flowRules) {
559 log.trace("{} flow rules in VLAN table: {} for dev: {}",
560 (install) ? "adding" : "removing", flowRule, deviceId);
561 ops = install ? ops.add(flowRule) : ops.remove(flowRule);
Charles Chand1172632017-03-15 17:33:09 -0700562 }
Saurav Das822c4e22015-10-23 10:51:11 -0700563 }
564 }
565
Saurav Das822c4e22015-10-23 10:51:11 -0700566 // apply filtering flow rules
567 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
568 @Override
569 public void onSuccess(FlowRuleOperations ops) {
Saurav Das018605f2017-02-18 14:05:44 -0800570 log.debug("Applied {} filtering rules in device {}",
Saurav Das822c4e22015-10-23 10:51:11 -0700571 ops.stages().get(0).size(), deviceId);
572 pass(filt);
573 }
574
575 @Override
576 public void onError(FlowRuleOperations ops) {
577 log.info("Failed to apply all filtering rules in dev {}", deviceId);
578 fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED);
579 }
580 }));
581
582 }
583
584 /**
Charles Chand1172632017-03-15 17:33:09 -0700585 * Internal implementation of processVlanIdFilter.
586 * <p>
587 * The is_present bit in set_vlan_vid action is required to be 0 in OFDPA i12.
588 * Since it is non-OF spec, we need an extension treatment for that.
589 * The useVlanExtension must be set to false for OFDPA i12.
590 * </p>
Charles Chan66291502018-03-02 16:43:28 -0800591 * <p>
592 * NOTE: Separate VLAN filtering rules and assignment rules
593 * into different stages in order to guarantee that filtering rules
594 * always go first, as required by OFDPA.
595 * </p>
Saurav Das0e99e2b2015-10-28 12:39:42 -0700596 *
Charles Chanf9e98652016-09-07 16:54:23 -0700597 * @param portCriterion port on device for which this filter is programmed
598 * @param vidCriterion vlan assigned to port, or NONE for untagged
599 * @param assignedVlan assigned vlan-id for untagged packets
600 * @param applicationId for application programming this filter
Charles Chan66291502018-03-02 16:43:28 -0800601 * @return stages of flow rules for port-vlan filters
Saurav Das822c4e22015-10-23 10:51:11 -0700602 */
Charles Chan66291502018-03-02 16:43:28 -0800603 protected List<List<FlowRule>> processVlanIdFilter(PortCriterion portCriterion,
Yi Tsengef19de12017-04-24 11:33:05 -0700604 VlanIdCriterion vidCriterion,
605 VlanId assignedVlan,
606 ApplicationId applicationId) {
Charles Chan66291502018-03-02 16:43:28 -0800607 List<FlowRule> filteringRules = new ArrayList<>();
608 List<FlowRule> assignmentRules = new ArrayList<>();
Saurav Das822c4e22015-10-23 10:51:11 -0700609 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
610 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800611 TrafficSelector.Builder preSelector = null;
612 TrafficTreatment.Builder preTreatment = null;
613
Saurav Das4f980082015-11-05 13:39:15 -0800614 treatment.transition(TMAC_TABLE);
615
Saurav Das822c4e22015-10-23 10:51:11 -0700616 if (vidCriterion.vlanId() == VlanId.NONE) {
617 // untagged packets are assigned vlans
Charles Chand1172632017-03-15 17:33:09 -0700618 preSelector = DefaultTrafficSelector.builder();
619 if (requireVlanExtensions()) {
620 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(VlanId.NONE);
621 selector.extension(ofdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700622 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
623 treatment.extension(ofdpaSetVlanVid, deviceId);
Charles Chand1172632017-03-15 17:33:09 -0700624
625 OfdpaMatchVlanVid preOfdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
626 preSelector.extension(preOfdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700627 } else {
Charles Chand1172632017-03-15 17:33:09 -0700628 selector.matchVlanId(VlanId.NONE);
Charles Chanf9e98652016-09-07 16:54:23 -0700629 treatment.setVlanId(assignedVlan);
Charles Chand1172632017-03-15 17:33:09 -0700630
631 preSelector.matchVlanId(assignedVlan);
632 }
633 preTreatment = DefaultTrafficTreatment.builder().transition(TMAC_TABLE);
634 } else {
635 if (requireVlanExtensions()) {
636 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
637 selector.extension(ofdpaMatchVlanVid, deviceId);
638 } else {
639 selector.matchVlanId(vidCriterion.vlanId());
Charles Chanf9e98652016-09-07 16:54:23 -0700640 }
Charles Chan14967c22015-12-07 11:11:50 -0800641
Charles Chand55e84d2016-03-30 17:54:24 -0700642 if (!assignedVlan.equals(vidCriterion.vlanId())) {
Charles Chand1172632017-03-15 17:33:09 -0700643 if (requireVlanExtensions()) {
Charles Chanc03782d2017-01-31 13:57:55 -0800644 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
645 treatment.extension(ofdpaSetVlanVid, deviceId);
646 } else {
647 treatment.setVlanId(assignedVlan);
648 }
Charles Chand55e84d2016-03-30 17:54:24 -0700649 }
Saurav Das822c4e22015-10-23 10:51:11 -0700650 }
Saurav Das822c4e22015-10-23 10:51:11 -0700651
652 // ofdpa cannot match on ALL portnumber, so we need to use separate
653 // rules for each port.
Charles Chan14967c22015-12-07 11:11:50 -0800654 List<PortNumber> portnums = new ArrayList<>();
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530655 if (portCriterion != null) {
656 if (PortNumber.ALL.equals(portCriterion.port())) {
657 for (Port port : deviceService.getPorts(deviceId)) {
658 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
659 portnums.add(port.number());
660 }
Saurav Das822c4e22015-10-23 10:51:11 -0700661 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530662 } else {
663 portnums.add(portCriterion.port());
Saurav Das822c4e22015-10-23 10:51:11 -0700664 }
665 } else {
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530666 log.warn("Filtering Objective missing Port Criterion . " +
Charles Chan66291502018-03-02 16:43:28 -0800667 "VLAN Table cannot be programmed for {}", deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700668 }
Saurav Das4f980082015-11-05 13:39:15 -0800669
Saurav Das822c4e22015-10-23 10:51:11 -0700670 for (PortNumber pnum : portnums) {
Saurav Das4f980082015-11-05 13:39:15 -0800671 // create rest of flowrule
Saurav Das822c4e22015-10-23 10:51:11 -0700672 selector.matchInPort(pnum);
673 FlowRule rule = DefaultFlowRule.builder()
674 .forDevice(deviceId)
675 .withSelector(selector.build())
676 .withTreatment(treatment.build())
677 .withPriority(DEFAULT_PRIORITY)
678 .fromApp(applicationId)
679 .makePermanent()
680 .forTable(VLAN_TABLE).build();
Charles Chan66291502018-03-02 16:43:28 -0800681 assignmentRules.add(rule);
Charles Chan14967c22015-12-07 11:11:50 -0800682
683 if (preSelector != null) {
684 preSelector.matchInPort(pnum);
685 FlowRule preRule = DefaultFlowRule.builder()
686 .forDevice(deviceId)
687 .withSelector(preSelector.build())
688 .withTreatment(preTreatment.build())
689 .withPriority(DEFAULT_PRIORITY)
690 .fromApp(applicationId)
691 .makePermanent()
692 .forTable(VLAN_TABLE).build();
Charles Chan66291502018-03-02 16:43:28 -0800693 filteringRules.add(preRule);
Charles Chan14967c22015-12-07 11:11:50 -0800694 }
Saurav Das822c4e22015-10-23 10:51:11 -0700695 }
Charles Chan2686dd72018-03-06 22:10:15 -0800696 return ImmutableList.of(filteringRules, assignmentRules);
Saurav Das822c4e22015-10-23 10:51:11 -0700697 }
698
699 /**
700 * Allows routed packets with correct destination MAC to be directed
Charles Chan45b69ab2018-03-02 15:41:41 -0800701 * to unicast routing table, multicast routing table or MPLS forwarding table.
Saurav Das822c4e22015-10-23 10:51:11 -0700702 *
703 * @param portCriterion port on device for which this filter is programmed
704 * @param ethCriterion dstMac of device for which is filter is programmed
705 * @param vidCriterion vlan assigned to port, or NONE for untagged
Saurav Das0e99e2b2015-10-28 12:39:42 -0700706 * @param assignedVlan assigned vlan-id for untagged packets
Charles Chan45b69ab2018-03-02 15:41:41 -0800707 * @param unicastMac some switches require a unicast TMAC flow to be programmed before multicast
708 * TMAC flow. This MAC address will be used for the unicast TMAC flow.
709 * This is unused if the filtering objective is a unicast.
Saurav Das822c4e22015-10-23 10:51:11 -0700710 * @param applicationId for application programming this filter
Charles Chan66291502018-03-02 16:43:28 -0800711 * @return stages of flow rules for port-vlan filters
Saurav Das822c4e22015-10-23 10:51:11 -0700712
713 */
Charles Chan66291502018-03-02 16:43:28 -0800714 protected List<List<FlowRule>> processEthDstFilter(PortCriterion portCriterion,
Saurav Das822c4e22015-10-23 10:51:11 -0700715 EthCriterion ethCriterion,
716 VlanIdCriterion vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700717 VlanId assignedVlan,
Charles Chan45b69ab2018-03-02 15:41:41 -0800718 MacAddress unicastMac,
Saurav Das822c4e22015-10-23 10:51:11 -0700719 ApplicationId applicationId) {
Charles Chan5270ed02016-01-30 23:22:37 -0800720 // Consider PortNumber.ANY as wildcard. Match ETH_DST only
Sivachidambaram Subramanianc598d062017-05-16 22:42:06 +0530721 if (portCriterion != null && PortNumber.ANY.equals(portCriterion.port())) {
Charles Chan5270ed02016-01-30 23:22:37 -0800722 return processEthDstOnlyFilter(ethCriterion, applicationId);
723 }
724
Charles Chan5b9df8d2016-03-28 22:21:40 -0700725 // Multicast MAC
726 if (ethCriterion.mask() != null) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800727 return processMcastEthDstFilter(ethCriterion, assignedVlan, unicastMac, applicationId);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700728 }
729
Saurav Das822c4e22015-10-23 10:51:11 -0700730 //handling untagged packets via assigned VLAN
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530731 if (vidCriterion != null && vidCriterion.vlanId() == VlanId.NONE) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700732 vidCriterion = (VlanIdCriterion) Criteria.matchVlanId(assignedVlan);
Saurav Das822c4e22015-10-23 10:51:11 -0700733 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530734 List<FlowRule> rules = new ArrayList<>();
735 OfdpaMatchVlanVid ofdpaMatchVlanVid = null;
736 if (vidCriterion != null && requireVlanExtensions()) {
737 ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
738 }
Saurav Das822c4e22015-10-23 10:51:11 -0700739 // ofdpa cannot match on ALL portnumber, so we need to use separate
740 // rules for each port.
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -0700741 List<PortNumber> portnums = new ArrayList<>();
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530742 if (portCriterion != null) {
743 if (PortNumber.ALL.equals(portCriterion.port())) {
744 for (Port port : deviceService.getPorts(deviceId)) {
745 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
746 portnums.add(port.number());
747 }
Saurav Das822c4e22015-10-23 10:51:11 -0700748 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530749 } else {
750 portnums.add(portCriterion.port());
751 }
752 for (PortNumber pnum : portnums) {
753 rules.add(buildTmacRuleForIpv4(ethCriterion,
754 vidCriterion,
755 ofdpaMatchVlanVid,
756 applicationId,
757 pnum));
758 rules.add(buildTmacRuleForMpls(ethCriterion,
759 vidCriterion,
760 ofdpaMatchVlanVid,
761 applicationId,
762 pnum));
763 rules.add(buildTmacRuleForIpv6(ethCriterion,
764 vidCriterion,
765 ofdpaMatchVlanVid,
766 applicationId,
767 pnum));
Saurav Das822c4e22015-10-23 10:51:11 -0700768 }
769 } else {
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530770 rules.add(buildTmacRuleForIpv4(ethCriterion,
771 vidCriterion,
772 ofdpaMatchVlanVid,
773 applicationId,
774 null));
775 rules.add(buildTmacRuleForMpls(ethCriterion,
776 vidCriterion,
777 ofdpaMatchVlanVid,
778 applicationId,
779 null));
780 rules.add(buildTmacRuleForIpv6(ethCriterion,
781 vidCriterion,
782 ofdpaMatchVlanVid,
783 applicationId,
784 null));
Saurav Das822c4e22015-10-23 10:51:11 -0700785 }
Charles Chan66291502018-03-02 16:43:28 -0800786 return ImmutableList.of(rules);
Saurav Das822c4e22015-10-23 10:51:11 -0700787 }
788
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530789 /**
790 * Builds TMAC rules for IPv4 packets.
791 *
Charles Chanc550f2e2017-12-19 19:55:57 -0800792 * @param ethCriterion dst mac matching
793 * @param vidCriterion vlan id assigned to the port
794 * @param ofdpaMatchVlanVid OFDPA vlan id matching
795 * @param applicationId application id
796 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530797 * @return TMAC rule for IPV4 packets
798 */
799 private FlowRule buildTmacRuleForIpv4(EthCriterion ethCriterion,
800 VlanIdCriterion vidCriterion,
801 OfdpaMatchVlanVid ofdpaMatchVlanVid,
802 ApplicationId applicationId,
803 PortNumber pnum) {
804 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
805 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
806 if (pnum != null) {
807 if (matchInPortTmacTable()) {
808 selector.matchInPort(pnum);
809 } else {
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800810 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530811 "ignoring the IN_PORT criteria");
812 }
813 }
814 if (vidCriterion != null) {
815 if (requireVlanExtensions()) {
816 selector.extension(ofdpaMatchVlanVid, deviceId);
817 } else {
818 selector.matchVlanId(vidCriterion.vlanId());
819 }
820 }
821 selector.matchEthType(Ethernet.TYPE_IPV4);
822 selector.matchEthDst(ethCriterion.mac());
823 treatment.transition(UNICAST_ROUTING_TABLE);
824 return DefaultFlowRule.builder()
825 .forDevice(deviceId)
826 .withSelector(selector.build())
827 .withTreatment(treatment.build())
828 .withPriority(DEFAULT_PRIORITY)
829 .fromApp(applicationId)
830 .makePermanent()
831 .forTable(TMAC_TABLE).build();
832 }
833
834 /**
835 * Builds TMAC rule for MPLS packets.
836 *
Charles Chanc550f2e2017-12-19 19:55:57 -0800837 * @param ethCriterion dst mac matching
838 * @param vidCriterion vlan id assigned to the port
839 * @param ofdpaMatchVlanVid OFDPA vlan id matching
840 * @param applicationId application id
841 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530842 * @return TMAC rule for MPLS packets
843 */
844 private FlowRule buildTmacRuleForMpls(EthCriterion ethCriterion,
845 VlanIdCriterion vidCriterion,
846 OfdpaMatchVlanVid ofdpaMatchVlanVid,
847 ApplicationId applicationId,
848 PortNumber pnum) {
849 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
850 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
851 if (pnum != null) {
852 if (matchInPortTmacTable()) {
853 selector.matchInPort(pnum);
854 } else {
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800855 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530856 "ignoring the IN_PORT criteria");
857 }
858 }
859 if (vidCriterion != null) {
860 if (requireVlanExtensions()) {
861 selector.extension(ofdpaMatchVlanVid, deviceId);
862 } else {
863 selector.matchVlanId(vidCriterion.vlanId());
864 }
865 }
866 selector.matchEthType(Ethernet.MPLS_UNICAST);
867 selector.matchEthDst(ethCriterion.mac());
868 treatment.transition(MPLS_TABLE_0);
869 return DefaultFlowRule.builder()
870 .forDevice(deviceId)
871 .withSelector(selector.build())
872 .withTreatment(treatment.build())
873 .withPriority(DEFAULT_PRIORITY)
874 .fromApp(applicationId)
875 .makePermanent()
876 .forTable(TMAC_TABLE).build();
877 }
878
879 /**
880 * Builds TMAC rules for IPv6 packets.
881 *
Charles Chanc550f2e2017-12-19 19:55:57 -0800882 * @param ethCriterion dst mac matching
883 * @param vidCriterion vlan id assigned to the port
884 * @param ofdpaMatchVlanVid OFDPA vlan id matching
885 * @param applicationId application id
886 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530887 * @return TMAC rule for IPV6 packets
888 */
889 private FlowRule buildTmacRuleForIpv6(EthCriterion ethCriterion,
890 VlanIdCriterion vidCriterion,
891 OfdpaMatchVlanVid ofdpaMatchVlanVid,
892 ApplicationId applicationId,
893 PortNumber pnum) {
894 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
895 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
896 if (pnum != null) {
897 if (matchInPortTmacTable()) {
898 selector.matchInPort(pnum);
899 } else {
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800900 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530901 "ignoring the IN_PORT criteria");
902 }
903 }
904 if (vidCriterion != null) {
905 if (requireVlanExtensions()) {
906 selector.extension(ofdpaMatchVlanVid, deviceId);
907 } else {
908 selector.matchVlanId(vidCriterion.vlanId());
909 }
910 }
911 selector.matchEthType(Ethernet.TYPE_IPV6);
912 selector.matchEthDst(ethCriterion.mac());
913 treatment.transition(UNICAST_ROUTING_TABLE);
914 return DefaultFlowRule.builder()
915 .forDevice(deviceId)
916 .withSelector(selector.build())
917 .withTreatment(treatment.build())
918 .withPriority(DEFAULT_PRIORITY)
919 .fromApp(applicationId)
920 .makePermanent()
921 .forTable(TMAC_TABLE).build();
922 }
923
Charles Chan66291502018-03-02 16:43:28 -0800924 protected List<List<FlowRule>> processEthDstOnlyFilter(EthCriterion ethCriterion,
Yi Tsengef19de12017-04-24 11:33:05 -0700925 ApplicationId applicationId) {
Pier Luigi0e358632017-01-31 09:35:05 -0800926 ImmutableList.Builder<FlowRule> builder = ImmutableList.builder();
927
Charles Chan5270ed02016-01-30 23:22:37 -0800928 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
929 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
930 selector.matchEthType(Ethernet.TYPE_IPV4);
931 selector.matchEthDst(ethCriterion.mac());
932 treatment.transition(UNICAST_ROUTING_TABLE);
933 FlowRule rule = DefaultFlowRule.builder()
934 .forDevice(deviceId)
935 .withSelector(selector.build())
936 .withTreatment(treatment.build())
937 .withPriority(DEFAULT_PRIORITY)
938 .fromApp(applicationId)
939 .makePermanent()
940 .forTable(TMAC_TABLE).build();
Pier Luigi0e358632017-01-31 09:35:05 -0800941 builder.add(rule);
942
943 selector = DefaultTrafficSelector.builder();
944 treatment = DefaultTrafficTreatment.builder();
945 selector.matchEthType(Ethernet.TYPE_IPV6);
946 selector.matchEthDst(ethCriterion.mac());
947 treatment.transition(UNICAST_ROUTING_TABLE);
948 rule = DefaultFlowRule.builder()
949 .forDevice(deviceId)
950 .withSelector(selector.build())
951 .withTreatment(treatment.build())
952 .withPriority(DEFAULT_PRIORITY)
953 .fromApp(applicationId)
954 .makePermanent()
955 .forTable(TMAC_TABLE).build();
Charles Chan66291502018-03-02 16:43:28 -0800956 return ImmutableList.of(builder.add(rule).build());
Charles Chan5270ed02016-01-30 23:22:37 -0800957 }
958
Charles Chan66291502018-03-02 16:43:28 -0800959 List<List<FlowRule>> processMcastEthDstFilter(EthCriterion ethCriterion,
Charles Chanb4879a52017-10-20 19:09:16 -0700960 VlanId assignedVlan,
Charles Chan45b69ab2018-03-02 15:41:41 -0800961 MacAddress unicastMac,
Yi Tsengef19de12017-04-24 11:33:05 -0700962 ApplicationId applicationId) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800963 ImmutableList.Builder<FlowRule> unicastFlows = ImmutableList.builder();
964 ImmutableList.Builder<FlowRule> multicastFlows = ImmutableList.builder();
965 TrafficSelector.Builder selector;
966 TrafficTreatment.Builder treatment;
Charles Chanb4879a52017-10-20 19:09:16 -0700967 FlowRule rule;
Julia Ferguson65428c32017-08-10 18:15:24 +0000968
Charles Chanb4879a52017-10-20 19:09:16 -0700969 if (IPV4_MULTICAST.equals(ethCriterion.mac())) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800970 if (requireUnicastBeforeMulticast()) {
971 selector = DefaultTrafficSelector.builder();
972 treatment = DefaultTrafficTreatment.builder();
973 selector.matchEthType(Ethernet.TYPE_IPV4);
974 selector.matchEthDst(unicastMac);
Charles Chan056e0c12018-05-10 22:19:49 +0000975 selector.matchVlanId(assignedVlan);
Charles Chan45b69ab2018-03-02 15:41:41 -0800976 treatment.transition(UNICAST_ROUTING_TABLE);
977 rule = DefaultFlowRule.builder()
978 .forDevice(deviceId)
979 .withSelector(selector.build())
980 .withTreatment(treatment.build())
Charles Chan056e0c12018-05-10 22:19:49 +0000981 .withPriority(DEFAULT_PRIORITY)
Charles Chan45b69ab2018-03-02 15:41:41 -0800982 .fromApp(applicationId)
983 .makePermanent()
984 .forTable(TMAC_TABLE).build();
985 unicastFlows.add(rule);
986 }
987
988 selector = DefaultTrafficSelector.builder();
989 treatment = DefaultTrafficTreatment.builder();
Charles Chanb4879a52017-10-20 19:09:16 -0700990 selector.matchEthType(Ethernet.TYPE_IPV4);
991 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
Charles Chan056e0c12018-05-10 22:19:49 +0000992 selector.matchVlanId(assignedVlan);
Charles Chanb4879a52017-10-20 19:09:16 -0700993 treatment.transition(MULTICAST_ROUTING_TABLE);
994 rule = DefaultFlowRule.builder()
995 .forDevice(deviceId)
996 .withSelector(selector.build())
997 .withTreatment(treatment.build())
Charles Chan056e0c12018-05-10 22:19:49 +0000998 .withPriority(DEFAULT_PRIORITY)
Charles Chanb4879a52017-10-20 19:09:16 -0700999 .fromApp(applicationId)
1000 .makePermanent()
1001 .forTable(TMAC_TABLE).build();
Charles Chan45b69ab2018-03-02 15:41:41 -08001002 multicastFlows.add(rule);
Charles Chanb4879a52017-10-20 19:09:16 -07001003 }
1004
1005 if (IPV6_MULTICAST.equals(ethCriterion.mac())) {
Charles Chan45b69ab2018-03-02 15:41:41 -08001006 if (requireUnicastBeforeMulticast()) {
1007 selector = DefaultTrafficSelector.builder();
1008 treatment = DefaultTrafficTreatment.builder();
1009 selector.matchEthType(Ethernet.TYPE_IPV6);
1010 selector.matchEthDst(unicastMac);
Charles Chan056e0c12018-05-10 22:19:49 +00001011 selector.matchVlanId(assignedVlan);
Charles Chan45b69ab2018-03-02 15:41:41 -08001012 treatment.transition(UNICAST_ROUTING_TABLE);
1013 rule = DefaultFlowRule.builder()
1014 .forDevice(deviceId)
1015 .withSelector(selector.build())
1016 .withTreatment(treatment.build())
Charles Chan056e0c12018-05-10 22:19:49 +00001017 .withPriority(DEFAULT_PRIORITY)
Charles Chan45b69ab2018-03-02 15:41:41 -08001018 .fromApp(applicationId)
1019 .makePermanent()
1020 .forTable(TMAC_TABLE).build();
1021 unicastFlows.add(rule);
1022 }
1023
Charles Chanb4879a52017-10-20 19:09:16 -07001024 selector = DefaultTrafficSelector.builder();
1025 treatment = DefaultTrafficTreatment.builder();
1026 selector.matchEthType(Ethernet.TYPE_IPV6);
1027 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
Charles Chan056e0c12018-05-10 22:19:49 +00001028 selector.matchVlanId(assignedVlan);
Charles Chanb4879a52017-10-20 19:09:16 -07001029 treatment.transition(MULTICAST_ROUTING_TABLE);
1030 rule = DefaultFlowRule.builder()
1031 .forDevice(deviceId)
1032 .withSelector(selector.build())
1033 .withTreatment(treatment.build())
Charles Chan056e0c12018-05-10 22:19:49 +00001034 .withPriority(DEFAULT_PRIORITY)
Charles Chanb4879a52017-10-20 19:09:16 -07001035 .fromApp(applicationId)
1036 .makePermanent()
1037 .forTable(TMAC_TABLE).build();
Charles Chan45b69ab2018-03-02 15:41:41 -08001038 multicastFlows.add(rule);
Charles Chanb4879a52017-10-20 19:09:16 -07001039 }
Charles Chan45b69ab2018-03-02 15:41:41 -08001040 return ImmutableList.of(unicastFlows.build(), multicastFlows.build());
Charles Chan5b9df8d2016-03-28 22:21:40 -07001041 }
1042
Saurav Das822c4e22015-10-23 10:51:11 -07001043 private Collection<FlowRule> processForward(ForwardingObjective fwd) {
1044 switch (fwd.flag()) {
1045 case SPECIFIC:
1046 return processSpecific(fwd);
1047 case VERSATILE:
1048 return processVersatile(fwd);
Jonghwan Hyun800d9d02018-04-09 09:40:50 -07001049 case EGRESS:
1050 return processEgress(fwd);
Saurav Das822c4e22015-10-23 10:51:11 -07001051 default:
1052 fail(fwd, ObjectiveError.UNKNOWN);
1053 log.warn("Unknown forwarding flag {}", fwd.flag());
1054 }
1055 return Collections.emptySet();
1056 }
1057
1058 /**
Jonghwan Hyun800d9d02018-04-09 09:40:50 -07001059 * In the OF-DPA 2.0 pipeline, egress forwarding objectives go to the
1060 * egress tables.
1061 * @param fwd the forwarding objective of type 'egress'
1062 * @return a collection of flow rules to be sent to the switch. An empty
1063 * collection may be returned if there is a problem in processing
1064 * the flow rule
1065 */
1066 protected Collection<FlowRule> processEgress(ForwardingObjective fwd) {
1067 log.debug("Processing egress forwarding objective:{} in dev:{}",
1068 fwd, deviceId);
1069
1070 List<FlowRule> rules = new ArrayList<>();
1071
1072 // Build selector
1073 TrafficSelector.Builder sb = DefaultTrafficSelector.builder();
1074 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) fwd.selector().getCriterion(Criterion.Type.VLAN_VID);
1075 if (vlanIdCriterion == null) {
1076 log.error("Egress forwarding objective:{} must include vlanId", fwd.id());
1077 fail(fwd, ObjectiveError.BADPARAMS);
1078 return rules;
1079 }
1080
1081 Optional<Instruction> outInstr = fwd.treatment().allInstructions().stream()
1082 .filter(instruction -> instruction instanceof OutputInstruction).findFirst();
1083 if (!outInstr.isPresent()) {
1084 log.error("Egress forwarding objective:{} must include output port", fwd.id());
1085 fail(fwd, ObjectiveError.BADPARAMS);
1086 return rules;
1087 }
1088
1089 PortNumber portNumber = ((OutputInstruction) outInstr.get()).port();
1090
1091 sb.matchVlanId(vlanIdCriterion.vlanId());
1092 OfdpaMatchActsetOutput actsetOutput = new OfdpaMatchActsetOutput(portNumber);
1093 sb.extension(actsetOutput, deviceId);
1094
1095 sb.extension(new OfdpaMatchAllowVlanTranslation(ALLOW_VLAN_TRANSLATION), deviceId);
1096
1097 // Build a flow rule for Egress VLAN Flow table
1098 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
1099 tb.transition(EGRESS_DSCP_PCP_REMARK_FLOW_TABLE);
1100 if (fwd.treatment() != null) {
1101 for (Instruction instr : fwd.treatment().allInstructions()) {
1102 if (instr instanceof L2ModificationInstruction &&
1103 ((L2ModificationInstruction) instr).subtype() == L2SubType.VLAN_ID) {
1104 tb.immediate().add(instr);
1105 }
1106 if (instr instanceof L2ModificationInstruction &&
1107 ((L2ModificationInstruction) instr).subtype() == L2SubType.VLAN_PUSH) {
1108 tb.immediate().pushVlan();
1109 EthType ethType = ((L2ModificationInstruction.ModVlanHeaderInstruction) instr).ethernetType();
1110 if (ethType.equals(EtherType.QINQ.ethType())) {
1111 // Build a flow rule for Egress TPID Flow table
1112 TrafficSelector tpidSelector = DefaultTrafficSelector.builder()
1113 .extension(actsetOutput, deviceId)
1114 .matchVlanId(VlanId.ANY).build();
1115
1116 TrafficTreatment tpidTreatment = DefaultTrafficTreatment.builder()
1117 .extension(new Ofdpa3CopyField(COPY_FIELD_NBITS, COPY_FIELD_OFFSET,
1118 COPY_FIELD_OFFSET, OXM_ID_VLAN_VID,
1119 OXM_ID_PACKET_REG_1),
1120 deviceId)
1121 .popVlan()
1122 .pushVlan(EtherType.QINQ.ethType())
1123 .extension(new Ofdpa3CopyField(COPY_FIELD_NBITS, COPY_FIELD_OFFSET,
1124 COPY_FIELD_OFFSET, OXM_ID_PACKET_REG_1,
1125 OXM_ID_VLAN_VID),
1126 deviceId)
1127 .build();
1128
1129 FlowRule.Builder tpidRuleBuilder = DefaultFlowRule.builder()
1130 .fromApp(fwd.appId())
1131 .withPriority(fwd.priority())
1132 .forDevice(deviceId)
1133 .withSelector(tpidSelector)
1134 .withTreatment(tpidTreatment)
1135 .makePermanent()
1136 .forTable(EGRESS_TPID_FLOW_TABLE);
1137 rules.add(tpidRuleBuilder.build());
1138 }
1139 }
1140 }
1141 }
1142
1143 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1144 .fromApp(fwd.appId())
1145 .withPriority(fwd.priority())
1146 .forDevice(deviceId)
1147 .withSelector(sb.build())
1148 .withTreatment(tb.build())
1149 .makePermanent()
1150 .forTable(EGRESS_VLAN_FLOW_TABLE);
1151 rules.add(ruleBuilder.build());
1152 return rules;
1153 }
1154
1155 /**
Saurav Das822c4e22015-10-23 10:51:11 -07001156 * In the OF-DPA 2.0 pipeline, versatile forwarding objectives go to the
1157 * ACL table.
1158 * @param fwd the forwarding objective of type 'versatile'
1159 * @return a collection of flow rules to be sent to the switch. An empty
1160 * collection may be returned if there is a problem in processing
1161 * the flow rule
1162 */
Saurav Das52025962016-01-28 22:30:01 -08001163 protected Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
Saurav Das018605f2017-02-18 14:05:44 -08001164 log.debug("Processing versatile forwarding objective:{} in dev:{}",
Saurav Dasd2fded02016-12-02 15:43:47 -08001165 fwd.id(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001166
Saurav Das822c4e22015-10-23 10:51:11 -07001167 if (fwd.nextId() == null && fwd.treatment() == null) {
1168 log.error("Forwarding objective {} from {} must contain "
1169 + "nextId or Treatment", fwd.selector(), fwd.appId());
Zsolt Haraszti9faab752016-02-17 15:55:20 -08001170 fail(fwd, ObjectiveError.BADPARAMS);
Saurav Das822c4e22015-10-23 10:51:11 -07001171 return Collections.emptySet();
1172 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001173
Charles Chanab591602019-01-22 17:25:04 -08001174 TrafficSelector.Builder sbuilder = versatileSelectorBuilder(fwd);
1175 TrafficTreatment.Builder ttBuilder = versatileTreatmentBuilder(fwd);
1176 if (ttBuilder == null) {
1177 return Collections.emptySet();
1178 }
1179
1180 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1181 .fromApp(fwd.appId())
1182 .withPriority(fwd.priority())
1183 .forDevice(deviceId)
1184 .withSelector(sbuilder.build())
1185 .withTreatment(ttBuilder.build())
1186 .makePermanent()
1187 .forTable(ACL_TABLE);
1188 return Collections.singletonList(ruleBuilder.build());
1189 }
1190
1191 /**
1192 * Helper function to create traffic selector builder for versatile forwarding objectives.
1193 *
1194 * @param fwd original forwarding objective
1195 * @return selector builder for the flow rule
1196 */
1197 protected TrafficSelector.Builder versatileSelectorBuilder(ForwardingObjective fwd) {
Saurav Das77b5e902016-01-27 17:01:59 -08001198 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
1199 fwd.selector().criteria().forEach(criterion -> {
1200 if (criterion instanceof VlanIdCriterion) {
1201 VlanId vlanId = ((VlanIdCriterion) criterion).vlanId();
1202 // ensure that match does not include vlan = NONE as OF-DPA does not
1203 // match untagged packets this way in the ACL table.
1204 if (vlanId.equals(VlanId.NONE)) {
1205 return;
1206 }
Charles Chand1172632017-03-15 17:33:09 -07001207 if (requireVlanExtensions()) {
1208 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanId);
1209 sbuilder.extension(ofdpaMatchVlanVid, deviceId);
1210 } else {
1211 sbuilder.matchVlanId(vlanId);
1212 }
Charles Chan09bf2692018-01-11 11:48:18 -08001213 } else if (criterion instanceof Icmpv6TypeCriterion) {
1214 byte icmpv6Type = (byte) ((Icmpv6TypeCriterion) criterion).icmpv6Type();
1215 sbuilder.matchIcmpv6Type(icmpv6Type);
1216 } else if (criterion instanceof Icmpv6CodeCriterion) {
1217 byte icmpv6Code = (byte) ((Icmpv6CodeCriterion) criterion).icmpv6Code();
1218 sbuilder.matchIcmpv6Type(icmpv6Code);
Charles Chand9e47c62017-10-05 15:17:15 -07001219 } else if (criterion instanceof TcpPortCriterion || criterion instanceof UdpPortCriterion) {
1220 // FIXME: QMX switches do not support L4 dst port matching in ACL table.
1221 // Currently L4 dst port matching is only used by DHCP relay feature
1222 // and therefore is safe to be replaced with L4 src port matching.
1223 // We need to revisit this if L4 dst port is used for other purpose in the future.
1224 if (!supportIpv6L4Dst() && isIpv6(fwd.selector())) {
Charles Chand9e47c62017-10-05 15:17:15 -07001225 switch (criterion.type()) {
1226 case UDP_DST:
Charles Chand9e47c62017-10-05 15:17:15 -07001227 case UDP_DST_MASKED:
Charles Chand9e47c62017-10-05 15:17:15 -07001228 case TCP_DST:
Charles Chand9e47c62017-10-05 15:17:15 -07001229 case TCP_DST_MASKED:
Charles Chand9e47c62017-10-05 15:17:15 -07001230 break;
1231 default:
1232 sbuilder.add(criterion);
1233 }
1234 } else {
1235 sbuilder.add(criterion);
1236 }
Saurav Das77b5e902016-01-27 17:01:59 -08001237 } else {
1238 sbuilder.add(criterion);
1239 }
1240 });
Charles Chanab591602019-01-22 17:25:04 -08001241 return sbuilder;
1242 }
Saurav Das77b5e902016-01-27 17:01:59 -08001243
Charles Chanab591602019-01-22 17:25:04 -08001244 /**
1245 * Helper function to create traffic treatment builder for versatile forwarding objectives.
1246 *
1247 * @param fwd original forwarding objective
1248 * @return treatment builder for the flow rule, or null if there is an error.
1249 */
1250 protected TrafficTreatment.Builder versatileTreatmentBuilder(ForwardingObjective fwd) {
Saurav Das822c4e22015-10-23 10:51:11 -07001251 // XXX driver does not currently do type checking as per Tables 65-67 in
1252 // OFDPA 2.0 spec. The only allowed treatment is a punt to the controller.
Saurav Das49cb5a12016-01-16 22:54:07 -08001253 TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder();
1254 if (fwd.treatment() != null) {
1255 for (Instruction ins : fwd.treatment().allInstructions()) {
1256 if (ins instanceof OutputInstruction) {
1257 OutputInstruction o = (OutputInstruction) ins;
Ray Milkeyfe0e0852018-01-18 11:14:05 -08001258 if (PortNumber.CONTROLLER.equals(o.port())) {
Saurav Das49cb5a12016-01-16 22:54:07 -08001259 ttBuilder.add(o);
1260 } else {
1261 log.warn("Only allowed treatments in versatile forwarding "
1262 + "objectives are punts to the controller");
1263 }
Charles Chanf76de302018-06-15 18:54:18 -07001264 } else if (ins instanceof NoActionInstruction) {
1265 // No action is allowed and nothing needs to be done
Saurav Das49cb5a12016-01-16 22:54:07 -08001266 } else {
1267 log.warn("Cannot process instruction in versatile fwd {}", ins);
1268 }
Saurav Das822c4e22015-10-23 10:51:11 -07001269 }
Charles Chan2df0e8a2017-01-09 11:45:08 -08001270 if (fwd.treatment().clearedDeferred()) {
1271 ttBuilder.wipeDeferred();
1272 }
Saurav Das822c4e22015-10-23 10:51:11 -07001273 }
Saurav Das822c4e22015-10-23 10:51:11 -07001274 if (fwd.nextId() != null) {
Charles Chanab591602019-01-22 17:25:04 -08001275 // Override case
Saurav Das49cb5a12016-01-16 22:54:07 -08001276 NextGroup next = getGroupForNextObjective(fwd.nextId());
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +05301277 if (next == null) {
1278 fail(fwd, ObjectiveError.BADPARAMS);
Charles Chanab591602019-01-22 17:25:04 -08001279 return null;
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +05301280 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001281 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1282 // we only need the top level group's key to point the flow to it
1283 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1284 if (group == null) {
1285 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
Charles Chanab591602019-01-22 17:25:04 -08001286 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
Saurav Das49cb5a12016-01-16 22:54:07 -08001287 fail(fwd, ObjectiveError.GROUPMISSING);
Charles Chanab591602019-01-22 17:25:04 -08001288 return null;
Saurav Das49cb5a12016-01-16 22:54:07 -08001289 }
1290 ttBuilder.deferred().group(group.id());
Saurav Das822c4e22015-10-23 10:51:11 -07001291 }
Charles Chanab591602019-01-22 17:25:04 -08001292 return ttBuilder;
Saurav Das822c4e22015-10-23 10:51:11 -07001293 }
1294
1295 /**
1296 * In the OF-DPA 2.0 pipeline, specific forwarding refers to the IP table
Saurav Das8a0732e2015-11-20 15:27:53 -08001297 * (unicast or multicast) or the L2 table (mac + vlan) or the MPLS table.
Saurav Das822c4e22015-10-23 10:51:11 -07001298 *
1299 * @param fwd the forwarding objective of type 'specific'
1300 * @return a collection of flow rules. Typically there will be only one
1301 * for this type of forwarding objective. An empty set may be
1302 * returned if there is an issue in processing the objective.
1303 */
Charles Chan50d900c2018-03-02 13:26:22 -08001304 private Collection<FlowRule> processSpecific(ForwardingObjective fwd) {
Saurav Das25190812016-05-27 13:54:07 -07001305 log.debug("Processing specific fwd objective:{} in dev:{} with next:{}",
Saurav Das4ce45962015-11-24 23:21:05 -08001306 fwd.id(), deviceId, fwd.nextId());
1307 boolean isEthTypeObj = isSupportedEthTypeObjective(fwd);
1308 boolean isEthDstObj = isSupportedEthDstObjective(fwd);
1309
1310 if (isEthTypeObj) {
1311 return processEthTypeSpecific(fwd);
1312 } else if (isEthDstObj) {
1313 return processEthDstSpecific(fwd);
1314 } else {
1315 log.warn("processSpecific: Unsupported forwarding objective "
1316 + "criteria fwd:{} in dev:{}", fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001317 fail(fwd, ObjectiveError.UNSUPPORTED);
1318 return Collections.emptySet();
1319 }
Saurav Das4ce45962015-11-24 23:21:05 -08001320 }
1321
Saurav Das4ce45962015-11-24 23:21:05 -08001322 /**
1323 * Handles forwarding rules to the IP and MPLS tables.
1324 *
1325 * @param fwd the forwarding objective
1326 * @return A collection of flow rules, or an empty set
1327 */
1328 protected Collection<FlowRule> processEthTypeSpecific(ForwardingObjective fwd) {
Charles Chancad338a2016-09-16 18:03:11 -07001329 return processEthTypeSpecificInternal(fwd, false, ACL_TABLE);
Charles Chanf9e98652016-09-07 16:54:23 -07001330 }
1331
1332 /**
1333 * Internal implementation of processEthTypeSpecific.
1334 * <p>
1335 * Wildcarded IPv4_DST is not supported in OFDPA i12. Therefore, we break
1336 * the rule into 0.0.0.0/1 and 128.0.0.0/1.
1337 * The allowDefaultRoute must be set to false for OFDPA i12.
1338 * </p>
1339 *
1340 * @param fwd the forwarding objective
1341 * @param allowDefaultRoute allow wildcarded IPv4_DST or not
Ray Milkey0bb1e102016-11-10 14:51:27 -08001342 * @param mplsNextTable next MPLS table
Charles Chanf9e98652016-09-07 16:54:23 -07001343 * @return A collection of flow rules, or an empty set
1344 */
1345 protected Collection<FlowRule> processEthTypeSpecificInternal(ForwardingObjective fwd,
Charles Chancad338a2016-09-16 18:03:11 -07001346 boolean allowDefaultRoute,
1347 int mplsNextTable) {
Saurav Das4ce45962015-11-24 23:21:05 -08001348 TrafficSelector selector = fwd.selector();
1349 EthTypeCriterion ethType =
1350 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001351 boolean emptyGroup = false;
Charles Chan188ebf52015-12-23 00:15:11 -08001352 int forTableId;
Saurav Das8a0732e2015-11-20 15:27:53 -08001353 TrafficSelector.Builder filteredSelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -08001354 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
Flavio Castroe10fa242016-01-15 12:43:51 -08001355 TrafficSelector.Builder complementarySelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -08001356
Saurav Das8a0732e2015-11-20 15:27:53 -08001357 if (ethType.ethType().toShort() == Ethernet.TYPE_IPV4) {
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001358 if (buildIpv4Selector(filteredSelector, complementarySelector, fwd, allowDefaultRoute) < 0) {
1359 return Collections.emptyList();
1360 }
1361 // We need to set properly the next table
Flavio Castroe10fa242016-01-15 12:43:51 -08001362 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
Charles Chan5b9df8d2016-03-28 22:21:40 -07001363 if (ipv4Dst.isMulticast()) {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001364 forTableId = MULTICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -08001365 } else {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001366 forTableId = UNICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -08001367 }
Charles Chan50d900c2018-03-02 13:26:22 -08001368
Charles Chan236653d2018-03-05 11:28:23 -08001369 // TODO decrementing IP ttl is done automatically for routing, this
Charles Chan50d900c2018-03-02 13:26:22 -08001370 // action is ignored or rejected in ofdpa as it is not fully implemented
Charles Chan236653d2018-03-05 11:28:23 -08001371 // if (fwd.treatment() != null) {
1372 // for (Instruction instr : fwd.treatment().allInstructions()) {
1373 // if (instr instanceof L3ModificationInstruction &&
1374 // ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1375 // tb.deferred().add(instr);
1376 // }
1377 // }
1378 // }
1379
Pier Ventree0ae7a32016-11-23 09:57:42 -08001380 } else if (ethType.ethType().toShort() == Ethernet.TYPE_IPV6) {
1381 if (buildIpv6Selector(filteredSelector, fwd) < 0) {
1382 return Collections.emptyList();
1383 }
Kalhee Kim3d26bf02017-10-04 14:59:11 +00001384 //We need to set the proper next table
1385 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
1386 if (ipv6Dst.isMulticast()) {
1387 forTableId = MULTICAST_ROUTING_TABLE;
1388 } else {
1389 forTableId = UNICAST_ROUTING_TABLE;
1390 }
1391
Charles Chan236653d2018-03-05 11:28:23 -08001392 // TODO decrementing IP ttl is done automatically for routing, this
Charles Chan50d900c2018-03-02 13:26:22 -08001393 // action is ignored or rejected in ofdpa as it is not fully implemented
Charles Chan236653d2018-03-05 11:28:23 -08001394 // if (fwd.treatment() != null) {
1395 // for (Instruction instr : fwd.treatment().allInstructions()) {
1396 // if (instr instanceof L3ModificationInstruction &&
1397 // ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1398 // tb.deferred().add(instr);
1399 // }
1400 // }
1401 // }
Saurav Das8a0732e2015-11-20 15:27:53 -08001402 } else {
1403 filteredSelector
1404 .matchEthType(Ethernet.MPLS_UNICAST)
1405 .matchMplsLabel(((MplsCriterion)
1406 selector.getCriterion(Criterion.Type.MPLS_LABEL)).label());
1407 MplsBosCriterion bos = (MplsBosCriterion) selector
Pier Ventre140a8942016-11-02 07:26:38 -07001408 .getCriterion(MPLS_BOS);
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001409 if (bos != null && requireMplsBosMatch()) {
Saurav Das8a0732e2015-11-20 15:27:53 -08001410 filteredSelector.matchMplsBos(bos.mplsBos());
1411 }
1412 forTableId = MPLS_TABLE_1;
Saurav Das4ce45962015-11-24 23:21:05 -08001413 log.debug("processing MPLS specific forwarding objective {} -> next:{}"
1414 + " in dev {}", fwd.id(), fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001415
Charles Chan14967c22015-12-07 11:11:50 -08001416 if (fwd.treatment() != null) {
1417 for (Instruction instr : fwd.treatment().allInstructions()) {
1418 if (instr instanceof L2ModificationInstruction &&
1419 ((L2ModificationInstruction) instr).subtype() == L2SubType.MPLS_POP) {
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001420 // OF-DPA does not pop in MPLS table in some cases. For the L3 VPN, it requires
Saurav Das9c705342017-01-06 11:36:01 -08001421 // setting the MPLS_TYPE so pop can happen down the pipeline
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001422 if (requireMplsPop()) {
1423 if (mplsNextTable == MPLS_TYPE_TABLE && isNotMplsBos(selector)) {
1424 tb.immediate().popMpls();
1425 }
1426 } else {
1427 // Skip mpls pop action for mpls_unicast label
1428 if (instr instanceof ModMplsHeaderInstruction &&
1429 !((ModMplsHeaderInstruction) instr).ethernetType()
1430 .equals(EtherType.MPLS_UNICAST.ethType())) {
1431 tb.immediate().add(instr);
1432 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001433 }
Charles Chan14967c22015-12-07 11:11:50 -08001434 }
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001435
1436 if (requireMplsTtlModification()) {
1437 if (instr instanceof L3ModificationInstruction &&
1438 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1439 // FIXME Should modify the app to send the correct DEC_MPLS_TTL instruction
1440 tb.immediate().decMplsTtl();
1441 }
1442 if (instr instanceof L3ModificationInstruction &&
1443 ((L3ModificationInstruction) instr).subtype() == L3SubType.TTL_IN) {
1444 tb.immediate().add(instr);
1445 }
Charles Chan14967c22015-12-07 11:11:50 -08001446 }
Saurav Das8a0732e2015-11-20 15:27:53 -08001447 }
1448 }
1449 }
Saurav Das822c4e22015-10-23 10:51:11 -07001450
1451 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001452 NextGroup next = getGroupForNextObjective(fwd.nextId());
1453 if (next != null) {
1454 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1455 // we only need the top level group's key to point the flow to it
1456 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1457 if (group == null) {
1458 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1459 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1460 fail(fwd, ObjectiveError.GROUPMISSING);
1461 return Collections.emptySet();
1462 }
Saurav Dasa4020382018-02-14 14:14:54 -08001463 if (isNotMplsBos(selector) && group.type().equals(SELECT)) {
1464 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
1465 + "is not implemented in OF-DPA yet. Aborting flow {} -> next:{} "
1466 + "in this device {}", fwd.id(), fwd.nextId(), deviceId);
1467 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1468 return Collections.emptySet();
1469 }
Saurav Das423fe2b2015-12-04 10:52:59 -08001470 tb.deferred().group(group.id());
Saurav Dasc568c342018-01-25 09:49:01 -08001471 // retrying flows may be necessary due to bug CORD-554
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001472 if (gkeys.size() == 1 && gkeys.get(0).size() == 1) {
Saurav Dasc568c342018-01-25 09:49:01 -08001473 if (shouldRetry()) {
1474 log.warn("Found empty group 0x{} in dev:{} .. will retry fwd:{}",
1475 Integer.toHexString(group.id().id()), deviceId,
1476 fwd.id());
1477 emptyGroup = true;
1478 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001479 }
Saurav Das25190812016-05-27 13:54:07 -07001480 } else {
1481 log.warn("Cannot find group for nextId:{} in dev:{}. Aborting fwd:{}",
1482 fwd.nextId(), deviceId, fwd.id());
1483 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1484 return Collections.emptySet();
Saurav Das822c4e22015-10-23 10:51:11 -07001485 }
Saurav Das822c4e22015-10-23 10:51:11 -07001486 }
Charles Chancad338a2016-09-16 18:03:11 -07001487
1488 if (forTableId == MPLS_TABLE_1) {
Pier Ventre140a8942016-11-02 07:26:38 -07001489 if (mplsNextTable == MPLS_L3_TYPE_TABLE) {
Charles Chancad338a2016-09-16 18:03:11 -07001490 Ofdpa3SetMplsType setMplsType = new Ofdpa3SetMplsType(Ofdpa3MplsType.L3_PHP);
Saurav Das9c705342017-01-06 11:36:01 -08001491 // set mpls type as apply_action
1492 tb.immediate().extension(setMplsType, deviceId);
Charles Chancad338a2016-09-16 18:03:11 -07001493 }
1494 tb.transition(mplsNextTable);
1495 } else {
1496 tb.transition(ACL_TABLE);
1497 }
1498
Andrea Campanellad980c6d2018-04-30 11:48:55 +02001499 if (fwd.treatment() != null && fwd.treatment().clearedDeferred()) {
1500 if (supportsUnicastBlackHole()) {
1501 tb.wipeDeferred();
1502 } else {
1503 log.warn("Clear Deferred is not supported Unicast Routing Table on device {}", deviceId);
1504 return Collections.emptySet();
1505 }
1506 }
1507
Saurav Das822c4e22015-10-23 10:51:11 -07001508 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1509 .fromApp(fwd.appId())
1510 .withPriority(fwd.priority())
1511 .forDevice(deviceId)
Saurav Das8a0732e2015-11-20 15:27:53 -08001512 .withSelector(filteredSelector.build())
1513 .withTreatment(tb.build())
1514 .forTable(forTableId);
Saurav Das822c4e22015-10-23 10:51:11 -07001515
1516 if (fwd.permanent()) {
1517 ruleBuilder.makePermanent();
1518 } else {
1519 ruleBuilder.makeTemporary(fwd.timeout());
1520 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001521 Collection<FlowRule> flowRuleCollection = new ArrayList<>();
1522 flowRuleCollection.add(ruleBuilder.build());
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001523 if (!allowDefaultRoute) {
Pier Ventree0ae7a32016-11-23 09:57:42 -08001524 flowRuleCollection.add(
1525 defaultRoute(fwd, complementarySelector, forTableId, tb)
1526 );
Flavio Castroe10fa242016-01-15 12:43:51 -08001527 log.debug("Default rule 0.0.0.0/0 is being installed two rules");
1528 }
Saurav Dasc568c342018-01-25 09:49:01 -08001529
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001530 if (emptyGroup) {
1531 executorService.schedule(new RetryFlows(fwd, flowRuleCollection),
1532 RETRY_MS, TimeUnit.MILLISECONDS);
1533 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001534 return flowRuleCollection;
Saurav Das822c4e22015-10-23 10:51:11 -07001535 }
1536
Charles Chan50d900c2018-03-02 13:26:22 -08001537 private int buildIpv4Selector(TrafficSelector.Builder builderToUpdate,
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001538 TrafficSelector.Builder extBuilder,
1539 ForwardingObjective fwd,
1540 boolean allowDefaultRoute) {
1541 TrafficSelector selector = fwd.selector();
1542
1543 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
1544 if (ipv4Dst.isMulticast()) {
1545 if (ipv4Dst.prefixLength() != 32) {
1546 log.warn("Multicast specific forwarding objective can only be /32");
1547 fail(fwd, ObjectiveError.BADPARAMS);
1548 return -1;
1549 }
1550 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
1551 if (assignedVlan == null) {
1552 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
1553 fail(fwd, ObjectiveError.BADPARAMS);
1554 return -1;
1555 }
Charles Chand1172632017-03-15 17:33:09 -07001556 if (requireVlanExtensions()) {
1557 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
1558 builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
1559 } else {
1560 builderToUpdate.matchVlanId(assignedVlan);
1561 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001562 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1563 log.debug("processing IPv4 multicast specific forwarding objective {} -> next:{}"
1564 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1565 } else {
1566 if (ipv4Dst.prefixLength() == 0) {
1567 if (allowDefaultRoute) {
1568 // The entire IPV4_DST field is wildcarded intentionally
1569 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4);
1570 } else {
1571 // NOTE: The switch does not support matching 0.0.0.0/0
1572 // Split it into 0.0.0.0/1 and 128.0.0.0/1
1573 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4)
1574 .matchIPDst(IpPrefix.valueOf("0.0.0.0/1"));
1575 extBuilder.matchEthType(Ethernet.TYPE_IPV4)
1576 .matchIPDst(IpPrefix.valueOf("128.0.0.0/1"));
1577 }
1578 } else {
1579 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1580 }
1581 log.debug("processing IPv4 unicast specific forwarding objective {} -> next:{}"
1582 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1583 }
1584 return 0;
1585 }
1586
1587 /**
1588 * Helper method to build Ipv6 selector using the selector provided by
1589 * a forwarding objective.
1590 *
1591 * @param builderToUpdate the builder to update
1592 * @param fwd the selector to read
1593 * @return 0 if the update ends correctly. -1 if the matches
1594 * are not yet supported
1595 */
Charles Chan50d900c2018-03-02 13:26:22 -08001596 int buildIpv6Selector(TrafficSelector.Builder builderToUpdate,
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001597 ForwardingObjective fwd) {
1598
1599 TrafficSelector selector = fwd.selector();
1600
1601 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
1602 if (ipv6Dst.isMulticast()) {
Julia Ferguson65428c32017-08-10 18:15:24 +00001603 if (ipv6Dst.prefixLength() != IpAddress.INET6_BIT_LENGTH) {
1604 log.warn("Multicast specific forwarding objective can only be /128");
1605 fail(fwd, ObjectiveError.BADPARAMS);
1606 return -1;
1607 }
1608 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
1609 if (assignedVlan == null) {
1610 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
1611 fail(fwd, ObjectiveError.BADPARAMS);
1612 return -1;
1613 }
1614 if (requireVlanExtensions()) {
1615 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
1616 builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
1617 } else {
1618 builderToUpdate.matchVlanId(assignedVlan);
1619 }
1620 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6).matchIPv6Dst(ipv6Dst);
1621 log.debug("processing IPv6 multicast specific forwarding objective {} -> next:{}"
1622 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1623 } else {
1624 if (ipv6Dst.prefixLength() != 0) {
1625 builderToUpdate.matchIPv6Dst(ipv6Dst);
1626 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001627 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6);
1628 log.debug("processing IPv6 unicast specific forwarding objective {} -> next:{}"
1629 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Julia Ferguson65428c32017-08-10 18:15:24 +00001630 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001631 return 0;
1632 }
1633
Charles Chan50d900c2018-03-02 13:26:22 -08001634 FlowRule defaultRoute(ForwardingObjective fwd,
Pier Ventree0ae7a32016-11-23 09:57:42 -08001635 TrafficSelector.Builder complementarySelector,
1636 int forTableId,
1637 TrafficTreatment.Builder tb) {
1638 FlowRule.Builder rule = DefaultFlowRule.builder()
1639 .fromApp(fwd.appId())
1640 .withPriority(fwd.priority())
1641 .forDevice(deviceId)
1642 .withSelector(complementarySelector.build())
1643 .withTreatment(tb.build())
1644 .forTable(forTableId);
1645 if (fwd.permanent()) {
1646 rule.makePermanent();
1647 } else {
1648 rule.makeTemporary(fwd.timeout());
1649 }
1650 return rule.build();
1651 }
1652
Saurav Das4ce45962015-11-24 23:21:05 -08001653 /**
1654 * Handles forwarding rules to the L2 bridging table. Flow actions are not
1655 * allowed in the bridging table - instead we use L2 Interface group or
1656 * L2 flood group
1657 *
1658 * @param fwd the forwarding objective
1659 * @return A collection of flow rules, or an empty set
1660 */
1661 protected Collection<FlowRule> processEthDstSpecific(ForwardingObjective fwd) {
1662 List<FlowRule> rules = new ArrayList<>();
1663
1664 // Build filtered selector
1665 TrafficSelector selector = fwd.selector();
1666 EthCriterion ethCriterion = (EthCriterion) selector
1667 .getCriterion(Criterion.Type.ETH_DST);
1668 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) selector
1669 .getCriterion(Criterion.Type.VLAN_VID);
1670
1671 if (vlanIdCriterion == null) {
1672 log.warn("Forwarding objective for bridging requires vlan. Not "
1673 + "installing fwd:{} in dev:{}", fwd.id(), deviceId);
1674 fail(fwd, ObjectiveError.BADPARAMS);
1675 return Collections.emptySet();
1676 }
1677
1678 TrafficSelector.Builder filteredSelectorBuilder =
1679 DefaultTrafficSelector.builder();
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001680
1681 if (!ethCriterion.mac().equals(NONE) &&
1682 !ethCriterion.mac().equals(BROADCAST)) {
Saurav Das4ce45962015-11-24 23:21:05 -08001683 filteredSelectorBuilder.matchEthDst(ethCriterion.mac());
1684 log.debug("processing L2 forwarding objective:{} -> next:{} in dev:{}",
1685 fwd.id(), fwd.nextId(), deviceId);
1686 } else {
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001687 // Use wildcard DST_MAC if the MacAddress is None or Broadcast
Saurav Das4ce45962015-11-24 23:21:05 -08001688 log.debug("processing L2 Broadcast forwarding objective:{} -> next:{} "
1689 + "in dev:{} for vlan:{}",
1690 fwd.id(), fwd.nextId(), deviceId, vlanIdCriterion.vlanId());
1691 }
Charles Chand1172632017-03-15 17:33:09 -07001692 if (requireVlanExtensions()) {
1693 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanIdCriterion.vlanId());
1694 filteredSelectorBuilder.extension(ofdpaMatchVlanVid, deviceId);
1695 } else {
1696 filteredSelectorBuilder.matchVlanId(vlanIdCriterion.vlanId());
1697 }
Saurav Das4ce45962015-11-24 23:21:05 -08001698 TrafficSelector filteredSelector = filteredSelectorBuilder.build();
1699
1700 if (fwd.treatment() != null) {
1701 log.warn("Ignoring traffic treatment in fwd rule {} meant for L2 table"
1702 + "for dev:{}. Expecting only nextId", fwd.id(), deviceId);
1703 }
1704
1705 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1706 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001707 NextGroup next = getGroupForNextObjective(fwd.nextId());
Saurav Das4ce45962015-11-24 23:21:05 -08001708 if (next != null) {
1709 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1710 // we only need the top level group's key to point the flow to it
1711 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1712 if (group != null) {
1713 treatmentBuilder.deferred().group(group.id());
1714 } else {
1715 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1716 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1717 fail(fwd, ObjectiveError.GROUPMISSING);
1718 return Collections.emptySet();
1719 }
1720 }
1721 }
1722 treatmentBuilder.immediate().transition(ACL_TABLE);
1723 TrafficTreatment filteredTreatment = treatmentBuilder.build();
1724
1725 // Build bridging table entries
1726 FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder();
1727 flowRuleBuilder.fromApp(fwd.appId())
1728 .withPriority(fwd.priority())
1729 .forDevice(deviceId)
1730 .withSelector(filteredSelector)
1731 .withTreatment(filteredTreatment)
1732 .forTable(BRIDGING_TABLE);
1733 if (fwd.permanent()) {
1734 flowRuleBuilder.makePermanent();
1735 } else {
1736 flowRuleBuilder.makeTemporary(fwd.timeout());
1737 }
1738 rules.add(flowRuleBuilder.build());
1739 return rules;
1740 }
1741
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001742 //////////////////////////////////////
1743 // Helper Methods and Classes
1744 //////////////////////////////////////
1745
1746 private boolean isSupportedEthTypeObjective(ForwardingObjective fwd) {
1747 TrafficSelector selector = fwd.selector();
1748 EthTypeCriterion ethType = (EthTypeCriterion) selector
1749 .getCriterion(Criterion.Type.ETH_TYPE);
1750 return !((ethType == null) ||
1751 ((ethType.ethType().toShort() != Ethernet.TYPE_IPV4) &&
Pier Ventree0ae7a32016-11-23 09:57:42 -08001752 (ethType.ethType().toShort() != Ethernet.MPLS_UNICAST)) &&
1753 (ethType.ethType().toShort() != Ethernet.TYPE_IPV6));
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001754 }
1755
1756 private boolean isSupportedEthDstObjective(ForwardingObjective fwd) {
1757 TrafficSelector selector = fwd.selector();
1758 EthCriterion ethDst = (EthCriterion) selector
1759 .getCriterion(Criterion.Type.ETH_DST);
1760 VlanIdCriterion vlanId = (VlanIdCriterion) selector
1761 .getCriterion(Criterion.Type.VLAN_VID);
1762 return !(ethDst == null && vlanId == null);
1763 }
1764
Charles Chan50d900c2018-03-02 13:26:22 -08001765 NextGroup getGroupForNextObjective(Integer nextId) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001766 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
1767 if (next != null) {
1768 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1769 if (gkeys != null && !gkeys.isEmpty()) {
1770 return next;
1771 } else {
1772 log.warn("Empty next group found in FlowObjective store for "
1773 + "next-id:{} in dev:{}", nextId, deviceId);
1774 }
1775 } else {
1776 log.warn("next-id {} not found in Flow objective store for dev:{}",
1777 nextId, deviceId);
1778 }
1779 return null;
1780 }
1781
Charles Chan188ebf52015-12-23 00:15:11 -08001782 protected static void pass(Objective obj) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001783 obj.context().ifPresent(context -> context.onSuccess(obj));
Saurav Das822c4e22015-10-23 10:51:11 -07001784 }
1785
Charles Chan188ebf52015-12-23 00:15:11 -08001786 protected static void fail(Objective obj, ObjectiveError error) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001787 obj.context().ifPresent(context -> context.onError(obj, error));
Saurav Das822c4e22015-10-23 10:51:11 -07001788 }
Saurav Das24431192016-03-07 19:13:00 -08001789
Saurav Das24431192016-03-07 19:13:00 -08001790 @Override
1791 public List<String> getNextMappings(NextGroup nextGroup) {
1792 List<String> mappings = new ArrayList<>();
1793 List<Deque<GroupKey>> gkeys = appKryo.deserialize(nextGroup.data());
1794 for (Deque<GroupKey> gkd : gkeys) {
1795 Group lastGroup = null;
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -07001796 StringBuilder gchain = new StringBuilder();
Saurav Das24431192016-03-07 19:13:00 -08001797 for (GroupKey gk : gkd) {
1798 Group g = groupService.getGroup(deviceId, gk);
Saurav Das8be4e3a2016-03-11 17:19:07 -08001799 if (g == null) {
Saurav Das25190812016-05-27 13:54:07 -07001800 gchain.append(" NoGrp").append(" -->");
Saurav Das8be4e3a2016-03-11 17:19:07 -08001801 continue;
1802 }
1803 gchain.append(" 0x").append(Integer.toHexString(g.id().id()))
1804 .append(" -->");
Saurav Das24431192016-03-07 19:13:00 -08001805 lastGroup = g;
1806 }
1807 // add port information for last group in group-chain
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -07001808 List<Instruction> lastGroupIns = new ArrayList<>();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001809 if (lastGroup != null && !lastGroup.buckets().buckets().isEmpty()) {
Saurav Das25190812016-05-27 13:54:07 -07001810 lastGroupIns = lastGroup.buckets().buckets().get(0)
1811 .treatment().allInstructions();
1812 }
1813 for (Instruction i: lastGroupIns) {
Saurav Das24431192016-03-07 19:13:00 -08001814 if (i instanceof OutputInstruction) {
Saurav Das8be4e3a2016-03-11 17:19:07 -08001815 gchain.append(" port:").append(((OutputInstruction) i).port());
Saurav Das24431192016-03-07 19:13:00 -08001816 }
1817 }
Saurav Das8be4e3a2016-03-11 17:19:07 -08001818 mappings.add(gchain.toString());
Saurav Das24431192016-03-07 19:13:00 -08001819 }
1820 return mappings;
1821 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001822
Saurav Dasa4020382018-02-14 14:14:54 -08001823 /**
1824 * Returns true iff the given selector matches on BOS==true, indicating that
1825 * the selector is trying to match on a label that is bottom-of-stack.
1826 *
1827 * @param selector the given match
1828 * @return true iff BoS==true; false if BOS==false, or BOS matching is not
1829 * expressed in the given selector
1830 */
Pier Ventre140a8942016-11-02 07:26:38 -07001831 static boolean isMplsBos(TrafficSelector selector) {
1832 MplsBosCriterion bosCriterion = (MplsBosCriterion) selector.getCriterion(MPLS_BOS);
1833 return bosCriterion != null && bosCriterion.mplsBos();
1834 }
1835
Saurav Dasa4020382018-02-14 14:14:54 -08001836 /**
1837 * Returns true iff the given selector matches on BOS==false, indicating
1838 * that the selector is trying to match on a label that is not the
1839 * bottom-of-stack label.
1840 *
1841 * @param selector the given match
1842 * @return true iff BoS==false;
1843 * false if BOS==true, or BOS matching is not expressed in the given selector
1844 */
Pier Ventre140a8942016-11-02 07:26:38 -07001845 static boolean isNotMplsBos(TrafficSelector selector) {
1846 MplsBosCriterion bosCriterion = (MplsBosCriterion) selector.getCriterion(MPLS_BOS);
1847 return bosCriterion != null && !bosCriterion.mplsBos();
1848 }
1849
Saurav Dasa4020382018-02-14 14:14:54 -08001850 /**
1851 * Returns true iff the forwarding objective includes a treatment to pop the
1852 * MPLS label.
1853 *
1854 * @param fwd the given forwarding objective
1855 * @return true iff mpls pop treatment exists
1856 */
1857 static boolean isMplsPop(ForwardingObjective fwd) {
1858 if (fwd.treatment() != null) {
1859 for (Instruction instr : fwd.treatment().allInstructions()) {
1860 if (instr instanceof L2ModificationInstruction
1861 && ((L2ModificationInstruction) instr)
1862 .subtype() == L2SubType.MPLS_POP) {
1863 return true;
1864 }
1865 }
1866 }
1867 return false;
1868 }
1869
Charles Chand9e47c62017-10-05 15:17:15 -07001870 private static boolean isIpv6(TrafficSelector selector) {
1871 EthTypeCriterion ethTypeCriterion = (EthTypeCriterion) selector.getCriterion(ETH_TYPE);
1872 return ethTypeCriterion != null && ethTypeCriterion.ethType().toShort() == Ethernet.TYPE_IPV6;
1873 }
1874
Charles Chan50d900c2018-03-02 13:26:22 -08001875 static VlanId readVlanFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001876 if (selector == null) {
1877 return null;
1878 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001879 Criterion criterion = selector.getCriterion(Criterion.Type.VLAN_VID);
1880 return (criterion == null)
1881 ? null : ((VlanIdCriterion) criterion).vlanId();
1882 }
1883
jayakumarthazhath655b9a82018-10-01 00:51:54 +05301884 static MacAddress readEthDstFromSelector(TrafficSelector selector) {
1885 if (selector == null) {
1886 return null;
1887 }
1888 Criterion criterion = selector.getCriterion(Criterion.Type.ETH_DST);
1889 return (criterion == null)
1890 ? null : ((EthCriterion) criterion).mac();
1891 }
1892
Charles Chand9e47c62017-10-05 15:17:15 -07001893 static IpPrefix readIpDstFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001894 if (selector == null) {
1895 return null;
1896 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001897 Criterion criterion = selector.getCriterion(Criterion.Type.IPV4_DST);
1898 return (criterion == null) ? null : ((IPCriterion) criterion).ip();
1899 }
Charles Chand55e84d2016-03-30 17:54:24 -07001900
1901 private static VlanId readVlanFromTreatment(TrafficTreatment treatment) {
Saurav Das59232cf2016-04-27 18:35:50 -07001902 if (treatment == null) {
1903 return null;
1904 }
Charles Chand55e84d2016-03-30 17:54:24 -07001905 for (Instruction i : treatment.allInstructions()) {
1906 if (i instanceof ModVlanIdInstruction) {
1907 return ((ModVlanIdInstruction) i).vlanId();
1908 }
1909 }
1910 return null;
1911 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001912
Jonghwan Hyun800d9d02018-04-09 09:40:50 -07001913 protected static MacAddress readEthDstFromTreatment(TrafficTreatment treatment) {
Charles Chan45b69ab2018-03-02 15:41:41 -08001914 if (treatment == null) {
1915 return null;
1916 }
1917 for (Instruction i : treatment.allInstructions()) {
1918 if (i instanceof ModEtherInstruction) {
1919 ModEtherInstruction modEtherInstruction = (ModEtherInstruction) i;
1920 if (modEtherInstruction.subtype() == L2SubType.ETH_DST) {
1921 return modEtherInstruction.mac();
1922 }
1923 }
1924 }
1925 return null;
1926 }
1927
jayakumarthazhath655b9a82018-10-01 00:51:54 +05301928 static ExtensionSelector readExtensionFromSelector(TrafficSelector selector) {
1929 if (selector == null) {
1930 return null;
1931 }
1932 ExtensionCriterion criterion = (ExtensionCriterion) selector.getCriterion(Criterion.Type.EXTENSION);
1933 return (criterion == null) ? null : criterion.extensionSelector();
1934 }
1935
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001936 /**
1937 * Utility class that retries sending flows a fixed number of times, even if
1938 * some of the attempts are successful. Used only for forwarding objectives.
1939 */
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001940 public final class RetryFlows implements Runnable {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001941 int attempts = MAX_RETRY_ATTEMPTS;
1942 private Collection<FlowRule> retryFlows;
1943 private ForwardingObjective fwd;
1944
Charles Chan50d900c2018-03-02 13:26:22 -08001945 RetryFlows(ForwardingObjective fwd, Collection<FlowRule> retryFlows) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001946 this.fwd = fwd;
1947 this.retryFlows = retryFlows;
1948 }
1949
1950 @Override
1951 public void run() {
1952 log.info("RETRY FLOWS ATTEMPT# {} for fwd:{} rules:{}",
1953 MAX_RETRY_ATTEMPTS - attempts, fwd.id(), retryFlows.size());
1954 sendForward(fwd, retryFlows);
1955 if (--attempts > 0) {
1956 executorService.schedule(this, RETRY_MS, TimeUnit.MILLISECONDS);
1957 }
1958 }
1959 }
1960
Saurav Das822c4e22015-10-23 10:51:11 -07001961}