Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019-present Open Networking Foundation |
| 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.codec; |
| 18 | |
| 19 | import com.google.protobuf.ByteString; |
| 20 | import org.onlab.util.ImmutableByteSequence; |
| 21 | import org.onosproject.net.pi.model.PiMatchFieldId; |
| 22 | import org.onosproject.net.pi.model.PiPipeconf; |
| 23 | import org.onosproject.net.pi.runtime.PiExactFieldMatch; |
| 24 | import org.onosproject.net.pi.runtime.PiFieldMatch; |
| 25 | import org.onosproject.net.pi.runtime.PiLpmFieldMatch; |
Daniele Moro | c6f2f7f | 2020-12-18 10:55:57 +0100 | [diff] [blame^] | 26 | import org.onosproject.net.pi.runtime.PiOptionalFieldMatch; |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 27 | import org.onosproject.net.pi.runtime.PiRangeFieldMatch; |
| 28 | import org.onosproject.net.pi.runtime.PiTernaryFieldMatch; |
| 29 | import org.onosproject.p4runtime.ctl.utils.P4InfoBrowser; |
| 30 | import p4.config.v1.P4InfoOuterClass; |
| 31 | import p4.v1.P4RuntimeOuterClass; |
| 32 | |
| 33 | import static java.lang.String.format; |
| 34 | import static org.onlab.util.ImmutableByteSequence.copyFrom; |
| 35 | import static org.onosproject.p4runtime.ctl.codec.Utils.assertPrefixLen; |
| 36 | import static org.onosproject.p4runtime.ctl.codec.Utils.assertSize; |
Daniele Moro | c6f2f7f | 2020-12-18 10:55:57 +0100 | [diff] [blame^] | 37 | import static org.onosproject.p4runtime.ctl.codec.Utils.sdnStringUnsupported; |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 38 | |
| 39 | /** |
| 40 | * Codec for P4Runtime FieldMatch. Metadata is expected to be a Preamble for |
| 41 | * P4Info.Table. |
| 42 | */ |
| 43 | public final class FieldMatchCodec |
| 44 | extends AbstractCodec<PiFieldMatch, P4RuntimeOuterClass.FieldMatch, |
| 45 | P4InfoOuterClass.Preamble> { |
| 46 | |
| 47 | private static final String VALUE_OF_PREFIX = "value of "; |
| 48 | private static final String MASK_OF_PREFIX = "mask of "; |
| 49 | private static final String HIGH_RANGE_VALUE_OF_PREFIX = "high range value of "; |
| 50 | private static final String LOW_RANGE_VALUE_OF_PREFIX = "low range value of "; |
| 51 | |
| 52 | @Override |
| 53 | public P4RuntimeOuterClass.FieldMatch encode( |
| 54 | PiFieldMatch piFieldMatch, P4InfoOuterClass.Preamble tablePreamble, |
| 55 | PiPipeconf pipeconf, P4InfoBrowser browser) |
| 56 | throws CodecException, P4InfoBrowser.NotFoundException { |
| 57 | |
| 58 | P4RuntimeOuterClass.FieldMatch.Builder messageBuilder = P4RuntimeOuterClass |
| 59 | .FieldMatch.newBuilder(); |
| 60 | |
| 61 | // FIXME: check how field names for stacked headers are constructed in P4Runtime. |
| 62 | String fieldName = piFieldMatch.fieldId().id(); |
| 63 | P4InfoOuterClass.MatchField matchFieldInfo = browser.matchFields( |
| 64 | tablePreamble.getId()).getByName(fieldName); |
| 65 | String entityName = format("field match '%s' of table '%s'", |
| 66 | matchFieldInfo.getName(), tablePreamble.getName()); |
| 67 | int fieldId = matchFieldInfo.getId(); |
| 68 | int fieldBitwidth = matchFieldInfo.getBitwidth(); |
Daniele Moro | c6f2f7f | 2020-12-18 10:55:57 +0100 | [diff] [blame^] | 69 | boolean isSdnString = browser.isTypeString(matchFieldInfo.getTypeName()); |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 70 | |
| 71 | messageBuilder.setFieldId(fieldId); |
| 72 | |
| 73 | switch (piFieldMatch.type()) { |
| 74 | case EXACT: |
| 75 | PiExactFieldMatch fieldMatch = (PiExactFieldMatch) piFieldMatch; |
| 76 | ByteString exactValue = ByteString.copyFrom(fieldMatch.value().asReadOnlyBuffer()); |
Daniele Moro | c6f2f7f | 2020-12-18 10:55:57 +0100 | [diff] [blame^] | 77 | if (!isSdnString) { |
Daniele Moro | 5c82b0f | 2020-12-07 20:56:30 +0100 | [diff] [blame] | 78 | assertSize(VALUE_OF_PREFIX + entityName, exactValue, fieldBitwidth); |
| 79 | } |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 80 | return messageBuilder.setExact( |
| 81 | P4RuntimeOuterClass.FieldMatch.Exact |
| 82 | .newBuilder() |
| 83 | .setValue(exactValue) |
| 84 | .build()) |
| 85 | .build(); |
| 86 | case TERNARY: |
| 87 | PiTernaryFieldMatch ternaryMatch = (PiTernaryFieldMatch) piFieldMatch; |
| 88 | ByteString ternaryValue = ByteString.copyFrom(ternaryMatch.value().asReadOnlyBuffer()); |
| 89 | ByteString ternaryMask = ByteString.copyFrom(ternaryMatch.mask().asReadOnlyBuffer()); |
Daniele Moro | c6f2f7f | 2020-12-18 10:55:57 +0100 | [diff] [blame^] | 90 | if (isSdnString) { |
| 91 | sdnStringUnsupported(entityName, piFieldMatch.type()); |
| 92 | } |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 93 | assertSize(VALUE_OF_PREFIX + entityName, ternaryValue, fieldBitwidth); |
| 94 | assertSize(MASK_OF_PREFIX + entityName, ternaryMask, fieldBitwidth); |
| 95 | return messageBuilder.setTernary( |
| 96 | P4RuntimeOuterClass.FieldMatch.Ternary |
| 97 | .newBuilder() |
| 98 | .setValue(ternaryValue) |
| 99 | .setMask(ternaryMask) |
| 100 | .build()) |
| 101 | .build(); |
| 102 | case LPM: |
| 103 | PiLpmFieldMatch lpmMatch = (PiLpmFieldMatch) piFieldMatch; |
| 104 | ByteString lpmValue = ByteString.copyFrom(lpmMatch.value().asReadOnlyBuffer()); |
| 105 | int lpmPrefixLen = lpmMatch.prefixLength(); |
Daniele Moro | c6f2f7f | 2020-12-18 10:55:57 +0100 | [diff] [blame^] | 106 | if (isSdnString) { |
| 107 | sdnStringUnsupported(entityName, piFieldMatch.type()); |
| 108 | } |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 109 | assertSize(VALUE_OF_PREFIX + entityName, lpmValue, fieldBitwidth); |
| 110 | assertPrefixLen(entityName, lpmPrefixLen, fieldBitwidth); |
| 111 | return messageBuilder.setLpm( |
| 112 | P4RuntimeOuterClass.FieldMatch.LPM.newBuilder() |
| 113 | .setValue(lpmValue) |
| 114 | .setPrefixLen(lpmPrefixLen) |
| 115 | .build()) |
| 116 | .build(); |
| 117 | case RANGE: |
| 118 | PiRangeFieldMatch rangeMatch = (PiRangeFieldMatch) piFieldMatch; |
| 119 | ByteString rangeHighValue = ByteString.copyFrom(rangeMatch.highValue().asReadOnlyBuffer()); |
| 120 | ByteString rangeLowValue = ByteString.copyFrom(rangeMatch.lowValue().asReadOnlyBuffer()); |
Daniele Moro | c6f2f7f | 2020-12-18 10:55:57 +0100 | [diff] [blame^] | 121 | if (isSdnString) { |
| 122 | sdnStringUnsupported(entityName, piFieldMatch.type()); |
| 123 | } |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 124 | assertSize(HIGH_RANGE_VALUE_OF_PREFIX + entityName, rangeHighValue, fieldBitwidth); |
| 125 | assertSize(LOW_RANGE_VALUE_OF_PREFIX + entityName, rangeLowValue, fieldBitwidth); |
| 126 | return messageBuilder.setRange( |
| 127 | P4RuntimeOuterClass.FieldMatch.Range.newBuilder() |
| 128 | .setHigh(rangeHighValue) |
| 129 | .setLow(rangeLowValue) |
| 130 | .build()) |
| 131 | .build(); |
Daniele Moro | c6f2f7f | 2020-12-18 10:55:57 +0100 | [diff] [blame^] | 132 | case OPTIONAL: |
| 133 | PiOptionalFieldMatch optionalMatch = (PiOptionalFieldMatch) piFieldMatch; |
| 134 | ByteString optionalValue = ByteString.copyFrom(optionalMatch.value().asReadOnlyBuffer()); |
| 135 | if (!isSdnString) { |
| 136 | assertSize(VALUE_OF_PREFIX + entityName, optionalValue, fieldBitwidth); |
| 137 | } |
| 138 | return messageBuilder.setOptional( |
| 139 | P4RuntimeOuterClass.FieldMatch.Optional.newBuilder() |
| 140 | .setValue(optionalValue) |
| 141 | .build()) |
| 142 | .build(); |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 143 | default: |
| 144 | throw new CodecException(format( |
| 145 | "Building of match type %s not implemented", piFieldMatch.type())); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public PiFieldMatch decode( |
| 151 | P4RuntimeOuterClass.FieldMatch message, P4InfoOuterClass.Preamble tablePreamble, |
| 152 | PiPipeconf pipeconf, P4InfoBrowser browser) |
| 153 | throws CodecException, P4InfoBrowser.NotFoundException { |
| 154 | |
| 155 | String fieldMatchName = browser.matchFields(tablePreamble.getId()) |
| 156 | .getById(message.getFieldId()).getName(); |
| 157 | PiMatchFieldId headerFieldId = PiMatchFieldId.of(fieldMatchName); |
| 158 | |
| 159 | P4RuntimeOuterClass.FieldMatch.FieldMatchTypeCase typeCase = message.getFieldMatchTypeCase(); |
| 160 | |
| 161 | switch (typeCase) { |
| 162 | case EXACT: |
| 163 | P4RuntimeOuterClass.FieldMatch.Exact exactFieldMatch = message.getExact(); |
| 164 | ImmutableByteSequence exactValue = copyFrom(exactFieldMatch.getValue().asReadOnlyByteBuffer()); |
| 165 | return new PiExactFieldMatch(headerFieldId, exactValue); |
| 166 | case TERNARY: |
| 167 | P4RuntimeOuterClass.FieldMatch.Ternary ternaryFieldMatch = message.getTernary(); |
| 168 | ImmutableByteSequence ternaryValue = copyFrom(ternaryFieldMatch.getValue().asReadOnlyByteBuffer()); |
| 169 | ImmutableByteSequence ternaryMask = copyFrom(ternaryFieldMatch.getMask().asReadOnlyByteBuffer()); |
| 170 | return new PiTernaryFieldMatch(headerFieldId, ternaryValue, ternaryMask); |
| 171 | case LPM: |
| 172 | P4RuntimeOuterClass.FieldMatch.LPM lpmFieldMatch = message.getLpm(); |
| 173 | ImmutableByteSequence lpmValue = copyFrom(lpmFieldMatch.getValue().asReadOnlyByteBuffer()); |
| 174 | int lpmPrefixLen = lpmFieldMatch.getPrefixLen(); |
| 175 | return new PiLpmFieldMatch(headerFieldId, lpmValue, lpmPrefixLen); |
| 176 | case RANGE: |
| 177 | P4RuntimeOuterClass.FieldMatch.Range rangeFieldMatch = message.getRange(); |
| 178 | ImmutableByteSequence rangeHighValue = copyFrom(rangeFieldMatch.getHigh().asReadOnlyByteBuffer()); |
| 179 | ImmutableByteSequence rangeLowValue = copyFrom(rangeFieldMatch.getLow().asReadOnlyByteBuffer()); |
| 180 | return new PiRangeFieldMatch(headerFieldId, rangeLowValue, rangeHighValue); |
Daniele Moro | c6f2f7f | 2020-12-18 10:55:57 +0100 | [diff] [blame^] | 181 | case OPTIONAL: |
| 182 | P4RuntimeOuterClass.FieldMatch.Optional optionalFieldMatch = message.getOptional(); |
| 183 | ImmutableByteSequence optionalValue = copyFrom(optionalFieldMatch.getValue().asReadOnlyByteBuffer()); |
| 184 | return new PiOptionalFieldMatch(headerFieldId, optionalValue); |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 185 | default: |
| 186 | throw new CodecException(format( |
| 187 | "Decoding of field match type '%s' not implemented", typeCase.name())); |
| 188 | } |
| 189 | } |
| 190 | } |