Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an API to get the netdev list #8860

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions components/net/netdev/src/netdev.c
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,51 @@ int netdev_family_get(struct netdev *netdev)
return ((struct sal_proto_family *)netdev->sal_user_data)->family;
}

#if defined(SAL_USING_AF_NETLINK)
int netdev_getnetdev(struct msg_buf *msg, int (*cb)(struct msg_buf *m_buf, struct netdev *nd, int nd_num, int index, int ipvx))
{
struct netdev *cur_nd_list = netdev_list;
struct netdev *nd_node;
struct netdev nd;
rt_base_t level;
int index = 0;
int nd_num = 0;
int err = 0;

if (cur_nd_list == RT_NULL)
return 0;

level = rt_spin_lock_irqsave(&_spinlock);
Copy link
Member

Choose a reason for hiding this comment

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

可以想想,怎么来降低spin_lock_irqsave的时间吗?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

如果只是计算netdev_list的长度,可以的,只需要定义一个原子类型的变量,在每次注册或者删除netdev的时候进行原子操作+1或者-1就可以了

nd_num = rt_slist_len(&cur_nd_list->list) + 1;
rt_spin_unlock_irqrestore(&_spinlock, level);

rt_memcpy(&nd, cur_nd_list, sizeof(struct netdev));
err = cb(msg, &nd, nd_num, index, ROUTE_IPV4_TRUE);
Copy link
Contributor

Choose a reason for hiding this comment

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

没开 NETLINK 的时应该没有这个宏定义,这么处理会编译失败。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

这个函数加了宏控,不会编译失败的,并且cb是传过来的回调函数,即使没有宏控,编译也不应该会失败的才对

Copy link
Contributor

Choose a reason for hiding this comment

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

是指 ROUTE_IPV4_TRUE

if (err < 0)
return err;

index ++;

level = rt_spin_lock_irqsave(&_spinlock);
rt_slist_for_each_entry(nd_node, &(cur_nd_list->list), list)
{
rt_memcpy(&nd, nd_node, sizeof(struct netdev));
rt_spin_unlock_irqrestore(&_spinlock, level);
err = cb(msg, &nd, nd_num, index, ROUTE_IPV4_TRUE);
if (err < 0)
{
return err;
}

index ++;
level = rt_spin_lock_irqsave(&_spinlock);
}
rt_spin_unlock_irqrestore(&_spinlock, level);

return 0;
}
#endif

#endif /* RT_USING_SAL */

/**
Expand Down
Loading