From: Phil Sutter <phil@nwl.cc>
To: Stephen Hemminger <stephen@networkplumber.org>
Cc: netdev@vger.kernel.org
Subject: [iproute PATCH v2 2/3] ss: Put filter DB parsing into a separate function
Date: Wed, 28 Mar 2018 01:51:55 +0200 [thread overview]
Message-ID: <20180327235156.16680-3-phil@nwl.cc> (raw)
In-Reply-To: <20180327235156.16680-1-phil@nwl.cc>
Use 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.
Signed-off-by: Phil Sutter <phil@nwl.cc>
---
Changes since v1:
- Fix checkpatch errors.
misc/ss.c | 114 ++++++++++++++++++++++++++++++--------------------------------
1 file changed, 56 insertions(+), 58 deletions(-)
diff --git a/misc/ss.c b/misc/ss.c
index 05522176f1e61..83e476a0407e5 100644
--- a/misc/ss.c
+++ b/misc/ss.c
@@ -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();
}
--
2.16.1
next prev parent reply other threads:[~2018-03-27 23:52 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-03-27 23:51 [iproute PATCH v2 0/3] ss: Allow excluding a socket table from being queried Phil Sutter
2018-03-27 23:51 ` [iproute PATCH v2 1/3] " Phil Sutter
2018-03-27 23:51 ` Phil Sutter [this message]
2018-03-27 23:51 ` [iproute PATCH v2 3/3] ss: Drop filter_default_dbs() Phil Sutter
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20180327235156.16680-3-phil@nwl.cc \
--to=phil@nwl.cc \
--cc=netdev@vger.kernel.org \
--cc=stephen@networkplumber.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox