设置Linux套接字-高优先级有什么作用? [英] What is the effect of setting a linux socket - high priority?

查看:680
本文介绍了设置Linux套接字-高优先级有什么作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从linux套接字联机帮助页:

SO_PRIORITY
将所有数据包的协议定义优先级设置为 在此套接字上发送. Linux使用此值来订购网络 队列:优先级较高的数据包可能会先被处理 取决于所选设备的排队规则.

SO_PRIORITY
Set the protocol-defined priority for all packets to be sent on this socket. Linux uses this value to order the networking queues: packets with a higher priority may be processed first depending on the selected device queueing discipline.

这是使用以下方法设置的:

And this is set using:

int optval=7 // valid values are in the range [1,7]  
             // 1- low priority, 7 - high priority  
setsockopt(socket, SOL_SOCKET, SO_PRIORITY, &optval, optlen)   

并且说,该过程具有:
一种.来自socket_1-socket_10
的10个低优先级套接字(priority = 4) b. 1个高优先级套接字(优先级= 7)-socket_11

And say, the process has:
a. 10 low priority sockets (priority=4) from socket_1 - socket_10,
b. 1 high priority socket (priority=7) - socket_11

在以下情况下会发生什么:

What will happen in the following scenarios:

  1. send():进程在socket_1-socket_10&上发送多个消息在socket_11上,相对于通过socket_1-socket_10发送的msg,将优先考虑socket_11上的IMO msg.

  1. send(): process sends multiple msgs on socket_1-socket_10 & on socket_11, IMO msgs on the socket_11 will be given preference over the msgs being sent over socket_1-socket_10.

recv():如果在上述所有套接字上都接收到多用msg,则socket_11在通过socket_1-socket_10读取消息时是否获得更高的优先级?

recv(): if there are mutilple msgs being received at all the sockets described above, does socket_11 gets higher priority in reading the messages over socket_1-socket_10 ?

是否可以使用lsofnetstat等工具从命令行测量套接字优先级?

Is there a way to measure the socket priority from command line using tools such as lsof, netstat etc?

推荐答案

每个Linux网络接口都附有所谓的 qdisc (排队规则).问题的答案取决于所使用的qdisc.某些排队学科,例如 pfifo和bfifo ,没有优先权的概念.因此,如果使用它们,答案很简单-将没有优先级

Every Linux network interface has a so called qdisc (queuing discipline) attached to it. And the answer to your questions depends on the qdisc in use. Some queuing disciplines like pfifo and bfifo, have no concept of priority. So if they're used, the answer is simple - there will be no prioritization

但是,使用优先级较高的qdisc,例如 pfifo_fast (通常是Linux上的默认qdisc),套接字优先级可以起作用.

However, with a prioritizing qdisc such as pfifo_fast (which typically the default qdisc on Linux), the socket priority can have an effect.

此图描述了pfifo_fast qdisc中发生的事情:

This image describes what's going on in a pfifo_fast qdisc:

我们看到数据包根据它们的优先级放置在队列中.当接口发送下一个数据包的时间到了(实际上是 frame ,但让我们不要讨论)时,它将始终选择发送优先级最高的数据包.这意味着如果有多个数据包正在等待,则优先级最高的数据包将首先发送.请注意,这要求接口处于拥塞状态-如果接口不处于拥塞状态,并且数据包从OS到达后就立即发送,则不会进行排队,因此也就没有优先级.

We see that packets are placed in queues depending on their priority. When the time comes for the interface to send the next packet (frame actually, but let's not get into that), it will always choose to send the packet with the highest priority. That means that if multiple packets are waiting, those with the highest priority will be sent first. Note that this requires the interface to be congested - if the interface isn't congested and packets are sent as soon as they arrive from the OS, then there is no queuing and therefore no prioritization.

其他qdiscs具有不同的结构和策略.例如SFQ qdisc:

Other qdiscs have different structures and policies. For example an SFQ qdisc:

考虑到这一点,让我们回到您的问题:

With that in mind, let's get back to your questions:

  1. 取决于qdisc,是的,来自socket_11的数据包可以先于来自其他套接字的数据包发送.如果使用了pfifo_fast,并且如果socket_11发送了足够的流量以使出站网络接口饱和,则来自其他套接字的数据包甚至可能根本不会发送.在实践中这不太可能,因为通常很难在饱和某些其他资源之前先饱和网络接口,除非它是无线接口.

  1. Depending on the qdisc, yes, packets from socket_11 may be sent ahead of packets from other sockets. If pfifo_fast is used, and if socket_11 sends enough traffic to saturate the outbound network interface, then the packets from the other sockets might even not be sent at all. This is unlikely in practice since it's usually hard to saturate a network interface before saturating some other resource, unless it's a wireless interface.

数据包从机器的网络接口到套接字的路径比网络本身快得多.而且,正如您回想的那样,要使优先级发挥作用,必须存在拥塞.在典型的情况下,到达您的网络接口的数据包已经通过了它们在网络中传输的瓶颈,因此拥塞不太可能.

The path that packets take from the machine's network interface to the socket is much faster than the network itself. And, as you recall, for prioritization to have any effect, there has to be congestion. In a typical scenario, packets that reached as far as your network interface have already passed the bottleneck of their journey across the network, so congestion is unlikely.

您当然可以使用入口qdisc 其他机制来人为地创建瓶颈并确定传入流量的优先级.但是你为什么呢?只有在构建流量整形器或类似的网络设备时,这才有意义.另外,由于此qdiscs是一种低级机制,发生在高级套接字之下(甚至在桥接或路由之前),因此我怀疑套接字的优先级是否会对in产生任何影响.

You can of course use an ingress qdisc or other mechanisms to artificially create a bottleneck, and prioritize incoming traffic. But why would you? That only makes sense if you're building a traffic shaper or a similar network device. Plus, since this qdiscs are a low level mechanism that happens well below the higher level sockets (even before bridging or routing), I doubt that the socket's priority could have any effect on in.

不是我知道的,但我很高兴学习. 此内核模块已接近,但似乎无法显示优先级标志,只是常规的套接字选项.

Not that I'm aware of, but I'd be happy to learn. This kernel module comes close, but it doesn't seem to be able to show priority flags, just regular socket options.

这篇关于设置Linux套接字-高优先级有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆