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