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