Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Laboratory |
| 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.bmv2; |
| 18 | |
| 19 | import org.onlab.util.SharedExecutors; |
| 20 | import org.onosproject.net.DeviceId; |
| 21 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 22 | import org.onosproject.net.pi.model.PiPipeconf; |
| 23 | import org.onosproject.net.pi.model.PiPipelineProgrammable; |
| 24 | import org.onosproject.p4runtime.api.P4RuntimeClient; |
| 25 | import org.onosproject.p4runtime.api.P4RuntimeController; |
| 26 | import org.slf4j.Logger; |
| 27 | |
| 28 | import java.io.InputStream; |
| 29 | import java.util.Optional; |
| 30 | import java.util.concurrent.CompletableFuture; |
| 31 | import java.util.concurrent.ExecutionException; |
| 32 | |
| 33 | import static org.onosproject.net.pi.model.PiPipeconf.ExtensionType.BMV2_JSON; |
| 34 | import static org.onosproject.net.pi.model.PiPipeconf.ExtensionType.P4_INFO_TEXT; |
| 35 | import static org.slf4j.LoggerFactory.getLogger; |
| 36 | |
| 37 | /** |
| 38 | * Implementation of the PiPipelineProgrammable for BMv2. |
| 39 | */ |
| 40 | public class Bmv2PipelineProgrammable extends AbstractHandlerBehaviour implements PiPipelineProgrammable { |
| 41 | |
Carmelo Cascone | 07d7271 | 2017-07-14 15:57:47 -0400 | [diff] [blame] | 42 | private static final PiPipeconf DEFAULT_PIPECONF = Bmv2DefaultPipeconfFactory.get(); |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 43 | |
| 44 | private final Logger log = getLogger(getClass()); |
| 45 | |
| 46 | @Override |
| 47 | public CompletableFuture<Boolean> deployPipeconf(PiPipeconf pipeconf) { |
| 48 | |
| 49 | CompletableFuture<Boolean> result = new CompletableFuture<>(); |
| 50 | |
| 51 | SharedExecutors.getPoolThreadExecutor().submit(() -> result.complete(doDeployConfig(pipeconf))); |
| 52 | |
| 53 | return result; |
| 54 | } |
| 55 | |
| 56 | private boolean doDeployConfig(PiPipeconf pipeconf) { |
| 57 | |
| 58 | P4RuntimeController controller = handler().get(P4RuntimeController.class); |
| 59 | |
| 60 | DeviceId deviceId = handler().data().deviceId(); |
| 61 | |
| 62 | if (!controller.hasClient(deviceId)) { |
| 63 | log.warn("Unable to find client for {}, aborting pipeconf deploy", deviceId); |
| 64 | return false; |
| 65 | |
| 66 | } |
| 67 | |
| 68 | P4RuntimeClient client = controller.getClient(deviceId); |
| 69 | |
| 70 | if (!pipeconf.extension(BMV2_JSON).isPresent()) { |
| 71 | log.warn("Missing BMv2 JSON config in pipeconf {}, aborting pipeconf deploy", pipeconf.id()); |
| 72 | return false; |
| 73 | } |
| 74 | |
| 75 | if (!pipeconf.extension(P4_INFO_TEXT).isPresent()) { |
| 76 | log.warn("Missing P4Info in pipeconf {}, aborting pipeconf deploy", pipeconf.id()); |
| 77 | return false; |
| 78 | } |
| 79 | |
| 80 | InputStream p4InfoStream = pipeconf.extension(P4_INFO_TEXT).get(); |
| 81 | InputStream jsonStream = pipeconf.extension(BMV2_JSON).get(); |
| 82 | |
| 83 | try { |
| 84 | if (!client.setPipelineConfig(p4InfoStream, jsonStream).get()) { |
| 85 | log.warn("Unable to deploy pipeconf {} to {}", pipeconf.id(), deviceId); |
| 86 | return false; |
| 87 | } |
| 88 | |
| 89 | // It would be more logical to have this performed at device handshake, but P4runtime would reject any |
| 90 | // command if a P4info has not been set first. |
| 91 | if (!client.initStreamChannel().get()) { |
| 92 | log.warn("Unable to init stream channel to {}.", deviceId); |
| 93 | return false; |
| 94 | } |
| 95 | |
| 96 | } catch (InterruptedException | ExecutionException e) { |
| 97 | throw new RuntimeException(e); |
| 98 | } |
| 99 | |
| 100 | return true; |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public Optional<PiPipeconf> getDefaultPipeconf() { |
| 105 | return Optional.of(DEFAULT_PIPECONF); |
| 106 | } |
| 107 | } |