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