]>
Commit | Line | Data |
---|---|---|
aba5acdf SH |
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> | |
aba5acdf SH |
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 | ||
aba5acdf SH |
21 | static void explain(void) |
22 | { | |
32a121cb | 23 | fprintf(stderr,"Usage: dsmark indices INDICES [ default_index DEFAULT_INDEX ] [ set_tc_index ]\n"); |
aba5acdf SH |
24 | } |
25 | ||
26 | ||
27 | static int dsmark_parse_opt(struct qdisc_util *qu, int argc, char **argv, | |
927e3cfb | 28 | struct nlmsghdr *n, const char *dev) |
aba5acdf SH |
29 | { |
30 | struct rtattr *tail; | |
31 | __u16 ind; | |
32 | char *end; | |
32a121cb | 33 | int dflt, set_tc_index; |
aba5acdf SH |
34 | |
35 | ind = set_tc_index = 0; | |
36 | dflt = -1; | |
37 | while (argc > 0) { | |
32a121cb | 38 | if (!strcmp(*argv, "indices")) { |
aba5acdf | 39 | NEXT_ARG(); |
32a121cb | 40 | ind = strtoul(*argv, &end, 0); |
aba5acdf SH |
41 | if (*end) { |
42 | explain(); | |
43 | return -1; | |
44 | } | |
32a121cb | 45 | } else if (!strcmp(*argv,"default_index") || !strcmp(*argv, |
aba5acdf SH |
46 | "default")) { |
47 | NEXT_ARG(); | |
32a121cb | 48 | dflt = strtoul(*argv, &end, 0); |
aba5acdf SH |
49 | if (*end) { |
50 | explain(); | |
51 | return -1; | |
52 | } | |
32a121cb | 53 | } else if (!strcmp(*argv,"set_tc_index")) { |
aba5acdf | 54 | set_tc_index = 1; |
32a121cb | 55 | } else { |
aba5acdf SH |
56 | explain(); |
57 | return -1; | |
58 | } | |
59 | argc--; | |
60 | argv++; | |
61 | } | |
62 | if (!ind) { | |
63 | explain(); | |
64 | return -1; | |
65 | } | |
228569c3 | 66 | tail = NLMSG_TAIL(n); |
32a121cb SH |
67 | addattr_l(n, 1024, TCA_OPTIONS, NULL, 0); |
68 | addattr_l(n, 1024, TCA_DSMARK_INDICES, &ind, sizeof(ind)); | |
aba5acdf SH |
69 | if (dflt != -1) { |
70 | __u16 tmp = dflt; | |
71 | ||
32a121cb | 72 | addattr_l(n, 1024, TCA_DSMARK_DEFAULT_INDEX, &tmp, sizeof(tmp)); |
aba5acdf | 73 | } |
32a121cb | 74 | if (set_tc_index) addattr_l(n, 1024, TCA_DSMARK_SET_TC_INDEX, NULL, 0); |
228569c3 | 75 | tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail; |
aba5acdf SH |
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, | |
927e3cfb | 87 | struct nlmsghdr *n, const char *dev) |
aba5acdf SH |
88 | { |
89 | struct rtattr *tail; | |
90 | __u8 tmp; | |
91 | char *end; | |
92 | ||
228569c3 | 93 | tail = NLMSG_TAIL(n); |
32a121cb | 94 | addattr_l(n, 1024, TCA_OPTIONS, NULL, 0); |
aba5acdf | 95 | while (argc > 0) { |
32a121cb | 96 | if (!strcmp(*argv, "mask")) { |
aba5acdf | 97 | NEXT_ARG(); |
32a121cb | 98 | tmp = strtoul(*argv, &end, 0); |
aba5acdf SH |
99 | if (*end) { |
100 | explain_class(); | |
101 | return -1; | |
102 | } | |
32a121cb SH |
103 | addattr_l(n, 1024, TCA_DSMARK_MASK, &tmp, 1); |
104 | } else if (!strcmp(*argv,"value")) { | |
aba5acdf | 105 | NEXT_ARG(); |
32a121cb | 106 | tmp = strtoul(*argv, &end, 0); |
aba5acdf SH |
107 | if (*end) { |
108 | explain_class(); | |
109 | return -1; | |
110 | } | |
32a121cb SH |
111 | addattr_l(n, 1024, TCA_DSMARK_VALUE, &tmp, 1); |
112 | } else { | |
aba5acdf SH |
113 | explain_class(); |
114 | return -1; | |
115 | } | |
116 | argc--; | |
117 | argv++; | |
118 | } | |
228569c3 | 119 | tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail; |
aba5acdf SH |
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; | |
aba5acdf SH |
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])) | |
32a121cb SH |
133 | fprintf(stderr, "dsmark: empty mask\n"); |
134 | else fprintf(f, "mask 0x%02x ", | |
ff24746c | 135 | rta_getattr_u8(tb[TCA_DSMARK_MASK])); |
aba5acdf SH |
136 | } |
137 | if (tb[TCA_DSMARK_VALUE]) { | |
138 | if (!RTA_PAYLOAD(tb[TCA_DSMARK_VALUE])) | |
32a121cb SH |
139 | fprintf(stderr, "dsmark: empty value\n"); |
140 | else fprintf(f, "value 0x%02x ", | |
ff24746c | 141 | rta_getattr_u8(tb[TCA_DSMARK_VALUE])); |
aba5acdf SH |
142 | } |
143 | if (tb[TCA_DSMARK_INDICES]) { | |
144 | if (RTA_PAYLOAD(tb[TCA_DSMARK_INDICES]) < sizeof(__u16)) | |
32a121cb SH |
145 | fprintf(stderr, "dsmark: indices too short\n"); |
146 | else fprintf(f, "indices 0x%04x ", | |
ff24746c | 147 | rta_getattr_u16(tb[TCA_DSMARK_INDICES])); |
aba5acdf SH |
148 | } |
149 | if (tb[TCA_DSMARK_DEFAULT_INDEX]) { | |
150 | if (RTA_PAYLOAD(tb[TCA_DSMARK_DEFAULT_INDEX]) < sizeof(__u16)) | |
32a121cb SH |
151 | fprintf(stderr, "dsmark: default_index too short\n"); |
152 | else fprintf(f, "default_index 0x%04x ", | |
ff24746c | 153 | rta_getattr_u16(tb[TCA_DSMARK_DEFAULT_INDEX])); |
aba5acdf | 154 | } |
32a121cb | 155 | if (tb[TCA_DSMARK_SET_TC_INDEX]) fprintf(f, "set_tc_index "); |
aba5acdf SH |
156 | return 0; |
157 | } | |
158 | ||
159 | ||
95812b56 | 160 | struct qdisc_util dsmark_qdisc_util = { |
f2f99e2e SH |
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, | |
aba5acdf | 166 | }; |