Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 1 | /* |
Andrea Campanella | 55c3f42 | 2018-02-08 17:10:11 +0100 | [diff] [blame] | 2 | * Copyright 2018-present Open Networking Foundation |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 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 | |
| 17 | package org.onosproject.t3.cli; |
| 18 | |
Andrea Campanella | 1445f7a | 2018-02-07 12:00:12 +0100 | [diff] [blame] | 19 | import com.google.common.base.Preconditions; |
Ray Milkey | 822567d | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 20 | import org.apache.karaf.shell.api.action.Command; |
Seyeon Jeong | 83e7986 | 2020-02-28 01:17:34 -0800 | [diff] [blame] | 21 | import org.apache.karaf.shell.api.action.Completion; |
Ray Milkey | 822567d | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 22 | import org.apache.karaf.shell.api.action.Option; |
Ray Milkey | ac72503 | 2018-09-28 10:58:28 -0700 | [diff] [blame] | 23 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 24 | import org.onlab.packet.IpAddress; |
| 25 | import org.onlab.packet.MacAddress; |
Andrea Campanella | 3970e47 | 2018-01-25 16:44:04 +0100 | [diff] [blame] | 26 | import org.onlab.packet.MplsLabel; |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 27 | import org.onlab.packet.TpPort; |
| 28 | import org.onlab.packet.VlanId; |
| 29 | import org.onosproject.cli.AbstractShellCommand; |
Seyeon Jeong | 83e7986 | 2020-02-28 01:17:34 -0800 | [diff] [blame] | 30 | import org.onosproject.cli.PlaceholderCompleter; |
| 31 | import org.onosproject.cli.net.ConnectPointCompleter; |
| 32 | import org.onosproject.cli.net.EthTypeCompleter; |
| 33 | import org.onosproject.cli.net.IpProtocolCompleter; |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 34 | import org.onosproject.net.ConnectPoint; |
Andrea Campanella | 1445f7a | 2018-02-07 12:00:12 +0100 | [diff] [blame] | 35 | import org.onosproject.net.DeviceId; |
| 36 | import org.onosproject.net.PortNumber; |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 37 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 38 | import org.onosproject.net.flow.TrafficSelector; |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 39 | import org.onosproject.t3.api.StaticPacketTrace; |
| 40 | import org.onosproject.t3.api.TroubleshootService; |
| 41 | |
Andrea Campanella | 7db72fc | 2018-01-24 15:14:03 +0100 | [diff] [blame] | 42 | import static org.onlab.packet.EthType.EtherType; |
| 43 | |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 44 | /** |
| 45 | * Starts a Static Packet Trace for a given input and prints the result. |
| 46 | */ |
Ray Milkey | ac72503 | 2018-09-28 10:58:28 -0700 | [diff] [blame] | 47 | @Service |
Andrea Campanella | d756ede | 2018-02-26 11:03:48 +0100 | [diff] [blame] | 48 | @Command(scope = "onos", name = "t3-troubleshoot", |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 49 | description = "troubleshoots flows and groups between source and destination") |
| 50 | public 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 Campanella | 1445f7a | 2018-02-07 12:00:12 +0100 | [diff] [blame] | 61 | private static final String CONTROLLER = "CONTROLLER"; |
| 62 | |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 63 | @Option(name = "-v", aliases = "--verbose", description = "Outputs complete path") |
Seyeon Jeong | 83e7986 | 2020-02-28 01:17:34 -0800 | [diff] [blame] | 64 | @Completion(PlaceholderCompleter.class) |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 65 | private boolean verbosity1 = false; |
| 66 | |
| 67 | @Option(name = "-vv", aliases = "--veryverbose", description = "Outputs flows and groups for every device") |
Seyeon Jeong | 83e7986 | 2020-02-28 01:17:34 -0800 | [diff] [blame] | 68 | @Completion(PlaceholderCompleter.class) |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 69 | private boolean verbosity2 = false; |
| 70 | |
| 71 | @Option(name = "-s", aliases = "--srcIp", description = "Source IP") |
Seyeon Jeong | 83e7986 | 2020-02-28 01:17:34 -0800 | [diff] [blame] | 72 | @Completion(PlaceholderCompleter.class) |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 73 | String srcIp = null; |
| 74 | |
| 75 | @Option(name = "-sp", aliases = "--srcPort", description = "Source Port", required = true) |
Seyeon Jeong | 83e7986 | 2020-02-28 01:17:34 -0800 | [diff] [blame] | 76 | @Completion(ConnectPointCompleter.class) |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 77 | String srcPort = null; |
| 78 | |
| 79 | @Option(name = "-sm", aliases = "--srcMac", description = "Source MAC") |
Seyeon Jeong | 83e7986 | 2020-02-28 01:17:34 -0800 | [diff] [blame] | 80 | @Completion(PlaceholderCompleter.class) |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 81 | String srcMac = null; |
| 82 | |
| 83 | @Option(name = "-et", aliases = "--ethType", description = "ETH Type", valueToShowInHelp = "ipv4") |
Seyeon Jeong | 83e7986 | 2020-02-28 01:17:34 -0800 | [diff] [blame] | 84 | @Completion(EthTypeCompleter.class) |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 85 | String ethType = "ipv4"; |
| 86 | |
| 87 | @Option(name = "-stp", aliases = "--srcTcpPort", description = "Source TCP Port") |
Seyeon Jeong | 83e7986 | 2020-02-28 01:17:34 -0800 | [diff] [blame] | 88 | @Completion(PlaceholderCompleter.class) |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 89 | String srcTcpPort = null; |
| 90 | |
Andrea Campanella | 7db72fc | 2018-01-24 15:14:03 +0100 | [diff] [blame] | 91 | @Option(name = "-d", aliases = "--dstIp", description = "Destination IP") |
Seyeon Jeong | 83e7986 | 2020-02-28 01:17:34 -0800 | [diff] [blame] | 92 | @Completion(PlaceholderCompleter.class) |
Andrea Campanella | 7db72fc | 2018-01-24 15:14:03 +0100 | [diff] [blame] | 93 | String dstIp = null; |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 94 | |
| 95 | @Option(name = "-dm", aliases = "--dstMac", description = "Destination MAC") |
Seyeon Jeong | 83e7986 | 2020-02-28 01:17:34 -0800 | [diff] [blame] | 96 | @Completion(PlaceholderCompleter.class) |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 97 | String dstMac = null; |
| 98 | |
Andrea Campanella | 1445f7a | 2018-02-07 12:00:12 +0100 | [diff] [blame] | 99 | @Option(name = "-dtp", aliases = "--dstTcpPort", description = "destination TCP Port") |
Seyeon Jeong | 83e7986 | 2020-02-28 01:17:34 -0800 | [diff] [blame] | 100 | @Completion(PlaceholderCompleter.class) |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 101 | String dstTcpPort = null; |
| 102 | |
| 103 | @Option(name = "-vid", aliases = "--vlanId", description = "Vlan of incoming packet", valueToShowInHelp = "None") |
Seyeon Jeong | 83e7986 | 2020-02-28 01:17:34 -0800 | [diff] [blame] | 104 | @Completion(PlaceholderCompleter.class) |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 105 | String vlan = "None"; |
| 106 | |
Andrea Campanella | 3970e47 | 2018-01-25 16:44:04 +0100 | [diff] [blame] | 107 | @Option(name = "-ml", aliases = "--mplsLabel", description = "Mpls label of incoming packet") |
Seyeon Jeong | 83e7986 | 2020-02-28 01:17:34 -0800 | [diff] [blame] | 108 | @Completion(PlaceholderCompleter.class) |
Andrea Campanella | 3970e47 | 2018-01-25 16:44:04 +0100 | [diff] [blame] | 109 | String mplsLabel = null; |
| 110 | |
Andrea Campanella | 8292ba6 | 2018-01-31 16:43:23 +0100 | [diff] [blame] | 111 | @Option(name = "-mb", aliases = "--mplsBos", description = "MPLS BOS") |
Seyeon Jeong | 83e7986 | 2020-02-28 01:17:34 -0800 | [diff] [blame] | 112 | @Completion(PlaceholderCompleter.class) |
Andrea Campanella | 3970e47 | 2018-01-25 16:44:04 +0100 | [diff] [blame] | 113 | String mplsBos = null; |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 114 | |
Andrea Campanella | 8292ba6 | 2018-01-31 16:43:23 +0100 | [diff] [blame] | 115 | @Option(name = "-ipp", aliases = "--ipProto", description = "IP Proto") |
Seyeon Jeong | 83e7986 | 2020-02-28 01:17:34 -0800 | [diff] [blame] | 116 | @Completion(IpProtocolCompleter.class) |
Andrea Campanella | 8292ba6 | 2018-01-31 16:43:23 +0100 | [diff] [blame] | 117 | String ipProto = null; |
| 118 | |
| 119 | @Option(name = "-udps", aliases = "--udpSrc", description = "UDP Source") |
Seyeon Jeong | 83e7986 | 2020-02-28 01:17:34 -0800 | [diff] [blame] | 120 | @Completion(PlaceholderCompleter.class) |
Andrea Campanella | 8292ba6 | 2018-01-31 16:43:23 +0100 | [diff] [blame] | 121 | String udpSrc = null; |
| 122 | |
| 123 | @Option(name = "-udpd", aliases = "--udpDst", description = "UDP Destination") |
Seyeon Jeong | 83e7986 | 2020-02-28 01:17:34 -0800 | [diff] [blame] | 124 | @Completion(PlaceholderCompleter.class) |
Andrea Campanella | 8292ba6 | 2018-01-31 16:43:23 +0100 | [diff] [blame] | 125 | String udpDst = null; |
| 126 | |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 127 | @Override |
Ray Milkey | 822567d | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 128 | protected void doExecute() { |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 129 | TroubleshootService service = get(TroubleshootService.class); |
Seyeon Jeong | 5018bdd | 2020-02-28 01:17:34 -0800 | [diff] [blame^] | 130 | if (!service.checkNibValidity()) { |
| 131 | // if the NIB is found invalid, fill it with the current network states so that this command can proceed |
| 132 | print(T3CliUtils.NIB_AUTOFILLED); |
| 133 | TroubleshootLoadSnapshotCommand cmd = new TroubleshootLoadSnapshotCommand(); |
| 134 | cmd.doExecute(); |
| 135 | if (!service.checkNibValidity()) { |
| 136 | // if the NIB is still invalid even after auto-filled snapshots, stop and warn |
| 137 | print(T3CliUtils.NIB_TERMINATE); |
| 138 | return; |
| 139 | } |
| 140 | } else { |
| 141 | print(service.printNibSummary()); |
Seyeon Jeong | 83e7986 | 2020-02-28 01:17:34 -0800 | [diff] [blame] | 142 | } |
| 143 | |
Andrea Campanella | 1445f7a | 2018-02-07 12:00:12 +0100 | [diff] [blame] | 144 | String[] cpInfo = srcPort.split("/"); |
| 145 | Preconditions.checkArgument(cpInfo.length == 2, "wrong format of source port"); |
| 146 | ConnectPoint cp; |
| 147 | //Uses input port as a convenience to carry the Controller port, proper flood behaviour is handled in the |
| 148 | // troubleshoot manager. |
| 149 | if (cpInfo[1].equalsIgnoreCase(CONTROLLER)) { |
| 150 | cp = new ConnectPoint(DeviceId.deviceId(cpInfo[0]), PortNumber.CONTROLLER); |
| 151 | } else { |
| 152 | cp = ConnectPoint.deviceConnectPoint(srcPort); |
| 153 | } |
| 154 | |
Andrea Campanella | 7db72fc | 2018-01-24 15:14:03 +0100 | [diff] [blame] | 155 | EtherType type = EtherType.valueOf(ethType.toUpperCase()); |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 156 | |
| 157 | //Input Port must be specified |
| 158 | TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder() |
| 159 | .matchInPort(cp.port()); |
| 160 | |
| 161 | if (srcIp != null) { |
Andrea Campanella | 7db72fc | 2018-01-24 15:14:03 +0100 | [diff] [blame] | 162 | if (type.equals(EtherType.IPV6)) { |
| 163 | selectorBuilder.matchIPv6Src(IpAddress.valueOf(srcIp).toIpPrefix()); |
| 164 | } else { |
| 165 | selectorBuilder.matchIPSrc(IpAddress.valueOf(srcIp).toIpPrefix()); |
| 166 | } |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | if (srcMac != null) { |
| 170 | selectorBuilder.matchEthSrc(MacAddress.valueOf(srcMac)); |
| 171 | } |
| 172 | |
| 173 | //if EthType option is not specified using IPv4 |
Andrea Campanella | 7db72fc | 2018-01-24 15:14:03 +0100 | [diff] [blame] | 174 | selectorBuilder.matchEthType(type.ethType().toShort()); |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 175 | |
| 176 | if (srcTcpPort != null) { |
| 177 | selectorBuilder.matchTcpSrc(TpPort.tpPort(Integer.parseInt(srcTcpPort))); |
| 178 | } |
| 179 | |
Andrea Campanella | 7db72fc | 2018-01-24 15:14:03 +0100 | [diff] [blame] | 180 | if (dstIp != null) { |
| 181 | if (type.equals(EtherType.IPV6)) { |
| 182 | selectorBuilder.matchIPv6Dst(IpAddress.valueOf(dstIp).toIpPrefix()); |
| 183 | } else { |
| 184 | selectorBuilder.matchIPDst(IpAddress.valueOf(dstIp).toIpPrefix()); |
| 185 | } |
| 186 | } |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 187 | |
| 188 | if (dstMac != null) { |
| 189 | selectorBuilder.matchEthDst(MacAddress.valueOf(dstMac)); |
| 190 | } |
| 191 | if (dstTcpPort != null) { |
| 192 | selectorBuilder.matchTcpDst(TpPort.tpPort(Integer.parseInt(dstTcpPort))); |
| 193 | } |
| 194 | |
| 195 | //if vlan option is not specified using NONE |
| 196 | selectorBuilder.matchVlanId(VlanId.vlanId(vlan)); |
| 197 | |
Andrea Campanella | 3970e47 | 2018-01-25 16:44:04 +0100 | [diff] [blame] | 198 | if (mplsLabel != null) { |
| 199 | selectorBuilder.matchMplsLabel(MplsLabel.mplsLabel(Integer.parseInt(mplsLabel))); |
| 200 | } |
| 201 | |
| 202 | if (mplsBos != null) { |
| 203 | selectorBuilder.matchMplsBos(Boolean.valueOf(mplsBos)); |
| 204 | } |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 205 | |
Andrea Campanella | 8292ba6 | 2018-01-31 16:43:23 +0100 | [diff] [blame] | 206 | if (ipProto != null) { |
| 207 | selectorBuilder.matchIPProtocol(Byte.valueOf(ipProto)); |
| 208 | } |
| 209 | |
| 210 | if (udpSrc != null) { |
| 211 | selectorBuilder.matchUdpSrc(TpPort.tpPort(Integer.parseInt(udpSrc))); |
| 212 | } |
| 213 | |
| 214 | if (udpDst != null) { |
| 215 | selectorBuilder.matchUdpDst(TpPort.tpPort(Integer.parseInt(udpDst))); |
| 216 | } |
| 217 | |
| 218 | |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 219 | TrafficSelector packet = selectorBuilder.build(); |
| 220 | |
| 221 | //Printing the created packet |
| 222 | print("Tracing packet: %s", packet.criteria()); |
| 223 | |
| 224 | //Build the trace |
| 225 | StaticPacketTrace trace = service.trace(packet, cp); |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 226 | |
Andrea Campanella | 55c3f42 | 2018-02-08 17:10:11 +0100 | [diff] [blame] | 227 | print("%s", T3CliUtils.printTrace(trace, verbosity1, verbosity2)); |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 228 | |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 229 | } |
| 230 | } |