]> git.proxmox.com Git - mirror_iproute2.git/blob - tc/q_dsmark.c
tc: B.W limits can now be specified in %.
[mirror_iproute2.git] / tc / q_dsmark.c
1 /*
2 * q_dsmark.c Differentiated Services field marking.
3 *
4 * Hacked 1998,1999 by Werner Almesberger, EPFL ICA
5 *
6 */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <fcntl.h>
12 #include <sys/socket.h>
13 #include <netinet/in.h>
14 #include <arpa/inet.h>
15 #include <string.h>
16
17 #include "utils.h"
18 #include "tc_util.h"
19
20
21 static void explain(void)
22 {
23 fprintf(stderr,"Usage: dsmark indices INDICES [ default_index DEFAULT_INDEX ] [ set_tc_index ]\n");
24 }
25
26
27 static int dsmark_parse_opt(struct qdisc_util *qu, int argc, char **argv,
28 struct nlmsghdr *n, const char *dev)
29 {
30 struct rtattr *tail;
31 __u16 ind;
32 char *end;
33 int dflt, set_tc_index;
34
35 ind = set_tc_index = 0;
36 dflt = -1;
37 while (argc > 0) {
38 if (!strcmp(*argv, "indices")) {
39 NEXT_ARG();
40 ind = strtoul(*argv, &end, 0);
41 if (*end) {
42 explain();
43 return -1;
44 }
45 } else if (!strcmp(*argv,"default_index") || !strcmp(*argv,
46 "default")) {
47 NEXT_ARG();
48 dflt = strtoul(*argv, &end, 0);
49 if (*end) {
50 explain();
51 return -1;
52 }
53 } else if (!strcmp(*argv,"set_tc_index")) {
54 set_tc_index = 1;
55 } else {
56 explain();
57 return -1;
58 }
59 argc--;
60 argv++;
61 }
62 if (!ind) {
63 explain();
64 return -1;
65 }
66 tail = NLMSG_TAIL(n);
67 addattr_l(n, 1024, TCA_OPTIONS, NULL, 0);
68 addattr_l(n, 1024, TCA_DSMARK_INDICES, &ind, sizeof(ind));
69 if (dflt != -1) {
70 __u16 tmp = dflt;
71
72 addattr_l(n, 1024, TCA_DSMARK_DEFAULT_INDEX, &tmp, sizeof(tmp));
73 }
74 if (set_tc_index) addattr_l(n, 1024, TCA_DSMARK_SET_TC_INDEX, NULL, 0);
75 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
76 return 0;
77 }
78
79
80 static void explain_class(void)
81 {
82 fprintf(stderr, "Usage: ... dsmark [ mask MASK ] [ value VALUE ]\n");
83 }
84
85
86 static int dsmark_parse_class_opt(struct qdisc_util *qu, int argc, char **argv,
87 struct nlmsghdr *n, const char *dev)
88 {
89 struct rtattr *tail;
90 __u8 tmp;
91 char *end;
92
93 tail = NLMSG_TAIL(n);
94 addattr_l(n, 1024, TCA_OPTIONS, NULL, 0);
95 while (argc > 0) {
96 if (!strcmp(*argv, "mask")) {
97 NEXT_ARG();
98 tmp = strtoul(*argv, &end, 0);
99 if (*end) {
100 explain_class();
101 return -1;
102 }
103 addattr_l(n, 1024, TCA_DSMARK_MASK, &tmp, 1);
104 } else if (!strcmp(*argv,"value")) {
105 NEXT_ARG();
106 tmp = strtoul(*argv, &end, 0);
107 if (*end) {
108 explain_class();
109 return -1;
110 }
111 addattr_l(n, 1024, TCA_DSMARK_VALUE, &tmp, 1);
112 } else {
113 explain_class();
114 return -1;
115 }
116 argc--;
117 argv++;
118 }
119 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
120 return 0;
121 }
122
123
124
125 static int dsmark_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
126 {
127 struct rtattr *tb[TCA_DSMARK_MAX+1];
128
129 if (!opt) return 0;
130 parse_rtattr(tb, TCA_DSMARK_MAX, RTA_DATA(opt), RTA_PAYLOAD(opt));
131 if (tb[TCA_DSMARK_MASK]) {
132 if (!RTA_PAYLOAD(tb[TCA_DSMARK_MASK]))
133 fprintf(stderr, "dsmark: empty mask\n");
134 else fprintf(f, "mask 0x%02x ",
135 rta_getattr_u8(tb[TCA_DSMARK_MASK]));
136 }
137 if (tb[TCA_DSMARK_VALUE]) {
138 if (!RTA_PAYLOAD(tb[TCA_DSMARK_VALUE]))
139 fprintf(stderr, "dsmark: empty value\n");
140 else fprintf(f, "value 0x%02x ",
141 rta_getattr_u8(tb[TCA_DSMARK_VALUE]));
142 }
143 if (tb[TCA_DSMARK_INDICES]) {
144 if (RTA_PAYLOAD(tb[TCA_DSMARK_INDICES]) < sizeof(__u16))
145 fprintf(stderr, "dsmark: indices too short\n");
146 else fprintf(f, "indices 0x%04x ",
147 rta_getattr_u16(tb[TCA_DSMARK_INDICES]));
148 }
149 if (tb[TCA_DSMARK_DEFAULT_INDEX]) {
150 if (RTA_PAYLOAD(tb[TCA_DSMARK_DEFAULT_INDEX]) < sizeof(__u16))
151 fprintf(stderr, "dsmark: default_index too short\n");
152 else fprintf(f, "default_index 0x%04x ",
153 rta_getattr_u16(tb[TCA_DSMARK_DEFAULT_INDEX]));
154 }
155 if (tb[TCA_DSMARK_SET_TC_INDEX]) fprintf(f, "set_tc_index ");
156 return 0;
157 }
158
159
160 struct qdisc_util dsmark_qdisc_util = {
161 .id = "dsmark",
162 .parse_qopt = dsmark_parse_opt,
163 .print_qopt = dsmark_print_opt,
164 .parse_copt = dsmark_parse_class_opt,
165 .print_copt = dsmark_print_opt,
166 };