Yi Tseng | 82512da | 2017-08-16 19:46:36 -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.p4runtime.ctl; |
| 18 | |
| 19 | import com.google.common.collect.ImmutableList; |
| 20 | import com.google.common.collect.ImmutableSet; |
| 21 | import com.google.common.collect.Lists; |
| 22 | import com.google.protobuf.ByteString; |
| 23 | import io.grpc.ManagedChannel; |
| 24 | import io.grpc.Server; |
| 25 | import io.grpc.inprocess.InProcessChannelBuilder; |
| 26 | import io.grpc.inprocess.InProcessServerBuilder; |
| 27 | import io.grpc.internal.AbstractServerImplBuilder; |
| 28 | import org.easymock.EasyMock; |
| 29 | import org.junit.AfterClass; |
| 30 | import org.junit.Before; |
| 31 | import org.junit.BeforeClass; |
| 32 | import org.junit.Test; |
| 33 | import org.onlab.util.ImmutableByteSequence; |
| 34 | import org.onosproject.net.DeviceId; |
| 35 | import org.onosproject.net.pi.model.DefaultPiPipeconf; |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame^] | 36 | import org.onosproject.net.pi.model.PiActionId; |
| 37 | import org.onosproject.net.pi.model.PiActionParamId; |
| 38 | import org.onosproject.net.pi.model.PiActionProfileId; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 39 | import org.onosproject.net.pi.model.PiPipeconf; |
| 40 | import org.onosproject.net.pi.model.PiPipeconfId; |
| 41 | import org.onosproject.net.pi.model.PiPipelineModel; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 42 | import org.onosproject.net.pi.runtime.PiAction; |
| 43 | import org.onosproject.net.pi.runtime.PiActionGroup; |
| 44 | import org.onosproject.net.pi.runtime.PiActionGroupId; |
| 45 | import org.onosproject.net.pi.runtime.PiActionGroupMember; |
| 46 | import org.onosproject.net.pi.runtime.PiActionGroupMemberId; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 47 | import org.onosproject.net.pi.runtime.PiActionParam; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 48 | import p4.P4RuntimeOuterClass.ActionProfileGroup; |
| 49 | import p4.P4RuntimeOuterClass.ActionProfileMember; |
| 50 | import p4.P4RuntimeOuterClass.Entity; |
Yi Tseng | 3e7f145 | 2017-10-20 10:31:53 -0700 | [diff] [blame] | 51 | import p4.P4RuntimeOuterClass.Uint128; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 52 | import p4.P4RuntimeOuterClass.Update; |
| 53 | import p4.P4RuntimeOuterClass.WriteRequest; |
| 54 | |
| 55 | import java.io.IOException; |
| 56 | import java.net.URL; |
| 57 | import java.util.Collection; |
| 58 | import java.util.List; |
| 59 | import java.util.concurrent.CompletableFuture; |
| 60 | import java.util.concurrent.TimeUnit; |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 61 | import java.util.stream.Collectors; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 62 | |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 63 | import static org.easymock.EasyMock.niceMock; |
Carmelo Cascone | ca94bcf | 2017-10-27 14:16:59 -0700 | [diff] [blame] | 64 | import static org.junit.Assert.assertEquals; |
| 65 | import static org.junit.Assert.assertNotNull; |
| 66 | import static org.junit.Assert.assertTrue; |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame^] | 67 | import static org.onosproject.net.pi.model.PiActionGroupType.SELECT; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 68 | import static org.onosproject.net.pi.model.PiPipeconf.ExtensionType.P4_INFO_TEXT; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 69 | import static org.onosproject.p4runtime.api.P4RuntimeClient.WriteOperationType.INSERT; |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 70 | import static p4.P4RuntimeOuterClass.Action; |
| 71 | import static p4.P4RuntimeOuterClass.ReadResponse; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 72 | |
| 73 | /** |
| 74 | * Tests for P4 Runtime Action Profile Group support. |
| 75 | */ |
| 76 | public class P4RuntimeGroupTest { |
| 77 | private static final String PIPECONF_ID = "p4runtime-mock-pipeconf"; |
Carmelo Cascone | ca94bcf | 2017-10-27 14:16:59 -0700 | [diff] [blame] | 78 | private static final String P4INFO_PATH = "/test.p4info"; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 79 | private static final PiPipeconf PIPECONF = buildPipeconf(); |
| 80 | private static final int P4_INFO_ACT_PROF_ID = 285227860; |
| 81 | private static final PiActionProfileId ACT_PROF_ID = PiActionProfileId.of("ecmp_selector"); |
| 82 | private static final PiActionGroupId GROUP_ID = PiActionGroupId.of(1); |
| 83 | private static final int DEFAULT_MEMBER_WEIGHT = 1; |
| 84 | private static final PiActionId EGRESS_PORT_ACTION_ID = PiActionId.of("set_egress_port"); |
| 85 | private static final PiActionParamId PORT_PARAM_ID = PiActionParamId.of("port"); |
| 86 | private static final int BASE_MEM_ID = 65535; |
| 87 | private static final List<Integer> MEMBER_IDS = ImmutableList.of(65536, 65537, 65538); |
| 88 | private static final Collection<PiActionGroupMember> GROUP_MEMBERS = |
| 89 | ImmutableSet.of( |
| 90 | outputMember((short) 1), |
| 91 | outputMember((short) 2), |
| 92 | outputMember((short) 3) |
| 93 | ); |
| 94 | private static final PiActionGroup GROUP = PiActionGroup.builder() |
| 95 | .withId(GROUP_ID) |
| 96 | .addMembers(GROUP_MEMBERS) |
| 97 | .withActionProfileId(ACT_PROF_ID) |
| 98 | .withType(SELECT) |
| 99 | .build(); |
| 100 | private static final DeviceId DEVICE_ID = DeviceId.deviceId("device:p4runtime:1"); |
| 101 | private static final int P4_DEVICE_ID = 1; |
| 102 | private static final int SET_EGRESS_PORT_ID = 16794308; |
| 103 | private static final String GRPC_SERVER_NAME = "P4RuntimeGroupTest"; |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 104 | private static final long DEFAULT_TIMEOUT_TIME = 10; |
Yi Tseng | 3e7f145 | 2017-10-20 10:31:53 -0700 | [diff] [blame] | 105 | private static final Uint128 DEFAULT_ELECTION_ID = Uint128.newBuilder().setLow(1).build(); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 106 | |
| 107 | private P4RuntimeClientImpl client; |
| 108 | private P4RuntimeControllerImpl controller; |
| 109 | private static MockP4RuntimeServer p4RuntimeServerImpl = new MockP4RuntimeServer(); |
| 110 | private static Server grpcServer; |
| 111 | private static ManagedChannel grpcChannel; |
| 112 | |
| 113 | private static PiActionGroupMember outputMember(short portNum) { |
| 114 | PiActionParam param = new PiActionParam(PORT_PARAM_ID, |
| 115 | ImmutableByteSequence.copyFrom(portNum)); |
| 116 | PiAction piAction = PiAction.builder() |
| 117 | .withId(EGRESS_PORT_ACTION_ID) |
| 118 | .withParameter(param).build(); |
| 119 | |
| 120 | return PiActionGroupMember.builder() |
| 121 | .withAction(piAction) |
| 122 | .withId(PiActionGroupMemberId.of(BASE_MEM_ID + portNum)) |
| 123 | .withWeight(DEFAULT_MEMBER_WEIGHT) |
| 124 | .build(); |
| 125 | } |
| 126 | |
| 127 | private static PiPipeconf buildPipeconf() { |
| 128 | final URL p4InfoUrl = P4RuntimeGroupTest.class.getResource(P4INFO_PATH); |
| 129 | return DefaultPiPipeconf.builder() |
| 130 | .withId(new PiPipeconfId(PIPECONF_ID)) |
| 131 | .withPipelineModel(EasyMock.niceMock(PiPipelineModel.class)) |
| 132 | .addExtension(P4_INFO_TEXT, p4InfoUrl) |
| 133 | .build(); |
| 134 | } |
| 135 | |
| 136 | @BeforeClass |
| 137 | public static void globalSetup() throws IOException { |
| 138 | AbstractServerImplBuilder builder = InProcessServerBuilder |
| 139 | .forName(GRPC_SERVER_NAME).directExecutor(); |
| 140 | builder.addService(p4RuntimeServerImpl); |
| 141 | grpcServer = builder.build().start(); |
| 142 | grpcChannel = InProcessChannelBuilder.forName(GRPC_SERVER_NAME) |
| 143 | .directExecutor() |
| 144 | .usePlaintext(true) |
| 145 | .build(); |
| 146 | } |
| 147 | |
| 148 | @AfterClass |
| 149 | public static void globalTeerDown() { |
| 150 | grpcServer.shutdown(); |
| 151 | grpcChannel.shutdown(); |
| 152 | } |
| 153 | |
| 154 | |
| 155 | @Before |
| 156 | public void setup() { |
| 157 | controller = niceMock(P4RuntimeControllerImpl.class); |
| 158 | client = new P4RuntimeClientImpl(DEVICE_ID, P4_DEVICE_ID, |
| 159 | grpcChannel, |
| 160 | controller); |
Yi Tseng | 3e7f145 | 2017-10-20 10:31:53 -0700 | [diff] [blame] | 161 | client.p4RuntimeElectionId = DEFAULT_ELECTION_ID; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | @Test |
| 165 | public void testInsertPiActionGroup() throws Exception { |
| 166 | CompletableFuture<Void> complete = p4RuntimeServerImpl.expectRequests(1); |
| 167 | client.writeActionGroup(GROUP, INSERT, PIPECONF); |
| 168 | complete.get(DEFAULT_TIMEOUT_TIME, TimeUnit.SECONDS); |
| 169 | WriteRequest result = p4RuntimeServerImpl.getWriteReqs().get(0); |
| 170 | assertEquals(1, result.getDeviceId()); |
| 171 | assertEquals(1, result.getUpdatesCount()); |
Yi Tseng | 3e7f145 | 2017-10-20 10:31:53 -0700 | [diff] [blame] | 172 | assertEquals(DEFAULT_ELECTION_ID, result.getElectionId()); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 173 | |
| 174 | Update update = result.getUpdatesList().get(0); |
| 175 | assertEquals(Update.Type.INSERT, update.getType()); |
| 176 | |
| 177 | Entity entity = update.getEntity(); |
| 178 | ActionProfileGroup actionProfileGroup = entity.getActionProfileGroup(); |
| 179 | assertNotNull(actionProfileGroup); |
| 180 | |
| 181 | assertEquals(P4_INFO_ACT_PROF_ID, actionProfileGroup.getActionProfileId()); |
| 182 | assertEquals(3, actionProfileGroup.getMembersCount()); |
| 183 | List<ActionProfileGroup.Member> members = actionProfileGroup.getMembersList(); |
| 184 | |
| 185 | for (ActionProfileGroup.Member member : members) { |
| 186 | // XXX: We can't guarantee the order of member, just make sure we |
| 187 | // have these member ids |
| 188 | assertTrue(MEMBER_IDS.contains(member.getMemberId())); |
| 189 | assertEquals(DEFAULT_MEMBER_WEIGHT, member.getWeight()); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | @Test |
| 194 | public void testInsertPiActionMembers() throws Exception { |
| 195 | CompletableFuture<Void> complete = p4RuntimeServerImpl.expectRequests(1); |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 196 | client.writeActionGroupMembers(GROUP, INSERT, PIPECONF); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 197 | complete.get(DEFAULT_TIMEOUT_TIME, TimeUnit.SECONDS); |
| 198 | WriteRequest result = p4RuntimeServerImpl.getWriteReqs().get(0); |
| 199 | assertEquals(1, result.getDeviceId()); |
| 200 | assertEquals(3, result.getUpdatesCount()); |
Yi Tseng | 3e7f145 | 2017-10-20 10:31:53 -0700 | [diff] [blame] | 201 | assertEquals(DEFAULT_ELECTION_ID, result.getElectionId()); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 202 | |
| 203 | List<Update> updates = result.getUpdatesList(); |
| 204 | for (Update update : updates) { |
| 205 | assertEquals(Update.Type.INSERT, update.getType()); |
| 206 | Entity entity = update.getEntity(); |
| 207 | ActionProfileMember member = entity.getActionProfileMember(); |
| 208 | assertNotNull(member); |
| 209 | assertEquals(P4_INFO_ACT_PROF_ID, member.getActionProfileId()); |
| 210 | assertTrue(MEMBER_IDS.contains(member.getMemberId())); |
| 211 | Action action = member.getAction(); |
| 212 | assertEquals(SET_EGRESS_PORT_ID, action.getActionId()); |
| 213 | assertEquals(1, action.getParamsCount()); |
| 214 | Action.Param param = action.getParamsList().get(0); |
| 215 | assertEquals(1, param.getParamId()); |
| 216 | byte outPort = (byte) (member.getMemberId() - BASE_MEM_ID); |
| 217 | ByteString bs = ByteString.copyFrom(new byte[]{0, outPort}); |
| 218 | assertEquals(bs, param.getValue()); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | @Test |
| 223 | public void testReadGroups() throws Exception { |
| 224 | ActionProfileGroup.Builder group = ActionProfileGroup.newBuilder() |
| 225 | .setGroupId(GROUP_ID.id()) |
| 226 | .setType(ActionProfileGroup.Type.SELECT) |
| 227 | .setActionProfileId(P4_INFO_ACT_PROF_ID); |
| 228 | |
| 229 | List<ActionProfileMember> members = Lists.newArrayList(); |
| 230 | |
| 231 | MEMBER_IDS.forEach(id -> { |
| 232 | ActionProfileGroup.Member member = ActionProfileGroup.Member.newBuilder() |
| 233 | .setMemberId(id) |
| 234 | .setWeight(DEFAULT_MEMBER_WEIGHT) |
| 235 | .build(); |
| 236 | group.addMembers(member); |
| 237 | |
| 238 | byte outPort = (byte) (id - BASE_MEM_ID); |
| 239 | ByteString bs = ByteString.copyFrom(new byte[]{0, outPort}); |
| 240 | Action.Param param = Action.Param.newBuilder() |
| 241 | .setParamId(1) |
| 242 | .setValue(bs) |
| 243 | .build(); |
| 244 | |
| 245 | Action action = Action.newBuilder() |
| 246 | .setActionId(SET_EGRESS_PORT_ID) |
| 247 | .addParams(param) |
| 248 | .build(); |
| 249 | |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 250 | ActionProfileMember actProfMember = |
| 251 | ActionProfileMember.newBuilder() |
| 252 | .setMemberId(id) |
| 253 | .setAction(action) |
| 254 | .build(); |
| 255 | members.add(actProfMember); |
| 256 | }); |
| 257 | |
| 258 | List<ReadResponse> responses = Lists.newArrayList(); |
| 259 | responses.add(ReadResponse.newBuilder() |
| 260 | .addEntities(Entity.newBuilder().setActionProfileGroup(group)) |
| 261 | .build() |
| 262 | ); |
| 263 | |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 264 | responses.add(ReadResponse.newBuilder() |
| 265 | .addAllEntities(members.stream() |
| 266 | .map(m -> Entity.newBuilder().setActionProfileMember(m).build()) |
| 267 | .collect(Collectors.toList())) |
| 268 | .build()); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 269 | |
| 270 | p4RuntimeServerImpl.willReturnReadResult(responses); |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 271 | CompletableFuture<Void> complete = p4RuntimeServerImpl.expectRequests(2); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 272 | CompletableFuture<Collection<PiActionGroup>> groupsComplete = client.dumpGroups(ACT_PROF_ID, PIPECONF); |
| 273 | complete.get(DEFAULT_TIMEOUT_TIME, TimeUnit.SECONDS); |
| 274 | |
| 275 | Collection<PiActionGroup> groups = groupsComplete.get(DEFAULT_TIMEOUT_TIME, TimeUnit.SECONDS); |
| 276 | assertEquals(1, groups.size()); |
| 277 | PiActionGroup piActionGroup = groups.iterator().next(); |
| 278 | assertEquals(ACT_PROF_ID, piActionGroup.actionProfileId()); |
| 279 | assertEquals(GROUP_ID, piActionGroup.id()); |
| 280 | assertEquals(SELECT, piActionGroup.type()); |
| 281 | assertEquals(3, piActionGroup.members().size()); |
| 282 | assertTrue(GROUP_MEMBERS.containsAll(piActionGroup.members())); |
| 283 | assertTrue(piActionGroup.members().containsAll(GROUP_MEMBERS)); |
| 284 | } |
| 285 | } |