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 | |
CyberHasH | e9ba39c | 2019-10-11 05:59:12 +0800 | [diff] [blame] | 19 | import org.onosproject.drivers.p4runtime.mirror.P4RuntimeDefaultEntryMirror; |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 20 | import org.onosproject.net.behaviour.PiPipelineProgrammable; |
CyberHasH | e9ba39c | 2019-10-11 05:59:12 +0800 | [diff] [blame] | 21 | import org.onosproject.net.pi.model.PiPipelineModel; |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 22 | import org.onosproject.net.pi.model.PiPipeconf; |
CyberHasH | e9ba39c | 2019-10-11 05:59:12 +0800 | [diff] [blame] | 23 | import org.onosproject.net.pi.runtime.PiTableEntry; |
| 24 | import org.onosproject.p4runtime.api.P4RuntimeReadClient; |
Carmelo Cascone | d61fdb3 | 2017-10-30 10:09:57 -0700 | [diff] [blame] | 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 | |
CyberHasH | e9ba39c | 2019-10-11 05:59:12 +0800 | [diff] [blame] | 31 | import static org.onosproject.drivers.p4runtime.P4RuntimeDriverProperties.SUPPORT_DEFAULT_TABLE_ENTRY; |
| 32 | import static org.onosproject.drivers.p4runtime.P4RuntimeDriverProperties.DEFAULT_SUPPORT_DEFAULT_TABLE_ENTRY; |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 33 | import static java.util.concurrent.CompletableFuture.completedFuture; |
Carmelo Cascone | d61fdb3 | 2017-10-30 10:09:57 -0700 | [diff] [blame] | 34 | import static org.slf4j.LoggerFactory.getLogger; |
| 35 | |
| 36 | /** |
| 37 | * Abstract implementation of the PiPipelineProgrammable behaviours for a P4Runtime device. |
| 38 | */ |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 39 | public abstract class AbstractP4RuntimePipelineProgrammable |
| 40 | extends AbstractP4RuntimeHandlerBehaviour |
Carmelo Cascone | d61fdb3 | 2017-10-30 10:09:57 -0700 | [diff] [blame] | 41 | implements PiPipelineProgrammable { |
| 42 | |
| 43 | protected final Logger log = getLogger(getClass()); |
| 44 | |
| 45 | /** |
| 46 | * Returns a byte buffer representing the target-specific device data to be used in the SetPipelineConfig message. |
| 47 | * |
| 48 | * @param pipeconf pipeconf |
| 49 | * @return byte buffer |
| 50 | */ |
| 51 | public abstract ByteBuffer createDeviceDataBuffer(PiPipeconf pipeconf); |
| 52 | |
| 53 | @Override |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 54 | public CompletableFuture<Boolean> setPipeconf(PiPipeconf pipeconf) { |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 55 | if (!setupBehaviour("setPipeconf()")) { |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 56 | return completedFuture(false); |
Carmelo Cascone | d61fdb3 | 2017-10-30 10:09:57 -0700 | [diff] [blame] | 57 | } |
Carmelo Cascone | d61fdb3 | 2017-10-30 10:09:57 -0700 | [diff] [blame] | 58 | |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 59 | final ByteBuffer deviceDataBuffer = createDeviceDataBuffer(pipeconf); |
Carmelo Cascone | d61fdb3 | 2017-10-30 10:09:57 -0700 | [diff] [blame] | 60 | if (deviceDataBuffer == null) { |
| 61 | // Hopefully the child class logged the problem. |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 62 | return completedFuture(false); |
Carmelo Cascone | d61fdb3 | 2017-10-30 10:09:57 -0700 | [diff] [blame] | 63 | } |
CyberHasH | e9ba39c | 2019-10-11 05:59:12 +0800 | [diff] [blame] | 64 | CompletableFuture<Boolean> pipeconfSet = client.setPipelineConfig( |
| 65 | p4DeviceId, pipeconf, deviceDataBuffer); |
| 66 | return getDefaultEntries(pipeconfSet, pipeconf); |
Carmelo Cascone | d61fdb3 | 2017-10-30 10:09:57 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | @Override |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 70 | public CompletableFuture<Boolean> isPipeconfSet(PiPipeconf pipeconf) { |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 71 | if (!setupBehaviour("isPipeconfSet()")) { |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 72 | return completedFuture(false); |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Carmelo Cascone | adb8905 | 2019-04-17 20:02:33 -0700 | [diff] [blame] | 75 | return client.isPipelineConfigSet(p4DeviceId, pipeconf); |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | @Override |
Carmelo Cascone | d61fdb3 | 2017-10-30 10:09:57 -0700 | [diff] [blame] | 79 | public abstract Optional<PiPipeconf> getDefaultPipeconf(); |
CyberHasH | e9ba39c | 2019-10-11 05:59:12 +0800 | [diff] [blame] | 80 | |
| 81 | /** |
| 82 | * Once the pipeconf is set successfully, we should store all the default entries |
| 83 | * before notify other service to prevent overwriting the default entries. |
| 84 | * Default entries may be used in P4RuntimeFlowRuleProgrammable. |
| 85 | * <p> |
| 86 | * This method returns a completable future with the result of the pipeconf set |
| 87 | * operation (which might not be true). |
| 88 | * |
| 89 | * @param pipeconfSet completable future for setting pipeconf |
| 90 | * @param pipeconf pipeconf |
| 91 | * @return completable future eventually true if the pipeconf set successfully |
| 92 | */ |
| 93 | private CompletableFuture<Boolean> getDefaultEntries(CompletableFuture<Boolean> pipeconfSet, PiPipeconf pipeconf) { |
| 94 | if (!driverBoolProperty( |
| 95 | SUPPORT_DEFAULT_TABLE_ENTRY, |
| 96 | DEFAULT_SUPPORT_DEFAULT_TABLE_ENTRY)) { |
| 97 | return pipeconfSet; |
| 98 | } |
| 99 | return pipeconfSet.thenApply(setSuccess -> { |
| 100 | if (!setSuccess) { |
| 101 | return setSuccess; |
| 102 | } |
| 103 | final P4RuntimeDefaultEntryMirror mirror = handler() |
| 104 | .get(P4RuntimeDefaultEntryMirror.class); |
| 105 | |
| 106 | final PiPipelineModel pipelineModel = pipeconf.pipelineModel(); |
| 107 | final P4RuntimeReadClient.ReadRequest request = client.read( |
| 108 | p4DeviceId, pipeconf); |
| 109 | // Read default entries from all non-constant tables. |
| 110 | // Ignore constant default entries. |
| 111 | pipelineModel.tables().stream() |
| 112 | .filter(t -> !t.isConstantTable()) |
| 113 | .forEach(t -> { |
| 114 | if (!t.constDefaultAction().isPresent()) { |
| 115 | request.defaultTableEntry(t.id()); |
| 116 | } |
| 117 | }); |
| 118 | final P4RuntimeReadClient.ReadResponse response = request.submitSync(); |
| 119 | mirror.sync(deviceId, response.all(PiTableEntry.class)); |
| 120 | return true; |
| 121 | }); |
| 122 | } |
Carmelo Cascone | d61fdb3 | 2017-10-30 10:09:57 -0700 | [diff] [blame] | 123 | } |