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; |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 21 | import com.google.common.collect.Maps; |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 22 | import org.onosproject.net.flow.DefaultFlowEntry; |
| 23 | import org.onosproject.net.flow.FlowEntry; |
| 24 | import org.onosproject.net.flow.FlowRule; |
| 25 | import org.onosproject.net.flow.FlowRuleProgrammable; |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 26 | import org.onosproject.net.pi.model.PiPipelineInterpreter; |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 27 | import org.onosproject.net.pi.model.PiPipelineModel; |
| 28 | import org.onosproject.net.pi.model.PiTableModel; |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 29 | import org.onosproject.net.pi.runtime.PiFlowRuleTranslationService; |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 30 | import org.onosproject.net.pi.runtime.PiTableEntry; |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 31 | import org.onosproject.net.pi.runtime.PiTableId; |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 32 | import org.onosproject.p4runtime.api.P4RuntimeClient.WriteOperationType; |
Andrea Campanella | 0288c87 | 2017-08-07 18:32:51 +0200 | [diff] [blame] | 33 | import org.onosproject.p4runtime.api.P4RuntimeFlowRuleWrapper; |
| 34 | import org.onosproject.p4runtime.api.P4RuntimeTableEntryReference; |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 35 | |
| 36 | import java.util.Collection; |
| 37 | import java.util.Collections; |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 38 | import java.util.List; |
| 39 | import java.util.concurrent.ConcurrentMap; |
| 40 | import java.util.concurrent.ExecutionException; |
| 41 | import java.util.concurrent.locks.Lock; |
| 42 | import java.util.concurrent.locks.ReentrantLock; |
| 43 | |
| 44 | import static com.google.common.collect.Lists.newArrayList; |
Andrea Campanella | 0288c87 | 2017-08-07 18:32:51 +0200 | [diff] [blame] | 45 | import static org.onosproject.drivers.p4runtime.P4RuntimeFlowRuleProgrammable.Operation.APPLY; |
| 46 | import static org.onosproject.drivers.p4runtime.P4RuntimeFlowRuleProgrammable.Operation.REMOVE; |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 47 | import static org.onosproject.net.flow.FlowEntry.FlowEntryState.ADDED; |
Carmelo Cascone | 2308e52 | 2017-08-25 02:35:12 +0200 | [diff] [blame] | 48 | import static org.onosproject.p4runtime.api.P4RuntimeClient.WriteOperationType.*; |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 49 | |
| 50 | /** |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame^] | 51 | * Implementation of the flow rule programmable behaviour for P4Runtime. |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 52 | */ |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame^] | 53 | public class P4RuntimeFlowRuleProgrammable extends AbstractP4RuntimeHandlerBehaviour implements FlowRuleProgrammable { |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 54 | |
Carmelo Cascone | 2308e52 | 2017-08-25 02:35:12 +0200 | [diff] [blame] | 55 | // TODO: make this attribute configurable by child drivers (e.g. BMv2 or Tofino) |
| 56 | /* |
| 57 | When updating an existing rule, if true, we issue a DELETE operation before inserting the new one, otherwise we |
| 58 | issue a MODIFY operation. This is useful fore devices that do not support MODIFY operations for table entries. |
| 59 | */ |
| 60 | private boolean deleteEntryBeforeUpdate = true; |
| 61 | |
| 62 | // TODO: can remove this check as soon as the multi-apply-per-same-flow rule bug is fixed. |
| 63 | /* |
| 64 | If true, we ignore re-installing rules that are already known in the ENTRY_STORE, i.e. same match key and action. |
| 65 | */ |
| 66 | private boolean checkEntryStoreBeforeUpdate = true; |
| 67 | |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 68 | // Needed to synchronize operations over the same table entry. |
Andrea Campanella | 0288c87 | 2017-08-07 18:32:51 +0200 | [diff] [blame] | 69 | private static final ConcurrentMap<P4RuntimeTableEntryReference, Lock> ENTRY_LOCKS = Maps.newConcurrentMap(); |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 70 | |
| 71 | // TODO: replace with distributed store. |
Carmelo Cascone | 2cad9ef | 2017-08-01 21:52:07 +0200 | [diff] [blame] | 72 | // Can reuse old BMv2TableEntryService from ONOS 1.6 |
Andrea Campanella | 0288c87 | 2017-08-07 18:32:51 +0200 | [diff] [blame] | 73 | private static final ConcurrentMap<P4RuntimeTableEntryReference, P4RuntimeFlowRuleWrapper> ENTRY_STORE = |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 74 | Maps.newConcurrentMap(); |
| 75 | |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 76 | private PiPipelineModel pipelineModel; |
| 77 | private PiPipelineInterpreter interpreter; |
| 78 | private PiFlowRuleTranslationService piFlowRuleTranslationService; |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 79 | |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame^] | 80 | @Override |
| 81 | protected boolean setupBehaviour() { |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 82 | |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame^] | 83 | if (!super.setupBehaviour()) { |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 84 | return false; |
| 85 | } |
| 86 | |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 87 | if (!device.is(PiPipelineInterpreter.class)) { |
| 88 | log.warn("Unable to get interpreter of {}", deviceId); |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 89 | return false; |
| 90 | } |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 91 | interpreter = device.as(PiPipelineInterpreter.class); |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame^] | 92 | pipelineModel = pipeconf.pipelineModel(); |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 93 | piFlowRuleTranslationService = handler().get(PiFlowRuleTranslationService.class); |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 94 | return true; |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public Collection<FlowEntry> getFlowEntries() { |
| 99 | |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame^] | 100 | if (!setupBehaviour()) { |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 101 | return Collections.emptyList(); |
| 102 | } |
| 103 | |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 104 | ImmutableList.Builder<FlowEntry> resultBuilder = ImmutableList.builder(); |
| 105 | List<PiTableEntry> inconsistentEntries = Lists.newArrayList(); |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 106 | |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 107 | for (PiTableModel tableModel : pipelineModel.tables()) { |
| 108 | |
| 109 | PiTableId piTableId = PiTableId.of(tableModel.name()); |
| 110 | |
| 111 | // Only dump tables that are exposed by the interpreter. |
| 112 | // The reason is that some P4 targets (e.g. BMv2's simple_switch) use more table than those defined in the |
| 113 | // P4 program, to implement other capabilities, e.g. action execution in control flow. |
| 114 | if (!interpreter.mapPiTableId(piTableId).isPresent()) { |
| 115 | continue; // next table |
| 116 | } |
| 117 | |
| 118 | Collection<PiTableEntry> installedEntries; |
| 119 | try { |
| 120 | installedEntries = client.dumpTable(piTableId, pipeconf).get(); |
| 121 | } catch (InterruptedException | ExecutionException e) { |
| 122 | log.error("Exception while dumping table {} of {}", piTableId, deviceId, e); |
| 123 | return Collections.emptyList(); |
| 124 | } |
| 125 | |
| 126 | for (PiTableEntry installedEntry : installedEntries) { |
| 127 | |
Andrea Campanella | 0288c87 | 2017-08-07 18:32:51 +0200 | [diff] [blame] | 128 | P4RuntimeTableEntryReference entryRef = new P4RuntimeTableEntryReference(deviceId, piTableId, |
Carmelo Cascone | 2308e52 | 2017-08-25 02:35:12 +0200 | [diff] [blame] | 129 | installedEntry.matchKey()); |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 130 | |
Andrea Campanella | 0288c87 | 2017-08-07 18:32:51 +0200 | [diff] [blame] | 131 | P4RuntimeFlowRuleWrapper frWrapper = ENTRY_STORE.get(entryRef); |
| 132 | |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 133 | |
| 134 | if (frWrapper == null) { |
| 135 | // Inconsistent entry |
| 136 | inconsistentEntries.add(installedEntry); |
| 137 | continue; // next one. |
| 138 | } |
| 139 | |
| 140 | // TODO: implement table entry counter retrieval. |
| 141 | long bytes = 0L; |
| 142 | long packets = 0L; |
| 143 | |
| 144 | FlowEntry entry = new DefaultFlowEntry(frWrapper.rule(), ADDED, frWrapper.lifeInSeconds(), |
Carmelo Cascone | 2308e52 | 2017-08-25 02:35:12 +0200 | [diff] [blame] | 145 | packets, bytes); |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 146 | resultBuilder.add(entry); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | if (inconsistentEntries.size() > 0) { |
| 151 | log.warn("Found {} entries in {} that are not known by table entry service," + |
Carmelo Cascone | 2308e52 | 2017-08-25 02:35:12 +0200 | [diff] [blame] | 152 | " removing them", inconsistentEntries.size(), deviceId); |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 153 | inconsistentEntries.forEach(entry -> log.debug(entry.toString())); |
| 154 | // Async remove them. |
| 155 | client.writeTableEntries(inconsistentEntries, DELETE, pipeconf); |
| 156 | } |
| 157 | |
| 158 | return resultBuilder.build(); |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | @Override |
| 162 | public Collection<FlowRule> applyFlowRules(Collection<FlowRule> rules) { |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 163 | return processFlowRules(rules, APPLY); |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | @Override |
| 167 | public Collection<FlowRule> removeFlowRules(Collection<FlowRule> rules) { |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 168 | return processFlowRules(rules, REMOVE); |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 169 | } |
| 170 | |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 171 | private Collection<FlowRule> processFlowRules(Collection<FlowRule> rules, Operation operation) { |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 172 | |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame^] | 173 | if (!setupBehaviour()) { |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 174 | return Collections.emptyList(); |
| 175 | } |
| 176 | |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 177 | ImmutableList.Builder<FlowRule> processedFlowRuleListBuilder = ImmutableList.builder(); |
| 178 | |
| 179 | // TODO: send write operations in bulk (e.g. all entries to insert, modify or delete). |
| 180 | // Instead of calling the client for each one of them. |
| 181 | |
| 182 | for (FlowRule rule : rules) { |
| 183 | |
| 184 | PiTableEntry piTableEntry; |
| 185 | |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 186 | try { |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 187 | piTableEntry = piFlowRuleTranslationService.translate(rule, pipeconf); |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 188 | } catch (PiFlowRuleTranslationService.PiFlowRuleTranslationException e) { |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 189 | log.warn("Unable to translate flow rule: {} - {}", e.getMessage(), rule); |
| 190 | continue; // next rule |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 191 | } |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 192 | |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 193 | PiTableId tableId = piTableEntry.table(); |
Andrea Campanella | 0288c87 | 2017-08-07 18:32:51 +0200 | [diff] [blame] | 194 | P4RuntimeTableEntryReference entryRef = new P4RuntimeTableEntryReference(deviceId, |
Carmelo Cascone | 2308e52 | 2017-08-25 02:35:12 +0200 | [diff] [blame] | 195 | tableId, piTableEntry.matchKey()); |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 196 | |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 197 | Lock lock = ENTRY_LOCKS.computeIfAbsent(entryRef, k -> new ReentrantLock()); |
| 198 | lock.lock(); |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 199 | |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 200 | try { |
| 201 | |
Andrea Campanella | 0288c87 | 2017-08-07 18:32:51 +0200 | [diff] [blame] | 202 | P4RuntimeFlowRuleWrapper frWrapper = ENTRY_STORE.get(entryRef); |
Carmelo Cascone | 2308e52 | 2017-08-25 02:35:12 +0200 | [diff] [blame] | 203 | WriteOperationType opType = null; |
| 204 | boolean doApply = true; |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 205 | |
Andrea Campanella | 0288c87 | 2017-08-07 18:32:51 +0200 | [diff] [blame] | 206 | if (operation == APPLY) { |
Carmelo Cascone | 2308e52 | 2017-08-25 02:35:12 +0200 | [diff] [blame] | 207 | if (frWrapper == null) { |
| 208 | // Entry is first-timer. |
| 209 | opType = INSERT; |
| 210 | } else { |
| 211 | // This match key already exists in the device. |
| 212 | if (checkEntryStoreBeforeUpdate && |
| 213 | piTableEntry.action().equals(frWrapper.piTableEntry().action())) { |
| 214 | doApply = false; |
| 215 | log.debug("Ignoring re-apply of existing entry: {}", piTableEntry); |
| 216 | } |
| 217 | if (doApply) { |
| 218 | if (deleteEntryBeforeUpdate) { |
| 219 | // We've seen some strange error when trying to modify existing flow rules. |
| 220 | // Remove before re-adding the modified one. |
| 221 | try { |
| 222 | if (client.writeTableEntries(newArrayList(piTableEntry), DELETE, pipeconf).get()) { |
| 223 | frWrapper = null; |
| 224 | } else { |
| 225 | log.warn("Unable to DELETE table entry (before re-adding) in {}: {}", |
| 226 | deviceId, piTableEntry); |
| 227 | } |
| 228 | } catch (InterruptedException | ExecutionException e) { |
| 229 | log.warn("Exception while deleting table entry:", operation.name(), e); |
| 230 | } |
| 231 | opType = INSERT; |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 232 | } else { |
Carmelo Cascone | 2308e52 | 2017-08-25 02:35:12 +0200 | [diff] [blame] | 233 | opType = MODIFY; |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 234 | } |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | } else { |
| 238 | opType = DELETE; |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 239 | } |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 240 | |
Carmelo Cascone | 2308e52 | 2017-08-25 02:35:12 +0200 | [diff] [blame] | 241 | if (doApply) { |
| 242 | try { |
| 243 | if (client.writeTableEntries(newArrayList(piTableEntry), opType, pipeconf).get()) { |
| 244 | processedFlowRuleListBuilder.add(rule); |
| 245 | if (operation == APPLY) { |
| 246 | frWrapper = new P4RuntimeFlowRuleWrapper(rule, piTableEntry, |
| 247 | System.currentTimeMillis()); |
| 248 | } else { |
| 249 | frWrapper = null; |
| 250 | } |
| 251 | } else { |
| 252 | log.warn("Unable to {} table entry in {}: {}", opType.name(), deviceId, piTableEntry); |
| 253 | } |
| 254 | } catch (InterruptedException | ExecutionException e) { |
| 255 | log.warn("Exception while performing {} table entry operation:", operation.name(), e); |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 256 | } |
Carmelo Cascone | 2308e52 | 2017-08-25 02:35:12 +0200 | [diff] [blame] | 257 | } else { |
| 258 | processedFlowRuleListBuilder.add(rule); |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | // Update entryRef binding in table entry service. |
| 262 | if (frWrapper != null) { |
| 263 | ENTRY_STORE.put(entryRef, frWrapper); |
| 264 | } else { |
| 265 | ENTRY_STORE.remove(entryRef); |
| 266 | } |
| 267 | |
| 268 | } finally { |
| 269 | lock.unlock(); |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | return processedFlowRuleListBuilder.build(); |
| 274 | } |
| 275 | |
| 276 | enum Operation { |
| 277 | APPLY, REMOVE |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 278 | } |
| 279 | } |