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