AF_UNIX multicast capability for 2.5.66
Subsystems (3)
For those who didn't read the first couple threads, this patch adds multicast functionality to unix sockets, in similar fashion (and using a similar API) as UDP multicast, but easier to use.
To use the functionality, in userspace you would allocate and bind a socket as normal in the AF_UNIX family, and then you would use setsockop() to associate yourself with one or more multicast addresses, also in the AF_UNIX family. Any message sent to a multicast address gets duplicated by the kernel and distributed to all processes associated with that address.
If an address already exists and is not multicast, you cannot associate yourself with it using setsockopt(), and if it exists and is multicast, you cannot bind() do it. All AF_UNIX addresses exist in the same namespace and must be either multicast or unicast.
It does not make sense to allow streaming to a multicast address, so I plan on disallowing this (and other similar things) in a future release of the patch. I'm not sure about allowing datagram sockets to connect() to a multicast address, I haven't looked at the code in depth.
At any rate, here are the results of some testing on the latest kernel comparing kernel multicast against a userspace solution. There is a sender program which sends messages of various sizes to various numbers of listeners. Each message has a timestamp embedded within it, and the listeners determine the latency between the sending and receiving of the message. In the userspace solution, the list of listeners is kept in a shared memory area, which is faster than using a distributor process.
In the interests of figuring out the best possible performance I've changed the testing methodology from the last bunch of tests, and the organization of the results is slightly different to make it easier to add new results. In this test sequence the sender and receiver processes have been run with nice -20 to minimize interference from other entities in the system. These are best-case results from a number of runs, but the numbers are fairly consistant across runs.
data size and kernel number of listeners
44 bytes 10 20 50 100 200
2.5.66 userspace 134,297 206,561 416,1401 761,2824 1500,5711 2.5.66 kernelspace 75,232 119,457 213,1142 356,2308 679,4710
236 bytes
2.5.66 userspace 143,306 218,584 447,1472 814,3013 1399,6007 2.5.66 kernelspace 80,244 115,469 216,1176 365,2371 682,4893
40036 bytes
2.5.66 userspace 478,3613 497,7405 496,18114 487,36759 518,74566 2.5.66 kernelspace 287,1672 327,3299 444,8129 663,16125 1000,31937
The numbers definately favour a kernel-space solution. That said, it would be possible to implement this using UDP messaging, but UDP latency is generally about 30 percent higher than AF_UNIX on my 1.8GHz P4, and it's more of a hassle to configure UDP multicast.
I would appreciate any comments on the patch, if you see any technical bugs or if you think there is a better way to do something please do let me know. I'm sure there's some fine point about locking that I missed, or something similar.
Thanks,
Chris
Signature · 4 lines
Chris Friesen | MailStop: 043/33/F10 Nortel Networks | work: (613) 765-0557 3500 Carling Avenue | fax: (613) 765-2986 Nepean, ON K2H 8E9 Canada | email: [email protected]
include/linux/un.h +8 -0
@@ -8,4 +8,12 @@ char sun_path[UNIX_PATH_MAX]; /* pathname */ }; +#ifdef CONFIG_UNIX_MULTICAST + +#define UNIX_ADD_MEMBERSHIP 35 +#define UNIX_DROP_MEMBERSHIP 36 + +#endif + +#endif /* _LINUX_UN_H */
include/net/af_unix.h +18 -0
@@ -61,6 +61,20 @@#define unix_state_wunlock (s) write_unlock(&unix_sk(s)->lock)#ifdef __KERNEL__ + +#ifdef CONFIG_UNIX_MULTICAST +struct unix_mcast +{ +unix_socket *listener; +unix_socket *addr; +struct unix_mcast *nextlistener; +struct unix_mcast *prevlistener; +struct unix_mcast *nextaddr; +struct unix_mcast *prevaddr; +}; + +#endif +/* The AF_UNIX socket */ struct unix_sock {/* WARNING: sk has to be the first member */
@@ -75,6 +89,10 @@atomic_t inflight;rwlock_t lock;wait_queue_head_t peer_wait; +#ifdef CONFIG_UNIX_MULTICAST +int is_mcast_addr; +struct unix_mcast *mcastnode; +#endif };#define unix_sk (__sk) ((struct unix_sock *)__sk)#endif
net/Kconfig +13 -0
@@ -157,6 +157,19 @@ Say Y unless you know what you are doing. +config UNIX_MULTICAST + bool "Unix domain multicasting (EXPERIMENTAL)" + depends on UNIX && EXPERIMENTAL + ---help--- + If you say Y here you will include support for multicast unix domain + sockets. Multiple sockets can add themselves to a multicast address + group, and any packet sent to the multicast address will be distributed + to all unix sockets that have associated themselves with the multicast + address. + + This code is experimental. Say N unless you want to try efficient + one-sender many-listeners messaging. + config NET_KEY tristate "PF_KEY sockets" ---help---
net/unix/af_unix.c +420 -9
528 lines not on this page · raw message