Setting Dscp (tos, QoS) for ssh sockets

117 views
Skip to first unread message

Christofer Tornkvist (ctornkvi)

unread,
Feb 17, 2020, 5:12:15 AM2/17/20
to erlang-q...@erlang.org
Hi,

I would like to set the quality of service flag Dscp of the Tos field in the IP packet
of an Ssh socket with the function inet:setopts(Socket, [{tos, Dscp}]).

It should be possible to set the Dscp per IP packet sent.

How do I get hold of the Ssh socket down in my Ssh channel module ?


Regards
/Christofer



Dmytro Lytovchenko

unread,
Feb 17, 2020, 5:23:06 AM2/17/20
to Christofer Tornkvist (ctornkvi), erlang-q...@erlang.org
As we discussed with you before, there are two options right now:

1. From SSH connection handler, using sys module, request its state (private #data{} record) which contains the socket in its 9th field.
2. (Christofer's idea) to replace the socket transport module using undocumented option {transport, {_, Module, _}}

Maybe someone sees other ways to do it?

Per Hedeland

unread,
Feb 17, 2020, 10:04:01 AM2/17/20
to Dmytro Lytovchenko, Christofer Tornkvist (ctornkvi), erlang-q...@erlang.org
On 2020-02-17 11:22, Dmytro Lytovchenko wrote:
> As we discussed with you before, there are two options right now:
>
> 1. From SSH connection handler, using sys module, request its state (private #data{} record) which contains the socket in its 9th field.
> 2. (Christofer's idea) to replace the socket transport module using undocumented option {transport, {_, Module, _}}
>
> Maybe someone sees other ways to do it?

Why would you need to use inet:setopts/2 specifically? Changing DSCP
on the fly is probably not meaningful in general, and from what I know
about your application, not required there (but maybe I'm wrong about
that).

IMHO it would be reasonable to allow most/all options that can be
given to gen_tcp:connect/3,4 and gen_tcp:listen/2 (which both have
'tos' as option), respectively, also for ssh:connect/2,3,4 and
ssh:daemon/2,3 - maybe it is allowed, but if so not documented, as far
as I can see.

Alternatively you can at least for ssh:connect/2,3 apparently pass an
already connected socket from gen_tcp:connect(), where you can pass
'tos' to the latter. It seems the socket that can be passed to
ssh:daemon/2,3 should be from gen_tcp:accept() - it would seem more
natural to me to pass a "listen socket" from gen_tcp:listen() (which
can be passed 'tos'), but I guess you can do the accept-loop outside
ssh and use inet:setopts/2 on the socket from gen_tcp:accept() before
passing it to ssh:daemon/2,3.

--Per

Per Hedeland

unread,
Feb 17, 2020, 2:44:35 PM2/17/20
to erlang-q...@erlang.org
On 2020-02-17 16:03, Per Hedeland wrote:
> On 2020-02-17 11:22, Dmytro Lytovchenko wrote:
> > As we discussed with you before, there are two options right now:
> >
> > 1. From SSH connection handler, using sys module, request its state (private #data{} record) which contains the socket in its 9th field.
> > 2. (Christofer's idea) to replace the socket transport module using undocumented option {transport, {_, Module, _}}
> >
> > Maybe someone sees other ways to do it?
>
> Why would you need to use inet:setopts/2 specifically? Changing DSCP
> on the fly is probably not meaningful in general, and from what I know
> about your application, not required there (but maybe I'm wrong about
> that).

I learned off-list that this requirement comes from RFC 8639, where a
NETCONF client/subscriber can request that the server uses a specific
DSCP value in the "establish-subscription" RPC (which is sent in an
already established session a.k.a. SSH channel). Surely OTP ssh should
support this in a "nice/clean" way...?

> IMHO it would be reasonable to allow most/all options that can be
> given to gen_tcp:connect/3,4 and gen_tcp:listen/2 (which both have
> 'tos' as option), respectively, also for ssh:connect/2,3,4 and
> ssh:daemon/2,3 - maybe it is allowed, but if so not documented, as far
> as I can see.
>
> Alternatively you can at least for ssh:connect/2,3 apparently pass an
> already connected socket from gen_tcp:connect(), where you can pass
> 'tos' to the latter. It seems the socket that can be passed to
> ssh:daemon/2,3 should be from gen_tcp:accept() - it would seem more
> natural to me to pass a "listen socket" from gen_tcp:listen() (which
> can be passed 'tos'), but I guess you can do the accept-loop outside
> ssh and use inet:setopts/2 on the socket from gen_tcp:accept() before
> passing it to ssh:daemon/2,3.

For this alternative, the way to go would rather be to pass the 'tos'
option to gen_tcp:listen(), and have it be "inherited" by the sockets
returned from gen_tcp:accept() - i.e. no need for inet:setopts/2
there. But of course neither alternative supports changing the DSCP
value for an already established connection.

Hans Nilsson R

unread,
Feb 18, 2020, 6:58:32 AM2/18/20
to erlang-q...@erlang.org
It is both possible and documented to give the gen_tcp:connect_options() in a call to ssh:connect.

See

where second from last is a link to

Let's try:

Eshell V10.6.4  (abort with ^G)
1> ssh:start().
ok
2> dbg:start().
{ok,<0.92.0>}
3> dbg:tracer().
{ok,<0.92.0>}
4> dbg:p(all,c).
{ok,[{matched,nonode@nohost,46}]}
5> dbg:tp(gen_tcp,connect,x).
{ok,[{matched,nonode@nohost,2},{saved,x}]}
6>
6> ssh:connect(loopback, 22, [{nodelay,true}, {tos,14}, {raw,1,2,<<3>>}]).
(<0.80.0>) call gen_tcp:connect({127,0,0,1},22,[{active,false},{raw,1,2,<<3>>},{tos,14},{nodelay,true}],infinity)
(<0.80.0>) returned from gen_tcp:connect/4 -> {ok,#Port<0.6>}
{ok,<0.99.0>}
7>

The gen_tcp options in 6> are really passed down to gen_tcp:connect.

/Hans


Från: erlang-questions <erlang-quest...@erlang.org> för Per Hedeland <p...@hedeland.org>
Skickat: den 17 februari 2020 20:44
Till: erlang-q...@erlang.org <erlang-q...@erlang.org>
Ämne: Re: Setting Dscp (tos, QoS) for ssh sockets
 

Per Hedeland

unread,
Feb 18, 2020, 8:09:14 AM2/18/20
to erlang-q...@erlang.org
On 2020-02-18 12:58, Hans Nilsson R wrote:
> It is both possible and documented to give the gen_tcp:connect_options() in a call to ssh:connect.
>
> See
> https://erlang.org/doc/man/ssh.html#type-client_option
>
> where second from last is a link to
> https://erlang.org/doc/man/gen_tcp.html#type-connect_option

Sorry, don't know how I missed that - too much going back and forth
between the "textual" man pages for ssh and gen_tcp, perhaps... And
indeed gen_tcp:listen_option() is also listed for daemon_option(). So
there is no problem whatsoever with setting DSCP at the start of a
connection.

Is there also some "clean" way within ssh to address Christofer's and
Dmytro's original question (below), which I managed to obscure - i.e.
setting DSCP for an already established connection? (Which is needed
for an implementation of RFC 8639.)

--Per

> Let's try:
>
> Eshell V10.6.4 (abort with ^G)
> 1> ssh:start().
> ok
> 2> dbg:start().
> {ok,<0.92.0>}
> 3> dbg:tracer().
> {ok,<0.92.0>}
> 4> dbg:p(all,c).
> {ok,[{matched,nonode@nohost,46}]}
> 5> dbg:tp(gen_tcp,connect,x).
> {ok,[{matched,nonode@nohost,2},{saved,x}]}
> 6>
> 6> ssh:connect(loopback, 22, [{nodelay,true}, {tos,14}, {raw,1,2,<<3>>}]).
> (<0.80.0>) call gen_tcp:connect({127,0,0,1},22,[{active,false},{raw,1,2,<<3>>},{tos,14},{nodelay,true}],infinity)
> (<0.80.0>) returned from gen_tcp:connect/4 -> {ok,#Port<0.6>}
> {ok,<0.99.0>}
> 7>
>
> The gen_tcp options in 6> are really passed down to gen_tcp:connect.
>
> /Hans
>
> --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
> *Från:* erlang-questions <erlang-quest...@erlang.org> för Per Hedeland <p...@hedeland.org>
> *Skickat:* den 17 februari 2020 20:44
> *Till:* erlang-q...@erlang.org <erlang-q...@erlang.org>
> *Ämne:* Re: Setting Dscp (tos, QoS) for ssh sockets

Hans Nilsson R

unread,
Feb 19, 2020, 7:45:51 AM2/19/20
to Per Hedeland, erlang-q...@erlang.org
Hej Per!

So an ssh:setopts(SshConnection, InetOptions) and an ssh:getopts similar to two functions in inet would solve the problem?

/Hans


Från: erlang-questions <erlang-quest...@erlang.org> för Per Hedeland <p...@hedeland.org>
Skickat: den 18 februari 2020 14:08
Till: erlang-q...@erlang.org <erlang-q...@erlang.org>
Ämne: Re: Sv: Setting Dscp (tos, QoS) for ssh sockets
 
On 2020-02-18 12:58, Hans Nilsson R wrote:
> It is both possible and documented to give the gen_tcp:connect_options() in a call to ssh:connect.
>
> See

>
> where second from last is a link to

Hans Nilsson R

unread,
Feb 19, 2020, 10:53:03 AM2/19/20
to Christofer Tornkvist (ctornkvi), Per Hedeland, erlang-q...@erlang.org
Hi Christofer.

Yes, it is the C in
   {ok,C} = ssh:connect(Host, Port...)

and in the channel messages, for example
   {ssh_channel_up, ChanId, C}
   {ssh_cm, C, {data,...}}
    ....

I'll make this asap.
/Hans

Från: Christofer Tornkvist (ctornkvi) <ctor...@cisco.com>
Skickat: den 19 februari 2020 15:25
Till: Hans Nilsson R <hans.r....@ericsson.com>; Per Hedeland <p...@hedeland.org>; erlang-q...@erlang.org <erlang-q...@erlang.org>
Ämne: Sv: Sv: Setting Dscp (tos, QoS) for ssh sockets
 
Hi Hans,

is the SshConnection the same as the Pid of the Connection Handler ?

Or put otherwise, is it so that, my callback channel module should know about this SshConnection ?

If that is the case, then this solution would be great.

Regards
/Christofer

Från: erlang-questions <erlang-quest...@erlang.org> för Hans Nilsson R <hans.r....@ericsson.com>
Skickat: den 19 februari 2020 13:45
Till: Per Hedeland <p...@hedeland.org>; erlang-q...@erlang.org <erlang-q...@erlang.org>
Ämne: Sv: Sv: Setting Dscp (tos, QoS) for ssh sockets
 

Christofer Tornkvist (ctornkvi)

unread,
Feb 19, 2020, 10:59:54 AM2/19/20
to Hans Nilsson R, Per Hedeland, erlang-q...@erlang.org
Hi Hans,

is the SshConnection the same as the Pid of the Connection Handler ?

Or put otherwise, is it so that, my callback channel module should know about this SshConnection ?

If that is the case, then this solution would be great.

Regards
/Christofer
Från: erlang-questions <erlang-quest...@erlang.org> för Hans Nilsson R <hans.r....@ericsson.com>
Skickat: den 19 februari 2020 13:45
Till: Per Hedeland <p...@hedeland.org>; erlang-q...@erlang.org <erlang-q...@erlang.org>
Ämne: Sv: Sv: Setting Dscp (tos, QoS) for ssh sockets
 

Christofer Tornkvist (ctornkvi)

unread,
Feb 20, 2020, 1:50:29 AM2/20/20
to Hans Nilsson R, Per Hedeland, erlang-q...@erlang.org
Hi,

sounds fantastic!

Do you know if it will be part of any minor OTP release in the near 3-4 months or so?

Thanks Hans!
Thanks Per for your dedicated involvement!

Regards
/Christofer

Från: Hans Nilsson R <hans.r....@ericsson.com>
Skickat: den 19 februari 2020 16:52
Till: Christofer Tornkvist (ctornkvi) <ctor...@cisco.com>; Per Hedeland <p...@hedeland.org>; erlang-q...@erlang.org <erlang-q...@erlang.org>

Hans Nilsson R

unread,
Feb 20, 2020, 7:12:57 AM2/20/20
to Christofer Tornkvist (ctornkvi), Per Hedeland, erlang-q...@erlang.org
Hi,
I'm aiming at OTP-22.3 which is planned for last part of March.  But if the tests go well, it will be in the maint branch on github next wwek.
/Hans


Från: Christofer Tornkvist (ctornkvi) <ctor...@cisco.com>
Skickat: den 20 februari 2020 07:49
Reply all
Reply to author
Forward
0 new messages