blob: b123accd67870ca5f1b39e7322943d6e1129d8bb [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;
Saurav Das822c4e22015-10-23 10:51:11 -070021import org.onlab.packet.Ethernet;
Julia Fergusond8f145e2017-08-10 18:15:24 +000022import org.onlab.packet.IpAddress;
Charles Chan14967c22015-12-07 11:11:50 -080023import org.onlab.packet.IpPrefix;
Charles Chan30122a72017-10-05 15:17:15 -070024import org.onlab.packet.TpPort;
Saurav Das822c4e22015-10-23 10:51:11 -070025import org.onlab.packet.VlanId;
26import org.onlab.util.KryoNamespace;
27import org.onosproject.core.ApplicationId;
28import org.onosproject.core.CoreService;
Charles Chancad338a2016-09-16 18:03:11 -070029import org.onosproject.driver.extensions.Ofdpa3MplsType;
30import org.onosproject.driver.extensions.Ofdpa3SetMplsType;
Charles Chan14967c22015-12-07 11:11:50 -080031import org.onosproject.driver.extensions.OfdpaMatchVlanVid;
32import org.onosproject.driver.extensions.OfdpaSetVlanVid;
Saurav Das822c4e22015-10-23 10:51:11 -070033import org.onosproject.net.DeviceId;
34import org.onosproject.net.Port;
35import org.onosproject.net.PortNumber;
36import org.onosproject.net.behaviour.NextGroup;
37import org.onosproject.net.behaviour.Pipeliner;
38import org.onosproject.net.behaviour.PipelinerContext;
39import org.onosproject.net.device.DeviceService;
40import org.onosproject.net.driver.AbstractHandlerBehaviour;
41import org.onosproject.net.flow.DefaultFlowRule;
42import org.onosproject.net.flow.DefaultTrafficSelector;
43import org.onosproject.net.flow.DefaultTrafficTreatment;
44import org.onosproject.net.flow.FlowRule;
45import org.onosproject.net.flow.FlowRuleOperations;
46import org.onosproject.net.flow.FlowRuleOperationsContext;
47import org.onosproject.net.flow.FlowRuleService;
48import org.onosproject.net.flow.TrafficSelector;
49import org.onosproject.net.flow.TrafficTreatment;
50import org.onosproject.net.flow.criteria.Criteria;
51import org.onosproject.net.flow.criteria.Criterion;
52import org.onosproject.net.flow.criteria.EthCriterion;
53import org.onosproject.net.flow.criteria.EthTypeCriterion;
Charles Chan14967c22015-12-07 11:11:50 -080054import org.onosproject.net.flow.criteria.ExtensionCriterion;
Saurav Das822c4e22015-10-23 10:51:11 -070055import org.onosproject.net.flow.criteria.IPCriterion;
Pier Ventree0ae7a32016-11-23 09:57:42 -080056import org.onosproject.net.flow.criteria.Icmpv6CodeCriterion;
57import org.onosproject.net.flow.criteria.Icmpv6TypeCriterion;
Saurav Das8a0732e2015-11-20 15:27:53 -080058import org.onosproject.net.flow.criteria.MplsBosCriterion;
59import org.onosproject.net.flow.criteria.MplsCriterion;
Saurav Das822c4e22015-10-23 10:51:11 -070060import org.onosproject.net.flow.criteria.PortCriterion;
Charles Chan30122a72017-10-05 15:17:15 -070061import org.onosproject.net.flow.criteria.TcpPortCriterion;
62import org.onosproject.net.flow.criteria.UdpPortCriterion;
Saurav Das822c4e22015-10-23 10:51:11 -070063import org.onosproject.net.flow.criteria.VlanIdCriterion;
64import org.onosproject.net.flow.instructions.Instruction;
65import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
66import org.onosproject.net.flow.instructions.L2ModificationInstruction;
Saurav Das8a0732e2015-11-20 15:27:53 -080067import org.onosproject.net.flow.instructions.L2ModificationInstruction.L2SubType;
Saurav Das822c4e22015-10-23 10:51:11 -070068import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanIdInstruction;
Charles Chan14967c22015-12-07 11:11:50 -080069import org.onosproject.net.flow.instructions.L3ModificationInstruction;
70import org.onosproject.net.flow.instructions.L3ModificationInstruction.L3SubType;
Saurav Das822c4e22015-10-23 10:51:11 -070071import org.onosproject.net.flowobjective.FilteringObjective;
72import org.onosproject.net.flowobjective.FlowObjectiveStore;
73import org.onosproject.net.flowobjective.ForwardingObjective;
74import org.onosproject.net.flowobjective.NextObjective;
75import org.onosproject.net.flowobjective.Objective;
76import org.onosproject.net.flowobjective.ObjectiveError;
Saurav Das822c4e22015-10-23 10:51:11 -070077import org.onosproject.net.group.DefaultGroupKey;
78import org.onosproject.net.group.Group;
Saurav Das822c4e22015-10-23 10:51:11 -070079import org.onosproject.net.group.GroupKey;
Saurav Das822c4e22015-10-23 10:51:11 -070080import org.onosproject.net.group.GroupService;
Saurav Das822c4e22015-10-23 10:51:11 -070081import org.onosproject.store.serializers.KryoNamespaces;
82import org.slf4j.Logger;
83
Pier Ventree0ae7a32016-11-23 09:57:42 -080084import java.util.ArrayDeque;
85import java.util.ArrayList;
86import java.util.Collection;
87import java.util.Collections;
88import java.util.Deque;
89import java.util.List;
90import java.util.Objects;
Pier Ventree0ae7a32016-11-23 09:57:42 -080091import java.util.concurrent.ScheduledExecutorService;
92import java.util.concurrent.TimeUnit;
93
94import static java.util.concurrent.Executors.newScheduledThreadPool;
Yi Tseng3a77b4f2017-02-06 15:02:17 -080095import static org.onlab.packet.MacAddress.BROADCAST;
96import static org.onlab.packet.MacAddress.NONE;
Pier Ventree0ae7a32016-11-23 09:57:42 -080097import static org.onlab.util.Tools.groupedThreads;
Yi Tsengef19de12017-04-24 11:33:05 -070098import static org.onosproject.driver.pipeline.ofdpa.OfdpaGroupHandlerUtility.*;
Charles Chan30122a72017-10-05 15:17:15 -070099import static org.onosproject.net.flow.criteria.Criterion.Type.ETH_TYPE;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800100import static org.slf4j.LoggerFactory.getLogger;
Pier Ventre140a8942016-11-02 07:26:38 -0700101import static org.onosproject.net.flow.criteria.Criterion.Type.MPLS_BOS;
102import static org.onosproject.net.flowobjective.NextObjective.Type.HASHED;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800103
Saurav Das822c4e22015-10-23 10:51:11 -0700104/**
105 * Driver for Broadcom's OF-DPA v2.0 TTP.
Saurav Das822c4e22015-10-23 10:51:11 -0700106 */
Charles Chan361154b2016-03-24 10:23:39 -0700107public class Ofdpa2Pipeline extends AbstractHandlerBehaviour implements Pipeliner {
Pier Ventre42287df2016-11-09 14:17:26 -0800108
Saurav Das822c4e22015-10-23 10:51:11 -0700109 protected static final int PORT_TABLE = 0;
110 protected static final int VLAN_TABLE = 10;
Pier Ventre42287df2016-11-09 14:17:26 -0800111 protected static final int VLAN_1_TABLE = 11;
112 protected static final int MPLS_L2_PORT_FLOW_TABLE = 13;
113 protected static final int MPLS_L2_PORT_PCP_TRUST_FLOW_TABLE = 16;
Saurav Das822c4e22015-10-23 10:51:11 -0700114 protected static final int TMAC_TABLE = 20;
115 protected static final int UNICAST_ROUTING_TABLE = 30;
116 protected static final int MULTICAST_ROUTING_TABLE = 40;
117 protected static final int MPLS_TABLE_0 = 23;
118 protected static final int MPLS_TABLE_1 = 24;
Pier Ventre140a8942016-11-02 07:26:38 -0700119 protected static final int MPLS_L3_TYPE_TABLE = 27;
120 protected static final int MPLS_TYPE_TABLE = 29;
Saurav Das822c4e22015-10-23 10:51:11 -0700121 protected static final int BRIDGING_TABLE = 50;
122 protected static final int ACL_TABLE = 60;
123 protected static final int MAC_LEARNING_TABLE = 254;
124 protected static final long OFPP_MAX = 0xffffff00L;
125
Saurav Das52025962016-01-28 22:30:01 -0800126 protected static final int HIGHEST_PRIORITY = 0xffff;
Saurav Das2857f382015-11-03 14:39:27 -0800127 protected static final int DEFAULT_PRIORITY = 0x8000;
Saurav Das822c4e22015-10-23 10:51:11 -0700128 protected static final int LOWEST_PRIORITY = 0x0;
129
Pier Ventre42287df2016-11-09 14:17:26 -0800130 protected static final int MPLS_L2_PORT_PRIORITY = 2;
131
132 protected static final int MPLS_TUNNEL_ID_BASE = 0x10000;
133 protected static final int MPLS_TUNNEL_ID_MAX = 0x1FFFF;
134
Pier Ventre70d53ba2016-11-17 22:26:29 -0800135 protected static final int MPLS_UNI_PORT_MAX = 0x0000FFFF;
136
137 protected static final int MPLS_NNI_PORT_BASE = 0x00020000;
138 protected static final int MPLS_NNI_PORT_MAX = 0x0002FFFF;
139
Saurav Das822c4e22015-10-23 10:51:11 -0700140 private final Logger log = getLogger(getClass());
Charles Chan425854b2016-04-11 15:32:12 -0700141 protected ServiceDirectory serviceDirectory;
Saurav Das822c4e22015-10-23 10:51:11 -0700142 protected FlowRuleService flowRuleService;
Charles Chan425854b2016-04-11 15:32:12 -0700143 protected CoreService coreService;
Saurav Das8a0732e2015-11-20 15:27:53 -0800144 protected GroupService groupService;
145 protected FlowObjectiveStore flowObjectiveStore;
Saurav Das822c4e22015-10-23 10:51:11 -0700146 protected DeviceId deviceId;
147 protected ApplicationId driverId;
Saurav Das822c4e22015-10-23 10:51:11 -0700148 protected DeviceService deviceService;
Charles Chan188ebf52015-12-23 00:15:11 -0800149 protected static KryoNamespace appKryo = new KryoNamespace.Builder()
Yi Tsengef19de12017-04-24 11:33:05 -0700150 .register(KryoNamespaces.API)
151 .register(GroupKey.class)
152 .register(DefaultGroupKey.class)
153 .register(OfdpaNextGroup.class)
154 .register(ArrayDeque.class)
155 .build("Ofdpa2Pipeline");
Saurav Das822c4e22015-10-23 10:51:11 -0700156
Charles Chan425854b2016-04-11 15:32:12 -0700157 protected Ofdpa2GroupHandler groupHandler;
Saurav Das822c4e22015-10-23 10:51:11 -0700158
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700159 // flows installations to be retried
160 protected ScheduledExecutorService executorService
161 = newScheduledThreadPool(5, groupedThreads("OfdpaPipeliner", "retry-%d", log));
162 protected static final int MAX_RETRY_ATTEMPTS = 10;
163 protected static final int RETRY_MS = 1000;
Saurav Das4f980082015-11-05 13:39:15 -0800164
Saurav Das822c4e22015-10-23 10:51:11 -0700165 @Override
166 public void init(DeviceId deviceId, PipelinerContext context) {
Saurav Das822c4e22015-10-23 10:51:11 -0700167 this.deviceId = deviceId;
168
Charles Chan425854b2016-04-11 15:32:12 -0700169 serviceDirectory = context.directory();
Saurav Das822c4e22015-10-23 10:51:11 -0700170 coreService = serviceDirectory.get(CoreService.class);
171 flowRuleService = serviceDirectory.get(FlowRuleService.class);
172 groupService = serviceDirectory.get(GroupService.class);
173 flowObjectiveStore = context.store();
Saurav Das822c4e22015-10-23 10:51:11 -0700174 deviceService = serviceDirectory.get(DeviceService.class);
Saurav Das822c4e22015-10-23 10:51:11 -0700175
Charles Chan40132b32017-01-22 00:19:37 -0800176 initDriverId();
177 initGroupHander(context);
Saurav Das822c4e22015-10-23 10:51:11 -0700178
Saurav Das822c4e22015-10-23 10:51:11 -0700179 initializePipeline();
Saurav Das822c4e22015-10-23 10:51:11 -0700180 }
181
Charles Chan40132b32017-01-22 00:19:37 -0800182 protected void initDriverId() {
183 driverId = coreService.registerApplication(
184 "org.onosproject.driver.Ofdpa2Pipeline");
185 }
186
187 protected void initGroupHander(PipelinerContext context) {
188 groupHandler = new Ofdpa2GroupHandler();
189 groupHandler.init(deviceId, context);
190 }
191
Saurav Das822c4e22015-10-23 10:51:11 -0700192 protected void initializePipeline() {
Charles Chan188ebf52015-12-23 00:15:11 -0800193 // OF-DPA does not require initializing the pipeline as it puts default
194 // rules automatically in the hardware. However emulation of OFDPA in
195 // software switches does require table-miss-entries.
Saurav Das822c4e22015-10-23 10:51:11 -0700196 }
197
Charles Chand1172632017-03-15 17:33:09 -0700198 /**
199 * Determines whether this pipeline requires OFDPA match and set VLAN extensions.
200 *
201 * @return true to use the extensions
202 */
203 protected boolean requireVlanExtensions() {
204 return true;
205 }
206
Saurav Das86d13e82017-04-28 17:03:48 -0700207 /**
208 * Determines whether in-port should be matched on in TMAC table rules.
209 *
210 * @return true if match on in-port should be programmed
211 */
212 protected boolean matchInPortTmacTable() {
213 return true;
214 }
215
Charles Chan30122a72017-10-05 15:17:15 -0700216 /**
217 * Determines whether matching L4 destination port on IPv6 packets is supported in ACL table.
218 *
219 * @return true if matching L4 destination port on IPv6 packets is supported in ACL table.
220 */
221 protected boolean supportIpv6L4Dst() {
222 return true;
223 }
224
Saurav Das822c4e22015-10-23 10:51:11 -0700225 //////////////////////////////////////
226 // Flow Objectives
227 //////////////////////////////////////
228
229 @Override
230 public void filter(FilteringObjective filteringObjective) {
231 if (filteringObjective.type() == FilteringObjective.Type.PERMIT) {
232 processFilter(filteringObjective,
233 filteringObjective.op() == Objective.Operation.ADD,
234 filteringObjective.appId());
235 } else {
236 // Note that packets that don't match the PERMIT filter are
237 // automatically denied. The DENY filter is used to deny packets
238 // that are otherwise permitted by the PERMIT filter.
239 // Use ACL table flow rules here for DENY filtering objectives
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530240 log.warn("filter objective other than PERMIT currently not supported");
Saurav Das822c4e22015-10-23 10:51:11 -0700241 fail(filteringObjective, ObjectiveError.UNSUPPORTED);
242 }
243 }
244
245 @Override
246 public void forward(ForwardingObjective fwd) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700247 Collection<FlowRule> rules = processForward(fwd);
Saurav Das25190812016-05-27 13:54:07 -0700248 if (rules == null || rules.isEmpty()) {
249 // Assumes fail message has already been generated to the objective
250 // context. Returning here prevents spurious pass message to be
251 // generated by FlowRule service for empty flowOps.
252 return;
253 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700254 sendForward(fwd, rules);
255 }
256
257 protected void sendForward(ForwardingObjective fwd, Collection<FlowRule> rules) {
258 FlowRuleOperations.Builder flowOpsBuilder = FlowRuleOperations.builder();
Saurav Das822c4e22015-10-23 10:51:11 -0700259 switch (fwd.op()) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700260 case ADD:
261 rules.stream()
262 .filter(Objects::nonNull)
263 .forEach(flowOpsBuilder::add);
Saurav Dasd2fded02016-12-02 15:43:47 -0800264 log.debug("Applying a add fwd-obj {} to sw:{}", fwd.id(), deviceId);
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700265 break;
266 case REMOVE:
267 rules.stream()
268 .filter(Objects::nonNull)
269 .forEach(flowOpsBuilder::remove);
Pier Ventre42287df2016-11-09 14:17:26 -0800270 log.debug("Deleting a flow rule to sw:{}", deviceId);
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700271 break;
272 default:
273 fail(fwd, ObjectiveError.UNKNOWN);
274 log.warn("Unknown forwarding type {}", fwd.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700275 }
276
Saurav Das822c4e22015-10-23 10:51:11 -0700277 flowRuleService.apply(flowOpsBuilder.build(new FlowRuleOperationsContext() {
278 @Override
279 public void onSuccess(FlowRuleOperations ops) {
280 pass(fwd);
281 }
282
283 @Override
284 public void onError(FlowRuleOperations ops) {
285 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
286 }
287 }));
Saurav Das822c4e22015-10-23 10:51:11 -0700288 }
289
290 @Override
291 public void next(NextObjective nextObjective) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800292 NextGroup nextGroup = flowObjectiveStore.getNextGroup(nextObjective.id());
293 switch (nextObjective.op()) {
294 case ADD:
Saurav Das4f980082015-11-05 13:39:15 -0800295 if (nextGroup != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800296 log.warn("Cannot add next {} that already exists in device {}",
297 nextObjective.id(), deviceId);
298 return;
299 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700300 log.debug("Processing NextObjective id {} in dev {} - add group",
Saurav Das8a0732e2015-11-20 15:27:53 -0800301 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700302 groupHandler.addGroup(nextObjective);
Saurav Das8a0732e2015-11-20 15:27:53 -0800303 break;
304 case ADD_TO_EXISTING:
305 if (nextGroup != null) {
Saurav Das1547b3f2017-05-05 17:01:08 -0700306 log.debug("Processing NextObjective id {} in dev {} - add bucket",
Saurav Das8a0732e2015-11-20 15:27:53 -0800307 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700308 groupHandler.addBucketToGroup(nextObjective, nextGroup);
Saurav Das4f980082015-11-05 13:39:15 -0800309 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800310 // it is possible that group-chain has not been fully created yet
Saurav Das423fe2b2015-12-04 10:52:59 -0800311 log.debug("Waiting to add bucket to group for next-id:{} in dev:{}",
312 nextObjective.id(), deviceId);
Yi Tseng47f82dc2017-03-05 22:48:39 -0800313
314 // by design multiple pending bucket is allowed for the group
315 groupHandler.pendingBuckets.compute(nextObjective.id(), (nextId, pendBkts) -> {
316 if (pendBkts == null) {
317 pendBkts = Sets.newHashSet();
318 }
319 pendBkts.add(nextObjective);
320 return pendBkts;
321 });
Saurav Das4f980082015-11-05 13:39:15 -0800322 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800323 break;
324 case REMOVE:
325 if (nextGroup == null) {
326 log.warn("Cannot remove next {} that does not exist in device {}",
327 nextObjective.id(), deviceId);
328 return;
329 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700330 log.debug("Processing NextObjective id {} in dev {} - remove group",
Saurav Das8a0732e2015-11-20 15:27:53 -0800331 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700332 groupHandler.removeGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800333 break;
334 case REMOVE_FROM_EXISTING:
335 if (nextGroup == null) {
336 log.warn("Cannot remove from next {} that does not exist in device {}",
337 nextObjective.id(), deviceId);
338 return;
339 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700340 log.debug("Processing NextObjective id {} in dev {} - remove bucket",
Saurav Das8a0732e2015-11-20 15:27:53 -0800341 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700342 groupHandler.removeBucketFromGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800343 break;
Saurav Dasceccf242017-08-03 18:30:35 -0700344 case VERIFY:
345 if (nextGroup == null) {
346 log.warn("Cannot verify next {} that does not exist in device {}",
347 nextObjective.id(), deviceId);
348 return;
349 }
350 log.debug("Processing NextObjective id {} in dev {} - verify",
351 nextObjective.id(), deviceId);
352 groupHandler.verifyGroup(nextObjective, nextGroup);
353 break;
Saurav Das8a0732e2015-11-20 15:27:53 -0800354 default:
Saurav Das4f980082015-11-05 13:39:15 -0800355 log.warn("Unsupported operation {}", nextObjective.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700356 }
357 }
358
359 //////////////////////////////////////
360 // Flow handling
361 //////////////////////////////////////
362
363 /**
Yi Tseng0fdc07e2017-04-24 19:17:08 -0700364 * As per OFDPA 2.0 TTP, filtering of VLAN ids and MAC addresses (for routing)
365 * configured on switch ports happen in different tables.
Saurav Das822c4e22015-10-23 10:51:11 -0700366 *
367 * @param filt the filtering objective
368 * @param install indicates whether to add or remove the objective
369 * @param applicationId the application that sent this objective
370 */
Saurav Das52025962016-01-28 22:30:01 -0800371 protected void processFilter(FilteringObjective filt,
372 boolean install, ApplicationId applicationId) {
Saurav Das822c4e22015-10-23 10:51:11 -0700373 // This driver only processes filtering criteria defined with switch
374 // ports as the key
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530375 PortCriterion portCriterion = null;
Saurav Das822c4e22015-10-23 10:51:11 -0700376 EthCriterion ethCriterion = null;
377 VlanIdCriterion vidCriterion = null;
Saurav Das822c4e22015-10-23 10:51:11 -0700378 if (!filt.key().equals(Criteria.dummy()) &&
379 filt.key().type() == Criterion.Type.IN_PORT) {
380 portCriterion = (PortCriterion) filt.key();
Saurav Das822c4e22015-10-23 10:51:11 -0700381 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530382 if (portCriterion == null) {
383 log.debug("No IN_PORT defined in filtering objective from app: {}",
384 applicationId);
385 } else {
386 log.debug("Received filtering objective for dev/port: {}/{}", deviceId,
387 portCriterion.port());
388 }
Saurav Das822c4e22015-10-23 10:51:11 -0700389 // convert filtering conditions for switch-intfs into flowrules
390 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
391 for (Criterion criterion : filt.conditions()) {
Yi Tseng0fdc07e2017-04-24 19:17:08 -0700392 switch (criterion.type()) {
393 case ETH_DST:
394 case ETH_DST_MASKED:
395 ethCriterion = (EthCriterion) criterion;
396 break;
397 case VLAN_VID:
398 vidCriterion = (VlanIdCriterion) criterion;
399 break;
400 default:
401 log.warn("Unsupported filter {}", criterion);
402 fail(filt, ObjectiveError.UNSUPPORTED);
403 return;
Saurav Das822c4e22015-10-23 10:51:11 -0700404 }
405 }
406
Saurav Das0e99e2b2015-10-28 12:39:42 -0700407 VlanId assignedVlan = null;
Charles Chane849c192016-01-11 18:28:54 -0800408 if (vidCriterion != null) {
Charles Chand55e84d2016-03-30 17:54:24 -0700409 // Use the VLAN in metadata whenever a metadata is provided
410 if (filt.meta() != null) {
411 assignedVlan = readVlanFromTreatment(filt.meta());
412 // Use the VLAN in criterion if metadata is not present and the traffic is tagged
413 } else if (!vidCriterion.vlanId().equals(VlanId.NONE)) {
Charles Chane849c192016-01-11 18:28:54 -0800414 assignedVlan = vidCriterion.vlanId();
Charles Chand55e84d2016-03-30 17:54:24 -0700415 }
Charles Chane849c192016-01-11 18:28:54 -0800416
Charles Chand55e84d2016-03-30 17:54:24 -0700417 if (assignedVlan == null) {
418 log.error("Driver fails to extract VLAN information. "
Frank Wang3a98e0a2017-08-11 11:09:30 +0800419 + "Not processing VLAN filters on device {}.", deviceId);
Charles Chand55e84d2016-03-30 17:54:24 -0700420 log.debug("VLAN ID in criterion={}, metadata={}",
421 readVlanFromTreatment(filt.meta()), vidCriterion.vlanId());
422 fail(filt, ObjectiveError.BADPARAMS);
423 return;
Saurav Das0e99e2b2015-10-28 12:39:42 -0700424 }
425 }
426
Yi Tseng3a77b4f2017-02-06 15:02:17 -0800427 if (ethCriterion == null || ethCriterion.mac().equals(NONE)) {
Charles Chan985b12e2016-05-11 19:47:22 -0700428 // NOTE: it is possible that a filtering objective only has vidCriterion
Saurav Das961beb22017-03-29 19:09:17 -0700429 log.warn("filtering objective missing dstMac, cannot program TMAC table");
Saurav Das822c4e22015-10-23 10:51:11 -0700430 } else {
431 for (FlowRule tmacRule : processEthDstFilter(portCriterion, ethCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700432 vidCriterion, assignedVlan,
433 applicationId)) {
Saurav Das961beb22017-03-29 19:09:17 -0700434 log.trace("{} MAC filtering rules in TMAC table: {} for dev: {}",
Saurav Das018605f2017-02-18 14:05:44 -0800435 (install) ? "adding" : "removing", tmacRule, deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700436 ops = install ? ops.add(tmacRule) : ops.remove(tmacRule);
437 }
438 }
439
Charles Chan985b12e2016-05-11 19:47:22 -0700440 if (vidCriterion == null) {
441 // NOTE: it is possible that a filtering objective only has ethCriterion
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530442 log.info("filtering objective missing VLAN, cannot program VLAN Table");
Saurav Das822c4e22015-10-23 10:51:11 -0700443 } else {
Charles Chan14967c22015-12-07 11:11:50 -0800444 /*
445 * NOTE: Separate vlan filtering rules and assignment rules
446 * into different stage in order to guarantee that filtering rules
Saurav Das52025962016-01-28 22:30:01 -0800447 * always go first, as required by ofdpa.
Charles Chan14967c22015-12-07 11:11:50 -0800448 */
449 List<FlowRule> allRules = processVlanIdFilter(
450 portCriterion, vidCriterion, assignedVlan, applicationId);
451 List<FlowRule> filteringRules = new ArrayList<>();
452 List<FlowRule> assignmentRules = new ArrayList<>();
453
454 allRules.forEach(flowRule -> {
Charles Chand1172632017-03-15 17:33:09 -0700455 VlanId vlanId;
456 if (requireVlanExtensions()) {
457 ExtensionCriterion extCriterion =
458 (ExtensionCriterion) flowRule.selector().getCriterion(Criterion.Type.EXTENSION);
459 vlanId = ((OfdpaMatchVlanVid) extCriterion.extensionSelector()).vlanId();
460 } else {
461 VlanIdCriterion vlanIdCriterion =
462 (VlanIdCriterion) flowRule.selector().getCriterion(Criterion.Type.VLAN_VID);
463 vlanId = vlanIdCriterion.vlanId();
464 }
Charles Chanbe8aea42016-02-24 12:04:47 -0800465 if (!vlanId.equals(VlanId.NONE)) {
Charles Chan14967c22015-12-07 11:11:50 -0800466 filteringRules.add(flowRule);
467 } else {
468 assignmentRules.add(flowRule);
469 }
470 });
471
472 for (FlowRule filteringRule : filteringRules) {
Saurav Das961beb22017-03-29 19:09:17 -0700473 log.trace("{} VLAN filtering rule in VLAN table: {} for dev: {}",
Saurav Das018605f2017-02-18 14:05:44 -0800474 (install) ? "adding" : "removing", filteringRule, deviceId);
Charles Chan14967c22015-12-07 11:11:50 -0800475 ops = install ? ops.add(filteringRule) : ops.remove(filteringRule);
476 }
477
478 ops.newStage();
479
480 for (FlowRule assignmentRule : assignmentRules) {
Saurav Das961beb22017-03-29 19:09:17 -0700481 log.trace("{} VLAN assignment rule in VLAN table: {} for dev: {}",
Saurav Das018605f2017-02-18 14:05:44 -0800482 (install) ? "adding" : "removing", assignmentRule, deviceId);
Charles Chan14967c22015-12-07 11:11:50 -0800483 ops = install ? ops.add(assignmentRule) : ops.remove(assignmentRule);
Saurav Das822c4e22015-10-23 10:51:11 -0700484 }
485 }
486
Saurav Das822c4e22015-10-23 10:51:11 -0700487 // apply filtering flow rules
488 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
489 @Override
490 public void onSuccess(FlowRuleOperations ops) {
Saurav Das018605f2017-02-18 14:05:44 -0800491 log.debug("Applied {} filtering rules in device {}",
Saurav Das822c4e22015-10-23 10:51:11 -0700492 ops.stages().get(0).size(), deviceId);
493 pass(filt);
494 }
495
496 @Override
497 public void onError(FlowRuleOperations ops) {
498 log.info("Failed to apply all filtering rules in dev {}", deviceId);
499 fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED);
500 }
501 }));
502
503 }
504
505 /**
Charles Chand1172632017-03-15 17:33:09 -0700506 * Internal implementation of processVlanIdFilter.
507 * <p>
508 * The is_present bit in set_vlan_vid action is required to be 0 in OFDPA i12.
509 * Since it is non-OF spec, we need an extension treatment for that.
510 * The useVlanExtension must be set to false for OFDPA i12.
511 * </p>
Saurav Das0e99e2b2015-10-28 12:39:42 -0700512 *
Charles Chanf9e98652016-09-07 16:54:23 -0700513 * @param portCriterion port on device for which this filter is programmed
514 * @param vidCriterion vlan assigned to port, or NONE for untagged
515 * @param assignedVlan assigned vlan-id for untagged packets
516 * @param applicationId for application programming this filter
Saurav Das822c4e22015-10-23 10:51:11 -0700517 * @return list of FlowRule for port-vlan filters
518 */
519 protected List<FlowRule> processVlanIdFilter(PortCriterion portCriterion,
Yi Tsengef19de12017-04-24 11:33:05 -0700520 VlanIdCriterion vidCriterion,
521 VlanId assignedVlan,
522 ApplicationId applicationId) {
Charles Chan14967c22015-12-07 11:11:50 -0800523 List<FlowRule> rules = new ArrayList<>();
Saurav Das822c4e22015-10-23 10:51:11 -0700524 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
525 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800526 TrafficSelector.Builder preSelector = null;
527 TrafficTreatment.Builder preTreatment = null;
528
Saurav Das4f980082015-11-05 13:39:15 -0800529 treatment.transition(TMAC_TABLE);
530
Saurav Das822c4e22015-10-23 10:51:11 -0700531 if (vidCriterion.vlanId() == VlanId.NONE) {
532 // untagged packets are assigned vlans
Charles Chand1172632017-03-15 17:33:09 -0700533 preSelector = DefaultTrafficSelector.builder();
534 if (requireVlanExtensions()) {
535 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(VlanId.NONE);
536 selector.extension(ofdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700537 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
538 treatment.extension(ofdpaSetVlanVid, deviceId);
Charles Chand1172632017-03-15 17:33:09 -0700539
540 OfdpaMatchVlanVid preOfdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
541 preSelector.extension(preOfdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700542 } else {
Charles Chand1172632017-03-15 17:33:09 -0700543 selector.matchVlanId(VlanId.NONE);
Charles Chanf9e98652016-09-07 16:54:23 -0700544 treatment.setVlanId(assignedVlan);
Charles Chand1172632017-03-15 17:33:09 -0700545
546 preSelector.matchVlanId(assignedVlan);
547 }
548 preTreatment = DefaultTrafficTreatment.builder().transition(TMAC_TABLE);
549 } else {
550 if (requireVlanExtensions()) {
551 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
552 selector.extension(ofdpaMatchVlanVid, deviceId);
553 } else {
554 selector.matchVlanId(vidCriterion.vlanId());
Charles Chanf9e98652016-09-07 16:54:23 -0700555 }
Charles Chan14967c22015-12-07 11:11:50 -0800556
Charles Chand55e84d2016-03-30 17:54:24 -0700557 if (!assignedVlan.equals(vidCriterion.vlanId())) {
Charles Chand1172632017-03-15 17:33:09 -0700558 if (requireVlanExtensions()) {
Charles Chanc03782d2017-01-31 13:57:55 -0800559 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
560 treatment.extension(ofdpaSetVlanVid, deviceId);
561 } else {
562 treatment.setVlanId(assignedVlan);
563 }
Charles Chand55e84d2016-03-30 17:54:24 -0700564 }
Saurav Das822c4e22015-10-23 10:51:11 -0700565 }
Saurav Das822c4e22015-10-23 10:51:11 -0700566
567 // ofdpa cannot match on ALL portnumber, so we need to use separate
568 // rules for each port.
Charles Chan14967c22015-12-07 11:11:50 -0800569 List<PortNumber> portnums = new ArrayList<>();
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530570 if (portCriterion != null) {
571 if (PortNumber.ALL.equals(portCriterion.port())) {
572 for (Port port : deviceService.getPorts(deviceId)) {
573 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
574 portnums.add(port.number());
575 }
Saurav Das822c4e22015-10-23 10:51:11 -0700576 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530577 } else {
578 portnums.add(portCriterion.port());
Saurav Das822c4e22015-10-23 10:51:11 -0700579 }
580 } else {
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530581 log.warn("Filtering Objective missing Port Criterion . " +
582 "VLAN Table cannot be programmed for {}",
583 deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700584 }
Saurav Das4f980082015-11-05 13:39:15 -0800585
Saurav Das822c4e22015-10-23 10:51:11 -0700586 for (PortNumber pnum : portnums) {
Saurav Das4f980082015-11-05 13:39:15 -0800587 // create rest of flowrule
Saurav Das822c4e22015-10-23 10:51:11 -0700588 selector.matchInPort(pnum);
589 FlowRule rule = DefaultFlowRule.builder()
590 .forDevice(deviceId)
591 .withSelector(selector.build())
592 .withTreatment(treatment.build())
593 .withPriority(DEFAULT_PRIORITY)
594 .fromApp(applicationId)
595 .makePermanent()
596 .forTable(VLAN_TABLE).build();
Charles Chan14967c22015-12-07 11:11:50 -0800597
598 if (preSelector != null) {
599 preSelector.matchInPort(pnum);
600 FlowRule preRule = DefaultFlowRule.builder()
601 .forDevice(deviceId)
602 .withSelector(preSelector.build())
603 .withTreatment(preTreatment.build())
604 .withPriority(DEFAULT_PRIORITY)
605 .fromApp(applicationId)
606 .makePermanent()
607 .forTable(VLAN_TABLE).build();
608 rules.add(preRule);
609 }
610
Saurav Das822c4e22015-10-23 10:51:11 -0700611 rules.add(rule);
612 }
613 return rules;
614 }
615
616 /**
617 * Allows routed packets with correct destination MAC to be directed
618 * to unicast-IP routing table or MPLS forwarding table.
Saurav Das822c4e22015-10-23 10:51:11 -0700619 *
620 * @param portCriterion port on device for which this filter is programmed
621 * @param ethCriterion dstMac of device for which is filter is programmed
622 * @param vidCriterion vlan assigned to port, or NONE for untagged
Saurav Das0e99e2b2015-10-28 12:39:42 -0700623 * @param assignedVlan assigned vlan-id for untagged packets
Saurav Das822c4e22015-10-23 10:51:11 -0700624 * @param applicationId for application programming this filter
625 * @return list of FlowRule for port-vlan filters
626
627 */
628 protected List<FlowRule> processEthDstFilter(PortCriterion portCriterion,
629 EthCriterion ethCriterion,
630 VlanIdCriterion vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700631 VlanId assignedVlan,
Saurav Das822c4e22015-10-23 10:51:11 -0700632 ApplicationId applicationId) {
Charles Chan5270ed02016-01-30 23:22:37 -0800633 // Consider PortNumber.ANY as wildcard. Match ETH_DST only
Sivachidambaram Subramanianc598d062017-05-16 22:42:06 +0530634 if (portCriterion != null && PortNumber.ANY.equals(portCriterion.port())) {
Charles Chan5270ed02016-01-30 23:22:37 -0800635 return processEthDstOnlyFilter(ethCriterion, applicationId);
636 }
637
Charles Chan5b9df8d2016-03-28 22:21:40 -0700638 // Multicast MAC
639 if (ethCriterion.mask() != null) {
640 return processMcastEthDstFilter(ethCriterion, applicationId);
641 }
642
Saurav Das822c4e22015-10-23 10:51:11 -0700643 //handling untagged packets via assigned VLAN
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530644 if (vidCriterion != null && vidCriterion.vlanId() == VlanId.NONE) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700645 vidCriterion = (VlanIdCriterion) Criteria.matchVlanId(assignedVlan);
Saurav Das822c4e22015-10-23 10:51:11 -0700646 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530647 List<FlowRule> rules = new ArrayList<>();
648 OfdpaMatchVlanVid ofdpaMatchVlanVid = null;
649 if (vidCriterion != null && requireVlanExtensions()) {
650 ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
651 }
Saurav Das822c4e22015-10-23 10:51:11 -0700652 // ofdpa cannot match on ALL portnumber, so we need to use separate
653 // rules for each port.
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -0700654 List<PortNumber> portnums = new ArrayList<>();
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530655 if (portCriterion != null) {
656 if (PortNumber.ALL.equals(portCriterion.port())) {
657 for (Port port : deviceService.getPorts(deviceId)) {
658 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
659 portnums.add(port.number());
660 }
Saurav Das822c4e22015-10-23 10:51:11 -0700661 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530662 } else {
663 portnums.add(portCriterion.port());
664 }
665 for (PortNumber pnum : portnums) {
666 rules.add(buildTmacRuleForIpv4(ethCriterion,
667 vidCriterion,
668 ofdpaMatchVlanVid,
669 applicationId,
670 pnum));
671 rules.add(buildTmacRuleForMpls(ethCriterion,
672 vidCriterion,
673 ofdpaMatchVlanVid,
674 applicationId,
675 pnum));
676 rules.add(buildTmacRuleForIpv6(ethCriterion,
677 vidCriterion,
678 ofdpaMatchVlanVid,
679 applicationId,
680 pnum));
Saurav Das822c4e22015-10-23 10:51:11 -0700681 }
682 } else {
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530683 rules.add(buildTmacRuleForIpv4(ethCriterion,
684 vidCriterion,
685 ofdpaMatchVlanVid,
686 applicationId,
687 null));
688 rules.add(buildTmacRuleForMpls(ethCriterion,
689 vidCriterion,
690 ofdpaMatchVlanVid,
691 applicationId,
692 null));
693 rules.add(buildTmacRuleForIpv6(ethCriterion,
694 vidCriterion,
695 ofdpaMatchVlanVid,
696 applicationId,
697 null));
Saurav Das822c4e22015-10-23 10:51:11 -0700698 }
699 return rules;
700 }
701
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530702 /**
703 * Builds TMAC rules for IPv4 packets.
704 *
705 * @param ethCriterion
706 * @param vidCriterion
707 * @param ofdpaMatchVlanVid
708 * @param applicationId
709 * @param pnum
710 * @return TMAC rule for IPV4 packets
711 */
712 private FlowRule buildTmacRuleForIpv4(EthCriterion ethCriterion,
713 VlanIdCriterion vidCriterion,
714 OfdpaMatchVlanVid ofdpaMatchVlanVid,
715 ApplicationId applicationId,
716 PortNumber pnum) {
717 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
718 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
719 if (pnum != null) {
720 if (matchInPortTmacTable()) {
721 selector.matchInPort(pnum);
722 } else {
723 log.info("Pipeline does not support IN_PORT matching in TMAC table, " +
724 "ignoring the IN_PORT criteria");
725 }
726 }
727 if (vidCriterion != null) {
728 if (requireVlanExtensions()) {
729 selector.extension(ofdpaMatchVlanVid, deviceId);
730 } else {
731 selector.matchVlanId(vidCriterion.vlanId());
732 }
733 }
734 selector.matchEthType(Ethernet.TYPE_IPV4);
735 selector.matchEthDst(ethCriterion.mac());
736 treatment.transition(UNICAST_ROUTING_TABLE);
737 return DefaultFlowRule.builder()
738 .forDevice(deviceId)
739 .withSelector(selector.build())
740 .withTreatment(treatment.build())
741 .withPriority(DEFAULT_PRIORITY)
742 .fromApp(applicationId)
743 .makePermanent()
744 .forTable(TMAC_TABLE).build();
745 }
746
747 /**
748 * Builds TMAC rule for MPLS packets.
749 *
750 * @param ethCriterion
751 * @param vidCriterion
752 * @param ofdpaMatchVlanVid
753 * @param applicationId
754 * @param pnum
755 * @return TMAC rule for MPLS packets
756 */
757 private FlowRule buildTmacRuleForMpls(EthCriterion ethCriterion,
758 VlanIdCriterion vidCriterion,
759 OfdpaMatchVlanVid ofdpaMatchVlanVid,
760 ApplicationId applicationId,
761 PortNumber pnum) {
762 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
763 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
764 if (pnum != null) {
765 if (matchInPortTmacTable()) {
766 selector.matchInPort(pnum);
767 } else {
768 log.info("Pipeline does not support IN_PORT matching in TMAC table, " +
769 "ignoring the IN_PORT criteria");
770 }
771 }
772 if (vidCriterion != null) {
773 if (requireVlanExtensions()) {
774 selector.extension(ofdpaMatchVlanVid, deviceId);
775 } else {
776 selector.matchVlanId(vidCriterion.vlanId());
777 }
778 }
779 selector.matchEthType(Ethernet.MPLS_UNICAST);
780 selector.matchEthDst(ethCriterion.mac());
781 treatment.transition(MPLS_TABLE_0);
782 return DefaultFlowRule.builder()
783 .forDevice(deviceId)
784 .withSelector(selector.build())
785 .withTreatment(treatment.build())
786 .withPriority(DEFAULT_PRIORITY)
787 .fromApp(applicationId)
788 .makePermanent()
789 .forTable(TMAC_TABLE).build();
790 }
791
792 /**
793 * Builds TMAC rules for IPv6 packets.
794 *
795 * @param ethCriterion
796 * @param vidCriterion
797 * @param ofdpaMatchVlanVid
798 * @param applicationId
799 * @param pnum
800 * @return TMAC rule for IPV6 packets
801 */
802 private FlowRule buildTmacRuleForIpv6(EthCriterion ethCriterion,
803 VlanIdCriterion vidCriterion,
804 OfdpaMatchVlanVid ofdpaMatchVlanVid,
805 ApplicationId applicationId,
806 PortNumber pnum) {
807 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
808 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
809 if (pnum != null) {
810 if (matchInPortTmacTable()) {
811 selector.matchInPort(pnum);
812 } else {
813 log.info("Pipeline does not support IN_PORT matching in TMAC table, " +
814 "ignoring the IN_PORT criteria");
815 }
816 }
817 if (vidCriterion != null) {
818 if (requireVlanExtensions()) {
819 selector.extension(ofdpaMatchVlanVid, deviceId);
820 } else {
821 selector.matchVlanId(vidCriterion.vlanId());
822 }
823 }
824 selector.matchEthType(Ethernet.TYPE_IPV6);
825 selector.matchEthDst(ethCriterion.mac());
826 treatment.transition(UNICAST_ROUTING_TABLE);
827 return DefaultFlowRule.builder()
828 .forDevice(deviceId)
829 .withSelector(selector.build())
830 .withTreatment(treatment.build())
831 .withPriority(DEFAULT_PRIORITY)
832 .fromApp(applicationId)
833 .makePermanent()
834 .forTable(TMAC_TABLE).build();
835 }
836
Charles Chan5270ed02016-01-30 23:22:37 -0800837 protected List<FlowRule> processEthDstOnlyFilter(EthCriterion ethCriterion,
Yi Tsengef19de12017-04-24 11:33:05 -0700838 ApplicationId applicationId) {
Pier Luigi0e358632017-01-31 09:35:05 -0800839 ImmutableList.Builder<FlowRule> builder = ImmutableList.builder();
840
Charles Chan5270ed02016-01-30 23:22:37 -0800841 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
842 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
843 selector.matchEthType(Ethernet.TYPE_IPV4);
844 selector.matchEthDst(ethCriterion.mac());
845 treatment.transition(UNICAST_ROUTING_TABLE);
846 FlowRule rule = DefaultFlowRule.builder()
847 .forDevice(deviceId)
848 .withSelector(selector.build())
849 .withTreatment(treatment.build())
850 .withPriority(DEFAULT_PRIORITY)
851 .fromApp(applicationId)
852 .makePermanent()
853 .forTable(TMAC_TABLE).build();
Pier Luigi0e358632017-01-31 09:35:05 -0800854 builder.add(rule);
855
856 selector = DefaultTrafficSelector.builder();
857 treatment = DefaultTrafficTreatment.builder();
858 selector.matchEthType(Ethernet.TYPE_IPV6);
859 selector.matchEthDst(ethCriterion.mac());
860 treatment.transition(UNICAST_ROUTING_TABLE);
861 rule = DefaultFlowRule.builder()
862 .forDevice(deviceId)
863 .withSelector(selector.build())
864 .withTreatment(treatment.build())
865 .withPriority(DEFAULT_PRIORITY)
866 .fromApp(applicationId)
867 .makePermanent()
868 .forTable(TMAC_TABLE).build();
869 return builder.add(rule).build();
Charles Chan5270ed02016-01-30 23:22:37 -0800870 }
871
Charles Chan5b9df8d2016-03-28 22:21:40 -0700872 protected List<FlowRule> processMcastEthDstFilter(EthCriterion ethCriterion,
Yi Tsengef19de12017-04-24 11:33:05 -0700873 ApplicationId applicationId) {
Julia Fergusond8f145e2017-08-10 18:15:24 +0000874 ImmutableList.Builder<FlowRule> builder = ImmutableList.builder();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700875 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
876 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
877 selector.matchEthType(Ethernet.TYPE_IPV4);
878 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
879 treatment.transition(MULTICAST_ROUTING_TABLE);
880 FlowRule rule = DefaultFlowRule.builder()
881 .forDevice(deviceId)
882 .withSelector(selector.build())
883 .withTreatment(treatment.build())
884 .withPriority(DEFAULT_PRIORITY)
885 .fromApp(applicationId)
886 .makePermanent()
887 .forTable(TMAC_TABLE).build();
Julia Fergusond8f145e2017-08-10 18:15:24 +0000888 builder.add(rule);
889
890 selector = DefaultTrafficSelector.builder();
891 treatment = DefaultTrafficTreatment.builder();
892 selector.matchEthType(Ethernet.TYPE_IPV6);
893 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
894 treatment.transition(MULTICAST_ROUTING_TABLE);
895 rule = DefaultFlowRule.builder()
896 .forDevice(deviceId)
897 .withSelector(selector.build())
898 .withTreatment(treatment.build())
899 .withPriority(DEFAULT_PRIORITY)
900 .fromApp(applicationId)
901 .makePermanent()
902 .forTable(TMAC_TABLE).build();
903 return builder.add(rule).build();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700904 }
905
Saurav Das822c4e22015-10-23 10:51:11 -0700906 private Collection<FlowRule> processForward(ForwardingObjective fwd) {
907 switch (fwd.flag()) {
908 case SPECIFIC:
909 return processSpecific(fwd);
910 case VERSATILE:
911 return processVersatile(fwd);
912 default:
913 fail(fwd, ObjectiveError.UNKNOWN);
914 log.warn("Unknown forwarding flag {}", fwd.flag());
915 }
916 return Collections.emptySet();
917 }
918
919 /**
920 * In the OF-DPA 2.0 pipeline, versatile forwarding objectives go to the
921 * ACL table.
922 * @param fwd the forwarding objective of type 'versatile'
923 * @return a collection of flow rules to be sent to the switch. An empty
924 * collection may be returned if there is a problem in processing
925 * the flow rule
926 */
Saurav Das52025962016-01-28 22:30:01 -0800927 protected Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
Saurav Das018605f2017-02-18 14:05:44 -0800928 log.debug("Processing versatile forwarding objective:{} in dev:{}",
Saurav Dasd2fded02016-12-02 15:43:47 -0800929 fwd.id(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700930
931 EthTypeCriterion ethType =
Saurav Das77b5e902016-01-27 17:01:59 -0800932 (EthTypeCriterion) fwd.selector().getCriterion(Criterion.Type.ETH_TYPE);
Saurav Das822c4e22015-10-23 10:51:11 -0700933 if (ethType == null) {
Saurav Dasd2fded02016-12-02 15:43:47 -0800934 log.error("Versatile forwarding objective:{} must include ethType",
935 fwd.id());
Saurav Das822c4e22015-10-23 10:51:11 -0700936 fail(fwd, ObjectiveError.BADPARAMS);
937 return Collections.emptySet();
938 }
939 if (fwd.nextId() == null && fwd.treatment() == null) {
940 log.error("Forwarding objective {} from {} must contain "
941 + "nextId or Treatment", fwd.selector(), fwd.appId());
Zsolt Haraszti9faab752016-02-17 15:55:20 -0800942 fail(fwd, ObjectiveError.BADPARAMS);
Saurav Das822c4e22015-10-23 10:51:11 -0700943 return Collections.emptySet();
944 }
Saurav Das49cb5a12016-01-16 22:54:07 -0800945
Saurav Das77b5e902016-01-27 17:01:59 -0800946 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
947 fwd.selector().criteria().forEach(criterion -> {
948 if (criterion instanceof VlanIdCriterion) {
949 VlanId vlanId = ((VlanIdCriterion) criterion).vlanId();
950 // ensure that match does not include vlan = NONE as OF-DPA does not
951 // match untagged packets this way in the ACL table.
952 if (vlanId.equals(VlanId.NONE)) {
953 return;
954 }
Charles Chand1172632017-03-15 17:33:09 -0700955 if (requireVlanExtensions()) {
956 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanId);
957 sbuilder.extension(ofdpaMatchVlanVid, deviceId);
958 } else {
959 sbuilder.matchVlanId(vlanId);
960 }
Pier Ventree0ae7a32016-11-23 09:57:42 -0800961 } else if (criterion instanceof Icmpv6TypeCriterion ||
962 criterion instanceof Icmpv6CodeCriterion) {
963 /*
964 * We silenty discard these criterions, our current
965 * OFDPA platform does not support these matches on
966 * the ACL table.
967 */
968 log.warn("ICMPv6 Type and ICMPv6 Code are not supported");
Charles Chan30122a72017-10-05 15:17:15 -0700969 } else if (criterion instanceof TcpPortCriterion || criterion instanceof UdpPortCriterion) {
970 // FIXME: QMX switches do not support L4 dst port matching in ACL table.
971 // Currently L4 dst port matching is only used by DHCP relay feature
972 // and therefore is safe to be replaced with L4 src port matching.
973 // We need to revisit this if L4 dst port is used for other purpose in the future.
974 if (!supportIpv6L4Dst() && isIpv6(fwd.selector())) {
975 TpPort tpPort = (criterion instanceof TcpPortCriterion) ?
976 ((TcpPortCriterion) criterion).tcpPort() :
977 ((UdpPortCriterion) criterion).udpPort();
978 TpPort tpMask = (criterion instanceof TcpPortCriterion) ?
979 ((TcpPortCriterion) criterion).mask() :
980 ((UdpPortCriterion) criterion).mask();
981 switch (criterion.type()) {
982 case UDP_DST:
983 sbuilder.matchUdpSrc(tpPort);
984 break;
985 case UDP_DST_MASKED:
986 sbuilder.matchUdpSrcMasked(tpPort, tpMask);
987 break;
988 case TCP_DST:
989 sbuilder.matchTcpSrc(tpPort);
990 break;
991 case TCP_DST_MASKED:
992 sbuilder.matchTcpSrcMasked(tpPort, tpMask);
993 break;
994 default:
995 sbuilder.add(criterion);
996 }
997 } else {
998 sbuilder.add(criterion);
999 }
Saurav Das77b5e902016-01-27 17:01:59 -08001000 } else {
1001 sbuilder.add(criterion);
1002 }
1003 });
1004
Saurav Das822c4e22015-10-23 10:51:11 -07001005 // XXX driver does not currently do type checking as per Tables 65-67 in
1006 // OFDPA 2.0 spec. The only allowed treatment is a punt to the controller.
Saurav Das49cb5a12016-01-16 22:54:07 -08001007 TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder();
1008 if (fwd.treatment() != null) {
1009 for (Instruction ins : fwd.treatment().allInstructions()) {
1010 if (ins instanceof OutputInstruction) {
1011 OutputInstruction o = (OutputInstruction) ins;
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +05301012 if (o != null && PortNumber.CONTROLLER.equals(o.port())) {
Saurav Das49cb5a12016-01-16 22:54:07 -08001013 ttBuilder.add(o);
1014 } else {
1015 log.warn("Only allowed treatments in versatile forwarding "
1016 + "objectives are punts to the controller");
1017 }
1018 } else {
1019 log.warn("Cannot process instruction in versatile fwd {}", ins);
1020 }
Saurav Das822c4e22015-10-23 10:51:11 -07001021 }
Charles Chan2df0e8a2017-01-09 11:45:08 -08001022 if (fwd.treatment().clearedDeferred()) {
1023 ttBuilder.wipeDeferred();
1024 }
Saurav Das822c4e22015-10-23 10:51:11 -07001025 }
Saurav Das822c4e22015-10-23 10:51:11 -07001026 if (fwd.nextId() != null) {
Saurav Das49cb5a12016-01-16 22:54:07 -08001027 // overide case
1028 NextGroup next = getGroupForNextObjective(fwd.nextId());
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +05301029 if (next == null) {
1030 fail(fwd, ObjectiveError.BADPARAMS);
1031 return Collections.emptySet();
1032 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001033 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1034 // we only need the top level group's key to point the flow to it
1035 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1036 if (group == null) {
1037 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1038 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1039 fail(fwd, ObjectiveError.GROUPMISSING);
1040 return Collections.emptySet();
1041 }
1042 ttBuilder.deferred().group(group.id());
Saurav Das822c4e22015-10-23 10:51:11 -07001043 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001044
1045 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1046 .fromApp(fwd.appId())
1047 .withPriority(fwd.priority())
1048 .forDevice(deviceId)
Saurav Das77b5e902016-01-27 17:01:59 -08001049 .withSelector(sbuilder.build())
Saurav Das49cb5a12016-01-16 22:54:07 -08001050 .withTreatment(ttBuilder.build())
1051 .makePermanent()
1052 .forTable(ACL_TABLE);
1053 return Collections.singletonList(ruleBuilder.build());
Saurav Das822c4e22015-10-23 10:51:11 -07001054 }
1055
1056 /**
1057 * In the OF-DPA 2.0 pipeline, specific forwarding refers to the IP table
Saurav Das8a0732e2015-11-20 15:27:53 -08001058 * (unicast or multicast) or the L2 table (mac + vlan) or the MPLS table.
Saurav Das822c4e22015-10-23 10:51:11 -07001059 *
1060 * @param fwd the forwarding objective of type 'specific'
1061 * @return a collection of flow rules. Typically there will be only one
1062 * for this type of forwarding objective. An empty set may be
1063 * returned if there is an issue in processing the objective.
1064 */
Saurav Das8a0732e2015-11-20 15:27:53 -08001065 protected Collection<FlowRule> processSpecific(ForwardingObjective fwd) {
Saurav Das25190812016-05-27 13:54:07 -07001066 log.debug("Processing specific fwd objective:{} in dev:{} with next:{}",
Saurav Das4ce45962015-11-24 23:21:05 -08001067 fwd.id(), deviceId, fwd.nextId());
1068 boolean isEthTypeObj = isSupportedEthTypeObjective(fwd);
1069 boolean isEthDstObj = isSupportedEthDstObjective(fwd);
1070
1071 if (isEthTypeObj) {
1072 return processEthTypeSpecific(fwd);
1073 } else if (isEthDstObj) {
1074 return processEthDstSpecific(fwd);
1075 } else {
1076 log.warn("processSpecific: Unsupported forwarding objective "
1077 + "criteria fwd:{} in dev:{}", fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001078 fail(fwd, ObjectiveError.UNSUPPORTED);
1079 return Collections.emptySet();
1080 }
Saurav Das4ce45962015-11-24 23:21:05 -08001081 }
1082
Saurav Das4ce45962015-11-24 23:21:05 -08001083 /**
1084 * Handles forwarding rules to the IP and MPLS tables.
1085 *
1086 * @param fwd the forwarding objective
1087 * @return A collection of flow rules, or an empty set
1088 */
1089 protected Collection<FlowRule> processEthTypeSpecific(ForwardingObjective fwd) {
Charles Chancad338a2016-09-16 18:03:11 -07001090 return processEthTypeSpecificInternal(fwd, false, ACL_TABLE);
Charles Chanf9e98652016-09-07 16:54:23 -07001091 }
1092
1093 /**
1094 * Internal implementation of processEthTypeSpecific.
1095 * <p>
1096 * Wildcarded IPv4_DST is not supported in OFDPA i12. Therefore, we break
1097 * the rule into 0.0.0.0/1 and 128.0.0.0/1.
1098 * The allowDefaultRoute must be set to false for OFDPA i12.
1099 * </p>
1100 *
1101 * @param fwd the forwarding objective
1102 * @param allowDefaultRoute allow wildcarded IPv4_DST or not
Ray Milkey0bb1e102016-11-10 14:51:27 -08001103 * @param mplsNextTable next MPLS table
Charles Chanf9e98652016-09-07 16:54:23 -07001104 * @return A collection of flow rules, or an empty set
1105 */
1106 protected Collection<FlowRule> processEthTypeSpecificInternal(ForwardingObjective fwd,
Charles Chancad338a2016-09-16 18:03:11 -07001107 boolean allowDefaultRoute,
1108 int mplsNextTable) {
Saurav Das4ce45962015-11-24 23:21:05 -08001109 TrafficSelector selector = fwd.selector();
1110 EthTypeCriterion ethType =
1111 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
Flavio Castroe10fa242016-01-15 12:43:51 -08001112 boolean popMpls = false;
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001113 boolean emptyGroup = false;
Charles Chan188ebf52015-12-23 00:15:11 -08001114 int forTableId;
Saurav Das8a0732e2015-11-20 15:27:53 -08001115 TrafficSelector.Builder filteredSelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -08001116 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
Flavio Castroe10fa242016-01-15 12:43:51 -08001117 TrafficSelector.Builder complementarySelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -08001118
Saurav Das8a0732e2015-11-20 15:27:53 -08001119 if (ethType.ethType().toShort() == Ethernet.TYPE_IPV4) {
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001120 if (buildIpv4Selector(filteredSelector, complementarySelector, fwd, allowDefaultRoute) < 0) {
1121 return Collections.emptyList();
1122 }
1123 // We need to set properly the next table
Flavio Castroe10fa242016-01-15 12:43:51 -08001124 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
Charles Chan5b9df8d2016-03-28 22:21:40 -07001125 if (ipv4Dst.isMulticast()) {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001126 forTableId = MULTICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -08001127 } else {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001128 forTableId = UNICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -08001129 }
Charles Chan14967c22015-12-07 11:11:50 -08001130 if (fwd.treatment() != null) {
1131 for (Instruction instr : fwd.treatment().allInstructions()) {
1132 if (instr instanceof L3ModificationInstruction &&
1133 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
Saurav Das9c705342017-01-06 11:36:01 -08001134 // XXX decrementing IP ttl is done automatically for routing, this
1135 // action is ignored or rejected in ofdpa as it is not fully implemented
1136 //tb.deferred().add(instr);
Charles Chan14967c22015-12-07 11:11:50 -08001137 }
1138 }
1139 }
1140
Pier Ventree0ae7a32016-11-23 09:57:42 -08001141 } else if (ethType.ethType().toShort() == Ethernet.TYPE_IPV6) {
1142 if (buildIpv6Selector(filteredSelector, fwd) < 0) {
1143 return Collections.emptyList();
1144 }
Kalhee Kim7c6f4f82017-10-04 14:59:11 +00001145 //We need to set the proper next table
1146 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
1147 if (ipv6Dst.isMulticast()) {
1148 forTableId = MULTICAST_ROUTING_TABLE;
1149 } else {
1150 forTableId = UNICAST_ROUTING_TABLE;
1151 }
1152
Pier Ventree0ae7a32016-11-23 09:57:42 -08001153 if (fwd.treatment() != null) {
1154 for (Instruction instr : fwd.treatment().allInstructions()) {
1155 if (instr instanceof L3ModificationInstruction &&
1156 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1157 // XXX decrementing IP ttl is done automatically for routing, this
1158 // action is ignored or rejected in ofdpa as it is not fully implemented
1159 //tb.deferred().add(instr);
1160 }
1161 }
1162 }
Saurav Das8a0732e2015-11-20 15:27:53 -08001163 } else {
1164 filteredSelector
1165 .matchEthType(Ethernet.MPLS_UNICAST)
1166 .matchMplsLabel(((MplsCriterion)
1167 selector.getCriterion(Criterion.Type.MPLS_LABEL)).label());
1168 MplsBosCriterion bos = (MplsBosCriterion) selector
Pier Ventre140a8942016-11-02 07:26:38 -07001169 .getCriterion(MPLS_BOS);
Saurav Das8a0732e2015-11-20 15:27:53 -08001170 if (bos != null) {
1171 filteredSelector.matchMplsBos(bos.mplsBos());
1172 }
1173 forTableId = MPLS_TABLE_1;
Saurav Das4ce45962015-11-24 23:21:05 -08001174 log.debug("processing MPLS specific forwarding objective {} -> next:{}"
1175 + " in dev {}", fwd.id(), fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001176
Charles Chan14967c22015-12-07 11:11:50 -08001177 if (fwd.treatment() != null) {
1178 for (Instruction instr : fwd.treatment().allInstructions()) {
1179 if (instr instanceof L2ModificationInstruction &&
1180 ((L2ModificationInstruction) instr).subtype() == L2SubType.MPLS_POP) {
Saurav Das8a0732e2015-11-20 15:27:53 -08001181 popMpls = true;
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001182 // OF-DPA does not pop in MPLS table in some cases. For the L3 VPN, it requires
Saurav Das9c705342017-01-06 11:36:01 -08001183 // setting the MPLS_TYPE so pop can happen down the pipeline
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001184 if (mplsNextTable == MPLS_TYPE_TABLE && isNotMplsBos(selector)) {
1185 tb.immediate().popMpls();
1186 }
Charles Chan14967c22015-12-07 11:11:50 -08001187 }
1188 if (instr instanceof L3ModificationInstruction &&
1189 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1190 // FIXME Should modify the app to send the correct DEC_MPLS_TTL instruction
1191 tb.immediate().decMplsTtl();
1192 }
1193 if (instr instanceof L3ModificationInstruction &&
1194 ((L3ModificationInstruction) instr).subtype() == L3SubType.TTL_IN) {
1195 tb.immediate().add(instr);
1196 }
Saurav Das8a0732e2015-11-20 15:27:53 -08001197 }
1198 }
1199 }
Saurav Das822c4e22015-10-23 10:51:11 -07001200
1201 if (fwd.nextId() != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -08001202 if (forTableId == MPLS_TABLE_1 && !popMpls) {
1203 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
Saurav Das25190812016-05-27 13:54:07 -07001204 + "is not implemented in OF-DPA yet. Aborting this flow {} -> next:{}"
1205 + "in this device {}", fwd.id(), fwd.nextId(), deviceId);
Pier Ventree0ae7a32016-11-23 09:57:42 -08001206 // XXX We could convert to forwarding to a single-port, via a MPLS interface,
1207 // or a MPLS SWAP (with-same) but that would have to be handled in the next-objective.
1208 // Also the pop-mpls logic used here won't work in non-BoS case.
Saurav Das4ce45962015-11-24 23:21:05 -08001209 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
Saurav Das8a0732e2015-11-20 15:27:53 -08001210 return Collections.emptySet();
1211 }
1212
Saurav Das423fe2b2015-12-04 10:52:59 -08001213 NextGroup next = getGroupForNextObjective(fwd.nextId());
1214 if (next != null) {
1215 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1216 // we only need the top level group's key to point the flow to it
1217 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
Pier Ventre140a8942016-11-02 07:26:38 -07001218 if (isNotMplsBos(selector) && group.type().equals(HASHED)) {
1219 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
1220 + "is not implemented in OF-DPA yet. Aborting this flow {} -> next:{}"
1221 + "in this device {}", fwd.id(), fwd.nextId(), deviceId);
1222 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1223 return Collections.emptySet();
1224 }
Saurav Das423fe2b2015-12-04 10:52:59 -08001225 if (group == null) {
1226 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1227 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1228 fail(fwd, ObjectiveError.GROUPMISSING);
1229 return Collections.emptySet();
1230 }
1231 tb.deferred().group(group.id());
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001232 // check if group is empty
1233 if (gkeys.size() == 1 && gkeys.get(0).size() == 1) {
1234 log.warn("Found empty group 0x{} in dev:{} .. will retry fwd:{}",
1235 Integer.toHexString(group.id().id()), deviceId, fwd.id());
1236 emptyGroup = true;
1237 }
Saurav Das25190812016-05-27 13:54:07 -07001238 } else {
1239 log.warn("Cannot find group for nextId:{} in dev:{}. Aborting fwd:{}",
1240 fwd.nextId(), deviceId, fwd.id());
1241 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1242 return Collections.emptySet();
Saurav Das822c4e22015-10-23 10:51:11 -07001243 }
Saurav Das822c4e22015-10-23 10:51:11 -07001244 }
Charles Chancad338a2016-09-16 18:03:11 -07001245
1246 if (forTableId == MPLS_TABLE_1) {
Pier Ventre140a8942016-11-02 07:26:38 -07001247 if (mplsNextTable == MPLS_L3_TYPE_TABLE) {
Charles Chancad338a2016-09-16 18:03:11 -07001248 Ofdpa3SetMplsType setMplsType = new Ofdpa3SetMplsType(Ofdpa3MplsType.L3_PHP);
Saurav Das9c705342017-01-06 11:36:01 -08001249 // set mpls type as apply_action
1250 tb.immediate().extension(setMplsType, deviceId);
Charles Chancad338a2016-09-16 18:03:11 -07001251 }
1252 tb.transition(mplsNextTable);
1253 } else {
1254 tb.transition(ACL_TABLE);
1255 }
1256
Saurav Das822c4e22015-10-23 10:51:11 -07001257 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1258 .fromApp(fwd.appId())
1259 .withPriority(fwd.priority())
1260 .forDevice(deviceId)
Saurav Das8a0732e2015-11-20 15:27:53 -08001261 .withSelector(filteredSelector.build())
1262 .withTreatment(tb.build())
1263 .forTable(forTableId);
Saurav Das822c4e22015-10-23 10:51:11 -07001264
1265 if (fwd.permanent()) {
1266 ruleBuilder.makePermanent();
1267 } else {
1268 ruleBuilder.makeTemporary(fwd.timeout());
1269 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001270 Collection<FlowRule> flowRuleCollection = new ArrayList<>();
1271 flowRuleCollection.add(ruleBuilder.build());
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001272 if (!allowDefaultRoute) {
Pier Ventree0ae7a32016-11-23 09:57:42 -08001273 flowRuleCollection.add(
1274 defaultRoute(fwd, complementarySelector, forTableId, tb)
1275 );
Flavio Castroe10fa242016-01-15 12:43:51 -08001276 log.debug("Default rule 0.0.0.0/0 is being installed two rules");
1277 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001278 // XXX retrying flows may be necessary due to bug CORD-554
1279 if (emptyGroup) {
1280 executorService.schedule(new RetryFlows(fwd, flowRuleCollection),
1281 RETRY_MS, TimeUnit.MILLISECONDS);
1282 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001283 return flowRuleCollection;
Saurav Das822c4e22015-10-23 10:51:11 -07001284 }
1285
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001286 protected int buildIpv4Selector(TrafficSelector.Builder builderToUpdate,
1287 TrafficSelector.Builder extBuilder,
1288 ForwardingObjective fwd,
1289 boolean allowDefaultRoute) {
1290 TrafficSelector selector = fwd.selector();
1291
1292 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
1293 if (ipv4Dst.isMulticast()) {
1294 if (ipv4Dst.prefixLength() != 32) {
1295 log.warn("Multicast specific forwarding objective can only be /32");
1296 fail(fwd, ObjectiveError.BADPARAMS);
1297 return -1;
1298 }
1299 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
1300 if (assignedVlan == null) {
1301 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
1302 fail(fwd, ObjectiveError.BADPARAMS);
1303 return -1;
1304 }
Charles Chand1172632017-03-15 17:33:09 -07001305 if (requireVlanExtensions()) {
1306 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
1307 builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
1308 } else {
1309 builderToUpdate.matchVlanId(assignedVlan);
1310 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001311 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1312 log.debug("processing IPv4 multicast specific forwarding objective {} -> next:{}"
1313 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1314 } else {
1315 if (ipv4Dst.prefixLength() == 0) {
1316 if (allowDefaultRoute) {
1317 // The entire IPV4_DST field is wildcarded intentionally
1318 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4);
1319 } else {
1320 // NOTE: The switch does not support matching 0.0.0.0/0
1321 // Split it into 0.0.0.0/1 and 128.0.0.0/1
1322 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4)
1323 .matchIPDst(IpPrefix.valueOf("0.0.0.0/1"));
1324 extBuilder.matchEthType(Ethernet.TYPE_IPV4)
1325 .matchIPDst(IpPrefix.valueOf("128.0.0.0/1"));
1326 }
1327 } else {
1328 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1329 }
1330 log.debug("processing IPv4 unicast specific forwarding objective {} -> next:{}"
1331 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1332 }
1333 return 0;
1334 }
1335
1336 /**
1337 * Helper method to build Ipv6 selector using the selector provided by
1338 * a forwarding objective.
1339 *
1340 * @param builderToUpdate the builder to update
1341 * @param fwd the selector to read
1342 * @return 0 if the update ends correctly. -1 if the matches
1343 * are not yet supported
1344 */
1345 protected int buildIpv6Selector(TrafficSelector.Builder builderToUpdate,
1346 ForwardingObjective fwd) {
1347
1348 TrafficSelector selector = fwd.selector();
1349
1350 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
1351 if (ipv6Dst.isMulticast()) {
Julia Fergusond8f145e2017-08-10 18:15:24 +00001352 if (ipv6Dst.prefixLength() != IpAddress.INET6_BIT_LENGTH) {
1353 log.warn("Multicast specific forwarding objective can only be /128");
1354 fail(fwd, ObjectiveError.BADPARAMS);
1355 return -1;
1356 }
1357 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
1358 if (assignedVlan == null) {
1359 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
1360 fail(fwd, ObjectiveError.BADPARAMS);
1361 return -1;
1362 }
1363 if (requireVlanExtensions()) {
1364 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
1365 builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
1366 } else {
1367 builderToUpdate.matchVlanId(assignedVlan);
1368 }
1369 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6).matchIPv6Dst(ipv6Dst);
1370 log.debug("processing IPv6 multicast specific forwarding objective {} -> next:{}"
1371 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1372 } else {
1373 if (ipv6Dst.prefixLength() != 0) {
1374 builderToUpdate.matchIPv6Dst(ipv6Dst);
1375 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001376 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6);
1377 log.debug("processing IPv6 unicast specific forwarding objective {} -> next:{}"
1378 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Julia Fergusond8f145e2017-08-10 18:15:24 +00001379 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001380 return 0;
1381 }
1382
Pier Ventree0ae7a32016-11-23 09:57:42 -08001383 protected FlowRule defaultRoute(ForwardingObjective fwd,
1384 TrafficSelector.Builder complementarySelector,
1385 int forTableId,
1386 TrafficTreatment.Builder tb) {
1387 FlowRule.Builder rule = DefaultFlowRule.builder()
1388 .fromApp(fwd.appId())
1389 .withPriority(fwd.priority())
1390 .forDevice(deviceId)
1391 .withSelector(complementarySelector.build())
1392 .withTreatment(tb.build())
1393 .forTable(forTableId);
1394 if (fwd.permanent()) {
1395 rule.makePermanent();
1396 } else {
1397 rule.makeTemporary(fwd.timeout());
1398 }
1399 return rule.build();
1400 }
1401
Saurav Das4ce45962015-11-24 23:21:05 -08001402 /**
1403 * Handles forwarding rules to the L2 bridging table. Flow actions are not
1404 * allowed in the bridging table - instead we use L2 Interface group or
1405 * L2 flood group
1406 *
1407 * @param fwd the forwarding objective
1408 * @return A collection of flow rules, or an empty set
1409 */
1410 protected Collection<FlowRule> processEthDstSpecific(ForwardingObjective fwd) {
1411 List<FlowRule> rules = new ArrayList<>();
1412
1413 // Build filtered selector
1414 TrafficSelector selector = fwd.selector();
1415 EthCriterion ethCriterion = (EthCriterion) selector
1416 .getCriterion(Criterion.Type.ETH_DST);
1417 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) selector
1418 .getCriterion(Criterion.Type.VLAN_VID);
1419
1420 if (vlanIdCriterion == null) {
1421 log.warn("Forwarding objective for bridging requires vlan. Not "
1422 + "installing fwd:{} in dev:{}", fwd.id(), deviceId);
1423 fail(fwd, ObjectiveError.BADPARAMS);
1424 return Collections.emptySet();
1425 }
1426
1427 TrafficSelector.Builder filteredSelectorBuilder =
1428 DefaultTrafficSelector.builder();
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001429
1430 if (!ethCriterion.mac().equals(NONE) &&
1431 !ethCriterion.mac().equals(BROADCAST)) {
Saurav Das4ce45962015-11-24 23:21:05 -08001432 filteredSelectorBuilder.matchEthDst(ethCriterion.mac());
1433 log.debug("processing L2 forwarding objective:{} -> next:{} in dev:{}",
1434 fwd.id(), fwd.nextId(), deviceId);
1435 } else {
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001436 // Use wildcard DST_MAC if the MacAddress is None or Broadcast
Saurav Das4ce45962015-11-24 23:21:05 -08001437 log.debug("processing L2 Broadcast forwarding objective:{} -> next:{} "
1438 + "in dev:{} for vlan:{}",
1439 fwd.id(), fwd.nextId(), deviceId, vlanIdCriterion.vlanId());
1440 }
Charles Chand1172632017-03-15 17:33:09 -07001441 if (requireVlanExtensions()) {
1442 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanIdCriterion.vlanId());
1443 filteredSelectorBuilder.extension(ofdpaMatchVlanVid, deviceId);
1444 } else {
1445 filteredSelectorBuilder.matchVlanId(vlanIdCriterion.vlanId());
1446 }
Saurav Das4ce45962015-11-24 23:21:05 -08001447 TrafficSelector filteredSelector = filteredSelectorBuilder.build();
1448
1449 if (fwd.treatment() != null) {
1450 log.warn("Ignoring traffic treatment in fwd rule {} meant for L2 table"
1451 + "for dev:{}. Expecting only nextId", fwd.id(), deviceId);
1452 }
1453
1454 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1455 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001456 NextGroup next = getGroupForNextObjective(fwd.nextId());
Saurav Das4ce45962015-11-24 23:21:05 -08001457 if (next != null) {
1458 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1459 // we only need the top level group's key to point the flow to it
1460 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1461 if (group != null) {
1462 treatmentBuilder.deferred().group(group.id());
1463 } else {
1464 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1465 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1466 fail(fwd, ObjectiveError.GROUPMISSING);
1467 return Collections.emptySet();
1468 }
1469 }
1470 }
1471 treatmentBuilder.immediate().transition(ACL_TABLE);
1472 TrafficTreatment filteredTreatment = treatmentBuilder.build();
1473
1474 // Build bridging table entries
1475 FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder();
1476 flowRuleBuilder.fromApp(fwd.appId())
1477 .withPriority(fwd.priority())
1478 .forDevice(deviceId)
1479 .withSelector(filteredSelector)
1480 .withTreatment(filteredTreatment)
1481 .forTable(BRIDGING_TABLE);
1482 if (fwd.permanent()) {
1483 flowRuleBuilder.makePermanent();
1484 } else {
1485 flowRuleBuilder.makeTemporary(fwd.timeout());
1486 }
1487 rules.add(flowRuleBuilder.build());
1488 return rules;
1489 }
1490
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001491 //////////////////////////////////////
1492 // Helper Methods and Classes
1493 //////////////////////////////////////
1494
1495 private boolean isSupportedEthTypeObjective(ForwardingObjective fwd) {
1496 TrafficSelector selector = fwd.selector();
1497 EthTypeCriterion ethType = (EthTypeCriterion) selector
1498 .getCriterion(Criterion.Type.ETH_TYPE);
1499 return !((ethType == null) ||
1500 ((ethType.ethType().toShort() != Ethernet.TYPE_IPV4) &&
Pier Ventree0ae7a32016-11-23 09:57:42 -08001501 (ethType.ethType().toShort() != Ethernet.MPLS_UNICAST)) &&
1502 (ethType.ethType().toShort() != Ethernet.TYPE_IPV6));
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001503 }
1504
1505 private boolean isSupportedEthDstObjective(ForwardingObjective fwd) {
1506 TrafficSelector selector = fwd.selector();
1507 EthCriterion ethDst = (EthCriterion) selector
1508 .getCriterion(Criterion.Type.ETH_DST);
1509 VlanIdCriterion vlanId = (VlanIdCriterion) selector
1510 .getCriterion(Criterion.Type.VLAN_VID);
1511 return !(ethDst == null && vlanId == null);
1512 }
1513
Saurav Das423fe2b2015-12-04 10:52:59 -08001514 protected NextGroup getGroupForNextObjective(Integer nextId) {
1515 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
1516 if (next != null) {
1517 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1518 if (gkeys != null && !gkeys.isEmpty()) {
1519 return next;
1520 } else {
1521 log.warn("Empty next group found in FlowObjective store for "
1522 + "next-id:{} in dev:{}", nextId, deviceId);
1523 }
1524 } else {
1525 log.warn("next-id {} not found in Flow objective store for dev:{}",
1526 nextId, deviceId);
1527 }
1528 return null;
1529 }
1530
Charles Chan188ebf52015-12-23 00:15:11 -08001531 protected static void pass(Objective obj) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001532 obj.context().ifPresent(context -> context.onSuccess(obj));
Saurav Das822c4e22015-10-23 10:51:11 -07001533 }
1534
Charles Chan188ebf52015-12-23 00:15:11 -08001535 protected static void fail(Objective obj, ObjectiveError error) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001536 obj.context().ifPresent(context -> context.onError(obj, error));
Saurav Das822c4e22015-10-23 10:51:11 -07001537 }
Saurav Das24431192016-03-07 19:13:00 -08001538
Saurav Das24431192016-03-07 19:13:00 -08001539 @Override
1540 public List<String> getNextMappings(NextGroup nextGroup) {
1541 List<String> mappings = new ArrayList<>();
1542 List<Deque<GroupKey>> gkeys = appKryo.deserialize(nextGroup.data());
1543 for (Deque<GroupKey> gkd : gkeys) {
1544 Group lastGroup = null;
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -07001545 StringBuilder gchain = new StringBuilder();
Saurav Das24431192016-03-07 19:13:00 -08001546 for (GroupKey gk : gkd) {
1547 Group g = groupService.getGroup(deviceId, gk);
Saurav Das8be4e3a2016-03-11 17:19:07 -08001548 if (g == null) {
Saurav Das25190812016-05-27 13:54:07 -07001549 gchain.append(" NoGrp").append(" -->");
Saurav Das8be4e3a2016-03-11 17:19:07 -08001550 continue;
1551 }
1552 gchain.append(" 0x").append(Integer.toHexString(g.id().id()))
1553 .append(" -->");
Saurav Das24431192016-03-07 19:13:00 -08001554 lastGroup = g;
1555 }
1556 // add port information for last group in group-chain
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -07001557 List<Instruction> lastGroupIns = new ArrayList<>();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001558 if (lastGroup != null && !lastGroup.buckets().buckets().isEmpty()) {
Saurav Das25190812016-05-27 13:54:07 -07001559 lastGroupIns = lastGroup.buckets().buckets().get(0)
1560 .treatment().allInstructions();
1561 }
1562 for (Instruction i: lastGroupIns) {
Saurav Das24431192016-03-07 19:13:00 -08001563 if (i instanceof OutputInstruction) {
Saurav Das8be4e3a2016-03-11 17:19:07 -08001564 gchain.append(" port:").append(((OutputInstruction) i).port());
Saurav Das24431192016-03-07 19:13:00 -08001565 }
1566 }
Saurav Das8be4e3a2016-03-11 17:19:07 -08001567 mappings.add(gchain.toString());
Saurav Das24431192016-03-07 19:13:00 -08001568 }
1569 return mappings;
1570 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001571
Pier Ventre140a8942016-11-02 07:26:38 -07001572 static boolean isMplsBos(TrafficSelector selector) {
1573 MplsBosCriterion bosCriterion = (MplsBosCriterion) selector.getCriterion(MPLS_BOS);
1574 return bosCriterion != null && bosCriterion.mplsBos();
1575 }
1576
1577 static boolean isNotMplsBos(TrafficSelector selector) {
1578 MplsBosCriterion bosCriterion = (MplsBosCriterion) selector.getCriterion(MPLS_BOS);
1579 return bosCriterion != null && !bosCriterion.mplsBos();
1580 }
1581
Charles Chan30122a72017-10-05 15:17:15 -07001582 private static boolean isIpv6(TrafficSelector selector) {
1583 EthTypeCriterion ethTypeCriterion = (EthTypeCriterion) selector.getCriterion(ETH_TYPE);
1584 return ethTypeCriterion != null && ethTypeCriterion.ethType().toShort() == Ethernet.TYPE_IPV6;
1585 }
1586
1587 static VlanId readVlanFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001588 if (selector == null) {
1589 return null;
1590 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001591 Criterion criterion = selector.getCriterion(Criterion.Type.VLAN_VID);
1592 return (criterion == null)
1593 ? null : ((VlanIdCriterion) criterion).vlanId();
1594 }
1595
Charles Chan30122a72017-10-05 15:17:15 -07001596 static IpPrefix readIpDstFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001597 if (selector == null) {
1598 return null;
1599 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001600 Criterion criterion = selector.getCriterion(Criterion.Type.IPV4_DST);
1601 return (criterion == null) ? null : ((IPCriterion) criterion).ip();
1602 }
Charles Chand55e84d2016-03-30 17:54:24 -07001603
1604 private static VlanId readVlanFromTreatment(TrafficTreatment treatment) {
Saurav Das59232cf2016-04-27 18:35:50 -07001605 if (treatment == null) {
1606 return null;
1607 }
Charles Chand55e84d2016-03-30 17:54:24 -07001608 for (Instruction i : treatment.allInstructions()) {
1609 if (i instanceof ModVlanIdInstruction) {
1610 return ((ModVlanIdInstruction) i).vlanId();
1611 }
1612 }
1613 return null;
1614 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001615
1616 /**
1617 * Utility class that retries sending flows a fixed number of times, even if
1618 * some of the attempts are successful. Used only for forwarding objectives.
1619 */
1620 protected final class RetryFlows implements Runnable {
1621 int attempts = MAX_RETRY_ATTEMPTS;
1622 private Collection<FlowRule> retryFlows;
1623 private ForwardingObjective fwd;
1624
1625 RetryFlows(ForwardingObjective fwd, Collection<FlowRule> retryFlows) {
1626 this.fwd = fwd;
1627 this.retryFlows = retryFlows;
1628 }
1629
1630 @Override
1631 public void run() {
1632 log.info("RETRY FLOWS ATTEMPT# {} for fwd:{} rules:{}",
1633 MAX_RETRY_ATTEMPTS - attempts, fwd.id(), retryFlows.size());
1634 sendForward(fwd, retryFlows);
1635 if (--attempts > 0) {
1636 executorService.schedule(this, RETRY_MS, TimeUnit.MILLISECONDS);
1637 }
1638 }
1639 }
1640
Saurav Das822c4e22015-10-23 10:51:11 -07001641}