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; |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 20 | import org.apache.karaf.shell.commands.Command; |
| 21 | import org.apache.karaf.shell.commands.Option; |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 22 | import org.onlab.packet.IpAddress; |
| 23 | import org.onlab.packet.MacAddress; |
Andrea Campanella | 3970e47 | 2018-01-25 16:44:04 +0100 | [diff] [blame] | 24 | import org.onlab.packet.MplsLabel; |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 25 | import org.onlab.packet.TpPort; |
| 26 | import org.onlab.packet.VlanId; |
| 27 | import org.onosproject.cli.AbstractShellCommand; |
| 28 | import org.onosproject.net.ConnectPoint; |
Andrea Campanella | 1445f7a | 2018-02-07 12:00:12 +0100 | [diff] [blame] | 29 | import org.onosproject.net.DeviceId; |
| 30 | import org.onosproject.net.PortNumber; |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 31 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 32 | import org.onosproject.net.flow.TrafficSelector; |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 33 | import org.onosproject.t3.api.StaticPacketTrace; |
| 34 | import org.onosproject.t3.api.TroubleshootService; |
| 35 | |
Andrea Campanella | 7db72fc | 2018-01-24 15:14:03 +0100 | [diff] [blame] | 36 | import static org.onlab.packet.EthType.EtherType; |
| 37 | |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 38 | /** |
| 39 | * Starts a Static Packet Trace for a given input and prints the result. |
| 40 | */ |
Andrea Campanella | d756ede | 2018-02-26 11:03:48 +0100 | [diff] [blame] | 41 | @Command(scope = "onos", name = "t3-troubleshoot", |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 42 | description = "troubleshoots flows and groups between source and destination") |
| 43 | public class TroubleshootTraceCommand extends AbstractShellCommand { |
| 44 | |
| 45 | |
| 46 | private static final String FLOW_SHORT_FORMAT = " %s, bytes=%s, packets=%s, " |
| 47 | + "table=%s, priority=%s, selector=%s, treatment=%s"; |
| 48 | |
| 49 | private static final String GROUP_FORMAT = |
| 50 | " id=0x%s, state=%s, type=%s, bytes=%s, packets=%s, appId=%s, referenceCount=%s"; |
| 51 | private static final String GROUP_BUCKET_FORMAT = |
| 52 | " id=0x%s, bucket=%s, bytes=%s, packets=%s, actions=%s"; |
| 53 | |
Andrea Campanella | 1445f7a | 2018-02-07 12:00:12 +0100 | [diff] [blame] | 54 | private static final String CONTROLLER = "CONTROLLER"; |
| 55 | |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 56 | @Option(name = "-v", aliases = "--verbose", description = "Outputs complete path") |
| 57 | private boolean verbosity1 = false; |
| 58 | |
| 59 | @Option(name = "-vv", aliases = "--veryverbose", description = "Outputs flows and groups for every device") |
| 60 | private boolean verbosity2 = false; |
| 61 | |
| 62 | @Option(name = "-s", aliases = "--srcIp", description = "Source IP") |
| 63 | String srcIp = null; |
| 64 | |
| 65 | @Option(name = "-sp", aliases = "--srcPort", description = "Source Port", required = true) |
| 66 | String srcPort = null; |
| 67 | |
| 68 | @Option(name = "-sm", aliases = "--srcMac", description = "Source MAC") |
| 69 | String srcMac = null; |
| 70 | |
| 71 | @Option(name = "-et", aliases = "--ethType", description = "ETH Type", valueToShowInHelp = "ipv4") |
| 72 | String ethType = "ipv4"; |
| 73 | |
| 74 | @Option(name = "-stp", aliases = "--srcTcpPort", description = "Source TCP Port") |
| 75 | String srcTcpPort = null; |
| 76 | |
Andrea Campanella | 7db72fc | 2018-01-24 15:14:03 +0100 | [diff] [blame] | 77 | @Option(name = "-d", aliases = "--dstIp", description = "Destination IP") |
| 78 | String dstIp = null; |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 79 | |
| 80 | @Option(name = "-dm", aliases = "--dstMac", description = "Destination MAC") |
| 81 | String dstMac = null; |
| 82 | |
Andrea Campanella | 1445f7a | 2018-02-07 12:00:12 +0100 | [diff] [blame] | 83 | @Option(name = "-dtp", aliases = "--dstTcpPort", description = "destination TCP Port") |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 84 | String dstTcpPort = null; |
| 85 | |
| 86 | @Option(name = "-vid", aliases = "--vlanId", description = "Vlan of incoming packet", valueToShowInHelp = "None") |
| 87 | String vlan = "None"; |
| 88 | |
Andrea Campanella | 3970e47 | 2018-01-25 16:44:04 +0100 | [diff] [blame] | 89 | @Option(name = "-ml", aliases = "--mplsLabel", description = "Mpls label of incoming packet") |
| 90 | String mplsLabel = null; |
| 91 | |
Andrea Campanella | 8292ba6 | 2018-01-31 16:43:23 +0100 | [diff] [blame] | 92 | @Option(name = "-mb", aliases = "--mplsBos", description = "MPLS BOS") |
Andrea Campanella | 3970e47 | 2018-01-25 16:44:04 +0100 | [diff] [blame] | 93 | String mplsBos = null; |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 94 | |
Andrea Campanella | 8292ba6 | 2018-01-31 16:43:23 +0100 | [diff] [blame] | 95 | @Option(name = "-ipp", aliases = "--ipProto", description = "IP Proto") |
| 96 | String ipProto = null; |
| 97 | |
| 98 | @Option(name = "-udps", aliases = "--udpSrc", description = "UDP Source") |
| 99 | String udpSrc = null; |
| 100 | |
| 101 | @Option(name = "-udpd", aliases = "--udpDst", description = "UDP Destination") |
| 102 | String udpDst = null; |
| 103 | |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 104 | @Override |
| 105 | protected void execute() { |
| 106 | TroubleshootService service = get(TroubleshootService.class); |
Andrea Campanella | 1445f7a | 2018-02-07 12:00:12 +0100 | [diff] [blame] | 107 | String[] cpInfo = srcPort.split("/"); |
| 108 | Preconditions.checkArgument(cpInfo.length == 2, "wrong format of source port"); |
| 109 | ConnectPoint cp; |
| 110 | //Uses input port as a convenience to carry the Controller port, proper flood behaviour is handled in the |
| 111 | // troubleshoot manager. |
| 112 | if (cpInfo[1].equalsIgnoreCase(CONTROLLER)) { |
| 113 | cp = new ConnectPoint(DeviceId.deviceId(cpInfo[0]), PortNumber.CONTROLLER); |
| 114 | } else { |
| 115 | cp = ConnectPoint.deviceConnectPoint(srcPort); |
| 116 | } |
| 117 | |
Andrea Campanella | 7db72fc | 2018-01-24 15:14:03 +0100 | [diff] [blame] | 118 | EtherType type = EtherType.valueOf(ethType.toUpperCase()); |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 119 | |
| 120 | //Input Port must be specified |
| 121 | TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder() |
| 122 | .matchInPort(cp.port()); |
| 123 | |
| 124 | if (srcIp != null) { |
Andrea Campanella | 7db72fc | 2018-01-24 15:14:03 +0100 | [diff] [blame] | 125 | if (type.equals(EtherType.IPV6)) { |
| 126 | selectorBuilder.matchIPv6Src(IpAddress.valueOf(srcIp).toIpPrefix()); |
| 127 | } else { |
| 128 | selectorBuilder.matchIPSrc(IpAddress.valueOf(srcIp).toIpPrefix()); |
| 129 | } |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | if (srcMac != null) { |
| 133 | selectorBuilder.matchEthSrc(MacAddress.valueOf(srcMac)); |
| 134 | } |
| 135 | |
| 136 | //if EthType option is not specified using IPv4 |
Andrea Campanella | 7db72fc | 2018-01-24 15:14:03 +0100 | [diff] [blame] | 137 | selectorBuilder.matchEthType(type.ethType().toShort()); |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 138 | |
| 139 | if (srcTcpPort != null) { |
| 140 | selectorBuilder.matchTcpSrc(TpPort.tpPort(Integer.parseInt(srcTcpPort))); |
| 141 | } |
| 142 | |
Andrea Campanella | 7db72fc | 2018-01-24 15:14:03 +0100 | [diff] [blame] | 143 | if (dstIp != null) { |
| 144 | if (type.equals(EtherType.IPV6)) { |
| 145 | selectorBuilder.matchIPv6Dst(IpAddress.valueOf(dstIp).toIpPrefix()); |
| 146 | } else { |
| 147 | selectorBuilder.matchIPDst(IpAddress.valueOf(dstIp).toIpPrefix()); |
| 148 | } |
| 149 | } |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 150 | |
| 151 | if (dstMac != null) { |
| 152 | selectorBuilder.matchEthDst(MacAddress.valueOf(dstMac)); |
| 153 | } |
| 154 | if (dstTcpPort != null) { |
| 155 | selectorBuilder.matchTcpDst(TpPort.tpPort(Integer.parseInt(dstTcpPort))); |
| 156 | } |
| 157 | |
| 158 | //if vlan option is not specified using NONE |
| 159 | selectorBuilder.matchVlanId(VlanId.vlanId(vlan)); |
| 160 | |
Andrea Campanella | 3970e47 | 2018-01-25 16:44:04 +0100 | [diff] [blame] | 161 | if (mplsLabel != null) { |
| 162 | selectorBuilder.matchMplsLabel(MplsLabel.mplsLabel(Integer.parseInt(mplsLabel))); |
| 163 | } |
| 164 | |
| 165 | if (mplsBos != null) { |
| 166 | selectorBuilder.matchMplsBos(Boolean.valueOf(mplsBos)); |
| 167 | } |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 168 | |
Andrea Campanella | 8292ba6 | 2018-01-31 16:43:23 +0100 | [diff] [blame] | 169 | if (ipProto != null) { |
| 170 | selectorBuilder.matchIPProtocol(Byte.valueOf(ipProto)); |
| 171 | } |
| 172 | |
| 173 | if (udpSrc != null) { |
| 174 | selectorBuilder.matchUdpSrc(TpPort.tpPort(Integer.parseInt(udpSrc))); |
| 175 | } |
| 176 | |
| 177 | if (udpDst != null) { |
| 178 | selectorBuilder.matchUdpDst(TpPort.tpPort(Integer.parseInt(udpDst))); |
| 179 | } |
| 180 | |
| 181 | |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 182 | TrafficSelector packet = selectorBuilder.build(); |
| 183 | |
| 184 | //Printing the created packet |
| 185 | print("Tracing packet: %s", packet.criteria()); |
| 186 | |
| 187 | //Build the trace |
| 188 | StaticPacketTrace trace = service.trace(packet, cp); |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 189 | |
Andrea Campanella | 55c3f42 | 2018-02-08 17:10:11 +0100 | [diff] [blame] | 190 | print("%s", T3CliUtils.printTrace(trace, verbosity1, verbosity2)); |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 191 | |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 192 | } |
| 193 | } |