Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified pinger
Binary file not shown.
113 changes: 36 additions & 77 deletions pinger.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,16 @@ var PayloadSize uint
func main() {

flag.IntVar(&PingCount, "count", 5, "Number of Ping packets to send.")

flag.StringVar(&NIC, "interface", "ens9", "Network interface to attach to.")
flag.IntVar(&QueueID, "queue", 0, "The queue on the network interface to attach to.")
flag.StringVar(&SrcMAC, "srcmac", "b2968175b211", "Source MAC address to use in sent frames.")
flag.StringVar(&DstMAC, "dstmac", "ffffffffffff", "Destination MAC address to use in sent frames.")
flag.StringVar(&SrcIP, "srcip", "192.168.111.10", "Source IP address to use in sent frames.")
flag.StringVar(&DstIP, "dstip", "192.168.111.1", "Destination IP address to use in sent frames.")
flag.UintVar(&SrcPort, "srcport", 1234, "Source UDP port.")
flag.UintVar(&DstPort, "dstport", 1234, "Destination UDP port.")
flag.UintVar(&PayloadSize, "payloadsize", 10, "Size of the UDP payload.")
flag.UintVar(&PayloadSize, "payloadsize", 10, "Size of the payload.")
flag.Parse()

// Initialize the XDP socket.

link, err := netlink.LinkByName(NIC)
if err != nil {
panic(err)
Expand All @@ -58,8 +54,9 @@ func main() {
panic(err)
}

// Pre-generate a frame containing a DNS query.
defer xsk.Close()

// Pre-generate a frame
srcMAC, _ := hex.DecodeString(SrcMAC)
dstMAC, _ := hex.DecodeString(DstMAC)

Expand All @@ -84,106 +81,68 @@ func main() {
Seq: 1,
}

// udp.SetNetworkLayerForChecksum(ip)
// add payload
payload := make([]byte, PayloadSize)
for i := 0; i < len(payload); i++ {
payload[i] = byte(i)
}

// wm := icmp.Message{
// Type: ipv6.ICMPTypeEchoRequest, Code: 0,
// Body: &icmp.Echo{
// ID: os.Getpid() & 0xffff, Seq: 1,
// Data: []byte("HELLO-R-U-THERE"),
// },
// }
// icmp_bytes, err := wm.Marshal(nil)
// if err != nil {
// log.Fatal(err)
// }

// serialize
buf := gopacket.NewSerializeBuffer()
opts := gopacket.SerializeOptions{
FixLengths: true,
ComputeChecksums: true,
}

fmt.Printf("sending icmp packets: %v (%v) to %v (%v)...\n", ip.SrcIP, eth.SrcMAC, ip.DstIP, eth.DstMAC)

err = gopacket.SerializeLayers(buf, opts, eth, ip, icmp, gopacket.Payload(payload))
if err != nil {
panic(err)
}
frameLen := len(buf.Bytes())

// buf.Bytes()[0x22] = 8
// buf.Bytes()[0x23] = 0

// Fill all the frames in UMEM with the pre-generated UDP packet.

// Fill all the frames in UMEM with the pre-generated packet.
descs := xsk.GetDescs(math.MaxInt32)
for i, _ := range descs {
frameLen = copy(xsk.GetFrame(descs[i]), buf.Bytes())
}

// Start sending the pre-generated frame as quickly as possible in an
// endless loop printing statistics of the number of sent frames and
// the number of sent bytes every second.
// go func() {
// var err error
// var prev xdp.Stats
// var cur xdp.Stats
// var numPkts uint64
// for i := uint64(0); ; i++ {
// time.Sleep(time.Duration(1) * time.Second)
// cur, err = xsk.Stats()
// if err != nil {
// panic(err)
// }
// numPkts = cur.Completed - prev.Completed
// fmt.Printf("%d packets/s (%d Mb/s)\n", numPkts, (numPkts*uint64(frameLen)*8)/(1000*1000))
// prev = cur
// }
// }()

fmt.Printf("sending icmp packets: %v (%v) to %v (%v)...\n", ip.SrcIP, eth.SrcMAC, ip.DstIP, eth.DstMAC)

go func() {
var err error
var prev xdp.Stats
var cur xdp.Stats
var numPkts uint64
for i := uint64(0); ; i++ {
time.Sleep(time.Duration(1) * time.Second)
cur, err = xsk.Stats()
// transmit
for i := 0; i < PingCount; i++ {

descs := xsk.GetDescs(xsk.NumFreeTxSlots())
if len(descs) > 0 {
fmt.Printf("Sending Ping Packet: %d\n", i)

for j, _ := range descs {
descs[j].Len = uint32(frameLen)
}

numPosted := xsk.Transmit(descs)
_, numCompleted, err := xsk.Poll(1)
if err != nil {
panic(err)
}
numPkts = cur.Completed - prev.Completed
fmt.Printf("%d packets/s (%d Mb/s)\n", numPkts, (numPkts*uint64(frameLen)*8)/(1000*1000))
prev = cur
fmt.Printf("Transmitted packets: %d posted, %d transmitted\n", numPosted, numCompleted)
}
}()

// var count = 0
for i := 0; i < PingCount; i++ {
fmt.Printf("Sending Ping Packet: %d\n", i)
descs := xsk.GetDescs(1)
for i, _ := range descs {
descs[i].Len = uint32(frameLen)
}
xsk.Transmit(descs)

_, _, err = xsk.Poll(-1)
if err != nil {
panic(err)
}

time.Sleep(time.Duration(150) * time.Millisecond)
}
}

// package main

// import (
// "fmt"
// "log"
// "net"
// "syscall"

// "golang.org/x/sys/unix"
// )

// func main() {
// fd, err := syscall.Socket(unix.AF_XDP, syscall.SOCK_RAW, 0)
// fmt.Println("Fd: ", fd)

// err = syscall.Close(fd)
// if err != nil {
// panic(err)
// }
// }