Skip to content

resolved: Implement continuous mDNS querying as per RFC6762 5.2 - #22532

Merged
keszybz merged 5 commits into
systemd:mainfrom
vchillara:resolved-mdns-browse
Jul 18, 2025
Merged

keszybz merged 5 commits into
systemd:mainfrom
vchillara:resolved-mdns-browse

Conversation

@vchillara

@vchillara vchillara commented Feb 16, 2022 •

Copy link
Copy Markdown
Contributor

Allow for mDNS service/domain/types browsing.
A client can connect to the backend via varlink and receive updates as the requested service becomes available.

The interval between the first two queries MUST be at least one second, the intervals between successive queries MUST increase by at least a factor of two.
When the interval between queries reaches or exceeds 60 minutes, a querier MAY cap the interval to a maximum of 60 minutes, and perform subsequent queries at a steady-state rate of one query per hour.

Cache maintenance performed by issuing queries at 80, 85, 90, 95% of the TTL, and updating/removing the records depending on replies.

TODO:
Improve the DNS transaction logic when multiple clients subscribe to the same service, ensuring that continuous queries are optimized.

@vchillara

Copy link
Copy Markdown
Contributor Author

Looks like a couple of autopkgtest checks are failing due to timeouts. Possibly unrelated to the content of the PR.
Could you please guide us with this?

@yuwata

yuwata commented Feb 17, 2022

Copy link
Copy Markdown
Member

Ubuntu CIs are quite unstable. Please simply ignore the failures if they seem unrelated.

@vchillara

Copy link
Copy Markdown
Contributor Author

@poettering
This pull request is a rework of mDNS browse based on your feedback on this PR by aditiambadkar.

Any feedback regarding this implementation would be appreciated.

@zonque

zonque commented Mar 21, 2022

Copy link
Copy Markdown
Contributor

@vchillara could you rebase this one?

@vchillara
vchillara force-pushed the resolved-mdns-browse branch from 3d16555 to 8e1ddd2 Compare March 22, 2022 09:31
@vchillara

Copy link
Copy Markdown
Contributor Author

@vchillara could you rebase this one?

rebased.

Also, reduced the scope of this pull request to include just the feature.
Bug fixes/improvements spun off into separate PRs #22750 (merged), #22751, #22753

@vchillara

Copy link
Copy Markdown
Contributor Author

@bluca branch has been rebased. could you please remove the "needs-rebase" label?

@vchillara

Copy link
Copy Markdown
Contributor Author

@zonque @yuwata could you please provide some feedback on this approach?

@vchillara

Copy link
Copy Markdown
Contributor Author

Added cache maintenance (issuing queries at 80, 85, 90, 95% of the TTL for services with actively interested clients, and updating records) as per RFC 6762.
Appreciate any feedback.

Comment thread src/resolve/resolved-varlink.c Resolved
@vchillara
vchillara force-pushed the resolved-mdns-browse branch from dfac3a5 to 403f3bc Compare June 30, 2022 11:58
evverx
evverx previously requested changes Jun 30, 2022

@evverx evverx 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.

Looking at #23750 (comment) it appears the idea is to provide the methods so that clients implemented elsewhere can interact with resolved. I think it generally makes sense (after all that's what the API is for) but I think it should also be possible to browse services using resolvectl browse or something like that.

Other than that with this patch applied resolved can be crashed by sending malformed packets:

Program received signal SIGABRT, Aborted.
0x00007fe46f6a1aec in __pthread_kill_implementation () from target:/lib64/libc.so.6
(gdb) bt
#0  0x00007fe46f6a1aec in __pthread_kill_implementation () from target:/lib64/libc.so.6
#1  0x00007fe46f6552a6 in raise () from target:/lib64/libc.so.6
#2  0x00007fe46f6287f3 in abort () from target:/lib64/libc.so.6
#3  0x00007fe47180cebf in log_assert_failed (text=0x65a4c0 "answer", file=0x659b23 "src/resolve/resolved-mdns-browse-services.c", line=371,
    func=0x65a920 <__PRETTY_FUNCTION__.13> "mdns_notify_subscribers_unsolicited_updates") at ../src/basic/log.c:856
#4  0x00000000005c3370 in mdns_notify_subscribers_unsolicited_updates (m=0x618000000080, answer=0x0, owner_family=2) at ../src/resolve/resolved-mdns-browse-services.c:371
#5  0x000000000053960a in on_mdns_packet (s=0x612000002140, fd=15, revents=1, userdata=0x618000000080) at ../src/resolve/resolved-mdns.c:428
#6  0x00007fe471ba938e in source_dispatch (s=0x612000002140) at ../src/libsystemd/sd-event/sd-event.c:3591
#7  0x00007fe471bb1ba5 in sd_event_dispatch (e=0x617000000080) at ../src/libsystemd/sd-event/sd-event.c:4175
#8  0x00007fe471bb2d09 in sd_event_run (e=0x617000000080, timeout=18446744073709551615) at ../src/libsystemd/sd-event/sd-event.c:4236
#9  0x00007fe471bb308a in sd_event_loop (e=0x617000000080) at ../src/libsystemd/sd-event/sd-event.c:4257
#10 0x0000000000553a13 in run (argc=1, argv=0x7ffc7834b908) at ../src/resolve/resolved.c:92
#11 0x0000000000553cad in main (argc=1, argv=0x7ffc7834b908) at ../src/resolve/resolved.c:99

@vchillara

Copy link
Copy Markdown
Contributor Author

@daandemeyer fixed now

@evverx
Removed unnecessary assert in latest commit.
I was going to point you to that comment (#23750 (comment)) to explain the idea behind this PR, but looks like you already got to it.
We have been testing these patches locally, and are currently working on adding functional tests suitable for upstream.
We've written a client for browsing services, which we'd like to add to src/resolve. This can be invoked from a test script.
Also, what mechanism are you using to feed packets to resolved for testing the mdns part? It'd be very useful for us to use in conjunction with the client to implement tests.
We tried mocking mDNS services locally, but packets originating on the local interface are rejected by resolved-mdns.

@evverx

evverx commented Jul 4, 2022

Copy link
Copy Markdown
Contributor

We've written a client for browsing services, which we'd like to add to src/resolve

Good to know. Thanks!

We have been testing these patches locally, and are currently working on adding functional tests suitable for upstream.

I'm not sure at this point the testsuite run with test-functions can be used to test the mDNS part of resolved but on the bright side it was at least discussed in #23104 (comment) and hopefully eventually it should be covered.

Also, what mechanism are you using to feed packets to resolved for testing the mdns part?

I create a local network where a VM can send packets to a container and vice versa. It isn't ideal because due to I presume some kernel bugs resolved stops handling LLMNR packets at some point, the network adapter hits "transmit queue 0 timed out" and gets reset rendering the whole network useless and stuff like that but it should be possible to get around those issues.

Ideally I think new features should be put on hold until resolved gets to a point where new heap-use-after-frees like #23894 and #23843 no longer pop up every day and at least basic tests are introduced.

Comment thread src/resolve/resolved-dns-question.c Resolved
Comment thread src/resolve/resolved-dns-question.c Resolved
@poettering

Copy link
Copy Markdown
Member

btw, please always mark the review items you think are addressed as "resolved".

Comment thread src/resolve/resolved-dns-answer.c Outdated
item->flags,
item->rrsig);
item->rrsig,
USEC_INFINITY);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

please prefix with /* until= */

(we do that these days in func calls where the passed parameter gives no hint what it is)

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.

Added /* until= */

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Where? It's not visible here, so maybe it ended up in some other commit.

@vchandap

Copy link
Copy Markdown
Contributor

btw, please always mark the review items you think are addressed as "resolved".

Hi, we have fixed the review comments and marked them as 'resolved', except for a few that need clarification

#22532 (comment)
#22532 (comment)
#22532 (comment)

Regards
Vishwa

@vchandap

vchandap commented Jul 3, 2025

Copy link
Copy Markdown
Contributor

Hi,

Could you please review and provide your valuable feedback?

@keszybz keszybz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't see anything unreasonable. Still a bit of style issues. It's confusing that the whitespace fixes are applied in commit 5/6, instead of squashed into the original commits.

Comment thread src/resolve/resolved-dns-answer.h Outdated
Comment on lines +85 to +90
return dns_answer_add_extend_full(a, rr, ifindex, flags, rrsig, USEC_INFINITY);
}

static inline int dns_answer_add(DnsAnswer *a, DnsResourceRecord *rr,
int ifindex, DnsAnswerFlags flags, DnsResourceRecord *rrsig) {
return dns_answer_add_full(a, rr, ifindex, flags, rrsig, USEC_INFINITY);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Also here, please add /* until= */ for completeness.

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.

Added.

Comment thread src/resolve/resolved-timeouts.h Outdated
* To adhere to these timing requirements for continuous queries,
* the maximum number of attempts should be set to 1.
*/
#define MDNS_TRANSACTION_CONTINUOUS_QUERY_MAX 1

ghost Jul 10, 2025

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hmm, also here I don't see this changed. (I think that the change is completely pointless too, but let's ignore that.)

I guess I'm dense, but I don't see how the two rules mean that the number is 1. "Continuous" is explicitly about sending a stream of queries infinitely…

Comment thread src/resolve/resolved-timeouts.h Outdated
switch (p) {

case DNS_PROTOCOL_LLMNR:
return LLMNR_TRANSACTION_ATTEMPTS_MAX;

ghost Jul 10, 2025

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Not addressed.

Comment thread src/resolve/resolved-dns-answer.c Resolved
* 60 minutes, perform subsequent queries at a steady-state rate of one
* query per hour */
usec_t mdns_calculate_next_query_delay(usec_t current_delay) {
assert(current_delay <= (3600 * USEC_PER_SEC));

ghost Jul 10, 2025

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

No need to wrap the multiplication in parens.

ghost Jul 14, 2025

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.

Removed the multiplication that was inside the parentheses

Comment on lines +556 to +557
"Failed to match answer key with "
"service browser's key: %m");

ghost Jul 10, 2025

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

One line.

ghost Jul 14, 2025

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.

changed to single line

Comment on lines +585 to +590
if (r < 0) {
log_error_errno(r,
"Failed to match answer key with service "
"browser's key: %m");
return;
}

ghost Jul 10, 2025

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

if (r < 0)
    return (void) log_error_errno(r, …);

and below too.

ghost Jul 14, 2025

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.

Using 'void'.


sb = dns_service_browser_ref(userdata);
if (!sb)
return log_error_errno(0, "Failed to reference service browser: %m");

ghost Jul 10, 2025

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

0 is not a valid value, it'd cause an assertion failure.

assert_se(sb = dns_service_browser_ref(userdata));

ghost Jul 14, 2025

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.

updated it as suggested

Vishal Chillara Srinivas and others added 3 commits July 14, 2025 14:50
Prepare for upcoming mDNS browse services.
Distinguish between registered and discovered services to avoid confusion.

Co-authored-by: Vishwanath Chandapur <[email protected]>
Prepare for mDNS browse services to facilitate scheduled cache
maintenance queries when browsing for services.

Introduce dns_answer_add_full() and dns_answer_add_extend_full()
to explicitly handle the 'until' argument, enabling fine-grained control
over the time property of DNS answers.

Ensure backward compatibility while providing the flexibility to
specify 'until' for specific cases when needed

Co-authored-by: Vishwanath Chandapur <[email protected]>
Prepare for upcoming mDNS browse services.

Prevent continuous queries from being attempted thrice.

Co-authored-by: Vishwanath Chandapur <[email protected]>
case DNS_RECORD_TTL_STATE_100_PERCENT:
return 100;
default:
return -1;

ghost Jul 14, 2025

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

maybe return -EINVAL here, that would be more in-line with our usual schemes.

(doesn't matter, but I would probably have implemented this via a table, i.e.

static const int ttl_percent_table[_DNS_RECORD_TTL_STATE_MAX] = {
        [DNS_RECORD_TTL_STATE_80_PERCENT] = 80,
        …
};
return ttl_percent_table[ttl_state];

you get the idea. but doesn't really matter, the switch is fine too, and i have the suspicion, that good compilers generate the same code from both these days

ghost Jul 15, 2025

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.

Done, Converted the switch cases into a table and added returning -EINVAL.

assert(percent > 0);

return usec_sub_unsigned(until, (100 - percent) * ttl * USEC_PER_SEC / 100);
}

ghost Jul 14, 2025

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

hmm, is this function used outside of this .c file? if so please do not export it. (same for all other funcs defind here). i.e. add static wherever you can.

ghost Jul 15, 2025

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.

We added a few unit tests to verify these timings. Hence, we removed the static, now we have added it back.
We will try to find another way to unit test later.


r = dns_answer_match_key(query->answer, sb->key, NULL);
if (r < 0)
return (void) log_error_errno(r, "mDNS answer does not match service browser key: %m");

ghost Jul 14, 2025

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

hmm, this error conditoin, how can this ever happen? if this is impossible, it should be an assert(). If it can happen but is nothing too concerning and we can go on, then it should be a warning level thing, and it should be saying ", ignoring" at the end of the msg. Or if it can happen and means the query has to fail, then we need to do more than just error log at error level, and somehow fail the query. or, in the case this actually is ok this way it deserves a comment, why it is.

ghost Jul 15, 2025

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.

This logic is redundant and has been removed.

Allow for mDNS service/domain/types browsing.

A client can connect to the backend via varlink and receive updates as the
requested service becomes available.
The interval between the first two queries MUST be at least one second,
the intervals between successive queries MUST increase by at least a factor of two.
When the interval between queries reaches or exceeds 60 minutes, a querier MAY cap
the interval to a maximum of 60 minutes, and perform subsequent queries at a
steady-state rate of one query per hour.
Delete expired cache entries one second after goodbye packet received
as per RFC6762 Section 10.1

Cache maintenance:
The querier should plan to issue a query at 80% of the record lifetime, and
then if no answer is received, at 85%, 90%, and 95%.
If an answer is received, then the remaining TTL is reset to the value given
in the answer, and this process repeats for as long as the Multicast DNS querier
has an ongoing interest in the record.
If no answer is received after four queries, the record is deleted when it
reaches 100% of its lifetime.

TODO:
Improve the DNS transaction logic when multiple clients subscribe to
the same service, ensuring that continuous queries are optimized.

Co-authored-by: Vishwanath Chandapur <[email protected]>
Comment thread test/units/TEST-89-RESOLVED-MDNS.sh Outdated

systemctl unmask systemd-resolved.service systemd-networkd.{service,socket} systemd-machined.service
systemctl enable --now systemd-resolved.service systemd-networkd.{socket,service} systemd-machined.service
ln -svrf /run/systemd/resolved.conf.d/99-mdns-llmnr.conf /etc/resolv.conf

ghost Jul 17, 2025

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't understand this here. /etc/resolv.conf is a completely different format than the ini .conf files used by systemd. This line effectively just disables resolv.conf.

ghost Jul 17, 2025

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.

Since the tests are very specific to mDNS, want to make sure the global mDNS is ON

ghost Jul 17, 2025

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

/etc/resolv.conf uses a format like this:

nameserver 127.0.0.53
options edns0 trust-ad
search home

but /run/systemd/resolved.conf and /run/systemd/resolved.conf.d/99-mdns-llmnr.conf are ini files with a [Resolve] section and key=value settings. Those formats are incompatible.

ghost Jul 17, 2025

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.

Understood; removed the soft link

Comment thread test/units/TEST-89-RESOLVED-MDNS.sh Resolved
Comment thread test/units/TEST-89-RESOLVED-MDNS.sh Resolved
Comment thread test/units/TEST-89-RESOLVED-MDNS.sh Resolved
Comment thread test/units/TEST-89-RESOLVED-MDNS.sh Resolved
@keszybz

ghost commented Jul 17, 2025

Copy link
Copy Markdown
Member

OK, I think we should merge this. I looked at the test, and there's one confused file and a few style tweaks needed, but otherwise it seems to make sense. @poettering, since you reviewed this in the past, good to go?

Co-authored-by: Frantisek Sumsal <[email protected]>
Co-authored-by: Vishwanath Chandapur <[email protected]>
@keszybz

ghost commented Jul 18, 2025

Copy link
Copy Markdown
Member

Thank you for your patience ;)

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

Development

Successfully merging this pull request may close these issues.