blob: 04d581adcf2070906c5621e15070bb7ae1d60bff [file] [log] [blame]
Saurav Das822c4e22015-10-23 10:51:11 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Saurav Das822c4e22015-10-23 10:51:11 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Yi Tsengef19de12017-04-24 11:33:05 -070016package org.onosproject.driver.pipeline.ofdpa;
Saurav Das822c4e22015-10-23 10:51:11 -070017
Charles Chan5270ed02016-01-30 23:22:37 -080018import com.google.common.collect.ImmutableList;
Yi Tseng47f82dc2017-03-05 22:48:39 -080019import com.google.common.collect.Sets;
Saurav Das822c4e22015-10-23 10:51:11 -070020import org.onlab.osgi.ServiceDirectory;
Jonghwan Hyun800d9d02018-04-09 09:40:50 -070021import org.onlab.packet.EthType;
Saurav Das822c4e22015-10-23 10:51:11 -070022import org.onlab.packet.Ethernet;
Alex Yashchuk4caa8e82017-12-08 17:40:05 +020023import org.onlab.packet.EthType.EtherType;
Julia Ferguson65428c32017-08-10 18:15:24 +000024import org.onlab.packet.IpAddress;
Charles Chan14967c22015-12-07 11:11:50 -080025import org.onlab.packet.IpPrefix;
Charles Chan45b69ab2018-03-02 15:41:41 -080026import org.onlab.packet.MacAddress;
Saurav Das822c4e22015-10-23 10:51:11 -070027import org.onlab.packet.VlanId;
28import org.onlab.util.KryoNamespace;
29import org.onosproject.core.ApplicationId;
30import org.onosproject.core.CoreService;
Jonghwan Hyun800d9d02018-04-09 09:40:50 -070031import org.onosproject.driver.extensions.Ofdpa3CopyField;
Charles Chancad338a2016-09-16 18:03:11 -070032import org.onosproject.driver.extensions.Ofdpa3MplsType;
33import org.onosproject.driver.extensions.Ofdpa3SetMplsType;
Jonghwan Hyun800d9d02018-04-09 09:40:50 -070034import org.onosproject.driver.extensions.OfdpaMatchActsetOutput;
35import org.onosproject.driver.extensions.OfdpaMatchAllowVlanTranslation;
Charles Chan14967c22015-12-07 11:11:50 -080036import org.onosproject.driver.extensions.OfdpaMatchVlanVid;
37import org.onosproject.driver.extensions.OfdpaSetVlanVid;
Saurav Das822c4e22015-10-23 10:51:11 -070038import org.onosproject.net.DeviceId;
39import org.onosproject.net.Port;
40import org.onosproject.net.PortNumber;
41import org.onosproject.net.behaviour.NextGroup;
42import org.onosproject.net.behaviour.Pipeliner;
43import org.onosproject.net.behaviour.PipelinerContext;
44import org.onosproject.net.device.DeviceService;
45import org.onosproject.net.driver.AbstractHandlerBehaviour;
46import org.onosproject.net.flow.DefaultFlowRule;
47import org.onosproject.net.flow.DefaultTrafficSelector;
48import org.onosproject.net.flow.DefaultTrafficTreatment;
49import org.onosproject.net.flow.FlowRule;
50import org.onosproject.net.flow.FlowRuleOperations;
51import org.onosproject.net.flow.FlowRuleOperationsContext;
52import org.onosproject.net.flow.FlowRuleService;
53import org.onosproject.net.flow.TrafficSelector;
54import org.onosproject.net.flow.TrafficTreatment;
55import org.onosproject.net.flow.criteria.Criteria;
56import org.onosproject.net.flow.criteria.Criterion;
57import org.onosproject.net.flow.criteria.EthCriterion;
58import org.onosproject.net.flow.criteria.EthTypeCriterion;
59import org.onosproject.net.flow.criteria.IPCriterion;
Pier Ventree0ae7a32016-11-23 09:57:42 -080060import org.onosproject.net.flow.criteria.Icmpv6CodeCriterion;
61import org.onosproject.net.flow.criteria.Icmpv6TypeCriterion;
Saurav Das8a0732e2015-11-20 15:27:53 -080062import org.onosproject.net.flow.criteria.MplsBosCriterion;
63import org.onosproject.net.flow.criteria.MplsCriterion;
Saurav Das822c4e22015-10-23 10:51:11 -070064import org.onosproject.net.flow.criteria.PortCriterion;
Charles Chand9e47c62017-10-05 15:17:15 -070065import org.onosproject.net.flow.criteria.TcpPortCriterion;
66import org.onosproject.net.flow.criteria.UdpPortCriterion;
Saurav Das822c4e22015-10-23 10:51:11 -070067import org.onosproject.net.flow.criteria.VlanIdCriterion;
68import org.onosproject.net.flow.instructions.Instruction;
69import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
70import org.onosproject.net.flow.instructions.L2ModificationInstruction;
Saurav Das8a0732e2015-11-20 15:27:53 -080071import org.onosproject.net.flow.instructions.L2ModificationInstruction.L2SubType;
Saurav Das822c4e22015-10-23 10:51:11 -070072import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanIdInstruction;
Charles Chan45b69ab2018-03-02 15:41:41 -080073import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModEtherInstruction;
Alex Yashchuk4caa8e82017-12-08 17:40:05 +020074import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModMplsHeaderInstruction;
Charles Chan14967c22015-12-07 11:11:50 -080075import org.onosproject.net.flow.instructions.L3ModificationInstruction;
76import org.onosproject.net.flow.instructions.L3ModificationInstruction.L3SubType;
Saurav Das822c4e22015-10-23 10:51:11 -070077import org.onosproject.net.flowobjective.FilteringObjective;
78import org.onosproject.net.flowobjective.FlowObjectiveStore;
79import org.onosproject.net.flowobjective.ForwardingObjective;
80import org.onosproject.net.flowobjective.NextObjective;
81import org.onosproject.net.flowobjective.Objective;
82import org.onosproject.net.flowobjective.ObjectiveError;
Saurav Das822c4e22015-10-23 10:51:11 -070083import org.onosproject.net.group.DefaultGroupKey;
84import org.onosproject.net.group.Group;
Saurav Das822c4e22015-10-23 10:51:11 -070085import org.onosproject.net.group.GroupKey;
Saurav Das822c4e22015-10-23 10:51:11 -070086import org.onosproject.net.group.GroupService;
Saurav Das822c4e22015-10-23 10:51:11 -070087import org.onosproject.store.serializers.KryoNamespaces;
88import org.slf4j.Logger;
89
Pier Ventree0ae7a32016-11-23 09:57:42 -080090import java.util.ArrayDeque;
91import java.util.ArrayList;
92import java.util.Collection;
93import java.util.Collections;
94import java.util.Deque;
95import java.util.List;
96import java.util.Objects;
Jonghwan Hyun800d9d02018-04-09 09:40:50 -070097import java.util.Optional;
Pier Ventree0ae7a32016-11-23 09:57:42 -080098import java.util.concurrent.ScheduledExecutorService;
99import java.util.concurrent.TimeUnit;
100
101import static java.util.concurrent.Executors.newScheduledThreadPool;
Yi Tseng3a77b4f2017-02-06 15:02:17 -0800102import static org.onlab.packet.MacAddress.BROADCAST;
Charles Chanb4879a52017-10-20 19:09:16 -0700103import static org.onlab.packet.MacAddress.IPV4_MULTICAST;
104import static org.onlab.packet.MacAddress.IPV6_MULTICAST;
Yi Tseng3a77b4f2017-02-06 15:02:17 -0800105import static org.onlab.packet.MacAddress.NONE;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800106import static org.onlab.util.Tools.groupedThreads;
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700107import static org.onosproject.driver.extensions.Ofdpa3CopyField.OXM_ID_PACKET_REG_1;
108import static org.onosproject.driver.extensions.Ofdpa3CopyField.OXM_ID_VLAN_VID;
Yi Tsengef19de12017-04-24 11:33:05 -0700109import static org.onosproject.driver.pipeline.ofdpa.OfdpaGroupHandlerUtility.*;
Charles Chand9e47c62017-10-05 15:17:15 -0700110import static org.onosproject.net.flow.criteria.Criterion.Type.ETH_TYPE;
Pier Luigi075f1012018-02-01 10:23:12 +0100111import static org.onosproject.net.group.GroupDescription.Type.SELECT;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800112import static org.slf4j.LoggerFactory.getLogger;
Pier Ventre140a8942016-11-02 07:26:38 -0700113import static org.onosproject.net.flow.criteria.Criterion.Type.MPLS_BOS;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800114
Saurav Das822c4e22015-10-23 10:51:11 -0700115/**
116 * Driver for Broadcom's OF-DPA v2.0 TTP.
Saurav Das822c4e22015-10-23 10:51:11 -0700117 */
Charles Chan361154b2016-03-24 10:23:39 -0700118public class Ofdpa2Pipeline extends AbstractHandlerBehaviour implements Pipeliner {
Saurav Das822c4e22015-10-23 10:51:11 -0700119 protected static final int PORT_TABLE = 0;
120 protected static final int VLAN_TABLE = 10;
Pier Ventre42287df2016-11-09 14:17:26 -0800121 protected static final int VLAN_1_TABLE = 11;
122 protected static final int MPLS_L2_PORT_FLOW_TABLE = 13;
123 protected static final int MPLS_L2_PORT_PCP_TRUST_FLOW_TABLE = 16;
Saurav Das822c4e22015-10-23 10:51:11 -0700124 protected static final int TMAC_TABLE = 20;
125 protected static final int UNICAST_ROUTING_TABLE = 30;
126 protected static final int MULTICAST_ROUTING_TABLE = 40;
127 protected static final int MPLS_TABLE_0 = 23;
128 protected static final int MPLS_TABLE_1 = 24;
Pier Ventre140a8942016-11-02 07:26:38 -0700129 protected static final int MPLS_L3_TYPE_TABLE = 27;
130 protected static final int MPLS_TYPE_TABLE = 29;
Saurav Das822c4e22015-10-23 10:51:11 -0700131 protected static final int BRIDGING_TABLE = 50;
132 protected static final int ACL_TABLE = 60;
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700133 protected static final int EGRESS_VLAN_FLOW_TABLE = 210;
134 protected static final int EGRESS_DSCP_PCP_REMARK_FLOW_TABLE = 230;
135 protected static final int EGRESS_TPID_FLOW_TABLE = 235;
Saurav Das822c4e22015-10-23 10:51:11 -0700136 protected static final int MAC_LEARNING_TABLE = 254;
137 protected static final long OFPP_MAX = 0xffffff00L;
138
Saurav Das52025962016-01-28 22:30:01 -0800139 protected static final int HIGHEST_PRIORITY = 0xffff;
Saurav Das2857f382015-11-03 14:39:27 -0800140 protected static final int DEFAULT_PRIORITY = 0x8000;
Saurav Das822c4e22015-10-23 10:51:11 -0700141 protected static final int LOWEST_PRIORITY = 0x0;
142
Pier Ventre42287df2016-11-09 14:17:26 -0800143 protected static final int MPLS_L2_PORT_PRIORITY = 2;
Pier Ventre42287df2016-11-09 14:17:26 -0800144 protected static final int MPLS_TUNNEL_ID_BASE = 0x10000;
145 protected static final int MPLS_TUNNEL_ID_MAX = 0x1FFFF;
146
Pier Ventre70d53ba2016-11-17 22:26:29 -0800147 protected static final int MPLS_UNI_PORT_MAX = 0x0000FFFF;
Pier Ventre70d53ba2016-11-17 22:26:29 -0800148 protected static final int MPLS_NNI_PORT_BASE = 0x00020000;
149 protected static final int MPLS_NNI_PORT_MAX = 0x0002FFFF;
150
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700151 protected static final short ALLOW_VLAN_TRANSLATION = 1;
152 protected static final int COPY_FIELD_NBITS = 12;
153 protected static final int COPY_FIELD_OFFSET = 0;
154
Saurav Das822c4e22015-10-23 10:51:11 -0700155 private final Logger log = getLogger(getClass());
Charles Chan425854b2016-04-11 15:32:12 -0700156 protected ServiceDirectory serviceDirectory;
Saurav Das822c4e22015-10-23 10:51:11 -0700157 protected FlowRuleService flowRuleService;
Charles Chan425854b2016-04-11 15:32:12 -0700158 protected CoreService coreService;
Saurav Das8a0732e2015-11-20 15:27:53 -0800159 protected GroupService groupService;
160 protected FlowObjectiveStore flowObjectiveStore;
Saurav Das822c4e22015-10-23 10:51:11 -0700161 protected DeviceId deviceId;
162 protected ApplicationId driverId;
Saurav Das822c4e22015-10-23 10:51:11 -0700163 protected DeviceService deviceService;
Charles Chan188ebf52015-12-23 00:15:11 -0800164 protected static KryoNamespace appKryo = new KryoNamespace.Builder()
Yi Tsengef19de12017-04-24 11:33:05 -0700165 .register(KryoNamespaces.API)
166 .register(GroupKey.class)
167 .register(DefaultGroupKey.class)
168 .register(OfdpaNextGroup.class)
169 .register(ArrayDeque.class)
170 .build("Ofdpa2Pipeline");
Saurav Das822c4e22015-10-23 10:51:11 -0700171
Charles Chan425854b2016-04-11 15:32:12 -0700172 protected Ofdpa2GroupHandler groupHandler;
Saurav Das822c4e22015-10-23 10:51:11 -0700173
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700174 // flows installations to be retried
Charles Chan50d900c2018-03-02 13:26:22 -0800175 private ScheduledExecutorService executorService
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700176 = newScheduledThreadPool(5, groupedThreads("OfdpaPipeliner", "retry-%d", log));
Charles Chan50d900c2018-03-02 13:26:22 -0800177 private static final int MAX_RETRY_ATTEMPTS = 10;
178 private static final int RETRY_MS = 1000;
Saurav Das4f980082015-11-05 13:39:15 -0800179
Saurav Das822c4e22015-10-23 10:51:11 -0700180 @Override
181 public void init(DeviceId deviceId, PipelinerContext context) {
Saurav Das822c4e22015-10-23 10:51:11 -0700182 this.deviceId = deviceId;
183
Charles Chan425854b2016-04-11 15:32:12 -0700184 serviceDirectory = context.directory();
Saurav Das822c4e22015-10-23 10:51:11 -0700185 coreService = serviceDirectory.get(CoreService.class);
186 flowRuleService = serviceDirectory.get(FlowRuleService.class);
187 groupService = serviceDirectory.get(GroupService.class);
188 flowObjectiveStore = context.store();
Saurav Das822c4e22015-10-23 10:51:11 -0700189 deviceService = serviceDirectory.get(DeviceService.class);
Saurav Das822c4e22015-10-23 10:51:11 -0700190
Charles Chan40132b32017-01-22 00:19:37 -0800191 initDriverId();
192 initGroupHander(context);
Saurav Das822c4e22015-10-23 10:51:11 -0700193
Saurav Das822c4e22015-10-23 10:51:11 -0700194 initializePipeline();
Saurav Das822c4e22015-10-23 10:51:11 -0700195 }
196
Charles Chan40132b32017-01-22 00:19:37 -0800197 protected void initDriverId() {
198 driverId = coreService.registerApplication(
199 "org.onosproject.driver.Ofdpa2Pipeline");
200 }
201
202 protected void initGroupHander(PipelinerContext context) {
203 groupHandler = new Ofdpa2GroupHandler();
204 groupHandler.init(deviceId, context);
205 }
206
Saurav Das822c4e22015-10-23 10:51:11 -0700207 protected void initializePipeline() {
Charles Chan188ebf52015-12-23 00:15:11 -0800208 // OF-DPA does not require initializing the pipeline as it puts default
209 // rules automatically in the hardware. However emulation of OFDPA in
210 // software switches does require table-miss-entries.
Saurav Das822c4e22015-10-23 10:51:11 -0700211 }
212
Charles Chand1172632017-03-15 17:33:09 -0700213 /**
Alex Yashchuk4caa8e82017-12-08 17:40:05 +0200214 * Determines whether this pipeline requires MPLS POP instruction.
215 *
216 * @return true to use MPLS POP instruction
217 */
218 public boolean requireMplsPop() {
219 return true;
220 }
221
222 /**
223 * Determines whether this pipeline requires MPLS BOS match.
224 *
225 * @return true to use MPLS BOS match
226 */
227 public boolean requireMplsBosMatch() {
228 return true;
229 }
230
231 /**
232 * Determines whether this pipeline requires MPLS TTL decrement and copy.
233 *
234 * @return true to use MPLS TTL decrement and copy
235 */
236 public boolean requireMplsTtlModification() {
237 return true;
238 }
239
240 /**
Charles Chand1172632017-03-15 17:33:09 -0700241 * Determines whether this pipeline requires OFDPA match and set VLAN extensions.
242 *
243 * @return true to use the extensions
244 */
245 protected boolean requireVlanExtensions() {
246 return true;
247 }
248
Saurav Das86d13e82017-04-28 17:03:48 -0700249 /**
250 * Determines whether in-port should be matched on in TMAC table rules.
251 *
252 * @return true if match on in-port should be programmed
253 */
254 protected boolean matchInPortTmacTable() {
255 return true;
256 }
257
Charles Chand9e47c62017-10-05 15:17:15 -0700258 /**
259 * Determines whether matching L4 destination port on IPv6 packets is supported in ACL table.
260 *
261 * @return true if matching L4 destination port on IPv6 packets is supported in ACL table.
262 */
263 protected boolean supportIpv6L4Dst() {
264 return true;
265 }
266
Saurav Dasc568c342018-01-25 09:49:01 -0800267 /**
268 * Determines whether this driver should continue to retry flows that point
269 * to empty groups. See CORD-554.
270 *
271 * @return true if the driver should retry flows
272 */
273 protected boolean shouldRetry() {
274 return true;
275 }
276
Charles Chan45b69ab2018-03-02 15:41:41 -0800277 /**
278 * Determines whether this driver requires unicast flow to be installed before multicast flow
279 * in TMAC table.
280 *
281 * @return true if required
282 */
283 protected boolean requireUnicastBeforeMulticast() {
284 return false;
285 }
286
Saurav Das822c4e22015-10-23 10:51:11 -0700287 //////////////////////////////////////
288 // Flow Objectives
289 //////////////////////////////////////
290
291 @Override
292 public void filter(FilteringObjective filteringObjective) {
293 if (filteringObjective.type() == FilteringObjective.Type.PERMIT) {
294 processFilter(filteringObjective,
295 filteringObjective.op() == Objective.Operation.ADD,
296 filteringObjective.appId());
297 } else {
298 // Note that packets that don't match the PERMIT filter are
299 // automatically denied. The DENY filter is used to deny packets
300 // that are otherwise permitted by the PERMIT filter.
301 // Use ACL table flow rules here for DENY filtering objectives
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530302 log.warn("filter objective other than PERMIT currently not supported");
Saurav Das822c4e22015-10-23 10:51:11 -0700303 fail(filteringObjective, ObjectiveError.UNSUPPORTED);
304 }
305 }
306
307 @Override
308 public void forward(ForwardingObjective fwd) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700309 Collection<FlowRule> rules = processForward(fwd);
Saurav Das25190812016-05-27 13:54:07 -0700310 if (rules == null || rules.isEmpty()) {
311 // Assumes fail message has already been generated to the objective
312 // context. Returning here prevents spurious pass message to be
313 // generated by FlowRule service for empty flowOps.
314 return;
315 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700316 sendForward(fwd, rules);
317 }
318
Charles Chan50d900c2018-03-02 13:26:22 -0800319 private void sendForward(ForwardingObjective fwd, Collection<FlowRule> rules) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700320 FlowRuleOperations.Builder flowOpsBuilder = FlowRuleOperations.builder();
Saurav Das822c4e22015-10-23 10:51:11 -0700321 switch (fwd.op()) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700322 case ADD:
323 rules.stream()
324 .filter(Objects::nonNull)
325 .forEach(flowOpsBuilder::add);
Saurav Dasd2fded02016-12-02 15:43:47 -0800326 log.debug("Applying a add fwd-obj {} to sw:{}", fwd.id(), deviceId);
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700327 break;
328 case REMOVE:
329 rules.stream()
330 .filter(Objects::nonNull)
331 .forEach(flowOpsBuilder::remove);
Pier Ventre42287df2016-11-09 14:17:26 -0800332 log.debug("Deleting a flow rule to sw:{}", deviceId);
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700333 break;
334 default:
335 fail(fwd, ObjectiveError.UNKNOWN);
336 log.warn("Unknown forwarding type {}", fwd.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700337 }
338
Saurav Das822c4e22015-10-23 10:51:11 -0700339 flowRuleService.apply(flowOpsBuilder.build(new FlowRuleOperationsContext() {
340 @Override
341 public void onSuccess(FlowRuleOperations ops) {
342 pass(fwd);
343 }
344
345 @Override
346 public void onError(FlowRuleOperations ops) {
347 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
348 }
349 }));
Saurav Das822c4e22015-10-23 10:51:11 -0700350 }
351
352 @Override
353 public void next(NextObjective nextObjective) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800354 NextGroup nextGroup = flowObjectiveStore.getNextGroup(nextObjective.id());
355 switch (nextObjective.op()) {
356 case ADD:
Saurav Das4f980082015-11-05 13:39:15 -0800357 if (nextGroup != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800358 log.warn("Cannot add next {} that already exists in device {}",
359 nextObjective.id(), deviceId);
360 return;
361 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700362 log.debug("Processing NextObjective id {} in dev {} - add group",
Saurav Das8a0732e2015-11-20 15:27:53 -0800363 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700364 groupHandler.addGroup(nextObjective);
Saurav Das8a0732e2015-11-20 15:27:53 -0800365 break;
366 case ADD_TO_EXISTING:
367 if (nextGroup != null) {
Saurav Das1547b3f2017-05-05 17:01:08 -0700368 log.debug("Processing NextObjective id {} in dev {} - add bucket",
Saurav Das8a0732e2015-11-20 15:27:53 -0800369 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700370 groupHandler.addBucketToGroup(nextObjective, nextGroup);
Saurav Das4f980082015-11-05 13:39:15 -0800371 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800372 // it is possible that group-chain has not been fully created yet
Saurav Das423fe2b2015-12-04 10:52:59 -0800373 log.debug("Waiting to add bucket to group for next-id:{} in dev:{}",
374 nextObjective.id(), deviceId);
Yi Tseng47f82dc2017-03-05 22:48:39 -0800375
376 // by design multiple pending bucket is allowed for the group
377 groupHandler.pendingBuckets.compute(nextObjective.id(), (nextId, pendBkts) -> {
378 if (pendBkts == null) {
379 pendBkts = Sets.newHashSet();
380 }
381 pendBkts.add(nextObjective);
382 return pendBkts;
383 });
Saurav Das4f980082015-11-05 13:39:15 -0800384 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800385 break;
386 case REMOVE:
387 if (nextGroup == null) {
388 log.warn("Cannot remove next {} that does not exist in device {}",
389 nextObjective.id(), deviceId);
390 return;
391 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700392 log.debug("Processing NextObjective id {} in dev {} - remove group",
Saurav Das8a0732e2015-11-20 15:27:53 -0800393 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700394 groupHandler.removeGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800395 break;
396 case REMOVE_FROM_EXISTING:
397 if (nextGroup == null) {
398 log.warn("Cannot remove from next {} that does not exist in device {}",
399 nextObjective.id(), deviceId);
400 return;
401 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700402 log.debug("Processing NextObjective id {} in dev {} - remove bucket",
Saurav Das8a0732e2015-11-20 15:27:53 -0800403 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700404 groupHandler.removeBucketFromGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800405 break;
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +0900406 case MODIFY:
407 if (nextGroup == null) {
408 log.warn("Cannot modify next {} that does not exist in device {}",
409 nextObjective.id(), deviceId);
410 return;
411 }
412 log.debug("Processing NextObjective id {} in dev {} - modify bucket",
413 nextObjective.id(), deviceId);
414 groupHandler.modifyBucketFromGroup(nextObjective, nextGroup);
415 break;
Saurav Dasceccf242017-08-03 18:30:35 -0700416 case VERIFY:
417 if (nextGroup == null) {
418 log.warn("Cannot verify next {} that does not exist in device {}",
419 nextObjective.id(), deviceId);
420 return;
421 }
422 log.debug("Processing NextObjective id {} in dev {} - verify",
423 nextObjective.id(), deviceId);
424 groupHandler.verifyGroup(nextObjective, nextGroup);
425 break;
Saurav Das8a0732e2015-11-20 15:27:53 -0800426 default:
Saurav Das4f980082015-11-05 13:39:15 -0800427 log.warn("Unsupported operation {}", nextObjective.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700428 }
429 }
430
431 //////////////////////////////////////
432 // Flow handling
433 //////////////////////////////////////
434
435 /**
Yi Tseng0fdc07e2017-04-24 19:17:08 -0700436 * As per OFDPA 2.0 TTP, filtering of VLAN ids and MAC addresses (for routing)
437 * configured on switch ports happen in different tables.
Saurav Das822c4e22015-10-23 10:51:11 -0700438 *
439 * @param filt the filtering objective
440 * @param install indicates whether to add or remove the objective
441 * @param applicationId the application that sent this objective
442 */
Saurav Das52025962016-01-28 22:30:01 -0800443 protected void processFilter(FilteringObjective filt,
444 boolean install, ApplicationId applicationId) {
Saurav Das822c4e22015-10-23 10:51:11 -0700445 // This driver only processes filtering criteria defined with switch
446 // ports as the key
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530447 PortCriterion portCriterion = null;
Saurav Das822c4e22015-10-23 10:51:11 -0700448 EthCriterion ethCriterion = null;
449 VlanIdCriterion vidCriterion = null;
Saurav Das822c4e22015-10-23 10:51:11 -0700450 if (!filt.key().equals(Criteria.dummy()) &&
451 filt.key().type() == Criterion.Type.IN_PORT) {
452 portCriterion = (PortCriterion) filt.key();
Saurav Das822c4e22015-10-23 10:51:11 -0700453 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530454 if (portCriterion == null) {
455 log.debug("No IN_PORT defined in filtering objective from app: {}",
456 applicationId);
457 } else {
458 log.debug("Received filtering objective for dev/port: {}/{}", deviceId,
459 portCriterion.port());
460 }
Saurav Das822c4e22015-10-23 10:51:11 -0700461 // convert filtering conditions for switch-intfs into flowrules
462 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
463 for (Criterion criterion : filt.conditions()) {
Yi Tseng0fdc07e2017-04-24 19:17:08 -0700464 switch (criterion.type()) {
465 case ETH_DST:
466 case ETH_DST_MASKED:
467 ethCriterion = (EthCriterion) criterion;
468 break;
469 case VLAN_VID:
470 vidCriterion = (VlanIdCriterion) criterion;
471 break;
472 default:
473 log.warn("Unsupported filter {}", criterion);
474 fail(filt, ObjectiveError.UNSUPPORTED);
475 return;
Saurav Das822c4e22015-10-23 10:51:11 -0700476 }
477 }
478
Saurav Das0e99e2b2015-10-28 12:39:42 -0700479 VlanId assignedVlan = null;
Charles Chane849c192016-01-11 18:28:54 -0800480 if (vidCriterion != null) {
Charles Chand55e84d2016-03-30 17:54:24 -0700481 // Use the VLAN in criterion if metadata is not present and the traffic is tagged
Charles Chanc550f2e2017-12-19 19:55:57 -0800482 if (!vidCriterion.vlanId().equals(VlanId.NONE)) {
Charles Chane849c192016-01-11 18:28:54 -0800483 assignedVlan = vidCriterion.vlanId();
Piere5bff482018-03-07 11:42:50 +0100484 }
485 // If the meta VLAN is present let's update the assigned vlan
486 if (filt.meta() != null) {
487 VlanId metaVlan = readVlanFromTreatment(filt.meta());
488 if (metaVlan != null) {
489 assignedVlan = metaVlan;
490 }
Charles Chand55e84d2016-03-30 17:54:24 -0700491 }
Charles Chane849c192016-01-11 18:28:54 -0800492
Charles Chand55e84d2016-03-30 17:54:24 -0700493 if (assignedVlan == null) {
494 log.error("Driver fails to extract VLAN information. "
Frank Wangd8ab0962017-08-11 11:09:30 +0800495 + "Not processing VLAN filters on device {}.", deviceId);
Charles Chand55e84d2016-03-30 17:54:24 -0700496 log.debug("VLAN ID in criterion={}, metadata={}",
497 readVlanFromTreatment(filt.meta()), vidCriterion.vlanId());
498 fail(filt, ObjectiveError.BADPARAMS);
499 return;
Saurav Das0e99e2b2015-10-28 12:39:42 -0700500 }
501 }
502
Yi Tseng3a77b4f2017-02-06 15:02:17 -0800503 if (ethCriterion == null || ethCriterion.mac().equals(NONE)) {
Charles Chan985b12e2016-05-11 19:47:22 -0700504 // NOTE: it is possible that a filtering objective only has vidCriterion
Saurav Das961beb22017-03-29 19:09:17 -0700505 log.warn("filtering objective missing dstMac, cannot program TMAC table");
Saurav Das822c4e22015-10-23 10:51:11 -0700506 } else {
Charles Chan45b69ab2018-03-02 15:41:41 -0800507 MacAddress unicastMac = readEthDstFromTreatment(filt.meta());
Charles Chan66291502018-03-02 16:43:28 -0800508 List<List<FlowRule>> allStages = processEthDstFilter(portCriterion, ethCriterion,
Charles Chan45b69ab2018-03-02 15:41:41 -0800509 vidCriterion, assignedVlan, unicastMac, applicationId);
Charles Chan66291502018-03-02 16:43:28 -0800510 for (List<FlowRule> flowRules : allStages) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800511 log.trace("Starting a new flow rule stage for TMAC table flow");
Charles Chan66291502018-03-02 16:43:28 -0800512 ops.newStage();
Charles Chanc550f2e2017-12-19 19:55:57 -0800513
Charles Chan66291502018-03-02 16:43:28 -0800514 for (FlowRule flowRule : flowRules) {
515 log.trace("{} flow rules in TMAC table: {} for dev: {}",
516 (install) ? "adding" : "removing", flowRules, deviceId);
Charles Chan66291502018-03-02 16:43:28 -0800517 if (install) {
518 ops = ops.add(flowRule);
Charles Chanc550f2e2017-12-19 19:55:57 -0800519 } else {
Charles Chan66291502018-03-02 16:43:28 -0800520 // NOTE: Only remove TMAC flow when there is no more enabled port within the
521 // same VLAN on this device if TMAC doesn't support matching on in_port.
522 if (matchInPortTmacTable() || (filt.meta() != null && filt.meta().clearedDeferred())) {
523 ops = ops.remove(flowRule);
524 } else {
525 log.debug("Abort TMAC flow removal on {}. Some other ports still share this TMAC flow");
526 }
Charles Chanc550f2e2017-12-19 19:55:57 -0800527 }
528 }
Saurav Das822c4e22015-10-23 10:51:11 -0700529 }
530 }
531
Charles Chan985b12e2016-05-11 19:47:22 -0700532 if (vidCriterion == null) {
533 // NOTE: it is possible that a filtering objective only has ethCriterion
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530534 log.info("filtering objective missing VLAN, cannot program VLAN Table");
Saurav Das822c4e22015-10-23 10:51:11 -0700535 } else {
Charles Chan66291502018-03-02 16:43:28 -0800536 List<List<FlowRule>> allStages = processVlanIdFilter(
Charles Chan14967c22015-12-07 11:11:50 -0800537 portCriterion, vidCriterion, assignedVlan, applicationId);
Charles Chan66291502018-03-02 16:43:28 -0800538 for (List<FlowRule> flowRules : allStages) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800539 log.trace("Starting a new flow rule stage for VLAN table flow");
Charles Chan66291502018-03-02 16:43:28 -0800540 ops.newStage();
Charles Chan14967c22015-12-07 11:11:50 -0800541
Charles Chan66291502018-03-02 16:43:28 -0800542 for (FlowRule flowRule : flowRules) {
543 log.trace("{} flow rules in VLAN table: {} for dev: {}",
544 (install) ? "adding" : "removing", flowRule, deviceId);
545 ops = install ? ops.add(flowRule) : ops.remove(flowRule);
Charles Chand1172632017-03-15 17:33:09 -0700546 }
Saurav Das822c4e22015-10-23 10:51:11 -0700547 }
548 }
549
Saurav Das822c4e22015-10-23 10:51:11 -0700550 // apply filtering flow rules
551 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
552 @Override
553 public void onSuccess(FlowRuleOperations ops) {
Saurav Das018605f2017-02-18 14:05:44 -0800554 log.debug("Applied {} filtering rules in device {}",
Saurav Das822c4e22015-10-23 10:51:11 -0700555 ops.stages().get(0).size(), deviceId);
556 pass(filt);
557 }
558
559 @Override
560 public void onError(FlowRuleOperations ops) {
561 log.info("Failed to apply all filtering rules in dev {}", deviceId);
562 fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED);
563 }
564 }));
565
566 }
567
568 /**
Charles Chand1172632017-03-15 17:33:09 -0700569 * Internal implementation of processVlanIdFilter.
570 * <p>
571 * The is_present bit in set_vlan_vid action is required to be 0 in OFDPA i12.
572 * Since it is non-OF spec, we need an extension treatment for that.
573 * The useVlanExtension must be set to false for OFDPA i12.
574 * </p>
Charles Chan66291502018-03-02 16:43:28 -0800575 * <p>
576 * NOTE: Separate VLAN filtering rules and assignment rules
577 * into different stages in order to guarantee that filtering rules
578 * always go first, as required by OFDPA.
579 * </p>
Saurav Das0e99e2b2015-10-28 12:39:42 -0700580 *
Charles Chanf9e98652016-09-07 16:54:23 -0700581 * @param portCriterion port on device for which this filter is programmed
582 * @param vidCriterion vlan assigned to port, or NONE for untagged
583 * @param assignedVlan assigned vlan-id for untagged packets
584 * @param applicationId for application programming this filter
Charles Chan66291502018-03-02 16:43:28 -0800585 * @return stages of flow rules for port-vlan filters
Saurav Das822c4e22015-10-23 10:51:11 -0700586 */
Charles Chan66291502018-03-02 16:43:28 -0800587 protected List<List<FlowRule>> processVlanIdFilter(PortCriterion portCriterion,
Yi Tsengef19de12017-04-24 11:33:05 -0700588 VlanIdCriterion vidCriterion,
589 VlanId assignedVlan,
590 ApplicationId applicationId) {
Charles Chan66291502018-03-02 16:43:28 -0800591 List<FlowRule> filteringRules = new ArrayList<>();
592 List<FlowRule> assignmentRules = new ArrayList<>();
Saurav Das822c4e22015-10-23 10:51:11 -0700593 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
594 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800595 TrafficSelector.Builder preSelector = null;
596 TrafficTreatment.Builder preTreatment = null;
597
Saurav Das4f980082015-11-05 13:39:15 -0800598 treatment.transition(TMAC_TABLE);
599
Saurav Das822c4e22015-10-23 10:51:11 -0700600 if (vidCriterion.vlanId() == VlanId.NONE) {
601 // untagged packets are assigned vlans
Charles Chand1172632017-03-15 17:33:09 -0700602 preSelector = DefaultTrafficSelector.builder();
603 if (requireVlanExtensions()) {
604 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(VlanId.NONE);
605 selector.extension(ofdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700606 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
607 treatment.extension(ofdpaSetVlanVid, deviceId);
Charles Chand1172632017-03-15 17:33:09 -0700608
609 OfdpaMatchVlanVid preOfdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
610 preSelector.extension(preOfdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700611 } else {
Charles Chand1172632017-03-15 17:33:09 -0700612 selector.matchVlanId(VlanId.NONE);
Charles Chanf9e98652016-09-07 16:54:23 -0700613 treatment.setVlanId(assignedVlan);
Charles Chand1172632017-03-15 17:33:09 -0700614
615 preSelector.matchVlanId(assignedVlan);
616 }
617 preTreatment = DefaultTrafficTreatment.builder().transition(TMAC_TABLE);
618 } else {
619 if (requireVlanExtensions()) {
620 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
621 selector.extension(ofdpaMatchVlanVid, deviceId);
622 } else {
623 selector.matchVlanId(vidCriterion.vlanId());
Charles Chanf9e98652016-09-07 16:54:23 -0700624 }
Charles Chan14967c22015-12-07 11:11:50 -0800625
Charles Chand55e84d2016-03-30 17:54:24 -0700626 if (!assignedVlan.equals(vidCriterion.vlanId())) {
Charles Chand1172632017-03-15 17:33:09 -0700627 if (requireVlanExtensions()) {
Charles Chanc03782d2017-01-31 13:57:55 -0800628 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
629 treatment.extension(ofdpaSetVlanVid, deviceId);
630 } else {
631 treatment.setVlanId(assignedVlan);
632 }
Charles Chand55e84d2016-03-30 17:54:24 -0700633 }
Saurav Das822c4e22015-10-23 10:51:11 -0700634 }
Saurav Das822c4e22015-10-23 10:51:11 -0700635
636 // ofdpa cannot match on ALL portnumber, so we need to use separate
637 // rules for each port.
Charles Chan14967c22015-12-07 11:11:50 -0800638 List<PortNumber> portnums = new ArrayList<>();
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530639 if (portCriterion != null) {
640 if (PortNumber.ALL.equals(portCriterion.port())) {
641 for (Port port : deviceService.getPorts(deviceId)) {
642 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
643 portnums.add(port.number());
644 }
Saurav Das822c4e22015-10-23 10:51:11 -0700645 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530646 } else {
647 portnums.add(portCriterion.port());
Saurav Das822c4e22015-10-23 10:51:11 -0700648 }
649 } else {
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530650 log.warn("Filtering Objective missing Port Criterion . " +
Charles Chan66291502018-03-02 16:43:28 -0800651 "VLAN Table cannot be programmed for {}", deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700652 }
Saurav Das4f980082015-11-05 13:39:15 -0800653
Saurav Das822c4e22015-10-23 10:51:11 -0700654 for (PortNumber pnum : portnums) {
Saurav Das4f980082015-11-05 13:39:15 -0800655 // create rest of flowrule
Saurav Das822c4e22015-10-23 10:51:11 -0700656 selector.matchInPort(pnum);
657 FlowRule rule = DefaultFlowRule.builder()
658 .forDevice(deviceId)
659 .withSelector(selector.build())
660 .withTreatment(treatment.build())
661 .withPriority(DEFAULT_PRIORITY)
662 .fromApp(applicationId)
663 .makePermanent()
664 .forTable(VLAN_TABLE).build();
Charles Chan66291502018-03-02 16:43:28 -0800665 assignmentRules.add(rule);
Charles Chan14967c22015-12-07 11:11:50 -0800666
667 if (preSelector != null) {
668 preSelector.matchInPort(pnum);
669 FlowRule preRule = DefaultFlowRule.builder()
670 .forDevice(deviceId)
671 .withSelector(preSelector.build())
672 .withTreatment(preTreatment.build())
673 .withPriority(DEFAULT_PRIORITY)
674 .fromApp(applicationId)
675 .makePermanent()
676 .forTable(VLAN_TABLE).build();
Charles Chan66291502018-03-02 16:43:28 -0800677 filteringRules.add(preRule);
Charles Chan14967c22015-12-07 11:11:50 -0800678 }
Saurav Das822c4e22015-10-23 10:51:11 -0700679 }
Charles Chan2686dd72018-03-06 22:10:15 -0800680 return ImmutableList.of(filteringRules, assignmentRules);
Saurav Das822c4e22015-10-23 10:51:11 -0700681 }
682
683 /**
684 * Allows routed packets with correct destination MAC to be directed
Charles Chan45b69ab2018-03-02 15:41:41 -0800685 * to unicast routing table, multicast routing table or MPLS forwarding table.
Saurav Das822c4e22015-10-23 10:51:11 -0700686 *
687 * @param portCriterion port on device for which this filter is programmed
688 * @param ethCriterion dstMac of device for which is filter is programmed
689 * @param vidCriterion vlan assigned to port, or NONE for untagged
Saurav Das0e99e2b2015-10-28 12:39:42 -0700690 * @param assignedVlan assigned vlan-id for untagged packets
Charles Chan45b69ab2018-03-02 15:41:41 -0800691 * @param unicastMac some switches require a unicast TMAC flow to be programmed before multicast
692 * TMAC flow. This MAC address will be used for the unicast TMAC flow.
693 * This is unused if the filtering objective is a unicast.
Saurav Das822c4e22015-10-23 10:51:11 -0700694 * @param applicationId for application programming this filter
Charles Chan66291502018-03-02 16:43:28 -0800695 * @return stages of flow rules for port-vlan filters
Saurav Das822c4e22015-10-23 10:51:11 -0700696
697 */
Charles Chan66291502018-03-02 16:43:28 -0800698 protected List<List<FlowRule>> processEthDstFilter(PortCriterion portCriterion,
Saurav Das822c4e22015-10-23 10:51:11 -0700699 EthCriterion ethCriterion,
700 VlanIdCriterion vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700701 VlanId assignedVlan,
Charles Chan45b69ab2018-03-02 15:41:41 -0800702 MacAddress unicastMac,
Saurav Das822c4e22015-10-23 10:51:11 -0700703 ApplicationId applicationId) {
Charles Chan5270ed02016-01-30 23:22:37 -0800704 // Consider PortNumber.ANY as wildcard. Match ETH_DST only
Sivachidambaram Subramanianc598d062017-05-16 22:42:06 +0530705 if (portCriterion != null && PortNumber.ANY.equals(portCriterion.port())) {
Charles Chan5270ed02016-01-30 23:22:37 -0800706 return processEthDstOnlyFilter(ethCriterion, applicationId);
707 }
708
Charles Chan5b9df8d2016-03-28 22:21:40 -0700709 // Multicast MAC
710 if (ethCriterion.mask() != null) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800711 return processMcastEthDstFilter(ethCriterion, assignedVlan, unicastMac, applicationId);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700712 }
713
Saurav Das822c4e22015-10-23 10:51:11 -0700714 //handling untagged packets via assigned VLAN
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530715 if (vidCriterion != null && vidCriterion.vlanId() == VlanId.NONE) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700716 vidCriterion = (VlanIdCriterion) Criteria.matchVlanId(assignedVlan);
Saurav Das822c4e22015-10-23 10:51:11 -0700717 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530718 List<FlowRule> rules = new ArrayList<>();
719 OfdpaMatchVlanVid ofdpaMatchVlanVid = null;
720 if (vidCriterion != null && requireVlanExtensions()) {
721 ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
722 }
Saurav Das822c4e22015-10-23 10:51:11 -0700723 // ofdpa cannot match on ALL portnumber, so we need to use separate
724 // rules for each port.
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -0700725 List<PortNumber> portnums = new ArrayList<>();
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530726 if (portCriterion != null) {
727 if (PortNumber.ALL.equals(portCriterion.port())) {
728 for (Port port : deviceService.getPorts(deviceId)) {
729 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
730 portnums.add(port.number());
731 }
Saurav Das822c4e22015-10-23 10:51:11 -0700732 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530733 } else {
734 portnums.add(portCriterion.port());
735 }
736 for (PortNumber pnum : portnums) {
737 rules.add(buildTmacRuleForIpv4(ethCriterion,
738 vidCriterion,
739 ofdpaMatchVlanVid,
740 applicationId,
741 pnum));
742 rules.add(buildTmacRuleForMpls(ethCriterion,
743 vidCriterion,
744 ofdpaMatchVlanVid,
745 applicationId,
746 pnum));
747 rules.add(buildTmacRuleForIpv6(ethCriterion,
748 vidCriterion,
749 ofdpaMatchVlanVid,
750 applicationId,
751 pnum));
Saurav Das822c4e22015-10-23 10:51:11 -0700752 }
753 } else {
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530754 rules.add(buildTmacRuleForIpv4(ethCriterion,
755 vidCriterion,
756 ofdpaMatchVlanVid,
757 applicationId,
758 null));
759 rules.add(buildTmacRuleForMpls(ethCriterion,
760 vidCriterion,
761 ofdpaMatchVlanVid,
762 applicationId,
763 null));
764 rules.add(buildTmacRuleForIpv6(ethCriterion,
765 vidCriterion,
766 ofdpaMatchVlanVid,
767 applicationId,
768 null));
Saurav Das822c4e22015-10-23 10:51:11 -0700769 }
Charles Chan66291502018-03-02 16:43:28 -0800770 return ImmutableList.of(rules);
Saurav Das822c4e22015-10-23 10:51:11 -0700771 }
772
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530773 /**
774 * Builds TMAC rules for IPv4 packets.
775 *
Charles Chanc550f2e2017-12-19 19:55:57 -0800776 * @param ethCriterion dst mac matching
777 * @param vidCriterion vlan id assigned to the port
778 * @param ofdpaMatchVlanVid OFDPA vlan id matching
779 * @param applicationId application id
780 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530781 * @return TMAC rule for IPV4 packets
782 */
783 private FlowRule buildTmacRuleForIpv4(EthCriterion ethCriterion,
784 VlanIdCriterion vidCriterion,
785 OfdpaMatchVlanVid ofdpaMatchVlanVid,
786 ApplicationId applicationId,
787 PortNumber pnum) {
788 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
789 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
790 if (pnum != null) {
791 if (matchInPortTmacTable()) {
792 selector.matchInPort(pnum);
793 } else {
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800794 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530795 "ignoring the IN_PORT criteria");
796 }
797 }
798 if (vidCriterion != null) {
799 if (requireVlanExtensions()) {
800 selector.extension(ofdpaMatchVlanVid, deviceId);
801 } else {
802 selector.matchVlanId(vidCriterion.vlanId());
803 }
804 }
805 selector.matchEthType(Ethernet.TYPE_IPV4);
806 selector.matchEthDst(ethCriterion.mac());
807 treatment.transition(UNICAST_ROUTING_TABLE);
808 return DefaultFlowRule.builder()
809 .forDevice(deviceId)
810 .withSelector(selector.build())
811 .withTreatment(treatment.build())
812 .withPriority(DEFAULT_PRIORITY)
813 .fromApp(applicationId)
814 .makePermanent()
815 .forTable(TMAC_TABLE).build();
816 }
817
818 /**
819 * Builds TMAC rule for MPLS packets.
820 *
Charles Chanc550f2e2017-12-19 19:55:57 -0800821 * @param ethCriterion dst mac matching
822 * @param vidCriterion vlan id assigned to the port
823 * @param ofdpaMatchVlanVid OFDPA vlan id matching
824 * @param applicationId application id
825 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530826 * @return TMAC rule for MPLS packets
827 */
828 private FlowRule buildTmacRuleForMpls(EthCriterion ethCriterion,
829 VlanIdCriterion vidCriterion,
830 OfdpaMatchVlanVid ofdpaMatchVlanVid,
831 ApplicationId applicationId,
832 PortNumber pnum) {
833 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
834 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
835 if (pnum != null) {
836 if (matchInPortTmacTable()) {
837 selector.matchInPort(pnum);
838 } else {
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800839 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530840 "ignoring the IN_PORT criteria");
841 }
842 }
843 if (vidCriterion != null) {
844 if (requireVlanExtensions()) {
845 selector.extension(ofdpaMatchVlanVid, deviceId);
846 } else {
847 selector.matchVlanId(vidCriterion.vlanId());
848 }
849 }
850 selector.matchEthType(Ethernet.MPLS_UNICAST);
851 selector.matchEthDst(ethCriterion.mac());
852 treatment.transition(MPLS_TABLE_0);
853 return DefaultFlowRule.builder()
854 .forDevice(deviceId)
855 .withSelector(selector.build())
856 .withTreatment(treatment.build())
857 .withPriority(DEFAULT_PRIORITY)
858 .fromApp(applicationId)
859 .makePermanent()
860 .forTable(TMAC_TABLE).build();
861 }
862
863 /**
864 * Builds TMAC rules for IPv6 packets.
865 *
Charles Chanc550f2e2017-12-19 19:55:57 -0800866 * @param ethCriterion dst mac matching
867 * @param vidCriterion vlan id assigned to the port
868 * @param ofdpaMatchVlanVid OFDPA vlan id matching
869 * @param applicationId application id
870 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530871 * @return TMAC rule for IPV6 packets
872 */
873 private FlowRule buildTmacRuleForIpv6(EthCriterion ethCriterion,
874 VlanIdCriterion vidCriterion,
875 OfdpaMatchVlanVid ofdpaMatchVlanVid,
876 ApplicationId applicationId,
877 PortNumber pnum) {
878 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
879 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
880 if (pnum != null) {
881 if (matchInPortTmacTable()) {
882 selector.matchInPort(pnum);
883 } else {
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800884 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530885 "ignoring the IN_PORT criteria");
886 }
887 }
888 if (vidCriterion != null) {
889 if (requireVlanExtensions()) {
890 selector.extension(ofdpaMatchVlanVid, deviceId);
891 } else {
892 selector.matchVlanId(vidCriterion.vlanId());
893 }
894 }
895 selector.matchEthType(Ethernet.TYPE_IPV6);
896 selector.matchEthDst(ethCriterion.mac());
897 treatment.transition(UNICAST_ROUTING_TABLE);
898 return DefaultFlowRule.builder()
899 .forDevice(deviceId)
900 .withSelector(selector.build())
901 .withTreatment(treatment.build())
902 .withPriority(DEFAULT_PRIORITY)
903 .fromApp(applicationId)
904 .makePermanent()
905 .forTable(TMAC_TABLE).build();
906 }
907
Charles Chan66291502018-03-02 16:43:28 -0800908 protected List<List<FlowRule>> processEthDstOnlyFilter(EthCriterion ethCriterion,
Yi Tsengef19de12017-04-24 11:33:05 -0700909 ApplicationId applicationId) {
Pier Luigi0e358632017-01-31 09:35:05 -0800910 ImmutableList.Builder<FlowRule> builder = ImmutableList.builder();
911
Charles Chan5270ed02016-01-30 23:22:37 -0800912 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
913 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
914 selector.matchEthType(Ethernet.TYPE_IPV4);
915 selector.matchEthDst(ethCriterion.mac());
916 treatment.transition(UNICAST_ROUTING_TABLE);
917 FlowRule rule = DefaultFlowRule.builder()
918 .forDevice(deviceId)
919 .withSelector(selector.build())
920 .withTreatment(treatment.build())
921 .withPriority(DEFAULT_PRIORITY)
922 .fromApp(applicationId)
923 .makePermanent()
924 .forTable(TMAC_TABLE).build();
Pier Luigi0e358632017-01-31 09:35:05 -0800925 builder.add(rule);
926
927 selector = DefaultTrafficSelector.builder();
928 treatment = DefaultTrafficTreatment.builder();
929 selector.matchEthType(Ethernet.TYPE_IPV6);
930 selector.matchEthDst(ethCriterion.mac());
931 treatment.transition(UNICAST_ROUTING_TABLE);
932 rule = DefaultFlowRule.builder()
933 .forDevice(deviceId)
934 .withSelector(selector.build())
935 .withTreatment(treatment.build())
936 .withPriority(DEFAULT_PRIORITY)
937 .fromApp(applicationId)
938 .makePermanent()
939 .forTable(TMAC_TABLE).build();
Charles Chan66291502018-03-02 16:43:28 -0800940 return ImmutableList.of(builder.add(rule).build());
Charles Chan5270ed02016-01-30 23:22:37 -0800941 }
942
Charles Chan66291502018-03-02 16:43:28 -0800943 List<List<FlowRule>> processMcastEthDstFilter(EthCriterion ethCriterion,
Charles Chanb4879a52017-10-20 19:09:16 -0700944 VlanId assignedVlan,
Charles Chan45b69ab2018-03-02 15:41:41 -0800945 MacAddress unicastMac,
Yi Tsengef19de12017-04-24 11:33:05 -0700946 ApplicationId applicationId) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800947 ImmutableList.Builder<FlowRule> unicastFlows = ImmutableList.builder();
948 ImmutableList.Builder<FlowRule> multicastFlows = ImmutableList.builder();
949 TrafficSelector.Builder selector;
950 TrafficTreatment.Builder treatment;
Charles Chanb4879a52017-10-20 19:09:16 -0700951 FlowRule rule;
Julia Ferguson65428c32017-08-10 18:15:24 +0000952
Charles Chanb4879a52017-10-20 19:09:16 -0700953 if (IPV4_MULTICAST.equals(ethCriterion.mac())) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800954 if (requireUnicastBeforeMulticast()) {
955 selector = DefaultTrafficSelector.builder();
956 treatment = DefaultTrafficTreatment.builder();
957 selector.matchEthType(Ethernet.TYPE_IPV4);
958 selector.matchEthDst(unicastMac);
959 selector.matchVlanId(assignedVlan);
960 treatment.transition(UNICAST_ROUTING_TABLE);
961 rule = DefaultFlowRule.builder()
962 .forDevice(deviceId)
963 .withSelector(selector.build())
964 .withTreatment(treatment.build())
965 .withPriority(DEFAULT_PRIORITY)
966 .fromApp(applicationId)
967 .makePermanent()
968 .forTable(TMAC_TABLE).build();
969 unicastFlows.add(rule);
970 }
971
972 selector = DefaultTrafficSelector.builder();
973 treatment = DefaultTrafficTreatment.builder();
Charles Chanb4879a52017-10-20 19:09:16 -0700974 selector.matchEthType(Ethernet.TYPE_IPV4);
975 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
976 selector.matchVlanId(assignedVlan);
977 treatment.transition(MULTICAST_ROUTING_TABLE);
978 rule = DefaultFlowRule.builder()
979 .forDevice(deviceId)
980 .withSelector(selector.build())
981 .withTreatment(treatment.build())
982 .withPriority(DEFAULT_PRIORITY)
983 .fromApp(applicationId)
984 .makePermanent()
985 .forTable(TMAC_TABLE).build();
Charles Chan45b69ab2018-03-02 15:41:41 -0800986 multicastFlows.add(rule);
Charles Chanb4879a52017-10-20 19:09:16 -0700987 }
988
989 if (IPV6_MULTICAST.equals(ethCriterion.mac())) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800990 if (requireUnicastBeforeMulticast()) {
991 selector = DefaultTrafficSelector.builder();
992 treatment = DefaultTrafficTreatment.builder();
993 selector.matchEthType(Ethernet.TYPE_IPV6);
994 selector.matchEthDst(unicastMac);
995 selector.matchVlanId(assignedVlan);
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();
1005 unicastFlows.add(rule);
1006 }
1007
Charles Chanb4879a52017-10-20 19:09:16 -07001008 selector = DefaultTrafficSelector.builder();
1009 treatment = DefaultTrafficTreatment.builder();
1010 selector.matchEthType(Ethernet.TYPE_IPV6);
1011 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
1012 selector.matchVlanId(assignedVlan);
1013 treatment.transition(MULTICAST_ROUTING_TABLE);
1014 rule = DefaultFlowRule.builder()
1015 .forDevice(deviceId)
1016 .withSelector(selector.build())
1017 .withTreatment(treatment.build())
1018 .withPriority(DEFAULT_PRIORITY)
1019 .fromApp(applicationId)
1020 .makePermanent()
1021 .forTable(TMAC_TABLE).build();
Charles Chan45b69ab2018-03-02 15:41:41 -08001022 multicastFlows.add(rule);
Charles Chanb4879a52017-10-20 19:09:16 -07001023 }
Charles Chan45b69ab2018-03-02 15:41:41 -08001024 return ImmutableList.of(unicastFlows.build(), multicastFlows.build());
Charles Chan5b9df8d2016-03-28 22:21:40 -07001025 }
1026
Saurav Das822c4e22015-10-23 10:51:11 -07001027 private Collection<FlowRule> processForward(ForwardingObjective fwd) {
1028 switch (fwd.flag()) {
1029 case SPECIFIC:
1030 return processSpecific(fwd);
1031 case VERSATILE:
1032 return processVersatile(fwd);
Jonghwan Hyun800d9d02018-04-09 09:40:50 -07001033 case EGRESS:
1034 return processEgress(fwd);
Saurav Das822c4e22015-10-23 10:51:11 -07001035 default:
1036 fail(fwd, ObjectiveError.UNKNOWN);
1037 log.warn("Unknown forwarding flag {}", fwd.flag());
1038 }
1039 return Collections.emptySet();
1040 }
1041
1042 /**
Jonghwan Hyun800d9d02018-04-09 09:40:50 -07001043 * In the OF-DPA 2.0 pipeline, egress forwarding objectives go to the
1044 * egress tables.
1045 * @param fwd the forwarding objective of type 'egress'
1046 * @return a collection of flow rules to be sent to the switch. An empty
1047 * collection may be returned if there is a problem in processing
1048 * the flow rule
1049 */
1050 protected Collection<FlowRule> processEgress(ForwardingObjective fwd) {
1051 log.debug("Processing egress forwarding objective:{} in dev:{}",
1052 fwd, deviceId);
1053
1054 List<FlowRule> rules = new ArrayList<>();
1055
1056 // Build selector
1057 TrafficSelector.Builder sb = DefaultTrafficSelector.builder();
1058 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) fwd.selector().getCriterion(Criterion.Type.VLAN_VID);
1059 if (vlanIdCriterion == null) {
1060 log.error("Egress forwarding objective:{} must include vlanId", fwd.id());
1061 fail(fwd, ObjectiveError.BADPARAMS);
1062 return rules;
1063 }
1064
1065 Optional<Instruction> outInstr = fwd.treatment().allInstructions().stream()
1066 .filter(instruction -> instruction instanceof OutputInstruction).findFirst();
1067 if (!outInstr.isPresent()) {
1068 log.error("Egress forwarding objective:{} must include output port", fwd.id());
1069 fail(fwd, ObjectiveError.BADPARAMS);
1070 return rules;
1071 }
1072
1073 PortNumber portNumber = ((OutputInstruction) outInstr.get()).port();
1074
1075 sb.matchVlanId(vlanIdCriterion.vlanId());
1076 OfdpaMatchActsetOutput actsetOutput = new OfdpaMatchActsetOutput(portNumber);
1077 sb.extension(actsetOutput, deviceId);
1078
1079 sb.extension(new OfdpaMatchAllowVlanTranslation(ALLOW_VLAN_TRANSLATION), deviceId);
1080
1081 // Build a flow rule for Egress VLAN Flow table
1082 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
1083 tb.transition(EGRESS_DSCP_PCP_REMARK_FLOW_TABLE);
1084 if (fwd.treatment() != null) {
1085 for (Instruction instr : fwd.treatment().allInstructions()) {
1086 if (instr instanceof L2ModificationInstruction &&
1087 ((L2ModificationInstruction) instr).subtype() == L2SubType.VLAN_ID) {
1088 tb.immediate().add(instr);
1089 }
1090 if (instr instanceof L2ModificationInstruction &&
1091 ((L2ModificationInstruction) instr).subtype() == L2SubType.VLAN_PUSH) {
1092 tb.immediate().pushVlan();
1093 EthType ethType = ((L2ModificationInstruction.ModVlanHeaderInstruction) instr).ethernetType();
1094 if (ethType.equals(EtherType.QINQ.ethType())) {
1095 // Build a flow rule for Egress TPID Flow table
1096 TrafficSelector tpidSelector = DefaultTrafficSelector.builder()
1097 .extension(actsetOutput, deviceId)
1098 .matchVlanId(VlanId.ANY).build();
1099
1100 TrafficTreatment tpidTreatment = DefaultTrafficTreatment.builder()
1101 .extension(new Ofdpa3CopyField(COPY_FIELD_NBITS, COPY_FIELD_OFFSET,
1102 COPY_FIELD_OFFSET, OXM_ID_VLAN_VID,
1103 OXM_ID_PACKET_REG_1),
1104 deviceId)
1105 .popVlan()
1106 .pushVlan(EtherType.QINQ.ethType())
1107 .extension(new Ofdpa3CopyField(COPY_FIELD_NBITS, COPY_FIELD_OFFSET,
1108 COPY_FIELD_OFFSET, OXM_ID_PACKET_REG_1,
1109 OXM_ID_VLAN_VID),
1110 deviceId)
1111 .build();
1112
1113 FlowRule.Builder tpidRuleBuilder = DefaultFlowRule.builder()
1114 .fromApp(fwd.appId())
1115 .withPriority(fwd.priority())
1116 .forDevice(deviceId)
1117 .withSelector(tpidSelector)
1118 .withTreatment(tpidTreatment)
1119 .makePermanent()
1120 .forTable(EGRESS_TPID_FLOW_TABLE);
1121 rules.add(tpidRuleBuilder.build());
1122 }
1123 }
1124 }
1125 }
1126
1127 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1128 .fromApp(fwd.appId())
1129 .withPriority(fwd.priority())
1130 .forDevice(deviceId)
1131 .withSelector(sb.build())
1132 .withTreatment(tb.build())
1133 .makePermanent()
1134 .forTable(EGRESS_VLAN_FLOW_TABLE);
1135 rules.add(ruleBuilder.build());
1136 return rules;
1137 }
1138
1139 /**
Saurav Das822c4e22015-10-23 10:51:11 -07001140 * In the OF-DPA 2.0 pipeline, versatile forwarding objectives go to the
1141 * ACL table.
1142 * @param fwd the forwarding objective of type 'versatile'
1143 * @return a collection of flow rules to be sent to the switch. An empty
1144 * collection may be returned if there is a problem in processing
1145 * the flow rule
1146 */
Saurav Das52025962016-01-28 22:30:01 -08001147 protected Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
Saurav Das018605f2017-02-18 14:05:44 -08001148 log.debug("Processing versatile forwarding objective:{} in dev:{}",
Saurav Dasd2fded02016-12-02 15:43:47 -08001149 fwd.id(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001150
1151 EthTypeCriterion ethType =
Saurav Das77b5e902016-01-27 17:01:59 -08001152 (EthTypeCriterion) fwd.selector().getCriterion(Criterion.Type.ETH_TYPE);
Saurav Das822c4e22015-10-23 10:51:11 -07001153 if (ethType == null) {
Saurav Dasd2fded02016-12-02 15:43:47 -08001154 log.error("Versatile forwarding objective:{} must include ethType",
1155 fwd.id());
Saurav Das822c4e22015-10-23 10:51:11 -07001156 fail(fwd, ObjectiveError.BADPARAMS);
1157 return Collections.emptySet();
1158 }
1159 if (fwd.nextId() == null && fwd.treatment() == null) {
1160 log.error("Forwarding objective {} from {} must contain "
1161 + "nextId or Treatment", fwd.selector(), fwd.appId());
Zsolt Haraszti9faab752016-02-17 15:55:20 -08001162 fail(fwd, ObjectiveError.BADPARAMS);
Saurav Das822c4e22015-10-23 10:51:11 -07001163 return Collections.emptySet();
1164 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001165
Saurav Das77b5e902016-01-27 17:01:59 -08001166 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
1167 fwd.selector().criteria().forEach(criterion -> {
1168 if (criterion instanceof VlanIdCriterion) {
1169 VlanId vlanId = ((VlanIdCriterion) criterion).vlanId();
1170 // ensure that match does not include vlan = NONE as OF-DPA does not
1171 // match untagged packets this way in the ACL table.
1172 if (vlanId.equals(VlanId.NONE)) {
1173 return;
1174 }
Charles Chand1172632017-03-15 17:33:09 -07001175 if (requireVlanExtensions()) {
1176 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanId);
1177 sbuilder.extension(ofdpaMatchVlanVid, deviceId);
1178 } else {
1179 sbuilder.matchVlanId(vlanId);
1180 }
Charles Chan09bf2692018-01-11 11:48:18 -08001181 } else if (criterion instanceof Icmpv6TypeCriterion) {
1182 byte icmpv6Type = (byte) ((Icmpv6TypeCriterion) criterion).icmpv6Type();
1183 sbuilder.matchIcmpv6Type(icmpv6Type);
1184 } else if (criterion instanceof Icmpv6CodeCriterion) {
1185 byte icmpv6Code = (byte) ((Icmpv6CodeCriterion) criterion).icmpv6Code();
1186 sbuilder.matchIcmpv6Type(icmpv6Code);
Charles Chand9e47c62017-10-05 15:17:15 -07001187 } else if (criterion instanceof TcpPortCriterion || criterion instanceof UdpPortCriterion) {
1188 // FIXME: QMX switches do not support L4 dst port matching in ACL table.
1189 // Currently L4 dst port matching is only used by DHCP relay feature
1190 // and therefore is safe to be replaced with L4 src port matching.
1191 // We need to revisit this if L4 dst port is used for other purpose in the future.
1192 if (!supportIpv6L4Dst() && isIpv6(fwd.selector())) {
Charles Chand9e47c62017-10-05 15:17:15 -07001193 switch (criterion.type()) {
1194 case UDP_DST:
Charles Chand9e47c62017-10-05 15:17:15 -07001195 case UDP_DST_MASKED:
Charles Chand9e47c62017-10-05 15:17:15 -07001196 case TCP_DST:
Charles Chand9e47c62017-10-05 15:17:15 -07001197 case TCP_DST_MASKED:
Charles Chand9e47c62017-10-05 15:17:15 -07001198 break;
1199 default:
1200 sbuilder.add(criterion);
1201 }
1202 } else {
1203 sbuilder.add(criterion);
1204 }
Saurav Das77b5e902016-01-27 17:01:59 -08001205 } else {
1206 sbuilder.add(criterion);
1207 }
1208 });
1209
Saurav Das822c4e22015-10-23 10:51:11 -07001210 // XXX driver does not currently do type checking as per Tables 65-67 in
1211 // OFDPA 2.0 spec. The only allowed treatment is a punt to the controller.
Saurav Das49cb5a12016-01-16 22:54:07 -08001212 TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder();
1213 if (fwd.treatment() != null) {
1214 for (Instruction ins : fwd.treatment().allInstructions()) {
1215 if (ins instanceof OutputInstruction) {
1216 OutputInstruction o = (OutputInstruction) ins;
Ray Milkeyfe0e0852018-01-18 11:14:05 -08001217 if (PortNumber.CONTROLLER.equals(o.port())) {
Saurav Das49cb5a12016-01-16 22:54:07 -08001218 ttBuilder.add(o);
1219 } else {
1220 log.warn("Only allowed treatments in versatile forwarding "
1221 + "objectives are punts to the controller");
1222 }
1223 } else {
1224 log.warn("Cannot process instruction in versatile fwd {}", ins);
1225 }
Saurav Das822c4e22015-10-23 10:51:11 -07001226 }
Charles Chan2df0e8a2017-01-09 11:45:08 -08001227 if (fwd.treatment().clearedDeferred()) {
1228 ttBuilder.wipeDeferred();
1229 }
Saurav Das822c4e22015-10-23 10:51:11 -07001230 }
Saurav Das822c4e22015-10-23 10:51:11 -07001231 if (fwd.nextId() != null) {
Saurav Das49cb5a12016-01-16 22:54:07 -08001232 // overide case
1233 NextGroup next = getGroupForNextObjective(fwd.nextId());
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +05301234 if (next == null) {
1235 fail(fwd, ObjectiveError.BADPARAMS);
1236 return Collections.emptySet();
1237 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001238 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1239 // we only need the top level group's key to point the flow to it
1240 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1241 if (group == null) {
1242 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1243 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1244 fail(fwd, ObjectiveError.GROUPMISSING);
1245 return Collections.emptySet();
1246 }
1247 ttBuilder.deferred().group(group.id());
Saurav Das822c4e22015-10-23 10:51:11 -07001248 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001249
1250 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1251 .fromApp(fwd.appId())
1252 .withPriority(fwd.priority())
1253 .forDevice(deviceId)
Saurav Das77b5e902016-01-27 17:01:59 -08001254 .withSelector(sbuilder.build())
Saurav Das49cb5a12016-01-16 22:54:07 -08001255 .withTreatment(ttBuilder.build())
1256 .makePermanent()
1257 .forTable(ACL_TABLE);
1258 return Collections.singletonList(ruleBuilder.build());
Saurav Das822c4e22015-10-23 10:51:11 -07001259 }
1260
1261 /**
1262 * In the OF-DPA 2.0 pipeline, specific forwarding refers to the IP table
Saurav Das8a0732e2015-11-20 15:27:53 -08001263 * (unicast or multicast) or the L2 table (mac + vlan) or the MPLS table.
Saurav Das822c4e22015-10-23 10:51:11 -07001264 *
1265 * @param fwd the forwarding objective of type 'specific'
1266 * @return a collection of flow rules. Typically there will be only one
1267 * for this type of forwarding objective. An empty set may be
1268 * returned if there is an issue in processing the objective.
1269 */
Charles Chan50d900c2018-03-02 13:26:22 -08001270 private Collection<FlowRule> processSpecific(ForwardingObjective fwd) {
Saurav Das25190812016-05-27 13:54:07 -07001271 log.debug("Processing specific fwd objective:{} in dev:{} with next:{}",
Saurav Das4ce45962015-11-24 23:21:05 -08001272 fwd.id(), deviceId, fwd.nextId());
1273 boolean isEthTypeObj = isSupportedEthTypeObjective(fwd);
1274 boolean isEthDstObj = isSupportedEthDstObjective(fwd);
1275
1276 if (isEthTypeObj) {
1277 return processEthTypeSpecific(fwd);
1278 } else if (isEthDstObj) {
1279 return processEthDstSpecific(fwd);
1280 } else {
1281 log.warn("processSpecific: Unsupported forwarding objective "
1282 + "criteria fwd:{} in dev:{}", fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001283 fail(fwd, ObjectiveError.UNSUPPORTED);
1284 return Collections.emptySet();
1285 }
Saurav Das4ce45962015-11-24 23:21:05 -08001286 }
1287
Saurav Das4ce45962015-11-24 23:21:05 -08001288 /**
1289 * Handles forwarding rules to the IP and MPLS tables.
1290 *
1291 * @param fwd the forwarding objective
1292 * @return A collection of flow rules, or an empty set
1293 */
1294 protected Collection<FlowRule> processEthTypeSpecific(ForwardingObjective fwd) {
Charles Chancad338a2016-09-16 18:03:11 -07001295 return processEthTypeSpecificInternal(fwd, false, ACL_TABLE);
Charles Chanf9e98652016-09-07 16:54:23 -07001296 }
1297
1298 /**
1299 * Internal implementation of processEthTypeSpecific.
1300 * <p>
1301 * Wildcarded IPv4_DST is not supported in OFDPA i12. Therefore, we break
1302 * the rule into 0.0.0.0/1 and 128.0.0.0/1.
1303 * The allowDefaultRoute must be set to false for OFDPA i12.
1304 * </p>
1305 *
1306 * @param fwd the forwarding objective
1307 * @param allowDefaultRoute allow wildcarded IPv4_DST or not
Ray Milkey0bb1e102016-11-10 14:51:27 -08001308 * @param mplsNextTable next MPLS table
Charles Chanf9e98652016-09-07 16:54:23 -07001309 * @return A collection of flow rules, or an empty set
1310 */
1311 protected Collection<FlowRule> processEthTypeSpecificInternal(ForwardingObjective fwd,
Charles Chancad338a2016-09-16 18:03:11 -07001312 boolean allowDefaultRoute,
1313 int mplsNextTable) {
Saurav Das4ce45962015-11-24 23:21:05 -08001314 TrafficSelector selector = fwd.selector();
1315 EthTypeCriterion ethType =
1316 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001317 boolean emptyGroup = false;
Charles Chan188ebf52015-12-23 00:15:11 -08001318 int forTableId;
Saurav Das8a0732e2015-11-20 15:27:53 -08001319 TrafficSelector.Builder filteredSelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -08001320 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
Flavio Castroe10fa242016-01-15 12:43:51 -08001321 TrafficSelector.Builder complementarySelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -08001322
Saurav Das8a0732e2015-11-20 15:27:53 -08001323 if (ethType.ethType().toShort() == Ethernet.TYPE_IPV4) {
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001324 if (buildIpv4Selector(filteredSelector, complementarySelector, fwd, allowDefaultRoute) < 0) {
1325 return Collections.emptyList();
1326 }
1327 // We need to set properly the next table
Flavio Castroe10fa242016-01-15 12:43:51 -08001328 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
Charles Chan5b9df8d2016-03-28 22:21:40 -07001329 if (ipv4Dst.isMulticast()) {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001330 forTableId = MULTICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -08001331 } else {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001332 forTableId = UNICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -08001333 }
Charles Chan50d900c2018-03-02 13:26:22 -08001334
Charles Chan236653d2018-03-05 11:28:23 -08001335 // TODO decrementing IP ttl is done automatically for routing, this
Charles Chan50d900c2018-03-02 13:26:22 -08001336 // action is ignored or rejected in ofdpa as it is not fully implemented
Charles Chan236653d2018-03-05 11:28:23 -08001337 // if (fwd.treatment() != null) {
1338 // for (Instruction instr : fwd.treatment().allInstructions()) {
1339 // if (instr instanceof L3ModificationInstruction &&
1340 // ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1341 // tb.deferred().add(instr);
1342 // }
1343 // }
1344 // }
1345
Pier Ventree0ae7a32016-11-23 09:57:42 -08001346 } else if (ethType.ethType().toShort() == Ethernet.TYPE_IPV6) {
1347 if (buildIpv6Selector(filteredSelector, fwd) < 0) {
1348 return Collections.emptyList();
1349 }
Kalhee Kim3d26bf02017-10-04 14:59:11 +00001350 //We need to set the proper next table
1351 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
1352 if (ipv6Dst.isMulticast()) {
1353 forTableId = MULTICAST_ROUTING_TABLE;
1354 } else {
1355 forTableId = UNICAST_ROUTING_TABLE;
1356 }
1357
Charles Chan236653d2018-03-05 11:28:23 -08001358 // TODO decrementing IP ttl is done automatically for routing, this
Charles Chan50d900c2018-03-02 13:26:22 -08001359 // action is ignored or rejected in ofdpa as it is not fully implemented
Charles Chan236653d2018-03-05 11:28:23 -08001360 // if (fwd.treatment() != null) {
1361 // for (Instruction instr : fwd.treatment().allInstructions()) {
1362 // if (instr instanceof L3ModificationInstruction &&
1363 // ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1364 // tb.deferred().add(instr);
1365 // }
1366 // }
1367 // }
Saurav Das8a0732e2015-11-20 15:27:53 -08001368 } else {
1369 filteredSelector
1370 .matchEthType(Ethernet.MPLS_UNICAST)
1371 .matchMplsLabel(((MplsCriterion)
1372 selector.getCriterion(Criterion.Type.MPLS_LABEL)).label());
1373 MplsBosCriterion bos = (MplsBosCriterion) selector
Pier Ventre140a8942016-11-02 07:26:38 -07001374 .getCriterion(MPLS_BOS);
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001375 if (bos != null && requireMplsBosMatch()) {
Saurav Das8a0732e2015-11-20 15:27:53 -08001376 filteredSelector.matchMplsBos(bos.mplsBos());
1377 }
1378 forTableId = MPLS_TABLE_1;
Saurav Das4ce45962015-11-24 23:21:05 -08001379 log.debug("processing MPLS specific forwarding objective {} -> next:{}"
1380 + " in dev {}", fwd.id(), fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001381
Charles Chan14967c22015-12-07 11:11:50 -08001382 if (fwd.treatment() != null) {
1383 for (Instruction instr : fwd.treatment().allInstructions()) {
1384 if (instr instanceof L2ModificationInstruction &&
1385 ((L2ModificationInstruction) instr).subtype() == L2SubType.MPLS_POP) {
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001386 // OF-DPA does not pop in MPLS table in some cases. For the L3 VPN, it requires
Saurav Das9c705342017-01-06 11:36:01 -08001387 // setting the MPLS_TYPE so pop can happen down the pipeline
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001388 if (requireMplsPop()) {
1389 if (mplsNextTable == MPLS_TYPE_TABLE && isNotMplsBos(selector)) {
1390 tb.immediate().popMpls();
1391 }
1392 } else {
1393 // Skip mpls pop action for mpls_unicast label
1394 if (instr instanceof ModMplsHeaderInstruction &&
1395 !((ModMplsHeaderInstruction) instr).ethernetType()
1396 .equals(EtherType.MPLS_UNICAST.ethType())) {
1397 tb.immediate().add(instr);
1398 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001399 }
Charles Chan14967c22015-12-07 11:11:50 -08001400 }
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001401
1402 if (requireMplsTtlModification()) {
1403 if (instr instanceof L3ModificationInstruction &&
1404 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1405 // FIXME Should modify the app to send the correct DEC_MPLS_TTL instruction
1406 tb.immediate().decMplsTtl();
1407 }
1408 if (instr instanceof L3ModificationInstruction &&
1409 ((L3ModificationInstruction) instr).subtype() == L3SubType.TTL_IN) {
1410 tb.immediate().add(instr);
1411 }
Charles Chan14967c22015-12-07 11:11:50 -08001412 }
Saurav Das8a0732e2015-11-20 15:27:53 -08001413 }
1414 }
1415 }
Saurav Das822c4e22015-10-23 10:51:11 -07001416
1417 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001418 NextGroup next = getGroupForNextObjective(fwd.nextId());
1419 if (next != null) {
1420 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1421 // we only need the top level group's key to point the flow to it
1422 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1423 if (group == null) {
1424 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1425 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1426 fail(fwd, ObjectiveError.GROUPMISSING);
1427 return Collections.emptySet();
1428 }
Saurav Dasa4020382018-02-14 14:14:54 -08001429 if (isNotMplsBos(selector) && group.type().equals(SELECT)) {
1430 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
1431 + "is not implemented in OF-DPA yet. Aborting flow {} -> next:{} "
1432 + "in this device {}", fwd.id(), fwd.nextId(), deviceId);
1433 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1434 return Collections.emptySet();
1435 }
Saurav Das423fe2b2015-12-04 10:52:59 -08001436 tb.deferred().group(group.id());
Saurav Dasc568c342018-01-25 09:49:01 -08001437 // retrying flows may be necessary due to bug CORD-554
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001438 if (gkeys.size() == 1 && gkeys.get(0).size() == 1) {
Saurav Dasc568c342018-01-25 09:49:01 -08001439 if (shouldRetry()) {
1440 log.warn("Found empty group 0x{} in dev:{} .. will retry fwd:{}",
1441 Integer.toHexString(group.id().id()), deviceId,
1442 fwd.id());
1443 emptyGroup = true;
1444 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001445 }
Saurav Das25190812016-05-27 13:54:07 -07001446 } else {
1447 log.warn("Cannot find group for nextId:{} in dev:{}. Aborting fwd:{}",
1448 fwd.nextId(), deviceId, fwd.id());
1449 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1450 return Collections.emptySet();
Saurav Das822c4e22015-10-23 10:51:11 -07001451 }
Saurav Das822c4e22015-10-23 10:51:11 -07001452 }
Charles Chancad338a2016-09-16 18:03:11 -07001453
1454 if (forTableId == MPLS_TABLE_1) {
Pier Ventre140a8942016-11-02 07:26:38 -07001455 if (mplsNextTable == MPLS_L3_TYPE_TABLE) {
Charles Chancad338a2016-09-16 18:03:11 -07001456 Ofdpa3SetMplsType setMplsType = new Ofdpa3SetMplsType(Ofdpa3MplsType.L3_PHP);
Saurav Das9c705342017-01-06 11:36:01 -08001457 // set mpls type as apply_action
1458 tb.immediate().extension(setMplsType, deviceId);
Charles Chancad338a2016-09-16 18:03:11 -07001459 }
1460 tb.transition(mplsNextTable);
1461 } else {
1462 tb.transition(ACL_TABLE);
1463 }
1464
Saurav Das822c4e22015-10-23 10:51:11 -07001465 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1466 .fromApp(fwd.appId())
1467 .withPriority(fwd.priority())
1468 .forDevice(deviceId)
Saurav Das8a0732e2015-11-20 15:27:53 -08001469 .withSelector(filteredSelector.build())
1470 .withTreatment(tb.build())
1471 .forTable(forTableId);
Saurav Das822c4e22015-10-23 10:51:11 -07001472
1473 if (fwd.permanent()) {
1474 ruleBuilder.makePermanent();
1475 } else {
1476 ruleBuilder.makeTemporary(fwd.timeout());
1477 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001478 Collection<FlowRule> flowRuleCollection = new ArrayList<>();
1479 flowRuleCollection.add(ruleBuilder.build());
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001480 if (!allowDefaultRoute) {
Pier Ventree0ae7a32016-11-23 09:57:42 -08001481 flowRuleCollection.add(
1482 defaultRoute(fwd, complementarySelector, forTableId, tb)
1483 );
Flavio Castroe10fa242016-01-15 12:43:51 -08001484 log.debug("Default rule 0.0.0.0/0 is being installed two rules");
1485 }
Saurav Dasc568c342018-01-25 09:49:01 -08001486
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001487 if (emptyGroup) {
1488 executorService.schedule(new RetryFlows(fwd, flowRuleCollection),
1489 RETRY_MS, TimeUnit.MILLISECONDS);
1490 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001491 return flowRuleCollection;
Saurav Das822c4e22015-10-23 10:51:11 -07001492 }
1493
Charles Chan50d900c2018-03-02 13:26:22 -08001494 private int buildIpv4Selector(TrafficSelector.Builder builderToUpdate,
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001495 TrafficSelector.Builder extBuilder,
1496 ForwardingObjective fwd,
1497 boolean allowDefaultRoute) {
1498 TrafficSelector selector = fwd.selector();
1499
1500 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
1501 if (ipv4Dst.isMulticast()) {
1502 if (ipv4Dst.prefixLength() != 32) {
1503 log.warn("Multicast specific forwarding objective can only be /32");
1504 fail(fwd, ObjectiveError.BADPARAMS);
1505 return -1;
1506 }
1507 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
1508 if (assignedVlan == null) {
1509 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
1510 fail(fwd, ObjectiveError.BADPARAMS);
1511 return -1;
1512 }
Charles Chand1172632017-03-15 17:33:09 -07001513 if (requireVlanExtensions()) {
1514 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
1515 builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
1516 } else {
1517 builderToUpdate.matchVlanId(assignedVlan);
1518 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001519 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1520 log.debug("processing IPv4 multicast specific forwarding objective {} -> next:{}"
1521 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1522 } else {
1523 if (ipv4Dst.prefixLength() == 0) {
1524 if (allowDefaultRoute) {
1525 // The entire IPV4_DST field is wildcarded intentionally
1526 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4);
1527 } else {
1528 // NOTE: The switch does not support matching 0.0.0.0/0
1529 // Split it into 0.0.0.0/1 and 128.0.0.0/1
1530 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4)
1531 .matchIPDst(IpPrefix.valueOf("0.0.0.0/1"));
1532 extBuilder.matchEthType(Ethernet.TYPE_IPV4)
1533 .matchIPDst(IpPrefix.valueOf("128.0.0.0/1"));
1534 }
1535 } else {
1536 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1537 }
1538 log.debug("processing IPv4 unicast specific forwarding objective {} -> next:{}"
1539 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1540 }
1541 return 0;
1542 }
1543
1544 /**
1545 * Helper method to build Ipv6 selector using the selector provided by
1546 * a forwarding objective.
1547 *
1548 * @param builderToUpdate the builder to update
1549 * @param fwd the selector to read
1550 * @return 0 if the update ends correctly. -1 if the matches
1551 * are not yet supported
1552 */
Charles Chan50d900c2018-03-02 13:26:22 -08001553 int buildIpv6Selector(TrafficSelector.Builder builderToUpdate,
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001554 ForwardingObjective fwd) {
1555
1556 TrafficSelector selector = fwd.selector();
1557
1558 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
1559 if (ipv6Dst.isMulticast()) {
Julia Ferguson65428c32017-08-10 18:15:24 +00001560 if (ipv6Dst.prefixLength() != IpAddress.INET6_BIT_LENGTH) {
1561 log.warn("Multicast specific forwarding objective can only be /128");
1562 fail(fwd, ObjectiveError.BADPARAMS);
1563 return -1;
1564 }
1565 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
1566 if (assignedVlan == null) {
1567 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
1568 fail(fwd, ObjectiveError.BADPARAMS);
1569 return -1;
1570 }
1571 if (requireVlanExtensions()) {
1572 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
1573 builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
1574 } else {
1575 builderToUpdate.matchVlanId(assignedVlan);
1576 }
1577 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6).matchIPv6Dst(ipv6Dst);
1578 log.debug("processing IPv6 multicast specific forwarding objective {} -> next:{}"
1579 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1580 } else {
1581 if (ipv6Dst.prefixLength() != 0) {
1582 builderToUpdate.matchIPv6Dst(ipv6Dst);
1583 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001584 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6);
1585 log.debug("processing IPv6 unicast specific forwarding objective {} -> next:{}"
1586 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Julia Ferguson65428c32017-08-10 18:15:24 +00001587 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001588 return 0;
1589 }
1590
Charles Chan50d900c2018-03-02 13:26:22 -08001591 FlowRule defaultRoute(ForwardingObjective fwd,
Pier Ventree0ae7a32016-11-23 09:57:42 -08001592 TrafficSelector.Builder complementarySelector,
1593 int forTableId,
1594 TrafficTreatment.Builder tb) {
1595 FlowRule.Builder rule = DefaultFlowRule.builder()
1596 .fromApp(fwd.appId())
1597 .withPriority(fwd.priority())
1598 .forDevice(deviceId)
1599 .withSelector(complementarySelector.build())
1600 .withTreatment(tb.build())
1601 .forTable(forTableId);
1602 if (fwd.permanent()) {
1603 rule.makePermanent();
1604 } else {
1605 rule.makeTemporary(fwd.timeout());
1606 }
1607 return rule.build();
1608 }
1609
Saurav Das4ce45962015-11-24 23:21:05 -08001610 /**
1611 * Handles forwarding rules to the L2 bridging table. Flow actions are not
1612 * allowed in the bridging table - instead we use L2 Interface group or
1613 * L2 flood group
1614 *
1615 * @param fwd the forwarding objective
1616 * @return A collection of flow rules, or an empty set
1617 */
1618 protected Collection<FlowRule> processEthDstSpecific(ForwardingObjective fwd) {
1619 List<FlowRule> rules = new ArrayList<>();
1620
1621 // Build filtered selector
1622 TrafficSelector selector = fwd.selector();
1623 EthCriterion ethCriterion = (EthCriterion) selector
1624 .getCriterion(Criterion.Type.ETH_DST);
1625 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) selector
1626 .getCriterion(Criterion.Type.VLAN_VID);
1627
1628 if (vlanIdCriterion == null) {
1629 log.warn("Forwarding objective for bridging requires vlan. Not "
1630 + "installing fwd:{} in dev:{}", fwd.id(), deviceId);
1631 fail(fwd, ObjectiveError.BADPARAMS);
1632 return Collections.emptySet();
1633 }
1634
1635 TrafficSelector.Builder filteredSelectorBuilder =
1636 DefaultTrafficSelector.builder();
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001637
1638 if (!ethCriterion.mac().equals(NONE) &&
1639 !ethCriterion.mac().equals(BROADCAST)) {
Saurav Das4ce45962015-11-24 23:21:05 -08001640 filteredSelectorBuilder.matchEthDst(ethCriterion.mac());
1641 log.debug("processing L2 forwarding objective:{} -> next:{} in dev:{}",
1642 fwd.id(), fwd.nextId(), deviceId);
1643 } else {
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001644 // Use wildcard DST_MAC if the MacAddress is None or Broadcast
Saurav Das4ce45962015-11-24 23:21:05 -08001645 log.debug("processing L2 Broadcast forwarding objective:{} -> next:{} "
1646 + "in dev:{} for vlan:{}",
1647 fwd.id(), fwd.nextId(), deviceId, vlanIdCriterion.vlanId());
1648 }
Charles Chand1172632017-03-15 17:33:09 -07001649 if (requireVlanExtensions()) {
1650 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanIdCriterion.vlanId());
1651 filteredSelectorBuilder.extension(ofdpaMatchVlanVid, deviceId);
1652 } else {
1653 filteredSelectorBuilder.matchVlanId(vlanIdCriterion.vlanId());
1654 }
Saurav Das4ce45962015-11-24 23:21:05 -08001655 TrafficSelector filteredSelector = filteredSelectorBuilder.build();
1656
1657 if (fwd.treatment() != null) {
1658 log.warn("Ignoring traffic treatment in fwd rule {} meant for L2 table"
1659 + "for dev:{}. Expecting only nextId", fwd.id(), deviceId);
1660 }
1661
1662 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1663 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001664 NextGroup next = getGroupForNextObjective(fwd.nextId());
Saurav Das4ce45962015-11-24 23:21:05 -08001665 if (next != null) {
1666 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1667 // we only need the top level group's key to point the flow to it
1668 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1669 if (group != null) {
1670 treatmentBuilder.deferred().group(group.id());
1671 } else {
1672 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1673 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1674 fail(fwd, ObjectiveError.GROUPMISSING);
1675 return Collections.emptySet();
1676 }
1677 }
1678 }
1679 treatmentBuilder.immediate().transition(ACL_TABLE);
1680 TrafficTreatment filteredTreatment = treatmentBuilder.build();
1681
1682 // Build bridging table entries
1683 FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder();
1684 flowRuleBuilder.fromApp(fwd.appId())
1685 .withPriority(fwd.priority())
1686 .forDevice(deviceId)
1687 .withSelector(filteredSelector)
1688 .withTreatment(filteredTreatment)
1689 .forTable(BRIDGING_TABLE);
1690 if (fwd.permanent()) {
1691 flowRuleBuilder.makePermanent();
1692 } else {
1693 flowRuleBuilder.makeTemporary(fwd.timeout());
1694 }
1695 rules.add(flowRuleBuilder.build());
1696 return rules;
1697 }
1698
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001699 //////////////////////////////////////
1700 // Helper Methods and Classes
1701 //////////////////////////////////////
1702
1703 private boolean isSupportedEthTypeObjective(ForwardingObjective fwd) {
1704 TrafficSelector selector = fwd.selector();
1705 EthTypeCriterion ethType = (EthTypeCriterion) selector
1706 .getCriterion(Criterion.Type.ETH_TYPE);
1707 return !((ethType == null) ||
1708 ((ethType.ethType().toShort() != Ethernet.TYPE_IPV4) &&
Pier Ventree0ae7a32016-11-23 09:57:42 -08001709 (ethType.ethType().toShort() != Ethernet.MPLS_UNICAST)) &&
1710 (ethType.ethType().toShort() != Ethernet.TYPE_IPV6));
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001711 }
1712
1713 private boolean isSupportedEthDstObjective(ForwardingObjective fwd) {
1714 TrafficSelector selector = fwd.selector();
1715 EthCriterion ethDst = (EthCriterion) selector
1716 .getCriterion(Criterion.Type.ETH_DST);
1717 VlanIdCriterion vlanId = (VlanIdCriterion) selector
1718 .getCriterion(Criterion.Type.VLAN_VID);
1719 return !(ethDst == null && vlanId == null);
1720 }
1721
Charles Chan50d900c2018-03-02 13:26:22 -08001722 NextGroup getGroupForNextObjective(Integer nextId) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001723 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
1724 if (next != null) {
1725 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1726 if (gkeys != null && !gkeys.isEmpty()) {
1727 return next;
1728 } else {
1729 log.warn("Empty next group found in FlowObjective store for "
1730 + "next-id:{} in dev:{}", nextId, deviceId);
1731 }
1732 } else {
1733 log.warn("next-id {} not found in Flow objective store for dev:{}",
1734 nextId, deviceId);
1735 }
1736 return null;
1737 }
1738
Charles Chan188ebf52015-12-23 00:15:11 -08001739 protected static void pass(Objective obj) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001740 obj.context().ifPresent(context -> context.onSuccess(obj));
Saurav Das822c4e22015-10-23 10:51:11 -07001741 }
1742
Charles Chan188ebf52015-12-23 00:15:11 -08001743 protected static void fail(Objective obj, ObjectiveError error) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001744 obj.context().ifPresent(context -> context.onError(obj, error));
Saurav Das822c4e22015-10-23 10:51:11 -07001745 }
Saurav Das24431192016-03-07 19:13:00 -08001746
Saurav Das24431192016-03-07 19:13:00 -08001747 @Override
1748 public List<String> getNextMappings(NextGroup nextGroup) {
1749 List<String> mappings = new ArrayList<>();
1750 List<Deque<GroupKey>> gkeys = appKryo.deserialize(nextGroup.data());
1751 for (Deque<GroupKey> gkd : gkeys) {
1752 Group lastGroup = null;
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -07001753 StringBuilder gchain = new StringBuilder();
Saurav Das24431192016-03-07 19:13:00 -08001754 for (GroupKey gk : gkd) {
1755 Group g = groupService.getGroup(deviceId, gk);
Saurav Das8be4e3a2016-03-11 17:19:07 -08001756 if (g == null) {
Saurav Das25190812016-05-27 13:54:07 -07001757 gchain.append(" NoGrp").append(" -->");
Saurav Das8be4e3a2016-03-11 17:19:07 -08001758 continue;
1759 }
1760 gchain.append(" 0x").append(Integer.toHexString(g.id().id()))
1761 .append(" -->");
Saurav Das24431192016-03-07 19:13:00 -08001762 lastGroup = g;
1763 }
1764 // add port information for last group in group-chain
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -07001765 List<Instruction> lastGroupIns = new ArrayList<>();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001766 if (lastGroup != null && !lastGroup.buckets().buckets().isEmpty()) {
Saurav Das25190812016-05-27 13:54:07 -07001767 lastGroupIns = lastGroup.buckets().buckets().get(0)
1768 .treatment().allInstructions();
1769 }
1770 for (Instruction i: lastGroupIns) {
Saurav Das24431192016-03-07 19:13:00 -08001771 if (i instanceof OutputInstruction) {
Saurav Das8be4e3a2016-03-11 17:19:07 -08001772 gchain.append(" port:").append(((OutputInstruction) i).port());
Saurav Das24431192016-03-07 19:13:00 -08001773 }
1774 }
Saurav Das8be4e3a2016-03-11 17:19:07 -08001775 mappings.add(gchain.toString());
Saurav Das24431192016-03-07 19:13:00 -08001776 }
1777 return mappings;
1778 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001779
Saurav Dasa4020382018-02-14 14:14:54 -08001780 /**
1781 * Returns true iff the given selector matches on BOS==true, indicating that
1782 * the selector is trying to match on a label that is bottom-of-stack.
1783 *
1784 * @param selector the given match
1785 * @return true iff BoS==true; false if BOS==false, or BOS matching is not
1786 * expressed in the given selector
1787 */
Pier Ventre140a8942016-11-02 07:26:38 -07001788 static boolean isMplsBos(TrafficSelector selector) {
1789 MplsBosCriterion bosCriterion = (MplsBosCriterion) selector.getCriterion(MPLS_BOS);
1790 return bosCriterion != null && bosCriterion.mplsBos();
1791 }
1792
Saurav Dasa4020382018-02-14 14:14:54 -08001793 /**
1794 * Returns true iff the given selector matches on BOS==false, indicating
1795 * that the selector is trying to match on a label that is not the
1796 * bottom-of-stack label.
1797 *
1798 * @param selector the given match
1799 * @return true iff BoS==false;
1800 * false if BOS==true, or BOS matching is not expressed in the given selector
1801 */
Pier Ventre140a8942016-11-02 07:26:38 -07001802 static boolean isNotMplsBos(TrafficSelector selector) {
1803 MplsBosCriterion bosCriterion = (MplsBosCriterion) selector.getCriterion(MPLS_BOS);
1804 return bosCriterion != null && !bosCriterion.mplsBos();
1805 }
1806
Saurav Dasa4020382018-02-14 14:14:54 -08001807 /**
1808 * Returns true iff the forwarding objective includes a treatment to pop the
1809 * MPLS label.
1810 *
1811 * @param fwd the given forwarding objective
1812 * @return true iff mpls pop treatment exists
1813 */
1814 static boolean isMplsPop(ForwardingObjective fwd) {
1815 if (fwd.treatment() != null) {
1816 for (Instruction instr : fwd.treatment().allInstructions()) {
1817 if (instr instanceof L2ModificationInstruction
1818 && ((L2ModificationInstruction) instr)
1819 .subtype() == L2SubType.MPLS_POP) {
1820 return true;
1821 }
1822 }
1823 }
1824 return false;
1825 }
1826
Charles Chand9e47c62017-10-05 15:17:15 -07001827 private static boolean isIpv6(TrafficSelector selector) {
1828 EthTypeCriterion ethTypeCriterion = (EthTypeCriterion) selector.getCriterion(ETH_TYPE);
1829 return ethTypeCriterion != null && ethTypeCriterion.ethType().toShort() == Ethernet.TYPE_IPV6;
1830 }
1831
Charles Chan50d900c2018-03-02 13:26:22 -08001832 static VlanId readVlanFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001833 if (selector == null) {
1834 return null;
1835 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001836 Criterion criterion = selector.getCriterion(Criterion.Type.VLAN_VID);
1837 return (criterion == null)
1838 ? null : ((VlanIdCriterion) criterion).vlanId();
1839 }
1840
Charles Chand9e47c62017-10-05 15:17:15 -07001841 static IpPrefix readIpDstFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001842 if (selector == null) {
1843 return null;
1844 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001845 Criterion criterion = selector.getCriterion(Criterion.Type.IPV4_DST);
1846 return (criterion == null) ? null : ((IPCriterion) criterion).ip();
1847 }
Charles Chand55e84d2016-03-30 17:54:24 -07001848
1849 private static VlanId readVlanFromTreatment(TrafficTreatment treatment) {
Saurav Das59232cf2016-04-27 18:35:50 -07001850 if (treatment == null) {
1851 return null;
1852 }
Charles Chand55e84d2016-03-30 17:54:24 -07001853 for (Instruction i : treatment.allInstructions()) {
1854 if (i instanceof ModVlanIdInstruction) {
1855 return ((ModVlanIdInstruction) i).vlanId();
1856 }
1857 }
1858 return null;
1859 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001860
Jonghwan Hyun800d9d02018-04-09 09:40:50 -07001861 protected static MacAddress readEthDstFromTreatment(TrafficTreatment treatment) {
Charles Chan45b69ab2018-03-02 15:41:41 -08001862 if (treatment == null) {
1863 return null;
1864 }
1865 for (Instruction i : treatment.allInstructions()) {
1866 if (i instanceof ModEtherInstruction) {
1867 ModEtherInstruction modEtherInstruction = (ModEtherInstruction) i;
1868 if (modEtherInstruction.subtype() == L2SubType.ETH_DST) {
1869 return modEtherInstruction.mac();
1870 }
1871 }
1872 }
1873 return null;
1874 }
1875
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001876 /**
1877 * Utility class that retries sending flows a fixed number of times, even if
1878 * some of the attempts are successful. Used only for forwarding objectives.
1879 */
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001880 public final class RetryFlows implements Runnable {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001881 int attempts = MAX_RETRY_ATTEMPTS;
1882 private Collection<FlowRule> retryFlows;
1883 private ForwardingObjective fwd;
1884
Charles Chan50d900c2018-03-02 13:26:22 -08001885 RetryFlows(ForwardingObjective fwd, Collection<FlowRule> retryFlows) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001886 this.fwd = fwd;
1887 this.retryFlows = retryFlows;
1888 }
1889
1890 @Override
1891 public void run() {
1892 log.info("RETRY FLOWS ATTEMPT# {} for fwd:{} rules:{}",
1893 MAX_RETRY_ATTEMPTS - attempts, fwd.id(), retryFlows.size());
1894 sendForward(fwd, retryFlows);
1895 if (--attempts > 0) {
1896 executorService.schedule(this, RETRY_MS, TimeUnit.MILLISECONDS);
1897 }
1898 }
1899 }
1900
Saurav Das822c4e22015-10-23 10:51:11 -07001901}