Jian Li | c38e903 | 2018-08-09 17:08:38 +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 | |
| 18 | import com.google.common.collect.Sets; |
| 19 | import org.apache.karaf.shell.commands.Argument; |
| 20 | import org.apache.karaf.shell.commands.Command; |
| 21 | import org.apache.karaf.shell.commands.Option; |
| 22 | import org.onosproject.cli.AbstractShellCommand; |
| 23 | import org.onosproject.cluster.ClusterService; |
| 24 | import org.onosproject.cluster.NodeId; |
| 25 | import org.onosproject.mastership.MastershipService; |
| 26 | import org.onosproject.openstacknetworking.api.InstancePort; |
| 27 | import org.onosproject.openstacknetworking.api.InstancePortService; |
| 28 | import org.onosproject.openstacknode.api.OpenstackNode; |
| 29 | import org.onosproject.openstacknode.api.OpenstackNodeService; |
| 30 | import org.onosproject.openstacktroubleshoot.api.OpenstackTroubleshootService; |
| 31 | import org.onosproject.openstacktroubleshoot.api.Reachability; |
| 32 | |
| 33 | import java.util.Set; |
Jian Li | 9171c00 | 2018-08-19 22:58:40 +0900 | [diff] [blame] | 34 | import java.util.concurrent.ExecutorService; |
Jian Li | c38e903 | 2018-08-09 17:08:38 +0900 | [diff] [blame] | 35 | |
Jian Li | 9171c00 | 2018-08-19 22:58:40 +0900 | [diff] [blame] | 36 | import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor; |
| 37 | import static org.onlab.util.Tools.groupedThreads; |
Jian Li | c38e903 | 2018-08-09 17:08:38 +0900 | [diff] [blame] | 38 | import static org.onosproject.openstacknetworking.api.InstancePort.State.ACTIVE; |
| 39 | import static org.onosproject.openstacknode.api.OpenstackNode.NodeType.GATEWAY; |
| 40 | |
| 41 | /** |
| 42 | * Checks the north-south VM connectivity. |
| 43 | */ |
| 44 | @Command(scope = "onos", name = "openstack-check-north-south", |
| 45 | description = "Checks the north-south VMs connectivity") |
| 46 | public class OpenstackNorthSouthProbeCommand extends AbstractShellCommand { |
| 47 | |
| 48 | private static final String REACHABLE = "Reachable :)"; |
| 49 | private static final String UNREACHABLE = "Unreachable :("; |
| 50 | private static final String ARROW = "->"; |
| 51 | |
| 52 | private static final String FORMAT = "%-20s%-5s%-20s%-20s"; |
| 53 | |
| 54 | @Option(name = "-a", aliases = "--all", description = "Apply this command to all VMs", |
| 55 | required = false, multiValued = false) |
| 56 | private boolean isAll = false; |
| 57 | |
| 58 | @Argument(index = 0, name = "vmIps", description = "VMs' IP addresses", |
| 59 | required = false, multiValued = true) |
| 60 | private String[] vmIps = null; |
| 61 | |
Jian Li | 9171c00 | 2018-08-19 22:58:40 +0900 | [diff] [blame] | 62 | private final ExecutorService probeExecutor = newSingleThreadScheduledExecutor( |
| 63 | groupedThreads(this.getClass().getSimpleName(), "probe-handler", log)); |
| 64 | |
Jian Li | c38e903 | 2018-08-09 17:08:38 +0900 | [diff] [blame] | 65 | @Override |
| 66 | protected void execute() { |
| 67 | OpenstackTroubleshootService tsService = get(OpenstackTroubleshootService.class); |
| 68 | InstancePortService instPortService = get(InstancePortService.class); |
| 69 | OpenstackNodeService osNodeService = get(OpenstackNodeService.class); |
| 70 | MastershipService mastershipService = get(MastershipService.class); |
| 71 | ClusterService clusterService = get(ClusterService.class); |
| 72 | |
| 73 | if (tsService == null || osNodeService == null || |
| 74 | instPortService == null || mastershipService == null) { |
| 75 | error("Failed to troubleshoot openstack networking."); |
| 76 | return; |
| 77 | } |
| 78 | |
| 79 | if ((!isAll && vmIps == null) || (isAll && vmIps != null)) { |
| 80 | print("Please specify one of VM IP address or -a option."); |
| 81 | return; |
| 82 | } |
| 83 | |
| 84 | NodeId localNodeId = clusterService.getLocalNode().id(); |
| 85 | |
| 86 | for (OpenstackNode gw : osNodeService.completeNodes(GATEWAY)) { |
| 87 | if (!localNodeId.equals(mastershipService.getMasterFor(gw.intgBridge()))) { |
Jian Li | 43d0428 | 2018-08-22 14:31:21 +0900 | [diff] [blame] | 88 | error("Current node is not the master for all gateway nodes. " + |
| 89 | "Please enforce mastership first using openstack-reset-mastership -c !"); |
Jian Li | c38e903 | 2018-08-09 17:08:38 +0900 | [diff] [blame] | 90 | return; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | if (isAll) { |
| 95 | printHeader(); |
| 96 | |
| 97 | // send ICMP PACKET_OUT to all connect VMs whose instance port state is ACTIVE |
| 98 | instPortService.instancePorts().stream() |
| 99 | .filter(p -> p.state() == ACTIVE) |
| 100 | .filter(p -> instPortService.floatingIp(p.portId()) != null) |
| 101 | .forEach(port -> printReachability(tsService.probeNorthSouth(port))); |
| 102 | } else { |
| 103 | |
| 104 | final Set<InstancePort> ports = Sets.newConcurrentHashSet(); |
| 105 | |
| 106 | for (String ip : vmIps) { |
| 107 | instPortService.instancePorts().stream() |
| 108 | .filter(p -> p.state().equals(InstancePort.State.ACTIVE)) |
| 109 | .filter(p -> instPortService.floatingIp(p.portId()) != null) |
| 110 | .filter(p -> ip.equals(instPortService.floatingIp(p.portId()).toString())) |
| 111 | .forEach(ports::add); |
| 112 | } |
| 113 | |
| 114 | printHeader(); |
Jian Li | 9171c00 | 2018-08-19 22:58:40 +0900 | [diff] [blame] | 115 | ports.forEach(port -> probeExecutor.execute(() -> |
| 116 | printReachability(tsService.probeNorthSouth(port)))); |
Jian Li | c38e903 | 2018-08-09 17:08:38 +0900 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | |
| 120 | private void printHeader() { |
| 121 | print(FORMAT, "Source IP", "", "Destination IP", "Reachability"); |
| 122 | } |
| 123 | |
| 124 | private void printReachability(Reachability r) { |
| 125 | if (r == null) { |
| 126 | return; |
| 127 | } |
| 128 | String result = r.isReachable() ? REACHABLE : UNREACHABLE; |
| 129 | print(FORMAT, r.srcIp().toString(), ARROW, r.dstIp().toString(), result); |
| 130 | } |
| 131 | } |