Skip to content

resolve: provide service resolve over varlink - #23750

Merged
poettering merged 1 commit into
systemd:mainfrom
vchillara:vl-resolve-service
Feb 16, 2024
Merged

poettering merged 1 commit into
systemd:mainfrom
vchillara:vl-resolve-service

Conversation

@vchillara

@vchillara vchillara commented Jun 15, 2022 •

Copy link
Copy Markdown
Contributor

ported the d-bus implementation of service resolve to varlink

@vchillara
vchillara force-pushed the vl-resolve-service branch from f858866 to 2f5fae7 Compare June 15, 2022 09:43
@systemd systemd deleted a comment from lgtm-com Bot Jun 15, 2022
@yuwata

yuwata commented Jun 16, 2022

Copy link
Copy Markdown
Member

Which systemd component uses the varlink method?

@vchillara

Copy link
Copy Markdown
Contributor Author

@yuwata
This is intended to complete the workflow of discovering and resolving services through varlink.
Clients can browse for mDNS services via #22532, and resolve services with this PR.

@yuwata

yuwata commented Jun 27, 2022

Copy link
Copy Markdown
Member

@yuwata This is intended to complete the workflow of discovering and resolving services through varlink. Clients can browse for mDNS services via #22532, and resolve services with this PR.

Sorry, I cannot follow. This provides a verlink method, but no one use it... Unlike DBus methods, we do not expose varlink methods, and only they are used internally, at least currently.

@vchillara

Copy link
Copy Markdown
Contributor Author

Our basic goal is to enable mDNS service discovery and resolution like in avahi.
Our initial (rather naive) approach was to dump the cache over d-bus in #20053.
But Poettering suggested doing this over varlink and sticking to the RFCs, which is what we did in #22532.
The idea is for an independent client, maybe implemented with libvarlink, to be able to connect to APIs exposed in resolved-varlink and get notified continuously.
The client could then make a follow-up call to io.systemd.Resolve.ResolveService (implemented here) to resolve any service of interest.

@evverx

evverx commented Jul 1, 2022

Copy link
Copy Markdown
Contributor

The client could then make a follow-up call to io.systemd.Resolve.ResolveService (implemented here) to resolve any service of interest.

I'm not sure why it can't use DBus for that. If it should be integrated with resolved tightly it should be able to interact with resolved via DBus as well anyway to get/set relevant properties like MulticastDNS. It's a mess but it is what it is at this point.

if (r < 0)
return r;
}
}

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.

this loop exists in almost the exact same way in vl_method_resolve_hostname_complete() already. please unify it in a common helper.

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.

Updated in the latest commit

@poettering

Copy link
Copy Markdown
Member

So I think this makes sense to add. But we really need some test case for this, that checks that this even remotely does what is intended.

For the address/hostname resolution we pretty quickly notice if things go wrong, since it's used all the time by nss-resolve. But this here has no user in our own codebase, hence relaly needs some proper testcase.

@poettering poettering added the reviewed/needs-rework 🔨 PR has been reviewed and needs another round of reworks label Jul 5, 2022
@vchillara
vchillara force-pushed the vl-resolve-service branch 2 times, most recently from 51a95f6 to b838245 Compare August 16, 2022 07:20
@vchillara

Copy link
Copy Markdown
Contributor Author

So I think this makes sense to add. But we really need some test case for this, that checks that this even remotely does what is intended.

For the address/hostname resolution we pretty quickly notice if things go wrong, since it's used all the time by nss-resolve. But this here has no user in our own codebase, hence relaly needs some proper testcase.

Testing is being discussed in #22532 (at least for mDNS)

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

this really needs some form of testing, i.e some simple addition to TEST-75-RESOLVED.

Comment thread src/resolve/resolved-varlink.c Outdated
static const JsonDispatch dispatch_table[] = {
{ "name", JSON_VARIANT_STRING, json_dispatch_string, offsetof(LookupParametersResolveService, name), JSON_MANDATORY },
{ "type", JSON_VARIANT_STRING, json_dispatch_string, offsetof(LookupParametersResolveService, type), JSON_MANDATORY },
{ "domain", JSON_VARIANT_STRING, json_dispatch_string, offsetof(LookupParametersResolveService, domain), JSON_MANDATORY },

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 use json_dispatch_const_string(). That way the strings don't have to be copied. Given you don't need them after this function finished that sounds like a good idea.

Comment thread src/resolve/resolved-varlink.c Outdated
DnsQuestion *question,
DnsQuery *q,
DnsResourceRecord **canonical,
bool search_domain_flag) {

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.

hmmm, why not make this parameter a const char* search_domain? i.e. already call DNS_SEARCH_DOMAIN_NAME() in the caller where appropriate and pass NULL here if not appropriate. I think that would be more readable.

r = json_variant_append_array(array, entry);
if (r < 0)
goto finish;
return r;

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'd really update the array first, since this can fail, and if it does then you already updated *canonical... if you switch this, then the function will either fail or succeed, but never return half-initialized data...

Comment thread src/resolve/resolved-varlink.c Outdated
if (i->length <= 0)
continue;

r = json_build(&entry, JSON_BUILD_BYTE_ARRAY(i->data, i->length));

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 encode this as base63, not as byte array.

also, use json_build() only if you build complex json objects. for simple stuff just call the indiviudal constructors directly, i.e.:

r = json_variant_new_base64(&entry, i->data, i->length);

r = json_variant_append_array(_txt, entry);
if (r < 0)
return r;
}

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.

this leaks memory, as you do not free the entry object here. i.e. move the declaration of entry into the loop

if (r < 0)
return r;

return 1;

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.

so we generally have this rule that parameters that are purely output should be filled in only on success, and not touched on failure. here, you will touch _srv early on, even if later stuff can still fail. Hence, please always build your stuff in temporary vairables, and only if you know nothing can fail anymore, move it to the return parameters. (usually TAKE_PTR() is a good way to do so).

also, please name return variables ret_xyz as indicated above

Comment thread src/resolve/resolved-varlink.c Outdated

static void resolve_service_all_complete(DnsQuery *query) {
_cleanup_(dns_query_freep) DnsQuery *q = query;
_cleanup_(json_variant_unrefp) JsonVariant *_srv = NULL, *_addr = NULL, *_txt = NULL, *_norm = NULL, *_canonical = NULL;

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.

why the underscore here?

Comment thread src/resolve/resolved-varlink.c Outdated
JSON_BUILD_PAIR("type", JSON_BUILD_STRING(type)),
JSON_BUILD_PAIR("domain", JSON_BUILD_STRING(domain))));
if (r < 0)
goto finish;

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.

why build this object separately? you can just build this as part of varlink_replyb(), simply by inserting JSON_BUILD_OBJECT() where you currently use JSON_BUILD_VARIANT() for the canonical field

Comment thread src/resolve/resolved-varlink.c Outdated
typedef struct LookupParametersResolveService {
char *name;
char *type;
char *domain;

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.

as mentioned further down, please use const char* here, and fill in the fields via json_dispatch_const_string() as then you don't need to copy them, and also not free them

@poettering

Copy link
Copy Markdown
Member

and sorry here too for the slow slow reviews. mea culpa!

@vchillara
vchillara force-pushed the vl-resolve-service branch from 0ec4cc6 to 222c2c9 Compare July 14, 2023 07:10
@github-actions github-actions Bot added please-review PR is ready for (re-)review by a maintainer and removed reviewed/needs-rework 🔨 PR has been reviewed and needs another round of reworks labels Jul 14, 2023
@github-actions

github-actions Bot commented Jul 14, 2023 •

Copy link
Copy Markdown

An -rc1 tag has been created and a release is being prepared, so please note that PRs introducing new features and APIs will be held back until the new version has been released.

@vchillara
vchillara force-pushed the vl-resolve-service branch from 222c2c9 to a5aa61e Compare July 14, 2023 07:27
@vchillara

Copy link
Copy Markdown
Contributor Author

@poettering addressed your comments.
Looking into testing for this

@vchillara

Copy link
Copy Markdown
Contributor Author

@poettering added test for resolve over varlink in TEST-75-RESOLVED

@vchillara

Copy link
Copy Markdown
Contributor Author

Rebased and resolved conflicts.
Please review @poettering

@keszybz
keszybz requested a review from poettering October 17, 2023 16:59
assert(rr->key);

if (rr->key->type != DNS_TYPE_TXT)
return 0;

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.

you return from the function here successfully (i.e. with an error code >= 0).

according to our coding style functions should initialize all return paramaters on success (and leave them untouched on failure). THus, you need to add *ret_txt = NULL here, if you return 0.

Comment thread src/resolve/resolved-varlink.c Outdated
if (r < 0)
return r;

r = json_variant_append_array(ret_txt, entry);

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, so now i am confused. our coding style says that you should name return parameters ret_xyz. A return parameter is a purely output param. But here you extend what you get. Hence either the code is wrong or the name (i.e. drop the "ret_" prefix from the parmaete name)

my comment regarding return parameter initialization above is rooted in the misunderstanding that I thought this was a purely output param because of the name, which it appaerntly isn't after all

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.

dropped the ret_ prefix.

Comment thread src/resolve/resolved-varlink.c Outdated

static void resolve_service_all_complete(DnsQuery *query) {
_cleanup_(dns_query_freep) DnsQuery *q = query;
_cleanup_(json_variant_unrefp) JsonVariant *ret_srv = NULL, *ret_addr = NULL, *ret_txt = NULL, *ret_norm = NULL;

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 don't name local variables ret_xyz. As per our coding style that's for output-only (i.e. "return" params), hence never for local variables. just drop the ret_ prefix

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.

fixed

Comment thread test/units/testsuite-75.sh Outdated
grep -qF '"srv":{"priority":10,"weight":5,"port":1234,"hostname":"myservice.signed.test"}' "$VL_RESOLVE_LOGS"
grep -qF '{"ifindex":4,"family":10,"address":[253,0,222,173,190,239,202,254,0,0,0,0,0,0,0,23]}' "$VL_RESOLVE_LOGS"
grep -qF '{"ifindex":4,"family":2,"address":[10,0,0,20]}' "$VL_RESOLVE_LOGS"
grep -qF '"normalized":{"normalized":"myservice.signed.test"}' "$VL_RESOLVE_LOGS"

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 a field which contains an object whith a field also called like the object#s field? that doesn't look right

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.

removed the wrapper field

grep -qF '"normalized":{"normalized":"myservice.signed.test"}' "$VL_RESOLVE_LOGS"
grep -qF '"canonical":{"name":null,"type":"_mysvc._tcp","domain":"signed.test"}' "$VL_RESOLVE_LOGS"
TXT_OUT=$(grep -a -o -P '(?<=\"txt\"\:\[\").*(?=\"\])' "$VL_RESOLVE_LOGS" | base64 --decode)
assert_in "This is TXT for myservice" "$TXT_OUT"

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 add a test with the varlinkctl tool

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.

added test with varlinkctl

@poettering

Copy link
Copy Markdown
Member

CI failed badly.

Please add varlink introspection support, i.e. add the method call, all errors and the structures you added to src/shared/varlink-io.systemd.Resolve.c

@poettering

Copy link
Copy Markdown
Member

And sorry for taking so terribly long to review this.

@poettering poettering added reviewed/needs-rework 🔨 PR has been reviewed and needs another round of reworks and removed please-review PR is ready for (re-)review by a maintainer labels Nov 6, 2023
@github-actions github-actions Bot added util-lib please-review PR is ready for (re-)review by a maintainer and removed reviewed/needs-rework 🔨 PR has been reviewed and needs another round of reworks labels Nov 10, 2023
ported the d-bus implementation of service resolve to varlink
extended TEST-75-RESOLVED to cover this use-case
@vchillara

Copy link
Copy Markdown
Contributor Author

@poettering fixed the CI failures due to TEST-75 additions.
Added introspection support.

A couple of mkosi checks seem to be failing, but it looks unrelated to the code changes.

@poettering
poettering merged commit e1634bb into systemd:main Feb 16, 2024
@github-actions github-actions Bot removed the please-review PR is ready for (re-)review by a maintainer label Feb 16, 2024
VARLINK_DEFINE_INPUT(flags, VARLINK_INT, VARLINK_NULLABLE),
VARLINK_DEFINE_OUTPUT_BY_TYPE(srv, ResolvedService, 0),
VARLINK_DEFINE_OUTPUT_BY_TYPE(addr, ResolvedAddress, VARLINK_ARRAY),
VARLINK_DEFINE_OUTPUT(txt, VARLINK_STRING, VARLINK_ARRAY),

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.

Looks like this is triggered when services comes with no key/value pairs:

Varlink-IDL: Mandatory field 'txt' is null or missing on object, refusing.
varlink-26: Return parameters for method reply ResolveService() didn't pass validation on field 'txt', ignoring: No anode
varlink-26: Sending message: {"parameters":{"srv":{"priority":0,"weight":0,"port":123,"hostname":"host-5.local"},"addr":[{"ifindex":3,"family":2,"address":[192,168,56,3]},{"ifindex":3,"family":10,"address":[254,128,0,0,0,0,0,0,0,224,25,56,10,209,68,157]}],"txt":null,"normalized":"host-5.local","canonical":{"name":"X","type":"_ipp._tcp","domain":"local"}}}

it should probably emit empty arrays instead of null when it receives TXT RRs with a single empty string

            Name: X._ipp._tcp.local                                                                                                                           
            Type: TXT (Text strings) (16)                                                                                                                     
            .000 0000 0000 0001 = Class: IN (0x0001)                                                                                                          
            1... .... .... .... = Cache flush: True                                                                                                           
            Time to live: 4500 (1 hour, 15 minutes)                                                                                                           
            Data length: 1                                                                                                                                    
            TXT Length: 0                                                                                                                                     
            TXT:                                  

or zero-length RDATA.

return varlink_error_invalid_parameter(link, JSON_VARIANT_STRING_CONST("domain"));

if (!validate_and_mangle_flags(p.name, &p.in_flags, 0))
return varlink_error_invalid_parameter(link, JSON_VARIANT_STRING_CONST("flags"));

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.

I'm not sure if it's intentional but it seems it isn't possible to pass SD_RESOLVED_NO_TXT or SD_RESOLVED_NO_ADDRESS (corresponding to --service-txt=no and --service-txt=no). Currently resolved rejects those flags with

varlink-26: Received message: {"method":"io.systemd.Resolve.ResolveService","parameters":{"name":"Y","type":"_ipp._tcp","domain":"local","flags":192}}
varlink-26: Changing state idle-server → processing-method
varlink-26: Sending message: {"error":"org.varlink.service.InvalidParameter","parameters":{"parameter":"flags"}}
varlink-26: Changing state processing-method → processed-method
varlink-26: Changing state processed-method → idle-server

@evverx

evverx commented Feb 17, 2024

Copy link
Copy Markdown
Contributor

There is also a memory leak #31371 but other than that it seems that unlike the D-Bus method (which returns all the SRV and TXT RRs) the varlink method picks the first ones:

resolvectl service X._qotd._tcp.local
X._qotd._tcp.local: host-5.local:124 [priority=0, weight=0]
                    192.168.56.3                            -- link: eth1
                    192.168.56.3                            -- link: eth1
                    host-5.local:123 [priority=0, weight=0]
                    192.168.56.3                            -- link: eth1
                    192.168.56.3                            -- link: eth1
                    x=1
                    x=2
                    a=1
                    b=2
                    (X/_qotd._tcp/local)
printf '{"method":"io.systemd.Resolve.ResolveService","parameters":{"name":"X","type":"_qotd._tcp","domain":"local","flags":0}}\0\0' | nc -U /run/systemd/resolve/io.systemd.Resolve | jq .parameters.srv
{
  "priority": 0,
  "weight": 0,
  "port": 123,
  "hostname": "host-5.local"
}

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.

4 participants