Skip to content

samples: net: IPv6 and IP family coexistence fixes for download and udp samples - #31381

Open
simonduq wants to merge 5 commits into
nrfconnect:mainfrom
simonduq:ipfamily-fixes-rebased
Open

simonduq wants to merge 5 commits into
nrfconnect:mainfrom
simonduq:ipfamily-fixes-rebased

Conversation

@simonduq

Copy link
Copy Markdown
Contributor

Fixes IP family management for both the download and udp samples.

UDP:

  • Straightforward addition of code that was missing for IPv6 support. Select IP family at runtime based on the supplied IP address then proceed as before.

Download:

  • Wait for v4/v6 events specifically in order to know what connectivity we have available
  • Run the downloader lib with v4, v6, or both, depending on availability
  • Add retries in such a way that if the device gets only one IP initially and fails its download, it has other chances in case the other family comes up

Tested on nRF7120dk.

Logs for the download sample under different conditions:

A. Device has two IPs but IPv6 server is not reachable

IPv4 connectivity established
Network connected
IPv6 connectivity established
Starting download over IPv6/IPv4
Downloading https://nrfconnectsdk.s3.eu-central-1.amazonaws.com/sample-img-100kb.png
[00:00:13.085,540] <inf> downloader: Host lookup failed for hostname nrfconnectsdk.s3.eu-central-1.amazonaws.com on IPv6 (err -118), attempting IPv4
[00:00:13.109,710] <inf> downloader: Setting up TLS credentials, sec tag count 1
[00:00:13.130,798] <inf> downloader: Connecting to 3.5.135.83
[00:00:14.539,306] <inf> downloader: Downloaded 2048/102923 bytes (1%)
[00:00:14.541,290] <inf> downloader: Downloaded 4096/102923 bytes (3%)
[00:00:14.543,243] <inf> downloader: Downloaded 6144/102923 bytes (5%)
[00:00:14.545,196] <inf> downloader: Downloaded 8192/102923 bytes (7%)

B. Device has only IPv4

Starting download over IPv4
Downloading https://nrfconnectsdk.s3.eu-central-1.amazonaws.com/sample-img-100kb.png
[00:00:07.968,139] <inf> downloader: Setting up TLS credentials, sec tag count 1
IPv6 connectivity established
[00:00:07.989,624] <inf> downloader: Connecting to 3.5.136.252
[00:00:09.655,670] <inf> downloader: Downloaded 2048/102923 bytes (1%)
[00:00:09.657,623] <inf> downloader: Downloaded 4096/102923 bytes (3%)
[00:00:09.659,576] <inf> downloader: Downloaded 6144/102923 bytes (5%)
[00:00:09.661,529] <inf> downloader: Downloaded 8192/102923 bytes (7%)

C. Device gets IPv6 first with server not available, then only later gets IPv4

IPv6 connectivity established
Network connected
Starting download over IPv6
Downloading https://nrfconnectsdk.s3.eu-central-1.amazonaws.com/sample-img-100kb.png
[00:00:08.059,722] <err> downloader: Host lookup failed for hostname nrfconnectsdk.s3.eu-central-1.amazonaws.com, err -118
Error -118 during download
Download canceled
Starting download over IPv6
Downloading https://nrfconnectsdk.s3.eu-central-1.amazonaws.com/sample-img-100kb.png
[00:00:09.082,641] <err> downloader: Host lookup failed for hostname nrfconnectsdk.s3.eu-central-1.amazonaws.com, err -118
Error -118 during download
Download canceled
Starting download over IPv6
Downloading https://nrfconnectsdk.s3.eu-central-1.amazonaws.com/sample-img-100kb.png
[00:00:11.105,194] <err> downloader: Host lookup failed for hostname nrfconnectsdk.s3.eu-central-1.amazonaws.com, err -118
Error -118 during download
Download canceled
[00:00:11.643,554] <inf> net_dhcpv4: Received: 10.42.0.138
IPv4 connectivity established
Starting download over IPv6/IPv4
Downloading https://nrfconnectsdk.s3.eu-central-1.amazonaws.com/sample-img-100kb.png
[00:00:15.121,307] <inf> downloader: Host lookup failed for hostname nrfconnectsdk.s3.eu-central-1.amazonaws.com on IPv6 (err -118), attempting IPv4
[00:00:15.138,885] <inf> downloader: Setting up TLS credentials, sec tag count 1
[00:00:15.160,003] <inf> downloader: Connecting to 3.5.139.149
[00:00:16.549,652] <inf> downloader: Downloaded 2048/102923 bytes (1%)
[00:00:16.551,666] <inf> downloader: Downloaded 4096/102923 bytes (3%)
[00:00:16.553,619] <inf> downloader: Downloaded 6144/102923 bytes (5%)
[00:00:16.555,572] <inf> downloader: Downloaded 8192/102923 bytes (7%)
[00:00:16.557,525] <inf> downloader: Downloaded 10240/102923 bytes (9%)

D. Functioning end-to-end IPv6

IPv6 connectivity established                                                                                                                             
Network connected                                                                                                                                         
Starting download over IPv6                                                                                                                               
Downloading http://nrfconnectsdk.s3.dualstack.eu-central-1.amazonaws.com/sample-img-100kb.png                                                             
[00:00:14.267,730] <inf> downloader: Connecting to 2a05:d030:4000:40::305:8825                                                                            
[00:00:14.559,570] <inf> downloader: Downloaded 682/102923 bytes (0%)                                                                                     
[00:00:14.607,940] <inf> downloader: Downloaded 1364/102923 bytes (1%)                                                                                    
[00:00:14.609,100] <inf> downloader: Downloaded 2046/102923 bytes (1%)                                                                                    
[00:00:14.667,785] <inf> downloader: Downloaded 2728/102923 bytes (2%)                                                                                    
[00:00:14.683,044] <inf> downloader: Downloaded 3410/102923 bytes (3%)                                                                                    
[00:00:14.736,724] <inf> downloader: Downloaded 4092/102923 bytes (3%)                                                                                    
[00:00:14.739,288] <inf> downloader: Downloaded 4774/102923 bytes (4%)                                                                                    
[00:00:14.786,468] <inf> downloader: Downloaded 5456/102923 bytes (5%)                                                                                    
[00:00:14.789,306] <inf> downloader: Downloaded 6138/102923 bytes (5%)                                                                                    
[00:00:14.836,364] <inf> downloader: Downloaded 6820/102923 bytes (6%)                                                                                    
[00:00:14.839,202] <inf> downloader: Downloaded 7502/102923 bytes (7%)                                                                                    
[00:00:14.888,153] <inf> downloader: Downloaded 8184/102923 bytes (7%)                                                                                    
[00:00:14.893,646] <inf> downloader: Downloaded 8866/102923 bytes (8%)                  

Tickets:

Copilot AI lite review requested due to automatic review settings September 16, 2026 15:11
@simonduq
simonduq requested review from a team as code owners September 16, 2026 15:11
@NordicBuilder NordicBuilder added doc-required PR must not be merged without tech writer approval. and removed changelog labels Sep 16, 2026
@NordicBuilder
NordicBuilder requested a review from a team September 16, 2026 15:12
@NordicBuilder

NordicBuilder commented Sep 16, 2026

Copy link
Copy Markdown
Contributor

CI Information

To view the history of this post, click the 'edited' button above
Build number: 9

Inputs:

Sources:

sdk-nrf: PR head: 4ef631a3654d4c88f29a997ec8e9224bad5e1d24

more details

sdk-nrf:

PR head: 4ef631a3654d4c88f29a997ec8e9224bad5e1d24
merge base: bbf55119c2d85583a242bb6ea03965517706be7a
target head (main): 6a1349080dc72ce0567e7849108679a0455d3902
Diff

Github labels

Enabled Name Description
ci-disabled Disable the ci execution
ci-all-test Run all of ci, no test spec filtering will be done
ci-force-downstream Force execution of downstream even if twister fails
ci-run-twister Force run twister
ci-run-zephyr-twister Force run zephyr twister
ci-run-draft Run CI on draft pull requests
List of changed files detected by CI (8)
doc
│  ├── nrf
│  │  ├── releases_and_maturity
│  │  │  ├── releases
│  │  │  │  │ release-notes-changelog.rst
samples
│  ├── net
│  │  ├── download
│  │  │  ├── boards
│  │  │  │  │ native_sim.conf
│  │  │  ├── prj.conf
│  │  │  ├── src
│  │  │  │  │ main.c
│  │  ├── udp
│  │  │  ├── README.rst
│  │  │  ├── prj.conf
│  │  │  ├── src
│  │  │  │  │ main.c
subsys
│  ├── net
│  │  ├── lib
│  │  │  ├── downloader
│  │  │  │  ├── src
│  │  │  │  │  │ dl_socket.c

Outputs:

Toolchain

Version: 4894884e96
Build docker image: docker-dtr.nordicsemi.no/sw-production/ncs-build:4894884e96_a140d687bb

Test Spec & Results: ✅ Success; ❌ Failure; 🟠 Queued; 🟡 Progress; ◻️ Skipped; ⚠️ Quarantine

  • ◻️ Toolchain - Skipped: existing toolchain is used
  • ✅ Build twister
    • sdk-nrf test count: 276
  • ✅ Integration tests
    • ✅ test-fw-nrfconnect-nrf-iot_zephyr_lwm2m
    • ✅ test-fw-nrfconnect-nrf-iot_samples
    • ✅ test-fw-nrfconnect-nrf-iot_lwm2m
    • ✅ test-fw-nrfconnect-nrf_lrcs_mosh
    • ✅ test-sdk-dfu
    • ⚠️ test-fw-nrfconnect-nrf-iot_cloud
    • ⚠️ test-fw-nrfconnect-nrf-iot_thingy91
    • ⚠️ test-fw-nrfconnect-nrf_lrcs_mosh
Disabled integration tests
    • test-fw-nrfconnect-nrf_lrcs_positioning
    • desktop52_verification
    • test_ble_nrf_config
    • test-fw-nrfconnect-apps
    • test-fw-nrfconnect-apps_nrfdesktop
    • test-fw-nrfconnect-ble_mesh
    • test-fw-nrfconnect-ble_samples
    • test-fw-nrfconnect-fem
    • test-fw-nrfconnect-nfc
    • test-fw-nrfconnect-nrf-iot_libmodem-nrf
    • test-fw-nrfconnect-nrf_crypto
    • test-fw-nrfconnect-ps-main
    • test-fw-nrfconnect-rpc
    • test-fw-nrfconnect-rs
    • test-fw-nrfconnect-tfm
    • test-fw-nrfconnect-thread-main
    • test-low-level
    • test-sdk-audio
    • test-sdk-find-my
    • test-sdk-mcuboot
    • test-sdk-wifi
    • test-sdk-wifi-net
    • test-sdk-wifi-wezen-7120

Note: This message is automatically posted and updated by the CI

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

Unresolved download deadlock and native_sim event-handling issues can prevent correct completion or socket setup.

Get a fresh assessment by requesting another Copilot review.

Pull request overview

Adds IPv4/IPv6 coexistence support to the UDP and download samples, including family selection, connectivity tracking, and retries.

Changes:

  • Adds IPv4/IPv6 UDP handling.
  • Adds family-aware download retries.
  • Enables IPv6 and updates documentation/logging.
File summaries
File Summary
subsys/net/lib/downloader/src/dl_socket.c Adjusts IPv6 lookup failure logging.
samples/net/udp/src/main.c Adds IPv4/IPv6 parsing, socket, and event handling.
samples/net/udp/README.rst Documents IPv4/IPv6 server support.
samples/net/udp/prj.conf Enables IPv6 networking.
samples/net/download/src/main.c Tracks families and retries downloads.
samples/net/download/prj.conf Enables IPv6 networking.
Review details

Suppressed comments (4)

samples/net/download/src/main.c:536

  • When all attempts fail, the preceding code prints Giving up, but this unconditional message immediately says Download finished, which makes a failed download look successful in the logs. Use a cleanup-only message here or emit the success wording only when download_succeeded is true.
	printk("Download finished, bringing network interfaces down\n");

samples/net/download/src/main.c:483

  • After the eighth failed attempt, the failure branch still sleeps using the maximum backoff, then the next iteration increments attempt to 9 and breaks. This adds an unnecessary delay after retries are exhausted and causes the final message to report 9 failed attempts even though only 8 downloads ran; the loop should stop/back off only when another attempt will actually be made.
		if (attempt > MAX_ATTEMPTS) {
			break;
		}

samples/net/download/src/main.c:538

  • This changes the terminal output, but samples/net/download/README.rst still documents Bye and omits the new shutdown message. Update the sample output documentation so it matches the messages emitted by this implementation.
	printk("Download finished, bringing network interfaces down\n");
	(void)conn_mgr_if_disconnect(net_if);
	(void)conn_mgr_all_if_down(true);

samples/net/udp/src/main.c:96

  • The new IPv6 path still reports packet size using UDP_IP_HEADER_SIZE, which is 28 bytes for the IPv4 header. IPv6 has a 40-byte base header, so IPv6 transmissions now log an incorrect size; make the accounting family-dependent or remove the header adjustment.
	if (host_addr_family == AF_INET6) {
		struct sockaddr_in6 *server6 = ((struct sockaddr_in6 *)&host_addr);

		if (server6->sin6_port == 0) {
			server6->sin6_port = htons(CONFIG_UDP_SAMPLE_SERVER_PORT);
  • Files reviewed: 6/6 changed files
  • Comments generated: 4
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread samples/net/download/src/main.c
Comment thread samples/net/download/src/main.c
Comment thread samples/net/udp/src/main.c
Comment thread samples/net/udp/src/main.c Outdated
@simonduq
simonduq force-pushed the ipfamily-fixes-rebased branch from 4b2f2a0 to 0b2dceb Compare September 16, 2026 15:21
@simonduq
simonduq requested a review from a team as a code owner September 16, 2026 15:21
@simonduq
simonduq force-pushed the ipfamily-fixes-rebased branch from 0b2dceb to 5fe6e16 Compare September 16, 2026 15:41
@github-actions

github-actions Bot commented Sep 16, 2026

Copy link
Copy Markdown

Comment thread samples/net/download/src/main.c

@peknis peknis left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Approved with a nit.

Comment thread doc/nrf/releases_and_maturity/releases/release-notes-changelog.rst Outdated
@simonduq
simonduq force-pushed the ipfamily-fixes-rebased branch from 0c9d243 to 6abf567 Compare September 17, 2026 07:43

@PavelVPV PavelVPV left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot comments seems valid

Comment thread samples/net/download/src/main.c
Comment thread samples/net/download/src/main.c
Comment thread samples/net/udp/src/main.c Outdated
* This is necessary because the network interface is automatically brought up
* at SYS_INIT() before main() is called.
* This means that NET_EVENT_L4_CONNECTED fires before the
* This means that the L4 connected event fires before the

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Wouldn't it be better to keep event name here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is because we now wait for specifically the v4 or v6 event depending on which destination address we're going to send to. Updated the comment for clarity

Comment thread samples/net/udp/src/main.c
Comment thread samples/net/download/src/main.c
@simonduq
simonduq force-pushed the ipfamily-fixes-rebased branch 2 times, most recently from 4f13a48 to d89e1a6 Compare September 17, 2026 09:02
Catch IPv4 vs IPv6 connected events separately so as to run
the downloder on the available families only. This avoids
failure to reach the server on a family advertised by it
but that the device has no address for.

Signed-off-by: Simon Duquennoy <simon.duquennoy@nordicsemi.no>
Retry the download a few times with an increasing backoff until
it completes successfully. Sometimes the device will get an
IP family first then attempt download on this family and we may
need a retry on the other family.

Signed-off-by: Simon Duquennoy <simon.duquennoy@nordicsemi.no>
Callers to dl_socket_host_lookup() log EHOSTUNREACH err
with LOG_ERR(), making the "Failed to resolve hostname" log
inside the function mostly redundant. The thing it adds
is the lower-level zsock_getaddrinfo() error code. Demote
to debug to avoid clutter.

Signed-off-by: Simon Duquennoy <simon.duquennoy@nordicsemi.no>
@simonduq
simonduq force-pushed the ipfamily-fixes-rebased branch from d89e1a6 to 788210e Compare September 17, 2026 09:47
@simonduq
simonduq requested a review from PavelVPV September 17, 2026 09:49

@PavelVPV PavelVPV left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

THanks! Approving! I wonder though how our test will behave with the retry considering instabilties we know: will it hide these instabilities or will it fail?

@simonduq

Copy link
Copy Markdown
Contributor Author

THanks! Approving! I wonder though how our test will behave with the retry considering instabilties we know: will it hide these instabilities or will it fail?

Agree, created this ticket https://nordicsemi.atlassian.net/jira/software/c/projects/NCSDK/boards/2963?selectedIssue=NCSDK-41388

Comment thread samples/net/udp/src/main.c Outdated
Make the sample work when supplied with an IPv6 server address.
Extract IP family from UDP_SAMPLE_SERVER_ADDRESS_STATIC then
wait for corresponding network manager connected event and
then transmit.

Signed-off-by: Simon Duquennoy <simon.duquennoy@nordicsemi.no>
Without these, zsock_setsockopt(SO_RCVTIMEO) fails with ENOPROTOOPT.

Signed-off-by: Simon Duquennoy <simon.duquennoy@nordicsemi.no>
@simonduq
simonduq force-pushed the ipfamily-fixes-rebased branch from 788210e to 4ef631a Compare September 17, 2026 11:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changelog doc-required PR must not be merged without tech writer approval.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants