blob: 15774ac40f5ebc584e1400839cff574b0201fffc [file] [log] [blame]
Carmelo Cascone58136812018-07-19 03:40:16 +02001/*
2 * Copyright 2018-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
17package org.onosproject.net.pi.impl;
18
19import com.google.common.testing.EqualsTester;
20import org.junit.Before;
21import org.junit.Test;
22import org.onlab.packet.MacAddress;
23import org.onosproject.core.ApplicationId;
24import org.onosproject.core.DefaultApplicationId;
25import org.onosproject.net.DeviceId;
26import org.onosproject.net.PortNumber;
27import org.onosproject.net.flow.DefaultFlowRule;
28import org.onosproject.net.flow.DefaultTrafficSelector;
29import org.onosproject.net.flow.DefaultTrafficTreatment;
30import org.onosproject.net.flow.FlowRule;
31import org.onosproject.net.flow.TrafficSelector;
32import org.onosproject.net.flow.TrafficTreatment;
33import org.onosproject.net.pi.model.PiPipeconf;
34import org.onosproject.net.pi.runtime.PiMatchKey;
35import org.onosproject.net.pi.runtime.PiTableEntry;
36import org.onosproject.net.pi.runtime.PiTernaryFieldMatch;
37import org.onosproject.pipelines.basic.PipeconfLoader;
38
39import java.util.Optional;
40import java.util.Random;
41
42import static org.hamcrest.CoreMatchers.equalTo;
43import static org.hamcrest.CoreMatchers.is;
44import static org.hamcrest.MatcherAssert.assertThat;
45import static org.onosproject.pipelines.basic.BasicConstants.HDR_ETH_DST_ID;
46import static org.onosproject.pipelines.basic.BasicConstants.HDR_ETH_SRC_ID;
47import static org.onosproject.pipelines.basic.BasicConstants.HDR_ETH_TYPE_ID;
48import static org.onosproject.pipelines.basic.BasicConstants.HDR_IN_PORT_ID;
49import static org.onosproject.pipelines.basic.BasicConstants.TBL_TABLE0_ID;
50
51/**
52 * Test for {@link PiFlowRuleTranslatorImpl}.
53 */
54@SuppressWarnings("ConstantConditions")
55public class PiFlowRuleTranslatorImplTest {
56 private static final short IN_PORT_MASK = 0x01ff; // 9-bit mask
57 private static final short ETH_TYPE_MASK = (short) 0xffff;
58 private static final DeviceId DEVICE_ID = DeviceId.deviceId("device:dummy:1");
59
60 private Random random = new Random();
61 private PiPipeconf pipeconf;
62
63 @Before
64 public void setUp() {
65 pipeconf = PipeconfLoader.BASIC_PIPECONF;
66 }
67
68 @Test
69 public void testTranslateFlowRules() throws Exception {
70
71 ApplicationId appId = new DefaultApplicationId(1, "test");
72 int tableId = 0;
73 MacAddress ethDstMac = MacAddress.valueOf(random.nextLong());
74 MacAddress ethSrcMac = MacAddress.valueOf(random.nextLong());
75 short ethType = (short) (0x0000FFFF & random.nextInt());
76 short outPort = (short) random.nextInt(65);
77 short inPort = (short) random.nextInt(65);
78 int timeout = random.nextInt(100);
79 int priority = random.nextInt(100);
80
81 TrafficSelector matchInPort1 = DefaultTrafficSelector
82 .builder()
83 .matchInPort(PortNumber.portNumber(inPort))
84 .matchEthDst(ethDstMac)
85 .matchEthSrc(ethSrcMac)
86 .matchEthType(ethType)
87 .build();
88
89 TrafficSelector emptySelector = DefaultTrafficSelector
90 .builder().build();
91
92 TrafficTreatment outPort2 = DefaultTrafficTreatment
93 .builder()
94 .setOutput(PortNumber.portNumber(outPort))
95 .build();
96
97 FlowRule rule1 = DefaultFlowRule.builder()
98 .forDevice(DEVICE_ID)
99 .forTable(tableId)
100 .fromApp(appId)
101 .withSelector(matchInPort1)
102 .withTreatment(outPort2)
103 .makeTemporary(timeout)
104 .withPriority(priority)
105 .build();
106
107 FlowRule rule2 = DefaultFlowRule.builder()
108 .forDevice(DEVICE_ID)
109 .forTable(tableId)
110 .fromApp(appId)
111 .withSelector(matchInPort1)
112 .withTreatment(outPort2)
113 .makeTemporary(timeout)
114 .withPriority(priority)
115 .build();
116
117 FlowRule defActionRule = DefaultFlowRule.builder()
118 .forDevice(DEVICE_ID)
119 .forTable(tableId)
120 .fromApp(appId)
121 .withSelector(emptySelector)
122 .withTreatment(outPort2)
123 .makeTemporary(timeout)
124 .withPriority(priority)
125 .build();
126
127 PiTableEntry entry1 = PiFlowRuleTranslatorImpl.translate(rule1, pipeconf, null);
128 PiTableEntry entry2 = PiFlowRuleTranslatorImpl.translate(rule2, pipeconf, null);
129 PiTableEntry defActionEntry = PiFlowRuleTranslatorImpl.translate(defActionRule, pipeconf, null);
130
131 // check equality, i.e. same rules must produce same entries
132 new EqualsTester()
133 .addEqualityGroup(rule1, rule2)
134 .addEqualityGroup(entry1, entry2)
135 .testEquals();
136
137 // parse values stored in entry1
138 PiTernaryFieldMatch inPortParam = (PiTernaryFieldMatch) entry1.matchKey().fieldMatch(HDR_IN_PORT_ID).get();
139 PiTernaryFieldMatch ethDstParam = (PiTernaryFieldMatch) entry1.matchKey().fieldMatch(HDR_ETH_DST_ID).get();
140 PiTernaryFieldMatch ethSrcParam = (PiTernaryFieldMatch) entry1.matchKey().fieldMatch(HDR_ETH_SRC_ID).get();
141 PiTernaryFieldMatch ethTypeParam = (PiTernaryFieldMatch) entry1.matchKey().fieldMatch(HDR_ETH_TYPE_ID).get();
142 Optional<Double> expectedTimeout = pipeconf.pipelineModel().table(TBL_TABLE0_ID).get().supportsAging()
143 ? Optional.of((double) rule1.timeout()) : Optional.empty();
144
145 // check that values stored in entry are the same used for the flow rule
146 assertThat("Incorrect inPort match param value",
147 inPortParam.value().asReadOnlyBuffer().getShort(), is(equalTo(inPort)));
148 assertThat("Incorrect inPort match param mask",
149 inPortParam.mask().asReadOnlyBuffer().getShort(), is(equalTo(IN_PORT_MASK)));
150 assertThat("Incorrect ethDestMac match param value",
151 ethDstParam.value().asArray(), is(equalTo(ethDstMac.toBytes())));
152 assertThat("Incorrect ethDestMac match param mask",
153 ethDstParam.mask().asArray(), is(equalTo(MacAddress.BROADCAST.toBytes())));
154 assertThat("Incorrect ethSrcMac match param value",
155 ethSrcParam.value().asArray(), is(equalTo(ethSrcMac.toBytes())));
156 assertThat("Incorrect ethSrcMac match param mask",
157 ethSrcParam.mask().asArray(), is(equalTo(MacAddress.BROADCAST.toBytes())));
158 assertThat("Incorrect ethType match param value",
159 ethTypeParam.value().asReadOnlyBuffer().getShort(), is(equalTo(ethType)));
160 assertThat("Incorrect ethType match param mask",
161 ethTypeParam.mask().asReadOnlyBuffer().getShort(), is(equalTo(ETH_TYPE_MASK)));
162 // FIXME: re-enable when P4Runtime priority handling will be moved out of transltion service
163 // see PiFlowRuleTranslatorImpl
164 // assertThat("Incorrect priority value",
165 // entry1.priority().get(), is(equalTo(MAX_PI_PRIORITY - rule1.priority())));
166 assertThat("Incorrect timeout value",
167 entry1.timeout(), is(equalTo(expectedTimeout)));
168 assertThat("Match key should be empty",
169 defActionEntry.matchKey(), is(equalTo(PiMatchKey.EMPTY)));
170 assertThat("Priority should not be set", !defActionEntry.priority().isPresent());
171 }
172}