LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
* How to find out name or id of newly created interface
@ 2021-07-31 20:30 Pali Rohár
  2021-08-01 14:31 ` Andrew Lunn
  2021-08-02 10:02 ` Guillaume Nault
  0 siblings, 2 replies; 9+ messages in thread
From: Pali Rohár @ 2021-07-31 20:30 UTC (permalink / raw)
  To: netdev, linux-kernel

Hello!

Via rtnetlink API (RTM_NEWLINK/NLM_F_CREATE) it is possible to create a
new network interface without specifying neither interface name nor id.
This will let kernel to choose some interface name which does not
conflicts with any already existing network interface. So seems like
ideal way if I do not care about interface names. But at some stage it
is needed to "configure" interface and for this action it is required to
know interface id or name (as some ioctls use interface name instead of
id).

And now I would like to know, how to race-free find out interface name
(or id) of this newly created interface?

Response to RTM_NEWLINK/NLM_F_CREATE packet from kernel contains only
buffer with struct nlmsgerr where is just error number (zero for
success) without any additional information.

I can send another rtnetlink packet to request list of all existing
network interfaces and expect that the interface with the highest id was
that one which was created. But it is racy as another process may
meanwhile create another network interface or it may delete this my
newly created one, prior I send this followup packet.

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

* Re: How to find out name or id of newly created interface
  2021-07-31 20:30 How to find out name or id of newly created interface Pali Rohár
@ 2021-08-01 14:31 ` Andrew Lunn
  2021-08-01 14:38   ` Pali Rohár
  2021-08-02 10:02 ` Guillaume Nault
  1 sibling, 1 reply; 9+ messages in thread
From: Andrew Lunn @ 2021-08-01 14:31 UTC (permalink / raw)
  To: Pali Rohár; +Cc: netdev, linux-kernel

On Sat, Jul 31, 2021 at 10:30:54PM +0200, Pali Rohár wrote:
> Hello!
> 
> Via rtnetlink API (RTM_NEWLINK/NLM_F_CREATE) it is possible to create a
> new network interface without specifying neither interface name nor id.
> This will let kernel to choose some interface name which does not
> conflicts with any already existing network interface. So seems like
> ideal way if I do not care about interface names. But at some stage it
> is needed to "configure" interface and for this action it is required to
> know interface id or name (as some ioctls use interface name instead of
> id).

Hi Pali

Looking at __rtnl_newlink() it looks like you can specify the
dev->ifindex when you request the create. So you can leave the kernel
to pick the name, but pick the if_index from user space.

   Andrew

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

* Re: How to find out name or id of newly created interface
  2021-08-01 14:31 ` Andrew Lunn
@ 2021-08-01 14:38   ` Pali Rohár
  2021-08-02 14:21     ` Andrew Lunn
  0 siblings, 1 reply; 9+ messages in thread
From: Pali Rohár @ 2021-08-01 14:38 UTC (permalink / raw)
  To: Andrew Lunn; +Cc: netdev, linux-kernel

On Sunday 01 August 2021 16:31:33 Andrew Lunn wrote:
> On Sat, Jul 31, 2021 at 10:30:54PM +0200, Pali Rohár wrote:
> > Hello!
> > 
> > Via rtnetlink API (RTM_NEWLINK/NLM_F_CREATE) it is possible to create a
> > new network interface without specifying neither interface name nor id.
> > This will let kernel to choose some interface name which does not
> > conflicts with any already existing network interface. So seems like
> > ideal way if I do not care about interface names. But at some stage it
> > is needed to "configure" interface and for this action it is required to
> > know interface id or name (as some ioctls use interface name instead of
> > id).
> 
> Hi Pali
> 
> Looking at __rtnl_newlink() it looks like you can specify the
> dev->ifindex when you request the create. So you can leave the kernel
> to pick the name, but pick the if_index from user space.
> 
>    Andrew

Hello! This has additional issue that I have to choose some free ifindex
number and it introduce another race condition that other userspace
process may choose same ifindex number. So create request in this case
fails if other userspace process is faster... So it has same race
condition as specifying interface name.

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

* Re: How to find out name or id of newly created interface
  2021-07-31 20:30 How to find out name or id of newly created interface Pali Rohár
  2021-08-01 14:31 ` Andrew Lunn
@ 2021-08-02 10:02 ` Guillaume Nault
  2021-08-02 10:58   ` Pali Rohár
  1 sibling, 1 reply; 9+ messages in thread
From: Guillaume Nault @ 2021-08-02 10:02 UTC (permalink / raw)
  To: Pali Rohár; +Cc: netdev, linux-kernel

On Sat, Jul 31, 2021 at 10:30:54PM +0200, Pali Rohár wrote:
> 
> And now I would like to know, how to race-free find out interface name
> (or id) of this newly created interface?
> 
> Response to RTM_NEWLINK/NLM_F_CREATE packet from kernel contains only
> buffer with struct nlmsgerr where is just error number (zero for
> success) without any additional information.

You'd normally pass the NLM_F_ECHO flag on the netlink request, so the
kernel would echo back a netlink message with all information about the
device it created.

Unfortunately, many netlink handlers don't implement this feature. And
it seems that RTM_NEWLINK is part of them (rtmsg_ifinfo_send() doesn't
provide the 'nlh' argument when it calls rtnl_notify()).

So the proper solution is to implement NLM_F_ECHO support for
RTM_NEWLINK messages (RTM_NEWROUTE is an example of netlink handler
that supports NLM_F_ECHO, see rtmsg_fib()).


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

* Re: How to find out name or id of newly created interface
  2021-08-02 10:02 ` Guillaume Nault
@ 2021-08-02 10:58   ` Pali Rohár
  2021-08-02 13:43     ` Guillaume Nault
  0 siblings, 1 reply; 9+ messages in thread
From: Pali Rohár @ 2021-08-02 10:58 UTC (permalink / raw)
  To: Guillaume Nault; +Cc: netdev, linux-kernel

On Monday 02 August 2021 12:02:38 Guillaume Nault wrote:
> On Sat, Jul 31, 2021 at 10:30:54PM +0200, Pali Rohár wrote:
> > 
> > And now I would like to know, how to race-free find out interface name
> > (or id) of this newly created interface?
> > 
> > Response to RTM_NEWLINK/NLM_F_CREATE packet from kernel contains only
> > buffer with struct nlmsgerr where is just error number (zero for
> > success) without any additional information.
> 
> You'd normally pass the NLM_F_ECHO flag on the netlink request, so the
> kernel would echo back a netlink message with all information about the
> device it created.
> 
> Unfortunately, many netlink handlers don't implement this feature. And
> it seems that RTM_NEWLINK is part of them (rtmsg_ifinfo_send() doesn't
> provide the 'nlh' argument when it calls rtnl_notify()).

I see...

> So the proper solution is to implement NLM_F_ECHO support for
> RTM_NEWLINK messages (RTM_NEWROUTE is an example of netlink handler
> that supports NLM_F_ECHO, see rtmsg_fib()).

Do you know if there is some workaround / other solution which can be
used by userspace applications now? And also with stable kernels (which
obviously do not receive this new NLM_F_ECHO support for RTM_NEWLINK)?

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

* Re: How to find out name or id of newly created interface
  2021-08-02 10:58   ` Pali Rohár
@ 2021-08-02 13:43     ` Guillaume Nault
  2021-08-02 17:23       ` Pali Rohár
  0 siblings, 1 reply; 9+ messages in thread
From: Guillaume Nault @ 2021-08-02 13:43 UTC (permalink / raw)
  To: Pali Rohár; +Cc: netdev, linux-kernel

On Mon, Aug 02, 2021 at 12:58:25PM +0200, Pali Rohár wrote:
> On Monday 02 August 2021 12:02:38 Guillaume Nault wrote:
> > 
> > So the proper solution is to implement NLM_F_ECHO support for
> > RTM_NEWLINK messages (RTM_NEWROUTE is an example of netlink handler
> > that supports NLM_F_ECHO, see rtmsg_fib()).
> 
> Do you know if there is some workaround / other solution which can be
> used by userspace applications now? And also with stable kernels (which
> obviously do not receive this new NLM_F_ECHO support for RTM_NEWLINK)?

I unfortunately can't think of any clean solution. It might be possible
to create the new interface with attributes very unlikely to be used by
external programs and retrieve the interface name and id by monitoring
link creation messages (like 'ip monitor' does). But at this point it's
probably easier to just set the interface name and retry with a
different name every time it conflicted with an existing device.

Maybe someone else could propose less hacky solutions, but I really
can't think of anything else apart from implementing NLM_F_ECHO.


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

* Re: How to find out name or id of newly created interface
  2021-08-01 14:38   ` Pali Rohár
@ 2021-08-02 14:21     ` Andrew Lunn
  2021-08-02 17:25       ` Pali Rohár
  0 siblings, 1 reply; 9+ messages in thread
From: Andrew Lunn @ 2021-08-02 14:21 UTC (permalink / raw)
  To: Pali Rohár; +Cc: netdev, linux-kernel

> Hello! This has additional issue that I have to choose some free ifindex
> number and it introduce another race condition that other userspace
> process may choose same ifindex number. So create request in this case
> fails if other userspace process is faster... So it has same race
> condition as specifying interface name.

O.K. if you don't want to deal with retries, you are going to have to
modify the return value. The nice thing is, its netlink. So you can
add additional attributes, and not break backwards compatibility. User
space should ignore all attributes it does not expect.

But i suspect the architecture of the code is not going to make it
easy.

	Andrew

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

* Re: How to find out name or id of newly created interface
  2021-08-02 13:43     ` Guillaume Nault
@ 2021-08-02 17:23       ` Pali Rohár
  0 siblings, 0 replies; 9+ messages in thread
From: Pali Rohár @ 2021-08-02 17:23 UTC (permalink / raw)
  To: Guillaume Nault; +Cc: netdev, linux-kernel

On Monday 02 August 2021 15:43:20 Guillaume Nault wrote:
> On Mon, Aug 02, 2021 at 12:58:25PM +0200, Pali Rohár wrote:
> > On Monday 02 August 2021 12:02:38 Guillaume Nault wrote:
> > > 
> > > So the proper solution is to implement NLM_F_ECHO support for
> > > RTM_NEWLINK messages (RTM_NEWROUTE is an example of netlink handler
> > > that supports NLM_F_ECHO, see rtmsg_fib()).
> > 
> > Do you know if there is some workaround / other solution which can be
> > used by userspace applications now? And also with stable kernels (which
> > obviously do not receive this new NLM_F_ECHO support for RTM_NEWLINK)?
> 
> I unfortunately can't think of any clean solution. It might be possible
> to create the new interface with attributes very unlikely to be used by
> external programs and retrieve the interface name and id by monitoring
> link creation messages (like 'ip monitor' does). But at this point it's
> probably easier to just set the interface name and retry with a
> different name every time it conflicted with an existing device.

"set interface name and retry" is what I'm using now... And looks like
it is the only stable solution for now.

I was already thinking about monitoring link creation messages... if
there is not some stable message ordering (e.g. order of response and
monitor message) but I have not deduced anything from the code.

> Maybe someone else could propose less hacky solutions, but I really
> can't think of anything else apart from implementing NLM_F_ECHO.

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

* Re: How to find out name or id of newly created interface
  2021-08-02 14:21     ` Andrew Lunn
@ 2021-08-02 17:25       ` Pali Rohár
  0 siblings, 0 replies; 9+ messages in thread
From: Pali Rohár @ 2021-08-02 17:25 UTC (permalink / raw)
  To: Andrew Lunn; +Cc: netdev, linux-kernel

On Monday 02 August 2021 16:21:05 Andrew Lunn wrote:
> > Hello! This has additional issue that I have to choose some free ifindex
> > number and it introduce another race condition that other userspace
> > process may choose same ifindex number. So create request in this case
> > fails if other userspace process is faster... So it has same race
> > condition as specifying interface name.
> 
> O.K. if you don't want to deal with retries, you are going to have to
> modify the return value. The nice thing is, its netlink. So you can
> add additional attributes, and not break backwards compatibility. User
> space should ignore all attributes it does not expect.

Guillaume already proposed to implement NLM_F_ECHO...

> But i suspect the architecture of the code is not going to make it
> easy.
> 
> 	Andrew

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

end of thread, other threads:[~2021-08-02 17:25 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-31 20:30 How to find out name or id of newly created interface Pali Rohár
2021-08-01 14:31 ` Andrew Lunn
2021-08-01 14:38   ` Pali Rohár
2021-08-02 14:21     ` Andrew Lunn
2021-08-02 17:25       ` Pali Rohár
2021-08-02 10:02 ` Guillaume Nault
2021-08-02 10:58   ` Pali Rohár
2021-08-02 13:43     ` Guillaume Nault
2021-08-02 17:23       ` Pali Rohár

This is a public inbox, see mirroring instructions
on how to clone and mirror all data and code used for this inbox