Added BMv2 demo apps
Change-Id: Id93f6b4f407ddd224fde8e31d79c9426633b4d75
diff --git a/apps/bmv2-demo/ecmp/features.xml b/apps/bmv2-demo/ecmp/features.xml
new file mode 100644
index 0000000..0bfb827
--- /dev/null
+++ b/apps/bmv2-demo/ecmp/features.xml
@@ -0,0 +1,25 @@
+<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+
+<!--
+ ~ Copyright 2016-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.
+ -->
+
+<features xmlns="http://karaf.apache.org/xmlns/features/v1.2.0" name="${project.artifactId}-${project.version}">
+ <feature name="${project.artifactId}" version="${project.version}"
+ description="${project.description}">
+ <bundle>mvn:${project.groupId}/${project.artifactId}/${project.version}</bundle>
+ <bundle>mvn:${project.groupId}/onos-app-bmv2-demo-common/${project.version}</bundle>
+ </feature>
+</features>
diff --git a/apps/bmv2-demo/ecmp/pom.xml b/apps/bmv2-demo/ecmp/pom.xml
new file mode 100644
index 0000000..3044283
--- /dev/null
+++ b/apps/bmv2-demo/ecmp/pom.xml
@@ -0,0 +1,50 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ ~ Copyright 2016-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.
+ -->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+
+ <parent>
+ <artifactId>onos-app-bmv2-demo</artifactId>
+ <groupId>org.onosproject</groupId>
+ <version>1.6.0-SNAPSHOT</version>
+ <relativePath>../pom.xml</relativePath>
+ </parent>
+
+ <artifactId>onos-app-bmv2-demo-ecmp</artifactId>
+
+ <packaging>bundle</packaging>
+
+ <properties>
+ <onos.app.name>org.onosproject.bmv2-ecmp-fabric</onos.app.name>
+ <onos.app.title>P4/BMv2 Demo Fabric App v1 (ECMP)</onos.app.title>
+ <onos.app.category>Traffic Steering</onos.app.category>
+ <onos.app.url>http://onosproject.org</onos.app.url>
+ <onos.app.readme>P4/BMv2 demo application with ECMP support for a 2-stage clos fabric topology</onos.app.readme>
+ </properties>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.onosproject</groupId>
+ <artifactId>onos-app-bmv2-demo-common</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ </dependencies>
+
+</project>
\ No newline at end of file
diff --git a/apps/bmv2-demo/ecmp/src/main/java/org/onosproject/bmv2/demo/app/ecmp/EcmpFabricApp.java b/apps/bmv2-demo/ecmp/src/main/java/org/onosproject/bmv2/demo/app/ecmp/EcmpFabricApp.java
new file mode 100644
index 0000000..e028458
--- /dev/null
+++ b/apps/bmv2-demo/ecmp/src/main/java/org/onosproject/bmv2/demo/app/ecmp/EcmpFabricApp.java
@@ -0,0 +1,248 @@
+/*
+ * Copyright 2016-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.
+ */
+
+package org.onosproject.bmv2.demo.app.ecmp;
+
+import com.eclipsesource.json.Json;
+import com.eclipsesource.json.JsonObject;
+import com.google.common.collect.Lists;
+import org.apache.commons.lang3.tuple.Pair;
+import org.apache.felix.scr.annotations.Component;
+import org.onosproject.bmv2.api.context.Bmv2Configuration;
+import org.onosproject.bmv2.api.context.Bmv2DefaultConfiguration;
+import org.onosproject.bmv2.api.context.Bmv2DeviceContext;
+import org.onosproject.bmv2.demo.app.common.AbstractUpgradableFabricApp;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.Host;
+import org.onosproject.net.Path;
+import org.onosproject.net.Port;
+import org.onosproject.net.PortNumber;
+import org.onosproject.net.flow.DefaultTrafficSelector;
+import org.onosproject.net.flow.DefaultTrafficTreatment;
+import org.onosproject.net.flow.FlowRule;
+import org.onosproject.net.flow.TrafficTreatment;
+import org.onosproject.net.flow.criteria.ExtensionSelector;
+import org.onosproject.net.flow.instructions.ExtensionTreatment;
+import org.onosproject.net.topology.DefaultTopologyVertex;
+import org.onosproject.net.topology.Topology;
+import org.onosproject.net.topology.TopologyGraph;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+import static java.util.stream.Collectors.toSet;
+import static org.onlab.packet.EthType.EtherType.IPV4;
+import static org.onosproject.bmv2.demo.app.ecmp.EcmpGroupTreatmentBuilder.groupIdOf;
+import static org.onosproject.bmv2.demo.app.ecmp.EcmpInterpreter.ECMP_GROUP_TABLE;
+import static org.onosproject.bmv2.demo.app.ecmp.EcmpInterpreter.TABLE0;
+
+/**
+ * Implementation of an upgradable fabric app for the ECMP configuration.
+ */
+@Component(immediate = true)
+public class EcmpFabricApp extends AbstractUpgradableFabricApp {
+
+ private static final String APP_NAME = "org.onosproject.bmv2-ecmp-fabric";
+ private static final String MODEL_NAME = "ECMP";
+ private static final String JSON_CONFIG_PATH = "/ecmp.json";
+
+ private static final Bmv2Configuration ECMP_CONFIGURATION = loadConfiguration();
+ private static final EcmpInterpreter ECMP_INTERPRETER = new EcmpInterpreter();
+ protected static final Bmv2DeviceContext ECMP_CONTEXT = new Bmv2DeviceContext(ECMP_CONFIGURATION, ECMP_INTERPRETER);
+
+ public EcmpFabricApp() {
+ super(APP_NAME, MODEL_NAME, ECMP_CONTEXT);
+ }
+
+ @Override
+ public boolean initDevice(DeviceId deviceId) {
+ // Nothing to do.
+ return true;
+ }
+
+ @Override
+ public List<FlowRule> generateLeafRules(DeviceId leaf, Host srcHost, Collection<Host> dstHosts,
+ Collection<DeviceId> availableSpines, Topology topo)
+ throws FlowRuleGeneratorException {
+
+ // Get ports which connect this leaf switch to hosts.
+ Set<PortNumber> hostPorts = deviceService.getPorts(leaf)
+ .stream()
+ .filter(port -> !isFabricPort(port, topo))
+ .map(Port::number)
+ .collect(Collectors.toSet());
+
+ // Get ports which connect this leaf to the given available spines.
+ TopologyGraph graph = topologyService.getGraph(topo);
+ Set<PortNumber> fabricPorts = graph.getEdgesFrom(new DefaultTopologyVertex(leaf))
+ .stream()
+ .filter(e -> availableSpines.contains(e.dst().deviceId()))
+ .map(e -> e.link().src().port())
+ .collect(Collectors.toSet());
+
+ if (hostPorts.size() != 1 || fabricPorts.size() == 0) {
+ log.error("Leaf switch has invalid port configuration: hostPorts={}, fabricPorts={}",
+ hostPorts.size(), fabricPorts.size());
+ throw new FlowRuleGeneratorException();
+ }
+ PortNumber hostPort = hostPorts.iterator().next();
+
+ List<FlowRule> rules = Lists.newArrayList();
+
+ TrafficTreatment treatment;
+ if (fabricPorts.size() > 1) {
+ // Do ECMP.
+ Pair<ExtensionTreatment, List<FlowRule>> result = provisionEcmpTreatment(leaf, fabricPorts);
+ rules.addAll(result.getRight());
+ ExtensionTreatment extTreatment = result.getLeft();
+ treatment = DefaultTrafficTreatment.builder().extension(extTreatment, leaf).build();
+ } else {
+ // Output on port.
+ PortNumber outPort = fabricPorts.iterator().next();
+ treatment = DefaultTrafficTreatment.builder().setOutput(outPort).build();
+ }
+
+ // From srHost to dstHosts.
+ for (Host dstHost : dstHosts) {
+ FlowRule rule = flowRuleBuilder(leaf, TABLE0)
+ .withSelector(
+ DefaultTrafficSelector.builder()
+ .matchInPort(hostPort)
+ .matchEthType(IPV4.ethType().toShort())
+ .matchEthSrc(srcHost.mac())
+ .matchEthDst(dstHost.mac())
+ .build())
+ .withTreatment(treatment)
+ .build();
+ rules.add(rule);
+ }
+
+ // From fabric ports to this leaf host.
+ for (PortNumber port : fabricPorts) {
+ FlowRule rule = flowRuleBuilder(leaf, TABLE0)
+ .withSelector(
+ DefaultTrafficSelector.builder()
+ .matchInPort(port)
+ .matchEthType(IPV4.ethType().toShort())
+ .matchEthDst(srcHost.mac())
+ .build())
+ .withTreatment(
+ DefaultTrafficTreatment.builder()
+ .setOutput(hostPort)
+ .build())
+ .build();
+ rules.add(rule);
+ }
+
+ return rules;
+ }
+
+ @Override
+ public List<FlowRule> generateSpineRules(DeviceId deviceId, Collection<Host> dstHosts, Topology topo)
+ throws FlowRuleGeneratorException {
+
+ List<FlowRule> rules = Lists.newArrayList();
+
+ // for each host
+ for (Host dstHost : dstHosts) {
+
+ Set<Path> paths = topologyService.getPaths(topo, deviceId, dstHost.location().deviceId());
+
+ if (paths.size() == 0) {
+ log.warn("Can't find any path between spine {} and host {}", deviceId, dstHost);
+ throw new FlowRuleGeneratorException();
+ }
+
+ TrafficTreatment treatment;
+
+ if (paths.size() == 1) {
+ // Only one path, do output on port.
+ PortNumber port = paths.iterator().next().src().port();
+ treatment = DefaultTrafficTreatment.builder().setOutput(port).build();
+ } else {
+ // Multiple paths, do ECMP.
+ Set<PortNumber> portNumbers = paths.stream().map(p -> p.src().port()).collect(toSet());
+ Pair<ExtensionTreatment, List<FlowRule>> result = provisionEcmpTreatment(deviceId, portNumbers);
+ rules.addAll(result.getRight());
+ treatment = DefaultTrafficTreatment.builder().extension(result.getLeft(), deviceId).build();
+ }
+
+ FlowRule rule = flowRuleBuilder(deviceId, TABLE0)
+ .withSelector(
+ DefaultTrafficSelector.builder()
+ .matchEthType(IPV4.ethType().toShort())
+ .matchEthDst(dstHost.mac())
+ .build())
+ .withTreatment(treatment)
+ .build();
+
+ rules.add(rule);
+ }
+
+ return rules;
+ }
+
+ private Pair<ExtensionTreatment, List<FlowRule>> provisionEcmpTreatment(DeviceId deviceId,
+ Set<PortNumber> fabricPorts)
+ throws FlowRuleGeneratorException {
+
+ // Install ECMP group table entries that map from hash values to actual fabric ports...
+ int groupId = groupIdOf(deviceId, fabricPorts);
+ int groupSize = fabricPorts.size();
+ Iterator<PortNumber> portIterator = fabricPorts.iterator();
+ List<FlowRule> rules = Lists.newArrayList();
+ for (short i = 0; i < groupSize; i++) {
+ ExtensionSelector extSelector = new EcmpGroupTableSelectorBuilder()
+ .withGroupId(groupId)
+ .withSelector(i)
+ .build();
+ FlowRule rule = flowRuleBuilder(deviceId, ECMP_GROUP_TABLE)
+ .withSelector(
+ DefaultTrafficSelector.builder()
+ .extension(extSelector, deviceId)
+ .build())
+ .withTreatment(
+ DefaultTrafficTreatment.builder()
+ .setOutput(portIterator.next())
+ .build())
+ .build();
+ rules.add(rule);
+ }
+
+ ExtensionTreatment extTreatment = new EcmpGroupTreatmentBuilder()
+ .withGroupId(groupId)
+ .withGroupSize(groupSize)
+ .build();
+
+ return Pair.of(extTreatment, rules);
+ }
+
+ private static Bmv2Configuration loadConfiguration() {
+ try {
+ JsonObject json = Json.parse(new BufferedReader(new InputStreamReader(
+ EcmpFabricApp.class.getResourceAsStream(JSON_CONFIG_PATH)))).asObject();
+ return Bmv2DefaultConfiguration.parse(json);
+ } catch (IOException e) {
+ throw new RuntimeException("Unable to load configuration", e);
+ }
+ }
+}
\ No newline at end of file
diff --git a/apps/bmv2-demo/ecmp/src/main/java/org/onosproject/bmv2/demo/app/ecmp/EcmpGroupTableSelectorBuilder.java b/apps/bmv2-demo/ecmp/src/main/java/org/onosproject/bmv2/demo/app/ecmp/EcmpGroupTableSelectorBuilder.java
new file mode 100644
index 0000000..e9ce0cf
--- /dev/null
+++ b/apps/bmv2-demo/ecmp/src/main/java/org/onosproject/bmv2/demo/app/ecmp/EcmpGroupTableSelectorBuilder.java
@@ -0,0 +1,88 @@
+/*
+ * Copyright 2016-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.
+ */
+
+package org.onosproject.bmv2.demo.app.ecmp;
+
+import com.google.common.collect.ImmutableMap;
+import org.onlab.util.ImmutableByteSequence;
+import org.onosproject.bmv2.api.context.Bmv2HeaderTypeModel;
+import org.onosproject.bmv2.api.runtime.Bmv2ExactMatchParam;
+import org.onosproject.bmv2.api.runtime.Bmv2ExtensionSelector;
+import org.onosproject.bmv2.api.utils.Bmv2TranslatorUtils;
+import org.onosproject.net.flow.criteria.ExtensionSelector;
+
+import static org.onosproject.bmv2.api.utils.Bmv2TranslatorUtils.fitByteSequence;
+import static org.onosproject.bmv2.demo.app.ecmp.EcmpFabricApp.ECMP_CONTEXT;
+import static org.onosproject.bmv2.demo.app.ecmp.EcmpInterpreter.*;
+
+/**
+ * Builder of ECMP group table extension selector.
+ */
+public class EcmpGroupTableSelectorBuilder {
+
+ private int groupId;
+ private int selector;
+
+ /**
+ * Sets the ECMP group ID.
+ *
+ * @param groupId an integer value
+ * @return this
+ */
+ public EcmpGroupTableSelectorBuilder withGroupId(int groupId) {
+ this.groupId = groupId;
+ return this;
+ }
+
+ /**
+ * Sets the ECMP selector.
+ *
+ * @param selector an integer value
+ * @return this
+ */
+ public EcmpGroupTableSelectorBuilder withSelector(int selector) {
+ this.selector = selector;
+ return this;
+ }
+
+ /**
+ * Returns a new extension selector.
+ *
+ * @return an extension selector
+ */
+ public ExtensionSelector build() {
+ Bmv2HeaderTypeModel headerTypeModel = ECMP_CONTEXT.configuration().headerType(ECMP_METADATA_T);
+ int groupIdBitWidth = headerTypeModel.field(GROUP_ID).bitWidth();
+ int selectorBitWidth = headerTypeModel.field(SELECTOR).bitWidth();
+
+ try {
+ ImmutableByteSequence groupIdBs = fitByteSequence(ImmutableByteSequence.copyFrom(groupId),
+ groupIdBitWidth);
+ ImmutableByteSequence selectorBs = fitByteSequence(ImmutableByteSequence.copyFrom(selector),
+ selectorBitWidth);
+
+ Bmv2ExactMatchParam groupIdMatch = new Bmv2ExactMatchParam(groupIdBs);
+ Bmv2ExactMatchParam hashMatch = new Bmv2ExactMatchParam(selectorBs);
+
+ return new Bmv2ExtensionSelector(ImmutableMap.of(
+ ECMP_METADATA + "." + GROUP_ID, groupIdMatch,
+ ECMP_METADATA + "." + SELECTOR, hashMatch));
+
+ } catch (Bmv2TranslatorUtils.ByteSequenceFitException e) {
+ throw new RuntimeException(e);
+ }
+ }
+}
diff --git a/apps/bmv2-demo/ecmp/src/main/java/org/onosproject/bmv2/demo/app/ecmp/EcmpGroupTreatmentBuilder.java b/apps/bmv2-demo/ecmp/src/main/java/org/onosproject/bmv2/demo/app/ecmp/EcmpGroupTreatmentBuilder.java
new file mode 100644
index 0000000..eee8c59
--- /dev/null
+++ b/apps/bmv2-demo/ecmp/src/main/java/org/onosproject/bmv2/demo/app/ecmp/EcmpGroupTreatmentBuilder.java
@@ -0,0 +1,107 @@
+/*
+ * Copyright 2016-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.
+ */
+
+package org.onosproject.bmv2.demo.app.ecmp;
+
+import com.google.common.collect.Maps;
+import org.onlab.util.ImmutableByteSequence;
+import org.onosproject.bmv2.api.context.Bmv2ActionModel;
+import org.onosproject.bmv2.api.runtime.Bmv2Action;
+import org.onosproject.bmv2.api.runtime.Bmv2ExtensionTreatment;
+import org.onosproject.bmv2.api.utils.Bmv2TranslatorUtils;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.PortNumber;
+import org.onosproject.net.flow.instructions.ExtensionTreatment;
+
+import java.util.Map;
+import java.util.Set;
+
+import static org.onosproject.bmv2.api.utils.Bmv2TranslatorUtils.fitByteSequence;
+import static org.onosproject.bmv2.demo.app.ecmp.EcmpFabricApp.ECMP_CONTEXT;
+import static org.onosproject.bmv2.demo.app.ecmp.EcmpInterpreter.*;
+
+/**
+ * Builder of ECMP extension treatments.
+ */
+public class EcmpGroupTreatmentBuilder {
+
+ private static final Map<DeviceId, Map<Set<PortNumber>, Short>> DEVICE_GROUP_ID_MAP = Maps.newHashMap();
+ private int groupId;
+ private int groupSize;
+
+ /**
+ * Sets the group ID.
+ *
+ * @param groupId an integer value
+ * @return this
+ */
+ public EcmpGroupTreatmentBuilder withGroupId(int groupId) {
+ this.groupId = groupId;
+ return this;
+ }
+
+ /**
+ * Sets the group size.
+ *
+ * @param groupSize an integer value
+ * @return this
+ */
+ public EcmpGroupTreatmentBuilder withGroupSize(int groupSize) {
+ this.groupSize = groupSize;
+ return this;
+ }
+
+ /**
+ * Returns a new extension treatment.
+ *
+ * @return an extension treatment
+ */
+ public ExtensionTreatment build() {
+ Bmv2ActionModel actionModel = ECMP_CONTEXT.configuration().action(ECMP_GROUP);
+ int groupIdBitWidth = actionModel.runtimeData(GROUP_ID).bitWidth();
+ int groupSizeBitWidth = actionModel.runtimeData(GROUP_SIZE).bitWidth();
+
+ try {
+ ImmutableByteSequence groupIdBs = fitByteSequence(ImmutableByteSequence.copyFrom(groupId), groupIdBitWidth);
+ ImmutableByteSequence groupSizeBs = fitByteSequence(ImmutableByteSequence.copyFrom(groupSize),
+ groupSizeBitWidth);
+
+ return new Bmv2ExtensionTreatment(Bmv2Action.builder()
+ .withName(ECMP_GROUP)
+ .addParameter(groupIdBs)
+ .addParameter(groupSizeBs)
+ .build());
+
+ } catch (Bmv2TranslatorUtils.ByteSequenceFitException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ /**
+ * Returns a group ID for the given device and set of ports.
+ *
+ * @param deviceId a device ID
+ * @param ports a set of ports
+ * @return an integer value
+ */
+ public static int groupIdOf(DeviceId deviceId, Set<PortNumber> ports) {
+ DEVICE_GROUP_ID_MAP.putIfAbsent(deviceId, Maps.newHashMap());
+ // Counts the number of unique portNumber sets for each deviceId.
+ // Each distinct set of portNumbers will have a unique ID.
+ return DEVICE_GROUP_ID_MAP.get(deviceId).computeIfAbsent(ports, (pp) ->
+ (short) (DEVICE_GROUP_ID_MAP.get(deviceId).size() + 1));
+ }
+}
diff --git a/apps/bmv2-demo/ecmp/src/main/java/org/onosproject/bmv2/demo/app/ecmp/EcmpInterpreter.java b/apps/bmv2-demo/ecmp/src/main/java/org/onosproject/bmv2/demo/app/ecmp/EcmpInterpreter.java
new file mode 100644
index 0000000..5585a64
--- /dev/null
+++ b/apps/bmv2-demo/ecmp/src/main/java/org/onosproject/bmv2/demo/app/ecmp/EcmpInterpreter.java
@@ -0,0 +1,124 @@
+/*
+ * Copyright 2016-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.
+ */
+
+package org.onosproject.bmv2.demo.app.ecmp;
+
+import com.google.common.collect.ImmutableBiMap;
+import org.onlab.util.ImmutableByteSequence;
+import org.onosproject.bmv2.api.context.Bmv2Configuration;
+import org.onosproject.bmv2.api.context.Bmv2Interpreter;
+import org.onosproject.bmv2.api.context.Bmv2InterpreterException;
+import org.onosproject.bmv2.api.runtime.Bmv2Action;
+import org.onosproject.bmv2.api.utils.Bmv2TranslatorUtils;
+import org.onosproject.net.PortNumber;
+import org.onosproject.net.flow.TrafficTreatment;
+import org.onosproject.net.flow.criteria.Criterion;
+import org.onosproject.net.flow.instructions.Instruction;
+
+import static org.onosproject.bmv2.api.utils.Bmv2TranslatorUtils.fitByteSequence;
+import static org.onosproject.net.PortNumber.CONTROLLER;
+import static org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
+
+/**
+ * Implementation of a BMv2 interpreter for the ecmp.json configuration.
+ */
+public class EcmpInterpreter implements Bmv2Interpreter {
+
+ protected static final String ECMP_METADATA_T = "ecmp_metadata_t";
+ protected static final String ECMP_METADATA = "ecmp_metadata";
+ protected static final String SELECTOR = "selector";
+ protected static final String GROUP_ID = "groupId";
+ protected static final String GROUP_SIZE = "groupSize";
+ protected static final String ECMP_GROUP = "ecmp_group";
+ protected static final String ECMP_GROUP_TABLE = "ecmp_group_table";
+ protected static final String TABLE0 = "table0";
+ protected static final String SEND_TO_CPU = "send_to_cpu";
+ protected static final String DROP = "_drop";
+ protected static final String SET_EGRESS_PORT = "set_egress_port";
+ protected static final String PORT = "port";
+
+ private static final ImmutableBiMap<Criterion.Type, String> CRITERION_TYPE_MAP = ImmutableBiMap.of(
+ Criterion.Type.IN_PORT, "standard_metadata.ingress_port",
+ Criterion.Type.ETH_DST, "ethernet.dstAddr",
+ Criterion.Type.ETH_SRC, "ethernet.srcAddr",
+ Criterion.Type.ETH_TYPE, "ethernet.etherType");
+
+ private static final ImmutableBiMap<Integer, String> TABLE_ID_MAP = ImmutableBiMap.of(
+ 0, TABLE0,
+ 1, ECMP_GROUP_TABLE);
+
+ @Override
+ public ImmutableBiMap<Integer, String> tableIdMap() {
+ return TABLE_ID_MAP;
+ }
+
+ @Override
+ public ImmutableBiMap<Criterion.Type, String> criterionTypeMap() {
+ return CRITERION_TYPE_MAP;
+ }
+
+ @Override
+ public Bmv2Action mapTreatment(TrafficTreatment treatment, Bmv2Configuration configuration)
+ throws Bmv2InterpreterException {
+
+ if (treatment.allInstructions().size() == 0) {
+ // No instructions means drop for us.
+ return actionWithName(DROP);
+ } else if (treatment.allInstructions().size() > 1) {
+ // Otherwise, we understand treatments with only 1 instruction.
+ throw new Bmv2InterpreterException("Treatment has multiple instructions");
+ }
+
+ Instruction instruction = treatment.allInstructions().get(0);
+
+ switch (instruction.type()) {
+ case OUTPUT:
+ OutputInstruction outInstruction = (OutputInstruction) instruction;
+ PortNumber port = outInstruction.port();
+ if (!port.isLogical()) {
+ return buildEgressAction(port, configuration);
+ } else if (port.equals(CONTROLLER)) {
+ return actionWithName(SEND_TO_CPU);
+ } else {
+ throw new Bmv2InterpreterException("Egress on logical port not supported: " + port);
+ }
+ case NOACTION:
+ return actionWithName(DROP);
+ default:
+ throw new Bmv2InterpreterException("Instruction type not supported: " + instruction.type().name());
+ }
+ }
+
+ private static Bmv2Action buildEgressAction(PortNumber port, Bmv2Configuration configuration)
+ throws Bmv2InterpreterException {
+
+ int portBitWidth = configuration.action(SET_EGRESS_PORT).runtimeData(PORT).bitWidth();
+
+ try {
+ ImmutableByteSequence portBs = fitByteSequence(ImmutableByteSequence.copyFrom(port.toLong()), portBitWidth);
+ return Bmv2Action.builder()
+ .withName(SET_EGRESS_PORT)
+ .addParameter(portBs)
+ .build();
+ } catch (Bmv2TranslatorUtils.ByteSequenceFitException e) {
+ throw new Bmv2InterpreterException(e.getMessage());
+ }
+ }
+
+ private static Bmv2Action actionWithName(String name) {
+ return Bmv2Action.builder().withName(name).build();
+ }
+}
diff --git a/apps/bmv2-demo/ecmp/src/main/java/org/onosproject/bmv2/demo/app/ecmp/package-info.java b/apps/bmv2-demo/ecmp/src/main/java/org/onosproject/bmv2/demo/app/ecmp/package-info.java
new file mode 100644
index 0000000..2980242
--- /dev/null
+++ b/apps/bmv2-demo/ecmp/src/main/java/org/onosproject/bmv2/demo/app/ecmp/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2016-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.
+ */
+
+/**
+ * BMv2 demo app for the ECMP configuration.
+ */
+package org.onosproject.bmv2.demo.app.ecmp;
\ No newline at end of file
diff --git a/apps/bmv2-demo/ecmp/src/main/resources/ecmp.json b/apps/bmv2-demo/ecmp/src/main/resources/ecmp.json
new file mode 120000
index 0000000..acdff05
--- /dev/null
+++ b/apps/bmv2-demo/ecmp/src/main/resources/ecmp.json
@@ -0,0 +1 @@
+/Users/carmelo/workspace/onos-p4-dev/p4src/build/ecmp.json
\ No newline at end of file