blob: 7b50377aae4040d53e07dc94ddfa57adfe7e301f [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
Yi Tseng8e0e2942020-06-08 17:10:56 +080034import static org.onosproject.net.pi.model.PiPipeconf.ExtensionType.RAW_DEVICE_CONFIG;
Carmelo Casconed61fdb32017-10-30 10:09:57 -070035import static org.onosproject.net.pi.model.PiPipeconf.ExtensionType.TOFINO_BIN;
36import 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 Casconed519b552018-01-31 14:48:09 -080041public class TofinoPipelineProgrammable
42 extends AbstractP4RuntimePipelineProgrammable
43 implements PiPipelineProgrammable {
Carmelo Casconed61fdb32017-10-30 10:09:57 -070044
45 @Override
46 public Optional<PiPipeconf> getDefaultPipeconf() {
47 return Optional.empty();
48 }
49
50 @Override
51 public ByteBuffer createDeviceDataBuffer(PiPipeconf pipeconf) {
Carmelo Casconed61fdb32017-10-30 10:09:57 -070052 List<ByteBuffer> buffers = Lists.newLinkedList();
Yi Tseng8e0e2942020-06-08 17:10:56 +080053
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 Casconed61fdb32017-10-30 10:09:57 -070064 }
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 Tseng8e0e2942020-06-08 17:10:56 +0800103 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 Casconed61fdb32017-10-30 10:09:57 -0700116 private static class ExtensionException extends IllegalArgumentException {
117 }
118}