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