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