-
Notifications
You must be signed in to change notification settings - Fork 874
Deliver VCONN_CLOSE for parked TLS hooks; fix SNI queue accounting #13406
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
094e66f
rate_limit: balance the SNI active-slot counter for queued connections
moonchen 46ace45
rate_limit: add an SNI reject-teardown autest
moonchen e724b54
rate_limit tests: annotate helpers and create the FIFO atomically
moonchen 313a70f
Deliver VCONN_CLOSE for connections parked in a TLS handshake hook
moonchen b175601
rate_limit: address review feedback
moonchen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
tests/gold_tests/pluginTest/rate_limit/rate_limit_sni_expiry.test.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| ''' | ||
| Regression test for the max_age expiry branch of the rate_limit SNI queue accounting. A | ||
| queued connection never reserves a slot, so when the sweep expires it the plugin must | ||
| detach it rather than release a slot it never held; otherwise the expiry underflows the | ||
| active-slot counter and the next reserve() trips a release assertion, aborting the server. | ||
| ATS must survive the expiry. | ||
| ''' | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import os | ||
|
|
||
| Test.Summary = __doc__ | ||
|
|
||
| Test.SkipUnless(Condition.PluginExists('rate_limit.so')) | ||
|
|
||
|
|
||
| class RateLimitSniExpiryTest: | ||
| """Age a queued connection out via max_age and assert the active-slot counter stays balanced.""" | ||
|
|
||
| def __init__(self) -> None: | ||
| tr = Test.AddTestRun('rate_limit SNI queue max_age expiry') | ||
| self._configure_trafficserver() | ||
| self._configure_client(tr) | ||
|
|
||
| def _configure_trafficserver(self) -> None: | ||
| ts = Test.MakeATSProcess('ts', enable_tls=True, enable_cache=False) | ||
| self._ts = ts | ||
| ts.addDefaultSSLFiles() | ||
| for line in ['ssl_multicert:', ' - dest_ip: "*"', ' ssl_cert_name: server.pem', ' ssl_key_name: server.key']: | ||
| ts.Disk.ssl_multicert_yaml.AddLine(line) | ||
|
|
||
| # One concurrent handshake for this SNI, a one-deep queue, and a 1s max age so the | ||
| # sweep expires the queued connection. Named .config (not .yaml) so autest treats it | ||
| # as a plain config file; the plugin parses it as YAML regardless. | ||
| ts.Disk.MakeConfigFile('rate_limit.config').AddLines( | ||
| [ | ||
| 'selector:', | ||
| ' - sni: rate.limited.com', | ||
| ' limit: 1', | ||
| ' queue:', | ||
| ' size: 1', | ||
| ' max_age: 1', | ||
| ]) | ||
| ts.Disk.plugin_config.AddLine(f'rate_limit.so {ts.Variables.CONFIGDIR}/rate_limit.config') | ||
|
|
||
| # Disable the freelist / ProxyAllocator so allocation behavior is not a confound; the | ||
| # abort under test is a release-assertion, and this keeps the run representative of CI. | ||
| ts.Command += ' -f -F' | ||
|
|
||
| ts.Disk.records_config.update( | ||
| { | ||
| 'proxy.config.ssl.server.cert.path': ts.Variables.SSLDir, | ||
| 'proxy.config.ssl.server.private_key.path': ts.Variables.SSLDir, | ||
| 'proxy.config.diags.debug.enabled': 1, | ||
| 'proxy.config.diags.debug.tags': 'rate_limit', | ||
| }) | ||
|
|
||
| # The expiry branch is actually reached... | ||
| ts.Disk.traffic_out.Content = Testers.ContainsExpression('too old', 'a queued connection was expired') | ||
| # ...and expiring it does not underflow the active-slot counter into the release assertion. | ||
| ts.Disk.traffic_out.Content += Testers.ExcludesExpression( | ||
| '_active <= _limit|received signal', 'expiring a queued connection must not underflow and abort ATS') | ||
|
|
||
| def _configure_client(self, tr: 'TestRun') -> None: | ||
| ts = self._ts | ||
| client = os.path.join(Test.TestDirectory, 'rate_limit_sni_expiry_client.sh') | ||
| tr.Processes.Default.Command = f'bash {client} 127.0.0.1 {ts.Variables.ssl_port} rate.limited.com' | ||
| tr.Processes.Default.ReturnCode = 0 | ||
| tr.Processes.Default.StartBefore(ts) | ||
| tr.Processes.Default.Streams.stdout = Testers.ContainsExpression('rate_limit-expiry-done', 'the client ran to completion') | ||
|
|
||
|
|
||
| RateLimitSniExpiryTest() |
77 changes: 77 additions & 0 deletions
77
tests/gold_tests/pluginTest/rate_limit/rate_limit_sni_expiry_client.sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| #!/usr/bin/env bash | ||
| # | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # | ||
| # Exercise the max_age EXPIRY branch of the rate_limit SNI queue accounting. A queued | ||
| # connection never reserves a slot; when the sweep expires it (max_age), it must be | ||
| # detached so its close does not release a slot it never held. Otherwise the expiry is an | ||
| # unmatched decrement of the active-slot counter, and -- combined with the holder's own | ||
| # close -- the counter wraps below zero and the limiter's release assertion aborts ATS. | ||
| # | ||
| # 1. holder completes its handshake and holds the single slot (counter = 1); | ||
| # 2. one connection enqueues (slot full) and stays parked -- it is NOT disconnected, so | ||
| # only the sweep's max_age expiry removes it; | ||
| # 3. after max_age the sweep errors it out -> (unfixed) unmatched decrement -> counter 1->0; | ||
| # 4. the holder is closed; its matched decrement lands on the understated counter -> wrap; | ||
| # 5. a probe connection's reserve() observes the wrapped counter and the assertion aborts. | ||
| # | ||
| # args: host port sni | ||
| set -u | ||
| host="$1" | ||
| port="$2" | ||
| sni="$3" | ||
|
|
||
| OSSL="openssl s_client -connect ${host}:${port} -servername ${sni} -quiet -no_ign_eof" | ||
|
|
||
| # Run a command in the background and terminate it after a deadline. coreutils "timeout" is not | ||
| # available everywhere (notably macOS), so do it with sleep and kill. | ||
| run_for() { | ||
| deadline="$1" | ||
| shift | ||
| "$@" & | ||
| target=$! | ||
| ( | ||
| sleep "${deadline}" | ||
| kill -TERM "${target}" 2>/dev/null | ||
| ) & | ||
| } | ||
|
|
||
| # 1. Holder: hold the single slot. Its stdin is a FIFO on fd 3 so we end it in step 4. | ||
| fifo_dir="$(mktemp -d "${TMPDIR:-/tmp}/rl_holder.XXXXXX")" | ||
| fifo="${fifo_dir}/fifo" | ||
| mkfifo "$fifo" | ||
| ${OSSL} <"$fifo" >/dev/null 2>&1 & | ||
| exec 3<>"$fifo" | ||
| rm -rf "$fifo_dir" | ||
| sleep 3 # let the holder reserve the one slot | ||
|
|
||
| # 2. One queued connection: enqueues and stays parked at the ClientHello hook (not killed), | ||
| # so the sweep's max_age expiry -- not a disconnect or a resume -- is what removes it. | ||
| ${OSSL} </dev/null >/dev/null 2>&1 & | ||
| queued=$! | ||
| sleep 3 # > max_age (1s) + sweeps: the expiry path errors the queued connection out | ||
|
|
||
| # 4. End the holder: its matched decrement lands on the (unfixed) understated counter. | ||
| exec 3>&- | ||
| sleep 2 | ||
|
|
||
| # 5. Probe: its reserve() reads the counter; if it wrapped, the release assertion aborts. | ||
| run_for 2 sh -c "${OSSL} </dev/null >/dev/null 2>&1" | ||
| kill "${queued}" 2>/dev/null || true | ||
| sleep 1 | ||
|
|
||
| echo "rate_limit-expiry-done" |
89 changes: 89 additions & 0 deletions
89
tests/gold_tests/pluginTest/rate_limit/rate_limit_sni_queue.test.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| ''' | ||
| Regression test for a queue-accounting balance bug in the rate_limit SNI limiter: a | ||
| queued connection never reserves a slot, but its VCONN_CLOSE unconditionally releases | ||
| one, so a queued connection that closes underflows the active-slot counter and the next | ||
| reserve() trips a release assertion, aborting the server. ATS must survive the queue | ||
| churn. | ||
| ''' | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import os | ||
|
|
||
| Test.Summary = __doc__ | ||
|
|
||
| Test.SkipUnless(Condition.PluginExists('rate_limit.so')) | ||
|
|
||
|
|
||
| class RateLimitSniQueueTest: | ||
| """Churn the rate_limit SNI queue and assert the active-slot counter never underflows.""" | ||
|
|
||
| def __init__(self) -> None: | ||
| tr = Test.AddTestRun('rate_limit SNI queue accounting') | ||
| self._configure_trafficserver() | ||
| self._configure_client(tr) | ||
|
|
||
| def _configure_trafficserver(self) -> None: | ||
| ts = Test.MakeATSProcess('ts', enable_tls=True, enable_cache=False) | ||
| self._ts = ts | ||
| ts.addDefaultSSLFiles() | ||
| for line in ['ssl_multicert:', ' - dest_ip: "*"', ' ssl_cert_name: server.pem', ' ssl_key_name: server.key']: | ||
| ts.Disk.ssl_multicert_yaml.AddLine(line) | ||
|
|
||
| # One concurrent handshake for this SNI and a queue that admits exactly one more. | ||
| # No rate and no max_age -- the sweep's resume path alone drives the scenario, with | ||
| # no rate-bucket or expiry timing to confound it. Named .config (not .yaml) so autest | ||
| # treats it as a plain config file; the plugin parses it as YAML regardless. | ||
| ts.Disk.MakeConfigFile('rate_limit.config').AddLines( | ||
| [ | ||
| 'selector:', | ||
| ' - sni: rate.limited.com', | ||
| ' limit: 1', | ||
| ' queue:', | ||
| ' size: 1', | ||
| ]) | ||
| ts.Disk.plugin_config.AddLine(f'rate_limit.so {ts.Variables.CONFIGDIR}/rate_limit.config') | ||
|
|
||
| # Disable the freelist / ProxyAllocator so freed objects are really released rather | ||
| # than recycled, keeping allocation reuse from masking a stale access. | ||
| ts.Command += ' -f -F' | ||
|
|
||
| ts.Disk.records_config.update( | ||
| { | ||
| 'proxy.config.ssl.server.cert.path': ts.Variables.SSLDir, | ||
| 'proxy.config.ssl.server.private_key.path': ts.Variables.SSLDir, | ||
| 'proxy.config.diags.debug.enabled': 1, | ||
| 'proxy.config.diags.debug.tags': 'rate_limit', | ||
| }) | ||
|
|
||
| # The queue path is reached... | ||
| ts.Disk.traffic_out.Content = Testers.ContainsExpression('Queueing the VC', 'a connection was queued') | ||
| # ...and the active-slot counter never underflows into the release assertion. Match | ||
| # both the specific assertion (pins the failure to this bug) and the generic abort. | ||
| ts.Disk.traffic_out.Content += Testers.ExcludesExpression( | ||
| '_active <= _limit|received signal', 'the active-slot counter must not underflow and abort ATS') | ||
|
|
||
| def _configure_client(self, tr: 'TestRun') -> None: | ||
| ts = self._ts | ||
| client = os.path.join(Test.TestDirectory, 'rate_limit_sni_queue_client.sh') | ||
| tr.Processes.Default.Command = f'bash {client} 127.0.0.1 {ts.Variables.ssl_port} rate.limited.com' | ||
| tr.Processes.Default.ReturnCode = 0 | ||
| tr.Processes.Default.StartBefore(ts) | ||
| tr.Processes.Default.Streams.stdout = Testers.ContainsExpression( | ||
| 'rate_limit-queue-crash-done', 'the client ran to completion') | ||
|
|
||
|
|
||
| RateLimitSniQueueTest() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how big can queue get?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unbounded by default, which I agree is not sensible.
_max_queueis0(no queue) until aqueue:block appears, and thenlimiter.h:214is:A
queue:block without asize:getsUINT32_MAX, andfull()(_size >= max_queue()) can then never trip. The practical ceilingbecomes
proxy.config.net.connections_throttle, 30000 by default._queueis astd::deque, so an erase from the middle is O(n). I'm filing an issue: #13511 to update defaults, and revisit this queue data structure.