blob: bdd53edf6a37ce5a50c1c8ad2830e236afbae723 [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;
22import org.onosproject.net.Device;
23import org.onosproject.net.DeviceId;
24import org.onosproject.net.device.DeviceService;
25import org.onosproject.net.driver.AbstractHandlerBehaviour;
26import org.onosproject.net.flow.DefaultFlowEntry;
27import org.onosproject.net.flow.FlowEntry;
28import org.onosproject.net.flow.FlowRule;
29import org.onosproject.net.flow.FlowRuleProgrammable;
30import org.onosproject.net.pi.model.PiPipeconf;
31import org.onosproject.net.pi.model.PiPipelineInterpreter;
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +020032import org.onosproject.net.pi.model.PiPipelineModel;
33import org.onosproject.net.pi.model.PiTableModel;
Frank Wang0e805082017-07-21 14:37:35 +080034import org.onosproject.net.pi.runtime.PiFlowRuleTranslationService;
35import org.onosproject.net.pi.runtime.PiPipeconfService;
36import org.onosproject.net.pi.runtime.PiTableEntry;
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +020037import org.onosproject.net.pi.runtime.PiTableId;
Frank Wang0e805082017-07-21 14:37:35 +080038import org.onosproject.p4runtime.api.P4RuntimeClient;
39import org.onosproject.p4runtime.api.P4RuntimeClient.WriteOperationType;
40import org.onosproject.p4runtime.api.P4RuntimeController;
Andrea Campanella0288c872017-08-07 18:32:51 +020041import org.onosproject.p4runtime.api.P4RuntimeFlowRuleWrapper;
42import org.onosproject.p4runtime.api.P4RuntimeTableEntryReference;
Frank Wang0e805082017-07-21 14:37:35 +080043import org.slf4j.Logger;
44import org.slf4j.LoggerFactory;
45
46import java.util.Collection;
47import java.util.Collections;
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +020048import java.util.List;
49import java.util.concurrent.ConcurrentMap;
50import java.util.concurrent.ExecutionException;
51import java.util.concurrent.locks.Lock;
52import java.util.concurrent.locks.ReentrantLock;
53
54import static com.google.common.collect.Lists.newArrayList;
Andrea Campanella0288c872017-08-07 18:32:51 +020055import static org.onosproject.drivers.p4runtime.P4RuntimeFlowRuleProgrammable.Operation.APPLY;
56import static org.onosproject.drivers.p4runtime.P4RuntimeFlowRuleProgrammable.Operation.REMOVE;
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +020057import static org.onosproject.net.flow.FlowEntry.FlowEntryState.ADDED;
58import static org.onosproject.p4runtime.api.P4RuntimeClient.WriteOperationType.DELETE;
59import static org.onosproject.p4runtime.api.P4RuntimeClient.WriteOperationType.INSERT;
Frank Wang0e805082017-07-21 14:37:35 +080060
61/**
62 * Implementation of the flow rule programmable behaviour for BMv2.
63 */
Andrea Campanella0288c872017-08-07 18:32:51 +020064public class P4RuntimeFlowRuleProgrammable extends AbstractHandlerBehaviour implements FlowRuleProgrammable {
Frank Wang0e805082017-07-21 14:37:35 +080065
66 private final Logger log = LoggerFactory.getLogger(getClass());
Frank Wang0e805082017-07-21 14:37:35 +080067
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +020068 // Needed to synchronize operations over the same table entry.
Andrea Campanella0288c872017-08-07 18:32:51 +020069 private static final ConcurrentMap<P4RuntimeTableEntryReference, Lock> ENTRY_LOCKS = Maps.newConcurrentMap();
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +020070
71 // TODO: replace with distributed store.
Carmelo Cascone2cad9ef2017-08-01 21:52:07 +020072 // Can reuse old BMv2TableEntryService from ONOS 1.6
Andrea Campanella0288c872017-08-07 18:32:51 +020073 private static final ConcurrentMap<P4RuntimeTableEntryReference, P4RuntimeFlowRuleWrapper> ENTRY_STORE =
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +020074 Maps.newConcurrentMap();
75
76 private DeviceId deviceId;
77 private P4RuntimeClient client;
78 private PiPipeconf pipeconf;
79 private PiPipelineModel pipelineModel;
80 private PiPipelineInterpreter interpreter;
81 private PiFlowRuleTranslationService piFlowRuleTranslationService;
Frank Wang0e805082017-07-21 14:37:35 +080082
83 private boolean init() {
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +020084
Frank Wang0e805082017-07-21 14:37:35 +080085 deviceId = handler().data().deviceId();
86
87 P4RuntimeController controller = handler().get(P4RuntimeController.class);
88 if (!controller.hasClient(deviceId)) {
89 log.warn("Unable to find client for {}, aborting flow rule operation", deviceId);
90 return false;
91 }
92
Frank Wang0e805082017-07-21 14:37:35 +080093 PiPipeconfService piPipeconfService = handler().get(PiPipeconfService.class);
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +020094 if (!piPipeconfService.ofDevice(deviceId).isPresent() ||
95 !piPipeconfService.getPipeconf(piPipeconfService.ofDevice(deviceId).get()).isPresent()) {
Frank Wang0e805082017-07-21 14:37:35 +080096 log.warn("Unable to get the pipeconf of {}", deviceId);
97 return false;
98 }
99
100 DeviceService deviceService = handler().get(DeviceService.class);
101 Device device = deviceService.getDevice(deviceId);
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200102 if (!device.is(PiPipelineInterpreter.class)) {
103 log.warn("Unable to get interpreter of {}", deviceId);
Frank Wang0e805082017-07-21 14:37:35 +0800104 return false;
105 }
106
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200107 client = controller.getClient(deviceId);
108 pipeconf = piPipeconfService.getPipeconf(piPipeconfService.ofDevice(deviceId).get()).get();
109 pipelineModel = pipeconf.pipelineModel();
110 interpreter = device.as(PiPipelineInterpreter.class);
111 piFlowRuleTranslationService = handler().get(PiFlowRuleTranslationService.class);
112
Frank Wang0e805082017-07-21 14:37:35 +0800113 return true;
114 }
115
116 @Override
117 public Collection<FlowEntry> getFlowEntries() {
118
119 if (!init()) {
120 return Collections.emptyList();
121 }
122
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200123 ImmutableList.Builder<FlowEntry> resultBuilder = ImmutableList.builder();
124 List<PiTableEntry> inconsistentEntries = Lists.newArrayList();
Frank Wang0e805082017-07-21 14:37:35 +0800125
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200126 for (PiTableModel tableModel : pipelineModel.tables()) {
127
128 PiTableId piTableId = PiTableId.of(tableModel.name());
129
130 // Only dump tables that are exposed by the interpreter.
131 // The reason is that some P4 targets (e.g. BMv2's simple_switch) use more table than those defined in the
132 // P4 program, to implement other capabilities, e.g. action execution in control flow.
133 if (!interpreter.mapPiTableId(piTableId).isPresent()) {
134 continue; // next table
135 }
136
137 Collection<PiTableEntry> installedEntries;
138 try {
139 installedEntries = client.dumpTable(piTableId, pipeconf).get();
140 } catch (InterruptedException | ExecutionException e) {
141 log.error("Exception while dumping table {} of {}", piTableId, deviceId, e);
142 return Collections.emptyList();
143 }
144
145 for (PiTableEntry installedEntry : installedEntries) {
146
Andrea Campanella0288c872017-08-07 18:32:51 +0200147 P4RuntimeTableEntryReference entryRef = new P4RuntimeTableEntryReference(deviceId, piTableId,
148 installedEntry.matchKey());
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200149
Andrea Campanella0288c872017-08-07 18:32:51 +0200150 P4RuntimeFlowRuleWrapper frWrapper = ENTRY_STORE.get(entryRef);
151
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200152
153 if (frWrapper == null) {
154 // Inconsistent entry
155 inconsistentEntries.add(installedEntry);
156 continue; // next one.
157 }
158
159 // TODO: implement table entry counter retrieval.
160 long bytes = 0L;
161 long packets = 0L;
162
163 FlowEntry entry = new DefaultFlowEntry(frWrapper.rule(), ADDED, frWrapper.lifeInSeconds(),
Andrea Campanella0288c872017-08-07 18:32:51 +0200164 packets, bytes);
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200165 resultBuilder.add(entry);
166 }
167 }
168
169 if (inconsistentEntries.size() > 0) {
170 log.warn("Found {} entries in {} that are not known by table entry service," +
Andrea Campanella0288c872017-08-07 18:32:51 +0200171 " removing them", inconsistentEntries.size(), deviceId);
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200172 inconsistentEntries.forEach(entry -> log.debug(entry.toString()));
173 // Async remove them.
174 client.writeTableEntries(inconsistentEntries, DELETE, pipeconf);
175 }
176
177 return resultBuilder.build();
Frank Wang0e805082017-07-21 14:37:35 +0800178 }
179
180 @Override
181 public Collection<FlowRule> applyFlowRules(Collection<FlowRule> rules) {
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200182 return processFlowRules(rules, APPLY);
Frank Wang0e805082017-07-21 14:37:35 +0800183 }
184
185 @Override
186 public Collection<FlowRule> removeFlowRules(Collection<FlowRule> rules) {
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200187 return processFlowRules(rules, REMOVE);
Frank Wang0e805082017-07-21 14:37:35 +0800188 }
189
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200190 private Collection<FlowRule> processFlowRules(Collection<FlowRule> rules, Operation operation) {
Frank Wang0e805082017-07-21 14:37:35 +0800191
192 if (!init()) {
193 return Collections.emptyList();
194 }
195
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200196 ImmutableList.Builder<FlowRule> processedFlowRuleListBuilder = ImmutableList.builder();
197
198 // TODO: send write operations in bulk (e.g. all entries to insert, modify or delete).
199 // Instead of calling the client for each one of them.
200
201 for (FlowRule rule : rules) {
202
203 PiTableEntry piTableEntry;
204
Frank Wang0e805082017-07-21 14:37:35 +0800205 try {
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200206 piTableEntry = piFlowRuleTranslationService.translate(rule, pipeconf);
Frank Wang0e805082017-07-21 14:37:35 +0800207 } catch (PiFlowRuleTranslationService.PiFlowRuleTranslationException e) {
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200208 log.warn("Unable to translate flow rule: {} - {}", e.getMessage(), rule);
209 continue; // next rule
Frank Wang0e805082017-07-21 14:37:35 +0800210 }
Frank Wang0e805082017-07-21 14:37:35 +0800211
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200212 PiTableId tableId = piTableEntry.table();
Andrea Campanella0288c872017-08-07 18:32:51 +0200213 P4RuntimeTableEntryReference entryRef = new P4RuntimeTableEntryReference(deviceId,
214 tableId, piTableEntry.matchKey());
Frank Wang0e805082017-07-21 14:37:35 +0800215
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200216 Lock lock = ENTRY_LOCKS.computeIfAbsent(entryRef, k -> new ReentrantLock());
217 lock.lock();
Frank Wang0e805082017-07-21 14:37:35 +0800218
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200219 try {
220
Andrea Campanella0288c872017-08-07 18:32:51 +0200221 P4RuntimeFlowRuleWrapper frWrapper = ENTRY_STORE.get(entryRef);
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200222
223 WriteOperationType opType;
Andrea Campanella0288c872017-08-07 18:32:51 +0200224 if (operation == APPLY) {
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200225 opType = INSERT;
226 if (frWrapper != null) {
227 // We've seen some strange error when trying to modify existing flow rules.
228 // Remove before re-adding the modified one.
229 try {
230 if (client.writeTableEntries(newArrayList(piTableEntry), DELETE, pipeconf).get()) {
231 frWrapper = null;
232 } else {
233 log.warn("Unable to DELETE table entry (before re-adding) in {}: {}",
Andrea Campanella0288c872017-08-07 18:32:51 +0200234 deviceId, piTableEntry);
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200235 }
236 } catch (InterruptedException | ExecutionException e) {
237 log.warn("Exception while deleting table entry:", operation.name(), e);
238 }
239 }
240 } else {
241 opType = DELETE;
Frank Wang0e805082017-07-21 14:37:35 +0800242 }
Frank Wang0e805082017-07-21 14:37:35 +0800243
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200244 try {
245 if (client.writeTableEntries(newArrayList(piTableEntry), opType, pipeconf).get()) {
246 processedFlowRuleListBuilder.add(rule);
Andrea Campanella0288c872017-08-07 18:32:51 +0200247 frWrapper = new P4RuntimeFlowRuleWrapper(rule, System.currentTimeMillis());
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200248 } else {
249 log.warn("Unable to {} table entry in {}: {}", opType.name(), deviceId, piTableEntry);
250 }
251 } catch (InterruptedException | ExecutionException e) {
252 log.warn("Exception while performing {} table entry operation:", operation.name(), e);
253 }
254
255 // Update entryRef binding in table entry service.
256 if (frWrapper != null) {
257 ENTRY_STORE.put(entryRef, frWrapper);
258 } else {
259 ENTRY_STORE.remove(entryRef);
260 }
261
262 } finally {
263 lock.unlock();
264 }
265 }
266
267 return processedFlowRuleListBuilder.build();
268 }
269
270 enum Operation {
271 APPLY, REMOVE
Frank Wang0e805082017-07-21 14:37:35 +0800272 }
273}