blob: 9ba942d24baf24c2a86ccefe8ca9f7d0ad2ce8b4 [file] [log] [blame]
Charles Chanf9e98652016-09-07 16:54:23 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Charles Chanf9e98652016-09-07 16:54:23 -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 */
16
Yi Tsengef19de12017-04-24 11:33:05 -070017package org.onosproject.driver.pipeline.ofdpa;
Charles Chanf9e98652016-09-07 16:54:23 -070018
Pier Ventre42287df2016-11-09 14:17:26 -080019import com.google.common.collect.ImmutableList;
Jonghwan Hyun800d9d02018-04-09 09:40:50 -070020import org.onlab.packet.MacAddress;
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -070021import org.onlab.packet.VlanId;
Charles Chanf9e98652016-09-07 16:54:23 -070022import org.onosproject.core.ApplicationId;
Pier Ventre42287df2016-11-09 14:17:26 -080023import org.onosproject.driver.extensions.Ofdpa3MatchMplsL2Port;
24import org.onosproject.driver.extensions.Ofdpa3MatchOvid;
Pier Ventre70d53ba2016-11-17 22:26:29 -080025import org.onosproject.driver.extensions.Ofdpa3PopCw;
26import org.onosproject.driver.extensions.Ofdpa3PopL2Header;
Pier Ventre42287df2016-11-09 14:17:26 -080027import org.onosproject.driver.extensions.Ofdpa3SetMplsL2Port;
28import org.onosproject.driver.extensions.Ofdpa3SetMplsType;
29import org.onosproject.driver.extensions.Ofdpa3SetOvid;
30import org.onosproject.driver.extensions.Ofdpa3SetQosIndex;
Jonghwan Hyun800d9d02018-04-09 09:40:50 -070031import org.onosproject.driver.extensions.OfdpaMatchVlanVid;
32import org.onosproject.net.PortNumber;
Pier Ventre42287df2016-11-09 14:17:26 -080033import org.onosproject.net.behaviour.NextGroup;
Charles Chanf9e98652016-09-07 16:54:23 -070034import org.onosproject.net.behaviour.PipelinerContext;
Pier Ventre42287df2016-11-09 14:17:26 -080035import org.onosproject.net.flow.DefaultFlowRule;
36import org.onosproject.net.flow.DefaultTrafficSelector;
37import org.onosproject.net.flow.DefaultTrafficTreatment;
Charles Chanf9e98652016-09-07 16:54:23 -070038import org.onosproject.net.flow.FlowRule;
Pier Ventre42287df2016-11-09 14:17:26 -080039import org.onosproject.net.flow.FlowRuleOperations;
40import org.onosproject.net.flow.FlowRuleOperationsContext;
Andreas Pantelopoulos2fef3112018-06-28 17:06:14 -070041import org.onosproject.net.flow.IndexTableId;
Pier Ventre42287df2016-11-09 14:17:26 -080042import org.onosproject.net.flow.TrafficSelector;
43import org.onosproject.net.flow.TrafficTreatment;
Jonghwan Hyun800d9d02018-04-09 09:40:50 -070044import org.onosproject.net.flow.criteria.Criteria;
Pier Ventre42287df2016-11-09 14:17:26 -080045import org.onosproject.net.flow.criteria.Criterion;
Jonghwan Hyun800d9d02018-04-09 09:40:50 -070046import org.onosproject.net.flow.criteria.EthCriterion;
Charles Chanf9e98652016-09-07 16:54:23 -070047import org.onosproject.net.flow.criteria.PortCriterion;
Pier Ventre42287df2016-11-09 14:17:26 -080048import org.onosproject.net.flow.criteria.TunnelIdCriterion;
Charles Chanf9e98652016-09-07 16:54:23 -070049import org.onosproject.net.flow.criteria.VlanIdCriterion;
Pier Ventre70d53ba2016-11-17 22:26:29 -080050import org.onosproject.net.flow.instructions.Instruction;
Andreas Pantelopoulos2fef3112018-06-28 17:06:14 -070051import org.onosproject.net.flow.instructions.Instructions;
Pier Ventre70d53ba2016-11-17 22:26:29 -080052import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
Pier Ventre42287df2016-11-09 14:17:26 -080053import org.onosproject.net.flow.instructions.L2ModificationInstruction;
54import org.onosproject.net.flow.instructions.L2ModificationInstruction.L2SubType;
Pier Ventre70d53ba2016-11-17 22:26:29 -080055import org.onosproject.net.flow.instructions.L3ModificationInstruction;
Pier Ventre42287df2016-11-09 14:17:26 -080056import org.onosproject.net.flowobjective.FilteringObjective;
Charles Chanf9e98652016-09-07 16:54:23 -070057import org.onosproject.net.flowobjective.ForwardingObjective;
Pier Ventre42287df2016-11-09 14:17:26 -080058import org.onosproject.net.flowobjective.ObjectiveError;
59import org.onosproject.net.group.Group;
60import org.onosproject.net.group.GroupKey;
61import org.slf4j.Logger;
Charles Chanf9e98652016-09-07 16:54:23 -070062
63import java.util.Collection;
Pier Ventre42287df2016-11-09 14:17:26 -080064import java.util.Collections;
65import java.util.Deque;
Charles Chanf9e98652016-09-07 16:54:23 -070066import java.util.List;
67
Jonghwan Hyun800d9d02018-04-09 09:40:50 -070068import static org.onlab.packet.MacAddress.NONE;
pier9469f3e2019-04-17 17:05:08 +020069import static org.onosproject.driver.pipeline.ofdpa.OfdpaPipelineUtility.*;
Pier Ventre42287df2016-11-09 14:17:26 -080070import static org.onosproject.driver.extensions.Ofdpa3MplsType.VPWS;
Andrea Campanellad980c6d2018-04-30 11:48:55 +020071import static org.onosproject.net.flow.criteria.Criterion.Type.INNER_VLAN_VID;
72import static org.onosproject.net.flow.criteria.Criterion.Type.IN_PORT;
73import static org.onosproject.net.flow.criteria.Criterion.Type.TUNNEL_ID;
74import static org.onosproject.net.flow.criteria.Criterion.Type.VLAN_VID;
Pier Ventre42287df2016-11-09 14:17:26 -080075import static org.onosproject.net.flow.instructions.Instruction.Type.L2MODIFICATION;
76import static org.onosproject.net.flow.instructions.L2ModificationInstruction.ModTunnelIdInstruction;
77import static org.slf4j.LoggerFactory.getLogger;
78
Charles Chanf9e98652016-09-07 16:54:23 -070079/**
80 * Pipeliner for Broadcom OF-DPA 3.0 TTP.
81 */
82public class Ofdpa3Pipeline extends Ofdpa2Pipeline {
Pier Ventre42287df2016-11-09 14:17:26 -080083
84 private final Logger log = getLogger(getClass());
85
Charles Chanf9e98652016-09-07 16:54:23 -070086 @Override
Charles Chan40132b32017-01-22 00:19:37 -080087 protected void initDriverId() {
Charles Chanf9e98652016-09-07 16:54:23 -070088 driverId = coreService.registerApplication(
89 "org.onosproject.driver.Ofdpa3Pipeline");
Charles Chan40132b32017-01-22 00:19:37 -080090 }
Charles Chanf9e98652016-09-07 16:54:23 -070091
Charles Chan40132b32017-01-22 00:19:37 -080092 @Override
93 protected void initGroupHander(PipelinerContext context) {
pier836d7fe2020-03-20 11:00:38 +010094 // Terminate internal references
95 // We are terminating the references here
96 // because when the device is offline the apps
97 // are still sending flowobjectives
98 if (groupHandler != null) {
99 groupHandler.terminate();
100 }
Charles Chan40132b32017-01-22 00:19:37 -0800101 groupHandler = new Ofdpa3GroupHandler();
102 groupHandler.init(deviceId, context);
Charles Chanf9e98652016-09-07 16:54:23 -0700103 }
104
105 @Override
Charles Chand1172632017-03-15 17:33:09 -0700106 protected boolean requireVlanExtensions() {
107 return false;
108 }
109
110 @Override
Saurav Dasc568c342018-01-25 09:49:01 -0800111 protected boolean shouldRetry() {
112 return false;
113 }
114
115 @Override
Andrea Campanellad980c6d2018-04-30 11:48:55 +0200116 protected boolean supportsUnicastBlackHole() {
117 return true;
118 }
119
120 @Override
Pier Ventre42287df2016-11-09 14:17:26 -0800121 protected void processFilter(FilteringObjective filteringObjective,
122 boolean install,
123 ApplicationId applicationId) {
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700124
125 // Check if filter is intended for pseudowire
126 boolean isPw = isPseudowire(filteringObjective);
127
128 if (isPw) {
Pier Ventre42287df2016-11-09 14:17:26 -0800129 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
130 PortCriterion portCriterion;
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700131
132 VlanIdCriterion innerVlanIdCriterion = null;
133 for (Criterion criterion : filteringObjective.conditions()) {
134 if (criterion.type() == INNER_VLAN_VID) {
135 innerVlanIdCriterion = (VlanIdCriterion) criterion;
136 break;
137 }
138 }
139
Pier Ventre42287df2016-11-09 14:17:26 -0800140 VlanIdCriterion outerVlanIdCriterion = null;
141 // We extract the expected port criterion in the key.
142 portCriterion = (PortCriterion) filteringObjective.key();
143 // We extract the outer vlan id criterion.
144 for (Criterion criterion : filteringObjective.conditions()) {
145 if (criterion.type() == VLAN_VID) {
146 outerVlanIdCriterion = (VlanIdCriterion) criterion;
147 break;
148 }
149 }
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700150
Pier Ventre42287df2016-11-09 14:17:26 -0800151 // We extract the tunnel id.
152 long tunnelId;
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700153 VlanId egressVlan;
154
Pier Ventre42287df2016-11-09 14:17:26 -0800155 if (filteringObjective.meta() != null &&
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700156 filteringObjective.meta().allInstructions().size() != 2) {
Pier Ventre42287df2016-11-09 14:17:26 -0800157 log.warn("Bad filtering objective from app: {}. Not"
158 + "processing filtering objective", applicationId);
159 fail(filteringObjective, ObjectiveError.BADPARAMS);
160 return;
161 } else if (filteringObjective.meta() != null &&
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700162 filteringObjective.meta().allInstructions().size() == 2 &&
163 filteringObjective.meta().allInstructions().get(0).type() == L2MODIFICATION &&
164 filteringObjective.meta().allInstructions().get(1).type() == L2MODIFICATION) {
165
Pier Ventre42287df2016-11-09 14:17:26 -0800166 L2ModificationInstruction l2instruction = (L2ModificationInstruction)
167 filteringObjective.meta().allInstructions().get(0);
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700168
Pier Ventre42287df2016-11-09 14:17:26 -0800169 if (l2instruction.subtype() != L2SubType.TUNNEL_ID) {
170 log.warn("Bad filtering objective from app: {}. Not"
171 + "processing filtering objective", applicationId);
172 fail(filteringObjective, ObjectiveError.BADPARAMS);
173 return;
174 } else {
175 tunnelId = ((ModTunnelIdInstruction) l2instruction).tunnelId();
176 }
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700177
178 L2ModificationInstruction vlanInstruction = (L2ModificationInstruction)
179 filteringObjective.meta().allInstructions().get(1);
180
181 if (vlanInstruction.subtype() != L2SubType.VLAN_ID) {
182 log.warn("Bad filtering objective from app: {}. Not"
183 + "processing filtering objective", applicationId);
184 fail(filteringObjective, ObjectiveError.BADPARAMS);
185 return;
186 } else {
187 egressVlan = ((L2ModificationInstruction.ModVlanIdInstruction) vlanInstruction).vlanId();
188 }
Pier Ventre42287df2016-11-09 14:17:26 -0800189 } else {
190 log.warn("Bad filtering objective from app: {}. Not"
191 + "processing filtering objective", applicationId);
192 fail(filteringObjective, ObjectiveError.BADPARAMS);
193 return;
194 }
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700195
Pier Ventre42287df2016-11-09 14:17:26 -0800196 // Mpls tunnel ids according to the OFDPA manual have to be
197 // in the range [2^17-1, 2^16].
198 tunnelId = MPLS_TUNNEL_ID_BASE | tunnelId;
199 // Sanity check for the filtering objective.
200 if (portCriterion == null ||
201 outerVlanIdCriterion == null ||
202 tunnelId > MPLS_TUNNEL_ID_MAX) {
203 log.warn("Bad filtering objective from app: {}. Not"
204 + "processing filtering objective", applicationId);
205 fail(filteringObjective, ObjectiveError.BADPARAMS);
206 return;
207 }
208 // 0x0000XXXX is UNI interface.
Pier Ventre70d53ba2016-11-17 22:26:29 -0800209 if (portCriterion.port().toLong() > MPLS_UNI_PORT_MAX) {
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530210 log.error("Filtering Objective invalid logical port {}",
Pier Ventre42287df2016-11-09 14:17:26 -0800211 portCriterion.port().toLong());
212 fail(filteringObjective, ObjectiveError.BADPARAMS);
213 return;
214 }
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700215
Pier Ventre42287df2016-11-09 14:17:26 -0800216 // We create the flows.
217 List<FlowRule> pwRules = processPwFilter(portCriterion,
218 innerVlanIdCriterion,
219 outerVlanIdCriterion,
220 tunnelId,
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700221 applicationId,
222 egressVlan
Pier Ventre42287df2016-11-09 14:17:26 -0800223 );
224 // We tag the flow for adding or for removing.
225 for (FlowRule pwRule : pwRules) {
226 log.debug("adding filtering rule in VLAN tables: {} for dev: {}",
227 pwRule, deviceId);
228 ops = install ? ops.add(pwRule) : ops.remove(pwRule);
229 }
230 // We push the filtering rules for the pw.
231 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
232 @Override
233 public void onSuccess(FlowRuleOperations ops) {
234 log.info("Applied {} filtering rules in device {}",
235 ops.stages().get(0).size(), deviceId);
236 pass(filteringObjective);
237 }
238
239 @Override
240 public void onError(FlowRuleOperations ops) {
241 log.info("Failed to apply all filtering rules in dev {}", deviceId);
242 fail(filteringObjective, ObjectiveError.FLOWINSTALLATIONFAILED);
243 }
244 }));
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700245 } else if (isDoubleTagged(filteringObjective)) {
246 processDoubleTaggedFilter(filteringObjective, install, applicationId);
247 } else {
248 // If it is not a pseudo wire flow or double-tagged filter, we fall back
249 // to the OFDPA 2.0 pipeline.
250 super.processFilter(filteringObjective, install, applicationId);
251 }
252 }
Pier Ventre42287df2016-11-09 14:17:26 -0800253
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700254 /**
Andreas Pantelopoulos2fef3112018-06-28 17:06:14 -0700255 *
256 * @param meta The metadata instruction of this treatment
257 * @return True if we should remove both VLAN and Mac Termination flow entries.
258 * This is only used for double tagged hosts.
259 */
260 public boolean shouldRemoveDoubleTagged(Instruction meta) {
261
262 Instructions.MetadataInstruction metadataInstruction = (Instructions.MetadataInstruction) meta;
263 return ((metadataInstruction.metadata() & metadataInstruction.metadataMask()) == 1);
264 }
265
266 /**
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700267 * Configure filtering rules of outer and inner VLAN IDs, and a MAC address.
268 * Filtering happens in three tables (VLAN_TABLE, VLAN_1_TABLE, TMAC_TABLE).
269 *
270 * @param filteringObjective the filtering objective
271 * @param install true to add, false to remove
272 * @param applicationId for application programming this filter
273 */
274 private void processDoubleTaggedFilter(FilteringObjective filteringObjective,
275 boolean install,
276 ApplicationId applicationId) {
277 PortCriterion portCriterion = null;
278 EthCriterion ethCriterion = null;
279 VlanIdCriterion innervidCriterion = null;
280 VlanIdCriterion outerVidCriterion = null;
281 boolean popVlan = false;
Andreas Pantelopoulos2fef3112018-06-28 17:06:14 -0700282 boolean removeDoubleTagged = true;
283 if (filteringObjective.meta().writeMetadata() != null) {
284 removeDoubleTagged = shouldRemoveDoubleTagged(filteringObjective.meta().writeMetadata());
285 }
286 log.info("HERE , removeDoubleTagged {}", removeDoubleTagged);
287
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700288 TrafficTreatment meta = filteringObjective.meta();
289 if (!filteringObjective.key().equals(Criteria.dummy()) &&
290 filteringObjective.key().type() == Criterion.Type.IN_PORT) {
291 portCriterion = (PortCriterion) filteringObjective.key();
292 }
293 if (portCriterion == null) {
294 log.warn("No IN_PORT defined in filtering objective from app: {}" +
295 "Failed to program VLAN tables.", applicationId);
296 return;
297 } else {
298 log.debug("Received filtering objective for dev/port: {}/{}", deviceId,
299 portCriterion.port());
300 }
301
302 // meta should have only one instruction, popVlan.
303 if (meta != null && meta.allInstructions().size() == 1) {
304 L2ModificationInstruction l2Inst = (L2ModificationInstruction) meta.allInstructions().get(0);
305 if (l2Inst.subtype().equals(L2SubType.VLAN_POP)) {
306 popVlan = true;
307 } else {
308 log.warn("Filtering objective can have only VLAN_POP instruction.");
309 return;
310 }
311 } else {
312 log.warn("Filtering objective should have one instruction.");
Pier Ventre42287df2016-11-09 14:17:26 -0800313 return;
314 }
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700315
316 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
317 for (Criterion criterion : filteringObjective.conditions()) {
318 switch (criterion.type()) {
319 case ETH_DST:
320 case ETH_DST_MASKED:
321 ethCriterion = (EthCriterion) criterion;
322 break;
323 case VLAN_VID:
Daniele Morofa382c22019-07-12 17:58:54 -0700324 outerVidCriterion = (VlanIdCriterion) criterion;
325 break;
326 case INNER_VLAN_VID:
327 innervidCriterion = (VlanIdCriterion) criterion;
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700328 break;
329 default:
330 log.warn("Unsupported filter {}", criterion);
331 fail(filteringObjective, ObjectiveError.UNSUPPORTED);
332 return;
333 }
334 }
335
336 if (innervidCriterion == null || outerVidCriterion == null) {
337 log.warn("filtering objective should have two vidCriterion.");
338 return;
339 }
340
341 if (ethCriterion == null || ethCriterion.mac().equals(NONE)) {
342 // NOTE: it is possible that a filtering objective only has vidCriterion
343 log.warn("filtering objective missing dstMac, cannot program TMAC table");
344 return;
345 } else {
346 MacAddress unicastMac = readEthDstFromTreatment(filteringObjective.meta());
347 List<List<FlowRule>> allStages = processEthDstFilter(portCriterion, ethCriterion, innervidCriterion,
348 innervidCriterion.vlanId(), unicastMac,
349 applicationId);
350 for (List<FlowRule> flowRules : allStages) {
351 log.trace("Starting a new flow rule stage for TMAC table flow");
352 ops.newStage();
353
354 for (FlowRule flowRule : flowRules) {
355 log.trace("{} flow rules in TMAC table: {} for dev: {}",
356 (install) ? "adding" : "removing", flowRules, deviceId);
357 if (install) {
358 ops = ops.add(flowRule);
359 } else {
360 // NOTE: Only remove TMAC flow when there is no more enabled port within the
361 // same VLAN on this device if TMAC doesn't support matching on in_port.
362 if (matchInPortTmacTable()
363 || (filteringObjective.meta() != null
364 && filteringObjective.meta().clearedDeferred())) {
Andreas Pantelopoulos2fef3112018-06-28 17:06:14 -0700365
366 // if metadata instruction not null and not removeDoubleTagged move on.
367 if ((filteringObjective.meta().writeMetadata() != null) && (!removeDoubleTagged)) {
368 log.info("Skipping removal of tmac rule for device {}", deviceId);
369 continue;
370 } else {
371 ops = ops.remove(flowRule);
372 }
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700373 } else {
374 log.debug("Abort TMAC flow removal on {}. Some other ports still share this TMAC flow");
375 }
376 }
377 }
378 }
379 }
380
381 List<FlowRule> rules;
382 rules = processDoubleVlanIdFilter(portCriterion, innervidCriterion,
383 outerVidCriterion, popVlan, applicationId);
384 for (FlowRule flowRule : rules) {
385 log.trace("{} flow rule in VLAN table: {} for dev: {}",
386 (install) ? "adding" : "removing", flowRule, deviceId);
Andreas Pantelopoulos2fef3112018-06-28 17:06:14 -0700387 // if context is remove, table is vlan_1 and removeDoubleTagged is false, continue.
388 if (flowRule.table().equals(IndexTableId.of(VLAN_TABLE)) &&
389 !removeDoubleTagged && !install) {
390 log.info("Skipping removal of vlan table rule for now!");
391 continue;
392 }
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700393 ops = install ? ops.add(flowRule) : ops.remove(flowRule);
394 }
395
396 // apply filtering flow rules
397 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
398 @Override
399 public void onSuccess(FlowRuleOperations ops) {
400 log.debug("Applied {} filtering rules in device {}",
401 ops.stages().get(0).size(), deviceId);
402 pass(filteringObjective);
403 }
404
405 @Override
406 public void onError(FlowRuleOperations ops) {
407 log.info("Failed to apply all filtering rules in dev {}", deviceId);
408 fail(filteringObjective, ObjectiveError.FLOWINSTALLATIONFAILED);
409 }
410 }));
411
412 }
413 /**
414 * Internal implementation of processDoubleVlanIdFilter.
415 *
416 * @param portCriterion port on device for which this filter is programmed
417 * @param innerVidCriterion inner vlan
418 * @param outerVidCriterion outer vlan
419 * @param popVlan true if outer vlan header needs to be removed
420 * @param applicationId for application programming this filter
421 * @return flow rules for port-vlan filters
422 */
423 private List<FlowRule> processDoubleVlanIdFilter(PortCriterion portCriterion,
424 VlanIdCriterion innerVidCriterion,
425 VlanIdCriterion outerVidCriterion,
426 boolean popVlan,
427 ApplicationId applicationId) {
428 TrafficSelector.Builder outerSelector = DefaultTrafficSelector.builder();
429 TrafficTreatment.Builder outerTreatment = DefaultTrafficTreatment.builder();
430 TrafficSelector.Builder innerSelector = DefaultTrafficSelector.builder();
431 TrafficTreatment.Builder innerTreatment = DefaultTrafficTreatment.builder();
432
433 VlanId outerVlanId = outerVidCriterion.vlanId();
434 VlanId innerVlanId = innerVidCriterion.vlanId();
435 PortNumber portNumber = portCriterion.port();
436 // Check arguments
437 if (PortNumber.ALL.equals(portNumber)
438 || outerVlanId.equals(VlanId.NONE)
439 || innerVlanId.equals(VlanId.NONE)) {
440 log.warn("Incomplete Filtering Objective. " +
441 "VLAN Table cannot be programmed for {}", deviceId);
442 return ImmutableList.of();
443 } else {
444 outerSelector.matchInPort(portNumber);
445 innerSelector.matchInPort(portNumber);
446 outerTreatment.transition(VLAN_1_TABLE);
447 innerTreatment.transition(TMAC_TABLE);
448
449 if (requireVlanExtensions()) {
450 OfdpaMatchVlanVid ofdpaOuterMatchVlanVid = new OfdpaMatchVlanVid(outerVlanId);
451 outerSelector.extension(ofdpaOuterMatchVlanVid, deviceId);
452 OfdpaMatchVlanVid ofdpaInnerMatchVlanVid = new OfdpaMatchVlanVid(innerVlanId);
453 innerSelector.extension(ofdpaInnerMatchVlanVid, deviceId);
454 } else {
455 outerSelector.matchVlanId(outerVlanId);
456 innerSelector.matchVlanId(innerVlanId);
457 }
458
459 innerSelector.extension(new Ofdpa3MatchOvid(outerVlanId), deviceId);
460 outerTreatment.extension(new Ofdpa3SetOvid(outerVlanId), deviceId);
461 if (popVlan) {
462 outerTreatment.popVlan();
463 }
464 }
465 FlowRule outerRule = DefaultFlowRule.builder()
466 .forDevice(deviceId)
467 .withSelector(outerSelector.build())
468 .withTreatment(outerTreatment.build())
469 .withPriority(DEFAULT_PRIORITY)
470 .fromApp(applicationId)
471 .makePermanent()
472 .forTable(VLAN_TABLE)
473 .build();
474 FlowRule innerRule = DefaultFlowRule.builder()
475 .forDevice(deviceId)
476 .withSelector(innerSelector.build())
477 .withTreatment(innerTreatment.build())
478 .withPriority(DEFAULT_PRIORITY)
479 .fromApp(applicationId)
480 .makePermanent()
481 .forTable(VLAN_1_TABLE)
482 .build();
483
484 return ImmutableList.of(outerRule, innerRule);
485 }
486
487 /**
Pier Ventre42287df2016-11-09 14:17:26 -0800488 * Method to process the pw related filtering objectives.
489 *
490 * @param portCriterion the in port match
491 * @param innerVlanIdCriterion the inner vlan match
492 * @param outerVlanIdCriterion the outer vlan match
493 * @param tunnelId the tunnel id
494 * @param applicationId the application id
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700495 * @param egressVlan the vlan to modify, was passed in metadata
Pier Ventre42287df2016-11-09 14:17:26 -0800496 * @return a list of flow rules to install
497 */
498 private List<FlowRule> processPwFilter(PortCriterion portCriterion,
Yi Tsengef19de12017-04-24 11:33:05 -0700499 VlanIdCriterion innerVlanIdCriterion,
500 VlanIdCriterion outerVlanIdCriterion,
501 long tunnelId,
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700502 ApplicationId applicationId,
503 VlanId egressVlan) {
504
Pier Ventre42287df2016-11-09 14:17:26 -0800505 FlowRule vlan1FlowRule;
506 int mplsLogicalPort = ((int) portCriterion.port().toLong());
507 // We have to match on the inner vlan and outer vlan at the same time.
508 // Ofdpa supports this through the OVID meta-data type.
Pier Ventre42287df2016-11-09 14:17:26 -0800509
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700510 ImmutableList<FlowRule> toReturn;
511
512 // pseudowire configured with double tagged vlans
513 if (!(innerVlanIdCriterion.vlanId().equals(VlanId.NONE))
514 && !(outerVlanIdCriterion.vlanId().equals(VlanId.NONE))) {
515
516 log.info("Installing filter objective for double tagged CE for tunnel {}", tunnelId);
517
518 TrafficSelector.Builder vlan1Selector = DefaultTrafficSelector.builder()
519 .matchInPort(portCriterion.port())
520 .matchVlanId(innerVlanIdCriterion.vlanId())
521 .extension(new Ofdpa3MatchOvid(outerVlanIdCriterion.vlanId()), deviceId);
522 // TODO understand for the future how to manage the vlan rewrite.
523 TrafficTreatment.Builder vlan1Treatment = DefaultTrafficTreatment.builder()
524 .pushVlan()
525 .setVlanId(egressVlan)
526 .extension(new Ofdpa3SetMplsType(VPWS), deviceId)
527 .extension(new Ofdpa3SetMplsL2Port(mplsLogicalPort), deviceId)
528 .setTunnelId(tunnelId)
529 .transition(MPLS_L2_PORT_FLOW_TABLE);
530 vlan1FlowRule = DefaultFlowRule.builder()
531 .forDevice(deviceId)
532 .withSelector(vlan1Selector.build())
533 .withTreatment(vlan1Treatment.build())
534 .withPriority(DEFAULT_PRIORITY)
535 .fromApp(applicationId)
536 .makePermanent()
537 .forTable(VLAN_1_TABLE)
538 .build();
539 // Finally we create the flow rule for the vlan table.
540 FlowRule vlanFlowRule;
541 // We have to match on the outer vlan.
542 TrafficSelector.Builder vlanSelector = DefaultTrafficSelector.builder()
543 .matchInPort(portCriterion.port())
544 .matchVlanId(outerVlanIdCriterion.vlanId());
545 // TODO understand for the future how to manage the vlan rewrite.
546 TrafficTreatment.Builder vlanTreatment = DefaultTrafficTreatment.builder()
547 .popVlan()
548 .extension(new Ofdpa3SetOvid(outerVlanIdCriterion.vlanId()), deviceId)
549 .transition(VLAN_1_TABLE);
550 vlanFlowRule = DefaultFlowRule.builder()
551 .forDevice(deviceId)
552 .withSelector(vlanSelector.build())
553 .withTreatment(vlanTreatment.build())
554 .withPriority(DEFAULT_PRIORITY)
555 .fromApp(applicationId)
556 .makePermanent()
557 .forTable(VLAN_TABLE)
558 .build();
559
560 return ImmutableList.of(vlan1FlowRule, vlanFlowRule);
561 } else if (!(innerVlanIdCriterion.vlanId().equals(VlanId.NONE))
562 && (outerVlanIdCriterion.vlanId().equals(VlanId.NONE))) {
563
564 log.info("Installing filter objective for single tagged CE for tunnel {}", tunnelId);
565
566 TrafficSelector.Builder singleVlanSelector = DefaultTrafficSelector.builder()
567 .matchInPort(portCriterion.port())
568 .matchVlanId(innerVlanIdCriterion.vlanId());
569
570 TrafficTreatment.Builder singleVlanTreatment = DefaultTrafficTreatment.builder()
Charles Chanf45552d2018-07-13 18:08:33 -0700571 // FIXME PW VLAN translation is not supported on Dune
572 // Need to explore doing that in egress table later if there is a requirement
573 // .setVlanId(egressVlan)
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700574 .extension(new Ofdpa3SetMplsType(VPWS), deviceId)
575 .extension(new Ofdpa3SetMplsL2Port(mplsLogicalPort), deviceId)
576 .setTunnelId(tunnelId)
577 .transition(MPLS_L2_PORT_FLOW_TABLE);
578
579 vlan1FlowRule = DefaultFlowRule.builder()
580 .forDevice(deviceId)
581 .withSelector(singleVlanSelector.build())
582 .withTreatment(singleVlanTreatment.build())
583 .withPriority(DEFAULT_PRIORITY)
584 .fromApp(applicationId)
585 .makePermanent()
586 .forTable(VLAN_TABLE)
587 .build();
588
589 return ImmutableList.of(vlan1FlowRule);
590 } else if ((innerVlanIdCriterion.vlanId().equals(VlanId.NONE))
591 && (outerVlanIdCriterion.vlanId().equals(VlanId.NONE))) {
592
593 TrafficSelector.Builder singleVlanSelector = DefaultTrafficSelector.builder()
594 .matchInPort(portCriterion.port())
595 .matchVlanId(innerVlanIdCriterion.vlanId());
596
597 TrafficTreatment.Builder singleVlanTreatment = DefaultTrafficTreatment.builder()
598 .extension(new Ofdpa3SetMplsType(VPWS), deviceId)
599 .extension(new Ofdpa3SetMplsL2Port(mplsLogicalPort), deviceId)
600 .setTunnelId(tunnelId)
601 .transition(MPLS_L2_PORT_FLOW_TABLE);
602
603 vlan1FlowRule = DefaultFlowRule.builder()
604 .forDevice(deviceId)
605 .withSelector(singleVlanSelector.build())
606 .withTreatment(singleVlanTreatment.build())
607 .withPriority(DEFAULT_PRIORITY)
608 .fromApp(applicationId)
609 .makePermanent()
610 .forTable(VLAN_TABLE)
611 .build();
612
613 return ImmutableList.of(vlan1FlowRule);
614 } else {
615 // failure...
616 return Collections.emptyList();
617 }
Pier Ventre42287df2016-11-09 14:17:26 -0800618 }
619
620 @Override
Charles Chanf9e98652016-09-07 16:54:23 -0700621 protected Collection<FlowRule> processEthTypeSpecific(ForwardingObjective fwd) {
Saurav Dasa4020382018-02-14 14:14:54 -0800622 // if its not-bos, we go to MPLS_TYPE_TABLE regardless of whether we pop or swap
623 // if it is bos, we go to MPLS_TYPE_TABLE only if we swap
624 if (isNotMplsBos(fwd.selector())
625 || (isMplsBos(fwd.selector()) && !isMplsPop(fwd))) {
Pier Ventre140a8942016-11-02 07:26:38 -0700626 return processEthTypeSpecificInternal(fwd, true, MPLS_TYPE_TABLE);
627 }
Saurav Dasa4020382018-02-14 14:14:54 -0800628 // if it is bos, and we pop, we go to MPLS_L3_TYPE_TABLE
Pier Ventre140a8942016-11-02 07:26:38 -0700629 return processEthTypeSpecificInternal(fwd, true, MPLS_L3_TYPE_TABLE);
Charles Chanf9e98652016-09-07 16:54:23 -0700630 }
Pier Ventre42287df2016-11-09 14:17:26 -0800631
632 @Override
633 protected Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
634 // We use the tunnel id to identify pw related flows.
Pier Ventre70d53ba2016-11-17 22:26:29 -0800635 // Looking for the fwd objective of the initiation.
Pier Ventre42287df2016-11-09 14:17:26 -0800636 TunnelIdCriterion tunnelIdCriterion = (TunnelIdCriterion) fwd.selector()
637 .getCriterion(TUNNEL_ID);
638 if (tunnelIdCriterion != null) {
Pier Ventre70d53ba2016-11-17 22:26:29 -0800639 return processInitPwVersatile(fwd);
640 }
641 // Looking for the fwd objective of the termination.
642 ModTunnelIdInstruction modTunnelIdInstruction = getModTunnelIdInstruction(fwd.treatment());
643 OutputInstruction outputInstruction = getOutputInstruction(fwd.treatment());
644 if (modTunnelIdInstruction != null && outputInstruction != null) {
645 return processTermPwVersatile(fwd, modTunnelIdInstruction, outputInstruction);
Pier Ventre42287df2016-11-09 14:17:26 -0800646 }
647 // If it is not a pseudo wire flow we fall back
648 // to the OFDPA 2.0 pipeline.
649 return super.processVersatile(fwd);
650 }
651
Pier Ventre70d53ba2016-11-17 22:26:29 -0800652 private Collection<FlowRule> processTermPwVersatile(ForwardingObjective forwardingObjective,
653 ModTunnelIdInstruction modTunnelIdInstruction,
654 OutputInstruction outputInstruction) {
655 TrafficTreatment.Builder flowTreatment;
656 TrafficSelector.Builder flowSelector;
657 // We divide the mpls actions from the tunnel actions. We need
658 // this to order the actions in the final treatment.
659 TrafficTreatment.Builder mplsTreatment = DefaultTrafficTreatment.builder();
660 createMplsTreatment(forwardingObjective.treatment(), mplsTreatment);
661 // The match of the forwarding objective is ready to go.
662 flowSelector = DefaultTrafficSelector.builder(forwardingObjective.selector());
663 // We verify the tunnel id and mpls port are correct.
664 long tunnelId = MPLS_TUNNEL_ID_BASE | modTunnelIdInstruction.tunnelId();
665 if (tunnelId > MPLS_TUNNEL_ID_MAX) {
666 log.error("Pw Versatile Forwarding Objective must include tunnel id < {}",
667 MPLS_TUNNEL_ID_MAX);
668 fail(forwardingObjective, ObjectiveError.BADPARAMS);
669 return Collections.emptySet();
670 }
671 // 0x0002XXXX is NNI interface.
672 int mplsLogicalPort = ((int) outputInstruction.port().toLong()) | MPLS_NNI_PORT_BASE;
673 if (mplsLogicalPort > MPLS_NNI_PORT_MAX) {
674 log.error("Pw Versatile Forwarding Objective invalid logical port {}",
675 mplsLogicalPort);
676 fail(forwardingObjective, ObjectiveError.BADPARAMS);
677 return Collections.emptySet();
678 }
679 // Next id cannot be null.
680 if (forwardingObjective.nextId() == null) {
681 log.error("Pw Versatile Forwarding Objective must contain nextId ",
682 forwardingObjective.nextId());
683 fail(forwardingObjective, ObjectiveError.BADPARAMS);
684 return Collections.emptySet();
685 }
686 // We retrieve the l2 interface group and point the mpls
687 // flow to this.
688 NextGroup next = getGroupForNextObjective(forwardingObjective.nextId());
689 if (next == null) {
690 log.warn("next-id:{} not found in dev:{}", forwardingObjective.nextId(), deviceId);
691 fail(forwardingObjective, ObjectiveError.GROUPMISSING);
692 return Collections.emptySet();
693 }
694 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
695 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
696 if (group == null) {
697 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
698 gkeys.get(0).peekFirst(), forwardingObjective.nextId(), deviceId);
699 fail(forwardingObjective, ObjectiveError.GROUPMISSING);
700 return Collections.emptySet();
701 }
702 // We prepare the treatment for the mpls flow table.
703 // The order of the actions has to be strictly this
704 // according to the OFDPA 2.0 specification.
705 flowTreatment = DefaultTrafficTreatment.builder(mplsTreatment.build());
706 flowTreatment.extension(new Ofdpa3PopCw(), deviceId);
Pier Luigi3bfe32c2017-01-30 09:47:36 -0800707 // Even though the specification and the xml/json files
708 // specify is allowed, the switch rejects the flow. In the
709 // OFDPA 3.0 EA0 version was necessary
710 //flowTreatment.popVlan();
Pier Ventre70d53ba2016-11-17 22:26:29 -0800711 flowTreatment.extension(new Ofdpa3PopL2Header(), deviceId);
712 flowTreatment.setTunnelId(tunnelId);
713 flowTreatment.extension(new Ofdpa3SetMplsL2Port(mplsLogicalPort), deviceId);
714 flowTreatment.extension(new Ofdpa3SetMplsType(VPWS), deviceId);
715 flowTreatment.transition(MPLS_TYPE_TABLE);
716 flowTreatment.deferred().group(group.id());
717 // We prepare the flow rule for the mpls table.
718 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
719 .fromApp(forwardingObjective.appId())
720 .withPriority(forwardingObjective.priority())
721 .forDevice(deviceId)
722 .withSelector(flowSelector.build())
723 .withTreatment(flowTreatment.build())
724 .makePermanent()
725 .forTable(MPLS_TABLE_1);
726 return Collections.singletonList(ruleBuilder.build());
727 }
728
Pier Ventre42287df2016-11-09 14:17:26 -0800729 /**
730 * Helper method to process the pw forwarding objectives.
731 *
732 * @param forwardingObjective the fw objective to process
733 * @return a singleton list of flow rule
734 */
Pier Ventre70d53ba2016-11-17 22:26:29 -0800735 private Collection<FlowRule> processInitPwVersatile(ForwardingObjective forwardingObjective) {
Pier Ventre42287df2016-11-09 14:17:26 -0800736 // We retrieve the matching criteria for mpls l2 port.
737 TunnelIdCriterion tunnelIdCriterion = (TunnelIdCriterion) forwardingObjective.selector()
738 .getCriterion(TUNNEL_ID);
739 PortCriterion portCriterion = (PortCriterion) forwardingObjective.selector()
740 .getCriterion(IN_PORT);
741 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
742 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
743 int mplsLogicalPort;
744 long tunnelId;
745 // Mpls tunnel ids according to the OFDPA manual have to be
746 // in the range [2^17-1, 2^16].
747 tunnelId = MPLS_TUNNEL_ID_BASE | tunnelIdCriterion.tunnelId();
748 if (tunnelId > MPLS_TUNNEL_ID_MAX) {
749 log.error("Pw Versatile Forwarding Objective must include tunnel id < {}",
750 MPLS_TUNNEL_ID_MAX);
751 fail(forwardingObjective, ObjectiveError.BADPARAMS);
752 return Collections.emptySet();
753 }
754 // Port has not been null.
755 if (portCriterion == null) {
756 log.error("Pw Versatile Forwarding Objective must include port");
757 fail(forwardingObjective, ObjectiveError.BADPARAMS);
758 return Collections.emptySet();
759 }
760 // 0x0000XXXX is UNI interface.
Pier Ventre70d53ba2016-11-17 22:26:29 -0800761 if (portCriterion.port().toLong() > MPLS_UNI_PORT_MAX) {
Pier Ventre42287df2016-11-09 14:17:26 -0800762 log.error("Pw Versatile Forwarding Objective invalid logical port {}",
763 portCriterion.port().toLong());
764 fail(forwardingObjective, ObjectiveError.BADPARAMS);
765 return Collections.emptySet();
766 }
767 mplsLogicalPort = ((int) portCriterion.port().toLong());
768 if (forwardingObjective.nextId() == null) {
769 log.error("Pw Versatile Forwarding Objective must contain nextId ",
770 forwardingObjective.nextId());
771 fail(forwardingObjective, ObjectiveError.BADPARAMS);
772 return Collections.emptySet();
773 }
774 // We don't expect a treatment.
775 if (forwardingObjective.treatment() != null &&
776 !forwardingObjective.treatment().equals(DefaultTrafficTreatment.emptyTreatment())) {
777 log.error("Pw Versatile Forwarding Objective cannot contain a treatment ",
778 forwardingObjective.nextId());
779 fail(forwardingObjective, ObjectiveError.BADPARAMS);
780 return Collections.emptySet();
781 }
782 // We retrieve the l2 vpn group and point the mpls
783 // l2 port to this.
784 NextGroup next = getGroupForNextObjective(forwardingObjective.nextId());
785 if (next == null) {
786 log.warn("next-id:{} not found in dev:{}", forwardingObjective.nextId(), deviceId);
787 fail(forwardingObjective, ObjectiveError.GROUPMISSING);
788 return Collections.emptySet();
789 }
790 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
791 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
792 if (group == null) {
793 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
794 gkeys.get(0).peekFirst(), forwardingObjective.nextId(), deviceId);
795 fail(forwardingObjective, ObjectiveError.GROUPMISSING);
796 return Collections.emptySet();
797 }
798 // We prepare the flow rule for the mpls l2 port table.
799 selector.matchTunnelId(tunnelId);
800 selector.extension(new Ofdpa3MatchMplsL2Port(mplsLogicalPort), deviceId);
801 // This should not be necessary but without we receive an error
802 treatment.extension(new Ofdpa3SetQosIndex(0), deviceId);
803 treatment.transition(MPLS_L2_PORT_PCP_TRUST_FLOW_TABLE);
804 treatment.deferred().group(group.id());
805 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
806 .fromApp(forwardingObjective.appId())
807 .withPriority(MPLS_L2_PORT_PRIORITY)
808 .forDevice(deviceId)
809 .withSelector(selector.build())
810 .withTreatment(treatment.build())
811 .makePermanent()
812 .forTable(MPLS_L2_PORT_FLOW_TABLE);
813 return Collections.singletonList(ruleBuilder.build());
814 }
Pier Ventre70d53ba2016-11-17 22:26:29 -0800815
816 /**
Pier Ventre70d53ba2016-11-17 22:26:29 -0800817 * Helper method for dividing the tunnel instructions from the mpls
818 * instructions.
819 *
820 * @param treatment the treatment to analyze
821 * @param mplsTreatment the mpls treatment builder
822 */
823 private void createMplsTreatment(TrafficTreatment treatment,
824 TrafficTreatment.Builder mplsTreatment) {
825
826 for (Instruction ins : treatment.allInstructions()) {
827
828 if (ins.type() == Instruction.Type.L2MODIFICATION) {
829 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
830 switch (l2ins.subtype()) {
831 // These instructions have to go in the mpls
832 // treatment.
833 case TUNNEL_ID:
834 break;
835 case DEC_MPLS_TTL:
836 case MPLS_POP:
837 mplsTreatment.add(ins);
838 break;
839 default:
840 log.warn("Driver does not handle this type of TrafficTreatment"
841 + " instruction in nextObjectives: {} - {}",
842 ins.type(), ins);
843 break;
844 }
845 } else if (ins.type() == Instruction.Type.OUTPUT) {
846 break;
847 } else if (ins.type() == Instruction.Type.L3MODIFICATION) {
848 // We support partially the l3 instructions.
849 L3ModificationInstruction l3ins = (L3ModificationInstruction) ins;
850 switch (l3ins.subtype()) {
851 case TTL_IN:
852 mplsTreatment.add(ins);
853 break;
854 default:
855 log.warn("Driver does not handle this type of TrafficTreatment"
856 + " instruction in nextObjectives: {} - {}",
857 ins.type(), ins);
858 }
859
860 } else {
861 log.warn("Driver does not handle this type of TrafficTreatment"
862 + " instruction in nextObjectives: {} - {}",
863 ins.type(), ins);
864 }
865 }
866 }
pier9469f3e2019-04-17 17:05:08 +0200867
Charles Chanf9e98652016-09-07 16:54:23 -0700868}