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