Skip to content

Commit

Permalink
Adding updates to automatically allocate the infra nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
test authored and test committed Aug 26, 2024
1 parent cea189b commit 98d4485
Show file tree
Hide file tree
Showing 28 changed files with 1,052 additions and 183 deletions.
6 changes: 6 additions & 0 deletions apps/Arm/vm_multi/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,12 @@ AddFileToOverlayDir(
"root"
overlay_vm0
)
AddFileToOverlayDir(
"build-router.sh"
${CMAKE_CURRENT_SOURCE_DIR}/network/build-router.sh
"root"
overlay_vm0
)
AddFileToOverlayDir(
"connect-vlans"
${CMAKE_CURRENT_SOURCE_DIR}/network/connect-vlans
Expand Down
126 changes: 126 additions & 0 deletions apps/Arm/vm_multi/network/build-router.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
#!/bin/sh

set -e

host="127.0.0.1"
dev_one="wg0"
dev_two="wg1"
ip1="127.0.0.1"
ip2="127.0.0.1"

# Help menu
show_help() {
echo "Adds an entry into /etc/hosts on a remote host"
echo
echo "Usage: $0 [options] host dev1 ip1 dev2 ip2"
echo
echo "Options:"
echo " --host, Specify the node the router should be built on"
echo " --dev1, Specify the first device IPs should be routed from/to"
echo " --ip1, Specify the ip and subnet the first device needs to forward"
echo " --dev2, Specify the second device IPs should be routed from/to"
echo " --ip2, Specify the ip and subnet the second device needs to forward"
echo
}


# Parse CLI
while [[ $# -gt 0 ]]; do
case $1 in
--host)
shift
host="$1"
shift
;;
--dev1)
shift
dev_one="$1"
shift
;;
--dev2)
shift
dev_two="$1"
shift
;;
--ip1)
shift
ip1="$1"
shift
;;
--ip2)
shift
ip2="$1"
shift
;;
--help)
show_help
exit 0
;;
esac
done

if [[ -z $host && $# -ge 1 ]]; then
host_ip="$1"
shift
fi
if [[ -z $dev_one && $# -ge 1 ]]; then
dev_one="$1"
shift
fi
if [[ -z $ip1 && $# -ge 1 ]]; then
ip1="$1"
shift
fi
if [[ -z $dev_two && $# -ge 1 ]]; then
dev_two="$1"
shift
fi
if [[ -z $ip2 && $# -ge 1 ]]; then
ip2="$1"
shift
fi



# Verify we have all the arguments
if [[ -z $host || -z $dev_one || -z $dev_two || -z $ip1|| -z $ip2 ]]; then
echo "Error: host, dev1, ip1, dev2, and ip2 are required."
show_help
exit 1
fi

# Set up interface one on the node
echo ip addr add $ip1 dev $dev_one
sshpass -p "root" dbclient -y "$host" "ip addr add $ip1 dev $dev_one"
echo ip link set $dev_one up
sshpass -p "root" dbclient -y "$host" "ip link set $dev_one up"

# Set up interface two on the node
echo ip addr add $ip2 dev $dev_two
sshpass -p "root" dbclient -y "$host" "ip addr add $ip2 dev $dev_two"
echo ip link set $dev_two up
sshpass -p "root" dbclient -y "$host" "ip link set $dev_two up"


# Enable IP forwarding
echo sshpass -p "root" dbclient -y "$host" "echo 1 | tee /proc/sys/net/ipv4/ip_forward"
sshpass -p "root" dbclient -y "$host" "echo 1 | tee /proc/sys/net/ipv4/ip_forward"

echo sshpass -p "root" dbclient -y "$host" "echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf"
sshpass -p "root" dbclient -y "$host" "echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf"

echo sshpass -p "root" dbclient -y "$host" "sysctl -p"
sshpass -p "root" dbclient -y "$host" "sysctl -p"

# Allow forwarding from Network 1 to Network 2
echo sshpass -p "root" dbclient -y "$host" "iptables -A FORWARD -i $dev_one -o $dev_two -j ACCEPT"
sshpass -p "root" dbclient -y "$host" "iptables -A FORWARD -i $dev_one -o $dev_two -j ACCEPT"

# Allow forwarding from Network 2 to Network 1
echo sshpass -p "root" dbclient -y "$host" "iptables -A FORWARD -i $dev_two -o $dev_one -j ACCEPT"
sshpass -p "root" dbclient -y "$host" "iptables -A FORWARD -i $dev_two -o $dev_one -j ACCEPT"

# Set up masquerading on the interface
echo iptables -t nat -A POSTROUTING -o $dev_one -j MASQUERADE
sshpass -p "root" dbclient -y "$host" "iptables -t nat -A POSTROUTING -o $dev_one -j MASQUERADE"

46 changes: 35 additions & 11 deletions apps/Arm/vm_multi/network/config.yml
Original file line number Diff line number Diff line change
@@ -1,19 +1,43 @@
data_paths:
name:
node1:
ingest: # Just wireguard settings
InterfaceName: wg0 # Will default to its order in the build list, if not provided
PrivateKey: /etc/wireguard/privkey
Address: 10.198.10.113
ListenPort: 5891
peers:
Name:
PublicKeys: t1A4mXWKHd2R3zlnDaDoFx/bCTHEz/FP6CK1PpdVzBs= #pubkey for example-wg-priv.txt
AllowedIPs:
- 10.198.10.1/24
Endpoint: 10.10.10.1:5891
# InterfaceName: wg0 # Will default to its order in the build list, if not provided
# PrivateKey: /etc/wireguard/privkey
# Address: 10.198.10.113
# ListenPort: 5891
# peers:
# Name:
# PublicKeys: t1A4mXWKHd2R3zlnDaDoFx/bCTHEz/FP6CK1PpdVzBs= #pubkey for example-wg-priv.txt
# AllowedIPs:
# - 10.198.10.1
# - 10.192.10.0/24
# Endpoint: 10.10.10.1:5861
VID: 3000
functions:
silent:
tmp: testing
router:
name: r1

node2:
ingest: # Just wireguard settings
# InterfaceName: wg1 # Will default to its order in the build list, if not provided
# PrivateKey: /etc/wireguard/privkey
# Address: 10.199.10.113
# ListenPort: 5892
# peers:
# Name:
# PublicKeys: t1A4mXWKHd2R3zlnDaDoFx/bCTHEz/FP6CK1PpdVzBs= #pubkey for example-wg-priv.txt
# AllowedIPs:
# - 10.199.10.1
# Endpoint: 10.10.10.1:5862
VID: 3001
functions:
silent:
tmp: testing
router:
name: r1


debug:
order_vlans: true
Expand Down
3 changes: 3 additions & 0 deletions apps/Arm/vm_multi/network/ip_allocation/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module ip-allocator

go 1.22.6
Binary file not shown.
123 changes: 123 additions & 0 deletions apps/Arm/vm_multi/network/ip_allocation/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package main

import (
"os"
"bufio"
"os/exec"
"fmt"
"net"
"strings"
)

var ext_intr string;

func main() {
// Figure out what our infra interface is:
file, err := os.Open("/proc/cmdline")
if err != nil {
fmt.Println("Error opening /proc/cmdline:", err)
return
}
defer file.Close()

scanner := bufio.NewScanner(file)
if scanner.Scan() {
bootArgs := scanner.Text()
// Parse the boot arguments to find "infra="
ext_intr = parseBootArgs(bootArgs, "infra")
}

if err := scanner.Err(); err != nil {
fmt.Println("Error reading /proc/cmdline:", err)
return
}

// Listen on a specific port
listener, err := net.Listen("tcp", ":8080")
if err != nil {
fmt.Println("Error listening:", err)
return
}
defer listener.Close()

fmt.Println("Server is listening on port 8080...")

// Accept an incoming connection
conn, err := listener.Accept()
if err != nil {
fmt.Println("Error accepting connection:", err)
}

fmt.Println("Accepted connection")

// Handle the connection in a new goroutine
handleConnection(conn)
}

func handleConnection(conn net.Conn) {
defer conn.Close()

// Read data from the connection
message, _ := bufio.NewReader(conn).ReadString('\n')
message = message[:len(message) - 1] // Remove trailing space so the prog doesn't die
fmt.Println("Received:", message)

// addresses=ip1:vlan,ip2:vlan
addresses := strings.Split(strings.Split(message, "=")[1], ",")

// Set up all the vlan addresses
for _, address := range addresses {
info := strings.Split(address, ":")
ip := info[0]
vlan := info[1]

err := startInterface(ext_intr, ip, vlan)
if err != nil {
fmt.Println("Error:", err)
return
}
}

// Respond to the client
conn.Write([]byte("Message received.\n"))
}

func startInterface( ext_intr string, ip string, vlan string) error {
// Figure out what our infra interface is:
// Add an ip interface for each of these vlans
command_str := []string{"link", "add", "link", ext_intr, "name",
fmt.Sprintf("%s.%s", ext_intr, vlan), "type", "vlan", "id", vlan}
fmt.Println(command_str)
err := exec.Command("ip",command_str...).Run()
if err != nil {
fmt.Println("Error:", err)
return err
}

command_str = []string{"link", "set", "up", "dev", fmt.Sprintf("%s.%s", ext_intr, vlan)}
fmt.Println(command_str)
err = exec.Command("ip", command_str...).Run()
if err != nil {
fmt.Println("Error:", err)
return err
}

command_str = []string{"addr", "add", ip, "dev", fmt.Sprintf("%s.%s", ext_intr, vlan)}
fmt.Println(command_str)
err = exec.Command("ip", command_str...).Run()
if err != nil {
fmt.Println("Error:", err)
return err
}
// Flush the buffer to ensure all data is written to the file
return nil
}

func parseBootArgs(args, key string) string {
for _, arg := range strings.Split(args, " ") {
if strings.HasPrefix(arg, key+"=") {
return strings.TrimPrefix(arg, key+"=")
}
}
return ""
}
Binary file added apps/Arm/vm_multi/network/ip_allocation/setup
Binary file not shown.
Loading

0 comments on commit 98d4485

Please sign in to comment.