Repository: kmesh-net/kmesh
Pull request #633 (2024-07-25 06:30:43)

copy bytes optimize
**What type of PR is this?**

<!--
Add one of the following kinds:

/kind bug
/kind cleanup
/kind enhancement
/kind security
/kind documentation
/kind feature

-->

**What this PR does / why we need it**:

**Which issue(s) this PR fixes**:
Fixes #

**Special notes for your reviewer**:

**Does this PR introduce a user-facing change?**:
<!--
If no, just write "NONE" in the release-note block below.
If yes, a release note is required.
-->
```release-note

```



Patch:
diff --git a/pkg/controller/workload/workload_processor.go b/pkg/controller/workload/workload_processor.go
index 23af5391..b4311e9d 100644
--- a/pkg/controller/workload/workload_processor.go
+++ b/pkg/controller/workload/workload_processor.go
@@ -129,7 +129,7 @@ func (p *Processor) storePodFrontendData(uid uint32, ip []byte) error {
 		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 {
@@ -313,7 +313,7 @@ func (p *Processor) storeBackendData(uid uint32, ip []byte, waypoint *workloadap
 	)
 
 	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)
@@ -325,7 +325,7 @@ func (p *Processor) storeBackendData(uid uint32, ip []byte, waypoint *workloadap
 	}
 
 	if waypoint != nil {
-		nets.CopyIpByteFromSlice(&bv.WaypointAddr, &waypoint.GetAddress().Address)
+		nets.CopyIpByteFromSlice(&bv.WaypointAddr, waypoint.GetAddress().Address)
 		bv.WaypointPort = nets.ConvertPortToBigEndian(waypoint.GetHboneMtlsPort())
 	}
 
@@ -395,13 +395,13 @@ func (p *Processor) handleDataWithoutService(workload *workloadapi.Workload) err
 	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)
 			bv.WaypointPort = nets.ConvertPortToBigEndian(waypoint.GetHboneMtlsPort())
 		}
 
 		bk.BackendUid = uid
 
-		nets.CopyIpByteFromSlice(&bv.Ip, &ip)
+		nets.CopyIpByteFromSlice(&bv.Ip, ip)
 		if err = p.bpf.BackendUpdate(&bk, &bv); err != nil {
 			log.Errorf("Update backend map failed, err:%s", err)
 			return err
@@ -444,7 +444,7 @@ func (p *Processor) storeServiceFrontendData(serviceId uint32, service *workload
 
 	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
@@ -467,7 +467,7 @@ func (p *Processor) storeServiceData(serviceName string, waypoint *workloadapi.G
 	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())
 	}
 
diff --git a/pkg/nets/nets.go b/pkg/nets/nets.go
index 542be582..773d0778 100644
--- a/pkg/nets/nets.go
+++ b/pkg/nets/nets.go
@@ -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) {


Test patch:
diff --git a/pkg/controller/workload/workload_processor_test.go b/pkg/controller/workload/workload_processor_test.go
index 0fe2c333..7fe91abe 100644
--- a/pkg/controller/workload/workload_processor_test.go
+++ b/pkg/controller/workload/workload_processor_test.go
@@ -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
diff --git a/pkg/nets/nets_test.go b/pkg/nets/nets_test.go
index 0c9a9dc7..bd77ec44 100644
--- a/pkg/nets/nets_test.go
+++ b/pkg/nets/nets_test.go
@@ -17,6 +17,7 @@
 package nets
 
 import (
+	"net/netip"
 	"testing"
 
 	"github.com/stretchr/testify/assert"
@@ -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)
+		})
+	}
+}