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