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