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; |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 40 | import org.onosproject.net.pi.model.PiTableType; |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 41 | import org.onosproject.net.pi.runtime.PiAction; |
| 42 | import org.onosproject.net.pi.runtime.PiActionParam; |
Daniele Moro | d900fe4 | 2021-02-11 16:12:57 +0100 | [diff] [blame^] | 43 | import org.onosproject.net.pi.runtime.PiActionSet; |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 44 | import org.onosproject.net.pi.runtime.PiExactFieldMatch; |
| 45 | import org.onosproject.net.pi.runtime.PiFieldMatch; |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 46 | import org.onosproject.net.pi.runtime.PiLpmFieldMatch; |
Carmelo Cascone | 0e896a0 | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 47 | import org.onosproject.net.pi.runtime.PiMatchKey; |
Daniele Moro | c6f2f7f | 2020-12-18 10:55:57 +0100 | [diff] [blame] | 48 | import org.onosproject.net.pi.runtime.PiOptionalFieldMatch; |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 49 | import org.onosproject.net.pi.runtime.PiRangeFieldMatch; |
| 50 | import org.onosproject.net.pi.runtime.PiTableAction; |
| 51 | import org.onosproject.net.pi.runtime.PiTableEntry; |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 52 | import org.onosproject.net.pi.runtime.PiTernaryFieldMatch; |
Carmelo Cascone | 326ad2d | 2017-11-28 18:09:13 -0800 | [diff] [blame] | 53 | import org.onosproject.net.pi.service.PiTranslationException; |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 54 | import org.slf4j.Logger; |
| 55 | import org.slf4j.LoggerFactory; |
| 56 | |
| 57 | import java.util.Collection; |
| 58 | import java.util.Map; |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 59 | import java.util.Set; |
| 60 | import java.util.StringJoiner; |
| 61 | |
| 62 | import static java.lang.String.format; |
| 63 | import static org.onlab.util.ImmutableByteSequence.ByteSequenceTrimException; |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 64 | import static org.onlab.util.ImmutableByteSequence.prefixOnes; |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 65 | import static org.onosproject.net.flow.criteria.Criterion.Type.PROTOCOL_INDEPENDENT; |
| 66 | import static org.onosproject.net.pi.impl.CriterionTranslatorHelper.translateCriterion; |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 67 | import static org.onosproject.net.pi.impl.PiUtils.getInterpreterOrNull; |
| 68 | import static org.onosproject.net.pi.impl.PiUtils.translateTableId; |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 69 | |
| 70 | /** |
| 71 | * Implementation of flow rule translation logic. |
| 72 | */ |
Carmelo Cascone | 326ad2d | 2017-11-28 18:09:13 -0800 | [diff] [blame] | 73 | final class PiFlowRuleTranslatorImpl { |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 74 | |
Carmelo Cascone | a3d811c | 2017-08-22 01:03:45 +0200 | [diff] [blame] | 75 | public static final int MAX_PI_PRIORITY = (int) Math.pow(2, 24); |
Carmelo Cascone | 6af4e17 | 2018-06-15 16:01:30 +0200 | [diff] [blame] | 76 | public static final int MIN_PI_PRIORITY = 1; |
Carmelo Cascone | 326ad2d | 2017-11-28 18:09:13 -0800 | [diff] [blame] | 77 | private static final Logger log = LoggerFactory.getLogger(PiFlowRuleTranslatorImpl.class); |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 78 | |
Carmelo Cascone | 326ad2d | 2017-11-28 18:09:13 -0800 | [diff] [blame] | 79 | private PiFlowRuleTranslatorImpl() { |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 80 | // Hide constructor. |
| 81 | } |
| 82 | |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 83 | /** |
Carmelo Cascone | 33f36a0 | 2019-04-17 20:05:21 -0700 | [diff] [blame] | 84 | * Returns a PI table entry equivalent to the given flow rule, for the given |
| 85 | * pipeconf and device. |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 86 | * |
| 87 | * @param rule flow rule |
| 88 | * @param pipeconf pipeconf |
| 89 | * @param device device |
| 90 | * @return PI table entry |
| 91 | * @throws PiTranslationException if the flow rule cannot be translated |
| 92 | */ |
| 93 | static PiTableEntry translate(FlowRule rule, PiPipeconf pipeconf, Device device) |
| 94 | throws PiTranslationException { |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 95 | |
| 96 | PiPipelineModel pipelineModel = pipeconf.pipelineModel(); |
| 97 | |
| 98 | // Retrieve interpreter, if any. |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 99 | final PiPipelineInterpreter interpreter = getInterpreterOrNull(device, pipeconf); |
| 100 | // Get table model. |
| 101 | final PiTableId piTableId = translateTableId(rule.table(), interpreter); |
| 102 | final PiTableModel tableModel = getTableModel(piTableId, pipelineModel); |
| 103 | // Translate selector. |
Carmelo Cascone | 4256bde | 2018-03-23 18:02:15 -0700 | [diff] [blame] | 104 | final PiMatchKey piMatchKey; |
| 105 | final boolean needPriority; |
| 106 | if (rule.selector().criteria().isEmpty()) { |
| 107 | piMatchKey = PiMatchKey.EMPTY; |
| 108 | needPriority = false; |
| 109 | } else { |
| 110 | final Collection<PiFieldMatch> fieldMatches = translateFieldMatches( |
| 111 | interpreter, rule.selector(), tableModel); |
| 112 | piMatchKey = PiMatchKey.builder() |
| 113 | .addFieldMatches(fieldMatches) |
| 114 | .build(); |
| 115 | // FIXME: P4Runtime limit |
| 116 | // Need to ignore priority if no TCAM lookup match field |
Yi Tseng | 667538d | 2018-06-22 02:19:23 +0800 | [diff] [blame] | 117 | needPriority = tableModel.matchFields().stream() |
| 118 | .anyMatch(match -> match.matchType() == PiMatchType.TERNARY || |
Daniele Moro | c6f2f7f | 2020-12-18 10:55:57 +0100 | [diff] [blame] | 119 | match.matchType() == PiMatchType.RANGE || |
| 120 | match.matchType() == PiMatchType.OPTIONAL); |
Carmelo Cascone | 4256bde | 2018-03-23 18:02:15 -0700 | [diff] [blame] | 121 | } |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 122 | // Translate treatment. |
| 123 | final PiTableAction piTableAction = translateTreatment(rule.treatment(), interpreter, piTableId, pipelineModel); |
Carmelo Cascone | a62ac3d | 2017-08-30 03:19:00 +0200 | [diff] [blame] | 124 | |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 125 | // Build PI entry. |
| 126 | final PiTableEntry.Builder tableEntryBuilder = PiTableEntry.builder(); |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 127 | |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 128 | tableEntryBuilder |
| 129 | .forTable(piTableId) |
Yi Tseng | df3eec5 | 2018-02-15 14:56:02 -0800 | [diff] [blame] | 130 | .withMatchKey(piMatchKey); |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 131 | |
Yi Tseng | d28936e | 2018-02-23 22:11:11 +0100 | [diff] [blame] | 132 | if (piTableAction != null) { |
| 133 | tableEntryBuilder.withAction(piTableAction); |
| 134 | } |
| 135 | |
Carmelo Cascone | 4256bde | 2018-03-23 18:02:15 -0700 | [diff] [blame] | 136 | if (needPriority) { |
Carmelo Cascone | 6af4e17 | 2018-06-15 16:01:30 +0200 | [diff] [blame] | 137 | // FIXME: move priority check to P4Runtime driver. |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 138 | final int newPriority; |
| 139 | if (rule.priority() > MAX_PI_PRIORITY) { |
| 140 | log.warn("Flow rule priority too big, setting translated priority to max value {}: {}", |
| 141 | MAX_PI_PRIORITY, rule); |
Carmelo Cascone | 6af4e17 | 2018-06-15 16:01:30 +0200 | [diff] [blame] | 142 | newPriority = MAX_PI_PRIORITY; |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 143 | } else { |
Carmelo Cascone | 6af4e17 | 2018-06-15 16:01:30 +0200 | [diff] [blame] | 144 | newPriority = MIN_PI_PRIORITY + rule.priority(); |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 145 | } |
| 146 | tableEntryBuilder.withPriority(newPriority); |
Yi Tseng | 02c4c57 | 2018-01-22 17:52:10 -0800 | [diff] [blame] | 147 | } |
| 148 | |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 149 | if (!rule.isPermanent()) { |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 150 | if (tableModel.supportsAging()) { |
Carmelo Cascone | f11a321 | 2020-08-13 17:34:17 -0700 | [diff] [blame] | 151 | tableEntryBuilder.withTimeout(rule.timeout()); |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 152 | } else { |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 153 | log.debug("Flow rule is temporary, but table '{}' doesn't support " + |
Carmelo Cascone | 33f36a0 | 2019-04-17 20:05:21 -0700 | [diff] [blame] | 154 | "aging, translating to permanent.", tableModel.id()); |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | } |
| 158 | |
| 159 | return tableEntryBuilder.build(); |
| 160 | } |
| 161 | |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 162 | |
| 163 | /** |
Carmelo Cascone | 33f36a0 | 2019-04-17 20:05:21 -0700 | [diff] [blame] | 164 | * Returns a PI action equivalent to the given treatment, optionally using |
| 165 | * the given interpreter. This method also checks that the produced PI table |
| 166 | * action is suitable for the given table ID and pipeline model. If |
| 167 | * suitable, the returned action instance will have parameters well-sized, |
| 168 | * according to the table model. |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 169 | * |
| 170 | * @param treatment traffic treatment |
| 171 | * @param interpreter interpreter |
| 172 | * @param tableId PI table ID |
| 173 | * @param pipelineModel pipeline model |
| 174 | * @return PI table action |
Carmelo Cascone | 33f36a0 | 2019-04-17 20:05:21 -0700 | [diff] [blame] | 175 | * @throws PiTranslationException if the treatment cannot be translated or |
| 176 | * if the PI action is not suitable for the |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 177 | * given pipeline model |
| 178 | */ |
| 179 | static PiTableAction translateTreatment(TrafficTreatment treatment, PiPipelineInterpreter interpreter, |
| 180 | PiTableId tableId, PiPipelineModel pipelineModel) |
| 181 | throws PiTranslationException { |
| 182 | PiTableModel tableModel = getTableModel(tableId, pipelineModel); |
| 183 | return typeCheckAction(buildAction(treatment, interpreter, tableId), tableModel); |
| 184 | } |
| 185 | |
| 186 | private static PiTableModel getTableModel(PiTableId piTableId, PiPipelineModel pipelineModel) |
| 187 | throws PiTranslationException { |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 188 | return pipelineModel.table(piTableId) |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 189 | .orElseThrow(() -> new PiTranslationException(format( |
| 190 | "Not such a table in pipeline model: %s", piTableId))); |
| 191 | } |
| 192 | |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 193 | /** |
Carmelo Cascone | 33f36a0 | 2019-04-17 20:05:21 -0700 | [diff] [blame] | 194 | * Builds a PI action out of the given treatment, optionally using the given |
| 195 | * interpreter. |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 196 | */ |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 197 | private static PiTableAction buildAction(TrafficTreatment treatment, PiPipelineInterpreter interpreter, |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 198 | PiTableId tableId) |
| 199 | throws PiTranslationException { |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 200 | |
| 201 | PiTableAction piTableAction = null; |
| 202 | |
| 203 | // If treatment has only one instruction of type PiInstruction, use that. |
| 204 | for (Instruction inst : treatment.allInstructions()) { |
| 205 | if (inst.type() == Instruction.Type.PROTOCOL_INDEPENDENT) { |
| 206 | if (treatment.allInstructions().size() == 1) { |
| 207 | piTableAction = ((PiInstruction) inst).action(); |
| 208 | } else { |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 209 | throw new PiTranslationException(format( |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 210 | "Unable to translate treatment, found multiple instructions " + |
| 211 | "of which one is protocol-independent: %s", treatment)); |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | if (piTableAction == null && interpreter != null) { |
| 217 | // No PiInstruction, use interpreter to build action. |
| 218 | try { |
Carmelo Cascone | f3a1a38 | 2017-07-27 12:04:39 -0400 | [diff] [blame] | 219 | piTableAction = interpreter.mapTreatment(treatment, tableId); |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 220 | } catch (PiPipelineInterpreter.PiInterpreterException e) { |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 221 | throw new PiTranslationException( |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 222 | "Interpreter was unable to translate treatment. " + e.getMessage()); |
| 223 | } |
| 224 | } |
| 225 | |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 226 | return piTableAction; |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 227 | } |
| 228 | |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 229 | private static PiTableAction typeCheckAction(PiTableAction piTableAction, PiTableModel table) |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 230 | throws PiTranslationException { |
Yi Tseng | d28936e | 2018-02-23 22:11:11 +0100 | [diff] [blame] | 231 | if (piTableAction == null) { |
| 232 | // skip check if null |
| 233 | return null; |
| 234 | } |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 235 | switch (piTableAction.type()) { |
| 236 | case ACTION: |
| 237 | return checkPiAction((PiAction) piTableAction, table); |
Daniele Moro | d900fe4 | 2021-02-11 16:12:57 +0100 | [diff] [blame^] | 238 | case ACTION_SET: |
| 239 | for (var actProfAct : ((PiActionSet) piTableAction).actions()) { |
| 240 | checkPiAction(actProfAct.action(), table); |
| 241 | } |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 242 | case ACTION_PROFILE_GROUP_ID: |
Daniele Moro | d900fe4 | 2021-02-11 16:12:57 +0100 | [diff] [blame^] | 243 | if (table.actionProfile() == null || !table.actionProfile().hasSelector()) { |
| 244 | throw new PiTranslationException(format( |
| 245 | "action is of type '%s', but table '%s' does not" + |
| 246 | "implement an action profile with dynamic selection", |
| 247 | piTableAction.type(), table.id())); |
| 248 | } |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 249 | case ACTION_PROFILE_MEMBER_ID: |
| 250 | if (!table.tableType().equals(PiTableType.INDIRECT)) { |
| 251 | throw new PiTranslationException(format( |
| 252 | "action is indirect of type '%s', but table '%s' is of type '%s'", |
| 253 | piTableAction.type(), table.id(), table.tableType())); |
| 254 | } |
Daniele Moro | d900fe4 | 2021-02-11 16:12:57 +0100 | [diff] [blame^] | 255 | if (!piTableAction.type().equals(PiTableAction.Type.ACTION_SET) && |
| 256 | table.oneShotOnly()) { |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 257 | throw new PiTranslationException(format( |
Daniele Moro | d900fe4 | 2021-02-11 16:12:57 +0100 | [diff] [blame^] | 258 | "table '%s' supports only one shot programming", table.id() |
| 259 | )); |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 260 | } |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 261 | return piTableAction; |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 262 | default: |
| 263 | throw new PiTranslationException(format( |
| 264 | "Unknown table action type %s", piTableAction.type())); |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 265 | |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 266 | } |
| 267 | } |
| 268 | |
| 269 | private static PiTableAction checkPiAction(PiAction piAction, PiTableModel table) |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 270 | throws PiTranslationException { |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 271 | // Table supports this action? |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 272 | PiActionModel actionModel = table.action(piAction.id()).orElseThrow( |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 273 | () -> new PiTranslationException(format("Not such action '%s' for table '%s'", |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 274 | piAction.id(), table.id()))); |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 275 | |
| 276 | // Is the number of runtime parameters correct? |
| 277 | if (actionModel.params().size() != piAction.parameters().size()) { |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 278 | throw new PiTranslationException(format( |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 279 | "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] | 280 | actionModel.id(), actionModel.params().size(), piAction.parameters().size())); |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | // Forge a new action instance with well-sized parameters. |
| 284 | // The same comment as in typeCheckFieldMatch() about duplicating field match instances applies here. |
| 285 | PiAction.Builder newActionBuilder = PiAction.builder().withId(piAction.id()); |
| 286 | for (PiActionParam param : piAction.parameters()) { |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 287 | PiActionParamModel paramModel = actionModel.param(param.id()) |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 288 | .orElseThrow(() -> new PiTranslationException(format( |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 289 | "Not such parameter '%s' for action '%s'", param.id(), actionModel))); |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 290 | try { |
| 291 | newActionBuilder.withParameter(new PiActionParam(param.id(), |
Daniele Moro | 5c82b0f | 2020-12-07 20:56:30 +0100 | [diff] [blame] | 292 | paramModel.hasBitWidth() ? |
| 293 | param.value().fit(paramModel.bitWidth()) : |
| 294 | param.value())); |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 295 | } catch (ByteSequenceTrimException e) { |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 296 | throw new PiTranslationException(format( |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 297 | "Size mismatch for parameter '%s' of action '%s': %s", |
| 298 | param.id(), piAction.id(), e.getMessage())); |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | return newActionBuilder.build(); |
| 303 | } |
| 304 | |
| 305 | /** |
Carmelo Cascone | 33f36a0 | 2019-04-17 20:05:21 -0700 | [diff] [blame] | 306 | * Builds a collection of PI field matches out of the given selector, |
| 307 | * optionally using the given interpreter. The field matches returned are |
| 308 | * guaranteed to be compatible for the given table model. |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 309 | */ |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 310 | private static Collection<PiFieldMatch> translateFieldMatches(PiPipelineInterpreter interpreter, |
| 311 | TrafficSelector selector, PiTableModel tableModel) |
| 312 | throws PiTranslationException { |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 313 | |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 314 | Map<PiMatchFieldId, PiFieldMatch> fieldMatches = Maps.newHashMap(); |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 315 | |
| 316 | // 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] | 317 | Map<PiMatchFieldId, PiFieldMatch> piCriterionFields = selector.criteria().stream() |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 318 | .filter(c -> c.type().equals(PROTOCOL_INDEPENDENT)) |
| 319 | .map(c -> (PiCriterion) c) |
| 320 | .findFirst() |
| 321 | .map(PiCriterion::fieldMatches) |
| 322 | .map(c -> { |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 323 | Map<PiMatchFieldId, PiFieldMatch> fieldMap = Maps.newHashMap(); |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 324 | c.forEach(fieldMatch -> fieldMap.put(fieldMatch.fieldId(), fieldMatch)); |
| 325 | return fieldMap; |
| 326 | }) |
| 327 | .orElse(Maps.newHashMap()); |
| 328 | |
| 329 | Set<Criterion> translatedCriteria = Sets.newHashSet(); |
| 330 | Set<Criterion> ignoredCriteria = Sets.newHashSet(); |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 331 | Set<PiMatchFieldId> usedPiCriterionFields = Sets.newHashSet(); |
| 332 | Set<PiMatchFieldId> ignoredPiCriterionFields = Sets.newHashSet(); |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 333 | |
Carmelo Cascone | 33f36a0 | 2019-04-17 20:05:21 -0700 | [diff] [blame] | 334 | |
| 335 | Map<PiMatchFieldId, Criterion> criterionMap = Maps.newHashMap(); |
| 336 | if (interpreter != null) { |
| 337 | // NOTE: if two criterion types map to the same match field ID, and |
| 338 | // those two criterion types are present in the selector, this won't |
| 339 | // work. This is unlikely to happen since those cases should be |
| 340 | // mutually exclusive: |
| 341 | // e.g. ICMPV6_TYPE -> metadata.my_normalized_icmp_type |
| 342 | // ICMPV4_TYPE -> metadata.my_normalized_icmp_type |
| 343 | // A packet can be either ICMPv6 or ICMPv4 but not both. |
| 344 | selector.criteria() |
| 345 | .stream() |
| 346 | .map(Criterion::type) |
| 347 | .filter(t -> t != PROTOCOL_INDEPENDENT) |
| 348 | .forEach(t -> { |
| 349 | PiMatchFieldId mfid = interpreter.mapCriterionType(t) |
| 350 | .orElse(null); |
| 351 | if (mfid != null) { |
| 352 | if (criterionMap.containsKey(mfid)) { |
| 353 | log.warn("Detected criterion mapping " + |
| 354 | "conflict for PiMatchFieldId {}", |
| 355 | mfid); |
| 356 | } |
| 357 | criterionMap.put(mfid, selector.getCriterion(t)); |
| 358 | } |
| 359 | }); |
| 360 | } |
| 361 | |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 362 | for (PiMatchFieldModel fieldModel : tableModel.matchFields()) { |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 363 | |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 364 | PiMatchFieldId fieldId = fieldModel.id(); |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 365 | |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 366 | int bitWidth = fieldModel.bitWidth(); |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 367 | |
Carmelo Cascone | 33f36a0 | 2019-04-17 20:05:21 -0700 | [diff] [blame] | 368 | Criterion criterion = criterionMap.get(fieldId); |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 369 | |
| 370 | if (!piCriterionFields.containsKey(fieldId) && criterion == null) { |
| 371 | // Neither a field in PiCriterion is available nor a Criterion mapping is possible. |
Carmelo Cascone | f11a321 | 2020-08-13 17:34:17 -0700 | [diff] [blame] | 372 | // Can ignore if match is ternary-like, as it means "don't care". |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 373 | switch (fieldModel.matchType()) { |
| 374 | case TERNARY: |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 375 | case LPM: |
Carmelo Cascone | f11a321 | 2020-08-13 17:34:17 -0700 | [diff] [blame] | 376 | case RANGE: |
Daniele Moro | c6f2f7f | 2020-12-18 10:55:57 +0100 | [diff] [blame] | 377 | case OPTIONAL: |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 378 | // Skip field. |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 379 | break; |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 380 | default: |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 381 | throw new PiTranslationException(format( |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 382 | "No value found for required match field '%s'", fieldId)); |
| 383 | } |
| 384 | // Next field. |
| 385 | continue; |
| 386 | } |
| 387 | |
| 388 | PiFieldMatch fieldMatch = null; |
| 389 | |
Daniele Moro | 5c82b0f | 2020-12-07 20:56:30 +0100 | [diff] [blame] | 390 | // TODO: we currently do not support fields with arbitrary bit width |
| 391 | if (criterion != null && fieldModel.hasBitWidth()) { |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 392 | // Criterion mapping is possible for this field id. |
| 393 | try { |
| 394 | fieldMatch = translateCriterion(criterion, fieldId, fieldModel.matchType(), bitWidth); |
| 395 | translatedCriteria.add(criterion); |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 396 | } catch (PiTranslationException ex) { |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 397 | // Ignore exception if the same field was found in PiCriterion. |
| 398 | if (piCriterionFields.containsKey(fieldId)) { |
| 399 | ignoredCriteria.add(criterion); |
| 400 | } else { |
| 401 | throw ex; |
| 402 | } |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | if (piCriterionFields.containsKey(fieldId)) { |
| 407 | // Field was found in PiCriterion. |
| 408 | if (fieldMatch != null) { |
| 409 | // Field was already translated from other criterion. |
| 410 | // Throw exception only if we are trying to match on different values of the same field... |
| 411 | if (!fieldMatch.equals(piCriterionFields.get(fieldId))) { |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 412 | throw new PiTranslationException(format( |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 413 | "Duplicate match field '%s': instance translated from criterion '%s' is different to " + |
| 414 | "what found in PiCriterion.", fieldId, criterion.type())); |
| 415 | } |
| 416 | ignoredPiCriterionFields.add(fieldId); |
| 417 | } else { |
| 418 | fieldMatch = piCriterionFields.get(fieldId); |
| 419 | fieldMatch = typeCheckFieldMatch(fieldMatch, fieldModel); |
| 420 | usedPiCriterionFields.add(fieldId); |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | fieldMatches.put(fieldId, fieldMatch); |
| 425 | } |
| 426 | |
| 427 | // Check if all criteria have been translated. |
| 428 | StringJoiner skippedCriteriaJoiner = new StringJoiner(", "); |
| 429 | selector.criteria().stream() |
| 430 | .filter(c -> !c.type().equals(PROTOCOL_INDEPENDENT)) |
| 431 | .filter(c -> !translatedCriteria.contains(c) && !ignoredCriteria.contains(c)) |
| 432 | .forEach(c -> skippedCriteriaJoiner.add(c.type().name())); |
| 433 | if (skippedCriteriaJoiner.length() > 0) { |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 434 | throw new PiTranslationException(format( |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 435 | "The following criteria cannot be translated for table '%s': %s", |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 436 | tableModel.id(), skippedCriteriaJoiner.toString())); |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | // Check if all fields found in PiCriterion have been used. |
| 440 | StringJoiner skippedPiFieldsJoiner = new StringJoiner(", "); |
| 441 | piCriterionFields.keySet().stream() |
| 442 | .filter(k -> !usedPiCriterionFields.contains(k) && !ignoredPiCriterionFields.contains(k)) |
| 443 | .forEach(k -> skippedPiFieldsJoiner.add(k.id())); |
| 444 | if (skippedPiFieldsJoiner.length() > 0) { |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 445 | throw new PiTranslationException(format( |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 446 | "The following PiCriterion field matches are not supported in table '%s': %s", |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 447 | tableModel.id(), skippedPiFieldsJoiner.toString())); |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | return fieldMatches.values(); |
| 451 | } |
| 452 | |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 453 | private static PiFieldMatch typeCheckFieldMatch(PiFieldMatch fieldMatch, PiMatchFieldModel fieldModel) |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 454 | throws PiTranslationException { |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 455 | |
| 456 | // Check parameter type and size |
| 457 | if (!fieldModel.matchType().equals(fieldMatch.type())) { |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 458 | throw new PiTranslationException(format( |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 459 | "Wrong match type for field '%s', expected %s, but found %s", |
| 460 | fieldMatch.fieldId(), fieldModel.matchType().name(), fieldMatch.type().name())); |
| 461 | } |
| 462 | |
Daniele Moro | c6f2f7f | 2020-12-18 10:55:57 +0100 | [diff] [blame] | 463 | // Check if the arbitrary bit width is supported |
| 464 | if (!fieldModel.hasBitWidth() && |
| 465 | !fieldModel.matchType().equals(PiMatchType.EXACT) && |
| 466 | !fieldModel.matchType().equals(PiMatchType.OPTIONAL)) { |
| 467 | throw new PiTranslationException(format( |
| 468 | "Arbitrary bit width for field '%s' and match type %s is not supported", |
| 469 | fieldMatch.fieldId(), fieldModel.matchType().name())); |
| 470 | } |
| 471 | |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 472 | int modelBitWidth = fieldModel.bitWidth(); |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 473 | |
| 474 | /* |
| 475 | Here we try to be robust against wrong size fields with the goal of having PiCriterion independent of the |
| 476 | 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] | 477 | model. We also normalize ternary (and LPM) field matches by setting to 0 unused bits, as required by P4Runtime. |
| 478 | |
| 479 | 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] | 480 | mitigated by the translation cache provided by PiFlowRuleTranslationServiceImpl. |
| 481 | */ |
| 482 | |
| 483 | try { |
| 484 | switch (fieldModel.matchType()) { |
| 485 | case EXACT: |
Daniele Moro | 5c82b0f | 2020-12-07 20:56:30 +0100 | [diff] [blame] | 486 | PiExactFieldMatch exactField = (PiExactFieldMatch) fieldMatch; |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 487 | return new PiExactFieldMatch(fieldMatch.fieldId(), |
Daniele Moro | 5c82b0f | 2020-12-07 20:56:30 +0100 | [diff] [blame] | 488 | fieldModel.hasBitWidth() ? |
| 489 | exactField.value().fit(modelBitWidth) : |
| 490 | exactField.value()); |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 491 | case TERNARY: |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 492 | PiTernaryFieldMatch ternField = (PiTernaryFieldMatch) fieldMatch; |
| 493 | ImmutableByteSequence ternMask = ternField.mask().fit(modelBitWidth); |
| 494 | ImmutableByteSequence ternValue = ternField.value() |
| 495 | .fit(modelBitWidth) |
| 496 | .bitwiseAnd(ternMask); |
| 497 | return new PiTernaryFieldMatch(fieldMatch.fieldId(), ternValue, ternMask); |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 498 | case LPM: |
| 499 | PiLpmFieldMatch lpmfield = (PiLpmFieldMatch) fieldMatch; |
| 500 | if (lpmfield.prefixLength() > modelBitWidth) { |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 501 | throw new PiTranslationException(format( |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 502 | "Invalid prefix length for LPM field '%s', found %d but field has bit-width %d", |
| 503 | fieldMatch.fieldId(), lpmfield.prefixLength(), modelBitWidth)); |
| 504 | } |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 505 | ImmutableByteSequence lpmValue = lpmfield.value() |
Carmelo Cascone | 181f3f4 | 2018-04-11 17:37:53 -0700 | [diff] [blame] | 506 | .fit(modelBitWidth); |
| 507 | ImmutableByteSequence lpmMask = prefixOnes(lpmValue.size(), |
| 508 | lpmfield.prefixLength()); |
| 509 | lpmValue = lpmValue.bitwiseAnd(lpmMask); |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 510 | return new PiLpmFieldMatch(fieldMatch.fieldId(), |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 511 | lpmValue, lpmfield.prefixLength()); |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 512 | case RANGE: |
| 513 | return new PiRangeFieldMatch(fieldMatch.fieldId(), |
Carmelo Cascone | 8a571af | 2018-04-06 23:17:04 -0700 | [diff] [blame] | 514 | ((PiRangeFieldMatch) fieldMatch).lowValue().fit(modelBitWidth), |
| 515 | ((PiRangeFieldMatch) fieldMatch).highValue().fit(modelBitWidth)); |
Daniele Moro | c6f2f7f | 2020-12-18 10:55:57 +0100 | [diff] [blame] | 516 | case OPTIONAL: |
| 517 | PiOptionalFieldMatch optionalField = (PiOptionalFieldMatch) fieldMatch; |
| 518 | return new PiOptionalFieldMatch(fieldMatch.fieldId(), |
| 519 | fieldModel.hasBitWidth() ? |
| 520 | optionalField.value().fit(modelBitWidth) : |
| 521 | optionalField.value()); |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 522 | default: |
| 523 | // Should never be here. |
Ray Milkey | 986a47a | 2018-01-25 11:38:51 -0800 | [diff] [blame] | 524 | throw new IllegalArgumentException( |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 525 | "Unrecognized match type " + fieldModel.matchType().name()); |
| 526 | } |
| 527 | } catch (ByteSequenceTrimException e) { |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 528 | throw new PiTranslationException(format( |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 529 | "Size mismatch for field %s: %s", fieldMatch.fieldId(), e.getMessage())); |
| 530 | } |
| 531 | } |
| 532 | } |