Skip to content

examples/lwip_ipv4: add example for LWIP IPv4 client/server#21519

Merged
maribu merged 5 commits intoRIOT-OS:masterfrom
krzysztof-cabaj:examples-lwip_ipv4
Dec 23, 2025
Merged

examples/lwip_ipv4: add example for LWIP IPv4 client/server#21519
maribu merged 5 commits intoRIOT-OS:masterfrom
krzysztof-cabaj:examples-lwip_ipv4

Conversation

@krzysztof-cabaj
Copy link
Contributor

@krzysztof-cabaj krzysztof-cabaj commented May 31, 2025

Contribution description

This PR adds new example which shows how to implement LWIP IPv4 client and server.

Moreover, this example can be used in CI to check, if new code do not break LWIP IPv4.
In the previous few months at least two problems with LWIP IPv4 are detected manually,
and they should be detected by CI - see PR #21316 or PR #21342.

Testing procedure

Check client and server commands as described in the README.md.

Issues/PRs references

None

@krzysztof-cabaj krzysztof-cabaj requested a review from jia200x as a code owner May 31, 2025 16:45
@github-actions github-actions bot added Area: doc Area: Documentation Area: examples Area: Example Applications labels May 31, 2025
@crasbe crasbe added Type: enhancement The issue suggests enhanceable parts / The PR enhances parts of the codebase / documentation CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR labels Jun 3, 2025
@riot-ci
Copy link

riot-ci commented Jun 3, 2025

Murdock results

✔️ PASSED

33365ca examples/lwip_ipv4: enable DHCP and server autostart

Success Failures Total Runtime
17 0 17 01m:31s

Artifacts

@krzysztof-cabaj
Copy link
Contributor Author

I try to generate makefile.ci automatically using sudo make -C ./examples/networking/misc/lwip_ipv4 generate-Makefile.ci
but after around 1h and occupied 5GB of disk it still stuck at acd52832 board.

@crasbe could you add label CI: no fast fail so I could generate makefile.ci manually.

@crasbe
Copy link
Contributor

crasbe commented Jun 3, 2025

I'll run it on my work computer tomorrow, then you can address the suggestions without triggering a full rebuild on every fixup.

@crasbe
Copy link
Contributor

crasbe commented Jun 4, 2025

This is the result for generate-Makefile.ci for your example:

BOARD_INSUFFICIENT_MEMORY := \
    bluepill-stm32f030c8 \
    i-nucleo-lrwan1 \
    nucleo-c031c6 \
    nucleo-f030r8 \
    nucleo-f031k6 \
    nucleo-f042k6 \
    nucleo-l011k4 \
    nucleo-l031k6 \
    nucleo-l053r8 \
    samd10-xmini \
    slstk3400a \
    stk3200 \
    stm32f030f4-demo \
    stm32f0discovery \
    stm32g0316-disco \
    stm32l0538-disco \
    weact-g030f6 \
    #

@krzysztof-cabaj
Copy link
Contributor Author

@crasbe Thanks for Makefile.ci generation.

@krzysztof-cabaj krzysztof-cabaj force-pushed the examples-lwip_ipv4 branch 3 times, most recently from 829cea9 to 867808b Compare June 7, 2025 16:00
Comment on lines +86 to +88
mutex_lock(&server_mutex);
server_running = 0;
mutex_unlock(&server_mutex);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you need that for?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My idea is that if due to some errors this thread ends - we could start server once again.
I tested this situation with similar code after sock_udp_recv function (lines 98-105). When RIOT receives UDP datagram longer than 64 bytes (buffer size) it ends with error, but I could restart server and later everything works well.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But why the need for a mutex?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This variable is changed by other thread (CLI thread in server command). Also I leave this in the example as example of synchronization. Yes, I known that probability of hazards or race is minimal.

I could leave this, remove mutex (in my opinion all should works fine, only in very rare case user have to issue server command once again, after error message that thread is started) or rewrite code to use _Atomic and atomic_compare_exchange_strong.

@krzysztof-cabaj krzysztof-cabaj force-pushed the examples-lwip_ipv4 branch 2 times, most recently from ea3ad62 to 9a20829 Compare June 13, 2025 10:24
@krzysztof-cabaj
Copy link
Contributor Author

All remarks from review fixed.
CI is green.

@krzysztof-cabaj
Copy link
Contributor Author

Can me move this PR a little forward?

}

if (data_len != (size_t)sock_udp_send(&sock, argv[3], data_len, NULL)) {
printf("Error sock_udp_send!\n");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You likely want to print the error.

Copy link
Contributor Author

@krzysztof-cabaj krzysztof-cabaj Jun 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I want to print error.
Your comment concerns wrong condition? You suggest add only condition for <0?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sock_udp_send() gives you an error code that can tell the user why the send failed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. Thanks for hint!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I add printing error code to error message. Is that OK?

Comment on lines +86 to +88
mutex_lock(&server_mutex);
server_running = 0;
mutex_unlock(&server_mutex);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But why the need for a mutex?

@krzysztof-cabaj krzysztof-cabaj force-pushed the examples-lwip_ipv4 branch 3 times, most recently from 85936e5 to eeb5403 Compare June 20, 2025 17:45
@krzysztof-cabaj
Copy link
Contributor Author

Can me move this PR little forward?

@krzysztof-cabaj
Copy link
Contributor Author

@benpicco I fix most of your remarks and have questions to one or too.

Could you review the code once again and could we move this PR forward?

@krzysztof-cabaj
Copy link
Contributor Author

Beginning of new week ... good time to finish this PR :)

@benpicco - can be move it little bit forward?

@benpicco
Copy link
Contributor

benpicco commented Dec 17, 2025

Huh how did you get there?
Do you have a 'Submit Review' button on the Changes view?

Copy link
Contributor Author

@krzysztof-cabaj krzysztof-cabaj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should show all my pending comments.

Comment on lines +86 to +88
mutex_lock(&server_mutex);
server_running = 0;
mutex_unlock(&server_mutex);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This variable is changed by other thread (CLI thread in server command). Also I leave this in the example as example of synchronization. Yes, I known that probability of hazards or race is minimal.

I could leave this, remove mutex (in my opinion all should works fine, only in very rare case user have to issue server command once again, after error message that thread is started) or rewrite code to use _Atomic and atomic_compare_exchange_strong.

}

if (data_len != (size_t)sock_udp_send(&sock, argv[3], data_len, NULL)) {
printf("Error sock_udp_send!\n");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. Thanks for hint!

}

if (data_len != (size_t)sock_udp_send(&sock, argv[3], data_len, NULL)) {
printf("Error sock_udp_send!\n");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I add printing error code to error message. Is that OK?

@krzysztof-cabaj
Copy link
Contributor Author

@benpicco thanks for spotting the problem and showing solution. I don't now why some of my comments are not send - I'm almost certain that I post all comments in this same manner.

I see that some of the comments are pending from June :(

I hope this allows us finalizing this PR without problems - and answers the question why you do not see my comments.

@krzysztof-cabaj
Copy link
Contributor Author

@benpicco I try to fix all comments. If I missed something please let me known.

I leave two issues, which are not changed:

  1. mutex - I still would like to leave this. We have two thread - CLI and server, which read and write this same variable server_running. In my opinion this needs synchronization.
  2. Ending the server when too long packet is received. This is some kind of feature which allows clear closing of the server and restarting it with new port. If you agree with such solution I could add clear explanation in the README.md file.

@krzysztof-cabaj
Copy link
Contributor Author

I simplify the example. Server thread is started automatically, and cannot be stopped.
This removes need for synchronization (usage of server_mutex variable) and CLI command for server start.
The README.md is updated accordingly.

Copy link
Contributor

@benpicco benpicco left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, that's much better.
An example shouldn't come with too many bells and whistles that will just confuse people.

@benpicco benpicco enabled auto-merge December 22, 2025 09:16
@benpicco benpicco added CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR and removed CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR labels Dec 22, 2025
@benpicco benpicco added this pull request to the merge queue Dec 22, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Dec 22, 2025
@krzysztof-cabaj
Copy link
Contributor Author

Murdock complains about small ROM and RAM for stm32c0116-dk.

I add this board to the Makefile.ci ... and also add stm32c0316-dk - this board has this same amount of RAM and ROM.

@krzysztof-cabaj
Copy link
Contributor Author

Could we add this PR to merge queue once again ...
... I hope this time everything goes smooth.

@maribu maribu added this pull request to the merge queue Dec 23, 2025
Merged via the queue into RIOT-OS:master with commit 7ba922f Dec 23, 2025
25 checks passed
@maribu
Copy link
Member

maribu commented Dec 23, 2025

Thx for adding this example ❤️

@krzysztof-cabaj
Copy link
Contributor Author

@crasbe, @benpicco and @maribu thanks for support with my longest, and most commented PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: doc Area: Documentation Area: examples Area: Example Applications CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR Type: enhancement The issue suggests enhanceable parts / The PR enhances parts of the codebase / documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants