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