blob: 8a182a778e33a56af85ca4e35ef3d9b9cbdb27ae [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
Carmelo Casconec2be50a2019-04-10 00:15:39 -070026import static org.onosproject.drivers.p4runtime.P4RuntimeDriverUtils.getInterpreter;
27
Andrea Campanella378e21a2017-06-07 12:09:59 +020028/**
Carmelo Casconee3a7c742017-09-01 01:25:52 +020029 * Implementation of PacketProgrammable behaviour for P4Runtime.
Andrea Campanella378e21a2017-06-07 12:09:59 +020030 */
Carmelo Casconee5b28722018-06-22 17:28:28 +020031public class P4RuntimePacketProgrammable
32 extends AbstractP4RuntimeHandlerBehaviour
33 implements PacketProgrammable {
Carmelo Cascone59f57de2017-07-11 19:55:09 -040034
Andrea Campanella378e21a2017-06-07 12:09:59 +020035 @Override
36 public void emit(OutboundPacket packet) {
Frank Wangc672c982017-07-18 16:09:28 +080037
Carmelo Casconec32976e2019-04-08 14:50:52 -070038 if (!this.setupBehaviour("emit()")) {
Frank Wangc672c982017-07-18 16:09:28 +080039 return;
40 }
41
Carmelo Casconec2be50a2019-04-10 00:15:39 -070042 final PiPipelineInterpreter interpreter = getInterpreter(handler());
Ray Milkey74e59132018-01-17 15:24:52 -080043 if (interpreter == null) {
Carmelo Cascone158b8c42018-07-04 19:42:37 +020044 // Error logged by getInterpreter().
Frank Wangc672c982017-07-18 16:09:28 +080045 return;
46 }
47
48 try {
Carmelo Casconef3a1a382017-07-27 12:04:39 -040049 Collection<PiPacketOperation> operations = interpreter.mapOutboundPacket(packet);
Andrea Campanellafc1d34c2017-07-18 17:01:41 +020050 operations.forEach(piPacketOperation -> {
Carmelo Cascone2cad9ef2017-08-01 21:52:07 +020051 log.debug("Doing PiPacketOperation {}", piPacketOperation);
Carmelo Casconec2be50a2019-04-10 00:15:39 -070052 client.packetOut(p4DeviceId, piPacketOperation, pipeconf);
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}