lwip-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[lwip-users] Sporadic TCP checksum error


From: Bettega Stefano
Subject: [lwip-users] Sporadic TCP checksum error
Date: Fri, 19 Mar 2010 16:33:09 +0100

Hi,
I'm using a LM3S9862 EV board with uC/OS-II 2.86 and lwIP 1.3.2.

I have some question about lwIP usage.

I have a console task that creates a communication task. The communication
task creates a socket in listen mode (lwip_socket/lwip_bind/lwip_listen),
then stops waiting for a connection (lwip_accept). If a connection is
created, every 50ms this task checks two global buffers (incoming and
outgoing) for characters to be processed. The outgoing queue is filled by a
putch() function, that is used by fprintf(stdout, ...) functions (so I have
a console to write on), while the incoming buffer is read by a getch()
function (so I can process fscanf(stdin ...)). fprintf and fscanf functions
are processed into console task, that acts as telnet interface without using
any lwIP function. The core of the function executed every 50ms is the
following:

        if (m_nSocket == -1)
                return;

        if (m_nClient == -1)
        {
                struct sockaddr_in client_addr;
                int addrlen = sizeof(client_addr);
                m_nClient = lwip_accept(m_nSocket, (struct 
sockaddr*)&client_addr,
(socklen_t*)&addrlen);
        }

        const int nbytesrx = lwip_recv(m_nClient, m_txbuf, TXBUFLEN, 
MSG_DONTWAIT);
        if (nbytesrx == 0)
        {
                // If cannot read due to socket closed by remote party, quit 
processing
                lwip_close(m_nClient);
                m_nClient = -1;
                return;
        }
        else if (nbytesrx > 0)
                ConsoleRx(m_txbuf, nbytesrx);

        // Time to send our data. Collect it into local buffer...
        const int nbytestx = ConsoleTx(m_txbuf, TXBUFLEN);

        if (nbytestx)
        {
                // ... then reformat buffer changing simple \n into \n\r to 
suit telnet
window style
                int npos = 0, nnewlen = nbytestx;
                for (int i=0; i<nbytestx; i++)
                {
                        m_txbufcrlf[npos++] = m_txbuf[i];
                        if (m_txbuf[i] == '\n')
                        {
                                m_txbufcrlf[npos++] = '\r';
                                nnewlen++;
                        }
                }

                if (lwip_send(m_nClient, m_txbufcrlf, nnewlen, MSG_MORE) < 0)
                {
                        lwip_close(m_nClient);
                        m_nClient = -1;
                }
        }

m_txbuf and m_txbufcrlf are two heap-allocated buffer, the former being
sized TXBUFLEN and the latter being sized 2*TXBUFLEN, where TXBUFLEN = 16. I
have to translate \n into \n\r for Windows to correctly display strings
using provided telnet application.
ConsoleTx and ConsoleRx are two non blocking functions that exchanges data
with the two low level global buffer in which getch() and putch() works.

First question: I have to add another listener on which a different
communication protocol is to be used (no telnet, no ftp, but rather a
proprietary protocol). Do I have to use the same communication thread to
handle the lwip_accept/lwip_recv/lwip_send sequence? If I correctly
understood lwIP is not to be used concurrently by multiple threads, so I
have to create a task wrapping access to network layer by myself.

Second question: if I put the following code in the 50ms function

        for (int i=0;i<TXBUFLEN; i++)
                m_txbuf[i] = (i < 10) ? ('0'+ i) : ('A' + i - 10);

        if (lwip_send(m_nClient, m_txbuf, TXBUFLEN, 0) < 0)
        {
                lwip_close(m_nClient);
                m_nClient = -1;
        }

I can see received packet on the PC coming up with no delay and no problem,
except sometimes communication seems to freeze, but suddenly it starts
again. Wireshark runned on another PC (connected to an hub with my board and
my test PC) reports no problem in the communication.

But If I use the real 50ms function core (reported above), sometimes the
communication fails. Wireshark complains about TCP checksum error, and it
seems my PC wont' ACK any other message. As the checksum is hardware
calculated when a new packet is put into TX FIFO, I wonder if writing
misaligned data could lead to wrong checksum calculation. LM3S9862 can
automatically pad transmitted data to properly align frame, and this option
is enabled, but do you think this misalignment can cause the problem
reported above?

Thanks in advance

Stefano





reply via email to

[Prev in Thread] Current Thread [Next in Thread]