blob: 3e7003fe36fbf0cdd9f06a2ffeea5c9b53e6a9a4 [file] [log] [blame]
Andrea Campanella41de8062018-02-28 16:43:16 +01001/*
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
17package org.onosproject.t3.cli;
18
19import org.apache.commons.lang.StringUtils;
Ray Milkey86ad7bb2018-09-27 12:32:28 -070020import org.apache.karaf.shell.api.action.Command;
Ray Milkeyebe0ec62018-10-10 14:32:38 -070021import org.apache.karaf.shell.api.action.Completion;
Ray Milkey86ad7bb2018-09-27 12:32:28 -070022import org.apache.karaf.shell.api.action.Option;
Ray Milkey7a2dee52018-09-28 10:58:28 -070023import org.apache.karaf.shell.api.action.lifecycle.Service;
Andrea Campanella41de8062018-02-28 16:43:16 +010024import org.onlab.packet.IpAddress;
25import org.onosproject.cli.AbstractShellCommand;
Seyeon Jeong357bcec2020-02-28 01:17:34 -080026import org.onosproject.cli.PlaceholderCompleter;
Ray Milkeyebe0ec62018-10-10 14:32:38 -070027import org.onosproject.cli.net.EthTypeCompleter;
Andrea Campanella41de8062018-02-28 16:43:16 +010028import org.onosproject.net.Host;
29import org.onosproject.net.flow.criteria.Criterion;
30import org.onosproject.net.flow.criteria.IPCriterion;
31import org.onosproject.t3.api.StaticPacketTrace;
32import org.onosproject.t3.api.TroubleshootService;
33import org.onosproject.t3.impl.Generator;
34
Andrea Campanellac8e6a502018-03-12 19:25:44 -070035import java.util.ArrayList;
36import java.util.List;
Andrea Campanella41de8062018-02-28 16:43:16 +010037import java.util.Set;
38
39import static java.lang.Thread.sleep;
40import static org.onlab.packet.EthType.EtherType;
41
42/**
43 * Starts a Static Packet Trace for a given input and prints the result.
44 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070045@Service
Andrea Campanella41de8062018-02-28 16:43:16 +010046@Command(scope = "onos", name = "t3-troubleshoot-pingall",
47 description = "Traces a ping between all hosts in the system of a given ETH type")
48public 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 Milkeyebe0ec62018-10-10 14:32:38 -070054 @Completion(EthTypeCompleter.class)
Andrea Campanella41de8062018-02-28 16:43:16 +010055 String ethType = "ipv4";
56
57 @Option(name = "-v", aliases = "--verbose", description = "Outputs trace for each host to host combination")
Seyeon Jeong357bcec2020-02-28 01:17:34 -080058 @Completion(PlaceholderCompleter.class)
Andrea Campanella41de8062018-02-28 16:43:16 +010059 private boolean verbosity1 = false;
60
61 @Option(name = "-vv", aliases = "--veryverbose", description = "Outputs details of every trace")
Seyeon Jeong357bcec2020-02-28 01:17:34 -080062 @Completion(PlaceholderCompleter.class)
Andrea Campanella41de8062018-02-28 16:43:16 +010063 private boolean verbosity2 = false;
64
65 @Option(name = "-d", aliases = "--delay", description = "delay between host to host trace display")
Seyeon Jeong357bcec2020-02-28 01:17:34 -080066 @Completion(PlaceholderCompleter.class)
Andrea Campanella41de8062018-02-28 16:43:16 +010067 private long delay = 0;
68
69 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070070 protected void doExecute() {
Andrea Campanella41de8062018-02-28 16:43:16 +010071 TroubleshootService service = get(TroubleshootService.class);
Seyeon Jeong357bcec2020-02-28 01:17:34 -080072 if (service.checkNibsUnavailable()) {
73 print(TroubleshootLoadFileCommand.ERROR_NULL);
74 return;
75 }
Andrea Campanella41de8062018-02-28 16:43:16 +010076
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 Campanellac8e6a502018-03-12 19:25:44 -070087 int totalTraces = 0;
88 List<StaticPacketTrace> failedTraces = new ArrayList<>();
89 boolean ipv4 = type.equals(EtherType.IPV4);
Andrea Campanella41de8062018-02-28 16:43:16 +010090 while (generator.iterator().hasNext()) {
91 Set<StaticPacketTrace> traces = generator.iterator().next();
Andrea Campanellac8e6a502018-03-12 19:25:44 -070092 totalTraces++;
Andrea Campanella41de8062018-02-28 16:43:16 +010093 for (StaticPacketTrace trace : traces) {
Andrea Campanella41de8062018-02-28 16:43:16 +010094 //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 Campanellac8e6a502018-03-12 19:25:44 -0700100 previousHost = printSrc(trace, ipv4, src);
Andrea Campanella41de8062018-02-28 16:43:16 +0100101 }
Andrea Campanellac8e6a502018-03-12 19:25:44 -0700102 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 Campanella41de8062018-02-28 16:43:16 +0100108 }
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 Campanellac8e6a502018-03-12 19:25:44 -0700135 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 Campanella41de8062018-02-28 16:43:16 +0100148 }
149 }
150
Andrea Campanellac8e6a502018-03-12 19:25:44 -0700151 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 Campanella41de8062018-02-28 16:43:16 +0100177 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;
pier4c2715e2019-04-11 18:20:01 +0200181 if (trace.getInitialPacket() != null && trace.getInitialPacket().getCriterion(type) != null) {
Andrea Campanella41de8062018-02-28 16:43:16 +0100182 ipAddress = ((IPCriterion) trace.getInitialPacket()
183 .getCriterion(type)).ip().address();
184 } else {
185 ipAddress = host.ipAddresses().stream().filter(IpAddress::isIp4)
Seyeon Jeonga7a6f4a2020-02-19 17:51:05 -0800186 .findAny().orElse(null);
Andrea Campanella41de8062018-02-28 16:43:16 +0100187 }
188 } else {
189 Criterion.Type type = src ? Criterion.Type.IPV6_SRC : Criterion.Type.IPV6_DST;
pier4c2715e2019-04-11 18:20:01 +0200190 if (trace.getInitialPacket() != null && trace.getInitialPacket().getCriterion(type) != null) {
Andrea Campanella41de8062018-02-28 16:43:16 +0100191 ipAddress = ((IPCriterion) trace.getInitialPacket()
192 .getCriterion(type)).ip().address();
193 } else {
194 ipAddress = host.ipAddresses().stream().filter(IpAddress::isIp6)
Seyeon Jeonga7a6f4a2020-02-19 17:51:05 -0800195 .findAny().orElse(null);
Andrea Campanella41de8062018-02-28 16:43:16 +0100196 }
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}