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