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