Skip to content
This repository has been archived by the owner on Dec 13, 2018. It is now read-only.

Commit

Permalink
Merge pull request #584 from Mashimiao/add-cgroup-subsystem-net_prio
Browse files Browse the repository at this point in the history
cgroup: add support for net_prio
  • Loading branch information
vmarmol committed May 14, 2015
2 parents acd866f + 0810bc8 commit 64c5e51
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 1 deletion.
40 changes: 40 additions & 0 deletions cgroups/fs/net_prio.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package fs

import (
"github.com/docker/libcontainer/cgroups"
"github.com/docker/libcontainer/configs"
)

type NetPrioGroup struct {
}

func (s *NetPrioGroup) Apply(d *data) error {
dir, err := d.join("net_prio")
if err != nil && !cgroups.IsNotFound(err) {
return err
}

if err := s.Set(dir, d.c); err != nil {
return err
}

return nil
}

func (s *NetPrioGroup) Set(path string, cgroup *configs.Cgroup) error {
for _, prioMap := range cgroup.NetPrioIfpriomap {
if err := writeFile(path, "net_prio.ifpriomap", prioMap.CgroupString()); err != nil {
return err
}
}

return nil
}

func (s *NetPrioGroup) Remove(d *data) error {
return removePath(d.path("net_prio"))
}

func (s *NetPrioGroup) GetStats(path string, stats *cgroups.Stats) error {
return nil
}
36 changes: 36 additions & 0 deletions cgroups/fs/net_prio_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package fs

import (
"strings"
"testing"

"github.com/docker/libcontainer/configs"
)

var (
prioMap = []*configs.IfPrioMap{
{
Interface: "test",
Priority: 5,
},
}
)

func TestNetPrioSetIfPrio(t *testing.T) {
helper := NewCgroupTestUtil("net_prio", t)
defer helper.cleanup()

helper.CgroupData.c.NetPrioIfpriomap = prioMap
netPrio := &NetPrioGroup{}
if err := netPrio.Set(helper.CgroupPath, helper.CgroupData.c); err != nil {
t.Fatal(err)
}

value, err := getCgroupParamString(helper.CgroupPath, "net_prio.ifpriomap")
if err != nil {
t.Fatalf("Failed to parse net_prio.ifpriomap - %s", err)
}
if !strings.Contains(value, "test 5") {
t.Fatal("Got the wrong value, set net_prio.ifpriomap failed.")
}
}
17 changes: 16 additions & 1 deletion cgroups/systemd/apply_systemd.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ var subsystems = map[string]subsystem{
"hugetlb": &fs.HugetlbGroup{},
"perf_event": &fs.PerfEventGroup{},
"freezer": &fs.FreezerGroup{},
"net_prio": &fs.NetPrioGroup{},
}

const (
Expand Down Expand Up @@ -207,12 +208,16 @@ func (m *Manager) Apply(pid int) error {

}

// we need to manually join the freezer and cpuset cgroup in systemd
// we need to manually join the freezer, net_prio and cpuset cgroup in systemd
// because it does not currently support it via the dbus api.
if err := joinFreezer(c, pid); err != nil {
return err
}

if err := joinNetPrio(c, pid); err != nil {
return err
}

if err := joinCpuset(c, pid); err != nil {
return err
}
Expand Down Expand Up @@ -316,6 +321,16 @@ func joinFreezer(c *configs.Cgroup, pid int) error {
return nil
}

func joinNetPrio(c *configs.Cgroup, pid int) error {
path, err := join(c, "net_prio", pid)
if err != nil && !cgroups.IsNotFound(err) {
return err
}

netPrio := subsystems["net_prio"]
return netPrio.Set(path, c)
}

func getSubsystemPath(c *configs.Cgroup, subsystem string) (string, error) {
mountpoint, err := cgroups.FindCgroupMountpoint(subsystem)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions configs/cgroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,7 @@ type Cgroup struct {

// Whether to disable OOM Killer
OomKillDisable bool `json:"oom_kill_disable"`

// Set priority of network traffic for container
NetPrioIfpriomap []*IfPrioMap `json:"net_prio_ifpriomap"`
}
14 changes: 14 additions & 0 deletions configs/interface_priority_map.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package configs

import (
"fmt"
)

type IfPrioMap struct {
Interface string `json:"interface"`
Priority int64 `json:"priority"`
}

func (i *IfPrioMap) CgroupString() string {
return fmt.Sprintf("%s %d", i.Interface, i.Priority)
}

0 comments on commit 64c5e51

Please sign in to comment.