Simon Hunt | 026a287 | 2017-11-13 17:09:43 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-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.t3.impl; |
| 18 | |
Andrea Campanella | 17d4519 | 2018-01-18 17:11:42 +0100 | [diff] [blame] | 19 | import com.google.common.base.Preconditions; |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 20 | import com.google.common.collect.ImmutableList; |
Andrea Campanella | 696ef03 | 2018-02-27 18:03:17 +0100 | [diff] [blame] | 21 | import com.google.common.collect.ImmutableSet; |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 22 | import com.google.common.collect.Lists; |
Andrea Campanella | 5befe1c | 2018-02-27 14:50:45 +0100 | [diff] [blame] | 23 | import com.google.common.collect.Sets; |
Andrea Campanella | 6a614fa | 2018-02-21 14:28:20 +0100 | [diff] [blame] | 24 | import org.apache.commons.lang3.tuple.Pair; |
Andrea Campanella | 55c3f42 | 2018-02-08 17:10:11 +0100 | [diff] [blame] | 25 | import org.onlab.packet.IpAddress; |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 26 | import org.onlab.packet.VlanId; |
Andrea Campanella | 54923d6 | 2018-01-23 12:46:04 +0100 | [diff] [blame] | 27 | import org.onosproject.cluster.NodeId; |
Simon Hunt | 026a287 | 2017-11-13 17:09:43 -0800 | [diff] [blame] | 28 | import org.onosproject.net.ConnectPoint; |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 29 | import org.onosproject.net.DeviceId; |
| 30 | import org.onosproject.net.Host; |
Andrea Campanella | 55c3f42 | 2018-02-08 17:10:11 +0100 | [diff] [blame] | 31 | import org.onosproject.net.HostId; |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 32 | import org.onosproject.net.Link; |
Andrea Campanella | 1445f7a | 2018-02-07 12:00:12 +0100 | [diff] [blame] | 33 | import org.onosproject.net.Port; |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 34 | import org.onosproject.net.PortNumber; |
Andrea Campanella | 55c3f42 | 2018-02-08 17:10:11 +0100 | [diff] [blame] | 35 | import org.onosproject.net.config.ConfigException; |
Andrea Campanella | 55c3f42 | 2018-02-08 17:10:11 +0100 | [diff] [blame] | 36 | import org.onosproject.net.config.basics.InterfaceConfig; |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 37 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 38 | import org.onosproject.net.flow.FlowEntry; |
| 39 | import org.onosproject.net.flow.FlowRule; |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 40 | import org.onosproject.net.flow.IndexTableId; |
| 41 | import org.onosproject.net.flow.TableId; |
Simon Hunt | 026a287 | 2017-11-13 17:09:43 -0800 | [diff] [blame] | 42 | import org.onosproject.net.flow.TrafficSelector; |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 43 | import org.onosproject.net.flow.criteria.Criteria; |
| 44 | import org.onosproject.net.flow.criteria.Criterion; |
| 45 | import org.onosproject.net.flow.criteria.EthCriterion; |
| 46 | import org.onosproject.net.flow.criteria.EthTypeCriterion; |
| 47 | import org.onosproject.net.flow.criteria.IPCriterion; |
Andrea Campanella | e679801 | 2018-02-06 15:46:52 +0100 | [diff] [blame] | 48 | import org.onosproject.net.flow.criteria.VlanIdCriterion; |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 49 | import org.onosproject.net.flow.instructions.Instruction; |
| 50 | import org.onosproject.net.flow.instructions.Instructions; |
| 51 | import org.onosproject.net.flow.instructions.Instructions.OutputInstruction; |
| 52 | import org.onosproject.net.flow.instructions.L2ModificationInstruction; |
| 53 | import org.onosproject.net.group.Group; |
| 54 | import org.onosproject.net.group.GroupBucket; |
Andrea Campanella | 55c3f42 | 2018-02-08 17:10:11 +0100 | [diff] [blame] | 55 | import org.onosproject.net.host.InterfaceIpAddress; |
| 56 | import org.onosproject.net.intf.Interface; |
Andrea Campanella | 08d07e1 | 2018-03-07 14:27:54 -0800 | [diff] [blame] | 57 | import org.onosproject.routeservice.ResolvedRoute; |
Andrea Campanella | 55c3f42 | 2018-02-08 17:10:11 +0100 | [diff] [blame] | 58 | import org.onosproject.segmentrouting.config.SegmentRoutingDeviceConfig; |
Seyeon Jeong | 83e7986 | 2020-02-28 01:17:34 -0800 | [diff] [blame^] | 59 | import org.onosproject.t3.api.DeviceNib; |
| 60 | import org.onosproject.t3.api.DriverNib; |
| 61 | import org.onosproject.t3.api.EdgePortNib; |
| 62 | import org.onosproject.t3.api.FlowNib; |
| 63 | import org.onosproject.t3.api.GroupNib; |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 64 | import org.onosproject.t3.api.GroupsInDevice; |
Seyeon Jeong | 83e7986 | 2020-02-28 01:17:34 -0800 | [diff] [blame^] | 65 | import org.onosproject.t3.api.HostNib; |
| 66 | import org.onosproject.t3.api.LinkNib; |
| 67 | import org.onosproject.t3.api.MastershipNib; |
| 68 | import org.onosproject.t3.api.MulticastRouteNib; |
| 69 | import org.onosproject.t3.api.NetworkConfigNib; |
| 70 | import org.onosproject.t3.api.RouteNib; |
Simon Hunt | 026a287 | 2017-11-13 17:09:43 -0800 | [diff] [blame] | 71 | import org.onosproject.t3.api.StaticPacketTrace; |
| 72 | import org.onosproject.t3.api.TroubleshootService; |
Ray Milkey | 9511b51 | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 73 | import org.osgi.service.component.annotations.Component; |
Simon Hunt | 026a287 | 2017-11-13 17:09:43 -0800 | [diff] [blame] | 74 | import org.slf4j.Logger; |
| 75 | |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 76 | import java.net.UnknownHostException; |
| 77 | import java.util.ArrayList; |
Andrea Campanella | 54923d6 | 2018-01-23 12:46:04 +0100 | [diff] [blame] | 78 | import java.util.Collection; |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 79 | import java.util.Collections; |
| 80 | import java.util.Comparator; |
| 81 | import java.util.HashSet; |
| 82 | import java.util.List; |
Andrea Campanella | 08d07e1 | 2018-03-07 14:27:54 -0800 | [diff] [blame] | 83 | import java.util.Optional; |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 84 | import java.util.Set; |
| 85 | import java.util.stream.Collectors; |
Seyeon Jeong | 83e7986 | 2020-02-28 01:17:34 -0800 | [diff] [blame^] | 86 | import java.util.stream.Stream; |
psneha | b823c51 | 2018-08-02 07:41:43 -0400 | [diff] [blame] | 87 | import java.util.stream.StreamSupport; |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 88 | |
| 89 | import static org.onlab.packet.EthType.EtherType; |
Andrea Campanella | e679801 | 2018-02-06 15:46:52 +0100 | [diff] [blame] | 90 | import static org.onosproject.net.flow.TrafficSelector.Builder; |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 91 | import static org.onosproject.net.flow.instructions.Instructions.GroupInstruction; |
Andrea Campanella | 94c594a | 2018-02-06 18:58:40 +0100 | [diff] [blame] | 92 | import static org.onosproject.net.flow.instructions.L2ModificationInstruction.ModEtherInstruction; |
| 93 | import static org.onosproject.net.flow.instructions.L2ModificationInstruction.ModMplsHeaderInstruction; |
| 94 | import static org.onosproject.net.flow.instructions.L2ModificationInstruction.ModMplsLabelInstruction; |
| 95 | import static org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanIdInstruction; |
Andrea Campanella | 4c6170a | 2018-01-17 16:34:51 +0100 | [diff] [blame] | 96 | import static org.onosproject.t3.impl.TroubleshootUtils.compareMac; |
Simon Hunt | 026a287 | 2017-11-13 17:09:43 -0800 | [diff] [blame] | 97 | import static org.slf4j.LoggerFactory.getLogger; |
| 98 | |
| 99 | /** |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 100 | * Manager to troubleshoot packets inside the network. |
| 101 | * Given a representation of a packet follows it's path in the network according to the existing flows and groups in |
| 102 | * the devices. |
Simon Hunt | 026a287 | 2017-11-13 17:09:43 -0800 | [diff] [blame] | 103 | */ |
Ray Milkey | 9511b51 | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 104 | @Component(immediate = true, service = TroubleshootService.class) |
Simon Hunt | 026a287 | 2017-11-13 17:09:43 -0800 | [diff] [blame] | 105 | public class TroubleshootManager implements TroubleshootService { |
| 106 | |
| 107 | private static final Logger log = getLogger(TroubleshootManager.class); |
| 108 | |
Andrea Campanella | 1445f7a | 2018-02-07 12:00:12 +0100 | [diff] [blame] | 109 | static final String PACKET_TO_CONTROLLER = "Packet goes to the controller"; |
| 110 | |
Seyeon Jeong | 83e7986 | 2020-02-28 01:17:34 -0800 | [diff] [blame^] | 111 | // uses a snapshot (cache) of NIBs instead of interacting with ONOS core in runtime |
| 112 | protected FlowNib flowNib; |
| 113 | protected GroupNib groupNib; |
| 114 | protected LinkNib linkNib; |
| 115 | protected HostNib hostNib; |
| 116 | protected DeviceNib deviceNib; |
| 117 | protected DriverNib driverNib; |
| 118 | protected MastershipNib mastershipNib; |
| 119 | protected EdgePortNib edgePortNib; |
| 120 | protected RouteNib routeNib; |
| 121 | protected NetworkConfigNib networkConfigNib; |
| 122 | protected MulticastRouteNib mcastRouteNib; |
Simon Hunt | 026a287 | 2017-11-13 17:09:43 -0800 | [diff] [blame] | 123 | |
Seyeon Jeong | 83e7986 | 2020-02-28 01:17:34 -0800 | [diff] [blame^] | 124 | @Override |
| 125 | public boolean checkNibsUnavailable() { |
| 126 | return Stream.of(flowNib, groupNib, linkNib, hostNib, deviceNib, driverNib, |
| 127 | mastershipNib, edgePortNib, routeNib, networkConfigNib, mcastRouteNib) |
| 128 | .anyMatch(x -> x == null); |
| 129 | } |
Simon Hunt | 026a287 | 2017-11-13 17:09:43 -0800 | [diff] [blame] | 130 | |
Seyeon Jeong | 83e7986 | 2020-02-28 01:17:34 -0800 | [diff] [blame^] | 131 | @Override |
| 132 | public void applyNibs() { |
| 133 | flowNib = FlowNib.getInstance(); |
| 134 | groupNib = GroupNib.getInstance(); |
| 135 | linkNib = LinkNib.getInstance(); |
| 136 | hostNib = HostNib.getInstance(); |
| 137 | deviceNib = DeviceNib.getInstance(); |
| 138 | driverNib = DriverNib.getInstance(); |
| 139 | mastershipNib = MastershipNib.getInstance(); |
| 140 | edgePortNib = EdgePortNib.getInstance(); |
| 141 | routeNib = RouteNib.getInstance(); |
| 142 | networkConfigNib = NetworkConfigNib.getInstance(); |
| 143 | mcastRouteNib = MulticastRouteNib.getInstance(); |
| 144 | } |
Andrea Campanella | bd15bf5 | 2018-04-06 16:30:18 +0200 | [diff] [blame] | 145 | |
Andrea Campanella | 55c3f42 | 2018-02-08 17:10:11 +0100 | [diff] [blame] | 146 | @Override |
Andrea Campanella | 6a614fa | 2018-02-21 14:28:20 +0100 | [diff] [blame] | 147 | public List<StaticPacketTrace> pingAll(EtherType type) { |
| 148 | ImmutableList.Builder<StaticPacketTrace> tracesBuilder = ImmutableList.builder(); |
Seyeon Jeong | 83e7986 | 2020-02-28 01:17:34 -0800 | [diff] [blame^] | 149 | hostNib.getHosts().forEach(host -> { |
Andrea Campanella | 6a614fa | 2018-02-21 14:28:20 +0100 | [diff] [blame] | 150 | List<IpAddress> ipAddresses = getIpAddresses(host, type, false); |
| 151 | if (ipAddresses.size() > 0) { |
Andrea Campanella | 5befe1c | 2018-02-27 14:50:45 +0100 | [diff] [blame] | 152 | //check if the host has only local IPs of that ETH type |
| 153 | boolean onlyLocalSrc = ipAddresses.size() == 1 && ipAddresses.get(0).isLinkLocal(); |
Seyeon Jeong | 83e7986 | 2020-02-28 01:17:34 -0800 | [diff] [blame^] | 154 | hostNib.getHosts().forEach(hostToPing -> { |
Andrea Campanella | 6a614fa | 2018-02-21 14:28:20 +0100 | [diff] [blame] | 155 | List<IpAddress> ipAddressesToPing = getIpAddresses(hostToPing, type, false); |
Andrea Campanella | 5befe1c | 2018-02-27 14:50:45 +0100 | [diff] [blame] | 156 | //check if the other host has only local IPs of that ETH type |
| 157 | boolean onlyLocalDst = ipAddressesToPing.size() == 1 && ipAddressesToPing.get(0).isLinkLocal(); |
| 158 | boolean sameLocation = Sets.intersection(host.locations(), hostToPing.locations()).size() > 0; |
| 159 | //Trace is done only if they are both local and under the same location |
| 160 | // or not local and if they are not the same host. |
| 161 | if (((sameLocation && onlyLocalDst && onlyLocalSrc) || |
| 162 | (!onlyLocalSrc && !onlyLocalDst && ipAddressesToPing.size() > 0)) |
| 163 | && !host.equals(hostToPing)) { |
Andrea Campanella | 696ef03 | 2018-02-27 18:03:17 +0100 | [diff] [blame] | 164 | tracesBuilder.addAll(trace(host.id(), hostToPing.id(), type)); |
Andrea Campanella | 6a614fa | 2018-02-21 14:28:20 +0100 | [diff] [blame] | 165 | } |
| 166 | }); |
| 167 | } |
| 168 | }); |
| 169 | return tracesBuilder.build(); |
| 170 | } |
| 171 | |
| 172 | @Override |
Andrea Campanella | 0cc6acd | 2018-02-28 16:43:16 +0100 | [diff] [blame] | 173 | public Generator<Set<StaticPacketTrace>> pingAllGenerator(EtherType type) { |
Seyeon Jeong | 83e7986 | 2020-02-28 01:17:34 -0800 | [diff] [blame^] | 174 | return new PingAllGenerator(type, hostNib, this); |
Andrea Campanella | 0cc6acd | 2018-02-28 16:43:16 +0100 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | @Override |
Andrea Campanella | bd15bf5 | 2018-04-06 16:30:18 +0200 | [diff] [blame] | 178 | public Generator<Set<StaticPacketTrace>> traceMcast(VlanId vlanId) { |
Seyeon Jeong | 83e7986 | 2020-02-28 01:17:34 -0800 | [diff] [blame^] | 179 | return new McastGenerator(mcastRouteNib, this, vlanId); |
Andrea Campanella | bd15bf5 | 2018-04-06 16:30:18 +0200 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | @Override |
Andrea Campanella | 696ef03 | 2018-02-27 18:03:17 +0100 | [diff] [blame] | 183 | public Set<StaticPacketTrace> trace(HostId sourceHost, HostId destinationHost, EtherType etherType) { |
Seyeon Jeong | 83e7986 | 2020-02-28 01:17:34 -0800 | [diff] [blame^] | 184 | Host source = hostNib.getHost(sourceHost); |
| 185 | Host destination = hostNib.getHost(destinationHost); |
Andrea Campanella | 55c3f42 | 2018-02-08 17:10:11 +0100 | [diff] [blame] | 186 | |
Andrea Campanella | 6a614fa | 2018-02-21 14:28:20 +0100 | [diff] [blame] | 187 | //Temporary trace to fail in case we don't have enough information or what is provided is incoherent |
| 188 | StaticPacketTrace failTrace = new StaticPacketTrace(null, null, Pair.of(source, destination)); |
Andrea Campanella | 55c3f42 | 2018-02-08 17:10:11 +0100 | [diff] [blame] | 189 | |
| 190 | if (source == null) { |
| 191 | failTrace.addResultMessage("Source Host " + sourceHost + " does not exist"); |
Andrea Campanella | 5af2d2b | 2018-03-12 19:25:44 -0700 | [diff] [blame] | 192 | failTrace.setSuccess(false); |
| 193 | |
Andrea Campanella | 696ef03 | 2018-02-27 18:03:17 +0100 | [diff] [blame] | 194 | return ImmutableSet.of(failTrace); |
Andrea Campanella | 55c3f42 | 2018-02-08 17:10:11 +0100 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | if (destination == null) { |
| 198 | failTrace.addResultMessage("Destination Host " + destinationHost + " does not exist"); |
Andrea Campanella | 5af2d2b | 2018-03-12 19:25:44 -0700 | [diff] [blame] | 199 | failTrace.setSuccess(false); |
Andrea Campanella | 696ef03 | 2018-02-27 18:03:17 +0100 | [diff] [blame] | 200 | return ImmutableSet.of(failTrace); |
Andrea Campanella | 55c3f42 | 2018-02-08 17:10:11 +0100 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder() |
Andrea Campanella | 55c3f42 | 2018-02-08 17:10:11 +0100 | [diff] [blame] | 204 | .matchEthType(etherType.ethType().toShort()) |
| 205 | .matchEthDst(source.mac()) |
| 206 | .matchVlanId(source.vlan()); |
| 207 | |
| 208 | |
Andrea Campanella | 55c3f42 | 2018-02-08 17:10:11 +0100 | [diff] [blame] | 209 | try { |
Andrea Campanella | e24d492 | 2018-03-26 10:39:22 -0700 | [diff] [blame] | 210 | ImmutableSet.Builder<StaticPacketTrace> traces = ImmutableSet.builder(); |
Andrea Campanella | 55c3f42 | 2018-02-08 17:10:11 +0100 | [diff] [blame] | 211 | //if the location deviceId is the same, the two hosts are under same subnet and vlan on the interface |
| 212 | // we are under same leaf so it's L2 Unicast. |
| 213 | if (areBridged(source, destination)) { |
| 214 | selectorBuilder.matchEthDst(destination.mac()); |
Andrea Campanella | 696ef03 | 2018-02-27 18:03:17 +0100 | [diff] [blame] | 215 | source.locations().forEach(hostLocation -> { |
Andrea Campanella | 2ff88a9 | 2018-03-06 15:21:09 -0800 | [diff] [blame] | 216 | selectorBuilder.matchInPort(hostLocation.port()); |
Andrea Campanella | 696ef03 | 2018-02-27 18:03:17 +0100 | [diff] [blame] | 217 | StaticPacketTrace trace = trace(selectorBuilder.build(), hostLocation); |
| 218 | trace.addEndpointHosts(Pair.of(source, destination)); |
| 219 | traces.add(trace); |
| 220 | }); |
Andrea Campanella | e24d492 | 2018-03-26 10:39:22 -0700 | [diff] [blame] | 221 | //The destination host is not dual homed, if it is the other path might be done through routing. |
| 222 | if (destination.locations().size() == 1) { |
| 223 | return traces.build(); |
| 224 | } |
Andrea Campanella | 55c3f42 | 2018-02-08 17:10:11 +0100 | [diff] [blame] | 225 | } |
Andrea Campanella | 55c3f42 | 2018-02-08 17:10:11 +0100 | [diff] [blame] | 226 | //handle the IPs for src and dst in case of L3 |
| 227 | if (etherType.equals(EtherType.IPV4) || etherType.equals(EtherType.IPV6)) { |
| 228 | |
| 229 | //Match on the source IP |
| 230 | if (!matchIP(source, failTrace, selectorBuilder, etherType, true)) { |
Andrea Campanella | 696ef03 | 2018-02-27 18:03:17 +0100 | [diff] [blame] | 231 | return ImmutableSet.of(failTrace); |
Andrea Campanella | 55c3f42 | 2018-02-08 17:10:11 +0100 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | //Match on destination IP |
| 235 | if (!matchIP(destination, failTrace, selectorBuilder, etherType, false)) { |
Andrea Campanella | 696ef03 | 2018-02-27 18:03:17 +0100 | [diff] [blame] | 236 | return ImmutableSet.of(failTrace); |
Andrea Campanella | 55c3f42 | 2018-02-08 17:10:11 +0100 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | } else { |
| 240 | failTrace.addResultMessage("Host based trace supports only IPv4 or IPv6 as EtherType, " + |
| 241 | "please use packet based"); |
Andrea Campanella | 5af2d2b | 2018-03-12 19:25:44 -0700 | [diff] [blame] | 242 | failTrace.setSuccess(false); |
Andrea Campanella | 696ef03 | 2018-02-27 18:03:17 +0100 | [diff] [blame] | 243 | return ImmutableSet.of(failTrace); |
Andrea Campanella | 55c3f42 | 2018-02-08 17:10:11 +0100 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | //l3 unicast, we get the dst mac of the leaf the source is connected to from netcfg |
Seyeon Jeong | 83e7986 | 2020-02-28 01:17:34 -0800 | [diff] [blame^] | 247 | SegmentRoutingDeviceConfig segmentRoutingConfig = networkConfigNib.getConfig(source.location() |
Andrea Campanella | 55c3f42 | 2018-02-08 17:10:11 +0100 | [diff] [blame] | 248 | .deviceId(), SegmentRoutingDeviceConfig.class); |
| 249 | if (segmentRoutingConfig != null) { |
| 250 | selectorBuilder.matchEthDst(segmentRoutingConfig.routerMac()); |
| 251 | } else { |
| 252 | failTrace.addResultMessage("Can't get " + source.location().deviceId() + |
| 253 | " router MAC from segment routing config can't perform L3 tracing."); |
Andrea Campanella | 5af2d2b | 2018-03-12 19:25:44 -0700 | [diff] [blame] | 254 | failTrace.setSuccess(false); |
Andrea Campanella | 55c3f42 | 2018-02-08 17:10:11 +0100 | [diff] [blame] | 255 | } |
Andrea Campanella | 696ef03 | 2018-02-27 18:03:17 +0100 | [diff] [blame] | 256 | source.locations().forEach(hostLocation -> { |
Andrea Campanella | 2ff88a9 | 2018-03-06 15:21:09 -0800 | [diff] [blame] | 257 | selectorBuilder.matchInPort(hostLocation.port()); |
Andrea Campanella | 696ef03 | 2018-02-27 18:03:17 +0100 | [diff] [blame] | 258 | StaticPacketTrace trace = trace(selectorBuilder.build(), hostLocation); |
| 259 | trace.addEndpointHosts(Pair.of(source, destination)); |
| 260 | traces.add(trace); |
| 261 | }); |
| 262 | return traces.build(); |
Andrea Campanella | 55c3f42 | 2018-02-08 17:10:11 +0100 | [diff] [blame] | 263 | |
| 264 | } catch (ConfigException e) { |
| 265 | failTrace.addResultMessage("Can't get config " + e.getMessage()); |
Andrea Campanella | 696ef03 | 2018-02-27 18:03:17 +0100 | [diff] [blame] | 266 | return ImmutableSet.of(failTrace); |
Andrea Campanella | 55c3f42 | 2018-02-08 17:10:11 +0100 | [diff] [blame] | 267 | } |
| 268 | } |
| 269 | |
| 270 | /** |
| 271 | * Matches src and dst IPs based on host information. |
| 272 | * |
| 273 | * @param host the host |
| 274 | * @param failTrace the trace to use in case of failure |
| 275 | * @param selectorBuilder the packet we are building to trace |
| 276 | * @param etherType the traffic type |
| 277 | * @param src is this src host or dst host |
| 278 | * @return true if properly matched |
| 279 | */ |
| 280 | private boolean matchIP(Host host, StaticPacketTrace failTrace, Builder selectorBuilder, |
| 281 | EtherType etherType, boolean src) { |
Andrea Campanella | 6a614fa | 2018-02-21 14:28:20 +0100 | [diff] [blame] | 282 | List<IpAddress> ips = getIpAddresses(host, etherType, true); |
Andrea Campanella | 55c3f42 | 2018-02-08 17:10:11 +0100 | [diff] [blame] | 283 | |
| 284 | if (ips.size() > 0) { |
Andrea Campanella | bf8d930 | 2018-02-21 14:43:21 +0100 | [diff] [blame] | 285 | if (etherType.equals(EtherType.IPV4)) { |
| 286 | if (src) { |
| 287 | selectorBuilder.matchIPSrc(ips.get(0).toIpPrefix()); |
| 288 | } else { |
| 289 | selectorBuilder.matchIPDst(ips.get(0).toIpPrefix()); |
| 290 | } |
| 291 | } else if (etherType.equals(EtherType.IPV6)) { |
| 292 | if (src) { |
| 293 | selectorBuilder.matchIPv6Src(ips.get(0).toIpPrefix()); |
| 294 | } else { |
| 295 | selectorBuilder.matchIPv6Dst(ips.get(0).toIpPrefix()); |
| 296 | } |
Andrea Campanella | 55c3f42 | 2018-02-08 17:10:11 +0100 | [diff] [blame] | 297 | } |
| 298 | } else { |
| 299 | failTrace.addResultMessage("Host " + host + " has no " + etherType + " address"); |
Andrea Campanella | 5af2d2b | 2018-03-12 19:25:44 -0700 | [diff] [blame] | 300 | failTrace.setSuccess(false); |
Andrea Campanella | 55c3f42 | 2018-02-08 17:10:11 +0100 | [diff] [blame] | 301 | return false; |
| 302 | } |
| 303 | return true; |
| 304 | } |
| 305 | |
Andrea Campanella | 0cc6acd | 2018-02-28 16:43:16 +0100 | [diff] [blame] | 306 | List<IpAddress> getIpAddresses(Host host, EtherType etherType, boolean checklocal) { |
Andrea Campanella | 6a614fa | 2018-02-21 14:28:20 +0100 | [diff] [blame] | 307 | return host.ipAddresses().stream().filter(ipAddress -> { |
| 308 | boolean correctIp = false; |
| 309 | if (etherType.equals(EtherType.IPV4)) { |
| 310 | correctIp = ipAddress.isIp4(); |
| 311 | } else if (etherType.equals(EtherType.IPV6)) { |
| 312 | correctIp = ipAddress.isIp6(); |
| 313 | } |
| 314 | if (checklocal) { |
| 315 | correctIp = correctIp && !ipAddress.isLinkLocal(); |
| 316 | } |
| 317 | return correctIp; |
| 318 | }).collect(Collectors.toList()); |
| 319 | } |
| 320 | |
Andrea Campanella | 55c3f42 | 2018-02-08 17:10:11 +0100 | [diff] [blame] | 321 | /** |
| 322 | * Checks that two hosts are bridged (L2Unicast). |
| 323 | * |
| 324 | * @param source the source host |
| 325 | * @param destination the destination host |
| 326 | * @return true if bridged. |
| 327 | * @throws ConfigException if config can't be properly retrieved |
| 328 | */ |
| 329 | private boolean areBridged(Host source, Host destination) throws ConfigException { |
| 330 | |
Andrea Campanella | 2ff88a9 | 2018-03-06 15:21:09 -0800 | [diff] [blame] | 331 | //If the locations is not the same we don't even check vlan or subnets |
| 332 | if (Collections.disjoint(source.locations(), destination.locations())) { |
| 333 | return false; |
| 334 | } |
| 335 | |
| 336 | if (!source.vlan().equals(VlanId.NONE) && !destination.vlan().equals(VlanId.NONE) |
| 337 | && !source.vlan().equals(destination.vlan())) { |
Andrea Campanella | 55c3f42 | 2018-02-08 17:10:11 +0100 | [diff] [blame] | 338 | return false; |
| 339 | } |
| 340 | |
Seyeon Jeong | 83e7986 | 2020-02-28 01:17:34 -0800 | [diff] [blame^] | 341 | InterfaceConfig interfaceCfgH1 = networkConfigNib.getConfig(source.location(), InterfaceConfig.class); |
| 342 | InterfaceConfig interfaceCfgH2 = networkConfigNib.getConfig(destination.location(), InterfaceConfig.class); |
Andrea Campanella | 55c3f42 | 2018-02-08 17:10:11 +0100 | [diff] [blame] | 343 | if (interfaceCfgH1 != null && interfaceCfgH2 != null) { |
| 344 | |
| 345 | //following can be optimized but for clarity is left as is |
| 346 | Interface intfH1 = interfaceCfgH1.getInterfaces().stream().findFirst().get(); |
| 347 | Interface intfH2 = interfaceCfgH2.getInterfaces().stream().findFirst().get(); |
| 348 | |
Andrea Campanella | 2ff88a9 | 2018-03-06 15:21:09 -0800 | [diff] [blame] | 349 | if (source.vlan().equals(VlanId.NONE) && !destination.vlan().equals(VlanId.NONE)) { |
| 350 | return intfH1.vlanUntagged().equals(destination.vlan()) || |
| 351 | intfH1.vlanNative().equals(destination.vlan()); |
Andrea Campanella | 55c3f42 | 2018-02-08 17:10:11 +0100 | [diff] [blame] | 352 | } |
| 353 | |
Andrea Campanella | 2ff88a9 | 2018-03-06 15:21:09 -0800 | [diff] [blame] | 354 | if (!source.vlan().equals(VlanId.NONE) && destination.vlan().equals(VlanId.NONE)) { |
| 355 | return intfH2.vlanUntagged().equals(source.vlan()) || |
| 356 | intfH2.vlanNative().equals(source.vlan()); |
| 357 | } |
| 358 | |
| 359 | if (!intfH1.vlanNative().equals(intfH2.vlanNative())) { |
Andrea Campanella | 55c3f42 | 2018-02-08 17:10:11 +0100 | [diff] [blame] | 360 | return false; |
| 361 | } |
| 362 | |
| 363 | if (!intfH1.vlanUntagged().equals(intfH2.vlanUntagged())) { |
| 364 | return false; |
| 365 | } |
| 366 | |
| 367 | List<InterfaceIpAddress> intersection = new ArrayList<>(intfH1.ipAddressesList()); |
| 368 | intersection.retainAll(intfH2.ipAddressesList()); |
| 369 | if (intersection.size() == 0) { |
| 370 | return false; |
| 371 | } |
| 372 | } |
| 373 | return true; |
| 374 | } |
| 375 | |
Simon Hunt | 026a287 | 2017-11-13 17:09:43 -0800 | [diff] [blame] | 376 | @Override |
| 377 | public StaticPacketTrace trace(TrafficSelector packet, ConnectPoint in) { |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 378 | log.info("Tracing packet {} coming in through {}", packet, in); |
Andrea Campanella | 17d4519 | 2018-01-18 17:11:42 +0100 | [diff] [blame] | 379 | //device must exist in ONOS |
Seyeon Jeong | 83e7986 | 2020-02-28 01:17:34 -0800 | [diff] [blame^] | 380 | Preconditions.checkNotNull(deviceNib.getDevice(in.deviceId()), |
Andrea Campanella | 17d4519 | 2018-01-18 17:11:42 +0100 | [diff] [blame] | 381 | "Device " + in.deviceId() + " must exist in ONOS"); |
| 382 | |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 383 | StaticPacketTrace trace = new StaticPacketTrace(packet, in); |
Andrea Campanella | 696ef03 | 2018-02-27 18:03:17 +0100 | [diff] [blame] | 384 | boolean isDualHomed = getHosts(trace).stream().anyMatch(host -> host.locations().size() > 1); |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 385 | //FIXME this can be done recursively |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 386 | //Building output connect Points |
| 387 | List<ConnectPoint> path = new ArrayList<>(); |
Andrea Campanella | d5e16ff | 2018-03-21 10:08:55 -0700 | [diff] [blame] | 388 | trace = traceInDevice(trace, packet, in, isDualHomed, path); |
Andrea Campanella | 696ef03 | 2018-02-27 18:03:17 +0100 | [diff] [blame] | 389 | trace = getTrace(path, in, trace, isDualHomed); |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 390 | return trace; |
| 391 | } |
| 392 | |
psneha | b823c51 | 2018-08-02 07:41:43 -0400 | [diff] [blame] | 393 | @Override |
| 394 | public List<Set<StaticPacketTrace>> getMulitcastTrace(VlanId vlanId) { |
Seyeon Jeong | 83e7986 | 2020-02-28 01:17:34 -0800 | [diff] [blame^] | 395 | Generator<Set<StaticPacketTrace>> gen = new McastGenerator(mcastRouteNib, this, vlanId); |
psneha | b823c51 | 2018-08-02 07:41:43 -0400 | [diff] [blame] | 396 | List<Set<StaticPacketTrace>> multicastTraceList = |
| 397 | StreamSupport.stream(gen.spliterator(), false).collect(Collectors.toList()); |
| 398 | return multicastTraceList; |
| 399 | } |
| 400 | |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 401 | /** |
| 402 | * Computes a trace for a give packet that start in the network at the given connect point. |
| 403 | * |
| 404 | * @param completePath the path traversed by the packet |
| 405 | * @param in the input connect point |
| 406 | * @param trace the trace to build |
Andrea Campanella | 696ef03 | 2018-02-27 18:03:17 +0100 | [diff] [blame] | 407 | * @param isDualHomed true if the trace we are doing starts or ends in a dual homed host |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 408 | * @return the build trace for that packet. |
| 409 | */ |
Andrea Campanella | 696ef03 | 2018-02-27 18:03:17 +0100 | [diff] [blame] | 410 | private StaticPacketTrace getTrace(List<ConnectPoint> completePath, ConnectPoint in, StaticPacketTrace trace, |
| 411 | boolean isDualHomed) { |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 412 | |
Andrea Campanella | b022b5e | 2018-01-31 14:59:03 +0100 | [diff] [blame] | 413 | log.debug("------------------------------------------------------------"); |
| 414 | |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 415 | //if the trace already contains the input connect point there is a loop |
| 416 | if (pathContainsDevice(completePath, in.deviceId())) { |
| 417 | trace.addResultMessage("Loop encountered in device " + in.deviceId()); |
Andrea Campanella | 3ddbadb | 2018-03-09 14:52:10 -0800 | [diff] [blame] | 418 | completePath.add(in); |
| 419 | trace.addCompletePath(completePath); |
Andrea Campanella | 5af2d2b | 2018-03-12 19:25:44 -0700 | [diff] [blame] | 420 | trace.setSuccess(false); |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 421 | return trace; |
| 422 | } |
| 423 | |
| 424 | //let's add the input connect point |
| 425 | completePath.add(in); |
| 426 | |
| 427 | //If the trace has no outputs for the given input we stop here |
| 428 | if (trace.getGroupOuputs(in.deviceId()) == null) { |
| 429 | computePath(completePath, trace, null); |
| 430 | trace.addResultMessage("No output out of device " + in.deviceId() + ". Packet is dropped"); |
Andrea Campanella | 5af2d2b | 2018-03-12 19:25:44 -0700 | [diff] [blame] | 431 | trace.setSuccess(false); |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 432 | return trace; |
| 433 | } |
Andrea Campanella | 7d3cf65 | 2018-01-22 15:10:30 +0100 | [diff] [blame] | 434 | |
Andrea Campanella | 27140da | 2018-04-09 14:22:32 +0200 | [diff] [blame] | 435 | //If the trace has outputs we analyze them all |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 436 | for (GroupsInDevice outputPath : trace.getGroupOuputs(in.deviceId())) { |
Andrea Campanella | 54923d6 | 2018-01-23 12:46:04 +0100 | [diff] [blame] | 437 | |
| 438 | ConnectPoint cp = outputPath.getOutput(); |
Andrea Campanella | b022b5e | 2018-01-31 14:59:03 +0100 | [diff] [blame] | 439 | log.debug("Connect point in {}", in); |
Andrea Campanella | 54923d6 | 2018-01-23 12:46:04 +0100 | [diff] [blame] | 440 | log.debug("Output path {}", cp); |
Andrea Campanella | 08d07e1 | 2018-03-07 14:27:54 -0800 | [diff] [blame] | 441 | log.debug("{}", outputPath.getFinalPacket()); |
Andrea Campanella | 54923d6 | 2018-01-23 12:46:04 +0100 | [diff] [blame] | 442 | |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 443 | //Hosts for the the given output |
Seyeon Jeong | 83e7986 | 2020-02-28 01:17:34 -0800 | [diff] [blame^] | 444 | Set<Host> hostsList = hostNib.getConnectedHosts(cp); |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 445 | //Hosts queried from the original ip or mac |
| 446 | Set<Host> hosts = getHosts(trace); |
| 447 | |
Andrea Campanella | 2ff88a9 | 2018-03-06 15:21:09 -0800 | [diff] [blame] | 448 | if (in.equals(cp) && trace.getInitialPacket().getCriterion(Criterion.Type.VLAN_VID) != null && |
| 449 | outputPath.getFinalPacket().getCriterion(Criterion.Type.VLAN_VID) != null |
| 450 | && ((VlanIdCriterion) trace.getInitialPacket().getCriterion(Criterion.Type.VLAN_VID)).vlanId() |
| 451 | .equals(((VlanIdCriterion) outputPath.getFinalPacket().getCriterion(Criterion.Type.VLAN_VID)) |
| 452 | .vlanId())) { |
Andrea Campanella | 5af2d2b | 2018-03-12 19:25:44 -0700 | [diff] [blame] | 453 | if (trace.getGroupOuputs(in.deviceId()).size() == 1 && |
| 454 | computePath(completePath, trace, outputPath.getOutput())) { |
Andrea Campanella | fe5d8df | 2018-03-12 11:07:35 -0700 | [diff] [blame] | 455 | trace.addResultMessage("Connect point out " + cp + " is same as initial input " + in); |
Andrea Campanella | 5af2d2b | 2018-03-12 19:25:44 -0700 | [diff] [blame] | 456 | trace.setSuccess(false); |
Andrea Campanella | fe5d8df | 2018-03-12 11:07:35 -0700 | [diff] [blame] | 457 | } |
Andrea Campanella | d5e16ff | 2018-03-21 10:08:55 -0700 | [diff] [blame] | 458 | } else if (!Collections.disjoint(hostsList, hosts)) { |
Andrea Campanella | 5af2d2b | 2018-03-12 19:25:44 -0700 | [diff] [blame] | 459 | //If the two host collections contain the same item it means we reached the proper output |
Andrea Campanella | b022b5e | 2018-01-31 14:59:03 +0100 | [diff] [blame] | 460 | log.debug("Stopping here because host is expected destination {}, reached through", completePath); |
Andrea Campanella | 696ef03 | 2018-02-27 18:03:17 +0100 | [diff] [blame] | 461 | if (computePath(completePath, trace, outputPath.getOutput())) { |
| 462 | trace.addResultMessage("Reached required destination Host " + cp); |
Andrea Campanella | 0cc6acd | 2018-02-28 16:43:16 +0100 | [diff] [blame] | 463 | trace.setSuccess(true); |
Andrea Campanella | 696ef03 | 2018-02-27 18:03:17 +0100 | [diff] [blame] | 464 | } |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 465 | break; |
Andrea Campanella | 54923d6 | 2018-01-23 12:46:04 +0100 | [diff] [blame] | 466 | } else if (cp.port().equals(PortNumber.CONTROLLER)) { |
Andrea Campanella | 8292ba6 | 2018-01-31 16:43:23 +0100 | [diff] [blame] | 467 | |
Andrea Campanella | 54923d6 | 2018-01-23 12:46:04 +0100 | [diff] [blame] | 468 | //Getting the master when the packet gets sent as packet in |
Seyeon Jeong | 83e7986 | 2020-02-28 01:17:34 -0800 | [diff] [blame^] | 469 | NodeId master = mastershipNib.getMasterFor(cp.deviceId()); |
| 470 | // TODO if we don't need to print master node id, exclude mastership NIB which is used only here |
Andrea Campanella | 1445f7a | 2018-02-07 12:00:12 +0100 | [diff] [blame] | 471 | trace.addResultMessage(PACKET_TO_CONTROLLER + " " + master.id()); |
Andrea Campanella | 54923d6 | 2018-01-23 12:46:04 +0100 | [diff] [blame] | 472 | computePath(completePath, trace, outputPath.getOutput()); |
Andrea Campanella | e679801 | 2018-02-06 15:46:52 +0100 | [diff] [blame] | 473 | handleVlanToController(outputPath, trace); |
Andrea Campanella | 54923d6 | 2018-01-23 12:46:04 +0100 | [diff] [blame] | 474 | |
Seyeon Jeong | 83e7986 | 2020-02-28 01:17:34 -0800 | [diff] [blame^] | 475 | } else if (linkNib.getEgressLinks(cp).size() > 0) { |
Andrea Campanella | b022b5e | 2018-01-31 14:59:03 +0100 | [diff] [blame] | 476 | |
| 477 | //TODO this can be optimized if we use a Tree structure for paths. |
| 478 | //if we already have outputs let's check if the one we are considering starts from one of the devices |
| 479 | // in any of the ones we have. |
| 480 | if (trace.getCompletePaths().size() > 0) { |
| 481 | ConnectPoint inputForOutput = null; |
| 482 | List<ConnectPoint> previousPath = new ArrayList<>(); |
| 483 | for (List<ConnectPoint> path : trace.getCompletePaths()) { |
| 484 | for (ConnectPoint connect : path) { |
| 485 | //if the path already contains the input for the output we've found we use it |
| 486 | if (connect.equals(in)) { |
| 487 | inputForOutput = connect; |
| 488 | previousPath = path; |
| 489 | break; |
| 490 | } |
| 491 | } |
| 492 | } |
Andrea Campanella | 3ddbadb | 2018-03-09 14:52:10 -0800 | [diff] [blame] | 493 | |
Andrea Campanella | b022b5e | 2018-01-31 14:59:03 +0100 | [diff] [blame] | 494 | //we use the pre-existing path up to the point we fork to a new output |
| 495 | if (inputForOutput != null && completePath.contains(inputForOutput)) { |
| 496 | List<ConnectPoint> temp = new ArrayList<>(previousPath); |
Andrea Campanella | 3ddbadb | 2018-03-09 14:52:10 -0800 | [diff] [blame] | 497 | temp = temp.subList(0, previousPath.indexOf(inputForOutput) + 1); |
| 498 | if (completePath.containsAll(temp)) { |
| 499 | completePath = temp; |
| 500 | } |
Andrea Campanella | b022b5e | 2018-01-31 14:59:03 +0100 | [diff] [blame] | 501 | } |
| 502 | } |
| 503 | |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 504 | //let's add the ouput for the input |
| 505 | completePath.add(cp); |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 506 | //let's compute the links for the given output |
Seyeon Jeong | 83e7986 | 2020-02-28 01:17:34 -0800 | [diff] [blame^] | 507 | Set<Link> links = linkNib.getEgressLinks(cp); |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 508 | log.debug("Egress Links {}", links); |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 509 | //For each link we trace the corresponding device |
| 510 | for (Link link : links) { |
| 511 | ConnectPoint dst = link.dst(); |
| 512 | //change in-port to the dst link in port |
Andrea Campanella | 8292ba6 | 2018-01-31 16:43:23 +0100 | [diff] [blame] | 513 | Builder updatedPacket = DefaultTrafficSelector.builder(); |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 514 | outputPath.getFinalPacket().criteria().forEach(updatedPacket::add); |
| 515 | updatedPacket.add(Criteria.matchInPort(dst.port())); |
| 516 | log.debug("DST Connect Point {}", dst); |
| 517 | //build the elements for that device |
Andrea Campanella | d5e16ff | 2018-03-21 10:08:55 -0700 | [diff] [blame] | 518 | traceInDevice(trace, updatedPacket.build(), dst, isDualHomed, completePath); |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 519 | //continue the trace along the path |
Andrea Campanella | 696ef03 | 2018-02-27 18:03:17 +0100 | [diff] [blame] | 520 | getTrace(completePath, dst, trace, isDualHomed); |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 521 | } |
Seyeon Jeong | 83e7986 | 2020-02-28 01:17:34 -0800 | [diff] [blame^] | 522 | } else if (edgePortNib.isEdgePoint(outputPath.getOutput()) && |
Andrea Campanella | 4c6170a | 2018-01-17 16:34:51 +0100 | [diff] [blame] | 523 | trace.getInitialPacket().getCriterion(Criterion.Type.ETH_DST) != null && |
| 524 | ((EthCriterion) trace.getInitialPacket().getCriterion(Criterion.Type.ETH_DST)) |
| 525 | .mac().isMulticast()) { |
| 526 | trace.addResultMessage("Packet is multicast and reached output " + outputPath.getOutput() + |
| 527 | " which is enabled and is edge port"); |
Andrea Campanella | 0cc6acd | 2018-02-28 16:43:16 +0100 | [diff] [blame] | 528 | trace.setSuccess(true); |
Andrea Campanella | 4c6170a | 2018-01-17 16:34:51 +0100 | [diff] [blame] | 529 | computePath(completePath, trace, outputPath.getOutput()); |
Andrea Campanella | 4c6170a | 2018-01-17 16:34:51 +0100 | [diff] [blame] | 530 | if (!hasOtherOutput(in.deviceId(), trace, outputPath.getOutput())) { |
| 531 | return trace; |
| 532 | } |
Seyeon Jeong | 83e7986 | 2020-02-28 01:17:34 -0800 | [diff] [blame^] | 533 | } else if (deviceNib.getPort(cp) != null && deviceNib.getPort(cp).isEnabled()) { |
Andrea Campanella | 1445f7a | 2018-02-07 12:00:12 +0100 | [diff] [blame] | 534 | EthTypeCriterion ethTypeCriterion = (EthTypeCriterion) trace.getInitialPacket() |
| 535 | .getCriterion(Criterion.Type.ETH_TYPE); |
| 536 | //We treat as correct output only if it's not LLDP or BDDP |
| 537 | if (!(ethTypeCriterion.ethType().equals(EtherType.LLDP.ethType()) |
Andrea Campanella | c6d10fc | 2018-02-27 12:42:28 +0100 | [diff] [blame] | 538 | && !ethTypeCriterion.ethType().equals(EtherType.BDDP.ethType()))) { |
Andrea Campanella | 08d07e1 | 2018-03-07 14:27:54 -0800 | [diff] [blame] | 539 | if (computePath(completePath, trace, outputPath.getOutput())) { |
| 540 | if (hostsList.isEmpty()) { |
| 541 | trace.addResultMessage("Packet is " + ((EthTypeCriterion) outputPath.getFinalPacket() |
| 542 | .getCriterion(Criterion.Type.ETH_TYPE)).ethType() + " and reached " + |
| 543 | cp + " with no hosts connected "); |
| 544 | } else { |
| 545 | IpAddress ipAddress = null; |
| 546 | if (trace.getInitialPacket().getCriterion(Criterion.Type.IPV4_DST) != null) { |
| 547 | ipAddress = ((IPCriterion) trace.getInitialPacket() |
| 548 | .getCriterion(Criterion.Type.IPV4_DST)).ip().address(); |
| 549 | } else if (trace.getInitialPacket().getCriterion(Criterion.Type.IPV6_DST) != null) { |
| 550 | ipAddress = ((IPCriterion) trace.getInitialPacket() |
| 551 | .getCriterion(Criterion.Type.IPV6_DST)).ip().address(); |
| 552 | } |
| 553 | if (ipAddress != null) { |
| 554 | IpAddress finalIpAddress = ipAddress; |
| 555 | if (hostsList.stream().anyMatch(host -> host.ipAddresses().contains(finalIpAddress)) || |
Seyeon Jeong | 83e7986 | 2020-02-28 01:17:34 -0800 | [diff] [blame^] | 556 | hostNib.getHostsByIp(finalIpAddress).isEmpty()) { |
Andrea Campanella | 08d07e1 | 2018-03-07 14:27:54 -0800 | [diff] [blame] | 557 | trace.addResultMessage("Packet is " + |
| 558 | ((EthTypeCriterion) outputPath.getFinalPacket() |
Andrea Campanella | 7fa8f0a | 2018-03-09 15:30:22 -0800 | [diff] [blame] | 559 | .getCriterion(Criterion.Type.ETH_TYPE)).ethType() + |
| 560 | " and reached " + cp + " with hosts " + hostsList); |
Andrea Campanella | 08d07e1 | 2018-03-07 14:27:54 -0800 | [diff] [blame] | 561 | } else { |
| 562 | trace.addResultMessage("Wrong output " + cp + " for required destination ip " + |
| 563 | ipAddress); |
Andrea Campanella | 5af2d2b | 2018-03-12 19:25:44 -0700 | [diff] [blame] | 564 | trace.setSuccess(false); |
Andrea Campanella | 08d07e1 | 2018-03-07 14:27:54 -0800 | [diff] [blame] | 565 | } |
| 566 | } else { |
Andrea Campanella | 2ff88a9 | 2018-03-06 15:21:09 -0800 | [diff] [blame] | 567 | trace.addResultMessage("Packet is " + ((EthTypeCriterion) outputPath.getFinalPacket() |
| 568 | .getCriterion(Criterion.Type.ETH_TYPE)).ethType() + " and reached " + |
| 569 | cp + " with hosts " + hostsList); |
Andrea Campanella | 2ff88a9 | 2018-03-06 15:21:09 -0800 | [diff] [blame] | 570 | } |
Andrea Campanella | 2ff88a9 | 2018-03-06 15:21:09 -0800 | [diff] [blame] | 571 | } |
Andrea Campanella | 08d07e1 | 2018-03-07 14:27:54 -0800 | [diff] [blame] | 572 | trace.setSuccess(true); |
Andrea Campanella | 1445f7a | 2018-02-07 12:00:12 +0100 | [diff] [blame] | 573 | } |
Andrea Campanella | 8292ba6 | 2018-01-31 16:43:23 +0100 | [diff] [blame] | 574 | } |
Andrea Campanella | 8292ba6 | 2018-01-31 16:43:23 +0100 | [diff] [blame] | 575 | |
| 576 | } else { |
Andrea Campanella | 8292ba6 | 2018-01-31 16:43:23 +0100 | [diff] [blame] | 577 | computePath(completePath, trace, cp); |
Andrea Campanella | 5af2d2b | 2018-03-12 19:25:44 -0700 | [diff] [blame] | 578 | trace.setSuccess(false); |
Seyeon Jeong | 83e7986 | 2020-02-28 01:17:34 -0800 | [diff] [blame^] | 579 | if (deviceNib.getPort(cp) == null) { |
Jon Hall | 4a28fc9 | 2018-04-24 18:03:10 -0700 | [diff] [blame] | 580 | //Port is not existent on device. |
Andrea Campanella | e24d492 | 2018-03-26 10:39:22 -0700 | [diff] [blame] | 581 | log.warn("Port {} is not available on device.", cp); |
| 582 | trace.addResultMessage("Port " + cp + "is not available on device. Packet is dropped"); |
| 583 | } else { |
| 584 | //No links means that the packet gets dropped. |
| 585 | log.warn("No links out of {}", cp); |
| 586 | trace.addResultMessage("No links depart from " + cp + ". Packet is dropped"); |
| 587 | } |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 588 | } |
| 589 | } |
| 590 | return trace; |
| 591 | } |
| 592 | |
Andrea Campanella | 4c6170a | 2018-01-17 16:34:51 +0100 | [diff] [blame] | 593 | |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 594 | /** |
Andrea Campanella | e679801 | 2018-02-06 15:46:52 +0100 | [diff] [blame] | 595 | * If the initial packet comes tagged with a Vlan we output it with that to ONOS. |
| 596 | * If ONOS applied a vlan we remove it. |
| 597 | * |
| 598 | * @param outputPath the output |
| 599 | * @param trace the trace we are building |
| 600 | */ |
Andrea Campanella | 4c6170a | 2018-01-17 16:34:51 +0100 | [diff] [blame] | 601 | |
Andrea Campanella | e679801 | 2018-02-06 15:46:52 +0100 | [diff] [blame] | 602 | private void handleVlanToController(GroupsInDevice outputPath, StaticPacketTrace trace) { |
| 603 | |
| 604 | VlanIdCriterion initialVid = (VlanIdCriterion) trace.getInitialPacket().getCriterion(Criterion.Type.VLAN_VID); |
| 605 | VlanIdCriterion finalVid = (VlanIdCriterion) outputPath.getFinalPacket().getCriterion(Criterion.Type.VLAN_VID); |
| 606 | |
| 607 | if (initialVid != null && !initialVid.equals(finalVid) && initialVid.vlanId().equals(VlanId.NONE)) { |
| 608 | |
| 609 | Set<Criterion> finalCriteria = new HashSet<>(outputPath.getFinalPacket().criteria()); |
| 610 | //removing the final vlanId |
| 611 | finalCriteria.remove(finalVid); |
| 612 | Builder packetUpdated = DefaultTrafficSelector.builder(); |
| 613 | finalCriteria.forEach(packetUpdated::add); |
| 614 | //Initial was none so we set it to that |
| 615 | packetUpdated.add(Criteria.matchVlanId(VlanId.NONE)); |
| 616 | //Update final packet |
| 617 | outputPath.setFinalPacket(packetUpdated.build()); |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | /** |
Andrea Campanella | 4c6170a | 2018-01-17 16:34:51 +0100 | [diff] [blame] | 622 | * Checks if the device has other outputs than the given connect point. |
| 623 | * |
| 624 | * @param inDeviceId the device |
| 625 | * @param trace the trace we are building |
| 626 | * @param cp an output connect point |
| 627 | * @return true if the device has other outputs. |
| 628 | */ |
| 629 | private boolean hasOtherOutput(DeviceId inDeviceId, StaticPacketTrace trace, ConnectPoint cp) { |
| 630 | return trace.getGroupOuputs(inDeviceId).stream().filter(groupsInDevice -> { |
| 631 | return !groupsInDevice.getOutput().equals(cp); |
| 632 | }).count() > 0; |
| 633 | } |
| 634 | |
| 635 | /** |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 636 | * Checks if the path contains the device. |
| 637 | * |
| 638 | * @param completePath the path |
| 639 | * @param deviceId the device to check |
| 640 | * @return true if the path contains the device |
| 641 | */ |
| 642 | //TODO might prove costly, improvement: a class with both CPs and DeviceIds point. |
| 643 | private boolean pathContainsDevice(List<ConnectPoint> completePath, DeviceId deviceId) { |
| 644 | for (ConnectPoint cp : completePath) { |
| 645 | if (cp.deviceId().equals(deviceId)) { |
| 646 | return true; |
| 647 | } |
| 648 | } |
| 649 | return false; |
| 650 | } |
| 651 | |
| 652 | /** |
| 653 | * Gets the hosts for the given initial packet. |
| 654 | * |
| 655 | * @param trace the trace we are building |
| 656 | * @return set of the hosts we are trying to reach |
| 657 | */ |
| 658 | private Set<Host> getHosts(StaticPacketTrace trace) { |
| 659 | IPCriterion ipv4Criterion = ((IPCriterion) trace.getInitialPacket() |
| 660 | .getCriterion(Criterion.Type.IPV4_DST)); |
| 661 | IPCriterion ipv6Criterion = ((IPCriterion) trace.getInitialPacket() |
| 662 | .getCriterion(Criterion.Type.IPV6_DST)); |
| 663 | Set<Host> hosts = new HashSet<>(); |
| 664 | if (ipv4Criterion != null) { |
Seyeon Jeong | 83e7986 | 2020-02-28 01:17:34 -0800 | [diff] [blame^] | 665 | hosts.addAll(hostNib.getHostsByIp(ipv4Criterion.ip().address())); |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 666 | } |
| 667 | if (ipv6Criterion != null) { |
Seyeon Jeong | 83e7986 | 2020-02-28 01:17:34 -0800 | [diff] [blame^] | 668 | hosts.addAll(hostNib.getHostsByIp(ipv6Criterion.ip().address())); |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 669 | } |
| 670 | EthCriterion ethCriterion = ((EthCriterion) trace.getInitialPacket() |
| 671 | .getCriterion(Criterion.Type.ETH_DST)); |
| 672 | if (ethCriterion != null) { |
Seyeon Jeong | 83e7986 | 2020-02-28 01:17:34 -0800 | [diff] [blame^] | 673 | hosts.addAll(hostNib.getHostsByMac(ethCriterion.mac())); |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 674 | } |
| 675 | return hosts; |
| 676 | } |
| 677 | |
| 678 | /** |
| 679 | * Computes the list of traversed connect points. |
| 680 | * |
| 681 | * @param completePath the list of devices |
| 682 | * @param trace the trace we are building |
| 683 | * @param output the final output connect point |
| 684 | */ |
Andrea Campanella | 696ef03 | 2018-02-27 18:03:17 +0100 | [diff] [blame] | 685 | private boolean computePath(List<ConnectPoint> completePath, StaticPacketTrace trace, ConnectPoint output) { |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 686 | List<ConnectPoint> traverseList = new ArrayList<>(); |
| 687 | if (!completePath.contains(trace.getInitialConnectPoint())) { |
| 688 | traverseList.add(trace.getInitialConnectPoint()); |
| 689 | } |
Andrea Campanella | 27140da | 2018-04-09 14:22:32 +0200 | [diff] [blame] | 690 | |
| 691 | if (output != null && trace.getInitialConnectPoint().deviceId().equals(output.deviceId())) { |
| 692 | trace.addCompletePath(ImmutableList.of(trace.getInitialConnectPoint(), output)); |
| 693 | return true; |
| 694 | } |
| 695 | |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 696 | traverseList.addAll(completePath); |
| 697 | if (output != null && !completePath.contains(output)) { |
| 698 | traverseList.add(output); |
| 699 | } |
Andrea Campanella | 696ef03 | 2018-02-27 18:03:17 +0100 | [diff] [blame] | 700 | if (!trace.getCompletePaths().contains(traverseList)) { |
Andrea Campanella | 3ddbadb | 2018-03-09 14:52:10 -0800 | [diff] [blame] | 701 | trace.addCompletePath(ImmutableList.copyOf(traverseList)); |
Andrea Campanella | 696ef03 | 2018-02-27 18:03:17 +0100 | [diff] [blame] | 702 | return true; |
| 703 | } |
| 704 | return false; |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | /** |
| 708 | * Traces the packet inside a device starting from an input connect point. |
| 709 | * |
Andrea Campanella | d5e16ff | 2018-03-21 10:08:55 -0700 | [diff] [blame] | 710 | * @param trace the trace we are building |
| 711 | * @param packet the packet we are tracing |
| 712 | * @param in the input connect point. |
| 713 | * @param isDualHomed true if the trace we are doing starts or ends in a dual homed host |
| 714 | * @param completePath the path up until this device |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 715 | * @return updated trace |
| 716 | */ |
Andrea Campanella | 696ef03 | 2018-02-27 18:03:17 +0100 | [diff] [blame] | 717 | private StaticPacketTrace traceInDevice(StaticPacketTrace trace, TrafficSelector packet, ConnectPoint in, |
Andrea Campanella | d5e16ff | 2018-03-21 10:08:55 -0700 | [diff] [blame] | 718 | boolean isDualHomed, List<ConnectPoint> completePath) { |
Andrea Campanella | b022b5e | 2018-01-31 14:59:03 +0100 | [diff] [blame] | 719 | |
Andrea Campanella | 08d07e1 | 2018-03-07 14:27:54 -0800 | [diff] [blame] | 720 | boolean multipleRoutes = false; |
| 721 | if (trace.getGroupOuputs(in.deviceId()) != null) { |
| 722 | multipleRoutes = multipleRoutes(trace); |
| 723 | } |
| 724 | if (trace.getGroupOuputs(in.deviceId()) != null && !isDualHomed && !multipleRoutes) { |
Andrea Campanella | b022b5e | 2018-01-31 14:59:03 +0100 | [diff] [blame] | 725 | log.debug("Trace already contains device and given outputs"); |
| 726 | return trace; |
| 727 | } |
Andrea Campanella | 08d07e1 | 2018-03-07 14:27:54 -0800 | [diff] [blame] | 728 | |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 729 | log.debug("Packet {} coming in from {}", packet, in); |
Andrea Campanella | 17d4519 | 2018-01-18 17:11:42 +0100 | [diff] [blame] | 730 | |
| 731 | //if device is not available exit here. |
Seyeon Jeong | 83e7986 | 2020-02-28 01:17:34 -0800 | [diff] [blame^] | 732 | if (!deviceNib.isAvailable(in.deviceId())) { |
Andrea Campanella | 17d4519 | 2018-01-18 17:11:42 +0100 | [diff] [blame] | 733 | trace.addResultMessage("Device is offline " + in.deviceId()); |
Andrea Campanella | d5e16ff | 2018-03-21 10:08:55 -0700 | [diff] [blame] | 734 | computePath(completePath, trace, null); |
Andrea Campanella | 17d4519 | 2018-01-18 17:11:42 +0100 | [diff] [blame] | 735 | return trace; |
| 736 | } |
| 737 | |
Andrea Campanella | 1445f7a | 2018-02-07 12:00:12 +0100 | [diff] [blame] | 738 | //handle when the input is the controller |
| 739 | //NOTE, we are using the input port as a convenience to carry the CONTROLLER port number even if |
| 740 | // a packet in from the controller will not actually traverse the pipeline and have no such notion |
| 741 | // as the input port. |
| 742 | if (in.port().equals(PortNumber.CONTROLLER)) { |
| 743 | StaticPacketTrace outputTrace = inputFromController(trace, in); |
| 744 | if (outputTrace != null) { |
| 745 | return trace; |
| 746 | } |
| 747 | } |
| 748 | |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 749 | List<FlowEntry> flows = new ArrayList<>(); |
| 750 | List<FlowEntry> outputFlows = new ArrayList<>(); |
Andrea Campanella | 8292ba6 | 2018-01-31 16:43:23 +0100 | [diff] [blame] | 751 | List<Instruction> deferredInstructions = new ArrayList<>(); |
| 752 | |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 753 | FlowEntry nextTableIdEntry = findNextTableIdEntry(in.deviceId(), -1); |
| 754 | if (nextTableIdEntry == null) { |
| 755 | trace.addResultMessage("No flow rules for device " + in.deviceId() + ". Aborting"); |
Andrea Campanella | d5e16ff | 2018-03-21 10:08:55 -0700 | [diff] [blame] | 756 | computePath(completePath, trace, null); |
Andrea Campanella | 5af2d2b | 2018-03-12 19:25:44 -0700 | [diff] [blame] | 757 | trace.setSuccess(false); |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 758 | return trace; |
| 759 | } |
| 760 | TableId tableId = nextTableIdEntry.table(); |
| 761 | FlowEntry flowEntry; |
| 762 | boolean output = false; |
| 763 | while (!output) { |
| 764 | log.debug("Searching a Flow Entry on table {} for packet {}", tableId, packet); |
| 765 | //get the rule that matches the incoming packet |
| 766 | flowEntry = matchHighestPriority(packet, in, tableId); |
| 767 | log.debug("Found Flow Entry {}", flowEntry); |
| 768 | |
| 769 | boolean isOfdpaHardware = TroubleshootUtils.hardwareOfdpaMap |
Seyeon Jeong | 83e7986 | 2020-02-28 01:17:34 -0800 | [diff] [blame^] | 770 | .getOrDefault(driverNib.getDriverName(in.deviceId()), false); |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 771 | |
| 772 | //if the flow entry on a table is null and we are on hardware we treat as table miss, with few exceptions |
| 773 | if (flowEntry == null && isOfdpaHardware) { |
| 774 | log.debug("Ofdpa Hw setup, no flow rule means table miss"); |
| 775 | |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 776 | if (((IndexTableId) tableId).id() == 27) { |
| 777 | //Apparently a miss but Table 27 on OFDPA is a fixed table |
| 778 | packet = handleOfdpa27FixedTable(trace, packet); |
| 779 | } |
| 780 | |
| 781 | //Finding next table to go In case of miss |
| 782 | nextTableIdEntry = findNextTableIdEntry(in.deviceId(), ((IndexTableId) tableId).id()); |
| 783 | log.debug("Next table id entry {}", nextTableIdEntry); |
| 784 | |
| 785 | //FIXME find better solution that enable granularity greater than 0 or all rules |
| 786 | //(another possibility is max tableId) |
| 787 | if (nextTableIdEntry == null && flows.size() == 0) { |
Andrea Campanella | 09eec85 | 2018-02-05 19:39:25 +0100 | [diff] [blame] | 788 | trace.addResultMessage("No matching flow rules for device " + in.deviceId() + ". Aborting"); |
Andrea Campanella | d5e16ff | 2018-03-21 10:08:55 -0700 | [diff] [blame] | 789 | computePath(completePath, trace, null); |
Andrea Campanella | 5af2d2b | 2018-03-12 19:25:44 -0700 | [diff] [blame] | 790 | trace.setSuccess(false); |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 791 | return trace; |
| 792 | |
| 793 | } else if (nextTableIdEntry == null) { |
| 794 | //Means that no more flow rules are present |
| 795 | output = true; |
| 796 | |
| 797 | } else if (((IndexTableId) tableId).id() == 20) { |
| 798 | //if the table is 20 OFDPA skips to table 50 |
| 799 | log.debug("A miss on Table 20 on OFDPA means that we skip directly to table 50"); |
| 800 | tableId = IndexTableId.of(50); |
| 801 | |
Andrea Campanella | cdf35cb | 2018-06-05 12:04:02 +0200 | [diff] [blame] | 802 | } else if (((IndexTableId) tableId).id() == 40) { |
| 803 | //if the table is 40 OFDPA skips to table 60 |
| 804 | log.debug("A miss on Table 40 on OFDPA means that we skip directly to table 60"); |
| 805 | tableId = IndexTableId.of(60); |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 806 | } else { |
| 807 | tableId = nextTableIdEntry.table(); |
| 808 | } |
| 809 | |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 810 | } else if (flowEntry == null) { |
| 811 | trace.addResultMessage("Packet has no match on table " + tableId + " in device " + |
| 812 | in.deviceId() + ". Dropping"); |
Andrea Campanella | d5e16ff | 2018-03-21 10:08:55 -0700 | [diff] [blame] | 813 | computePath(completePath, trace, null); |
Andrea Campanella | 5af2d2b | 2018-03-12 19:25:44 -0700 | [diff] [blame] | 814 | trace.setSuccess(false); |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 815 | return trace; |
| 816 | } else { |
Andrea Campanella | 94c594a | 2018-02-06 18:58:40 +0100 | [diff] [blame] | 817 | |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 818 | //IF the table has a transition |
| 819 | if (flowEntry.treatment().tableTransition() != null) { |
| 820 | //update the next table we transitions to |
| 821 | tableId = IndexTableId.of(flowEntry.treatment().tableTransition().tableId()); |
| 822 | log.debug("Flow Entry has transition to table Id {}", tableId); |
| 823 | flows.add(flowEntry); |
| 824 | } else { |
| 825 | //table has no transition so it means that it's an output rule if on the last table |
| 826 | log.debug("Flow Entry has no transition to table, treating as last rule {}", flowEntry); |
| 827 | flows.add(flowEntry); |
| 828 | outputFlows.add(flowEntry); |
| 829 | output = true; |
| 830 | } |
Andrea Campanella | 8292ba6 | 2018-01-31 16:43:23 +0100 | [diff] [blame] | 831 | //update the packet according to the immediate actions of this flow rule. |
| 832 | packet = updatePacket(packet, flowEntry.treatment().immediate()).build(); |
| 833 | |
| 834 | //save the deferred rules for later |
| 835 | deferredInstructions.addAll(flowEntry.treatment().deferred()); |
| 836 | |
| 837 | //If the flow requires to clear deferred actions we do so for all the ones we encountered. |
| 838 | if (flowEntry.treatment().clearedDeferred()) { |
| 839 | deferredInstructions.clear(); |
| 840 | } |
| 841 | |
Andrea Campanella | 94c594a | 2018-02-06 18:58:40 +0100 | [diff] [blame] | 842 | //On table 10 OFDPA needs two rules to apply the vlan if none and then to transition to the next table. |
| 843 | if (needsSecondTable10Flow(flowEntry, isOfdpaHardware)) { |
| 844 | |
| 845 | //Let's get the packet vlanId instruction |
| 846 | VlanIdCriterion packetVlanIdCriterion = |
| 847 | (VlanIdCriterion) packet.getCriterion(Criterion.Type.VLAN_VID); |
| 848 | |
| 849 | //Let's get the flow entry vlan mod instructions |
| 850 | ModVlanIdInstruction entryModVlanIdInstruction = (ModVlanIdInstruction) flowEntry.treatment() |
| 851 | .immediate().stream() |
| 852 | .filter(instruction -> instruction instanceof ModVlanIdInstruction) |
| 853 | .findFirst().orElse(null); |
| 854 | |
| 855 | //If the entry modVlan is not null we need to make sure that the packet has been updated and there |
| 856 | // is a flow rule that matches on same criteria and with updated vlanId |
| 857 | if (entryModVlanIdInstruction != null) { |
| 858 | |
| 859 | FlowEntry secondVlanFlow = getSecondFlowEntryOnTable10(packet, in, |
| 860 | packetVlanIdCriterion, entryModVlanIdInstruction); |
| 861 | |
| 862 | //We found the flow that we expected |
| 863 | if (secondVlanFlow != null) { |
| 864 | flows.add(secondVlanFlow); |
| 865 | } else { |
| 866 | trace.addResultMessage("Missing forwarding rule for tagged packet on " + in); |
Andrea Campanella | d5e16ff | 2018-03-21 10:08:55 -0700 | [diff] [blame] | 867 | computePath(completePath, trace, null); |
Andrea Campanella | 94c594a | 2018-02-06 18:58:40 +0100 | [diff] [blame] | 868 | return trace; |
| 869 | } |
| 870 | } |
| 871 | |
| 872 | } |
| 873 | |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 874 | } |
| 875 | } |
| 876 | |
| 877 | //Creating a modifiable builder for the output packet |
Andrea Campanella | 8292ba6 | 2018-01-31 16:43:23 +0100 | [diff] [blame] | 878 | Builder builder = DefaultTrafficSelector.builder(); |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 879 | packet.criteria().forEach(builder::add); |
Andrea Campanella | 8292ba6 | 2018-01-31 16:43:23 +0100 | [diff] [blame] | 880 | |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 881 | //Adding all the flows to the trace |
Andrea Campanella | 54923d6 | 2018-01-23 12:46:04 +0100 | [diff] [blame] | 882 | trace.addFlowsForDevice(in.deviceId(), ImmutableList.copyOf(flows)); |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 883 | |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 884 | List<PortNumber> outputPorts = new ArrayList<>(); |
Andrea Campanella | 08d07e1 | 2018-03-07 14:27:54 -0800 | [diff] [blame] | 885 | List<FlowEntry> outputFlowEntries = handleFlows(trace, packet, in, outputFlows, builder, outputPorts); |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 886 | |
Andrea Campanella | 08d07e1 | 2018-03-07 14:27:54 -0800 | [diff] [blame] | 887 | |
| 888 | log.debug("Handling Groups"); |
| 889 | //Analyze Groups |
| 890 | List<Group> groups = new ArrayList<>(); |
| 891 | |
| 892 | Collection<FlowEntry> nonOutputFlows = flows; |
| 893 | nonOutputFlows.removeAll(outputFlowEntries); |
| 894 | |
| 895 | //Handling groups pointed at by immediate instructions |
| 896 | for (FlowEntry entry : flows) { |
| 897 | getGroupsFromInstructions(trace, groups, entry.treatment().immediate(), |
Andrea Campanella | d5e16ff | 2018-03-21 10:08:55 -0700 | [diff] [blame] | 898 | entry.deviceId(), builder, outputPorts, in, completePath); |
Andrea Campanella | 08d07e1 | 2018-03-07 14:27:54 -0800 | [diff] [blame] | 899 | } |
| 900 | |
| 901 | //If we have deferred instructions at this point we handle them. |
| 902 | if (deferredInstructions.size() > 0) { |
Andrea Campanella | d5e16ff | 2018-03-21 10:08:55 -0700 | [diff] [blame] | 903 | builder = handleDeferredActions(trace, packet, in, deferredInstructions, outputPorts, groups, completePath); |
Andrea Campanella | 08d07e1 | 2018-03-07 14:27:54 -0800 | [diff] [blame] | 904 | |
| 905 | } |
| 906 | packet = builder.build(); |
| 907 | |
| 908 | log.debug("Output Packet {}", packet); |
| 909 | return trace; |
| 910 | } |
| 911 | |
| 912 | private List<FlowEntry> handleFlows(StaticPacketTrace trace, TrafficSelector packet, ConnectPoint in, |
| 913 | List<FlowEntry> outputFlows, Builder builder, List<PortNumber> outputPorts) { |
Andrea Campanella | 54923d6 | 2018-01-23 12:46:04 +0100 | [diff] [blame] | 914 | //TODO optimization |
Andrea Campanella | 54923d6 | 2018-01-23 12:46:04 +0100 | [diff] [blame] | 915 | //outputFlows contains also last rule of device, so we need filtering for OUTPUT instructions. |
| 916 | List<FlowEntry> outputFlowEntries = outputFlows.stream().filter(flow -> flow.treatment() |
| 917 | .allInstructions().stream().filter(instruction -> instruction.type() |
| 918 | .equals(Instruction.Type.OUTPUT)).count() > 0).collect(Collectors.toList()); |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 919 | |
Andrea Campanella | 54923d6 | 2018-01-23 12:46:04 +0100 | [diff] [blame] | 920 | if (outputFlowEntries.size() > 1) { |
| 921 | trace.addResultMessage("More than one flow rule with OUTPUT instruction"); |
| 922 | log.warn("There cannot be more than one flow entry with OUTPUT instruction for {}", packet); |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 923 | } |
Andrea Campanella | 54923d6 | 2018-01-23 12:46:04 +0100 | [diff] [blame] | 924 | |
| 925 | if (outputFlowEntries.size() == 1) { |
| 926 | |
| 927 | OutputInstruction outputInstruction = (OutputInstruction) outputFlowEntries.get(0).treatment() |
| 928 | .allInstructions().stream() |
| 929 | .filter(instruction -> { |
| 930 | return instruction.type().equals(Instruction.Type.OUTPUT); |
| 931 | }).findFirst().get(); |
| 932 | |
| 933 | //FIXME using GroupsInDevice for output even if flows. |
| 934 | buildOutputFromDevice(trace, in, builder, outputPorts, outputInstruction, ImmutableList.of()); |
| 935 | |
| 936 | } |
Andrea Campanella | 08d07e1 | 2018-03-07 14:27:54 -0800 | [diff] [blame] | 937 | return outputFlowEntries; |
| 938 | } |
Andrea Campanella | 54923d6 | 2018-01-23 12:46:04 +0100 | [diff] [blame] | 939 | |
Andrea Campanella | 08d07e1 | 2018-03-07 14:27:54 -0800 | [diff] [blame] | 940 | private boolean multipleRoutes(StaticPacketTrace trace) { |
| 941 | boolean multipleRoutes = false; |
| 942 | IPCriterion ipCriterion = ((IPCriterion) trace.getInitialPacket().getCriterion(Criterion.Type.IPV4_DST)); |
| 943 | IpAddress ip = null; |
| 944 | if (ipCriterion != null) { |
| 945 | ip = ipCriterion.ip().address(); |
| 946 | } else if (trace.getInitialPacket().getCriterion(Criterion.Type.IPV6_DST) != null) { |
| 947 | ip = ((IPCriterion) trace.getInitialPacket().getCriterion(Criterion.Type.IPV6_DST)).ip().address(); |
Andrea Campanella | 54923d6 | 2018-01-23 12:46:04 +0100 | [diff] [blame] | 948 | } |
Andrea Campanella | dd71aca | 2018-04-04 13:10:45 +0200 | [diff] [blame] | 949 | if (ip != null) { |
Seyeon Jeong | 83e7986 | 2020-02-28 01:17:34 -0800 | [diff] [blame^] | 950 | Optional<ResolvedRoute> optionalRoute = routeNib.longestPrefixLookup(ip); |
Andrea Campanella | dd71aca | 2018-04-04 13:10:45 +0200 | [diff] [blame] | 951 | if (optionalRoute.isPresent()) { |
| 952 | ResolvedRoute route = optionalRoute.get(); |
Seyeon Jeong | 83e7986 | 2020-02-28 01:17:34 -0800 | [diff] [blame^] | 953 | multipleRoutes = routeNib.getAllResolvedRoutes(route.prefix()).size() > 1; |
Andrea Campanella | dd71aca | 2018-04-04 13:10:45 +0200 | [diff] [blame] | 954 | } |
Andrea Campanella | 8292ba6 | 2018-01-31 16:43:23 +0100 | [diff] [blame] | 955 | } |
Andrea Campanella | 08d07e1 | 2018-03-07 14:27:54 -0800 | [diff] [blame] | 956 | return multipleRoutes; |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 957 | } |
| 958 | |
Andrea Campanella | 1445f7a | 2018-02-07 12:00:12 +0100 | [diff] [blame] | 959 | /** |
| 960 | * Handles the specific case where the Input is the controller. |
| 961 | * Note that the in port is used as a convenience to store the port of the controller even if the packet in |
| 962 | * from a controller should not have a physical input port. The in port from the Controller is used to make sure |
| 963 | * the flood to all active physical ports of the device. |
| 964 | * |
| 965 | * @param trace the trace |
| 966 | * @param in the controller port |
| 967 | * @return the augmented trace. |
| 968 | */ |
| 969 | private StaticPacketTrace inputFromController(StaticPacketTrace trace, ConnectPoint in) { |
| 970 | EthTypeCriterion ethTypeCriterion = (EthTypeCriterion) trace.getInitialPacket() |
| 971 | .getCriterion(Criterion.Type.ETH_TYPE); |
| 972 | //If the packet is LLDP or BDDP we flood it on all active ports of the switch. |
| 973 | if (ethTypeCriterion != null && (ethTypeCriterion.ethType().equals(EtherType.LLDP.ethType()) |
| 974 | || ethTypeCriterion.ethType().equals(EtherType.BDDP.ethType()))) { |
| 975 | //get the active ports |
Seyeon Jeong | 83e7986 | 2020-02-28 01:17:34 -0800 | [diff] [blame^] | 976 | List<Port> enabledPorts = deviceNib.getPorts(in.deviceId()).stream() |
Andrea Campanella | 1445f7a | 2018-02-07 12:00:12 +0100 | [diff] [blame] | 977 | .filter(Port::isEnabled) |
| 978 | .collect(Collectors.toList()); |
| 979 | //build an output from each one |
| 980 | enabledPorts.forEach(port -> { |
| 981 | GroupsInDevice output = new GroupsInDevice(new ConnectPoint(port.element().id(), port.number()), |
| 982 | ImmutableList.of(), trace.getInitialPacket()); |
| 983 | trace.addGroupOutputPath(in.deviceId(), output); |
| 984 | }); |
| 985 | return trace; |
| 986 | } |
| 987 | return null; |
| 988 | } |
| 989 | |
Andrea Campanella | 94c594a | 2018-02-06 18:58:40 +0100 | [diff] [blame] | 990 | private boolean needsSecondTable10Flow(FlowEntry flowEntry, boolean isOfdpaHardware) { |
| 991 | return isOfdpaHardware && flowEntry.table().equals(IndexTableId.of(10)) |
| 992 | && flowEntry.selector().getCriterion(Criterion.Type.VLAN_VID) != null |
| 993 | && ((VlanIdCriterion) flowEntry.selector().getCriterion(Criterion.Type.VLAN_VID)) |
| 994 | .vlanId().equals(VlanId.NONE); |
| 995 | } |
| 996 | |
| 997 | /** |
| 998 | * Method that finds a flow rule on table 10 that matches the packet and the VLAN of the already |
| 999 | * found rule on table 10. This is because OFDPA needs two rules on table 10, first to apply the rule, |
| 1000 | * second to transition to following table |
| 1001 | * |
| 1002 | * @param packet the incoming packet |
| 1003 | * @param in the input connect point |
| 1004 | * @param packetVlanIdCriterion the vlan criterion from the packet |
| 1005 | * @param entryModVlanIdInstruction the entry vlan instruction |
| 1006 | * @return the second flow entry that matched |
| 1007 | */ |
| 1008 | private FlowEntry getSecondFlowEntryOnTable10(TrafficSelector packet, ConnectPoint in, |
| 1009 | VlanIdCriterion packetVlanIdCriterion, |
| 1010 | ModVlanIdInstruction entryModVlanIdInstruction) { |
| 1011 | FlowEntry secondVlanFlow = null; |
| 1012 | //Check the packet has been update from the first rule. |
| 1013 | if (packetVlanIdCriterion.vlanId().equals(entryModVlanIdInstruction.vlanId())) { |
| 1014 | //find a rule on the same table that matches the vlan and |
| 1015 | // also all the other elements of the flow such as input port |
Seyeon Jeong | 83e7986 | 2020-02-28 01:17:34 -0800 | [diff] [blame^] | 1016 | secondVlanFlow = Lists.newArrayList(flowNib.getFlowEntriesByState(in.deviceId(), |
| 1017 | FlowEntry.FlowEntryState.ADDED) |
| 1018 | .iterator()).stream() |
Andrea Campanella | 94c594a | 2018-02-06 18:58:40 +0100 | [diff] [blame] | 1019 | .filter(entry -> { |
| 1020 | return entry.table().equals(IndexTableId.of(10)); |
| 1021 | }) |
| 1022 | .filter(entry -> { |
| 1023 | VlanIdCriterion criterion = (VlanIdCriterion) entry.selector() |
| 1024 | .getCriterion(Criterion.Type.VLAN_VID); |
| 1025 | return criterion != null && match(packet, entry) |
| 1026 | && criterion.vlanId().equals(entryModVlanIdInstruction.vlanId()); |
| 1027 | }).findFirst().orElse(null); |
| 1028 | |
| 1029 | } |
| 1030 | return secondVlanFlow; |
| 1031 | } |
| 1032 | |
Andrea Campanella | 8292ba6 | 2018-01-31 16:43:23 +0100 | [diff] [blame] | 1033 | |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 1034 | /** |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 1035 | * Handles table 27 in Ofpda which is a fixed table not visible to any controller that handles Mpls Labels. |
| 1036 | * |
| 1037 | * @param packet the incoming packet |
| 1038 | * @return the updated packet |
| 1039 | */ |
| 1040 | private TrafficSelector handleOfdpa27FixedTable(StaticPacketTrace trace, TrafficSelector packet) { |
| 1041 | log.debug("Handling table 27 on OFDPA, removing mpls ETH Type and change mpls label"); |
| 1042 | Criterion mplsCriterion = packet.getCriterion(Criterion.Type.ETH_TYPE); |
| 1043 | ImmutableList.Builder<Instruction> builder = ImmutableList.builder(); |
| 1044 | |
| 1045 | //If the pakcet comes in with the expected elements we update it as per OFDPA spec. |
| 1046 | if (mplsCriterion != null && ((EthTypeCriterion) mplsCriterion).ethType() |
| 1047 | .equals(EtherType.MPLS_UNICAST.ethType())) { |
Andrea Campanella | 3970e47 | 2018-01-25 16:44:04 +0100 | [diff] [blame] | 1048 | //TODO update with parsing with eth MPLS pop Instruction for treating label an bos |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 1049 | Instruction ethInstruction = Instructions.popMpls(((EthTypeCriterion) trace.getInitialPacket() |
| 1050 | .getCriterion(Criterion.Type.ETH_TYPE)).ethType()); |
| 1051 | //FIXME what do we use as L3_Unicast mpls Label ? |
Andrea Campanella | 3970e47 | 2018-01-25 16:44:04 +0100 | [diff] [blame] | 1052 | //translateInstruction(builder, ethInstruction); |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 1053 | builder.add(ethInstruction); |
| 1054 | } |
| 1055 | packet = updatePacket(packet, builder.build()).build(); |
| 1056 | return packet; |
| 1057 | } |
| 1058 | |
| 1059 | /** |
| 1060 | * Finds the flow entry with the minimun next table Id. |
| 1061 | * |
| 1062 | * @param deviceId the device to search |
| 1063 | * @param currentId the current id. the search will use this as minimum |
| 1064 | * @return the flow entry with the minimum table Id after the given one. |
| 1065 | */ |
| 1066 | private FlowEntry findNextTableIdEntry(DeviceId deviceId, int currentId) { |
| 1067 | |
| 1068 | final Comparator<FlowEntry> comparator = Comparator.comparing((FlowEntry f) -> ((IndexTableId) f.table()).id()); |
Seyeon Jeong | 83e7986 | 2020-02-28 01:17:34 -0800 | [diff] [blame^] | 1069 | return Lists.newArrayList(flowNib.getFlowEntriesByState(deviceId, FlowEntry.FlowEntryState.ADDED) |
Andrea Campanella | 39ec33e | 2018-04-13 12:14:48 +0200 | [diff] [blame] | 1070 | .iterator()).stream() |
| 1071 | .filter(f -> ((IndexTableId) f.table()).id() > currentId).min(comparator).orElse(null); |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 1072 | } |
| 1073 | |
Andrea Campanella | 8292ba6 | 2018-01-31 16:43:23 +0100 | [diff] [blame] | 1074 | private Builder handleDeferredActions(StaticPacketTrace trace, TrafficSelector packet, |
| 1075 | ConnectPoint in, List<Instruction> deferredInstructions, |
Andrea Campanella | d5e16ff | 2018-03-21 10:08:55 -0700 | [diff] [blame] | 1076 | List<PortNumber> outputPorts, List<Group> groups, |
| 1077 | List<ConnectPoint> completePath) { |
Andrea Campanella | 8292ba6 | 2018-01-31 16:43:23 +0100 | [diff] [blame] | 1078 | |
| 1079 | //Update the packet with the deferred instructions |
| 1080 | Builder builder = updatePacket(packet, deferredInstructions); |
| 1081 | |
| 1082 | //Gather any output instructions from the deferred instruction |
| 1083 | List<Instruction> outputFlowInstruction = deferredInstructions.stream().filter(instruction -> { |
| 1084 | return instruction.type().equals(Instruction.Type.OUTPUT); |
| 1085 | }).collect(Collectors.toList()); |
| 1086 | |
| 1087 | //We are considering deferred instructions from flows, there can only be one output. |
| 1088 | if (outputFlowInstruction.size() > 1) { |
| 1089 | trace.addResultMessage("More than one flow rule with OUTPUT instruction"); |
| 1090 | log.warn("There cannot be more than one flow entry with OUTPUT instruction for {}", packet); |
| 1091 | } |
| 1092 | //If there is one output let's go through that |
| 1093 | if (outputFlowInstruction.size() == 1) { |
| 1094 | buildOutputFromDevice(trace, in, builder, outputPorts, (OutputInstruction) outputFlowInstruction.get(0), |
| 1095 | ImmutableList.of()); |
| 1096 | } |
| 1097 | //If there is no output let's see if there any deferred instruction point to groups. |
| 1098 | if (outputFlowInstruction.size() == 0) { |
| 1099 | getGroupsFromInstructions(trace, groups, deferredInstructions, |
Andrea Campanella | d5e16ff | 2018-03-21 10:08:55 -0700 | [diff] [blame] | 1100 | in.deviceId(), builder, outputPorts, in, completePath); |
Andrea Campanella | 8292ba6 | 2018-01-31 16:43:23 +0100 | [diff] [blame] | 1101 | } |
| 1102 | return builder; |
| 1103 | } |
| 1104 | |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 1105 | /** |
| 1106 | * Gets group information from instructions. |
| 1107 | * |
| 1108 | * @param trace the trace we are building |
| 1109 | * @param groupsForDevice the set of groups for this device |
| 1110 | * @param instructions the set of instructions we are searching for groups. |
| 1111 | * @param deviceId the device we are considering |
| 1112 | * @param builder the builder of the input packet |
| 1113 | * @param outputPorts the output ports for that packet |
| 1114 | */ |
| 1115 | private void getGroupsFromInstructions(StaticPacketTrace trace, List<Group> groupsForDevice, |
| 1116 | List<Instruction> instructions, DeviceId deviceId, |
Andrea Campanella | 8292ba6 | 2018-01-31 16:43:23 +0100 | [diff] [blame] | 1117 | Builder builder, List<PortNumber> outputPorts, |
Andrea Campanella | d5e16ff | 2018-03-21 10:08:55 -0700 | [diff] [blame] | 1118 | ConnectPoint in, List<ConnectPoint> completePath) { |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 1119 | List<Instruction> groupInstructionlist = new ArrayList<>(); |
Seyeon Jeong | a974551 | 2020-04-07 12:06:18 -0700 | [diff] [blame] | 1120 | // sort instructions according to priority (larger Instruction.Type ENUM constant first) |
| 1121 | // which enables to treat other actions before the OUTPUT action |
| 1122 | //TODO improve the priority scheme according to the OpenFlow ActionSet spec |
| 1123 | List<Instruction> instructionsSorted = new ArrayList<>(); |
| 1124 | instructionsSorted.addAll(instructions); |
| 1125 | instructionsSorted.sort((instr1, instr2) -> { |
| 1126 | return Integer.compare(instr2.type().ordinal(), instr1.type().ordinal()); |
| 1127 | }); |
| 1128 | |
| 1129 | for (Instruction instruction : instructionsSorted) { |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 1130 | log.debug("Considering Instruction {}", instruction); |
| 1131 | //if the instruction is not group we need to update the packet or add the output |
| 1132 | //to the possible outputs for this packet |
| 1133 | if (!instruction.type().equals(Instruction.Type.GROUP)) { |
| 1134 | //if the instruction is not group we need to update the packet or add the output |
| 1135 | //to the possible outputs for this packet |
| 1136 | if (instruction.type().equals(Instruction.Type.OUTPUT)) { |
Andrea Campanella | 7d3cf65 | 2018-01-22 15:10:30 +0100 | [diff] [blame] | 1137 | buildOutputFromDevice(trace, in, builder, outputPorts, |
Andrea Campanella | 573d4b9 | 2018-02-19 17:03:46 +0100 | [diff] [blame] | 1138 | (OutputInstruction) instruction, ImmutableList.copyOf(groupsForDevice)); |
| 1139 | //clearing the groups because we start from the top. |
| 1140 | groupsForDevice.clear(); |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 1141 | } else { |
| 1142 | builder = translateInstruction(builder, instruction); |
| 1143 | } |
| 1144 | } else { |
| 1145 | //if the instuction is pointing to a group we need to get the group |
| 1146 | groupInstructionlist.add(instruction); |
| 1147 | } |
| 1148 | } |
| 1149 | //handle all the internal instructions pointing to a group. |
| 1150 | for (Instruction instr : groupInstructionlist) { |
| 1151 | GroupInstruction groupInstruction = (GroupInstruction) instr; |
Seyeon Jeong | 83e7986 | 2020-02-28 01:17:34 -0800 | [diff] [blame^] | 1152 | Group group = Lists.newArrayList(groupNib.getGroups(deviceId)).stream().filter(groupInternal -> { |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 1153 | return groupInternal.id().equals(groupInstruction.groupId()); |
| 1154 | }).findAny().orElse(null); |
| 1155 | if (group == null) { |
| 1156 | trace.addResultMessage("Null group for Instruction " + instr); |
Andrea Campanella | 5af2d2b | 2018-03-12 19:25:44 -0700 | [diff] [blame] | 1157 | trace.setSuccess(false); |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 1158 | break; |
| 1159 | } |
Andrea Campanella | 94dfb9e | 2018-02-27 12:36:00 +0100 | [diff] [blame] | 1160 | if (group.buckets().buckets().size() == 0) { |
Andrea Campanella | d5e16ff | 2018-03-21 10:08:55 -0700 | [diff] [blame] | 1161 | trace.addResultMessage("Group " + group.id() + " has no buckets"); |
Andrea Campanella | 5af2d2b | 2018-03-12 19:25:44 -0700 | [diff] [blame] | 1162 | trace.setSuccess(false); |
Andrea Campanella | d5e16ff | 2018-03-21 10:08:55 -0700 | [diff] [blame] | 1163 | computePath(completePath, trace, null); |
Andrea Campanella | 94dfb9e | 2018-02-27 12:36:00 +0100 | [diff] [blame] | 1164 | break; |
| 1165 | } |
Andrea Campanella | 573d4b9 | 2018-02-19 17:03:46 +0100 | [diff] [blame] | 1166 | |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 1167 | //Cycle in each of the group's buckets and add them to the groups for this Device. |
| 1168 | for (GroupBucket bucket : group.buckets().buckets()) { |
Andrea Campanella | 573d4b9 | 2018-02-19 17:03:46 +0100 | [diff] [blame] | 1169 | |
| 1170 | //add the group to the traversed groups |
| 1171 | if (!groupsForDevice.contains(group)) { |
| 1172 | groupsForDevice.add(group); |
| 1173 | } |
| 1174 | |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 1175 | getGroupsFromInstructions(trace, groupsForDevice, bucket.treatment().allInstructions(), |
Andrea Campanella | d5e16ff | 2018-03-21 10:08:55 -0700 | [diff] [blame] | 1176 | deviceId, builder, outputPorts, in, completePath); |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 1177 | } |
| 1178 | } |
| 1179 | } |
| 1180 | |
| 1181 | /** |
Andrea Campanella | 7d3cf65 | 2018-01-22 15:10:30 +0100 | [diff] [blame] | 1182 | * Check if the output is the input port, if so adds a dop result message, otherwise builds |
| 1183 | * a possible output from this device. |
| 1184 | * |
| 1185 | * @param trace the trace |
| 1186 | * @param in the input connect point |
| 1187 | * @param builder the packet builder |
| 1188 | * @param outputPorts the list of output ports for this device |
| 1189 | * @param outputInstruction the output instruction |
Andrea Campanella | 8292ba6 | 2018-01-31 16:43:23 +0100 | [diff] [blame] | 1190 | * @param groupsForDevice the groups we output from |
Andrea Campanella | 7d3cf65 | 2018-01-22 15:10:30 +0100 | [diff] [blame] | 1191 | */ |
Andrea Campanella | 8292ba6 | 2018-01-31 16:43:23 +0100 | [diff] [blame] | 1192 | private void buildOutputFromDevice(StaticPacketTrace trace, ConnectPoint in, Builder builder, |
Andrea Campanella | 7d3cf65 | 2018-01-22 15:10:30 +0100 | [diff] [blame] | 1193 | List<PortNumber> outputPorts, OutputInstruction outputInstruction, |
| 1194 | List<Group> groupsForDevice) { |
Andrea Campanella | 54923d6 | 2018-01-23 12:46:04 +0100 | [diff] [blame] | 1195 | ConnectPoint output = new ConnectPoint(in.deviceId(), outputInstruction.port()); |
Andrea Campanella | 1445f7a | 2018-02-07 12:00:12 +0100 | [diff] [blame] | 1196 | |
Andrea Campanella | 2ff88a9 | 2018-03-06 15:21:09 -0800 | [diff] [blame] | 1197 | outputPorts.add(outputInstruction.port()); |
| 1198 | |
| 1199 | GroupsInDevice device = new GroupsInDevice(output, groupsForDevice, builder.build()); |
| 1200 | if (trace.getGroupOuputs(output.deviceId()) != null |
| 1201 | && trace.getGroupOuputs(output.deviceId()).contains(device)) { |
| 1202 | return; |
Andrea Campanella | 7d3cf65 | 2018-01-22 15:10:30 +0100 | [diff] [blame] | 1203 | } |
Andrea Campanella | 2ff88a9 | 2018-03-06 15:21:09 -0800 | [diff] [blame] | 1204 | trace.addGroupOutputPath(in.deviceId(), |
| 1205 | new GroupsInDevice(output, groupsForDevice, builder.build())); |
Andrea Campanella | 7d3cf65 | 2018-01-22 15:10:30 +0100 | [diff] [blame] | 1206 | } |
| 1207 | |
| 1208 | /** |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 1209 | * Applies all give instructions to the input packet. |
| 1210 | * |
| 1211 | * @param packet the input packet |
| 1212 | * @param instructions the set of instructions |
| 1213 | * @return the packet with the applied instructions |
| 1214 | */ |
Andrea Campanella | 8292ba6 | 2018-01-31 16:43:23 +0100 | [diff] [blame] | 1215 | private Builder updatePacket(TrafficSelector packet, List<Instruction> instructions) { |
| 1216 | Builder newSelector = DefaultTrafficSelector.builder(); |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 1217 | packet.criteria().forEach(newSelector::add); |
Andrea Campanella | 3970e47 | 2018-01-25 16:44:04 +0100 | [diff] [blame] | 1218 | //FIXME optimize |
| 1219 | for (Instruction instruction : instructions) { |
| 1220 | newSelector = translateInstruction(newSelector, instruction); |
| 1221 | } |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 1222 | return newSelector; |
| 1223 | } |
| 1224 | |
| 1225 | /** |
| 1226 | * Applies an instruction to the packet in the form of a selector. |
| 1227 | * |
| 1228 | * @param newSelector the packet selector |
| 1229 | * @param instruction the instruction to be translated |
| 1230 | * @return the new selector with the applied instruction |
| 1231 | */ |
Andrea Campanella | 8292ba6 | 2018-01-31 16:43:23 +0100 | [diff] [blame] | 1232 | private Builder translateInstruction(Builder newSelector, Instruction instruction) { |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 1233 | log.debug("Translating instruction {}", instruction); |
Andrea Campanella | 3970e47 | 2018-01-25 16:44:04 +0100 | [diff] [blame] | 1234 | log.debug("New Selector {}", newSelector.build()); |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 1235 | //TODO add as required |
| 1236 | Criterion criterion = null; |
| 1237 | switch (instruction.type()) { |
| 1238 | case L2MODIFICATION: |
| 1239 | L2ModificationInstruction l2Instruction = (L2ModificationInstruction) instruction; |
| 1240 | switch (l2Instruction.subtype()) { |
| 1241 | case VLAN_ID: |
Andrea Campanella | 94c594a | 2018-02-06 18:58:40 +0100 | [diff] [blame] | 1242 | ModVlanIdInstruction vlanIdInstruction = |
| 1243 | (ModVlanIdInstruction) instruction; |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 1244 | VlanId id = vlanIdInstruction.vlanId(); |
| 1245 | criterion = Criteria.matchVlanId(id); |
| 1246 | break; |
| 1247 | case VLAN_POP: |
| 1248 | criterion = Criteria.matchVlanId(VlanId.NONE); |
| 1249 | break; |
| 1250 | case MPLS_PUSH: |
Andrea Campanella | 94c594a | 2018-02-06 18:58:40 +0100 | [diff] [blame] | 1251 | ModMplsHeaderInstruction mplsEthInstruction = |
| 1252 | (ModMplsHeaderInstruction) instruction; |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 1253 | criterion = Criteria.matchEthType(mplsEthInstruction.ethernetType().toShort()); |
| 1254 | break; |
| 1255 | case MPLS_POP: |
Andrea Campanella | 94c594a | 2018-02-06 18:58:40 +0100 | [diff] [blame] | 1256 | ModMplsHeaderInstruction mplsPopInstruction = |
| 1257 | (ModMplsHeaderInstruction) instruction; |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 1258 | criterion = Criteria.matchEthType(mplsPopInstruction.ethernetType().toShort()); |
Andrea Campanella | 3970e47 | 2018-01-25 16:44:04 +0100 | [diff] [blame] | 1259 | |
| 1260 | //When popping MPLS we remove label and BOS |
| 1261 | TrafficSelector temporaryPacket = newSelector.build(); |
| 1262 | if (temporaryPacket.getCriterion(Criterion.Type.MPLS_LABEL) != null) { |
Andrea Campanella | 8292ba6 | 2018-01-31 16:43:23 +0100 | [diff] [blame] | 1263 | Builder noMplsSelector = DefaultTrafficSelector.builder(); |
Andrea Campanella | 3970e47 | 2018-01-25 16:44:04 +0100 | [diff] [blame] | 1264 | temporaryPacket.criteria().stream().filter(c -> { |
| 1265 | return !c.type().equals(Criterion.Type.MPLS_LABEL) && |
| 1266 | !c.type().equals(Criterion.Type.MPLS_BOS); |
| 1267 | }).forEach(noMplsSelector::add); |
| 1268 | newSelector = noMplsSelector; |
| 1269 | } |
| 1270 | |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 1271 | break; |
| 1272 | case MPLS_LABEL: |
Andrea Campanella | 94c594a | 2018-02-06 18:58:40 +0100 | [diff] [blame] | 1273 | ModMplsLabelInstruction mplsLabelInstruction = |
| 1274 | (ModMplsLabelInstruction) instruction; |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 1275 | criterion = Criteria.matchMplsLabel(mplsLabelInstruction.label()); |
Andrea Campanella | 3970e47 | 2018-01-25 16:44:04 +0100 | [diff] [blame] | 1276 | newSelector.matchMplsBos(true); |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 1277 | break; |
| 1278 | case ETH_DST: |
Andrea Campanella | 94c594a | 2018-02-06 18:58:40 +0100 | [diff] [blame] | 1279 | ModEtherInstruction modEtherDstInstruction = |
| 1280 | (ModEtherInstruction) instruction; |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 1281 | criterion = Criteria.matchEthDst(modEtherDstInstruction.mac()); |
| 1282 | break; |
| 1283 | case ETH_SRC: |
Andrea Campanella | 94c594a | 2018-02-06 18:58:40 +0100 | [diff] [blame] | 1284 | ModEtherInstruction modEtherSrcInstruction = |
| 1285 | (ModEtherInstruction) instruction; |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 1286 | criterion = Criteria.matchEthSrc(modEtherSrcInstruction.mac()); |
| 1287 | break; |
| 1288 | default: |
| 1289 | log.debug("Unsupported L2 Instruction"); |
| 1290 | break; |
| 1291 | } |
| 1292 | break; |
| 1293 | default: |
| 1294 | log.debug("Unsupported Instruction"); |
| 1295 | break; |
| 1296 | } |
| 1297 | if (criterion != null) { |
| 1298 | log.debug("Adding criterion {}", criterion); |
| 1299 | newSelector.add(criterion); |
| 1300 | } |
| 1301 | return newSelector; |
| 1302 | } |
| 1303 | |
| 1304 | /** |
| 1305 | * Finds the rule in the device that mathces the input packet and has the highest priority. |
| 1306 | * |
| 1307 | * @param packet the input packet |
| 1308 | * @param in the connect point the packet comes in from |
| 1309 | * @param tableId the table to search |
| 1310 | * @return the flow entry |
| 1311 | */ |
| 1312 | private FlowEntry matchHighestPriority(TrafficSelector packet, ConnectPoint in, TableId tableId) { |
| 1313 | //Computing the possible match rules. |
| 1314 | final Comparator<FlowEntry> comparator = Comparator.comparing(FlowRule::priority); |
Seyeon Jeong | 83e7986 | 2020-02-28 01:17:34 -0800 | [diff] [blame^] | 1315 | return Lists.newArrayList(flowNib.getFlowEntriesByState(in.deviceId(), FlowEntry.FlowEntryState.ADDED) |
Andrea Campanella | 39ec33e | 2018-04-13 12:14:48 +0200 | [diff] [blame] | 1316 | .iterator()).stream() |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 1317 | .filter(flowEntry -> { |
| 1318 | return flowEntry.table().equals(tableId); |
| 1319 | }) |
| 1320 | .filter(flowEntry -> { |
| 1321 | return match(packet, flowEntry); |
| 1322 | }).max(comparator).orElse(null); |
| 1323 | } |
| 1324 | |
| 1325 | /** |
| 1326 | * Matches the packet with the given flow entry. |
| 1327 | * |
| 1328 | * @param packet the packet to match |
| 1329 | * @param flowEntry the flow entry to match the packet against |
| 1330 | * @return true if the packet matches the flow. |
| 1331 | */ |
| 1332 | private boolean match(TrafficSelector packet, FlowEntry flowEntry) { |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 1333 | return flowEntry.selector().criteria().stream().allMatch(criterion -> { |
| 1334 | Criterion.Type type = criterion.type(); |
Andrea Campanella | 128d9c6 | 2018-01-31 12:20:48 +0100 | [diff] [blame] | 1335 | //If the criterion has IP we need to do LPM to establish matching. |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 1336 | if (type.equals(Criterion.Type.IPV4_SRC) || type.equals(Criterion.Type.IPV4_DST) || |
| 1337 | type.equals(Criterion.Type.IPV6_SRC) || type.equals(Criterion.Type.IPV6_DST)) { |
Andrea Campanella | 4c6170a | 2018-01-17 16:34:51 +0100 | [diff] [blame] | 1338 | return matchIp(packet, (IPCriterion) criterion); |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 1339 | //we check that the packet contains the criterion provided by the flow rule. |
Andrea Campanella | 4c6170a | 2018-01-17 16:34:51 +0100 | [diff] [blame] | 1340 | } else if (type.equals(Criterion.Type.ETH_SRC_MASKED)) { |
| 1341 | return matchMac(packet, (EthCriterion) criterion, false); |
| 1342 | } else if (type.equals(Criterion.Type.ETH_DST_MASKED)) { |
| 1343 | return matchMac(packet, (EthCriterion) criterion, true); |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 1344 | } else { |
| 1345 | return packet.criteria().contains(criterion); |
| 1346 | } |
| 1347 | }); |
Simon Hunt | 026a287 | 2017-11-13 17:09:43 -0800 | [diff] [blame] | 1348 | } |
Andrea Campanella | 4c6170a | 2018-01-17 16:34:51 +0100 | [diff] [blame] | 1349 | |
| 1350 | /** |
| 1351 | * Checks if the packet has an dst or src IP and if that IP matches the subnet of the ip criterion. |
| 1352 | * |
| 1353 | * @param packet the incoming packet |
| 1354 | * @param criterion the criterion to match |
| 1355 | * @return true if match |
| 1356 | */ |
| 1357 | private boolean matchIp(TrafficSelector packet, IPCriterion criterion) { |
| 1358 | IPCriterion matchCriterion = (IPCriterion) packet.getCriterion(criterion.type()); |
| 1359 | //if the packet does not have an IPv4 or IPv6 criterion we return true |
| 1360 | if (matchCriterion == null) { |
| 1361 | return false; |
| 1362 | } |
| 1363 | try { |
| 1364 | log.debug("Checking if {} is under {}", matchCriterion.ip(), criterion.ip()); |
| 1365 | Subnet subnet = Subnet.createInstance(criterion.ip().toString()); |
| 1366 | return subnet.isInSubnet(matchCriterion.ip().address().toInetAddress()); |
| 1367 | } catch (UnknownHostException e) { |
| 1368 | return false; |
| 1369 | } |
| 1370 | } |
| 1371 | |
| 1372 | /** |
| 1373 | * Checks if the packet has a dst or src MAC and if that Mac matches the mask of the mac criterion. |
| 1374 | * |
| 1375 | * @param packet the incoming packet |
| 1376 | * @param hitCriterion the criterion to match |
| 1377 | * @param dst true if we are checking DST MAC |
| 1378 | * @return true if match |
| 1379 | */ |
| 1380 | private boolean matchMac(TrafficSelector packet, EthCriterion hitCriterion, boolean dst) { |
| 1381 | //Packet can have only one EthCriterion |
| 1382 | EthCriterion matchCriterion; |
| 1383 | if (dst) { |
| 1384 | matchCriterion = (EthCriterion) packet.criteria().stream().filter(criterion1 -> { |
| 1385 | return criterion1.type().equals(Criterion.Type.ETH_DST_MASKED) || |
| 1386 | criterion1.type().equals(Criterion.Type.ETH_DST); |
| 1387 | }).findFirst().orElse(null); |
| 1388 | } else { |
| 1389 | matchCriterion = (EthCriterion) packet.criteria().stream().filter(criterion1 -> { |
| 1390 | return criterion1.type().equals(Criterion.Type.ETH_SRC_MASKED) || |
| 1391 | criterion1.type().equals(Criterion.Type.ETH_SRC); |
| 1392 | }).findFirst().orElse(null); |
| 1393 | } |
| 1394 | //if the packet does not have an ETH criterion we return true |
| 1395 | if (matchCriterion == null) { |
| 1396 | return true; |
| 1397 | } |
| 1398 | log.debug("Checking if {} is under {}/{}", matchCriterion.mac(), hitCriterion.mac(), hitCriterion.mask()); |
| 1399 | return compareMac(matchCriterion.mac(), hitCriterion.mac(), hitCriterion.mask()); |
| 1400 | } |
Simon Hunt | 026a287 | 2017-11-13 17:09:43 -0800 | [diff] [blame] | 1401 | } |