Skip to content

Conversation

@Maihuanyi
Copy link
Contributor

@Maihuanyi Maihuanyi commented Oct 11, 2025

1.解决phy没有初始化成功,但网卡已存在的问题;

拉取/合并请求描述:(PR description)

[

  • 解决phy没有初始化成功,但网卡已存在的问题; 解决方法:当连接状态改变后,如果phy没有初始化成功便会再次初始化phy;

]

当前拉取/合并请求的状态 Intent for your PR

必须选择一项 Choose one (Mandatory):

  • 本拉取/合并请求是一个草稿版本 This PR is for a code-review and is intended to get feedback
  • 本拉取/合并请求是一个成熟版本 This PR is mature, and ready to be integrated into the repo

代码质量 Code Quality:

我在这个拉取/合并请求中已经考虑了 As part of this pull request, I've considered the following:

  • 已经仔细查看过代码改动的对比 Already check the difference between PR and old code
  • 代码风格正确,包括缩进空格,命名及其他风格 Style guide is adhered to, including spacing, naming and other styles
  • 没有垃圾代码,代码尽量精简,不包含#if 0代码,不包含已经被注释了的代码 All redundant code is removed and cleaned up
  • 所有变更均有原因及合理的,并且不会影响到其他软件组件代码或BSP All modifications are justified and not affect other components or BSP
  • 对难懂代码均提供对应的注释 I've commented appropriately where code is tricky
  • 代码是高质量的 Code in this PR is of high quality
  • 已经使用formatting 等源码格式化工具确保格式符合RT-Thread代码规范 This PR complies with RT-Thread code specification
  • 如果是新增bsp, 已经添加ci检查到.github/ALL_BSP_COMPILE.json 详细请参考链接BSP自查

    1.修复网卡在在注册时phy初始化失败,当连接状态改变后重新初始化phy;
@github-actions
Copy link

👋 感谢您对 RT-Thread 的贡献!Thank you for your contribution to RT-Thread!

为确保代码符合 RT-Thread 的编码规范,请在你的仓库中执行以下步骤运行代码格式化工作流(如果格式化CI运行失败)。
To ensure your code complies with RT-Thread's coding style, please run the code formatting workflow by following the steps below (If the formatting of CI fails to run).


🛠 操作步骤 | Steps

  1. 前往 Actions 页面 | Go to the Actions page
    点击进入工作流 → | Click to open workflow →

  2. 点击 Run workflow | Click Run workflow

  • 设置需排除的文件/目录(目录请以"/"结尾)
    Set files/directories to exclude (directories should end with "/")
  • 将目标分支设置为 \ Set the target branch to:dev_lwip
  • 设置PR number为 \ Set the PR number to:10793
  1. 等待工作流完成 | Wait for the workflow to complete
    格式化后的代码将自动推送至你的分支。
    The formatted code will be automatically pushed to your branch.

完成后,提交将自动更新至 dev_lwip 分支,关联的 Pull Request 也会同步更新。
Once completed, commits will be pushed to the dev_lwip branch automatically, and the related Pull Request will be updated.

如有问题欢迎联系我们,再次感谢您的贡献!💐
If you have any questions, feel free to reach out. Thanks again for your contribution!

@github-actions
Copy link

github-actions bot commented Oct 11, 2025

📌 Code Review Assignment

🏷️ Tag: components

Reviewers: Maihuanyi

Changed Files (Click to expand)
  • components/net/lwip/port/ethernetif.c

📊 Current Review Status (Last Updated: 2025-10-11 17:21 CST)

  • Maihuanyi Pending Review

📝 Review Instructions

  1. 维护者可以通过单击此处来刷新审查状态: 🔄 刷新状态
    Maintainers can refresh the review status by clicking here: 🔄 Refresh Status

  2. 确认审核通过后评论 LGTM/lgtm
    Comment LGTM/lgtm after confirming approval

  3. PR合并前需至少一位维护者确认
    PR must be confirmed by at least one maintainer before merging

ℹ️ 刷新CI状态操作需要具备仓库写入权限。
ℹ️ Refresh CI status operation requires repository Write permission.

@Rbb666 Rbb666 requested review from Copilot and kurisaW October 11, 2025 09:08
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes an issue where network interface initialization failures during device registration are now handled by re-attempting PHY initialization when the link status changes.

Key changes:

  • Removes error returns from device initialization/open failures, allowing the system to continue operation
  • Adds re-initialization logic in eth_device_linkchange() to retry device initialization when link comes up
  • Adds debug print statements to help identify initialization failures

if (up == RT_TRUE)
{
/* @note Check whether the eth device was successfully initialized, otherwise re-initialize */
if (!(device->flag & RT_DEVICE_FLAG_ACTIVATED))
Copy link

Copilot AI Oct 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Variable 'device' is used but not declared in this scope. In the #else block (line 897), the device variable is referenced but not defined, unlike in the #if block where it's properly declared on line 861. / 变量 'device' 在此作用域中被使用但未声明。在 #else 块(第897行)中,device 变量被引用但未定义,不像在 #if 块中在第861行正确声明。

Copilot uses AI. Check for mistakes.
Comment on lines 859 to 863
struct eth_device* enetif;
enetif = (struct eth_device*)dev->netif->state;
rt_device_t device = (rt_device_t)(&(enetif->parent));


Copy link

Copilot AI Oct 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The variable declarations and assignments can be simplified and made more readable. Consider declaring and initializing variables in a single line where possible, and the extra empty line on 862 is unnecessary. / 变量声明和赋值可以简化并提高可读性。建议在可能的情况下在单行中声明和初始化变量,第862行的额外空行是不必要的。

Suggested change
struct eth_device* enetif;
enetif = (struct eth_device*)dev->netif->state;
rt_device_t device = (rt_device_t)(&(enetif->parent));
struct eth_device* enetif = (struct eth_device*)dev->netif->state;
rt_device_t device = (rt_device_t)(&(enetif->parent));

Copilot uses AI. Check for mistakes.
Comment on lines 500 to +506
}
if (rt_device_open(device, RT_DEVICE_FLAG_RDWR) != RT_EOK)
else
{
return ERR_IF;
if (rt_device_open(device, RT_DEVICE_FLAG_RDWR) != RT_EOK)
{
rt_kprintf("eth device open failed!\n");
}
Copy link

Copilot AI Oct 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing the error return (ERR_IF) from device initialization failure changes the function's behavior significantly. The caller will not know that initialization failed, which could lead to undefined behavior when trying to use an uninitialized device. Consider keeping the error return or adding alternative error handling. / 从设备初始化失败中移除错误返回(ERR_IF)显著改变了函数的行为。调用者将不知道初始化失败,这可能在尝试使用未初始化的设备时导致未定义行为。建议保留错误返回或添加替代错误处理。

Copilot uses AI. Check for mistakes.
return ERR_IF;
if (rt_device_open(device, RT_DEVICE_FLAG_RDWR) != RT_EOK)
{
rt_kprintf("eth device open failed!\n");
Copy link

Copilot AI Oct 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to the initialization case, removing the error return from device open failure removes important error information from the caller. The function should indicate failure when critical operations like device opening fail. / 与初始化情况类似,从设备打开失败中移除错误返回会从调用者那里移除重要的错误信息。当设备打开等关键操作失败时,函数应该指示失败。

Suggested change
rt_kprintf("eth device open failed!\n");
rt_kprintf("eth device open failed!\n");
return ERR_IF;

Copilot uses AI. Check for mistakes.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Maihuanyi 这块不需要错误返回?

@kurisaW
Copy link
Member

kurisaW commented Oct 11, 2025

问一下你说的“如果没有插入网线会超时(没有初始化phy成功)”具体是指什么意思?能否给个说明场景和现象截图

@Maihuanyi
Copy link
Contributor Author

如果板子在没有插入网线的情况下,注册网卡时会检测网线是否插入,超时后就不会打开eth外设的接口,比如eth的DMA等配置;你可以看看bsp/at32/libraries/rt_drivers/drv_emac.c的emac_speed_config函数;当连接状态改变时网口没有正真初始化的,此时工作时不正常的;

@Maihuanyi
Copy link
Contributor Author

LGTM

@Maihuanyi Maihuanyi mentioned this pull request Oct 13, 2025
10 tasks
@Maihuanyi
Copy link
Contributor Author

Maihuanyi commented Oct 13, 2025 via email

@Rbb666
Copy link
Member

Rbb666 commented Oct 13, 2025

如果板子在没有插入网线的情况下,注册网卡时会检测网线是否插入,超时后就不会打开eth外设的接口,比如eth的DMA等配置;你可以看看bsp/at32/libraries/rt_drivers/drv_emac.c的emac_speed_config函数;当连接状态改变时网口没有正真初始化的,此时工作时不正常的;

不太理解 “phy初始化失败” 是什么意思?phy 的初始化都是通过 MDC/MDIO 进行初始化的吧,这块如果出现了初始化失败理论上应该由驱动层进行错误处理的。

我认为这个PR应该修复的是给 netifapi_netif_add 添加正确的返回值处理,通过 eth_device_init->eth_device_init_with_flag 来返回正确的错误信息,从而在驱动层进行判断处理网卡注册是否成功:

netifapi_netif_add(netif, &ipaddr, &netmask, &gw, dev, eth_netif_device_init, tcpip_input);

@Rbb666
Copy link
Member

Rbb666 commented Oct 13, 2025

另外,PR的标题需要规范下,需要说清楚这个PR解决了什么问题

@Maihuanyi
Copy link
Contributor Author

Maihuanyi commented Oct 13, 2025

我有考虑过返回netifapi_netif_add的错误码的情况,如果返回netifapi_netif_add的错误码,移除网卡设备,显示注册失败?当连接状态改变后后在重新注册一遍;

  • 但需要更改stm32和at32等的网卡驱动,涉及的文件比较多的;
  • 感觉在设计网卡上就有的问题了,主要还是为了兼容之前的网卡驱动

@Rbb666
Copy link
Member

Rbb666 commented Oct 13, 2025

我有考虑过返回netifapi_netif_add的错误码的情况,如果返回netifapi_netif_add的错误码,移除网卡设备,显示注册失败?当连接状态改变后后在重新注册一遍;

  • 但需要更改stm32和at32等的网卡驱动,涉及的文件比较多的;
  • 感觉在设计网卡上就有的问题了,主要还是为了兼容之前的网卡驱动

调用顺序是这样的:eth_device_init ->eth_device_init_with_flag

eth_device_init 在驱动层初始化失败后,就说明网卡初始化失败了,那么就需要在驱动层进行重新的初始化,也就不涉及到后续连接状态改变

@Maihuanyi
Copy link
Contributor Author

没有提交PR前的eth_device_init_with_flag没有返回eth_device_init的错误码的(没有判断netifapi_netif_add的返回);
原则上eth_device_init ->eth_device_init_with_flag要返回eth_device_init层的错误码,驱动层当连接状态再判读是否注册了网卡,否则重新注册一次;
但涉及的驱动层文件比较多的,需要有人员去跟进;

@Maihuanyi
Copy link
Contributor Author

目前我知道的驱动层需要更改的有STM32 、at32、wch->riscv的;应该还有其他的文件需要改的;

@Rbb666
Copy link
Member

Rbb666 commented Oct 13, 2025

没有提交PR前的eth_device_init_with_flag没有返回eth_device_init的错误码的(没有判断netifapi_netif_add的返回); 原则上eth_device_init ->eth_device_init_with_flag要返回eth_device_init层的错误码,驱动层当连接状态再判读是否注册了网卡,否则重新注册一次; 但涉及的驱动层文件比较多的,需要有人员去跟进;

这个问题不大,可以

没有提交PR前的eth_device_init_with_flag没有返回eth_device_init的错误码的(没有判断netifapi_netif_add的返回); 原则上eth_device_init ->eth_device_init_with_flag要返回eth_device_init层的错误码,驱动层当连接状态再判读是否注册了网卡,否则重新注册一次; 但涉及的驱动层文件比较多的,需要有人员去跟进;

这个问题不大,这个PR可以先添加 netifapi_netif_add 的返回值;

其他BSP暂时不改也不影响驱动正常使用的

@Maihuanyi
Copy link
Contributor Author

Maihuanyi commented Oct 13, 2025 via email

@Rbb666
Copy link
Member

Rbb666 commented Oct 13, 2025

if (RT_EOK!= netifapi_netif_add(netif, &ipaddr, &netmask, &gw, dev, eth_netif_device_init, tcpip_input) ) { #ifdef RT_USING_NETDEV netdev_del(netif); #endif rt_device_close(&(dev->parent)); rt_device_unregister(&(dev->parent)); rt_free(netif); } 要删除netdev,不插入网线启动跑起来会崩溃(dma没有开启,调用eth的rx、tx回调就不死机的。

---- 回复的原邮件 ---- | 发件人 | @.> | | 日期 | 2025年10月13日 17:55 | | 收件人 | @.> | | 抄送至 | @.>@.> | | 主题 | Re: [RT-Thread/rt-thread] [componentss][net][lwip][port] (PR #10793) | Rbb666 left a comment (RT-Thread/rt-thread#10793) 没有提交PR前的eth_device_init_with_flag没有返回eth_device_init的错误码的(没有判断netifapi_netif_add的返回); 原则上eth_device_init ->eth_device_init_with_flag要返回eth_device_init层的错误码,驱动层当连接状态再判读是否注册了网卡,否则重新注册一次; 但涉及的驱动层文件比较多的,需要有人员去跟进; 这个问题不大,可以 没有提交PR前的eth_device_init_with_flag没有返回eth_device_init的错误码的(没有判断netifapi_netif_add的返回); 原则上eth_device_init ->eth_device_init_with_flag要返回eth_device_init层的错误码,驱动层当连接状态再判读是否注册了网卡,否则重新注册一次; 但涉及的驱动层文件比较多的,需要有人员去跟进; 这个问题不大,这个PR可以先添加 netifapi_netif_add 的返回值; 其他BSP暂时不改也不影响驱动正常使用的 — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you were mentioned.Message ID: @.***>

netifapi_netif_add(netif, &ipaddr, &netmask, &gw, dev, eth_netif_device_init, tcpip_input);

修改成:

return netifapi_netif_add(netif, &ipaddr, &netmask, &gw, dev, eth_netif_device_init, tcpip_input);

就可以了吧 ==

@Maihuanyi
Copy link
Contributor Author

Maihuanyi commented Oct 13, 2025 via email

@Maihuanyi
Copy link
Contributor Author

Maihuanyi commented Oct 13, 2025 via email

@Maihuanyi Maihuanyi closed this Oct 14, 2025
@Maihuanyi
Copy link
Contributor Author

#10810 @Rbb666
帮忙审核一下这个PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants