Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [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 | |
Andrea Campanella | 0288c87 | 2017-08-07 18:32:51 +0200 | [diff] [blame] | 17 | package org.onosproject.drivers.p4runtime; |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 18 | |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableList; |
| 20 | import com.google.common.collect.Lists; |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 21 | import com.google.common.collect.Maps; |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame] | 22 | import com.google.common.util.concurrent.Futures; |
| 23 | import com.google.common.util.concurrent.Striped; |
CyberHasH | 39b808b | 2019-10-11 05:59:12 +0800 | [diff] [blame] | 24 | import org.onosproject.drivers.p4runtime.mirror.P4RuntimeDefaultEntryMirror; |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 25 | import org.onosproject.drivers.p4runtime.mirror.P4RuntimeTableMirror; |
| 26 | import org.onosproject.drivers.p4runtime.mirror.TimedEntry; |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 27 | import org.onosproject.net.flow.DefaultFlowEntry; |
| 28 | import org.onosproject.net.flow.FlowEntry; |
| 29 | import org.onosproject.net.flow.FlowRule; |
| 30 | import org.onosproject.net.flow.FlowRuleProgrammable; |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 31 | import org.onosproject.net.pi.model.PiCounterType; |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 32 | import org.onosproject.net.pi.model.PiPipelineModel; |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 33 | import org.onosproject.net.pi.model.PiTableId; |
steven30801 | 7632e15 | 2018-10-20 00:51:08 +0800 | [diff] [blame] | 34 | import org.onosproject.net.pi.runtime.PiCounterCell; |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 35 | import org.onosproject.net.pi.runtime.PiCounterCellData; |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 36 | import org.onosproject.net.pi.runtime.PiCounterCellHandle; |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 37 | import org.onosproject.net.pi.runtime.PiCounterCellId; |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 38 | import org.onosproject.net.pi.runtime.PiEntityType; |
| 39 | import org.onosproject.net.pi.runtime.PiHandle; |
CyberHasH | 39b808b | 2019-10-11 05:59:12 +0800 | [diff] [blame] | 40 | import org.onosproject.net.pi.runtime.PiMatchKey; |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 41 | import org.onosproject.net.pi.runtime.PiTableEntry; |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 42 | import org.onosproject.net.pi.runtime.PiTableEntryHandle; |
| 43 | import org.onosproject.net.pi.service.PiFlowRuleTranslator; |
| 44 | import org.onosproject.net.pi.service.PiTranslatedEntity; |
Carmelo Cascone | 326ad2d | 2017-11-28 18:09:13 -0800 | [diff] [blame] | 45 | import org.onosproject.net.pi.service.PiTranslationException; |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 46 | import org.onosproject.p4runtime.api.P4RuntimeReadClient; |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 47 | import org.onosproject.p4runtime.api.P4RuntimeWriteClient.UpdateType; |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame] | 48 | import org.onosproject.p4runtime.api.P4RuntimeWriteClient.WriteRequest; |
| 49 | import org.onosproject.p4runtime.api.P4RuntimeWriteClient.WriteResponse; |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 50 | |
| 51 | import java.util.Collection; |
| 52 | import java.util.Collections; |
Manjunath Vanaraj | 59ad657 | 2017-12-26 11:10:57 +0530 | [diff] [blame] | 53 | import java.util.List; |
Carmelo Cascone | 3da671a | 2018-02-12 10:43:35 -0800 | [diff] [blame] | 54 | import java.util.Map; |
Carmelo Cascone | 2660097 | 2018-09-10 00:23:20 -0700 | [diff] [blame] | 55 | import java.util.Objects; |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 56 | import java.util.Optional; |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 57 | import java.util.Set; |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame] | 58 | import java.util.concurrent.CompletableFuture; |
| 59 | import java.util.concurrent.locks.Lock; |
Carmelo Cascone | fe99be9 | 2017-09-11 21:55:54 +0200 | [diff] [blame] | 60 | import java.util.stream.Collectors; |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 61 | |
CyberHasH | 39b808b | 2019-10-11 05:59:12 +0800 | [diff] [blame] | 62 | import static org.onosproject.drivers.p4runtime.P4RuntimeDriverProperties.DEFAULT_DELETE_BEFORE_UPDATE; |
| 63 | import static org.onosproject.drivers.p4runtime.P4RuntimeDriverProperties.DEFAULT_READ_COUNTERS_WITH_TABLE_ENTRIES; |
| 64 | import static org.onosproject.drivers.p4runtime.P4RuntimeDriverProperties.DEFAULT_READ_FROM_MIRROR; |
| 65 | import static org.onosproject.drivers.p4runtime.P4RuntimeDriverProperties.DEFAULT_SUPPORT_DEFAULT_TABLE_ENTRY; |
| 66 | import static org.onosproject.drivers.p4runtime.P4RuntimeDriverProperties.DEFAULT_SUPPORT_TABLE_COUNTERS; |
| 67 | import static org.onosproject.drivers.p4runtime.P4RuntimeDriverProperties.DELETE_BEFORE_UPDATE; |
| 68 | import static org.onosproject.drivers.p4runtime.P4RuntimeDriverProperties.READ_COUNTERS_WITH_TABLE_ENTRIES; |
| 69 | import static org.onosproject.drivers.p4runtime.P4RuntimeDriverProperties.READ_FROM_MIRROR; |
| 70 | import static org.onosproject.drivers.p4runtime.P4RuntimeDriverProperties.SUPPORT_DEFAULT_TABLE_ENTRY; |
| 71 | import static org.onosproject.drivers.p4runtime.P4RuntimeDriverProperties.SUPPORT_TABLE_COUNTERS; |
Andrea Campanella | 0288c87 | 2017-08-07 18:32:51 +0200 | [diff] [blame] | 72 | import static org.onosproject.drivers.p4runtime.P4RuntimeFlowRuleProgrammable.Operation.APPLY; |
| 73 | import static org.onosproject.drivers.p4runtime.P4RuntimeFlowRuleProgrammable.Operation.REMOVE; |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 74 | import static org.onosproject.net.flow.FlowEntry.FlowEntryState.ADDED; |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 75 | import static org.onosproject.p4runtime.api.P4RuntimeWriteClient.UpdateType.DELETE; |
| 76 | import static org.onosproject.p4runtime.api.P4RuntimeWriteClient.UpdateType.INSERT; |
| 77 | import static org.onosproject.p4runtime.api.P4RuntimeWriteClient.UpdateType.MODIFY; |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 78 | |
| 79 | /** |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 80 | * Implementation of the flow rule programmable behaviour for P4Runtime. |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 81 | */ |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 82 | public class P4RuntimeFlowRuleProgrammable |
| 83 | extends AbstractP4RuntimeHandlerBehaviour |
| 84 | implements FlowRuleProgrammable { |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 85 | |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame] | 86 | // Used to make sure concurrent calls to write flow rules are serialized so |
| 87 | // that each request gets consistent access to mirror state. |
| 88 | private static final Striped<Lock> WRITE_LOCKS = Striped.lock(30); |
| 89 | |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 90 | private PiPipelineModel pipelineModel; |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 91 | private P4RuntimeTableMirror tableMirror; |
| 92 | private PiFlowRuleTranslator translator; |
CyberHasH | 39b808b | 2019-10-11 05:59:12 +0800 | [diff] [blame] | 93 | private P4RuntimeDefaultEntryMirror defaultEntryMirror; |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 94 | |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 95 | @Override |
Carmelo Cascone | c32976e | 2019-04-08 14:50:52 -0700 | [diff] [blame] | 96 | protected boolean setupBehaviour(String opName) { |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 97 | |
Carmelo Cascone | c32976e | 2019-04-08 14:50:52 -0700 | [diff] [blame] | 98 | if (!super.setupBehaviour(opName)) { |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 99 | return false; |
| 100 | } |
| 101 | |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 102 | pipelineModel = pipeconf.pipelineModel(); |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 103 | tableMirror = handler().get(P4RuntimeTableMirror.class); |
Yi Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame] | 104 | translator = translationService.flowRuleTranslator(); |
CyberHasH | 39b808b | 2019-10-11 05:59:12 +0800 | [diff] [blame] | 105 | defaultEntryMirror = handler().get(P4RuntimeDefaultEntryMirror.class); |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 106 | return true; |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public Collection<FlowEntry> getFlowEntries() { |
| 111 | |
Carmelo Cascone | c32976e | 2019-04-08 14:50:52 -0700 | [diff] [blame] | 112 | if (!setupBehaviour("getFlowEntries()")) { |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 113 | return Collections.emptyList(); |
| 114 | } |
| 115 | |
CyberHasH | 39b808b | 2019-10-11 05:59:12 +0800 | [diff] [blame] | 116 | if (driverBoolProperty(READ_FROM_MIRROR, |
Carmelo Cascone | 5e1ae25 | 2019-11-21 14:42:38 -0800 | [diff] [blame] | 117 | DEFAULT_READ_FROM_MIRROR)) { |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 118 | return getFlowEntriesFromMirror(); |
Carmelo Cascone | fe99be9 | 2017-09-11 21:55:54 +0200 | [diff] [blame] | 119 | } |
| 120 | |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 121 | final ImmutableList.Builder<FlowEntry> result = ImmutableList.builder(); |
| 122 | final List<PiTableEntry> inconsistentEntries = Lists.newArrayList(); |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 123 | |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 124 | // Read table entries from device. |
| 125 | final Collection<PiTableEntry> deviceEntries = getAllTableEntriesFromDevice(); |
| 126 | if (deviceEntries == null) { |
| 127 | // Potential error at the client level. |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 128 | return Collections.emptyList(); |
| 129 | } |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 130 | |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 131 | // Synchronize mirror with the device state. |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 132 | tableMirror.sync(deviceId, deviceEntries); |
| 133 | |
| 134 | if (deviceEntries.isEmpty()) { |
| 135 | // Nothing to do. |
| 136 | return Collections.emptyList(); |
| 137 | } |
| 138 | |
Carmelo Cascone | c32976e | 2019-04-08 14:50:52 -0700 | [diff] [blame] | 139 | final Map<PiTableEntryHandle, PiCounterCellData> counterCellMap = |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 140 | readEntryCounters(deviceEntries); |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 141 | // Forge flow entries with counter values. |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 142 | for (PiTableEntry entry : deviceEntries) { |
Carmelo Cascone | c32976e | 2019-04-08 14:50:52 -0700 | [diff] [blame] | 143 | final PiTableEntryHandle handle = entry.handle(deviceId); |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 144 | final FlowEntry flowEntry = forgeFlowEntry( |
Carmelo Cascone | c32976e | 2019-04-08 14:50:52 -0700 | [diff] [blame] | 145 | entry, handle, counterCellMap.get(handle)); |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 146 | if (flowEntry == null) { |
| 147 | // Entry is on device but unknown to translation service or |
Carmelo Cascone | 5e1ae25 | 2019-11-21 14:42:38 -0800 | [diff] [blame] | 148 | // device mirror. Inconsistent. Mark for removal if this is not |
| 149 | // an original default entry (i.e, the same defined in the P4 |
| 150 | // program via default_action, which cannot be removed.) |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 151 | if (!isOriginalDefaultEntry(entry)) { |
| 152 | inconsistentEntries.add(entry); |
| 153 | } |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 154 | } else { |
| 155 | result.add(flowEntry); |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
pierventre | b1769a9 | 2021-02-12 17:22:38 +0100 | [diff] [blame^] | 159 | // Default entries need to be treated in a different way according to the spec: |
| 160 | // the client can modify (reset or update) them but cannot remove the entries |
| 161 | List<PiTableEntry> inconsistentDefaultEntries = Lists.newArrayList(); |
| 162 | List<PiTableEntry> tempDefaultEntries = inconsistentEntries.stream() |
| 163 | .filter(PiTableEntry::isDefaultAction) |
| 164 | .collect(Collectors.toList()); |
| 165 | inconsistentEntries.removeAll(tempDefaultEntries); |
| 166 | // Once we have removed the default entry from inconsistentEntries we need to |
| 167 | // craft for each default entry a copy without the action field. According to |
| 168 | // the spec leaving the action field unset will reset the original default entry. |
| 169 | tempDefaultEntries.forEach(piTableEntry -> { |
| 170 | PiTableEntry resetEntry = PiTableEntry.builder() |
| 171 | .forTable(piTableEntry.table()).build(); |
| 172 | inconsistentDefaultEntries.add(resetEntry); |
| 173 | }); |
| 174 | |
| 175 | // Clean up of inconsistent entries. |
| 176 | if (!inconsistentEntries.isEmpty() || !inconsistentDefaultEntries.isEmpty()) { |
| 177 | WriteRequest writeRequest = client.write(p4DeviceId, pipeconf); |
| 178 | // Trigger remove of inconsistent entries. |
| 179 | if (!inconsistentEntries.isEmpty()) { |
| 180 | log.warn("Found {} inconsistent table entries on {}, removing them...", |
| 181 | inconsistentEntries.size(), deviceId); |
| 182 | writeRequest = writeRequest.entities(inconsistentEntries, DELETE); |
| 183 | } |
| 184 | |
| 185 | // Trigger reset of inconsistent default entries. |
| 186 | if (!inconsistentDefaultEntries.isEmpty()) { |
| 187 | log.warn("Found {} inconsistent default table entries on {}, resetting them...", |
| 188 | inconsistentDefaultEntries.size(), deviceId); |
| 189 | writeRequest = writeRequest.entities(inconsistentDefaultEntries, MODIFY); |
| 190 | } |
| 191 | |
| 192 | // Submit delete request for non-default entries and modify request |
| 193 | // for default entries. Updates mirror when done. |
| 194 | writeRequest.submit().whenComplete((response, ex) -> { |
Carmelo Cascone | b4863b3 | 2019-03-13 18:54:34 -0700 | [diff] [blame] | 195 | if (ex != null) { |
| 196 | log.error("Exception removing inconsistent table entries", ex); |
| 197 | } else { |
| 198 | log.debug("Successfully removed {} out of {} inconsistent entries", |
pierventre | b1769a9 | 2021-02-12 17:22:38 +0100 | [diff] [blame^] | 199 | response.success().size(), response.all().size()); |
Carmelo Cascone | b4863b3 | 2019-03-13 18:54:34 -0700 | [diff] [blame] | 200 | } |
pierventre | b1769a9 | 2021-02-12 17:22:38 +0100 | [diff] [blame^] | 201 | // We can use the entity as the handle does not contain the action field |
| 202 | // so the key will be removed even if the table entry is different |
| 203 | response.success().forEach(entity -> tableMirror.remove((PiTableEntryHandle) entity.handle())); |
Carmelo Cascone | b4863b3 | 2019-03-13 18:54:34 -0700 | [diff] [blame] | 204 | }); |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 205 | } |
| 206 | |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 207 | return result.build(); |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 208 | } |
| 209 | |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 210 | private Collection<PiTableEntry> getAllTableEntriesFromDevice() { |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 211 | final P4RuntimeReadClient.ReadRequest request = client.read( |
| 212 | p4DeviceId, pipeconf); |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 213 | // Read entries from all non-constant tables, including default ones. |
| 214 | pipelineModel.tables().stream() |
| 215 | .filter(t -> !t.isConstantTable()) |
| 216 | .forEach(t -> { |
| 217 | request.tableEntries(t.id()); |
Carmelo Cascone | 1ae2506 | 2019-03-07 16:02:22 -0800 | [diff] [blame] | 218 | if (driverBoolProperty(SUPPORT_DEFAULT_TABLE_ENTRY, |
| 219 | DEFAULT_SUPPORT_DEFAULT_TABLE_ENTRY) && |
Carmelo Cascone | 5e1ae25 | 2019-11-21 14:42:38 -0800 | [diff] [blame] | 220 | t.constDefaultAction().isEmpty()) { |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 221 | request.defaultTableEntry(t.id()); |
| 222 | } |
| 223 | }); |
| 224 | final P4RuntimeReadClient.ReadResponse response = request.submitSync(); |
| 225 | if (!response.isSuccess()) { |
| 226 | return null; |
| 227 | } |
| 228 | return response.all(PiTableEntry.class).stream() |
| 229 | // Device implementation might return duplicate entries. For |
| 230 | // example if reading only default ones is not supported and |
| 231 | // non-default entries are returned, by using distinct() we |
| 232 | // are robust against that possibility. |
| 233 | .distinct() |
| 234 | .collect(Collectors.toList()); |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 235 | } |
| 236 | |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 237 | @Override |
| 238 | public Collection<FlowRule> applyFlowRules(Collection<FlowRule> rules) { |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 239 | return processFlowRules(rules, APPLY); |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | @Override |
| 243 | public Collection<FlowRule> removeFlowRules(Collection<FlowRule> rules) { |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 244 | return processFlowRules(rules, REMOVE); |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 245 | } |
| 246 | |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 247 | private FlowEntry forgeFlowEntry(PiTableEntry entry, |
Carmelo Cascone | c32976e | 2019-04-08 14:50:52 -0700 | [diff] [blame] | 248 | PiTableEntryHandle handle, |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 249 | PiCounterCellData cellData) { |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 250 | final Optional<PiTranslatedEntity<FlowRule, PiTableEntry>> |
| 251 | translatedEntity = translator.lookup(handle); |
| 252 | final TimedEntry<PiTableEntry> timedEntry = tableMirror.get(handle); |
| 253 | |
CyberHasH | 39b808b | 2019-10-11 05:59:12 +0800 | [diff] [blame] | 254 | // A default entry might not be present in the translation store if it |
| 255 | // was not inserted by an app. No need to log. |
Carmelo Cascone | 5e1ae25 | 2019-11-21 14:42:38 -0800 | [diff] [blame] | 256 | if (translatedEntity.isEmpty()) { |
CyberHasH | 39b808b | 2019-10-11 05:59:12 +0800 | [diff] [blame] | 257 | if (!isOriginalDefaultEntry(entry)) { |
| 258 | log.warn("Table entry handle not found in translation store: {}", handle); |
| 259 | } |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 260 | return null; |
| 261 | } |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 262 | if (!translatedEntity.get().translated().equals(entry)) { |
| 263 | log.warn("Table entry obtained from device {} is different from " + |
| 264 | "one in in translation store: device={}, store={}", |
| 265 | deviceId, entry, translatedEntity.get().translated()); |
| 266 | return null; |
| 267 | } |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 268 | if (timedEntry == null) { |
Carmelo Cascone | 2660097 | 2018-09-10 00:23:20 -0700 | [diff] [blame] | 269 | log.warn("Table entry handle not found in device mirror: {}", handle); |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 270 | return null; |
| 271 | } |
| 272 | |
| 273 | if (cellData != null) { |
| 274 | return new DefaultFlowEntry(translatedEntity.get().original(), |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 275 | ADDED, timedEntry.lifeSec(), cellData.packets(), |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 276 | cellData.bytes()); |
| 277 | } else { |
| 278 | return new DefaultFlowEntry(translatedEntity.get().original(), |
| 279 | ADDED, timedEntry.lifeSec(), 0, 0); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | private Collection<FlowEntry> getFlowEntriesFromMirror() { |
| 284 | return tableMirror.getAll(deviceId).stream() |
| 285 | .map(timedEntry -> forgeFlowEntry( |
Carmelo Cascone | c32976e | 2019-04-08 14:50:52 -0700 | [diff] [blame] | 286 | timedEntry.entry(), timedEntry.entry().handle(deviceId), null)) |
Carmelo Cascone | 2660097 | 2018-09-10 00:23:20 -0700 | [diff] [blame] | 287 | .filter(Objects::nonNull) |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 288 | .collect(Collectors.toList()); |
| 289 | } |
| 290 | |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 291 | private Collection<FlowRule> processFlowRules(Collection<FlowRule> rules, |
| 292 | Operation driverOperation) { |
Carmelo Cascone | c32976e | 2019-04-08 14:50:52 -0700 | [diff] [blame] | 293 | if (!setupBehaviour("processFlowRules()") || rules.isEmpty()) { |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 294 | return Collections.emptyList(); |
| 295 | } |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 296 | // Created batched write request. |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 297 | final WriteRequest request = client.write(p4DeviceId, pipeconf); |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 298 | // For each rule, translate to PI and append to write request. |
| 299 | final Map<PiHandle, FlowRule> handleToRuleMap = Maps.newHashMap(); |
| 300 | final List<FlowRule> skippedRules = Lists.newArrayList(); |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame] | 301 | final CompletableFuture<WriteResponse> futureResponse; |
| 302 | WRITE_LOCKS.get(deviceId).lock(); |
| 303 | try { |
| 304 | for (FlowRule rule : rules) { |
Carmelo Cascone | b4863b3 | 2019-03-13 18:54:34 -0700 | [diff] [blame] | 305 | // Translate. |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame] | 306 | final PiTableEntry entry; |
| 307 | try { |
| 308 | entry = translator.translate(rule, pipeconf); |
| 309 | } catch (PiTranslationException e) { |
| 310 | log.warn("Unable to translate flow rule for pipeconf '{}': {} [{}]", |
| 311 | pipeconf.id(), e.getMessage(), rule); |
| 312 | // Next rule. |
| 313 | continue; |
| 314 | } |
| 315 | final PiTableEntryHandle handle = entry.handle(deviceId); |
| 316 | handleToRuleMap.put(handle, rule); |
Carmelo Cascone | b4863b3 | 2019-03-13 18:54:34 -0700 | [diff] [blame] | 317 | // Update translation store. |
| 318 | if (driverOperation.equals(APPLY)) { |
| 319 | translator.learn(handle, new PiTranslatedEntity<>( |
| 320 | rule, entry, handle)); |
| 321 | } else { |
| 322 | translator.forget(handle); |
| 323 | } |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame] | 324 | // Append entry to batched write request (returns false), or skip (true) |
| 325 | if (appendEntryToWriteRequestOrSkip( |
| 326 | request, handle, entry, driverOperation)) { |
| 327 | skippedRules.add(rule); |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame] | 328 | } |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 329 | } |
Carmelo Cascone | b4863b3 | 2019-03-13 18:54:34 -0700 | [diff] [blame] | 330 | if (request.pendingUpdates().isEmpty()) { |
| 331 | // All good. No need to write on device. |
| 332 | return rules; |
| 333 | } |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame] | 334 | // Update mirror. |
| 335 | tableMirror.applyWriteRequest(request); |
| 336 | // Async submit request to server. |
| 337 | futureResponse = request.submit(); |
| 338 | } finally { |
| 339 | WRITE_LOCKS.get(deviceId).unlock(); |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 340 | } |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame] | 341 | // Wait for response. |
| 342 | final WriteResponse response = Futures.getUnchecked(futureResponse); |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 343 | // Derive successfully applied flow rule from response. |
Carmelo Cascone | b4863b3 | 2019-03-13 18:54:34 -0700 | [diff] [blame] | 344 | final List<FlowRule> appliedRules = getAppliedFlowRules( |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 345 | response, handleToRuleMap, driverOperation); |
| 346 | // Return skipped and applied rules. |
| 347 | return ImmutableList.<FlowRule>builder() |
| 348 | .addAll(skippedRules).addAll(appliedRules).build(); |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 349 | } |
| 350 | |
Carmelo Cascone | b4863b3 | 2019-03-13 18:54:34 -0700 | [diff] [blame] | 351 | private List<FlowRule> getAppliedFlowRules( |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame] | 352 | WriteResponse response, |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 353 | Map<PiHandle, FlowRule> handleToFlowRuleMap, |
| 354 | Operation driverOperation) { |
| 355 | // Returns a list of flow rules that were successfully written on the |
| 356 | // server according to the given write response and operation. |
| 357 | return response.success().stream() |
| 358 | .filter(r -> r.entityType().equals(PiEntityType.TABLE_ENTRY)) |
Carmelo Cascone | b4863b3 | 2019-03-13 18:54:34 -0700 | [diff] [blame] | 359 | .filter(r -> { |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 360 | // Filter intermediate responses (e.g. P4Runtime DELETE |
| 361 | // during FlowRule APPLY because we are performing |
| 362 | // delete-before-update) |
Carmelo Cascone | b4863b3 | 2019-03-13 18:54:34 -0700 | [diff] [blame] | 363 | return isUpdateTypeRelevant(r.updateType(), driverOperation); |
| 364 | }) |
| 365 | .map(r -> { |
| 366 | final FlowRule rule = handleToFlowRuleMap.get(r.handle()); |
| 367 | if (rule == null) { |
| 368 | log.warn("Server returned unrecognized table entry " + |
| 369 | "handle in write response: {}", r.handle()); |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 370 | } |
Carmelo Cascone | b4863b3 | 2019-03-13 18:54:34 -0700 | [diff] [blame] | 371 | return rule; |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 372 | }) |
| 373 | .filter(Objects::nonNull) |
| 374 | .collect(Collectors.toList()); |
| 375 | } |
| 376 | |
| 377 | private boolean isUpdateTypeRelevant(UpdateType p4UpdateType, Operation driverOperation) { |
| 378 | switch (p4UpdateType) { |
| 379 | case INSERT: |
| 380 | case MODIFY: |
| 381 | if (!driverOperation.equals(APPLY)) { |
| 382 | return false; |
| 383 | } |
| 384 | break; |
| 385 | case DELETE: |
| 386 | if (!driverOperation.equals(REMOVE)) { |
| 387 | return false; |
| 388 | } |
| 389 | break; |
| 390 | default: |
| 391 | log.error("Unknown update type {}", p4UpdateType); |
| 392 | return false; |
| 393 | } |
| 394 | return true; |
| 395 | } |
| 396 | |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 397 | private boolean appendEntryToWriteRequestOrSkip( |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame] | 398 | final WriteRequest writeRequest, |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 399 | final PiTableEntryHandle handle, |
| 400 | PiTableEntry piEntryToApply, |
| 401 | final Operation driverOperation) { |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 402 | // Depending on the driver operation, and if a matching rule exists on |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 403 | // the device/mirror, decide which P4Runtime update operation to perform |
| 404 | // for this entry. In some cases, the entry is skipped from the write |
| 405 | // request but we want to return the corresponding flow rule as |
| 406 | // successfully written. In this case, we return true. |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 407 | final TimedEntry<PiTableEntry> piEntryOnDevice = tableMirror.get(handle); |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 408 | final UpdateType updateType; |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 409 | |
Carmelo Cascone | 1ae2506 | 2019-03-07 16:02:22 -0800 | [diff] [blame] | 410 | final boolean supportDefaultEntry = driverBoolProperty( |
CyberHasH | 39b808b | 2019-10-11 05:59:12 +0800 | [diff] [blame] | 411 | SUPPORT_DEFAULT_TABLE_ENTRY, |
| 412 | DEFAULT_SUPPORT_DEFAULT_TABLE_ENTRY); |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 413 | final boolean deleteBeforeUpdate = driverBoolProperty( |
CyberHasH | 39b808b | 2019-10-11 05:59:12 +0800 | [diff] [blame] | 414 | DELETE_BEFORE_UPDATE, |
| 415 | DEFAULT_DELETE_BEFORE_UPDATE); |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 416 | |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 417 | if (driverOperation == APPLY) { |
| 418 | if (piEntryOnDevice == null) { |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 419 | // Entry is first-timer, INSERT or MODIFY if default action. |
Carmelo Cascone | 1ae2506 | 2019-03-07 16:02:22 -0800 | [diff] [blame] | 420 | updateType = !piEntryToApply.isDefaultAction() || !supportDefaultEntry |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 421 | ? INSERT : MODIFY; |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 422 | } else { |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 423 | if (piEntryToApply.action().equals(piEntryOnDevice.entry().action())) { |
| 424 | // FIXME: should we check for other attributes of the table |
| 425 | // entry? For example can we modify the priority? |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 426 | log.debug("Ignoring re-apply of existing entry: {}", piEntryToApply); |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 427 | return true; |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 428 | } else if (deleteBeforeUpdate && !piEntryToApply.isDefaultAction()) { |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 429 | // Some devices return error when updating existing entries. |
| 430 | // If requested, remove entry before re-inserting the |
| 431 | // modified one, except the default action entry, that |
| 432 | // cannot be removed. |
| 433 | writeRequest.delete(handle); |
| 434 | updateType = INSERT; |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 435 | } else { |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 436 | updateType = MODIFY; |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 437 | } |
| 438 | } |
| 439 | } else { |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 440 | // REMOVE. |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 441 | if (piEntryToApply.isDefaultAction()) { |
CyberHasH | 39b808b | 2019-10-11 05:59:12 +0800 | [diff] [blame] | 442 | // Cannot remove default action. Instead we should modify it to |
| 443 | // use the original one as specified in the P4 program. |
Carmelo Cascone | af877e9 | 2019-04-28 13:44:01 -0700 | [diff] [blame] | 444 | final PiTableEntry originalDefaultEntry = getOriginalDefaultEntry( |
| 445 | piEntryToApply.table()); |
| 446 | if (originalDefaultEntry == null) { |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 447 | return false; |
| 448 | } |
Carmelo Cascone | af877e9 | 2019-04-28 13:44:01 -0700 | [diff] [blame] | 449 | return appendEntryToWriteRequestOrSkip( |
| 450 | writeRequest, originalDefaultEntry.handle(deviceId), |
| 451 | originalDefaultEntry, APPLY); |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 452 | } else { |
Carmelo Cascone | af877e9 | 2019-04-28 13:44:01 -0700 | [diff] [blame] | 453 | if (piEntryOnDevice == null) { |
| 454 | log.debug("Ignoring delete of missing entry: {}", |
| 455 | piEntryToApply); |
| 456 | return true; |
| 457 | } |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 458 | updateType = DELETE; |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 459 | } |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 460 | } |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 461 | writeRequest.entity(piEntryToApply, updateType); |
| 462 | return false; |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 463 | } |
| 464 | |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 465 | private PiTableEntry getOriginalDefaultEntry(PiTableId tableId) { |
CyberHasH | 39b808b | 2019-10-11 05:59:12 +0800 | [diff] [blame] | 466 | final PiTableEntryHandle handle = PiTableEntry.builder() |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 467 | .forTable(tableId) |
CyberHasH | 39b808b | 2019-10-11 05:59:12 +0800 | [diff] [blame] | 468 | .withMatchKey(PiMatchKey.EMPTY) |
| 469 | .build() |
| 470 | .handle(deviceId); |
| 471 | final TimedEntry<PiTableEntry> originalDefaultEntry = defaultEntryMirror.get(handle); |
| 472 | if (originalDefaultEntry != null) { |
| 473 | return originalDefaultEntry.entry(); |
| 474 | } |
| 475 | return null; |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | private boolean isOriginalDefaultEntry(PiTableEntry entry) { |
| 479 | if (!entry.isDefaultAction()) { |
| 480 | return false; |
| 481 | } |
| 482 | final PiTableEntry originalDefaultEntry = getOriginalDefaultEntry(entry.table()); |
CyberHasH | 39b808b | 2019-10-11 05:59:12 +0800 | [diff] [blame] | 483 | if (originalDefaultEntry == null) { |
| 484 | return false; |
| 485 | } |
| 486 | // Sometimes the default action may be null |
| 487 | // e.g. In basic pipeline, the default action in wcmp_table is null |
| 488 | if (originalDefaultEntry.action() == null) { |
| 489 | return entry.action() == null; |
| 490 | } |
| 491 | return originalDefaultEntry.action().equals(entry.action()); |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 492 | } |
| 493 | |
Carmelo Cascone | c32976e | 2019-04-08 14:50:52 -0700 | [diff] [blame] | 494 | private Map<PiTableEntryHandle, PiCounterCellData> readEntryCounters( |
Carmelo Cascone | 255125d | 2018-04-11 14:03:22 -0700 | [diff] [blame] | 495 | Collection<PiTableEntry> tableEntries) { |
Carmelo Cascone | c32976e | 2019-04-08 14:50:52 -0700 | [diff] [blame] | 496 | |
Carmelo Cascone | 255125d | 2018-04-11 14:03:22 -0700 | [diff] [blame] | 497 | if (!driverBoolProperty(SUPPORT_TABLE_COUNTERS, |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 498 | DEFAULT_SUPPORT_TABLE_COUNTERS) |
| 499 | || tableEntries.isEmpty()) { |
Carmelo Cascone | 255125d | 2018-04-11 14:03:22 -0700 | [diff] [blame] | 500 | return Collections.emptyMap(); |
| 501 | } |
| 502 | |
Carmelo Cascone | c32976e | 2019-04-08 14:50:52 -0700 | [diff] [blame] | 503 | if (driverBoolProperty(READ_COUNTERS_WITH_TABLE_ENTRIES, |
| 504 | DEFAULT_READ_COUNTERS_WITH_TABLE_ENTRIES)) { |
| 505 | return tableEntries.stream() |
| 506 | .filter(t -> t.counter() != null) |
| 507 | .collect(Collectors.toMap( |
| 508 | t -> t.handle(deviceId), PiTableEntry::counter)); |
| 509 | } else { |
| 510 | final Set<PiHandle> cellHandles = tableEntries.stream() |
| 511 | .filter(e -> !e.isDefaultAction()) |
| 512 | .filter(e -> tableHasCounter(e.table())) |
| 513 | .map(PiCounterCellId::ofDirect) |
| 514 | .map(id -> PiCounterCellHandle.of(deviceId, id)) |
| 515 | .collect(Collectors.toSet()); |
| 516 | // FIXME: We might be sending a very large read request... |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 517 | return client.read(p4DeviceId, pipeconf) |
Carmelo Cascone | c32976e | 2019-04-08 14:50:52 -0700 | [diff] [blame] | 518 | .handles(cellHandles) |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 519 | .submitSync() |
| 520 | .all(PiCounterCell.class).stream() |
| 521 | .filter(c -> c.cellId().counterType().equals(PiCounterType.DIRECT)) |
Carmelo Cascone | c32976e | 2019-04-08 14:50:52 -0700 | [diff] [blame] | 522 | .collect(Collectors.toMap( |
| 523 | c -> c.cellId().tableEntry().handle(deviceId), |
| 524 | PiCounterCell::data)); |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 525 | } |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 526 | } |
| 527 | |
Carmelo Cascone | 255125d | 2018-04-11 14:03:22 -0700 | [diff] [blame] | 528 | private boolean tableHasCounter(PiTableId tableId) { |
| 529 | return pipelineModel.table(tableId).isPresent() && |
| 530 | !pipelineModel.table(tableId).get().counters().isEmpty(); |
| 531 | } |
| 532 | |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 533 | enum Operation { |
| 534 | APPLY, REMOVE |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 535 | } |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 536 | } |