-
-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
optimize(bpf): Skip attaching {tcp,udp}_send* hooks when cgroup hooks…
… are attached (#197) * optimize(bpf): Skip attaching {tcp,udp}_send* hooks when cgroup hooks are attached * fix missing `tcnl.SetOption(netlink.ExtendedAcknowledge, true)` * remove fallbak dir when not found cgroup v2 root dir * fix `-i any` will cause an error when running on Alibaba Cloud Linux (Aliyun Linux) release 2.1903 LTS
- Loading branch information
Showing
8 changed files
with
179 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package bpf | ||
|
||
import ( | ||
"fmt" | ||
"github.com/cilium/ebpf" | ||
"github.com/cilium/ebpf/link" | ||
"github.com/mozillazg/ptcpdump/internal/log" | ||
) | ||
|
||
func (b *BPF) AttachCgroups(cgroupPath string) error { | ||
if cgroupPath == "" { | ||
b.skipAttachCgroup = true | ||
} | ||
if b.skipAttachCgroup { | ||
return nil | ||
} | ||
|
||
log.Info("attaching cgroup/sock_create") | ||
lk, err := link.AttachCgroup(link.CgroupOptions{ | ||
Path: cgroupPath, | ||
Attach: ebpf.AttachCGroupInetSockCreate, | ||
Program: b.objs.CgroupSockCreate, | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("attach cgroup/sock_create: %w", err) | ||
} | ||
b.links = append(b.links, lk) | ||
|
||
log.Info("attaching cgroup/sock_release") | ||
lk, err = link.AttachCgroup(link.CgroupOptions{ | ||
Path: cgroupPath, | ||
Attach: ebpf.AttachCgroupInetSockRelease, | ||
Program: b.objs.CgroupSockRelease, | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("attach cgroup/sock_release: %w", err) | ||
} | ||
b.links = append(b.links, lk) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package bpf | ||
|
||
import ( | ||
"fmt" | ||
"github.com/mozillazg/ptcpdump/internal/log" | ||
) | ||
|
||
func (b *BPF) attachNatHooks() error { | ||
err := b.attachFentryOrKprobe("nf_nat_packet", | ||
b.objs.FentryNfNatPacket, b.objs.KprobeNfNatPacket) | ||
if err != nil { | ||
log.Infof("%+v", err) | ||
if isProbeNotSupportErr(err) { | ||
log.Info("the kernel does not support netfilter based NAT feature, skip attach kprobe/nf_nat_packet") | ||
} else { | ||
return fmt.Errorf(": %w", err) | ||
} | ||
} | ||
|
||
err = b.attachFentryOrKprobe("nf_nat_manip_pkt", | ||
b.objs.FentryNfNatManipPkt, b.objs.KprobeNfNatManipPkt) | ||
if err != nil { | ||
log.Infof("%+v", err) | ||
if isProbeNotSupportErr(err) { | ||
log.Info("the kernel does not support netfilter based NAT feature, skip attach kprobe/nf_nat_manip_pkt") | ||
} else { | ||
return fmt.Errorf(": %w", err) | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package bpf | ||
|
||
import ( | ||
"github.com/mozillazg/ptcpdump/internal/log" | ||
) | ||
|
||
func (b *BPF) attachNetDevHooks() error { | ||
if !b.opts.hookNetDev { | ||
return nil | ||
} | ||
|
||
err := b.attachFexitOrKprobe("register_netdevice", | ||
nil, b.objs.KprobeRegisterNetdevice, b.objs.KretprobeRegisterNetdevice) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// TODO: refine | ||
err = b.attachFexitOrKprobe("__dev_get_by_index", | ||
nil, nil, b.objs.KretprobeDevGetByIndex) | ||
if err != nil { | ||
log.Infof("%+v", err) | ||
if isProbeNotSupportErr(err) { | ||
err = b.attachFexitOrKprobe("dev_get_by_index", | ||
nil, nil, b.objs.KretprobeDevGetByIndexLegacy) | ||
if err != nil { | ||
return err | ||
} | ||
} else { | ||
return err | ||
} | ||
} | ||
|
||
err = b.attachFentryOrKprobe("__dev_change_net_namespace", | ||
nil, b.objs.KprobeDevChangeNetNamespace) | ||
if err != nil { | ||
log.Infof("%+v", err) | ||
if isProbeNotSupportErr(err) { | ||
err = b.attachFentryOrKprobe("dev_change_net_namespace", | ||
nil, b.objs.KprobeDevChangeNetNamespaceLegacy) | ||
if err != nil { | ||
return err | ||
} | ||
} else { | ||
return err | ||
} | ||
} | ||
|
||
err = b.attachFexitOrKprobe("__dev_change_net_namespace", | ||
nil, nil, b.objs.KretprobeDevChangeNetNamespace) | ||
if err != nil { | ||
log.Infof("%+v", err) | ||
if isProbeNotSupportErr(err) { | ||
err = b.attachFexitOrKprobe("dev_change_net_namespace", | ||
nil, nil, b.objs.KretprobeDevChangeNetNamespaceLegacy) | ||
if err != nil { | ||
return err | ||
} | ||
} else { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters