[Openvpn-devel,05/21] Document frame related function and variables a bit more

Message ID 20211207170211.3275837-6-arne@rfc2549.org
State Accepted
Headers show
Series Big buffer/frame refactoring patch set | expand

Commit Message

Arne Schwabe Dec. 7, 2021, 6:01 a.m. UTC
Signed-off-by: Arne Schwabe <arne@rfc2549.org>
---
 src/openvpn/mtu.h | 22 ++++++++++++++++++++--
 1 file changed, 20 insertions(+), 2 deletions(-)

Comments

Frank Lichtenheld Dec. 10, 2021, 5:10 a.m. UTC | #1
Acked-by: Frank Lichtenheld <frank@lichtenheld.com>

> Arne Schwabe <arne@rfc2549.org> hat am 07.12.2021 18:01 geschrieben:
> @@ -246,6 +262,8 @@ static inline int
>  frame_headroom(const struct frame *f)
>  {
>      const int offset = FRAME_HEADROOM_BASE(f);
> +    /* These two lines just pad offset to next multiple of PAYLOAD_ALIGN in
> +     * a complicated and confusing way */
>      const int delta = ((PAYLOAD_ALIGN << 24) - offset) & (PAYLOAD_ALIGN - 1);

The 24 is completely arbitrary, right? As long as the result is always bigger than
offset, you can use any other number.
I wonder whether it might be possible to replace it with something slightly less
confusing (IMHO, anyway), like 

delta = (PAYLOAD_ALIGN - (offset % PAYLOAD_ALIGN)) & (PAYLOAD_ALIGN - 1);

But my C-foo is not strong enough to judge potential corner cases.

Regards,
  Frank

--
Frank Lichtenheld
Gert Doering Dec. 13, 2021, 9:34 p.m. UTC | #2
Not tested in any way, as this is just comments, and Frank has reviewed
them.  I hope we'll see --tun-mtu-extra die later in this series... :-)

Your patch has been applied to the master branch.

commit 66c05aeabc7218e58266bd2bb39ddbd040030328
Author: Arne Schwabe
Date:   Tue Dec 7 18:01:55 2021 +0100

     Document frame related function and variables a bit more

     Signed-off-by: Arne Schwabe <arne@rfc2549.org>
     Acked-by: Frank Lichtenheld <frank@lichtenheld.com>
     Message-Id: <20211207170211.3275837-6-arne@rfc2549.org>
     URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg23332.html
     Signed-off-by: Gert Doering <gert@greenie.muc.de>


--
kind regards,

Gert Doering

Patch

diff --git a/src/openvpn/mtu.h b/src/openvpn/mtu.h
index 72a9e515b..c1148c317 100644
--- a/src/openvpn/mtu.h
+++ b/src/openvpn/mtu.h
@@ -28,7 +28,7 @@ 
 
 /*
  *
- * Packet maninipulation routes such as encrypt, decrypt, compress, decompress
+ * Packet manipulation routes such as encrypt, decrypt, compress, decompress
  * are passed a frame buffer that looks like this:
  *
  *    [extra_frame bytes] [mtu bytes] [extra_frame_bytes] [compression overflow bytes]
@@ -117,7 +117,12 @@  struct frame {
     int extra_tun;              /**< Maximum number of bytes in excess of
                                  *   the tun/tap MTU that might be read
                                  *   from or written to the virtual
-                                 *   tun/tap network interface. */
+                                 *   tun/tap network interface.
+                                 *
+                                 *   Only set with the option --tun-mtu-extra
+                                 *   which defaults to 0 for tun and 32
+                                 *   (\c TAP_MTU_EXTRA_DEFAULT) for tap.
+                                 *   */
 
     int extra_link;             /**< Maximum number of bytes in excess of
                                  *   external network interface's MTU that
@@ -177,11 +182,22 @@  struct options;
  * Control buffer headroom allocations to allow for efficient prepending.
  */
 #define FRAME_HEADROOM_BASE(f)     (TUN_LINK_DELTA(f) + (f)->extra_buffer + (f)->extra_link)
+/* Same as FRAME_HEADROOM_BASE but rounded up to next multiple of PAYLOAD_ALIGN */
 #define FRAME_HEADROOM(f)          frame_headroom(f)
 
 /*
  * Max size of a buffer used to build a packet for output to
  * the TCP/UDP port.
+ *
+ * the FRAME_HEADROOM_BASE(f) * 2 should not be necessary but it looks that at
+ * some point in the past we seem to have lost the information what parts of
+ * the extra space we need to have before the data and which we need after
+ * the data. So we ensure we have the FRAME_HEADROOM before and after the
+ * actual data.
+ *
+ * Most of our code only prepends headers but compression needs the extra bytes
+ * *after* the data as compressed data might end up larger than the original
+ * data (and max compression overhead is part of extra_buffer)
  */
 #define BUF_SIZE(f)              (TUN_MTU_SIZE(f) + FRAME_HEADROOM_BASE(f) * 2)
 
@@ -246,6 +262,8 @@  static inline int
 frame_headroom(const struct frame *f)
 {
     const int offset = FRAME_HEADROOM_BASE(f);
+    /* These two lines just pad offset to next multiple of PAYLOAD_ALIGN in
+     * a complicated and confusing way */
     const int delta = ((PAYLOAD_ALIGN << 24) - offset) & (PAYLOAD_ALIGN - 1);
     return offset + delta;
 }