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 |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame^] | 57 | public void testFilteringTreatmentPermitWithInternalVlan() throws Exception { |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 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); |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame^] | 64 | PiActionParam param = new PiActionParam(FabricConstants.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() |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame^] | 67 | .withId(FabricConstants.FABRIC_INGRESS_FILTERING_PERMIT_WITH_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 | /** |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame^] | 75 | * Map treatment to permit action. |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 76 | */ |
| 77 | @Test |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame^] | 78 | public void testFilteringTreatmentPermit() throws Exception { |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 79 | TrafficTreatment treatment = DefaultTrafficTreatment.emptyTreatment(); |
| 80 | PiAction mappedAction = interpreter.mapTreatment(treatment, |
Yi Tseng | 43ee7e8 | 2018-04-12 16:37:34 +0800 | [diff] [blame] | 81 | FabricConstants.FABRIC_INGRESS_FILTERING_INGRESS_PORT_VLAN); |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 82 | PiAction expectedAction = PiAction.builder() |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame^] | 83 | .withId(FabricConstants.FABRIC_INGRESS_FILTERING_PERMIT) |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 84 | .build(); |
| 85 | |
| 86 | assertEquals(expectedAction, mappedAction); |
| 87 | } |
| 88 | |
| 89 | /* Forwarding control block */ |
| 90 | |
| 91 | /** |
| 92 | * Map treatment to duplicate_to_controller action. |
| 93 | */ |
| 94 | @Test |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame^] | 95 | public void testAclTreatmentCloneToCpu() throws Exception { |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 96 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 97 | .punt() |
| 98 | .build(); |
| 99 | PiAction mappedAction = interpreter.mapTreatment(treatment, |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame^] | 100 | FabricConstants.FABRIC_INGRESS_ACL_ACL); |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 101 | PiAction expectedAction = PiAction.builder() |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame^] | 102 | .withId(FabricConstants.FABRIC_INGRESS_ACL_CLONE_TO_CPU) |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 103 | .build(); |
| 104 | |
| 105 | assertEquals(expectedAction, mappedAction); |
| 106 | } |
| 107 | |
Yi Tseng | df3eec5 | 2018-02-15 14:56:02 -0800 | [diff] [blame] | 108 | /** |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame^] | 109 | * Map empty treatment for routing v4 table. |
Yi Tseng | df3eec5 | 2018-02-15 14:56:02 -0800 | [diff] [blame] | 110 | */ |
| 111 | @Test |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame^] | 112 | public void testRoutingV4TreatmentEmpty() throws Exception { |
Yi Tseng | df3eec5 | 2018-02-15 14:56:02 -0800 | [diff] [blame] | 113 | TrafficTreatment treatment = DefaultTrafficTreatment.emptyTreatment(); |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame^] | 114 | PiAction mappedAction = interpreter.mapTreatment( |
| 115 | treatment, FabricConstants.FABRIC_INGRESS_FORWARDING_ROUTING_V4); |
Charles Chan | cf696e5 | 2018-08-16 16:25:13 -0700 | [diff] [blame] | 116 | PiAction expectedAction = PiAction.builder() |
Charles Chan | cd03f07 | 2018-08-31 17:46:37 -0700 | [diff] [blame] | 117 | .withId(FabricConstants.FABRIC_INGRESS_FORWARDING_NOP_ROUTING_V4) |
Charles Chan | cf696e5 | 2018-08-16 16:25:13 -0700 | [diff] [blame] | 118 | .build(); |
| 119 | assertEquals(expectedAction, mappedAction); |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame^] | 120 | } |
Charles Chan | cf696e5 | 2018-08-16 16:25:13 -0700 | [diff] [blame] | 121 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame^] | 122 | /** |
| 123 | * Map empty treatment for ACL table. |
| 124 | */ |
| 125 | @Test |
| 126 | public void testAclTreatmentEmpty() throws Exception { |
| 127 | TrafficTreatment treatment = DefaultTrafficTreatment.emptyTreatment(); |
| 128 | PiAction mappedAction = interpreter.mapTreatment( |
| 129 | treatment, FabricConstants.FABRIC_INGRESS_ACL_ACL); |
| 130 | PiAction expectedAction = PiAction.builder() |
| 131 | .withId(FabricConstants.FABRIC_INGRESS_ACL_NOP_ACL) |
Charles Chan | cf696e5 | 2018-08-16 16:25:13 -0700 | [diff] [blame] | 132 | .build(); |
| 133 | assertEquals(expectedAction, mappedAction); |
Yi Tseng | df3eec5 | 2018-02-15 14:56:02 -0800 | [diff] [blame] | 134 | } |
| 135 | |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 136 | /* Next control block */ |
| 137 | |
| 138 | /** |
| 139 | * Map treatment to output action. |
| 140 | */ |
| 141 | @Test |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame^] | 142 | public void testNextTreatmentSimpleOutput() throws Exception { |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 143 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 144 | .setOutput(PORT_1) |
| 145 | .build(); |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame^] | 146 | PiAction mappedAction = interpreter.mapTreatment( |
| 147 | treatment, FabricConstants.FABRIC_INGRESS_NEXT_SIMPLE); |
| 148 | PiActionParam param = new PiActionParam(FabricConstants.PORT_NUM, PORT_1.toLong()); |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 149 | PiAction expectedAction = PiAction.builder() |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame] | 150 | .withId(FabricConstants.FABRIC_INGRESS_NEXT_OUTPUT_SIMPLE) |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 151 | .withParameter(param) |
| 152 | .build(); |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 153 | assertEquals(expectedAction, mappedAction); |
| 154 | } |
| 155 | |
| 156 | /** |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame^] | 157 | * Map treatment for hashed table to routing v4 action. |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 158 | */ |
| 159 | @Test |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame^] | 160 | public void testNextTreatmentHashedRoutingV4() throws Exception { |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 161 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 162 | .setEthSrc(SRC_MAC) |
| 163 | .setEthDst(DST_MAC) |
| 164 | .setOutput(PORT_1) |
| 165 | .build(); |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame^] | 166 | PiAction mappedAction = interpreter.mapTreatment( |
| 167 | treatment, FabricConstants.FABRIC_INGRESS_NEXT_HASHED); |
| 168 | PiActionParam ethSrcParam = new PiActionParam(FabricConstants.SMAC, SRC_MAC.toBytes()); |
| 169 | PiActionParam ethDstParam = new PiActionParam(FabricConstants.DMAC, DST_MAC.toBytes()); |
| 170 | PiActionParam portParam = new PiActionParam(FabricConstants.PORT_NUM, PORT_1.toLong()); |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 171 | PiAction expectedAction = PiAction.builder() |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame^] | 172 | .withId(FabricConstants.FABRIC_INGRESS_NEXT_ROUTING_HASHED) |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 173 | .withParameters(ImmutableList.of(ethSrcParam, ethDstParam, portParam)) |
| 174 | .build(); |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame^] | 175 | assertEquals(expectedAction, mappedAction); |
| 176 | } |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 177 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame^] | 178 | /** |
| 179 | * Map treatment for hashed table to routing v4 action. |
| 180 | */ |
| 181 | @Test |
| 182 | public void testNextTreatmentHashedRoutingMpls() throws Exception { |
| 183 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 184 | .setEthSrc(SRC_MAC) |
| 185 | .setEthDst(DST_MAC) |
| 186 | .setOutput(PORT_1) |
| 187 | .pushMpls() |
| 188 | .setMpls(MPLS_10) |
| 189 | .build(); |
| 190 | PiAction mappedAction = interpreter.mapTreatment( |
| 191 | treatment, FabricConstants.FABRIC_INGRESS_NEXT_HASHED); |
| 192 | PiActionParam ethSrcParam = new PiActionParam(FabricConstants.SMAC, SRC_MAC.toBytes()); |
| 193 | PiActionParam ethDstParam = new PiActionParam(FabricConstants.DMAC, DST_MAC.toBytes()); |
| 194 | PiActionParam portParam = new PiActionParam(FabricConstants.PORT_NUM, PORT_1.toLong()); |
| 195 | PiActionParam mplsParam = new PiActionParam(FabricConstants.LABEL, MPLS_10.toInt()); |
| 196 | PiAction expectedAction = PiAction.builder() |
| 197 | .withId(FabricConstants.FABRIC_INGRESS_NEXT_MPLS_ROUTING_HASHED) |
| 198 | .withParameters(ImmutableList.of(ethSrcParam, ethDstParam, portParam, mplsParam)) |
| 199 | .build(); |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 200 | assertEquals(expectedAction, mappedAction); |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Map treatment to set_vlan_output action. |
| 205 | */ |
| 206 | @Test |
| 207 | public void testNextTreatment3() throws Exception { |
| 208 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 209 | .setVlanId(VLAN_100) |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 210 | .build(); |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame^] | 211 | PiAction mappedAction = interpreter.mapTreatment( |
| 212 | treatment, FabricConstants.FABRIC_INGRESS_NEXT_NEXT_VLAN); |
| 213 | PiActionParam vlanParam = new PiActionParam( |
| 214 | FabricConstants.VLAN_ID, VLAN_100.toShort()); |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 215 | PiAction expectedAction = PiAction.builder() |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame^] | 216 | .withId(FabricConstants.FABRIC_INGRESS_NEXT_SET_VLAN) |
| 217 | .withParameter(vlanParam) |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 218 | .build(); |
| 219 | assertEquals(expectedAction, mappedAction); |
| 220 | } |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 221 | } |