Skip to content

Commit 25022fe

Browse files
author
Ma Shimiao
committed
generate: add network related option
Signed-off-by: Ma Shimiao <[email protected]>
1 parent 389c73a commit 25022fe

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)"},
@@ -420,6 +422,25 @@ func setupSpec(g *generate.Generator, context *cli.Context) error {
420422
g.SetLinuxResourcesMemorySwappiness(context.Uint64("linux-mem-swappiness"))
421423
}
422424

425+
if context.IsSet("linux-network-classid") {
426+
g.SetLinuxResourcesNetworkClassID(uint32(context.Int("linux-network-classid")))
427+
}
428+
429+
if context.IsSet("linux-network-priorities") {
430+
priorities := context.StringSlice("linux-network-priorities")
431+
for _, p := range priorities {
432+
name, priority, err := parseNetworkPriority(p)
433+
if err != nil {
434+
return err
435+
}
436+
if priority == -1 {
437+
g.DropLinuxResourcesNetworkPriorities(name)
438+
} else {
439+
g.AddLinuxResourcesNetworkPriorities(name, uint32(priority))
440+
}
441+
}
442+
}
443+
423444
err := addSeccomp(context, g)
424445
return err
425446
}
@@ -472,6 +493,21 @@ func parseHook(s string) (string, []string) {
472493
return path, args
473494
}
474495

496+
func parseNetworkPriority(np string) (string, int32, error) {
497+
var err error
498+
499+
parts := strings.Split(np, ":")
500+
if len(parts) != 2 {
501+
return "", 0, fmt.Errorf("invalid value %v for --linux-network-priorities", np)
502+
}
503+
priority, err := strconv.Atoi(parts[1])
504+
if err != nil {
505+
return "", 0, err
506+
}
507+
508+
return parts[0], int32(priority), nil
509+
}
510+
475511
func parseTmpfsMount(s string) (string, []string, error) {
476512
var dest string
477513
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)