Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [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 | 8d99b17 | 2017-07-18 17:26:31 -0400 | [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.p4runtime.ctl; |
| 18 | |
| 19 | import com.google.common.collect.ImmutableList; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 20 | import com.google.common.collect.Lists; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 21 | import com.google.protobuf.ByteString; |
| 22 | import org.onlab.util.ImmutableByteSequence; |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame^] | 23 | import org.onosproject.net.pi.model.PiActionId; |
| 24 | import org.onosproject.net.pi.model.PiActionParamId; |
| 25 | import org.onosproject.net.pi.model.PiMatchFieldId; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 26 | import org.onosproject.net.pi.model.PiPipeconf; |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame^] | 27 | import org.onosproject.net.pi.model.PiTableId; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 28 | import org.onosproject.net.pi.runtime.PiAction; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 29 | import org.onosproject.net.pi.runtime.PiActionGroupId; |
| 30 | import org.onosproject.net.pi.runtime.PiActionGroupMemberId; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 31 | import org.onosproject.net.pi.runtime.PiActionParam; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 32 | import org.onosproject.net.pi.runtime.PiExactFieldMatch; |
| 33 | import org.onosproject.net.pi.runtime.PiFieldMatch; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 34 | import org.onosproject.net.pi.runtime.PiLpmFieldMatch; |
Carmelo Cascone | 0e896a0 | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 35 | import org.onosproject.net.pi.runtime.PiMatchKey; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 36 | import org.onosproject.net.pi.runtime.PiRangeFieldMatch; |
| 37 | import org.onosproject.net.pi.runtime.PiTableAction; |
| 38 | import org.onosproject.net.pi.runtime.PiTableEntry; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 39 | import org.onosproject.net.pi.runtime.PiTernaryFieldMatch; |
| 40 | import org.onosproject.net.pi.runtime.PiValidFieldMatch; |
| 41 | import org.slf4j.Logger; |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 42 | import p4.P4RuntimeOuterClass.Action; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 43 | import p4.P4RuntimeOuterClass.FieldMatch; |
| 44 | import p4.P4RuntimeOuterClass.TableAction; |
| 45 | import p4.P4RuntimeOuterClass.TableEntry; |
| 46 | import p4.config.P4InfoOuterClass; |
| 47 | |
| 48 | import java.util.Collection; |
| 49 | import java.util.Collections; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 50 | import java.util.List; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 51 | |
| 52 | import static java.lang.String.format; |
| 53 | import static org.onlab.util.ImmutableByteSequence.copyFrom; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 54 | import static org.onosproject.p4runtime.ctl.P4RuntimeUtils.assertPrefixLen; |
| 55 | import static org.onosproject.p4runtime.ctl.P4RuntimeUtils.assertSize; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 56 | import static org.slf4j.LoggerFactory.getLogger; |
| 57 | |
| 58 | /** |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 59 | * Encoder/Decoder of table entries, from ONOS Pi* format, to P4Runtime protobuf messages, and vice versa. |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 60 | */ |
| 61 | final class TableEntryEncoder { |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 62 | private static final Logger log = getLogger(TableEntryEncoder.class); |
| 63 | |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 64 | private static final String VALUE_OF_PREFIX = "value of "; |
| 65 | private static final String MASK_OF_PREFIX = "mask of "; |
| 66 | private static final String HIGH_RANGE_VALUE_OF_PREFIX = "high range value of "; |
| 67 | private static final String LOW_RANGE_VALUE_OF_PREFIX = "low range value of "; |
| 68 | |
| 69 | // TODO: implement cache of encoded entities. |
| 70 | |
| 71 | private TableEntryEncoder() { |
| 72 | // hide. |
| 73 | } |
| 74 | |
| 75 | /** |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 76 | * Returns a collection of P4Runtime table entry protobuf messages, encoded from the given collection of PI table |
| 77 | * entries for the given pipeconf. If a PI table entry cannot be encoded, it is skipped, hence the returned |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 78 | * collection might have different size than the input one. |
| 79 | * <p> |
| 80 | * Please check the log for an explanation of any error that might have occurred. |
| 81 | * |
| 82 | * @param piTableEntries PI table entries |
| 83 | * @param pipeconf PI pipeconf |
| 84 | * @return collection of P4Runtime table entry protobuf messages |
| 85 | */ |
| 86 | static Collection<TableEntry> encode(Collection<PiTableEntry> piTableEntries, PiPipeconf pipeconf) { |
| 87 | |
| 88 | P4InfoBrowser browser = PipeconfHelper.getP4InfoBrowser(pipeconf); |
| 89 | |
| 90 | if (browser == null) { |
| 91 | log.error("Unable to get a P4Info browser for pipeconf {}, skipping encoding of all table entries"); |
| 92 | return Collections.emptyList(); |
| 93 | } |
| 94 | |
| 95 | ImmutableList.Builder<TableEntry> tableEntryMsgListBuilder = ImmutableList.builder(); |
| 96 | |
| 97 | for (PiTableEntry piTableEntry : piTableEntries) { |
| 98 | try { |
| 99 | tableEntryMsgListBuilder.add(encodePiTableEntry(piTableEntry, browser)); |
| 100 | } catch (P4InfoBrowser.NotFoundException | EncodeException e) { |
| 101 | log.error("Unable to encode PI table entry: {}", e.getMessage()); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | return tableEntryMsgListBuilder.build(); |
| 106 | } |
| 107 | |
| 108 | /** |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 109 | * Same as {@link #encode(Collection, PiPipeconf)} but encodes only one entry. |
| 110 | * |
| 111 | * @param piTableEntry table entry |
| 112 | * @param pipeconf pipeconf |
| 113 | * @return encoded table entry message |
| 114 | * @throws EncodeException if entry cannot be encoded |
| 115 | * @throws P4InfoBrowser.NotFoundException if the required information cannot be find in the pipeconf's P4info |
| 116 | */ |
| 117 | static TableEntry encode(PiTableEntry piTableEntry, PiPipeconf pipeconf) |
| 118 | throws EncodeException, P4InfoBrowser.NotFoundException { |
| 119 | |
| 120 | P4InfoBrowser browser = PipeconfHelper.getP4InfoBrowser(pipeconf); |
| 121 | if (browser == null) { |
| 122 | throw new EncodeException(format("Unable to get a P4Info browser for pipeconf %s", pipeconf.id())); |
| 123 | } |
| 124 | |
| 125 | return encodePiTableEntry(piTableEntry, browser); |
| 126 | } |
| 127 | |
| 128 | /** |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 129 | * Returns a collection of PI table entry objects, decoded from the given collection of P4Runtime table entry |
| 130 | * messages for the given pipeconf. If a table entry message cannot be decoded, it is skipped, hence the returned |
| 131 | * collection might have different size than the input one. |
| 132 | * <p> |
| 133 | * Please check the log for an explanation of any error that might have occurred. |
| 134 | * |
| 135 | * @param tableEntryMsgs P4Runtime table entry messages |
| 136 | * @param pipeconf PI pipeconf |
| 137 | * @return collection of PI table entry objects |
| 138 | */ |
| 139 | static Collection<PiTableEntry> decode(Collection<TableEntry> tableEntryMsgs, PiPipeconf pipeconf) { |
| 140 | |
| 141 | P4InfoBrowser browser = PipeconfHelper.getP4InfoBrowser(pipeconf); |
| 142 | |
| 143 | if (browser == null) { |
| 144 | log.error("Unable to get a P4Info browser for pipeconf {}, skipping decoding of all table entries"); |
| 145 | return Collections.emptyList(); |
| 146 | } |
| 147 | |
| 148 | ImmutableList.Builder<PiTableEntry> piTableEntryListBuilder = ImmutableList.builder(); |
| 149 | |
| 150 | for (TableEntry tableEntryMsg : tableEntryMsgs) { |
| 151 | try { |
| 152 | piTableEntryListBuilder.add(decodeTableEntryMsg(tableEntryMsg, browser)); |
| 153 | } catch (P4InfoBrowser.NotFoundException | EncodeException e) { |
| 154 | log.error("Unable to decode table entry message: {}", e.getMessage()); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | return piTableEntryListBuilder.build(); |
| 159 | } |
| 160 | |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 161 | /** |
| 162 | * Same as {@link #decode(Collection, PiPipeconf)} but decodes only one entry. |
| 163 | * |
| 164 | * @param tableEntryMsg table entry message |
| 165 | * @param pipeconf pipeconf |
| 166 | * @return decoded PI table entry |
| 167 | * @throws EncodeException if message cannot be decoded |
| 168 | * @throws P4InfoBrowser.NotFoundException if the required information cannot be find in the pipeconf's P4info |
| 169 | */ |
| 170 | static PiTableEntry decode(TableEntry tableEntryMsg, PiPipeconf pipeconf) |
| 171 | throws EncodeException, P4InfoBrowser.NotFoundException { |
| 172 | |
| 173 | P4InfoBrowser browser = PipeconfHelper.getP4InfoBrowser(pipeconf); |
| 174 | if (browser == null) { |
| 175 | throw new EncodeException(format("Unable to get a P4Info browser for pipeconf %s", pipeconf.id())); |
| 176 | } |
| 177 | return decodeTableEntryMsg(tableEntryMsg, browser); |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Returns a table entry protobuf message, encoded from the given table id and match key, for the given pipeconf. |
| 182 | * The returned table entry message can be only used to reference an existing entry, i.e. a read operation, and not |
| 183 | * a write one wince it misses other fields (action, priority, etc.). |
| 184 | * |
| 185 | * @param tableId table identifier |
| 186 | * @param matchKey match key |
| 187 | * @param pipeconf pipeconf |
| 188 | * @return table entry message |
| 189 | * @throws EncodeException if message cannot be encoded |
| 190 | * @throws P4InfoBrowser.NotFoundException if the required information cannot be find in the pipeconf's P4info |
| 191 | */ |
| 192 | static TableEntry encode(PiTableId tableId, PiMatchKey matchKey, PiPipeconf pipeconf) |
| 193 | throws EncodeException, P4InfoBrowser.NotFoundException { |
| 194 | |
| 195 | P4InfoBrowser browser = PipeconfHelper.getP4InfoBrowser(pipeconf); |
| 196 | TableEntry.Builder tableEntryMsgBuilder = TableEntry.newBuilder(); |
| 197 | |
Carmelo Cascone | cb0a49c | 2017-10-03 14:32:23 +0200 | [diff] [blame] | 198 | P4InfoOuterClass.Table tableInfo = browser.tables().getByName(tableId.id()); |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 199 | |
| 200 | // Table id. |
| 201 | tableEntryMsgBuilder.setTableId(tableInfo.getPreamble().getId()); |
| 202 | |
| 203 | // Field matches. |
| 204 | for (PiFieldMatch piFieldMatch : matchKey.fieldMatches()) { |
| 205 | tableEntryMsgBuilder.addMatch(encodePiFieldMatch(piFieldMatch, tableInfo, browser)); |
| 206 | } |
| 207 | |
| 208 | return tableEntryMsgBuilder.build(); |
| 209 | } |
| 210 | |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 211 | private static TableEntry encodePiTableEntry(PiTableEntry piTableEntry, P4InfoBrowser browser) |
| 212 | throws P4InfoBrowser.NotFoundException, EncodeException { |
| 213 | |
| 214 | TableEntry.Builder tableEntryMsgBuilder = TableEntry.newBuilder(); |
| 215 | |
Carmelo Cascone | cb0a49c | 2017-10-03 14:32:23 +0200 | [diff] [blame] | 216 | P4InfoOuterClass.Table tableInfo = browser.tables().getByName(piTableEntry.table().id()); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 217 | |
| 218 | // Table id. |
| 219 | tableEntryMsgBuilder.setTableId(tableInfo.getPreamble().getId()); |
| 220 | |
| 221 | // Priority. |
Yi Tseng | 088f4ce | 2017-08-15 10:54:14 -0700 | [diff] [blame] | 222 | // FIXME: check on P4Runtime if/what is the default priority. |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 223 | int priority = piTableEntry.priority().orElse(0); |
| 224 | tableEntryMsgBuilder.setPriority(priority); |
| 225 | |
| 226 | // Controller metadata (cookie) |
| 227 | tableEntryMsgBuilder.setControllerMetadata(piTableEntry.cookie()); |
| 228 | |
| 229 | // Timeout. |
| 230 | if (piTableEntry.timeout().isPresent()) { |
| 231 | log.warn("Found PI table entry with timeout set, not supported in P4Runtime: {}", piTableEntry); |
| 232 | } |
| 233 | |
| 234 | // Table action. |
| 235 | tableEntryMsgBuilder.setAction(encodePiTableAction(piTableEntry.action(), browser)); |
| 236 | |
| 237 | // Field matches. |
Carmelo Cascone | 0e896a0 | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 238 | for (PiFieldMatch piFieldMatch : piTableEntry.matchKey().fieldMatches()) { |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 239 | tableEntryMsgBuilder.addMatch(encodePiFieldMatch(piFieldMatch, tableInfo, browser)); |
| 240 | } |
| 241 | |
| 242 | return tableEntryMsgBuilder.build(); |
| 243 | } |
| 244 | |
| 245 | private static PiTableEntry decodeTableEntryMsg(TableEntry tableEntryMsg, P4InfoBrowser browser) |
| 246 | throws P4InfoBrowser.NotFoundException, EncodeException { |
| 247 | |
| 248 | PiTableEntry.Builder piTableEntryBuilder = PiTableEntry.builder(); |
| 249 | |
| 250 | P4InfoOuterClass.Table tableInfo = browser.tables().getById(tableEntryMsg.getTableId()); |
| 251 | |
| 252 | // Table id. |
| 253 | piTableEntryBuilder.forTable(PiTableId.of(tableInfo.getPreamble().getName())); |
| 254 | |
| 255 | // Priority. |
| 256 | piTableEntryBuilder.withPriority(tableEntryMsg.getPriority()); |
| 257 | |
| 258 | // Controller metadata (cookie) |
| 259 | piTableEntryBuilder.withCookie(tableEntryMsg.getControllerMetadata()); |
| 260 | |
| 261 | // Table action. |
| 262 | piTableEntryBuilder.withAction(decodeTableActionMsg(tableEntryMsg.getAction(), browser)); |
| 263 | |
| 264 | // Timeout. |
| 265 | // FIXME: how to decode table entry messages with timeout, given that the timeout value is lost after encoding? |
| 266 | |
Carmelo Cascone | 0e896a0 | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 267 | // Match key for field matches. |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 268 | piTableEntryBuilder.withMatchKey(decodeFieldMatchMsgs(tableEntryMsg.getMatchList(), tableInfo, browser)); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 269 | |
| 270 | return piTableEntryBuilder.build(); |
| 271 | } |
| 272 | |
| 273 | private static FieldMatch encodePiFieldMatch(PiFieldMatch piFieldMatch, P4InfoOuterClass.Table tableInfo, |
| 274 | P4InfoBrowser browser) |
| 275 | throws P4InfoBrowser.NotFoundException, EncodeException { |
| 276 | |
| 277 | FieldMatch.Builder fieldMatchMsgBuilder = FieldMatch.newBuilder(); |
| 278 | |
| 279 | // FIXME: check how field names for stacked headers are constructed in P4Runtime. |
| 280 | String fieldName = piFieldMatch.fieldId().id(); |
| 281 | int tableId = tableInfo.getPreamble().getId(); |
Carmelo Cascone | cb0a49c | 2017-10-03 14:32:23 +0200 | [diff] [blame] | 282 | P4InfoOuterClass.MatchField matchFieldInfo = browser.matchFields(tableId).getByName(fieldName); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 283 | String entityName = format("field match '%s' of table '%s'", |
| 284 | matchFieldInfo.getName(), tableInfo.getPreamble().getName()); |
| 285 | int fieldId = matchFieldInfo.getId(); |
| 286 | int fieldBitwidth = matchFieldInfo.getBitwidth(); |
| 287 | |
| 288 | fieldMatchMsgBuilder.setFieldId(fieldId); |
| 289 | |
| 290 | switch (piFieldMatch.type()) { |
| 291 | case EXACT: |
| 292 | PiExactFieldMatch fieldMatch = (PiExactFieldMatch) piFieldMatch; |
| 293 | ByteString exactValue = ByteString.copyFrom(fieldMatch.value().asReadOnlyBuffer()); |
| 294 | assertSize(VALUE_OF_PREFIX + entityName, exactValue, fieldBitwidth); |
| 295 | return fieldMatchMsgBuilder.setExact( |
| 296 | FieldMatch.Exact |
| 297 | .newBuilder() |
| 298 | .setValue(exactValue) |
| 299 | .build()) |
| 300 | .build(); |
| 301 | case TERNARY: |
| 302 | PiTernaryFieldMatch ternaryMatch = (PiTernaryFieldMatch) piFieldMatch; |
| 303 | ByteString ternaryValue = ByteString.copyFrom(ternaryMatch.value().asReadOnlyBuffer()); |
| 304 | ByteString ternaryMask = ByteString.copyFrom(ternaryMatch.mask().asReadOnlyBuffer()); |
| 305 | assertSize(VALUE_OF_PREFIX + entityName, ternaryValue, fieldBitwidth); |
| 306 | assertSize(MASK_OF_PREFIX + entityName, ternaryMask, fieldBitwidth); |
| 307 | return fieldMatchMsgBuilder.setTernary( |
| 308 | FieldMatch.Ternary |
| 309 | .newBuilder() |
| 310 | .setValue(ternaryValue) |
| 311 | .setMask(ternaryMask) |
| 312 | .build()) |
| 313 | .build(); |
| 314 | case LPM: |
| 315 | PiLpmFieldMatch lpmMatch = (PiLpmFieldMatch) piFieldMatch; |
| 316 | ByteString lpmValue = ByteString.copyFrom(lpmMatch.value().asReadOnlyBuffer()); |
| 317 | int lpmPrefixLen = lpmMatch.prefixLength(); |
| 318 | assertSize(VALUE_OF_PREFIX + entityName, lpmValue, fieldBitwidth); |
| 319 | assertPrefixLen(entityName, lpmPrefixLen, fieldBitwidth); |
| 320 | return fieldMatchMsgBuilder.setLpm( |
| 321 | FieldMatch.LPM.newBuilder() |
| 322 | .setValue(lpmValue) |
| 323 | .setPrefixLen(lpmPrefixLen) |
| 324 | .build()) |
| 325 | .build(); |
| 326 | case RANGE: |
| 327 | PiRangeFieldMatch rangeMatch = (PiRangeFieldMatch) piFieldMatch; |
| 328 | ByteString rangeHighValue = ByteString.copyFrom(rangeMatch.highValue().asReadOnlyBuffer()); |
| 329 | ByteString rangeLowValue = ByteString.copyFrom(rangeMatch.lowValue().asReadOnlyBuffer()); |
| 330 | assertSize(HIGH_RANGE_VALUE_OF_PREFIX + entityName, rangeHighValue, fieldBitwidth); |
| 331 | assertSize(LOW_RANGE_VALUE_OF_PREFIX + entityName, rangeLowValue, fieldBitwidth); |
| 332 | return fieldMatchMsgBuilder.setRange( |
| 333 | FieldMatch.Range.newBuilder() |
| 334 | .setHigh(rangeHighValue) |
| 335 | .setLow(rangeLowValue) |
| 336 | .build()) |
| 337 | .build(); |
| 338 | case VALID: |
| 339 | PiValidFieldMatch validMatch = (PiValidFieldMatch) piFieldMatch; |
| 340 | return fieldMatchMsgBuilder.setValid( |
| 341 | FieldMatch.Valid.newBuilder() |
| 342 | .setValue(validMatch.isValid()) |
| 343 | .build()) |
| 344 | .build(); |
| 345 | default: |
| 346 | throw new EncodeException(format( |
| 347 | "Building of match type %s not implemented", piFieldMatch.type())); |
| 348 | } |
| 349 | } |
| 350 | |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 351 | /** |
| 352 | * Returns a PI match key, decoded from the given table entry protobuf message, for the given pipeconf. |
| 353 | * |
| 354 | * @param tableEntryMsg table entry message |
| 355 | * @param pipeconf pipeconf |
| 356 | * @return PI match key |
| 357 | * @throws EncodeException if message cannot be decoded |
| 358 | * @throws P4InfoBrowser.NotFoundException if the required information cannot be find in the pipeconf's P4info |
| 359 | */ |
| 360 | static PiMatchKey decodeMatchKey(TableEntry tableEntryMsg, PiPipeconf pipeconf) |
| 361 | throws P4InfoBrowser.NotFoundException, EncodeException { |
| 362 | P4InfoBrowser browser = PipeconfHelper.getP4InfoBrowser(pipeconf); |
| 363 | P4InfoOuterClass.Table tableInfo = browser.tables().getById(tableEntryMsg.getTableId()); |
| 364 | return decodeFieldMatchMsgs(tableEntryMsg.getMatchList(), tableInfo, browser); |
| 365 | } |
| 366 | |
| 367 | private static PiMatchKey decodeFieldMatchMsgs(Collection<FieldMatch> fieldMatchs, P4InfoOuterClass.Table tableInfo, |
| 368 | P4InfoBrowser browser) |
| 369 | throws P4InfoBrowser.NotFoundException, EncodeException { |
| 370 | // Match key for field matches. |
| 371 | PiMatchKey.Builder piMatchKeyBuilder = PiMatchKey.builder(); |
| 372 | for (FieldMatch fieldMatchMsg : fieldMatchs) { |
| 373 | piMatchKeyBuilder.addFieldMatch(decodeFieldMatchMsg(fieldMatchMsg, tableInfo, browser)); |
| 374 | } |
| 375 | return piMatchKeyBuilder.build(); |
| 376 | } |
| 377 | |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 378 | private static PiFieldMatch decodeFieldMatchMsg(FieldMatch fieldMatchMsg, P4InfoOuterClass.Table tableInfo, |
| 379 | P4InfoBrowser browser) |
| 380 | throws P4InfoBrowser.NotFoundException, EncodeException { |
| 381 | |
| 382 | int tableId = tableInfo.getPreamble().getId(); |
| 383 | String fieldMatchName = browser.matchFields(tableId).getById(fieldMatchMsg.getFieldId()).getName(); |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame^] | 384 | PiMatchFieldId headerFieldId = PiMatchFieldId.of(fieldMatchName); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 385 | |
| 386 | FieldMatch.FieldMatchTypeCase typeCase = fieldMatchMsg.getFieldMatchTypeCase(); |
| 387 | |
| 388 | switch (typeCase) { |
| 389 | case EXACT: |
| 390 | FieldMatch.Exact exactFieldMatch = fieldMatchMsg.getExact(); |
| 391 | ImmutableByteSequence exactValue = copyFrom(exactFieldMatch.getValue().asReadOnlyByteBuffer()); |
| 392 | return new PiExactFieldMatch(headerFieldId, exactValue); |
| 393 | case TERNARY: |
| 394 | FieldMatch.Ternary ternaryFieldMatch = fieldMatchMsg.getTernary(); |
| 395 | ImmutableByteSequence ternaryValue = copyFrom(ternaryFieldMatch.getValue().asReadOnlyByteBuffer()); |
| 396 | ImmutableByteSequence ternaryMask = copyFrom(ternaryFieldMatch.getMask().asReadOnlyByteBuffer()); |
| 397 | return new PiTernaryFieldMatch(headerFieldId, ternaryValue, ternaryMask); |
| 398 | case LPM: |
| 399 | FieldMatch.LPM lpmFieldMatch = fieldMatchMsg.getLpm(); |
| 400 | ImmutableByteSequence lpmValue = copyFrom(lpmFieldMatch.getValue().asReadOnlyByteBuffer()); |
| 401 | int lpmPrefixLen = lpmFieldMatch.getPrefixLen(); |
| 402 | return new PiLpmFieldMatch(headerFieldId, lpmValue, lpmPrefixLen); |
| 403 | case RANGE: |
| 404 | FieldMatch.Range rangeFieldMatch = fieldMatchMsg.getRange(); |
| 405 | ImmutableByteSequence rangeHighValue = copyFrom(rangeFieldMatch.getHigh().asReadOnlyByteBuffer()); |
| 406 | ImmutableByteSequence rangeLowValue = copyFrom(rangeFieldMatch.getLow().asReadOnlyByteBuffer()); |
| 407 | return new PiRangeFieldMatch(headerFieldId, rangeLowValue, rangeHighValue); |
| 408 | case VALID: |
| 409 | FieldMatch.Valid validFieldMatch = fieldMatchMsg.getValid(); |
| 410 | return new PiValidFieldMatch(headerFieldId, validFieldMatch.getValue()); |
| 411 | default: |
| 412 | throw new EncodeException(format( |
| 413 | "Decoding of field match type '%s' not implemented", typeCase.name())); |
| 414 | } |
| 415 | } |
| 416 | |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 417 | static TableAction encodePiTableAction(PiTableAction piTableAction, P4InfoBrowser browser) |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 418 | throws P4InfoBrowser.NotFoundException, EncodeException { |
| 419 | |
| 420 | TableAction.Builder tableActionMsgBuilder = TableAction.newBuilder(); |
| 421 | |
| 422 | switch (piTableAction.type()) { |
| 423 | case ACTION: |
| 424 | PiAction piAction = (PiAction) piTableAction; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 425 | Action theAction = encodePiAction(piAction, browser); |
| 426 | tableActionMsgBuilder.setAction(theAction); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 427 | break; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 428 | case ACTION_GROUP_ID: |
| 429 | PiActionGroupId actionGroupId = (PiActionGroupId) piTableAction; |
| 430 | tableActionMsgBuilder.setActionProfileGroupId(actionGroupId.id()); |
| 431 | break; |
| 432 | case GROUP_MEMBER_ID: |
| 433 | PiActionGroupMemberId actionGroupMemberId = (PiActionGroupMemberId) piTableAction; |
| 434 | tableActionMsgBuilder.setActionProfileMemberId(actionGroupMemberId.id()); |
| 435 | break; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 436 | default: |
| 437 | throw new EncodeException( |
| 438 | format("Building of table action type %s not implemented", piTableAction.type())); |
| 439 | } |
| 440 | |
| 441 | return tableActionMsgBuilder.build(); |
| 442 | } |
| 443 | |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 444 | static PiTableAction decodeTableActionMsg(TableAction tableActionMsg, P4InfoBrowser browser) |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 445 | throws P4InfoBrowser.NotFoundException, EncodeException { |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 446 | TableAction.TypeCase typeCase = tableActionMsg.getTypeCase(); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 447 | switch (typeCase) { |
| 448 | case ACTION: |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 449 | Action actionMsg = tableActionMsg.getAction(); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 450 | return decodeActionMsg(actionMsg, browser); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 451 | default: |
| 452 | throw new EncodeException( |
| 453 | format("Decoding of table action type %s not implemented", typeCase.name())); |
| 454 | } |
| 455 | } |
| 456 | |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 457 | static Action encodePiAction(PiAction piAction, P4InfoBrowser browser) |
| 458 | throws P4InfoBrowser.NotFoundException, EncodeException { |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 459 | |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame^] | 460 | int actionId = browser.actions().getByName(piAction.id().toString()).getPreamble().getId(); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 461 | |
| 462 | Action.Builder actionMsgBuilder = |
| 463 | Action.newBuilder().setActionId(actionId); |
| 464 | |
| 465 | for (PiActionParam p : piAction.parameters()) { |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame^] | 466 | P4InfoOuterClass.Action.Param paramInfo = browser.actionParams(actionId).getByName(p.id().toString()); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 467 | ByteString paramValue = ByteString.copyFrom(p.value().asReadOnlyBuffer()); |
| 468 | assertSize(format("param '%s' of action '%s'", p.id(), piAction.id()), |
| 469 | paramValue, paramInfo.getBitwidth()); |
| 470 | actionMsgBuilder.addParams(Action.Param.newBuilder() |
| 471 | .setParamId(paramInfo.getId()) |
| 472 | .setValue(paramValue) |
| 473 | .build()); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 474 | } |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 475 | |
| 476 | return actionMsgBuilder.build(); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 477 | } |
| 478 | |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 479 | static PiAction decodeActionMsg(Action action, P4InfoBrowser browser) |
| 480 | throws P4InfoBrowser.NotFoundException { |
| 481 | P4InfoBrowser.EntityBrowser<P4InfoOuterClass.Action.Param> paramInfo = |
| 482 | browser.actionParams(action.getActionId()); |
| 483 | String actionName = browser.actions() |
| 484 | .getById(action.getActionId()) |
| 485 | .getPreamble().getName(); |
| 486 | PiActionId id = PiActionId.of(actionName); |
| 487 | List<PiActionParam> params = Lists.newArrayList(); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 488 | |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 489 | for (Action.Param p : action.getParamsList()) { |
| 490 | String paramName = paramInfo.getById(p.getParamId()).getName(); |
| 491 | ImmutableByteSequence value = ImmutableByteSequence.copyFrom(p.getValue().toByteArray()); |
| 492 | params.add(new PiActionParam(PiActionParamId.of(paramName), value)); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 493 | } |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 494 | return PiAction.builder().withId(id).withParameters(params).build(); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 495 | } |
| 496 | } |