blob: 5b245b234375096e6265ad9b722d0f8fcb49d84c [file] [log] [blame]
Simon Hunt6fefd852017-11-13 17:09:43 -08001/*
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
17package org.onosproject.t3.impl;
18
Andrea Campanella37d10622018-01-18 17:11:42 +010019import com.google.common.base.Preconditions;
Andrea Campanella01e886e2017-12-15 15:27:31 +010020import com.google.common.collect.ImmutableList;
Andrea Campanella79cb17d2018-02-27 18:03:17 +010021import com.google.common.collect.ImmutableSet;
Andrea Campanella01e886e2017-12-15 15:27:31 +010022import com.google.common.collect.Lists;
Andrea Campanella7e2200e2018-02-27 14:50:45 +010023import com.google.common.collect.Sets;
Andrea Campanella6be5c872018-02-21 14:28:20 +010024import org.apache.commons.lang3.tuple.Pair;
Simon Hunt6fefd852017-11-13 17:09:43 -080025import org.apache.felix.scr.annotations.Component;
26import org.apache.felix.scr.annotations.Reference;
27import org.apache.felix.scr.annotations.ReferenceCardinality;
28import org.apache.felix.scr.annotations.Service;
Andrea Campanellaaf34b7c2018-02-08 17:10:11 +010029import org.onlab.packet.IpAddress;
Andrea Campanella01e886e2017-12-15 15:27:31 +010030import org.onlab.packet.VlanId;
Andrea Campanella7c8e7912018-01-23 12:46:04 +010031import org.onosproject.cluster.NodeId;
32import org.onosproject.mastership.MastershipService;
Andrea Campanellafa3ec192018-04-06 16:30:18 +020033import org.onosproject.mcast.api.MulticastRouteService;
Simon Hunt6fefd852017-11-13 17:09:43 -080034import org.onosproject.net.ConnectPoint;
Andrea Campanella01e886e2017-12-15 15:27:31 +010035import org.onosproject.net.DeviceId;
36import org.onosproject.net.Host;
Andrea Campanellaaf34b7c2018-02-08 17:10:11 +010037import org.onosproject.net.HostId;
Andrea Campanella01e886e2017-12-15 15:27:31 +010038import org.onosproject.net.Link;
Andrea Campanella6f2d6742018-02-07 12:00:12 +010039import org.onosproject.net.Port;
Andrea Campanella01e886e2017-12-15 15:27:31 +010040import org.onosproject.net.PortNumber;
Andrea Campanellaaf34b7c2018-02-08 17:10:11 +010041import org.onosproject.net.config.ConfigException;
42import org.onosproject.net.config.NetworkConfigService;
43import org.onosproject.net.config.basics.InterfaceConfig;
Andrea Campanella37d10622018-01-18 17:11:42 +010044import org.onosproject.net.device.DeviceService;
Andrea Campanella01e886e2017-12-15 15:27:31 +010045import org.onosproject.net.driver.DriverService;
Andrea Campanella04924b92018-01-17 16:34:51 +010046import org.onosproject.net.edge.EdgePortService;
Andrea Campanella01e886e2017-12-15 15:27:31 +010047import org.onosproject.net.flow.DefaultTrafficSelector;
48import org.onosproject.net.flow.FlowEntry;
49import org.onosproject.net.flow.FlowRule;
Simon Hunt6fefd852017-11-13 17:09:43 -080050import org.onosproject.net.flow.FlowRuleService;
Andrea Campanella01e886e2017-12-15 15:27:31 +010051import org.onosproject.net.flow.IndexTableId;
52import org.onosproject.net.flow.TableId;
Simon Hunt6fefd852017-11-13 17:09:43 -080053import org.onosproject.net.flow.TrafficSelector;
Andrea Campanella01e886e2017-12-15 15:27:31 +010054import org.onosproject.net.flow.criteria.Criteria;
55import org.onosproject.net.flow.criteria.Criterion;
56import org.onosproject.net.flow.criteria.EthCriterion;
57import org.onosproject.net.flow.criteria.EthTypeCriterion;
58import org.onosproject.net.flow.criteria.IPCriterion;
Andrea Campanella58b3b522018-02-06 15:46:52 +010059import org.onosproject.net.flow.criteria.VlanIdCriterion;
Andrea Campanella01e886e2017-12-15 15:27:31 +010060import org.onosproject.net.flow.instructions.Instruction;
61import org.onosproject.net.flow.instructions.Instructions;
62import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
63import org.onosproject.net.flow.instructions.L2ModificationInstruction;
64import org.onosproject.net.group.Group;
65import org.onosproject.net.group.GroupBucket;
Simon Hunt6fefd852017-11-13 17:09:43 -080066import org.onosproject.net.group.GroupService;
Andrea Campanella01e886e2017-12-15 15:27:31 +010067import org.onosproject.net.host.HostService;
Andrea Campanellaaf34b7c2018-02-08 17:10:11 +010068import org.onosproject.net.host.InterfaceIpAddress;
69import org.onosproject.net.intf.Interface;
Andrea Campanella01e886e2017-12-15 15:27:31 +010070import org.onosproject.net.link.LinkService;
Andrea Campanellacc2424a2018-03-07 14:27:54 -080071import org.onosproject.routeservice.ResolvedRoute;
72import org.onosproject.routeservice.RouteService;
Andrea Campanellaaf34b7c2018-02-08 17:10:11 +010073import org.onosproject.segmentrouting.config.SegmentRoutingDeviceConfig;
Andrea Campanella01e886e2017-12-15 15:27:31 +010074import org.onosproject.t3.api.GroupsInDevice;
Simon Hunt6fefd852017-11-13 17:09:43 -080075import org.onosproject.t3.api.StaticPacketTrace;
76import org.onosproject.t3.api.TroubleshootService;
77import org.slf4j.Logger;
78
Andrea Campanella01e886e2017-12-15 15:27:31 +010079import java.net.UnknownHostException;
80import java.util.ArrayList;
Andrea Campanella7c8e7912018-01-23 12:46:04 +010081import java.util.Collection;
Andrea Campanella01e886e2017-12-15 15:27:31 +010082import java.util.Collections;
83import java.util.Comparator;
84import java.util.HashSet;
85import java.util.List;
Andrea Campanellacc2424a2018-03-07 14:27:54 -080086import java.util.Optional;
Andrea Campanella01e886e2017-12-15 15:27:31 +010087import java.util.Set;
88import java.util.stream.Collectors;
89
90import static org.onlab.packet.EthType.EtherType;
Andrea Campanella58b3b522018-02-06 15:46:52 +010091import static org.onosproject.net.flow.TrafficSelector.Builder;
Andrea Campanella01e886e2017-12-15 15:27:31 +010092import static org.onosproject.net.flow.instructions.Instructions.GroupInstruction;
Andrea Campanella97f9d4c2018-02-06 18:58:40 +010093import static org.onosproject.net.flow.instructions.L2ModificationInstruction.ModEtherInstruction;
94import static org.onosproject.net.flow.instructions.L2ModificationInstruction.ModMplsHeaderInstruction;
95import static org.onosproject.net.flow.instructions.L2ModificationInstruction.ModMplsLabelInstruction;
96import static org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanIdInstruction;
Andrea Campanella04924b92018-01-17 16:34:51 +010097import static org.onosproject.t3.impl.TroubleshootUtils.compareMac;
Simon Hunt6fefd852017-11-13 17:09:43 -080098import static org.slf4j.LoggerFactory.getLogger;
99
100/**
Andrea Campanella01e886e2017-12-15 15:27:31 +0100101 * 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 Hunt6fefd852017-11-13 17:09:43 -0800104 */
105@Service
106@Component(immediate = true)
107public class TroubleshootManager implements TroubleshootService {
108
109 private static final Logger log = getLogger(TroubleshootManager.class);
110
Andrea Campanella6f2d6742018-02-07 12:00:12 +0100111 static final String PACKET_TO_CONTROLLER = "Packet goes to the controller";
112
Simon Hunt6fefd852017-11-13 17:09:43 -0800113 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
114 protected FlowRuleService flowRuleService;
115
116 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
117 protected GroupService groupService;
118
Andrea Campanella01e886e2017-12-15 15:27:31 +0100119 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
120 protected LinkService linkService;
Simon Hunt6fefd852017-11-13 17:09:43 -0800121
Andrea Campanella01e886e2017-12-15 15:27:31 +0100122 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
123 protected HostService hostService;
124
125 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
126 protected DriverService driverService;
Simon Hunt6fefd852017-11-13 17:09:43 -0800127
Andrea Campanella37d10622018-01-18 17:11:42 +0100128 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
129 protected DeviceService deviceService;
130
Andrea Campanella7c8e7912018-01-23 12:46:04 +0100131 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
132 protected MastershipService mastershipService;
133
Andrea Campanellaaf34b7c2018-02-08 17:10:11 +0100134 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
135 protected NetworkConfigService networkConfigService;
136
Andrea Campanella04924b92018-01-17 16:34:51 +0100137 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
138 protected EdgePortService edgePortService;
139
Andrea Campanellacc2424a2018-03-07 14:27:54 -0800140 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
141 protected RouteService routeService;
142
Andrea Campanellafa3ec192018-04-06 16:30:18 +0200143 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
144 protected MulticastRouteService mcastService;
145
Andrea Campanellaaf34b7c2018-02-08 17:10:11 +0100146 @Override
Andrea Campanella6be5c872018-02-21 14:28:20 +0100147 public List<StaticPacketTrace> pingAll(EtherType type) {
148 ImmutableList.Builder<StaticPacketTrace> tracesBuilder = ImmutableList.builder();
149 hostService.getHosts().forEach(host -> {
150 List<IpAddress> ipAddresses = getIpAddresses(host, type, false);
151 if (ipAddresses.size() > 0) {
Andrea Campanella7e2200e2018-02-27 14:50:45 +0100152 //check if the host has only local IPs of that ETH type
153 boolean onlyLocalSrc = ipAddresses.size() == 1 && ipAddresses.get(0).isLinkLocal();
Andrea Campanella6be5c872018-02-21 14:28:20 +0100154 hostService.getHosts().forEach(hostToPing -> {
155 List<IpAddress> ipAddressesToPing = getIpAddresses(hostToPing, type, false);
Andrea Campanella7e2200e2018-02-27 14:50:45 +0100156 //check if the other host has only local IPs of that ETH type
157 boolean onlyLocalDst = ipAddressesToPing.size() == 1 && ipAddressesToPing.get(0).isLinkLocal();
158 boolean sameLocation = Sets.intersection(host.locations(), hostToPing.locations()).size() > 0;
159 //Trace is done only if they are both local and under the same location
160 // or not local and if they are not the same host.
161 if (((sameLocation && onlyLocalDst && onlyLocalSrc) ||
162 (!onlyLocalSrc && !onlyLocalDst && ipAddressesToPing.size() > 0))
163 && !host.equals(hostToPing)) {
Andrea Campanella79cb17d2018-02-27 18:03:17 +0100164 tracesBuilder.addAll(trace(host.id(), hostToPing.id(), type));
Andrea Campanella6be5c872018-02-21 14:28:20 +0100165 }
166 });
167 }
168 });
169 return tracesBuilder.build();
170 }
171
172 @Override
Andrea Campanella41de8062018-02-28 16:43:16 +0100173 public Generator<Set<StaticPacketTrace>> pingAllGenerator(EtherType type) {
174 return new PingAllGenerator(type, hostService, this);
175 }
176
177 @Override
Andrea Campanellafa3ec192018-04-06 16:30:18 +0200178 public Generator<Set<StaticPacketTrace>> traceMcast(VlanId vlanId) {
179 return new McastGenerator(mcastService, this, vlanId);
180 }
181
182 @Override
Andrea Campanella79cb17d2018-02-27 18:03:17 +0100183 public Set<StaticPacketTrace> trace(HostId sourceHost, HostId destinationHost, EtherType etherType) {
Andrea Campanellaaf34b7c2018-02-08 17:10:11 +0100184 Host source = hostService.getHost(sourceHost);
185 Host destination = hostService.getHost(destinationHost);
186
Andrea Campanella6be5c872018-02-21 14:28:20 +0100187 //Temporary trace to fail in case we don't have enough information or what is provided is incoherent
188 StaticPacketTrace failTrace = new StaticPacketTrace(null, null, Pair.of(source, destination));
Andrea Campanellaaf34b7c2018-02-08 17:10:11 +0100189
190 if (source == null) {
191 failTrace.addResultMessage("Source Host " + sourceHost + " does not exist");
Andrea Campanellac8e6a502018-03-12 19:25:44 -0700192 failTrace.setSuccess(false);
193
Andrea Campanella79cb17d2018-02-27 18:03:17 +0100194 return ImmutableSet.of(failTrace);
Andrea Campanellaaf34b7c2018-02-08 17:10:11 +0100195 }
196
197 if (destination == null) {
198 failTrace.addResultMessage("Destination Host " + destinationHost + " does not exist");
Andrea Campanellac8e6a502018-03-12 19:25:44 -0700199 failTrace.setSuccess(false);
Andrea Campanella79cb17d2018-02-27 18:03:17 +0100200 return ImmutableSet.of(failTrace);
Andrea Campanellaaf34b7c2018-02-08 17:10:11 +0100201 }
202
203 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder()
Andrea Campanellaaf34b7c2018-02-08 17:10:11 +0100204 .matchEthType(etherType.ethType().toShort())
205 .matchEthDst(source.mac())
206 .matchVlanId(source.vlan());
207
208
Andrea Campanellaaf34b7c2018-02-08 17:10:11 +0100209 try {
Andrea Campanella15278a12018-03-26 10:39:22 -0700210 ImmutableSet.Builder<StaticPacketTrace> traces = ImmutableSet.builder();
Andrea Campanellaaf34b7c2018-02-08 17:10:11 +0100211 //if the location deviceId is the same, the two hosts are under same subnet and vlan on the interface
212 // we are under same leaf so it's L2 Unicast.
213 if (areBridged(source, destination)) {
214 selectorBuilder.matchEthDst(destination.mac());
Andrea Campanella79cb17d2018-02-27 18:03:17 +0100215 source.locations().forEach(hostLocation -> {
Andrea Campanella7b84c072018-03-06 15:21:09 -0800216 selectorBuilder.matchInPort(hostLocation.port());
Andrea Campanella79cb17d2018-02-27 18:03:17 +0100217 StaticPacketTrace trace = trace(selectorBuilder.build(), hostLocation);
218 trace.addEndpointHosts(Pair.of(source, destination));
219 traces.add(trace);
220 });
Andrea Campanella15278a12018-03-26 10:39:22 -0700221 //The destination host is not dual homed, if it is the other path might be done through routing.
222 if (destination.locations().size() == 1) {
223 return traces.build();
224 }
Andrea Campanellaaf34b7c2018-02-08 17:10:11 +0100225 }
Andrea Campanellaaf34b7c2018-02-08 17:10:11 +0100226 //handle the IPs for src and dst in case of L3
227 if (etherType.equals(EtherType.IPV4) || etherType.equals(EtherType.IPV6)) {
228
229 //Match on the source IP
230 if (!matchIP(source, failTrace, selectorBuilder, etherType, true)) {
Andrea Campanella79cb17d2018-02-27 18:03:17 +0100231 return ImmutableSet.of(failTrace);
Andrea Campanellaaf34b7c2018-02-08 17:10:11 +0100232 }
233
234 //Match on destination IP
235 if (!matchIP(destination, failTrace, selectorBuilder, etherType, false)) {
Andrea Campanella79cb17d2018-02-27 18:03:17 +0100236 return ImmutableSet.of(failTrace);
Andrea Campanellaaf34b7c2018-02-08 17:10:11 +0100237 }
238
239 } else {
240 failTrace.addResultMessage("Host based trace supports only IPv4 or IPv6 as EtherType, " +
241 "please use packet based");
Andrea Campanellac8e6a502018-03-12 19:25:44 -0700242 failTrace.setSuccess(false);
Andrea Campanella79cb17d2018-02-27 18:03:17 +0100243 return ImmutableSet.of(failTrace);
Andrea Campanellaaf34b7c2018-02-08 17:10:11 +0100244 }
245
246 //l3 unicast, we get the dst mac of the leaf the source is connected to from netcfg
247 SegmentRoutingDeviceConfig segmentRoutingConfig = networkConfigService.getConfig(source.location()
248 .deviceId(), SegmentRoutingDeviceConfig.class);
249 if (segmentRoutingConfig != null) {
250 selectorBuilder.matchEthDst(segmentRoutingConfig.routerMac());
251 } else {
252 failTrace.addResultMessage("Can't get " + source.location().deviceId() +
253 " router MAC from segment routing config can't perform L3 tracing.");
Andrea Campanellac8e6a502018-03-12 19:25:44 -0700254 failTrace.setSuccess(false);
Andrea Campanellaaf34b7c2018-02-08 17:10:11 +0100255 }
Andrea Campanella79cb17d2018-02-27 18:03:17 +0100256 source.locations().forEach(hostLocation -> {
Andrea Campanella7b84c072018-03-06 15:21:09 -0800257 selectorBuilder.matchInPort(hostLocation.port());
Andrea Campanella79cb17d2018-02-27 18:03:17 +0100258 StaticPacketTrace trace = trace(selectorBuilder.build(), hostLocation);
259 trace.addEndpointHosts(Pair.of(source, destination));
260 traces.add(trace);
261 });
262 return traces.build();
Andrea Campanellaaf34b7c2018-02-08 17:10:11 +0100263
264 } catch (ConfigException e) {
265 failTrace.addResultMessage("Can't get config " + e.getMessage());
Andrea Campanella79cb17d2018-02-27 18:03:17 +0100266 return ImmutableSet.of(failTrace);
Andrea Campanellaaf34b7c2018-02-08 17:10:11 +0100267 }
268 }
269
270 /**
271 * Matches src and dst IPs based on host information.
272 *
273 * @param host the host
274 * @param failTrace the trace to use in case of failure
275 * @param selectorBuilder the packet we are building to trace
276 * @param etherType the traffic type
277 * @param src is this src host or dst host
278 * @return true if properly matched
279 */
280 private boolean matchIP(Host host, StaticPacketTrace failTrace, Builder selectorBuilder,
281 EtherType etherType, boolean src) {
Andrea Campanella6be5c872018-02-21 14:28:20 +0100282 List<IpAddress> ips = getIpAddresses(host, etherType, true);
Andrea Campanellaaf34b7c2018-02-08 17:10:11 +0100283
284 if (ips.size() > 0) {
Andrea Campanella465c7be2018-02-21 14:43:21 +0100285 if (etherType.equals(EtherType.IPV4)) {
286 if (src) {
287 selectorBuilder.matchIPSrc(ips.get(0).toIpPrefix());
288 } else {
289 selectorBuilder.matchIPDst(ips.get(0).toIpPrefix());
290 }
291 } else if (etherType.equals(EtherType.IPV6)) {
292 if (src) {
293 selectorBuilder.matchIPv6Src(ips.get(0).toIpPrefix());
294 } else {
295 selectorBuilder.matchIPv6Dst(ips.get(0).toIpPrefix());
296 }
Andrea Campanellaaf34b7c2018-02-08 17:10:11 +0100297 }
298 } else {
299 failTrace.addResultMessage("Host " + host + " has no " + etherType + " address");
Andrea Campanellac8e6a502018-03-12 19:25:44 -0700300 failTrace.setSuccess(false);
Andrea Campanellaaf34b7c2018-02-08 17:10:11 +0100301 return false;
302 }
303 return true;
304 }
305
Andrea Campanella41de8062018-02-28 16:43:16 +0100306 List<IpAddress> getIpAddresses(Host host, EtherType etherType, boolean checklocal) {
Andrea Campanella6be5c872018-02-21 14:28:20 +0100307 return host.ipAddresses().stream().filter(ipAddress -> {
308 boolean correctIp = false;
309 if (etherType.equals(EtherType.IPV4)) {
310 correctIp = ipAddress.isIp4();
311 } else if (etherType.equals(EtherType.IPV6)) {
312 correctIp = ipAddress.isIp6();
313 }
314 if (checklocal) {
315 correctIp = correctIp && !ipAddress.isLinkLocal();
316 }
317 return correctIp;
318 }).collect(Collectors.toList());
319 }
320
Andrea Campanellaaf34b7c2018-02-08 17:10:11 +0100321 /**
322 * Checks that two hosts are bridged (L2Unicast).
323 *
324 * @param source the source host
325 * @param destination the destination host
326 * @return true if bridged.
327 * @throws ConfigException if config can't be properly retrieved
328 */
329 private boolean areBridged(Host source, Host destination) throws ConfigException {
330
Andrea Campanella7b84c072018-03-06 15:21:09 -0800331 //If the locations is not the same we don't even check vlan or subnets
332 if (Collections.disjoint(source.locations(), destination.locations())) {
333 return false;
334 }
335
336 if (!source.vlan().equals(VlanId.NONE) && !destination.vlan().equals(VlanId.NONE)
337 && !source.vlan().equals(destination.vlan())) {
Andrea Campanellaaf34b7c2018-02-08 17:10:11 +0100338 return false;
339 }
340
341 InterfaceConfig interfaceCfgH1 = networkConfigService.getConfig(source.location(), InterfaceConfig.class);
342 InterfaceConfig interfaceCfgH2 = networkConfigService.getConfig(destination.location(), InterfaceConfig.class);
343 if (interfaceCfgH1 != null && interfaceCfgH2 != null) {
344
345 //following can be optimized but for clarity is left as is
346 Interface intfH1 = interfaceCfgH1.getInterfaces().stream().findFirst().get();
347 Interface intfH2 = interfaceCfgH2.getInterfaces().stream().findFirst().get();
348
Andrea Campanella7b84c072018-03-06 15:21:09 -0800349 if (source.vlan().equals(VlanId.NONE) && !destination.vlan().equals(VlanId.NONE)) {
350 return intfH1.vlanUntagged().equals(destination.vlan()) ||
351 intfH1.vlanNative().equals(destination.vlan());
Andrea Campanellaaf34b7c2018-02-08 17:10:11 +0100352 }
353
Andrea Campanella7b84c072018-03-06 15:21:09 -0800354 if (!source.vlan().equals(VlanId.NONE) && destination.vlan().equals(VlanId.NONE)) {
355 return intfH2.vlanUntagged().equals(source.vlan()) ||
356 intfH2.vlanNative().equals(source.vlan());
357 }
358
359 if (!intfH1.vlanNative().equals(intfH2.vlanNative())) {
Andrea Campanellaaf34b7c2018-02-08 17:10:11 +0100360 return false;
361 }
362
363 if (!intfH1.vlanUntagged().equals(intfH2.vlanUntagged())) {
364 return false;
365 }
366
367 List<InterfaceIpAddress> intersection = new ArrayList<>(intfH1.ipAddressesList());
368 intersection.retainAll(intfH2.ipAddressesList());
369 if (intersection.size() == 0) {
370 return false;
371 }
372 }
373 return true;
374 }
375
Simon Hunt6fefd852017-11-13 17:09:43 -0800376 @Override
377 public StaticPacketTrace trace(TrafficSelector packet, ConnectPoint in) {
Andrea Campanella01e886e2017-12-15 15:27:31 +0100378 log.info("Tracing packet {} coming in through {}", packet, in);
Andrea Campanella37d10622018-01-18 17:11:42 +0100379 //device must exist in ONOS
380 Preconditions.checkNotNull(deviceService.getDevice(in.deviceId()),
381 "Device " + in.deviceId() + " must exist in ONOS");
382
Andrea Campanella01e886e2017-12-15 15:27:31 +0100383 StaticPacketTrace trace = new StaticPacketTrace(packet, in);
Andrea Campanella79cb17d2018-02-27 18:03:17 +0100384 boolean isDualHomed = getHosts(trace).stream().anyMatch(host -> host.locations().size() > 1);
Andrea Campanella01e886e2017-12-15 15:27:31 +0100385 //FIXME this can be done recursively
Andrea Campanella01e886e2017-12-15 15:27:31 +0100386 //Building output connect Points
387 List<ConnectPoint> path = new ArrayList<>();
Andrea Campanellad1cc1a32018-03-21 10:08:55 -0700388 trace = traceInDevice(trace, packet, in, isDualHomed, path);
Andrea Campanella79cb17d2018-02-27 18:03:17 +0100389 trace = getTrace(path, in, trace, isDualHomed);
Andrea Campanella01e886e2017-12-15 15:27:31 +0100390 return trace;
391 }
392
393 /**
394 * Computes a trace for a give packet that start in the network at the given connect point.
395 *
396 * @param completePath the path traversed by the packet
397 * @param in the input connect point
398 * @param trace the trace to build
Andrea Campanella79cb17d2018-02-27 18:03:17 +0100399 * @param isDualHomed true if the trace we are doing starts or ends in a dual homed host
Andrea Campanella01e886e2017-12-15 15:27:31 +0100400 * @return the build trace for that packet.
401 */
Andrea Campanella79cb17d2018-02-27 18:03:17 +0100402 private StaticPacketTrace getTrace(List<ConnectPoint> completePath, ConnectPoint in, StaticPacketTrace trace,
403 boolean isDualHomed) {
Andrea Campanella01e886e2017-12-15 15:27:31 +0100404
Andrea Campanellae04aac92018-01-31 14:59:03 +0100405 log.debug("------------------------------------------------------------");
406
Andrea Campanella01e886e2017-12-15 15:27:31 +0100407 //if the trace already contains the input connect point there is a loop
408 if (pathContainsDevice(completePath, in.deviceId())) {
409 trace.addResultMessage("Loop encountered in device " + in.deviceId());
Andrea Campanellaece11772018-03-09 14:52:10 -0800410 completePath.add(in);
411 trace.addCompletePath(completePath);
Andrea Campanellac8e6a502018-03-12 19:25:44 -0700412 trace.setSuccess(false);
Andrea Campanella01e886e2017-12-15 15:27:31 +0100413 return trace;
414 }
415
416 //let's add the input connect point
417 completePath.add(in);
418
419 //If the trace has no outputs for the given input we stop here
420 if (trace.getGroupOuputs(in.deviceId()) == null) {
421 computePath(completePath, trace, null);
422 trace.addResultMessage("No output out of device " + in.deviceId() + ". Packet is dropped");
Andrea Campanellac8e6a502018-03-12 19:25:44 -0700423 trace.setSuccess(false);
Andrea Campanella01e886e2017-12-15 15:27:31 +0100424 return trace;
425 }
Andrea Campanellabb9d3fb2018-01-22 15:10:30 +0100426
Andrea Campanella01e886e2017-12-15 15:27:31 +0100427 //If the trace has ouputs we analyze them all
428 for (GroupsInDevice outputPath : trace.getGroupOuputs(in.deviceId())) {
Andrea Campanella7c8e7912018-01-23 12:46:04 +0100429
430 ConnectPoint cp = outputPath.getOutput();
Andrea Campanellae04aac92018-01-31 14:59:03 +0100431 log.debug("Connect point in {}", in);
Andrea Campanella7c8e7912018-01-23 12:46:04 +0100432 log.debug("Output path {}", cp);
Andrea Campanellacc2424a2018-03-07 14:27:54 -0800433 log.debug("{}", outputPath.getFinalPacket());
Andrea Campanella7c8e7912018-01-23 12:46:04 +0100434
Andrea Campanella01e886e2017-12-15 15:27:31 +0100435 //Hosts for the the given output
Andrea Campanella7c8e7912018-01-23 12:46:04 +0100436 Set<Host> hostsList = hostService.getConnectedHosts(cp);
Andrea Campanella01e886e2017-12-15 15:27:31 +0100437 //Hosts queried from the original ip or mac
438 Set<Host> hosts = getHosts(trace);
439
Andrea Campanella7b84c072018-03-06 15:21:09 -0800440 if (in.equals(cp) && trace.getInitialPacket().getCriterion(Criterion.Type.VLAN_VID) != null &&
441 outputPath.getFinalPacket().getCriterion(Criterion.Type.VLAN_VID) != null
442 && ((VlanIdCriterion) trace.getInitialPacket().getCriterion(Criterion.Type.VLAN_VID)).vlanId()
443 .equals(((VlanIdCriterion) outputPath.getFinalPacket().getCriterion(Criterion.Type.VLAN_VID))
444 .vlanId())) {
Andrea Campanellac8e6a502018-03-12 19:25:44 -0700445 if (trace.getGroupOuputs(in.deviceId()).size() == 1 &&
446 computePath(completePath, trace, outputPath.getOutput())) {
Andrea Campanella6fb95fc2018-03-12 11:07:35 -0700447 trace.addResultMessage("Connect point out " + cp + " is same as initial input " + in);
Andrea Campanellac8e6a502018-03-12 19:25:44 -0700448 trace.setSuccess(false);
Andrea Campanella6fb95fc2018-03-12 11:07:35 -0700449 }
Andrea Campanellad1cc1a32018-03-21 10:08:55 -0700450 } else if (!Collections.disjoint(hostsList, hosts)) {
Andrea Campanellac8e6a502018-03-12 19:25:44 -0700451 //If the two host collections contain the same item it means we reached the proper output
Andrea Campanellae04aac92018-01-31 14:59:03 +0100452 log.debug("Stopping here because host is expected destination {}, reached through", completePath);
Andrea Campanella79cb17d2018-02-27 18:03:17 +0100453 if (computePath(completePath, trace, outputPath.getOutput())) {
454 trace.addResultMessage("Reached required destination Host " + cp);
Andrea Campanella41de8062018-02-28 16:43:16 +0100455 trace.setSuccess(true);
Andrea Campanella79cb17d2018-02-27 18:03:17 +0100456 }
Andrea Campanella01e886e2017-12-15 15:27:31 +0100457 break;
Andrea Campanella7c8e7912018-01-23 12:46:04 +0100458 } else if (cp.port().equals(PortNumber.CONTROLLER)) {
Andrea Campanellad5bb2ef2018-01-31 16:43:23 +0100459
Andrea Campanella7c8e7912018-01-23 12:46:04 +0100460 //Getting the master when the packet gets sent as packet in
461 NodeId master = mastershipService.getMasterFor(cp.deviceId());
Andrea Campanella6f2d6742018-02-07 12:00:12 +0100462 trace.addResultMessage(PACKET_TO_CONTROLLER + " " + master.id());
Andrea Campanella7c8e7912018-01-23 12:46:04 +0100463 computePath(completePath, trace, outputPath.getOutput());
Andrea Campanella58b3b522018-02-06 15:46:52 +0100464 handleVlanToController(outputPath, trace);
Andrea Campanella7c8e7912018-01-23 12:46:04 +0100465
Andrea Campanellad5bb2ef2018-01-31 16:43:23 +0100466 } else if (linkService.getEgressLinks(cp).size() > 0) {
Andrea Campanellae04aac92018-01-31 14:59:03 +0100467
468 //TODO this can be optimized if we use a Tree structure for paths.
469 //if we already have outputs let's check if the one we are considering starts from one of the devices
470 // in any of the ones we have.
471 if (trace.getCompletePaths().size() > 0) {
472 ConnectPoint inputForOutput = null;
473 List<ConnectPoint> previousPath = new ArrayList<>();
474 for (List<ConnectPoint> path : trace.getCompletePaths()) {
475 for (ConnectPoint connect : path) {
476 //if the path already contains the input for the output we've found we use it
477 if (connect.equals(in)) {
478 inputForOutput = connect;
479 previousPath = path;
480 break;
481 }
482 }
483 }
Andrea Campanellaece11772018-03-09 14:52:10 -0800484
Andrea Campanellae04aac92018-01-31 14:59:03 +0100485 //we use the pre-existing path up to the point we fork to a new output
486 if (inputForOutput != null && completePath.contains(inputForOutput)) {
487 List<ConnectPoint> temp = new ArrayList<>(previousPath);
Andrea Campanellaece11772018-03-09 14:52:10 -0800488 temp = temp.subList(0, previousPath.indexOf(inputForOutput) + 1);
489 if (completePath.containsAll(temp)) {
490 completePath = temp;
491 }
Andrea Campanellae04aac92018-01-31 14:59:03 +0100492 }
493 }
494
Andrea Campanella01e886e2017-12-15 15:27:31 +0100495 //let's add the ouput for the input
496 completePath.add(cp);
Andrea Campanella01e886e2017-12-15 15:27:31 +0100497 //let's compute the links for the given output
498 Set<Link> links = linkService.getEgressLinks(cp);
499 log.debug("Egress Links {}", links);
Andrea Campanella01e886e2017-12-15 15:27:31 +0100500 //For each link we trace the corresponding device
501 for (Link link : links) {
502 ConnectPoint dst = link.dst();
503 //change in-port to the dst link in port
Andrea Campanellad5bb2ef2018-01-31 16:43:23 +0100504 Builder updatedPacket = DefaultTrafficSelector.builder();
Andrea Campanella01e886e2017-12-15 15:27:31 +0100505 outputPath.getFinalPacket().criteria().forEach(updatedPacket::add);
506 updatedPacket.add(Criteria.matchInPort(dst.port()));
507 log.debug("DST Connect Point {}", dst);
508 //build the elements for that device
Andrea Campanellad1cc1a32018-03-21 10:08:55 -0700509 traceInDevice(trace, updatedPacket.build(), dst, isDualHomed, completePath);
Andrea Campanella01e886e2017-12-15 15:27:31 +0100510 //continue the trace along the path
Andrea Campanella79cb17d2018-02-27 18:03:17 +0100511 getTrace(completePath, dst, trace, isDualHomed);
Andrea Campanella01e886e2017-12-15 15:27:31 +0100512 }
Andrea Campanella04924b92018-01-17 16:34:51 +0100513 } else if (edgePortService.isEdgePoint(outputPath.getOutput()) &&
514 trace.getInitialPacket().getCriterion(Criterion.Type.ETH_DST) != null &&
515 ((EthCriterion) trace.getInitialPacket().getCriterion(Criterion.Type.ETH_DST))
516 .mac().isMulticast()) {
517 trace.addResultMessage("Packet is multicast and reached output " + outputPath.getOutput() +
518 " which is enabled and is edge port");
Andrea Campanella41de8062018-02-28 16:43:16 +0100519 trace.setSuccess(true);
Andrea Campanella04924b92018-01-17 16:34:51 +0100520 computePath(completePath, trace, outputPath.getOutput());
Andrea Campanella04924b92018-01-17 16:34:51 +0100521 if (!hasOtherOutput(in.deviceId(), trace, outputPath.getOutput())) {
522 return trace;
523 }
Andrea Campanella15278a12018-03-26 10:39:22 -0700524 } else if (deviceService.getPort(cp) != null && deviceService.getPort(cp).isEnabled()) {
Andrea Campanella6f2d6742018-02-07 12:00:12 +0100525 EthTypeCriterion ethTypeCriterion = (EthTypeCriterion) trace.getInitialPacket()
526 .getCriterion(Criterion.Type.ETH_TYPE);
527 //We treat as correct output only if it's not LLDP or BDDP
528 if (!(ethTypeCriterion.ethType().equals(EtherType.LLDP.ethType())
Andrea Campanella62841d42018-02-27 12:42:28 +0100529 && !ethTypeCriterion.ethType().equals(EtherType.BDDP.ethType()))) {
Andrea Campanellacc2424a2018-03-07 14:27:54 -0800530 if (computePath(completePath, trace, outputPath.getOutput())) {
531 if (hostsList.isEmpty()) {
532 trace.addResultMessage("Packet is " + ((EthTypeCriterion) outputPath.getFinalPacket()
533 .getCriterion(Criterion.Type.ETH_TYPE)).ethType() + " and reached " +
534 cp + " with no hosts connected ");
535 } else {
536 IpAddress ipAddress = null;
537 if (trace.getInitialPacket().getCriterion(Criterion.Type.IPV4_DST) != null) {
538 ipAddress = ((IPCriterion) trace.getInitialPacket()
539 .getCriterion(Criterion.Type.IPV4_DST)).ip().address();
540 } else if (trace.getInitialPacket().getCriterion(Criterion.Type.IPV6_DST) != null) {
541 ipAddress = ((IPCriterion) trace.getInitialPacket()
542 .getCriterion(Criterion.Type.IPV6_DST)).ip().address();
543 }
544 if (ipAddress != null) {
545 IpAddress finalIpAddress = ipAddress;
546 if (hostsList.stream().anyMatch(host -> host.ipAddresses().contains(finalIpAddress)) ||
547 hostService.getHostsByIp(finalIpAddress).isEmpty()) {
548 trace.addResultMessage("Packet is " +
549 ((EthTypeCriterion) outputPath.getFinalPacket()
Andrea Campanella220f19b2018-03-09 15:30:22 -0800550 .getCriterion(Criterion.Type.ETH_TYPE)).ethType() +
551 " and reached " + cp + " with hosts " + hostsList);
Andrea Campanellacc2424a2018-03-07 14:27:54 -0800552 } else {
553 trace.addResultMessage("Wrong output " + cp + " for required destination ip " +
554 ipAddress);
Andrea Campanellac8e6a502018-03-12 19:25:44 -0700555 trace.setSuccess(false);
Andrea Campanellacc2424a2018-03-07 14:27:54 -0800556 }
557 } else {
Andrea Campanella7b84c072018-03-06 15:21:09 -0800558 trace.addResultMessage("Packet is " + ((EthTypeCriterion) outputPath.getFinalPacket()
559 .getCriterion(Criterion.Type.ETH_TYPE)).ethType() + " and reached " +
560 cp + " with hosts " + hostsList);
Andrea Campanella7b84c072018-03-06 15:21:09 -0800561 }
Andrea Campanella7b84c072018-03-06 15:21:09 -0800562 }
Andrea Campanellacc2424a2018-03-07 14:27:54 -0800563 trace.setSuccess(true);
Andrea Campanella6f2d6742018-02-07 12:00:12 +0100564 }
Andrea Campanellad5bb2ef2018-01-31 16:43:23 +0100565 }
Andrea Campanellad5bb2ef2018-01-31 16:43:23 +0100566
567 } else {
Andrea Campanellad5bb2ef2018-01-31 16:43:23 +0100568 computePath(completePath, trace, cp);
Andrea Campanellac8e6a502018-03-12 19:25:44 -0700569 trace.setSuccess(false);
Andrea Campanella15278a12018-03-26 10:39:22 -0700570 if (deviceService.getPort(cp) == null) {
571 //Port is not existant on device.
572 log.warn("Port {} is not available on device.", cp);
573 trace.addResultMessage("Port " + cp + "is not available on device. Packet is dropped");
574 } else {
575 //No links means that the packet gets dropped.
576 log.warn("No links out of {}", cp);
577 trace.addResultMessage("No links depart from " + cp + ". Packet is dropped");
578 }
Andrea Campanella01e886e2017-12-15 15:27:31 +0100579 }
580 }
581 return trace;
582 }
583
Andrea Campanella04924b92018-01-17 16:34:51 +0100584
Andrea Campanella01e886e2017-12-15 15:27:31 +0100585 /**
Andrea Campanella58b3b522018-02-06 15:46:52 +0100586 * If the initial packet comes tagged with a Vlan we output it with that to ONOS.
587 * If ONOS applied a vlan we remove it.
588 *
589 * @param outputPath the output
590 * @param trace the trace we are building
591 */
Andrea Campanella04924b92018-01-17 16:34:51 +0100592
Andrea Campanella58b3b522018-02-06 15:46:52 +0100593 private void handleVlanToController(GroupsInDevice outputPath, StaticPacketTrace trace) {
594
595 VlanIdCriterion initialVid = (VlanIdCriterion) trace.getInitialPacket().getCriterion(Criterion.Type.VLAN_VID);
596 VlanIdCriterion finalVid = (VlanIdCriterion) outputPath.getFinalPacket().getCriterion(Criterion.Type.VLAN_VID);
597
598 if (initialVid != null && !initialVid.equals(finalVid) && initialVid.vlanId().equals(VlanId.NONE)) {
599
600 Set<Criterion> finalCriteria = new HashSet<>(outputPath.getFinalPacket().criteria());
601 //removing the final vlanId
602 finalCriteria.remove(finalVid);
603 Builder packetUpdated = DefaultTrafficSelector.builder();
604 finalCriteria.forEach(packetUpdated::add);
605 //Initial was none so we set it to that
606 packetUpdated.add(Criteria.matchVlanId(VlanId.NONE));
607 //Update final packet
608 outputPath.setFinalPacket(packetUpdated.build());
609 }
610 }
611
612 /**
Andrea Campanella04924b92018-01-17 16:34:51 +0100613 * Checks if the device has other outputs than the given connect point.
614 *
615 * @param inDeviceId the device
616 * @param trace the trace we are building
617 * @param cp an output connect point
618 * @return true if the device has other outputs.
619 */
620 private boolean hasOtherOutput(DeviceId inDeviceId, StaticPacketTrace trace, ConnectPoint cp) {
621 return trace.getGroupOuputs(inDeviceId).stream().filter(groupsInDevice -> {
622 return !groupsInDevice.getOutput().equals(cp);
623 }).count() > 0;
624 }
625
626 /**
Andrea Campanella01e886e2017-12-15 15:27:31 +0100627 * Checks if the path contains the device.
628 *
629 * @param completePath the path
630 * @param deviceId the device to check
631 * @return true if the path contains the device
632 */
633 //TODO might prove costly, improvement: a class with both CPs and DeviceIds point.
634 private boolean pathContainsDevice(List<ConnectPoint> completePath, DeviceId deviceId) {
635 for (ConnectPoint cp : completePath) {
636 if (cp.deviceId().equals(deviceId)) {
637 return true;
638 }
639 }
640 return false;
641 }
642
643 /**
644 * Gets the hosts for the given initial packet.
645 *
646 * @param trace the trace we are building
647 * @return set of the hosts we are trying to reach
648 */
649 private Set<Host> getHosts(StaticPacketTrace trace) {
650 IPCriterion ipv4Criterion = ((IPCriterion) trace.getInitialPacket()
651 .getCriterion(Criterion.Type.IPV4_DST));
652 IPCriterion ipv6Criterion = ((IPCriterion) trace.getInitialPacket()
653 .getCriterion(Criterion.Type.IPV6_DST));
654 Set<Host> hosts = new HashSet<>();
655 if (ipv4Criterion != null) {
656 hosts.addAll(hostService.getHostsByIp(ipv4Criterion.ip().address()));
657 }
658 if (ipv6Criterion != null) {
659 hosts.addAll(hostService.getHostsByIp(ipv6Criterion.ip().address()));
660 }
661 EthCriterion ethCriterion = ((EthCriterion) trace.getInitialPacket()
662 .getCriterion(Criterion.Type.ETH_DST));
663 if (ethCriterion != null) {
664 hosts.addAll(hostService.getHostsByMac(ethCriterion.mac()));
665 }
666 return hosts;
667 }
668
669 /**
670 * Computes the list of traversed connect points.
671 *
672 * @param completePath the list of devices
673 * @param trace the trace we are building
674 * @param output the final output connect point
675 */
Andrea Campanella79cb17d2018-02-27 18:03:17 +0100676 private boolean computePath(List<ConnectPoint> completePath, StaticPacketTrace trace, ConnectPoint output) {
Andrea Campanella01e886e2017-12-15 15:27:31 +0100677 List<ConnectPoint> traverseList = new ArrayList<>();
678 if (!completePath.contains(trace.getInitialConnectPoint())) {
679 traverseList.add(trace.getInitialConnectPoint());
680 }
681 traverseList.addAll(completePath);
682 if (output != null && !completePath.contains(output)) {
683 traverseList.add(output);
684 }
Andrea Campanella79cb17d2018-02-27 18:03:17 +0100685 if (!trace.getCompletePaths().contains(traverseList)) {
Andrea Campanellaece11772018-03-09 14:52:10 -0800686 trace.addCompletePath(ImmutableList.copyOf(traverseList));
Andrea Campanella79cb17d2018-02-27 18:03:17 +0100687 return true;
688 }
689 return false;
Andrea Campanella01e886e2017-12-15 15:27:31 +0100690 }
691
692 /**
693 * Traces the packet inside a device starting from an input connect point.
694 *
Andrea Campanellad1cc1a32018-03-21 10:08:55 -0700695 * @param trace the trace we are building
696 * @param packet the packet we are tracing
697 * @param in the input connect point.
698 * @param isDualHomed true if the trace we are doing starts or ends in a dual homed host
699 * @param completePath the path up until this device
Andrea Campanella01e886e2017-12-15 15:27:31 +0100700 * @return updated trace
701 */
Andrea Campanella79cb17d2018-02-27 18:03:17 +0100702 private StaticPacketTrace traceInDevice(StaticPacketTrace trace, TrafficSelector packet, ConnectPoint in,
Andrea Campanellad1cc1a32018-03-21 10:08:55 -0700703 boolean isDualHomed, List<ConnectPoint> completePath) {
Andrea Campanellae04aac92018-01-31 14:59:03 +0100704
Andrea Campanellacc2424a2018-03-07 14:27:54 -0800705 boolean multipleRoutes = false;
706 if (trace.getGroupOuputs(in.deviceId()) != null) {
707 multipleRoutes = multipleRoutes(trace);
708 }
709 if (trace.getGroupOuputs(in.deviceId()) != null && !isDualHomed && !multipleRoutes) {
Andrea Campanellae04aac92018-01-31 14:59:03 +0100710 log.debug("Trace already contains device and given outputs");
711 return trace;
712 }
Andrea Campanellacc2424a2018-03-07 14:27:54 -0800713
Andrea Campanella01e886e2017-12-15 15:27:31 +0100714 log.debug("Packet {} coming in from {}", packet, in);
Andrea Campanella37d10622018-01-18 17:11:42 +0100715
716 //if device is not available exit here.
717 if (!deviceService.isAvailable(in.deviceId())) {
718 trace.addResultMessage("Device is offline " + in.deviceId());
Andrea Campanellad1cc1a32018-03-21 10:08:55 -0700719 computePath(completePath, trace, null);
Andrea Campanella37d10622018-01-18 17:11:42 +0100720 return trace;
721 }
722
Andrea Campanella6f2d6742018-02-07 12:00:12 +0100723 //handle when the input is the controller
724 //NOTE, we are using the input port as a convenience to carry the CONTROLLER port number even if
725 // a packet in from the controller will not actually traverse the pipeline and have no such notion
726 // as the input port.
727 if (in.port().equals(PortNumber.CONTROLLER)) {
728 StaticPacketTrace outputTrace = inputFromController(trace, in);
729 if (outputTrace != null) {
730 return trace;
731 }
732 }
733
Andrea Campanella01e886e2017-12-15 15:27:31 +0100734 List<FlowEntry> flows = new ArrayList<>();
735 List<FlowEntry> outputFlows = new ArrayList<>();
Andrea Campanellad5bb2ef2018-01-31 16:43:23 +0100736 List<Instruction> deferredInstructions = new ArrayList<>();
737
Andrea Campanella01e886e2017-12-15 15:27:31 +0100738 FlowEntry nextTableIdEntry = findNextTableIdEntry(in.deviceId(), -1);
739 if (nextTableIdEntry == null) {
740 trace.addResultMessage("No flow rules for device " + in.deviceId() + ". Aborting");
Andrea Campanellad1cc1a32018-03-21 10:08:55 -0700741 computePath(completePath, trace, null);
Andrea Campanellac8e6a502018-03-12 19:25:44 -0700742 trace.setSuccess(false);
Andrea Campanella01e886e2017-12-15 15:27:31 +0100743 return trace;
744 }
745 TableId tableId = nextTableIdEntry.table();
746 FlowEntry flowEntry;
747 boolean output = false;
748 while (!output) {
749 log.debug("Searching a Flow Entry on table {} for packet {}", tableId, packet);
750 //get the rule that matches the incoming packet
751 flowEntry = matchHighestPriority(packet, in, tableId);
752 log.debug("Found Flow Entry {}", flowEntry);
753
754 boolean isOfdpaHardware = TroubleshootUtils.hardwareOfdpaMap
755 .getOrDefault(driverService.getDriver(in.deviceId()).name(), false);
756
757 //if the flow entry on a table is null and we are on hardware we treat as table miss, with few exceptions
758 if (flowEntry == null && isOfdpaHardware) {
759 log.debug("Ofdpa Hw setup, no flow rule means table miss");
760
Andrea Campanella01e886e2017-12-15 15:27:31 +0100761 if (((IndexTableId) tableId).id() == 27) {
762 //Apparently a miss but Table 27 on OFDPA is a fixed table
763 packet = handleOfdpa27FixedTable(trace, packet);
764 }
765
766 //Finding next table to go In case of miss
767 nextTableIdEntry = findNextTableIdEntry(in.deviceId(), ((IndexTableId) tableId).id());
768 log.debug("Next table id entry {}", nextTableIdEntry);
769
770 //FIXME find better solution that enable granularity greater than 0 or all rules
771 //(another possibility is max tableId)
772 if (nextTableIdEntry == null && flows.size() == 0) {
Andrea Campanella7382c7f2018-02-05 19:39:25 +0100773 trace.addResultMessage("No matching flow rules for device " + in.deviceId() + ". Aborting");
Andrea Campanellad1cc1a32018-03-21 10:08:55 -0700774 computePath(completePath, trace, null);
Andrea Campanellac8e6a502018-03-12 19:25:44 -0700775 trace.setSuccess(false);
Andrea Campanella01e886e2017-12-15 15:27:31 +0100776 return trace;
777
778 } else if (nextTableIdEntry == null) {
779 //Means that no more flow rules are present
780 output = true;
781
782 } else if (((IndexTableId) tableId).id() == 20) {
783 //if the table is 20 OFDPA skips to table 50
784 log.debug("A miss on Table 20 on OFDPA means that we skip directly to table 50");
785 tableId = IndexTableId.of(50);
786
787 } else {
788 tableId = nextTableIdEntry.table();
789 }
790
Andrea Campanella01e886e2017-12-15 15:27:31 +0100791 } else if (flowEntry == null) {
792 trace.addResultMessage("Packet has no match on table " + tableId + " in device " +
793 in.deviceId() + ". Dropping");
Andrea Campanellad1cc1a32018-03-21 10:08:55 -0700794 computePath(completePath, trace, null);
Andrea Campanellac8e6a502018-03-12 19:25:44 -0700795 trace.setSuccess(false);
Andrea Campanella01e886e2017-12-15 15:27:31 +0100796 return trace;
797 } else {
Andrea Campanella97f9d4c2018-02-06 18:58:40 +0100798
Andrea Campanella01e886e2017-12-15 15:27:31 +0100799 //IF the table has a transition
800 if (flowEntry.treatment().tableTransition() != null) {
801 //update the next table we transitions to
802 tableId = IndexTableId.of(flowEntry.treatment().tableTransition().tableId());
803 log.debug("Flow Entry has transition to table Id {}", tableId);
804 flows.add(flowEntry);
805 } else {
806 //table has no transition so it means that it's an output rule if on the last table
807 log.debug("Flow Entry has no transition to table, treating as last rule {}", flowEntry);
808 flows.add(flowEntry);
809 outputFlows.add(flowEntry);
810 output = true;
811 }
Andrea Campanellad5bb2ef2018-01-31 16:43:23 +0100812 //update the packet according to the immediate actions of this flow rule.
813 packet = updatePacket(packet, flowEntry.treatment().immediate()).build();
814
815 //save the deferred rules for later
816 deferredInstructions.addAll(flowEntry.treatment().deferred());
817
818 //If the flow requires to clear deferred actions we do so for all the ones we encountered.
819 if (flowEntry.treatment().clearedDeferred()) {
820 deferredInstructions.clear();
821 }
822
Andrea Campanella97f9d4c2018-02-06 18:58:40 +0100823 //On table 10 OFDPA needs two rules to apply the vlan if none and then to transition to the next table.
824 if (needsSecondTable10Flow(flowEntry, isOfdpaHardware)) {
825
826 //Let's get the packet vlanId instruction
827 VlanIdCriterion packetVlanIdCriterion =
828 (VlanIdCriterion) packet.getCriterion(Criterion.Type.VLAN_VID);
829
830 //Let's get the flow entry vlan mod instructions
831 ModVlanIdInstruction entryModVlanIdInstruction = (ModVlanIdInstruction) flowEntry.treatment()
832 .immediate().stream()
833 .filter(instruction -> instruction instanceof ModVlanIdInstruction)
834 .findFirst().orElse(null);
835
836 //If the entry modVlan is not null we need to make sure that the packet has been updated and there
837 // is a flow rule that matches on same criteria and with updated vlanId
838 if (entryModVlanIdInstruction != null) {
839
840 FlowEntry secondVlanFlow = getSecondFlowEntryOnTable10(packet, in,
841 packetVlanIdCriterion, entryModVlanIdInstruction);
842
843 //We found the flow that we expected
844 if (secondVlanFlow != null) {
845 flows.add(secondVlanFlow);
846 } else {
847 trace.addResultMessage("Missing forwarding rule for tagged packet on " + in);
Andrea Campanellad1cc1a32018-03-21 10:08:55 -0700848 computePath(completePath, trace, null);
Andrea Campanella97f9d4c2018-02-06 18:58:40 +0100849 return trace;
850 }
851 }
852
853 }
854
Andrea Campanella01e886e2017-12-15 15:27:31 +0100855 }
856 }
857
858 //Creating a modifiable builder for the output packet
Andrea Campanellad5bb2ef2018-01-31 16:43:23 +0100859 Builder builder = DefaultTrafficSelector.builder();
Andrea Campanella01e886e2017-12-15 15:27:31 +0100860 packet.criteria().forEach(builder::add);
Andrea Campanellad5bb2ef2018-01-31 16:43:23 +0100861
Andrea Campanella01e886e2017-12-15 15:27:31 +0100862 //Adding all the flows to the trace
Andrea Campanella7c8e7912018-01-23 12:46:04 +0100863 trace.addFlowsForDevice(in.deviceId(), ImmutableList.copyOf(flows));
Andrea Campanella01e886e2017-12-15 15:27:31 +0100864
Andrea Campanella01e886e2017-12-15 15:27:31 +0100865 List<PortNumber> outputPorts = new ArrayList<>();
Andrea Campanellacc2424a2018-03-07 14:27:54 -0800866 List<FlowEntry> outputFlowEntries = handleFlows(trace, packet, in, outputFlows, builder, outputPorts);
Andrea Campanella01e886e2017-12-15 15:27:31 +0100867
Andrea Campanellacc2424a2018-03-07 14:27:54 -0800868
869 log.debug("Handling Groups");
870 //Analyze Groups
871 List<Group> groups = new ArrayList<>();
872
873 Collection<FlowEntry> nonOutputFlows = flows;
874 nonOutputFlows.removeAll(outputFlowEntries);
875
876 //Handling groups pointed at by immediate instructions
877 for (FlowEntry entry : flows) {
878 getGroupsFromInstructions(trace, groups, entry.treatment().immediate(),
Andrea Campanellad1cc1a32018-03-21 10:08:55 -0700879 entry.deviceId(), builder, outputPorts, in, completePath);
Andrea Campanellacc2424a2018-03-07 14:27:54 -0800880 }
881
882 //If we have deferred instructions at this point we handle them.
883 if (deferredInstructions.size() > 0) {
Andrea Campanellad1cc1a32018-03-21 10:08:55 -0700884 builder = handleDeferredActions(trace, packet, in, deferredInstructions, outputPorts, groups, completePath);
Andrea Campanellacc2424a2018-03-07 14:27:54 -0800885
886 }
887 packet = builder.build();
888
889 log.debug("Output Packet {}", packet);
890 return trace;
891 }
892
893 private List<FlowEntry> handleFlows(StaticPacketTrace trace, TrafficSelector packet, ConnectPoint in,
894 List<FlowEntry> outputFlows, Builder builder, List<PortNumber> outputPorts) {
Andrea Campanella7c8e7912018-01-23 12:46:04 +0100895 //TODO optimization
Andrea Campanella7c8e7912018-01-23 12:46:04 +0100896 //outputFlows contains also last rule of device, so we need filtering for OUTPUT instructions.
897 List<FlowEntry> outputFlowEntries = outputFlows.stream().filter(flow -> flow.treatment()
898 .allInstructions().stream().filter(instruction -> instruction.type()
899 .equals(Instruction.Type.OUTPUT)).count() > 0).collect(Collectors.toList());
Andrea Campanella01e886e2017-12-15 15:27:31 +0100900
Andrea Campanella7c8e7912018-01-23 12:46:04 +0100901 if (outputFlowEntries.size() > 1) {
902 trace.addResultMessage("More than one flow rule with OUTPUT instruction");
903 log.warn("There cannot be more than one flow entry with OUTPUT instruction for {}", packet);
Andrea Campanella01e886e2017-12-15 15:27:31 +0100904 }
Andrea Campanella7c8e7912018-01-23 12:46:04 +0100905
906 if (outputFlowEntries.size() == 1) {
907
908 OutputInstruction outputInstruction = (OutputInstruction) outputFlowEntries.get(0).treatment()
909 .allInstructions().stream()
910 .filter(instruction -> {
911 return instruction.type().equals(Instruction.Type.OUTPUT);
912 }).findFirst().get();
913
914 //FIXME using GroupsInDevice for output even if flows.
915 buildOutputFromDevice(trace, in, builder, outputPorts, outputInstruction, ImmutableList.of());
916
917 }
Andrea Campanellacc2424a2018-03-07 14:27:54 -0800918 return outputFlowEntries;
919 }
Andrea Campanella7c8e7912018-01-23 12:46:04 +0100920
Andrea Campanellacc2424a2018-03-07 14:27:54 -0800921 private boolean multipleRoutes(StaticPacketTrace trace) {
922 boolean multipleRoutes = false;
923 IPCriterion ipCriterion = ((IPCriterion) trace.getInitialPacket().getCriterion(Criterion.Type.IPV4_DST));
924 IpAddress ip = null;
925 if (ipCriterion != null) {
926 ip = ipCriterion.ip().address();
927 } else if (trace.getInitialPacket().getCriterion(Criterion.Type.IPV6_DST) != null) {
928 ip = ((IPCriterion) trace.getInitialPacket().getCriterion(Criterion.Type.IPV6_DST)).ip().address();
Andrea Campanella7c8e7912018-01-23 12:46:04 +0100929 }
Andrea Campanellae93f6612018-04-04 13:10:45 +0200930 if (ip != null) {
931 Optional<ResolvedRoute> optionalRoute = routeService.longestPrefixLookup(ip);
932 if (optionalRoute.isPresent()) {
933 ResolvedRoute route = optionalRoute.get();
934 route.prefix();
935 multipleRoutes = routeService.getAllResolvedRoutes(route.prefix()).size() > 1;
936 }
Andrea Campanellad5bb2ef2018-01-31 16:43:23 +0100937 }
Andrea Campanellacc2424a2018-03-07 14:27:54 -0800938 return multipleRoutes;
Andrea Campanella01e886e2017-12-15 15:27:31 +0100939 }
940
Andrea Campanella6f2d6742018-02-07 12:00:12 +0100941 /**
942 * Handles the specific case where the Input is the controller.
943 * Note that the in port is used as a convenience to store the port of the controller even if the packet in
944 * from a controller should not have a physical input port. The in port from the Controller is used to make sure
945 * the flood to all active physical ports of the device.
946 *
947 * @param trace the trace
948 * @param in the controller port
949 * @return the augmented trace.
950 */
951 private StaticPacketTrace inputFromController(StaticPacketTrace trace, ConnectPoint in) {
952 EthTypeCriterion ethTypeCriterion = (EthTypeCriterion) trace.getInitialPacket()
953 .getCriterion(Criterion.Type.ETH_TYPE);
954 //If the packet is LLDP or BDDP we flood it on all active ports of the switch.
955 if (ethTypeCriterion != null && (ethTypeCriterion.ethType().equals(EtherType.LLDP.ethType())
956 || ethTypeCriterion.ethType().equals(EtherType.BDDP.ethType()))) {
957 //get the active ports
958 List<Port> enabledPorts = deviceService.getPorts(in.deviceId()).stream()
959 .filter(Port::isEnabled)
960 .collect(Collectors.toList());
961 //build an output from each one
962 enabledPorts.forEach(port -> {
963 GroupsInDevice output = new GroupsInDevice(new ConnectPoint(port.element().id(), port.number()),
964 ImmutableList.of(), trace.getInitialPacket());
965 trace.addGroupOutputPath(in.deviceId(), output);
966 });
967 return trace;
968 }
969 return null;
970 }
971
Andrea Campanella97f9d4c2018-02-06 18:58:40 +0100972 private boolean needsSecondTable10Flow(FlowEntry flowEntry, boolean isOfdpaHardware) {
973 return isOfdpaHardware && flowEntry.table().equals(IndexTableId.of(10))
974 && flowEntry.selector().getCriterion(Criterion.Type.VLAN_VID) != null
975 && ((VlanIdCriterion) flowEntry.selector().getCriterion(Criterion.Type.VLAN_VID))
976 .vlanId().equals(VlanId.NONE);
977 }
978
979 /**
980 * Method that finds a flow rule on table 10 that matches the packet and the VLAN of the already
981 * found rule on table 10. This is because OFDPA needs two rules on table 10, first to apply the rule,
982 * second to transition to following table
983 *
984 * @param packet the incoming packet
985 * @param in the input connect point
986 * @param packetVlanIdCriterion the vlan criterion from the packet
987 * @param entryModVlanIdInstruction the entry vlan instruction
988 * @return the second flow entry that matched
989 */
990 private FlowEntry getSecondFlowEntryOnTable10(TrafficSelector packet, ConnectPoint in,
991 VlanIdCriterion packetVlanIdCriterion,
992 ModVlanIdInstruction entryModVlanIdInstruction) {
993 FlowEntry secondVlanFlow = null;
994 //Check the packet has been update from the first rule.
995 if (packetVlanIdCriterion.vlanId().equals(entryModVlanIdInstruction.vlanId())) {
996 //find a rule on the same table that matches the vlan and
997 // also all the other elements of the flow such as input port
998 secondVlanFlow = Lists.newArrayList(flowRuleService.getFlowEntries(in.deviceId()).iterator())
999 .stream()
1000 .filter(entry -> {
1001 return entry.table().equals(IndexTableId.of(10));
1002 })
1003 .filter(entry -> {
1004 VlanIdCriterion criterion = (VlanIdCriterion) entry.selector()
1005 .getCriterion(Criterion.Type.VLAN_VID);
1006 return criterion != null && match(packet, entry)
1007 && criterion.vlanId().equals(entryModVlanIdInstruction.vlanId());
1008 }).findFirst().orElse(null);
1009
1010 }
1011 return secondVlanFlow;
1012 }
1013
Andrea Campanellad5bb2ef2018-01-31 16:43:23 +01001014
Andrea Campanella01e886e2017-12-15 15:27:31 +01001015 /**
Andrea Campanella01e886e2017-12-15 15:27:31 +01001016 * Handles table 27 in Ofpda which is a fixed table not visible to any controller that handles Mpls Labels.
1017 *
1018 * @param packet the incoming packet
1019 * @return the updated packet
1020 */
1021 private TrafficSelector handleOfdpa27FixedTable(StaticPacketTrace trace, TrafficSelector packet) {
1022 log.debug("Handling table 27 on OFDPA, removing mpls ETH Type and change mpls label");
1023 Criterion mplsCriterion = packet.getCriterion(Criterion.Type.ETH_TYPE);
1024 ImmutableList.Builder<Instruction> builder = ImmutableList.builder();
1025
1026 //If the pakcet comes in with the expected elements we update it as per OFDPA spec.
1027 if (mplsCriterion != null && ((EthTypeCriterion) mplsCriterion).ethType()
1028 .equals(EtherType.MPLS_UNICAST.ethType())) {
Andrea Campanella09ca07a2018-01-25 16:44:04 +01001029 //TODO update with parsing with eth MPLS pop Instruction for treating label an bos
Andrea Campanella01e886e2017-12-15 15:27:31 +01001030 Instruction ethInstruction = Instructions.popMpls(((EthTypeCriterion) trace.getInitialPacket()
1031 .getCriterion(Criterion.Type.ETH_TYPE)).ethType());
1032 //FIXME what do we use as L3_Unicast mpls Label ?
Andrea Campanella09ca07a2018-01-25 16:44:04 +01001033 //translateInstruction(builder, ethInstruction);
Andrea Campanella01e886e2017-12-15 15:27:31 +01001034 builder.add(ethInstruction);
1035 }
1036 packet = updatePacket(packet, builder.build()).build();
1037 return packet;
1038 }
1039
1040 /**
1041 * Finds the flow entry with the minimun next table Id.
1042 *
1043 * @param deviceId the device to search
1044 * @param currentId the current id. the search will use this as minimum
1045 * @return the flow entry with the minimum table Id after the given one.
1046 */
1047 private FlowEntry findNextTableIdEntry(DeviceId deviceId, int currentId) {
1048
1049 final Comparator<FlowEntry> comparator = Comparator.comparing((FlowEntry f) -> ((IndexTableId) f.table()).id());
1050
1051 return Lists.newArrayList(flowRuleService.getFlowEntries(deviceId).iterator())
1052 .stream().filter(f -> ((IndexTableId) f.table()).id() > currentId).min(comparator).orElse(null);
1053 }
1054
Andrea Campanellad5bb2ef2018-01-31 16:43:23 +01001055 private Builder handleDeferredActions(StaticPacketTrace trace, TrafficSelector packet,
1056 ConnectPoint in, List<Instruction> deferredInstructions,
Andrea Campanellad1cc1a32018-03-21 10:08:55 -07001057 List<PortNumber> outputPorts, List<Group> groups,
1058 List<ConnectPoint> completePath) {
Andrea Campanellad5bb2ef2018-01-31 16:43:23 +01001059
1060 //Update the packet with the deferred instructions
1061 Builder builder = updatePacket(packet, deferredInstructions);
1062
1063 //Gather any output instructions from the deferred instruction
1064 List<Instruction> outputFlowInstruction = deferredInstructions.stream().filter(instruction -> {
1065 return instruction.type().equals(Instruction.Type.OUTPUT);
1066 }).collect(Collectors.toList());
1067
1068 //We are considering deferred instructions from flows, there can only be one output.
1069 if (outputFlowInstruction.size() > 1) {
1070 trace.addResultMessage("More than one flow rule with OUTPUT instruction");
1071 log.warn("There cannot be more than one flow entry with OUTPUT instruction for {}", packet);
1072 }
1073 //If there is one output let's go through that
1074 if (outputFlowInstruction.size() == 1) {
1075 buildOutputFromDevice(trace, in, builder, outputPorts, (OutputInstruction) outputFlowInstruction.get(0),
1076 ImmutableList.of());
1077 }
1078 //If there is no output let's see if there any deferred instruction point to groups.
1079 if (outputFlowInstruction.size() == 0) {
1080 getGroupsFromInstructions(trace, groups, deferredInstructions,
Andrea Campanellad1cc1a32018-03-21 10:08:55 -07001081 in.deviceId(), builder, outputPorts, in, completePath);
Andrea Campanellad5bb2ef2018-01-31 16:43:23 +01001082 }
1083 return builder;
1084 }
1085
Andrea Campanella01e886e2017-12-15 15:27:31 +01001086 /**
1087 * Gets group information from instructions.
1088 *
1089 * @param trace the trace we are building
1090 * @param groupsForDevice the set of groups for this device
1091 * @param instructions the set of instructions we are searching for groups.
1092 * @param deviceId the device we are considering
1093 * @param builder the builder of the input packet
1094 * @param outputPorts the output ports for that packet
1095 */
1096 private void getGroupsFromInstructions(StaticPacketTrace trace, List<Group> groupsForDevice,
1097 List<Instruction> instructions, DeviceId deviceId,
Andrea Campanellad5bb2ef2018-01-31 16:43:23 +01001098 Builder builder, List<PortNumber> outputPorts,
Andrea Campanellad1cc1a32018-03-21 10:08:55 -07001099 ConnectPoint in, List<ConnectPoint> completePath) {
Andrea Campanella01e886e2017-12-15 15:27:31 +01001100 List<Instruction> groupInstructionlist = new ArrayList<>();
1101 for (Instruction instruction : instructions) {
1102 log.debug("Considering Instruction {}", instruction);
1103 //if the instruction is not group we need to update the packet or add the output
1104 //to the possible outputs for this packet
1105 if (!instruction.type().equals(Instruction.Type.GROUP)) {
1106 //if the instruction is not group we need to update the packet or add the output
1107 //to the possible outputs for this packet
1108 if (instruction.type().equals(Instruction.Type.OUTPUT)) {
Andrea Campanellabb9d3fb2018-01-22 15:10:30 +01001109 buildOutputFromDevice(trace, in, builder, outputPorts,
Andrea Campanella3f98c212018-02-19 17:03:46 +01001110 (OutputInstruction) instruction, ImmutableList.copyOf(groupsForDevice));
1111 //clearing the groups because we start from the top.
1112 groupsForDevice.clear();
Andrea Campanella01e886e2017-12-15 15:27:31 +01001113 } else {
1114 builder = translateInstruction(builder, instruction);
1115 }
1116 } else {
1117 //if the instuction is pointing to a group we need to get the group
1118 groupInstructionlist.add(instruction);
1119 }
1120 }
1121 //handle all the internal instructions pointing to a group.
1122 for (Instruction instr : groupInstructionlist) {
1123 GroupInstruction groupInstruction = (GroupInstruction) instr;
1124 Group group = Lists.newArrayList(groupService.getGroups(deviceId)).stream().filter(groupInternal -> {
1125 return groupInternal.id().equals(groupInstruction.groupId());
1126 }).findAny().orElse(null);
1127 if (group == null) {
1128 trace.addResultMessage("Null group for Instruction " + instr);
Andrea Campanellac8e6a502018-03-12 19:25:44 -07001129 trace.setSuccess(false);
Andrea Campanella01e886e2017-12-15 15:27:31 +01001130 break;
1131 }
Andrea Campanella7cb4fd82018-02-27 12:36:00 +01001132 if (group.buckets().buckets().size() == 0) {
Andrea Campanellad1cc1a32018-03-21 10:08:55 -07001133 trace.addResultMessage("Group " + group.id() + " has no buckets");
Andrea Campanellac8e6a502018-03-12 19:25:44 -07001134 trace.setSuccess(false);
Andrea Campanellad1cc1a32018-03-21 10:08:55 -07001135 computePath(completePath, trace, null);
Andrea Campanella7cb4fd82018-02-27 12:36:00 +01001136 break;
1137 }
Andrea Campanella3f98c212018-02-19 17:03:46 +01001138
Andrea Campanella01e886e2017-12-15 15:27:31 +01001139 //Cycle in each of the group's buckets and add them to the groups for this Device.
1140 for (GroupBucket bucket : group.buckets().buckets()) {
Andrea Campanella3f98c212018-02-19 17:03:46 +01001141
1142 //add the group to the traversed groups
1143 if (!groupsForDevice.contains(group)) {
1144 groupsForDevice.add(group);
1145 }
1146
Andrea Campanella01e886e2017-12-15 15:27:31 +01001147 getGroupsFromInstructions(trace, groupsForDevice, bucket.treatment().allInstructions(),
Andrea Campanellad1cc1a32018-03-21 10:08:55 -07001148 deviceId, builder, outputPorts, in, completePath);
Andrea Campanella01e886e2017-12-15 15:27:31 +01001149 }
1150 }
1151 }
1152
1153 /**
Andrea Campanellabb9d3fb2018-01-22 15:10:30 +01001154 * Check if the output is the input port, if so adds a dop result message, otherwise builds
1155 * a possible output from this device.
1156 *
1157 * @param trace the trace
1158 * @param in the input connect point
1159 * @param builder the packet builder
1160 * @param outputPorts the list of output ports for this device
1161 * @param outputInstruction the output instruction
Andrea Campanellad5bb2ef2018-01-31 16:43:23 +01001162 * @param groupsForDevice the groups we output from
Andrea Campanellabb9d3fb2018-01-22 15:10:30 +01001163 */
Andrea Campanellad5bb2ef2018-01-31 16:43:23 +01001164 private void buildOutputFromDevice(StaticPacketTrace trace, ConnectPoint in, Builder builder,
Andrea Campanellabb9d3fb2018-01-22 15:10:30 +01001165 List<PortNumber> outputPorts, OutputInstruction outputInstruction,
1166 List<Group> groupsForDevice) {
Andrea Campanella7c8e7912018-01-23 12:46:04 +01001167 ConnectPoint output = new ConnectPoint(in.deviceId(), outputInstruction.port());
Andrea Campanella6f2d6742018-02-07 12:00:12 +01001168
Andrea Campanella7b84c072018-03-06 15:21:09 -08001169 outputPorts.add(outputInstruction.port());
1170
1171 GroupsInDevice device = new GroupsInDevice(output, groupsForDevice, builder.build());
1172 if (trace.getGroupOuputs(output.deviceId()) != null
1173 && trace.getGroupOuputs(output.deviceId()).contains(device)) {
1174 return;
Andrea Campanellabb9d3fb2018-01-22 15:10:30 +01001175 }
Andrea Campanella7b84c072018-03-06 15:21:09 -08001176 trace.addGroupOutputPath(in.deviceId(),
1177 new GroupsInDevice(output, groupsForDevice, builder.build()));
Andrea Campanellabb9d3fb2018-01-22 15:10:30 +01001178 }
1179
1180 /**
Andrea Campanella01e886e2017-12-15 15:27:31 +01001181 * Applies all give instructions to the input packet.
1182 *
1183 * @param packet the input packet
1184 * @param instructions the set of instructions
1185 * @return the packet with the applied instructions
1186 */
Andrea Campanellad5bb2ef2018-01-31 16:43:23 +01001187 private Builder updatePacket(TrafficSelector packet, List<Instruction> instructions) {
1188 Builder newSelector = DefaultTrafficSelector.builder();
Andrea Campanella01e886e2017-12-15 15:27:31 +01001189 packet.criteria().forEach(newSelector::add);
Andrea Campanella09ca07a2018-01-25 16:44:04 +01001190 //FIXME optimize
1191 for (Instruction instruction : instructions) {
1192 newSelector = translateInstruction(newSelector, instruction);
1193 }
Andrea Campanella01e886e2017-12-15 15:27:31 +01001194 return newSelector;
1195 }
1196
1197 /**
1198 * Applies an instruction to the packet in the form of a selector.
1199 *
1200 * @param newSelector the packet selector
1201 * @param instruction the instruction to be translated
1202 * @return the new selector with the applied instruction
1203 */
Andrea Campanellad5bb2ef2018-01-31 16:43:23 +01001204 private Builder translateInstruction(Builder newSelector, Instruction instruction) {
Andrea Campanella01e886e2017-12-15 15:27:31 +01001205 log.debug("Translating instruction {}", instruction);
Andrea Campanella09ca07a2018-01-25 16:44:04 +01001206 log.debug("New Selector {}", newSelector.build());
Andrea Campanella01e886e2017-12-15 15:27:31 +01001207 //TODO add as required
1208 Criterion criterion = null;
1209 switch (instruction.type()) {
1210 case L2MODIFICATION:
1211 L2ModificationInstruction l2Instruction = (L2ModificationInstruction) instruction;
1212 switch (l2Instruction.subtype()) {
1213 case VLAN_ID:
Andrea Campanella97f9d4c2018-02-06 18:58:40 +01001214 ModVlanIdInstruction vlanIdInstruction =
1215 (ModVlanIdInstruction) instruction;
Andrea Campanella01e886e2017-12-15 15:27:31 +01001216 VlanId id = vlanIdInstruction.vlanId();
1217 criterion = Criteria.matchVlanId(id);
1218 break;
1219 case VLAN_POP:
1220 criterion = Criteria.matchVlanId(VlanId.NONE);
1221 break;
1222 case MPLS_PUSH:
Andrea Campanella97f9d4c2018-02-06 18:58:40 +01001223 ModMplsHeaderInstruction mplsEthInstruction =
1224 (ModMplsHeaderInstruction) instruction;
Andrea Campanella01e886e2017-12-15 15:27:31 +01001225 criterion = Criteria.matchEthType(mplsEthInstruction.ethernetType().toShort());
1226 break;
1227 case MPLS_POP:
Andrea Campanella97f9d4c2018-02-06 18:58:40 +01001228 ModMplsHeaderInstruction mplsPopInstruction =
1229 (ModMplsHeaderInstruction) instruction;
Andrea Campanella01e886e2017-12-15 15:27:31 +01001230 criterion = Criteria.matchEthType(mplsPopInstruction.ethernetType().toShort());
Andrea Campanella09ca07a2018-01-25 16:44:04 +01001231
1232 //When popping MPLS we remove label and BOS
1233 TrafficSelector temporaryPacket = newSelector.build();
1234 if (temporaryPacket.getCriterion(Criterion.Type.MPLS_LABEL) != null) {
Andrea Campanellad5bb2ef2018-01-31 16:43:23 +01001235 Builder noMplsSelector = DefaultTrafficSelector.builder();
Andrea Campanella09ca07a2018-01-25 16:44:04 +01001236 temporaryPacket.criteria().stream().filter(c -> {
1237 return !c.type().equals(Criterion.Type.MPLS_LABEL) &&
1238 !c.type().equals(Criterion.Type.MPLS_BOS);
1239 }).forEach(noMplsSelector::add);
1240 newSelector = noMplsSelector;
1241 }
1242
Andrea Campanella01e886e2017-12-15 15:27:31 +01001243 break;
1244 case MPLS_LABEL:
Andrea Campanella97f9d4c2018-02-06 18:58:40 +01001245 ModMplsLabelInstruction mplsLabelInstruction =
1246 (ModMplsLabelInstruction) instruction;
Andrea Campanella01e886e2017-12-15 15:27:31 +01001247 criterion = Criteria.matchMplsLabel(mplsLabelInstruction.label());
Andrea Campanella09ca07a2018-01-25 16:44:04 +01001248 newSelector.matchMplsBos(true);
Andrea Campanella01e886e2017-12-15 15:27:31 +01001249 break;
1250 case ETH_DST:
Andrea Campanella97f9d4c2018-02-06 18:58:40 +01001251 ModEtherInstruction modEtherDstInstruction =
1252 (ModEtherInstruction) instruction;
Andrea Campanella01e886e2017-12-15 15:27:31 +01001253 criterion = Criteria.matchEthDst(modEtherDstInstruction.mac());
1254 break;
1255 case ETH_SRC:
Andrea Campanella97f9d4c2018-02-06 18:58:40 +01001256 ModEtherInstruction modEtherSrcInstruction =
1257 (ModEtherInstruction) instruction;
Andrea Campanella01e886e2017-12-15 15:27:31 +01001258 criterion = Criteria.matchEthSrc(modEtherSrcInstruction.mac());
1259 break;
1260 default:
1261 log.debug("Unsupported L2 Instruction");
1262 break;
1263 }
1264 break;
1265 default:
1266 log.debug("Unsupported Instruction");
1267 break;
1268 }
1269 if (criterion != null) {
1270 log.debug("Adding criterion {}", criterion);
1271 newSelector.add(criterion);
1272 }
1273 return newSelector;
1274 }
1275
1276 /**
1277 * Finds the rule in the device that mathces the input packet and has the highest priority.
1278 *
1279 * @param packet the input packet
1280 * @param in the connect point the packet comes in from
1281 * @param tableId the table to search
1282 * @return the flow entry
1283 */
1284 private FlowEntry matchHighestPriority(TrafficSelector packet, ConnectPoint in, TableId tableId) {
1285 //Computing the possible match rules.
1286 final Comparator<FlowEntry> comparator = Comparator.comparing(FlowRule::priority);
1287 return Lists.newArrayList(flowRuleService.getFlowEntries(in.deviceId()).iterator())
1288 .stream()
1289 .filter(flowEntry -> {
1290 return flowEntry.table().equals(tableId);
1291 })
1292 .filter(flowEntry -> {
1293 return match(packet, flowEntry);
1294 }).max(comparator).orElse(null);
1295 }
1296
1297 /**
1298 * Matches the packet with the given flow entry.
1299 *
1300 * @param packet the packet to match
1301 * @param flowEntry the flow entry to match the packet against
1302 * @return true if the packet matches the flow.
1303 */
1304 private boolean match(TrafficSelector packet, FlowEntry flowEntry) {
Andrea Campanella01e886e2017-12-15 15:27:31 +01001305 return flowEntry.selector().criteria().stream().allMatch(criterion -> {
1306 Criterion.Type type = criterion.type();
Andrea Campanella4ee4af12018-01-31 12:20:48 +01001307 //If the criterion has IP we need to do LPM to establish matching.
Andrea Campanella01e886e2017-12-15 15:27:31 +01001308 if (type.equals(Criterion.Type.IPV4_SRC) || type.equals(Criterion.Type.IPV4_DST) ||
1309 type.equals(Criterion.Type.IPV6_SRC) || type.equals(Criterion.Type.IPV6_DST)) {
Andrea Campanella04924b92018-01-17 16:34:51 +01001310 return matchIp(packet, (IPCriterion) criterion);
Andrea Campanella01e886e2017-12-15 15:27:31 +01001311 //we check that the packet contains the criterion provided by the flow rule.
Andrea Campanella04924b92018-01-17 16:34:51 +01001312 } else if (type.equals(Criterion.Type.ETH_SRC_MASKED)) {
1313 return matchMac(packet, (EthCriterion) criterion, false);
1314 } else if (type.equals(Criterion.Type.ETH_DST_MASKED)) {
1315 return matchMac(packet, (EthCriterion) criterion, true);
Andrea Campanella01e886e2017-12-15 15:27:31 +01001316 } else {
1317 return packet.criteria().contains(criterion);
1318 }
1319 });
Simon Hunt6fefd852017-11-13 17:09:43 -08001320 }
Andrea Campanella04924b92018-01-17 16:34:51 +01001321
1322 /**
1323 * Checks if the packet has an dst or src IP and if that IP matches the subnet of the ip criterion.
1324 *
1325 * @param packet the incoming packet
1326 * @param criterion the criterion to match
1327 * @return true if match
1328 */
1329 private boolean matchIp(TrafficSelector packet, IPCriterion criterion) {
1330 IPCriterion matchCriterion = (IPCriterion) packet.getCriterion(criterion.type());
1331 //if the packet does not have an IPv4 or IPv6 criterion we return true
1332 if (matchCriterion == null) {
1333 return false;
1334 }
1335 try {
1336 log.debug("Checking if {} is under {}", matchCriterion.ip(), criterion.ip());
1337 Subnet subnet = Subnet.createInstance(criterion.ip().toString());
1338 return subnet.isInSubnet(matchCriterion.ip().address().toInetAddress());
1339 } catch (UnknownHostException e) {
1340 return false;
1341 }
1342 }
1343
1344 /**
1345 * Checks if the packet has a dst or src MAC and if that Mac matches the mask of the mac criterion.
1346 *
1347 * @param packet the incoming packet
1348 * @param hitCriterion the criterion to match
1349 * @param dst true if we are checking DST MAC
1350 * @return true if match
1351 */
1352 private boolean matchMac(TrafficSelector packet, EthCriterion hitCriterion, boolean dst) {
1353 //Packet can have only one EthCriterion
1354 EthCriterion matchCriterion;
1355 if (dst) {
1356 matchCriterion = (EthCriterion) packet.criteria().stream().filter(criterion1 -> {
1357 return criterion1.type().equals(Criterion.Type.ETH_DST_MASKED) ||
1358 criterion1.type().equals(Criterion.Type.ETH_DST);
1359 }).findFirst().orElse(null);
1360 } else {
1361 matchCriterion = (EthCriterion) packet.criteria().stream().filter(criterion1 -> {
1362 return criterion1.type().equals(Criterion.Type.ETH_SRC_MASKED) ||
1363 criterion1.type().equals(Criterion.Type.ETH_SRC);
1364 }).findFirst().orElse(null);
1365 }
1366 //if the packet does not have an ETH criterion we return true
1367 if (matchCriterion == null) {
1368 return true;
1369 }
1370 log.debug("Checking if {} is under {}/{}", matchCriterion.mac(), hitCriterion.mac(), hitCriterion.mask());
1371 return compareMac(matchCriterion.mac(), hitCriterion.mac(), hitCriterion.mask());
1372 }
Simon Hunt6fefd852017-11-13 17:09:43 -08001373}