Added P4Runtime-based Barefoot Tofino driver
Change-Id: I09ba8dd4468fa5a792ca481921e8a51dad49702e
diff --git a/drivers/barefoot/BUCK b/drivers/barefoot/BUCK
new file mode 100644
index 0000000..f38ad9f
--- /dev/null
+++ b/drivers/barefoot/BUCK
@@ -0,0 +1,32 @@
+GRPC_VER = '1.3.0'
+
+COMPILE_DEPS = [
+ '//lib:CORE_DEPS',
+ '//lib:minimal-json',
+ '//incubator/bmv2/model:onos-incubator-bmv2-model',
+ '//protocols/p4runtime/api:onos-protocols-p4runtime-api',
+ '//drivers/p4runtime:onos-drivers-p4runtime',
+ '//incubator/grpc-dependencies:grpc-core-repkg-' + GRPC_VER,
+ '//lib:grpc-netty-' + GRPC_VER,
+]
+
+BUNDLES = [
+ ':onos-drivers-barefoot',
+ '//incubator/bmv2/model:onos-incubator-bmv2-model',
+]
+
+osgi_jar(
+ deps = COMPILE_DEPS,
+)
+
+onos_app (
+ app_name = 'org.onosproject.drivers.barefoot',
+ title = 'Barefoot Drivers',
+ category = 'Drivers',
+ url = 'http://onosproject.org',
+ description = 'Adds support for Barefoot-based devices',
+ included_bundles = BUNDLES,
+ required_apps = [
+ 'org.onosproject.drivers.p4runtime',
+ ],
+)
diff --git a/drivers/barefoot/src/main/java/org/onosproject/drivers/barefoot/BarefootDriversLoader.java b/drivers/barefoot/src/main/java/org/onosproject/drivers/barefoot/BarefootDriversLoader.java
new file mode 100644
index 0000000..41ab063
--- /dev/null
+++ b/drivers/barefoot/src/main/java/org/onosproject/drivers/barefoot/BarefootDriversLoader.java
@@ -0,0 +1,31 @@
+/*
+ * Copyright 2017-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.drivers.barefoot;
+
+import org.apache.felix.scr.annotations.Component;
+import org.onosproject.net.driver.AbstractDriverLoader;
+
+/**
+ * Loader for the Barefoot device drivers.
+ */
+@Component(immediate = true)
+public class BarefootDriversLoader extends AbstractDriverLoader {
+
+ public BarefootDriversLoader() {
+ super("/barefoot-drivers.xml");
+ }
+}
diff --git a/drivers/barefoot/src/main/java/org/onosproject/drivers/barefoot/TofinoPipelineProgrammable.java b/drivers/barefoot/src/main/java/org/onosproject/drivers/barefoot/TofinoPipelineProgrammable.java
new file mode 100644
index 0000000..197b681
--- /dev/null
+++ b/drivers/barefoot/src/main/java/org/onosproject/drivers/barefoot/TofinoPipelineProgrammable.java
@@ -0,0 +1,97 @@
+/*
+ * Copyright 2017-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.drivers.barefoot;
+
+import com.google.common.collect.Lists;
+import org.apache.commons.io.IOUtils;
+import org.onosproject.drivers.p4runtime.AbstractP4RuntimePipelineProgrammable;
+import org.onosproject.net.pi.model.PiPipeconf;
+import org.onosproject.net.pi.model.PiPipeconf.ExtensionType;
+
+import java.io.IOException;
+import java.nio.Buffer;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.nio.charset.StandardCharsets;
+import java.util.List;
+import java.util.Optional;
+
+import static org.onosproject.net.pi.model.PiPipeconf.ExtensionType.TOFINO_BIN;
+import static org.onosproject.net.pi.model.PiPipeconf.ExtensionType.TOFINO_CONTEXT_JSON;
+
+/**
+ * Implementation of the PiPipelineProgrammable behaviour for a Tofino-based switch.
+ */
+public class TofinoPipelineProgrammable extends AbstractP4RuntimePipelineProgrammable {
+
+ @Override
+ public Optional<PiPipeconf> getDefaultPipeconf() {
+ return Optional.empty();
+ }
+
+ @Override
+ public ByteBuffer createDeviceDataBuffer(PiPipeconf pipeconf) {
+
+ List<ByteBuffer> buffers = Lists.newLinkedList();
+ try {
+ buffers.add(nameBuffer(pipeconf));
+ buffers.add(extensionBuffer(pipeconf, TOFINO_BIN));
+ buffers.add(extensionBuffer(pipeconf, TOFINO_CONTEXT_JSON));
+ } catch (ExtensionException e) {
+ return null;
+ }
+
+ // Concatenate buffers (flip so they can be read).
+ int len = buffers.stream().mapToInt(Buffer::limit).sum();
+ ByteBuffer deviceData = ByteBuffer.allocate(len);
+ buffers.forEach(b -> deviceData.put((ByteBuffer) b.flip()));
+ deviceData.flip();
+
+ return deviceData.asReadOnlyBuffer();
+ }
+
+ private ByteBuffer nameBuffer(PiPipeconf pipeconf) {
+ // Length of the name + name.
+ String name = pipeconf.id().toString();
+ return ByteBuffer.allocate(Integer.BYTES + name.length())
+ .order(ByteOrder.LITTLE_ENDIAN)
+ .putInt(name.length())
+ .put(name.getBytes(StandardCharsets.UTF_8));
+ }
+
+ private ByteBuffer extensionBuffer(PiPipeconf pipeconf, ExtensionType extType) {
+ if (!pipeconf.extension(extType).isPresent()) {
+ log.warn("Missing extension {} in pipeconf {}", extType, pipeconf.id());
+ throw new ExtensionException();
+ }
+ try {
+ byte[] bytes = IOUtils.toByteArray(pipeconf.extension(extType).get());
+ // Length of the extension + bytes.
+ return ByteBuffer.allocate(Integer.BYTES + bytes.length)
+ .order(ByteOrder.LITTLE_ENDIAN)
+ .putInt(bytes.length)
+ .put(bytes);
+ } catch (IOException ex) {
+ log.warn("Unable to read extension {} from pipeconf {}: {}",
+ extType, pipeconf.id(), ex.getMessage());
+ throw new ExtensionException();
+ }
+ }
+
+ private static class ExtensionException extends IllegalArgumentException {
+ }
+}
diff --git a/drivers/barefoot/src/main/java/org/onosproject/drivers/barefoot/package-info.java b/drivers/barefoot/src/main/java/org/onosproject/drivers/barefoot/package-info.java
new file mode 100644
index 0000000..649731e
--- /dev/null
+++ b/drivers/barefoot/src/main/java/org/onosproject/drivers/barefoot/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2017-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * Package for Barefoot device drivers.
+ */
+package org.onosproject.drivers.barefoot;
\ No newline at end of file
diff --git a/drivers/barefoot/src/main/resources/barefoot-drivers.xml b/drivers/barefoot/src/main/resources/barefoot-drivers.xml
new file mode 100644
index 0000000..ee56455
--- /dev/null
+++ b/drivers/barefoot/src/main/resources/barefoot-drivers.xml
@@ -0,0 +1,23 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ ~ Copyright 2017-present Open Networking Laboratory
+ ~
+ ~ Licensed under the Apache License, Version 2.0 (the "License");
+ ~ you may not use this file except in compliance with the License.
+ ~ You may obtain a copy of the License at
+ ~
+ ~ http://www.apache.org/licenses/LICENSE-2.0
+ ~
+ ~ Unless required by applicable law or agreed to in writing, software
+ ~ distributed under the License is distributed on an "AS IS" BASIS,
+ ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ ~ See the License for the specific language governing permissions and
+ ~ limitations under the License.
+ -->
+<drivers>
+ <driver name="tofino" manufacturer="Barefoot Networks" hwVersion="1.0" swVersion="1.0" extends="p4runtime">
+ <behaviour api="org.onosproject.net.pi.model.PiPipelineProgrammable"
+ impl="org.onosproject.drivers.barefoot.TofinoPipelineProgrammable"/>
+ </driver>
+</drivers>
+
diff --git a/drivers/bmv2/BUCK b/drivers/bmv2/BUCK
index c8224f5..ed32104 100644
--- a/drivers/bmv2/BUCK
+++ b/drivers/bmv2/BUCK
@@ -5,7 +5,6 @@
'//lib:minimal-json',
'//protocols/p4runtime/api:onos-protocols-p4runtime-api',
'//incubator/bmv2/model:onos-incubator-bmv2-model',
- '//drivers/default:onos-drivers-default',
'//drivers/p4runtime:onos-drivers-p4runtime',
'//incubator/grpc-dependencies:grpc-core-repkg-' + GRPC_VER,
'//lib:grpc-netty-' + GRPC_VER,
diff --git a/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/Bmv2PipelineProgrammable.java b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/Bmv2PipelineProgrammable.java
index bf88238..bf7061f 100644
--- a/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/Bmv2PipelineProgrammable.java
+++ b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/Bmv2PipelineProgrammable.java
@@ -16,72 +16,34 @@
package org.onosproject.drivers.bmv2;
-import org.onlab.util.SharedExecutors;
-import org.onosproject.net.DeviceId;
-import org.onosproject.net.driver.AbstractHandlerBehaviour;
+import org.apache.commons.io.IOUtils;
+import org.onosproject.drivers.p4runtime.AbstractP4RuntimePipelineProgrammable;
import org.onosproject.net.pi.model.PiPipeconf;
-import org.onosproject.net.pi.model.PiPipelineProgrammable;
-import org.onosproject.p4runtime.api.P4RuntimeClient;
-import org.onosproject.p4runtime.api.P4RuntimeController;
import org.onosproject.pipelines.basic.PipeconfLoader;
-import org.slf4j.Logger;
+import java.io.IOException;
+import java.nio.ByteBuffer;
import java.util.Optional;
-import java.util.concurrent.CompletableFuture;
-import java.util.concurrent.ExecutionException;
import static org.onosproject.net.pi.model.PiPipeconf.ExtensionType.BMV2_JSON;
-import static org.slf4j.LoggerFactory.getLogger;
/**
- * Implementation of the PiPipelineProgrammable for BMv2.
+ * Implementation of the PiPipelineProgrammable behavior for BMv2.
*/
-public class Bmv2PipelineProgrammable extends AbstractHandlerBehaviour implements PiPipelineProgrammable {
-
- private final Logger log = getLogger(getClass());
+public class Bmv2PipelineProgrammable extends AbstractP4RuntimePipelineProgrammable {
@Override
- public CompletableFuture<Boolean> deployPipeconf(PiPipeconf pipeconf) {
-
- CompletableFuture<Boolean> result = new CompletableFuture<>();
-
- SharedExecutors.getPoolThreadExecutor().submit(() -> result.complete(doDeployConfig(pipeconf)));
-
- return result;
- }
-
- private boolean doDeployConfig(PiPipeconf pipeconf) {
-
- P4RuntimeController controller = handler().get(P4RuntimeController.class);
-
- DeviceId deviceId = handler().data().deviceId();
-
- if (!controller.hasClient(deviceId)) {
- log.warn("Unable to find client for {}, aborting pipeconf deploy", deviceId);
- return false;
-
+ public ByteBuffer createDeviceDataBuffer(PiPipeconf pipeconf) {
+ if (!pipeconf.extension(BMV2_JSON).isPresent()) {
+ log.warn("Missing extension {} in pipeconf {}", BMV2_JSON, pipeconf.id());
+ return null;
}
-
- P4RuntimeClient client = controller.getClient(deviceId);
-
try {
- if (!client.setPipelineConfig(pipeconf, BMV2_JSON).get()) {
- log.warn("Unable to deploy pipeconf {} to {}", pipeconf.id(), deviceId);
- return false;
- }
-
- // It would be more logical to have this performed at device handshake, but P4runtime would reject any
- // command if a P4info has not been set first.
- if (!client.initStreamChannel().get()) {
- log.warn("Unable to init stream channel to {}.", deviceId);
- return false;
- }
-
- } catch (InterruptedException | ExecutionException e) {
- throw new RuntimeException(e);
+ return ByteBuffer.wrap(IOUtils.toByteArray(pipeconf.extension(BMV2_JSON).get()));
+ } catch (IOException e) {
+ log.warn("Unable to read {} from pipeconf {}", BMV2_JSON, pipeconf.id());
+ return null;
}
-
- return true;
}
@Override
diff --git a/drivers/p4runtime/src/main/java/org/onosproject/drivers/p4runtime/AbstractP4RuntimePipelineProgrammable.java b/drivers/p4runtime/src/main/java/org/onosproject/drivers/p4runtime/AbstractP4RuntimePipelineProgrammable.java
new file mode 100644
index 0000000..1272305
--- /dev/null
+++ b/drivers/p4runtime/src/main/java/org/onosproject/drivers/p4runtime/AbstractP4RuntimePipelineProgrammable.java
@@ -0,0 +1,103 @@
+/*
+ * Copyright 2017-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.drivers.p4runtime;
+
+import org.onlab.util.SharedExecutors;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.driver.AbstractHandlerBehaviour;
+import org.onosproject.net.pi.model.PiPipeconf;
+import org.onosproject.net.pi.model.PiPipelineProgrammable;
+import org.onosproject.p4runtime.api.P4RuntimeClient;
+import org.onosproject.p4runtime.api.P4RuntimeController;
+import org.slf4j.Logger;
+
+import java.nio.ByteBuffer;
+import java.util.Optional;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutionException;
+
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Abstract implementation of the PiPipelineProgrammable behaviours for a P4Runtime device.
+ */
+public abstract class AbstractP4RuntimePipelineProgrammable extends AbstractHandlerBehaviour
+ implements PiPipelineProgrammable {
+
+ protected final Logger log = getLogger(getClass());
+
+ /**
+ * Returns a byte buffer representing the target-specific device data to be used in the SetPipelineConfig message.
+ *
+ * @param pipeconf pipeconf
+ * @return byte buffer
+ */
+ public abstract ByteBuffer createDeviceDataBuffer(PiPipeconf pipeconf);
+
+ @Override
+ public CompletableFuture<Boolean> deployPipeconf(PiPipeconf pipeconf) {
+ return CompletableFuture.supplyAsync(
+ () -> doDeployConfig(pipeconf),
+ SharedExecutors.getPoolThreadExecutor());
+ }
+
+ private boolean doDeployConfig(PiPipeconf pipeconf) {
+
+ DeviceId deviceId = handler().data().deviceId();
+ P4RuntimeController controller = handler().get(P4RuntimeController.class);
+
+ if (!controller.hasClient(deviceId)) {
+ log.warn("Unable to find client for {}, aborting pipeconf deploy", deviceId);
+ return false;
+ }
+ P4RuntimeClient client = controller.getClient(deviceId);
+
+ ByteBuffer deviceDataBuffer = createDeviceDataBuffer(pipeconf);
+ if (deviceDataBuffer == null) {
+ // Hopefully the child class logged the problem.
+ return false;
+ }
+
+ try {
+ if (!client.setPipelineConfig(pipeconf, deviceDataBuffer).get()) {
+ log.warn("Unable to deploy pipeconf {} to {}", pipeconf.id(), deviceId);
+ return false;
+ }
+ } catch (InterruptedException | ExecutionException e) {
+ log.error("Exception while deploying pipeconf to {}", deviceId, e);
+ return false;
+ }
+
+ try {
+ // It would make more sense to init the stream channel once the client
+ // is created, but P4runtime would reject any command if a P4info has
+ // not been set first.
+ if (!client.initStreamChannel().get()) {
+ log.warn("Unable to init stream channel to {}.", deviceId);
+ return false;
+ }
+ } catch (InterruptedException | ExecutionException e) {
+ log.error("Exception while initializing stream channel on {}", deviceId, e);
+ return false;
+ }
+
+ return true;
+ }
+
+ @Override
+ public abstract Optional<PiPipeconf> getDefaultPipeconf();
+}