blob: cc79cc15af1e635b1640c1970dd8b2eb3dbe8398 [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
Frank Wangc672c982017-07-18 16:09:28 +080040 final PiPipelineInterpreter interpreter = device.is(PiPipelineInterpreter.class)
41 ? device.as(PiPipelineInterpreter.class) : null;
Ray Milkey74e59132018-01-17 15:24:52 -080042 if (interpreter == null) {
Carmelo Casconee5b28722018-06-22 17:28:28 +020043 log.warn("Unable to get interpreter for {} with pipeconf {}, aborting emit operation",
44 deviceId, pipeconf.id());
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 Casconee5b28722018-06-22 17:28:28 +020052 getFutureWithDeadline(
53 client.packetOut(piPacketOperation, pipeconf),
54 "sending packet-out", false);
Andrea Campanellafc1d34c2017-07-18 17:01:41 +020055 });
Frank Wangc672c982017-07-18 16:09:28 +080056 } catch (PiPipelineInterpreter.PiInterpreterException e) {
Carmelo Casconee5b28722018-06-22 17:28:28 +020057 log.error("Unable to translate outbound packet for {} with pipeconf {}: {}",
58 deviceId, pipeconf.id(), e.getMessage());
Frank Wangc672c982017-07-18 16:09:28 +080059 }
Andrea Campanella378e21a2017-06-07 12:09:59 +020060 }
61}