Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.pipelines.fabric; |
| 18 | |
| 19 | import com.google.common.collect.ImmutableList; |
| 20 | import org.junit.Before; |
| 21 | import org.junit.Test; |
| 22 | import org.onlab.packet.MacAddress; |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 23 | import org.onlab.packet.MplsLabel; |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 24 | import org.onlab.packet.VlanId; |
| 25 | import org.onlab.util.ImmutableByteSequence; |
| 26 | import org.onosproject.net.PortNumber; |
| 27 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 28 | import org.onosproject.net.flow.TrafficTreatment; |
| 29 | import org.onosproject.net.pi.runtime.PiAction; |
| 30 | import org.onosproject.net.pi.runtime.PiActionParam; |
| 31 | |
| 32 | import static org.junit.Assert.assertEquals; |
| 33 | |
| 34 | /** |
| 35 | * Test for fabric interpreter. |
| 36 | */ |
| 37 | public class FabricInterpreterTest { |
| 38 | private static final VlanId VLAN_100 = VlanId.vlanId("100"); |
| 39 | private static final PortNumber PORT_1 = PortNumber.portNumber(1); |
| 40 | private static final MacAddress SRC_MAC = MacAddress.valueOf("00:00:00:00:00:01"); |
| 41 | private static final MacAddress DST_MAC = MacAddress.valueOf("00:00:00:00:00:02"); |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 42 | private static final MplsLabel MPLS_10 = MplsLabel.mplsLabel(10); |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 43 | |
| 44 | private FabricInterpreter interpreter; |
| 45 | |
| 46 | @Before |
| 47 | public void setup() { |
| 48 | interpreter = new FabricInterpreter(); |
| 49 | } |
| 50 | |
| 51 | /* Filtering control block */ |
| 52 | |
| 53 | /** |
| 54 | * Map treatment to push_internal_vlan action. |
| 55 | */ |
| 56 | @Test |
| 57 | public void testFilteringTreatment1() throws Exception { |
| 58 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 59 | .pushVlan() |
| 60 | .setVlanId(VLAN_100) |
| 61 | .build(); |
| 62 | PiAction mappedAction = interpreter.mapTreatment(treatment, |
Yi Tseng | 43ee7e8 | 2018-04-12 16:37:34 +0800 | [diff] [blame] | 63 | FabricConstants.FABRIC_INGRESS_FILTERING_INGRESS_PORT_VLAN); |
| 64 | PiActionParam param = new PiActionParam(FabricConstants.NEW_VLAN_ID, |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 65 | ImmutableByteSequence.copyFrom(VLAN_100.toShort())); |
| 66 | PiAction expectedAction = PiAction.builder() |
Yi Tseng | 43ee7e8 | 2018-04-12 16:37:34 +0800 | [diff] [blame] | 67 | .withId(FabricConstants.FABRIC_INGRESS_FILTERING_PUSH_INTERNAL_VLAN) |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 68 | .withParameter(param) |
| 69 | .build(); |
| 70 | |
| 71 | assertEquals(expectedAction, mappedAction); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Map treatment to set_vlan action. |
| 76 | */ |
| 77 | @Test |
| 78 | public void testFilteringTreatment2() throws Exception { |
| 79 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 80 | .setVlanId(VLAN_100) |
| 81 | .build(); |
| 82 | PiAction mappedAction = interpreter.mapTreatment(treatment, |
Yi Tseng | 43ee7e8 | 2018-04-12 16:37:34 +0800 | [diff] [blame] | 83 | FabricConstants.FABRIC_INGRESS_FILTERING_INGRESS_PORT_VLAN); |
| 84 | PiActionParam param = new PiActionParam(FabricConstants.NEW_VLAN_ID, |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 85 | ImmutableByteSequence.copyFrom(VLAN_100.toShort())); |
| 86 | PiAction expectedAction = PiAction.builder() |
Yi Tseng | 43ee7e8 | 2018-04-12 16:37:34 +0800 | [diff] [blame] | 87 | .withId(FabricConstants.FABRIC_INGRESS_FILTERING_SET_VLAN) |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 88 | .withParameter(param) |
| 89 | .build(); |
| 90 | |
| 91 | assertEquals(expectedAction, mappedAction); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Map treatment to nop action. |
| 96 | */ |
| 97 | @Test |
| 98 | public void testFilteringTreatment3() throws Exception { |
| 99 | TrafficTreatment treatment = DefaultTrafficTreatment.emptyTreatment(); |
| 100 | PiAction mappedAction = interpreter.mapTreatment(treatment, |
Yi Tseng | 43ee7e8 | 2018-04-12 16:37:34 +0800 | [diff] [blame] | 101 | FabricConstants.FABRIC_INGRESS_FILTERING_INGRESS_PORT_VLAN); |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 102 | PiAction expectedAction = PiAction.builder() |
Carmelo Cascone | 8a715f8 | 2018-08-20 23:16:27 -0700 | [diff] [blame] | 103 | .withId(FabricConstants.FABRIC_INGRESS_FILTERING_NOP_INGRESS_PORT_VLAN) |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 104 | .build(); |
| 105 | |
| 106 | assertEquals(expectedAction, mappedAction); |
| 107 | } |
| 108 | |
| 109 | /* Forwarding control block */ |
| 110 | |
| 111 | /** |
| 112 | * Map treatment to duplicate_to_controller action. |
| 113 | */ |
| 114 | @Test |
| 115 | public void testForwardingTreatment1() throws Exception { |
| 116 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 117 | .punt() |
| 118 | .build(); |
| 119 | PiAction mappedAction = interpreter.mapTreatment(treatment, |
Yi Tseng | 43ee7e8 | 2018-04-12 16:37:34 +0800 | [diff] [blame] | 120 | FabricConstants.FABRIC_INGRESS_FORWARDING_ACL); |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 121 | PiAction expectedAction = PiAction.builder() |
Carmelo Cascone | 1e8843f | 2018-07-19 19:01:12 +0200 | [diff] [blame] | 122 | .withId(FabricConstants.FABRIC_INGRESS_FORWARDING_CLONE_TO_CPU) |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 123 | .build(); |
| 124 | |
| 125 | assertEquals(expectedAction, mappedAction); |
| 126 | } |
| 127 | |
Yi Tseng | df3eec5 | 2018-02-15 14:56:02 -0800 | [diff] [blame] | 128 | /** |
| 129 | * Map empty treatment for forwarding block to nop action. |
| 130 | */ |
| 131 | @Test |
| 132 | public void testEmptyForwardingTreatment() throws Exception { |
| 133 | TrafficTreatment treatment = DefaultTrafficTreatment.emptyTreatment(); |
Charles Chan | cf696e5 | 2018-08-16 16:25:13 -0700 | [diff] [blame] | 134 | |
Yi Tseng | df3eec5 | 2018-02-15 14:56:02 -0800 | [diff] [blame] | 135 | PiAction mappedAction = interpreter.mapTreatment(treatment, |
Charles Chan | 384aea2 | 2018-08-23 22:08:02 -0700 | [diff] [blame^] | 136 | FabricConstants.FABRIC_INGRESS_FORWARDING_ROUTING_V4); |
Charles Chan | cf696e5 | 2018-08-16 16:25:13 -0700 | [diff] [blame] | 137 | PiAction expectedAction = PiAction.builder() |
| 138 | .withId(FabricConstants.NOP) |
| 139 | .build(); |
| 140 | assertEquals(expectedAction, mappedAction); |
| 141 | |
| 142 | mappedAction = interpreter.mapTreatment(treatment, |
| 143 | FabricConstants.FABRIC_INGRESS_FORWARDING_ACL); |
| 144 | expectedAction = PiAction.builder() |
| 145 | .withId(FabricConstants.FABRIC_INGRESS_FORWARDING_NOP_ACL) |
| 146 | .build(); |
| 147 | assertEquals(expectedAction, mappedAction); |
Yi Tseng | df3eec5 | 2018-02-15 14:56:02 -0800 | [diff] [blame] | 148 | } |
| 149 | |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 150 | /* Next control block */ |
| 151 | |
| 152 | /** |
| 153 | * Map treatment to output action. |
| 154 | */ |
| 155 | @Test |
| 156 | public void testNextTreatment1() throws Exception { |
| 157 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 158 | .setOutput(PORT_1) |
| 159 | .build(); |
| 160 | PiAction mappedAction = interpreter.mapTreatment(treatment, |
Yi Tseng | 43ee7e8 | 2018-04-12 16:37:34 +0800 | [diff] [blame] | 161 | FabricConstants.FABRIC_INGRESS_NEXT_SIMPLE); |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 162 | short portNumVal = (short) PORT_1.toLong(); |
Yi Tseng | 43ee7e8 | 2018-04-12 16:37:34 +0800 | [diff] [blame] | 163 | PiActionParam param = new PiActionParam(FabricConstants.PORT_NUM, |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 164 | ImmutableByteSequence.copyFrom(portNumVal)); |
| 165 | PiAction expectedAction = PiAction.builder() |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame] | 166 | .withId(FabricConstants.FABRIC_INGRESS_NEXT_OUTPUT_SIMPLE) |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 167 | .withParameter(param) |
| 168 | .build(); |
| 169 | |
| 170 | assertEquals(expectedAction, mappedAction); |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Map treatment to output_ecmp action. |
| 175 | */ |
| 176 | @Test |
| 177 | public void testNextTreatment2() throws Exception { |
| 178 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 179 | .setEthSrc(SRC_MAC) |
| 180 | .setEthDst(DST_MAC) |
| 181 | .setOutput(PORT_1) |
| 182 | .build(); |
| 183 | PiAction mappedAction = interpreter.mapTreatment(treatment, |
Yi Tseng | 43ee7e8 | 2018-04-12 16:37:34 +0800 | [diff] [blame] | 184 | FabricConstants.FABRIC_INGRESS_NEXT_HASHED); |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 185 | short portNumVal = (short) PORT_1.toLong(); |
Yi Tseng | 43ee7e8 | 2018-04-12 16:37:34 +0800 | [diff] [blame] | 186 | PiActionParam ethSrcParam = new PiActionParam(FabricConstants.SMAC, |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 187 | ImmutableByteSequence.copyFrom(SRC_MAC.toBytes())); |
Yi Tseng | 43ee7e8 | 2018-04-12 16:37:34 +0800 | [diff] [blame] | 188 | PiActionParam ethDstParam = new PiActionParam(FabricConstants.DMAC, |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 189 | ImmutableByteSequence.copyFrom(DST_MAC.toBytes())); |
Yi Tseng | 43ee7e8 | 2018-04-12 16:37:34 +0800 | [diff] [blame] | 190 | PiActionParam portParam = new PiActionParam(FabricConstants.PORT_NUM, |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 191 | ImmutableByteSequence.copyFrom(portNumVal)); |
| 192 | PiAction expectedAction = PiAction.builder() |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame] | 193 | .withId(FabricConstants.FABRIC_INGRESS_NEXT_L3_ROUTING_HASHED) |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 194 | .withParameters(ImmutableList.of(ethSrcParam, ethDstParam, portParam)) |
| 195 | .build(); |
| 196 | |
| 197 | assertEquals(expectedAction, mappedAction); |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Map treatment to set_vlan_output action. |
| 202 | */ |
| 203 | @Test |
| 204 | public void testNextTreatment3() throws Exception { |
| 205 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 206 | .setVlanId(VLAN_100) |
| 207 | .setOutput(PORT_1) |
| 208 | .build(); |
| 209 | PiAction mappedAction = interpreter.mapTreatment(treatment, |
Yi Tseng | 43ee7e8 | 2018-04-12 16:37:34 +0800 | [diff] [blame] | 210 | FabricConstants.FABRIC_INGRESS_NEXT_SIMPLE); |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 211 | short portNumVal = (short) PORT_1.toLong(); |
Yi Tseng | 43ee7e8 | 2018-04-12 16:37:34 +0800 | [diff] [blame] | 212 | PiActionParam portParam = new PiActionParam(FabricConstants.PORT_NUM, |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 213 | ImmutableByteSequence.copyFrom(portNumVal)); |
| 214 | short vlanVal = VLAN_100.toShort(); |
Yi Tseng | 43ee7e8 | 2018-04-12 16:37:34 +0800 | [diff] [blame] | 215 | PiActionParam vlanParam = new PiActionParam(FabricConstants.NEW_VLAN_ID, |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 216 | ImmutableByteSequence.copyFrom(vlanVal)); |
| 217 | |
| 218 | PiAction expectedAction = PiAction.builder() |
Yi Tseng | 43ee7e8 | 2018-04-12 16:37:34 +0800 | [diff] [blame] | 219 | .withId(FabricConstants.FABRIC_INGRESS_NEXT_SET_VLAN_OUTPUT) |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 220 | .withParameters(ImmutableList.of(vlanParam, portParam)) |
| 221 | .build(); |
| 222 | |
| 223 | assertEquals(expectedAction, mappedAction); |
| 224 | } |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 225 | |
| 226 | @Test |
| 227 | public void testMplsRoutingTreatment() throws Exception { |
| 228 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 229 | .setEthDst(DST_MAC) |
| 230 | .setEthSrc(SRC_MAC) |
| 231 | .pushMpls() |
| 232 | .copyTtlOut() |
| 233 | .setMpls(MPLS_10) |
| 234 | .setOutput(PORT_1) |
| 235 | .build(); |
| 236 | PiAction mappedAction = interpreter.mapTreatment(treatment, |
Yi Tseng | 43ee7e8 | 2018-04-12 16:37:34 +0800 | [diff] [blame] | 237 | FabricConstants.FABRIC_INGRESS_NEXT_HASHED); |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 238 | short portNumVal = (short) PORT_1.toLong(); |
Yi Tseng | 43ee7e8 | 2018-04-12 16:37:34 +0800 | [diff] [blame] | 239 | PiActionParam ethSrcParam = new PiActionParam(FabricConstants.SMAC, |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 240 | ImmutableByteSequence.copyFrom(SRC_MAC.toBytes())); |
Yi Tseng | 43ee7e8 | 2018-04-12 16:37:34 +0800 | [diff] [blame] | 241 | PiActionParam ethDstParam = new PiActionParam(FabricConstants.DMAC, |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 242 | ImmutableByteSequence.copyFrom(DST_MAC.toBytes())); |
Yi Tseng | 43ee7e8 | 2018-04-12 16:37:34 +0800 | [diff] [blame] | 243 | PiActionParam portParam = new PiActionParam(FabricConstants.PORT_NUM, |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 244 | ImmutableByteSequence.copyFrom(portNumVal)); |
| 245 | ImmutableByteSequence mplsVal = |
Carmelo Cascone | 8a571af | 2018-04-06 23:17:04 -0700 | [diff] [blame] | 246 | ImmutableByteSequence.copyFrom(MPLS_10.toInt()).fit(20); |
Yi Tseng | 43ee7e8 | 2018-04-12 16:37:34 +0800 | [diff] [blame] | 247 | PiActionParam mplsParam = new PiActionParam(FabricConstants.LABEL, mplsVal); |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 248 | PiAction expectedAction = PiAction.builder() |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame] | 249 | .withId(FabricConstants.FABRIC_INGRESS_NEXT_MPLS_ROUTING_V4_HASHED) |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 250 | .withParameters(ImmutableList.of(ethSrcParam, ethDstParam, portParam, mplsParam)) |
| 251 | .build(); |
| 252 | assertEquals(expectedAction, mappedAction); |
| 253 | } |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 254 | } |