blob: c5ea2aea1b2e758660fbc408617132fd39a55464 [file] [log] [blame]
Carmelo Casconeefc0a922016-06-14 14:32:33 -07001/*
2 * Copyright 2016-present Open Networking Laboratory
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.bmv2.demo.app.ecmp;
18
19import com.google.common.collect.ImmutableBiMap;
20import org.onlab.util.ImmutableByteSequence;
21import org.onosproject.bmv2.api.context.Bmv2Configuration;
22import org.onosproject.bmv2.api.context.Bmv2Interpreter;
23import org.onosproject.bmv2.api.context.Bmv2InterpreterException;
24import org.onosproject.bmv2.api.runtime.Bmv2Action;
25import org.onosproject.bmv2.api.utils.Bmv2TranslatorUtils;
26import org.onosproject.net.PortNumber;
27import org.onosproject.net.flow.TrafficTreatment;
28import org.onosproject.net.flow.criteria.Criterion;
29import org.onosproject.net.flow.instructions.Instruction;
30
31import static org.onosproject.bmv2.api.utils.Bmv2TranslatorUtils.fitByteSequence;
32import static org.onosproject.net.PortNumber.CONTROLLER;
33import static org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
34
35/**
36 * Implementation of a BMv2 interpreter for the ecmp.json configuration.
37 */
38public class EcmpInterpreter implements Bmv2Interpreter {
39
Carmelo Casconeefc0a922016-06-14 14:32:33 -070040 protected static final String ECMP_METADATA = "ecmp_metadata";
41 protected static final String SELECTOR = "selector";
42 protected static final String GROUP_ID = "groupId";
43 protected static final String GROUP_SIZE = "groupSize";
44 protected static final String ECMP_GROUP = "ecmp_group";
45 protected static final String ECMP_GROUP_TABLE = "ecmp_group_table";
46 protected static final String TABLE0 = "table0";
47 protected static final String SEND_TO_CPU = "send_to_cpu";
48 protected static final String DROP = "_drop";
49 protected static final String SET_EGRESS_PORT = "set_egress_port";
50 protected static final String PORT = "port";
51
52 private static final ImmutableBiMap<Criterion.Type, String> CRITERION_TYPE_MAP = ImmutableBiMap.of(
53 Criterion.Type.IN_PORT, "standard_metadata.ingress_port",
54 Criterion.Type.ETH_DST, "ethernet.dstAddr",
55 Criterion.Type.ETH_SRC, "ethernet.srcAddr",
56 Criterion.Type.ETH_TYPE, "ethernet.etherType");
57
58 private static final ImmutableBiMap<Integer, String> TABLE_ID_MAP = ImmutableBiMap.of(
59 0, TABLE0,
60 1, ECMP_GROUP_TABLE);
61
62 @Override
63 public ImmutableBiMap<Integer, String> tableIdMap() {
64 return TABLE_ID_MAP;
65 }
66
67 @Override
68 public ImmutableBiMap<Criterion.Type, String> criterionTypeMap() {
69 return CRITERION_TYPE_MAP;
70 }
71
72 @Override
73 public Bmv2Action mapTreatment(TrafficTreatment treatment, Bmv2Configuration configuration)
74 throws Bmv2InterpreterException {
75
76 if (treatment.allInstructions().size() == 0) {
77 // No instructions means drop for us.
78 return actionWithName(DROP);
79 } else if (treatment.allInstructions().size() > 1) {
80 // Otherwise, we understand treatments with only 1 instruction.
81 throw new Bmv2InterpreterException("Treatment has multiple instructions");
82 }
83
84 Instruction instruction = treatment.allInstructions().get(0);
85
86 switch (instruction.type()) {
87 case OUTPUT:
88 OutputInstruction outInstruction = (OutputInstruction) instruction;
89 PortNumber port = outInstruction.port();
90 if (!port.isLogical()) {
91 return buildEgressAction(port, configuration);
92 } else if (port.equals(CONTROLLER)) {
93 return actionWithName(SEND_TO_CPU);
94 } else {
95 throw new Bmv2InterpreterException("Egress on logical port not supported: " + port);
96 }
97 case NOACTION:
98 return actionWithName(DROP);
99 default:
100 throw new Bmv2InterpreterException("Instruction type not supported: " + instruction.type().name());
101 }
102 }
103
104 private static Bmv2Action buildEgressAction(PortNumber port, Bmv2Configuration configuration)
105 throws Bmv2InterpreterException {
106
107 int portBitWidth = configuration.action(SET_EGRESS_PORT).runtimeData(PORT).bitWidth();
108
109 try {
110 ImmutableByteSequence portBs = fitByteSequence(ImmutableByteSequence.copyFrom(port.toLong()), portBitWidth);
111 return Bmv2Action.builder()
112 .withName(SET_EGRESS_PORT)
113 .addParameter(portBs)
114 .build();
115 } catch (Bmv2TranslatorUtils.ByteSequenceFitException e) {
116 throw new Bmv2InterpreterException(e.getMessage());
117 }
118 }
119
120 private static Bmv2Action actionWithName(String name) {
121 return Bmv2Action.builder().withName(name).build();
122 }
123}