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 | |
Carmelo Cascone | 080969a | 2019-08-27 16:06:55 -0700 | [diff] [blame^] | 19 | import org.onlab.packet.EthType; |
| 20 | import org.onosproject.net.flow.instructions.Instructions; |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 21 | import org.onosproject.net.packet.OutboundPacket; |
| 22 | import org.onosproject.net.packet.PacketProgrammable; |
Frank Wang | c672c98 | 2017-07-18 16:09:28 +0800 | [diff] [blame] | 23 | import org.onosproject.net.pi.model.PiPipelineInterpreter; |
Frank Wang | c672c98 | 2017-07-18 16:09:28 +0800 | [diff] [blame] | 24 | |
Carmelo Cascone | 080969a | 2019-08-27 16:06:55 -0700 | [diff] [blame^] | 25 | import java.nio.ByteBuffer; |
| 26 | import java.util.stream.Collectors; |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 27 | |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 28 | import static org.onosproject.drivers.p4runtime.P4RuntimeDriverUtils.getInterpreter; |
Carmelo Cascone | 080969a | 2019-08-27 16:06:55 -0700 | [diff] [blame^] | 29 | import static org.onosproject.net.flow.instructions.Instruction.Type.OUTPUT; |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 30 | |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 31 | /** |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 32 | * Implementation of PacketProgrammable behaviour for P4Runtime. |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 33 | */ |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 34 | public class P4RuntimePacketProgrammable |
| 35 | extends AbstractP4RuntimeHandlerBehaviour |
| 36 | implements PacketProgrammable { |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 37 | |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 38 | @Override |
| 39 | public void emit(OutboundPacket packet) { |
Frank Wang | c672c98 | 2017-07-18 16:09:28 +0800 | [diff] [blame] | 40 | |
Carmelo Cascone | c32976e | 2019-04-08 14:50:52 -0700 | [diff] [blame] | 41 | if (!this.setupBehaviour("emit()")) { |
Frank Wang | c672c98 | 2017-07-18 16:09:28 +0800 | [diff] [blame] | 42 | return; |
| 43 | } |
| 44 | |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 45 | final PiPipelineInterpreter interpreter = getInterpreter(handler()); |
Ray Milkey | 74e5913 | 2018-01-17 15:24:52 -0800 | [diff] [blame] | 46 | if (interpreter == null) { |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 47 | // Error logged by getInterpreter(). |
Frank Wang | c672c98 | 2017-07-18 16:09:28 +0800 | [diff] [blame] | 48 | return; |
| 49 | } |
| 50 | |
Carmelo Cascone | 080969a | 2019-08-27 16:06:55 -0700 | [diff] [blame^] | 51 | if (log.isTraceEnabled()) { |
| 52 | logPacketOut(packet); |
| 53 | } |
| 54 | |
Frank Wang | c672c98 | 2017-07-18 16:09:28 +0800 | [diff] [blame] | 55 | try { |
Carmelo Cascone | 080969a | 2019-08-27 16:06:55 -0700 | [diff] [blame^] | 56 | interpreter.mapOutboundPacket(packet).forEach( |
| 57 | op -> client.packetOut(p4DeviceId, op, pipeconf)); |
Frank Wang | c672c98 | 2017-07-18 16:09:28 +0800 | [diff] [blame] | 58 | } catch (PiPipelineInterpreter.PiInterpreterException e) { |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 59 | log.error("Unable to translate outbound packet for {} with pipeconf {}: {}", |
| 60 | deviceId, pipeconf.id(), e.getMessage()); |
Frank Wang | c672c98 | 2017-07-18 16:09:28 +0800 | [diff] [blame] | 61 | } |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 62 | } |
Carmelo Cascone | 080969a | 2019-08-27 16:06:55 -0700 | [diff] [blame^] | 63 | |
| 64 | private void logPacketOut(OutboundPacket packet) { |
| 65 | final EthType.EtherType etherType = getEtherType(packet.data()); |
| 66 | final String outPorts = packet.treatment().immediate().stream() |
| 67 | .filter(i -> i.type().equals(OUTPUT)) |
| 68 | .map(i -> Long.toString(((Instructions.OutputInstruction) i).port().toLong())) |
| 69 | .collect(Collectors.joining(",")); |
| 70 | final String desc = outPorts.isBlank() |
| 71 | ? "treatment=" + packet.treatment().toString() |
| 72 | : "egress_ports=" + outPorts; |
| 73 | log.trace("Sending PACKET-OUT >>> device={} {} eth_type={}", |
| 74 | packet.sendThrough(), desc, |
| 75 | etherType.ethType().toString()); |
| 76 | } |
| 77 | |
| 78 | private EthType.EtherType getEtherType(ByteBuffer data) { |
| 79 | final short shortEthType = data.getShort(12); |
| 80 | data.rewind(); |
| 81 | return EthType.EtherType.lookup(shortEthType); |
| 82 | } |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 83 | } |