Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [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 | |
| 17 | package org.onosproject.net.pi.impl; |
| 18 | |
| 19 | import com.google.common.collect.Maps; |
| 20 | import com.google.common.collect.Sets; |
| 21 | import org.onlab.util.ImmutableByteSequence; |
| 22 | import org.onosproject.net.Device; |
| 23 | import org.onosproject.net.flow.FlowRule; |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 24 | import org.onosproject.net.flow.TrafficSelector; |
| 25 | import org.onosproject.net.flow.TrafficTreatment; |
| 26 | import org.onosproject.net.flow.criteria.Criterion; |
| 27 | import org.onosproject.net.flow.criteria.PiCriterion; |
| 28 | import org.onosproject.net.flow.instructions.Instruction; |
| 29 | import org.onosproject.net.flow.instructions.PiInstruction; |
| 30 | import org.onosproject.net.pi.model.PiActionModel; |
| 31 | import org.onosproject.net.pi.model.PiActionParamModel; |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 32 | import org.onosproject.net.pi.model.PiMatchFieldId; |
| 33 | import org.onosproject.net.pi.model.PiMatchFieldModel; |
Yi Tseng | 02c4c57 | 2018-01-22 17:52:10 -0800 | [diff] [blame] | 34 | import org.onosproject.net.pi.model.PiMatchType; |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 35 | import org.onosproject.net.pi.model.PiPipeconf; |
| 36 | import org.onosproject.net.pi.model.PiPipelineInterpreter; |
| 37 | import org.onosproject.net.pi.model.PiPipelineModel; |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 38 | import org.onosproject.net.pi.model.PiTableId; |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 39 | import org.onosproject.net.pi.model.PiTableModel; |
| 40 | import org.onosproject.net.pi.runtime.PiAction; |
| 41 | import org.onosproject.net.pi.runtime.PiActionParam; |
| 42 | import org.onosproject.net.pi.runtime.PiExactFieldMatch; |
| 43 | import org.onosproject.net.pi.runtime.PiFieldMatch; |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 44 | import org.onosproject.net.pi.runtime.PiLpmFieldMatch; |
Carmelo Cascone | 0e896a0 | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 45 | import org.onosproject.net.pi.runtime.PiMatchKey; |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 46 | import org.onosproject.net.pi.runtime.PiRangeFieldMatch; |
| 47 | import org.onosproject.net.pi.runtime.PiTableAction; |
| 48 | import org.onosproject.net.pi.runtime.PiTableEntry; |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 49 | import org.onosproject.net.pi.runtime.PiTernaryFieldMatch; |
Carmelo Cascone | 326ad2d | 2017-11-28 18:09:13 -0800 | [diff] [blame] | 50 | import org.onosproject.net.pi.service.PiTranslationException; |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 51 | import org.slf4j.Logger; |
| 52 | import org.slf4j.LoggerFactory; |
| 53 | |
| 54 | import java.util.Collection; |
| 55 | import java.util.Map; |
| 56 | import java.util.Optional; |
| 57 | import java.util.Set; |
| 58 | import java.util.StringJoiner; |
| 59 | |
| 60 | import static java.lang.String.format; |
| 61 | import static org.onlab.util.ImmutableByteSequence.ByteSequenceTrimException; |
| 62 | import static org.onlab.util.ImmutableByteSequence.fit; |
| 63 | import static org.onosproject.net.flow.criteria.Criterion.Type.PROTOCOL_INDEPENDENT; |
| 64 | import static org.onosproject.net.pi.impl.CriterionTranslatorHelper.translateCriterion; |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 65 | import static org.onosproject.net.pi.impl.PiUtils.getInterpreterOrNull; |
| 66 | import static org.onosproject.net.pi.impl.PiUtils.translateTableId; |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 67 | |
| 68 | /** |
| 69 | * Implementation of flow rule translation logic. |
| 70 | */ |
Carmelo Cascone | 326ad2d | 2017-11-28 18:09:13 -0800 | [diff] [blame] | 71 | final class PiFlowRuleTranslatorImpl { |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 72 | |
Carmelo Cascone | a3d811c | 2017-08-22 01:03:45 +0200 | [diff] [blame] | 73 | public static final int MAX_PI_PRIORITY = (int) Math.pow(2, 24); |
Carmelo Cascone | 326ad2d | 2017-11-28 18:09:13 -0800 | [diff] [blame] | 74 | private static final Logger log = LoggerFactory.getLogger(PiFlowRuleTranslatorImpl.class); |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 75 | |
Carmelo Cascone | 326ad2d | 2017-11-28 18:09:13 -0800 | [diff] [blame] | 76 | private PiFlowRuleTranslatorImpl() { |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 77 | // Hide constructor. |
| 78 | } |
| 79 | |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 80 | /** |
| 81 | * Returns a PI table entry equivalent to the given flow rule, for the given pipeconf and device. |
| 82 | * |
| 83 | * @param rule flow rule |
| 84 | * @param pipeconf pipeconf |
| 85 | * @param device device |
| 86 | * @return PI table entry |
| 87 | * @throws PiTranslationException if the flow rule cannot be translated |
| 88 | */ |
| 89 | static PiTableEntry translate(FlowRule rule, PiPipeconf pipeconf, Device device) |
| 90 | throws PiTranslationException { |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 91 | |
| 92 | PiPipelineModel pipelineModel = pipeconf.pipelineModel(); |
| 93 | |
| 94 | // Retrieve interpreter, if any. |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 95 | final PiPipelineInterpreter interpreter = getInterpreterOrNull(device, pipeconf); |
| 96 | // Get table model. |
| 97 | final PiTableId piTableId = translateTableId(rule.table(), interpreter); |
| 98 | final PiTableModel tableModel = getTableModel(piTableId, pipelineModel); |
| 99 | // Translate selector. |
| 100 | final Collection<PiFieldMatch> fieldMatches = translateFieldMatches(interpreter, rule.selector(), tableModel); |
| 101 | // Translate treatment. |
| 102 | final PiTableAction piTableAction = translateTreatment(rule.treatment(), interpreter, piTableId, pipelineModel); |
Carmelo Cascone | a62ac3d | 2017-08-30 03:19:00 +0200 | [diff] [blame] | 103 | |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 104 | // Build PI entry. |
| 105 | final PiTableEntry.Builder tableEntryBuilder = PiTableEntry.builder(); |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 106 | |
Yi Tseng | 02c4c57 | 2018-01-22 17:52:10 -0800 | [diff] [blame] | 107 | // FIXME: P4Runtime limit |
| 108 | // Need to ignore priority if no TCAM lookup match field |
| 109 | boolean dontIgnorePriority = fieldMatches.stream() |
| 110 | .anyMatch(match -> match.type() == PiMatchType.TERNARY || |
| 111 | match.type() == PiMatchType.RANGE); |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 112 | |
| 113 | tableEntryBuilder |
| 114 | .forTable(piTableId) |
Carmelo Cascone | 0e896a0 | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 115 | .withMatchKey(PiMatchKey.builder() |
| 116 | .addFieldMatches(fieldMatches) |
| 117 | .build()) |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 118 | .withAction(piTableAction); |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 119 | |
Yi Tseng | 02c4c57 | 2018-01-22 17:52:10 -0800 | [diff] [blame] | 120 | if (dontIgnorePriority) { |
| 121 | tableEntryBuilder.withPriority(rule.priority()); |
| 122 | } |
| 123 | |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 124 | if (!rule.isPermanent()) { |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 125 | if (tableModel.supportsAging()) { |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 126 | tableEntryBuilder.withTimeout((double) rule.timeout()); |
| 127 | } else { |
| 128 | log.warn("Flow rule is temporary, but table '{}' doesn't support " + |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 129 | "aging, translating to permanent.", tableModel.id()); |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | } |
| 133 | |
| 134 | return tableEntryBuilder.build(); |
| 135 | } |
| 136 | |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 137 | |
| 138 | /** |
| 139 | * Returns a PI action equivalent to the given treatment, optionally using the given interpreter. This method also |
| 140 | * checks that the produced PI table action is suitable for the given table ID and pipeline model. If suitable, the |
| 141 | * returned action instance will have parameters well-sized, according to the table model. |
| 142 | * |
| 143 | * @param treatment traffic treatment |
| 144 | * @param interpreter interpreter |
| 145 | * @param tableId PI table ID |
| 146 | * @param pipelineModel pipeline model |
| 147 | * @return PI table action |
| 148 | * @throws PiTranslationException if the treatment cannot be translated or if the PI action is not suitable for the |
| 149 | * given pipeline model |
| 150 | */ |
| 151 | static PiTableAction translateTreatment(TrafficTreatment treatment, PiPipelineInterpreter interpreter, |
| 152 | PiTableId tableId, PiPipelineModel pipelineModel) |
| 153 | throws PiTranslationException { |
| 154 | PiTableModel tableModel = getTableModel(tableId, pipelineModel); |
| 155 | return typeCheckAction(buildAction(treatment, interpreter, tableId), tableModel); |
| 156 | } |
| 157 | |
| 158 | private static PiTableModel getTableModel(PiTableId piTableId, PiPipelineModel pipelineModel) |
| 159 | throws PiTranslationException { |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 160 | return pipelineModel.table(piTableId) |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 161 | .orElseThrow(() -> new PiTranslationException(format( |
| 162 | "Not such a table in pipeline model: %s", piTableId))); |
| 163 | } |
| 164 | |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 165 | /** |
| 166 | * Builds a PI action out of the given treatment, optionally using the given interpreter. |
| 167 | */ |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 168 | private static PiTableAction buildAction(TrafficTreatment treatment, PiPipelineInterpreter interpreter, |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 169 | PiTableId tableId) |
| 170 | throws PiTranslationException { |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 171 | |
| 172 | PiTableAction piTableAction = null; |
| 173 | |
| 174 | // If treatment has only one instruction of type PiInstruction, use that. |
| 175 | for (Instruction inst : treatment.allInstructions()) { |
| 176 | if (inst.type() == Instruction.Type.PROTOCOL_INDEPENDENT) { |
| 177 | if (treatment.allInstructions().size() == 1) { |
| 178 | piTableAction = ((PiInstruction) inst).action(); |
| 179 | } else { |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 180 | throw new PiTranslationException(format( |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 181 | "Unable to translate treatment, found multiple instructions " + |
| 182 | "of which one is protocol-independent: %s", treatment)); |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | if (piTableAction == null && interpreter != null) { |
| 188 | // No PiInstruction, use interpreter to build action. |
| 189 | try { |
Carmelo Cascone | f3a1a38 | 2017-07-27 12:04:39 -0400 | [diff] [blame] | 190 | piTableAction = interpreter.mapTreatment(treatment, tableId); |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 191 | } catch (PiPipelineInterpreter.PiInterpreterException e) { |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 192 | throw new PiTranslationException( |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 193 | "Interpreter was unable to translate treatment. " + e.getMessage()); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | if (piTableAction == null) { |
| 198 | // No PiInstruction, no interpreter. It's time to give up. |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 199 | throw new PiTranslationException( |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 200 | "Unable to translate treatment, neither an interpreter or a " |
| 201 | + "protocol-independent instruction were provided."); |
| 202 | } |
| 203 | |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 204 | return piTableAction; |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 205 | } |
| 206 | |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 207 | private static PiTableAction typeCheckAction(PiTableAction piTableAction, PiTableModel table) |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 208 | throws PiTranslationException { |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 209 | switch (piTableAction.type()) { |
| 210 | case ACTION: |
| 211 | return checkPiAction((PiAction) piTableAction, table); |
| 212 | default: |
| 213 | // FIXME: should we check? how? |
| 214 | return piTableAction; |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 215 | |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 216 | } |
| 217 | } |
| 218 | |
| 219 | private static PiTableAction checkPiAction(PiAction piAction, PiTableModel table) |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 220 | throws PiTranslationException { |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 221 | // Table supports this action? |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 222 | PiActionModel actionModel = table.action(piAction.id()).orElseThrow( |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 223 | () -> new PiTranslationException(format("Not such action '%s' for table '%s'", |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 224 | piAction.id(), table.id()))); |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 225 | |
| 226 | // Is the number of runtime parameters correct? |
| 227 | if (actionModel.params().size() != piAction.parameters().size()) { |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 228 | throw new PiTranslationException(format( |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 229 | "Wrong number of runtime parameters for action '%s', expected %d but found %d", |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 230 | actionModel.id(), actionModel.params().size(), piAction.parameters().size())); |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | // Forge a new action instance with well-sized parameters. |
| 234 | // The same comment as in typeCheckFieldMatch() about duplicating field match instances applies here. |
| 235 | PiAction.Builder newActionBuilder = PiAction.builder().withId(piAction.id()); |
| 236 | for (PiActionParam param : piAction.parameters()) { |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 237 | PiActionParamModel paramModel = actionModel.param(param.id()) |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 238 | .orElseThrow(() -> new PiTranslationException(format( |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 239 | "Not such parameter '%s' for action '%s'", param.id(), actionModel))); |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 240 | try { |
| 241 | newActionBuilder.withParameter(new PiActionParam(param.id(), |
| 242 | fit(param.value(), paramModel.bitWidth()))); |
| 243 | } catch (ByteSequenceTrimException e) { |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 244 | throw new PiTranslationException(format( |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 245 | "Size mismatch for parameter '%s' of action '%s': %s", |
| 246 | param.id(), piAction.id(), e.getMessage())); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | return newActionBuilder.build(); |
| 251 | } |
| 252 | |
| 253 | /** |
| 254 | * Builds a collection of PI field matches out of the given selector, optionally using the given interpreter. The |
| 255 | * field matches returned are guaranteed to be compatible for the given table model. |
| 256 | */ |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 257 | private static Collection<PiFieldMatch> translateFieldMatches(PiPipelineInterpreter interpreter, |
| 258 | TrafficSelector selector, PiTableModel tableModel) |
| 259 | throws PiTranslationException { |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 260 | |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 261 | Map<PiMatchFieldId, PiFieldMatch> fieldMatches = Maps.newHashMap(); |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 262 | |
| 263 | // If present, find a PiCriterion and get its field matches as a map. Otherwise, use an empty map. |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 264 | Map<PiMatchFieldId, PiFieldMatch> piCriterionFields = selector.criteria().stream() |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 265 | .filter(c -> c.type().equals(PROTOCOL_INDEPENDENT)) |
| 266 | .map(c -> (PiCriterion) c) |
| 267 | .findFirst() |
| 268 | .map(PiCriterion::fieldMatches) |
| 269 | .map(c -> { |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 270 | Map<PiMatchFieldId, PiFieldMatch> fieldMap = Maps.newHashMap(); |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 271 | c.forEach(fieldMatch -> fieldMap.put(fieldMatch.fieldId(), fieldMatch)); |
| 272 | return fieldMap; |
| 273 | }) |
| 274 | .orElse(Maps.newHashMap()); |
| 275 | |
| 276 | Set<Criterion> translatedCriteria = Sets.newHashSet(); |
| 277 | Set<Criterion> ignoredCriteria = Sets.newHashSet(); |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 278 | Set<PiMatchFieldId> usedPiCriterionFields = Sets.newHashSet(); |
| 279 | Set<PiMatchFieldId> ignoredPiCriterionFields = Sets.newHashSet(); |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 280 | |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 281 | for (PiMatchFieldModel fieldModel : tableModel.matchFields()) { |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 282 | |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 283 | PiMatchFieldId fieldId = fieldModel.id(); |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 284 | |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 285 | int bitWidth = fieldModel.bitWidth(); |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 286 | int fieldByteWidth = (int) Math.ceil((double) bitWidth / 8); |
| 287 | |
| 288 | Optional<Criterion.Type> criterionType = |
| 289 | interpreter == null |
| 290 | ? Optional.empty() |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 291 | : interpreter.mapPiMatchFieldId(fieldId); |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 292 | |
| 293 | Criterion criterion = criterionType.map(selector::getCriterion).orElse(null); |
| 294 | |
| 295 | if (!piCriterionFields.containsKey(fieldId) && criterion == null) { |
| 296 | // Neither a field in PiCriterion is available nor a Criterion mapping is possible. |
| 297 | // Can ignore if the match is ternary or LPM. |
| 298 | switch (fieldModel.matchType()) { |
| 299 | case TERNARY: |
| 300 | // Wildcard the whole field. |
| 301 | fieldMatches.put(fieldId, new PiTernaryFieldMatch( |
| 302 | fieldId, |
| 303 | ImmutableByteSequence.ofZeros(fieldByteWidth), |
| 304 | ImmutableByteSequence.ofZeros(fieldByteWidth))); |
| 305 | break; |
| 306 | case LPM: |
| 307 | // LPM with prefix 0 |
| 308 | fieldMatches.put(fieldId, new PiLpmFieldMatch(fieldId, |
| 309 | ImmutableByteSequence.ofZeros(fieldByteWidth), |
| 310 | 0)); |
| 311 | break; |
| 312 | // FIXME: Can we handle the case of RANGE or VALID match? |
| 313 | default: |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 314 | throw new PiTranslationException(format( |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 315 | "No value found for required match field '%s'", fieldId)); |
| 316 | } |
| 317 | // Next field. |
| 318 | continue; |
| 319 | } |
| 320 | |
| 321 | PiFieldMatch fieldMatch = null; |
| 322 | |
| 323 | if (criterion != null) { |
| 324 | // Criterion mapping is possible for this field id. |
| 325 | try { |
| 326 | fieldMatch = translateCriterion(criterion, fieldId, fieldModel.matchType(), bitWidth); |
| 327 | translatedCriteria.add(criterion); |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 328 | } catch (PiTranslationException ex) { |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 329 | // Ignore exception if the same field was found in PiCriterion. |
| 330 | if (piCriterionFields.containsKey(fieldId)) { |
| 331 | ignoredCriteria.add(criterion); |
| 332 | } else { |
| 333 | throw ex; |
| 334 | } |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | if (piCriterionFields.containsKey(fieldId)) { |
| 339 | // Field was found in PiCriterion. |
| 340 | if (fieldMatch != null) { |
| 341 | // Field was already translated from other criterion. |
| 342 | // Throw exception only if we are trying to match on different values of the same field... |
| 343 | if (!fieldMatch.equals(piCriterionFields.get(fieldId))) { |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 344 | throw new PiTranslationException(format( |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 345 | "Duplicate match field '%s': instance translated from criterion '%s' is different to " + |
| 346 | "what found in PiCriterion.", fieldId, criterion.type())); |
| 347 | } |
| 348 | ignoredPiCriterionFields.add(fieldId); |
| 349 | } else { |
| 350 | fieldMatch = piCriterionFields.get(fieldId); |
| 351 | fieldMatch = typeCheckFieldMatch(fieldMatch, fieldModel); |
| 352 | usedPiCriterionFields.add(fieldId); |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | fieldMatches.put(fieldId, fieldMatch); |
| 357 | } |
| 358 | |
| 359 | // Check if all criteria have been translated. |
| 360 | StringJoiner skippedCriteriaJoiner = new StringJoiner(", "); |
| 361 | selector.criteria().stream() |
| 362 | .filter(c -> !c.type().equals(PROTOCOL_INDEPENDENT)) |
| 363 | .filter(c -> !translatedCriteria.contains(c) && !ignoredCriteria.contains(c)) |
| 364 | .forEach(c -> skippedCriteriaJoiner.add(c.type().name())); |
| 365 | if (skippedCriteriaJoiner.length() > 0) { |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 366 | throw new PiTranslationException(format( |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 367 | "The following criteria cannot be translated for table '%s': %s", |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 368 | tableModel.id(), skippedCriteriaJoiner.toString())); |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | // Check if all fields found in PiCriterion have been used. |
| 372 | StringJoiner skippedPiFieldsJoiner = new StringJoiner(", "); |
| 373 | piCriterionFields.keySet().stream() |
| 374 | .filter(k -> !usedPiCriterionFields.contains(k) && !ignoredPiCriterionFields.contains(k)) |
| 375 | .forEach(k -> skippedPiFieldsJoiner.add(k.id())); |
| 376 | if (skippedPiFieldsJoiner.length() > 0) { |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 377 | throw new PiTranslationException(format( |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 378 | "The following PiCriterion field matches are not supported in table '%s': %s", |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 379 | tableModel.id(), skippedPiFieldsJoiner.toString())); |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | return fieldMatches.values(); |
| 383 | } |
| 384 | |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 385 | private static PiFieldMatch typeCheckFieldMatch(PiFieldMatch fieldMatch, PiMatchFieldModel fieldModel) |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 386 | throws PiTranslationException { |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 387 | |
| 388 | // Check parameter type and size |
| 389 | if (!fieldModel.matchType().equals(fieldMatch.type())) { |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 390 | throw new PiTranslationException(format( |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 391 | "Wrong match type for field '%s', expected %s, but found %s", |
| 392 | fieldMatch.fieldId(), fieldModel.matchType().name(), fieldMatch.type().name())); |
| 393 | } |
| 394 | |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 395 | int modelBitWidth = fieldModel.bitWidth(); |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 396 | |
| 397 | /* |
| 398 | Here we try to be robust against wrong size fields with the goal of having PiCriterion independent of the |
| 399 | pipeline model. We duplicate the field match, fitting the byte sequences to the bit-width specified in the |
| 400 | model. This operation is expensive when performed for each field match of each flow rule, but should be |
| 401 | mitigated by the translation cache provided by PiFlowRuleTranslationServiceImpl. |
| 402 | */ |
| 403 | |
| 404 | try { |
| 405 | switch (fieldModel.matchType()) { |
| 406 | case EXACT: |
| 407 | return new PiExactFieldMatch(fieldMatch.fieldId(), |
| 408 | fit(((PiExactFieldMatch) fieldMatch).value(), modelBitWidth)); |
| 409 | case TERNARY: |
| 410 | return new PiTernaryFieldMatch(fieldMatch.fieldId(), |
| 411 | fit(((PiTernaryFieldMatch) fieldMatch).value(), modelBitWidth), |
| 412 | fit(((PiTernaryFieldMatch) fieldMatch).mask(), modelBitWidth)); |
| 413 | case LPM: |
| 414 | PiLpmFieldMatch lpmfield = (PiLpmFieldMatch) fieldMatch; |
| 415 | if (lpmfield.prefixLength() > modelBitWidth) { |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 416 | throw new PiTranslationException(format( |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 417 | "Invalid prefix length for LPM field '%s', found %d but field has bit-width %d", |
| 418 | fieldMatch.fieldId(), lpmfield.prefixLength(), modelBitWidth)); |
| 419 | } |
| 420 | return new PiLpmFieldMatch(fieldMatch.fieldId(), |
| 421 | fit(lpmfield.value(), modelBitWidth), |
| 422 | lpmfield.prefixLength()); |
| 423 | case RANGE: |
| 424 | return new PiRangeFieldMatch(fieldMatch.fieldId(), |
| 425 | fit(((PiRangeFieldMatch) fieldMatch).lowValue(), modelBitWidth), |
| 426 | fit(((PiRangeFieldMatch) fieldMatch).highValue(), modelBitWidth)); |
| 427 | case VALID: |
| 428 | return fieldMatch; |
| 429 | default: |
| 430 | // Should never be here. |
Ray Milkey | 986a47a | 2018-01-25 11:38:51 -0800 | [diff] [blame^] | 431 | throw new IllegalArgumentException( |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 432 | "Unrecognized match type " + fieldModel.matchType().name()); |
| 433 | } |
| 434 | } catch (ByteSequenceTrimException e) { |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 435 | throw new PiTranslationException(format( |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 436 | "Size mismatch for field %s: %s", fieldMatch.fieldId(), e.getMessage())); |
| 437 | } |
| 438 | } |
| 439 | } |