Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-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.drivers.p4runtime; |
| 18 | |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 19 | import io.grpc.StatusRuntimeException; |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 20 | import org.onosproject.net.Device; |
| 21 | import org.onosproject.net.DeviceId; |
| 22 | import org.onosproject.net.device.DeviceService; |
| 23 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 24 | import org.onosproject.net.pi.model.PiPipeconf; |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 25 | import org.onosproject.net.pi.model.PiPipeconfId; |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 26 | import org.onosproject.net.pi.model.PiPipelineInterpreter; |
Carmelo Cascone | 39c28ca | 2017-11-15 13:03:57 -0800 | [diff] [blame] | 27 | import org.onosproject.net.pi.service.PiPipeconfService; |
| 28 | import org.onosproject.net.pi.service.PiTranslationService; |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 29 | import org.onosproject.p4runtime.api.P4RuntimeClient; |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame^] | 30 | import org.onosproject.p4runtime.api.P4RuntimeClientKey; |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 31 | import org.onosproject.p4runtime.api.P4RuntimeController; |
| 32 | import org.slf4j.Logger; |
| 33 | import org.slf4j.LoggerFactory; |
| 34 | |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 35 | import java.util.concurrent.CompletableFuture; |
| 36 | import java.util.concurrent.ExecutionException; |
| 37 | import java.util.concurrent.TimeUnit; |
| 38 | import java.util.concurrent.TimeoutException; |
| 39 | |
Carmelo Cascone | 3da671a | 2018-02-12 10:43:35 -0800 | [diff] [blame] | 40 | import static com.google.common.base.Preconditions.checkNotNull; |
| 41 | |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 42 | /** |
| 43 | * Abstract implementation of a behaviour handler for a P4Runtime device. |
| 44 | */ |
| 45 | public class AbstractP4RuntimeHandlerBehaviour extends AbstractHandlerBehaviour { |
| 46 | |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 47 | // Default timeout in seconds for device operations. |
| 48 | private static final String DEVICE_REQ_TIMEOUT = "deviceRequestTimeout"; |
| 49 | private static final int DEFAULT_DEVICE_REQ_TIMEOUT = 60; |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 50 | |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 51 | public static final String P4RUNTIME_SERVER_ADDR_KEY = "p4runtime_ip"; |
| 52 | public static final String P4RUNTIME_SERVER_PORT_KEY = "p4runtime_port"; |
| 53 | public static final String P4RUNTIME_DEVICE_ID_KEY = "p4runtime_deviceId"; |
| 54 | |
| 55 | protected final Logger log = LoggerFactory.getLogger(getClass()); |
| 56 | |
| 57 | // Initialized by setupBehaviour() |
| 58 | protected DeviceId deviceId; |
| 59 | protected DeviceService deviceService; |
| 60 | protected Device device; |
| 61 | protected P4RuntimeController controller; |
| 62 | protected PiPipeconf pipeconf; |
| 63 | protected P4RuntimeClient client; |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 64 | protected PiTranslationService piTranslationService; |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 65 | |
| 66 | /** |
Carmelo Cascone | 3da671a | 2018-02-12 10:43:35 -0800 | [diff] [blame] | 67 | * Initializes this behaviour attributes. Returns true if the operation was |
| 68 | * successful, false otherwise. This method assumes that the P4runtime |
| 69 | * controller already has a client for this device and that the device has |
| 70 | * been created in the core. |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 71 | * |
| 72 | * @return true if successful, false otherwise |
| 73 | */ |
| 74 | protected boolean setupBehaviour() { |
| 75 | deviceId = handler().data().deviceId(); |
| 76 | |
| 77 | deviceService = handler().get(DeviceService.class); |
| 78 | device = deviceService.getDevice(deviceId); |
| 79 | if (device == null) { |
| 80 | log.warn("Unable to find device with id {}, aborting operation", deviceId); |
| 81 | return false; |
| 82 | } |
| 83 | |
| 84 | controller = handler().get(P4RuntimeController.class); |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 85 | client = controller.getClient(deviceId); |
| 86 | if (client == null) { |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 87 | log.warn("Unable to find client for {}, aborting operation", deviceId); |
| 88 | return false; |
| 89 | } |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 90 | |
| 91 | PiPipeconfService piPipeconfService = handler().get(PiPipeconfService.class); |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 92 | if (!piPipeconfService.ofDevice(deviceId).isPresent()) { |
| 93 | log.warn("Unable to get assigned pipeconf for {} (mapping " + |
| 94 | "missing in PiPipeconfService), aborting operation", |
| 95 | deviceId); |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 96 | return false; |
| 97 | } |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 98 | PiPipeconfId pipeconfId = piPipeconfService.ofDevice(deviceId).get(); |
| 99 | if (!piPipeconfService.getPipeconf(pipeconfId).isPresent()) { |
| 100 | log.warn("Cannot find any pipeconf with ID '{}' ({}), aborting operation", pipeconfId, deviceId); |
| 101 | return false; |
| 102 | } |
| 103 | pipeconf = piPipeconfService.getPipeconf(pipeconfId).get(); |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 104 | |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 105 | piTranslationService = handler().get(PiTranslationService.class); |
| 106 | |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 107 | return true; |
| 108 | } |
| 109 | |
| 110 | /** |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 111 | * Returns an instance of the interpreter implementation for this device, |
| 112 | * null if an interpreter cannot be retrieved. |
| 113 | * |
| 114 | * @return interpreter or null |
| 115 | */ |
| 116 | PiPipelineInterpreter getInterpreter() { |
| 117 | if (!device.is(PiPipelineInterpreter.class)) { |
| 118 | log.warn("Unable to get interpreter for {}, missing behaviour", |
| 119 | deviceId); |
| 120 | return null; |
| 121 | } |
| 122 | return device.as(PiPipelineInterpreter.class); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Returns a P4Runtime client for this device, null if such client cannot be |
| 127 | * created. |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 128 | * |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 129 | * @return client or null |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 130 | */ |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 131 | P4RuntimeClient createClient() { |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 132 | deviceId = handler().data().deviceId(); |
| 133 | controller = handler().get(P4RuntimeController.class); |
| 134 | |
Carmelo Cascone | 44448a5 | 2018-06-25 23:36:57 +0200 | [diff] [blame] | 135 | final String serverAddr = this.data().value(P4RUNTIME_SERVER_ADDR_KEY); |
| 136 | final String serverPortString = this.data().value(P4RUNTIME_SERVER_PORT_KEY); |
| 137 | final String p4DeviceIdString = this.data().value(P4RUNTIME_DEVICE_ID_KEY); |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 138 | |
| 139 | if (serverAddr == null || serverPortString == null || p4DeviceIdString == null) { |
| 140 | log.warn("Unable to create client for {}, missing driver data key (required is {}, {}, and {})", |
| 141 | deviceId, P4RUNTIME_SERVER_ADDR_KEY, P4RUNTIME_SERVER_PORT_KEY, P4RUNTIME_DEVICE_ID_KEY); |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 142 | return null; |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 143 | } |
| 144 | |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 145 | final int serverPort; |
| 146 | final long p4DeviceId; |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 147 | |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 148 | try { |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 149 | serverPort = Integer.parseUnsignedInt(serverPortString); |
| 150 | } catch (NumberFormatException e) { |
| 151 | log.error("{} is not a valid P4Runtime port number", serverPortString); |
| 152 | return null; |
| 153 | } |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 154 | try { |
| 155 | p4DeviceId = Long.parseUnsignedLong(p4DeviceIdString); |
| 156 | } catch (NumberFormatException e) { |
| 157 | log.error("{} is not a valid P4Runtime-internal device ID", p4DeviceIdString); |
| 158 | return null; |
| 159 | } |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 160 | |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame^] | 161 | P4RuntimeClientKey clientKey = new |
| 162 | P4RuntimeClientKey(deviceId, serverAddr, serverPort, p4DeviceId); |
| 163 | if (!controller.createClient(clientKey)) { |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 164 | log.warn("Unable to create client for {}, aborting operation", deviceId); |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 165 | return null; |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 166 | } |
| 167 | |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 168 | return controller.getClient(deviceId); |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 169 | } |
Carmelo Cascone | 3da671a | 2018-02-12 10:43:35 -0800 | [diff] [blame] | 170 | |
| 171 | /** |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 172 | * Returns the value of the given driver property, if present, otherwise |
| 173 | * returns the given default value. |
Carmelo Cascone | 3da671a | 2018-02-12 10:43:35 -0800 | [diff] [blame] | 174 | * |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 175 | * @param propName property name |
Carmelo Cascone | 3da671a | 2018-02-12 10:43:35 -0800 | [diff] [blame] | 176 | * @param defaultVal default value |
| 177 | * @return boolean |
| 178 | */ |
| 179 | protected boolean driverBoolProperty(String propName, boolean defaultVal) { |
| 180 | checkNotNull(propName); |
| 181 | if (handler().driver().getProperty(propName) == null) { |
| 182 | return defaultVal; |
| 183 | } else { |
| 184 | return Boolean.parseBoolean(handler().driver().getProperty(propName)); |
| 185 | } |
| 186 | } |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 187 | |
| 188 | /** |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 189 | * Returns the device request timeout driver property, or a default value |
| 190 | * if the property is not present or cannot be parsed. |
| 191 | * |
| 192 | * @return timeout value |
| 193 | */ |
| 194 | private int getDeviceRequestTimeout() { |
| 195 | final String timeout = handler().driver() |
| 196 | .getProperty(DEVICE_REQ_TIMEOUT); |
| 197 | if (timeout == null) { |
| 198 | return DEFAULT_DEVICE_REQ_TIMEOUT; |
| 199 | } else { |
| 200 | try { |
| 201 | return Integer.parseInt(timeout); |
| 202 | } catch (NumberFormatException e) { |
| 203 | log.error("{} driver property '{}' is not a number, using default value {}", |
| 204 | DEVICE_REQ_TIMEOUT, timeout, DEFAULT_DEVICE_REQ_TIMEOUT); |
| 205 | return DEFAULT_DEVICE_REQ_TIMEOUT; |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | /** |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 211 | * Convenience method to get the result of a completable future while |
| 212 | * setting a timeout and checking for exceptions. |
| 213 | * |
| 214 | * @param future completable future |
| 215 | * @param opDescription operation description to use in log messages. Should |
| 216 | * be a sentence starting with a verb ending in -ing, |
| 217 | * e.g. "reading...", "writing...", etc. |
| 218 | * @param defaultValue value to return if operation fails |
| 219 | * @param <U> type of returned value |
| 220 | * @return future result or default value |
| 221 | */ |
| 222 | <U> U getFutureWithDeadline(CompletableFuture<U> future, String opDescription, |
| 223 | U defaultValue) { |
| 224 | try { |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 225 | return future.get(getDeviceRequestTimeout(), TimeUnit.SECONDS); |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 226 | } catch (InterruptedException e) { |
| 227 | log.error("Exception while {} on {}", opDescription, deviceId); |
| 228 | } catch (ExecutionException e) { |
| 229 | final Throwable cause = e.getCause(); |
| 230 | if (cause instanceof StatusRuntimeException) { |
| 231 | final StatusRuntimeException grpcError = (StatusRuntimeException) cause; |
| 232 | log.warn("Error while {} on {}: {}", opDescription, deviceId, grpcError.getMessage()); |
| 233 | } else { |
| 234 | log.error("Exception while {} on {}", opDescription, deviceId, e.getCause()); |
| 235 | } |
| 236 | } catch (TimeoutException e) { |
| 237 | log.error("Operation TIMEOUT while {} on {}", opDescription, deviceId); |
| 238 | } |
| 239 | return defaultValue; |
| 240 | } |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 241 | } |