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