blob: 041d930371f93c8c62744e45201437cf88f20d14 [file] [log] [blame]
Carmelo Casconed61fdb32017-10-30 10:09:57 -07001/*
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
17package org.onosproject.drivers.p4runtime;
18
CyberHasHe9ba39c2019-10-11 05:59:12 +080019import org.onosproject.drivers.p4runtime.mirror.P4RuntimeDefaultEntryMirror;
Carmelo Cascone87892e22017-11-13 16:01:29 -080020import org.onosproject.net.behaviour.PiPipelineProgrammable;
CyberHasHe9ba39c2019-10-11 05:59:12 +080021import org.onosproject.net.pi.model.PiPipelineModel;
Carmelo Casconee5b28722018-06-22 17:28:28 +020022import org.onosproject.net.pi.model.PiPipeconf;
CyberHasHe9ba39c2019-10-11 05:59:12 +080023import org.onosproject.net.pi.runtime.PiTableEntry;
24import org.onosproject.p4runtime.api.P4RuntimeReadClient;
Carmelo Casconed61fdb32017-10-30 10:09:57 -070025import org.slf4j.Logger;
26
27import java.nio.ByteBuffer;
28import java.util.Optional;
29import java.util.concurrent.CompletableFuture;
Carmelo Casconed61fdb32017-10-30 10:09:57 -070030
CyberHasHe9ba39c2019-10-11 05:59:12 +080031import static org.onosproject.drivers.p4runtime.P4RuntimeDriverProperties.SUPPORT_DEFAULT_TABLE_ENTRY;
32import static org.onosproject.drivers.p4runtime.P4RuntimeDriverProperties.DEFAULT_SUPPORT_DEFAULT_TABLE_ENTRY;
Carmelo Cascone3977ea42019-02-28 13:43:42 -080033import static java.util.concurrent.CompletableFuture.completedFuture;
Carmelo Casconed61fdb32017-10-30 10:09:57 -070034import static org.slf4j.LoggerFactory.getLogger;
35
36/**
37 * Abstract implementation of the PiPipelineProgrammable behaviours for a P4Runtime device.
38 */
Carmelo Casconee5b28722018-06-22 17:28:28 +020039public abstract class AbstractP4RuntimePipelineProgrammable
40 extends AbstractP4RuntimeHandlerBehaviour
Carmelo Casconed61fdb32017-10-30 10:09:57 -070041 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 Cascone9e4972c2018-08-30 00:29:16 -070054 public CompletableFuture<Boolean> setPipeconf(PiPipeconf pipeconf) {
Carmelo Casconec2be50a2019-04-10 00:15:39 -070055 if (!setupBehaviour("setPipeconf()")) {
Carmelo Cascone3977ea42019-02-28 13:43:42 -080056 return completedFuture(false);
Carmelo Casconed61fdb32017-10-30 10:09:57 -070057 }
Carmelo Casconed61fdb32017-10-30 10:09:57 -070058
Carmelo Casconec2be50a2019-04-10 00:15:39 -070059 final ByteBuffer deviceDataBuffer = createDeviceDataBuffer(pipeconf);
Carmelo Casconed61fdb32017-10-30 10:09:57 -070060 if (deviceDataBuffer == null) {
61 // Hopefully the child class logged the problem.
Carmelo Cascone3977ea42019-02-28 13:43:42 -080062 return completedFuture(false);
Carmelo Casconed61fdb32017-10-30 10:09:57 -070063 }
CyberHasHe9ba39c2019-10-11 05:59:12 +080064 CompletableFuture<Boolean> pipeconfSet = client.setPipelineConfig(
65 p4DeviceId, pipeconf, deviceDataBuffer);
66 return getDefaultEntries(pipeconfSet, pipeconf);
Carmelo Casconed61fdb32017-10-30 10:09:57 -070067 }
68
69 @Override
Carmelo Cascone3977ea42019-02-28 13:43:42 -080070 public CompletableFuture<Boolean> isPipeconfSet(PiPipeconf pipeconf) {
Carmelo Casconec2be50a2019-04-10 00:15:39 -070071 if (!setupBehaviour("isPipeconfSet()")) {
Carmelo Cascone3977ea42019-02-28 13:43:42 -080072 return completedFuture(false);
Carmelo Cascone9e4972c2018-08-30 00:29:16 -070073 }
74
Carmelo Casconeadb89052019-04-17 20:02:33 -070075 return client.isPipelineConfigSet(p4DeviceId, pipeconf);
Carmelo Cascone9e4972c2018-08-30 00:29:16 -070076 }
77
78 @Override
Carmelo Casconed61fdb32017-10-30 10:09:57 -070079 public abstract Optional<PiPipeconf> getDefaultPipeconf();
CyberHasHe9ba39c2019-10-11 05:59:12 +080080
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 Casconed61fdb32017-10-30 10:09:57 -0700123}