Skip to contentSkip to conversation

Teach trinity to not use some sockets family

PATCH In review Cyrill Gorcunov 2 participants 9 messages Oct 24, 2013 – Oct 31, 2013

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

cover Teach trinity to not use some sockets family

When I've been running trinity on testing machine admins were not that happy about traffic generated by trinity (especially ones created with packet socket). So here is an attempt to teach trinity to use say only some set of sockets if needed with help of --fds option.

Please take a look, thanks.

#1 lore raw

PATCH 1/4 make: Add cscope target

Just another source indexing system which might
be preferred over tags.

---
 Makefile | 3 +++
 1 file changed, 3 insertions(+)
Makefile +3 -0
index b0bdb36..76f078b 100644
@@ -125,3 +125,6 @@ coverity:
 	cov-build --dir cov-int make
 	tar czvf trinity-coverity.tgz cov-int
 
+cscope:
+	@find . -name '*.[hcS]' ! -path './.*' -print > cscope.files
+	@cscope -bkqu
Signature · 1 line
1.8.3.1
#2 lore raw

PATCH 2/4 log: Add BUG_ON helper

In reply to Cyrill Gorcunov

We will use it in sockets creating routine.

---
 include/log.h | 2 ++
 1 file changed, 2 insertions(+)
include/log.h +2 -0
index 500353d..22ad80a 100644
@@ -59,4 +59,6 @@ void debugf(const char *fmt, ...);
 	}\
 }
 
+#define BUG_ON(condition)	do { if ((condition)) BUG(BUGTXT); } while (0)
+
 #endif	/* _LOG_H */
Signature · 1 line
1.8.3.1
#3 lore raw

PATCH 3/4 params: Add parsing of -f|--fds option

In reply to Cyrill Gorcunov

Here we introduce a trivial tokenizer to control socket domains assignment. It might be needed to disabled some sockets (or even all of them, if you're not interested in testing sockets). For this sake -f|--fds option introduced.

The option takes argument in form of

	--fds sockets:[(all,)|(none,)|(PF_X,^PF_X)]

where PF_X is appropriate domain like PF_INET and such. The prefix ^ before name means to exclude some particular family from being tested.

Also note that all|none do overwrite previous option, this is done in a sake of code simplicity. IOW, if

--fds sockets:^PF_INET,all

is passed, in result all socket families are allowed.

---
 include/net.h   |  6 +++-
 net/protocols.c | 11 +++++++
 params.c        | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 107 insertions(+), 2 deletions(-)
include/net.h +5 -1
index 4e43546..c5baaf4 100644
@@ -1,9 +1,12 @@
 #ifndef _NET_H
 #define _NET_H 1
 
+#include <sys/types.h>
+#include <sys/socket.h>
 #include <netinet/in.h>
 
 extern unsigned int nr_sockets;
+extern char disabled_protos[PF_MAX];
 void open_sockets(void);
 void generate_sockaddr(unsigned long *addr, unsigned long *addrlen, int pf);
 
@@ -11,7 +14,8 @@ void generate_sockaddr(unsigned long *addr, unsigned long *addrlen, int pf);
 extern unsigned int specific_proto;
 const char * get_proto_name(unsigned int proto);
 void find_specific_proto(const char *protoarg);
-
+unsigned int find_proto_by_name(const char *name);
+int disable_socket(const char *domain);
 
 /* glibc headers might be older than the kernel, so chances are we know
  * about more protocols than glibc does. So we define our own PF_MAX */
net/protocols.c +11 -0
index 891acfe..6aedd29 100644
@@ -58,6 +58,17 @@ static const struct protocol protocols[] = {
 	{ "PF_VSOCK",        40 },
 };
 
+unsigned int find_proto_by_name(const char *name)
+{
+	unsigned int i;
+
+	for (i = 0; i < ARRAY_SIZE(protocols); i++) {
+		if (!strcmp(protocols[i].name, name))
+			return protocols[i].proto;
+	}
+	return -1u;
+}
+
 const char * get_proto_name(unsigned int proto)
 {
 	unsigned int i;
params.c +91 -1

138 lines not on this page · raw message

Signature · 1 line
1.8.3.1
#4 lore raw

PATCH 4/4 sockets: Take into account socket families disabled when generating sockets

In reply to Cyrill Gorcunov

When we generate sockets cache file we should take into account the option passed with --fds making sure the socket we're about to create was not disabled.

---
 sockets.c         | 17 ++++++++++++++---
 syscalls/socket.c | 20 +++++++++++++++++++-
 2 files changed, 33 insertions(+), 4 deletions(-)
sockets.c +14 -3
index 590c814..cb9a9a2 100644
@@ -132,6 +132,13 @@ static void generate_sockets(void)
 
 	lock_cachefile(cachefile, F_WRLCK);
 
+	/*
+	 * If we have all sockets disabled, don't create any.
+	 */
+	if (index((const char *)disabled_protos, 'y') &&
+	    !index((const char *)disabled_protos, 'n'))
+		nr_to_create = 0;
+
 	while (nr_to_create > 0) {
 
 		struct socket_triplet st;
@@ -141,6 +148,8 @@ static void generate_sockets(void)
 			return;
 		}
 
+		BUG_ON(ARRAY_SIZE(disabled_protos) >= TRINITY_PF_MAX);
+
 		for (st.family = 0; st.family < TRINITY_PF_MAX; st.family++) {
 
 			if (do_specific_proto == TRUE)
@@ -149,6 +158,9 @@ static void generate_sockets(void)
 			if (get_proto_name(st.family) == NULL)
 				goto skip;
 
+			if (disabled_protos[st.family] == 'y')
+				goto skip;
+
 			if (sanitise_socket_triplet(&st) == -1)
 				rand_proto_type(&st);
 
@@ -236,8 +248,8 @@ void open_sockets(void)
 		type = buffer[1];
 		protocol = buffer[2];
 
-		if (do_specific_proto == TRUE) {
-			if (domain != specific_proto) {
+		if ((do_specific_proto == TRUE && domain != specific_proto) ||
+		    (domain < ARRAY_SIZE(disabled_protos) && disabled_protos[domain] == 'y')) {
 				output(1, "ignoring socket cachefile due to specific protocol request, and stale data in cachefile.\n");
 regenerate:
 				unlock_cachefile(cachefile);	/* drop the reader lock. */
@@ -245,7 +257,6 @@ regenerate:
 				unlink(cachefilename);
 				generate_sockets();
 				return;
-			}
 		}
 
 		fd = open_socket(domain, type, protocol);
syscalls/socket.c +19 -1
index 21dabb1..8d5100c 100644
@@ -44,17 +44,35 @@ static const struct socket_ptr socketptrs[] = {
 
 void rand_proto_type(struct socket_triplet *st)
 {
+	int n = 6;
 	st->protocol = rand() % PROTO_MAX;
 
-	switch (rand() % 6) {
+again:
+	switch (rand() % n) {
 	case 0:	st->type = SOCK_DGRAM;	break;
 	case 1:	st->type = SOCK_STREAM;	break;
 	case 2:	st->type = SOCK_SEQPACKET;	break;
 	case 3:	st->type = SOCK_RAW;	break;
 	case 4:	st->type = SOCK_RDM;	break;
+
+	/*
+	 * It must be last.
+	 */
 	case 5:	st->type = SOCK_PACKET;	break;
 	default: break;
 	}
+
+	/*
+	 * One special moment on packet sockets. They
+	 * can be created with SOCK_PACKET, so if
+	 * PF_PACKET is disabled, choose some other type.
+	 */
+	if (st->type == SOCK_PACKET	&&
+	    st->family == PF_INET	&&
+	    disabled_protos[PF_PACKET] == 'y') {
+		n = 5;
+		goto again;
+	}
 }
 
 /* note: also called from generate_sockets() */
Signature · 1 line
1.8.3.1
#5 lore raw
In reply to Cyrill Gorcunov
Quoted 7 lines · Cyrill Gorcunov

When I've been running trinity on testing machine admins were not that happy about traffic generated by trinity (especially ones created with packet socket). So here is an attempt to teach trinity to use say only some set of sockets if needed with help of --fds option.

Please take a look, thanks.

Drop the serice please, it'll be reworked.

#6 lore raw
Quoted 10 lines · Cyrill Gorcunov
Quoted 7 lines · Cyrill Gorcunov

When I've been running trinity on testing machine admins were not that happy about traffic generated by trinity (especially ones created with packet socket). So here is an attempt to teach trinity to use say only some set of sockets if needed with help of --fds option.

Please take a look, thanks.

Drop the serice please, it'll be reworked.

I'm about to apply your later series, but I noticed that that one instead of the --fds patch, has the -E change.

Do you still plan on submitting the --fds change later?
The reason I ask is that it if we're doing --fds, then it might at some point mean we can deprecate (and then remove) use of -P in favor of it, so adding an inverse (-E) seems odd.

Dave

#7 lore raw
Quoted 12 lines · Dave Jones
Quoted 10 lines · Cyrill Gorcunov
Quoted 7 lines · Cyrill Gorcunov

When I've been running trinity on testing machine admins were not that happy about traffic generated by trinity (especially ones created with packet socket). So here is an attempt to teach trinity to use say only some set of sockets if needed with help of --fds option.

Please take a look, thanks.

Drop the serice please, it'll be reworked.

Quoted 7 lines

I'm about to apply your later series, but I noticed that that one instead of the --fds patch, has the -E change.

Do you still plan on submitting the --fds change later?
The reason I ask is that it if we're doing --fds, then it might at some point mean we can deprecate (and then remove) use of -P in favor of it, so adding an inverse (-E) seems odd.

Hi Dave! To be fair, not sure at moment. Initially I wanted to implement a general --fds option which might take more complex command line like sockets:PF_X,^PF_X and such, then extend it to file:^pipe,epoll. But this end up in being somehow more complex rework. So I decided to stick with simplier approach first -- -E option which would exclude some socket protocols from being generated.

But sure, once time permit I can try to implement --fds option as well. Lets fisrt summarize what kind of syntax it will carry.

--fds [sockets:PF_X,^PF_X,N,^N,all,none],[files:pipe,^epoll,all,none]

Sounds good?

#8 lore raw
Quoted 14 lines · Cyrill Gorcunov
Quoted 7 lines

I'm about to apply your later series, but I noticed that that one instead of the --fds patch, has the -E change.

Do you still plan on submitting the --fds change later?
The reason I ask is that it if we're doing --fds, then it might at some point mean we can deprecate (and then remove) use of -P in favor of it, so adding an inverse (-E) seems odd.

Hi Dave! To be fair, not sure at moment. Initially I wanted to implement a general --fds option which might take more complex command line like sockets:PF_X,^PF_X and such, then extend it to file:^pipe,epoll. But this end up in being somehow more complex rework. So I decided to stick with simplier approach first -- -E option which would exclude some socket protocols from being generated.

Quoted 6 lines

But sure, once time permit I can try to implement --fds option as well. Lets fisrt summarize what kind of syntax it will carry.

--fds [sockets:PF_X,^PF_X,N,^N,all,none],[files:pipe,^epoll,all,none]

Sounds good?

Sure. I just pushed out your 2nd patch set.

thanks,

Dave