Carmelo Cascone | be5789d | 2018-04-19 09:28:01 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-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.mellanox; |
| 18 | |
Alan Lo | 5e2b705 | 2018-10-18 14:24:24 +0300 | [diff] [blame] | 19 | import org.apache.commons.io.IOUtils; |
Carmelo Cascone | be5789d | 2018-04-19 09:28:01 +0900 | [diff] [blame] | 20 | import org.onosproject.drivers.p4runtime.AbstractP4RuntimePipelineProgrammable; |
| 21 | import org.onosproject.net.behaviour.PiPipelineProgrammable; |
| 22 | import org.onosproject.net.pi.model.PiPipeconf; |
| 23 | import org.onosproject.net.pi.model.PiPipeconfId; |
Alan Lo | 5e2b705 | 2018-10-18 14:24:24 +0300 | [diff] [blame] | 24 | import org.onosproject.net.pi.model.PiPipeconf.ExtensionType; |
Carmelo Cascone | be5789d | 2018-04-19 09:28:01 +0900 | [diff] [blame] | 25 | import org.onosproject.net.pi.service.PiPipeconfService; |
| 26 | |
Alan Lo | 5e2b705 | 2018-10-18 14:24:24 +0300 | [diff] [blame] | 27 | import java.io.IOException; |
| 28 | |
Carmelo Cascone | be5789d | 2018-04-19 09:28:01 +0900 | [diff] [blame] | 29 | import java.nio.ByteBuffer; |
Alan Lo | 5e2b705 | 2018-10-18 14:24:24 +0300 | [diff] [blame] | 30 | import java.nio.ByteOrder; |
Carmelo Cascone | be5789d | 2018-04-19 09:28:01 +0900 | [diff] [blame] | 31 | import java.util.Optional; |
| 32 | |
Alan Lo | 5e2b705 | 2018-10-18 14:24:24 +0300 | [diff] [blame] | 33 | import static org.onosproject.net.pi.model.PiPipeconf.ExtensionType.SPECTRUM_BIN; |
Carmelo Cascone | be5789d | 2018-04-19 09:28:01 +0900 | [diff] [blame] | 34 | |
| 35 | /** |
| 36 | * Implementation of the PiPipelineProgrammable behaviour for a Spectrum-based |
| 37 | * switch with P4Runtime support. |
| 38 | */ |
| 39 | public class SpectrumPipelineProgrammable |
| 40 | extends AbstractP4RuntimePipelineProgrammable |
| 41 | implements PiPipelineProgrammable { |
| 42 | |
Alan Lo | 5e2b705 | 2018-10-18 14:24:24 +0300 | [diff] [blame] | 43 | private static final PiPipeconfId MLNX_FABRIC_PIPECONF_ID = |
| 44 | new PiPipeconfId("org.onosproject.pipelines.fabric-mlnx"); |
Carmelo Cascone | be5789d | 2018-04-19 09:28:01 +0900 | [diff] [blame] | 45 | |
| 46 | @Override |
| 47 | public ByteBuffer createDeviceDataBuffer(PiPipeconf pipeconf) { |
Alan Lo | 5e2b705 | 2018-10-18 14:24:24 +0300 | [diff] [blame] | 48 | log.debug("Creating device data buffer for {} in pipeconf {}", SPECTRUM_BIN, pipeconf.id()); |
| 49 | ByteBuffer deviceData; |
| 50 | try { |
| 51 | deviceData = extensionBuffer(pipeconf, SPECTRUM_BIN); |
| 52 | } catch (ExtensionException e) { |
| 53 | log.error("Failed to create device data buffer for {} in pipeconf {}", SPECTRUM_BIN, pipeconf.id()); |
| 54 | return null; |
| 55 | } |
| 56 | // flip buffer data so they can be read |
| 57 | deviceData.flip(); |
| 58 | return deviceData.asReadOnlyBuffer(); |
Carmelo Cascone | be5789d | 2018-04-19 09:28:01 +0900 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public Optional<PiPipeconf> getDefaultPipeconf() { |
| 63 | return handler().get(PiPipeconfService.class) |
Alan Lo | 5e2b705 | 2018-10-18 14:24:24 +0300 | [diff] [blame] | 64 | .getPipeconf(MLNX_FABRIC_PIPECONF_ID); |
| 65 | } |
| 66 | |
| 67 | private ByteBuffer extensionBuffer(PiPipeconf pipeconf, ExtensionType extType) { |
| 68 | if (!pipeconf.extension(extType).isPresent()) { |
| 69 | log.warn("Missing extension {} in pipeconf {}", extType, pipeconf.id()); |
| 70 | throw new ExtensionException(); |
| 71 | } |
| 72 | try { |
| 73 | byte[] bytes = IOUtils.toByteArray(pipeconf.extension(extType).get()); |
| 74 | // Length of the extension + bytes. |
| 75 | return ByteBuffer.allocate(bytes.length) |
| 76 | .order(ByteOrder.LITTLE_ENDIAN) |
| 77 | .put(bytes); |
| 78 | } catch (IOException ex) { |
| 79 | log.warn("Unable to read extension {} from pipeconf {}: {}", |
| 80 | extType, pipeconf.id(), ex.getMessage()); |
| 81 | throw new ExtensionException(); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | private static class ExtensionException extends IllegalArgumentException { |
Carmelo Cascone | be5789d | 2018-04-19 09:28:01 +0900 | [diff] [blame] | 86 | } |
| 87 | } |