Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019-present Open Networking Foundation |
| 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.client; |
| 18 | |
| 19 | import io.grpc.ManagedChannel; |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 20 | import io.grpc.Status; |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 21 | import io.grpc.StatusRuntimeException; |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 22 | import io.grpc.stub.StreamObserver; |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 23 | import org.onosproject.grpc.ctl.AbstractGrpcClient; |
| 24 | import org.onosproject.net.DeviceId; |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 25 | import org.onosproject.net.device.DeviceAgentEvent; |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 26 | import org.onosproject.net.pi.model.PiPipeconf; |
| 27 | import org.onosproject.net.pi.runtime.PiPacketOperation; |
| 28 | import org.onosproject.net.pi.service.PiPipeconfService; |
| 29 | import org.onosproject.p4runtime.api.P4RuntimeClient; |
| 30 | import org.onosproject.p4runtime.api.P4RuntimeClientKey; |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 31 | import org.onosproject.p4runtime.ctl.controller.MasterElectionIdStore; |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 32 | import org.onosproject.p4runtime.ctl.controller.P4RuntimeControllerImpl; |
| 33 | import p4.v1.P4RuntimeGrpc; |
| 34 | import p4.v1.P4RuntimeOuterClass; |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 35 | import p4.v1.P4RuntimeOuterClass.GetForwardingPipelineConfigRequest; |
| 36 | import p4.v1.P4RuntimeOuterClass.GetForwardingPipelineConfigResponse; |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 37 | |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 38 | import java.math.BigInteger; |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 39 | import java.nio.ByteBuffer; |
| 40 | import java.util.concurrent.CompletableFuture; |
| 41 | import java.util.concurrent.TimeUnit; |
| 42 | import java.util.function.Consumer; |
| 43 | |
| 44 | import static com.google.common.base.Preconditions.checkNotNull; |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 45 | import static p4.v1.P4RuntimeOuterClass.GetForwardingPipelineConfigRequest.ResponseType.COOKIE_ONLY; |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 46 | |
| 47 | /** |
| 48 | * Implementation of P4RuntimeClient. |
| 49 | */ |
| 50 | public final class P4RuntimeClientImpl |
| 51 | extends AbstractGrpcClient implements P4RuntimeClient { |
| 52 | |
| 53 | // TODO: consider making timeouts configurable per-device via netcfg |
| 54 | /** |
| 55 | * Timeout in seconds for short/fast RPCs. |
| 56 | */ |
| 57 | static final int SHORT_TIMEOUT_SECONDS = 10; |
| 58 | /** |
| 59 | * Timeout in seconds for RPCs that involve transfer of potentially large |
| 60 | * amount of data. This shoulld be long enough to allow for network delay |
| 61 | * (e.g. to transfer large pipeline binaries over slow network). |
| 62 | */ |
| 63 | static final int LONG_TIMEOUT_SECONDS = 60; |
| 64 | |
| 65 | private final long p4DeviceId; |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 66 | private final P4RuntimeControllerImpl controller; |
| 67 | private final StreamClientImpl streamClient; |
| 68 | private final PipelineConfigClientImpl pipelineConfigClient; |
| 69 | |
| 70 | /** |
| 71 | * Instantiates a new client with the given arguments. |
| 72 | * |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 73 | * @param clientKey client key |
| 74 | * @param channel gRPC managed channel |
| 75 | * @param controller P$Runtime controller instance |
| 76 | * @param pipeconfService pipeconf service instance |
| 77 | * @param masterElectionIdStore master election ID store |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 78 | */ |
| 79 | public P4RuntimeClientImpl(P4RuntimeClientKey clientKey, |
| 80 | ManagedChannel channel, |
| 81 | P4RuntimeControllerImpl controller, |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 82 | PiPipeconfService pipeconfService, |
| 83 | MasterElectionIdStore masterElectionIdStore) { |
| 84 | super(clientKey, channel, true, controller); |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 85 | checkNotNull(channel); |
| 86 | checkNotNull(controller); |
| 87 | checkNotNull(pipeconfService); |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 88 | checkNotNull(masterElectionIdStore); |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 89 | |
| 90 | this.p4DeviceId = clientKey.p4DeviceId(); |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 91 | this.controller = controller; |
| 92 | this.streamClient = new StreamClientImpl( |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 93 | pipeconfService, masterElectionIdStore, this, controller); |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 94 | this.pipelineConfigClient = new PipelineConfigClientImpl(this); |
| 95 | } |
| 96 | |
| 97 | @Override |
Carmelo Cascone | ab5d41e | 2019-03-06 18:02:34 -0800 | [diff] [blame^] | 98 | public void shutdown() { |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 99 | streamClient.closeSession(); |
Carmelo Cascone | ab5d41e | 2019-03-06 18:02:34 -0800 | [diff] [blame^] | 100 | super.shutdown(); |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public CompletableFuture<Boolean> setPipelineConfig( |
| 105 | PiPipeconf pipeconf, ByteBuffer deviceData) { |
| 106 | return pipelineConfigClient.setPipelineConfig(pipeconf, deviceData); |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public CompletableFuture<Boolean> isPipelineConfigSet( |
| 111 | PiPipeconf pipeconf, ByteBuffer deviceData) { |
| 112 | return pipelineConfigClient.isPipelineConfigSet(pipeconf, deviceData); |
| 113 | } |
| 114 | |
| 115 | @Override |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 116 | public CompletableFuture<Boolean> isAnyPipelineConfigSet() { |
| 117 | return pipelineConfigClient.isAnyPipelineConfigSet(); |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | @Override |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 121 | public ReadRequest read(PiPipeconf pipeconf) { |
| 122 | return new ReadRequestImpl(this, pipeconf); |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public boolean isSessionOpen() { |
| 127 | return streamClient.isSessionOpen(); |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public void closeSession() { |
| 132 | streamClient.closeSession(); |
| 133 | } |
| 134 | |
| 135 | @Override |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 136 | public void setMastership(boolean master, BigInteger newElectionId) { |
| 137 | streamClient.setMastership(master, newElectionId); |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public boolean isMaster() { |
| 142 | return streamClient.isMaster(); |
| 143 | } |
| 144 | |
| 145 | @Override |
| 146 | public void packetOut(PiPacketOperation packet, PiPipeconf pipeconf) { |
| 147 | streamClient.packetOut(packet, pipeconf); |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public WriteRequest write(PiPipeconf pipeconf) { |
| 152 | return new WriteRequestImpl(this, pipeconf); |
| 153 | } |
| 154 | |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 155 | @Override |
| 156 | public CompletableFuture<Boolean> probeService() { |
| 157 | final CompletableFuture<Boolean> future = new CompletableFuture<>(); |
| 158 | final StreamObserver<GetForwardingPipelineConfigResponse> responseObserver = |
| 159 | new StreamObserver<GetForwardingPipelineConfigResponse>() { |
| 160 | @Override |
| 161 | public void onNext(GetForwardingPipelineConfigResponse value) { |
| 162 | future.complete(true); |
| 163 | } |
| 164 | |
| 165 | @Override |
| 166 | public void onError(Throwable t) { |
| 167 | if (Status.fromThrowable(t).getCode() == |
| 168 | Status.Code.FAILED_PRECONDITION) { |
| 169 | // Pipeline not set but service is available. |
| 170 | future.complete(true); |
| 171 | } else { |
| 172 | log.debug("", t); |
| 173 | } |
| 174 | future.complete(false); |
| 175 | } |
| 176 | |
| 177 | @Override |
| 178 | public void onCompleted() { |
| 179 | // Ignore, unary call. |
| 180 | } |
| 181 | }; |
| 182 | // Use long timeout as the device might return the full P4 blob |
| 183 | // (e.g. server does not support cookie), over a slow network. |
| 184 | execRpc(s -> s.getForwardingPipelineConfig( |
| 185 | GetForwardingPipelineConfigRequest.newBuilder() |
| 186 | .setDeviceId(p4DeviceId) |
| 187 | .setResponseType(COOKIE_ONLY) |
| 188 | .build(), responseObserver), |
| 189 | SHORT_TIMEOUT_SECONDS); |
| 190 | return future; |
| 191 | } |
| 192 | |
Carmelo Cascone | ab5d41e | 2019-03-06 18:02:34 -0800 | [diff] [blame^] | 193 | @Override |
| 194 | protected void handleRpcError(Throwable throwable, String opDescription) { |
| 195 | if (throwable instanceof StatusRuntimeException) { |
| 196 | checkGrpcException((StatusRuntimeException) throwable); |
| 197 | } |
| 198 | super.handleRpcError(throwable, opDescription); |
| 199 | } |
| 200 | |
| 201 | private void checkGrpcException(StatusRuntimeException sre) { |
| 202 | if (sre.getStatus().getCode() == Status.Code.PERMISSION_DENIED) { |
| 203 | // Notify upper layers that this node is not master. |
| 204 | controller.postEvent(new DeviceAgentEvent( |
| 205 | DeviceAgentEvent.Type.NOT_MASTER, deviceId)); |
| 206 | } |
| 207 | } |
| 208 | |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 209 | /** |
| 210 | * Returns the P4Runtime-internal device ID associated with this client. |
| 211 | * |
| 212 | * @return P4Runtime-internal device ID |
| 213 | */ |
| 214 | long p4DeviceId() { |
| 215 | return this.p4DeviceId; |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Returns the ONOS device ID associated with this client. |
| 220 | * |
| 221 | * @return ONOS device ID |
| 222 | */ |
| 223 | DeviceId deviceId() { |
| 224 | return this.deviceId; |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Returns the election ID last used in a MasterArbitrationUpdate message |
| 229 | * sent by the client to the server. No guarantees are given that this is |
| 230 | * the current election ID associated to the session, nor that the server |
| 231 | * has acknowledged this value as valid. |
| 232 | * |
| 233 | * @return election ID uint128 protobuf message |
| 234 | */ |
| 235 | P4RuntimeOuterClass.Uint128 lastUsedElectionId() { |
| 236 | return streamClient.lastUsedElectionId(); |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Forces execution of an RPC in a cancellable context with the given |
| 241 | * timeout (in seconds). |
| 242 | * |
| 243 | * @param stubConsumer P4Runtime stub consumer |
| 244 | * @param timeout timeout in seconds |
| 245 | */ |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 246 | void execRpc(Consumer<P4RuntimeGrpc.P4RuntimeStub> stubConsumer, |
| 247 | int timeout) { |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 248 | if (log.isTraceEnabled()) { |
| 249 | log.trace("Executing RPC with timeout {} seconds (context deadline {})...", |
| 250 | timeout, context().getDeadline()); |
| 251 | } |
| 252 | runInCancellableContext(() -> stubConsumer.accept( |
| 253 | P4RuntimeGrpc.newStub(channel) |
| 254 | .withDeadlineAfter(timeout, TimeUnit.SECONDS))); |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Forces execution of an RPC in a cancellable context with no timeout. |
| 259 | * |
| 260 | * @param stubConsumer P4Runtime stub consumer |
| 261 | */ |
| 262 | void execRpcNoTimeout(Consumer<P4RuntimeGrpc.P4RuntimeStub> stubConsumer) { |
| 263 | if (log.isTraceEnabled()) { |
| 264 | log.trace("Executing RPC with no timeout (context deadline {})...", |
| 265 | context().getDeadline()); |
| 266 | } |
| 267 | runInCancellableContext(() -> stubConsumer.accept( |
| 268 | P4RuntimeGrpc.newStub(channel))); |
| 269 | } |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 270 | } |