Copilot commented on code in PR #13383: URL: https://github.com/apache/trafficserver/pull/13383#discussion_r3581998952
########## tests/tools/plugins/client_packet_mark.cc: ########## @@ -0,0 +1,164 @@ +/** @file + + Test plugin for the TSHttpTxnClientPacketMarkSet API. + + On each request it reads a target mark from request headers, applies it to the + client-side connection via TSHttpTxnClientPacketMarkSet, then reads the mark + back off the client socket with getsockopt(SO_MARK) and echoes the observed + value into the X-Client-Packet-Mark response header for the AuTest to assert + on. + + @section license License + + 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. + */ + +#include <ts/ts.h> + +extern "C" { +#include <sys/socket.h> +} + +#include <cstdint> +#include <cstdio> +#include <cstdlib> +#include <optional> +#include <string> +#include <string_view> + +namespace +{ +constexpr std::string_view PLUGIN_NAME = "client_packet_mark"; +constexpr std::string_view MARK_HEADER = "X-Set-Mark"; +constexpr std::string_view ECHO_HEADER = "X-Client-Packet-Mark"; + +DbgCtl dbg_ctl{PLUGIN_NAME.data()}; + +/** Read a header field and interpret its value as a 32-bit unsigned quantity. + + Values are parsed with strtoul (base 0), so "0x0000000A" and "10" are both + accepted. Returns std::nullopt if the header is absent. */ +std::optional<uint32_t> +get_uint_header(TSMBuffer bufp, TSMLoc hdr_loc, std::string_view header) +{ + TSMLoc field_loc = TSMimeHdrFieldFind(bufp, hdr_loc, header.data(), static_cast<int>(header.length())); + if (field_loc == TS_NULL_MLOC) { + return std::nullopt; + } + + int value_len = 0; + const char *value_str = TSMimeHdrFieldValueStringGet(bufp, hdr_loc, field_loc, -1, &value_len); + uint32_t result = 0; + if (value_str != nullptr && value_len > 0) { + std::string value(value_str, value_len); + result = static_cast<uint32_t>(strtoul(value.c_str(), nullptr, 0)); + } + TSHandleMLocRelease(bufp, hdr_loc, field_loc); + return result; +} + +/** Create the echo header on the response with the value formatted as 0x%08x. */ +void +set_echo_header(TSMBuffer bufp, TSMLoc hdr_loc, uint32_t value) +{ + char formatted[16]; + int len = snprintf(formatted, sizeof(formatted), "0x%08x", value); + + TSMLoc field_loc = TS_NULL_MLOC; + if (TSMimeHdrFieldCreateNamed(bufp, hdr_loc, ECHO_HEADER.data(), static_cast<int>(ECHO_HEADER.length()), &field_loc) == + TS_SUCCESS) { + TSMimeHdrFieldValueStringSet(bufp, hdr_loc, field_loc, -1, formatted, len); + TSMimeHdrFieldAppend(bufp, hdr_loc, field_loc); + TSHandleMLocRelease(bufp, hdr_loc, field_loc); + } +} + +int +handle_send_response(TSCont /* contp ATS_UNUSED */, TSEvent event, void *edata) +{ + TSHttpTxn txnp = static_cast<TSHttpTxn>(edata); + + if (event != TS_EVENT_HTTP_SEND_RESPONSE_HDR) { + TSHttpTxnReenable(txnp, TS_EVENT_HTTP_CONTINUE); + return 0; + } + + TSMBuffer req_bufp = nullptr; + TSMLoc req_loc = TS_NULL_MLOC; + if (TSHttpTxnClientReqGet(txnp, &req_bufp, &req_loc) != TS_SUCCESS) { + TSError("[%s] Failed to get client request headers", PLUGIN_NAME.data()); + TSHttpTxnReenable(txnp, TS_EVENT_HTTP_CONTINUE); + return 0; + } + + std::optional<uint32_t> mark = get_uint_header(req_bufp, req_loc, MARK_HEADER); + TSHandleMLocRelease(req_bufp, TS_NULL_MLOC, req_loc); + + if (mark.has_value()) { + Dbg(dbg_ctl, "Setting client packet mark to 0x%08x", *mark); + TSHttpTxnClientPacketMarkSet(txnp, static_cast<int>(*mark)); + } Review Comment: Casting the parsed 32-bit mark to `int` can yield implementation-defined results for values >= 0x80000000 (or on platforms where `int` is not 32-bit). Since the API argument is treated as a 32-bit bit-pattern internally, preserve the exact bits when converting from `uint32_t` before calling TSHttpTxnClientPacketMarkSet. ########## include/ts/ts.h: ########## @@ -1585,10 +1585,19 @@ TSReturnCode TSHttpSsnClientFdGet(TSHttpSsn ssnp, int *fdp); /* TS-1008 END */ /** Change packet firewall mark for the client side connection - * - @note The change takes effect immediately - @return TS_SUCCESS if the client connection was modified + Sets the entire client-side packet firewall mark to @a mark; the whole mark is replaced. @a mark + is interpreted as a 32-bit unsigned bit pattern. + + @note The firewall mark is only honored on platforms whose OS supports it, specifically Linux via + @c SO_MARK. On platforms without @c SO_MARK support the call still returns TS_SUCCESS when a + client connection is present, but setting the mark has no effect at the OS layer (it is a safe + no-op). Review Comment: This note implies the mark is "honored" whenever SO_MARK exists, but the implementation uses `safe_setsockopt(SO_MARK)` and ignores its return value. As a result, TSHttpTxnClientPacketMarkSet can return TS_SUCCESS even if the OS rejects the setsockopt (e.g. insufficient privileges), leaving the OS-layer mark unchanged. The API docs should mention that OS-level failures are not reported (or the implementation should be updated separately to surface them). ########## doc/developer-guide/api/functions/TSHttpTxnClientPacketMarkSet.en.rst: ########## @@ -32,11 +32,24 @@ Synopsis Description =========== -Change packet firewall :arg:`mark` for the client side connection. +Change the packet firewall :arg:`mark` for the client side connection. The +entire firewall mark is replaced with :arg:`mark`, which is interpreted as a +32-bit unsigned bit pattern. + +Returns :const:`TS_SUCCESS` when the client connection was modified, and +:const:`TS_ERROR` when there is no client connection to modify. + +.. note:: + + The firewall mark is only honored on platforms whose OS supports it, + specifically Linux via ``SO_MARK``. On platforms without ``SO_MARK`` support + the call still returns :const:`TS_SUCCESS` when a client connection is + present, but setting the mark has no effect at the OS layer (it is a safe + no-op). Review Comment: This note says the mark is honored on Linux via SO_MARK, but the current implementation calls `setsockopt(SO_MARK)` via `safe_setsockopt()` and does not check the return value. That means the function can return TS_SUCCESS even if the OS rejects the option (e.g. missing CAP_NET_ADMIN), so the OS-layer mark may remain unchanged. Please clarify that OS-level failures are not reported (or change the implementation separately). -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
