Skip to content

Commit 476f1fb

Browse files
author
Ma Shimiao
authored
Merge pull request #257 from Mashimiao/generate-add-network-related-options
generate: add network related option
2 parents 9ec55d5 + 25022fe commit 476f1fb

File tree

5 files changed

+86
-0
lines changed

5 files changed

+86
-0
lines changed

cmd/oci-runtime-tool/generate.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ var generateFlags = []cli.Flag{
4242
cli.Uint64Flag{Name: "linux-mem-swap", Usage: "total memory limit (memory + swap) (in bytes)"},
4343
cli.Uint64Flag{Name: "linux-mem-swappiness", Usage: "how aggressive the kernel will swap memory pages (Range from 0 to 100)"},
4444
cli.StringFlag{Name: "linux-mems", Usage: "list of memory nodes in the cpuset (default is to use any available memory node)"},
45+
cli.IntFlag{Name: "linux-network-classid", Usage: "specifies class identifier tagged by container's network packets"},
46+
cli.StringSliceFlag{Name: "linux-network-priorities", Usage: "specifies priorities of network traffic"},
4547
cli.Int64Flag{Name: "linux-pids-limit", Usage: "maximum number of PIDs"},
4648
cli.Uint64Flag{Name: "linux-realtime-period", Usage: "CPU period to be used for realtime scheduling (in usecs)"},
4749
cli.Uint64Flag{Name: "linux-realtime-runtime", Usage: "the time realtime scheduling may use (in usecs)"},
@@ -424,6 +426,25 @@ func setupSpec(g *generate.Generator, context *cli.Context) error {
424426
g.SetLinuxResourcesMemorySwappiness(context.Uint64("linux-mem-swappiness"))
425427
}
426428

429+
if context.IsSet("linux-network-classid") {
430+
g.SetLinuxResourcesNetworkClassID(uint32(context.Int("linux-network-classid")))
431+
}
432+
433+
if context.IsSet("linux-network-priorities") {
434+
priorities := context.StringSlice("linux-network-priorities")
435+
for _, p := range priorities {
436+
name, priority, err := parseNetworkPriority(p)
437+
if err != nil {
438+
return err
439+
}
440+
if priority == -1 {
441+
g.DropLinuxResourcesNetworkPriorities(name)
442+
} else {
443+
g.AddLinuxResourcesNetworkPriorities(name, uint32(priority))
444+
}
445+
}
446+
}
447+
427448
err := addSeccomp(context, g)
428449
return err
429450
}
@@ -476,6 +497,21 @@ func parseHook(s string) (string, []string) {
476497
return path, args
477498
}
478499

500+
func parseNetworkPriority(np string) (string, int32, error) {
501+
var err error
502+
503+
parts := strings.Split(np, ":")
504+
if len(parts) != 2 {
505+
return "", 0, fmt.Errorf("invalid value %v for --linux-network-priorities", np)
506+
}
507+
priority, err := strconv.Atoi(parts[1])
508+
if err != nil {
509+
return "", 0, err
510+
}
511+
512+
return parts[0], int32(priority), nil
513+
}
514+
479515
func parseTmpfsMount(s string) (string, []string, error) {
480516
var dest string
481517
var options []string

completions/bash/oci-runtime-tool

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,8 @@ _oci-runtime-tool_generate() {
286286
--help
287287
--ipc
288288
--label
289+
--linux-network-classid
290+
--linux-network-priorities
289291
--linux-pids-limit
290292
--masked-paths
291293
--mount

generate/generate.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,38 @@ func (g *Generator) SetLinuxResourcesMemorySwappiness(swappiness uint64) {
464464
g.spec.Linux.Resources.Memory.Swappiness = &swappiness
465465
}
466466

467+
// SetLinuxResourcesNetworkClassID sets g.spec.Linux.Resources.Network.ClassID.
468+
func (g *Generator) SetLinuxResourcesNetworkClassID(classid uint32) {
469+
g.initSpecLinuxResourcesNetwork()
470+
g.spec.Linux.Resources.Network.ClassID = &classid
471+
}
472+
473+
// AddLinuxResourcesNetworkPriorities adds or sets g.spec.Linux.Resources.Network.Priorities.
474+
func (g *Generator) AddLinuxResourcesNetworkPriorities(name string, prio uint32) {
475+
g.initSpecLinuxResourcesNetwork()
476+
for i, netPriority := range g.spec.Linux.Resources.Network.Priorities {
477+
if netPriority.Name == name {
478+
g.spec.Linux.Resources.Network.Priorities[i].Priority = prio
479+
return
480+
}
481+
}
482+
interfacePrio := new(rspec.InterfacePriority)
483+
interfacePrio.Name = name
484+
interfacePrio.Priority = prio
485+
g.spec.Linux.Resources.Network.Priorities = append(g.spec.Linux.Resources.Network.Priorities, *interfacePrio)
486+
}
487+
488+
// DropLinuxResourcesNetworkPriorities drops one item from g.spec.Linux.Resources.Network.Priorities.
489+
func (g *Generator) DropLinuxResourcesNetworkPriorities(name string) {
490+
g.initSpecLinuxResourcesNetwork()
491+
for i, netPriority := range g.spec.Linux.Resources.Network.Priorities {
492+
if netPriority.Name == name {
493+
g.spec.Linux.Resources.Network.Priorities = append(g.spec.Linux.Resources.Network.Priorities[:i], g.spec.Linux.Resources.Network.Priorities[i+1:]...)
494+
return
495+
}
496+
}
497+
}
498+
467499
// SetLinuxResourcesPidsLimit sets g.spec.Linux.Resources.Pids.Limit.
468500
func (g *Generator) SetLinuxResourcesPidsLimit(limit int64) {
469501
g.initSpecLinuxResourcesPids()

generate/spec.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ func (g *Generator) initSpecLinuxResourcesMemory() {
5959
}
6060
}
6161

62+
func (g *Generator) initSpecLinuxResourcesNetwork() {
63+
g.initSpecLinuxResources()
64+
if g.spec.Linux.Resources.Network == nil {
65+
g.spec.Linux.Resources.Network = &rspec.Network{}
66+
}
67+
}
68+
6269
func (g *Generator) initSpecLinuxResourcesPids() {
6370
g.initSpecLinuxResources()
6471
if g.spec.Linux.Resources.Pids == nil {

man/oci-runtime-tool-generate.1.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,15 @@ read the configuration from `config.json`.
123123
**--linux-mems**=MEMS
124124
Sets the list of memory nodes in the cpuset (default is to use any available memory node).
125125

126+
**--linux-network-classid**=CLASSID
127+
Specifies network class identifier which will be tagged by container's network packets.
128+
129+
**--linux-network-priorities**=[]
130+
Specifies network priorities of network traffic, format is NAME:PRIORITY.
131+
e.g. --linux-network-priorities=eth0:123
132+
This option can be specified multiple times. If a interface name was specified more than once, the last PRIORITY makes sense.
133+
The special *PRIORITY* -1 removes existing setting for interface NAME.
134+
126135
**--linux-pids-limit**=PIDSLIMIT
127136
Set maximum number of PIDs.
128137

0 commit comments

Comments
 (0)