net_prio.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. Copyright The containerd Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package cgroup1
  14. import (
  15. "fmt"
  16. "os"
  17. "path/filepath"
  18. specs "github.com/opencontainers/runtime-spec/specs-go"
  19. )
  20. func NewNetPrio(root string) *netprioController {
  21. return &netprioController{
  22. root: filepath.Join(root, string(NetPrio)),
  23. }
  24. }
  25. type netprioController struct {
  26. root string
  27. }
  28. func (n *netprioController) Name() Name {
  29. return NetPrio
  30. }
  31. func (n *netprioController) Path(path string) string {
  32. return filepath.Join(n.root, path)
  33. }
  34. func (n *netprioController) Create(path string, resources *specs.LinuxResources) error {
  35. if err := os.MkdirAll(n.Path(path), defaultDirPerm); err != nil {
  36. return err
  37. }
  38. if resources.Network != nil {
  39. for _, prio := range resources.Network.Priorities {
  40. if err := os.WriteFile(
  41. filepath.Join(n.Path(path), "net_prio.ifpriomap"),
  42. formatPrio(prio.Name, prio.Priority),
  43. defaultFilePerm,
  44. ); err != nil {
  45. return err
  46. }
  47. }
  48. }
  49. return nil
  50. }
  51. func formatPrio(name string, prio uint32) []byte {
  52. return []byte(fmt.Sprintf("%s %d", name, prio))
  53. }