Skip to content

Commit 7840b4b

Browse files
Optimised handling of address calculations
Signed-off-by: LiZhenCheng9527 <lizhencheng6@huawei.com>
1 parent e99acab commit 7840b4b

File tree

4 files changed

+69
-68
lines changed

4 files changed

+69
-68
lines changed

pkg/bpf/bpf.go

Lines changed: 33 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"context"
2424
"hash/fnv"
2525
"net"
26+
"net/netip"
2627
"os"
2728
"os/exec"
2829
"path/filepath"
@@ -39,6 +40,7 @@ import (
3940
"kmesh.net/kmesh/pkg/bpf/workload"
4041
"kmesh.net/kmesh/pkg/constants"
4142
"kmesh.net/kmesh/pkg/logger"
43+
"kmesh.net/kmesh/pkg/nets"
4244
"kmesh.net/kmesh/pkg/utils"
4345
"kmesh.net/kmesh/pkg/version"
4446
)
@@ -57,6 +59,12 @@ type BpfLoader struct {
5759
versionMap *ebpf.Map
5860
}
5961

62+
type KmeshBpfConfig struct {
63+
BpfLogLevel uint32
64+
NodeIP [16]byte
65+
PodGateway [16]byte
66+
}
67+
6068
func NewBpfLoader(config *options.BpfConfig) *BpfLoader {
6169
return &BpfLoader{
6270
config: config,
@@ -94,6 +102,8 @@ func (l *BpfLoader) Start() error {
94102
return err
95103
}
96104
l.kmeshConfig = l.workloadObj.GetKmeshConfigMap()
105+
// TODO: set bpf prog option in kernel native node
106+
l.setBpfProgOptions()
97107
}
98108

99109
// TODO: move start mds out of bpf loader
@@ -103,8 +113,6 @@ func (l *BpfLoader) Start() error {
103113
}
104114
}
105115

106-
l.UpdateBpfProgOptions()
107-
108116
if restart.GetStartType() == restart.Restart {
109117
log.Infof("bpf load from last pinPath")
110118
}
@@ -271,10 +279,10 @@ func recoverVersionMap(pinPath string) *ebpf.Map {
271279
return versionMap
272280
}
273281

274-
func (l *BpfLoader) UpdateBpfProgOptions() {
282+
func (l *BpfLoader) setBpfProgOptions() {
275283
nodeName := os.Getenv("NODE_NAME")
276284
if nodeName == "" {
277-
log.Errorf("skip kubelet probe failed: %s", "node name empty")
285+
log.Error("skip kubelet probe failed: node name empty")
278286
return
279287
}
280288

@@ -291,13 +299,13 @@ func (l *BpfLoader) UpdateBpfProgOptions() {
291299
}
292300

293301
// pass node ip and pod gateway to skip processing of kubelet access traffic.
294-
nodeIP := getNodeIPAddress(nodeName, node)
295-
gateway := getNodePodSubGateway(nodeName, node)
302+
nodeIP := getNodeIPAddress(node)
303+
gateway := getNodePodSubGateway(node)
296304

297305
keyOfKmeshBpfConfig := uint32(0)
298-
ValueOfKmeshBpfConfig := constants.KmeshBpfConfig{
306+
ValueOfKmeshBpfConfig := KmeshBpfConfig{
299307
// Write this map only when the kmesh daemon starts, so set bpfloglevel to the default value.
300-
BpfLogLevel: uint32(2),
308+
BpfLogLevel: constants.BPF_LOG_INFO,
301309
NodeIP: nodeIP,
302310
PodGateway: gateway,
303311
}
@@ -312,7 +320,7 @@ func (l *BpfLoader) UpdateBpfProgOptions() {
312320
}
313321
}
314322

315-
func getNodeIPAddress(nodeName string, node *corev1.Node) [4]uint32 {
323+
func getNodeIPAddress(node *corev1.Node) [16]byte {
316324
var nodeIPStr string
317325
nodeAddresses := node.Status.Addresses
318326
for _, address := range nodeAddresses {
@@ -321,65 +329,30 @@ func getNodeIPAddress(nodeName string, node *corev1.Node) [4]uint32 {
321329
}
322330
}
323331

324-
nodeIP := net.ParseIP(nodeIPStr)
325-
nodeIPToUint := IPToUint32(nodeIP)
332+
nodeIP, err := netip.ParseAddr(nodeIPStr)
333+
if err != nil {
334+
log.Errorf("failed to parse node ip: %v", err)
335+
return [16]byte{}
336+
}
326337

327-
return nodeIPToUint
338+
return nodeIP.As16()
328339
}
329340

330-
func getNodePodSubGateway(nodeName string, node *corev1.Node) [4]uint32 {
341+
func getNodePodSubGateway(node *corev1.Node) [16]byte {
331342
podCIDR := node.Spec.PodCIDR
332-
ip, _, err := net.ParseCIDR(podCIDR)
343+
_, subNet, err := net.ParseCIDR(podCIDR)
333344
if err != nil {
334345
log.Errorf("failed to resolve ip from podCIDR: %v", err)
335-
return [4]uint32{0, 0, 0, 0}
336-
}
337-
338-
podGateway := IPToUint32(ip)
339-
podGateway[3] = podGateway[3] + 1<<24
340-
return podGateway
341-
}
342-
343-
func IPToUint32(ip net.IP) [4]uint32 {
344-
ipToUint32 := [4]uint32{0, 0, 0, 0}
345-
if isIPv6(ip) {
346-
ipToUint32[0] = binaryToUint32(ip[:4])
347-
ipToUint32[1] = binaryToUint32(ip[4:8])
348-
ipToUint32[2] = binaryToUint32(ip[8:12])
349-
ipToUint32[3] = binaryToUint32(ip[12:16])
350-
} else {
351-
if len(ip) == 16 {
352-
// ipv4 to ipv6
353-
ipToUint32[3] = binaryToUint32(ip[12:16])
354-
} else {
355-
ipToUint32[3] = binaryToUint32(ip)
356-
}
346+
return [16]byte{0}
357347
}
358-
359-
return ipToUint32
360-
}
361-
362-
func isIPv6(ip net.IP) bool {
363-
if len(ip) == 16 {
364-
for i := 0; i < 10; i++ {
365-
if ip[i] != 0 {
366-
return true
367-
}
368-
}
369-
370-
if ip[10] != 0xff {
371-
return true
372-
}
373-
374-
if ip[11] != 0xff {
375-
return true
376-
}
348+
podGateway := [16]byte{0}
349+
nets.CopyIpByteFromSlice(&podGateway, subNet.IP.To16())
350+
if err != nil {
351+
log.Errorf("failed to parse pod gateway: %v", err)
352+
return [16]byte{}
377353
}
378-
return false
379-
}
380-
381-
func binaryToUint32(ip net.IP) uint32 {
382-
return uint32(ip[3])<<24 + uint32(ip[2])<<16 + uint32(ip[1])<<8 + uint32(ip[0])
354+
podGateway[15] = podGateway[15] + 1
355+
return podGateway
383356
}
384357

385358
func closeMap(m *ebpf.Map) {

pkg/bpf/bpf_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,11 @@ import (
2222
"syscall"
2323
"testing"
2424

25+
"reflect"
26+
2527
"github.com/cilium/ebpf/rlimit"
2628
"github.com/stretchr/testify/assert"
29+
corev1 "k8s.io/api/core/v1"
2730

2831
"kmesh.net/kmesh/daemon/options"
2932
"kmesh.net/kmesh/pkg/bpf/restart"
@@ -106,3 +109,33 @@ func runTestRestart(t *testing.T) {
106109
restart.SetExitType(restart.Normal)
107110
bpfLoader.Stop()
108111
}
112+
113+
func TestGetNodePodSubGateway(t *testing.T) {
114+
type args struct {
115+
node *corev1.Node
116+
}
117+
tests := []struct {
118+
name string
119+
args args
120+
want [16]byte
121+
}{
122+
{
123+
name: "test Generated nodeIP",
124+
args: args{
125+
node: &corev1.Node{
126+
Spec: corev1.NodeSpec{
127+
PodCIDR: "10.244.0.0/24",
128+
},
129+
},
130+
},
131+
want: [16]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 10, 244, 0, 1},
132+
},
133+
}
134+
for _, tt := range tests {
135+
t.Run(tt.name, func(t *testing.T) {
136+
if got := getNodePodSubGateway(tt.args.node); !reflect.DeepEqual(got, tt.want) {
137+
assert.Equal(t, tt.want, got)
138+
}
139+
})
140+
}
141+
}

pkg/constants/constants.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,3 @@ const (
6161
VersionPath = "/bpf_kmesh/map/"
6262
WorkloadVersionPath = "/bpf_kmesh_workload/map/"
6363
)
64-
65-
type KmeshBpfConfig struct {
66-
BpfLogLevel uint32
67-
NodeIP [4]uint32
68-
PodGateway [4]uint32
69-
}

pkg/status/status_server.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
adminv2 "kmesh.net/kmesh/api/v2/admin"
3434
"kmesh.net/kmesh/api/v2/workloadapi/security"
3535
"kmesh.net/kmesh/daemon/options"
36+
"kmesh.net/kmesh/pkg/bpf"
3637
bpfads "kmesh.net/kmesh/pkg/bpf/ads"
3738
maps_v2 "kmesh.net/kmesh/pkg/cache/v2/maps"
3839
"kmesh.net/kmesh/pkg/constants"
@@ -407,7 +408,7 @@ func (s *Server) readyProbe(w http.ResponseWriter, r *http.Request) {
407408

408409
func (s *Server) getBpfLogLevel() (*LoggerInfo, error) {
409410
key := uint32(0)
410-
value := constants.KmeshBpfConfig{}
411+
value := bpf.KmeshBpfConfig{}
411412
if err := s.kmeshConfigMap.Lookup(&key, &value); err != nil {
412413
return nil, fmt.Errorf("get log level error: %v", err)
413414
}
@@ -451,7 +452,7 @@ func (s *Server) setBpfLogLevel(w http.ResponseWriter, levelStr string) {
451452
return
452453
}
453454
key := uint32(0)
454-
value := constants.KmeshBpfConfig{}
455+
value := bpf.KmeshBpfConfig{}
455456
if s.kmeshConfigMap == nil {
456457
http.Error(w, fmt.Sprintf("update log level error: %v", "kmeshConfigMap is nil"), http.StatusBadRequest)
457458
return

0 commit comments

Comments
 (0)