mirror of
https://github.com/vishvananda/netlink
synced 2025-01-23 07:54:09 +00:00
Add clsact and bpf filter support
New in kernel 4.5 is a type of qdisc for lightweight (no qdisc lock) ingress/egress parsing, called clsact. Add support for such a qdisc type. One primary consumer of clsact is the bpf filter, add support for that as well.
This commit is contained in:
parent
6f0327edfd
commit
17b1e35dbc
16
filter.go
16
filter.go
@ -158,6 +158,22 @@ func (filter *Fw) Type() string {
|
||||
return "fw"
|
||||
}
|
||||
|
||||
type BpfFilter struct {
|
||||
FilterAttrs
|
||||
ClassId uint32
|
||||
Fd int
|
||||
Name string
|
||||
DirectAction bool
|
||||
}
|
||||
|
||||
func (filter *BpfFilter) Type() string {
|
||||
return "bpf"
|
||||
}
|
||||
|
||||
func (filter *BpfFilter) Attrs() *FilterAttrs {
|
||||
return &filter.FilterAttrs
|
||||
}
|
||||
|
||||
// GenericFilter filters represent types that are not currently understood
|
||||
// by this netlink library.
|
||||
type GenericFilter struct {
|
||||
|
@ -90,6 +90,21 @@ func FilterAdd(filter Filter) error {
|
||||
native.PutUint32(b, fw.ClassId)
|
||||
nl.NewRtAttrChild(options, nl.TCA_FW_CLASSID, b)
|
||||
}
|
||||
} else if bpf, ok := filter.(*BpfFilter); ok {
|
||||
var bpf_flags uint32
|
||||
if bpf.ClassId != 0 {
|
||||
nl.NewRtAttrChild(options, nl.TCA_BPF_CLASSID, nl.Uint32Attr(bpf.ClassId))
|
||||
}
|
||||
if bpf.Fd >= 0 {
|
||||
nl.NewRtAttrChild(options, nl.TCA_BPF_FD, nl.Uint32Attr((uint32(bpf.Fd))))
|
||||
}
|
||||
if bpf.Name != "" {
|
||||
nl.NewRtAttrChild(options, nl.TCA_BPF_NAME, nl.ZeroTerminated(bpf.Name))
|
||||
}
|
||||
if bpf.DirectAction {
|
||||
bpf_flags |= nl.TCA_BPF_FLAG_ACT_DIRECT
|
||||
}
|
||||
nl.NewRtAttrChild(options, nl.TCA_BPF_FLAGS, nl.Uint32Attr(bpf_flags))
|
||||
}
|
||||
|
||||
req.AddData(options)
|
||||
@ -147,6 +162,8 @@ func FilterList(link Link, parent uint32) ([]Filter, error) {
|
||||
filter = &U32{}
|
||||
case "fw":
|
||||
filter = &Fw{}
|
||||
case "bpf":
|
||||
filter = &BpfFilter{}
|
||||
default:
|
||||
filter = &GenericFilter{FilterType: filterType}
|
||||
}
|
||||
@ -166,6 +183,11 @@ func FilterList(link Link, parent uint32) ([]Filter, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
case "bpf":
|
||||
detailed, err = parseBpfData(filter, data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -319,6 +341,28 @@ func parseFwData(filter Filter, data []syscall.NetlinkRouteAttr) (bool, error) {
|
||||
return detailed, nil
|
||||
}
|
||||
|
||||
func parseBpfData(filter Filter, data []syscall.NetlinkRouteAttr) (bool, error) {
|
||||
native = nl.NativeEndian()
|
||||
bpf := filter.(*BpfFilter)
|
||||
detailed := true
|
||||
for _, datum := range data {
|
||||
switch datum.Attr.Type {
|
||||
case nl.TCA_BPF_FD:
|
||||
bpf.Fd = int(native.Uint32(datum.Value[0:4]))
|
||||
case nl.TCA_BPF_NAME:
|
||||
bpf.Name = string(datum.Value[:len(datum.Value)-1])
|
||||
case nl.TCA_BPF_CLASSID:
|
||||
bpf.ClassId = native.Uint32(datum.Value[0:4])
|
||||
case nl.TCA_BPF_FLAGS:
|
||||
flags := native.Uint32(datum.Value[0:4])
|
||||
if (flags & nl.TCA_BPF_FLAG_ACT_DIRECT) != 0 {
|
||||
bpf.DirectAction = true
|
||||
}
|
||||
}
|
||||
}
|
||||
return detailed, nil
|
||||
}
|
||||
|
||||
func AlignToAtm(size uint) uint {
|
||||
var linksize, cells int
|
||||
cells = int(size / nl.ATM_CELL_PAYLOAD)
|
||||
|
104
filter_test.go
104
filter_test.go
@ -368,3 +368,107 @@ func TestFilterU32BpfAddDel(t *testing.T) {
|
||||
t.Fatal("Failed to remove qdisc")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilterClsActBpfAddDel(t *testing.T) {
|
||||
tearDown := setUpNetlinkTest(t)
|
||||
defer tearDown()
|
||||
if err := LinkAdd(&Ifb{LinkAttrs{Name: "foo"}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
link, err := LinkByName("foo")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := LinkSetUp(link); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
attrs := QdiscAttrs{
|
||||
LinkIndex: link.Attrs().Index,
|
||||
Handle: MakeHandle(0xffff, 0),
|
||||
Parent: HANDLE_CLSACT,
|
||||
}
|
||||
qdisc := &GenericQdisc{
|
||||
QdiscAttrs: attrs,
|
||||
QdiscType: "clsact",
|
||||
}
|
||||
// This feature was added in kernel 4.5
|
||||
if err := QdiscAdd(qdisc); err != nil {
|
||||
t.Skipf("Failed adding clsact qdisc, unsupported kernel")
|
||||
}
|
||||
qdiscs, err := QdiscList(link)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(qdiscs) != 1 {
|
||||
t.Fatal("Failed to add qdisc")
|
||||
}
|
||||
if q, ok := qdiscs[0].(*GenericQdisc); !ok || q.Type() != "clsact" {
|
||||
t.Fatal("qdisc is the wrong type")
|
||||
}
|
||||
|
||||
filterattrs := FilterAttrs{
|
||||
LinkIndex: link.Attrs().Index,
|
||||
Parent: HANDLE_MIN_EGRESS,
|
||||
Handle: MakeHandle(0, 1),
|
||||
Protocol: syscall.ETH_P_ALL,
|
||||
Priority: 1,
|
||||
}
|
||||
fd, err := loadSimpleBpf(BPF_PROG_TYPE_SCHED_CLS)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
filter := &BpfFilter{
|
||||
FilterAttrs: filterattrs,
|
||||
Fd: fd,
|
||||
Name: "simple",
|
||||
DirectAction: true,
|
||||
}
|
||||
if filter.Fd < 0 {
|
||||
t.Skipf("Failed to load bpf program")
|
||||
}
|
||||
|
||||
if err := FilterAdd(filter); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
filters, err := FilterList(link, HANDLE_MIN_EGRESS)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(filters) != 1 {
|
||||
t.Fatal("Failed to add filter")
|
||||
}
|
||||
bpf, ok := filters[0].(*BpfFilter)
|
||||
if !ok {
|
||||
t.Fatal("Filter is the wrong type")
|
||||
}
|
||||
|
||||
if bpf.Fd != filter.Fd {
|
||||
t.Fatal("Filter Fd does not match")
|
||||
}
|
||||
if bpf.DirectAction != filter.DirectAction {
|
||||
t.Fatal("Filter DirectAction does not match")
|
||||
}
|
||||
|
||||
if err := FilterDel(filter); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
filters, err = FilterList(link, HANDLE_MIN_EGRESS)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(filters) != 0 {
|
||||
t.Fatal("Failed to remove filter")
|
||||
}
|
||||
|
||||
if err := QdiscDel(qdisc); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
qdiscs, err = QdiscList(link)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(qdiscs) != 0 {
|
||||
t.Fatal("Failed to remove qdisc")
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user