hashicorp / vagrant

Vagrant is a tool for building and distributing development environments.
https://www.vagrantup.com
Other
26.01k stars 4.42k forks source link

Cannot hit UDP server in vagrant provisioned Ubuntu 22.04 VM (using VMware Fusion) from host macOS Sonoma/Apple Silicon #13407

Closed prsr55 closed 3 weeks ago

prsr55 commented 3 weeks ago

Debug output

https://gist.github.com/prsr55/f49ef54c6be337efc72159295f6a0ba5

Expected behavior

UDP client requests on host (macOS, Apple Silicon) should be able to hit UDP server in vagrant provisioned Ubuntu 22.04 ARM VM

Actual behavior

Reproduction information

Here is the sample server and client that I ran in the combinations above. Code taken from https://wiki.python.org/moin/UdpCommunication#Receiving

udpClient.py:

import socket

UDP_IP = "127.0.0.1"
UDP_PORT = <try with a port from the vagrant file>
MESSAGE = b"Hello, World!"

print("UDP target IP: %s" % UDP_IP)
print("UDP target port: %s" % UDP_PORT)
print("message: %s" % MESSAGE)
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP
sock.sendto(MESSAGE, (UDP_IP, UDP_PORT))

udpServer.py:

UDP_IP = "127.0.0.1"
UDP_PORT = <try with a port from the vagrant file>

sock = socket.socket(socket.AF_INET, # Internet
                     socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))

while True:
    data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
    print("received message: %s" % data)

I also tried sending from my macOS host with the following in the terminal:

nc -u 127.0.0.1 <try appropriate open UDP port>
nc -u 127.0.0.1 <try appropriate open UDP port>
nc -u 127.0.0.1:<try appropriate open UDP port>
nc -u 0.0.0.0 <try appropriate open UDP port>

Vagrant version

Vagrant 2.4.1 (matches latest version I see for arm64 at the time of this post from https://developer.hashicorp.com/vagrant/downloads)

Host operating system

macOS Sonoma 14.4.1

Guest operating system

No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 22.04.4 LTS
Release:    22.04
Codename:   jammy

Steps to reproduce

  1. Start the UDP server from within the VM with python3 udpServer.py
  2. Start the UDP client from within the macOS host with python3 udpClient.py, or try with the nc commands
  3. Nothing is received

I've tried with various combinations of the ports listed below. The UDP port always fails.

I'm not sure if it matters but I noticed if I switch from my 'Standard' macOS user to my sudo user to list all the open ports, I still see the ports listed as open, even after I vagrant halt. Is that expected behavior (see ports 5000, 14500, 17001, 18001, which are all in my vagrant file, still show as open)? Or if I've already done vagrant halt, but try and re-use one of hte ports below in my python server, I get an error that that port is in use already (commands taken from https://stackoverflow.com/questions/4421633/who-is-listening-on-a-given-tcp-port-on-mac-os-x?newreg=17ce3002e2e94e86a88ced5fa1a23f5f):

☁  ~  sudo lsof -iUDP -n -P
COMMAND     PID       USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
...
vagrant-v   900           root   12u  IPv6 0xe88d90fb9a0d4965      0t0  UDP *:14500
vagrant-v   900           root   15u  IPv6 0xe88d90fb9a0d1565      0t0  UDP *:17001
vagrant-v   900           root   18u  IPv6 0xe88d90fb9a0d1d65      0t0  UDP *:18001
...
☁  ~  netstat -p tcp -p udp
Active Internet connections
Proto Recv-Q Send-Q  Local Address          Foreign Address        (state)
...
udp46      0      0  *.18001                *.*
...
udp46      0      0  *.17001                *.*
...
udp4       0      0  *.14550                *.*

Vagrantfile

Vagrant.configure("2") do |config|
  config.vm.box = "bento/ubuntu-22.04-arm64"
  config.ssh.forward_agent = true  
  config.vm.network "forwarded_port", guest: 5000, host: 5000, protocol: "tcp"
  config.vm.network "forwarded_port", guest: 14500, host: 14500, protocol: "tcp"
  config.vm.network "forwarded_port", guest: 14500, host: 14500, protocol: "udp"
  config.vm.network "forwarded_port", guest: 17001, host: 17001, protocol: "tcp"
  config.vm.network "forwarded_port", guest: 17001, host: 17001, protocol: "udp"
  config.vm.network "forwarded_port", guest: 18001, host: 18001, protocol: "tcp"
  config.vm.network "forwarded_port", guest: 18001, host: 18001, protocol: "udp"
# misc apt-get installs, cpu/ram config, turning on GUI, etc. removed
end
prsr55 commented 3 weeks ago

Update: If I change the IP to 0.0.0.0 on teh server, I was able to receive a UDP packet from the sample python programs (though interestingly if I change it back to localhost and back to 0.0.0.0, it doesn't work unless i vagrant reload). No luck with nc, I will try a fixed IP for the vagrant Vm and report back https://developer.hashicorp.com/vagrant/docs/networking/private_network

prsr55 commented 3 weeks ago

For anyone else that has this issue, I also did the following:

  1. added config.vm.network "private_network", type: "dhcp" in my vagrantfile
  2. got the IP addr for eth1 in ip addr show from within my ubuntu VM
  3. set that Addr for both teh server python script (in the ubuntu) and host client script (in the macOS)
  4. packets flow fine