Re: /proc/net/tcp6 inconsistent line length
From: Eric Dumazet <hidden>
Date: 2012-01-17 15:13:21
Le mardi 17 janvier 2012 à 16:14 +0000, abirvalg@lavabit.com a écrit :
Thank you, David. I first fread() the whole file to membuf and then process it thusly trying to find a line that matches my source port (porthex) :
while(1){
memcpy(buffer,&tcp6_membuf[184+170*i],4);
if ( !memcmp ( porthex, buffer, 4 ) ){ //match!
goto endloop;
}
i++;
continue;Wow... no comment on this code.
N.B. 184 is the offset of source port on line 2 from the beginning of file (line 1 is used for column headings) my /proc/net/tcp6 file contains 300 - 500 lines and I parse it at least from 4 to 10 times per sec. So the maximum is 5000 iterations per sec. It would be very costly CPU wise to do strchr(buf, '\n') I'm not skillful enough to understand the code in kernel sources. My concern boils down to this: Was /proc/net/tcp* meant to have an equal length of each line? If yes, then I discovered a bug. If no, then would it be possible to patch the kernel to enable an equal length on each line?
netlink can give you the needed information much faster.
Take a look at what is done by :
"ss -emoi src :22"
State Recv-Q Send-Q Local Address:Port
Peer Address:Port
ESTAB 0 0 192.168.20.108:22
10.37.168.20:39444 timer:(keepalive,115min,0) ino:6852
sk:ffff88011ca81900
mem:(r0,w0,f8192,t0) ts sack cubic wscale:4,4 rto:208 rtt:8/11 ato:51
cwnd:10 ssthresh:24 send 14.5Mbps rcv_rtt:15 rcv_space:14480
sendmsg(3, {msg_name(12)={sa_family=AF_NETLINK, pid=0, groups=00000000},
msg_iov(3)=[{"`\0\0\0\22\0\1\3@\342\1\0\0\0\0\0\2\0\0\17\0\0\0\0\0\0\0\0
\0\0\0\0"..., 76}, {"\24\0\1\0", 4}, {"\7\20\24\0\0\0\0\0\26\0\0\0\0\0\0
\0", 16}], msg_controllen=0, msg_flags=0}, 0) = 96
recvmsg(3, {msg_name(12)={sa_family=AF_NETLINK, pid=0, groups=00000000},
msg_iov(1)=[{"\344\0\0\0\22\0\2\0@\342\1\0l\f\0\0\2\1\1\0\0\26\232\24
\300\250\24l320 "..., 8192}], msg_controllen=0, msg_flags=0}, 0) = 228
One sendmsg() for the request, one recvmsg() for the answer (with _all_
details included), even if your /proc/net/tcp*** has 1.000.000 lines.
Really say no to /proc files.