blob: 40f747f7d55c6e16885c1b7dcf132fe6db5c96d8 [file] [log] [blame]
Andrea Campanella0cc6acd2018-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 Milkey822567d2018-09-27 12:32:28 -070020import org.apache.karaf.shell.api.action.Command;
Ray Milkeyba9c4f52018-10-10 14:32:38 -070021import org.apache.karaf.shell.api.action.Completion;
Ray Milkey822567d2018-09-27 12:32:28 -070022import org.apache.karaf.shell.api.action.Option;
Ray Milkeyac725032018-09-28 10:58:28 -070023import org.apache.karaf.shell.api.action.lifecycle.Service;
Andrea Campanella0cc6acd2018-02-28 16:43:16 +010024import org.onlab.packet.IpAddress;
pierventrefe57fda2020-08-04 22:52:02 +020025import org.onlab.util.Generator;
Andrea Campanella0cc6acd2018-02-28 16:43:16 +010026import org.onosproject.cli.AbstractShellCommand;
Seyeon Jeong83e79862020-02-28 01:17:34 -080027import org.onosproject.cli.PlaceholderCompleter;
Ray Milkeyba9c4f52018-10-10 14:32:38 -070028import org.onosproject.cli.net.EthTypeCompleter;
Andrea Campanella0cc6acd2018-02-28 16:43:16 +010029import org.onosproject.net.Host;
30import org.onosproject.net.flow.criteria.Criterion;
31import org.onosproject.net.flow.criteria.IPCriterion;
32import org.onosproject.t3.api.StaticPacketTrace;
33import org.onosproject.t3.api.TroubleshootService;
Andrea Campanella0cc6acd2018-02-28 16:43:16 +010034
Andrea Campanella5af2d2b2018-03-12 19:25:44 -070035import java.util.ArrayList;
36import java.util.List;
Andrea Campanella0cc6acd2018-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 Milkeyac725032018-09-28 10:58:28 -070045@Service
Andrea Campanella0cc6acd2018-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 Milkeyba9c4f52018-10-10 14:32:38 -070054 @Completion(EthTypeCompleter.class)
Andrea Campanella0cc6acd2018-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 Jeong83e79862020-02-28 01:17:34 -080058 @Completion(PlaceholderCompleter.class)
Andrea Campanella0cc6acd2018-02-28 16:43:16 +010059 private boolean verbosity1 = false;
60
61 @Option(name = "-vv", aliases = "--veryverbose", description = "Outputs details of every trace")
Seyeon Jeong83e79862020-02-28 01:17:34 -080062 @Completion(PlaceholderCompleter.class)
Andrea Campanella0cc6acd2018-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 Jeong83e79862020-02-28 01:17:34 -080066 @Completion(PlaceholderCompleter.class)
Andrea Campanella0cc6acd2018-02-28 16:43:16 +010067 private long delay = 0;
68
69 @Override
Ray Milkey822567d2018-09-27 12:32:28 -070070 protected void doExecute() {
Andrea Campanella0cc6acd2018-02-28 16:43:16 +010071 TroubleshootService service = get(TroubleshootService.class);
Seyeon Jeong5018bdd2020-02-28 01:17:34 -080072 if (!service.checkNibValidity()) {
73 // if the NIB is found invalid, fill it with the current network states so that this command can proceed
74 print(T3CliUtils.NIB_AUTOFILLED);
75 TroubleshootLoadSnapshotCommand cmd = new TroubleshootLoadSnapshotCommand();
76 cmd.doExecute();
77 if (!service.checkNibValidity()) {
78 // if the NIB is still invalid even after auto-filled snapshots, stop and warn
79 print(T3CliUtils.NIB_TERMINATE);
80 return;
81 }
82 } else {
83 print(service.printNibSummary());
Seyeon Jeong83e79862020-02-28 01:17:34 -080084 }
Andrea Campanella0cc6acd2018-02-28 16:43:16 +010085
86 EtherType type = EtherType.valueOf(ethType.toUpperCase());
87
88 print("Tracing between all %s hosts", ethType);
89
90 if (!type.equals(EtherType.IPV4) && !type.equals(EtherType.IPV6)) {
91 print("Command only support IPv4 or IPv6");
92 } else {
93 //Create the generator for the list of traces.
94 Generator<Set<StaticPacketTrace>> generator = service.pingAllGenerator(type);
95 Host previousHost = null;
Andrea Campanella5af2d2b2018-03-12 19:25:44 -070096 int totalTraces = 0;
97 List<StaticPacketTrace> failedTraces = new ArrayList<>();
98 boolean ipv4 = type.equals(EtherType.IPV4);
Andrea Campanella0cc6acd2018-02-28 16:43:16 +010099 while (generator.iterator().hasNext()) {
100 Set<StaticPacketTrace> traces = generator.iterator().next();
Andrea Campanella5af2d2b2018-03-12 19:25:44 -0700101 totalTraces++;
Andrea Campanella0cc6acd2018-02-28 16:43:16 +0100102 for (StaticPacketTrace trace : traces) {
Andrea Campanella0cc6acd2018-02-28 16:43:16 +0100103 //no verbosity is mininet style output
104 if (!verbosity1 && !verbosity2) {
105 if (trace.getEndpointHosts().isPresent()) {
106 Host src = trace.getEndpointHosts().get().getLeft();
107 if (previousHost == null || !previousHost.equals(src)) {
108 print("%s", StringUtils.rightPad("", 125, '-'));
Andrea Campanella5af2d2b2018-03-12 19:25:44 -0700109 previousHost = printSrc(trace, ipv4, src);
Andrea Campanella0cc6acd2018-02-28 16:43:16 +0100110 }
Andrea Campanella5af2d2b2018-03-12 19:25:44 -0700111 String host = getDstString(trace, ipv4, src);
112 if (!trace.isSuccess()) {
113 host = host + " " + trace.resultMessage();
114 failedTraces.add(trace);
115 }
116 print("%s", host);
Andrea Campanella0cc6acd2018-02-28 16:43:16 +0100117 }
118 } else {
119 print("%s", StringUtils.leftPad("", 125, '-'));
120
121 if (trace.getInitialPacket() != null) {
122 if (verbosity1) {
123 printResultOnly(trace, ipv4);
124 } else if (verbosity2) {
125 printVerbose(trace);
126 }
127 } else {
128 if (trace.getEndpointHosts().isPresent()) {
129 Host source = trace.getEndpointHosts().get().getLeft();
130 Host destination = trace.getEndpointHosts().get().getRight();
131 print("Source %s --> Destination %s", source.id(), destination.id());
132 }
133 print("Error in obtaining trace: %s", trace.resultMessage());
134 }
135 }
136 }
137 try {
138 sleep(delay);
139 } catch (InterruptedException e) {
140 log.debug("interrupted while sleep");
141 }
142 }
143 print("%s", StringUtils.rightPad("", 125, '-'));
Andrea Campanella5af2d2b2018-03-12 19:25:44 -0700144 print("Failed Traces: %s", failedTraces.size());
145 print("%s", StringUtils.rightPad("", 125, '-'));
146 failedTraces.forEach(t -> {
147 if (t.getEndpointHosts().isPresent()) {
148 printSrc(t, ipv4, t.getEndpointHosts().get().getLeft());
149 String dst = getDstString(t, ipv4, t.getEndpointHosts().get().getRight());
150 dst = dst + " " + t.resultMessage();
151 print("%s", dst);
152 print("%s", StringUtils.rightPad("", 125, '-'));
153 }
154 });
155 print("Summary");
156 print("Total Traces %s, errors %s", totalTraces, failedTraces.size());
Andrea Campanella0cc6acd2018-02-28 16:43:16 +0100157 }
158 }
159
Andrea Campanella5af2d2b2018-03-12 19:25:44 -0700160 private String getDstString(StaticPacketTrace trace, boolean ipv4, Host src) {
161 String host;
162 IpAddress ipAddress = getIpAddress(trace, ipv4, src, false);
163 if (ipAddress == null) {
164 host = String.format(" %s %s", trace.getEndpointHosts().get().getRight().id(),
165 trace.isSuccess());
166 } else {
167 host = String.format(" %s (%s) %s",
168 trace.getEndpointHosts().get().getRight().id(), ipAddress,
169 trace.isSuccess());
170 }
171 return host;
172 }
173
174 private Host printSrc(StaticPacketTrace trace, boolean ipv4, Host src) {
175 Host previousHost;
176 IpAddress ipAddress = getIpAddress(trace, ipv4, src, true);
177 if (ipAddress == null) {
178 print("%s", src.id() + " -->");
179 } else {
180 print("%s (%s) -->", src.id(), ipAddress);
181 }
182 previousHost = src;
183 return previousHost;
184 }
185
Andrea Campanella0cc6acd2018-02-28 16:43:16 +0100186 private IpAddress getIpAddress(StaticPacketTrace trace, boolean ipv4, Host host, boolean src) {
187 IpAddress ipAddress;
188 if (ipv4) {
189 Criterion.Type type = src ? Criterion.Type.IPV4_SRC : Criterion.Type.IPV4_DST;
pier49106d52019-04-11 18:20:01 +0200190 if (trace.getInitialPacket() != null && trace.getInitialPacket().getCriterion(type) != null) {
Andrea Campanella0cc6acd2018-02-28 16:43:16 +0100191 ipAddress = ((IPCriterion) trace.getInitialPacket()
192 .getCriterion(type)).ip().address();
193 } else {
194 ipAddress = host.ipAddresses().stream().filter(IpAddress::isIp4)
Seyeon Jeong9e89ff62020-02-19 17:51:05 -0800195 .findAny().orElse(null);
Andrea Campanella0cc6acd2018-02-28 16:43:16 +0100196 }
197 } else {
198 Criterion.Type type = src ? Criterion.Type.IPV6_SRC : Criterion.Type.IPV6_DST;
pier49106d52019-04-11 18:20:01 +0200199 if (trace.getInitialPacket() != null && trace.getInitialPacket().getCriterion(type) != null) {
Andrea Campanella0cc6acd2018-02-28 16:43:16 +0100200 ipAddress = ((IPCriterion) trace.getInitialPacket()
201 .getCriterion(type)).ip().address();
202 } else {
203 ipAddress = host.ipAddresses().stream().filter(IpAddress::isIp6)
Seyeon Jeong9e89ff62020-02-19 17:51:05 -0800204 .findAny().orElse(null);
Andrea Campanella0cc6acd2018-02-28 16:43:16 +0100205 }
206 }
207 return ipAddress;
208 }
209
210 private void printResultOnly(StaticPacketTrace trace, boolean ipv4) {
211 if (trace.getEndpointHosts().isPresent()) {
212 Host source = trace.getEndpointHosts().get().getLeft();
213 Host destination = trace.getEndpointHosts().get().getRight();
214 IpAddress srcIP;
215 IpAddress dstIP;
216 if (ipv4 && trace.getInitialPacket().getCriterion(Criterion.Type.IPV4_SRC) != null) {
217 srcIP = ((IPCriterion) trace.getInitialPacket().getCriterion(Criterion.Type.IPV4_SRC)).ip().address();
218 dstIP = ((IPCriterion) trace.getInitialPacket().getCriterion(Criterion.Type.IPV4_DST)).ip().address();
219 print("Source %s (%s) --> Destination %s (%s)", source.id(), srcIP, destination.id(), dstIP);
220 } else if (trace.getInitialPacket().getCriterion(Criterion.Type.IPV6_SRC) != null) {
221 srcIP = ((IPCriterion) trace.getInitialPacket().getCriterion(Criterion.Type.IPV6_SRC)).ip().address();
222 dstIP = ((IPCriterion) trace.getInitialPacket().getCriterion(Criterion.Type.IPV6_DST)).ip().address();
223 print("Source %s (%s) --> Destination %s (%s)", source.id(), srcIP, destination.id(), dstIP);
224 } else {
225 print("Source %s --> Destination %s", source.id(), destination.id());
226 }
227 print("%s", trace.resultMessage());
228 } else {
229 print("Can't gather host information from trace");
230 print("%s", trace.resultMessage());
231 }
232 }
233
234 private void printVerbose(StaticPacketTrace trace) {
235 if (trace.getEndpointHosts().isPresent()) {
236 Host source = trace.getEndpointHosts().get().getLeft();
237 print("Source host %s", printHost(source));
238 Host destination = trace.getEndpointHosts().get().getRight();
239 print("Destination host %s", printHost(destination));
240 }
241 print("%s", trace.getInitialPacket());
242 print("%s", T3CliUtils.printTrace(trace, false, false));
243 }
244
245 private String printHost(Host host) {
246 return String.format(FMT_SHORT, host.id(), host.mac(),
247 host.locations(),
248 host.vlan(), host.ipAddresses());
249 }
250}