blob: d834b4865b80f7e4bc7b8f92c7bc55a9e9eb730a [file] [log] [blame]
Saurav Das822c4e22015-10-23 10:51:11 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Saurav Das822c4e22015-10-23 10:51:11 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Yi Tsengef19de12017-04-24 11:33:05 -070016package org.onosproject.driver.pipeline.ofdpa;
Saurav Das822c4e22015-10-23 10:51:11 -070017
Charles Chan5270ed02016-01-30 23:22:37 -080018import com.google.common.collect.ImmutableList;
Yi Tseng47f82dc2017-03-05 22:48:39 -080019import com.google.common.collect.Sets;
Saurav Das822c4e22015-10-23 10:51:11 -070020import org.onlab.osgi.ServiceDirectory;
Jonghwan Hyun800d9d02018-04-09 09:40:50 -070021import org.onlab.packet.EthType;
Saurav Das822c4e22015-10-23 10:51:11 -070022import org.onlab.packet.Ethernet;
Alex Yashchuk4caa8e82017-12-08 17:40:05 +020023import org.onlab.packet.EthType.EtherType;
Julia Ferguson65428c32017-08-10 18:15:24 +000024import org.onlab.packet.IpAddress;
Charles Chan14967c22015-12-07 11:11:50 -080025import org.onlab.packet.IpPrefix;
Charles Chan45b69ab2018-03-02 15:41:41 -080026import org.onlab.packet.MacAddress;
Saurav Das822c4e22015-10-23 10:51:11 -070027import org.onlab.packet.VlanId;
28import org.onlab.util.KryoNamespace;
29import org.onosproject.core.ApplicationId;
30import org.onosproject.core.CoreService;
Jonghwan Hyun800d9d02018-04-09 09:40:50 -070031import org.onosproject.driver.extensions.Ofdpa3CopyField;
Charles Chancad338a2016-09-16 18:03:11 -070032import org.onosproject.driver.extensions.Ofdpa3MplsType;
33import org.onosproject.driver.extensions.Ofdpa3SetMplsType;
Jonghwan Hyun800d9d02018-04-09 09:40:50 -070034import org.onosproject.driver.extensions.OfdpaMatchActsetOutput;
35import org.onosproject.driver.extensions.OfdpaMatchAllowVlanTranslation;
Charles Chan14967c22015-12-07 11:11:50 -080036import org.onosproject.driver.extensions.OfdpaMatchVlanVid;
37import org.onosproject.driver.extensions.OfdpaSetVlanVid;
Saurav Das822c4e22015-10-23 10:51:11 -070038import org.onosproject.net.DeviceId;
39import org.onosproject.net.Port;
40import org.onosproject.net.PortNumber;
41import org.onosproject.net.behaviour.NextGroup;
42import org.onosproject.net.behaviour.Pipeliner;
43import org.onosproject.net.behaviour.PipelinerContext;
44import org.onosproject.net.device.DeviceService;
45import org.onosproject.net.driver.AbstractHandlerBehaviour;
46import org.onosproject.net.flow.DefaultFlowRule;
47import org.onosproject.net.flow.DefaultTrafficSelector;
48import org.onosproject.net.flow.DefaultTrafficTreatment;
49import org.onosproject.net.flow.FlowRule;
50import org.onosproject.net.flow.FlowRuleOperations;
51import org.onosproject.net.flow.FlowRuleOperationsContext;
52import org.onosproject.net.flow.FlowRuleService;
53import org.onosproject.net.flow.TrafficSelector;
54import org.onosproject.net.flow.TrafficTreatment;
55import org.onosproject.net.flow.criteria.Criteria;
56import org.onosproject.net.flow.criteria.Criterion;
57import org.onosproject.net.flow.criteria.EthCriterion;
58import org.onosproject.net.flow.criteria.EthTypeCriterion;
59import org.onosproject.net.flow.criteria.IPCriterion;
Pier Ventree0ae7a32016-11-23 09:57:42 -080060import org.onosproject.net.flow.criteria.Icmpv6CodeCriterion;
61import org.onosproject.net.flow.criteria.Icmpv6TypeCriterion;
Saurav Das8a0732e2015-11-20 15:27:53 -080062import org.onosproject.net.flow.criteria.MplsBosCriterion;
63import org.onosproject.net.flow.criteria.MplsCriterion;
Saurav Das822c4e22015-10-23 10:51:11 -070064import org.onosproject.net.flow.criteria.PortCriterion;
Charles Chand9e47c62017-10-05 15:17:15 -070065import org.onosproject.net.flow.criteria.TcpPortCriterion;
66import org.onosproject.net.flow.criteria.UdpPortCriterion;
Saurav Das822c4e22015-10-23 10:51:11 -070067import org.onosproject.net.flow.criteria.VlanIdCriterion;
68import org.onosproject.net.flow.instructions.Instruction;
69import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
70import org.onosproject.net.flow.instructions.L2ModificationInstruction;
Saurav Das8a0732e2015-11-20 15:27:53 -080071import org.onosproject.net.flow.instructions.L2ModificationInstruction.L2SubType;
Saurav Das822c4e22015-10-23 10:51:11 -070072import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanIdInstruction;
Charles Chan45b69ab2018-03-02 15:41:41 -080073import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModEtherInstruction;
Alex Yashchuk4caa8e82017-12-08 17:40:05 +020074import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModMplsHeaderInstruction;
Charles Chan14967c22015-12-07 11:11:50 -080075import org.onosproject.net.flow.instructions.L3ModificationInstruction;
76import org.onosproject.net.flow.instructions.L3ModificationInstruction.L3SubType;
Saurav Das822c4e22015-10-23 10:51:11 -070077import org.onosproject.net.flowobjective.FilteringObjective;
78import org.onosproject.net.flowobjective.FlowObjectiveStore;
79import org.onosproject.net.flowobjective.ForwardingObjective;
80import org.onosproject.net.flowobjective.NextObjective;
81import org.onosproject.net.flowobjective.Objective;
82import org.onosproject.net.flowobjective.ObjectiveError;
Saurav Das822c4e22015-10-23 10:51:11 -070083import org.onosproject.net.group.DefaultGroupKey;
84import org.onosproject.net.group.Group;
Saurav Das822c4e22015-10-23 10:51:11 -070085import org.onosproject.net.group.GroupKey;
Saurav Das822c4e22015-10-23 10:51:11 -070086import org.onosproject.net.group.GroupService;
Saurav Das822c4e22015-10-23 10:51:11 -070087import org.onosproject.store.serializers.KryoNamespaces;
88import org.slf4j.Logger;
89
Pier Ventree0ae7a32016-11-23 09:57:42 -080090import java.util.ArrayDeque;
91import java.util.ArrayList;
92import java.util.Collection;
93import java.util.Collections;
94import java.util.Deque;
95import java.util.List;
96import java.util.Objects;
Jonghwan Hyun800d9d02018-04-09 09:40:50 -070097import java.util.Optional;
Pier Ventree0ae7a32016-11-23 09:57:42 -080098import java.util.concurrent.ScheduledExecutorService;
99import java.util.concurrent.TimeUnit;
100
101import static java.util.concurrent.Executors.newScheduledThreadPool;
Yi Tseng3a77b4f2017-02-06 15:02:17 -0800102import static org.onlab.packet.MacAddress.BROADCAST;
Charles Chanb4879a52017-10-20 19:09:16 -0700103import static org.onlab.packet.MacAddress.IPV4_MULTICAST;
104import static org.onlab.packet.MacAddress.IPV6_MULTICAST;
Yi Tseng3a77b4f2017-02-06 15:02:17 -0800105import static org.onlab.packet.MacAddress.NONE;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800106import static org.onlab.util.Tools.groupedThreads;
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700107import static org.onosproject.driver.extensions.Ofdpa3CopyField.OXM_ID_PACKET_REG_1;
108import static org.onosproject.driver.extensions.Ofdpa3CopyField.OXM_ID_VLAN_VID;
Yi Tsengef19de12017-04-24 11:33:05 -0700109import static org.onosproject.driver.pipeline.ofdpa.OfdpaGroupHandlerUtility.*;
Charles Chand9e47c62017-10-05 15:17:15 -0700110import static org.onosproject.net.flow.criteria.Criterion.Type.ETH_TYPE;
Pier Luigi075f1012018-02-01 10:23:12 +0100111import static org.onosproject.net.group.GroupDescription.Type.SELECT;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800112import static org.slf4j.LoggerFactory.getLogger;
Pier Ventre140a8942016-11-02 07:26:38 -0700113import static org.onosproject.net.flow.criteria.Criterion.Type.MPLS_BOS;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800114
Saurav Das822c4e22015-10-23 10:51:11 -0700115/**
116 * Driver for Broadcom's OF-DPA v2.0 TTP.
Saurav Das822c4e22015-10-23 10:51:11 -0700117 */
Charles Chan361154b2016-03-24 10:23:39 -0700118public class Ofdpa2Pipeline extends AbstractHandlerBehaviour implements Pipeliner {
Saurav Das822c4e22015-10-23 10:51:11 -0700119 protected static final int PORT_TABLE = 0;
120 protected static final int VLAN_TABLE = 10;
Pier Ventre42287df2016-11-09 14:17:26 -0800121 protected static final int VLAN_1_TABLE = 11;
122 protected static final int MPLS_L2_PORT_FLOW_TABLE = 13;
123 protected static final int MPLS_L2_PORT_PCP_TRUST_FLOW_TABLE = 16;
Saurav Das822c4e22015-10-23 10:51:11 -0700124 protected static final int TMAC_TABLE = 20;
125 protected static final int UNICAST_ROUTING_TABLE = 30;
126 protected static final int MULTICAST_ROUTING_TABLE = 40;
127 protected static final int MPLS_TABLE_0 = 23;
128 protected static final int MPLS_TABLE_1 = 24;
Pier Ventre140a8942016-11-02 07:26:38 -0700129 protected static final int MPLS_L3_TYPE_TABLE = 27;
130 protected static final int MPLS_TYPE_TABLE = 29;
Saurav Das822c4e22015-10-23 10:51:11 -0700131 protected static final int BRIDGING_TABLE = 50;
132 protected static final int ACL_TABLE = 60;
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700133 protected static final int EGRESS_VLAN_FLOW_TABLE = 210;
134 protected static final int EGRESS_DSCP_PCP_REMARK_FLOW_TABLE = 230;
135 protected static final int EGRESS_TPID_FLOW_TABLE = 235;
Saurav Das822c4e22015-10-23 10:51:11 -0700136 protected static final int MAC_LEARNING_TABLE = 254;
137 protected static final long OFPP_MAX = 0xffffff00L;
138
Saurav Das52025962016-01-28 22:30:01 -0800139 protected static final int HIGHEST_PRIORITY = 0xffff;
Saurav Das2857f382015-11-03 14:39:27 -0800140 protected static final int DEFAULT_PRIORITY = 0x8000;
Saurav Das822c4e22015-10-23 10:51:11 -0700141 protected static final int LOWEST_PRIORITY = 0x0;
142
Pier Ventre42287df2016-11-09 14:17:26 -0800143 protected static final int MPLS_L2_PORT_PRIORITY = 2;
Pier Ventre42287df2016-11-09 14:17:26 -0800144 protected static final int MPLS_TUNNEL_ID_BASE = 0x10000;
145 protected static final int MPLS_TUNNEL_ID_MAX = 0x1FFFF;
146
Pier Ventre70d53ba2016-11-17 22:26:29 -0800147 protected static final int MPLS_UNI_PORT_MAX = 0x0000FFFF;
Pier Ventre70d53ba2016-11-17 22:26:29 -0800148 protected static final int MPLS_NNI_PORT_BASE = 0x00020000;
149 protected static final int MPLS_NNI_PORT_MAX = 0x0002FFFF;
150
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700151 protected static final short ALLOW_VLAN_TRANSLATION = 1;
152 protected static final int COPY_FIELD_NBITS = 12;
153 protected static final int COPY_FIELD_OFFSET = 0;
154
Saurav Das822c4e22015-10-23 10:51:11 -0700155 private final Logger log = getLogger(getClass());
Charles Chan425854b2016-04-11 15:32:12 -0700156 protected ServiceDirectory serviceDirectory;
Saurav Das822c4e22015-10-23 10:51:11 -0700157 protected FlowRuleService flowRuleService;
Charles Chan425854b2016-04-11 15:32:12 -0700158 protected CoreService coreService;
Saurav Das8a0732e2015-11-20 15:27:53 -0800159 protected GroupService groupService;
160 protected FlowObjectiveStore flowObjectiveStore;
Saurav Das822c4e22015-10-23 10:51:11 -0700161 protected DeviceId deviceId;
162 protected ApplicationId driverId;
Saurav Das822c4e22015-10-23 10:51:11 -0700163 protected DeviceService deviceService;
Charles Chan188ebf52015-12-23 00:15:11 -0800164 protected static KryoNamespace appKryo = new KryoNamespace.Builder()
Yi Tsengef19de12017-04-24 11:33:05 -0700165 .register(KryoNamespaces.API)
166 .register(GroupKey.class)
167 .register(DefaultGroupKey.class)
168 .register(OfdpaNextGroup.class)
169 .register(ArrayDeque.class)
170 .build("Ofdpa2Pipeline");
Saurav Das822c4e22015-10-23 10:51:11 -0700171
Charles Chan425854b2016-04-11 15:32:12 -0700172 protected Ofdpa2GroupHandler groupHandler;
Saurav Das822c4e22015-10-23 10:51:11 -0700173
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700174 // flows installations to be retried
Charles Chan50d900c2018-03-02 13:26:22 -0800175 private ScheduledExecutorService executorService
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700176 = newScheduledThreadPool(5, groupedThreads("OfdpaPipeliner", "retry-%d", log));
Charles Chan50d900c2018-03-02 13:26:22 -0800177 private static final int MAX_RETRY_ATTEMPTS = 10;
178 private static final int RETRY_MS = 1000;
Saurav Das4f980082015-11-05 13:39:15 -0800179
Saurav Das822c4e22015-10-23 10:51:11 -0700180 @Override
181 public void init(DeviceId deviceId, PipelinerContext context) {
Saurav Das822c4e22015-10-23 10:51:11 -0700182 this.deviceId = deviceId;
183
Charles Chan425854b2016-04-11 15:32:12 -0700184 serviceDirectory = context.directory();
Saurav Das822c4e22015-10-23 10:51:11 -0700185 coreService = serviceDirectory.get(CoreService.class);
186 flowRuleService = serviceDirectory.get(FlowRuleService.class);
187 groupService = serviceDirectory.get(GroupService.class);
188 flowObjectiveStore = context.store();
Saurav Das822c4e22015-10-23 10:51:11 -0700189 deviceService = serviceDirectory.get(DeviceService.class);
Saurav Das822c4e22015-10-23 10:51:11 -0700190
Charles Chan40132b32017-01-22 00:19:37 -0800191 initDriverId();
192 initGroupHander(context);
Saurav Das822c4e22015-10-23 10:51:11 -0700193
Saurav Das822c4e22015-10-23 10:51:11 -0700194 initializePipeline();
Saurav Das822c4e22015-10-23 10:51:11 -0700195 }
196
Charles Chan40132b32017-01-22 00:19:37 -0800197 protected void initDriverId() {
198 driverId = coreService.registerApplication(
199 "org.onosproject.driver.Ofdpa2Pipeline");
200 }
201
202 protected void initGroupHander(PipelinerContext context) {
203 groupHandler = new Ofdpa2GroupHandler();
204 groupHandler.init(deviceId, context);
205 }
206
Saurav Das822c4e22015-10-23 10:51:11 -0700207 protected void initializePipeline() {
Charles Chan188ebf52015-12-23 00:15:11 -0800208 // OF-DPA does not require initializing the pipeline as it puts default
209 // rules automatically in the hardware. However emulation of OFDPA in
210 // software switches does require table-miss-entries.
Saurav Das822c4e22015-10-23 10:51:11 -0700211 }
212
Charles Chand1172632017-03-15 17:33:09 -0700213 /**
Alex Yashchuk4caa8e82017-12-08 17:40:05 +0200214 * Determines whether this pipeline requires MPLS POP instruction.
215 *
216 * @return true to use MPLS POP instruction
217 */
218 public boolean requireMplsPop() {
219 return true;
220 }
221
222 /**
223 * Determines whether this pipeline requires MPLS BOS match.
224 *
225 * @return true to use MPLS BOS match
226 */
227 public boolean requireMplsBosMatch() {
228 return true;
229 }
230
231 /**
232 * Determines whether this pipeline requires MPLS TTL decrement and copy.
233 *
234 * @return true to use MPLS TTL decrement and copy
235 */
236 public boolean requireMplsTtlModification() {
237 return true;
238 }
239
240 /**
Charles Chand1172632017-03-15 17:33:09 -0700241 * Determines whether this pipeline requires OFDPA match and set VLAN extensions.
242 *
243 * @return true to use the extensions
244 */
245 protected boolean requireVlanExtensions() {
246 return true;
247 }
248
Saurav Das86d13e82017-04-28 17:03:48 -0700249 /**
250 * Determines whether in-port should be matched on in TMAC table rules.
251 *
252 * @return true if match on in-port should be programmed
253 */
254 protected boolean matchInPortTmacTable() {
255 return true;
256 }
257
Charles Chand9e47c62017-10-05 15:17:15 -0700258 /**
259 * Determines whether matching L4 destination port on IPv6 packets is supported in ACL table.
260 *
261 * @return true if matching L4 destination port on IPv6 packets is supported in ACL table.
262 */
263 protected boolean supportIpv6L4Dst() {
264 return true;
265 }
266
Saurav Dasc568c342018-01-25 09:49:01 -0800267 /**
268 * Determines whether this driver should continue to retry flows that point
269 * to empty groups. See CORD-554.
270 *
271 * @return true if the driver should retry flows
272 */
273 protected boolean shouldRetry() {
274 return true;
275 }
276
Charles Chan45b69ab2018-03-02 15:41:41 -0800277 /**
278 * Determines whether this driver requires unicast flow to be installed before multicast flow
279 * in TMAC table.
280 *
281 * @return true if required
282 */
283 protected boolean requireUnicastBeforeMulticast() {
284 return false;
285 }
286
Andrea Campanellad980c6d2018-04-30 11:48:55 +0200287 /**
288 * Determines whether this driver supports installing a clearDeferred action on table 30.
289 *
290 * @return true if required
291 */
292 protected boolean supportsUnicastBlackHole() {
293 return true;
294 }
295
Saurav Das822c4e22015-10-23 10:51:11 -0700296 //////////////////////////////////////
297 // Flow Objectives
298 //////////////////////////////////////
299
300 @Override
301 public void filter(FilteringObjective filteringObjective) {
302 if (filteringObjective.type() == FilteringObjective.Type.PERMIT) {
303 processFilter(filteringObjective,
304 filteringObjective.op() == Objective.Operation.ADD,
305 filteringObjective.appId());
306 } else {
307 // Note that packets that don't match the PERMIT filter are
308 // automatically denied. The DENY filter is used to deny packets
309 // that are otherwise permitted by the PERMIT filter.
310 // Use ACL table flow rules here for DENY filtering objectives
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530311 log.warn("filter objective other than PERMIT currently not supported");
Saurav Das822c4e22015-10-23 10:51:11 -0700312 fail(filteringObjective, ObjectiveError.UNSUPPORTED);
313 }
314 }
315
316 @Override
317 public void forward(ForwardingObjective fwd) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700318 Collection<FlowRule> rules = processForward(fwd);
Saurav Das25190812016-05-27 13:54:07 -0700319 if (rules == null || rules.isEmpty()) {
320 // Assumes fail message has already been generated to the objective
321 // context. Returning here prevents spurious pass message to be
322 // generated by FlowRule service for empty flowOps.
323 return;
324 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700325 sendForward(fwd, rules);
326 }
327
Charles Chan50d900c2018-03-02 13:26:22 -0800328 private void sendForward(ForwardingObjective fwd, Collection<FlowRule> rules) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700329 FlowRuleOperations.Builder flowOpsBuilder = FlowRuleOperations.builder();
Saurav Das822c4e22015-10-23 10:51:11 -0700330 switch (fwd.op()) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700331 case ADD:
332 rules.stream()
333 .filter(Objects::nonNull)
334 .forEach(flowOpsBuilder::add);
Saurav Dasd2fded02016-12-02 15:43:47 -0800335 log.debug("Applying a add fwd-obj {} to sw:{}", fwd.id(), deviceId);
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700336 break;
337 case REMOVE:
338 rules.stream()
339 .filter(Objects::nonNull)
340 .forEach(flowOpsBuilder::remove);
Pier Ventre42287df2016-11-09 14:17:26 -0800341 log.debug("Deleting a flow rule to sw:{}", deviceId);
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700342 break;
343 default:
344 fail(fwd, ObjectiveError.UNKNOWN);
345 log.warn("Unknown forwarding type {}", fwd.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700346 }
347
Saurav Das822c4e22015-10-23 10:51:11 -0700348 flowRuleService.apply(flowOpsBuilder.build(new FlowRuleOperationsContext() {
349 @Override
350 public void onSuccess(FlowRuleOperations ops) {
351 pass(fwd);
352 }
353
354 @Override
355 public void onError(FlowRuleOperations ops) {
356 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
357 }
358 }));
Saurav Das822c4e22015-10-23 10:51:11 -0700359 }
360
361 @Override
362 public void next(NextObjective nextObjective) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800363 NextGroup nextGroup = flowObjectiveStore.getNextGroup(nextObjective.id());
364 switch (nextObjective.op()) {
365 case ADD:
Saurav Das4f980082015-11-05 13:39:15 -0800366 if (nextGroup != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800367 log.warn("Cannot add next {} that already exists in device {}",
368 nextObjective.id(), deviceId);
369 return;
370 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700371 log.debug("Processing NextObjective id {} in dev {} - add group",
Saurav Das8a0732e2015-11-20 15:27:53 -0800372 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700373 groupHandler.addGroup(nextObjective);
Saurav Das8a0732e2015-11-20 15:27:53 -0800374 break;
375 case ADD_TO_EXISTING:
376 if (nextGroup != null) {
Saurav Das1547b3f2017-05-05 17:01:08 -0700377 log.debug("Processing NextObjective id {} in dev {} - add bucket",
Saurav Das8a0732e2015-11-20 15:27:53 -0800378 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700379 groupHandler.addBucketToGroup(nextObjective, nextGroup);
Saurav Das4f980082015-11-05 13:39:15 -0800380 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800381 // it is possible that group-chain has not been fully created yet
Saurav Das423fe2b2015-12-04 10:52:59 -0800382 log.debug("Waiting to add bucket to group for next-id:{} in dev:{}",
383 nextObjective.id(), deviceId);
Yi Tseng47f82dc2017-03-05 22:48:39 -0800384
385 // by design multiple pending bucket is allowed for the group
386 groupHandler.pendingBuckets.compute(nextObjective.id(), (nextId, pendBkts) -> {
387 if (pendBkts == null) {
388 pendBkts = Sets.newHashSet();
389 }
390 pendBkts.add(nextObjective);
391 return pendBkts;
392 });
Saurav Das4f980082015-11-05 13:39:15 -0800393 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800394 break;
395 case REMOVE:
396 if (nextGroup == null) {
397 log.warn("Cannot remove next {} that does not exist in device {}",
398 nextObjective.id(), deviceId);
399 return;
400 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700401 log.debug("Processing NextObjective id {} in dev {} - remove group",
Saurav Das8a0732e2015-11-20 15:27:53 -0800402 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700403 groupHandler.removeGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800404 break;
405 case REMOVE_FROM_EXISTING:
406 if (nextGroup == null) {
407 log.warn("Cannot remove from next {} that does not exist in device {}",
408 nextObjective.id(), deviceId);
409 return;
410 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700411 log.debug("Processing NextObjective id {} in dev {} - remove bucket",
Saurav Das8a0732e2015-11-20 15:27:53 -0800412 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700413 groupHandler.removeBucketFromGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800414 break;
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +0900415 case MODIFY:
416 if (nextGroup == null) {
417 log.warn("Cannot modify next {} that does not exist in device {}",
418 nextObjective.id(), deviceId);
419 return;
420 }
421 log.debug("Processing NextObjective id {} in dev {} - modify bucket",
422 nextObjective.id(), deviceId);
423 groupHandler.modifyBucketFromGroup(nextObjective, nextGroup);
424 break;
Saurav Dasceccf242017-08-03 18:30:35 -0700425 case VERIFY:
426 if (nextGroup == null) {
427 log.warn("Cannot verify next {} that does not exist in device {}",
428 nextObjective.id(), deviceId);
429 return;
430 }
431 log.debug("Processing NextObjective id {} in dev {} - verify",
432 nextObjective.id(), deviceId);
433 groupHandler.verifyGroup(nextObjective, nextGroup);
434 break;
Saurav Das8a0732e2015-11-20 15:27:53 -0800435 default:
Saurav Das4f980082015-11-05 13:39:15 -0800436 log.warn("Unsupported operation {}", nextObjective.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700437 }
438 }
439
440 //////////////////////////////////////
441 // Flow handling
442 //////////////////////////////////////
443
444 /**
Yi Tseng0fdc07e2017-04-24 19:17:08 -0700445 * As per OFDPA 2.0 TTP, filtering of VLAN ids and MAC addresses (for routing)
446 * configured on switch ports happen in different tables.
Saurav Das822c4e22015-10-23 10:51:11 -0700447 *
448 * @param filt the filtering objective
449 * @param install indicates whether to add or remove the objective
450 * @param applicationId the application that sent this objective
451 */
Saurav Das52025962016-01-28 22:30:01 -0800452 protected void processFilter(FilteringObjective filt,
453 boolean install, ApplicationId applicationId) {
Saurav Das822c4e22015-10-23 10:51:11 -0700454 // This driver only processes filtering criteria defined with switch
455 // ports as the key
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530456 PortCriterion portCriterion = null;
Saurav Das822c4e22015-10-23 10:51:11 -0700457 EthCriterion ethCriterion = null;
458 VlanIdCriterion vidCriterion = null;
Saurav Das822c4e22015-10-23 10:51:11 -0700459 if (!filt.key().equals(Criteria.dummy()) &&
460 filt.key().type() == Criterion.Type.IN_PORT) {
461 portCriterion = (PortCriterion) filt.key();
Saurav Das822c4e22015-10-23 10:51:11 -0700462 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530463 if (portCriterion == null) {
464 log.debug("No IN_PORT defined in filtering objective from app: {}",
465 applicationId);
466 } else {
467 log.debug("Received filtering objective for dev/port: {}/{}", deviceId,
468 portCriterion.port());
469 }
Saurav Das822c4e22015-10-23 10:51:11 -0700470 // convert filtering conditions for switch-intfs into flowrules
471 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
472 for (Criterion criterion : filt.conditions()) {
Yi Tseng0fdc07e2017-04-24 19:17:08 -0700473 switch (criterion.type()) {
474 case ETH_DST:
475 case ETH_DST_MASKED:
476 ethCriterion = (EthCriterion) criterion;
477 break;
478 case VLAN_VID:
479 vidCriterion = (VlanIdCriterion) criterion;
480 break;
481 default:
482 log.warn("Unsupported filter {}", criterion);
483 fail(filt, ObjectiveError.UNSUPPORTED);
484 return;
Saurav Das822c4e22015-10-23 10:51:11 -0700485 }
486 }
487
Saurav Das0e99e2b2015-10-28 12:39:42 -0700488 VlanId assignedVlan = null;
Charles Chane849c192016-01-11 18:28:54 -0800489 if (vidCriterion != null) {
Charles Chand55e84d2016-03-30 17:54:24 -0700490 // Use the VLAN in criterion if metadata is not present and the traffic is tagged
Charles Chanc550f2e2017-12-19 19:55:57 -0800491 if (!vidCriterion.vlanId().equals(VlanId.NONE)) {
Charles Chane849c192016-01-11 18:28:54 -0800492 assignedVlan = vidCriterion.vlanId();
Piere5bff482018-03-07 11:42:50 +0100493 }
494 // If the meta VLAN is present let's update the assigned vlan
495 if (filt.meta() != null) {
496 VlanId metaVlan = readVlanFromTreatment(filt.meta());
497 if (metaVlan != null) {
498 assignedVlan = metaVlan;
499 }
Charles Chand55e84d2016-03-30 17:54:24 -0700500 }
Charles Chane849c192016-01-11 18:28:54 -0800501
Charles Chand55e84d2016-03-30 17:54:24 -0700502 if (assignedVlan == null) {
503 log.error("Driver fails to extract VLAN information. "
Frank Wangd8ab0962017-08-11 11:09:30 +0800504 + "Not processing VLAN filters on device {}.", deviceId);
Charles Chand55e84d2016-03-30 17:54:24 -0700505 log.debug("VLAN ID in criterion={}, metadata={}",
506 readVlanFromTreatment(filt.meta()), vidCriterion.vlanId());
507 fail(filt, ObjectiveError.BADPARAMS);
508 return;
Saurav Das0e99e2b2015-10-28 12:39:42 -0700509 }
510 }
511
Yi Tseng3a77b4f2017-02-06 15:02:17 -0800512 if (ethCriterion == null || ethCriterion.mac().equals(NONE)) {
Charles Chan985b12e2016-05-11 19:47:22 -0700513 // NOTE: it is possible that a filtering objective only has vidCriterion
Saurav Das961beb22017-03-29 19:09:17 -0700514 log.warn("filtering objective missing dstMac, cannot program TMAC table");
Saurav Das822c4e22015-10-23 10:51:11 -0700515 } else {
Charles Chan45b69ab2018-03-02 15:41:41 -0800516 MacAddress unicastMac = readEthDstFromTreatment(filt.meta());
Charles Chan66291502018-03-02 16:43:28 -0800517 List<List<FlowRule>> allStages = processEthDstFilter(portCriterion, ethCriterion,
Charles Chan45b69ab2018-03-02 15:41:41 -0800518 vidCriterion, assignedVlan, unicastMac, applicationId);
Charles Chan66291502018-03-02 16:43:28 -0800519 for (List<FlowRule> flowRules : allStages) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800520 log.trace("Starting a new flow rule stage for TMAC table flow");
Charles Chan66291502018-03-02 16:43:28 -0800521 ops.newStage();
Charles Chanc550f2e2017-12-19 19:55:57 -0800522
Charles Chan66291502018-03-02 16:43:28 -0800523 for (FlowRule flowRule : flowRules) {
524 log.trace("{} flow rules in TMAC table: {} for dev: {}",
525 (install) ? "adding" : "removing", flowRules, deviceId);
Charles Chan66291502018-03-02 16:43:28 -0800526 if (install) {
527 ops = ops.add(flowRule);
Charles Chanc550f2e2017-12-19 19:55:57 -0800528 } else {
Charles Chan66291502018-03-02 16:43:28 -0800529 // NOTE: Only remove TMAC flow when there is no more enabled port within the
530 // same VLAN on this device if TMAC doesn't support matching on in_port.
531 if (matchInPortTmacTable() || (filt.meta() != null && filt.meta().clearedDeferred())) {
532 ops = ops.remove(flowRule);
533 } else {
534 log.debug("Abort TMAC flow removal on {}. Some other ports still share this TMAC flow");
535 }
Charles Chanc550f2e2017-12-19 19:55:57 -0800536 }
537 }
Saurav Das822c4e22015-10-23 10:51:11 -0700538 }
539 }
540
Charles Chan985b12e2016-05-11 19:47:22 -0700541 if (vidCriterion == null) {
542 // NOTE: it is possible that a filtering objective only has ethCriterion
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530543 log.info("filtering objective missing VLAN, cannot program VLAN Table");
Saurav Das822c4e22015-10-23 10:51:11 -0700544 } else {
Charles Chan66291502018-03-02 16:43:28 -0800545 List<List<FlowRule>> allStages = processVlanIdFilter(
Charles Chan14967c22015-12-07 11:11:50 -0800546 portCriterion, vidCriterion, assignedVlan, applicationId);
Charles Chan66291502018-03-02 16:43:28 -0800547 for (List<FlowRule> flowRules : allStages) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800548 log.trace("Starting a new flow rule stage for VLAN table flow");
Charles Chan66291502018-03-02 16:43:28 -0800549 ops.newStage();
Charles Chan14967c22015-12-07 11:11:50 -0800550
Charles Chan66291502018-03-02 16:43:28 -0800551 for (FlowRule flowRule : flowRules) {
552 log.trace("{} flow rules in VLAN table: {} for dev: {}",
553 (install) ? "adding" : "removing", flowRule, deviceId);
554 ops = install ? ops.add(flowRule) : ops.remove(flowRule);
Charles Chand1172632017-03-15 17:33:09 -0700555 }
Saurav Das822c4e22015-10-23 10:51:11 -0700556 }
557 }
558
Saurav Das822c4e22015-10-23 10:51:11 -0700559 // apply filtering flow rules
560 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
561 @Override
562 public void onSuccess(FlowRuleOperations ops) {
Saurav Das018605f2017-02-18 14:05:44 -0800563 log.debug("Applied {} filtering rules in device {}",
Saurav Das822c4e22015-10-23 10:51:11 -0700564 ops.stages().get(0).size(), deviceId);
565 pass(filt);
566 }
567
568 @Override
569 public void onError(FlowRuleOperations ops) {
570 log.info("Failed to apply all filtering rules in dev {}", deviceId);
571 fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED);
572 }
573 }));
574
575 }
576
577 /**
Charles Chand1172632017-03-15 17:33:09 -0700578 * Internal implementation of processVlanIdFilter.
579 * <p>
580 * The is_present bit in set_vlan_vid action is required to be 0 in OFDPA i12.
581 * Since it is non-OF spec, we need an extension treatment for that.
582 * The useVlanExtension must be set to false for OFDPA i12.
583 * </p>
Charles Chan66291502018-03-02 16:43:28 -0800584 * <p>
585 * NOTE: Separate VLAN filtering rules and assignment rules
586 * into different stages in order to guarantee that filtering rules
587 * always go first, as required by OFDPA.
588 * </p>
Saurav Das0e99e2b2015-10-28 12:39:42 -0700589 *
Charles Chanf9e98652016-09-07 16:54:23 -0700590 * @param portCriterion port on device for which this filter is programmed
591 * @param vidCriterion vlan assigned to port, or NONE for untagged
592 * @param assignedVlan assigned vlan-id for untagged packets
593 * @param applicationId for application programming this filter
Charles Chan66291502018-03-02 16:43:28 -0800594 * @return stages of flow rules for port-vlan filters
Saurav Das822c4e22015-10-23 10:51:11 -0700595 */
Charles Chan66291502018-03-02 16:43:28 -0800596 protected List<List<FlowRule>> processVlanIdFilter(PortCriterion portCriterion,
Yi Tsengef19de12017-04-24 11:33:05 -0700597 VlanIdCriterion vidCriterion,
598 VlanId assignedVlan,
599 ApplicationId applicationId) {
Charles Chan66291502018-03-02 16:43:28 -0800600 List<FlowRule> filteringRules = new ArrayList<>();
601 List<FlowRule> assignmentRules = new ArrayList<>();
Saurav Das822c4e22015-10-23 10:51:11 -0700602 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
603 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800604 TrafficSelector.Builder preSelector = null;
605 TrafficTreatment.Builder preTreatment = null;
606
Saurav Das4f980082015-11-05 13:39:15 -0800607 treatment.transition(TMAC_TABLE);
608
Saurav Das822c4e22015-10-23 10:51:11 -0700609 if (vidCriterion.vlanId() == VlanId.NONE) {
610 // untagged packets are assigned vlans
Charles Chand1172632017-03-15 17:33:09 -0700611 preSelector = DefaultTrafficSelector.builder();
612 if (requireVlanExtensions()) {
613 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(VlanId.NONE);
614 selector.extension(ofdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700615 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
616 treatment.extension(ofdpaSetVlanVid, deviceId);
Charles Chand1172632017-03-15 17:33:09 -0700617
618 OfdpaMatchVlanVid preOfdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
619 preSelector.extension(preOfdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700620 } else {
Charles Chand1172632017-03-15 17:33:09 -0700621 selector.matchVlanId(VlanId.NONE);
Charles Chanf9e98652016-09-07 16:54:23 -0700622 treatment.setVlanId(assignedVlan);
Charles Chand1172632017-03-15 17:33:09 -0700623
624 preSelector.matchVlanId(assignedVlan);
625 }
626 preTreatment = DefaultTrafficTreatment.builder().transition(TMAC_TABLE);
627 } else {
628 if (requireVlanExtensions()) {
629 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
630 selector.extension(ofdpaMatchVlanVid, deviceId);
631 } else {
632 selector.matchVlanId(vidCriterion.vlanId());
Charles Chanf9e98652016-09-07 16:54:23 -0700633 }
Charles Chan14967c22015-12-07 11:11:50 -0800634
Charles Chand55e84d2016-03-30 17:54:24 -0700635 if (!assignedVlan.equals(vidCriterion.vlanId())) {
Charles Chand1172632017-03-15 17:33:09 -0700636 if (requireVlanExtensions()) {
Charles Chanc03782d2017-01-31 13:57:55 -0800637 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
638 treatment.extension(ofdpaSetVlanVid, deviceId);
639 } else {
640 treatment.setVlanId(assignedVlan);
641 }
Charles Chand55e84d2016-03-30 17:54:24 -0700642 }
Saurav Das822c4e22015-10-23 10:51:11 -0700643 }
Saurav Das822c4e22015-10-23 10:51:11 -0700644
645 // ofdpa cannot match on ALL portnumber, so we need to use separate
646 // rules for each port.
Charles Chan14967c22015-12-07 11:11:50 -0800647 List<PortNumber> portnums = new ArrayList<>();
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530648 if (portCriterion != null) {
649 if (PortNumber.ALL.equals(portCriterion.port())) {
650 for (Port port : deviceService.getPorts(deviceId)) {
651 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
652 portnums.add(port.number());
653 }
Saurav Das822c4e22015-10-23 10:51:11 -0700654 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530655 } else {
656 portnums.add(portCriterion.port());
Saurav Das822c4e22015-10-23 10:51:11 -0700657 }
658 } else {
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530659 log.warn("Filtering Objective missing Port Criterion . " +
Charles Chan66291502018-03-02 16:43:28 -0800660 "VLAN Table cannot be programmed for {}", deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700661 }
Saurav Das4f980082015-11-05 13:39:15 -0800662
Saurav Das822c4e22015-10-23 10:51:11 -0700663 for (PortNumber pnum : portnums) {
Saurav Das4f980082015-11-05 13:39:15 -0800664 // create rest of flowrule
Saurav Das822c4e22015-10-23 10:51:11 -0700665 selector.matchInPort(pnum);
666 FlowRule rule = DefaultFlowRule.builder()
667 .forDevice(deviceId)
668 .withSelector(selector.build())
669 .withTreatment(treatment.build())
670 .withPriority(DEFAULT_PRIORITY)
671 .fromApp(applicationId)
672 .makePermanent()
673 .forTable(VLAN_TABLE).build();
Charles Chan66291502018-03-02 16:43:28 -0800674 assignmentRules.add(rule);
Charles Chan14967c22015-12-07 11:11:50 -0800675
676 if (preSelector != null) {
677 preSelector.matchInPort(pnum);
678 FlowRule preRule = DefaultFlowRule.builder()
679 .forDevice(deviceId)
680 .withSelector(preSelector.build())
681 .withTreatment(preTreatment.build())
682 .withPriority(DEFAULT_PRIORITY)
683 .fromApp(applicationId)
684 .makePermanent()
685 .forTable(VLAN_TABLE).build();
Charles Chan66291502018-03-02 16:43:28 -0800686 filteringRules.add(preRule);
Charles Chan14967c22015-12-07 11:11:50 -0800687 }
Saurav Das822c4e22015-10-23 10:51:11 -0700688 }
Charles Chan2686dd72018-03-06 22:10:15 -0800689 return ImmutableList.of(filteringRules, assignmentRules);
Saurav Das822c4e22015-10-23 10:51:11 -0700690 }
691
692 /**
693 * Allows routed packets with correct destination MAC to be directed
Charles Chan45b69ab2018-03-02 15:41:41 -0800694 * to unicast routing table, multicast routing table or MPLS forwarding table.
Saurav Das822c4e22015-10-23 10:51:11 -0700695 *
696 * @param portCriterion port on device for which this filter is programmed
697 * @param ethCriterion dstMac of device for which is filter is programmed
698 * @param vidCriterion vlan assigned to port, or NONE for untagged
Saurav Das0e99e2b2015-10-28 12:39:42 -0700699 * @param assignedVlan assigned vlan-id for untagged packets
Charles Chan45b69ab2018-03-02 15:41:41 -0800700 * @param unicastMac some switches require a unicast TMAC flow to be programmed before multicast
701 * TMAC flow. This MAC address will be used for the unicast TMAC flow.
702 * This is unused if the filtering objective is a unicast.
Saurav Das822c4e22015-10-23 10:51:11 -0700703 * @param applicationId for application programming this filter
Charles Chan66291502018-03-02 16:43:28 -0800704 * @return stages of flow rules for port-vlan filters
Saurav Das822c4e22015-10-23 10:51:11 -0700705
706 */
Charles Chan66291502018-03-02 16:43:28 -0800707 protected List<List<FlowRule>> processEthDstFilter(PortCriterion portCriterion,
Saurav Das822c4e22015-10-23 10:51:11 -0700708 EthCriterion ethCriterion,
709 VlanIdCriterion vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700710 VlanId assignedVlan,
Charles Chan45b69ab2018-03-02 15:41:41 -0800711 MacAddress unicastMac,
Saurav Das822c4e22015-10-23 10:51:11 -0700712 ApplicationId applicationId) {
Charles Chan5270ed02016-01-30 23:22:37 -0800713 // Consider PortNumber.ANY as wildcard. Match ETH_DST only
Sivachidambaram Subramanianc598d062017-05-16 22:42:06 +0530714 if (portCriterion != null && PortNumber.ANY.equals(portCriterion.port())) {
Charles Chan5270ed02016-01-30 23:22:37 -0800715 return processEthDstOnlyFilter(ethCriterion, applicationId);
716 }
717
Charles Chan5b9df8d2016-03-28 22:21:40 -0700718 // Multicast MAC
719 if (ethCriterion.mask() != null) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800720 return processMcastEthDstFilter(ethCriterion, assignedVlan, unicastMac, applicationId);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700721 }
722
Saurav Das822c4e22015-10-23 10:51:11 -0700723 //handling untagged packets via assigned VLAN
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530724 if (vidCriterion != null && vidCriterion.vlanId() == VlanId.NONE) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700725 vidCriterion = (VlanIdCriterion) Criteria.matchVlanId(assignedVlan);
Saurav Das822c4e22015-10-23 10:51:11 -0700726 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530727 List<FlowRule> rules = new ArrayList<>();
728 OfdpaMatchVlanVid ofdpaMatchVlanVid = null;
729 if (vidCriterion != null && requireVlanExtensions()) {
730 ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
731 }
Saurav Das822c4e22015-10-23 10:51:11 -0700732 // ofdpa cannot match on ALL portnumber, so we need to use separate
733 // rules for each port.
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -0700734 List<PortNumber> portnums = new ArrayList<>();
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530735 if (portCriterion != null) {
736 if (PortNumber.ALL.equals(portCriterion.port())) {
737 for (Port port : deviceService.getPorts(deviceId)) {
738 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
739 portnums.add(port.number());
740 }
Saurav Das822c4e22015-10-23 10:51:11 -0700741 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530742 } else {
743 portnums.add(portCriterion.port());
744 }
745 for (PortNumber pnum : portnums) {
746 rules.add(buildTmacRuleForIpv4(ethCriterion,
747 vidCriterion,
748 ofdpaMatchVlanVid,
749 applicationId,
750 pnum));
751 rules.add(buildTmacRuleForMpls(ethCriterion,
752 vidCriterion,
753 ofdpaMatchVlanVid,
754 applicationId,
755 pnum));
756 rules.add(buildTmacRuleForIpv6(ethCriterion,
757 vidCriterion,
758 ofdpaMatchVlanVid,
759 applicationId,
760 pnum));
Saurav Das822c4e22015-10-23 10:51:11 -0700761 }
762 } else {
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530763 rules.add(buildTmacRuleForIpv4(ethCriterion,
764 vidCriterion,
765 ofdpaMatchVlanVid,
766 applicationId,
767 null));
768 rules.add(buildTmacRuleForMpls(ethCriterion,
769 vidCriterion,
770 ofdpaMatchVlanVid,
771 applicationId,
772 null));
773 rules.add(buildTmacRuleForIpv6(ethCriterion,
774 vidCriterion,
775 ofdpaMatchVlanVid,
776 applicationId,
777 null));
Saurav Das822c4e22015-10-23 10:51:11 -0700778 }
Charles Chan66291502018-03-02 16:43:28 -0800779 return ImmutableList.of(rules);
Saurav Das822c4e22015-10-23 10:51:11 -0700780 }
781
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530782 /**
783 * Builds TMAC rules for IPv4 packets.
784 *
Charles Chanc550f2e2017-12-19 19:55:57 -0800785 * @param ethCriterion dst mac matching
786 * @param vidCriterion vlan id assigned to the port
787 * @param ofdpaMatchVlanVid OFDPA vlan id matching
788 * @param applicationId application id
789 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530790 * @return TMAC rule for IPV4 packets
791 */
792 private FlowRule buildTmacRuleForIpv4(EthCriterion ethCriterion,
793 VlanIdCriterion vidCriterion,
794 OfdpaMatchVlanVid ofdpaMatchVlanVid,
795 ApplicationId applicationId,
796 PortNumber pnum) {
797 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
798 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
799 if (pnum != null) {
800 if (matchInPortTmacTable()) {
801 selector.matchInPort(pnum);
802 } else {
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800803 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530804 "ignoring the IN_PORT criteria");
805 }
806 }
807 if (vidCriterion != null) {
808 if (requireVlanExtensions()) {
809 selector.extension(ofdpaMatchVlanVid, deviceId);
810 } else {
811 selector.matchVlanId(vidCriterion.vlanId());
812 }
813 }
814 selector.matchEthType(Ethernet.TYPE_IPV4);
815 selector.matchEthDst(ethCriterion.mac());
816 treatment.transition(UNICAST_ROUTING_TABLE);
817 return DefaultFlowRule.builder()
818 .forDevice(deviceId)
819 .withSelector(selector.build())
820 .withTreatment(treatment.build())
821 .withPriority(DEFAULT_PRIORITY)
822 .fromApp(applicationId)
823 .makePermanent()
824 .forTable(TMAC_TABLE).build();
825 }
826
827 /**
828 * Builds TMAC rule for MPLS packets.
829 *
Charles Chanc550f2e2017-12-19 19:55:57 -0800830 * @param ethCriterion dst mac matching
831 * @param vidCriterion vlan id assigned to the port
832 * @param ofdpaMatchVlanVid OFDPA vlan id matching
833 * @param applicationId application id
834 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530835 * @return TMAC rule for MPLS packets
836 */
837 private FlowRule buildTmacRuleForMpls(EthCriterion ethCriterion,
838 VlanIdCriterion vidCriterion,
839 OfdpaMatchVlanVid ofdpaMatchVlanVid,
840 ApplicationId applicationId,
841 PortNumber pnum) {
842 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
843 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
844 if (pnum != null) {
845 if (matchInPortTmacTable()) {
846 selector.matchInPort(pnum);
847 } else {
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800848 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530849 "ignoring the IN_PORT criteria");
850 }
851 }
852 if (vidCriterion != null) {
853 if (requireVlanExtensions()) {
854 selector.extension(ofdpaMatchVlanVid, deviceId);
855 } else {
856 selector.matchVlanId(vidCriterion.vlanId());
857 }
858 }
859 selector.matchEthType(Ethernet.MPLS_UNICAST);
860 selector.matchEthDst(ethCriterion.mac());
861 treatment.transition(MPLS_TABLE_0);
862 return DefaultFlowRule.builder()
863 .forDevice(deviceId)
864 .withSelector(selector.build())
865 .withTreatment(treatment.build())
866 .withPriority(DEFAULT_PRIORITY)
867 .fromApp(applicationId)
868 .makePermanent()
869 .forTable(TMAC_TABLE).build();
870 }
871
872 /**
873 * Builds TMAC rules for IPv6 packets.
874 *
Charles Chanc550f2e2017-12-19 19:55:57 -0800875 * @param ethCriterion dst mac matching
876 * @param vidCriterion vlan id assigned to the port
877 * @param ofdpaMatchVlanVid OFDPA vlan id matching
878 * @param applicationId application id
879 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530880 * @return TMAC rule for IPV6 packets
881 */
882 private FlowRule buildTmacRuleForIpv6(EthCriterion ethCriterion,
883 VlanIdCriterion vidCriterion,
884 OfdpaMatchVlanVid ofdpaMatchVlanVid,
885 ApplicationId applicationId,
886 PortNumber pnum) {
887 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
888 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
889 if (pnum != null) {
890 if (matchInPortTmacTable()) {
891 selector.matchInPort(pnum);
892 } else {
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800893 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530894 "ignoring the IN_PORT criteria");
895 }
896 }
897 if (vidCriterion != null) {
898 if (requireVlanExtensions()) {
899 selector.extension(ofdpaMatchVlanVid, deviceId);
900 } else {
901 selector.matchVlanId(vidCriterion.vlanId());
902 }
903 }
904 selector.matchEthType(Ethernet.TYPE_IPV6);
905 selector.matchEthDst(ethCriterion.mac());
906 treatment.transition(UNICAST_ROUTING_TABLE);
907 return DefaultFlowRule.builder()
908 .forDevice(deviceId)
909 .withSelector(selector.build())
910 .withTreatment(treatment.build())
911 .withPriority(DEFAULT_PRIORITY)
912 .fromApp(applicationId)
913 .makePermanent()
914 .forTable(TMAC_TABLE).build();
915 }
916
Charles Chan66291502018-03-02 16:43:28 -0800917 protected List<List<FlowRule>> processEthDstOnlyFilter(EthCriterion ethCriterion,
Yi Tsengef19de12017-04-24 11:33:05 -0700918 ApplicationId applicationId) {
Pier Luigi0e358632017-01-31 09:35:05 -0800919 ImmutableList.Builder<FlowRule> builder = ImmutableList.builder();
920
Charles Chan5270ed02016-01-30 23:22:37 -0800921 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
922 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
923 selector.matchEthType(Ethernet.TYPE_IPV4);
924 selector.matchEthDst(ethCriterion.mac());
925 treatment.transition(UNICAST_ROUTING_TABLE);
926 FlowRule rule = DefaultFlowRule.builder()
927 .forDevice(deviceId)
928 .withSelector(selector.build())
929 .withTreatment(treatment.build())
930 .withPriority(DEFAULT_PRIORITY)
931 .fromApp(applicationId)
932 .makePermanent()
933 .forTable(TMAC_TABLE).build();
Pier Luigi0e358632017-01-31 09:35:05 -0800934 builder.add(rule);
935
936 selector = DefaultTrafficSelector.builder();
937 treatment = DefaultTrafficTreatment.builder();
938 selector.matchEthType(Ethernet.TYPE_IPV6);
939 selector.matchEthDst(ethCriterion.mac());
940 treatment.transition(UNICAST_ROUTING_TABLE);
941 rule = DefaultFlowRule.builder()
942 .forDevice(deviceId)
943 .withSelector(selector.build())
944 .withTreatment(treatment.build())
945 .withPriority(DEFAULT_PRIORITY)
946 .fromApp(applicationId)
947 .makePermanent()
948 .forTable(TMAC_TABLE).build();
Charles Chan66291502018-03-02 16:43:28 -0800949 return ImmutableList.of(builder.add(rule).build());
Charles Chan5270ed02016-01-30 23:22:37 -0800950 }
951
Charles Chan66291502018-03-02 16:43:28 -0800952 List<List<FlowRule>> processMcastEthDstFilter(EthCriterion ethCriterion,
Charles Chanb4879a52017-10-20 19:09:16 -0700953 VlanId assignedVlan,
Charles Chan45b69ab2018-03-02 15:41:41 -0800954 MacAddress unicastMac,
Yi Tsengef19de12017-04-24 11:33:05 -0700955 ApplicationId applicationId) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800956 ImmutableList.Builder<FlowRule> unicastFlows = ImmutableList.builder();
957 ImmutableList.Builder<FlowRule> multicastFlows = ImmutableList.builder();
958 TrafficSelector.Builder selector;
959 TrafficTreatment.Builder treatment;
Charles Chanb4879a52017-10-20 19:09:16 -0700960 FlowRule rule;
Julia Ferguson65428c32017-08-10 18:15:24 +0000961
Charles Chanb4879a52017-10-20 19:09:16 -0700962 if (IPV4_MULTICAST.equals(ethCriterion.mac())) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800963 if (requireUnicastBeforeMulticast()) {
964 selector = DefaultTrafficSelector.builder();
965 treatment = DefaultTrafficTreatment.builder();
966 selector.matchEthType(Ethernet.TYPE_IPV4);
967 selector.matchEthDst(unicastMac);
968 selector.matchVlanId(assignedVlan);
969 treatment.transition(UNICAST_ROUTING_TABLE);
970 rule = DefaultFlowRule.builder()
971 .forDevice(deviceId)
972 .withSelector(selector.build())
973 .withTreatment(treatment.build())
974 .withPriority(DEFAULT_PRIORITY)
975 .fromApp(applicationId)
976 .makePermanent()
977 .forTable(TMAC_TABLE).build();
978 unicastFlows.add(rule);
979 }
980
981 selector = DefaultTrafficSelector.builder();
982 treatment = DefaultTrafficTreatment.builder();
Charles Chanb4879a52017-10-20 19:09:16 -0700983 selector.matchEthType(Ethernet.TYPE_IPV4);
984 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
985 selector.matchVlanId(assignedVlan);
986 treatment.transition(MULTICAST_ROUTING_TABLE);
987 rule = DefaultFlowRule.builder()
988 .forDevice(deviceId)
989 .withSelector(selector.build())
990 .withTreatment(treatment.build())
991 .withPriority(DEFAULT_PRIORITY)
992 .fromApp(applicationId)
993 .makePermanent()
994 .forTable(TMAC_TABLE).build();
Charles Chan45b69ab2018-03-02 15:41:41 -0800995 multicastFlows.add(rule);
Charles Chanb4879a52017-10-20 19:09:16 -0700996 }
997
998 if (IPV6_MULTICAST.equals(ethCriterion.mac())) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800999 if (requireUnicastBeforeMulticast()) {
1000 selector = DefaultTrafficSelector.builder();
1001 treatment = DefaultTrafficTreatment.builder();
1002 selector.matchEthType(Ethernet.TYPE_IPV6);
1003 selector.matchEthDst(unicastMac);
1004 selector.matchVlanId(assignedVlan);
1005 treatment.transition(UNICAST_ROUTING_TABLE);
1006 rule = DefaultFlowRule.builder()
1007 .forDevice(deviceId)
1008 .withSelector(selector.build())
1009 .withTreatment(treatment.build())
1010 .withPriority(DEFAULT_PRIORITY)
1011 .fromApp(applicationId)
1012 .makePermanent()
1013 .forTable(TMAC_TABLE).build();
1014 unicastFlows.add(rule);
1015 }
1016
Charles Chanb4879a52017-10-20 19:09:16 -07001017 selector = DefaultTrafficSelector.builder();
1018 treatment = DefaultTrafficTreatment.builder();
1019 selector.matchEthType(Ethernet.TYPE_IPV6);
1020 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
1021 selector.matchVlanId(assignedVlan);
1022 treatment.transition(MULTICAST_ROUTING_TABLE);
1023 rule = DefaultFlowRule.builder()
1024 .forDevice(deviceId)
1025 .withSelector(selector.build())
1026 .withTreatment(treatment.build())
1027 .withPriority(DEFAULT_PRIORITY)
1028 .fromApp(applicationId)
1029 .makePermanent()
1030 .forTable(TMAC_TABLE).build();
Charles Chan45b69ab2018-03-02 15:41:41 -08001031 multicastFlows.add(rule);
Charles Chanb4879a52017-10-20 19:09:16 -07001032 }
Charles Chan45b69ab2018-03-02 15:41:41 -08001033 return ImmutableList.of(unicastFlows.build(), multicastFlows.build());
Charles Chan5b9df8d2016-03-28 22:21:40 -07001034 }
1035
Saurav Das822c4e22015-10-23 10:51:11 -07001036 private Collection<FlowRule> processForward(ForwardingObjective fwd) {
1037 switch (fwd.flag()) {
1038 case SPECIFIC:
1039 return processSpecific(fwd);
1040 case VERSATILE:
1041 return processVersatile(fwd);
Jonghwan Hyun800d9d02018-04-09 09:40:50 -07001042 case EGRESS:
1043 return processEgress(fwd);
Saurav Das822c4e22015-10-23 10:51:11 -07001044 default:
1045 fail(fwd, ObjectiveError.UNKNOWN);
1046 log.warn("Unknown forwarding flag {}", fwd.flag());
1047 }
1048 return Collections.emptySet();
1049 }
1050
1051 /**
Jonghwan Hyun800d9d02018-04-09 09:40:50 -07001052 * In the OF-DPA 2.0 pipeline, egress forwarding objectives go to the
1053 * egress tables.
1054 * @param fwd the forwarding objective of type 'egress'
1055 * @return a collection of flow rules to be sent to the switch. An empty
1056 * collection may be returned if there is a problem in processing
1057 * the flow rule
1058 */
1059 protected Collection<FlowRule> processEgress(ForwardingObjective fwd) {
1060 log.debug("Processing egress forwarding objective:{} in dev:{}",
1061 fwd, deviceId);
1062
1063 List<FlowRule> rules = new ArrayList<>();
1064
1065 // Build selector
1066 TrafficSelector.Builder sb = DefaultTrafficSelector.builder();
1067 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) fwd.selector().getCriterion(Criterion.Type.VLAN_VID);
1068 if (vlanIdCriterion == null) {
1069 log.error("Egress forwarding objective:{} must include vlanId", fwd.id());
1070 fail(fwd, ObjectiveError.BADPARAMS);
1071 return rules;
1072 }
1073
1074 Optional<Instruction> outInstr = fwd.treatment().allInstructions().stream()
1075 .filter(instruction -> instruction instanceof OutputInstruction).findFirst();
1076 if (!outInstr.isPresent()) {
1077 log.error("Egress forwarding objective:{} must include output port", fwd.id());
1078 fail(fwd, ObjectiveError.BADPARAMS);
1079 return rules;
1080 }
1081
1082 PortNumber portNumber = ((OutputInstruction) outInstr.get()).port();
1083
1084 sb.matchVlanId(vlanIdCriterion.vlanId());
1085 OfdpaMatchActsetOutput actsetOutput = new OfdpaMatchActsetOutput(portNumber);
1086 sb.extension(actsetOutput, deviceId);
1087
1088 sb.extension(new OfdpaMatchAllowVlanTranslation(ALLOW_VLAN_TRANSLATION), deviceId);
1089
1090 // Build a flow rule for Egress VLAN Flow table
1091 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
1092 tb.transition(EGRESS_DSCP_PCP_REMARK_FLOW_TABLE);
1093 if (fwd.treatment() != null) {
1094 for (Instruction instr : fwd.treatment().allInstructions()) {
1095 if (instr instanceof L2ModificationInstruction &&
1096 ((L2ModificationInstruction) instr).subtype() == L2SubType.VLAN_ID) {
1097 tb.immediate().add(instr);
1098 }
1099 if (instr instanceof L2ModificationInstruction &&
1100 ((L2ModificationInstruction) instr).subtype() == L2SubType.VLAN_PUSH) {
1101 tb.immediate().pushVlan();
1102 EthType ethType = ((L2ModificationInstruction.ModVlanHeaderInstruction) instr).ethernetType();
1103 if (ethType.equals(EtherType.QINQ.ethType())) {
1104 // Build a flow rule for Egress TPID Flow table
1105 TrafficSelector tpidSelector = DefaultTrafficSelector.builder()
1106 .extension(actsetOutput, deviceId)
1107 .matchVlanId(VlanId.ANY).build();
1108
1109 TrafficTreatment tpidTreatment = DefaultTrafficTreatment.builder()
1110 .extension(new Ofdpa3CopyField(COPY_FIELD_NBITS, COPY_FIELD_OFFSET,
1111 COPY_FIELD_OFFSET, OXM_ID_VLAN_VID,
1112 OXM_ID_PACKET_REG_1),
1113 deviceId)
1114 .popVlan()
1115 .pushVlan(EtherType.QINQ.ethType())
1116 .extension(new Ofdpa3CopyField(COPY_FIELD_NBITS, COPY_FIELD_OFFSET,
1117 COPY_FIELD_OFFSET, OXM_ID_PACKET_REG_1,
1118 OXM_ID_VLAN_VID),
1119 deviceId)
1120 .build();
1121
1122 FlowRule.Builder tpidRuleBuilder = DefaultFlowRule.builder()
1123 .fromApp(fwd.appId())
1124 .withPriority(fwd.priority())
1125 .forDevice(deviceId)
1126 .withSelector(tpidSelector)
1127 .withTreatment(tpidTreatment)
1128 .makePermanent()
1129 .forTable(EGRESS_TPID_FLOW_TABLE);
1130 rules.add(tpidRuleBuilder.build());
1131 }
1132 }
1133 }
1134 }
1135
1136 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1137 .fromApp(fwd.appId())
1138 .withPriority(fwd.priority())
1139 .forDevice(deviceId)
1140 .withSelector(sb.build())
1141 .withTreatment(tb.build())
1142 .makePermanent()
1143 .forTable(EGRESS_VLAN_FLOW_TABLE);
1144 rules.add(ruleBuilder.build());
1145 return rules;
1146 }
1147
1148 /**
Saurav Das822c4e22015-10-23 10:51:11 -07001149 * In the OF-DPA 2.0 pipeline, versatile forwarding objectives go to the
1150 * ACL table.
1151 * @param fwd the forwarding objective of type 'versatile'
1152 * @return a collection of flow rules to be sent to the switch. An empty
1153 * collection may be returned if there is a problem in processing
1154 * the flow rule
1155 */
Saurav Das52025962016-01-28 22:30:01 -08001156 protected Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
Saurav Das018605f2017-02-18 14:05:44 -08001157 log.debug("Processing versatile forwarding objective:{} in dev:{}",
Saurav Dasd2fded02016-12-02 15:43:47 -08001158 fwd.id(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001159
1160 EthTypeCriterion ethType =
Saurav Das77b5e902016-01-27 17:01:59 -08001161 (EthTypeCriterion) fwd.selector().getCriterion(Criterion.Type.ETH_TYPE);
Saurav Das822c4e22015-10-23 10:51:11 -07001162 if (ethType == null) {
Saurav Dasd2fded02016-12-02 15:43:47 -08001163 log.error("Versatile forwarding objective:{} must include ethType",
1164 fwd.id());
Saurav Das822c4e22015-10-23 10:51:11 -07001165 fail(fwd, ObjectiveError.BADPARAMS);
1166 return Collections.emptySet();
1167 }
1168 if (fwd.nextId() == null && fwd.treatment() == null) {
1169 log.error("Forwarding objective {} from {} must contain "
1170 + "nextId or Treatment", fwd.selector(), fwd.appId());
Zsolt Haraszti9faab752016-02-17 15:55:20 -08001171 fail(fwd, ObjectiveError.BADPARAMS);
Saurav Das822c4e22015-10-23 10:51:11 -07001172 return Collections.emptySet();
1173 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001174
Saurav Das77b5e902016-01-27 17:01:59 -08001175 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
1176 fwd.selector().criteria().forEach(criterion -> {
1177 if (criterion instanceof VlanIdCriterion) {
1178 VlanId vlanId = ((VlanIdCriterion) criterion).vlanId();
1179 // ensure that match does not include vlan = NONE as OF-DPA does not
1180 // match untagged packets this way in the ACL table.
1181 if (vlanId.equals(VlanId.NONE)) {
1182 return;
1183 }
Charles Chand1172632017-03-15 17:33:09 -07001184 if (requireVlanExtensions()) {
1185 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanId);
1186 sbuilder.extension(ofdpaMatchVlanVid, deviceId);
1187 } else {
1188 sbuilder.matchVlanId(vlanId);
1189 }
Charles Chan09bf2692018-01-11 11:48:18 -08001190 } else if (criterion instanceof Icmpv6TypeCriterion) {
1191 byte icmpv6Type = (byte) ((Icmpv6TypeCriterion) criterion).icmpv6Type();
1192 sbuilder.matchIcmpv6Type(icmpv6Type);
1193 } else if (criterion instanceof Icmpv6CodeCriterion) {
1194 byte icmpv6Code = (byte) ((Icmpv6CodeCriterion) criterion).icmpv6Code();
1195 sbuilder.matchIcmpv6Type(icmpv6Code);
Charles Chand9e47c62017-10-05 15:17:15 -07001196 } else if (criterion instanceof TcpPortCriterion || criterion instanceof UdpPortCriterion) {
1197 // FIXME: QMX switches do not support L4 dst port matching in ACL table.
1198 // Currently L4 dst port matching is only used by DHCP relay feature
1199 // and therefore is safe to be replaced with L4 src port matching.
1200 // We need to revisit this if L4 dst port is used for other purpose in the future.
1201 if (!supportIpv6L4Dst() && isIpv6(fwd.selector())) {
Charles Chand9e47c62017-10-05 15:17:15 -07001202 switch (criterion.type()) {
1203 case UDP_DST:
Charles Chand9e47c62017-10-05 15:17:15 -07001204 case UDP_DST_MASKED:
Charles Chand9e47c62017-10-05 15:17:15 -07001205 case TCP_DST:
Charles Chand9e47c62017-10-05 15:17:15 -07001206 case TCP_DST_MASKED:
Charles Chand9e47c62017-10-05 15:17:15 -07001207 break;
1208 default:
1209 sbuilder.add(criterion);
1210 }
1211 } else {
1212 sbuilder.add(criterion);
1213 }
Saurav Das77b5e902016-01-27 17:01:59 -08001214 } else {
1215 sbuilder.add(criterion);
1216 }
1217 });
1218
Saurav Das822c4e22015-10-23 10:51:11 -07001219 // XXX driver does not currently do type checking as per Tables 65-67 in
1220 // OFDPA 2.0 spec. The only allowed treatment is a punt to the controller.
Saurav Das49cb5a12016-01-16 22:54:07 -08001221 TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder();
1222 if (fwd.treatment() != null) {
1223 for (Instruction ins : fwd.treatment().allInstructions()) {
1224 if (ins instanceof OutputInstruction) {
1225 OutputInstruction o = (OutputInstruction) ins;
Ray Milkeyfe0e0852018-01-18 11:14:05 -08001226 if (PortNumber.CONTROLLER.equals(o.port())) {
Saurav Das49cb5a12016-01-16 22:54:07 -08001227 ttBuilder.add(o);
1228 } else {
1229 log.warn("Only allowed treatments in versatile forwarding "
1230 + "objectives are punts to the controller");
1231 }
1232 } else {
1233 log.warn("Cannot process instruction in versatile fwd {}", ins);
1234 }
Saurav Das822c4e22015-10-23 10:51:11 -07001235 }
Charles Chan2df0e8a2017-01-09 11:45:08 -08001236 if (fwd.treatment().clearedDeferred()) {
1237 ttBuilder.wipeDeferred();
1238 }
Saurav Das822c4e22015-10-23 10:51:11 -07001239 }
Saurav Das822c4e22015-10-23 10:51:11 -07001240 if (fwd.nextId() != null) {
Saurav Das49cb5a12016-01-16 22:54:07 -08001241 // overide case
1242 NextGroup next = getGroupForNextObjective(fwd.nextId());
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +05301243 if (next == null) {
1244 fail(fwd, ObjectiveError.BADPARAMS);
1245 return Collections.emptySet();
1246 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001247 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1248 // we only need the top level group's key to point the flow to it
1249 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1250 if (group == null) {
1251 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1252 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1253 fail(fwd, ObjectiveError.GROUPMISSING);
1254 return Collections.emptySet();
1255 }
1256 ttBuilder.deferred().group(group.id());
Saurav Das822c4e22015-10-23 10:51:11 -07001257 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001258
1259 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1260 .fromApp(fwd.appId())
1261 .withPriority(fwd.priority())
1262 .forDevice(deviceId)
Saurav Das77b5e902016-01-27 17:01:59 -08001263 .withSelector(sbuilder.build())
Saurav Das49cb5a12016-01-16 22:54:07 -08001264 .withTreatment(ttBuilder.build())
1265 .makePermanent()
1266 .forTable(ACL_TABLE);
1267 return Collections.singletonList(ruleBuilder.build());
Saurav Das822c4e22015-10-23 10:51:11 -07001268 }
1269
1270 /**
1271 * In the OF-DPA 2.0 pipeline, specific forwarding refers to the IP table
Saurav Das8a0732e2015-11-20 15:27:53 -08001272 * (unicast or multicast) or the L2 table (mac + vlan) or the MPLS table.
Saurav Das822c4e22015-10-23 10:51:11 -07001273 *
1274 * @param fwd the forwarding objective of type 'specific'
1275 * @return a collection of flow rules. Typically there will be only one
1276 * for this type of forwarding objective. An empty set may be
1277 * returned if there is an issue in processing the objective.
1278 */
Charles Chan50d900c2018-03-02 13:26:22 -08001279 private Collection<FlowRule> processSpecific(ForwardingObjective fwd) {
Saurav Das25190812016-05-27 13:54:07 -07001280 log.debug("Processing specific fwd objective:{} in dev:{} with next:{}",
Saurav Das4ce45962015-11-24 23:21:05 -08001281 fwd.id(), deviceId, fwd.nextId());
1282 boolean isEthTypeObj = isSupportedEthTypeObjective(fwd);
1283 boolean isEthDstObj = isSupportedEthDstObjective(fwd);
1284
1285 if (isEthTypeObj) {
1286 return processEthTypeSpecific(fwd);
1287 } else if (isEthDstObj) {
1288 return processEthDstSpecific(fwd);
1289 } else {
1290 log.warn("processSpecific: Unsupported forwarding objective "
1291 + "criteria fwd:{} in dev:{}", fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001292 fail(fwd, ObjectiveError.UNSUPPORTED);
1293 return Collections.emptySet();
1294 }
Saurav Das4ce45962015-11-24 23:21:05 -08001295 }
1296
Saurav Das4ce45962015-11-24 23:21:05 -08001297 /**
1298 * Handles forwarding rules to the IP and MPLS tables.
1299 *
1300 * @param fwd the forwarding objective
1301 * @return A collection of flow rules, or an empty set
1302 */
1303 protected Collection<FlowRule> processEthTypeSpecific(ForwardingObjective fwd) {
Charles Chancad338a2016-09-16 18:03:11 -07001304 return processEthTypeSpecificInternal(fwd, false, ACL_TABLE);
Charles Chanf9e98652016-09-07 16:54:23 -07001305 }
1306
1307 /**
1308 * Internal implementation of processEthTypeSpecific.
1309 * <p>
1310 * Wildcarded IPv4_DST is not supported in OFDPA i12. Therefore, we break
1311 * the rule into 0.0.0.0/1 and 128.0.0.0/1.
1312 * The allowDefaultRoute must be set to false for OFDPA i12.
1313 * </p>
1314 *
1315 * @param fwd the forwarding objective
1316 * @param allowDefaultRoute allow wildcarded IPv4_DST or not
Ray Milkey0bb1e102016-11-10 14:51:27 -08001317 * @param mplsNextTable next MPLS table
Charles Chanf9e98652016-09-07 16:54:23 -07001318 * @return A collection of flow rules, or an empty set
1319 */
1320 protected Collection<FlowRule> processEthTypeSpecificInternal(ForwardingObjective fwd,
Charles Chancad338a2016-09-16 18:03:11 -07001321 boolean allowDefaultRoute,
1322 int mplsNextTable) {
Saurav Das4ce45962015-11-24 23:21:05 -08001323 TrafficSelector selector = fwd.selector();
1324 EthTypeCriterion ethType =
1325 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001326 boolean emptyGroup = false;
Charles Chan188ebf52015-12-23 00:15:11 -08001327 int forTableId;
Saurav Das8a0732e2015-11-20 15:27:53 -08001328 TrafficSelector.Builder filteredSelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -08001329 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
Flavio Castroe10fa242016-01-15 12:43:51 -08001330 TrafficSelector.Builder complementarySelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -08001331
Saurav Das8a0732e2015-11-20 15:27:53 -08001332 if (ethType.ethType().toShort() == Ethernet.TYPE_IPV4) {
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001333 if (buildIpv4Selector(filteredSelector, complementarySelector, fwd, allowDefaultRoute) < 0) {
1334 return Collections.emptyList();
1335 }
1336 // We need to set properly the next table
Flavio Castroe10fa242016-01-15 12:43:51 -08001337 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
Charles Chan5b9df8d2016-03-28 22:21:40 -07001338 if (ipv4Dst.isMulticast()) {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001339 forTableId = MULTICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -08001340 } else {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001341 forTableId = UNICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -08001342 }
Charles Chan50d900c2018-03-02 13:26:22 -08001343
Charles Chan236653d2018-03-05 11:28:23 -08001344 // TODO decrementing IP ttl is done automatically for routing, this
Charles Chan50d900c2018-03-02 13:26:22 -08001345 // action is ignored or rejected in ofdpa as it is not fully implemented
Charles Chan236653d2018-03-05 11:28:23 -08001346 // if (fwd.treatment() != null) {
1347 // for (Instruction instr : fwd.treatment().allInstructions()) {
1348 // if (instr instanceof L3ModificationInstruction &&
1349 // ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1350 // tb.deferred().add(instr);
1351 // }
1352 // }
1353 // }
1354
Pier Ventree0ae7a32016-11-23 09:57:42 -08001355 } else if (ethType.ethType().toShort() == Ethernet.TYPE_IPV6) {
1356 if (buildIpv6Selector(filteredSelector, fwd) < 0) {
1357 return Collections.emptyList();
1358 }
Kalhee Kim3d26bf02017-10-04 14:59:11 +00001359 //We need to set the proper next table
1360 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
1361 if (ipv6Dst.isMulticast()) {
1362 forTableId = MULTICAST_ROUTING_TABLE;
1363 } else {
1364 forTableId = UNICAST_ROUTING_TABLE;
1365 }
1366
Charles Chan236653d2018-03-05 11:28:23 -08001367 // TODO decrementing IP ttl is done automatically for routing, this
Charles Chan50d900c2018-03-02 13:26:22 -08001368 // action is ignored or rejected in ofdpa as it is not fully implemented
Charles Chan236653d2018-03-05 11:28:23 -08001369 // if (fwd.treatment() != null) {
1370 // for (Instruction instr : fwd.treatment().allInstructions()) {
1371 // if (instr instanceof L3ModificationInstruction &&
1372 // ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1373 // tb.deferred().add(instr);
1374 // }
1375 // }
1376 // }
Saurav Das8a0732e2015-11-20 15:27:53 -08001377 } else {
1378 filteredSelector
1379 .matchEthType(Ethernet.MPLS_UNICAST)
1380 .matchMplsLabel(((MplsCriterion)
1381 selector.getCriterion(Criterion.Type.MPLS_LABEL)).label());
1382 MplsBosCriterion bos = (MplsBosCriterion) selector
Pier Ventre140a8942016-11-02 07:26:38 -07001383 .getCriterion(MPLS_BOS);
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001384 if (bos != null && requireMplsBosMatch()) {
Saurav Das8a0732e2015-11-20 15:27:53 -08001385 filteredSelector.matchMplsBos(bos.mplsBos());
1386 }
1387 forTableId = MPLS_TABLE_1;
Saurav Das4ce45962015-11-24 23:21:05 -08001388 log.debug("processing MPLS specific forwarding objective {} -> next:{}"
1389 + " in dev {}", fwd.id(), fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001390
Charles Chan14967c22015-12-07 11:11:50 -08001391 if (fwd.treatment() != null) {
1392 for (Instruction instr : fwd.treatment().allInstructions()) {
1393 if (instr instanceof L2ModificationInstruction &&
1394 ((L2ModificationInstruction) instr).subtype() == L2SubType.MPLS_POP) {
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001395 // OF-DPA does not pop in MPLS table in some cases. For the L3 VPN, it requires
Saurav Das9c705342017-01-06 11:36:01 -08001396 // setting the MPLS_TYPE so pop can happen down the pipeline
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001397 if (requireMplsPop()) {
1398 if (mplsNextTable == MPLS_TYPE_TABLE && isNotMplsBos(selector)) {
1399 tb.immediate().popMpls();
1400 }
1401 } else {
1402 // Skip mpls pop action for mpls_unicast label
1403 if (instr instanceof ModMplsHeaderInstruction &&
1404 !((ModMplsHeaderInstruction) instr).ethernetType()
1405 .equals(EtherType.MPLS_UNICAST.ethType())) {
1406 tb.immediate().add(instr);
1407 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001408 }
Charles Chan14967c22015-12-07 11:11:50 -08001409 }
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001410
1411 if (requireMplsTtlModification()) {
1412 if (instr instanceof L3ModificationInstruction &&
1413 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1414 // FIXME Should modify the app to send the correct DEC_MPLS_TTL instruction
1415 tb.immediate().decMplsTtl();
1416 }
1417 if (instr instanceof L3ModificationInstruction &&
1418 ((L3ModificationInstruction) instr).subtype() == L3SubType.TTL_IN) {
1419 tb.immediate().add(instr);
1420 }
Charles Chan14967c22015-12-07 11:11:50 -08001421 }
Saurav Das8a0732e2015-11-20 15:27:53 -08001422 }
1423 }
1424 }
Saurav Das822c4e22015-10-23 10:51:11 -07001425
1426 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001427 NextGroup next = getGroupForNextObjective(fwd.nextId());
1428 if (next != null) {
1429 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1430 // we only need the top level group's key to point the flow to it
1431 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1432 if (group == null) {
1433 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1434 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1435 fail(fwd, ObjectiveError.GROUPMISSING);
1436 return Collections.emptySet();
1437 }
Saurav Dasa4020382018-02-14 14:14:54 -08001438 if (isNotMplsBos(selector) && group.type().equals(SELECT)) {
1439 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
1440 + "is not implemented in OF-DPA yet. Aborting flow {} -> next:{} "
1441 + "in this device {}", fwd.id(), fwd.nextId(), deviceId);
1442 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1443 return Collections.emptySet();
1444 }
Saurav Das423fe2b2015-12-04 10:52:59 -08001445 tb.deferred().group(group.id());
Saurav Dasc568c342018-01-25 09:49:01 -08001446 // retrying flows may be necessary due to bug CORD-554
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001447 if (gkeys.size() == 1 && gkeys.get(0).size() == 1) {
Saurav Dasc568c342018-01-25 09:49:01 -08001448 if (shouldRetry()) {
1449 log.warn("Found empty group 0x{} in dev:{} .. will retry fwd:{}",
1450 Integer.toHexString(group.id().id()), deviceId,
1451 fwd.id());
1452 emptyGroup = true;
1453 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001454 }
Saurav Das25190812016-05-27 13:54:07 -07001455 } else {
1456 log.warn("Cannot find group for nextId:{} in dev:{}. Aborting fwd:{}",
1457 fwd.nextId(), deviceId, fwd.id());
1458 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1459 return Collections.emptySet();
Saurav Das822c4e22015-10-23 10:51:11 -07001460 }
Saurav Das822c4e22015-10-23 10:51:11 -07001461 }
Charles Chancad338a2016-09-16 18:03:11 -07001462
1463 if (forTableId == MPLS_TABLE_1) {
Pier Ventre140a8942016-11-02 07:26:38 -07001464 if (mplsNextTable == MPLS_L3_TYPE_TABLE) {
Charles Chancad338a2016-09-16 18:03:11 -07001465 Ofdpa3SetMplsType setMplsType = new Ofdpa3SetMplsType(Ofdpa3MplsType.L3_PHP);
Saurav Das9c705342017-01-06 11:36:01 -08001466 // set mpls type as apply_action
1467 tb.immediate().extension(setMplsType, deviceId);
Charles Chancad338a2016-09-16 18:03:11 -07001468 }
1469 tb.transition(mplsNextTable);
1470 } else {
1471 tb.transition(ACL_TABLE);
1472 }
1473
Andrea Campanellad980c6d2018-04-30 11:48:55 +02001474 if (fwd.treatment() != null && fwd.treatment().clearedDeferred()) {
1475 if (supportsUnicastBlackHole()) {
1476 tb.wipeDeferred();
1477 } else {
1478 log.warn("Clear Deferred is not supported Unicast Routing Table on device {}", deviceId);
1479 return Collections.emptySet();
1480 }
1481 }
1482
Saurav Das822c4e22015-10-23 10:51:11 -07001483 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1484 .fromApp(fwd.appId())
1485 .withPriority(fwd.priority())
1486 .forDevice(deviceId)
Saurav Das8a0732e2015-11-20 15:27:53 -08001487 .withSelector(filteredSelector.build())
1488 .withTreatment(tb.build())
1489 .forTable(forTableId);
Saurav Das822c4e22015-10-23 10:51:11 -07001490
1491 if (fwd.permanent()) {
1492 ruleBuilder.makePermanent();
1493 } else {
1494 ruleBuilder.makeTemporary(fwd.timeout());
1495 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001496 Collection<FlowRule> flowRuleCollection = new ArrayList<>();
1497 flowRuleCollection.add(ruleBuilder.build());
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001498 if (!allowDefaultRoute) {
Pier Ventree0ae7a32016-11-23 09:57:42 -08001499 flowRuleCollection.add(
1500 defaultRoute(fwd, complementarySelector, forTableId, tb)
1501 );
Flavio Castroe10fa242016-01-15 12:43:51 -08001502 log.debug("Default rule 0.0.0.0/0 is being installed two rules");
1503 }
Saurav Dasc568c342018-01-25 09:49:01 -08001504
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001505 if (emptyGroup) {
1506 executorService.schedule(new RetryFlows(fwd, flowRuleCollection),
1507 RETRY_MS, TimeUnit.MILLISECONDS);
1508 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001509 return flowRuleCollection;
Saurav Das822c4e22015-10-23 10:51:11 -07001510 }
1511
Charles Chan50d900c2018-03-02 13:26:22 -08001512 private int buildIpv4Selector(TrafficSelector.Builder builderToUpdate,
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001513 TrafficSelector.Builder extBuilder,
1514 ForwardingObjective fwd,
1515 boolean allowDefaultRoute) {
1516 TrafficSelector selector = fwd.selector();
1517
1518 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
1519 if (ipv4Dst.isMulticast()) {
1520 if (ipv4Dst.prefixLength() != 32) {
1521 log.warn("Multicast specific forwarding objective can only be /32");
1522 fail(fwd, ObjectiveError.BADPARAMS);
1523 return -1;
1524 }
1525 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
1526 if (assignedVlan == null) {
1527 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
1528 fail(fwd, ObjectiveError.BADPARAMS);
1529 return -1;
1530 }
Charles Chand1172632017-03-15 17:33:09 -07001531 if (requireVlanExtensions()) {
1532 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
1533 builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
1534 } else {
1535 builderToUpdate.matchVlanId(assignedVlan);
1536 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001537 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1538 log.debug("processing IPv4 multicast specific forwarding objective {} -> next:{}"
1539 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1540 } else {
1541 if (ipv4Dst.prefixLength() == 0) {
1542 if (allowDefaultRoute) {
1543 // The entire IPV4_DST field is wildcarded intentionally
1544 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4);
1545 } else {
1546 // NOTE: The switch does not support matching 0.0.0.0/0
1547 // Split it into 0.0.0.0/1 and 128.0.0.0/1
1548 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4)
1549 .matchIPDst(IpPrefix.valueOf("0.0.0.0/1"));
1550 extBuilder.matchEthType(Ethernet.TYPE_IPV4)
1551 .matchIPDst(IpPrefix.valueOf("128.0.0.0/1"));
1552 }
1553 } else {
1554 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1555 }
1556 log.debug("processing IPv4 unicast specific forwarding objective {} -> next:{}"
1557 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1558 }
1559 return 0;
1560 }
1561
1562 /**
1563 * Helper method to build Ipv6 selector using the selector provided by
1564 * a forwarding objective.
1565 *
1566 * @param builderToUpdate the builder to update
1567 * @param fwd the selector to read
1568 * @return 0 if the update ends correctly. -1 if the matches
1569 * are not yet supported
1570 */
Charles Chan50d900c2018-03-02 13:26:22 -08001571 int buildIpv6Selector(TrafficSelector.Builder builderToUpdate,
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001572 ForwardingObjective fwd) {
1573
1574 TrafficSelector selector = fwd.selector();
1575
1576 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
1577 if (ipv6Dst.isMulticast()) {
Julia Ferguson65428c32017-08-10 18:15:24 +00001578 if (ipv6Dst.prefixLength() != IpAddress.INET6_BIT_LENGTH) {
1579 log.warn("Multicast specific forwarding objective can only be /128");
1580 fail(fwd, ObjectiveError.BADPARAMS);
1581 return -1;
1582 }
1583 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
1584 if (assignedVlan == null) {
1585 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
1586 fail(fwd, ObjectiveError.BADPARAMS);
1587 return -1;
1588 }
1589 if (requireVlanExtensions()) {
1590 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
1591 builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
1592 } else {
1593 builderToUpdate.matchVlanId(assignedVlan);
1594 }
1595 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6).matchIPv6Dst(ipv6Dst);
1596 log.debug("processing IPv6 multicast specific forwarding objective {} -> next:{}"
1597 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1598 } else {
1599 if (ipv6Dst.prefixLength() != 0) {
1600 builderToUpdate.matchIPv6Dst(ipv6Dst);
1601 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001602 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6);
1603 log.debug("processing IPv6 unicast specific forwarding objective {} -> next:{}"
1604 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Julia Ferguson65428c32017-08-10 18:15:24 +00001605 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001606 return 0;
1607 }
1608
Charles Chan50d900c2018-03-02 13:26:22 -08001609 FlowRule defaultRoute(ForwardingObjective fwd,
Pier Ventree0ae7a32016-11-23 09:57:42 -08001610 TrafficSelector.Builder complementarySelector,
1611 int forTableId,
1612 TrafficTreatment.Builder tb) {
1613 FlowRule.Builder rule = DefaultFlowRule.builder()
1614 .fromApp(fwd.appId())
1615 .withPriority(fwd.priority())
1616 .forDevice(deviceId)
1617 .withSelector(complementarySelector.build())
1618 .withTreatment(tb.build())
1619 .forTable(forTableId);
1620 if (fwd.permanent()) {
1621 rule.makePermanent();
1622 } else {
1623 rule.makeTemporary(fwd.timeout());
1624 }
1625 return rule.build();
1626 }
1627
Saurav Das4ce45962015-11-24 23:21:05 -08001628 /**
1629 * Handles forwarding rules to the L2 bridging table. Flow actions are not
1630 * allowed in the bridging table - instead we use L2 Interface group or
1631 * L2 flood group
1632 *
1633 * @param fwd the forwarding objective
1634 * @return A collection of flow rules, or an empty set
1635 */
1636 protected Collection<FlowRule> processEthDstSpecific(ForwardingObjective fwd) {
1637 List<FlowRule> rules = new ArrayList<>();
1638
1639 // Build filtered selector
1640 TrafficSelector selector = fwd.selector();
1641 EthCriterion ethCriterion = (EthCriterion) selector
1642 .getCriterion(Criterion.Type.ETH_DST);
1643 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) selector
1644 .getCriterion(Criterion.Type.VLAN_VID);
1645
1646 if (vlanIdCriterion == null) {
1647 log.warn("Forwarding objective for bridging requires vlan. Not "
1648 + "installing fwd:{} in dev:{}", fwd.id(), deviceId);
1649 fail(fwd, ObjectiveError.BADPARAMS);
1650 return Collections.emptySet();
1651 }
1652
1653 TrafficSelector.Builder filteredSelectorBuilder =
1654 DefaultTrafficSelector.builder();
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001655
1656 if (!ethCriterion.mac().equals(NONE) &&
1657 !ethCriterion.mac().equals(BROADCAST)) {
Saurav Das4ce45962015-11-24 23:21:05 -08001658 filteredSelectorBuilder.matchEthDst(ethCriterion.mac());
1659 log.debug("processing L2 forwarding objective:{} -> next:{} in dev:{}",
1660 fwd.id(), fwd.nextId(), deviceId);
1661 } else {
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001662 // Use wildcard DST_MAC if the MacAddress is None or Broadcast
Saurav Das4ce45962015-11-24 23:21:05 -08001663 log.debug("processing L2 Broadcast forwarding objective:{} -> next:{} "
1664 + "in dev:{} for vlan:{}",
1665 fwd.id(), fwd.nextId(), deviceId, vlanIdCriterion.vlanId());
1666 }
Charles Chand1172632017-03-15 17:33:09 -07001667 if (requireVlanExtensions()) {
1668 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanIdCriterion.vlanId());
1669 filteredSelectorBuilder.extension(ofdpaMatchVlanVid, deviceId);
1670 } else {
1671 filteredSelectorBuilder.matchVlanId(vlanIdCriterion.vlanId());
1672 }
Saurav Das4ce45962015-11-24 23:21:05 -08001673 TrafficSelector filteredSelector = filteredSelectorBuilder.build();
1674
1675 if (fwd.treatment() != null) {
1676 log.warn("Ignoring traffic treatment in fwd rule {} meant for L2 table"
1677 + "for dev:{}. Expecting only nextId", fwd.id(), deviceId);
1678 }
1679
1680 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1681 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001682 NextGroup next = getGroupForNextObjective(fwd.nextId());
Saurav Das4ce45962015-11-24 23:21:05 -08001683 if (next != null) {
1684 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1685 // we only need the top level group's key to point the flow to it
1686 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1687 if (group != null) {
1688 treatmentBuilder.deferred().group(group.id());
1689 } else {
1690 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1691 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1692 fail(fwd, ObjectiveError.GROUPMISSING);
1693 return Collections.emptySet();
1694 }
1695 }
1696 }
1697 treatmentBuilder.immediate().transition(ACL_TABLE);
1698 TrafficTreatment filteredTreatment = treatmentBuilder.build();
1699
1700 // Build bridging table entries
1701 FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder();
1702 flowRuleBuilder.fromApp(fwd.appId())
1703 .withPriority(fwd.priority())
1704 .forDevice(deviceId)
1705 .withSelector(filteredSelector)
1706 .withTreatment(filteredTreatment)
1707 .forTable(BRIDGING_TABLE);
1708 if (fwd.permanent()) {
1709 flowRuleBuilder.makePermanent();
1710 } else {
1711 flowRuleBuilder.makeTemporary(fwd.timeout());
1712 }
1713 rules.add(flowRuleBuilder.build());
1714 return rules;
1715 }
1716
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001717 //////////////////////////////////////
1718 // Helper Methods and Classes
1719 //////////////////////////////////////
1720
1721 private boolean isSupportedEthTypeObjective(ForwardingObjective fwd) {
1722 TrafficSelector selector = fwd.selector();
1723 EthTypeCriterion ethType = (EthTypeCriterion) selector
1724 .getCriterion(Criterion.Type.ETH_TYPE);
1725 return !((ethType == null) ||
1726 ((ethType.ethType().toShort() != Ethernet.TYPE_IPV4) &&
Pier Ventree0ae7a32016-11-23 09:57:42 -08001727 (ethType.ethType().toShort() != Ethernet.MPLS_UNICAST)) &&
1728 (ethType.ethType().toShort() != Ethernet.TYPE_IPV6));
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001729 }
1730
1731 private boolean isSupportedEthDstObjective(ForwardingObjective fwd) {
1732 TrafficSelector selector = fwd.selector();
1733 EthCriterion ethDst = (EthCriterion) selector
1734 .getCriterion(Criterion.Type.ETH_DST);
1735 VlanIdCriterion vlanId = (VlanIdCriterion) selector
1736 .getCriterion(Criterion.Type.VLAN_VID);
1737 return !(ethDst == null && vlanId == null);
1738 }
1739
Charles Chan50d900c2018-03-02 13:26:22 -08001740 NextGroup getGroupForNextObjective(Integer nextId) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001741 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
1742 if (next != null) {
1743 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1744 if (gkeys != null && !gkeys.isEmpty()) {
1745 return next;
1746 } else {
1747 log.warn("Empty next group found in FlowObjective store for "
1748 + "next-id:{} in dev:{}", nextId, deviceId);
1749 }
1750 } else {
1751 log.warn("next-id {} not found in Flow objective store for dev:{}",
1752 nextId, deviceId);
1753 }
1754 return null;
1755 }
1756
Charles Chan188ebf52015-12-23 00:15:11 -08001757 protected static void pass(Objective obj) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001758 obj.context().ifPresent(context -> context.onSuccess(obj));
Saurav Das822c4e22015-10-23 10:51:11 -07001759 }
1760
Charles Chan188ebf52015-12-23 00:15:11 -08001761 protected static void fail(Objective obj, ObjectiveError error) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001762 obj.context().ifPresent(context -> context.onError(obj, error));
Saurav Das822c4e22015-10-23 10:51:11 -07001763 }
Saurav Das24431192016-03-07 19:13:00 -08001764
Saurav Das24431192016-03-07 19:13:00 -08001765 @Override
1766 public List<String> getNextMappings(NextGroup nextGroup) {
1767 List<String> mappings = new ArrayList<>();
1768 List<Deque<GroupKey>> gkeys = appKryo.deserialize(nextGroup.data());
1769 for (Deque<GroupKey> gkd : gkeys) {
1770 Group lastGroup = null;
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -07001771 StringBuilder gchain = new StringBuilder();
Saurav Das24431192016-03-07 19:13:00 -08001772 for (GroupKey gk : gkd) {
1773 Group g = groupService.getGroup(deviceId, gk);
Saurav Das8be4e3a2016-03-11 17:19:07 -08001774 if (g == null) {
Saurav Das25190812016-05-27 13:54:07 -07001775 gchain.append(" NoGrp").append(" -->");
Saurav Das8be4e3a2016-03-11 17:19:07 -08001776 continue;
1777 }
1778 gchain.append(" 0x").append(Integer.toHexString(g.id().id()))
1779 .append(" -->");
Saurav Das24431192016-03-07 19:13:00 -08001780 lastGroup = g;
1781 }
1782 // add port information for last group in group-chain
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -07001783 List<Instruction> lastGroupIns = new ArrayList<>();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001784 if (lastGroup != null && !lastGroup.buckets().buckets().isEmpty()) {
Saurav Das25190812016-05-27 13:54:07 -07001785 lastGroupIns = lastGroup.buckets().buckets().get(0)
1786 .treatment().allInstructions();
1787 }
1788 for (Instruction i: lastGroupIns) {
Saurav Das24431192016-03-07 19:13:00 -08001789 if (i instanceof OutputInstruction) {
Saurav Das8be4e3a2016-03-11 17:19:07 -08001790 gchain.append(" port:").append(((OutputInstruction) i).port());
Saurav Das24431192016-03-07 19:13:00 -08001791 }
1792 }
Saurav Das8be4e3a2016-03-11 17:19:07 -08001793 mappings.add(gchain.toString());
Saurav Das24431192016-03-07 19:13:00 -08001794 }
1795 return mappings;
1796 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001797
Saurav Dasa4020382018-02-14 14:14:54 -08001798 /**
1799 * Returns true iff the given selector matches on BOS==true, indicating that
1800 * the selector is trying to match on a label that is bottom-of-stack.
1801 *
1802 * @param selector the given match
1803 * @return true iff BoS==true; false if BOS==false, or BOS matching is not
1804 * expressed in the given selector
1805 */
Pier Ventre140a8942016-11-02 07:26:38 -07001806 static boolean isMplsBos(TrafficSelector selector) {
1807 MplsBosCriterion bosCriterion = (MplsBosCriterion) selector.getCriterion(MPLS_BOS);
1808 return bosCriterion != null && bosCriterion.mplsBos();
1809 }
1810
Saurav Dasa4020382018-02-14 14:14:54 -08001811 /**
1812 * Returns true iff the given selector matches on BOS==false, indicating
1813 * that the selector is trying to match on a label that is not the
1814 * bottom-of-stack label.
1815 *
1816 * @param selector the given match
1817 * @return true iff BoS==false;
1818 * false if BOS==true, or BOS matching is not expressed in the given selector
1819 */
Pier Ventre140a8942016-11-02 07:26:38 -07001820 static boolean isNotMplsBos(TrafficSelector selector) {
1821 MplsBosCriterion bosCriterion = (MplsBosCriterion) selector.getCriterion(MPLS_BOS);
1822 return bosCriterion != null && !bosCriterion.mplsBos();
1823 }
1824
Saurav Dasa4020382018-02-14 14:14:54 -08001825 /**
1826 * Returns true iff the forwarding objective includes a treatment to pop the
1827 * MPLS label.
1828 *
1829 * @param fwd the given forwarding objective
1830 * @return true iff mpls pop treatment exists
1831 */
1832 static boolean isMplsPop(ForwardingObjective fwd) {
1833 if (fwd.treatment() != null) {
1834 for (Instruction instr : fwd.treatment().allInstructions()) {
1835 if (instr instanceof L2ModificationInstruction
1836 && ((L2ModificationInstruction) instr)
1837 .subtype() == L2SubType.MPLS_POP) {
1838 return true;
1839 }
1840 }
1841 }
1842 return false;
1843 }
1844
Charles Chand9e47c62017-10-05 15:17:15 -07001845 private static boolean isIpv6(TrafficSelector selector) {
1846 EthTypeCriterion ethTypeCriterion = (EthTypeCriterion) selector.getCriterion(ETH_TYPE);
1847 return ethTypeCriterion != null && ethTypeCriterion.ethType().toShort() == Ethernet.TYPE_IPV6;
1848 }
1849
Charles Chan50d900c2018-03-02 13:26:22 -08001850 static VlanId readVlanFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001851 if (selector == null) {
1852 return null;
1853 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001854 Criterion criterion = selector.getCriterion(Criterion.Type.VLAN_VID);
1855 return (criterion == null)
1856 ? null : ((VlanIdCriterion) criterion).vlanId();
1857 }
1858
Charles Chand9e47c62017-10-05 15:17:15 -07001859 static IpPrefix readIpDstFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001860 if (selector == null) {
1861 return null;
1862 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001863 Criterion criterion = selector.getCriterion(Criterion.Type.IPV4_DST);
1864 return (criterion == null) ? null : ((IPCriterion) criterion).ip();
1865 }
Charles Chand55e84d2016-03-30 17:54:24 -07001866
1867 private static VlanId readVlanFromTreatment(TrafficTreatment treatment) {
Saurav Das59232cf2016-04-27 18:35:50 -07001868 if (treatment == null) {
1869 return null;
1870 }
Charles Chand55e84d2016-03-30 17:54:24 -07001871 for (Instruction i : treatment.allInstructions()) {
1872 if (i instanceof ModVlanIdInstruction) {
1873 return ((ModVlanIdInstruction) i).vlanId();
1874 }
1875 }
1876 return null;
1877 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001878
Jonghwan Hyun800d9d02018-04-09 09:40:50 -07001879 protected static MacAddress readEthDstFromTreatment(TrafficTreatment treatment) {
Charles Chan45b69ab2018-03-02 15:41:41 -08001880 if (treatment == null) {
1881 return null;
1882 }
1883 for (Instruction i : treatment.allInstructions()) {
1884 if (i instanceof ModEtherInstruction) {
1885 ModEtherInstruction modEtherInstruction = (ModEtherInstruction) i;
1886 if (modEtherInstruction.subtype() == L2SubType.ETH_DST) {
1887 return modEtherInstruction.mac();
1888 }
1889 }
1890 }
1891 return null;
1892 }
1893
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001894 /**
1895 * Utility class that retries sending flows a fixed number of times, even if
1896 * some of the attempts are successful. Used only for forwarding objectives.
1897 */
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001898 public final class RetryFlows implements Runnable {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001899 int attempts = MAX_RETRY_ATTEMPTS;
1900 private Collection<FlowRule> retryFlows;
1901 private ForwardingObjective fwd;
1902
Charles Chan50d900c2018-03-02 13:26:22 -08001903 RetryFlows(ForwardingObjective fwd, Collection<FlowRule> retryFlows) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001904 this.fwd = fwd;
1905 this.retryFlows = retryFlows;
1906 }
1907
1908 @Override
1909 public void run() {
1910 log.info("RETRY FLOWS ATTEMPT# {} for fwd:{} rules:{}",
1911 MAX_RETRY_ATTEMPTS - attempts, fwd.id(), retryFlows.size());
1912 sendForward(fwd, retryFlows);
1913 if (--attempts > 0) {
1914 executorService.schedule(this, RETRY_MS, TimeUnit.MILLISECONDS);
1915 }
1916 }
1917 }
1918
Saurav Das822c4e22015-10-23 10:51:11 -07001919}