Location>code7788 >text

Seeing the big picture - counting network stack memory usage with ss tools

Popularity:479 ℃/2024-12-19 20:47:59

preamble

This paper describes the use ofsstool to count the current memory usage of the network stack

environmental preparation

subassemblies releases
operating system Ubuntu 22.04.4 LTS

To view socket memory-related parameters, the-m parameters

▶ ss -tm
State       Recv-Q Send-Q                                                                   Local Address:Port                                                                                    Peer Address:Port

ESTAB       0      36                                                                         10.8.19.109:ssh                                                                                   182.148.53.161:25924
         skmem:(r0,rb369280,t0,tb133632,f1792,w2304,o0,bl0,d1)

among others, aboutskmem

skmem:(r<rmem_alloc>,rb<rcv_buf>,t<wmem_alloc>,tb<snd_buf>,
              f<fwd_alloc>,w<wmem_queued>,o<opt_mem>,
              bl<back_log>,d<sock_drop>)
  • rmem_alloc Memory has been allocated for receiving packets, which is essentially 0 if the application layer is able to consume the data received by the TCP kernel layer in a timely manner
  • rcv_buf The total memory that can be used by the receiving packet, as compared to thenet.ipv4.tcp_rmem The second parameter is relevant
  • wmem_alloc Memory has been allocated for sending packets (has reached the ip layer), the packet has reached the ip layer and you need to wait for the network card to pick up the packet from the ip layer
  • snd_buf The total amount of memory that can be used to send a packet is the same as thenet.ipv4.tcp_wmem The second parameter is relevant
  • fwd_alloc Memory already allocated for socket use, not for receive/send use
  • wmem_queued Memory already allocated for sending packets (not reaching the ip layer)
  • opt_mem Used to hold the memory used by the socket option
  • back_log back_logMemory used by the process to get new packets from the queue
  • sock_drop Number of packets dropped before demultiplexing them to the socket

Calculating the memory consumed by each socket, the-tm

socket_memory = r(rmem_alloc) + t(wmem_alloc) + f(fwd_alloc) + w(wmem_queued) + o(opt_mem) + bl(back_log)
▶ ss -tm
State       Recv-Q Send-Q                                                                   Local Address:Port                                                                                    Peer Address:Port

ESTAB       0      36                                                                         10.8.19.109:ssh                                                                                   182.148.53.161:25924
         skmem:(r0,rb369280,t0,tb133632,f1792,w2304,o0,bl0,d1)

The memory consumed by this socket can be calculated:mem = 1792+2304 = 4096 4k RAM

Calculate the memory used by the network stack

cat /proc/net/sockstat
sockets: used 502
TCP: inuse 65 orphan 0 tw 128 alloc 311 mem 21
UDP: inuse 5 mem 4
UDPLITE: inuse 0
RAW: inuse 0
FRAG: inuse 0 memory 0

mem 21The unit is page. Check the size of the page.

getconf PAGESIZE
4096

From this, it follows that the current tcp network stack uses approximatelymem = 21*4096 = 86016 84k RAM

To view information about other parameters of the protocol stack, the-i

▶ ss -ti
State       Recv-Q Send-Q                                                                   Local Address:Port                                                                                    Peer Address:Port
ESTAB       0      0                                                                        192.168.3.168:46099                                                                                  192.168.3.168:58625
         cubic wscale:2,7 rto:201 rtt:0.043/0.005 ato:40 mss:65483 rcvmss:1924 advmss:65483 cwnd:10 bytes_acked:12424915507 bytes_received:3150 segs_out:10395577 segs_in:10395575 send 121828.8Mbps lastsnd:2188 lastrcv:2244390049 lastack:2188 rcv_rtt:1 rcv_space:43690
  • rto Retransmission timeout, i.e., counting from the moment the data is sent, retransmission will be performed if the time exceeds this time, the unit is ms.
  • rtt Round-trip time of a connection, i.e., the difference between the moment when the data is sent and the moment when the acknowledgement is received, in ms
  • ato Timeout time for ack in ms
  • mss The maximum segmentation of valid data, removing layers 3 and 4 of the header, is the MSSMTU - (TCP header + IP header) = MSS
  • rcvmss This parameter is not described in the documentation and is labeled on the source code comments:MSS used for delayed ACK decisions. The opposite end tells this end that the size of the opposite end's mss
  • advmss This parameter is not described in the documentation and is labeled on the source code comments:Advertised MSS. Tell the opposite end, the size of the mss on the home end
  • cwnd The size of the congestion window:cwnd*mss
  • bytes_acked Number of bytes that have been acked
  • bytes_received Number of bytes received but not acked
  • segs_out Number of segments sent, the largest segment is an mss, but it can be less than an mss (sent without filling an mss)
  • segs_in Number of segments received
  • send transmission rate
  • lastsnd Time since the last packet was sent, in ms
  • lastrcv Time since last received packet, in ms
  • lastack Time since last received ack, in ms
  • rcv_rtt This parameter is not described in the documentation
  • rcv_space Used for tcp autotuning, used to adjust sender's buffer

Contact me

  • Contact me for an in-depth chat


This concludes this article
I'm not very knowledgeable, so if there is any soup leakage, please do not hesitate to give me advice...