blob: 85a87fe99a099e14ab7d432eed6b1a9c0a523d91 [file] [log] [blame]
Frank Wang0e805082017-07-21 14:37:35 +08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Frank Wang0e805082017-07-21 14:37:35 +08003 *
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 Campanella0288c872017-08-07 18:32:51 +020017package org.onosproject.drivers.p4runtime;
Frank Wang0e805082017-07-21 14:37:35 +080018
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +020019import com.google.common.collect.ImmutableList;
20import com.google.common.collect.Lists;
Carmelo Cascone50d195f2018-09-11 13:26:38 -070021import com.google.common.collect.Maps;
Carmelo Cascone33b27bc2018-09-09 22:56:14 -070022import com.google.common.util.concurrent.Striped;
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -080023import org.onlab.util.SharedExecutors;
24import org.onosproject.drivers.p4runtime.mirror.P4RuntimeTableMirror;
25import org.onosproject.drivers.p4runtime.mirror.TimedEntry;
Frank Wang0e805082017-07-21 14:37:35 +080026import org.onosproject.net.flow.DefaultFlowEntry;
27import org.onosproject.net.flow.FlowEntry;
28import org.onosproject.net.flow.FlowRule;
29import org.onosproject.net.flow.FlowRuleProgrammable;
Carmelo Cascone50d195f2018-09-11 13:26:38 -070030import org.onosproject.net.pi.model.PiPipelineInterpreter;
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +020031import org.onosproject.net.pi.model.PiPipelineModel;
Carmelo Cascone87892e22017-11-13 16:01:29 -080032import org.onosproject.net.pi.model.PiTableId;
Carmelo Cascone50d195f2018-09-11 13:26:38 -070033import org.onosproject.net.pi.model.PiTableModel;
Carmelo Cascone7f75be42017-09-07 14:37:02 +020034import org.onosproject.net.pi.runtime.PiCounterCellData;
35import org.onosproject.net.pi.runtime.PiCounterCellId;
Frank Wang0e805082017-07-21 14:37:35 +080036import org.onosproject.net.pi.runtime.PiTableEntry;
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -080037import org.onosproject.net.pi.runtime.PiTableEntryHandle;
38import org.onosproject.net.pi.service.PiFlowRuleTranslator;
39import org.onosproject.net.pi.service.PiTranslatedEntity;
Carmelo Cascone326ad2d2017-11-28 18:09:13 -080040import org.onosproject.net.pi.service.PiTranslationException;
Frank Wang0e805082017-07-21 14:37:35 +080041import org.onosproject.p4runtime.api.P4RuntimeClient.WriteOperationType;
Frank Wang0e805082017-07-21 14:37:35 +080042
43import java.util.Collection;
44import java.util.Collections;
Manjunath Vanaraj59ad6572017-12-26 11:10:57 +053045import java.util.List;
Carmelo Cascone3da671a2018-02-12 10:43:35 -080046import java.util.Map;
Carmelo Cascone26600972018-09-10 00:23:20 -070047import java.util.Objects;
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -080048import java.util.Optional;
Carmelo Cascone7f75be42017-09-07 14:37:02 +020049import java.util.Set;
Carmelo Casconee5b28722018-06-22 17:28:28 +020050import java.util.concurrent.CompletableFuture;
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +020051import java.util.concurrent.locks.Lock;
Carmelo Casconefe99be92017-09-11 21:55:54 +020052import java.util.stream.Collectors;
Carmelo Cascone50d195f2018-09-11 13:26:38 -070053import java.util.stream.Stream;
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +020054
55import static com.google.common.collect.Lists.newArrayList;
Andrea Campanella0288c872017-08-07 18:32:51 +020056import static org.onosproject.drivers.p4runtime.P4RuntimeFlowRuleProgrammable.Operation.APPLY;
57import static org.onosproject.drivers.p4runtime.P4RuntimeFlowRuleProgrammable.Operation.REMOVE;
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +020058import static org.onosproject.net.flow.FlowEntry.FlowEntryState.ADDED;
Carmelo Cascone87892e22017-11-13 16:01:29 -080059import static org.onosproject.p4runtime.api.P4RuntimeClient.WriteOperationType.DELETE;
60import static org.onosproject.p4runtime.api.P4RuntimeClient.WriteOperationType.INSERT;
61import static org.onosproject.p4runtime.api.P4RuntimeClient.WriteOperationType.MODIFY;
Frank Wang0e805082017-07-21 14:37:35 +080062
63/**
Carmelo Casconee3a7c742017-09-01 01:25:52 +020064 * Implementation of the flow rule programmable behaviour for P4Runtime.
Frank Wang0e805082017-07-21 14:37:35 +080065 */
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -080066public class P4RuntimeFlowRuleProgrammable
67 extends AbstractP4RuntimeHandlerBehaviour
68 implements FlowRuleProgrammable {
Frank Wang0e805082017-07-21 14:37:35 +080069
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -080070 // When updating an existing rule, if true, we issue a DELETE operation
71 // before inserting the new one, otherwise we issue a MODIFY operation. This
72 // is useful fore devices that do not support MODIFY operations for table
73 // entries.
Carmelo Cascone3da671a2018-02-12 10:43:35 -080074 private static final String DELETE_BEFORE_UPDATE = "tableDeleteBeforeUpdate";
75 private static final boolean DEFAULT_DELETE_BEFORE_UPDATE = false;
Carmelo Cascone2308e522017-08-25 02:35:12 +020076
Carmelo Cascone81929aa2018-04-07 01:38:55 -070077 // If true, we ignore re-installing rules that already exist in the
78 // device mirror, i.e. same match key and action.
Carmelo Cascone3da671a2018-02-12 10:43:35 -080079 private static final String IGNORE_SAME_ENTRY_UPDATE = "tableIgnoreSameEntryUpdate";
80 private static final boolean DEFAULT_IGNORE_SAME_ENTRY_UPDATE = false;
Carmelo Cascone2308e522017-08-25 02:35:12 +020081
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -080082 // If true, we avoid querying the device and return what's already known by
83 // the ONOS store.
Carmelo Cascone3da671a2018-02-12 10:43:35 -080084 private static final String READ_FROM_MIRROR = "tableReadFromMirror";
85 private static final boolean DEFAULT_READ_FROM_MIRROR = false;
Carmelo Casconefe99be92017-09-11 21:55:54 +020086
Carmelo Cascone255125d2018-04-11 14:03:22 -070087 // 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
Carmelo Cascone3da671a2018-02-12 10:43:35 -080092 // If true, we read all direct counters of a table with one request.
93 // Otherwise, we send as many requests as the number of table entries.
94 private static final String READ_ALL_DIRECT_COUNTERS = "tableReadAllDirectCounters";
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -080095 // FIXME: set to true as soon as the feature is implemented in P4Runtime.
Carmelo Cascone3da671a2018-02-12 10:43:35 -080096 private static final boolean DEFAULT_READ_ALL_DIRECT_COUNTERS = false;
Carmelo Cascone7f75be42017-09-07 14:37:02 +020097
Carmelo Cascone50d195f2018-09-11 13:26:38 -070098 // 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
Manjunath Vanaraj59ad6572017-12-26 11:10:57 +0530104 // Needed to synchronize operations over the same table entry.
Carmelo Cascone33b27bc2018-09-09 22:56:14 -0700105 private static final Striped<Lock> ENTRY_LOCKS = Striped.lock(30);
106
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200107 private PiPipelineModel pipelineModel;
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800108 private P4RuntimeTableMirror tableMirror;
109 private PiFlowRuleTranslator translator;
Frank Wang0e805082017-07-21 14:37:35 +0800110
Carmelo Casconee3a7c742017-09-01 01:25:52 +0200111 @Override
112 protected boolean setupBehaviour() {
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200113
Carmelo Casconee3a7c742017-09-01 01:25:52 +0200114 if (!super.setupBehaviour()) {
Frank Wang0e805082017-07-21 14:37:35 +0800115 return false;
116 }
117
Carmelo Casconee3a7c742017-09-01 01:25:52 +0200118 pipelineModel = pipeconf.pipelineModel();
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800119 tableMirror = handler().get(P4RuntimeTableMirror.class);
120 translator = piTranslationService.flowRuleTranslator();
Frank Wang0e805082017-07-21 14:37:35 +0800121 return true;
122 }
123
124 @Override
125 public Collection<FlowEntry> getFlowEntries() {
126
Carmelo Casconee3a7c742017-09-01 01:25:52 +0200127 if (!setupBehaviour()) {
Frank Wang0e805082017-07-21 14:37:35 +0800128 return Collections.emptyList();
129 }
130
Carmelo Cascone3da671a2018-02-12 10:43:35 -0800131 if (driverBoolProperty(READ_FROM_MIRROR, DEFAULT_READ_FROM_MIRROR)) {
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800132 return getFlowEntriesFromMirror();
Carmelo Casconefe99be92017-09-11 21:55:54 +0200133 }
134
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800135 final ImmutableList.Builder<FlowEntry> result = ImmutableList.builder();
136 final List<PiTableEntry> inconsistentEntries = Lists.newArrayList();
Frank Wang0e805082017-07-21 14:37:35 +0800137
Carmelo Cascone50d195f2018-09-11 13:26:38 -0700138 // Read table entries, including default ones.
139 final Collection<PiTableEntry> deviceEntries = Stream.concat(
140 streamEntries(), streamDefaultEntries())
141 // Ignore entries from constant tables.
Carmelo Cascone33b27bc2018-09-09 22:56:14 -0700142 .filter(e -> !tableIsConstant(e.table()))
143 .collect(Collectors.toList());
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200144
Carmelo Cascone50d195f2018-09-11 13:26:38 -0700145 if (deviceEntries.isEmpty()) {
Carmelo Casconee5b28722018-06-22 17:28:28 +0200146 return Collections.emptyList();
147 }
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200148
Carmelo Cascone50d195f2018-09-11 13:26:38 -0700149 // Synchronize mirror with the device state.
150 syncMirror(deviceEntries);
151 // Read table direct counters for non default-entries (if any).
152 // TODO: ONOS-7596 read counters with table entries
Carmelo Casconee5b28722018-06-22 17:28:28 +0200153 final Map<PiTableEntry, PiCounterCellData> counterCellMap =
Carmelo Cascone50d195f2018-09-11 13:26:38 -0700154 readEntryCounters(deviceEntries);
Carmelo Casconee5b28722018-06-22 17:28:28 +0200155 // Forge flow entries with counter values.
Carmelo Cascone50d195f2018-09-11 13:26:38 -0700156 for (PiTableEntry entry : deviceEntries) {
Carmelo Casconee5b28722018-06-22 17:28:28 +0200157 final FlowEntry flowEntry = forgeFlowEntry(
Carmelo Cascone50d195f2018-09-11 13:26:38 -0700158 entry, counterCellMap.get(entry));
Carmelo Casconee5b28722018-06-22 17:28:28 +0200159 if (flowEntry == null) {
160 // Entry is on device but unknown to translation service or
161 // device mirror. Inconsistent. Mark for removal.
162 // TODO: make this behaviour configurable
163 // In some cases it's fine for the device to have rules
Carmelo Cascone50d195f2018-09-11 13:26:38 -0700164 // that were not installed by us, e.g. original default entry.
165 if (!isOriginalDefaultEntry(entry)) {
166 inconsistentEntries.add(entry);
167 }
Carmelo Casconee5b28722018-06-22 17:28:28 +0200168 } else {
169 result.add(flowEntry);
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200170 }
171 }
172
173 if (inconsistentEntries.size() > 0) {
Carmelo Cascone33b27bc2018-09-09 22:56:14 -0700174 // Trigger clean up of inconsistent entries.
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800175 SharedExecutors.getSingleThreadExecutor().execute(
176 () -> cleanUpInconsistentEntries(inconsistentEntries));
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200177 }
178
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800179 return result.build();
Frank Wang0e805082017-07-21 14:37:35 +0800180 }
181
Carmelo Cascone50d195f2018-09-11 13:26:38 -0700182 private Stream<PiTableEntry> streamEntries() {
183 return getFutureWithDeadline(
184 client.dumpAllTables(pipeconf), "dumping all tables",
185 Collections.emptyList())
186 .stream();
187 }
188
189 private Stream<PiTableEntry> streamDefaultEntries() {
190 // Ignore tables with constant default action.
191 final Set<PiTableId> defaultTables = pipelineModel.tables()
192 .stream()
193 .filter(table -> !table.constDefaultAction().isPresent())
194 .map(PiTableModel::id)
195 .collect(Collectors.toSet());
196 return defaultTables.isEmpty() ? Stream.empty()
197 : getFutureWithDeadline(
198 client.dumpTables(defaultTables, true, pipeconf),
199 "dumping default table entries",
200 Collections.emptyList())
201 .stream();
202 }
203
204 private void syncMirror(Collection<PiTableEntry> entries) {
205 Map<PiTableEntryHandle, PiTableEntry> handleMap = Maps.newHashMap();
206 entries.forEach(e -> handleMap.put(PiTableEntryHandle.of(deviceId, e), e));
207 tableMirror.sync(deviceId, handleMap);
208 }
209
Frank Wang0e805082017-07-21 14:37:35 +0800210 @Override
211 public Collection<FlowRule> applyFlowRules(Collection<FlowRule> rules) {
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200212 return processFlowRules(rules, APPLY);
Frank Wang0e805082017-07-21 14:37:35 +0800213 }
214
215 @Override
216 public Collection<FlowRule> removeFlowRules(Collection<FlowRule> rules) {
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200217 return processFlowRules(rules, REMOVE);
Frank Wang0e805082017-07-21 14:37:35 +0800218 }
219
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800220 private FlowEntry forgeFlowEntry(PiTableEntry entry,
221 PiCounterCellData cellData) {
222 final PiTableEntryHandle handle = PiTableEntryHandle
223 .of(deviceId, entry);
224 final Optional<PiTranslatedEntity<FlowRule, PiTableEntry>>
225 translatedEntity = translator.lookup(handle);
226 final TimedEntry<PiTableEntry> timedEntry = tableMirror.get(handle);
227
228 if (!translatedEntity.isPresent()) {
Carmelo Cascone26600972018-09-10 00:23:20 -0700229 log.warn("Table entry handle not found in translation store: {}", handle);
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800230 return null;
231 }
232
233 if (timedEntry == null) {
Carmelo Cascone26600972018-09-10 00:23:20 -0700234 log.warn("Table entry handle not found in device mirror: {}", handle);
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800235 return null;
236 }
237
238 if (cellData != null) {
239 return new DefaultFlowEntry(translatedEntity.get().original(),
Carmelo Cascone81929aa2018-04-07 01:38:55 -0700240 ADDED, timedEntry.lifeSec(), cellData.packets(),
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800241 cellData.bytes());
242 } else {
243 return new DefaultFlowEntry(translatedEntity.get().original(),
244 ADDED, timedEntry.lifeSec(), 0, 0);
245 }
246 }
247
248 private Collection<FlowEntry> getFlowEntriesFromMirror() {
249 return tableMirror.getAll(deviceId).stream()
250 .map(timedEntry -> forgeFlowEntry(
251 timedEntry.entry(), null))
Carmelo Cascone26600972018-09-10 00:23:20 -0700252 .filter(Objects::nonNull)
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800253 .collect(Collectors.toList());
254 }
255
256 private void cleanUpInconsistentEntries(Collection<PiTableEntry> piEntries) {
Carmelo Cascone26600972018-09-10 00:23:20 -0700257 log.warn("Found {} inconsistent table entries on {}, removing them...",
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800258 piEntries.size(), deviceId);
259 piEntries.forEach(entry -> {
260 log.debug(entry.toString());
Carmelo Cascone26600972018-09-10 00:23:20 -0700261 final PiTableEntryHandle handle = PiTableEntryHandle.of(deviceId, entry);
262 ENTRY_LOCKS.get(handle).lock();
263 try {
264 applyEntry(handle, entry, null, REMOVE);
265 } finally {
266 ENTRY_LOCKS.get(handle).unlock();
267 }
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800268 });
269 }
270
271 private Collection<FlowRule> processFlowRules(Collection<FlowRule> rules,
272 Operation driverOperation) {
Frank Wang0e805082017-07-21 14:37:35 +0800273
Carmelo Cascone50d195f2018-09-11 13:26:38 -0700274 if (!setupBehaviour() || rules.isEmpty()) {
Frank Wang0e805082017-07-21 14:37:35 +0800275 return Collections.emptyList();
276 }
277
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800278 final ImmutableList.Builder<FlowRule> result = ImmutableList.builder();
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200279
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800280 // TODO: send writes in bulk (e.g. all entries to insert, modify or delete).
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200281 // Instead of calling the client for each one of them.
282
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800283 for (FlowRule ruleToApply : rules) {
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200284
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800285 final PiTableEntry piEntryToApply;
Frank Wang0e805082017-07-21 14:37:35 +0800286 try {
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800287 piEntryToApply = translator.translate(ruleToApply, pipeconf);
Carmelo Cascone326ad2d2017-11-28 18:09:13 -0800288 } catch (PiTranslationException e) {
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800289 log.warn("Unable to translate flow rule for pipeconf '{}': {} - {}",
290 pipeconf.id(), e.getMessage(), ruleToApply);
291 // Next rule.
292 continue;
Frank Wang0e805082017-07-21 14:37:35 +0800293 }
Frank Wang0e805082017-07-21 14:37:35 +0800294
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800295 final PiTableEntryHandle handle = PiTableEntryHandle
296 .of(deviceId, piEntryToApply);
Frank Wang0e805082017-07-21 14:37:35 +0800297
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800298 // Serialize operations over the same match key/table/device ID.
Carmelo Cascone33b27bc2018-09-09 22:56:14 -0700299 ENTRY_LOCKS.get(handle).lock();
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200300 try {
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800301 if (applyEntry(handle, piEntryToApply,
302 ruleToApply, driverOperation)) {
303 result.add(ruleToApply);
Frank Wang0e805082017-07-21 14:37:35 +0800304 }
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200305 } finally {
Carmelo Cascone33b27bc2018-09-09 22:56:14 -0700306 ENTRY_LOCKS.get(handle).unlock();
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200307 }
308 }
309
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800310 return result.build();
311 }
312
313 /**
314 * Applies the given entry to the device, and returns true if the operation
315 * was successful, false otherwise.
316 */
Carmelo Cascone50d195f2018-09-11 13:26:38 -0700317 private boolean applyEntry(final PiTableEntryHandle handle,
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800318 PiTableEntry piEntryToApply,
Carmelo Cascone50d195f2018-09-11 13:26:38 -0700319 final FlowRule ruleToApply,
320 final Operation driverOperation) {
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800321 // Depending on the driver operation, and if a matching rule exists on
322 // the device, decide which P4 Runtime write operation to perform for
323 // this entry.
324 final TimedEntry<PiTableEntry> piEntryOnDevice = tableMirror.get(handle);
325 final WriteOperationType p4Operation;
Carmelo Cascone50d195f2018-09-11 13:26:38 -0700326 final WriteOperationType storeOperation;
327
328 final boolean defaultAsEntry = driverBoolProperty(
329 TABLE_DEFAULT_AS_ENTRY, DEFAULT_TABLE_DEFAULT_AS_ENTRY);
330 final boolean ignoreSameEntryUpdate = driverBoolProperty(
331 IGNORE_SAME_ENTRY_UPDATE, DEFAULT_IGNORE_SAME_ENTRY_UPDATE);
332 final boolean deleteBeforeUpdate = driverBoolProperty(
333 DELETE_BEFORE_UPDATE, DEFAULT_DELETE_BEFORE_UPDATE);
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800334 if (driverOperation == APPLY) {
335 if (piEntryOnDevice == null) {
Carmelo Cascone50d195f2018-09-11 13:26:38 -0700336 // Entry is first-timer, INSERT or MODIFY if default action.
337 p4Operation = !piEntryToApply.isDefaultAction() || defaultAsEntry
338 ? INSERT : MODIFY;
339 storeOperation = p4Operation;
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800340 } else {
Carmelo Cascone50d195f2018-09-11 13:26:38 -0700341 if (ignoreSameEntryUpdate &&
342 piEntryToApply.action().equals(piEntryOnDevice.entry().action())) {
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800343 log.debug("Ignoring re-apply of existing entry: {}", piEntryToApply);
344 p4Operation = null;
Carmelo Cascone50d195f2018-09-11 13:26:38 -0700345 } else if (deleteBeforeUpdate && !piEntryToApply.isDefaultAction()) {
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800346 // Some devices return error when updating existing
347 // entries. If requested, remove entry before
Carmelo Cascone50d195f2018-09-11 13:26:38 -0700348 // re-inserting the modified one, except the default action
349 // entry, that cannot be removed.
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800350 applyEntry(handle, piEntryOnDevice.entry(), null, REMOVE);
351 p4Operation = INSERT;
352 } else {
353 p4Operation = MODIFY;
354 }
Carmelo Cascone50d195f2018-09-11 13:26:38 -0700355 storeOperation = p4Operation;
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800356 }
357 } else {
Carmelo Cascone50d195f2018-09-11 13:26:38 -0700358 if (piEntryToApply.isDefaultAction()) {
359 // Cannot remove default action. Instead we should use the
360 // original defined by the interpreter (if any).
361 piEntryToApply = getOriginalDefaultEntry(piEntryToApply.table());
362 if (piEntryToApply == null) {
363 return false;
364 }
365 p4Operation = MODIFY;
366 } else {
367 p4Operation = DELETE;
368 }
369 // Still want to delete the default entry from the mirror and
370 // translation store.
371 storeOperation = DELETE;
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800372 }
373
374 if (p4Operation != null) {
375 if (writeEntry(piEntryToApply, p4Operation)) {
Carmelo Cascone50d195f2018-09-11 13:26:38 -0700376 updateStores(handle, piEntryToApply, ruleToApply, storeOperation);
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800377 return true;
378 } else {
379 return false;
380 }
381 } else {
382 // If no operation, let's pretend we applied the rule to the device.
383 return true;
384 }
385 }
386
Carmelo Cascone50d195f2018-09-11 13:26:38 -0700387 private PiTableEntry getOriginalDefaultEntry(PiTableId tableId) {
388 final PiPipelineInterpreter interpreter = getInterpreter();
389 if (interpreter == null) {
390 log.warn("Missing interpreter for {}, cannot get default action",
391 deviceId);
392 return null;
393 }
394 if (!interpreter.getOriginalDefaultAction(tableId).isPresent()) {
395 log.warn("Interpreter of {} doesn't define a default action for " +
396 "table {}, cannot produce default action entry",
397 deviceId, tableId);
398 return null;
399 }
400 return PiTableEntry.builder()
401 .forTable(tableId)
402 .withAction(interpreter.getOriginalDefaultAction(tableId).get())
403 .build();
404 }
405
406 private boolean isOriginalDefaultEntry(PiTableEntry entry) {
407 if (!entry.isDefaultAction()) {
408 return false;
409 }
410 final PiTableEntry originalDefaultEntry = getOriginalDefaultEntry(entry.table());
411 return originalDefaultEntry != null &&
412 originalDefaultEntry.action().equals(entry.action());
413 }
414
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800415 /**
416 * Performs a write operation on the device.
417 */
418 private boolean writeEntry(PiTableEntry entry,
419 WriteOperationType p4Operation) {
Carmelo Casconee5b28722018-06-22 17:28:28 +0200420 final CompletableFuture<Boolean> future = client.writeTableEntries(
421 newArrayList(entry), p4Operation, pipeconf);
Carmelo Cascone50d195f2018-09-11 13:26:38 -0700422 // If false, errors logged by internal calls.
423 return getFutureWithDeadline(
424 future, "performing table " + p4Operation.name(), false);
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800425 }
426
427 private void updateStores(PiTableEntryHandle handle,
428 PiTableEntry entry,
429 FlowRule rule,
430 WriteOperationType p4Operation) {
431 switch (p4Operation) {
432 case INSERT:
433 case MODIFY:
434 tableMirror.put(handle, entry);
435 translator.learn(handle, new PiTranslatedEntity<>(rule, entry, handle));
436 break;
437 case DELETE:
438 tableMirror.remove(handle);
439 translator.forget(handle);
440 break;
441 default:
442 throw new IllegalArgumentException(
443 "Unknown operation " + p4Operation.name());
444 }
445 }
446
447 private Map<PiTableEntry, PiCounterCellData> readEntryCounters(
Carmelo Cascone255125d2018-04-11 14:03:22 -0700448 Collection<PiTableEntry> tableEntries) {
449 if (!driverBoolProperty(SUPPORT_TABLE_COUNTERS,
Carmelo Casconee5b28722018-06-22 17:28:28 +0200450 DEFAULT_SUPPORT_TABLE_COUNTERS)
451 || tableEntries.isEmpty()) {
Carmelo Cascone255125d2018-04-11 14:03:22 -0700452 return Collections.emptyMap();
453 }
454
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800455 Collection<PiCounterCellData> cellDatas;
Carmelo Casconee5b28722018-06-22 17:28:28 +0200456
457 if (driverBoolProperty(READ_ALL_DIRECT_COUNTERS,
458 DEFAULT_READ_ALL_DIRECT_COUNTERS)) {
459 // FIXME: read counters when dumping table entries ONOS-7596
460 cellDatas = Collections.emptyList();
461 } else {
462 Set<PiCounterCellId> cellIds = tableEntries.stream()
Carmelo Cascone50d195f2018-09-11 13:26:38 -0700463 .filter(e -> !e.isDefaultAction())
Carmelo Casconee5b28722018-06-22 17:28:28 +0200464 .filter(e -> tableHasCounter(e.table()))
465 .map(PiCounterCellId::ofDirect)
466 .collect(Collectors.toSet());
467 cellDatas = getFutureWithDeadline(client.readCounterCells(cellIds, pipeconf),
468 "reading table counters", Collections.emptyList());
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800469 }
Carmelo Casconee5b28722018-06-22 17:28:28 +0200470 return cellDatas.stream()
471 .collect(Collectors.toMap(c -> c.cellId().tableEntry(), c -> c));
472
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200473 }
474
Carmelo Cascone255125d2018-04-11 14:03:22 -0700475 private boolean tableHasCounter(PiTableId tableId) {
476 return pipelineModel.table(tableId).isPresent() &&
477 !pipelineModel.table(tableId).get().counters().isEmpty();
478 }
479
Carmelo Cascone33b27bc2018-09-09 22:56:14 -0700480 private boolean tableIsConstant(PiTableId tableId) {
481 return pipelineModel.table(tableId).isPresent() &&
482 pipelineModel.table(tableId).get().isConstantTable();
483 }
484
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200485 enum Operation {
486 APPLY, REMOVE
Frank Wang0e805082017-07-21 14:37:35 +0800487 }
Carmelo Cascone87892e22017-11-13 16:01:29 -0800488}