meta-google: gbmc-bridge: add udev and bridging mapping

This CL adds support for bridging ethernet devices to gbmcbr and
interface renaming based on the dev address.

Change-Id: Ibc5fc8e0426e117191574553b36ea59a6735b91c
Signed-off-by: Yuxiao Zhang <yuxiaozhang@google.com>
diff --git a/meta-google/recipes-google/networking/gbmc-bridge.bb b/meta-google/recipes-google/networking/gbmc-bridge.bb
index 8b9cdb5..d5ee98f 100644
--- a/meta-google/recipes-google/networking/gbmc-bridge.bb
+++ b/meta-google/recipes-google/networking/gbmc-bridge.bb
@@ -73,6 +73,23 @@
   idx = range(0, len(b)-1, 2)
   return ':'.join([format((b[i] << 8) + b[i+1], '04x') for i in idx])
 
+GBMC_BRIDGE_INTFS ?= ""
+
+ethernet_bridge_install() {
+  # install udev rules if any
+  if [ -z "${GBMC_BRIDGE_INTFS}"]; then
+    return
+  fi
+  cat /dev/null > ${WORKDIR}/-ether-bridge.network
+  echo "[Match]" >> ${WORKDIR}/-ether-bridge.network
+  echo "Name=${GBMC_BRIDGE_INTFS}" >>  ${WORKDIR}/-ether-bridge.network
+  echo "[Network]" >> ${WORKDIR}/-ether-bridge.network
+  echo "Bridge=gbmcbr" >> ${WORKDIR}/-ether-bridge.network
+
+  install -d ${D}/${sysconfdir}/systemd/network
+  install -m 0644 ${WORKDIR}/-ether-bridge.network ${D}/${sysconfdir}/systemd/network/
+}
+
 do_install() {
   netdir=${D}${systemd_unitdir}/network
   install -d -m0755 $netdir
@@ -85,6 +102,8 @@
     sed -i '/@ADDR@/d' ${WORKDIR}/-bmc-gbmcbr.network.in
   fi
 
+  ethernet_bridge_install
+
   install -m0644 ${WORKDIR}/-bmc-gbmcbr.netdev $netdir/
   install -m0644 ${WORKDIR}/-bmc-gbmcbr.network.in $netdir/-bmc-gbmcbr.network
   install -m0644 ${WORKDIR}/-bmc-gbmcbrdummy.netdev $netdir/
diff --git a/meta-google/recipes-google/networking/gbmc-nic-rename.bb b/meta-google/recipes-google/networking/gbmc-nic-rename.bb
new file mode 100644
index 0000000..cff5086
--- /dev/null
+++ b/meta-google/recipes-google/networking/gbmc-nic-rename.bb
@@ -0,0 +1,34 @@
+SUMMARY = "Rename the network device name"
+PR = "r1"
+LICENSE = "Apache-2.0"
+LIC_FILES_CHKSUM = "file://${COREBASE}/meta/files/common-licenses/Apache-2.0;md5=89aea4e17d99a7cacdbeed46a0096b10"
+
+GBMC_ETHER_MAP ?= ""
+
+inherit systemd
+
+S = "${WORKDIR}"
+
+FILES:${PN} += "${systemd_unitdir}"
+
+do_install() {
+  netdir=${D}${systemd_unitdir}/network
+  install -d -m0755 $netdir
+
+  # install dev renaming files if any
+  if [ -z "${GBMC_ETHER_MAP}"]; then
+    return
+  fi
+  devmap="${GBMC_ETHER_MAP}"
+  for str in $devmap
+  do
+    devaddr="$(echo "${str}" | cut -d'|' -f1)"
+    devname="$(echo "${str}" | cut -d'|' -f2)"
+    echo "[Match]" > ${WORKDIR}/30-netdev-${devname}.link
+    echo "Path=*-${devaddr}" >> ${WORKDIR}/30-netdev-${devname}.link
+    echo "[Link]"  >> ${WORKDIR}/30-netdev-${devname}.link
+    echo "Name=${devname}" >> ${WORKDIR}/30-netdev-${devname}.link
+    install -m0644 ${WORKDIR}/30-netdev-${devname}.link ${netdir}
+  done
+}
+
diff --git a/meta-google/recipes-phosphor/images/obmc-phosphor-image.bbappend b/meta-google/recipes-phosphor/images/obmc-phosphor-image.bbappend
index 8627011..207e7c5 100644
--- a/meta-google/recipes-phosphor/images/obmc-phosphor-image.bbappend
+++ b/meta-google/recipes-phosphor/images/obmc-phosphor-image.bbappend
@@ -18,6 +18,8 @@
   '${@"" if not d.getVar("GBMC_NCSI_IF_NAME") else " gbmc-ncsi-config"}'
 OBMC_IMAGE_EXTRA_INSTALL:append:gbmc = \
   '${@"" if not d.getVar("GBMC_MAC_EEPROM_OF_NAME") else " gbmc-mac-config"}'
+OBMC_IMAGE_EXTRA_INSTALL:append:gbmc = \
+  '${@"" if not d.getVar("GBMC_ETHER_MAP") else " gbmc-nic-rename"}'
 
 # Include these useful utilities for all gbmc platforms
 OBMC_IMAGE_EXTRA_INSTALL:append:gbmc = " ipmitool"