diff --git a/pinger b/pinger index 5703e71..57d563d 100755 Binary files a/pinger and b/pinger differ diff --git a/pinger.go b/pinger.go index 83b15ad..903556f 100644 --- a/pinger.go +++ b/pinger.go @@ -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) @@ -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) @@ -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) -// } -// }