Data sent through sockets are *sometimes* inverted ?!!!

Discussion in 'Linux Networking' started by mast4as, Feb 17, 2012.

  1. One reason may be that in a good design, the way such exceptions are
    handled depends on the frequency with which they occur.
    Nobody's arguing that it doesn't have to be handled correctly, but
    choosing an optimum way to handle it is easier if you know how often
    and under what circumstances it happens. If it happens on 80% of
    write() calls you handle it entirely diffently if it happens once or
    twice a decand and only when you've received a signal during the
    Grant Edwards, Feb 24, 2012
    1. Advertisements

  2. mast4as

    Rick Jones Guest

    Failure exactly how - return status of less than the send size, or
    return status of -1 and errno set? I wouldn't call the latter a
    partial write.
    In iptables what is the difference between REJECT and DROP? Doesn't
    the former actively send a TCP Reset segment?

    rick jones
    Rick Jones, Feb 24, 2012
    1. Advertisements

  3. mast4as

    Chris Davies Guest

    Good question. I had intended to provide some results to demonstrate
    that this really is a partial write we're talking about, but forgot.

    0: written 508 bytes of 508 available (OK)
    100: written 508 bytes of 508 available (OK)
    [...somewhere around here the network connection is blocked...]
    146: written 508 bytes of 508 available (OK)
    147: written 508 bytes of 508 available (OK)
    148: written 508 bytes of 508 available (OK)
    [ this point the local buffer fills and the client blocks...]
    149: written 284 bytes of 508 available (TRUNCATED)
    [...program ends...]

    Chris Davies, Feb 24, 2012
  4. mast4as

    Jorgen Grahn Guest

    No, REJECT sends an ICMP port unreachable. (And I think you know
    better than me what that does to TCP; "host unreachable" means another
    path may work later, but "port unreachable" in the middle of a session
    seems more final.)

    Jorgen Grahn, Feb 25, 2012
  5. mast4as

    Jorgen Grahn Guest

    The kernel logic is in tcp.c:tcp_sendmsg() ... but it's a large
    function, largely undocumented, with GOTOs back and forth. It would
    take me at least a day to understand it.

    It seems though that buffering happens on skbuf basis rather than
    octet basis, so I suppose that helps avoiding partial writes --
    whatever you write(), you're either blocked or your data is packaged
    and put on queue.

    More experiments with a blocking TCP socket showed that:
    - RST can trigger a partial write
    - a signal (SIGSTOP) can, too
    - I couldn't force a partial write in any other way, e.g. SIGSTOPping
    the peer
    - I couldn't force partial writes by writing much more than my MSS at
    a time, either (I'm on plain Ethernet, and tried writing 10k at a

    Experiments with a non-blocking TCP socket showed that:
    - if TCP can't keep up, you get plenty of partial writes,
    even if you write just 50 octets at a time

    I don't see the connection between what I saw in practice and
    tcp_sendmsg(), but I haven't really tried.

    I guess this boils down to, for blocking sockets, on a recent Linux:
    - you shouldn't optimize your code for the partial write case
    - you need to test that code path some other way, probably
    using signals

    Jorgen Grahn, Feb 25, 2012
  6. mast4as

    Chris Davies Guest

    And DROP simply discards the packet. I couldn't be bothered to wait for
    a TCP timeout, so I used REJECT.

    Chris Davies, Feb 26, 2012
  7. Hello,

    Jorgen Grahn a écrit :
    By default. But it as an option to send various other ICMP destination
    unreachable error codes, or TCP Reset.
    Pascal Hambourg, Feb 26, 2012
  8. mast4as

    Jorgen Grahn Guest

    True; I didn't bother to mention it -- there is a manpage, after all.
    I missed that in the manpage, though. Thanks!

    Jorgen Grahn, Feb 26, 2012
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.