blob: 4b8fe500b981298d51f89874909a972545ef3d28 [file] [log] [blame]
Carmelo Cascone4c289b72019-01-22 15:30:45 -08001/*
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
17package org.onosproject.p4runtime.ctl.codec;
18
19import com.google.protobuf.ByteString;
20import org.onlab.util.ImmutableByteSequence;
21import org.onosproject.net.pi.model.PiMatchFieldId;
22import org.onosproject.net.pi.model.PiPipeconf;
23import org.onosproject.net.pi.runtime.PiExactFieldMatch;
24import org.onosproject.net.pi.runtime.PiFieldMatch;
25import org.onosproject.net.pi.runtime.PiLpmFieldMatch;
Daniele Morocdac24f2020-12-18 10:55:57 +010026import org.onosproject.net.pi.runtime.PiOptionalFieldMatch;
Carmelo Cascone4c289b72019-01-22 15:30:45 -080027import org.onosproject.net.pi.runtime.PiRangeFieldMatch;
28import org.onosproject.net.pi.runtime.PiTernaryFieldMatch;
29import org.onosproject.p4runtime.ctl.utils.P4InfoBrowser;
30import p4.config.v1.P4InfoOuterClass;
31import p4.v1.P4RuntimeOuterClass;
32
33import static java.lang.String.format;
Daniele Moro8eb7c0a2021-05-17 14:49:31 +020034import static org.onlab.util.ImmutableByteSequence.copyAndFit;
Carmelo Cascone4c289b72019-01-22 15:30:45 -080035import static org.onlab.util.ImmutableByteSequence.copyFrom;
36import static org.onosproject.p4runtime.ctl.codec.Utils.assertPrefixLen;
37import static org.onosproject.p4runtime.ctl.codec.Utils.assertSize;
Daniele Morocdac24f2020-12-18 10:55:57 +010038import static org.onosproject.p4runtime.ctl.codec.Utils.sdnStringUnsupported;
Carmelo Cascone4c289b72019-01-22 15:30:45 -080039
40/**
41 * Codec for P4Runtime FieldMatch. Metadata is expected to be a Preamble for
42 * P4Info.Table.
43 */
44public 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 Morocdac24f2020-12-18 10:55:57 +010070 boolean isSdnString = browser.isTypeString(matchFieldInfo.getTypeName());
Carmelo Cascone4c289b72019-01-22 15:30:45 -080071
72 messageBuilder.setFieldId(fieldId);
73
74 switch (piFieldMatch.type()) {
75 case EXACT:
76 PiExactFieldMatch fieldMatch = (PiExactFieldMatch) piFieldMatch;
Yi Tseng38213992022-03-17 16:04:06 -070077 ByteString exactValue;
78 if (isSdnString) {
79 exactValue = ByteString.copyFrom(fieldMatch.value().asReadOnlyBuffer());
80 } else {
81 exactValue = ByteString.copyFrom(fieldMatch.value().canonical().asReadOnlyBuffer());
Daniele Moroc9f115a2020-12-07 20:56:30 +010082 assertSize(VALUE_OF_PREFIX + entityName, exactValue, fieldBitwidth);
83 }
Carmelo Cascone4c289b72019-01-22 15:30:45 -080084 return messageBuilder.setExact(
85 P4RuntimeOuterClass.FieldMatch.Exact
86 .newBuilder()
87 .setValue(exactValue)
88 .build())
89 .build();
90 case TERNARY:
91 PiTernaryFieldMatch ternaryMatch = (PiTernaryFieldMatch) piFieldMatch;
Yi Tseng38213992022-03-17 16:04:06 -070092 ByteString ternaryValue = ByteString.copyFrom(ternaryMatch.value().canonical().asReadOnlyBuffer());
93 ByteString ternaryMask = ByteString.copyFrom(ternaryMatch.mask().canonical().asReadOnlyBuffer());
Daniele Morocdac24f2020-12-18 10:55:57 +010094 if (isSdnString) {
95 sdnStringUnsupported(entityName, piFieldMatch.type());
96 }
Carmelo Cascone4c289b72019-01-22 15:30:45 -080097 assertSize(VALUE_OF_PREFIX + entityName, ternaryValue, fieldBitwidth);
98 assertSize(MASK_OF_PREFIX + entityName, ternaryMask, fieldBitwidth);
99 return messageBuilder.setTernary(
100 P4RuntimeOuterClass.FieldMatch.Ternary
101 .newBuilder()
102 .setValue(ternaryValue)
103 .setMask(ternaryMask)
104 .build())
105 .build();
106 case LPM:
107 PiLpmFieldMatch lpmMatch = (PiLpmFieldMatch) piFieldMatch;
Yi Tseng38213992022-03-17 16:04:06 -0700108 ByteString lpmValue = ByteString.copyFrom(lpmMatch.value().canonical().asReadOnlyBuffer());
Carmelo Cascone4c289b72019-01-22 15:30:45 -0800109 int lpmPrefixLen = lpmMatch.prefixLength();
Daniele Morocdac24f2020-12-18 10:55:57 +0100110 if (isSdnString) {
111 sdnStringUnsupported(entityName, piFieldMatch.type());
112 }
Carmelo Cascone4c289b72019-01-22 15:30:45 -0800113 assertSize(VALUE_OF_PREFIX + entityName, lpmValue, fieldBitwidth);
114 assertPrefixLen(entityName, lpmPrefixLen, fieldBitwidth);
115 return messageBuilder.setLpm(
116 P4RuntimeOuterClass.FieldMatch.LPM.newBuilder()
117 .setValue(lpmValue)
118 .setPrefixLen(lpmPrefixLen)
119 .build())
120 .build();
121 case RANGE:
122 PiRangeFieldMatch rangeMatch = (PiRangeFieldMatch) piFieldMatch;
Yi Tseng38213992022-03-17 16:04:06 -0700123 ByteString rangeHighValue = ByteString.copyFrom(rangeMatch.highValue().canonical().asReadOnlyBuffer());
124 ByteString rangeLowValue = ByteString.copyFrom(rangeMatch.lowValue().canonical().asReadOnlyBuffer());
Daniele Morocdac24f2020-12-18 10:55:57 +0100125 if (isSdnString) {
126 sdnStringUnsupported(entityName, piFieldMatch.type());
127 }
Carmelo Cascone4c289b72019-01-22 15:30:45 -0800128 assertSize(HIGH_RANGE_VALUE_OF_PREFIX + entityName, rangeHighValue, fieldBitwidth);
129 assertSize(LOW_RANGE_VALUE_OF_PREFIX + entityName, rangeLowValue, fieldBitwidth);
130 return messageBuilder.setRange(
131 P4RuntimeOuterClass.FieldMatch.Range.newBuilder()
132 .setHigh(rangeHighValue)
133 .setLow(rangeLowValue)
134 .build())
135 .build();
Daniele Morocdac24f2020-12-18 10:55:57 +0100136 case OPTIONAL:
137 PiOptionalFieldMatch optionalMatch = (PiOptionalFieldMatch) piFieldMatch;
Yi Tseng38213992022-03-17 16:04:06 -0700138 ByteString optionalValue;
139 if (isSdnString) {
140 optionalValue = ByteString.copyFrom(optionalMatch.value().asReadOnlyBuffer());
141 } else {
142 optionalValue = ByteString.copyFrom(optionalMatch.value().canonical().asReadOnlyBuffer());
Daniele Morocdac24f2020-12-18 10:55:57 +0100143 assertSize(VALUE_OF_PREFIX + entityName, optionalValue, fieldBitwidth);
144 }
145 return messageBuilder.setOptional(
146 P4RuntimeOuterClass.FieldMatch.Optional.newBuilder()
147 .setValue(optionalValue)
148 .build())
149 .build();
Carmelo Cascone4c289b72019-01-22 15:30:45 -0800150 default:
151 throw new CodecException(format(
152 "Building of match type %s not implemented", piFieldMatch.type()));
153 }
154 }
155
156 @Override
157 public PiFieldMatch decode(
158 P4RuntimeOuterClass.FieldMatch message, P4InfoOuterClass.Preamble tablePreamble,
159 PiPipeconf pipeconf, P4InfoBrowser browser)
160 throws CodecException, P4InfoBrowser.NotFoundException {
161
Daniele Moro787a0642021-02-23 15:28:07 +0100162 final P4InfoOuterClass.MatchField matchField =
163 browser.matchFields(tablePreamble.getId())
164 .getById(message.getFieldId());
Daniele Moro8eb7c0a2021-05-17 14:49:31 +0200165 final int fieldBitwidth = matchField.getBitwidth();
Daniele Moro787a0642021-02-23 15:28:07 +0100166 final PiMatchFieldId headerFieldId = PiMatchFieldId.of(matchField.getName());
167 final boolean isSdnString = browser.isTypeString(matchField.getTypeName());
Carmelo Cascone4c289b72019-01-22 15:30:45 -0800168
Daniele Moro8eb7c0a2021-05-17 14:49:31 +0200169 final P4RuntimeOuterClass.FieldMatch.FieldMatchTypeCase typeCase = message.getFieldMatchTypeCase();
170 try {
171 switch (typeCase) {
172 case EXACT:
173 P4RuntimeOuterClass.FieldMatch.Exact exactFieldMatch = message.getExact();
174 final ImmutableByteSequence exactValue;
175 if (isSdnString) {
176 exactValue = copyFrom(new String(exactFieldMatch.getValue().toByteArray()));
177 } else {
178 exactValue = copyAndFit(
179 exactFieldMatch.getValue().asReadOnlyByteBuffer(),
180 fieldBitwidth);
181 }
182 return new PiExactFieldMatch(headerFieldId, exactValue);
183 case TERNARY:
184 P4RuntimeOuterClass.FieldMatch.Ternary ternaryFieldMatch = message.getTernary();
185 ImmutableByteSequence ternaryValue = copyAndFit(
186 ternaryFieldMatch.getValue().asReadOnlyByteBuffer(),
187 fieldBitwidth);
188 ImmutableByteSequence ternaryMask = copyAndFit(
189 ternaryFieldMatch.getMask().asReadOnlyByteBuffer(),
190 fieldBitwidth);
191 return new PiTernaryFieldMatch(headerFieldId, ternaryValue, ternaryMask);
192 case LPM:
193 P4RuntimeOuterClass.FieldMatch.LPM lpmFieldMatch = message.getLpm();
194 ImmutableByteSequence lpmValue = copyAndFit(
195 lpmFieldMatch.getValue().asReadOnlyByteBuffer(),
196 fieldBitwidth);
197 int lpmPrefixLen = lpmFieldMatch.getPrefixLen();
198 return new PiLpmFieldMatch(headerFieldId, lpmValue, lpmPrefixLen);
199 case RANGE:
200 P4RuntimeOuterClass.FieldMatch.Range rangeFieldMatch = message.getRange();
201 ImmutableByteSequence rangeHighValue = copyAndFit(
202 rangeFieldMatch.getHigh().asReadOnlyByteBuffer(),
203 fieldBitwidth);
204 ImmutableByteSequence rangeLowValue = copyAndFit(
205 rangeFieldMatch.getLow().asReadOnlyByteBuffer(),
206 fieldBitwidth);
207 return new PiRangeFieldMatch(headerFieldId, rangeLowValue, rangeHighValue);
208 case OPTIONAL:
209 P4RuntimeOuterClass.FieldMatch.Optional optionalFieldMatch = message.getOptional();
210 final ImmutableByteSequence optionalValue;
211 if (isSdnString) {
212 optionalValue = copyFrom(new String(optionalFieldMatch.getValue().toByteArray()));
213 } else {
214 optionalValue = copyAndFit(
215 optionalFieldMatch.getValue().asReadOnlyByteBuffer(),
216 fieldBitwidth);
217 }
218 return new PiOptionalFieldMatch(headerFieldId, optionalValue);
219 default:
220 throw new CodecException(format(
221 "Decoding of field match type '%s' not implemented", typeCase.name()));
222 }
223 } catch (ImmutableByteSequence.ByteSequenceTrimException e) {
224 throw new CodecException(e.getMessage());
Carmelo Cascone4c289b72019-01-22 15:30:45 -0800225 }
226 }
227}