Andrea Campanella | fc1d34c | 2017-07-18 17:01:41 +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 | fc1d34c | 2017-07-18 17:01:41 +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 | |
| 17 | package org.onosproject.p4runtime.ctl; |
| 18 | |
| 19 | import com.google.protobuf.ByteString; |
Andrea Campanella | 288b273 | 2017-07-28 14:16:16 +0200 | [diff] [blame] | 20 | import org.onlab.util.ImmutableByteSequence; |
Andrea Campanella | fc1d34c | 2017-07-18 17:01:41 +0200 | [diff] [blame] | 21 | import org.onosproject.net.pi.model.PiPipeconf; |
Andrea Campanella | 288b273 | 2017-07-28 14:16:16 +0200 | [diff] [blame] | 22 | import org.onosproject.net.pi.runtime.PiPacketMetadata; |
| 23 | import org.onosproject.net.pi.runtime.PiPacketMetadataId; |
Andrea Campanella | fc1d34c | 2017-07-18 17:01:41 +0200 | [diff] [blame] | 24 | import org.onosproject.net.pi.runtime.PiPacketOperation; |
| 25 | import org.slf4j.Logger; |
Andrea Campanella | fc1d34c | 2017-07-18 17:01:41 +0200 | [diff] [blame] | 26 | import p4.config.P4InfoOuterClass; |
| 27 | |
Carmelo Cascone | 2cad9ef | 2017-08-01 21:52:07 +0200 | [diff] [blame] | 28 | import java.util.Collections; |
Andrea Campanella | fc1d34c | 2017-07-18 17:01:41 +0200 | [diff] [blame] | 29 | import java.util.List; |
| 30 | import java.util.Objects; |
| 31 | import java.util.stream.Collectors; |
| 32 | |
Andrea Campanella | 288b273 | 2017-07-28 14:16:16 +0200 | [diff] [blame] | 33 | import static org.onlab.util.ImmutableByteSequence.copyFrom; |
| 34 | import static org.onosproject.p4runtime.ctl.P4InfoBrowser.NotFoundException; |
Andrea Campanella | fc1d34c | 2017-07-18 17:01:41 +0200 | [diff] [blame] | 35 | import static org.slf4j.LoggerFactory.getLogger; |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 36 | import static p4.P4RuntimeOuterClass.*; |
Andrea Campanella | fc1d34c | 2017-07-18 17:01:41 +0200 | [diff] [blame] | 37 | |
| 38 | /** |
| 39 | * Encoder of packet metadata, from ONOS Pi* format, to P4Runtime protobuf messages, and vice versa. |
| 40 | */ |
| 41 | final class PacketIOCodec { |
| 42 | |
| 43 | private static final Logger log = getLogger(PacketIOCodec.class); |
| 44 | |
| 45 | private static final String PACKET_OUT = "packet_out"; |
| 46 | |
Andrea Campanella | 288b273 | 2017-07-28 14:16:16 +0200 | [diff] [blame] | 47 | private static final String PACKET_IN = "packet_in"; |
| 48 | |
Andrea Campanella | fc1d34c | 2017-07-18 17:01:41 +0200 | [diff] [blame] | 49 | // TODO: implement cache of encoded entities. |
| 50 | |
| 51 | private PacketIOCodec() { |
| 52 | // hide. |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Returns a P4Runtime packet out protobuf message, encoded from the given PiPacketOperation |
| 57 | * for the given pipeconf. If a PI packet metadata inside the PacketOperation cannot be encoded, |
| 58 | * it is skipped, hence the returned PacketOut collection of metadatas might have different |
| 59 | * size than the input one. |
| 60 | * <p> |
| 61 | * Please check the log for an explanation of any error that might have occurred. |
| 62 | * |
Andrea Campanella | 288b273 | 2017-07-28 14:16:16 +0200 | [diff] [blame] | 63 | * @param packet PI packet operation |
Andrea Campanella | fc1d34c | 2017-07-18 17:01:41 +0200 | [diff] [blame] | 64 | * @param pipeconf the pipeconf for the program on the switch |
| 65 | * @return a P4Runtime packet out protobuf message |
| 66 | * @throws NotFoundException if the browser can't find the packet_out in the given p4Info |
| 67 | */ |
Andrea Campanella | 288b273 | 2017-07-28 14:16:16 +0200 | [diff] [blame] | 68 | static PacketOut encodePacketOut(PiPacketOperation packet, PiPipeconf pipeconf) |
Andrea Campanella | fc1d34c | 2017-07-18 17:01:41 +0200 | [diff] [blame] | 69 | throws NotFoundException { |
| 70 | |
| 71 | //Get the P4browser |
| 72 | P4InfoBrowser browser = PipeconfHelper.getP4InfoBrowser(pipeconf); |
| 73 | |
Carmelo Cascone | 2cad9ef | 2017-08-01 21:52:07 +0200 | [diff] [blame] | 74 | //Get the packet out controller packet metadata |
Andrea Campanella | fc1d34c | 2017-07-18 17:01:41 +0200 | [diff] [blame] | 75 | P4InfoOuterClass.ControllerPacketMetadata controllerPacketMetadata = |
Carmelo Cascone | cb0a49c | 2017-10-03 14:32:23 +0200 | [diff] [blame] | 76 | browser.controllerPacketMetadatas().getByName(PACKET_OUT); |
Andrea Campanella | 288b273 | 2017-07-28 14:16:16 +0200 | [diff] [blame] | 77 | PacketOut.Builder packetOutBuilder = PacketOut.newBuilder(); |
Andrea Campanella | fc1d34c | 2017-07-18 17:01:41 +0200 | [diff] [blame] | 78 | |
| 79 | //outer controller packet metadata id |
| 80 | int controllerPacketMetadataId = controllerPacketMetadata.getPreamble().getId(); |
| 81 | |
| 82 | //Add all its metadata to the packet out |
| 83 | packetOutBuilder.addAllMetadata(encodePacketMetadata(packet, browser, controllerPacketMetadataId)); |
| 84 | |
| 85 | //Set the packet out payload |
| 86 | packetOutBuilder.setPayload(ByteString.copyFrom(packet.data().asReadOnlyBuffer())); |
| 87 | return packetOutBuilder.build(); |
| 88 | |
| 89 | } |
| 90 | |
| 91 | private static List<PacketMetadata> encodePacketMetadata(PiPacketOperation packet, |
| 92 | P4InfoBrowser browser, int controllerPacketMetadataId) { |
| 93 | return packet.metadatas().stream().map(metadata -> { |
| 94 | try { |
| 95 | //get each metadata id |
| 96 | int metadataId = browser.packetMetadatas(controllerPacketMetadataId) |
Carmelo Cascone | cb0a49c | 2017-10-03 14:32:23 +0200 | [diff] [blame] | 97 | .getByName(metadata.id().name()).getId(); |
Andrea Campanella | fc1d34c | 2017-07-18 17:01:41 +0200 | [diff] [blame] | 98 | |
| 99 | //Add the metadata id and it's data the packet out |
| 100 | return PacketMetadata.newBuilder() |
| 101 | .setMetadataId(metadataId) |
| 102 | .setValue(ByteString.copyFrom(metadata.value().asReadOnlyBuffer())) |
| 103 | .build(); |
| 104 | } catch (NotFoundException e) { |
| 105 | log.error("Cant find metadata with name {} in p4Info file.", metadata.id().name()); |
| 106 | return null; |
| 107 | } |
| 108 | }).filter(Objects::nonNull).collect(Collectors.toList()); |
| 109 | } |
| 110 | |
Andrea Campanella | 288b273 | 2017-07-28 14:16:16 +0200 | [diff] [blame] | 111 | /** |
| 112 | * Returns a PiPacketOperation, decoded from the given P4Runtime PacketIn protobuf message |
| 113 | * for the given pipeconf. If a PI packet metadata inside the protobuf message cannot be decoded, |
| 114 | * it is skipped, hence the returned PiPacketOperation collection of metadatas might have different |
| 115 | * size than the input one. |
| 116 | * <p> |
| 117 | * Please check the log for an explanation of any error that might have occurred. |
| 118 | * |
| 119 | * @param packetIn the P4Runtime PAcketIn message |
| 120 | * @param pipeconf the pipeconf for the program on the switch |
| 121 | * @return a PiPacketOperation |
| 122 | */ |
| 123 | static PiPacketOperation decodePacketIn(PacketIn packetIn, PiPipeconf pipeconf) { |
| 124 | |
| 125 | //Get the P4browser |
| 126 | P4InfoBrowser browser = PipeconfHelper.getP4InfoBrowser(pipeconf); |
| 127 | |
Carmelo Cascone | 2cad9ef | 2017-08-01 21:52:07 +0200 | [diff] [blame] | 128 | List<PiPacketMetadata> packetMetadatas; |
| 129 | try { |
Carmelo Cascone | cb0a49c | 2017-10-03 14:32:23 +0200 | [diff] [blame] | 130 | int controllerPacketMetadataId = browser.controllerPacketMetadatas().getByName(PACKET_IN) |
Carmelo Cascone | 2cad9ef | 2017-08-01 21:52:07 +0200 | [diff] [blame] | 131 | .getPreamble().getId(); |
| 132 | packetMetadatas = decodePacketMetadataIn(packetIn.getMetadataList(), browser, |
| 133 | controllerPacketMetadataId); |
| 134 | } catch (NotFoundException e) { |
| 135 | log.error("Unable to decode packet metadatas: {}", e.getMessage()); |
| 136 | packetMetadatas = Collections.emptyList(); |
| 137 | } |
| 138 | |
Andrea Campanella | 288b273 | 2017-07-28 14:16:16 +0200 | [diff] [blame] | 139 | //Transform the packetIn data |
| 140 | ImmutableByteSequence data = copyFrom(packetIn.getPayload().asReadOnlyByteBuffer()); |
| 141 | |
| 142 | //Build the PiPacketOperation with all the metadatas. |
| 143 | return PiPacketOperation.builder() |
| 144 | .withType(PiPacketOperation.Type.PACKET_IN) |
Carmelo Cascone | 2cad9ef | 2017-08-01 21:52:07 +0200 | [diff] [blame] | 145 | .withMetadatas(packetMetadatas) |
Andrea Campanella | 288b273 | 2017-07-28 14:16:16 +0200 | [diff] [blame] | 146 | .withData(data) |
| 147 | .build(); |
| 148 | } |
| 149 | |
Carmelo Cascone | 2cad9ef | 2017-08-01 21:52:07 +0200 | [diff] [blame] | 150 | private static List<PiPacketMetadata> decodePacketMetadataIn(List<PacketMetadata> packetMetadatas, |
| 151 | P4InfoBrowser browser, int controllerPacketMetadataId) { |
Andrea Campanella | 288b273 | 2017-07-28 14:16:16 +0200 | [diff] [blame] | 152 | return packetMetadatas.stream().map(packetMetadata -> { |
| 153 | try { |
| 154 | |
Carmelo Cascone | 2cad9ef | 2017-08-01 21:52:07 +0200 | [diff] [blame] | 155 | int packetMetadataId = packetMetadata.getMetadataId(); |
| 156 | String packetMetadataName = browser.packetMetadatas(controllerPacketMetadataId) |
| 157 | .getById(packetMetadataId).getName(); |
| 158 | |
| 159 | PiPacketMetadataId metadataId = PiPacketMetadataId.of(packetMetadataName); |
Andrea Campanella | 288b273 | 2017-07-28 14:16:16 +0200 | [diff] [blame] | 160 | |
| 161 | //Build each metadata. |
| 162 | return PiPacketMetadata.builder() |
| 163 | .withId(metadataId) |
| 164 | .withValue(ImmutableByteSequence.copyFrom(packetMetadata.getValue().asReadOnlyByteBuffer())) |
| 165 | .build(); |
| 166 | } catch (NotFoundException e) { |
| 167 | log.error("Cant find metadata with id {} in p4Info file.", packetMetadata.getMetadataId()); |
| 168 | return null; |
| 169 | } |
| 170 | }).filter(Objects::nonNull).collect(Collectors.toList()); |
| 171 | } |
Andrea Campanella | fc1d34c | 2017-07-18 17:01:41 +0200 | [diff] [blame] | 172 | |
| 173 | } |