Search in sources :

Example 1 with ServiceModeRequiredException

use of com.cisco.trex.stateless.exception.ServiceModeRequiredException in project trex-stateless-gui by cisco-system-traffic-generator.

the class PortLayerConfiguration method handleStartIPv6Scan.

private void handleStartIPv6Scan(ActionEvent actionEvent) {
    LogsController.getInstance().appendText(LogType.INFO, "Start scanning IPv6 neighbor hosts.");
    AsyncResponseManager.getInstance().muteLogger();
    AsyncResponseManager.getInstance().suppressIncomingEvents(true);
    boolean multicastEnabled = model.getMulticast();
    if (!multicastEnabled) {
        model.multicastProperty().setValue(true);
    }
    getIPv6NDService();
    startScanIpv6Btn.setDisable(true);
    ipv6Hosts.getItems().clear();
    ipv6Hosts.setPlaceholder(new Label("Scanning in progress..."));
    Task<Optional<Map<String, Ipv6Node>>> scanIpv6NeighborsTask = new Task<Optional<Map<String, Ipv6Node>>>() {

        @Override
        public Optional<Map<String, Ipv6Node>> call() {
            try {
                return Optional.of(iPv6NDService.scan(model.getIndex(), 10, null, null));
            } catch (ServiceModeRequiredException e) {
                AsyncResponseManager.getInstance().unmuteLogger();
                AsyncResponseManager.getInstance().suppressIncomingEvents(false);
                Platform.runLater(() -> {
                    ipv6Hosts.setPlaceholder(ipv6HostsDefaultPlaceholder);
                    LogsController.getInstance().appendText(LogType.ERROR, "Service mode is not enabled for port: " + model.getIndex() + ". Enable Service Mode in Control tab.");
                });
            }
            return Optional.empty();
        }
    };
    scanIpv6NeighborsTask.setOnSucceeded(e -> {
        AsyncResponseManager.getInstance().unmuteLogger();
        AsyncResponseManager.getInstance().suppressIncomingEvents(false);
        startScanIpv6Btn.setDisable(false);
        Optional<Map<String, Ipv6Node>> result = scanIpv6NeighborsTask.getValue();
        result.ifPresent((hosts) -> {
            ipv6Hosts.getItems().addAll(hosts.entrySet().stream().map(entry -> new IPv6Host(entry.getValue().getMac(), entry.getValue().getIp())).collect(Collectors.toList()));
            if (hosts.isEmpty()) {
                ipv6Hosts.setPlaceholder(ipv6HostsNotFoundPlaceholder);
            }
            LogsController.getInstance().appendText(LogType.INFO, "Found " + hosts.size() + " nodes.");
            LogsController.getInstance().appendText(LogType.INFO, "Scanning complete.");
        });
        if (!multicastEnabled) {
            model.multicastProperty().setValue(false);
        }
    });
    executorService.submit(scanIpv6NeighborsTask);
}
Also used : Ipv6Node(com.cisco.trex.stateless.model.Ipv6Node) IPv6Host(com.cisco.trex.stl.gui.models.IPv6Host) Task(javafx.concurrent.Task) ServiceModeRequiredException(com.cisco.trex.stateless.exception.ServiceModeRequiredException)

Example 2 with ServiceModeRequiredException

use of com.cisco.trex.stateless.exception.ServiceModeRequiredException in project trex-stateless-gui by cisco-system-traffic-generator.

the class PortLayerConfiguration method saveConfiguration.

private void saveConfiguration(Event event) {
    if (model.getPortStatus().equalsIgnoreCase("tx")) {
        guiLogger.appendText(LogType.ERROR, "Port " + model.getIndex() + " is in TX mode. Please stop traffic first.");
        return;
    }
    int vlanIdsCount = Arrays.stream(model.getVlan().split(" ")).filter(vlanId -> !Strings.isNullOrEmpty(vlanId)).map(Integer::valueOf).collect(Collectors.toList()).size();
    if (vlanIdsCount > 2) {
        guiLogger.appendText(LogType.ERROR, "Maximum two nested VLAN tags are allowed.");
        return;
    }
    if (l2Mode.isSelected()) {
        if (Strings.isNullOrEmpty(l2Destination.getText())) {
            guiLogger.appendText(LogType.ERROR, "Destination MAC is empty. ");
            return;
        }
    } else {
        final String dstAddress = l3Destination.getText();
        if (!validateIpAddress(dstAddress)) {
            return;
        }
        boolean isDstIPv4 = InetAddresses.forString(dstAddress).getAddress().length == 4;
        if (isDstIPv4 && !validateIpv4Address(l3Source.getText())) {
            return;
        }
    }
    saveBtn.setDisable(true);
    saveBtn.setText("Applying...");
    Task saveConfigurationTask = new Task<Optional<String>>() {

        @Override
        public Optional<String> call() {
            TRexClient trexClient = ConnectionManager.getInstance().getTrexClient();
            List<Integer> vlanIds = new ArrayList<>();
            if (!Strings.isNullOrEmpty(model.getVlan())) {
                vlanIds.addAll(Arrays.stream(model.getVlan().split(" ")).filter(vlanId -> !Strings.isNullOrEmpty(vlanId)).map(Integer::valueOf).collect(Collectors.toList()));
            }
            AsyncResponseManager.getInstance().suppressIncomingEvents(true);
            boolean serviceModeBeforeResolve = model.getServiceMode();
            trexClient.serviceMode(model.getIndex(), true);
            TRexClientResult<StubResult> vlanConfigResult = trexClient.setVlan(model.getIndex(), vlanIds);
            if (vlanConfigResult.isFailed()) {
                guiLogger.appendText(LogType.ERROR, "Unable to save VLAN configuration");
            } else {
                guiLogger.appendText(LogType.INFO, "VLAN configuration updated");
            }
            if (l2Mode.isSelected()) {
                String dstMac = l2Destination.getText();
                try {
                    serverRPCMethods.setSetL2(model.getIndex(), dstMac);
                    guiLogger.appendText(LogType.INFO, "L2 mode configured for " + model.getIndex());
                    clearModelIPv6();
                } catch (Exception e1) {
                    logger.error("Failed to set L2 mode: " + e1.getMessage());
                }
            } else if (l3Mode.isSelected()) {
                try {
                    AsyncResponseManager.getInstance().muteLogger();
                    String portSrcIP = l3Source.getText();
                    String portDstIP = l3Destination.getText();
                    final boolean dstIsIPv6 = InetAddresses.forString(portDstIP).getAddress().length > 4;
                    final boolean srcIsIPv6 = InetAddresses.forString(portSrcIP).getAddress().length > 4;
                    if (srcIsIPv6 != dstIsIPv6) {
                        throw new Exception("src and dst addresses are different version");
                    }
                    if (dstIsIPv6) {
                        Map<String, Ipv6Node> result = getIPv6NDService().scan(model.getIndex(), 5, portDstIP, portSrcIP);
                        AsyncResponseManager.getInstance().unmuteLogger();
                        String statusString;
                        String logEntry;
                        if (result.size() == 1) {
                            Ipv6Node host = result.entrySet().iterator().next().getValue();
                            final String mac = host.getMac();
                            statusString = "resolved";
                            logEntry = "IPv6 destination resolved: " + mac;
                            setAsL2DstAction(mac);
                            try {
                                serverRPCMethods.setSetL2(model.getIndex(), mac);
                                guiLogger.appendText(LogType.INFO, "L2 mode configured for " + model.getIndex());
                                final PortLayerConfigurationModel l3Conf = model.getL3LayerConfiguration();
                                l3Conf.setSrc6(portSrcIP);
                                l3Conf.setDst6(portDstIP);
                            } catch (Exception e1) {
                                logger.error("Failed to set L2 mode: " + e1.getMessage());
                            }
                        } else {
                            statusString = "-";
                            logEntry = "Unable to resolve IPv6 destination.";
                        }
                        Platform.runLater(() -> {
                            arpStatus.setText("none");
                            ipv6NDStatus.setText(statusString);
                            guiLogger.appendText(LogType.INFO, logEntry);
                        });
                    } else {
                        trexClient.setL3Mode(model.getIndex(), null, portSrcIP, portDstIP);
                        clearModelIPv6();
                        String nextHopMac = trexClient.resolveArp(model.getIndex(), portSrcIP, portDstIP);
                        if (nextHopMac != null) {
                            trexClient.setL3Mode(model.getIndex(), nextHopMac, portSrcIP, portDstIP);
                        }
                        Platform.runLater(() -> {
                            ipv6NDStatus.setText("-");
                        });
                        return nextHopMac == null ? Optional.empty() : Optional.of(nextHopMac);
                    }
                } catch (ServiceModeRequiredException e) {
                    AsyncResponseManager.getInstance().unmuteLogger();
                    Platform.runLater(() -> {
                        ipv6Hosts.setPlaceholder(ipv6HostsDefaultPlaceholder);
                        LogsController.getInstance().appendText(LogType.ERROR, "Service mode is not enabled for port: " + model.getIndex() + ". Enable Service Mode in Control tab.");
                    });
                } catch (Exception e) {
                    logger.error("Failed to set L3 mode: " + e.getMessage());
                    guiLogger.appendText(LogType.ERROR, "Failed to set L3 mode: " + e.getMessage());
                } finally {
                    trexClient.serviceMode(model.getIndex(), serviceModeBeforeResolve);
                    AsyncResponseManager.getInstance().suppressIncomingEvents(false);
                    AsyncResponseManager.getInstance().unmuteLogger();
                }
            }
            return Optional.empty();
        }
    };
    saveConfigurationTask.setOnSucceeded(e -> {
        saveBtn.setText("Apply");
        saveBtn.setDisable(false);
        final boolean dstIsIPv6 = InetAddresses.forString(l3Source.getText()).getAddress().length > 4;
        final boolean srcIsIPv6 = InetAddresses.forString(l3Destination.getText()).getAddress().length > 4;
        Optional result = (Optional) (saveConfigurationTask.getValue());
        if (l3Mode.isSelected() && !dstIsIPv6 && !srcIsIPv6) {
            String status = "unresolved";
            if (result.isPresent()) {
                status = "resolved";
                guiLogger.appendText(LogType.INFO, "ARP resolution for " + l3Destination.getText() + " is " + result.get());
            } else {
                guiLogger.appendText(LogType.ERROR, "ARP resolution arpStatus: FAILED");
            }
            this.arpStatus.setText(status);
        }
    });
    new Thread(saveConfigurationTask).start();
}
Also used : java.util(java.util) javafx.scene.control(javafx.scene.control) AsyncResponseManager(com.exalttech.trex.core.AsyncResponseManager) ConnectionManager(com.exalttech.trex.core.ConnectionManager) ServiceModeRequiredException(com.cisco.trex.stateless.exception.ServiceModeRequiredException) FXCollections(javafx.collections.FXCollections) LogType(com.exalttech.trex.ui.views.logs.LogType) Bindings(javafx.beans.binding.Bindings) TrexApp(com.exalttech.trex.application.TrexApp) IcmpV6CommonPacket(org.pcap4j.packet.IcmpV6CommonPacket) Strings(com.google.common.base.Strings) Logger(org.apache.log4j.Logger) TRexClientResult(com.cisco.trex.stateless.model.TRexClientResult) Task(javafx.concurrent.Task) PortModel(com.exalttech.trex.ui.models.PortModel) IPv6NeighborDiscoveryService(com.cisco.trex.stateless.IPv6NeighborDiscoveryService) IpV4Packet(org.pcap4j.packet.IpV4Packet) PortLayerConfigurationModel(com.exalttech.trex.ui.models.PortLayerConfigurationModel) Initialization(com.exalttech.trex.util.Initialization) IcmpV4CommonPacket(org.pcap4j.packet.IcmpV4CommonPacket) ExecutorService(java.util.concurrent.ExecutorService) LogsController(com.exalttech.trex.ui.views.logs.LogsController) IcmpV6Type(org.pcap4j.packet.namednumber.IcmpV6Type) TRexClient(com.cisco.trex.stateless.TRexClient) StubResult(com.cisco.trex.stateless.model.StubResult) RPCMethods(com.exalttech.trex.core.RPCMethods) ConfigurationMode(com.exalttech.trex.ui.models.ConfigurationMode) Event(javafx.event.Event) EthernetPacket(org.pcap4j.packet.EthernetPacket) UnknownHostException(java.net.UnknownHostException) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) Platform(javafx.application.Platform) FXML(javafx.fxml.FXML) ActionEvent(javafx.event.ActionEvent) Clipboard(javafx.scene.input.Clipboard) IPv6Host(com.cisco.trex.stl.gui.models.IPv6Host) AnchorPane(javafx.scene.layout.AnchorPane) Ipv6Node(com.cisco.trex.stateless.model.Ipv6Node) ObservableList(javafx.collections.ObservableList) ClipboardContent(javafx.scene.input.ClipboardContent) IcmpV4Type(org.pcap4j.packet.namednumber.IcmpV4Type) InetAddresses(com.google.common.net.InetAddresses) ChangeListener(javafx.beans.value.ChangeListener) Ipv6Node(com.cisco.trex.stateless.model.Ipv6Node) PortLayerConfigurationModel(com.exalttech.trex.ui.models.PortLayerConfigurationModel) Task(javafx.concurrent.Task) TRexClient(com.cisco.trex.stateless.TRexClient) ServiceModeRequiredException(com.cisco.trex.stateless.exception.ServiceModeRequiredException) ServiceModeRequiredException(com.cisco.trex.stateless.exception.ServiceModeRequiredException) UnknownHostException(java.net.UnknownHostException) StubResult(com.cisco.trex.stateless.model.StubResult)

Example 3 with ServiceModeRequiredException

use of com.cisco.trex.stateless.exception.ServiceModeRequiredException in project trex-stateless-gui by cisco-system-traffic-generator.

the class PortLayerConfiguration method runPingCmd.

private void runPingCmd(Event event) {
    if (model.getPortStatus().equalsIgnoreCase("tx")) {
        guiLogger.appendText(LogType.ERROR, "Port " + model.getIndex() + " is in TX mode. Please stop traffic first.");
        return;
    }
    if (Strings.isNullOrEmpty(pingDestination.getText())) {
        guiLogger.appendText(LogType.ERROR, "Empty ping destination address.");
        return;
    }
    final String targetIP = pingDestination.getText();
    if (!targetIP.contains(":") && !model.getL3LayerConfiguration().getState().equalsIgnoreCase("resolved")) {
        guiLogger.appendText(LogType.ERROR, "ARP resolution required. Configure L3IPv4 mode properly.");
        return;
    }
    pingCommandBtn.setDisable(true);
    Task<Void> pingTask = new Task<Void>() {

        @Override
        public Void call() {
            TRexClient trexClient = ConnectionManager.getInstance().getTrexClient();
            trexClient.serviceMode(model.getIndex(), true);
            guiLogger.appendText(LogType.PING, " Start ping " + targetIP);
            AsyncResponseManager.getInstance().muteLogger();
            AsyncResponseManager.getInstance().suppressIncomingEvents(true);
            try {
                int icmp_id = new Random().nextInt(100);
                for (int icmp_sec = 1; icmp_sec < 6; icmp_sec++) {
                    EthernetPacket reply = null;
                    if (targetIP.contains(":")) {
                        // IPv6
                        reply = trexClient.sendIcmpV6Echo(model.getIndex(), targetIP, icmp_id, icmp_sec, 2);
                        if (reply != null) {
                            IcmpV6CommonPacket icmpV6CommonPacket = reply.get(IcmpV6CommonPacket.class);
                            IcmpV6Type icmpReplyType = icmpV6CommonPacket.getHeader().getType();
                            String msg = null;
                            if (IcmpV6Type.ECHO_REPLY.equals(icmpReplyType)) {
                                msg = " Reply from " + targetIP + " size=" + reply.getRawData().length + " icmp_sec=" + icmp_sec;
                            } else if (IcmpV6Type.DESTINATION_UNREACHABLE.equals(icmpReplyType)) {
                                msg = " Destination host unreachable";
                            }
                            guiLogger.appendText(LogType.PING, msg);
                        } else {
                            guiLogger.appendText(LogType.PING, "Request timeout.");
                        }
                    } else {
                        // IPv4
                        reply = trexClient.sendIcmpEcho(model.getIndex(), targetIP, icmp_id, icmp_sec, 1000);
                        if (reply != null) {
                            IpV4Packet ip = reply.get(IpV4Packet.class);
                            String ttl = String.valueOf(ip.getHeader().getTtlAsInt());
                            IcmpV4CommonPacket echoReplyPacket = reply.get(IcmpV4CommonPacket.class);
                            IcmpV4Type replyType = echoReplyPacket.getHeader().getType();
                            if (IcmpV4Type.ECHO_REPLY.equals(replyType)) {
                                guiLogger.appendText(LogType.PING, " Reply from " + targetIP + " size=" + reply.getRawData().length + " ttl=" + ttl + " icmp_sec=" + icmp_sec);
                            } else if (IcmpV4Type.DESTINATION_UNREACHABLE.equals(replyType)) {
                                guiLogger.appendText(LogType.PING, " Destination host unreachable");
                            }
                        } else {
                            guiLogger.appendText(LogType.PING, " Request timeout for icmp_seq " + icmp_sec);
                        }
                    }
                }
                guiLogger.appendText(LogType.PING, " Ping finished.");
            } catch (UnknownHostException e) {
                guiLogger.appendText(LogType.PING, " Unknown host");
            } catch (ServiceModeRequiredException e) {
                e.printStackTrace();
            } finally {
                pingCommandBtn.setDisable(false);
                trexClient.serviceMode(model.getIndex(), false);
                AsyncResponseManager.getInstance().unmuteLogger();
                AsyncResponseManager.getInstance().suppressIncomingEvents(false);
            }
            return null;
        }
    };
    pingTask.setOnSucceeded(e -> pingCommandBtn.setDisable(false));
    new Thread(pingTask).start();
}
Also used : Task(javafx.concurrent.Task) IcmpV6CommonPacket(org.pcap4j.packet.IcmpV6CommonPacket) IcmpV4CommonPacket(org.pcap4j.packet.IcmpV4CommonPacket) TRexClient(com.cisco.trex.stateless.TRexClient) UnknownHostException(java.net.UnknownHostException) ServiceModeRequiredException(com.cisco.trex.stateless.exception.ServiceModeRequiredException) EthernetPacket(org.pcap4j.packet.EthernetPacket) IcmpV4Type(org.pcap4j.packet.namednumber.IcmpV4Type) IcmpV6Type(org.pcap4j.packet.namednumber.IcmpV6Type) IpV4Packet(org.pcap4j.packet.IpV4Packet)

Aggregations

ServiceModeRequiredException (com.cisco.trex.stateless.exception.ServiceModeRequiredException)3 Task (javafx.concurrent.Task)3 TRexClient (com.cisco.trex.stateless.TRexClient)2 Ipv6Node (com.cisco.trex.stateless.model.Ipv6Node)2 IPv6Host (com.cisco.trex.stl.gui.models.IPv6Host)2 UnknownHostException (java.net.UnknownHostException)2 EthernetPacket (org.pcap4j.packet.EthernetPacket)2 IcmpV4CommonPacket (org.pcap4j.packet.IcmpV4CommonPacket)2 IPv6NeighborDiscoveryService (com.cisco.trex.stateless.IPv6NeighborDiscoveryService)1 StubResult (com.cisco.trex.stateless.model.StubResult)1 TRexClientResult (com.cisco.trex.stateless.model.TRexClientResult)1 TrexApp (com.exalttech.trex.application.TrexApp)1 AsyncResponseManager (com.exalttech.trex.core.AsyncResponseManager)1 ConnectionManager (com.exalttech.trex.core.ConnectionManager)1 RPCMethods (com.exalttech.trex.core.RPCMethods)1 ConfigurationMode (com.exalttech.trex.ui.models.ConfigurationMode)1 PortLayerConfigurationModel (com.exalttech.trex.ui.models.PortLayerConfigurationModel)1 PortModel (com.exalttech.trex.ui.models.PortModel)1 LogType (com.exalttech.trex.ui.views.logs.LogType)1 LogsController (com.exalttech.trex.ui.views.logs.LogsController)1