blob: bb6b45d1731b2a2320e296fd4ccc4f423d08576f [file] [log] [blame]
Yi Tsengfa4a1c72017-11-03 10:22:38 -07001/*
2 * Copyright 2017-present Open Networking Foundation
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.pipelines.fabric;
18
19import com.google.common.collect.ImmutableList;
20import org.junit.Before;
21import org.junit.Test;
22import org.onlab.packet.MacAddress;
Yi Tseng1b154bd2017-11-20 17:48:19 -080023import org.onlab.packet.MplsLabel;
Yi Tsengfa4a1c72017-11-03 10:22:38 -070024import org.onlab.packet.VlanId;
25import org.onlab.util.ImmutableByteSequence;
26import org.onosproject.net.PortNumber;
27import org.onosproject.net.flow.DefaultTrafficTreatment;
28import org.onosproject.net.flow.TrafficTreatment;
29import org.onosproject.net.pi.runtime.PiAction;
30import org.onosproject.net.pi.runtime.PiActionParam;
31
32import static org.junit.Assert.assertEquals;
Yi Tsengdf3eec52018-02-15 14:56:02 -080033import static org.junit.Assert.assertNull;
Yi Tsengfa4a1c72017-11-03 10:22:38 -070034
35/**
36 * Test for fabric interpreter.
37 */
38public class FabricInterpreterTest {
39 private static final VlanId VLAN_100 = VlanId.vlanId("100");
40 private static final PortNumber PORT_1 = PortNumber.portNumber(1);
41 private static final MacAddress SRC_MAC = MacAddress.valueOf("00:00:00:00:00:01");
42 private static final MacAddress DST_MAC = MacAddress.valueOf("00:00:00:00:00:02");
Yi Tseng1b154bd2017-11-20 17:48:19 -080043 private static final MplsLabel MPLS_10 = MplsLabel.mplsLabel(10);
Yi Tsengfa4a1c72017-11-03 10:22:38 -070044
45 private FabricInterpreter interpreter;
46
47 @Before
48 public void setup() {
49 interpreter = new FabricInterpreter();
50 }
51
52 /* Filtering control block */
53
54 /**
55 * Map treatment to push_internal_vlan action.
56 */
57 @Test
58 public void testFilteringTreatment1() throws Exception {
59 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
60 .pushVlan()
61 .setVlanId(VLAN_100)
62 .build();
63 PiAction mappedAction = interpreter.mapTreatment(treatment,
Yi Tseng43ee7e82018-04-12 16:37:34 +080064 FabricConstants.FABRIC_INGRESS_FILTERING_INGRESS_PORT_VLAN);
65 PiActionParam param = new PiActionParam(FabricConstants.NEW_VLAN_ID,
Yi Tsengfa4a1c72017-11-03 10:22:38 -070066 ImmutableByteSequence.copyFrom(VLAN_100.toShort()));
67 PiAction expectedAction = PiAction.builder()
Yi Tseng43ee7e82018-04-12 16:37:34 +080068 .withId(FabricConstants.FABRIC_INGRESS_FILTERING_PUSH_INTERNAL_VLAN)
Yi Tsengfa4a1c72017-11-03 10:22:38 -070069 .withParameter(param)
70 .build();
71
72 assertEquals(expectedAction, mappedAction);
73 }
74
75 /**
76 * Map treatment to set_vlan action.
77 */
78 @Test
79 public void testFilteringTreatment2() throws Exception {
80 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
81 .setVlanId(VLAN_100)
82 .build();
83 PiAction mappedAction = interpreter.mapTreatment(treatment,
Yi Tseng43ee7e82018-04-12 16:37:34 +080084 FabricConstants.FABRIC_INGRESS_FILTERING_INGRESS_PORT_VLAN);
85 PiActionParam param = new PiActionParam(FabricConstants.NEW_VLAN_ID,
Yi Tsengfa4a1c72017-11-03 10:22:38 -070086 ImmutableByteSequence.copyFrom(VLAN_100.toShort()));
87 PiAction expectedAction = PiAction.builder()
Yi Tseng43ee7e82018-04-12 16:37:34 +080088 .withId(FabricConstants.FABRIC_INGRESS_FILTERING_SET_VLAN)
Yi Tsengfa4a1c72017-11-03 10:22:38 -070089 .withParameter(param)
90 .build();
91
92 assertEquals(expectedAction, mappedAction);
93 }
94
95 /**
96 * Map treatment to nop action.
97 */
98 @Test
99 public void testFilteringTreatment3() throws Exception {
100 TrafficTreatment treatment = DefaultTrafficTreatment.emptyTreatment();
101 PiAction mappedAction = interpreter.mapTreatment(treatment,
Yi Tseng43ee7e82018-04-12 16:37:34 +0800102 FabricConstants.FABRIC_INGRESS_FILTERING_INGRESS_PORT_VLAN);
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700103 PiAction expectedAction = PiAction.builder()
Yi Tseng43ee7e82018-04-12 16:37:34 +0800104 .withId(FabricConstants.NOP)
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700105 .build();
106
107 assertEquals(expectedAction, mappedAction);
108 }
109
110 /* Forwarding control block */
111
112 /**
113 * Map treatment to duplicate_to_controller action.
114 */
115 @Test
116 public void testForwardingTreatment1() throws Exception {
117 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
118 .punt()
119 .build();
120 PiAction mappedAction = interpreter.mapTreatment(treatment,
Yi Tseng43ee7e82018-04-12 16:37:34 +0800121 FabricConstants.FABRIC_INGRESS_FORWARDING_ACL);
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700122 PiAction expectedAction = PiAction.builder()
Carmelo Cascone1e8843f2018-07-19 19:01:12 +0200123 .withId(FabricConstants.FABRIC_INGRESS_FORWARDING_CLONE_TO_CPU)
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700124 .build();
125
126 assertEquals(expectedAction, mappedAction);
127 }
128
Yi Tsengdf3eec52018-02-15 14:56:02 -0800129 /**
130 * Map empty treatment for forwarding block to nop action.
131 */
132 @Test
133 public void testEmptyForwardingTreatment() throws Exception {
134 TrafficTreatment treatment = DefaultTrafficTreatment.emptyTreatment();
135 PiAction mappedAction = interpreter.mapTreatment(treatment,
Yi Tseng43ee7e82018-04-12 16:37:34 +0800136 FabricConstants.FABRIC_INGRESS_FORWARDING_UNICAST_V4);
Yi Tsengdf3eec52018-02-15 14:56:02 -0800137 assertNull(mappedAction);
138 }
139
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700140 /* Next control block */
141
142 /**
143 * Map treatment to output action.
144 */
145 @Test
146 public void testNextTreatment1() throws Exception {
147 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
148 .setOutput(PORT_1)
149 .build();
150 PiAction mappedAction = interpreter.mapTreatment(treatment,
Yi Tseng43ee7e82018-04-12 16:37:34 +0800151 FabricConstants.FABRIC_INGRESS_NEXT_SIMPLE);
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700152 short portNumVal = (short) PORT_1.toLong();
Yi Tseng43ee7e82018-04-12 16:37:34 +0800153 PiActionParam param = new PiActionParam(FabricConstants.PORT_NUM,
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700154 ImmutableByteSequence.copyFrom(portNumVal));
155 PiAction expectedAction = PiAction.builder()
Yi Tseng47eac892018-07-11 02:17:04 +0800156 .withId(FabricConstants.FABRIC_INGRESS_NEXT_OUTPUT_SIMPLE)
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700157 .withParameter(param)
158 .build();
159
160 assertEquals(expectedAction, mappedAction);
161 }
162
163 /**
164 * Map treatment to output_ecmp action.
165 */
166 @Test
167 public void testNextTreatment2() throws Exception {
168 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
169 .setEthSrc(SRC_MAC)
170 .setEthDst(DST_MAC)
171 .setOutput(PORT_1)
172 .build();
173 PiAction mappedAction = interpreter.mapTreatment(treatment,
Yi Tseng43ee7e82018-04-12 16:37:34 +0800174 FabricConstants.FABRIC_INGRESS_NEXT_HASHED);
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700175 short portNumVal = (short) PORT_1.toLong();
Yi Tseng43ee7e82018-04-12 16:37:34 +0800176 PiActionParam ethSrcParam = new PiActionParam(FabricConstants.SMAC,
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700177 ImmutableByteSequence.copyFrom(SRC_MAC.toBytes()));
Yi Tseng43ee7e82018-04-12 16:37:34 +0800178 PiActionParam ethDstParam = new PiActionParam(FabricConstants.DMAC,
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700179 ImmutableByteSequence.copyFrom(DST_MAC.toBytes()));
Yi Tseng43ee7e82018-04-12 16:37:34 +0800180 PiActionParam portParam = new PiActionParam(FabricConstants.PORT_NUM,
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700181 ImmutableByteSequence.copyFrom(portNumVal));
182 PiAction expectedAction = PiAction.builder()
Yi Tseng47eac892018-07-11 02:17:04 +0800183 .withId(FabricConstants.FABRIC_INGRESS_NEXT_L3_ROUTING_HASHED)
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700184 .withParameters(ImmutableList.of(ethSrcParam, ethDstParam, portParam))
185 .build();
186
187 assertEquals(expectedAction, mappedAction);
188 }
189
190 /**
191 * Map treatment to set_vlan_output action.
192 */
193 @Test
194 public void testNextTreatment3() throws Exception {
195 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
196 .setVlanId(VLAN_100)
197 .setOutput(PORT_1)
198 .build();
199 PiAction mappedAction = interpreter.mapTreatment(treatment,
Yi Tseng43ee7e82018-04-12 16:37:34 +0800200 FabricConstants.FABRIC_INGRESS_NEXT_SIMPLE);
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700201 short portNumVal = (short) PORT_1.toLong();
Yi Tseng43ee7e82018-04-12 16:37:34 +0800202 PiActionParam portParam = new PiActionParam(FabricConstants.PORT_NUM,
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700203 ImmutableByteSequence.copyFrom(portNumVal));
204 short vlanVal = VLAN_100.toShort();
Yi Tseng43ee7e82018-04-12 16:37:34 +0800205 PiActionParam vlanParam = new PiActionParam(FabricConstants.NEW_VLAN_ID,
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700206 ImmutableByteSequence.copyFrom(vlanVal));
207
208 PiAction expectedAction = PiAction.builder()
Yi Tseng43ee7e82018-04-12 16:37:34 +0800209 .withId(FabricConstants.FABRIC_INGRESS_NEXT_SET_VLAN_OUTPUT)
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700210 .withParameters(ImmutableList.of(vlanParam, portParam))
211 .build();
212
213 assertEquals(expectedAction, mappedAction);
214 }
Yi Tseng1b154bd2017-11-20 17:48:19 -0800215
216 @Test
217 public void testMplsRoutingTreatment() throws Exception {
218 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
219 .setEthDst(DST_MAC)
220 .setEthSrc(SRC_MAC)
221 .pushMpls()
222 .copyTtlOut()
223 .setMpls(MPLS_10)
224 .setOutput(PORT_1)
225 .build();
226 PiAction mappedAction = interpreter.mapTreatment(treatment,
Yi Tseng43ee7e82018-04-12 16:37:34 +0800227 FabricConstants.FABRIC_INGRESS_NEXT_HASHED);
Yi Tseng1b154bd2017-11-20 17:48:19 -0800228 short portNumVal = (short) PORT_1.toLong();
Yi Tseng43ee7e82018-04-12 16:37:34 +0800229 PiActionParam ethSrcParam = new PiActionParam(FabricConstants.SMAC,
Yi Tseng1b154bd2017-11-20 17:48:19 -0800230 ImmutableByteSequence.copyFrom(SRC_MAC.toBytes()));
Yi Tseng43ee7e82018-04-12 16:37:34 +0800231 PiActionParam ethDstParam = new PiActionParam(FabricConstants.DMAC,
Yi Tseng1b154bd2017-11-20 17:48:19 -0800232 ImmutableByteSequence.copyFrom(DST_MAC.toBytes()));
Yi Tseng43ee7e82018-04-12 16:37:34 +0800233 PiActionParam portParam = new PiActionParam(FabricConstants.PORT_NUM,
Yi Tseng1b154bd2017-11-20 17:48:19 -0800234 ImmutableByteSequence.copyFrom(portNumVal));
235 ImmutableByteSequence mplsVal =
Carmelo Cascone8a571af2018-04-06 23:17:04 -0700236 ImmutableByteSequence.copyFrom(MPLS_10.toInt()).fit(20);
Yi Tseng43ee7e82018-04-12 16:37:34 +0800237 PiActionParam mplsParam = new PiActionParam(FabricConstants.LABEL, mplsVal);
Yi Tseng1b154bd2017-11-20 17:48:19 -0800238 PiAction expectedAction = PiAction.builder()
Yi Tseng47eac892018-07-11 02:17:04 +0800239 .withId(FabricConstants.FABRIC_INGRESS_NEXT_MPLS_ROUTING_V4_HASHED)
Yi Tseng1b154bd2017-11-20 17:48:19 -0800240 .withParameters(ImmutableList.of(ethSrcParam, ethDstParam, portParam, mplsParam))
241 .build();
242 assertEquals(expectedAction, mappedAction);
243 }
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700244}