Merge pull request #582 from Mashimiao/add-cgroup-subsystem-net_cls

cgroups: add support for net_cls
This commit is contained in:
Michael Crosby 2015-05-15 10:22:54 -07:00
commit fabd8e98be
5 changed files with 97 additions and 2 deletions

View File

@ -22,6 +22,8 @@ var (
"cpuacct": &CpuacctGroup{}, "cpuacct": &CpuacctGroup{},
"blkio": &BlkioGroup{}, "blkio": &BlkioGroup{},
"hugetlb": &HugetlbGroup{}, "hugetlb": &HugetlbGroup{},
"net_cls": &NetClsGroup{},
"net_prio": &NetPrioGroup{},
"perf_event": &PerfEventGroup{}, "perf_event": &PerfEventGroup{},
"freezer": &FreezerGroup{}, "freezer": &FreezerGroup{},
} }

40
cgroups/fs/net_cls.go Normal file
View File

@ -0,0 +1,40 @@
package fs
import (
"github.com/docker/libcontainer/cgroups"
"github.com/docker/libcontainer/configs"
)
type NetClsGroup struct {
}
func (s *NetClsGroup) Apply(d *data) error {
dir, err := d.join("net_cls")
if err != nil && !cgroups.IsNotFound(err) {
return err
}
if err := s.Set(dir, d.c); err != nil {
return err
}
return nil
}
func (s *NetClsGroup) Set(path string, cgroup *configs.Cgroup) error {
if cgroup.NetClsClassid != "" {
if err := writeFile(path, "net_cls.classid", cgroup.NetClsClassid); err != nil {
return err
}
}
return nil
}
func (s *NetClsGroup) Remove(d *data) error {
return removePath(d.path("net_cls"))
}
func (s *NetClsGroup) GetStats(path string, stats *cgroups.Stats) error {
return nil
}

View File

@ -0,0 +1,36 @@
package fs
import (
"testing"
)
const (
classidBefore = "0x100002"
classidAfter = "0x100001"
)
func TestNetClsSetClassid(t *testing.T) {
helper := NewCgroupTestUtil("net_cls", t)
defer helper.cleanup()
helper.writeFileContents(map[string]string{
"net_cls.classid": classidBefore,
})
helper.CgroupData.c.NetClsClassid = classidAfter
netcls := &NetClsGroup{}
if err := netcls.Set(helper.CgroupPath, helper.CgroupData.c); err != nil {
t.Fatal(err)
}
// As we are in mock environment, we can't get correct value of classid from
// net_cls.classid.
// So. we just judge if we successfully write classid into file
value, err := getCgroupParamString(helper.CgroupPath, "net_cls.classid")
if err != nil {
t.Fatalf("Failed to parse net_cls.classid - %s", err)
}
if value != classidAfter {
t.Fatal("Got the wrong value, set net_cls.classid failed.")
}
}

View File

@ -42,6 +42,7 @@ var subsystems = map[string]subsystem{
"perf_event": &fs.PerfEventGroup{}, "perf_event": &fs.PerfEventGroup{},
"freezer": &fs.FreezerGroup{}, "freezer": &fs.FreezerGroup{},
"net_prio": &fs.NetPrioGroup{}, "net_prio": &fs.NetPrioGroup{},
"net_cls": &fs.NetClsGroup{},
} }
const ( const (
@ -204,7 +205,7 @@ func (m *Manager) Apply(pid int) error {
return err return err
} }
// we need to manually join the freezer, net_prio and cpuset cgroup in systemd // we need to manually join the freezer, net_cls, net_prio and cpuset cgroup in systemd
// because it does not currently support it via the dbus api. // because it does not currently support it via the dbus api.
if err := joinFreezer(c, pid); err != nil { if err := joinFreezer(c, pid); err != nil {
return err return err
@ -213,6 +214,9 @@ func (m *Manager) Apply(pid int) error {
if err := joinNetPrio(c, pid); err != nil { if err := joinNetPrio(c, pid); err != nil {
return err return err
} }
if err := joinNetCls(c, pid); err != nil {
return err
}
if err := joinCpuset(c, pid); err != nil { if err := joinCpuset(c, pid); err != nil {
return err return err
@ -322,11 +326,21 @@ func joinNetPrio(c *configs.Cgroup, pid int) error {
if err != nil && !cgroups.IsNotFound(err) { if err != nil && !cgroups.IsNotFound(err) {
return err return err
} }
netPrio := subsystems["net_prio"] netPrio := subsystems["net_prio"]
return netPrio.Set(path, c) return netPrio.Set(path, c)
} }
func joinNetCls(c *configs.Cgroup, pid int) error {
path, err := join(c, "net_cls", pid)
if err != nil && !cgroups.IsNotFound(err) {
return err
}
netcls := subsystems["net_cls"]
return netcls.Set(path, c)
}
func getSubsystemPath(c *configs.Cgroup, subsystem string) (string, error) { func getSubsystemPath(c *configs.Cgroup, subsystem string) (string, error) {
mountpoint, err := cgroups.FindCgroupMountpoint(subsystem) mountpoint, err := cgroups.FindCgroupMountpoint(subsystem)
if err != nil { if err != nil {

View File

@ -83,4 +83,7 @@ type Cgroup struct {
// Set priority of network traffic for container // Set priority of network traffic for container
NetPrioIfpriomap []*IfPrioMap `json:"net_prio_ifpriomap"` NetPrioIfpriomap []*IfPrioMap `json:"net_prio_ifpriomap"`
// Set class identifier for container's network packets
NetClsClassid string `json:"net_cls_classid"`
} }