socket-util: introduce tos_to_priority()

This maps from TOS, which can be used for setsockopt(IPPROTO_IP, IP_TOS),
to socket priority, which can be used for setsockopt(SOL_SOCKET, SO_PRIORITY).

With this, we can set priority like the following:
```
uint8_t tos = IPTOS_CLASS_CS6;
setsockopt_int(fd, IPPROTO_IP, IP_TOS, tos);
setsockopt_int(fd, SOL_SOCKET, SO_PRIORITY, tos_to_priority(tos));
```

Co-authored with Google Gemini.
This commit is contained in:
Yu Watanabe
2026-05-03 10:17:20 +01:00
committed by Luca Boccassi
parent 2cfaf50cc8
commit fa9b3b26c7
3 changed files with 49 additions and 0 deletions
+20
View File
@@ -2,6 +2,8 @@
#include <fcntl.h>
#include <grp.h>
#include <linux/pkt_sched.h>
#include <netinet/ip.h>
#include <unistd.h>
#include "alloc-util.h"
@@ -530,4 +532,22 @@ TEST(getpeerpidref) {
ASSERT_TRUE(!pidref_equal(&pidref0, &pidref_pid1));
}
TEST(tos_to_priority) {
ASSERT_EQ(tos_to_priority(IPTOS_CLASS_CS7), TC_PRIO_CONTROL);
ASSERT_EQ(tos_to_priority(IPTOS_CLASS_CS6), TC_PRIO_CONTROL);
ASSERT_EQ(tos_to_priority(IPTOS_CLASS_CS5), TC_PRIO_INTERACTIVE);
ASSERT_EQ(tos_to_priority(IPTOS_CLASS_CS4), TC_PRIO_INTERACTIVE);
ASSERT_EQ(tos_to_priority(IPTOS_CLASS_CS3), TC_PRIO_INTERACTIVE_BULK);
ASSERT_EQ(tos_to_priority(IPTOS_CLASS_CS2), TC_PRIO_INTERACTIVE_BULK);
ASSERT_EQ(tos_to_priority(IPTOS_CLASS_CS1), TC_PRIO_BULK);
ASSERT_EQ(tos_to_priority(IPTOS_CLASS_CS0), TC_PRIO_BESTEFFORT);
/* check if lower bits are correctly filtered. */
ASSERT_EQ(tos_to_priority(IPTOS_CLASS_CS7 | IPTOS_LOWDELAY), TC_PRIO_CONTROL);
ASSERT_EQ(tos_to_priority(IPTOS_CLASS_CS1 | IPTOS_LOWCOST), TC_PRIO_BULK);
ASSERT_EQ(tos_to_priority(0x00), TC_PRIO_BESTEFFORT);
ASSERT_EQ(tos_to_priority(0xff), TC_PRIO_CONTROL);
}
DEFINE_TEST_MAIN(LOG_DEBUG);