Jian Li | 0b93b00 | 2018-07-31 13:41:08 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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 | package org.onosproject.openstacktroubleshoot.cli; |
| 17 | |
Jian Li | e189c1c | 2018-08-08 15:55:08 +0900 | [diff] [blame] | 18 | import com.google.common.collect.Sets; |
| 19 | import org.apache.karaf.shell.commands.Argument; |
Jian Li | 0b93b00 | 2018-07-31 13:41:08 +0900 | [diff] [blame] | 20 | import org.apache.karaf.shell.commands.Command; |
Jian Li | e189c1c | 2018-08-08 15:55:08 +0900 | [diff] [blame] | 21 | import org.apache.karaf.shell.commands.Option; |
| 22 | import org.onlab.packet.IpAddress; |
Jian Li | 0b93b00 | 2018-07-31 13:41:08 +0900 | [diff] [blame] | 23 | import org.onosproject.cli.AbstractShellCommand; |
Jian Li | 43d0428 | 2018-08-22 14:31:21 +0900 | [diff] [blame] | 24 | import org.onosproject.cluster.ClusterService; |
| 25 | import org.onosproject.cluster.NodeId; |
| 26 | import org.onosproject.mastership.MastershipService; |
Jian Li | e189c1c | 2018-08-08 15:55:08 +0900 | [diff] [blame] | 27 | import org.onosproject.openstacknetworking.api.InstancePort; |
| 28 | import org.onosproject.openstacknetworking.api.InstancePortService; |
Jian Li | 43d0428 | 2018-08-22 14:31:21 +0900 | [diff] [blame] | 29 | import org.onosproject.openstacknode.api.OpenstackNode; |
| 30 | import org.onosproject.openstacknode.api.OpenstackNodeService; |
Jian Li | 0b93b00 | 2018-07-31 13:41:08 +0900 | [diff] [blame] | 31 | import org.onosproject.openstacktroubleshoot.api.OpenstackTroubleshootService; |
| 32 | import org.onosproject.openstacktroubleshoot.api.Reachability; |
| 33 | |
Jian Li | e189c1c | 2018-08-08 15:55:08 +0900 | [diff] [blame] | 34 | import java.util.Optional; |
| 35 | import java.util.Set; |
Jian Li | 9171c00 | 2018-08-19 22:58:40 +0900 | [diff] [blame] | 36 | import java.util.concurrent.ExecutorService; |
Jian Li | e189c1c | 2018-08-08 15:55:08 +0900 | [diff] [blame] | 37 | import java.util.stream.Collectors; |
Jian Li | 0b93b00 | 2018-07-31 13:41:08 +0900 | [diff] [blame] | 38 | |
Jian Li | 9171c00 | 2018-08-19 22:58:40 +0900 | [diff] [blame] | 39 | import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor; |
| 40 | import static org.onlab.util.Tools.groupedThreads; |
Jian Li | c38e903 | 2018-08-09 17:08:38 +0900 | [diff] [blame] | 41 | import static org.onosproject.openstacknetworking.api.InstancePort.State.ACTIVE; |
Jian Li | 43d0428 | 2018-08-22 14:31:21 +0900 | [diff] [blame] | 42 | import static org.onosproject.openstacknode.api.OpenstackNode.NodeType.COMPUTE; |
Jian Li | c38e903 | 2018-08-09 17:08:38 +0900 | [diff] [blame] | 43 | |
Jian Li | 0b93b00 | 2018-07-31 13:41:08 +0900 | [diff] [blame] | 44 | /** |
| 45 | * Checks the east-west VMs connectivity. |
| 46 | */ |
| 47 | @Command(scope = "onos", name = "openstack-check-east-west", |
| 48 | description = "Checks the east-west VMs connectivity") |
| 49 | public class OpenstackEastWestProbeCommand extends AbstractShellCommand { |
| 50 | |
| 51 | private static final String REACHABLE = "Reachable :)"; |
| 52 | private static final String UNREACHABLE = "Unreachable :("; |
| 53 | private static final String ARROW = "->"; |
| 54 | |
| 55 | private static final String FORMAT = "%-20s%-5s%-20s%-20s"; |
| 56 | |
Jian Li | e189c1c | 2018-08-08 15:55:08 +0900 | [diff] [blame] | 57 | @Option(name = "-a", aliases = "--all", description = "Apply this command to all VMs", |
| 58 | required = false, multiValued = false) |
| 59 | private boolean isAll = false; |
| 60 | |
| 61 | @Argument(index = 0, name = "vmIps", description = "VMs' IP addresses", |
| 62 | required = false, multiValued = true) |
| 63 | private String[] vmIps = null; |
| 64 | |
Jian Li | 9171c00 | 2018-08-19 22:58:40 +0900 | [diff] [blame] | 65 | private final ExecutorService probeExecutor = newSingleThreadScheduledExecutor( |
| 66 | groupedThreads(this.getClass().getSimpleName(), "probe-handler", log)); |
| 67 | |
Jian Li | 0b93b00 | 2018-07-31 13:41:08 +0900 | [diff] [blame] | 68 | @Override |
| 69 | protected void execute() { |
Jian Li | 43d0428 | 2018-08-22 14:31:21 +0900 | [diff] [blame] | 70 | OpenstackTroubleshootService tsService = get(OpenstackTroubleshootService.class); |
Jian Li | c38e903 | 2018-08-09 17:08:38 +0900 | [diff] [blame] | 71 | InstancePortService instPortService = get(InstancePortService.class); |
Jian Li | 43d0428 | 2018-08-22 14:31:21 +0900 | [diff] [blame] | 72 | MastershipService mastershipService = get(MastershipService.class); |
| 73 | ClusterService clusterService = get(ClusterService.class); |
| 74 | OpenstackNodeService osNodeService = get(OpenstackNodeService.class); |
Jian Li | e189c1c | 2018-08-08 15:55:08 +0900 | [diff] [blame] | 75 | |
| 76 | if (tsService == null) { |
Jian Li | 0b93b00 | 2018-07-31 13:41:08 +0900 | [diff] [blame] | 77 | error("Failed to troubleshoot openstack networking."); |
| 78 | return; |
| 79 | } |
| 80 | |
Jian Li | e189c1c | 2018-08-08 15:55:08 +0900 | [diff] [blame] | 81 | if ((!isAll && vmIps == null) || (isAll && vmIps != null)) { |
| 82 | print("Please specify one of VM IP address or -a option."); |
| 83 | return; |
| 84 | } |
| 85 | |
Jian Li | 43d0428 | 2018-08-22 14:31:21 +0900 | [diff] [blame] | 86 | NodeId localNodeId = clusterService.getLocalNode().id(); |
| 87 | |
| 88 | for (OpenstackNode node : osNodeService.completeNodes(COMPUTE)) { |
| 89 | if (!localNodeId.equals(mastershipService.getMasterFor(node.intgBridge()))) { |
| 90 | error("Current node is not the master for all compute nodes. " + |
| 91 | "Please enforce mastership first using openstack-reset-mastership -c !"); |
| 92 | return; |
| 93 | } |
| 94 | } |
| 95 | |
Jian Li | e189c1c | 2018-08-08 15:55:08 +0900 | [diff] [blame] | 96 | if (isAll) { |
| 97 | printHeader(); |
Jian Li | c38e903 | 2018-08-09 17:08:38 +0900 | [diff] [blame] | 98 | // send ICMP PACKET_OUT to all connect VMs whose instance port state is ACTIVE |
| 99 | Set<InstancePort> activePorts = instPortService.instancePorts().stream() |
| 100 | .filter(p -> p.state() == ACTIVE) |
| 101 | .collect(Collectors.toSet()); |
| 102 | |
| 103 | activePorts.forEach(srcPort -> |
| 104 | activePorts.forEach(dstPort -> |
| 105 | printReachability(tsService.probeEastWest(srcPort, dstPort)) |
| 106 | ) |
| 107 | ); |
Jian Li | e189c1c | 2018-08-08 15:55:08 +0900 | [diff] [blame] | 108 | } else { |
| 109 | if (vmIps.length > 2) { |
| 110 | print("Too many VM IPs. The number of IP should be limited to 2."); |
| 111 | return; |
| 112 | } |
| 113 | |
Jian Li | c38e903 | 2018-08-09 17:08:38 +0900 | [diff] [blame] | 114 | IpAddress srcIp = getIpAddress(vmIps[0]); |
| 115 | |
| 116 | if (srcIp == null) { |
| 117 | return; |
| 118 | } |
| 119 | |
Jian Li | e189c1c | 2018-08-08 15:55:08 +0900 | [diff] [blame] | 120 | InstancePort srcPort = instPort(instPortService, srcIp); |
| 121 | |
| 122 | if (srcPort == null) { |
| 123 | print("Specified source IP is not existing."); |
| 124 | return; |
| 125 | } |
| 126 | |
| 127 | final Set<IpAddress> dstIps = Sets.newConcurrentHashSet(); |
| 128 | |
| 129 | if (vmIps.length == 2) { |
Jian Li | c38e903 | 2018-08-09 17:08:38 +0900 | [diff] [blame] | 130 | IpAddress dstIp = getIpAddress(vmIps[1]); |
| 131 | |
| 132 | if (dstIp == null) { |
| 133 | return; |
| 134 | } |
| 135 | |
| 136 | dstIps.add(dstIp); |
Jian Li | e189c1c | 2018-08-08 15:55:08 +0900 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | if (vmIps.length == 1) { |
| 140 | dstIps.addAll(instPortService.instancePorts().stream() |
| 141 | .filter(p -> !p.ipAddress().equals(srcIp)) |
| 142 | .filter(p -> p.state().equals(InstancePort.State.ACTIVE)) |
| 143 | .map(InstancePort::ipAddress) |
| 144 | .collect(Collectors.toSet())); |
| 145 | } |
| 146 | |
| 147 | printHeader(); |
| 148 | dstIps.stream() |
| 149 | .filter(ip -> instPort(instPortService, ip) != null) |
| 150 | .map(ip -> instPort(instPortService, ip)) |
Jian Li | 9171c00 | 2018-08-19 22:58:40 +0900 | [diff] [blame] | 151 | .forEach(port -> probeExecutor.execute(() -> |
| 152 | printReachability(tsService.probeEastWest(srcPort, port)))); |
Jian Li | e189c1c | 2018-08-08 15:55:08 +0900 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | |
| 156 | private InstancePort instPort(InstancePortService service, IpAddress ip) { |
| 157 | Optional<InstancePort> port = service.instancePorts().stream() |
| 158 | .filter(p -> p.ipAddress().equals(ip)).findFirst(); |
| 159 | |
| 160 | if (port.isPresent()) { |
| 161 | return port.get(); |
| 162 | } else { |
| 163 | print("Specified destination IP is not existing."); |
| 164 | return null; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | private void printHeader() { |
Jian Li | 0b93b00 | 2018-07-31 13:41:08 +0900 | [diff] [blame] | 169 | print(FORMAT, "Source IP", "", "Destination IP", "Reachability"); |
Jian Li | e189c1c | 2018-08-08 15:55:08 +0900 | [diff] [blame] | 170 | } |
Jian Li | 0b93b00 | 2018-07-31 13:41:08 +0900 | [diff] [blame] | 171 | |
Jian Li | e189c1c | 2018-08-08 15:55:08 +0900 | [diff] [blame] | 172 | private void printReachability(Reachability r) { |
| 173 | String result = r.isReachable() ? REACHABLE : UNREACHABLE; |
| 174 | print(FORMAT, r.srcIp().toString(), ARROW, r.dstIp().toString(), result); |
Jian Li | 0b93b00 | 2018-07-31 13:41:08 +0900 | [diff] [blame] | 175 | } |
Jian Li | c38e903 | 2018-08-09 17:08:38 +0900 | [diff] [blame] | 176 | |
| 177 | private IpAddress getIpAddress(String ipString) { |
| 178 | try { |
| 179 | return IpAddress.valueOf(vmIps[0]); |
| 180 | } catch (IllegalArgumentException e) { |
| 181 | error("Invalid IP address string."); |
| 182 | return null; |
| 183 | } |
| 184 | } |
Jian Li | 0b93b00 | 2018-07-31 13:41:08 +0900 | [diff] [blame] | 185 | } |