简体   繁体   English

线程中的 netlink 套接字:nl_pid 的值

[英]netlink socket in threads: value for nl_pid

I'm trying to use netlink sockets in a pthread .我正在尝试在pthread中使用netlink sockets 。 I googled some examples and most of them use the following pattern to identify a "local" end of the socket:我用谷歌搜索了一些例子,其中大部分使用以下模式来识别套接字的“本地”端:

struct sockaddr_nl  local = {};
local.nl_family = AF_NETLINK;
local.nl_pid = pthread_self() << 16 | getpid();
...
ret = bind(fd, (struct sockaddr*)&local, sizeof(local));
...

As I understand, normally when a.netlink socket is opened per process, nl_pid is set to 0 and the kernel would pick the right portid.据我了解,通常当每个进程打开 a.netlink 套接字时,nl_pid 设置为 0,kernel 会选择正确的 portid。 However it is not true for.netlink-per-thread case, where PID value will be shared among all threads.然而,对于 .netlink-per-thread 情况并非如此,其中PID值将在所有线程之间共享。

Besides I think pthread_self() << 16 | getpid()此外我认为pthread_self() << 16 | getpid() pthread_self() << 16 | getpid() is not portable. pthread_self() << 16 | getpid()不可移植。 What is the right way to set nl_pid in this case?在这种情况下设置 nl_pid 的正确方法是什么?

As I understand, normally when a.netlink socket is opened per process, nl_pid is set to 0 and the kernel would pick the right portid.据我了解,通常当每个进程打开 a.netlink 套接字时,nl_pid 设置为 0,kernel 会选择正确的 portid。 However it is not true for.netlink-per-thread case, where PID value will be shared among all threads.然而,对于 .netlink-per-thread 情况并非如此,其中 PID 值将在所有线程之间共享。

There is no "the right" portid.没有“正确的”portid。 Identification of portids with process ids is a convention, consistent with the kernel's default behavior when it assigns the portid, not a rule.使用进程 ID 标识 portid 是一种约定,与内核在分配 portid 时的默认行为一致,而不是规则。 Applications, whether single- or multi-threaded, are free to request arbitrary port ids within the allowed range of port ids.应用程序,无论是单线程还是多线程,都可以在允许的端口 ID 范围内自由请求任意端口 ID。

Moreover, when a process, whether single- or multi-threaded, binds multiple.netlink sockets and delegates portid selection to the kernel, the kernel can be relied upon to assign a unique portid to each.此外,当一个进程(无论是单线程还是多线程)绑定多个 .netlink sockets 并将 portid 选择委托给 kernel 时,可以依赖 kernel 为每个进程分配一个唯一的 portid。

Besides I think pthread_self() << 16 | getpid()此外我认为pthread_self() << 16 | getpid() pthread_self() << 16 | getpid() is not portable. pthread_self() << 16 | getpid()不可移植。

You are correct.你是对的。 For that to have well-defined behavior in the general case, pthread_t , the return type of pthread_self() , would need to be an unsigned integer type, but POSIX does not require it to be even an integer, signedness notwithstanding.为了在一般情况下具有明确定义的行为, pthread_tpthread_self()的返回类型,需要是无符号的 integer 类型,但 POSIX 甚至不要求它是 integer,尽管有符号。

Netlink is Linux-specific, and that does limit the scope you need to care about. Netlink 是特定于 Linux 的,它确实限制了您需要关心的 scope。 Still, there have been more than one pthreads implementation for Linux, there may be new ones in the future, and because this detail is unspecified it is conceivable that implementations will change the type between versions.尽管如此,对于 Linux 已经有不止一个 pthreads 实现,未来可能会有新的实现,并且由于这个细节未指定,可以想象实现将改变版本之间的类型。

What is the right way to set nl_pid in this case?在这种情况下设置 nl_pid 的正确方法是什么?

I would say that the best option for choosing.netlink portids is almost always to let the kernel assign them, as you concluded in comments.我会说选择 .netlink portids 的最佳选择几乎总是让 kernel 分配它们,正如您在评论中总结的那样。 Including in single-threaded applications.包括在单线程应用程序中。 This is not a question of the process's file descriptor table, however, because portids are not file descriptors.然而,这不是进程的文件描述符表的问题,因为 portid 不是文件描述符。 In particular, unlike file descriptors, portids are system-wide.特别是,与文件描述符不同,portid 是系统范围的。 This is all the more reason to let the kernel choose.这更是让kernel选择的理由。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM