AsyncUDPSocket: Move to a well defined UDP size
authorGuido Trotter <ultrotter@google.com>
Mon, 14 Sep 2009 15:42:33 +0000 (16:42 +0100)
committerGuido Trotter <ultrotter@google.com>
Wed, 16 Sep 2009 12:55:54 +0000 (13:55 +0100)
Currently we read maximum 4K packets, and don't check packets when
sending them. With this patch we move to a well defined maximum size of
60K.

Signed-off-by: Guido Trotter <ultrotter@google.com>
Reviewed-by: Michael Hanselmann <hansmi@google.com>

lib/constants.py
lib/daemon.py
lib/errors.py

index fddb30c..1239948 100644 (file)
@@ -681,3 +681,10 @@ CONFD_CONFIG_RELOAD_TIMEOUT = 60
 # polling every RATELIMIT seconds, rather than relying on inotify, to be able
 # to serve more requests.
 CONFD_CONFIG_RELOAD_RATELIMIT = 2
+
+# Maximum UDP datagram size.
+# On IPv4: 64K - 20 (ip header size) - 8 (udp header size) = 65507
+# On IPv6: 64K - 40 (ip6 header size) - 8 (udp header size) = 65487
+#   (assuming we can't use jumbo frames)
+# We just set this to 60K, which should be enough
+MAX_UDP_DATA_SIZE = 61440
index 9467bb9..ac250cd 100644 (file)
@@ -95,7 +95,7 @@ class AsyncUDPSocket(asyncore.dispatcher):
   def handle_read(self):
     try:
       try:
-        payload, address = self.recvfrom(4096)
+        payload, address = self.recvfrom(constants.MAX_UDP_DATA_SIZE)
       except socket.error, err:
         if err.errno == errno.EINTR:
           # we got a signal while trying to read. no need to do anything,
@@ -148,6 +148,9 @@ class AsyncUDPSocket(asyncore.dispatcher):
     """Enqueue a datagram to be sent when possible
 
     """
+    if len(payload) > constants.MAX_UDP_DATA_SIZE:
+      raise errors.UdpDataSizeError('Packet too big: %s > %s' % (len(payload),
+                                    constants.MAX_UDP_DATA_SIZE))
     self._out_queue.append((ip, port, payload))
 
 
index ec154f7..663bc02 100644 (file)
@@ -297,6 +297,12 @@ class ConfdRequestError(GenericError):
   """
 
 
+class UdpDataSizeError(GenericError):
+  """UDP payload too big.
+
+  """
+
+
 # errors should be added above