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

[tuning] add ability to set tx queue len #914

Merged
merged 1 commit into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion plugins/meta/tuning/tuning.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ type TuningConf struct {
Mac string `json:"mac,omitempty"`
Promisc bool `json:"promisc,omitempty"`
Mtu int `json:"mtu,omitempty"`
TxQLen *int `json:"txQLen,omitempty"`
Allmulti *bool `json:"allmulti,omitempty"`

RuntimeConfig struct {
Expand All @@ -69,6 +70,7 @@ type IPAMArgs struct {
Promisc *bool `json:"promisc,omitempty"`
Mtu *int `json:"mtu,omitempty"`
Allmulti *bool `json:"allmulti,omitempty"`
TxQLen *int `json:"txQLen,omitempty"`
}

// configToRestore will contain interface attributes that should be restored on cmdDel
Expand All @@ -77,6 +79,7 @@ type configToRestore struct {
Promisc *bool `json:"promisc,omitempty"`
Mtu int `json:"mtu,omitempty"`
Allmulti *bool `json:"allmulti,omitempty"`
TxQLen *int `json:"txQLen,omitempty"`
}

// MacEnvArgs represents CNI_ARG
Expand Down Expand Up @@ -136,6 +139,10 @@ func parseConf(data []byte, envArgs string) (*TuningConf, error) {
if conf.Args.A.Allmulti != nil {
conf.Allmulti = conf.Args.A.Allmulti
}

if conf.Args.A.TxQLen != nil {
conf.TxQLen = conf.Args.A.TxQLen
}
}

return &conf, nil
Expand Down Expand Up @@ -204,6 +211,14 @@ func changeAllmulti(ifName string, val bool) error {
return netlink.LinkSetAllmulticastOff(link)
}

func changeTxQLen(ifName string, txQLen int) error {
link, err := netlink.LinkByName(ifName)
if err != nil {
return fmt.Errorf("failed to get %q: %v", ifName, err)
}
return netlink.LinkSetTxQLen(link, txQLen)
}

func createBackup(ifName, containerID, backupPath string, tuningConf *TuningConf) error {
config := configToRestore{}
link, err := netlink.LinkByName(ifName)
Expand All @@ -224,6 +239,10 @@ func createBackup(ifName, containerID, backupPath string, tuningConf *TuningConf
config.Allmulti = new(bool)
*config.Allmulti = (link.Attrs().RawFlags&unix.IFF_ALLMULTI != 0)
}
if tuningConf.TxQLen != nil {
qlen := link.Attrs().TxQLen
config.TxQLen = &qlen
}

if _, err := os.Stat(backupPath); os.IsNotExist(err) {
if err = os.MkdirAll(backupPath, 0o600); err != nil {
Expand Down Expand Up @@ -292,6 +311,13 @@ func restoreBackup(ifName, containerID, backupPath string) error {
}
}

if config.TxQLen != nil {
if err = changeTxQLen(ifName, *config.TxQLen); err != nil {
err = fmt.Errorf("failed to restore transmit queue length: %v", err)
errStr = append(errStr, err.Error())
}
}

if len(errStr) > 0 {
return fmt.Errorf(strings.Join(errStr, "; "))
}
Expand Down Expand Up @@ -346,7 +372,7 @@ func cmdAdd(args *skel.CmdArgs) error {
}
}

if tuningConf.Mac != "" || tuningConf.Mtu != 0 || tuningConf.Promisc || tuningConf.Allmulti != nil {
if tuningConf.Mac != "" || tuningConf.Mtu != 0 || tuningConf.Promisc || tuningConf.Allmulti != nil || tuningConf.TxQLen != nil {
if err = createBackup(args.IfName, args.ContainerID, tuningConf.DataDir, tuningConf); err != nil {
return err
}
Expand Down Expand Up @@ -377,6 +403,12 @@ func cmdAdd(args *skel.CmdArgs) error {
return err
}
}

if tuningConf.TxQLen != nil {
if err = changeTxQLen(args.IfName, *tuningConf.TxQLen); err != nil {
return err
}
}
return nil
})
if err != nil {
Expand Down Expand Up @@ -483,6 +515,13 @@ func cmdCheck(args *skel.CmdArgs) error {
args.IfName, tuningConf.Allmulti, allmulti)
}
}

if tuningConf.TxQLen != nil {
if *tuningConf.TxQLen != link.Attrs().TxQLen {
return fmt.Errorf("Error: Tuning configured Transmit Queue Length of %s is %d, current value is %d",
args.IfName, tuningConf.TxQLen, link.Attrs().TxQLen)
}
}
return nil
})
if err != nil {
Expand Down
139 changes: 138 additions & 1 deletion plugins/meta/tuning/tuning_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ var _ = Describe("tuning plugin", func() {
*beforeConf.Promisc = (link.Attrs().Promisc != 0)
beforeConf.Allmulti = new(bool)
*beforeConf.Allmulti = (link.Attrs().RawFlags&unix.IFF_ALLMULTI != 0)
beforeConf.TxQLen = new(int)
*beforeConf.TxQLen = link.Attrs().TxQLen
return nil
})
Expect(err).NotTo(HaveOccurred())
Expand Down Expand Up @@ -487,6 +489,138 @@ var _ = Describe("tuning plugin", func() {
Expect(err).NotTo(HaveOccurred())
})

It(fmt.Sprintf("[%s] configures and deconfigures tx queue len with ADD/DEL", ver), func() {
conf := []byte(fmt.Sprintf(`{
"name": "test",
"type": "iplink",
"cniVersion": "%s",
"txQLen": 20000,
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: make make this 20000 value a const, which can be re-used throughout the test ?
It is IMO better than consistently repeating this "magic" number.

"prevResult": {
"interfaces": [
{"name": "dummy0", "sandbox":"netns"}
],
"ips": [
{
"version": "4",
"address": "10.0.0.2/24",
"gateway": "10.0.0.1",
"interface": 0
}
]
}
}`, ver))

args := &skel.CmdArgs{
ContainerID: "dummy",
Netns: originalNS.Path(),
IfName: IFNAME,
StdinData: conf,
}

err := originalNS.Do(func(ns.NetNS) error {
defer GinkgoRecover()

r, _, err := testutils.CmdAddWithArgs(args, func() error {
return cmdAdd(args)
})
Expect(err).NotTo(HaveOccurred())

link, err := netlink.LinkByName(IFNAME)
Expect(err).NotTo(HaveOccurred())
Expect(link.Attrs().TxQLen).To(Equal(20000))

if testutils.SpecVersionHasCHECK(ver) {
n := &TuningConf{}
Expect(json.Unmarshal(conf, &n)).NotTo(HaveOccurred())

confString, err := buildOneConfig(ver, n, r)
Expect(err).NotTo(HaveOccurred())

args.StdinData = confString

Expect(testutils.CmdCheckWithArgs(args, func() error {
return cmdCheck(args)
})).NotTo(HaveOccurred())
}

err = testutils.CmdDel(originalNS.Path(),
args.ContainerID, "", func() error { return cmdDel(args) })
Expect(err).NotTo(HaveOccurred())

link, err = netlink.LinkByName(IFNAME)
Expect(err).NotTo(HaveOccurred())
Expect(link.Attrs().TxQLen).To(Equal(*beforeConf.TxQLen))

return nil
})
Expect(err).NotTo(HaveOccurred())
})

It(fmt.Sprintf("[%s] configures and deconfigures tx queue len from args with ADD/DEL", ver), func() {
conf := []byte(fmt.Sprintf(`{
"name": "test",
"type": "iplink",
"cniVersion": "%s",
"args": {
"cni": {
"txQLen": 20000
}
},
"prevResult": {
"interfaces": [
{"name": "dummy0", "sandbox":"netns"}
],
"ips": [
{
"version": "4",
"address": "10.0.0.2/24",
"gateway": "10.0.0.1",
"interface": 0
}
]
}
}`, ver))

args := &skel.CmdArgs{
ContainerID: "dummy",
Netns: originalNS.Path(),
IfName: IFNAME,
StdinData: conf,
}

err := originalNS.Do(func(ns.NetNS) error {
defer GinkgoRecover()

r, _, err := testutils.CmdAddWithArgs(args, func() error {
return cmdAdd(args)
})
Expect(err).NotTo(HaveOccurred())

result, err := types100.GetResult(r)
Expect(err).NotTo(HaveOccurred())

Expect(result.Interfaces).To(HaveLen(1))
Expect(result.Interfaces[0].Name).To(Equal(IFNAME))
Expect(result.IPs).To(HaveLen(1))
Expect(result.IPs[0].Address.String()).To(Equal("10.0.0.2/24"))
tariq1890 marked this conversation as resolved.
Show resolved Hide resolved

link, err := netlink.LinkByName(IFNAME)
Expect(err).NotTo(HaveOccurred())
Expect(link.Attrs().TxQLen).To(Equal(20000))

err = testutils.CmdDel(originalNS.Path(),
args.ContainerID, "", func() error { return cmdDel(args) })
Expect(err).NotTo(HaveOccurred())

link, err = netlink.LinkByName(IFNAME)
Expect(err).NotTo(HaveOccurred())
Expect(link.Attrs().TxQLen).To(Equal(*beforeConf.TxQLen))

return nil
})
Expect(err).NotTo(HaveOccurred())
})

It(fmt.Sprintf("[%s] configures and deconfigures mac address (from conf file) with ADD/DEL", ver), func() {
mac := "c2:11:22:33:44:55"
conf := []byte(fmt.Sprintf(`{
Expand Down Expand Up @@ -780,14 +914,15 @@ var _ = Describe("tuning plugin", func() {
Expect(err).NotTo(HaveOccurred())
})

It(fmt.Sprintf("[%s] configures and deconfigures mac address, promisc mode and MTU (from conf file) with custom dataDir", ver), func() {
It(fmt.Sprintf("[%s] configures and deconfigures mac address, promisc mode, MTU and tx queue len (from conf file) with custom dataDir", ver), func() {
conf := []byte(fmt.Sprintf(`{
"name": "test",
"type": "iplink",
"cniVersion": "%s",
"mac": "c2:11:22:33:44:77",
"promisc": true,
"mtu": 4000,
"txQLen": 20000,
"dataDir": "/tmp/tuning-test",
"prevResult": {
"interfaces": [
Expand Down Expand Up @@ -834,6 +969,7 @@ var _ = Describe("tuning plugin", func() {
Expect(link.Attrs().HardwareAddr).To(Equal(hw))
Expect(link.Attrs().Promisc).To(Equal(1))
Expect(link.Attrs().MTU).To(Equal(4000))
Expect(link.Attrs().TxQLen).To(Equal(20000))

Expect("/tmp/tuning-test/dummy_dummy0.json").Should(BeAnExistingFile())

Expand Down Expand Up @@ -862,6 +998,7 @@ var _ = Describe("tuning plugin", func() {
Expect(link.Attrs().HardwareAddr.String()).To(Equal(beforeConf.Mac))
Expect(link.Attrs().MTU).To(Equal(beforeConf.Mtu))
Expect(link.Attrs().Promisc != 0).To(Equal(*beforeConf.Promisc))
Expect(link.Attrs().TxQLen).To(Equal(*beforeConf.TxQLen))

return nil
})
Expand Down