@@ -17,8 +17,11 @@ import (
1717 "time"
1818
1919 "connectrpc.com/connect"
20+ "github.com/containernetworking/plugins/pkg/ns"
21+ "github.com/coreos/go-iptables/iptables"
2022 "github.com/google/uuid"
2123 "github.com/launchdarkly/go-sdk-common/v3/ldlog"
24+ "github.com/vishvananda/netlink"
2225 "golang.org/x/sys/unix"
2326
2427 "github.com/e2b-dev/infra/packages/clickhouse/pkg/hoststats"
@@ -56,6 +59,7 @@ func main() {
5659 iterations := flag .Int ("iterations" , 0 , "run N iterations (0 = interactive)" )
5760 coldStart := flag .Bool ("cold" , false , "clear cache between iterations (cold start each time)" )
5861 noPrefetch := flag .Bool ("no-prefetch" , false , "disable memory prefetching" )
62+ noEgress := flag .Bool ("no-egress" , false , "block all guest internet egress" )
5963 verbose := flag .Bool ("v" , false , "verbose logging" )
6064
6165 // Command execution (no pause)
@@ -155,7 +159,7 @@ func main() {
155159 iterations : * iterations ,
156160 }
157161
158- err := run (ctx , * fromBuild , * iterations , * coldStart , * noPrefetch , * verbose , pauseOpts , runOpts )
162+ err := run (ctx , * fromBuild , * iterations , * coldStart , * noPrefetch , * noEgress , * verbose , pauseOpts , runOpts )
159163 cancel ()
160164
161165 if err != nil {
@@ -955,7 +959,7 @@ func (r *runner) benchmark(ctx context.Context, n int) error {
955959 return lastErr
956960}
957961
958- func run (ctx context.Context , buildID string , iterations int , coldStart , noPrefetch , verbose bool , pauseOpts pauseOptions , runOpts runOptions ) error {
962+ func run (ctx context.Context , buildID string , iterations int , coldStart , noPrefetch , noEgress , verbose bool , pauseOpts pauseOptions , runOpts runOptions ) error {
959963 // Silence other loggers unless verbose mode
960964 var l logger.Logger
961965 if ! verbose {
@@ -1006,10 +1010,15 @@ func run(ctx context.Context, buildID string, iterations int, coldStart, noPrefe
10061010 go tcpFw .Start (ctx )
10071011 defer tcpFw .Close (context .WithoutCancel (ctx ))
10081012
1013+ var egressProxy network.EgressProxy = network.NoopEgressProxy {}
1014+ if noEgress {
1015+ egressProxy = noEgressProxy {}
1016+ }
1017+
10091018 if verbose {
10101019 fmt .Println ("🔧 Creating network storage..." )
10111020 }
1012- slotStorage , err := network .NewStorageLocal (ctx , config .NetworkConfig , network. NoopEgressProxy {} )
1021+ slotStorage , err := network .NewStorageLocal (ctx , config .NetworkConfig , egressProxy )
10131022 if err != nil {
10141023 return fmt .Errorf ("network storage: %w" , err )
10151024 }
@@ -1066,7 +1075,7 @@ func run(ctx context.Context, buildID string, iterations int, coldStart, noPrefe
10661075 if verbose {
10671076 fmt .Println ("🔧 Creating sandbox factory..." )
10681077 }
1069- factory := sandbox .NewFactory (config .BuilderConfig , networkPool , devicePool , flags , hoststats .NewNoopDelivery (), cgroup .NewNoopManager (), network . NewNoopEgressProxy () , sandboxes )
1078+ factory := sandbox .NewFactory (config .BuilderConfig , networkPool , devicePool , flags , hoststats .NewNoopDelivery (), cgroup .NewNoopManager (), egressProxy , sandboxes )
10701079
10711080 fmt .Printf ("📦 Loading %s...\n " , buildID )
10721081 tmpl , err := cache .GetTemplate (ctx , buildID , false , false )
@@ -1458,3 +1467,32 @@ func (t *noPrefetchTemplate) Metadata() (metadata.Template, error) {
14581467
14591468 return meta , nil
14601469}
1470+
1471+ // noEgressProxy is an EgressProxy that removes the default route from the
1472+ // sandbox's netns at slot-creation time.
1473+ type noEgressProxy struct {
1474+ network.NoopEgressProxy
1475+ }
1476+
1477+ func (noEgressProxy ) OnSlotCreate (s * network.Slot , _ * iptables.IPTables ) error {
1478+ nsPath := filepath .Join ("/var/run/netns" , s .NamespaceID ())
1479+
1480+ handle , err := ns .GetNS (nsPath )
1481+ if err != nil {
1482+ return fmt .Errorf ("get netns %q: %w" , nsPath , err )
1483+ }
1484+ defer handle .Close ()
1485+
1486+ // Match the route installed earlier in Slot.CreateNetwork:
1487+ // Scope = SCOPE_UNIVERSE, Gw = VethIP.
1488+ return handle .Do (func (_ ns.NetNS ) error {
1489+ if err := netlink .RouteDel (& netlink.Route {
1490+ Scope : netlink .SCOPE_UNIVERSE ,
1491+ Gw : s .VethIP (),
1492+ }); err != nil {
1493+ return fmt .Errorf ("delete default route in %s: %w" , s .NamespaceID (), err )
1494+ }
1495+
1496+ return nil
1497+ })
1498+ }
0 commit comments