Skip to content
This repository was archived by the owner on Aug 26, 2024. It is now read-only.

Commit d96ade0

Browse files
authored
Merge pull request #2 from glaslos/gofmt_gomod
go module and gofmt and lint warnings
2 parents c65a48c + dd15614 commit d96ade0

File tree

9 files changed

+320
-264
lines changed

9 files changed

+320
-264
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@
1010

1111
# Output of the go coverage tool, specifically when used with LiteIDE
1212
*.out
13+
14+
# Developer
15+
.vscode

examples/dumpframes/dumpframes.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
// Copyright 2019 Asavie Technologies Ltd. All rights reserved.
2-
//
3-
// Use of this source code is governed by a BSD-style license
4-
// that can be found in the LICENSE file in the root of the source
5-
// tree.
1+
// Copyright 2019 Asavie Technologies Ltd. All rights reserved.
2+
//
3+
// Use of this source code is governed by a BSD-style license
4+
// that can be found in the LICENSE file in the root of the source
5+
// tree.
66

77
/*
88
dumpframes demostrates how to receive frames from a network link using

examples/l2fwd/l2fwd.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,21 @@ import (
2121
func main() {
2222
var inLinkName string
2323
var inLinkDstStr string
24-
var inLinkQueueId int
24+
var inLinkQueueID int
2525
var outLinkName string
2626
var outLinkDstStr string
27-
var outLinkQueueId int
27+
var outLinkQueueID int
2828
var verbose bool
2929

3030
flag.Usage = func() {
3131
fmt.Fprintf(flag.CommandLine.Output(), "usage: %s -inlink <network link name> -outlink <network link name>\n", os.Args[0])
3232
flag.PrintDefaults()
3333
}
3434
flag.StringVar(&inLinkName, "inlink", "", "Input network link name.")
35-
flag.IntVar(&inLinkQueueId, "inlinkqueue", 0, "The queue ID to attach to on input link.")
35+
flag.IntVar(&inLinkQueueID, "inlinkqueue", 0, "The queue ID to attach to on input link.")
3636
flag.StringVar(&inLinkDstStr, "inlinkdst", "ff:ff:ff:ff:ff:ff", "Destination MAC address to forward frames to from 'in' interface.")
3737
flag.StringVar(&outLinkName, "outlink", "", "Output network link name.")
38-
flag.IntVar(&outLinkQueueId, "outlinkqueue", 0, "The queue ID to attach to on output link.")
38+
flag.IntVar(&outLinkQueueID, "outlinkqueue", 0, "The queue ID to attach to on output link.")
3939
flag.StringVar(&outLinkDstStr, "outlinkdst", "ff:ff:ff:ff:ff:ff", "Destination MAC address to forward frames to from 'out' interface.")
4040
flag.BoolVar(&verbose, "verbose", false, "Output forwarding statistics.")
4141
flag.Parse()
@@ -67,18 +67,18 @@ func main() {
6767
log.Fatalf("failed to fetch info about link %s: %v", outLinkName, err)
6868
}
6969

70-
forwardL2(verbose, inLink, inLinkQueueId, inLinkDst, outLink, outLinkQueueId, outLinkDst)
70+
forwardL2(verbose, inLink, inLinkQueueID, inLinkDst, outLink, outLinkQueueID, outLinkDst)
7171
}
7272

73-
func forwardL2(verbose bool, inLink netlink.Link, inLinkQueueId int, inLinkDst net.HardwareAddr, outLink netlink.Link, outLinkQueueId int, outLinkDst net.HardwareAddr) {
73+
func forwardL2(verbose bool, inLink netlink.Link, inLinkQueueID int, inLinkDst net.HardwareAddr, outLink netlink.Link, outLinkQueueID int, outLinkDst net.HardwareAddr) {
7474
log.Printf("opening XDP socket for %s...", inLink.Attrs().Name)
75-
inXsk, err := xdp.NewSocket(inLink.Attrs().Index, inLinkQueueId)
75+
inXsk, err := xdp.NewSocket(inLink.Attrs().Index, inLinkQueueID)
7676
if err != nil {
7777
log.Fatalf("failed to open XDP socket for link %s: %v", inLink.Attrs().Name, err)
7878
}
7979

8080
log.Printf("opening XDP socket for %s...", outLink.Attrs().Name)
81-
outXsk, err := xdp.NewSocket(outLink.Attrs().Index, outLinkQueueId)
81+
outXsk, err := xdp.NewSocket(outLink.Attrs().Index, outLinkQueueID)
8282
if err != nil {
8383
log.Fatalf("failed to open XDP socket for link %s: %v", outLink.Attrs().Name, err)
8484
}

examples/rebroadcast/rebroadcast.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
// Copyright 2019 Asavie Technologies Ltd. All rights reserved.
2-
//
3-
// Use of this source code is governed by a BSD-style license
4-
// that can be found in the LICENSE file in the root of the source
5-
// tree.
1+
// Copyright 2019 Asavie Technologies Ltd. All rights reserved.
2+
//
3+
// Use of this source code is governed by a BSD-style license
4+
// that can be found in the LICENSE file in the root of the source
5+
// tree.
66

77
/*
88
rebroadcast demonstrates how to receive and transmit network frames using

examples/senddnsqueries/senddnsqueries.go

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,22 @@ import (
1313
"time"
1414

1515
"github.com/asavie/xdp"
16-
"github.com/vishvananda/netlink"
17-
"github.com/miekg/dns"
1816
"github.com/google/gopacket"
1917
"github.com/google/gopacket/layers"
18+
"github.com/miekg/dns"
19+
"github.com/vishvananda/netlink"
2020
)
2121

22-
var NIC string
23-
var QueueID int
24-
var SrcMAC string
25-
var DstMAC string
26-
var SrcIP string
27-
var DstIP string
28-
var DomainName string
22+
// ...
23+
var (
24+
NIC string
25+
QueueID int
26+
SrcMAC string
27+
DstMAC string
28+
SrcIP string
29+
DstIP string
30+
DomainName string
31+
)
2932

3033
func main() {
3134
flag.StringVar(&NIC, "interface", "enp3s0", "Network interface to attach to.")
@@ -55,18 +58,18 @@ func main() {
5558
dstMAC, _ := hex.DecodeString(DstMAC)
5659

5760
eth := &layers.Ethernet{
58-
SrcMAC: net.HardwareAddr(srcMAC),
59-
DstMAC: net.HardwareAddr(dstMAC),
61+
SrcMAC: net.HardwareAddr(srcMAC),
62+
DstMAC: net.HardwareAddr(dstMAC),
6063
EthernetType: layers.EthernetTypeIPv4,
6164
}
6265
ip := &layers.IPv4{
63-
Version: 4,
64-
IHL: 5,
65-
TTL: 64,
66-
Id: 0,
66+
Version: 4,
67+
IHL: 5,
68+
TTL: 64,
69+
Id: 0,
6770
Protocol: layers.IPProtocolUDP,
68-
SrcIP: net.ParseIP(SrcIP).To4(),
69-
DstIP: net.ParseIP(DstIP).To4(),
71+
SrcIP: net.ParseIP(SrcIP).To4(),
72+
DstIP: net.ParseIP(DstIP).To4(),
7073
}
7174
udp := &layers.UDP{
7275
SrcPort: 1234,
@@ -82,7 +85,7 @@ func main() {
8285

8386
buf := gopacket.NewSerializeBuffer()
8487
opts := gopacket.SerializeOptions{
85-
FixLengths: true,
88+
FixLengths: true,
8689
ComputeChecksums: true,
8790
}
8891
err = gopacket.SerializeLayers(buf, opts, eth, ip, udp, gopacket.Payload(payload))
@@ -94,7 +97,7 @@ func main() {
9497
// Fill all the frames in UMEM with the pre-generated DNS query frame.
9598

9699
descs := xsk.GetDescs(math.MaxInt32)
97-
for i, _ := range descs {
100+
for i := range descs {
98101
frameLen = copy(xsk.GetFrame(descs[i]), buf.Bytes())
99102
}
100103

@@ -104,26 +107,26 @@ func main() {
104107

105108
fmt.Printf("sending DNS queries from %v (%v) to %v (%v) for domain name %s...\n", ip.SrcIP, eth.SrcMAC, ip.DstIP, eth.DstMAC, DomainName)
106109

107-
go func(){
110+
go func() {
108111
var err error
109112
var prev xdp.Stats
110113
var cur xdp.Stats
111114
var numPkts uint64
112115
for i := uint64(0); ; i++ {
113-
time.Sleep(time.Duration(1)*time.Second)
116+
time.Sleep(time.Duration(1) * time.Second)
114117
cur, err = xsk.Stats()
115118
if err != nil {
116119
panic(err)
117120
}
118121
numPkts = cur.Completed - prev.Completed
119-
fmt.Printf("%d packets/s (%d bytes/s)\n", numPkts, numPkts * uint64(frameLen))
122+
fmt.Printf("%d packets/s (%d bytes/s)\n", numPkts, numPkts*uint64(frameLen))
120123
prev = cur
121124
}
122125
}()
123126

124127
for {
125128
descs := xsk.GetDescs(xsk.NumFreeTxSlots())
126-
for i, _ := range descs {
129+
for i := range descs {
127130
descs[i].Len = uint32(frameLen)
128131
}
129132
xsk.Transmit(descs)

examples/sendudp/sendudp.go

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,23 @@ import (
1414
"time"
1515

1616
"github.com/asavie/xdp"
17-
"github.com/vishvananda/netlink"
1817
"github.com/google/gopacket"
1918
"github.com/google/gopacket/layers"
19+
"github.com/vishvananda/netlink"
2020
)
2121

22-
var NIC string
23-
var QueueID int
24-
var SrcMAC string
25-
var DstMAC string
26-
var SrcIP string
27-
var DstIP string
28-
var SrcPort uint
29-
var DstPort uint
30-
var PayloadSize uint
22+
// ...
23+
var (
24+
NIC string
25+
QueueID int
26+
SrcMAC string
27+
DstMAC string
28+
SrcIP string
29+
DstIP string
30+
SrcPort uint
31+
DstPort uint
32+
PayloadSize uint
33+
)
3134

3235
func main() {
3336
flag.StringVar(&NIC, "interface", "ens9", "Network interface to attach to.")
@@ -59,18 +62,18 @@ func main() {
5962
dstMAC, _ := hex.DecodeString(DstMAC)
6063

6164
eth := &layers.Ethernet{
62-
SrcMAC: net.HardwareAddr(srcMAC),
63-
DstMAC: net.HardwareAddr(dstMAC),
65+
SrcMAC: net.HardwareAddr(srcMAC),
66+
DstMAC: net.HardwareAddr(dstMAC),
6467
EthernetType: layers.EthernetTypeIPv4,
6568
}
6669
ip := &layers.IPv4{
67-
Version: 4,
68-
IHL: 5,
69-
TTL: 64,
70-
Id: 0,
70+
Version: 4,
71+
IHL: 5,
72+
TTL: 64,
73+
Id: 0,
7174
Protocol: layers.IPProtocolUDP,
72-
SrcIP: net.ParseIP(SrcIP).To4(),
73-
DstIP: net.ParseIP(DstIP).To4(),
75+
SrcIP: net.ParseIP(SrcIP).To4(),
76+
DstIP: net.ParseIP(DstIP).To4(),
7477
}
7578
udp := &layers.UDP{
7679
SrcPort: layers.UDPPort(SrcPort),
@@ -84,7 +87,7 @@ func main() {
8487

8588
buf := gopacket.NewSerializeBuffer()
8689
opts := gopacket.SerializeOptions{
87-
FixLengths: true,
90+
FixLengths: true,
8891
ComputeChecksums: true,
8992
}
9093
err = gopacket.SerializeLayers(buf, opts, eth, ip, udp, gopacket.Payload(payload))
@@ -96,7 +99,7 @@ func main() {
9699
// Fill all the frames in UMEM with the pre-generated UDP packet.
97100

98101
descs := xsk.GetDescs(math.MaxInt32)
99-
for i, _ := range descs {
102+
for i := range descs {
100103
frameLen = copy(xsk.GetFrame(descs[i]), buf.Bytes())
101104
}
102105

@@ -106,26 +109,26 @@ func main() {
106109

107110
fmt.Printf("sending UDP packets from %v (%v) to %v (%v)...\n", ip.SrcIP, eth.SrcMAC, ip.DstIP, eth.DstMAC)
108111

109-
go func(){
112+
go func() {
110113
var err error
111114
var prev xdp.Stats
112115
var cur xdp.Stats
113116
var numPkts uint64
114117
for i := uint64(0); ; i++ {
115-
time.Sleep(time.Duration(1)*time.Second)
118+
time.Sleep(time.Duration(1) * time.Second)
116119
cur, err = xsk.Stats()
117120
if err != nil {
118121
panic(err)
119122
}
120123
numPkts = cur.Completed - prev.Completed
121-
fmt.Printf("%d packets/s (%d Mb/s)\n", numPkts, (numPkts * uint64(frameLen) * 8)/(1000*1000))
124+
fmt.Printf("%d packets/s (%d Mb/s)\n", numPkts, (numPkts*uint64(frameLen)*8)/(1000*1000))
122125
prev = cur
123126
}
124127
}()
125128

126129
for {
127130
descs := xsk.GetDescs(xsk.NumFreeTxSlots())
128-
for i, _ := range descs {
131+
for i := range descs {
129132
descs[i].Len = uint32(frameLen)
130133
}
131134
xsk.Transmit(descs)

go.mod

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module github.com/asavie/xdp
2+
3+
go 1.13
4+
5+
require (
6+
github.com/google/gopacket v1.1.19
7+
github.com/miekg/dns v1.1.35
8+
github.com/newtools/ebpf v0.1.0
9+
github.com/vishvananda/netlink v1.1.0
10+
golang.org/x/sys v0.0.0-20201204225414-ed752295db88
11+
)

go.sum

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
github.com/google/gopacket v1.1.19 h1:ves8RnFZPGiFnTS0uPQStjwru6uO6h+nlr9j6fL7kF8=
2+
github.com/google/gopacket v1.1.19/go.mod h1:iJ8V8n6KS+z2U1A8pUwu8bW5SyEMkXJB8Yo/Vo+TKTo=
3+
github.com/miekg/dns v1.1.35 h1:oTfOaDH+mZkdcgdIjH6yBajRGtIwcwcaR+rt23ZSrJs=
4+
github.com/miekg/dns v1.1.35/go.mod h1:KNUDUusw/aVsxyTYZM1oqvCicbwhgbNgztCETuNZ7xM=
5+
github.com/newtools/ebpf v0.1.0 h1:9wjL7yOxpC08I7PrTThTUnBZbGvg6PLCnrs4OBosIdE=
6+
github.com/newtools/ebpf v0.1.0/go.mod h1:H74E4gvXfcsOzgaoPSOuQVoKsWQrg2Xbx6+WybLKvyA=
7+
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
8+
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
9+
github.com/vishvananda/netlink v1.1.0 h1:1iyaYNBLmP6L0220aDnYQpo1QEV4t4hJ+xEEhhJH8j0=
10+
github.com/vishvananda/netlink v1.1.0/go.mod h1:cTgwzPIzzgDAYoQrMm0EdrjRUBkTqKYppBueQtXaqoE=
11+
github.com/vishvananda/netns v0.0.0-20191106174202-0a2b9b5464df h1:OviZH7qLw/7ZovXvuNyL3XQl8UFofeikI1NW1Gypu7k=
12+
github.com/vishvananda/netns v0.0.0-20191106174202-0a2b9b5464df/go.mod h1:JP3t17pCcGlemwknint6hfoeCVQrEMVwxRLRjXpq+BU=
13+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
14+
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550 h1:ObdrDkeb4kJdCP557AjRjq69pTHfNouLtWZG7j9rPN8=
15+
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
16+
golang.org/x/lint v0.0.0-20200302205851-738671d3881b/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY=
17+
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
18+
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
19+
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
20+
golang.org/x/net v0.0.0-20190923162816-aa69164e4478 h1:l5EDrHhldLYb3ZRHDUhXF7Om7MvYXnkV9/iQNo1lX6g=
21+
golang.org/x/net v0.0.0-20190923162816-aa69164e4478/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
22+
golang.org/x/sync v0.0.0-20190423024810-112230192c58 h1:8gQV6CLnAEikrhgkHFbMAEhagSSnXWGV915qUMm9mrU=
23+
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
24+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
25+
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
26+
golang.org/x/sys v0.0.0-20190502175342-a43fa875dd82/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
27+
golang.org/x/sys v0.0.0-20190606203320-7fc4e5ec1444/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
28+
golang.org/x/sys v0.0.0-20190924154521-2837fb4f24fe/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
29+
golang.org/x/sys v0.0.0-20201204225414-ed752295db88 h1:KmZPnMocC93w341XZp26yTJg8Za7lhb2KhkYmixoeso=
30+
golang.org/x/sys v0.0.0-20201204225414-ed752295db88/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
31+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
32+
golang.org/x/tools v0.0.0-20191216052735-49a3e744a425/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
33+
golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
34+
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

0 commit comments

Comments
 (0)