blob: 4a105a3108f2c686b6852247cd51b839196ae565 [file] [log] [blame]
Carmelo Casconeb7388bd2016-04-14 10:20:13 -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.drivers.bmv2.translators;
18
Carmelo Casconeb7388bd2016-04-14 10:20:13 -070019import com.google.common.annotations.Beta;
Carmelo Casconed4e7a772016-05-03 11:21:29 -070020import com.google.common.collect.ImmutableMap;
Carmelo Casconeb7388bd2016-04-14 10:20:13 -070021import org.onlab.util.ImmutableByteSequence;
22import org.onosproject.bmv2.api.model.Bmv2Model;
23import org.onosproject.bmv2.api.runtime.Bmv2Action;
Carmelo Casconee9121642016-04-27 17:02:38 -070024import org.onosproject.net.PortNumber;
Carmelo Casconeb7388bd2016-04-14 10:20:13 -070025import org.onosproject.net.flow.TrafficTreatment;
26import org.onosproject.net.flow.criteria.Criterion;
27import org.onosproject.net.flow.instructions.Instruction;
28import org.onosproject.net.flow.instructions.Instructions;
29
Carmelo Casconeb7388bd2016-04-14 10:20:13 -070030import java.util.Map;
31
32/**
33 * Implementation of a Bmv2 flow rule translator configuration for the
Carmelo Casconee9121642016-04-27 17:02:38 -070034 * simple.p4 model.
Carmelo Casconeb7388bd2016-04-14 10:20:13 -070035 */
36@Beta
Carmelo Casconed4e7a772016-05-03 11:21:29 -070037public class Bmv2SimpleTranslatorConfig extends Bmv2DefaultTranslatorConfig {
Carmelo Casconeb7388bd2016-04-14 10:20:13 -070038
Carmelo Casconed4e7a772016-05-03 11:21:29 -070039 // Lazily populate field map.
40 private static final Map<String, Criterion.Type> FIELD_MAP = ImmutableMap.of(
41 "standard_metadata.ingress_port", Criterion.Type.IN_PORT,
42 "ethernet.dstAddr", Criterion.Type.ETH_DST,
43 "ethernet.srcAddr", Criterion.Type.ETH_SRC,
44 "ethernet.etherType", Criterion.Type.ETH_TYPE);
Carmelo Casconeb7388bd2016-04-14 10:20:13 -070045
46 /**
47 * Creates a new simple pipeline translator configuration.
48 */
Carmelo Casconed4e7a772016-05-03 11:21:29 -070049 public Bmv2SimpleTranslatorConfig(Bmv2Model model) {
50 // Populate fieldMap.
51 super(model, FIELD_MAP);
Carmelo Casconeb7388bd2016-04-14 10:20:13 -070052 }
53
54 private static Bmv2Action buildDropAction() {
55 return Bmv2Action.builder()
56 .withName("_drop")
57 .build();
58 }
59
Carmelo Casconee9121642016-04-27 17:02:38 -070060 private static Bmv2Action buildPushToCpAction() {
61 return Bmv2Action.builder()
62 .withName("send_to_cpu")
63 .build();
64 }
65
Carmelo Casconeb7388bd2016-04-14 10:20:13 -070066 private static Bmv2Action buildFwdAction(Instructions.OutputInstruction inst)
67 throws Bmv2FlowRuleTranslatorException {
68
69 Bmv2Action.Builder actionBuilder = Bmv2Action.builder();
70
71 actionBuilder.withName("fwd");
72
73 if (inst.port().isLogical()) {
Carmelo Casconee9121642016-04-27 17:02:38 -070074 if (inst.port() == PortNumber.CONTROLLER) {
75 return buildPushToCpAction();
76 } else {
77 throw new Bmv2FlowRuleTranslatorException(
78 "Output logic port number not supported: " + inst);
79 }
Carmelo Casconeb7388bd2016-04-14 10:20:13 -070080 }
81
82 actionBuilder.addParameter(
83 ImmutableByteSequence.copyFrom((short) inst.port().toLong()));
84
85 return actionBuilder.build();
86 }
87
Carmelo Casconeb7388bd2016-04-14 10:20:13 -070088 @Override
89 public Bmv2Action buildAction(TrafficTreatment treatment)
90 throws Bmv2FlowRuleTranslatorException {
91
92
93 if (treatment.allInstructions().size() == 0) {
Carmelo Casconed4e7a772016-05-03 11:21:29 -070094 // No instructions means drop.
Carmelo Casconeb7388bd2016-04-14 10:20:13 -070095 return buildDropAction();
96 } else if (treatment.allInstructions().size() > 1) {
Carmelo Casconed4e7a772016-05-03 11:21:29 -070097 // Otherwise, we understand treatments with only 1 instruction.
Carmelo Casconeb7388bd2016-04-14 10:20:13 -070098 throw new Bmv2FlowRuleTranslatorException(
99 "Treatment not supported, more than 1 instructions found: "
100 + treatment.toString());
101 }
102
103 Instruction instruction = treatment.allInstructions().get(0);
104
105 switch (instruction.type()) {
106 case OUTPUT:
107 return buildFwdAction((Instructions.OutputInstruction) instruction);
108 case NOACTION:
109 return buildDropAction();
110 default:
111 throw new Bmv2FlowRuleTranslatorException(
112 "Instruction type not supported: "
113 + instruction.type().name());
114 }
115 }
116}