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