Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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.net.pi.impl; |
| 18 | |
Yi Tseng | 9f94a13 | 2021-07-28 17:00:46 -0700 | [diff] [blame] | 19 | import com.google.common.collect.Lists; |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 20 | import com.google.common.collect.Maps; |
| 21 | import com.google.common.collect.Sets; |
Carmelo Cascone | 1e8843f | 2018-07-19 19:01:12 +0200 | [diff] [blame] | 22 | import org.onosproject.net.Device; |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 23 | import org.onosproject.net.PortNumber; |
| 24 | import org.onosproject.net.flow.instructions.Instruction; |
| 25 | import org.onosproject.net.flow.instructions.Instructions.OutputInstruction; |
Yi Tseng | 9f94a13 | 2021-07-28 17:00:46 -0700 | [diff] [blame] | 26 | import org.onosproject.net.flow.instructions.Instructions.TruncateInstruction; |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 27 | import org.onosproject.net.group.Group; |
Yi Tseng | 9f94a13 | 2021-07-28 17:00:46 -0700 | [diff] [blame] | 28 | import org.onosproject.net.group.GroupBucket; |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 29 | import org.onosproject.net.group.GroupDescription; |
Carmelo Cascone | 1e8843f | 2018-07-19 19:01:12 +0200 | [diff] [blame] | 30 | import org.onosproject.net.pi.model.PiPipeconf; |
| 31 | import org.onosproject.net.pi.model.PiPipelineInterpreter; |
Carmelo Cascone | 9db4d5c | 2019-04-16 17:36:33 -0700 | [diff] [blame] | 32 | import org.onosproject.net.pi.runtime.PiCloneSessionEntry; |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 33 | import org.onosproject.net.pi.runtime.PiMulticastGroupEntry; |
Carmelo Cascone | 9db4d5c | 2019-04-16 17:36:33 -0700 | [diff] [blame] | 34 | import org.onosproject.net.pi.runtime.PiPreEntry; |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 35 | import org.onosproject.net.pi.runtime.PiPreReplica; |
| 36 | import org.onosproject.net.pi.service.PiTranslationException; |
| 37 | |
| 38 | import java.util.Collection; |
| 39 | import java.util.List; |
| 40 | import java.util.Map; |
Carmelo Cascone | 1e8843f | 2018-07-19 19:01:12 +0200 | [diff] [blame] | 41 | import java.util.Optional; |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 42 | import java.util.Set; |
| 43 | import java.util.stream.Collectors; |
| 44 | |
| 45 | import static com.google.common.base.Preconditions.checkNotNull; |
| 46 | import static java.lang.String.format; |
| 47 | |
| 48 | /** |
Carmelo Cascone | 9db4d5c | 2019-04-16 17:36:33 -0700 | [diff] [blame] | 49 | * Implementation of replication group translation logic. |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 50 | */ |
Carmelo Cascone | 9db4d5c | 2019-04-16 17:36:33 -0700 | [diff] [blame] | 51 | final class PiReplicationGroupTranslatorImpl { |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 52 | |
Carmelo Cascone | 9db4d5c | 2019-04-16 17:36:33 -0700 | [diff] [blame] | 53 | private PiReplicationGroupTranslatorImpl() { |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 54 | // Hides constructor. |
| 55 | } |
| 56 | |
| 57 | /** |
Carmelo Cascone | 9db4d5c | 2019-04-16 17:36:33 -0700 | [diff] [blame] | 58 | * Returns a PI PRE entry equivalent to the given group, for the given |
| 59 | * pipeconf and device. |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 60 | * <p> |
Carmelo Cascone | 9db4d5c | 2019-04-16 17:36:33 -0700 | [diff] [blame] | 61 | * The passed group is expected to have type {@link GroupDescription.Type#ALL} |
| 62 | * or {@link GroupDescription.Type#CLONE}. |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 63 | * |
| 64 | * @param group group |
Carmelo Cascone | 1e8843f | 2018-07-19 19:01:12 +0200 | [diff] [blame] | 65 | * @param pipeconf pipeconf |
| 66 | * @param device device |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 67 | * @return PI PRE entry |
| 68 | * @throws PiTranslationException if the group cannot be translated |
| 69 | */ |
Carmelo Cascone | 9db4d5c | 2019-04-16 17:36:33 -0700 | [diff] [blame] | 70 | static PiPreEntry translate(Group group, PiPipeconf pipeconf, Device device) |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 71 | throws PiTranslationException { |
| 72 | |
| 73 | checkNotNull(group); |
| 74 | |
Yi Tseng | 9f94a13 | 2021-07-28 17:00:46 -0700 | [diff] [blame] | 75 | final List<OutputInstruction> outInstructions = Lists.newArrayList(); |
| 76 | int truncateMaxLen = PiCloneSessionEntry.DO_NOT_TRUNCATE; |
| 77 | for (GroupBucket bucket : group.buckets().buckets()) { |
| 78 | int numInstructionsInBucket = bucket.treatment().allInstructions().size(); |
| 79 | List<OutputInstruction> outputs = |
| 80 | getInstructions(bucket, Instruction.Type.OUTPUT, OutputInstruction.class); |
| 81 | List<TruncateInstruction> truncates = |
| 82 | getInstructions(bucket, Instruction.Type.TRUNCATE, TruncateInstruction.class); |
| 83 | if (outputs.size() != 1) { |
| 84 | throw new PiTranslationException( |
| 85 | "support only groups with just one OUTPUT instruction per bucket"); |
| 86 | } |
| 87 | outInstructions.add(outputs.get(0)); |
| 88 | if (truncates.size() != 0) { |
| 89 | if (group.type() != GroupDescription.Type.CLONE) { |
| 90 | throw new PiTranslationException("only CLONE group support truncate instruction"); |
| 91 | } |
| 92 | if (truncates.size() != 1) { |
| 93 | throw new PiTranslationException( |
| 94 | "support only groups with just one TRUNCATE instruction per bucket"); |
| 95 | } |
| 96 | int truncateInstMaxLen = truncates.get(0).maxLen(); |
| 97 | if (truncateMaxLen != PiCloneSessionEntry.DO_NOT_TRUNCATE && |
| 98 | truncateMaxLen != truncateInstMaxLen) { |
| 99 | throw new PiTranslationException("all TRUNCATE instruction must be the same in a CLONE group"); |
| 100 | } |
| 101 | truncateMaxLen = truncateInstMaxLen; |
| 102 | } else if (truncateMaxLen != PiCloneSessionEntry.DO_NOT_TRUNCATE) { |
| 103 | // No truncate instruction found in this bucket, but previous bucket contains one. |
| 104 | throw new PiTranslationException("all TRUNCATE instruction must be the same in a CLONE group"); |
| 105 | } |
| 106 | if (numInstructionsInBucket != outputs.size() + truncates.size()) { |
| 107 | throw new PiTranslationException("bucket contains unsupported instruction(s)"); |
| 108 | } |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 109 | } |
| 110 | |
Carmelo Cascone | 9db4d5c | 2019-04-16 17:36:33 -0700 | [diff] [blame] | 111 | switch (group.type()) { |
| 112 | case ALL: |
| 113 | return PiMulticastGroupEntry.builder() |
| 114 | .withGroupId(group.id().id()) |
| 115 | .addReplicas(getReplicas(outInstructions, device)) |
| 116 | .build(); |
| 117 | case CLONE: |
| 118 | return PiCloneSessionEntry.builder() |
| 119 | .withSessionId(group.id().id()) |
| 120 | .addReplicas(getReplicas(outInstructions, device)) |
Yi Tseng | 9f94a13 | 2021-07-28 17:00:46 -0700 | [diff] [blame] | 121 | .withMaxPacketLengthBytes(truncateMaxLen) |
Carmelo Cascone | 9db4d5c | 2019-04-16 17:36:33 -0700 | [diff] [blame] | 122 | .build(); |
| 123 | default: |
| 124 | throw new PiTranslationException(format( |
| 125 | "group type %s not supported", group.type())); |
| 126 | } |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 127 | } |
| 128 | |
Carmelo Cascone | 1e8843f | 2018-07-19 19:01:12 +0200 | [diff] [blame] | 129 | private static Set<PiPreReplica> getReplicas( |
| 130 | Collection<OutputInstruction> instructions, Device device) |
| 131 | throws PiTranslationException { |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 132 | // Account for multiple replicas for the same port. |
| 133 | final Map<PortNumber, Set<PiPreReplica>> replicaMap = Maps.newHashMap(); |
| 134 | final List<PortNumber> ports = instructions.stream() |
| 135 | .map(OutputInstruction::port) |
| 136 | .collect(Collectors.toList()); |
| 137 | for (PortNumber port : ports) { |
Carmelo Cascone | 1e8843f | 2018-07-19 19:01:12 +0200 | [diff] [blame] | 138 | if (port.isLogical()) { |
| 139 | port = logicalToPipelineSpecific(port, device); |
| 140 | } |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 141 | // Use incremental instance IDs for replicas of the same port. |
Carmelo Cascone | 1e8843f | 2018-07-19 19:01:12 +0200 | [diff] [blame] | 142 | replicaMap.putIfAbsent(port, Sets.newHashSet()); |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 143 | replicaMap.get(port).add( |
Carmelo Cascone | dcd56ba | 2020-08-04 17:19:40 -0700 | [diff] [blame] | 144 | new PiPreReplica(port, replicaMap.get(port).size())); |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 145 | } |
| 146 | return replicaMap.values().stream() |
| 147 | .flatMap(Collection::stream) |
| 148 | .collect(Collectors.toSet()); |
| 149 | } |
Carmelo Cascone | 1e8843f | 2018-07-19 19:01:12 +0200 | [diff] [blame] | 150 | |
| 151 | private static PortNumber logicalToPipelineSpecific( |
| 152 | PortNumber logicalPort, Device device) |
| 153 | throws PiTranslationException { |
| 154 | if (!device.is(PiPipelineInterpreter.class)) { |
| 155 | throw new PiTranslationException( |
| 156 | "missing interpreter, cannot map logical port " + logicalPort.toString()); |
| 157 | } |
| 158 | final PiPipelineInterpreter interpreter = device.as(PiPipelineInterpreter.class); |
Yi Tseng | 7a77287 | 2022-01-24 10:51:44 -0800 | [diff] [blame^] | 159 | Optional<Long> mappedPort = interpreter.mapLogicalPort(logicalPort); |
Carmelo Cascone | 1e8843f | 2018-07-19 19:01:12 +0200 | [diff] [blame] | 160 | if (!mappedPort.isPresent()) { |
| 161 | throw new PiTranslationException( |
| 162 | "interpreter cannot map logical port " + logicalPort.toString()); |
| 163 | } |
| 164 | return PortNumber.portNumber(mappedPort.get()); |
| 165 | } |
Yi Tseng | 9f94a13 | 2021-07-28 17:00:46 -0700 | [diff] [blame] | 166 | |
| 167 | private static <I extends Instruction> List<I> getInstructions(GroupBucket bucket, |
| 168 | Instruction.Type type, |
| 169 | Class<I> instructionClass) { |
| 170 | return bucket.treatment().allInstructions().stream() |
| 171 | .filter(i -> i.type() == type) |
| 172 | .map(instructionClass::cast) |
| 173 | .collect(Collectors.toList()); |
| 174 | } |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 175 | } |