blob: 0eef5ed56aa5d5a74ac4dbe8956bc570dbcff014 [file] [log] [blame]
Carmelo Cascone59f57de2017-07-11 19:55:09 -04001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Carmelo Cascone59f57de2017-07-11 19:55:09 -04003 *
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.bmv2;
18
19import org.onlab.util.SharedExecutors;
20import org.onosproject.net.DeviceId;
21import org.onosproject.net.driver.AbstractHandlerBehaviour;
22import org.onosproject.net.pi.model.PiPipeconf;
23import org.onosproject.net.pi.model.PiPipelineProgrammable;
24import org.onosproject.p4runtime.api.P4RuntimeClient;
25import org.onosproject.p4runtime.api.P4RuntimeController;
26import org.slf4j.Logger;
27
Carmelo Cascone59f57de2017-07-11 19:55:09 -040028import java.util.Optional;
29import java.util.concurrent.CompletableFuture;
30import java.util.concurrent.ExecutionException;
31
32import static org.onosproject.net.pi.model.PiPipeconf.ExtensionType.BMV2_JSON;
Carmelo Cascone59f57de2017-07-11 19:55:09 -040033import static org.slf4j.LoggerFactory.getLogger;
34
35/**
36 * Implementation of the PiPipelineProgrammable for BMv2.
37 */
38public class Bmv2PipelineProgrammable extends AbstractHandlerBehaviour implements PiPipelineProgrammable {
39
Carmelo Cascone07d72712017-07-14 15:57:47 -040040 private static final PiPipeconf DEFAULT_PIPECONF = Bmv2DefaultPipeconfFactory.get();
Carmelo Cascone59f57de2017-07-11 19:55:09 -040041
42 private final Logger log = getLogger(getClass());
43
44 @Override
45 public CompletableFuture<Boolean> deployPipeconf(PiPipeconf pipeconf) {
46
47 CompletableFuture<Boolean> result = new CompletableFuture<>();
48
49 SharedExecutors.getPoolThreadExecutor().submit(() -> result.complete(doDeployConfig(pipeconf)));
50
51 return result;
52 }
53
54 private boolean doDeployConfig(PiPipeconf pipeconf) {
55
56 P4RuntimeController controller = handler().get(P4RuntimeController.class);
57
58 DeviceId deviceId = handler().data().deviceId();
59
60 if (!controller.hasClient(deviceId)) {
61 log.warn("Unable to find client for {}, aborting pipeconf deploy", deviceId);
62 return false;
63
64 }
65
66 P4RuntimeClient client = controller.getClient(deviceId);
67
Carmelo Cascone59f57de2017-07-11 19:55:09 -040068 try {
Carmelo Cascone8d99b172017-07-18 17:26:31 -040069 if (!client.setPipelineConfig(pipeconf, BMV2_JSON).get()) {
Carmelo Cascone59f57de2017-07-11 19:55:09 -040070 log.warn("Unable to deploy pipeconf {} to {}", pipeconf.id(), deviceId);
71 return false;
72 }
73
74 // It would be more logical to have this performed at device handshake, but P4runtime would reject any
75 // command if a P4info has not been set first.
76 if (!client.initStreamChannel().get()) {
77 log.warn("Unable to init stream channel to {}.", deviceId);
78 return false;
79 }
80
81 } catch (InterruptedException | ExecutionException e) {
82 throw new RuntimeException(e);
83 }
84
85 return true;
86 }
87
88 @Override
89 public Optional<PiPipeconf> getDefaultPipeconf() {
90 return Optional.of(DEFAULT_PIPECONF);
91 }
92}