blob: d421be463563254c1cb4d3a454320582fe6a0368 [file] [log] [blame]
Carmelo Casconebe5789d2018-04-19 09:28:01 +09001/*
2 * Copyright 2018-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.mellanox;
18
Alan Lo5e2b7052018-10-18 14:24:24 +030019import org.apache.commons.io.IOUtils;
Carmelo Casconebe5789d2018-04-19 09:28:01 +090020import org.onosproject.drivers.p4runtime.AbstractP4RuntimePipelineProgrammable;
21import org.onosproject.net.behaviour.PiPipelineProgrammable;
22import org.onosproject.net.pi.model.PiPipeconf;
23import org.onosproject.net.pi.model.PiPipeconfId;
Alan Lo5e2b7052018-10-18 14:24:24 +030024import org.onosproject.net.pi.model.PiPipeconf.ExtensionType;
Carmelo Casconebe5789d2018-04-19 09:28:01 +090025import org.onosproject.net.pi.service.PiPipeconfService;
26
Alan Lo5e2b7052018-10-18 14:24:24 +030027import java.io.IOException;
28
Carmelo Casconebe5789d2018-04-19 09:28:01 +090029import java.nio.ByteBuffer;
Alan Lo5e2b7052018-10-18 14:24:24 +030030import java.nio.ByteOrder;
Carmelo Casconebe5789d2018-04-19 09:28:01 +090031import java.util.Optional;
32
Alan Lo5e2b7052018-10-18 14:24:24 +030033import static org.onosproject.net.pi.model.PiPipeconf.ExtensionType.SPECTRUM_BIN;
Carmelo Casconebe5789d2018-04-19 09:28:01 +090034
35/**
36 * Implementation of the PiPipelineProgrammable behaviour for a Spectrum-based
37 * switch with P4Runtime support.
38 */
39public class SpectrumPipelineProgrammable
40 extends AbstractP4RuntimePipelineProgrammable
41 implements PiPipelineProgrammable {
42
Alan Lo5e2b7052018-10-18 14:24:24 +030043 private static final PiPipeconfId MLNX_FABRIC_PIPECONF_ID =
44 new PiPipeconfId("org.onosproject.pipelines.fabric-mlnx");
Carmelo Casconebe5789d2018-04-19 09:28:01 +090045
46 @Override
47 public ByteBuffer createDeviceDataBuffer(PiPipeconf pipeconf) {
Alan Lo5e2b7052018-10-18 14:24:24 +030048 log.debug("Creating device data buffer for {} in pipeconf {}", SPECTRUM_BIN, pipeconf.id());
49 ByteBuffer deviceData;
50 try {
51 deviceData = extensionBuffer(pipeconf, SPECTRUM_BIN);
52 } catch (ExtensionException e) {
53 log.error("Failed to create device data buffer for {} in pipeconf {}", SPECTRUM_BIN, pipeconf.id());
54 return null;
55 }
56 // flip buffer data so they can be read
57 deviceData.flip();
58 return deviceData.asReadOnlyBuffer();
Carmelo Casconebe5789d2018-04-19 09:28:01 +090059 }
60
61 @Override
62 public Optional<PiPipeconf> getDefaultPipeconf() {
63 return handler().get(PiPipeconfService.class)
Alan Lo5e2b7052018-10-18 14:24:24 +030064 .getPipeconf(MLNX_FABRIC_PIPECONF_ID);
65 }
66
67 private ByteBuffer extensionBuffer(PiPipeconf pipeconf, ExtensionType extType) {
68 if (!pipeconf.extension(extType).isPresent()) {
69 log.warn("Missing extension {} in pipeconf {}", extType, pipeconf.id());
70 throw new ExtensionException();
71 }
72 try {
73 byte[] bytes = IOUtils.toByteArray(pipeconf.extension(extType).get());
74 // Length of the extension + bytes.
75 return ByteBuffer.allocate(bytes.length)
76 .order(ByteOrder.LITTLE_ENDIAN)
77 .put(bytes);
78 } catch (IOException ex) {
79 log.warn("Unable to read extension {} from pipeconf {}: {}",
80 extType, pipeconf.id(), ex.getMessage());
81 throw new ExtensionException();
82 }
83 }
84
85 private static class ExtensionException extends IllegalArgumentException {
Carmelo Casconebe5789d2018-04-19 09:28:01 +090086 }
87}