diff --git a/defs/options.go b/defs/options.go index d3a3b01..255a5b1 100644 --- a/defs/options.go +++ b/defs/options.go @@ -8,6 +8,7 @@ const ( OptionIPv6Alt = "6" OptionNoDownload = "no-download" OptionNoUpload = "no-upload" + OptionNoICMP = "no-icmp" OptionConcurrent = "concurrent" OptionBytes = "bytes" OptionMebiBytes = "mebibytes" diff --git a/defs/server.go b/defs/server.go index c65f541..33417eb 100644 --- a/defs/server.go +++ b/defs/server.go @@ -32,8 +32,8 @@ type Server struct { SponsorName string `json:"sponsorName"` SponsorURL string `json:"sponsorURL"` - icmpFailed bool `json:"-"` - TLog TelemetryLog `json:"-"` + NoICMP bool `json:"-"` + TLog TelemetryLog `json:"-"` } // IsUp checks the speed test backend is up by accessing the ping URL @@ -71,8 +71,8 @@ func (s *Server) ICMPPingAndJitter(count int, srcIp, network string) (float64, f s.TLog.Logf("ICMP ping took %s", time.Now().Sub(t).String()) }() - if s.icmpFailed { - log.Debug("ICMP ping failed already, using HTTP ping") + if s.NoICMP { + log.Debugf("Skipping ICMP for server %s, will use HTTP ping", s.Name) return s.PingAndJitter(count + 2) } @@ -116,7 +116,7 @@ func (s *Server) ICMPPingAndJitter(count int, srcIp, network string) (float64, f } if len(stats.Rtts) == 0 { - s.icmpFailed = true + s.NoICMP = true log.Debugf("No ICMP pings returned for server %s (%s), trying TCP ping", s.Name, u.Hostname()) return s.PingAndJitter(count + 2) } diff --git a/main.go b/main.go index 1acc53f..4c2193c 100644 --- a/main.go +++ b/main.go @@ -56,6 +56,11 @@ func main() { Name: defs.OptionNoUpload, Usage: "Do not perform upload test", }, + &cli.BoolFlag{ + Name: defs.OptionNoICMP, + Usage: "Do not use ICMP ping. ICMP doesn't work well under Linux\n" + + "at this moment, so you might want to disable it", + }, &cli.IntFlag{ Name: defs.OptionConcurrent, Usage: "Concurrent HTTP requests being made", diff --git a/speedtest/helper.go b/speedtest/helper.go index 12ca450..69e0c56 100644 --- a/speedtest/helper.go +++ b/speedtest/helper.go @@ -65,6 +65,9 @@ func doSpeedTest(c *cli.Context, servers []defs.Server, telemetryServer defs.Tel pb.Start() } + // skip ICMP if option given + currentServer.NoICMP = c.Bool(defs.OptionNoICMP) + p, jitter, err := currentServer.ICMPPingAndJitter(pingCount, c.String(defs.OptionSource), network) if err != nil { log.Errorf("Failed to get ping and jitter: %s", err) diff --git a/speedtest/speedtest.go b/speedtest/speedtest.go index a2e8057..f24f4b0 100644 --- a/speedtest/speedtest.go +++ b/speedtest/speedtest.go @@ -262,7 +262,7 @@ func SpeedTest(c *cli.Context) error { // spawn 10 concurrent pingers for i := 0; i < 10; i++ { - go pingWorker(jobs, results, &wg, c.String(defs.OptionSource), network) + go pingWorker(jobs, results, &wg, c.String(defs.OptionSource), network, c.Bool(defs.OptionNoICMP)) } // send ping jobs to workers @@ -303,7 +303,7 @@ func SpeedTest(c *cli.Context) error { } } -func pingWorker(jobs <-chan PingJob, results chan<- PingResult, wg *sync.WaitGroup, srcIp, network string) { +func pingWorker(jobs <-chan PingJob, results chan<- PingResult, wg *sync.WaitGroup, srcIp, network string, noICMP bool) { for { job := <-jobs server := job.Server @@ -317,6 +317,9 @@ func pingWorker(jobs <-chan PingJob, results chan<- PingResult, wg *sync.WaitGro // check the server is up by accessing the ping URL and checking its returned value == empty and status code == 200 if server.IsUp() { + // skip ICMP if option given + server.NoICMP = noICMP + // if server is up, get ping ping, _, err := server.ICMPPingAndJitter(1, srcIp, network) if err != nil {