Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,7 @@ static void *create_struct(struct op_context *ctx, int *err)
}

ctx->value = value;
((ProtobufCMessage *)value)->descriptor = desc;
for (i = 0; i < desc->n_fields; i++) {
const ProtobufCFieldDescriptor *field = desc->fields + i;

Expand Down
8 changes: 5 additions & 3 deletions pkg/bpf/ads/loader_enhanced.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,16 @@ type BpfAds struct {

func NewBpfAds(cfg *options.BpfConfig) (*BpfAds, error) {
sc := &BpfAds{}
sc.TracePoint.NewBpf(cfg)
if err := sc.TracePoint.NewBpf(cfg); err != nil {
return nil, err
}

if err := sc.SockOps.NewBpf(cfg); err != nil {
return sc, err
return nil, err
}

if err := sc.SockConn.NewBpf(cfg); err != nil {
return sc, err
return nil, err
}
return sc, nil
}
Expand Down
40 changes: 31 additions & 9 deletions pkg/bpf/ads/sock_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,16 @@
import "C"
import (
"os"
"path/filepath"
"reflect"
"syscall"

"github.com/cilium/ebpf"
"github.com/cilium/ebpf/link"

"kmesh.net/kmesh/pkg/bpf/restart"
"kmesh.net/kmesh/pkg/constants"

bpf2go "kmesh.net/kmesh/bpf/kmesh/bpf2go/kernelnative/normal"
"kmesh.net/kmesh/daemon/options"
"kmesh.net/kmesh/pkg/bpf/utils"
Expand Down Expand Up @@ -98,11 +102,6 @@
return nil, err
}

value := reflect.ValueOf(sc.KmeshCgroupSockObjects.KmeshCgroupSockPrograms)
if err = utils.PinPrograms(&value, sc.Info.BpfFsPath); err != nil {
return nil, err
}

return spec, nil
}

Expand Down Expand Up @@ -154,18 +153,41 @@
}

func (sc *BpfSockConn) Attach() error {
var err error
cgopt := link.CgroupOptions{
Path: sc.Info.Cgroup2Path,
Attach: sc.Info.AttachType,
Program: sc.KmeshCgroupSockObjects.CgroupConnect4Prog,
}

lk, err := link.AttachCgroup(cgopt)
if err != nil {
// pin bpf_tail_call map
// tail_call map cannot pin in SetMapPinType->LoadAndAssign, we pin them independently
// When we need to update tail_call map, delete the old map and then pin the new one.
tailCallmapPinPath := filepath.Join(sc.Info.BpfFsPath, constants.TailCallMap)
progPinPath := filepath.Join(sc.Info.BpfFsPath, constants.Prog_link)
if restart.GetStartType() == restart.Restart {
if sc.Link, err = utils.BpfProgUpdate(progPinPath, cgopt); err != nil {
return err
}

Check warning on line 171 in pkg/bpf/ads/sock_connection.go

View check run for this annotation

Codecov / codecov/patch

pkg/bpf/ads/sock_connection.go#L170-L171

Added lines #L170 - L171 were not covered by tests

// Unpin tailcallmap. Considering that kmesh coredump may not have
// this path after an unexpected restart, here we unpin the file by
// directly removing it without doing error handling.
os.Remove(tailCallmapPinPath)

} else {
sc.Link, err = link.AttachCgroup(cgopt)
if err != nil {
return err
}

Check warning on line 182 in pkg/bpf/ads/sock_connection.go

View check run for this annotation

Codecov / codecov/patch

pkg/bpf/ads/sock_connection.go#L181-L182

Added lines #L181 - L182 were not covered by tests

if err := sc.Link.Pin(progPinPath); err != nil {
return err
}

Check warning on line 186 in pkg/bpf/ads/sock_connection.go

View check run for this annotation

Codecov / codecov/patch

pkg/bpf/ads/sock_connection.go#L185-L186

Added lines #L185 - L186 were not covered by tests
}
if err = sc.KmeshCgroupSockMaps.KmeshTailCallProg.Pin(tailCallmapPinPath); err != nil {
return err
}
sc.Link = lk

return nil
}

Expand Down
37 changes: 28 additions & 9 deletions pkg/bpf/ads/sock_ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package ads

import (
"os"
"path/filepath"
"reflect"
"syscall"

Expand All @@ -29,7 +30,9 @@ import (

bpf2go "kmesh.net/kmesh/bpf/kmesh/bpf2go/kernelnative/enhanced"
"kmesh.net/kmesh/daemon/options"
"kmesh.net/kmesh/pkg/bpf/restart"
"kmesh.net/kmesh/pkg/bpf/utils"
"kmesh.net/kmesh/pkg/constants"
helper "kmesh.net/kmesh/pkg/utils"
)

Expand Down Expand Up @@ -81,11 +84,6 @@ func (sc *BpfSockOps) loadKmeshSockopsObjects() (*ebpf.CollectionSpec, error) {
return nil, err
}

value := reflect.ValueOf(sc.KmeshSockopsObjects.KmeshSockopsPrograms)
if err = utils.PinPrograms(&value, sc.Info.BpfFsPath); err != nil {
return nil, err
}

return spec, nil
}

Expand Down Expand Up @@ -180,18 +178,39 @@ func (sc *BpfSockOps) Load() error {
}

func (sc *BpfSockOps) Attach() error {
var err error
cgopt := link.CgroupOptions{
Path: sc.Info.Cgroup2Path,
Attach: sc.Info.AttachType,
Program: sc.KmeshSockopsObjects.SockopsProg,
}

lk, err := link.AttachCgroup(cgopt)
if err != nil {
// pin bpf_link and bpf_tail_call map
// pin bpf_link, after restart, update prog in bpf_link
// tail_call map cannot pin in SetMapPinType->LoadAndAssign, we pin them independently
// When we need to update tail_call map, delete the old map and then pin the new one.
tailCallmapPinPath := filepath.Join(sc.Info.BpfFsPath, constants.TailCallMap)
progPinPath := filepath.Join(sc.Info.BpfFsPath, constants.Prog_link)
if restart.GetStartType() == restart.Restart {
if sc.Link, err = utils.BpfProgUpdate(progPinPath, cgopt); err != nil {
return err
}
// Unpin tailcallmap. Considering that kmesh coredump may not have
// this path after an unexpected restart, here we unpin the file by
// directly removing it without doing error handling.
os.Remove(tailCallmapPinPath)
} else {
sc.Link, err = link.AttachCgroup(cgopt)
if err != nil {
return err
}
if err = sc.Link.Pin(progPinPath); err != nil {
return err
}
}
if err = sc.KmeshSockopsMaps.KmeshTailCallProg.Pin(tailCallmapPinPath); err != nil {
return err
}
sc.Link = lk

return nil
}

Expand Down
48 changes: 40 additions & 8 deletions pkg/bpf/ads/trace_point.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,17 @@
package ads

import (
"os"
"path/filepath"
"syscall"

"github.com/cilium/ebpf"
"github.com/cilium/ebpf/link"

bpf2go "kmesh.net/kmesh/bpf/kmesh/bpf2go/kernelnative/enhanced"
"kmesh.net/kmesh/daemon/options"
"kmesh.net/kmesh/pkg/bpf/restart"
"kmesh.net/kmesh/pkg/constants"
helper "kmesh.net/kmesh/pkg/utils"
)

Expand All @@ -34,10 +40,24 @@ type BpfTracePoint struct {
bpf2go.KmeshTracePointObjects
}

func (sc *BpfTracePoint) NewBpf(cfg *options.BpfConfig) {
sc.Info.MapPath = cfg.BpfFsPath
sc.Info.BpfFsPath = cfg.BpfFsPath
func (sc *BpfTracePoint) NewBpf(cfg *options.BpfConfig) error {
sc.Info.MapPath = cfg.BpfFsPath + "/bpf_kmesh/map/"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you list the dirs/files in /sys/fs/bpf? I think these directory structures need to be reviewed to avoid unreasonable directory division.

Copy link
Contributor Author

@lec-bit lec-bit Nov 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[root@kmesh-z2szb bpf_kmesh]# tree
.
|-- map
|   |-- kmesh_cluster
|   |-- kmesh_cluster_stats
|   |-- kmesh_config_map
|   |-- kmesh_events
|   |-- kmesh_listener
|   |-- kmesh_manage
|   |-- kmesh_map1600
|   |-- kmesh_map192
|   |-- kmesh_map296
|   |-- kmesh_map64
|   |-- kmesh_version
|   |-- map_of_cluster_eps
|   |-- map_of_cluster_eps_data
|   |-- map_of_cluster_sock
|   |-- map_of_router_config
|   |-- map_of_sock_storage
|   |-- outer_of_maglev
|   |-- tmp_buf
|   `-- tmp_log_buf
|-- sockconn
|   |-- prog_link
|   `-- tail_call_map
|-- sockops
|   |-- prog_link
|   `-- tail_call_map
`-- tracepoint
    `-- prog_link

5 directories, 24 files

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/sys/fs/bpf

sc.Info.BpfFsPath = cfg.BpfFsPath + "/bpf_kmesh/tracepoint/"
sc.Info.Cgroup2Path = cfg.Cgroup2Path

if err := os.MkdirAll(sc.Info.MapPath,
syscall.S_IRUSR|syscall.S_IWUSR|syscall.S_IXUSR|
syscall.S_IRGRP|syscall.S_IXGRP); err != nil && !os.IsExist(err) {
return err
}

if err := os.MkdirAll(sc.Info.BpfFsPath,
syscall.S_IRUSR|syscall.S_IWUSR|syscall.S_IXUSR|
syscall.S_IRGRP|syscall.S_IXGRP); err != nil && !os.IsExist(err) {
return err
}

return nil
}

func (sc *BpfTracePoint) loadKmeshTracePointObjects() (*ebpf.CollectionSpec, error) {
Expand Down Expand Up @@ -76,17 +96,29 @@ func (sc *BpfTracePoint) Load() error {
}

func (sc *BpfTracePoint) Attach() error {
var err error
tpopt := link.RawTracepointOptions{
Name: "connect_ret",
Program: sc.KmeshTracePointObjects.ConnectRet,
}

lk, err := link.AttachRawTracepoint(tpopt)
if err != nil {
return err
}
sc.Link = lk
pinPath := filepath.Join(sc.Info.BpfFsPath, constants.Prog_link)
if restart.GetStartType() == restart.Restart {
sc.Link, err = link.LoadPinnedLink(pinPath, &ebpf.LoadPinOptions{})
if err != nil {
return err
}
} else {
sc.Link, err = link.AttachRawTracepoint(tpopt)
if err != nil {
return err
}

if err := sc.Link.Pin(pinPath); err != nil {
return err
}

}
return nil
}

Expand Down
6 changes: 2 additions & 4 deletions pkg/bpf/bpf.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,12 @@ func StopMda() error {

func (l *BpfLoader) Stop() {
var err error
if restart.GetExitType() == restart.Restart && l.config.DualEngineEnabled() {
C.deserial_uninit()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why remove C.deserial_uninit()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The deserial logic is updated, and now the default C.deserial_uninit().

log.Infof("kmesh restart, not clean bpf map and prog")
C.deserial_uninit()
if restart.GetExitType() == restart.Restart {
return
}

closeMap(l.versionMap)

if l.config.KernelNativeEnabled() {
if err = l.obj.Stop(); err != nil {
CleanupBpfMap()
Expand Down
Loading