Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 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 | |
| 17 | package org.onosproject.pipelines.fabric.pipeliner; |
| 18 | |
| 19 | import com.google.common.collect.ImmutableSet; |
| 20 | import org.onlab.packet.MacAddress; |
| 21 | import org.onlab.packet.VlanId; |
| 22 | import org.onlab.util.ImmutableByteSequence; |
| 23 | import org.onosproject.net.DeviceId; |
| 24 | import org.onosproject.net.flow.DefaultFlowRule; |
| 25 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 26 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 27 | import org.onosproject.net.flow.FlowRule; |
| 28 | import org.onosproject.net.flow.TrafficSelector; |
| 29 | import org.onosproject.net.flow.TrafficTreatment; |
| 30 | import org.onosproject.net.flow.criteria.Criterion; |
| 31 | import org.onosproject.net.flow.criteria.EthCriterion; |
Yi Tseng | dbe0560 | 2017-11-17 18:02:43 -0800 | [diff] [blame] | 32 | import org.onosproject.net.flow.criteria.IPCriterion; |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 33 | import org.onosproject.net.flow.criteria.MplsCriterion; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 34 | import org.onosproject.net.flow.criteria.VlanIdCriterion; |
| 35 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 36 | import org.onosproject.net.flowobjective.ObjectiveError; |
| 37 | import org.onosproject.net.pi.runtime.PiAction; |
| 38 | import org.onosproject.net.pi.runtime.PiActionParam; |
| 39 | import org.onosproject.pipelines.fabric.FabricConstants; |
| 40 | import org.slf4j.Logger; |
| 41 | |
| 42 | import java.util.Set; |
| 43 | |
| 44 | import static com.google.common.base.Preconditions.checkNotNull; |
| 45 | import static org.slf4j.LoggerFactory.getLogger; |
| 46 | |
| 47 | /** |
| 48 | * Handling forwarding objective for fabric pipeliner. |
| 49 | */ |
| 50 | public class FabricForwardingPipeliner { |
| 51 | private static final Logger log = getLogger(FabricForwardingPipeliner.class); |
| 52 | |
| 53 | protected DeviceId deviceId; |
| 54 | |
| 55 | public FabricForwardingPipeliner(DeviceId deviceId) { |
| 56 | this.deviceId = deviceId; |
| 57 | } |
| 58 | |
| 59 | public PipelinerTranslationResult forward(ForwardingObjective forwardObjective) { |
| 60 | PipelinerTranslationResult.Builder resultBuilder = PipelinerTranslationResult.builder(); |
| 61 | if (forwardObjective.flag() == ForwardingObjective.Flag.VERSATILE) { |
| 62 | processVersatileFwd(forwardObjective, resultBuilder); |
| 63 | } else { |
| 64 | processSpecificFwd(forwardObjective, resultBuilder); |
| 65 | } |
| 66 | return resultBuilder.build(); |
| 67 | } |
| 68 | |
| 69 | private void processVersatileFwd(ForwardingObjective fwd, |
| 70 | PipelinerTranslationResult.Builder resultBuilder) { |
Yi Tseng | c6844f5 | 2017-12-19 11:58:25 -0800 | [diff] [blame] | 71 | // TODO: Move IPv6 match to different ACL table |
| 72 | |
| 73 | boolean unsupported = fwd.selector().criteria().stream() |
| 74 | .anyMatch(criterion -> criterion.type() == Criterion.Type.IPV6_DST); |
| 75 | unsupported |= fwd.selector().criteria().stream() |
| 76 | .anyMatch(criterion -> criterion.type() == Criterion.Type.IPV6_SRC); |
| 77 | |
| 78 | if (unsupported) { |
| 79 | resultBuilder.setError(ObjectiveError.UNSUPPORTED); |
| 80 | return; |
| 81 | } |
| 82 | |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 83 | // program ACL table only |
| 84 | FlowRule flowRule = DefaultFlowRule.builder() |
| 85 | .withSelector(fwd.selector()) |
| 86 | .withTreatment(fwd.treatment()) |
| 87 | .forTable(FabricConstants.TBL_ACL_ID) |
| 88 | .withPriority(fwd.priority()) |
| 89 | .forDevice(deviceId) |
| 90 | .makePermanent() |
| 91 | .fromApp(fwd.appId()) |
| 92 | .build(); |
| 93 | resultBuilder.addFlowRule(flowRule); |
| 94 | } |
| 95 | |
| 96 | private void processSpecificFwd(ForwardingObjective fwd, |
| 97 | PipelinerTranslationResult.Builder resultBuilder) { |
| 98 | TrafficSelector selector = fwd.selector(); |
| 99 | TrafficSelector meta = fwd.meta(); |
| 100 | |
| 101 | ImmutableSet.Builder<Criterion> criterionSetBuilder = ImmutableSet.builder(); |
| 102 | criterionSetBuilder.addAll(selector.criteria()); |
| 103 | |
| 104 | if (meta != null) { |
| 105 | criterionSetBuilder.addAll(meta.criteria()); |
| 106 | } |
| 107 | |
| 108 | Set<Criterion> criteria = criterionSetBuilder.build(); |
| 109 | |
| 110 | VlanIdCriterion vlanIdCriterion = null; |
| 111 | EthCriterion ethDstCriterion = null; |
Yi Tseng | dbe0560 | 2017-11-17 18:02:43 -0800 | [diff] [blame] | 112 | IPCriterion ipDstCriterion = null; |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 113 | MplsCriterion mplsCriterion = null; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 114 | |
| 115 | for (Criterion criterion : criteria) { |
| 116 | switch (criterion.type()) { |
| 117 | case ETH_DST: |
| 118 | ethDstCriterion = (EthCriterion) criterion; |
| 119 | break; |
| 120 | case VLAN_VID: |
| 121 | vlanIdCriterion = (VlanIdCriterion) criterion; |
| 122 | break; |
Yi Tseng | dbe0560 | 2017-11-17 18:02:43 -0800 | [diff] [blame] | 123 | case IPV4_DST: |
| 124 | ipDstCriterion = (IPCriterion) criterion; |
| 125 | break; |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 126 | case MPLS_LABEL: |
| 127 | mplsCriterion = (MplsCriterion) criterion; |
| 128 | break; |
| 129 | case ETH_TYPE: |
| 130 | case MPLS_BOS: |
| 131 | // do nothing |
| 132 | break; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 133 | default: |
| 134 | log.warn("Unsupported criterion {}", criterion); |
| 135 | break; |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | ForwardingFunctionType forwardingFunctionType = |
| 140 | ForwardingFunctionType.getForwardingFunctionType(fwd); |
| 141 | switch (forwardingFunctionType) { |
| 142 | case L2_UNICAST: |
| 143 | processL2UnicastRule(vlanIdCriterion, ethDstCriterion, fwd, resultBuilder); |
| 144 | break; |
| 145 | case L2_BROADCAST: |
| 146 | processL2BroadcastRule(vlanIdCriterion, fwd, resultBuilder); |
| 147 | break; |
| 148 | case IPV4_UNICAST: |
Yi Tseng | dbe0560 | 2017-11-17 18:02:43 -0800 | [diff] [blame] | 149 | processIpv4UnicastRule(ipDstCriterion, fwd, resultBuilder); |
| 150 | break; |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 151 | case MPLS: |
| 152 | processMplsRule(mplsCriterion, fwd, resultBuilder); |
| 153 | break; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 154 | case IPV4_MULTICAST: |
| 155 | case IPV6_UNICAST: |
| 156 | case IPV6_MULTICAST: |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 157 | default: |
| 158 | log.warn("Unsupported forwarding function type {}", criteria); |
| 159 | resultBuilder.setError(ObjectiveError.UNSUPPORTED); |
| 160 | break; |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | // L2 Unicast: learnt mac address + vlan |
| 165 | private void processL2UnicastRule(VlanIdCriterion vlanIdCriterion, |
| 166 | EthCriterion ethDstCriterion, |
| 167 | ForwardingObjective fwd, |
| 168 | PipelinerTranslationResult.Builder resultBuilder) { |
| 169 | checkNotNull(vlanIdCriterion, "VlanId criterion should not be null"); |
| 170 | checkNotNull(ethDstCriterion, "EthDst criterion should not be null"); |
| 171 | |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 172 | VlanId vlanId = vlanIdCriterion.vlanId(); |
| 173 | MacAddress ethDst = ethDstCriterion.mac(); |
| 174 | |
| 175 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 176 | .matchVlanId(vlanId) |
| 177 | .matchEthDst(ethDst) |
| 178 | .build(); |
Yi Tseng | df3eec5 | 2018-02-15 14:56:02 -0800 | [diff] [blame] | 179 | TrafficTreatment treatment = fwd.treatment(); |
| 180 | if (fwd.nextId() != null) { |
| 181 | treatment = buildSetNextIdTreatment(fwd.nextId()); |
| 182 | } |
| 183 | |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 184 | FlowRule flowRule = DefaultFlowRule.builder() |
| 185 | .withSelector(selector) |
| 186 | .withTreatment(treatment) |
| 187 | .fromApp(fwd.appId()) |
| 188 | .withPriority(fwd.priority()) |
| 189 | .makePermanent() |
| 190 | .forDevice(deviceId) |
| 191 | .forTable(FabricConstants.TBL_BRIDGING_ID) |
| 192 | .build(); |
| 193 | |
| 194 | resultBuilder.addFlowRule(flowRule); |
| 195 | } |
| 196 | |
| 197 | private void processL2BroadcastRule(VlanIdCriterion vlanIdCriterion, |
| 198 | ForwardingObjective fwd, |
| 199 | PipelinerTranslationResult.Builder resultBuilder) { |
| 200 | checkNotNull(vlanIdCriterion, "VlanId criterion should not be null"); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 201 | |
| 202 | VlanId vlanId = vlanIdCriterion.vlanId(); |
| 203 | |
| 204 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 205 | .matchVlanId(vlanId) |
| 206 | .build(); |
Yi Tseng | df3eec5 | 2018-02-15 14:56:02 -0800 | [diff] [blame] | 207 | TrafficTreatment treatment = fwd.treatment(); |
| 208 | if (fwd.nextId() != null) { |
| 209 | treatment = buildSetNextIdTreatment(fwd.nextId()); |
| 210 | } |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 211 | FlowRule flowRule = DefaultFlowRule.builder() |
| 212 | .withSelector(selector) |
| 213 | .withTreatment(treatment) |
| 214 | .fromApp(fwd.appId()) |
| 215 | .withPriority(fwd.priority()) |
| 216 | .makePermanent() |
| 217 | .forDevice(deviceId) |
| 218 | .forTable(FabricConstants.TBL_BRIDGING_ID) |
| 219 | .build(); |
| 220 | |
| 221 | resultBuilder.addFlowRule(flowRule); |
| 222 | } |
| 223 | |
Yi Tseng | dbe0560 | 2017-11-17 18:02:43 -0800 | [diff] [blame] | 224 | private void processIpv4UnicastRule(IPCriterion ipDstCriterion, ForwardingObjective fwd, |
| 225 | PipelinerTranslationResult.Builder resultBuilder) { |
| 226 | checkNotNull(ipDstCriterion, "IP dst criterion should not be null"); |
Yi Tseng | dbe0560 | 2017-11-17 18:02:43 -0800 | [diff] [blame] | 227 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 228 | .matchIPDst(ipDstCriterion.ip()) |
| 229 | .build(); |
Yi Tseng | df3eec5 | 2018-02-15 14:56:02 -0800 | [diff] [blame] | 230 | TrafficTreatment treatment = fwd.treatment(); |
| 231 | if (fwd.nextId() != null) { |
| 232 | treatment = buildSetNextIdTreatment(fwd.nextId()); |
| 233 | } |
Yi Tseng | dbe0560 | 2017-11-17 18:02:43 -0800 | [diff] [blame] | 234 | FlowRule flowRule = DefaultFlowRule.builder() |
| 235 | .withSelector(selector) |
| 236 | .withTreatment(treatment) |
| 237 | .fromApp(fwd.appId()) |
| 238 | .withPriority(fwd.priority()) |
| 239 | .makePermanent() |
| 240 | .forDevice(deviceId) |
| 241 | .forTable(FabricConstants.TBL_UNICAST_V4_ID) |
| 242 | .build(); |
| 243 | |
| 244 | resultBuilder.addFlowRule(flowRule); |
| 245 | } |
| 246 | |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 247 | private void processMplsRule(MplsCriterion mplsCriterion, ForwardingObjective fwd, |
| 248 | PipelinerTranslationResult.Builder resultBuilder) { |
| 249 | checkNotNull(mplsCriterion, "Mpls criterion should not be null"); |
Yi Tseng | df3eec5 | 2018-02-15 14:56:02 -0800 | [diff] [blame] | 250 | TrafficTreatment treatment; |
| 251 | |
| 252 | treatment = fwd.treatment(); |
| 253 | if (fwd.nextId() != null) { |
| 254 | PiActionParam nextIdParam = new PiActionParam(FabricConstants.ACT_PRM_NEXT_ID_ID, |
| 255 | ImmutableByteSequence.copyFrom(fwd.nextId().byteValue())); |
| 256 | PiAction nextIdAction = PiAction.builder() |
Yi Tseng | 27b9bc0 | 2018-04-12 14:52:40 +0800 | [diff] [blame] | 257 | .withId(FabricConstants.ACT_FABRICINGRESS_FORWARDING_POP_MPLS_AND_NEXT_ID) |
Yi Tseng | df3eec5 | 2018-02-15 14:56:02 -0800 | [diff] [blame] | 258 | .withParameter(nextIdParam) |
| 259 | .build(); |
| 260 | treatment = DefaultTrafficTreatment.builder() |
| 261 | .piTableAction(nextIdAction) |
| 262 | .build(); |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 263 | } |
Yi Tseng | df3eec5 | 2018-02-15 14:56:02 -0800 | [diff] [blame] | 264 | |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 265 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 266 | .add(mplsCriterion) |
| 267 | .build(); |
| 268 | |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 269 | FlowRule flowRule = DefaultFlowRule.builder() |
| 270 | .withSelector(selector) |
| 271 | .withTreatment(treatment) |
| 272 | .fromApp(fwd.appId()) |
| 273 | .withPriority(fwd.priority()) |
| 274 | .makePermanent() |
| 275 | .forDevice(deviceId) |
| 276 | .forTable(FabricConstants.TBL_MPLS_ID) |
| 277 | .build(); |
| 278 | |
| 279 | resultBuilder.addFlowRule(flowRule); |
| 280 | } |
| 281 | |
Yi Tseng | df3eec5 | 2018-02-15 14:56:02 -0800 | [diff] [blame] | 282 | /** |
| 283 | * Builds treatment with set_next_id action, returns empty treatment |
| 284 | * if next id is null. |
| 285 | * |
| 286 | * @param nextId the next id for action |
| 287 | * @return treatment with set_next_id action; empty treatment if next id is null |
| 288 | */ |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 289 | private static TrafficTreatment buildSetNextIdTreatment(Integer nextId) { |
| 290 | PiActionParam nextIdParam = new PiActionParam(FabricConstants.ACT_PRM_NEXT_ID_ID, |
| 291 | ImmutableByteSequence.copyFrom(nextId.byteValue())); |
| 292 | PiAction nextIdAction = PiAction.builder() |
Yi Tseng | 27b9bc0 | 2018-04-12 14:52:40 +0800 | [diff] [blame] | 293 | .withId(FabricConstants.ACT_FABRICINGRESS_FORWARDING_SET_NEXT_ID_ID) |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 294 | .withParameter(nextIdParam) |
| 295 | .build(); |
| 296 | |
| 297 | return DefaultTrafficTreatment.builder() |
| 298 | .piTableAction(nextIdAction) |
| 299 | .build(); |
| 300 | } |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 301 | } |