@@ -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+
6068func 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
385358func closeMap (m * ebpf.Map ) {
0 commit comments