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

Provide option to enforce network connection rate limiting #13891

Merged
merged 3 commits into from
Oct 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
12 changes: 11 additions & 1 deletion components/ws-daemon/nsinsider/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,10 @@ func main() {
Name: "bucketsize",
Required: false,
},
&cli.BoolFlag{
Name: "enforce",
Required: false,
},
},
Action: func(c *cli.Context) error {
const drop_stats = "ws-connection-drop-stats"
Expand All @@ -500,6 +504,7 @@ func main() {
if bucketSize == 0 {
bucketSize = 1000
}
enforce := c.Bool("enforce")

// nft add table ip gitpod
gitpodTable := nftcon.AddTable(&nftables.Table{
Expand Down Expand Up @@ -534,6 +539,11 @@ func main() {
return err
}

verdict := expr.VerdictAccept
if enforce {
verdict = expr.VerdictDrop
}

// nft add rule ip gitpod ratelimit ip protocol tcp ct state new meter ws-connections
// '{ ip daddr & 0.0.0.0 timeout 1m limit rate over 3000/minute burst 1000 packets }' counter name ws-connection-drop-stats drop
nftcon.AddRule(&nftables.Rule{
Expand Down Expand Up @@ -613,7 +623,7 @@ func main() {
},
// drop
&expr.Verdict{
Kind: expr.VerdictAccept,
Kind: verdict,
},
},
})
Expand Down
22 changes: 13 additions & 9 deletions components/ws-daemon/pkg/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,24 +97,28 @@ func NewDaemon(config Config, reg prometheus.Registerer) (*Daemon, error) {
return nil, xerrors.Errorf("cannot register cgroup plugin metrics: %w", err)
}

var configReloader CompositeConfigReloader
configReloader = append(configReloader, ConfigReloaderFunc(func(ctx context.Context, config *Config) error {
cgroupV1IOLimiter.Update(config.IOLimit.WriteBWPerSecond.Value(), config.IOLimit.ReadBWPerSecond.Value(), config.IOLimit.WriteIOPS, config.IOLimit.ReadIOPS)
cgroupV2IOLimiter.Update(config.IOLimit.WriteBWPerSecond.Value(), config.IOLimit.ReadBWPerSecond.Value(), config.IOLimit.WriteIOPS, config.IOLimit.ReadIOPS)
procV2Plugin.Update(config.ProcLimit)
return nil
}))

listener := []dispatch.Listener{
cpulimit.NewDispatchListener(&config.CPULimit, reg),
markUnmountFallback,
cgroupPlugins,
}

netlimiter := netlimit.NewConnLimiter(config.NetLimit, reg)
if config.NetLimit.Enabled {
listener = append(listener, netlimit.NewConnLimiter(config.NetLimit, reg))
listener = append(listener, netlimiter)
}

var configReloader CompositeConfigReloader
configReloader = append(configReloader, ConfigReloaderFunc(func(ctx context.Context, config *Config) error {
cgroupV1IOLimiter.Update(config.IOLimit.WriteBWPerSecond.Value(), config.IOLimit.ReadBWPerSecond.Value(), config.IOLimit.WriteIOPS, config.IOLimit.ReadIOPS)
cgroupV2IOLimiter.Update(config.IOLimit.WriteBWPerSecond.Value(), config.IOLimit.ReadBWPerSecond.Value(), config.IOLimit.WriteIOPS, config.IOLimit.ReadIOPS)
procV2Plugin.Update(config.ProcLimit)
if config.NetLimit.Enabled {
netlimiter.Update(config.NetLimit)
}
return nil
Comment on lines +116 to +119
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we able to disable netlimit without manually restarting ws-daemon?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are not able to completely disable it without restarting ws-daemon, but we can disable that traffic is dropped. In other words it would work like it does at the moment where the traffic is monitored but no action is taken.

}))

dsptch, err := dispatch.NewDispatch(containerRuntime, clientset, config.Runtime.KubernetesNamespace, nodename, listener...)
if err != nil {
return nil, err
Expand Down
1 change: 1 addition & 0 deletions components/ws-daemon/pkg/netlimit/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package netlimit

type Config struct {
Enabled bool `json:"enabled"`
Enforce bool `json:"enforce"`
ConnectionsPerMinute int64 `json:"connectionsPerMinute"`
BucketSize int64 `json:"bucketSize"`
}
21 changes: 17 additions & 4 deletions components/ws-daemon/pkg/netlimit/netlimit.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,12 @@ func NewConnLimiter(config Config, prom prometheus.Registerer) *ConnLimiter {

s.config = config

prom.MustRegister(
s.droppedBytes,
s.droppedPackets,
)
if config.Enabled {
prom.MustRegister(
s.droppedBytes,
s.droppedPackets,
)
}

return s
}
Expand Down Expand Up @@ -136,6 +138,9 @@ func (c *ConnLimiter) limitWorkspace(ctx context.Context, ws *dispatch.Workspace
err = nsinsider.Nsinsider(ws.InstanceID, int(pid), func(cmd *exec.Cmd) {
cmd.Args = append(cmd.Args, "setup-connection-limit", "--limit", strconv.Itoa(int(c.config.ConnectionsPerMinute)),
"--bucketsize", strconv.Itoa(int(c.config.BucketSize)))
if c.config.Enforce {
cmd.Args = append(cmd.Args, "--enforce")
}
}, nsinsider.EnterMountNS(false), nsinsider.EnterNetNS(true))
if err != nil {
log.WithError(err).WithFields(ws.OWI()).Error("cannot enable connection limiting")
Expand Down Expand Up @@ -171,3 +176,11 @@ func (c *ConnLimiter) limitWorkspace(ctx context.Context, ws *dispatch.Workspace

return nil
}

func (c *ConnLimiter) Update(config Config) {
c.mu.Lock()
defer c.mu.Unlock()

c.config = config
log.WithField("config", config).Info("updating network connection limits")
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion install/installer/cmd/testdata/render/minimal/output.golden

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions install/installer/pkg/components/ws-daemon/configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func configmap(ctx *common.RenderContext) ([]runtime.Object, error) {
var procLimit int64
networkLimitConfig := netlimit.Config{
Enabled: false,
Enforce: false,
ConnectionsPerMinute: 3000,
BucketSize: 1000,
}
Expand All @@ -74,6 +75,7 @@ func configmap(ctx *common.RenderContext) ([]runtime.Object, error) {
ioLimitConfig.ReadIOPS = ucfg.Workspace.IOLimits.ReadIOPS

networkLimitConfig.Enabled = ucfg.Workspace.NetworkLimits.Enabled
networkLimitConfig.Enforce = ucfg.Workspace.NetworkLimits.Enforce
networkLimitConfig.ConnectionsPerMinute = ucfg.Workspace.NetworkLimits.ConnectionsPerMinute
networkLimitConfig.BucketSize = ucfg.Workspace.NetworkLimits.BucketSize

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ type WorkspaceConfig struct {
} `json:"ioLimits"`
NetworkLimits struct {
Enabled bool `json:"enabled"`
Enforce bool `json:"enforce"`
ConnectionsPerMinute int64 `json:"connectionsPerMinute"`
BucketSize int64 `json:"bucketSize"`
} `json:"networkLimits"`
Expand Down