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