Andrea Campanella | 41de806 | 2018-02-28 16:43:16 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-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.cli; |
| 18 | |
| 19 | import org.apache.commons.lang.StringUtils; |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 20 | import org.apache.karaf.shell.api.action.Command; |
Ray Milkey | ebe0ec6 | 2018-10-10 14:32:38 -0700 | [diff] [blame] | 21 | import org.apache.karaf.shell.api.action.Completion; |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 22 | import org.apache.karaf.shell.api.action.Option; |
Ray Milkey | 7a2dee5 | 2018-09-28 10:58:28 -0700 | [diff] [blame] | 23 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
Andrea Campanella | 41de806 | 2018-02-28 16:43:16 +0100 | [diff] [blame] | 24 | import org.onlab.packet.IpAddress; |
| 25 | import org.onosproject.cli.AbstractShellCommand; |
Seyeon Jeong | 8d3cad2 | 2020-02-28 01:17:34 -0800 | [diff] [blame^] | 26 | import org.onosproject.cli.PlaceholderCompleter; |
Ray Milkey | ebe0ec6 | 2018-10-10 14:32:38 -0700 | [diff] [blame] | 27 | import org.onosproject.cli.net.EthTypeCompleter; |
Andrea Campanella | 41de806 | 2018-02-28 16:43:16 +0100 | [diff] [blame] | 28 | import org.onosproject.net.Host; |
| 29 | import org.onosproject.net.flow.criteria.Criterion; |
| 30 | import org.onosproject.net.flow.criteria.IPCriterion; |
| 31 | import org.onosproject.t3.api.StaticPacketTrace; |
| 32 | import org.onosproject.t3.api.TroubleshootService; |
| 33 | import org.onosproject.t3.impl.Generator; |
| 34 | |
Andrea Campanella | c8e6a50 | 2018-03-12 19:25:44 -0700 | [diff] [blame] | 35 | import java.util.ArrayList; |
| 36 | import java.util.List; |
Andrea Campanella | 41de806 | 2018-02-28 16:43:16 +0100 | [diff] [blame] | 37 | import java.util.Set; |
| 38 | |
| 39 | import static java.lang.Thread.sleep; |
| 40 | import static org.onlab.packet.EthType.EtherType; |
| 41 | |
| 42 | /** |
| 43 | * Starts a Static Packet Trace for a given input and prints the result. |
| 44 | */ |
Ray Milkey | 7a2dee5 | 2018-09-28 10:58:28 -0700 | [diff] [blame] | 45 | @Service |
Andrea Campanella | 41de806 | 2018-02-28 16:43:16 +0100 | [diff] [blame] | 46 | @Command(scope = "onos", name = "t3-troubleshoot-pingall", |
| 47 | description = "Traces a ping between all hosts in the system of a given ETH type") |
| 48 | public class TroubleshootPingAllCommand extends AbstractShellCommand { |
| 49 | |
| 50 | private static final String FMT_SHORT = |
| 51 | "id=%s, mac=%s, locations=%s, vlan=%s, ip(s)=%s"; |
| 52 | |
| 53 | @Option(name = "-et", aliases = "--ethType", description = "ETH Type", valueToShowInHelp = "ipv4") |
Ray Milkey | ebe0ec6 | 2018-10-10 14:32:38 -0700 | [diff] [blame] | 54 | @Completion(EthTypeCompleter.class) |
Andrea Campanella | 41de806 | 2018-02-28 16:43:16 +0100 | [diff] [blame] | 55 | String ethType = "ipv4"; |
| 56 | |
| 57 | @Option(name = "-v", aliases = "--verbose", description = "Outputs trace for each host to host combination") |
Seyeon Jeong | 8d3cad2 | 2020-02-28 01:17:34 -0800 | [diff] [blame^] | 58 | @Completion(PlaceholderCompleter.class) |
Andrea Campanella | 41de806 | 2018-02-28 16:43:16 +0100 | [diff] [blame] | 59 | private boolean verbosity1 = false; |
| 60 | |
| 61 | @Option(name = "-vv", aliases = "--veryverbose", description = "Outputs details of every trace") |
Seyeon Jeong | 8d3cad2 | 2020-02-28 01:17:34 -0800 | [diff] [blame^] | 62 | @Completion(PlaceholderCompleter.class) |
Andrea Campanella | 41de806 | 2018-02-28 16:43:16 +0100 | [diff] [blame] | 63 | private boolean verbosity2 = false; |
| 64 | |
| 65 | @Option(name = "-d", aliases = "--delay", description = "delay between host to host trace display") |
Seyeon Jeong | 8d3cad2 | 2020-02-28 01:17:34 -0800 | [diff] [blame^] | 66 | @Completion(PlaceholderCompleter.class) |
Andrea Campanella | 41de806 | 2018-02-28 16:43:16 +0100 | [diff] [blame] | 67 | private long delay = 0; |
| 68 | |
| 69 | @Override |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 70 | protected void doExecute() { |
Andrea Campanella | 41de806 | 2018-02-28 16:43:16 +0100 | [diff] [blame] | 71 | TroubleshootService service = get(TroubleshootService.class); |
Seyeon Jeong | 8d3cad2 | 2020-02-28 01:17:34 -0800 | [diff] [blame^] | 72 | if (service.checkNibsUnavailable()) { |
| 73 | print(TroubleshootLoadFileCommand.ERROR_NULL); |
| 74 | return; |
| 75 | } |
Andrea Campanella | 41de806 | 2018-02-28 16:43:16 +0100 | [diff] [blame] | 76 | |
| 77 | EtherType type = EtherType.valueOf(ethType.toUpperCase()); |
| 78 | |
| 79 | print("Tracing between all %s hosts", ethType); |
| 80 | |
| 81 | if (!type.equals(EtherType.IPV4) && !type.equals(EtherType.IPV6)) { |
| 82 | print("Command only support IPv4 or IPv6"); |
| 83 | } else { |
| 84 | //Create the generator for the list of traces. |
| 85 | Generator<Set<StaticPacketTrace>> generator = service.pingAllGenerator(type); |
| 86 | Host previousHost = null; |
Andrea Campanella | c8e6a50 | 2018-03-12 19:25:44 -0700 | [diff] [blame] | 87 | int totalTraces = 0; |
| 88 | List<StaticPacketTrace> failedTraces = new ArrayList<>(); |
| 89 | boolean ipv4 = type.equals(EtherType.IPV4); |
Andrea Campanella | 41de806 | 2018-02-28 16:43:16 +0100 | [diff] [blame] | 90 | while (generator.iterator().hasNext()) { |
| 91 | Set<StaticPacketTrace> traces = generator.iterator().next(); |
Andrea Campanella | c8e6a50 | 2018-03-12 19:25:44 -0700 | [diff] [blame] | 92 | totalTraces++; |
Andrea Campanella | 41de806 | 2018-02-28 16:43:16 +0100 | [diff] [blame] | 93 | for (StaticPacketTrace trace : traces) { |
Andrea Campanella | 41de806 | 2018-02-28 16:43:16 +0100 | [diff] [blame] | 94 | //no verbosity is mininet style output |
| 95 | if (!verbosity1 && !verbosity2) { |
| 96 | if (trace.getEndpointHosts().isPresent()) { |
| 97 | Host src = trace.getEndpointHosts().get().getLeft(); |
| 98 | if (previousHost == null || !previousHost.equals(src)) { |
| 99 | print("%s", StringUtils.rightPad("", 125, '-')); |
Andrea Campanella | c8e6a50 | 2018-03-12 19:25:44 -0700 | [diff] [blame] | 100 | previousHost = printSrc(trace, ipv4, src); |
Andrea Campanella | 41de806 | 2018-02-28 16:43:16 +0100 | [diff] [blame] | 101 | } |
Andrea Campanella | c8e6a50 | 2018-03-12 19:25:44 -0700 | [diff] [blame] | 102 | String host = getDstString(trace, ipv4, src); |
| 103 | if (!trace.isSuccess()) { |
| 104 | host = host + " " + trace.resultMessage(); |
| 105 | failedTraces.add(trace); |
| 106 | } |
| 107 | print("%s", host); |
Andrea Campanella | 41de806 | 2018-02-28 16:43:16 +0100 | [diff] [blame] | 108 | } |
| 109 | } else { |
| 110 | print("%s", StringUtils.leftPad("", 125, '-')); |
| 111 | |
| 112 | if (trace.getInitialPacket() != null) { |
| 113 | if (verbosity1) { |
| 114 | printResultOnly(trace, ipv4); |
| 115 | } else if (verbosity2) { |
| 116 | printVerbose(trace); |
| 117 | } |
| 118 | } else { |
| 119 | if (trace.getEndpointHosts().isPresent()) { |
| 120 | Host source = trace.getEndpointHosts().get().getLeft(); |
| 121 | Host destination = trace.getEndpointHosts().get().getRight(); |
| 122 | print("Source %s --> Destination %s", source.id(), destination.id()); |
| 123 | } |
| 124 | print("Error in obtaining trace: %s", trace.resultMessage()); |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | try { |
| 129 | sleep(delay); |
| 130 | } catch (InterruptedException e) { |
| 131 | log.debug("interrupted while sleep"); |
| 132 | } |
| 133 | } |
| 134 | print("%s", StringUtils.rightPad("", 125, '-')); |
Andrea Campanella | c8e6a50 | 2018-03-12 19:25:44 -0700 | [diff] [blame] | 135 | print("Failed Traces: %s", failedTraces.size()); |
| 136 | print("%s", StringUtils.rightPad("", 125, '-')); |
| 137 | failedTraces.forEach(t -> { |
| 138 | if (t.getEndpointHosts().isPresent()) { |
| 139 | printSrc(t, ipv4, t.getEndpointHosts().get().getLeft()); |
| 140 | String dst = getDstString(t, ipv4, t.getEndpointHosts().get().getRight()); |
| 141 | dst = dst + " " + t.resultMessage(); |
| 142 | print("%s", dst); |
| 143 | print("%s", StringUtils.rightPad("", 125, '-')); |
| 144 | } |
| 145 | }); |
| 146 | print("Summary"); |
| 147 | print("Total Traces %s, errors %s", totalTraces, failedTraces.size()); |
Andrea Campanella | 41de806 | 2018-02-28 16:43:16 +0100 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | |
Andrea Campanella | c8e6a50 | 2018-03-12 19:25:44 -0700 | [diff] [blame] | 151 | private String getDstString(StaticPacketTrace trace, boolean ipv4, Host src) { |
| 152 | String host; |
| 153 | IpAddress ipAddress = getIpAddress(trace, ipv4, src, false); |
| 154 | if (ipAddress == null) { |
| 155 | host = String.format(" %s %s", trace.getEndpointHosts().get().getRight().id(), |
| 156 | trace.isSuccess()); |
| 157 | } else { |
| 158 | host = String.format(" %s (%s) %s", |
| 159 | trace.getEndpointHosts().get().getRight().id(), ipAddress, |
| 160 | trace.isSuccess()); |
| 161 | } |
| 162 | return host; |
| 163 | } |
| 164 | |
| 165 | private Host printSrc(StaticPacketTrace trace, boolean ipv4, Host src) { |
| 166 | Host previousHost; |
| 167 | IpAddress ipAddress = getIpAddress(trace, ipv4, src, true); |
| 168 | if (ipAddress == null) { |
| 169 | print("%s", src.id() + " -->"); |
| 170 | } else { |
| 171 | print("%s (%s) -->", src.id(), ipAddress); |
| 172 | } |
| 173 | previousHost = src; |
| 174 | return previousHost; |
| 175 | } |
| 176 | |
Andrea Campanella | 41de806 | 2018-02-28 16:43:16 +0100 | [diff] [blame] | 177 | private IpAddress getIpAddress(StaticPacketTrace trace, boolean ipv4, Host host, boolean src) { |
| 178 | IpAddress ipAddress; |
| 179 | if (ipv4) { |
| 180 | Criterion.Type type = src ? Criterion.Type.IPV4_SRC : Criterion.Type.IPV4_DST; |
pier | 4c2715e | 2019-04-11 18:20:01 +0200 | [diff] [blame] | 181 | if (trace.getInitialPacket() != null && trace.getInitialPacket().getCriterion(type) != null) { |
Andrea Campanella | 41de806 | 2018-02-28 16:43:16 +0100 | [diff] [blame] | 182 | ipAddress = ((IPCriterion) trace.getInitialPacket() |
| 183 | .getCriterion(type)).ip().address(); |
| 184 | } else { |
| 185 | ipAddress = host.ipAddresses().stream().filter(IpAddress::isIp4) |
Seyeon Jeong | 25669c6 | 2020-02-19 17:51:05 -0800 | [diff] [blame] | 186 | .findAny().orElse(null); |
Andrea Campanella | 41de806 | 2018-02-28 16:43:16 +0100 | [diff] [blame] | 187 | } |
| 188 | } else { |
| 189 | Criterion.Type type = src ? Criterion.Type.IPV6_SRC : Criterion.Type.IPV6_DST; |
pier | 4c2715e | 2019-04-11 18:20:01 +0200 | [diff] [blame] | 190 | if (trace.getInitialPacket() != null && trace.getInitialPacket().getCriterion(type) != null) { |
Andrea Campanella | 41de806 | 2018-02-28 16:43:16 +0100 | [diff] [blame] | 191 | ipAddress = ((IPCriterion) trace.getInitialPacket() |
| 192 | .getCriterion(type)).ip().address(); |
| 193 | } else { |
| 194 | ipAddress = host.ipAddresses().stream().filter(IpAddress::isIp6) |
Seyeon Jeong | 25669c6 | 2020-02-19 17:51:05 -0800 | [diff] [blame] | 195 | .findAny().orElse(null); |
Andrea Campanella | 41de806 | 2018-02-28 16:43:16 +0100 | [diff] [blame] | 196 | } |
| 197 | } |
| 198 | return ipAddress; |
| 199 | } |
| 200 | |
| 201 | private void printResultOnly(StaticPacketTrace trace, boolean ipv4) { |
| 202 | if (trace.getEndpointHosts().isPresent()) { |
| 203 | Host source = trace.getEndpointHosts().get().getLeft(); |
| 204 | Host destination = trace.getEndpointHosts().get().getRight(); |
| 205 | IpAddress srcIP; |
| 206 | IpAddress dstIP; |
| 207 | if (ipv4 && trace.getInitialPacket().getCriterion(Criterion.Type.IPV4_SRC) != null) { |
| 208 | srcIP = ((IPCriterion) trace.getInitialPacket().getCriterion(Criterion.Type.IPV4_SRC)).ip().address(); |
| 209 | dstIP = ((IPCriterion) trace.getInitialPacket().getCriterion(Criterion.Type.IPV4_DST)).ip().address(); |
| 210 | print("Source %s (%s) --> Destination %s (%s)", source.id(), srcIP, destination.id(), dstIP); |
| 211 | } else if (trace.getInitialPacket().getCriterion(Criterion.Type.IPV6_SRC) != null) { |
| 212 | srcIP = ((IPCriterion) trace.getInitialPacket().getCriterion(Criterion.Type.IPV6_SRC)).ip().address(); |
| 213 | dstIP = ((IPCriterion) trace.getInitialPacket().getCriterion(Criterion.Type.IPV6_DST)).ip().address(); |
| 214 | print("Source %s (%s) --> Destination %s (%s)", source.id(), srcIP, destination.id(), dstIP); |
| 215 | } else { |
| 216 | print("Source %s --> Destination %s", source.id(), destination.id()); |
| 217 | } |
| 218 | print("%s", trace.resultMessage()); |
| 219 | } else { |
| 220 | print("Can't gather host information from trace"); |
| 221 | print("%s", trace.resultMessage()); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | private void printVerbose(StaticPacketTrace trace) { |
| 226 | if (trace.getEndpointHosts().isPresent()) { |
| 227 | Host source = trace.getEndpointHosts().get().getLeft(); |
| 228 | print("Source host %s", printHost(source)); |
| 229 | Host destination = trace.getEndpointHosts().get().getRight(); |
| 230 | print("Destination host %s", printHost(destination)); |
| 231 | } |
| 232 | print("%s", trace.getInitialPacket()); |
| 233 | print("%s", T3CliUtils.printTrace(trace, false, false)); |
| 234 | } |
| 235 | |
| 236 | private String printHost(Host host) { |
| 237 | return String.format(FMT_SHORT, host.id(), host.mac(), |
| 238 | host.locations(), |
| 239 | host.vlan(), host.ipAddresses()); |
| 240 | } |
| 241 | } |