Carmelo Cascone | d61fdb3 | 2017-10-30 10:09:57 -0700 | [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 | |
| 19 | import org.onlab.util.SharedExecutors; |
| 20 | import org.onosproject.net.DeviceId; |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 21 | import org.onosproject.net.behaviour.PiPipelineProgrammable; |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 22 | import org.onosproject.net.pi.model.PiPipeconf; |
Carmelo Cascone | d61fdb3 | 2017-10-30 10:09:57 -0700 | [diff] [blame] | 23 | import org.onosproject.p4runtime.api.P4RuntimeClient; |
| 24 | import org.onosproject.p4runtime.api.P4RuntimeController; |
| 25 | import org.slf4j.Logger; |
| 26 | |
| 27 | import java.nio.ByteBuffer; |
| 28 | import java.util.Optional; |
| 29 | import java.util.concurrent.CompletableFuture; |
Carmelo Cascone | d61fdb3 | 2017-10-30 10:09:57 -0700 | [diff] [blame] | 30 | |
| 31 | import static org.slf4j.LoggerFactory.getLogger; |
| 32 | |
| 33 | /** |
| 34 | * Abstract implementation of the PiPipelineProgrammable behaviours for a P4Runtime device. |
| 35 | */ |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 36 | public abstract class AbstractP4RuntimePipelineProgrammable |
| 37 | extends AbstractP4RuntimeHandlerBehaviour |
Carmelo Cascone | d61fdb3 | 2017-10-30 10:09:57 -0700 | [diff] [blame] | 38 | implements PiPipelineProgrammable { |
| 39 | |
| 40 | protected final Logger log = getLogger(getClass()); |
| 41 | |
| 42 | /** |
| 43 | * Returns a byte buffer representing the target-specific device data to be used in the SetPipelineConfig message. |
| 44 | * |
| 45 | * @param pipeconf pipeconf |
| 46 | * @return byte buffer |
| 47 | */ |
| 48 | public abstract ByteBuffer createDeviceDataBuffer(PiPipeconf pipeconf); |
| 49 | |
| 50 | @Override |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 51 | public CompletableFuture<Boolean> setPipeconf(PiPipeconf pipeconf) { |
Carmelo Cascone | d61fdb3 | 2017-10-30 10:09:57 -0700 | [diff] [blame] | 52 | return CompletableFuture.supplyAsync( |
| 53 | () -> doDeployConfig(pipeconf), |
| 54 | SharedExecutors.getPoolThreadExecutor()); |
| 55 | } |
| 56 | |
| 57 | private boolean doDeployConfig(PiPipeconf pipeconf) { |
| 58 | |
| 59 | DeviceId deviceId = handler().data().deviceId(); |
| 60 | P4RuntimeController controller = handler().get(P4RuntimeController.class); |
| 61 | |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 62 | P4RuntimeClient client = controller.getClient(deviceId); |
| 63 | if (client == null) { |
Carmelo Cascone | d61fdb3 | 2017-10-30 10:09:57 -0700 | [diff] [blame] | 64 | log.warn("Unable to find client for {}, aborting pipeconf deploy", deviceId); |
| 65 | return false; |
| 66 | } |
Carmelo Cascone | d61fdb3 | 2017-10-30 10:09:57 -0700 | [diff] [blame] | 67 | |
| 68 | ByteBuffer deviceDataBuffer = createDeviceDataBuffer(pipeconf); |
| 69 | if (deviceDataBuffer == null) { |
| 70 | // Hopefully the child class logged the problem. |
| 71 | return false; |
| 72 | } |
| 73 | |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 74 | final Boolean deploySuccess = getFutureWithDeadline( |
| 75 | client.setPipelineConfig(pipeconf, deviceDataBuffer), |
| 76 | "deploying pipeconf", null); |
| 77 | if (deploySuccess == null) { |
| 78 | return false; |
| 79 | } else if (!deploySuccess) { |
| 80 | log.warn("Unable to deploy pipeconf {} to {}", pipeconf.id(), deviceId); |
Yi Tseng | 3e7f145 | 2017-10-20 10:31:53 -0700 | [diff] [blame] | 81 | return false; |
| 82 | } |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 83 | |
Carmelo Cascone | d61fdb3 | 2017-10-30 10:09:57 -0700 | [diff] [blame] | 84 | return true; |
| 85 | } |
| 86 | |
| 87 | @Override |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 88 | public boolean isPipeconfSet(PiPipeconf pipeconf) { |
| 89 | DeviceId deviceId = handler().data().deviceId(); |
| 90 | P4RuntimeController controller = handler().get(P4RuntimeController.class); |
| 91 | |
| 92 | P4RuntimeClient client = controller.getClient(deviceId); |
| 93 | if (client == null) { |
| 94 | log.warn("Unable to find client for {}, cannot check if pipeconf is set", deviceId); |
| 95 | return false; |
| 96 | } |
| 97 | |
| 98 | ByteBuffer deviceDataBuffer = createDeviceDataBuffer(pipeconf); |
| 99 | if (deviceDataBuffer == null) { |
| 100 | // Hopefully the child class logged the problem. |
| 101 | return false; |
| 102 | } |
| 103 | |
| 104 | return client.isPipelineConfigSet(pipeconf, deviceDataBuffer); |
| 105 | } |
| 106 | |
| 107 | @Override |
Carmelo Cascone | d61fdb3 | 2017-10-30 10:09:57 -0700 | [diff] [blame] | 108 | public abstract Optional<PiPipeconf> getDefaultPipeconf(); |
| 109 | } |