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

refactor: Replace ToS with DSCP #2564

Closed
wants to merge 2 commits into from
Closed

Conversation

dgcampea
Copy link
Contributor

@dgcampea dgcampea commented Feb 2, 2022

IP TOS was superseeded by DSCP in RFC 2474 (December 1998).

I've tested with wireshark and I can confirm that the DSCP options are applied (transmission-daemon).
It also passed make test.

Copy link
Member

@ckerr ckerr left a comment

Choose a reason for hiding this comment

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

Looks like this is FTBFS on a few platforms, e.g. here on FreeBSD: https://trtc.mikedld.com/buildConfiguration/Transmission_Sanity_FreeBSDAmd64cyassl/170967?showRootCauses=true&expandBuildProblemsSection=true&expandBuildChangesSection=true&showLog=170967_963_628.1051

Also, please use code_style.sh (which calls clang-format) to format the code

Thanks!

@dgcampea
Copy link
Contributor Author

dgcampea commented Feb 2, 2022

Looks like this is FTBFS on a few platforms, e.g. here on FreeBSD: https://trtc.mikedld.com/buildConfiguration/Transmission_Sanity_FreeBSDAmd64cyassl/170967?showRootCauses=true&expandBuildProblemsSection=true&expandBuildChangesSection=true&showLog=170967_963_628.1051

I'll guess it broke on FreeBSD because <netinet/ip.h> doesn't include <netinet/in.h> though I don't have a FreeBSD machine around to readily test it. I've force-pushed a change to include both headers to test the hypothesis.

Also, please use code_style.sh (which calls clang-format) to format the code

When I ran code_style.sh it somehow mangled the code so badly that it wouldn't compile anymore. I tried clang-format manually on libtransmission/net.h and it mangles the lines after the DSCP macro definitions.

@ckerr
Copy link
Member

ckerr commented Feb 2, 2022

When I ran code_style.sh it somehow mangled the code so badly that it wouldn't compile anymore. I tried clang-format manually on libtransmission/net.h and it mangles the lines after the DSCP macro definitions.

Yikes, OK don't sweat the code_style then -- different clang-format versions can give different outputs and I can re-run it after this PR gets merged. But if you are using clang-format (12, 13, or 14) and the only issue is that it's breaking macros in new code, you could try wrapping them in

// clang-format off
...
// clang-format on

@ckerr
Copy link
Member

ckerr commented Feb 2, 2022

The 'Sanity' CI is still failing on the BSDs. Please LMK if you can't see the build logs when logging in as a guest to the CI box

@dgcampea
Copy link
Contributor Author

dgcampea commented Feb 2, 2022

Zzzz of course, I amended the commit without staging the change. Hopefully it will go through this time

@dgcampea
Copy link
Contributor Author

dgcampea commented Feb 2, 2022

But if you are using clang-format (12, 13, or 14) and the only issue is that it's breaking macros in new code

Using clang-format 13 here, the line it complains about is the std::map one.

IP TOS was superseeded by DSCP in RFC 2474 (December 1998).
Comment on lines +114 to +177
/*
* Definitions for DiffServ Codepoints as per RFCs 2474, 3246, 4594 & 8622.
* Not all are guaranteed to be defined in <netinet/ip.h> so we keep a copy of the DSCP values.
*/
#ifndef IPTOS_DSCP_AF11
#define IPTOS_DSCP_AF11 0x28
#define IPTOS_DSCP_AF12 0x30
#define IPTOS_DSCP_AF13 0x38
#define IPTOS_DSCP_AF21 0x48
#define IPTOS_DSCP_AF22 0x50
#define IPTOS_DSCP_AF23 0x58
#define IPTOS_DSCP_AF31 0x68
#define IPTOS_DSCP_AF32 0x70
#define IPTOS_DSCP_AF33 0x78
#define IPTOS_DSCP_AF41 0x88
#define IPTOS_DSCP_AF42 0x90
#define IPTOS_DSCP_AF43 0x98
#define IPTOS_DSCP_EF 0xb8
#endif

#ifndef IPTOS_DSCP_CS0
#define IPTOS_DSCP_CS0 0x00
#define IPTOS_DSCP_CS1 0x20
#define IPTOS_DSCP_CS2 0x40
#define IPTOS_DSCP_CS3 0x60
#define IPTOS_DSCP_CS4 0x80
#define IPTOS_DSCP_CS5 0xa0
#define IPTOS_DSCP_CS6 0xc0
#define IPTOS_DSCP_CS7 0xe0
#endif

#ifndef IPTOS_DSCP_EF
#define IPTOS_DSCP_EF 0xb8
#endif

#ifndef IPTOS_DSCP_LE
#define IPTOS_DSCP_LE 0x04
#endif

const std::map<const std::string, int> DSCPvalues {
{ "none", INT_MAX }, // CS0 has value 0 but we want 'none' to mean OS default
{ "", INT_MAX }, // an alias to 'none'
{ "af11", IPTOS_DSCP_AF11 },
{ "af12", IPTOS_DSCP_AF12 },
{ "af13", IPTOS_DSCP_AF13 },
{ "af21", IPTOS_DSCP_AF21 },
{ "af22", IPTOS_DSCP_AF22 },
{ "af23", IPTOS_DSCP_AF23 },
{ "af31", IPTOS_DSCP_AF31 },
{ "af32", IPTOS_DSCP_AF32 },
{ "af33", IPTOS_DSCP_AF33 },
{ "af41", IPTOS_DSCP_AF41 },
{ "af42", IPTOS_DSCP_AF42 },
{ "af43", IPTOS_DSCP_AF43 },
{ "cs0", IPTOS_DSCP_CS0 },
{ "cs1", IPTOS_DSCP_CS1 },
{ "cs2", IPTOS_DSCP_CS2 },
{ "cs3", IPTOS_DSCP_CS3 },
{ "cs4", IPTOS_DSCP_CS4 },
{ "cs5", IPTOS_DSCP_CS5 },
{ "cs6", IPTOS_DSCP_CS6 },
{ "cs7", IPTOS_DSCP_CS7 },
{ "ef", IPTOS_DSCP_EF },
{ "le", IPTOS_DSCP_LE }
Copy link
Member

Choose a reason for hiding this comment

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

This exposes an unnecessary amount of implementation info into the header.

Maybe just declare a couple of accessor functions, e.g.

std::optional<int> tr_netDscpGetValue(std::string_view name);

std::optional<std::string_view> tr_netDscpGetName(int value);

...and then move all the rest of this into net.cc

#define IPTOS_DSCP_LE 0x04
#endif

const std::map<const std::string, int> DSCPvalues {
Copy link
Member

@ckerr ckerr Feb 3, 2022

Choose a reason for hiding this comment

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

These are all known at compile-time so there's no need to have a std::map or std::string which both require heap allocation. This should be a constexpr std::array<std::pair<std::string_view, int>, N> that we could sort at compile time and then bsearch.

Though TBH this will probably only be called a handful of times per session; linear search would also be fine

Comment on lines +134 to +143
#ifndef IPTOS_DSCP_CS0
#define IPTOS_DSCP_CS0 0x00
#define IPTOS_DSCP_CS1 0x20
#define IPTOS_DSCP_CS2 0x40
#define IPTOS_DSCP_CS3 0x60
#define IPTOS_DSCP_CS4 0x80
#define IPTOS_DSCP_CS5 0xa0
#define IPTOS_DSCP_CS6 0xc0
#define IPTOS_DSCP_CS7 0xe0
#endif
Copy link
Member

Choose a reason for hiding this comment

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

If the DSCP_AF_ and DSCP_CS values are known and never going to change, maybe just make them constexpr ints e.g. static constexpr int TR_IPTOS_DSCP_CS4 = 0x80; so that we never have to worry about header conflicts?

#include <string_view>

#include "utils.h"
Copy link
Member

Choose a reason for hiding this comment

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

not needed in this header?

@@ -9,15 +9,22 @@
#endif

#include <cstddef> // size_t
#include <climits>
Copy link
Member

Choose a reason for hiding this comment

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

not needed in this header?

Comment on lines +266 to +268
for (auto it = DSCPvalues.begin(); it != DSCPvalues.end(); ++it)
if (it->second == value)
return it->first;
Copy link
Member

Choose a reason for hiding this comment

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

consider find_if or

for (auto const& [name, val] : DSCPvalues)
{
    if (val == value)
    {
        return name;
    }
}

@dgcampea
Copy link
Contributor Author

dgcampea commented Feb 5, 2022

Hi @ckerr, thanks for the detailed review though I'm afraid I can't improve this PR much further, at least not without learning C++ the right way (you could almost say this PR was hammered together using a search engine and some rudimentary C knowledge).
If you're OK with filling or refactoring the problematic parts, I'm OK with leaving this branch open for edits (or closing it).

@ckerr
Copy link
Member

ckerr commented Feb 7, 2022

OK. Please leave it up for a bit and I'll create a followup PR

@ckerr ckerr marked this pull request as draft February 7, 2022 01:53
@ckerr
Copy link
Member

ckerr commented Feb 10, 2022

@dgcampea do you have any thoughs on #2594 ?

I've kept the same settings.json names and command-line arguments that exist in Transmission 3.0 so that existing settings won't break, but they now support the DSCP class names as well as the older ToS names, and the default for new users is a DSCP class name.

If you have feedback, let's continue this in 2594

@ckerr ckerr closed this Feb 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants