blob: 81626fe988eec3487f634a8aee6b2e299ae080e7 [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;
Frank Wang0e805082017-07-21 14:37:35 +080021import com.google.common.collect.Maps;
Carmelo Cascone7f75be42017-09-07 14:37:02 +020022import io.grpc.StatusRuntimeException;
Frank Wang0e805082017-07-21 14:37:35 +080023import org.onosproject.net.flow.DefaultFlowEntry;
24import org.onosproject.net.flow.FlowEntry;
25import org.onosproject.net.flow.FlowRule;
26import org.onosproject.net.flow.FlowRuleProgrammable;
Carmelo Cascone87892e22017-11-13 16:01:29 -080027import org.onosproject.net.pi.model.PiCounterId;
Frank Wang0e805082017-07-21 14:37:35 +080028import org.onosproject.net.pi.model.PiPipelineInterpreter;
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +020029import org.onosproject.net.pi.model.PiPipelineModel;
Carmelo Cascone87892e22017-11-13 16:01:29 -080030import org.onosproject.net.pi.model.PiTableId;
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +020031import org.onosproject.net.pi.model.PiTableModel;
Carmelo Cascone7f75be42017-09-07 14:37:02 +020032import org.onosproject.net.pi.runtime.PiCounterCellData;
33import org.onosproject.net.pi.runtime.PiCounterCellId;
Frank Wang0e805082017-07-21 14:37:35 +080034import org.onosproject.net.pi.runtime.PiTableEntry;
Carmelo Cascone87892e22017-11-13 16:01:29 -080035import org.onosproject.net.pi.runtime.PiTranslationService;
Frank Wang0e805082017-07-21 14:37:35 +080036import org.onosproject.p4runtime.api.P4RuntimeClient.WriteOperationType;
Andrea Campanella0288c872017-08-07 18:32:51 +020037import org.onosproject.p4runtime.api.P4RuntimeFlowRuleWrapper;
38import org.onosproject.p4runtime.api.P4RuntimeTableEntryReference;
Frank Wang0e805082017-07-21 14:37:35 +080039
40import java.util.Collection;
41import java.util.Collections;
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +020042import java.util.List;
Carmelo Cascone7f75be42017-09-07 14:37:02 +020043import java.util.Map;
44import java.util.Set;
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +020045import java.util.concurrent.ConcurrentMap;
46import java.util.concurrent.ExecutionException;
47import java.util.concurrent.locks.Lock;
48import java.util.concurrent.locks.ReentrantLock;
Carmelo Casconefe99be92017-09-11 21:55:54 +020049import java.util.stream.Collectors;
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +020050
51import static com.google.common.collect.Lists.newArrayList;
Andrea Campanella0288c872017-08-07 18:32:51 +020052import static org.onosproject.drivers.p4runtime.P4RuntimeFlowRuleProgrammable.Operation.APPLY;
53import static org.onosproject.drivers.p4runtime.P4RuntimeFlowRuleProgrammable.Operation.REMOVE;
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +020054import static org.onosproject.net.flow.FlowEntry.FlowEntryState.ADDED;
Carmelo Cascone87892e22017-11-13 16:01:29 -080055import static org.onosproject.p4runtime.api.P4RuntimeClient.WriteOperationType.DELETE;
56import static org.onosproject.p4runtime.api.P4RuntimeClient.WriteOperationType.INSERT;
57import static org.onosproject.p4runtime.api.P4RuntimeClient.WriteOperationType.MODIFY;
Frank Wang0e805082017-07-21 14:37:35 +080058
59/**
Carmelo Casconee3a7c742017-09-01 01:25:52 +020060 * Implementation of the flow rule programmable behaviour for P4Runtime.
Frank Wang0e805082017-07-21 14:37:35 +080061 */
Carmelo Casconee3a7c742017-09-01 01:25:52 +020062public class P4RuntimeFlowRuleProgrammable extends AbstractP4RuntimeHandlerBehaviour implements FlowRuleProgrammable {
Frank Wang0e805082017-07-21 14:37:35 +080063
Carmelo Cascone2308e522017-08-25 02:35:12 +020064 /*
65 When updating an existing rule, if true, we issue a DELETE operation before inserting the new one, otherwise we
66 issue a MODIFY operation. This is useful fore devices that do not support MODIFY operations for table entries.
67 */
Carmelo Casconefe99be92017-09-11 21:55:54 +020068 // TODO: make this attribute configurable by child drivers (e.g. BMv2 or Tofino)
Carmelo Cascone2308e522017-08-25 02:35:12 +020069 private boolean deleteEntryBeforeUpdate = true;
70
Carmelo Cascone2308e522017-08-25 02:35:12 +020071 /*
72 If true, we ignore re-installing rules that are already known in the ENTRY_STORE, i.e. same match key and action.
73 */
Carmelo Casconefe99be92017-09-11 21:55:54 +020074 // TODO: can remove this check as soon as the multi-apply-per-same-flow rule bug is fixed.
Carmelo Cascone2308e522017-08-25 02:35:12 +020075 private boolean checkEntryStoreBeforeUpdate = true;
76
Carmelo Casconefe99be92017-09-11 21:55:54 +020077 /*
78 If true, we avoid querying the device and return the content of the ENTRY_STORE.
79 */
Carmelo Casconef2a5ea62017-09-15 01:19:28 +020080 private boolean ignoreDeviceWhenGet = false;
Carmelo Casconefe99be92017-09-11 21:55:54 +020081
Carmelo Cascone7f75be42017-09-07 14:37:02 +020082 /*
83 If true, we read all direct counters of a table with one request. Otherwise, send as many request as the number of
84 table entries.
85 */
86 // TODO: set to true as soon as the feature is implemented in P4Runtime.
87 private boolean readAllDirectCounters = false;
88
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +020089 // Needed to synchronize operations over the same table entry.
Andrea Campanella0288c872017-08-07 18:32:51 +020090 private static final ConcurrentMap<P4RuntimeTableEntryReference, Lock> ENTRY_LOCKS = Maps.newConcurrentMap();
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +020091
92 // TODO: replace with distributed store.
Carmelo Cascone2cad9ef2017-08-01 21:52:07 +020093 // Can reuse old BMv2TableEntryService from ONOS 1.6
Andrea Campanella0288c872017-08-07 18:32:51 +020094 private static final ConcurrentMap<P4RuntimeTableEntryReference, P4RuntimeFlowRuleWrapper> ENTRY_STORE =
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +020095 Maps.newConcurrentMap();
96
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +020097 private PiPipelineModel pipelineModel;
98 private PiPipelineInterpreter interpreter;
Frank Wang0e805082017-07-21 14:37:35 +080099
Carmelo Casconee3a7c742017-09-01 01:25:52 +0200100 @Override
101 protected boolean setupBehaviour() {
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200102
Carmelo Casconee3a7c742017-09-01 01:25:52 +0200103 if (!super.setupBehaviour()) {
Frank Wang0e805082017-07-21 14:37:35 +0800104 return false;
105 }
106
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200107 if (!device.is(PiPipelineInterpreter.class)) {
108 log.warn("Unable to get interpreter of {}", deviceId);
Frank Wang0e805082017-07-21 14:37:35 +0800109 return false;
110 }
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200111 interpreter = device.as(PiPipelineInterpreter.class);
Carmelo Casconee3a7c742017-09-01 01:25:52 +0200112 pipelineModel = pipeconf.pipelineModel();
Frank Wang0e805082017-07-21 14:37:35 +0800113 return true;
114 }
115
116 @Override
117 public Collection<FlowEntry> getFlowEntries() {
118
Carmelo Casconee3a7c742017-09-01 01:25:52 +0200119 if (!setupBehaviour()) {
Frank Wang0e805082017-07-21 14:37:35 +0800120 return Collections.emptyList();
121 }
122
Carmelo Casconefe99be92017-09-11 21:55:54 +0200123 if (ignoreDeviceWhenGet) {
124 return ENTRY_STORE.values().stream()
125 .filter(frWrapper -> frWrapper.rule().deviceId().equals(this.deviceId))
126 .map(frWrapper -> new DefaultFlowEntry(frWrapper.rule(), ADDED, frWrapper.lifeInSeconds(),
127 0, 0))
128 .collect(Collectors.toList());
129 }
130
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200131 ImmutableList.Builder<FlowEntry> resultBuilder = ImmutableList.builder();
132 List<PiTableEntry> inconsistentEntries = Lists.newArrayList();
Frank Wang0e805082017-07-21 14:37:35 +0800133
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200134 for (PiTableModel tableModel : pipelineModel.tables()) {
135
Carmelo Cascone87892e22017-11-13 16:01:29 -0800136 PiTableId piTableId = tableModel.id();
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200137
138 // Only dump tables that are exposed by the interpreter.
139 // The reason is that some P4 targets (e.g. BMv2's simple_switch) use more table than those defined in the
140 // P4 program, to implement other capabilities, e.g. action execution in control flow.
141 if (!interpreter.mapPiTableId(piTableId).isPresent()) {
142 continue; // next table
143 }
144
145 Collection<PiTableEntry> installedEntries;
146 try {
Carmelo Cascone7f75be42017-09-07 14:37:02 +0200147 // TODO: optimize by dumping entries and counters in parallel, from ALL tables with the same request.
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200148 installedEntries = client.dumpTable(piTableId, pipeconf).get();
149 } catch (InterruptedException | ExecutionException e) {
Carmelo Cascone7f75be42017-09-07 14:37:02 +0200150 if (!(e.getCause() instanceof StatusRuntimeException)) {
151 // gRPC errors are logged in the client.
152 log.error("Exception while dumping table {} of {}", piTableId, deviceId, e);
153 }
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200154 return Collections.emptyList();
155 }
156
Carmelo Cascone7f75be42017-09-07 14:37:02 +0200157 Map<PiTableEntry, PiCounterCellData> counterCellMap;
158 try {
159 if (interpreter.mapTableCounter(piTableId).isPresent()) {
160 PiCounterId piCounterId = interpreter.mapTableCounter(piTableId).get();
161 Collection<PiCounterCellData> cellDatas;
162 if (readAllDirectCounters) {
163 cellDatas = client.readAllCounterCells(Collections.singleton(piCounterId), pipeconf).get();
164 } else {
165 Set<PiCounterCellId> cellIds = installedEntries.stream()
Carmelo Cascone87892e22017-11-13 16:01:29 -0800166 .map(entry -> PiCounterCellId.ofDirect(piCounterId, entry))
Carmelo Cascone7f75be42017-09-07 14:37:02 +0200167 .collect(Collectors.toSet());
168 cellDatas = client.readCounterCells(cellIds, pipeconf).get();
169 }
170 counterCellMap = cellDatas.stream()
Carmelo Cascone87892e22017-11-13 16:01:29 -0800171 .collect(Collectors.toMap(c -> (c.cellId()).tableEntry(), c -> c));
Carmelo Cascone7f75be42017-09-07 14:37:02 +0200172 } else {
173 counterCellMap = Collections.emptyMap();
174 }
175 installedEntries = client.dumpTable(piTableId, pipeconf).get();
176 } catch (InterruptedException | ExecutionException e) {
177 if (!(e.getCause() instanceof StatusRuntimeException)) {
178 // gRPC errors are logged in the client.
179 log.error("Exception while reading counters of table {} of {}", piTableId, deviceId, e);
180 }
181 counterCellMap = Collections.emptyMap();
182 }
183
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200184 for (PiTableEntry installedEntry : installedEntries) {
185
Carmelo Cascone7f75be42017-09-07 14:37:02 +0200186 P4RuntimeTableEntryReference entryRef = new P4RuntimeTableEntryReference(deviceId,
187 piTableId,
Carmelo Cascone2308e522017-08-25 02:35:12 +0200188 installedEntry.matchKey());
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200189
Carmelo Cascone7f75be42017-09-07 14:37:02 +0200190 if (!ENTRY_STORE.containsKey(entryRef)) {
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200191 // Inconsistent entry
192 inconsistentEntries.add(installedEntry);
193 continue; // next one.
194 }
195
Carmelo Cascone7f75be42017-09-07 14:37:02 +0200196 P4RuntimeFlowRuleWrapper frWrapper = ENTRY_STORE.get(entryRef);
197
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200198 long bytes = 0L;
199 long packets = 0L;
Carmelo Cascone7f75be42017-09-07 14:37:02 +0200200 if (counterCellMap.containsKey(installedEntry)) {
201 PiCounterCellData counterCellData = counterCellMap.get(installedEntry);
202 bytes = counterCellData.bytes();
203 packets = counterCellData.packets();
204 }
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200205
Carmelo Cascone7f75be42017-09-07 14:37:02 +0200206 resultBuilder.add(new DefaultFlowEntry(frWrapper.rule(),
207 ADDED,
208 frWrapper.lifeInSeconds(),
209 packets,
210 bytes));
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200211 }
212 }
213
214 if (inconsistentEntries.size() > 0) {
215 log.warn("Found {} entries in {} that are not known by table entry service," +
Carmelo Cascone2308e522017-08-25 02:35:12 +0200216 " removing them", inconsistentEntries.size(), deviceId);
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200217 inconsistentEntries.forEach(entry -> log.debug(entry.toString()));
218 // Async remove them.
219 client.writeTableEntries(inconsistentEntries, DELETE, pipeconf);
220 }
221
222 return resultBuilder.build();
Frank Wang0e805082017-07-21 14:37:35 +0800223 }
224
225 @Override
226 public Collection<FlowRule> applyFlowRules(Collection<FlowRule> rules) {
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200227 return processFlowRules(rules, APPLY);
Frank Wang0e805082017-07-21 14:37:35 +0800228 }
229
230 @Override
231 public Collection<FlowRule> removeFlowRules(Collection<FlowRule> rules) {
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200232 return processFlowRules(rules, REMOVE);
Frank Wang0e805082017-07-21 14:37:35 +0800233 }
234
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200235 private Collection<FlowRule> processFlowRules(Collection<FlowRule> rules, Operation operation) {
Frank Wang0e805082017-07-21 14:37:35 +0800236
Carmelo Casconee3a7c742017-09-01 01:25:52 +0200237 if (!setupBehaviour()) {
Frank Wang0e805082017-07-21 14:37:35 +0800238 return Collections.emptyList();
239 }
240
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200241 ImmutableList.Builder<FlowRule> processedFlowRuleListBuilder = ImmutableList.builder();
242
243 // TODO: send write operations in bulk (e.g. all entries to insert, modify or delete).
244 // Instead of calling the client for each one of them.
245
246 for (FlowRule rule : rules) {
247
248 PiTableEntry piTableEntry;
249
Frank Wang0e805082017-07-21 14:37:35 +0800250 try {
Carmelo Cascone87b9b392017-10-02 18:33:20 +0200251 piTableEntry = piTranslationService.translateFlowRule(rule, pipeconf);
252 } catch (PiTranslationService.PiTranslationException e) {
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200253 log.warn("Unable to translate flow rule: {} - {}", e.getMessage(), rule);
254 continue; // next rule
Frank Wang0e805082017-07-21 14:37:35 +0800255 }
Frank Wang0e805082017-07-21 14:37:35 +0800256
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200257 PiTableId tableId = piTableEntry.table();
Andrea Campanella0288c872017-08-07 18:32:51 +0200258 P4RuntimeTableEntryReference entryRef = new P4RuntimeTableEntryReference(deviceId,
Carmelo Cascone2308e522017-08-25 02:35:12 +0200259 tableId, piTableEntry.matchKey());
Frank Wang0e805082017-07-21 14:37:35 +0800260
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200261 Lock lock = ENTRY_LOCKS.computeIfAbsent(entryRef, k -> new ReentrantLock());
262 lock.lock();
Frank Wang0e805082017-07-21 14:37:35 +0800263
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200264 try {
265
Andrea Campanella0288c872017-08-07 18:32:51 +0200266 P4RuntimeFlowRuleWrapper frWrapper = ENTRY_STORE.get(entryRef);
Carmelo Cascone2308e522017-08-25 02:35:12 +0200267 WriteOperationType opType = null;
268 boolean doApply = true;
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200269
Andrea Campanella0288c872017-08-07 18:32:51 +0200270 if (operation == APPLY) {
Carmelo Cascone2308e522017-08-25 02:35:12 +0200271 if (frWrapper == null) {
272 // Entry is first-timer.
273 opType = INSERT;
274 } else {
275 // This match key already exists in the device.
276 if (checkEntryStoreBeforeUpdate &&
277 piTableEntry.action().equals(frWrapper.piTableEntry().action())) {
278 doApply = false;
279 log.debug("Ignoring re-apply of existing entry: {}", piTableEntry);
280 }
281 if (doApply) {
282 if (deleteEntryBeforeUpdate) {
283 // We've seen some strange error when trying to modify existing flow rules.
284 // Remove before re-adding the modified one.
285 try {
286 if (client.writeTableEntries(newArrayList(piTableEntry), DELETE, pipeconf).get()) {
287 frWrapper = null;
288 } else {
289 log.warn("Unable to DELETE table entry (before re-adding) in {}: {}",
290 deviceId, piTableEntry);
291 }
292 } catch (InterruptedException | ExecutionException e) {
293 log.warn("Exception while deleting table entry:", operation.name(), e);
294 }
295 opType = INSERT;
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200296 } else {
Carmelo Cascone2308e522017-08-25 02:35:12 +0200297 opType = MODIFY;
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200298 }
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200299 }
300 }
301 } else {
302 opType = DELETE;
Frank Wang0e805082017-07-21 14:37:35 +0800303 }
Frank Wang0e805082017-07-21 14:37:35 +0800304
Carmelo Cascone2308e522017-08-25 02:35:12 +0200305 if (doApply) {
306 try {
307 if (client.writeTableEntries(newArrayList(piTableEntry), opType, pipeconf).get()) {
308 processedFlowRuleListBuilder.add(rule);
309 if (operation == APPLY) {
310 frWrapper = new P4RuntimeFlowRuleWrapper(rule, piTableEntry,
311 System.currentTimeMillis());
312 } else {
313 frWrapper = null;
314 }
315 } else {
316 log.warn("Unable to {} table entry in {}: {}", opType.name(), deviceId, piTableEntry);
317 }
318 } catch (InterruptedException | ExecutionException e) {
319 log.warn("Exception while performing {} table entry operation:", operation.name(), e);
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200320 }
Carmelo Cascone2308e522017-08-25 02:35:12 +0200321 } else {
322 processedFlowRuleListBuilder.add(rule);
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200323 }
324
325 // Update entryRef binding in table entry service.
326 if (frWrapper != null) {
327 ENTRY_STORE.put(entryRef, frWrapper);
328 } else {
329 ENTRY_STORE.remove(entryRef);
330 }
331
332 } finally {
333 lock.unlock();
334 }
335 }
336
337 return processedFlowRuleListBuilder.build();
338 }
339
340 enum Operation {
341 APPLY, REMOVE
Frank Wang0e805082017-07-21 14:37:35 +0800342 }
Carmelo Cascone87892e22017-11-13 16:01:29 -0800343}