场景:两个线程在同一个套接字上发送UDP,而线程1想要设置与线程2不同的diffserv / QoS类。我们先前通过在互斥锁中包装sendto()调用并执行setsockopt()来解决这个问题。 sendto()之前和之后的适当QoS类(然后解锁互斥锁)。
我们在极少数情况下(由于与信号的交互)在使用此解决方案时出现死锁/挂起,我的问题是 - 如果我们将所需的QoS类作为辅助数据发送到sendmsg中,我们是否可以完全删除互斥锁()打电话?为了澄清,sendmsg()是原子的,所以数据报将使用正确的QoS类发送,并且不会有来自线程1和2的sendmsg()调用相互干扰的风险吗?
我在SO上发现了类似的问题,所以我知道一个套接字上的一个UDP数据报的正常sendmsg()是' atomic',但问题是整个呼叫是否包括临时改变的QoS比特套接字是用户空间线程看到的原子?
Linux内核中的相关补丁是这样的:
commit aa6615814533c634190019ee3a5b10490026d545
Author: Francesco Fusco <ffusco@redhat.com>
Date: Tue Sep 24 15:43:09 2013 +0200
ipv4: processing ancillary IP_TOS or IP_TTL
If IP_TOS or IP_TTL are specified as ancillary data, then sendmsg() sends out
packets with the specified TTL or TOS overriding the socket values specified
with the traditional setsockopt().
The struct inet_cork stores the values of TOS, TTL and priority that are
passed through the struct ipcm_cookie. If there are user-specified TOS
(tos != -1) or TTL (ttl != 0) in the struct ipcm_cookie, these values are
used to override the per-socket values. In case of TOS also the priority
is changed accordingly.
Two helper functions get_rttos and get_rtconn_flags are defined to take
into account the presence of a user specified TOS value when computing
RT_TOS and RT_CONN_FLAGS.
Signed-off-by: Francesco Fusco <ffusco@redhat.com>
Signed-off-by: David S. Miller <davem@davemloft.net>