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 | 33b27bc | 2018-09-09 22:56:14 -0700 | [diff] [blame] | 22 | import com.google.common.util.concurrent.Striped; |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 23 | import org.onlab.util.SharedExecutors; |
| 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 | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 30 | import org.onosproject.net.pi.model.PiPipelineInterpreter; |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 31 | import org.onosproject.net.pi.model.PiPipelineModel; |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 32 | import org.onosproject.net.pi.model.PiTableId; |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 33 | import org.onosproject.net.pi.model.PiTableModel; |
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; |
| 36 | import org.onosproject.net.pi.runtime.PiCounterCellId; |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 37 | import org.onosproject.net.pi.runtime.PiTableEntry; |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 38 | import org.onosproject.net.pi.runtime.PiTableEntryHandle; |
| 39 | import org.onosproject.net.pi.service.PiFlowRuleTranslator; |
| 40 | import org.onosproject.net.pi.service.PiTranslatedEntity; |
Carmelo Cascone | 326ad2d | 2017-11-28 18:09:13 -0800 | [diff] [blame] | 41 | import org.onosproject.net.pi.service.PiTranslationException; |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 42 | import org.onosproject.p4runtime.api.P4RuntimeClient.WriteOperationType; |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 43 | |
| 44 | import java.util.Collection; |
| 45 | import java.util.Collections; |
Manjunath Vanaraj | 59ad657 | 2017-12-26 11:10:57 +0530 | [diff] [blame] | 46 | import java.util.List; |
Carmelo Cascone | 3da671a | 2018-02-12 10:43:35 -0800 | [diff] [blame] | 47 | import java.util.Map; |
Carmelo Cascone | 2660097 | 2018-09-10 00:23:20 -0700 | [diff] [blame] | 48 | import java.util.Objects; |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 49 | import java.util.Optional; |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 50 | import java.util.Set; |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 51 | import java.util.concurrent.CompletableFuture; |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 52 | import java.util.concurrent.locks.Lock; |
Carmelo Cascone | fe99be9 | 2017-09-11 21:55:54 +0200 | [diff] [blame] | 53 | import java.util.stream.Collectors; |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 54 | import java.util.stream.Stream; |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 55 | |
| 56 | import static com.google.common.collect.Lists.newArrayList; |
Andrea Campanella | 0288c87 | 2017-08-07 18:32:51 +0200 | [diff] [blame] | 57 | import static org.onosproject.drivers.p4runtime.P4RuntimeFlowRuleProgrammable.Operation.APPLY; |
| 58 | import static org.onosproject.drivers.p4runtime.P4RuntimeFlowRuleProgrammable.Operation.REMOVE; |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 59 | import static org.onosproject.net.flow.FlowEntry.FlowEntryState.ADDED; |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 60 | import static org.onosproject.p4runtime.api.P4RuntimeClient.WriteOperationType.DELETE; |
| 61 | import static org.onosproject.p4runtime.api.P4RuntimeClient.WriteOperationType.INSERT; |
| 62 | import static org.onosproject.p4runtime.api.P4RuntimeClient.WriteOperationType.MODIFY; |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 63 | |
| 64 | /** |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 65 | * Implementation of the flow rule programmable behaviour for P4Runtime. |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 66 | */ |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 67 | public class P4RuntimeFlowRuleProgrammable |
| 68 | extends AbstractP4RuntimeHandlerBehaviour |
| 69 | implements FlowRuleProgrammable { |
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 | // When updating an existing rule, if true, we issue a DELETE operation |
| 72 | // before inserting the new one, otherwise we issue a MODIFY operation. This |
| 73 | // is useful fore devices that do not support MODIFY operations for table |
| 74 | // entries. |
Carmelo Cascone | 3da671a | 2018-02-12 10:43:35 -0800 | [diff] [blame] | 75 | private static final String DELETE_BEFORE_UPDATE = "tableDeleteBeforeUpdate"; |
| 76 | private static final boolean DEFAULT_DELETE_BEFORE_UPDATE = false; |
Carmelo Cascone | 2308e52 | 2017-08-25 02:35:12 +0200 | [diff] [blame] | 77 | |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 78 | // If true, we ignore re-installing rules that already exist in the |
| 79 | // device mirror, i.e. same match key and action. |
Carmelo Cascone | 3da671a | 2018-02-12 10:43:35 -0800 | [diff] [blame] | 80 | private static final String IGNORE_SAME_ENTRY_UPDATE = "tableIgnoreSameEntryUpdate"; |
| 81 | private static final boolean DEFAULT_IGNORE_SAME_ENTRY_UPDATE = false; |
Carmelo Cascone | 2308e52 | 2017-08-25 02:35:12 +0200 | [diff] [blame] | 82 | |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 83 | // If true, we avoid querying the device and return what's already known by |
| 84 | // the ONOS store. |
Carmelo Cascone | 3da671a | 2018-02-12 10:43:35 -0800 | [diff] [blame] | 85 | private static final String READ_FROM_MIRROR = "tableReadFromMirror"; |
| 86 | private static final boolean DEFAULT_READ_FROM_MIRROR = false; |
Carmelo Cascone | fe99be9 | 2017-09-11 21:55:54 +0200 | [diff] [blame] | 87 | |
Carmelo Cascone | 255125d | 2018-04-11 14:03:22 -0700 | [diff] [blame] | 88 | // If true, we read counters when reading table entries (if table has |
| 89 | // counters). Otherwise, we don't. |
| 90 | private static final String SUPPORT_TABLE_COUNTERS = "supportTableCounters"; |
| 91 | private static final boolean DEFAULT_SUPPORT_TABLE_COUNTERS = true; |
| 92 | |
steven30801 | 7632e15 | 2018-10-20 00:51:08 +0800 | [diff] [blame^] | 93 | // If true, assumes that the device returns table entry message populated |
| 94 | // with direct counter values. If false, we issue a second P4Runtime request |
| 95 | // to read the direct counter values. |
| 96 | private static final String READ_COUNTERS_WITH_TABLE_ENTRIES = "tableReadCountersWithTableEntries"; |
| 97 | private static final boolean DEFAULT_READ_COUNTERS_WITH_TABLE_ENTRIES = true; |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 98 | |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 99 | // For default entries, P4Runtime mandates that only MODIFY messages are |
| 100 | // allowed. If true, treats default entries as normal table entries, |
| 101 | // e.g. inserting them first. |
| 102 | private static final String TABLE_DEFAULT_AS_ENTRY = "tableDefaultAsEntry"; |
| 103 | private static final boolean DEFAULT_TABLE_DEFAULT_AS_ENTRY = false; |
| 104 | |
Manjunath Vanaraj | 59ad657 | 2017-12-26 11:10:57 +0530 | [diff] [blame] | 105 | // Needed to synchronize operations over the same table entry. |
Carmelo Cascone | 33b27bc | 2018-09-09 22:56:14 -0700 | [diff] [blame] | 106 | private static final Striped<Lock> ENTRY_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); |
| 121 | translator = piTranslationService.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 | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 139 | // Read table entries, including default ones. |
| 140 | final Collection<PiTableEntry> deviceEntries = Stream.concat( |
| 141 | streamEntries(), streamDefaultEntries()) |
| 142 | // Ignore entries from constant tables. |
Carmelo Cascone | 33b27bc | 2018-09-09 22:56:14 -0700 | [diff] [blame] | 143 | .filter(e -> !tableIsConstant(e.table())) |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 144 | // Device implementation might return duplicate entries. For |
| 145 | // example if reading only default ones is not supported and |
| 146 | // non-default entries are returned, by using distinct() we are |
| 147 | // robust against that possibility. |
| 148 | .distinct() |
Carmelo Cascone | 33b27bc | 2018-09-09 22:56:14 -0700 | [diff] [blame] | 149 | .collect(Collectors.toList()); |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 150 | |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 151 | if (deviceEntries.isEmpty()) { |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 152 | return Collections.emptyList(); |
| 153 | } |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 154 | |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 155 | // Synchronize mirror with the device state. |
| 156 | syncMirror(deviceEntries); |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 157 | final Map<PiTableEntry, PiCounterCellData> counterCellMap = |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 158 | readEntryCounters(deviceEntries); |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 159 | // Forge flow entries with counter values. |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 160 | for (PiTableEntry entry : deviceEntries) { |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 161 | final FlowEntry flowEntry = forgeFlowEntry( |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 162 | entry, counterCellMap.get(entry)); |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 163 | if (flowEntry == null) { |
| 164 | // Entry is on device but unknown to translation service or |
| 165 | // device mirror. Inconsistent. Mark for removal. |
| 166 | // TODO: make this behaviour configurable |
| 167 | // In some cases it's fine for the device to have rules |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 168 | // that were not installed by us, e.g. original default entry. |
| 169 | if (!isOriginalDefaultEntry(entry)) { |
| 170 | inconsistentEntries.add(entry); |
| 171 | } |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 172 | } else { |
| 173 | result.add(flowEntry); |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | |
| 177 | if (inconsistentEntries.size() > 0) { |
Carmelo Cascone | 33b27bc | 2018-09-09 22:56:14 -0700 | [diff] [blame] | 178 | // Trigger clean up of inconsistent entries. |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 179 | SharedExecutors.getSingleThreadExecutor().execute( |
| 180 | () -> cleanUpInconsistentEntries(inconsistentEntries)); |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 181 | } |
| 182 | |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 183 | return result.build(); |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 184 | } |
| 185 | |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 186 | private Stream<PiTableEntry> streamEntries() { |
| 187 | return getFutureWithDeadline( |
| 188 | client.dumpAllTables(pipeconf), "dumping all tables", |
| 189 | Collections.emptyList()) |
| 190 | .stream(); |
| 191 | } |
| 192 | |
| 193 | private Stream<PiTableEntry> streamDefaultEntries() { |
| 194 | // Ignore tables with constant default action. |
| 195 | final Set<PiTableId> defaultTables = pipelineModel.tables() |
| 196 | .stream() |
| 197 | .filter(table -> !table.constDefaultAction().isPresent()) |
| 198 | .map(PiTableModel::id) |
| 199 | .collect(Collectors.toSet()); |
| 200 | return defaultTables.isEmpty() ? Stream.empty() |
| 201 | : getFutureWithDeadline( |
| 202 | client.dumpTables(defaultTables, true, pipeconf), |
| 203 | "dumping default table entries", |
| 204 | Collections.emptyList()) |
| 205 | .stream(); |
| 206 | } |
| 207 | |
| 208 | private void syncMirror(Collection<PiTableEntry> entries) { |
| 209 | Map<PiTableEntryHandle, PiTableEntry> handleMap = Maps.newHashMap(); |
| 210 | entries.forEach(e -> handleMap.put(PiTableEntryHandle.of(deviceId, e), e)); |
| 211 | tableMirror.sync(deviceId, handleMap); |
| 212 | } |
| 213 | |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 214 | @Override |
| 215 | public Collection<FlowRule> applyFlowRules(Collection<FlowRule> rules) { |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 216 | return processFlowRules(rules, APPLY); |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | @Override |
| 220 | public Collection<FlowRule> removeFlowRules(Collection<FlowRule> rules) { |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 221 | return processFlowRules(rules, REMOVE); |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 222 | } |
| 223 | |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 224 | private FlowEntry forgeFlowEntry(PiTableEntry entry, |
| 225 | PiCounterCellData cellData) { |
| 226 | final PiTableEntryHandle handle = PiTableEntryHandle |
| 227 | .of(deviceId, entry); |
| 228 | final Optional<PiTranslatedEntity<FlowRule, PiTableEntry>> |
| 229 | translatedEntity = translator.lookup(handle); |
| 230 | final TimedEntry<PiTableEntry> timedEntry = tableMirror.get(handle); |
| 231 | |
| 232 | if (!translatedEntity.isPresent()) { |
Carmelo Cascone | 2660097 | 2018-09-10 00:23:20 -0700 | [diff] [blame] | 233 | log.warn("Table entry handle not found in translation store: {}", handle); |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 234 | return null; |
| 235 | } |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 236 | if (!translatedEntity.get().translated().equals(entry)) { |
| 237 | log.warn("Table entry obtained from device {} is different from " + |
| 238 | "one in in translation store: device={}, store={}", |
| 239 | deviceId, entry, translatedEntity.get().translated()); |
| 240 | return null; |
| 241 | } |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 242 | if (timedEntry == null) { |
Carmelo Cascone | 2660097 | 2018-09-10 00:23:20 -0700 | [diff] [blame] | 243 | log.warn("Table entry handle not found in device mirror: {}", handle); |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 244 | return null; |
| 245 | } |
| 246 | |
| 247 | if (cellData != null) { |
| 248 | return new DefaultFlowEntry(translatedEntity.get().original(), |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 249 | ADDED, timedEntry.lifeSec(), cellData.packets(), |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 250 | cellData.bytes()); |
| 251 | } else { |
| 252 | return new DefaultFlowEntry(translatedEntity.get().original(), |
| 253 | ADDED, timedEntry.lifeSec(), 0, 0); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | private Collection<FlowEntry> getFlowEntriesFromMirror() { |
| 258 | return tableMirror.getAll(deviceId).stream() |
| 259 | .map(timedEntry -> forgeFlowEntry( |
| 260 | timedEntry.entry(), null)) |
Carmelo Cascone | 2660097 | 2018-09-10 00:23:20 -0700 | [diff] [blame] | 261 | .filter(Objects::nonNull) |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 262 | .collect(Collectors.toList()); |
| 263 | } |
| 264 | |
| 265 | private void cleanUpInconsistentEntries(Collection<PiTableEntry> piEntries) { |
Carmelo Cascone | 2660097 | 2018-09-10 00:23:20 -0700 | [diff] [blame] | 266 | log.warn("Found {} inconsistent table entries on {}, removing them...", |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 267 | piEntries.size(), deviceId); |
| 268 | piEntries.forEach(entry -> { |
| 269 | log.debug(entry.toString()); |
Carmelo Cascone | 2660097 | 2018-09-10 00:23:20 -0700 | [diff] [blame] | 270 | final PiTableEntryHandle handle = PiTableEntryHandle.of(deviceId, entry); |
| 271 | ENTRY_LOCKS.get(handle).lock(); |
| 272 | try { |
| 273 | applyEntry(handle, entry, null, REMOVE); |
| 274 | } finally { |
| 275 | ENTRY_LOCKS.get(handle).unlock(); |
| 276 | } |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 277 | }); |
| 278 | } |
| 279 | |
| 280 | private Collection<FlowRule> processFlowRules(Collection<FlowRule> rules, |
| 281 | Operation driverOperation) { |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 282 | |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 283 | if (!setupBehaviour() || rules.isEmpty()) { |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 284 | return Collections.emptyList(); |
| 285 | } |
| 286 | |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 287 | final ImmutableList.Builder<FlowRule> result = ImmutableList.builder(); |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 288 | |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 289 | // TODO: send writes in bulk (e.g. all entries to insert, modify or delete). |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 290 | // Instead of calling the client for each one of them. |
| 291 | |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 292 | for (FlowRule ruleToApply : rules) { |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 293 | |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 294 | final PiTableEntry piEntryToApply; |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 295 | try { |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 296 | piEntryToApply = translator.translate(ruleToApply, pipeconf); |
Carmelo Cascone | 326ad2d | 2017-11-28 18:09:13 -0800 | [diff] [blame] | 297 | } catch (PiTranslationException e) { |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 298 | log.warn("Unable to translate flow rule for pipeconf '{}': {} - {}", |
| 299 | pipeconf.id(), e.getMessage(), ruleToApply); |
| 300 | // Next rule. |
| 301 | continue; |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 302 | } |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 303 | |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 304 | final PiTableEntryHandle handle = PiTableEntryHandle |
| 305 | .of(deviceId, piEntryToApply); |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 306 | |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 307 | // Serialize operations over the same match key/table/device ID. |
Carmelo Cascone | 33b27bc | 2018-09-09 22:56:14 -0700 | [diff] [blame] | 308 | ENTRY_LOCKS.get(handle).lock(); |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 309 | try { |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 310 | if (applyEntry(handle, piEntryToApply, |
| 311 | ruleToApply, driverOperation)) { |
| 312 | result.add(ruleToApply); |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 313 | } |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 314 | } finally { |
Carmelo Cascone | 33b27bc | 2018-09-09 22:56:14 -0700 | [diff] [blame] | 315 | ENTRY_LOCKS.get(handle).unlock(); |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 316 | } |
| 317 | } |
| 318 | |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 319 | return result.build(); |
| 320 | } |
| 321 | |
| 322 | /** |
| 323 | * Applies the given entry to the device, and returns true if the operation |
| 324 | * was successful, false otherwise. |
| 325 | */ |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 326 | private boolean applyEntry(final PiTableEntryHandle handle, |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 327 | PiTableEntry piEntryToApply, |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 328 | final FlowRule ruleToApply, |
| 329 | final Operation driverOperation) { |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 330 | // Depending on the driver operation, and if a matching rule exists on |
| 331 | // the device, decide which P4 Runtime write operation to perform for |
| 332 | // this entry. |
| 333 | final TimedEntry<PiTableEntry> piEntryOnDevice = tableMirror.get(handle); |
| 334 | final WriteOperationType p4Operation; |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 335 | final WriteOperationType storeOperation; |
| 336 | |
| 337 | final boolean defaultAsEntry = driverBoolProperty( |
| 338 | TABLE_DEFAULT_AS_ENTRY, DEFAULT_TABLE_DEFAULT_AS_ENTRY); |
| 339 | final boolean ignoreSameEntryUpdate = driverBoolProperty( |
| 340 | IGNORE_SAME_ENTRY_UPDATE, DEFAULT_IGNORE_SAME_ENTRY_UPDATE); |
| 341 | final boolean deleteBeforeUpdate = driverBoolProperty( |
| 342 | DELETE_BEFORE_UPDATE, DEFAULT_DELETE_BEFORE_UPDATE); |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 343 | if (driverOperation == APPLY) { |
| 344 | if (piEntryOnDevice == null) { |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 345 | // Entry is first-timer, INSERT or MODIFY if default action. |
| 346 | p4Operation = !piEntryToApply.isDefaultAction() || defaultAsEntry |
| 347 | ? INSERT : MODIFY; |
| 348 | storeOperation = p4Operation; |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 349 | } else { |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 350 | if (ignoreSameEntryUpdate && |
| 351 | piEntryToApply.action().equals(piEntryOnDevice.entry().action())) { |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 352 | log.debug("Ignoring re-apply of existing entry: {}", piEntryToApply); |
| 353 | p4Operation = null; |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 354 | } else if (deleteBeforeUpdate && !piEntryToApply.isDefaultAction()) { |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 355 | // Some devices return error when updating existing |
| 356 | // entries. If requested, remove entry before |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 357 | // re-inserting the modified one, except the default action |
| 358 | // entry, that cannot be removed. |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 359 | applyEntry(handle, piEntryOnDevice.entry(), null, REMOVE); |
| 360 | p4Operation = INSERT; |
| 361 | } else { |
| 362 | p4Operation = MODIFY; |
| 363 | } |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 364 | storeOperation = p4Operation; |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 365 | } |
| 366 | } else { |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 367 | if (piEntryToApply.isDefaultAction()) { |
| 368 | // Cannot remove default action. Instead we should use the |
| 369 | // original defined by the interpreter (if any). |
| 370 | piEntryToApply = getOriginalDefaultEntry(piEntryToApply.table()); |
| 371 | if (piEntryToApply == null) { |
| 372 | return false; |
| 373 | } |
| 374 | p4Operation = MODIFY; |
| 375 | } else { |
| 376 | p4Operation = DELETE; |
| 377 | } |
| 378 | // Still want to delete the default entry from the mirror and |
| 379 | // translation store. |
| 380 | storeOperation = DELETE; |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | if (p4Operation != null) { |
| 384 | if (writeEntry(piEntryToApply, p4Operation)) { |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 385 | updateStores(handle, piEntryToApply, ruleToApply, storeOperation); |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 386 | return true; |
| 387 | } else { |
| 388 | return false; |
| 389 | } |
| 390 | } else { |
| 391 | // If no operation, let's pretend we applied the rule to the device. |
| 392 | return true; |
| 393 | } |
| 394 | } |
| 395 | |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 396 | private PiTableEntry getOriginalDefaultEntry(PiTableId tableId) { |
| 397 | final PiPipelineInterpreter interpreter = getInterpreter(); |
| 398 | if (interpreter == null) { |
| 399 | log.warn("Missing interpreter for {}, cannot get default action", |
| 400 | deviceId); |
| 401 | return null; |
| 402 | } |
| 403 | if (!interpreter.getOriginalDefaultAction(tableId).isPresent()) { |
| 404 | log.warn("Interpreter of {} doesn't define a default action for " + |
| 405 | "table {}, cannot produce default action entry", |
| 406 | deviceId, tableId); |
| 407 | return null; |
| 408 | } |
| 409 | return PiTableEntry.builder() |
| 410 | .forTable(tableId) |
| 411 | .withAction(interpreter.getOriginalDefaultAction(tableId).get()) |
| 412 | .build(); |
| 413 | } |
| 414 | |
| 415 | private boolean isOriginalDefaultEntry(PiTableEntry entry) { |
| 416 | if (!entry.isDefaultAction()) { |
| 417 | return false; |
| 418 | } |
| 419 | final PiTableEntry originalDefaultEntry = getOriginalDefaultEntry(entry.table()); |
| 420 | return originalDefaultEntry != null && |
| 421 | originalDefaultEntry.action().equals(entry.action()); |
| 422 | } |
| 423 | |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 424 | /** |
| 425 | * Performs a write operation on the device. |
| 426 | */ |
| 427 | private boolean writeEntry(PiTableEntry entry, |
| 428 | WriteOperationType p4Operation) { |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 429 | final CompletableFuture<Boolean> future = client.writeTableEntries( |
| 430 | newArrayList(entry), p4Operation, pipeconf); |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 431 | // If false, errors logged by internal calls. |
| 432 | return getFutureWithDeadline( |
| 433 | future, "performing table " + p4Operation.name(), false); |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | private void updateStores(PiTableEntryHandle handle, |
| 437 | PiTableEntry entry, |
| 438 | FlowRule rule, |
| 439 | WriteOperationType p4Operation) { |
| 440 | switch (p4Operation) { |
| 441 | case INSERT: |
| 442 | case MODIFY: |
| 443 | tableMirror.put(handle, entry); |
| 444 | translator.learn(handle, new PiTranslatedEntity<>(rule, entry, handle)); |
| 445 | break; |
| 446 | case DELETE: |
| 447 | tableMirror.remove(handle); |
| 448 | translator.forget(handle); |
| 449 | break; |
| 450 | default: |
| 451 | throw new IllegalArgumentException( |
| 452 | "Unknown operation " + p4Operation.name()); |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | private Map<PiTableEntry, PiCounterCellData> readEntryCounters( |
Carmelo Cascone | 255125d | 2018-04-11 14:03:22 -0700 | [diff] [blame] | 457 | Collection<PiTableEntry> tableEntries) { |
| 458 | if (!driverBoolProperty(SUPPORT_TABLE_COUNTERS, |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 459 | DEFAULT_SUPPORT_TABLE_COUNTERS) |
| 460 | || tableEntries.isEmpty()) { |
Carmelo Cascone | 255125d | 2018-04-11 14:03:22 -0700 | [diff] [blame] | 461 | return Collections.emptyMap(); |
| 462 | } |
| 463 | |
steven30801 | 7632e15 | 2018-10-20 00:51:08 +0800 | [diff] [blame^] | 464 | if (driverBoolProperty(READ_COUNTERS_WITH_TABLE_ENTRIES, |
| 465 | DEFAULT_READ_COUNTERS_WITH_TABLE_ENTRIES)) { |
| 466 | return tableEntries.stream().collect(Collectors.toMap(c -> c, PiTableEntry::counter)); |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 467 | } else { |
steven30801 | 7632e15 | 2018-10-20 00:51:08 +0800 | [diff] [blame^] | 468 | Collection<PiCounterCell> cells; |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 469 | Set<PiCounterCellId> cellIds = tableEntries.stream() |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 470 | // Ignore counter for default entry. |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 471 | .filter(e -> !e.isDefaultAction()) |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 472 | .filter(e -> tableHasCounter(e.table())) |
| 473 | .map(PiCounterCellId::ofDirect) |
| 474 | .collect(Collectors.toSet()); |
steven30801 | 7632e15 | 2018-10-20 00:51:08 +0800 | [diff] [blame^] | 475 | cells = getFutureWithDeadline(client.readCounterCells(cellIds, pipeconf), |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 476 | "reading table counters", Collections.emptyList()); |
steven30801 | 7632e15 | 2018-10-20 00:51:08 +0800 | [diff] [blame^] | 477 | return cells.stream() |
| 478 | .collect(Collectors.toMap(c -> c.cellId().tableEntry(), PiCounterCell::data)); |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 479 | } |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 480 | } |
| 481 | |
Carmelo Cascone | 255125d | 2018-04-11 14:03:22 -0700 | [diff] [blame] | 482 | private boolean tableHasCounter(PiTableId tableId) { |
| 483 | return pipelineModel.table(tableId).isPresent() && |
| 484 | !pipelineModel.table(tableId).get().counters().isEmpty(); |
| 485 | } |
| 486 | |
Carmelo Cascone | 33b27bc | 2018-09-09 22:56:14 -0700 | [diff] [blame] | 487 | private boolean tableIsConstant(PiTableId tableId) { |
| 488 | return pipelineModel.table(tableId).isPresent() && |
| 489 | pipelineModel.table(tableId).get().isConstantTable(); |
| 490 | } |
| 491 | |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 492 | enum Operation { |
| 493 | APPLY, REMOVE |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 494 | } |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 495 | } |