diff mbox series

[PATCHv6,04/14] net/lwip: implement dhcp cmd

Message ID 20230814133253.4150-5-maxim.uvarov@linaro.org
State Superseded
Headers show
Series net/lwip: add lwip library for the network stack | expand

Commit Message

Maxim Uvarov Aug. 14, 2023, 1:32 p.m. UTC
Implement function for dhcp command with lwIP variant. Usage and output is
the same as the original command. This code called by compatibility code
between U-Boot and lwIP.

Signed-off-by: Maxim Uvarov <maxim.uvarov@linaro.org>
---
 include/net/lwip.h             | 10 +++++++
 net/lwip/Makefile              |  1 +
 net/lwip/apps/dhcp/lwip-dhcp.c | 51 ++++++++++++++++++++++++++++++++++
 3 files changed, 62 insertions(+)
 create mode 100644 net/lwip/apps/dhcp/lwip-dhcp.c

Comments

Ilias Apalodimas Aug. 14, 2023, 2:21 p.m. UTC | #1
On Mon, Aug 14, 2023 at 07:32:43PM +0600, Maxim Uvarov wrote:
> Implement function for dhcp command with lwIP variant. Usage and output is
> the same as the original command. This code called by compatibility code
> between U-Boot and lwIP.

Same as the dns command

> 
> Signed-off-by: Maxim Uvarov <maxim.uvarov@linaro.org>
> ---
>  include/net/lwip.h             | 10 +++++++
>  net/lwip/Makefile              |  1 +
>  net/lwip/apps/dhcp/lwip-dhcp.c | 51 ++++++++++++++++++++++++++++++++++
>  3 files changed, 62 insertions(+)
>  create mode 100644 net/lwip/apps/dhcp/lwip-dhcp.c
> 
> diff --git a/include/net/lwip.h b/include/net/lwip.h
> index c83b5c8231..2f035280eb 100644
> --- a/include/net/lwip.h
> +++ b/include/net/lwip.h
> @@ -15,3 +15,13 @@ int do_lwip_dns(struct cmd_tbl *cmdtp, int flag, int argc,
>  *         Other value < 0, if error
>  */
>  int ulwip_dns(char *name, char *varname);
> +
> +/*
> +* This function creates the DHCP request to obtain IP address. If DHCP server

Sphinx needs something more, please check the existing functions

> +* returns file name, this file will be downloaded with tftp.  After this
> +* function you need to invoke the polling loop to process network communication.
> +*
> +* Return: 0 if success
> +*         Other value < 0, if error
> +*/
> +int ulwip_dhcp(void);
> diff --git a/net/lwip/Makefile b/net/lwip/Makefile
> index 6d2c00605b..59323fb325 100644
> --- a/net/lwip/Makefile
> +
> +static struct dhcp dhcp;
> +
> +static int ulwip_dhcp_tmo(void)
> +{
> +	switch (dhcp.state) {
> +	case DHCP_STATE_BOUND:
> +		env_set("bootfile", dhcp.boot_file_name);
> +		env_set("ipaddr", ip4addr_ntoa(&dhcp.offered_ip_addr));
> +		env_set("netmask", ip4addr_ntoa(&dhcp.offered_sn_mask));
> +		env_set("serverip", ip4addr_ntoa(&dhcp.server_ip_addr));
> +		printf("DHCP client bound to address %s\n", ip4addr_ntoa(&dhcp.offered_ip_addr));
> +		break;
> +	default:
> +		return -1;
> +	}
> +
> +	return 0;
> +}
> +
> +int ulwip_dhcp(void)
> +{
> +	int err;
> +	struct netif *netif;
> +
> +	ulwip_set_tmo(ulwip_dhcp_tmo);
> +	netif = netif_get_by_index(1);

What's (1)?

> +
> +	if (!netif_get_client_data(netif, LWIP_NETIF_CLIENT_DATA_INDEX_DHCP))
> +		dhcp_set_struct(netif, &dhcp);
> +
> +	err = dhcp_start(netif);
> +	if (err)
> +		printf("dhcp_start error %d\n", err);
> +
> +	return err;
> +}
> -- 
> 2.30.2
> 

Thanks
/Ilias
Maxim Uvarov Aug. 14, 2023, 3:18 p.m. UTC | #2
On Mon, 14 Aug 2023 at 20:21, Ilias Apalodimas <ilias.apalodimas@linaro.org>
wrote:

> On Mon, Aug 14, 2023 at 07:32:43PM +0600, Maxim Uvarov wrote:
> > Implement function for dhcp command with lwIP variant. Usage and output
> is
> > the same as the original command. This code called by compatibility code
> > between U-Boot and lwIP.
>
> Same as the dns command
>
> >
> > Signed-off-by: Maxim Uvarov <maxim.uvarov@linaro.org>
> > ---
> >  include/net/lwip.h             | 10 +++++++
> >  net/lwip/Makefile              |  1 +
> >  net/lwip/apps/dhcp/lwip-dhcp.c | 51 ++++++++++++++++++++++++++++++++++
> >  3 files changed, 62 insertions(+)
> >  create mode 100644 net/lwip/apps/dhcp/lwip-dhcp.c
> >
> > diff --git a/include/net/lwip.h b/include/net/lwip.h
> > index c83b5c8231..2f035280eb 100644
> > --- a/include/net/lwip.h
> > +++ b/include/net/lwip.h
> > @@ -15,3 +15,13 @@ int do_lwip_dns(struct cmd_tbl *cmdtp, int flag, int
> argc,
> >  *         Other value < 0, if error
> >  */
> >  int ulwip_dns(char *name, char *varname);
> > +
> > +/*
> > +* This function creates the DHCP request to obtain IP address. If DHCP
> server
>
> Sphinx needs something more, please check the existing functions
>
> > +* returns file name, this file will be downloaded with tftp.  After this
> > +* function you need to invoke the polling loop to process network
> communication.
> > +*
> > +* Return: 0 if success
> > +*         Other value < 0, if error
> > +*/
> > +int ulwip_dhcp(void);
> > diff --git a/net/lwip/Makefile b/net/lwip/Makefile
> > index 6d2c00605b..59323fb325 100644
> > --- a/net/lwip/Makefile
> > +
> > +static struct dhcp dhcp;
> > +
> > +static int ulwip_dhcp_tmo(void)
> > +{
> > +     switch (dhcp.state) {
> > +     case DHCP_STATE_BOUND:
> > +             env_set("bootfile", dhcp.boot_file_name);
> > +             env_set("ipaddr", ip4addr_ntoa(&dhcp.offered_ip_addr));
> > +             env_set("netmask", ip4addr_ntoa(&dhcp.offered_sn_mask));
> > +             env_set("serverip", ip4addr_ntoa(&dhcp.server_ip_addr));
> > +             printf("DHCP client bound to address %s\n",
> ip4addr_ntoa(&dhcp.offered_ip_addr));
> > +             break;
> > +     default:
> > +             return -1;
> > +     }
> > +
> > +     return 0;
> > +}
> > +
> > +int ulwip_dhcp(void)
> > +{
> > +     int err;
> > +     struct netif *netif;
> > +
> > +     ulwip_set_tmo(ulwip_dhcp_tmo);
> > +     netif = netif_get_by_index(1);
>
> What's (1)?
>
>
Only one lwip netif is registered. 1 - here is the index of netif. I don't
think that there is any definition for that...



> > +
> > +     if (!netif_get_client_data(netif,
> LWIP_NETIF_CLIENT_DATA_INDEX_DHCP))
> > +             dhcp_set_struct(netif, &dhcp);
> > +
> > +     err = dhcp_start(netif);
> > +     if (err)
> > +             printf("dhcp_start error %d\n", err);
> > +
> > +     return err;
> > +}
> > --
> > 2.30.2
> >
>
> Thanks
> /Ilias
>
Tom Rini Aug. 14, 2023, 3:29 p.m. UTC | #3
On Mon, Aug 14, 2023 at 09:18:19PM +0600, Maxim Uvarov wrote:
> On Mon, 14 Aug 2023 at 20:21, Ilias Apalodimas <ilias.apalodimas@linaro.org>
> wrote:
> 
> > On Mon, Aug 14, 2023 at 07:32:43PM +0600, Maxim Uvarov wrote:
> > > Implement function for dhcp command with lwIP variant. Usage and output
> > is
> > > the same as the original command. This code called by compatibility code
> > > between U-Boot and lwIP.
> >
> > Same as the dns command
> >
> > >
> > > Signed-off-by: Maxim Uvarov <maxim.uvarov@linaro.org>
> > > ---
> > >  include/net/lwip.h             | 10 +++++++
> > >  net/lwip/Makefile              |  1 +
> > >  net/lwip/apps/dhcp/lwip-dhcp.c | 51 ++++++++++++++++++++++++++++++++++
> > >  3 files changed, 62 insertions(+)
> > >  create mode 100644 net/lwip/apps/dhcp/lwip-dhcp.c
> > >
> > > diff --git a/include/net/lwip.h b/include/net/lwip.h
> > > index c83b5c8231..2f035280eb 100644
> > > --- a/include/net/lwip.h
> > > +++ b/include/net/lwip.h
> > > @@ -15,3 +15,13 @@ int do_lwip_dns(struct cmd_tbl *cmdtp, int flag, int
> > argc,
> > >  *         Other value < 0, if error
> > >  */
> > >  int ulwip_dns(char *name, char *varname);
> > > +
> > > +/*
> > > +* This function creates the DHCP request to obtain IP address. If DHCP
> > server
> >
> > Sphinx needs something more, please check the existing functions
> >
> > > +* returns file name, this file will be downloaded with tftp.  After this
> > > +* function you need to invoke the polling loop to process network
> > communication.
> > > +*
> > > +* Return: 0 if success
> > > +*         Other value < 0, if error
> > > +*/
> > > +int ulwip_dhcp(void);
> > > diff --git a/net/lwip/Makefile b/net/lwip/Makefile
> > > index 6d2c00605b..59323fb325 100644
> > > --- a/net/lwip/Makefile
> > > +
> > > +static struct dhcp dhcp;
> > > +
> > > +static int ulwip_dhcp_tmo(void)
> > > +{
> > > +     switch (dhcp.state) {
> > > +     case DHCP_STATE_BOUND:
> > > +             env_set("bootfile", dhcp.boot_file_name);
> > > +             env_set("ipaddr", ip4addr_ntoa(&dhcp.offered_ip_addr));
> > > +             env_set("netmask", ip4addr_ntoa(&dhcp.offered_sn_mask));
> > > +             env_set("serverip", ip4addr_ntoa(&dhcp.server_ip_addr));
> > > +             printf("DHCP client bound to address %s\n",
> > ip4addr_ntoa(&dhcp.offered_ip_addr));
> > > +             break;
> > > +     default:
> > > +             return -1;
> > > +     }
> > > +
> > > +     return 0;
> > > +}
> > > +
> > > +int ulwip_dhcp(void)
> > > +{
> > > +     int err;
> > > +     struct netif *netif;
> > > +
> > > +     ulwip_set_tmo(ulwip_dhcp_tmo);
> > > +     netif = netif_get_by_index(1);
> >
> > What's (1)?
> >
> >
> Only one lwip netif is registered. 1 - here is the index of netif. I don't
> think that there is any definition for that...
> 

And there's only ever going to be one interface (even if we have ipv4
and ipv60 ? If so, define it to something please, thanks.
Maxim Uvarov Aug. 17, 2023, 1:46 p.m. UTC | #4
On Mon, 14 Aug 2023 at 21:29, Tom Rini <trini@konsulko.com> wrote:

> On Mon, Aug 14, 2023 at 09:18:19PM +0600, Maxim Uvarov wrote:
> > On Mon, 14 Aug 2023 at 20:21, Ilias Apalodimas <
> ilias.apalodimas@linaro.org>
> > wrote:
> >
> > > On Mon, Aug 14, 2023 at 07:32:43PM +0600, Maxim Uvarov wrote:
> > > > Implement function for dhcp command with lwIP variant. Usage and
> output
> > > is
> > > > the same as the original command. This code called by compatibility
> code
> > > > between U-Boot and lwIP.
> > >
> > > Same as the dns command
> > >
> > > >
> > > > Signed-off-by: Maxim Uvarov <maxim.uvarov@linaro.org>
> > > > ---
> > > >  include/net/lwip.h             | 10 +++++++
> > > >  net/lwip/Makefile              |  1 +
> > > >  net/lwip/apps/dhcp/lwip-dhcp.c | 51
> ++++++++++++++++++++++++++++++++++
> > > >  3 files changed, 62 insertions(+)
> > > >  create mode 100644 net/lwip/apps/dhcp/lwip-dhcp.c
> > > >
> > > > diff --git a/include/net/lwip.h b/include/net/lwip.h
> > > > index c83b5c8231..2f035280eb 100644
> > > > --- a/include/net/lwip.h
> > > > +++ b/include/net/lwip.h
> > > > @@ -15,3 +15,13 @@ int do_lwip_dns(struct cmd_tbl *cmdtp, int flag,
> int
> > > argc,
> > > >  *         Other value < 0, if error
> > > >  */
> > > >  int ulwip_dns(char *name, char *varname);
> > > > +
> > > > +/*
> > > > +* This function creates the DHCP request to obtain IP address. If
> DHCP
> > > server
> > >
> > > Sphinx needs something more, please check the existing functions
> > >
> > > > +* returns file name, this file will be downloaded with tftp.  After
> this
> > > > +* function you need to invoke the polling loop to process network
> > > communication.
> > > > +*
> > > > +* Return: 0 if success
> > > > +*         Other value < 0, if error
> > > > +*/
> > > > +int ulwip_dhcp(void);
> > > > diff --git a/net/lwip/Makefile b/net/lwip/Makefile
> > > > index 6d2c00605b..59323fb325 100644
> > > > --- a/net/lwip/Makefile
> > > > +
> > > > +static struct dhcp dhcp;
> > > > +
> > > > +static int ulwip_dhcp_tmo(void)
> > > > +{
> > > > +     switch (dhcp.state) {
> > > > +     case DHCP_STATE_BOUND:
> > > > +             env_set("bootfile", dhcp.boot_file_name);
> > > > +             env_set("ipaddr", ip4addr_ntoa(&dhcp.offered_ip_addr));
> > > > +             env_set("netmask",
> ip4addr_ntoa(&dhcp.offered_sn_mask));
> > > > +             env_set("serverip",
> ip4addr_ntoa(&dhcp.server_ip_addr));
> > > > +             printf("DHCP client bound to address %s\n",
> > > ip4addr_ntoa(&dhcp.offered_ip_addr));
> > > > +             break;
> > > > +     default:
> > > > +             return -1;
> > > > +     }
> > > > +
> > > > +     return 0;
> > > > +}
> > > > +
> > > > +int ulwip_dhcp(void)
> > > > +{
> > > > +     int err;
> > > > +     struct netif *netif;
> > > > +
> > > > +     ulwip_set_tmo(ulwip_dhcp_tmo);
> > > > +     netif = netif_get_by_index(1);
> > >
> > > What's (1)?
> > >
> > >
> > Only one lwip netif is registered. 1 - here is the index of netif. I
> don't
> > think that there is any definition for that...
> >
>
> And there's only ever going to be one interface (even if we have ipv4
> and ipv60 ? If so, define it to something please, thanks.
>
> --
> Tom
>

Yes, one interface has 2 addresses ipv4 and ipv6.
Peter Robinson Aug. 17, 2023, 2:04 p.m. UTC | #5
On Thu, Aug 17, 2023 at 2:46 PM Maxim Uvarov <maxim.uvarov@linaro.org> wrote:
>
> On Mon, 14 Aug 2023 at 21:29, Tom Rini <trini@konsulko.com> wrote:
>
> > On Mon, Aug 14, 2023 at 09:18:19PM +0600, Maxim Uvarov wrote:
> > > On Mon, 14 Aug 2023 at 20:21, Ilias Apalodimas <
> > ilias.apalodimas@linaro.org>
> > > wrote:
> > >
> > > > On Mon, Aug 14, 2023 at 07:32:43PM +0600, Maxim Uvarov wrote:
> > > > > Implement function for dhcp command with lwIP variant. Usage and
> > output
> > > > is
> > > > > the same as the original command. This code called by compatibility
> > code
> > > > > between U-Boot and lwIP.
> > > >
> > > > Same as the dns command
> > > >
> > > > >
> > > > > Signed-off-by: Maxim Uvarov <maxim.uvarov@linaro.org>
> > > > > ---
> > > > >  include/net/lwip.h             | 10 +++++++
> > > > >  net/lwip/Makefile              |  1 +
> > > > >  net/lwip/apps/dhcp/lwip-dhcp.c | 51
> > ++++++++++++++++++++++++++++++++++
> > > > >  3 files changed, 62 insertions(+)
> > > > >  create mode 100644 net/lwip/apps/dhcp/lwip-dhcp.c
> > > > >
> > > > > diff --git a/include/net/lwip.h b/include/net/lwip.h
> > > > > index c83b5c8231..2f035280eb 100644
> > > > > --- a/include/net/lwip.h
> > > > > +++ b/include/net/lwip.h
> > > > > @@ -15,3 +15,13 @@ int do_lwip_dns(struct cmd_tbl *cmdtp, int flag,
> > int
> > > > argc,
> > > > >  *         Other value < 0, if error
> > > > >  */
> > > > >  int ulwip_dns(char *name, char *varname);
> > > > > +
> > > > > +/*
> > > > > +* This function creates the DHCP request to obtain IP address. If
> > DHCP
> > > > server
> > > >
> > > > Sphinx needs something more, please check the existing functions
> > > >
> > > > > +* returns file name, this file will be downloaded with tftp.  After
> > this
> > > > > +* function you need to invoke the polling loop to process network
> > > > communication.
> > > > > +*
> > > > > +* Return: 0 if success
> > > > > +*         Other value < 0, if error
> > > > > +*/
> > > > > +int ulwip_dhcp(void);
> > > > > diff --git a/net/lwip/Makefile b/net/lwip/Makefile
> > > > > index 6d2c00605b..59323fb325 100644
> > > > > --- a/net/lwip/Makefile
> > > > > +
> > > > > +static struct dhcp dhcp;
> > > > > +
> > > > > +static int ulwip_dhcp_tmo(void)
> > > > > +{
> > > > > +     switch (dhcp.state) {
> > > > > +     case DHCP_STATE_BOUND:
> > > > > +             env_set("bootfile", dhcp.boot_file_name);
> > > > > +             env_set("ipaddr", ip4addr_ntoa(&dhcp.offered_ip_addr));
> > > > > +             env_set("netmask",
> > ip4addr_ntoa(&dhcp.offered_sn_mask));
> > > > > +             env_set("serverip",
> > ip4addr_ntoa(&dhcp.server_ip_addr));
> > > > > +             printf("DHCP client bound to address %s\n",
> > > > ip4addr_ntoa(&dhcp.offered_ip_addr));
> > > > > +             break;
> > > > > +     default:
> > > > > +             return -1;
> > > > > +     }
> > > > > +
> > > > > +     return 0;
> > > > > +}
> > > > > +
> > > > > +int ulwip_dhcp(void)
> > > > > +{
> > > > > +     int err;
> > > > > +     struct netif *netif;
> > > > > +
> > > > > +     ulwip_set_tmo(ulwip_dhcp_tmo);
> > > > > +     netif = netif_get_by_index(1);
> > > >
> > > > What's (1)?
> > > >
> > > >
> > > Only one lwip netif is registered. 1 - here is the index of netif. I
> > don't
> > > think that there is any definition for that...
> > >
> >
> > And there's only ever going to be one interface (even if we have ipv4
> > and ipv60 ? If so, define it to something please, thanks.
> >
> > --
> > Tom
> >
>
> Yes, one interface has 2 addresses ipv4 and ipv6.

What about a device like a router which may have multiple wired
ethernet interface?
Maxim Uvarov Aug. 17, 2023, 2:55 p.m. UTC | #6
On Thu, 17 Aug 2023 at 20:04, Peter Robinson <pbrobinson@gmail.com> wrote:

> On Thu, Aug 17, 2023 at 2:46 PM Maxim Uvarov <maxim.uvarov@linaro.org>
> wrote:
> >
> > On Mon, 14 Aug 2023 at 21:29, Tom Rini <trini@konsulko.com> wrote:
> >
> > > On Mon, Aug 14, 2023 at 09:18:19PM +0600, Maxim Uvarov wrote:
> > > > On Mon, 14 Aug 2023 at 20:21, Ilias Apalodimas <
> > > ilias.apalodimas@linaro.org>
> > > > wrote:
> > > >
> > > > > On Mon, Aug 14, 2023 at 07:32:43PM +0600, Maxim Uvarov wrote:
> > > > > > Implement function for dhcp command with lwIP variant. Usage and
> > > output
> > > > > is
> > > > > > the same as the original command. This code called by
> compatibility
> > > code
> > > > > > between U-Boot and lwIP.
> > > > >
> > > > > Same as the dns command
> > > > >
> > > > > >
> > > > > > Signed-off-by: Maxim Uvarov <maxim.uvarov@linaro.org>
> > > > > > ---
> > > > > >  include/net/lwip.h             | 10 +++++++
> > > > > >  net/lwip/Makefile              |  1 +
> > > > > >  net/lwip/apps/dhcp/lwip-dhcp.c | 51
> > > ++++++++++++++++++++++++++++++++++
> > > > > >  3 files changed, 62 insertions(+)
> > > > > >  create mode 100644 net/lwip/apps/dhcp/lwip-dhcp.c
> > > > > >
> > > > > > diff --git a/include/net/lwip.h b/include/net/lwip.h
> > > > > > index c83b5c8231..2f035280eb 100644
> > > > > > --- a/include/net/lwip.h
> > > > > > +++ b/include/net/lwip.h
> > > > > > @@ -15,3 +15,13 @@ int do_lwip_dns(struct cmd_tbl *cmdtp, int
> flag,
> > > int
> > > > > argc,
> > > > > >  *         Other value < 0, if error
> > > > > >  */
> > > > > >  int ulwip_dns(char *name, char *varname);
> > > > > > +
> > > > > > +/*
> > > > > > +* This function creates the DHCP request to obtain IP address.
> If
> > > DHCP
> > > > > server
> > > > >
> > > > > Sphinx needs something more, please check the existing functions
> > > > >
> > > > > > +* returns file name, this file will be downloaded with tftp.
> After
> > > this
> > > > > > +* function you need to invoke the polling loop to process
> network
> > > > > communication.
> > > > > > +*
> > > > > > +* Return: 0 if success
> > > > > > +*         Other value < 0, if error
> > > > > > +*/
> > > > > > +int ulwip_dhcp(void);
> > > > > > diff --git a/net/lwip/Makefile b/net/lwip/Makefile
> > > > > > index 6d2c00605b..59323fb325 100644
> > > > > > --- a/net/lwip/Makefile
> > > > > > +
> > > > > > +static struct dhcp dhcp;
> > > > > > +
> > > > > > +static int ulwip_dhcp_tmo(void)
> > > > > > +{
> > > > > > +     switch (dhcp.state) {
> > > > > > +     case DHCP_STATE_BOUND:
> > > > > > +             env_set("bootfile", dhcp.boot_file_name);
> > > > > > +             env_set("ipaddr",
> ip4addr_ntoa(&dhcp.offered_ip_addr));
> > > > > > +             env_set("netmask",
> > > ip4addr_ntoa(&dhcp.offered_sn_mask));
> > > > > > +             env_set("serverip",
> > > ip4addr_ntoa(&dhcp.server_ip_addr));
> > > > > > +             printf("DHCP client bound to address %s\n",
> > > > > ip4addr_ntoa(&dhcp.offered_ip_addr));
> > > > > > +             break;
> > > > > > +     default:
> > > > > > +             return -1;
> > > > > > +     }
> > > > > > +
> > > > > > +     return 0;
> > > > > > +}
> > > > > > +
> > > > > > +int ulwip_dhcp(void)
> > > > > > +{
> > > > > > +     int err;
> > > > > > +     struct netif *netif;
> > > > > > +
> > > > > > +     ulwip_set_tmo(ulwip_dhcp_tmo);
> > > > > > +     netif = netif_get_by_index(1);
> > > > >
> > > > > What's (1)?
> > > > >
> > > > >
> > > > Only one lwip netif is registered. 1 - here is the index of netif. I
> > > don't
> > > > think that there is any definition for that...
> > > >
> > >
> > > And there's only ever going to be one interface (even if we have ipv4
> > > and ipv60 ? If so, define it to something please, thanks.
> > >
> > > --
> > > Tom
> > >
> >
> > Yes, one interface has 2 addresses ipv4 and ipv6.
>
> What about a device like a router which may have multiple wired
> ethernet interface?
>

Yea, looks like we need lwip netif per U-Boots eth_devs. I did not yet
support several eth devices, but it will be good to add this to not break
existing functionality..

BR,
Maxim.
Tom Rini Aug. 17, 2023, 3:10 p.m. UTC | #7
On Thu, Aug 17, 2023 at 08:55:17PM +0600, Maxim Uvarov wrote:
> On Thu, 17 Aug 2023 at 20:04, Peter Robinson <pbrobinson@gmail.com> wrote:
> 
> > On Thu, Aug 17, 2023 at 2:46 PM Maxim Uvarov <maxim.uvarov@linaro.org>
> > wrote:
> > >
> > > On Mon, 14 Aug 2023 at 21:29, Tom Rini <trini@konsulko.com> wrote:
> > >
> > > > On Mon, Aug 14, 2023 at 09:18:19PM +0600, Maxim Uvarov wrote:
> > > > > On Mon, 14 Aug 2023 at 20:21, Ilias Apalodimas <
> > > > ilias.apalodimas@linaro.org>
> > > > > wrote:
> > > > >
> > > > > > On Mon, Aug 14, 2023 at 07:32:43PM +0600, Maxim Uvarov wrote:
> > > > > > > Implement function for dhcp command with lwIP variant. Usage and
> > > > output
> > > > > > is
> > > > > > > the same as the original command. This code called by
> > compatibility
> > > > code
> > > > > > > between U-Boot and lwIP.
> > > > > >
> > > > > > Same as the dns command
> > > > > >
> > > > > > >
> > > > > > > Signed-off-by: Maxim Uvarov <maxim.uvarov@linaro.org>
> > > > > > > ---
> > > > > > >  include/net/lwip.h             | 10 +++++++
> > > > > > >  net/lwip/Makefile              |  1 +
> > > > > > >  net/lwip/apps/dhcp/lwip-dhcp.c | 51
> > > > ++++++++++++++++++++++++++++++++++
> > > > > > >  3 files changed, 62 insertions(+)
> > > > > > >  create mode 100644 net/lwip/apps/dhcp/lwip-dhcp.c
> > > > > > >
> > > > > > > diff --git a/include/net/lwip.h b/include/net/lwip.h
> > > > > > > index c83b5c8231..2f035280eb 100644
> > > > > > > --- a/include/net/lwip.h
> > > > > > > +++ b/include/net/lwip.h
> > > > > > > @@ -15,3 +15,13 @@ int do_lwip_dns(struct cmd_tbl *cmdtp, int
> > flag,
> > > > int
> > > > > > argc,
> > > > > > >  *         Other value < 0, if error
> > > > > > >  */
> > > > > > >  int ulwip_dns(char *name, char *varname);
> > > > > > > +
> > > > > > > +/*
> > > > > > > +* This function creates the DHCP request to obtain IP address.
> > If
> > > > DHCP
> > > > > > server
> > > > > >
> > > > > > Sphinx needs something more, please check the existing functions
> > > > > >
> > > > > > > +* returns file name, this file will be downloaded with tftp.
> > After
> > > > this
> > > > > > > +* function you need to invoke the polling loop to process
> > network
> > > > > > communication.
> > > > > > > +*
> > > > > > > +* Return: 0 if success
> > > > > > > +*         Other value < 0, if error
> > > > > > > +*/
> > > > > > > +int ulwip_dhcp(void);
> > > > > > > diff --git a/net/lwip/Makefile b/net/lwip/Makefile
> > > > > > > index 6d2c00605b..59323fb325 100644
> > > > > > > --- a/net/lwip/Makefile
> > > > > > > +
> > > > > > > +static struct dhcp dhcp;
> > > > > > > +
> > > > > > > +static int ulwip_dhcp_tmo(void)
> > > > > > > +{
> > > > > > > +     switch (dhcp.state) {
> > > > > > > +     case DHCP_STATE_BOUND:
> > > > > > > +             env_set("bootfile", dhcp.boot_file_name);
> > > > > > > +             env_set("ipaddr",
> > ip4addr_ntoa(&dhcp.offered_ip_addr));
> > > > > > > +             env_set("netmask",
> > > > ip4addr_ntoa(&dhcp.offered_sn_mask));
> > > > > > > +             env_set("serverip",
> > > > ip4addr_ntoa(&dhcp.server_ip_addr));
> > > > > > > +             printf("DHCP client bound to address %s\n",
> > > > > > ip4addr_ntoa(&dhcp.offered_ip_addr));
> > > > > > > +             break;
> > > > > > > +     default:
> > > > > > > +             return -1;
> > > > > > > +     }
> > > > > > > +
> > > > > > > +     return 0;
> > > > > > > +}
> > > > > > > +
> > > > > > > +int ulwip_dhcp(void)
> > > > > > > +{
> > > > > > > +     int err;
> > > > > > > +     struct netif *netif;
> > > > > > > +
> > > > > > > +     ulwip_set_tmo(ulwip_dhcp_tmo);
> > > > > > > +     netif = netif_get_by_index(1);
> > > > > >
> > > > > > What's (1)?
> > > > > >
> > > > > >
> > > > > Only one lwip netif is registered. 1 - here is the index of netif. I
> > > > don't
> > > > > think that there is any definition for that...
> > > > >
> > > >
> > > > And there's only ever going to be one interface (even if we have ipv4
> > > > and ipv60 ? If so, define it to something please, thanks.
> > > >
> > > > --
> > > > Tom
> > > >
> > >
> > > Yes, one interface has 2 addresses ipv4 and ipv6.
> >
> > What about a device like a router which may have multiple wired
> > ethernet interface?
> 
> Yea, looks like we need lwip netif per U-Boots eth_devs. I did not yet
> support several eth devices, but it will be good to add this to not break
> existing functionality..

The general case ends up being if we have more than one interface,
ethact is what's used.  I'm unsure off-hand if some of the fancier
networking-centric chipsets and devices have something more complex
setup in their stacks.
Maxim Uvarov Aug. 18, 2023, 9:39 a.m. UTC | #8
On Thu, 17 Aug 2023 at 21:10, Tom Rini <trini@konsulko.com> wrote:

> On Thu, Aug 17, 2023 at 08:55:17PM +0600, Maxim Uvarov wrote:
> > On Thu, 17 Aug 2023 at 20:04, Peter Robinson <pbrobinson@gmail.com>
> wrote:
> >
> > > On Thu, Aug 17, 2023 at 2:46 PM Maxim Uvarov <maxim.uvarov@linaro.org>
> > > wrote:
> > > >
> > > > On Mon, 14 Aug 2023 at 21:29, Tom Rini <trini@konsulko.com> wrote:
> > > >
> > > > > On Mon, Aug 14, 2023 at 09:18:19PM +0600, Maxim Uvarov wrote:
> > > > > > On Mon, 14 Aug 2023 at 20:21, Ilias Apalodimas <
> > > > > ilias.apalodimas@linaro.org>
> > > > > > wrote:
> > > > > >
> > > > > > > On Mon, Aug 14, 2023 at 07:32:43PM +0600, Maxim Uvarov wrote:
> > > > > > > > Implement function for dhcp command with lwIP variant. Usage
> and
> > > > > output
> > > > > > > is
> > > > > > > > the same as the original command. This code called by
> > > compatibility
> > > > > code
> > > > > > > > between U-Boot and lwIP.
> > > > > > >
> > > > > > > Same as the dns command
> > > > > > >
> > > > > > > >
> > > > > > > > Signed-off-by: Maxim Uvarov <maxim.uvarov@linaro.org>
> > > > > > > > ---
> > > > > > > >  include/net/lwip.h             | 10 +++++++
> > > > > > > >  net/lwip/Makefile              |  1 +
> > > > > > > >  net/lwip/apps/dhcp/lwip-dhcp.c | 51
> > > > > ++++++++++++++++++++++++++++++++++
> > > > > > > >  3 files changed, 62 insertions(+)
> > > > > > > >  create mode 100644 net/lwip/apps/dhcp/lwip-dhcp.c
> > > > > > > >
> > > > > > > > diff --git a/include/net/lwip.h b/include/net/lwip.h
> > > > > > > > index c83b5c8231..2f035280eb 100644
> > > > > > > > --- a/include/net/lwip.h
> > > > > > > > +++ b/include/net/lwip.h
> > > > > > > > @@ -15,3 +15,13 @@ int do_lwip_dns(struct cmd_tbl *cmdtp, int
> > > flag,
> > > > > int
> > > > > > > argc,
> > > > > > > >  *         Other value < 0, if error
> > > > > > > >  */
> > > > > > > >  int ulwip_dns(char *name, char *varname);
> > > > > > > > +
> > > > > > > > +/*
> > > > > > > > +* This function creates the DHCP request to obtain IP
> address.
> > > If
> > > > > DHCP
> > > > > > > server
> > > > > > >
> > > > > > > Sphinx needs something more, please check the existing
> functions
> > > > > > >
> > > > > > > > +* returns file name, this file will be downloaded with tftp.
> > > After
> > > > > this
> > > > > > > > +* function you need to invoke the polling loop to process
> > > network
> > > > > > > communication.
> > > > > > > > +*
> > > > > > > > +* Return: 0 if success
> > > > > > > > +*         Other value < 0, if error
> > > > > > > > +*/
> > > > > > > > +int ulwip_dhcp(void);
> > > > > > > > diff --git a/net/lwip/Makefile b/net/lwip/Makefile
> > > > > > > > index 6d2c00605b..59323fb325 100644
> > > > > > > > --- a/net/lwip/Makefile
> > > > > > > > +
> > > > > > > > +static struct dhcp dhcp;
> > > > > > > > +
> > > > > > > > +static int ulwip_dhcp_tmo(void)
> > > > > > > > +{
> > > > > > > > +     switch (dhcp.state) {
> > > > > > > > +     case DHCP_STATE_BOUND:
> > > > > > > > +             env_set("bootfile", dhcp.boot_file_name);
> > > > > > > > +             env_set("ipaddr",
> > > ip4addr_ntoa(&dhcp.offered_ip_addr));
> > > > > > > > +             env_set("netmask",
> > > > > ip4addr_ntoa(&dhcp.offered_sn_mask));
> > > > > > > > +             env_set("serverip",
> > > > > ip4addr_ntoa(&dhcp.server_ip_addr));
> > > > > > > > +             printf("DHCP client bound to address %s\n",
> > > > > > > ip4addr_ntoa(&dhcp.offered_ip_addr));
> > > > > > > > +             break;
> > > > > > > > +     default:
> > > > > > > > +             return -1;
> > > > > > > > +     }
> > > > > > > > +
> > > > > > > > +     return 0;
> > > > > > > > +}
> > > > > > > > +
> > > > > > > > +int ulwip_dhcp(void)
> > > > > > > > +{
> > > > > > > > +     int err;
> > > > > > > > +     struct netif *netif;
> > > > > > > > +
> > > > > > > > +     ulwip_set_tmo(ulwip_dhcp_tmo);
> > > > > > > > +     netif = netif_get_by_index(1);
> > > > > > >
> > > > > > > What's (1)?
> > > > > > >
> > > > > > >
> > > > > > Only one lwip netif is registered. 1 - here is the index of
> netif. I
> > > > > don't
> > > > > > think that there is any definition for that...
> > > > > >
> > > > >
> > > > > And there's only ever going to be one interface (even if we have
> ipv4
> > > > > and ipv60 ? If so, define it to something please, thanks.
> > > > >
> > > > > --
> > > > > Tom
> > > > >
> > > >
> > > > Yes, one interface has 2 addresses ipv4 and ipv6.
> > >
> > > What about a device like a router which may have multiple wired
> > > ethernet interface?
> >
> > Yea, looks like we need lwip netif per U-Boots eth_devs. I did not yet
> > support several eth devices, but it will be good to add this to not break
> > existing functionality..
>
> The general case ends up being if we have more than one interface,
> ethact is what's used.  I'm unsure off-hand if some of the fancier
> networking-centric chipsets and devices have something more complex
> setup in their stacks.
>
> --
> Tom
>

Likely the bootloader uses the only one ethernet interface for loading. And
it might
be enough to have an IP stack on top of the active eth device only. This is
a model
that I am following in these patches.

BR,
Maxim.
Peter Robinson Aug. 18, 2023, 11:14 a.m. UTC | #9
On Fri, Aug 18, 2023 at 10:39 AM Maxim Uvarov <maxim.uvarov@linaro.org> wrote:
>
>
>
> On Thu, 17 Aug 2023 at 21:10, Tom Rini <trini@konsulko.com> wrote:
>>
>> On Thu, Aug 17, 2023 at 08:55:17PM +0600, Maxim Uvarov wrote:
>> > On Thu, 17 Aug 2023 at 20:04, Peter Robinson <pbrobinson@gmail.com> wrote:
>> >
>> > > On Thu, Aug 17, 2023 at 2:46 PM Maxim Uvarov <maxim.uvarov@linaro.org>
>> > > wrote:
>> > > >
>> > > > On Mon, 14 Aug 2023 at 21:29, Tom Rini <trini@konsulko.com> wrote:
>> > > >
>> > > > > On Mon, Aug 14, 2023 at 09:18:19PM +0600, Maxim Uvarov wrote:
>> > > > > > On Mon, 14 Aug 2023 at 20:21, Ilias Apalodimas <
>> > > > > ilias.apalodimas@linaro.org>
>> > > > > > wrote:
>> > > > > >
>> > > > > > > On Mon, Aug 14, 2023 at 07:32:43PM +0600, Maxim Uvarov wrote:
>> > > > > > > > Implement function for dhcp command with lwIP variant. Usage and
>> > > > > output
>> > > > > > > is
>> > > > > > > > the same as the original command. This code called by
>> > > compatibility
>> > > > > code
>> > > > > > > > between U-Boot and lwIP.
>> > > > > > >
>> > > > > > > Same as the dns command
>> > > > > > >
>> > > > > > > >
>> > > > > > > > Signed-off-by: Maxim Uvarov <maxim.uvarov@linaro.org>
>> > > > > > > > ---
>> > > > > > > >  include/net/lwip.h             | 10 +++++++
>> > > > > > > >  net/lwip/Makefile              |  1 +
>> > > > > > > >  net/lwip/apps/dhcp/lwip-dhcp.c | 51
>> > > > > ++++++++++++++++++++++++++++++++++
>> > > > > > > >  3 files changed, 62 insertions(+)
>> > > > > > > >  create mode 100644 net/lwip/apps/dhcp/lwip-dhcp.c
>> > > > > > > >
>> > > > > > > > diff --git a/include/net/lwip.h b/include/net/lwip.h
>> > > > > > > > index c83b5c8231..2f035280eb 100644
>> > > > > > > > --- a/include/net/lwip.h
>> > > > > > > > +++ b/include/net/lwip.h
>> > > > > > > > @@ -15,3 +15,13 @@ int do_lwip_dns(struct cmd_tbl *cmdtp, int
>> > > flag,
>> > > > > int
>> > > > > > > argc,
>> > > > > > > >  *         Other value < 0, if error
>> > > > > > > >  */
>> > > > > > > >  int ulwip_dns(char *name, char *varname);
>> > > > > > > > +
>> > > > > > > > +/*
>> > > > > > > > +* This function creates the DHCP request to obtain IP address.
>> > > If
>> > > > > DHCP
>> > > > > > > server
>> > > > > > >
>> > > > > > > Sphinx needs something more, please check the existing functions
>> > > > > > >
>> > > > > > > > +* returns file name, this file will be downloaded with tftp.
>> > > After
>> > > > > this
>> > > > > > > > +* function you need to invoke the polling loop to process
>> > > network
>> > > > > > > communication.
>> > > > > > > > +*
>> > > > > > > > +* Return: 0 if success
>> > > > > > > > +*         Other value < 0, if error
>> > > > > > > > +*/
>> > > > > > > > +int ulwip_dhcp(void);
>> > > > > > > > diff --git a/net/lwip/Makefile b/net/lwip/Makefile
>> > > > > > > > index 6d2c00605b..59323fb325 100644
>> > > > > > > > --- a/net/lwip/Makefile
>> > > > > > > > +
>> > > > > > > > +static struct dhcp dhcp;
>> > > > > > > > +
>> > > > > > > > +static int ulwip_dhcp_tmo(void)
>> > > > > > > > +{
>> > > > > > > > +     switch (dhcp.state) {
>> > > > > > > > +     case DHCP_STATE_BOUND:
>> > > > > > > > +             env_set("bootfile", dhcp.boot_file_name);
>> > > > > > > > +             env_set("ipaddr",
>> > > ip4addr_ntoa(&dhcp.offered_ip_addr));
>> > > > > > > > +             env_set("netmask",
>> > > > > ip4addr_ntoa(&dhcp.offered_sn_mask));
>> > > > > > > > +             env_set("serverip",
>> > > > > ip4addr_ntoa(&dhcp.server_ip_addr));
>> > > > > > > > +             printf("DHCP client bound to address %s\n",
>> > > > > > > ip4addr_ntoa(&dhcp.offered_ip_addr));
>> > > > > > > > +             break;
>> > > > > > > > +     default:
>> > > > > > > > +             return -1;
>> > > > > > > > +     }
>> > > > > > > > +
>> > > > > > > > +     return 0;
>> > > > > > > > +}
>> > > > > > > > +
>> > > > > > > > +int ulwip_dhcp(void)
>> > > > > > > > +{
>> > > > > > > > +     int err;
>> > > > > > > > +     struct netif *netif;
>> > > > > > > > +
>> > > > > > > > +     ulwip_set_tmo(ulwip_dhcp_tmo);
>> > > > > > > > +     netif = netif_get_by_index(1);
>> > > > > > >
>> > > > > > > What's (1)?
>> > > > > > >
>> > > > > > >
>> > > > > > Only one lwip netif is registered. 1 - here is the index of netif. I
>> > > > > don't
>> > > > > > think that there is any definition for that...
>> > > > > >
>> > > > >
>> > > > > And there's only ever going to be one interface (even if we have ipv4
>> > > > > and ipv60 ? If so, define it to something please, thanks.
>> > > > >
>> > > > > --
>> > > > > Tom
>> > > > >
>> > > >
>> > > > Yes, one interface has 2 addresses ipv4 and ipv6.
>> > >
>> > > What about a device like a router which may have multiple wired
>> > > ethernet interface?
>> >
>> > Yea, looks like we need lwip netif per U-Boots eth_devs. I did not yet
>> > support several eth devices, but it will be good to add this to not break
>> > existing functionality..
>>
>> The general case ends up being if we have more than one interface,
>> ethact is what's used.  I'm unsure off-hand if some of the fancier
>> networking-centric chipsets and devices have something more complex
>> setup in their stacks.
>>
>> --
>> Tom
>
>
> Likely the bootloader uses the only one ethernet interface for loading. And it might
> be enough to have an IP stack on top of the active eth device only. This is a model
> that I am following in these patches.

It's possible, yes, that there may only be one active device, but you
still will need to be able to handle all of them to at least work out
which one has a link that you can bring up to get an IP. There's a
bunch of devices that will have two interfaces with the same NIC
driver, plus also there's at least a few devices that have an onboard
switch (MV88E6XXX) although grep only shows a single one upstream.
Tom Rini Aug. 18, 2023, 2:24 p.m. UTC | #10
On Fri, Aug 18, 2023 at 12:14:11PM +0100, Peter Robinson wrote:
> On Fri, Aug 18, 2023 at 10:39 AM Maxim Uvarov <maxim.uvarov@linaro.org> wrote:
> >
> >
> >
> > On Thu, 17 Aug 2023 at 21:10, Tom Rini <trini@konsulko.com> wrote:
> >>
> >> On Thu, Aug 17, 2023 at 08:55:17PM +0600, Maxim Uvarov wrote:
> >> > On Thu, 17 Aug 2023 at 20:04, Peter Robinson <pbrobinson@gmail.com> wrote:
> >> >
> >> > > On Thu, Aug 17, 2023 at 2:46 PM Maxim Uvarov <maxim.uvarov@linaro.org>
> >> > > wrote:
> >> > > >
> >> > > > On Mon, 14 Aug 2023 at 21:29, Tom Rini <trini@konsulko.com> wrote:
> >> > > >
> >> > > > > On Mon, Aug 14, 2023 at 09:18:19PM +0600, Maxim Uvarov wrote:
> >> > > > > > On Mon, 14 Aug 2023 at 20:21, Ilias Apalodimas <
> >> > > > > ilias.apalodimas@linaro.org>
> >> > > > > > wrote:
> >> > > > > >
> >> > > > > > > On Mon, Aug 14, 2023 at 07:32:43PM +0600, Maxim Uvarov wrote:
> >> > > > > > > > Implement function for dhcp command with lwIP variant. Usage and
> >> > > > > output
> >> > > > > > > is
> >> > > > > > > > the same as the original command. This code called by
> >> > > compatibility
> >> > > > > code
> >> > > > > > > > between U-Boot and lwIP.
> >> > > > > > >
> >> > > > > > > Same as the dns command
> >> > > > > > >
> >> > > > > > > >
> >> > > > > > > > Signed-off-by: Maxim Uvarov <maxim.uvarov@linaro.org>
> >> > > > > > > > ---
> >> > > > > > > >  include/net/lwip.h             | 10 +++++++
> >> > > > > > > >  net/lwip/Makefile              |  1 +
> >> > > > > > > >  net/lwip/apps/dhcp/lwip-dhcp.c | 51
> >> > > > > ++++++++++++++++++++++++++++++++++
> >> > > > > > > >  3 files changed, 62 insertions(+)
> >> > > > > > > >  create mode 100644 net/lwip/apps/dhcp/lwip-dhcp.c
> >> > > > > > > >
> >> > > > > > > > diff --git a/include/net/lwip.h b/include/net/lwip.h
> >> > > > > > > > index c83b5c8231..2f035280eb 100644
> >> > > > > > > > --- a/include/net/lwip.h
> >> > > > > > > > +++ b/include/net/lwip.h
> >> > > > > > > > @@ -15,3 +15,13 @@ int do_lwip_dns(struct cmd_tbl *cmdtp, int
> >> > > flag,
> >> > > > > int
> >> > > > > > > argc,
> >> > > > > > > >  *         Other value < 0, if error
> >> > > > > > > >  */
> >> > > > > > > >  int ulwip_dns(char *name, char *varname);
> >> > > > > > > > +
> >> > > > > > > > +/*
> >> > > > > > > > +* This function creates the DHCP request to obtain IP address.
> >> > > If
> >> > > > > DHCP
> >> > > > > > > server
> >> > > > > > >
> >> > > > > > > Sphinx needs something more, please check the existing functions
> >> > > > > > >
> >> > > > > > > > +* returns file name, this file will be downloaded with tftp.
> >> > > After
> >> > > > > this
> >> > > > > > > > +* function you need to invoke the polling loop to process
> >> > > network
> >> > > > > > > communication.
> >> > > > > > > > +*
> >> > > > > > > > +* Return: 0 if success
> >> > > > > > > > +*         Other value < 0, if error
> >> > > > > > > > +*/
> >> > > > > > > > +int ulwip_dhcp(void);
> >> > > > > > > > diff --git a/net/lwip/Makefile b/net/lwip/Makefile
> >> > > > > > > > index 6d2c00605b..59323fb325 100644
> >> > > > > > > > --- a/net/lwip/Makefile
> >> > > > > > > > +
> >> > > > > > > > +static struct dhcp dhcp;
> >> > > > > > > > +
> >> > > > > > > > +static int ulwip_dhcp_tmo(void)
> >> > > > > > > > +{
> >> > > > > > > > +     switch (dhcp.state) {
> >> > > > > > > > +     case DHCP_STATE_BOUND:
> >> > > > > > > > +             env_set("bootfile", dhcp.boot_file_name);
> >> > > > > > > > +             env_set("ipaddr",
> >> > > ip4addr_ntoa(&dhcp.offered_ip_addr));
> >> > > > > > > > +             env_set("netmask",
> >> > > > > ip4addr_ntoa(&dhcp.offered_sn_mask));
> >> > > > > > > > +             env_set("serverip",
> >> > > > > ip4addr_ntoa(&dhcp.server_ip_addr));
> >> > > > > > > > +             printf("DHCP client bound to address %s\n",
> >> > > > > > > ip4addr_ntoa(&dhcp.offered_ip_addr));
> >> > > > > > > > +             break;
> >> > > > > > > > +     default:
> >> > > > > > > > +             return -1;
> >> > > > > > > > +     }
> >> > > > > > > > +
> >> > > > > > > > +     return 0;
> >> > > > > > > > +}
> >> > > > > > > > +
> >> > > > > > > > +int ulwip_dhcp(void)
> >> > > > > > > > +{
> >> > > > > > > > +     int err;
> >> > > > > > > > +     struct netif *netif;
> >> > > > > > > > +
> >> > > > > > > > +     ulwip_set_tmo(ulwip_dhcp_tmo);
> >> > > > > > > > +     netif = netif_get_by_index(1);
> >> > > > > > >
> >> > > > > > > What's (1)?
> >> > > > > > >
> >> > > > > > >
> >> > > > > > Only one lwip netif is registered. 1 - here is the index of netif. I
> >> > > > > don't
> >> > > > > > think that there is any definition for that...
> >> > > > > >
> >> > > > >
> >> > > > > And there's only ever going to be one interface (even if we have ipv4
> >> > > > > and ipv60 ? If so, define it to something please, thanks.
> >> > > > >
> >> > > > > --
> >> > > > > Tom
> >> > > > >
> >> > > >
> >> > > > Yes, one interface has 2 addresses ipv4 and ipv6.
> >> > >
> >> > > What about a device like a router which may have multiple wired
> >> > > ethernet interface?
> >> >
> >> > Yea, looks like we need lwip netif per U-Boots eth_devs. I did not yet
> >> > support several eth devices, but it will be good to add this to not break
> >> > existing functionality..
> >>
> >> The general case ends up being if we have more than one interface,
> >> ethact is what's used.  I'm unsure off-hand if some of the fancier
> >> networking-centric chipsets and devices have something more complex
> >> setup in their stacks.
> >>
> >> --
> >> Tom
> >
> >
> > Likely the bootloader uses the only one ethernet interface for loading. And it might
> > be enough to have an IP stack on top of the active eth device only. This is a model
> > that I am following in these patches.
> 
> It's possible, yes, that there may only be one active device, but you
> still will need to be able to handle all of them to at least work out
> which one has a link that you can bring up to get an IP. There's a
> bunch of devices that will have two interfaces with the same NIC
> driver, plus also there's at least a few devices that have an onboard
> switch (MV88E6XXX) although grep only shows a single one upstream.

Right.  You can even simulate this on some other devices via the gadget
ethernet driver.  In other words, please make sure we don't forget to
use "ethact" (see net/eth-uclass.c and net/eth_common.c).
diff mbox series

Patch

diff --git a/include/net/lwip.h b/include/net/lwip.h
index c83b5c8231..2f035280eb 100644
--- a/include/net/lwip.h
+++ b/include/net/lwip.h
@@ -15,3 +15,13 @@  int do_lwip_dns(struct cmd_tbl *cmdtp, int flag, int argc,
 *         Other value < 0, if error
 */
 int ulwip_dns(char *name, char *varname);
+
+/*
+* This function creates the DHCP request to obtain IP address. If DHCP server
+* returns file name, this file will be downloaded with tftp.  After this
+* function you need to invoke the polling loop to process network communication.
+*
+* Return: 0 if success
+*         Other value < 0, if error
+*/
+int ulwip_dhcp(void);
diff --git a/net/lwip/Makefile b/net/lwip/Makefile
index 6d2c00605b..59323fb325 100644
--- a/net/lwip/Makefile
+++ b/net/lwip/Makefile
@@ -65,4 +65,5 @@  obj-$(CONFIG_NET) += $(LWIPDIR)/netif/ethernet.o
 obj-$(CONFIG_NET) += port/if.o
 obj-$(CONFIG_NET) += port/sys-arch.o
 
+obj-$(CONFIG_CMD_DHCP) += apps/dhcp/lwip-dhcp.o
 obj-$(CONFIG_CMD_DNS) += apps/dns/lwip-dns.o
diff --git a/net/lwip/apps/dhcp/lwip-dhcp.c b/net/lwip/apps/dhcp/lwip-dhcp.c
new file mode 100644
index 0000000000..fb7431b248
--- /dev/null
+++ b/net/lwip/apps/dhcp/lwip-dhcp.c
@@ -0,0 +1,51 @@ 
+// SPDX-License-Identifier: GPL-2.0
+
+/*
+ * (C) Copyright 2023 Linaro Ltd. <maxim.uvarov@linaro.org>
+ */
+
+#include <common.h>
+#include <command.h>
+#include <console.h>
+
+#include <lwip/dhcp.h>
+#include <lwip/prot/dhcp.h>
+
+#include <net/ulwip.h>
+
+static struct dhcp dhcp;
+
+static int ulwip_dhcp_tmo(void)
+{
+	switch (dhcp.state) {
+	case DHCP_STATE_BOUND:
+		env_set("bootfile", dhcp.boot_file_name);
+		env_set("ipaddr", ip4addr_ntoa(&dhcp.offered_ip_addr));
+		env_set("netmask", ip4addr_ntoa(&dhcp.offered_sn_mask));
+		env_set("serverip", ip4addr_ntoa(&dhcp.server_ip_addr));
+		printf("DHCP client bound to address %s\n", ip4addr_ntoa(&dhcp.offered_ip_addr));
+		break;
+	default:
+		return -1;
+	}
+
+	return 0;
+}
+
+int ulwip_dhcp(void)
+{
+	int err;
+	struct netif *netif;
+
+	ulwip_set_tmo(ulwip_dhcp_tmo);
+	netif = netif_get_by_index(1);
+
+	if (!netif_get_client_data(netif, LWIP_NETIF_CLIENT_DATA_INDEX_DHCP))
+		dhcp_set_struct(netif, &dhcp);
+
+	err = dhcp_start(netif);
+	if (err)
+		printf("dhcp_start error %d\n", err);
+
+	return err;
+}