Skip to content
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

Basic PROXY protocol support for Squid-to-peer connections #281

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

eduard-bagdasaryan
Copy link

No description provided.

src/FwdState.cc Outdated

MemBuf mb;
mb.init();
header.packInto(mb);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, MemBuf::grow() asserts on overlfows. A lot of Squid code ignores that fact, and the default 2GB max_capacity value makes such assertions rare. I believe we can also ignore them just like, say, HttpHeader::packInto() does. It is a negative factor when it comes to SBuf-vs-MemBuf decision, but it is not a critical one.

For now, please continue to use MemBuf. We will revisit this decision when serialization API is finalized.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I switched PROXY protocol header packing code to use new serialization API, but this sendProxyProtoHeaderIfNeeded() code still uses MemBuf for Comm::Write(), as I suggested earlier. I added a TODO to get rid of this deprecated MemBuf addition, but let's not change/improve that for now -- we have much bigger issues to solve!

ProxyProtocol::Header::Header(const SBuf &ver, const Two::Command cmd):
version_(ver),
command_(cmd),
ignoreAddresses_(false)
{}

void
ProxyProtocol::Header::packInto(MemBuf &mb) const

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this code survives, it should become:

Suggested change
ProxyProtocol::Header::packInto(MemBuf &mb) const
ProxyProtocol::Header::packInto(Packable &out) const

However, for now, please continue to use MemBuf (and do not focus on the quality of this packing code -- as long as it produces the right serialization/bytes-on-the-wire). I am investigating a possible replacement/upgrade, but that will take some time, and there is no good reason to block your progress on that.

// for TCP/UDP over IPv4, len = 12
// for TCP/UDP over IPv6, len = 36
const uint16_t len = 12;
mb.append(reinterpret_cast<const char *>(&len), sizeof(len));
Copy link

@rousskov rousskov Jan 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(AFAICT from the specs, we do not write the address length. It is determined by the address family (written earlier). See also: ProxyProtocol::Two::ParseAddresses().) Edit: I missed tokHeader.pstring16("header") that extracts these two bytes before calling ParseAddresses().

(BTW, ) this may not be the right way to write a 16-bit integer. See how Parser::BinaryTokenizer::uint16() handles endianness.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I addressed this concern in branch commit 8c367fb.

inline const auto &
Magic()
{
static const auto magic = new SBuf("PROXY", 5);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do not inline functions that use static variables, especially when we do not need to optimize their performance.

namespace Two {
/// magic octet prefix for PROXY protocol version 2
inline const auto &
Magic()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these declarations belong to proxyp/Elements.h (with definitions moved to Elements.cc).

This implementation is still untested. I also plan to remove the failed
attempt at (hopefully premature) optimization to simplify further.

This commit should fix PROXY protocol "tail" length packing.

Also added code to pack TLVs. TLVs are currently absent, but
ProxyProtocol::Header::pack() should not know that.
If we ever implement locking (removed in previous commit), this position
will also help to "automatically" check the lock before any object
updates.

Also fixed method const-correctness.
Copy link

@rousskov rousskov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also fix "make check" when you get a chance.

// for TCP/UDP over IPv4, len = 12
// for TCP/UDP over IPv6, len = 36
const uint16_t len = 12;
mb.append(reinterpret_cast<const char *>(&len), sizeof(len));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I addressed this concern in branch commit 8c367fb.

src/FwdState.cc Outdated

MemBuf mb;
mb.init();
header.packInto(mb);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I switched PROXY protocol header packing code to use new serialization API, but this sendProxyProtoHeaderIfNeeded() code still uses MemBuf for Comm::Write(), as I suggested earlier. I added a TODO to get rid of this deprecated MemBuf addition, but let's not change/improve that for now -- we have much bigger issues to solve!


BinaryPacker packer;
header.pack(packer);
const auto packed = packer.packed();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When you update this code to generate the PROXY protocol header before opening a connection, please store it as SBuf. Convert to MemBuf only when it is time to write it (using the freshly opened connection).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants