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, |
| 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 |
| 148 | // device mirror. Inconsistent. Mark for removal. |
| 149 | // TODO: make this behaviour configurable |
| 150 | // In some cases it's fine for the device to have rules |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 151 | // that were not installed by us, e.g. original default entry. |
| 152 | if (!isOriginalDefaultEntry(entry)) { |
| 153 | inconsistentEntries.add(entry); |
| 154 | } |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 155 | } else { |
| 156 | result.add(flowEntry); |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 160 | if (!inconsistentEntries.isEmpty()) { |
Carmelo Cascone | 33b27bc | 2018-09-09 22:56:14 -0700 | [diff] [blame] | 161 | // Trigger clean up of inconsistent entries. |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame] | 162 | log.warn("Found {} inconsistent table entries on {}, removing them...", |
| 163 | inconsistentEntries.size(), deviceId); |
Carmelo Cascone | b4863b3 | 2019-03-13 18:54:34 -0700 | [diff] [blame] | 164 | // Submit delete request and update mirror when done. |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 165 | client.write(p4DeviceId, pipeconf) |
Carmelo Cascone | b4863b3 | 2019-03-13 18:54:34 -0700 | [diff] [blame] | 166 | .entities(inconsistentEntries, DELETE) |
| 167 | .submit().whenComplete((response, ex) -> { |
| 168 | if (ex != null) { |
| 169 | log.error("Exception removing inconsistent table entries", ex); |
| 170 | } else { |
| 171 | log.debug("Successfully removed {} out of {} inconsistent entries", |
| 172 | response.success().size(), response.all().size()); |
| 173 | } |
| 174 | tableMirror.applyWriteResponse(response); |
| 175 | }); |
| 176 | |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 177 | } |
| 178 | |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 179 | return result.build(); |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 180 | } |
| 181 | |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 182 | private Collection<PiTableEntry> getAllTableEntriesFromDevice() { |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 183 | final P4RuntimeReadClient.ReadRequest request = client.read( |
| 184 | p4DeviceId, pipeconf); |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 185 | // Read entries from all non-constant tables, including default ones. |
| 186 | pipelineModel.tables().stream() |
| 187 | .filter(t -> !t.isConstantTable()) |
| 188 | .forEach(t -> { |
| 189 | request.tableEntries(t.id()); |
Carmelo Cascone | 1ae2506 | 2019-03-07 16:02:22 -0800 | [diff] [blame] | 190 | if (driverBoolProperty(SUPPORT_DEFAULT_TABLE_ENTRY, |
| 191 | DEFAULT_SUPPORT_DEFAULT_TABLE_ENTRY) && |
| 192 | !t.constDefaultAction().isPresent()) { |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 193 | request.defaultTableEntry(t.id()); |
| 194 | } |
| 195 | }); |
| 196 | final P4RuntimeReadClient.ReadResponse response = request.submitSync(); |
| 197 | if (!response.isSuccess()) { |
| 198 | return null; |
| 199 | } |
| 200 | return response.all(PiTableEntry.class).stream() |
| 201 | // Device implementation might return duplicate entries. For |
| 202 | // example if reading only default ones is not supported and |
| 203 | // non-default entries are returned, by using distinct() we |
| 204 | // are robust against that possibility. |
| 205 | .distinct() |
| 206 | .collect(Collectors.toList()); |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 209 | @Override |
| 210 | public Collection<FlowRule> applyFlowRules(Collection<FlowRule> rules) { |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 211 | return processFlowRules(rules, APPLY); |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | @Override |
| 215 | public Collection<FlowRule> removeFlowRules(Collection<FlowRule> rules) { |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 216 | return processFlowRules(rules, REMOVE); |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 217 | } |
| 218 | |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 219 | private FlowEntry forgeFlowEntry(PiTableEntry entry, |
Carmelo Cascone | c32976e | 2019-04-08 14:50:52 -0700 | [diff] [blame] | 220 | PiTableEntryHandle handle, |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 221 | PiCounterCellData cellData) { |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 222 | final Optional<PiTranslatedEntity<FlowRule, PiTableEntry>> |
| 223 | translatedEntity = translator.lookup(handle); |
| 224 | final TimedEntry<PiTableEntry> timedEntry = tableMirror.get(handle); |
| 225 | |
CyberHasH | 39b808b | 2019-10-11 05:59:12 +0800 | [diff] [blame^] | 226 | // A default entry might not be present in the translation store if it |
| 227 | // was not inserted by an app. No need to log. |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 228 | if (!translatedEntity.isPresent()) { |
CyberHasH | 39b808b | 2019-10-11 05:59:12 +0800 | [diff] [blame^] | 229 | if (!isOriginalDefaultEntry(entry)) { |
| 230 | log.warn("Table entry handle not found in translation store: {}", handle); |
| 231 | } |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 232 | return null; |
| 233 | } |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 234 | if (!translatedEntity.get().translated().equals(entry)) { |
| 235 | log.warn("Table entry obtained from device {} is different from " + |
| 236 | "one in in translation store: device={}, store={}", |
| 237 | deviceId, entry, translatedEntity.get().translated()); |
| 238 | return null; |
| 239 | } |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 240 | if (timedEntry == null) { |
Carmelo Cascone | 2660097 | 2018-09-10 00:23:20 -0700 | [diff] [blame] | 241 | log.warn("Table entry handle not found in device mirror: {}", handle); |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 242 | return null; |
| 243 | } |
| 244 | |
| 245 | if (cellData != null) { |
| 246 | return new DefaultFlowEntry(translatedEntity.get().original(), |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 247 | ADDED, timedEntry.lifeSec(), cellData.packets(), |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 248 | cellData.bytes()); |
| 249 | } else { |
| 250 | return new DefaultFlowEntry(translatedEntity.get().original(), |
| 251 | ADDED, timedEntry.lifeSec(), 0, 0); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | private Collection<FlowEntry> getFlowEntriesFromMirror() { |
| 256 | return tableMirror.getAll(deviceId).stream() |
| 257 | .map(timedEntry -> forgeFlowEntry( |
Carmelo Cascone | c32976e | 2019-04-08 14:50:52 -0700 | [diff] [blame] | 258 | timedEntry.entry(), timedEntry.entry().handle(deviceId), null)) |
Carmelo Cascone | 2660097 | 2018-09-10 00:23:20 -0700 | [diff] [blame] | 259 | .filter(Objects::nonNull) |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 260 | .collect(Collectors.toList()); |
| 261 | } |
| 262 | |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 263 | private Collection<FlowRule> processFlowRules(Collection<FlowRule> rules, |
| 264 | Operation driverOperation) { |
Carmelo Cascone | c32976e | 2019-04-08 14:50:52 -0700 | [diff] [blame] | 265 | if (!setupBehaviour("processFlowRules()") || rules.isEmpty()) { |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 266 | return Collections.emptyList(); |
| 267 | } |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 268 | // Created batched write request. |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 269 | final WriteRequest request = client.write(p4DeviceId, pipeconf); |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 270 | // For each rule, translate to PI and append to write request. |
| 271 | final Map<PiHandle, FlowRule> handleToRuleMap = Maps.newHashMap(); |
| 272 | final List<FlowRule> skippedRules = Lists.newArrayList(); |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame] | 273 | final CompletableFuture<WriteResponse> futureResponse; |
| 274 | WRITE_LOCKS.get(deviceId).lock(); |
| 275 | try { |
| 276 | for (FlowRule rule : rules) { |
Carmelo Cascone | b4863b3 | 2019-03-13 18:54:34 -0700 | [diff] [blame] | 277 | // Translate. |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame] | 278 | final PiTableEntry entry; |
| 279 | try { |
| 280 | entry = translator.translate(rule, pipeconf); |
| 281 | } catch (PiTranslationException e) { |
| 282 | log.warn("Unable to translate flow rule for pipeconf '{}': {} [{}]", |
| 283 | pipeconf.id(), e.getMessage(), rule); |
| 284 | // Next rule. |
| 285 | continue; |
| 286 | } |
| 287 | final PiTableEntryHandle handle = entry.handle(deviceId); |
| 288 | handleToRuleMap.put(handle, rule); |
Carmelo Cascone | b4863b3 | 2019-03-13 18:54:34 -0700 | [diff] [blame] | 289 | // Update translation store. |
| 290 | if (driverOperation.equals(APPLY)) { |
| 291 | translator.learn(handle, new PiTranslatedEntity<>( |
| 292 | rule, entry, handle)); |
| 293 | } else { |
| 294 | translator.forget(handle); |
| 295 | } |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame] | 296 | // Append entry to batched write request (returns false), or skip (true) |
| 297 | if (appendEntryToWriteRequestOrSkip( |
| 298 | request, handle, entry, driverOperation)) { |
| 299 | skippedRules.add(rule); |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame] | 300 | } |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 301 | } |
Carmelo Cascone | b4863b3 | 2019-03-13 18:54:34 -0700 | [diff] [blame] | 302 | if (request.pendingUpdates().isEmpty()) { |
| 303 | // All good. No need to write on device. |
| 304 | return rules; |
| 305 | } |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame] | 306 | // Update mirror. |
| 307 | tableMirror.applyWriteRequest(request); |
| 308 | // Async submit request to server. |
| 309 | futureResponse = request.submit(); |
| 310 | } finally { |
| 311 | WRITE_LOCKS.get(deviceId).unlock(); |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 312 | } |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame] | 313 | // Wait for response. |
| 314 | final WriteResponse response = Futures.getUnchecked(futureResponse); |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 315 | // Derive successfully applied flow rule from response. |
Carmelo Cascone | b4863b3 | 2019-03-13 18:54:34 -0700 | [diff] [blame] | 316 | final List<FlowRule> appliedRules = getAppliedFlowRules( |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 317 | response, handleToRuleMap, driverOperation); |
| 318 | // Return skipped and applied rules. |
| 319 | return ImmutableList.<FlowRule>builder() |
| 320 | .addAll(skippedRules).addAll(appliedRules).build(); |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 321 | } |
| 322 | |
Carmelo Cascone | b4863b3 | 2019-03-13 18:54:34 -0700 | [diff] [blame] | 323 | private List<FlowRule> getAppliedFlowRules( |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame] | 324 | WriteResponse response, |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 325 | Map<PiHandle, FlowRule> handleToFlowRuleMap, |
| 326 | Operation driverOperation) { |
| 327 | // Returns a list of flow rules that were successfully written on the |
| 328 | // server according to the given write response and operation. |
| 329 | return response.success().stream() |
| 330 | .filter(r -> r.entityType().equals(PiEntityType.TABLE_ENTRY)) |
Carmelo Cascone | b4863b3 | 2019-03-13 18:54:34 -0700 | [diff] [blame] | 331 | .filter(r -> { |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 332 | // Filter intermediate responses (e.g. P4Runtime DELETE |
| 333 | // during FlowRule APPLY because we are performing |
| 334 | // delete-before-update) |
Carmelo Cascone | b4863b3 | 2019-03-13 18:54:34 -0700 | [diff] [blame] | 335 | return isUpdateTypeRelevant(r.updateType(), driverOperation); |
| 336 | }) |
| 337 | .map(r -> { |
| 338 | final FlowRule rule = handleToFlowRuleMap.get(r.handle()); |
| 339 | if (rule == null) { |
| 340 | log.warn("Server returned unrecognized table entry " + |
| 341 | "handle in write response: {}", r.handle()); |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 342 | } |
Carmelo Cascone | b4863b3 | 2019-03-13 18:54:34 -0700 | [diff] [blame] | 343 | return rule; |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 344 | }) |
| 345 | .filter(Objects::nonNull) |
| 346 | .collect(Collectors.toList()); |
| 347 | } |
| 348 | |
| 349 | private boolean isUpdateTypeRelevant(UpdateType p4UpdateType, Operation driverOperation) { |
| 350 | switch (p4UpdateType) { |
| 351 | case INSERT: |
| 352 | case MODIFY: |
| 353 | if (!driverOperation.equals(APPLY)) { |
| 354 | return false; |
| 355 | } |
| 356 | break; |
| 357 | case DELETE: |
| 358 | if (!driverOperation.equals(REMOVE)) { |
| 359 | return false; |
| 360 | } |
| 361 | break; |
| 362 | default: |
| 363 | log.error("Unknown update type {}", p4UpdateType); |
| 364 | return false; |
| 365 | } |
| 366 | return true; |
| 367 | } |
| 368 | |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 369 | private boolean appendEntryToWriteRequestOrSkip( |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame] | 370 | final WriteRequest writeRequest, |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 371 | final PiTableEntryHandle handle, |
| 372 | PiTableEntry piEntryToApply, |
| 373 | final Operation driverOperation) { |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 374 | // Depending on the driver operation, and if a matching rule exists on |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 375 | // the device/mirror, decide which P4Runtime update operation to perform |
| 376 | // for this entry. In some cases, the entry is skipped from the write |
| 377 | // request but we want to return the corresponding flow rule as |
| 378 | // successfully written. In this case, we return true. |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 379 | final TimedEntry<PiTableEntry> piEntryOnDevice = tableMirror.get(handle); |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 380 | final UpdateType updateType; |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 381 | |
Carmelo Cascone | 1ae2506 | 2019-03-07 16:02:22 -0800 | [diff] [blame] | 382 | final boolean supportDefaultEntry = driverBoolProperty( |
CyberHasH | 39b808b | 2019-10-11 05:59:12 +0800 | [diff] [blame^] | 383 | SUPPORT_DEFAULT_TABLE_ENTRY, |
| 384 | DEFAULT_SUPPORT_DEFAULT_TABLE_ENTRY); |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 385 | final boolean deleteBeforeUpdate = driverBoolProperty( |
CyberHasH | 39b808b | 2019-10-11 05:59:12 +0800 | [diff] [blame^] | 386 | DELETE_BEFORE_UPDATE, |
| 387 | DEFAULT_DELETE_BEFORE_UPDATE); |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 388 | |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 389 | if (driverOperation == APPLY) { |
| 390 | if (piEntryOnDevice == null) { |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 391 | // Entry is first-timer, INSERT or MODIFY if default action. |
Carmelo Cascone | 1ae2506 | 2019-03-07 16:02:22 -0800 | [diff] [blame] | 392 | updateType = !piEntryToApply.isDefaultAction() || !supportDefaultEntry |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 393 | ? INSERT : MODIFY; |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 394 | } else { |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 395 | if (piEntryToApply.action().equals(piEntryOnDevice.entry().action())) { |
| 396 | // FIXME: should we check for other attributes of the table |
| 397 | // entry? For example can we modify the priority? |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 398 | log.debug("Ignoring re-apply of existing entry: {}", piEntryToApply); |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 399 | return true; |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 400 | } else if (deleteBeforeUpdate && !piEntryToApply.isDefaultAction()) { |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 401 | // Some devices return error when updating existing entries. |
| 402 | // If requested, remove entry before re-inserting the |
| 403 | // modified one, except the default action entry, that |
| 404 | // cannot be removed. |
| 405 | writeRequest.delete(handle); |
| 406 | updateType = INSERT; |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 407 | } else { |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 408 | updateType = MODIFY; |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 409 | } |
| 410 | } |
| 411 | } else { |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 412 | // REMOVE. |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 413 | if (piEntryToApply.isDefaultAction()) { |
CyberHasH | 39b808b | 2019-10-11 05:59:12 +0800 | [diff] [blame^] | 414 | // Cannot remove default action. Instead we should modify it to |
| 415 | // use the original one as specified in the P4 program. |
Carmelo Cascone | af877e9 | 2019-04-28 13:44:01 -0700 | [diff] [blame] | 416 | final PiTableEntry originalDefaultEntry = getOriginalDefaultEntry( |
| 417 | piEntryToApply.table()); |
| 418 | if (originalDefaultEntry == null) { |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 419 | return false; |
| 420 | } |
Carmelo Cascone | af877e9 | 2019-04-28 13:44:01 -0700 | [diff] [blame] | 421 | return appendEntryToWriteRequestOrSkip( |
| 422 | writeRequest, originalDefaultEntry.handle(deviceId), |
| 423 | originalDefaultEntry, APPLY); |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 424 | } else { |
Carmelo Cascone | af877e9 | 2019-04-28 13:44:01 -0700 | [diff] [blame] | 425 | if (piEntryOnDevice == null) { |
| 426 | log.debug("Ignoring delete of missing entry: {}", |
| 427 | piEntryToApply); |
| 428 | return true; |
| 429 | } |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 430 | updateType = DELETE; |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 431 | } |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 432 | } |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 433 | writeRequest.entity(piEntryToApply, updateType); |
| 434 | return false; |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 435 | } |
| 436 | |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 437 | private PiTableEntry getOriginalDefaultEntry(PiTableId tableId) { |
CyberHasH | 39b808b | 2019-10-11 05:59:12 +0800 | [diff] [blame^] | 438 | final PiTableEntryHandle handle = PiTableEntry.builder() |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 439 | .forTable(tableId) |
CyberHasH | 39b808b | 2019-10-11 05:59:12 +0800 | [diff] [blame^] | 440 | .withMatchKey(PiMatchKey.EMPTY) |
| 441 | .build() |
| 442 | .handle(deviceId); |
| 443 | final TimedEntry<PiTableEntry> originalDefaultEntry = defaultEntryMirror.get(handle); |
| 444 | if (originalDefaultEntry != null) { |
| 445 | return originalDefaultEntry.entry(); |
| 446 | } |
| 447 | return null; |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | private boolean isOriginalDefaultEntry(PiTableEntry entry) { |
| 451 | if (!entry.isDefaultAction()) { |
| 452 | return false; |
| 453 | } |
| 454 | final PiTableEntry originalDefaultEntry = getOriginalDefaultEntry(entry.table()); |
CyberHasH | 39b808b | 2019-10-11 05:59:12 +0800 | [diff] [blame^] | 455 | if (originalDefaultEntry == null) { |
| 456 | return false; |
| 457 | } |
| 458 | // Sometimes the default action may be null |
| 459 | // e.g. In basic pipeline, the default action in wcmp_table is null |
| 460 | if (originalDefaultEntry.action() == null) { |
| 461 | return entry.action() == null; |
| 462 | } |
| 463 | return originalDefaultEntry.action().equals(entry.action()); |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 464 | } |
| 465 | |
Carmelo Cascone | c32976e | 2019-04-08 14:50:52 -0700 | [diff] [blame] | 466 | private Map<PiTableEntryHandle, PiCounterCellData> readEntryCounters( |
Carmelo Cascone | 255125d | 2018-04-11 14:03:22 -0700 | [diff] [blame] | 467 | Collection<PiTableEntry> tableEntries) { |
Carmelo Cascone | c32976e | 2019-04-08 14:50:52 -0700 | [diff] [blame] | 468 | |
Carmelo Cascone | 255125d | 2018-04-11 14:03:22 -0700 | [diff] [blame] | 469 | if (!driverBoolProperty(SUPPORT_TABLE_COUNTERS, |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 470 | DEFAULT_SUPPORT_TABLE_COUNTERS) |
| 471 | || tableEntries.isEmpty()) { |
Carmelo Cascone | 255125d | 2018-04-11 14:03:22 -0700 | [diff] [blame] | 472 | return Collections.emptyMap(); |
| 473 | } |
| 474 | |
Carmelo Cascone | c32976e | 2019-04-08 14:50:52 -0700 | [diff] [blame] | 475 | if (driverBoolProperty(READ_COUNTERS_WITH_TABLE_ENTRIES, |
| 476 | DEFAULT_READ_COUNTERS_WITH_TABLE_ENTRIES)) { |
| 477 | return tableEntries.stream() |
| 478 | .filter(t -> t.counter() != null) |
| 479 | .collect(Collectors.toMap( |
| 480 | t -> t.handle(deviceId), PiTableEntry::counter)); |
| 481 | } else { |
| 482 | final Set<PiHandle> cellHandles = tableEntries.stream() |
| 483 | .filter(e -> !e.isDefaultAction()) |
| 484 | .filter(e -> tableHasCounter(e.table())) |
| 485 | .map(PiCounterCellId::ofDirect) |
| 486 | .map(id -> PiCounterCellHandle.of(deviceId, id)) |
| 487 | .collect(Collectors.toSet()); |
| 488 | // FIXME: We might be sending a very large read request... |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 489 | return client.read(p4DeviceId, pipeconf) |
Carmelo Cascone | c32976e | 2019-04-08 14:50:52 -0700 | [diff] [blame] | 490 | .handles(cellHandles) |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 491 | .submitSync() |
| 492 | .all(PiCounterCell.class).stream() |
| 493 | .filter(c -> c.cellId().counterType().equals(PiCounterType.DIRECT)) |
Carmelo Cascone | c32976e | 2019-04-08 14:50:52 -0700 | [diff] [blame] | 494 | .collect(Collectors.toMap( |
| 495 | c -> c.cellId().tableEntry().handle(deviceId), |
| 496 | PiCounterCell::data)); |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 497 | } |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 498 | } |
| 499 | |
Carmelo Cascone | 255125d | 2018-04-11 14:03:22 -0700 | [diff] [blame] | 500 | private boolean tableHasCounter(PiTableId tableId) { |
| 501 | return pipelineModel.table(tableId).isPresent() && |
| 502 | !pipelineModel.table(tableId).get().counters().isEmpty(); |
| 503 | } |
| 504 | |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 505 | enum Operation { |
| 506 | APPLY, REMOVE |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 507 | } |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 508 | } |