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:
parent
ac61d453de
commit
c5d79b7fdc
6
.env
6
.env
|
@ -1,8 +1,8 @@
|
||||||
# Configuration for xbnet0
|
# Configuration for xbnet0
|
||||||
XBEE0_PORT=/dev/ttyUSB0
|
XBEE0_PORT=/dev/ttyUSB0
|
||||||
XBEE0_BAUDRATE=230400
|
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_SRC_IP=192.168.1.10 # Ensure this IP matches the macvlan network in Docker Compose
|
||||||
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_DST_IP=192.168.1.11 # Ensure this IP matches the network range
|
||||||
XBEE0_NET_IFACE_NAME=xbnet0
|
XBEE0_NET_IFACE_NAME=xbnet0
|
||||||
|
|
||||||
# Configuration for xbnet1
|
# 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_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
|
XBEE1_NET_IFACE_NAME=xbnet1
|
||||||
|
|
||||||
|
# Default Gateway
|
||||||
|
DEFAULT_GATEWAY=192.168.1.1
|
11
Dockerfile
11
Dockerfile
|
@ -1,7 +1,7 @@
|
||||||
# Use an official Rust image as the base
|
# Use an official Rust image as the base
|
||||||
FROM rust:latest
|
FROM rust:latest
|
||||||
|
|
||||||
# Install necessary packages including supervisord
|
# Install necessary packages
|
||||||
RUN apt-get update && apt-get install -y \
|
RUN apt-get update && apt-get install -y \
|
||||||
libudev-dev \
|
libudev-dev \
|
||||||
iproute2 \
|
iproute2 \
|
||||||
|
@ -10,31 +10,40 @@ RUN apt-get update && apt-get install -y \
|
||||||
bridge-utils \
|
bridge-utils \
|
||||||
iptables \
|
iptables \
|
||||||
supervisor \
|
supervisor \
|
||||||
|
traceroute \
|
||||||
&& rm -rf /var/lib/apt/lists/*
|
&& rm -rf /var/lib/apt/lists/*
|
||||||
|
|
||||||
# Clone the xbnet repository
|
# 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
|
RUN git clone https://github.com/jgoerzen/xbnet.git /usr/src/xbnet
|
||||||
|
|
||||||
# Build xbnet
|
# Build xbnet
|
||||||
|
# We build the xbnet project from source using Cargo, Rust's package manager and build system
|
||||||
WORKDIR /usr/src/xbnet
|
WORKDIR /usr/src/xbnet
|
||||||
RUN cargo build --release
|
RUN cargo build --release
|
||||||
|
|
||||||
# Copy the built binary to /usr/local/bin
|
# 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
|
RUN cp target/release/xbnet /usr/local/bin/xbnet
|
||||||
|
|
||||||
# Copy the entrypoint script
|
# Copy the entrypoint script
|
||||||
|
# The entrypoint script handles the setup and monitoring of the XBee network
|
||||||
COPY ./scripts/entrypoint.sh /entrypoint.sh
|
COPY ./scripts/entrypoint.sh /entrypoint.sh
|
||||||
RUN chmod +x /entrypoint.sh
|
RUN chmod +x /entrypoint.sh
|
||||||
|
|
||||||
# Copy the supervisor config file
|
# 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 supervisord.conf /etc/supervisor/conf.d/supervisord.conf
|
||||||
|
|
||||||
# Copy the health check script
|
# 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
|
COPY ./scripts/health_check.sh /health_check.sh
|
||||||
RUN chmod +x /health_check.sh
|
RUN chmod +x /health_check.sh
|
||||||
|
|
||||||
# Start supervisord as the main command
|
# 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"]
|
CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf"]
|
||||||
|
|
||||||
# Add healthcheck
|
# Add healthcheck
|
||||||
|
# The health check command periodically checks the health of the container using a custom script
|
||||||
HEALTHCHECK CMD /health_check.sh || exit 1
|
HEALTHCHECK CMD /health_check.sh || exit 1
|
||||||
|
|
|
@ -26,6 +26,7 @@
|
||||||
# SERVICES-XBEE-NET
|
# 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.
|
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
|
## File Structure
|
||||||
```
|
```
|
||||||
|
|
|
@ -6,14 +6,40 @@ services:
|
||||||
container_name: xbee_node
|
container_name: xbee_node
|
||||||
privileged: true
|
privileged: true
|
||||||
env_file: .env
|
env_file: .env
|
||||||
restart: always
|
|
||||||
networks:
|
networks:
|
||||||
default:
|
# xbee_net:
|
||||||
ipv4_address: ${XBEE0_NET_SRC_IP}
|
# 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:
|
networks:
|
||||||
default:
|
# 1. Docker's internal way of communicating between host and containers - Support 2 way comms
|
||||||
driver: bridge
|
# 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:
|
ipam:
|
||||||
config:
|
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
|
||||||
|
|
|
@ -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."
|
|
@ -1,7 +1,7 @@
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
# Source the get_connected_wifi_info.sh script
|
# 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 if the script is running with root privileges
|
||||||
check_root() {
|
check_root() {
|
||||||
|
@ -32,6 +32,7 @@ cleanup() {
|
||||||
}
|
}
|
||||||
|
|
||||||
# Function to check if the XBee device is connected
|
# 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() {
|
check_xbee_device() {
|
||||||
if [ -e "$XBEE0_PORT" ]; then
|
if [ -e "$XBEE0_PORT" ]; then
|
||||||
echo "XBee device found at $XBEE0_PORT. Proceeding with setup..."
|
echo "XBee device found at $XBEE0_PORT. Proceeding with setup..."
|
||||||
|
@ -43,21 +44,25 @@ check_xbee_device() {
|
||||||
}
|
}
|
||||||
|
|
||||||
# Start the xbnet interface
|
# Start the xbnet interface
|
||||||
# - Also check if the starting the xbnet interface was successful
|
# - Attempts to start the xbnet interface in tap mode
|
||||||
# - And return appropriate status codes
|
# - Verifies if the interface is successfully created and returns the appropriate status code
|
||||||
start_xbnet_interface() {
|
start_xbnet_interface() {
|
||||||
echo "Starting XBee network interface..."
|
echo "Starting XBee network interface..."
|
||||||
xbnet -d --serial-speed $XBEE0_BAUDRATE $XBEE0_PORT tap &
|
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
|
while [ ! -d "/sys/class/net/$XBEE0_NET_IFACE_NAME" ]; do
|
||||||
echo "Waiting for interface $XBEE0_NET_IFACE_NAME to be created..."
|
echo "Waiting for interface $XBEE0_NET_IFACE_NAME to be created..."
|
||||||
sleep 1
|
sleep 1
|
||||||
done
|
done
|
||||||
|
|
||||||
|
# Get the xbnet interface name (e.g., xbnet0) automatically
|
||||||
XBEE0_NET_IFACE_NAME=$(ls /sys/class/net | grep 'xbnet')
|
XBEE0_NET_IFACE_NAME=$(ls /sys/class/net | grep 'xbnet')
|
||||||
|
|
||||||
|
# Check if the xbnet interface is found
|
||||||
if [ -z "$XBEE0_NET_IFACE_NAME" ]; then
|
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
|
return 1
|
||||||
else
|
else
|
||||||
echo "XBee network interface $XBEE0_NET_IFACE_NAME created successfully."
|
echo "XBee network interface $XBEE0_NET_IFACE_NAME created successfully."
|
||||||
|
@ -66,10 +71,9 @@ start_xbnet_interface() {
|
||||||
}
|
}
|
||||||
|
|
||||||
# Configure the network interface and bridge
|
# Configure the network interface and bridge
|
||||||
# - Create interface with name specified by XBEE0_NET_IFACE_NAME in .env file
|
# - Creates the network interface with the name specified by XBEE0_NET_IFACE_NAME in .env file
|
||||||
# - Create bridge interface (this bridge will be used to connect xbnet to host wifi)
|
# - Creates a bridge interface and attaches the xbnet interface to it
|
||||||
# - Attach xbnet to bridge
|
# - Brings up the bridge interface
|
||||||
# - Start the bridge interface
|
|
||||||
configure_network_and_bridge() {
|
configure_network_and_bridge() {
|
||||||
ip addr add $XBEE0_NET_SRC_IP/24 dev $XBEE0_NET_IFACE_NAME
|
ip addr add $XBEE0_NET_SRC_IP/24 dev $XBEE0_NET_IFACE_NAME
|
||||||
ip link set dev $XBEE0_NET_IFACE_NAME up
|
ip link set dev $XBEE0_NET_IFACE_NAME up
|
||||||
|
@ -80,10 +84,9 @@ configure_network_and_bridge() {
|
||||||
}
|
}
|
||||||
|
|
||||||
# Monitor and bridge Wi-Fi
|
# Monitor and bridge Wi-Fi
|
||||||
# - If a connected wifi interface is found
|
# - Continuously checks for a connected Wi-Fi interface
|
||||||
# - Add it to the bridge interface with xbnet
|
# - Bridges the Wi-Fi interface with the xbnet interface when found
|
||||||
# - Else
|
# - If no Wi-Fi interface is found, continues checking every 5 seconds
|
||||||
# - keep looking for a newly established connection with a wifi interface
|
|
||||||
monitor_and_bridge_wifi() {
|
monitor_and_bridge_wifi() {
|
||||||
while true; do
|
while true; do
|
||||||
HOST_WIFI_IFACE=$(get_connected_wifi_device)
|
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
|
# 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() {
|
monitor_xbee_device() {
|
||||||
while [ -e "$XBEE0_PORT" ]; do
|
while [ -e "$XBEE0_PORT" ]; do
|
||||||
sleep 5
|
sleep 5
|
||||||
|
@ -116,6 +121,8 @@ monitor_xbee_device() {
|
||||||
}
|
}
|
||||||
|
|
||||||
# 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() {
|
setup_xbee_network() {
|
||||||
while true; do
|
while true; do
|
||||||
check_xbee_device
|
check_xbee_device
|
||||||
|
|
Loading…
Reference in New Issue