FREEBSD 使用 ng_eiface 和 ng_bridge 创建虚拟以太网接口
作者:互联网
使用通用以太网接口 netgraph 节点,可以创建可通过 ifconfig(8) 访问的新接口,使用以太网桥接 netgraph 节点类型可以在不同的以太网节点类型之间进行桥接。通过组合这两种类型,可以在同一物理接口上创建具有不同以太网 (MAC) 地址的多个接口。这是从标准 FreeBSD 发行版附带的 netgraph ether.bridge 示例脚本修改而来的示例脚本:
1 #!/bin/sh 2 # 3 # Author: Jan Melen-jan(at)melen.org 4 # 5 # This script sets up an Ethernet bridging network across multiple 6 # ng_eiface(4) using the ng_bridge(4) and ng_ether(4) netgraph 7 # node types. 8 # 9 10 BRIDGE_NAME="bnet0" 11 12 VIRTUAL_IFACES="ngeth0 ngeth1 ngeth2" 13 LOCAL_IFACE="bge0" 14 15 # Routine to verify node's existence. 16 vether_verify() { 17 ngctl info ${BRIDGE_NAME}: >/dev/null 2>&1 18 if [ $? -ne 0 ]; then 19 echo "${BRIDGE_NAME}: bridge network not found" 20 exit 1 21 fi 22 } 23 24 # Start/restart routine. 25 vether_start() { 26 27 # Load netgraph KLD's as necessary. 28 for KLD in ng_ether ng_bridge; do 29 if ! kldstat -v | grep -qw ${KLD}; then 30 echo -n "Loading ${KLD}.ko... " 31 kldload ${KLD} || exit 1 32 echo "done" 33 fi 34 done 35 36 # Reset all interfaces. 37 vether_stop 38 39 # Verify all interfaces exist. 40 LINKNUM=2 41 ETHERADDR=`ifconfig ${LOCAL_IFACE} ether | grep "ether" | cut -b 8-22` 42 for ETHER in ${VIRTUAL_IFACES}; do 43 if ! ngctl info ${ETHER}: >/dev/null 2>&1; then 44 ngctl mkpeer . eiface hook ether 45 fi 46 ifconfig ${ETHER} ether ${ETHERADDR}${LINKNUM} 47 ifconfig ${ETHER} up || exit 1 48 LINKNUM=`expr ${LINKNUM} + 1` 49 done 50 51 # Create new ng_bridge(4) node, attached to the first interface. 52 ngctl mkpeer ${LOCAL_IFACE}: bridge lower link0 || exit 1 53 ngctl name ${LOCAL_IFACE}:lower ${BRIDGE_NAME} || exit 1 54 ngctl connect ${LOCAL_IFACE}: ${BRIDGE_NAME}: upper link1 || exit 1 55 56 LINKNUM=2 57 # Attach other interfaces as well. 58 for ETHER in ${VIRTUAL_IFACES}; do 59 if [ ${LINKNUM} != 0 ]; then 60 ngctl connect ${ETHER}: ${BRIDGE_NAME}: \ 61 ether link${LINKNUM} || exit 1 62 fi 63 LINKNUM=`expr ${LINKNUM} + 1` 64 done 65 66 # Set all interfaces in promiscuous mode and don't overwrite src addr. 67 for ETHER in ${LOCAL_IFACE}; do 68 ngctl msg ${ETHER}: setpromisc 1 69 ngctl msg ${ETHER}: setautosrc 0 70 done 71 } 72 73 # Stop routine. 74 vether_stop() { 75 ngctl kill ${BRIDGE_NAME}: >/dev/null 2>&1 76 for ETHER in ${VIRTUAL_IFACES} ${LOCAL_IFACE}; do 77 ngctl kill ${ETHER}: >/dev/null 2>&1 78 done 79 } 80 81 # Main entry point. 82 case $1 in 83 start) 84 vether_start 85 ;; 86 stop) 87 vether_verify 88 vether_stop 89 ;; 90 *) 91 echo "usage: $0 [ start | stop ]" 92 exit 1 93 esac
标签:bridge,LINKNUM,ngctl,以太网,exit,ether,ng,ETHER 来源: https://www.cnblogs.com/ip99/p/15264942.html