blob: 79dfd9a27e3436e2ca83a3c8fd210d27d4b013d7 [file] [log] [blame]
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001/*
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 <syslog.h>
12#include <fcntl.h>
13#include <sys/socket.h>
14#include <netinet/in.h>
15#include <arpa/inet.h>
16#include <string.h>
17
18#include "utils.h"
19#include "tc_util.h"
20
21
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000022static void explain(void)
23{
Stephen Hemminger32a121c2016-03-21 11:48:36 -070024 fprintf(stderr,"Usage: dsmark indices INDICES [ default_index DEFAULT_INDEX ] [ set_tc_index ]\n");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000025}
26
27
28static int dsmark_parse_opt(struct qdisc_util *qu, int argc, char **argv,
29 struct nlmsghdr *n)
30{
31 struct rtattr *tail;
32 __u16 ind;
33 char *end;
Stephen Hemminger32a121c2016-03-21 11:48:36 -070034 int dflt, set_tc_index;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000035
36 ind = set_tc_index = 0;
37 dflt = -1;
38 while (argc > 0) {
Stephen Hemminger32a121c2016-03-21 11:48:36 -070039 if (!strcmp(*argv, "indices")) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000040 NEXT_ARG();
Stephen Hemminger32a121c2016-03-21 11:48:36 -070041 ind = strtoul(*argv, &end, 0);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000042 if (*end) {
43 explain();
44 return -1;
45 }
Stephen Hemminger32a121c2016-03-21 11:48:36 -070046 } else if (!strcmp(*argv,"default_index") || !strcmp(*argv,
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000047 "default")) {
48 NEXT_ARG();
Stephen Hemminger32a121c2016-03-21 11:48:36 -070049 dflt = strtoul(*argv, &end, 0);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000050 if (*end) {
51 explain();
52 return -1;
53 }
Stephen Hemminger32a121c2016-03-21 11:48:36 -070054 } else if (!strcmp(*argv,"set_tc_index")) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000055 set_tc_index = 1;
Stephen Hemminger32a121c2016-03-21 11:48:36 -070056 } else {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000057 explain();
58 return -1;
59 }
60 argc--;
61 argv++;
62 }
63 if (!ind) {
64 explain();
65 return -1;
66 }
4!tgraf228569c2005-01-18 01:24:18 +000067 tail = NLMSG_TAIL(n);
Stephen Hemminger32a121c2016-03-21 11:48:36 -070068 addattr_l(n, 1024, TCA_OPTIONS, NULL, 0);
69 addattr_l(n, 1024, TCA_DSMARK_INDICES, &ind, sizeof(ind));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000070 if (dflt != -1) {
71 __u16 tmp = dflt;
72
Stephen Hemminger32a121c2016-03-21 11:48:36 -070073 addattr_l(n, 1024, TCA_DSMARK_DEFAULT_INDEX, &tmp, sizeof(tmp));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000074 }
Stephen Hemminger32a121c2016-03-21 11:48:36 -070075 if (set_tc_index) addattr_l(n, 1024, TCA_DSMARK_SET_TC_INDEX, NULL, 0);
4!tgraf228569c2005-01-18 01:24:18 +000076 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000077 return 0;
78}
79
80
81static void explain_class(void)
82{
83 fprintf(stderr, "Usage: ... dsmark [ mask MASK ] [ value VALUE ]\n");
84}
85
86
87static int dsmark_parse_class_opt(struct qdisc_util *qu, int argc, char **argv,
88 struct nlmsghdr *n)
89{
90 struct rtattr *tail;
91 __u8 tmp;
92 char *end;
93
4!tgraf228569c2005-01-18 01:24:18 +000094 tail = NLMSG_TAIL(n);
Stephen Hemminger32a121c2016-03-21 11:48:36 -070095 addattr_l(n, 1024, TCA_OPTIONS, NULL, 0);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000096 while (argc > 0) {
Stephen Hemminger32a121c2016-03-21 11:48:36 -070097 if (!strcmp(*argv, "mask")) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000098 NEXT_ARG();
Stephen Hemminger32a121c2016-03-21 11:48:36 -070099 tmp = strtoul(*argv, &end, 0);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000100 if (*end) {
101 explain_class();
102 return -1;
103 }
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700104 addattr_l(n, 1024, TCA_DSMARK_MASK, &tmp, 1);
105 } else if (!strcmp(*argv,"value")) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000106 NEXT_ARG();
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700107 tmp = strtoul(*argv, &end, 0);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000108 if (*end) {
109 explain_class();
110 return -1;
111 }
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700112 addattr_l(n, 1024, TCA_DSMARK_VALUE, &tmp, 1);
113 } else {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000114 explain_class();
115 return -1;
116 }
117 argc--;
118 argv++;
119 }
4!tgraf228569c2005-01-18 01:24:18 +0000120 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000121 return 0;
122}
123
124
125
126static int dsmark_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
127{
128 struct rtattr *tb[TCA_DSMARK_MAX+1];
129
130 if (!opt) return 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000131 parse_rtattr(tb, TCA_DSMARK_MAX, RTA_DATA(opt), RTA_PAYLOAD(opt));
132 if (tb[TCA_DSMARK_MASK]) {
133 if (!RTA_PAYLOAD(tb[TCA_DSMARK_MASK]))
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700134 fprintf(stderr, "dsmark: empty mask\n");
135 else fprintf(f, "mask 0x%02x ",
Stephen Hemmingerff247462012-04-10 08:47:55 -0700136 rta_getattr_u8(tb[TCA_DSMARK_MASK]));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000137 }
138 if (tb[TCA_DSMARK_VALUE]) {
139 if (!RTA_PAYLOAD(tb[TCA_DSMARK_VALUE]))
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700140 fprintf(stderr, "dsmark: empty value\n");
141 else fprintf(f, "value 0x%02x ",
Stephen Hemmingerff247462012-04-10 08:47:55 -0700142 rta_getattr_u8(tb[TCA_DSMARK_VALUE]));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000143 }
144 if (tb[TCA_DSMARK_INDICES]) {
145 if (RTA_PAYLOAD(tb[TCA_DSMARK_INDICES]) < sizeof(__u16))
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700146 fprintf(stderr, "dsmark: indices too short\n");
147 else fprintf(f, "indices 0x%04x ",
Stephen Hemmingerff247462012-04-10 08:47:55 -0700148 rta_getattr_u16(tb[TCA_DSMARK_INDICES]));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000149 }
150 if (tb[TCA_DSMARK_DEFAULT_INDEX]) {
151 if (RTA_PAYLOAD(tb[TCA_DSMARK_DEFAULT_INDEX]) < sizeof(__u16))
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700152 fprintf(stderr, "dsmark: default_index too short\n");
153 else fprintf(f, "default_index 0x%04x ",
Stephen Hemmingerff247462012-04-10 08:47:55 -0700154 rta_getattr_u16(tb[TCA_DSMARK_DEFAULT_INDEX]));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000155 }
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700156 if (tb[TCA_DSMARK_SET_TC_INDEX]) fprintf(f, "set_tc_index ");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000157 return 0;
158}
159
160
net[shemminger]!kaber95812b52004-09-28 18:35:49 +0000161struct qdisc_util dsmark_qdisc_util = {
osdl.net!shemmingerf2f99e22004-08-31 17:45:21 +0000162 .id = "dsmark",
163 .parse_qopt = dsmark_parse_opt,
164 .print_qopt = dsmark_print_opt,
165 .parse_copt = dsmark_parse_class_opt,
166 .print_copt = dsmark_print_opt,
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000167};