[U-Boot] [PATCH v2 11/11] sandbox: eth-raw: Add a SIMPLE_BUS to enumerate host interfaces

Heinrich Schuchardt xypron.glpk at gmx.de
Sat Jun 30 04:44:21 UTC 2018


On 06/30/2018 05:21 AM, Joe Hershberger wrote:
> Ask the OS for each of its interfaces and for each one, bind a U-Boot
> device and then probe it. This will allocate the priv data structure
> that is then populated.
> 
> Signed-off-by: Joe Hershberger <joe.hershberger at ni.com>
> ---
> 
> Changes in v2: None
> 
>  arch/sandbox/cpu/eth-raw-os.c         | 10 ++++++
>  arch/sandbox/dts/sandbox.dts          | 19 ++++------
>  arch/sandbox/dts/sandbox64.dts        | 19 ++++------
>  arch/sandbox/include/asm/eth-raw-os.h | 11 ++++++
>  drivers/net/Makefile                  |  1 +
>  drivers/net/sandbox-raw-bus.c         | 65 +++++++++++++++++++++++++++++++++++
>  6 files changed, 99 insertions(+), 26 deletions(-)
>  create mode 100644 drivers/net/sandbox-raw-bus.c
> 
> diff --git a/arch/sandbox/cpu/eth-raw-os.c b/arch/sandbox/cpu/eth-raw-os.c
> index df7acaa0bc..75bfaa4c90 100644
> --- a/arch/sandbox/cpu/eth-raw-os.c
> +++ b/arch/sandbox/cpu/eth-raw-os.c
> @@ -23,6 +23,16 @@
>  #include <linux/if_ether.h>
>  #include <linux/if_packet.h>
>  
> +struct sandbox_eth_raw_if_nameindex *sandbox_eth_raw_if_nameindex(void)
> +{
> +	return (struct sandbox_eth_raw_if_nameindex *)if_nameindex();
> +}
> +
> +void sandbox_eth_raw_if_freenameindex(struct sandbox_eth_raw_if_nameindex *ptr)
> +{
> +	if_freenameindex((struct if_nameindex *)ptr);
> +}
> +
>  int sandbox_eth_raw_os_is_local(const char *ifname)
>  {
>  	int fd = socket(AF_INET, SOCK_DGRAM, 0);
> diff --git a/arch/sandbox/dts/sandbox.dts b/arch/sandbox/dts/sandbox.dts
> index 0ea2452742..9164194023 100644
> --- a/arch/sandbox/dts/sandbox.dts
> +++ b/arch/sandbox/dts/sandbox.dts
> @@ -8,7 +8,6 @@
>  	model = "sandbox";
>  
>  	aliases {
> -		eth5 = "/eth at 90000000";
>  		i2c0 = &i2c_0;
>  		pci0 = &pci;
>  		rtc0 = &rtc_0;
> @@ -47,24 +46,18 @@
>  		};
>  	};
>  
> +	ethrawbus {
> +		reg = <0 0>;
> +		compatible = "sandbox,eth-raw-bus";
> +		skip-localhost = <0>;
> +	};
> +
>  	eth at 10002000 {
>  		compatible = "sandbox,eth";
>  		reg = <0x10002000 0x1000>;
>  		fake-host-hwaddr = [00 00 66 44 22 00];
>  	};
>  
> -	eth at 80000000 {
> -		compatible = "sandbox,eth-raw";
> -		reg = <0x80000000 0x1000>;
> -		host-raw-interface = "eth0";
> -	};
> -
> -	eth at 90000000 {
> -		compatible = "sandbox,eth-raw";
> -		reg = <0x90000000 0x1000>;
> -		host-raw-interface = "lo";
> -	};
> -
>  	gpio_a: gpios at 0 {
>  		gpio-controller;
>  		compatible = "sandbox,gpio";
> diff --git a/arch/sandbox/dts/sandbox64.dts b/arch/sandbox/dts/sandbox64.dts
> index 48e420e721..4a2fa9908e 100644
> --- a/arch/sandbox/dts/sandbox64.dts
> +++ b/arch/sandbox/dts/sandbox64.dts
> @@ -8,7 +8,6 @@
>  	model = "sandbox";
>  
>  	aliases {
> -		eth5 = "/eth at 90000000";
>  		i2c0 = &i2c_0;
>  		pci0 = &pci;
>  		rtc0 = &rtc_0;
> @@ -47,24 +46,18 @@
>  		};
>  	};
>  
> +	ethrawbus {
> +		reg = <0 0>;
> +		compatible = "sandbox,eth-raw-bus";
> +		skip-localhost = <1>;
> +	};
> +
>  	eth at 10002000 {
>  		compatible = "sandbox,eth";
>  		reg = <0x0 0x10002000 0x0 0x1000>;
>  		fake-host-hwaddr = [00 00 66 44 22 00];
>  	};
>  
> -	eth at 80000000 {
> -		compatible = "sandbox,eth-raw";
> -		reg = <0x0 0x80000000 0x0 0x1000>;
> -		host-raw-interface = "eth0";
> -	};
> -
> -	eth at 90000000 {
> -		compatible = "sandbox,eth-raw";
> -		reg = <0x0 0x90000000 0x0 0x1000>;
> -		host-raw-interface = "lo";
> -	};
> -
>  	gpio_a: gpios at 0 {
>  		gpio-controller;
>  		compatible = "sandbox,gpio";
> diff --git a/arch/sandbox/include/asm/eth-raw-os.h b/arch/sandbox/include/asm/eth-raw-os.h
> index bd352e9db4..50ec56ec16 100644
> --- a/arch/sandbox/include/asm/eth-raw-os.h
> +++ b/arch/sandbox/include/asm/eth-raw-os.h
> @@ -34,6 +34,17 @@ struct eth_sandbox_raw_priv {
>  	unsigned short local_bind_udp_port;
>  };
>  
> +/* A struct to mimic if_nameindex but that does not depend on Linux headers */
> +struct sandbox_eth_raw_if_nameindex {
> +	unsigned int if_index; /* Index of interface (1, 2, ...) */
> +	char        *if_name;  /* Null-terminated name ("eth0", etc.) */
> +};
> +
> +/* Enumerate host network interfaces */
> +struct sandbox_eth_raw_if_nameindex *sandbox_eth_raw_if_nameindex(void);
> +/* Free the data structure of enumerated network interfaces */
> +void sandbox_eth_raw_if_freenameindex(struct sandbox_eth_raw_if_nameindex *ptr);
> +
>  /*
>   * Check if the interface named "ifname" is a localhost interface or not.
>   * ifname - the interface nome on the host to check
> diff --git a/drivers/net/Makefile b/drivers/net/Makefile
> index 058dd00768..c1ed44e21f 100644
> --- a/drivers/net/Makefile
> +++ b/drivers/net/Makefile
> @@ -50,6 +50,7 @@ obj-$(CONFIG_RTL8139) += rtl8139.o
>  obj-$(CONFIG_RTL8169) += rtl8169.o
>  obj-$(CONFIG_ETH_SANDBOX) += sandbox.o
>  obj-$(CONFIG_ETH_SANDBOX_RAW) += sandbox-raw.o
> +obj-$(CONFIG_ETH_SANDBOX_RAW) += sandbox-raw-bus.o
>  obj-$(CONFIG_SH_ETHER) += sh_eth.o
>  obj-$(CONFIG_RENESAS_RAVB) += ravb.o
>  obj-$(CONFIG_SMC91111) += smc91111.o
> diff --git a/drivers/net/sandbox-raw-bus.c b/drivers/net/sandbox-raw-bus.c
> new file mode 100644
> index 0000000000..2a310891b5
> --- /dev/null
> +++ b/drivers/net/sandbox-raw-bus.c
> @@ -0,0 +1,65 @@
> +// SPDX-License-Identifier: GPL-2.0+
> +/*
> + * Copyright (c) 2018 National Instruments
> + * Copyright (c) 2018 Joe Hershberger <joe.hershberger at ni.com>
> + */
> +
> +#include <common.h>
> +#include <asm/eth-raw-os.h>
> +#include <dm.h>
> +#include <errno.h>
> +#include <dm/device-internal.h>
> +#include <dm/lists.h>
> +
> +static int eth_raw_bus_post_bind(struct udevice *dev)
> +{
> +	struct sandbox_eth_raw_if_nameindex *ni, *i;
> +	struct udevice *child;
> +	struct eth_sandbox_raw_priv *priv;
> +	char *ub_ifname;
> +	u32 skip_localhost = 0;
> +
> +	ni = sandbox_eth_raw_if_nameindex();
> +	if (!ni)
> +		return -EINVAL;
> +
> +	dev_read_u32(dev, "skip-localhost", &skip_localhost);
> +	for (i = ni; !(i->if_index == 0 && !i->if_name); i++) {
> +		int local = sandbox_eth_raw_os_is_local(i->if_name);
> +
> +		if (local < 0)
> +			continue;
> +		if (skip_localhost && local)
> +			continue;
> +
> +		ub_ifname = malloc(IFNAMSIZ + 5);

Hello Joe,

thanks a lot for redesigning how host interfaces are enumerated.

In the line above it is not immediately clear what the extra four bytes
(after IFNAMSIZ characters plus '\0') are used for. If they are needed,
a comment line would be helpful.

Should we use calloc to zero out unused bytes?

Best regards

Heinrich

> +		strcpy(ub_ifname, "host_");
> +		strncat(ub_ifname, i->if_name, IFNAMSIZ);
> +		device_bind_driver(dev, "eth_sandbox_raw", ub_ifname, &child);
> +
> +		device_set_name_alloced(child);
> +		device_probe(child);
> +		priv = dev_get_priv(child);
> +		if (priv) {
> +			memcpy(priv->host_ifname, i->if_name, IFNAMSIZ);
> +			priv->host_ifindex = i->if_index;
> +			priv->local = local;
> +		}
> +	}
> +
> +	sandbox_eth_raw_if_freenameindex(ni);
> +
> +	return 0;
> +}
> +
> +static const struct udevice_id sandbox_eth_raw_bus_ids[] = {
> +	{ .compatible = "sandbox,eth-raw-bus" },
> +	{ }
> +};
> +
> +U_BOOT_DRIVER(sandbox_eth_raw_bus) = {
> +	.name       = "sb_eth_raw_bus",
> +	.id         = UCLASS_SIMPLE_BUS,
> +	.of_match   = sandbox_eth_raw_bus_ids,
> +	.bind       = eth_raw_bus_post_bind,
> +};
> 



More information about the U-Boot mailing list