Skip to contentSkip to conversation

tc class: Show class names from file

PATCH Applied Vadim Kochan 3 participants 10 messages Mar 3, 2015 – Mar 16, 2015

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

PATCH 1/1 tc class: Show class names from file

From: Vadim Kochan <[email protected]>

It is possible to use class names from file /etc/iproute2/cls_names which tc will use when showing class info:

    # tc/tc -nm class show dev lo
	class htb 1:10 parent 1:1 leaf 10: prio 0 rate 5Mbit ceil 5Mbit burst 15Kb cburst 1600b
	class htb 1:1 root rate 6Mbit ceil 6Mbit burst 15Kb cburst 1599b
	class htb web#1:20 parent 1:1 leaf 20: prio 0 rate 3Mbit ceil 6Mbit burst 15Kb cburst 1599b
	class htb 1:2 root rate 6Mbit ceil 6Mbit burst 15Kb cburst 1599b
	class htb 1:30 parent 1:1 leaf 30: prio 0 rate 1Kbit ceil 6Mbit burst 15Kb cburst 1599b
	class htb voip#1:40 parent 1:2 leaf 40: prio 0 rate 5Mbit ceil 5Mbit burst 15Kb cburst 1600b
	class htb 1:50 parent 1:2 leaf 50: prio 0 rate 3Mbit ceil 6Mbit burst 15Kb cburst 1599b
	class htb 1:60 parent 1:2 leaf 60: prio 0 rate 1Kbit ceil 6Mbit burst 15Kb cburst 1599b

or to specify via file path:

    # tc/tc -nm -cf /tmp/cls_names class show dev lo

Class names file contains simple "maj:min name" structure:

1:20 web
1:40 voip

---
 include/names.h |  25 +++++++++
 lib/Makefile    |   3 +-
 lib/names.c     | 156 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 tc/tc.c         |  25 ++++++++-
 tc/tc_common.h  |   1 +
 tc/tc_util.c    |  48 ++++++++++++++---
 tc/tc_util.h    |   3 ++
 7 files changed, 252 insertions(+), 9 deletions(-)
 create mode 100644 include/names.h
 create mode 100644 lib/names.c
include/names.h new file +25 -0
new file mode 100644
index 0000000..4123d0b
@@ -0,0 +1,25 @@
+#ifndef DB_NAMES_H_
+#define DB_NAMES_H_ 1
+
+#define IDNAME_MAX 256
+
+struct db_entry {
+	struct db_entry *next;
+	unsigned int id;
+	char *name;
+};
+
+struct db_names {
+	unsigned int size;
+	struct db_entry *cached;
+	struct db_entry **hash;
+	int max;
+};
+
+struct db_names *db_names_alloc(const char *path);
+void db_names_free(struct db_names *db);
+
+char *id_to_name(struct db_names *db, int id, char *name);
+int name_to_id(struct db_names *db, int *id, const char *name);
+
+#endif
lib/Makefile +2 -1
index 66f89f1..4c7cbc2 100644
@@ -6,7 +6,8 @@ endif
 
 CFLAGS += -fPIC
 
-UTILOBJ=utils.o rt_names.o ll_types.o ll_proto.o ll_addr.o inet_proto.o namespace.o
+UTILOBJ=utils.o rt_names.o ll_types.o ll_proto.o ll_addr.o inet_proto.o namespace.o \
+	names.o
 
 NLOBJ=libgenl.o ll_map.o libnetlink.o
 
lib/names.c new file +156 -0

156 lines not on this page · raw message

tc/tc.c +23 -2
index 9380305..22c3be4 100644
@@ -41,6 +41,10 @@ int batch_mode = 0;
 int resolve_hosts = 0;
 int use_iec = 0;
 int force = 0;
+bool use_names = false;
+
+static char *conf_file;
+
 struct rtnl_handle rth;
 
 static void *BODY = NULL;	/* cached handle dlopen(NULL) */
@@ -188,7 +192,8 @@ static void usage(void)
 			"       tc [-force] -batch filename\n"
 	                "where  OBJECT := { qdisc | class | filter | action | monitor }\n"
 	                "       OPTIONS := { -s[tatistics] | -d[etails] | -r[aw] | -p[retty] | -b[atch] [filename] | "
-			"-n[etns] name }\n");
+			"-n[etns] name |\n"
+			"                    -nm | -nam[es] | { -cf | -conf } path }\n");
 }
 
 static int do_cmd(int argc, char **argv)
@@ -293,7 +298,7 @@ int main(int argc, char **argv)
 			return 0;
 		} else if (matches(argv[1], "-force") == 0) {
 			++force;
-		} else 	if (matches(argv[1], "-batch") == 0) {
+		} else if (matches(argv[1], "-batch") == 0) {
 			argc--;	argv++;
 			if (argc <= 1)
 				usage();
@@ -302,6 +307,13 @@ int main(int argc, char **argv)
 			NEXT_ARG();
 			if (netns_switch(argv[1]))
 				return -1;
+		} else if (matches(argv[1], "-names") == 0 ||
+				matches(argv[1], "-nm") == 0) {
+			use_names = true;
+		} else if (matches(argv[1], "-cf") == 0 ||
+				matches(argv[1], "-conf") == 0) {
+			NEXT_ARG();
+			conf_file = argv[1];
 		} else {
 			fprintf(stderr, "Option \"%s\" is unknown, try \"tc -help\".\n", argv[1]);
 			return -1;
@@ -323,8 +335,17 @@ int main(int argc, char **argv)
 		exit(1);
 	}
 
+	if (use_names && cls_names_init(conf_file)) {
+		ret = -1;
+		goto Exit;
+	}
+
 	ret = do_cmd(argc-1, argv+1);
+Exit:
 	rtnl_close(&rth);
 
+	if (use_names)
+		cls_names_uninit();
+
 	return ret;
 }
tc/tc_common.h +1 -0
index ea16f7f..96a0e20 100644
@@ -21,3 +21,4 @@ extern int parse_size_table(int *p_argc, char ***p_argv, struct tc_sizespec *s);
 extern int check_size_table_opts(struct tc_sizespec *s);
 
 extern int show_graph;
+extern bool use_names;
tc/tc_util.c +42 -6

74 lines not on this page · raw message

tc/tc_util.h +3 -0
index d418367..1be1b50 100644
@@ -100,4 +100,7 @@ extern int  parse_action(int *, char ***, int, struct nlmsghdr *);
 extern void print_tm(FILE *f, const struct tcf_t *tm);
 extern int prio_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt);
 
+extern int cls_names_init(char *path);
+extern void cls_names_uninit(void);
+
 #endif
Signature · 1 line
2.2.2
#1 lore raw
Quoted 17 lines · On Tue, 3 Mar 2015 18:41:18 +0200 Vadim Kochan <[email protected]> wrote:

From: Vadim Kochan <[email protected]>

It is possible to use class names from file /etc/iproute2/cls_names which tc will use when showing class info:

    # tc/tc -nm class show dev lo
	class htb 1:10 parent 1:1 leaf 10: prio 0 rate 5Mbit ceil 5Mbit burst 15Kb cburst 1600b
	class htb 1:1 root rate 6Mbit ceil 6Mbit burst 15Kb cburst 1599b
	class htb web#1:20 parent 1:1 leaf 20: prio 0 rate 3Mbit ceil 6Mbit burst 15Kb cburst 1599b
	class htb 1:2 root rate 6Mbit ceil 6Mbit burst 15Kb cburst 1599b
	class htb 1:30 parent 1:1 leaf 30: prio 0 rate 1Kbit ceil 6Mbit burst 15Kb cburst 1599b
	class htb voip#1:40 parent 1:2 leaf 40: prio 0 rate 5Mbit ceil 5Mbit burst 15Kb cburst 1600b
	class htb 1:50 parent 1:2 leaf 50: prio 0 rate 3Mbit ceil 6Mbit burst 15Kb cburst 1599b
	class htb 1:60 parent 1:2 leaf 60: prio 0 rate 1Kbit ceil 6Mbit burst 15Kb cburst 1599b

or to specify via file path:

Quoted 8 lines
    # tc/tc -nm -cf /tmp/cls_names class show dev lo

Class names file contains simple "maj:min name" structure:

1:20 web
1:40 voip

This adds new dependency on berkely db.
See how iproute handled that for arpd and use same #ifdef and config.

#2 lore raw
Quoted 24 lines · Stephen Hemminger
Quoted 21 lines · On Tue, 3 Mar 2015 18:41:18 +0200 Vadim Kochan <[email protected]> wrote:

From: Vadim Kochan <[email protected]>

It is possible to use class names from file /etc/iproute2/cls_names which tc will use when showing class info:

    # tc/tc -nm class show dev lo
	class htb 1:10 parent 1:1 leaf 10: prio 0 rate 5Mbit ceil 5Mbit burst 15Kb cburst 1600b
	class htb 1:1 root rate 6Mbit ceil 6Mbit burst 15Kb cburst 1599b
	class htb web#1:20 parent 1:1 leaf 20: prio 0 rate 3Mbit ceil 6Mbit burst 15Kb cburst 1599b
	class htb 1:2 root rate 6Mbit ceil 6Mbit burst 15Kb cburst 1599b
	class htb 1:30 parent 1:1 leaf 30: prio 0 rate 1Kbit ceil 6Mbit burst 15Kb cburst 1599b
	class htb voip#1:40 parent 1:2 leaf 40: prio 0 rate 5Mbit ceil 5Mbit burst 15Kb cburst 1600b
	class htb 1:50 parent 1:2 leaf 50: prio 0 rate 3Mbit ceil 6Mbit burst 15Kb cburst 1599b
	class htb 1:60 parent 1:2 leaf 60: prio 0 rate 1Kbit ceil 6Mbit burst 15Kb cburst 1599b

or to specify via file path:

    # tc/tc -nm -cf /tmp/cls_names class show dev lo

Class names file contains simple "maj:min name" structure:

Quoted 7 lines
Quoted 4 lines

1:20 web
1:40 voip

This adds new dependency on berkely db.
See how iproute handled that for arpd and use same #ifdef and config.

Sorry I did not get how this adds the dependency for berkely db ? Would you please help me to understand the problem ?

Thanks,

#3 lore raw
Quoted 4 lines · On Tue, 3 Mar 2015 20:52:49 +0200 Vadim Kochan <[email protected]> wrote:

Sorry I did not get how this adds the dependency for berkely db ? Would you please help me to understand the problem ?

Thanks,

Sorry, i was thinking and typing at same time.
I was thinking this would be a good case for using a berkely db for 1000's of classes, but a file works ok.

#4 lore raw
Quoted 11 lines · Stephen Hemminger
Quoted 4 lines · On Tue, 3 Mar 2015 20:52:49 +0200 Vadim Kochan <[email protected]> wrote:

Sorry I did not get how this adds the dependency for berkely db ? Would you please help me to understand the problem ?

Thanks,

Sorry, i was thinking and typing at same time.
I was thinking this would be a good case for using a berkely db for 1000's of classes, but a file works ok.

I think that /etc/iproute2/tc_cls might be better name for the default db file.

Also if this feature have chance to be applied I will try to add ability to use class name to other places - 'tc filter', 'tc class del/change/show'.

#5 lore raw
In reply to Vadim Kochan
Quoted 17 lines · On Tue, 3 Mar 2015 18:41:18 +0200 Vadim Kochan <[email protected]> wrote:

From: Vadim Kochan <[email protected]>

It is possible to use class names from file /etc/iproute2/cls_names which tc will use when showing class info:

    # tc/tc -nm class show dev lo
	class htb 1:10 parent 1:1 leaf 10: prio 0 rate 5Mbit ceil 5Mbit burst 15Kb cburst 1600b
	class htb 1:1 root rate 6Mbit ceil 6Mbit burst 15Kb cburst 1599b
	class htb web#1:20 parent 1:1 leaf 20: prio 0 rate 3Mbit ceil 6Mbit burst 15Kb cburst 1599b
	class htb 1:2 root rate 6Mbit ceil 6Mbit burst 15Kb cburst 1599b
	class htb 1:30 parent 1:1 leaf 30: prio 0 rate 1Kbit ceil 6Mbit burst 15Kb cburst 1599b
	class htb voip#1:40 parent 1:2 leaf 40: prio 0 rate 5Mbit ceil 5Mbit burst 15Kb cburst 1600b
	class htb 1:50 parent 1:2 leaf 50: prio 0 rate 3Mbit ceil 6Mbit burst 15Kb cburst 1599b
	class htb 1:60 parent 1:2 leaf 60: prio 0 rate 1Kbit ceil 6Mbit burst 15Kb cburst 1599b

or to specify via file path:

Quoted 8 lines
    # tc/tc -nm -cf /tmp/cls_names class show dev lo

Class names file contains simple "maj:min name" structure:

1:20 web
1:40 voip

Applied, thanks

#6 lore raw
In reply to Vadim Kochan

Hi Vadim,

Quoted 17 lines · Vadim Kochan

From: Vadim Kochan <[email protected]>

It is possible to use class names from file /etc/iproute2/cls_names which tc will use when showing class info:

     # tc/tc -nm class show dev lo
	class htb 1:10 parent 1:1 leaf 10: prio 0 rate 5Mbit ceil 5Mbit burst 15Kb cburst 1600b
	class htb 1:1 root rate 6Mbit ceil 6Mbit burst 15Kb cburst 1599b
	class htb web#1:20 parent 1:1 leaf 20: prio 0 rate 3Mbit ceil 6Mbit burst 15Kb cburst 1599b
	class htb 1:2 root rate 6Mbit ceil 6Mbit burst 15Kb cburst 1599b
	class htb 1:30 parent 1:1 leaf 30: prio 0 rate 1Kbit ceil 6Mbit burst 15Kb cburst 1599b
	class htb voip#1:40 parent 1:2 leaf 40: prio 0 rate 5Mbit ceil 5Mbit burst 15Kb cburst 1600b
	class htb 1:50 parent 1:2 leaf 50: prio 0 rate 3Mbit ceil 6Mbit burst 15Kb cburst 1599b
	class htb 1:60 parent 1:2 leaf 60: prio 0 rate 1Kbit ceil 6Mbit burst 15Kb cburst 1599b

or to specify via file path:

Quoted 8 lines
     # tc/tc -nm -cf /tmp/cls_names class show dev lo

Class names file contains simple "maj:min name" structure:

1:20 web
1:40 voip

in your patch, there are a couple of malloc() calls in your db creation, that you should check if they actually fail, see i.e. db_names_alloc(). Would be good if you could follow-up with a fix.

Thanks,
Daniel

#7 lore raw
Quoted 27 lines · Daniel Borkmann

Hi Vadim,

Quoted 24 lines · Vadim Kochan

From: Vadim Kochan <[email protected]>

It is possible to use class names from file /etc/iproute2/cls_names which tc will use when showing class info:

    # tc/tc -nm class show dev lo
	class htb 1:10 parent 1:1 leaf 10: prio 0 rate 5Mbit ceil 5Mbit burst 15Kb cburst 1600b
	class htb 1:1 root rate 6Mbit ceil 6Mbit burst 15Kb cburst 1599b
	class htb web#1:20 parent 1:1 leaf 20: prio 0 rate 3Mbit ceil 6Mbit burst 15Kb cburst 1599b
	class htb 1:2 root rate 6Mbit ceil 6Mbit burst 15Kb cburst 1599b
	class htb 1:30 parent 1:1 leaf 30: prio 0 rate 1Kbit ceil 6Mbit burst 15Kb cburst 1599b
	class htb voip#1:40 parent 1:2 leaf 40: prio 0 rate 5Mbit ceil 5Mbit burst 15Kb cburst 1600b
	class htb 1:50 parent 1:2 leaf 50: prio 0 rate 3Mbit ceil 6Mbit burst 15Kb cburst 1599b
	class htb 1:60 parent 1:2 leaf 60: prio 0 rate 1Kbit ceil 6Mbit burst 15Kb cburst 1599b

or to specify via file path:

    # tc/tc -nm -cf /tmp/cls_names class show dev lo

Class names file contains simple "maj:min name" structure:

1:20 web
1:40 voip

Quoted 8 lines
Quoted 1 line

in your patch, there are a couple of malloc() calls in your db creation, that you should check if they actually fail, see i.e. db_names_alloc(). Would be good if you could follow-up with a fix.

Thanks,
Daniel

Hi,

Sure I will do.

Also I was thinking about to get rid of this '-nm | -name' option and resolve class names by default if they are in /etc/iproute2/cls_names or specified by -cf option, what do you think ?

Regards,

#8 lore raw

On 03/16/2015 02:22 PM, Vadim Kochan wrote:
...

Quoted 3 lines

Also I was thinking about to get rid of this '-nm | -name' option and resolve class names by default if they are in /etc/iproute2/cls_names or specified by -cf option, what do you think ?

Generally, I think that would be good, but it could break applications or shell scripts' expectations if they do parse tc output. I think if admins do care, they could simply set up an alias.

Cheers,
Daniel

#9 lore raw
Quoted 6 lines · Daniel Borkmann

On 03/16/2015 02:22 PM, Vadim Kochan wrote:
...

Quoted 3 lines

Also I was thinking about to get rid of this '-nm | -name' option and resolve class names by default if they are in /etc/iproute2/cls_names or specified by -cf option, what do you think ?

Quoted 6 lines

Generally, I think that would be good, but it could break applications or shell scripts' expectations if they do parse tc output. I think if admins do care, they could simply set up an alias.

Cheers,
Daniel

Hm, yes, so may be let this option exist, but then may be it should not fail if the default file /etc/iproute2/cls_names does not exist.

Regards,