blob: d54ef9754b28da2a7b4f88d4a114f30dc1cc69de [file] [log] [blame]
Carmelo Cascone4c289b72019-01-22 15:30:45 -08001/*
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
17package org.onosproject.p4runtime.ctl.client;
18
19import io.grpc.ManagedChannel;
Carmelo Cascone3977ea42019-02-28 13:43:42 -080020import io.grpc.Status;
Carmelo Cascone4c289b72019-01-22 15:30:45 -080021import io.grpc.StatusRuntimeException;
Carmelo Cascone3977ea42019-02-28 13:43:42 -080022import io.grpc.stub.StreamObserver;
Carmelo Cascone4c289b72019-01-22 15:30:45 -080023import org.onosproject.grpc.ctl.AbstractGrpcClient;
24import org.onosproject.net.DeviceId;
Carmelo Cascone3977ea42019-02-28 13:43:42 -080025import org.onosproject.net.device.DeviceAgentEvent;
Carmelo Cascone4c289b72019-01-22 15:30:45 -080026import org.onosproject.net.pi.model.PiPipeconf;
27import org.onosproject.net.pi.runtime.PiPacketOperation;
28import org.onosproject.net.pi.service.PiPipeconfService;
29import org.onosproject.p4runtime.api.P4RuntimeClient;
30import org.onosproject.p4runtime.api.P4RuntimeClientKey;
Carmelo Cascone3977ea42019-02-28 13:43:42 -080031import org.onosproject.p4runtime.ctl.controller.MasterElectionIdStore;
Carmelo Cascone4c289b72019-01-22 15:30:45 -080032import org.onosproject.p4runtime.ctl.controller.P4RuntimeControllerImpl;
33import p4.v1.P4RuntimeGrpc;
34import p4.v1.P4RuntimeOuterClass;
Carmelo Cascone3977ea42019-02-28 13:43:42 -080035import p4.v1.P4RuntimeOuterClass.GetForwardingPipelineConfigRequest;
36import p4.v1.P4RuntimeOuterClass.GetForwardingPipelineConfigResponse;
Carmelo Cascone4c289b72019-01-22 15:30:45 -080037
Carmelo Cascone3977ea42019-02-28 13:43:42 -080038import java.math.BigInteger;
Carmelo Cascone4c289b72019-01-22 15:30:45 -080039import java.nio.ByteBuffer;
40import java.util.concurrent.CompletableFuture;
41import java.util.concurrent.TimeUnit;
42import java.util.function.Consumer;
43
44import static com.google.common.base.Preconditions.checkNotNull;
Carmelo Cascone3977ea42019-02-28 13:43:42 -080045import static p4.v1.P4RuntimeOuterClass.GetForwardingPipelineConfigRequest.ResponseType.COOKIE_ONLY;
Carmelo Cascone4c289b72019-01-22 15:30:45 -080046
47/**
48 * Implementation of P4RuntimeClient.
49 */
50public 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 Cascone4c289b72019-01-22 15:30:45 -080066 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 Cascone3977ea42019-02-28 13:43:42 -080073 * @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 Cascone4c289b72019-01-22 15:30:45 -080078 */
79 public P4RuntimeClientImpl(P4RuntimeClientKey clientKey,
80 ManagedChannel channel,
81 P4RuntimeControllerImpl controller,
Carmelo Cascone3977ea42019-02-28 13:43:42 -080082 PiPipeconfService pipeconfService,
83 MasterElectionIdStore masterElectionIdStore) {
84 super(clientKey, channel, true, controller);
Carmelo Cascone4c289b72019-01-22 15:30:45 -080085 checkNotNull(channel);
86 checkNotNull(controller);
87 checkNotNull(pipeconfService);
Carmelo Cascone3977ea42019-02-28 13:43:42 -080088 checkNotNull(masterElectionIdStore);
Carmelo Cascone4c289b72019-01-22 15:30:45 -080089
90 this.p4DeviceId = clientKey.p4DeviceId();
Carmelo Cascone4c289b72019-01-22 15:30:45 -080091 this.controller = controller;
92 this.streamClient = new StreamClientImpl(
Carmelo Cascone3977ea42019-02-28 13:43:42 -080093 pipeconfService, masterElectionIdStore, this, controller);
Carmelo Cascone4c289b72019-01-22 15:30:45 -080094 this.pipelineConfigClient = new PipelineConfigClientImpl(this);
95 }
96
97 @Override
Carmelo Casconeab5d41e2019-03-06 18:02:34 -080098 public void shutdown() {
Carmelo Cascone4c289b72019-01-22 15:30:45 -080099 streamClient.closeSession();
Carmelo Casconeab5d41e2019-03-06 18:02:34 -0800100 super.shutdown();
Carmelo Cascone4c289b72019-01-22 15:30:45 -0800101 }
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 Cascone3977ea42019-02-28 13:43:42 -0800116 public CompletableFuture<Boolean> isAnyPipelineConfigSet() {
117 return pipelineConfigClient.isAnyPipelineConfigSet();
Carmelo Cascone4c289b72019-01-22 15:30:45 -0800118 }
119
120 @Override
Carmelo Cascone3977ea42019-02-28 13:43:42 -0800121 public ReadRequest read(PiPipeconf pipeconf) {
122 return new ReadRequestImpl(this, pipeconf);
Carmelo Cascone4c289b72019-01-22 15:30:45 -0800123 }
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 Cascone3977ea42019-02-28 13:43:42 -0800136 public void setMastership(boolean master, BigInteger newElectionId) {
137 streamClient.setMastership(master, newElectionId);
Carmelo Cascone4c289b72019-01-22 15:30:45 -0800138 }
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 Cascone3977ea42019-02-28 13:43:42 -0800155 @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 Casconeab5d41e2019-03-06 18:02:34 -0800193 @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 Cascone4c289b72019-01-22 15:30:45 -0800209 /**
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 Cascone3977ea42019-02-28 13:43:42 -0800246 void execRpc(Consumer<P4RuntimeGrpc.P4RuntimeStub> stubConsumer,
247 int timeout) {
Carmelo Cascone4c289b72019-01-22 15:30:45 -0800248 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 Cascone4c289b72019-01-22 15:30:45 -0800270}