Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-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; |
| 18 | |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 19 | import org.onosproject.net.pi.model.PiCounterId; |
| 20 | import org.onosproject.net.pi.model.PiCounterType; |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 21 | import org.onosproject.net.pi.model.PiPipeconf; |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 22 | import org.onosproject.net.pi.model.PiTableId; |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 23 | import org.onosproject.net.pi.runtime.PiCounterCellData; |
| 24 | import org.onosproject.net.pi.runtime.PiCounterCellId; |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 25 | import org.onosproject.net.pi.runtime.PiTableEntry; |
| 26 | import org.slf4j.Logger; |
Carmelo Cascone | 6af4e17 | 2018-06-15 16:01:30 +0200 | [diff] [blame] | 27 | import p4.v1.P4RuntimeOuterClass; |
| 28 | import p4.v1.P4RuntimeOuterClass.CounterData; |
| 29 | import p4.v1.P4RuntimeOuterClass.CounterEntry; |
| 30 | import p4.v1.P4RuntimeOuterClass.DirectCounterEntry; |
| 31 | import p4.v1.P4RuntimeOuterClass.Entity; |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 32 | |
| 33 | import java.util.Collection; |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 34 | import java.util.Collections; |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 35 | import java.util.Objects; |
| 36 | import java.util.stream.Collectors; |
| 37 | |
| 38 | import static java.lang.String.format; |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 39 | import static org.onosproject.p4runtime.ctl.P4RuntimeUtils.indexMsg; |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 40 | import static org.slf4j.LoggerFactory.getLogger; |
Carmelo Cascone | 6af4e17 | 2018-06-15 16:01:30 +0200 | [diff] [blame] | 41 | import static p4.v1.P4RuntimeOuterClass.Entity.EntityCase.COUNTER_ENTRY; |
| 42 | import static p4.v1.P4RuntimeOuterClass.Entity.EntityCase.DIRECT_COUNTER_ENTRY; |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 43 | |
| 44 | /** |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 45 | * Encoder/decoder of PI counter IDs to counter entry protobuf messages, and |
| 46 | * vice versa. |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 47 | */ |
| 48 | final class CounterEntryCodec { |
| 49 | |
| 50 | private static final Logger log = getLogger(CounterEntryCodec.class); |
| 51 | |
| 52 | private CounterEntryCodec() { |
| 53 | // Hides constructor. |
| 54 | } |
| 55 | |
| 56 | /** |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 57 | * Returns a collection of P4Runtime entity protobuf messages describing |
| 58 | * both counter or direct counter entries, encoded from the given collection |
| 59 | * of PI counter cell identifiers, for the given pipeconf. If a PI counter |
| 60 | * cell identifier cannot be encoded, it is skipped, hence the returned |
| 61 | * collection might have different size than the input one. |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 62 | * |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 63 | * @param cellIds counter cell identifiers |
| 64 | * @param pipeconf pipeconf |
| 65 | * @return collection of entity messages describing both counter or direct |
| 66 | * counter entries |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 67 | */ |
| 68 | static Collection<Entity> encodePiCounterCellIds(Collection<PiCounterCellId> cellIds, |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 69 | PiPipeconf pipeconf) { |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 70 | |
| 71 | final P4InfoBrowser browser = PipeconfHelper.getP4InfoBrowser(pipeconf); |
| 72 | |
| 73 | if (browser == null) { |
| 74 | log.error("Unable to get a P4Info browser for pipeconf {}", pipeconf.id()); |
| 75 | return Collections.emptyList(); |
| 76 | } |
| 77 | |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 78 | return cellIds |
| 79 | .stream() |
| 80 | .map(cellId -> { |
| 81 | try { |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 82 | return encodePiCounterCellId(cellId, pipeconf, browser); |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 83 | } catch (P4InfoBrowser.NotFoundException | EncodeException e) { |
| 84 | log.warn("Unable to encode PI counter cell id: {}", e.getMessage()); |
| 85 | return null; |
| 86 | } |
| 87 | }) |
| 88 | .filter(Objects::nonNull) |
| 89 | .collect(Collectors.toList()); |
| 90 | } |
| 91 | |
| 92 | /** |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 93 | * Returns a collection of P4Runtime entity protobuf messages to be used in |
| 94 | * requests to read all cells from the given counter identifiers. Works for |
| 95 | * both indirect or direct counters. If a PI counter identifier cannot be |
| 96 | * encoded, it is skipped, hence the returned collection might have |
| 97 | * different size than the input one. |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 98 | * |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 99 | * @param counterIds counter identifiers |
| 100 | * @param pipeconf pipeconf |
| 101 | * @return collection of entity messages |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 102 | */ |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 103 | static Collection<Entity> readAllCellsEntities(Collection<PiCounterId> counterIds, |
| 104 | PiPipeconf pipeconf) { |
| 105 | final P4InfoBrowser browser = PipeconfHelper.getP4InfoBrowser(pipeconf); |
| 106 | |
| 107 | if (browser == null) { |
| 108 | log.error("Unable to get a P4Info browser for pipeconf {}", pipeconf.id()); |
| 109 | return Collections.emptyList(); |
| 110 | } |
| 111 | |
| 112 | return counterIds |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 113 | .stream() |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 114 | .map(counterId -> { |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 115 | try { |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 116 | return readAllCellsEntity(counterId, pipeconf, browser); |
| 117 | } catch (P4InfoBrowser.NotFoundException | EncodeException e) { |
| 118 | log.warn("Unable to encode counter ID to read-all-cells entity: {}", |
| 119 | e.getMessage()); |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 120 | return null; |
| 121 | } |
| 122 | }) |
| 123 | .filter(Objects::nonNull) |
| 124 | .collect(Collectors.toList()); |
| 125 | } |
| 126 | |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 127 | /** |
| 128 | * Returns a collection of PI counter cell data, decoded from the given |
| 129 | * P4Runtime entity protobuf messages describing both counter or direct |
| 130 | * counter entries, and pipeconf. If an entity message cannot be encoded, it |
| 131 | * is skipped, hence the returned collection might have different size than |
| 132 | * the input one. |
| 133 | * |
| 134 | * @param entities P4Runtime entity messages |
| 135 | * @param pipeconf pipeconf |
| 136 | * @return collection of PI counter cell data |
| 137 | */ |
| 138 | static Collection<PiCounterCellData> decodeCounterEntities(Collection<Entity> entities, |
| 139 | PiPipeconf pipeconf) { |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 140 | |
| 141 | final P4InfoBrowser browser = PipeconfHelper.getP4InfoBrowser(pipeconf); |
| 142 | |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 143 | if (browser == null) { |
| 144 | log.error("Unable to get a P4Info browser for pipeconf {}", pipeconf.id()); |
| 145 | return Collections.emptyList(); |
| 146 | } |
| 147 | |
| 148 | return entities |
| 149 | .stream() |
| 150 | .filter(entity -> entity.getEntityCase() == COUNTER_ENTRY || |
| 151 | entity.getEntityCase() == DIRECT_COUNTER_ENTRY) |
| 152 | .map(entity -> { |
| 153 | try { |
| 154 | return decodeCounterEntity(entity, pipeconf, browser); |
| 155 | } catch (EncodeException | P4InfoBrowser.NotFoundException e) { |
| 156 | log.warn("Unable to decode counter entity message: {}", |
| 157 | e.getMessage()); |
| 158 | return null; |
| 159 | } |
| 160 | }) |
| 161 | .filter(Objects::nonNull) |
| 162 | .collect(Collectors.toList()); |
| 163 | } |
| 164 | |
| 165 | private static Entity encodePiCounterCellId(PiCounterCellId cellId, |
| 166 | PiPipeconf pipeconf, |
| 167 | P4InfoBrowser browser) |
| 168 | throws P4InfoBrowser.NotFoundException, EncodeException { |
| 169 | |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 170 | int counterId; |
| 171 | Entity entity; |
| 172 | // Encode PI cell ID into entity message and add to read request. |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 173 | switch (cellId.counterType()) { |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 174 | case INDIRECT: |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 175 | counterId = browser.counters() |
| 176 | .getByName(cellId.counterId().id()) |
| 177 | .getPreamble() |
| 178 | .getId(); |
| 179 | entity = Entity.newBuilder() |
| 180 | .setCounterEntry( |
| 181 | CounterEntry.newBuilder() |
| 182 | .setCounterId(counterId) |
| 183 | .setIndex(indexMsg(cellId.index())) |
| 184 | .build()) |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 185 | .build(); |
| 186 | break; |
| 187 | case DIRECT: |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 188 | DirectCounterEntry.Builder entryBuilder = DirectCounterEntry.newBuilder(); |
| 189 | entryBuilder.setTableEntry( |
| 190 | TableEntryEncoder.encode(cellId.tableEntry(), pipeconf)); |
| 191 | entity = Entity.newBuilder() |
| 192 | .setDirectCounterEntry(entryBuilder.build()) |
| 193 | .build(); |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 194 | break; |
| 195 | default: |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 196 | throw new EncodeException(format( |
| 197 | "Unrecognized PI counter cell ID type '%s'", |
| 198 | cellId.counterType())); |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 199 | } |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 200 | |
| 201 | return entity; |
| 202 | } |
| 203 | |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 204 | private static Entity readAllCellsEntity(PiCounterId counterId, |
| 205 | PiPipeconf pipeconf, |
| 206 | P4InfoBrowser browser) |
| 207 | throws P4InfoBrowser.NotFoundException, EncodeException { |
| 208 | |
| 209 | if (!pipeconf.pipelineModel().counter(counterId).isPresent()) { |
| 210 | throw new EncodeException(format( |
| 211 | "not such counter '%s' in pipeline model", counterId)); |
| 212 | } |
| 213 | final PiCounterType counterType = pipeconf.pipelineModel() |
| 214 | .counter(counterId).get().counterType(); |
| 215 | |
| 216 | switch (counterType) { |
| 217 | case INDIRECT: |
| 218 | final int p4InfoCounterId = browser.counters() |
| 219 | .getByName(counterId.id()) |
| 220 | .getPreamble().getId(); |
| 221 | return Entity.newBuilder().setCounterEntry( |
| 222 | P4RuntimeOuterClass.CounterEntry.newBuilder() |
| 223 | // Index unset to read all cells |
| 224 | .setCounterId(p4InfoCounterId) |
| 225 | .build()) |
| 226 | .build(); |
| 227 | case DIRECT: |
| 228 | final PiTableId tableId = pipeconf.pipelineModel() |
| 229 | .counter(counterId).get().table(); |
| 230 | if (tableId == null) { |
| 231 | throw new EncodeException(format( |
| 232 | "null table for direct counter '%s'", counterId)); |
| 233 | } |
| 234 | final int p4TableId = browser.tables().getByName(tableId.id()) |
| 235 | .getPreamble().getId(); |
| 236 | return Entity.newBuilder().setDirectCounterEntry( |
| 237 | P4RuntimeOuterClass.DirectCounterEntry.newBuilder() |
| 238 | .setTableEntry( |
| 239 | // Match unset to read all cells |
| 240 | P4RuntimeOuterClass.TableEntry.newBuilder() |
| 241 | .setTableId(p4TableId) |
| 242 | .build()) |
| 243 | .build()) |
| 244 | .build(); |
| 245 | default: |
| 246 | throw new EncodeException(format( |
| 247 | "unrecognized PI counter type '%s'", counterType)); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | private static PiCounterCellData decodeCounterEntity(Entity entity, |
| 252 | PiPipeconf pipeconf, |
| 253 | P4InfoBrowser browser) |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 254 | throws EncodeException, P4InfoBrowser.NotFoundException { |
| 255 | |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 256 | CounterData counterData; |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 257 | PiCounterCellId piCellId; |
| 258 | |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 259 | if (entity.getEntityCase() == COUNTER_ENTRY) { |
| 260 | String counterName = browser.counters() |
| 261 | .getById(entity.getCounterEntry().getCounterId()) |
| 262 | .getPreamble() |
| 263 | .getName(); |
| 264 | piCellId = PiCounterCellId.ofIndirect( |
| 265 | PiCounterId.of(counterName), |
| 266 | entity.getCounterEntry().getIndex().getIndex()); |
| 267 | counterData = entity.getCounterEntry().getData(); |
| 268 | } else if (entity.getEntityCase() == DIRECT_COUNTER_ENTRY) { |
| 269 | PiTableEntry piTableEntry = TableEntryEncoder.decode( |
| 270 | entity.getDirectCounterEntry().getTableEntry(), pipeconf); |
| 271 | piCellId = PiCounterCellId.ofDirect(piTableEntry); |
| 272 | counterData = entity.getDirectCounterEntry().getData(); |
| 273 | } else { |
| 274 | throw new EncodeException(format( |
| 275 | "Unrecognized entity type '%s' in P4Runtime message", |
| 276 | entity.getEntityCase().name())); |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 277 | } |
| 278 | |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 279 | return new PiCounterCellData(piCellId, |
| 280 | counterData.getPacketCount(), |
| 281 | counterData.getByteCount()); |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 282 | } |
| 283 | } |