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