Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

copy bytes optimize #633

Merged
merged 3 commits into from
Jul 28, 2024
Merged
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
14 changes: 7 additions & 7 deletions pkg/controller/workload/workload_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@
fv = bpf.FrontendValue{}
)

nets.CopyIpByteFromSlice(&fk.Ip, &ip)
nets.CopyIpByteFromSlice(&fk.Ip, ip)

fv.UpstreamId = uid
if err := p.bpf.FrontendUpdate(&fk, &fv); err != nil {
Expand Down Expand Up @@ -313,7 +313,7 @@
)

bk.BackendUid = uid
nets.CopyIpByteFromSlice(&bv.Ip, &ip)
nets.CopyIpByteFromSlice(&bv.Ip, ip)
bv.ServiceCount = 0
for serviceName := range portList {
bv.Services[bv.ServiceCount] = p.hashName.StrToNum(serviceName)
Expand All @@ -325,7 +325,7 @@
}

if waypoint != nil {
nets.CopyIpByteFromSlice(&bv.WaypointAddr, &waypoint.GetAddress().Address)
nets.CopyIpByteFromSlice(&bv.WaypointAddr, waypoint.GetAddress().Address)

Check warning on line 328 in pkg/controller/workload/workload_processor.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/workload/workload_processor.go#L328

Added line #L328 was not covered by tests
bv.WaypointPort = nets.ConvertPortToBigEndian(waypoint.GetHboneMtlsPort())
}

Expand Down Expand Up @@ -395,13 +395,13 @@
ips := workload.GetAddresses()
for _, ip := range ips {
if waypoint := workload.GetWaypoint(); waypoint != nil {
nets.CopyIpByteFromSlice(&bv.WaypointAddr, &waypoint.GetAddress().Address)
nets.CopyIpByteFromSlice(&bv.WaypointAddr, waypoint.GetAddress().Address)

Check warning on line 398 in pkg/controller/workload/workload_processor.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/workload/workload_processor.go#L398

Added line #L398 was not covered by tests
bv.WaypointPort = nets.ConvertPortToBigEndian(waypoint.GetHboneMtlsPort())
}

bk.BackendUid = uid

nets.CopyIpByteFromSlice(&bv.Ip, &ip)
nets.CopyIpByteFromSlice(&bv.Ip, ip)

Check warning on line 404 in pkg/controller/workload/workload_processor.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/workload/workload_processor.go#L404

Added line #L404 was not covered by tests
if err = p.bpf.BackendUpdate(&bk, &bv); err != nil {
log.Errorf("Update backend map failed, err:%s", err)
return err
Expand Down Expand Up @@ -444,7 +444,7 @@

fv.UpstreamId = serviceId
for _, networkAddress := range service.GetAddresses() {
nets.CopyIpByteFromSlice(&fk.Ip, &networkAddress.Address)
nets.CopyIpByteFromSlice(&fk.Ip, networkAddress.Address)
if err = p.bpf.FrontendUpdate(&fk, &fv); err != nil {
log.Errorf("Update Frontend failed, err:%s", err)
return err
Expand All @@ -467,7 +467,7 @@
newValue := bpf.ServiceValue{}
newValue.LbPolicy = LbPolicyRandom
if waypoint != nil {
nets.CopyIpByteFromSlice(&newValue.WaypointAddr, &waypoint.GetAddress().Address)
nets.CopyIpByteFromSlice(&newValue.WaypointAddr, waypoint.GetAddress().Address)
newValue.WaypointPort = nets.ConvertPortToBigEndian(waypoint.GetHboneMtlsPort())
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/workload/workload_processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func checkBackendMap(t *testing.T, p *Processor, workloadID uint32, wl *workload
func checkFrontEndMap(t *testing.T, ip []byte, p *Processor) (upstreamId uint32) {
var fk bpfcache.FrontendKey
var fv bpfcache.FrontendValue
nets.CopyIpByteFromSlice(&fk.Ip, &ip)
nets.CopyIpByteFromSlice(&fk.Ip, ip)
err := p.bpf.FrontendLookup(&fk, &fv)
assert.NoError(t, err)
upstreamId = fv.UpstreamId
Expand Down
9 changes: 3 additions & 6 deletions pkg/nets/nets.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,12 @@ func ConvertPortToBigEndian(little uint32) uint32 {
return uint32(big16)
}

func CopyIpByteFromSlice(dst *[16]byte, src *[]byte) {
len := len(*src)
func CopyIpByteFromSlice(dst *[16]byte, src []byte) {
len := len(src)
if len != 4 && len != 16 {
return
}

for i := 0; i < len; i++ {
(*dst)[i] = (*src)[i]
}
copy(dst[:], src)
}

func checkIPVersion() (ipv4, ipv6 bool) {
Expand Down
36 changes: 36 additions & 0 deletions pkg/nets/nets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package nets

import (
"net/netip"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -31,3 +32,38 @@ func Test_ConvertIpToUint32(t *testing.T) {
val = ConvertIpToUint32("a.b.c.d")
assert.Equal(t, uint32(0), val)
}

func TestCopyIpByteFromSlice(t *testing.T) {
v6addr, _ := netip.ParseAddr("2001::1")
v6Slices := v6addr.AsSlice()
testcases := []struct {
name string
input []byte
expected [16]byte
}{
{
name: "ipv4",
input: []byte{192, 168, 1, 1},
expected: [16]byte{192, 168, 1, 1},
},
{
name: "ipv6",
input: v6Slices,
expected: [16]byte{0x20, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1},
},
{
name: "invalid",
input: []byte{192, 168, 1, 1, 1, 1},
expected: [16]byte{},
},
}

for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
var out [16]byte

CopyIpByteFromSlice(&out, tc.input)
assert.Equal(t, tc.expected, out)
})
}
}
Loading