X Tutup
Skip to content

Commit 871a3a3

Browse files
authored
Merge pull request systemd#19608 from keszybz/resolved-pahole
Pahole optimization for resolved's DnsQuery
2 parents 6db7b53 + 6f1d18a commit 871a3a3

12 files changed

+174
-156
lines changed

src/resolve/resolved-dns-dnssec.c

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
uint16_t dnssec_keytag(DnsResourceRecord *dnskey, bool mask_revoke) {
3636
const uint8_t *p;
3737
uint32_t sum, f;
38-
size_t i;
3938

4039
/* The algorithm from RFC 4034, Appendix B. */
4140

@@ -51,7 +50,7 @@ uint16_t dnssec_keytag(DnsResourceRecord *dnskey, bool mask_revoke) {
5150

5251
p = dnskey->dnskey.key;
5352

54-
for (i = 0; i < dnskey->dnskey.key_size; i++)
53+
for (size_t i = 0; i < dnskey->dnskey.key_size; i++)
5554
sum += (i & 1) == 0 ? (uint32_t) p[i] << 8 : (uint32_t) p[i];
5655

5756
sum += (sum >> 16) & UINT32_C(0xFFFF);
@@ -483,14 +482,14 @@ static int dnssec_rrsig_prepare(DnsResourceRecord *rrsig) {
483482
const char *name;
484483
int r;
485484

486-
/* Checks whether the specified RRSIG RR is somewhat valid, and initializes the .n_skip_labels_source and
487-
* .n_skip_labels_signer fields so that we can use them later on. */
485+
/* Checks whether the specified RRSIG RR is somewhat valid, and initializes the .n_skip_labels_source
486+
* and .n_skip_labels_signer fields so that we can use them later on. */
488487

489488
assert(rrsig);
490489
assert(rrsig->key->type == DNS_TYPE_RRSIG);
491490

492491
/* Check if this RRSIG RR is already prepared */
493-
if (rrsig->n_skip_labels_source != UINT_MAX)
492+
if (rrsig->n_skip_labels_source != UINT8_MAX)
494493
return 0;
495494

496495
if (rrsig->rrsig.inception > rrsig->rrsig.expiration)
@@ -523,6 +522,7 @@ static int dnssec_rrsig_prepare(DnsResourceRecord *rrsig) {
523522
if (r == 0)
524523
return -EINVAL;
525524

525+
assert(n_key_labels < UINT8_MAX); /* UINT8_MAX/-1 means unsigned. */
526526
rrsig->n_skip_labels_source = n_key_labels - rrsig->rrsig.labels;
527527
rrsig->n_skip_labels_signer = n_key_labels - n_signer_labels;
528528

@@ -604,13 +604,11 @@ static void dnssec_fix_rrset_ttl(
604604
DnsResourceRecord *rrsig,
605605
usec_t realtime) {
606606

607-
unsigned k;
608-
609607
assert(list);
610608
assert(n > 0);
611609
assert(rrsig);
612610

613-
for (k = 0; k < n; k++) {
611+
for (unsigned k = 0; k < n; k++) {
614612
DnsResourceRecord *rr = list[k];
615613

616614
/* Pick the TTL as the minimum of the RR's TTL, the
@@ -640,7 +638,7 @@ int dnssec_verify_rrset(
640638
const char *source, *name;
641639
_cleanup_(gcry_md_closep) gcry_md_hd_t md = NULL;
642640
int r, md_algorithm;
643-
size_t k, n = 0;
641+
size_t n = 0;
644642
size_t sig_size = 0;
645643
_cleanup_free_ char *sig_data = NULL;
646644
_cleanup_fclose_ FILE *f = NULL;
@@ -770,7 +768,7 @@ int dnssec_verify_rrset(
770768
if (r < 0)
771769
return r;
772770

773-
for (k = 0; k < n; k++) {
771+
for (size_t k = 0; k < n; k++) {
774772
size_t l;
775773

776774
rr = list[k];
@@ -1210,7 +1208,6 @@ int dnssec_nsec3_hash(DnsResourceRecord *nsec3, const char *name, void *ret) {
12101208
size_t hash_size;
12111209
int algorithm;
12121210
void *result;
1213-
unsigned k;
12141211
int r;
12151212

12161213
assert(nsec3);
@@ -1252,7 +1249,7 @@ int dnssec_nsec3_hash(DnsResourceRecord *nsec3, const char *name, void *ret) {
12521249
if (!result)
12531250
return -EIO;
12541251

1255-
for (k = 0; k < nsec3->nsec3.iterations; k++) {
1252+
for (unsigned k = 0; k < nsec3->nsec3.iterations; k++) {
12561253
uint8_t tmp[hash_size];
12571254
memcpy(tmp, result, hash_size);
12581255

@@ -1291,10 +1288,10 @@ static int nsec3_is_good(DnsResourceRecord *rr, DnsResourceRecord *nsec3) {
12911288

12921289
/* Ignore NSEC3 RRs generated from wildcards. If these NSEC3 RRs weren't correctly signed we can't make this
12931290
* check (since rr->n_skip_labels_source is -1), but that's OK, as we won't trust them anyway in that case. */
1294-
if (!IN_SET(rr->n_skip_labels_source, 0, UINT_MAX))
1291+
if (!IN_SET(rr->n_skip_labels_source, 0, UINT8_MAX))
12951292
return 0;
12961293
/* Ignore NSEC3 RRs that are located anywhere else than one label below the zone */
1297-
if (!IN_SET(rr->n_skip_labels_signer, 1, UINT_MAX))
1294+
if (!IN_SET(rr->n_skip_labels_signer, 1, UINT8_MAX))
12981295
return 0;
12991296

13001297
if (!nsec3)
@@ -2234,24 +2231,24 @@ int dnssec_test_positive_wildcard(
22342231
#endif
22352232

22362233
static const char* const dnssec_result_table[_DNSSEC_RESULT_MAX] = {
2237-
[DNSSEC_VALIDATED] = "validated",
2238-
[DNSSEC_VALIDATED_WILDCARD] = "validated-wildcard",
2239-
[DNSSEC_INVALID] = "invalid",
2240-
[DNSSEC_SIGNATURE_EXPIRED] = "signature-expired",
2234+
[DNSSEC_VALIDATED] = "validated",
2235+
[DNSSEC_VALIDATED_WILDCARD] = "validated-wildcard",
2236+
[DNSSEC_INVALID] = "invalid",
2237+
[DNSSEC_SIGNATURE_EXPIRED] = "signature-expired",
22412238
[DNSSEC_UNSUPPORTED_ALGORITHM] = "unsupported-algorithm",
2242-
[DNSSEC_NO_SIGNATURE] = "no-signature",
2243-
[DNSSEC_MISSING_KEY] = "missing-key",
2244-
[DNSSEC_UNSIGNED] = "unsigned",
2245-
[DNSSEC_FAILED_AUXILIARY] = "failed-auxiliary",
2246-
[DNSSEC_NSEC_MISMATCH] = "nsec-mismatch",
2247-
[DNSSEC_INCOMPATIBLE_SERVER] = "incompatible-server",
2239+
[DNSSEC_NO_SIGNATURE] = "no-signature",
2240+
[DNSSEC_MISSING_KEY] = "missing-key",
2241+
[DNSSEC_UNSIGNED] = "unsigned",
2242+
[DNSSEC_FAILED_AUXILIARY] = "failed-auxiliary",
2243+
[DNSSEC_NSEC_MISMATCH] = "nsec-mismatch",
2244+
[DNSSEC_INCOMPATIBLE_SERVER] = "incompatible-server",
22482245
};
22492246
DEFINE_STRING_TABLE_LOOKUP(dnssec_result, DnssecResult);
22502247

22512248
static const char* const dnssec_verdict_table[_DNSSEC_VERDICT_MAX] = {
2252-
[DNSSEC_SECURE] = "secure",
2253-
[DNSSEC_INSECURE] = "insecure",
2254-
[DNSSEC_BOGUS] = "bogus",
2249+
[DNSSEC_SECURE] = "secure",
2250+
[DNSSEC_INSECURE] = "insecure",
2251+
[DNSSEC_BOGUS] = "bogus",
22552252
[DNSSEC_INDETERMINATE] = "indeterminate",
22562253
};
22572254
DEFINE_STRING_TABLE_LOOKUP(dnssec_verdict, DnssecVerdict);

src/resolve/resolved-dns-packet.c

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2635,31 +2635,31 @@ size_t dns_packet_size_unfragmented(DnsPacket *p) {
26352635
}
26362636

26372637
static const char* const dns_rcode_table[_DNS_RCODE_MAX_DEFINED] = {
2638-
[DNS_RCODE_SUCCESS] = "SUCCESS",
2639-
[DNS_RCODE_FORMERR] = "FORMERR",
2640-
[DNS_RCODE_SERVFAIL] = "SERVFAIL",
2641-
[DNS_RCODE_NXDOMAIN] = "NXDOMAIN",
2642-
[DNS_RCODE_NOTIMP] = "NOTIMP",
2643-
[DNS_RCODE_REFUSED] = "REFUSED",
2644-
[DNS_RCODE_YXDOMAIN] = "YXDOMAIN",
2645-
[DNS_RCODE_YXRRSET] = "YRRSET",
2646-
[DNS_RCODE_NXRRSET] = "NXRRSET",
2647-
[DNS_RCODE_NOTAUTH] = "NOTAUTH",
2648-
[DNS_RCODE_NOTZONE] = "NOTZONE",
2649-
[DNS_RCODE_BADVERS] = "BADVERS",
2650-
[DNS_RCODE_BADKEY] = "BADKEY",
2651-
[DNS_RCODE_BADTIME] = "BADTIME",
2652-
[DNS_RCODE_BADMODE] = "BADMODE",
2653-
[DNS_RCODE_BADNAME] = "BADNAME",
2654-
[DNS_RCODE_BADALG] = "BADALG",
2655-
[DNS_RCODE_BADTRUNC] = "BADTRUNC",
2638+
[DNS_RCODE_SUCCESS] = "SUCCESS",
2639+
[DNS_RCODE_FORMERR] = "FORMERR",
2640+
[DNS_RCODE_SERVFAIL] = "SERVFAIL",
2641+
[DNS_RCODE_NXDOMAIN] = "NXDOMAIN",
2642+
[DNS_RCODE_NOTIMP] = "NOTIMP",
2643+
[DNS_RCODE_REFUSED] = "REFUSED",
2644+
[DNS_RCODE_YXDOMAIN] = "YXDOMAIN",
2645+
[DNS_RCODE_YXRRSET] = "YRRSET",
2646+
[DNS_RCODE_NXRRSET] = "NXRRSET",
2647+
[DNS_RCODE_NOTAUTH] = "NOTAUTH",
2648+
[DNS_RCODE_NOTZONE] = "NOTZONE",
2649+
[DNS_RCODE_BADVERS] = "BADVERS",
2650+
[DNS_RCODE_BADKEY] = "BADKEY",
2651+
[DNS_RCODE_BADTIME] = "BADTIME",
2652+
[DNS_RCODE_BADMODE] = "BADMODE",
2653+
[DNS_RCODE_BADNAME] = "BADNAME",
2654+
[DNS_RCODE_BADALG] = "BADALG",
2655+
[DNS_RCODE_BADTRUNC] = "BADTRUNC",
26562656
[DNS_RCODE_BADCOOKIE] = "BADCOOKIE",
26572657
};
26582658
DEFINE_STRING_TABLE_LOOKUP(dns_rcode, int);
26592659

26602660
static const char* const dns_protocol_table[_DNS_PROTOCOL_MAX] = {
2661-
[DNS_PROTOCOL_DNS] = "dns",
2662-
[DNS_PROTOCOL_MDNS] = "mdns",
2661+
[DNS_PROTOCOL_DNS] = "dns",
2662+
[DNS_PROTOCOL_MDNS] = "mdns",
26632663
[DNS_PROTOCOL_LLMNR] = "llmnr",
26642664
};
26652665
DEFINE_STRING_TABLE_LOOKUP(dns_protocol, DnsProtocol);

src/resolve/resolved-dns-packet.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,21 +71,23 @@ struct DnsPacket {
7171
DnsAnswer *answer;
7272
DnsResourceRecord *opt;
7373

74+
/* For support of truncated packets */
75+
DnsPacket *more;
76+
7477
/* Packet reception metadata */
78+
usec_t timestamp; /* CLOCK_BOOTTIME (or CLOCK_MONOTONIC if the former doesn't exist) */
7579
int ifindex;
7680
int family, ipproto;
7781
union in_addr_union sender, destination;
7882
uint16_t sender_port, destination_port;
7983
uint32_t ttl;
80-
usec_t timestamp; /* CLOCK_BOOTTIME (or CLOCK_MONOTONIC if the former doesn't exist) */
8184

82-
/* For support of truncated packets */
83-
DnsPacket *more;
85+
bool on_stack;
86+
bool extracted;
87+
bool refuse_compression;
88+
bool canonical_form;
8489

85-
bool on_stack:1;
86-
bool extracted:1;
87-
bool refuse_compression:1;
88-
bool canonical_form:1;
90+
/* Note: fields should be ordered to minimize alignment gaps. Use pahole! */
8991
};
9092

9193
static inline uint8_t* DNS_PACKET_DATA(const DnsPacket *p) {

src/resolve/resolved-dns-query.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212

1313
#define QUERIES_MAX 2048
1414
#define AUXILIARY_QUERIES_MAX 64
15+
#define CNAME_REDIRECTS_MAX 16
16+
17+
assert_cc(AUXILIARY_QUERIES_MAX < UINT8_MAX);
18+
assert_cc(CNAME_REDIRECTS_MAX < UINT8_MAX);
1519

1620
static int dns_query_candidate_new(DnsQueryCandidate **ret, DnsQuery *q, DnsScope *s) {
1721
DnsQueryCandidate *c;
@@ -1004,9 +1008,9 @@ static int dns_query_cname_redirect(DnsQuery *q, const DnsResourceRecord *cname)
10041008

10051009
assert(q);
10061010

1007-
q->n_cname_redirects++;
1008-
if (q->n_cname_redirects > CNAME_REDIRECT_MAX)
1011+
if (q->n_cname_redirects >= CNAME_REDIRECTS_MAX)
10091012
return -ELOOP;
1013+
q->n_cname_redirects++;
10101014

10111015
r = dns_question_cname_redirect(q->question_idna, cname, &nq_idna);
10121016
if (r < 0)

src/resolve/resolved-dns-query.h

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,6 @@ struct DnsQueryCandidate {
3333
struct DnsQuery {
3434
Manager *manager;
3535

36-
/* When resolving a service, we first create a TXT+SRV query, and then for the hostnames we discover
37-
* auxiliary A+AAAA queries. This pointer always points from the auxiliary queries back to the
38-
* TXT+SRV query. */
39-
DnsQuery *auxiliary_for;
40-
LIST_HEAD(DnsQuery, auxiliary_queries);
41-
unsigned n_auxiliary_queries;
42-
int auxiliary_result;
43-
4436
/* The question, formatted in IDNA for use on classic DNS, and as UTF8 for use in LLMNR or mDNS. Note
4537
* that even on classic DNS some labels might use UTF8 encoding. Specifically, DNS-SD service names
4638
* (in contrast to their domain suffixes) use UTF-8 encoding even on DNS. Thus, the difference
@@ -63,8 +55,12 @@ struct DnsQuery {
6355
uint64_t flags;
6456
int ifindex;
6557

66-
DnsTransactionState state;
67-
unsigned n_cname_redirects;
58+
/* When resolving a service, we first create a TXT+SRV query, and then for the hostnames we discover
59+
* auxiliary A+AAAA queries. This pointer always points from the auxiliary queries back to the
60+
* TXT+SRV query. */
61+
int auxiliary_result;
62+
DnsQuery *auxiliary_for;
63+
LIST_HEAD(DnsQuery, auxiliary_queries);
6864

6965
LIST_HEAD(DnsQueryCandidate, candidates);
7066
sd_event_source *timeout_event_source;
@@ -76,18 +72,26 @@ struct DnsQuery {
7672
uint64_t answer_query_flags;
7773
DnsProtocol answer_protocol;
7874
int answer_family;
75+
DnsPacket *answer_full_packet;
7976
DnsSearchDomain *answer_search_domain;
77+
78+
DnsTransactionState state;
8079
int answer_errno; /* if state is DNS_TRANSACTION_ERRNO */
81-
bool previous_redirect_unauthenticated;
82-
bool previous_redirect_non_confidential;
83-
bool previous_redirect_non_synthetic;
84-
DnsPacket *answer_full_packet;
80+
81+
unsigned block_ready;
82+
83+
uint8_t n_auxiliary_queries;
84+
uint8_t n_cname_redirects;
85+
86+
bool previous_redirect_unauthenticated:1;
87+
bool previous_redirect_non_confidential:1;
88+
bool previous_redirect_non_synthetic:1;
89+
bool request_address_valid:1;
8590

8691
/* Bus + Varlink client information */
8792
sd_bus_message *bus_request;
8893
Varlink *varlink_request;
8994
int request_family;
90-
bool request_address_valid;
9195
union in_addr_union request_address;
9296
unsigned block_all_complete;
9397
char *request_address_string;
@@ -102,12 +106,13 @@ struct DnsQuery {
102106

103107
/* Completion callback */
104108
void (*complete)(DnsQuery* q);
105-
unsigned block_ready;
106109

107110
sd_bus_track *bus_track;
108111

109112
LIST_FIELDS(DnsQuery, queries);
110113
LIST_FIELDS(DnsQuery, auxiliary_queries);
114+
115+
/* Note: fields should be ordered to minimize alignment gaps. Use pahole! */
111116
};
112117

113118
enum {
@@ -154,5 +159,3 @@ static inline uint64_t dns_query_reply_flags_make(DnsQuery *q) {
154159
dns_query_fully_confidential(q)) |
155160
(q->answer_query_flags & (SD_RESOLVED_FROM_MASK|SD_RESOLVED_SYNTHETIC));
156161
}
157-
158-
#define CNAME_REDIRECT_MAX 16

src/resolve/resolved-dns-rr.c

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -381,8 +381,8 @@ DnsResourceRecord* dns_resource_record_new(DnsResourceKey *key) {
381381
.n_ref = 1,
382382
.key = dns_resource_key_ref(key),
383383
.expiry = USEC_INFINITY,
384-
.n_skip_labels_signer = UINT_MAX,
385-
.n_skip_labels_source = UINT_MAX,
384+
.n_skip_labels_signer = UINT8_MAX,
385+
.n_skip_labels_source = UINT8_MAX,
386386
};
387387

388388
return rr;
@@ -795,14 +795,12 @@ static char *format_txt(DnsTxtItem *first) {
795795
return NULL;
796796

797797
LIST_FOREACH(items, i, first) {
798-
size_t j;
799-
800798
if (i != first)
801799
*(p++) = ' ';
802800

803801
*(p++) = '"';
804802

805-
for (j = 0; j < i->length; j++) {
803+
for (size_t j = 0; j < i->length; j++) {
806804
if (i->data[j] < ' ' || i->data[j] == '"' || i->data[j] >= 127) {
807805
*(p++) = '\\';
808806
*(p++) = '0' + (i->data[j] / 100);
@@ -1258,7 +1256,7 @@ int dns_resource_record_signer(DnsResourceRecord *rr, const char **ret) {
12581256

12591257
/* Returns the RRset's signer, if it is known. */
12601258

1261-
if (rr->n_skip_labels_signer == UINT_MAX)
1259+
if (rr->n_skip_labels_signer == UINT8_MAX)
12621260
return -ENODATA;
12631261

12641262
n = dns_resource_key_name(rr->key);
@@ -1281,7 +1279,7 @@ int dns_resource_record_source(DnsResourceRecord *rr, const char **ret) {
12811279

12821280
/* Returns the RRset's synthesizing source, if it is known. */
12831281

1284-
if (rr->n_skip_labels_source == UINT_MAX)
1282+
if (rr->n_skip_labels_source == UINT8_MAX)
12851283
return -ENODATA;
12861284

12871285
n = dns_resource_key_name(rr->key);
@@ -1315,7 +1313,7 @@ int dns_resource_record_is_synthetic(DnsResourceRecord *rr) {
13151313

13161314
/* Returns > 0 if the RR is generated from a wildcard, and is not the asterisk name itself */
13171315

1318-
if (rr->n_skip_labels_source == UINT_MAX)
1316+
if (rr->n_skip_labels_source == UINT8_MAX)
13191317
return -ENODATA;
13201318

13211319
if (rr->n_skip_labels_source == 0)
@@ -1868,9 +1866,9 @@ DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(dnssec_algorithm, int, 255);
18681866

18691867
static const char* const dnssec_digest_table[_DNSSEC_DIGEST_MAX_DEFINED] = {
18701868
/* Names as listed on https://www.iana.org/assignments/ds-rr-types/ds-rr-types.xhtml */
1871-
[DNSSEC_DIGEST_SHA1] = "SHA-1",
1872-
[DNSSEC_DIGEST_SHA256] = "SHA-256",
1869+
[DNSSEC_DIGEST_SHA1] = "SHA-1",
1870+
[DNSSEC_DIGEST_SHA256] = "SHA-256",
18731871
[DNSSEC_DIGEST_GOST_R_34_11_94] = "GOST_R_34.11-94",
1874-
[DNSSEC_DIGEST_SHA384] = "SHA-384",
1872+
[DNSSEC_DIGEST_SHA384] = "SHA-384",
18751873
};
18761874
DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(dnssec_digest, int, 255);

0 commit comments

Comments
 (0)
X Tutup