All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] packet.7: add description of zero protocol for socket
@ 2022-01-25 15:22 Grzegorz Szpetkowski
  2022-01-30 10:48 ` Grzegorz Szpetkowski
  2022-02-09 20:29 ` Alejandro Colomar (man-pages)
  0 siblings, 2 replies; 7+ messages in thread
From: Grzegorz Szpetkowski @ 2022-01-25 15:22 UTC (permalink / raw)
  To: linux-man; +Cc: alx.manpages, mtk.manpages

Dear Maintainers,

According to packet(7), whenever raw packet is created by socket(),
it's immediately running, meaning that internal packet_rcv() handler
will be triggered and socket buffer will begin allocation of sk_buff
until sk_rcvbuf limit is reached.

However, by examination of kernel's internal handler packet_create()
it looks that kernel handles case of zero protocol in a special
manner.
When packet_create() is called with arg protocol = 0,
__register_prot_hook is not executed, meaning running state is still 0
and most notably, packet handler is not added to kernel list (vide
dev_add_pack).

I found this behavior invaluable for solving a subtle issue. When
process creates raw packet socket to listen for (let's say) all
protocols, but limited to a single network interface, then while this
interface can set by bind, it may to be too late due to preemption
(e.g. if real-time scheduling is used) and/or high-rate of packets on
other interfaces, meaning that undesired packets (any count) may be
pulled into the socket buffer.

The proposed patch to define behavior when protocol is to zero with socket().
Please review.

Signed-off-by: Grzegorz Szpetkowski gszpetkowski@gmail.com

diff --git a/man7/packet.7 b/man7/packet.7
index 706efbb54..461444c43 100644
--- a/man7/packet.7
+++ b/man7/packet.7
@@ -47,6 +47,9 @@ is set to
 then all protocols are received.
 All incoming packets of that protocol type will be passed to the packet
 socket before they are passed to the protocols implemented in the kernel.
+When protocol is set to zero, then no packets are received until
+.BR bind (2)
+specifies allowed protocol or to receive all protocols.
 .PP
 In order to create a packet socket, a process must have the
 .B CAP_NET_RAW

Thanks,
Grzegorz

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH] packet.7: add description of zero protocol for socket
  2022-01-25 15:22 [PATCH] packet.7: add description of zero protocol for socket Grzegorz Szpetkowski
@ 2022-01-30 10:48 ` Grzegorz Szpetkowski
  2022-02-12 20:17   ` Alejandro Colomar (man-pages)
  2022-02-09 20:29 ` Alejandro Colomar (man-pages)
  1 sibling, 1 reply; 7+ messages in thread
From: Grzegorz Szpetkowski @ 2022-01-30 10:48 UTC (permalink / raw)
  To: linux-man; +Cc: alx.manpages, mtk.manpages

In addtion of preventing unwanted packets (from other interfaces)
being received between socket and bind, I've found another one. If
desired usage of the raw packet socket is egress-only, meaning user
doesn't want to receive any packet, then such a socket created with
protocol = 0 argument will save both CPU/memory resources of the
process, since Rx protocol handler won't be invoked upon reception of
packets and the associated Rx socket buffer won't be populated,
regardless of ingress traffic load  This can be especially useful in
embedded applications.

Not sure if this can also be added to man, though.

Thanks,
Grzegorz

wt., 25 sty 2022 o 16:22 Grzegorz Szpetkowski <gszpetkowski@gmail.com>
napisał(a):
>
> Dear Maintainers,
>
> According to packet(7), whenever raw packet is created by socket(),
> it's immediately running, meaning that internal packet_rcv() handler
> will be triggered and socket buffer will begin allocation of sk_buff
> until sk_rcvbuf limit is reached.
>
> However, by examination of kernel's internal handler packet_create()
> it looks that kernel handles case of zero protocol in a special
> manner.
> When packet_create() is called with arg protocol = 0,
> __register_prot_hook is not executed, meaning running state is still 0
> and most notably, packet handler is not added to kernel list (vide
> dev_add_pack).
>
> I found this behavior invaluable for solving a subtle issue. When
> process creates raw packet socket to listen for (let's say) all
> protocols, but limited to a single network interface, then while this
> interface can set by bind, it may to be too late due to preemption
> (e.g. if real-time scheduling is used) and/or high-rate of packets on
> other interfaces, meaning that undesired packets (any count) may be
> pulled into the socket buffer.
>
> The proposed patch to define behavior when protocol is to zero with socket().
> Please review.
>
> Signed-off-by: Grzegorz Szpetkowski gszpetkowski@gmail.com
>
> diff --git a/man7/packet.7 b/man7/packet.7
> index 706efbb54..461444c43 100644
> --- a/man7/packet.7
> +++ b/man7/packet.7
> @@ -47,6 +47,9 @@ is set to
>  then all protocols are received.
>  All incoming packets of that protocol type will be passed to the packet
>  socket before they are passed to the protocols implemented in the kernel.
> +When protocol is set to zero, then no packets are received until
> +.BR bind (2)
> +specifies allowed protocol or to receive all protocols.
>  .PP
>  In order to create a packet socket, a process must have the
>  .B CAP_NET_RAW
>
> Thanks,
> Grzegorz

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] packet.7: add description of zero protocol for socket
  2022-01-25 15:22 [PATCH] packet.7: add description of zero protocol for socket Grzegorz Szpetkowski
  2022-01-30 10:48 ` Grzegorz Szpetkowski
@ 2022-02-09 20:29 ` Alejandro Colomar (man-pages)
  2022-02-09 20:32   ` Alejandro Colomar (man-pages)
  1 sibling, 1 reply; 7+ messages in thread
From: Alejandro Colomar (man-pages) @ 2022-02-09 20:29 UTC (permalink / raw)
  To: Grzegorz Szpetkowski; +Cc: mtk.manpages, linux-man

Hello Grzegorz,

On 1/25/22 16:22, Grzegorz Szpetkowski wrote:
> Dear Maintainers,
> 
> According to packet(7), whenever raw packet is created by socket(),
> it's immediately running, meaning that internal packet_rcv() handler
> will be triggered and socket buffer will begin allocation of sk_buff
> until sk_rcvbuf limit is reached.
> 
> However, by examination of kernel's internal handler packet_create()
> it looks that kernel handles case of zero protocol in a special
> manner.
> When packet_create() is called with arg protocol = 0,
> __register_prot_hook is not executed, meaning running state is still 0
> and most notably, packet handler is not added to kernel list (vide
> dev_add_pack).
> 
> I found this behavior invaluable for solving a subtle issue. When
> process creates raw packet socket to listen for (let's say) all
> protocols, but limited to a single network interface, then while this
> interface can set by bind, it may to be too late due to preemption
> (e.g. if real-time scheduling is used) and/or high-rate of packets on
> other interfaces, meaning that undesired packets (any count) may be
> pulled into the socket buffer.
> 
> The proposed patch to define behavior when protocol is to zero with socket().
> Please review.
> 
> Signed-off-by: Grzegorz Szpetkowski gszpetkowski@gmail.com
> 
> diff --git a/man7/packet.7 b/man7/packet.7
> index 706efbb54..461444c43 100644
> --- a/man7/packet.7
> +++ b/man7/packet.7
> @@ -47,6 +47,9 @@ is set to
>  then all protocols are received.
>  All incoming packets of that protocol type will be passed to the packet
>  socket before they are passed to the protocols implemented in the kernel.
> +When protocol is set to zero, then no packets are received until
> +.BR bind (2)
> +specifies allowed protocol or to receive all protocols.man

There's something wrong in that line, or I'm not understanding it.
Could you please review it?

Thanks,

Alex

>  .PP
>  In order to create a packet socket, a process must have the
>  .B CAP_NET_RAW
> 
> Thanks,
> Grzegorz

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] packet.7: add description of zero protocol for socket
  2022-02-09 20:29 ` Alejandro Colomar (man-pages)
@ 2022-02-09 20:32   ` Alejandro Colomar (man-pages)
       [not found]     ` <CAMW=du=BAp_jkLKvhca3=Sv6NDSA+XdUQREhRB+2XfzsLn_x0w@mail.gmail.com>
  0 siblings, 1 reply; 7+ messages in thread
From: Alejandro Colomar (man-pages) @ 2022-02-09 20:32 UTC (permalink / raw)
  To: Grzegorz Szpetkowski; +Cc: mtk.manpages, linux-man



On 2/9/22 21:29, Alejandro Colomar (man-pages) wrote:
>> +specifies allowed protocol or to receive all protocols.man
> 
> There's something wrong in that line, or I'm not understanding it.
> Could you please review it?

Huh, I don't know where that trailing 'man' comes from.  I think I typed
it accidentally.  However, I still think the wording could be improved
for that line.

Cheers,

Alex

-- 
Alejandro Colomar
Linux man-pages comaintainer; https://www.kernel.org/doc/man-pages/
http://www.alejandro-colomar.es/

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] packet.7: add description of zero protocol for socket
       [not found]       ` <09084d8d-5416-2bc1-0e59-cea44111b375@gmail.com>
@ 2022-02-12 19:44         ` Grzegorz Szpetkowski
  2022-02-12 20:34           ` Alejandro Colomar (man-pages)
  0 siblings, 1 reply; 7+ messages in thread
From: Grzegorz Szpetkowski @ 2022-02-12 19:44 UTC (permalink / raw)
  To: Alejandro Colomar (man-pages); +Cc: mtk.manpages, linux-man

Hello Alex,

On 2/12/22 00:28 Alejandro Colomar (man-pages) wrote:
>
>
>
> On 2/11/22 23:08, Grzegorz Szpetkowski wrote:
> > Hello Alex,
> >
> > On 2/9/22 21:32, Alejandro Colomar (man-pages) wrote:
> >> Huh, I don't know where that trailing 'man' comes from.  I think I typed
> >> it accidentally.  However, I still think the wording could be improved
> >> for that line.
> >
> > I tend to agree that wording could be better, but nothing straight
> > comes to my mind.
> > Would something like this work better?
>
> Yes, that reads a bit better, but I have a question.
>
>
> >
> >> +If protocol is set to zero, then no protocols are received until
> >> .BR bind (2)
> >> is subsequently called with with non-zero
>
> What do you mean by subsequently?  Since you already wrote 'until' it
> implies that there will be a future call to bind(2); I'm not sure if
> subsequently adds a slightly different meaning to the sentence, or it
> should be removed for clarity?

Yes, this wording is unfortunate due to the fact the call to bind is optional.
As an example, If user intends to treat the socket as Tx-only, then
bind(2) may be skipped.

> Also a minor typo: repeated 'with'.
>
> Still, I'd remove the negation in the sentence so that it reads
> positively.  We need to break the sentence into two.  What about
> something like the following (please tweak it as necessary to be as
> precise as possible)?:
>
>
>
> If protocol is set to zero, no packets are received.  bind(2)
> can optionally be called with a nonzero sll_protocol to start
> receiving packets for the protocols specified.

Yes, this sounds right to me as well.
Attaching in git diff -u form:

diff --git a/man7/packet.7 b/man7/packet.7
index 706efbb54..d377fe583 100644
--- a/man7/packet.7
+++ b/man7/packet.7
@@ -47,6 +47,11 @@ is set to
 then all protocols are received.
 All incoming packets of that protocol type will be passed to the packet
 socket before they are passed to the protocols implemented in the kernel.
+If protocol is set to zero, no packets are received.
+.BR bind (2)
+can optionally be called with a nonzero
+.IR sll_protocol
+to start receiving packets for the protocols specified.
 .PP
 In order to create a packet socket, a process must have the
 .B CAP_NET_RAW

Please approve.

Thanks,
Grzegorz

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH] packet.7: add description of zero protocol for socket
  2022-01-30 10:48 ` Grzegorz Szpetkowski
@ 2022-02-12 20:17   ` Alejandro Colomar (man-pages)
  0 siblings, 0 replies; 7+ messages in thread
From: Alejandro Colomar (man-pages) @ 2022-02-12 20:17 UTC (permalink / raw)
  To: Grzegorz Szpetkowski; +Cc: mtk.manpages, linux-man

Hi Grzegorz,

On 1/30/22 11:48, Grzegorz Szpetkowski wrote:
> In addtion of preventing unwanted packets (from other interfaces)
> being received between socket and bind, I've found another one. If
> desired usage of the raw packet socket is egress-only, meaning user
> doesn't want to receive any packet, then such a socket created with
> protocol = 0 argument will save both CPU/memory resources of the
> process, since Rx protocol handler won't be invoked upon reception of
> packets and the associated Rx socket buffer won't be populated,
> regardless of ingress traffic load  This can be especially useful in
> embedded applications.
> 
> Not sure if this can also be added to man, though.

I'd say yes.

I'll merge the other patch, and them you can suggest a new one with this
information.

Thanks,

Alex


-- 
Alejandro Colomar
Linux man-pages comaintainer; https://www.kernel.org/doc/man-pages/
http://www.alejandro-colomar.es/

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] packet.7: add description of zero protocol for socket
  2022-02-12 19:44         ` Grzegorz Szpetkowski
@ 2022-02-12 20:34           ` Alejandro Colomar (man-pages)
  0 siblings, 0 replies; 7+ messages in thread
From: Alejandro Colomar (man-pages) @ 2022-02-12 20:34 UTC (permalink / raw)
  To: Grzegorz Szpetkowski; +Cc: mtk.manpages, linux-man

Hi Grzegorz,

On 2/12/22 20:44, Grzegorz Szpetkowski wrote:
> Yes, this sounds right to me as well.
> Attaching in git diff -u form:

I applied the following patch
<http://www.alejandro-colomar.es/src/alx/linux/man-pages/man-pages.git/commit/?id=015e7241b392713f145f94cdfa9c2a68fbcb7f1d>.

Cheers,

Alex

commit 015e7241b392713f145f94cdfa9c2a68fbcb7f1d (HEAD -> main)
Author: Grzegorz Szpetkowski <gszpetkowski@gmail.com>
Date:   Tue Jan 25 16:22:17 2022 +0100

    packet.7: add description of zero protocol for socket

    According to packet(7), whenever raw packet is created by
    socket(), it's immediately running, meaning that internal
    packet_rcv() handler will be triggered and socket buffer will
    begin allocation of sk_buff until sk_rcvbuf limit is reached.

    However, by examination of kernel's internal handler
    packet_create() it looks that kernel handles case of zero protocol
    in a special manner.  When packet_create() is called with arg
    protocol = 0, __register_prot_hook is not executed, meaning
    running state is still 0 and most notably, packet handler is not
    added to kernel list (vide dev_add_pack).

    I found this behavior invaluable for solving a subtle issue.  When
    process creates raw packet socket to listen for (let's say) all
    protocols, but limited to a single network interface, then while
    this interface can set by bind, it may to be too late due to
    preemption (e.g. if real-time scheduling is used) and/or high-rate
    of packets on other interfaces, meaning that undesired packets
    (any count) may be pulled into the socket buffer.

    The idea is that protocol zero means no packets on receive and an
    optional call to bind with nonzero sll_procol will act "as if" the
    originating socket API was called with this (nonzero) protocol.

    The call to bind(2) is optional.  As an example, if user intends
    to treat the socket as Tx-only, then bind(2) may be skipped.

    Reported-by: Grzegorz Szpetkowski <gszpetkowski@gmail.com>
    Link: linux-man@
<https://lore.kernel.org/linux-man/CAMW=dumhWDu6LdhaQCJMskA4yNRBtOHs4iyrG6TP7xRv28AVWA@mail.gmail.com/>
    Cowritten-by: Alejandro Colomar <alx.manpages@gmail.com>
    Signed-off-by: Alejandro Colomar <alx.manpages@gmail.com>
    Signed-off-by Grzegorz Szpetkowski <gszpetkowski@gmail.com>:

diff --git a/man7/packet.7 b/man7/packet.7
index effed18c7..182f628a4 100644
--- a/man7/packet.7
+++ b/man7/packet.7
@@ -47,6 +47,14 @@ is set to
 then all protocols are received.
 All incoming packets of that protocol type will be passed to the packet
 socket before they are passed to the protocols implemented in the kernel.
+If
+.I protocol
+is set to zero,
+no packets are received.
+.BR bind (2)
+can optionally be called with a nonzero
+.IR sll_protocol
+to start receiving packets for the protocols specified.
 .PP
 In order to create a packet socket, a process must have the
 .B CAP_NET_RAW


-- 
Alejandro Colomar
Linux man-pages comaintainer; https://www.kernel.org/doc/man-pages/
http://www.alejandro-colomar.es/

^ permalink raw reply related	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2022-02-12 20:34 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-25 15:22 [PATCH] packet.7: add description of zero protocol for socket Grzegorz Szpetkowski
2022-01-30 10:48 ` Grzegorz Szpetkowski
2022-02-12 20:17   ` Alejandro Colomar (man-pages)
2022-02-09 20:29 ` Alejandro Colomar (man-pages)
2022-02-09 20:32   ` Alejandro Colomar (man-pages)
     [not found]     ` <CAMW=du=BAp_jkLKvhca3=Sv6NDSA+XdUQREhRB+2XfzsLn_x0w@mail.gmail.com>
     [not found]       ` <09084d8d-5416-2bc1-0e59-cea44111b375@gmail.com>
2022-02-12 19:44         ` Grzegorz Szpetkowski
2022-02-12 20:34           ` Alejandro Colomar (man-pages)

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.