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 | |
Carmelo Cascone | d61fdb3 | 2017-10-30 10:09:57 -0700 | [diff] [blame] | 19 | import org.onosproject.net.DeviceId; |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 20 | import org.onosproject.net.behaviour.PiPipelineProgrammable; |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 21 | import org.onosproject.net.pi.model.PiPipeconf; |
Carmelo Cascone | d61fdb3 | 2017-10-30 10:09:57 -0700 | [diff] [blame] | 22 | import org.onosproject.p4runtime.api.P4RuntimeClient; |
| 23 | import org.onosproject.p4runtime.api.P4RuntimeController; |
| 24 | import org.slf4j.Logger; |
| 25 | |
| 26 | import java.nio.ByteBuffer; |
| 27 | import java.util.Optional; |
| 28 | import java.util.concurrent.CompletableFuture; |
Carmelo Cascone | d61fdb3 | 2017-10-30 10:09:57 -0700 | [diff] [blame] | 29 | |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 30 | import static java.util.concurrent.CompletableFuture.completedFuture; |
Carmelo Cascone | d61fdb3 | 2017-10-30 10:09:57 -0700 | [diff] [blame] | 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 | DeviceId deviceId = handler().data().deviceId(); |
| 53 | P4RuntimeController controller = handler().get(P4RuntimeController.class); |
| 54 | |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 55 | P4RuntimeClient client = controller.getClient(deviceId); |
| 56 | if (client == null) { |
Carmelo Cascone | d61fdb3 | 2017-10-30 10:09:57 -0700 | [diff] [blame] | 57 | log.warn("Unable to find client for {}, aborting pipeconf deploy", deviceId); |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 58 | return completedFuture(false); |
Carmelo Cascone | d61fdb3 | 2017-10-30 10:09:57 -0700 | [diff] [blame] | 59 | } |
Carmelo Cascone | d61fdb3 | 2017-10-30 10:09:57 -0700 | [diff] [blame] | 60 | |
| 61 | ByteBuffer deviceDataBuffer = createDeviceDataBuffer(pipeconf); |
| 62 | if (deviceDataBuffer == null) { |
| 63 | // Hopefully the child class logged the problem. |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 64 | return completedFuture(false); |
Carmelo Cascone | d61fdb3 | 2017-10-30 10:09:57 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 67 | return client.setPipelineConfig(pipeconf, deviceDataBuffer); |
Carmelo Cascone | d61fdb3 | 2017-10-30 10:09:57 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | @Override |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 71 | public CompletableFuture<Boolean> isPipeconfSet(PiPipeconf pipeconf) { |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 72 | DeviceId deviceId = handler().data().deviceId(); |
| 73 | P4RuntimeController controller = handler().get(P4RuntimeController.class); |
| 74 | |
| 75 | P4RuntimeClient client = controller.getClient(deviceId); |
| 76 | if (client == null) { |
| 77 | log.warn("Unable to find client for {}, cannot check if pipeconf is set", deviceId); |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 78 | return completedFuture(false); |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | ByteBuffer deviceDataBuffer = createDeviceDataBuffer(pipeconf); |
| 82 | if (deviceDataBuffer == null) { |
| 83 | // Hopefully the child class logged the problem. |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 84 | return completedFuture(false); |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 87 | return client.isPipelineConfigSet(pipeconf, deviceDataBuffer); |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | @Override |
Carmelo Cascone | d61fdb3 | 2017-10-30 10:09:57 -0700 | [diff] [blame] | 91 | public abstract Optional<PiPipeconf> getDefaultPipeconf(); |
| 92 | } |