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 | |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 19 | import org.onosproject.net.DeviceId; |
Yi Tseng | 4fd2843 | 2018-02-01 14:48:03 -0800 | [diff] [blame] | 20 | import org.onosproject.net.driver.Driver; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 21 | import org.onosproject.net.flow.DefaultFlowRule; |
| 22 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 23 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 24 | import org.onosproject.net.flow.TrafficSelector; |
| 25 | import org.onosproject.net.flow.TrafficTreatment; |
| 26 | import org.onosproject.net.flow.criteria.PiCriterion; |
| 27 | import org.onosproject.net.flow.instructions.Instruction; |
| 28 | import org.onosproject.net.flow.instructions.Instructions.OutputInstruction; |
Yi Tseng | 4fd2843 | 2018-02-01 14:48:03 -0800 | [diff] [blame] | 29 | import org.onosproject.net.flowobjective.DefaultNextObjective; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 30 | import org.onosproject.net.flowobjective.NextObjective; |
Yi Tseng | 6e9b6f5 | 2018-02-27 10:40:51 +0100 | [diff] [blame] | 31 | import org.onosproject.net.flowobjective.Objective; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 32 | import org.onosproject.net.flowobjective.ObjectiveError; |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 33 | import org.onosproject.net.group.DefaultGroupBucket; |
| 34 | import org.onosproject.net.group.DefaultGroupDescription; |
| 35 | import org.onosproject.net.group.GroupBucket; |
| 36 | import org.onosproject.net.group.GroupBuckets; |
| 37 | import org.onosproject.net.group.GroupDescription; |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 38 | import org.onosproject.net.pi.runtime.PiActionGroupId; |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 39 | import org.onosproject.net.pi.runtime.PiGroupKey; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 40 | import org.slf4j.Logger; |
| 41 | |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 42 | import java.util.List; |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 43 | import java.util.stream.Collectors; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 44 | |
Yi Tseng | f55eaa8 | 2017-11-29 15:51:28 -0800 | [diff] [blame] | 45 | import static org.onosproject.pipelines.fabric.FabricConstants.ACT_PRF_NEXT_ECMP_SELECTOR_ID; |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 46 | import static org.onosproject.pipelines.fabric.FabricConstants.HF_FABRIC_METADATA_NEXT_ID_ID; |
| 47 | import static org.onosproject.pipelines.fabric.FabricConstants.TBL_HASHED_ID; |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 48 | import static org.onosproject.pipelines.fabric.FabricConstants.TBL_SIMPLE_ID; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 49 | import static org.slf4j.LoggerFactory.getLogger; |
| 50 | |
| 51 | /** |
| 52 | * Handling next objective for fabric pipeliner. |
| 53 | */ |
| 54 | public class FabricNextPipeliner { |
| 55 | private static final Logger log = getLogger(FabricNextPipeliner.class); |
Yi Tseng | 4fd2843 | 2018-02-01 14:48:03 -0800 | [diff] [blame] | 56 | private static final String NO_HASHED_TABLE = "noHashedTable"; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 57 | |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 58 | protected DeviceId deviceId; |
Yi Tseng | 4fd2843 | 2018-02-01 14:48:03 -0800 | [diff] [blame] | 59 | protected Driver driver; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 60 | |
Yi Tseng | 4fd2843 | 2018-02-01 14:48:03 -0800 | [diff] [blame] | 61 | public FabricNextPipeliner(DeviceId deviceId, Driver driver) { |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 62 | this.deviceId = deviceId; |
Yi Tseng | 4fd2843 | 2018-02-01 14:48:03 -0800 | [diff] [blame] | 63 | this.driver = driver; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | public PipelinerTranslationResult next(NextObjective nextObjective) { |
| 67 | PipelinerTranslationResult.Builder resultBuilder = PipelinerTranslationResult.builder(); |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 68 | |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 69 | switch (nextObjective.type()) { |
| 70 | case SIMPLE: |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 71 | processSimpleNext(nextObjective, resultBuilder); |
| 72 | break; |
| 73 | case HASHED: |
| 74 | processHashedNext(nextObjective, resultBuilder); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 75 | break; |
| 76 | default: |
| 77 | log.warn("Unsupported next type {}", nextObjective); |
| 78 | resultBuilder.setError(ObjectiveError.UNSUPPORTED); |
| 79 | break; |
| 80 | } |
| 81 | |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 82 | return resultBuilder.build(); |
| 83 | } |
| 84 | |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 85 | private void processSimpleNext(NextObjective next, |
| 86 | PipelinerTranslationResult.Builder resultBuilder) { |
| 87 | |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 88 | if (next.next().size() > 1) { |
| 89 | log.warn("Only one treatment in simple next objective"); |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 90 | resultBuilder.setError(ObjectiveError.BADPARAMS); |
| 91 | return; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | TrafficSelector selector = buildNextIdSelector(next.id()); |
| 95 | TrafficTreatment treatment = next.next().iterator().next(); |
| 96 | OutputInstruction outputInst = treatment.allInstructions() |
| 97 | .stream() |
| 98 | .filter(inst -> inst.type() == Instruction.Type.OUTPUT) |
| 99 | .map(inst -> (OutputInstruction) inst) |
| 100 | .findFirst() |
| 101 | .orElse(null); |
| 102 | |
| 103 | if (outputInst == null) { |
| 104 | log.warn("At least one output instruction in simple next objective"); |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 105 | resultBuilder.setError(ObjectiveError.BADPARAMS); |
| 106 | return; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 107 | } |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 108 | resultBuilder.addFlowRule(DefaultFlowRule.builder() |
| 109 | .withSelector(selector) |
| 110 | .withTreatment(treatment) |
| 111 | .forTable(TBL_SIMPLE_ID) |
| 112 | .makePermanent() |
| 113 | .withPriority(next.priority()) |
| 114 | .forDevice(deviceId) |
| 115 | .fromApp(next.appId()) |
| 116 | .build()); |
| 117 | } |
| 118 | |
| 119 | private void processHashedNext(NextObjective nextObjective, PipelinerTranslationResult.Builder resultBuilder) { |
Yi Tseng | 4fd2843 | 2018-02-01 14:48:03 -0800 | [diff] [blame] | 120 | boolean noHashedTable = Boolean.parseBoolean(driver.getProperty(NO_HASHED_TABLE)); |
| 121 | |
| 122 | if (noHashedTable) { |
| 123 | if (nextObjective.next().isEmpty()) { |
| 124 | return; |
| 125 | } |
| 126 | // use first action if not support hashed group |
| 127 | TrafficTreatment treatment = nextObjective.next().iterator().next(); |
| 128 | |
| 129 | NextObjective.Builder simpleNext = DefaultNextObjective.builder() |
| 130 | .addTreatment(treatment) |
| 131 | .withId(nextObjective.id()) |
| 132 | .fromApp(nextObjective.appId()) |
| 133 | .makePermanent() |
| 134 | .withMeta(nextObjective.meta()) |
| 135 | .withPriority(nextObjective.priority()) |
| 136 | .withType(NextObjective.Type.SIMPLE); |
| 137 | |
| 138 | if (nextObjective.context().isPresent()) { |
| 139 | processSimpleNext(simpleNext.add(nextObjective.context().get()), resultBuilder); |
| 140 | } else { |
| 141 | processSimpleNext(simpleNext.add(), resultBuilder); |
| 142 | } |
| 143 | return; |
| 144 | } |
| 145 | |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 146 | // create hash groups |
| 147 | int groupId = nextObjective.id(); |
| 148 | List<GroupBucket> bucketList = nextObjective.next().stream() |
| 149 | .map(DefaultGroupBucket::createSelectGroupBucket) |
| 150 | .collect(Collectors.toList()); |
| 151 | |
| 152 | if (bucketList.size() != nextObjective.next().size()) { |
| 153 | // some action not converted |
| 154 | // set error |
| 155 | log.warn("Expected bucket size {}, got {}", nextObjective.next().size(), bucketList.size()); |
| 156 | resultBuilder.setError(ObjectiveError.BADPARAMS); |
| 157 | return; |
| 158 | } |
| 159 | |
| 160 | GroupBuckets buckets = new GroupBuckets(bucketList); |
| 161 | PiGroupKey groupKey = new PiGroupKey(TBL_HASHED_ID, |
Yi Tseng | f55eaa8 | 2017-11-29 15:51:28 -0800 | [diff] [blame] | 162 | ACT_PRF_NEXT_ECMP_SELECTOR_ID, |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 163 | groupId); |
| 164 | |
| 165 | resultBuilder.addGroup(new DefaultGroupDescription(deviceId, |
| 166 | GroupDescription.Type.SELECT, |
| 167 | buckets, |
| 168 | groupKey, |
| 169 | groupId, |
| 170 | nextObjective.appId())); |
| 171 | |
| 172 | // flow |
Yi Tseng | 6e9b6f5 | 2018-02-27 10:40:51 +0100 | [diff] [blame] | 173 | // If operation is ADD_TO_EXIST or REMOVE_FROM_EXIST, means we modify |
| 174 | // group buckets only, no changes for flow rule |
| 175 | if (nextObjective.op() == Objective.Operation.ADD_TO_EXISTING || |
| 176 | nextObjective.op() == Objective.Operation.REMOVE_FROM_EXISTING) { |
| 177 | return; |
| 178 | } |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 179 | TrafficSelector selector = buildNextIdSelector(nextObjective.id()); |
| 180 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 181 | .piTableAction(PiActionGroupId.of(nextObjective.id())) |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 182 | .build(); |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 183 | |
| 184 | resultBuilder.addFlowRule(DefaultFlowRule.builder() |
| 185 | .withSelector(selector) |
| 186 | .withTreatment(treatment) |
| 187 | .forTable(TBL_HASHED_ID) |
| 188 | .makePermanent() |
| 189 | .withPriority(nextObjective.priority()) |
| 190 | .forDevice(deviceId) |
| 191 | .fromApp(nextObjective.appId()) |
| 192 | .build()); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | private TrafficSelector buildNextIdSelector(int nextId) { |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 196 | PiCriterion nextIdCriterion = PiCriterion.builder() |
| 197 | .matchExact(HF_FABRIC_METADATA_NEXT_ID_ID, nextId) |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 198 | .build(); |
| 199 | return DefaultTrafficSelector.builder() |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 200 | .matchPi(nextIdCriterion) |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 201 | .build(); |
| 202 | } |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 203 | } |