-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Florian Lehner <dev@der-flo.net>
- Loading branch information
Showing
18 changed files
with
185 additions
and
22 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,77 @@ | ||
package tc | ||
|
||
import ( | ||
"github.com/florianl/go-tc/internal/unix" | ||
"github.com/mdlayher/netlink" | ||
) | ||
|
||
// Actions represents the actions part of rtnetlink | ||
type Actions struct { | ||
Tc | ||
} | ||
|
||
// tcamsg is Actions specific | ||
type tcaMsg struct { | ||
family uint8 | ||
_ uint8 // pad1 | ||
_ uint16 // pad2 | ||
} | ||
|
||
// Actions allows to read and alter actions | ||
func (tc *Tc) Actions() *Actions { | ||
return &Actions{*tc} | ||
} | ||
|
||
// Add creates a new actions | ||
func (a *Actions) Add(info []*Action) error { | ||
if len(info) == 0 { | ||
return ErrNoArg | ||
} | ||
options, err := validateActionsObject(unix.RTM_NEWACTION, info) | ||
if err != nil { | ||
return err | ||
} | ||
return a.action(unix.RTM_NEWACTION, netlink.Create|netlink.Excl, tcaMsg{ | ||
family: unix.AF_UNSPEC, | ||
}, options) | ||
} | ||
|
||
// Replace add/remove an actions. If the node does not exist yet it is created | ||
func (a *Actions) Replace(info []*Action) error { | ||
if len(info) == 0 { | ||
return ErrNoArg | ||
} | ||
options, err := validateActionsObject(unix.RTM_NEWACTION, info) | ||
if err != nil { | ||
return err | ||
} | ||
return a.action(unix.RTM_NEWACTION, netlink.Create, tcaMsg{ | ||
family: unix.AF_UNSPEC, | ||
}, options) | ||
} | ||
|
||
// Delete removes an actions | ||
func (a *Actions) Delete(info []*Action) error { | ||
if len(info) == 0 { | ||
return ErrNoArg | ||
} | ||
options, err := validateActionsObject(unix.RTM_DELACTION, info) | ||
if err != nil { | ||
return err | ||
} | ||
return a.action(unix.RTM_DELACTION, netlink.HeaderFlags(0), tcaMsg{ | ||
family: unix.AF_UNSPEC, | ||
}, options) | ||
} | ||
|
||
func validateActionsObject(cmd int, info []*Action) ([]tcOption, error) { | ||
options := []tcOption{} | ||
|
||
data, err := marshalActions(cmd, info) | ||
if err != nil { | ||
return options, err | ||
} | ||
options = append(options, tcOption{Interpretation: vtBytes, Type: 1 /*TCA_ROOT_TAB*/, Data: data}) | ||
|
||
return options, 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,50 @@ | ||
//go:build linux | ||
// +build linux | ||
|
||
package tc_test | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/florianl/go-tc" | ||
) | ||
|
||
func ExampleActions() { | ||
tcnl, err := tc.Open(&tc.Config{}) | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "could not open rtnetlink socket: %v\n", err) | ||
return | ||
} | ||
defer func() { | ||
if err := tcnl.Close(); err != nil { | ||
fmt.Fprintf(os.Stderr, "could not close rtnetlink socket: %v\n", err) | ||
} | ||
}() | ||
|
||
// Create a gact Actions. | ||
if err := tcnl.Actions().Add([]*tc.Action{ | ||
{ | ||
Kind: "gact", | ||
Gact: &tc.Gact{ | ||
Parms: &tc.GactParms{ | ||
Action: 2, // drop | ||
}, | ||
}, | ||
}, | ||
}); err != nil { | ||
fmt.Fprintf(os.Stderr, "failed to add actions: %v\n", err) | ||
return | ||
} | ||
|
||
// Delete the gact Actions on Index 1. | ||
if err := tcnl.Actions().Delete([]*tc.Action{ | ||
{ | ||
Kind: "gact", | ||
Index: 1, | ||
}, | ||
}); err != nil { | ||
fmt.Fprintf(os.Stderr, "failed to delete gact actions: %v\n", err) | ||
return | ||
} | ||
} |
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
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
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
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