blob: 2ab8a18a787a18787132dbb4511714a3a70b3440 [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) {
94 groupHandler = new Ofdpa3GroupHandler();
95 groupHandler.init(deviceId, context);
Charles Chanf9e98652016-09-07 16:54:23 -070096 }
97
98 @Override
Charles Chand1172632017-03-15 17:33:09 -070099 protected boolean requireVlanExtensions() {
100 return false;
101 }
102
103 @Override
Saurav Dasc568c342018-01-25 09:49:01 -0800104 protected boolean shouldRetry() {
105 return false;
106 }
107
108 @Override
Andrea Campanellad980c6d2018-04-30 11:48:55 +0200109 protected boolean supportsUnicastBlackHole() {
110 return true;
111 }
112
113 @Override
Pier Ventre42287df2016-11-09 14:17:26 -0800114 protected void processFilter(FilteringObjective filteringObjective,
115 boolean install,
116 ApplicationId applicationId) {
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700117
118 // Check if filter is intended for pseudowire
119 boolean isPw = isPseudowire(filteringObjective);
120
121 if (isPw) {
Pier Ventre42287df2016-11-09 14:17:26 -0800122 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
123 PortCriterion portCriterion;
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700124
125 VlanIdCriterion innerVlanIdCriterion = null;
126 for (Criterion criterion : filteringObjective.conditions()) {
127 if (criterion.type() == INNER_VLAN_VID) {
128 innerVlanIdCriterion = (VlanIdCriterion) criterion;
129 break;
130 }
131 }
132
Pier Ventre42287df2016-11-09 14:17:26 -0800133 VlanIdCriterion outerVlanIdCriterion = null;
134 // We extract the expected port criterion in the key.
135 portCriterion = (PortCriterion) filteringObjective.key();
136 // We extract the outer vlan id criterion.
137 for (Criterion criterion : filteringObjective.conditions()) {
138 if (criterion.type() == VLAN_VID) {
139 outerVlanIdCriterion = (VlanIdCriterion) criterion;
140 break;
141 }
142 }
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700143
Pier Ventre42287df2016-11-09 14:17:26 -0800144 // We extract the tunnel id.
145 long tunnelId;
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700146 VlanId egressVlan;
147
Pier Ventre42287df2016-11-09 14:17:26 -0800148 if (filteringObjective.meta() != null &&
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700149 filteringObjective.meta().allInstructions().size() != 2) {
Pier Ventre42287df2016-11-09 14:17:26 -0800150 log.warn("Bad filtering objective from app: {}. Not"
151 + "processing filtering objective", applicationId);
152 fail(filteringObjective, ObjectiveError.BADPARAMS);
153 return;
154 } else if (filteringObjective.meta() != null &&
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700155 filteringObjective.meta().allInstructions().size() == 2 &&
156 filteringObjective.meta().allInstructions().get(0).type() == L2MODIFICATION &&
157 filteringObjective.meta().allInstructions().get(1).type() == L2MODIFICATION) {
158
Pier Ventre42287df2016-11-09 14:17:26 -0800159 L2ModificationInstruction l2instruction = (L2ModificationInstruction)
160 filteringObjective.meta().allInstructions().get(0);
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700161
Pier Ventre42287df2016-11-09 14:17:26 -0800162 if (l2instruction.subtype() != L2SubType.TUNNEL_ID) {
163 log.warn("Bad filtering objective from app: {}. Not"
164 + "processing filtering objective", applicationId);
165 fail(filteringObjective, ObjectiveError.BADPARAMS);
166 return;
167 } else {
168 tunnelId = ((ModTunnelIdInstruction) l2instruction).tunnelId();
169 }
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700170
171 L2ModificationInstruction vlanInstruction = (L2ModificationInstruction)
172 filteringObjective.meta().allInstructions().get(1);
173
174 if (vlanInstruction.subtype() != L2SubType.VLAN_ID) {
175 log.warn("Bad filtering objective from app: {}. Not"
176 + "processing filtering objective", applicationId);
177 fail(filteringObjective, ObjectiveError.BADPARAMS);
178 return;
179 } else {
180 egressVlan = ((L2ModificationInstruction.ModVlanIdInstruction) vlanInstruction).vlanId();
181 }
Pier Ventre42287df2016-11-09 14:17:26 -0800182 } else {
183 log.warn("Bad filtering objective from app: {}. Not"
184 + "processing filtering objective", applicationId);
185 fail(filteringObjective, ObjectiveError.BADPARAMS);
186 return;
187 }
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700188
Pier Ventre42287df2016-11-09 14:17:26 -0800189 // Mpls tunnel ids according to the OFDPA manual have to be
190 // in the range [2^17-1, 2^16].
191 tunnelId = MPLS_TUNNEL_ID_BASE | tunnelId;
192 // Sanity check for the filtering objective.
193 if (portCriterion == null ||
194 outerVlanIdCriterion == null ||
195 tunnelId > MPLS_TUNNEL_ID_MAX) {
196 log.warn("Bad filtering objective from app: {}. Not"
197 + "processing filtering objective", applicationId);
198 fail(filteringObjective, ObjectiveError.BADPARAMS);
199 return;
200 }
201 // 0x0000XXXX is UNI interface.
Pier Ventre70d53ba2016-11-17 22:26:29 -0800202 if (portCriterion.port().toLong() > MPLS_UNI_PORT_MAX) {
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530203 log.error("Filtering Objective invalid logical port {}",
Pier Ventre42287df2016-11-09 14:17:26 -0800204 portCriterion.port().toLong());
205 fail(filteringObjective, ObjectiveError.BADPARAMS);
206 return;
207 }
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700208
Pier Ventre42287df2016-11-09 14:17:26 -0800209 // We create the flows.
210 List<FlowRule> pwRules = processPwFilter(portCriterion,
211 innerVlanIdCriterion,
212 outerVlanIdCriterion,
213 tunnelId,
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700214 applicationId,
215 egressVlan
Pier Ventre42287df2016-11-09 14:17:26 -0800216 );
217 // We tag the flow for adding or for removing.
218 for (FlowRule pwRule : pwRules) {
219 log.debug("adding filtering rule in VLAN tables: {} for dev: {}",
220 pwRule, deviceId);
221 ops = install ? ops.add(pwRule) : ops.remove(pwRule);
222 }
223 // We push the filtering rules for the pw.
224 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
225 @Override
226 public void onSuccess(FlowRuleOperations ops) {
227 log.info("Applied {} filtering rules in device {}",
228 ops.stages().get(0).size(), deviceId);
229 pass(filteringObjective);
230 }
231
232 @Override
233 public void onError(FlowRuleOperations ops) {
234 log.info("Failed to apply all filtering rules in dev {}", deviceId);
235 fail(filteringObjective, ObjectiveError.FLOWINSTALLATIONFAILED);
236 }
237 }));
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700238 } else if (isDoubleTagged(filteringObjective)) {
239 processDoubleTaggedFilter(filteringObjective, install, applicationId);
240 } else {
241 // If it is not a pseudo wire flow or double-tagged filter, we fall back
242 // to the OFDPA 2.0 pipeline.
243 super.processFilter(filteringObjective, install, applicationId);
244 }
245 }
Pier Ventre42287df2016-11-09 14:17:26 -0800246
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700247 /**
Andreas Pantelopoulos2fef3112018-06-28 17:06:14 -0700248 *
249 * @param meta The metadata instruction of this treatment
250 * @return True if we should remove both VLAN and Mac Termination flow entries.
251 * This is only used for double tagged hosts.
252 */
253 public boolean shouldRemoveDoubleTagged(Instruction meta) {
254
255 Instructions.MetadataInstruction metadataInstruction = (Instructions.MetadataInstruction) meta;
256 return ((metadataInstruction.metadata() & metadataInstruction.metadataMask()) == 1);
257 }
258
259 /**
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700260 * Configure filtering rules of outer and inner VLAN IDs, and a MAC address.
261 * Filtering happens in three tables (VLAN_TABLE, VLAN_1_TABLE, TMAC_TABLE).
262 *
263 * @param filteringObjective the filtering objective
264 * @param install true to add, false to remove
265 * @param applicationId for application programming this filter
266 */
267 private void processDoubleTaggedFilter(FilteringObjective filteringObjective,
268 boolean install,
269 ApplicationId applicationId) {
270 PortCriterion portCriterion = null;
271 EthCriterion ethCriterion = null;
272 VlanIdCriterion innervidCriterion = null;
273 VlanIdCriterion outerVidCriterion = null;
274 boolean popVlan = false;
Andreas Pantelopoulos2fef3112018-06-28 17:06:14 -0700275 boolean removeDoubleTagged = true;
276 if (filteringObjective.meta().writeMetadata() != null) {
277 removeDoubleTagged = shouldRemoveDoubleTagged(filteringObjective.meta().writeMetadata());
278 }
279 log.info("HERE , removeDoubleTagged {}", removeDoubleTagged);
280
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700281 TrafficTreatment meta = filteringObjective.meta();
282 if (!filteringObjective.key().equals(Criteria.dummy()) &&
283 filteringObjective.key().type() == Criterion.Type.IN_PORT) {
284 portCriterion = (PortCriterion) filteringObjective.key();
285 }
286 if (portCriterion == null) {
287 log.warn("No IN_PORT defined in filtering objective from app: {}" +
288 "Failed to program VLAN tables.", applicationId);
289 return;
290 } else {
291 log.debug("Received filtering objective for dev/port: {}/{}", deviceId,
292 portCriterion.port());
293 }
294
295 // meta should have only one instruction, popVlan.
296 if (meta != null && meta.allInstructions().size() == 1) {
297 L2ModificationInstruction l2Inst = (L2ModificationInstruction) meta.allInstructions().get(0);
298 if (l2Inst.subtype().equals(L2SubType.VLAN_POP)) {
299 popVlan = true;
300 } else {
301 log.warn("Filtering objective can have only VLAN_POP instruction.");
302 return;
303 }
304 } else {
305 log.warn("Filtering objective should have one instruction.");
Pier Ventre42287df2016-11-09 14:17:26 -0800306 return;
307 }
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700308
309 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
310 for (Criterion criterion : filteringObjective.conditions()) {
311 switch (criterion.type()) {
312 case ETH_DST:
313 case ETH_DST_MASKED:
314 ethCriterion = (EthCriterion) criterion;
315 break;
316 case VLAN_VID:
317 if (innervidCriterion == null) {
318 innervidCriterion = (VlanIdCriterion) criterion;
319 } else {
320 outerVidCriterion = innervidCriterion;
321 innervidCriterion = (VlanIdCriterion) criterion;
322 }
323 break;
324 default:
325 log.warn("Unsupported filter {}", criterion);
326 fail(filteringObjective, ObjectiveError.UNSUPPORTED);
327 return;
328 }
329 }
330
331 if (innervidCriterion == null || outerVidCriterion == null) {
332 log.warn("filtering objective should have two vidCriterion.");
333 return;
334 }
335
336 if (ethCriterion == null || ethCriterion.mac().equals(NONE)) {
337 // NOTE: it is possible that a filtering objective only has vidCriterion
338 log.warn("filtering objective missing dstMac, cannot program TMAC table");
339 return;
340 } else {
341 MacAddress unicastMac = readEthDstFromTreatment(filteringObjective.meta());
342 List<List<FlowRule>> allStages = processEthDstFilter(portCriterion, ethCriterion, innervidCriterion,
343 innervidCriterion.vlanId(), unicastMac,
344 applicationId);
345 for (List<FlowRule> flowRules : allStages) {
346 log.trace("Starting a new flow rule stage for TMAC table flow");
347 ops.newStage();
348
349 for (FlowRule flowRule : flowRules) {
350 log.trace("{} flow rules in TMAC table: {} for dev: {}",
351 (install) ? "adding" : "removing", flowRules, deviceId);
352 if (install) {
353 ops = ops.add(flowRule);
354 } else {
355 // NOTE: Only remove TMAC flow when there is no more enabled port within the
356 // same VLAN on this device if TMAC doesn't support matching on in_port.
357 if (matchInPortTmacTable()
358 || (filteringObjective.meta() != null
359 && filteringObjective.meta().clearedDeferred())) {
Andreas Pantelopoulos2fef3112018-06-28 17:06:14 -0700360
361 // if metadata instruction not null and not removeDoubleTagged move on.
362 if ((filteringObjective.meta().writeMetadata() != null) && (!removeDoubleTagged)) {
363 log.info("Skipping removal of tmac rule for device {}", deviceId);
364 continue;
365 } else {
366 ops = ops.remove(flowRule);
367 }
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700368 } else {
369 log.debug("Abort TMAC flow removal on {}. Some other ports still share this TMAC flow");
370 }
371 }
372 }
373 }
374 }
375
376 List<FlowRule> rules;
377 rules = processDoubleVlanIdFilter(portCriterion, innervidCriterion,
378 outerVidCriterion, popVlan, applicationId);
379 for (FlowRule flowRule : rules) {
380 log.trace("{} flow rule in VLAN table: {} for dev: {}",
381 (install) ? "adding" : "removing", flowRule, deviceId);
Andreas Pantelopoulos2fef3112018-06-28 17:06:14 -0700382 // if context is remove, table is vlan_1 and removeDoubleTagged is false, continue.
383 if (flowRule.table().equals(IndexTableId.of(VLAN_TABLE)) &&
384 !removeDoubleTagged && !install) {
385 log.info("Skipping removal of vlan table rule for now!");
386 continue;
387 }
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700388 ops = install ? ops.add(flowRule) : ops.remove(flowRule);
389 }
390
391 // apply filtering flow rules
392 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
393 @Override
394 public void onSuccess(FlowRuleOperations ops) {
395 log.debug("Applied {} filtering rules in device {}",
396 ops.stages().get(0).size(), deviceId);
397 pass(filteringObjective);
398 }
399
400 @Override
401 public void onError(FlowRuleOperations ops) {
402 log.info("Failed to apply all filtering rules in dev {}", deviceId);
403 fail(filteringObjective, ObjectiveError.FLOWINSTALLATIONFAILED);
404 }
405 }));
406
407 }
408 /**
409 * Internal implementation of processDoubleVlanIdFilter.
410 *
411 * @param portCriterion port on device for which this filter is programmed
412 * @param innerVidCriterion inner vlan
413 * @param outerVidCriterion outer vlan
414 * @param popVlan true if outer vlan header needs to be removed
415 * @param applicationId for application programming this filter
416 * @return flow rules for port-vlan filters
417 */
418 private List<FlowRule> processDoubleVlanIdFilter(PortCriterion portCriterion,
419 VlanIdCriterion innerVidCriterion,
420 VlanIdCriterion outerVidCriterion,
421 boolean popVlan,
422 ApplicationId applicationId) {
423 TrafficSelector.Builder outerSelector = DefaultTrafficSelector.builder();
424 TrafficTreatment.Builder outerTreatment = DefaultTrafficTreatment.builder();
425 TrafficSelector.Builder innerSelector = DefaultTrafficSelector.builder();
426 TrafficTreatment.Builder innerTreatment = DefaultTrafficTreatment.builder();
427
428 VlanId outerVlanId = outerVidCriterion.vlanId();
429 VlanId innerVlanId = innerVidCriterion.vlanId();
430 PortNumber portNumber = portCriterion.port();
431 // Check arguments
432 if (PortNumber.ALL.equals(portNumber)
433 || outerVlanId.equals(VlanId.NONE)
434 || innerVlanId.equals(VlanId.NONE)) {
435 log.warn("Incomplete Filtering Objective. " +
436 "VLAN Table cannot be programmed for {}", deviceId);
437 return ImmutableList.of();
438 } else {
439 outerSelector.matchInPort(portNumber);
440 innerSelector.matchInPort(portNumber);
441 outerTreatment.transition(VLAN_1_TABLE);
442 innerTreatment.transition(TMAC_TABLE);
443
444 if (requireVlanExtensions()) {
445 OfdpaMatchVlanVid ofdpaOuterMatchVlanVid = new OfdpaMatchVlanVid(outerVlanId);
446 outerSelector.extension(ofdpaOuterMatchVlanVid, deviceId);
447 OfdpaMatchVlanVid ofdpaInnerMatchVlanVid = new OfdpaMatchVlanVid(innerVlanId);
448 innerSelector.extension(ofdpaInnerMatchVlanVid, deviceId);
449 } else {
450 outerSelector.matchVlanId(outerVlanId);
451 innerSelector.matchVlanId(innerVlanId);
452 }
453
454 innerSelector.extension(new Ofdpa3MatchOvid(outerVlanId), deviceId);
455 outerTreatment.extension(new Ofdpa3SetOvid(outerVlanId), deviceId);
456 if (popVlan) {
457 outerTreatment.popVlan();
458 }
459 }
460 FlowRule outerRule = DefaultFlowRule.builder()
461 .forDevice(deviceId)
462 .withSelector(outerSelector.build())
463 .withTreatment(outerTreatment.build())
464 .withPriority(DEFAULT_PRIORITY)
465 .fromApp(applicationId)
466 .makePermanent()
467 .forTable(VLAN_TABLE)
468 .build();
469 FlowRule innerRule = DefaultFlowRule.builder()
470 .forDevice(deviceId)
471 .withSelector(innerSelector.build())
472 .withTreatment(innerTreatment.build())
473 .withPriority(DEFAULT_PRIORITY)
474 .fromApp(applicationId)
475 .makePermanent()
476 .forTable(VLAN_1_TABLE)
477 .build();
478
479 return ImmutableList.of(outerRule, innerRule);
480 }
481
482 /**
Pier Ventre42287df2016-11-09 14:17:26 -0800483 * Method to process the pw related filtering objectives.
484 *
485 * @param portCriterion the in port match
486 * @param innerVlanIdCriterion the inner vlan match
487 * @param outerVlanIdCriterion the outer vlan match
488 * @param tunnelId the tunnel id
489 * @param applicationId the application id
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700490 * @param egressVlan the vlan to modify, was passed in metadata
Pier Ventre42287df2016-11-09 14:17:26 -0800491 * @return a list of flow rules to install
492 */
493 private List<FlowRule> processPwFilter(PortCriterion portCriterion,
Yi Tsengef19de12017-04-24 11:33:05 -0700494 VlanIdCriterion innerVlanIdCriterion,
495 VlanIdCriterion outerVlanIdCriterion,
496 long tunnelId,
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700497 ApplicationId applicationId,
498 VlanId egressVlan) {
499
Pier Ventre42287df2016-11-09 14:17:26 -0800500 FlowRule vlan1FlowRule;
501 int mplsLogicalPort = ((int) portCriterion.port().toLong());
502 // We have to match on the inner vlan and outer vlan at the same time.
503 // Ofdpa supports this through the OVID meta-data type.
Pier Ventre42287df2016-11-09 14:17:26 -0800504
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700505 ImmutableList<FlowRule> toReturn;
506
507 // pseudowire configured with double tagged vlans
508 if (!(innerVlanIdCriterion.vlanId().equals(VlanId.NONE))
509 && !(outerVlanIdCriterion.vlanId().equals(VlanId.NONE))) {
510
511 log.info("Installing filter objective for double tagged CE for tunnel {}", tunnelId);
512
513 TrafficSelector.Builder vlan1Selector = DefaultTrafficSelector.builder()
514 .matchInPort(portCriterion.port())
515 .matchVlanId(innerVlanIdCriterion.vlanId())
516 .extension(new Ofdpa3MatchOvid(outerVlanIdCriterion.vlanId()), deviceId);
517 // TODO understand for the future how to manage the vlan rewrite.
518 TrafficTreatment.Builder vlan1Treatment = DefaultTrafficTreatment.builder()
519 .pushVlan()
520 .setVlanId(egressVlan)
521 .extension(new Ofdpa3SetMplsType(VPWS), deviceId)
522 .extension(new Ofdpa3SetMplsL2Port(mplsLogicalPort), deviceId)
523 .setTunnelId(tunnelId)
524 .transition(MPLS_L2_PORT_FLOW_TABLE);
525 vlan1FlowRule = DefaultFlowRule.builder()
526 .forDevice(deviceId)
527 .withSelector(vlan1Selector.build())
528 .withTreatment(vlan1Treatment.build())
529 .withPriority(DEFAULT_PRIORITY)
530 .fromApp(applicationId)
531 .makePermanent()
532 .forTable(VLAN_1_TABLE)
533 .build();
534 // Finally we create the flow rule for the vlan table.
535 FlowRule vlanFlowRule;
536 // We have to match on the outer vlan.
537 TrafficSelector.Builder vlanSelector = DefaultTrafficSelector.builder()
538 .matchInPort(portCriterion.port())
539 .matchVlanId(outerVlanIdCriterion.vlanId());
540 // TODO understand for the future how to manage the vlan rewrite.
541 TrafficTreatment.Builder vlanTreatment = DefaultTrafficTreatment.builder()
542 .popVlan()
543 .extension(new Ofdpa3SetOvid(outerVlanIdCriterion.vlanId()), deviceId)
544 .transition(VLAN_1_TABLE);
545 vlanFlowRule = DefaultFlowRule.builder()
546 .forDevice(deviceId)
547 .withSelector(vlanSelector.build())
548 .withTreatment(vlanTreatment.build())
549 .withPriority(DEFAULT_PRIORITY)
550 .fromApp(applicationId)
551 .makePermanent()
552 .forTable(VLAN_TABLE)
553 .build();
554
555 return ImmutableList.of(vlan1FlowRule, vlanFlowRule);
556 } else if (!(innerVlanIdCriterion.vlanId().equals(VlanId.NONE))
557 && (outerVlanIdCriterion.vlanId().equals(VlanId.NONE))) {
558
559 log.info("Installing filter objective for single tagged CE for tunnel {}", tunnelId);
560
561 TrafficSelector.Builder singleVlanSelector = DefaultTrafficSelector.builder()
562 .matchInPort(portCriterion.port())
563 .matchVlanId(innerVlanIdCriterion.vlanId());
564
565 TrafficTreatment.Builder singleVlanTreatment = DefaultTrafficTreatment.builder()
Charles Chanf45552d2018-07-13 18:08:33 -0700566 // FIXME PW VLAN translation is not supported on Dune
567 // Need to explore doing that in egress table later if there is a requirement
568 // .setVlanId(egressVlan)
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700569 .extension(new Ofdpa3SetMplsType(VPWS), deviceId)
570 .extension(new Ofdpa3SetMplsL2Port(mplsLogicalPort), deviceId)
571 .setTunnelId(tunnelId)
572 .transition(MPLS_L2_PORT_FLOW_TABLE);
573
574 vlan1FlowRule = DefaultFlowRule.builder()
575 .forDevice(deviceId)
576 .withSelector(singleVlanSelector.build())
577 .withTreatment(singleVlanTreatment.build())
578 .withPriority(DEFAULT_PRIORITY)
579 .fromApp(applicationId)
580 .makePermanent()
581 .forTable(VLAN_TABLE)
582 .build();
583
584 return ImmutableList.of(vlan1FlowRule);
585 } else if ((innerVlanIdCriterion.vlanId().equals(VlanId.NONE))
586 && (outerVlanIdCriterion.vlanId().equals(VlanId.NONE))) {
587
588 TrafficSelector.Builder singleVlanSelector = DefaultTrafficSelector.builder()
589 .matchInPort(portCriterion.port())
590 .matchVlanId(innerVlanIdCriterion.vlanId());
591
592 TrafficTreatment.Builder singleVlanTreatment = DefaultTrafficTreatment.builder()
593 .extension(new Ofdpa3SetMplsType(VPWS), deviceId)
594 .extension(new Ofdpa3SetMplsL2Port(mplsLogicalPort), deviceId)
595 .setTunnelId(tunnelId)
596 .transition(MPLS_L2_PORT_FLOW_TABLE);
597
598 vlan1FlowRule = DefaultFlowRule.builder()
599 .forDevice(deviceId)
600 .withSelector(singleVlanSelector.build())
601 .withTreatment(singleVlanTreatment.build())
602 .withPriority(DEFAULT_PRIORITY)
603 .fromApp(applicationId)
604 .makePermanent()
605 .forTable(VLAN_TABLE)
606 .build();
607
608 return ImmutableList.of(vlan1FlowRule);
609 } else {
610 // failure...
611 return Collections.emptyList();
612 }
Pier Ventre42287df2016-11-09 14:17:26 -0800613 }
614
615 @Override
Charles Chanf9e98652016-09-07 16:54:23 -0700616 protected Collection<FlowRule> processEthTypeSpecific(ForwardingObjective fwd) {
Saurav Dasa4020382018-02-14 14:14:54 -0800617 // if its not-bos, we go to MPLS_TYPE_TABLE regardless of whether we pop or swap
618 // if it is bos, we go to MPLS_TYPE_TABLE only if we swap
619 if (isNotMplsBos(fwd.selector())
620 || (isMplsBos(fwd.selector()) && !isMplsPop(fwd))) {
Pier Ventre140a8942016-11-02 07:26:38 -0700621 return processEthTypeSpecificInternal(fwd, true, MPLS_TYPE_TABLE);
622 }
Saurav Dasa4020382018-02-14 14:14:54 -0800623 // if it is bos, and we pop, we go to MPLS_L3_TYPE_TABLE
Pier Ventre140a8942016-11-02 07:26:38 -0700624 return processEthTypeSpecificInternal(fwd, true, MPLS_L3_TYPE_TABLE);
Charles Chanf9e98652016-09-07 16:54:23 -0700625 }
Pier Ventre42287df2016-11-09 14:17:26 -0800626
627 @Override
628 protected Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
629 // We use the tunnel id to identify pw related flows.
Pier Ventre70d53ba2016-11-17 22:26:29 -0800630 // Looking for the fwd objective of the initiation.
Pier Ventre42287df2016-11-09 14:17:26 -0800631 TunnelIdCriterion tunnelIdCriterion = (TunnelIdCriterion) fwd.selector()
632 .getCriterion(TUNNEL_ID);
633 if (tunnelIdCriterion != null) {
Pier Ventre70d53ba2016-11-17 22:26:29 -0800634 return processInitPwVersatile(fwd);
635 }
636 // Looking for the fwd objective of the termination.
637 ModTunnelIdInstruction modTunnelIdInstruction = getModTunnelIdInstruction(fwd.treatment());
638 OutputInstruction outputInstruction = getOutputInstruction(fwd.treatment());
639 if (modTunnelIdInstruction != null && outputInstruction != null) {
640 return processTermPwVersatile(fwd, modTunnelIdInstruction, outputInstruction);
Pier Ventre42287df2016-11-09 14:17:26 -0800641 }
642 // If it is not a pseudo wire flow we fall back
643 // to the OFDPA 2.0 pipeline.
644 return super.processVersatile(fwd);
645 }
646
Pier Ventre70d53ba2016-11-17 22:26:29 -0800647 private Collection<FlowRule> processTermPwVersatile(ForwardingObjective forwardingObjective,
648 ModTunnelIdInstruction modTunnelIdInstruction,
649 OutputInstruction outputInstruction) {
650 TrafficTreatment.Builder flowTreatment;
651 TrafficSelector.Builder flowSelector;
652 // We divide the mpls actions from the tunnel actions. We need
653 // this to order the actions in the final treatment.
654 TrafficTreatment.Builder mplsTreatment = DefaultTrafficTreatment.builder();
655 createMplsTreatment(forwardingObjective.treatment(), mplsTreatment);
656 // The match of the forwarding objective is ready to go.
657 flowSelector = DefaultTrafficSelector.builder(forwardingObjective.selector());
658 // We verify the tunnel id and mpls port are correct.
659 long tunnelId = MPLS_TUNNEL_ID_BASE | modTunnelIdInstruction.tunnelId();
660 if (tunnelId > MPLS_TUNNEL_ID_MAX) {
661 log.error("Pw Versatile Forwarding Objective must include tunnel id < {}",
662 MPLS_TUNNEL_ID_MAX);
663 fail(forwardingObjective, ObjectiveError.BADPARAMS);
664 return Collections.emptySet();
665 }
666 // 0x0002XXXX is NNI interface.
667 int mplsLogicalPort = ((int) outputInstruction.port().toLong()) | MPLS_NNI_PORT_BASE;
668 if (mplsLogicalPort > MPLS_NNI_PORT_MAX) {
669 log.error("Pw Versatile Forwarding Objective invalid logical port {}",
670 mplsLogicalPort);
671 fail(forwardingObjective, ObjectiveError.BADPARAMS);
672 return Collections.emptySet();
673 }
674 // Next id cannot be null.
675 if (forwardingObjective.nextId() == null) {
676 log.error("Pw Versatile Forwarding Objective must contain nextId ",
677 forwardingObjective.nextId());
678 fail(forwardingObjective, ObjectiveError.BADPARAMS);
679 return Collections.emptySet();
680 }
681 // We retrieve the l2 interface group and point the mpls
682 // flow to this.
683 NextGroup next = getGroupForNextObjective(forwardingObjective.nextId());
684 if (next == null) {
685 log.warn("next-id:{} not found in dev:{}", forwardingObjective.nextId(), deviceId);
686 fail(forwardingObjective, ObjectiveError.GROUPMISSING);
687 return Collections.emptySet();
688 }
689 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
690 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
691 if (group == null) {
692 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
693 gkeys.get(0).peekFirst(), forwardingObjective.nextId(), deviceId);
694 fail(forwardingObjective, ObjectiveError.GROUPMISSING);
695 return Collections.emptySet();
696 }
697 // We prepare the treatment for the mpls flow table.
698 // The order of the actions has to be strictly this
699 // according to the OFDPA 2.0 specification.
700 flowTreatment = DefaultTrafficTreatment.builder(mplsTreatment.build());
701 flowTreatment.extension(new Ofdpa3PopCw(), deviceId);
Pier Luigi3bfe32c2017-01-30 09:47:36 -0800702 // Even though the specification and the xml/json files
703 // specify is allowed, the switch rejects the flow. In the
704 // OFDPA 3.0 EA0 version was necessary
705 //flowTreatment.popVlan();
Pier Ventre70d53ba2016-11-17 22:26:29 -0800706 flowTreatment.extension(new Ofdpa3PopL2Header(), deviceId);
707 flowTreatment.setTunnelId(tunnelId);
708 flowTreatment.extension(new Ofdpa3SetMplsL2Port(mplsLogicalPort), deviceId);
709 flowTreatment.extension(new Ofdpa3SetMplsType(VPWS), deviceId);
710 flowTreatment.transition(MPLS_TYPE_TABLE);
711 flowTreatment.deferred().group(group.id());
712 // We prepare the flow rule for the mpls table.
713 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
714 .fromApp(forwardingObjective.appId())
715 .withPriority(forwardingObjective.priority())
716 .forDevice(deviceId)
717 .withSelector(flowSelector.build())
718 .withTreatment(flowTreatment.build())
719 .makePermanent()
720 .forTable(MPLS_TABLE_1);
721 return Collections.singletonList(ruleBuilder.build());
722 }
723
Pier Ventre42287df2016-11-09 14:17:26 -0800724 /**
725 * Helper method to process the pw forwarding objectives.
726 *
727 * @param forwardingObjective the fw objective to process
728 * @return a singleton list of flow rule
729 */
Pier Ventre70d53ba2016-11-17 22:26:29 -0800730 private Collection<FlowRule> processInitPwVersatile(ForwardingObjective forwardingObjective) {
Pier Ventre42287df2016-11-09 14:17:26 -0800731 // We retrieve the matching criteria for mpls l2 port.
732 TunnelIdCriterion tunnelIdCriterion = (TunnelIdCriterion) forwardingObjective.selector()
733 .getCriterion(TUNNEL_ID);
734 PortCriterion portCriterion = (PortCriterion) forwardingObjective.selector()
735 .getCriterion(IN_PORT);
736 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
737 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
738 int mplsLogicalPort;
739 long tunnelId;
740 // Mpls tunnel ids according to the OFDPA manual have to be
741 // in the range [2^17-1, 2^16].
742 tunnelId = MPLS_TUNNEL_ID_BASE | tunnelIdCriterion.tunnelId();
743 if (tunnelId > MPLS_TUNNEL_ID_MAX) {
744 log.error("Pw Versatile Forwarding Objective must include tunnel id < {}",
745 MPLS_TUNNEL_ID_MAX);
746 fail(forwardingObjective, ObjectiveError.BADPARAMS);
747 return Collections.emptySet();
748 }
749 // Port has not been null.
750 if (portCriterion == null) {
751 log.error("Pw Versatile Forwarding Objective must include port");
752 fail(forwardingObjective, ObjectiveError.BADPARAMS);
753 return Collections.emptySet();
754 }
755 // 0x0000XXXX is UNI interface.
Pier Ventre70d53ba2016-11-17 22:26:29 -0800756 if (portCriterion.port().toLong() > MPLS_UNI_PORT_MAX) {
Pier Ventre42287df2016-11-09 14:17:26 -0800757 log.error("Pw Versatile Forwarding Objective invalid logical port {}",
758 portCriterion.port().toLong());
759 fail(forwardingObjective, ObjectiveError.BADPARAMS);
760 return Collections.emptySet();
761 }
762 mplsLogicalPort = ((int) portCriterion.port().toLong());
763 if (forwardingObjective.nextId() == null) {
764 log.error("Pw Versatile Forwarding Objective must contain nextId ",
765 forwardingObjective.nextId());
766 fail(forwardingObjective, ObjectiveError.BADPARAMS);
767 return Collections.emptySet();
768 }
769 // We don't expect a treatment.
770 if (forwardingObjective.treatment() != null &&
771 !forwardingObjective.treatment().equals(DefaultTrafficTreatment.emptyTreatment())) {
772 log.error("Pw Versatile Forwarding Objective cannot contain a treatment ",
773 forwardingObjective.nextId());
774 fail(forwardingObjective, ObjectiveError.BADPARAMS);
775 return Collections.emptySet();
776 }
777 // We retrieve the l2 vpn group and point the mpls
778 // l2 port to this.
779 NextGroup next = getGroupForNextObjective(forwardingObjective.nextId());
780 if (next == null) {
781 log.warn("next-id:{} not found in dev:{}", forwardingObjective.nextId(), deviceId);
782 fail(forwardingObjective, ObjectiveError.GROUPMISSING);
783 return Collections.emptySet();
784 }
785 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
786 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
787 if (group == null) {
788 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
789 gkeys.get(0).peekFirst(), forwardingObjective.nextId(), deviceId);
790 fail(forwardingObjective, ObjectiveError.GROUPMISSING);
791 return Collections.emptySet();
792 }
793 // We prepare the flow rule for the mpls l2 port table.
794 selector.matchTunnelId(tunnelId);
795 selector.extension(new Ofdpa3MatchMplsL2Port(mplsLogicalPort), deviceId);
796 // This should not be necessary but without we receive an error
797 treatment.extension(new Ofdpa3SetQosIndex(0), deviceId);
798 treatment.transition(MPLS_L2_PORT_PCP_TRUST_FLOW_TABLE);
799 treatment.deferred().group(group.id());
800 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
801 .fromApp(forwardingObjective.appId())
802 .withPriority(MPLS_L2_PORT_PRIORITY)
803 .forDevice(deviceId)
804 .withSelector(selector.build())
805 .withTreatment(treatment.build())
806 .makePermanent()
807 .forTable(MPLS_L2_PORT_FLOW_TABLE);
808 return Collections.singletonList(ruleBuilder.build());
809 }
Pier Ventre70d53ba2016-11-17 22:26:29 -0800810
811 /**
Pier Ventre70d53ba2016-11-17 22:26:29 -0800812 * Helper method for dividing the tunnel instructions from the mpls
813 * instructions.
814 *
815 * @param treatment the treatment to analyze
816 * @param mplsTreatment the mpls treatment builder
817 */
818 private void createMplsTreatment(TrafficTreatment treatment,
819 TrafficTreatment.Builder mplsTreatment) {
820
821 for (Instruction ins : treatment.allInstructions()) {
822
823 if (ins.type() == Instruction.Type.L2MODIFICATION) {
824 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
825 switch (l2ins.subtype()) {
826 // These instructions have to go in the mpls
827 // treatment.
828 case TUNNEL_ID:
829 break;
830 case DEC_MPLS_TTL:
831 case MPLS_POP:
832 mplsTreatment.add(ins);
833 break;
834 default:
835 log.warn("Driver does not handle this type of TrafficTreatment"
836 + " instruction in nextObjectives: {} - {}",
837 ins.type(), ins);
838 break;
839 }
840 } else if (ins.type() == Instruction.Type.OUTPUT) {
841 break;
842 } else if (ins.type() == Instruction.Type.L3MODIFICATION) {
843 // We support partially the l3 instructions.
844 L3ModificationInstruction l3ins = (L3ModificationInstruction) ins;
845 switch (l3ins.subtype()) {
846 case TTL_IN:
847 mplsTreatment.add(ins);
848 break;
849 default:
850 log.warn("Driver does not handle this type of TrafficTreatment"
851 + " instruction in nextObjectives: {} - {}",
852 ins.type(), ins);
853 }
854
855 } else {
856 log.warn("Driver does not handle this type of TrafficTreatment"
857 + " instruction in nextObjectives: {} - {}",
858 ins.type(), ins);
859 }
860 }
861 }
pier9469f3e2019-04-17 17:05:08 +0200862
Charles Chanf9e98652016-09-07 16:54:23 -0700863}