blob: 59f0c8c605d6fdee7e3449a00f22de0eeecc9751 [file] [log] [blame]
Carmelo Cascone59f57de2017-07-11 19:55:09 -04001/*
2 * Copyright 2017-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.drivers.bmv2;
18
19import com.google.common.collect.ImmutableBiMap;
20import org.onlab.util.ImmutableByteSequence;
21import org.onosproject.net.PortNumber;
22import org.onosproject.net.driver.AbstractHandlerBehaviour;
23import org.onosproject.net.flow.TrafficTreatment;
24import org.onosproject.net.flow.criteria.Criterion;
25import org.onosproject.net.flow.instructions.Instruction;
26import org.onosproject.net.flow.instructions.Instructions;
27import org.onosproject.net.pi.model.PiPipeconf;
28import org.onosproject.net.pi.model.PiPipelineInterpreter;
29import org.onosproject.net.pi.runtime.PiAction;
30import org.onosproject.net.pi.runtime.PiActionId;
31import org.onosproject.net.pi.runtime.PiActionParam;
32import org.onosproject.net.pi.runtime.PiActionParamId;
33import org.onosproject.net.pi.runtime.PiHeaderFieldId;
34import org.onosproject.net.pi.runtime.PiTableAction;
35import org.onosproject.net.pi.runtime.PiTableId;
36
37import java.util.Optional;
38
39import static org.onosproject.net.PortNumber.CONTROLLER;
40
41/**
42 * Interpreter implementation for the default pipeconf.
43 */
44public class Bmv2DefaultInterpreter extends AbstractHandlerBehaviour implements PiPipelineInterpreter {
45 private static final String TABLE0 = "table0";
46 private static final String SEND_TO_CPU = "send_to_cpu_0";
47 private static final String PORT = "port";
48 private static final String DROP = "_drop_0";
49 private static final String SET_EGRESS_PORT = "set_egress_port_0";
50
51 private static final PiHeaderFieldId IN_PORT_ID = PiHeaderFieldId.of("standard_metadata", "ingress_port");
52 private static final PiHeaderFieldId ETH_DST_ID = PiHeaderFieldId.of("ethernet_t", "dstAddr");
53 private static final PiHeaderFieldId ETH_SRC_ID = PiHeaderFieldId.of("ethernet_t", "srcAddr");
54 private static final PiHeaderFieldId ETH_TYPE_ID = PiHeaderFieldId.of("ethernet_t", "etherType");
55
56 private static final ImmutableBiMap<Criterion.Type, PiHeaderFieldId> CRITERION_MAP = ImmutableBiMap.of(
57 Criterion.Type.IN_PORT, IN_PORT_ID,
58 Criterion.Type.ETH_DST, ETH_DST_ID,
59 Criterion.Type.ETH_SRC, ETH_SRC_ID,
60 Criterion.Type.ETH_TYPE, ETH_TYPE_ID);
61
62 private static final ImmutableBiMap<Integer, PiTableId> TABLE_MAP = ImmutableBiMap.of(
63 0, PiTableId.of(TABLE0));
64
65 @Override
66 public PiTableAction mapTreatment(TrafficTreatment treatment, PiPipeconf pipeconf) throws PiInterpreterException {
67
68 if (treatment.allInstructions().size() == 0) {
69 // No instructions means drop for us.
70 return actionWithName(DROP);
71 } else if (treatment.allInstructions().size() > 1) {
72 // Otherwise, we understand treatments with only 1 instruction.
73 throw new PiPipelineInterpreter.PiInterpreterException("Treatment has multiple instructions");
74 }
75
76 Instruction instruction = treatment.allInstructions().get(0);
77
78 switch (instruction.type()) {
79 case OUTPUT:
80 Instructions.OutputInstruction outInstruction = (Instructions.OutputInstruction) instruction;
81 PortNumber port = outInstruction.port();
82 if (!port.isLogical()) {
83 PiAction.builder()
84 .withId(PiActionId.of(SET_EGRESS_PORT))
85 .withParameter(new PiActionParam(PiActionParamId.of(PORT),
86 ImmutableByteSequence.copyFrom(port.toLong())))
87 .build();
88 } else if (port.equals(CONTROLLER)) {
89 return actionWithName(SEND_TO_CPU);
90 } else {
91 throw new PiInterpreterException("Egress on logical port not supported: " + port);
92 }
93 case NOACTION:
94 return actionWithName(DROP);
95 default:
96 throw new PiInterpreterException("Instruction type not supported: " + instruction.type().name());
97 }
98 }
99
100 /**
101 * Returns an action instance with no runtime parameters.
102 */
103 private PiAction actionWithName(String name) {
104 return PiAction.builder().withId(PiActionId.of(name)).build();
105 }
106
107 @Override
108 public Optional<PiHeaderFieldId> mapCriterionType(Criterion.Type type) {
109 return Optional.ofNullable(CRITERION_MAP.get(type));
110 }
111
112 @Override
113 public Optional<Criterion.Type> mapPiHeaderFieldId(PiHeaderFieldId headerFieldId) {
114 return Optional.ofNullable(CRITERION_MAP.inverse().get(headerFieldId));
115 }
116
117 @Override
118 public Optional<PiTableId> mapFlowRuleTableId(int flowRuleTableId) {
119 return Optional.ofNullable(TABLE_MAP.get(flowRuleTableId));
120 }
121}