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