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