blob: eae78fd8c59c94aec601044dd307b9264d1474ed [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
Saurav Das822c4e22015-10-23 10:51:11 -0700299 //////////////////////////////////////
300 // Flow Objectives
301 //////////////////////////////////////
302
303 @Override
304 public void filter(FilteringObjective filteringObjective) {
305 if (filteringObjective.type() == FilteringObjective.Type.PERMIT) {
306 processFilter(filteringObjective,
307 filteringObjective.op() == Objective.Operation.ADD,
308 filteringObjective.appId());
309 } else {
310 // Note that packets that don't match the PERMIT filter are
311 // automatically denied. The DENY filter is used to deny packets
312 // that are otherwise permitted by the PERMIT filter.
313 // Use ACL table flow rules here for DENY filtering objectives
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530314 log.warn("filter objective other than PERMIT currently not supported");
Saurav Das822c4e22015-10-23 10:51:11 -0700315 fail(filteringObjective, ObjectiveError.UNSUPPORTED);
316 }
317 }
318
319 @Override
320 public void forward(ForwardingObjective fwd) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700321 Collection<FlowRule> rules = processForward(fwd);
Saurav Das25190812016-05-27 13:54:07 -0700322 if (rules == null || rules.isEmpty()) {
323 // Assumes fail message has already been generated to the objective
324 // context. Returning here prevents spurious pass message to be
325 // generated by FlowRule service for empty flowOps.
326 return;
327 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700328 sendForward(fwd, rules);
329 }
330
Charles Chan50d900c2018-03-02 13:26:22 -0800331 private void sendForward(ForwardingObjective fwd, Collection<FlowRule> rules) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700332 FlowRuleOperations.Builder flowOpsBuilder = FlowRuleOperations.builder();
Saurav Das822c4e22015-10-23 10:51:11 -0700333 switch (fwd.op()) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700334 case ADD:
335 rules.stream()
336 .filter(Objects::nonNull)
337 .forEach(flowOpsBuilder::add);
Saurav Dasd2fded02016-12-02 15:43:47 -0800338 log.debug("Applying a add fwd-obj {} to sw:{}", fwd.id(), deviceId);
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700339 break;
340 case REMOVE:
341 rules.stream()
342 .filter(Objects::nonNull)
343 .forEach(flowOpsBuilder::remove);
Pier Ventre42287df2016-11-09 14:17:26 -0800344 log.debug("Deleting a flow rule to sw:{}", deviceId);
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700345 break;
346 default:
347 fail(fwd, ObjectiveError.UNKNOWN);
348 log.warn("Unknown forwarding type {}", fwd.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700349 }
350
Saurav Das822c4e22015-10-23 10:51:11 -0700351 flowRuleService.apply(flowOpsBuilder.build(new FlowRuleOperationsContext() {
352 @Override
353 public void onSuccess(FlowRuleOperations ops) {
354 pass(fwd);
355 }
356
357 @Override
358 public void onError(FlowRuleOperations ops) {
359 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
360 }
361 }));
Saurav Das822c4e22015-10-23 10:51:11 -0700362 }
363
364 @Override
365 public void next(NextObjective nextObjective) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800366 NextGroup nextGroup = flowObjectiveStore.getNextGroup(nextObjective.id());
367 switch (nextObjective.op()) {
368 case ADD:
Saurav Das4f980082015-11-05 13:39:15 -0800369 if (nextGroup != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800370 log.warn("Cannot add next {} that already exists in device {}",
371 nextObjective.id(), deviceId);
372 return;
373 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700374 log.debug("Processing NextObjective id {} in dev {} - add group",
Saurav Das8a0732e2015-11-20 15:27:53 -0800375 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700376 groupHandler.addGroup(nextObjective);
Saurav Das8a0732e2015-11-20 15:27:53 -0800377 break;
378 case ADD_TO_EXISTING:
379 if (nextGroup != null) {
Saurav Das1547b3f2017-05-05 17:01:08 -0700380 log.debug("Processing NextObjective id {} in dev {} - add bucket",
Saurav Das8a0732e2015-11-20 15:27:53 -0800381 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700382 groupHandler.addBucketToGroup(nextObjective, nextGroup);
Saurav Das4f980082015-11-05 13:39:15 -0800383 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800384 // it is possible that group-chain has not been fully created yet
Saurav Das423fe2b2015-12-04 10:52:59 -0800385 log.debug("Waiting to add bucket to group for next-id:{} in dev:{}",
386 nextObjective.id(), deviceId);
Yi Tseng47f82dc2017-03-05 22:48:39 -0800387
388 // by design multiple pending bucket is allowed for the group
389 groupHandler.pendingBuckets.compute(nextObjective.id(), (nextId, pendBkts) -> {
390 if (pendBkts == null) {
391 pendBkts = Sets.newHashSet();
392 }
393 pendBkts.add(nextObjective);
394 return pendBkts;
395 });
Saurav Das4f980082015-11-05 13:39:15 -0800396 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800397 break;
398 case REMOVE:
399 if (nextGroup == null) {
400 log.warn("Cannot remove next {} that does not exist in device {}",
401 nextObjective.id(), deviceId);
402 return;
403 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700404 log.debug("Processing NextObjective id {} in dev {} - remove group",
Saurav Das8a0732e2015-11-20 15:27:53 -0800405 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700406 groupHandler.removeGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800407 break;
408 case REMOVE_FROM_EXISTING:
409 if (nextGroup == null) {
410 log.warn("Cannot remove from next {} that does not exist in device {}",
411 nextObjective.id(), deviceId);
412 return;
413 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700414 log.debug("Processing NextObjective id {} in dev {} - remove bucket",
Saurav Das8a0732e2015-11-20 15:27:53 -0800415 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700416 groupHandler.removeBucketFromGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800417 break;
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +0900418 case MODIFY:
419 if (nextGroup == null) {
420 log.warn("Cannot modify next {} that does not exist in device {}",
421 nextObjective.id(), deviceId);
422 return;
423 }
424 log.debug("Processing NextObjective id {} in dev {} - modify bucket",
425 nextObjective.id(), deviceId);
426 groupHandler.modifyBucketFromGroup(nextObjective, nextGroup);
427 break;
Saurav Dasceccf242017-08-03 18:30:35 -0700428 case VERIFY:
429 if (nextGroup == null) {
430 log.warn("Cannot verify next {} that does not exist in device {}",
431 nextObjective.id(), deviceId);
432 return;
433 }
434 log.debug("Processing NextObjective id {} in dev {} - verify",
435 nextObjective.id(), deviceId);
436 groupHandler.verifyGroup(nextObjective, nextGroup);
437 break;
Saurav Das8a0732e2015-11-20 15:27:53 -0800438 default:
Saurav Das4f980082015-11-05 13:39:15 -0800439 log.warn("Unsupported operation {}", nextObjective.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700440 }
441 }
442
443 //////////////////////////////////////
444 // Flow handling
445 //////////////////////////////////////
446
447 /**
Yi Tseng0fdc07e2017-04-24 19:17:08 -0700448 * As per OFDPA 2.0 TTP, filtering of VLAN ids and MAC addresses (for routing)
449 * configured on switch ports happen in different tables.
Saurav Das822c4e22015-10-23 10:51:11 -0700450 *
451 * @param filt the filtering objective
452 * @param install indicates whether to add or remove the objective
453 * @param applicationId the application that sent this objective
454 */
Saurav Das52025962016-01-28 22:30:01 -0800455 protected void processFilter(FilteringObjective filt,
456 boolean install, ApplicationId applicationId) {
Saurav Das822c4e22015-10-23 10:51:11 -0700457 // This driver only processes filtering criteria defined with switch
458 // ports as the key
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530459 PortCriterion portCriterion = null;
Saurav Das822c4e22015-10-23 10:51:11 -0700460 EthCriterion ethCriterion = null;
461 VlanIdCriterion vidCriterion = null;
Saurav Das822c4e22015-10-23 10:51:11 -0700462 if (!filt.key().equals(Criteria.dummy()) &&
463 filt.key().type() == Criterion.Type.IN_PORT) {
464 portCriterion = (PortCriterion) filt.key();
Saurav Das822c4e22015-10-23 10:51:11 -0700465 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530466 if (portCriterion == null) {
467 log.debug("No IN_PORT defined in filtering objective from app: {}",
468 applicationId);
469 } else {
470 log.debug("Received filtering objective for dev/port: {}/{}", deviceId,
471 portCriterion.port());
472 }
Saurav Das822c4e22015-10-23 10:51:11 -0700473 // convert filtering conditions for switch-intfs into flowrules
474 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
475 for (Criterion criterion : filt.conditions()) {
Yi Tseng0fdc07e2017-04-24 19:17:08 -0700476 switch (criterion.type()) {
477 case ETH_DST:
478 case ETH_DST_MASKED:
479 ethCriterion = (EthCriterion) criterion;
480 break;
481 case VLAN_VID:
482 vidCriterion = (VlanIdCriterion) criterion;
483 break;
484 default:
485 log.warn("Unsupported filter {}", criterion);
486 fail(filt, ObjectiveError.UNSUPPORTED);
487 return;
Saurav Das822c4e22015-10-23 10:51:11 -0700488 }
489 }
490
Saurav Das0e99e2b2015-10-28 12:39:42 -0700491 VlanId assignedVlan = null;
Charles Chane849c192016-01-11 18:28:54 -0800492 if (vidCriterion != null) {
Charles Chand55e84d2016-03-30 17:54:24 -0700493 // Use the VLAN in criterion if metadata is not present and the traffic is tagged
Charles Chanc550f2e2017-12-19 19:55:57 -0800494 if (!vidCriterion.vlanId().equals(VlanId.NONE)) {
Charles Chane849c192016-01-11 18:28:54 -0800495 assignedVlan = vidCriterion.vlanId();
Piere5bff482018-03-07 11:42:50 +0100496 }
497 // If the meta VLAN is present let's update the assigned vlan
498 if (filt.meta() != null) {
499 VlanId metaVlan = readVlanFromTreatment(filt.meta());
500 if (metaVlan != null) {
501 assignedVlan = metaVlan;
502 }
Charles Chand55e84d2016-03-30 17:54:24 -0700503 }
Charles Chane849c192016-01-11 18:28:54 -0800504
Charles Chand55e84d2016-03-30 17:54:24 -0700505 if (assignedVlan == null) {
506 log.error("Driver fails to extract VLAN information. "
Frank Wangd8ab0962017-08-11 11:09:30 +0800507 + "Not processing VLAN filters on device {}.", deviceId);
Charles Chand55e84d2016-03-30 17:54:24 -0700508 log.debug("VLAN ID in criterion={}, metadata={}",
509 readVlanFromTreatment(filt.meta()), vidCriterion.vlanId());
510 fail(filt, ObjectiveError.BADPARAMS);
511 return;
Saurav Das0e99e2b2015-10-28 12:39:42 -0700512 }
513 }
514
Yi Tseng3a77b4f2017-02-06 15:02:17 -0800515 if (ethCriterion == null || ethCriterion.mac().equals(NONE)) {
Charles Chan985b12e2016-05-11 19:47:22 -0700516 // NOTE: it is possible that a filtering objective only has vidCriterion
Daniel Ginsburgc1d47e92018-04-30 19:27:19 -0400517 log.debug("filtering objective missing dstMac, won't program TMAC table");
Saurav Das822c4e22015-10-23 10:51:11 -0700518 } else {
Charles Chan45b69ab2018-03-02 15:41:41 -0800519 MacAddress unicastMac = readEthDstFromTreatment(filt.meta());
Charles Chan66291502018-03-02 16:43:28 -0800520 List<List<FlowRule>> allStages = processEthDstFilter(portCriterion, ethCriterion,
Charles Chan45b69ab2018-03-02 15:41:41 -0800521 vidCriterion, assignedVlan, unicastMac, applicationId);
Charles Chan66291502018-03-02 16:43:28 -0800522 for (List<FlowRule> flowRules : allStages) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800523 log.trace("Starting a new flow rule stage for TMAC table flow");
Charles Chan66291502018-03-02 16:43:28 -0800524 ops.newStage();
Charles Chanc550f2e2017-12-19 19:55:57 -0800525
Charles Chan66291502018-03-02 16:43:28 -0800526 for (FlowRule flowRule : flowRules) {
Charles Chan056e0c12018-05-10 22:19:49 +0000527 log.trace("{} flow rules in TMAC table: {} for dev: {}",
Charles Chan66291502018-03-02 16:43:28 -0800528 (install) ? "adding" : "removing", flowRules, deviceId);
Charles Chan66291502018-03-02 16:43:28 -0800529 if (install) {
530 ops = ops.add(flowRule);
Charles Chanc550f2e2017-12-19 19:55:57 -0800531 } else {
Charles Chan66291502018-03-02 16:43:28 -0800532 // NOTE: Only remove TMAC flow when there is no more enabled port within the
533 // same VLAN on this device if TMAC doesn't support matching on in_port.
534 if (matchInPortTmacTable() || (filt.meta() != null && filt.meta().clearedDeferred())) {
535 ops = ops.remove(flowRule);
536 } else {
537 log.debug("Abort TMAC flow removal on {}. Some other ports still share this TMAC flow");
538 }
Charles Chanc550f2e2017-12-19 19:55:57 -0800539 }
540 }
Saurav Das822c4e22015-10-23 10:51:11 -0700541 }
542 }
543
Charles Chan985b12e2016-05-11 19:47:22 -0700544 if (vidCriterion == null) {
545 // NOTE: it is possible that a filtering objective only has ethCriterion
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530546 log.info("filtering objective missing VLAN, cannot program VLAN Table");
Saurav Das822c4e22015-10-23 10:51:11 -0700547 } else {
Charles Chan66291502018-03-02 16:43:28 -0800548 List<List<FlowRule>> allStages = processVlanIdFilter(
Charles Chan14967c22015-12-07 11:11:50 -0800549 portCriterion, vidCriterion, assignedVlan, applicationId);
Charles Chan66291502018-03-02 16:43:28 -0800550 for (List<FlowRule> flowRules : allStages) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800551 log.trace("Starting a new flow rule stage for VLAN table flow");
Charles Chan66291502018-03-02 16:43:28 -0800552 ops.newStage();
Charles Chan14967c22015-12-07 11:11:50 -0800553
Charles Chan66291502018-03-02 16:43:28 -0800554 for (FlowRule flowRule : flowRules) {
555 log.trace("{} flow rules in VLAN table: {} for dev: {}",
556 (install) ? "adding" : "removing", flowRule, deviceId);
557 ops = install ? ops.add(flowRule) : ops.remove(flowRule);
Charles Chand1172632017-03-15 17:33:09 -0700558 }
Saurav Das822c4e22015-10-23 10:51:11 -0700559 }
560 }
561
Saurav Das822c4e22015-10-23 10:51:11 -0700562 // apply filtering flow rules
563 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
564 @Override
565 public void onSuccess(FlowRuleOperations ops) {
Saurav Das018605f2017-02-18 14:05:44 -0800566 log.debug("Applied {} filtering rules in device {}",
Saurav Das822c4e22015-10-23 10:51:11 -0700567 ops.stages().get(0).size(), deviceId);
568 pass(filt);
569 }
570
571 @Override
572 public void onError(FlowRuleOperations ops) {
573 log.info("Failed to apply all filtering rules in dev {}", deviceId);
574 fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED);
575 }
576 }));
577
578 }
579
580 /**
Charles Chand1172632017-03-15 17:33:09 -0700581 * Internal implementation of processVlanIdFilter.
582 * <p>
583 * The is_present bit in set_vlan_vid action is required to be 0 in OFDPA i12.
584 * Since it is non-OF spec, we need an extension treatment for that.
585 * The useVlanExtension must be set to false for OFDPA i12.
586 * </p>
Charles Chan66291502018-03-02 16:43:28 -0800587 * <p>
588 * NOTE: Separate VLAN filtering rules and assignment rules
589 * into different stages in order to guarantee that filtering rules
590 * always go first, as required by OFDPA.
591 * </p>
Saurav Das0e99e2b2015-10-28 12:39:42 -0700592 *
Charles Chanf9e98652016-09-07 16:54:23 -0700593 * @param portCriterion port on device for which this filter is programmed
594 * @param vidCriterion vlan assigned to port, or NONE for untagged
595 * @param assignedVlan assigned vlan-id for untagged packets
596 * @param applicationId for application programming this filter
Charles Chan66291502018-03-02 16:43:28 -0800597 * @return stages of flow rules for port-vlan filters
Saurav Das822c4e22015-10-23 10:51:11 -0700598 */
Charles Chan66291502018-03-02 16:43:28 -0800599 protected List<List<FlowRule>> processVlanIdFilter(PortCriterion portCriterion,
Yi Tsengef19de12017-04-24 11:33:05 -0700600 VlanIdCriterion vidCriterion,
601 VlanId assignedVlan,
602 ApplicationId applicationId) {
Charles Chan66291502018-03-02 16:43:28 -0800603 List<FlowRule> filteringRules = new ArrayList<>();
604 List<FlowRule> assignmentRules = new ArrayList<>();
Saurav Das822c4e22015-10-23 10:51:11 -0700605 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
606 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800607 TrafficSelector.Builder preSelector = null;
608 TrafficTreatment.Builder preTreatment = null;
609
Saurav Das4f980082015-11-05 13:39:15 -0800610 treatment.transition(TMAC_TABLE);
611
Saurav Das822c4e22015-10-23 10:51:11 -0700612 if (vidCriterion.vlanId() == VlanId.NONE) {
613 // untagged packets are assigned vlans
Charles Chand1172632017-03-15 17:33:09 -0700614 preSelector = DefaultTrafficSelector.builder();
615 if (requireVlanExtensions()) {
616 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(VlanId.NONE);
617 selector.extension(ofdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700618 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
619 treatment.extension(ofdpaSetVlanVid, deviceId);
Charles Chand1172632017-03-15 17:33:09 -0700620
621 OfdpaMatchVlanVid preOfdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
622 preSelector.extension(preOfdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700623 } else {
Charles Chand1172632017-03-15 17:33:09 -0700624 selector.matchVlanId(VlanId.NONE);
Charles Chanf9e98652016-09-07 16:54:23 -0700625 treatment.setVlanId(assignedVlan);
Charles Chand1172632017-03-15 17:33:09 -0700626
627 preSelector.matchVlanId(assignedVlan);
628 }
629 preTreatment = DefaultTrafficTreatment.builder().transition(TMAC_TABLE);
630 } else {
631 if (requireVlanExtensions()) {
632 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
633 selector.extension(ofdpaMatchVlanVid, deviceId);
634 } else {
635 selector.matchVlanId(vidCriterion.vlanId());
Charles Chanf9e98652016-09-07 16:54:23 -0700636 }
Charles Chan14967c22015-12-07 11:11:50 -0800637
Charles Chand55e84d2016-03-30 17:54:24 -0700638 if (!assignedVlan.equals(vidCriterion.vlanId())) {
Charles Chand1172632017-03-15 17:33:09 -0700639 if (requireVlanExtensions()) {
Charles Chanc03782d2017-01-31 13:57:55 -0800640 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
641 treatment.extension(ofdpaSetVlanVid, deviceId);
642 } else {
643 treatment.setVlanId(assignedVlan);
644 }
Charles Chand55e84d2016-03-30 17:54:24 -0700645 }
Saurav Das822c4e22015-10-23 10:51:11 -0700646 }
Saurav Das822c4e22015-10-23 10:51:11 -0700647
648 // ofdpa cannot match on ALL portnumber, so we need to use separate
649 // rules for each port.
Charles Chan14967c22015-12-07 11:11:50 -0800650 List<PortNumber> portnums = new ArrayList<>();
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530651 if (portCriterion != null) {
652 if (PortNumber.ALL.equals(portCriterion.port())) {
653 for (Port port : deviceService.getPorts(deviceId)) {
654 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
655 portnums.add(port.number());
656 }
Saurav Das822c4e22015-10-23 10:51:11 -0700657 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530658 } else {
659 portnums.add(portCriterion.port());
Saurav Das822c4e22015-10-23 10:51:11 -0700660 }
661 } else {
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530662 log.warn("Filtering Objective missing Port Criterion . " +
Charles Chan66291502018-03-02 16:43:28 -0800663 "VLAN Table cannot be programmed for {}", deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700664 }
Saurav Das4f980082015-11-05 13:39:15 -0800665
Saurav Das822c4e22015-10-23 10:51:11 -0700666 for (PortNumber pnum : portnums) {
Saurav Das4f980082015-11-05 13:39:15 -0800667 // create rest of flowrule
Saurav Das822c4e22015-10-23 10:51:11 -0700668 selector.matchInPort(pnum);
669 FlowRule rule = DefaultFlowRule.builder()
670 .forDevice(deviceId)
671 .withSelector(selector.build())
672 .withTreatment(treatment.build())
673 .withPriority(DEFAULT_PRIORITY)
674 .fromApp(applicationId)
675 .makePermanent()
676 .forTable(VLAN_TABLE).build();
Charles Chan66291502018-03-02 16:43:28 -0800677 assignmentRules.add(rule);
Charles Chan14967c22015-12-07 11:11:50 -0800678
679 if (preSelector != null) {
680 preSelector.matchInPort(pnum);
681 FlowRule preRule = DefaultFlowRule.builder()
682 .forDevice(deviceId)
683 .withSelector(preSelector.build())
684 .withTreatment(preTreatment.build())
685 .withPriority(DEFAULT_PRIORITY)
686 .fromApp(applicationId)
687 .makePermanent()
688 .forTable(VLAN_TABLE).build();
Charles Chan66291502018-03-02 16:43:28 -0800689 filteringRules.add(preRule);
Charles Chan14967c22015-12-07 11:11:50 -0800690 }
Saurav Das822c4e22015-10-23 10:51:11 -0700691 }
Charles Chan2686dd72018-03-06 22:10:15 -0800692 return ImmutableList.of(filteringRules, assignmentRules);
Saurav Das822c4e22015-10-23 10:51:11 -0700693 }
694
695 /**
696 * Allows routed packets with correct destination MAC to be directed
Charles Chan45b69ab2018-03-02 15:41:41 -0800697 * to unicast routing table, multicast routing table or MPLS forwarding table.
Saurav Das822c4e22015-10-23 10:51:11 -0700698 *
699 * @param portCriterion port on device for which this filter is programmed
700 * @param ethCriterion dstMac of device for which is filter is programmed
701 * @param vidCriterion vlan assigned to port, or NONE for untagged
Saurav Das0e99e2b2015-10-28 12:39:42 -0700702 * @param assignedVlan assigned vlan-id for untagged packets
Charles Chan45b69ab2018-03-02 15:41:41 -0800703 * @param unicastMac some switches require a unicast TMAC flow to be programmed before multicast
704 * TMAC flow. This MAC address will be used for the unicast TMAC flow.
705 * This is unused if the filtering objective is a unicast.
Saurav Das822c4e22015-10-23 10:51:11 -0700706 * @param applicationId for application programming this filter
Charles Chan66291502018-03-02 16:43:28 -0800707 * @return stages of flow rules for port-vlan filters
Saurav Das822c4e22015-10-23 10:51:11 -0700708
709 */
Charles Chan66291502018-03-02 16:43:28 -0800710 protected List<List<FlowRule>> processEthDstFilter(PortCriterion portCriterion,
Saurav Das822c4e22015-10-23 10:51:11 -0700711 EthCriterion ethCriterion,
712 VlanIdCriterion vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700713 VlanId assignedVlan,
Charles Chan45b69ab2018-03-02 15:41:41 -0800714 MacAddress unicastMac,
Saurav Das822c4e22015-10-23 10:51:11 -0700715 ApplicationId applicationId) {
Charles Chan5270ed02016-01-30 23:22:37 -0800716 // Consider PortNumber.ANY as wildcard. Match ETH_DST only
Sivachidambaram Subramanianc598d062017-05-16 22:42:06 +0530717 if (portCriterion != null && PortNumber.ANY.equals(portCriterion.port())) {
Charles Chan5270ed02016-01-30 23:22:37 -0800718 return processEthDstOnlyFilter(ethCriterion, applicationId);
719 }
720
Charles Chan5b9df8d2016-03-28 22:21:40 -0700721 // Multicast MAC
722 if (ethCriterion.mask() != null) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800723 return processMcastEthDstFilter(ethCriterion, assignedVlan, unicastMac, applicationId);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700724 }
725
Saurav Das822c4e22015-10-23 10:51:11 -0700726 //handling untagged packets via assigned VLAN
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530727 if (vidCriterion != null && vidCriterion.vlanId() == VlanId.NONE) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700728 vidCriterion = (VlanIdCriterion) Criteria.matchVlanId(assignedVlan);
Saurav Das822c4e22015-10-23 10:51:11 -0700729 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530730 List<FlowRule> rules = new ArrayList<>();
731 OfdpaMatchVlanVid ofdpaMatchVlanVid = null;
732 if (vidCriterion != null && requireVlanExtensions()) {
733 ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
734 }
Saurav Das822c4e22015-10-23 10:51:11 -0700735 // ofdpa cannot match on ALL portnumber, so we need to use separate
736 // rules for each port.
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -0700737 List<PortNumber> portnums = new ArrayList<>();
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530738 if (portCriterion != null) {
739 if (PortNumber.ALL.equals(portCriterion.port())) {
740 for (Port port : deviceService.getPorts(deviceId)) {
741 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
742 portnums.add(port.number());
743 }
Saurav Das822c4e22015-10-23 10:51:11 -0700744 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530745 } else {
746 portnums.add(portCriterion.port());
747 }
748 for (PortNumber pnum : portnums) {
749 rules.add(buildTmacRuleForIpv4(ethCriterion,
750 vidCriterion,
751 ofdpaMatchVlanVid,
752 applicationId,
753 pnum));
754 rules.add(buildTmacRuleForMpls(ethCriterion,
755 vidCriterion,
756 ofdpaMatchVlanVid,
757 applicationId,
758 pnum));
759 rules.add(buildTmacRuleForIpv6(ethCriterion,
760 vidCriterion,
761 ofdpaMatchVlanVid,
762 applicationId,
763 pnum));
Saurav Das822c4e22015-10-23 10:51:11 -0700764 }
765 } else {
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530766 rules.add(buildTmacRuleForIpv4(ethCriterion,
767 vidCriterion,
768 ofdpaMatchVlanVid,
769 applicationId,
770 null));
771 rules.add(buildTmacRuleForMpls(ethCriterion,
772 vidCriterion,
773 ofdpaMatchVlanVid,
774 applicationId,
775 null));
776 rules.add(buildTmacRuleForIpv6(ethCriterion,
777 vidCriterion,
778 ofdpaMatchVlanVid,
779 applicationId,
780 null));
Saurav Das822c4e22015-10-23 10:51:11 -0700781 }
Charles Chan66291502018-03-02 16:43:28 -0800782 return ImmutableList.of(rules);
Saurav Das822c4e22015-10-23 10:51:11 -0700783 }
784
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530785 /**
786 * Builds TMAC rules for IPv4 packets.
787 *
Charles Chanc550f2e2017-12-19 19:55:57 -0800788 * @param ethCriterion dst mac matching
789 * @param vidCriterion vlan id assigned to the port
790 * @param ofdpaMatchVlanVid OFDPA vlan id matching
791 * @param applicationId application id
792 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530793 * @return TMAC rule for IPV4 packets
794 */
795 private FlowRule buildTmacRuleForIpv4(EthCriterion ethCriterion,
796 VlanIdCriterion vidCriterion,
797 OfdpaMatchVlanVid ofdpaMatchVlanVid,
798 ApplicationId applicationId,
799 PortNumber pnum) {
800 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
801 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
802 if (pnum != null) {
803 if (matchInPortTmacTable()) {
804 selector.matchInPort(pnum);
805 } else {
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800806 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530807 "ignoring the IN_PORT criteria");
808 }
809 }
810 if (vidCriterion != null) {
811 if (requireVlanExtensions()) {
812 selector.extension(ofdpaMatchVlanVid, deviceId);
813 } else {
814 selector.matchVlanId(vidCriterion.vlanId());
815 }
816 }
817 selector.matchEthType(Ethernet.TYPE_IPV4);
818 selector.matchEthDst(ethCriterion.mac());
819 treatment.transition(UNICAST_ROUTING_TABLE);
820 return DefaultFlowRule.builder()
821 .forDevice(deviceId)
822 .withSelector(selector.build())
823 .withTreatment(treatment.build())
824 .withPriority(DEFAULT_PRIORITY)
825 .fromApp(applicationId)
826 .makePermanent()
827 .forTable(TMAC_TABLE).build();
828 }
829
830 /**
831 * Builds TMAC rule for MPLS packets.
832 *
Charles Chanc550f2e2017-12-19 19:55:57 -0800833 * @param ethCriterion dst mac matching
834 * @param vidCriterion vlan id assigned to the port
835 * @param ofdpaMatchVlanVid OFDPA vlan id matching
836 * @param applicationId application id
837 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530838 * @return TMAC rule for MPLS packets
839 */
840 private FlowRule buildTmacRuleForMpls(EthCriterion ethCriterion,
841 VlanIdCriterion vidCriterion,
842 OfdpaMatchVlanVid ofdpaMatchVlanVid,
843 ApplicationId applicationId,
844 PortNumber pnum) {
845 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
846 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
847 if (pnum != null) {
848 if (matchInPortTmacTable()) {
849 selector.matchInPort(pnum);
850 } else {
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800851 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530852 "ignoring the IN_PORT criteria");
853 }
854 }
855 if (vidCriterion != null) {
856 if (requireVlanExtensions()) {
857 selector.extension(ofdpaMatchVlanVid, deviceId);
858 } else {
859 selector.matchVlanId(vidCriterion.vlanId());
860 }
861 }
862 selector.matchEthType(Ethernet.MPLS_UNICAST);
863 selector.matchEthDst(ethCriterion.mac());
864 treatment.transition(MPLS_TABLE_0);
865 return DefaultFlowRule.builder()
866 .forDevice(deviceId)
867 .withSelector(selector.build())
868 .withTreatment(treatment.build())
869 .withPriority(DEFAULT_PRIORITY)
870 .fromApp(applicationId)
871 .makePermanent()
872 .forTable(TMAC_TABLE).build();
873 }
874
875 /**
876 * Builds TMAC rules for IPv6 packets.
877 *
Charles Chanc550f2e2017-12-19 19:55:57 -0800878 * @param ethCriterion dst mac matching
879 * @param vidCriterion vlan id assigned to the port
880 * @param ofdpaMatchVlanVid OFDPA vlan id matching
881 * @param applicationId application id
882 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530883 * @return TMAC rule for IPV6 packets
884 */
885 private FlowRule buildTmacRuleForIpv6(EthCriterion ethCriterion,
886 VlanIdCriterion vidCriterion,
887 OfdpaMatchVlanVid ofdpaMatchVlanVid,
888 ApplicationId applicationId,
889 PortNumber pnum) {
890 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
891 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
892 if (pnum != null) {
893 if (matchInPortTmacTable()) {
894 selector.matchInPort(pnum);
895 } else {
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800896 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530897 "ignoring the IN_PORT criteria");
898 }
899 }
900 if (vidCriterion != null) {
901 if (requireVlanExtensions()) {
902 selector.extension(ofdpaMatchVlanVid, deviceId);
903 } else {
904 selector.matchVlanId(vidCriterion.vlanId());
905 }
906 }
907 selector.matchEthType(Ethernet.TYPE_IPV6);
908 selector.matchEthDst(ethCriterion.mac());
909 treatment.transition(UNICAST_ROUTING_TABLE);
910 return DefaultFlowRule.builder()
911 .forDevice(deviceId)
912 .withSelector(selector.build())
913 .withTreatment(treatment.build())
914 .withPriority(DEFAULT_PRIORITY)
915 .fromApp(applicationId)
916 .makePermanent()
917 .forTable(TMAC_TABLE).build();
918 }
919
Charles Chan66291502018-03-02 16:43:28 -0800920 protected List<List<FlowRule>> processEthDstOnlyFilter(EthCriterion ethCriterion,
Yi Tsengef19de12017-04-24 11:33:05 -0700921 ApplicationId applicationId) {
Pier Luigi0e358632017-01-31 09:35:05 -0800922 ImmutableList.Builder<FlowRule> builder = ImmutableList.builder();
923
Charles Chan5270ed02016-01-30 23:22:37 -0800924 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
925 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
926 selector.matchEthType(Ethernet.TYPE_IPV4);
927 selector.matchEthDst(ethCriterion.mac());
928 treatment.transition(UNICAST_ROUTING_TABLE);
929 FlowRule rule = DefaultFlowRule.builder()
930 .forDevice(deviceId)
931 .withSelector(selector.build())
932 .withTreatment(treatment.build())
933 .withPriority(DEFAULT_PRIORITY)
934 .fromApp(applicationId)
935 .makePermanent()
936 .forTable(TMAC_TABLE).build();
Pier Luigi0e358632017-01-31 09:35:05 -0800937 builder.add(rule);
938
939 selector = DefaultTrafficSelector.builder();
940 treatment = DefaultTrafficTreatment.builder();
941 selector.matchEthType(Ethernet.TYPE_IPV6);
942 selector.matchEthDst(ethCriterion.mac());
943 treatment.transition(UNICAST_ROUTING_TABLE);
944 rule = DefaultFlowRule.builder()
945 .forDevice(deviceId)
946 .withSelector(selector.build())
947 .withTreatment(treatment.build())
948 .withPriority(DEFAULT_PRIORITY)
949 .fromApp(applicationId)
950 .makePermanent()
951 .forTable(TMAC_TABLE).build();
Charles Chan66291502018-03-02 16:43:28 -0800952 return ImmutableList.of(builder.add(rule).build());
Charles Chan5270ed02016-01-30 23:22:37 -0800953 }
954
Charles Chan66291502018-03-02 16:43:28 -0800955 List<List<FlowRule>> processMcastEthDstFilter(EthCriterion ethCriterion,
Charles Chanb4879a52017-10-20 19:09:16 -0700956 VlanId assignedVlan,
Charles Chan45b69ab2018-03-02 15:41:41 -0800957 MacAddress unicastMac,
Yi Tsengef19de12017-04-24 11:33:05 -0700958 ApplicationId applicationId) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800959 ImmutableList.Builder<FlowRule> unicastFlows = ImmutableList.builder();
960 ImmutableList.Builder<FlowRule> multicastFlows = ImmutableList.builder();
961 TrafficSelector.Builder selector;
962 TrafficTreatment.Builder treatment;
Charles Chanb4879a52017-10-20 19:09:16 -0700963 FlowRule rule;
Julia Ferguson65428c32017-08-10 18:15:24 +0000964
Charles Chanb4879a52017-10-20 19:09:16 -0700965 if (IPV4_MULTICAST.equals(ethCriterion.mac())) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800966 if (requireUnicastBeforeMulticast()) {
967 selector = DefaultTrafficSelector.builder();
968 treatment = DefaultTrafficTreatment.builder();
969 selector.matchEthType(Ethernet.TYPE_IPV4);
970 selector.matchEthDst(unicastMac);
Charles Chan056e0c12018-05-10 22:19:49 +0000971 selector.matchVlanId(assignedVlan);
Charles Chan45b69ab2018-03-02 15:41:41 -0800972 treatment.transition(UNICAST_ROUTING_TABLE);
973 rule = DefaultFlowRule.builder()
974 .forDevice(deviceId)
975 .withSelector(selector.build())
976 .withTreatment(treatment.build())
Charles Chan056e0c12018-05-10 22:19:49 +0000977 .withPriority(DEFAULT_PRIORITY)
Charles Chan45b69ab2018-03-02 15:41:41 -0800978 .fromApp(applicationId)
979 .makePermanent()
980 .forTable(TMAC_TABLE).build();
981 unicastFlows.add(rule);
982 }
983
984 selector = DefaultTrafficSelector.builder();
985 treatment = DefaultTrafficTreatment.builder();
Charles Chanb4879a52017-10-20 19:09:16 -0700986 selector.matchEthType(Ethernet.TYPE_IPV4);
987 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
Charles Chan056e0c12018-05-10 22:19:49 +0000988 selector.matchVlanId(assignedVlan);
Charles Chanb4879a52017-10-20 19:09:16 -0700989 treatment.transition(MULTICAST_ROUTING_TABLE);
990 rule = DefaultFlowRule.builder()
991 .forDevice(deviceId)
992 .withSelector(selector.build())
993 .withTreatment(treatment.build())
Charles Chan056e0c12018-05-10 22:19:49 +0000994 .withPriority(DEFAULT_PRIORITY)
Charles Chanb4879a52017-10-20 19:09:16 -0700995 .fromApp(applicationId)
996 .makePermanent()
997 .forTable(TMAC_TABLE).build();
Charles Chan45b69ab2018-03-02 15:41:41 -0800998 multicastFlows.add(rule);
Charles Chanb4879a52017-10-20 19:09:16 -0700999 }
1000
1001 if (IPV6_MULTICAST.equals(ethCriterion.mac())) {
Charles Chan45b69ab2018-03-02 15:41:41 -08001002 if (requireUnicastBeforeMulticast()) {
1003 selector = DefaultTrafficSelector.builder();
1004 treatment = DefaultTrafficTreatment.builder();
1005 selector.matchEthType(Ethernet.TYPE_IPV6);
1006 selector.matchEthDst(unicastMac);
Charles Chan056e0c12018-05-10 22:19:49 +00001007 selector.matchVlanId(assignedVlan);
Charles Chan45b69ab2018-03-02 15:41:41 -08001008 treatment.transition(UNICAST_ROUTING_TABLE);
1009 rule = DefaultFlowRule.builder()
1010 .forDevice(deviceId)
1011 .withSelector(selector.build())
1012 .withTreatment(treatment.build())
Charles Chan056e0c12018-05-10 22:19:49 +00001013 .withPriority(DEFAULT_PRIORITY)
Charles Chan45b69ab2018-03-02 15:41:41 -08001014 .fromApp(applicationId)
1015 .makePermanent()
1016 .forTable(TMAC_TABLE).build();
1017 unicastFlows.add(rule);
1018 }
1019
Charles Chanb4879a52017-10-20 19:09:16 -07001020 selector = DefaultTrafficSelector.builder();
1021 treatment = DefaultTrafficTreatment.builder();
1022 selector.matchEthType(Ethernet.TYPE_IPV6);
1023 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
Charles Chan056e0c12018-05-10 22:19:49 +00001024 selector.matchVlanId(assignedVlan);
Charles Chanb4879a52017-10-20 19:09:16 -07001025 treatment.transition(MULTICAST_ROUTING_TABLE);
1026 rule = DefaultFlowRule.builder()
1027 .forDevice(deviceId)
1028 .withSelector(selector.build())
1029 .withTreatment(treatment.build())
Charles Chan056e0c12018-05-10 22:19:49 +00001030 .withPriority(DEFAULT_PRIORITY)
Charles Chanb4879a52017-10-20 19:09:16 -07001031 .fromApp(applicationId)
1032 .makePermanent()
1033 .forTable(TMAC_TABLE).build();
Charles Chan45b69ab2018-03-02 15:41:41 -08001034 multicastFlows.add(rule);
Charles Chanb4879a52017-10-20 19:09:16 -07001035 }
Charles Chan45b69ab2018-03-02 15:41:41 -08001036 return ImmutableList.of(unicastFlows.build(), multicastFlows.build());
Charles Chan5b9df8d2016-03-28 22:21:40 -07001037 }
1038
Saurav Das822c4e22015-10-23 10:51:11 -07001039 private Collection<FlowRule> processForward(ForwardingObjective fwd) {
1040 switch (fwd.flag()) {
1041 case SPECIFIC:
1042 return processSpecific(fwd);
1043 case VERSATILE:
1044 return processVersatile(fwd);
Jonghwan Hyun800d9d02018-04-09 09:40:50 -07001045 case EGRESS:
1046 return processEgress(fwd);
Saurav Das822c4e22015-10-23 10:51:11 -07001047 default:
1048 fail(fwd, ObjectiveError.UNKNOWN);
1049 log.warn("Unknown forwarding flag {}", fwd.flag());
1050 }
1051 return Collections.emptySet();
1052 }
1053
1054 /**
Jonghwan Hyun800d9d02018-04-09 09:40:50 -07001055 * In the OF-DPA 2.0 pipeline, egress forwarding objectives go to the
1056 * egress tables.
1057 * @param fwd the forwarding objective of type 'egress'
1058 * @return a collection of flow rules to be sent to the switch. An empty
1059 * collection may be returned if there is a problem in processing
1060 * the flow rule
1061 */
1062 protected Collection<FlowRule> processEgress(ForwardingObjective fwd) {
1063 log.debug("Processing egress forwarding objective:{} in dev:{}",
1064 fwd, deviceId);
1065
1066 List<FlowRule> rules = new ArrayList<>();
1067
1068 // Build selector
1069 TrafficSelector.Builder sb = DefaultTrafficSelector.builder();
1070 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) fwd.selector().getCriterion(Criterion.Type.VLAN_VID);
1071 if (vlanIdCriterion == null) {
1072 log.error("Egress forwarding objective:{} must include vlanId", fwd.id());
1073 fail(fwd, ObjectiveError.BADPARAMS);
1074 return rules;
1075 }
1076
1077 Optional<Instruction> outInstr = fwd.treatment().allInstructions().stream()
1078 .filter(instruction -> instruction instanceof OutputInstruction).findFirst();
1079 if (!outInstr.isPresent()) {
1080 log.error("Egress forwarding objective:{} must include output port", fwd.id());
1081 fail(fwd, ObjectiveError.BADPARAMS);
1082 return rules;
1083 }
1084
1085 PortNumber portNumber = ((OutputInstruction) outInstr.get()).port();
1086
1087 sb.matchVlanId(vlanIdCriterion.vlanId());
1088 OfdpaMatchActsetOutput actsetOutput = new OfdpaMatchActsetOutput(portNumber);
1089 sb.extension(actsetOutput, deviceId);
1090
1091 sb.extension(new OfdpaMatchAllowVlanTranslation(ALLOW_VLAN_TRANSLATION), deviceId);
1092
1093 // Build a flow rule for Egress VLAN Flow table
1094 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
1095 tb.transition(EGRESS_DSCP_PCP_REMARK_FLOW_TABLE);
1096 if (fwd.treatment() != null) {
1097 for (Instruction instr : fwd.treatment().allInstructions()) {
1098 if (instr instanceof L2ModificationInstruction &&
1099 ((L2ModificationInstruction) instr).subtype() == L2SubType.VLAN_ID) {
1100 tb.immediate().add(instr);
1101 }
1102 if (instr instanceof L2ModificationInstruction &&
1103 ((L2ModificationInstruction) instr).subtype() == L2SubType.VLAN_PUSH) {
1104 tb.immediate().pushVlan();
1105 EthType ethType = ((L2ModificationInstruction.ModVlanHeaderInstruction) instr).ethernetType();
1106 if (ethType.equals(EtherType.QINQ.ethType())) {
1107 // Build a flow rule for Egress TPID Flow table
1108 TrafficSelector tpidSelector = DefaultTrafficSelector.builder()
1109 .extension(actsetOutput, deviceId)
1110 .matchVlanId(VlanId.ANY).build();
1111
1112 TrafficTreatment tpidTreatment = DefaultTrafficTreatment.builder()
1113 .extension(new Ofdpa3CopyField(COPY_FIELD_NBITS, COPY_FIELD_OFFSET,
1114 COPY_FIELD_OFFSET, OXM_ID_VLAN_VID,
1115 OXM_ID_PACKET_REG_1),
1116 deviceId)
1117 .popVlan()
1118 .pushVlan(EtherType.QINQ.ethType())
1119 .extension(new Ofdpa3CopyField(COPY_FIELD_NBITS, COPY_FIELD_OFFSET,
1120 COPY_FIELD_OFFSET, OXM_ID_PACKET_REG_1,
1121 OXM_ID_VLAN_VID),
1122 deviceId)
1123 .build();
1124
1125 FlowRule.Builder tpidRuleBuilder = DefaultFlowRule.builder()
1126 .fromApp(fwd.appId())
1127 .withPriority(fwd.priority())
1128 .forDevice(deviceId)
1129 .withSelector(tpidSelector)
1130 .withTreatment(tpidTreatment)
1131 .makePermanent()
1132 .forTable(EGRESS_TPID_FLOW_TABLE);
1133 rules.add(tpidRuleBuilder.build());
1134 }
1135 }
1136 }
1137 }
1138
1139 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1140 .fromApp(fwd.appId())
1141 .withPriority(fwd.priority())
1142 .forDevice(deviceId)
1143 .withSelector(sb.build())
1144 .withTreatment(tb.build())
1145 .makePermanent()
1146 .forTable(EGRESS_VLAN_FLOW_TABLE);
1147 rules.add(ruleBuilder.build());
1148 return rules;
1149 }
1150
1151 /**
Saurav Das822c4e22015-10-23 10:51:11 -07001152 * In the OF-DPA 2.0 pipeline, versatile forwarding objectives go to the
1153 * ACL table.
1154 * @param fwd the forwarding objective of type 'versatile'
1155 * @return a collection of flow rules to be sent to the switch. An empty
1156 * collection may be returned if there is a problem in processing
1157 * the flow rule
1158 */
Saurav Das52025962016-01-28 22:30:01 -08001159 protected Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
Saurav Das018605f2017-02-18 14:05:44 -08001160 log.debug("Processing versatile forwarding objective:{} in dev:{}",
Saurav Dasd2fded02016-12-02 15:43:47 -08001161 fwd.id(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001162
Saurav Das822c4e22015-10-23 10:51:11 -07001163 if (fwd.nextId() == null && fwd.treatment() == null) {
1164 log.error("Forwarding objective {} from {} must contain "
1165 + "nextId or Treatment", fwd.selector(), fwd.appId());
Zsolt Haraszti9faab752016-02-17 15:55:20 -08001166 fail(fwd, ObjectiveError.BADPARAMS);
Saurav Das822c4e22015-10-23 10:51:11 -07001167 return Collections.emptySet();
1168 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001169
Saurav Das77b5e902016-01-27 17:01:59 -08001170 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
1171 fwd.selector().criteria().forEach(criterion -> {
1172 if (criterion instanceof VlanIdCriterion) {
1173 VlanId vlanId = ((VlanIdCriterion) criterion).vlanId();
1174 // ensure that match does not include vlan = NONE as OF-DPA does not
1175 // match untagged packets this way in the ACL table.
1176 if (vlanId.equals(VlanId.NONE)) {
1177 return;
1178 }
Charles Chand1172632017-03-15 17:33:09 -07001179 if (requireVlanExtensions()) {
1180 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanId);
1181 sbuilder.extension(ofdpaMatchVlanVid, deviceId);
1182 } else {
1183 sbuilder.matchVlanId(vlanId);
1184 }
Charles Chan09bf2692018-01-11 11:48:18 -08001185 } else if (criterion instanceof Icmpv6TypeCriterion) {
1186 byte icmpv6Type = (byte) ((Icmpv6TypeCriterion) criterion).icmpv6Type();
1187 sbuilder.matchIcmpv6Type(icmpv6Type);
1188 } else if (criterion instanceof Icmpv6CodeCriterion) {
1189 byte icmpv6Code = (byte) ((Icmpv6CodeCriterion) criterion).icmpv6Code();
1190 sbuilder.matchIcmpv6Type(icmpv6Code);
Charles Chand9e47c62017-10-05 15:17:15 -07001191 } else if (criterion instanceof TcpPortCriterion || criterion instanceof UdpPortCriterion) {
1192 // FIXME: QMX switches do not support L4 dst port matching in ACL table.
1193 // Currently L4 dst port matching is only used by DHCP relay feature
1194 // and therefore is safe to be replaced with L4 src port matching.
1195 // We need to revisit this if L4 dst port is used for other purpose in the future.
1196 if (!supportIpv6L4Dst() && isIpv6(fwd.selector())) {
Charles Chand9e47c62017-10-05 15:17:15 -07001197 switch (criterion.type()) {
1198 case UDP_DST:
Charles Chand9e47c62017-10-05 15:17:15 -07001199 case UDP_DST_MASKED:
Charles Chand9e47c62017-10-05 15:17:15 -07001200 case TCP_DST:
Charles Chand9e47c62017-10-05 15:17:15 -07001201 case TCP_DST_MASKED:
Charles Chand9e47c62017-10-05 15:17:15 -07001202 break;
1203 default:
1204 sbuilder.add(criterion);
1205 }
1206 } else {
1207 sbuilder.add(criterion);
1208 }
Saurav Das77b5e902016-01-27 17:01:59 -08001209 } else {
1210 sbuilder.add(criterion);
1211 }
1212 });
1213
Saurav Das822c4e22015-10-23 10:51:11 -07001214 // XXX driver does not currently do type checking as per Tables 65-67 in
1215 // OFDPA 2.0 spec. The only allowed treatment is a punt to the controller.
Saurav Das49cb5a12016-01-16 22:54:07 -08001216 TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder();
1217 if (fwd.treatment() != null) {
1218 for (Instruction ins : fwd.treatment().allInstructions()) {
1219 if (ins instanceof OutputInstruction) {
1220 OutputInstruction o = (OutputInstruction) ins;
Ray Milkeyfe0e0852018-01-18 11:14:05 -08001221 if (PortNumber.CONTROLLER.equals(o.port())) {
Saurav Das49cb5a12016-01-16 22:54:07 -08001222 ttBuilder.add(o);
1223 } else {
1224 log.warn("Only allowed treatments in versatile forwarding "
1225 + "objectives are punts to the controller");
1226 }
Charles Chanf76de302018-06-15 18:54:18 -07001227 } else if (ins instanceof NoActionInstruction) {
1228 // No action is allowed and nothing needs to be done
Saurav Das49cb5a12016-01-16 22:54:07 -08001229 } else {
1230 log.warn("Cannot process instruction in versatile fwd {}", ins);
1231 }
Saurav Das822c4e22015-10-23 10:51:11 -07001232 }
Charles Chan2df0e8a2017-01-09 11:45:08 -08001233 if (fwd.treatment().clearedDeferred()) {
1234 ttBuilder.wipeDeferred();
1235 }
Saurav Das822c4e22015-10-23 10:51:11 -07001236 }
Saurav Das822c4e22015-10-23 10:51:11 -07001237 if (fwd.nextId() != null) {
Saurav Das49cb5a12016-01-16 22:54:07 -08001238 // overide case
1239 NextGroup next = getGroupForNextObjective(fwd.nextId());
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +05301240 if (next == null) {
1241 fail(fwd, ObjectiveError.BADPARAMS);
1242 return Collections.emptySet();
1243 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001244 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1245 // we only need the top level group's key to point the flow to it
1246 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1247 if (group == null) {
1248 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1249 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1250 fail(fwd, ObjectiveError.GROUPMISSING);
1251 return Collections.emptySet();
1252 }
1253 ttBuilder.deferred().group(group.id());
Saurav Das822c4e22015-10-23 10:51:11 -07001254 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001255
1256 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1257 .fromApp(fwd.appId())
1258 .withPriority(fwd.priority())
1259 .forDevice(deviceId)
Saurav Das77b5e902016-01-27 17:01:59 -08001260 .withSelector(sbuilder.build())
Saurav Das49cb5a12016-01-16 22:54:07 -08001261 .withTreatment(ttBuilder.build())
1262 .makePermanent()
1263 .forTable(ACL_TABLE);
1264 return Collections.singletonList(ruleBuilder.build());
Saurav Das822c4e22015-10-23 10:51:11 -07001265 }
1266
1267 /**
1268 * In the OF-DPA 2.0 pipeline, specific forwarding refers to the IP table
Saurav Das8a0732e2015-11-20 15:27:53 -08001269 * (unicast or multicast) or the L2 table (mac + vlan) or the MPLS table.
Saurav Das822c4e22015-10-23 10:51:11 -07001270 *
1271 * @param fwd the forwarding objective of type 'specific'
1272 * @return a collection of flow rules. Typically there will be only one
1273 * for this type of forwarding objective. An empty set may be
1274 * returned if there is an issue in processing the objective.
1275 */
Charles Chan50d900c2018-03-02 13:26:22 -08001276 private Collection<FlowRule> processSpecific(ForwardingObjective fwd) {
Saurav Das25190812016-05-27 13:54:07 -07001277 log.debug("Processing specific fwd objective:{} in dev:{} with next:{}",
Saurav Das4ce45962015-11-24 23:21:05 -08001278 fwd.id(), deviceId, fwd.nextId());
1279 boolean isEthTypeObj = isSupportedEthTypeObjective(fwd);
1280 boolean isEthDstObj = isSupportedEthDstObjective(fwd);
1281
1282 if (isEthTypeObj) {
1283 return processEthTypeSpecific(fwd);
1284 } else if (isEthDstObj) {
1285 return processEthDstSpecific(fwd);
1286 } else {
1287 log.warn("processSpecific: Unsupported forwarding objective "
1288 + "criteria fwd:{} in dev:{}", fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001289 fail(fwd, ObjectiveError.UNSUPPORTED);
1290 return Collections.emptySet();
1291 }
Saurav Das4ce45962015-11-24 23:21:05 -08001292 }
1293
Saurav Das4ce45962015-11-24 23:21:05 -08001294 /**
1295 * Handles forwarding rules to the IP and MPLS tables.
1296 *
1297 * @param fwd the forwarding objective
1298 * @return A collection of flow rules, or an empty set
1299 */
1300 protected Collection<FlowRule> processEthTypeSpecific(ForwardingObjective fwd) {
Charles Chancad338a2016-09-16 18:03:11 -07001301 return processEthTypeSpecificInternal(fwd, false, ACL_TABLE);
Charles Chanf9e98652016-09-07 16:54:23 -07001302 }
1303
1304 /**
1305 * Internal implementation of processEthTypeSpecific.
1306 * <p>
1307 * Wildcarded IPv4_DST is not supported in OFDPA i12. Therefore, we break
1308 * the rule into 0.0.0.0/1 and 128.0.0.0/1.
1309 * The allowDefaultRoute must be set to false for OFDPA i12.
1310 * </p>
1311 *
1312 * @param fwd the forwarding objective
1313 * @param allowDefaultRoute allow wildcarded IPv4_DST or not
Ray Milkey0bb1e102016-11-10 14:51:27 -08001314 * @param mplsNextTable next MPLS table
Charles Chanf9e98652016-09-07 16:54:23 -07001315 * @return A collection of flow rules, or an empty set
1316 */
1317 protected Collection<FlowRule> processEthTypeSpecificInternal(ForwardingObjective fwd,
Charles Chancad338a2016-09-16 18:03:11 -07001318 boolean allowDefaultRoute,
1319 int mplsNextTable) {
Saurav Das4ce45962015-11-24 23:21:05 -08001320 TrafficSelector selector = fwd.selector();
1321 EthTypeCriterion ethType =
1322 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001323 boolean emptyGroup = false;
Charles Chan188ebf52015-12-23 00:15:11 -08001324 int forTableId;
Saurav Das8a0732e2015-11-20 15:27:53 -08001325 TrafficSelector.Builder filteredSelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -08001326 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
Flavio Castroe10fa242016-01-15 12:43:51 -08001327 TrafficSelector.Builder complementarySelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -08001328
Saurav Das8a0732e2015-11-20 15:27:53 -08001329 if (ethType.ethType().toShort() == Ethernet.TYPE_IPV4) {
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001330 if (buildIpv4Selector(filteredSelector, complementarySelector, fwd, allowDefaultRoute) < 0) {
1331 return Collections.emptyList();
1332 }
1333 // We need to set properly the next table
Flavio Castroe10fa242016-01-15 12:43:51 -08001334 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
Charles Chan5b9df8d2016-03-28 22:21:40 -07001335 if (ipv4Dst.isMulticast()) {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001336 forTableId = MULTICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -08001337 } else {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001338 forTableId = UNICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -08001339 }
Charles Chan50d900c2018-03-02 13:26:22 -08001340
Charles Chan236653d2018-03-05 11:28:23 -08001341 // TODO decrementing IP ttl is done automatically for routing, this
Charles Chan50d900c2018-03-02 13:26:22 -08001342 // action is ignored or rejected in ofdpa as it is not fully implemented
Charles Chan236653d2018-03-05 11:28:23 -08001343 // if (fwd.treatment() != null) {
1344 // for (Instruction instr : fwd.treatment().allInstructions()) {
1345 // if (instr instanceof L3ModificationInstruction &&
1346 // ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1347 // tb.deferred().add(instr);
1348 // }
1349 // }
1350 // }
1351
Pier Ventree0ae7a32016-11-23 09:57:42 -08001352 } else if (ethType.ethType().toShort() == Ethernet.TYPE_IPV6) {
1353 if (buildIpv6Selector(filteredSelector, fwd) < 0) {
1354 return Collections.emptyList();
1355 }
Kalhee Kim3d26bf02017-10-04 14:59:11 +00001356 //We need to set the proper next table
1357 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
1358 if (ipv6Dst.isMulticast()) {
1359 forTableId = MULTICAST_ROUTING_TABLE;
1360 } else {
1361 forTableId = UNICAST_ROUTING_TABLE;
1362 }
1363
Charles Chan236653d2018-03-05 11:28:23 -08001364 // TODO decrementing IP ttl is done automatically for routing, this
Charles Chan50d900c2018-03-02 13:26:22 -08001365 // action is ignored or rejected in ofdpa as it is not fully implemented
Charles Chan236653d2018-03-05 11:28:23 -08001366 // if (fwd.treatment() != null) {
1367 // for (Instruction instr : fwd.treatment().allInstructions()) {
1368 // if (instr instanceof L3ModificationInstruction &&
1369 // ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1370 // tb.deferred().add(instr);
1371 // }
1372 // }
1373 // }
Saurav Das8a0732e2015-11-20 15:27:53 -08001374 } else {
1375 filteredSelector
1376 .matchEthType(Ethernet.MPLS_UNICAST)
1377 .matchMplsLabel(((MplsCriterion)
1378 selector.getCriterion(Criterion.Type.MPLS_LABEL)).label());
1379 MplsBosCriterion bos = (MplsBosCriterion) selector
Pier Ventre140a8942016-11-02 07:26:38 -07001380 .getCriterion(MPLS_BOS);
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001381 if (bos != null && requireMplsBosMatch()) {
Saurav Das8a0732e2015-11-20 15:27:53 -08001382 filteredSelector.matchMplsBos(bos.mplsBos());
1383 }
1384 forTableId = MPLS_TABLE_1;
Saurav Das4ce45962015-11-24 23:21:05 -08001385 log.debug("processing MPLS specific forwarding objective {} -> next:{}"
1386 + " in dev {}", fwd.id(), fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001387
Charles Chan14967c22015-12-07 11:11:50 -08001388 if (fwd.treatment() != null) {
1389 for (Instruction instr : fwd.treatment().allInstructions()) {
1390 if (instr instanceof L2ModificationInstruction &&
1391 ((L2ModificationInstruction) instr).subtype() == L2SubType.MPLS_POP) {
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001392 // OF-DPA does not pop in MPLS table in some cases. For the L3 VPN, it requires
Saurav Das9c705342017-01-06 11:36:01 -08001393 // setting the MPLS_TYPE so pop can happen down the pipeline
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001394 if (requireMplsPop()) {
1395 if (mplsNextTable == MPLS_TYPE_TABLE && isNotMplsBos(selector)) {
1396 tb.immediate().popMpls();
1397 }
1398 } else {
1399 // Skip mpls pop action for mpls_unicast label
1400 if (instr instanceof ModMplsHeaderInstruction &&
1401 !((ModMplsHeaderInstruction) instr).ethernetType()
1402 .equals(EtherType.MPLS_UNICAST.ethType())) {
1403 tb.immediate().add(instr);
1404 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001405 }
Charles Chan14967c22015-12-07 11:11:50 -08001406 }
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001407
1408 if (requireMplsTtlModification()) {
1409 if (instr instanceof L3ModificationInstruction &&
1410 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1411 // FIXME Should modify the app to send the correct DEC_MPLS_TTL instruction
1412 tb.immediate().decMplsTtl();
1413 }
1414 if (instr instanceof L3ModificationInstruction &&
1415 ((L3ModificationInstruction) instr).subtype() == L3SubType.TTL_IN) {
1416 tb.immediate().add(instr);
1417 }
Charles Chan14967c22015-12-07 11:11:50 -08001418 }
Saurav Das8a0732e2015-11-20 15:27:53 -08001419 }
1420 }
1421 }
Saurav Das822c4e22015-10-23 10:51:11 -07001422
1423 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001424 NextGroup next = getGroupForNextObjective(fwd.nextId());
1425 if (next != null) {
1426 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1427 // we only need the top level group's key to point the flow to it
1428 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1429 if (group == null) {
1430 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1431 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1432 fail(fwd, ObjectiveError.GROUPMISSING);
1433 return Collections.emptySet();
1434 }
Saurav Dasa4020382018-02-14 14:14:54 -08001435 if (isNotMplsBos(selector) && group.type().equals(SELECT)) {
1436 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
1437 + "is not implemented in OF-DPA yet. Aborting flow {} -> next:{} "
1438 + "in this device {}", fwd.id(), fwd.nextId(), deviceId);
1439 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1440 return Collections.emptySet();
1441 }
Saurav Das423fe2b2015-12-04 10:52:59 -08001442 tb.deferred().group(group.id());
Saurav Dasc568c342018-01-25 09:49:01 -08001443 // retrying flows may be necessary due to bug CORD-554
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001444 if (gkeys.size() == 1 && gkeys.get(0).size() == 1) {
Saurav Dasc568c342018-01-25 09:49:01 -08001445 if (shouldRetry()) {
1446 log.warn("Found empty group 0x{} in dev:{} .. will retry fwd:{}",
1447 Integer.toHexString(group.id().id()), deviceId,
1448 fwd.id());
1449 emptyGroup = true;
1450 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001451 }
Saurav Das25190812016-05-27 13:54:07 -07001452 } else {
1453 log.warn("Cannot find group for nextId:{} in dev:{}. Aborting fwd:{}",
1454 fwd.nextId(), deviceId, fwd.id());
1455 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1456 return Collections.emptySet();
Saurav Das822c4e22015-10-23 10:51:11 -07001457 }
Saurav Das822c4e22015-10-23 10:51:11 -07001458 }
Charles Chancad338a2016-09-16 18:03:11 -07001459
1460 if (forTableId == MPLS_TABLE_1) {
Pier Ventre140a8942016-11-02 07:26:38 -07001461 if (mplsNextTable == MPLS_L3_TYPE_TABLE) {
Charles Chancad338a2016-09-16 18:03:11 -07001462 Ofdpa3SetMplsType setMplsType = new Ofdpa3SetMplsType(Ofdpa3MplsType.L3_PHP);
Saurav Das9c705342017-01-06 11:36:01 -08001463 // set mpls type as apply_action
1464 tb.immediate().extension(setMplsType, deviceId);
Charles Chancad338a2016-09-16 18:03:11 -07001465 }
1466 tb.transition(mplsNextTable);
1467 } else {
1468 tb.transition(ACL_TABLE);
1469 }
1470
Andrea Campanellad980c6d2018-04-30 11:48:55 +02001471 if (fwd.treatment() != null && fwd.treatment().clearedDeferred()) {
1472 if (supportsUnicastBlackHole()) {
1473 tb.wipeDeferred();
1474 } else {
1475 log.warn("Clear Deferred is not supported Unicast Routing Table on device {}", deviceId);
1476 return Collections.emptySet();
1477 }
1478 }
1479
Saurav Das822c4e22015-10-23 10:51:11 -07001480 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1481 .fromApp(fwd.appId())
1482 .withPriority(fwd.priority())
1483 .forDevice(deviceId)
Saurav Das8a0732e2015-11-20 15:27:53 -08001484 .withSelector(filteredSelector.build())
1485 .withTreatment(tb.build())
1486 .forTable(forTableId);
Saurav Das822c4e22015-10-23 10:51:11 -07001487
1488 if (fwd.permanent()) {
1489 ruleBuilder.makePermanent();
1490 } else {
1491 ruleBuilder.makeTemporary(fwd.timeout());
1492 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001493 Collection<FlowRule> flowRuleCollection = new ArrayList<>();
1494 flowRuleCollection.add(ruleBuilder.build());
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001495 if (!allowDefaultRoute) {
Pier Ventree0ae7a32016-11-23 09:57:42 -08001496 flowRuleCollection.add(
1497 defaultRoute(fwd, complementarySelector, forTableId, tb)
1498 );
Flavio Castroe10fa242016-01-15 12:43:51 -08001499 log.debug("Default rule 0.0.0.0/0 is being installed two rules");
1500 }
Saurav Dasc568c342018-01-25 09:49:01 -08001501
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001502 if (emptyGroup) {
1503 executorService.schedule(new RetryFlows(fwd, flowRuleCollection),
1504 RETRY_MS, TimeUnit.MILLISECONDS);
1505 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001506 return flowRuleCollection;
Saurav Das822c4e22015-10-23 10:51:11 -07001507 }
1508
Charles Chan50d900c2018-03-02 13:26:22 -08001509 private int buildIpv4Selector(TrafficSelector.Builder builderToUpdate,
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001510 TrafficSelector.Builder extBuilder,
1511 ForwardingObjective fwd,
1512 boolean allowDefaultRoute) {
1513 TrafficSelector selector = fwd.selector();
1514
1515 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
1516 if (ipv4Dst.isMulticast()) {
1517 if (ipv4Dst.prefixLength() != 32) {
1518 log.warn("Multicast specific forwarding objective can only be /32");
1519 fail(fwd, ObjectiveError.BADPARAMS);
1520 return -1;
1521 }
1522 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
1523 if (assignedVlan == null) {
1524 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
1525 fail(fwd, ObjectiveError.BADPARAMS);
1526 return -1;
1527 }
Charles Chand1172632017-03-15 17:33:09 -07001528 if (requireVlanExtensions()) {
1529 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
1530 builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
1531 } else {
1532 builderToUpdate.matchVlanId(assignedVlan);
1533 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001534 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1535 log.debug("processing IPv4 multicast specific forwarding objective {} -> next:{}"
1536 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1537 } else {
1538 if (ipv4Dst.prefixLength() == 0) {
1539 if (allowDefaultRoute) {
1540 // The entire IPV4_DST field is wildcarded intentionally
1541 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4);
1542 } else {
1543 // NOTE: The switch does not support matching 0.0.0.0/0
1544 // Split it into 0.0.0.0/1 and 128.0.0.0/1
1545 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4)
1546 .matchIPDst(IpPrefix.valueOf("0.0.0.0/1"));
1547 extBuilder.matchEthType(Ethernet.TYPE_IPV4)
1548 .matchIPDst(IpPrefix.valueOf("128.0.0.0/1"));
1549 }
1550 } else {
1551 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1552 }
1553 log.debug("processing IPv4 unicast specific forwarding objective {} -> next:{}"
1554 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1555 }
1556 return 0;
1557 }
1558
1559 /**
1560 * Helper method to build Ipv6 selector using the selector provided by
1561 * a forwarding objective.
1562 *
1563 * @param builderToUpdate the builder to update
1564 * @param fwd the selector to read
1565 * @return 0 if the update ends correctly. -1 if the matches
1566 * are not yet supported
1567 */
Charles Chan50d900c2018-03-02 13:26:22 -08001568 int buildIpv6Selector(TrafficSelector.Builder builderToUpdate,
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001569 ForwardingObjective fwd) {
1570
1571 TrafficSelector selector = fwd.selector();
1572
1573 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
1574 if (ipv6Dst.isMulticast()) {
Julia Ferguson65428c32017-08-10 18:15:24 +00001575 if (ipv6Dst.prefixLength() != IpAddress.INET6_BIT_LENGTH) {
1576 log.warn("Multicast specific forwarding objective can only be /128");
1577 fail(fwd, ObjectiveError.BADPARAMS);
1578 return -1;
1579 }
1580 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
1581 if (assignedVlan == null) {
1582 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
1583 fail(fwd, ObjectiveError.BADPARAMS);
1584 return -1;
1585 }
1586 if (requireVlanExtensions()) {
1587 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
1588 builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
1589 } else {
1590 builderToUpdate.matchVlanId(assignedVlan);
1591 }
1592 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6).matchIPv6Dst(ipv6Dst);
1593 log.debug("processing IPv6 multicast specific forwarding objective {} -> next:{}"
1594 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1595 } else {
1596 if (ipv6Dst.prefixLength() != 0) {
1597 builderToUpdate.matchIPv6Dst(ipv6Dst);
1598 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001599 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6);
1600 log.debug("processing IPv6 unicast specific forwarding objective {} -> next:{}"
1601 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Julia Ferguson65428c32017-08-10 18:15:24 +00001602 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001603 return 0;
1604 }
1605
Charles Chan50d900c2018-03-02 13:26:22 -08001606 FlowRule defaultRoute(ForwardingObjective fwd,
Pier Ventree0ae7a32016-11-23 09:57:42 -08001607 TrafficSelector.Builder complementarySelector,
1608 int forTableId,
1609 TrafficTreatment.Builder tb) {
1610 FlowRule.Builder rule = DefaultFlowRule.builder()
1611 .fromApp(fwd.appId())
1612 .withPriority(fwd.priority())
1613 .forDevice(deviceId)
1614 .withSelector(complementarySelector.build())
1615 .withTreatment(tb.build())
1616 .forTable(forTableId);
1617 if (fwd.permanent()) {
1618 rule.makePermanent();
1619 } else {
1620 rule.makeTemporary(fwd.timeout());
1621 }
1622 return rule.build();
1623 }
1624
Saurav Das4ce45962015-11-24 23:21:05 -08001625 /**
1626 * Handles forwarding rules to the L2 bridging table. Flow actions are not
1627 * allowed in the bridging table - instead we use L2 Interface group or
1628 * L2 flood group
1629 *
1630 * @param fwd the forwarding objective
1631 * @return A collection of flow rules, or an empty set
1632 */
1633 protected Collection<FlowRule> processEthDstSpecific(ForwardingObjective fwd) {
1634 List<FlowRule> rules = new ArrayList<>();
1635
1636 // Build filtered selector
1637 TrafficSelector selector = fwd.selector();
1638 EthCriterion ethCriterion = (EthCriterion) selector
1639 .getCriterion(Criterion.Type.ETH_DST);
1640 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) selector
1641 .getCriterion(Criterion.Type.VLAN_VID);
1642
1643 if (vlanIdCriterion == null) {
1644 log.warn("Forwarding objective for bridging requires vlan. Not "
1645 + "installing fwd:{} in dev:{}", fwd.id(), deviceId);
1646 fail(fwd, ObjectiveError.BADPARAMS);
1647 return Collections.emptySet();
1648 }
1649
1650 TrafficSelector.Builder filteredSelectorBuilder =
1651 DefaultTrafficSelector.builder();
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001652
1653 if (!ethCriterion.mac().equals(NONE) &&
1654 !ethCriterion.mac().equals(BROADCAST)) {
Saurav Das4ce45962015-11-24 23:21:05 -08001655 filteredSelectorBuilder.matchEthDst(ethCriterion.mac());
1656 log.debug("processing L2 forwarding objective:{} -> next:{} in dev:{}",
1657 fwd.id(), fwd.nextId(), deviceId);
1658 } else {
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001659 // Use wildcard DST_MAC if the MacAddress is None or Broadcast
Saurav Das4ce45962015-11-24 23:21:05 -08001660 log.debug("processing L2 Broadcast forwarding objective:{} -> next:{} "
1661 + "in dev:{} for vlan:{}",
1662 fwd.id(), fwd.nextId(), deviceId, vlanIdCriterion.vlanId());
1663 }
Charles Chand1172632017-03-15 17:33:09 -07001664 if (requireVlanExtensions()) {
1665 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanIdCriterion.vlanId());
1666 filteredSelectorBuilder.extension(ofdpaMatchVlanVid, deviceId);
1667 } else {
1668 filteredSelectorBuilder.matchVlanId(vlanIdCriterion.vlanId());
1669 }
Saurav Das4ce45962015-11-24 23:21:05 -08001670 TrafficSelector filteredSelector = filteredSelectorBuilder.build();
1671
1672 if (fwd.treatment() != null) {
1673 log.warn("Ignoring traffic treatment in fwd rule {} meant for L2 table"
1674 + "for dev:{}. Expecting only nextId", fwd.id(), deviceId);
1675 }
1676
1677 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1678 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001679 NextGroup next = getGroupForNextObjective(fwd.nextId());
Saurav Das4ce45962015-11-24 23:21:05 -08001680 if (next != null) {
1681 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1682 // we only need the top level group's key to point the flow to it
1683 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1684 if (group != null) {
1685 treatmentBuilder.deferred().group(group.id());
1686 } else {
1687 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1688 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1689 fail(fwd, ObjectiveError.GROUPMISSING);
1690 return Collections.emptySet();
1691 }
1692 }
1693 }
1694 treatmentBuilder.immediate().transition(ACL_TABLE);
1695 TrafficTreatment filteredTreatment = treatmentBuilder.build();
1696
1697 // Build bridging table entries
1698 FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder();
1699 flowRuleBuilder.fromApp(fwd.appId())
1700 .withPriority(fwd.priority())
1701 .forDevice(deviceId)
1702 .withSelector(filteredSelector)
1703 .withTreatment(filteredTreatment)
1704 .forTable(BRIDGING_TABLE);
1705 if (fwd.permanent()) {
1706 flowRuleBuilder.makePermanent();
1707 } else {
1708 flowRuleBuilder.makeTemporary(fwd.timeout());
1709 }
1710 rules.add(flowRuleBuilder.build());
1711 return rules;
1712 }
1713
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001714 //////////////////////////////////////
1715 // Helper Methods and Classes
1716 //////////////////////////////////////
1717
1718 private boolean isSupportedEthTypeObjective(ForwardingObjective fwd) {
1719 TrafficSelector selector = fwd.selector();
1720 EthTypeCriterion ethType = (EthTypeCriterion) selector
1721 .getCriterion(Criterion.Type.ETH_TYPE);
1722 return !((ethType == null) ||
1723 ((ethType.ethType().toShort() != Ethernet.TYPE_IPV4) &&
Pier Ventree0ae7a32016-11-23 09:57:42 -08001724 (ethType.ethType().toShort() != Ethernet.MPLS_UNICAST)) &&
1725 (ethType.ethType().toShort() != Ethernet.TYPE_IPV6));
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001726 }
1727
1728 private boolean isSupportedEthDstObjective(ForwardingObjective fwd) {
1729 TrafficSelector selector = fwd.selector();
1730 EthCriterion ethDst = (EthCriterion) selector
1731 .getCriterion(Criterion.Type.ETH_DST);
1732 VlanIdCriterion vlanId = (VlanIdCriterion) selector
1733 .getCriterion(Criterion.Type.VLAN_VID);
1734 return !(ethDst == null && vlanId == null);
1735 }
1736
Charles Chan50d900c2018-03-02 13:26:22 -08001737 NextGroup getGroupForNextObjective(Integer nextId) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001738 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
1739 if (next != null) {
1740 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1741 if (gkeys != null && !gkeys.isEmpty()) {
1742 return next;
1743 } else {
1744 log.warn("Empty next group found in FlowObjective store for "
1745 + "next-id:{} in dev:{}", nextId, deviceId);
1746 }
1747 } else {
1748 log.warn("next-id {} not found in Flow objective store for dev:{}",
1749 nextId, deviceId);
1750 }
1751 return null;
1752 }
1753
Charles Chan188ebf52015-12-23 00:15:11 -08001754 protected static void pass(Objective obj) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001755 obj.context().ifPresent(context -> context.onSuccess(obj));
Saurav Das822c4e22015-10-23 10:51:11 -07001756 }
1757
Charles Chan188ebf52015-12-23 00:15:11 -08001758 protected static void fail(Objective obj, ObjectiveError error) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001759 obj.context().ifPresent(context -> context.onError(obj, error));
Saurav Das822c4e22015-10-23 10:51:11 -07001760 }
Saurav Das24431192016-03-07 19:13:00 -08001761
Saurav Das24431192016-03-07 19:13:00 -08001762 @Override
1763 public List<String> getNextMappings(NextGroup nextGroup) {
1764 List<String> mappings = new ArrayList<>();
1765 List<Deque<GroupKey>> gkeys = appKryo.deserialize(nextGroup.data());
1766 for (Deque<GroupKey> gkd : gkeys) {
1767 Group lastGroup = null;
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -07001768 StringBuilder gchain = new StringBuilder();
Saurav Das24431192016-03-07 19:13:00 -08001769 for (GroupKey gk : gkd) {
1770 Group g = groupService.getGroup(deviceId, gk);
Saurav Das8be4e3a2016-03-11 17:19:07 -08001771 if (g == null) {
Saurav Das25190812016-05-27 13:54:07 -07001772 gchain.append(" NoGrp").append(" -->");
Saurav Das8be4e3a2016-03-11 17:19:07 -08001773 continue;
1774 }
1775 gchain.append(" 0x").append(Integer.toHexString(g.id().id()))
1776 .append(" -->");
Saurav Das24431192016-03-07 19:13:00 -08001777 lastGroup = g;
1778 }
1779 // add port information for last group in group-chain
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -07001780 List<Instruction> lastGroupIns = new ArrayList<>();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001781 if (lastGroup != null && !lastGroup.buckets().buckets().isEmpty()) {
Saurav Das25190812016-05-27 13:54:07 -07001782 lastGroupIns = lastGroup.buckets().buckets().get(0)
1783 .treatment().allInstructions();
1784 }
1785 for (Instruction i: lastGroupIns) {
Saurav Das24431192016-03-07 19:13:00 -08001786 if (i instanceof OutputInstruction) {
Saurav Das8be4e3a2016-03-11 17:19:07 -08001787 gchain.append(" port:").append(((OutputInstruction) i).port());
Saurav Das24431192016-03-07 19:13:00 -08001788 }
1789 }
Saurav Das8be4e3a2016-03-11 17:19:07 -08001790 mappings.add(gchain.toString());
Saurav Das24431192016-03-07 19:13:00 -08001791 }
1792 return mappings;
1793 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001794
Saurav Dasa4020382018-02-14 14:14:54 -08001795 /**
1796 * Returns true iff the given selector matches on BOS==true, indicating that
1797 * the selector is trying to match on a label that is bottom-of-stack.
1798 *
1799 * @param selector the given match
1800 * @return true iff BoS==true; false if BOS==false, or BOS matching is not
1801 * expressed in the given selector
1802 */
Pier Ventre140a8942016-11-02 07:26:38 -07001803 static boolean isMplsBos(TrafficSelector selector) {
1804 MplsBosCriterion bosCriterion = (MplsBosCriterion) selector.getCriterion(MPLS_BOS);
1805 return bosCriterion != null && bosCriterion.mplsBos();
1806 }
1807
Saurav Dasa4020382018-02-14 14:14:54 -08001808 /**
1809 * Returns true iff the given selector matches on BOS==false, indicating
1810 * that the selector is trying to match on a label that is not the
1811 * bottom-of-stack label.
1812 *
1813 * @param selector the given match
1814 * @return true iff BoS==false;
1815 * false if BOS==true, or BOS matching is not expressed in the given selector
1816 */
Pier Ventre140a8942016-11-02 07:26:38 -07001817 static boolean isNotMplsBos(TrafficSelector selector) {
1818 MplsBosCriterion bosCriterion = (MplsBosCriterion) selector.getCriterion(MPLS_BOS);
1819 return bosCriterion != null && !bosCriterion.mplsBos();
1820 }
1821
Saurav Dasa4020382018-02-14 14:14:54 -08001822 /**
1823 * Returns true iff the forwarding objective includes a treatment to pop the
1824 * MPLS label.
1825 *
1826 * @param fwd the given forwarding objective
1827 * @return true iff mpls pop treatment exists
1828 */
1829 static boolean isMplsPop(ForwardingObjective fwd) {
1830 if (fwd.treatment() != null) {
1831 for (Instruction instr : fwd.treatment().allInstructions()) {
1832 if (instr instanceof L2ModificationInstruction
1833 && ((L2ModificationInstruction) instr)
1834 .subtype() == L2SubType.MPLS_POP) {
1835 return true;
1836 }
1837 }
1838 }
1839 return false;
1840 }
1841
Charles Chand9e47c62017-10-05 15:17:15 -07001842 private static boolean isIpv6(TrafficSelector selector) {
1843 EthTypeCriterion ethTypeCriterion = (EthTypeCriterion) selector.getCriterion(ETH_TYPE);
1844 return ethTypeCriterion != null && ethTypeCriterion.ethType().toShort() == Ethernet.TYPE_IPV6;
1845 }
1846
Charles Chan50d900c2018-03-02 13:26:22 -08001847 static VlanId readVlanFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001848 if (selector == null) {
1849 return null;
1850 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001851 Criterion criterion = selector.getCriterion(Criterion.Type.VLAN_VID);
1852 return (criterion == null)
1853 ? null : ((VlanIdCriterion) criterion).vlanId();
1854 }
1855
jayakumarthazhath655b9a82018-10-01 00:51:54 +05301856 static MacAddress readEthDstFromSelector(TrafficSelector selector) {
1857 if (selector == null) {
1858 return null;
1859 }
1860 Criterion criterion = selector.getCriterion(Criterion.Type.ETH_DST);
1861 return (criterion == null)
1862 ? null : ((EthCriterion) criterion).mac();
1863 }
1864
Charles Chand9e47c62017-10-05 15:17:15 -07001865 static IpPrefix readIpDstFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001866 if (selector == null) {
1867 return null;
1868 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001869 Criterion criterion = selector.getCriterion(Criterion.Type.IPV4_DST);
1870 return (criterion == null) ? null : ((IPCriterion) criterion).ip();
1871 }
Charles Chand55e84d2016-03-30 17:54:24 -07001872
1873 private static VlanId readVlanFromTreatment(TrafficTreatment treatment) {
Saurav Das59232cf2016-04-27 18:35:50 -07001874 if (treatment == null) {
1875 return null;
1876 }
Charles Chand55e84d2016-03-30 17:54:24 -07001877 for (Instruction i : treatment.allInstructions()) {
1878 if (i instanceof ModVlanIdInstruction) {
1879 return ((ModVlanIdInstruction) i).vlanId();
1880 }
1881 }
1882 return null;
1883 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001884
Jonghwan Hyun800d9d02018-04-09 09:40:50 -07001885 protected static MacAddress readEthDstFromTreatment(TrafficTreatment treatment) {
Charles Chan45b69ab2018-03-02 15:41:41 -08001886 if (treatment == null) {
1887 return null;
1888 }
1889 for (Instruction i : treatment.allInstructions()) {
1890 if (i instanceof ModEtherInstruction) {
1891 ModEtherInstruction modEtherInstruction = (ModEtherInstruction) i;
1892 if (modEtherInstruction.subtype() == L2SubType.ETH_DST) {
1893 return modEtherInstruction.mac();
1894 }
1895 }
1896 }
1897 return null;
1898 }
1899
jayakumarthazhath655b9a82018-10-01 00:51:54 +05301900 static ExtensionSelector readExtensionFromSelector(TrafficSelector selector) {
1901 if (selector == null) {
1902 return null;
1903 }
1904 ExtensionCriterion criterion = (ExtensionCriterion) selector.getCriterion(Criterion.Type.EXTENSION);
1905 return (criterion == null) ? null : criterion.extensionSelector();
1906 }
1907
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001908 /**
1909 * Utility class that retries sending flows a fixed number of times, even if
1910 * some of the attempts are successful. Used only for forwarding objectives.
1911 */
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001912 public final class RetryFlows implements Runnable {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001913 int attempts = MAX_RETRY_ATTEMPTS;
1914 private Collection<FlowRule> retryFlows;
1915 private ForwardingObjective fwd;
1916
Charles Chan50d900c2018-03-02 13:26:22 -08001917 RetryFlows(ForwardingObjective fwd, Collection<FlowRule> retryFlows) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001918 this.fwd = fwd;
1919 this.retryFlows = retryFlows;
1920 }
1921
1922 @Override
1923 public void run() {
1924 log.info("RETRY FLOWS ATTEMPT# {} for fwd:{} rules:{}",
1925 MAX_RETRY_ATTEMPTS - attempts, fwd.id(), retryFlows.size());
1926 sendForward(fwd, retryFlows);
1927 if (--attempts > 0) {
1928 executorService.schedule(this, RETRY_MS, TimeUnit.MILLISECONDS);
1929 }
1930 }
1931 }
1932
Saurav Das822c4e22015-10-23 10:51:11 -07001933}