blob: b18081cd0aba806859cbf406563eeca4c2bbc9f6 [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 Ferguson65428c32017-08-10 18:15:24 +000022import org.onlab.packet.IpAddress;
Charles Chan14967c22015-12-07 11:11:50 -080023import org.onlab.packet.IpPrefix;
Saurav Das822c4e22015-10-23 10:51:11 -070024import org.onlab.packet.VlanId;
25import org.onlab.util.KryoNamespace;
26import org.onosproject.core.ApplicationId;
27import org.onosproject.core.CoreService;
Charles Chancad338a2016-09-16 18:03:11 -070028import org.onosproject.driver.extensions.Ofdpa3MplsType;
29import org.onosproject.driver.extensions.Ofdpa3SetMplsType;
Charles Chan14967c22015-12-07 11:11:50 -080030import org.onosproject.driver.extensions.OfdpaMatchVlanVid;
31import org.onosproject.driver.extensions.OfdpaSetVlanVid;
Saurav Das822c4e22015-10-23 10:51:11 -070032import org.onosproject.net.DeviceId;
33import org.onosproject.net.Port;
34import org.onosproject.net.PortNumber;
35import org.onosproject.net.behaviour.NextGroup;
36import org.onosproject.net.behaviour.Pipeliner;
37import org.onosproject.net.behaviour.PipelinerContext;
38import org.onosproject.net.device.DeviceService;
39import org.onosproject.net.driver.AbstractHandlerBehaviour;
40import org.onosproject.net.flow.DefaultFlowRule;
41import org.onosproject.net.flow.DefaultTrafficSelector;
42import org.onosproject.net.flow.DefaultTrafficTreatment;
43import org.onosproject.net.flow.FlowRule;
44import org.onosproject.net.flow.FlowRuleOperations;
45import org.onosproject.net.flow.FlowRuleOperationsContext;
46import org.onosproject.net.flow.FlowRuleService;
47import org.onosproject.net.flow.TrafficSelector;
48import org.onosproject.net.flow.TrafficTreatment;
49import org.onosproject.net.flow.criteria.Criteria;
50import org.onosproject.net.flow.criteria.Criterion;
51import org.onosproject.net.flow.criteria.EthCriterion;
52import org.onosproject.net.flow.criteria.EthTypeCriterion;
Charles Chan14967c22015-12-07 11:11:50 -080053import org.onosproject.net.flow.criteria.ExtensionCriterion;
Saurav Das822c4e22015-10-23 10:51:11 -070054import org.onosproject.net.flow.criteria.IPCriterion;
Pier Ventree0ae7a32016-11-23 09:57:42 -080055import org.onosproject.net.flow.criteria.Icmpv6CodeCriterion;
56import org.onosproject.net.flow.criteria.Icmpv6TypeCriterion;
Saurav Das8a0732e2015-11-20 15:27:53 -080057import org.onosproject.net.flow.criteria.MplsBosCriterion;
58import org.onosproject.net.flow.criteria.MplsCriterion;
Saurav Das822c4e22015-10-23 10:51:11 -070059import org.onosproject.net.flow.criteria.PortCriterion;
60import org.onosproject.net.flow.criteria.VlanIdCriterion;
61import org.onosproject.net.flow.instructions.Instruction;
62import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
63import org.onosproject.net.flow.instructions.L2ModificationInstruction;
Saurav Das8a0732e2015-11-20 15:27:53 -080064import org.onosproject.net.flow.instructions.L2ModificationInstruction.L2SubType;
Saurav Das822c4e22015-10-23 10:51:11 -070065import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanIdInstruction;
Charles Chan14967c22015-12-07 11:11:50 -080066import org.onosproject.net.flow.instructions.L3ModificationInstruction;
67import org.onosproject.net.flow.instructions.L3ModificationInstruction.L3SubType;
Saurav Das822c4e22015-10-23 10:51:11 -070068import org.onosproject.net.flowobjective.FilteringObjective;
69import org.onosproject.net.flowobjective.FlowObjectiveStore;
70import org.onosproject.net.flowobjective.ForwardingObjective;
71import org.onosproject.net.flowobjective.NextObjective;
72import org.onosproject.net.flowobjective.Objective;
73import org.onosproject.net.flowobjective.ObjectiveError;
Saurav Das822c4e22015-10-23 10:51:11 -070074import org.onosproject.net.group.DefaultGroupKey;
75import org.onosproject.net.group.Group;
Saurav Das822c4e22015-10-23 10:51:11 -070076import org.onosproject.net.group.GroupKey;
Saurav Das822c4e22015-10-23 10:51:11 -070077import org.onosproject.net.group.GroupService;
Saurav Das822c4e22015-10-23 10:51:11 -070078import org.onosproject.store.serializers.KryoNamespaces;
79import org.slf4j.Logger;
80
Pier Ventree0ae7a32016-11-23 09:57:42 -080081import java.util.ArrayDeque;
82import java.util.ArrayList;
83import java.util.Collection;
84import java.util.Collections;
85import java.util.Deque;
86import java.util.List;
87import java.util.Objects;
Pier Ventree0ae7a32016-11-23 09:57:42 -080088import java.util.concurrent.ScheduledExecutorService;
89import java.util.concurrent.TimeUnit;
90
91import static java.util.concurrent.Executors.newScheduledThreadPool;
Yi Tseng3a77b4f2017-02-06 15:02:17 -080092import static org.onlab.packet.MacAddress.BROADCAST;
93import static org.onlab.packet.MacAddress.NONE;
Pier Ventree0ae7a32016-11-23 09:57:42 -080094import static org.onlab.util.Tools.groupedThreads;
Yi Tsengef19de12017-04-24 11:33:05 -070095import static org.onosproject.driver.pipeline.ofdpa.OfdpaGroupHandlerUtility.*;
Pier Ventree0ae7a32016-11-23 09:57:42 -080096import static org.slf4j.LoggerFactory.getLogger;
Pier Ventre140a8942016-11-02 07:26:38 -070097import static org.onosproject.net.flow.criteria.Criterion.Type.MPLS_BOS;
98import static org.onosproject.net.flowobjective.NextObjective.Type.HASHED;
Pier Ventree0ae7a32016-11-23 09:57:42 -080099
Saurav Das822c4e22015-10-23 10:51:11 -0700100/**
101 * Driver for Broadcom's OF-DPA v2.0 TTP.
Saurav Das822c4e22015-10-23 10:51:11 -0700102 */
Charles Chan361154b2016-03-24 10:23:39 -0700103public class Ofdpa2Pipeline extends AbstractHandlerBehaviour implements Pipeliner {
Pier Ventre42287df2016-11-09 14:17:26 -0800104
Saurav Das822c4e22015-10-23 10:51:11 -0700105 protected static final int PORT_TABLE = 0;
106 protected static final int VLAN_TABLE = 10;
Pier Ventre42287df2016-11-09 14:17:26 -0800107 protected static final int VLAN_1_TABLE = 11;
108 protected static final int MPLS_L2_PORT_FLOW_TABLE = 13;
109 protected static final int MPLS_L2_PORT_PCP_TRUST_FLOW_TABLE = 16;
Saurav Das822c4e22015-10-23 10:51:11 -0700110 protected static final int TMAC_TABLE = 20;
111 protected static final int UNICAST_ROUTING_TABLE = 30;
112 protected static final int MULTICAST_ROUTING_TABLE = 40;
113 protected static final int MPLS_TABLE_0 = 23;
114 protected static final int MPLS_TABLE_1 = 24;
Pier Ventre140a8942016-11-02 07:26:38 -0700115 protected static final int MPLS_L3_TYPE_TABLE = 27;
116 protected static final int MPLS_TYPE_TABLE = 29;
Saurav Das822c4e22015-10-23 10:51:11 -0700117 protected static final int BRIDGING_TABLE = 50;
118 protected static final int ACL_TABLE = 60;
119 protected static final int MAC_LEARNING_TABLE = 254;
120 protected static final long OFPP_MAX = 0xffffff00L;
121
Saurav Das52025962016-01-28 22:30:01 -0800122 protected static final int HIGHEST_PRIORITY = 0xffff;
Saurav Das2857f382015-11-03 14:39:27 -0800123 protected static final int DEFAULT_PRIORITY = 0x8000;
Saurav Das822c4e22015-10-23 10:51:11 -0700124 protected static final int LOWEST_PRIORITY = 0x0;
125
Pier Ventre42287df2016-11-09 14:17:26 -0800126 protected static final int MPLS_L2_PORT_PRIORITY = 2;
127
128 protected static final int MPLS_TUNNEL_ID_BASE = 0x10000;
129 protected static final int MPLS_TUNNEL_ID_MAX = 0x1FFFF;
130
Pier Ventre70d53ba2016-11-17 22:26:29 -0800131 protected static final int MPLS_UNI_PORT_MAX = 0x0000FFFF;
132
133 protected static final int MPLS_NNI_PORT_BASE = 0x00020000;
134 protected static final int MPLS_NNI_PORT_MAX = 0x0002FFFF;
135
Saurav Das822c4e22015-10-23 10:51:11 -0700136 private final Logger log = getLogger(getClass());
Charles Chan425854b2016-04-11 15:32:12 -0700137 protected ServiceDirectory serviceDirectory;
Saurav Das822c4e22015-10-23 10:51:11 -0700138 protected FlowRuleService flowRuleService;
Charles Chan425854b2016-04-11 15:32:12 -0700139 protected CoreService coreService;
Saurav Das8a0732e2015-11-20 15:27:53 -0800140 protected GroupService groupService;
141 protected FlowObjectiveStore flowObjectiveStore;
Saurav Das822c4e22015-10-23 10:51:11 -0700142 protected DeviceId deviceId;
143 protected ApplicationId driverId;
Saurav Das822c4e22015-10-23 10:51:11 -0700144 protected DeviceService deviceService;
Charles Chan188ebf52015-12-23 00:15:11 -0800145 protected static KryoNamespace appKryo = new KryoNamespace.Builder()
Yi Tsengef19de12017-04-24 11:33:05 -0700146 .register(KryoNamespaces.API)
147 .register(GroupKey.class)
148 .register(DefaultGroupKey.class)
149 .register(OfdpaNextGroup.class)
150 .register(ArrayDeque.class)
151 .build("Ofdpa2Pipeline");
Saurav Das822c4e22015-10-23 10:51:11 -0700152
Charles Chan425854b2016-04-11 15:32:12 -0700153 protected Ofdpa2GroupHandler groupHandler;
Saurav Das822c4e22015-10-23 10:51:11 -0700154
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700155 // flows installations to be retried
156 protected ScheduledExecutorService executorService
157 = newScheduledThreadPool(5, groupedThreads("OfdpaPipeliner", "retry-%d", log));
158 protected static final int MAX_RETRY_ATTEMPTS = 10;
159 protected static final int RETRY_MS = 1000;
Saurav Das4f980082015-11-05 13:39:15 -0800160
Saurav Das822c4e22015-10-23 10:51:11 -0700161 @Override
162 public void init(DeviceId deviceId, PipelinerContext context) {
Saurav Das822c4e22015-10-23 10:51:11 -0700163 this.deviceId = deviceId;
164
Charles Chan425854b2016-04-11 15:32:12 -0700165 serviceDirectory = context.directory();
Saurav Das822c4e22015-10-23 10:51:11 -0700166 coreService = serviceDirectory.get(CoreService.class);
167 flowRuleService = serviceDirectory.get(FlowRuleService.class);
168 groupService = serviceDirectory.get(GroupService.class);
169 flowObjectiveStore = context.store();
Saurav Das822c4e22015-10-23 10:51:11 -0700170 deviceService = serviceDirectory.get(DeviceService.class);
Saurav Das822c4e22015-10-23 10:51:11 -0700171
Charles Chan40132b32017-01-22 00:19:37 -0800172 initDriverId();
173 initGroupHander(context);
Saurav Das822c4e22015-10-23 10:51:11 -0700174
Saurav Das822c4e22015-10-23 10:51:11 -0700175 initializePipeline();
Saurav Das822c4e22015-10-23 10:51:11 -0700176 }
177
Charles Chan40132b32017-01-22 00:19:37 -0800178 protected void initDriverId() {
179 driverId = coreService.registerApplication(
180 "org.onosproject.driver.Ofdpa2Pipeline");
181 }
182
183 protected void initGroupHander(PipelinerContext context) {
184 groupHandler = new Ofdpa2GroupHandler();
185 groupHandler.init(deviceId, context);
186 }
187
Saurav Das822c4e22015-10-23 10:51:11 -0700188 protected void initializePipeline() {
Charles Chan188ebf52015-12-23 00:15:11 -0800189 // OF-DPA does not require initializing the pipeline as it puts default
190 // rules automatically in the hardware. However emulation of OFDPA in
191 // software switches does require table-miss-entries.
Saurav Das822c4e22015-10-23 10:51:11 -0700192 }
193
Charles Chand1172632017-03-15 17:33:09 -0700194 /**
195 * Determines whether this pipeline requires OFDPA match and set VLAN extensions.
196 *
197 * @return true to use the extensions
198 */
199 protected boolean requireVlanExtensions() {
200 return true;
201 }
202
Saurav Das86d13e82017-04-28 17:03:48 -0700203 /**
204 * Determines whether in-port should be matched on in TMAC table rules.
205 *
206 * @return true if match on in-port should be programmed
207 */
208 protected boolean matchInPortTmacTable() {
209 return true;
210 }
211
Saurav Das822c4e22015-10-23 10:51:11 -0700212 //////////////////////////////////////
213 // Flow Objectives
214 //////////////////////////////////////
215
216 @Override
217 public void filter(FilteringObjective filteringObjective) {
218 if (filteringObjective.type() == FilteringObjective.Type.PERMIT) {
219 processFilter(filteringObjective,
220 filteringObjective.op() == Objective.Operation.ADD,
221 filteringObjective.appId());
222 } else {
223 // Note that packets that don't match the PERMIT filter are
224 // automatically denied. The DENY filter is used to deny packets
225 // that are otherwise permitted by the PERMIT filter.
226 // Use ACL table flow rules here for DENY filtering objectives
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530227 log.warn("filter objective other than PERMIT currently not supported");
Saurav Das822c4e22015-10-23 10:51:11 -0700228 fail(filteringObjective, ObjectiveError.UNSUPPORTED);
229 }
230 }
231
232 @Override
233 public void forward(ForwardingObjective fwd) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700234 Collection<FlowRule> rules = processForward(fwd);
Saurav Das25190812016-05-27 13:54:07 -0700235 if (rules == null || rules.isEmpty()) {
236 // Assumes fail message has already been generated to the objective
237 // context. Returning here prevents spurious pass message to be
238 // generated by FlowRule service for empty flowOps.
239 return;
240 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700241 sendForward(fwd, rules);
242 }
243
244 protected void sendForward(ForwardingObjective fwd, Collection<FlowRule> rules) {
245 FlowRuleOperations.Builder flowOpsBuilder = FlowRuleOperations.builder();
Saurav Das822c4e22015-10-23 10:51:11 -0700246 switch (fwd.op()) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700247 case ADD:
248 rules.stream()
249 .filter(Objects::nonNull)
250 .forEach(flowOpsBuilder::add);
Saurav Dasd2fded02016-12-02 15:43:47 -0800251 log.debug("Applying a add fwd-obj {} to sw:{}", fwd.id(), deviceId);
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700252 break;
253 case REMOVE:
254 rules.stream()
255 .filter(Objects::nonNull)
256 .forEach(flowOpsBuilder::remove);
Pier Ventre42287df2016-11-09 14:17:26 -0800257 log.debug("Deleting a flow rule to sw:{}", deviceId);
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700258 break;
259 default:
260 fail(fwd, ObjectiveError.UNKNOWN);
261 log.warn("Unknown forwarding type {}", fwd.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700262 }
263
Saurav Das822c4e22015-10-23 10:51:11 -0700264 flowRuleService.apply(flowOpsBuilder.build(new FlowRuleOperationsContext() {
265 @Override
266 public void onSuccess(FlowRuleOperations ops) {
267 pass(fwd);
268 }
269
270 @Override
271 public void onError(FlowRuleOperations ops) {
272 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
273 }
274 }));
Saurav Das822c4e22015-10-23 10:51:11 -0700275 }
276
277 @Override
278 public void next(NextObjective nextObjective) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800279 NextGroup nextGroup = flowObjectiveStore.getNextGroup(nextObjective.id());
280 switch (nextObjective.op()) {
281 case ADD:
Saurav Das4f980082015-11-05 13:39:15 -0800282 if (nextGroup != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800283 log.warn("Cannot add next {} that already exists in device {}",
284 nextObjective.id(), deviceId);
285 return;
286 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700287 log.debug("Processing NextObjective id {} in dev {} - add group",
Saurav Das8a0732e2015-11-20 15:27:53 -0800288 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700289 groupHandler.addGroup(nextObjective);
Saurav Das8a0732e2015-11-20 15:27:53 -0800290 break;
291 case ADD_TO_EXISTING:
292 if (nextGroup != null) {
Saurav Das1547b3f2017-05-05 17:01:08 -0700293 log.debug("Processing NextObjective id {} in dev {} - add bucket",
Saurav Das8a0732e2015-11-20 15:27:53 -0800294 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700295 groupHandler.addBucketToGroup(nextObjective, nextGroup);
Saurav Das4f980082015-11-05 13:39:15 -0800296 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800297 // it is possible that group-chain has not been fully created yet
Saurav Das423fe2b2015-12-04 10:52:59 -0800298 log.debug("Waiting to add bucket to group for next-id:{} in dev:{}",
299 nextObjective.id(), deviceId);
Yi Tseng47f82dc2017-03-05 22:48:39 -0800300
301 // by design multiple pending bucket is allowed for the group
302 groupHandler.pendingBuckets.compute(nextObjective.id(), (nextId, pendBkts) -> {
303 if (pendBkts == null) {
304 pendBkts = Sets.newHashSet();
305 }
306 pendBkts.add(nextObjective);
307 return pendBkts;
308 });
Saurav Das4f980082015-11-05 13:39:15 -0800309 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800310 break;
311 case REMOVE:
312 if (nextGroup == null) {
313 log.warn("Cannot remove next {} that does not exist in device {}",
314 nextObjective.id(), deviceId);
315 return;
316 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700317 log.debug("Processing NextObjective id {} in dev {} - remove group",
Saurav Das8a0732e2015-11-20 15:27:53 -0800318 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700319 groupHandler.removeGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800320 break;
321 case REMOVE_FROM_EXISTING:
322 if (nextGroup == null) {
323 log.warn("Cannot remove from next {} that does not exist in device {}",
324 nextObjective.id(), deviceId);
325 return;
326 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700327 log.debug("Processing NextObjective id {} in dev {} - remove bucket",
Saurav Das8a0732e2015-11-20 15:27:53 -0800328 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700329 groupHandler.removeBucketFromGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800330 break;
Saurav Dasceccf242017-08-03 18:30:35 -0700331 case VERIFY:
332 if (nextGroup == null) {
333 log.warn("Cannot verify next {} that does not exist in device {}",
334 nextObjective.id(), deviceId);
335 return;
336 }
337 log.debug("Processing NextObjective id {} in dev {} - verify",
338 nextObjective.id(), deviceId);
339 groupHandler.verifyGroup(nextObjective, nextGroup);
340 break;
Saurav Das8a0732e2015-11-20 15:27:53 -0800341 default:
Saurav Das4f980082015-11-05 13:39:15 -0800342 log.warn("Unsupported operation {}", nextObjective.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700343 }
344 }
345
346 //////////////////////////////////////
347 // Flow handling
348 //////////////////////////////////////
349
350 /**
Yi Tseng0fdc07e2017-04-24 19:17:08 -0700351 * As per OFDPA 2.0 TTP, filtering of VLAN ids and MAC addresses (for routing)
352 * configured on switch ports happen in different tables.
Saurav Das822c4e22015-10-23 10:51:11 -0700353 *
354 * @param filt the filtering objective
355 * @param install indicates whether to add or remove the objective
356 * @param applicationId the application that sent this objective
357 */
Saurav Das52025962016-01-28 22:30:01 -0800358 protected void processFilter(FilteringObjective filt,
359 boolean install, ApplicationId applicationId) {
Saurav Das822c4e22015-10-23 10:51:11 -0700360 // This driver only processes filtering criteria defined with switch
361 // ports as the key
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530362 PortCriterion portCriterion = null;
Saurav Das822c4e22015-10-23 10:51:11 -0700363 EthCriterion ethCriterion = null;
364 VlanIdCriterion vidCriterion = null;
Saurav Das822c4e22015-10-23 10:51:11 -0700365 if (!filt.key().equals(Criteria.dummy()) &&
366 filt.key().type() == Criterion.Type.IN_PORT) {
367 portCriterion = (PortCriterion) filt.key();
Saurav Das822c4e22015-10-23 10:51:11 -0700368 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530369 if (portCriterion == null) {
370 log.debug("No IN_PORT defined in filtering objective from app: {}",
371 applicationId);
372 } else {
373 log.debug("Received filtering objective for dev/port: {}/{}", deviceId,
374 portCriterion.port());
375 }
Saurav Das822c4e22015-10-23 10:51:11 -0700376 // convert filtering conditions for switch-intfs into flowrules
377 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
378 for (Criterion criterion : filt.conditions()) {
Yi Tseng0fdc07e2017-04-24 19:17:08 -0700379 switch (criterion.type()) {
380 case ETH_DST:
381 case ETH_DST_MASKED:
382 ethCriterion = (EthCriterion) criterion;
383 break;
384 case VLAN_VID:
385 vidCriterion = (VlanIdCriterion) criterion;
386 break;
387 default:
388 log.warn("Unsupported filter {}", criterion);
389 fail(filt, ObjectiveError.UNSUPPORTED);
390 return;
Saurav Das822c4e22015-10-23 10:51:11 -0700391 }
392 }
393
Saurav Das0e99e2b2015-10-28 12:39:42 -0700394 VlanId assignedVlan = null;
Charles Chane849c192016-01-11 18:28:54 -0800395 if (vidCriterion != null) {
Charles Chand55e84d2016-03-30 17:54:24 -0700396 // Use the VLAN in metadata whenever a metadata is provided
397 if (filt.meta() != null) {
398 assignedVlan = readVlanFromTreatment(filt.meta());
399 // Use the VLAN in criterion if metadata is not present and the traffic is tagged
400 } else if (!vidCriterion.vlanId().equals(VlanId.NONE)) {
Charles Chane849c192016-01-11 18:28:54 -0800401 assignedVlan = vidCriterion.vlanId();
Charles Chand55e84d2016-03-30 17:54:24 -0700402 }
Charles Chane849c192016-01-11 18:28:54 -0800403
Charles Chand55e84d2016-03-30 17:54:24 -0700404 if (assignedVlan == null) {
405 log.error("Driver fails to extract VLAN information. "
Frank Wangd8ab0962017-08-11 11:09:30 +0800406 + "Not processing VLAN filters on device {}.", deviceId);
Charles Chand55e84d2016-03-30 17:54:24 -0700407 log.debug("VLAN ID in criterion={}, metadata={}",
408 readVlanFromTreatment(filt.meta()), vidCriterion.vlanId());
409 fail(filt, ObjectiveError.BADPARAMS);
410 return;
Saurav Das0e99e2b2015-10-28 12:39:42 -0700411 }
412 }
413
Yi Tseng3a77b4f2017-02-06 15:02:17 -0800414 if (ethCriterion == null || ethCriterion.mac().equals(NONE)) {
Charles Chan985b12e2016-05-11 19:47:22 -0700415 // NOTE: it is possible that a filtering objective only has vidCriterion
Saurav Das961beb22017-03-29 19:09:17 -0700416 log.warn("filtering objective missing dstMac, cannot program TMAC table");
Saurav Das822c4e22015-10-23 10:51:11 -0700417 } else {
418 for (FlowRule tmacRule : processEthDstFilter(portCriterion, ethCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700419 vidCriterion, assignedVlan,
420 applicationId)) {
Saurav Das961beb22017-03-29 19:09:17 -0700421 log.trace("{} MAC filtering rules in TMAC table: {} for dev: {}",
Saurav Das018605f2017-02-18 14:05:44 -0800422 (install) ? "adding" : "removing", tmacRule, deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700423 ops = install ? ops.add(tmacRule) : ops.remove(tmacRule);
424 }
425 }
426
Charles Chan985b12e2016-05-11 19:47:22 -0700427 if (vidCriterion == null) {
428 // NOTE: it is possible that a filtering objective only has ethCriterion
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530429 log.info("filtering objective missing VLAN, cannot program VLAN Table");
Saurav Das822c4e22015-10-23 10:51:11 -0700430 } else {
Charles Chan14967c22015-12-07 11:11:50 -0800431 /*
432 * NOTE: Separate vlan filtering rules and assignment rules
433 * into different stage in order to guarantee that filtering rules
Saurav Das52025962016-01-28 22:30:01 -0800434 * always go first, as required by ofdpa.
Charles Chan14967c22015-12-07 11:11:50 -0800435 */
436 List<FlowRule> allRules = processVlanIdFilter(
437 portCriterion, vidCriterion, assignedVlan, applicationId);
438 List<FlowRule> filteringRules = new ArrayList<>();
439 List<FlowRule> assignmentRules = new ArrayList<>();
440
441 allRules.forEach(flowRule -> {
Charles Chand1172632017-03-15 17:33:09 -0700442 VlanId vlanId;
443 if (requireVlanExtensions()) {
444 ExtensionCriterion extCriterion =
445 (ExtensionCriterion) flowRule.selector().getCriterion(Criterion.Type.EXTENSION);
446 vlanId = ((OfdpaMatchVlanVid) extCriterion.extensionSelector()).vlanId();
447 } else {
448 VlanIdCriterion vlanIdCriterion =
449 (VlanIdCriterion) flowRule.selector().getCriterion(Criterion.Type.VLAN_VID);
450 vlanId = vlanIdCriterion.vlanId();
451 }
Charles Chanbe8aea42016-02-24 12:04:47 -0800452 if (!vlanId.equals(VlanId.NONE)) {
Charles Chan14967c22015-12-07 11:11:50 -0800453 filteringRules.add(flowRule);
454 } else {
455 assignmentRules.add(flowRule);
456 }
457 });
458
459 for (FlowRule filteringRule : filteringRules) {
Saurav Das961beb22017-03-29 19:09:17 -0700460 log.trace("{} VLAN filtering rule in VLAN table: {} for dev: {}",
Saurav Das018605f2017-02-18 14:05:44 -0800461 (install) ? "adding" : "removing", filteringRule, deviceId);
Charles Chan14967c22015-12-07 11:11:50 -0800462 ops = install ? ops.add(filteringRule) : ops.remove(filteringRule);
463 }
464
465 ops.newStage();
466
467 for (FlowRule assignmentRule : assignmentRules) {
Saurav Das961beb22017-03-29 19:09:17 -0700468 log.trace("{} VLAN assignment rule in VLAN table: {} for dev: {}",
Saurav Das018605f2017-02-18 14:05:44 -0800469 (install) ? "adding" : "removing", assignmentRule, deviceId);
Charles Chan14967c22015-12-07 11:11:50 -0800470 ops = install ? ops.add(assignmentRule) : ops.remove(assignmentRule);
Saurav Das822c4e22015-10-23 10:51:11 -0700471 }
472 }
473
Saurav Das822c4e22015-10-23 10:51:11 -0700474 // apply filtering flow rules
475 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
476 @Override
477 public void onSuccess(FlowRuleOperations ops) {
Saurav Das018605f2017-02-18 14:05:44 -0800478 log.debug("Applied {} filtering rules in device {}",
Saurav Das822c4e22015-10-23 10:51:11 -0700479 ops.stages().get(0).size(), deviceId);
480 pass(filt);
481 }
482
483 @Override
484 public void onError(FlowRuleOperations ops) {
485 log.info("Failed to apply all filtering rules in dev {}", deviceId);
486 fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED);
487 }
488 }));
489
490 }
491
492 /**
Charles Chand1172632017-03-15 17:33:09 -0700493 * Internal implementation of processVlanIdFilter.
494 * <p>
495 * The is_present bit in set_vlan_vid action is required to be 0 in OFDPA i12.
496 * Since it is non-OF spec, we need an extension treatment for that.
497 * The useVlanExtension must be set to false for OFDPA i12.
498 * </p>
Saurav Das0e99e2b2015-10-28 12:39:42 -0700499 *
Charles Chanf9e98652016-09-07 16:54:23 -0700500 * @param portCriterion port on device for which this filter is programmed
501 * @param vidCriterion vlan assigned to port, or NONE for untagged
502 * @param assignedVlan assigned vlan-id for untagged packets
503 * @param applicationId for application programming this filter
Saurav Das822c4e22015-10-23 10:51:11 -0700504 * @return list of FlowRule for port-vlan filters
505 */
506 protected List<FlowRule> processVlanIdFilter(PortCriterion portCriterion,
Yi Tsengef19de12017-04-24 11:33:05 -0700507 VlanIdCriterion vidCriterion,
508 VlanId assignedVlan,
509 ApplicationId applicationId) {
Charles Chan14967c22015-12-07 11:11:50 -0800510 List<FlowRule> rules = new ArrayList<>();
Saurav Das822c4e22015-10-23 10:51:11 -0700511 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
512 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800513 TrafficSelector.Builder preSelector = null;
514 TrafficTreatment.Builder preTreatment = null;
515
Saurav Das4f980082015-11-05 13:39:15 -0800516 treatment.transition(TMAC_TABLE);
517
Saurav Das822c4e22015-10-23 10:51:11 -0700518 if (vidCriterion.vlanId() == VlanId.NONE) {
519 // untagged packets are assigned vlans
Charles Chand1172632017-03-15 17:33:09 -0700520 preSelector = DefaultTrafficSelector.builder();
521 if (requireVlanExtensions()) {
522 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(VlanId.NONE);
523 selector.extension(ofdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700524 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
525 treatment.extension(ofdpaSetVlanVid, deviceId);
Charles Chand1172632017-03-15 17:33:09 -0700526
527 OfdpaMatchVlanVid preOfdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
528 preSelector.extension(preOfdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700529 } else {
Charles Chand1172632017-03-15 17:33:09 -0700530 selector.matchVlanId(VlanId.NONE);
Charles Chanf9e98652016-09-07 16:54:23 -0700531 treatment.setVlanId(assignedVlan);
Charles Chand1172632017-03-15 17:33:09 -0700532
533 preSelector.matchVlanId(assignedVlan);
534 }
535 preTreatment = DefaultTrafficTreatment.builder().transition(TMAC_TABLE);
536 } else {
537 if (requireVlanExtensions()) {
538 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
539 selector.extension(ofdpaMatchVlanVid, deviceId);
540 } else {
541 selector.matchVlanId(vidCriterion.vlanId());
Charles Chanf9e98652016-09-07 16:54:23 -0700542 }
Charles Chan14967c22015-12-07 11:11:50 -0800543
Charles Chand55e84d2016-03-30 17:54:24 -0700544 if (!assignedVlan.equals(vidCriterion.vlanId())) {
Charles Chand1172632017-03-15 17:33:09 -0700545 if (requireVlanExtensions()) {
Charles Chanc03782d2017-01-31 13:57:55 -0800546 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
547 treatment.extension(ofdpaSetVlanVid, deviceId);
548 } else {
549 treatment.setVlanId(assignedVlan);
550 }
Charles Chand55e84d2016-03-30 17:54:24 -0700551 }
Saurav Das822c4e22015-10-23 10:51:11 -0700552 }
Saurav Das822c4e22015-10-23 10:51:11 -0700553
554 // ofdpa cannot match on ALL portnumber, so we need to use separate
555 // rules for each port.
Charles Chan14967c22015-12-07 11:11:50 -0800556 List<PortNumber> portnums = new ArrayList<>();
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530557 if (portCriterion != null) {
558 if (PortNumber.ALL.equals(portCriterion.port())) {
559 for (Port port : deviceService.getPorts(deviceId)) {
560 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
561 portnums.add(port.number());
562 }
Saurav Das822c4e22015-10-23 10:51:11 -0700563 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530564 } else {
565 portnums.add(portCriterion.port());
Saurav Das822c4e22015-10-23 10:51:11 -0700566 }
567 } else {
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530568 log.warn("Filtering Objective missing Port Criterion . " +
569 "VLAN Table cannot be programmed for {}",
570 deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700571 }
Saurav Das4f980082015-11-05 13:39:15 -0800572
Saurav Das822c4e22015-10-23 10:51:11 -0700573 for (PortNumber pnum : portnums) {
Saurav Das4f980082015-11-05 13:39:15 -0800574 // create rest of flowrule
Saurav Das822c4e22015-10-23 10:51:11 -0700575 selector.matchInPort(pnum);
576 FlowRule rule = DefaultFlowRule.builder()
577 .forDevice(deviceId)
578 .withSelector(selector.build())
579 .withTreatment(treatment.build())
580 .withPriority(DEFAULT_PRIORITY)
581 .fromApp(applicationId)
582 .makePermanent()
583 .forTable(VLAN_TABLE).build();
Charles Chan14967c22015-12-07 11:11:50 -0800584
585 if (preSelector != null) {
586 preSelector.matchInPort(pnum);
587 FlowRule preRule = DefaultFlowRule.builder()
588 .forDevice(deviceId)
589 .withSelector(preSelector.build())
590 .withTreatment(preTreatment.build())
591 .withPriority(DEFAULT_PRIORITY)
592 .fromApp(applicationId)
593 .makePermanent()
594 .forTable(VLAN_TABLE).build();
595 rules.add(preRule);
596 }
597
Saurav Das822c4e22015-10-23 10:51:11 -0700598 rules.add(rule);
599 }
600 return rules;
601 }
602
603 /**
604 * Allows routed packets with correct destination MAC to be directed
605 * to unicast-IP routing table or MPLS forwarding table.
Saurav Das822c4e22015-10-23 10:51:11 -0700606 *
607 * @param portCriterion port on device for which this filter is programmed
608 * @param ethCriterion dstMac of device for which is filter is programmed
609 * @param vidCriterion vlan assigned to port, or NONE for untagged
Saurav Das0e99e2b2015-10-28 12:39:42 -0700610 * @param assignedVlan assigned vlan-id for untagged packets
Saurav Das822c4e22015-10-23 10:51:11 -0700611 * @param applicationId for application programming this filter
612 * @return list of FlowRule for port-vlan filters
613
614 */
615 protected List<FlowRule> processEthDstFilter(PortCriterion portCriterion,
616 EthCriterion ethCriterion,
617 VlanIdCriterion vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700618 VlanId assignedVlan,
Saurav Das822c4e22015-10-23 10:51:11 -0700619 ApplicationId applicationId) {
Charles Chan5270ed02016-01-30 23:22:37 -0800620 // Consider PortNumber.ANY as wildcard. Match ETH_DST only
Sivachidambaram Subramanianc598d062017-05-16 22:42:06 +0530621 if (portCriterion != null && PortNumber.ANY.equals(portCriterion.port())) {
Charles Chan5270ed02016-01-30 23:22:37 -0800622 return processEthDstOnlyFilter(ethCriterion, applicationId);
623 }
624
Charles Chan5b9df8d2016-03-28 22:21:40 -0700625 // Multicast MAC
626 if (ethCriterion.mask() != null) {
627 return processMcastEthDstFilter(ethCriterion, applicationId);
628 }
629
Saurav Das822c4e22015-10-23 10:51:11 -0700630 //handling untagged packets via assigned VLAN
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530631 if (vidCriterion != null && vidCriterion.vlanId() == VlanId.NONE) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700632 vidCriterion = (VlanIdCriterion) Criteria.matchVlanId(assignedVlan);
Saurav Das822c4e22015-10-23 10:51:11 -0700633 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530634 List<FlowRule> rules = new ArrayList<>();
635 OfdpaMatchVlanVid ofdpaMatchVlanVid = null;
636 if (vidCriterion != null && requireVlanExtensions()) {
637 ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
638 }
Saurav Das822c4e22015-10-23 10:51:11 -0700639 // ofdpa cannot match on ALL portnumber, so we need to use separate
640 // rules for each port.
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -0700641 List<PortNumber> portnums = new ArrayList<>();
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530642 if (portCriterion != null) {
643 if (PortNumber.ALL.equals(portCriterion.port())) {
644 for (Port port : deviceService.getPorts(deviceId)) {
645 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
646 portnums.add(port.number());
647 }
Saurav Das822c4e22015-10-23 10:51:11 -0700648 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530649 } else {
650 portnums.add(portCriterion.port());
651 }
652 for (PortNumber pnum : portnums) {
653 rules.add(buildTmacRuleForIpv4(ethCriterion,
654 vidCriterion,
655 ofdpaMatchVlanVid,
656 applicationId,
657 pnum));
658 rules.add(buildTmacRuleForMpls(ethCriterion,
659 vidCriterion,
660 ofdpaMatchVlanVid,
661 applicationId,
662 pnum));
663 rules.add(buildTmacRuleForIpv6(ethCriterion,
664 vidCriterion,
665 ofdpaMatchVlanVid,
666 applicationId,
667 pnum));
Saurav Das822c4e22015-10-23 10:51:11 -0700668 }
669 } else {
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530670 rules.add(buildTmacRuleForIpv4(ethCriterion,
671 vidCriterion,
672 ofdpaMatchVlanVid,
673 applicationId,
674 null));
675 rules.add(buildTmacRuleForMpls(ethCriterion,
676 vidCriterion,
677 ofdpaMatchVlanVid,
678 applicationId,
679 null));
680 rules.add(buildTmacRuleForIpv6(ethCriterion,
681 vidCriterion,
682 ofdpaMatchVlanVid,
683 applicationId,
684 null));
Saurav Das822c4e22015-10-23 10:51:11 -0700685 }
686 return rules;
687 }
688
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530689 /**
690 * Builds TMAC rules for IPv4 packets.
691 *
692 * @param ethCriterion
693 * @param vidCriterion
694 * @param ofdpaMatchVlanVid
695 * @param applicationId
696 * @param pnum
697 * @return TMAC rule for IPV4 packets
698 */
699 private FlowRule buildTmacRuleForIpv4(EthCriterion ethCriterion,
700 VlanIdCriterion vidCriterion,
701 OfdpaMatchVlanVid ofdpaMatchVlanVid,
702 ApplicationId applicationId,
703 PortNumber pnum) {
704 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
705 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
706 if (pnum != null) {
707 if (matchInPortTmacTable()) {
708 selector.matchInPort(pnum);
709 } else {
710 log.info("Pipeline does not support IN_PORT matching in TMAC table, " +
711 "ignoring the IN_PORT criteria");
712 }
713 }
714 if (vidCriterion != null) {
715 if (requireVlanExtensions()) {
716 selector.extension(ofdpaMatchVlanVid, deviceId);
717 } else {
718 selector.matchVlanId(vidCriterion.vlanId());
719 }
720 }
721 selector.matchEthType(Ethernet.TYPE_IPV4);
722 selector.matchEthDst(ethCriterion.mac());
723 treatment.transition(UNICAST_ROUTING_TABLE);
724 return DefaultFlowRule.builder()
725 .forDevice(deviceId)
726 .withSelector(selector.build())
727 .withTreatment(treatment.build())
728 .withPriority(DEFAULT_PRIORITY)
729 .fromApp(applicationId)
730 .makePermanent()
731 .forTable(TMAC_TABLE).build();
732 }
733
734 /**
735 * Builds TMAC rule for MPLS packets.
736 *
737 * @param ethCriterion
738 * @param vidCriterion
739 * @param ofdpaMatchVlanVid
740 * @param applicationId
741 * @param pnum
742 * @return TMAC rule for MPLS packets
743 */
744 private FlowRule buildTmacRuleForMpls(EthCriterion ethCriterion,
745 VlanIdCriterion vidCriterion,
746 OfdpaMatchVlanVid ofdpaMatchVlanVid,
747 ApplicationId applicationId,
748 PortNumber pnum) {
749 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
750 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
751 if (pnum != null) {
752 if (matchInPortTmacTable()) {
753 selector.matchInPort(pnum);
754 } else {
755 log.info("Pipeline does not support IN_PORT matching in TMAC table, " +
756 "ignoring the IN_PORT criteria");
757 }
758 }
759 if (vidCriterion != null) {
760 if (requireVlanExtensions()) {
761 selector.extension(ofdpaMatchVlanVid, deviceId);
762 } else {
763 selector.matchVlanId(vidCriterion.vlanId());
764 }
765 }
766 selector.matchEthType(Ethernet.MPLS_UNICAST);
767 selector.matchEthDst(ethCriterion.mac());
768 treatment.transition(MPLS_TABLE_0);
769 return DefaultFlowRule.builder()
770 .forDevice(deviceId)
771 .withSelector(selector.build())
772 .withTreatment(treatment.build())
773 .withPriority(DEFAULT_PRIORITY)
774 .fromApp(applicationId)
775 .makePermanent()
776 .forTable(TMAC_TABLE).build();
777 }
778
779 /**
780 * Builds TMAC rules for IPv6 packets.
781 *
782 * @param ethCriterion
783 * @param vidCriterion
784 * @param ofdpaMatchVlanVid
785 * @param applicationId
786 * @param pnum
787 * @return TMAC rule for IPV6 packets
788 */
789 private FlowRule buildTmacRuleForIpv6(EthCriterion ethCriterion,
790 VlanIdCriterion vidCriterion,
791 OfdpaMatchVlanVid ofdpaMatchVlanVid,
792 ApplicationId applicationId,
793 PortNumber pnum) {
794 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
795 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
796 if (pnum != null) {
797 if (matchInPortTmacTable()) {
798 selector.matchInPort(pnum);
799 } else {
800 log.info("Pipeline does not support IN_PORT matching in TMAC table, " +
801 "ignoring the IN_PORT criteria");
802 }
803 }
804 if (vidCriterion != null) {
805 if (requireVlanExtensions()) {
806 selector.extension(ofdpaMatchVlanVid, deviceId);
807 } else {
808 selector.matchVlanId(vidCriterion.vlanId());
809 }
810 }
811 selector.matchEthType(Ethernet.TYPE_IPV6);
812 selector.matchEthDst(ethCriterion.mac());
813 treatment.transition(UNICAST_ROUTING_TABLE);
814 return DefaultFlowRule.builder()
815 .forDevice(deviceId)
816 .withSelector(selector.build())
817 .withTreatment(treatment.build())
818 .withPriority(DEFAULT_PRIORITY)
819 .fromApp(applicationId)
820 .makePermanent()
821 .forTable(TMAC_TABLE).build();
822 }
823
Charles Chan5270ed02016-01-30 23:22:37 -0800824 protected List<FlowRule> processEthDstOnlyFilter(EthCriterion ethCriterion,
Yi Tsengef19de12017-04-24 11:33:05 -0700825 ApplicationId applicationId) {
Pier Luigi0e358632017-01-31 09:35:05 -0800826 ImmutableList.Builder<FlowRule> builder = ImmutableList.builder();
827
Charles Chan5270ed02016-01-30 23:22:37 -0800828 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
829 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
830 selector.matchEthType(Ethernet.TYPE_IPV4);
831 selector.matchEthDst(ethCriterion.mac());
832 treatment.transition(UNICAST_ROUTING_TABLE);
833 FlowRule rule = DefaultFlowRule.builder()
834 .forDevice(deviceId)
835 .withSelector(selector.build())
836 .withTreatment(treatment.build())
837 .withPriority(DEFAULT_PRIORITY)
838 .fromApp(applicationId)
839 .makePermanent()
840 .forTable(TMAC_TABLE).build();
Pier Luigi0e358632017-01-31 09:35:05 -0800841 builder.add(rule);
842
843 selector = DefaultTrafficSelector.builder();
844 treatment = DefaultTrafficTreatment.builder();
845 selector.matchEthType(Ethernet.TYPE_IPV6);
846 selector.matchEthDst(ethCriterion.mac());
847 treatment.transition(UNICAST_ROUTING_TABLE);
848 rule = DefaultFlowRule.builder()
849 .forDevice(deviceId)
850 .withSelector(selector.build())
851 .withTreatment(treatment.build())
852 .withPriority(DEFAULT_PRIORITY)
853 .fromApp(applicationId)
854 .makePermanent()
855 .forTable(TMAC_TABLE).build();
856 return builder.add(rule).build();
Charles Chan5270ed02016-01-30 23:22:37 -0800857 }
858
Charles Chan5b9df8d2016-03-28 22:21:40 -0700859 protected List<FlowRule> processMcastEthDstFilter(EthCriterion ethCriterion,
Yi Tsengef19de12017-04-24 11:33:05 -0700860 ApplicationId applicationId) {
Julia Ferguson65428c32017-08-10 18:15:24 +0000861 ImmutableList.Builder<FlowRule> builder = ImmutableList.builder();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700862 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
863 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
864 selector.matchEthType(Ethernet.TYPE_IPV4);
865 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
866 treatment.transition(MULTICAST_ROUTING_TABLE);
867 FlowRule rule = DefaultFlowRule.builder()
868 .forDevice(deviceId)
869 .withSelector(selector.build())
870 .withTreatment(treatment.build())
871 .withPriority(DEFAULT_PRIORITY)
872 .fromApp(applicationId)
873 .makePermanent()
874 .forTable(TMAC_TABLE).build();
Julia Ferguson65428c32017-08-10 18:15:24 +0000875 builder.add(rule);
876
877 selector = DefaultTrafficSelector.builder();
878 treatment = DefaultTrafficTreatment.builder();
879 selector.matchEthType(Ethernet.TYPE_IPV6);
880 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
881 treatment.transition(MULTICAST_ROUTING_TABLE);
882 rule = DefaultFlowRule.builder()
883 .forDevice(deviceId)
884 .withSelector(selector.build())
885 .withTreatment(treatment.build())
886 .withPriority(DEFAULT_PRIORITY)
887 .fromApp(applicationId)
888 .makePermanent()
889 .forTable(TMAC_TABLE).build();
890 return builder.add(rule).build();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700891 }
892
Saurav Das822c4e22015-10-23 10:51:11 -0700893 private Collection<FlowRule> processForward(ForwardingObjective fwd) {
894 switch (fwd.flag()) {
895 case SPECIFIC:
896 return processSpecific(fwd);
897 case VERSATILE:
898 return processVersatile(fwd);
899 default:
900 fail(fwd, ObjectiveError.UNKNOWN);
901 log.warn("Unknown forwarding flag {}", fwd.flag());
902 }
903 return Collections.emptySet();
904 }
905
906 /**
907 * In the OF-DPA 2.0 pipeline, versatile forwarding objectives go to the
908 * ACL table.
909 * @param fwd the forwarding objective of type 'versatile'
910 * @return a collection of flow rules to be sent to the switch. An empty
911 * collection may be returned if there is a problem in processing
912 * the flow rule
913 */
Saurav Das52025962016-01-28 22:30:01 -0800914 protected Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
Saurav Das018605f2017-02-18 14:05:44 -0800915 log.debug("Processing versatile forwarding objective:{} in dev:{}",
Saurav Dasd2fded02016-12-02 15:43:47 -0800916 fwd.id(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700917
918 EthTypeCriterion ethType =
Saurav Das77b5e902016-01-27 17:01:59 -0800919 (EthTypeCriterion) fwd.selector().getCriterion(Criterion.Type.ETH_TYPE);
Saurav Das822c4e22015-10-23 10:51:11 -0700920 if (ethType == null) {
Saurav Dasd2fded02016-12-02 15:43:47 -0800921 log.error("Versatile forwarding objective:{} must include ethType",
922 fwd.id());
Saurav Das822c4e22015-10-23 10:51:11 -0700923 fail(fwd, ObjectiveError.BADPARAMS);
924 return Collections.emptySet();
925 }
926 if (fwd.nextId() == null && fwd.treatment() == null) {
927 log.error("Forwarding objective {} from {} must contain "
928 + "nextId or Treatment", fwd.selector(), fwd.appId());
Zsolt Haraszti9faab752016-02-17 15:55:20 -0800929 fail(fwd, ObjectiveError.BADPARAMS);
Saurav Das822c4e22015-10-23 10:51:11 -0700930 return Collections.emptySet();
931 }
Saurav Das49cb5a12016-01-16 22:54:07 -0800932
Saurav Das77b5e902016-01-27 17:01:59 -0800933 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
934 fwd.selector().criteria().forEach(criterion -> {
935 if (criterion instanceof VlanIdCriterion) {
936 VlanId vlanId = ((VlanIdCriterion) criterion).vlanId();
937 // ensure that match does not include vlan = NONE as OF-DPA does not
938 // match untagged packets this way in the ACL table.
939 if (vlanId.equals(VlanId.NONE)) {
940 return;
941 }
Charles Chand1172632017-03-15 17:33:09 -0700942 if (requireVlanExtensions()) {
943 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanId);
944 sbuilder.extension(ofdpaMatchVlanVid, deviceId);
945 } else {
946 sbuilder.matchVlanId(vlanId);
947 }
Pier Ventree0ae7a32016-11-23 09:57:42 -0800948 } else if (criterion instanceof Icmpv6TypeCriterion ||
949 criterion instanceof Icmpv6CodeCriterion) {
950 /*
951 * We silenty discard these criterions, our current
952 * OFDPA platform does not support these matches on
953 * the ACL table.
954 */
955 log.warn("ICMPv6 Type and ICMPv6 Code are not supported");
Saurav Das77b5e902016-01-27 17:01:59 -0800956 } else {
957 sbuilder.add(criterion);
958 }
959 });
960
Saurav Das822c4e22015-10-23 10:51:11 -0700961 // XXX driver does not currently do type checking as per Tables 65-67 in
962 // OFDPA 2.0 spec. The only allowed treatment is a punt to the controller.
Saurav Das49cb5a12016-01-16 22:54:07 -0800963 TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder();
964 if (fwd.treatment() != null) {
965 for (Instruction ins : fwd.treatment().allInstructions()) {
966 if (ins instanceof OutputInstruction) {
967 OutputInstruction o = (OutputInstruction) ins;
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530968 if (o != null && PortNumber.CONTROLLER.equals(o.port())) {
Saurav Das49cb5a12016-01-16 22:54:07 -0800969 ttBuilder.add(o);
970 } else {
971 log.warn("Only allowed treatments in versatile forwarding "
972 + "objectives are punts to the controller");
973 }
974 } else {
975 log.warn("Cannot process instruction in versatile fwd {}", ins);
976 }
Saurav Das822c4e22015-10-23 10:51:11 -0700977 }
Charles Chan2df0e8a2017-01-09 11:45:08 -0800978 if (fwd.treatment().clearedDeferred()) {
979 ttBuilder.wipeDeferred();
980 }
Saurav Das822c4e22015-10-23 10:51:11 -0700981 }
Saurav Das822c4e22015-10-23 10:51:11 -0700982 if (fwd.nextId() != null) {
Saurav Das49cb5a12016-01-16 22:54:07 -0800983 // overide case
984 NextGroup next = getGroupForNextObjective(fwd.nextId());
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530985 if (next == null) {
986 fail(fwd, ObjectiveError.BADPARAMS);
987 return Collections.emptySet();
988 }
Saurav Das49cb5a12016-01-16 22:54:07 -0800989 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
990 // we only need the top level group's key to point the flow to it
991 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
992 if (group == null) {
993 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
994 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
995 fail(fwd, ObjectiveError.GROUPMISSING);
996 return Collections.emptySet();
997 }
998 ttBuilder.deferred().group(group.id());
Saurav Das822c4e22015-10-23 10:51:11 -0700999 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001000
1001 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1002 .fromApp(fwd.appId())
1003 .withPriority(fwd.priority())
1004 .forDevice(deviceId)
Saurav Das77b5e902016-01-27 17:01:59 -08001005 .withSelector(sbuilder.build())
Saurav Das49cb5a12016-01-16 22:54:07 -08001006 .withTreatment(ttBuilder.build())
1007 .makePermanent()
1008 .forTable(ACL_TABLE);
1009 return Collections.singletonList(ruleBuilder.build());
Saurav Das822c4e22015-10-23 10:51:11 -07001010 }
1011
1012 /**
1013 * In the OF-DPA 2.0 pipeline, specific forwarding refers to the IP table
Saurav Das8a0732e2015-11-20 15:27:53 -08001014 * (unicast or multicast) or the L2 table (mac + vlan) or the MPLS table.
Saurav Das822c4e22015-10-23 10:51:11 -07001015 *
1016 * @param fwd the forwarding objective of type 'specific'
1017 * @return a collection of flow rules. Typically there will be only one
1018 * for this type of forwarding objective. An empty set may be
1019 * returned if there is an issue in processing the objective.
1020 */
Saurav Das8a0732e2015-11-20 15:27:53 -08001021 protected Collection<FlowRule> processSpecific(ForwardingObjective fwd) {
Saurav Das25190812016-05-27 13:54:07 -07001022 log.debug("Processing specific fwd objective:{} in dev:{} with next:{}",
Saurav Das4ce45962015-11-24 23:21:05 -08001023 fwd.id(), deviceId, fwd.nextId());
1024 boolean isEthTypeObj = isSupportedEthTypeObjective(fwd);
1025 boolean isEthDstObj = isSupportedEthDstObjective(fwd);
1026
1027 if (isEthTypeObj) {
1028 return processEthTypeSpecific(fwd);
1029 } else if (isEthDstObj) {
1030 return processEthDstSpecific(fwd);
1031 } else {
1032 log.warn("processSpecific: Unsupported forwarding objective "
1033 + "criteria fwd:{} in dev:{}", fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001034 fail(fwd, ObjectiveError.UNSUPPORTED);
1035 return Collections.emptySet();
1036 }
Saurav Das4ce45962015-11-24 23:21:05 -08001037 }
1038
Saurav Das4ce45962015-11-24 23:21:05 -08001039 /**
1040 * Handles forwarding rules to the IP and MPLS tables.
1041 *
1042 * @param fwd the forwarding objective
1043 * @return A collection of flow rules, or an empty set
1044 */
1045 protected Collection<FlowRule> processEthTypeSpecific(ForwardingObjective fwd) {
Charles Chancad338a2016-09-16 18:03:11 -07001046 return processEthTypeSpecificInternal(fwd, false, ACL_TABLE);
Charles Chanf9e98652016-09-07 16:54:23 -07001047 }
1048
1049 /**
1050 * Internal implementation of processEthTypeSpecific.
1051 * <p>
1052 * Wildcarded IPv4_DST is not supported in OFDPA i12. Therefore, we break
1053 * the rule into 0.0.0.0/1 and 128.0.0.0/1.
1054 * The allowDefaultRoute must be set to false for OFDPA i12.
1055 * </p>
1056 *
1057 * @param fwd the forwarding objective
1058 * @param allowDefaultRoute allow wildcarded IPv4_DST or not
Ray Milkey0bb1e102016-11-10 14:51:27 -08001059 * @param mplsNextTable next MPLS table
Charles Chanf9e98652016-09-07 16:54:23 -07001060 * @return A collection of flow rules, or an empty set
1061 */
1062 protected Collection<FlowRule> processEthTypeSpecificInternal(ForwardingObjective fwd,
Charles Chancad338a2016-09-16 18:03:11 -07001063 boolean allowDefaultRoute,
1064 int mplsNextTable) {
Saurav Das4ce45962015-11-24 23:21:05 -08001065 TrafficSelector selector = fwd.selector();
1066 EthTypeCriterion ethType =
1067 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
Flavio Castroe10fa242016-01-15 12:43:51 -08001068 boolean popMpls = false;
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001069 boolean emptyGroup = false;
Charles Chan188ebf52015-12-23 00:15:11 -08001070 int forTableId;
Saurav Das8a0732e2015-11-20 15:27:53 -08001071 TrafficSelector.Builder filteredSelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -08001072 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
Flavio Castroe10fa242016-01-15 12:43:51 -08001073 TrafficSelector.Builder complementarySelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -08001074
Saurav Das8a0732e2015-11-20 15:27:53 -08001075 if (ethType.ethType().toShort() == Ethernet.TYPE_IPV4) {
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001076 if (buildIpv4Selector(filteredSelector, complementarySelector, fwd, allowDefaultRoute) < 0) {
1077 return Collections.emptyList();
1078 }
1079 // We need to set properly the next table
Flavio Castroe10fa242016-01-15 12:43:51 -08001080 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
Charles Chan5b9df8d2016-03-28 22:21:40 -07001081 if (ipv4Dst.isMulticast()) {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001082 forTableId = MULTICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -08001083 } else {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001084 forTableId = UNICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -08001085 }
Charles Chan14967c22015-12-07 11:11:50 -08001086 if (fwd.treatment() != null) {
1087 for (Instruction instr : fwd.treatment().allInstructions()) {
1088 if (instr instanceof L3ModificationInstruction &&
1089 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
Saurav Das9c705342017-01-06 11:36:01 -08001090 // XXX decrementing IP ttl is done automatically for routing, this
1091 // action is ignored or rejected in ofdpa as it is not fully implemented
1092 //tb.deferred().add(instr);
Charles Chan14967c22015-12-07 11:11:50 -08001093 }
1094 }
1095 }
1096
Pier Ventree0ae7a32016-11-23 09:57:42 -08001097 } else if (ethType.ethType().toShort() == Ethernet.TYPE_IPV6) {
1098 if (buildIpv6Selector(filteredSelector, fwd) < 0) {
1099 return Collections.emptyList();
1100 }
Kalhee Kim3d26bf02017-10-04 14:59:11 +00001101 //We need to set the proper next table
1102 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
1103 if (ipv6Dst.isMulticast()) {
1104 forTableId = MULTICAST_ROUTING_TABLE;
1105 } else {
1106 forTableId = UNICAST_ROUTING_TABLE;
1107 }
1108
Pier Ventree0ae7a32016-11-23 09:57:42 -08001109 if (fwd.treatment() != null) {
1110 for (Instruction instr : fwd.treatment().allInstructions()) {
1111 if (instr instanceof L3ModificationInstruction &&
1112 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1113 // XXX decrementing IP ttl is done automatically for routing, this
1114 // action is ignored or rejected in ofdpa as it is not fully implemented
1115 //tb.deferred().add(instr);
1116 }
1117 }
1118 }
Saurav Das8a0732e2015-11-20 15:27:53 -08001119 } else {
1120 filteredSelector
1121 .matchEthType(Ethernet.MPLS_UNICAST)
1122 .matchMplsLabel(((MplsCriterion)
1123 selector.getCriterion(Criterion.Type.MPLS_LABEL)).label());
1124 MplsBosCriterion bos = (MplsBosCriterion) selector
Pier Ventre140a8942016-11-02 07:26:38 -07001125 .getCriterion(MPLS_BOS);
Saurav Das8a0732e2015-11-20 15:27:53 -08001126 if (bos != null) {
1127 filteredSelector.matchMplsBos(bos.mplsBos());
1128 }
1129 forTableId = MPLS_TABLE_1;
Saurav Das4ce45962015-11-24 23:21:05 -08001130 log.debug("processing MPLS specific forwarding objective {} -> next:{}"
1131 + " in dev {}", fwd.id(), fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001132
Charles Chan14967c22015-12-07 11:11:50 -08001133 if (fwd.treatment() != null) {
1134 for (Instruction instr : fwd.treatment().allInstructions()) {
1135 if (instr instanceof L2ModificationInstruction &&
1136 ((L2ModificationInstruction) instr).subtype() == L2SubType.MPLS_POP) {
Saurav Das8a0732e2015-11-20 15:27:53 -08001137 popMpls = true;
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001138 // OF-DPA does not pop in MPLS table in some cases. For the L3 VPN, it requires
Saurav Das9c705342017-01-06 11:36:01 -08001139 // setting the MPLS_TYPE so pop can happen down the pipeline
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001140 if (mplsNextTable == MPLS_TYPE_TABLE && isNotMplsBos(selector)) {
1141 tb.immediate().popMpls();
1142 }
Charles Chan14967c22015-12-07 11:11:50 -08001143 }
1144 if (instr instanceof L3ModificationInstruction &&
1145 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1146 // FIXME Should modify the app to send the correct DEC_MPLS_TTL instruction
1147 tb.immediate().decMplsTtl();
1148 }
1149 if (instr instanceof L3ModificationInstruction &&
1150 ((L3ModificationInstruction) instr).subtype() == L3SubType.TTL_IN) {
1151 tb.immediate().add(instr);
1152 }
Saurav Das8a0732e2015-11-20 15:27:53 -08001153 }
1154 }
1155 }
Saurav Das822c4e22015-10-23 10:51:11 -07001156
1157 if (fwd.nextId() != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -08001158 if (forTableId == MPLS_TABLE_1 && !popMpls) {
1159 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
Saurav Das25190812016-05-27 13:54:07 -07001160 + "is not implemented in OF-DPA yet. Aborting this flow {} -> next:{}"
1161 + "in this device {}", fwd.id(), fwd.nextId(), deviceId);
Pier Ventree0ae7a32016-11-23 09:57:42 -08001162 // XXX We could convert to forwarding to a single-port, via a MPLS interface,
1163 // or a MPLS SWAP (with-same) but that would have to be handled in the next-objective.
1164 // Also the pop-mpls logic used here won't work in non-BoS case.
Saurav Das4ce45962015-11-24 23:21:05 -08001165 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
Saurav Das8a0732e2015-11-20 15:27:53 -08001166 return Collections.emptySet();
1167 }
1168
Saurav Das423fe2b2015-12-04 10:52:59 -08001169 NextGroup next = getGroupForNextObjective(fwd.nextId());
1170 if (next != null) {
1171 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1172 // we only need the top level group's key to point the flow to it
1173 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
Pier Ventre140a8942016-11-02 07:26:38 -07001174 if (isNotMplsBos(selector) && group.type().equals(HASHED)) {
1175 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
1176 + "is not implemented in OF-DPA yet. Aborting this flow {} -> next:{}"
1177 + "in this device {}", fwd.id(), fwd.nextId(), deviceId);
1178 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1179 return Collections.emptySet();
1180 }
Saurav Das423fe2b2015-12-04 10:52:59 -08001181 if (group == null) {
1182 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1183 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1184 fail(fwd, ObjectiveError.GROUPMISSING);
1185 return Collections.emptySet();
1186 }
1187 tb.deferred().group(group.id());
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001188 // check if group is empty
1189 if (gkeys.size() == 1 && gkeys.get(0).size() == 1) {
1190 log.warn("Found empty group 0x{} in dev:{} .. will retry fwd:{}",
1191 Integer.toHexString(group.id().id()), deviceId, fwd.id());
1192 emptyGroup = true;
1193 }
Saurav Das25190812016-05-27 13:54:07 -07001194 } else {
1195 log.warn("Cannot find group for nextId:{} in dev:{}. Aborting fwd:{}",
1196 fwd.nextId(), deviceId, fwd.id());
1197 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1198 return Collections.emptySet();
Saurav Das822c4e22015-10-23 10:51:11 -07001199 }
Saurav Das822c4e22015-10-23 10:51:11 -07001200 }
Charles Chancad338a2016-09-16 18:03:11 -07001201
1202 if (forTableId == MPLS_TABLE_1) {
Pier Ventre140a8942016-11-02 07:26:38 -07001203 if (mplsNextTable == MPLS_L3_TYPE_TABLE) {
Charles Chancad338a2016-09-16 18:03:11 -07001204 Ofdpa3SetMplsType setMplsType = new Ofdpa3SetMplsType(Ofdpa3MplsType.L3_PHP);
Saurav Das9c705342017-01-06 11:36:01 -08001205 // set mpls type as apply_action
1206 tb.immediate().extension(setMplsType, deviceId);
Charles Chancad338a2016-09-16 18:03:11 -07001207 }
1208 tb.transition(mplsNextTable);
1209 } else {
1210 tb.transition(ACL_TABLE);
1211 }
1212
Saurav Das822c4e22015-10-23 10:51:11 -07001213 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1214 .fromApp(fwd.appId())
1215 .withPriority(fwd.priority())
1216 .forDevice(deviceId)
Saurav Das8a0732e2015-11-20 15:27:53 -08001217 .withSelector(filteredSelector.build())
1218 .withTreatment(tb.build())
1219 .forTable(forTableId);
Saurav Das822c4e22015-10-23 10:51:11 -07001220
1221 if (fwd.permanent()) {
1222 ruleBuilder.makePermanent();
1223 } else {
1224 ruleBuilder.makeTemporary(fwd.timeout());
1225 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001226 Collection<FlowRule> flowRuleCollection = new ArrayList<>();
1227 flowRuleCollection.add(ruleBuilder.build());
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001228 if (!allowDefaultRoute) {
Pier Ventree0ae7a32016-11-23 09:57:42 -08001229 flowRuleCollection.add(
1230 defaultRoute(fwd, complementarySelector, forTableId, tb)
1231 );
Flavio Castroe10fa242016-01-15 12:43:51 -08001232 log.debug("Default rule 0.0.0.0/0 is being installed two rules");
1233 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001234 // XXX retrying flows may be necessary due to bug CORD-554
1235 if (emptyGroup) {
1236 executorService.schedule(new RetryFlows(fwd, flowRuleCollection),
1237 RETRY_MS, TimeUnit.MILLISECONDS);
1238 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001239 return flowRuleCollection;
Saurav Das822c4e22015-10-23 10:51:11 -07001240 }
1241
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001242 protected int buildIpv4Selector(TrafficSelector.Builder builderToUpdate,
1243 TrafficSelector.Builder extBuilder,
1244 ForwardingObjective fwd,
1245 boolean allowDefaultRoute) {
1246 TrafficSelector selector = fwd.selector();
1247
1248 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
1249 if (ipv4Dst.isMulticast()) {
1250 if (ipv4Dst.prefixLength() != 32) {
1251 log.warn("Multicast specific forwarding objective can only be /32");
1252 fail(fwd, ObjectiveError.BADPARAMS);
1253 return -1;
1254 }
1255 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
1256 if (assignedVlan == null) {
1257 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
1258 fail(fwd, ObjectiveError.BADPARAMS);
1259 return -1;
1260 }
Charles Chand1172632017-03-15 17:33:09 -07001261 if (requireVlanExtensions()) {
1262 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
1263 builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
1264 } else {
1265 builderToUpdate.matchVlanId(assignedVlan);
1266 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001267 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1268 log.debug("processing IPv4 multicast specific forwarding objective {} -> next:{}"
1269 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1270 } else {
1271 if (ipv4Dst.prefixLength() == 0) {
1272 if (allowDefaultRoute) {
1273 // The entire IPV4_DST field is wildcarded intentionally
1274 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4);
1275 } else {
1276 // NOTE: The switch does not support matching 0.0.0.0/0
1277 // Split it into 0.0.0.0/1 and 128.0.0.0/1
1278 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4)
1279 .matchIPDst(IpPrefix.valueOf("0.0.0.0/1"));
1280 extBuilder.matchEthType(Ethernet.TYPE_IPV4)
1281 .matchIPDst(IpPrefix.valueOf("128.0.0.0/1"));
1282 }
1283 } else {
1284 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1285 }
1286 log.debug("processing IPv4 unicast specific forwarding objective {} -> next:{}"
1287 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1288 }
1289 return 0;
1290 }
1291
1292 /**
1293 * Helper method to build Ipv6 selector using the selector provided by
1294 * a forwarding objective.
1295 *
1296 * @param builderToUpdate the builder to update
1297 * @param fwd the selector to read
1298 * @return 0 if the update ends correctly. -1 if the matches
1299 * are not yet supported
1300 */
1301 protected int buildIpv6Selector(TrafficSelector.Builder builderToUpdate,
1302 ForwardingObjective fwd) {
1303
1304 TrafficSelector selector = fwd.selector();
1305
1306 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
1307 if (ipv6Dst.isMulticast()) {
Julia Ferguson65428c32017-08-10 18:15:24 +00001308 if (ipv6Dst.prefixLength() != IpAddress.INET6_BIT_LENGTH) {
1309 log.warn("Multicast specific forwarding objective can only be /128");
1310 fail(fwd, ObjectiveError.BADPARAMS);
1311 return -1;
1312 }
1313 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
1314 if (assignedVlan == null) {
1315 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
1316 fail(fwd, ObjectiveError.BADPARAMS);
1317 return -1;
1318 }
1319 if (requireVlanExtensions()) {
1320 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
1321 builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
1322 } else {
1323 builderToUpdate.matchVlanId(assignedVlan);
1324 }
1325 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6).matchIPv6Dst(ipv6Dst);
1326 log.debug("processing IPv6 multicast specific forwarding objective {} -> next:{}"
1327 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1328 } else {
1329 if (ipv6Dst.prefixLength() != 0) {
1330 builderToUpdate.matchIPv6Dst(ipv6Dst);
1331 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001332 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6);
1333 log.debug("processing IPv6 unicast specific forwarding objective {} -> next:{}"
1334 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Julia Ferguson65428c32017-08-10 18:15:24 +00001335 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001336 return 0;
1337 }
1338
Pier Ventree0ae7a32016-11-23 09:57:42 -08001339 protected FlowRule defaultRoute(ForwardingObjective fwd,
1340 TrafficSelector.Builder complementarySelector,
1341 int forTableId,
1342 TrafficTreatment.Builder tb) {
1343 FlowRule.Builder rule = DefaultFlowRule.builder()
1344 .fromApp(fwd.appId())
1345 .withPriority(fwd.priority())
1346 .forDevice(deviceId)
1347 .withSelector(complementarySelector.build())
1348 .withTreatment(tb.build())
1349 .forTable(forTableId);
1350 if (fwd.permanent()) {
1351 rule.makePermanent();
1352 } else {
1353 rule.makeTemporary(fwd.timeout());
1354 }
1355 return rule.build();
1356 }
1357
Saurav Das4ce45962015-11-24 23:21:05 -08001358 /**
1359 * Handles forwarding rules to the L2 bridging table. Flow actions are not
1360 * allowed in the bridging table - instead we use L2 Interface group or
1361 * L2 flood group
1362 *
1363 * @param fwd the forwarding objective
1364 * @return A collection of flow rules, or an empty set
1365 */
1366 protected Collection<FlowRule> processEthDstSpecific(ForwardingObjective fwd) {
1367 List<FlowRule> rules = new ArrayList<>();
1368
1369 // Build filtered selector
1370 TrafficSelector selector = fwd.selector();
1371 EthCriterion ethCriterion = (EthCriterion) selector
1372 .getCriterion(Criterion.Type.ETH_DST);
1373 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) selector
1374 .getCriterion(Criterion.Type.VLAN_VID);
1375
1376 if (vlanIdCriterion == null) {
1377 log.warn("Forwarding objective for bridging requires vlan. Not "
1378 + "installing fwd:{} in dev:{}", fwd.id(), deviceId);
1379 fail(fwd, ObjectiveError.BADPARAMS);
1380 return Collections.emptySet();
1381 }
1382
1383 TrafficSelector.Builder filteredSelectorBuilder =
1384 DefaultTrafficSelector.builder();
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001385
1386 if (!ethCriterion.mac().equals(NONE) &&
1387 !ethCriterion.mac().equals(BROADCAST)) {
Saurav Das4ce45962015-11-24 23:21:05 -08001388 filteredSelectorBuilder.matchEthDst(ethCriterion.mac());
1389 log.debug("processing L2 forwarding objective:{} -> next:{} in dev:{}",
1390 fwd.id(), fwd.nextId(), deviceId);
1391 } else {
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001392 // Use wildcard DST_MAC if the MacAddress is None or Broadcast
Saurav Das4ce45962015-11-24 23:21:05 -08001393 log.debug("processing L2 Broadcast forwarding objective:{} -> next:{} "
1394 + "in dev:{} for vlan:{}",
1395 fwd.id(), fwd.nextId(), deviceId, vlanIdCriterion.vlanId());
1396 }
Charles Chand1172632017-03-15 17:33:09 -07001397 if (requireVlanExtensions()) {
1398 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanIdCriterion.vlanId());
1399 filteredSelectorBuilder.extension(ofdpaMatchVlanVid, deviceId);
1400 } else {
1401 filteredSelectorBuilder.matchVlanId(vlanIdCriterion.vlanId());
1402 }
Saurav Das4ce45962015-11-24 23:21:05 -08001403 TrafficSelector filteredSelector = filteredSelectorBuilder.build();
1404
1405 if (fwd.treatment() != null) {
1406 log.warn("Ignoring traffic treatment in fwd rule {} meant for L2 table"
1407 + "for dev:{}. Expecting only nextId", fwd.id(), deviceId);
1408 }
1409
1410 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1411 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001412 NextGroup next = getGroupForNextObjective(fwd.nextId());
Saurav Das4ce45962015-11-24 23:21:05 -08001413 if (next != null) {
1414 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1415 // we only need the top level group's key to point the flow to it
1416 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1417 if (group != null) {
1418 treatmentBuilder.deferred().group(group.id());
1419 } else {
1420 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1421 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1422 fail(fwd, ObjectiveError.GROUPMISSING);
1423 return Collections.emptySet();
1424 }
1425 }
1426 }
1427 treatmentBuilder.immediate().transition(ACL_TABLE);
1428 TrafficTreatment filteredTreatment = treatmentBuilder.build();
1429
1430 // Build bridging table entries
1431 FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder();
1432 flowRuleBuilder.fromApp(fwd.appId())
1433 .withPriority(fwd.priority())
1434 .forDevice(deviceId)
1435 .withSelector(filteredSelector)
1436 .withTreatment(filteredTreatment)
1437 .forTable(BRIDGING_TABLE);
1438 if (fwd.permanent()) {
1439 flowRuleBuilder.makePermanent();
1440 } else {
1441 flowRuleBuilder.makeTemporary(fwd.timeout());
1442 }
1443 rules.add(flowRuleBuilder.build());
1444 return rules;
1445 }
1446
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001447 //////////////////////////////////////
1448 // Helper Methods and Classes
1449 //////////////////////////////////////
1450
1451 private boolean isSupportedEthTypeObjective(ForwardingObjective fwd) {
1452 TrafficSelector selector = fwd.selector();
1453 EthTypeCriterion ethType = (EthTypeCriterion) selector
1454 .getCriterion(Criterion.Type.ETH_TYPE);
1455 return !((ethType == null) ||
1456 ((ethType.ethType().toShort() != Ethernet.TYPE_IPV4) &&
Pier Ventree0ae7a32016-11-23 09:57:42 -08001457 (ethType.ethType().toShort() != Ethernet.MPLS_UNICAST)) &&
1458 (ethType.ethType().toShort() != Ethernet.TYPE_IPV6));
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001459 }
1460
1461 private boolean isSupportedEthDstObjective(ForwardingObjective fwd) {
1462 TrafficSelector selector = fwd.selector();
1463 EthCriterion ethDst = (EthCriterion) selector
1464 .getCriterion(Criterion.Type.ETH_DST);
1465 VlanIdCriterion vlanId = (VlanIdCriterion) selector
1466 .getCriterion(Criterion.Type.VLAN_VID);
1467 return !(ethDst == null && vlanId == null);
1468 }
1469
Saurav Das423fe2b2015-12-04 10:52:59 -08001470 protected NextGroup getGroupForNextObjective(Integer nextId) {
1471 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
1472 if (next != null) {
1473 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1474 if (gkeys != null && !gkeys.isEmpty()) {
1475 return next;
1476 } else {
1477 log.warn("Empty next group found in FlowObjective store for "
1478 + "next-id:{} in dev:{}", nextId, deviceId);
1479 }
1480 } else {
1481 log.warn("next-id {} not found in Flow objective store for dev:{}",
1482 nextId, deviceId);
1483 }
1484 return null;
1485 }
1486
Charles Chan188ebf52015-12-23 00:15:11 -08001487 protected static void pass(Objective obj) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001488 obj.context().ifPresent(context -> context.onSuccess(obj));
Saurav Das822c4e22015-10-23 10:51:11 -07001489 }
1490
Charles Chan188ebf52015-12-23 00:15:11 -08001491 protected static void fail(Objective obj, ObjectiveError error) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001492 obj.context().ifPresent(context -> context.onError(obj, error));
Saurav Das822c4e22015-10-23 10:51:11 -07001493 }
Saurav Das24431192016-03-07 19:13:00 -08001494
Saurav Das24431192016-03-07 19:13:00 -08001495 @Override
1496 public List<String> getNextMappings(NextGroup nextGroup) {
1497 List<String> mappings = new ArrayList<>();
1498 List<Deque<GroupKey>> gkeys = appKryo.deserialize(nextGroup.data());
1499 for (Deque<GroupKey> gkd : gkeys) {
1500 Group lastGroup = null;
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -07001501 StringBuilder gchain = new StringBuilder();
Saurav Das24431192016-03-07 19:13:00 -08001502 for (GroupKey gk : gkd) {
1503 Group g = groupService.getGroup(deviceId, gk);
Saurav Das8be4e3a2016-03-11 17:19:07 -08001504 if (g == null) {
Saurav Das25190812016-05-27 13:54:07 -07001505 gchain.append(" NoGrp").append(" -->");
Saurav Das8be4e3a2016-03-11 17:19:07 -08001506 continue;
1507 }
1508 gchain.append(" 0x").append(Integer.toHexString(g.id().id()))
1509 .append(" -->");
Saurav Das24431192016-03-07 19:13:00 -08001510 lastGroup = g;
1511 }
1512 // add port information for last group in group-chain
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -07001513 List<Instruction> lastGroupIns = new ArrayList<>();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001514 if (lastGroup != null && !lastGroup.buckets().buckets().isEmpty()) {
Saurav Das25190812016-05-27 13:54:07 -07001515 lastGroupIns = lastGroup.buckets().buckets().get(0)
1516 .treatment().allInstructions();
1517 }
1518 for (Instruction i: lastGroupIns) {
Saurav Das24431192016-03-07 19:13:00 -08001519 if (i instanceof OutputInstruction) {
Saurav Das8be4e3a2016-03-11 17:19:07 -08001520 gchain.append(" port:").append(((OutputInstruction) i).port());
Saurav Das24431192016-03-07 19:13:00 -08001521 }
1522 }
Saurav Das8be4e3a2016-03-11 17:19:07 -08001523 mappings.add(gchain.toString());
Saurav Das24431192016-03-07 19:13:00 -08001524 }
1525 return mappings;
1526 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001527
Pier Ventre140a8942016-11-02 07:26:38 -07001528 static boolean isMplsBos(TrafficSelector selector) {
1529 MplsBosCriterion bosCriterion = (MplsBosCriterion) selector.getCriterion(MPLS_BOS);
1530 return bosCriterion != null && bosCriterion.mplsBos();
1531 }
1532
1533 static boolean isNotMplsBos(TrafficSelector selector) {
1534 MplsBosCriterion bosCriterion = (MplsBosCriterion) selector.getCriterion(MPLS_BOS);
1535 return bosCriterion != null && !bosCriterion.mplsBos();
1536 }
1537
Charles Chan5b9df8d2016-03-28 22:21:40 -07001538 protected static VlanId readVlanFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001539 if (selector == null) {
1540 return null;
1541 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001542 Criterion criterion = selector.getCriterion(Criterion.Type.VLAN_VID);
1543 return (criterion == null)
1544 ? null : ((VlanIdCriterion) criterion).vlanId();
1545 }
1546
1547 protected static IpPrefix readIpDstFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001548 if (selector == null) {
1549 return null;
1550 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001551 Criterion criterion = selector.getCriterion(Criterion.Type.IPV4_DST);
1552 return (criterion == null) ? null : ((IPCriterion) criterion).ip();
1553 }
Charles Chand55e84d2016-03-30 17:54:24 -07001554
1555 private static VlanId readVlanFromTreatment(TrafficTreatment treatment) {
Saurav Das59232cf2016-04-27 18:35:50 -07001556 if (treatment == null) {
1557 return null;
1558 }
Charles Chand55e84d2016-03-30 17:54:24 -07001559 for (Instruction i : treatment.allInstructions()) {
1560 if (i instanceof ModVlanIdInstruction) {
1561 return ((ModVlanIdInstruction) i).vlanId();
1562 }
1563 }
1564 return null;
1565 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001566
1567 /**
1568 * Utility class that retries sending flows a fixed number of times, even if
1569 * some of the attempts are successful. Used only for forwarding objectives.
1570 */
1571 protected final class RetryFlows implements Runnable {
1572 int attempts = MAX_RETRY_ATTEMPTS;
1573 private Collection<FlowRule> retryFlows;
1574 private ForwardingObjective fwd;
1575
1576 RetryFlows(ForwardingObjective fwd, Collection<FlowRule> retryFlows) {
1577 this.fwd = fwd;
1578 this.retryFlows = retryFlows;
1579 }
1580
1581 @Override
1582 public void run() {
1583 log.info("RETRY FLOWS ATTEMPT# {} for fwd:{} rules:{}",
1584 MAX_RETRY_ATTEMPTS - attempts, fwd.id(), retryFlows.size());
1585 sendForward(fwd, retryFlows);
1586 if (--attempts > 0) {
1587 executorService.schedule(this, RETRY_MS, TimeUnit.MILLISECONDS);
1588 }
1589 }
1590 }
1591
Saurav Das822c4e22015-10-23 10:51:11 -07001592}