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 | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 23 | import com.google.protobuf.ByteString; |
Yi Tseng | e67e141 | 2018-01-31 17:35:20 -0800 | [diff] [blame] | 24 | import com.google.protobuf.InvalidProtocolBufferException; |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 25 | import io.grpc.Context; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 26 | import io.grpc.ManagedChannel; |
| 27 | import io.grpc.Status; |
| 28 | import io.grpc.StatusRuntimeException; |
| 29 | import io.grpc.stub.StreamObserver; |
Carmelo Cascone | 5bc7e10 | 2018-02-18 18:27:55 -0800 | [diff] [blame] | 30 | import org.apache.commons.lang3.tuple.ImmutablePair; |
Andrea Campanella | 288b273 | 2017-07-28 14:16:16 +0200 | [diff] [blame] | 31 | import org.onlab.osgi.DefaultServiceDirectory; |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 32 | import org.onlab.util.SharedExecutors; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 33 | import org.onlab.util.Tools; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 34 | import org.onosproject.net.DeviceId; |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 35 | import org.onosproject.net.pi.model.PiActionProfileId; |
| 36 | import org.onosproject.net.pi.model.PiCounterId; |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 37 | import org.onosproject.net.pi.model.PiMeterId; |
Andrea Campanella | 432f718 | 2017-07-14 18:43:27 +0200 | [diff] [blame] | 38 | import org.onosproject.net.pi.model.PiPipeconf; |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 39 | import org.onosproject.net.pi.model.PiTableId; |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 40 | import org.onosproject.net.pi.runtime.PiActionGroup; |
| 41 | import org.onosproject.net.pi.runtime.PiActionGroupMember; |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 42 | import org.onosproject.net.pi.runtime.PiCounterCellData; |
| 43 | import org.onosproject.net.pi.runtime.PiCounterCellId; |
Carmelo Cascone | 5bc7e10 | 2018-02-18 18:27:55 -0800 | [diff] [blame] | 44 | import org.onosproject.net.pi.runtime.PiEntity; |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 45 | import org.onosproject.net.pi.runtime.PiMeterCellConfig; |
| 46 | import org.onosproject.net.pi.runtime.PiMeterCellId; |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame^] | 47 | import org.onosproject.net.pi.runtime.PiMulticastGroupEntry; |
Andrea Campanella | 432f718 | 2017-07-14 18:43:27 +0200 | [diff] [blame] | 48 | import org.onosproject.net.pi.runtime.PiPacketOperation; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 49 | import org.onosproject.net.pi.runtime.PiTableEntry; |
Yi Tseng | e67e141 | 2018-01-31 17:35:20 -0800 | [diff] [blame] | 50 | import org.onosproject.net.pi.service.PiPipeconfService; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 51 | import org.onosproject.p4runtime.api.P4RuntimeClient; |
| 52 | import org.onosproject.p4runtime.api.P4RuntimeEvent; |
| 53 | import org.slf4j.Logger; |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 54 | import p4.config.v1.P4InfoOuterClass.P4Info; |
| 55 | import p4.tmp.P4Config; |
Carmelo Cascone | 6af4e17 | 2018-06-15 16:01:30 +0200 | [diff] [blame] | 56 | import p4.v1.P4RuntimeGrpc; |
| 57 | import p4.v1.P4RuntimeOuterClass; |
| 58 | import p4.v1.P4RuntimeOuterClass.ActionProfileGroup; |
| 59 | import p4.v1.P4RuntimeOuterClass.ActionProfileMember; |
| 60 | import p4.v1.P4RuntimeOuterClass.Entity; |
| 61 | import p4.v1.P4RuntimeOuterClass.ForwardingPipelineConfig; |
| 62 | import p4.v1.P4RuntimeOuterClass.MasterArbitrationUpdate; |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame^] | 63 | import p4.v1.P4RuntimeOuterClass.MulticastGroupEntry; |
| 64 | import p4.v1.P4RuntimeOuterClass.PacketReplicationEngineEntry; |
Carmelo Cascone | 6af4e17 | 2018-06-15 16:01:30 +0200 | [diff] [blame] | 65 | import p4.v1.P4RuntimeOuterClass.ReadRequest; |
| 66 | import p4.v1.P4RuntimeOuterClass.ReadResponse; |
| 67 | import p4.v1.P4RuntimeOuterClass.SetForwardingPipelineConfigRequest; |
| 68 | import p4.v1.P4RuntimeOuterClass.StreamMessageRequest; |
| 69 | import p4.v1.P4RuntimeOuterClass.StreamMessageResponse; |
| 70 | import p4.v1.P4RuntimeOuterClass.TableEntry; |
| 71 | import p4.v1.P4RuntimeOuterClass.Uint128; |
| 72 | import p4.v1.P4RuntimeOuterClass.Update; |
| 73 | import p4.v1.P4RuntimeOuterClass.WriteRequest; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 74 | |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 75 | import java.math.BigInteger; |
Carmelo Cascone | d61fdb3 | 2017-10-30 10:09:57 -0700 | [diff] [blame] | 76 | import java.nio.ByteBuffer; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 77 | import java.util.Collection; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 78 | import java.util.Collections; |
| 79 | import java.util.Iterator; |
| 80 | import java.util.List; |
| 81 | import java.util.Map; |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 82 | import java.util.Objects; |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 83 | import java.util.Set; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 84 | import java.util.concurrent.CompletableFuture; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 85 | import java.util.concurrent.Executor; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 86 | import java.util.concurrent.ExecutorService; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 87 | import java.util.concurrent.Executors; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 88 | import java.util.concurrent.TimeUnit; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 89 | import java.util.concurrent.locks.Lock; |
| 90 | import java.util.concurrent.locks.ReentrantLock; |
| 91 | import java.util.function.Supplier; |
| 92 | import java.util.stream.Collectors; |
| 93 | import java.util.stream.StreamSupport; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 94 | |
Carmelo Cascone | d61fdb3 | 2017-10-30 10:09:57 -0700 | [diff] [blame] | 95 | import static com.google.common.base.Preconditions.checkNotNull; |
Carmelo Cascone | 5bc7e10 | 2018-02-18 18:27:55 -0800 | [diff] [blame] | 96 | import static java.lang.String.format; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 97 | import static org.onlab.util.Tools.groupedThreads; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 98 | import static org.slf4j.LoggerFactory.getLogger; |
Carmelo Cascone | 6af4e17 | 2018-06-15 16:01:30 +0200 | [diff] [blame] | 99 | import static p4.v1.P4RuntimeOuterClass.Entity.EntityCase.ACTION_PROFILE_GROUP; |
| 100 | import static p4.v1.P4RuntimeOuterClass.Entity.EntityCase.ACTION_PROFILE_MEMBER; |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame^] | 101 | import static p4.v1.P4RuntimeOuterClass.Entity.EntityCase.PACKET_REPLICATION_ENGINE_ENTRY; |
Carmelo Cascone | 6af4e17 | 2018-06-15 16:01:30 +0200 | [diff] [blame] | 102 | import static p4.v1.P4RuntimeOuterClass.Entity.EntityCase.TABLE_ENTRY; |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 103 | import static p4.v1.P4RuntimeOuterClass.PacketIn; |
Carmelo Cascone | 6af4e17 | 2018-06-15 16:01:30 +0200 | [diff] [blame] | 104 | import static p4.v1.P4RuntimeOuterClass.PacketOut; |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame^] | 105 | import static p4.v1.P4RuntimeOuterClass.PacketReplicationEngineEntry.TypeCase.MULTICAST_GROUP_ENTRY; |
Carmelo Cascone | 6af4e17 | 2018-06-15 16:01:30 +0200 | [diff] [blame] | 106 | import static p4.v1.P4RuntimeOuterClass.SetForwardingPipelineConfigRequest.Action.VERIFY_AND_COMMIT; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 107 | |
| 108 | /** |
| 109 | * Implementation of a P4Runtime client. |
| 110 | */ |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 111 | final class P4RuntimeClientImpl implements P4RuntimeClient { |
| 112 | |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 113 | // Timeout in seconds to obtain the request lock. |
| 114 | private static final int LOCK_TIMEOUT = 60; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 115 | |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 116 | private static final Map<WriteOperationType, Update.Type> UPDATE_TYPES = ImmutableMap.of( |
| 117 | WriteOperationType.UNSPECIFIED, Update.Type.UNSPECIFIED, |
| 118 | WriteOperationType.INSERT, Update.Type.INSERT, |
| 119 | WriteOperationType.MODIFY, Update.Type.MODIFY, |
| 120 | WriteOperationType.DELETE, Update.Type.DELETE |
| 121 | ); |
| 122 | |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 123 | private final Logger log = getLogger(getClass()); |
| 124 | |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 125 | private final Lock requestLock = new ReentrantLock(); |
| 126 | private final Context.CancellableContext cancellableContext = |
| 127 | Context.current().withCancellation(); |
| 128 | |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 129 | private final DeviceId deviceId; |
Carmelo Cascone | f423bec | 2017-08-30 01:56:25 +0200 | [diff] [blame] | 130 | private final long p4DeviceId; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 131 | private final P4RuntimeControllerImpl controller; |
| 132 | private final P4RuntimeGrpc.P4RuntimeBlockingStub blockingStub; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 133 | private final ExecutorService executorService; |
| 134 | private final Executor contextExecutor; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 135 | private final StreamObserver<StreamMessageRequest> streamRequestObserver; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 136 | |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 137 | // Used by this client for write requests. |
| 138 | private Uint128 clientElectionId = Uint128.newBuilder().setLow(1).build(); |
Yi Tseng | 3e7f145 | 2017-10-20 10:31:53 -0700 | [diff] [blame] | 139 | |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 140 | /** |
| 141 | * Default constructor. |
| 142 | * |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 143 | * @param deviceId the ONOS device id |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 144 | * @param p4DeviceId the P4 device id |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 145 | * @param channel gRPC channel |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 146 | * @param controller runtime client controller |
| 147 | */ |
Carmelo Cascone | f423bec | 2017-08-30 01:56:25 +0200 | [diff] [blame] | 148 | P4RuntimeClientImpl(DeviceId deviceId, long p4DeviceId, ManagedChannel channel, |
| 149 | P4RuntimeControllerImpl controller) { |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 150 | this.deviceId = deviceId; |
| 151 | this.p4DeviceId = p4DeviceId; |
| 152 | this.controller = controller; |
Carmelo Cascone | a966c34 | 2017-07-30 01:56:30 -0400 | [diff] [blame] | 153 | this.executorService = Executors.newFixedThreadPool(15, groupedThreads( |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 154 | "onos-p4runtime-client-" + deviceId.toString(), "%d")); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 155 | this.contextExecutor = this.cancellableContext.fixedContextExecutor(executorService); |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 156 | //TODO Investigate use of stub deadlines instead of timeout in supplyInContext |
Carmelo Cascone | 2cad9ef | 2017-08-01 21:52:07 +0200 | [diff] [blame] | 157 | this.blockingStub = P4RuntimeGrpc.newBlockingStub(channel); |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 158 | this.streamRequestObserver = P4RuntimeGrpc.newStub(channel) |
| 159 | .streamChannel(new StreamChannelResponseObserver()); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | /** |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame^] | 163 | * Submits a task for async execution via the given executor. All tasks |
| 164 | * submitted with this method will be executed sequentially. |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 165 | */ |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 166 | private <U> CompletableFuture<U> supplyWithExecutor( |
| 167 | Supplier<U> supplier, String opDescription, Executor executor) { |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 168 | return CompletableFuture.supplyAsync(() -> { |
| 169 | // TODO: explore a more relaxed locking strategy. |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 170 | try { |
| 171 | if (!requestLock.tryLock(LOCK_TIMEOUT, TimeUnit.SECONDS)) { |
| 172 | log.error("LOCK TIMEOUT! This is likely a deadlock, " |
| 173 | + "please debug (executing {})", |
| 174 | opDescription); |
| 175 | throw new IllegalThreadStateException("Lock timeout"); |
| 176 | } |
| 177 | } catch (InterruptedException e) { |
| 178 | log.warn("Thread interrupted while waiting for lock (executing {})", |
| 179 | opDescription); |
Ray Milkey | dbd3821 | 2018-07-02 09:18:09 -0700 | [diff] [blame] | 180 | throw new IllegalStateException(e); |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 181 | } |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 182 | try { |
| 183 | return supplier.get(); |
Yi Tseng | e67e141 | 2018-01-31 17:35:20 -0800 | [diff] [blame] | 184 | } catch (StatusRuntimeException ex) { |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 185 | log.warn("Unable to execute {} on {}: {}", |
| 186 | opDescription, deviceId, ex.toString()); |
Yi Tseng | e67e141 | 2018-01-31 17:35:20 -0800 | [diff] [blame] | 187 | throw ex; |
Carmelo Cascone | a966c34 | 2017-07-30 01:56:30 -0400 | [diff] [blame] | 188 | } catch (Throwable ex) { |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 189 | log.error("Exception in client of {}, executing {}", |
| 190 | deviceId, opDescription, ex); |
Carmelo Cascone | a966c34 | 2017-07-30 01:56:30 -0400 | [diff] [blame] | 191 | throw ex; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 192 | } finally { |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 193 | requestLock.unlock(); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 194 | } |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 195 | }, executor); |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Equivalent of supplyWithExecutor using the gRPC context executor of this |
| 200 | * client, such that if the context is cancelled (e.g. client shutdown) the |
| 201 | * RPC is automatically cancelled. |
| 202 | */ |
| 203 | private <U> CompletableFuture<U> supplyInContext( |
| 204 | Supplier<U> supplier, String opDescription) { |
| 205 | return supplyWithExecutor(supplier, opDescription, contextExecutor); |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | @Override |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 209 | public CompletableFuture<Boolean> start() { |
| 210 | return supplyInContext(this::doInitStreamChannel, |
| 211 | "start-initStreamChannel"); |
| 212 | } |
| 213 | |
| 214 | @Override |
| 215 | public CompletableFuture<Void> shutdown() { |
| 216 | return supplyWithExecutor(this::doShutdown, "shutdown", |
| 217 | SharedExecutors.getPoolThreadExecutor()); |
| 218 | } |
| 219 | |
| 220 | @Override |
| 221 | public CompletableFuture<Boolean> becomeMaster() { |
| 222 | return supplyInContext(this::doBecomeMaster, |
| 223 | "becomeMaster"); |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 224 | } |
| 225 | |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 226 | @Override |
Carmelo Cascone | d61fdb3 | 2017-10-30 10:09:57 -0700 | [diff] [blame] | 227 | public CompletableFuture<Boolean> setPipelineConfig(PiPipeconf pipeconf, ByteBuffer deviceData) { |
| 228 | return supplyInContext(() -> doSetPipelineConfig(pipeconf, deviceData), "setPipelineConfig"); |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | @Override |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 232 | public CompletableFuture<Boolean> writeTableEntries(Collection<PiTableEntry> piTableEntries, |
| 233 | WriteOperationType opType, PiPipeconf pipeconf) { |
Carmelo Cascone | 2cad9ef | 2017-08-01 21:52:07 +0200 | [diff] [blame] | 234 | return supplyInContext(() -> doWriteTableEntries(piTableEntries, opType, pipeconf), |
| 235 | "writeTableEntries-" + opType.name()); |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | @Override |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 239 | public CompletableFuture<Collection<PiTableEntry>> dumpTable(PiTableId piTableId, PiPipeconf pipeconf) { |
Carmelo Cascone | 2cad9ef | 2017-08-01 21:52:07 +0200 | [diff] [blame] | 240 | return supplyInContext(() -> doDumpTable(piTableId, pipeconf), "dumpTable-" + piTableId); |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | @Override |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 244 | public CompletableFuture<Collection<PiTableEntry>> dumpAllTables(PiPipeconf pipeconf) { |
| 245 | return supplyInContext(() -> doDumpTable(null, pipeconf), "dumpAllTables"); |
| 246 | } |
| 247 | |
| 248 | @Override |
Andrea Campanella | 432f718 | 2017-07-14 18:43:27 +0200 | [diff] [blame] | 249 | public CompletableFuture<Boolean> packetOut(PiPacketOperation packet, PiPipeconf pipeconf) { |
Carmelo Cascone | 2cad9ef | 2017-08-01 21:52:07 +0200 | [diff] [blame] | 250 | return supplyInContext(() -> doPacketOut(packet, pipeconf), "packetOut"); |
Andrea Campanella | 432f718 | 2017-07-14 18:43:27 +0200 | [diff] [blame] | 251 | } |
| 252 | |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 253 | @Override |
| 254 | public CompletableFuture<Collection<PiCounterCellData>> readCounterCells(Set<PiCounterCellId> cellIds, |
| 255 | PiPipeconf pipeconf) { |
| 256 | return supplyInContext(() -> doReadCounterCells(cellIds, pipeconf), |
| 257 | "readCounterCells-" + cellIds.hashCode()); |
| 258 | } |
| 259 | |
| 260 | @Override |
| 261 | public CompletableFuture<Collection<PiCounterCellData>> readAllCounterCells(Set<PiCounterId> counterIds, |
| 262 | PiPipeconf pipeconf) { |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 263 | return supplyInContext(() -> doReadAllCounterCells(counterIds, pipeconf), |
| 264 | "readAllCounterCells-" + counterIds.hashCode()); |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 265 | } |
| 266 | |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 267 | @Override |
Yi Tseng | 8d35513 | 2018-04-13 01:40:48 +0800 | [diff] [blame] | 268 | public CompletableFuture<Boolean> writeActionGroupMembers(PiActionProfileId profileId, |
| 269 | Collection<PiActionGroupMember> members, |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 270 | WriteOperationType opType, |
| 271 | PiPipeconf pipeconf) { |
Yi Tseng | 8d35513 | 2018-04-13 01:40:48 +0800 | [diff] [blame] | 272 | return supplyInContext(() -> doWriteActionGroupMembers(profileId, members, opType, pipeconf), |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 273 | "writeActionGroupMembers-" + opType.name()); |
| 274 | } |
| 275 | |
Yi Tseng | 8d35513 | 2018-04-13 01:40:48 +0800 | [diff] [blame] | 276 | |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 277 | @Override |
| 278 | public CompletableFuture<Boolean> writeActionGroup(PiActionGroup group, |
| 279 | WriteOperationType opType, |
| 280 | PiPipeconf pipeconf) { |
| 281 | return supplyInContext(() -> doWriteActionGroup(group, opType, pipeconf), |
| 282 | "writeActionGroup-" + opType.name()); |
| 283 | } |
| 284 | |
| 285 | @Override |
| 286 | public CompletableFuture<Collection<PiActionGroup>> dumpGroups(PiActionProfileId actionProfileId, |
| 287 | PiPipeconf pipeconf) { |
| 288 | return supplyInContext(() -> doDumpGroups(actionProfileId, pipeconf), |
| 289 | "dumpGroups-" + actionProfileId.id()); |
| 290 | } |
| 291 | |
Yi Tseng | 3e7f145 | 2017-10-20 10:31:53 -0700 | [diff] [blame] | 292 | @Override |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 293 | public CompletableFuture<Boolean> writeMeterCells(Collection<PiMeterCellConfig> cellIds, PiPipeconf pipeconf) { |
| 294 | |
| 295 | return supplyInContext(() -> doWriteMeterCells(cellIds, pipeconf), |
| 296 | "writeMeterCells"); |
| 297 | } |
| 298 | |
| 299 | @Override |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame^] | 300 | public CompletableFuture<Boolean> writePreMulticastGroupEntries( |
| 301 | Collection<PiMulticastGroupEntry> entries, |
| 302 | WriteOperationType opType) { |
| 303 | return supplyInContext(() -> doWriteMulticastGroupEntries(entries, opType), |
| 304 | "writePreMulticastGroupEntries"); |
| 305 | } |
| 306 | |
| 307 | @Override |
| 308 | public CompletableFuture<Collection<PiMulticastGroupEntry>> readAllMulticastGroupEntries() { |
| 309 | return supplyInContext(this::doReadAllMulticastGroupEntries, |
| 310 | "readAllMulticastGroupEntries"); |
| 311 | } |
| 312 | |
| 313 | @Override |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 314 | public CompletableFuture<Collection<PiMeterCellConfig>> readMeterCells(Set<PiMeterCellId> cellIds, |
| 315 | PiPipeconf pipeconf) { |
| 316 | return supplyInContext(() -> doReadMeterCells(cellIds, pipeconf), |
| 317 | "readMeterCells-" + cellIds.hashCode()); |
| 318 | } |
| 319 | |
| 320 | @Override |
| 321 | public CompletableFuture<Collection<PiMeterCellConfig>> readAllMeterCells(Set<PiMeterId> meterIds, |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame^] | 322 | PiPipeconf pipeconf) { |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 323 | return supplyInContext(() -> doReadAllMeterCells(meterIds, pipeconf), |
| 324 | "readAllMeterCells-" + meterIds.hashCode()); |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 325 | } |
Yi Tseng | 3e7f145 | 2017-10-20 10:31:53 -0700 | [diff] [blame] | 326 | |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 327 | /* Blocking method implementations below */ |
| 328 | |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 329 | private boolean doBecomeMaster() { |
| 330 | final Uint128 newId = bigIntegerToUint128( |
| 331 | controller.newMasterElectionId(deviceId)); |
| 332 | if (sendMasterArbitrationUpdate(newId)) { |
| 333 | clientElectionId = newId; |
| 334 | return true; |
| 335 | } |
| 336 | return false; |
| 337 | } |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame] | 338 | |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 339 | private boolean sendMasterArbitrationUpdate(Uint128 electionId) { |
| 340 | log.info("Sending arbitration update to {}... electionId={}", |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame^] | 341 | deviceId, uint128ToBigInteger(electionId)); |
Yi Tseng | 3e7f145 | 2017-10-20 10:31:53 -0700 | [diff] [blame] | 342 | try { |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 343 | streamRequestObserver.onNext( |
| 344 | StreamMessageRequest.newBuilder() |
| 345 | .setArbitration( |
| 346 | MasterArbitrationUpdate |
| 347 | .newBuilder() |
| 348 | .setDeviceId(p4DeviceId) |
| 349 | .setElectionId(electionId) |
| 350 | .build()) |
| 351 | .build()); |
| 352 | return true; |
Yi Tseng | e67e141 | 2018-01-31 17:35:20 -0800 | [diff] [blame] | 353 | } catch (StatusRuntimeException e) { |
Carmelo Cascone | 5bc7e10 | 2018-02-18 18:27:55 -0800 | [diff] [blame] | 354 | log.error("Unable to perform arbitration update on {}: {}", deviceId, e.getMessage()); |
Yi Tseng | 3e7f145 | 2017-10-20 10:31:53 -0700 | [diff] [blame] | 355 | } |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 356 | return false; |
Yi Tseng | 3e7f145 | 2017-10-20 10:31:53 -0700 | [diff] [blame] | 357 | } |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 358 | |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 359 | private boolean doInitStreamChannel() { |
| 360 | // To listen for packets and other events, we need to start the RPC. |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 361 | // Here we send an empty StreamMessageRequest. |
| 362 | try { |
| 363 | log.info("Starting stream channel with {}...", deviceId); |
| 364 | streamRequestObserver.onNext(StreamMessageRequest.newBuilder().build()); |
| 365 | return true; |
| 366 | } catch (StatusRuntimeException e) { |
| 367 | log.error("Unable to start stream channel with {}: {}", |
| 368 | deviceId, e.getMessage()); |
| 369 | return false; |
| 370 | } |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 371 | } |
| 372 | |
Carmelo Cascone | d61fdb3 | 2017-10-30 10:09:57 -0700 | [diff] [blame] | 373 | private boolean doSetPipelineConfig(PiPipeconf pipeconf, ByteBuffer deviceData) { |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 374 | |
Carmelo Cascone | d61fdb3 | 2017-10-30 10:09:57 -0700 | [diff] [blame] | 375 | log.info("Setting pipeline config for {} to {}...", deviceId, pipeconf.id()); |
| 376 | |
| 377 | checkNotNull(deviceData, "deviceData cannot be null"); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 378 | |
| 379 | P4Info p4Info = PipeconfHelper.getP4Info(pipeconf); |
| 380 | if (p4Info == null) { |
| 381 | // Problem logged by PipeconfHelper. |
| 382 | return false; |
| 383 | } |
| 384 | |
Carmelo Cascone | d61fdb3 | 2017-10-30 10:09:57 -0700 | [diff] [blame] | 385 | P4Config.P4DeviceConfig p4DeviceConfigMsg = P4Config.P4DeviceConfig |
| 386 | .newBuilder() |
| 387 | .setExtras(P4Config.P4DeviceConfig.Extras.getDefaultInstance()) |
| 388 | .setReassign(true) |
| 389 | .setDeviceData(ByteString.copyFrom(deviceData)) |
| 390 | .build(); |
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 | ForwardingPipelineConfig pipelineConfig = ForwardingPipelineConfig |
Andrea Campanella | 0288c87 | 2017-08-07 18:32:51 +0200 | [diff] [blame] | 393 | .newBuilder() |
Carmelo Cascone | d61fdb3 | 2017-10-30 10:09:57 -0700 | [diff] [blame] | 394 | .setP4Info(p4Info) |
| 395 | .setP4DeviceConfig(p4DeviceConfigMsg.toByteString()) |
| 396 | .build(); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 397 | |
| 398 | SetForwardingPipelineConfigRequest request = SetForwardingPipelineConfigRequest |
| 399 | .newBuilder() |
Andrea Campanella | 8bcd586 | 2017-12-11 11:34:45 +0100 | [diff] [blame] | 400 | .setDeviceId(p4DeviceId) |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 401 | .setElectionId(clientElectionId) |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 402 | .setAction(VERIFY_AND_COMMIT) |
Andrea Campanella | 8bcd586 | 2017-12-11 11:34:45 +0100 | [diff] [blame] | 403 | .setConfig(pipelineConfig) |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 404 | .build(); |
| 405 | |
| 406 | try { |
| 407 | this.blockingStub.setForwardingPipelineConfig(request); |
Carmelo Cascone | d61fdb3 | 2017-10-30 10:09:57 -0700 | [diff] [blame] | 408 | return true; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 409 | } catch (StatusRuntimeException ex) { |
Carmelo Cascone | 5bc7e10 | 2018-02-18 18:27:55 -0800 | [diff] [blame] | 410 | log.warn("Unable to set pipeline config on {}: {}", deviceId, ex.getMessage()); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 411 | return false; |
| 412 | } |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | private boolean doWriteTableEntries(Collection<PiTableEntry> piTableEntries, WriteOperationType opType, |
| 416 | PiPipeconf pipeconf) { |
Carmelo Cascone | 5bc7e10 | 2018-02-18 18:27:55 -0800 | [diff] [blame] | 417 | if (piTableEntries.size() == 0) { |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 418 | return true; |
| 419 | } |
| 420 | |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 421 | Collection<Update> updateMsgs; |
Carmelo Cascone | 5bc7e10 | 2018-02-18 18:27:55 -0800 | [diff] [blame] | 422 | try { |
| 423 | updateMsgs = TableEntryEncoder.encode(piTableEntries, pipeconf) |
| 424 | .stream() |
| 425 | .map(tableEntryMsg -> |
| 426 | Update.newBuilder() |
| 427 | .setEntity(Entity.newBuilder() |
| 428 | .setTableEntry(tableEntryMsg) |
| 429 | .build()) |
| 430 | .setType(UPDATE_TYPES.get(opType)) |
| 431 | .build()) |
| 432 | .collect(Collectors.toList()); |
| 433 | } catch (EncodeException e) { |
| 434 | log.error("Unable to encode table entries, aborting {} operation: {}", |
| 435 | opType.name(), e.getMessage()); |
| 436 | return false; |
| 437 | } |
| 438 | |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame^] | 439 | return write(updateMsgs, piTableEntries, opType, "table entry"); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | private Collection<PiTableEntry> doDumpTable(PiTableId piTableId, PiPipeconf pipeconf) { |
| 443 | |
Carmelo Cascone | 9f00770 | 2017-08-24 13:30:51 +0200 | [diff] [blame] | 444 | log.debug("Dumping table {} from {} (pipeconf {})...", piTableId, deviceId, pipeconf.id()); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 445 | |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 446 | int tableId; |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 447 | if (piTableId == null) { |
| 448 | // Dump all tables. |
| 449 | tableId = 0; |
| 450 | } else { |
| 451 | P4InfoBrowser browser = PipeconfHelper.getP4InfoBrowser(pipeconf); |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame^] | 452 | if (browser == null) { |
| 453 | log.warn("Unable to get a P4Info browser for pipeconf {}", pipeconf); |
| 454 | return Collections.emptyList(); |
| 455 | } |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 456 | try { |
| 457 | tableId = browser.tables().getByName(piTableId.id()).getPreamble().getId(); |
| 458 | } catch (P4InfoBrowser.NotFoundException e) { |
| 459 | log.warn("Unable to dump table: {}", e.getMessage()); |
| 460 | return Collections.emptyList(); |
| 461 | } |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | ReadRequest requestMsg = ReadRequest.newBuilder() |
| 465 | .setDeviceId(p4DeviceId) |
| 466 | .addEntities(Entity.newBuilder() |
| 467 | .setTableEntry(TableEntry.newBuilder() |
| 468 | .setTableId(tableId) |
| 469 | .build()) |
| 470 | .build()) |
| 471 | .build(); |
| 472 | |
| 473 | Iterator<ReadResponse> responses; |
| 474 | try { |
| 475 | responses = blockingStub.read(requestMsg); |
| 476 | } catch (StatusRuntimeException e) { |
Carmelo Cascone | 5bc7e10 | 2018-02-18 18:27:55 -0800 | [diff] [blame] | 477 | log.warn("Unable to dump table {} from {}: {}", piTableId, deviceId, e.getMessage()); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 478 | return Collections.emptyList(); |
| 479 | } |
| 480 | |
| 481 | Iterable<ReadResponse> responseIterable = () -> responses; |
| 482 | List<TableEntry> tableEntryMsgs = StreamSupport |
| 483 | .stream(responseIterable.spliterator(), false) |
| 484 | .map(ReadResponse::getEntitiesList) |
| 485 | .flatMap(List::stream) |
| 486 | .filter(entity -> entity.getEntityCase() == TABLE_ENTRY) |
| 487 | .map(Entity::getTableEntry) |
| 488 | .collect(Collectors.toList()); |
| 489 | |
Carmelo Cascone | 9f00770 | 2017-08-24 13:30:51 +0200 | [diff] [blame] | 490 | log.debug("Retrieved {} entries from table {} on {}...", tableEntryMsgs.size(), piTableId, deviceId); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 491 | |
| 492 | return TableEntryEncoder.decode(tableEntryMsgs, pipeconf); |
| 493 | } |
| 494 | |
Andrea Campanella | fc1d34c | 2017-07-18 17:01:41 +0200 | [diff] [blame] | 495 | private boolean doPacketOut(PiPacketOperation packet, PiPipeconf pipeconf) { |
| 496 | try { |
| 497 | //encode the PiPacketOperation into a PacketOut |
| 498 | PacketOut packetOut = PacketIOCodec.encodePacketOut(packet, pipeconf); |
| 499 | |
| 500 | //Build the request |
| 501 | StreamMessageRequest packetOutRequest = StreamMessageRequest |
| 502 | .newBuilder().setPacket(packetOut).build(); |
| 503 | |
| 504 | //Send the request |
| 505 | streamRequestObserver.onNext(packetOutRequest); |
| 506 | |
| 507 | } catch (P4InfoBrowser.NotFoundException e) { |
| 508 | log.error("Cant find expected metadata in p4Info file. {}", e.getMessage()); |
| 509 | log.debug("Exception", e); |
| 510 | return false; |
| 511 | } |
| 512 | return true; |
| 513 | } |
| 514 | |
Carmelo Cascone | a966c34 | 2017-07-30 01:56:30 -0400 | [diff] [blame] | 515 | private void doPacketIn(PacketIn packetInMsg) { |
| 516 | |
| 517 | // Retrieve the pipeconf for this client's device. |
| 518 | PiPipeconfService pipeconfService = DefaultServiceDirectory.getService(PiPipeconfService.class); |
| 519 | if (pipeconfService == null) { |
| 520 | throw new IllegalStateException("PiPipeconfService is null. Can't handle packet in."); |
| 521 | } |
| 522 | final PiPipeconf pipeconf; |
| 523 | if (pipeconfService.ofDevice(deviceId).isPresent() && |
| 524 | pipeconfService.getPipeconf(pipeconfService.ofDevice(deviceId).get()).isPresent()) { |
| 525 | pipeconf = pipeconfService.getPipeconf(pipeconfService.ofDevice(deviceId).get()).get(); |
| 526 | } else { |
| 527 | log.warn("Unable to get pipeconf of {}. Can't handle packet in", deviceId); |
| 528 | return; |
| 529 | } |
| 530 | // Decode packet message and post event. |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 531 | PiPacketOperation packetOperation = PacketIOCodec.decodePacketIn(packetInMsg, pipeconf, deviceId); |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 532 | PacketInEvent packetInEventSubject = new PacketInEvent(deviceId, packetOperation); |
Carmelo Cascone | 2cad9ef | 2017-08-01 21:52:07 +0200 | [diff] [blame] | 533 | P4RuntimeEvent event = new P4RuntimeEvent(P4RuntimeEvent.Type.PACKET_IN, packetInEventSubject); |
Carmelo Cascone | a966c34 | 2017-07-30 01:56:30 -0400 | [diff] [blame] | 534 | log.debug("Received packet in: {}", event); |
| 535 | controller.postEvent(event); |
| 536 | } |
| 537 | |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 538 | private void doArbitrationResponse(MasterArbitrationUpdate msg) { |
| 539 | // From the spec... |
| 540 | // - Election_id: The stream RPC with the highest election_id is the |
| 541 | // master. Switch populates with the highest election ID it |
| 542 | // has received from all connected controllers. |
| 543 | // - Status: Switch populates this with OK for the client that is the |
| 544 | // master, and with an error status for all other connected clients (at |
| 545 | // every mastership change). |
| 546 | if (!msg.hasElectionId() || !msg.hasStatus()) { |
Yi Tseng | 3e7f145 | 2017-10-20 10:31:53 -0700 | [diff] [blame] | 547 | return; |
| 548 | } |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 549 | final boolean isMaster = msg.getStatus().getCode() == Status.OK.getCode().value(); |
| 550 | log.info("Received arbitration update from {}: isMaster={}, electionId={}", |
| 551 | deviceId, isMaster, uint128ToBigInteger(msg.getElectionId())); |
| 552 | controller.postEvent(new P4RuntimeEvent( |
| 553 | P4RuntimeEvent.Type.ARBITRATION_RESPONSE, |
| 554 | new ArbitrationResponse(deviceId, isMaster))); |
Carmelo Cascone | a966c34 | 2017-07-30 01:56:30 -0400 | [diff] [blame] | 555 | } |
| 556 | |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 557 | private Collection<PiCounterCellData> doReadAllCounterCells( |
| 558 | Collection<PiCounterId> counterIds, PiPipeconf pipeconf) { |
| 559 | return doReadCounterEntities( |
| 560 | CounterEntryCodec.readAllCellsEntities(counterIds, pipeconf), |
| 561 | pipeconf); |
| 562 | } |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 563 | |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 564 | private Collection<PiCounterCellData> doReadCounterCells( |
| 565 | Collection<PiCounterCellId> cellIds, PiPipeconf pipeconf) { |
| 566 | return doReadCounterEntities( |
| 567 | CounterEntryCodec.encodePiCounterCellIds(cellIds, pipeconf), |
| 568 | pipeconf); |
| 569 | } |
| 570 | |
| 571 | private Collection<PiCounterCellData> doReadCounterEntities( |
| 572 | Collection<Entity> counterEntities, PiPipeconf pipeconf) { |
| 573 | |
| 574 | if (counterEntities.size() == 0) { |
| 575 | return Collections.emptyList(); |
| 576 | } |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 577 | |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 578 | final ReadRequest request = ReadRequest.newBuilder() |
| 579 | .setDeviceId(p4DeviceId) |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 580 | .addAllEntities(counterEntities) |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 581 | .build(); |
| 582 | |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 583 | final Iterable<ReadResponse> responses; |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 584 | try { |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 585 | responses = () -> blockingStub.read(request); |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 586 | } catch (StatusRuntimeException e) { |
Carmelo Cascone | 5bc7e10 | 2018-02-18 18:27:55 -0800 | [diff] [blame] | 587 | log.warn("Unable to read counter cells from {}: {}", deviceId, e.getMessage()); |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 588 | return Collections.emptyList(); |
| 589 | } |
| 590 | |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 591 | List<Entity> entities = StreamSupport.stream(responses.spliterator(), false) |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 592 | .map(ReadResponse::getEntitiesList) |
| 593 | .flatMap(List::stream) |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 594 | .collect(Collectors.toList()); |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 595 | |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 596 | return CounterEntryCodec.decodeCounterEntities(entities, pipeconf); |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 597 | } |
| 598 | |
Yi Tseng | 8d35513 | 2018-04-13 01:40:48 +0800 | [diff] [blame] | 599 | private boolean doWriteActionGroupMembers(PiActionProfileId profileId, Collection<PiActionGroupMember> members, |
| 600 | WriteOperationType opType, PiPipeconf pipeconf) { |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 601 | final Collection<ActionProfileMember> actionProfileMembers = Lists.newArrayList(); |
Carmelo Cascone | 5bc7e10 | 2018-02-18 18:27:55 -0800 | [diff] [blame] | 602 | |
Yi Tseng | 8d35513 | 2018-04-13 01:40:48 +0800 | [diff] [blame] | 603 | for (PiActionGroupMember member : members) { |
Carmelo Cascone | 5bc7e10 | 2018-02-18 18:27:55 -0800 | [diff] [blame] | 604 | try { |
Yi Tseng | 8d35513 | 2018-04-13 01:40:48 +0800 | [diff] [blame] | 605 | actionProfileMembers.add(ActionProfileMemberEncoder.encode(profileId, member, pipeconf)); |
Carmelo Cascone | 5bc7e10 | 2018-02-18 18:27:55 -0800 | [diff] [blame] | 606 | } catch (EncodeException | P4InfoBrowser.NotFoundException e) { |
| 607 | log.warn("Unable to encode group member, aborting {} operation: {} [{}]", |
| 608 | opType.name(), e.getMessage(), member.toString()); |
| 609 | return false; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 610 | } |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 611 | } |
| 612 | |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 613 | final Collection<Update> updateMsgs = actionProfileMembers.stream() |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 614 | .map(actionProfileMember -> |
| 615 | Update.newBuilder() |
| 616 | .setEntity(Entity.newBuilder() |
| 617 | .setActionProfileMember(actionProfileMember) |
| 618 | .build()) |
| 619 | .setType(UPDATE_TYPES.get(opType)) |
| 620 | .build()) |
| 621 | .collect(Collectors.toList()); |
| 622 | |
| 623 | if (updateMsgs.size() == 0) { |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 624 | // Nothing to update. |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 625 | return true; |
| 626 | } |
| 627 | |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame^] | 628 | return write(updateMsgs, members, opType, "group member"); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | private Collection<PiActionGroup> doDumpGroups(PiActionProfileId piActionProfileId, PiPipeconf pipeconf) { |
| 632 | log.debug("Dumping groups from action profile {} from {} (pipeconf {})...", |
| 633 | piActionProfileId.id(), deviceId, pipeconf.id()); |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 634 | |
| 635 | final P4InfoBrowser browser = PipeconfHelper.getP4InfoBrowser(pipeconf); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 636 | if (browser == null) { |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 637 | log.warn("Unable to get a P4Info browser for pipeconf {}, aborting dump action profile", pipeconf); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 638 | return Collections.emptySet(); |
| 639 | } |
| 640 | |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 641 | final int actionProfileId; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 642 | try { |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 643 | actionProfileId = browser |
| 644 | .actionProfiles() |
Carmelo Cascone | cb0a49c | 2017-10-03 14:32:23 +0200 | [diff] [blame] | 645 | .getByName(piActionProfileId.id()) |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 646 | .getPreamble() |
| 647 | .getId(); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 648 | } catch (P4InfoBrowser.NotFoundException e) { |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 649 | log.warn("Unable to dump groups: {}", e.getMessage()); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 650 | return Collections.emptySet(); |
| 651 | } |
| 652 | |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 653 | // Prepare read request to read all groups from the given action profile. |
| 654 | final ReadRequest groupRequestMsg = ReadRequest.newBuilder() |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 655 | .setDeviceId(p4DeviceId) |
| 656 | .addEntities(Entity.newBuilder() |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 657 | .setActionProfileGroup( |
| 658 | ActionProfileGroup.newBuilder() |
| 659 | .setActionProfileId(actionProfileId) |
| 660 | .build()) |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 661 | .build()) |
| 662 | .build(); |
| 663 | |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 664 | // Read groups. |
| 665 | final Iterator<ReadResponse> groupResponses; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 666 | try { |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 667 | groupResponses = blockingStub.read(groupRequestMsg); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 668 | } catch (StatusRuntimeException e) { |
Carmelo Cascone | 5bc7e10 | 2018-02-18 18:27:55 -0800 | [diff] [blame] | 669 | log.warn("Unable to dump action profile {} from {}: {}", piActionProfileId, deviceId, e.getMessage()); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 670 | return Collections.emptySet(); |
| 671 | } |
| 672 | |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 673 | final List<ActionProfileGroup> groupMsgs = Tools.stream(() -> groupResponses) |
| 674 | .map(ReadResponse::getEntitiesList) |
| 675 | .flatMap(List::stream) |
| 676 | .filter(entity -> entity.getEntityCase() == ACTION_PROFILE_GROUP) |
| 677 | .map(Entity::getActionProfileGroup) |
| 678 | .collect(Collectors.toList()); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 679 | |
| 680 | log.debug("Retrieved {} groups from action profile {} on {}...", |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 681 | groupMsgs.size(), piActionProfileId.id(), deviceId); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 682 | |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 683 | // Returned groups contain only a minimal description of their members. |
| 684 | // 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] | 685 | |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 686 | // Keep a map of all member IDs for each group ID, will need it later. |
| 687 | final Multimap<Integer, Integer> groupIdToMemberIdsMap = HashMultimap.create(); |
| 688 | groupMsgs.forEach(g -> groupIdToMemberIdsMap.putAll( |
| 689 | g.getGroupId(), |
| 690 | g.getMembersList().stream() |
| 691 | .map(ActionProfileGroup.Member::getMemberId) |
| 692 | .collect(Collectors.toList()))); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 693 | |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 694 | // Prepare one big read request to read all members in one shot. |
| 695 | final Set<Entity> entityMsgs = groupMsgs.stream() |
| 696 | .flatMap(g -> g.getMembersList().stream()) |
| 697 | .map(ActionProfileGroup.Member::getMemberId) |
| 698 | // Prevent issuing many read requests for the same member. |
| 699 | .distinct() |
| 700 | .map(id -> ActionProfileMember.newBuilder() |
| 701 | .setActionProfileId(actionProfileId) |
| 702 | .setMemberId(id) |
| 703 | .build()) |
| 704 | .map(m -> Entity.newBuilder() |
| 705 | .setActionProfileMember(m) |
| 706 | .build()) |
| 707 | .collect(Collectors.toSet()); |
| 708 | final ReadRequest memberRequestMsg = ReadRequest.newBuilder().setDeviceId(p4DeviceId) |
| 709 | .addAllEntities(entityMsgs) |
| 710 | .build(); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 711 | |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 712 | // Read members. |
| 713 | final Iterator<ReadResponse> memberResponses; |
| 714 | try { |
| 715 | memberResponses = blockingStub.read(memberRequestMsg); |
| 716 | } catch (StatusRuntimeException e) { |
Carmelo Cascone | 5bc7e10 | 2018-02-18 18:27:55 -0800 | [diff] [blame] | 717 | log.warn("Unable to read members of action profile {} from {}: {}", |
| 718 | piActionProfileId, deviceId, e.getMessage()); |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 719 | return Collections.emptyList(); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 720 | } |
| 721 | |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 722 | final Multimap<Integer, ActionProfileMember> groupIdToMembersMap = HashMultimap.create(); |
| 723 | Tools.stream(() -> memberResponses) |
| 724 | .map(ReadResponse::getEntitiesList) |
| 725 | .flatMap(List::stream) |
| 726 | .filter(e -> e.getEntityCase() == ACTION_PROFILE_MEMBER) |
| 727 | .map(Entity::getActionProfileMember) |
| 728 | .forEach(member -> groupIdToMemberIdsMap.asMap() |
| 729 | // Get all group IDs that contain this member. |
| 730 | .entrySet() |
| 731 | .stream() |
| 732 | .filter(entry -> entry.getValue().contains(member.getMemberId())) |
| 733 | .map(Map.Entry::getKey) |
| 734 | .forEach(gid -> groupIdToMembersMap.put(gid, member))); |
| 735 | |
| 736 | log.debug("Retrieved {} group members from action profile {} on {}...", |
| 737 | groupIdToMembersMap.size(), piActionProfileId.id(), deviceId); |
| 738 | |
| 739 | return groupMsgs.stream() |
| 740 | .map(groupMsg -> { |
| 741 | try { |
| 742 | return ActionProfileGroupEncoder.decode(groupMsg, |
| 743 | groupIdToMembersMap.get(groupMsg.getGroupId()), |
| 744 | pipeconf); |
| 745 | } catch (P4InfoBrowser.NotFoundException | EncodeException e) { |
| 746 | log.warn("Unable to decode group: {}\n {}", e.getMessage(), groupMsg); |
| 747 | return null; |
| 748 | } |
| 749 | }) |
| 750 | .filter(Objects::nonNull) |
| 751 | .collect(Collectors.toList()); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | private boolean doWriteActionGroup(PiActionGroup group, WriteOperationType opType, PiPipeconf pipeconf) { |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 755 | final ActionProfileGroup actionProfileGroup; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 756 | try { |
| 757 | actionProfileGroup = ActionProfileGroupEncoder.encode(group, pipeconf); |
| 758 | } catch (EncodeException | P4InfoBrowser.NotFoundException e) { |
Carmelo Cascone | 5bc7e10 | 2018-02-18 18:27:55 -0800 | [diff] [blame] | 759 | log.warn("Unable to encode group, aborting {} operation: {}", e.getMessage(), opType.name()); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 760 | return false; |
| 761 | } |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 762 | |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame^] | 763 | final Update updateMsg = Update.newBuilder() |
| 764 | .setEntity(Entity.newBuilder() |
| 765 | .setActionProfileGroup(actionProfileGroup) |
| 766 | .build()) |
| 767 | .setType(UPDATE_TYPES.get(opType)) |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 768 | .build(); |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame^] | 769 | |
| 770 | return write(Collections.singleton(updateMsg), Collections.singleton(group), |
| 771 | opType, "group"); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 772 | } |
| 773 | |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 774 | private Collection<PiMeterCellConfig> doReadAllMeterCells( |
| 775 | Collection<PiMeterId> meterIds, PiPipeconf pipeconf) { |
| 776 | return doReadMeterEntities(MeterEntryCodec.readAllCellsEntities( |
| 777 | meterIds, pipeconf), pipeconf); |
| 778 | } |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 779 | |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 780 | private Collection<PiMeterCellConfig> doReadMeterCells( |
| 781 | Collection<PiMeterCellId> cellIds, PiPipeconf pipeconf) { |
| 782 | |
| 783 | final Collection<PiMeterCellConfig> piMeterCellConfigs = cellIds.stream() |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 784 | .map(cellId -> PiMeterCellConfig.builder() |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 785 | .withMeterCellId(cellId) |
| 786 | .build()) |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 787 | .collect(Collectors.toList()); |
| 788 | |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 789 | return doReadMeterEntities(MeterEntryCodec.encodePiMeterCellConfigs( |
| 790 | piMeterCellConfigs, pipeconf), pipeconf); |
| 791 | } |
| 792 | |
| 793 | private Collection<PiMeterCellConfig> doReadMeterEntities( |
| 794 | Collection<Entity> entitiesToRead, PiPipeconf pipeconf) { |
| 795 | |
| 796 | if (entitiesToRead.size() == 0) { |
| 797 | return Collections.emptyList(); |
| 798 | } |
| 799 | |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 800 | final ReadRequest request = ReadRequest.newBuilder() |
| 801 | .setDeviceId(p4DeviceId) |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 802 | .addAllEntities(entitiesToRead) |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 803 | .build(); |
| 804 | |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 805 | final Iterable<ReadResponse> responses; |
| 806 | try { |
| 807 | responses = () -> blockingStub.read(request); |
| 808 | } catch (StatusRuntimeException e) { |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 809 | log.warn("Unable to read meter cells: {}", e.getMessage()); |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 810 | log.debug("exception", e); |
| 811 | return Collections.emptyList(); |
| 812 | } |
| 813 | |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 814 | List<Entity> responseEntities = StreamSupport |
| 815 | .stream(responses.spliterator(), false) |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 816 | .map(ReadResponse::getEntitiesList) |
| 817 | .flatMap(List::stream) |
| 818 | .collect(Collectors.toList()); |
| 819 | |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 820 | return MeterEntryCodec.decodeMeterEntities(responseEntities, pipeconf); |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 821 | } |
| 822 | |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame^] | 823 | private boolean doWriteMeterCells(Collection<PiMeterCellConfig> cellConfigs, PiPipeconf pipeconf) { |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 824 | |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame^] | 825 | Collection<Update> updateMsgs = MeterEntryCodec.encodePiMeterCellConfigs(cellConfigs, pipeconf) |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 826 | .stream() |
| 827 | .map(meterEntryMsg -> |
| 828 | Update.newBuilder() |
| 829 | .setEntity(meterEntryMsg) |
| 830 | .setType(UPDATE_TYPES.get(WriteOperationType.MODIFY)) |
| 831 | .build()) |
| 832 | .collect(Collectors.toList()); |
| 833 | |
| 834 | if (updateMsgs.size() == 0) { |
| 835 | return true; |
| 836 | } |
| 837 | |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame^] | 838 | return write(updateMsgs, cellConfigs, WriteOperationType.MODIFY, "meter cell config"); |
| 839 | } |
| 840 | |
| 841 | private boolean doWriteMulticastGroupEntries( |
| 842 | Collection<PiMulticastGroupEntry> entries, |
| 843 | WriteOperationType opType) { |
| 844 | |
| 845 | final List<Update> updateMsgs = entries.stream() |
| 846 | .map(MulticastGroupEntryCodec::encode) |
| 847 | .map(mcMsg -> PacketReplicationEngineEntry.newBuilder() |
| 848 | .setMulticastGroupEntry(mcMsg) |
| 849 | .build()) |
| 850 | .map(preMsg -> Entity.newBuilder() |
| 851 | .setPacketReplicationEngineEntry(preMsg) |
| 852 | .build()) |
| 853 | .map(entityMsg -> Update.newBuilder() |
| 854 | .setEntity(entityMsg) |
| 855 | .setType(UPDATE_TYPES.get(opType)) |
| 856 | .build()) |
| 857 | .collect(Collectors.toList()); |
| 858 | return write(updateMsgs, entries, opType, "multicast group entry"); |
| 859 | } |
| 860 | |
| 861 | private Collection<PiMulticastGroupEntry> doReadAllMulticastGroupEntries() { |
| 862 | |
| 863 | final Entity entity = Entity.newBuilder() |
| 864 | .setPacketReplicationEngineEntry( |
| 865 | PacketReplicationEngineEntry.newBuilder() |
| 866 | .setMulticastGroupEntry( |
| 867 | MulticastGroupEntry.newBuilder() |
| 868 | .build()) |
| 869 | .build()) |
| 870 | .build(); |
| 871 | |
| 872 | final ReadRequest req = ReadRequest.newBuilder() |
| 873 | .setDeviceId(p4DeviceId) |
| 874 | .addEntities(entity) |
| 875 | .build(); |
| 876 | |
| 877 | Iterator<ReadResponse> responses; |
| 878 | try { |
| 879 | responses = blockingStub.read(req); |
| 880 | } catch (StatusRuntimeException e) { |
| 881 | log.warn("Unable to read multicast group entries from {}: {}", deviceId, e.getMessage()); |
| 882 | return Collections.emptyList(); |
| 883 | } |
| 884 | |
| 885 | Iterable<ReadResponse> responseIterable = () -> responses; |
| 886 | final List<PiMulticastGroupEntry> mcEntries = StreamSupport |
| 887 | .stream(responseIterable.spliterator(), false) |
| 888 | .map(ReadResponse::getEntitiesList) |
| 889 | .flatMap(List::stream) |
| 890 | .filter(e -> e.getEntityCase() |
| 891 | .equals(PACKET_REPLICATION_ENGINE_ENTRY)) |
| 892 | .map(Entity::getPacketReplicationEngineEntry) |
| 893 | .filter(e -> e.getTypeCase().equals(MULTICAST_GROUP_ENTRY)) |
| 894 | .map(PacketReplicationEngineEntry::getMulticastGroupEntry) |
| 895 | .map(MulticastGroupEntryCodec::decode) |
| 896 | .collect(Collectors.toList()); |
| 897 | |
| 898 | log.debug("Retrieved {} multicast group entries from {}...", |
| 899 | mcEntries.size(), deviceId); |
| 900 | |
| 901 | return mcEntries; |
| 902 | } |
| 903 | |
| 904 | private <E extends PiEntity> boolean write(Collection<Update> updates, |
| 905 | Collection<E> writeEntities, |
| 906 | WriteOperationType opType, |
| 907 | String entryType) { |
| 908 | try { |
| 909 | blockingStub.write(writeRequest(updates)); |
| 910 | return true; |
| 911 | } catch (StatusRuntimeException e) { |
| 912 | checkAndLogWriteErrors(writeEntities, e, opType, entryType); |
| 913 | return false; |
| 914 | } |
| 915 | } |
| 916 | |
| 917 | private WriteRequest writeRequest(Iterable<Update> updateMsgs) { |
| 918 | return WriteRequest.newBuilder() |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 919 | .setDeviceId(p4DeviceId) |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 920 | .setElectionId(clientElectionId) |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 921 | .addAllUpdates(updateMsgs) |
| 922 | .build(); |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 923 | } |
| 924 | |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 925 | private Void doShutdown() { |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 926 | log.info("Shutting down client for {}...", deviceId); |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 927 | if (streamRequestObserver != null) { |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 928 | try { |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 929 | streamRequestObserver.onCompleted(); |
| 930 | } catch (IllegalStateException e) { |
| 931 | // Thrown if stream channel is already completed. Can ignore. |
| 932 | log.debug("Ignored expection: {}", e); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 933 | } |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 934 | cancellableContext.cancel(new InterruptedException( |
| 935 | "Requested client shutdown")); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 936 | } |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 937 | this.executorService.shutdown(); |
| 938 | try { |
| 939 | executorService.awaitTermination(5, TimeUnit.SECONDS); |
| 940 | } catch (InterruptedException e) { |
| 941 | log.warn("Executor service didn't shutdown in time."); |
| 942 | Thread.currentThread().interrupt(); |
| 943 | } |
| 944 | return null; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 945 | } |
| 946 | |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 947 | private <E extends PiEntity> void checkAndLogWriteErrors( |
| 948 | Collection<E> writeEntities, StatusRuntimeException ex, |
| 949 | WriteOperationType opType, String entryType) { |
| 950 | |
| 951 | checkGrpcException(ex); |
| 952 | |
Carmelo Cascone | 5bc7e10 | 2018-02-18 18:27:55 -0800 | [diff] [blame] | 953 | List<P4RuntimeOuterClass.Error> errors = null; |
| 954 | String description = null; |
| 955 | try { |
| 956 | errors = extractWriteErrorDetails(ex); |
| 957 | } catch (InvalidProtocolBufferException e) { |
| 958 | description = ex.getStatus().getDescription(); |
| 959 | } |
| 960 | |
| 961 | log.warn("Unable to {} {} {}(s) on {}: {}{} (detailed errors might be logged below)", |
| 962 | opType.name(), writeEntities.size(), entryType, deviceId, |
| 963 | ex.getStatus().getCode().name(), |
| 964 | description == null ? "" : " - " + description); |
| 965 | |
| 966 | if (errors == null || errors.isEmpty()) { |
| 967 | return; |
| 968 | } |
| 969 | |
| 970 | // FIXME: we are assuming entities is an ordered collection, e.g. a list, |
| 971 | // and that errors are reported in the same order as the corresponding |
| 972 | // written entity. Write RPC methods should be refactored to accept an |
| 973 | // order list of entities, instead of a collection. |
| 974 | if (errors.size() == writeEntities.size()) { |
| 975 | Iterator<E> entityIterator = writeEntities.iterator(); |
| 976 | errors.stream() |
| 977 | .map(e -> ImmutablePair.of(e, entityIterator.next())) |
| 978 | .filter(p -> p.left.getCanonicalCode() != Status.OK.getCode().value()) |
| 979 | .forEach(p -> log.warn("Unable to {} {}: {} [{}]", |
| 980 | opType.name(), entryType, parseP4Error(p.getLeft()), |
| 981 | p.getRight().toString())); |
| 982 | } else { |
| 983 | log.error("Unable to reconcile error details to updates " + |
| 984 | "(sent {} updates, but device returned {} errors)", |
| 985 | entryType, writeEntities.size(), errors.size()); |
| 986 | errors.stream() |
| 987 | .filter(err -> err.getCanonicalCode() != Status.OK.getCode().value()) |
| 988 | .forEach(err -> log.warn("Unable to {} {} (unknown): {}", |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame^] | 989 | opType.name(), entryType, parseP4Error(err))); |
Carmelo Cascone | 5bc7e10 | 2018-02-18 18:27:55 -0800 | [diff] [blame] | 990 | } |
| 991 | } |
| 992 | |
| 993 | private List<P4RuntimeOuterClass.Error> extractWriteErrorDetails( |
| 994 | StatusRuntimeException ex) throws InvalidProtocolBufferException { |
| 995 | String statusString = ex.getStatus().getDescription(); |
| 996 | if (statusString == null) { |
| 997 | return Collections.emptyList(); |
| 998 | } |
| 999 | com.google.rpc.Status status = com.google.rpc.Status |
| 1000 | .parseFrom(statusString.getBytes()); |
| 1001 | return status.getDetailsList().stream() |
| 1002 | .map(any -> { |
| 1003 | try { |
| 1004 | return any.unpack(P4RuntimeOuterClass.Error.class); |
| 1005 | } catch (InvalidProtocolBufferException e) { |
| 1006 | log.warn("Unable to unpack P4Runtime Error: {}", |
| 1007 | any.toString()); |
| 1008 | return null; |
| 1009 | } |
| 1010 | }) |
| 1011 | .filter(Objects::nonNull) |
| 1012 | .collect(Collectors.toList()); |
| 1013 | |
| 1014 | } |
| 1015 | |
| 1016 | private String parseP4Error(P4RuntimeOuterClass.Error err) { |
| 1017 | return format("%s %s (%s code %d)%s", |
| 1018 | Status.fromCodeValue(err.getCanonicalCode()), |
| 1019 | err.getMessage(), |
| 1020 | err.getSpace(), |
| 1021 | err.getCode(), |
| 1022 | err.hasDetails() ? "\n" + err.getDetails().toString() : ""); |
| 1023 | } |
| 1024 | |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 1025 | private void checkGrpcException(StatusRuntimeException ex) { |
| 1026 | switch (ex.getStatus().getCode()) { |
| 1027 | case OK: |
| 1028 | break; |
| 1029 | case CANCELLED: |
| 1030 | break; |
| 1031 | case UNKNOWN: |
| 1032 | break; |
| 1033 | case INVALID_ARGUMENT: |
| 1034 | break; |
| 1035 | case DEADLINE_EXCEEDED: |
| 1036 | break; |
| 1037 | case NOT_FOUND: |
| 1038 | break; |
| 1039 | case ALREADY_EXISTS: |
| 1040 | break; |
| 1041 | case PERMISSION_DENIED: |
| 1042 | // Notify upper layers that this node is not master. |
| 1043 | controller.postEvent(new P4RuntimeEvent( |
| 1044 | P4RuntimeEvent.Type.ARBITRATION_RESPONSE, |
| 1045 | new ArbitrationResponse(deviceId, false))); |
| 1046 | break; |
| 1047 | case RESOURCE_EXHAUSTED: |
| 1048 | break; |
| 1049 | case FAILED_PRECONDITION: |
| 1050 | break; |
| 1051 | case ABORTED: |
| 1052 | break; |
| 1053 | case OUT_OF_RANGE: |
| 1054 | break; |
| 1055 | case UNIMPLEMENTED: |
| 1056 | break; |
| 1057 | case INTERNAL: |
| 1058 | break; |
| 1059 | case UNAVAILABLE: |
| 1060 | // Channel might be closed. |
| 1061 | controller.postEvent(new P4RuntimeEvent( |
| 1062 | P4RuntimeEvent.Type.CHANNEL_EVENT, |
| 1063 | new ChannelEvent(deviceId, ChannelEvent.Type.ERROR))); |
| 1064 | break; |
| 1065 | case DATA_LOSS: |
| 1066 | break; |
| 1067 | case UNAUTHENTICATED: |
| 1068 | break; |
| 1069 | default: |
| 1070 | break; |
| 1071 | } |
| 1072 | } |
| 1073 | |
| 1074 | private Uint128 bigIntegerToUint128(BigInteger value) { |
| 1075 | final byte[] arr = value.toByteArray(); |
| 1076 | final ByteBuffer bb = ByteBuffer.allocate(Long.BYTES * 2) |
| 1077 | .put(new byte[Long.BYTES * 2 - arr.length]) |
| 1078 | .put(arr); |
| 1079 | bb.rewind(); |
| 1080 | return Uint128.newBuilder() |
| 1081 | .setHigh(bb.getLong()) |
| 1082 | .setLow(bb.getLong()) |
| 1083 | .build(); |
| 1084 | } |
| 1085 | |
| 1086 | private BigInteger uint128ToBigInteger(Uint128 value) { |
| 1087 | return new BigInteger( |
| 1088 | ByteBuffer.allocate(Long.BYTES * 2) |
| 1089 | .putLong(value.getHigh()) |
| 1090 | .putLong(value.getLow()) |
| 1091 | .array()); |
| 1092 | } |
| 1093 | |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 1094 | /** |
| 1095 | * Handles messages received from the device on the stream channel. |
| 1096 | */ |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 1097 | private class StreamChannelResponseObserver |
| 1098 | implements StreamObserver<StreamMessageResponse> { |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 1099 | |
| 1100 | @Override |
| 1101 | public void onNext(StreamMessageResponse message) { |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 1102 | executorService.submit(() -> doNext(message)); |
| 1103 | } |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 1104 | |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 1105 | private void doNext(StreamMessageResponse message) { |
Carmelo Cascone | a966c34 | 2017-07-30 01:56:30 -0400 | [diff] [blame] | 1106 | try { |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 1107 | log.debug("Received message on stream channel from {}: {}", |
| 1108 | deviceId, message.getUpdateCase()); |
Carmelo Cascone | a966c34 | 2017-07-30 01:56:30 -0400 | [diff] [blame] | 1109 | switch (message.getUpdateCase()) { |
| 1110 | case PACKET: |
Carmelo Cascone | a966c34 | 2017-07-30 01:56:30 -0400 | [diff] [blame] | 1111 | doPacketIn(message.getPacket()); |
Andrea Campanella | 288b273 | 2017-07-28 14:16:16 +0200 | [diff] [blame] | 1112 | return; |
Carmelo Cascone | a966c34 | 2017-07-30 01:56:30 -0400 | [diff] [blame] | 1113 | case ARBITRATION: |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 1114 | doArbitrationResponse(message.getArbitration()); |
Carmelo Cascone | a966c34 | 2017-07-30 01:56:30 -0400 | [diff] [blame] | 1115 | return; |
| 1116 | default: |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 1117 | log.warn("Unrecognized stream message from {}: {}", |
| 1118 | deviceId, message.getUpdateCase()); |
Carmelo Cascone | a966c34 | 2017-07-30 01:56:30 -0400 | [diff] [blame] | 1119 | } |
| 1120 | } catch (Throwable ex) { |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 1121 | log.error("Exception while processing stream message from {}", |
| 1122 | deviceId, ex); |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 1123 | } |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 1124 | } |
| 1125 | |
| 1126 | @Override |
| 1127 | public void onError(Throwable throwable) { |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 1128 | log.warn("Error on stream channel for {}: {}", |
| 1129 | deviceId, Status.fromThrowable(throwable)); |
| 1130 | controller.postEvent(new P4RuntimeEvent( |
| 1131 | P4RuntimeEvent.Type.CHANNEL_EVENT, |
| 1132 | new ChannelEvent(deviceId, ChannelEvent.Type.ERROR))); |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 1133 | } |
| 1134 | |
| 1135 | @Override |
| 1136 | public void onCompleted() { |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 1137 | log.warn("Stream channel for {} has completed", deviceId); |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 1138 | controller.postEvent(new P4RuntimeEvent( |
| 1139 | P4RuntimeEvent.Type.CHANNEL_EVENT, |
| 1140 | new ChannelEvent(deviceId, ChannelEvent.Type.CLOSED))); |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 1141 | } |
| 1142 | } |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 1143 | } |