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