Fixed bridging issues with ipvlan which allows comms from container to host but not the other way around. Macvlan not working, which would be ideal for network isolation and 2 way comms. 3rd route to manually bridge xbnet0 and eth0 interfaces over br0, but this is least appealing. Need to find a way to make macvlan work

This commit is contained in:
Emran Billah 2024-08-16 18:07:17 -03:00
parent ac61d453de
commit c5d79b7fdc
6 changed files with 89 additions and 26 deletions

6
.env
View File

@ -1,8 +1,8 @@
# Configuration for xbnet0
XBEE0_PORT=/dev/ttyUSB0
XBEE0_BAUDRATE=230400
XBEE0_NET_SRC_IP=192.168.1.10 # Note: This requires the subnet 192.168.1... (to match with the default gateway, or use Masquerade if changing the subnet)
XBEE0_NET_DST_IP=192.168.1.11 # Note: This requires the subnet 192.168.1... (to match with the default gateway, or use Masquerade if changing the subnet)
XBEE0_NET_SRC_IP=192.168.1.10 # Ensure this IP matches the macvlan network in Docker Compose
XBEE0_NET_DST_IP=192.168.1.11 # Ensure this IP matches the network range
XBEE0_NET_IFACE_NAME=xbnet0
# Configuration for xbnet1
@ -12,3 +12,5 @@ XBEE1_NET_SRC_IP=192.168.1.11 # Note: This requires the subnet 192.168.1... (t
XBEE1_NET_DST_IP=192.168.1.10 # Note: This requires the subnet 192.168.1... (to match with the default gateway, or use Masquerade if changing the subnet)
XBEE1_NET_IFACE_NAME=xbnet1
# Default Gateway
DEFAULT_GATEWAY=192.168.1.1

View File

@ -1,7 +1,7 @@
# Use an official Rust image as the base
FROM rust:latest
# Install necessary packages including supervisord
# Install necessary packages
RUN apt-get update && apt-get install -y \
libudev-dev \
iproute2 \
@ -10,31 +10,40 @@ RUN apt-get update && apt-get install -y \
bridge-utils \
iptables \
supervisor \
traceroute \
&& rm -rf /var/lib/apt/lists/*
# Clone the xbnet repository
# The xbnet repository contains the source code needed to set up and manage the XBee network
RUN git clone https://github.com/jgoerzen/xbnet.git /usr/src/xbnet
# Build xbnet
# We build the xbnet project from source using Cargo, Rust's package manager and build system
WORKDIR /usr/src/xbnet
RUN cargo build --release
# Copy the built binary to /usr/local/bin
# The xbnet binary will be placed in /usr/local/bin to be accessible system-wide
RUN cp target/release/xbnet /usr/local/bin/xbnet
# Copy the entrypoint script
# The entrypoint script handles the setup and monitoring of the XBee network
COPY ./scripts/entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
# Copy the supervisor config file
# Supervisor configuration ensures that the XBee network service is managed and stays running
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
# Copy the health check script
# The health check script will be used by Docker to monitor the health of the container
COPY ./scripts/health_check.sh /health_check.sh
RUN chmod +x /health_check.sh
# Start supervisord as the main command
# Supervisord will manage and monitor the services, including the XBee network
CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf"]
# Add healthcheck
# The health check command periodically checks the health of the container using a custom script
HEALTHCHECK CMD /health_check.sh || exit 1

View File

@ -26,6 +26,7 @@
# SERVICES-XBEE-NET
This project contains a Dockerized setup to create and manage an XBee network using the `xbnet` utility. The network interface is bridged with a host Wi-Fi interface, enabling internet access for connected devices.
##### Note: This project uses the `tap` xbnet protocol, which is a layer 2 protocol. This supports full ethernet pipeline. For a simple IP level protocol support, the `tun` xbnet protocol can be used.
## File Structure
```

View File

@ -6,14 +6,40 @@ services:
container_name: xbee_node
privileged: true
env_file: .env
restart: always
networks:
default:
ipv4_address: ${XBEE0_NET_SRC_IP}
# xbee_net:
# ipv4_address: ${XBEE0_NET_SRC_IP} # Ensure this IP belongs to the xbee_net subnet
ipvlan_net:
ipv4_address: 192.168.1.20 # Assign an IP within the same range as your host's Wi-Fi
# macvlan_net:
# ipv4_address: 192.168.1.20 # Assign an IP within the same range as your host's Wi-Fi
networks:
default:
driver: bridge
# 1. Docker's internal way of communicating between host and containers - Support 2 way comms
# xbee_net:
# driver: bridge
# ipam:
# config:
# - subnet: 192.168.2.0/24 # Ensure your xbnet IPs are in this range
# 2. ipvlan working ok for comms from container to host, but not host to container
ipvlan_net:
driver: ipvlan
driver_opts:
mode: l3 # Use L3 mode, which is simpler for most setups
parent: wlp0s20f3 # This should be your host's Wi-Fi or Ethernet interface
ipam:
config:
- subnet: 192.168.1.0/24 # Note: the xbnet assigned IPs must be on this subnet (if Masquerade is not used)
- subnet: 192.168.1.0/24 # Match the Wi-Fi network range
gateway: 192.168.1.1 # Your router's gateway
# 3. macvlan not working for some reason
macvlan_net:
# driver: macvlan
# driver_opts:
# parent: wlp0s20f3 # This should be your host's Wi-Fi interface
# ipam:
# config:
# - subnet: 192.168.1.0/24 # Match the Wi-Fi network range
# gateway: ${DEFAULT_GATEWAY} # Your router's gateway

18
scripts/enable_host_macvlan.sh Executable file
View File

@ -0,0 +1,18 @@
#!/bin/bash
# Enable IP forwarding
sudo sysctl net.ipv4.ip_forward
# Delete the existing macvlan0 interface if it exists
if ip link show macvlan0 &> /dev/null; then
echo "Deleting existing macvlan0 interface..."
sudo ip link delete macvlan0
fi
# Create a new macvlan0 interface
echo "Creating macvlan0 interface..."
sudo ip link add link wlp0s20f3 macvlan0 type macvlan mode bridge
sudo ip addr add 192.168.1.100/24 dev macvlan0 # Use an IP in the same range as the container network but different from any assigned IP
sudo ip link set macvlan0 up
echo "macvlan0 interface created and set up successfully."

View File

@ -1,7 +1,7 @@
#!/bin/bash
# Source the get_connected_wifi_info.sh script
source ./get_connected_wifi_info.sh
source /scripts/get_connected_wifi_info.sh
# Check if the script is running with root privileges
check_root() {
@ -12,10 +12,10 @@ check_root() {
}
# Clean up resources
# - Kill the background process used to create the xbnet interface
# - Cleanup any resources created or copied over. This includes:
# - Bridge interface
# - xbnet interface
# - Kill the background process used to create the xbnet interface
# - Clean up any resources created or copied over. This includes:
# - Bridge interface
# - xbnet interface
cleanup() {
echo "Cleaning up resources..."
pkill -f "xbnet -d --serial-speed $XBEE0_BAUDRATE $XBEE0_PORT tap"
@ -32,6 +32,7 @@ cleanup() {
}
# Function to check if the XBee device is connected
# - Ensures that the XBee device is present at the specified port before proceeding
check_xbee_device() {
if [ -e "$XBEE0_PORT" ]; then
echo "XBee device found at $XBEE0_PORT. Proceeding with setup..."
@ -43,21 +44,25 @@ check_xbee_device() {
}
# Start the xbnet interface
# - Also check if the starting the xbnet interface was successful
# - And return appropriate status codes
# - Attempts to start the xbnet interface in tap mode
# - Verifies if the interface is successfully created and returns the appropriate status code
start_xbnet_interface() {
echo "Starting XBee network interface..."
xbnet -d --serial-speed $XBEE0_BAUDRATE $XBEE0_PORT tap &
# Wait until the xbnet interface is created
while [ ! -d "/sys/class/net/$XBEE0_NET_IFACE_NAME" ]; do
echo "Waiting for interface $XBEE0_NET_IFACE_NAME to be created..."
sleep 1
done
# Get the xbnet interface name (e.g., xbnet0) automatically
XBEE0_NET_IFACE_NAME=$(ls /sys/class/net | grep 'xbnet')
# Check if the xbnet interface is found
if [ -z "$XBEE0_NET_IFACE_NAME" ]; then
echo "Error: No XBee network interface found."
echo "Error: No XBee network interface found. Retrying setup..."
cleanup
return 1
else
echo "XBee network interface $XBEE0_NET_IFACE_NAME created successfully."
@ -66,10 +71,9 @@ start_xbnet_interface() {
}
# Configure the network interface and bridge
# - Create interface with name specified by XBEE0_NET_IFACE_NAME in .env file
# - Create bridge interface (this bridge will be used to connect xbnet to host wifi)
# - Attach xbnet to bridge
# - Start the bridge interface
# - Creates the network interface with the name specified by XBEE0_NET_IFACE_NAME in .env file
# - Creates a bridge interface and attaches the xbnet interface to it
# - Brings up the bridge interface
configure_network_and_bridge() {
ip addr add $XBEE0_NET_SRC_IP/24 dev $XBEE0_NET_IFACE_NAME
ip link set dev $XBEE0_NET_IFACE_NAME up
@ -80,10 +84,9 @@ configure_network_and_bridge() {
}
# Monitor and bridge Wi-Fi
# - If a connected wifi interface is found
# - Add it to the bridge interface with xbnet
# - Else
# - keep looking for a newly established connection with a wifi interface
# - Continuously checks for a connected Wi-Fi interface
# - Bridges the Wi-Fi interface with the xbnet interface when found
# - If no Wi-Fi interface is found, continues checking every 5 seconds
monitor_and_bridge_wifi() {
while true; do
HOST_WIFI_IFACE=$(get_connected_wifi_device)
@ -106,6 +109,8 @@ monitor_and_bridge_wifi() {
}
# Function to monitor the XBee device and restart setup if disconnected
# - Periodically checks if the XBee device is still connected
# - If the device is disconnected, triggers cleanup and restarts the setup process
monitor_xbee_device() {
while [ -e "$XBEE0_PORT" ]; do
sleep 5
@ -115,7 +120,9 @@ monitor_xbee_device() {
cleanup
}
# Main function to set up the XBee network
# Main function to set up the XBee network
# - Runs the setup process in a loop to ensure continuous operation
# - If any step fails, it retries the entire process after a short delay
setup_xbee_network() {
while true; do
check_xbee_device