blob: 73d1d7d25d45659f0e26a8ebd3b5f230c2559ab7 [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;
Charles Chanf76de302018-06-15 18:54:18 -070070import org.onosproject.net.flow.instructions.Instructions.NoActionInstruction;
Saurav Das822c4e22015-10-23 10:51:11 -070071import org.onosproject.net.flow.instructions.L2ModificationInstruction;
Saurav Das8a0732e2015-11-20 15:27:53 -080072import org.onosproject.net.flow.instructions.L2ModificationInstruction.L2SubType;
Saurav Das822c4e22015-10-23 10:51:11 -070073import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanIdInstruction;
Charles Chan45b69ab2018-03-02 15:41:41 -080074import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModEtherInstruction;
Alex Yashchuk4caa8e82017-12-08 17:40:05 +020075import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModMplsHeaderInstruction;
Charles Chan14967c22015-12-07 11:11:50 -080076import org.onosproject.net.flow.instructions.L3ModificationInstruction;
77import org.onosproject.net.flow.instructions.L3ModificationInstruction.L3SubType;
Saurav Das822c4e22015-10-23 10:51:11 -070078import org.onosproject.net.flowobjective.FilteringObjective;
79import org.onosproject.net.flowobjective.FlowObjectiveStore;
80import org.onosproject.net.flowobjective.ForwardingObjective;
81import org.onosproject.net.flowobjective.NextObjective;
82import org.onosproject.net.flowobjective.Objective;
83import org.onosproject.net.flowobjective.ObjectiveError;
Saurav Das822c4e22015-10-23 10:51:11 -070084import org.onosproject.net.group.DefaultGroupKey;
85import org.onosproject.net.group.Group;
Saurav Das822c4e22015-10-23 10:51:11 -070086import org.onosproject.net.group.GroupKey;
Saurav Das822c4e22015-10-23 10:51:11 -070087import org.onosproject.net.group.GroupService;
Saurav Das822c4e22015-10-23 10:51:11 -070088import org.onosproject.store.serializers.KryoNamespaces;
89import org.slf4j.Logger;
90
Pier Ventree0ae7a32016-11-23 09:57:42 -080091import java.util.ArrayDeque;
92import java.util.ArrayList;
93import java.util.Collection;
94import java.util.Collections;
95import java.util.Deque;
96import java.util.List;
97import java.util.Objects;
Jonghwan Hyun800d9d02018-04-09 09:40:50 -070098import java.util.Optional;
Pier Ventree0ae7a32016-11-23 09:57:42 -080099import java.util.concurrent.ScheduledExecutorService;
100import java.util.concurrent.TimeUnit;
101
102import static java.util.concurrent.Executors.newScheduledThreadPool;
Yi Tseng3a77b4f2017-02-06 15:02:17 -0800103import static org.onlab.packet.MacAddress.BROADCAST;
Charles Chanb4879a52017-10-20 19:09:16 -0700104import static org.onlab.packet.MacAddress.IPV4_MULTICAST;
105import static org.onlab.packet.MacAddress.IPV6_MULTICAST;
Yi Tseng3a77b4f2017-02-06 15:02:17 -0800106import static org.onlab.packet.MacAddress.NONE;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800107import static org.onlab.util.Tools.groupedThreads;
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700108import static org.onosproject.driver.extensions.Ofdpa3CopyField.OXM_ID_PACKET_REG_1;
109import static org.onosproject.driver.extensions.Ofdpa3CopyField.OXM_ID_VLAN_VID;
Yi Tsengef19de12017-04-24 11:33:05 -0700110import static org.onosproject.driver.pipeline.ofdpa.OfdpaGroupHandlerUtility.*;
Charles Chand9e47c62017-10-05 15:17:15 -0700111import static org.onosproject.net.flow.criteria.Criterion.Type.ETH_TYPE;
Pier Luigi075f1012018-02-01 10:23:12 +0100112import static org.onosproject.net.group.GroupDescription.Type.SELECT;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800113import static org.slf4j.LoggerFactory.getLogger;
Pier Ventre140a8942016-11-02 07:26:38 -0700114import static org.onosproject.net.flow.criteria.Criterion.Type.MPLS_BOS;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800115
Saurav Das822c4e22015-10-23 10:51:11 -0700116/**
117 * Driver for Broadcom's OF-DPA v2.0 TTP.
Saurav Das822c4e22015-10-23 10:51:11 -0700118 */
Charles Chan361154b2016-03-24 10:23:39 -0700119public class Ofdpa2Pipeline extends AbstractHandlerBehaviour implements Pipeliner {
Saurav Das822c4e22015-10-23 10:51:11 -0700120 protected static final int PORT_TABLE = 0;
121 protected static final int VLAN_TABLE = 10;
Pier Ventre42287df2016-11-09 14:17:26 -0800122 protected static final int VLAN_1_TABLE = 11;
123 protected static final int MPLS_L2_PORT_FLOW_TABLE = 13;
124 protected static final int MPLS_L2_PORT_PCP_TRUST_FLOW_TABLE = 16;
Saurav Das822c4e22015-10-23 10:51:11 -0700125 protected static final int TMAC_TABLE = 20;
126 protected static final int UNICAST_ROUTING_TABLE = 30;
127 protected static final int MULTICAST_ROUTING_TABLE = 40;
128 protected static final int MPLS_TABLE_0 = 23;
129 protected static final int MPLS_TABLE_1 = 24;
Pier Ventre140a8942016-11-02 07:26:38 -0700130 protected static final int MPLS_L3_TYPE_TABLE = 27;
131 protected static final int MPLS_TYPE_TABLE = 29;
Saurav Das822c4e22015-10-23 10:51:11 -0700132 protected static final int BRIDGING_TABLE = 50;
133 protected static final int ACL_TABLE = 60;
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700134 protected static final int EGRESS_VLAN_FLOW_TABLE = 210;
135 protected static final int EGRESS_DSCP_PCP_REMARK_FLOW_TABLE = 230;
136 protected static final int EGRESS_TPID_FLOW_TABLE = 235;
Saurav Das822c4e22015-10-23 10:51:11 -0700137 protected static final int MAC_LEARNING_TABLE = 254;
138 protected static final long OFPP_MAX = 0xffffff00L;
139
Saurav Das52025962016-01-28 22:30:01 -0800140 protected static final int HIGHEST_PRIORITY = 0xffff;
Saurav Das2857f382015-11-03 14:39:27 -0800141 protected static final int DEFAULT_PRIORITY = 0x8000;
Saurav Das822c4e22015-10-23 10:51:11 -0700142 protected static final int LOWEST_PRIORITY = 0x0;
143
Pier Ventre42287df2016-11-09 14:17:26 -0800144 protected static final int MPLS_L2_PORT_PRIORITY = 2;
Pier Ventre42287df2016-11-09 14:17:26 -0800145 protected static final int MPLS_TUNNEL_ID_BASE = 0x10000;
146 protected static final int MPLS_TUNNEL_ID_MAX = 0x1FFFF;
147
Pier Ventre70d53ba2016-11-17 22:26:29 -0800148 protected static final int MPLS_UNI_PORT_MAX = 0x0000FFFF;
Pier Ventre70d53ba2016-11-17 22:26:29 -0800149 protected static final int MPLS_NNI_PORT_BASE = 0x00020000;
150 protected static final int MPLS_NNI_PORT_MAX = 0x0002FFFF;
151
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700152 protected static final short ALLOW_VLAN_TRANSLATION = 1;
153 protected static final int COPY_FIELD_NBITS = 12;
154 protected static final int COPY_FIELD_OFFSET = 0;
155
Saurav Das822c4e22015-10-23 10:51:11 -0700156 private final Logger log = getLogger(getClass());
Charles Chan425854b2016-04-11 15:32:12 -0700157 protected ServiceDirectory serviceDirectory;
Saurav Das822c4e22015-10-23 10:51:11 -0700158 protected FlowRuleService flowRuleService;
Charles Chan425854b2016-04-11 15:32:12 -0700159 protected CoreService coreService;
Saurav Das8a0732e2015-11-20 15:27:53 -0800160 protected GroupService groupService;
161 protected FlowObjectiveStore flowObjectiveStore;
Saurav Das822c4e22015-10-23 10:51:11 -0700162 protected DeviceId deviceId;
163 protected ApplicationId driverId;
Saurav Das822c4e22015-10-23 10:51:11 -0700164 protected DeviceService deviceService;
Charles Chan188ebf52015-12-23 00:15:11 -0800165 protected static KryoNamespace appKryo = new KryoNamespace.Builder()
Yi Tsengef19de12017-04-24 11:33:05 -0700166 .register(KryoNamespaces.API)
167 .register(GroupKey.class)
168 .register(DefaultGroupKey.class)
169 .register(OfdpaNextGroup.class)
170 .register(ArrayDeque.class)
171 .build("Ofdpa2Pipeline");
Saurav Das822c4e22015-10-23 10:51:11 -0700172
Charles Chan425854b2016-04-11 15:32:12 -0700173 protected Ofdpa2GroupHandler groupHandler;
Saurav Das822c4e22015-10-23 10:51:11 -0700174
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700175 // flows installations to be retried
Charles Chan50d900c2018-03-02 13:26:22 -0800176 private ScheduledExecutorService executorService
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700177 = newScheduledThreadPool(5, groupedThreads("OfdpaPipeliner", "retry-%d", log));
Charles Chan50d900c2018-03-02 13:26:22 -0800178 private static final int MAX_RETRY_ATTEMPTS = 10;
179 private static final int RETRY_MS = 1000;
Saurav Das4f980082015-11-05 13:39:15 -0800180
Saurav Das822c4e22015-10-23 10:51:11 -0700181 @Override
182 public void init(DeviceId deviceId, PipelinerContext context) {
Saurav Das822c4e22015-10-23 10:51:11 -0700183 this.deviceId = deviceId;
184
Charles Chan425854b2016-04-11 15:32:12 -0700185 serviceDirectory = context.directory();
Saurav Das822c4e22015-10-23 10:51:11 -0700186 coreService = serviceDirectory.get(CoreService.class);
187 flowRuleService = serviceDirectory.get(FlowRuleService.class);
188 groupService = serviceDirectory.get(GroupService.class);
189 flowObjectiveStore = context.store();
Saurav Das822c4e22015-10-23 10:51:11 -0700190 deviceService = serviceDirectory.get(DeviceService.class);
Saurav Das822c4e22015-10-23 10:51:11 -0700191
Charles Chan40132b32017-01-22 00:19:37 -0800192 initDriverId();
193 initGroupHander(context);
Saurav Das822c4e22015-10-23 10:51:11 -0700194
Saurav Das822c4e22015-10-23 10:51:11 -0700195 initializePipeline();
Saurav Das822c4e22015-10-23 10:51:11 -0700196 }
197
Charles Chan40132b32017-01-22 00:19:37 -0800198 protected void initDriverId() {
199 driverId = coreService.registerApplication(
200 "org.onosproject.driver.Ofdpa2Pipeline");
201 }
202
203 protected void initGroupHander(PipelinerContext context) {
204 groupHandler = new Ofdpa2GroupHandler();
205 groupHandler.init(deviceId, context);
206 }
207
Saurav Das822c4e22015-10-23 10:51:11 -0700208 protected void initializePipeline() {
Charles Chan188ebf52015-12-23 00:15:11 -0800209 // OF-DPA does not require initializing the pipeline as it puts default
210 // rules automatically in the hardware. However emulation of OFDPA in
211 // software switches does require table-miss-entries.
Saurav Das822c4e22015-10-23 10:51:11 -0700212 }
213
Charles Chand1172632017-03-15 17:33:09 -0700214 /**
Alex Yashchuk4caa8e82017-12-08 17:40:05 +0200215 * Determines whether this pipeline requires MPLS POP instruction.
216 *
217 * @return true to use MPLS POP instruction
218 */
219 public boolean requireMplsPop() {
220 return true;
221 }
222
223 /**
224 * Determines whether this pipeline requires MPLS BOS match.
225 *
226 * @return true to use MPLS BOS match
227 */
228 public boolean requireMplsBosMatch() {
229 return true;
230 }
231
232 /**
233 * Determines whether this pipeline requires MPLS TTL decrement and copy.
234 *
235 * @return true to use MPLS TTL decrement and copy
236 */
237 public boolean requireMplsTtlModification() {
238 return true;
239 }
240
241 /**
Charles Chand1172632017-03-15 17:33:09 -0700242 * Determines whether this pipeline requires OFDPA match and set VLAN extensions.
243 *
244 * @return true to use the extensions
245 */
246 protected boolean requireVlanExtensions() {
247 return true;
248 }
249
Saurav Das86d13e82017-04-28 17:03:48 -0700250 /**
251 * Determines whether in-port should be matched on in TMAC table rules.
252 *
253 * @return true if match on in-port should be programmed
254 */
255 protected boolean matchInPortTmacTable() {
256 return true;
257 }
258
Charles Chand9e47c62017-10-05 15:17:15 -0700259 /**
260 * Determines whether matching L4 destination port on IPv6 packets is supported in ACL table.
261 *
262 * @return true if matching L4 destination port on IPv6 packets is supported in ACL table.
263 */
264 protected boolean supportIpv6L4Dst() {
265 return true;
266 }
267
Saurav Dasc568c342018-01-25 09:49:01 -0800268 /**
269 * Determines whether this driver should continue to retry flows that point
270 * to empty groups. See CORD-554.
271 *
272 * @return true if the driver should retry flows
273 */
274 protected boolean shouldRetry() {
275 return true;
276 }
277
Charles Chan45b69ab2018-03-02 15:41:41 -0800278 /**
279 * Determines whether this driver requires unicast flow to be installed before multicast flow
280 * in TMAC table.
281 *
282 * @return true if required
283 */
284 protected boolean requireUnicastBeforeMulticast() {
285 return false;
286 }
287
Andrea Campanellad980c6d2018-04-30 11:48:55 +0200288 /**
289 * Determines whether this driver supports installing a clearDeferred action on table 30.
290 *
291 * @return true if required
292 */
293 protected boolean supportsUnicastBlackHole() {
294 return true;
295 }
296
Saurav Das822c4e22015-10-23 10:51:11 -0700297 //////////////////////////////////////
298 // Flow Objectives
299 //////////////////////////////////////
300
301 @Override
302 public void filter(FilteringObjective filteringObjective) {
303 if (filteringObjective.type() == FilteringObjective.Type.PERMIT) {
304 processFilter(filteringObjective,
305 filteringObjective.op() == Objective.Operation.ADD,
306 filteringObjective.appId());
307 } else {
308 // Note that packets that don't match the PERMIT filter are
309 // automatically denied. The DENY filter is used to deny packets
310 // that are otherwise permitted by the PERMIT filter.
311 // Use ACL table flow rules here for DENY filtering objectives
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530312 log.warn("filter objective other than PERMIT currently not supported");
Saurav Das822c4e22015-10-23 10:51:11 -0700313 fail(filteringObjective, ObjectiveError.UNSUPPORTED);
314 }
315 }
316
317 @Override
318 public void forward(ForwardingObjective fwd) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700319 Collection<FlowRule> rules = processForward(fwd);
Saurav Das25190812016-05-27 13:54:07 -0700320 if (rules == null || rules.isEmpty()) {
321 // Assumes fail message has already been generated to the objective
322 // context. Returning here prevents spurious pass message to be
323 // generated by FlowRule service for empty flowOps.
324 return;
325 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700326 sendForward(fwd, rules);
327 }
328
Charles Chan50d900c2018-03-02 13:26:22 -0800329 private void sendForward(ForwardingObjective fwd, Collection<FlowRule> rules) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700330 FlowRuleOperations.Builder flowOpsBuilder = FlowRuleOperations.builder();
Saurav Das822c4e22015-10-23 10:51:11 -0700331 switch (fwd.op()) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700332 case ADD:
333 rules.stream()
334 .filter(Objects::nonNull)
335 .forEach(flowOpsBuilder::add);
Saurav Dasd2fded02016-12-02 15:43:47 -0800336 log.debug("Applying a add fwd-obj {} to sw:{}", fwd.id(), deviceId);
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700337 break;
338 case REMOVE:
339 rules.stream()
340 .filter(Objects::nonNull)
341 .forEach(flowOpsBuilder::remove);
Pier Ventre42287df2016-11-09 14:17:26 -0800342 log.debug("Deleting a flow rule to sw:{}", deviceId);
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700343 break;
344 default:
345 fail(fwd, ObjectiveError.UNKNOWN);
346 log.warn("Unknown forwarding type {}", fwd.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700347 }
348
Saurav Das822c4e22015-10-23 10:51:11 -0700349 flowRuleService.apply(flowOpsBuilder.build(new FlowRuleOperationsContext() {
350 @Override
351 public void onSuccess(FlowRuleOperations ops) {
352 pass(fwd);
353 }
354
355 @Override
356 public void onError(FlowRuleOperations ops) {
357 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
358 }
359 }));
Saurav Das822c4e22015-10-23 10:51:11 -0700360 }
361
362 @Override
363 public void next(NextObjective nextObjective) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800364 NextGroup nextGroup = flowObjectiveStore.getNextGroup(nextObjective.id());
365 switch (nextObjective.op()) {
366 case ADD:
Saurav Das4f980082015-11-05 13:39:15 -0800367 if (nextGroup != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800368 log.warn("Cannot add next {} that already exists in device {}",
369 nextObjective.id(), deviceId);
370 return;
371 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700372 log.debug("Processing NextObjective id {} in dev {} - add group",
Saurav Das8a0732e2015-11-20 15:27:53 -0800373 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700374 groupHandler.addGroup(nextObjective);
Saurav Das8a0732e2015-11-20 15:27:53 -0800375 break;
376 case ADD_TO_EXISTING:
377 if (nextGroup != null) {
Saurav Das1547b3f2017-05-05 17:01:08 -0700378 log.debug("Processing NextObjective id {} in dev {} - add bucket",
Saurav Das8a0732e2015-11-20 15:27:53 -0800379 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700380 groupHandler.addBucketToGroup(nextObjective, nextGroup);
Saurav Das4f980082015-11-05 13:39:15 -0800381 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800382 // it is possible that group-chain has not been fully created yet
Saurav Das423fe2b2015-12-04 10:52:59 -0800383 log.debug("Waiting to add bucket to group for next-id:{} in dev:{}",
384 nextObjective.id(), deviceId);
Yi Tseng47f82dc2017-03-05 22:48:39 -0800385
386 // by design multiple pending bucket is allowed for the group
387 groupHandler.pendingBuckets.compute(nextObjective.id(), (nextId, pendBkts) -> {
388 if (pendBkts == null) {
389 pendBkts = Sets.newHashSet();
390 }
391 pendBkts.add(nextObjective);
392 return pendBkts;
393 });
Saurav Das4f980082015-11-05 13:39:15 -0800394 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800395 break;
396 case REMOVE:
397 if (nextGroup == null) {
398 log.warn("Cannot remove 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 group",
Saurav Das8a0732e2015-11-20 15:27:53 -0800403 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700404 groupHandler.removeGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800405 break;
406 case REMOVE_FROM_EXISTING:
407 if (nextGroup == null) {
408 log.warn("Cannot remove from next {} that does not exist in device {}",
409 nextObjective.id(), deviceId);
410 return;
411 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700412 log.debug("Processing NextObjective id {} in dev {} - remove bucket",
Saurav Das8a0732e2015-11-20 15:27:53 -0800413 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700414 groupHandler.removeBucketFromGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800415 break;
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +0900416 case MODIFY:
417 if (nextGroup == null) {
418 log.warn("Cannot modify next {} that does not exist in device {}",
419 nextObjective.id(), deviceId);
420 return;
421 }
422 log.debug("Processing NextObjective id {} in dev {} - modify bucket",
423 nextObjective.id(), deviceId);
424 groupHandler.modifyBucketFromGroup(nextObjective, nextGroup);
425 break;
Saurav Dasceccf242017-08-03 18:30:35 -0700426 case VERIFY:
427 if (nextGroup == null) {
428 log.warn("Cannot verify next {} that does not exist in device {}",
429 nextObjective.id(), deviceId);
430 return;
431 }
432 log.debug("Processing NextObjective id {} in dev {} - verify",
433 nextObjective.id(), deviceId);
434 groupHandler.verifyGroup(nextObjective, nextGroup);
435 break;
Saurav Das8a0732e2015-11-20 15:27:53 -0800436 default:
Saurav Das4f980082015-11-05 13:39:15 -0800437 log.warn("Unsupported operation {}", nextObjective.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700438 }
439 }
440
441 //////////////////////////////////////
442 // Flow handling
443 //////////////////////////////////////
444
445 /**
Yi Tseng0fdc07e2017-04-24 19:17:08 -0700446 * As per OFDPA 2.0 TTP, filtering of VLAN ids and MAC addresses (for routing)
447 * configured on switch ports happen in different tables.
Saurav Das822c4e22015-10-23 10:51:11 -0700448 *
449 * @param filt the filtering objective
450 * @param install indicates whether to add or remove the objective
451 * @param applicationId the application that sent this objective
452 */
Saurav Das52025962016-01-28 22:30:01 -0800453 protected void processFilter(FilteringObjective filt,
454 boolean install, ApplicationId applicationId) {
Saurav Das822c4e22015-10-23 10:51:11 -0700455 // This driver only processes filtering criteria defined with switch
456 // ports as the key
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530457 PortCriterion portCriterion = null;
Saurav Das822c4e22015-10-23 10:51:11 -0700458 EthCriterion ethCriterion = null;
459 VlanIdCriterion vidCriterion = null;
Saurav Das822c4e22015-10-23 10:51:11 -0700460 if (!filt.key().equals(Criteria.dummy()) &&
461 filt.key().type() == Criterion.Type.IN_PORT) {
462 portCriterion = (PortCriterion) filt.key();
Saurav Das822c4e22015-10-23 10:51:11 -0700463 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530464 if (portCriterion == null) {
465 log.debug("No IN_PORT defined in filtering objective from app: {}",
466 applicationId);
467 } else {
468 log.debug("Received filtering objective for dev/port: {}/{}", deviceId,
469 portCriterion.port());
470 }
Saurav Das822c4e22015-10-23 10:51:11 -0700471 // convert filtering conditions for switch-intfs into flowrules
472 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
473 for (Criterion criterion : filt.conditions()) {
Yi Tseng0fdc07e2017-04-24 19:17:08 -0700474 switch (criterion.type()) {
475 case ETH_DST:
476 case ETH_DST_MASKED:
477 ethCriterion = (EthCriterion) criterion;
478 break;
479 case VLAN_VID:
480 vidCriterion = (VlanIdCriterion) criterion;
481 break;
482 default:
483 log.warn("Unsupported filter {}", criterion);
484 fail(filt, ObjectiveError.UNSUPPORTED);
485 return;
Saurav Das822c4e22015-10-23 10:51:11 -0700486 }
487 }
488
Saurav Das0e99e2b2015-10-28 12:39:42 -0700489 VlanId assignedVlan = null;
Charles Chane849c192016-01-11 18:28:54 -0800490 if (vidCriterion != null) {
Charles Chand55e84d2016-03-30 17:54:24 -0700491 // Use the VLAN in criterion if metadata is not present and the traffic is tagged
Charles Chanc550f2e2017-12-19 19:55:57 -0800492 if (!vidCriterion.vlanId().equals(VlanId.NONE)) {
Charles Chane849c192016-01-11 18:28:54 -0800493 assignedVlan = vidCriterion.vlanId();
Piere5bff482018-03-07 11:42:50 +0100494 }
495 // If the meta VLAN is present let's update the assigned vlan
496 if (filt.meta() != null) {
497 VlanId metaVlan = readVlanFromTreatment(filt.meta());
498 if (metaVlan != null) {
499 assignedVlan = metaVlan;
500 }
Charles Chand55e84d2016-03-30 17:54:24 -0700501 }
Charles Chane849c192016-01-11 18:28:54 -0800502
Charles Chand55e84d2016-03-30 17:54:24 -0700503 if (assignedVlan == null) {
504 log.error("Driver fails to extract VLAN information. "
Frank Wangd8ab0962017-08-11 11:09:30 +0800505 + "Not processing VLAN filters on device {}.", deviceId);
Charles Chand55e84d2016-03-30 17:54:24 -0700506 log.debug("VLAN ID in criterion={}, metadata={}",
507 readVlanFromTreatment(filt.meta()), vidCriterion.vlanId());
508 fail(filt, ObjectiveError.BADPARAMS);
509 return;
Saurav Das0e99e2b2015-10-28 12:39:42 -0700510 }
511 }
512
Yi Tseng3a77b4f2017-02-06 15:02:17 -0800513 if (ethCriterion == null || ethCriterion.mac().equals(NONE)) {
Charles Chan985b12e2016-05-11 19:47:22 -0700514 // NOTE: it is possible that a filtering objective only has vidCriterion
Daniel Ginsburgc1d47e92018-04-30 19:27:19 -0400515 log.debug("filtering objective missing dstMac, won't program TMAC table");
Saurav Das822c4e22015-10-23 10:51:11 -0700516 } else {
Charles Chan45b69ab2018-03-02 15:41:41 -0800517 MacAddress unicastMac = readEthDstFromTreatment(filt.meta());
Charles Chan66291502018-03-02 16:43:28 -0800518 List<List<FlowRule>> allStages = processEthDstFilter(portCriterion, ethCriterion,
Charles Chan45b69ab2018-03-02 15:41:41 -0800519 vidCriterion, assignedVlan, unicastMac, applicationId);
Charles Chan66291502018-03-02 16:43:28 -0800520 for (List<FlowRule> flowRules : allStages) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800521 log.trace("Starting a new flow rule stage for TMAC table flow");
Charles Chan66291502018-03-02 16:43:28 -0800522 ops.newStage();
Charles Chanc550f2e2017-12-19 19:55:57 -0800523
Charles Chan66291502018-03-02 16:43:28 -0800524 for (FlowRule flowRule : flowRules) {
Charles Chan056e0c12018-05-10 22:19:49 +0000525 log.trace("{} flow rules in TMAC table: {} for dev: {}",
Charles Chan66291502018-03-02 16:43:28 -0800526 (install) ? "adding" : "removing", flowRules, deviceId);
Charles Chan66291502018-03-02 16:43:28 -0800527 if (install) {
528 ops = ops.add(flowRule);
Charles Chanc550f2e2017-12-19 19:55:57 -0800529 } else {
Charles Chan66291502018-03-02 16:43:28 -0800530 // NOTE: Only remove TMAC flow when there is no more enabled port within the
531 // same VLAN on this device if TMAC doesn't support matching on in_port.
532 if (matchInPortTmacTable() || (filt.meta() != null && filt.meta().clearedDeferred())) {
533 ops = ops.remove(flowRule);
534 } else {
535 log.debug("Abort TMAC flow removal on {}. Some other ports still share this TMAC flow");
536 }
Charles Chanc550f2e2017-12-19 19:55:57 -0800537 }
538 }
Saurav Das822c4e22015-10-23 10:51:11 -0700539 }
540 }
541
Charles Chan985b12e2016-05-11 19:47:22 -0700542 if (vidCriterion == null) {
543 // NOTE: it is possible that a filtering objective only has ethCriterion
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530544 log.info("filtering objective missing VLAN, cannot program VLAN Table");
Saurav Das822c4e22015-10-23 10:51:11 -0700545 } else {
Charles Chan66291502018-03-02 16:43:28 -0800546 List<List<FlowRule>> allStages = processVlanIdFilter(
Charles Chan14967c22015-12-07 11:11:50 -0800547 portCriterion, vidCriterion, assignedVlan, applicationId);
Charles Chan66291502018-03-02 16:43:28 -0800548 for (List<FlowRule> flowRules : allStages) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800549 log.trace("Starting a new flow rule stage for VLAN table flow");
Charles Chan66291502018-03-02 16:43:28 -0800550 ops.newStage();
Charles Chan14967c22015-12-07 11:11:50 -0800551
Charles Chan66291502018-03-02 16:43:28 -0800552 for (FlowRule flowRule : flowRules) {
553 log.trace("{} flow rules in VLAN table: {} for dev: {}",
554 (install) ? "adding" : "removing", flowRule, deviceId);
555 ops = install ? ops.add(flowRule) : ops.remove(flowRule);
Charles Chand1172632017-03-15 17:33:09 -0700556 }
Saurav Das822c4e22015-10-23 10:51:11 -0700557 }
558 }
559
Saurav Das822c4e22015-10-23 10:51:11 -0700560 // apply filtering flow rules
561 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
562 @Override
563 public void onSuccess(FlowRuleOperations ops) {
Saurav Das018605f2017-02-18 14:05:44 -0800564 log.debug("Applied {} filtering rules in device {}",
Saurav Das822c4e22015-10-23 10:51:11 -0700565 ops.stages().get(0).size(), deviceId);
566 pass(filt);
567 }
568
569 @Override
570 public void onError(FlowRuleOperations ops) {
571 log.info("Failed to apply all filtering rules in dev {}", deviceId);
572 fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED);
573 }
574 }));
575
576 }
577
578 /**
Charles Chand1172632017-03-15 17:33:09 -0700579 * Internal implementation of processVlanIdFilter.
580 * <p>
581 * The is_present bit in set_vlan_vid action is required to be 0 in OFDPA i12.
582 * Since it is non-OF spec, we need an extension treatment for that.
583 * The useVlanExtension must be set to false for OFDPA i12.
584 * </p>
Charles Chan66291502018-03-02 16:43:28 -0800585 * <p>
586 * NOTE: Separate VLAN filtering rules and assignment rules
587 * into different stages in order to guarantee that filtering rules
588 * always go first, as required by OFDPA.
589 * </p>
Saurav Das0e99e2b2015-10-28 12:39:42 -0700590 *
Charles Chanf9e98652016-09-07 16:54:23 -0700591 * @param portCriterion port on device for which this filter is programmed
592 * @param vidCriterion vlan assigned to port, or NONE for untagged
593 * @param assignedVlan assigned vlan-id for untagged packets
594 * @param applicationId for application programming this filter
Charles Chan66291502018-03-02 16:43:28 -0800595 * @return stages of flow rules for port-vlan filters
Saurav Das822c4e22015-10-23 10:51:11 -0700596 */
Charles Chan66291502018-03-02 16:43:28 -0800597 protected List<List<FlowRule>> processVlanIdFilter(PortCriterion portCriterion,
Yi Tsengef19de12017-04-24 11:33:05 -0700598 VlanIdCriterion vidCriterion,
599 VlanId assignedVlan,
600 ApplicationId applicationId) {
Charles Chan66291502018-03-02 16:43:28 -0800601 List<FlowRule> filteringRules = new ArrayList<>();
602 List<FlowRule> assignmentRules = new ArrayList<>();
Saurav Das822c4e22015-10-23 10:51:11 -0700603 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
604 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800605 TrafficSelector.Builder preSelector = null;
606 TrafficTreatment.Builder preTreatment = null;
607
Saurav Das4f980082015-11-05 13:39:15 -0800608 treatment.transition(TMAC_TABLE);
609
Saurav Das822c4e22015-10-23 10:51:11 -0700610 if (vidCriterion.vlanId() == VlanId.NONE) {
611 // untagged packets are assigned vlans
Charles Chand1172632017-03-15 17:33:09 -0700612 preSelector = DefaultTrafficSelector.builder();
613 if (requireVlanExtensions()) {
614 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(VlanId.NONE);
615 selector.extension(ofdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700616 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
617 treatment.extension(ofdpaSetVlanVid, deviceId);
Charles Chand1172632017-03-15 17:33:09 -0700618
619 OfdpaMatchVlanVid preOfdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
620 preSelector.extension(preOfdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700621 } else {
Charles Chand1172632017-03-15 17:33:09 -0700622 selector.matchVlanId(VlanId.NONE);
Charles Chanf9e98652016-09-07 16:54:23 -0700623 treatment.setVlanId(assignedVlan);
Charles Chand1172632017-03-15 17:33:09 -0700624
625 preSelector.matchVlanId(assignedVlan);
626 }
627 preTreatment = DefaultTrafficTreatment.builder().transition(TMAC_TABLE);
628 } else {
629 if (requireVlanExtensions()) {
630 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
631 selector.extension(ofdpaMatchVlanVid, deviceId);
632 } else {
633 selector.matchVlanId(vidCriterion.vlanId());
Charles Chanf9e98652016-09-07 16:54:23 -0700634 }
Charles Chan14967c22015-12-07 11:11:50 -0800635
Charles Chand55e84d2016-03-30 17:54:24 -0700636 if (!assignedVlan.equals(vidCriterion.vlanId())) {
Charles Chand1172632017-03-15 17:33:09 -0700637 if (requireVlanExtensions()) {
Charles Chanc03782d2017-01-31 13:57:55 -0800638 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
639 treatment.extension(ofdpaSetVlanVid, deviceId);
640 } else {
641 treatment.setVlanId(assignedVlan);
642 }
Charles Chand55e84d2016-03-30 17:54:24 -0700643 }
Saurav Das822c4e22015-10-23 10:51:11 -0700644 }
Saurav Das822c4e22015-10-23 10:51:11 -0700645
646 // ofdpa cannot match on ALL portnumber, so we need to use separate
647 // rules for each port.
Charles Chan14967c22015-12-07 11:11:50 -0800648 List<PortNumber> portnums = new ArrayList<>();
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530649 if (portCriterion != null) {
650 if (PortNumber.ALL.equals(portCriterion.port())) {
651 for (Port port : deviceService.getPorts(deviceId)) {
652 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
653 portnums.add(port.number());
654 }
Saurav Das822c4e22015-10-23 10:51:11 -0700655 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530656 } else {
657 portnums.add(portCriterion.port());
Saurav Das822c4e22015-10-23 10:51:11 -0700658 }
659 } else {
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530660 log.warn("Filtering Objective missing Port Criterion . " +
Charles Chan66291502018-03-02 16:43:28 -0800661 "VLAN Table cannot be programmed for {}", deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700662 }
Saurav Das4f980082015-11-05 13:39:15 -0800663
Saurav Das822c4e22015-10-23 10:51:11 -0700664 for (PortNumber pnum : portnums) {
Saurav Das4f980082015-11-05 13:39:15 -0800665 // create rest of flowrule
Saurav Das822c4e22015-10-23 10:51:11 -0700666 selector.matchInPort(pnum);
667 FlowRule rule = DefaultFlowRule.builder()
668 .forDevice(deviceId)
669 .withSelector(selector.build())
670 .withTreatment(treatment.build())
671 .withPriority(DEFAULT_PRIORITY)
672 .fromApp(applicationId)
673 .makePermanent()
674 .forTable(VLAN_TABLE).build();
Charles Chan66291502018-03-02 16:43:28 -0800675 assignmentRules.add(rule);
Charles Chan14967c22015-12-07 11:11:50 -0800676
677 if (preSelector != null) {
678 preSelector.matchInPort(pnum);
679 FlowRule preRule = DefaultFlowRule.builder()
680 .forDevice(deviceId)
681 .withSelector(preSelector.build())
682 .withTreatment(preTreatment.build())
683 .withPriority(DEFAULT_PRIORITY)
684 .fromApp(applicationId)
685 .makePermanent()
686 .forTable(VLAN_TABLE).build();
Charles Chan66291502018-03-02 16:43:28 -0800687 filteringRules.add(preRule);
Charles Chan14967c22015-12-07 11:11:50 -0800688 }
Saurav Das822c4e22015-10-23 10:51:11 -0700689 }
Charles Chan2686dd72018-03-06 22:10:15 -0800690 return ImmutableList.of(filteringRules, assignmentRules);
Saurav Das822c4e22015-10-23 10:51:11 -0700691 }
692
693 /**
694 * Allows routed packets with correct destination MAC to be directed
Charles Chan45b69ab2018-03-02 15:41:41 -0800695 * to unicast routing table, multicast routing table or MPLS forwarding table.
Saurav Das822c4e22015-10-23 10:51:11 -0700696 *
697 * @param portCriterion port on device for which this filter is programmed
698 * @param ethCriterion dstMac of device for which is filter is programmed
699 * @param vidCriterion vlan assigned to port, or NONE for untagged
Saurav Das0e99e2b2015-10-28 12:39:42 -0700700 * @param assignedVlan assigned vlan-id for untagged packets
Charles Chan45b69ab2018-03-02 15:41:41 -0800701 * @param unicastMac some switches require a unicast TMAC flow to be programmed before multicast
702 * TMAC flow. This MAC address will be used for the unicast TMAC flow.
703 * This is unused if the filtering objective is a unicast.
Saurav Das822c4e22015-10-23 10:51:11 -0700704 * @param applicationId for application programming this filter
Charles Chan66291502018-03-02 16:43:28 -0800705 * @return stages of flow rules for port-vlan filters
Saurav Das822c4e22015-10-23 10:51:11 -0700706
707 */
Charles Chan66291502018-03-02 16:43:28 -0800708 protected List<List<FlowRule>> processEthDstFilter(PortCriterion portCriterion,
Saurav Das822c4e22015-10-23 10:51:11 -0700709 EthCriterion ethCriterion,
710 VlanIdCriterion vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700711 VlanId assignedVlan,
Charles Chan45b69ab2018-03-02 15:41:41 -0800712 MacAddress unicastMac,
Saurav Das822c4e22015-10-23 10:51:11 -0700713 ApplicationId applicationId) {
Charles Chan5270ed02016-01-30 23:22:37 -0800714 // Consider PortNumber.ANY as wildcard. Match ETH_DST only
Sivachidambaram Subramanianc598d062017-05-16 22:42:06 +0530715 if (portCriterion != null && PortNumber.ANY.equals(portCriterion.port())) {
Charles Chan5270ed02016-01-30 23:22:37 -0800716 return processEthDstOnlyFilter(ethCriterion, applicationId);
717 }
718
Charles Chan5b9df8d2016-03-28 22:21:40 -0700719 // Multicast MAC
720 if (ethCriterion.mask() != null) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800721 return processMcastEthDstFilter(ethCriterion, assignedVlan, unicastMac, applicationId);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700722 }
723
Saurav Das822c4e22015-10-23 10:51:11 -0700724 //handling untagged packets via assigned VLAN
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530725 if (vidCriterion != null && vidCriterion.vlanId() == VlanId.NONE) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700726 vidCriterion = (VlanIdCriterion) Criteria.matchVlanId(assignedVlan);
Saurav Das822c4e22015-10-23 10:51:11 -0700727 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530728 List<FlowRule> rules = new ArrayList<>();
729 OfdpaMatchVlanVid ofdpaMatchVlanVid = null;
730 if (vidCriterion != null && requireVlanExtensions()) {
731 ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
732 }
Saurav Das822c4e22015-10-23 10:51:11 -0700733 // ofdpa cannot match on ALL portnumber, so we need to use separate
734 // rules for each port.
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -0700735 List<PortNumber> portnums = new ArrayList<>();
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530736 if (portCriterion != null) {
737 if (PortNumber.ALL.equals(portCriterion.port())) {
738 for (Port port : deviceService.getPorts(deviceId)) {
739 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
740 portnums.add(port.number());
741 }
Saurav Das822c4e22015-10-23 10:51:11 -0700742 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530743 } else {
744 portnums.add(portCriterion.port());
745 }
746 for (PortNumber pnum : portnums) {
747 rules.add(buildTmacRuleForIpv4(ethCriterion,
748 vidCriterion,
749 ofdpaMatchVlanVid,
750 applicationId,
751 pnum));
752 rules.add(buildTmacRuleForMpls(ethCriterion,
753 vidCriterion,
754 ofdpaMatchVlanVid,
755 applicationId,
756 pnum));
757 rules.add(buildTmacRuleForIpv6(ethCriterion,
758 vidCriterion,
759 ofdpaMatchVlanVid,
760 applicationId,
761 pnum));
Saurav Das822c4e22015-10-23 10:51:11 -0700762 }
763 } else {
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530764 rules.add(buildTmacRuleForIpv4(ethCriterion,
765 vidCriterion,
766 ofdpaMatchVlanVid,
767 applicationId,
768 null));
769 rules.add(buildTmacRuleForMpls(ethCriterion,
770 vidCriterion,
771 ofdpaMatchVlanVid,
772 applicationId,
773 null));
774 rules.add(buildTmacRuleForIpv6(ethCriterion,
775 vidCriterion,
776 ofdpaMatchVlanVid,
777 applicationId,
778 null));
Saurav Das822c4e22015-10-23 10:51:11 -0700779 }
Charles Chan66291502018-03-02 16:43:28 -0800780 return ImmutableList.of(rules);
Saurav Das822c4e22015-10-23 10:51:11 -0700781 }
782
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530783 /**
784 * Builds TMAC rules for IPv4 packets.
785 *
Charles Chanc550f2e2017-12-19 19:55:57 -0800786 * @param ethCriterion dst mac matching
787 * @param vidCriterion vlan id assigned to the port
788 * @param ofdpaMatchVlanVid OFDPA vlan id matching
789 * @param applicationId application id
790 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530791 * @return TMAC rule for IPV4 packets
792 */
793 private FlowRule buildTmacRuleForIpv4(EthCriterion ethCriterion,
794 VlanIdCriterion vidCriterion,
795 OfdpaMatchVlanVid ofdpaMatchVlanVid,
796 ApplicationId applicationId,
797 PortNumber pnum) {
798 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
799 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
800 if (pnum != null) {
801 if (matchInPortTmacTable()) {
802 selector.matchInPort(pnum);
803 } else {
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800804 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530805 "ignoring the IN_PORT criteria");
806 }
807 }
808 if (vidCriterion != null) {
809 if (requireVlanExtensions()) {
810 selector.extension(ofdpaMatchVlanVid, deviceId);
811 } else {
812 selector.matchVlanId(vidCriterion.vlanId());
813 }
814 }
815 selector.matchEthType(Ethernet.TYPE_IPV4);
816 selector.matchEthDst(ethCriterion.mac());
817 treatment.transition(UNICAST_ROUTING_TABLE);
818 return DefaultFlowRule.builder()
819 .forDevice(deviceId)
820 .withSelector(selector.build())
821 .withTreatment(treatment.build())
822 .withPriority(DEFAULT_PRIORITY)
823 .fromApp(applicationId)
824 .makePermanent()
825 .forTable(TMAC_TABLE).build();
826 }
827
828 /**
829 * Builds TMAC rule for MPLS packets.
830 *
Charles Chanc550f2e2017-12-19 19:55:57 -0800831 * @param ethCriterion dst mac matching
832 * @param vidCriterion vlan id assigned to the port
833 * @param ofdpaMatchVlanVid OFDPA vlan id matching
834 * @param applicationId application id
835 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530836 * @return TMAC rule for MPLS packets
837 */
838 private FlowRule buildTmacRuleForMpls(EthCriterion ethCriterion,
839 VlanIdCriterion vidCriterion,
840 OfdpaMatchVlanVid ofdpaMatchVlanVid,
841 ApplicationId applicationId,
842 PortNumber pnum) {
843 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
844 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
845 if (pnum != null) {
846 if (matchInPortTmacTable()) {
847 selector.matchInPort(pnum);
848 } else {
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800849 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530850 "ignoring the IN_PORT criteria");
851 }
852 }
853 if (vidCriterion != null) {
854 if (requireVlanExtensions()) {
855 selector.extension(ofdpaMatchVlanVid, deviceId);
856 } else {
857 selector.matchVlanId(vidCriterion.vlanId());
858 }
859 }
860 selector.matchEthType(Ethernet.MPLS_UNICAST);
861 selector.matchEthDst(ethCriterion.mac());
862 treatment.transition(MPLS_TABLE_0);
863 return DefaultFlowRule.builder()
864 .forDevice(deviceId)
865 .withSelector(selector.build())
866 .withTreatment(treatment.build())
867 .withPriority(DEFAULT_PRIORITY)
868 .fromApp(applicationId)
869 .makePermanent()
870 .forTable(TMAC_TABLE).build();
871 }
872
873 /**
874 * Builds TMAC rules for IPv6 packets.
875 *
Charles Chanc550f2e2017-12-19 19:55:57 -0800876 * @param ethCriterion dst mac matching
877 * @param vidCriterion vlan id assigned to the port
878 * @param ofdpaMatchVlanVid OFDPA vlan id matching
879 * @param applicationId application id
880 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530881 * @return TMAC rule for IPV6 packets
882 */
883 private FlowRule buildTmacRuleForIpv6(EthCriterion ethCriterion,
884 VlanIdCriterion vidCriterion,
885 OfdpaMatchVlanVid ofdpaMatchVlanVid,
886 ApplicationId applicationId,
887 PortNumber pnum) {
888 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
889 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
890 if (pnum != null) {
891 if (matchInPortTmacTable()) {
892 selector.matchInPort(pnum);
893 } else {
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800894 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530895 "ignoring the IN_PORT criteria");
896 }
897 }
898 if (vidCriterion != null) {
899 if (requireVlanExtensions()) {
900 selector.extension(ofdpaMatchVlanVid, deviceId);
901 } else {
902 selector.matchVlanId(vidCriterion.vlanId());
903 }
904 }
905 selector.matchEthType(Ethernet.TYPE_IPV6);
906 selector.matchEthDst(ethCriterion.mac());
907 treatment.transition(UNICAST_ROUTING_TABLE);
908 return DefaultFlowRule.builder()
909 .forDevice(deviceId)
910 .withSelector(selector.build())
911 .withTreatment(treatment.build())
912 .withPriority(DEFAULT_PRIORITY)
913 .fromApp(applicationId)
914 .makePermanent()
915 .forTable(TMAC_TABLE).build();
916 }
917
Charles Chan66291502018-03-02 16:43:28 -0800918 protected List<List<FlowRule>> processEthDstOnlyFilter(EthCriterion ethCriterion,
Yi Tsengef19de12017-04-24 11:33:05 -0700919 ApplicationId applicationId) {
Pier Luigi0e358632017-01-31 09:35:05 -0800920 ImmutableList.Builder<FlowRule> builder = ImmutableList.builder();
921
Charles Chan5270ed02016-01-30 23:22:37 -0800922 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
923 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
924 selector.matchEthType(Ethernet.TYPE_IPV4);
925 selector.matchEthDst(ethCriterion.mac());
926 treatment.transition(UNICAST_ROUTING_TABLE);
927 FlowRule rule = DefaultFlowRule.builder()
928 .forDevice(deviceId)
929 .withSelector(selector.build())
930 .withTreatment(treatment.build())
931 .withPriority(DEFAULT_PRIORITY)
932 .fromApp(applicationId)
933 .makePermanent()
934 .forTable(TMAC_TABLE).build();
Pier Luigi0e358632017-01-31 09:35:05 -0800935 builder.add(rule);
936
937 selector = DefaultTrafficSelector.builder();
938 treatment = DefaultTrafficTreatment.builder();
939 selector.matchEthType(Ethernet.TYPE_IPV6);
940 selector.matchEthDst(ethCriterion.mac());
941 treatment.transition(UNICAST_ROUTING_TABLE);
942 rule = DefaultFlowRule.builder()
943 .forDevice(deviceId)
944 .withSelector(selector.build())
945 .withTreatment(treatment.build())
946 .withPriority(DEFAULT_PRIORITY)
947 .fromApp(applicationId)
948 .makePermanent()
949 .forTable(TMAC_TABLE).build();
Charles Chan66291502018-03-02 16:43:28 -0800950 return ImmutableList.of(builder.add(rule).build());
Charles Chan5270ed02016-01-30 23:22:37 -0800951 }
952
Charles Chan66291502018-03-02 16:43:28 -0800953 List<List<FlowRule>> processMcastEthDstFilter(EthCriterion ethCriterion,
Charles Chanb4879a52017-10-20 19:09:16 -0700954 VlanId assignedVlan,
Charles Chan45b69ab2018-03-02 15:41:41 -0800955 MacAddress unicastMac,
Yi Tsengef19de12017-04-24 11:33:05 -0700956 ApplicationId applicationId) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800957 ImmutableList.Builder<FlowRule> unicastFlows = ImmutableList.builder();
958 ImmutableList.Builder<FlowRule> multicastFlows = ImmutableList.builder();
959 TrafficSelector.Builder selector;
960 TrafficTreatment.Builder treatment;
Charles Chanb4879a52017-10-20 19:09:16 -0700961 FlowRule rule;
Julia Ferguson65428c32017-08-10 18:15:24 +0000962
Charles Chanb4879a52017-10-20 19:09:16 -0700963 if (IPV4_MULTICAST.equals(ethCriterion.mac())) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800964 if (requireUnicastBeforeMulticast()) {
965 selector = DefaultTrafficSelector.builder();
966 treatment = DefaultTrafficTreatment.builder();
967 selector.matchEthType(Ethernet.TYPE_IPV4);
968 selector.matchEthDst(unicastMac);
Charles Chan056e0c12018-05-10 22:19:49 +0000969 selector.matchVlanId(assignedVlan);
Charles Chan45b69ab2018-03-02 15:41:41 -0800970 treatment.transition(UNICAST_ROUTING_TABLE);
971 rule = DefaultFlowRule.builder()
972 .forDevice(deviceId)
973 .withSelector(selector.build())
974 .withTreatment(treatment.build())
Charles Chan056e0c12018-05-10 22:19:49 +0000975 .withPriority(DEFAULT_PRIORITY)
Charles Chan45b69ab2018-03-02 15:41:41 -0800976 .fromApp(applicationId)
977 .makePermanent()
978 .forTable(TMAC_TABLE).build();
979 unicastFlows.add(rule);
980 }
981
982 selector = DefaultTrafficSelector.builder();
983 treatment = DefaultTrafficTreatment.builder();
Charles Chanb4879a52017-10-20 19:09:16 -0700984 selector.matchEthType(Ethernet.TYPE_IPV4);
985 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
Charles Chan056e0c12018-05-10 22:19:49 +0000986 selector.matchVlanId(assignedVlan);
Charles Chanb4879a52017-10-20 19:09:16 -0700987 treatment.transition(MULTICAST_ROUTING_TABLE);
988 rule = DefaultFlowRule.builder()
989 .forDevice(deviceId)
990 .withSelector(selector.build())
991 .withTreatment(treatment.build())
Charles Chan056e0c12018-05-10 22:19:49 +0000992 .withPriority(DEFAULT_PRIORITY)
Charles Chanb4879a52017-10-20 19:09:16 -0700993 .fromApp(applicationId)
994 .makePermanent()
995 .forTable(TMAC_TABLE).build();
Charles Chan45b69ab2018-03-02 15:41:41 -0800996 multicastFlows.add(rule);
Charles Chanb4879a52017-10-20 19:09:16 -0700997 }
998
999 if (IPV6_MULTICAST.equals(ethCriterion.mac())) {
Charles Chan45b69ab2018-03-02 15:41:41 -08001000 if (requireUnicastBeforeMulticast()) {
1001 selector = DefaultTrafficSelector.builder();
1002 treatment = DefaultTrafficTreatment.builder();
1003 selector.matchEthType(Ethernet.TYPE_IPV6);
1004 selector.matchEthDst(unicastMac);
Charles Chan056e0c12018-05-10 22:19:49 +00001005 selector.matchVlanId(assignedVlan);
Charles Chan45b69ab2018-03-02 15:41:41 -08001006 treatment.transition(UNICAST_ROUTING_TABLE);
1007 rule = DefaultFlowRule.builder()
1008 .forDevice(deviceId)
1009 .withSelector(selector.build())
1010 .withTreatment(treatment.build())
Charles Chan056e0c12018-05-10 22:19:49 +00001011 .withPriority(DEFAULT_PRIORITY)
Charles Chan45b69ab2018-03-02 15:41:41 -08001012 .fromApp(applicationId)
1013 .makePermanent()
1014 .forTable(TMAC_TABLE).build();
1015 unicastFlows.add(rule);
1016 }
1017
Charles Chanb4879a52017-10-20 19:09:16 -07001018 selector = DefaultTrafficSelector.builder();
1019 treatment = DefaultTrafficTreatment.builder();
1020 selector.matchEthType(Ethernet.TYPE_IPV6);
1021 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
Charles Chan056e0c12018-05-10 22:19:49 +00001022 selector.matchVlanId(assignedVlan);
Charles Chanb4879a52017-10-20 19:09:16 -07001023 treatment.transition(MULTICAST_ROUTING_TABLE);
1024 rule = DefaultFlowRule.builder()
1025 .forDevice(deviceId)
1026 .withSelector(selector.build())
1027 .withTreatment(treatment.build())
Charles Chan056e0c12018-05-10 22:19:49 +00001028 .withPriority(DEFAULT_PRIORITY)
Charles Chanb4879a52017-10-20 19:09:16 -07001029 .fromApp(applicationId)
1030 .makePermanent()
1031 .forTable(TMAC_TABLE).build();
Charles Chan45b69ab2018-03-02 15:41:41 -08001032 multicastFlows.add(rule);
Charles Chanb4879a52017-10-20 19:09:16 -07001033 }
Charles Chan45b69ab2018-03-02 15:41:41 -08001034 return ImmutableList.of(unicastFlows.build(), multicastFlows.build());
Charles Chan5b9df8d2016-03-28 22:21:40 -07001035 }
1036
Saurav Das822c4e22015-10-23 10:51:11 -07001037 private Collection<FlowRule> processForward(ForwardingObjective fwd) {
1038 switch (fwd.flag()) {
1039 case SPECIFIC:
1040 return processSpecific(fwd);
1041 case VERSATILE:
1042 return processVersatile(fwd);
Jonghwan Hyun800d9d02018-04-09 09:40:50 -07001043 case EGRESS:
1044 return processEgress(fwd);
Saurav Das822c4e22015-10-23 10:51:11 -07001045 default:
1046 fail(fwd, ObjectiveError.UNKNOWN);
1047 log.warn("Unknown forwarding flag {}", fwd.flag());
1048 }
1049 return Collections.emptySet();
1050 }
1051
1052 /**
Jonghwan Hyun800d9d02018-04-09 09:40:50 -07001053 * In the OF-DPA 2.0 pipeline, egress forwarding objectives go to the
1054 * egress tables.
1055 * @param fwd the forwarding objective of type 'egress'
1056 * @return a collection of flow rules to be sent to the switch. An empty
1057 * collection may be returned if there is a problem in processing
1058 * the flow rule
1059 */
1060 protected Collection<FlowRule> processEgress(ForwardingObjective fwd) {
1061 log.debug("Processing egress forwarding objective:{} in dev:{}",
1062 fwd, deviceId);
1063
1064 List<FlowRule> rules = new ArrayList<>();
1065
1066 // Build selector
1067 TrafficSelector.Builder sb = DefaultTrafficSelector.builder();
1068 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) fwd.selector().getCriterion(Criterion.Type.VLAN_VID);
1069 if (vlanIdCriterion == null) {
1070 log.error("Egress forwarding objective:{} must include vlanId", fwd.id());
1071 fail(fwd, ObjectiveError.BADPARAMS);
1072 return rules;
1073 }
1074
1075 Optional<Instruction> outInstr = fwd.treatment().allInstructions().stream()
1076 .filter(instruction -> instruction instanceof OutputInstruction).findFirst();
1077 if (!outInstr.isPresent()) {
1078 log.error("Egress forwarding objective:{} must include output port", fwd.id());
1079 fail(fwd, ObjectiveError.BADPARAMS);
1080 return rules;
1081 }
1082
1083 PortNumber portNumber = ((OutputInstruction) outInstr.get()).port();
1084
1085 sb.matchVlanId(vlanIdCriterion.vlanId());
1086 OfdpaMatchActsetOutput actsetOutput = new OfdpaMatchActsetOutput(portNumber);
1087 sb.extension(actsetOutput, deviceId);
1088
1089 sb.extension(new OfdpaMatchAllowVlanTranslation(ALLOW_VLAN_TRANSLATION), deviceId);
1090
1091 // Build a flow rule for Egress VLAN Flow table
1092 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
1093 tb.transition(EGRESS_DSCP_PCP_REMARK_FLOW_TABLE);
1094 if (fwd.treatment() != null) {
1095 for (Instruction instr : fwd.treatment().allInstructions()) {
1096 if (instr instanceof L2ModificationInstruction &&
1097 ((L2ModificationInstruction) instr).subtype() == L2SubType.VLAN_ID) {
1098 tb.immediate().add(instr);
1099 }
1100 if (instr instanceof L2ModificationInstruction &&
1101 ((L2ModificationInstruction) instr).subtype() == L2SubType.VLAN_PUSH) {
1102 tb.immediate().pushVlan();
1103 EthType ethType = ((L2ModificationInstruction.ModVlanHeaderInstruction) instr).ethernetType();
1104 if (ethType.equals(EtherType.QINQ.ethType())) {
1105 // Build a flow rule for Egress TPID Flow table
1106 TrafficSelector tpidSelector = DefaultTrafficSelector.builder()
1107 .extension(actsetOutput, deviceId)
1108 .matchVlanId(VlanId.ANY).build();
1109
1110 TrafficTreatment tpidTreatment = DefaultTrafficTreatment.builder()
1111 .extension(new Ofdpa3CopyField(COPY_FIELD_NBITS, COPY_FIELD_OFFSET,
1112 COPY_FIELD_OFFSET, OXM_ID_VLAN_VID,
1113 OXM_ID_PACKET_REG_1),
1114 deviceId)
1115 .popVlan()
1116 .pushVlan(EtherType.QINQ.ethType())
1117 .extension(new Ofdpa3CopyField(COPY_FIELD_NBITS, COPY_FIELD_OFFSET,
1118 COPY_FIELD_OFFSET, OXM_ID_PACKET_REG_1,
1119 OXM_ID_VLAN_VID),
1120 deviceId)
1121 .build();
1122
1123 FlowRule.Builder tpidRuleBuilder = DefaultFlowRule.builder()
1124 .fromApp(fwd.appId())
1125 .withPriority(fwd.priority())
1126 .forDevice(deviceId)
1127 .withSelector(tpidSelector)
1128 .withTreatment(tpidTreatment)
1129 .makePermanent()
1130 .forTable(EGRESS_TPID_FLOW_TABLE);
1131 rules.add(tpidRuleBuilder.build());
1132 }
1133 }
1134 }
1135 }
1136
1137 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1138 .fromApp(fwd.appId())
1139 .withPriority(fwd.priority())
1140 .forDevice(deviceId)
1141 .withSelector(sb.build())
1142 .withTreatment(tb.build())
1143 .makePermanent()
1144 .forTable(EGRESS_VLAN_FLOW_TABLE);
1145 rules.add(ruleBuilder.build());
1146 return rules;
1147 }
1148
1149 /**
Saurav Das822c4e22015-10-23 10:51:11 -07001150 * In the OF-DPA 2.0 pipeline, versatile forwarding objectives go to the
1151 * ACL table.
1152 * @param fwd the forwarding objective of type 'versatile'
1153 * @return a collection of flow rules to be sent to the switch. An empty
1154 * collection may be returned if there is a problem in processing
1155 * the flow rule
1156 */
Saurav Das52025962016-01-28 22:30:01 -08001157 protected Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
Saurav Das018605f2017-02-18 14:05:44 -08001158 log.debug("Processing versatile forwarding objective:{} in dev:{}",
Saurav Dasd2fded02016-12-02 15:43:47 -08001159 fwd.id(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001160
Saurav Das822c4e22015-10-23 10:51:11 -07001161 if (fwd.nextId() == null && fwd.treatment() == null) {
1162 log.error("Forwarding objective {} from {} must contain "
1163 + "nextId or Treatment", fwd.selector(), fwd.appId());
Zsolt Haraszti9faab752016-02-17 15:55:20 -08001164 fail(fwd, ObjectiveError.BADPARAMS);
Saurav Das822c4e22015-10-23 10:51:11 -07001165 return Collections.emptySet();
1166 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001167
Saurav Das77b5e902016-01-27 17:01:59 -08001168 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
1169 fwd.selector().criteria().forEach(criterion -> {
1170 if (criterion instanceof VlanIdCriterion) {
1171 VlanId vlanId = ((VlanIdCriterion) criterion).vlanId();
1172 // ensure that match does not include vlan = NONE as OF-DPA does not
1173 // match untagged packets this way in the ACL table.
1174 if (vlanId.equals(VlanId.NONE)) {
1175 return;
1176 }
Charles Chand1172632017-03-15 17:33:09 -07001177 if (requireVlanExtensions()) {
1178 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanId);
1179 sbuilder.extension(ofdpaMatchVlanVid, deviceId);
1180 } else {
1181 sbuilder.matchVlanId(vlanId);
1182 }
Charles Chan09bf2692018-01-11 11:48:18 -08001183 } else if (criterion instanceof Icmpv6TypeCriterion) {
1184 byte icmpv6Type = (byte) ((Icmpv6TypeCriterion) criterion).icmpv6Type();
1185 sbuilder.matchIcmpv6Type(icmpv6Type);
1186 } else if (criterion instanceof Icmpv6CodeCriterion) {
1187 byte icmpv6Code = (byte) ((Icmpv6CodeCriterion) criterion).icmpv6Code();
1188 sbuilder.matchIcmpv6Type(icmpv6Code);
Charles Chand9e47c62017-10-05 15:17:15 -07001189 } else if (criterion instanceof TcpPortCriterion || criterion instanceof UdpPortCriterion) {
1190 // FIXME: QMX switches do not support L4 dst port matching in ACL table.
1191 // Currently L4 dst port matching is only used by DHCP relay feature
1192 // and therefore is safe to be replaced with L4 src port matching.
1193 // We need to revisit this if L4 dst port is used for other purpose in the future.
1194 if (!supportIpv6L4Dst() && isIpv6(fwd.selector())) {
Charles Chand9e47c62017-10-05 15:17:15 -07001195 switch (criterion.type()) {
1196 case UDP_DST:
Charles Chand9e47c62017-10-05 15:17:15 -07001197 case UDP_DST_MASKED:
Charles Chand9e47c62017-10-05 15:17:15 -07001198 case TCP_DST:
Charles Chand9e47c62017-10-05 15:17:15 -07001199 case TCP_DST_MASKED:
Charles Chand9e47c62017-10-05 15:17:15 -07001200 break;
1201 default:
1202 sbuilder.add(criterion);
1203 }
1204 } else {
1205 sbuilder.add(criterion);
1206 }
Saurav Das77b5e902016-01-27 17:01:59 -08001207 } else {
1208 sbuilder.add(criterion);
1209 }
1210 });
1211
Saurav Das822c4e22015-10-23 10:51:11 -07001212 // XXX driver does not currently do type checking as per Tables 65-67 in
1213 // OFDPA 2.0 spec. The only allowed treatment is a punt to the controller.
Saurav Das49cb5a12016-01-16 22:54:07 -08001214 TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder();
1215 if (fwd.treatment() != null) {
1216 for (Instruction ins : fwd.treatment().allInstructions()) {
1217 if (ins instanceof OutputInstruction) {
1218 OutputInstruction o = (OutputInstruction) ins;
Ray Milkeyfe0e0852018-01-18 11:14:05 -08001219 if (PortNumber.CONTROLLER.equals(o.port())) {
Saurav Das49cb5a12016-01-16 22:54:07 -08001220 ttBuilder.add(o);
1221 } else {
1222 log.warn("Only allowed treatments in versatile forwarding "
1223 + "objectives are punts to the controller");
1224 }
Charles Chanf76de302018-06-15 18:54:18 -07001225 } else if (ins instanceof NoActionInstruction) {
1226 // No action is allowed and nothing needs to be done
Saurav Das49cb5a12016-01-16 22:54:07 -08001227 } else {
1228 log.warn("Cannot process instruction in versatile fwd {}", ins);
1229 }
Saurav Das822c4e22015-10-23 10:51:11 -07001230 }
Charles Chan2df0e8a2017-01-09 11:45:08 -08001231 if (fwd.treatment().clearedDeferred()) {
1232 ttBuilder.wipeDeferred();
1233 }
Saurav Das822c4e22015-10-23 10:51:11 -07001234 }
Saurav Das822c4e22015-10-23 10:51:11 -07001235 if (fwd.nextId() != null) {
Saurav Das49cb5a12016-01-16 22:54:07 -08001236 // overide case
1237 NextGroup next = getGroupForNextObjective(fwd.nextId());
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +05301238 if (next == null) {
1239 fail(fwd, ObjectiveError.BADPARAMS);
1240 return Collections.emptySet();
1241 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001242 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1243 // we only need the top level group's key to point the flow to it
1244 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1245 if (group == null) {
1246 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1247 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1248 fail(fwd, ObjectiveError.GROUPMISSING);
1249 return Collections.emptySet();
1250 }
1251 ttBuilder.deferred().group(group.id());
Saurav Das822c4e22015-10-23 10:51:11 -07001252 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001253
1254 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1255 .fromApp(fwd.appId())
1256 .withPriority(fwd.priority())
1257 .forDevice(deviceId)
Saurav Das77b5e902016-01-27 17:01:59 -08001258 .withSelector(sbuilder.build())
Saurav Das49cb5a12016-01-16 22:54:07 -08001259 .withTreatment(ttBuilder.build())
1260 .makePermanent()
1261 .forTable(ACL_TABLE);
1262 return Collections.singletonList(ruleBuilder.build());
Saurav Das822c4e22015-10-23 10:51:11 -07001263 }
1264
1265 /**
1266 * In the OF-DPA 2.0 pipeline, specific forwarding refers to the IP table
Saurav Das8a0732e2015-11-20 15:27:53 -08001267 * (unicast or multicast) or the L2 table (mac + vlan) or the MPLS table.
Saurav Das822c4e22015-10-23 10:51:11 -07001268 *
1269 * @param fwd the forwarding objective of type 'specific'
1270 * @return a collection of flow rules. Typically there will be only one
1271 * for this type of forwarding objective. An empty set may be
1272 * returned if there is an issue in processing the objective.
1273 */
Charles Chan50d900c2018-03-02 13:26:22 -08001274 private Collection<FlowRule> processSpecific(ForwardingObjective fwd) {
Saurav Das25190812016-05-27 13:54:07 -07001275 log.debug("Processing specific fwd objective:{} in dev:{} with next:{}",
Saurav Das4ce45962015-11-24 23:21:05 -08001276 fwd.id(), deviceId, fwd.nextId());
1277 boolean isEthTypeObj = isSupportedEthTypeObjective(fwd);
1278 boolean isEthDstObj = isSupportedEthDstObjective(fwd);
1279
1280 if (isEthTypeObj) {
1281 return processEthTypeSpecific(fwd);
1282 } else if (isEthDstObj) {
1283 return processEthDstSpecific(fwd);
1284 } else {
1285 log.warn("processSpecific: Unsupported forwarding objective "
1286 + "criteria fwd:{} in dev:{}", fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001287 fail(fwd, ObjectiveError.UNSUPPORTED);
1288 return Collections.emptySet();
1289 }
Saurav Das4ce45962015-11-24 23:21:05 -08001290 }
1291
Saurav Das4ce45962015-11-24 23:21:05 -08001292 /**
1293 * Handles forwarding rules to the IP and MPLS tables.
1294 *
1295 * @param fwd the forwarding objective
1296 * @return A collection of flow rules, or an empty set
1297 */
1298 protected Collection<FlowRule> processEthTypeSpecific(ForwardingObjective fwd) {
Charles Chancad338a2016-09-16 18:03:11 -07001299 return processEthTypeSpecificInternal(fwd, false, ACL_TABLE);
Charles Chanf9e98652016-09-07 16:54:23 -07001300 }
1301
1302 /**
1303 * Internal implementation of processEthTypeSpecific.
1304 * <p>
1305 * Wildcarded IPv4_DST is not supported in OFDPA i12. Therefore, we break
1306 * the rule into 0.0.0.0/1 and 128.0.0.0/1.
1307 * The allowDefaultRoute must be set to false for OFDPA i12.
1308 * </p>
1309 *
1310 * @param fwd the forwarding objective
1311 * @param allowDefaultRoute allow wildcarded IPv4_DST or not
Ray Milkey0bb1e102016-11-10 14:51:27 -08001312 * @param mplsNextTable next MPLS table
Charles Chanf9e98652016-09-07 16:54:23 -07001313 * @return A collection of flow rules, or an empty set
1314 */
1315 protected Collection<FlowRule> processEthTypeSpecificInternal(ForwardingObjective fwd,
Charles Chancad338a2016-09-16 18:03:11 -07001316 boolean allowDefaultRoute,
1317 int mplsNextTable) {
Saurav Das4ce45962015-11-24 23:21:05 -08001318 TrafficSelector selector = fwd.selector();
1319 EthTypeCriterion ethType =
1320 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001321 boolean emptyGroup = false;
Charles Chan188ebf52015-12-23 00:15:11 -08001322 int forTableId;
Saurav Das8a0732e2015-11-20 15:27:53 -08001323 TrafficSelector.Builder filteredSelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -08001324 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
Flavio Castroe10fa242016-01-15 12:43:51 -08001325 TrafficSelector.Builder complementarySelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -08001326
Saurav Das8a0732e2015-11-20 15:27:53 -08001327 if (ethType.ethType().toShort() == Ethernet.TYPE_IPV4) {
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001328 if (buildIpv4Selector(filteredSelector, complementarySelector, fwd, allowDefaultRoute) < 0) {
1329 return Collections.emptyList();
1330 }
1331 // We need to set properly the next table
Flavio Castroe10fa242016-01-15 12:43:51 -08001332 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
Charles Chan5b9df8d2016-03-28 22:21:40 -07001333 if (ipv4Dst.isMulticast()) {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001334 forTableId = MULTICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -08001335 } else {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001336 forTableId = UNICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -08001337 }
Charles Chan50d900c2018-03-02 13:26:22 -08001338
Charles Chan236653d2018-03-05 11:28:23 -08001339 // TODO decrementing IP ttl is done automatically for routing, this
Charles Chan50d900c2018-03-02 13:26:22 -08001340 // action is ignored or rejected in ofdpa as it is not fully implemented
Charles Chan236653d2018-03-05 11:28:23 -08001341 // if (fwd.treatment() != null) {
1342 // for (Instruction instr : fwd.treatment().allInstructions()) {
1343 // if (instr instanceof L3ModificationInstruction &&
1344 // ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1345 // tb.deferred().add(instr);
1346 // }
1347 // }
1348 // }
1349
Pier Ventree0ae7a32016-11-23 09:57:42 -08001350 } else if (ethType.ethType().toShort() == Ethernet.TYPE_IPV6) {
1351 if (buildIpv6Selector(filteredSelector, fwd) < 0) {
1352 return Collections.emptyList();
1353 }
Kalhee Kim3d26bf02017-10-04 14:59:11 +00001354 //We need to set the proper next table
1355 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
1356 if (ipv6Dst.isMulticast()) {
1357 forTableId = MULTICAST_ROUTING_TABLE;
1358 } else {
1359 forTableId = UNICAST_ROUTING_TABLE;
1360 }
1361
Charles Chan236653d2018-03-05 11:28:23 -08001362 // TODO decrementing IP ttl is done automatically for routing, this
Charles Chan50d900c2018-03-02 13:26:22 -08001363 // action is ignored or rejected in ofdpa as it is not fully implemented
Charles Chan236653d2018-03-05 11:28:23 -08001364 // if (fwd.treatment() != null) {
1365 // for (Instruction instr : fwd.treatment().allInstructions()) {
1366 // if (instr instanceof L3ModificationInstruction &&
1367 // ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1368 // tb.deferred().add(instr);
1369 // }
1370 // }
1371 // }
Saurav Das8a0732e2015-11-20 15:27:53 -08001372 } else {
1373 filteredSelector
1374 .matchEthType(Ethernet.MPLS_UNICAST)
1375 .matchMplsLabel(((MplsCriterion)
1376 selector.getCriterion(Criterion.Type.MPLS_LABEL)).label());
1377 MplsBosCriterion bos = (MplsBosCriterion) selector
Pier Ventre140a8942016-11-02 07:26:38 -07001378 .getCriterion(MPLS_BOS);
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001379 if (bos != null && requireMplsBosMatch()) {
Saurav Das8a0732e2015-11-20 15:27:53 -08001380 filteredSelector.matchMplsBos(bos.mplsBos());
1381 }
1382 forTableId = MPLS_TABLE_1;
Saurav Das4ce45962015-11-24 23:21:05 -08001383 log.debug("processing MPLS specific forwarding objective {} -> next:{}"
1384 + " in dev {}", fwd.id(), fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001385
Charles Chan14967c22015-12-07 11:11:50 -08001386 if (fwd.treatment() != null) {
1387 for (Instruction instr : fwd.treatment().allInstructions()) {
1388 if (instr instanceof L2ModificationInstruction &&
1389 ((L2ModificationInstruction) instr).subtype() == L2SubType.MPLS_POP) {
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001390 // OF-DPA does not pop in MPLS table in some cases. For the L3 VPN, it requires
Saurav Das9c705342017-01-06 11:36:01 -08001391 // setting the MPLS_TYPE so pop can happen down the pipeline
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001392 if (requireMplsPop()) {
1393 if (mplsNextTable == MPLS_TYPE_TABLE && isNotMplsBos(selector)) {
1394 tb.immediate().popMpls();
1395 }
1396 } else {
1397 // Skip mpls pop action for mpls_unicast label
1398 if (instr instanceof ModMplsHeaderInstruction &&
1399 !((ModMplsHeaderInstruction) instr).ethernetType()
1400 .equals(EtherType.MPLS_UNICAST.ethType())) {
1401 tb.immediate().add(instr);
1402 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001403 }
Charles Chan14967c22015-12-07 11:11:50 -08001404 }
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001405
1406 if (requireMplsTtlModification()) {
1407 if (instr instanceof L3ModificationInstruction &&
1408 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1409 // FIXME Should modify the app to send the correct DEC_MPLS_TTL instruction
1410 tb.immediate().decMplsTtl();
1411 }
1412 if (instr instanceof L3ModificationInstruction &&
1413 ((L3ModificationInstruction) instr).subtype() == L3SubType.TTL_IN) {
1414 tb.immediate().add(instr);
1415 }
Charles Chan14967c22015-12-07 11:11:50 -08001416 }
Saurav Das8a0732e2015-11-20 15:27:53 -08001417 }
1418 }
1419 }
Saurav Das822c4e22015-10-23 10:51:11 -07001420
1421 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001422 NextGroup next = getGroupForNextObjective(fwd.nextId());
1423 if (next != null) {
1424 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1425 // we only need the top level group's key to point the flow to it
1426 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1427 if (group == null) {
1428 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1429 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1430 fail(fwd, ObjectiveError.GROUPMISSING);
1431 return Collections.emptySet();
1432 }
Saurav Dasa4020382018-02-14 14:14:54 -08001433 if (isNotMplsBos(selector) && group.type().equals(SELECT)) {
1434 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
1435 + "is not implemented in OF-DPA yet. Aborting flow {} -> next:{} "
1436 + "in this device {}", fwd.id(), fwd.nextId(), deviceId);
1437 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1438 return Collections.emptySet();
1439 }
Saurav Das423fe2b2015-12-04 10:52:59 -08001440 tb.deferred().group(group.id());
Saurav Dasc568c342018-01-25 09:49:01 -08001441 // retrying flows may be necessary due to bug CORD-554
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001442 if (gkeys.size() == 1 && gkeys.get(0).size() == 1) {
Saurav Dasc568c342018-01-25 09:49:01 -08001443 if (shouldRetry()) {
1444 log.warn("Found empty group 0x{} in dev:{} .. will retry fwd:{}",
1445 Integer.toHexString(group.id().id()), deviceId,
1446 fwd.id());
1447 emptyGroup = true;
1448 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001449 }
Saurav Das25190812016-05-27 13:54:07 -07001450 } else {
1451 log.warn("Cannot find group for nextId:{} in dev:{}. Aborting fwd:{}",
1452 fwd.nextId(), deviceId, fwd.id());
1453 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1454 return Collections.emptySet();
Saurav Das822c4e22015-10-23 10:51:11 -07001455 }
Saurav Das822c4e22015-10-23 10:51:11 -07001456 }
Charles Chancad338a2016-09-16 18:03:11 -07001457
1458 if (forTableId == MPLS_TABLE_1) {
Pier Ventre140a8942016-11-02 07:26:38 -07001459 if (mplsNextTable == MPLS_L3_TYPE_TABLE) {
Charles Chancad338a2016-09-16 18:03:11 -07001460 Ofdpa3SetMplsType setMplsType = new Ofdpa3SetMplsType(Ofdpa3MplsType.L3_PHP);
Saurav Das9c705342017-01-06 11:36:01 -08001461 // set mpls type as apply_action
1462 tb.immediate().extension(setMplsType, deviceId);
Charles Chancad338a2016-09-16 18:03:11 -07001463 }
1464 tb.transition(mplsNextTable);
1465 } else {
1466 tb.transition(ACL_TABLE);
1467 }
1468
Andrea Campanellad980c6d2018-04-30 11:48:55 +02001469 if (fwd.treatment() != null && fwd.treatment().clearedDeferred()) {
1470 if (supportsUnicastBlackHole()) {
1471 tb.wipeDeferred();
1472 } else {
1473 log.warn("Clear Deferred is not supported Unicast Routing Table on device {}", deviceId);
1474 return Collections.emptySet();
1475 }
1476 }
1477
Saurav Das822c4e22015-10-23 10:51:11 -07001478 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1479 .fromApp(fwd.appId())
1480 .withPriority(fwd.priority())
1481 .forDevice(deviceId)
Saurav Das8a0732e2015-11-20 15:27:53 -08001482 .withSelector(filteredSelector.build())
1483 .withTreatment(tb.build())
1484 .forTable(forTableId);
Saurav Das822c4e22015-10-23 10:51:11 -07001485
1486 if (fwd.permanent()) {
1487 ruleBuilder.makePermanent();
1488 } else {
1489 ruleBuilder.makeTemporary(fwd.timeout());
1490 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001491 Collection<FlowRule> flowRuleCollection = new ArrayList<>();
1492 flowRuleCollection.add(ruleBuilder.build());
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001493 if (!allowDefaultRoute) {
Pier Ventree0ae7a32016-11-23 09:57:42 -08001494 flowRuleCollection.add(
1495 defaultRoute(fwd, complementarySelector, forTableId, tb)
1496 );
Flavio Castroe10fa242016-01-15 12:43:51 -08001497 log.debug("Default rule 0.0.0.0/0 is being installed two rules");
1498 }
Saurav Dasc568c342018-01-25 09:49:01 -08001499
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001500 if (emptyGroup) {
1501 executorService.schedule(new RetryFlows(fwd, flowRuleCollection),
1502 RETRY_MS, TimeUnit.MILLISECONDS);
1503 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001504 return flowRuleCollection;
Saurav Das822c4e22015-10-23 10:51:11 -07001505 }
1506
Charles Chan50d900c2018-03-02 13:26:22 -08001507 private int buildIpv4Selector(TrafficSelector.Builder builderToUpdate,
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001508 TrafficSelector.Builder extBuilder,
1509 ForwardingObjective fwd,
1510 boolean allowDefaultRoute) {
1511 TrafficSelector selector = fwd.selector();
1512
1513 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
1514 if (ipv4Dst.isMulticast()) {
1515 if (ipv4Dst.prefixLength() != 32) {
1516 log.warn("Multicast specific forwarding objective can only be /32");
1517 fail(fwd, ObjectiveError.BADPARAMS);
1518 return -1;
1519 }
1520 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
1521 if (assignedVlan == null) {
1522 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
1523 fail(fwd, ObjectiveError.BADPARAMS);
1524 return -1;
1525 }
Charles Chand1172632017-03-15 17:33:09 -07001526 if (requireVlanExtensions()) {
1527 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
1528 builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
1529 } else {
1530 builderToUpdate.matchVlanId(assignedVlan);
1531 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001532 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1533 log.debug("processing IPv4 multicast specific forwarding objective {} -> next:{}"
1534 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1535 } else {
1536 if (ipv4Dst.prefixLength() == 0) {
1537 if (allowDefaultRoute) {
1538 // The entire IPV4_DST field is wildcarded intentionally
1539 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4);
1540 } else {
1541 // NOTE: The switch does not support matching 0.0.0.0/0
1542 // Split it into 0.0.0.0/1 and 128.0.0.0/1
1543 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4)
1544 .matchIPDst(IpPrefix.valueOf("0.0.0.0/1"));
1545 extBuilder.matchEthType(Ethernet.TYPE_IPV4)
1546 .matchIPDst(IpPrefix.valueOf("128.0.0.0/1"));
1547 }
1548 } else {
1549 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1550 }
1551 log.debug("processing IPv4 unicast specific forwarding objective {} -> next:{}"
1552 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1553 }
1554 return 0;
1555 }
1556
1557 /**
1558 * Helper method to build Ipv6 selector using the selector provided by
1559 * a forwarding objective.
1560 *
1561 * @param builderToUpdate the builder to update
1562 * @param fwd the selector to read
1563 * @return 0 if the update ends correctly. -1 if the matches
1564 * are not yet supported
1565 */
Charles Chan50d900c2018-03-02 13:26:22 -08001566 int buildIpv6Selector(TrafficSelector.Builder builderToUpdate,
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001567 ForwardingObjective fwd) {
1568
1569 TrafficSelector selector = fwd.selector();
1570
1571 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
1572 if (ipv6Dst.isMulticast()) {
Julia Ferguson65428c32017-08-10 18:15:24 +00001573 if (ipv6Dst.prefixLength() != IpAddress.INET6_BIT_LENGTH) {
1574 log.warn("Multicast specific forwarding objective can only be /128");
1575 fail(fwd, ObjectiveError.BADPARAMS);
1576 return -1;
1577 }
1578 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
1579 if (assignedVlan == null) {
1580 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
1581 fail(fwd, ObjectiveError.BADPARAMS);
1582 return -1;
1583 }
1584 if (requireVlanExtensions()) {
1585 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
1586 builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
1587 } else {
1588 builderToUpdate.matchVlanId(assignedVlan);
1589 }
1590 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6).matchIPv6Dst(ipv6Dst);
1591 log.debug("processing IPv6 multicast specific forwarding objective {} -> next:{}"
1592 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1593 } else {
1594 if (ipv6Dst.prefixLength() != 0) {
1595 builderToUpdate.matchIPv6Dst(ipv6Dst);
1596 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001597 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6);
1598 log.debug("processing IPv6 unicast specific forwarding objective {} -> next:{}"
1599 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Julia Ferguson65428c32017-08-10 18:15:24 +00001600 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001601 return 0;
1602 }
1603
Charles Chan50d900c2018-03-02 13:26:22 -08001604 FlowRule defaultRoute(ForwardingObjective fwd,
Pier Ventree0ae7a32016-11-23 09:57:42 -08001605 TrafficSelector.Builder complementarySelector,
1606 int forTableId,
1607 TrafficTreatment.Builder tb) {
1608 FlowRule.Builder rule = DefaultFlowRule.builder()
1609 .fromApp(fwd.appId())
1610 .withPriority(fwd.priority())
1611 .forDevice(deviceId)
1612 .withSelector(complementarySelector.build())
1613 .withTreatment(tb.build())
1614 .forTable(forTableId);
1615 if (fwd.permanent()) {
1616 rule.makePermanent();
1617 } else {
1618 rule.makeTemporary(fwd.timeout());
1619 }
1620 return rule.build();
1621 }
1622
Saurav Das4ce45962015-11-24 23:21:05 -08001623 /**
1624 * Handles forwarding rules to the L2 bridging table. Flow actions are not
1625 * allowed in the bridging table - instead we use L2 Interface group or
1626 * L2 flood group
1627 *
1628 * @param fwd the forwarding objective
1629 * @return A collection of flow rules, or an empty set
1630 */
1631 protected Collection<FlowRule> processEthDstSpecific(ForwardingObjective fwd) {
1632 List<FlowRule> rules = new ArrayList<>();
1633
1634 // Build filtered selector
1635 TrafficSelector selector = fwd.selector();
1636 EthCriterion ethCriterion = (EthCriterion) selector
1637 .getCriterion(Criterion.Type.ETH_DST);
1638 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) selector
1639 .getCriterion(Criterion.Type.VLAN_VID);
1640
1641 if (vlanIdCriterion == null) {
1642 log.warn("Forwarding objective for bridging requires vlan. Not "
1643 + "installing fwd:{} in dev:{}", fwd.id(), deviceId);
1644 fail(fwd, ObjectiveError.BADPARAMS);
1645 return Collections.emptySet();
1646 }
1647
1648 TrafficSelector.Builder filteredSelectorBuilder =
1649 DefaultTrafficSelector.builder();
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001650
1651 if (!ethCriterion.mac().equals(NONE) &&
1652 !ethCriterion.mac().equals(BROADCAST)) {
Saurav Das4ce45962015-11-24 23:21:05 -08001653 filteredSelectorBuilder.matchEthDst(ethCriterion.mac());
1654 log.debug("processing L2 forwarding objective:{} -> next:{} in dev:{}",
1655 fwd.id(), fwd.nextId(), deviceId);
1656 } else {
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001657 // Use wildcard DST_MAC if the MacAddress is None or Broadcast
Saurav Das4ce45962015-11-24 23:21:05 -08001658 log.debug("processing L2 Broadcast forwarding objective:{} -> next:{} "
1659 + "in dev:{} for vlan:{}",
1660 fwd.id(), fwd.nextId(), deviceId, vlanIdCriterion.vlanId());
1661 }
Charles Chand1172632017-03-15 17:33:09 -07001662 if (requireVlanExtensions()) {
1663 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanIdCriterion.vlanId());
1664 filteredSelectorBuilder.extension(ofdpaMatchVlanVid, deviceId);
1665 } else {
1666 filteredSelectorBuilder.matchVlanId(vlanIdCriterion.vlanId());
1667 }
Saurav Das4ce45962015-11-24 23:21:05 -08001668 TrafficSelector filteredSelector = filteredSelectorBuilder.build();
1669
1670 if (fwd.treatment() != null) {
1671 log.warn("Ignoring traffic treatment in fwd rule {} meant for L2 table"
1672 + "for dev:{}. Expecting only nextId", fwd.id(), deviceId);
1673 }
1674
1675 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1676 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001677 NextGroup next = getGroupForNextObjective(fwd.nextId());
Saurav Das4ce45962015-11-24 23:21:05 -08001678 if (next != null) {
1679 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1680 // we only need the top level group's key to point the flow to it
1681 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1682 if (group != null) {
1683 treatmentBuilder.deferred().group(group.id());
1684 } else {
1685 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1686 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1687 fail(fwd, ObjectiveError.GROUPMISSING);
1688 return Collections.emptySet();
1689 }
1690 }
1691 }
1692 treatmentBuilder.immediate().transition(ACL_TABLE);
1693 TrafficTreatment filteredTreatment = treatmentBuilder.build();
1694
1695 // Build bridging table entries
1696 FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder();
1697 flowRuleBuilder.fromApp(fwd.appId())
1698 .withPriority(fwd.priority())
1699 .forDevice(deviceId)
1700 .withSelector(filteredSelector)
1701 .withTreatment(filteredTreatment)
1702 .forTable(BRIDGING_TABLE);
1703 if (fwd.permanent()) {
1704 flowRuleBuilder.makePermanent();
1705 } else {
1706 flowRuleBuilder.makeTemporary(fwd.timeout());
1707 }
1708 rules.add(flowRuleBuilder.build());
1709 return rules;
1710 }
1711
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001712 //////////////////////////////////////
1713 // Helper Methods and Classes
1714 //////////////////////////////////////
1715
1716 private boolean isSupportedEthTypeObjective(ForwardingObjective fwd) {
1717 TrafficSelector selector = fwd.selector();
1718 EthTypeCriterion ethType = (EthTypeCriterion) selector
1719 .getCriterion(Criterion.Type.ETH_TYPE);
1720 return !((ethType == null) ||
1721 ((ethType.ethType().toShort() != Ethernet.TYPE_IPV4) &&
Pier Ventree0ae7a32016-11-23 09:57:42 -08001722 (ethType.ethType().toShort() != Ethernet.MPLS_UNICAST)) &&
1723 (ethType.ethType().toShort() != Ethernet.TYPE_IPV6));
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001724 }
1725
1726 private boolean isSupportedEthDstObjective(ForwardingObjective fwd) {
1727 TrafficSelector selector = fwd.selector();
1728 EthCriterion ethDst = (EthCriterion) selector
1729 .getCriterion(Criterion.Type.ETH_DST);
1730 VlanIdCriterion vlanId = (VlanIdCriterion) selector
1731 .getCriterion(Criterion.Type.VLAN_VID);
1732 return !(ethDst == null && vlanId == null);
1733 }
1734
Charles Chan50d900c2018-03-02 13:26:22 -08001735 NextGroup getGroupForNextObjective(Integer nextId) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001736 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
1737 if (next != null) {
1738 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1739 if (gkeys != null && !gkeys.isEmpty()) {
1740 return next;
1741 } else {
1742 log.warn("Empty next group found in FlowObjective store for "
1743 + "next-id:{} in dev:{}", nextId, deviceId);
1744 }
1745 } else {
1746 log.warn("next-id {} not found in Flow objective store for dev:{}",
1747 nextId, deviceId);
1748 }
1749 return null;
1750 }
1751
Charles Chan188ebf52015-12-23 00:15:11 -08001752 protected static void pass(Objective obj) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001753 obj.context().ifPresent(context -> context.onSuccess(obj));
Saurav Das822c4e22015-10-23 10:51:11 -07001754 }
1755
Charles Chan188ebf52015-12-23 00:15:11 -08001756 protected static void fail(Objective obj, ObjectiveError error) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001757 obj.context().ifPresent(context -> context.onError(obj, error));
Saurav Das822c4e22015-10-23 10:51:11 -07001758 }
Saurav Das24431192016-03-07 19:13:00 -08001759
Saurav Das24431192016-03-07 19:13:00 -08001760 @Override
1761 public List<String> getNextMappings(NextGroup nextGroup) {
1762 List<String> mappings = new ArrayList<>();
1763 List<Deque<GroupKey>> gkeys = appKryo.deserialize(nextGroup.data());
1764 for (Deque<GroupKey> gkd : gkeys) {
1765 Group lastGroup = null;
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -07001766 StringBuilder gchain = new StringBuilder();
Saurav Das24431192016-03-07 19:13:00 -08001767 for (GroupKey gk : gkd) {
1768 Group g = groupService.getGroup(deviceId, gk);
Saurav Das8be4e3a2016-03-11 17:19:07 -08001769 if (g == null) {
Saurav Das25190812016-05-27 13:54:07 -07001770 gchain.append(" NoGrp").append(" -->");
Saurav Das8be4e3a2016-03-11 17:19:07 -08001771 continue;
1772 }
1773 gchain.append(" 0x").append(Integer.toHexString(g.id().id()))
1774 .append(" -->");
Saurav Das24431192016-03-07 19:13:00 -08001775 lastGroup = g;
1776 }
1777 // add port information for last group in group-chain
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -07001778 List<Instruction> lastGroupIns = new ArrayList<>();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001779 if (lastGroup != null && !lastGroup.buckets().buckets().isEmpty()) {
Saurav Das25190812016-05-27 13:54:07 -07001780 lastGroupIns = lastGroup.buckets().buckets().get(0)
1781 .treatment().allInstructions();
1782 }
1783 for (Instruction i: lastGroupIns) {
Saurav Das24431192016-03-07 19:13:00 -08001784 if (i instanceof OutputInstruction) {
Saurav Das8be4e3a2016-03-11 17:19:07 -08001785 gchain.append(" port:").append(((OutputInstruction) i).port());
Saurav Das24431192016-03-07 19:13:00 -08001786 }
1787 }
Saurav Das8be4e3a2016-03-11 17:19:07 -08001788 mappings.add(gchain.toString());
Saurav Das24431192016-03-07 19:13:00 -08001789 }
1790 return mappings;
1791 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001792
Saurav Dasa4020382018-02-14 14:14:54 -08001793 /**
1794 * Returns true iff the given selector matches on BOS==true, indicating that
1795 * the selector is trying to match on a label that is bottom-of-stack.
1796 *
1797 * @param selector the given match
1798 * @return true iff BoS==true; false if BOS==false, or BOS matching is not
1799 * expressed in the given selector
1800 */
Pier Ventre140a8942016-11-02 07:26:38 -07001801 static boolean isMplsBos(TrafficSelector selector) {
1802 MplsBosCriterion bosCriterion = (MplsBosCriterion) selector.getCriterion(MPLS_BOS);
1803 return bosCriterion != null && bosCriterion.mplsBos();
1804 }
1805
Saurav Dasa4020382018-02-14 14:14:54 -08001806 /**
1807 * Returns true iff the given selector matches on BOS==false, indicating
1808 * that the selector is trying to match on a label that is not the
1809 * bottom-of-stack label.
1810 *
1811 * @param selector the given match
1812 * @return true iff BoS==false;
1813 * false if BOS==true, or BOS matching is not expressed in the given selector
1814 */
Pier Ventre140a8942016-11-02 07:26:38 -07001815 static boolean isNotMplsBos(TrafficSelector selector) {
1816 MplsBosCriterion bosCriterion = (MplsBosCriterion) selector.getCriterion(MPLS_BOS);
1817 return bosCriterion != null && !bosCriterion.mplsBos();
1818 }
1819
Saurav Dasa4020382018-02-14 14:14:54 -08001820 /**
1821 * Returns true iff the forwarding objective includes a treatment to pop the
1822 * MPLS label.
1823 *
1824 * @param fwd the given forwarding objective
1825 * @return true iff mpls pop treatment exists
1826 */
1827 static boolean isMplsPop(ForwardingObjective fwd) {
1828 if (fwd.treatment() != null) {
1829 for (Instruction instr : fwd.treatment().allInstructions()) {
1830 if (instr instanceof L2ModificationInstruction
1831 && ((L2ModificationInstruction) instr)
1832 .subtype() == L2SubType.MPLS_POP) {
1833 return true;
1834 }
1835 }
1836 }
1837 return false;
1838 }
1839
Charles Chand9e47c62017-10-05 15:17:15 -07001840 private static boolean isIpv6(TrafficSelector selector) {
1841 EthTypeCriterion ethTypeCriterion = (EthTypeCriterion) selector.getCriterion(ETH_TYPE);
1842 return ethTypeCriterion != null && ethTypeCriterion.ethType().toShort() == Ethernet.TYPE_IPV6;
1843 }
1844
Charles Chan50d900c2018-03-02 13:26:22 -08001845 static VlanId readVlanFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001846 if (selector == null) {
1847 return null;
1848 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001849 Criterion criterion = selector.getCriterion(Criterion.Type.VLAN_VID);
1850 return (criterion == null)
1851 ? null : ((VlanIdCriterion) criterion).vlanId();
1852 }
1853
Charles Chand9e47c62017-10-05 15:17:15 -07001854 static IpPrefix readIpDstFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001855 if (selector == null) {
1856 return null;
1857 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001858 Criterion criterion = selector.getCriterion(Criterion.Type.IPV4_DST);
1859 return (criterion == null) ? null : ((IPCriterion) criterion).ip();
1860 }
Charles Chand55e84d2016-03-30 17:54:24 -07001861
1862 private static VlanId readVlanFromTreatment(TrafficTreatment treatment) {
Saurav Das59232cf2016-04-27 18:35:50 -07001863 if (treatment == null) {
1864 return null;
1865 }
Charles Chand55e84d2016-03-30 17:54:24 -07001866 for (Instruction i : treatment.allInstructions()) {
1867 if (i instanceof ModVlanIdInstruction) {
1868 return ((ModVlanIdInstruction) i).vlanId();
1869 }
1870 }
1871 return null;
1872 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001873
Jonghwan Hyun800d9d02018-04-09 09:40:50 -07001874 protected static MacAddress readEthDstFromTreatment(TrafficTreatment treatment) {
Charles Chan45b69ab2018-03-02 15:41:41 -08001875 if (treatment == null) {
1876 return null;
1877 }
1878 for (Instruction i : treatment.allInstructions()) {
1879 if (i instanceof ModEtherInstruction) {
1880 ModEtherInstruction modEtherInstruction = (ModEtherInstruction) i;
1881 if (modEtherInstruction.subtype() == L2SubType.ETH_DST) {
1882 return modEtherInstruction.mac();
1883 }
1884 }
1885 }
1886 return null;
1887 }
1888
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001889 /**
1890 * Utility class that retries sending flows a fixed number of times, even if
1891 * some of the attempts are successful. Used only for forwarding objectives.
1892 */
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001893 public final class RetryFlows implements Runnable {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001894 int attempts = MAX_RETRY_ATTEMPTS;
1895 private Collection<FlowRule> retryFlows;
1896 private ForwardingObjective fwd;
1897
Charles Chan50d900c2018-03-02 13:26:22 -08001898 RetryFlows(ForwardingObjective fwd, Collection<FlowRule> retryFlows) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001899 this.fwd = fwd;
1900 this.retryFlows = retryFlows;
1901 }
1902
1903 @Override
1904 public void run() {
1905 log.info("RETRY FLOWS ATTEMPT# {} for fwd:{} rules:{}",
1906 MAX_RETRY_ATTEMPTS - attempts, fwd.id(), retryFlows.size());
1907 sendForward(fwd, retryFlows);
1908 if (--attempts > 0) {
1909 executorService.schedule(this, RETRY_MS, TimeUnit.MILLISECONDS);
1910 }
1911 }
1912 }
1913
Saurav Das822c4e22015-10-23 10:51:11 -07001914}