Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for CGroups #221

Merged
merged 29 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
e1ec33f
fix: Harmonize API for passing network options
stv0g Oct 30, 2024
ca759a2
feat: Add CGroup v2 support
stv0g Oct 30, 2024
55537c4
website: Remove blog and redirect to cunicu blog
stv0g Oct 30, 2024
93a1543
fix(cgroups): Create Cgroup slice for host node
stv0g Nov 8, 2024
2675063
fix(cmd): Split gontc command to several files
stv0g Nov 8, 2024
8e5aec9
fix: Generation of random network names
stv0g Nov 8, 2024
0d83233
feat(cgroup): Add symlinks to cgroup fs
stv0g Nov 8, 2024
cce8743
fix: Remove NSPrefix option
stv0g Nov 8, 2024
b8b0f27
fix: Improve comments and error messages
stv0g Nov 8, 2024
12405ad
feat: Add MustClose closers to Network and Namespace to avoid silence…
stv0g Nov 8, 2024
8468181
feat: Use PIDFDs to start sub-processes
stv0g Nov 15, 2024
d60b85e
fix(cmd): Improve clean and gc commands
stv0g Nov 15, 2024
d37e06b
feat(cgroups): Add PIDFDs option
stv0g Nov 15, 2024
ab795a3
fix(test): Make netem test more robust
stv0g Nov 15, 2024
24f4b07
feat(options): Add support for global network-wide options
stv0g Dec 9, 2024
2ebe1d5
fix: Broken link to slides
stv0g Dec 9, 2024
8ee168e
feat: Check for already used node and network names
stv0g Dec 9, 2024
ff3e6e5
fix(test): Shifted line numbers in debug test
stv0g Dec 9, 2024
5c8a416
fix: Code style cleanups and comments
stv0g Dec 9, 2024
1ef1ec4
feat: Refactor host node handling
stv0g Dec 9, 2024
2e1d9ce
fix: Several fixes to cgroup handling
stv0g Dec 9, 2024
59daab6
fix: update debugger to new Delve version
stv0g Dec 9, 2024
c13cda2
fix(build): Update Makefile
stv0g Dec 9, 2024
a64b3d4
fix: Linter warnings
stv0g Dec 9, 2024
5ec9f29
fix: Add support for older systems without PidFD support
stv0g Dec 9, 2024
083b564
fix: Bump GitHub actions runner image
stv0g Dec 9, 2024
a3a82a3
feat: Add more network names
stv0g Dec 9, 2024
b7d632d
fix: Disallow names with hyphons
stv0g Dec 9, 2024
1f44538
fix: Flaky test TestTraceDissector
stv0g Dec 9, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ on:

jobs:
build:
runs-on: ubuntu-latest
runs-on: ubuntu-24.04
steps:
- name: Checkout code
uses: actions/checkout@v4
Expand Down Expand Up @@ -46,6 +46,10 @@ jobs:
- name: Set up gotestfmt
run: go install github.com/gotesttools/gotestfmt/v2/cmd/gotestfmt@latest

- name: Check systemd version
run: |
sudo systemctl --version

- name: Test
run: |
sudo --preserve-env \
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ permissions:
jobs:
golangci:
name: lint
runs-on: ubuntu-latest
runs-on: ubuntu-24.04
steps:
- name: Checkout code
uses: actions/checkout@v4
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/website.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ defaults:
jobs:
deploy:
name: Build and deploy website
runs-on: ubuntu-latest
runs-on: ubuntu-24.04
environment: release
steps:
- name: Checkout
Expand Down
11 changes: 11 additions & 0 deletions LICENSES/BSD-3-Clause.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Copyright (c) <year> <owner>.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
7 changes: 4 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@

GIT_TAG = $(shell git describe --tags HEAD)

GOFLAGS = -ldflags "-X main.tag=$(GIT_TAG)"
export GOFLAGS = -buildvcs=false -ldflags=-X=main.tag=$(GIT_TAG)

all: gontc

tests:
sudo -E go test ./pkg ./internal -v $(TEST_OPTS)
sudo -E go test ./pkg -v $(TEST_OPTS)
sudo -E go test ./internal -v $(TEST_OPTS)

gontc:
go build $(GOFLAGS) -o $@ ./cmd/gontc
go build -o $@ ./cmd/gontc

.PHONY: all gontc
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Integration tests can be automated and executed in CI environments like GitHub a

## Getting started

Have a look at our **[slide set](https://cunicu.github.io/gont/)** to get you started.
Have a look at our **[slide set](https://gont.cunicu.li/slides/)** to get you started.

## Features

Expand Down
38 changes: 38 additions & 0 deletions cmd/gontc/clean.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// SPDX-FileCopyrightText: 2023 Steffen Vogel <[email protected]>
// SPDX-License-Identifier: Apache-2.0

package main

import (
"context"
"fmt"
"time"

g "cunicu.li/gont/v2/pkg"
"github.com/coreos/go-systemd/v22/dbus"
)

func clean(args []string) error {
ctx := context.Background()
ctx, _ = context.WithTimeout(ctx, 30*time.Second) //nolint:govet

c, err := dbus.NewWithContext(ctx)
if err != nil {
return fmt.Errorf("failed to connect to D-Bus: %w", err)
}

networks := args[1:]
if len(networks) == 0 {
networks = g.NetworkNames()
}

for _, name := range networks {
if err := g.TeardownNetwork(ctx, c, name); err != nil {
return fmt.Errorf("failed to teardown network '%s': %w", name, err)
}

fmt.Println(name)
}

return nil
}
56 changes: 56 additions & 0 deletions cmd/gontc/exec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// SPDX-FileCopyrightText: 2023 Steffen Vogel <[email protected]>
// SPDX-License-Identifier: Apache-2.0

package main

import (
"flag"
"fmt"
"os"

g "cunicu.li/gont/v2/pkg"
sdbus "github.com/coreos/go-systemd/v22/dbus"
"github.com/godbus/dbus/v5"
)

func exec(network, node string, args []string) error {
if len(flag.Args()) <= 1 {
return fmt.Errorf("not enough arguments")
}

if network == "" {
return fmt.Errorf("there is no active Gont network")
}

if err := os.Setenv("GONT_NETWORK", network); err != nil {
return err
}
if err := os.Setenv("GONT_NODE", node); err != nil {
return err
}

sliceName := fmt.Sprintf("gont-%s-%s", network, node)
scopeName := fmt.Sprintf("gont-run-%d", os.Getpid())

cgroup, err := g.NewCGroup(nil, "scope", scopeName)
if err != nil {
return fmt.Errorf("failed to create cgroup: %w", err)
}

cgroup.Properties = append(cgroup.Properties,
sdbus.Property{
Name: "Slice",
Value: dbus.MakeVariant(sliceName + ".slice"),
},
sdbus.Property{
Name: "PIDs",
Value: dbus.MakeVariant([]uint{uint(os.Getpid())}), //nolint:gosec
},
)

if err := cgroup.Start(); err != nil {
return fmt.Errorf("failed to start cgroup: %w", err)
}

return g.Exec(network, node, args)
}
34 changes: 34 additions & 0 deletions cmd/gontc/gc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// SPDX-FileCopyrightText: 2023 Steffen Vogel <[email protected]>
// SPDX-License-Identifier: Apache-2.0

package main

import (
"context"
"fmt"
"time"

g "cunicu.li/gont/v2/pkg"
"github.com/coreos/go-systemd/v22/dbus"
)

func collectGarbage(args []string) error {
ctx := context.Background()
ctx, _ = context.WithTimeout(ctx, 10*time.Second) //nolint:govet

c, err := dbus.NewWithContext(ctx)
if err != nil {
return fmt.Errorf("failed to connect to D-Bus: %w", err)
}

deleted, err := g.TeardownStaleCgroups(ctx, c)
if err != nil {
return err
}

for _, name := range deleted {
fmt.Println(name)
}

return nil
}
81 changes: 3 additions & 78 deletions cmd/gontc/gontc.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"strings"

"cunicu.li/gont/v2/internal"
"cunicu.li/gont/v2/internal/utils"
g "cunicu.li/gont/v2/pkg"
"golang.org/x/exp/slices"
)
Expand Down Expand Up @@ -97,6 +96,9 @@ func main() {
case "version":
version()

case "gc":
err = collectGarbage(args)

case "help":
flag.Usage()
err = nil
Expand Down Expand Up @@ -141,80 +143,3 @@ func networkNode(args []string) (string, string, error) {

return network, node, nil
}

func version() {
version := "unknown"
if tag != "" {
version = tag
}

if ok, rev, dirty, btime := utils.ReadVCSInfos(); ok {
dirtyFlag := ""
if dirty {
dirtyFlag = "-dirty"
}

fmt.Printf("%s (%s%s, build on %s)\n", version, rev[:8], dirtyFlag, btime.String())
} else {
fmt.Println(version)
}
}

func list(args []string) {
if len(args) > 1 {
network := args[1]
for _, name := range g.NodeNames(network) {
fmt.Printf("%s/%s\n", network, name)
}
} else {
for _, name := range g.NetworkNames() {
fmt.Println(name)
}
}
}

func clean(args []string) error {
if len(args) > 1 {
network := args[1]
if err := g.TeardownNetwork(network); err != nil {
return fmt.Errorf("failed to teardown network '%s': %w", network, err)
}
} else {
return g.TeardownAllNetworks()
}

return nil
}

func exec(network, node string, args []string) error {
if len(flag.Args()) <= 1 {
return fmt.Errorf("not enough arguments")
}

if network == "" {
return fmt.Errorf("there is no active Gont network")
}

if err := os.Setenv("GONT_NETWORK", network); err != nil {
return err
}
if err := os.Setenv("GONT_NODE", node); err != nil {
return err
}

return g.Exec(network, node, args)
}

func shell(network, node string) error {
shell := os.Getenv("SHELL")
if shell == "" {
shell = "/bin/bash"
}

ps1 := fmt.Sprintf("%s/%s: ", network, node)
os.Setenv("PS1", ps1)

cmd := []string{shell, "--norc"}

return exec(network, node, cmd)
}
23 changes: 23 additions & 0 deletions cmd/gontc/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// SPDX-FileCopyrightText: 2023 Steffen Vogel <[email protected]>
// SPDX-License-Identifier: Apache-2.0

package main

import (
"fmt"

g "cunicu.li/gont/v2/pkg"
)

func list(args []string) {
if len(args) > 1 {
network := args[1]
for _, name := range g.NodeNames(network) {
fmt.Printf("%s/%s\n", network, name)
}
} else {
for _, name := range g.NetworkNames() {
fmt.Println(name)
}
}
}
23 changes: 23 additions & 0 deletions cmd/gontc/shell.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// SPDX-FileCopyrightText: 2023 Steffen Vogel <[email protected]>
// SPDX-License-Identifier: Apache-2.0

package main

import (
"fmt"
"os"
)

func shell(network, node string) error {
shell := os.Getenv("SHELL")
if shell == "" {
shell = "/bin/bash"
}

ps1 := fmt.Sprintf("%s/%s: ", network, node)
os.Setenv("PS1", ps1)

cmd := []string{shell, "--norc"}

return exec(network, node, cmd)
}
28 changes: 28 additions & 0 deletions cmd/gontc/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// SPDX-FileCopyrightText: 2023 Steffen Vogel <[email protected]>
// SPDX-License-Identifier: Apache-2.0

package main

import (
"fmt"

"cunicu.li/gont/v2/internal/utils"
)

func version() {
version := "unknown"
if tag != "" {
version = tag
}

if ok, rev, dirty, btime := utils.ReadVCSInfos(); ok {
dirtyFlag := ""
if dirty {
dirtyFlag = "-dirty"
}

fmt.Printf("%s (%s%s, build on %s)\n", version, rev[:8], dirtyFlag, btime.String())
} else {
fmt.Println(version)
}
}
2 changes: 1 addition & 1 deletion default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
buildGoModule {
name = "gont";
src = ./.;
vendorHash = "sha256-IXTpMzTrWRH10vB6hRsMf7ilT5tUG/EPJbYLO+8d9Ik=";
vendorHash = "sha256-EAwP8nNyS6lnLi/OBxxdZzePIiy30l6uFr1Z8SPAllA=";
buildInputs = [ libpcap ];
doCheck = false;
}
Loading
Loading