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