Last active
May 31, 2022 13:57
-
-
Save BruceChen7/07f41199e27fc489bacbb303c848e76d to your computer and use it in GitHub Desktop.
#networking#lab#k8s#namespace#ip#link
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 创建bridge | |
create_bridge() { | |
local nsname="$1" | |
local ifname="$2" | |
echo "Creating bridge ${nsname}/${ifname}" | |
ip netns add ${nsname} | |
ip netns exec ${nsname} ip link set lo up | |
ip netns exec ${nsname} ip link add ${ifname} type bridge | |
ip netns exec ${nsname} ip link set ${ifname} up | |
} | |
// 在不同的net namespace 来创建汇end 节点 | |
// https://iximiuz.com/en/posts/networking-lab-ethernet-broadcast-domains/ | |
create_end_host() { | |
local host_nsname="$1" | |
local peer1_ifname="$2a" | |
local peer2_ifname="$2b" | |
local bridge_nsname="$3" | |
local bridge_ifname="$4" | |
echo "Creating end host ${host_nsname} connected to ${bridge_nsname}/${bridge_ifname} bridge" | |
# Create end host network namespace. | |
ip netns add ${host_nsname} | |
ip netns exec ${host_nsname} ip link set lo up | |
# Create a veth pair connecting end host and bridge namespaces. | |
ip link add ${peer1_ifname} netns ${host_nsname} type veth peer \ | |
${peer2_ifname} netns ${bridge_nsname} | |
ip netns exec ${host_nsname} ip link set ${peer1_ifname} up | |
ip netns exec ${bridge_nsname} ip link set ${peer2_ifname} up | |
# Attach peer2 interface to the bridge. | |
ip netns exec ${bridge_nsname} ip link set ${peer2_ifname} master ${bridge_ifname} | |
} | |
// 用来给制定的mac地址来发送L2包 | |
#!/usr/bin/env python3 | |
# Usage: ethsend.py eth0 ff:ff:ff:ff:ff:ff 'Hello everybody!' | |
# ethsend.py eth0 06:e5:f0:20:af:7a 'Hello 06:e5:f0:20:af:7a!' | |
# | |
# Note: CAP_NET_RAW capability is required to use SOCK_RAW | |
import fcntl | |
import socket | |
import struct | |
import sys | |
def send_frame(ifname, dstmac, eth_type, payload): | |
# Open raw socket and bind it to network interface. | |
s = socket.socket(socket.AF_PACKET, socket.SOCK_RAW) | |
s.bind((ifname, 0)) | |
# Get source interface's MAC address. | |
info = fcntl.ioctl(s.fileno(), | |
0x8927, | |
struct.pack('256s', bytes(ifname, 'utf-8')[:15])) | |
srcmac = ':'.join('%02x' % b for b in info[18:24]) | |
# Build Ethernet frame | |
payload_bytes = payload.encode('utf-8') | |
assert len(payload_bytes) <= 1500 # Ethernet MTU | |
frame = human_mac_to_bytes(dstmac) + \ | |
human_mac_to_bytes(srcmac) + \ | |
eth_type + \ | |
payload_bytes | |
# Send Ethernet frame | |
return s.send(frame) | |
def human_mac_to_bytes(addr): | |
return bytes.fromhex(addr.replace(':', '')) | |
def main(): | |
ifname = sys.argv[1] | |
dstmac = sys.argv[2] | |
payload = sys.argv[3] | |
ethtype = b'\x7A\x05' # arbitrary, non-reserved | |
send_frame(ifname, dstmac, ethtype, payload) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
创建bridge设备