[PATCH][RFC] rules: add persistent vf mac rules generator for SR-IOV device intel 82576

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This is the rule generator for SR-IOV device intel 82576,
because the mac address of each Virtual Function interfaces are generated
randomly each time the igb driver loaded, this is a way to make the
mac of the VFs consistent cross reboots.
What we do is when we see the VFs up first time, we record each mac
and generate a rule, writing each mac into the rule and set it via ip utility
on the next boot.

and when we generate the rule, we block and wait on the files under /sys
cause sometimes the udev event is triggerd before the VFs are up completely,
and we might see no mac of the VFs if we do not block. However, during
installation of a system like SLES, the rule generator will stall
and wait for the file, so I think some suggestions are needed, Thanks

Signed-off-by: Li Dongyang <lidongyang@xxxxxxxxxx>
---
 Makefile.am                                        |   10 ++-
 .../75-persistent-mac-vf-generator.rules           |    1 +
 extras/rule_generator/set_vf_mac                   |   13 +++
 extras/rule_generator/vf_mac.functions             |   35 +++++++++
 extras/rule_generator/write_vf_mac_rules           |   81 ++++++++++++++++++++
 5 files changed, 137 insertions(+), 3 deletions(-)
 create mode 100644 extras/rule_generator/75-persistent-mac-vf-generator.rules
 create mode 100644 extras/rule_generator/set_vf_mac
 create mode 100644 extras/rule_generator/vf_mac.functions
 create mode 100644 extras/rule_generator/write_vf_mac_rules

diff --git a/Makefile.am b/Makefile.am
index 5347569..3f868ac 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -579,14 +579,18 @@ if ENABLE_RULE_GENERATOR
 # ------------------------------------------------------------------------------
 dist_libexec_SCRIPTS += \
 	extras/rule_generator/write_cd_rules \
-	extras/rule_generator/write_net_rules
+	extras/rule_generator/write_net_rules \
+	extras/rule_generator/write_vf_mac_rules \
+	extras/rule_generator/set_vf_mac
 
 udevhomedir = $(libexecdir)
-dist_udevhome_DATA = extras/rule_generator/rule_generator.functions
+dist_udevhome_DATA = extras/rule_generator/rule_generator.functions \
+		     extras/rule_generator/vf_mac.functions
 
 dist_udevrules_DATA += \
 	extras/rule_generator/75-cd-aliases-generator.rules \
-	extras/rule_generator/75-persistent-net-generator.rules
+	extras/rule_generator/75-persistent-net-generator.rules \
+	extras/rule_generator/75-persistent-mac-vf-generator.rules
 endif
 
 if ENABLE_UDEV_ACL
diff --git a/extras/rule_generator/75-persistent-mac-vf-generator.rules b/extras/rule_generator/75-persistent-mac-vf-generator.rules
new file mode 100644
index 0000000..d18eea9
--- /dev/null
+++ b/extras/rule_generator/75-persistent-mac-vf-generator.rules
@@ -0,0 +1 @@
+ATTR{vendor}=="0x8086", ATTR{device}=="0x10ca", ENV{PCI_SLOT_NAME}="%k", ENV{MATCHADDR}="$attr{address}", RUN+="write_vf_mac_rules"
diff --git a/extras/rule_generator/set_vf_mac b/extras/rule_generator/set_vf_mac
new file mode 100644
index 0000000..e539a7c
--- /dev/null
+++ b/extras/rule_generator/set_vf_mac
@@ -0,0 +1,13 @@
+#!/bin/sh
+
+. /lib/udev/vf_mac.functions
+
+PCI_SLOT_NAME=$1
+VF_MAC=$2
+
+get_pf_name $PCI_SLOT_NAME
+get_vf_id $PCI_SLOT_NAME
+
+/sbin/ip link set dev $PF_INF_NAME vf $VF_ID mac $VF_MAC
+
+exit 0
diff --git a/extras/rule_generator/vf_mac.functions b/extras/rule_generator/vf_mac.functions
new file mode 100644
index 0000000..19896d7
--- /dev/null
+++ b/extras/rule_generator/vf_mac.functions
@@ -0,0 +1,35 @@
+# functions used by set_vf_mac and write_vf_mac_rules
+#
+# This program is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by the
+# Free Software Foundation version 2 of the License.
+
+get_pf_name() {
+	local pci_slot_id="$1"
+        local name
+	while [ 1 ]; do
+		if [ -d /sys/bus/pci/devices/${pci_slot_id}/physfn/net/ ]; then
+			break
+		fi
+	done
+        for name in /sys/bus/pci/devices/${pci_slot_id}/physfn/net/* ;do
+                PF_INF_NAME=${name##*/}
+        done
+}
+
+get_vf_id() {
+	local pci_slot_id="$1"
+        local name
+	while [ 1 ]; do
+                if [ -d /sys/bus/pci/devices/${pci_slot_id}/physfn/ ]; then
+                        break
+                fi
+        done
+        for name in /sys/bus/pci/devices/${pci_slot_id}/physfn/virtfn* ;do
+                readlink "$name" | grep -q "$pci_slot_id"
+                if [ $? -eq 0 ] ;then
+                        VF_ID=${name##${name%%?}}
+                        return 0
+                fi
+        done
+}
diff --git a/extras/rule_generator/write_vf_mac_rules b/extras/rule_generator/write_vf_mac_rules
new file mode 100644
index 0000000..6f4a9ca
--- /dev/null
+++ b/extras/rule_generator/write_vf_mac_rules
@@ -0,0 +1,81 @@
+#!/bin/sh
+
+# This script is run to create persistent network device naming rules
+# based on properties of the device.
+# If the interface needs to be renamed, INTERFACE_NEW=<name> will be printed
+# on stdout to allow udev to IMPORT it.
+
+# variables used to communicate:
+#   PCI_SLOT_NAME         The pci slot name of one virtual function inited
+
+# Copyright (C) 2011 Li Dongyang <lidongyang@xxxxxxxxxx>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+
+RULES_FILE='/etc/udev/rules.d/70-persistent-mac-vf.rules'
+
+. /lib/udev/rule_generator.functions
+. /lib/udev/vf_mac.functions
+
+if [ -z "$PCI_SLOT_NAME" ]; then
+        echo "missing \$PCI_SLOT_NAME" >&2
+        exit 1
+fi
+
+if [ -e $RULES_FILE ]; then
+        grep -q $PCI_SLOT_NAME $RULES_FILE
+        if [ $? -eq 0 ]; then
+                exit 0
+        fi
+fi
+
+write_rule() {
+        local pci_slot_name="$1"
+        local vf_mac="$2"
+        local comment="$3"
+
+        {
+        if [ "$PRINT_HEADER" ]; then
+                PRINT_HEADER=
+                echo "# This file was automatically generated by the $0"
+                echo "# program, run by the persistent-mac-vf-generator.rules rules file."
+                echo "#"
+                echo "# You can modify it, as long as you keep each rule on a single"
+                echo "# line, and change only the value of the MAC you want."
+        fi
+
+        echo ""
+        [ "$comment" ] && echo "# $comment"
+        echo "SUBSYSTEM==\"pci\", ACTION==\"change\", KERNEL==\"$pci_slot_name\", RUN+=\"set_vf_mac $pci_slot_name $vf_mac\""
+        } >> $RULES_FILE
+}
+
+get_pf_name $PCI_SLOT_NAME
+get_vf_id $PCI_SLOT_NAME
+
+VF_MAC=$(ip link show $PF_INF_NAME | grep "vf $VF_ID")
+VF_MAC=${VF_MAC##* }
+
+# Prevent concurrent processes from modifying the file at the same time.
+lock_rules_file
+
+# Check if the rules file is writeable.
+choose_rules_file
+
+write_rule "$PCI_SLOT_NAME" "$VF_MAC"
+
+unlock_rules_file
+
+exit 0
-- 
1.7.6

--
To unsubscribe from this list: send the line "unsubscribe linux-hotplug" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [Linux Kernel]     [Linux DVB]     [Asterisk Internet PBX]     [DCCP]     [Netdev]     [X.org]     [Util Linux NG]     [Fedora Women]     [ALSA Devel]     [Linux USB]

  Powered by Linux