Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -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 | f7aa3f9 | 2017-07-06 23:56:50 -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 | |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 19 | import com.google.common.collect.HashMultimap; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 20 | import com.google.common.collect.ImmutableMap; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 21 | import com.google.common.collect.Lists; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 22 | import com.google.common.collect.Multimap; |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 23 | import com.google.common.collect.Sets; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 24 | import com.google.protobuf.ByteString; |
Yi Tseng | e67e141 | 2018-01-31 17:35:20 -0800 | [diff] [blame] | 25 | import com.google.protobuf.InvalidProtocolBufferException; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 26 | import io.grpc.ManagedChannel; |
Carmelo Cascone | 943c664 | 2018-09-11 13:01:03 -0700 | [diff] [blame] | 27 | import io.grpc.Metadata; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 28 | import io.grpc.Status; |
| 29 | import io.grpc.StatusRuntimeException; |
Carmelo Cascone | 943c664 | 2018-09-11 13:01:03 -0700 | [diff] [blame] | 30 | import io.grpc.protobuf.lite.ProtoLiteUtils; |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 31 | import io.grpc.stub.ClientCallStreamObserver; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 32 | import io.grpc.stub.StreamObserver; |
Andrea Campanella | 288b273 | 2017-07-28 14:16:16 +0200 | [diff] [blame] | 33 | import org.onlab.osgi.DefaultServiceDirectory; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 34 | import org.onlab.util.Tools; |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 35 | import org.onosproject.grpc.ctl.AbstractGrpcClient; |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 36 | import org.onosproject.net.pi.model.PiActionProfileId; |
| 37 | import org.onosproject.net.pi.model.PiCounterId; |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 38 | import org.onosproject.net.pi.model.PiMeterId; |
Andrea Campanella | 432f718 | 2017-07-14 18:43:27 +0200 | [diff] [blame] | 39 | import org.onosproject.net.pi.model.PiPipeconf; |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 40 | import org.onosproject.net.pi.model.PiTableId; |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 41 | import org.onosproject.net.pi.runtime.PiActionGroup; |
| 42 | import org.onosproject.net.pi.runtime.PiActionGroupMember; |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 43 | import org.onosproject.net.pi.runtime.PiActionGroupMemberId; |
steven30801 | 7632e15 | 2018-10-20 00:51:08 +0800 | [diff] [blame] | 44 | import org.onosproject.net.pi.runtime.PiCounterCell; |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 45 | import org.onosproject.net.pi.runtime.PiCounterCellId; |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 46 | import org.onosproject.net.pi.runtime.PiMeterCellConfig; |
| 47 | import org.onosproject.net.pi.runtime.PiMeterCellId; |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 48 | import org.onosproject.net.pi.runtime.PiMulticastGroupEntry; |
Andrea Campanella | 432f718 | 2017-07-14 18:43:27 +0200 | [diff] [blame] | 49 | import org.onosproject.net.pi.runtime.PiPacketOperation; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 50 | import org.onosproject.net.pi.runtime.PiTableEntry; |
Yi Tseng | e67e141 | 2018-01-31 17:35:20 -0800 | [diff] [blame] | 51 | import org.onosproject.net.pi.service.PiPipeconfService; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 52 | import org.onosproject.p4runtime.api.P4RuntimeClient; |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 53 | import org.onosproject.p4runtime.api.P4RuntimeClientKey; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 54 | import org.onosproject.p4runtime.api.P4RuntimeEvent; |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 55 | import p4.config.v1.P4InfoOuterClass.P4Info; |
| 56 | import p4.tmp.P4Config; |
Carmelo Cascone | 6af4e17 | 2018-06-15 16:01:30 +0200 | [diff] [blame] | 57 | import p4.v1.P4RuntimeGrpc; |
| 58 | import p4.v1.P4RuntimeOuterClass; |
| 59 | import p4.v1.P4RuntimeOuterClass.ActionProfileGroup; |
| 60 | import p4.v1.P4RuntimeOuterClass.ActionProfileMember; |
| 61 | import p4.v1.P4RuntimeOuterClass.Entity; |
| 62 | import p4.v1.P4RuntimeOuterClass.ForwardingPipelineConfig; |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 63 | import p4.v1.P4RuntimeOuterClass.GetForwardingPipelineConfigRequest; |
| 64 | import p4.v1.P4RuntimeOuterClass.GetForwardingPipelineConfigResponse; |
Carmelo Cascone | 6af4e17 | 2018-06-15 16:01:30 +0200 | [diff] [blame] | 65 | import p4.v1.P4RuntimeOuterClass.MasterArbitrationUpdate; |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 66 | import p4.v1.P4RuntimeOuterClass.MulticastGroupEntry; |
| 67 | import p4.v1.P4RuntimeOuterClass.PacketReplicationEngineEntry; |
Carmelo Cascone | 6af4e17 | 2018-06-15 16:01:30 +0200 | [diff] [blame] | 68 | import p4.v1.P4RuntimeOuterClass.ReadRequest; |
| 69 | import p4.v1.P4RuntimeOuterClass.ReadResponse; |
| 70 | import p4.v1.P4RuntimeOuterClass.SetForwardingPipelineConfigRequest; |
| 71 | import p4.v1.P4RuntimeOuterClass.StreamMessageRequest; |
| 72 | import p4.v1.P4RuntimeOuterClass.StreamMessageResponse; |
| 73 | import p4.v1.P4RuntimeOuterClass.TableEntry; |
| 74 | import p4.v1.P4RuntimeOuterClass.Uint128; |
| 75 | import p4.v1.P4RuntimeOuterClass.Update; |
| 76 | import p4.v1.P4RuntimeOuterClass.WriteRequest; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 77 | |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 78 | import java.math.BigInteger; |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 79 | import java.net.ConnectException; |
Carmelo Cascone | d61fdb3 | 2017-10-30 10:09:57 -0700 | [diff] [blame] | 80 | import java.nio.ByteBuffer; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 81 | import java.util.Collections; |
| 82 | import java.util.Iterator; |
| 83 | import java.util.List; |
| 84 | import java.util.Map; |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 85 | import java.util.Objects; |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 86 | import java.util.Set; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 87 | import java.util.concurrent.CompletableFuture; |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 88 | import java.util.concurrent.atomic.AtomicBoolean; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 89 | import java.util.stream.Collectors; |
| 90 | import java.util.stream.StreamSupport; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 91 | |
Carmelo Cascone | d61fdb3 | 2017-10-30 10:09:57 -0700 | [diff] [blame] | 92 | import static com.google.common.base.Preconditions.checkNotNull; |
Carmelo Cascone | 5bc7e10 | 2018-02-18 18:27:55 -0800 | [diff] [blame] | 93 | import static java.lang.String.format; |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 94 | import static java.util.Collections.singletonList; |
Carmelo Cascone | 6af4e17 | 2018-06-15 16:01:30 +0200 | [diff] [blame] | 95 | import static p4.v1.P4RuntimeOuterClass.Entity.EntityCase.ACTION_PROFILE_GROUP; |
| 96 | import static p4.v1.P4RuntimeOuterClass.Entity.EntityCase.ACTION_PROFILE_MEMBER; |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 97 | import static p4.v1.P4RuntimeOuterClass.Entity.EntityCase.PACKET_REPLICATION_ENGINE_ENTRY; |
Carmelo Cascone | 6af4e17 | 2018-06-15 16:01:30 +0200 | [diff] [blame] | 98 | import static p4.v1.P4RuntimeOuterClass.Entity.EntityCase.TABLE_ENTRY; |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 99 | import static p4.v1.P4RuntimeOuterClass.PacketIn; |
Carmelo Cascone | 6af4e17 | 2018-06-15 16:01:30 +0200 | [diff] [blame] | 100 | import static p4.v1.P4RuntimeOuterClass.PacketOut; |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 101 | import static p4.v1.P4RuntimeOuterClass.PacketReplicationEngineEntry.TypeCase.MULTICAST_GROUP_ENTRY; |
Carmelo Cascone | 6af4e17 | 2018-06-15 16:01:30 +0200 | [diff] [blame] | 102 | import static p4.v1.P4RuntimeOuterClass.SetForwardingPipelineConfigRequest.Action.VERIFY_AND_COMMIT; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 103 | |
| 104 | /** |
| 105 | * Implementation of a P4Runtime client. |
| 106 | */ |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 107 | final class P4RuntimeClientImpl extends AbstractGrpcClient implements P4RuntimeClient { |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 108 | |
Carmelo Cascone | 943c664 | 2018-09-11 13:01:03 -0700 | [diff] [blame] | 109 | private static final Metadata.Key<com.google.rpc.Status> STATUS_DETAILS_KEY = |
| 110 | Metadata.Key.of("grpc-status-details-bin", |
| 111 | ProtoLiteUtils.metadataMarshaller( |
| 112 | com.google.rpc.Status.getDefaultInstance())); |
| 113 | |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 114 | private static final Map<WriteOperationType, Update.Type> UPDATE_TYPES = ImmutableMap.of( |
| 115 | WriteOperationType.UNSPECIFIED, Update.Type.UNSPECIFIED, |
| 116 | WriteOperationType.INSERT, Update.Type.INSERT, |
| 117 | WriteOperationType.MODIFY, Update.Type.MODIFY, |
| 118 | WriteOperationType.DELETE, Update.Type.DELETE |
| 119 | ); |
| 120 | |
Carmelo Cascone | f423bec | 2017-08-30 01:56:25 +0200 | [diff] [blame] | 121 | private final long p4DeviceId; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 122 | private final P4RuntimeControllerImpl controller; |
| 123 | private final P4RuntimeGrpc.P4RuntimeBlockingStub blockingStub; |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 124 | private StreamChannelManager streamChannelManager; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 125 | |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 126 | // Used by this client for write requests. |
| 127 | private Uint128 clientElectionId = Uint128.newBuilder().setLow(1).build(); |
Yi Tseng | 3e7f145 | 2017-10-20 10:31:53 -0700 | [diff] [blame] | 128 | |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 129 | private final AtomicBoolean isClientMaster = new AtomicBoolean(false); |
| 130 | |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 131 | /** |
| 132 | * Default constructor. |
| 133 | * |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 134 | * @param clientKey the client key of this client |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 135 | * @param channel gRPC channel |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 136 | * @param controller runtime client controller |
| 137 | */ |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 138 | P4RuntimeClientImpl(P4RuntimeClientKey clientKey, ManagedChannel channel, |
Carmelo Cascone | f423bec | 2017-08-30 01:56:25 +0200 | [diff] [blame] | 139 | P4RuntimeControllerImpl controller) { |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 140 | |
| 141 | super(clientKey, channel); |
| 142 | this.p4DeviceId = clientKey.p4DeviceId(); |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 143 | this.controller = controller; |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 144 | |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 145 | //TODO Investigate use of stub deadlines instead of timeout in supplyInContext |
Carmelo Cascone | 2cad9ef | 2017-08-01 21:52:07 +0200 | [diff] [blame] | 146 | this.blockingStub = P4RuntimeGrpc.newBlockingStub(channel); |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 147 | this.streamChannelManager = new StreamChannelManager(channel); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 148 | } |
| 149 | |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 150 | @Override |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 151 | public CompletableFuture<Boolean> startStreamChannel() { |
Carmelo Cascone | 85d72ef | 2018-08-23 19:06:29 -0700 | [diff] [blame] | 152 | return supplyInContext(() -> sendMasterArbitrationUpdate(false), |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 153 | "start-initStreamChannel"); |
| 154 | } |
| 155 | |
| 156 | @Override |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 157 | public CompletableFuture<Boolean> becomeMaster() { |
Carmelo Cascone | 85d72ef | 2018-08-23 19:06:29 -0700 | [diff] [blame] | 158 | return supplyInContext(() -> sendMasterArbitrationUpdate(true), |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 159 | "becomeMaster"); |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 160 | } |
| 161 | |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 162 | @Override |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 163 | public boolean isMaster() { |
| 164 | return streamChannelManager.isOpen() && isClientMaster.get(); |
| 165 | } |
| 166 | |
| 167 | @Override |
| 168 | public boolean isStreamChannelOpen() { |
| 169 | return streamChannelManager.isOpen(); |
| 170 | } |
| 171 | |
| 172 | @Override |
Carmelo Cascone | d61fdb3 | 2017-10-30 10:09:57 -0700 | [diff] [blame] | 173 | public CompletableFuture<Boolean> setPipelineConfig(PiPipeconf pipeconf, ByteBuffer deviceData) { |
| 174 | return supplyInContext(() -> doSetPipelineConfig(pipeconf, deviceData), "setPipelineConfig"); |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | @Override |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 178 | public boolean isPipelineConfigSet(PiPipeconf pipeconf, ByteBuffer deviceData) { |
| 179 | return doIsPipelineConfigSet(pipeconf, deviceData); |
| 180 | } |
| 181 | |
| 182 | @Override |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 183 | public CompletableFuture<Boolean> writeTableEntries(List<PiTableEntry> piTableEntries, |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 184 | WriteOperationType opType, PiPipeconf pipeconf) { |
Carmelo Cascone | 2cad9ef | 2017-08-01 21:52:07 +0200 | [diff] [blame] | 185 | return supplyInContext(() -> doWriteTableEntries(piTableEntries, opType, pipeconf), |
| 186 | "writeTableEntries-" + opType.name()); |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | @Override |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 190 | public CompletableFuture<List<PiTableEntry>> dumpTables( |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 191 | Set<PiTableId> piTableIds, boolean defaultEntries, PiPipeconf pipeconf) { |
| 192 | return supplyInContext(() -> doDumpTables(piTableIds, defaultEntries, pipeconf), |
| 193 | "dumpTables-" + piTableIds.hashCode()); |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | @Override |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 197 | public CompletableFuture<List<PiTableEntry>> dumpAllTables(PiPipeconf pipeconf) { |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 198 | return supplyInContext(() -> doDumpTables(null, false, pipeconf), "dumpAllTables"); |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | @Override |
Andrea Campanella | 432f718 | 2017-07-14 18:43:27 +0200 | [diff] [blame] | 202 | public CompletableFuture<Boolean> packetOut(PiPacketOperation packet, PiPipeconf pipeconf) { |
Carmelo Cascone | 2cad9ef | 2017-08-01 21:52:07 +0200 | [diff] [blame] | 203 | return supplyInContext(() -> doPacketOut(packet, pipeconf), "packetOut"); |
Andrea Campanella | 432f718 | 2017-07-14 18:43:27 +0200 | [diff] [blame] | 204 | } |
| 205 | |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 206 | @Override |
steven30801 | 7632e15 | 2018-10-20 00:51:08 +0800 | [diff] [blame] | 207 | public CompletableFuture<List<PiCounterCell>> readCounterCells(Set<PiCounterCellId> cellIds, |
| 208 | PiPipeconf pipeconf) { |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 209 | return supplyInContext(() -> doReadCounterCells(Lists.newArrayList(cellIds), pipeconf), |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 210 | "readCounterCells-" + cellIds.hashCode()); |
| 211 | } |
| 212 | |
| 213 | @Override |
steven30801 | 7632e15 | 2018-10-20 00:51:08 +0800 | [diff] [blame] | 214 | public CompletableFuture<List<PiCounterCell>> readAllCounterCells(Set<PiCounterId> counterIds, |
| 215 | PiPipeconf pipeconf) { |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 216 | return supplyInContext(() -> doReadAllCounterCells(Lists.newArrayList(counterIds), pipeconf), |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 217 | "readAllCounterCells-" + counterIds.hashCode()); |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 218 | } |
| 219 | |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 220 | @Override |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 221 | public CompletableFuture<Boolean> writeActionGroupMembers(List<PiActionGroupMember> members, |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 222 | WriteOperationType opType, |
| 223 | PiPipeconf pipeconf) { |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 224 | return supplyInContext(() -> doWriteActionGroupMembers(members, opType, pipeconf), |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 225 | "writeActionGroupMembers-" + opType.name()); |
| 226 | } |
| 227 | |
Yi Tseng | 8d35513 | 2018-04-13 01:40:48 +0800 | [diff] [blame] | 228 | |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 229 | @Override |
| 230 | public CompletableFuture<Boolean> writeActionGroup(PiActionGroup group, |
| 231 | WriteOperationType opType, |
ghj0504520 | ed7340c | 2018-10-26 13:06:35 -0700 | [diff] [blame] | 232 | PiPipeconf pipeconf, |
| 233 | int maxMemberSize) { |
| 234 | return supplyInContext(() -> doWriteActionGroup(group, opType, pipeconf, maxMemberSize), |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 235 | "writeActionGroup-" + opType.name()); |
| 236 | } |
| 237 | |
| 238 | @Override |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 239 | public CompletableFuture<List<PiActionGroup>> dumpGroups(PiActionProfileId actionProfileId, |
| 240 | PiPipeconf pipeconf) { |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 241 | return supplyInContext(() -> doDumpGroups(actionProfileId, pipeconf), |
| 242 | "dumpGroups-" + actionProfileId.id()); |
| 243 | } |
| 244 | |
Yi Tseng | 3e7f145 | 2017-10-20 10:31:53 -0700 | [diff] [blame] | 245 | @Override |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 246 | public CompletableFuture<List<PiActionGroupMemberId>> dumpActionProfileMemberIds( |
| 247 | PiActionProfileId actionProfileId, PiPipeconf pipeconf) { |
| 248 | return supplyInContext(() -> doDumpActionProfileMemberIds(actionProfileId, pipeconf), |
| 249 | "dumpActionProfileMemberIds-" + actionProfileId.id()); |
| 250 | } |
| 251 | |
| 252 | @Override |
| 253 | public CompletableFuture<List<PiActionGroupMemberId>> removeActionProfileMembers( |
| 254 | PiActionProfileId actionProfileId, |
| 255 | List<PiActionGroupMemberId> memberIds, |
| 256 | PiPipeconf pipeconf) { |
| 257 | return supplyInContext( |
| 258 | () -> doRemoveActionProfileMembers(actionProfileId, memberIds, pipeconf), |
| 259 | "cleanupActionProfileMembers-" + actionProfileId.id()); |
| 260 | } |
| 261 | |
| 262 | @Override |
| 263 | public CompletableFuture<Boolean> writeMeterCells(List<PiMeterCellConfig> cellIds, PiPipeconf pipeconf) { |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 264 | |
| 265 | return supplyInContext(() -> doWriteMeterCells(cellIds, pipeconf), |
| 266 | "writeMeterCells"); |
| 267 | } |
| 268 | |
| 269 | @Override |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 270 | public CompletableFuture<Boolean> writePreMulticastGroupEntries( |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 271 | List<PiMulticastGroupEntry> entries, |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 272 | WriteOperationType opType) { |
| 273 | return supplyInContext(() -> doWriteMulticastGroupEntries(entries, opType), |
| 274 | "writePreMulticastGroupEntries"); |
| 275 | } |
| 276 | |
| 277 | @Override |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 278 | public CompletableFuture<List<PiMulticastGroupEntry>> readAllMulticastGroupEntries() { |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 279 | return supplyInContext(this::doReadAllMulticastGroupEntries, |
| 280 | "readAllMulticastGroupEntries"); |
| 281 | } |
| 282 | |
| 283 | @Override |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 284 | public CompletableFuture<List<PiMeterCellConfig>> readMeterCells(Set<PiMeterCellId> cellIds, |
| 285 | PiPipeconf pipeconf) { |
| 286 | return supplyInContext(() -> doReadMeterCells(Lists.newArrayList(cellIds), pipeconf), |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 287 | "readMeterCells-" + cellIds.hashCode()); |
| 288 | } |
| 289 | |
| 290 | @Override |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 291 | public CompletableFuture<List<PiMeterCellConfig>> readAllMeterCells(Set<PiMeterId> meterIds, |
| 292 | PiPipeconf pipeconf) { |
| 293 | return supplyInContext(() -> doReadAllMeterCells(Lists.newArrayList(meterIds), pipeconf), |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 294 | "readAllMeterCells-" + meterIds.hashCode()); |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 295 | } |
Yi Tseng | 3e7f145 | 2017-10-20 10:31:53 -0700 | [diff] [blame] | 296 | |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 297 | /* Blocking method implementations below */ |
| 298 | |
Carmelo Cascone | 85d72ef | 2018-08-23 19:06:29 -0700 | [diff] [blame] | 299 | private boolean sendMasterArbitrationUpdate(boolean asMaster) { |
| 300 | BigInteger newId = controller.newMasterElectionId(deviceId); |
| 301 | if (asMaster) { |
| 302 | // Becoming master is a race. Here we increase our chances of win |
| 303 | // against other ONOS nodes in the cluster that are calling start() |
| 304 | // (which is used to start the stream RPC session, not to become |
| 305 | // master). |
| 306 | newId = newId.add(BigInteger.valueOf(1000)); |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 307 | } |
Carmelo Cascone | 85d72ef | 2018-08-23 19:06:29 -0700 | [diff] [blame] | 308 | final Uint128 idMsg = bigIntegerToUint128( |
| 309 | controller.newMasterElectionId(deviceId)); |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame] | 310 | |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 311 | log.debug("Sending arbitration update to {}... electionId={}", |
| 312 | deviceId, newId); |
| 313 | |
| 314 | streamChannelManager.send( |
| 315 | StreamMessageRequest.newBuilder() |
| 316 | .setArbitration( |
| 317 | MasterArbitrationUpdate |
| 318 | .newBuilder() |
| 319 | .setDeviceId(p4DeviceId) |
| 320 | .setElectionId(idMsg) |
| 321 | .build()) |
| 322 | .build()); |
| 323 | clientElectionId = idMsg; |
| 324 | return true; |
| 325 | } |
| 326 | |
| 327 | private ForwardingPipelineConfig getPipelineConfig( |
| 328 | PiPipeconf pipeconf, ByteBuffer deviceData) { |
| 329 | P4Info p4Info = PipeconfHelper.getP4Info(pipeconf); |
| 330 | if (p4Info == null) { |
| 331 | // Problem logged by PipeconfHelper. |
| 332 | return null; |
Yi Tseng | 3e7f145 | 2017-10-20 10:31:53 -0700 | [diff] [blame] | 333 | } |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 334 | |
ghj0504520 | 92a48bf | 2018-10-22 10:50:41 -0700 | [diff] [blame] | 335 | ForwardingPipelineConfig.Cookie pipeconfCookie = ForwardingPipelineConfig.Cookie |
| 336 | .newBuilder() |
ghj0504520 | ec1a420 | 2018-10-22 10:50:41 -0700 | [diff] [blame] | 337 | .setCookie(pipeconf.fingerprint()) |
ghj0504520 | 92a48bf | 2018-10-22 10:50:41 -0700 | [diff] [blame] | 338 | .build(); |
| 339 | |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 340 | // FIXME: This is specific to PI P4Runtime implementation. |
| 341 | P4Config.P4DeviceConfig p4DeviceConfigMsg = P4Config.P4DeviceConfig |
| 342 | .newBuilder() |
| 343 | .setExtras(P4Config.P4DeviceConfig.Extras.getDefaultInstance()) |
| 344 | .setReassign(true) |
| 345 | .setDeviceData(ByteString.copyFrom(deviceData)) |
| 346 | .build(); |
| 347 | |
| 348 | return ForwardingPipelineConfig |
| 349 | .newBuilder() |
| 350 | .setP4Info(p4Info) |
| 351 | .setP4DeviceConfig(p4DeviceConfigMsg.toByteString()) |
ghj0504520 | 92a48bf | 2018-10-22 10:50:41 -0700 | [diff] [blame] | 352 | .setCookie(pipeconfCookie) |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 353 | .build(); |
| 354 | } |
| 355 | |
| 356 | private boolean doIsPipelineConfigSet(PiPipeconf pipeconf, ByteBuffer deviceData) { |
| 357 | |
| 358 | GetForwardingPipelineConfigRequest request = GetForwardingPipelineConfigRequest |
| 359 | .newBuilder() |
| 360 | .setDeviceId(p4DeviceId) |
ghj0504520 | 92a48bf | 2018-10-22 10:50:41 -0700 | [diff] [blame] | 361 | .setResponseType(GetForwardingPipelineConfigRequest |
| 362 | .ResponseType.COOKIE_ONLY) |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 363 | .build(); |
| 364 | |
| 365 | GetForwardingPipelineConfigResponse resp; |
| 366 | try { |
| 367 | resp = this.blockingStub |
| 368 | .getForwardingPipelineConfig(request); |
| 369 | } catch (StatusRuntimeException ex) { |
| 370 | checkGrpcException(ex); |
| 371 | // FAILED_PRECONDITION means that a pipeline config was not set in |
| 372 | // the first place. Don't bother logging. |
| 373 | if (!ex.getStatus().getCode() |
| 374 | .equals(Status.FAILED_PRECONDITION.getCode())) { |
| 375 | log.warn("Unable to get pipeline config from {}: {}", |
| 376 | deviceId, ex.getMessage()); |
| 377 | } |
| 378 | return false; |
| 379 | } |
ghj0504520 | 92a48bf | 2018-10-22 10:50:41 -0700 | [diff] [blame] | 380 | if (!resp.getConfig().hasCookie()) { |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 381 | log.warn("{} returned GetForwardingPipelineConfigResponse " + |
ghj0504520 | 92a48bf | 2018-10-22 10:50:41 -0700 | [diff] [blame] | 382 | "with 'cookie' field unset", |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 383 | deviceId); |
| 384 | return false; |
| 385 | } |
ghj0504520 | 92a48bf | 2018-10-22 10:50:41 -0700 | [diff] [blame] | 386 | |
ghj0504520 | ec1a420 | 2018-10-22 10:50:41 -0700 | [diff] [blame] | 387 | return resp.getConfig().getCookie().getCookie() == pipeconf.fingerprint(); |
Yi Tseng | 3e7f145 | 2017-10-20 10:31:53 -0700 | [diff] [blame] | 388 | } |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 389 | |
Carmelo Cascone | d61fdb3 | 2017-10-30 10:09:57 -0700 | [diff] [blame] | 390 | private boolean doSetPipelineConfig(PiPipeconf pipeconf, ByteBuffer deviceData) { |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 391 | |
Carmelo Cascone | d61fdb3 | 2017-10-30 10:09:57 -0700 | [diff] [blame] | 392 | log.info("Setting pipeline config for {} to {}...", deviceId, pipeconf.id()); |
| 393 | |
| 394 | checkNotNull(deviceData, "deviceData cannot be null"); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 395 | |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 396 | ForwardingPipelineConfig pipelineConfig = getPipelineConfig(pipeconf, deviceData); |
| 397 | |
| 398 | if (pipelineConfig == null) { |
| 399 | // Error logged in getPipelineConfig() |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 400 | return false; |
| 401 | } |
| 402 | |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 403 | SetForwardingPipelineConfigRequest request = SetForwardingPipelineConfigRequest |
| 404 | .newBuilder() |
Andrea Campanella | 8bcd586 | 2017-12-11 11:34:45 +0100 | [diff] [blame] | 405 | .setDeviceId(p4DeviceId) |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 406 | .setElectionId(clientElectionId) |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 407 | .setAction(VERIFY_AND_COMMIT) |
Andrea Campanella | 8bcd586 | 2017-12-11 11:34:45 +0100 | [diff] [blame] | 408 | .setConfig(pipelineConfig) |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 409 | .build(); |
| 410 | |
| 411 | try { |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 412 | //noinspection ResultOfMethodCallIgnored |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 413 | this.blockingStub.setForwardingPipelineConfig(request); |
Carmelo Cascone | d61fdb3 | 2017-10-30 10:09:57 -0700 | [diff] [blame] | 414 | return true; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 415 | } catch (StatusRuntimeException ex) { |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 416 | checkGrpcException(ex); |
Carmelo Cascone | 5bc7e10 | 2018-02-18 18:27:55 -0800 | [diff] [blame] | 417 | log.warn("Unable to set pipeline config on {}: {}", deviceId, ex.getMessage()); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 418 | return false; |
| 419 | } |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 420 | } |
| 421 | |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 422 | private boolean doWriteTableEntries(List<PiTableEntry> piTableEntries, WriteOperationType opType, |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 423 | PiPipeconf pipeconf) { |
Carmelo Cascone | 5bc7e10 | 2018-02-18 18:27:55 -0800 | [diff] [blame] | 424 | if (piTableEntries.size() == 0) { |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 425 | return true; |
| 426 | } |
| 427 | |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 428 | List<Update> updateMsgs; |
Carmelo Cascone | 5bc7e10 | 2018-02-18 18:27:55 -0800 | [diff] [blame] | 429 | try { |
| 430 | updateMsgs = TableEntryEncoder.encode(piTableEntries, pipeconf) |
| 431 | .stream() |
| 432 | .map(tableEntryMsg -> |
| 433 | Update.newBuilder() |
| 434 | .setEntity(Entity.newBuilder() |
| 435 | .setTableEntry(tableEntryMsg) |
| 436 | .build()) |
| 437 | .setType(UPDATE_TYPES.get(opType)) |
| 438 | .build()) |
| 439 | .collect(Collectors.toList()); |
| 440 | } catch (EncodeException e) { |
| 441 | log.error("Unable to encode table entries, aborting {} operation: {}", |
| 442 | opType.name(), e.getMessage()); |
| 443 | return false; |
| 444 | } |
| 445 | |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 446 | return write(updateMsgs, piTableEntries, opType, "table entry"); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 447 | } |
| 448 | |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 449 | private List<PiTableEntry> doDumpTables( |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 450 | Set<PiTableId> piTableIds, boolean defaultEntries, PiPipeconf pipeconf) { |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 451 | |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 452 | log.debug("Dumping tables {} from {} (pipeconf {})...", |
| 453 | piTableIds, deviceId, pipeconf.id()); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 454 | |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 455 | Set<Integer> tableIds = Sets.newHashSet(); |
| 456 | if (piTableIds == null) { |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 457 | // Dump all tables. |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 458 | tableIds.add(0); |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 459 | } else { |
| 460 | P4InfoBrowser browser = PipeconfHelper.getP4InfoBrowser(pipeconf); |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 461 | if (browser == null) { |
| 462 | log.warn("Unable to get a P4Info browser for pipeconf {}", pipeconf); |
| 463 | return Collections.emptyList(); |
| 464 | } |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 465 | piTableIds.forEach(piTableId -> { |
| 466 | try { |
| 467 | tableIds.add(browser.tables().getByName(piTableId.id()).getPreamble().getId()); |
| 468 | } catch (P4InfoBrowser.NotFoundException e) { |
| 469 | log.warn("Unable to dump table {}: {}", piTableId, e.getMessage()); |
| 470 | } |
| 471 | }); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 472 | } |
| 473 | |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 474 | if (tableIds.isEmpty()) { |
| 475 | return Collections.emptyList(); |
| 476 | } |
| 477 | |
| 478 | ReadRequest.Builder requestMsgBuilder = ReadRequest.newBuilder() |
| 479 | .setDeviceId(p4DeviceId); |
| 480 | tableIds.forEach(tableId -> requestMsgBuilder.addEntities( |
| 481 | Entity.newBuilder() |
| 482 | .setTableEntry( |
| 483 | TableEntry.newBuilder() |
| 484 | .setTableId(tableId) |
| 485 | .setIsDefaultAction(defaultEntries) |
steven30801 | 7632e15 | 2018-10-20 00:51:08 +0800 | [diff] [blame] | 486 | .setCounterData(P4RuntimeOuterClass.CounterData.getDefaultInstance()) |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 487 | .build()) |
| 488 | .build()) |
| 489 | .build()); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 490 | |
| 491 | Iterator<ReadResponse> responses; |
| 492 | try { |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 493 | responses = blockingStub.read(requestMsgBuilder.build()); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 494 | } catch (StatusRuntimeException e) { |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 495 | checkGrpcException(e); |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 496 | log.warn("Unable to dump tables from {}: {}", deviceId, e.getMessage()); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 497 | return Collections.emptyList(); |
| 498 | } |
| 499 | |
| 500 | Iterable<ReadResponse> responseIterable = () -> responses; |
| 501 | List<TableEntry> tableEntryMsgs = StreamSupport |
| 502 | .stream(responseIterable.spliterator(), false) |
| 503 | .map(ReadResponse::getEntitiesList) |
| 504 | .flatMap(List::stream) |
| 505 | .filter(entity -> entity.getEntityCase() == TABLE_ENTRY) |
| 506 | .map(Entity::getTableEntry) |
| 507 | .collect(Collectors.toList()); |
| 508 | |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 509 | log.debug("Retrieved {} entries from {} tables on {}...", |
| 510 | tableEntryMsgs.size(), tableIds.size(), deviceId); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 511 | |
| 512 | return TableEntryEncoder.decode(tableEntryMsgs, pipeconf); |
| 513 | } |
| 514 | |
Andrea Campanella | fc1d34c | 2017-07-18 17:01:41 +0200 | [diff] [blame] | 515 | private boolean doPacketOut(PiPacketOperation packet, PiPipeconf pipeconf) { |
| 516 | try { |
| 517 | //encode the PiPacketOperation into a PacketOut |
| 518 | PacketOut packetOut = PacketIOCodec.encodePacketOut(packet, pipeconf); |
| 519 | |
| 520 | //Build the request |
| 521 | StreamMessageRequest packetOutRequest = StreamMessageRequest |
| 522 | .newBuilder().setPacket(packetOut).build(); |
| 523 | |
| 524 | //Send the request |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 525 | streamChannelManager.send(packetOutRequest); |
Andrea Campanella | fc1d34c | 2017-07-18 17:01:41 +0200 | [diff] [blame] | 526 | |
| 527 | } catch (P4InfoBrowser.NotFoundException e) { |
| 528 | log.error("Cant find expected metadata in p4Info file. {}", e.getMessage()); |
| 529 | log.debug("Exception", e); |
| 530 | return false; |
| 531 | } |
| 532 | return true; |
| 533 | } |
| 534 | |
Carmelo Cascone | a966c34 | 2017-07-30 01:56:30 -0400 | [diff] [blame] | 535 | private void doPacketIn(PacketIn packetInMsg) { |
| 536 | |
| 537 | // Retrieve the pipeconf for this client's device. |
| 538 | PiPipeconfService pipeconfService = DefaultServiceDirectory.getService(PiPipeconfService.class); |
| 539 | if (pipeconfService == null) { |
| 540 | throw new IllegalStateException("PiPipeconfService is null. Can't handle packet in."); |
| 541 | } |
| 542 | final PiPipeconf pipeconf; |
| 543 | if (pipeconfService.ofDevice(deviceId).isPresent() && |
| 544 | pipeconfService.getPipeconf(pipeconfService.ofDevice(deviceId).get()).isPresent()) { |
| 545 | pipeconf = pipeconfService.getPipeconf(pipeconfService.ofDevice(deviceId).get()).get(); |
| 546 | } else { |
| 547 | log.warn("Unable to get pipeconf of {}. Can't handle packet in", deviceId); |
| 548 | return; |
| 549 | } |
| 550 | // Decode packet message and post event. |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 551 | PiPacketOperation packetOperation = PacketIOCodec.decodePacketIn(packetInMsg, pipeconf, deviceId); |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 552 | PacketInEvent packetInEventSubject = new PacketInEvent(deviceId, packetOperation); |
Carmelo Cascone | 2cad9ef | 2017-08-01 21:52:07 +0200 | [diff] [blame] | 553 | P4RuntimeEvent event = new P4RuntimeEvent(P4RuntimeEvent.Type.PACKET_IN, packetInEventSubject); |
Carmelo Cascone | a966c34 | 2017-07-30 01:56:30 -0400 | [diff] [blame] | 554 | log.debug("Received packet in: {}", event); |
| 555 | controller.postEvent(event); |
| 556 | } |
| 557 | |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 558 | private void doArbitrationResponse(MasterArbitrationUpdate msg) { |
| 559 | // From the spec... |
| 560 | // - Election_id: The stream RPC with the highest election_id is the |
| 561 | // master. Switch populates with the highest election ID it |
| 562 | // has received from all connected controllers. |
| 563 | // - Status: Switch populates this with OK for the client that is the |
| 564 | // master, and with an error status for all other connected clients (at |
| 565 | // every mastership change). |
| 566 | if (!msg.hasElectionId() || !msg.hasStatus()) { |
Yi Tseng | 3e7f145 | 2017-10-20 10:31:53 -0700 | [diff] [blame] | 567 | return; |
| 568 | } |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 569 | final boolean isMaster = |
| 570 | msg.getStatus().getCode() == Status.OK.getCode().value(); |
| 571 | log.debug("Received arbitration update from {}: isMaster={}, electionId={}", |
| 572 | deviceId, isMaster, uint128ToBigInteger(msg.getElectionId())); |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 573 | controller.postEvent(new P4RuntimeEvent( |
| 574 | P4RuntimeEvent.Type.ARBITRATION_RESPONSE, |
| 575 | new ArbitrationResponse(deviceId, isMaster))); |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 576 | isClientMaster.set(isMaster); |
Carmelo Cascone | a966c34 | 2017-07-30 01:56:30 -0400 | [diff] [blame] | 577 | } |
| 578 | |
steven30801 | 7632e15 | 2018-10-20 00:51:08 +0800 | [diff] [blame] | 579 | private List<PiCounterCell> doReadAllCounterCells( |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 580 | List<PiCounterId> counterIds, PiPipeconf pipeconf) { |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 581 | return doReadCounterEntities( |
| 582 | CounterEntryCodec.readAllCellsEntities(counterIds, pipeconf), |
| 583 | pipeconf); |
| 584 | } |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 585 | |
steven30801 | 7632e15 | 2018-10-20 00:51:08 +0800 | [diff] [blame] | 586 | private List<PiCounterCell> doReadCounterCells( |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 587 | List<PiCounterCellId> cellIds, PiPipeconf pipeconf) { |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 588 | return doReadCounterEntities( |
| 589 | CounterEntryCodec.encodePiCounterCellIds(cellIds, pipeconf), |
| 590 | pipeconf); |
| 591 | } |
| 592 | |
steven30801 | 7632e15 | 2018-10-20 00:51:08 +0800 | [diff] [blame] | 593 | private List<PiCounterCell> doReadCounterEntities( |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 594 | List<Entity> counterEntities, PiPipeconf pipeconf) { |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 595 | |
| 596 | if (counterEntities.size() == 0) { |
| 597 | return Collections.emptyList(); |
| 598 | } |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 599 | |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 600 | final ReadRequest request = ReadRequest.newBuilder() |
| 601 | .setDeviceId(p4DeviceId) |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 602 | .addAllEntities(counterEntities) |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 603 | .build(); |
| 604 | |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 605 | final Iterable<ReadResponse> responses; |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 606 | try { |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 607 | responses = () -> blockingStub.read(request); |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 608 | } catch (StatusRuntimeException e) { |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 609 | checkGrpcException(e); |
Carmelo Cascone | 5bc7e10 | 2018-02-18 18:27:55 -0800 | [diff] [blame] | 610 | log.warn("Unable to read counter cells from {}: {}", deviceId, e.getMessage()); |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 611 | return Collections.emptyList(); |
| 612 | } |
| 613 | |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 614 | List<Entity> entities = StreamSupport.stream(responses.spliterator(), false) |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 615 | .map(ReadResponse::getEntitiesList) |
| 616 | .flatMap(List::stream) |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 617 | .collect(Collectors.toList()); |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 618 | |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 619 | return CounterEntryCodec.decodeCounterEntities(entities, pipeconf); |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 620 | } |
| 621 | |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 622 | private boolean doWriteActionGroupMembers(List<PiActionGroupMember> members, |
Yi Tseng | 8d35513 | 2018-04-13 01:40:48 +0800 | [diff] [blame] | 623 | WriteOperationType opType, PiPipeconf pipeconf) { |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 624 | final List<ActionProfileMember> actionProfileMembers = Lists.newArrayList(); |
Carmelo Cascone | 5bc7e10 | 2018-02-18 18:27:55 -0800 | [diff] [blame] | 625 | |
Yi Tseng | 8d35513 | 2018-04-13 01:40:48 +0800 | [diff] [blame] | 626 | for (PiActionGroupMember member : members) { |
Carmelo Cascone | 5bc7e10 | 2018-02-18 18:27:55 -0800 | [diff] [blame] | 627 | try { |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 628 | actionProfileMembers.add(ActionProfileMemberEncoder.encode(member, pipeconf)); |
Carmelo Cascone | 5bc7e10 | 2018-02-18 18:27:55 -0800 | [diff] [blame] | 629 | } catch (EncodeException | P4InfoBrowser.NotFoundException e) { |
| 630 | log.warn("Unable to encode group member, aborting {} operation: {} [{}]", |
| 631 | opType.name(), e.getMessage(), member.toString()); |
| 632 | return false; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 633 | } |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 634 | } |
| 635 | |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 636 | final List<Update> updateMsgs = actionProfileMembers.stream() |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 637 | .map(actionProfileMember -> |
| 638 | Update.newBuilder() |
| 639 | .setEntity(Entity.newBuilder() |
| 640 | .setActionProfileMember(actionProfileMember) |
| 641 | .build()) |
| 642 | .setType(UPDATE_TYPES.get(opType)) |
| 643 | .build()) |
| 644 | .collect(Collectors.toList()); |
| 645 | |
| 646 | if (updateMsgs.size() == 0) { |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 647 | // Nothing to update. |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 648 | return true; |
| 649 | } |
| 650 | |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 651 | return write(updateMsgs, members, opType, "group member"); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 652 | } |
| 653 | |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 654 | private List<PiActionGroup> doDumpGroups(PiActionProfileId piActionProfileId, PiPipeconf pipeconf) { |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 655 | log.debug("Dumping groups from action profile {} from {} (pipeconf {})...", |
| 656 | piActionProfileId.id(), deviceId, pipeconf.id()); |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 657 | |
| 658 | final P4InfoBrowser browser = PipeconfHelper.getP4InfoBrowser(pipeconf); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 659 | if (browser == null) { |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 660 | log.warn("Unable to get a P4Info browser for pipeconf {}, aborting dump action profile", pipeconf); |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 661 | return Collections.emptyList(); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 662 | } |
| 663 | |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 664 | final int actionProfileId; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 665 | try { |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 666 | actionProfileId = browser |
| 667 | .actionProfiles() |
Carmelo Cascone | cb0a49c | 2017-10-03 14:32:23 +0200 | [diff] [blame] | 668 | .getByName(piActionProfileId.id()) |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 669 | .getPreamble() |
| 670 | .getId(); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 671 | } catch (P4InfoBrowser.NotFoundException e) { |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 672 | log.warn("Unable to dump groups: {}", e.getMessage()); |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 673 | return Collections.emptyList(); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 674 | } |
| 675 | |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 676 | // Prepare read request to read all groups from the given action profile. |
| 677 | final ReadRequest groupRequestMsg = ReadRequest.newBuilder() |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 678 | .setDeviceId(p4DeviceId) |
| 679 | .addEntities(Entity.newBuilder() |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 680 | .setActionProfileGroup( |
| 681 | ActionProfileGroup.newBuilder() |
| 682 | .setActionProfileId(actionProfileId) |
| 683 | .build()) |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 684 | .build()) |
| 685 | .build(); |
| 686 | |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 687 | // Read groups. |
| 688 | final Iterator<ReadResponse> groupResponses; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 689 | try { |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 690 | groupResponses = blockingStub.read(groupRequestMsg); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 691 | } catch (StatusRuntimeException e) { |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 692 | checkGrpcException(e); |
Carmelo Cascone | 5bc7e10 | 2018-02-18 18:27:55 -0800 | [diff] [blame] | 693 | log.warn("Unable to dump action profile {} from {}: {}", piActionProfileId, deviceId, e.getMessage()); |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 694 | return Collections.emptyList(); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 695 | } |
| 696 | |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 697 | final List<ActionProfileGroup> groupMsgs = Tools.stream(() -> groupResponses) |
| 698 | .map(ReadResponse::getEntitiesList) |
| 699 | .flatMap(List::stream) |
| 700 | .filter(entity -> entity.getEntityCase() == ACTION_PROFILE_GROUP) |
| 701 | .map(Entity::getActionProfileGroup) |
| 702 | .collect(Collectors.toList()); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 703 | |
| 704 | log.debug("Retrieved {} groups from action profile {} on {}...", |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 705 | groupMsgs.size(), piActionProfileId.id(), deviceId); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 706 | |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 707 | // Returned groups contain only a minimal description of their members. |
| 708 | // We need to issue a new request to get the full description of each member. |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 709 | |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 710 | // Keep a map of all member IDs for each group ID, will need it later. |
| 711 | final Multimap<Integer, Integer> groupIdToMemberIdsMap = HashMultimap.create(); |
| 712 | groupMsgs.forEach(g -> groupIdToMemberIdsMap.putAll( |
| 713 | g.getGroupId(), |
| 714 | g.getMembersList().stream() |
| 715 | .map(ActionProfileGroup.Member::getMemberId) |
| 716 | .collect(Collectors.toList()))); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 717 | |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 718 | // Prepare one big read request to read all members in one shot. |
| 719 | final Set<Entity> entityMsgs = groupMsgs.stream() |
| 720 | .flatMap(g -> g.getMembersList().stream()) |
| 721 | .map(ActionProfileGroup.Member::getMemberId) |
| 722 | // Prevent issuing many read requests for the same member. |
| 723 | .distinct() |
| 724 | .map(id -> ActionProfileMember.newBuilder() |
| 725 | .setActionProfileId(actionProfileId) |
| 726 | .setMemberId(id) |
| 727 | .build()) |
| 728 | .map(m -> Entity.newBuilder() |
| 729 | .setActionProfileMember(m) |
| 730 | .build()) |
| 731 | .collect(Collectors.toSet()); |
| 732 | final ReadRequest memberRequestMsg = ReadRequest.newBuilder().setDeviceId(p4DeviceId) |
| 733 | .addAllEntities(entityMsgs) |
| 734 | .build(); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 735 | |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 736 | // Read members. |
| 737 | final Iterator<ReadResponse> memberResponses; |
| 738 | try { |
| 739 | memberResponses = blockingStub.read(memberRequestMsg); |
| 740 | } catch (StatusRuntimeException e) { |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 741 | checkGrpcException(e); |
Carmelo Cascone | 5bc7e10 | 2018-02-18 18:27:55 -0800 | [diff] [blame] | 742 | log.warn("Unable to read members of action profile {} from {}: {}", |
| 743 | piActionProfileId, deviceId, e.getMessage()); |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 744 | return Collections.emptyList(); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 745 | } |
| 746 | |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 747 | final Multimap<Integer, ActionProfileMember> groupIdToMembersMap = HashMultimap.create(); |
| 748 | Tools.stream(() -> memberResponses) |
| 749 | .map(ReadResponse::getEntitiesList) |
| 750 | .flatMap(List::stream) |
| 751 | .filter(e -> e.getEntityCase() == ACTION_PROFILE_MEMBER) |
| 752 | .map(Entity::getActionProfileMember) |
| 753 | .forEach(member -> groupIdToMemberIdsMap.asMap() |
| 754 | // Get all group IDs that contain this member. |
| 755 | .entrySet() |
| 756 | .stream() |
| 757 | .filter(entry -> entry.getValue().contains(member.getMemberId())) |
| 758 | .map(Map.Entry::getKey) |
| 759 | .forEach(gid -> groupIdToMembersMap.put(gid, member))); |
| 760 | |
| 761 | log.debug("Retrieved {} group members from action profile {} on {}...", |
| 762 | groupIdToMembersMap.size(), piActionProfileId.id(), deviceId); |
| 763 | |
| 764 | return groupMsgs.stream() |
| 765 | .map(groupMsg -> { |
| 766 | try { |
| 767 | return ActionProfileGroupEncoder.decode(groupMsg, |
| 768 | groupIdToMembersMap.get(groupMsg.getGroupId()), |
| 769 | pipeconf); |
| 770 | } catch (P4InfoBrowser.NotFoundException | EncodeException e) { |
| 771 | log.warn("Unable to decode group: {}\n {}", e.getMessage(), groupMsg); |
| 772 | return null; |
| 773 | } |
| 774 | }) |
| 775 | .filter(Objects::nonNull) |
| 776 | .collect(Collectors.toList()); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 777 | } |
| 778 | |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 779 | private List<PiActionGroupMemberId> doDumpActionProfileMemberIds( |
| 780 | PiActionProfileId actionProfileId, PiPipeconf pipeconf) { |
| 781 | |
| 782 | final P4InfoBrowser browser = PipeconfHelper.getP4InfoBrowser(pipeconf); |
| 783 | if (browser == null) { |
| 784 | log.warn("Unable to get a P4Info browser for pipeconf {}, " + |
| 785 | "aborting cleanup of action profile members", |
| 786 | pipeconf); |
| 787 | return Collections.emptyList(); |
| 788 | } |
| 789 | |
| 790 | final int p4ActProfId; |
| 791 | try { |
| 792 | p4ActProfId = browser |
| 793 | .actionProfiles() |
| 794 | .getByName(actionProfileId.id()) |
| 795 | .getPreamble() |
| 796 | .getId(); |
| 797 | } catch (P4InfoBrowser.NotFoundException e) { |
| 798 | log.warn("Unable to cleanup action profile members: {}", e.getMessage()); |
| 799 | return Collections.emptyList(); |
| 800 | } |
| 801 | |
| 802 | final ReadRequest memberRequestMsg = ReadRequest.newBuilder() |
| 803 | .setDeviceId(p4DeviceId) |
| 804 | .addEntities(Entity.newBuilder().setActionProfileMember( |
| 805 | ActionProfileMember.newBuilder() |
| 806 | .setActionProfileId(p4ActProfId) |
| 807 | .build()).build()) |
| 808 | .build(); |
| 809 | |
| 810 | // Read members. |
| 811 | final Iterator<ReadResponse> memberResponses; |
| 812 | try { |
| 813 | memberResponses = blockingStub.read(memberRequestMsg); |
| 814 | } catch (StatusRuntimeException e) { |
| 815 | checkGrpcException(e); |
| 816 | log.warn("Unable to read members of action profile {} from {}: {}", |
| 817 | actionProfileId, deviceId, e.getMessage()); |
| 818 | return Collections.emptyList(); |
| 819 | } |
| 820 | |
| 821 | return Tools.stream(() -> memberResponses) |
| 822 | .map(ReadResponse::getEntitiesList) |
| 823 | .flatMap(List::stream) |
| 824 | .filter(e -> e.getEntityCase() == ACTION_PROFILE_MEMBER) |
| 825 | .map(Entity::getActionProfileMember) |
| 826 | // Perhaps not needed, but better to double check to avoid |
| 827 | // removing members of other groups. |
| 828 | .filter(m -> m.getActionProfileId() == p4ActProfId) |
| 829 | .map(ActionProfileMember::getMemberId) |
| 830 | .map(PiActionGroupMemberId::of) |
| 831 | .collect(Collectors.toList()); |
| 832 | } |
| 833 | |
| 834 | private List<PiActionGroupMemberId> doRemoveActionProfileMembers( |
| 835 | PiActionProfileId actionProfileId, |
| 836 | List<PiActionGroupMemberId> memberIds, |
| 837 | PiPipeconf pipeconf) { |
| 838 | |
| 839 | if (memberIds.isEmpty()) { |
| 840 | return Collections.emptyList(); |
| 841 | } |
| 842 | |
| 843 | final P4InfoBrowser browser = PipeconfHelper.getP4InfoBrowser(pipeconf); |
| 844 | if (browser == null) { |
| 845 | log.warn("Unable to get a P4Info browser for pipeconf {}, " + |
| 846 | "aborting cleanup of action profile members", |
| 847 | pipeconf); |
| 848 | return Collections.emptyList(); |
| 849 | } |
| 850 | |
| 851 | final int p4ActProfId; |
| 852 | try { |
| 853 | p4ActProfId = browser.actionProfiles() |
| 854 | .getByName(actionProfileId.id()).getPreamble().getId(); |
| 855 | } catch (P4InfoBrowser.NotFoundException e) { |
| 856 | log.warn("Unable to cleanup action profile members: {}", e.getMessage()); |
| 857 | return Collections.emptyList(); |
| 858 | } |
| 859 | |
| 860 | final List<Update> updateMsgs = memberIds.stream() |
| 861 | .map(m -> ActionProfileMember.newBuilder() |
| 862 | .setActionProfileId(p4ActProfId) |
| 863 | .setMemberId(m.id()).build()) |
| 864 | .map(m -> Entity.newBuilder().setActionProfileMember(m).build()) |
| 865 | .map(e -> Update.newBuilder().setEntity(e) |
| 866 | .setType(Update.Type.DELETE).build()) |
| 867 | .collect(Collectors.toList()); |
| 868 | |
| 869 | log.debug("Removing {} members of action profile '{}'...", |
| 870 | memberIds.size(), actionProfileId); |
| 871 | |
| 872 | return writeAndReturnSuccessEntities( |
| 873 | updateMsgs, memberIds, WriteOperationType.DELETE, |
| 874 | "action profile members"); |
| 875 | } |
| 876 | |
ghj0504520 | ed7340c | 2018-10-26 13:06:35 -0700 | [diff] [blame] | 877 | private boolean doWriteActionGroup(PiActionGroup group, WriteOperationType opType, PiPipeconf pipeconf, |
| 878 | int maxMemberSize) { |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 879 | final ActionProfileGroup actionProfileGroup; |
ghj0504520 | ed7340c | 2018-10-26 13:06:35 -0700 | [diff] [blame] | 880 | if (opType == P4RuntimeClient.WriteOperationType.INSERT && maxMemberSize < group.members().size()) { |
| 881 | log.warn("Unable to encode group, since group member larger than maximum member size"); |
| 882 | return false; |
| 883 | } |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 884 | try { |
ghj0504520 | ed7340c | 2018-10-26 13:06:35 -0700 | [diff] [blame] | 885 | actionProfileGroup = ActionProfileGroupEncoder.encode(group, pipeconf, maxMemberSize); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 886 | } catch (EncodeException | P4InfoBrowser.NotFoundException e) { |
Carmelo Cascone | 5bc7e10 | 2018-02-18 18:27:55 -0800 | [diff] [blame] | 887 | log.warn("Unable to encode group, aborting {} operation: {}", e.getMessage(), opType.name()); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 888 | return false; |
| 889 | } |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 890 | |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 891 | final Update updateMsg = Update.newBuilder() |
| 892 | .setEntity(Entity.newBuilder() |
| 893 | .setActionProfileGroup(actionProfileGroup) |
| 894 | .build()) |
| 895 | .setType(UPDATE_TYPES.get(opType)) |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 896 | .build(); |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 897 | |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 898 | return write(singletonList(updateMsg), singletonList(group), |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 899 | opType, "group"); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 900 | } |
| 901 | |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 902 | private List<PiMeterCellConfig> doReadAllMeterCells( |
| 903 | List<PiMeterId> meterIds, PiPipeconf pipeconf) { |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 904 | return doReadMeterEntities(MeterEntryCodec.readAllCellsEntities( |
| 905 | meterIds, pipeconf), pipeconf); |
| 906 | } |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 907 | |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 908 | private List<PiMeterCellConfig> doReadMeterCells( |
| 909 | List<PiMeterCellId> cellIds, PiPipeconf pipeconf) { |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 910 | |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 911 | final List<PiMeterCellConfig> piMeterCellConfigs = cellIds.stream() |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 912 | .map(cellId -> PiMeterCellConfig.builder() |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 913 | .withMeterCellId(cellId) |
| 914 | .build()) |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 915 | .collect(Collectors.toList()); |
| 916 | |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 917 | return doReadMeterEntities(MeterEntryCodec.encodePiMeterCellConfigs( |
| 918 | piMeterCellConfigs, pipeconf), pipeconf); |
| 919 | } |
| 920 | |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 921 | private List<PiMeterCellConfig> doReadMeterEntities( |
| 922 | List<Entity> entitiesToRead, PiPipeconf pipeconf) { |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 923 | |
| 924 | if (entitiesToRead.size() == 0) { |
| 925 | return Collections.emptyList(); |
| 926 | } |
| 927 | |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 928 | final ReadRequest request = ReadRequest.newBuilder() |
| 929 | .setDeviceId(p4DeviceId) |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 930 | .addAllEntities(entitiesToRead) |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 931 | .build(); |
| 932 | |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 933 | final Iterable<ReadResponse> responses; |
| 934 | try { |
| 935 | responses = () -> blockingStub.read(request); |
| 936 | } catch (StatusRuntimeException e) { |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 937 | checkGrpcException(e); |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 938 | log.warn("Unable to read meter cells: {}", e.getMessage()); |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 939 | log.debug("exception", e); |
| 940 | return Collections.emptyList(); |
| 941 | } |
| 942 | |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 943 | List<Entity> responseEntities = StreamSupport |
| 944 | .stream(responses.spliterator(), false) |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 945 | .map(ReadResponse::getEntitiesList) |
| 946 | .flatMap(List::stream) |
| 947 | .collect(Collectors.toList()); |
| 948 | |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 949 | return MeterEntryCodec.decodeMeterEntities(responseEntities, pipeconf); |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 950 | } |
| 951 | |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 952 | private boolean doWriteMeterCells(List<PiMeterCellConfig> cellConfigs, PiPipeconf pipeconf) { |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 953 | |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 954 | List<Update> updateMsgs = MeterEntryCodec.encodePiMeterCellConfigs(cellConfigs, pipeconf) |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 955 | .stream() |
| 956 | .map(meterEntryMsg -> |
| 957 | Update.newBuilder() |
| 958 | .setEntity(meterEntryMsg) |
| 959 | .setType(UPDATE_TYPES.get(WriteOperationType.MODIFY)) |
| 960 | .build()) |
| 961 | .collect(Collectors.toList()); |
| 962 | |
| 963 | if (updateMsgs.size() == 0) { |
| 964 | return true; |
| 965 | } |
| 966 | |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 967 | return write(updateMsgs, cellConfigs, WriteOperationType.MODIFY, "meter cell config"); |
| 968 | } |
| 969 | |
| 970 | private boolean doWriteMulticastGroupEntries( |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 971 | List<PiMulticastGroupEntry> entries, |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 972 | WriteOperationType opType) { |
| 973 | |
| 974 | final List<Update> updateMsgs = entries.stream() |
Carmelo Cascone | 03ae0ac | 2018-10-11 08:31:59 -0700 | [diff] [blame] | 975 | .map(piEntry -> { |
| 976 | try { |
| 977 | return MulticastGroupEntryCodec.encode(piEntry); |
| 978 | } catch (EncodeException e) { |
| 979 | log.warn("Unable to encode PiMulticastGroupEntry: {}", e.getMessage()); |
| 980 | return null; |
| 981 | } |
| 982 | }) |
| 983 | .filter(Objects::nonNull) |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 984 | .map(mcMsg -> PacketReplicationEngineEntry.newBuilder() |
| 985 | .setMulticastGroupEntry(mcMsg) |
| 986 | .build()) |
| 987 | .map(preMsg -> Entity.newBuilder() |
| 988 | .setPacketReplicationEngineEntry(preMsg) |
| 989 | .build()) |
| 990 | .map(entityMsg -> Update.newBuilder() |
| 991 | .setEntity(entityMsg) |
| 992 | .setType(UPDATE_TYPES.get(opType)) |
| 993 | .build()) |
| 994 | .collect(Collectors.toList()); |
| 995 | return write(updateMsgs, entries, opType, "multicast group entry"); |
| 996 | } |
| 997 | |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 998 | private List<PiMulticastGroupEntry> doReadAllMulticastGroupEntries() { |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 999 | |
| 1000 | final Entity entity = Entity.newBuilder() |
| 1001 | .setPacketReplicationEngineEntry( |
| 1002 | PacketReplicationEngineEntry.newBuilder() |
| 1003 | .setMulticastGroupEntry( |
| 1004 | MulticastGroupEntry.newBuilder() |
| 1005 | .build()) |
| 1006 | .build()) |
| 1007 | .build(); |
| 1008 | |
| 1009 | final ReadRequest req = ReadRequest.newBuilder() |
| 1010 | .setDeviceId(p4DeviceId) |
| 1011 | .addEntities(entity) |
| 1012 | .build(); |
| 1013 | |
| 1014 | Iterator<ReadResponse> responses; |
| 1015 | try { |
| 1016 | responses = blockingStub.read(req); |
| 1017 | } catch (StatusRuntimeException e) { |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 1018 | checkGrpcException(e); |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 1019 | log.warn("Unable to read multicast group entries from {}: {}", deviceId, e.getMessage()); |
| 1020 | return Collections.emptyList(); |
| 1021 | } |
| 1022 | |
| 1023 | Iterable<ReadResponse> responseIterable = () -> responses; |
| 1024 | final List<PiMulticastGroupEntry> mcEntries = StreamSupport |
| 1025 | .stream(responseIterable.spliterator(), false) |
| 1026 | .map(ReadResponse::getEntitiesList) |
| 1027 | .flatMap(List::stream) |
| 1028 | .filter(e -> e.getEntityCase() |
| 1029 | .equals(PACKET_REPLICATION_ENGINE_ENTRY)) |
| 1030 | .map(Entity::getPacketReplicationEngineEntry) |
| 1031 | .filter(e -> e.getTypeCase().equals(MULTICAST_GROUP_ENTRY)) |
| 1032 | .map(PacketReplicationEngineEntry::getMulticastGroupEntry) |
| 1033 | .map(MulticastGroupEntryCodec::decode) |
| 1034 | .collect(Collectors.toList()); |
| 1035 | |
| 1036 | log.debug("Retrieved {} multicast group entries from {}...", |
| 1037 | mcEntries.size(), deviceId); |
| 1038 | |
| 1039 | return mcEntries; |
| 1040 | } |
| 1041 | |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 1042 | private <T> boolean write(List<Update> updates, |
| 1043 | List<T> writeEntities, |
| 1044 | WriteOperationType opType, |
| 1045 | String entryType) { |
| 1046 | // True if all entities were successfully written. |
Carmelo Cascone | 03ae0ac | 2018-10-11 08:31:59 -0700 | [diff] [blame] | 1047 | return writeAndReturnSuccessEntities(updates, writeEntities, opType, entryType) |
| 1048 | .size() == writeEntities.size(); |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 1049 | } |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 1050 | |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 1051 | private <T> List<T> writeAndReturnSuccessEntities( |
| 1052 | List<Update> updates, List<T> writeEntities, |
| 1053 | WriteOperationType opType, String entryType) { |
Carmelo Cascone | 03ae0ac | 2018-10-11 08:31:59 -0700 | [diff] [blame] | 1054 | if (updates.isEmpty()) { |
| 1055 | return Collections.emptyList(); |
| 1056 | } |
| 1057 | if (updates.size() != writeEntities.size()) { |
| 1058 | log.error("Cannot perform {} operation, provided {} " + |
| 1059 | "update messages for {} {} - BUG?", |
| 1060 | opType, updates.size(), writeEntities.size(), entryType); |
| 1061 | return Collections.emptyList(); |
| 1062 | } |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 1063 | try { |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 1064 | //noinspection ResultOfMethodCallIgnored |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 1065 | blockingStub.write(writeRequest(updates)); |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 1066 | return writeEntities; |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 1067 | } catch (StatusRuntimeException e) { |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 1068 | return checkAndLogWriteErrors(writeEntities, e, opType, entryType); |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 1069 | } |
| 1070 | } |
| 1071 | |
| 1072 | private WriteRequest writeRequest(Iterable<Update> updateMsgs) { |
| 1073 | return WriteRequest.newBuilder() |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 1074 | .setDeviceId(p4DeviceId) |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 1075 | .setElectionId(clientElectionId) |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 1076 | .addAllUpdates(updateMsgs) |
| 1077 | .build(); |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 1078 | } |
| 1079 | |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 1080 | protected Void doShutdown() { |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 1081 | streamChannelManager.complete(); |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 1082 | return super.doShutdown(); |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 1083 | } |
| 1084 | |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 1085 | // Returns the collection of succesfully write entities. |
| 1086 | private <T> List<T> checkAndLogWriteErrors( |
| 1087 | List<T> writeEntities, StatusRuntimeException ex, |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 1088 | WriteOperationType opType, String entryType) { |
| 1089 | |
| 1090 | checkGrpcException(ex); |
| 1091 | |
Carmelo Cascone | 943c664 | 2018-09-11 13:01:03 -0700 | [diff] [blame] | 1092 | final List<P4RuntimeOuterClass.Error> errors = extractWriteErrorDetails(ex); |
Carmelo Cascone | 5bc7e10 | 2018-02-18 18:27:55 -0800 | [diff] [blame] | 1093 | |
Carmelo Cascone | 943c664 | 2018-09-11 13:01:03 -0700 | [diff] [blame] | 1094 | if (errors.isEmpty()) { |
| 1095 | final String description = ex.getStatus().getDescription(); |
| 1096 | log.warn("Unable to {} {} {}(s) on {}: {}", |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 1097 | opType.name(), writeEntities.size(), entryType, deviceId, |
| 1098 | ex.getStatus().getCode().name(), |
Carmelo Cascone | 943c664 | 2018-09-11 13:01:03 -0700 | [diff] [blame] | 1099 | description == null ? "" : " - " + description); |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 1100 | return Collections.emptyList(); |
Carmelo Cascone | 5bc7e10 | 2018-02-18 18:27:55 -0800 | [diff] [blame] | 1101 | } |
| 1102 | |
Carmelo Cascone | 5bc7e10 | 2018-02-18 18:27:55 -0800 | [diff] [blame] | 1103 | if (errors.size() == writeEntities.size()) { |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 1104 | List<T> okEntities = Lists.newArrayList(); |
| 1105 | Iterator<T> entityIterator = writeEntities.iterator(); |
| 1106 | for (P4RuntimeOuterClass.Error error : errors) { |
| 1107 | T entity = entityIterator.next(); |
| 1108 | if (error.getCanonicalCode() != Status.OK.getCode().value()) { |
| 1109 | log.warn("Unable to {} {} on {}: {} [{}]", |
| 1110 | opType.name(), entryType, deviceId, |
| 1111 | parseP4Error(error), entity.toString()); |
| 1112 | } else { |
| 1113 | okEntities.add(entity); |
| 1114 | } |
| 1115 | } |
| 1116 | return okEntities; |
Carmelo Cascone | 5bc7e10 | 2018-02-18 18:27:55 -0800 | [diff] [blame] | 1117 | } else { |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 1118 | log.warn("Unable to reconcile error details to {} updates " + |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 1119 | "(sent {} updates, but device returned {} errors)", |
| 1120 | entryType, writeEntities.size(), errors.size()); |
Carmelo Cascone | 5bc7e10 | 2018-02-18 18:27:55 -0800 | [diff] [blame] | 1121 | errors.stream() |
| 1122 | .filter(err -> err.getCanonicalCode() != Status.OK.getCode().value()) |
| 1123 | .forEach(err -> log.warn("Unable to {} {} (unknown): {}", |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 1124 | opType.name(), entryType, parseP4Error(err))); |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 1125 | return Collections.emptyList(); |
Carmelo Cascone | 5bc7e10 | 2018-02-18 18:27:55 -0800 | [diff] [blame] | 1126 | } |
| 1127 | } |
| 1128 | |
| 1129 | private List<P4RuntimeOuterClass.Error> extractWriteErrorDetails( |
Carmelo Cascone | 943c664 | 2018-09-11 13:01:03 -0700 | [diff] [blame] | 1130 | StatusRuntimeException ex) { |
| 1131 | if (!ex.getTrailers().containsKey(STATUS_DETAILS_KEY)) { |
Carmelo Cascone | 5bc7e10 | 2018-02-18 18:27:55 -0800 | [diff] [blame] | 1132 | return Collections.emptyList(); |
| 1133 | } |
Carmelo Cascone | 943c664 | 2018-09-11 13:01:03 -0700 | [diff] [blame] | 1134 | com.google.rpc.Status status = ex.getTrailers().get(STATUS_DETAILS_KEY); |
| 1135 | if (status == null) { |
| 1136 | return Collections.emptyList(); |
| 1137 | } |
Carmelo Cascone | 5bc7e10 | 2018-02-18 18:27:55 -0800 | [diff] [blame] | 1138 | return status.getDetailsList().stream() |
| 1139 | .map(any -> { |
| 1140 | try { |
| 1141 | return any.unpack(P4RuntimeOuterClass.Error.class); |
| 1142 | } catch (InvalidProtocolBufferException e) { |
| 1143 | log.warn("Unable to unpack P4Runtime Error: {}", |
| 1144 | any.toString()); |
| 1145 | return null; |
| 1146 | } |
| 1147 | }) |
| 1148 | .filter(Objects::nonNull) |
| 1149 | .collect(Collectors.toList()); |
Carmelo Cascone | 5bc7e10 | 2018-02-18 18:27:55 -0800 | [diff] [blame] | 1150 | } |
| 1151 | |
| 1152 | private String parseP4Error(P4RuntimeOuterClass.Error err) { |
Carmelo Cascone | 943c664 | 2018-09-11 13:01:03 -0700 | [diff] [blame] | 1153 | return format("%s %s%s (%s:%d)", |
| 1154 | Status.fromCodeValue(err.getCanonicalCode()).getCode(), |
Carmelo Cascone | 5bc7e10 | 2018-02-18 18:27:55 -0800 | [diff] [blame] | 1155 | err.getMessage(), |
Carmelo Cascone | 943c664 | 2018-09-11 13:01:03 -0700 | [diff] [blame] | 1156 | err.hasDetails() ? ", " + err.getDetails().toString() : "", |
Carmelo Cascone | 5bc7e10 | 2018-02-18 18:27:55 -0800 | [diff] [blame] | 1157 | err.getSpace(), |
Carmelo Cascone | 943c664 | 2018-09-11 13:01:03 -0700 | [diff] [blame] | 1158 | err.getCode()); |
Carmelo Cascone | 5bc7e10 | 2018-02-18 18:27:55 -0800 | [diff] [blame] | 1159 | } |
| 1160 | |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 1161 | private void checkGrpcException(StatusRuntimeException ex) { |
| 1162 | switch (ex.getStatus().getCode()) { |
| 1163 | case OK: |
| 1164 | break; |
| 1165 | case CANCELLED: |
| 1166 | break; |
| 1167 | case UNKNOWN: |
| 1168 | break; |
| 1169 | case INVALID_ARGUMENT: |
| 1170 | break; |
| 1171 | case DEADLINE_EXCEEDED: |
| 1172 | break; |
| 1173 | case NOT_FOUND: |
| 1174 | break; |
| 1175 | case ALREADY_EXISTS: |
| 1176 | break; |
| 1177 | case PERMISSION_DENIED: |
| 1178 | // Notify upper layers that this node is not master. |
| 1179 | controller.postEvent(new P4RuntimeEvent( |
Carmelo Cascone | de3b684 | 2018-09-05 17:45:10 -0700 | [diff] [blame] | 1180 | P4RuntimeEvent.Type.PERMISSION_DENIED, |
| 1181 | new BaseP4RuntimeEventSubject(deviceId))); |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 1182 | break; |
| 1183 | case RESOURCE_EXHAUSTED: |
| 1184 | break; |
| 1185 | case FAILED_PRECONDITION: |
| 1186 | break; |
| 1187 | case ABORTED: |
| 1188 | break; |
| 1189 | case OUT_OF_RANGE: |
| 1190 | break; |
| 1191 | case UNIMPLEMENTED: |
| 1192 | break; |
| 1193 | case INTERNAL: |
| 1194 | break; |
| 1195 | case UNAVAILABLE: |
| 1196 | // Channel might be closed. |
| 1197 | controller.postEvent(new P4RuntimeEvent( |
| 1198 | P4RuntimeEvent.Type.CHANNEL_EVENT, |
| 1199 | new ChannelEvent(deviceId, ChannelEvent.Type.ERROR))); |
| 1200 | break; |
| 1201 | case DATA_LOSS: |
| 1202 | break; |
| 1203 | case UNAUTHENTICATED: |
| 1204 | break; |
| 1205 | default: |
| 1206 | break; |
| 1207 | } |
| 1208 | } |
| 1209 | |
| 1210 | private Uint128 bigIntegerToUint128(BigInteger value) { |
| 1211 | final byte[] arr = value.toByteArray(); |
| 1212 | final ByteBuffer bb = ByteBuffer.allocate(Long.BYTES * 2) |
| 1213 | .put(new byte[Long.BYTES * 2 - arr.length]) |
| 1214 | .put(arr); |
| 1215 | bb.rewind(); |
| 1216 | return Uint128.newBuilder() |
| 1217 | .setHigh(bb.getLong()) |
| 1218 | .setLow(bb.getLong()) |
| 1219 | .build(); |
| 1220 | } |
| 1221 | |
| 1222 | private BigInteger uint128ToBigInteger(Uint128 value) { |
| 1223 | return new BigInteger( |
| 1224 | ByteBuffer.allocate(Long.BYTES * 2) |
| 1225 | .putLong(value.getHigh()) |
| 1226 | .putLong(value.getLow()) |
| 1227 | .array()); |
| 1228 | } |
| 1229 | |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 1230 | /** |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 1231 | * A manager for the P4Runtime stream channel that opportunistically creates |
| 1232 | * new stream RCP stubs (e.g. when one fails because of errors) and posts |
| 1233 | * channel events via the P4Runtime controller. |
| 1234 | */ |
| 1235 | private final class StreamChannelManager { |
| 1236 | |
| 1237 | private final ManagedChannel channel; |
| 1238 | private final AtomicBoolean open; |
| 1239 | private final StreamObserver<StreamMessageResponse> responseObserver; |
| 1240 | private ClientCallStreamObserver<StreamMessageRequest> requestObserver; |
| 1241 | |
| 1242 | private StreamChannelManager(ManagedChannel channel) { |
| 1243 | this.channel = channel; |
| 1244 | this.responseObserver = new InternalStreamResponseObserver(this); |
| 1245 | this.open = new AtomicBoolean(false); |
| 1246 | } |
| 1247 | |
| 1248 | private void initIfRequired() { |
| 1249 | if (requestObserver == null) { |
| 1250 | log.debug("Creating new stream channel for {}...", deviceId); |
| 1251 | requestObserver = |
| 1252 | (ClientCallStreamObserver<StreamMessageRequest>) |
| 1253 | P4RuntimeGrpc.newStub(channel) |
| 1254 | .streamChannel(responseObserver); |
| 1255 | open.set(false); |
| 1256 | } |
| 1257 | } |
| 1258 | |
| 1259 | public boolean send(StreamMessageRequest value) { |
| 1260 | synchronized (this) { |
| 1261 | initIfRequired(); |
| 1262 | try { |
| 1263 | requestObserver.onNext(value); |
| 1264 | // FIXME |
| 1265 | // signalOpen(); |
| 1266 | return true; |
| 1267 | } catch (Throwable ex) { |
| 1268 | if (ex instanceof StatusRuntimeException) { |
| 1269 | log.warn("Unable to send {} to {}: {}", |
| 1270 | value.getUpdateCase().toString(), deviceId, ex.getMessage()); |
| 1271 | } else { |
| 1272 | log.warn(format( |
| 1273 | "Exception while sending %s to %s", |
| 1274 | value.getUpdateCase().toString(), deviceId), ex); |
| 1275 | } |
| 1276 | complete(); |
| 1277 | return false; |
| 1278 | } |
| 1279 | } |
| 1280 | } |
| 1281 | |
| 1282 | public void complete() { |
| 1283 | synchronized (this) { |
| 1284 | signalClosed(); |
| 1285 | if (requestObserver != null) { |
| 1286 | requestObserver.onCompleted(); |
| 1287 | requestObserver.cancel("Terminated", null); |
| 1288 | requestObserver = null; |
| 1289 | } |
| 1290 | } |
| 1291 | } |
| 1292 | |
| 1293 | void signalOpen() { |
| 1294 | synchronized (this) { |
| 1295 | final boolean wasOpen = open.getAndSet(true); |
| 1296 | if (!wasOpen) { |
| 1297 | controller.postEvent(new P4RuntimeEvent( |
| 1298 | P4RuntimeEvent.Type.CHANNEL_EVENT, |
| 1299 | new ChannelEvent(deviceId, ChannelEvent.Type.OPEN))); |
| 1300 | } |
| 1301 | } |
| 1302 | } |
| 1303 | |
| 1304 | void signalClosed() { |
| 1305 | synchronized (this) { |
| 1306 | final boolean wasOpen = open.getAndSet(false); |
| 1307 | if (wasOpen) { |
| 1308 | controller.postEvent(new P4RuntimeEvent( |
| 1309 | P4RuntimeEvent.Type.CHANNEL_EVENT, |
| 1310 | new ChannelEvent(deviceId, ChannelEvent.Type.CLOSED))); |
| 1311 | } |
| 1312 | } |
| 1313 | } |
| 1314 | |
| 1315 | public boolean isOpen() { |
| 1316 | return open.get(); |
| 1317 | } |
| 1318 | } |
| 1319 | |
| 1320 | /** |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 1321 | * Handles messages received from the device on the stream channel. |
| 1322 | */ |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 1323 | private final class InternalStreamResponseObserver |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 1324 | implements StreamObserver<StreamMessageResponse> { |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 1325 | |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 1326 | private final StreamChannelManager streamChannelManager; |
| 1327 | |
| 1328 | private InternalStreamResponseObserver( |
| 1329 | StreamChannelManager streamChannelManager) { |
| 1330 | this.streamChannelManager = streamChannelManager; |
| 1331 | } |
| 1332 | |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 1333 | @Override |
| 1334 | public void onNext(StreamMessageResponse message) { |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 1335 | streamChannelManager.signalOpen(); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 1336 | executorService.submit(() -> doNext(message)); |
| 1337 | } |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 1338 | |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 1339 | private void doNext(StreamMessageResponse message) { |
Carmelo Cascone | a966c34 | 2017-07-30 01:56:30 -0400 | [diff] [blame] | 1340 | try { |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 1341 | log.debug("Received message on stream channel from {}: {}", |
| 1342 | deviceId, message.getUpdateCase()); |
Carmelo Cascone | a966c34 | 2017-07-30 01:56:30 -0400 | [diff] [blame] | 1343 | switch (message.getUpdateCase()) { |
| 1344 | case PACKET: |
Carmelo Cascone | a966c34 | 2017-07-30 01:56:30 -0400 | [diff] [blame] | 1345 | doPacketIn(message.getPacket()); |
Andrea Campanella | 288b273 | 2017-07-28 14:16:16 +0200 | [diff] [blame] | 1346 | return; |
Carmelo Cascone | a966c34 | 2017-07-30 01:56:30 -0400 | [diff] [blame] | 1347 | case ARBITRATION: |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 1348 | doArbitrationResponse(message.getArbitration()); |
Carmelo Cascone | a966c34 | 2017-07-30 01:56:30 -0400 | [diff] [blame] | 1349 | return; |
| 1350 | default: |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 1351 | log.warn("Unrecognized stream message from {}: {}", |
| 1352 | deviceId, message.getUpdateCase()); |
Carmelo Cascone | a966c34 | 2017-07-30 01:56:30 -0400 | [diff] [blame] | 1353 | } |
| 1354 | } catch (Throwable ex) { |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 1355 | log.error("Exception while processing stream message from {}", |
| 1356 | deviceId, ex); |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 1357 | } |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 1358 | } |
| 1359 | |
| 1360 | @Override |
| 1361 | public void onError(Throwable throwable) { |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 1362 | if (throwable instanceof StatusRuntimeException) { |
| 1363 | StatusRuntimeException sre = (StatusRuntimeException) throwable; |
| 1364 | if (sre.getStatus().getCause() instanceof ConnectException) { |
| 1365 | log.warn("Device {} is unreachable ({})", |
| 1366 | deviceId, sre.getCause().getMessage()); |
| 1367 | } else { |
| 1368 | log.warn("Received error on stream channel for {}: {}", |
| 1369 | deviceId, throwable.getMessage()); |
| 1370 | } |
| 1371 | } else { |
| 1372 | log.warn(format("Received exception on stream channel for %s", |
| 1373 | deviceId), throwable); |
| 1374 | } |
| 1375 | streamChannelManager.complete(); |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 1376 | } |
| 1377 | |
| 1378 | @Override |
| 1379 | public void onCompleted() { |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 1380 | log.warn("Stream channel for {} has completed", deviceId); |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 1381 | streamChannelManager.complete(); |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 1382 | } |
| 1383 | } |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 1384 | } |