blob: ddee2ca0f4dbc0c693704ab44f089fe1d902540d [file] [log] [blame]
Carmelo Cascone00a59962017-06-16 17:51:49 +09001/*
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.net.pi.impl;
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;
Andrea Campanella432f7182017-07-14 18:43:27 +020026import org.onosproject.net.packet.OutboundPacket;
Carmelo Cascone00a59962017-06-16 17:51:49 +090027import 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;
Andrea Campanella432f7182017-07-14 18:43:27 +020034import org.onosproject.net.pi.runtime.PiPacketMetadata;
Carmelo Cascone00a59962017-06-16 17:51:49 +090035import org.onosproject.net.pi.runtime.PiTableAction;
36import org.onosproject.net.pi.runtime.PiTableId;
37
Andrea Campanella432f7182017-07-14 18:43:27 +020038import java.util.Collection;
Carmelo Cascone00a59962017-06-16 17:51:49 +090039import java.util.Optional;
40
41import static org.onosproject.net.PortNumber.CONTROLLER;
42import static org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
43
44/**
45 * Mock interpreter implementation.
46 */
47public class MockInterpreter extends AbstractHandlerBehaviour implements PiPipelineInterpreter {
48
49 static final String TABLE0 = "table0";
50 static final String SEND_TO_CPU = "send_to_cpu_0";
51 static final String PORT = "port";
52 static final String DROP = "_drop_0";
53 static final String SET_EGRESS_PORT = "set_egress_port_0";
54
55 static final PiHeaderFieldId IN_PORT_ID = PiHeaderFieldId.of("standard_metadata", "ingress_port");
56 static final PiHeaderFieldId ETH_DST_ID = PiHeaderFieldId.of("ethernet_t", "dstAddr");
57 static final PiHeaderFieldId ETH_SRC_ID = PiHeaderFieldId.of("ethernet_t", "srcAddr");
58 static final PiHeaderFieldId ETH_TYPE_ID = PiHeaderFieldId.of("ethernet_t", "etherType");
59
60 private static final ImmutableBiMap<Criterion.Type, PiHeaderFieldId> CRITERION_MAP = ImmutableBiMap.of(
61 Criterion.Type.IN_PORT, IN_PORT_ID,
62 Criterion.Type.ETH_DST, ETH_DST_ID,
63 Criterion.Type.ETH_SRC, ETH_SRC_ID,
64 Criterion.Type.ETH_TYPE, ETH_TYPE_ID);
65
66 private static final ImmutableBiMap<Integer, PiTableId> TABLE_MAP = ImmutableBiMap.of(
67 0, PiTableId.of(TABLE0));
68
69 @Override
70 public PiTableAction mapTreatment(TrafficTreatment treatment, PiPipeconf pipeconf) throws PiInterpreterException {
71
72 if (treatment.allInstructions().size() == 0) {
73 // No instructions means drop for us.
74 return actionWithName(DROP);
75 } else if (treatment.allInstructions().size() > 1) {
76 // Otherwise, we understand treatments with only 1 instruction.
77 throw new PiPipelineInterpreter.PiInterpreterException("Treatment has multiple instructions");
78 }
79
80 Instruction instruction = treatment.allInstructions().get(0);
81
82 switch (instruction.type()) {
83 case OUTPUT:
84 OutputInstruction outInstruction = (OutputInstruction) instruction;
85 PortNumber port = outInstruction.port();
86 if (!port.isLogical()) {
87 PiAction.builder()
88 .withId(PiActionId.of(SET_EGRESS_PORT))
89 .withParameter(new PiActionParam(PiActionParamId.of(PORT),
90 ImmutableByteSequence.copyFrom(port.toLong())))
91 .build();
92 } else if (port.equals(CONTROLLER)) {
93 return actionWithName(SEND_TO_CPU);
94 } else {
95 throw new PiInterpreterException("Egress on logical port not supported: " + port);
96 }
97 case NOACTION:
98 return actionWithName(DROP);
99 default:
100 throw new PiInterpreterException("Instruction type not supported: " + instruction.type().name());
101 }
102 }
103
Andrea Campanella432f7182017-07-14 18:43:27 +0200104 @Override
105 public Collection<PiPacketMetadata> mapOutboundPacket(OutboundPacket packet, PiPipeconf pipeconf)
106 throws PiInterpreterException {
107 return null;
108 }
109
Carmelo Cascone00a59962017-06-16 17:51:49 +0900110 /**
111 * Returns an action instance with no runtime parameters.
112 */
113 private PiAction actionWithName(String name) {
114 return PiAction.builder().withId(PiActionId.of(name)).build();
115 }
116
117 @Override
118 public Optional<PiHeaderFieldId> mapCriterionType(Criterion.Type type) {
119 return Optional.ofNullable(CRITERION_MAP.get(type));
120 }
121
122 @Override
123 public Optional<Criterion.Type> mapPiHeaderFieldId(PiHeaderFieldId headerFieldId) {
124 return Optional.ofNullable(CRITERION_MAP.inverse().get(headerFieldId));
125 }
126
127 @Override
128 public Optional<PiTableId> mapFlowRuleTableId(int flowRuleTableId) {
129 return Optional.ofNullable(TABLE_MAP.get(flowRuleTableId));
130 }
131
132}