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