raspberrypi / pico-examples

BSD 3-Clause "New" or "Revised" License
2.84k stars 820 forks source link

lwIP http request takes too long at altcp_connect() in TLS example #411

Open svetlollc opened 1 year ago

svetlollc commented 1 year ago

Problem

I successfully build and run a TLS client Example (picow_tls_client_background executable) from the GitHub, however a simple http GET request takes about 20 seconds. It does not actually depend on the server response: I tested server, that responds empty string at all, and server with about few kb output - no matter, it takes about 20 seconds to make a request.

As the result of debug, I understand, that 99% of the request time takes altcp_connect- before that, it takes several miliseconds, and after that, starting from the callback, i.e. tls_client_connected, it takes about millisecond until the request is made:

Pico W turned on -- [few ms]  -- altcp_connect() -- [19 sec] -- tls_client_connected() -- [1 ms] -- I see comleted incoming request on the server

Tried to solve

I made a reserch, I found that such behaviour, can be caused by lwIP configuration in lwipopts.h. It already sounds weird, because it probably should been tested by Raspberry Pi developers, who posted the code on GitHub.

So I set #define MEMP_SANITY_CHECK 0, set

#define LWIP_DEBUG 0 
#define ALTCP_MBEDTLS_DEBUG  LWIP_DBG_OFF

I also tried to reduce TCP_WND or\and TCP_MSS, but any signficant changes produced cmake errors, that something overflows something. However, I reduced a bit that values, without errors at building.

However, all of that actions did not change request time - it still the same.

svetlollc commented 1 year ago

I also tried (although, as I wroted, this step is passed fast) to connect to ip directly, instead of DNS resolving by hostname, and it still takes same long.

svetlollc commented 1 year ago

Also, in the code, I tried to set big (45) as well as small (1-5) values of TLS_CLIENT_TIMEOUT_SECS - it does not affect anything

svetlollc commented 1 year ago

picow_tls_client_poll executable makes ~20 sec HTTP request as well. Changing delay value in cyw43_arch_wait_for_work_until(make_timeout_time_ms(1000)); does not solve the problem

svetlollc commented 1 year ago

I also tried to call altcp_nagle_disable, when setup callbacks, tried to define TPC_NODELAY, I also tried another Pico W board — no changes at all.

Can someone at least tell do they have or not same experience? Maybe it just how things works?

svetlollc commented 1 year ago

So, in case of other people have at least few seconds long requests, after weeks of searching, I have only few possible issues:

1) In some examples, I saw that people somewhere defines country. In the Pico W examples, country doesn’t specify, maybe it is the reason. 2) Some examples call ntp_time_init() and get_ntp_time() — I do not know what it is, but maybe it relates to the problem, when you try to open some site on pc with wrong date, and browser doesn’t open it with error, that the clock is late. 3) Some examples call functions that setup mask, ip-adress, and similar stuff, as understand Pico W itself. 4) Low power supply, although I tried to connect Pico W to different sources: laptop usb, just a phone usb charger, other. 5) This how things works - it is not a full computer, it has only 133 MHz clock, so it works slow (hope this is wrong, at least because I saw, how Pico can output some graphics on VGA in with 60 fps, and do other relatively heave work).

svetlollc commented 1 year ago

Today I finally managed to make debug work, here is what Pico W outputs (nothing special):

resolving <hostIspecified>
DNS resolving complete
connecting to server IP <123.456.7.8(ip that host pointing to)> port 443
connected to server, sending request
***
new data received from server:
***

HTTP/1.1 200 OK
Content-Length: 0
Content-Type: text/html; charset=UTF-8
Date: Tue, 22 Aug 2023 16:11:54 GMT
Server: Apache
X-Powered-By: PHP/7.0.32
Connection: close

connection was closed gracefully
connection closed
Test passed
All done

So, of course 99% of time (about 18 seconds) takes connecting to server IP <123.456.7.8(ip that host pointing to)> port 443 line

svetlollc commented 1 year ago

Today I decided to run python, instead of C, to test http request on the Pico W, and it works fine (takes few seconds).

So, it is definetely me doing somehing wrong.

Can someone please at least point the direction, where should look to?