Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [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 | |
Andrea Campanella | 0288c87 | 2017-08-07 18:32:51 +0200 | [diff] [blame^] | 17 | package org.onosproject.drivers.p4runtime; |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 18 | |
Frank Wang | c672c98 | 2017-07-18 16:09:28 +0800 | [diff] [blame] | 19 | import org.onosproject.net.Device; |
| 20 | import org.onosproject.net.DeviceId; |
| 21 | import org.onosproject.net.device.DeviceService; |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 22 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 23 | import org.onosproject.net.packet.OutboundPacket; |
| 24 | import org.onosproject.net.packet.PacketProgrammable; |
Frank Wang | c672c98 | 2017-07-18 16:09:28 +0800 | [diff] [blame] | 25 | import org.onosproject.net.pi.model.PiPipeconf; |
| 26 | import org.onosproject.net.pi.model.PiPipelineInterpreter; |
| 27 | import org.onosproject.net.pi.runtime.PiPacketOperation; |
| 28 | import org.onosproject.net.pi.runtime.PiPipeconfService; |
| 29 | import org.onosproject.p4runtime.api.P4RuntimeClient; |
| 30 | import org.onosproject.p4runtime.api.P4RuntimeController; |
| 31 | import org.slf4j.Logger; |
| 32 | import org.slf4j.LoggerFactory; |
| 33 | |
Andrea Campanella | fc1d34c | 2017-07-18 17:01:41 +0200 | [diff] [blame] | 34 | import java.util.Collection; |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 35 | |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 36 | /** |
| 37 | * Packet Programmable behaviour for BMv2 devices. |
| 38 | */ |
Andrea Campanella | 0288c87 | 2017-08-07 18:32:51 +0200 | [diff] [blame^] | 39 | public class P4RuntimePacketProgrammable extends AbstractHandlerBehaviour implements PacketProgrammable { |
Frank Wang | c672c98 | 2017-07-18 16:09:28 +0800 | [diff] [blame] | 40 | private final Logger log = LoggerFactory.getLogger(getClass()); |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 41 | |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 42 | @Override |
| 43 | public void emit(OutboundPacket packet) { |
Frank Wang | c672c98 | 2017-07-18 16:09:28 +0800 | [diff] [blame] | 44 | |
| 45 | DeviceId deviceId = handler().data().deviceId(); |
| 46 | P4RuntimeController controller = handler().get(P4RuntimeController.class); |
| 47 | if (!controller.hasClient(deviceId)) { |
| 48 | log.warn("Unable to find client for {}, aborting the sending packet", deviceId); |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | P4RuntimeClient client = controller.getClient(deviceId); |
| 53 | PiPipeconfService piPipeconfService = handler().get(PiPipeconfService.class); |
| 54 | |
| 55 | final PiPipeconf pipeconf; |
| 56 | if (piPipeconfService.ofDevice(deviceId).isPresent() && |
| 57 | piPipeconfService.getPipeconf(piPipeconfService.ofDevice(deviceId).get()).isPresent()) { |
| 58 | pipeconf = piPipeconfService.getPipeconf(piPipeconfService.ofDevice(deviceId).get()).get(); |
| 59 | } else { |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 60 | log.warn("Unable to get the pipeconf of {}", deviceId); |
Frank Wang | c672c98 | 2017-07-18 16:09:28 +0800 | [diff] [blame] | 61 | return; |
| 62 | } |
| 63 | |
| 64 | DeviceService deviceService = handler().get(DeviceService.class); |
| 65 | Device device = deviceService.getDevice(deviceId); |
| 66 | final PiPipelineInterpreter interpreter = device.is(PiPipelineInterpreter.class) |
| 67 | ? device.as(PiPipelineInterpreter.class) : null; |
Carmelo Cascone | 2cad9ef | 2017-08-01 21:52:07 +0200 | [diff] [blame] | 68 | if (!device.is(PiPipelineInterpreter.class)) { |
Frank Wang | c672c98 | 2017-07-18 16:09:28 +0800 | [diff] [blame] | 69 | log.warn("Device {} unable to instantiate interpreter of pipeconf {}", deviceId, pipeconf.id()); |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | try { |
Carmelo Cascone | f3a1a38 | 2017-07-27 12:04:39 -0400 | [diff] [blame] | 74 | Collection<PiPacketOperation> operations = interpreter.mapOutboundPacket(packet); |
Andrea Campanella | fc1d34c | 2017-07-18 17:01:41 +0200 | [diff] [blame] | 75 | operations.forEach(piPacketOperation -> { |
Carmelo Cascone | 2cad9ef | 2017-08-01 21:52:07 +0200 | [diff] [blame] | 76 | log.debug("Doing PiPacketOperation {}", piPacketOperation); |
Andrea Campanella | fc1d34c | 2017-07-18 17:01:41 +0200 | [diff] [blame] | 77 | client.packetOut(piPacketOperation, pipeconf); |
| 78 | }); |
Frank Wang | c672c98 | 2017-07-18 16:09:28 +0800 | [diff] [blame] | 79 | } catch (PiPipelineInterpreter.PiInterpreterException e) { |
| 80 | log.error("Interpreter of pipeconf {} was unable to translate outbound packet: {}", |
Andrea Campanella | fc1d34c | 2017-07-18 17:01:41 +0200 | [diff] [blame] | 81 | pipeconf.id(), e.getMessage()); |
Frank Wang | c672c98 | 2017-07-18 16:09:28 +0800 | [diff] [blame] | 82 | } |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 83 | } |
| 84 | } |