autobridge patch
Andrew Thompson
thompsa at freebsd.org
Mon Jan 16 01:16:34 PST 2006
Hi,
I have a patch here that adds the ability to automatically add an interface to a
bridge when its attached. This is aimed towards apps like qemu or vmware that
open a tap interface and need it bridged with the network adapter, the user can
set up a glob for interfaces to be automatically added (eg tap*). It may
also be useful for Xen dom0 support.
This patch includes a big change to how interfaces are configured in
userland. Before only physical Ethernet cards were handled by devd,
now _ALL_ interfaces are (vlans, pflog, pfsync, tap, tun, etc..). This
has the added bonus that the pseudo interfaces can be configured after
boottime in rc.conf, ifconfig_xxx="".
Please test this patch, even if you dont use a bridge. Im not in a hurry
to commit it.
cheers,
Andrew
-------------- next part --------------
Index: etc/devd.conf
===================================================================
RCS file: /home/ncvs/src/etc/devd.conf,v
retrieving revision 1.30
diff -u -p -r1.30 devd.conf
--- etc/devd.conf 11 Dec 2005 00:18:28 -0000 1.30
+++ etc/devd.conf 12 Jan 2006 21:15:22 -0000
@@ -28,17 +28,19 @@ options {
# override these general rules.
#
-# For ethernet like devices start configuring the interface. Due to
-# a historical accident, this script is called pccard_ether.
+# Configure the interface on attach. Due to a historical accident, this
+# script is called pccard_ether.
#
-attach 0 {
- media-type "ethernet";
- action "/etc/pccard_ether $device-name start";
+notify 0 {
+ match "system" "IFNET";
+ match "type" "ATTACH";
+ action "/etc/pccard_ether $subsystem start";
};
-detach 0 {
- media-type "ethernet";
- action "/etc/pccard_ether $device-name stop";
+notify 0 {
+ match "system" "IFNET";
+ match "type" "DETACH";
+ action "/etc/pccard_ether $subsystem stop";
};
#
Index: etc/defaults/rc.conf
===================================================================
RCS file: /home/ncvs/src/etc/defaults/rc.conf,v
retrieving revision 1.270
diff -u -p -r1.270 rc.conf
--- etc/defaults/rc.conf 8 Jan 2006 10:15:30 -0000 1.270
+++ etc/defaults/rc.conf 12 Jan 2006 21:15:09 -0000
@@ -163,6 +163,9 @@ ifconfig_lo0="inet 127.0.0.1" # default
#ifconfig_fxp0_name="net0" # Change interface name from fxp0 to net0.
#ipv4_addrs_fxp0="192.168.0.1/24 192.168.1.1-5/28" # example IPv4 address entry.
#
+#autobridge_interfaces="bridge0" # List of bridges to check
+#autobridge_bridge0="tap* vlan0" # Interface glob to automatically add to the bridge
+#
# If you have any sppp(4) interfaces above, you might also want to set
# the following parameters. Refer to spppcontrol(8) for their meaning.
sppp_interfaces="" # List of sppp interfaces.
Index: etc/rc.d/Makefile
===================================================================
RCS file: /home/ncvs/src/etc/rc.d/Makefile,v
retrieving revision 1.62
diff -u -p -r1.62 Makefile
--- etc/rc.d/Makefile 15 Dec 2005 01:04:48 -0000 1.62
+++ etc/rc.d/Makefile 16 Jan 2006 08:59:15 -0000
@@ -4,7 +4,7 @@
FILES= DAEMON LOGIN NETWORKING SERVERS \
abi accounting addswap adjkerntz amd \
apm apmd archdep atm1 atm2 atm3 \
- bgfsck bluetooth bootparams bsnmpd \
+ bgfsck bluetooth bootparams bridge bsnmpd \
ccd cleanvar cleartmp cron \
devd devfs dhclient \
dmesg dumpon \
Index: etc/rc.d/bridge
===================================================================
RCS file: etc/rc.d/bridge
diff -N etc/rc.d/bridge
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ etc/rc.d/bridge 16 Jan 2006 08:53:34 -0000
@@ -0,0 +1,71 @@
+#!/bin/sh
+#
+# $FreeBSD$
+#
+
+# PROVIDE: bridge
+# REQUIRE: netif
+# KEYWORD: nojail
+
+. /etc/rc.subr
+. /etc/network.subr
+
+name="bridge"
+start_cmd="bridge_start"
+stop_cmd="bridge_stop"
+_cmd=""
+
+glob_int () {
+ case "$1" in
+ $2 ) true ;;
+ * ) false ;;
+ esac
+}
+
+bridge_test () {
+ bridge=$1
+ iface=$2
+
+ eval interfaces=\$autobridge_${bridge}
+ if [ -n "${interfaces}" ]; then
+ for i in ${interfaces}; do
+ if glob_int $iface $i ; then
+ ifconfig $bridge $_cmd $iface > /dev/null 2>&1
+ return
+ fi
+ done
+ fi
+}
+
+autobridge()
+{
+ if [ -n "${autobridge_interfaces}" ]; then
+ if [ -z "$_iflist" ]; then
+ # We're operating as a general network start routine.
+ _iflist="`list_net_interfaces`"
+ fi
+
+ for br in ${autobridge_interfaces}; do
+ for i in $_iflist; do
+ bridge_test $br $i
+ done
+ done
+ fi
+}
+
+bridge_start()
+{
+ _cmd="addm"
+ autobridge
+}
+
+bridge_stop()
+{
+ _cmd="deletem"
+ autobridge
+}
+
+_iflist=$2
+
+load_rc_config $name
+run_rc_command "$1"
Index: etc/rc.d/netif
===================================================================
RCS file: /home/ncvs/src/etc/rc.d/netif,v
retrieving revision 1.18
diff -u -p -r1.18 netif
--- etc/rc.d/netif 14 Nov 2005 23:34:50 -0000 1.18
+++ etc/rc.d/netif 16 Jan 2006 08:48:56 -0000
@@ -71,6 +71,9 @@ network_start()
# Resync ipfilter
/etc/rc.d/ipfilter resync
fi
+ if [ -f /etc/rc.d/bridge -a -n "$_cmdifn" ] ; then
+ /etc/rc.d/bridge start $_cmdifn
+ fi
}
network_stop()
Index: share/man/man5/rc.conf.5
===================================================================
RCS file: /home/ncvs/src/share/man/man5/rc.conf.5,v
retrieving revision 1.278
diff -u -p -r1.278 rc.conf.5
--- share/man/man5/rc.conf.5 8 Jan 2006 13:20:57 -0000 1.278
+++ share/man/man5/rc.conf.5 11 Jan 2006 18:58:11 -0000
@@ -3395,6 +3395,23 @@ has been mounted.
Both the
.Xr md 4
device and the mount point will be changed.
+.It Va autobridge_interfaces
+.Pq Vt str
+Set to the list of bridge interfaces that will have newly arriving interfaces
+checked against to be automatically added.
+If not set to
+.Dq Li NO
+then for each whitespace separated
+.Ar element
+in the value, a
+.Va autobridge_ Ns Aq Ar element
+variable is assumed to exist which has a whitespace separated list of interface
+names to match, these names can use wildcards.
+For example:
+.Bd -literal
+autobridge_interfaces="bridge0"
+autobridge_bridge0="tap* dc0 dc[345]"
+.Ed
.El
.Sh FILES
.Bl -tag -width ".Pa /etc/defaults/rc.conf" -compact
@@ -3411,6 +3428,7 @@ device and the mount point will be chang
.Xr makewhatis 1 ,
.Xr vi 1 ,
.Xr vidcontrol 1 ,
+.Xr bridge 4 ,
.Xr ip 4 ,
.Xr ipf 4 ,
.Xr ipfw 4 ,
Index: sys/net/if.c
===================================================================
RCS file: /home/ncvs/src/sys/net/if.c,v
retrieving revision 1.251
diff -u -p -r1.251 if.c
--- sys/net/if.c 11 Nov 2005 16:04:48 -0000 1.251
+++ sys/net/if.c 12 Jan 2006 21:15:22 -0000
@@ -501,6 +501,7 @@ if_attach(struct ifnet *ifp)
if_attachdomain1(ifp);
EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
+ devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
/* Announce the interface. */
rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
@@ -684,6 +685,7 @@ if_detach(struct ifnet *ifp)
/* Announce that the interface is gone. */
rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
+ devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
IF_AFDATA_LOCK(ifp);
for (dp = domains; dp; dp = dp->dom_next) {
More information about the freebsd-net
mailing list