osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 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 <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!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 22 | static void explain(void) |
| 23 | { |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 24 | fprintf(stderr,"Usage: dsmark indices INDICES [ default_index DEFAULT_INDEX ] [ set_tc_index ]\n"); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | |
| 28 | static 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 Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 34 | int dflt, set_tc_index; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 35 | |
| 36 | ind = set_tc_index = 0; |
| 37 | dflt = -1; |
| 38 | while (argc > 0) { |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 39 | if (!strcmp(*argv, "indices")) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 40 | NEXT_ARG(); |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 41 | ind = strtoul(*argv, &end, 0); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 42 | if (*end) { |
| 43 | explain(); |
| 44 | return -1; |
| 45 | } |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 46 | } else if (!strcmp(*argv,"default_index") || !strcmp(*argv, |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 47 | "default")) { |
| 48 | NEXT_ARG(); |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 49 | dflt = strtoul(*argv, &end, 0); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 50 | if (*end) { |
| 51 | explain(); |
| 52 | return -1; |
| 53 | } |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 54 | } else if (!strcmp(*argv,"set_tc_index")) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 55 | set_tc_index = 1; |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 56 | } else { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 57 | explain(); |
| 58 | return -1; |
| 59 | } |
| 60 | argc--; |
| 61 | argv++; |
| 62 | } |
| 63 | if (!ind) { |
| 64 | explain(); |
| 65 | return -1; |
| 66 | } |
4!tgraf | 228569c | 2005-01-18 01:24:18 +0000 | [diff] [blame] | 67 | tail = NLMSG_TAIL(n); |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 68 | addattr_l(n, 1024, TCA_OPTIONS, NULL, 0); |
| 69 | addattr_l(n, 1024, TCA_DSMARK_INDICES, &ind, sizeof(ind)); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 70 | if (dflt != -1) { |
| 71 | __u16 tmp = dflt; |
| 72 | |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 73 | addattr_l(n, 1024, TCA_DSMARK_DEFAULT_INDEX, &tmp, sizeof(tmp)); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 74 | } |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 75 | if (set_tc_index) addattr_l(n, 1024, TCA_DSMARK_SET_TC_INDEX, NULL, 0); |
4!tgraf | 228569c | 2005-01-18 01:24:18 +0000 | [diff] [blame] | 76 | tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | |
| 81 | static void explain_class(void) |
| 82 | { |
| 83 | fprintf(stderr, "Usage: ... dsmark [ mask MASK ] [ value VALUE ]\n"); |
| 84 | } |
| 85 | |
| 86 | |
| 87 | static 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!tgraf | 228569c | 2005-01-18 01:24:18 +0000 | [diff] [blame] | 94 | tail = NLMSG_TAIL(n); |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 95 | addattr_l(n, 1024, TCA_OPTIONS, NULL, 0); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 96 | while (argc > 0) { |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 97 | if (!strcmp(*argv, "mask")) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 98 | NEXT_ARG(); |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 99 | tmp = strtoul(*argv, &end, 0); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 100 | if (*end) { |
| 101 | explain_class(); |
| 102 | return -1; |
| 103 | } |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 104 | addattr_l(n, 1024, TCA_DSMARK_MASK, &tmp, 1); |
| 105 | } else if (!strcmp(*argv,"value")) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 106 | NEXT_ARG(); |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 107 | tmp = strtoul(*argv, &end, 0); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 108 | if (*end) { |
| 109 | explain_class(); |
| 110 | return -1; |
| 111 | } |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 112 | addattr_l(n, 1024, TCA_DSMARK_VALUE, &tmp, 1); |
| 113 | } else { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 114 | explain_class(); |
| 115 | return -1; |
| 116 | } |
| 117 | argc--; |
| 118 | argv++; |
| 119 | } |
4!tgraf | 228569c | 2005-01-18 01:24:18 +0000 | [diff] [blame] | 120 | tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | |
| 125 | |
| 126 | static 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!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 131 | 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 Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 134 | fprintf(stderr, "dsmark: empty mask\n"); |
| 135 | else fprintf(f, "mask 0x%02x ", |
Stephen Hemminger | ff24746 | 2012-04-10 08:47:55 -0700 | [diff] [blame] | 136 | rta_getattr_u8(tb[TCA_DSMARK_MASK])); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 137 | } |
| 138 | if (tb[TCA_DSMARK_VALUE]) { |
| 139 | if (!RTA_PAYLOAD(tb[TCA_DSMARK_VALUE])) |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 140 | fprintf(stderr, "dsmark: empty value\n"); |
| 141 | else fprintf(f, "value 0x%02x ", |
Stephen Hemminger | ff24746 | 2012-04-10 08:47:55 -0700 | [diff] [blame] | 142 | rta_getattr_u8(tb[TCA_DSMARK_VALUE])); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 143 | } |
| 144 | if (tb[TCA_DSMARK_INDICES]) { |
| 145 | if (RTA_PAYLOAD(tb[TCA_DSMARK_INDICES]) < sizeof(__u16)) |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 146 | fprintf(stderr, "dsmark: indices too short\n"); |
| 147 | else fprintf(f, "indices 0x%04x ", |
Stephen Hemminger | ff24746 | 2012-04-10 08:47:55 -0700 | [diff] [blame] | 148 | rta_getattr_u16(tb[TCA_DSMARK_INDICES])); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 149 | } |
| 150 | if (tb[TCA_DSMARK_DEFAULT_INDEX]) { |
| 151 | if (RTA_PAYLOAD(tb[TCA_DSMARK_DEFAULT_INDEX]) < sizeof(__u16)) |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 152 | fprintf(stderr, "dsmark: default_index too short\n"); |
| 153 | else fprintf(f, "default_index 0x%04x ", |
Stephen Hemminger | ff24746 | 2012-04-10 08:47:55 -0700 | [diff] [blame] | 154 | rta_getattr_u16(tb[TCA_DSMARK_DEFAULT_INDEX])); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 155 | } |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 156 | if (tb[TCA_DSMARK_SET_TC_INDEX]) fprintf(f, "set_tc_index "); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | |
net[shemminger]!kaber | 95812b5 | 2004-09-28 18:35:49 +0000 | [diff] [blame] | 161 | struct qdisc_util dsmark_qdisc_util = { |
osdl.net!shemminger | f2f99e2 | 2004-08-31 17:45:21 +0000 | [diff] [blame] | 162 | .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!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 167 | }; |