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