blob: 5da3f8ccc639222aeb24ad0394cbc1f62c2e7c81 [file] [log] [blame]
Andrea Campanella01e886e2017-12-15 15:27:31 +01001/*
Andrea Campanellaaf34b7c2018-02-08 17:10:11 +01002 * Copyright 2018-present Open Networking Foundation
Andrea Campanella01e886e2017-12-15 15:27:31 +01003 *
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
Andrea Campanella6f2d6742018-02-07 12:00:12 +010019import com.google.common.base.Preconditions;
Ray Milkey86ad7bb2018-09-27 12:32:28 -070020import org.apache.karaf.shell.api.action.Command;
Seyeon Jeong357bcec2020-02-28 01:17:34 -080021import org.apache.karaf.shell.api.action.Completion;
Ray Milkey86ad7bb2018-09-27 12:32:28 -070022import org.apache.karaf.shell.api.action.Option;
Ray Milkey7a2dee52018-09-28 10:58:28 -070023import org.apache.karaf.shell.api.action.lifecycle.Service;
Andrea Campanella01e886e2017-12-15 15:27:31 +010024import org.onlab.packet.IpAddress;
25import org.onlab.packet.MacAddress;
Andrea Campanella09ca07a2018-01-25 16:44:04 +010026import org.onlab.packet.MplsLabel;
Andrea Campanella01e886e2017-12-15 15:27:31 +010027import org.onlab.packet.TpPort;
28import org.onlab.packet.VlanId;
29import org.onosproject.cli.AbstractShellCommand;
Seyeon Jeong357bcec2020-02-28 01:17:34 -080030import org.onosproject.cli.PlaceholderCompleter;
31import org.onosproject.cli.net.ConnectPointCompleter;
32import org.onosproject.cli.net.EthTypeCompleter;
33import org.onosproject.cli.net.IpProtocolCompleter;
Andrea Campanella01e886e2017-12-15 15:27:31 +010034import org.onosproject.net.ConnectPoint;
Andrea Campanella6f2d6742018-02-07 12:00:12 +010035import org.onosproject.net.DeviceId;
36import org.onosproject.net.PortNumber;
Andrea Campanella01e886e2017-12-15 15:27:31 +010037import org.onosproject.net.flow.DefaultTrafficSelector;
38import org.onosproject.net.flow.TrafficSelector;
Andrea Campanella01e886e2017-12-15 15:27:31 +010039import org.onosproject.t3.api.StaticPacketTrace;
40import org.onosproject.t3.api.TroubleshootService;
41
Andrea Campanella8be1af92018-01-24 15:14:03 +010042import static org.onlab.packet.EthType.EtherType;
43
Andrea Campanella01e886e2017-12-15 15:27:31 +010044/**
45 * Starts a Static Packet Trace for a given input and prints the result.
46 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070047@Service
Andrea Campanella36769e22018-02-26 11:03:48 +010048@Command(scope = "onos", name = "t3-troubleshoot",
Andrea Campanella01e886e2017-12-15 15:27:31 +010049 description = "troubleshoots flows and groups between source and destination")
50public class TroubleshootTraceCommand extends AbstractShellCommand {
51
52
53 private static final String FLOW_SHORT_FORMAT = " %s, bytes=%s, packets=%s, "
54 + "table=%s, priority=%s, selector=%s, treatment=%s";
55
56 private static final String GROUP_FORMAT =
57 " id=0x%s, state=%s, type=%s, bytes=%s, packets=%s, appId=%s, referenceCount=%s";
58 private static final String GROUP_BUCKET_FORMAT =
59 " id=0x%s, bucket=%s, bytes=%s, packets=%s, actions=%s";
60
Andrea Campanella6f2d6742018-02-07 12:00:12 +010061 private static final String CONTROLLER = "CONTROLLER";
62
Andrea Campanella01e886e2017-12-15 15:27:31 +010063 @Option(name = "-v", aliases = "--verbose", description = "Outputs complete path")
Seyeon Jeong357bcec2020-02-28 01:17:34 -080064 @Completion(PlaceholderCompleter.class)
Andrea Campanella01e886e2017-12-15 15:27:31 +010065 private boolean verbosity1 = false;
66
67 @Option(name = "-vv", aliases = "--veryverbose", description = "Outputs flows and groups for every device")
Seyeon Jeong357bcec2020-02-28 01:17:34 -080068 @Completion(PlaceholderCompleter.class)
Andrea Campanella01e886e2017-12-15 15:27:31 +010069 private boolean verbosity2 = false;
70
71 @Option(name = "-s", aliases = "--srcIp", description = "Source IP")
Seyeon Jeong357bcec2020-02-28 01:17:34 -080072 @Completion(PlaceholderCompleter.class)
Andrea Campanella01e886e2017-12-15 15:27:31 +010073 String srcIp = null;
74
75 @Option(name = "-sp", aliases = "--srcPort", description = "Source Port", required = true)
Seyeon Jeong357bcec2020-02-28 01:17:34 -080076 @Completion(ConnectPointCompleter.class)
Andrea Campanella01e886e2017-12-15 15:27:31 +010077 String srcPort = null;
78
79 @Option(name = "-sm", aliases = "--srcMac", description = "Source MAC")
Seyeon Jeong357bcec2020-02-28 01:17:34 -080080 @Completion(PlaceholderCompleter.class)
Andrea Campanella01e886e2017-12-15 15:27:31 +010081 String srcMac = null;
82
83 @Option(name = "-et", aliases = "--ethType", description = "ETH Type", valueToShowInHelp = "ipv4")
Seyeon Jeong357bcec2020-02-28 01:17:34 -080084 @Completion(EthTypeCompleter.class)
Andrea Campanella01e886e2017-12-15 15:27:31 +010085 String ethType = "ipv4";
86
87 @Option(name = "-stp", aliases = "--srcTcpPort", description = "Source TCP Port")
Seyeon Jeong357bcec2020-02-28 01:17:34 -080088 @Completion(PlaceholderCompleter.class)
Andrea Campanella01e886e2017-12-15 15:27:31 +010089 String srcTcpPort = null;
90
Andrea Campanella8be1af92018-01-24 15:14:03 +010091 @Option(name = "-d", aliases = "--dstIp", description = "Destination IP")
Seyeon Jeong357bcec2020-02-28 01:17:34 -080092 @Completion(PlaceholderCompleter.class)
Andrea Campanella8be1af92018-01-24 15:14:03 +010093 String dstIp = null;
Andrea Campanella01e886e2017-12-15 15:27:31 +010094
95 @Option(name = "-dm", aliases = "--dstMac", description = "Destination MAC")
Seyeon Jeong357bcec2020-02-28 01:17:34 -080096 @Completion(PlaceholderCompleter.class)
Andrea Campanella01e886e2017-12-15 15:27:31 +010097 String dstMac = null;
98
Andrea Campanella6f2d6742018-02-07 12:00:12 +010099 @Option(name = "-dtp", aliases = "--dstTcpPort", description = "destination TCP Port")
Seyeon Jeong357bcec2020-02-28 01:17:34 -0800100 @Completion(PlaceholderCompleter.class)
Andrea Campanella01e886e2017-12-15 15:27:31 +0100101 String dstTcpPort = null;
102
103 @Option(name = "-vid", aliases = "--vlanId", description = "Vlan of incoming packet", valueToShowInHelp = "None")
Seyeon Jeong357bcec2020-02-28 01:17:34 -0800104 @Completion(PlaceholderCompleter.class)
Andrea Campanella01e886e2017-12-15 15:27:31 +0100105 String vlan = "None";
106
Andrea Campanella09ca07a2018-01-25 16:44:04 +0100107 @Option(name = "-ml", aliases = "--mplsLabel", description = "Mpls label of incoming packet")
Seyeon Jeong357bcec2020-02-28 01:17:34 -0800108 @Completion(PlaceholderCompleter.class)
Andrea Campanella09ca07a2018-01-25 16:44:04 +0100109 String mplsLabel = null;
110
Andrea Campanellad5bb2ef2018-01-31 16:43:23 +0100111 @Option(name = "-mb", aliases = "--mplsBos", description = "MPLS BOS")
Seyeon Jeong357bcec2020-02-28 01:17:34 -0800112 @Completion(PlaceholderCompleter.class)
Andrea Campanella09ca07a2018-01-25 16:44:04 +0100113 String mplsBos = null;
Andrea Campanella01e886e2017-12-15 15:27:31 +0100114
Andrea Campanellad5bb2ef2018-01-31 16:43:23 +0100115 @Option(name = "-ipp", aliases = "--ipProto", description = "IP Proto")
Seyeon Jeong357bcec2020-02-28 01:17:34 -0800116 @Completion(IpProtocolCompleter.class)
Andrea Campanellad5bb2ef2018-01-31 16:43:23 +0100117 String ipProto = null;
118
119 @Option(name = "-udps", aliases = "--udpSrc", description = "UDP Source")
Seyeon Jeong357bcec2020-02-28 01:17:34 -0800120 @Completion(PlaceholderCompleter.class)
Andrea Campanellad5bb2ef2018-01-31 16:43:23 +0100121 String udpSrc = null;
122
123 @Option(name = "-udpd", aliases = "--udpDst", description = "UDP Destination")
Seyeon Jeong357bcec2020-02-28 01:17:34 -0800124 @Completion(PlaceholderCompleter.class)
Andrea Campanellad5bb2ef2018-01-31 16:43:23 +0100125 String udpDst = null;
126
Andrea Campanella01e886e2017-12-15 15:27:31 +0100127 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -0700128 protected void doExecute() {
Andrea Campanella01e886e2017-12-15 15:27:31 +0100129 TroubleshootService service = get(TroubleshootService.class);
Seyeon Jeong357bcec2020-02-28 01:17:34 -0800130 if (service.checkNibsUnavailable()) {
131 print(TroubleshootLoadFileCommand.ERROR_NULL);
132 return;
133 }
134
Andrea Campanella6f2d6742018-02-07 12:00:12 +0100135 String[] cpInfo = srcPort.split("/");
136 Preconditions.checkArgument(cpInfo.length == 2, "wrong format of source port");
137 ConnectPoint cp;
138 //Uses input port as a convenience to carry the Controller port, proper flood behaviour is handled in the
139 // troubleshoot manager.
140 if (cpInfo[1].equalsIgnoreCase(CONTROLLER)) {
141 cp = new ConnectPoint(DeviceId.deviceId(cpInfo[0]), PortNumber.CONTROLLER);
142 } else {
143 cp = ConnectPoint.deviceConnectPoint(srcPort);
144 }
145
Andrea Campanella8be1af92018-01-24 15:14:03 +0100146 EtherType type = EtherType.valueOf(ethType.toUpperCase());
Andrea Campanella01e886e2017-12-15 15:27:31 +0100147
148 //Input Port must be specified
149 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder()
150 .matchInPort(cp.port());
151
152 if (srcIp != null) {
Andrea Campanella8be1af92018-01-24 15:14:03 +0100153 if (type.equals(EtherType.IPV6)) {
154 selectorBuilder.matchIPv6Src(IpAddress.valueOf(srcIp).toIpPrefix());
155 } else {
156 selectorBuilder.matchIPSrc(IpAddress.valueOf(srcIp).toIpPrefix());
157 }
Andrea Campanella01e886e2017-12-15 15:27:31 +0100158 }
159
160 if (srcMac != null) {
161 selectorBuilder.matchEthSrc(MacAddress.valueOf(srcMac));
162 }
163
164 //if EthType option is not specified using IPv4
Andrea Campanella8be1af92018-01-24 15:14:03 +0100165 selectorBuilder.matchEthType(type.ethType().toShort());
Andrea Campanella01e886e2017-12-15 15:27:31 +0100166
167 if (srcTcpPort != null) {
168 selectorBuilder.matchTcpSrc(TpPort.tpPort(Integer.parseInt(srcTcpPort)));
169 }
170
Andrea Campanella8be1af92018-01-24 15:14:03 +0100171 if (dstIp != null) {
172 if (type.equals(EtherType.IPV6)) {
173 selectorBuilder.matchIPv6Dst(IpAddress.valueOf(dstIp).toIpPrefix());
174 } else {
175 selectorBuilder.matchIPDst(IpAddress.valueOf(dstIp).toIpPrefix());
176 }
177 }
Andrea Campanella01e886e2017-12-15 15:27:31 +0100178
179 if (dstMac != null) {
180 selectorBuilder.matchEthDst(MacAddress.valueOf(dstMac));
181 }
182 if (dstTcpPort != null) {
183 selectorBuilder.matchTcpDst(TpPort.tpPort(Integer.parseInt(dstTcpPort)));
184 }
185
186 //if vlan option is not specified using NONE
187 selectorBuilder.matchVlanId(VlanId.vlanId(vlan));
188
Andrea Campanella09ca07a2018-01-25 16:44:04 +0100189 if (mplsLabel != null) {
190 selectorBuilder.matchMplsLabel(MplsLabel.mplsLabel(Integer.parseInt(mplsLabel)));
191 }
192
193 if (mplsBos != null) {
194 selectorBuilder.matchMplsBos(Boolean.valueOf(mplsBos));
195 }
Andrea Campanella01e886e2017-12-15 15:27:31 +0100196
Andrea Campanellad5bb2ef2018-01-31 16:43:23 +0100197 if (ipProto != null) {
198 selectorBuilder.matchIPProtocol(Byte.valueOf(ipProto));
199 }
200
201 if (udpSrc != null) {
202 selectorBuilder.matchUdpSrc(TpPort.tpPort(Integer.parseInt(udpSrc)));
203 }
204
205 if (udpDst != null) {
206 selectorBuilder.matchUdpDst(TpPort.tpPort(Integer.parseInt(udpDst)));
207 }
208
209
Andrea Campanella01e886e2017-12-15 15:27:31 +0100210 TrafficSelector packet = selectorBuilder.build();
211
212 //Printing the created packet
213 print("Tracing packet: %s", packet.criteria());
214
215 //Build the trace
216 StaticPacketTrace trace = service.trace(packet, cp);
Andrea Campanella01e886e2017-12-15 15:27:31 +0100217
Andrea Campanellaaf34b7c2018-02-08 17:10:11 +0100218 print("%s", T3CliUtils.printTrace(trace, verbosity1, verbosity2));
Andrea Campanella01e886e2017-12-15 15:27:31 +0100219
Andrea Campanella01e886e2017-12-15 15:27:31 +0100220 }
221}