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