blob: b434b4da801bab36ed637f35f87c269b8b33a00a [file] [log] [blame]
Carmelo Cascone8d99b172017-07-18 17:26:31 -04001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Carmelo Cascone8d99b172017-07-18 17:26:31 -04003 *
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.p4runtime.ctl;
18
19import com.google.common.collect.Lists;
20import com.google.common.testing.EqualsTester;
Yi Tseng82512da2017-08-16 19:46:36 -070021import org.easymock.EasyMock;
Carmelo Cascone8d99b172017-07-18 17:26:31 -040022import org.junit.Test;
23import org.onlab.util.ImmutableByteSequence;
Carmelo Cascone8d99b172017-07-18 17:26:31 -040024import org.onosproject.net.pi.model.DefaultPiPipeconf;
25import org.onosproject.net.pi.model.PiPipeconf;
26import org.onosproject.net.pi.model.PiPipeconfId;
Yi Tseng82512da2017-08-16 19:46:36 -070027import org.onosproject.net.pi.model.PiPipelineModel;
Carmelo Cascone8d99b172017-07-18 17:26:31 -040028import org.onosproject.net.pi.runtime.PiAction;
29import org.onosproject.net.pi.runtime.PiActionId;
30import org.onosproject.net.pi.runtime.PiActionParam;
31import org.onosproject.net.pi.runtime.PiActionParamId;
32import org.onosproject.net.pi.runtime.PiHeaderFieldId;
Carmelo Cascone0e896a02017-07-31 07:22:27 +020033import org.onosproject.net.pi.runtime.PiMatchKey;
Carmelo Cascone8d99b172017-07-18 17:26:31 -040034import org.onosproject.net.pi.runtime.PiTableEntry;
35import org.onosproject.net.pi.runtime.PiTableId;
36import org.onosproject.net.pi.runtime.PiTernaryFieldMatch;
37import p4.P4RuntimeOuterClass.Action;
38import p4.P4RuntimeOuterClass.TableEntry;
39
40import java.net.URL;
41import java.util.Collection;
42import java.util.Random;
43
44import static org.hamcrest.MatcherAssert.assertThat;
45import static org.hamcrest.Matchers.hasSize;
46import static org.hamcrest.Matchers.is;
Carmelo Cascone87b9b392017-10-02 18:33:20 +020047import static org.onlab.util.ImmutableByteSequence.*;
Carmelo Cascone8d99b172017-07-18 17:26:31 -040048import static org.onosproject.net.pi.model.PiPipeconf.ExtensionType.P4_INFO_TEXT;
49import static org.onosproject.p4runtime.ctl.TableEntryEncoder.decode;
50import static org.onosproject.p4runtime.ctl.TableEntryEncoder.encode;
Andrea Campanellafc1d34c2017-07-18 17:01:41 +020051
Yi Tseng82512da2017-08-16 19:46:36 -070052/**
53 * Test for P4 runtime table entry encoder.
54 */
Carmelo Cascone8d99b172017-07-18 17:26:31 -040055public class TableEntryEncoderTest {
Carmelo Cascone8d99b172017-07-18 17:26:31 -040056 private static final String TABLE_0 = "table0";
57 private static final String SET_EGRESS_PORT = "set_egress_port";
58 private static final String PORT = "port";
59 private static final String ETHERNET = "ethernet";
60 private static final String DST_ADDR = "dstAddr";
61 private static final String SRC_ADDR = "srcAddr";
62 private static final String STANDARD_METADATA = "standard_metadata";
63 private static final String INGRESS_PORT = "ingress_port";
64 private static final String ETHER_TYPE = "etherType";
65
66 private final Random rand = new Random();
67 private final URL p4InfoUrl = this.getClass().getResource("/default.p4info");
Carmelo Cascone8d99b172017-07-18 17:26:31 -040068
69 private final PiPipeconf defaultPipeconf = DefaultPiPipeconf.builder()
70 .withId(new PiPipeconfId("mock"))
Yi Tseng82512da2017-08-16 19:46:36 -070071 .withPipelineModel(EasyMock.niceMock(PiPipelineModel.class))
Carmelo Cascone8d99b172017-07-18 17:26:31 -040072 .addExtension(P4_INFO_TEXT, p4InfoUrl)
Carmelo Cascone8d99b172017-07-18 17:26:31 -040073 .build();
74
75 private final P4InfoBrowser browser = PipeconfHelper.getP4InfoBrowser(defaultPipeconf);
76 private final ImmutableByteSequence ethAddr = fit(copyFrom(rand.nextInt()), 48);
77 private final ImmutableByteSequence portValue = copyFrom((short) rand.nextInt());
78 private final PiHeaderFieldId ethDstAddrFieldId = PiHeaderFieldId.of(ETHERNET, DST_ADDR);
79 private final PiHeaderFieldId ethSrcAddrFieldId = PiHeaderFieldId.of(ETHERNET, SRC_ADDR);
80 private final PiHeaderFieldId inPortFieldId = PiHeaderFieldId.of(STANDARD_METADATA, INGRESS_PORT);
81 private final PiHeaderFieldId ethTypeFieldId = PiHeaderFieldId.of(ETHERNET, ETHER_TYPE);
82 private final PiActionParamId portParamId = PiActionParamId.of(PORT);
83 private final PiActionId outActionId = PiActionId.of(SET_EGRESS_PORT);
84 private final PiTableId tableId = PiTableId.of(TABLE_0);
85
86 private final PiTableEntry piTableEntry = PiTableEntry
87 .builder()
88 .forTable(tableId)
Carmelo Cascone0e896a02017-07-31 07:22:27 +020089 .withMatchKey(PiMatchKey.builder()
90 .addFieldMatch(new PiTernaryFieldMatch(ethDstAddrFieldId, ethAddr, ofOnes(6)))
91 .addFieldMatch(new PiTernaryFieldMatch(ethSrcAddrFieldId, ethAddr, ofOnes(6)))
92 .addFieldMatch(new PiTernaryFieldMatch(inPortFieldId, portValue, ofOnes(2)))
93 .addFieldMatch(new PiTernaryFieldMatch(ethTypeFieldId, portValue, ofOnes(2)))
94 .build())
Carmelo Cascone8d99b172017-07-18 17:26:31 -040095 .withAction(PiAction
Carmelo Cascone0e896a02017-07-31 07:22:27 +020096 .builder()
97 .withId(outActionId)
98 .withParameter(new PiActionParam(portParamId, portValue))
99 .build())
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400100 .withPriority(1)
101 .withCookie(2)
102 .build();
103
104 public TableEntryEncoderTest() throws ImmutableByteSequence.ByteSequenceTrimException {
105 }
106
107 @Test
108 public void testP4InfoBrowser() throws Exception {
109
110 P4InfoBrowser browser = PipeconfHelper.getP4InfoBrowser(defaultPipeconf);
111
112 assertThat(browser.tables().hasName(TABLE_0), is(true));
113 assertThat(browser.actions().hasName(SET_EGRESS_PORT), is(true));
114
Carmelo Cascone87b9b392017-10-02 18:33:20 +0200115 int tableId = browser.tables().getByNameOrAlias(TABLE_0).getPreamble().getId();
116 int actionId = browser.actions().getByNameOrAlias(SET_EGRESS_PORT).getPreamble().getId();
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400117
118 assertThat(browser.matchFields(tableId).hasName(STANDARD_METADATA + "." + INGRESS_PORT), is(true));
119 assertThat(browser.actionParams(actionId).hasName(PORT), is(true));
120
121 // TODO: improve, assert browsing other entities (counters, meters, etc.)
122 }
123
124 @Test
125 public void testTableEntryEncoder()
126 throws P4InfoBrowser.NotFoundException, ImmutableByteSequence.ByteSequenceTrimException {
127
128 Collection<TableEntry> result = encode(Lists.newArrayList(piTableEntry), defaultPipeconf);
129 assertThat(result, hasSize(1));
130
131 TableEntry tableEntryMsg = result.iterator().next();
132
133 Collection<PiTableEntry> decodedResults = decode(Lists.newArrayList(tableEntryMsg), defaultPipeconf);
134 PiTableEntry decodedPiTableEntry = decodedResults.iterator().next();
135
136 // Test equality for decoded entry.
137 new EqualsTester()
138 .addEqualityGroup(piTableEntry, decodedPiTableEntry)
139 .testEquals();
140
141 // Table ID.
Carmelo Cascone87b9b392017-10-02 18:33:20 +0200142 int p4InfoTableId = browser.tables().getByNameOrAlias(tableId.id()).getPreamble().getId();
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400143 int encodedTableId = tableEntryMsg.getTableId();
144 assertThat(encodedTableId, is(p4InfoTableId));
145
146 // Ternary match.
147 byte[] encodedTernaryMatchValue = tableEntryMsg.getMatch(0).getTernary().getValue().toByteArray();
148 assertThat(encodedTernaryMatchValue, is(ethAddr.asArray()));
149
150 Action actionMsg = tableEntryMsg.getAction().getAction();
151
152 // Action ID.
Carmelo Cascone87b9b392017-10-02 18:33:20 +0200153 int p4InfoActionId = browser.actions().getByNameOrAlias(outActionId.name()).getPreamble().getId();
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400154 int encodedActionId = actionMsg.getActionId();
155 assertThat(encodedActionId, is(p4InfoActionId));
156
157 // Action param ID.
Carmelo Cascone87b9b392017-10-02 18:33:20 +0200158 int p4InfoActionParamId = browser.actionParams(p4InfoActionId).getByNameOrAlias(portParamId.name()).getId();
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400159 int encodedActionParamId = actionMsg.getParams(0).getParamId();
160 assertThat(encodedActionParamId, is(p4InfoActionParamId));
161
162 // Action param value.
163 byte[] encodedActionParam = actionMsg.getParams(0).getValue().toByteArray();
164 assertThat(encodedActionParam, is(portValue.asArray()));
165
166 // TODO: improve, assert other field match types (ternary, LPM)
167 }
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400168}