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; |
| 25 | import org.onosproject.net.pi.model.PiPipeconf; |
| 26 | import org.onosproject.net.pi.model.PiPipeconfId; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 27 | import org.onosproject.net.pi.model.PiPipelineModel; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 28 | import org.onosproject.net.pi.runtime.PiAction; |
| 29 | import org.onosproject.net.pi.runtime.PiActionId; |
| 30 | import org.onosproject.net.pi.runtime.PiActionParam; |
| 31 | import org.onosproject.net.pi.runtime.PiActionParamId; |
| 32 | import org.onosproject.net.pi.runtime.PiHeaderFieldId; |
Carmelo Cascone | 0e896a0 | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 33 | import org.onosproject.net.pi.runtime.PiMatchKey; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 34 | import org.onosproject.net.pi.runtime.PiTableEntry; |
| 35 | import org.onosproject.net.pi.runtime.PiTableId; |
| 36 | import org.onosproject.net.pi.runtime.PiTernaryFieldMatch; |
| 37 | import p4.P4RuntimeOuterClass.Action; |
| 38 | import p4.P4RuntimeOuterClass.TableEntry; |
| 39 | |
| 40 | import java.net.URL; |
| 41 | import java.util.Collection; |
| 42 | import java.util.Random; |
| 43 | |
| 44 | import static org.hamcrest.MatcherAssert.assertThat; |
| 45 | import static org.hamcrest.Matchers.hasSize; |
| 46 | import static org.hamcrest.Matchers.is; |
| 47 | import static org.onlab.util.ImmutableByteSequence.copyFrom; |
| 48 | import static org.onlab.util.ImmutableByteSequence.fit; |
Carmelo Cascone | 0e896a0 | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 49 | import static org.onlab.util.ImmutableByteSequence.ofOnes; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 50 | import static org.onosproject.net.pi.model.PiPipeconf.ExtensionType.P4_INFO_TEXT; |
| 51 | import static org.onosproject.p4runtime.ctl.TableEntryEncoder.decode; |
| 52 | import static org.onosproject.p4runtime.ctl.TableEntryEncoder.encode; |
Andrea Campanella | fc1d34c | 2017-07-18 17:01:41 +0200 | [diff] [blame] | 53 | |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 54 | /** |
| 55 | * Test for P4 runtime table entry encoder. |
| 56 | */ |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 57 | public class TableEntryEncoderTest { |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 58 | private static final String TABLE_0 = "table0"; |
| 59 | private static final String SET_EGRESS_PORT = "set_egress_port"; |
| 60 | private static final String PORT = "port"; |
| 61 | private static final String ETHERNET = "ethernet"; |
| 62 | private static final String DST_ADDR = "dstAddr"; |
| 63 | private static final String SRC_ADDR = "srcAddr"; |
| 64 | private static final String STANDARD_METADATA = "standard_metadata"; |
| 65 | private static final String INGRESS_PORT = "ingress_port"; |
| 66 | private static final String ETHER_TYPE = "etherType"; |
| 67 | |
| 68 | private final Random rand = new Random(); |
| 69 | private final URL p4InfoUrl = this.getClass().getResource("/default.p4info"); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 70 | |
| 71 | private final PiPipeconf defaultPipeconf = DefaultPiPipeconf.builder() |
| 72 | .withId(new PiPipeconfId("mock")) |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 73 | .withPipelineModel(EasyMock.niceMock(PiPipelineModel.class)) |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 74 | .addExtension(P4_INFO_TEXT, p4InfoUrl) |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 75 | .build(); |
| 76 | |
| 77 | private final P4InfoBrowser browser = PipeconfHelper.getP4InfoBrowser(defaultPipeconf); |
| 78 | private final ImmutableByteSequence ethAddr = fit(copyFrom(rand.nextInt()), 48); |
| 79 | private final ImmutableByteSequence portValue = copyFrom((short) rand.nextInt()); |
| 80 | private final PiHeaderFieldId ethDstAddrFieldId = PiHeaderFieldId.of(ETHERNET, DST_ADDR); |
| 81 | private final PiHeaderFieldId ethSrcAddrFieldId = PiHeaderFieldId.of(ETHERNET, SRC_ADDR); |
| 82 | private final PiHeaderFieldId inPortFieldId = PiHeaderFieldId.of(STANDARD_METADATA, INGRESS_PORT); |
| 83 | private final PiHeaderFieldId ethTypeFieldId = PiHeaderFieldId.of(ETHERNET, ETHER_TYPE); |
| 84 | private final PiActionParamId portParamId = PiActionParamId.of(PORT); |
| 85 | private final PiActionId outActionId = PiActionId.of(SET_EGRESS_PORT); |
| 86 | private final PiTableId tableId = PiTableId.of(TABLE_0); |
| 87 | |
| 88 | private final PiTableEntry piTableEntry = PiTableEntry |
| 89 | .builder() |
| 90 | .forTable(tableId) |
Carmelo Cascone | 0e896a0 | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 91 | .withMatchKey(PiMatchKey.builder() |
| 92 | .addFieldMatch(new PiTernaryFieldMatch(ethDstAddrFieldId, ethAddr, ofOnes(6))) |
| 93 | .addFieldMatch(new PiTernaryFieldMatch(ethSrcAddrFieldId, ethAddr, ofOnes(6))) |
| 94 | .addFieldMatch(new PiTernaryFieldMatch(inPortFieldId, portValue, ofOnes(2))) |
| 95 | .addFieldMatch(new PiTernaryFieldMatch(ethTypeFieldId, portValue, ofOnes(2))) |
| 96 | .build()) |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 97 | .withAction(PiAction |
Carmelo Cascone | 0e896a0 | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 98 | .builder() |
| 99 | .withId(outActionId) |
| 100 | .withParameter(new PiActionParam(portParamId, portValue)) |
| 101 | .build()) |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 102 | .withPriority(1) |
| 103 | .withCookie(2) |
| 104 | .build(); |
| 105 | |
| 106 | public TableEntryEncoderTest() throws ImmutableByteSequence.ByteSequenceTrimException { |
| 107 | } |
| 108 | |
| 109 | @Test |
| 110 | public void testP4InfoBrowser() throws Exception { |
| 111 | |
| 112 | P4InfoBrowser browser = PipeconfHelper.getP4InfoBrowser(defaultPipeconf); |
| 113 | |
| 114 | assertThat(browser.tables().hasName(TABLE_0), is(true)); |
| 115 | assertThat(browser.actions().hasName(SET_EGRESS_PORT), is(true)); |
| 116 | |
| 117 | int tableId = browser.tables().getByName(TABLE_0).getPreamble().getId(); |
| 118 | int actionId = browser.actions().getByName(SET_EGRESS_PORT).getPreamble().getId(); |
| 119 | |
| 120 | assertThat(browser.matchFields(tableId).hasName(STANDARD_METADATA + "." + INGRESS_PORT), is(true)); |
| 121 | assertThat(browser.actionParams(actionId).hasName(PORT), is(true)); |
| 122 | |
| 123 | // TODO: improve, assert browsing other entities (counters, meters, etc.) |
| 124 | } |
| 125 | |
| 126 | @Test |
| 127 | public void testTableEntryEncoder() |
| 128 | throws P4InfoBrowser.NotFoundException, ImmutableByteSequence.ByteSequenceTrimException { |
| 129 | |
| 130 | Collection<TableEntry> result = encode(Lists.newArrayList(piTableEntry), defaultPipeconf); |
| 131 | assertThat(result, hasSize(1)); |
| 132 | |
| 133 | TableEntry tableEntryMsg = result.iterator().next(); |
| 134 | |
| 135 | Collection<PiTableEntry> decodedResults = decode(Lists.newArrayList(tableEntryMsg), defaultPipeconf); |
| 136 | PiTableEntry decodedPiTableEntry = decodedResults.iterator().next(); |
| 137 | |
| 138 | // Test equality for decoded entry. |
| 139 | new EqualsTester() |
| 140 | .addEqualityGroup(piTableEntry, decodedPiTableEntry) |
| 141 | .testEquals(); |
| 142 | |
| 143 | // Table ID. |
| 144 | int p4InfoTableId = browser.tables().getByName(tableId.id()).getPreamble().getId(); |
| 145 | int encodedTableId = tableEntryMsg.getTableId(); |
| 146 | assertThat(encodedTableId, is(p4InfoTableId)); |
| 147 | |
| 148 | // Ternary match. |
| 149 | byte[] encodedTernaryMatchValue = tableEntryMsg.getMatch(0).getTernary().getValue().toByteArray(); |
| 150 | assertThat(encodedTernaryMatchValue, is(ethAddr.asArray())); |
| 151 | |
| 152 | Action actionMsg = tableEntryMsg.getAction().getAction(); |
| 153 | |
| 154 | // Action ID. |
| 155 | int p4InfoActionId = browser.actions().getByName(outActionId.name()).getPreamble().getId(); |
| 156 | int encodedActionId = actionMsg.getActionId(); |
| 157 | assertThat(encodedActionId, is(p4InfoActionId)); |
| 158 | |
| 159 | // Action param ID. |
| 160 | int p4InfoActionParamId = browser.actionParams(p4InfoActionId).getByName(portParamId.name()).getId(); |
| 161 | int encodedActionParamId = actionMsg.getParams(0).getParamId(); |
| 162 | assertThat(encodedActionParamId, is(p4InfoActionParamId)); |
| 163 | |
| 164 | // Action param value. |
| 165 | byte[] encodedActionParam = actionMsg.getParams(0).getValue().toByteArray(); |
| 166 | assertThat(encodedActionParam, is(portValue.asArray())); |
| 167 | |
| 168 | // TODO: improve, assert other field match types (ternary, LPM) |
| 169 | } |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 170 | } |