Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package org.onosproject.p4runtime.ctl; |
| 18 | |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableMap; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 20 | import com.google.protobuf.ByteString; |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 21 | import io.grpc.Context; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 22 | import io.grpc.ManagedChannel; |
| 23 | import io.grpc.Status; |
| 24 | import io.grpc.StatusRuntimeException; |
| 25 | import io.grpc.stub.StreamObserver; |
Andrea Campanella | 288b273 | 2017-07-28 14:16:16 +0200 | [diff] [blame] | 26 | import org.onlab.osgi.DefaultServiceDirectory; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 27 | import org.onosproject.net.DeviceId; |
Andrea Campanella | 432f718 | 2017-07-14 18:43:27 +0200 | [diff] [blame] | 28 | import org.onosproject.net.pi.model.PiPipeconf; |
| 29 | import org.onosproject.net.pi.runtime.PiPacketOperation; |
Andrea Campanella | 288b273 | 2017-07-28 14:16:16 +0200 | [diff] [blame] | 30 | import org.onosproject.net.pi.runtime.PiPipeconfService; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 31 | import org.onosproject.net.pi.runtime.PiTableEntry; |
| 32 | import org.onosproject.net.pi.runtime.PiTableId; |
| 33 | import org.onosproject.p4runtime.api.P4RuntimeClient; |
| 34 | import org.onosproject.p4runtime.api.P4RuntimeEvent; |
| 35 | import org.slf4j.Logger; |
| 36 | import p4.P4RuntimeGrpc; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 37 | import p4.P4RuntimeOuterClass.Entity; |
| 38 | import p4.P4RuntimeOuterClass.ForwardingPipelineConfig; |
| 39 | import p4.P4RuntimeOuterClass.MasterArbitrationUpdate; |
| 40 | import p4.P4RuntimeOuterClass.PacketIn; |
| 41 | import p4.P4RuntimeOuterClass.ReadRequest; |
| 42 | import p4.P4RuntimeOuterClass.ReadResponse; |
| 43 | import p4.P4RuntimeOuterClass.SetForwardingPipelineConfigRequest; |
| 44 | import p4.P4RuntimeOuterClass.StreamMessageRequest; |
| 45 | import p4.P4RuntimeOuterClass.StreamMessageResponse; |
| 46 | import p4.P4RuntimeOuterClass.TableEntry; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 47 | import p4.P4RuntimeOuterClass.Update; |
| 48 | import p4.P4RuntimeOuterClass.WriteRequest; |
Andrea Campanella | fc1d34c | 2017-07-18 17:01:41 +0200 | [diff] [blame] | 49 | import p4.config.P4InfoOuterClass.P4Info; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 50 | import p4.tmp.P4Config; |
| 51 | |
| 52 | import java.io.IOException; |
| 53 | import java.io.InputStream; |
Andrea Campanella | bf1301d | 2017-08-07 18:33:52 +0200 | [diff] [blame^] | 54 | import java.nio.ByteBuffer; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 55 | import java.util.Collection; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 56 | import java.util.Collections; |
| 57 | import java.util.Iterator; |
| 58 | import java.util.List; |
| 59 | import java.util.Map; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 60 | import java.util.concurrent.CompletableFuture; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 61 | import java.util.concurrent.Executor; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 62 | import java.util.concurrent.ExecutorService; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 63 | import java.util.concurrent.Executors; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 64 | import java.util.concurrent.TimeUnit; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 65 | import java.util.concurrent.locks.Lock; |
| 66 | import java.util.concurrent.locks.ReentrantLock; |
| 67 | import java.util.function.Supplier; |
| 68 | import java.util.stream.Collectors; |
| 69 | import java.util.stream.StreamSupport; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 70 | |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 71 | import static org.onlab.util.Tools.groupedThreads; |
| 72 | import static org.onosproject.net.pi.model.PiPipeconf.ExtensionType; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 73 | import static org.slf4j.LoggerFactory.getLogger; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 74 | import static p4.P4RuntimeOuterClass.Entity.EntityCase.TABLE_ENTRY; |
Andrea Campanella | fc1d34c | 2017-07-18 17:01:41 +0200 | [diff] [blame] | 75 | import static p4.P4RuntimeOuterClass.PacketOut; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 76 | import static p4.P4RuntimeOuterClass.SetForwardingPipelineConfigRequest.Action.VERIFY_AND_COMMIT; |
| 77 | |
| 78 | /** |
| 79 | * Implementation of a P4Runtime client. |
| 80 | */ |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 81 | public final class P4RuntimeClientImpl implements P4RuntimeClient { |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 82 | |
| 83 | private static final int DEADLINE_SECONDS = 15; |
| 84 | |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 85 | // FIXME: use static election ID, since mastership arbitration is not yet support on BMv2 or Tofino. |
| 86 | private static final int ELECTION_ID = 1; |
| 87 | |
| 88 | private static final Map<WriteOperationType, Update.Type> UPDATE_TYPES = ImmutableMap.of( |
| 89 | WriteOperationType.UNSPECIFIED, Update.Type.UNSPECIFIED, |
| 90 | WriteOperationType.INSERT, Update.Type.INSERT, |
| 91 | WriteOperationType.MODIFY, Update.Type.MODIFY, |
| 92 | WriteOperationType.DELETE, Update.Type.DELETE |
| 93 | ); |
| 94 | |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 95 | private final Logger log = getLogger(getClass()); |
| 96 | |
| 97 | private final DeviceId deviceId; |
Carmelo Cascone | f423bec | 2017-08-30 01:56:25 +0200 | [diff] [blame] | 98 | private final long p4DeviceId; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 99 | private final P4RuntimeControllerImpl controller; |
| 100 | private final P4RuntimeGrpc.P4RuntimeBlockingStub blockingStub; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 101 | private final Context.CancellableContext cancellableContext; |
| 102 | private final ExecutorService executorService; |
| 103 | private final Executor contextExecutor; |
| 104 | private final Lock writeLock = new ReentrantLock(); |
| 105 | private final StreamObserver<StreamMessageRequest> streamRequestObserver; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 106 | |
| 107 | |
Carmelo Cascone | f423bec | 2017-08-30 01:56:25 +0200 | [diff] [blame] | 108 | P4RuntimeClientImpl(DeviceId deviceId, long p4DeviceId, ManagedChannel channel, |
| 109 | P4RuntimeControllerImpl controller) { |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 110 | this.deviceId = deviceId; |
| 111 | this.p4DeviceId = p4DeviceId; |
| 112 | this.controller = controller; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 113 | this.cancellableContext = Context.current().withCancellation(); |
Carmelo Cascone | a966c34 | 2017-07-30 01:56:30 -0400 | [diff] [blame] | 114 | this.executorService = Executors.newFixedThreadPool(15, groupedThreads( |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 115 | "onos/p4runtime-client-" + deviceId.toString(), |
| 116 | deviceId.toString() + "-%d")); |
| 117 | this.contextExecutor = this.cancellableContext.fixedContextExecutor(executorService); |
Carmelo Cascone | 2cad9ef | 2017-08-01 21:52:07 +0200 | [diff] [blame] | 118 | //TODO Investigate deadline or timeout in supplyInContext Method |
| 119 | this.blockingStub = P4RuntimeGrpc.newBlockingStub(channel); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 120 | P4RuntimeGrpc.P4RuntimeStub asyncStub = P4RuntimeGrpc.newStub(channel); |
| 121 | this.streamRequestObserver = asyncStub.streamChannel(new StreamChannelResponseObserver()); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Executes the given task (supplier) in the gRPC context executor of this client, such that if the context is |
| 126 | * cancelled (e.g. client shutdown) the RPC is automatically cancelled. |
| 127 | * <p> |
| 128 | * Important: Tasks submitted in parallel by different threads are forced executed sequentially. |
| 129 | * <p> |
| 130 | */ |
Carmelo Cascone | 2cad9ef | 2017-08-01 21:52:07 +0200 | [diff] [blame] | 131 | private <U> CompletableFuture<U> supplyInContext(Supplier<U> supplier, String opDescription) { |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 132 | return CompletableFuture.supplyAsync(() -> { |
| 133 | // TODO: explore a more relaxed locking strategy. |
| 134 | writeLock.lock(); |
| 135 | try { |
| 136 | return supplier.get(); |
Carmelo Cascone | a966c34 | 2017-07-30 01:56:30 -0400 | [diff] [blame] | 137 | } catch (Throwable ex) { |
Carmelo Cascone | 2cad9ef | 2017-08-01 21:52:07 +0200 | [diff] [blame] | 138 | log.error("Exception in P4Runtime client of {}, executing {}", deviceId, opDescription, ex); |
Carmelo Cascone | a966c34 | 2017-07-30 01:56:30 -0400 | [diff] [blame] | 139 | throw ex; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 140 | } finally { |
| 141 | writeLock.unlock(); |
| 142 | } |
| 143 | }, contextExecutor); |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public CompletableFuture<Boolean> initStreamChannel() { |
Carmelo Cascone | 2cad9ef | 2017-08-01 21:52:07 +0200 | [diff] [blame] | 148 | return supplyInContext(this::doInitStreamChannel, "initStreamChannel"); |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 149 | } |
| 150 | |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 151 | @Override |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 152 | public CompletableFuture<Boolean> setPipelineConfig(PiPipeconf pipeconf, ExtensionType targetConfigExtType) { |
Carmelo Cascone | 2cad9ef | 2017-08-01 21:52:07 +0200 | [diff] [blame] | 153 | return supplyInContext(() -> doSetPipelineConfig(pipeconf, targetConfigExtType), "setPipelineConfig"); |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | @Override |
Andrea Campanella | bf1301d | 2017-08-07 18:33:52 +0200 | [diff] [blame^] | 157 | public CompletableFuture<Boolean> setPipelineConfig(PiPipeconf pipeconf, ByteBuffer byteBuffer) { |
| 158 | return supplyInContext(() -> doSetPipelineConfig(pipeconf, byteBuffer), "setPipelineConfigByteString"); |
| 159 | } |
| 160 | |
| 161 | @Override |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 162 | public CompletableFuture<Boolean> writeTableEntries(Collection<PiTableEntry> piTableEntries, |
| 163 | WriteOperationType opType, PiPipeconf pipeconf) { |
Carmelo Cascone | 2cad9ef | 2017-08-01 21:52:07 +0200 | [diff] [blame] | 164 | return supplyInContext(() -> doWriteTableEntries(piTableEntries, opType, pipeconf), |
Andrea Campanella | bf1301d | 2017-08-07 18:33:52 +0200 | [diff] [blame^] | 165 | "writeTableEntries-" + opType.name()); |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | @Override |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 169 | public CompletableFuture<Collection<PiTableEntry>> dumpTable(PiTableId piTableId, PiPipeconf pipeconf) { |
Carmelo Cascone | 2cad9ef | 2017-08-01 21:52:07 +0200 | [diff] [blame] | 170 | return supplyInContext(() -> doDumpTable(piTableId, pipeconf), "dumpTable-" + piTableId); |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | @Override |
Andrea Campanella | 432f718 | 2017-07-14 18:43:27 +0200 | [diff] [blame] | 174 | public CompletableFuture<Boolean> packetOut(PiPacketOperation packet, PiPipeconf pipeconf) { |
Carmelo Cascone | 2cad9ef | 2017-08-01 21:52:07 +0200 | [diff] [blame] | 175 | return supplyInContext(() -> doPacketOut(packet, pipeconf), "packetOut"); |
Andrea Campanella | 432f718 | 2017-07-14 18:43:27 +0200 | [diff] [blame] | 176 | } |
| 177 | |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 178 | /* Blocking method implementations below */ |
| 179 | |
| 180 | private boolean doInitStreamChannel() { |
| 181 | // To listen for packets and other events, we need to start the RPC. |
| 182 | // Here we do it by sending a master arbitration update. |
| 183 | log.info("initializing stream chanel on {}...", deviceId); |
| 184 | if (!doArbitrationUpdate()) { |
| 185 | log.warn("Unable to initialize stream channel for {}", deviceId); |
| 186 | return false; |
| 187 | } else { |
| 188 | return true; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | private boolean doArbitrationUpdate() { |
| 193 | log.info("Sending arbitration update to {}...", deviceId); |
| 194 | StreamMessageRequest requestMsg = StreamMessageRequest.newBuilder() |
| 195 | .setArbitration(MasterArbitrationUpdate.newBuilder() |
Andrea Campanella | bf1301d | 2017-08-07 18:33:52 +0200 | [diff] [blame^] | 196 | .setDeviceId(p4DeviceId) |
| 197 | .build()) |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 198 | .build(); |
| 199 | try { |
| 200 | streamRequestObserver.onNext(requestMsg); |
| 201 | return true; |
| 202 | } catch (StatusRuntimeException e) { |
| 203 | log.warn("Arbitration update failed for {}: {}", deviceId, e); |
| 204 | return false; |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | private boolean doSetPipelineConfig(PiPipeconf pipeconf, ExtensionType targetConfigExtType) { |
| 209 | |
| 210 | log.info("Setting pipeline config for {} to {} using {}...", deviceId, pipeconf.id(), targetConfigExtType); |
| 211 | |
| 212 | P4Info p4Info = PipeconfHelper.getP4Info(pipeconf); |
| 213 | if (p4Info == null) { |
| 214 | // Problem logged by PipeconfHelper. |
| 215 | return false; |
| 216 | } |
| 217 | |
Andrea Campanella | bf1301d | 2017-08-07 18:33:52 +0200 | [diff] [blame^] | 218 | if (!pipeconf.extension(targetConfigExtType).isPresent()) { |
| 219 | log.warn("Missing extension {} in pipeconf {}", targetConfigExtType, pipeconf.id()); |
| 220 | return false; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 221 | } |
| 222 | |
Andrea Campanella | bf1301d | 2017-08-07 18:33:52 +0200 | [diff] [blame^] | 223 | InputStream targetConfig = pipeconf.extension(targetConfigExtType).get(); |
| 224 | try { |
| 225 | return sendPipelineConfig(p4Info, ByteString.readFrom(targetConfig)); |
| 226 | } catch (IOException ex) { |
| 227 | log.warn("Unable to load target-specific config for {}: {}", deviceId, ex.getMessage()); |
| 228 | return false; |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | private boolean doSetPipelineConfig(PiPipeconf pipeconf, ByteBuffer byteBuffer) { |
| 233 | |
| 234 | log.info("Setting pipeline config for {} to {} using bytebuffer...", deviceId, pipeconf.id()); |
| 235 | |
| 236 | P4Info p4Info = PipeconfHelper.getP4Info(pipeconf); |
| 237 | return p4Info != null && byteBuffer != null |
| 238 | && sendPipelineConfig(p4Info, ByteString.copyFrom(byteBuffer)); |
| 239 | } |
| 240 | |
| 241 | private boolean sendPipelineConfig(P4Info p4Info, ByteString deviceData) { |
| 242 | |
| 243 | //p4 device config |
| 244 | P4Config.P4DeviceConfig p4DeviceConfigMsg = P4Config.P4DeviceConfig |
| 245 | .newBuilder() |
| 246 | .setExtras(P4Config.P4DeviceConfig.Extras.getDefaultInstance()) |
| 247 | .setReassign(true) |
| 248 | .setDeviceData(deviceData) |
| 249 | .build(); |
| 250 | //pipeline config message |
| 251 | ForwardingPipelineConfig pipelineConfig = ForwardingPipelineConfig |
| 252 | .newBuilder() |
| 253 | .setDeviceId(p4DeviceId) |
| 254 | .setP4Info(p4Info) |
| 255 | .setP4DeviceConfig(p4DeviceConfigMsg.toByteString()) |
| 256 | .build(); |
| 257 | |
| 258 | //set forwarding pipeline config request |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 259 | SetForwardingPipelineConfigRequest request = SetForwardingPipelineConfigRequest |
| 260 | .newBuilder() |
| 261 | .setAction(VERIFY_AND_COMMIT) |
Andrea Campanella | bf1301d | 2017-08-07 18:33:52 +0200 | [diff] [blame^] | 262 | .addConfigs(pipelineConfig) |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 263 | .build(); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 264 | try { |
| 265 | this.blockingStub.setForwardingPipelineConfig(request); |
| 266 | |
| 267 | } catch (StatusRuntimeException ex) { |
| 268 | log.warn("Unable to set pipeline config for {}: {}", deviceId, ex.getMessage()); |
| 269 | return false; |
| 270 | } |
| 271 | |
| 272 | return true; |
| 273 | } |
| 274 | |
Andrea Campanella | bf1301d | 2017-08-07 18:33:52 +0200 | [diff] [blame^] | 275 | |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 276 | private boolean doWriteTableEntries(Collection<PiTableEntry> piTableEntries, WriteOperationType opType, |
| 277 | PiPipeconf pipeconf) { |
| 278 | |
| 279 | WriteRequest.Builder writeRequestBuilder = WriteRequest.newBuilder(); |
| 280 | |
| 281 | Collection<Update> updateMsgs = TableEntryEncoder.encode(piTableEntries, pipeconf) |
| 282 | .stream() |
| 283 | .map(tableEntryMsg -> |
Andrea Campanella | bf1301d | 2017-08-07 18:33:52 +0200 | [diff] [blame^] | 284 | Update.newBuilder() |
| 285 | .setEntity(Entity.newBuilder() |
| 286 | .setTableEntry(tableEntryMsg) |
| 287 | .build()) |
| 288 | .setType(UPDATE_TYPES.get(opType)) |
| 289 | .build()) |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 290 | .collect(Collectors.toList()); |
| 291 | |
| 292 | if (updateMsgs.size() == 0) { |
| 293 | return true; |
| 294 | } |
| 295 | |
| 296 | writeRequestBuilder |
| 297 | .setDeviceId(p4DeviceId) |
Carmelo Cascone | 2cad9ef | 2017-08-01 21:52:07 +0200 | [diff] [blame] | 298 | /* PI ignores this ElectionId, commenting out for now. |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 299 | .setElectionId(Uint128.newBuilder() |
| 300 | .setHigh(0) |
| 301 | .setLow(ELECTION_ID) |
Carmelo Cascone | 2cad9ef | 2017-08-01 21:52:07 +0200 | [diff] [blame] | 302 | .build()) */ |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 303 | .addAllUpdates(updateMsgs) |
| 304 | .build(); |
| 305 | |
| 306 | try { |
| 307 | blockingStub.write(writeRequestBuilder.build()); |
| 308 | return true; |
| 309 | } catch (StatusRuntimeException e) { |
| 310 | log.warn("Unable to write table entries ({}): {}", opType, e.getMessage()); |
| 311 | return false; |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | private Collection<PiTableEntry> doDumpTable(PiTableId piTableId, PiPipeconf pipeconf) { |
| 316 | |
Carmelo Cascone | 9f00770 | 2017-08-24 13:30:51 +0200 | [diff] [blame] | 317 | log.debug("Dumping table {} from {} (pipeconf {})...", piTableId, deviceId, pipeconf.id()); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 318 | |
| 319 | P4InfoBrowser browser = PipeconfHelper.getP4InfoBrowser(pipeconf); |
| 320 | int tableId; |
| 321 | try { |
| 322 | tableId = browser.tables().getByName(piTableId.id()).getPreamble().getId(); |
| 323 | } catch (P4InfoBrowser.NotFoundException e) { |
| 324 | log.warn("Unable to dump table: {}", e.getMessage()); |
| 325 | return Collections.emptyList(); |
| 326 | } |
| 327 | |
| 328 | ReadRequest requestMsg = ReadRequest.newBuilder() |
| 329 | .setDeviceId(p4DeviceId) |
| 330 | .addEntities(Entity.newBuilder() |
Andrea Campanella | bf1301d | 2017-08-07 18:33:52 +0200 | [diff] [blame^] | 331 | .setTableEntry(TableEntry.newBuilder() |
| 332 | .setTableId(tableId) |
| 333 | .build()) |
| 334 | .build()) |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 335 | .build(); |
| 336 | |
| 337 | Iterator<ReadResponse> responses; |
| 338 | try { |
| 339 | responses = blockingStub.read(requestMsg); |
| 340 | } catch (StatusRuntimeException e) { |
| 341 | log.warn("Unable to dump table: {}", e.getMessage()); |
| 342 | return Collections.emptyList(); |
| 343 | } |
| 344 | |
| 345 | Iterable<ReadResponse> responseIterable = () -> responses; |
| 346 | List<TableEntry> tableEntryMsgs = StreamSupport |
| 347 | .stream(responseIterable.spliterator(), false) |
| 348 | .map(ReadResponse::getEntitiesList) |
| 349 | .flatMap(List::stream) |
| 350 | .filter(entity -> entity.getEntityCase() == TABLE_ENTRY) |
| 351 | .map(Entity::getTableEntry) |
| 352 | .collect(Collectors.toList()); |
| 353 | |
Carmelo Cascone | 9f00770 | 2017-08-24 13:30:51 +0200 | [diff] [blame] | 354 | log.debug("Retrieved {} entries from table {} on {}...", tableEntryMsgs.size(), piTableId, deviceId); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 355 | |
| 356 | return TableEntryEncoder.decode(tableEntryMsgs, pipeconf); |
| 357 | } |
| 358 | |
Andrea Campanella | fc1d34c | 2017-07-18 17:01:41 +0200 | [diff] [blame] | 359 | private boolean doPacketOut(PiPacketOperation packet, PiPipeconf pipeconf) { |
| 360 | try { |
| 361 | //encode the PiPacketOperation into a PacketOut |
| 362 | PacketOut packetOut = PacketIOCodec.encodePacketOut(packet, pipeconf); |
| 363 | |
| 364 | //Build the request |
| 365 | StreamMessageRequest packetOutRequest = StreamMessageRequest |
| 366 | .newBuilder().setPacket(packetOut).build(); |
| 367 | |
| 368 | //Send the request |
| 369 | streamRequestObserver.onNext(packetOutRequest); |
| 370 | |
| 371 | } catch (P4InfoBrowser.NotFoundException e) { |
| 372 | log.error("Cant find expected metadata in p4Info file. {}", e.getMessage()); |
| 373 | log.debug("Exception", e); |
| 374 | return false; |
| 375 | } |
| 376 | return true; |
| 377 | } |
| 378 | |
Carmelo Cascone | a966c34 | 2017-07-30 01:56:30 -0400 | [diff] [blame] | 379 | private void doPacketIn(PacketIn packetInMsg) { |
| 380 | |
| 381 | // Retrieve the pipeconf for this client's device. |
| 382 | PiPipeconfService pipeconfService = DefaultServiceDirectory.getService(PiPipeconfService.class); |
| 383 | if (pipeconfService == null) { |
| 384 | throw new IllegalStateException("PiPipeconfService is null. Can't handle packet in."); |
| 385 | } |
| 386 | final PiPipeconf pipeconf; |
| 387 | if (pipeconfService.ofDevice(deviceId).isPresent() && |
| 388 | pipeconfService.getPipeconf(pipeconfService.ofDevice(deviceId).get()).isPresent()) { |
| 389 | pipeconf = pipeconfService.getPipeconf(pipeconfService.ofDevice(deviceId).get()).get(); |
| 390 | } else { |
| 391 | log.warn("Unable to get pipeconf of {}. Can't handle packet in", deviceId); |
| 392 | return; |
| 393 | } |
| 394 | // Decode packet message and post event. |
Carmelo Cascone | 2cad9ef | 2017-08-01 21:52:07 +0200 | [diff] [blame] | 395 | PiPacketOperation packetOperation = PacketIOCodec.decodePacketIn(packetInMsg, pipeconf); |
| 396 | DefaultPacketIn packetInEventSubject = new DefaultPacketIn(deviceId, packetOperation); |
| 397 | P4RuntimeEvent event = new P4RuntimeEvent(P4RuntimeEvent.Type.PACKET_IN, packetInEventSubject); |
Carmelo Cascone | a966c34 | 2017-07-30 01:56:30 -0400 | [diff] [blame] | 398 | log.debug("Received packet in: {}", event); |
| 399 | controller.postEvent(event); |
| 400 | } |
| 401 | |
| 402 | private void doArbitrationUpdateFromDevice(MasterArbitrationUpdate arbitrationMsg) { |
| 403 | |
| 404 | log.warn("Received arbitration update from {} (NOT IMPLEMENTED YET): {}", deviceId, arbitrationMsg); |
| 405 | } |
| 406 | |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 407 | /** |
| 408 | * Returns the internal P4 device ID associated with this client. |
| 409 | * |
| 410 | * @return P4 device ID |
| 411 | */ |
Carmelo Cascone | f423bec | 2017-08-30 01:56:25 +0200 | [diff] [blame] | 412 | public long p4DeviceId() { |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 413 | return p4DeviceId; |
| 414 | } |
| 415 | |
| 416 | /** |
| 417 | * For testing purpose only. TODO: remove before release. |
| 418 | * |
| 419 | * @return blocking stub |
| 420 | */ |
| 421 | public P4RuntimeGrpc.P4RuntimeBlockingStub blockingStub() { |
| 422 | return this.blockingStub; |
| 423 | } |
| 424 | |
Andrea Campanella | fc1d34c | 2017-07-18 17:01:41 +0200 | [diff] [blame] | 425 | |
Andrea Campanella | 432f718 | 2017-07-14 18:43:27 +0200 | [diff] [blame] | 426 | @Override |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 427 | public void shutdown() { |
| 428 | |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 429 | log.info("Shutting down client for {}...", deviceId); |
| 430 | |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 431 | writeLock.lock(); |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 432 | try { |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 433 | if (streamRequestObserver != null) { |
| 434 | streamRequestObserver.onCompleted(); |
| 435 | cancellableContext.cancel(new InterruptedException("Requested client shutdown")); |
| 436 | } |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 437 | |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 438 | this.executorService.shutdown(); |
| 439 | try { |
| 440 | executorService.awaitTermination(5, TimeUnit.SECONDS); |
| 441 | } catch (InterruptedException e) { |
| 442 | log.warn("Executor service didn't shutdown in time."); |
| 443 | } |
| 444 | } finally { |
| 445 | writeLock.unlock(); |
| 446 | } |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 447 | } |
| 448 | |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 449 | /** |
| 450 | * Handles messages received from the device on the stream channel. |
| 451 | */ |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 452 | private class StreamChannelResponseObserver implements StreamObserver<StreamMessageResponse> { |
| 453 | |
| 454 | @Override |
| 455 | public void onNext(StreamMessageResponse message) { |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 456 | executorService.submit(() -> doNext(message)); |
| 457 | } |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 458 | |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 459 | private void doNext(StreamMessageResponse message) { |
Carmelo Cascone | a966c34 | 2017-07-30 01:56:30 -0400 | [diff] [blame] | 460 | try { |
Andrea Campanella | 0288c87 | 2017-08-07 18:32:51 +0200 | [diff] [blame] | 461 | log.debug("Received message on stream channel from {}: {}", deviceId, message.getUpdateCase()); |
Carmelo Cascone | a966c34 | 2017-07-30 01:56:30 -0400 | [diff] [blame] | 462 | switch (message.getUpdateCase()) { |
| 463 | case PACKET: |
| 464 | // Packet-in |
| 465 | doPacketIn(message.getPacket()); |
Andrea Campanella | 288b273 | 2017-07-28 14:16:16 +0200 | [diff] [blame] | 466 | return; |
Carmelo Cascone | a966c34 | 2017-07-30 01:56:30 -0400 | [diff] [blame] | 467 | case ARBITRATION: |
| 468 | doArbitrationUpdateFromDevice(message.getArbitration()); |
| 469 | return; |
| 470 | default: |
| 471 | log.warn("Unrecognized stream message from {}: {}", deviceId, message.getUpdateCase()); |
| 472 | } |
| 473 | } catch (Throwable ex) { |
| 474 | log.error("Exception while processing stream channel message from {}", deviceId, ex); |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 475 | } |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | @Override |
| 479 | public void onError(Throwable throwable) { |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 480 | log.warn("Error on stream channel for {}: {}", deviceId, Status.fromThrowable(throwable)); |
| 481 | // FIXME: we might want to recreate the channel. |
| 482 | // In general, we want to be robust against any transient error and, if the channel is open, make sure the |
| 483 | // stream channel is always on. |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | @Override |
| 487 | public void onCompleted() { |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 488 | log.warn("Stream channel for {} has completed", deviceId); |
| 489 | // FIXME: same concern as before. |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 490 | } |
| 491 | } |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 492 | } |