blob: c30745602613641ddb9a8ddf83c384c52e84daf2 [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;
Charles Chan14967c22015-12-07 11:11:50 -080022import org.onlab.packet.IpPrefix;
Saurav Das822c4e22015-10-23 10:51:11 -070023import org.onlab.packet.VlanId;
24import org.onlab.util.KryoNamespace;
25import org.onosproject.core.ApplicationId;
26import org.onosproject.core.CoreService;
Charles Chancad338a2016-09-16 18:03:11 -070027import org.onosproject.driver.extensions.Ofdpa3MplsType;
28import org.onosproject.driver.extensions.Ofdpa3SetMplsType;
Charles Chan14967c22015-12-07 11:11:50 -080029import org.onosproject.driver.extensions.OfdpaMatchVlanVid;
30import org.onosproject.driver.extensions.OfdpaSetVlanVid;
Saurav Das822c4e22015-10-23 10:51:11 -070031import org.onosproject.net.DeviceId;
32import org.onosproject.net.Port;
33import org.onosproject.net.PortNumber;
34import org.onosproject.net.behaviour.NextGroup;
35import org.onosproject.net.behaviour.Pipeliner;
36import org.onosproject.net.behaviour.PipelinerContext;
37import org.onosproject.net.device.DeviceService;
38import org.onosproject.net.driver.AbstractHandlerBehaviour;
39import org.onosproject.net.flow.DefaultFlowRule;
40import org.onosproject.net.flow.DefaultTrafficSelector;
41import org.onosproject.net.flow.DefaultTrafficTreatment;
42import org.onosproject.net.flow.FlowRule;
43import org.onosproject.net.flow.FlowRuleOperations;
44import org.onosproject.net.flow.FlowRuleOperationsContext;
45import org.onosproject.net.flow.FlowRuleService;
46import org.onosproject.net.flow.TrafficSelector;
47import org.onosproject.net.flow.TrafficTreatment;
48import org.onosproject.net.flow.criteria.Criteria;
49import org.onosproject.net.flow.criteria.Criterion;
50import org.onosproject.net.flow.criteria.EthCriterion;
51import org.onosproject.net.flow.criteria.EthTypeCriterion;
Charles Chan14967c22015-12-07 11:11:50 -080052import org.onosproject.net.flow.criteria.ExtensionCriterion;
Saurav Das822c4e22015-10-23 10:51:11 -070053import org.onosproject.net.flow.criteria.IPCriterion;
Pier Ventree0ae7a32016-11-23 09:57:42 -080054import org.onosproject.net.flow.criteria.Icmpv6CodeCriterion;
55import org.onosproject.net.flow.criteria.Icmpv6TypeCriterion;
Saurav Das8a0732e2015-11-20 15:27:53 -080056import org.onosproject.net.flow.criteria.MplsBosCriterion;
57import org.onosproject.net.flow.criteria.MplsCriterion;
Saurav Das822c4e22015-10-23 10:51:11 -070058import org.onosproject.net.flow.criteria.PortCriterion;
59import org.onosproject.net.flow.criteria.VlanIdCriterion;
60import org.onosproject.net.flow.instructions.Instruction;
61import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
62import org.onosproject.net.flow.instructions.L2ModificationInstruction;
Saurav Das8a0732e2015-11-20 15:27:53 -080063import org.onosproject.net.flow.instructions.L2ModificationInstruction.L2SubType;
Saurav Das822c4e22015-10-23 10:51:11 -070064import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanIdInstruction;
Charles Chan14967c22015-12-07 11:11:50 -080065import org.onosproject.net.flow.instructions.L3ModificationInstruction;
66import org.onosproject.net.flow.instructions.L3ModificationInstruction.L3SubType;
Saurav Das822c4e22015-10-23 10:51:11 -070067import org.onosproject.net.flowobjective.FilteringObjective;
68import org.onosproject.net.flowobjective.FlowObjectiveStore;
69import org.onosproject.net.flowobjective.ForwardingObjective;
70import org.onosproject.net.flowobjective.NextObjective;
71import org.onosproject.net.flowobjective.Objective;
72import org.onosproject.net.flowobjective.ObjectiveError;
Saurav Das822c4e22015-10-23 10:51:11 -070073import org.onosproject.net.group.DefaultGroupKey;
74import org.onosproject.net.group.Group;
Saurav Das822c4e22015-10-23 10:51:11 -070075import org.onosproject.net.group.GroupKey;
Saurav Das822c4e22015-10-23 10:51:11 -070076import org.onosproject.net.group.GroupService;
Saurav Das822c4e22015-10-23 10:51:11 -070077import org.onosproject.store.serializers.KryoNamespaces;
78import org.slf4j.Logger;
79
Pier Ventree0ae7a32016-11-23 09:57:42 -080080import java.util.ArrayDeque;
81import java.util.ArrayList;
82import java.util.Collection;
83import java.util.Collections;
84import java.util.Deque;
85import java.util.List;
86import java.util.Objects;
Pier Ventree0ae7a32016-11-23 09:57:42 -080087import java.util.concurrent.ScheduledExecutorService;
88import java.util.concurrent.TimeUnit;
89
90import static java.util.concurrent.Executors.newScheduledThreadPool;
Yi Tseng3a77b4f2017-02-06 15:02:17 -080091import static org.onlab.packet.MacAddress.BROADCAST;
92import static org.onlab.packet.MacAddress.NONE;
Pier Ventree0ae7a32016-11-23 09:57:42 -080093import static org.onlab.util.Tools.groupedThreads;
Yi Tsengef19de12017-04-24 11:33:05 -070094import static org.onosproject.driver.pipeline.ofdpa.OfdpaGroupHandlerUtility.*;
Pier Ventree0ae7a32016-11-23 09:57:42 -080095import static org.slf4j.LoggerFactory.getLogger;
Pier Ventre140a8942016-11-02 07:26:38 -070096import static org.onosproject.net.flow.criteria.Criterion.Type.MPLS_BOS;
97import static org.onosproject.net.flowobjective.NextObjective.Type.HASHED;
Pier Ventree0ae7a32016-11-23 09:57:42 -080098
Saurav Das822c4e22015-10-23 10:51:11 -070099/**
100 * Driver for Broadcom's OF-DPA v2.0 TTP.
Saurav Das822c4e22015-10-23 10:51:11 -0700101 */
Charles Chan361154b2016-03-24 10:23:39 -0700102public class Ofdpa2Pipeline extends AbstractHandlerBehaviour implements Pipeliner {
Pier Ventre42287df2016-11-09 14:17:26 -0800103
Saurav Das822c4e22015-10-23 10:51:11 -0700104 protected static final int PORT_TABLE = 0;
105 protected static final int VLAN_TABLE = 10;
Pier Ventre42287df2016-11-09 14:17:26 -0800106 protected static final int VLAN_1_TABLE = 11;
107 protected static final int MPLS_L2_PORT_FLOW_TABLE = 13;
108 protected static final int MPLS_L2_PORT_PCP_TRUST_FLOW_TABLE = 16;
Saurav Das822c4e22015-10-23 10:51:11 -0700109 protected static final int TMAC_TABLE = 20;
110 protected static final int UNICAST_ROUTING_TABLE = 30;
111 protected static final int MULTICAST_ROUTING_TABLE = 40;
112 protected static final int MPLS_TABLE_0 = 23;
113 protected static final int MPLS_TABLE_1 = 24;
Pier Ventre140a8942016-11-02 07:26:38 -0700114 protected static final int MPLS_L3_TYPE_TABLE = 27;
115 protected static final int MPLS_TYPE_TABLE = 29;
Saurav Das822c4e22015-10-23 10:51:11 -0700116 protected static final int BRIDGING_TABLE = 50;
117 protected static final int ACL_TABLE = 60;
118 protected static final int MAC_LEARNING_TABLE = 254;
119 protected static final long OFPP_MAX = 0xffffff00L;
120
Saurav Das52025962016-01-28 22:30:01 -0800121 protected static final int HIGHEST_PRIORITY = 0xffff;
Saurav Das2857f382015-11-03 14:39:27 -0800122 protected static final int DEFAULT_PRIORITY = 0x8000;
Saurav Das822c4e22015-10-23 10:51:11 -0700123 protected static final int LOWEST_PRIORITY = 0x0;
124
Pier Ventre42287df2016-11-09 14:17:26 -0800125 protected static final int MPLS_L2_PORT_PRIORITY = 2;
126
127 protected static final int MPLS_TUNNEL_ID_BASE = 0x10000;
128 protected static final int MPLS_TUNNEL_ID_MAX = 0x1FFFF;
129
Pier Ventre70d53ba2016-11-17 22:26:29 -0800130 protected static final int MPLS_UNI_PORT_MAX = 0x0000FFFF;
131
132 protected static final int MPLS_NNI_PORT_BASE = 0x00020000;
133 protected static final int MPLS_NNI_PORT_MAX = 0x0002FFFF;
134
Saurav Das822c4e22015-10-23 10:51:11 -0700135 private final Logger log = getLogger(getClass());
Charles Chan425854b2016-04-11 15:32:12 -0700136 protected ServiceDirectory serviceDirectory;
Saurav Das822c4e22015-10-23 10:51:11 -0700137 protected FlowRuleService flowRuleService;
Charles Chan425854b2016-04-11 15:32:12 -0700138 protected CoreService coreService;
Saurav Das8a0732e2015-11-20 15:27:53 -0800139 protected GroupService groupService;
140 protected FlowObjectiveStore flowObjectiveStore;
Saurav Das822c4e22015-10-23 10:51:11 -0700141 protected DeviceId deviceId;
142 protected ApplicationId driverId;
Saurav Das822c4e22015-10-23 10:51:11 -0700143 protected DeviceService deviceService;
Charles Chan188ebf52015-12-23 00:15:11 -0800144 protected static KryoNamespace appKryo = new KryoNamespace.Builder()
Yi Tsengef19de12017-04-24 11:33:05 -0700145 .register(KryoNamespaces.API)
146 .register(GroupKey.class)
147 .register(DefaultGroupKey.class)
148 .register(OfdpaNextGroup.class)
149 .register(ArrayDeque.class)
150 .build("Ofdpa2Pipeline");
Saurav Das822c4e22015-10-23 10:51:11 -0700151
Charles Chan425854b2016-04-11 15:32:12 -0700152 protected Ofdpa2GroupHandler groupHandler;
Saurav Das822c4e22015-10-23 10:51:11 -0700153
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700154 // flows installations to be retried
155 protected ScheduledExecutorService executorService
156 = newScheduledThreadPool(5, groupedThreads("OfdpaPipeliner", "retry-%d", log));
157 protected static final int MAX_RETRY_ATTEMPTS = 10;
158 protected static final int RETRY_MS = 1000;
Saurav Das4f980082015-11-05 13:39:15 -0800159
Saurav Das822c4e22015-10-23 10:51:11 -0700160 @Override
161 public void init(DeviceId deviceId, PipelinerContext context) {
Saurav Das822c4e22015-10-23 10:51:11 -0700162 this.deviceId = deviceId;
163
Charles Chan425854b2016-04-11 15:32:12 -0700164 serviceDirectory = context.directory();
Saurav Das822c4e22015-10-23 10:51:11 -0700165 coreService = serviceDirectory.get(CoreService.class);
166 flowRuleService = serviceDirectory.get(FlowRuleService.class);
167 groupService = serviceDirectory.get(GroupService.class);
168 flowObjectiveStore = context.store();
Saurav Das822c4e22015-10-23 10:51:11 -0700169 deviceService = serviceDirectory.get(DeviceService.class);
Saurav Das822c4e22015-10-23 10:51:11 -0700170
Charles Chan40132b32017-01-22 00:19:37 -0800171 initDriverId();
172 initGroupHander(context);
Saurav Das822c4e22015-10-23 10:51:11 -0700173
Saurav Das822c4e22015-10-23 10:51:11 -0700174 initializePipeline();
Saurav Das822c4e22015-10-23 10:51:11 -0700175 }
176
Charles Chan40132b32017-01-22 00:19:37 -0800177 protected void initDriverId() {
178 driverId = coreService.registerApplication(
179 "org.onosproject.driver.Ofdpa2Pipeline");
180 }
181
182 protected void initGroupHander(PipelinerContext context) {
183 groupHandler = new Ofdpa2GroupHandler();
184 groupHandler.init(deviceId, context);
185 }
186
Saurav Das822c4e22015-10-23 10:51:11 -0700187 protected void initializePipeline() {
Charles Chan188ebf52015-12-23 00:15:11 -0800188 // OF-DPA does not require initializing the pipeline as it puts default
189 // rules automatically in the hardware. However emulation of OFDPA in
190 // software switches does require table-miss-entries.
Saurav Das822c4e22015-10-23 10:51:11 -0700191 }
192
Charles Chand1172632017-03-15 17:33:09 -0700193 /**
194 * Determines whether this pipeline requires OFDPA match and set VLAN extensions.
195 *
196 * @return true to use the extensions
197 */
198 protected boolean requireVlanExtensions() {
199 return true;
200 }
201
Saurav Das86d13e82017-04-28 17:03:48 -0700202 /**
203 * Determines whether in-port should be matched on in TMAC table rules.
204 *
205 * @return true if match on in-port should be programmed
206 */
207 protected boolean matchInPortTmacTable() {
208 return true;
209 }
210
Saurav Das822c4e22015-10-23 10:51:11 -0700211 //////////////////////////////////////
212 // Flow Objectives
213 //////////////////////////////////////
214
215 @Override
216 public void filter(FilteringObjective filteringObjective) {
217 if (filteringObjective.type() == FilteringObjective.Type.PERMIT) {
218 processFilter(filteringObjective,
219 filteringObjective.op() == Objective.Operation.ADD,
220 filteringObjective.appId());
221 } else {
222 // Note that packets that don't match the PERMIT filter are
223 // automatically denied. The DENY filter is used to deny packets
224 // that are otherwise permitted by the PERMIT filter.
225 // Use ACL table flow rules here for DENY filtering objectives
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530226 log.warn("filter objective other than PERMIT currently not supported");
Saurav Das822c4e22015-10-23 10:51:11 -0700227 fail(filteringObjective, ObjectiveError.UNSUPPORTED);
228 }
229 }
230
231 @Override
232 public void forward(ForwardingObjective fwd) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700233 Collection<FlowRule> rules = processForward(fwd);
Saurav Das25190812016-05-27 13:54:07 -0700234 if (rules == null || rules.isEmpty()) {
235 // Assumes fail message has already been generated to the objective
236 // context. Returning here prevents spurious pass message to be
237 // generated by FlowRule service for empty flowOps.
238 return;
239 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700240 sendForward(fwd, rules);
241 }
242
243 protected void sendForward(ForwardingObjective fwd, Collection<FlowRule> rules) {
244 FlowRuleOperations.Builder flowOpsBuilder = FlowRuleOperations.builder();
Saurav Das822c4e22015-10-23 10:51:11 -0700245 switch (fwd.op()) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700246 case ADD:
247 rules.stream()
248 .filter(Objects::nonNull)
249 .forEach(flowOpsBuilder::add);
Saurav Dasd2fded02016-12-02 15:43:47 -0800250 log.debug("Applying a add fwd-obj {} to sw:{}", fwd.id(), deviceId);
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700251 break;
252 case REMOVE:
253 rules.stream()
254 .filter(Objects::nonNull)
255 .forEach(flowOpsBuilder::remove);
Pier Ventre42287df2016-11-09 14:17:26 -0800256 log.debug("Deleting a flow rule to sw:{}", deviceId);
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700257 break;
258 default:
259 fail(fwd, ObjectiveError.UNKNOWN);
260 log.warn("Unknown forwarding type {}", fwd.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700261 }
262
Saurav Das822c4e22015-10-23 10:51:11 -0700263 flowRuleService.apply(flowOpsBuilder.build(new FlowRuleOperationsContext() {
264 @Override
265 public void onSuccess(FlowRuleOperations ops) {
266 pass(fwd);
267 }
268
269 @Override
270 public void onError(FlowRuleOperations ops) {
271 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
272 }
273 }));
Saurav Das822c4e22015-10-23 10:51:11 -0700274 }
275
276 @Override
277 public void next(NextObjective nextObjective) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800278 NextGroup nextGroup = flowObjectiveStore.getNextGroup(nextObjective.id());
279 switch (nextObjective.op()) {
280 case ADD:
Saurav Das4f980082015-11-05 13:39:15 -0800281 if (nextGroup != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800282 log.warn("Cannot add next {} that already exists in device {}",
283 nextObjective.id(), deviceId);
284 return;
285 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700286 log.debug("Processing NextObjective id {} in dev {} - add group",
Saurav Das8a0732e2015-11-20 15:27:53 -0800287 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700288 groupHandler.addGroup(nextObjective);
Saurav Das8a0732e2015-11-20 15:27:53 -0800289 break;
290 case ADD_TO_EXISTING:
291 if (nextGroup != null) {
Saurav Das1547b3f2017-05-05 17:01:08 -0700292 log.debug("Processing NextObjective id {} in dev {} - add bucket",
Saurav Das8a0732e2015-11-20 15:27:53 -0800293 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700294 groupHandler.addBucketToGroup(nextObjective, nextGroup);
Saurav Das4f980082015-11-05 13:39:15 -0800295 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800296 // it is possible that group-chain has not been fully created yet
Saurav Das423fe2b2015-12-04 10:52:59 -0800297 log.debug("Waiting to add bucket to group for next-id:{} in dev:{}",
298 nextObjective.id(), deviceId);
Yi Tseng47f82dc2017-03-05 22:48:39 -0800299
300 // by design multiple pending bucket is allowed for the group
301 groupHandler.pendingBuckets.compute(nextObjective.id(), (nextId, pendBkts) -> {
302 if (pendBkts == null) {
303 pendBkts = Sets.newHashSet();
304 }
305 pendBkts.add(nextObjective);
306 return pendBkts;
307 });
Saurav Das4f980082015-11-05 13:39:15 -0800308 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800309 break;
310 case REMOVE:
311 if (nextGroup == null) {
312 log.warn("Cannot remove next {} that does not exist in device {}",
313 nextObjective.id(), deviceId);
314 return;
315 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700316 log.debug("Processing NextObjective id {} in dev {} - remove group",
Saurav Das8a0732e2015-11-20 15:27:53 -0800317 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700318 groupHandler.removeGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800319 break;
320 case REMOVE_FROM_EXISTING:
321 if (nextGroup == null) {
322 log.warn("Cannot remove from next {} that does not exist in device {}",
323 nextObjective.id(), deviceId);
324 return;
325 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700326 log.debug("Processing NextObjective id {} in dev {} - remove bucket",
Saurav Das8a0732e2015-11-20 15:27:53 -0800327 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700328 groupHandler.removeBucketFromGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800329 break;
Saurav Dasceccf242017-08-03 18:30:35 -0700330 case VERIFY:
331 if (nextGroup == null) {
332 log.warn("Cannot verify next {} that does not exist in device {}",
333 nextObjective.id(), deviceId);
334 return;
335 }
336 log.debug("Processing NextObjective id {} in dev {} - verify",
337 nextObjective.id(), deviceId);
338 groupHandler.verifyGroup(nextObjective, nextGroup);
339 break;
Saurav Das8a0732e2015-11-20 15:27:53 -0800340 default:
Saurav Das4f980082015-11-05 13:39:15 -0800341 log.warn("Unsupported operation {}", nextObjective.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700342 }
343 }
344
345 //////////////////////////////////////
346 // Flow handling
347 //////////////////////////////////////
348
349 /**
Yi Tseng0fdc07e2017-04-24 19:17:08 -0700350 * As per OFDPA 2.0 TTP, filtering of VLAN ids and MAC addresses (for routing)
351 * configured on switch ports happen in different tables.
Saurav Das822c4e22015-10-23 10:51:11 -0700352 *
353 * @param filt the filtering objective
354 * @param install indicates whether to add or remove the objective
355 * @param applicationId the application that sent this objective
356 */
Saurav Das52025962016-01-28 22:30:01 -0800357 protected void processFilter(FilteringObjective filt,
358 boolean install, ApplicationId applicationId) {
Saurav Das822c4e22015-10-23 10:51:11 -0700359 // This driver only processes filtering criteria defined with switch
360 // ports as the key
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530361 PortCriterion portCriterion = null;
Saurav Das822c4e22015-10-23 10:51:11 -0700362 EthCriterion ethCriterion = null;
363 VlanIdCriterion vidCriterion = null;
Saurav Das822c4e22015-10-23 10:51:11 -0700364 if (!filt.key().equals(Criteria.dummy()) &&
365 filt.key().type() == Criterion.Type.IN_PORT) {
366 portCriterion = (PortCriterion) filt.key();
Saurav Das822c4e22015-10-23 10:51:11 -0700367 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530368 if (portCriterion == null) {
369 log.debug("No IN_PORT defined in filtering objective from app: {}",
370 applicationId);
371 } else {
372 log.debug("Received filtering objective for dev/port: {}/{}", deviceId,
373 portCriterion.port());
374 }
Saurav Das822c4e22015-10-23 10:51:11 -0700375 // convert filtering conditions for switch-intfs into flowrules
376 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
377 for (Criterion criterion : filt.conditions()) {
Yi Tseng0fdc07e2017-04-24 19:17:08 -0700378 switch (criterion.type()) {
379 case ETH_DST:
380 case ETH_DST_MASKED:
381 ethCriterion = (EthCriterion) criterion;
382 break;
383 case VLAN_VID:
384 vidCriterion = (VlanIdCriterion) criterion;
385 break;
386 default:
387 log.warn("Unsupported filter {}", criterion);
388 fail(filt, ObjectiveError.UNSUPPORTED);
389 return;
Saurav Das822c4e22015-10-23 10:51:11 -0700390 }
391 }
392
Saurav Das0e99e2b2015-10-28 12:39:42 -0700393 VlanId assignedVlan = null;
Charles Chane849c192016-01-11 18:28:54 -0800394 if (vidCriterion != null) {
Charles Chand55e84d2016-03-30 17:54:24 -0700395 // Use the VLAN in metadata whenever a metadata is provided
396 if (filt.meta() != null) {
397 assignedVlan = readVlanFromTreatment(filt.meta());
398 // Use the VLAN in criterion if metadata is not present and the traffic is tagged
399 } else if (!vidCriterion.vlanId().equals(VlanId.NONE)) {
Charles Chane849c192016-01-11 18:28:54 -0800400 assignedVlan = vidCriterion.vlanId();
Charles Chand55e84d2016-03-30 17:54:24 -0700401 }
Charles Chane849c192016-01-11 18:28:54 -0800402
Charles Chand55e84d2016-03-30 17:54:24 -0700403 if (assignedVlan == null) {
404 log.error("Driver fails to extract VLAN information. "
405 + "Not proccessing VLAN filters on device {}.", deviceId);
406 log.debug("VLAN ID in criterion={}, metadata={}",
407 readVlanFromTreatment(filt.meta()), vidCriterion.vlanId());
408 fail(filt, ObjectiveError.BADPARAMS);
409 return;
Saurav Das0e99e2b2015-10-28 12:39:42 -0700410 }
411 }
412
Yi Tseng3a77b4f2017-02-06 15:02:17 -0800413 if (ethCriterion == null || ethCriterion.mac().equals(NONE)) {
Charles Chan985b12e2016-05-11 19:47:22 -0700414 // NOTE: it is possible that a filtering objective only has vidCriterion
Saurav Das961beb22017-03-29 19:09:17 -0700415 log.warn("filtering objective missing dstMac, cannot program TMAC table");
Saurav Das822c4e22015-10-23 10:51:11 -0700416 } else {
417 for (FlowRule tmacRule : processEthDstFilter(portCriterion, ethCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700418 vidCriterion, assignedVlan,
419 applicationId)) {
Saurav Das961beb22017-03-29 19:09:17 -0700420 log.trace("{} MAC filtering rules in TMAC table: {} for dev: {}",
Saurav Das018605f2017-02-18 14:05:44 -0800421 (install) ? "adding" : "removing", tmacRule, deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700422 ops = install ? ops.add(tmacRule) : ops.remove(tmacRule);
423 }
424 }
425
Charles Chan985b12e2016-05-11 19:47:22 -0700426 if (vidCriterion == null) {
427 // NOTE: it is possible that a filtering objective only has ethCriterion
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530428 log.info("filtering objective missing VLAN, cannot program VLAN Table");
Saurav Das822c4e22015-10-23 10:51:11 -0700429 } else {
Charles Chan14967c22015-12-07 11:11:50 -0800430 /*
431 * NOTE: Separate vlan filtering rules and assignment rules
432 * into different stage in order to guarantee that filtering rules
Saurav Das52025962016-01-28 22:30:01 -0800433 * always go first, as required by ofdpa.
Charles Chan14967c22015-12-07 11:11:50 -0800434 */
435 List<FlowRule> allRules = processVlanIdFilter(
436 portCriterion, vidCriterion, assignedVlan, applicationId);
437 List<FlowRule> filteringRules = new ArrayList<>();
438 List<FlowRule> assignmentRules = new ArrayList<>();
439
440 allRules.forEach(flowRule -> {
Charles Chand1172632017-03-15 17:33:09 -0700441 VlanId vlanId;
442 if (requireVlanExtensions()) {
443 ExtensionCriterion extCriterion =
444 (ExtensionCriterion) flowRule.selector().getCriterion(Criterion.Type.EXTENSION);
445 vlanId = ((OfdpaMatchVlanVid) extCriterion.extensionSelector()).vlanId();
446 } else {
447 VlanIdCriterion vlanIdCriterion =
448 (VlanIdCriterion) flowRule.selector().getCriterion(Criterion.Type.VLAN_VID);
449 vlanId = vlanIdCriterion.vlanId();
450 }
Charles Chanbe8aea42016-02-24 12:04:47 -0800451 if (!vlanId.equals(VlanId.NONE)) {
Charles Chan14967c22015-12-07 11:11:50 -0800452 filteringRules.add(flowRule);
453 } else {
454 assignmentRules.add(flowRule);
455 }
456 });
457
458 for (FlowRule filteringRule : filteringRules) {
Saurav Das961beb22017-03-29 19:09:17 -0700459 log.trace("{} VLAN filtering rule in VLAN table: {} for dev: {}",
Saurav Das018605f2017-02-18 14:05:44 -0800460 (install) ? "adding" : "removing", filteringRule, deviceId);
Charles Chan14967c22015-12-07 11:11:50 -0800461 ops = install ? ops.add(filteringRule) : ops.remove(filteringRule);
462 }
463
464 ops.newStage();
465
466 for (FlowRule assignmentRule : assignmentRules) {
Saurav Das961beb22017-03-29 19:09:17 -0700467 log.trace("{} VLAN assignment rule in VLAN table: {} for dev: {}",
Saurav Das018605f2017-02-18 14:05:44 -0800468 (install) ? "adding" : "removing", assignmentRule, deviceId);
Charles Chan14967c22015-12-07 11:11:50 -0800469 ops = install ? ops.add(assignmentRule) : ops.remove(assignmentRule);
Saurav Das822c4e22015-10-23 10:51:11 -0700470 }
471 }
472
Saurav Das822c4e22015-10-23 10:51:11 -0700473 // apply filtering flow rules
474 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
475 @Override
476 public void onSuccess(FlowRuleOperations ops) {
Saurav Das018605f2017-02-18 14:05:44 -0800477 log.debug("Applied {} filtering rules in device {}",
Saurav Das822c4e22015-10-23 10:51:11 -0700478 ops.stages().get(0).size(), deviceId);
479 pass(filt);
480 }
481
482 @Override
483 public void onError(FlowRuleOperations ops) {
484 log.info("Failed to apply all filtering rules in dev {}", deviceId);
485 fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED);
486 }
487 }));
488
489 }
490
491 /**
Charles Chand1172632017-03-15 17:33:09 -0700492 * Internal implementation of processVlanIdFilter.
493 * <p>
494 * The is_present bit in set_vlan_vid action is required to be 0 in OFDPA i12.
495 * Since it is non-OF spec, we need an extension treatment for that.
496 * The useVlanExtension must be set to false for OFDPA i12.
497 * </p>
Saurav Das0e99e2b2015-10-28 12:39:42 -0700498 *
Charles Chanf9e98652016-09-07 16:54:23 -0700499 * @param portCriterion port on device for which this filter is programmed
500 * @param vidCriterion vlan assigned to port, or NONE for untagged
501 * @param assignedVlan assigned vlan-id for untagged packets
502 * @param applicationId for application programming this filter
Saurav Das822c4e22015-10-23 10:51:11 -0700503 * @return list of FlowRule for port-vlan filters
504 */
505 protected List<FlowRule> processVlanIdFilter(PortCriterion portCriterion,
Yi Tsengef19de12017-04-24 11:33:05 -0700506 VlanIdCriterion vidCriterion,
507 VlanId assignedVlan,
508 ApplicationId applicationId) {
Charles Chan14967c22015-12-07 11:11:50 -0800509 List<FlowRule> rules = new ArrayList<>();
Saurav Das822c4e22015-10-23 10:51:11 -0700510 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
511 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800512 TrafficSelector.Builder preSelector = null;
513 TrafficTreatment.Builder preTreatment = null;
514
Saurav Das4f980082015-11-05 13:39:15 -0800515 treatment.transition(TMAC_TABLE);
516
Saurav Das822c4e22015-10-23 10:51:11 -0700517 if (vidCriterion.vlanId() == VlanId.NONE) {
518 // untagged packets are assigned vlans
Charles Chand1172632017-03-15 17:33:09 -0700519 preSelector = DefaultTrafficSelector.builder();
520 if (requireVlanExtensions()) {
521 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(VlanId.NONE);
522 selector.extension(ofdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700523 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
524 treatment.extension(ofdpaSetVlanVid, deviceId);
Charles Chand1172632017-03-15 17:33:09 -0700525
526 OfdpaMatchVlanVid preOfdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
527 preSelector.extension(preOfdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700528 } else {
Charles Chand1172632017-03-15 17:33:09 -0700529 selector.matchVlanId(VlanId.NONE);
Charles Chanf9e98652016-09-07 16:54:23 -0700530 treatment.setVlanId(assignedVlan);
Charles Chand1172632017-03-15 17:33:09 -0700531
532 preSelector.matchVlanId(assignedVlan);
533 }
534 preTreatment = DefaultTrafficTreatment.builder().transition(TMAC_TABLE);
535 } else {
536 if (requireVlanExtensions()) {
537 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
538 selector.extension(ofdpaMatchVlanVid, deviceId);
539 } else {
540 selector.matchVlanId(vidCriterion.vlanId());
Charles Chanf9e98652016-09-07 16:54:23 -0700541 }
Charles Chan14967c22015-12-07 11:11:50 -0800542
Charles Chand55e84d2016-03-30 17:54:24 -0700543 if (!assignedVlan.equals(vidCriterion.vlanId())) {
Charles Chand1172632017-03-15 17:33:09 -0700544 if (requireVlanExtensions()) {
Charles Chanc03782d2017-01-31 13:57:55 -0800545 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
546 treatment.extension(ofdpaSetVlanVid, deviceId);
547 } else {
548 treatment.setVlanId(assignedVlan);
549 }
Charles Chand55e84d2016-03-30 17:54:24 -0700550 }
Saurav Das822c4e22015-10-23 10:51:11 -0700551 }
Saurav Das822c4e22015-10-23 10:51:11 -0700552
553 // ofdpa cannot match on ALL portnumber, so we need to use separate
554 // rules for each port.
Charles Chan14967c22015-12-07 11:11:50 -0800555 List<PortNumber> portnums = new ArrayList<>();
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530556 if (portCriterion != null) {
557 if (PortNumber.ALL.equals(portCriterion.port())) {
558 for (Port port : deviceService.getPorts(deviceId)) {
559 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
560 portnums.add(port.number());
561 }
Saurav Das822c4e22015-10-23 10:51:11 -0700562 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530563 } else {
564 portnums.add(portCriterion.port());
Saurav Das822c4e22015-10-23 10:51:11 -0700565 }
566 } else {
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530567 log.warn("Filtering Objective missing Port Criterion . " +
568 "VLAN Table cannot be programmed for {}",
569 deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700570 }
Saurav Das4f980082015-11-05 13:39:15 -0800571
Saurav Das822c4e22015-10-23 10:51:11 -0700572 for (PortNumber pnum : portnums) {
Saurav Das4f980082015-11-05 13:39:15 -0800573 // create rest of flowrule
Saurav Das822c4e22015-10-23 10:51:11 -0700574 selector.matchInPort(pnum);
575 FlowRule rule = DefaultFlowRule.builder()
576 .forDevice(deviceId)
577 .withSelector(selector.build())
578 .withTreatment(treatment.build())
579 .withPriority(DEFAULT_PRIORITY)
580 .fromApp(applicationId)
581 .makePermanent()
582 .forTable(VLAN_TABLE).build();
Charles Chan14967c22015-12-07 11:11:50 -0800583
584 if (preSelector != null) {
585 preSelector.matchInPort(pnum);
586 FlowRule preRule = DefaultFlowRule.builder()
587 .forDevice(deviceId)
588 .withSelector(preSelector.build())
589 .withTreatment(preTreatment.build())
590 .withPriority(DEFAULT_PRIORITY)
591 .fromApp(applicationId)
592 .makePermanent()
593 .forTable(VLAN_TABLE).build();
594 rules.add(preRule);
595 }
596
Saurav Das822c4e22015-10-23 10:51:11 -0700597 rules.add(rule);
598 }
599 return rules;
600 }
601
602 /**
603 * Allows routed packets with correct destination MAC to be directed
604 * to unicast-IP routing table or MPLS forwarding table.
Saurav Das822c4e22015-10-23 10:51:11 -0700605 *
606 * @param portCriterion port on device for which this filter is programmed
607 * @param ethCriterion dstMac of device for which is filter is programmed
608 * @param vidCriterion vlan assigned to port, or NONE for untagged
Saurav Das0e99e2b2015-10-28 12:39:42 -0700609 * @param assignedVlan assigned vlan-id for untagged packets
Saurav Das822c4e22015-10-23 10:51:11 -0700610 * @param applicationId for application programming this filter
611 * @return list of FlowRule for port-vlan filters
612
613 */
614 protected List<FlowRule> processEthDstFilter(PortCriterion portCriterion,
615 EthCriterion ethCriterion,
616 VlanIdCriterion vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700617 VlanId assignedVlan,
Saurav Das822c4e22015-10-23 10:51:11 -0700618 ApplicationId applicationId) {
Charles Chan5270ed02016-01-30 23:22:37 -0800619 // Consider PortNumber.ANY as wildcard. Match ETH_DST only
Sivachidambaram Subramanianc598d062017-05-16 22:42:06 +0530620 if (portCriterion != null && PortNumber.ANY.equals(portCriterion.port())) {
Charles Chan5270ed02016-01-30 23:22:37 -0800621 return processEthDstOnlyFilter(ethCriterion, applicationId);
622 }
623
Charles Chan5b9df8d2016-03-28 22:21:40 -0700624 // Multicast MAC
625 if (ethCriterion.mask() != null) {
626 return processMcastEthDstFilter(ethCriterion, applicationId);
627 }
628
Saurav Das822c4e22015-10-23 10:51:11 -0700629 //handling untagged packets via assigned VLAN
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530630 if (vidCriterion != null && vidCriterion.vlanId() == VlanId.NONE) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700631 vidCriterion = (VlanIdCriterion) Criteria.matchVlanId(assignedVlan);
Saurav Das822c4e22015-10-23 10:51:11 -0700632 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530633 List<FlowRule> rules = new ArrayList<>();
634 OfdpaMatchVlanVid ofdpaMatchVlanVid = null;
635 if (vidCriterion != null && requireVlanExtensions()) {
636 ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
637 }
Saurav Das822c4e22015-10-23 10:51:11 -0700638 // ofdpa cannot match on ALL portnumber, so we need to use separate
639 // rules for each port.
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -0700640 List<PortNumber> portnums = new ArrayList<>();
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530641 if (portCriterion != null) {
642 if (PortNumber.ALL.equals(portCriterion.port())) {
643 for (Port port : deviceService.getPorts(deviceId)) {
644 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
645 portnums.add(port.number());
646 }
Saurav Das822c4e22015-10-23 10:51:11 -0700647 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530648 } else {
649 portnums.add(portCriterion.port());
650 }
651 for (PortNumber pnum : portnums) {
652 rules.add(buildTmacRuleForIpv4(ethCriterion,
653 vidCriterion,
654 ofdpaMatchVlanVid,
655 applicationId,
656 pnum));
657 rules.add(buildTmacRuleForMpls(ethCriterion,
658 vidCriterion,
659 ofdpaMatchVlanVid,
660 applicationId,
661 pnum));
662 rules.add(buildTmacRuleForIpv6(ethCriterion,
663 vidCriterion,
664 ofdpaMatchVlanVid,
665 applicationId,
666 pnum));
Saurav Das822c4e22015-10-23 10:51:11 -0700667 }
668 } else {
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530669 rules.add(buildTmacRuleForIpv4(ethCriterion,
670 vidCriterion,
671 ofdpaMatchVlanVid,
672 applicationId,
673 null));
674 rules.add(buildTmacRuleForMpls(ethCriterion,
675 vidCriterion,
676 ofdpaMatchVlanVid,
677 applicationId,
678 null));
679 rules.add(buildTmacRuleForIpv6(ethCriterion,
680 vidCriterion,
681 ofdpaMatchVlanVid,
682 applicationId,
683 null));
Saurav Das822c4e22015-10-23 10:51:11 -0700684 }
685 return rules;
686 }
687
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530688 /**
689 * Builds TMAC rules for IPv4 packets.
690 *
691 * @param ethCriterion
692 * @param vidCriterion
693 * @param ofdpaMatchVlanVid
694 * @param applicationId
695 * @param pnum
696 * @return TMAC rule for IPV4 packets
697 */
698 private FlowRule buildTmacRuleForIpv4(EthCriterion ethCriterion,
699 VlanIdCriterion vidCriterion,
700 OfdpaMatchVlanVid ofdpaMatchVlanVid,
701 ApplicationId applicationId,
702 PortNumber pnum) {
703 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
704 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
705 if (pnum != null) {
706 if (matchInPortTmacTable()) {
707 selector.matchInPort(pnum);
708 } else {
709 log.info("Pipeline does not support IN_PORT matching in TMAC table, " +
710 "ignoring the IN_PORT criteria");
711 }
712 }
713 if (vidCriterion != null) {
714 if (requireVlanExtensions()) {
715 selector.extension(ofdpaMatchVlanVid, deviceId);
716 } else {
717 selector.matchVlanId(vidCriterion.vlanId());
718 }
719 }
720 selector.matchEthType(Ethernet.TYPE_IPV4);
721 selector.matchEthDst(ethCriterion.mac());
722 treatment.transition(UNICAST_ROUTING_TABLE);
723 return DefaultFlowRule.builder()
724 .forDevice(deviceId)
725 .withSelector(selector.build())
726 .withTreatment(treatment.build())
727 .withPriority(DEFAULT_PRIORITY)
728 .fromApp(applicationId)
729 .makePermanent()
730 .forTable(TMAC_TABLE).build();
731 }
732
733 /**
734 * Builds TMAC rule for MPLS packets.
735 *
736 * @param ethCriterion
737 * @param vidCriterion
738 * @param ofdpaMatchVlanVid
739 * @param applicationId
740 * @param pnum
741 * @return TMAC rule for MPLS packets
742 */
743 private FlowRule buildTmacRuleForMpls(EthCriterion ethCriterion,
744 VlanIdCriterion vidCriterion,
745 OfdpaMatchVlanVid ofdpaMatchVlanVid,
746 ApplicationId applicationId,
747 PortNumber pnum) {
748 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
749 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
750 if (pnum != null) {
751 if (matchInPortTmacTable()) {
752 selector.matchInPort(pnum);
753 } else {
754 log.info("Pipeline does not support IN_PORT matching in TMAC table, " +
755 "ignoring the IN_PORT criteria");
756 }
757 }
758 if (vidCriterion != null) {
759 if (requireVlanExtensions()) {
760 selector.extension(ofdpaMatchVlanVid, deviceId);
761 } else {
762 selector.matchVlanId(vidCriterion.vlanId());
763 }
764 }
765 selector.matchEthType(Ethernet.MPLS_UNICAST);
766 selector.matchEthDst(ethCriterion.mac());
767 treatment.transition(MPLS_TABLE_0);
768 return DefaultFlowRule.builder()
769 .forDevice(deviceId)
770 .withSelector(selector.build())
771 .withTreatment(treatment.build())
772 .withPriority(DEFAULT_PRIORITY)
773 .fromApp(applicationId)
774 .makePermanent()
775 .forTable(TMAC_TABLE).build();
776 }
777
778 /**
779 * Builds TMAC rules for IPv6 packets.
780 *
781 * @param ethCriterion
782 * @param vidCriterion
783 * @param ofdpaMatchVlanVid
784 * @param applicationId
785 * @param pnum
786 * @return TMAC rule for IPV6 packets
787 */
788 private FlowRule buildTmacRuleForIpv6(EthCriterion ethCriterion,
789 VlanIdCriterion vidCriterion,
790 OfdpaMatchVlanVid ofdpaMatchVlanVid,
791 ApplicationId applicationId,
792 PortNumber pnum) {
793 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
794 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
795 if (pnum != null) {
796 if (matchInPortTmacTable()) {
797 selector.matchInPort(pnum);
798 } else {
799 log.info("Pipeline does not support IN_PORT matching in TMAC table, " +
800 "ignoring the IN_PORT criteria");
801 }
802 }
803 if (vidCriterion != null) {
804 if (requireVlanExtensions()) {
805 selector.extension(ofdpaMatchVlanVid, deviceId);
806 } else {
807 selector.matchVlanId(vidCriterion.vlanId());
808 }
809 }
810 selector.matchEthType(Ethernet.TYPE_IPV6);
811 selector.matchEthDst(ethCriterion.mac());
812 treatment.transition(UNICAST_ROUTING_TABLE);
813 return DefaultFlowRule.builder()
814 .forDevice(deviceId)
815 .withSelector(selector.build())
816 .withTreatment(treatment.build())
817 .withPriority(DEFAULT_PRIORITY)
818 .fromApp(applicationId)
819 .makePermanent()
820 .forTable(TMAC_TABLE).build();
821 }
822
Charles Chan5270ed02016-01-30 23:22:37 -0800823 protected List<FlowRule> processEthDstOnlyFilter(EthCriterion ethCriterion,
Yi Tsengef19de12017-04-24 11:33:05 -0700824 ApplicationId applicationId) {
Pier Luigi0e358632017-01-31 09:35:05 -0800825 ImmutableList.Builder<FlowRule> builder = ImmutableList.builder();
826
Charles Chan5270ed02016-01-30 23:22:37 -0800827 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
828 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
829 selector.matchEthType(Ethernet.TYPE_IPV4);
830 selector.matchEthDst(ethCriterion.mac());
831 treatment.transition(UNICAST_ROUTING_TABLE);
832 FlowRule rule = DefaultFlowRule.builder()
833 .forDevice(deviceId)
834 .withSelector(selector.build())
835 .withTreatment(treatment.build())
836 .withPriority(DEFAULT_PRIORITY)
837 .fromApp(applicationId)
838 .makePermanent()
839 .forTable(TMAC_TABLE).build();
Pier Luigi0e358632017-01-31 09:35:05 -0800840 builder.add(rule);
841
842 selector = DefaultTrafficSelector.builder();
843 treatment = DefaultTrafficTreatment.builder();
844 selector.matchEthType(Ethernet.TYPE_IPV6);
845 selector.matchEthDst(ethCriterion.mac());
846 treatment.transition(UNICAST_ROUTING_TABLE);
847 rule = DefaultFlowRule.builder()
848 .forDevice(deviceId)
849 .withSelector(selector.build())
850 .withTreatment(treatment.build())
851 .withPriority(DEFAULT_PRIORITY)
852 .fromApp(applicationId)
853 .makePermanent()
854 .forTable(TMAC_TABLE).build();
855 return builder.add(rule).build();
Charles Chan5270ed02016-01-30 23:22:37 -0800856 }
857
Charles Chan5b9df8d2016-03-28 22:21:40 -0700858 protected List<FlowRule> processMcastEthDstFilter(EthCriterion ethCriterion,
Yi Tsengef19de12017-04-24 11:33:05 -0700859 ApplicationId applicationId) {
Charles Chan5b9df8d2016-03-28 22:21:40 -0700860 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
861 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
862 selector.matchEthType(Ethernet.TYPE_IPV4);
863 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
864 treatment.transition(MULTICAST_ROUTING_TABLE);
865 FlowRule rule = DefaultFlowRule.builder()
866 .forDevice(deviceId)
867 .withSelector(selector.build())
868 .withTreatment(treatment.build())
869 .withPriority(DEFAULT_PRIORITY)
870 .fromApp(applicationId)
871 .makePermanent()
872 .forTable(TMAC_TABLE).build();
873 return ImmutableList.<FlowRule>builder().add(rule).build();
874 }
875
Saurav Das822c4e22015-10-23 10:51:11 -0700876 private Collection<FlowRule> processForward(ForwardingObjective fwd) {
877 switch (fwd.flag()) {
878 case SPECIFIC:
879 return processSpecific(fwd);
880 case VERSATILE:
881 return processVersatile(fwd);
882 default:
883 fail(fwd, ObjectiveError.UNKNOWN);
884 log.warn("Unknown forwarding flag {}", fwd.flag());
885 }
886 return Collections.emptySet();
887 }
888
889 /**
890 * In the OF-DPA 2.0 pipeline, versatile forwarding objectives go to the
891 * ACL table.
892 * @param fwd the forwarding objective of type 'versatile'
893 * @return a collection of flow rules to be sent to the switch. An empty
894 * collection may be returned if there is a problem in processing
895 * the flow rule
896 */
Saurav Das52025962016-01-28 22:30:01 -0800897 protected Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
Saurav Das018605f2017-02-18 14:05:44 -0800898 log.debug("Processing versatile forwarding objective:{} in dev:{}",
Saurav Dasd2fded02016-12-02 15:43:47 -0800899 fwd.id(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700900
901 EthTypeCriterion ethType =
Saurav Das77b5e902016-01-27 17:01:59 -0800902 (EthTypeCriterion) fwd.selector().getCriterion(Criterion.Type.ETH_TYPE);
Saurav Das822c4e22015-10-23 10:51:11 -0700903 if (ethType == null) {
Saurav Dasd2fded02016-12-02 15:43:47 -0800904 log.error("Versatile forwarding objective:{} must include ethType",
905 fwd.id());
Saurav Das822c4e22015-10-23 10:51:11 -0700906 fail(fwd, ObjectiveError.BADPARAMS);
907 return Collections.emptySet();
908 }
909 if (fwd.nextId() == null && fwd.treatment() == null) {
910 log.error("Forwarding objective {} from {} must contain "
911 + "nextId or Treatment", fwd.selector(), fwd.appId());
Zsolt Haraszti9faab752016-02-17 15:55:20 -0800912 fail(fwd, ObjectiveError.BADPARAMS);
Saurav Das822c4e22015-10-23 10:51:11 -0700913 return Collections.emptySet();
914 }
Saurav Das49cb5a12016-01-16 22:54:07 -0800915
Saurav Das77b5e902016-01-27 17:01:59 -0800916 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
917 fwd.selector().criteria().forEach(criterion -> {
918 if (criterion instanceof VlanIdCriterion) {
919 VlanId vlanId = ((VlanIdCriterion) criterion).vlanId();
920 // ensure that match does not include vlan = NONE as OF-DPA does not
921 // match untagged packets this way in the ACL table.
922 if (vlanId.equals(VlanId.NONE)) {
923 return;
924 }
Charles Chand1172632017-03-15 17:33:09 -0700925 if (requireVlanExtensions()) {
926 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanId);
927 sbuilder.extension(ofdpaMatchVlanVid, deviceId);
928 } else {
929 sbuilder.matchVlanId(vlanId);
930 }
Pier Ventree0ae7a32016-11-23 09:57:42 -0800931 } else if (criterion instanceof Icmpv6TypeCriterion ||
932 criterion instanceof Icmpv6CodeCriterion) {
933 /*
934 * We silenty discard these criterions, our current
935 * OFDPA platform does not support these matches on
936 * the ACL table.
937 */
938 log.warn("ICMPv6 Type and ICMPv6 Code are not supported");
Saurav Das77b5e902016-01-27 17:01:59 -0800939 } else {
940 sbuilder.add(criterion);
941 }
942 });
943
Saurav Das822c4e22015-10-23 10:51:11 -0700944 // XXX driver does not currently do type checking as per Tables 65-67 in
945 // OFDPA 2.0 spec. The only allowed treatment is a punt to the controller.
Saurav Das49cb5a12016-01-16 22:54:07 -0800946 TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder();
947 if (fwd.treatment() != null) {
948 for (Instruction ins : fwd.treatment().allInstructions()) {
949 if (ins instanceof OutputInstruction) {
950 OutputInstruction o = (OutputInstruction) ins;
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530951 if (o != null && PortNumber.CONTROLLER.equals(o.port())) {
Saurav Das49cb5a12016-01-16 22:54:07 -0800952 ttBuilder.add(o);
953 } else {
954 log.warn("Only allowed treatments in versatile forwarding "
955 + "objectives are punts to the controller");
956 }
957 } else {
958 log.warn("Cannot process instruction in versatile fwd {}", ins);
959 }
Saurav Das822c4e22015-10-23 10:51:11 -0700960 }
Charles Chan2df0e8a2017-01-09 11:45:08 -0800961 if (fwd.treatment().clearedDeferred()) {
962 ttBuilder.wipeDeferred();
963 }
Saurav Das822c4e22015-10-23 10:51:11 -0700964 }
Saurav Das822c4e22015-10-23 10:51:11 -0700965 if (fwd.nextId() != null) {
Saurav Das49cb5a12016-01-16 22:54:07 -0800966 // overide case
967 NextGroup next = getGroupForNextObjective(fwd.nextId());
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530968 if (next == null) {
969 fail(fwd, ObjectiveError.BADPARAMS);
970 return Collections.emptySet();
971 }
Saurav Das49cb5a12016-01-16 22:54:07 -0800972 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
973 // we only need the top level group's key to point the flow to it
974 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
975 if (group == null) {
976 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
977 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
978 fail(fwd, ObjectiveError.GROUPMISSING);
979 return Collections.emptySet();
980 }
981 ttBuilder.deferred().group(group.id());
Saurav Das822c4e22015-10-23 10:51:11 -0700982 }
Saurav Das49cb5a12016-01-16 22:54:07 -0800983
984 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
985 .fromApp(fwd.appId())
986 .withPriority(fwd.priority())
987 .forDevice(deviceId)
Saurav Das77b5e902016-01-27 17:01:59 -0800988 .withSelector(sbuilder.build())
Saurav Das49cb5a12016-01-16 22:54:07 -0800989 .withTreatment(ttBuilder.build())
990 .makePermanent()
991 .forTable(ACL_TABLE);
992 return Collections.singletonList(ruleBuilder.build());
Saurav Das822c4e22015-10-23 10:51:11 -0700993 }
994
995 /**
996 * In the OF-DPA 2.0 pipeline, specific forwarding refers to the IP table
Saurav Das8a0732e2015-11-20 15:27:53 -0800997 * (unicast or multicast) or the L2 table (mac + vlan) or the MPLS table.
Saurav Das822c4e22015-10-23 10:51:11 -0700998 *
999 * @param fwd the forwarding objective of type 'specific'
1000 * @return a collection of flow rules. Typically there will be only one
1001 * for this type of forwarding objective. An empty set may be
1002 * returned if there is an issue in processing the objective.
1003 */
Saurav Das8a0732e2015-11-20 15:27:53 -08001004 protected Collection<FlowRule> processSpecific(ForwardingObjective fwd) {
Saurav Das25190812016-05-27 13:54:07 -07001005 log.debug("Processing specific fwd objective:{} in dev:{} with next:{}",
Saurav Das4ce45962015-11-24 23:21:05 -08001006 fwd.id(), deviceId, fwd.nextId());
1007 boolean isEthTypeObj = isSupportedEthTypeObjective(fwd);
1008 boolean isEthDstObj = isSupportedEthDstObjective(fwd);
1009
1010 if (isEthTypeObj) {
1011 return processEthTypeSpecific(fwd);
1012 } else if (isEthDstObj) {
1013 return processEthDstSpecific(fwd);
1014 } else {
1015 log.warn("processSpecific: Unsupported forwarding objective "
1016 + "criteria fwd:{} in dev:{}", fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001017 fail(fwd, ObjectiveError.UNSUPPORTED);
1018 return Collections.emptySet();
1019 }
Saurav Das4ce45962015-11-24 23:21:05 -08001020 }
1021
Saurav Das4ce45962015-11-24 23:21:05 -08001022 /**
1023 * Handles forwarding rules to the IP and MPLS tables.
1024 *
1025 * @param fwd the forwarding objective
1026 * @return A collection of flow rules, or an empty set
1027 */
1028 protected Collection<FlowRule> processEthTypeSpecific(ForwardingObjective fwd) {
Charles Chancad338a2016-09-16 18:03:11 -07001029 return processEthTypeSpecificInternal(fwd, false, ACL_TABLE);
Charles Chanf9e98652016-09-07 16:54:23 -07001030 }
1031
1032 /**
1033 * Internal implementation of processEthTypeSpecific.
1034 * <p>
1035 * Wildcarded IPv4_DST is not supported in OFDPA i12. Therefore, we break
1036 * the rule into 0.0.0.0/1 and 128.0.0.0/1.
1037 * The allowDefaultRoute must be set to false for OFDPA i12.
1038 * </p>
1039 *
1040 * @param fwd the forwarding objective
1041 * @param allowDefaultRoute allow wildcarded IPv4_DST or not
Ray Milkey0bb1e102016-11-10 14:51:27 -08001042 * @param mplsNextTable next MPLS table
Charles Chanf9e98652016-09-07 16:54:23 -07001043 * @return A collection of flow rules, or an empty set
1044 */
1045 protected Collection<FlowRule> processEthTypeSpecificInternal(ForwardingObjective fwd,
Charles Chancad338a2016-09-16 18:03:11 -07001046 boolean allowDefaultRoute,
1047 int mplsNextTable) {
Saurav Das4ce45962015-11-24 23:21:05 -08001048 TrafficSelector selector = fwd.selector();
1049 EthTypeCriterion ethType =
1050 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
Flavio Castroe10fa242016-01-15 12:43:51 -08001051 boolean popMpls = false;
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001052 boolean emptyGroup = false;
Charles Chan188ebf52015-12-23 00:15:11 -08001053 int forTableId;
Saurav Das8a0732e2015-11-20 15:27:53 -08001054 TrafficSelector.Builder filteredSelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -08001055 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
Flavio Castroe10fa242016-01-15 12:43:51 -08001056 TrafficSelector.Builder complementarySelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -08001057
Saurav Das8a0732e2015-11-20 15:27:53 -08001058 if (ethType.ethType().toShort() == Ethernet.TYPE_IPV4) {
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001059 if (buildIpv4Selector(filteredSelector, complementarySelector, fwd, allowDefaultRoute) < 0) {
1060 return Collections.emptyList();
1061 }
1062 // We need to set properly the next table
Flavio Castroe10fa242016-01-15 12:43:51 -08001063 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
Charles Chan5b9df8d2016-03-28 22:21:40 -07001064 if (ipv4Dst.isMulticast()) {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001065 forTableId = MULTICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -08001066 } else {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001067 forTableId = UNICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -08001068 }
Charles Chan14967c22015-12-07 11:11:50 -08001069 if (fwd.treatment() != null) {
1070 for (Instruction instr : fwd.treatment().allInstructions()) {
1071 if (instr instanceof L3ModificationInstruction &&
1072 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
Saurav Das9c705342017-01-06 11:36:01 -08001073 // XXX decrementing IP ttl is done automatically for routing, this
1074 // action is ignored or rejected in ofdpa as it is not fully implemented
1075 //tb.deferred().add(instr);
Charles Chan14967c22015-12-07 11:11:50 -08001076 }
1077 }
1078 }
1079
Pier Ventree0ae7a32016-11-23 09:57:42 -08001080 } else if (ethType.ethType().toShort() == Ethernet.TYPE_IPV6) {
1081 if (buildIpv6Selector(filteredSelector, fwd) < 0) {
1082 return Collections.emptyList();
1083 }
1084 forTableId = UNICAST_ROUTING_TABLE;
1085 if (fwd.treatment() != null) {
1086 for (Instruction instr : fwd.treatment().allInstructions()) {
1087 if (instr instanceof L3ModificationInstruction &&
1088 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1089 // XXX decrementing IP ttl is done automatically for routing, this
1090 // action is ignored or rejected in ofdpa as it is not fully implemented
1091 //tb.deferred().add(instr);
1092 }
1093 }
1094 }
Saurav Das8a0732e2015-11-20 15:27:53 -08001095 } else {
1096 filteredSelector
1097 .matchEthType(Ethernet.MPLS_UNICAST)
1098 .matchMplsLabel(((MplsCriterion)
1099 selector.getCriterion(Criterion.Type.MPLS_LABEL)).label());
1100 MplsBosCriterion bos = (MplsBosCriterion) selector
Pier Ventre140a8942016-11-02 07:26:38 -07001101 .getCriterion(MPLS_BOS);
Saurav Das8a0732e2015-11-20 15:27:53 -08001102 if (bos != null) {
1103 filteredSelector.matchMplsBos(bos.mplsBos());
1104 }
1105 forTableId = MPLS_TABLE_1;
Saurav Das4ce45962015-11-24 23:21:05 -08001106 log.debug("processing MPLS specific forwarding objective {} -> next:{}"
1107 + " in dev {}", fwd.id(), fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001108
Charles Chan14967c22015-12-07 11:11:50 -08001109 if (fwd.treatment() != null) {
1110 for (Instruction instr : fwd.treatment().allInstructions()) {
1111 if (instr instanceof L2ModificationInstruction &&
1112 ((L2ModificationInstruction) instr).subtype() == L2SubType.MPLS_POP) {
Saurav Das8a0732e2015-11-20 15:27:53 -08001113 popMpls = true;
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001114 // OF-DPA does not pop in MPLS table in some cases. For the L3 VPN, it requires
Saurav Das9c705342017-01-06 11:36:01 -08001115 // setting the MPLS_TYPE so pop can happen down the pipeline
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001116 if (mplsNextTable == MPLS_TYPE_TABLE && isNotMplsBos(selector)) {
1117 tb.immediate().popMpls();
1118 }
Charles Chan14967c22015-12-07 11:11:50 -08001119 }
1120 if (instr instanceof L3ModificationInstruction &&
1121 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1122 // FIXME Should modify the app to send the correct DEC_MPLS_TTL instruction
1123 tb.immediate().decMplsTtl();
1124 }
1125 if (instr instanceof L3ModificationInstruction &&
1126 ((L3ModificationInstruction) instr).subtype() == L3SubType.TTL_IN) {
1127 tb.immediate().add(instr);
1128 }
Saurav Das8a0732e2015-11-20 15:27:53 -08001129 }
1130 }
1131 }
Saurav Das822c4e22015-10-23 10:51:11 -07001132
1133 if (fwd.nextId() != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -08001134 if (forTableId == MPLS_TABLE_1 && !popMpls) {
1135 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
Saurav Das25190812016-05-27 13:54:07 -07001136 + "is not implemented in OF-DPA yet. Aborting this flow {} -> next:{}"
1137 + "in this device {}", fwd.id(), fwd.nextId(), deviceId);
Pier Ventree0ae7a32016-11-23 09:57:42 -08001138 // XXX We could convert to forwarding to a single-port, via a MPLS interface,
1139 // or a MPLS SWAP (with-same) but that would have to be handled in the next-objective.
1140 // Also the pop-mpls logic used here won't work in non-BoS case.
Saurav Das4ce45962015-11-24 23:21:05 -08001141 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
Saurav Das8a0732e2015-11-20 15:27:53 -08001142 return Collections.emptySet();
1143 }
1144
Saurav Das423fe2b2015-12-04 10:52:59 -08001145 NextGroup next = getGroupForNextObjective(fwd.nextId());
1146 if (next != null) {
1147 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1148 // we only need the top level group's key to point the flow to it
1149 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
Pier Ventre140a8942016-11-02 07:26:38 -07001150 if (isNotMplsBos(selector) && group.type().equals(HASHED)) {
1151 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
1152 + "is not implemented in OF-DPA yet. Aborting this flow {} -> next:{}"
1153 + "in this device {}", fwd.id(), fwd.nextId(), deviceId);
1154 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1155 return Collections.emptySet();
1156 }
Saurav Das423fe2b2015-12-04 10:52:59 -08001157 if (group == null) {
1158 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1159 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1160 fail(fwd, ObjectiveError.GROUPMISSING);
1161 return Collections.emptySet();
1162 }
1163 tb.deferred().group(group.id());
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001164 // check if group is empty
1165 if (gkeys.size() == 1 && gkeys.get(0).size() == 1) {
1166 log.warn("Found empty group 0x{} in dev:{} .. will retry fwd:{}",
1167 Integer.toHexString(group.id().id()), deviceId, fwd.id());
1168 emptyGroup = true;
1169 }
Saurav Das25190812016-05-27 13:54:07 -07001170 } else {
1171 log.warn("Cannot find group for nextId:{} in dev:{}. Aborting fwd:{}",
1172 fwd.nextId(), deviceId, fwd.id());
1173 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1174 return Collections.emptySet();
Saurav Das822c4e22015-10-23 10:51:11 -07001175 }
Saurav Das822c4e22015-10-23 10:51:11 -07001176 }
Charles Chancad338a2016-09-16 18:03:11 -07001177
1178 if (forTableId == MPLS_TABLE_1) {
Pier Ventre140a8942016-11-02 07:26:38 -07001179 if (mplsNextTable == MPLS_L3_TYPE_TABLE) {
Charles Chancad338a2016-09-16 18:03:11 -07001180 Ofdpa3SetMplsType setMplsType = new Ofdpa3SetMplsType(Ofdpa3MplsType.L3_PHP);
Saurav Das9c705342017-01-06 11:36:01 -08001181 // set mpls type as apply_action
1182 tb.immediate().extension(setMplsType, deviceId);
Charles Chancad338a2016-09-16 18:03:11 -07001183 }
1184 tb.transition(mplsNextTable);
1185 } else {
1186 tb.transition(ACL_TABLE);
1187 }
1188
Saurav Das822c4e22015-10-23 10:51:11 -07001189 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1190 .fromApp(fwd.appId())
1191 .withPriority(fwd.priority())
1192 .forDevice(deviceId)
Saurav Das8a0732e2015-11-20 15:27:53 -08001193 .withSelector(filteredSelector.build())
1194 .withTreatment(tb.build())
1195 .forTable(forTableId);
Saurav Das822c4e22015-10-23 10:51:11 -07001196
1197 if (fwd.permanent()) {
1198 ruleBuilder.makePermanent();
1199 } else {
1200 ruleBuilder.makeTemporary(fwd.timeout());
1201 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001202 Collection<FlowRule> flowRuleCollection = new ArrayList<>();
1203 flowRuleCollection.add(ruleBuilder.build());
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001204 if (!allowDefaultRoute) {
Pier Ventree0ae7a32016-11-23 09:57:42 -08001205 flowRuleCollection.add(
1206 defaultRoute(fwd, complementarySelector, forTableId, tb)
1207 );
Flavio Castroe10fa242016-01-15 12:43:51 -08001208 log.debug("Default rule 0.0.0.0/0 is being installed two rules");
1209 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001210 // XXX retrying flows may be necessary due to bug CORD-554
1211 if (emptyGroup) {
1212 executorService.schedule(new RetryFlows(fwd, flowRuleCollection),
1213 RETRY_MS, TimeUnit.MILLISECONDS);
1214 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001215 return flowRuleCollection;
Saurav Das822c4e22015-10-23 10:51:11 -07001216 }
1217
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001218 protected int buildIpv4Selector(TrafficSelector.Builder builderToUpdate,
1219 TrafficSelector.Builder extBuilder,
1220 ForwardingObjective fwd,
1221 boolean allowDefaultRoute) {
1222 TrafficSelector selector = fwd.selector();
1223
1224 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
1225 if (ipv4Dst.isMulticast()) {
1226 if (ipv4Dst.prefixLength() != 32) {
1227 log.warn("Multicast specific forwarding objective can only be /32");
1228 fail(fwd, ObjectiveError.BADPARAMS);
1229 return -1;
1230 }
1231 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
1232 if (assignedVlan == null) {
1233 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
1234 fail(fwd, ObjectiveError.BADPARAMS);
1235 return -1;
1236 }
Charles Chand1172632017-03-15 17:33:09 -07001237 if (requireVlanExtensions()) {
1238 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
1239 builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
1240 } else {
1241 builderToUpdate.matchVlanId(assignedVlan);
1242 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001243 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1244 log.debug("processing IPv4 multicast specific forwarding objective {} -> next:{}"
1245 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1246 } else {
1247 if (ipv4Dst.prefixLength() == 0) {
1248 if (allowDefaultRoute) {
1249 // The entire IPV4_DST field is wildcarded intentionally
1250 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4);
1251 } else {
1252 // NOTE: The switch does not support matching 0.0.0.0/0
1253 // Split it into 0.0.0.0/1 and 128.0.0.0/1
1254 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4)
1255 .matchIPDst(IpPrefix.valueOf("0.0.0.0/1"));
1256 extBuilder.matchEthType(Ethernet.TYPE_IPV4)
1257 .matchIPDst(IpPrefix.valueOf("128.0.0.0/1"));
1258 }
1259 } else {
1260 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1261 }
1262 log.debug("processing IPv4 unicast specific forwarding objective {} -> next:{}"
1263 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1264 }
1265 return 0;
1266 }
1267
1268 /**
1269 * Helper method to build Ipv6 selector using the selector provided by
1270 * a forwarding objective.
1271 *
1272 * @param builderToUpdate the builder to update
1273 * @param fwd the selector to read
1274 * @return 0 if the update ends correctly. -1 if the matches
1275 * are not yet supported
1276 */
1277 protected int buildIpv6Selector(TrafficSelector.Builder builderToUpdate,
1278 ForwardingObjective fwd) {
1279
1280 TrafficSelector selector = fwd.selector();
1281
1282 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
1283 if (ipv6Dst.isMulticast()) {
1284 log.warn("IPv6 Multicast is currently not supported");
1285 fail(fwd, ObjectiveError.BADPARAMS);
1286 return -1;
1287 }
1288 if (ipv6Dst.prefixLength() != 0) {
1289 builderToUpdate.matchIPv6Dst(ipv6Dst);
1290 }
1291 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6);
1292 log.debug("processing IPv6 unicast specific forwarding objective {} -> next:{}"
1293 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1294 return 0;
1295 }
1296
Pier Ventree0ae7a32016-11-23 09:57:42 -08001297 protected FlowRule defaultRoute(ForwardingObjective fwd,
1298 TrafficSelector.Builder complementarySelector,
1299 int forTableId,
1300 TrafficTreatment.Builder tb) {
1301 FlowRule.Builder rule = DefaultFlowRule.builder()
1302 .fromApp(fwd.appId())
1303 .withPriority(fwd.priority())
1304 .forDevice(deviceId)
1305 .withSelector(complementarySelector.build())
1306 .withTreatment(tb.build())
1307 .forTable(forTableId);
1308 if (fwd.permanent()) {
1309 rule.makePermanent();
1310 } else {
1311 rule.makeTemporary(fwd.timeout());
1312 }
1313 return rule.build();
1314 }
1315
Saurav Das4ce45962015-11-24 23:21:05 -08001316 /**
1317 * Handles forwarding rules to the L2 bridging table. Flow actions are not
1318 * allowed in the bridging table - instead we use L2 Interface group or
1319 * L2 flood group
1320 *
1321 * @param fwd the forwarding objective
1322 * @return A collection of flow rules, or an empty set
1323 */
1324 protected Collection<FlowRule> processEthDstSpecific(ForwardingObjective fwd) {
1325 List<FlowRule> rules = new ArrayList<>();
1326
1327 // Build filtered selector
1328 TrafficSelector selector = fwd.selector();
1329 EthCriterion ethCriterion = (EthCriterion) selector
1330 .getCriterion(Criterion.Type.ETH_DST);
1331 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) selector
1332 .getCriterion(Criterion.Type.VLAN_VID);
1333
1334 if (vlanIdCriterion == null) {
1335 log.warn("Forwarding objective for bridging requires vlan. Not "
1336 + "installing fwd:{} in dev:{}", fwd.id(), deviceId);
1337 fail(fwd, ObjectiveError.BADPARAMS);
1338 return Collections.emptySet();
1339 }
1340
1341 TrafficSelector.Builder filteredSelectorBuilder =
1342 DefaultTrafficSelector.builder();
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001343
1344 if (!ethCriterion.mac().equals(NONE) &&
1345 !ethCriterion.mac().equals(BROADCAST)) {
Saurav Das4ce45962015-11-24 23:21:05 -08001346 filteredSelectorBuilder.matchEthDst(ethCriterion.mac());
1347 log.debug("processing L2 forwarding objective:{} -> next:{} in dev:{}",
1348 fwd.id(), fwd.nextId(), deviceId);
1349 } else {
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001350 // Use wildcard DST_MAC if the MacAddress is None or Broadcast
Saurav Das4ce45962015-11-24 23:21:05 -08001351 log.debug("processing L2 Broadcast forwarding objective:{} -> next:{} "
1352 + "in dev:{} for vlan:{}",
1353 fwd.id(), fwd.nextId(), deviceId, vlanIdCriterion.vlanId());
1354 }
Charles Chand1172632017-03-15 17:33:09 -07001355 if (requireVlanExtensions()) {
1356 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanIdCriterion.vlanId());
1357 filteredSelectorBuilder.extension(ofdpaMatchVlanVid, deviceId);
1358 } else {
1359 filteredSelectorBuilder.matchVlanId(vlanIdCriterion.vlanId());
1360 }
Saurav Das4ce45962015-11-24 23:21:05 -08001361 TrafficSelector filteredSelector = filteredSelectorBuilder.build();
1362
1363 if (fwd.treatment() != null) {
1364 log.warn("Ignoring traffic treatment in fwd rule {} meant for L2 table"
1365 + "for dev:{}. Expecting only nextId", fwd.id(), deviceId);
1366 }
1367
1368 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1369 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001370 NextGroup next = getGroupForNextObjective(fwd.nextId());
Saurav Das4ce45962015-11-24 23:21:05 -08001371 if (next != null) {
1372 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1373 // we only need the top level group's key to point the flow to it
1374 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1375 if (group != null) {
1376 treatmentBuilder.deferred().group(group.id());
1377 } else {
1378 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1379 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1380 fail(fwd, ObjectiveError.GROUPMISSING);
1381 return Collections.emptySet();
1382 }
1383 }
1384 }
1385 treatmentBuilder.immediate().transition(ACL_TABLE);
1386 TrafficTreatment filteredTreatment = treatmentBuilder.build();
1387
1388 // Build bridging table entries
1389 FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder();
1390 flowRuleBuilder.fromApp(fwd.appId())
1391 .withPriority(fwd.priority())
1392 .forDevice(deviceId)
1393 .withSelector(filteredSelector)
1394 .withTreatment(filteredTreatment)
1395 .forTable(BRIDGING_TABLE);
1396 if (fwd.permanent()) {
1397 flowRuleBuilder.makePermanent();
1398 } else {
1399 flowRuleBuilder.makeTemporary(fwd.timeout());
1400 }
1401 rules.add(flowRuleBuilder.build());
1402 return rules;
1403 }
1404
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001405 //////////////////////////////////////
1406 // Helper Methods and Classes
1407 //////////////////////////////////////
1408
1409 private boolean isSupportedEthTypeObjective(ForwardingObjective fwd) {
1410 TrafficSelector selector = fwd.selector();
1411 EthTypeCriterion ethType = (EthTypeCriterion) selector
1412 .getCriterion(Criterion.Type.ETH_TYPE);
1413 return !((ethType == null) ||
1414 ((ethType.ethType().toShort() != Ethernet.TYPE_IPV4) &&
Pier Ventree0ae7a32016-11-23 09:57:42 -08001415 (ethType.ethType().toShort() != Ethernet.MPLS_UNICAST)) &&
1416 (ethType.ethType().toShort() != Ethernet.TYPE_IPV6));
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001417 }
1418
1419 private boolean isSupportedEthDstObjective(ForwardingObjective fwd) {
1420 TrafficSelector selector = fwd.selector();
1421 EthCriterion ethDst = (EthCriterion) selector
1422 .getCriterion(Criterion.Type.ETH_DST);
1423 VlanIdCriterion vlanId = (VlanIdCriterion) selector
1424 .getCriterion(Criterion.Type.VLAN_VID);
1425 return !(ethDst == null && vlanId == null);
1426 }
1427
Saurav Das423fe2b2015-12-04 10:52:59 -08001428 protected NextGroup getGroupForNextObjective(Integer nextId) {
1429 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
1430 if (next != null) {
1431 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1432 if (gkeys != null && !gkeys.isEmpty()) {
1433 return next;
1434 } else {
1435 log.warn("Empty next group found in FlowObjective store for "
1436 + "next-id:{} in dev:{}", nextId, deviceId);
1437 }
1438 } else {
1439 log.warn("next-id {} not found in Flow objective store for dev:{}",
1440 nextId, deviceId);
1441 }
1442 return null;
1443 }
1444
Charles Chan188ebf52015-12-23 00:15:11 -08001445 protected static void pass(Objective obj) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001446 obj.context().ifPresent(context -> context.onSuccess(obj));
Saurav Das822c4e22015-10-23 10:51:11 -07001447 }
1448
Charles Chan188ebf52015-12-23 00:15:11 -08001449 protected static void fail(Objective obj, ObjectiveError error) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001450 obj.context().ifPresent(context -> context.onError(obj, error));
Saurav Das822c4e22015-10-23 10:51:11 -07001451 }
Saurav Das24431192016-03-07 19:13:00 -08001452
Saurav Das24431192016-03-07 19:13:00 -08001453 @Override
1454 public List<String> getNextMappings(NextGroup nextGroup) {
1455 List<String> mappings = new ArrayList<>();
1456 List<Deque<GroupKey>> gkeys = appKryo.deserialize(nextGroup.data());
1457 for (Deque<GroupKey> gkd : gkeys) {
1458 Group lastGroup = null;
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -07001459 StringBuilder gchain = new StringBuilder();
Saurav Das24431192016-03-07 19:13:00 -08001460 for (GroupKey gk : gkd) {
1461 Group g = groupService.getGroup(deviceId, gk);
Saurav Das8be4e3a2016-03-11 17:19:07 -08001462 if (g == null) {
Saurav Das25190812016-05-27 13:54:07 -07001463 gchain.append(" NoGrp").append(" -->");
Saurav Das8be4e3a2016-03-11 17:19:07 -08001464 continue;
1465 }
1466 gchain.append(" 0x").append(Integer.toHexString(g.id().id()))
1467 .append(" -->");
Saurav Das24431192016-03-07 19:13:00 -08001468 lastGroup = g;
1469 }
1470 // add port information for last group in group-chain
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -07001471 List<Instruction> lastGroupIns = new ArrayList<>();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001472 if (lastGroup != null && !lastGroup.buckets().buckets().isEmpty()) {
Saurav Das25190812016-05-27 13:54:07 -07001473 lastGroupIns = lastGroup.buckets().buckets().get(0)
1474 .treatment().allInstructions();
1475 }
1476 for (Instruction i: lastGroupIns) {
Saurav Das24431192016-03-07 19:13:00 -08001477 if (i instanceof OutputInstruction) {
Saurav Das8be4e3a2016-03-11 17:19:07 -08001478 gchain.append(" port:").append(((OutputInstruction) i).port());
Saurav Das24431192016-03-07 19:13:00 -08001479 }
1480 }
Saurav Das8be4e3a2016-03-11 17:19:07 -08001481 mappings.add(gchain.toString());
Saurav Das24431192016-03-07 19:13:00 -08001482 }
1483 return mappings;
1484 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001485
Pier Ventre140a8942016-11-02 07:26:38 -07001486 static boolean isMplsBos(TrafficSelector selector) {
1487 MplsBosCriterion bosCriterion = (MplsBosCriterion) selector.getCriterion(MPLS_BOS);
1488 return bosCriterion != null && bosCriterion.mplsBos();
1489 }
1490
1491 static boolean isNotMplsBos(TrafficSelector selector) {
1492 MplsBosCriterion bosCriterion = (MplsBosCriterion) selector.getCriterion(MPLS_BOS);
1493 return bosCriterion != null && !bosCriterion.mplsBos();
1494 }
1495
Charles Chan5b9df8d2016-03-28 22:21:40 -07001496 protected static VlanId readVlanFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001497 if (selector == null) {
1498 return null;
1499 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001500 Criterion criterion = selector.getCriterion(Criterion.Type.VLAN_VID);
1501 return (criterion == null)
1502 ? null : ((VlanIdCriterion) criterion).vlanId();
1503 }
1504
1505 protected static IpPrefix readIpDstFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001506 if (selector == null) {
1507 return null;
1508 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001509 Criterion criterion = selector.getCriterion(Criterion.Type.IPV4_DST);
1510 return (criterion == null) ? null : ((IPCriterion) criterion).ip();
1511 }
Charles Chand55e84d2016-03-30 17:54:24 -07001512
1513 private static VlanId readVlanFromTreatment(TrafficTreatment treatment) {
Saurav Das59232cf2016-04-27 18:35:50 -07001514 if (treatment == null) {
1515 return null;
1516 }
Charles Chand55e84d2016-03-30 17:54:24 -07001517 for (Instruction i : treatment.allInstructions()) {
1518 if (i instanceof ModVlanIdInstruction) {
1519 return ((ModVlanIdInstruction) i).vlanId();
1520 }
1521 }
1522 return null;
1523 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001524
1525 /**
1526 * Utility class that retries sending flows a fixed number of times, even if
1527 * some of the attempts are successful. Used only for forwarding objectives.
1528 */
1529 protected final class RetryFlows implements Runnable {
1530 int attempts = MAX_RETRY_ATTEMPTS;
1531 private Collection<FlowRule> retryFlows;
1532 private ForwardingObjective fwd;
1533
1534 RetryFlows(ForwardingObjective fwd, Collection<FlowRule> retryFlows) {
1535 this.fwd = fwd;
1536 this.retryFlows = retryFlows;
1537 }
1538
1539 @Override
1540 public void run() {
1541 log.info("RETRY FLOWS ATTEMPT# {} for fwd:{} rules:{}",
1542 MAX_RETRY_ATTEMPTS - attempts, fwd.id(), retryFlows.size());
1543 sendForward(fwd, retryFlows);
1544 if (--attempts > 0) {
1545 executorService.schedule(this, RETRY_MS, TimeUnit.MILLISECONDS);
1546 }
1547 }
1548 }
1549
Saurav Das822c4e22015-10-23 10:51:11 -07001550}