Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [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 | |
| 17 | package org.onosproject.p4runtime.ctl; |
| 18 | |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 19 | import com.google.common.collect.HashMultimap; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 20 | import com.google.common.collect.ImmutableMap; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 21 | import com.google.common.collect.Lists; |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 22 | import com.google.common.collect.Maps; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 23 | import com.google.common.collect.Multimap; |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 24 | import com.google.common.collect.Sets; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 25 | import com.google.protobuf.ByteString; |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 26 | import io.grpc.Context; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 27 | import io.grpc.ManagedChannel; |
| 28 | import io.grpc.Status; |
| 29 | import io.grpc.StatusRuntimeException; |
| 30 | import io.grpc.stub.StreamObserver; |
Andrea Campanella | 288b273 | 2017-07-28 14:16:16 +0200 | [diff] [blame] | 31 | import org.onlab.osgi.DefaultServiceDirectory; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 32 | import org.onlab.util.Tools; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 33 | import org.onosproject.net.DeviceId; |
Yi Tseng | 3e7f145 | 2017-10-20 10:31:53 -0700 | [diff] [blame] | 34 | import org.onosproject.net.MastershipRole; |
Andrea Campanella | 432f718 | 2017-07-14 18:43:27 +0200 | [diff] [blame] | 35 | import org.onosproject.net.pi.model.PiPipeconf; |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 36 | import org.onosproject.net.pi.runtime.PiActionGroup; |
| 37 | import org.onosproject.net.pi.runtime.PiActionGroupMember; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 38 | import org.onosproject.net.pi.runtime.PiActionProfileId; |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 39 | import org.onosproject.net.pi.runtime.PiCounterCellData; |
| 40 | import org.onosproject.net.pi.runtime.PiCounterCellId; |
| 41 | import org.onosproject.net.pi.runtime.PiCounterId; |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 42 | import org.onosproject.net.pi.runtime.PiDirectCounterCellId; |
| 43 | import org.onosproject.net.pi.runtime.PiIndirectCounterCellId; |
Andrea Campanella | 432f718 | 2017-07-14 18:43:27 +0200 | [diff] [blame] | 44 | import org.onosproject.net.pi.runtime.PiPacketOperation; |
Andrea Campanella | 288b273 | 2017-07-28 14:16:16 +0200 | [diff] [blame] | 45 | import org.onosproject.net.pi.runtime.PiPipeconfService; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 46 | import org.onosproject.net.pi.runtime.PiTableEntry; |
| 47 | import org.onosproject.net.pi.runtime.PiTableId; |
| 48 | import org.onosproject.p4runtime.api.P4RuntimeClient; |
| 49 | import org.onosproject.p4runtime.api.P4RuntimeEvent; |
| 50 | import org.slf4j.Logger; |
| 51 | import p4.P4RuntimeGrpc; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 52 | import p4.P4RuntimeOuterClass.ActionProfileGroup; |
| 53 | import p4.P4RuntimeOuterClass.ActionProfileMember; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 54 | import p4.P4RuntimeOuterClass.Entity; |
| 55 | import p4.P4RuntimeOuterClass.ForwardingPipelineConfig; |
| 56 | import p4.P4RuntimeOuterClass.MasterArbitrationUpdate; |
| 57 | import p4.P4RuntimeOuterClass.PacketIn; |
| 58 | import p4.P4RuntimeOuterClass.ReadRequest; |
| 59 | import p4.P4RuntimeOuterClass.ReadResponse; |
| 60 | import p4.P4RuntimeOuterClass.SetForwardingPipelineConfigRequest; |
| 61 | import p4.P4RuntimeOuterClass.StreamMessageRequest; |
| 62 | import p4.P4RuntimeOuterClass.StreamMessageResponse; |
| 63 | import p4.P4RuntimeOuterClass.TableEntry; |
Yi Tseng | 3e7f145 | 2017-10-20 10:31:53 -0700 | [diff] [blame] | 64 | import p4.P4RuntimeOuterClass.Uint128; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 65 | import p4.P4RuntimeOuterClass.Update; |
| 66 | import p4.P4RuntimeOuterClass.WriteRequest; |
Andrea Campanella | fc1d34c | 2017-07-18 17:01:41 +0200 | [diff] [blame] | 67 | import p4.config.P4InfoOuterClass.P4Info; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 68 | import p4.tmp.P4Config; |
| 69 | |
Carmelo Cascone | d61fdb3 | 2017-10-30 10:09:57 -0700 | [diff] [blame] | 70 | import java.nio.ByteBuffer; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 71 | import java.util.Collection; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 72 | import java.util.Collections; |
| 73 | import java.util.Iterator; |
| 74 | import java.util.List; |
| 75 | import java.util.Map; |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 76 | import java.util.Objects; |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 77 | import java.util.Set; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 78 | import java.util.concurrent.CompletableFuture; |
Yi Tseng | 3e7f145 | 2017-10-20 10:31:53 -0700 | [diff] [blame] | 79 | import java.util.concurrent.ExecutionException; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 80 | import java.util.concurrent.Executor; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 81 | import java.util.concurrent.ExecutorService; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 82 | import java.util.concurrent.Executors; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 83 | import java.util.concurrent.TimeUnit; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 84 | import java.util.concurrent.locks.Lock; |
| 85 | import java.util.concurrent.locks.ReentrantLock; |
| 86 | import java.util.function.Supplier; |
| 87 | import java.util.stream.Collectors; |
| 88 | import java.util.stream.StreamSupport; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 89 | |
Carmelo Cascone | d61fdb3 | 2017-10-30 10:09:57 -0700 | [diff] [blame] | 90 | import static com.google.common.base.Preconditions.checkNotNull; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 91 | import static org.onlab.util.Tools.groupedThreads; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 92 | import static org.slf4j.LoggerFactory.getLogger; |
Carmelo Cascone | d61fdb3 | 2017-10-30 10:09:57 -0700 | [diff] [blame] | 93 | import static p4.P4RuntimeOuterClass.Entity.EntityCase.ACTION_PROFILE_GROUP; |
| 94 | import static p4.P4RuntimeOuterClass.Entity.EntityCase.ACTION_PROFILE_MEMBER; |
| 95 | import static p4.P4RuntimeOuterClass.Entity.EntityCase.TABLE_ENTRY; |
Andrea Campanella | fc1d34c | 2017-07-18 17:01:41 +0200 | [diff] [blame] | 96 | import static p4.P4RuntimeOuterClass.PacketOut; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 97 | import static p4.P4RuntimeOuterClass.SetForwardingPipelineConfigRequest.Action.VERIFY_AND_COMMIT; |
| 98 | |
| 99 | /** |
| 100 | * Implementation of a P4Runtime client. |
| 101 | */ |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 102 | public final class P4RuntimeClientImpl implements P4RuntimeClient { |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 103 | |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 104 | private static final Map<WriteOperationType, Update.Type> UPDATE_TYPES = ImmutableMap.of( |
| 105 | WriteOperationType.UNSPECIFIED, Update.Type.UNSPECIFIED, |
| 106 | WriteOperationType.INSERT, Update.Type.INSERT, |
| 107 | WriteOperationType.MODIFY, Update.Type.MODIFY, |
| 108 | WriteOperationType.DELETE, Update.Type.DELETE |
| 109 | ); |
Yi Tseng | 3e7f145 | 2017-10-20 10:31:53 -0700 | [diff] [blame] | 110 | private static final String ARBITRATION_RESULT_MASTER = "Is master"; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 111 | |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 112 | private final Logger log = getLogger(getClass()); |
| 113 | |
| 114 | private final DeviceId deviceId; |
Carmelo Cascone | f423bec | 2017-08-30 01:56:25 +0200 | [diff] [blame] | 115 | private final long p4DeviceId; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 116 | private final P4RuntimeControllerImpl controller; |
| 117 | private final P4RuntimeGrpc.P4RuntimeBlockingStub blockingStub; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 118 | private final Context.CancellableContext cancellableContext; |
| 119 | private final ExecutorService executorService; |
| 120 | private final Executor contextExecutor; |
| 121 | private final Lock writeLock = new ReentrantLock(); |
| 122 | private final StreamObserver<StreamMessageRequest> streamRequestObserver; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 123 | |
Yi Tseng | 3e7f145 | 2017-10-20 10:31:53 -0700 | [diff] [blame] | 124 | private Map<Uint128, CompletableFuture<Boolean>> arbitrationUpdateMap = Maps.newConcurrentMap(); |
| 125 | protected Uint128 p4RuntimeElectionId; |
| 126 | |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 127 | /** |
| 128 | * Default constructor. |
| 129 | * |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 130 | * @param deviceId the ONOS device id |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 131 | * @param p4DeviceId the P4 device id |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 132 | * @param channel gRPC channel |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 133 | * @param controller runtime client controller |
| 134 | */ |
Carmelo Cascone | f423bec | 2017-08-30 01:56:25 +0200 | [diff] [blame] | 135 | P4RuntimeClientImpl(DeviceId deviceId, long p4DeviceId, ManagedChannel channel, |
| 136 | P4RuntimeControllerImpl controller) { |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 137 | this.deviceId = deviceId; |
| 138 | this.p4DeviceId = p4DeviceId; |
| 139 | this.controller = controller; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 140 | this.cancellableContext = Context.current().withCancellation(); |
Carmelo Cascone | a966c34 | 2017-07-30 01:56:30 -0400 | [diff] [blame] | 141 | this.executorService = Executors.newFixedThreadPool(15, groupedThreads( |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 142 | "onos/p4runtime-client-" + deviceId.toString(), |
| 143 | deviceId.toString() + "-%d")); |
| 144 | this.contextExecutor = this.cancellableContext.fixedContextExecutor(executorService); |
Carmelo Cascone | 2cad9ef | 2017-08-01 21:52:07 +0200 | [diff] [blame] | 145 | //TODO Investigate deadline or timeout in supplyInContext Method |
| 146 | this.blockingStub = P4RuntimeGrpc.newBlockingStub(channel); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 147 | P4RuntimeGrpc.P4RuntimeStub asyncStub = P4RuntimeGrpc.newStub(channel); |
| 148 | this.streamRequestObserver = asyncStub.streamChannel(new StreamChannelResponseObserver()); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Executes the given task (supplier) in the gRPC context executor of this client, such that if the context is |
| 153 | * cancelled (e.g. client shutdown) the RPC is automatically cancelled. |
| 154 | * <p> |
| 155 | * Important: Tasks submitted in parallel by different threads are forced executed sequentially. |
| 156 | * <p> |
| 157 | */ |
Carmelo Cascone | 2cad9ef | 2017-08-01 21:52:07 +0200 | [diff] [blame] | 158 | private <U> CompletableFuture<U> supplyInContext(Supplier<U> supplier, String opDescription) { |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 159 | return CompletableFuture.supplyAsync(() -> { |
| 160 | // TODO: explore a more relaxed locking strategy. |
| 161 | writeLock.lock(); |
| 162 | try { |
| 163 | return supplier.get(); |
Carmelo Cascone | a966c34 | 2017-07-30 01:56:30 -0400 | [diff] [blame] | 164 | } catch (Throwable ex) { |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 165 | if (ex instanceof StatusRuntimeException) { |
| 166 | log.warn("Unable to execute {} on {}: {}", opDescription, deviceId, ex.toString()); |
| 167 | } else { |
| 168 | log.error("Exception in client of {}, executing {}", deviceId, opDescription, ex); |
| 169 | } |
Carmelo Cascone | a966c34 | 2017-07-30 01:56:30 -0400 | [diff] [blame] | 170 | throw ex; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 171 | } finally { |
| 172 | writeLock.unlock(); |
| 173 | } |
| 174 | }, contextExecutor); |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | @Override |
| 178 | public CompletableFuture<Boolean> initStreamChannel() { |
Carmelo Cascone | 2cad9ef | 2017-08-01 21:52:07 +0200 | [diff] [blame] | 179 | return supplyInContext(this::doInitStreamChannel, "initStreamChannel"); |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 180 | } |
| 181 | |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 182 | @Override |
Carmelo Cascone | d61fdb3 | 2017-10-30 10:09:57 -0700 | [diff] [blame] | 183 | public CompletableFuture<Boolean> setPipelineConfig(PiPipeconf pipeconf, ByteBuffer deviceData) { |
| 184 | return supplyInContext(() -> doSetPipelineConfig(pipeconf, deviceData), "setPipelineConfig"); |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | @Override |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 188 | public CompletableFuture<Boolean> writeTableEntries(Collection<PiTableEntry> piTableEntries, |
| 189 | WriteOperationType opType, PiPipeconf pipeconf) { |
Carmelo Cascone | 2cad9ef | 2017-08-01 21:52:07 +0200 | [diff] [blame] | 190 | return supplyInContext(() -> doWriteTableEntries(piTableEntries, opType, pipeconf), |
| 191 | "writeTableEntries-" + opType.name()); |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | @Override |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 195 | public CompletableFuture<Collection<PiTableEntry>> dumpTable(PiTableId piTableId, PiPipeconf pipeconf) { |
Carmelo Cascone | 2cad9ef | 2017-08-01 21:52:07 +0200 | [diff] [blame] | 196 | return supplyInContext(() -> doDumpTable(piTableId, pipeconf), "dumpTable-" + piTableId); |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | @Override |
Andrea Campanella | 432f718 | 2017-07-14 18:43:27 +0200 | [diff] [blame] | 200 | public CompletableFuture<Boolean> packetOut(PiPacketOperation packet, PiPipeconf pipeconf) { |
Carmelo Cascone | 2cad9ef | 2017-08-01 21:52:07 +0200 | [diff] [blame] | 201 | return supplyInContext(() -> doPacketOut(packet, pipeconf), "packetOut"); |
Andrea Campanella | 432f718 | 2017-07-14 18:43:27 +0200 | [diff] [blame] | 202 | } |
| 203 | |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 204 | @Override |
| 205 | public CompletableFuture<Collection<PiCounterCellData>> readCounterCells(Set<PiCounterCellId> cellIds, |
| 206 | PiPipeconf pipeconf) { |
| 207 | return supplyInContext(() -> doReadCounterCells(cellIds, pipeconf), |
| 208 | "readCounterCells-" + cellIds.hashCode()); |
| 209 | } |
| 210 | |
| 211 | @Override |
| 212 | public CompletableFuture<Collection<PiCounterCellData>> readAllCounterCells(Set<PiCounterId> counterIds, |
| 213 | PiPipeconf pipeconf) { |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 214 | |
| 215 | /* |
| 216 | From p4runtime.proto, the scope of a ReadRequest is defined as follows: |
| 217 | CounterEntry: |
| 218 | - All counter cells for all meters if counter_id = 0 (default). |
| 219 | - All counter cells for given counter_id if index = 0 (default). |
| 220 | DirectCounterEntry: |
| 221 | - All counter cells for all meters if counter_id = 0 (default). |
| 222 | - All counter cells for given counter_id if table_entry.match is empty. |
| 223 | */ |
| 224 | |
| 225 | Set<PiCounterCellId> cellIds = Sets.newHashSet(); |
| 226 | |
| 227 | for (PiCounterId counterId : counterIds) { |
| 228 | switch (counterId.type()) { |
| 229 | case INDIRECT: |
| 230 | cellIds.add(PiIndirectCounterCellId.of(counterId, 0)); |
| 231 | break; |
| 232 | case DIRECT: |
| 233 | cellIds.add(PiDirectCounterCellId.of(counterId, PiTableEntry.EMTPY)); |
| 234 | break; |
| 235 | default: |
| 236 | log.warn("Unrecognized PI counter ID '{}'", counterId.type()); |
| 237 | } |
| 238 | } |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 239 | |
| 240 | return supplyInContext(() -> doReadCounterCells(cellIds, pipeconf), |
| 241 | "readAllCounterCells-" + cellIds.hashCode()); |
| 242 | } |
| 243 | |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 244 | @Override |
| 245 | public CompletableFuture<Boolean> writeActionGroupMembers(PiActionGroup group, |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 246 | WriteOperationType opType, |
| 247 | PiPipeconf pipeconf) { |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 248 | return supplyInContext(() -> doWriteActionGroupMembers(group, opType, pipeconf), |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 249 | "writeActionGroupMembers-" + opType.name()); |
| 250 | } |
| 251 | |
| 252 | @Override |
| 253 | public CompletableFuture<Boolean> writeActionGroup(PiActionGroup group, |
| 254 | WriteOperationType opType, |
| 255 | PiPipeconf pipeconf) { |
| 256 | return supplyInContext(() -> doWriteActionGroup(group, opType, pipeconf), |
| 257 | "writeActionGroup-" + opType.name()); |
| 258 | } |
| 259 | |
| 260 | @Override |
| 261 | public CompletableFuture<Collection<PiActionGroup>> dumpGroups(PiActionProfileId actionProfileId, |
| 262 | PiPipeconf pipeconf) { |
| 263 | return supplyInContext(() -> doDumpGroups(actionProfileId, pipeconf), |
| 264 | "dumpGroups-" + actionProfileId.id()); |
| 265 | } |
| 266 | |
Yi Tseng | 3e7f145 | 2017-10-20 10:31:53 -0700 | [diff] [blame] | 267 | @Override |
| 268 | public CompletableFuture<Boolean> sendMasterArbitrationUpdate() { |
| 269 | return supplyInContext(this::doArbitrationUpdate, "arbitrationUpdate"); |
| 270 | } |
| 271 | |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 272 | /* Blocking method implementations below */ |
| 273 | |
Yi Tseng | 3e7f145 | 2017-10-20 10:31:53 -0700 | [diff] [blame] | 274 | private boolean doArbitrationUpdate() { |
| 275 | CompletableFuture<Boolean> result = new CompletableFuture<>(); |
| 276 | // TODO: currently we use 64-bit Long type for election id, should |
| 277 | // we use 128-bit ? |
| 278 | long nextElectId = controller.getNewMasterElectionId(); |
| 279 | Uint128 newElectionId = Uint128.newBuilder() |
| 280 | .setLow(nextElectId) |
| 281 | .build(); |
| 282 | MasterArbitrationUpdate arbitrationUpdate = MasterArbitrationUpdate.newBuilder() |
| 283 | .setDeviceId(p4DeviceId) |
| 284 | .setElectionId(newElectionId) |
| 285 | .build(); |
| 286 | StreamMessageRequest requestMsg = StreamMessageRequest.newBuilder() |
| 287 | .setArbitration(arbitrationUpdate) |
| 288 | .build(); |
| 289 | log.debug("Sending arbitration update to {} with election id {}...", |
| 290 | deviceId, newElectionId); |
| 291 | arbitrationUpdateMap.put(newElectionId, result); |
| 292 | try { |
| 293 | streamRequestObserver.onNext(requestMsg); |
| 294 | return result.get(); |
| 295 | } catch (InterruptedException | ExecutionException | StatusRuntimeException e) { |
| 296 | log.warn("Arbitration update failed for {} due to {}", deviceId, e); |
| 297 | arbitrationUpdateMap.remove(newElectionId); |
| 298 | return false; |
| 299 | } |
| 300 | } |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 301 | private boolean doInitStreamChannel() { |
| 302 | // To listen for packets and other events, we need to start the RPC. |
| 303 | // Here we do it by sending a master arbitration update. |
Yi Tseng | 3e7f145 | 2017-10-20 10:31:53 -0700 | [diff] [blame] | 304 | return doArbitrationUpdate(); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 305 | } |
| 306 | |
Carmelo Cascone | d61fdb3 | 2017-10-30 10:09:57 -0700 | [diff] [blame] | 307 | private boolean doSetPipelineConfig(PiPipeconf pipeconf, ByteBuffer deviceData) { |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 308 | |
Carmelo Cascone | d61fdb3 | 2017-10-30 10:09:57 -0700 | [diff] [blame] | 309 | log.info("Setting pipeline config for {} to {}...", deviceId, pipeconf.id()); |
| 310 | |
| 311 | checkNotNull(deviceData, "deviceData cannot be null"); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 312 | |
| 313 | P4Info p4Info = PipeconfHelper.getP4Info(pipeconf); |
| 314 | if (p4Info == null) { |
| 315 | // Problem logged by PipeconfHelper. |
| 316 | return false; |
| 317 | } |
| 318 | |
Carmelo Cascone | d61fdb3 | 2017-10-30 10:09:57 -0700 | [diff] [blame] | 319 | P4Config.P4DeviceConfig p4DeviceConfigMsg = P4Config.P4DeviceConfig |
| 320 | .newBuilder() |
| 321 | .setExtras(P4Config.P4DeviceConfig.Extras.getDefaultInstance()) |
| 322 | .setReassign(true) |
| 323 | .setDeviceData(ByteString.copyFrom(deviceData)) |
| 324 | .build(); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 325 | |
Carmelo Cascone | d61fdb3 | 2017-10-30 10:09:57 -0700 | [diff] [blame] | 326 | ForwardingPipelineConfig pipelineConfig = ForwardingPipelineConfig |
Andrea Campanella | 0288c87 | 2017-08-07 18:32:51 +0200 | [diff] [blame] | 327 | .newBuilder() |
| 328 | .setDeviceId(p4DeviceId) |
Carmelo Cascone | d61fdb3 | 2017-10-30 10:09:57 -0700 | [diff] [blame] | 329 | .setP4Info(p4Info) |
| 330 | .setP4DeviceConfig(p4DeviceConfigMsg.toByteString()) |
| 331 | .build(); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 332 | |
| 333 | SetForwardingPipelineConfigRequest request = SetForwardingPipelineConfigRequest |
| 334 | .newBuilder() |
Yi Tseng | 3e7f145 | 2017-10-20 10:31:53 -0700 | [diff] [blame] | 335 | .setElectionId(p4RuntimeElectionId) |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 336 | .setAction(VERIFY_AND_COMMIT) |
Carmelo Cascone | d61fdb3 | 2017-10-30 10:09:57 -0700 | [diff] [blame] | 337 | .addConfigs(pipelineConfig) |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 338 | .build(); |
| 339 | |
| 340 | try { |
| 341 | this.blockingStub.setForwardingPipelineConfig(request); |
Carmelo Cascone | d61fdb3 | 2017-10-30 10:09:57 -0700 | [diff] [blame] | 342 | return true; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 343 | } catch (StatusRuntimeException ex) { |
| 344 | log.warn("Unable to set pipeline config for {}: {}", deviceId, ex.getMessage()); |
| 345 | return false; |
| 346 | } |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | private boolean doWriteTableEntries(Collection<PiTableEntry> piTableEntries, WriteOperationType opType, |
| 350 | PiPipeconf pipeconf) { |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 351 | WriteRequest.Builder writeRequestBuilder = WriteRequest.newBuilder(); |
| 352 | |
| 353 | Collection<Update> updateMsgs = TableEntryEncoder.encode(piTableEntries, pipeconf) |
| 354 | .stream() |
| 355 | .map(tableEntryMsg -> |
| 356 | Update.newBuilder() |
| 357 | .setEntity(Entity.newBuilder() |
| 358 | .setTableEntry(tableEntryMsg) |
| 359 | .build()) |
| 360 | .setType(UPDATE_TYPES.get(opType)) |
| 361 | .build()) |
| 362 | .collect(Collectors.toList()); |
| 363 | |
| 364 | if (updateMsgs.size() == 0) { |
| 365 | return true; |
| 366 | } |
| 367 | |
| 368 | writeRequestBuilder |
| 369 | .setDeviceId(p4DeviceId) |
Yi Tseng | 3e7f145 | 2017-10-20 10:31:53 -0700 | [diff] [blame] | 370 | .setElectionId(p4RuntimeElectionId) |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 371 | .addAllUpdates(updateMsgs) |
| 372 | .build(); |
| 373 | |
| 374 | try { |
| 375 | blockingStub.write(writeRequestBuilder.build()); |
| 376 | return true; |
| 377 | } catch (StatusRuntimeException e) { |
| 378 | log.warn("Unable to write table entries ({}): {}", opType, e.getMessage()); |
| 379 | return false; |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | private Collection<PiTableEntry> doDumpTable(PiTableId piTableId, PiPipeconf pipeconf) { |
| 384 | |
Carmelo Cascone | 9f00770 | 2017-08-24 13:30:51 +0200 | [diff] [blame] | 385 | log.debug("Dumping table {} from {} (pipeconf {})...", piTableId, deviceId, pipeconf.id()); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 386 | |
| 387 | P4InfoBrowser browser = PipeconfHelper.getP4InfoBrowser(pipeconf); |
| 388 | int tableId; |
| 389 | try { |
Carmelo Cascone | cb0a49c | 2017-10-03 14:32:23 +0200 | [diff] [blame] | 390 | tableId = browser.tables().getByName(piTableId.id()).getPreamble().getId(); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 391 | } catch (P4InfoBrowser.NotFoundException e) { |
| 392 | log.warn("Unable to dump table: {}", e.getMessage()); |
| 393 | return Collections.emptyList(); |
| 394 | } |
| 395 | |
| 396 | ReadRequest requestMsg = ReadRequest.newBuilder() |
| 397 | .setDeviceId(p4DeviceId) |
| 398 | .addEntities(Entity.newBuilder() |
| 399 | .setTableEntry(TableEntry.newBuilder() |
| 400 | .setTableId(tableId) |
| 401 | .build()) |
| 402 | .build()) |
| 403 | .build(); |
| 404 | |
| 405 | Iterator<ReadResponse> responses; |
| 406 | try { |
| 407 | responses = blockingStub.read(requestMsg); |
| 408 | } catch (StatusRuntimeException e) { |
| 409 | log.warn("Unable to dump table: {}", e.getMessage()); |
| 410 | return Collections.emptyList(); |
| 411 | } |
| 412 | |
| 413 | Iterable<ReadResponse> responseIterable = () -> responses; |
| 414 | List<TableEntry> tableEntryMsgs = StreamSupport |
| 415 | .stream(responseIterable.spliterator(), false) |
| 416 | .map(ReadResponse::getEntitiesList) |
| 417 | .flatMap(List::stream) |
| 418 | .filter(entity -> entity.getEntityCase() == TABLE_ENTRY) |
| 419 | .map(Entity::getTableEntry) |
| 420 | .collect(Collectors.toList()); |
| 421 | |
Carmelo Cascone | 9f00770 | 2017-08-24 13:30:51 +0200 | [diff] [blame] | 422 | log.debug("Retrieved {} entries from table {} on {}...", tableEntryMsgs.size(), piTableId, deviceId); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 423 | |
| 424 | return TableEntryEncoder.decode(tableEntryMsgs, pipeconf); |
| 425 | } |
| 426 | |
Andrea Campanella | fc1d34c | 2017-07-18 17:01:41 +0200 | [diff] [blame] | 427 | private boolean doPacketOut(PiPacketOperation packet, PiPipeconf pipeconf) { |
| 428 | try { |
| 429 | //encode the PiPacketOperation into a PacketOut |
| 430 | PacketOut packetOut = PacketIOCodec.encodePacketOut(packet, pipeconf); |
| 431 | |
| 432 | //Build the request |
| 433 | StreamMessageRequest packetOutRequest = StreamMessageRequest |
| 434 | .newBuilder().setPacket(packetOut).build(); |
| 435 | |
| 436 | //Send the request |
| 437 | streamRequestObserver.onNext(packetOutRequest); |
| 438 | |
| 439 | } catch (P4InfoBrowser.NotFoundException e) { |
| 440 | log.error("Cant find expected metadata in p4Info file. {}", e.getMessage()); |
| 441 | log.debug("Exception", e); |
| 442 | return false; |
| 443 | } |
| 444 | return true; |
| 445 | } |
| 446 | |
Carmelo Cascone | a966c34 | 2017-07-30 01:56:30 -0400 | [diff] [blame] | 447 | private void doPacketIn(PacketIn packetInMsg) { |
| 448 | |
| 449 | // Retrieve the pipeconf for this client's device. |
| 450 | PiPipeconfService pipeconfService = DefaultServiceDirectory.getService(PiPipeconfService.class); |
| 451 | if (pipeconfService == null) { |
| 452 | throw new IllegalStateException("PiPipeconfService is null. Can't handle packet in."); |
| 453 | } |
| 454 | final PiPipeconf pipeconf; |
| 455 | if (pipeconfService.ofDevice(deviceId).isPresent() && |
| 456 | pipeconfService.getPipeconf(pipeconfService.ofDevice(deviceId).get()).isPresent()) { |
| 457 | pipeconf = pipeconfService.getPipeconf(pipeconfService.ofDevice(deviceId).get()).get(); |
| 458 | } else { |
| 459 | log.warn("Unable to get pipeconf of {}. Can't handle packet in", deviceId); |
| 460 | return; |
| 461 | } |
| 462 | // Decode packet message and post event. |
Carmelo Cascone | 2cad9ef | 2017-08-01 21:52:07 +0200 | [diff] [blame] | 463 | PiPacketOperation packetOperation = PacketIOCodec.decodePacketIn(packetInMsg, pipeconf); |
| 464 | DefaultPacketIn packetInEventSubject = new DefaultPacketIn(deviceId, packetOperation); |
| 465 | P4RuntimeEvent event = new P4RuntimeEvent(P4RuntimeEvent.Type.PACKET_IN, packetInEventSubject); |
Carmelo Cascone | a966c34 | 2017-07-30 01:56:30 -0400 | [diff] [blame] | 466 | log.debug("Received packet in: {}", event); |
| 467 | controller.postEvent(event); |
| 468 | } |
| 469 | |
| 470 | private void doArbitrationUpdateFromDevice(MasterArbitrationUpdate arbitrationMsg) { |
Yi Tseng | 3e7f145 | 2017-10-20 10:31:53 -0700 | [diff] [blame] | 471 | log.debug("Received arbitration update from {}: {}", deviceId, arbitrationMsg); |
Carmelo Cascone | a966c34 | 2017-07-30 01:56:30 -0400 | [diff] [blame] | 472 | |
Yi Tseng | 3e7f145 | 2017-10-20 10:31:53 -0700 | [diff] [blame] | 473 | Uint128 electionId = arbitrationMsg.getElectionId(); |
| 474 | CompletableFuture<Boolean> mastershipFeature = arbitrationUpdateMap.remove(electionId); |
| 475 | |
| 476 | if (mastershipFeature == null) { |
| 477 | log.warn("Can't find completable future of election id {}", electionId); |
| 478 | return; |
| 479 | } |
| 480 | |
| 481 | this.p4RuntimeElectionId = electionId; |
| 482 | int statusCode = arbitrationMsg.getStatus().getCode(); |
| 483 | MastershipRole arbitrationRole; |
| 484 | // arbitration update success |
| 485 | |
| 486 | if (statusCode == Status.OK.getCode().value()) { |
| 487 | mastershipFeature.complete(true); |
| 488 | arbitrationRole = MastershipRole.MASTER; |
| 489 | } else { |
| 490 | mastershipFeature.complete(false); |
| 491 | arbitrationRole = MastershipRole.STANDBY; |
| 492 | } |
| 493 | |
| 494 | DefaultArbitration arbitrationEventSubject = new DefaultArbitration(arbitrationRole, electionId); |
| 495 | P4RuntimeEvent event = new P4RuntimeEvent(P4RuntimeEvent.Type.ARBITRATION, |
| 496 | arbitrationEventSubject); |
| 497 | controller.postEvent(event); |
Carmelo Cascone | a966c34 | 2017-07-30 01:56:30 -0400 | [diff] [blame] | 498 | } |
| 499 | |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 500 | private Collection<PiCounterCellData> doReadCounterCells(Collection<PiCounterCellId> cellIds, PiPipeconf pipeconf) { |
| 501 | |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 502 | // We use this map to remember the original PI counter IDs of the returned response. |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 503 | final Map<Integer, PiCounterId> counterIdMap = Maps.newHashMap(); |
| 504 | |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 505 | final ReadRequest request = ReadRequest.newBuilder() |
| 506 | .setDeviceId(p4DeviceId) |
| 507 | .addAllEntities(CounterEntryCodec.encodePiCounterCellIds(cellIds, counterIdMap, pipeconf)) |
| 508 | .build(); |
| 509 | |
| 510 | if (request.getEntitiesList().size() == 0) { |
| 511 | return Collections.emptyList(); |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 512 | } |
| 513 | |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 514 | final Iterable<ReadResponse> responses; |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 515 | try { |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 516 | responses = () -> blockingStub.read(request); |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 517 | } catch (StatusRuntimeException e) { |
| 518 | log.warn("Unable to read counters: {}", e.getMessage()); |
| 519 | return Collections.emptyList(); |
| 520 | } |
| 521 | |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 522 | List<Entity> entities = StreamSupport.stream(responses.spliterator(), false) |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 523 | .map(ReadResponse::getEntitiesList) |
| 524 | .flatMap(List::stream) |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 525 | .collect(Collectors.toList()); |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 526 | |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 527 | return CounterEntryCodec.decodeCounterEntities(entities, counterIdMap, pipeconf); |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 528 | } |
| 529 | |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 530 | private boolean doWriteActionGroupMembers(PiActionGroup group, WriteOperationType opType, PiPipeconf pipeconf) { |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 531 | final Collection<ActionProfileMember> actionProfileMembers = Lists.newArrayList(); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 532 | try { |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 533 | for (PiActionGroupMember member : group.members()) { |
| 534 | actionProfileMembers.add(ActionProfileMemberEncoder.encode(group, member, pipeconf)); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 535 | } |
| 536 | } catch (EncodeException | P4InfoBrowser.NotFoundException e) { |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 537 | log.warn("Unable to write ({}) group members: {}", opType, e.getMessage()); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 538 | return false; |
| 539 | } |
| 540 | |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 541 | final Collection<Update> updateMsgs = actionProfileMembers.stream() |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 542 | .map(actionProfileMember -> |
| 543 | Update.newBuilder() |
| 544 | .setEntity(Entity.newBuilder() |
| 545 | .setActionProfileMember(actionProfileMember) |
| 546 | .build()) |
| 547 | .setType(UPDATE_TYPES.get(opType)) |
| 548 | .build()) |
| 549 | .collect(Collectors.toList()); |
| 550 | |
| 551 | if (updateMsgs.size() == 0) { |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 552 | // Nothing to update. |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 553 | return true; |
| 554 | } |
| 555 | |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 556 | WriteRequest writeRequestMsg = WriteRequest.newBuilder() |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 557 | .setDeviceId(p4DeviceId) |
Yi Tseng | 3e7f145 | 2017-10-20 10:31:53 -0700 | [diff] [blame] | 558 | .setElectionId(p4RuntimeElectionId) |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 559 | .addAllUpdates(updateMsgs) |
| 560 | .build(); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 561 | try { |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 562 | blockingStub.write(writeRequestMsg); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 563 | return true; |
| 564 | } catch (StatusRuntimeException e) { |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 565 | log.warn("Unable to write ({}) group members: {}", opType, e.getMessage()); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 566 | return false; |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | private Collection<PiActionGroup> doDumpGroups(PiActionProfileId piActionProfileId, PiPipeconf pipeconf) { |
| 571 | log.debug("Dumping groups from action profile {} from {} (pipeconf {})...", |
| 572 | piActionProfileId.id(), deviceId, pipeconf.id()); |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 573 | |
| 574 | final P4InfoBrowser browser = PipeconfHelper.getP4InfoBrowser(pipeconf); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 575 | if (browser == null) { |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 576 | log.warn("Unable to get a P4Info browser for pipeconf {}, aborting dump action profile", pipeconf); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 577 | return Collections.emptySet(); |
| 578 | } |
| 579 | |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 580 | final int actionProfileId; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 581 | try { |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 582 | actionProfileId = browser |
| 583 | .actionProfiles() |
Carmelo Cascone | cb0a49c | 2017-10-03 14:32:23 +0200 | [diff] [blame] | 584 | .getByName(piActionProfileId.id()) |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 585 | .getPreamble() |
| 586 | .getId(); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 587 | } catch (P4InfoBrowser.NotFoundException e) { |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 588 | log.warn("Unable to dump groups: {}", e.getMessage()); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 589 | return Collections.emptySet(); |
| 590 | } |
| 591 | |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 592 | // Prepare read request to read all groups from the given action profile. |
| 593 | final ReadRequest groupRequestMsg = ReadRequest.newBuilder() |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 594 | .setDeviceId(p4DeviceId) |
| 595 | .addEntities(Entity.newBuilder() |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 596 | .setActionProfileGroup( |
| 597 | ActionProfileGroup.newBuilder() |
| 598 | .setActionProfileId(actionProfileId) |
| 599 | .build()) |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 600 | .build()) |
| 601 | .build(); |
| 602 | |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 603 | // Read groups. |
| 604 | final Iterator<ReadResponse> groupResponses; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 605 | try { |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 606 | groupResponses = blockingStub.read(groupRequestMsg); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 607 | } catch (StatusRuntimeException e) { |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 608 | log.warn("Unable dump groups from action profile '{}': {}", piActionProfileId.id(), e.getMessage()); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 609 | return Collections.emptySet(); |
| 610 | } |
| 611 | |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 612 | final List<ActionProfileGroup> groupMsgs = Tools.stream(() -> groupResponses) |
| 613 | .map(ReadResponse::getEntitiesList) |
| 614 | .flatMap(List::stream) |
| 615 | .filter(entity -> entity.getEntityCase() == ACTION_PROFILE_GROUP) |
| 616 | .map(Entity::getActionProfileGroup) |
| 617 | .collect(Collectors.toList()); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 618 | |
| 619 | log.debug("Retrieved {} groups from action profile {} on {}...", |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 620 | groupMsgs.size(), piActionProfileId.id(), deviceId); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 621 | |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 622 | // Returned groups contain only a minimal description of their members. |
| 623 | // We need to issue a new request to get the full description of each member. |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 624 | |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 625 | // Keep a map of all member IDs for each group ID, will need it later. |
| 626 | final Multimap<Integer, Integer> groupIdToMemberIdsMap = HashMultimap.create(); |
| 627 | groupMsgs.forEach(g -> groupIdToMemberIdsMap.putAll( |
| 628 | g.getGroupId(), |
| 629 | g.getMembersList().stream() |
| 630 | .map(ActionProfileGroup.Member::getMemberId) |
| 631 | .collect(Collectors.toList()))); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 632 | |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 633 | // Prepare one big read request to read all members in one shot. |
| 634 | final Set<Entity> entityMsgs = groupMsgs.stream() |
| 635 | .flatMap(g -> g.getMembersList().stream()) |
| 636 | .map(ActionProfileGroup.Member::getMemberId) |
| 637 | // Prevent issuing many read requests for the same member. |
| 638 | .distinct() |
| 639 | .map(id -> ActionProfileMember.newBuilder() |
| 640 | .setActionProfileId(actionProfileId) |
| 641 | .setMemberId(id) |
| 642 | .build()) |
| 643 | .map(m -> Entity.newBuilder() |
| 644 | .setActionProfileMember(m) |
| 645 | .build()) |
| 646 | .collect(Collectors.toSet()); |
| 647 | final ReadRequest memberRequestMsg = ReadRequest.newBuilder().setDeviceId(p4DeviceId) |
| 648 | .addAllEntities(entityMsgs) |
| 649 | .build(); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 650 | |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 651 | // Read members. |
| 652 | final Iterator<ReadResponse> memberResponses; |
| 653 | try { |
| 654 | memberResponses = blockingStub.read(memberRequestMsg); |
| 655 | } catch (StatusRuntimeException e) { |
| 656 | log.warn("Unable to read members from action profile {}: {}", piActionProfileId, e.getMessage()); |
| 657 | return Collections.emptyList(); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 658 | } |
| 659 | |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 660 | final Multimap<Integer, ActionProfileMember> groupIdToMembersMap = HashMultimap.create(); |
| 661 | Tools.stream(() -> memberResponses) |
| 662 | .map(ReadResponse::getEntitiesList) |
| 663 | .flatMap(List::stream) |
| 664 | .filter(e -> e.getEntityCase() == ACTION_PROFILE_MEMBER) |
| 665 | .map(Entity::getActionProfileMember) |
| 666 | .forEach(member -> groupIdToMemberIdsMap.asMap() |
| 667 | // Get all group IDs that contain this member. |
| 668 | .entrySet() |
| 669 | .stream() |
| 670 | .filter(entry -> entry.getValue().contains(member.getMemberId())) |
| 671 | .map(Map.Entry::getKey) |
| 672 | .forEach(gid -> groupIdToMembersMap.put(gid, member))); |
| 673 | |
| 674 | log.debug("Retrieved {} group members from action profile {} on {}...", |
| 675 | groupIdToMembersMap.size(), piActionProfileId.id(), deviceId); |
| 676 | |
| 677 | return groupMsgs.stream() |
| 678 | .map(groupMsg -> { |
| 679 | try { |
| 680 | return ActionProfileGroupEncoder.decode(groupMsg, |
| 681 | groupIdToMembersMap.get(groupMsg.getGroupId()), |
| 682 | pipeconf); |
| 683 | } catch (P4InfoBrowser.NotFoundException | EncodeException e) { |
| 684 | log.warn("Unable to decode group: {}\n {}", e.getMessage(), groupMsg); |
| 685 | return null; |
| 686 | } |
| 687 | }) |
| 688 | .filter(Objects::nonNull) |
| 689 | .collect(Collectors.toList()); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | private boolean doWriteActionGroup(PiActionGroup group, WriteOperationType opType, PiPipeconf pipeconf) { |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 693 | final ActionProfileGroup actionProfileGroup; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 694 | try { |
| 695 | actionProfileGroup = ActionProfileGroupEncoder.encode(group, pipeconf); |
| 696 | } catch (EncodeException | P4InfoBrowser.NotFoundException e) { |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 697 | log.warn("Unable to encode group: {}", e.getMessage()); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 698 | return false; |
| 699 | } |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 700 | |
| 701 | final WriteRequest writeRequestMsg = WriteRequest.newBuilder() |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 702 | .setDeviceId(p4DeviceId) |
Yi Tseng | 3e7f145 | 2017-10-20 10:31:53 -0700 | [diff] [blame] | 703 | .setElectionId(p4RuntimeElectionId) |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 704 | .addUpdates(Update.newBuilder() |
| 705 | .setEntity(Entity.newBuilder() |
| 706 | .setActionProfileGroup(actionProfileGroup) |
| 707 | .build()) |
| 708 | .setType(UPDATE_TYPES.get(opType)) |
| 709 | .build()) |
| 710 | .build(); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 711 | try { |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 712 | blockingStub.write(writeRequestMsg); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 713 | return true; |
| 714 | } catch (StatusRuntimeException e) { |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 715 | log.warn("Unable to write groups ({}): {}", opType, e.getMessage()); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 716 | return false; |
| 717 | } |
| 718 | } |
| 719 | |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 720 | /** |
| 721 | * Returns the internal P4 device ID associated with this client. |
| 722 | * |
| 723 | * @return P4 device ID |
| 724 | */ |
Carmelo Cascone | f423bec | 2017-08-30 01:56:25 +0200 | [diff] [blame] | 725 | public long p4DeviceId() { |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 726 | return p4DeviceId; |
| 727 | } |
| 728 | |
| 729 | /** |
| 730 | * For testing purpose only. TODO: remove before release. |
| 731 | * |
| 732 | * @return blocking stub |
| 733 | */ |
| 734 | public P4RuntimeGrpc.P4RuntimeBlockingStub blockingStub() { |
| 735 | return this.blockingStub; |
| 736 | } |
| 737 | |
Andrea Campanella | fc1d34c | 2017-07-18 17:01:41 +0200 | [diff] [blame] | 738 | |
Andrea Campanella | 432f718 | 2017-07-14 18:43:27 +0200 | [diff] [blame] | 739 | @Override |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 740 | public void shutdown() { |
| 741 | |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 742 | log.info("Shutting down client for {}...", deviceId); |
| 743 | |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 744 | writeLock.lock(); |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 745 | try { |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 746 | if (streamRequestObserver != null) { |
| 747 | streamRequestObserver.onCompleted(); |
| 748 | cancellableContext.cancel(new InterruptedException("Requested client shutdown")); |
| 749 | } |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 750 | |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 751 | this.executorService.shutdown(); |
| 752 | try { |
| 753 | executorService.awaitTermination(5, TimeUnit.SECONDS); |
| 754 | } catch (InterruptedException e) { |
| 755 | log.warn("Executor service didn't shutdown in time."); |
| 756 | } |
| 757 | } finally { |
| 758 | writeLock.unlock(); |
| 759 | } |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 760 | } |
| 761 | |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 762 | /** |
| 763 | * Handles messages received from the device on the stream channel. |
| 764 | */ |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 765 | private class StreamChannelResponseObserver implements StreamObserver<StreamMessageResponse> { |
| 766 | |
| 767 | @Override |
| 768 | public void onNext(StreamMessageResponse message) { |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 769 | executorService.submit(() -> doNext(message)); |
| 770 | } |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 771 | |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 772 | private void doNext(StreamMessageResponse message) { |
Carmelo Cascone | a966c34 | 2017-07-30 01:56:30 -0400 | [diff] [blame] | 773 | try { |
Andrea Campanella | 0288c87 | 2017-08-07 18:32:51 +0200 | [diff] [blame] | 774 | log.debug("Received message on stream channel from {}: {}", deviceId, message.getUpdateCase()); |
Carmelo Cascone | a966c34 | 2017-07-30 01:56:30 -0400 | [diff] [blame] | 775 | switch (message.getUpdateCase()) { |
| 776 | case PACKET: |
| 777 | // Packet-in |
| 778 | doPacketIn(message.getPacket()); |
Andrea Campanella | 288b273 | 2017-07-28 14:16:16 +0200 | [diff] [blame] | 779 | return; |
Carmelo Cascone | a966c34 | 2017-07-30 01:56:30 -0400 | [diff] [blame] | 780 | case ARBITRATION: |
| 781 | doArbitrationUpdateFromDevice(message.getArbitration()); |
| 782 | return; |
| 783 | default: |
| 784 | log.warn("Unrecognized stream message from {}: {}", deviceId, message.getUpdateCase()); |
| 785 | } |
| 786 | } catch (Throwable ex) { |
| 787 | log.error("Exception while processing stream channel message from {}", deviceId, ex); |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 788 | } |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 789 | } |
| 790 | |
| 791 | @Override |
| 792 | public void onError(Throwable throwable) { |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 793 | log.warn("Error on stream channel for {}: {}", deviceId, Status.fromThrowable(throwable)); |
| 794 | // FIXME: we might want to recreate the channel. |
| 795 | // In general, we want to be robust against any transient error and, if the channel is open, make sure the |
| 796 | // stream channel is always on. |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 797 | } |
| 798 | |
| 799 | @Override |
| 800 | public void onCompleted() { |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 801 | log.warn("Stream channel for {} has completed", deviceId); |
| 802 | // FIXME: same concern as before. |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 803 | } |
| 804 | } |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 805 | } |