Created
January 29, 2015 03:01
-
-
Save wenjianhn/0f7a9a1e36018a42515c to your computer and use it in GitHub Desktop.
Test setsockopt with SO_PRIORITY and IP_TOS. It shows that order matters.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <assert.h> | |
#include <netinet/ip.h> | |
#include <stdio.h> | |
#include <sys/socket.h> | |
#include <sys/types.h> | |
void test_setsockopt(int priority_first) | |
{ | |
int priority = 6; | |
int iptos = IPTOS_CLASS_CS6; | |
int fd = socket(AF_INET, SOCK_STREAM, 0); | |
if (priority_first){ | |
if(setsockopt(fd, SOL_SOCKET, SO_PRIORITY, &priority, | |
sizeof(priority)) < 0){ | |
printf("Oh no\n"); | |
} | |
} | |
if(setsockopt(fd, IPPROTO_IP, IP_TOS, &iptos, sizeof(iptos)) < 0){ | |
printf("Oh no\n"); | |
} | |
if (!priority_first){ | |
if(setsockopt(fd, SOL_SOCKET, SO_PRIORITY, &priority, | |
sizeof(priority)) < 0){ | |
printf("Oh no\n"); | |
} | |
} | |
int new_priority; | |
int optlen = sizeof(new_priority); | |
if (getsockopt(fd, SOL_SOCKET, SO_PRIORITY, &new_priority, &optlen) < 0){ | |
printf("Oh no\n"); | |
} | |
if (priority_first){ | |
printf("Priority is %d if setsockopt(SO_PRIORITY) *before* setsockopt(IP_TOS)\n", | |
new_priority); | |
} else { | |
printf("Priority is %d if setsockopt(SO_PRIORITY) *after* setsockopt(IP_TOS)\n", | |
new_priority); | |
} | |
close(fd); | |
} | |
int main(void) | |
{ | |
// background: http://lists.openwall.net/netdev/2009/12/21/59 | |
int priority_first = 0; | |
test_setsockopt(priority_first); | |
priority_first = 1; | |
test_setsockopt(priority_first); | |
return 0; | |
} |
So, I should set socket priority AFTER IP_TOS
value ? That is good to know, thanks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
No worries! :) If you look into the kernel
setsockopt
forIP_TOS
you'll see where it copies the values to theinet_sk(sk)->tos
andsk->sk_priority
fields. The corresponding TOS/TC values are stored in an arrayconst __u8 ip_tos2prio[16]
.