[U-Boot] [PATCH 5/5] net: sandbox-raw: Allow interface to be specified by index

Joe Hershberger joe.hershberger at ni.com
Tue Jun 26 21:19:26 UTC 2018


With systemd stable interface names, eth0 will almost never exist.
Instead of using that name in the sandbox.dts, use index 2 (the first
interface after "lo"). Enumerate the interfaces on the system to choose
a valid interace name.

Signed-off-by: Joe Hershberger <joe.hershberger at ni.com>
---

 arch/sandbox/cpu/eth-raw-os.c         | 28 ++++++++++++++++++++++++++++
 arch/sandbox/dts/sandbox.dts          |  2 +-
 arch/sandbox/include/asm/eth-raw-os.h |  4 +++-
 drivers/net/sandbox-raw.c             |  8 +++++++-
 4 files changed, 39 insertions(+), 3 deletions(-)

diff --git a/arch/sandbox/cpu/eth-raw-os.c b/arch/sandbox/cpu/eth-raw-os.c
index 61f23ed210..11b5271f31 100644
--- a/arch/sandbox/cpu/eth-raw-os.c
+++ b/arch/sandbox/cpu/eth-raw-os.c
@@ -150,6 +150,34 @@ int sandbox_eth_raw_os_start(const char *ifname, unsigned char *ethmac,
 		return _raw_packet_start(ifname, ethmac, priv);
 }
 
+int sandbox_eth_raw_os_start_idx(unsigned int ifindex, unsigned char *ethmac,
+				 struct eth_sandbox_raw_priv *priv)
+{
+	int ret;
+	struct if_nameindex *ni, *i;
+	const char *ifname = NULL;
+
+	ni = if_nameindex();
+	if (!ni)
+		return -EINVAL;
+
+	for (i = ni; !(i->if_index == 0 && !i->if_name); i++) {
+		if (ifindex == i->if_index) {
+			ifname = i->if_name;
+			printf("(%s)\n", ifname);
+			break;
+		}
+	}
+	/* Don't bother supporting 'lo' by index. Just use the name in DTS */
+	if (ifname)
+		ret = _raw_packet_start(ifname, ethmac, priv);
+	else
+		ret = -EINVAL;
+
+	if_freenameindex(ni);
+	return ret;
+}
+
 int sandbox_eth_raw_os_send(void *packet, int length,
 			    struct eth_sandbox_raw_priv *priv)
 {
diff --git a/arch/sandbox/dts/sandbox.dts b/arch/sandbox/dts/sandbox.dts
index 0ea2452742..e7f6c194d0 100644
--- a/arch/sandbox/dts/sandbox.dts
+++ b/arch/sandbox/dts/sandbox.dts
@@ -56,7 +56,7 @@
 	eth at 80000000 {
 		compatible = "sandbox,eth-raw";
 		reg = <0x80000000 0x1000>;
-		host-raw-interface = "eth0";
+		host-raw-interface-idx = <2>;
 	};
 
 	eth at 90000000 {
diff --git a/arch/sandbox/include/asm/eth-raw-os.h b/arch/sandbox/include/asm/eth-raw-os.h
index f986d3142d..70b29b4508 100644
--- a/arch/sandbox/include/asm/eth-raw-os.h
+++ b/arch/sandbox/include/asm/eth-raw-os.h
@@ -29,7 +29,9 @@ struct eth_sandbox_raw_priv {
 };
 
 int sandbox_eth_raw_os_start(const char *ifname, unsigned char *ethmac,
-			    struct eth_sandbox_raw_priv *priv);
+			     struct eth_sandbox_raw_priv *priv);
+int sandbox_eth_raw_os_start_idx(unsigned int ifindex, unsigned char *ethmac,
+				 struct eth_sandbox_raw_priv *priv);
 int sandbox_eth_raw_os_send(void *packet, int length,
 			    struct eth_sandbox_raw_priv *priv);
 int sandbox_eth_raw_os_recv(void *packet, int *length,
diff --git a/drivers/net/sandbox-raw.c b/drivers/net/sandbox-raw.c
index f835a6a7f3..3bde8d84ce 100644
--- a/drivers/net/sandbox-raw.c
+++ b/drivers/net/sandbox-raw.c
@@ -22,6 +22,7 @@ static int sb_eth_raw_start(struct udevice *dev)
 	struct eth_sandbox_raw_priv *priv = dev_get_priv(dev);
 	struct eth_pdata *pdata = dev_get_platdata(dev);
 	const char *interface;
+	u32 index;
 
 	debug("eth_sandbox_raw: Start\n");
 
@@ -37,7 +38,12 @@ static int sb_eth_raw_start(struct udevice *dev)
 						priv);
 	}
 
-	return -EINVAL;
+	if (dev_read_u32(dev, "host-raw-interface-idx", &index) < 0)
+		return -EINVAL;
+
+	printf("eth_sandbox_raw: Using interface index %d from DT ", index);
+
+	return sandbox_eth_raw_os_start_idx(index, pdata->enetaddr, priv);
 }
 
 static int sb_eth_raw_send(struct udevice *dev, void *packet, int length)
-- 
2.11.0



More information about the U-Boot mailing list