Documentation ¶
Overview ¶
Package kping implements large scale & high performance ICMP flood ping.
It can send up to 1024 ICMP packets in one system call (sendmmsg), and provides two methods for receiving packets quickly: batch, afpacket.
The simplest example:
// Create a new Pinger pinger, err := NewPinger("10.0.0.1", 100, 10, 1*time.Minute, 100*time.Millisecond) if err != nil { logger.Fatalln(err) } // Add IP addresses to Pinger if err := pinger.AddIPs([]string{"114.114.114.114", "8.8.8.8"}); err != nil { logger.Fatalln(err) } // Run ! statistics, err : = pinger.Run() if err != nil { logger.Fatalln(err) } // Print result for ip, statistic := range statistics { fmt.Printf("%s: %v\n", ip, statistic) }
You can customize and optimize the send & receive options based on your system:
// Create a new Pinger pinger, err := NewPinger("10.0.0.1", 100, 10, 1*time.Minute, 100*time.Millisecond) if err != nil { logger.Fatalln(err) } // Set send options if sendOpts, err := SendOptions(1024, 10*1024*1024, 30, 100*time.Second, 20*time.Millsecond); err != nil{ logger.Fatalln(err) } else if err := pinger.SetOptions(sendOpts); err != nil { logger.Fatalln(err) } // Set Recv mode: afpacket | batch if err := pinger.SetRecvMode("afpacket"); err != nil{ logger.Fatalln(err) } // Set afpacket recv mode options if recvOpts, err := AfPacketRecvOptions(2, 256, 128, "eth1", 100*time.Millsecond); err != nil{ logger.Fatalln(err) } else if err := pinger.SetOptions(recvOpts) { logger.Fatalln(err) } // Add IP addresses to Pinger if err := pinger.AddIPs([]string{"114.114.114.114", "8.8.8.8"}); err != nil { logger.Fatalln(err) } // Run ! statistics, err : = pinger.Run() if err != nil { logger.Fatalln(err) } // Print result for ip, statistic := range statistics { fmt.Printf("%s: %v\n", ip, statistic) }
Index ¶
- Variables
- type Options
- func AfPacketRecvOptions(parallel, blockMB int64, iface string, timeout time.Duration) (options Options, err error)
- func BatchRecvOptions(batchSize, bufferSize, parallel int64, timeout time.Duration) (options Options, err error)
- func SendOptions(batchSize, bufferSize, parallel int64, timeout, waitTimeout time.Duration) (options Options, err error)
- type Pinger
- type Statistic
Constants ¶
This section is empty.
Variables ¶
View Source
var DefaultRecvMode = "afpacket"
DefaultRecvMode is the default recv mode
Functions ¶
This section is empty.
Types ¶
type Options ¶
type Options interface{}
Options representes send & recv options
func AfPacketRecvOptions ¶
func AfPacketRecvOptions(parallel, blockMB int64, iface string, timeout time.Duration) (options Options, err error)
AfPacketRecvOptions creates af_packet recv options
parallel: recv goroutine number, default: 1 blockMB: af_packet: total block size, default: 128MB timeout: af_packet: poll timeout, default: 100ms iface: recv interface name, default: eth0
func BatchRecvOptions ¶
func BatchRecvOptions(batchSize, bufferSize, parallel int64, timeout time.Duration) (options Options, err error)
BatchRecvOptions creates batch recv options
batchSize: batch recv ICMP packet number, must <= 1024, default: 100 bufferSize: batch recv buffer size, default: 10MB parallel: recv goroutine number, default: 10 timeout: recv timeout, default: 100ms
func SendOptions ¶
func SendOptions(batchSize, bufferSize, parallel int64, timeout, waitTimeout time.Duration) (options Options, err error)
SendOptions creates batch send options
batchSize: batch send ICMP packet number, must <= 1024, default: 1024 bufferSize: batch send buffer size, default: 10MB parallel: send goroutine number, default: 30 timeout: send timeout, default: 100s waitTimeout: batch send interval, default: 20ms
type Pinger ¶
type Pinger interface { // SetRecvMode set recv mode, oneof: afpacket(default)|batch SetRecvMode(recvMode string) error // SetOptions set send or recv options SetOptions(options Options) error // AddIPs add IP addrs to pinger AddIPs(addrs []string) error // Run flood ping, then calculate statistic of each IP Run() (statistics map[string]*Statistic, err error) }
A Pinger provides various methods of kping
func NewPinger ¶
func NewPinger(sourceIP string, count, size int64, timeout, interval time.Duration) (p Pinger, err error)
NewPinger returns a new pinger
sourceIP: ping source IP count: number of request packets to send to each IP size: ICMP payload size in bytes, must >= 8 timeout: the time to wait for all ping done interval: the time between sending a ping packet to each IP
type Statistic ¶
type Statistic struct { RecvNum int64 // recv packets number SentNum int64 // sent packets number LostNum float64 // lost packets number RTTs []float64 // RTT of each recv packet MinRTT float64 // minimum RTT of RTTs MaxRTT float64 // maxmum RTT of RTTs AvgRTT float64 // average RTT of RTTs StdDevRTT float64 // stddev of RTTs // contains filtered or unexported fields }
A Statistic is a ping statistic of IP
Click to show internal directories.
Click to hide internal directories.