blob: 48baa1751873373ffefd16933bc1422d1ee5bb94 [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 Moroc6f2f7f2020-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 Moro53a3cdf2021-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 Moroc6f2f7f2020-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 Moroc6f2f7f2020-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;
77 ByteString exactValue = ByteString.copyFrom(fieldMatch.value().asReadOnlyBuffer());
Daniele Moroc6f2f7f2020-12-18 10:55:57 +010078 if (!isSdnString) {
Daniele Moro5c82b0f2020-12-07 20:56:30 +010079 assertSize(VALUE_OF_PREFIX + entityName, exactValue, fieldBitwidth);
80 }
Carmelo Cascone4c289b72019-01-22 15:30:45 -080081 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 Moroc6f2f7f2020-12-18 10:55:57 +010091 if (isSdnString) {
92 sdnStringUnsupported(entityName, piFieldMatch.type());
93 }
Carmelo Cascone4c289b72019-01-22 15:30:45 -080094 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 Moroc6f2f7f2020-12-18 10:55:57 +0100107 if (isSdnString) {
108 sdnStringUnsupported(entityName, piFieldMatch.type());
109 }
Carmelo Cascone4c289b72019-01-22 15:30:45 -0800110 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 Moroc6f2f7f2020-12-18 10:55:57 +0100122 if (isSdnString) {
123 sdnStringUnsupported(entityName, piFieldMatch.type());
124 }
Carmelo Cascone4c289b72019-01-22 15:30:45 -0800125 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 Moroc6f2f7f2020-12-18 10:55:57 +0100133 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 Cascone4c289b72019-01-22 15:30:45 -0800144 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 Moro7aa13e62021-02-23 15:28:07 +0100156 final P4InfoOuterClass.MatchField matchField =
157 browser.matchFields(tablePreamble.getId())
158 .getById(message.getFieldId());
Daniele Moro53a3cdf2021-05-17 14:49:31 +0200159 final int fieldBitwidth = matchField.getBitwidth();
Daniele Moro7aa13e62021-02-23 15:28:07 +0100160 final PiMatchFieldId headerFieldId = PiMatchFieldId.of(matchField.getName());
161 final boolean isSdnString = browser.isTypeString(matchField.getTypeName());
Carmelo Cascone4c289b72019-01-22 15:30:45 -0800162
Daniele Moro53a3cdf2021-05-17 14:49:31 +0200163 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 Cascone4c289b72019-01-22 15:30:45 -0800219 }
220 }
221}