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