blob: 114b2af3026740ca7c57b69c7bdc1cf58c3cd518 [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;
Carmelo Casconed519b552018-01-31 14:48:09 -080022import org.onosproject.net.behaviour.PiPipelineProgrammable;
Carmelo Casconed61fdb32017-10-30 10:09:57 -070023import org.onosproject.net.pi.model.PiPipeconf;
24import org.onosproject.net.pi.model.PiPipeconf.ExtensionType;
25
26import java.io.IOException;
27import java.nio.Buffer;
28import java.nio.ByteBuffer;
29import java.nio.ByteOrder;
30import java.nio.charset.StandardCharsets;
31import java.util.List;
32import java.util.Optional;
33
34import static org.onosproject.net.pi.model.PiPipeconf.ExtensionType.TOFINO_BIN;
35import 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 Casconed519b552018-01-31 14:48:09 -080040public class TofinoPipelineProgrammable
41 extends AbstractP4RuntimePipelineProgrammable
42 implements PiPipelineProgrammable {
Carmelo Casconed61fdb32017-10-30 10:09:57 -070043
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}