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 | 39c28ca | 2017-11-15 13:03:57 -0800 | [diff] [blame] | 35 | import org.onosproject.net.pi.service.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 | */ |
Yi Tseng | 63a82df | 2017-11-17 16:41:40 -0800 | [diff] [blame] | 80 | // TODO: set to false after bmv2/PI bug fixed |
| 81 | private boolean ignoreDeviceWhenGet = true; |
Carmelo Cascone | fe99be9 | 2017-09-11 21:55:54 +0200 | [diff] [blame] | 82 | |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 83 | /* |
| 84 | If true, we read all direct counters of a table with one request. Otherwise, send as many request as the number of |
| 85 | table entries. |
| 86 | */ |
| 87 | // TODO: set to true as soon as the feature is implemented in P4Runtime. |
| 88 | private boolean readAllDirectCounters = false; |
| 89 | |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 90 | // Needed to synchronize operations over the same table entry. |
Andrea Campanella | 0288c87 | 2017-08-07 18:32:51 +0200 | [diff] [blame] | 91 | private static final ConcurrentMap<P4RuntimeTableEntryReference, Lock> ENTRY_LOCKS = Maps.newConcurrentMap(); |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 92 | |
| 93 | // TODO: replace with distributed store. |
Carmelo Cascone | 2cad9ef | 2017-08-01 21:52:07 +0200 | [diff] [blame] | 94 | // Can reuse old BMv2TableEntryService from ONOS 1.6 |
Andrea Campanella | 0288c87 | 2017-08-07 18:32:51 +0200 | [diff] [blame] | 95 | private static final ConcurrentMap<P4RuntimeTableEntryReference, P4RuntimeFlowRuleWrapper> ENTRY_STORE = |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 96 | Maps.newConcurrentMap(); |
| 97 | |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 98 | private PiPipelineModel pipelineModel; |
| 99 | private PiPipelineInterpreter interpreter; |
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(); |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 114 | return true; |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public Collection<FlowEntry> getFlowEntries() { |
| 119 | |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 120 | if (!setupBehaviour()) { |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 121 | return Collections.emptyList(); |
| 122 | } |
| 123 | |
Carmelo Cascone | fe99be9 | 2017-09-11 21:55:54 +0200 | [diff] [blame] | 124 | if (ignoreDeviceWhenGet) { |
| 125 | return ENTRY_STORE.values().stream() |
| 126 | .filter(frWrapper -> frWrapper.rule().deviceId().equals(this.deviceId)) |
| 127 | .map(frWrapper -> new DefaultFlowEntry(frWrapper.rule(), ADDED, frWrapper.lifeInSeconds(), |
| 128 | 0, 0)) |
| 129 | .collect(Collectors.toList()); |
| 130 | } |
| 131 | |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 132 | ImmutableList.Builder<FlowEntry> resultBuilder = ImmutableList.builder(); |
| 133 | List<PiTableEntry> inconsistentEntries = Lists.newArrayList(); |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 134 | |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 135 | for (PiTableModel tableModel : pipelineModel.tables()) { |
| 136 | |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 137 | PiTableId piTableId = tableModel.id(); |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 138 | |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 139 | Collection<PiTableEntry> installedEntries; |
| 140 | try { |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 141 | // 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] | 142 | installedEntries = client.dumpTable(piTableId, pipeconf).get(); |
| 143 | } catch (InterruptedException | ExecutionException e) { |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 144 | if (!(e.getCause() instanceof StatusRuntimeException)) { |
| 145 | // gRPC errors are logged in the client. |
| 146 | log.error("Exception while dumping table {} of {}", piTableId, deviceId, e); |
| 147 | } |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 148 | return Collections.emptyList(); |
| 149 | } |
| 150 | |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 151 | Map<PiTableEntry, PiCounterCellData> counterCellMap; |
| 152 | try { |
| 153 | if (interpreter.mapTableCounter(piTableId).isPresent()) { |
| 154 | PiCounterId piCounterId = interpreter.mapTableCounter(piTableId).get(); |
| 155 | Collection<PiCounterCellData> cellDatas; |
| 156 | if (readAllDirectCounters) { |
| 157 | cellDatas = client.readAllCounterCells(Collections.singleton(piCounterId), pipeconf).get(); |
| 158 | } else { |
| 159 | Set<PiCounterCellId> cellIds = installedEntries.stream() |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 160 | .map(entry -> PiCounterCellId.ofDirect(piCounterId, entry)) |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 161 | .collect(Collectors.toSet()); |
| 162 | cellDatas = client.readCounterCells(cellIds, pipeconf).get(); |
| 163 | } |
| 164 | counterCellMap = cellDatas.stream() |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 165 | .collect(Collectors.toMap(c -> (c.cellId()).tableEntry(), c -> c)); |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 166 | } else { |
| 167 | counterCellMap = Collections.emptyMap(); |
| 168 | } |
| 169 | installedEntries = client.dumpTable(piTableId, pipeconf).get(); |
| 170 | } catch (InterruptedException | ExecutionException e) { |
| 171 | if (!(e.getCause() instanceof StatusRuntimeException)) { |
| 172 | // gRPC errors are logged in the client. |
| 173 | log.error("Exception while reading counters of table {} of {}", piTableId, deviceId, e); |
| 174 | } |
| 175 | counterCellMap = Collections.emptyMap(); |
| 176 | } |
| 177 | |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 178 | for (PiTableEntry installedEntry : installedEntries) { |
| 179 | |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 180 | P4RuntimeTableEntryReference entryRef = new P4RuntimeTableEntryReference(deviceId, |
| 181 | piTableId, |
Carmelo Cascone | 2308e52 | 2017-08-25 02:35:12 +0200 | [diff] [blame] | 182 | installedEntry.matchKey()); |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 183 | |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 184 | if (!ENTRY_STORE.containsKey(entryRef)) { |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 185 | // Inconsistent entry |
| 186 | inconsistentEntries.add(installedEntry); |
| 187 | continue; // next one. |
| 188 | } |
| 189 | |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 190 | P4RuntimeFlowRuleWrapper frWrapper = ENTRY_STORE.get(entryRef); |
| 191 | |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 192 | long bytes = 0L; |
| 193 | long packets = 0L; |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 194 | if (counterCellMap.containsKey(installedEntry)) { |
| 195 | PiCounterCellData counterCellData = counterCellMap.get(installedEntry); |
| 196 | bytes = counterCellData.bytes(); |
| 197 | packets = counterCellData.packets(); |
| 198 | } |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 199 | |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 200 | resultBuilder.add(new DefaultFlowEntry(frWrapper.rule(), |
| 201 | ADDED, |
| 202 | frWrapper.lifeInSeconds(), |
| 203 | packets, |
| 204 | bytes)); |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | |
| 208 | if (inconsistentEntries.size() > 0) { |
| 209 | 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] | 210 | " removing them", inconsistentEntries.size(), deviceId); |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 211 | inconsistentEntries.forEach(entry -> log.debug(entry.toString())); |
| 212 | // Async remove them. |
| 213 | client.writeTableEntries(inconsistentEntries, DELETE, pipeconf); |
| 214 | } |
| 215 | |
| 216 | return resultBuilder.build(); |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | @Override |
| 220 | public Collection<FlowRule> applyFlowRules(Collection<FlowRule> rules) { |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 221 | return processFlowRules(rules, APPLY); |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | @Override |
| 225 | public Collection<FlowRule> removeFlowRules(Collection<FlowRule> rules) { |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 226 | return processFlowRules(rules, REMOVE); |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 227 | } |
| 228 | |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 229 | private Collection<FlowRule> processFlowRules(Collection<FlowRule> rules, Operation operation) { |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 230 | |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 231 | if (!setupBehaviour()) { |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 232 | return Collections.emptyList(); |
| 233 | } |
| 234 | |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 235 | ImmutableList.Builder<FlowRule> processedFlowRuleListBuilder = ImmutableList.builder(); |
| 236 | |
| 237 | // TODO: send write operations in bulk (e.g. all entries to insert, modify or delete). |
| 238 | // Instead of calling the client for each one of them. |
| 239 | |
| 240 | for (FlowRule rule : rules) { |
| 241 | |
| 242 | PiTableEntry piTableEntry; |
| 243 | |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 244 | try { |
Carmelo Cascone | 1a7e4f9 | 2017-11-20 23:04:02 -0800 | [diff] [blame] | 245 | piTableEntry = piTranslationService.translate(rule, pipeconf); |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 246 | } catch (PiTranslationService.PiTranslationException e) { |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 247 | log.warn("Unable to translate flow rule: {} - {}", e.getMessage(), rule); |
| 248 | continue; // next rule |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 249 | } |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 250 | |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 251 | PiTableId tableId = piTableEntry.table(); |
Andrea Campanella | 0288c87 | 2017-08-07 18:32:51 +0200 | [diff] [blame] | 252 | P4RuntimeTableEntryReference entryRef = new P4RuntimeTableEntryReference(deviceId, |
Carmelo Cascone | 2308e52 | 2017-08-25 02:35:12 +0200 | [diff] [blame] | 253 | tableId, piTableEntry.matchKey()); |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 254 | |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 255 | Lock lock = ENTRY_LOCKS.computeIfAbsent(entryRef, k -> new ReentrantLock()); |
| 256 | lock.lock(); |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 257 | |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 258 | try { |
| 259 | |
Andrea Campanella | 0288c87 | 2017-08-07 18:32:51 +0200 | [diff] [blame] | 260 | P4RuntimeFlowRuleWrapper frWrapper = ENTRY_STORE.get(entryRef); |
Carmelo Cascone | 2308e52 | 2017-08-25 02:35:12 +0200 | [diff] [blame] | 261 | WriteOperationType opType = null; |
| 262 | boolean doApply = true; |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 263 | |
Andrea Campanella | 0288c87 | 2017-08-07 18:32:51 +0200 | [diff] [blame] | 264 | if (operation == APPLY) { |
Carmelo Cascone | 2308e52 | 2017-08-25 02:35:12 +0200 | [diff] [blame] | 265 | if (frWrapper == null) { |
| 266 | // Entry is first-timer. |
| 267 | opType = INSERT; |
| 268 | } else { |
| 269 | // This match key already exists in the device. |
| 270 | if (checkEntryStoreBeforeUpdate && |
| 271 | piTableEntry.action().equals(frWrapper.piTableEntry().action())) { |
| 272 | doApply = false; |
| 273 | log.debug("Ignoring re-apply of existing entry: {}", piTableEntry); |
| 274 | } |
| 275 | if (doApply) { |
| 276 | if (deleteEntryBeforeUpdate) { |
| 277 | // We've seen some strange error when trying to modify existing flow rules. |
| 278 | // Remove before re-adding the modified one. |
| 279 | try { |
| 280 | if (client.writeTableEntries(newArrayList(piTableEntry), DELETE, pipeconf).get()) { |
| 281 | frWrapper = null; |
| 282 | } else { |
| 283 | log.warn("Unable to DELETE table entry (before re-adding) in {}: {}", |
| 284 | deviceId, piTableEntry); |
| 285 | } |
| 286 | } catch (InterruptedException | ExecutionException e) { |
| 287 | log.warn("Exception while deleting table entry:", operation.name(), e); |
| 288 | } |
| 289 | opType = INSERT; |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 290 | } else { |
Carmelo Cascone | 2308e52 | 2017-08-25 02:35:12 +0200 | [diff] [blame] | 291 | opType = MODIFY; |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 292 | } |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | } else { |
| 296 | opType = DELETE; |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 297 | } |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 298 | |
Carmelo Cascone | 2308e52 | 2017-08-25 02:35:12 +0200 | [diff] [blame] | 299 | if (doApply) { |
| 300 | try { |
| 301 | if (client.writeTableEntries(newArrayList(piTableEntry), opType, pipeconf).get()) { |
| 302 | processedFlowRuleListBuilder.add(rule); |
| 303 | if (operation == APPLY) { |
| 304 | frWrapper = new P4RuntimeFlowRuleWrapper(rule, piTableEntry, |
| 305 | System.currentTimeMillis()); |
| 306 | } else { |
| 307 | frWrapper = null; |
| 308 | } |
| 309 | } else { |
| 310 | log.warn("Unable to {} table entry in {}: {}", opType.name(), deviceId, piTableEntry); |
| 311 | } |
| 312 | } catch (InterruptedException | ExecutionException e) { |
| 313 | log.warn("Exception while performing {} table entry operation:", operation.name(), e); |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 314 | } |
Carmelo Cascone | 2308e52 | 2017-08-25 02:35:12 +0200 | [diff] [blame] | 315 | } else { |
| 316 | processedFlowRuleListBuilder.add(rule); |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | // Update entryRef binding in table entry service. |
| 320 | if (frWrapper != null) { |
| 321 | ENTRY_STORE.put(entryRef, frWrapper); |
| 322 | } else { |
| 323 | ENTRY_STORE.remove(entryRef); |
| 324 | } |
| 325 | |
| 326 | } finally { |
| 327 | lock.unlock(); |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | return processedFlowRuleListBuilder.build(); |
| 332 | } |
| 333 | |
| 334 | enum Operation { |
| 335 | APPLY, REMOVE |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 336 | } |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 337 | } |