Skip to content

Commit

Permalink
Adding --corruption to tools netem set command (#2271)
Browse files Browse the repository at this point in the history
* adding --corruption to tools netem set command

* added probability

* added test

---------

Co-authored-by: Roman Dodin <dodin.roman@gmail.com>
  • Loading branch information
steiler and hellt authored Oct 31, 2024
1 parent 5c25be6 commit bd5bcbb
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 10 deletions.
22 changes: 14 additions & 8 deletions cmd/tools_netem.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ import (
)

var (
netemNode string
netemInterface string
netemDelay time.Duration
netemJitter time.Duration
netemLoss float64
netemRate uint64
netemNode string
netemInterface string
netemDelay time.Duration
netemJitter time.Duration
netemLoss float64
netemRate uint64
netemCorruption float64
)

func init() {
Expand All @@ -47,6 +48,7 @@ func init() {
netemSetCmd.Flags().Float64VarP(&netemLoss, "loss", "", 0,
"random packet loss expressed in percentage (e.g. 0.1 means 0.1%)")
netemSetCmd.Flags().Uint64VarP(&netemRate, "rate", "", 0, "link rate limit in kbit")
netemSetCmd.Flags().Float64VarP(&netemCorruption, "corruption", "", 0, "random packet corruption probability expressed in percentage (e.g. 0.1 means 0.1%)")

netemSetCmd.MarkFlagRequired("node")
netemSetCmd.MarkFlagRequired("interface")
Expand Down Expand Up @@ -136,7 +138,7 @@ func netemSetFn(_ *cobra.Command, _ []string) error {
return err
}

qdisc, err := tc.SetImpairments(tcnl, netemNode, link, netemDelay, netemJitter, netemLoss, netemRate)
qdisc, err := tc.SetImpairments(tcnl, netemNode, link, netemDelay, netemJitter, netemLoss, netemRate, netemCorruption)
if err != nil {
return err
}
Expand Down Expand Up @@ -169,6 +171,7 @@ func printImpairments(qdiscs []gotc.Object) {
"Jitter",
"Packet Loss",
"Rate (kbit)",
"Corruption",
}

table.SetHeader(header)
Expand All @@ -191,7 +194,7 @@ func qdiscToTableData(qdisc gotc.Object) []string {
log.Errorf("could not get netlink interface by index: %v", err)
}

var delay, jitter, loss, rate string
var delay, jitter, loss, rate, corruption string

ifDisplayName := link.Attrs().Name
if link.Attrs().Alias != "" {
Expand All @@ -207,6 +210,7 @@ func qdiscToTableData(qdisc gotc.Object) []string {
"N/A", // jitter
"N/A", // loss
"N/A", // rate
"N/A", // corruption
}
}

Expand All @@ -220,13 +224,15 @@ func qdiscToTableData(qdisc gotc.Object) []string {

loss = strconv.FormatFloat(float64(qdisc.Netem.Qopt.Loss)/float64(math.MaxUint32)*100, 'f', 2, 64) + "%"
rate = strconv.Itoa(int(qdisc.Netem.Rate.Rate * 8 / 1000))
corruption = strconv.FormatFloat(float64(qdisc.Netem.Corrupt.Probability)/float64(math.MaxUint32)*100, 'f', 2, 64) + "%"

return []string{
ifDisplayName,
delay,
jitter,
loss,
rate,
corruption,
}
}

Expand Down
7 changes: 7 additions & 0 deletions docs/cmd/tools/netem/set.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ With the `containerlab tools netem set` command users can set link impairments o
* delay & jitter
* packet loss
* rate limiting
* packet corruption

/// details | Considerations
Note, that `netem` is a Linux kernel module and it might not be available in particular kernel configurations.
Expand Down Expand Up @@ -59,6 +60,12 @@ Packet loss is specified with the `--loss` flag. The loss is specified in percen

Egress rate limiting is specified with the `--rate` flag. The rate is specified in kbit per second format. Example: value `100` means rate of 100kbit/s.

### corruption

Packet corruption percentage is specified with the `--corruption` flag. Corruption modifies the contents of the packet at a random position based on percentage set.

Example: corruption of 10 means 10% corruption probability for a traffic passing the interface.

## Examples

### Setting delay and jitter
Expand Down
13 changes: 12 additions & 1 deletion internal/tc/tc.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func NewTC(ns int) (*tc.Tc, error) {
}

// SetImpairments sets the impairments on the given interface of a node.
func SetImpairments(tcnl *tc.Tc, nodeName string, link *net.Interface, delay, jitter time.Duration, loss float64, rate uint64) (*tc.Object, error) {
func SetImpairments(tcnl *tc.Tc, nodeName string, link *net.Interface, delay, jitter time.Duration, loss float64, rate uint64, probability float64) (*tc.Object, error) {
err := tcnl.SetOption(netlink.ExtendedAcknowledge, true)
if err != nil {
return nil, fmt.Errorf("could not set option ExtendedAcknowledge: %v", err)
Expand Down Expand Up @@ -59,6 +59,10 @@ func SetImpairments(tcnl *tc.Tc, nodeName string, link *net.Interface, delay, ji

setRate(&qdisc, rate)

if probability > 0 {
setCorruption(&qdisc, probability)
}

err = tcnl.Qdisc().Replace(&qdisc)
if err != nil {
return nil, err
Expand Down Expand Up @@ -117,6 +121,13 @@ func setRate(qdisc *tc.Object, rate uint64) {
}
}

// setCorruption sets the corruption probability and correlation.
func setCorruption(qdisc *tc.Object, probability float64) {
qdisc.Netem.Corrupt = &tc.NetemCorrupt{
Probability: uint32(math.Round(math.MaxUint32 * (probability / float64(100)))),
}
}

// Impairments returns all link impairments of a node.
func Impairments(tcnl *tc.Tc) ([]tc.Object, error) {
qdiscs, err := tcnl.Qdisc().Get()
Expand Down
3 changes: 2 additions & 1 deletion tests/01-smoke/08-tools-cmds.robot
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@ Deploy ${lab-name} lab

Add link impairments
${rc} ${output} = Run And Return Rc And Output
... sudo -E ${CLAB_BIN} --runtime ${runtime} tools netem set -n clab-${lab-name}-l1 -i eth3 --delay 100ms --jitter 2ms --loss 10 --rate 1000
... sudo -E ${CLAB_BIN} --runtime ${runtime} tools netem set -n clab-${lab-name}-l1 -i eth3 --delay 100ms --jitter 2ms --loss 10 --rate 1000 --corruption 2
Log ${output}
Should Be Equal As Integers ${rc} 0
Should Contain ${output} 100ms
Should Contain ${output} 2ms
Should Contain ${output} 10.00%
Should Contain ${output} 1000
Should Contain ${output} 2.00%

Show link impairments
${rc} ${output} = Run And Return Rc And Output
Expand Down

0 comments on commit bd5bcbb

Please sign in to comment.