meta-facebook: yosemite4:Reconfig network interface for NIC hot service

While NIC hot remove/plug or replace another NIC,
it must down/up to NCSI re-initialization
and reconfigure net interface to recovery network link.

Tested:
build and test on yosemite4 system

NIC1 (eth0) connected CX7 NIC

root@bmc:~# ifconfig eth0
eth0      Link encap:Ethernet  HWaddr E8:EB:D3:46:D8:71
          inet addr:10.10.14.236  Bcast:10.10.255.255  Mask:255.255.0.0
          inet6 addr: 2018:5::4ca9/128 Scope:Global
          inet6 addr: fe80::eaeb:d3ff:fe46:d871/64 Scope:Link
          inet6 addr: 2019:7::eaeb:d3ff:fe46:d871/64 Scope:Global
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:23692 errors:0 dropped:11 overruns:0 frame:0
          TX packets:263 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:2510627 (2.3 MiB)  TX bytes:21546 (21.0 KiB)
          Interrupt:42

Test 1: remove and plug same NIC

bmc still pingable

ping 10.10.14.236

Pinging 10.10.14.236 with 32 bytes of data:
Reply from 10.10.14.236: bytes=32 time=1ms TTL=64
Reply from 10.10.14.236: bytes=32 time=1ms TTL=64
Reply from 10.10.14.236: bytes=32 time=1ms TTL=64
Reply from 10.10.14.236: bytes=32 time<1ms TTL=64

Test2: Remove CX7 NIC and plug another BRCM NIC

eth0 will reconfigured by correct MAC address and its pingable

ifconfig eth0
eth0      Link encap:Ethernet  HWaddr D4:04:E6:36:58:19
          inet addr:10.10.12.226  Bcast:10.10.255.255  Mask:255.255.0.0
          inet6 addr: fe80::d604:e6ff:fe36:5819/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:25736 errors:0 dropped:12 overruns:0 frame:0
          TX packets:404 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:2749245 (2.6 MiB)  TX bytes:32907 (32.1 KiB)
          Interrupt:42

ping 10.10.12.226

Pinging 10.10.12.226 with 32 bytes of data:
Reply from 10.10.12.226: bytes=32 time=1ms TTL=64
Reply from 10.10.12.226: bytes=32 time=1ms TTL=64
Reply from 10.10.12.226: bytes=32 time=7ms TTL=64
Reply from 10.10.12.226: bytes=32 time=1ms TTL=64

Change-Id: I8130eb4ba317ebd66378d646ca95e03dd7481fd7
Signed-off-by: Allen.Wang <Allen_Wang@quantatw.com>
diff --git a/meta-facebook/meta-yosemite4/recipes-phosphor/gpio/phosphor-gpio-monitor/reconfig-net-interface b/meta-facebook/meta-yosemite4/recipes-phosphor/gpio/phosphor-gpio-monitor/reconfig-net-interface
new file mode 100644
index 0000000..b358230
--- /dev/null
+++ b/meta-facebook/meta-yosemite4/recipes-phosphor/gpio/phosphor-gpio-monitor/reconfig-net-interface
@@ -0,0 +1,23 @@
+#!/bin/bash
+
+to_local_admin_mac() {
+    # Set sencond bit of first byte
+    modified_byte=$((16#${1:0:2} | 0x02))
+    modified_mac="$(printf "%02X" "$modified_byte")${1:2}"
+    echo "$modified_mac"
+}
+
+INTF=$1
+
+networkctl down "$INTF"
+
+# Set interface to local Admin MAC
+local_mac=$(to_local_admin_mac "$(cat /sys/class/net/"$INTF"/address)")
+ip link set dev "$INTF" address "$local_mac"
+
+networkctl up "$INTF"
+
+# wait getting address from NC-SI
+systemctl restart network-wait-ipv6-ll@"$INTF".service
+
+networkctl reconfigure "$INTF"
diff --git a/meta-facebook/meta-yosemite4/recipes-phosphor/gpio/phosphor-gpio-monitor/reconfig-net-interface@.service b/meta-facebook/meta-yosemite4/recipes-phosphor/gpio/phosphor-gpio-monitor/reconfig-net-interface@.service
new file mode 100644
index 0000000..2ddf62b
--- /dev/null
+++ b/meta-facebook/meta-yosemite4/recipes-phosphor/gpio/phosphor-gpio-monitor/reconfig-net-interface@.service
@@ -0,0 +1,7 @@
+[Unit]
+Description=Reconfigure network interface %i
+
+[Service]
+Type=oneshot
+ExecStart=/usr/libexec/phosphor-gpio-monitor/reconfig-net-interface %i
+SyslogIdentifier=reconfig-net-interface-%i
\ No newline at end of file
diff --git a/meta-facebook/meta-yosemite4/recipes-phosphor/gpio/phosphor-gpio-monitor/yosemite4-phosphor-multi-gpio-monitor.json b/meta-facebook/meta-yosemite4/recipes-phosphor/gpio/phosphor-gpio-monitor/yosemite4-phosphor-multi-gpio-monitor.json
index bfa7ea7..ab898a9 100644
--- a/meta-facebook/meta-yosemite4/recipes-phosphor/gpio/phosphor-gpio-monitor/yosemite4-phosphor-multi-gpio-monitor.json
+++ b/meta-facebook/meta-yosemite4/recipes-phosphor/gpio/phosphor-gpio-monitor/yosemite4-phosphor-multi-gpio-monitor.json
@@ -122,7 +122,8 @@
             "FALLING": [
                 "obmc-chassis-powercycle@3.target",
                 "obmc-chassis-powercycle@4.target",
-                "setup-nic-endpoint-slot@1.service"
+                "setup-nic-endpoint-slot@1.service",
+                "reconfig-net-interface@eth0.service"
             ]
         },
         "Continue": true
@@ -137,7 +138,8 @@
             "FALLING": [
                 "obmc-chassis-powercycle@5.target",
                 "obmc-chassis-powercycle@6.target",
-                "setup-nic-endpoint-slot@2.service"
+                "setup-nic-endpoint-slot@2.service",
+                "reconfig-net-interface@eth1.service"
             ]
         },
         "Continue": true
diff --git a/meta-facebook/meta-yosemite4/recipes-phosphor/gpio/phosphor-gpio-monitor_%.bbappend b/meta-facebook/meta-yosemite4/recipes-phosphor/gpio/phosphor-gpio-monitor_%.bbappend
index 14057f6..112b342 100644
--- a/meta-facebook/meta-yosemite4/recipes-phosphor/gpio/phosphor-gpio-monitor_%.bbappend
+++ b/meta-facebook/meta-yosemite4/recipes-phosphor/gpio/phosphor-gpio-monitor_%.bbappend
@@ -9,6 +9,8 @@
             file://set-button-sled.service \
             file://probe-slot-device@.service \
             file://probe-slot-device \
+            file://reconfig-net-interface@.service \
+            file://reconfig-net-interface \
             file://rescan-fru-device@.service \
             file://rescan-fru-device \
             file://slot-hot-plug@.service \
@@ -28,6 +30,7 @@
     setup-nic-endpoint-slot@.service \
     remove-nic-endpoint-slot@.service \
     rescan-wf-bic@.service \
+    reconfig-net-interface@.service \
     "
 
 SYSTEMD_AUTO_ENABLE = "enable"
@@ -41,10 +44,12 @@
     install -m 0644 ${WORKDIR}/rescan-fru-device@.service ${D}${systemd_system_unitdir}/rescan-fru-device@.service
     install -m 0644 ${WORKDIR}/slot-hot-plug@.service ${D}${systemd_system_unitdir}/slot-hot-plug@.service
     install -m 0644 ${WORKDIR}/setup-nic-endpoint-slot@.service ${D}${systemd_system_unitdir}/
+    install -m 0644 ${WORKDIR}/reconfig-net-interface@.service  ${D}${systemd_system_unitdir}/reconfig-net-interface@.service
     install -m 0644 ${WORKDIR}/remove-nic-endpoint-slot@.service ${D}${systemd_system_unitdir}/
     install -m 0644 ${WORKDIR}/rescan-wf-bic@.service ${D}${systemd_system_unitdir}/
     install -d ${D}${libexecdir}/${PN}
     install -m 0755 ${WORKDIR}/probe-slot-device ${D}${libexecdir}/${PN}/
+    install -m 0755 ${WORKDIR}/reconfig-net-interface ${D}${libexecdir}/${PN}/
     install -m 0755 ${WORKDIR}/rescan-fru-device ${D}${libexecdir}/${PN}/
     install -m 0755 ${WORKDIR}/rescan-wf-bic ${D}${libexecdir}/${PN}/
     install -d ${D}/${bindir}