ss: Allow excluding a socket table from being queried
Subsystems (2)
v2 cover ss: Allow excluding a socket table from being queried
The first patch in this series adds the new functionality, the remaining two refactor the code a bit.
Note that Patch 1 creates warnings with checkpatch due to overlong lines, but patch 2 removes them again so for the sake of readability I left this as is.
Changes since v1:
- Fixed checkpatch errors in patch 2/3.
Phil Sutter (3):
ss: Allow excluding a socket table from being queried
ss: Put filter DB parsing into a separate function
ss: Drop filter_default_dbs()
man/man8/ss.8 | 8 +++- misc/ss.c | 147 +++++++++++++++++++++++++++------------------------------- 2 files changed, 76 insertions(+), 79 deletions(-)
Signature · 1 line
2.16.1
PATCH v2 1/3 ss: Allow excluding a socket table from being queried
The original problem was that a simple call to 'ss' leads to loading of sctp_diag kernel module which might not be desired. While searching for a workaround, it became clear how inconvenient it is to exclude a single socket table from being queried.
This patch allows to prefix an item passed to '-A' parameter with an exclamation mark to inverse its meaning.
--- man/man8/ss.8 | 8 ++++- misc/ss.c | 108 ++++++++++++++++++++++++++++++++-------------------------- 2 files changed, 66 insertions(+), 50 deletions(-)
man/man8/ss.8 +7 -1
@@ -317,7 +317,10 @@ Currently the following families are supported: unix, inet, inet6, link, netlink List of socket tables to dump, separated by commas. The following identifiers are understood: all, inet, tcp, udp, raw, unix, packet, netlink, unix_dgram, unix_stream, unix_seqpacket, packet_raw, packet_dgram, dccp, sctp, -vsock_stream, vsock_dgram. +vsock_stream, vsock_dgram. Any item in the list may optionally be prefixed by +an exclamation mark +.RB ( ! ) +to exclude that socket table from being dumped. .TP .B \-D FILE, \-\-diag=FILE Do not display anything, just dump raw information about TCP sockets to FILE after applying filters. If FILE is - stdout is used.
@@ -380,6 +383,9 @@ Find all local processes connected to X server. .TP .B ss -o state fin-wait-1 '( sport = :http or sport = :https )' dst 193.233.7/24 List all the tcp sockets in state FIN-WAIT-1 for our apache to network 193.233.7/24 and look at their timers. +.TP +.B ss -a -A 'all,!tcp' +List sockets in all states from all socket tables but TCP. .SH SEE ALSO .BR ip (8), .br
misc/ss.c +59 -49
175 lines not on this page · raw message
Signature · 1 line
2.16.1
PATCH v2 2/3 ss: Put filter DB parsing into a separate function
In reply to Phil SutterUse a table for database name parsing. The tricky bit is to allow for association of a (nearly) arbitrary number of DBs with each name. Luckily the number is not fully arbitrary as there is an upper bound of MAX_DB items. Since it is not possible to have a variable length array inside a variable length array, use this knowledge to make the inner array of fixed length. But since DB values start from zero, an explicit end entry needs to be present as well, so the inner array has to be MAX_DB + 1 in size.
---
Changes since v1:
- Fix checkpatch errors.
misc/ss.c | 114 ++++++++++++++++++++++++++++++-------------------------------- 1 file changed, 56 insertions(+), 58 deletions(-)
misc/ss.c +56 -58
@@ -340,6 +340,61 @@ static void filter_db_set(struct filter *f, int db, bool enable) do_default =0 ; } +static int filter_db_parse (struct filter *f,const char *s) +{ +const struct { +const char *name; +int dbs[MAX_DB +1 ]; + } db_name_tbl[] = { +#define ENTRY(name, ...) { #name, { __VA_ARGS__, MAX_DB } } +ENTRY (all,UDP_DB ,DCCP_DB ,TCP_DB ,RAW_DB , +UNIX_ST_DB ,UNIX_DG_DB ,UNIX_SQ_DB , +PACKET_R_DB ,PACKET_DG_DB ,NETLINK_DB , +SCTP_DB ,VSOCK_ST_DB ,VSOCK_DG_DB ), +ENTRY (inet,UDP_DB ,DCCP_DB ,TCP_DB ,SCTP_DB ,RAW_DB ), +ENTRY (udp,UDP_DB ), +ENTRY (dccp,DCCP_DB ), +ENTRY (tcp,TCP_DB ), +ENTRY (sctp,SCTP_DB ), +ENTRY (raw,RAW_DB ), +ENTRY (unix,UNIX_ST_DB ,UNIX_DG_DB ,UNIX_SQ_DB ), +ENTRY (unix_stream,UNIX_ST_DB ), +ENTRY (u_str,UNIX_ST_DB ),/* alias for unix_stream */ +ENTRY (unix_dgram,UNIX_DG_DB ), +ENTRY (u_dgr,UNIX_DG_DB ),/* alias for unix_dgram */ +ENTRY (unix_seqpacket,UNIX_SQ_DB ), +ENTRY (u_seq,UNIX_SQ_DB ),/* alias for unix_seqpacket */ +ENTRY (packet,PACKET_R_DB ,PACKET_DG_DB ), +ENTRY (packet_raw,PACKET_R_DB ), +ENTRY (p_raw,PACKET_R_DB ),/* alias for packet_raw */ +ENTRY (packet_dgram,PACKET_DG_DB ), +ENTRY (p_dgr,PACKET_DG_DB ),/* alias for packet_dgram */ +ENTRY (netlink,NETLINK_DB ), +ENTRY (vsock,VSOCK_ST_DB ,VSOCK_DG_DB ), +ENTRY (vsock_stream,VSOCK_ST_DB ), +ENTRY (v_str,VSOCK_ST_DB ),/* alias for vsock_stream */ +ENTRY (vsock_dgram,VSOCK_DG_DB ), +ENTRY (v_dgr,VSOCK_DG_DB ),/* alias for vsock_dgram */ +#undef ENTRY + }; +bool enable = true; +unsigned int i; +const int *dbp; + +if (s[0 ] =='!' ) { + enable = false; + s++; + } +for (i =0 ; i <ARRAY_SIZE (db_name_tbl); i++) { +if (strcmp (s, db_name_tbl[i].name)) +continue ; +for (dbp = db_name_tbl[i].dbs; *dbp !=MAX_DB ; dbp++) +filter_db_set (f, *dbp, enable); +return 0 ; + } +return -1 ; +} + static void filter_af_set(struct filter *f, int af) { f->states |= default_afs[af].states;
@@ -4785,66 +4840,9 @@ int main(int argc, char *argv[]) } p = p1 = optarg; do { - bool enable = true; - if ((p1 = strchr(p, ',')) != NULL) *p1 = 0; - if (p[0] == '!') { - enable = false; - p++; - } - if (strcmp(p, "all") == 0) { - filter_default_dbs(¤t_filter, enable); - } else if (strcmp(p, "inet") == 0) { - filter_db_set(¤t_filter, UDP_DB, enable); - filter_db_set(¤t_filter, DCCP_DB, enable); - filter_db_set(¤t_filter, TCP_DB, enable); - filter_db_set(¤t_filter, SCTP_DB, enable); - filter_db_set(¤t_filter, RAW_DB, enable); - } else if (strcmp(p, "udp") == 0) { - filter_db_set(¤t_filter, UDP_DB, enable); - } else if (strcmp(p, "dccp") == 0) { - filter_db_set(¤t_filter, DCCP_DB, enable); - } else if (strcmp(p, "tcp") == 0) { - filter_db_set(¤t_filter, TCP_DB, enable); - } else if (strcmp(p, "sctp") == 0) { - filter_db_set(¤t_filter, SCTP_DB, enable); - } else if (strcmp(p, "raw") == 0) { - filter_db_set(¤t_filter, RAW_DB, enable); - } else if (strcmp(p, "unix") == 0) { - filter_db_set(¤t_filter, UNIX_ST_DB, enable); - filter_db_set(¤t_filter, UNIX_DG_DB, enable); - filter_db_set(¤t_filter, UNIX_SQ_DB, enable); - } else if (strcasecmp(p, "unix_stream") == 0 || - strcmp(p, "u_str") == 0) { - filter_db_set(¤t_filter, UNIX_ST_DB, enable); - } else if (strcasecmp(p, "unix_dgram") == 0 || - strcmp(p, "u_dgr") == 0) { - filter_db_set(¤t_filter, UNIX_DG_DB, enable); - } else if (strcasecmp(p, "unix_seqpacket") == 0 || - strcmp(p, "u_seq") == 0) { - filter_db_set(¤t_filter, UNIX_SQ_DB, enable); - } else if (strcmp(p, "packet") == 0) { - filter_db_set(¤t_filter, PACKET_R_DB, enable); - filter_db_set(¤t_filter, PACKET_DG_DB, enable); - } else if (strcmp(p, "packet_raw") == 0 || - strcmp(p, "p_raw") == 0) { - filter_db_set(¤t_filter, PACKET_R_DB, enable); - } else if (strcmp(p, "packet_dgram") == 0 || - strcmp(p, "p_dgr") == 0) { - filter_db_set(¤t_filter, PACKET_DG_DB, enable); - } else if (strcmp(p, "netlink") == 0) { - filter_db_set(¤t_filter, NETLINK_DB, enable); - } else if (strcmp(p, "vsock") == 0) { - filter_db_set(¤t_filter, VSOCK_ST_DB, enable); - filter_db_set(¤t_filter, VSOCK_DG_DB, enable); - } else if (strcmp(p, "vsock_stream") == 0 || - strcmp(p, "v_str") == 0) { - filter_db_set(¤t_filter, VSOCK_ST_DB, enable); - } else if (strcmp(p, "vsock_dgram") == 0 || - strcmp(p, "v_dgr") == 0) { - filter_db_set(¤t_filter, VSOCK_DG_DB, enable); - } else { + if (filter_db_parse(¤t_filter, p)) { fprintf(stderr, "ss: \"%s\" is illegal socket table id\n", p); usage(); }
Signature · 1 line
2.16.1
PATCH v2 3/3 ss: Drop filter_default_dbs()
In reply to Phil SutterInstead call filter_db_parse(..., "all"). This eliminates the duplicate default DB definition.
--- misc/ss.c | 19 +------------------ 1 file changed, 1 insertion(+), 18 deletions(-)
misc/ss.c +1 -18
@@ -408,23 +408,6 @@ static int filter_af_get(struct filter *f, int af)return !!(f->families &FAMILY_MASK (af)); } -static void filter_default_dbs (struct filter *f,bool enable) -{ -filter_db_set (f,UDP_DB , enable); -filter_db_set (f,DCCP_DB , enable); -filter_db_set (f,TCP_DB , enable); -filter_db_set (f,RAW_DB , enable); -filter_db_set (f,UNIX_ST_DB , enable); -filter_db_set (f,UNIX_DG_DB , enable); -filter_db_set (f,UNIX_SQ_DB , enable); -filter_db_set (f,PACKET_R_DB , enable); -filter_db_set (f,PACKET_DG_DB , enable); -filter_db_set (f,NETLINK_DB , enable); -filter_db_set (f,SCTP_DB , enable); -filter_db_set (f,VSOCK_ST_DB , enable); -filter_db_set (f,VSOCK_DG_DB , enable); -} -static void filter_states_set (struct filter *f,int states) {if (states)
@@ -4934,7 +4917,7 @@ int main(int argc, char *argv[])if (do_default) { state_filter = state_filter ? state_filter :SS_CONN ; -filter_default_dbs (¤t_filter, true); +filter_db_parse (¤t_filter,"all" ); }filter_states_set (¤t_filter, state_filter);
Signature · 1 line
2.16.1