blob: 7122784b9e00cc41cb3657730aea673629a99252 [file] [log] [blame]
Andrea Campanella378e21a2017-06-07 12:09:59 +02001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Andrea Campanella378e21a2017-06-07 12:09:59 +02003 *
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
Andrea Campanella0288c872017-08-07 18:32:51 +020017package org.onosproject.drivers.p4runtime;
Andrea Campanella378e21a2017-06-07 12:09:59 +020018
Andrea Campanella378e21a2017-06-07 12:09:59 +020019import org.onosproject.net.packet.OutboundPacket;
20import org.onosproject.net.packet.PacketProgrammable;
Frank Wangc672c982017-07-18 16:09:28 +080021import org.onosproject.net.pi.model.PiPipelineInterpreter;
22import org.onosproject.net.pi.runtime.PiPacketOperation;
Frank Wangc672c982017-07-18 16:09:28 +080023
Andrea Campanellafc1d34c2017-07-18 17:01:41 +020024import java.util.Collection;
Andrea Campanella378e21a2017-06-07 12:09:59 +020025
Andrea Campanella378e21a2017-06-07 12:09:59 +020026/**
Carmelo Casconee3a7c742017-09-01 01:25:52 +020027 * Implementation of PacketProgrammable behaviour for P4Runtime.
Andrea Campanella378e21a2017-06-07 12:09:59 +020028 */
Carmelo Casconee5b28722018-06-22 17:28:28 +020029public class P4RuntimePacketProgrammable
30 extends AbstractP4RuntimeHandlerBehaviour
31 implements PacketProgrammable {
Carmelo Cascone59f57de2017-07-11 19:55:09 -040032
Andrea Campanella378e21a2017-06-07 12:09:59 +020033 @Override
34 public void emit(OutboundPacket packet) {
Frank Wangc672c982017-07-18 16:09:28 +080035
Carmelo Casconee3a7c742017-09-01 01:25:52 +020036 if (!this.setupBehaviour()) {
Frank Wangc672c982017-07-18 16:09:28 +080037 return;
38 }
39
Carmelo Cascone158b8c42018-07-04 19:42:37 +020040 final PiPipelineInterpreter interpreter = getInterpreter();
Ray Milkey74e59132018-01-17 15:24:52 -080041 if (interpreter == null) {
Carmelo Cascone158b8c42018-07-04 19:42:37 +020042 // Error logged by getInterpreter().
Frank Wangc672c982017-07-18 16:09:28 +080043 return;
44 }
45
46 try {
Carmelo Casconef3a1a382017-07-27 12:04:39 -040047 Collection<PiPacketOperation> operations = interpreter.mapOutboundPacket(packet);
Andrea Campanellafc1d34c2017-07-18 17:01:41 +020048 operations.forEach(piPacketOperation -> {
Carmelo Cascone2cad9ef2017-08-01 21:52:07 +020049 log.debug("Doing PiPacketOperation {}", piPacketOperation);
Carmelo Casconee5b28722018-06-22 17:28:28 +020050 getFutureWithDeadline(
51 client.packetOut(piPacketOperation, pipeconf),
52 "sending packet-out", false);
Andrea Campanellafc1d34c2017-07-18 17:01:41 +020053 });
Frank Wangc672c982017-07-18 16:09:28 +080054 } catch (PiPipelineInterpreter.PiInterpreterException e) {
Carmelo Casconee5b28722018-06-22 17:28:28 +020055 log.error("Unable to translate outbound packet for {} with pipeconf {}: {}",
56 deviceId, pipeconf.id(), e.getMessage());
Frank Wangc672c982017-07-18 16:09:28 +080057 }
Andrea Campanella378e21a2017-06-07 12:09:59 +020058 }
59}