@@ -21,6 +21,7 @@ package ads
2121
2222import (
2323 "os"
24+ "path/filepath"
2425 "reflect"
2526 "syscall"
2627
@@ -29,7 +30,9 @@ import (
2930
3031 bpf2go "kmesh.net/kmesh/bpf/kmesh/bpf2go/kernelnative/enhanced"
3132 "kmesh.net/kmesh/daemon/options"
33+ "kmesh.net/kmesh/pkg/bpf/restart"
3234 "kmesh.net/kmesh/pkg/bpf/utils"
35+ "kmesh.net/kmesh/pkg/constants"
3336 helper "kmesh.net/kmesh/pkg/utils"
3437)
3538
@@ -81,11 +84,6 @@ func (sc *BpfSockOps) loadKmeshSockopsObjects() (*ebpf.CollectionSpec, error) {
8184 return nil , err
8285 }
8386
84- value := reflect .ValueOf (sc .KmeshSockopsObjects .KmeshSockopsPrograms )
85- if err = utils .PinPrograms (& value , sc .Info .BpfFsPath ); err != nil {
86- return nil , err
87- }
88-
8987 return spec , nil
9088}
9189
@@ -180,18 +178,39 @@ func (sc *BpfSockOps) Load() error {
180178}
181179
182180func (sc * BpfSockOps ) Attach () error {
181+ var err error
183182 cgopt := link.CgroupOptions {
184183 Path : sc .Info .Cgroup2Path ,
185184 Attach : sc .Info .AttachType ,
186185 Program : sc .KmeshSockopsObjects .SockopsProg ,
187186 }
188187
189- lk , err := link .AttachCgroup (cgopt )
190- if err != nil {
188+ // pin bpf_link and bpf_tail_call map
189+ // pin bpf_link, after restart, update prog in bpf_link
190+ // tail_call map cannot pin in SetMapPinType->LoadAndAssign, we pin them independently
191+ // When we need to update tail_call map, delete the old map and then pin the new one.
192+ tailCallmapPinPath := filepath .Join (sc .Info .BpfFsPath , constants .TailCallMap )
193+ progPinPath := filepath .Join (sc .Info .BpfFsPath , constants .Prog_link )
194+ if restart .GetStartType () == restart .Restart {
195+ if sc .Link , err = utils .BpfProgUpdate (progPinPath , cgopt ); err != nil {
196+ return err
197+ }
198+ // Unpin tailcallmap. Considering that kmesh coredump may not have
199+ // this path after an unexpected restart, here we unpin the file by
200+ // directly removing it without doing error handling.
201+ os .Remove (tailCallmapPinPath )
202+ } else {
203+ sc .Link , err = link .AttachCgroup (cgopt )
204+ if err != nil {
205+ return err
206+ }
207+ if err = sc .Link .Pin (progPinPath ); err != nil {
208+ return err
209+ }
210+ }
211+ if err = sc .KmeshSockopsMaps .KmeshTailCallProg .Pin (tailCallmapPinPath ); err != nil {
191212 return err
192213 }
193- sc .Link = lk
194-
195214 return nil
196215}
197216
0 commit comments