blob: c809065ce43f0d62796c01a92241051e16168875 [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 Campanella01e886e2017-12-15 15:27:31 +010091 @Option(name = "-mb", aliases = "--mplsBos", description = "MPLS BOS", valueToShowInHelp = "True")
Andrea Campanella09ca07a2018-01-25 16:44:04 +010092 String mplsBos = null;
Andrea Campanella01e886e2017-12-15 15:27:31 +010093
94 @Override
95 protected void execute() {
96 TroubleshootService service = get(TroubleshootService.class);
97 ConnectPoint cp = ConnectPoint.deviceConnectPoint(srcPort);
Andrea Campanella8be1af92018-01-24 15:14:03 +010098 EtherType type = EtherType.valueOf(ethType.toUpperCase());
Andrea Campanella01e886e2017-12-15 15:27:31 +010099
100 //Input Port must be specified
101 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder()
102 .matchInPort(cp.port());
103
104 if (srcIp != null) {
Andrea Campanella8be1af92018-01-24 15:14:03 +0100105 if (type.equals(EtherType.IPV6)) {
106 selectorBuilder.matchIPv6Src(IpAddress.valueOf(srcIp).toIpPrefix());
107 } else {
108 selectorBuilder.matchIPSrc(IpAddress.valueOf(srcIp).toIpPrefix());
109 }
Andrea Campanella01e886e2017-12-15 15:27:31 +0100110 }
111
112 if (srcMac != null) {
113 selectorBuilder.matchEthSrc(MacAddress.valueOf(srcMac));
114 }
115
116 //if EthType option is not specified using IPv4
Andrea Campanella8be1af92018-01-24 15:14:03 +0100117 selectorBuilder.matchEthType(type.ethType().toShort());
Andrea Campanella01e886e2017-12-15 15:27:31 +0100118
119 if (srcTcpPort != null) {
120 selectorBuilder.matchTcpSrc(TpPort.tpPort(Integer.parseInt(srcTcpPort)));
121 }
122
Andrea Campanella8be1af92018-01-24 15:14:03 +0100123 if (dstIp != null) {
124 if (type.equals(EtherType.IPV6)) {
125 selectorBuilder.matchIPv6Dst(IpAddress.valueOf(dstIp).toIpPrefix());
126 } else {
127 selectorBuilder.matchIPDst(IpAddress.valueOf(dstIp).toIpPrefix());
128 }
129 }
Andrea Campanella01e886e2017-12-15 15:27:31 +0100130
131 if (dstMac != null) {
132 selectorBuilder.matchEthDst(MacAddress.valueOf(dstMac));
133 }
134 if (dstTcpPort != null) {
135 selectorBuilder.matchTcpDst(TpPort.tpPort(Integer.parseInt(dstTcpPort)));
136 }
137
138 //if vlan option is not specified using NONE
139 selectorBuilder.matchVlanId(VlanId.vlanId(vlan));
140
Andrea Campanella09ca07a2018-01-25 16:44:04 +0100141 if (mplsLabel != null) {
142 selectorBuilder.matchMplsLabel(MplsLabel.mplsLabel(Integer.parseInt(mplsLabel)));
143 }
144
145 if (mplsBos != null) {
146 selectorBuilder.matchMplsBos(Boolean.valueOf(mplsBos));
147 }
Andrea Campanella01e886e2017-12-15 15:27:31 +0100148
149 TrafficSelector packet = selectorBuilder.build();
150
151 //Printing the created packet
152 print("Tracing packet: %s", packet.criteria());
153
154 //Build the trace
155 StaticPacketTrace trace = service.trace(packet, cp);
156
157 //Print based on verbosity
158 if (verbosity1) {
159 printTrace(trace, false);
160 } else if (verbosity2) {
161 printTrace(trace, true);
162 } else {
163 print("Paths");
164 List<List<ConnectPoint>> paths = trace.getCompletePaths();
165 paths.forEach(path -> print("%s", path));
166 }
167 print("Result: \n%s", trace.resultMessage());
168 }
169
170 //prints the trace
171 private void printTrace(StaticPacketTrace trace, boolean verbose) {
172 List<List<ConnectPoint>> paths = trace.getCompletePaths();
173 paths.forEach(path -> {
174 print("Path %s", path);
175 ConnectPoint previous = null;
176 for (ConnectPoint connectPoint : path) {
177 if (previous == null || !previous.deviceId().equals(connectPoint.deviceId())) {
178 print("Device %s", connectPoint.deviceId());
179 print("Input from %s", connectPoint);
180 printFlows(trace, verbose, connectPoint);
181 } else {
182 printGroups(trace, verbose, connectPoint);
183 print("Output through %s", connectPoint);
184 print("");
185 }
186 previous = connectPoint;
187 }
188 });
189 }
190
191 //Prints the flows for a given trace and a specified level of verbosity
192 private void printFlows(StaticPacketTrace trace, boolean verbose, ConnectPoint connectPoint) {
193 print("Flows");
194 trace.getFlowsForDevice(connectPoint.deviceId()).forEach(f -> {
195 if (verbose) {
196 print(FLOW_SHORT_FORMAT, f.state(), f.bytes(), f.packets(),
197 f.table(), f.priority(), f.selector().criteria(),
198 printTreatment(f.treatment()));
199 } else {
200 print(" flowId=%s, selector=%s ", f.id(), f.selector().criteria());
201 }
202 });
203 }
204
205 //Prints the groups for a given trace and a specified level of verbosity
206 private void printGroups(StaticPacketTrace trace, boolean verbose, ConnectPoint connectPoint) {
207 print("Groups");
208 trace.getGroupOuputs(connectPoint.deviceId()).forEach(output -> {
209 if (output.getOutput().equals(connectPoint)) {
210 output.getGroups().forEach(group -> {
211 if (verbose) {
212 print(GROUP_FORMAT, Integer.toHexString(group.id().id()), group.state(), group.type(),
213 group.bytes(), group.packets(), group.appId().name(), group.referenceCount());
214 int i = 0;
215 for (GroupBucket bucket : group.buckets().buckets()) {
216 print(GROUP_BUCKET_FORMAT, Integer.toHexString(group.id().id()), ++i,
217 bucket.bytes(), bucket.packets(),
218 bucket.treatment().allInstructions());
219 }
220 } else {
221 print(" groupId=%s", group.id());
222 }
223 });
224 print("Outgoing Packet %s", output.getFinalPacket());
225 }
226 });
227 }
228
229 private String printTreatment(TrafficTreatment treatment) {
230 final String delimiter = ", ";
231 StringBuilder builder = new StringBuilder("[");
232 if (!treatment.immediate().isEmpty()) {
233 builder.append("immediate=" + treatment.immediate() + delimiter);
234 }
235 if (!treatment.deferred().isEmpty()) {
236 builder.append("deferred=" + treatment.deferred() + delimiter);
237 }
238 if (treatment.clearedDeferred()) {
239 builder.append("clearDeferred" + delimiter);
240 }
241 if (treatment.tableTransition() != null) {
242 builder.append("transition=" + treatment.tableTransition() + delimiter);
243 }
244 if (treatment.metered() != null) {
245 builder.append("meter=" + treatment.metered() + delimiter);
246 }
247 if (treatment.writeMetadata() != null) {
248 builder.append("metadata=" + treatment.writeMetadata() + delimiter);
249 }
250 // Chop off last delimiter
251 builder.replace(builder.length() - delimiter.length(), builder.length(), "");
252 builder.append("]");
253 return builder.toString();
254 }
255}