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; |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 23 | import org.onlab.util.SharedExecutors; |
| 24 | import org.onosproject.drivers.p4runtime.mirror.P4RuntimeTableMirror; |
| 25 | import org.onosproject.drivers.p4runtime.mirror.TimedEntry; |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 26 | import org.onosproject.net.flow.DefaultFlowEntry; |
| 27 | import org.onosproject.net.flow.FlowEntry; |
| 28 | import org.onosproject.net.flow.FlowRule; |
| 29 | import org.onosproject.net.flow.FlowRuleProgrammable; |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 30 | import org.onosproject.net.pi.model.PiCounterId; |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 31 | import org.onosproject.net.pi.model.PiPipelineInterpreter; |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 32 | import org.onosproject.net.pi.model.PiPipelineModel; |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 33 | import org.onosproject.net.pi.model.PiTableId; |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 34 | import org.onosproject.net.pi.model.PiTableModel; |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 35 | import org.onosproject.net.pi.runtime.PiCounterCellData; |
| 36 | import org.onosproject.net.pi.runtime.PiCounterCellId; |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 37 | import org.onosproject.net.pi.runtime.PiTableEntry; |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 38 | import org.onosproject.net.pi.runtime.PiTableEntryHandle; |
| 39 | import org.onosproject.net.pi.service.PiFlowRuleTranslator; |
| 40 | import org.onosproject.net.pi.service.PiTranslatedEntity; |
Carmelo Cascone | 326ad2d | 2017-11-28 18:09:13 -0800 | [diff] [blame] | 41 | import org.onosproject.net.pi.service.PiTranslationException; |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 42 | import org.onosproject.p4runtime.api.P4RuntimeClient.WriteOperationType; |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 43 | |
| 44 | import java.util.Collection; |
| 45 | import java.util.Collections; |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 46 | import java.util.List; |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 47 | import java.util.Map; |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 48 | import java.util.Optional; |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 49 | import java.util.Set; |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 50 | import java.util.concurrent.ConcurrentMap; |
| 51 | import java.util.concurrent.ExecutionException; |
| 52 | import java.util.concurrent.locks.Lock; |
| 53 | import java.util.concurrent.locks.ReentrantLock; |
Carmelo Cascone | fe99be9 | 2017-09-11 21:55:54 +0200 | [diff] [blame] | 54 | import java.util.stream.Collectors; |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 55 | |
| 56 | import static com.google.common.collect.Lists.newArrayList; |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 57 | import static java.util.Collections.singleton; |
Andrea Campanella | 0288c87 | 2017-08-07 18:32:51 +0200 | [diff] [blame] | 58 | import static org.onosproject.drivers.p4runtime.P4RuntimeFlowRuleProgrammable.Operation.APPLY; |
| 59 | import static org.onosproject.drivers.p4runtime.P4RuntimeFlowRuleProgrammable.Operation.REMOVE; |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 60 | import static org.onosproject.net.flow.FlowEntry.FlowEntryState.ADDED; |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 61 | import static org.onosproject.p4runtime.api.P4RuntimeClient.WriteOperationType.DELETE; |
| 62 | import static org.onosproject.p4runtime.api.P4RuntimeClient.WriteOperationType.INSERT; |
| 63 | import static org.onosproject.p4runtime.api.P4RuntimeClient.WriteOperationType.MODIFY; |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 64 | |
| 65 | /** |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 66 | * Implementation of the flow rule programmable behaviour for P4Runtime. |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 67 | */ |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 68 | public class P4RuntimeFlowRuleProgrammable |
| 69 | extends AbstractP4RuntimeHandlerBehaviour |
| 70 | implements FlowRuleProgrammable { |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 71 | |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 72 | // When updating an existing rule, if true, we issue a DELETE operation |
| 73 | // before inserting the new one, otherwise we issue a MODIFY operation. This |
| 74 | // is useful fore devices that do not support MODIFY operations for table |
| 75 | // entries. |
Carmelo Cascone | fe99be9 | 2017-09-11 21:55:54 +0200 | [diff] [blame] | 76 | // 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] | 77 | private boolean deleteEntryBeforeUpdate = true; |
| 78 | |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 79 | // If true, we ignore re-installing rules that are already exists the |
| 80 | // device, i.e. same match key and action. |
| 81 | // FIXME: can remove this check as soon as the multi-apply-per-same-flow rule bug is fixed. |
| 82 | private boolean checkStoreBeforeUpdate = true; |
Carmelo Cascone | 2308e52 | 2017-08-25 02:35:12 +0200 | [diff] [blame] | 83 | |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 84 | // If true, we avoid querying the device and return what's already known by |
| 85 | // the ONOS store. |
Yi Tseng | 63a82df | 2017-11-17 16:41:40 -0800 | [diff] [blame] | 86 | private boolean ignoreDeviceWhenGet = true; |
Carmelo Cascone | fe99be9 | 2017-09-11 21:55:54 +0200 | [diff] [blame] | 87 | |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 88 | /* If true, we read all direct counters of a table with one request. |
| 89 | Otherwise, we send as many requests as the number of table entries. */ |
| 90 | // FIXME: set to true as soon as the feature is implemented in P4Runtime. |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 91 | private boolean readAllDirectCounters = false; |
| 92 | |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 93 | // Needed to synchronize operations over the same table entry. |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 94 | // FIXME: locks should be removed when unused (hint use cache with timeout) |
| 95 | private static final ConcurrentMap<PiTableEntryHandle, Lock> |
| 96 | ENTRY_LOCKS = Maps.newConcurrentMap(); |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 97 | |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 98 | private PiPipelineModel pipelineModel; |
| 99 | private PiPipelineInterpreter interpreter; |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 100 | private P4RuntimeTableMirror tableMirror; |
| 101 | private PiFlowRuleTranslator translator; |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 102 | |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 103 | @Override |
| 104 | protected boolean setupBehaviour() { |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 105 | |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 106 | if (!super.setupBehaviour()) { |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 107 | return false; |
| 108 | } |
| 109 | |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 110 | if (!device.is(PiPipelineInterpreter.class)) { |
| 111 | log.warn("Unable to get interpreter of {}", deviceId); |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 112 | return false; |
| 113 | } |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 114 | interpreter = device.as(PiPipelineInterpreter.class); |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 115 | pipelineModel = pipeconf.pipelineModel(); |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 116 | tableMirror = handler().get(P4RuntimeTableMirror.class); |
| 117 | translator = piTranslationService.flowRuleTranslator(); |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 118 | return true; |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public Collection<FlowEntry> getFlowEntries() { |
| 123 | |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 124 | if (!setupBehaviour()) { |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 125 | return Collections.emptyList(); |
| 126 | } |
| 127 | |
Carmelo Cascone | fe99be9 | 2017-09-11 21:55:54 +0200 | [diff] [blame] | 128 | if (ignoreDeviceWhenGet) { |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 129 | return getFlowEntriesFromMirror(); |
Carmelo Cascone | fe99be9 | 2017-09-11 21:55:54 +0200 | [diff] [blame] | 130 | } |
| 131 | |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 132 | final ImmutableList.Builder<FlowEntry> result = ImmutableList.builder(); |
| 133 | final 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 | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 137 | final PiTableId piTableId = tableModel.id(); |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 138 | |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 139 | // Read table entries. |
| 140 | final Collection<PiTableEntry> installedEntries; |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 141 | try { |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 142 | // TODO: optimize by dumping entries and counters in parallel |
| 143 | // From ALL tables with the same request. |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 144 | installedEntries = client.dumpTable(piTableId, pipeconf).get(); |
| 145 | } catch (InterruptedException | ExecutionException e) { |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 146 | if (!(e.getCause() instanceof StatusRuntimeException)) { |
| 147 | // gRPC errors are logged in the client. |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 148 | log.error("Exception while dumping table {} of {}", |
| 149 | piTableId, deviceId, e); |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 150 | } |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 151 | continue; // next table |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 152 | } |
| 153 | |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 154 | if (installedEntries.size() == 0) { |
| 155 | continue; // next table |
| 156 | } |
| 157 | |
| 158 | // Read table direct counters (if any). |
| 159 | final Map<PiTableEntry, PiCounterCellData> counterCellMap; |
| 160 | if (interpreter.mapTableCounter(piTableId).isPresent()) { |
| 161 | PiCounterId piCounterId = interpreter.mapTableCounter(piTableId).get(); |
| 162 | counterCellMap = readEntryCounters(piCounterId, installedEntries); |
| 163 | } else { |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 164 | counterCellMap = Collections.emptyMap(); |
| 165 | } |
| 166 | |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 167 | // Forge flow entries with counter values. |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 168 | for (PiTableEntry installedEntry : installedEntries) { |
| 169 | |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 170 | final FlowEntry flowEntry = forgeFlowEntry( |
| 171 | installedEntry, counterCellMap.get(installedEntry)); |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 172 | |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 173 | if (flowEntry == null) { |
| 174 | // Entry is on device but unknown to translation service or |
| 175 | // device mirror. Inconsistent. Mark for removal. |
| 176 | // TODO: make this behaviour configurable |
| 177 | // In some cases it's fine for the device to have rules |
| 178 | // that were not installed by us. |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 179 | inconsistentEntries.add(installedEntry); |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 180 | } else { |
| 181 | result.add(flowEntry); |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 182 | } |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 183 | } |
| 184 | } |
| 185 | |
| 186 | if (inconsistentEntries.size() > 0) { |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 187 | // Async clean up inconsistent entries. |
| 188 | SharedExecutors.getSingleThreadExecutor().execute( |
| 189 | () -> cleanUpInconsistentEntries(inconsistentEntries)); |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 190 | } |
| 191 | |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 192 | return result.build(); |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | @Override |
| 196 | public Collection<FlowRule> applyFlowRules(Collection<FlowRule> rules) { |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 197 | return processFlowRules(rules, APPLY); |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | @Override |
| 201 | public Collection<FlowRule> removeFlowRules(Collection<FlowRule> rules) { |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 202 | return processFlowRules(rules, REMOVE); |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 203 | } |
| 204 | |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 205 | private FlowEntry forgeFlowEntry(PiTableEntry entry, |
| 206 | PiCounterCellData cellData) { |
| 207 | final PiTableEntryHandle handle = PiTableEntryHandle |
| 208 | .of(deviceId, entry); |
| 209 | final Optional<PiTranslatedEntity<FlowRule, PiTableEntry>> |
| 210 | translatedEntity = translator.lookup(handle); |
| 211 | final TimedEntry<PiTableEntry> timedEntry = tableMirror.get(handle); |
| 212 | |
| 213 | if (!translatedEntity.isPresent()) { |
| 214 | log.debug("Handle not found in store: {}", handle); |
| 215 | return null; |
| 216 | } |
| 217 | |
| 218 | if (timedEntry == null) { |
| 219 | log.debug("Handle not found in device mirror: {}", handle); |
| 220 | return null; |
| 221 | } |
| 222 | |
| 223 | if (cellData != null) { |
| 224 | return new DefaultFlowEntry(translatedEntity.get().original(), |
| 225 | ADDED, timedEntry.lifeSec(), cellData.bytes(), |
| 226 | cellData.bytes()); |
| 227 | } else { |
| 228 | return new DefaultFlowEntry(translatedEntity.get().original(), |
| 229 | ADDED, timedEntry.lifeSec(), 0, 0); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | private Collection<FlowEntry> getFlowEntriesFromMirror() { |
| 234 | return tableMirror.getAll(deviceId).stream() |
| 235 | .map(timedEntry -> forgeFlowEntry( |
| 236 | timedEntry.entry(), null)) |
| 237 | .collect(Collectors.toList()); |
| 238 | } |
| 239 | |
| 240 | private void cleanUpInconsistentEntries(Collection<PiTableEntry> piEntries) { |
| 241 | log.warn("Found {} entries from {} not on translation store, removing them...", |
| 242 | piEntries.size(), deviceId); |
| 243 | piEntries.forEach(entry -> { |
| 244 | log.debug(entry.toString()); |
| 245 | applyEntry(PiTableEntryHandle.of(deviceId, entry), |
| 246 | entry, null, REMOVE); |
| 247 | }); |
| 248 | } |
| 249 | |
| 250 | private Collection<FlowRule> processFlowRules(Collection<FlowRule> rules, |
| 251 | Operation driverOperation) { |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 252 | |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 253 | if (!setupBehaviour()) { |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 254 | return Collections.emptyList(); |
| 255 | } |
| 256 | |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 257 | final ImmutableList.Builder<FlowRule> result = ImmutableList.builder(); |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 258 | |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 259 | // TODO: send writes in bulk (e.g. all entries to insert, modify or delete). |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 260 | // Instead of calling the client for each one of them. |
| 261 | |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 262 | for (FlowRule ruleToApply : rules) { |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 263 | |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 264 | final PiTableEntry piEntryToApply; |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 265 | try { |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 266 | piEntryToApply = translator.translate(ruleToApply, pipeconf); |
Carmelo Cascone | 326ad2d | 2017-11-28 18:09:13 -0800 | [diff] [blame] | 267 | } catch (PiTranslationException e) { |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 268 | log.warn("Unable to translate flow rule for pipeconf '{}': {} - {}", |
| 269 | pipeconf.id(), e.getMessage(), ruleToApply); |
| 270 | // Next rule. |
| 271 | continue; |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 272 | } |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 273 | |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 274 | final PiTableEntryHandle handle = PiTableEntryHandle |
| 275 | .of(deviceId, piEntryToApply); |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 276 | |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 277 | // Serialize operations over the same match key/table/device ID. |
| 278 | final Lock lock = ENTRY_LOCKS.computeIfAbsent(handle, k -> new ReentrantLock()); |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 279 | lock.lock(); |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 280 | try { |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 281 | if (applyEntry(handle, piEntryToApply, |
| 282 | ruleToApply, driverOperation)) { |
| 283 | result.add(ruleToApply); |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 284 | } |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 285 | } finally { |
| 286 | lock.unlock(); |
| 287 | } |
| 288 | } |
| 289 | |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 290 | return result.build(); |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * Applies the given entry to the device, and returns true if the operation |
| 295 | * was successful, false otherwise. |
| 296 | */ |
| 297 | private boolean applyEntry(PiTableEntryHandle handle, |
| 298 | PiTableEntry piEntryToApply, |
| 299 | FlowRule ruleToApply, |
| 300 | Operation driverOperation) { |
| 301 | // Depending on the driver operation, and if a matching rule exists on |
| 302 | // the device, decide which P4 Runtime write operation to perform for |
| 303 | // this entry. |
| 304 | final TimedEntry<PiTableEntry> piEntryOnDevice = tableMirror.get(handle); |
| 305 | final WriteOperationType p4Operation; |
| 306 | if (driverOperation == APPLY) { |
| 307 | if (piEntryOnDevice == null) { |
| 308 | // Entry is first-timer. |
| 309 | p4Operation = INSERT; |
| 310 | } else { |
| 311 | if (checkStoreBeforeUpdate |
| 312 | && piEntryToApply.action().equals(piEntryOnDevice.entry().action())) { |
| 313 | log.debug("Ignoring re-apply of existing entry: {}", piEntryToApply); |
| 314 | p4Operation = null; |
| 315 | } else if (deleteEntryBeforeUpdate) { |
| 316 | // Some devices return error when updating existing |
| 317 | // entries. If requested, remove entry before |
| 318 | // re-inserting the modified one. |
| 319 | applyEntry(handle, piEntryOnDevice.entry(), null, REMOVE); |
| 320 | p4Operation = INSERT; |
| 321 | } else { |
| 322 | p4Operation = MODIFY; |
| 323 | } |
| 324 | } |
| 325 | } else { |
| 326 | p4Operation = DELETE; |
| 327 | } |
| 328 | |
| 329 | if (p4Operation != null) { |
| 330 | if (writeEntry(piEntryToApply, p4Operation)) { |
| 331 | updateStores(handle, piEntryToApply, ruleToApply, p4Operation); |
| 332 | return true; |
| 333 | } else { |
| 334 | return false; |
| 335 | } |
| 336 | } else { |
| 337 | // If no operation, let's pretend we applied the rule to the device. |
| 338 | return true; |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | /** |
| 343 | * Performs a write operation on the device. |
| 344 | */ |
| 345 | private boolean writeEntry(PiTableEntry entry, |
| 346 | WriteOperationType p4Operation) { |
| 347 | try { |
| 348 | if (client.writeTableEntries( |
| 349 | newArrayList(entry), p4Operation, pipeconf).get()) { |
| 350 | return true; |
| 351 | } else { |
| 352 | log.warn("Unable to {} table entry in {}: {}", |
| 353 | p4Operation.name(), deviceId, entry); |
| 354 | } |
| 355 | } catch (InterruptedException | ExecutionException e) { |
| 356 | log.warn("Exception while performing {} table entry operation:", |
| 357 | p4Operation, e); |
| 358 | } |
| 359 | return false; |
| 360 | } |
| 361 | |
| 362 | private void updateStores(PiTableEntryHandle handle, |
| 363 | PiTableEntry entry, |
| 364 | FlowRule rule, |
| 365 | WriteOperationType p4Operation) { |
| 366 | switch (p4Operation) { |
| 367 | case INSERT: |
| 368 | case MODIFY: |
| 369 | tableMirror.put(handle, entry); |
| 370 | translator.learn(handle, new PiTranslatedEntity<>(rule, entry, handle)); |
| 371 | break; |
| 372 | case DELETE: |
| 373 | tableMirror.remove(handle); |
| 374 | translator.forget(handle); |
| 375 | break; |
| 376 | default: |
| 377 | throw new IllegalArgumentException( |
| 378 | "Unknown operation " + p4Operation.name()); |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | private Map<PiTableEntry, PiCounterCellData> readEntryCounters( |
| 383 | PiCounterId counterId, Collection<PiTableEntry> tableEntries) { |
| 384 | Collection<PiCounterCellData> cellDatas; |
| 385 | try { |
| 386 | if (readAllDirectCounters) { |
| 387 | cellDatas = client.readAllCounterCells( |
| 388 | singleton(counterId), pipeconf).get(); |
| 389 | } else { |
| 390 | Set<PiCounterCellId> cellIds = tableEntries.stream() |
| 391 | .map(entry -> PiCounterCellId.ofDirect(counterId, entry)) |
| 392 | .collect(Collectors.toSet()); |
| 393 | cellDatas = client.readCounterCells(cellIds, pipeconf).get(); |
| 394 | } |
| 395 | return cellDatas.stream() |
| 396 | .collect(Collectors.toMap(c -> c.cellId().tableEntry(), c -> c)); |
| 397 | } catch (InterruptedException | ExecutionException e) { |
| 398 | if (!(e.getCause() instanceof StatusRuntimeException)) { |
| 399 | // gRPC errors are logged in the client. |
| 400 | log.error("Exception while reading counter '{}' from {}: {}", |
| 401 | counterId, deviceId, e); |
| 402 | } |
| 403 | return Collections.emptyMap(); |
| 404 | } |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | enum Operation { |
| 408 | APPLY, REMOVE |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 409 | } |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 410 | } |