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 | |
Yi Tseng | 8e0e294 | 2020-06-08 17:10:56 +0800 | [diff] [blame] | 34 | import static org.onosproject.net.pi.model.PiPipeconf.ExtensionType.RAW_DEVICE_CONFIG; |
Carmelo Cascone | d61fdb3 | 2017-10-30 10:09:57 -0700 | [diff] [blame] | 35 | import static org.onosproject.net.pi.model.PiPipeconf.ExtensionType.TOFINO_BIN; |
| 36 | import static org.onosproject.net.pi.model.PiPipeconf.ExtensionType.TOFINO_CONTEXT_JSON; |
| 37 | |
| 38 | /** |
| 39 | * Implementation of the PiPipelineProgrammable behaviour for a Tofino-based switch. |
| 40 | */ |
Carmelo Cascone | d519b55 | 2018-01-31 14:48:09 -0800 | [diff] [blame] | 41 | public class TofinoPipelineProgrammable |
| 42 | extends AbstractP4RuntimePipelineProgrammable |
| 43 | implements PiPipelineProgrammable { |
Carmelo Cascone | d61fdb3 | 2017-10-30 10:09:57 -0700 | [diff] [blame] | 44 | |
| 45 | @Override |
| 46 | public Optional<PiPipeconf> getDefaultPipeconf() { |
| 47 | return Optional.empty(); |
| 48 | } |
| 49 | |
| 50 | @Override |
| 51 | public ByteBuffer createDeviceDataBuffer(PiPipeconf pipeconf) { |
Carmelo Cascone | d61fdb3 | 2017-10-30 10:09:57 -0700 | [diff] [blame] | 52 | List<ByteBuffer> buffers = Lists.newLinkedList(); |
Yi Tseng | 8e0e294 | 2020-06-08 17:10:56 +0800 | [diff] [blame] | 53 | |
| 54 | if (pipeconf.extension(RAW_DEVICE_CONFIG).isPresent()) { |
| 55 | buffers.add(rawDeviceConfig(pipeconf)); |
| 56 | } else { |
| 57 | try { |
| 58 | buffers.add(nameBuffer(pipeconf)); |
| 59 | buffers.add(extensionBuffer(pipeconf, TOFINO_BIN)); |
| 60 | buffers.add(extensionBuffer(pipeconf, TOFINO_CONTEXT_JSON)); |
| 61 | } catch (ExtensionException e) { |
| 62 | return null; |
| 63 | } |
Carmelo Cascone | d61fdb3 | 2017-10-30 10:09:57 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | // Concatenate buffers (flip so they can be read). |
| 67 | int len = buffers.stream().mapToInt(Buffer::limit).sum(); |
| 68 | ByteBuffer deviceData = ByteBuffer.allocate(len); |
| 69 | buffers.forEach(b -> deviceData.put((ByteBuffer) b.flip())); |
| 70 | deviceData.flip(); |
| 71 | |
| 72 | return deviceData.asReadOnlyBuffer(); |
| 73 | } |
| 74 | |
| 75 | private ByteBuffer nameBuffer(PiPipeconf pipeconf) { |
| 76 | // Length of the name + name. |
| 77 | String name = pipeconf.id().toString(); |
| 78 | return ByteBuffer.allocate(Integer.BYTES + name.length()) |
| 79 | .order(ByteOrder.LITTLE_ENDIAN) |
| 80 | .putInt(name.length()) |
| 81 | .put(name.getBytes(StandardCharsets.UTF_8)); |
| 82 | } |
| 83 | |
| 84 | private ByteBuffer extensionBuffer(PiPipeconf pipeconf, ExtensionType extType) { |
| 85 | if (!pipeconf.extension(extType).isPresent()) { |
| 86 | log.warn("Missing extension {} in pipeconf {}", extType, pipeconf.id()); |
| 87 | throw new ExtensionException(); |
| 88 | } |
| 89 | try { |
| 90 | byte[] bytes = IOUtils.toByteArray(pipeconf.extension(extType).get()); |
| 91 | // Length of the extension + bytes. |
| 92 | return ByteBuffer.allocate(Integer.BYTES + bytes.length) |
| 93 | .order(ByteOrder.LITTLE_ENDIAN) |
| 94 | .putInt(bytes.length) |
| 95 | .put(bytes); |
| 96 | } catch (IOException ex) { |
| 97 | log.warn("Unable to read extension {} from pipeconf {}: {}", |
| 98 | extType, pipeconf.id(), ex.getMessage()); |
| 99 | throw new ExtensionException(); |
| 100 | } |
| 101 | } |
| 102 | |
Yi Tseng | 8e0e294 | 2020-06-08 17:10:56 +0800 | [diff] [blame] | 103 | private ByteBuffer rawDeviceConfig(PiPipeconf pipeconf) { |
| 104 | try { |
| 105 | byte[] bytes = IOUtils.toByteArray(pipeconf.extension(RAW_DEVICE_CONFIG).get()); |
| 106 | return ByteBuffer.allocate(bytes.length) |
| 107 | .order(ByteOrder.LITTLE_ENDIAN) |
| 108 | .put(bytes); |
| 109 | } catch (IOException ex) { |
| 110 | log.warn("Unable to read raw device config from pipeconf {}: {}", |
| 111 | pipeconf.id(), ex.getMessage()); |
| 112 | throw new ExtensionException(); |
| 113 | } |
| 114 | } |
| 115 | |
Carmelo Cascone | d61fdb3 | 2017-10-30 10:09:57 -0700 | [diff] [blame] | 116 | private static class ExtensionException extends IllegalArgumentException { |
| 117 | } |
| 118 | } |