blob: a76aa4640f6129458bb4f5c015bba396784573f4 [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;
330 default:
Saurav Das4f980082015-11-05 13:39:15 -0800331 log.warn("Unsupported operation {}", nextObjective.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700332 }
333 }
334
335 //////////////////////////////////////
336 // Flow handling
337 //////////////////////////////////////
338
339 /**
Yi Tseng0fdc07e2017-04-24 19:17:08 -0700340 * As per OFDPA 2.0 TTP, filtering of VLAN ids and MAC addresses (for routing)
341 * configured on switch ports happen in different tables.
Saurav Das822c4e22015-10-23 10:51:11 -0700342 *
343 * @param filt the filtering objective
344 * @param install indicates whether to add or remove the objective
345 * @param applicationId the application that sent this objective
346 */
Saurav Das52025962016-01-28 22:30:01 -0800347 protected void processFilter(FilteringObjective filt,
348 boolean install, ApplicationId applicationId) {
Saurav Das822c4e22015-10-23 10:51:11 -0700349 // This driver only processes filtering criteria defined with switch
350 // ports as the key
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530351 PortCriterion portCriterion = null;
Saurav Das822c4e22015-10-23 10:51:11 -0700352 EthCriterion ethCriterion = null;
353 VlanIdCriterion vidCriterion = null;
Saurav Das822c4e22015-10-23 10:51:11 -0700354 if (!filt.key().equals(Criteria.dummy()) &&
355 filt.key().type() == Criterion.Type.IN_PORT) {
356 portCriterion = (PortCriterion) filt.key();
Saurav Das822c4e22015-10-23 10:51:11 -0700357 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530358 if (portCriterion == null) {
359 log.debug("No IN_PORT defined in filtering objective from app: {}",
360 applicationId);
361 } else {
362 log.debug("Received filtering objective for dev/port: {}/{}", deviceId,
363 portCriterion.port());
364 }
Saurav Das822c4e22015-10-23 10:51:11 -0700365 // convert filtering conditions for switch-intfs into flowrules
366 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
367 for (Criterion criterion : filt.conditions()) {
Yi Tseng0fdc07e2017-04-24 19:17:08 -0700368 switch (criterion.type()) {
369 case ETH_DST:
370 case ETH_DST_MASKED:
371 ethCriterion = (EthCriterion) criterion;
372 break;
373 case VLAN_VID:
374 vidCriterion = (VlanIdCriterion) criterion;
375 break;
376 default:
377 log.warn("Unsupported filter {}", criterion);
378 fail(filt, ObjectiveError.UNSUPPORTED);
379 return;
Saurav Das822c4e22015-10-23 10:51:11 -0700380 }
381 }
382
Saurav Das0e99e2b2015-10-28 12:39:42 -0700383 VlanId assignedVlan = null;
Charles Chane849c192016-01-11 18:28:54 -0800384 if (vidCriterion != null) {
Charles Chand55e84d2016-03-30 17:54:24 -0700385 // Use the VLAN in metadata whenever a metadata is provided
386 if (filt.meta() != null) {
387 assignedVlan = readVlanFromTreatment(filt.meta());
388 // Use the VLAN in criterion if metadata is not present and the traffic is tagged
389 } else if (!vidCriterion.vlanId().equals(VlanId.NONE)) {
Charles Chane849c192016-01-11 18:28:54 -0800390 assignedVlan = vidCriterion.vlanId();
Charles Chand55e84d2016-03-30 17:54:24 -0700391 }
Charles Chane849c192016-01-11 18:28:54 -0800392
Charles Chand55e84d2016-03-30 17:54:24 -0700393 if (assignedVlan == null) {
394 log.error("Driver fails to extract VLAN information. "
395 + "Not proccessing VLAN filters on device {}.", deviceId);
396 log.debug("VLAN ID in criterion={}, metadata={}",
397 readVlanFromTreatment(filt.meta()), vidCriterion.vlanId());
398 fail(filt, ObjectiveError.BADPARAMS);
399 return;
Saurav Das0e99e2b2015-10-28 12:39:42 -0700400 }
401 }
402
Yi Tseng3a77b4f2017-02-06 15:02:17 -0800403 if (ethCriterion == null || ethCriterion.mac().equals(NONE)) {
Charles Chan985b12e2016-05-11 19:47:22 -0700404 // NOTE: it is possible that a filtering objective only has vidCriterion
Saurav Das961beb22017-03-29 19:09:17 -0700405 log.warn("filtering objective missing dstMac, cannot program TMAC table");
Saurav Das822c4e22015-10-23 10:51:11 -0700406 } else {
407 for (FlowRule tmacRule : processEthDstFilter(portCriterion, ethCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700408 vidCriterion, assignedVlan,
409 applicationId)) {
Saurav Das961beb22017-03-29 19:09:17 -0700410 log.trace("{} MAC filtering rules in TMAC table: {} for dev: {}",
Saurav Das018605f2017-02-18 14:05:44 -0800411 (install) ? "adding" : "removing", tmacRule, deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700412 ops = install ? ops.add(tmacRule) : ops.remove(tmacRule);
413 }
414 }
415
Charles Chan985b12e2016-05-11 19:47:22 -0700416 if (vidCriterion == null) {
417 // NOTE: it is possible that a filtering objective only has ethCriterion
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530418 log.info("filtering objective missing VLAN, cannot program VLAN Table");
Saurav Das822c4e22015-10-23 10:51:11 -0700419 } else {
Charles Chan14967c22015-12-07 11:11:50 -0800420 /*
421 * NOTE: Separate vlan filtering rules and assignment rules
422 * into different stage in order to guarantee that filtering rules
Saurav Das52025962016-01-28 22:30:01 -0800423 * always go first, as required by ofdpa.
Charles Chan14967c22015-12-07 11:11:50 -0800424 */
425 List<FlowRule> allRules = processVlanIdFilter(
426 portCriterion, vidCriterion, assignedVlan, applicationId);
427 List<FlowRule> filteringRules = new ArrayList<>();
428 List<FlowRule> assignmentRules = new ArrayList<>();
429
430 allRules.forEach(flowRule -> {
Charles Chand1172632017-03-15 17:33:09 -0700431 VlanId vlanId;
432 if (requireVlanExtensions()) {
433 ExtensionCriterion extCriterion =
434 (ExtensionCriterion) flowRule.selector().getCriterion(Criterion.Type.EXTENSION);
435 vlanId = ((OfdpaMatchVlanVid) extCriterion.extensionSelector()).vlanId();
436 } else {
437 VlanIdCriterion vlanIdCriterion =
438 (VlanIdCriterion) flowRule.selector().getCriterion(Criterion.Type.VLAN_VID);
439 vlanId = vlanIdCriterion.vlanId();
440 }
Charles Chanbe8aea42016-02-24 12:04:47 -0800441 if (!vlanId.equals(VlanId.NONE)) {
Charles Chan14967c22015-12-07 11:11:50 -0800442 filteringRules.add(flowRule);
443 } else {
444 assignmentRules.add(flowRule);
445 }
446 });
447
448 for (FlowRule filteringRule : filteringRules) {
Saurav Das961beb22017-03-29 19:09:17 -0700449 log.trace("{} VLAN filtering rule in VLAN table: {} for dev: {}",
Saurav Das018605f2017-02-18 14:05:44 -0800450 (install) ? "adding" : "removing", filteringRule, deviceId);
Charles Chan14967c22015-12-07 11:11:50 -0800451 ops = install ? ops.add(filteringRule) : ops.remove(filteringRule);
452 }
453
454 ops.newStage();
455
456 for (FlowRule assignmentRule : assignmentRules) {
Saurav Das961beb22017-03-29 19:09:17 -0700457 log.trace("{} VLAN assignment rule in VLAN table: {} for dev: {}",
Saurav Das018605f2017-02-18 14:05:44 -0800458 (install) ? "adding" : "removing", assignmentRule, deviceId);
Charles Chan14967c22015-12-07 11:11:50 -0800459 ops = install ? ops.add(assignmentRule) : ops.remove(assignmentRule);
Saurav Das822c4e22015-10-23 10:51:11 -0700460 }
461 }
462
Saurav Das822c4e22015-10-23 10:51:11 -0700463 // apply filtering flow rules
464 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
465 @Override
466 public void onSuccess(FlowRuleOperations ops) {
Saurav Das018605f2017-02-18 14:05:44 -0800467 log.debug("Applied {} filtering rules in device {}",
Saurav Das822c4e22015-10-23 10:51:11 -0700468 ops.stages().get(0).size(), deviceId);
469 pass(filt);
470 }
471
472 @Override
473 public void onError(FlowRuleOperations ops) {
474 log.info("Failed to apply all filtering rules in dev {}", deviceId);
475 fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED);
476 }
477 }));
478
479 }
480
481 /**
Charles Chand1172632017-03-15 17:33:09 -0700482 * Internal implementation of processVlanIdFilter.
483 * <p>
484 * The is_present bit in set_vlan_vid action is required to be 0 in OFDPA i12.
485 * Since it is non-OF spec, we need an extension treatment for that.
486 * The useVlanExtension must be set to false for OFDPA i12.
487 * </p>
Saurav Das0e99e2b2015-10-28 12:39:42 -0700488 *
Charles Chanf9e98652016-09-07 16:54:23 -0700489 * @param portCriterion port on device for which this filter is programmed
490 * @param vidCriterion vlan assigned to port, or NONE for untagged
491 * @param assignedVlan assigned vlan-id for untagged packets
492 * @param applicationId for application programming this filter
Saurav Das822c4e22015-10-23 10:51:11 -0700493 * @return list of FlowRule for port-vlan filters
494 */
495 protected List<FlowRule> processVlanIdFilter(PortCriterion portCriterion,
Yi Tsengef19de12017-04-24 11:33:05 -0700496 VlanIdCriterion vidCriterion,
497 VlanId assignedVlan,
498 ApplicationId applicationId) {
Charles Chan14967c22015-12-07 11:11:50 -0800499 List<FlowRule> rules = new ArrayList<>();
Saurav Das822c4e22015-10-23 10:51:11 -0700500 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
501 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800502 TrafficSelector.Builder preSelector = null;
503 TrafficTreatment.Builder preTreatment = null;
504
Saurav Das4f980082015-11-05 13:39:15 -0800505 treatment.transition(TMAC_TABLE);
506
Saurav Das822c4e22015-10-23 10:51:11 -0700507 if (vidCriterion.vlanId() == VlanId.NONE) {
508 // untagged packets are assigned vlans
Charles Chand1172632017-03-15 17:33:09 -0700509 preSelector = DefaultTrafficSelector.builder();
510 if (requireVlanExtensions()) {
511 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(VlanId.NONE);
512 selector.extension(ofdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700513 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
514 treatment.extension(ofdpaSetVlanVid, deviceId);
Charles Chand1172632017-03-15 17:33:09 -0700515
516 OfdpaMatchVlanVid preOfdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
517 preSelector.extension(preOfdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700518 } else {
Charles Chand1172632017-03-15 17:33:09 -0700519 selector.matchVlanId(VlanId.NONE);
Charles Chanf9e98652016-09-07 16:54:23 -0700520 treatment.setVlanId(assignedVlan);
Charles Chand1172632017-03-15 17:33:09 -0700521
522 preSelector.matchVlanId(assignedVlan);
523 }
524 preTreatment = DefaultTrafficTreatment.builder().transition(TMAC_TABLE);
525 } else {
526 if (requireVlanExtensions()) {
527 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
528 selector.extension(ofdpaMatchVlanVid, deviceId);
529 } else {
530 selector.matchVlanId(vidCriterion.vlanId());
Charles Chanf9e98652016-09-07 16:54:23 -0700531 }
Charles Chan14967c22015-12-07 11:11:50 -0800532
Charles Chand55e84d2016-03-30 17:54:24 -0700533 if (!assignedVlan.equals(vidCriterion.vlanId())) {
Charles Chand1172632017-03-15 17:33:09 -0700534 if (requireVlanExtensions()) {
Charles Chanc03782d2017-01-31 13:57:55 -0800535 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
536 treatment.extension(ofdpaSetVlanVid, deviceId);
537 } else {
538 treatment.setVlanId(assignedVlan);
539 }
Charles Chand55e84d2016-03-30 17:54:24 -0700540 }
Saurav Das822c4e22015-10-23 10:51:11 -0700541 }
Saurav Das822c4e22015-10-23 10:51:11 -0700542
543 // ofdpa cannot match on ALL portnumber, so we need to use separate
544 // rules for each port.
Charles Chan14967c22015-12-07 11:11:50 -0800545 List<PortNumber> portnums = new ArrayList<>();
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530546 if (portCriterion != null) {
547 if (PortNumber.ALL.equals(portCriterion.port())) {
548 for (Port port : deviceService.getPorts(deviceId)) {
549 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
550 portnums.add(port.number());
551 }
Saurav Das822c4e22015-10-23 10:51:11 -0700552 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530553 } else {
554 portnums.add(portCriterion.port());
Saurav Das822c4e22015-10-23 10:51:11 -0700555 }
556 } else {
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530557 log.warn("Filtering Objective missing Port Criterion . " +
558 "VLAN Table cannot be programmed for {}",
559 deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700560 }
Saurav Das4f980082015-11-05 13:39:15 -0800561
Saurav Das822c4e22015-10-23 10:51:11 -0700562 for (PortNumber pnum : portnums) {
Saurav Das4f980082015-11-05 13:39:15 -0800563 // create rest of flowrule
Saurav Das822c4e22015-10-23 10:51:11 -0700564 selector.matchInPort(pnum);
565 FlowRule rule = DefaultFlowRule.builder()
566 .forDevice(deviceId)
567 .withSelector(selector.build())
568 .withTreatment(treatment.build())
569 .withPriority(DEFAULT_PRIORITY)
570 .fromApp(applicationId)
571 .makePermanent()
572 .forTable(VLAN_TABLE).build();
Charles Chan14967c22015-12-07 11:11:50 -0800573
574 if (preSelector != null) {
575 preSelector.matchInPort(pnum);
576 FlowRule preRule = DefaultFlowRule.builder()
577 .forDevice(deviceId)
578 .withSelector(preSelector.build())
579 .withTreatment(preTreatment.build())
580 .withPriority(DEFAULT_PRIORITY)
581 .fromApp(applicationId)
582 .makePermanent()
583 .forTable(VLAN_TABLE).build();
584 rules.add(preRule);
585 }
586
Saurav Das822c4e22015-10-23 10:51:11 -0700587 rules.add(rule);
588 }
589 return rules;
590 }
591
592 /**
593 * Allows routed packets with correct destination MAC to be directed
594 * to unicast-IP routing table or MPLS forwarding table.
Saurav Das822c4e22015-10-23 10:51:11 -0700595 *
596 * @param portCriterion port on device for which this filter is programmed
597 * @param ethCriterion dstMac of device for which is filter is programmed
598 * @param vidCriterion vlan assigned to port, or NONE for untagged
Saurav Das0e99e2b2015-10-28 12:39:42 -0700599 * @param assignedVlan assigned vlan-id for untagged packets
Saurav Das822c4e22015-10-23 10:51:11 -0700600 * @param applicationId for application programming this filter
601 * @return list of FlowRule for port-vlan filters
602
603 */
604 protected List<FlowRule> processEthDstFilter(PortCriterion portCriterion,
605 EthCriterion ethCriterion,
606 VlanIdCriterion vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700607 VlanId assignedVlan,
Saurav Das822c4e22015-10-23 10:51:11 -0700608 ApplicationId applicationId) {
Charles Chan5270ed02016-01-30 23:22:37 -0800609 // Consider PortNumber.ANY as wildcard. Match ETH_DST only
Sivachidambaram Subramanianc598d062017-05-16 22:42:06 +0530610 if (portCriterion != null && PortNumber.ANY.equals(portCriterion.port())) {
Charles Chan5270ed02016-01-30 23:22:37 -0800611 return processEthDstOnlyFilter(ethCriterion, applicationId);
612 }
613
Charles Chan5b9df8d2016-03-28 22:21:40 -0700614 // Multicast MAC
615 if (ethCriterion.mask() != null) {
616 return processMcastEthDstFilter(ethCriterion, applicationId);
617 }
618
Saurav Das822c4e22015-10-23 10:51:11 -0700619 //handling untagged packets via assigned VLAN
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530620 if (vidCriterion != null && vidCriterion.vlanId() == VlanId.NONE) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700621 vidCriterion = (VlanIdCriterion) Criteria.matchVlanId(assignedVlan);
Saurav Das822c4e22015-10-23 10:51:11 -0700622 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530623 List<FlowRule> rules = new ArrayList<>();
624 OfdpaMatchVlanVid ofdpaMatchVlanVid = null;
625 if (vidCriterion != null && requireVlanExtensions()) {
626 ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
627 }
Saurav Das822c4e22015-10-23 10:51:11 -0700628 // ofdpa cannot match on ALL portnumber, so we need to use separate
629 // rules for each port.
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -0700630 List<PortNumber> portnums = new ArrayList<>();
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530631 if (portCriterion != null) {
632 if (PortNumber.ALL.equals(portCriterion.port())) {
633 for (Port port : deviceService.getPorts(deviceId)) {
634 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
635 portnums.add(port.number());
636 }
Saurav Das822c4e22015-10-23 10:51:11 -0700637 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530638 } else {
639 portnums.add(portCriterion.port());
640 }
641 for (PortNumber pnum : portnums) {
642 rules.add(buildTmacRuleForIpv4(ethCriterion,
643 vidCriterion,
644 ofdpaMatchVlanVid,
645 applicationId,
646 pnum));
647 rules.add(buildTmacRuleForMpls(ethCriterion,
648 vidCriterion,
649 ofdpaMatchVlanVid,
650 applicationId,
651 pnum));
652 rules.add(buildTmacRuleForIpv6(ethCriterion,
653 vidCriterion,
654 ofdpaMatchVlanVid,
655 applicationId,
656 pnum));
Saurav Das822c4e22015-10-23 10:51:11 -0700657 }
658 } else {
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530659 rules.add(buildTmacRuleForIpv4(ethCriterion,
660 vidCriterion,
661 ofdpaMatchVlanVid,
662 applicationId,
663 null));
664 rules.add(buildTmacRuleForMpls(ethCriterion,
665 vidCriterion,
666 ofdpaMatchVlanVid,
667 applicationId,
668 null));
669 rules.add(buildTmacRuleForIpv6(ethCriterion,
670 vidCriterion,
671 ofdpaMatchVlanVid,
672 applicationId,
673 null));
Saurav Das822c4e22015-10-23 10:51:11 -0700674 }
675 return rules;
676 }
677
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530678 /**
679 * Builds TMAC rules for IPv4 packets.
680 *
681 * @param ethCriterion
682 * @param vidCriterion
683 * @param ofdpaMatchVlanVid
684 * @param applicationId
685 * @param pnum
686 * @return TMAC rule for IPV4 packets
687 */
688 private FlowRule buildTmacRuleForIpv4(EthCriterion ethCriterion,
689 VlanIdCriterion vidCriterion,
690 OfdpaMatchVlanVid ofdpaMatchVlanVid,
691 ApplicationId applicationId,
692 PortNumber pnum) {
693 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
694 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
695 if (pnum != null) {
696 if (matchInPortTmacTable()) {
697 selector.matchInPort(pnum);
698 } else {
699 log.info("Pipeline does not support IN_PORT matching in TMAC table, " +
700 "ignoring the IN_PORT criteria");
701 }
702 }
703 if (vidCriterion != null) {
704 if (requireVlanExtensions()) {
705 selector.extension(ofdpaMatchVlanVid, deviceId);
706 } else {
707 selector.matchVlanId(vidCriterion.vlanId());
708 }
709 }
710 selector.matchEthType(Ethernet.TYPE_IPV4);
711 selector.matchEthDst(ethCriterion.mac());
712 treatment.transition(UNICAST_ROUTING_TABLE);
713 return DefaultFlowRule.builder()
714 .forDevice(deviceId)
715 .withSelector(selector.build())
716 .withTreatment(treatment.build())
717 .withPriority(DEFAULT_PRIORITY)
718 .fromApp(applicationId)
719 .makePermanent()
720 .forTable(TMAC_TABLE).build();
721 }
722
723 /**
724 * Builds TMAC rule for MPLS packets.
725 *
726 * @param ethCriterion
727 * @param vidCriterion
728 * @param ofdpaMatchVlanVid
729 * @param applicationId
730 * @param pnum
731 * @return TMAC rule for MPLS packets
732 */
733 private FlowRule buildTmacRuleForMpls(EthCriterion ethCriterion,
734 VlanIdCriterion vidCriterion,
735 OfdpaMatchVlanVid ofdpaMatchVlanVid,
736 ApplicationId applicationId,
737 PortNumber pnum) {
738 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
739 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
740 if (pnum != null) {
741 if (matchInPortTmacTable()) {
742 selector.matchInPort(pnum);
743 } else {
744 log.info("Pipeline does not support IN_PORT matching in TMAC table, " +
745 "ignoring the IN_PORT criteria");
746 }
747 }
748 if (vidCriterion != null) {
749 if (requireVlanExtensions()) {
750 selector.extension(ofdpaMatchVlanVid, deviceId);
751 } else {
752 selector.matchVlanId(vidCriterion.vlanId());
753 }
754 }
755 selector.matchEthType(Ethernet.MPLS_UNICAST);
756 selector.matchEthDst(ethCriterion.mac());
757 treatment.transition(MPLS_TABLE_0);
758 return DefaultFlowRule.builder()
759 .forDevice(deviceId)
760 .withSelector(selector.build())
761 .withTreatment(treatment.build())
762 .withPriority(DEFAULT_PRIORITY)
763 .fromApp(applicationId)
764 .makePermanent()
765 .forTable(TMAC_TABLE).build();
766 }
767
768 /**
769 * Builds TMAC rules for IPv6 packets.
770 *
771 * @param ethCriterion
772 * @param vidCriterion
773 * @param ofdpaMatchVlanVid
774 * @param applicationId
775 * @param pnum
776 * @return TMAC rule for IPV6 packets
777 */
778 private FlowRule buildTmacRuleForIpv6(EthCriterion ethCriterion,
779 VlanIdCriterion vidCriterion,
780 OfdpaMatchVlanVid ofdpaMatchVlanVid,
781 ApplicationId applicationId,
782 PortNumber pnum) {
783 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
784 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
785 if (pnum != null) {
786 if (matchInPortTmacTable()) {
787 selector.matchInPort(pnum);
788 } else {
789 log.info("Pipeline does not support IN_PORT matching in TMAC table, " +
790 "ignoring the IN_PORT criteria");
791 }
792 }
793 if (vidCriterion != null) {
794 if (requireVlanExtensions()) {
795 selector.extension(ofdpaMatchVlanVid, deviceId);
796 } else {
797 selector.matchVlanId(vidCriterion.vlanId());
798 }
799 }
800 selector.matchEthType(Ethernet.TYPE_IPV6);
801 selector.matchEthDst(ethCriterion.mac());
802 treatment.transition(UNICAST_ROUTING_TABLE);
803 return DefaultFlowRule.builder()
804 .forDevice(deviceId)
805 .withSelector(selector.build())
806 .withTreatment(treatment.build())
807 .withPriority(DEFAULT_PRIORITY)
808 .fromApp(applicationId)
809 .makePermanent()
810 .forTable(TMAC_TABLE).build();
811 }
812
Charles Chan5270ed02016-01-30 23:22:37 -0800813 protected List<FlowRule> processEthDstOnlyFilter(EthCriterion ethCriterion,
Yi Tsengef19de12017-04-24 11:33:05 -0700814 ApplicationId applicationId) {
Pier Luigi0e358632017-01-31 09:35:05 -0800815 ImmutableList.Builder<FlowRule> builder = ImmutableList.builder();
816
Charles Chan5270ed02016-01-30 23:22:37 -0800817 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
818 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
819 selector.matchEthType(Ethernet.TYPE_IPV4);
820 selector.matchEthDst(ethCriterion.mac());
821 treatment.transition(UNICAST_ROUTING_TABLE);
822 FlowRule rule = DefaultFlowRule.builder()
823 .forDevice(deviceId)
824 .withSelector(selector.build())
825 .withTreatment(treatment.build())
826 .withPriority(DEFAULT_PRIORITY)
827 .fromApp(applicationId)
828 .makePermanent()
829 .forTable(TMAC_TABLE).build();
Pier Luigi0e358632017-01-31 09:35:05 -0800830 builder.add(rule);
831
832 selector = DefaultTrafficSelector.builder();
833 treatment = DefaultTrafficTreatment.builder();
834 selector.matchEthType(Ethernet.TYPE_IPV6);
835 selector.matchEthDst(ethCriterion.mac());
836 treatment.transition(UNICAST_ROUTING_TABLE);
837 rule = DefaultFlowRule.builder()
838 .forDevice(deviceId)
839 .withSelector(selector.build())
840 .withTreatment(treatment.build())
841 .withPriority(DEFAULT_PRIORITY)
842 .fromApp(applicationId)
843 .makePermanent()
844 .forTable(TMAC_TABLE).build();
845 return builder.add(rule).build();
Charles Chan5270ed02016-01-30 23:22:37 -0800846 }
847
Charles Chan5b9df8d2016-03-28 22:21:40 -0700848 protected List<FlowRule> processMcastEthDstFilter(EthCriterion ethCriterion,
Yi Tsengef19de12017-04-24 11:33:05 -0700849 ApplicationId applicationId) {
Charles Chan5b9df8d2016-03-28 22:21:40 -0700850 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
851 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
852 selector.matchEthType(Ethernet.TYPE_IPV4);
853 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
854 treatment.transition(MULTICAST_ROUTING_TABLE);
855 FlowRule rule = DefaultFlowRule.builder()
856 .forDevice(deviceId)
857 .withSelector(selector.build())
858 .withTreatment(treatment.build())
859 .withPriority(DEFAULT_PRIORITY)
860 .fromApp(applicationId)
861 .makePermanent()
862 .forTable(TMAC_TABLE).build();
863 return ImmutableList.<FlowRule>builder().add(rule).build();
864 }
865
Saurav Das822c4e22015-10-23 10:51:11 -0700866 private Collection<FlowRule> processForward(ForwardingObjective fwd) {
867 switch (fwd.flag()) {
868 case SPECIFIC:
869 return processSpecific(fwd);
870 case VERSATILE:
871 return processVersatile(fwd);
872 default:
873 fail(fwd, ObjectiveError.UNKNOWN);
874 log.warn("Unknown forwarding flag {}", fwd.flag());
875 }
876 return Collections.emptySet();
877 }
878
879 /**
880 * In the OF-DPA 2.0 pipeline, versatile forwarding objectives go to the
881 * ACL table.
882 * @param fwd the forwarding objective of type 'versatile'
883 * @return a collection of flow rules to be sent to the switch. An empty
884 * collection may be returned if there is a problem in processing
885 * the flow rule
886 */
Saurav Das52025962016-01-28 22:30:01 -0800887 protected Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
Saurav Das018605f2017-02-18 14:05:44 -0800888 log.debug("Processing versatile forwarding objective:{} in dev:{}",
Saurav Dasd2fded02016-12-02 15:43:47 -0800889 fwd.id(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700890
891 EthTypeCriterion ethType =
Saurav Das77b5e902016-01-27 17:01:59 -0800892 (EthTypeCriterion) fwd.selector().getCriterion(Criterion.Type.ETH_TYPE);
Saurav Das822c4e22015-10-23 10:51:11 -0700893 if (ethType == null) {
Saurav Dasd2fded02016-12-02 15:43:47 -0800894 log.error("Versatile forwarding objective:{} must include ethType",
895 fwd.id());
Saurav Das822c4e22015-10-23 10:51:11 -0700896 fail(fwd, ObjectiveError.BADPARAMS);
897 return Collections.emptySet();
898 }
899 if (fwd.nextId() == null && fwd.treatment() == null) {
900 log.error("Forwarding objective {} from {} must contain "
901 + "nextId or Treatment", fwd.selector(), fwd.appId());
Zsolt Haraszti9faab752016-02-17 15:55:20 -0800902 fail(fwd, ObjectiveError.BADPARAMS);
Saurav Das822c4e22015-10-23 10:51:11 -0700903 return Collections.emptySet();
904 }
Saurav Das49cb5a12016-01-16 22:54:07 -0800905
Saurav Das77b5e902016-01-27 17:01:59 -0800906 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
907 fwd.selector().criteria().forEach(criterion -> {
908 if (criterion instanceof VlanIdCriterion) {
909 VlanId vlanId = ((VlanIdCriterion) criterion).vlanId();
910 // ensure that match does not include vlan = NONE as OF-DPA does not
911 // match untagged packets this way in the ACL table.
912 if (vlanId.equals(VlanId.NONE)) {
913 return;
914 }
Charles Chand1172632017-03-15 17:33:09 -0700915 if (requireVlanExtensions()) {
916 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanId);
917 sbuilder.extension(ofdpaMatchVlanVid, deviceId);
918 } else {
919 sbuilder.matchVlanId(vlanId);
920 }
Pier Ventree0ae7a32016-11-23 09:57:42 -0800921 } else if (criterion instanceof Icmpv6TypeCriterion ||
922 criterion instanceof Icmpv6CodeCriterion) {
923 /*
924 * We silenty discard these criterions, our current
925 * OFDPA platform does not support these matches on
926 * the ACL table.
927 */
928 log.warn("ICMPv6 Type and ICMPv6 Code are not supported");
Saurav Das77b5e902016-01-27 17:01:59 -0800929 } else {
930 sbuilder.add(criterion);
931 }
932 });
933
Saurav Das822c4e22015-10-23 10:51:11 -0700934 // XXX driver does not currently do type checking as per Tables 65-67 in
935 // OFDPA 2.0 spec. The only allowed treatment is a punt to the controller.
Saurav Das49cb5a12016-01-16 22:54:07 -0800936 TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder();
937 if (fwd.treatment() != null) {
938 for (Instruction ins : fwd.treatment().allInstructions()) {
939 if (ins instanceof OutputInstruction) {
940 OutputInstruction o = (OutputInstruction) ins;
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530941 if (o != null && PortNumber.CONTROLLER.equals(o.port())) {
Saurav Das49cb5a12016-01-16 22:54:07 -0800942 ttBuilder.add(o);
943 } else {
944 log.warn("Only allowed treatments in versatile forwarding "
945 + "objectives are punts to the controller");
946 }
947 } else {
948 log.warn("Cannot process instruction in versatile fwd {}", ins);
949 }
Saurav Das822c4e22015-10-23 10:51:11 -0700950 }
Charles Chan2df0e8a2017-01-09 11:45:08 -0800951 if (fwd.treatment().clearedDeferred()) {
952 ttBuilder.wipeDeferred();
953 }
Saurav Das822c4e22015-10-23 10:51:11 -0700954 }
Saurav Das822c4e22015-10-23 10:51:11 -0700955 if (fwd.nextId() != null) {
Saurav Das49cb5a12016-01-16 22:54:07 -0800956 // overide case
957 NextGroup next = getGroupForNextObjective(fwd.nextId());
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530958 if (next == null) {
959 fail(fwd, ObjectiveError.BADPARAMS);
960 return Collections.emptySet();
961 }
Saurav Das49cb5a12016-01-16 22:54:07 -0800962 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
963 // we only need the top level group's key to point the flow to it
964 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
965 if (group == null) {
966 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
967 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
968 fail(fwd, ObjectiveError.GROUPMISSING);
969 return Collections.emptySet();
970 }
971 ttBuilder.deferred().group(group.id());
Saurav Das822c4e22015-10-23 10:51:11 -0700972 }
Saurav Das49cb5a12016-01-16 22:54:07 -0800973
974 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
975 .fromApp(fwd.appId())
976 .withPriority(fwd.priority())
977 .forDevice(deviceId)
Saurav Das77b5e902016-01-27 17:01:59 -0800978 .withSelector(sbuilder.build())
Saurav Das49cb5a12016-01-16 22:54:07 -0800979 .withTreatment(ttBuilder.build())
980 .makePermanent()
981 .forTable(ACL_TABLE);
982 return Collections.singletonList(ruleBuilder.build());
Saurav Das822c4e22015-10-23 10:51:11 -0700983 }
984
985 /**
986 * In the OF-DPA 2.0 pipeline, specific forwarding refers to the IP table
Saurav Das8a0732e2015-11-20 15:27:53 -0800987 * (unicast or multicast) or the L2 table (mac + vlan) or the MPLS table.
Saurav Das822c4e22015-10-23 10:51:11 -0700988 *
989 * @param fwd the forwarding objective of type 'specific'
990 * @return a collection of flow rules. Typically there will be only one
991 * for this type of forwarding objective. An empty set may be
992 * returned if there is an issue in processing the objective.
993 */
Saurav Das8a0732e2015-11-20 15:27:53 -0800994 protected Collection<FlowRule> processSpecific(ForwardingObjective fwd) {
Saurav Das25190812016-05-27 13:54:07 -0700995 log.debug("Processing specific fwd objective:{} in dev:{} with next:{}",
Saurav Das4ce45962015-11-24 23:21:05 -0800996 fwd.id(), deviceId, fwd.nextId());
997 boolean isEthTypeObj = isSupportedEthTypeObjective(fwd);
998 boolean isEthDstObj = isSupportedEthDstObjective(fwd);
999
1000 if (isEthTypeObj) {
1001 return processEthTypeSpecific(fwd);
1002 } else if (isEthDstObj) {
1003 return processEthDstSpecific(fwd);
1004 } else {
1005 log.warn("processSpecific: Unsupported forwarding objective "
1006 + "criteria fwd:{} in dev:{}", fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001007 fail(fwd, ObjectiveError.UNSUPPORTED);
1008 return Collections.emptySet();
1009 }
Saurav Das4ce45962015-11-24 23:21:05 -08001010 }
1011
Saurav Das4ce45962015-11-24 23:21:05 -08001012 /**
1013 * Handles forwarding rules to the IP and MPLS tables.
1014 *
1015 * @param fwd the forwarding objective
1016 * @return A collection of flow rules, or an empty set
1017 */
1018 protected Collection<FlowRule> processEthTypeSpecific(ForwardingObjective fwd) {
Charles Chancad338a2016-09-16 18:03:11 -07001019 return processEthTypeSpecificInternal(fwd, false, ACL_TABLE);
Charles Chanf9e98652016-09-07 16:54:23 -07001020 }
1021
1022 /**
1023 * Internal implementation of processEthTypeSpecific.
1024 * <p>
1025 * Wildcarded IPv4_DST is not supported in OFDPA i12. Therefore, we break
1026 * the rule into 0.0.0.0/1 and 128.0.0.0/1.
1027 * The allowDefaultRoute must be set to false for OFDPA i12.
1028 * </p>
1029 *
1030 * @param fwd the forwarding objective
1031 * @param allowDefaultRoute allow wildcarded IPv4_DST or not
Ray Milkey0bb1e102016-11-10 14:51:27 -08001032 * @param mplsNextTable next MPLS table
Charles Chanf9e98652016-09-07 16:54:23 -07001033 * @return A collection of flow rules, or an empty set
1034 */
1035 protected Collection<FlowRule> processEthTypeSpecificInternal(ForwardingObjective fwd,
Charles Chancad338a2016-09-16 18:03:11 -07001036 boolean allowDefaultRoute,
1037 int mplsNextTable) {
Saurav Das4ce45962015-11-24 23:21:05 -08001038 TrafficSelector selector = fwd.selector();
1039 EthTypeCriterion ethType =
1040 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
Flavio Castroe10fa242016-01-15 12:43:51 -08001041 boolean popMpls = false;
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001042 boolean emptyGroup = false;
Charles Chan188ebf52015-12-23 00:15:11 -08001043 int forTableId;
Saurav Das8a0732e2015-11-20 15:27:53 -08001044 TrafficSelector.Builder filteredSelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -08001045 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
Flavio Castroe10fa242016-01-15 12:43:51 -08001046 TrafficSelector.Builder complementarySelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -08001047
Saurav Das8a0732e2015-11-20 15:27:53 -08001048 if (ethType.ethType().toShort() == Ethernet.TYPE_IPV4) {
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001049 if (buildIpv4Selector(filteredSelector, complementarySelector, fwd, allowDefaultRoute) < 0) {
1050 return Collections.emptyList();
1051 }
1052 // We need to set properly the next table
Flavio Castroe10fa242016-01-15 12:43:51 -08001053 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
Charles Chan5b9df8d2016-03-28 22:21:40 -07001054 if (ipv4Dst.isMulticast()) {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001055 forTableId = MULTICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -08001056 } else {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001057 forTableId = UNICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -08001058 }
Charles Chan14967c22015-12-07 11:11:50 -08001059 if (fwd.treatment() != null) {
1060 for (Instruction instr : fwd.treatment().allInstructions()) {
1061 if (instr instanceof L3ModificationInstruction &&
1062 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
Saurav Das9c705342017-01-06 11:36:01 -08001063 // XXX decrementing IP ttl is done automatically for routing, this
1064 // action is ignored or rejected in ofdpa as it is not fully implemented
1065 //tb.deferred().add(instr);
Charles Chan14967c22015-12-07 11:11:50 -08001066 }
1067 }
1068 }
1069
Pier Ventree0ae7a32016-11-23 09:57:42 -08001070 } else if (ethType.ethType().toShort() == Ethernet.TYPE_IPV6) {
1071 if (buildIpv6Selector(filteredSelector, fwd) < 0) {
1072 return Collections.emptyList();
1073 }
1074 forTableId = UNICAST_ROUTING_TABLE;
1075 if (fwd.treatment() != null) {
1076 for (Instruction instr : fwd.treatment().allInstructions()) {
1077 if (instr instanceof L3ModificationInstruction &&
1078 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1079 // XXX decrementing IP ttl is done automatically for routing, this
1080 // action is ignored or rejected in ofdpa as it is not fully implemented
1081 //tb.deferred().add(instr);
1082 }
1083 }
1084 }
Saurav Das8a0732e2015-11-20 15:27:53 -08001085 } else {
1086 filteredSelector
1087 .matchEthType(Ethernet.MPLS_UNICAST)
1088 .matchMplsLabel(((MplsCriterion)
1089 selector.getCriterion(Criterion.Type.MPLS_LABEL)).label());
1090 MplsBosCriterion bos = (MplsBosCriterion) selector
Pier Ventre140a8942016-11-02 07:26:38 -07001091 .getCriterion(MPLS_BOS);
Saurav Das8a0732e2015-11-20 15:27:53 -08001092 if (bos != null) {
1093 filteredSelector.matchMplsBos(bos.mplsBos());
1094 }
1095 forTableId = MPLS_TABLE_1;
Saurav Das4ce45962015-11-24 23:21:05 -08001096 log.debug("processing MPLS specific forwarding objective {} -> next:{}"
1097 + " in dev {}", fwd.id(), fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001098
Charles Chan14967c22015-12-07 11:11:50 -08001099 if (fwd.treatment() != null) {
1100 for (Instruction instr : fwd.treatment().allInstructions()) {
1101 if (instr instanceof L2ModificationInstruction &&
1102 ((L2ModificationInstruction) instr).subtype() == L2SubType.MPLS_POP) {
Saurav Das8a0732e2015-11-20 15:27:53 -08001103 popMpls = true;
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001104 // OF-DPA does not pop in MPLS table in some cases. For the L3 VPN, it requires
Saurav Das9c705342017-01-06 11:36:01 -08001105 // setting the MPLS_TYPE so pop can happen down the pipeline
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001106 if (mplsNextTable == MPLS_TYPE_TABLE && isNotMplsBos(selector)) {
1107 tb.immediate().popMpls();
1108 }
Charles Chan14967c22015-12-07 11:11:50 -08001109 }
1110 if (instr instanceof L3ModificationInstruction &&
1111 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1112 // FIXME Should modify the app to send the correct DEC_MPLS_TTL instruction
1113 tb.immediate().decMplsTtl();
1114 }
1115 if (instr instanceof L3ModificationInstruction &&
1116 ((L3ModificationInstruction) instr).subtype() == L3SubType.TTL_IN) {
1117 tb.immediate().add(instr);
1118 }
Saurav Das8a0732e2015-11-20 15:27:53 -08001119 }
1120 }
1121 }
Saurav Das822c4e22015-10-23 10:51:11 -07001122
1123 if (fwd.nextId() != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -08001124 if (forTableId == MPLS_TABLE_1 && !popMpls) {
1125 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
Saurav Das25190812016-05-27 13:54:07 -07001126 + "is not implemented in OF-DPA yet. Aborting this flow {} -> next:{}"
1127 + "in this device {}", fwd.id(), fwd.nextId(), deviceId);
Pier Ventree0ae7a32016-11-23 09:57:42 -08001128 // XXX We could convert to forwarding to a single-port, via a MPLS interface,
1129 // or a MPLS SWAP (with-same) but that would have to be handled in the next-objective.
1130 // Also the pop-mpls logic used here won't work in non-BoS case.
Saurav Das4ce45962015-11-24 23:21:05 -08001131 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
Saurav Das8a0732e2015-11-20 15:27:53 -08001132 return Collections.emptySet();
1133 }
1134
Saurav Das423fe2b2015-12-04 10:52:59 -08001135 NextGroup next = getGroupForNextObjective(fwd.nextId());
1136 if (next != null) {
1137 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1138 // we only need the top level group's key to point the flow to it
1139 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
Pier Ventre140a8942016-11-02 07:26:38 -07001140 if (isNotMplsBos(selector) && group.type().equals(HASHED)) {
1141 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
1142 + "is not implemented in OF-DPA yet. Aborting this flow {} -> next:{}"
1143 + "in this device {}", fwd.id(), fwd.nextId(), deviceId);
1144 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1145 return Collections.emptySet();
1146 }
Saurav Das423fe2b2015-12-04 10:52:59 -08001147 if (group == null) {
1148 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1149 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1150 fail(fwd, ObjectiveError.GROUPMISSING);
1151 return Collections.emptySet();
1152 }
1153 tb.deferred().group(group.id());
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001154 // check if group is empty
1155 if (gkeys.size() == 1 && gkeys.get(0).size() == 1) {
1156 log.warn("Found empty group 0x{} in dev:{} .. will retry fwd:{}",
1157 Integer.toHexString(group.id().id()), deviceId, fwd.id());
1158 emptyGroup = true;
1159 }
Saurav Das25190812016-05-27 13:54:07 -07001160 } else {
1161 log.warn("Cannot find group for nextId:{} in dev:{}. Aborting fwd:{}",
1162 fwd.nextId(), deviceId, fwd.id());
1163 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1164 return Collections.emptySet();
Saurav Das822c4e22015-10-23 10:51:11 -07001165 }
Saurav Das822c4e22015-10-23 10:51:11 -07001166 }
Charles Chancad338a2016-09-16 18:03:11 -07001167
1168 if (forTableId == MPLS_TABLE_1) {
Pier Ventre140a8942016-11-02 07:26:38 -07001169 if (mplsNextTable == MPLS_L3_TYPE_TABLE) {
Charles Chancad338a2016-09-16 18:03:11 -07001170 Ofdpa3SetMplsType setMplsType = new Ofdpa3SetMplsType(Ofdpa3MplsType.L3_PHP);
Saurav Das9c705342017-01-06 11:36:01 -08001171 // set mpls type as apply_action
1172 tb.immediate().extension(setMplsType, deviceId);
Charles Chancad338a2016-09-16 18:03:11 -07001173 }
1174 tb.transition(mplsNextTable);
1175 } else {
1176 tb.transition(ACL_TABLE);
1177 }
1178
Saurav Das822c4e22015-10-23 10:51:11 -07001179 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1180 .fromApp(fwd.appId())
1181 .withPriority(fwd.priority())
1182 .forDevice(deviceId)
Saurav Das8a0732e2015-11-20 15:27:53 -08001183 .withSelector(filteredSelector.build())
1184 .withTreatment(tb.build())
1185 .forTable(forTableId);
Saurav Das822c4e22015-10-23 10:51:11 -07001186
1187 if (fwd.permanent()) {
1188 ruleBuilder.makePermanent();
1189 } else {
1190 ruleBuilder.makeTemporary(fwd.timeout());
1191 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001192 Collection<FlowRule> flowRuleCollection = new ArrayList<>();
1193 flowRuleCollection.add(ruleBuilder.build());
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001194 if (!allowDefaultRoute) {
Pier Ventree0ae7a32016-11-23 09:57:42 -08001195 flowRuleCollection.add(
1196 defaultRoute(fwd, complementarySelector, forTableId, tb)
1197 );
Flavio Castroe10fa242016-01-15 12:43:51 -08001198 log.debug("Default rule 0.0.0.0/0 is being installed two rules");
1199 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001200 // XXX retrying flows may be necessary due to bug CORD-554
1201 if (emptyGroup) {
1202 executorService.schedule(new RetryFlows(fwd, flowRuleCollection),
1203 RETRY_MS, TimeUnit.MILLISECONDS);
1204 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001205 return flowRuleCollection;
Saurav Das822c4e22015-10-23 10:51:11 -07001206 }
1207
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001208 protected int buildIpv4Selector(TrafficSelector.Builder builderToUpdate,
1209 TrafficSelector.Builder extBuilder,
1210 ForwardingObjective fwd,
1211 boolean allowDefaultRoute) {
1212 TrafficSelector selector = fwd.selector();
1213
1214 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
1215 if (ipv4Dst.isMulticast()) {
1216 if (ipv4Dst.prefixLength() != 32) {
1217 log.warn("Multicast specific forwarding objective can only be /32");
1218 fail(fwd, ObjectiveError.BADPARAMS);
1219 return -1;
1220 }
1221 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
1222 if (assignedVlan == null) {
1223 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
1224 fail(fwd, ObjectiveError.BADPARAMS);
1225 return -1;
1226 }
Charles Chand1172632017-03-15 17:33:09 -07001227 if (requireVlanExtensions()) {
1228 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
1229 builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
1230 } else {
1231 builderToUpdate.matchVlanId(assignedVlan);
1232 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001233 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1234 log.debug("processing IPv4 multicast specific forwarding objective {} -> next:{}"
1235 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1236 } else {
1237 if (ipv4Dst.prefixLength() == 0) {
1238 if (allowDefaultRoute) {
1239 // The entire IPV4_DST field is wildcarded intentionally
1240 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4);
1241 } else {
1242 // NOTE: The switch does not support matching 0.0.0.0/0
1243 // Split it into 0.0.0.0/1 and 128.0.0.0/1
1244 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4)
1245 .matchIPDst(IpPrefix.valueOf("0.0.0.0/1"));
1246 extBuilder.matchEthType(Ethernet.TYPE_IPV4)
1247 .matchIPDst(IpPrefix.valueOf("128.0.0.0/1"));
1248 }
1249 } else {
1250 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1251 }
1252 log.debug("processing IPv4 unicast specific forwarding objective {} -> next:{}"
1253 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1254 }
1255 return 0;
1256 }
1257
1258 /**
1259 * Helper method to build Ipv6 selector using the selector provided by
1260 * a forwarding objective.
1261 *
1262 * @param builderToUpdate the builder to update
1263 * @param fwd the selector to read
1264 * @return 0 if the update ends correctly. -1 if the matches
1265 * are not yet supported
1266 */
1267 protected int buildIpv6Selector(TrafficSelector.Builder builderToUpdate,
1268 ForwardingObjective fwd) {
1269
1270 TrafficSelector selector = fwd.selector();
1271
1272 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
1273 if (ipv6Dst.isMulticast()) {
1274 log.warn("IPv6 Multicast is currently not supported");
1275 fail(fwd, ObjectiveError.BADPARAMS);
1276 return -1;
1277 }
1278 if (ipv6Dst.prefixLength() != 0) {
1279 builderToUpdate.matchIPv6Dst(ipv6Dst);
1280 }
1281 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6);
1282 log.debug("processing IPv6 unicast specific forwarding objective {} -> next:{}"
1283 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1284 return 0;
1285 }
1286
Pier Ventree0ae7a32016-11-23 09:57:42 -08001287 protected FlowRule defaultRoute(ForwardingObjective fwd,
1288 TrafficSelector.Builder complementarySelector,
1289 int forTableId,
1290 TrafficTreatment.Builder tb) {
1291 FlowRule.Builder rule = DefaultFlowRule.builder()
1292 .fromApp(fwd.appId())
1293 .withPriority(fwd.priority())
1294 .forDevice(deviceId)
1295 .withSelector(complementarySelector.build())
1296 .withTreatment(tb.build())
1297 .forTable(forTableId);
1298 if (fwd.permanent()) {
1299 rule.makePermanent();
1300 } else {
1301 rule.makeTemporary(fwd.timeout());
1302 }
1303 return rule.build();
1304 }
1305
Saurav Das4ce45962015-11-24 23:21:05 -08001306 /**
1307 * Handles forwarding rules to the L2 bridging table. Flow actions are not
1308 * allowed in the bridging table - instead we use L2 Interface group or
1309 * L2 flood group
1310 *
1311 * @param fwd the forwarding objective
1312 * @return A collection of flow rules, or an empty set
1313 */
1314 protected Collection<FlowRule> processEthDstSpecific(ForwardingObjective fwd) {
1315 List<FlowRule> rules = new ArrayList<>();
1316
1317 // Build filtered selector
1318 TrafficSelector selector = fwd.selector();
1319 EthCriterion ethCriterion = (EthCriterion) selector
1320 .getCriterion(Criterion.Type.ETH_DST);
1321 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) selector
1322 .getCriterion(Criterion.Type.VLAN_VID);
1323
1324 if (vlanIdCriterion == null) {
1325 log.warn("Forwarding objective for bridging requires vlan. Not "
1326 + "installing fwd:{} in dev:{}", fwd.id(), deviceId);
1327 fail(fwd, ObjectiveError.BADPARAMS);
1328 return Collections.emptySet();
1329 }
1330
1331 TrafficSelector.Builder filteredSelectorBuilder =
1332 DefaultTrafficSelector.builder();
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001333
1334 if (!ethCriterion.mac().equals(NONE) &&
1335 !ethCriterion.mac().equals(BROADCAST)) {
Saurav Das4ce45962015-11-24 23:21:05 -08001336 filteredSelectorBuilder.matchEthDst(ethCriterion.mac());
1337 log.debug("processing L2 forwarding objective:{} -> next:{} in dev:{}",
1338 fwd.id(), fwd.nextId(), deviceId);
1339 } else {
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001340 // Use wildcard DST_MAC if the MacAddress is None or Broadcast
Saurav Das4ce45962015-11-24 23:21:05 -08001341 log.debug("processing L2 Broadcast forwarding objective:{} -> next:{} "
1342 + "in dev:{} for vlan:{}",
1343 fwd.id(), fwd.nextId(), deviceId, vlanIdCriterion.vlanId());
1344 }
Charles Chand1172632017-03-15 17:33:09 -07001345 if (requireVlanExtensions()) {
1346 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanIdCriterion.vlanId());
1347 filteredSelectorBuilder.extension(ofdpaMatchVlanVid, deviceId);
1348 } else {
1349 filteredSelectorBuilder.matchVlanId(vlanIdCriterion.vlanId());
1350 }
Saurav Das4ce45962015-11-24 23:21:05 -08001351 TrafficSelector filteredSelector = filteredSelectorBuilder.build();
1352
1353 if (fwd.treatment() != null) {
1354 log.warn("Ignoring traffic treatment in fwd rule {} meant for L2 table"
1355 + "for dev:{}. Expecting only nextId", fwd.id(), deviceId);
1356 }
1357
1358 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1359 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001360 NextGroup next = getGroupForNextObjective(fwd.nextId());
Saurav Das4ce45962015-11-24 23:21:05 -08001361 if (next != null) {
1362 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1363 // we only need the top level group's key to point the flow to it
1364 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1365 if (group != null) {
1366 treatmentBuilder.deferred().group(group.id());
1367 } else {
1368 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1369 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1370 fail(fwd, ObjectiveError.GROUPMISSING);
1371 return Collections.emptySet();
1372 }
1373 }
1374 }
1375 treatmentBuilder.immediate().transition(ACL_TABLE);
1376 TrafficTreatment filteredTreatment = treatmentBuilder.build();
1377
1378 // Build bridging table entries
1379 FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder();
1380 flowRuleBuilder.fromApp(fwd.appId())
1381 .withPriority(fwd.priority())
1382 .forDevice(deviceId)
1383 .withSelector(filteredSelector)
1384 .withTreatment(filteredTreatment)
1385 .forTable(BRIDGING_TABLE);
1386 if (fwd.permanent()) {
1387 flowRuleBuilder.makePermanent();
1388 } else {
1389 flowRuleBuilder.makeTemporary(fwd.timeout());
1390 }
1391 rules.add(flowRuleBuilder.build());
1392 return rules;
1393 }
1394
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001395 //////////////////////////////////////
1396 // Helper Methods and Classes
1397 //////////////////////////////////////
1398
1399 private boolean isSupportedEthTypeObjective(ForwardingObjective fwd) {
1400 TrafficSelector selector = fwd.selector();
1401 EthTypeCriterion ethType = (EthTypeCriterion) selector
1402 .getCriterion(Criterion.Type.ETH_TYPE);
1403 return !((ethType == null) ||
1404 ((ethType.ethType().toShort() != Ethernet.TYPE_IPV4) &&
Pier Ventree0ae7a32016-11-23 09:57:42 -08001405 (ethType.ethType().toShort() != Ethernet.MPLS_UNICAST)) &&
1406 (ethType.ethType().toShort() != Ethernet.TYPE_IPV6));
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001407 }
1408
1409 private boolean isSupportedEthDstObjective(ForwardingObjective fwd) {
1410 TrafficSelector selector = fwd.selector();
1411 EthCriterion ethDst = (EthCriterion) selector
1412 .getCriterion(Criterion.Type.ETH_DST);
1413 VlanIdCriterion vlanId = (VlanIdCriterion) selector
1414 .getCriterion(Criterion.Type.VLAN_VID);
1415 return !(ethDst == null && vlanId == null);
1416 }
1417
Saurav Das423fe2b2015-12-04 10:52:59 -08001418 protected NextGroup getGroupForNextObjective(Integer nextId) {
1419 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
1420 if (next != null) {
1421 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1422 if (gkeys != null && !gkeys.isEmpty()) {
1423 return next;
1424 } else {
1425 log.warn("Empty next group found in FlowObjective store for "
1426 + "next-id:{} in dev:{}", nextId, deviceId);
1427 }
1428 } else {
1429 log.warn("next-id {} not found in Flow objective store for dev:{}",
1430 nextId, deviceId);
1431 }
1432 return null;
1433 }
1434
Charles Chan188ebf52015-12-23 00:15:11 -08001435 protected static void pass(Objective obj) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001436 obj.context().ifPresent(context -> context.onSuccess(obj));
Saurav Das822c4e22015-10-23 10:51:11 -07001437 }
1438
Charles Chan188ebf52015-12-23 00:15:11 -08001439 protected static void fail(Objective obj, ObjectiveError error) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001440 obj.context().ifPresent(context -> context.onError(obj, error));
Saurav Das822c4e22015-10-23 10:51:11 -07001441 }
Saurav Das24431192016-03-07 19:13:00 -08001442
Saurav Das24431192016-03-07 19:13:00 -08001443 @Override
1444 public List<String> getNextMappings(NextGroup nextGroup) {
1445 List<String> mappings = new ArrayList<>();
1446 List<Deque<GroupKey>> gkeys = appKryo.deserialize(nextGroup.data());
1447 for (Deque<GroupKey> gkd : gkeys) {
1448 Group lastGroup = null;
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -07001449 StringBuilder gchain = new StringBuilder();
Saurav Das24431192016-03-07 19:13:00 -08001450 for (GroupKey gk : gkd) {
1451 Group g = groupService.getGroup(deviceId, gk);
Saurav Das8be4e3a2016-03-11 17:19:07 -08001452 if (g == null) {
Saurav Das25190812016-05-27 13:54:07 -07001453 gchain.append(" NoGrp").append(" -->");
Saurav Das8be4e3a2016-03-11 17:19:07 -08001454 continue;
1455 }
1456 gchain.append(" 0x").append(Integer.toHexString(g.id().id()))
1457 .append(" -->");
Saurav Das24431192016-03-07 19:13:00 -08001458 lastGroup = g;
1459 }
1460 // add port information for last group in group-chain
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -07001461 List<Instruction> lastGroupIns = new ArrayList<>();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001462 if (lastGroup != null && !lastGroup.buckets().buckets().isEmpty()) {
Saurav Das25190812016-05-27 13:54:07 -07001463 lastGroupIns = lastGroup.buckets().buckets().get(0)
1464 .treatment().allInstructions();
1465 }
1466 for (Instruction i: lastGroupIns) {
Saurav Das24431192016-03-07 19:13:00 -08001467 if (i instanceof OutputInstruction) {
Saurav Das8be4e3a2016-03-11 17:19:07 -08001468 gchain.append(" port:").append(((OutputInstruction) i).port());
Saurav Das24431192016-03-07 19:13:00 -08001469 }
1470 }
Saurav Das8be4e3a2016-03-11 17:19:07 -08001471 mappings.add(gchain.toString());
Saurav Das24431192016-03-07 19:13:00 -08001472 }
1473 return mappings;
1474 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001475
Pier Ventre140a8942016-11-02 07:26:38 -07001476 static boolean isMplsBos(TrafficSelector selector) {
1477 MplsBosCriterion bosCriterion = (MplsBosCriterion) selector.getCriterion(MPLS_BOS);
1478 return bosCriterion != null && bosCriterion.mplsBos();
1479 }
1480
1481 static boolean isNotMplsBos(TrafficSelector selector) {
1482 MplsBosCriterion bosCriterion = (MplsBosCriterion) selector.getCriterion(MPLS_BOS);
1483 return bosCriterion != null && !bosCriterion.mplsBos();
1484 }
1485
Charles Chan5b9df8d2016-03-28 22:21:40 -07001486 protected static VlanId readVlanFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001487 if (selector == null) {
1488 return null;
1489 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001490 Criterion criterion = selector.getCriterion(Criterion.Type.VLAN_VID);
1491 return (criterion == null)
1492 ? null : ((VlanIdCriterion) criterion).vlanId();
1493 }
1494
1495 protected static IpPrefix readIpDstFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001496 if (selector == null) {
1497 return null;
1498 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001499 Criterion criterion = selector.getCriterion(Criterion.Type.IPV4_DST);
1500 return (criterion == null) ? null : ((IPCriterion) criterion).ip();
1501 }
Charles Chand55e84d2016-03-30 17:54:24 -07001502
1503 private static VlanId readVlanFromTreatment(TrafficTreatment treatment) {
Saurav Das59232cf2016-04-27 18:35:50 -07001504 if (treatment == null) {
1505 return null;
1506 }
Charles Chand55e84d2016-03-30 17:54:24 -07001507 for (Instruction i : treatment.allInstructions()) {
1508 if (i instanceof ModVlanIdInstruction) {
1509 return ((ModVlanIdInstruction) i).vlanId();
1510 }
1511 }
1512 return null;
1513 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001514
1515 /**
1516 * Utility class that retries sending flows a fixed number of times, even if
1517 * some of the attempts are successful. Used only for forwarding objectives.
1518 */
1519 protected final class RetryFlows implements Runnable {
1520 int attempts = MAX_RETRY_ATTEMPTS;
1521 private Collection<FlowRule> retryFlows;
1522 private ForwardingObjective fwd;
1523
1524 RetryFlows(ForwardingObjective fwd, Collection<FlowRule> retryFlows) {
1525 this.fwd = fwd;
1526 this.retryFlows = retryFlows;
1527 }
1528
1529 @Override
1530 public void run() {
1531 log.info("RETRY FLOWS ATTEMPT# {} for fwd:{} rules:{}",
1532 MAX_RETRY_ATTEMPTS - attempts, fwd.id(), retryFlows.size());
1533 sendForward(fwd, retryFlows);
1534 if (--attempts > 0) {
1535 executorService.schedule(this, RETRY_MS, TimeUnit.MILLISECONDS);
1536 }
1537 }
1538 }
1539
Saurav Das822c4e22015-10-23 10:51:11 -07001540}