Mercurial > hg > nginx
annotate src/core/ngx_rwlock.c @ 9189:fcec773dd249
QUIC: avoid partial expansion of PATH_CHALLENGE/PATH_RESPONSE.
By default packets with these frames are expanded to 1200 bytes. Previously,
if anti-amplification limit did not allow this expansion, it was limited to
whatever size was allowed. However RFC 9000 clearly states no partial
expansion should happen in both cases.
Section 8.2.1. Initiating Path Validation:
An endpoint MUST expand datagrams that contain a PATH_CHALLENGE frame
to at least the smallest allowed maximum datagram size of 1200 bytes,
unless the anti-amplification limit for the path does not permit
sending a datagram of this size.
Section 8.2.2. Path Validation Responses:
An endpoint MUST expand datagrams that contain a PATH_RESPONSE frame
to at least the smallest allowed maximum datagram size of 1200 bytes.
...
However, an endpoint MUST NOT expand the datagram containing the
PATH_RESPONSE if the resulting data exceeds the anti-amplification limit.
author | Roman Arutyunyan <arut@nginx.com> |
---|---|
date | Wed, 29 Nov 2023 18:13:25 +0400 |
parents | 7752d8523066 |
children |
rev | line source |
---|---|
6101 | 1 |
2 /* | |
3 * Copyright (C) Ruslan Ermilov | |
4 * Copyright (C) Nginx, Inc. | |
5 */ | |
6 | |
7 | |
8 #include <ngx_config.h> | |
9 #include <ngx_core.h> | |
10 | |
11 | |
12 #if (NGX_HAVE_ATOMIC_OPS) | |
13 | |
14 | |
15 #define NGX_RWLOCK_SPIN 2048 | |
16 #define NGX_RWLOCK_WLOCK ((ngx_atomic_uint_t) -1) | |
17 | |
18 | |
19 void | |
20 ngx_rwlock_wlock(ngx_atomic_t *lock) | |
21 { | |
22 ngx_uint_t i, n; | |
23 | |
24 for ( ;; ) { | |
25 | |
26 if (*lock == 0 && ngx_atomic_cmp_set(lock, 0, NGX_RWLOCK_WLOCK)) { | |
27 return; | |
28 } | |
29 | |
30 if (ngx_ncpu > 1) { | |
31 | |
32 for (n = 1; n < NGX_RWLOCK_SPIN; n <<= 1) { | |
33 | |
34 for (i = 0; i < n; i++) { | |
35 ngx_cpu_pause(); | |
36 } | |
37 | |
38 if (*lock == 0 | |
39 && ngx_atomic_cmp_set(lock, 0, NGX_RWLOCK_WLOCK)) | |
40 { | |
41 return; | |
42 } | |
43 } | |
44 } | |
45 | |
46 ngx_sched_yield(); | |
47 } | |
48 } | |
49 | |
50 | |
51 void | |
52 ngx_rwlock_rlock(ngx_atomic_t *lock) | |
53 { | |
54 ngx_uint_t i, n; | |
55 ngx_atomic_uint_t readers; | |
56 | |
57 for ( ;; ) { | |
58 readers = *lock; | |
59 | |
60 if (readers != NGX_RWLOCK_WLOCK | |
61 && ngx_atomic_cmp_set(lock, readers, readers + 1)) | |
62 { | |
63 return; | |
64 } | |
65 | |
66 if (ngx_ncpu > 1) { | |
67 | |
68 for (n = 1; n < NGX_RWLOCK_SPIN; n <<= 1) { | |
69 | |
70 for (i = 0; i < n; i++) { | |
71 ngx_cpu_pause(); | |
72 } | |
73 | |
74 readers = *lock; | |
75 | |
76 if (readers != NGX_RWLOCK_WLOCK | |
77 && ngx_atomic_cmp_set(lock, readers, readers + 1)) | |
78 { | |
79 return; | |
80 } | |
81 } | |
82 } | |
83 | |
84 ngx_sched_yield(); | |
85 } | |
86 } | |
87 | |
88 | |
89 void | |
90 ngx_rwlock_unlock(ngx_atomic_t *lock) | |
91 { | |
7995
7752d8523066
Core: simplify reader lock release.
Pavel Pautov <p.pautov@f5.com>
parents:
7038
diff
changeset
|
92 if (*lock == NGX_RWLOCK_WLOCK) { |
7037
12efcdcb8a4b
Added memory barrier semantics to ngx_rwlock_unlock().
Ruslan Ermilov <ru@nginx.com>
parents:
6270
diff
changeset
|
93 (void) ngx_atomic_cmp_set(lock, NGX_RWLOCK_WLOCK, 0); |
7995
7752d8523066
Core: simplify reader lock release.
Pavel Pautov <p.pautov@f5.com>
parents:
7038
diff
changeset
|
94 } else { |
7752d8523066
Core: simplify reader lock release.
Pavel Pautov <p.pautov@f5.com>
parents:
7038
diff
changeset
|
95 (void) ngx_atomic_fetch_add(lock, -1); |
6101 | 96 } |
97 } | |
98 | |
99 | |
7038
d1816a2696de
Introduced ngx_rwlock_downgrade().
Ruslan Ermilov <ru@nginx.com>
parents:
7037
diff
changeset
|
100 void |
d1816a2696de
Introduced ngx_rwlock_downgrade().
Ruslan Ermilov <ru@nginx.com>
parents:
7037
diff
changeset
|
101 ngx_rwlock_downgrade(ngx_atomic_t *lock) |
d1816a2696de
Introduced ngx_rwlock_downgrade().
Ruslan Ermilov <ru@nginx.com>
parents:
7037
diff
changeset
|
102 { |
d1816a2696de
Introduced ngx_rwlock_downgrade().
Ruslan Ermilov <ru@nginx.com>
parents:
7037
diff
changeset
|
103 if (*lock == NGX_RWLOCK_WLOCK) { |
d1816a2696de
Introduced ngx_rwlock_downgrade().
Ruslan Ermilov <ru@nginx.com>
parents:
7037
diff
changeset
|
104 *lock = 1; |
d1816a2696de
Introduced ngx_rwlock_downgrade().
Ruslan Ermilov <ru@nginx.com>
parents:
7037
diff
changeset
|
105 } |
d1816a2696de
Introduced ngx_rwlock_downgrade().
Ruslan Ermilov <ru@nginx.com>
parents:
7037
diff
changeset
|
106 } |
d1816a2696de
Introduced ngx_rwlock_downgrade().
Ruslan Ermilov <ru@nginx.com>
parents:
7037
diff
changeset
|
107 |
d1816a2696de
Introduced ngx_rwlock_downgrade().
Ruslan Ermilov <ru@nginx.com>
parents:
7037
diff
changeset
|
108 |
6103
79ddb0bdb273
Upstream: the "zone" directive.
Ruslan Ermilov <ru@nginx.com>
parents:
6101
diff
changeset
|
109 #else |
79ddb0bdb273
Upstream: the "zone" directive.
Ruslan Ermilov <ru@nginx.com>
parents:
6101
diff
changeset
|
110 |
6270
e769cc88f996
Core: read/write locks are also required by the Stream module.
Piotr Sikora <piotrsikora@google.com>
parents:
6103
diff
changeset
|
111 #if (NGX_HTTP_UPSTREAM_ZONE || NGX_STREAM_UPSTREAM_ZONE) |
6103
79ddb0bdb273
Upstream: the "zone" directive.
Ruslan Ermilov <ru@nginx.com>
parents:
6101
diff
changeset
|
112 |
79ddb0bdb273
Upstream: the "zone" directive.
Ruslan Ermilov <ru@nginx.com>
parents:
6101
diff
changeset
|
113 #error ngx_atomic_cmp_set() is not defined! |
79ddb0bdb273
Upstream: the "zone" directive.
Ruslan Ermilov <ru@nginx.com>
parents:
6101
diff
changeset
|
114 |
6101 | 115 #endif |
6103
79ddb0bdb273
Upstream: the "zone" directive.
Ruslan Ermilov <ru@nginx.com>
parents:
6101
diff
changeset
|
116 |
79ddb0bdb273
Upstream: the "zone" directive.
Ruslan Ermilov <ru@nginx.com>
parents:
6101
diff
changeset
|
117 #endif |