wu | 2883c76 | 2017-07-20 16:24:48 +0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
wu | 2883c76 | 2017-07-20 16:24:48 +0800 | [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 | |
| 17 | package org.onosproject.provider.p4runtime.packet.impl; |
| 18 | |
Carmelo Cascone | 080969a | 2019-08-27 16:06:55 -0700 | [diff] [blame] | 19 | import org.onlab.packet.EthType; |
pierventre | 283e217 | 2021-02-05 17:56:22 +0100 | [diff] [blame^] | 20 | import org.onlab.util.PredictableExecutor; |
| 21 | import org.onosproject.cfg.ComponentConfigService; |
Andrea Campanella | 14e196d | 2017-07-24 18:11:36 +0200 | [diff] [blame] | 22 | import org.onosproject.mastership.MastershipService; |
wu | 2883c76 | 2017-07-20 16:24:48 +0800 | [diff] [blame] | 23 | import org.onosproject.net.Device; |
| 24 | import org.onosproject.net.DeviceId; |
| 25 | import org.onosproject.net.device.DeviceService; |
| 26 | import org.onosproject.net.flow.TrafficTreatment; |
| 27 | import org.onosproject.net.packet.DefaultOutboundPacket; |
| 28 | import org.onosproject.net.packet.DefaultPacketContext; |
| 29 | import org.onosproject.net.packet.InboundPacket; |
| 30 | import org.onosproject.net.packet.OutboundPacket; |
Andrea Campanella | 288b273 | 2017-07-28 14:16:16 +0200 | [diff] [blame] | 31 | import org.onosproject.net.packet.PacketContext; |
wu | 2883c76 | 2017-07-20 16:24:48 +0800 | [diff] [blame] | 32 | import org.onosproject.net.packet.PacketProgrammable; |
| 33 | import org.onosproject.net.packet.PacketProvider; |
| 34 | import org.onosproject.net.packet.PacketProviderRegistry; |
| 35 | import org.onosproject.net.packet.PacketProviderService; |
Andrea Campanella | 288b273 | 2017-07-28 14:16:16 +0200 | [diff] [blame] | 36 | import org.onosproject.net.pi.model.PiPipelineInterpreter; |
| 37 | import org.onosproject.net.pi.runtime.PiPacketOperation; |
wu | 2883c76 | 2017-07-20 16:24:48 +0800 | [diff] [blame] | 38 | import org.onosproject.net.provider.AbstractProvider; |
| 39 | import org.onosproject.net.provider.ProviderId; |
| 40 | import org.onosproject.p4runtime.api.P4RuntimeController; |
| 41 | import org.onosproject.p4runtime.api.P4RuntimeEvent; |
| 42 | import org.onosproject.p4runtime.api.P4RuntimeEventListener; |
Andrea Campanella | 288b273 | 2017-07-28 14:16:16 +0200 | [diff] [blame] | 43 | import org.onosproject.p4runtime.api.P4RuntimePacketIn; |
pierventre | 283e217 | 2021-02-05 17:56:22 +0100 | [diff] [blame^] | 44 | import org.osgi.service.component.ComponentContext; |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 45 | import org.osgi.service.component.annotations.Activate; |
| 46 | import org.osgi.service.component.annotations.Component; |
| 47 | import org.osgi.service.component.annotations.Deactivate; |
pierventre | 283e217 | 2021-02-05 17:56:22 +0100 | [diff] [blame^] | 48 | import org.osgi.service.component.annotations.Modified; |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 49 | import org.osgi.service.component.annotations.Reference; |
| 50 | import org.osgi.service.component.annotations.ReferenceCardinality; |
wu | 2883c76 | 2017-07-20 16:24:48 +0800 | [diff] [blame] | 51 | import org.slf4j.Logger; |
| 52 | |
| 53 | import java.nio.ByteBuffer; |
pierventre | 283e217 | 2021-02-05 17:56:22 +0100 | [diff] [blame^] | 54 | import java.util.Dictionary; |
| 55 | import java.util.concurrent.ExecutorService; |
wu | 2883c76 | 2017-07-20 16:24:48 +0800 | [diff] [blame] | 56 | |
pierventre | 283e217 | 2021-02-05 17:56:22 +0100 | [diff] [blame^] | 57 | import static com.google.common.base.Strings.isNullOrEmpty; |
| 58 | import static org.onlab.util.Tools.get; |
| 59 | import static org.onlab.util.Tools.groupedThreads; |
wu | 2883c76 | 2017-07-20 16:24:48 +0800 | [diff] [blame] | 60 | import static org.onosproject.net.flow.DefaultTrafficTreatment.emptyTreatment; |
pierventre | 283e217 | 2021-02-05 17:56:22 +0100 | [diff] [blame^] | 61 | import static org.onosproject.provider.p4runtime.packet.impl.OsgiPropertyConstants.P4RUNTIME_PACKET_PROVIDER_WORKERS; |
| 62 | import static org.onosproject.provider.p4runtime.packet.impl.OsgiPropertyConstants.P4RUNTIME_PACKET_PROVIDER_WORKERS_DEFAULT; |
wu | 2883c76 | 2017-07-20 16:24:48 +0800 | [diff] [blame] | 63 | import static org.slf4j.LoggerFactory.getLogger; |
| 64 | |
| 65 | /** |
| 66 | * Implementation of a packet provider for P4Runtime device. |
| 67 | */ |
pierventre | 283e217 | 2021-02-05 17:56:22 +0100 | [diff] [blame^] | 68 | @Component(immediate = true, |
| 69 | property = { |
| 70 | P4RUNTIME_PACKET_PROVIDER_WORKERS + ":Integer=" + P4RUNTIME_PACKET_PROVIDER_WORKERS_DEFAULT, |
| 71 | }) |
wu | 2883c76 | 2017-07-20 16:24:48 +0800 | [diff] [blame] | 72 | public class P4RuntimePacketProvider extends AbstractProvider implements PacketProvider { |
| 73 | |
| 74 | private final Logger log = getLogger(getClass()); |
| 75 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 76 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
wu | 2883c76 | 2017-07-20 16:24:48 +0800 | [diff] [blame] | 77 | protected P4RuntimeController controller; |
| 78 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 79 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
wu | 2883c76 | 2017-07-20 16:24:48 +0800 | [diff] [blame] | 80 | protected PacketProviderRegistry providerRegistry; |
| 81 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 82 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
wu | 2883c76 | 2017-07-20 16:24:48 +0800 | [diff] [blame] | 83 | protected DeviceService deviceService; |
| 84 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 85 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Andrea Campanella | 14e196d | 2017-07-24 18:11:36 +0200 | [diff] [blame] | 86 | protected MastershipService mastershipService; |
| 87 | |
pierventre | 283e217 | 2021-02-05 17:56:22 +0100 | [diff] [blame^] | 88 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 89 | protected ComponentConfigService cfgService; |
| 90 | |
wu | 2883c76 | 2017-07-20 16:24:48 +0800 | [diff] [blame] | 91 | private PacketProviderService providerService; |
| 92 | |
| 93 | private InternalPacketListener packetListener = new InternalPacketListener(); |
| 94 | |
pierventre | 283e217 | 2021-02-05 17:56:22 +0100 | [diff] [blame^] | 95 | /** Number of P4Runtime packet provider workers. */ |
| 96 | private int workers = P4RUNTIME_PACKET_PROVIDER_WORKERS_DEFAULT; |
| 97 | // Predictable executor to stitch the packet processing always to the same thread |
| 98 | private PredictableExecutor packetWorkers; |
| 99 | |
wu | 2883c76 | 2017-07-20 16:24:48 +0800 | [diff] [blame] | 100 | /** |
| 101 | * Creates a new P4Runtime packet provider. |
| 102 | */ |
| 103 | public P4RuntimePacketProvider() { |
| 104 | super(new ProviderId("p4runtime", "org.onosproject.provider.p4runtime.packet")); |
| 105 | } |
| 106 | |
| 107 | @Activate |
pierventre | 283e217 | 2021-02-05 17:56:22 +0100 | [diff] [blame^] | 108 | protected void activate(ComponentContext context) { |
| 109 | cfgService.registerProperties(getClass()); |
wu | 2883c76 | 2017-07-20 16:24:48 +0800 | [diff] [blame] | 110 | providerService = providerRegistry.register(this); |
pierventre | 283e217 | 2021-02-05 17:56:22 +0100 | [diff] [blame^] | 111 | modified(context); |
wu | 2883c76 | 2017-07-20 16:24:48 +0800 | [diff] [blame] | 112 | controller.addListener(packetListener); |
| 113 | log.info("Started"); |
| 114 | } |
| 115 | |
| 116 | @Deactivate |
| 117 | public void deactivate() { |
pierventre | 283e217 | 2021-02-05 17:56:22 +0100 | [diff] [blame^] | 118 | cfgService.unregisterProperties(getClass(), false); |
wu | 2883c76 | 2017-07-20 16:24:48 +0800 | [diff] [blame] | 119 | controller.removeListener(packetListener); |
| 120 | providerRegistry.unregister(this); |
| 121 | providerService = null; |
pierventre | 283e217 | 2021-02-05 17:56:22 +0100 | [diff] [blame^] | 122 | stopWorkersIfNeeded(); |
wu | 2883c76 | 2017-07-20 16:24:48 +0800 | [diff] [blame] | 123 | log.info("Stopped"); |
| 124 | } |
| 125 | |
pierventre | 283e217 | 2021-02-05 17:56:22 +0100 | [diff] [blame^] | 126 | @Modified |
| 127 | protected void modified(ComponentContext context) { |
| 128 | if (context != null) { |
| 129 | Dictionary<?, ?> properties = context.getProperties(); |
| 130 | int newWorkers; |
| 131 | try { |
| 132 | String s = get(properties, P4RUNTIME_PACKET_PROVIDER_WORKERS); |
| 133 | newWorkers = isNullOrEmpty(s) ? workers : Integer.parseInt(s.trim()); |
| 134 | } catch (NumberFormatException | ClassCastException e) { |
| 135 | newWorkers = workers; |
| 136 | } |
| 137 | |
| 138 | // Stop previous executor and start a new one when there are changes in the config |
| 139 | // OR during the start up of the service |
| 140 | if (newWorkers != workers || packetWorkers == null) { |
| 141 | workers = newWorkers; |
| 142 | stopWorkersIfNeeded(); |
| 143 | packetWorkers = new PredictableExecutor(workers, groupedThreads("onos/p4rt", |
| 144 | "packet-worker-%d", log)); |
| 145 | log.info("Settings: p4RuntimePacketProviderWorkers={}", workers); |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | |
wu | 2883c76 | 2017-07-20 16:24:48 +0800 | [diff] [blame] | 150 | @Override |
| 151 | public void emit(OutboundPacket packet) { |
| 152 | if (packet != null) { |
| 153 | DeviceId deviceId = packet.sendThrough(); |
| 154 | Device device = deviceService.getDevice(deviceId); |
Andrea Campanella | 14e196d | 2017-07-24 18:11:36 +0200 | [diff] [blame] | 155 | if (device.is(PacketProgrammable.class) && mastershipService.isLocalMaster(deviceId)) { |
wu | 2883c76 | 2017-07-20 16:24:48 +0800 | [diff] [blame] | 156 | PacketProgrammable packetProgrammable = device.as(PacketProgrammable.class); |
| 157 | packetProgrammable.emit(packet); |
| 158 | } else { |
| 159 | log.warn("No PacketProgrammable behavior for device {}", deviceId); |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | |
pierventre | 283e217 | 2021-02-05 17:56:22 +0100 | [diff] [blame^] | 164 | private void stopWorkersIfNeeded() { |
| 165 | if (packetWorkers != null) { |
| 166 | ExecutorService oldWorkerExecutor = packetWorkers; |
| 167 | oldWorkerExecutor.shutdown(); |
| 168 | packetWorkers = null; |
| 169 | } |
| 170 | } |
| 171 | |
Carmelo Cascone | 080969a | 2019-08-27 16:06:55 -0700 | [diff] [blame] | 172 | private EthType.EtherType getEtherType(ByteBuffer data) { |
| 173 | final short shortEthType = data.getShort(12); |
| 174 | data.rewind(); |
| 175 | return EthType.EtherType.lookup(shortEthType); |
| 176 | } |
| 177 | |
wu | 2883c76 | 2017-07-20 16:24:48 +0800 | [diff] [blame] | 178 | /** |
| 179 | * Internal packet context implementation. |
| 180 | */ |
| 181 | private class P4RuntimePacketContext extends DefaultPacketContext { |
| 182 | |
| 183 | P4RuntimePacketContext(long time, InboundPacket inPkt, OutboundPacket outPkt, boolean block) { |
| 184 | super(time, inPkt, outPkt, block); |
| 185 | } |
| 186 | |
| 187 | @Override |
| 188 | public void send() { |
| 189 | |
| 190 | if (this.block()) { |
| 191 | log.info("Unable to send, packet context is blocked"); |
| 192 | return; |
| 193 | } |
| 194 | |
| 195 | DeviceId deviceId = outPacket().sendThrough(); |
| 196 | ByteBuffer rawData = outPacket().data(); |
| 197 | |
| 198 | TrafficTreatment treatment; |
| 199 | if (outPacket().treatment() == null) { |
| 200 | treatment = (treatmentBuilder() == null) ? emptyTreatment() : treatmentBuilder().build(); |
| 201 | } else { |
| 202 | treatment = outPacket().treatment(); |
| 203 | } |
| 204 | |
Carmelo Cascone | 2cad9ef | 2017-08-01 21:52:07 +0200 | [diff] [blame] | 205 | OutboundPacket outboundPacket = new DefaultOutboundPacket(deviceId, treatment, rawData); |
Carmelo Cascone | 2cad9ef | 2017-08-01 21:52:07 +0200 | [diff] [blame] | 206 | |
| 207 | emit(outboundPacket); |
wu | 2883c76 | 2017-07-20 16:24:48 +0800 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | |
pierventre | 283e217 | 2021-02-05 17:56:22 +0100 | [diff] [blame^] | 211 | private void handleP4RuntimeEvent(P4RuntimeEvent event) { |
| 212 | //Mastership message is sent to everybody but picked up only by master. |
| 213 | //FIXME we need the device ID into p4RuntimeEvnetSubject to check for mastsership |
| 214 | if (!(event.subject() instanceof P4RuntimePacketIn) || event.type() != P4RuntimeEvent.Type.PACKET_IN) { |
| 215 | log.debug("Unrecognized event type {}, discarding", event.type()); |
| 216 | // Not a packet-in event, ignore it. |
| 217 | return; |
| 218 | } |
| 219 | P4RuntimePacketIn eventSubject = (P4RuntimePacketIn) event.subject(); |
| 220 | DeviceId deviceId = eventSubject.deviceId(); |
| 221 | |
| 222 | Device device = deviceService.getDevice(eventSubject.deviceId()); |
| 223 | if (device == null) { |
| 224 | log.warn("Unable to process packet-in from {}, device is null in the core", deviceId); |
| 225 | return; |
| 226 | } |
| 227 | |
| 228 | if (!device.is(PiPipelineInterpreter.class)) { |
| 229 | log.warn("Unable to process packet-in from {}, device has no PiPipelineInterpreter behaviour", |
| 230 | deviceId); |
| 231 | return; |
| 232 | } |
| 233 | |
| 234 | PiPacketOperation operation = eventSubject.packetOperation(); |
| 235 | InboundPacket inPkt; |
| 236 | try { |
| 237 | inPkt = device.as(PiPipelineInterpreter.class).mapInboundPacket(operation, deviceId); |
| 238 | } catch (PiPipelineInterpreter.PiInterpreterException e) { |
| 239 | log.warn("Unable to interpret inbound packet from {}: {}", deviceId, e.getMessage()); |
| 240 | return; |
| 241 | } |
| 242 | |
| 243 | if (log.isTraceEnabled()) { |
| 244 | final EthType.EtherType etherType = getEtherType(inPkt.unparsed()); |
| 245 | log.trace("Received PACKET-IN <<< device={} ingress_port={} eth_type={}", |
| 246 | inPkt.receivedFrom().deviceId(), inPkt.receivedFrom().port(), |
| 247 | etherType.ethType().toString()); |
| 248 | } |
| 249 | |
| 250 | if (inPkt == null) { |
| 251 | log.debug("Received null inbound packet. Ignoring."); |
| 252 | return; |
| 253 | } |
| 254 | |
| 255 | OutboundPacket outPkt = new DefaultOutboundPacket(eventSubject.deviceId(), null, |
| 256 | operation.data().asReadOnlyBuffer()); |
| 257 | PacketContext pktCtx = new P4RuntimePacketContext(System.currentTimeMillis(), inPkt, outPkt, false); |
| 258 | |
| 259 | // Pushing the packet context up for processing. |
| 260 | providerService.processPacket(pktCtx); |
| 261 | } |
| 262 | |
wu | 2883c76 | 2017-07-20 16:24:48 +0800 | [diff] [blame] | 263 | /** |
| 264 | * Internal packet listener to handle packet-in events received from the P4Runtime controller. |
| 265 | */ |
| 266 | private class InternalPacketListener implements P4RuntimeEventListener { |
| 267 | |
wu | 2883c76 | 2017-07-20 16:24:48 +0800 | [diff] [blame] | 268 | @Override |
| 269 | public void event(P4RuntimeEvent event) { |
pierventre | 283e217 | 2021-02-05 17:56:22 +0100 | [diff] [blame^] | 270 | // Offload to another executor to prevent the ejection of the listener - it uses |
| 271 | // the device id to stitch the packets coming from a device always to the same worker |
| 272 | packetWorkers.execute(() -> handleP4RuntimeEvent(event), event.subject().deviceId().hashCode()); |
wu | 2883c76 | 2017-07-20 16:24:48 +0800 | [diff] [blame] | 273 | } |
| 274 | } |
| 275 | } |