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