본문 바로가기

Language/python

SO_RCVBUF, SO_SNDBUF의 Maximum/Minimum 측정 파이썬 소스

OS의 종류마다 소켓에 지정할 수 있는 SO_RCVBUF와 SO_SNDBUF의 크기가 기본값도 다를 뿐만 아니라, 최대값/최소값도 다르다. 파이썬 스크립트로 기본값/최대값/최소값을 측정할 수 있는 프로그램을 작성하였다.

Windows XP(32bit) 결과값

Tcp.RcvBuf.Def : 8192
Tcp.RcvBuf.Max : 2147483647
Tcp.RcvBuf.Min : 0
Tcp.SndBuf.Def : 8192
Tcp.SndBuf.Max : 2147483647
Tcp.SndBuf.Min : 0
Udp.RcvBuf.Def : 8192
Udp.RcvBuf.Max : 2147483647
Udp.RcvBuf.Min : 0
Udp.SndBuf.Def : 8192
Udp.SndBuf.Max : 2147483647
Udp.SndBuf.Min : 0

Portable Ubuntu 9.04(32bit) 결과값

Tcp.RcvBuf.Def : 87380
Tcp.RcvBuf.Max : 215040
Tcp.RcvBuf.Min : 256
Tcp.SndBuf.Def : 16384
Tcp.SndBuf.Max : 215040
Tcp.SndBuf.Min : 2048
Udp.RcvBuf.Def : 107520
Udp.RcvBuf.Max : 215040
Udp.RcvBuf.Min : 256
Udp.SndBuf.Def : 107520
Udp.SndBuf.Max : 215040
Udp.SndBuf.Min : 2048


SocketBufferChecker.py
import socket

MAX_BUFF_SIZE = 0x7FFFFFFF
MIN_BUFF_SIZE = 0

def checkSocketBuffer():
    reportDict = {}
    # check TCP Buffer
    checkTcpSocketBuffer(reportDict)
   
    # check UDP Buffer
    checkUdpSocketBuffer(reportDict)
   
    # report
    printReport(reportDict)

def checkTcpSocketBuffer(reportDict):
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
   
    # default SO_RCVBUF
    rcvbuf_size = s.getsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF)
    reportDict["Tcp.RcvBuf.Def"] = rcvbuf_size
   
    # check MAX SO_RCVBUF
    s.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, MAX_BUFF_SIZE)
    rcvbuf_size = s.getsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF)
    reportDict["Tcp.RcvBuf.Max"] = rcvbuf_size
   
    # check MIN SO_RCVBUF
    s.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, MIN_BUFF_SIZE)
    rcvbuf_size = s.getsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF)
    reportDict["Tcp.RcvBuf.Min"] = rcvbuf_size
   
    # default SO_SNDBUF
    sndbuf_size = s.getsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF)
    reportDict["Tcp.SndBuf.Def"] = sndbuf_size
   
    # check MAX SO_SNDBUF
    s.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, MAX_BUFF_SIZE)
    sndbuf_size = s.getsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF)
    reportDict["Tcp.SndBuf.Max"] = sndbuf_size
   
    # check MIN SO_SNDBUF
    s.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, MIN_BUFF_SIZE)
    sndbuf_size = s.getsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF)
    reportDict["Tcp.SndBuf.Min"] = sndbuf_size
   
    s.close()

def checkUdpSocketBuffer(reportDict):
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
   
    # default SO_RCVBUF
    rcvbuf_size = s.getsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF)
    reportDict["Udp.RcvBuf.Def"] = rcvbuf_size
   
    # check MAX SO_RCVBUF
    s.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, MAX_BUFF_SIZE)
    rcvbuf_size = s.getsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF)
    reportDict["Udp.RcvBuf.Max"] = rcvbuf_size
   
    # check MIN SO_RCVBUF
    s.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, MIN_BUFF_SIZE)
    rcvbuf_size = s.getsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF)
    reportDict["Udp.RcvBuf.Min"] = rcvbuf_size
   
    # default SO_SNDBUF
    sndbuf_size = s.getsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF)
    reportDict["Udp.SndBuf.Def"] = sndbuf_size
   
    # check MAX SO_SNDBUF
    s.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, MAX_BUFF_SIZE)
    sndbuf_size = s.getsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF)
    reportDict["Udp.SndBuf.Max"] = sndbuf_size
   
    # check MIN SO_SNDBUF
    s.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, MIN_BUFF_SIZE)
    sndbuf_size = s.getsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF)
    reportDict["Udp.SndBuf.Min"] = sndbuf_size
   
    s.close()

def printReport(reportDict):
    key_list = reportDict.keys()
    key_list.sort()
       
    for key in key_list:
        print "%s : %d" % (key, reportDict[key])

if __name__ == "__main__":
    checkSocketBuffer()