Skip to contentSkip to conversation

Multicast socket option

PATCH Applied Nivedita Singhvi 5 participants 7 messages May 28, 2009 – Jun 2, 2009

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Multicast socket option

After some discussion offline with Christoph Lameter and David Stevens regarding multicast behaviour in Linux, I'm submitting a slightly modified patch from the one Christoph submitted earlier.

This patch provides a new socket option IP_MULTICAST_ALL.

In this case, default behaviour is _unchanged_ from the current
Linux standard. The socket option is set by default to provide original behaviour. Sockets wishing to receive data only from multicast groups they join explicitly will need to clear this socket option.

include/linux/in.h +0 -0
diff -urN linux-2.6.29.2/include/linux/in.h linux-2.6.29.2.new/include/linux/in.h
@@ -107,6 +107,7 @@

#define MCAST_JOIN_SOURCE_GROUP 46
#define MCAST_LEAVE_SOURCE_GROUP 47
#define MCAST_MSFILTER 48
+#define IP_MULTICAST_ALL 49

#define MCAST_EXCLUDE 0
#define MCAST_INCLUDE 1

include/net/inet_sock.h +0 -0
diff -urN linux-2.6.29.2/include/net/inet_sock.h linux-2.6.29.2.new/include/net/inet_sock.h
@@ -130,7 +130,8 @@
				freebind:1,
				hdrincl:1,
				mc_loop:1,
-				transparent:1;
+				transparent:1,
+				mc_all:1;
	int			mc_index;
	__be32			mc_addr;
	struct ip_mc_socklist	*mc_list;
net/ipv4/af_inet.c +0 -0
diff -urN linux-2.6.29.2/net/ipv4/af_inet.c linux-2.6.29.2.new/net/ipv4/af_inet.c
@@ -376,6 +376,7 @@
	inet->uc_ttl	= -1;
	inet->mc_loop	= 1;
	inet->mc_ttl	= 1;
+	inet->mc_all	= 1;
	inet->mc_index	= 0;
	inet->mc_list	= NULL;
net/ipv4/igmp.c +0 -0
diff -urN linux-2.6.29.2/net/ipv4/igmp.c linux-2.6.29.2.new/net/ipv4/igmp.c
@@ -2196,7 +2196,7 @@
			break;
	}
	if (!pmc)
-		return 1;
+		return inet->mc_all;
	psl = pmc->sflist;
	if (!psl)
		return pmc->sfmode == MCAST_EXCLUDE;
net/ipv4/ip_sockglue.c +0 -0
diff -urN linux-2.6.29.2/net/ipv4/ip_sockglue.c linux-2.6.29.2.new/net/ipv4/ip_sockglue.c
@@ -449,6 +449,7 @@
			     (1<<IP_ROUTER_ALERT) | (1<<IP_FREEBIND) |
			     (1<<IP_PASSSEC) | (1<<IP_TRANSPARENT))) ||
	    optname == IP_MULTICAST_TTL ||
+	    optname == IP_MULTICAST_ALL ||
	    optname == IP_MULTICAST_LOOP ||
	    optname == IP_RECVORIGDSTADDR) {
		if (optlen >= sizeof(int)) {
@@ -895,6 +896,13 @@
		kfree(gsf);
		break;
	}

+ case IP_MULTICAST_ALL:
+ if (optlen<1)
+ goto e_inval;
+ if (val != 0 && val != 1)
+ goto e_inval;
+ inet->mc_all = val;

+		break;
	case IP_ROUTER_ALERT:
		err = ip_ra_control(sk, val ? 1 : 0, NULL);
		break;
@@ -1147,6 +1155,9 @@
		release_sock(sk);
		return err;
	}
+	case IP_MULTICAST_ALL:
+		val = inet->mc_all;
+		break;
	case IP_PKTOPTIONS:
	{
		struct msghdr msg;
#1 lore raw
Quoted 11 lines · On Thursday 28 May 2009 20:00:46 ext Nivedita Singhvi wrote:

After some discussion offline with Christoph Lameter and David Stevens regarding multicast behaviour in Linux, I'm submitting a slightly modified patch from the one Christoph submitted earlier.

This patch provides a new socket option IP_MULTICAST_ALL.

In this case, default behaviour is _unchanged_ from the current Linux standard. The socket option is set by default to provide original behaviour. Sockets wishing to receive data only from multicast groups they join explicitly will need to clear this socket option.

You can already achieve this by checking the destination address in the SOL_PKTINFO ancilliary data. Sure, it will cause extra context switches to process unwanted packets but it will work with any kernel version.

Signature · 2 lines
Rémi Denis-Courmont
Nokia Devices R&D, Maemo Software, Helsinki
#3 lore raw
In reply to Nivedita Singhvi
Quoted 56 lines · Nivedita Singhvi

After some discussion offline with Christoph Lameter and David Stevens regarding multicast behaviour in Linux, I'm submitting a slightly modified patch from the one Christoph submitted earlier.

This patch provides a new socket option IP_MULTICAST_ALL.

In this case, default behaviour is _unchanged_ from the current Linux standard. The socket option is set by default to provide original behaviour. Sockets wishing to receive data only from multicast groups they join explicitly will need to clear this socket option.

diff -urN linux-2.6.29.2/include/linux/in.h linux-2.6.29.2.new/include/linux/in.h

include/linux/in.h +1 -0
@@ -107,6 +107,7 @@
 #define MCAST_JOIN_SOURCE_GROUP		46
 #define MCAST_LEAVE_SOURCE_GROUP	47
 #define MCAST_MSFILTER			48
+#define IP_MULTICAST_ALL		49
 
 #define MCAST_EXCLUDE	0
 #define MCAST_INCLUDE	1

diff -urN linux-2.6.29.2/include/net/inet_sock.h linux-2.6.29.2.new/include/net/inet_sock.h

include/net/inet_sock.h +2 -1
@@ -130,7 +130,8 @@
 				freebind:1,
 				hdrincl:1,
 				mc_loop:1,
-				transparent:1;
+				transparent:1,
+				mc_all:1;
 	int			mc_index;
 	__be32			mc_addr;
 	struct ip_mc_socklist	*mc_list;

diff -urN linux-2.6.29.2/net/ipv4/af_inet.c linux-2.6.29.2.new/net/ipv4/af_inet.c

net/ipv4/af_inet.c +1 -0
@@ -376,6 +376,7 @@
 	inet->uc_ttl	= -1;
 	inet->mc_loop	= 1;
 	inet->mc_ttl	= 1;
+	inet->mc_all	= 1;
 	inet->mc_index	= 0;
 	inet->mc_list	= NULL;
 

diff -urN linux-2.6.29.2/net/ipv4/igmp.c linux-2.6.29.2.new/net/ipv4/igmp.c

net/ipv4/igmp.c line 2199

Quoted 5 lines
net/ipv4/igmp.c +1 -1
@@ -2196,7 +2196,7 @@
 			break;
 	}
 	if (!pmc)
-		return 1;
+		return inet->mc_all;

This change also filters out broadcasts sent to sockets listening on a given port. Is that what you want?

Neil

Quoted 1 line
#4 lore raw
Quoted 4 lines · Neil Horman
Quoted 4 lines

diff -urN linux-2.6.29.2/net/ipv4/igmp.c linux-2.6.29.2.new/net/ipv4/igmp.c

net/ipv4/igmp.c +0 -0
@@ -2196,7 +2196,7 @@
Quoted 8 lines

net/ipv4/igmp.c line 2199

Quoted 5 lines
net/ipv4/igmp.c +1 -1
 			break;
 	}
 	if (!pmc)
-		return 1;
+		return inet->mc_all;

This change also filters out broadcasts sent to sockets listening on a given port. Is that what you want?

Look at the beginning of the function:

if (!ipv4_is_multicast(loc_addr))

                return 1;

will return 1 for broadcast.

#2 lore raw
In reply to Rémi Denis-Courmont
Quoted 8 lines · Rémi Denis-Courmont
Quoted 5 lines

In this case, default behaviour is _unchanged_ from the current Linux standard. The socket option is set by default to provide original behaviour. Sockets wishing to receive data only from multicast groups they join explicitly will need to clear this socket option.

You can already achieve this by checking the destination address in the SOL_PKTINFO ancilliary data. Sure, it will cause extra context switches to process unwanted packets but it will work with any kernel version.

True, and depending on your environment and workload, this is a non-event or a big deal. For low latency, real time and other performance-sensitive applications, this can be an issue. Think lots (hundreds of threads, perhaps) listening on different channels(groups). They will likely all get woken, scheduled, possibly preempt running lower-priority tasks to process this delivery that they don't need. Seen across the system, the delivery by the kernel of packets to user space processes which don't want them can be very significant overhead (context switches, scheduling out/in,...).

thanks,
Nivedita

#5 lore raw
In reply to Neil Horman
Quoted 6 lines · Neil Horman

Review

Quoted 3 lines
	if (!pmc)
(quoted diff) +1 -1
-		return 1;
+		return inet->mc_all;

This change also filters out broadcasts sent to sockets listening on a given port. Is that what you want?

Yes, as Christoph pointed out, this does not impact broadcast delivery, which will remain consistent with original behaviour.

thanks,
Nivedita

maintainer Applied
#6 lore raw
In reply to Nivedita Singhvi

From: Nivedita Singhvi <[email protected]>
Date: Thu, 28 May 2009 10:00:46 -0700

Quoted 13 lines

After some discussion offline with Christoph Lameter and David Stevens regarding multicast behaviour in Linux, I'm submitting a slightly modified patch from the one Christoph submitted earlier.

This patch provides a new socket option IP_MULTICAST_ALL.

In this case, default behaviour is _unchanged_ from the current Linux standard. The socket option is set by default to provide original behaviour. Sockets wishing to receive data only from multicast groups they join explicitly will need to clear this socket option.

Quoted 3 lines

Applied, thanks.