Skip to content

Commit 076432c

Browse files
Merge Main to Release 1.0 (#90)
* Fixup update flow (#88) * Bump golang.org/x/sys from 0.20.0 to 0.24.0 (#89) Bumps [golang.org/x/sys](https://github.com/golang/sys) from 0.20.0 to 0.24.0. - [Commits](golang/sys@v0.20.0...v0.24.0) --- updated-dependencies: - dependency-name: golang.org/x/sys dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * fix test on rel-branch --------- Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
1 parent 3073c25 commit 076432c

File tree

7 files changed

+164
-33
lines changed

7 files changed

+164
-33
lines changed

go.mod

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ require (
77
github.com/stretchr/testify v1.9.0
88
github.com/vishvananda/netlink v1.1.0
99
go.uber.org/zap v1.27.0
10-
golang.org/x/sys v0.20.0
10+
golang.org/x/sys v0.24.0
1111
gopkg.in/natefinch/lumberjack.v2 v2.2.1
1212
)
1313

@@ -16,8 +16,5 @@ require (
1616
github.com/pmezard/go-difflib v1.0.0 // indirect
1717
github.com/vishvananda/netns v0.0.0-20191106174202-0a2b9b5464df // indirect
1818
go.uber.org/multierr v1.10.0 // indirect
19-
golang.org/x/mod v0.4.2 // indirect
20-
golang.org/x/tools v0.1.1 // indirect
21-
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
2219
gopkg.in/yaml.v3 v3.0.1 // indirect
2320
)

go.sum

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
1818
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
1919
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
2020
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
21-
golang.org/x/mod v0.4.2 h1:Gz96sIWK3OalVv/I/qNygP42zyoKp3xptRVCWRFEBvo=
2221
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
2322
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
2423
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
@@ -31,18 +30,16 @@ golang.org/x/sys v0.0.0-20190606203320-7fc4e5ec1444/go.mod h1:h1NjWce9XRLGQEsW7w
3130
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
3231
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
3332
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
34-
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
35-
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
33+
golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg=
34+
golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
3635
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
3736
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
3837
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
3938
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
4039
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
41-
golang.org/x/tools v0.1.1 h1:wGiQel/hW0NnEkJUk8lbzkX2gFJU6PFxf1v5OlCfuOs=
4240
golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
4341
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
4442
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
45-
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
4643
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
4744
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
4845
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

pkg/maps/loader.go

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -168,11 +168,11 @@ type BpfMapAPIs interface {
168168
// Get map value
169169
GetMapEntry(key, value uintptr) error
170170
// Update multiple map entries
171-
BulkUpdateMapEntry(keyvalue map[uintptr]uintptr) error
171+
BulkUpdateMapEntry(keyvalue map[string][]byte) error
172172
// Delete multiple map entries
173173
BulkDeleteMapEntry(keyvalue map[uintptr]uintptr) error
174174
// Wrapper for delete and update map entries
175-
BulkRefreshMapEntries(newMapContents map[string]uintptr) error
175+
BulkRefreshMapEntries(newMapContents map[string][]byte) error
176176
// Retrieve map info from pin path
177177
GetMapFromPinPath(pinPath string) (BpfMapInfo, error)
178178
}
@@ -456,9 +456,12 @@ func (m *BpfMap) BulkDeleteMapEntry(keyvalue map[uintptr]uintptr) error {
456456
return nil
457457
}
458458

459-
func (m *BpfMap) BulkUpdateMapEntry(keyvalue map[uintptr]uintptr) error {
459+
func (m *BpfMap) BulkUpdateMapEntry(keyvalue map[string][]byte) error {
460460
for k, v := range keyvalue {
461-
err := m.UpdateMapEntry(k, v)
461+
keyByte := []byte(k)
462+
keyPtr := uintptr(unsafe.Pointer(&keyByte[0]))
463+
valuePtr := uintptr(unsafe.Pointer(&v[0]))
464+
err := m.UpdateMapEntry(keyPtr, valuePtr)
462465
if err != nil {
463466
log.Infof("One of the element update failed hence returning from bulk update")
464467
return err
@@ -468,33 +471,23 @@ func (m *BpfMap) BulkUpdateMapEntry(keyvalue map[uintptr]uintptr) error {
468471
return nil
469472
}
470473

471-
func (m *BpfMap) BulkRefreshMapEntries(newMapContents map[string]uintptr) error {
472-
473-
// 1. Construct i/p to bulkMap
474-
keyvaluePtr := make(map[uintptr]uintptr)
475-
476-
for k, v := range newMapContents {
477-
keyByte := []byte(k)
478-
log.Infof("Converted string to bytearray %v", keyByte)
479-
keyPtr := uintptr(unsafe.Pointer(&keyByte[0]))
480-
keyvaluePtr[keyPtr] = v
481-
}
474+
func (m *BpfMap) BulkRefreshMapEntries(newMapContents map[string][]byte) error {
482475

483-
// 2. Update all map entries
484-
err := m.BulkUpdateMapEntry(keyvaluePtr)
476+
// 1. Update all map entries
477+
err := m.BulkUpdateMapEntry(newMapContents)
485478
if err != nil {
486479
log.Errorf("refresh map failed: during update %v", err)
487480
return err
488481
}
489482

490-
// 3. Read all map entries
483+
// 2. Read all map entries
491484
retrievedMapKeyList, err := m.GetAllMapKeys()
492485
if err != nil {
493486
log.Errorf("get all map keys failed: during Refresh %v", err)
494487
return err
495488
}
496489

497-
// 4. Delete stale Keys
490+
// 3. Delete stale Keys
498491
log.Infof("Check for stale entries and got %d entries from BPF map", len(retrievedMapKeyList))
499492
for _, key := range retrievedMapKeyList {
500493
log.Infof("Checking if key %s is deletable", key)

pkg/maps/mocks/ebpf_mocks.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ require (
1414
github.com/vishvananda/netns v0.0.4 // indirect
1515
go.uber.org/multierr v1.10.0 // indirect
1616
go.uber.org/zap v1.27.0 // indirect
17-
golang.org/x/sys v0.20.0 // indirect
17+
golang.org/x/sys v0.24.0 // indirect
1818
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
1919
)
2020

test/go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
2121
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
2222
golang.org/x/sys v0.0.0-20190606203320-7fc4e5ec1444/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
2323
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
24-
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
25-
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
24+
golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg=
25+
golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
2626
gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc=
2727
gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc=
2828
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

test/main.go

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package main
22

33
import (
4+
"encoding/binary"
45
"fmt"
6+
"net"
57
"os"
68
"strings"
79
"syscall"
@@ -60,6 +62,8 @@ func main() {
6062
{Name: "Test loading Maps without Program", Func: TestLoadMapWithNoProg},
6163
{Name: "Test loading Map operations", Func: TestMapOperations},
6264
{Name: "Test updating Map size", Func: TestLoadMapWithCustomSize},
65+
{Name: "Test bulk Map operations", Func: TestBulkMapOperations},
66+
{Name: "Test bulk refresh Map operations", Func: TestBulkRefreshMapOperations},
6367
}
6468

6569
testSummary := make(map[string]string)
@@ -340,3 +344,143 @@ func TestLoadMapWithCustomSize() error {
340344
return nil
341345

342346
}
347+
348+
func TestBulkMapOperations() error {
349+
gosdkClient := goelf.New()
350+
_, loadedMap, err := gosdkClient.LoadBpfFile("c/test-map.bpf.elf", "operations")
351+
if err != nil {
352+
fmt.Println("Load BPF failed", "err:", err)
353+
return err
354+
}
355+
356+
for mapName, _ := range loadedMap {
357+
fmt.Println("Map Info: ", "Name: ", mapName)
358+
}
359+
360+
type BPFInetTrieKey struct {
361+
Prefixlen uint32
362+
Addr [4]byte
363+
}
364+
365+
const numEntries = 32 * 1000 // 32K entries
366+
367+
// Create 32K entries
368+
mapToUpdate, ok := loadedMap["ingress_map"]
369+
if !ok {
370+
return fmt.Errorf("map 'ingress_map' not found")
371+
}
372+
373+
for i := 0; i < numEntries; i++ {
374+
dummykey := BPFInetTrieKey{
375+
Prefixlen: 32,
376+
Addr: [4]byte{byte(192 + i/256), byte(168 + (i/256)%256), byte(i % 256), 0},
377+
}
378+
dummyvalue := uint32(40)
379+
380+
err = mapToUpdate.CreateMapEntry(uintptr(unsafe.Pointer(&dummykey)), uintptr(unsafe.Pointer(&dummyvalue)))
381+
if err != nil {
382+
fmt.Println("Unable to Insert into eBPF map: ", err)
383+
return err
384+
}
385+
}
386+
fmt.Println("Created 32K entries successfully")
387+
388+
// Update 32K entries
389+
for i := 0; i < numEntries; i++ {
390+
dummykey := BPFInetTrieKey{
391+
Prefixlen: 32,
392+
Addr: [4]byte{byte(192 + i/256), byte(168 + (i/256)%256), byte(i % 256), 0},
393+
}
394+
dummyvalue := uint32(20)
395+
396+
err = mapToUpdate.UpdateMapEntry(uintptr(unsafe.Pointer(&dummykey)), uintptr(unsafe.Pointer(&dummyvalue)))
397+
if err != nil {
398+
fmt.Println("Unable to Update into eBPF map: ", err)
399+
return err
400+
}
401+
}
402+
fmt.Println("Updated 32K entries successfully")
403+
404+
return nil
405+
}
406+
407+
func ComputeTrieKey(n net.IPNet) []byte {
408+
prefixLen, _ := n.Mask.Size()
409+
key := make([]byte, 8)
410+
411+
// Set the prefix length
412+
key[0] = byte(prefixLen)
413+
414+
// Set the IP address
415+
copy(key[4:], n.IP.To4())
416+
417+
fmt.Printf("Key: %v\n", key)
418+
return key
419+
}
420+
421+
type BPFInetTrieKey struct {
422+
Prefixlen uint32
423+
Addr [4]byte
424+
}
425+
426+
func bpfInetTrieKeyToIPNet(key BPFInetTrieKey) net.IPNet {
427+
ip := net.IPv4(key.Addr[0], key.Addr[1], key.Addr[2], key.Addr[3])
428+
return net.IPNet{
429+
IP: ip,
430+
Mask: net.CIDRMask(int(key.Prefixlen), 32),
431+
}
432+
}
433+
434+
func TestBulkRefreshMapOperations() error {
435+
gosdkClient := goelf.New()
436+
_, loadedMap, err := gosdkClient.LoadBpfFile("c/test-map.bpf.elf", "operations")
437+
if err != nil {
438+
fmt.Println("Load BPF failed", "err:", err)
439+
return err
440+
}
441+
442+
for mapName, _ := range loadedMap {
443+
fmt.Println("Map Info: ", "Name: ", mapName)
444+
}
445+
446+
const numEntries = 32 * 1000 // 32K entries
447+
// Create 32K entries
448+
mapToUpdate, ok := loadedMap["ingress_map"]
449+
if !ok {
450+
return fmt.Errorf("map 'ingress_map' not found")
451+
}
452+
453+
newMapContents := make(map[string][]byte, numEntries)
454+
for i := 0; i < numEntries; i++ {
455+
dummykey := BPFInetTrieKey{
456+
Prefixlen: 32,
457+
Addr: [4]byte{byte(1 + i/65536), byte(0 + (i/256)%256), byte(i % 256), 0},
458+
}
459+
dummyvalue := uint32(40)
460+
461+
err = mapToUpdate.CreateMapEntry(uintptr(unsafe.Pointer(&dummykey)), uintptr(unsafe.Pointer(&dummyvalue)))
462+
if err != nil {
463+
fmt.Println("Unable to Insert into eBPF map: ", err)
464+
return err
465+
}
466+
dummyvalue = uint32(50)
467+
ipnet := bpfInetTrieKeyToIPNet(dummykey)
468+
fmt.Println(ipnet)
469+
keyByte := ComputeTrieKey(ipnet)
470+
dummyValueByteArray := make([]byte, 4)
471+
binary.LittleEndian.PutUint32(dummyValueByteArray, dummyvalue)
472+
newMapContents[string(keyByte)] = dummyValueByteArray
473+
474+
}
475+
fmt.Println("Created 32K entries successfully")
476+
477+
// Update 32K entries
478+
err = mapToUpdate.BulkRefreshMapEntries(newMapContents)
479+
if err != nil {
480+
fmt.Println("Unable to Bulk Refresh eBPF map: ", err)
481+
return err
482+
}
483+
fmt.Println("Updated 32K entries successfully")
484+
485+
return nil
486+
}

0 commit comments

Comments
 (0)