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() { |
Carmelo Cascone | 0e427dc | 2018-08-14 22:15:10 -0700 | [diff] [blame^] | 210 | return supplyInContext(this::doBecomeMaster, |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 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 | d61fdb3 | 2017-10-30 10:09:57 -0700 | [diff] [blame] | 359 | private boolean doSetPipelineConfig(PiPipeconf pipeconf, ByteBuffer deviceData) { |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 360 | |
Carmelo Cascone | d61fdb3 | 2017-10-30 10:09:57 -0700 | [diff] [blame] | 361 | log.info("Setting pipeline config for {} to {}...", deviceId, pipeconf.id()); |
| 362 | |
| 363 | checkNotNull(deviceData, "deviceData cannot be null"); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 364 | |
| 365 | P4Info p4Info = PipeconfHelper.getP4Info(pipeconf); |
| 366 | if (p4Info == null) { |
| 367 | // Problem logged by PipeconfHelper. |
| 368 | return false; |
| 369 | } |
| 370 | |
Carmelo Cascone | d61fdb3 | 2017-10-30 10:09:57 -0700 | [diff] [blame] | 371 | P4Config.P4DeviceConfig p4DeviceConfigMsg = P4Config.P4DeviceConfig |
| 372 | .newBuilder() |
| 373 | .setExtras(P4Config.P4DeviceConfig.Extras.getDefaultInstance()) |
| 374 | .setReassign(true) |
| 375 | .setDeviceData(ByteString.copyFrom(deviceData)) |
| 376 | .build(); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 377 | |
Carmelo Cascone | d61fdb3 | 2017-10-30 10:09:57 -0700 | [diff] [blame] | 378 | ForwardingPipelineConfig pipelineConfig = ForwardingPipelineConfig |
Andrea Campanella | 0288c87 | 2017-08-07 18:32:51 +0200 | [diff] [blame] | 379 | .newBuilder() |
Carmelo Cascone | d61fdb3 | 2017-10-30 10:09:57 -0700 | [diff] [blame] | 380 | .setP4Info(p4Info) |
| 381 | .setP4DeviceConfig(p4DeviceConfigMsg.toByteString()) |
| 382 | .build(); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 383 | |
| 384 | SetForwardingPipelineConfigRequest request = SetForwardingPipelineConfigRequest |
| 385 | .newBuilder() |
Andrea Campanella | 8bcd586 | 2017-12-11 11:34:45 +0100 | [diff] [blame] | 386 | .setDeviceId(p4DeviceId) |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 387 | .setElectionId(clientElectionId) |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 388 | .setAction(VERIFY_AND_COMMIT) |
Andrea Campanella | 8bcd586 | 2017-12-11 11:34:45 +0100 | [diff] [blame] | 389 | .setConfig(pipelineConfig) |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 390 | .build(); |
| 391 | |
| 392 | try { |
| 393 | this.blockingStub.setForwardingPipelineConfig(request); |
Carmelo Cascone | d61fdb3 | 2017-10-30 10:09:57 -0700 | [diff] [blame] | 394 | return true; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 395 | } catch (StatusRuntimeException ex) { |
Carmelo Cascone | 5bc7e10 | 2018-02-18 18:27:55 -0800 | [diff] [blame] | 396 | log.warn("Unable to set pipeline config on {}: {}", deviceId, ex.getMessage()); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 397 | return false; |
| 398 | } |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | private boolean doWriteTableEntries(Collection<PiTableEntry> piTableEntries, WriteOperationType opType, |
| 402 | PiPipeconf pipeconf) { |
Carmelo Cascone | 5bc7e10 | 2018-02-18 18:27:55 -0800 | [diff] [blame] | 403 | if (piTableEntries.size() == 0) { |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 404 | return true; |
| 405 | } |
| 406 | |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 407 | Collection<Update> updateMsgs; |
Carmelo Cascone | 5bc7e10 | 2018-02-18 18:27:55 -0800 | [diff] [blame] | 408 | try { |
| 409 | updateMsgs = TableEntryEncoder.encode(piTableEntries, pipeconf) |
| 410 | .stream() |
| 411 | .map(tableEntryMsg -> |
| 412 | Update.newBuilder() |
| 413 | .setEntity(Entity.newBuilder() |
| 414 | .setTableEntry(tableEntryMsg) |
| 415 | .build()) |
| 416 | .setType(UPDATE_TYPES.get(opType)) |
| 417 | .build()) |
| 418 | .collect(Collectors.toList()); |
| 419 | } catch (EncodeException e) { |
| 420 | log.error("Unable to encode table entries, aborting {} operation: {}", |
| 421 | opType.name(), e.getMessage()); |
| 422 | return false; |
| 423 | } |
| 424 | |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 425 | return write(updateMsgs, piTableEntries, opType, "table entry"); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | private Collection<PiTableEntry> doDumpTable(PiTableId piTableId, PiPipeconf pipeconf) { |
| 429 | |
Carmelo Cascone | 9f00770 | 2017-08-24 13:30:51 +0200 | [diff] [blame] | 430 | log.debug("Dumping table {} from {} (pipeconf {})...", piTableId, deviceId, pipeconf.id()); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 431 | |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 432 | int tableId; |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 433 | if (piTableId == null) { |
| 434 | // Dump all tables. |
| 435 | tableId = 0; |
| 436 | } else { |
| 437 | P4InfoBrowser browser = PipeconfHelper.getP4InfoBrowser(pipeconf); |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 438 | if (browser == null) { |
| 439 | log.warn("Unable to get a P4Info browser for pipeconf {}", pipeconf); |
| 440 | return Collections.emptyList(); |
| 441 | } |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 442 | try { |
| 443 | tableId = browser.tables().getByName(piTableId.id()).getPreamble().getId(); |
| 444 | } catch (P4InfoBrowser.NotFoundException e) { |
| 445 | log.warn("Unable to dump table: {}", e.getMessage()); |
| 446 | return Collections.emptyList(); |
| 447 | } |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | ReadRequest requestMsg = ReadRequest.newBuilder() |
| 451 | .setDeviceId(p4DeviceId) |
| 452 | .addEntities(Entity.newBuilder() |
| 453 | .setTableEntry(TableEntry.newBuilder() |
| 454 | .setTableId(tableId) |
| 455 | .build()) |
| 456 | .build()) |
| 457 | .build(); |
| 458 | |
| 459 | Iterator<ReadResponse> responses; |
| 460 | try { |
| 461 | responses = blockingStub.read(requestMsg); |
| 462 | } catch (StatusRuntimeException e) { |
Carmelo Cascone | 5bc7e10 | 2018-02-18 18:27:55 -0800 | [diff] [blame] | 463 | log.warn("Unable to dump table {} from {}: {}", piTableId, deviceId, e.getMessage()); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 464 | return Collections.emptyList(); |
| 465 | } |
| 466 | |
| 467 | Iterable<ReadResponse> responseIterable = () -> responses; |
| 468 | List<TableEntry> tableEntryMsgs = StreamSupport |
| 469 | .stream(responseIterable.spliterator(), false) |
| 470 | .map(ReadResponse::getEntitiesList) |
| 471 | .flatMap(List::stream) |
| 472 | .filter(entity -> entity.getEntityCase() == TABLE_ENTRY) |
| 473 | .map(Entity::getTableEntry) |
| 474 | .collect(Collectors.toList()); |
| 475 | |
Carmelo Cascone | 9f00770 | 2017-08-24 13:30:51 +0200 | [diff] [blame] | 476 | log.debug("Retrieved {} entries from table {} on {}...", tableEntryMsgs.size(), piTableId, deviceId); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 477 | |
| 478 | return TableEntryEncoder.decode(tableEntryMsgs, pipeconf); |
| 479 | } |
| 480 | |
Andrea Campanella | fc1d34c | 2017-07-18 17:01:41 +0200 | [diff] [blame] | 481 | private boolean doPacketOut(PiPacketOperation packet, PiPipeconf pipeconf) { |
| 482 | try { |
| 483 | //encode the PiPacketOperation into a PacketOut |
| 484 | PacketOut packetOut = PacketIOCodec.encodePacketOut(packet, pipeconf); |
| 485 | |
| 486 | //Build the request |
| 487 | StreamMessageRequest packetOutRequest = StreamMessageRequest |
| 488 | .newBuilder().setPacket(packetOut).build(); |
| 489 | |
| 490 | //Send the request |
| 491 | streamRequestObserver.onNext(packetOutRequest); |
| 492 | |
| 493 | } catch (P4InfoBrowser.NotFoundException e) { |
| 494 | log.error("Cant find expected metadata in p4Info file. {}", e.getMessage()); |
| 495 | log.debug("Exception", e); |
| 496 | return false; |
| 497 | } |
| 498 | return true; |
| 499 | } |
| 500 | |
Carmelo Cascone | a966c34 | 2017-07-30 01:56:30 -0400 | [diff] [blame] | 501 | private void doPacketIn(PacketIn packetInMsg) { |
| 502 | |
| 503 | // Retrieve the pipeconf for this client's device. |
| 504 | PiPipeconfService pipeconfService = DefaultServiceDirectory.getService(PiPipeconfService.class); |
| 505 | if (pipeconfService == null) { |
| 506 | throw new IllegalStateException("PiPipeconfService is null. Can't handle packet in."); |
| 507 | } |
| 508 | final PiPipeconf pipeconf; |
| 509 | if (pipeconfService.ofDevice(deviceId).isPresent() && |
| 510 | pipeconfService.getPipeconf(pipeconfService.ofDevice(deviceId).get()).isPresent()) { |
| 511 | pipeconf = pipeconfService.getPipeconf(pipeconfService.ofDevice(deviceId).get()).get(); |
| 512 | } else { |
| 513 | log.warn("Unable to get pipeconf of {}. Can't handle packet in", deviceId); |
| 514 | return; |
| 515 | } |
| 516 | // Decode packet message and post event. |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 517 | PiPacketOperation packetOperation = PacketIOCodec.decodePacketIn(packetInMsg, pipeconf, deviceId); |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 518 | PacketInEvent packetInEventSubject = new PacketInEvent(deviceId, packetOperation); |
Carmelo Cascone | 2cad9ef | 2017-08-01 21:52:07 +0200 | [diff] [blame] | 519 | P4RuntimeEvent event = new P4RuntimeEvent(P4RuntimeEvent.Type.PACKET_IN, packetInEventSubject); |
Carmelo Cascone | a966c34 | 2017-07-30 01:56:30 -0400 | [diff] [blame] | 520 | log.debug("Received packet in: {}", event); |
| 521 | controller.postEvent(event); |
| 522 | } |
| 523 | |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 524 | private void doArbitrationResponse(MasterArbitrationUpdate msg) { |
| 525 | // From the spec... |
| 526 | // - Election_id: The stream RPC with the highest election_id is the |
| 527 | // master. Switch populates with the highest election ID it |
| 528 | // has received from all connected controllers. |
| 529 | // - Status: Switch populates this with OK for the client that is the |
| 530 | // master, and with an error status for all other connected clients (at |
| 531 | // every mastership change). |
| 532 | if (!msg.hasElectionId() || !msg.hasStatus()) { |
Yi Tseng | 3e7f145 | 2017-10-20 10:31:53 -0700 | [diff] [blame] | 533 | return; |
| 534 | } |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 535 | final boolean isMaster = msg.getStatus().getCode() == Status.OK.getCode().value(); |
| 536 | log.info("Received arbitration update from {}: isMaster={}, electionId={}", |
| 537 | deviceId, isMaster, uint128ToBigInteger(msg.getElectionId())); |
| 538 | controller.postEvent(new P4RuntimeEvent( |
| 539 | P4RuntimeEvent.Type.ARBITRATION_RESPONSE, |
| 540 | new ArbitrationResponse(deviceId, isMaster))); |
Carmelo Cascone | a966c34 | 2017-07-30 01:56:30 -0400 | [diff] [blame] | 541 | } |
| 542 | |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 543 | private Collection<PiCounterCellData> doReadAllCounterCells( |
| 544 | Collection<PiCounterId> counterIds, PiPipeconf pipeconf) { |
| 545 | return doReadCounterEntities( |
| 546 | CounterEntryCodec.readAllCellsEntities(counterIds, pipeconf), |
| 547 | pipeconf); |
| 548 | } |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 549 | |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 550 | private Collection<PiCounterCellData> doReadCounterCells( |
| 551 | Collection<PiCounterCellId> cellIds, PiPipeconf pipeconf) { |
| 552 | return doReadCounterEntities( |
| 553 | CounterEntryCodec.encodePiCounterCellIds(cellIds, pipeconf), |
| 554 | pipeconf); |
| 555 | } |
| 556 | |
| 557 | private Collection<PiCounterCellData> doReadCounterEntities( |
| 558 | Collection<Entity> counterEntities, PiPipeconf pipeconf) { |
| 559 | |
| 560 | if (counterEntities.size() == 0) { |
| 561 | return Collections.emptyList(); |
| 562 | } |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 563 | |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 564 | final ReadRequest request = ReadRequest.newBuilder() |
| 565 | .setDeviceId(p4DeviceId) |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 566 | .addAllEntities(counterEntities) |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 567 | .build(); |
| 568 | |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 569 | final Iterable<ReadResponse> responses; |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 570 | try { |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 571 | responses = () -> blockingStub.read(request); |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 572 | } catch (StatusRuntimeException e) { |
Carmelo Cascone | 5bc7e10 | 2018-02-18 18:27:55 -0800 | [diff] [blame] | 573 | log.warn("Unable to read counter cells from {}: {}", deviceId, e.getMessage()); |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 574 | return Collections.emptyList(); |
| 575 | } |
| 576 | |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 577 | List<Entity> entities = StreamSupport.stream(responses.spliterator(), false) |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 578 | .map(ReadResponse::getEntitiesList) |
| 579 | .flatMap(List::stream) |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 580 | .collect(Collectors.toList()); |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 581 | |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 582 | return CounterEntryCodec.decodeCounterEntities(entities, pipeconf); |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 583 | } |
| 584 | |
Yi Tseng | 8d35513 | 2018-04-13 01:40:48 +0800 | [diff] [blame] | 585 | private boolean doWriteActionGroupMembers(PiActionProfileId profileId, Collection<PiActionGroupMember> members, |
| 586 | WriteOperationType opType, PiPipeconf pipeconf) { |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 587 | final Collection<ActionProfileMember> actionProfileMembers = Lists.newArrayList(); |
Carmelo Cascone | 5bc7e10 | 2018-02-18 18:27:55 -0800 | [diff] [blame] | 588 | |
Yi Tseng | 8d35513 | 2018-04-13 01:40:48 +0800 | [diff] [blame] | 589 | for (PiActionGroupMember member : members) { |
Carmelo Cascone | 5bc7e10 | 2018-02-18 18:27:55 -0800 | [diff] [blame] | 590 | try { |
Yi Tseng | 8d35513 | 2018-04-13 01:40:48 +0800 | [diff] [blame] | 591 | actionProfileMembers.add(ActionProfileMemberEncoder.encode(profileId, member, pipeconf)); |
Carmelo Cascone | 5bc7e10 | 2018-02-18 18:27:55 -0800 | [diff] [blame] | 592 | } catch (EncodeException | P4InfoBrowser.NotFoundException e) { |
| 593 | log.warn("Unable to encode group member, aborting {} operation: {} [{}]", |
| 594 | opType.name(), e.getMessage(), member.toString()); |
| 595 | return false; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 596 | } |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 597 | } |
| 598 | |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 599 | final Collection<Update> updateMsgs = actionProfileMembers.stream() |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 600 | .map(actionProfileMember -> |
| 601 | Update.newBuilder() |
| 602 | .setEntity(Entity.newBuilder() |
| 603 | .setActionProfileMember(actionProfileMember) |
| 604 | .build()) |
| 605 | .setType(UPDATE_TYPES.get(opType)) |
| 606 | .build()) |
| 607 | .collect(Collectors.toList()); |
| 608 | |
| 609 | if (updateMsgs.size() == 0) { |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 610 | // Nothing to update. |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 611 | return true; |
| 612 | } |
| 613 | |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 614 | return write(updateMsgs, members, opType, "group member"); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | private Collection<PiActionGroup> doDumpGroups(PiActionProfileId piActionProfileId, PiPipeconf pipeconf) { |
| 618 | log.debug("Dumping groups from action profile {} from {} (pipeconf {})...", |
| 619 | piActionProfileId.id(), deviceId, pipeconf.id()); |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 620 | |
| 621 | final P4InfoBrowser browser = PipeconfHelper.getP4InfoBrowser(pipeconf); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 622 | if (browser == null) { |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 623 | 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] | 624 | return Collections.emptySet(); |
| 625 | } |
| 626 | |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 627 | final int actionProfileId; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 628 | try { |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 629 | actionProfileId = browser |
| 630 | .actionProfiles() |
Carmelo Cascone | cb0a49c | 2017-10-03 14:32:23 +0200 | [diff] [blame] | 631 | .getByName(piActionProfileId.id()) |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 632 | .getPreamble() |
| 633 | .getId(); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 634 | } catch (P4InfoBrowser.NotFoundException e) { |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 635 | log.warn("Unable to dump groups: {}", e.getMessage()); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 636 | return Collections.emptySet(); |
| 637 | } |
| 638 | |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 639 | // Prepare read request to read all groups from the given action profile. |
| 640 | final ReadRequest groupRequestMsg = ReadRequest.newBuilder() |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 641 | .setDeviceId(p4DeviceId) |
| 642 | .addEntities(Entity.newBuilder() |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 643 | .setActionProfileGroup( |
| 644 | ActionProfileGroup.newBuilder() |
| 645 | .setActionProfileId(actionProfileId) |
| 646 | .build()) |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 647 | .build()) |
| 648 | .build(); |
| 649 | |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 650 | // Read groups. |
| 651 | final Iterator<ReadResponse> groupResponses; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 652 | try { |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 653 | groupResponses = blockingStub.read(groupRequestMsg); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 654 | } catch (StatusRuntimeException e) { |
Carmelo Cascone | 5bc7e10 | 2018-02-18 18:27:55 -0800 | [diff] [blame] | 655 | log.warn("Unable to dump action profile {} from {}: {}", piActionProfileId, deviceId, e.getMessage()); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 656 | return Collections.emptySet(); |
| 657 | } |
| 658 | |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 659 | final List<ActionProfileGroup> groupMsgs = Tools.stream(() -> groupResponses) |
| 660 | .map(ReadResponse::getEntitiesList) |
| 661 | .flatMap(List::stream) |
| 662 | .filter(entity -> entity.getEntityCase() == ACTION_PROFILE_GROUP) |
| 663 | .map(Entity::getActionProfileGroup) |
| 664 | .collect(Collectors.toList()); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 665 | |
| 666 | log.debug("Retrieved {} groups from action profile {} on {}...", |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 667 | groupMsgs.size(), piActionProfileId.id(), deviceId); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 668 | |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 669 | // Returned groups contain only a minimal description of their members. |
| 670 | // 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] | 671 | |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 672 | // Keep a map of all member IDs for each group ID, will need it later. |
| 673 | final Multimap<Integer, Integer> groupIdToMemberIdsMap = HashMultimap.create(); |
| 674 | groupMsgs.forEach(g -> groupIdToMemberIdsMap.putAll( |
| 675 | g.getGroupId(), |
| 676 | g.getMembersList().stream() |
| 677 | .map(ActionProfileGroup.Member::getMemberId) |
| 678 | .collect(Collectors.toList()))); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 679 | |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 680 | // Prepare one big read request to read all members in one shot. |
| 681 | final Set<Entity> entityMsgs = groupMsgs.stream() |
| 682 | .flatMap(g -> g.getMembersList().stream()) |
| 683 | .map(ActionProfileGroup.Member::getMemberId) |
| 684 | // Prevent issuing many read requests for the same member. |
| 685 | .distinct() |
| 686 | .map(id -> ActionProfileMember.newBuilder() |
| 687 | .setActionProfileId(actionProfileId) |
| 688 | .setMemberId(id) |
| 689 | .build()) |
| 690 | .map(m -> Entity.newBuilder() |
| 691 | .setActionProfileMember(m) |
| 692 | .build()) |
| 693 | .collect(Collectors.toSet()); |
| 694 | final ReadRequest memberRequestMsg = ReadRequest.newBuilder().setDeviceId(p4DeviceId) |
| 695 | .addAllEntities(entityMsgs) |
| 696 | .build(); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 697 | |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 698 | // Read members. |
| 699 | final Iterator<ReadResponse> memberResponses; |
| 700 | try { |
| 701 | memberResponses = blockingStub.read(memberRequestMsg); |
| 702 | } catch (StatusRuntimeException e) { |
Carmelo Cascone | 5bc7e10 | 2018-02-18 18:27:55 -0800 | [diff] [blame] | 703 | log.warn("Unable to read members of action profile {} from {}: {}", |
| 704 | piActionProfileId, deviceId, e.getMessage()); |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 705 | return Collections.emptyList(); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 706 | } |
| 707 | |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 708 | final Multimap<Integer, ActionProfileMember> groupIdToMembersMap = HashMultimap.create(); |
| 709 | Tools.stream(() -> memberResponses) |
| 710 | .map(ReadResponse::getEntitiesList) |
| 711 | .flatMap(List::stream) |
| 712 | .filter(e -> e.getEntityCase() == ACTION_PROFILE_MEMBER) |
| 713 | .map(Entity::getActionProfileMember) |
| 714 | .forEach(member -> groupIdToMemberIdsMap.asMap() |
| 715 | // Get all group IDs that contain this member. |
| 716 | .entrySet() |
| 717 | .stream() |
| 718 | .filter(entry -> entry.getValue().contains(member.getMemberId())) |
| 719 | .map(Map.Entry::getKey) |
| 720 | .forEach(gid -> groupIdToMembersMap.put(gid, member))); |
| 721 | |
| 722 | log.debug("Retrieved {} group members from action profile {} on {}...", |
| 723 | groupIdToMembersMap.size(), piActionProfileId.id(), deviceId); |
| 724 | |
| 725 | return groupMsgs.stream() |
| 726 | .map(groupMsg -> { |
| 727 | try { |
| 728 | return ActionProfileGroupEncoder.decode(groupMsg, |
| 729 | groupIdToMembersMap.get(groupMsg.getGroupId()), |
| 730 | pipeconf); |
| 731 | } catch (P4InfoBrowser.NotFoundException | EncodeException e) { |
| 732 | log.warn("Unable to decode group: {}\n {}", e.getMessage(), groupMsg); |
| 733 | return null; |
| 734 | } |
| 735 | }) |
| 736 | .filter(Objects::nonNull) |
| 737 | .collect(Collectors.toList()); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 738 | } |
| 739 | |
| 740 | private boolean doWriteActionGroup(PiActionGroup group, WriteOperationType opType, PiPipeconf pipeconf) { |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 741 | final ActionProfileGroup actionProfileGroup; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 742 | try { |
| 743 | actionProfileGroup = ActionProfileGroupEncoder.encode(group, pipeconf); |
| 744 | } catch (EncodeException | P4InfoBrowser.NotFoundException e) { |
Carmelo Cascone | 5bc7e10 | 2018-02-18 18:27:55 -0800 | [diff] [blame] | 745 | log.warn("Unable to encode group, aborting {} operation: {}", e.getMessage(), opType.name()); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 746 | return false; |
| 747 | } |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 748 | |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 749 | final Update updateMsg = Update.newBuilder() |
| 750 | .setEntity(Entity.newBuilder() |
| 751 | .setActionProfileGroup(actionProfileGroup) |
| 752 | .build()) |
| 753 | .setType(UPDATE_TYPES.get(opType)) |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 754 | .build(); |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 755 | |
| 756 | return write(Collections.singleton(updateMsg), Collections.singleton(group), |
| 757 | opType, "group"); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 758 | } |
| 759 | |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 760 | private Collection<PiMeterCellConfig> doReadAllMeterCells( |
| 761 | Collection<PiMeterId> meterIds, PiPipeconf pipeconf) { |
| 762 | return doReadMeterEntities(MeterEntryCodec.readAllCellsEntities( |
| 763 | meterIds, pipeconf), pipeconf); |
| 764 | } |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 765 | |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 766 | private Collection<PiMeterCellConfig> doReadMeterCells( |
| 767 | Collection<PiMeterCellId> cellIds, PiPipeconf pipeconf) { |
| 768 | |
| 769 | final Collection<PiMeterCellConfig> piMeterCellConfigs = cellIds.stream() |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 770 | .map(cellId -> PiMeterCellConfig.builder() |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 771 | .withMeterCellId(cellId) |
| 772 | .build()) |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 773 | .collect(Collectors.toList()); |
| 774 | |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 775 | return doReadMeterEntities(MeterEntryCodec.encodePiMeterCellConfigs( |
| 776 | piMeterCellConfigs, pipeconf), pipeconf); |
| 777 | } |
| 778 | |
| 779 | private Collection<PiMeterCellConfig> doReadMeterEntities( |
| 780 | Collection<Entity> entitiesToRead, PiPipeconf pipeconf) { |
| 781 | |
| 782 | if (entitiesToRead.size() == 0) { |
| 783 | return Collections.emptyList(); |
| 784 | } |
| 785 | |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 786 | final ReadRequest request = ReadRequest.newBuilder() |
| 787 | .setDeviceId(p4DeviceId) |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 788 | .addAllEntities(entitiesToRead) |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 789 | .build(); |
| 790 | |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 791 | final Iterable<ReadResponse> responses; |
| 792 | try { |
| 793 | responses = () -> blockingStub.read(request); |
| 794 | } catch (StatusRuntimeException e) { |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 795 | log.warn("Unable to read meter cells: {}", e.getMessage()); |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 796 | log.debug("exception", e); |
| 797 | return Collections.emptyList(); |
| 798 | } |
| 799 | |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 800 | List<Entity> responseEntities = StreamSupport |
| 801 | .stream(responses.spliterator(), false) |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 802 | .map(ReadResponse::getEntitiesList) |
| 803 | .flatMap(List::stream) |
| 804 | .collect(Collectors.toList()); |
| 805 | |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 806 | return MeterEntryCodec.decodeMeterEntities(responseEntities, pipeconf); |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 807 | } |
| 808 | |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 809 | private boolean doWriteMeterCells(Collection<PiMeterCellConfig> cellConfigs, PiPipeconf pipeconf) { |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 810 | |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 811 | Collection<Update> updateMsgs = MeterEntryCodec.encodePiMeterCellConfigs(cellConfigs, pipeconf) |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 812 | .stream() |
| 813 | .map(meterEntryMsg -> |
| 814 | Update.newBuilder() |
| 815 | .setEntity(meterEntryMsg) |
| 816 | .setType(UPDATE_TYPES.get(WriteOperationType.MODIFY)) |
| 817 | .build()) |
| 818 | .collect(Collectors.toList()); |
| 819 | |
| 820 | if (updateMsgs.size() == 0) { |
| 821 | return true; |
| 822 | } |
| 823 | |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 824 | return write(updateMsgs, cellConfigs, WriteOperationType.MODIFY, "meter cell config"); |
| 825 | } |
| 826 | |
| 827 | private boolean doWriteMulticastGroupEntries( |
| 828 | Collection<PiMulticastGroupEntry> entries, |
| 829 | WriteOperationType opType) { |
| 830 | |
| 831 | final List<Update> updateMsgs = entries.stream() |
| 832 | .map(MulticastGroupEntryCodec::encode) |
| 833 | .map(mcMsg -> PacketReplicationEngineEntry.newBuilder() |
| 834 | .setMulticastGroupEntry(mcMsg) |
| 835 | .build()) |
| 836 | .map(preMsg -> Entity.newBuilder() |
| 837 | .setPacketReplicationEngineEntry(preMsg) |
| 838 | .build()) |
| 839 | .map(entityMsg -> Update.newBuilder() |
| 840 | .setEntity(entityMsg) |
| 841 | .setType(UPDATE_TYPES.get(opType)) |
| 842 | .build()) |
| 843 | .collect(Collectors.toList()); |
| 844 | return write(updateMsgs, entries, opType, "multicast group entry"); |
| 845 | } |
| 846 | |
| 847 | private Collection<PiMulticastGroupEntry> doReadAllMulticastGroupEntries() { |
| 848 | |
| 849 | final Entity entity = Entity.newBuilder() |
| 850 | .setPacketReplicationEngineEntry( |
| 851 | PacketReplicationEngineEntry.newBuilder() |
| 852 | .setMulticastGroupEntry( |
| 853 | MulticastGroupEntry.newBuilder() |
| 854 | .build()) |
| 855 | .build()) |
| 856 | .build(); |
| 857 | |
| 858 | final ReadRequest req = ReadRequest.newBuilder() |
| 859 | .setDeviceId(p4DeviceId) |
| 860 | .addEntities(entity) |
| 861 | .build(); |
| 862 | |
| 863 | Iterator<ReadResponse> responses; |
| 864 | try { |
| 865 | responses = blockingStub.read(req); |
| 866 | } catch (StatusRuntimeException e) { |
| 867 | log.warn("Unable to read multicast group entries from {}: {}", deviceId, e.getMessage()); |
| 868 | return Collections.emptyList(); |
| 869 | } |
| 870 | |
| 871 | Iterable<ReadResponse> responseIterable = () -> responses; |
| 872 | final List<PiMulticastGroupEntry> mcEntries = StreamSupport |
| 873 | .stream(responseIterable.spliterator(), false) |
| 874 | .map(ReadResponse::getEntitiesList) |
| 875 | .flatMap(List::stream) |
| 876 | .filter(e -> e.getEntityCase() |
| 877 | .equals(PACKET_REPLICATION_ENGINE_ENTRY)) |
| 878 | .map(Entity::getPacketReplicationEngineEntry) |
| 879 | .filter(e -> e.getTypeCase().equals(MULTICAST_GROUP_ENTRY)) |
| 880 | .map(PacketReplicationEngineEntry::getMulticastGroupEntry) |
| 881 | .map(MulticastGroupEntryCodec::decode) |
| 882 | .collect(Collectors.toList()); |
| 883 | |
| 884 | log.debug("Retrieved {} multicast group entries from {}...", |
| 885 | mcEntries.size(), deviceId); |
| 886 | |
| 887 | return mcEntries; |
| 888 | } |
| 889 | |
| 890 | private <E extends PiEntity> boolean write(Collection<Update> updates, |
| 891 | Collection<E> writeEntities, |
| 892 | WriteOperationType opType, |
| 893 | String entryType) { |
| 894 | try { |
| 895 | blockingStub.write(writeRequest(updates)); |
| 896 | return true; |
| 897 | } catch (StatusRuntimeException e) { |
| 898 | checkAndLogWriteErrors(writeEntities, e, opType, entryType); |
| 899 | return false; |
| 900 | } |
| 901 | } |
| 902 | |
| 903 | private WriteRequest writeRequest(Iterable<Update> updateMsgs) { |
| 904 | return WriteRequest.newBuilder() |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 905 | .setDeviceId(p4DeviceId) |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 906 | .setElectionId(clientElectionId) |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 907 | .addAllUpdates(updateMsgs) |
| 908 | .build(); |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 909 | } |
| 910 | |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 911 | private Void doShutdown() { |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 912 | log.info("Shutting down client for {}...", deviceId); |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 913 | if (streamRequestObserver != null) { |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 914 | try { |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 915 | streamRequestObserver.onCompleted(); |
| 916 | } catch (IllegalStateException e) { |
| 917 | // Thrown if stream channel is already completed. Can ignore. |
| 918 | log.debug("Ignored expection: {}", e); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 919 | } |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 920 | cancellableContext.cancel(new InterruptedException( |
| 921 | "Requested client shutdown")); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 922 | } |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 923 | this.executorService.shutdown(); |
| 924 | try { |
| 925 | executorService.awaitTermination(5, TimeUnit.SECONDS); |
| 926 | } catch (InterruptedException e) { |
| 927 | log.warn("Executor service didn't shutdown in time."); |
| 928 | Thread.currentThread().interrupt(); |
| 929 | } |
| 930 | return null; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 931 | } |
| 932 | |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 933 | private <E extends PiEntity> void checkAndLogWriteErrors( |
| 934 | Collection<E> writeEntities, StatusRuntimeException ex, |
| 935 | WriteOperationType opType, String entryType) { |
| 936 | |
| 937 | checkGrpcException(ex); |
| 938 | |
Carmelo Cascone | 5bc7e10 | 2018-02-18 18:27:55 -0800 | [diff] [blame] | 939 | List<P4RuntimeOuterClass.Error> errors = null; |
| 940 | String description = null; |
| 941 | try { |
| 942 | errors = extractWriteErrorDetails(ex); |
| 943 | } catch (InvalidProtocolBufferException e) { |
| 944 | description = ex.getStatus().getDescription(); |
| 945 | } |
| 946 | |
| 947 | log.warn("Unable to {} {} {}(s) on {}: {}{} (detailed errors might be logged below)", |
| 948 | opType.name(), writeEntities.size(), entryType, deviceId, |
| 949 | ex.getStatus().getCode().name(), |
| 950 | description == null ? "" : " - " + description); |
| 951 | |
| 952 | if (errors == null || errors.isEmpty()) { |
| 953 | return; |
| 954 | } |
| 955 | |
| 956 | // FIXME: we are assuming entities is an ordered collection, e.g. a list, |
| 957 | // and that errors are reported in the same order as the corresponding |
| 958 | // written entity. Write RPC methods should be refactored to accept an |
| 959 | // order list of entities, instead of a collection. |
| 960 | if (errors.size() == writeEntities.size()) { |
| 961 | Iterator<E> entityIterator = writeEntities.iterator(); |
| 962 | errors.stream() |
| 963 | .map(e -> ImmutablePair.of(e, entityIterator.next())) |
| 964 | .filter(p -> p.left.getCanonicalCode() != Status.OK.getCode().value()) |
| 965 | .forEach(p -> log.warn("Unable to {} {}: {} [{}]", |
| 966 | opType.name(), entryType, parseP4Error(p.getLeft()), |
| 967 | p.getRight().toString())); |
| 968 | } else { |
| 969 | log.error("Unable to reconcile error details to updates " + |
| 970 | "(sent {} updates, but device returned {} errors)", |
| 971 | entryType, writeEntities.size(), errors.size()); |
| 972 | errors.stream() |
| 973 | .filter(err -> err.getCanonicalCode() != Status.OK.getCode().value()) |
| 974 | .forEach(err -> log.warn("Unable to {} {} (unknown): {}", |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 975 | opType.name(), entryType, parseP4Error(err))); |
Carmelo Cascone | 5bc7e10 | 2018-02-18 18:27:55 -0800 | [diff] [blame] | 976 | } |
| 977 | } |
| 978 | |
| 979 | private List<P4RuntimeOuterClass.Error> extractWriteErrorDetails( |
| 980 | StatusRuntimeException ex) throws InvalidProtocolBufferException { |
| 981 | String statusString = ex.getStatus().getDescription(); |
| 982 | if (statusString == null) { |
| 983 | return Collections.emptyList(); |
| 984 | } |
| 985 | com.google.rpc.Status status = com.google.rpc.Status |
| 986 | .parseFrom(statusString.getBytes()); |
| 987 | return status.getDetailsList().stream() |
| 988 | .map(any -> { |
| 989 | try { |
| 990 | return any.unpack(P4RuntimeOuterClass.Error.class); |
| 991 | } catch (InvalidProtocolBufferException e) { |
| 992 | log.warn("Unable to unpack P4Runtime Error: {}", |
| 993 | any.toString()); |
| 994 | return null; |
| 995 | } |
| 996 | }) |
| 997 | .filter(Objects::nonNull) |
| 998 | .collect(Collectors.toList()); |
| 999 | |
| 1000 | } |
| 1001 | |
| 1002 | private String parseP4Error(P4RuntimeOuterClass.Error err) { |
| 1003 | return format("%s %s (%s code %d)%s", |
| 1004 | Status.fromCodeValue(err.getCanonicalCode()), |
| 1005 | err.getMessage(), |
| 1006 | err.getSpace(), |
| 1007 | err.getCode(), |
| 1008 | err.hasDetails() ? "\n" + err.getDetails().toString() : ""); |
| 1009 | } |
| 1010 | |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 1011 | private void checkGrpcException(StatusRuntimeException ex) { |
| 1012 | switch (ex.getStatus().getCode()) { |
| 1013 | case OK: |
| 1014 | break; |
| 1015 | case CANCELLED: |
| 1016 | break; |
| 1017 | case UNKNOWN: |
| 1018 | break; |
| 1019 | case INVALID_ARGUMENT: |
| 1020 | break; |
| 1021 | case DEADLINE_EXCEEDED: |
| 1022 | break; |
| 1023 | case NOT_FOUND: |
| 1024 | break; |
| 1025 | case ALREADY_EXISTS: |
| 1026 | break; |
| 1027 | case PERMISSION_DENIED: |
| 1028 | // Notify upper layers that this node is not master. |
| 1029 | controller.postEvent(new P4RuntimeEvent( |
| 1030 | P4RuntimeEvent.Type.ARBITRATION_RESPONSE, |
| 1031 | new ArbitrationResponse(deviceId, false))); |
| 1032 | break; |
| 1033 | case RESOURCE_EXHAUSTED: |
| 1034 | break; |
| 1035 | case FAILED_PRECONDITION: |
| 1036 | break; |
| 1037 | case ABORTED: |
| 1038 | break; |
| 1039 | case OUT_OF_RANGE: |
| 1040 | break; |
| 1041 | case UNIMPLEMENTED: |
| 1042 | break; |
| 1043 | case INTERNAL: |
| 1044 | break; |
| 1045 | case UNAVAILABLE: |
| 1046 | // Channel might be closed. |
| 1047 | controller.postEvent(new P4RuntimeEvent( |
| 1048 | P4RuntimeEvent.Type.CHANNEL_EVENT, |
| 1049 | new ChannelEvent(deviceId, ChannelEvent.Type.ERROR))); |
| 1050 | break; |
| 1051 | case DATA_LOSS: |
| 1052 | break; |
| 1053 | case UNAUTHENTICATED: |
| 1054 | break; |
| 1055 | default: |
| 1056 | break; |
| 1057 | } |
| 1058 | } |
| 1059 | |
| 1060 | private Uint128 bigIntegerToUint128(BigInteger value) { |
| 1061 | final byte[] arr = value.toByteArray(); |
| 1062 | final ByteBuffer bb = ByteBuffer.allocate(Long.BYTES * 2) |
| 1063 | .put(new byte[Long.BYTES * 2 - arr.length]) |
| 1064 | .put(arr); |
| 1065 | bb.rewind(); |
| 1066 | return Uint128.newBuilder() |
| 1067 | .setHigh(bb.getLong()) |
| 1068 | .setLow(bb.getLong()) |
| 1069 | .build(); |
| 1070 | } |
| 1071 | |
| 1072 | private BigInteger uint128ToBigInteger(Uint128 value) { |
| 1073 | return new BigInteger( |
| 1074 | ByteBuffer.allocate(Long.BYTES * 2) |
| 1075 | .putLong(value.getHigh()) |
| 1076 | .putLong(value.getLow()) |
| 1077 | .array()); |
| 1078 | } |
| 1079 | |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 1080 | /** |
| 1081 | * Handles messages received from the device on the stream channel. |
| 1082 | */ |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 1083 | private class StreamChannelResponseObserver |
| 1084 | implements StreamObserver<StreamMessageResponse> { |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 1085 | |
| 1086 | @Override |
| 1087 | public void onNext(StreamMessageResponse message) { |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 1088 | executorService.submit(() -> doNext(message)); |
| 1089 | } |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 1090 | |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 1091 | private void doNext(StreamMessageResponse message) { |
Carmelo Cascone | a966c34 | 2017-07-30 01:56:30 -0400 | [diff] [blame] | 1092 | try { |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 1093 | log.debug("Received message on stream channel from {}: {}", |
| 1094 | deviceId, message.getUpdateCase()); |
Carmelo Cascone | a966c34 | 2017-07-30 01:56:30 -0400 | [diff] [blame] | 1095 | switch (message.getUpdateCase()) { |
| 1096 | case PACKET: |
Carmelo Cascone | a966c34 | 2017-07-30 01:56:30 -0400 | [diff] [blame] | 1097 | doPacketIn(message.getPacket()); |
Andrea Campanella | 288b273 | 2017-07-28 14:16:16 +0200 | [diff] [blame] | 1098 | return; |
Carmelo Cascone | a966c34 | 2017-07-30 01:56:30 -0400 | [diff] [blame] | 1099 | case ARBITRATION: |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 1100 | doArbitrationResponse(message.getArbitration()); |
Carmelo Cascone | a966c34 | 2017-07-30 01:56:30 -0400 | [diff] [blame] | 1101 | return; |
| 1102 | default: |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 1103 | log.warn("Unrecognized stream message from {}: {}", |
| 1104 | deviceId, message.getUpdateCase()); |
Carmelo Cascone | a966c34 | 2017-07-30 01:56:30 -0400 | [diff] [blame] | 1105 | } |
| 1106 | } catch (Throwable ex) { |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 1107 | log.error("Exception while processing stream message from {}", |
| 1108 | deviceId, ex); |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 1109 | } |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 1110 | } |
| 1111 | |
| 1112 | @Override |
| 1113 | public void onError(Throwable throwable) { |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 1114 | log.warn("Error on stream channel for {}: {}", |
| 1115 | deviceId, Status.fromThrowable(throwable)); |
| 1116 | controller.postEvent(new P4RuntimeEvent( |
| 1117 | P4RuntimeEvent.Type.CHANNEL_EVENT, |
| 1118 | new ChannelEvent(deviceId, ChannelEvent.Type.ERROR))); |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 1119 | } |
| 1120 | |
| 1121 | @Override |
| 1122 | public void onCompleted() { |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 1123 | log.warn("Stream channel for {} has completed", deviceId); |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 1124 | controller.postEvent(new P4RuntimeEvent( |
| 1125 | P4RuntimeEvent.Type.CHANNEL_EVENT, |
| 1126 | new ChannelEvent(deviceId, ChannelEvent.Type.CLOSED))); |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 1127 | } |
| 1128 | } |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 1129 | } |