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.barefoot; |
| 18 | |
| 19 | import com.google.common.collect.Lists; |
| 20 | import org.apache.commons.io.IOUtils; |
| 21 | import org.onosproject.drivers.p4runtime.AbstractP4RuntimePipelineProgrammable; |
Carmelo Cascone | d519b55 | 2018-01-31 14:48:09 -0800 | [diff] [blame] | 22 | import org.onosproject.net.behaviour.PiPipelineProgrammable; |
Carmelo Cascone | d61fdb3 | 2017-10-30 10:09:57 -0700 | [diff] [blame] | 23 | import org.onosproject.net.pi.model.PiPipeconf; |
| 24 | import org.onosproject.net.pi.model.PiPipeconf.ExtensionType; |
| 25 | |
| 26 | import java.io.IOException; |
| 27 | import java.nio.Buffer; |
| 28 | import java.nio.ByteBuffer; |
| 29 | import java.nio.ByteOrder; |
| 30 | import java.nio.charset.StandardCharsets; |
| 31 | import java.util.List; |
| 32 | import java.util.Optional; |
| 33 | |
| 34 | import static org.onosproject.net.pi.model.PiPipeconf.ExtensionType.TOFINO_BIN; |
| 35 | import static org.onosproject.net.pi.model.PiPipeconf.ExtensionType.TOFINO_CONTEXT_JSON; |
| 36 | |
| 37 | /** |
| 38 | * Implementation of the PiPipelineProgrammable behaviour for a Tofino-based switch. |
| 39 | */ |
Carmelo Cascone | d519b55 | 2018-01-31 14:48:09 -0800 | [diff] [blame] | 40 | public class TofinoPipelineProgrammable |
| 41 | extends AbstractP4RuntimePipelineProgrammable |
| 42 | implements PiPipelineProgrammable { |
Carmelo Cascone | d61fdb3 | 2017-10-30 10:09:57 -0700 | [diff] [blame] | 43 | |
| 44 | @Override |
| 45 | public Optional<PiPipeconf> getDefaultPipeconf() { |
| 46 | return Optional.empty(); |
| 47 | } |
| 48 | |
| 49 | @Override |
| 50 | public ByteBuffer createDeviceDataBuffer(PiPipeconf pipeconf) { |
| 51 | |
| 52 | List<ByteBuffer> buffers = Lists.newLinkedList(); |
| 53 | try { |
| 54 | buffers.add(nameBuffer(pipeconf)); |
| 55 | buffers.add(extensionBuffer(pipeconf, TOFINO_BIN)); |
| 56 | buffers.add(extensionBuffer(pipeconf, TOFINO_CONTEXT_JSON)); |
| 57 | } catch (ExtensionException e) { |
| 58 | return null; |
| 59 | } |
| 60 | |
| 61 | // Concatenate buffers (flip so they can be read). |
| 62 | int len = buffers.stream().mapToInt(Buffer::limit).sum(); |
| 63 | ByteBuffer deviceData = ByteBuffer.allocate(len); |
| 64 | buffers.forEach(b -> deviceData.put((ByteBuffer) b.flip())); |
| 65 | deviceData.flip(); |
| 66 | |
| 67 | return deviceData.asReadOnlyBuffer(); |
| 68 | } |
| 69 | |
| 70 | private ByteBuffer nameBuffer(PiPipeconf pipeconf) { |
| 71 | // Length of the name + name. |
| 72 | String name = pipeconf.id().toString(); |
| 73 | return ByteBuffer.allocate(Integer.BYTES + name.length()) |
| 74 | .order(ByteOrder.LITTLE_ENDIAN) |
| 75 | .putInt(name.length()) |
| 76 | .put(name.getBytes(StandardCharsets.UTF_8)); |
| 77 | } |
| 78 | |
| 79 | private ByteBuffer extensionBuffer(PiPipeconf pipeconf, ExtensionType extType) { |
| 80 | if (!pipeconf.extension(extType).isPresent()) { |
| 81 | log.warn("Missing extension {} in pipeconf {}", extType, pipeconf.id()); |
| 82 | throw new ExtensionException(); |
| 83 | } |
| 84 | try { |
| 85 | byte[] bytes = IOUtils.toByteArray(pipeconf.extension(extType).get()); |
| 86 | // Length of the extension + bytes. |
| 87 | return ByteBuffer.allocate(Integer.BYTES + bytes.length) |
| 88 | .order(ByteOrder.LITTLE_ENDIAN) |
| 89 | .putInt(bytes.length) |
| 90 | .put(bytes); |
| 91 | } catch (IOException ex) { |
| 92 | log.warn("Unable to read extension {} from pipeconf {}: {}", |
| 93 | extType, pipeconf.id(), ex.getMessage()); |
| 94 | throw new ExtensionException(); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | private static class ExtensionException extends IllegalArgumentException { |
| 99 | } |
| 100 | } |