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