blob: f4ab685c44fa5162b2c9a3f17b7041df8cd8c1df [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;
pier8b3aef42019-03-11 15:14:02 -070019import com.google.common.collect.Lists;
Yi Tseng47f82dc2017-03-05 22:48:39 -080020import com.google.common.collect.Sets;
pier8b3aef42019-03-11 15:14:02 -070021import org.apache.commons.lang3.tuple.Pair;
Saurav Das822c4e22015-10-23 10:51:11 -070022import org.onlab.osgi.ServiceDirectory;
Jonghwan Hyun800d9d02018-04-09 09:40:50 -070023import org.onlab.packet.EthType;
Saurav Das822c4e22015-10-23 10:51:11 -070024import org.onlab.packet.Ethernet;
Alex Yashchuk4caa8e82017-12-08 17:40:05 +020025import org.onlab.packet.EthType.EtherType;
Julia Ferguson65428c32017-08-10 18:15:24 +000026import org.onlab.packet.IpAddress;
Charles Chan14967c22015-12-07 11:11:50 -080027import org.onlab.packet.IpPrefix;
Charles Chan45b69ab2018-03-02 15:41:41 -080028import org.onlab.packet.MacAddress;
Saurav Das822c4e22015-10-23 10:51:11 -070029import org.onlab.packet.VlanId;
pier8b3aef42019-03-11 15:14:02 -070030import org.onlab.util.AbstractAccumulator;
31import org.onlab.util.Accumulator;
Saurav Das822c4e22015-10-23 10:51:11 -070032import org.onlab.util.KryoNamespace;
33import org.onosproject.core.ApplicationId;
34import org.onosproject.core.CoreService;
Jonghwan Hyun800d9d02018-04-09 09:40:50 -070035import org.onosproject.driver.extensions.Ofdpa3CopyField;
Charles Chancad338a2016-09-16 18:03:11 -070036import org.onosproject.driver.extensions.Ofdpa3MplsType;
37import org.onosproject.driver.extensions.Ofdpa3SetMplsType;
Jonghwan Hyun800d9d02018-04-09 09:40:50 -070038import org.onosproject.driver.extensions.OfdpaMatchActsetOutput;
39import org.onosproject.driver.extensions.OfdpaMatchAllowVlanTranslation;
Charles Chan14967c22015-12-07 11:11:50 -080040import org.onosproject.driver.extensions.OfdpaMatchVlanVid;
41import org.onosproject.driver.extensions.OfdpaSetVlanVid;
Saurav Das822c4e22015-10-23 10:51:11 -070042import org.onosproject.net.DeviceId;
43import org.onosproject.net.Port;
44import org.onosproject.net.PortNumber;
45import org.onosproject.net.behaviour.NextGroup;
46import org.onosproject.net.behaviour.Pipeliner;
47import org.onosproject.net.behaviour.PipelinerContext;
48import org.onosproject.net.device.DeviceService;
49import org.onosproject.net.driver.AbstractHandlerBehaviour;
pier8b3aef42019-03-11 15:14:02 -070050import org.onosproject.net.driver.Driver;
Saurav Das822c4e22015-10-23 10:51:11 -070051import org.onosproject.net.flow.DefaultFlowRule;
52import org.onosproject.net.flow.DefaultTrafficSelector;
53import org.onosproject.net.flow.DefaultTrafficTreatment;
54import org.onosproject.net.flow.FlowRule;
55import org.onosproject.net.flow.FlowRuleOperations;
56import org.onosproject.net.flow.FlowRuleOperationsContext;
57import org.onosproject.net.flow.FlowRuleService;
58import org.onosproject.net.flow.TrafficSelector;
59import org.onosproject.net.flow.TrafficTreatment;
60import org.onosproject.net.flow.criteria.Criteria;
61import org.onosproject.net.flow.criteria.Criterion;
62import org.onosproject.net.flow.criteria.EthCriterion;
63import org.onosproject.net.flow.criteria.EthTypeCriterion;
jayakumarthazhath655b9a82018-10-01 00:51:54 +053064import org.onosproject.net.flow.criteria.ExtensionCriterion;
65import org.onosproject.net.flow.criteria.ExtensionSelector;
Saurav Das822c4e22015-10-23 10:51:11 -070066import org.onosproject.net.flow.criteria.IPCriterion;
Pier Ventree0ae7a32016-11-23 09:57:42 -080067import org.onosproject.net.flow.criteria.Icmpv6CodeCriterion;
68import org.onosproject.net.flow.criteria.Icmpv6TypeCriterion;
Saurav Das8a0732e2015-11-20 15:27:53 -080069import org.onosproject.net.flow.criteria.MplsBosCriterion;
70import org.onosproject.net.flow.criteria.MplsCriterion;
Saurav Das822c4e22015-10-23 10:51:11 -070071import org.onosproject.net.flow.criteria.PortCriterion;
Charles Chand9e47c62017-10-05 15:17:15 -070072import org.onosproject.net.flow.criteria.TcpPortCriterion;
73import org.onosproject.net.flow.criteria.UdpPortCriterion;
Saurav Das822c4e22015-10-23 10:51:11 -070074import org.onosproject.net.flow.criteria.VlanIdCriterion;
75import org.onosproject.net.flow.instructions.Instruction;
76import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
Charles Chanf76de302018-06-15 18:54:18 -070077import org.onosproject.net.flow.instructions.Instructions.NoActionInstruction;
Saurav Das822c4e22015-10-23 10:51:11 -070078import org.onosproject.net.flow.instructions.L2ModificationInstruction;
Saurav Das8a0732e2015-11-20 15:27:53 -080079import org.onosproject.net.flow.instructions.L2ModificationInstruction.L2SubType;
Saurav Das822c4e22015-10-23 10:51:11 -070080import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanIdInstruction;
Charles Chan45b69ab2018-03-02 15:41:41 -080081import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModEtherInstruction;
Alex Yashchuk4caa8e82017-12-08 17:40:05 +020082import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModMplsHeaderInstruction;
Charles Chan14967c22015-12-07 11:11:50 -080083import org.onosproject.net.flow.instructions.L3ModificationInstruction;
84import org.onosproject.net.flow.instructions.L3ModificationInstruction.L3SubType;
Saurav Das822c4e22015-10-23 10:51:11 -070085import org.onosproject.net.flowobjective.FilteringObjective;
86import org.onosproject.net.flowobjective.FlowObjectiveStore;
87import org.onosproject.net.flowobjective.ForwardingObjective;
88import org.onosproject.net.flowobjective.NextObjective;
89import org.onosproject.net.flowobjective.Objective;
90import org.onosproject.net.flowobjective.ObjectiveError;
Saurav Das822c4e22015-10-23 10:51:11 -070091import org.onosproject.net.group.DefaultGroupKey;
92import org.onosproject.net.group.Group;
Saurav Das822c4e22015-10-23 10:51:11 -070093import org.onosproject.net.group.GroupKey;
Saurav Das822c4e22015-10-23 10:51:11 -070094import org.onosproject.net.group.GroupService;
Saurav Das822c4e22015-10-23 10:51:11 -070095import org.onosproject.store.serializers.KryoNamespaces;
96import org.slf4j.Logger;
97
Pier Ventree0ae7a32016-11-23 09:57:42 -080098import java.util.ArrayDeque;
99import java.util.ArrayList;
100import java.util.Collection;
101import java.util.Collections;
102import java.util.Deque;
103import java.util.List;
104import java.util.Objects;
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700105import java.util.Optional;
pier8b3aef42019-03-11 15:14:02 -0700106import java.util.Timer;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800107import java.util.concurrent.ScheduledExecutorService;
108import java.util.concurrent.TimeUnit;
109
110import static java.util.concurrent.Executors.newScheduledThreadPool;
pier8b3aef42019-03-11 15:14:02 -0700111import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
Yi Tseng3a77b4f2017-02-06 15:02:17 -0800112import static org.onlab.packet.MacAddress.BROADCAST;
Charles Chanb4879a52017-10-20 19:09:16 -0700113import static org.onlab.packet.MacAddress.IPV4_MULTICAST;
114import static org.onlab.packet.MacAddress.IPV6_MULTICAST;
Yi Tseng3a77b4f2017-02-06 15:02:17 -0800115import static org.onlab.packet.MacAddress.NONE;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800116import static org.onlab.util.Tools.groupedThreads;
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700117import static org.onosproject.driver.extensions.Ofdpa3CopyField.OXM_ID_PACKET_REG_1;
118import static org.onosproject.driver.extensions.Ofdpa3CopyField.OXM_ID_VLAN_VID;
Yi Tsengef19de12017-04-24 11:33:05 -0700119import static org.onosproject.driver.pipeline.ofdpa.OfdpaGroupHandlerUtility.*;
Charles Chand9e47c62017-10-05 15:17:15 -0700120import static org.onosproject.net.flow.criteria.Criterion.Type.ETH_TYPE;
Pier Luigi075f1012018-02-01 10:23:12 +0100121import static org.onosproject.net.group.GroupDescription.Type.SELECT;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800122import static org.slf4j.LoggerFactory.getLogger;
Pier Ventre140a8942016-11-02 07:26:38 -0700123import static org.onosproject.net.flow.criteria.Criterion.Type.MPLS_BOS;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800124
Saurav Das822c4e22015-10-23 10:51:11 -0700125/**
126 * Driver for Broadcom's OF-DPA v2.0 TTP.
Saurav Das822c4e22015-10-23 10:51:11 -0700127 */
Charles Chan361154b2016-03-24 10:23:39 -0700128public class Ofdpa2Pipeline extends AbstractHandlerBehaviour implements Pipeliner {
pier8b3aef42019-03-11 15:14:02 -0700129 // Timer for the accumulator
130 private static final Timer TIMER = new Timer("fwdobj-batching");
131 private Accumulator<Pair<ForwardingObjective, Collection<FlowRule>>> accumulator;
132
Saurav Das822c4e22015-10-23 10:51:11 -0700133 protected static final int PORT_TABLE = 0;
134 protected static final int VLAN_TABLE = 10;
Pier Ventre42287df2016-11-09 14:17:26 -0800135 protected static final int VLAN_1_TABLE = 11;
136 protected static final int MPLS_L2_PORT_FLOW_TABLE = 13;
137 protected static final int MPLS_L2_PORT_PCP_TRUST_FLOW_TABLE = 16;
Saurav Das822c4e22015-10-23 10:51:11 -0700138 protected static final int TMAC_TABLE = 20;
139 protected static final int UNICAST_ROUTING_TABLE = 30;
140 protected static final int MULTICAST_ROUTING_TABLE = 40;
141 protected static final int MPLS_TABLE_0 = 23;
142 protected static final int MPLS_TABLE_1 = 24;
Pier Ventre140a8942016-11-02 07:26:38 -0700143 protected static final int MPLS_L3_TYPE_TABLE = 27;
144 protected static final int MPLS_TYPE_TABLE = 29;
Saurav Das822c4e22015-10-23 10:51:11 -0700145 protected static final int BRIDGING_TABLE = 50;
146 protected static final int ACL_TABLE = 60;
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700147 protected static final int EGRESS_VLAN_FLOW_TABLE = 210;
148 protected static final int EGRESS_DSCP_PCP_REMARK_FLOW_TABLE = 230;
149 protected static final int EGRESS_TPID_FLOW_TABLE = 235;
Saurav Das822c4e22015-10-23 10:51:11 -0700150 protected static final int MAC_LEARNING_TABLE = 254;
151 protected static final long OFPP_MAX = 0xffffff00L;
152
Saurav Das52025962016-01-28 22:30:01 -0800153 protected static final int HIGHEST_PRIORITY = 0xffff;
Saurav Das2857f382015-11-03 14:39:27 -0800154 protected static final int DEFAULT_PRIORITY = 0x8000;
Saurav Das822c4e22015-10-23 10:51:11 -0700155 protected static final int LOWEST_PRIORITY = 0x0;
156
Pier Ventre42287df2016-11-09 14:17:26 -0800157 protected static final int MPLS_L2_PORT_PRIORITY = 2;
Pier Ventre42287df2016-11-09 14:17:26 -0800158 protected static final int MPLS_TUNNEL_ID_BASE = 0x10000;
159 protected static final int MPLS_TUNNEL_ID_MAX = 0x1FFFF;
160
Pier Ventre70d53ba2016-11-17 22:26:29 -0800161 protected static final int MPLS_UNI_PORT_MAX = 0x0000FFFF;
Pier Ventre70d53ba2016-11-17 22:26:29 -0800162 protected static final int MPLS_NNI_PORT_BASE = 0x00020000;
163 protected static final int MPLS_NNI_PORT_MAX = 0x0002FFFF;
164
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700165 protected static final short ALLOW_VLAN_TRANSLATION = 1;
166 protected static final int COPY_FIELD_NBITS = 12;
167 protected static final int COPY_FIELD_OFFSET = 0;
168
Saurav Das822c4e22015-10-23 10:51:11 -0700169 private final Logger log = getLogger(getClass());
Charles Chan425854b2016-04-11 15:32:12 -0700170 protected ServiceDirectory serviceDirectory;
Saurav Das822c4e22015-10-23 10:51:11 -0700171 protected FlowRuleService flowRuleService;
Charles Chan425854b2016-04-11 15:32:12 -0700172 protected CoreService coreService;
Saurav Das8a0732e2015-11-20 15:27:53 -0800173 protected GroupService groupService;
174 protected FlowObjectiveStore flowObjectiveStore;
Saurav Das822c4e22015-10-23 10:51:11 -0700175 protected DeviceId deviceId;
176 protected ApplicationId driverId;
Saurav Das822c4e22015-10-23 10:51:11 -0700177 protected DeviceService deviceService;
Charles Chan188ebf52015-12-23 00:15:11 -0800178 protected static KryoNamespace appKryo = new KryoNamespace.Builder()
Yi Tsengef19de12017-04-24 11:33:05 -0700179 .register(KryoNamespaces.API)
180 .register(GroupKey.class)
181 .register(DefaultGroupKey.class)
182 .register(OfdpaNextGroup.class)
183 .register(ArrayDeque.class)
184 .build("Ofdpa2Pipeline");
Saurav Das822c4e22015-10-23 10:51:11 -0700185
Charles Chan425854b2016-04-11 15:32:12 -0700186 protected Ofdpa2GroupHandler groupHandler;
Saurav Das822c4e22015-10-23 10:51:11 -0700187
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700188 // flows installations to be retried
pier8b3aef42019-03-11 15:14:02 -0700189 private ScheduledExecutorService retryExecutorService
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700190 = newScheduledThreadPool(5, groupedThreads("OfdpaPipeliner", "retry-%d", log));
Charles Chan50d900c2018-03-02 13:26:22 -0800191 private static final int MAX_RETRY_ATTEMPTS = 10;
192 private static final int RETRY_MS = 1000;
Saurav Das4f980082015-11-05 13:39:15 -0800193
pier8b3aef42019-03-11 15:14:02 -0700194 // accumulator executor service
195 private ScheduledExecutorService accumulatorExecutorService
196 = newSingleThreadScheduledExecutor(groupedThreads("OfdpaPipeliner", "acc-%d", log));
197
Saurav Das822c4e22015-10-23 10:51:11 -0700198 @Override
199 public void init(DeviceId deviceId, PipelinerContext context) {
Saurav Das822c4e22015-10-23 10:51:11 -0700200 this.deviceId = deviceId;
201
Charles Chan425854b2016-04-11 15:32:12 -0700202 serviceDirectory = context.directory();
Saurav Das822c4e22015-10-23 10:51:11 -0700203 coreService = serviceDirectory.get(CoreService.class);
204 flowRuleService = serviceDirectory.get(FlowRuleService.class);
205 groupService = serviceDirectory.get(GroupService.class);
206 flowObjectiveStore = context.store();
Saurav Das822c4e22015-10-23 10:51:11 -0700207 deviceService = serviceDirectory.get(DeviceService.class);
pier8b3aef42019-03-11 15:14:02 -0700208 // Init the accumulator, if enabled
209 if (isAccumulatorEnabled()) {
210 accumulator = new ForwardingObjectiveAccumulator(context.accumulatorMaxObjectives(),
211 context.accumulatorMaxBatchMillis(),
212 context.accumulatorMaxIdleMillis());
213 }
Saurav Das822c4e22015-10-23 10:51:11 -0700214
Charles Chan40132b32017-01-22 00:19:37 -0800215 initDriverId();
216 initGroupHander(context);
Saurav Das822c4e22015-10-23 10:51:11 -0700217
Saurav Das822c4e22015-10-23 10:51:11 -0700218 initializePipeline();
Saurav Das822c4e22015-10-23 10:51:11 -0700219 }
220
Charles Chan40132b32017-01-22 00:19:37 -0800221 protected void initDriverId() {
222 driverId = coreService.registerApplication(
223 "org.onosproject.driver.Ofdpa2Pipeline");
224 }
225
226 protected void initGroupHander(PipelinerContext context) {
227 groupHandler = new Ofdpa2GroupHandler();
228 groupHandler.init(deviceId, context);
229 }
230
Saurav Das822c4e22015-10-23 10:51:11 -0700231 protected void initializePipeline() {
Charles Chan188ebf52015-12-23 00:15:11 -0800232 // OF-DPA does not require initializing the pipeline as it puts default
233 // rules automatically in the hardware. However emulation of OFDPA in
234 // software switches does require table-miss-entries.
Saurav Das822c4e22015-10-23 10:51:11 -0700235 }
236
pier8b3aef42019-03-11 15:14:02 -0700237 public boolean isAccumulatorEnabled() {
238 Driver driver = super.data().driver();
239 // we cannot determine the property
240 if (driver == null) {
241 return false;
242 }
243 return Boolean.parseBoolean(driver.getProperty(ACCUMULATOR_ENABLED));
244 }
245
Charles Chand1172632017-03-15 17:33:09 -0700246 /**
Alex Yashchuk4caa8e82017-12-08 17:40:05 +0200247 * Determines whether this pipeline requires MPLS POP instruction.
248 *
249 * @return true to use MPLS POP instruction
250 */
251 public boolean requireMplsPop() {
252 return true;
253 }
254
255 /**
256 * Determines whether this pipeline requires MPLS BOS match.
257 *
258 * @return true to use MPLS BOS match
259 */
260 public boolean requireMplsBosMatch() {
261 return true;
262 }
263
264 /**
265 * Determines whether this pipeline requires MPLS TTL decrement and copy.
266 *
267 * @return true to use MPLS TTL decrement and copy
268 */
269 public boolean requireMplsTtlModification() {
270 return true;
271 }
272
273 /**
Charles Chand1172632017-03-15 17:33:09 -0700274 * Determines whether this pipeline requires OFDPA match and set VLAN extensions.
275 *
276 * @return true to use the extensions
277 */
278 protected boolean requireVlanExtensions() {
279 return true;
280 }
281
Saurav Das86d13e82017-04-28 17:03:48 -0700282 /**
283 * Determines whether in-port should be matched on in TMAC table rules.
284 *
285 * @return true if match on in-port should be programmed
286 */
287 protected boolean matchInPortTmacTable() {
288 return true;
289 }
290
Charles Chand9e47c62017-10-05 15:17:15 -0700291 /**
292 * Determines whether matching L4 destination port on IPv6 packets is supported in ACL table.
293 *
294 * @return true if matching L4 destination port on IPv6 packets is supported in ACL table.
295 */
296 protected boolean supportIpv6L4Dst() {
297 return true;
298 }
299
Saurav Dasc568c342018-01-25 09:49:01 -0800300 /**
301 * Determines whether this driver should continue to retry flows that point
302 * to empty groups. See CORD-554.
303 *
304 * @return true if the driver should retry flows
305 */
306 protected boolean shouldRetry() {
307 return true;
308 }
309
Charles Chan45b69ab2018-03-02 15:41:41 -0800310 /**
311 * Determines whether this driver requires unicast flow to be installed before multicast flow
312 * in TMAC table.
313 *
314 * @return true if required
315 */
316 protected boolean requireUnicastBeforeMulticast() {
317 return false;
318 }
319
Andrea Campanellad980c6d2018-04-30 11:48:55 +0200320 /**
321 * Determines whether this driver supports installing a clearDeferred action on table 30.
322 *
323 * @return true if required
324 */
325 protected boolean supportsUnicastBlackHole() {
326 return true;
327 }
328
Charles Chanab591602019-01-22 17:25:04 -0800329 protected boolean requirePuntTable() {
330 return false;
331 }
332
Saurav Das822c4e22015-10-23 10:51:11 -0700333 //////////////////////////////////////
334 // Flow Objectives
335 //////////////////////////////////////
336
337 @Override
338 public void filter(FilteringObjective filteringObjective) {
339 if (filteringObjective.type() == FilteringObjective.Type.PERMIT) {
340 processFilter(filteringObjective,
341 filteringObjective.op() == Objective.Operation.ADD,
342 filteringObjective.appId());
343 } else {
344 // Note that packets that don't match the PERMIT filter are
345 // automatically denied. The DENY filter is used to deny packets
346 // that are otherwise permitted by the PERMIT filter.
347 // Use ACL table flow rules here for DENY filtering objectives
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530348 log.warn("filter objective other than PERMIT currently not supported");
Saurav Das822c4e22015-10-23 10:51:11 -0700349 fail(filteringObjective, ObjectiveError.UNSUPPORTED);
350 }
351 }
352
353 @Override
354 public void forward(ForwardingObjective fwd) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700355 Collection<FlowRule> rules = processForward(fwd);
Saurav Das25190812016-05-27 13:54:07 -0700356 if (rules == null || rules.isEmpty()) {
357 // Assumes fail message has already been generated to the objective
358 // context. Returning here prevents spurious pass message to be
359 // generated by FlowRule service for empty flowOps.
360 return;
361 }
pier8b3aef42019-03-11 15:14:02 -0700362 // Let's accumulate flow rules - otherwise send directly
363 if (accumulator != null) {
364 accumulator.add(Pair.of(fwd, rules));
365 } else {
366 sendForwards(Collections.singletonList(Pair.of(fwd, rules)));
367 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700368 }
369
pier8b3aef42019-03-11 15:14:02 -0700370 // Builds the batch using the accumulated flow rules
371 private void sendForwards(List<Pair<ForwardingObjective, Collection<FlowRule>>> pairs) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700372 FlowRuleOperations.Builder flowOpsBuilder = FlowRuleOperations.builder();
pier8b3aef42019-03-11 15:14:02 -0700373 log.debug("Sending {} fwd-objs", pairs.size());
374 List<Objective> fwdObjs = Lists.newArrayList();
375 // Iterates over all accumulated flow rules and then build an unique batch
376 pairs.forEach(pair -> {
377 ForwardingObjective fwd = pair.getLeft();
378 Collection<FlowRule> rules = pair.getRight();
379 switch (fwd.op()) {
380 case ADD:
381 rules.stream()
382 .filter(Objects::nonNull)
383 .forEach(flowOpsBuilder::add);
384 log.debug("Applying a add fwd-obj {} to sw:{}", fwd.id(), deviceId);
385 fwdObjs.add(fwd);
386 break;
387 case REMOVE:
388 rules.stream()
389 .filter(Objects::nonNull)
390 .forEach(flowOpsBuilder::remove);
391 log.debug("Deleting a flow rule to sw:{}", deviceId);
392 fwdObjs.add(fwd);
393 break;
394 default:
395 fail(fwd, ObjectiveError.UNKNOWN);
396 log.warn("Unknown forwarding type {}", fwd.op());
397 }
398 });
399 // Finally applies the operations
Saurav Das822c4e22015-10-23 10:51:11 -0700400 flowRuleService.apply(flowOpsBuilder.build(new FlowRuleOperationsContext() {
pier8b3aef42019-03-11 15:14:02 -0700401
Saurav Das822c4e22015-10-23 10:51:11 -0700402 @Override
403 public void onSuccess(FlowRuleOperations ops) {
pier8b3aef42019-03-11 15:14:02 -0700404 log.trace("Flow rule operations onSuccess {}", ops);
405 fwdObjs.forEach(Ofdpa2Pipeline::pass);
Saurav Das822c4e22015-10-23 10:51:11 -0700406 }
407
408 @Override
409 public void onError(FlowRuleOperations ops) {
pier8b3aef42019-03-11 15:14:02 -0700410 ObjectiveError error = ObjectiveError.FLOWINSTALLATIONFAILED;
411 log.warn("Flow rule operations onError {}. Reason = {}", ops, error);
412 fwdObjs.forEach(fwdObj -> fail(fwdObj, error));
Saurav Das822c4e22015-10-23 10:51:11 -0700413 }
414 }));
Saurav Das822c4e22015-10-23 10:51:11 -0700415 }
416
417 @Override
418 public void next(NextObjective nextObjective) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800419 NextGroup nextGroup = flowObjectiveStore.getNextGroup(nextObjective.id());
420 switch (nextObjective.op()) {
421 case ADD:
Saurav Das4f980082015-11-05 13:39:15 -0800422 if (nextGroup != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800423 log.warn("Cannot add next {} that already exists in device {}",
424 nextObjective.id(), deviceId);
425 return;
426 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700427 log.debug("Processing NextObjective id {} in dev {} - add group",
Saurav Das8a0732e2015-11-20 15:27:53 -0800428 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700429 groupHandler.addGroup(nextObjective);
Saurav Das8a0732e2015-11-20 15:27:53 -0800430 break;
431 case ADD_TO_EXISTING:
432 if (nextGroup != null) {
Saurav Das1547b3f2017-05-05 17:01:08 -0700433 log.debug("Processing NextObjective id {} in dev {} - add bucket",
Saurav Das8a0732e2015-11-20 15:27:53 -0800434 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700435 groupHandler.addBucketToGroup(nextObjective, nextGroup);
Saurav Das4f980082015-11-05 13:39:15 -0800436 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800437 // it is possible that group-chain has not been fully created yet
Saurav Das423fe2b2015-12-04 10:52:59 -0800438 log.debug("Waiting to add bucket to group for next-id:{} in dev:{}",
439 nextObjective.id(), deviceId);
Yi Tseng47f82dc2017-03-05 22:48:39 -0800440
441 // by design multiple pending bucket is allowed for the group
442 groupHandler.pendingBuckets.compute(nextObjective.id(), (nextId, pendBkts) -> {
443 if (pendBkts == null) {
444 pendBkts = Sets.newHashSet();
445 }
446 pendBkts.add(nextObjective);
447 return pendBkts;
448 });
Saurav Das4f980082015-11-05 13:39:15 -0800449 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800450 break;
451 case REMOVE:
452 if (nextGroup == null) {
453 log.warn("Cannot remove next {} that does not exist in device {}",
454 nextObjective.id(), deviceId);
455 return;
456 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700457 log.debug("Processing NextObjective id {} in dev {} - remove group",
Saurav Das8a0732e2015-11-20 15:27:53 -0800458 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700459 groupHandler.removeGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800460 break;
461 case REMOVE_FROM_EXISTING:
462 if (nextGroup == null) {
463 log.warn("Cannot remove from next {} that does not exist in device {}",
464 nextObjective.id(), deviceId);
465 return;
466 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700467 log.debug("Processing NextObjective id {} in dev {} - remove bucket",
Saurav Das8a0732e2015-11-20 15:27:53 -0800468 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700469 groupHandler.removeBucketFromGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800470 break;
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +0900471 case MODIFY:
472 if (nextGroup == null) {
473 log.warn("Cannot modify next {} that does not exist in device {}",
474 nextObjective.id(), deviceId);
475 return;
476 }
Ruchi Sahotaef0761c2019-01-28 01:08:18 +0000477 log.debug("Processing NextObjective id {} in dev {} group {} - modify bucket",
478 nextObjective.id(), deviceId, nextGroup);
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +0900479 groupHandler.modifyBucketFromGroup(nextObjective, nextGroup);
480 break;
Saurav Dasceccf242017-08-03 18:30:35 -0700481 case VERIFY:
482 if (nextGroup == null) {
483 log.warn("Cannot verify next {} that does not exist in device {}",
484 nextObjective.id(), deviceId);
485 return;
486 }
487 log.debug("Processing NextObjective id {} in dev {} - verify",
488 nextObjective.id(), deviceId);
489 groupHandler.verifyGroup(nextObjective, nextGroup);
490 break;
Saurav Das8a0732e2015-11-20 15:27:53 -0800491 default:
Saurav Das4f980082015-11-05 13:39:15 -0800492 log.warn("Unsupported operation {}", nextObjective.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700493 }
494 }
495
496 //////////////////////////////////////
497 // Flow handling
498 //////////////////////////////////////
499
500 /**
Yi Tseng0fdc07e2017-04-24 19:17:08 -0700501 * As per OFDPA 2.0 TTP, filtering of VLAN ids and MAC addresses (for routing)
502 * configured on switch ports happen in different tables.
Saurav Das822c4e22015-10-23 10:51:11 -0700503 *
504 * @param filt the filtering objective
505 * @param install indicates whether to add or remove the objective
506 * @param applicationId the application that sent this objective
507 */
Saurav Das52025962016-01-28 22:30:01 -0800508 protected void processFilter(FilteringObjective filt,
509 boolean install, ApplicationId applicationId) {
Saurav Das822c4e22015-10-23 10:51:11 -0700510 // This driver only processes filtering criteria defined with switch
511 // ports as the key
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530512 PortCriterion portCriterion = null;
Saurav Das822c4e22015-10-23 10:51:11 -0700513 EthCriterion ethCriterion = null;
514 VlanIdCriterion vidCriterion = null;
Saurav Das822c4e22015-10-23 10:51:11 -0700515 if (!filt.key().equals(Criteria.dummy()) &&
516 filt.key().type() == Criterion.Type.IN_PORT) {
517 portCriterion = (PortCriterion) filt.key();
Saurav Das822c4e22015-10-23 10:51:11 -0700518 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530519 if (portCriterion == null) {
520 log.debug("No IN_PORT defined in filtering objective from app: {}",
521 applicationId);
522 } else {
523 log.debug("Received filtering objective for dev/port: {}/{}", deviceId,
524 portCriterion.port());
525 }
Saurav Das822c4e22015-10-23 10:51:11 -0700526 // convert filtering conditions for switch-intfs into flowrules
527 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
528 for (Criterion criterion : filt.conditions()) {
Yi Tseng0fdc07e2017-04-24 19:17:08 -0700529 switch (criterion.type()) {
530 case ETH_DST:
531 case ETH_DST_MASKED:
532 ethCriterion = (EthCriterion) criterion;
533 break;
534 case VLAN_VID:
535 vidCriterion = (VlanIdCriterion) criterion;
536 break;
537 default:
538 log.warn("Unsupported filter {}", criterion);
539 fail(filt, ObjectiveError.UNSUPPORTED);
540 return;
Saurav Das822c4e22015-10-23 10:51:11 -0700541 }
542 }
543
Saurav Das0e99e2b2015-10-28 12:39:42 -0700544 VlanId assignedVlan = null;
Charles Chane849c192016-01-11 18:28:54 -0800545 if (vidCriterion != null) {
Charles Chand55e84d2016-03-30 17:54:24 -0700546 // Use the VLAN in criterion if metadata is not present and the traffic is tagged
Charles Chanc550f2e2017-12-19 19:55:57 -0800547 if (!vidCriterion.vlanId().equals(VlanId.NONE)) {
Charles Chane849c192016-01-11 18:28:54 -0800548 assignedVlan = vidCriterion.vlanId();
Piere5bff482018-03-07 11:42:50 +0100549 }
550 // If the meta VLAN is present let's update the assigned vlan
551 if (filt.meta() != null) {
552 VlanId metaVlan = readVlanFromTreatment(filt.meta());
553 if (metaVlan != null) {
554 assignedVlan = metaVlan;
555 }
Charles Chand55e84d2016-03-30 17:54:24 -0700556 }
Charles Chane849c192016-01-11 18:28:54 -0800557
Charles Chand55e84d2016-03-30 17:54:24 -0700558 if (assignedVlan == null) {
559 log.error("Driver fails to extract VLAN information. "
Frank Wangd8ab0962017-08-11 11:09:30 +0800560 + "Not processing VLAN filters on device {}.", deviceId);
Charles Chand55e84d2016-03-30 17:54:24 -0700561 log.debug("VLAN ID in criterion={}, metadata={}",
562 readVlanFromTreatment(filt.meta()), vidCriterion.vlanId());
563 fail(filt, ObjectiveError.BADPARAMS);
564 return;
Saurav Das0e99e2b2015-10-28 12:39:42 -0700565 }
566 }
567
Yi Tseng3a77b4f2017-02-06 15:02:17 -0800568 if (ethCriterion == null || ethCriterion.mac().equals(NONE)) {
Charles Chan985b12e2016-05-11 19:47:22 -0700569 // NOTE: it is possible that a filtering objective only has vidCriterion
Daniel Ginsburgc1d47e92018-04-30 19:27:19 -0400570 log.debug("filtering objective missing dstMac, won't program TMAC table");
Saurav Das822c4e22015-10-23 10:51:11 -0700571 } else {
Charles Chan45b69ab2018-03-02 15:41:41 -0800572 MacAddress unicastMac = readEthDstFromTreatment(filt.meta());
Charles Chan66291502018-03-02 16:43:28 -0800573 List<List<FlowRule>> allStages = processEthDstFilter(portCriterion, ethCriterion,
Charles Chan45b69ab2018-03-02 15:41:41 -0800574 vidCriterion, assignedVlan, unicastMac, applicationId);
Charles Chan66291502018-03-02 16:43:28 -0800575 for (List<FlowRule> flowRules : allStages) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800576 log.trace("Starting a new flow rule stage for TMAC table flow");
Charles Chan66291502018-03-02 16:43:28 -0800577 ops.newStage();
Charles Chanc550f2e2017-12-19 19:55:57 -0800578
Charles Chan66291502018-03-02 16:43:28 -0800579 for (FlowRule flowRule : flowRules) {
Charles Chan056e0c12018-05-10 22:19:49 +0000580 log.trace("{} flow rules in TMAC table: {} for dev: {}",
Charles Chan66291502018-03-02 16:43:28 -0800581 (install) ? "adding" : "removing", flowRules, deviceId);
Charles Chan66291502018-03-02 16:43:28 -0800582 if (install) {
583 ops = ops.add(flowRule);
Charles Chanc550f2e2017-12-19 19:55:57 -0800584 } else {
Charles Chan66291502018-03-02 16:43:28 -0800585 // NOTE: Only remove TMAC flow when there is no more enabled port within the
586 // same VLAN on this device if TMAC doesn't support matching on in_port.
587 if (matchInPortTmacTable() || (filt.meta() != null && filt.meta().clearedDeferred())) {
588 ops = ops.remove(flowRule);
589 } else {
590 log.debug("Abort TMAC flow removal on {}. Some other ports still share this TMAC flow");
591 }
Charles Chanc550f2e2017-12-19 19:55:57 -0800592 }
593 }
Saurav Das822c4e22015-10-23 10:51:11 -0700594 }
595 }
596
Charles Chan985b12e2016-05-11 19:47:22 -0700597 if (vidCriterion == null) {
598 // NOTE: it is possible that a filtering objective only has ethCriterion
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530599 log.info("filtering objective missing VLAN, cannot program VLAN Table");
Saurav Das822c4e22015-10-23 10:51:11 -0700600 } else {
Charles Chan66291502018-03-02 16:43:28 -0800601 List<List<FlowRule>> allStages = processVlanIdFilter(
Charles Chan14967c22015-12-07 11:11:50 -0800602 portCriterion, vidCriterion, assignedVlan, applicationId);
Charles Chan66291502018-03-02 16:43:28 -0800603 for (List<FlowRule> flowRules : allStages) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800604 log.trace("Starting a new flow rule stage for VLAN table flow");
Charles Chan66291502018-03-02 16:43:28 -0800605 ops.newStage();
Charles Chan14967c22015-12-07 11:11:50 -0800606
Charles Chan66291502018-03-02 16:43:28 -0800607 for (FlowRule flowRule : flowRules) {
608 log.trace("{} flow rules in VLAN table: {} for dev: {}",
609 (install) ? "adding" : "removing", flowRule, deviceId);
610 ops = install ? ops.add(flowRule) : ops.remove(flowRule);
Charles Chand1172632017-03-15 17:33:09 -0700611 }
Saurav Das822c4e22015-10-23 10:51:11 -0700612 }
613 }
614
Saurav Das822c4e22015-10-23 10:51:11 -0700615 // apply filtering flow rules
616 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
617 @Override
618 public void onSuccess(FlowRuleOperations ops) {
Saurav Das018605f2017-02-18 14:05:44 -0800619 log.debug("Applied {} filtering rules in device {}",
Saurav Das822c4e22015-10-23 10:51:11 -0700620 ops.stages().get(0).size(), deviceId);
621 pass(filt);
622 }
623
624 @Override
625 public void onError(FlowRuleOperations ops) {
626 log.info("Failed to apply all filtering rules in dev {}", deviceId);
627 fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED);
628 }
629 }));
630
631 }
632
633 /**
Charles Chand1172632017-03-15 17:33:09 -0700634 * Internal implementation of processVlanIdFilter.
635 * <p>
636 * The is_present bit in set_vlan_vid action is required to be 0 in OFDPA i12.
637 * Since it is non-OF spec, we need an extension treatment for that.
638 * The useVlanExtension must be set to false for OFDPA i12.
639 * </p>
Charles Chan66291502018-03-02 16:43:28 -0800640 * <p>
641 * NOTE: Separate VLAN filtering rules and assignment rules
642 * into different stages in order to guarantee that filtering rules
643 * always go first, as required by OFDPA.
644 * </p>
Saurav Das0e99e2b2015-10-28 12:39:42 -0700645 *
Charles Chanf9e98652016-09-07 16:54:23 -0700646 * @param portCriterion port on device for which this filter is programmed
647 * @param vidCriterion vlan assigned to port, or NONE for untagged
648 * @param assignedVlan assigned vlan-id for untagged packets
649 * @param applicationId for application programming this filter
Charles Chan66291502018-03-02 16:43:28 -0800650 * @return stages of flow rules for port-vlan filters
Saurav Das822c4e22015-10-23 10:51:11 -0700651 */
Charles Chan66291502018-03-02 16:43:28 -0800652 protected List<List<FlowRule>> processVlanIdFilter(PortCriterion portCriterion,
Yi Tsengef19de12017-04-24 11:33:05 -0700653 VlanIdCriterion vidCriterion,
654 VlanId assignedVlan,
655 ApplicationId applicationId) {
Charles Chan66291502018-03-02 16:43:28 -0800656 List<FlowRule> filteringRules = new ArrayList<>();
657 List<FlowRule> assignmentRules = new ArrayList<>();
Saurav Das822c4e22015-10-23 10:51:11 -0700658 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
659 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800660 TrafficSelector.Builder preSelector = null;
661 TrafficTreatment.Builder preTreatment = null;
662
Saurav Das4f980082015-11-05 13:39:15 -0800663 treatment.transition(TMAC_TABLE);
664
Saurav Das822c4e22015-10-23 10:51:11 -0700665 if (vidCriterion.vlanId() == VlanId.NONE) {
666 // untagged packets are assigned vlans
Charles Chand1172632017-03-15 17:33:09 -0700667 preSelector = DefaultTrafficSelector.builder();
668 if (requireVlanExtensions()) {
669 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(VlanId.NONE);
670 selector.extension(ofdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700671 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
672 treatment.extension(ofdpaSetVlanVid, deviceId);
Charles Chand1172632017-03-15 17:33:09 -0700673
674 OfdpaMatchVlanVid preOfdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
675 preSelector.extension(preOfdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700676 } else {
Charles Chand1172632017-03-15 17:33:09 -0700677 selector.matchVlanId(VlanId.NONE);
Charles Chanf9e98652016-09-07 16:54:23 -0700678 treatment.setVlanId(assignedVlan);
Charles Chand1172632017-03-15 17:33:09 -0700679
680 preSelector.matchVlanId(assignedVlan);
681 }
682 preTreatment = DefaultTrafficTreatment.builder().transition(TMAC_TABLE);
683 } else {
684 if (requireVlanExtensions()) {
685 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
686 selector.extension(ofdpaMatchVlanVid, deviceId);
687 } else {
688 selector.matchVlanId(vidCriterion.vlanId());
Charles Chanf9e98652016-09-07 16:54:23 -0700689 }
Charles Chan14967c22015-12-07 11:11:50 -0800690
Charles Chand55e84d2016-03-30 17:54:24 -0700691 if (!assignedVlan.equals(vidCriterion.vlanId())) {
Charles Chand1172632017-03-15 17:33:09 -0700692 if (requireVlanExtensions()) {
Charles Chanc03782d2017-01-31 13:57:55 -0800693 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
694 treatment.extension(ofdpaSetVlanVid, deviceId);
695 } else {
696 treatment.setVlanId(assignedVlan);
697 }
Charles Chand55e84d2016-03-30 17:54:24 -0700698 }
Saurav Das822c4e22015-10-23 10:51:11 -0700699 }
Saurav Das822c4e22015-10-23 10:51:11 -0700700
701 // ofdpa cannot match on ALL portnumber, so we need to use separate
702 // rules for each port.
Charles Chan14967c22015-12-07 11:11:50 -0800703 List<PortNumber> portnums = new ArrayList<>();
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530704 if (portCriterion != null) {
705 if (PortNumber.ALL.equals(portCriterion.port())) {
706 for (Port port : deviceService.getPorts(deviceId)) {
707 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
708 portnums.add(port.number());
709 }
Saurav Das822c4e22015-10-23 10:51:11 -0700710 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530711 } else {
712 portnums.add(portCriterion.port());
Saurav Das822c4e22015-10-23 10:51:11 -0700713 }
714 } else {
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530715 log.warn("Filtering Objective missing Port Criterion . " +
Charles Chan66291502018-03-02 16:43:28 -0800716 "VLAN Table cannot be programmed for {}", deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700717 }
Saurav Das4f980082015-11-05 13:39:15 -0800718
Saurav Das822c4e22015-10-23 10:51:11 -0700719 for (PortNumber pnum : portnums) {
Saurav Das4f980082015-11-05 13:39:15 -0800720 // create rest of flowrule
Saurav Das822c4e22015-10-23 10:51:11 -0700721 selector.matchInPort(pnum);
722 FlowRule rule = DefaultFlowRule.builder()
723 .forDevice(deviceId)
724 .withSelector(selector.build())
725 .withTreatment(treatment.build())
726 .withPriority(DEFAULT_PRIORITY)
727 .fromApp(applicationId)
728 .makePermanent()
729 .forTable(VLAN_TABLE).build();
Charles Chan66291502018-03-02 16:43:28 -0800730 assignmentRules.add(rule);
Charles Chan14967c22015-12-07 11:11:50 -0800731
732 if (preSelector != null) {
733 preSelector.matchInPort(pnum);
734 FlowRule preRule = DefaultFlowRule.builder()
735 .forDevice(deviceId)
736 .withSelector(preSelector.build())
737 .withTreatment(preTreatment.build())
738 .withPriority(DEFAULT_PRIORITY)
739 .fromApp(applicationId)
740 .makePermanent()
741 .forTable(VLAN_TABLE).build();
Charles Chan66291502018-03-02 16:43:28 -0800742 filteringRules.add(preRule);
Charles Chan14967c22015-12-07 11:11:50 -0800743 }
Saurav Das822c4e22015-10-23 10:51:11 -0700744 }
Charles Chan2686dd72018-03-06 22:10:15 -0800745 return ImmutableList.of(filteringRules, assignmentRules);
Saurav Das822c4e22015-10-23 10:51:11 -0700746 }
747
748 /**
749 * Allows routed packets with correct destination MAC to be directed
Charles Chan45b69ab2018-03-02 15:41:41 -0800750 * to unicast routing table, multicast routing table or MPLS forwarding table.
Saurav Das822c4e22015-10-23 10:51:11 -0700751 *
752 * @param portCriterion port on device for which this filter is programmed
753 * @param ethCriterion dstMac of device for which is filter is programmed
754 * @param vidCriterion vlan assigned to port, or NONE for untagged
Saurav Das0e99e2b2015-10-28 12:39:42 -0700755 * @param assignedVlan assigned vlan-id for untagged packets
Charles Chan45b69ab2018-03-02 15:41:41 -0800756 * @param unicastMac some switches require a unicast TMAC flow to be programmed before multicast
757 * TMAC flow. This MAC address will be used for the unicast TMAC flow.
758 * This is unused if the filtering objective is a unicast.
Saurav Das822c4e22015-10-23 10:51:11 -0700759 * @param applicationId for application programming this filter
Charles Chan66291502018-03-02 16:43:28 -0800760 * @return stages of flow rules for port-vlan filters
Saurav Das822c4e22015-10-23 10:51:11 -0700761
762 */
Charles Chan66291502018-03-02 16:43:28 -0800763 protected List<List<FlowRule>> processEthDstFilter(PortCriterion portCriterion,
Saurav Das822c4e22015-10-23 10:51:11 -0700764 EthCriterion ethCriterion,
765 VlanIdCriterion vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700766 VlanId assignedVlan,
Charles Chan45b69ab2018-03-02 15:41:41 -0800767 MacAddress unicastMac,
Saurav Das822c4e22015-10-23 10:51:11 -0700768 ApplicationId applicationId) {
Charles Chan5270ed02016-01-30 23:22:37 -0800769 // Consider PortNumber.ANY as wildcard. Match ETH_DST only
Sivachidambaram Subramanianc598d062017-05-16 22:42:06 +0530770 if (portCriterion != null && PortNumber.ANY.equals(portCriterion.port())) {
Charles Chan5270ed02016-01-30 23:22:37 -0800771 return processEthDstOnlyFilter(ethCriterion, applicationId);
772 }
773
Charles Chan5b9df8d2016-03-28 22:21:40 -0700774 // Multicast MAC
775 if (ethCriterion.mask() != null) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800776 return processMcastEthDstFilter(ethCriterion, assignedVlan, unicastMac, applicationId);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700777 }
778
Saurav Das822c4e22015-10-23 10:51:11 -0700779 //handling untagged packets via assigned VLAN
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530780 if (vidCriterion != null && vidCriterion.vlanId() == VlanId.NONE) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700781 vidCriterion = (VlanIdCriterion) Criteria.matchVlanId(assignedVlan);
Saurav Das822c4e22015-10-23 10:51:11 -0700782 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530783 List<FlowRule> rules = new ArrayList<>();
784 OfdpaMatchVlanVid ofdpaMatchVlanVid = null;
785 if (vidCriterion != null && requireVlanExtensions()) {
786 ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
787 }
Saurav Das822c4e22015-10-23 10:51:11 -0700788 // ofdpa cannot match on ALL portnumber, so we need to use separate
789 // rules for each port.
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -0700790 List<PortNumber> portnums = new ArrayList<>();
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530791 if (portCriterion != null) {
792 if (PortNumber.ALL.equals(portCriterion.port())) {
793 for (Port port : deviceService.getPorts(deviceId)) {
794 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
795 portnums.add(port.number());
796 }
Saurav Das822c4e22015-10-23 10:51:11 -0700797 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530798 } else {
799 portnums.add(portCriterion.port());
800 }
801 for (PortNumber pnum : portnums) {
802 rules.add(buildTmacRuleForIpv4(ethCriterion,
803 vidCriterion,
804 ofdpaMatchVlanVid,
805 applicationId,
806 pnum));
807 rules.add(buildTmacRuleForMpls(ethCriterion,
808 vidCriterion,
809 ofdpaMatchVlanVid,
810 applicationId,
811 pnum));
812 rules.add(buildTmacRuleForIpv6(ethCriterion,
813 vidCriterion,
814 ofdpaMatchVlanVid,
815 applicationId,
816 pnum));
Saurav Das822c4e22015-10-23 10:51:11 -0700817 }
818 } else {
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530819 rules.add(buildTmacRuleForIpv4(ethCriterion,
820 vidCriterion,
821 ofdpaMatchVlanVid,
822 applicationId,
823 null));
824 rules.add(buildTmacRuleForMpls(ethCriterion,
825 vidCriterion,
826 ofdpaMatchVlanVid,
827 applicationId,
828 null));
829 rules.add(buildTmacRuleForIpv6(ethCriterion,
830 vidCriterion,
831 ofdpaMatchVlanVid,
832 applicationId,
833 null));
Saurav Das822c4e22015-10-23 10:51:11 -0700834 }
Charles Chan66291502018-03-02 16:43:28 -0800835 return ImmutableList.of(rules);
Saurav Das822c4e22015-10-23 10:51:11 -0700836 }
837
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530838 /**
839 * Builds TMAC rules for IPv4 packets.
840 *
Charles Chanc550f2e2017-12-19 19:55:57 -0800841 * @param ethCriterion dst mac matching
842 * @param vidCriterion vlan id assigned to the port
843 * @param ofdpaMatchVlanVid OFDPA vlan id matching
844 * @param applicationId application id
845 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530846 * @return TMAC rule for IPV4 packets
847 */
848 private FlowRule buildTmacRuleForIpv4(EthCriterion ethCriterion,
849 VlanIdCriterion vidCriterion,
850 OfdpaMatchVlanVid ofdpaMatchVlanVid,
851 ApplicationId applicationId,
852 PortNumber pnum) {
853 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
854 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
855 if (pnum != null) {
856 if (matchInPortTmacTable()) {
857 selector.matchInPort(pnum);
858 } else {
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800859 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530860 "ignoring the IN_PORT criteria");
861 }
862 }
863 if (vidCriterion != null) {
864 if (requireVlanExtensions()) {
865 selector.extension(ofdpaMatchVlanVid, deviceId);
866 } else {
867 selector.matchVlanId(vidCriterion.vlanId());
868 }
869 }
870 selector.matchEthType(Ethernet.TYPE_IPV4);
871 selector.matchEthDst(ethCriterion.mac());
872 treatment.transition(UNICAST_ROUTING_TABLE);
873 return DefaultFlowRule.builder()
874 .forDevice(deviceId)
875 .withSelector(selector.build())
876 .withTreatment(treatment.build())
877 .withPriority(DEFAULT_PRIORITY)
878 .fromApp(applicationId)
879 .makePermanent()
880 .forTable(TMAC_TABLE).build();
881 }
882
883 /**
884 * Builds TMAC rule for MPLS packets.
885 *
Charles Chanc550f2e2017-12-19 19:55:57 -0800886 * @param ethCriterion dst mac matching
887 * @param vidCriterion vlan id assigned to the port
888 * @param ofdpaMatchVlanVid OFDPA vlan id matching
889 * @param applicationId application id
890 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530891 * @return TMAC rule for MPLS packets
892 */
893 private FlowRule buildTmacRuleForMpls(EthCriterion ethCriterion,
894 VlanIdCriterion vidCriterion,
895 OfdpaMatchVlanVid ofdpaMatchVlanVid,
896 ApplicationId applicationId,
897 PortNumber pnum) {
898 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
899 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
900 if (pnum != null) {
901 if (matchInPortTmacTable()) {
902 selector.matchInPort(pnum);
903 } else {
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800904 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530905 "ignoring the IN_PORT criteria");
906 }
907 }
908 if (vidCriterion != null) {
909 if (requireVlanExtensions()) {
910 selector.extension(ofdpaMatchVlanVid, deviceId);
911 } else {
912 selector.matchVlanId(vidCriterion.vlanId());
913 }
914 }
915 selector.matchEthType(Ethernet.MPLS_UNICAST);
916 selector.matchEthDst(ethCriterion.mac());
917 treatment.transition(MPLS_TABLE_0);
918 return DefaultFlowRule.builder()
919 .forDevice(deviceId)
920 .withSelector(selector.build())
921 .withTreatment(treatment.build())
922 .withPriority(DEFAULT_PRIORITY)
923 .fromApp(applicationId)
924 .makePermanent()
925 .forTable(TMAC_TABLE).build();
926 }
927
928 /**
929 * Builds TMAC rules for IPv6 packets.
930 *
Charles Chanc550f2e2017-12-19 19:55:57 -0800931 * @param ethCriterion dst mac matching
932 * @param vidCriterion vlan id assigned to the port
933 * @param ofdpaMatchVlanVid OFDPA vlan id matching
934 * @param applicationId application id
935 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530936 * @return TMAC rule for IPV6 packets
937 */
938 private FlowRule buildTmacRuleForIpv6(EthCriterion ethCriterion,
939 VlanIdCriterion vidCriterion,
940 OfdpaMatchVlanVid ofdpaMatchVlanVid,
941 ApplicationId applicationId,
942 PortNumber pnum) {
943 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
944 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
945 if (pnum != null) {
946 if (matchInPortTmacTable()) {
947 selector.matchInPort(pnum);
948 } else {
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800949 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530950 "ignoring the IN_PORT criteria");
951 }
952 }
953 if (vidCriterion != null) {
954 if (requireVlanExtensions()) {
955 selector.extension(ofdpaMatchVlanVid, deviceId);
956 } else {
957 selector.matchVlanId(vidCriterion.vlanId());
958 }
959 }
960 selector.matchEthType(Ethernet.TYPE_IPV6);
961 selector.matchEthDst(ethCriterion.mac());
962 treatment.transition(UNICAST_ROUTING_TABLE);
963 return DefaultFlowRule.builder()
964 .forDevice(deviceId)
965 .withSelector(selector.build())
966 .withTreatment(treatment.build())
967 .withPriority(DEFAULT_PRIORITY)
968 .fromApp(applicationId)
969 .makePermanent()
970 .forTable(TMAC_TABLE).build();
971 }
972
Charles Chan66291502018-03-02 16:43:28 -0800973 protected List<List<FlowRule>> processEthDstOnlyFilter(EthCriterion ethCriterion,
Yi Tsengef19de12017-04-24 11:33:05 -0700974 ApplicationId applicationId) {
Pier Luigi0e358632017-01-31 09:35:05 -0800975 ImmutableList.Builder<FlowRule> builder = ImmutableList.builder();
976
Charles Chan5270ed02016-01-30 23:22:37 -0800977 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
978 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
979 selector.matchEthType(Ethernet.TYPE_IPV4);
980 selector.matchEthDst(ethCriterion.mac());
981 treatment.transition(UNICAST_ROUTING_TABLE);
982 FlowRule rule = DefaultFlowRule.builder()
983 .forDevice(deviceId)
984 .withSelector(selector.build())
985 .withTreatment(treatment.build())
986 .withPriority(DEFAULT_PRIORITY)
987 .fromApp(applicationId)
988 .makePermanent()
989 .forTable(TMAC_TABLE).build();
Pier Luigi0e358632017-01-31 09:35:05 -0800990 builder.add(rule);
991
992 selector = DefaultTrafficSelector.builder();
993 treatment = DefaultTrafficTreatment.builder();
994 selector.matchEthType(Ethernet.TYPE_IPV6);
995 selector.matchEthDst(ethCriterion.mac());
996 treatment.transition(UNICAST_ROUTING_TABLE);
997 rule = DefaultFlowRule.builder()
998 .forDevice(deviceId)
999 .withSelector(selector.build())
1000 .withTreatment(treatment.build())
1001 .withPriority(DEFAULT_PRIORITY)
1002 .fromApp(applicationId)
1003 .makePermanent()
1004 .forTable(TMAC_TABLE).build();
Charles Chan66291502018-03-02 16:43:28 -08001005 return ImmutableList.of(builder.add(rule).build());
Charles Chan5270ed02016-01-30 23:22:37 -08001006 }
1007
Charles Chan66291502018-03-02 16:43:28 -08001008 List<List<FlowRule>> processMcastEthDstFilter(EthCriterion ethCriterion,
Charles Chanb4879a52017-10-20 19:09:16 -07001009 VlanId assignedVlan,
Charles Chan45b69ab2018-03-02 15:41:41 -08001010 MacAddress unicastMac,
Yi Tsengef19de12017-04-24 11:33:05 -07001011 ApplicationId applicationId) {
Charles Chan45b69ab2018-03-02 15:41:41 -08001012 ImmutableList.Builder<FlowRule> unicastFlows = ImmutableList.builder();
1013 ImmutableList.Builder<FlowRule> multicastFlows = ImmutableList.builder();
1014 TrafficSelector.Builder selector;
1015 TrafficTreatment.Builder treatment;
Charles Chanb4879a52017-10-20 19:09:16 -07001016 FlowRule rule;
Julia Ferguson65428c32017-08-10 18:15:24 +00001017
Charles Chanb4879a52017-10-20 19:09:16 -07001018 if (IPV4_MULTICAST.equals(ethCriterion.mac())) {
Charles Chan45b69ab2018-03-02 15:41:41 -08001019 if (requireUnicastBeforeMulticast()) {
1020 selector = DefaultTrafficSelector.builder();
1021 treatment = DefaultTrafficTreatment.builder();
1022 selector.matchEthType(Ethernet.TYPE_IPV4);
1023 selector.matchEthDst(unicastMac);
Charles Chan056e0c12018-05-10 22:19:49 +00001024 selector.matchVlanId(assignedVlan);
Charles Chan45b69ab2018-03-02 15:41:41 -08001025 treatment.transition(UNICAST_ROUTING_TABLE);
1026 rule = DefaultFlowRule.builder()
1027 .forDevice(deviceId)
1028 .withSelector(selector.build())
1029 .withTreatment(treatment.build())
Charles Chan056e0c12018-05-10 22:19:49 +00001030 .withPriority(DEFAULT_PRIORITY)
Charles Chan45b69ab2018-03-02 15:41:41 -08001031 .fromApp(applicationId)
1032 .makePermanent()
1033 .forTable(TMAC_TABLE).build();
1034 unicastFlows.add(rule);
1035 }
1036
1037 selector = DefaultTrafficSelector.builder();
1038 treatment = DefaultTrafficTreatment.builder();
Charles Chanb4879a52017-10-20 19:09:16 -07001039 selector.matchEthType(Ethernet.TYPE_IPV4);
1040 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
Charles Chan056e0c12018-05-10 22:19:49 +00001041 selector.matchVlanId(assignedVlan);
Charles Chanb4879a52017-10-20 19:09:16 -07001042 treatment.transition(MULTICAST_ROUTING_TABLE);
1043 rule = DefaultFlowRule.builder()
1044 .forDevice(deviceId)
1045 .withSelector(selector.build())
1046 .withTreatment(treatment.build())
Charles Chan056e0c12018-05-10 22:19:49 +00001047 .withPriority(DEFAULT_PRIORITY)
Charles Chanb4879a52017-10-20 19:09:16 -07001048 .fromApp(applicationId)
1049 .makePermanent()
1050 .forTable(TMAC_TABLE).build();
Charles Chan45b69ab2018-03-02 15:41:41 -08001051 multicastFlows.add(rule);
Charles Chanb4879a52017-10-20 19:09:16 -07001052 }
1053
1054 if (IPV6_MULTICAST.equals(ethCriterion.mac())) {
Charles Chan45b69ab2018-03-02 15:41:41 -08001055 if (requireUnicastBeforeMulticast()) {
1056 selector = DefaultTrafficSelector.builder();
1057 treatment = DefaultTrafficTreatment.builder();
1058 selector.matchEthType(Ethernet.TYPE_IPV6);
1059 selector.matchEthDst(unicastMac);
Charles Chan056e0c12018-05-10 22:19:49 +00001060 selector.matchVlanId(assignedVlan);
Charles Chan45b69ab2018-03-02 15:41:41 -08001061 treatment.transition(UNICAST_ROUTING_TABLE);
1062 rule = DefaultFlowRule.builder()
1063 .forDevice(deviceId)
1064 .withSelector(selector.build())
1065 .withTreatment(treatment.build())
Charles Chan056e0c12018-05-10 22:19:49 +00001066 .withPriority(DEFAULT_PRIORITY)
Charles Chan45b69ab2018-03-02 15:41:41 -08001067 .fromApp(applicationId)
1068 .makePermanent()
1069 .forTable(TMAC_TABLE).build();
1070 unicastFlows.add(rule);
1071 }
1072
Charles Chanb4879a52017-10-20 19:09:16 -07001073 selector = DefaultTrafficSelector.builder();
1074 treatment = DefaultTrafficTreatment.builder();
1075 selector.matchEthType(Ethernet.TYPE_IPV6);
1076 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
Charles Chan056e0c12018-05-10 22:19:49 +00001077 selector.matchVlanId(assignedVlan);
Charles Chanb4879a52017-10-20 19:09:16 -07001078 treatment.transition(MULTICAST_ROUTING_TABLE);
1079 rule = DefaultFlowRule.builder()
1080 .forDevice(deviceId)
1081 .withSelector(selector.build())
1082 .withTreatment(treatment.build())
Charles Chan056e0c12018-05-10 22:19:49 +00001083 .withPriority(DEFAULT_PRIORITY)
Charles Chanb4879a52017-10-20 19:09:16 -07001084 .fromApp(applicationId)
1085 .makePermanent()
1086 .forTable(TMAC_TABLE).build();
Charles Chan45b69ab2018-03-02 15:41:41 -08001087 multicastFlows.add(rule);
Charles Chanb4879a52017-10-20 19:09:16 -07001088 }
Charles Chan45b69ab2018-03-02 15:41:41 -08001089 return ImmutableList.of(unicastFlows.build(), multicastFlows.build());
Charles Chan5b9df8d2016-03-28 22:21:40 -07001090 }
1091
Saurav Das822c4e22015-10-23 10:51:11 -07001092 private Collection<FlowRule> processForward(ForwardingObjective fwd) {
1093 switch (fwd.flag()) {
1094 case SPECIFIC:
1095 return processSpecific(fwd);
1096 case VERSATILE:
1097 return processVersatile(fwd);
Jonghwan Hyun800d9d02018-04-09 09:40:50 -07001098 case EGRESS:
1099 return processEgress(fwd);
Saurav Das822c4e22015-10-23 10:51:11 -07001100 default:
1101 fail(fwd, ObjectiveError.UNKNOWN);
1102 log.warn("Unknown forwarding flag {}", fwd.flag());
1103 }
1104 return Collections.emptySet();
1105 }
1106
1107 /**
Jonghwan Hyun800d9d02018-04-09 09:40:50 -07001108 * In the OF-DPA 2.0 pipeline, egress forwarding objectives go to the
1109 * egress tables.
1110 * @param fwd the forwarding objective of type 'egress'
1111 * @return a collection of flow rules to be sent to the switch. An empty
1112 * collection may be returned if there is a problem in processing
1113 * the flow rule
1114 */
1115 protected Collection<FlowRule> processEgress(ForwardingObjective fwd) {
1116 log.debug("Processing egress forwarding objective:{} in dev:{}",
1117 fwd, deviceId);
1118
1119 List<FlowRule> rules = new ArrayList<>();
1120
1121 // Build selector
1122 TrafficSelector.Builder sb = DefaultTrafficSelector.builder();
1123 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) fwd.selector().getCriterion(Criterion.Type.VLAN_VID);
1124 if (vlanIdCriterion == null) {
1125 log.error("Egress forwarding objective:{} must include vlanId", fwd.id());
1126 fail(fwd, ObjectiveError.BADPARAMS);
1127 return rules;
1128 }
1129
1130 Optional<Instruction> outInstr = fwd.treatment().allInstructions().stream()
1131 .filter(instruction -> instruction instanceof OutputInstruction).findFirst();
1132 if (!outInstr.isPresent()) {
1133 log.error("Egress forwarding objective:{} must include output port", fwd.id());
1134 fail(fwd, ObjectiveError.BADPARAMS);
1135 return rules;
1136 }
1137
1138 PortNumber portNumber = ((OutputInstruction) outInstr.get()).port();
1139
1140 sb.matchVlanId(vlanIdCriterion.vlanId());
1141 OfdpaMatchActsetOutput actsetOutput = new OfdpaMatchActsetOutput(portNumber);
1142 sb.extension(actsetOutput, deviceId);
1143
1144 sb.extension(new OfdpaMatchAllowVlanTranslation(ALLOW_VLAN_TRANSLATION), deviceId);
1145
1146 // Build a flow rule for Egress VLAN Flow table
1147 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
1148 tb.transition(EGRESS_DSCP_PCP_REMARK_FLOW_TABLE);
1149 if (fwd.treatment() != null) {
1150 for (Instruction instr : fwd.treatment().allInstructions()) {
1151 if (instr instanceof L2ModificationInstruction &&
1152 ((L2ModificationInstruction) instr).subtype() == L2SubType.VLAN_ID) {
1153 tb.immediate().add(instr);
1154 }
1155 if (instr instanceof L2ModificationInstruction &&
1156 ((L2ModificationInstruction) instr).subtype() == L2SubType.VLAN_PUSH) {
1157 tb.immediate().pushVlan();
1158 EthType ethType = ((L2ModificationInstruction.ModVlanHeaderInstruction) instr).ethernetType();
1159 if (ethType.equals(EtherType.QINQ.ethType())) {
1160 // Build a flow rule for Egress TPID Flow table
1161 TrafficSelector tpidSelector = DefaultTrafficSelector.builder()
1162 .extension(actsetOutput, deviceId)
1163 .matchVlanId(VlanId.ANY).build();
1164
1165 TrafficTreatment tpidTreatment = DefaultTrafficTreatment.builder()
1166 .extension(new Ofdpa3CopyField(COPY_FIELD_NBITS, COPY_FIELD_OFFSET,
1167 COPY_FIELD_OFFSET, OXM_ID_VLAN_VID,
1168 OXM_ID_PACKET_REG_1),
1169 deviceId)
1170 .popVlan()
1171 .pushVlan(EtherType.QINQ.ethType())
1172 .extension(new Ofdpa3CopyField(COPY_FIELD_NBITS, COPY_FIELD_OFFSET,
1173 COPY_FIELD_OFFSET, OXM_ID_PACKET_REG_1,
1174 OXM_ID_VLAN_VID),
1175 deviceId)
1176 .build();
1177
1178 FlowRule.Builder tpidRuleBuilder = DefaultFlowRule.builder()
1179 .fromApp(fwd.appId())
1180 .withPriority(fwd.priority())
1181 .forDevice(deviceId)
1182 .withSelector(tpidSelector)
1183 .withTreatment(tpidTreatment)
1184 .makePermanent()
1185 .forTable(EGRESS_TPID_FLOW_TABLE);
1186 rules.add(tpidRuleBuilder.build());
1187 }
1188 }
1189 }
1190 }
1191
1192 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1193 .fromApp(fwd.appId())
1194 .withPriority(fwd.priority())
1195 .forDevice(deviceId)
1196 .withSelector(sb.build())
1197 .withTreatment(tb.build())
1198 .makePermanent()
1199 .forTable(EGRESS_VLAN_FLOW_TABLE);
1200 rules.add(ruleBuilder.build());
1201 return rules;
1202 }
1203
1204 /**
Saurav Das822c4e22015-10-23 10:51:11 -07001205 * In the OF-DPA 2.0 pipeline, versatile forwarding objectives go to the
1206 * ACL table.
1207 * @param fwd the forwarding objective of type 'versatile'
1208 * @return a collection of flow rules to be sent to the switch. An empty
1209 * collection may be returned if there is a problem in processing
1210 * the flow rule
1211 */
Saurav Das52025962016-01-28 22:30:01 -08001212 protected Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
Saurav Das018605f2017-02-18 14:05:44 -08001213 log.debug("Processing versatile forwarding objective:{} in dev:{}",
Saurav Dasd2fded02016-12-02 15:43:47 -08001214 fwd.id(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001215
Saurav Das822c4e22015-10-23 10:51:11 -07001216 if (fwd.nextId() == null && fwd.treatment() == null) {
1217 log.error("Forwarding objective {} from {} must contain "
1218 + "nextId or Treatment", fwd.selector(), fwd.appId());
Zsolt Haraszti9faab752016-02-17 15:55:20 -08001219 fail(fwd, ObjectiveError.BADPARAMS);
Saurav Das822c4e22015-10-23 10:51:11 -07001220 return Collections.emptySet();
1221 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001222
Charles Chanab591602019-01-22 17:25:04 -08001223 TrafficSelector.Builder sbuilder = versatileSelectorBuilder(fwd);
1224 TrafficTreatment.Builder ttBuilder = versatileTreatmentBuilder(fwd);
1225 if (ttBuilder == null) {
1226 return Collections.emptySet();
1227 }
1228
1229 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1230 .fromApp(fwd.appId())
1231 .withPriority(fwd.priority())
1232 .forDevice(deviceId)
1233 .withSelector(sbuilder.build())
1234 .withTreatment(ttBuilder.build())
1235 .makePermanent()
1236 .forTable(ACL_TABLE);
1237 return Collections.singletonList(ruleBuilder.build());
1238 }
1239
1240 /**
1241 * Helper function to create traffic selector builder for versatile forwarding objectives.
1242 *
1243 * @param fwd original forwarding objective
1244 * @return selector builder for the flow rule
1245 */
1246 protected TrafficSelector.Builder versatileSelectorBuilder(ForwardingObjective fwd) {
Saurav Das77b5e902016-01-27 17:01:59 -08001247 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
1248 fwd.selector().criteria().forEach(criterion -> {
1249 if (criterion instanceof VlanIdCriterion) {
1250 VlanId vlanId = ((VlanIdCriterion) criterion).vlanId();
1251 // ensure that match does not include vlan = NONE as OF-DPA does not
1252 // match untagged packets this way in the ACL table.
1253 if (vlanId.equals(VlanId.NONE)) {
1254 return;
1255 }
Charles Chand1172632017-03-15 17:33:09 -07001256 if (requireVlanExtensions()) {
1257 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanId);
1258 sbuilder.extension(ofdpaMatchVlanVid, deviceId);
1259 } else {
1260 sbuilder.matchVlanId(vlanId);
1261 }
Charles Chan09bf2692018-01-11 11:48:18 -08001262 } else if (criterion instanceof Icmpv6TypeCriterion) {
1263 byte icmpv6Type = (byte) ((Icmpv6TypeCriterion) criterion).icmpv6Type();
1264 sbuilder.matchIcmpv6Type(icmpv6Type);
1265 } else if (criterion instanceof Icmpv6CodeCriterion) {
1266 byte icmpv6Code = (byte) ((Icmpv6CodeCriterion) criterion).icmpv6Code();
1267 sbuilder.matchIcmpv6Type(icmpv6Code);
Charles Chand9e47c62017-10-05 15:17:15 -07001268 } else if (criterion instanceof TcpPortCriterion || criterion instanceof UdpPortCriterion) {
1269 // FIXME: QMX switches do not support L4 dst port matching in ACL table.
1270 // Currently L4 dst port matching is only used by DHCP relay feature
1271 // and therefore is safe to be replaced with L4 src port matching.
1272 // We need to revisit this if L4 dst port is used for other purpose in the future.
1273 if (!supportIpv6L4Dst() && isIpv6(fwd.selector())) {
Charles Chand9e47c62017-10-05 15:17:15 -07001274 switch (criterion.type()) {
1275 case UDP_DST:
Charles Chand9e47c62017-10-05 15:17:15 -07001276 case UDP_DST_MASKED:
Charles Chand9e47c62017-10-05 15:17:15 -07001277 case TCP_DST:
Charles Chand9e47c62017-10-05 15:17:15 -07001278 case TCP_DST_MASKED:
Charles Chand9e47c62017-10-05 15:17:15 -07001279 break;
1280 default:
1281 sbuilder.add(criterion);
1282 }
1283 } else {
1284 sbuilder.add(criterion);
1285 }
Saurav Das77b5e902016-01-27 17:01:59 -08001286 } else {
1287 sbuilder.add(criterion);
1288 }
1289 });
Charles Chanab591602019-01-22 17:25:04 -08001290 return sbuilder;
1291 }
Saurav Das77b5e902016-01-27 17:01:59 -08001292
Charles Chanab591602019-01-22 17:25:04 -08001293 /**
1294 * Helper function to create traffic treatment builder for versatile forwarding objectives.
1295 *
1296 * @param fwd original forwarding objective
1297 * @return treatment builder for the flow rule, or null if there is an error.
1298 */
1299 protected TrafficTreatment.Builder versatileTreatmentBuilder(ForwardingObjective fwd) {
Saurav Das822c4e22015-10-23 10:51:11 -07001300 // XXX driver does not currently do type checking as per Tables 65-67 in
1301 // OFDPA 2.0 spec. The only allowed treatment is a punt to the controller.
Saurav Das49cb5a12016-01-16 22:54:07 -08001302 TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder();
1303 if (fwd.treatment() != null) {
1304 for (Instruction ins : fwd.treatment().allInstructions()) {
1305 if (ins instanceof OutputInstruction) {
1306 OutputInstruction o = (OutputInstruction) ins;
Ray Milkeyfe0e0852018-01-18 11:14:05 -08001307 if (PortNumber.CONTROLLER.equals(o.port())) {
Saurav Das49cb5a12016-01-16 22:54:07 -08001308 ttBuilder.add(o);
1309 } else {
1310 log.warn("Only allowed treatments in versatile forwarding "
1311 + "objectives are punts to the controller");
1312 }
Charles Chanf76de302018-06-15 18:54:18 -07001313 } else if (ins instanceof NoActionInstruction) {
1314 // No action is allowed and nothing needs to be done
Saurav Das49cb5a12016-01-16 22:54:07 -08001315 } else {
1316 log.warn("Cannot process instruction in versatile fwd {}", ins);
1317 }
Saurav Das822c4e22015-10-23 10:51:11 -07001318 }
Charles Chan2df0e8a2017-01-09 11:45:08 -08001319 if (fwd.treatment().clearedDeferred()) {
1320 ttBuilder.wipeDeferred();
1321 }
Saurav Das822c4e22015-10-23 10:51:11 -07001322 }
Saurav Das822c4e22015-10-23 10:51:11 -07001323 if (fwd.nextId() != null) {
Charles Chanab591602019-01-22 17:25:04 -08001324 // Override case
Saurav Das49cb5a12016-01-16 22:54:07 -08001325 NextGroup next = getGroupForNextObjective(fwd.nextId());
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +05301326 if (next == null) {
1327 fail(fwd, ObjectiveError.BADPARAMS);
Charles Chanab591602019-01-22 17:25:04 -08001328 return null;
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +05301329 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001330 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1331 // we only need the top level group's key to point the flow to it
1332 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1333 if (group == null) {
1334 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
Charles Chanab591602019-01-22 17:25:04 -08001335 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
Saurav Das49cb5a12016-01-16 22:54:07 -08001336 fail(fwd, ObjectiveError.GROUPMISSING);
Charles Chanab591602019-01-22 17:25:04 -08001337 return null;
Saurav Das49cb5a12016-01-16 22:54:07 -08001338 }
1339 ttBuilder.deferred().group(group.id());
Saurav Das822c4e22015-10-23 10:51:11 -07001340 }
Charles Chanab591602019-01-22 17:25:04 -08001341 return ttBuilder;
Saurav Das822c4e22015-10-23 10:51:11 -07001342 }
1343
1344 /**
1345 * In the OF-DPA 2.0 pipeline, specific forwarding refers to the IP table
Saurav Das8a0732e2015-11-20 15:27:53 -08001346 * (unicast or multicast) or the L2 table (mac + vlan) or the MPLS table.
Saurav Das822c4e22015-10-23 10:51:11 -07001347 *
1348 * @param fwd the forwarding objective of type 'specific'
1349 * @return a collection of flow rules. Typically there will be only one
1350 * for this type of forwarding objective. An empty set may be
1351 * returned if there is an issue in processing the objective.
1352 */
Charles Chan50d900c2018-03-02 13:26:22 -08001353 private Collection<FlowRule> processSpecific(ForwardingObjective fwd) {
Saurav Das25190812016-05-27 13:54:07 -07001354 log.debug("Processing specific fwd objective:{} in dev:{} with next:{}",
Saurav Das4ce45962015-11-24 23:21:05 -08001355 fwd.id(), deviceId, fwd.nextId());
1356 boolean isEthTypeObj = isSupportedEthTypeObjective(fwd);
1357 boolean isEthDstObj = isSupportedEthDstObjective(fwd);
1358
1359 if (isEthTypeObj) {
1360 return processEthTypeSpecific(fwd);
1361 } else if (isEthDstObj) {
1362 return processEthDstSpecific(fwd);
1363 } else {
1364 log.warn("processSpecific: Unsupported forwarding objective "
1365 + "criteria fwd:{} in dev:{}", fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001366 fail(fwd, ObjectiveError.UNSUPPORTED);
1367 return Collections.emptySet();
1368 }
Saurav Das4ce45962015-11-24 23:21:05 -08001369 }
1370
Saurav Das4ce45962015-11-24 23:21:05 -08001371 /**
1372 * Handles forwarding rules to the IP and MPLS tables.
1373 *
1374 * @param fwd the forwarding objective
1375 * @return A collection of flow rules, or an empty set
1376 */
1377 protected Collection<FlowRule> processEthTypeSpecific(ForwardingObjective fwd) {
Charles Chancad338a2016-09-16 18:03:11 -07001378 return processEthTypeSpecificInternal(fwd, false, ACL_TABLE);
Charles Chanf9e98652016-09-07 16:54:23 -07001379 }
1380
1381 /**
1382 * Internal implementation of processEthTypeSpecific.
1383 * <p>
1384 * Wildcarded IPv4_DST is not supported in OFDPA i12. Therefore, we break
1385 * the rule into 0.0.0.0/1 and 128.0.0.0/1.
1386 * The allowDefaultRoute must be set to false for OFDPA i12.
1387 * </p>
1388 *
1389 * @param fwd the forwarding objective
1390 * @param allowDefaultRoute allow wildcarded IPv4_DST or not
Ray Milkey0bb1e102016-11-10 14:51:27 -08001391 * @param mplsNextTable next MPLS table
Charles Chanf9e98652016-09-07 16:54:23 -07001392 * @return A collection of flow rules, or an empty set
1393 */
1394 protected Collection<FlowRule> processEthTypeSpecificInternal(ForwardingObjective fwd,
Charles Chancad338a2016-09-16 18:03:11 -07001395 boolean allowDefaultRoute,
1396 int mplsNextTable) {
Saurav Das4ce45962015-11-24 23:21:05 -08001397 TrafficSelector selector = fwd.selector();
1398 EthTypeCriterion ethType =
1399 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001400 boolean emptyGroup = false;
Charles Chan188ebf52015-12-23 00:15:11 -08001401 int forTableId;
Saurav Das8a0732e2015-11-20 15:27:53 -08001402 TrafficSelector.Builder filteredSelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -08001403 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
Flavio Castroe10fa242016-01-15 12:43:51 -08001404 TrafficSelector.Builder complementarySelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -08001405
Saurav Das8a0732e2015-11-20 15:27:53 -08001406 if (ethType.ethType().toShort() == Ethernet.TYPE_IPV4) {
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001407 if (buildIpv4Selector(filteredSelector, complementarySelector, fwd, allowDefaultRoute) < 0) {
1408 return Collections.emptyList();
1409 }
1410 // We need to set properly the next table
Flavio Castroe10fa242016-01-15 12:43:51 -08001411 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
Charles Chan5b9df8d2016-03-28 22:21:40 -07001412 if (ipv4Dst.isMulticast()) {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001413 forTableId = MULTICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -08001414 } else {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001415 forTableId = UNICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -08001416 }
Charles Chan50d900c2018-03-02 13:26:22 -08001417
Charles Chan236653d2018-03-05 11:28:23 -08001418 // TODO decrementing IP ttl is done automatically for routing, this
Charles Chan50d900c2018-03-02 13:26:22 -08001419 // action is ignored or rejected in ofdpa as it is not fully implemented
Charles Chan236653d2018-03-05 11:28:23 -08001420 // if (fwd.treatment() != null) {
1421 // for (Instruction instr : fwd.treatment().allInstructions()) {
1422 // if (instr instanceof L3ModificationInstruction &&
1423 // ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1424 // tb.deferred().add(instr);
1425 // }
1426 // }
1427 // }
1428
Pier Ventree0ae7a32016-11-23 09:57:42 -08001429 } else if (ethType.ethType().toShort() == Ethernet.TYPE_IPV6) {
1430 if (buildIpv6Selector(filteredSelector, fwd) < 0) {
1431 return Collections.emptyList();
1432 }
Kalhee Kim3d26bf02017-10-04 14:59:11 +00001433 //We need to set the proper next table
1434 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
1435 if (ipv6Dst.isMulticast()) {
1436 forTableId = MULTICAST_ROUTING_TABLE;
1437 } else {
1438 forTableId = UNICAST_ROUTING_TABLE;
1439 }
1440
Charles Chan236653d2018-03-05 11:28:23 -08001441 // TODO decrementing IP ttl is done automatically for routing, this
Charles Chan50d900c2018-03-02 13:26:22 -08001442 // action is ignored or rejected in ofdpa as it is not fully implemented
Charles Chan236653d2018-03-05 11:28:23 -08001443 // if (fwd.treatment() != null) {
1444 // for (Instruction instr : fwd.treatment().allInstructions()) {
1445 // if (instr instanceof L3ModificationInstruction &&
1446 // ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1447 // tb.deferred().add(instr);
1448 // }
1449 // }
1450 // }
Saurav Das8a0732e2015-11-20 15:27:53 -08001451 } else {
1452 filteredSelector
1453 .matchEthType(Ethernet.MPLS_UNICAST)
1454 .matchMplsLabel(((MplsCriterion)
1455 selector.getCriterion(Criterion.Type.MPLS_LABEL)).label());
1456 MplsBosCriterion bos = (MplsBosCriterion) selector
Pier Ventre140a8942016-11-02 07:26:38 -07001457 .getCriterion(MPLS_BOS);
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001458 if (bos != null && requireMplsBosMatch()) {
Saurav Das8a0732e2015-11-20 15:27:53 -08001459 filteredSelector.matchMplsBos(bos.mplsBos());
1460 }
1461 forTableId = MPLS_TABLE_1;
Saurav Das4ce45962015-11-24 23:21:05 -08001462 log.debug("processing MPLS specific forwarding objective {} -> next:{}"
1463 + " in dev {}", fwd.id(), fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001464
Charles Chan14967c22015-12-07 11:11:50 -08001465 if (fwd.treatment() != null) {
1466 for (Instruction instr : fwd.treatment().allInstructions()) {
1467 if (instr instanceof L2ModificationInstruction &&
1468 ((L2ModificationInstruction) instr).subtype() == L2SubType.MPLS_POP) {
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001469 // OF-DPA does not pop in MPLS table in some cases. For the L3 VPN, it requires
Saurav Das9c705342017-01-06 11:36:01 -08001470 // setting the MPLS_TYPE so pop can happen down the pipeline
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001471 if (requireMplsPop()) {
1472 if (mplsNextTable == MPLS_TYPE_TABLE && isNotMplsBos(selector)) {
1473 tb.immediate().popMpls();
1474 }
1475 } else {
1476 // Skip mpls pop action for mpls_unicast label
1477 if (instr instanceof ModMplsHeaderInstruction &&
1478 !((ModMplsHeaderInstruction) instr).ethernetType()
1479 .equals(EtherType.MPLS_UNICAST.ethType())) {
1480 tb.immediate().add(instr);
1481 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001482 }
Charles Chan14967c22015-12-07 11:11:50 -08001483 }
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001484
1485 if (requireMplsTtlModification()) {
1486 if (instr instanceof L3ModificationInstruction &&
1487 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1488 // FIXME Should modify the app to send the correct DEC_MPLS_TTL instruction
1489 tb.immediate().decMplsTtl();
1490 }
1491 if (instr instanceof L3ModificationInstruction &&
1492 ((L3ModificationInstruction) instr).subtype() == L3SubType.TTL_IN) {
1493 tb.immediate().add(instr);
1494 }
Charles Chan14967c22015-12-07 11:11:50 -08001495 }
Saurav Das8a0732e2015-11-20 15:27:53 -08001496 }
1497 }
1498 }
Saurav Das822c4e22015-10-23 10:51:11 -07001499
1500 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001501 NextGroup next = getGroupForNextObjective(fwd.nextId());
1502 if (next != null) {
1503 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1504 // we only need the top level group's key to point the flow to it
1505 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1506 if (group == null) {
1507 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1508 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1509 fail(fwd, ObjectiveError.GROUPMISSING);
1510 return Collections.emptySet();
1511 }
Saurav Dasa4020382018-02-14 14:14:54 -08001512 if (isNotMplsBos(selector) && group.type().equals(SELECT)) {
1513 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
1514 + "is not implemented in OF-DPA yet. Aborting flow {} -> next:{} "
1515 + "in this device {}", fwd.id(), fwd.nextId(), deviceId);
1516 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1517 return Collections.emptySet();
1518 }
Saurav Das423fe2b2015-12-04 10:52:59 -08001519 tb.deferred().group(group.id());
Saurav Dasc568c342018-01-25 09:49:01 -08001520 // retrying flows may be necessary due to bug CORD-554
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001521 if (gkeys.size() == 1 && gkeys.get(0).size() == 1) {
Saurav Dasc568c342018-01-25 09:49:01 -08001522 if (shouldRetry()) {
1523 log.warn("Found empty group 0x{} in dev:{} .. will retry fwd:{}",
1524 Integer.toHexString(group.id().id()), deviceId,
1525 fwd.id());
1526 emptyGroup = true;
1527 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001528 }
Saurav Das25190812016-05-27 13:54:07 -07001529 } else {
1530 log.warn("Cannot find group for nextId:{} in dev:{}. Aborting fwd:{}",
1531 fwd.nextId(), deviceId, fwd.id());
1532 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1533 return Collections.emptySet();
Saurav Das822c4e22015-10-23 10:51:11 -07001534 }
Saurav Das822c4e22015-10-23 10:51:11 -07001535 }
Charles Chancad338a2016-09-16 18:03:11 -07001536
1537 if (forTableId == MPLS_TABLE_1) {
Pier Ventre140a8942016-11-02 07:26:38 -07001538 if (mplsNextTable == MPLS_L3_TYPE_TABLE) {
Charles Chancad338a2016-09-16 18:03:11 -07001539 Ofdpa3SetMplsType setMplsType = new Ofdpa3SetMplsType(Ofdpa3MplsType.L3_PHP);
Saurav Das9c705342017-01-06 11:36:01 -08001540 // set mpls type as apply_action
1541 tb.immediate().extension(setMplsType, deviceId);
Charles Chancad338a2016-09-16 18:03:11 -07001542 }
1543 tb.transition(mplsNextTable);
1544 } else {
1545 tb.transition(ACL_TABLE);
1546 }
1547
Andrea Campanellad980c6d2018-04-30 11:48:55 +02001548 if (fwd.treatment() != null && fwd.treatment().clearedDeferred()) {
1549 if (supportsUnicastBlackHole()) {
1550 tb.wipeDeferred();
1551 } else {
1552 log.warn("Clear Deferred is not supported Unicast Routing Table on device {}", deviceId);
1553 return Collections.emptySet();
1554 }
1555 }
1556
Saurav Das822c4e22015-10-23 10:51:11 -07001557 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1558 .fromApp(fwd.appId())
1559 .withPriority(fwd.priority())
1560 .forDevice(deviceId)
Saurav Das8a0732e2015-11-20 15:27:53 -08001561 .withSelector(filteredSelector.build())
1562 .withTreatment(tb.build())
1563 .forTable(forTableId);
Saurav Das822c4e22015-10-23 10:51:11 -07001564
1565 if (fwd.permanent()) {
1566 ruleBuilder.makePermanent();
1567 } else {
1568 ruleBuilder.makeTemporary(fwd.timeout());
1569 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001570 Collection<FlowRule> flowRuleCollection = new ArrayList<>();
1571 flowRuleCollection.add(ruleBuilder.build());
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001572 if (!allowDefaultRoute) {
Pier Ventree0ae7a32016-11-23 09:57:42 -08001573 flowRuleCollection.add(
1574 defaultRoute(fwd, complementarySelector, forTableId, tb)
1575 );
Flavio Castroe10fa242016-01-15 12:43:51 -08001576 log.debug("Default rule 0.0.0.0/0 is being installed two rules");
1577 }
Saurav Dasc568c342018-01-25 09:49:01 -08001578
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001579 if (emptyGroup) {
pier8b3aef42019-03-11 15:14:02 -07001580 retryExecutorService.schedule(new RetryFlows(fwd, flowRuleCollection),
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001581 RETRY_MS, TimeUnit.MILLISECONDS);
1582 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001583 return flowRuleCollection;
Saurav Das822c4e22015-10-23 10:51:11 -07001584 }
1585
Charles Chan50d900c2018-03-02 13:26:22 -08001586 private int buildIpv4Selector(TrafficSelector.Builder builderToUpdate,
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001587 TrafficSelector.Builder extBuilder,
1588 ForwardingObjective fwd,
1589 boolean allowDefaultRoute) {
1590 TrafficSelector selector = fwd.selector();
1591
1592 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
1593 if (ipv4Dst.isMulticast()) {
1594 if (ipv4Dst.prefixLength() != 32) {
1595 log.warn("Multicast specific forwarding objective can only be /32");
1596 fail(fwd, ObjectiveError.BADPARAMS);
1597 return -1;
1598 }
1599 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
1600 if (assignedVlan == null) {
1601 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
1602 fail(fwd, ObjectiveError.BADPARAMS);
1603 return -1;
1604 }
Charles Chand1172632017-03-15 17:33:09 -07001605 if (requireVlanExtensions()) {
1606 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
1607 builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
1608 } else {
1609 builderToUpdate.matchVlanId(assignedVlan);
1610 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001611 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1612 log.debug("processing IPv4 multicast specific forwarding objective {} -> next:{}"
1613 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1614 } else {
1615 if (ipv4Dst.prefixLength() == 0) {
1616 if (allowDefaultRoute) {
1617 // The entire IPV4_DST field is wildcarded intentionally
1618 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4);
1619 } else {
1620 // NOTE: The switch does not support matching 0.0.0.0/0
1621 // Split it into 0.0.0.0/1 and 128.0.0.0/1
1622 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4)
1623 .matchIPDst(IpPrefix.valueOf("0.0.0.0/1"));
1624 extBuilder.matchEthType(Ethernet.TYPE_IPV4)
1625 .matchIPDst(IpPrefix.valueOf("128.0.0.0/1"));
1626 }
1627 } else {
1628 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1629 }
1630 log.debug("processing IPv4 unicast specific forwarding objective {} -> next:{}"
1631 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1632 }
1633 return 0;
1634 }
1635
1636 /**
1637 * Helper method to build Ipv6 selector using the selector provided by
1638 * a forwarding objective.
1639 *
1640 * @param builderToUpdate the builder to update
1641 * @param fwd the selector to read
1642 * @return 0 if the update ends correctly. -1 if the matches
1643 * are not yet supported
1644 */
Charles Chan50d900c2018-03-02 13:26:22 -08001645 int buildIpv6Selector(TrafficSelector.Builder builderToUpdate,
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001646 ForwardingObjective fwd) {
1647
1648 TrafficSelector selector = fwd.selector();
1649
1650 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
1651 if (ipv6Dst.isMulticast()) {
Julia Ferguson65428c32017-08-10 18:15:24 +00001652 if (ipv6Dst.prefixLength() != IpAddress.INET6_BIT_LENGTH) {
1653 log.warn("Multicast specific forwarding objective can only be /128");
1654 fail(fwd, ObjectiveError.BADPARAMS);
1655 return -1;
1656 }
1657 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
1658 if (assignedVlan == null) {
1659 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
1660 fail(fwd, ObjectiveError.BADPARAMS);
1661 return -1;
1662 }
1663 if (requireVlanExtensions()) {
1664 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
1665 builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
1666 } else {
1667 builderToUpdate.matchVlanId(assignedVlan);
1668 }
1669 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6).matchIPv6Dst(ipv6Dst);
1670 log.debug("processing IPv6 multicast specific forwarding objective {} -> next:{}"
1671 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1672 } else {
1673 if (ipv6Dst.prefixLength() != 0) {
1674 builderToUpdate.matchIPv6Dst(ipv6Dst);
1675 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001676 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6);
1677 log.debug("processing IPv6 unicast specific forwarding objective {} -> next:{}"
1678 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Julia Ferguson65428c32017-08-10 18:15:24 +00001679 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001680 return 0;
1681 }
1682
Charles Chan50d900c2018-03-02 13:26:22 -08001683 FlowRule defaultRoute(ForwardingObjective fwd,
Pier Ventree0ae7a32016-11-23 09:57:42 -08001684 TrafficSelector.Builder complementarySelector,
1685 int forTableId,
1686 TrafficTreatment.Builder tb) {
1687 FlowRule.Builder rule = DefaultFlowRule.builder()
1688 .fromApp(fwd.appId())
1689 .withPriority(fwd.priority())
1690 .forDevice(deviceId)
1691 .withSelector(complementarySelector.build())
1692 .withTreatment(tb.build())
1693 .forTable(forTableId);
1694 if (fwd.permanent()) {
1695 rule.makePermanent();
1696 } else {
1697 rule.makeTemporary(fwd.timeout());
1698 }
1699 return rule.build();
1700 }
1701
Saurav Das4ce45962015-11-24 23:21:05 -08001702 /**
1703 * Handles forwarding rules to the L2 bridging table. Flow actions are not
1704 * allowed in the bridging table - instead we use L2 Interface group or
1705 * L2 flood group
1706 *
1707 * @param fwd the forwarding objective
1708 * @return A collection of flow rules, or an empty set
1709 */
1710 protected Collection<FlowRule> processEthDstSpecific(ForwardingObjective fwd) {
1711 List<FlowRule> rules = new ArrayList<>();
1712
1713 // Build filtered selector
1714 TrafficSelector selector = fwd.selector();
1715 EthCriterion ethCriterion = (EthCriterion) selector
1716 .getCriterion(Criterion.Type.ETH_DST);
1717 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) selector
1718 .getCriterion(Criterion.Type.VLAN_VID);
1719
1720 if (vlanIdCriterion == null) {
1721 log.warn("Forwarding objective for bridging requires vlan. Not "
1722 + "installing fwd:{} in dev:{}", fwd.id(), deviceId);
1723 fail(fwd, ObjectiveError.BADPARAMS);
1724 return Collections.emptySet();
1725 }
1726
1727 TrafficSelector.Builder filteredSelectorBuilder =
1728 DefaultTrafficSelector.builder();
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001729
1730 if (!ethCriterion.mac().equals(NONE) &&
1731 !ethCriterion.mac().equals(BROADCAST)) {
Saurav Das4ce45962015-11-24 23:21:05 -08001732 filteredSelectorBuilder.matchEthDst(ethCriterion.mac());
1733 log.debug("processing L2 forwarding objective:{} -> next:{} in dev:{}",
1734 fwd.id(), fwd.nextId(), deviceId);
1735 } else {
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001736 // Use wildcard DST_MAC if the MacAddress is None or Broadcast
Saurav Das4ce45962015-11-24 23:21:05 -08001737 log.debug("processing L2 Broadcast forwarding objective:{} -> next:{} "
1738 + "in dev:{} for vlan:{}",
1739 fwd.id(), fwd.nextId(), deviceId, vlanIdCriterion.vlanId());
1740 }
Charles Chand1172632017-03-15 17:33:09 -07001741 if (requireVlanExtensions()) {
1742 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanIdCriterion.vlanId());
1743 filteredSelectorBuilder.extension(ofdpaMatchVlanVid, deviceId);
1744 } else {
1745 filteredSelectorBuilder.matchVlanId(vlanIdCriterion.vlanId());
1746 }
Saurav Das4ce45962015-11-24 23:21:05 -08001747 TrafficSelector filteredSelector = filteredSelectorBuilder.build();
1748
1749 if (fwd.treatment() != null) {
1750 log.warn("Ignoring traffic treatment in fwd rule {} meant for L2 table"
1751 + "for dev:{}. Expecting only nextId", fwd.id(), deviceId);
1752 }
1753
1754 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1755 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001756 NextGroup next = getGroupForNextObjective(fwd.nextId());
Saurav Das4ce45962015-11-24 23:21:05 -08001757 if (next != null) {
1758 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1759 // we only need the top level group's key to point the flow to it
1760 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1761 if (group != null) {
1762 treatmentBuilder.deferred().group(group.id());
1763 } else {
1764 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1765 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1766 fail(fwd, ObjectiveError.GROUPMISSING);
1767 return Collections.emptySet();
1768 }
1769 }
1770 }
1771 treatmentBuilder.immediate().transition(ACL_TABLE);
1772 TrafficTreatment filteredTreatment = treatmentBuilder.build();
1773
1774 // Build bridging table entries
1775 FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder();
1776 flowRuleBuilder.fromApp(fwd.appId())
1777 .withPriority(fwd.priority())
1778 .forDevice(deviceId)
1779 .withSelector(filteredSelector)
1780 .withTreatment(filteredTreatment)
1781 .forTable(BRIDGING_TABLE);
1782 if (fwd.permanent()) {
1783 flowRuleBuilder.makePermanent();
1784 } else {
1785 flowRuleBuilder.makeTemporary(fwd.timeout());
1786 }
1787 rules.add(flowRuleBuilder.build());
1788 return rules;
1789 }
1790
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001791 //////////////////////////////////////
1792 // Helper Methods and Classes
1793 //////////////////////////////////////
1794
1795 private boolean isSupportedEthTypeObjective(ForwardingObjective fwd) {
1796 TrafficSelector selector = fwd.selector();
1797 EthTypeCriterion ethType = (EthTypeCriterion) selector
1798 .getCriterion(Criterion.Type.ETH_TYPE);
1799 return !((ethType == null) ||
1800 ((ethType.ethType().toShort() != Ethernet.TYPE_IPV4) &&
Pier Ventree0ae7a32016-11-23 09:57:42 -08001801 (ethType.ethType().toShort() != Ethernet.MPLS_UNICAST)) &&
1802 (ethType.ethType().toShort() != Ethernet.TYPE_IPV6));
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001803 }
1804
1805 private boolean isSupportedEthDstObjective(ForwardingObjective fwd) {
1806 TrafficSelector selector = fwd.selector();
1807 EthCriterion ethDst = (EthCriterion) selector
1808 .getCriterion(Criterion.Type.ETH_DST);
1809 VlanIdCriterion vlanId = (VlanIdCriterion) selector
1810 .getCriterion(Criterion.Type.VLAN_VID);
1811 return !(ethDst == null && vlanId == null);
1812 }
1813
Charles Chan50d900c2018-03-02 13:26:22 -08001814 NextGroup getGroupForNextObjective(Integer nextId) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001815 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
1816 if (next != null) {
1817 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1818 if (gkeys != null && !gkeys.isEmpty()) {
1819 return next;
1820 } else {
1821 log.warn("Empty next group found in FlowObjective store for "
1822 + "next-id:{} in dev:{}", nextId, deviceId);
1823 }
1824 } else {
1825 log.warn("next-id {} not found in Flow objective store for dev:{}",
1826 nextId, deviceId);
1827 }
1828 return null;
1829 }
1830
Charles Chan188ebf52015-12-23 00:15:11 -08001831 protected static void pass(Objective obj) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001832 obj.context().ifPresent(context -> context.onSuccess(obj));
Saurav Das822c4e22015-10-23 10:51:11 -07001833 }
1834
Charles Chan188ebf52015-12-23 00:15:11 -08001835 protected static void fail(Objective obj, ObjectiveError error) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001836 obj.context().ifPresent(context -> context.onError(obj, error));
Saurav Das822c4e22015-10-23 10:51:11 -07001837 }
Saurav Das24431192016-03-07 19:13:00 -08001838
Saurav Das24431192016-03-07 19:13:00 -08001839 @Override
1840 public List<String> getNextMappings(NextGroup nextGroup) {
1841 List<String> mappings = new ArrayList<>();
1842 List<Deque<GroupKey>> gkeys = appKryo.deserialize(nextGroup.data());
1843 for (Deque<GroupKey> gkd : gkeys) {
1844 Group lastGroup = null;
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -07001845 StringBuilder gchain = new StringBuilder();
Saurav Das24431192016-03-07 19:13:00 -08001846 for (GroupKey gk : gkd) {
1847 Group g = groupService.getGroup(deviceId, gk);
Saurav Das8be4e3a2016-03-11 17:19:07 -08001848 if (g == null) {
Saurav Das25190812016-05-27 13:54:07 -07001849 gchain.append(" NoGrp").append(" -->");
Saurav Das8be4e3a2016-03-11 17:19:07 -08001850 continue;
1851 }
1852 gchain.append(" 0x").append(Integer.toHexString(g.id().id()))
1853 .append(" -->");
Saurav Das24431192016-03-07 19:13:00 -08001854 lastGroup = g;
1855 }
1856 // add port information for last group in group-chain
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -07001857 List<Instruction> lastGroupIns = new ArrayList<>();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001858 if (lastGroup != null && !lastGroup.buckets().buckets().isEmpty()) {
Saurav Das25190812016-05-27 13:54:07 -07001859 lastGroupIns = lastGroup.buckets().buckets().get(0)
1860 .treatment().allInstructions();
1861 }
1862 for (Instruction i: lastGroupIns) {
Saurav Das24431192016-03-07 19:13:00 -08001863 if (i instanceof OutputInstruction) {
Saurav Das8be4e3a2016-03-11 17:19:07 -08001864 gchain.append(" port:").append(((OutputInstruction) i).port());
Saurav Das24431192016-03-07 19:13:00 -08001865 }
1866 }
Saurav Das8be4e3a2016-03-11 17:19:07 -08001867 mappings.add(gchain.toString());
Saurav Das24431192016-03-07 19:13:00 -08001868 }
1869 return mappings;
1870 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001871
Saurav Dasa4020382018-02-14 14:14:54 -08001872 /**
1873 * Returns true iff the given selector matches on BOS==true, indicating that
1874 * the selector is trying to match on a label that is bottom-of-stack.
1875 *
1876 * @param selector the given match
1877 * @return true iff BoS==true; false if BOS==false, or BOS matching is not
1878 * expressed in the given selector
1879 */
Pier Ventre140a8942016-11-02 07:26:38 -07001880 static boolean isMplsBos(TrafficSelector selector) {
1881 MplsBosCriterion bosCriterion = (MplsBosCriterion) selector.getCriterion(MPLS_BOS);
1882 return bosCriterion != null && bosCriterion.mplsBos();
1883 }
1884
Saurav Dasa4020382018-02-14 14:14:54 -08001885 /**
1886 * Returns true iff the given selector matches on BOS==false, indicating
1887 * that the selector is trying to match on a label that is not the
1888 * bottom-of-stack label.
1889 *
1890 * @param selector the given match
1891 * @return true iff BoS==false;
1892 * false if BOS==true, or BOS matching is not expressed in the given selector
1893 */
Pier Ventre140a8942016-11-02 07:26:38 -07001894 static boolean isNotMplsBos(TrafficSelector selector) {
1895 MplsBosCriterion bosCriterion = (MplsBosCriterion) selector.getCriterion(MPLS_BOS);
1896 return bosCriterion != null && !bosCriterion.mplsBos();
1897 }
1898
Saurav Dasa4020382018-02-14 14:14:54 -08001899 /**
1900 * Returns true iff the forwarding objective includes a treatment to pop the
1901 * MPLS label.
1902 *
1903 * @param fwd the given forwarding objective
1904 * @return true iff mpls pop treatment exists
1905 */
1906 static boolean isMplsPop(ForwardingObjective fwd) {
1907 if (fwd.treatment() != null) {
1908 for (Instruction instr : fwd.treatment().allInstructions()) {
1909 if (instr instanceof L2ModificationInstruction
1910 && ((L2ModificationInstruction) instr)
1911 .subtype() == L2SubType.MPLS_POP) {
1912 return true;
1913 }
1914 }
1915 }
1916 return false;
1917 }
1918
Charles Chand9e47c62017-10-05 15:17:15 -07001919 private static boolean isIpv6(TrafficSelector selector) {
1920 EthTypeCriterion ethTypeCriterion = (EthTypeCriterion) selector.getCriterion(ETH_TYPE);
1921 return ethTypeCriterion != null && ethTypeCriterion.ethType().toShort() == Ethernet.TYPE_IPV6;
1922 }
1923
Charles Chan50d900c2018-03-02 13:26:22 -08001924 static VlanId readVlanFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001925 if (selector == null) {
1926 return null;
1927 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001928 Criterion criterion = selector.getCriterion(Criterion.Type.VLAN_VID);
1929 return (criterion == null)
1930 ? null : ((VlanIdCriterion) criterion).vlanId();
1931 }
1932
jayakumarthazhath655b9a82018-10-01 00:51:54 +05301933 static MacAddress readEthDstFromSelector(TrafficSelector selector) {
1934 if (selector == null) {
1935 return null;
1936 }
1937 Criterion criterion = selector.getCriterion(Criterion.Type.ETH_DST);
1938 return (criterion == null)
1939 ? null : ((EthCriterion) criterion).mac();
1940 }
1941
Charles Chand9e47c62017-10-05 15:17:15 -07001942 static IpPrefix readIpDstFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001943 if (selector == null) {
1944 return null;
1945 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001946 Criterion criterion = selector.getCriterion(Criterion.Type.IPV4_DST);
1947 return (criterion == null) ? null : ((IPCriterion) criterion).ip();
1948 }
Charles Chand55e84d2016-03-30 17:54:24 -07001949
1950 private static VlanId readVlanFromTreatment(TrafficTreatment treatment) {
Saurav Das59232cf2016-04-27 18:35:50 -07001951 if (treatment == null) {
1952 return null;
1953 }
Charles Chand55e84d2016-03-30 17:54:24 -07001954 for (Instruction i : treatment.allInstructions()) {
1955 if (i instanceof ModVlanIdInstruction) {
1956 return ((ModVlanIdInstruction) i).vlanId();
1957 }
1958 }
1959 return null;
1960 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001961
Jonghwan Hyun800d9d02018-04-09 09:40:50 -07001962 protected static MacAddress readEthDstFromTreatment(TrafficTreatment treatment) {
Charles Chan45b69ab2018-03-02 15:41:41 -08001963 if (treatment == null) {
1964 return null;
1965 }
1966 for (Instruction i : treatment.allInstructions()) {
1967 if (i instanceof ModEtherInstruction) {
1968 ModEtherInstruction modEtherInstruction = (ModEtherInstruction) i;
1969 if (modEtherInstruction.subtype() == L2SubType.ETH_DST) {
1970 return modEtherInstruction.mac();
1971 }
1972 }
1973 }
1974 return null;
1975 }
1976
jayakumarthazhath655b9a82018-10-01 00:51:54 +05301977 static ExtensionSelector readExtensionFromSelector(TrafficSelector selector) {
1978 if (selector == null) {
1979 return null;
1980 }
1981 ExtensionCriterion criterion = (ExtensionCriterion) selector.getCriterion(Criterion.Type.EXTENSION);
1982 return (criterion == null) ? null : criterion.extensionSelector();
1983 }
1984
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001985 /**
1986 * Utility class that retries sending flows a fixed number of times, even if
1987 * some of the attempts are successful. Used only for forwarding objectives.
1988 */
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001989 public final class RetryFlows implements Runnable {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001990 int attempts = MAX_RETRY_ATTEMPTS;
1991 private Collection<FlowRule> retryFlows;
1992 private ForwardingObjective fwd;
1993
Charles Chan50d900c2018-03-02 13:26:22 -08001994 RetryFlows(ForwardingObjective fwd, Collection<FlowRule> retryFlows) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001995 this.fwd = fwd;
1996 this.retryFlows = retryFlows;
1997 }
1998
1999 @Override
2000 public void run() {
2001 log.info("RETRY FLOWS ATTEMPT# {} for fwd:{} rules:{}",
2002 MAX_RETRY_ATTEMPTS - attempts, fwd.id(), retryFlows.size());
pier8b3aef42019-03-11 15:14:02 -07002003 sendForwards(Collections.singletonList(Pair.of(fwd, retryFlows)));
Saurav Das1ce0a7b2016-10-21 14:06:29 -07002004 if (--attempts > 0) {
pier8b3aef42019-03-11 15:14:02 -07002005 retryExecutorService.schedule(this, RETRY_MS, TimeUnit.MILLISECONDS);
2006 }
2007 }
2008 }
2009
2010 // Flow rules accumulator for reducing the number of transactions required to the devices.
2011 private final class ForwardingObjectiveAccumulator
2012 extends AbstractAccumulator<Pair<ForwardingObjective, Collection<FlowRule>>> {
2013
2014 ForwardingObjectiveAccumulator(int maxFwd, int maxBatchMS, int maxIdleMS) {
2015 super(TIMER, maxFwd, maxBatchMS, maxIdleMS);
2016 }
2017
2018 @Override
2019 public void processItems(List<Pair<ForwardingObjective, Collection<FlowRule>>> pairs) {
2020 // Triggers creation of a batch using the list of flowrules generated from fwdobjs.
2021 accumulatorExecutorService.execute(new FlowRulesBuilderTask(pairs));
2022 }
2023 }
2024
2025 // Task for building batch of flow rules in a separate thread.
2026 private final class FlowRulesBuilderTask implements Runnable {
2027 private final List<Pair<ForwardingObjective, Collection<FlowRule>>> pairs;
2028
2029 FlowRulesBuilderTask(List<Pair<ForwardingObjective, Collection<FlowRule>>> pairs) {
2030 this.pairs = pairs;
2031 }
2032
2033 @Override
2034 public void run() {
2035 try {
2036 sendForwards(pairs);
2037 } catch (Exception e) {
2038 log.warn("Unable to send forwards", e);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07002039 }
2040 }
2041 }
2042
Saurav Das822c4e22015-10-23 10:51:11 -07002043}