Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 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.p4runtime.ctl; |
| 18 | |
| 19 | import com.google.common.collect.Lists; |
| 20 | import com.google.common.testing.EqualsTester; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 21 | import org.easymock.EasyMock; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 22 | import org.junit.Test; |
| 23 | import org.onlab.util.ImmutableByteSequence; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 24 | import org.onosproject.net.pi.model.DefaultPiPipeconf; |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 25 | import org.onosproject.net.pi.model.PiActionId; |
| 26 | import org.onosproject.net.pi.model.PiActionParamId; |
| 27 | import org.onosproject.net.pi.model.PiMatchFieldId; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 28 | import org.onosproject.net.pi.model.PiPipeconf; |
| 29 | import org.onosproject.net.pi.model.PiPipeconfId; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 30 | import org.onosproject.net.pi.model.PiPipelineModel; |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 31 | import org.onosproject.net.pi.model.PiTableId; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 32 | import org.onosproject.net.pi.runtime.PiAction; |
Yi Tseng | 9539082 | 2018-01-22 17:57:22 -0800 | [diff] [blame] | 33 | import org.onosproject.net.pi.runtime.PiActionGroupId; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 34 | import org.onosproject.net.pi.runtime.PiActionParam; |
Yi Tseng | 9539082 | 2018-01-22 17:57:22 -0800 | [diff] [blame] | 35 | import org.onosproject.net.pi.runtime.PiExactFieldMatch; |
Carmelo Cascone | 0e896a0 | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 36 | import org.onosproject.net.pi.runtime.PiMatchKey; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 37 | import org.onosproject.net.pi.runtime.PiTableEntry; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 38 | import org.onosproject.net.pi.runtime.PiTernaryFieldMatch; |
Carmelo Cascone | 6af4e17 | 2018-06-15 16:01:30 +0200 | [diff] [blame^] | 39 | import p4.v1.P4RuntimeOuterClass.Action; |
| 40 | import p4.v1.P4RuntimeOuterClass.TableEntry; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 41 | |
| 42 | import java.net.URL; |
| 43 | import java.util.Collection; |
| 44 | import java.util.Random; |
| 45 | |
| 46 | import static org.hamcrest.MatcherAssert.assertThat; |
| 47 | import static org.hamcrest.Matchers.hasSize; |
| 48 | import static org.hamcrest.Matchers.is; |
Carmelo Cascone | ca94bcf | 2017-10-27 14:16:59 -0700 | [diff] [blame] | 49 | import static org.onlab.util.ImmutableByteSequence.copyFrom; |
Carmelo Cascone | ca94bcf | 2017-10-27 14:16:59 -0700 | [diff] [blame] | 50 | import static org.onlab.util.ImmutableByteSequence.ofOnes; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 51 | import static org.onosproject.net.pi.model.PiPipeconf.ExtensionType.P4_INFO_TEXT; |
| 52 | import static org.onosproject.p4runtime.ctl.TableEntryEncoder.decode; |
| 53 | import static org.onosproject.p4runtime.ctl.TableEntryEncoder.encode; |
Andrea Campanella | fc1d34c | 2017-07-18 17:01:41 +0200 | [diff] [blame] | 54 | |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 55 | /** |
| 56 | * Test for P4 runtime table entry encoder. |
| 57 | */ |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 58 | public class TableEntryEncoderTest { |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 59 | private static final String DOT = "."; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 60 | private static final String TABLE_0 = "table0"; |
Yi Tseng | 9539082 | 2018-01-22 17:57:22 -0800 | [diff] [blame] | 61 | private static final String TABLE_ECMP = "ecmp"; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 62 | private static final String SET_EGRESS_PORT = "set_egress_port"; |
| 63 | private static final String PORT = "port"; |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 64 | private static final String HDR = "hdr"; |
Yi Tseng | 9539082 | 2018-01-22 17:57:22 -0800 | [diff] [blame] | 65 | private static final String META = "meta"; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 66 | private static final String ETHERNET = "ethernet"; |
| 67 | private static final String DST_ADDR = "dstAddr"; |
| 68 | private static final String SRC_ADDR = "srcAddr"; |
| 69 | private static final String STANDARD_METADATA = "standard_metadata"; |
Yi Tseng | 9539082 | 2018-01-22 17:57:22 -0800 | [diff] [blame] | 70 | private static final String ECMP_METADATA = "ecmp_metadata"; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 71 | private static final String INGRESS_PORT = "ingress_port"; |
| 72 | private static final String ETHER_TYPE = "etherType"; |
Yi Tseng | 9539082 | 2018-01-22 17:57:22 -0800 | [diff] [blame] | 73 | private static final String ECMP_GROUP_ID = "ecmp_group_id"; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 74 | |
| 75 | private final Random rand = new Random(); |
Carmelo Cascone | ca94bcf | 2017-10-27 14:16:59 -0700 | [diff] [blame] | 76 | private final URL p4InfoUrl = this.getClass().getResource("/test.p4info"); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 77 | |
| 78 | private final PiPipeconf defaultPipeconf = DefaultPiPipeconf.builder() |
| 79 | .withId(new PiPipeconfId("mock")) |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 80 | .withPipelineModel(EasyMock.niceMock(PiPipelineModel.class)) |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 81 | .addExtension(P4_INFO_TEXT, p4InfoUrl) |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 82 | .build(); |
| 83 | |
| 84 | private final P4InfoBrowser browser = PipeconfHelper.getP4InfoBrowser(defaultPipeconf); |
Carmelo Cascone | 8a571af | 2018-04-06 23:17:04 -0700 | [diff] [blame] | 85 | private final ImmutableByteSequence ethAddr = copyFrom(rand.nextInt()).fit(48); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 86 | private final ImmutableByteSequence portValue = copyFrom((short) rand.nextInt()); |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 87 | private final PiMatchFieldId ethDstAddrFieldId = PiMatchFieldId.of(HDR + DOT + ETHERNET + DOT + DST_ADDR); |
| 88 | private final PiMatchFieldId ethSrcAddrFieldId = PiMatchFieldId.of(HDR + DOT + ETHERNET + DOT + SRC_ADDR); |
| 89 | private final PiMatchFieldId inPortFieldId = PiMatchFieldId.of(STANDARD_METADATA + DOT + INGRESS_PORT); |
| 90 | private final PiMatchFieldId ethTypeFieldId = PiMatchFieldId.of(HDR + DOT + ETHERNET + DOT + ETHER_TYPE); |
Yi Tseng | 9539082 | 2018-01-22 17:57:22 -0800 | [diff] [blame] | 91 | private final PiMatchFieldId ecmpGroupFieldId = |
| 92 | PiMatchFieldId.of(META + DOT + ECMP_METADATA + DOT + ECMP_GROUP_ID); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 93 | private final PiActionParamId portParamId = PiActionParamId.of(PORT); |
| 94 | private final PiActionId outActionId = PiActionId.of(SET_EGRESS_PORT); |
| 95 | private final PiTableId tableId = PiTableId.of(TABLE_0); |
Yi Tseng | 9539082 | 2018-01-22 17:57:22 -0800 | [diff] [blame] | 96 | private final PiTableId ecmpTableId = PiTableId.of(TABLE_ECMP); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 97 | |
| 98 | private final PiTableEntry piTableEntry = PiTableEntry |
| 99 | .builder() |
| 100 | .forTable(tableId) |
Carmelo Cascone | 0e896a0 | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 101 | .withMatchKey(PiMatchKey.builder() |
| 102 | .addFieldMatch(new PiTernaryFieldMatch(ethDstAddrFieldId, ethAddr, ofOnes(6))) |
| 103 | .addFieldMatch(new PiTernaryFieldMatch(ethSrcAddrFieldId, ethAddr, ofOnes(6))) |
| 104 | .addFieldMatch(new PiTernaryFieldMatch(inPortFieldId, portValue, ofOnes(2))) |
| 105 | .addFieldMatch(new PiTernaryFieldMatch(ethTypeFieldId, portValue, ofOnes(2))) |
| 106 | .build()) |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 107 | .withAction(PiAction |
Carmelo Cascone | 0e896a0 | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 108 | .builder() |
| 109 | .withId(outActionId) |
| 110 | .withParameter(new PiActionParam(portParamId, portValue)) |
| 111 | .build()) |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 112 | .withPriority(1) |
| 113 | .withCookie(2) |
| 114 | .build(); |
| 115 | |
Yi Tseng | d28936e | 2018-02-23 22:11:11 +0100 | [diff] [blame] | 116 | private final PiTableEntry piTableEntryWithoutAction = PiTableEntry |
| 117 | .builder() |
| 118 | .forTable(tableId) |
| 119 | .withMatchKey(PiMatchKey.builder() |
| 120 | .addFieldMatch(new PiTernaryFieldMatch(ethDstAddrFieldId, ethAddr, ofOnes(6))) |
| 121 | .addFieldMatch(new PiTernaryFieldMatch(ethSrcAddrFieldId, ethAddr, ofOnes(6))) |
| 122 | .addFieldMatch(new PiTernaryFieldMatch(inPortFieldId, portValue, ofOnes(2))) |
| 123 | .addFieldMatch(new PiTernaryFieldMatch(ethTypeFieldId, portValue, ofOnes(2))) |
| 124 | .build()) |
| 125 | .withPriority(1) |
| 126 | .withCookie(2) |
| 127 | .build(); |
| 128 | |
Yi Tseng | 9539082 | 2018-01-22 17:57:22 -0800 | [diff] [blame] | 129 | private final PiTableEntry piTableEntryWithGroupAction = PiTableEntry |
| 130 | .builder() |
| 131 | .forTable(ecmpTableId) |
| 132 | .withMatchKey(PiMatchKey.builder() |
| 133 | .addFieldMatch(new PiExactFieldMatch(ecmpGroupFieldId, ofOnes(1))) |
| 134 | .build()) |
| 135 | .withAction(PiActionGroupId.of(1)) |
| 136 | .withPriority(1) |
| 137 | .withCookie(2) |
| 138 | .build(); |
| 139 | |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 140 | public TableEntryEncoderTest() throws ImmutableByteSequence.ByteSequenceTrimException { |
| 141 | } |
| 142 | |
| 143 | @Test |
| 144 | public void testP4InfoBrowser() throws Exception { |
| 145 | |
| 146 | P4InfoBrowser browser = PipeconfHelper.getP4InfoBrowser(defaultPipeconf); |
| 147 | |
| 148 | assertThat(browser.tables().hasName(TABLE_0), is(true)); |
| 149 | assertThat(browser.actions().hasName(SET_EGRESS_PORT), is(true)); |
| 150 | |
Carmelo Cascone | cb0a49c | 2017-10-03 14:32:23 +0200 | [diff] [blame] | 151 | int tableId = browser.tables().getByName(TABLE_0).getPreamble().getId(); |
| 152 | int actionId = browser.actions().getByName(SET_EGRESS_PORT).getPreamble().getId(); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 153 | |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 154 | assertThat(browser.matchFields(tableId).hasName(STANDARD_METADATA + DOT + INGRESS_PORT), is(true)); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 155 | assertThat(browser.actionParams(actionId).hasName(PORT), is(true)); |
| 156 | |
| 157 | // TODO: improve, assert browsing other entities (counters, meters, etc.) |
| 158 | } |
| 159 | |
| 160 | @Test |
Carmelo Cascone | 5bc7e10 | 2018-02-18 18:27:55 -0800 | [diff] [blame] | 161 | public void testTableEntryEncoder() throws Exception { |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 162 | |
| 163 | Collection<TableEntry> result = encode(Lists.newArrayList(piTableEntry), defaultPipeconf); |
| 164 | assertThat(result, hasSize(1)); |
| 165 | |
| 166 | TableEntry tableEntryMsg = result.iterator().next(); |
| 167 | |
| 168 | Collection<PiTableEntry> decodedResults = decode(Lists.newArrayList(tableEntryMsg), defaultPipeconf); |
| 169 | PiTableEntry decodedPiTableEntry = decodedResults.iterator().next(); |
| 170 | |
| 171 | // Test equality for decoded entry. |
| 172 | new EqualsTester() |
| 173 | .addEqualityGroup(piTableEntry, decodedPiTableEntry) |
| 174 | .testEquals(); |
| 175 | |
| 176 | // Table ID. |
Carmelo Cascone | cb0a49c | 2017-10-03 14:32:23 +0200 | [diff] [blame] | 177 | int p4InfoTableId = browser.tables().getByName(tableId.id()).getPreamble().getId(); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 178 | int encodedTableId = tableEntryMsg.getTableId(); |
| 179 | assertThat(encodedTableId, is(p4InfoTableId)); |
| 180 | |
| 181 | // Ternary match. |
| 182 | byte[] encodedTernaryMatchValue = tableEntryMsg.getMatch(0).getTernary().getValue().toByteArray(); |
| 183 | assertThat(encodedTernaryMatchValue, is(ethAddr.asArray())); |
| 184 | |
| 185 | Action actionMsg = tableEntryMsg.getAction().getAction(); |
| 186 | |
| 187 | // Action ID. |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 188 | int p4InfoActionId = browser.actions().getByName(outActionId.toString()).getPreamble().getId(); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 189 | int encodedActionId = actionMsg.getActionId(); |
| 190 | assertThat(encodedActionId, is(p4InfoActionId)); |
| 191 | |
| 192 | // Action param ID. |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 193 | int p4InfoActionParamId = browser.actionParams(p4InfoActionId).getByName(portParamId.toString()).getId(); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 194 | int encodedActionParamId = actionMsg.getParams(0).getParamId(); |
| 195 | assertThat(encodedActionParamId, is(p4InfoActionParamId)); |
| 196 | |
| 197 | // Action param value. |
| 198 | byte[] encodedActionParam = actionMsg.getParams(0).getValue().toByteArray(); |
| 199 | assertThat(encodedActionParam, is(portValue.asArray())); |
| 200 | |
| 201 | // TODO: improve, assert other field match types (ternary, LPM) |
| 202 | } |
Yi Tseng | 9539082 | 2018-01-22 17:57:22 -0800 | [diff] [blame] | 203 | |
| 204 | @Test |
Carmelo Cascone | 5bc7e10 | 2018-02-18 18:27:55 -0800 | [diff] [blame] | 205 | public void testActopProfileGroup() throws Exception { |
Yi Tseng | 9539082 | 2018-01-22 17:57:22 -0800 | [diff] [blame] | 206 | Collection<TableEntry> result = encode(Lists.newArrayList(piTableEntryWithGroupAction), defaultPipeconf); |
| 207 | assertThat(result, hasSize(1)); |
| 208 | |
| 209 | TableEntry tableEntryMsg = result.iterator().next(); |
| 210 | |
| 211 | Collection<PiTableEntry> decodedResults = decode(Lists.newArrayList(tableEntryMsg), defaultPipeconf); |
| 212 | PiTableEntry decodedPiTableEntry = decodedResults.iterator().next(); |
| 213 | |
| 214 | // Test equality for decoded entry. |
| 215 | new EqualsTester() |
| 216 | .addEqualityGroup(piTableEntryWithGroupAction, decodedPiTableEntry) |
| 217 | .testEquals(); |
| 218 | |
| 219 | // Table ID. |
| 220 | int p4InfoTableId = browser.tables().getByName(ecmpTableId.id()).getPreamble().getId(); |
| 221 | int encodedTableId = tableEntryMsg.getTableId(); |
| 222 | assertThat(encodedTableId, is(p4InfoTableId)); |
| 223 | |
| 224 | // Exact match. |
| 225 | byte[] encodedTernaryMatchValue = tableEntryMsg.getMatch(0).getExact().getValue().toByteArray(); |
| 226 | assertThat(encodedTernaryMatchValue, is(new byte[]{(byte) 0xff})); |
| 227 | |
| 228 | // Action profile group id |
| 229 | int actionProfileGroupId = tableEntryMsg.getAction().getActionProfileGroupId(); |
| 230 | assertThat(actionProfileGroupId, is(1)); |
| 231 | } |
Yi Tseng | d28936e | 2018-02-23 22:11:11 +0100 | [diff] [blame] | 232 | |
| 233 | @Test |
| 234 | public void testEncodeWithNoAction() throws Exception { |
| 235 | Collection<TableEntry> result = encode(Lists.newArrayList(piTableEntryWithoutAction), defaultPipeconf); |
| 236 | assertThat(result, hasSize(1)); |
| 237 | |
| 238 | TableEntry tableEntryMsg = result.iterator().next(); |
| 239 | |
| 240 | Collection<PiTableEntry> decodedResults = decode(Lists.newArrayList(tableEntryMsg), defaultPipeconf); |
| 241 | PiTableEntry decodedPiTableEntry = decodedResults.iterator().next(); |
| 242 | |
| 243 | // Test equality for decoded entry. |
| 244 | new EqualsTester() |
| 245 | .addEqualityGroup(piTableEntryWithoutAction, decodedPiTableEntry) |
| 246 | .testEquals(); |
| 247 | |
| 248 | // Table ID. |
| 249 | int p4InfoTableId = browser.tables().getByName(tableId.id()).getPreamble().getId(); |
| 250 | int encodedTableId = tableEntryMsg.getTableId(); |
| 251 | assertThat(encodedTableId, is(p4InfoTableId)); |
| 252 | |
| 253 | // Ternary match. |
| 254 | byte[] encodedTernaryMatchValue = tableEntryMsg.getMatch(0).getTernary().getValue().toByteArray(); |
| 255 | assertThat(encodedTernaryMatchValue, is(ethAddr.asArray())); |
| 256 | |
| 257 | // no action |
| 258 | assertThat(tableEntryMsg.hasAction(), is(false)); |
| 259 | |
| 260 | // TODO: improve, assert other field match types (ternary, LPM) |
| 261 | } |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 262 | } |