1 /*
2 * tc_stab.c "tc qdisc ... stab *".
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * Authors: Jussi Kivilinna, <jussi.kivilinna@mbnet.fi>
10 *
11 */
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include <syslog.h>
17 #include <fcntl.h>
18 #include <math.h>
19 #include <sys/socket.h>
20 #include <sys/param.h>
21 #include <netinet/in.h>
22 #include <arpa/inet.h>
23 #include <string.h>
24 #include <malloc.h>
25
26 #include "utils.h"
27 #include "tc_util.h"
28 #include "tc_core.h"
29 #include "tc_common.h"
30
stab_help(void)31 static void stab_help(void)
32 {
33 fprintf(stderr,
34 "Usage: ... stab [ mtu BYTES ] [ tsize SLOTS ] [ mpu BYTES ]\n"
35 " [ overhead BYTES ] [ linklayer TYPE ] ...\n"
36 " mtu : max packet size we create rate map for {2047}\n"
37 " tsize : how many slots should size table have {512}\n"
38 " mpu : minimum packet size used in rate computations\n"
39 " overhead : per-packet size overhead used in rate computations\n"
40 " linklayer : adapting to a linklayer e.g. atm\n"
41 "Example: ... stab overhead 20 linklayer atm\n");
42
43 }
44
check_size_table_opts(struct tc_sizespec * s)45 int check_size_table_opts(struct tc_sizespec *s)
46 {
47 return s->linklayer >= LINKLAYER_ETHERNET || s->mpu != 0 ||
48 s->overhead != 0;
49 }
50
parse_size_table(int * argcp,char *** argvp,struct tc_sizespec * sp)51 int parse_size_table(int *argcp, char ***argvp, struct tc_sizespec *sp)
52 {
53 char **argv = *argvp;
54 int argc = *argcp;
55 struct tc_sizespec s = {};
56
57 NEXT_ARG();
58 if (matches(*argv, "help") == 0) {
59 stab_help();
60 return -1;
61 }
62 while (argc > 0) {
63 if (matches(*argv, "mtu") == 0) {
64 NEXT_ARG();
65 if (s.mtu)
66 duparg("mtu", *argv);
67 if (get_u32(&s.mtu, *argv, 10))
68 invarg("mtu", "invalid mtu");
69 } else if (matches(*argv, "mpu") == 0) {
70 NEXT_ARG();
71 if (s.mpu)
72 duparg("mpu", *argv);
73 if (get_u32(&s.mpu, *argv, 10))
74 invarg("mpu", "invalid mpu");
75 } else if (matches(*argv, "overhead") == 0) {
76 NEXT_ARG();
77 if (s.overhead)
78 duparg("overhead", *argv);
79 if (get_integer(&s.overhead, *argv, 10))
80 invarg("overhead", "invalid overhead");
81 } else if (matches(*argv, "tsize") == 0) {
82 NEXT_ARG();
83 if (s.tsize)
84 duparg("tsize", *argv);
85 if (get_u32(&s.tsize, *argv, 10))
86 invarg("tsize", "invalid table size");
87 } else if (matches(*argv, "linklayer") == 0) {
88 NEXT_ARG();
89 if (s.linklayer != LINKLAYER_UNSPEC)
90 duparg("linklayer", *argv);
91 if (get_linklayer(&s.linklayer, *argv))
92 invarg("linklayer", "invalid linklayer");
93 } else
94 break;
95 argc--; argv++;
96 }
97
98 if (!check_size_table_opts(&s))
99 return -1;
100
101 *sp = s;
102 *argvp = argv;
103 *argcp = argc;
104 return 0;
105 }
106
print_size_table(FILE * fp,const char * prefix,struct rtattr * rta)107 void print_size_table(FILE *fp, const char *prefix, struct rtattr *rta)
108 {
109 struct rtattr *tb[TCA_STAB_MAX + 1];
110
111 SPRINT_BUF(b1);
112
113 parse_rtattr_nested(tb, TCA_STAB_MAX, rta);
114
115 if (tb[TCA_STAB_BASE]) {
116 struct tc_sizespec s = {0};
117
118 memcpy(&s, RTA_DATA(tb[TCA_STAB_BASE]),
119 MIN(RTA_PAYLOAD(tb[TCA_STAB_BASE]), sizeof(s)));
120
121 fprintf(fp, "%s", prefix);
122 if (s.linklayer)
123 fprintf(fp, "linklayer %s ",
124 sprint_linklayer(s.linklayer, b1));
125 if (s.overhead)
126 fprintf(fp, "overhead %d ", s.overhead);
127 if (s.mpu)
128 fprintf(fp, "mpu %u ", s.mpu);
129 if (s.mtu)
130 fprintf(fp, "mtu %u ", s.mtu);
131 if (s.tsize)
132 fprintf(fp, "tsize %u ", s.tsize);
133 }
134
135 #if 0
136 if (tb[TCA_STAB_DATA]) {
137 unsigned int i, j, dlen;
138 __u16 *data = RTA_DATA(tb[TCA_STAB_DATA]);
139
140 dlen = RTA_PAYLOAD(tb[TCA_STAB_DATA]) / sizeof(__u16);
141
142 fprintf(fp, "\n%sstab data:", prefix);
143 for (i = 0; i < dlen/12; i++) {
144 fprintf(fp, "\n%s %3u:", prefix, i * 12);
145 for (j = 0; i * 12 + j < dlen; j++)
146 fprintf(fp, " %05x", data[i * 12 + j]);
147 }
148 }
149 #endif
150 }
151