2008-12-05 08:18:13

by Jiri Olsa

[permalink] [raw]
Subject: netlink - notify when the socket gets closed

Hi,

I'm using netlink in my module. Based on the communication,
this module dynamically creates some internal structure, which
needs to be destroyed when the user level socket is closed (or
the application dies).

I found I could use the netlink_register_notifier function to register
NETLINK_URELEASE callback during the netlink_release function.
But since my module uses the multicast netlink socket communication,
it wont be called:

static int netlink_release(struct socket *sock)
{
...

if (nlk->pid && !nlk->subscriptions) {
struct netlink_notify n = {
.net = sock_net(sk),
.protocol = sk->sk_protocol,
.pid = nlk->pid,
};
atomic_notifier_call_chain(&netlink_chain,
NETLINK_URELEASE, &n);
}
...

Whats the reason this callback is not called for multicast sockets?

To workaround it I created simple misc device which the user application
opens before creating the netlink socket. This way I get some callbacks
inside the module when the application dies, at least.. pretty ugly :)

Is there a netlink mechanism to be notified when the netlink socket is
closed on the user level side? (when using multicast communication)

Or is there any other design I could use, since I think I'm not alone in
using internal module data which needs to be removed once the application dies.

thanks,
jirka


2008-12-06 03:56:43

by Andrew Morton

[permalink] [raw]
Subject: Re: netlink - notify when the socket gets closed

(cc [email protected])

On Fri, 5 Dec 2008 09:17:51 +0100 "m m" <[email protected]> wrote:

> Hi,
>
> I'm using netlink in my module. Based on the communication,
> this module dynamically creates some internal structure, which
> needs to be destroyed when the user level socket is closed (or
> the application dies).
>
> I found I could use the netlink_register_notifier function to register
> NETLINK_URELEASE callback during the netlink_release function.
> But since my module uses the multicast netlink socket communication,
> it wont be called:
>
> static int netlink_release(struct socket *sock)
> {
> ...
>
> if (nlk->pid && !nlk->subscriptions) {
> struct netlink_notify n = {
> .net = sock_net(sk),
> .protocol = sk->sk_protocol,
> .pid = nlk->pid,
> };
> atomic_notifier_call_chain(&netlink_chain,
> NETLINK_URELEASE, &n);
> }
> ...
>
> Whats the reason this callback is not called for multicast sockets?
>
> To workaround it I created simple misc device which the user application
> opens before creating the netlink socket. This way I get some callbacks
> inside the module when the application dies, at least.. pretty ugly :)
>
> Is there a netlink mechanism to be notified when the netlink socket is
> closed on the user level side? (when using multicast communication)
>
> Or is there any other design I could use, since I think I'm not alone in
> using internal module data which needs to be removed once the application dies.

2008-12-06 14:58:23

by Patrick McHardy

[permalink] [raw]
Subject: Re: netlink - notify when the socket gets closed

Andrew Morton wrote:
> (cc [email protected])
>
> On Fri, 5 Dec 2008 09:17:51 +0100 "m m" <[email protected]> wrote:
>> I'm using netlink in my module. Based on the communication,
>> this module dynamically creates some internal structure, which
>> needs to be destroyed when the user level socket is closed (or
>> the application dies).
>>
>> I found I could use the netlink_register_notifier function to register
>> NETLINK_URELEASE callback during the netlink_release function.
>> But since my module uses the multicast netlink socket communication,
>> it wont be called:
>>
>> [...]
>> Whats the reason this callback is not called for multicast sockets?
>>
>> To workaround it I created simple misc device which the user application
>> opens before creating the netlink socket. This way I get some callbacks
>> inside the module when the application dies, at least.. pretty ugly :)
>>
>> Is there a netlink mechanism to be notified when the netlink socket is
>> closed on the user level side? (when using multicast communication)
>>
>> Or is there any other design I could use, since I think I'm not alone in
>> using internal module data which needs to be removed once the application dies.


That doesn't make sense. When you use multicast, multiple applications
can be listening to the messages. If you really need state for a single
listener and exchange messages in both directions, it sounds like you
should use unicast.

2008-12-06 18:11:19

by Jiri Olsa

[permalink] [raw]
Subject: Re: netlink - notify when the socket gets closed

On Sat, Dec 6, 2008 at 3:58 PM, Patrick McHardy <[email protected]> wrote:
> Andrew Morton wrote:
>>
>> (cc [email protected])
>>
>> On Fri, 5 Dec 2008 09:17:51 +0100 "m m" <[email protected]> wrote:
>>>
>>> I'm using netlink in my module. Based on the communication,
>>> this module dynamically creates some internal structure, which
>>> needs to be destroyed when the user level socket is closed (or
>>> the application dies).
>>>
>>> I found I could use the netlink_register_notifier function to register
>>> NETLINK_URELEASE callback during the netlink_release function.
>>> But since my module uses the multicast netlink socket communication,
>>> it wont be called:
>>>
>>> [...]
>>> Whats the reason this callback is not called for multicast sockets?
>>>
>>> To workaround it I created simple misc device which the user application
>>> opens before creating the netlink socket. This way I get some callbacks
>>> inside the module when the application dies, at least.. pretty ugly :)
>>>
>>> Is there a netlink mechanism to be notified when the netlink socket is
>>> closed on the user level side? (when using multicast communication)
>>>
>>> Or is there any other design I could use, since I think I'm not alone in
>>> using internal module data which needs to be removed once the application
>>> dies.
>
>
> That doesn't make sense. When you use multicast, multiple applications
> can be listening to the messages. If you really need state for a single
> listener and exchange messages in both directions, it sounds like you
> should use unicast.
>
>

My user app register in kernel for a data. When there is a first
application request,
kernel module creates an internal record and ask DATA subsystem for a data,
which are then passed to the user application.

The data are then delivered to the application as they are comming from
the DATA subsystem.

When there's another application requesting the same data type, kernel module
just increase reference count in the internal record, and multicast delivers
data to the other app.

Now when one of those applications end, kernel module just decrease
the reference count in the internal record.

When the other application ends, kernel module needs to release the internal
record and ask the DATA subsystem to stop delivering data.

Hope this make sense... I could do some ascii art next time if needed :)

thanks,
jirka

2008-12-07 16:04:50

by Patrick McHardy

[permalink] [raw]
Subject: Re: netlink - notify when the socket gets closed

m m wrote:
> On Sat, Dec 6, 2008 at 3:58 PM, Patrick McHardy <[email protected]> wrote:
>> That doesn't make sense. When you use multicast, multiple applications
>> can be listening to the messages. If you really need state for a single
>> listener and exchange messages in both directions, it sounds like you
>> should use unicast.
>>
>>
> My user app register in kernel for a data. When there is a first
> application request,
> kernel module creates an internal record and ask DATA subsystem for a data,
> which are then passed to the user application.
>
> The data are then delivered to the application as they are comming from
> the DATA subsystem.
>
> When there's another application requesting the same data type, kernel module
> just increase reference count in the internal record, and multicast delivers
> data to the other app.
>
> Now when one of those applications end, kernel module just decrease
> the reference count in the internal record.
>
> When the other application ends, kernel module needs to release the internal
> record and ask the DATA subsystem to stop delivering data.
>
> Hope this make sense... I could do some ascii art next time if needed :)

Thats an unusual way to use netlink, but it sounds reasonable.

Adding a notifier for multicast unsubscriptions should be easy.
Nothing in the kernel needs it though, so you need to carry the
patch to do this yourself until you submit your module.