wu | 2883c76 | 2017-07-20 16:24:48 +0800 | [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.provider.p4runtime.packet.impl; |
| 18 | |
| 19 | |
| 20 | import org.apache.felix.scr.annotations.Activate; |
| 21 | import org.apache.felix.scr.annotations.Component; |
| 22 | import org.apache.felix.scr.annotations.Deactivate; |
| 23 | import org.apache.felix.scr.annotations.Reference; |
| 24 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 25 | import org.onosproject.net.Device; |
| 26 | import org.onosproject.net.DeviceId; |
| 27 | import org.onosproject.net.device.DeviceService; |
| 28 | import org.onosproject.net.flow.TrafficTreatment; |
| 29 | import org.onosproject.net.packet.DefaultOutboundPacket; |
| 30 | import org.onosproject.net.packet.DefaultPacketContext; |
| 31 | import org.onosproject.net.packet.InboundPacket; |
| 32 | import org.onosproject.net.packet.OutboundPacket; |
Andrea Campanella | 288b273 | 2017-07-28 14:16:16 +0200 | [diff] [blame] | 33 | import org.onosproject.net.packet.PacketContext; |
wu | 2883c76 | 2017-07-20 16:24:48 +0800 | [diff] [blame] | 34 | import org.onosproject.net.packet.PacketProgrammable; |
| 35 | import org.onosproject.net.packet.PacketProvider; |
| 36 | import org.onosproject.net.packet.PacketProviderRegistry; |
| 37 | import org.onosproject.net.packet.PacketProviderService; |
Andrea Campanella | 288b273 | 2017-07-28 14:16:16 +0200 | [diff] [blame] | 38 | import org.onosproject.net.pi.model.PiPipelineInterpreter; |
| 39 | import org.onosproject.net.pi.runtime.PiPacketOperation; |
wu | 2883c76 | 2017-07-20 16:24:48 +0800 | [diff] [blame] | 40 | import org.onosproject.net.provider.AbstractProvider; |
| 41 | import org.onosproject.net.provider.ProviderId; |
| 42 | import org.onosproject.p4runtime.api.P4RuntimeController; |
| 43 | import org.onosproject.p4runtime.api.P4RuntimeEvent; |
| 44 | import org.onosproject.p4runtime.api.P4RuntimeEventListener; |
Andrea Campanella | 288b273 | 2017-07-28 14:16:16 +0200 | [diff] [blame] | 45 | import org.onosproject.p4runtime.api.P4RuntimePacketIn; |
wu | 2883c76 | 2017-07-20 16:24:48 +0800 | [diff] [blame] | 46 | import org.slf4j.Logger; |
| 47 | |
| 48 | import java.nio.ByteBuffer; |
| 49 | |
| 50 | import static org.onosproject.net.flow.DefaultTrafficTreatment.emptyTreatment; |
| 51 | import static org.slf4j.LoggerFactory.getLogger; |
| 52 | |
| 53 | /** |
| 54 | * Implementation of a packet provider for P4Runtime device. |
| 55 | */ |
| 56 | @Component(immediate = true) |
| 57 | public class P4RuntimePacketProvider extends AbstractProvider implements PacketProvider { |
| 58 | |
| 59 | private final Logger log = getLogger(getClass()); |
| 60 | |
| 61 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 62 | protected P4RuntimeController controller; |
| 63 | |
| 64 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 65 | protected PacketProviderRegistry providerRegistry; |
| 66 | |
| 67 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 68 | protected DeviceService deviceService; |
| 69 | |
| 70 | private PacketProviderService providerService; |
| 71 | |
| 72 | private InternalPacketListener packetListener = new InternalPacketListener(); |
| 73 | |
| 74 | /** |
| 75 | * Creates a new P4Runtime packet provider. |
| 76 | */ |
| 77 | public P4RuntimePacketProvider() { |
| 78 | super(new ProviderId("p4runtime", "org.onosproject.provider.p4runtime.packet")); |
| 79 | } |
| 80 | |
| 81 | @Activate |
| 82 | protected void activate() { |
| 83 | providerService = providerRegistry.register(this); |
| 84 | controller.addListener(packetListener); |
| 85 | log.info("Started"); |
| 86 | } |
| 87 | |
| 88 | @Deactivate |
| 89 | public void deactivate() { |
| 90 | controller.removeListener(packetListener); |
| 91 | providerRegistry.unregister(this); |
| 92 | providerService = null; |
| 93 | log.info("Stopped"); |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public void emit(OutboundPacket packet) { |
| 98 | if (packet != null) { |
| 99 | DeviceId deviceId = packet.sendThrough(); |
| 100 | Device device = deviceService.getDevice(deviceId); |
| 101 | if (device.is(PacketProgrammable.class)) { |
| 102 | PacketProgrammable packetProgrammable = device.as(PacketProgrammable.class); |
| 103 | packetProgrammable.emit(packet); |
| 104 | } else { |
| 105 | log.warn("No PacketProgrammable behavior for device {}", deviceId); |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Internal packet context implementation. |
| 112 | */ |
| 113 | private class P4RuntimePacketContext extends DefaultPacketContext { |
| 114 | |
| 115 | P4RuntimePacketContext(long time, InboundPacket inPkt, OutboundPacket outPkt, boolean block) { |
| 116 | super(time, inPkt, outPkt, block); |
| 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public void send() { |
| 121 | |
| 122 | if (this.block()) { |
| 123 | log.info("Unable to send, packet context is blocked"); |
| 124 | return; |
| 125 | } |
| 126 | |
| 127 | DeviceId deviceId = outPacket().sendThrough(); |
| 128 | ByteBuffer rawData = outPacket().data(); |
| 129 | |
| 130 | TrafficTreatment treatment; |
| 131 | if (outPacket().treatment() == null) { |
| 132 | treatment = (treatmentBuilder() == null) ? emptyTreatment() : treatmentBuilder().build(); |
| 133 | } else { |
| 134 | treatment = outPacket().treatment(); |
| 135 | } |
| 136 | |
Carmelo Cascone | 2cad9ef | 2017-08-01 21:52:07 +0200 | [diff] [blame^] | 137 | OutboundPacket outboundPacket = new DefaultOutboundPacket(deviceId, treatment, rawData); |
| 138 | log.debug("Processing outbound packet: {}", outboundPacket); |
| 139 | |
| 140 | emit(outboundPacket); |
wu | 2883c76 | 2017-07-20 16:24:48 +0800 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Internal packet listener to handle packet-in events received from the P4Runtime controller. |
| 146 | */ |
| 147 | private class InternalPacketListener implements P4RuntimeEventListener { |
| 148 | |
wu | 2883c76 | 2017-07-20 16:24:48 +0800 | [diff] [blame] | 149 | @Override |
| 150 | public void event(P4RuntimeEvent event) { |
Andrea Campanella | 288b273 | 2017-07-28 14:16:16 +0200 | [diff] [blame] | 151 | P4RuntimePacketIn eventSubject = (P4RuntimePacketIn) event.subject(); |
| 152 | if (deviceService.getDevice(eventSubject.deviceId()).is(PiPipelineInterpreter.class)) { |
| 153 | PiPacketOperation operation = eventSubject.packetOperation(); |
| 154 | try { |
| 155 | InboundPacket inPkt = deviceService.getDevice(eventSubject.deviceId()) |
| 156 | .as(PiPipelineInterpreter.class) |
| 157 | .mapInboundPacket(eventSubject.deviceId(), operation); |
| 158 | //Creating the corresponding outbound Packet |
| 159 | //FIXME Wrapping of bytebuffer might be optimized with .asReadOnlyByteBuffer() |
| 160 | OutboundPacket outPkt = new DefaultOutboundPacket(eventSubject.deviceId(), null, |
| 161 | ByteBuffer.wrap(operation.data().asArray())); |
Carmelo Cascone | 2cad9ef | 2017-08-01 21:52:07 +0200 | [diff] [blame^] | 162 | log.debug("Processing inbound packet: {}", inPkt.toString()); |
Andrea Campanella | 288b273 | 2017-07-28 14:16:16 +0200 | [diff] [blame] | 163 | //Creating PacketContext |
| 164 | PacketContext pktCtx = new P4RuntimePacketContext(System.currentTimeMillis(), inPkt, outPkt, false); |
| 165 | //Sendign the ctx up for processing. |
| 166 | providerService.processPacket(pktCtx); |
| 167 | } catch (PiPipelineInterpreter.PiInterpreterException e) { |
| 168 | log.error("Can't properly interpret the packetIn", e); |
| 169 | } |
| 170 | } |
wu | 2883c76 | 2017-07-20 16:24:48 +0800 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | } |