| 1 | #!/bin/bash |
| 2 | # |
| 3 | # Copyright (C) 2025 Linutronix GmbH |
| 4 | # Author Kurt Kanzenbach <[email protected]> |
| 5 | # |
| 6 | # SPDX-License-Identifier: BSD-2-Clause |
| 7 | # |
| 8 | # Create virtual network interfaces move them into containers and bridge them together. |
| 9 | # |
| 10 | |
| 11 | set -e |
| 12 | |
| 13 | # |
| 14 | # Network for containers |
| 15 | # |
| 16 | ip link add veth0 type veth peer name veth0tb |
| 17 | ip link add veth1 type veth peer name veth1tb |
| 18 | |
| 19 | # |
| 20 | # Setup MAC addresses. |
| 21 | # |
| 22 | HOSTNAME=$(hostname) |
| 23 | if [ "${HOSTNAME}" = "bpc1" ]; then |
| 24 | ifconfig veth0tb hw ether 5e:a7:0c:3f:e4:a9 |
| 25 | ifconfig veth1tb hw ether d2:4c:c5:e2:f7:b7 |
| 26 | else |
| 27 | ifconfig veth0tb hw ether 5e:a7:0c:3f:e4:aa |
| 28 | ifconfig veth1tb hw ether d2:4c:c5:e2:f7:b8 |
| 29 | fi |
| 30 | |
| 31 | # |
| 32 | # Setup veth up |
| 33 | # |
| 34 | ip link set veth0 up |
| 35 | ip link set veth1 up |
| 36 | |
| 37 | # |
| 38 | # Disable VLAN Rx offload. |
| 39 | # |
| 40 | ethtool -K veth0 rx-vlan-offload off |
| 41 | ethtool -K veth1 rx-vlan-offload off |
| 42 | ethtool -K veth0tb rx-vlan-offload off |
| 43 | ethtool -K veth1tb rx-vlan-offload off |
| 44 | ethtool -K veth0 tx-vlan-offload off |
| 45 | ethtool -K veth1 tx-vlan-offload off |
| 46 | ethtool -K veth0tb tx-vlan-offload off |
| 47 | ethtool -K veth1tb tx-vlan-offload off |
| 48 | |
| 49 | # |
| 50 | # Move vethXtb to container network namespaces |
| 51 | # |
| 52 | NETNS1=$(lsns -t net | tail -n1 | awk '{ print $4 }') |
| 53 | NETNS2=$(lsns -t net | tail -n2 | head -n 1 | awk '{ print $4 }') |
| 54 | ip link set veth0tb netns ${NETNS1} |
| 55 | ip link set veth1tb netns ${NETNS2} |
| 56 | |
| 57 | # |
| 58 | # Bridge together: virtual and physical NICs |
| 59 | # |
| 60 | ip link add br0 type bridge |
| 61 | ip link set veth0 master br0 |
| 62 | ip link set veth1 master br0 |
| 63 | ip link set enp3s0 master br0 |
| 64 | ip link set br0 up |
| 65 | |
| 66 | # |
| 67 | # Ignore iptable rules setup by docker |
| 68 | # |
| 69 | sysctl net.bridge.bridge-nf-call-iptables=0 |
| 70 | |
| 71 | # |
| 72 | # Configure VLANs on br0 |
| 73 | # |
| 74 | ifconfig enp3s0.100 down |
| 75 | ip link delete enp3s0.100 |
| 76 | ip link set dev br0 type bridge vlan_filtering 1 |
| 77 | bridge vlan add dev enp3s0 vid 100 |
| 78 | bridge vlan add dev veth0 vid 100 |
| 79 | bridge vlan add dev veth1 vid 100 |
| 80 | bridge vlan add dev enp3s0 vid 200 |
| 81 | bridge vlan add dev veth0 vid 200 |
| 82 | bridge vlan add dev veth1 vid 200 |
| 83 | |
| 84 | # |
| 85 | # Setup MAC addresses. |
| 86 | # |
| 87 | if [ "${HOSTNAME}" = "bpc1" ]; then |
| 88 | ifconfig br0 hw ether d6:96:12:ef:d6:77 |
| 89 | else |
| 90 | ifconfig br0 hw ether d6:96:12:ef:d6:78 |
| 91 | fi |
| 92 | |
| 93 | # |
| 94 | # Disable VLAN Rx offload. |
| 95 | # |
| 96 | ethtool -K br0 rx-vlan-offload off |
| 97 | ethtool -K br0 tx-vlan-offload off |
| 98 | |
| 99 | # |
| 100 | # Setup XDP on physical NIC. |
| 101 | # |
| 102 | cp ../../build/xdp_kern_*.o . |
| 103 | xdp-loader load --prog-name xdp_sock_prog enp3s0 xdp_kern_profinet_veth_dispatch.o |
| 104 | IFINDEX_VETH0=$(cat /sys/class/net/veth0/ifindex) |
| 105 | IFINDEX_VETH1=$(cat /sys/class/net/veth1/ifindex) |
| 106 | bpftool map update name veth_map key 0 0 0 0 value ${IFINDEX_VETH0} 0 0 0 |
| 107 | bpftool map update name veth_map key 1 0 0 0 value ${IFINDEX_VETH1} 0 0 0 |