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