Skip to content

Commit

Permalink
stream-unix: Give accepted sockets distinct names for log messages.
Browse files Browse the repository at this point in the history
At least on Linux, when process A connects to process B over a Unix
domain socket, unless process A bound its socket to a name before
it made the connection, process B gets an empty peer name.  Until
now, OVS has just reported the name of the connection as "unix".
This is not meaningful, of course.  I do not know of a good general
solution to this problem, but this commit attempts a step in the
right direction by at least giving each connection of this kind a
number: "unix#1", "unix#2", and so on.  That way, in log messages
one can at least see which messages are related to a particular
connection.

Signed-off-by: Ben Pfaff <blp@ovn.org>
Acked-by: Justin Pettit <jpettit@ovn.org>
  • Loading branch information
blp committed Dec 8, 2017
1 parent 1cae21e commit 14347f3
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
15 changes: 12 additions & 3 deletions lib/stream-unix.c
Expand Up @@ -26,6 +26,7 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "ovs-atomic.h"
#include "packets.h"
#include "openvswitch/poll-loop.h"
#include "socket-util.h"
Expand Down Expand Up @@ -112,9 +113,17 @@ punix_accept(int fd, const struct sockaddr_storage *ss, size_t ss_len,
{
const struct sockaddr_un *sun = (const struct sockaddr_un *) ss;
int name_len = get_unix_name_len(sun, ss_len);
char *bound_name = (name_len > 0
? xasprintf("unix:%.*s", name_len, sun->sun_path)
: xstrdup("unix"));
char *bound_name;

if (name_len > 0) {
bound_name = xasprintf("unix:%.*s", name_len, sun->sun_path);
} else {
/* When a Unix socket connects to us without first binding a name, we
* don't get any name for it. It's useful nevertheless to be able to
* distinguish separate sockets in log messages, so use a counter. */
static atomic_count next_idx = ATOMIC_COUNT_INIT(0);
bound_name = xasprintf("unix#%u", atomic_count_inc(&next_idx));
}
return new_fd_stream(bound_name, fd, 0, AF_UNIX, streamp);
}

Expand Down
1 change: 1 addition & 0 deletions tests/ofproto-macros.at
Expand Up @@ -32,6 +32,7 @@ prt==1 { sub(/[ \t]*$/, ""); print $0 }
vconn_sub() {
sed '
s/tcp:127.0.0.1:[0-9][0-9]*:/unix:/
s/unix#[0-9]*:/unix:/
'
}
]
Expand Down
2 changes: 1 addition & 1 deletion tests/ofproto.at
Expand Up @@ -5219,7 +5219,7 @@ vconn|DBG|unix: sent (Success): NXST_FLOW reply:
in_port=2,dl_src=00:66:77:88:99:aa actions=drop
])

AT_CHECK([grep " flow_mods in the last " ovs-vswitchd.log | sed -e 's/^.*connmgr|INFO|//'], [0], [dnl
AT_CHECK([grep " flow_mods in the last " ovs-vswitchd.log | sed -e 's/^.*connmgr|INFO|//' | vconn_sub], [0], [dnl
br0<->unix: 1 flow_mods in the last 0 s (1 deletes)
br0<->unix: 9 flow_mods in the last 0 s (7 adds, 2 deletes)
br0<->unix: 2 flow_mods in the last 0 s (2 modifications)
Expand Down

0 comments on commit 14347f3

Please sign in to comment.