Charles Chan | f9e9865 | 2016-09-07 16:54:23 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Charles Chan | f9e9865 | 2016-09-07 16:54:23 -0700 | [diff] [blame] | 3 | * |
| 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 Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 17 | package org.onosproject.driver.pipeline.ofdpa; |
Charles Chan | f9e9865 | 2016-09-07 16:54:23 -0700 | [diff] [blame] | 18 | |
Pier Ventre | 42287df | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableList; |
Jonghwan Hyun | 800d9d0 | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 20 | import org.onlab.packet.MacAddress; |
Andreas Pantelopoulos | 27532cd | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 21 | import org.onlab.packet.VlanId; |
Charles Chan | f9e9865 | 2016-09-07 16:54:23 -0700 | [diff] [blame] | 22 | import org.onosproject.core.ApplicationId; |
Pier Ventre | 42287df | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 23 | import org.onosproject.driver.extensions.Ofdpa3MatchMplsL2Port; |
| 24 | import org.onosproject.driver.extensions.Ofdpa3MatchOvid; |
Pier Ventre | 70d53ba | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 25 | import org.onosproject.driver.extensions.Ofdpa3PopCw; |
| 26 | import org.onosproject.driver.extensions.Ofdpa3PopL2Header; |
Pier Ventre | 42287df | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 27 | import org.onosproject.driver.extensions.Ofdpa3SetMplsL2Port; |
| 28 | import org.onosproject.driver.extensions.Ofdpa3SetMplsType; |
| 29 | import org.onosproject.driver.extensions.Ofdpa3SetOvid; |
| 30 | import org.onosproject.driver.extensions.Ofdpa3SetQosIndex; |
Jonghwan Hyun | 800d9d0 | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 31 | import org.onosproject.driver.extensions.OfdpaMatchVlanVid; |
| 32 | import org.onosproject.net.PortNumber; |
Pier Ventre | 42287df | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 33 | import org.onosproject.net.behaviour.NextGroup; |
Charles Chan | f9e9865 | 2016-09-07 16:54:23 -0700 | [diff] [blame] | 34 | import org.onosproject.net.behaviour.PipelinerContext; |
Pier Ventre | 42287df | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 35 | import org.onosproject.net.flow.DefaultFlowRule; |
| 36 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 37 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
Charles Chan | f9e9865 | 2016-09-07 16:54:23 -0700 | [diff] [blame] | 38 | import org.onosproject.net.flow.FlowRule; |
Pier Ventre | 42287df | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 39 | import org.onosproject.net.flow.FlowRuleOperations; |
| 40 | import org.onosproject.net.flow.FlowRuleOperationsContext; |
| 41 | import org.onosproject.net.flow.TrafficSelector; |
| 42 | import org.onosproject.net.flow.TrafficTreatment; |
Jonghwan Hyun | 800d9d0 | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 43 | import org.onosproject.net.flow.criteria.Criteria; |
Pier Ventre | 42287df | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 44 | import org.onosproject.net.flow.criteria.Criterion; |
Jonghwan Hyun | 800d9d0 | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 45 | import org.onosproject.net.flow.criteria.EthCriterion; |
Charles Chan | f9e9865 | 2016-09-07 16:54:23 -0700 | [diff] [blame] | 46 | import org.onosproject.net.flow.criteria.PortCriterion; |
Pier Ventre | 42287df | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 47 | import org.onosproject.net.flow.criteria.TunnelIdCriterion; |
Charles Chan | f9e9865 | 2016-09-07 16:54:23 -0700 | [diff] [blame] | 48 | import org.onosproject.net.flow.criteria.VlanIdCriterion; |
Pier Ventre | 70d53ba | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 49 | import org.onosproject.net.flow.instructions.Instruction; |
| 50 | import org.onosproject.net.flow.instructions.Instructions.OutputInstruction; |
Pier Ventre | 42287df | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 51 | import org.onosproject.net.flow.instructions.L2ModificationInstruction; |
| 52 | import org.onosproject.net.flow.instructions.L2ModificationInstruction.L2SubType; |
Pier Ventre | 70d53ba | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 53 | import org.onosproject.net.flow.instructions.L3ModificationInstruction; |
Pier Ventre | 42287df | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 54 | import org.onosproject.net.flowobjective.FilteringObjective; |
Charles Chan | f9e9865 | 2016-09-07 16:54:23 -0700 | [diff] [blame] | 55 | import org.onosproject.net.flowobjective.ForwardingObjective; |
Pier Ventre | 42287df | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 56 | import org.onosproject.net.flowobjective.ObjectiveError; |
| 57 | import org.onosproject.net.group.Group; |
| 58 | import org.onosproject.net.group.GroupKey; |
| 59 | import org.slf4j.Logger; |
Charles Chan | f9e9865 | 2016-09-07 16:54:23 -0700 | [diff] [blame] | 60 | |
| 61 | import java.util.Collection; |
Pier Ventre | 42287df | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 62 | import java.util.Collections; |
| 63 | import java.util.Deque; |
Charles Chan | f9e9865 | 2016-09-07 16:54:23 -0700 | [diff] [blame] | 64 | import java.util.List; |
| 65 | |
Jonghwan Hyun | 800d9d0 | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 66 | import static org.onlab.packet.MacAddress.NONE; |
Pier Ventre | 42287df | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 67 | import static org.onosproject.driver.extensions.Ofdpa3MplsType.VPWS; |
Andrea Campanella | d980c6d | 2018-04-30 11:48:55 +0200 | [diff] [blame] | 68 | import static org.onosproject.net.flow.criteria.Criterion.Type.INNER_VLAN_VID; |
| 69 | import static org.onosproject.net.flow.criteria.Criterion.Type.IN_PORT; |
| 70 | import static org.onosproject.net.flow.criteria.Criterion.Type.TUNNEL_ID; |
| 71 | import static org.onosproject.net.flow.criteria.Criterion.Type.VLAN_VID; |
Pier Ventre | 42287df | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 72 | import static org.onosproject.net.flow.instructions.Instruction.Type.L2MODIFICATION; |
| 73 | import static org.onosproject.net.flow.instructions.L2ModificationInstruction.ModTunnelIdInstruction; |
| 74 | import static org.slf4j.LoggerFactory.getLogger; |
| 75 | |
Charles Chan | f9e9865 | 2016-09-07 16:54:23 -0700 | [diff] [blame] | 76 | /** |
| 77 | * Pipeliner for Broadcom OF-DPA 3.0 TTP. |
| 78 | */ |
| 79 | public class Ofdpa3Pipeline extends Ofdpa2Pipeline { |
Pier Ventre | 42287df | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 80 | |
| 81 | private final Logger log = getLogger(getClass()); |
| 82 | |
Charles Chan | f9e9865 | 2016-09-07 16:54:23 -0700 | [diff] [blame] | 83 | @Override |
Charles Chan | 40132b3 | 2017-01-22 00:19:37 -0800 | [diff] [blame] | 84 | protected void initDriverId() { |
Charles Chan | f9e9865 | 2016-09-07 16:54:23 -0700 | [diff] [blame] | 85 | driverId = coreService.registerApplication( |
| 86 | "org.onosproject.driver.Ofdpa3Pipeline"); |
Charles Chan | 40132b3 | 2017-01-22 00:19:37 -0800 | [diff] [blame] | 87 | } |
Charles Chan | f9e9865 | 2016-09-07 16:54:23 -0700 | [diff] [blame] | 88 | |
Charles Chan | 40132b3 | 2017-01-22 00:19:37 -0800 | [diff] [blame] | 89 | @Override |
| 90 | protected void initGroupHander(PipelinerContext context) { |
| 91 | groupHandler = new Ofdpa3GroupHandler(); |
| 92 | groupHandler.init(deviceId, context); |
Charles Chan | f9e9865 | 2016-09-07 16:54:23 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | @Override |
Charles Chan | d117263 | 2017-03-15 17:33:09 -0700 | [diff] [blame] | 96 | protected boolean requireVlanExtensions() { |
| 97 | return false; |
| 98 | } |
| 99 | |
| 100 | @Override |
Saurav Das | c568c34 | 2018-01-25 09:49:01 -0800 | [diff] [blame] | 101 | protected boolean shouldRetry() { |
| 102 | return false; |
| 103 | } |
| 104 | |
| 105 | @Override |
Andrea Campanella | d980c6d | 2018-04-30 11:48:55 +0200 | [diff] [blame] | 106 | protected boolean supportsUnicastBlackHole() { |
| 107 | return true; |
| 108 | } |
| 109 | |
| 110 | @Override |
Pier Ventre | 42287df | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 111 | protected void processFilter(FilteringObjective filteringObjective, |
| 112 | boolean install, |
| 113 | ApplicationId applicationId) { |
Andreas Pantelopoulos | 27532cd | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 114 | |
| 115 | // Check if filter is intended for pseudowire |
| 116 | boolean isPw = isPseudowire(filteringObjective); |
| 117 | |
| 118 | if (isPw) { |
Pier Ventre | 42287df | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 119 | FlowRuleOperations.Builder ops = FlowRuleOperations.builder(); |
| 120 | PortCriterion portCriterion; |
Andreas Pantelopoulos | 27532cd | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 121 | |
| 122 | VlanIdCriterion innerVlanIdCriterion = null; |
| 123 | for (Criterion criterion : filteringObjective.conditions()) { |
| 124 | if (criterion.type() == INNER_VLAN_VID) { |
| 125 | innerVlanIdCriterion = (VlanIdCriterion) criterion; |
| 126 | break; |
| 127 | } |
| 128 | } |
| 129 | |
Pier Ventre | 42287df | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 130 | VlanIdCriterion outerVlanIdCriterion = null; |
| 131 | // We extract the expected port criterion in the key. |
| 132 | portCriterion = (PortCriterion) filteringObjective.key(); |
| 133 | // We extract the outer vlan id criterion. |
| 134 | for (Criterion criterion : filteringObjective.conditions()) { |
| 135 | if (criterion.type() == VLAN_VID) { |
| 136 | outerVlanIdCriterion = (VlanIdCriterion) criterion; |
| 137 | break; |
| 138 | } |
| 139 | } |
Andreas Pantelopoulos | 27532cd | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 140 | |
Pier Ventre | 42287df | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 141 | // We extract the tunnel id. |
| 142 | long tunnelId; |
Andreas Pantelopoulos | 27532cd | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 143 | VlanId egressVlan; |
| 144 | |
Pier Ventre | 42287df | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 145 | if (filteringObjective.meta() != null && |
Andreas Pantelopoulos | 27532cd | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 146 | filteringObjective.meta().allInstructions().size() != 2) { |
Pier Ventre | 42287df | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 147 | log.warn("Bad filtering objective from app: {}. Not" |
| 148 | + "processing filtering objective", applicationId); |
| 149 | fail(filteringObjective, ObjectiveError.BADPARAMS); |
| 150 | return; |
| 151 | } else if (filteringObjective.meta() != null && |
Andreas Pantelopoulos | 27532cd | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 152 | filteringObjective.meta().allInstructions().size() == 2 && |
| 153 | filteringObjective.meta().allInstructions().get(0).type() == L2MODIFICATION && |
| 154 | filteringObjective.meta().allInstructions().get(1).type() == L2MODIFICATION) { |
| 155 | |
Pier Ventre | 42287df | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 156 | L2ModificationInstruction l2instruction = (L2ModificationInstruction) |
| 157 | filteringObjective.meta().allInstructions().get(0); |
Andreas Pantelopoulos | 27532cd | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 158 | |
Pier Ventre | 42287df | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 159 | if (l2instruction.subtype() != L2SubType.TUNNEL_ID) { |
| 160 | log.warn("Bad filtering objective from app: {}. Not" |
| 161 | + "processing filtering objective", applicationId); |
| 162 | fail(filteringObjective, ObjectiveError.BADPARAMS); |
| 163 | return; |
| 164 | } else { |
| 165 | tunnelId = ((ModTunnelIdInstruction) l2instruction).tunnelId(); |
| 166 | } |
Andreas Pantelopoulos | 27532cd | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 167 | |
| 168 | L2ModificationInstruction vlanInstruction = (L2ModificationInstruction) |
| 169 | filteringObjective.meta().allInstructions().get(1); |
| 170 | |
| 171 | if (vlanInstruction.subtype() != L2SubType.VLAN_ID) { |
| 172 | log.warn("Bad filtering objective from app: {}. Not" |
| 173 | + "processing filtering objective", applicationId); |
| 174 | fail(filteringObjective, ObjectiveError.BADPARAMS); |
| 175 | return; |
| 176 | } else { |
| 177 | egressVlan = ((L2ModificationInstruction.ModVlanIdInstruction) vlanInstruction).vlanId(); |
| 178 | } |
Pier Ventre | 42287df | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 179 | } else { |
| 180 | log.warn("Bad filtering objective from app: {}. Not" |
| 181 | + "processing filtering objective", applicationId); |
| 182 | fail(filteringObjective, ObjectiveError.BADPARAMS); |
| 183 | return; |
| 184 | } |
Andreas Pantelopoulos | 27532cd | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 185 | |
Pier Ventre | 42287df | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 186 | // Mpls tunnel ids according to the OFDPA manual have to be |
| 187 | // in the range [2^17-1, 2^16]. |
| 188 | tunnelId = MPLS_TUNNEL_ID_BASE | tunnelId; |
| 189 | // Sanity check for the filtering objective. |
| 190 | if (portCriterion == null || |
| 191 | outerVlanIdCriterion == null || |
| 192 | tunnelId > MPLS_TUNNEL_ID_MAX) { |
| 193 | log.warn("Bad filtering objective from app: {}. Not" |
| 194 | + "processing filtering objective", applicationId); |
| 195 | fail(filteringObjective, ObjectiveError.BADPARAMS); |
| 196 | return; |
| 197 | } |
| 198 | // 0x0000XXXX is UNI interface. |
Pier Ventre | 70d53ba | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 199 | if (portCriterion.port().toLong() > MPLS_UNI_PORT_MAX) { |
Sivachidambaram Subramanian | a0efdcc | 2017-06-22 05:26:46 +0530 | [diff] [blame] | 200 | log.error("Filtering Objective invalid logical port {}", |
Pier Ventre | 42287df | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 201 | portCriterion.port().toLong()); |
| 202 | fail(filteringObjective, ObjectiveError.BADPARAMS); |
| 203 | return; |
| 204 | } |
Andreas Pantelopoulos | 27532cd | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 205 | |
Pier Ventre | 42287df | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 206 | // We create the flows. |
| 207 | List<FlowRule> pwRules = processPwFilter(portCriterion, |
| 208 | innerVlanIdCriterion, |
| 209 | outerVlanIdCriterion, |
| 210 | tunnelId, |
Andreas Pantelopoulos | 27532cd | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 211 | applicationId, |
| 212 | egressVlan |
Pier Ventre | 42287df | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 213 | ); |
| 214 | // We tag the flow for adding or for removing. |
| 215 | for (FlowRule pwRule : pwRules) { |
| 216 | log.debug("adding filtering rule in VLAN tables: {} for dev: {}", |
| 217 | pwRule, deviceId); |
| 218 | ops = install ? ops.add(pwRule) : ops.remove(pwRule); |
| 219 | } |
| 220 | // We push the filtering rules for the pw. |
| 221 | flowRuleService.apply(ops.build(new FlowRuleOperationsContext() { |
| 222 | @Override |
| 223 | public void onSuccess(FlowRuleOperations ops) { |
| 224 | log.info("Applied {} filtering rules in device {}", |
| 225 | ops.stages().get(0).size(), deviceId); |
| 226 | pass(filteringObjective); |
| 227 | } |
| 228 | |
| 229 | @Override |
| 230 | public void onError(FlowRuleOperations ops) { |
| 231 | log.info("Failed to apply all filtering rules in dev {}", deviceId); |
| 232 | fail(filteringObjective, ObjectiveError.FLOWINSTALLATIONFAILED); |
| 233 | } |
| 234 | })); |
Jonghwan Hyun | 800d9d0 | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 235 | } else if (isDoubleTagged(filteringObjective)) { |
| 236 | processDoubleTaggedFilter(filteringObjective, install, applicationId); |
| 237 | } else { |
| 238 | // If it is not a pseudo wire flow or double-tagged filter, we fall back |
| 239 | // to the OFDPA 2.0 pipeline. |
| 240 | super.processFilter(filteringObjective, install, applicationId); |
| 241 | } |
| 242 | } |
Pier Ventre | 42287df | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 243 | |
Jonghwan Hyun | 800d9d0 | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 244 | /** |
| 245 | * Configure filtering rules of outer and inner VLAN IDs, and a MAC address. |
| 246 | * Filtering happens in three tables (VLAN_TABLE, VLAN_1_TABLE, TMAC_TABLE). |
| 247 | * |
| 248 | * @param filteringObjective the filtering objective |
| 249 | * @param install true to add, false to remove |
| 250 | * @param applicationId for application programming this filter |
| 251 | */ |
| 252 | private void processDoubleTaggedFilter(FilteringObjective filteringObjective, |
| 253 | boolean install, |
| 254 | ApplicationId applicationId) { |
| 255 | PortCriterion portCriterion = null; |
| 256 | EthCriterion ethCriterion = null; |
| 257 | VlanIdCriterion innervidCriterion = null; |
| 258 | VlanIdCriterion outerVidCriterion = null; |
| 259 | boolean popVlan = false; |
| 260 | TrafficTreatment meta = filteringObjective.meta(); |
| 261 | if (!filteringObjective.key().equals(Criteria.dummy()) && |
| 262 | filteringObjective.key().type() == Criterion.Type.IN_PORT) { |
| 263 | portCriterion = (PortCriterion) filteringObjective.key(); |
| 264 | } |
| 265 | if (portCriterion == null) { |
| 266 | log.warn("No IN_PORT defined in filtering objective from app: {}" + |
| 267 | "Failed to program VLAN tables.", applicationId); |
| 268 | return; |
| 269 | } else { |
| 270 | log.debug("Received filtering objective for dev/port: {}/{}", deviceId, |
| 271 | portCriterion.port()); |
| 272 | } |
| 273 | |
| 274 | // meta should have only one instruction, popVlan. |
| 275 | if (meta != null && meta.allInstructions().size() == 1) { |
| 276 | L2ModificationInstruction l2Inst = (L2ModificationInstruction) meta.allInstructions().get(0); |
| 277 | if (l2Inst.subtype().equals(L2SubType.VLAN_POP)) { |
| 278 | popVlan = true; |
| 279 | } else { |
| 280 | log.warn("Filtering objective can have only VLAN_POP instruction."); |
| 281 | return; |
| 282 | } |
| 283 | } else { |
| 284 | log.warn("Filtering objective should have one instruction."); |
Pier Ventre | 42287df | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 285 | return; |
| 286 | } |
Jonghwan Hyun | 800d9d0 | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 287 | |
| 288 | FlowRuleOperations.Builder ops = FlowRuleOperations.builder(); |
| 289 | for (Criterion criterion : filteringObjective.conditions()) { |
| 290 | switch (criterion.type()) { |
| 291 | case ETH_DST: |
| 292 | case ETH_DST_MASKED: |
| 293 | ethCriterion = (EthCriterion) criterion; |
| 294 | break; |
| 295 | case VLAN_VID: |
| 296 | if (innervidCriterion == null) { |
| 297 | innervidCriterion = (VlanIdCriterion) criterion; |
| 298 | } else { |
| 299 | outerVidCriterion = innervidCriterion; |
| 300 | innervidCriterion = (VlanIdCriterion) criterion; |
| 301 | } |
| 302 | break; |
| 303 | default: |
| 304 | log.warn("Unsupported filter {}", criterion); |
| 305 | fail(filteringObjective, ObjectiveError.UNSUPPORTED); |
| 306 | return; |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | if (innervidCriterion == null || outerVidCriterion == null) { |
| 311 | log.warn("filtering objective should have two vidCriterion."); |
| 312 | return; |
| 313 | } |
| 314 | |
| 315 | if (ethCriterion == null || ethCriterion.mac().equals(NONE)) { |
| 316 | // NOTE: it is possible that a filtering objective only has vidCriterion |
| 317 | log.warn("filtering objective missing dstMac, cannot program TMAC table"); |
| 318 | return; |
| 319 | } else { |
| 320 | MacAddress unicastMac = readEthDstFromTreatment(filteringObjective.meta()); |
| 321 | List<List<FlowRule>> allStages = processEthDstFilter(portCriterion, ethCriterion, innervidCriterion, |
| 322 | innervidCriterion.vlanId(), unicastMac, |
| 323 | applicationId); |
| 324 | for (List<FlowRule> flowRules : allStages) { |
| 325 | log.trace("Starting a new flow rule stage for TMAC table flow"); |
| 326 | ops.newStage(); |
| 327 | |
| 328 | for (FlowRule flowRule : flowRules) { |
| 329 | log.trace("{} flow rules in TMAC table: {} for dev: {}", |
| 330 | (install) ? "adding" : "removing", flowRules, deviceId); |
| 331 | if (install) { |
| 332 | ops = ops.add(flowRule); |
| 333 | } else { |
| 334 | // NOTE: Only remove TMAC flow when there is no more enabled port within the |
| 335 | // same VLAN on this device if TMAC doesn't support matching on in_port. |
| 336 | if (matchInPortTmacTable() |
| 337 | || (filteringObjective.meta() != null |
| 338 | && filteringObjective.meta().clearedDeferred())) { |
| 339 | ops = ops.remove(flowRule); |
| 340 | } else { |
| 341 | log.debug("Abort TMAC flow removal on {}. Some other ports still share this TMAC flow"); |
| 342 | } |
| 343 | } |
| 344 | } |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | List<FlowRule> rules; |
| 349 | rules = processDoubleVlanIdFilter(portCriterion, innervidCriterion, |
| 350 | outerVidCriterion, popVlan, applicationId); |
| 351 | for (FlowRule flowRule : rules) { |
| 352 | log.trace("{} flow rule in VLAN table: {} for dev: {}", |
| 353 | (install) ? "adding" : "removing", flowRule, deviceId); |
| 354 | ops = install ? ops.add(flowRule) : ops.remove(flowRule); |
| 355 | } |
| 356 | |
| 357 | // apply filtering flow rules |
| 358 | flowRuleService.apply(ops.build(new FlowRuleOperationsContext() { |
| 359 | @Override |
| 360 | public void onSuccess(FlowRuleOperations ops) { |
| 361 | log.debug("Applied {} filtering rules in device {}", |
| 362 | ops.stages().get(0).size(), deviceId); |
| 363 | pass(filteringObjective); |
| 364 | } |
| 365 | |
| 366 | @Override |
| 367 | public void onError(FlowRuleOperations ops) { |
| 368 | log.info("Failed to apply all filtering rules in dev {}", deviceId); |
| 369 | fail(filteringObjective, ObjectiveError.FLOWINSTALLATIONFAILED); |
| 370 | } |
| 371 | })); |
| 372 | |
| 373 | } |
| 374 | /** |
| 375 | * Internal implementation of processDoubleVlanIdFilter. |
| 376 | * |
| 377 | * @param portCriterion port on device for which this filter is programmed |
| 378 | * @param innerVidCriterion inner vlan |
| 379 | * @param outerVidCriterion outer vlan |
| 380 | * @param popVlan true if outer vlan header needs to be removed |
| 381 | * @param applicationId for application programming this filter |
| 382 | * @return flow rules for port-vlan filters |
| 383 | */ |
| 384 | private List<FlowRule> processDoubleVlanIdFilter(PortCriterion portCriterion, |
| 385 | VlanIdCriterion innerVidCriterion, |
| 386 | VlanIdCriterion outerVidCriterion, |
| 387 | boolean popVlan, |
| 388 | ApplicationId applicationId) { |
| 389 | TrafficSelector.Builder outerSelector = DefaultTrafficSelector.builder(); |
| 390 | TrafficTreatment.Builder outerTreatment = DefaultTrafficTreatment.builder(); |
| 391 | TrafficSelector.Builder innerSelector = DefaultTrafficSelector.builder(); |
| 392 | TrafficTreatment.Builder innerTreatment = DefaultTrafficTreatment.builder(); |
| 393 | |
| 394 | VlanId outerVlanId = outerVidCriterion.vlanId(); |
| 395 | VlanId innerVlanId = innerVidCriterion.vlanId(); |
| 396 | PortNumber portNumber = portCriterion.port(); |
| 397 | // Check arguments |
| 398 | if (PortNumber.ALL.equals(portNumber) |
| 399 | || outerVlanId.equals(VlanId.NONE) |
| 400 | || innerVlanId.equals(VlanId.NONE)) { |
| 401 | log.warn("Incomplete Filtering Objective. " + |
| 402 | "VLAN Table cannot be programmed for {}", deviceId); |
| 403 | return ImmutableList.of(); |
| 404 | } else { |
| 405 | outerSelector.matchInPort(portNumber); |
| 406 | innerSelector.matchInPort(portNumber); |
| 407 | outerTreatment.transition(VLAN_1_TABLE); |
| 408 | innerTreatment.transition(TMAC_TABLE); |
| 409 | |
| 410 | if (requireVlanExtensions()) { |
| 411 | OfdpaMatchVlanVid ofdpaOuterMatchVlanVid = new OfdpaMatchVlanVid(outerVlanId); |
| 412 | outerSelector.extension(ofdpaOuterMatchVlanVid, deviceId); |
| 413 | OfdpaMatchVlanVid ofdpaInnerMatchVlanVid = new OfdpaMatchVlanVid(innerVlanId); |
| 414 | innerSelector.extension(ofdpaInnerMatchVlanVid, deviceId); |
| 415 | } else { |
| 416 | outerSelector.matchVlanId(outerVlanId); |
| 417 | innerSelector.matchVlanId(innerVlanId); |
| 418 | } |
| 419 | |
| 420 | innerSelector.extension(new Ofdpa3MatchOvid(outerVlanId), deviceId); |
| 421 | outerTreatment.extension(new Ofdpa3SetOvid(outerVlanId), deviceId); |
| 422 | if (popVlan) { |
| 423 | outerTreatment.popVlan(); |
| 424 | } |
| 425 | } |
| 426 | FlowRule outerRule = DefaultFlowRule.builder() |
| 427 | .forDevice(deviceId) |
| 428 | .withSelector(outerSelector.build()) |
| 429 | .withTreatment(outerTreatment.build()) |
| 430 | .withPriority(DEFAULT_PRIORITY) |
| 431 | .fromApp(applicationId) |
| 432 | .makePermanent() |
| 433 | .forTable(VLAN_TABLE) |
| 434 | .build(); |
| 435 | FlowRule innerRule = DefaultFlowRule.builder() |
| 436 | .forDevice(deviceId) |
| 437 | .withSelector(innerSelector.build()) |
| 438 | .withTreatment(innerTreatment.build()) |
| 439 | .withPriority(DEFAULT_PRIORITY) |
| 440 | .fromApp(applicationId) |
| 441 | .makePermanent() |
| 442 | .forTable(VLAN_1_TABLE) |
| 443 | .build(); |
| 444 | |
| 445 | return ImmutableList.of(outerRule, innerRule); |
| 446 | } |
| 447 | |
| 448 | /** |
| 449 | * Determines if the filtering objective will be used for double-tagged packets. |
| 450 | * |
| 451 | * @param fob Filtering objective |
| 452 | * @return True if the objective was created for double-tagged packets, false otherwise. |
| 453 | */ |
| 454 | private boolean isDoubleTagged(FilteringObjective fob) { |
| 455 | return fob.meta() != null && |
| 456 | fob.meta().allInstructions().stream().anyMatch(inst -> inst.type() == L2MODIFICATION |
| 457 | && ((L2ModificationInstruction) inst).subtype() == L2SubType.VLAN_POP) && |
| 458 | fob.conditions().stream().filter(criterion -> criterion.type() == VLAN_VID).count() == 2; |
Pier Ventre | 42287df | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | /** |
Andreas Pantelopoulos | 27532cd | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 462 | * Determines if the filtering objective will be used for a pseudowire. |
| 463 | * |
| 464 | * @param filteringObjective |
| 465 | * @return True if objective was created for a pseudowire, false otherwise. |
| 466 | */ |
| 467 | private boolean isPseudowire(FilteringObjective filteringObjective) { |
| 468 | |
| 469 | |
| 470 | if (filteringObjective.meta() != null) { |
| 471 | |
| 472 | TrafficTreatment treatment = filteringObjective.meta(); |
| 473 | for (Instruction instr : treatment.immediate()) { |
| 474 | if (instr.type().equals(Instruction.Type.L2MODIFICATION)) { |
| 475 | |
| 476 | L2ModificationInstruction l2Instr = (L2ModificationInstruction) instr; |
| 477 | if (l2Instr.subtype().equals(L2SubType.TUNNEL_ID)) { |
| 478 | return true; |
| 479 | } |
| 480 | } |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | return false; |
| 485 | } |
| 486 | |
| 487 | /** |
Pier Ventre | 42287df | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 488 | * 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 Pantelopoulos | 27532cd | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 495 | * @param egressVlan the vlan to modify, was passed in metadata |
Pier Ventre | 42287df | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 496 | * @return a list of flow rules to install |
| 497 | */ |
| 498 | private List<FlowRule> processPwFilter(PortCriterion portCriterion, |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 499 | VlanIdCriterion innerVlanIdCriterion, |
| 500 | VlanIdCriterion outerVlanIdCriterion, |
| 501 | long tunnelId, |
Andreas Pantelopoulos | 27532cd | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 502 | ApplicationId applicationId, |
| 503 | VlanId egressVlan) { |
| 504 | |
Pier Ventre | 42287df | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 505 | 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 Ventre | 42287df | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 509 | |
Andreas Pantelopoulos | 27532cd | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 510 | 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() |
| 571 | // .pushVlan() |
| 572 | .setVlanId(egressVlan) |
| 573 | .extension(new Ofdpa3SetMplsType(VPWS), deviceId) |
| 574 | .extension(new Ofdpa3SetMplsL2Port(mplsLogicalPort), deviceId) |
| 575 | .setTunnelId(tunnelId) |
| 576 | .transition(MPLS_L2_PORT_FLOW_TABLE); |
| 577 | |
| 578 | vlan1FlowRule = DefaultFlowRule.builder() |
| 579 | .forDevice(deviceId) |
| 580 | .withSelector(singleVlanSelector.build()) |
| 581 | .withTreatment(singleVlanTreatment.build()) |
| 582 | .withPriority(DEFAULT_PRIORITY) |
| 583 | .fromApp(applicationId) |
| 584 | .makePermanent() |
| 585 | .forTable(VLAN_TABLE) |
| 586 | .build(); |
| 587 | |
| 588 | return ImmutableList.of(vlan1FlowRule); |
| 589 | } else if ((innerVlanIdCriterion.vlanId().equals(VlanId.NONE)) |
| 590 | && (outerVlanIdCriterion.vlanId().equals(VlanId.NONE))) { |
| 591 | |
| 592 | TrafficSelector.Builder singleVlanSelector = DefaultTrafficSelector.builder() |
| 593 | .matchInPort(portCriterion.port()) |
| 594 | .matchVlanId(innerVlanIdCriterion.vlanId()); |
| 595 | |
| 596 | TrafficTreatment.Builder singleVlanTreatment = DefaultTrafficTreatment.builder() |
| 597 | .extension(new Ofdpa3SetMplsType(VPWS), deviceId) |
| 598 | .extension(new Ofdpa3SetMplsL2Port(mplsLogicalPort), deviceId) |
| 599 | .setTunnelId(tunnelId) |
| 600 | .transition(MPLS_L2_PORT_FLOW_TABLE); |
| 601 | |
| 602 | vlan1FlowRule = DefaultFlowRule.builder() |
| 603 | .forDevice(deviceId) |
| 604 | .withSelector(singleVlanSelector.build()) |
| 605 | .withTreatment(singleVlanTreatment.build()) |
| 606 | .withPriority(DEFAULT_PRIORITY) |
| 607 | .fromApp(applicationId) |
| 608 | .makePermanent() |
| 609 | .forTable(VLAN_TABLE) |
| 610 | .build(); |
| 611 | |
| 612 | return ImmutableList.of(vlan1FlowRule); |
| 613 | } else { |
| 614 | // failure... |
| 615 | return Collections.emptyList(); |
| 616 | } |
Pier Ventre | 42287df | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | @Override |
Charles Chan | f9e9865 | 2016-09-07 16:54:23 -0700 | [diff] [blame] | 620 | protected Collection<FlowRule> processEthTypeSpecific(ForwardingObjective fwd) { |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 621 | // if its not-bos, we go to MPLS_TYPE_TABLE regardless of whether we pop or swap |
| 622 | // if it is bos, we go to MPLS_TYPE_TABLE only if we swap |
| 623 | if (isNotMplsBos(fwd.selector()) |
| 624 | || (isMplsBos(fwd.selector()) && !isMplsPop(fwd))) { |
Pier Ventre | 140a894 | 2016-11-02 07:26:38 -0700 | [diff] [blame] | 625 | return processEthTypeSpecificInternal(fwd, true, MPLS_TYPE_TABLE); |
| 626 | } |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 627 | // if it is bos, and we pop, we go to MPLS_L3_TYPE_TABLE |
Pier Ventre | 140a894 | 2016-11-02 07:26:38 -0700 | [diff] [blame] | 628 | return processEthTypeSpecificInternal(fwd, true, MPLS_L3_TYPE_TABLE); |
Charles Chan | f9e9865 | 2016-09-07 16:54:23 -0700 | [diff] [blame] | 629 | } |
Pier Ventre | 42287df | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 630 | |
| 631 | @Override |
| 632 | protected Collection<FlowRule> processVersatile(ForwardingObjective fwd) { |
| 633 | // We use the tunnel id to identify pw related flows. |
Pier Ventre | 70d53ba | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 634 | // Looking for the fwd objective of the initiation. |
Pier Ventre | 42287df | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 635 | TunnelIdCriterion tunnelIdCriterion = (TunnelIdCriterion) fwd.selector() |
| 636 | .getCriterion(TUNNEL_ID); |
| 637 | if (tunnelIdCriterion != null) { |
Pier Ventre | 70d53ba | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 638 | return processInitPwVersatile(fwd); |
| 639 | } |
| 640 | // Looking for the fwd objective of the termination. |
| 641 | ModTunnelIdInstruction modTunnelIdInstruction = getModTunnelIdInstruction(fwd.treatment()); |
| 642 | OutputInstruction outputInstruction = getOutputInstruction(fwd.treatment()); |
| 643 | if (modTunnelIdInstruction != null && outputInstruction != null) { |
| 644 | return processTermPwVersatile(fwd, modTunnelIdInstruction, outputInstruction); |
Pier Ventre | 42287df | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 645 | } |
| 646 | // If it is not a pseudo wire flow we fall back |
| 647 | // to the OFDPA 2.0 pipeline. |
| 648 | return super.processVersatile(fwd); |
| 649 | } |
| 650 | |
Pier Ventre | 70d53ba | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 651 | private Collection<FlowRule> processTermPwVersatile(ForwardingObjective forwardingObjective, |
| 652 | ModTunnelIdInstruction modTunnelIdInstruction, |
| 653 | OutputInstruction outputInstruction) { |
| 654 | TrafficTreatment.Builder flowTreatment; |
| 655 | TrafficSelector.Builder flowSelector; |
| 656 | // We divide the mpls actions from the tunnel actions. We need |
| 657 | // this to order the actions in the final treatment. |
| 658 | TrafficTreatment.Builder mplsTreatment = DefaultTrafficTreatment.builder(); |
| 659 | createMplsTreatment(forwardingObjective.treatment(), mplsTreatment); |
| 660 | // The match of the forwarding objective is ready to go. |
| 661 | flowSelector = DefaultTrafficSelector.builder(forwardingObjective.selector()); |
| 662 | // We verify the tunnel id and mpls port are correct. |
| 663 | long tunnelId = MPLS_TUNNEL_ID_BASE | modTunnelIdInstruction.tunnelId(); |
| 664 | if (tunnelId > MPLS_TUNNEL_ID_MAX) { |
| 665 | log.error("Pw Versatile Forwarding Objective must include tunnel id < {}", |
| 666 | MPLS_TUNNEL_ID_MAX); |
| 667 | fail(forwardingObjective, ObjectiveError.BADPARAMS); |
| 668 | return Collections.emptySet(); |
| 669 | } |
| 670 | // 0x0002XXXX is NNI interface. |
| 671 | int mplsLogicalPort = ((int) outputInstruction.port().toLong()) | MPLS_NNI_PORT_BASE; |
| 672 | if (mplsLogicalPort > MPLS_NNI_PORT_MAX) { |
| 673 | log.error("Pw Versatile Forwarding Objective invalid logical port {}", |
| 674 | mplsLogicalPort); |
| 675 | fail(forwardingObjective, ObjectiveError.BADPARAMS); |
| 676 | return Collections.emptySet(); |
| 677 | } |
| 678 | // Next id cannot be null. |
| 679 | if (forwardingObjective.nextId() == null) { |
| 680 | log.error("Pw Versatile Forwarding Objective must contain nextId ", |
| 681 | forwardingObjective.nextId()); |
| 682 | fail(forwardingObjective, ObjectiveError.BADPARAMS); |
| 683 | return Collections.emptySet(); |
| 684 | } |
| 685 | // We retrieve the l2 interface group and point the mpls |
| 686 | // flow to this. |
| 687 | NextGroup next = getGroupForNextObjective(forwardingObjective.nextId()); |
| 688 | if (next == null) { |
| 689 | log.warn("next-id:{} not found in dev:{}", forwardingObjective.nextId(), deviceId); |
| 690 | fail(forwardingObjective, ObjectiveError.GROUPMISSING); |
| 691 | return Collections.emptySet(); |
| 692 | } |
| 693 | List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data()); |
| 694 | Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst()); |
| 695 | if (group == null) { |
| 696 | log.warn("Group with key:{} for next-id:{} not found in dev:{}", |
| 697 | gkeys.get(0).peekFirst(), forwardingObjective.nextId(), deviceId); |
| 698 | fail(forwardingObjective, ObjectiveError.GROUPMISSING); |
| 699 | return Collections.emptySet(); |
| 700 | } |
| 701 | // We prepare the treatment for the mpls flow table. |
| 702 | // The order of the actions has to be strictly this |
| 703 | // according to the OFDPA 2.0 specification. |
| 704 | flowTreatment = DefaultTrafficTreatment.builder(mplsTreatment.build()); |
| 705 | flowTreatment.extension(new Ofdpa3PopCw(), deviceId); |
Pier Luigi | 3bfe32c | 2017-01-30 09:47:36 -0800 | [diff] [blame] | 706 | // Even though the specification and the xml/json files |
| 707 | // specify is allowed, the switch rejects the flow. In the |
| 708 | // OFDPA 3.0 EA0 version was necessary |
| 709 | //flowTreatment.popVlan(); |
Pier Ventre | 70d53ba | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 710 | flowTreatment.extension(new Ofdpa3PopL2Header(), deviceId); |
| 711 | flowTreatment.setTunnelId(tunnelId); |
| 712 | flowTreatment.extension(new Ofdpa3SetMplsL2Port(mplsLogicalPort), deviceId); |
| 713 | flowTreatment.extension(new Ofdpa3SetMplsType(VPWS), deviceId); |
| 714 | flowTreatment.transition(MPLS_TYPE_TABLE); |
| 715 | flowTreatment.deferred().group(group.id()); |
| 716 | // We prepare the flow rule for the mpls table. |
| 717 | FlowRule.Builder ruleBuilder = DefaultFlowRule.builder() |
| 718 | .fromApp(forwardingObjective.appId()) |
| 719 | .withPriority(forwardingObjective.priority()) |
| 720 | .forDevice(deviceId) |
| 721 | .withSelector(flowSelector.build()) |
| 722 | .withTreatment(flowTreatment.build()) |
| 723 | .makePermanent() |
| 724 | .forTable(MPLS_TABLE_1); |
| 725 | return Collections.singletonList(ruleBuilder.build()); |
| 726 | } |
| 727 | |
Pier Ventre | 42287df | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 728 | /** |
| 729 | * Helper method to process the pw forwarding objectives. |
| 730 | * |
| 731 | * @param forwardingObjective the fw objective to process |
| 732 | * @return a singleton list of flow rule |
| 733 | */ |
Pier Ventre | 70d53ba | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 734 | private Collection<FlowRule> processInitPwVersatile(ForwardingObjective forwardingObjective) { |
Pier Ventre | 42287df | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 735 | // We retrieve the matching criteria for mpls l2 port. |
| 736 | TunnelIdCriterion tunnelIdCriterion = (TunnelIdCriterion) forwardingObjective.selector() |
| 737 | .getCriterion(TUNNEL_ID); |
| 738 | PortCriterion portCriterion = (PortCriterion) forwardingObjective.selector() |
| 739 | .getCriterion(IN_PORT); |
| 740 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 741 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
| 742 | int mplsLogicalPort; |
| 743 | long tunnelId; |
| 744 | // Mpls tunnel ids according to the OFDPA manual have to be |
| 745 | // in the range [2^17-1, 2^16]. |
| 746 | tunnelId = MPLS_TUNNEL_ID_BASE | tunnelIdCriterion.tunnelId(); |
| 747 | if (tunnelId > MPLS_TUNNEL_ID_MAX) { |
| 748 | log.error("Pw Versatile Forwarding Objective must include tunnel id < {}", |
| 749 | MPLS_TUNNEL_ID_MAX); |
| 750 | fail(forwardingObjective, ObjectiveError.BADPARAMS); |
| 751 | return Collections.emptySet(); |
| 752 | } |
| 753 | // Port has not been null. |
| 754 | if (portCriterion == null) { |
| 755 | log.error("Pw Versatile Forwarding Objective must include port"); |
| 756 | fail(forwardingObjective, ObjectiveError.BADPARAMS); |
| 757 | return Collections.emptySet(); |
| 758 | } |
| 759 | // 0x0000XXXX is UNI interface. |
Pier Ventre | 70d53ba | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 760 | if (portCriterion.port().toLong() > MPLS_UNI_PORT_MAX) { |
Pier Ventre | 42287df | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 761 | log.error("Pw Versatile Forwarding Objective invalid logical port {}", |
| 762 | portCriterion.port().toLong()); |
| 763 | fail(forwardingObjective, ObjectiveError.BADPARAMS); |
| 764 | return Collections.emptySet(); |
| 765 | } |
| 766 | mplsLogicalPort = ((int) portCriterion.port().toLong()); |
| 767 | if (forwardingObjective.nextId() == null) { |
| 768 | log.error("Pw Versatile Forwarding Objective must contain nextId ", |
| 769 | forwardingObjective.nextId()); |
| 770 | fail(forwardingObjective, ObjectiveError.BADPARAMS); |
| 771 | return Collections.emptySet(); |
| 772 | } |
| 773 | // We don't expect a treatment. |
| 774 | if (forwardingObjective.treatment() != null && |
| 775 | !forwardingObjective.treatment().equals(DefaultTrafficTreatment.emptyTreatment())) { |
| 776 | log.error("Pw Versatile Forwarding Objective cannot contain a treatment ", |
| 777 | forwardingObjective.nextId()); |
| 778 | fail(forwardingObjective, ObjectiveError.BADPARAMS); |
| 779 | return Collections.emptySet(); |
| 780 | } |
| 781 | // We retrieve the l2 vpn group and point the mpls |
| 782 | // l2 port to this. |
| 783 | NextGroup next = getGroupForNextObjective(forwardingObjective.nextId()); |
| 784 | if (next == null) { |
| 785 | log.warn("next-id:{} not found in dev:{}", forwardingObjective.nextId(), deviceId); |
| 786 | fail(forwardingObjective, ObjectiveError.GROUPMISSING); |
| 787 | return Collections.emptySet(); |
| 788 | } |
| 789 | List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data()); |
| 790 | Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst()); |
| 791 | if (group == null) { |
| 792 | log.warn("Group with key:{} for next-id:{} not found in dev:{}", |
| 793 | gkeys.get(0).peekFirst(), forwardingObjective.nextId(), deviceId); |
| 794 | fail(forwardingObjective, ObjectiveError.GROUPMISSING); |
| 795 | return Collections.emptySet(); |
| 796 | } |
| 797 | // We prepare the flow rule for the mpls l2 port table. |
| 798 | selector.matchTunnelId(tunnelId); |
| 799 | selector.extension(new Ofdpa3MatchMplsL2Port(mplsLogicalPort), deviceId); |
| 800 | // This should not be necessary but without we receive an error |
| 801 | treatment.extension(new Ofdpa3SetQosIndex(0), deviceId); |
| 802 | treatment.transition(MPLS_L2_PORT_PCP_TRUST_FLOW_TABLE); |
| 803 | treatment.deferred().group(group.id()); |
| 804 | FlowRule.Builder ruleBuilder = DefaultFlowRule.builder() |
| 805 | .fromApp(forwardingObjective.appId()) |
| 806 | .withPriority(MPLS_L2_PORT_PRIORITY) |
| 807 | .forDevice(deviceId) |
| 808 | .withSelector(selector.build()) |
| 809 | .withTreatment(treatment.build()) |
| 810 | .makePermanent() |
| 811 | .forTable(MPLS_L2_PORT_FLOW_TABLE); |
| 812 | return Collections.singletonList(ruleBuilder.build()); |
| 813 | } |
Pier Ventre | 70d53ba | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 814 | |
| 815 | /** |
| 816 | * Utility function to get the mod tunnel id instruction |
| 817 | * if present. |
| 818 | * |
| 819 | * @param treatment the treatment to analyze |
| 820 | * @return the mod tunnel id instruction if present, |
| 821 | * otherwise null |
| 822 | */ |
| 823 | private ModTunnelIdInstruction getModTunnelIdInstruction(TrafficTreatment treatment) { |
Charles Chan | 3bb1170 | 2017-02-16 14:58:58 -0800 | [diff] [blame] | 824 | if (treatment == null) { |
| 825 | return null; |
| 826 | } |
| 827 | |
Pier Ventre | 70d53ba | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 828 | L2ModificationInstruction l2ModificationInstruction; |
| 829 | for (Instruction instruction : treatment.allInstructions()) { |
| 830 | if (instruction.type() == L2MODIFICATION) { |
| 831 | l2ModificationInstruction = (L2ModificationInstruction) instruction; |
| 832 | if (l2ModificationInstruction.subtype() == L2SubType.TUNNEL_ID) { |
| 833 | return (ModTunnelIdInstruction) l2ModificationInstruction; |
| 834 | } |
| 835 | } |
| 836 | } |
| 837 | return null; |
| 838 | } |
| 839 | |
| 840 | /** |
| 841 | * Utility function to get the output instruction |
| 842 | * if present. |
| 843 | * |
| 844 | * @param treatment the treatment to analyze |
| 845 | * @return the output instruction if present, |
| 846 | * otherwise null |
| 847 | */ |
| 848 | private OutputInstruction getOutputInstruction(TrafficTreatment treatment) { |
Saurav Das | 018605f | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 849 | if (treatment == null) { |
| 850 | return null; |
| 851 | } |
| 852 | |
Pier Ventre | 70d53ba | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 853 | for (Instruction instruction : treatment.allInstructions()) { |
| 854 | if (instruction.type() == Instruction.Type.OUTPUT) { |
| 855 | return (OutputInstruction) instruction; |
| 856 | } |
| 857 | } |
| 858 | return null; |
| 859 | } |
| 860 | |
| 861 | /** |
| 862 | * Helper method for dividing the tunnel instructions from the mpls |
| 863 | * instructions. |
| 864 | * |
| 865 | * @param treatment the treatment to analyze |
| 866 | * @param mplsTreatment the mpls treatment builder |
| 867 | */ |
| 868 | private void createMplsTreatment(TrafficTreatment treatment, |
| 869 | TrafficTreatment.Builder mplsTreatment) { |
| 870 | |
| 871 | for (Instruction ins : treatment.allInstructions()) { |
| 872 | |
| 873 | if (ins.type() == Instruction.Type.L2MODIFICATION) { |
| 874 | L2ModificationInstruction l2ins = (L2ModificationInstruction) ins; |
| 875 | switch (l2ins.subtype()) { |
| 876 | // These instructions have to go in the mpls |
| 877 | // treatment. |
| 878 | case TUNNEL_ID: |
| 879 | break; |
| 880 | case DEC_MPLS_TTL: |
| 881 | case MPLS_POP: |
| 882 | mplsTreatment.add(ins); |
| 883 | break; |
| 884 | default: |
| 885 | log.warn("Driver does not handle this type of TrafficTreatment" |
| 886 | + " instruction in nextObjectives: {} - {}", |
| 887 | ins.type(), ins); |
| 888 | break; |
| 889 | } |
| 890 | } else if (ins.type() == Instruction.Type.OUTPUT) { |
| 891 | break; |
| 892 | } else if (ins.type() == Instruction.Type.L3MODIFICATION) { |
| 893 | // We support partially the l3 instructions. |
| 894 | L3ModificationInstruction l3ins = (L3ModificationInstruction) ins; |
| 895 | switch (l3ins.subtype()) { |
| 896 | case TTL_IN: |
| 897 | mplsTreatment.add(ins); |
| 898 | break; |
| 899 | default: |
| 900 | log.warn("Driver does not handle this type of TrafficTreatment" |
| 901 | + " instruction in nextObjectives: {} - {}", |
| 902 | ins.type(), ins); |
| 903 | } |
| 904 | |
| 905 | } else { |
| 906 | log.warn("Driver does not handle this type of TrafficTreatment" |
| 907 | + " instruction in nextObjectives: {} - {}", |
| 908 | ins.type(), ins); |
| 909 | } |
| 910 | } |
| 911 | } |
Charles Chan | f9e9865 | 2016-09-07 16:54:23 -0700 | [diff] [blame] | 912 | } |