forked from vishvananda/netlink
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'vishvananda:main' into main
- Loading branch information
Showing
39 changed files
with
2,033 additions
and
146 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package netlink | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
// Chain contains the attributes of a Chain | ||
type Chain struct { | ||
Parent uint32 | ||
Chain uint32 | ||
} | ||
|
||
func (c Chain) String() string { | ||
return fmt.Sprintf("{Parent: %d, Chain: %d}", c.Parent, c.Chain) | ||
} | ||
|
||
func NewChain(parent uint32, chain uint32) Chain { | ||
return Chain{ | ||
Parent: parent, | ||
Chain: chain, | ||
} | ||
} |
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,112 @@ | ||
package netlink | ||
|
||
import ( | ||
"github.com/vishvananda/netlink/nl" | ||
"golang.org/x/sys/unix" | ||
) | ||
|
||
// ChainDel will delete a chain from the system. | ||
func ChainDel(link Link, chain Chain) error { | ||
// Equivalent to: `tc chain del $chain` | ||
return pkgHandle.ChainDel(link, chain) | ||
} | ||
|
||
// ChainDel will delete a chain from the system. | ||
// Equivalent to: `tc chain del $chain` | ||
func (h *Handle) ChainDel(link Link, chain Chain) error { | ||
return h.chainModify(unix.RTM_DELCHAIN, 0, link, chain) | ||
} | ||
|
||
// ChainAdd will add a chain to the system. | ||
// Equivalent to: `tc chain add` | ||
func ChainAdd(link Link, chain Chain) error { | ||
return pkgHandle.ChainAdd(link, chain) | ||
} | ||
|
||
// ChainAdd will add a chain to the system. | ||
// Equivalent to: `tc chain add` | ||
func (h *Handle) ChainAdd(link Link, chain Chain) error { | ||
return h.chainModify( | ||
unix.RTM_NEWCHAIN, | ||
unix.NLM_F_CREATE|unix.NLM_F_EXCL, | ||
link, | ||
chain) | ||
} | ||
|
||
func (h *Handle) chainModify(cmd, flags int, link Link, chain Chain) error { | ||
req := h.newNetlinkRequest(cmd, flags|unix.NLM_F_ACK) | ||
index := int32(0) | ||
if link != nil { | ||
base := link.Attrs() | ||
h.ensureIndex(base) | ||
index = int32(base.Index) | ||
} | ||
msg := &nl.TcMsg{ | ||
Family: nl.FAMILY_ALL, | ||
Ifindex: index, | ||
Parent: chain.Parent, | ||
} | ||
req.AddData(msg) | ||
req.AddData(nl.NewRtAttr(nl.TCA_CHAIN, nl.Uint32Attr(chain.Chain))) | ||
|
||
_, err := req.Execute(unix.NETLINK_ROUTE, 0) | ||
return err | ||
} | ||
|
||
// ChainList gets a list of chains in the system. | ||
// Equivalent to: `tc chain list`. | ||
// The list can be filtered by link. | ||
func ChainList(link Link, parent uint32) ([]Chain, error) { | ||
return pkgHandle.ChainList(link, parent) | ||
} | ||
|
||
// ChainList gets a list of chains in the system. | ||
// Equivalent to: `tc chain list`. | ||
// The list can be filtered by link. | ||
func (h *Handle) ChainList(link Link, parent uint32) ([]Chain, error) { | ||
req := h.newNetlinkRequest(unix.RTM_GETCHAIN, unix.NLM_F_DUMP) | ||
index := int32(0) | ||
if link != nil { | ||
base := link.Attrs() | ||
h.ensureIndex(base) | ||
index = int32(base.Index) | ||
} | ||
msg := &nl.TcMsg{ | ||
Family: nl.FAMILY_ALL, | ||
Ifindex: index, | ||
Parent: parent, | ||
} | ||
req.AddData(msg) | ||
|
||
msgs, err := req.Execute(unix.NETLINK_ROUTE, unix.RTM_NEWCHAIN) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var res []Chain | ||
for _, m := range msgs { | ||
msg := nl.DeserializeTcMsg(m) | ||
|
||
attrs, err := nl.ParseRouteAttr(m[msg.Len():]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// skip chains from other interfaces | ||
if link != nil && msg.Ifindex != index { | ||
continue | ||
} | ||
|
||
var chain Chain | ||
for _, attr := range attrs { | ||
switch attr.Attr.Type { | ||
case nl.TCA_CHAIN: | ||
chain.Chain = native.Uint32(attr.Value) | ||
chain.Parent = parent | ||
} | ||
} | ||
res = append(res, chain) | ||
} | ||
|
||
return res, 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,77 @@ | ||
//go:build linux | ||
// +build linux | ||
|
||
package netlink | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestChainAddDel(t *testing.T) { | ||
tearDown := setUpNetlinkTest(t) | ||
defer tearDown() | ||
if err := LinkAdd(&Ifb{LinkAttrs{Name: "foo"}}); err != nil { | ||
t.Fatal(err) | ||
} | ||
if err := LinkAdd(&Ifb{LinkAttrs{Name: "bar"}}); err != nil { | ||
t.Fatal(err) | ||
} | ||
link, err := LinkByName("foo") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if err := LinkSetUp(link); err != nil { | ||
t.Fatal(err) | ||
} | ||
qdisc := &Ingress{ | ||
QdiscAttrs: QdiscAttrs{ | ||
LinkIndex: link.Attrs().Index, | ||
Handle: MakeHandle(0xffff, 0), | ||
Parent: HANDLE_INGRESS, | ||
}, | ||
} | ||
if err := QdiscAdd(qdisc); err != nil { | ||
t.Fatal(err) | ||
} | ||
qdiscs, err := SafeQdiscList(link) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if len(qdiscs) != 1 { | ||
t.Fatal("Failed to add qdisc") | ||
} | ||
_, ok := qdiscs[0].(*Ingress) | ||
if !ok { | ||
t.Fatal("Qdisc is the wrong type") | ||
} | ||
chainVal := new(uint32) | ||
*chainVal = 20 | ||
chain := NewChain(HANDLE_INGRESS, *chainVal) | ||
err = ChainAdd(link, chain) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
chains, err := ChainList(link, HANDLE_INGRESS) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if len(chains) != 1 { | ||
t.Fatal("Failed to add chain") | ||
} | ||
if chains[0].Chain != *chainVal { | ||
t.Fatal("Incorrect chain added") | ||
} | ||
if chains[0].Parent != HANDLE_INGRESS { | ||
t.Fatal("Incorrect chain parent") | ||
} | ||
if err := ChainDel(link, chain); err != nil { | ||
t.Fatal(err) | ||
} | ||
chains, err = ChainList(link, HANDLE_INGRESS) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if len(chains) != 0 { | ||
t.Fatal("Failed to remove chain") | ||
} | ||
} |
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
Oops, something went wrong.