blob: 7387c5f81dba717dd391ac5d6a7a609d4bc2dadd [file] [log] [blame]
Andrea Campanella01e886e2017-12-15 15:27:31 +01001/*
2 * Copyright 2015-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.karaf.shell.commands.Command;
20import org.apache.karaf.shell.commands.Option;
Andrea Campanella01e886e2017-12-15 15:27:31 +010021import org.onlab.packet.IpAddress;
22import org.onlab.packet.MacAddress;
Andrea Campanella09ca07a2018-01-25 16:44:04 +010023import org.onlab.packet.MplsLabel;
Andrea Campanella01e886e2017-12-15 15:27:31 +010024import org.onlab.packet.TpPort;
25import org.onlab.packet.VlanId;
26import org.onosproject.cli.AbstractShellCommand;
27import org.onosproject.net.ConnectPoint;
28import org.onosproject.net.flow.DefaultTrafficSelector;
29import org.onosproject.net.flow.TrafficSelector;
30import org.onosproject.net.flow.TrafficTreatment;
31import org.onosproject.net.group.GroupBucket;
32import org.onosproject.t3.api.StaticPacketTrace;
33import org.onosproject.t3.api.TroubleshootService;
34
35import java.util.List;
36
Andrea Campanella8be1af92018-01-24 15:14:03 +010037import static org.onlab.packet.EthType.EtherType;
38
Andrea Campanella01e886e2017-12-15 15:27:31 +010039/**
40 * Starts a Static Packet Trace for a given input and prints the result.
41 */
42@Command(scope = "onos", name = "troubleshoot",
43 description = "troubleshoots flows and groups between source and destination")
44public class TroubleshootTraceCommand extends AbstractShellCommand {
45
46
47 private static final String FLOW_SHORT_FORMAT = " %s, bytes=%s, packets=%s, "
48 + "table=%s, priority=%s, selector=%s, treatment=%s";
49
50 private static final String GROUP_FORMAT =
51 " id=0x%s, state=%s, type=%s, bytes=%s, packets=%s, appId=%s, referenceCount=%s";
52 private static final String GROUP_BUCKET_FORMAT =
53 " id=0x%s, bucket=%s, bytes=%s, packets=%s, actions=%s";
54
55 @Option(name = "-v", aliases = "--verbose", description = "Outputs complete path")
56 private boolean verbosity1 = false;
57
58 @Option(name = "-vv", aliases = "--veryverbose", description = "Outputs flows and groups for every device")
59 private boolean verbosity2 = false;
60
61 @Option(name = "-s", aliases = "--srcIp", description = "Source IP")
62 String srcIp = null;
63
64 @Option(name = "-sp", aliases = "--srcPort", description = "Source Port", required = true)
65 String srcPort = null;
66
67 @Option(name = "-sm", aliases = "--srcMac", description = "Source MAC")
68 String srcMac = null;
69
70 @Option(name = "-et", aliases = "--ethType", description = "ETH Type", valueToShowInHelp = "ipv4")
71 String ethType = "ipv4";
72
73 @Option(name = "-stp", aliases = "--srcTcpPort", description = "Source TCP Port")
74 String srcTcpPort = null;
75
Andrea Campanella8be1af92018-01-24 15:14:03 +010076 @Option(name = "-d", aliases = "--dstIp", description = "Destination IP")
77 String dstIp = null;
Andrea Campanella01e886e2017-12-15 15:27:31 +010078
79 @Option(name = "-dm", aliases = "--dstMac", description = "Destination MAC")
80 String dstMac = null;
81
82 @Option(name = "-dtp", aliases = "dstTcpPort", description = "destination TCP Port")
83 String dstTcpPort = null;
84
85 @Option(name = "-vid", aliases = "--vlanId", description = "Vlan of incoming packet", valueToShowInHelp = "None")
86 String vlan = "None";
87
Andrea Campanella09ca07a2018-01-25 16:44:04 +010088 @Option(name = "-ml", aliases = "--mplsLabel", description = "Mpls label of incoming packet")
89 String mplsLabel = null;
90
Andrea Campanellad5bb2ef2018-01-31 16:43:23 +010091 @Option(name = "-mb", aliases = "--mplsBos", description = "MPLS BOS")
Andrea Campanella09ca07a2018-01-25 16:44:04 +010092 String mplsBos = null;
Andrea Campanella01e886e2017-12-15 15:27:31 +010093
Andrea Campanellad5bb2ef2018-01-31 16:43:23 +010094 @Option(name = "-ipp", aliases = "--ipProto", description = "IP Proto")
95 String ipProto = null;
96
97 @Option(name = "-udps", aliases = "--udpSrc", description = "UDP Source")
98 String udpSrc = null;
99
100 @Option(name = "-udpd", aliases = "--udpDst", description = "UDP Destination")
101 String udpDst = null;
102
Andrea Campanella01e886e2017-12-15 15:27:31 +0100103 @Override
104 protected void execute() {
105 TroubleshootService service = get(TroubleshootService.class);
106 ConnectPoint cp = ConnectPoint.deviceConnectPoint(srcPort);
Andrea Campanella8be1af92018-01-24 15:14:03 +0100107 EtherType type = EtherType.valueOf(ethType.toUpperCase());
Andrea Campanella01e886e2017-12-15 15:27:31 +0100108
109 //Input Port must be specified
110 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder()
111 .matchInPort(cp.port());
112
113 if (srcIp != null) {
Andrea Campanella8be1af92018-01-24 15:14:03 +0100114 if (type.equals(EtherType.IPV6)) {
115 selectorBuilder.matchIPv6Src(IpAddress.valueOf(srcIp).toIpPrefix());
116 } else {
117 selectorBuilder.matchIPSrc(IpAddress.valueOf(srcIp).toIpPrefix());
118 }
Andrea Campanella01e886e2017-12-15 15:27:31 +0100119 }
120
121 if (srcMac != null) {
122 selectorBuilder.matchEthSrc(MacAddress.valueOf(srcMac));
123 }
124
125 //if EthType option is not specified using IPv4
Andrea Campanella8be1af92018-01-24 15:14:03 +0100126 selectorBuilder.matchEthType(type.ethType().toShort());
Andrea Campanella01e886e2017-12-15 15:27:31 +0100127
128 if (srcTcpPort != null) {
129 selectorBuilder.matchTcpSrc(TpPort.tpPort(Integer.parseInt(srcTcpPort)));
130 }
131
Andrea Campanella8be1af92018-01-24 15:14:03 +0100132 if (dstIp != null) {
133 if (type.equals(EtherType.IPV6)) {
134 selectorBuilder.matchIPv6Dst(IpAddress.valueOf(dstIp).toIpPrefix());
135 } else {
136 selectorBuilder.matchIPDst(IpAddress.valueOf(dstIp).toIpPrefix());
137 }
138 }
Andrea Campanella01e886e2017-12-15 15:27:31 +0100139
140 if (dstMac != null) {
141 selectorBuilder.matchEthDst(MacAddress.valueOf(dstMac));
142 }
143 if (dstTcpPort != null) {
144 selectorBuilder.matchTcpDst(TpPort.tpPort(Integer.parseInt(dstTcpPort)));
145 }
146
147 //if vlan option is not specified using NONE
148 selectorBuilder.matchVlanId(VlanId.vlanId(vlan));
149
Andrea Campanella09ca07a2018-01-25 16:44:04 +0100150 if (mplsLabel != null) {
151 selectorBuilder.matchMplsLabel(MplsLabel.mplsLabel(Integer.parseInt(mplsLabel)));
152 }
153
154 if (mplsBos != null) {
155 selectorBuilder.matchMplsBos(Boolean.valueOf(mplsBos));
156 }
Andrea Campanella01e886e2017-12-15 15:27:31 +0100157
Andrea Campanellad5bb2ef2018-01-31 16:43:23 +0100158 if (ipProto != null) {
159 selectorBuilder.matchIPProtocol(Byte.valueOf(ipProto));
160 }
161
162 if (udpSrc != null) {
163 selectorBuilder.matchUdpSrc(TpPort.tpPort(Integer.parseInt(udpSrc)));
164 }
165
166 if (udpDst != null) {
167 selectorBuilder.matchUdpDst(TpPort.tpPort(Integer.parseInt(udpDst)));
168 }
169
170
Andrea Campanella01e886e2017-12-15 15:27:31 +0100171 TrafficSelector packet = selectorBuilder.build();
172
173 //Printing the created packet
174 print("Tracing packet: %s", packet.criteria());
175
176 //Build the trace
177 StaticPacketTrace trace = service.trace(packet, cp);
178
179 //Print based on verbosity
180 if (verbosity1) {
181 printTrace(trace, false);
182 } else if (verbosity2) {
183 printTrace(trace, true);
184 } else {
185 print("Paths");
186 List<List<ConnectPoint>> paths = trace.getCompletePaths();
187 paths.forEach(path -> print("%s", path));
188 }
189 print("Result: \n%s", trace.resultMessage());
190 }
191
192 //prints the trace
193 private void printTrace(StaticPacketTrace trace, boolean verbose) {
194 List<List<ConnectPoint>> paths = trace.getCompletePaths();
195 paths.forEach(path -> {
196 print("Path %s", path);
197 ConnectPoint previous = null;
198 for (ConnectPoint connectPoint : path) {
199 if (previous == null || !previous.deviceId().equals(connectPoint.deviceId())) {
200 print("Device %s", connectPoint.deviceId());
201 print("Input from %s", connectPoint);
202 printFlows(trace, verbose, connectPoint);
203 } else {
204 printGroups(trace, verbose, connectPoint);
205 print("Output through %s", connectPoint);
206 print("");
207 }
208 previous = connectPoint;
209 }
Andrea Campanella7382c7f2018-02-05 19:39:25 +0100210 print("---------------------------------------------------------------\n");
Andrea Campanella01e886e2017-12-15 15:27:31 +0100211 });
212 }
213
214 //Prints the flows for a given trace and a specified level of verbosity
215 private void printFlows(StaticPacketTrace trace, boolean verbose, ConnectPoint connectPoint) {
216 print("Flows");
217 trace.getFlowsForDevice(connectPoint.deviceId()).forEach(f -> {
218 if (verbose) {
219 print(FLOW_SHORT_FORMAT, f.state(), f.bytes(), f.packets(),
220 f.table(), f.priority(), f.selector().criteria(),
221 printTreatment(f.treatment()));
222 } else {
Andrea Campanella7382c7f2018-02-05 19:39:25 +0100223 print(" flowId=%s, table=%s, selector=%s", f.id(), f.table(), f.selector().criteria());
Andrea Campanella01e886e2017-12-15 15:27:31 +0100224 }
225 });
226 }
227
228 //Prints the groups for a given trace and a specified level of verbosity
229 private void printGroups(StaticPacketTrace trace, boolean verbose, ConnectPoint connectPoint) {
230 print("Groups");
231 trace.getGroupOuputs(connectPoint.deviceId()).forEach(output -> {
232 if (output.getOutput().equals(connectPoint)) {
233 output.getGroups().forEach(group -> {
234 if (verbose) {
235 print(GROUP_FORMAT, Integer.toHexString(group.id().id()), group.state(), group.type(),
236 group.bytes(), group.packets(), group.appId().name(), group.referenceCount());
237 int i = 0;
238 for (GroupBucket bucket : group.buckets().buckets()) {
239 print(GROUP_BUCKET_FORMAT, Integer.toHexString(group.id().id()), ++i,
240 bucket.bytes(), bucket.packets(),
241 bucket.treatment().allInstructions());
242 }
243 } else {
244 print(" groupId=%s", group.id());
245 }
246 });
247 print("Outgoing Packet %s", output.getFinalPacket());
248 }
249 });
250 }
251
252 private String printTreatment(TrafficTreatment treatment) {
253 final String delimiter = ", ";
254 StringBuilder builder = new StringBuilder("[");
255 if (!treatment.immediate().isEmpty()) {
256 builder.append("immediate=" + treatment.immediate() + delimiter);
257 }
258 if (!treatment.deferred().isEmpty()) {
259 builder.append("deferred=" + treatment.deferred() + delimiter);
260 }
261 if (treatment.clearedDeferred()) {
262 builder.append("clearDeferred" + delimiter);
263 }
264 if (treatment.tableTransition() != null) {
265 builder.append("transition=" + treatment.tableTransition() + delimiter);
266 }
267 if (treatment.metered() != null) {
268 builder.append("meter=" + treatment.metered() + delimiter);
269 }
270 if (treatment.writeMetadata() != null) {
271 builder.append("metadata=" + treatment.writeMetadata() + delimiter);
272 }
273 // Chop off last delimiter
274 builder.replace(builder.length() - delimiter.length(), builder.length(), "");
275 builder.append("]");
276 return builder.toString();
277 }
278}