cloudflare / slirpnetstack

slirp4netns implementation using gvisor/netstack
BSD 3-Clause "New" or "Revised" License
115 stars 13 forks source link

Four new features. #27

Open KusakabeShi opened 3 years ago

KusakabeShi commented 3 years ago

Four new features.

  1. Customizable address
  2. Forward list
  3. Endpoint close detection
  4. New endpoint-type

1. Customizable address

Now we can set the address via command line. main.go image image

2. New endpoint-type

Add two new endpoint-type sock-server and sock-client main.go image

sock-server

sock-server mode will listen a socket until someone connected, then continue following routines. image With this mode, we can easily connect slirpnetstack to other programs like user-mode-linux

Setup slirpnetstack

bin/slirpnetstack \
  -endpoint-type=sock-server \
  -sock-protocol=unixpacket \
  -sock-server-listen=$HOME/slirpnetstack.sock \
  -allow=all \
  -nat-ipv4=192.168.42.0/24 \
  -gw-ipv4=192.168.42.1 \
  -fwd-default-ipv4=192.168.42.2 \
  -gw-macaddr=70:71:00:00:00:01 \
  -L=tcp://127.0.0.1:9897:192.168.42.2:8877

Slirpnetstack will listen on $HOME/slirpnetstack.sock with protocol unixpacket

Setup user-mode-linux

Now we can move on user-mode-linux part:

# build user-mode-linux
cd ~
git clone --depth 1 https://github.com/KusakabeSi/UML-Config UML-Config 
cd ~
git clone -b linux-5.10.y --single-branch --depth 1 https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git 
cd linux
cp ~/UML-Config/5.10.config .config
make linux ARCH=um SUBARCH=x86_64 -j $(nproc)

# Run user-mode-linux
mkdir -p /tmp/uml
export TEMP=/tmp/uml
./linux \
  root=/dev/root rootfstype=hostfs hostfs=/ \
  con=fd:0,fd:1 \
  vec0:transport=bess,dst=$HOME/slirpnetstack.sock,depth=128,gro=1 \
  rw mem=64M init=/bin/bash quiet

# Now we should be in the user-mode-linux shell, setup the network vec0
ip addr add 192.168.42.2/24 dev vec0
ip link set dev vec0 address 02:42:ac:10:00:03 
ip link set vec0 up
ip route add default via 192.168.42.1

# Check network, should output some html
wget -O- http://www.google.com

sock-client

sock-client mode will connect to somewhere by gosocket image

3. Endpoint close detection

Now, if the endpoint closed(fd closed or tap device removed by system admin), our program will also exit main.go image image image

4. IPPortRangeSlice alias

add alias all for IPPortRangeSlice, it ,means udp://0.0.0.0/0:0-65535,udp://[::]/0:0-65535,tcp://0.0.0.0/0:0-65535,tcp://[::]/0:0-65535 addr.go image

So that we can use all instead of a long string to allow all connection

bin/slirpnetstack \
  -endpoint-type=sock-server \
  -sock-protocol=unixpacket \
  -sock-server-listen=$HOME/slirpnetstack.sock \
  -allow=all \ # <-----------------------------------Here!!!!!!!!!!!!!!!
  -nat-ipv4=192.168.42.0/24 \
  -gw-ipv4=192.168.42.1 \
  -fwd-default-ipv4=192.168.42.2 \
  -gw-macaddr=70:71:00:00:00:01 \
  -L=tcp://127.0.0.1:9897:192.168.42.2:8877

Support go1.17

Although golang has backwards compatibility, but gvisor@v0.0.0-20201204040109-0ba39926c86f preventing it compile at golang 1.17 explicitly.
So I upgrade it to gvisor@v0.0.0-20211008224438-0927c63f8ea7 to make this slirpnetstack compatible with golang 1.17.
If there is any problem with it, just revert the commit 0b88429feda6d8c7d6a181a160c2c577437d7a54.

JuniorJPDJ commented 1 year ago

I tested it and it works good :D