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