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