Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add json output format for tools netem show #2454

Merged
merged 9 commits into from
Feb 12, 2025
Merged

Add json output format for tools netem show #2454

merged 9 commits into from
Feb 12, 2025

Conversation

FloSch62
Copy link
Member

@FloSch62 FloSch62 commented Feb 8, 2025

This PR brings -format json to containerlab tools netem show --node clab-vlan-srl1 --format json
And we do a modprobe sch_netem first before setting an impairment to ensure rhel/fedora can directly use it.

./containerlab tools netem show --node clab-vlan-srl1 --format json
[
  {
    "Interface": "lo",
    "NodeName": "clab-vlan-srl1",
    "Delay": "0s",
    "Jitter": "0s",
    "PacketLoss": "0.00%",
    "Rate": "0",
    "Corruption": "0.00%"
  },
  {
    "Interface": "mgmt0",
    "NodeName": "clab-vlan-srl1",
    "Delay": "0s",
    "Jitter": "0s",
    "PacketLoss": "0.00%",
    "Rate": "0",
    "Corruption": "0.00%"
  },
  {
    "Interface": "e1-1",
    "NodeName": "clab-vlan-srl1",
    "Delay": "1s",
    "Jitter": "0s",
    "PacketLoss": "0.00%",
    "Rate": "0",
    "Corruption": "0.00%"
  },
  {
    "Interface": "e1-10",
    "NodeName": "clab-vlan-srl1",
    "Delay": "0s",
    "Jitter": "0s",
    "PacketLoss": "0.00%",
    "Rate": "0",
    "Corruption": "0.00%"
  },
  {
    "Interface": "gway-2800",
    "NodeName": "clab-vlan-srl1",
    "Delay": "0s",
    "Jitter": "0s",
    "PacketLoss": "0.00%",
    "Rate": "0",
    "Corruption": "0.00%"
  },
  {
    "Interface": "monit_in",
    "NodeName": "clab-vlan-srl1",
    "Delay": "0s",
    "Jitter": "0s",
    "PacketLoss": "0.00%",
    "Rate": "0",
    "Corruption": "0.00%"
  },
  {
    "Interface": "mgmt0-0 (mgmt0.0)",
    "NodeName": "clab-vlan-srl1",
    "Delay": "0s",
    "Jitter": "0s",
    "PacketLoss": "0.00%",
    "Rate": "0",
    "Corruption": "0.00%"
  }
]

closes #1848

@FloSch62 FloSch62 requested a review from hellt February 8, 2025 13:00
@hellt
Copy link
Member

hellt commented Feb 8, 2025

@FloSch62 would you find some juice to add a robot test for this? Or I can do it later, both is fine

@FloSch62
Copy link
Member Author

FloSch62 commented Feb 8, 2025

Sure

@FloSch62
Copy link
Member Author

FloSch62 commented Feb 8, 2025

@hellt Done

cmd/tools_netem.go Outdated Show resolved Hide resolved
cmd/tools_netem.go Outdated Show resolved Hide resolved
cmd/tools_netem.go Outdated Show resolved Hide resolved
@FloSch62
Copy link
Member Author

FloSch62 commented Feb 9, 2025

agreeing to all point of @vista-

{
  "clab-vlan-srl1": [
    {
      "interface": "lo",
      "delay": "N/A",
      "jitter": "N/A",
      "packet_loss": "N/A",
      "rate": "N/A",
      "corruption": "N/A"
    },
    {
      "interface": "mgmt0",
      "delay": "N/A",
      "jitter": "N/A",
      "packet_loss": "N/A",
      "rate": "N/A",
      "corruption": "N/A"
    },
    {
      "interface": "e1-1",
      "delay": "N/A",
      "jitter": "N/A",
      "packet_loss": "N/A",
      "rate": "N/A",
      "corruption": "N/A"
    },
    {
      "interface": "e1-10",
      "delay": "N/A",
      "jitter": "N/A",
      "packet_loss": "N/A",
      "rate": "N/A",
      "corruption": "N/A"
    },
    {
      "interface": "gway-2801",
      "delay": "N/A",
      "jitter": "N/A",
      "packet_loss": "N/A",
      "rate": "N/A",
      "corruption": "N/A"
    },
    {
      "interface": "monit_in",
      "delay": "N/A",
      "jitter": "N/A",
      "packet_loss": "N/A",
      "rate": "N/A",
      "corruption": "N/A"
    },
    {
      "interface": "mgmt0-0 (mgmt0.0)",
      "delay": "N/A",
      "jitter": "N/A",
      "packet_loss": "N/A",
      "rate": "N/A",
      "corruption": "N/A"
    }
  ]
}

@kaelemc
Copy link
Contributor

kaelemc commented Feb 9, 2025

IMO it should be an empty string instead of "N/A"

@vista-
Copy link
Contributor

vista- commented Feb 9, 2025

Agree with @kaelemc, let the formatter handle nil values (and represent those in the proper data format). In this case, maybe representing the values as *float64 in the type could help represent nil values better?

@FloSch62
Copy link
Member Author

Agree, if someone wants to change it, feel free to do so. I would need to find some time

@hellt hellt changed the title Netem json Add json output format for tools netem show Feb 12, 2025
Comment on lines +285 to +302
if qdisc.Netem.Latency64 != nil && *qdisc.Netem.Latency64 != 0 {
delay = (time.Duration(*qdisc.Netem.Latency64) * time.Nanosecond).String()
}
if qdisc.Netem.Jitter64 != nil && *qdisc.Netem.Jitter64 != 0 {
jitter = (time.Duration(*qdisc.Netem.Jitter64) * time.Nanosecond).String()
}
if qdisc.Netem.Rate != nil && int(qdisc.Netem.Rate.Rate) != 0 {
rate = int(qdisc.Netem.Rate.Rate * 8 / 1000)
}
if qdisc.Netem.Corrupt != nil && qdisc.Netem.Corrupt.Probability != 0 {
// round to 2 decimal places
corruption = math.Round((float64(qdisc.Netem.Corrupt.Probability)/
float64(math.MaxUint32)*100)*100) / 100
}
if qdisc.Netem.Qopt.Loss != 0 {
// round to 2 decimal places
loss = math.Round((float64(qdisc.Netem.Qopt.Loss)/float64(math.MaxUint32)*100)*100) / 100
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kaelemc @vista- @FloSch62

I have changed the output types to be float/int/string for the respecting netem values, with empty values represented by go types.

here is the output they produce.

    {
      "interface": "mgmt0",
      "delay": "1s",
      "jitter": "5ms",
      "packet_loss": 0.1,
      "rate": 0,
      "corruption": 0.2
    },

Everyone OK with the change?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, looks good.

@hellt hellt merged commit 6495894 into main Feb 12, 2025
67 checks passed
@hellt hellt deleted the netem_json branch February 12, 2025 20:10
Copy link

codecov bot commented Feb 12, 2025

Codecov Report

Attention: Patch coverage is 80.59701% with 13 lines in your changes missing coverage. Please review.

Project coverage is 52.60%. Comparing base (f861ff6) to head (d2d5970).
Report is 7 commits behind head on main.

Files with missing lines Patch % Lines
cmd/tools_netem.go 80.59% 9 Missing and 4 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2454      +/-   ##
==========================================
+ Coverage   52.48%   52.60%   +0.11%     
==========================================
  Files         172      172              
  Lines       17614    17677      +63     
==========================================
+ Hits         9245     9299      +54     
- Misses       7398     7404       +6     
- Partials      971      974       +3     
Files with missing lines Coverage Δ
types/types.go 69.07% <ø> (ø)
cmd/tools_netem.go 72.83% <80.59%> (+1.95%) ⬆️

... and 3 files with indirect coverage changes

@@ -83,6 +88,11 @@ var netemShowCmd = &cobra.Command{
}

func netemSetFn(_ *cobra.Command, _ []string) error {
// Ensure that the sch_netem kernel module is loaded (for Fedora/RHEL compatibility)
if err := exec.Command("modprobe", "sch_netem").Run(); err != nil {
Copy link
Collaborator

@steiler steiler Feb 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@FloSch62 can you maybe try to get rid of this exec and use the module loading code we have in place already?
Might need some reorg then but would be good.

func (*CLab) loadKernelModules() error {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

add json output format for tools netem show
5 participants