]> git.proxmox.com Git - mirror_iproute2.git/blob - tc/q_atm.c
tc: B.W limits can now be specified in %.
[mirror_iproute2.git] / tc / q_atm.c
1 /*
2 * q_atm.c ATM.
3 *
4 * Hacked 1998-2000 by Werner Almesberger, EPFL ICA
5 *
6 */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <ctype.h>
12 #include <fcntl.h>
13 #include <sys/socket.h>
14 #include <sys/ioctl.h>
15 #include <netinet/in.h>
16 #include <arpa/inet.h>
17 #include <string.h>
18 #include <atm.h>
19 #include <linux/atmdev.h>
20 #include <linux/atmarp.h>
21
22 #include "utils.h"
23 #include "tc_util.h"
24
25
26 #define MAX_HDR_LEN 64
27
28
29 static int atm_parse_opt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n, const char *dev)
30 {
31 if (argc) {
32 fprintf(stderr, "Usage: atm\n");
33 return -1;
34 }
35 return 0;
36 }
37
38
39 static void explain(void)
40 {
41 fprintf(stderr, "Usage: ... atm ( pvc ADDR | svc ADDR [ sap SAP ] ) [ qos QOS ] [ sndbuf BYTES ]\n");
42 fprintf(stderr, " [ hdr HEX... ] [ excess ( CLASSID | clp ) ] [ clip ]\n");
43 }
44
45
46 static int atm_parse_class_opt(struct qdisc_util *qu, int argc, char **argv,
47 struct nlmsghdr *n, const char *dev)
48 {
49 struct sockaddr_atmsvc addr = {};
50 struct atm_qos qos;
51 struct atm_sap sap;
52 unsigned char hdr[MAX_HDR_LEN];
53 __u32 excess = 0;
54 struct rtattr *tail;
55 int sndbuf = 0;
56 int hdr_len = -1;
57 int set_clip = 0;
58 int s;
59
60 (void) text2qos("aal5,ubr:sdu=9180,rx:none", &qos, 0);
61 (void) text2sap("blli:l2=iso8802", &sap, 0);
62 while (argc > 0) {
63 if (!strcmp(*argv, "pvc")) {
64 NEXT_ARG();
65 if (text2atm(*argv, (struct sockaddr *) &addr,
66 sizeof(addr), T2A_PVC | T2A_NAME) < 0) {
67 explain();
68 return -1;
69 }
70 } else if (!strcmp(*argv,"svc")) {
71 NEXT_ARG();
72 if (text2atm(*argv, (struct sockaddr *) &addr,
73 sizeof(addr), T2A_SVC | T2A_NAME) < 0) {
74 explain();
75 return -1;
76 }
77 } else if (!strcmp(*argv,"qos")) {
78 NEXT_ARG();
79 if (text2qos(*argv, &qos, 0) < 0) {
80 explain();
81 return -1;
82 }
83 } else if (!strcmp(*argv,"sndbuf")) {
84 char *end;
85
86 NEXT_ARG();
87 sndbuf = strtol(*argv, &end, 0);
88 if (*end) {
89 explain();
90 return -1;
91 }
92 } else if (!strcmp(*argv,"sap")) {
93 NEXT_ARG();
94 if (addr.sas_family != AF_ATMSVC ||
95 text2sap(*argv, &sap, T2A_NAME) < 0) {
96 explain();
97 return -1;
98 }
99 } else if (!strcmp(*argv,"hdr")) {
100 unsigned char *ptr;
101 char *walk;
102
103 NEXT_ARG();
104 ptr = hdr;
105 for (walk = *argv; *walk; walk++) {
106 int tmp;
107
108 if (ptr == hdr+MAX_HDR_LEN) {
109 fprintf(stderr, "header is too long\n");
110 return -1;
111 }
112 if (*walk == '.') continue;
113 if (!isxdigit(walk[0]) || !walk[1] ||
114 !isxdigit(walk[1])) {
115 explain();
116 return -1;
117 }
118 sscanf(walk, "%2x", &tmp);
119 *ptr++ = tmp;
120 walk++;
121 }
122 hdr_len = ptr-hdr;
123 } else if (!strcmp(*argv,"excess")) {
124 NEXT_ARG();
125 if (!strcmp(*argv, "clp")) excess = 0;
126 else if (get_tc_classid(&excess, *argv)) {
127 explain();
128 return -1;
129 }
130 } else if (!strcmp(*argv,"clip")) {
131 set_clip = 1;
132 } else {
133 explain();
134 return 1;
135 }
136 argc--;
137 argv++;
138 }
139 s = socket(addr.sas_family, SOCK_DGRAM, 0);
140 if (s < 0) {
141 perror("socket");
142 return -1;
143 }
144 if (setsockopt(s, SOL_ATM, SO_ATMQOS, &qos, sizeof(qos)) < 0) {
145 perror("SO_ATMQOS");
146 return -1;
147 }
148 if (sndbuf)
149 if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, &sndbuf, sizeof(sndbuf)) < 0) {
150 perror("SO_SNDBUF");
151 return -1;
152 }
153 if (addr.sas_family == AF_ATMSVC && setsockopt(s, SOL_ATM, SO_ATMSAP,
154 &sap, sizeof(sap)) < 0) {
155 perror("SO_ATMSAP");
156 return -1;
157 }
158 if (connect(s, (struct sockaddr *) &addr, addr.sas_family == AF_ATMPVC ?
159 sizeof(struct sockaddr_atmpvc) : sizeof(addr)) < 0) {
160 perror("connect");
161 return -1;
162 }
163 if (set_clip)
164 if (ioctl(s, ATMARP_MKIP, 0) < 0) {
165 perror("ioctl ATMARP_MKIP");
166 return -1;
167 }
168 tail = NLMSG_TAIL(n);
169 addattr_l(n, 1024, TCA_OPTIONS, NULL, 0);
170 addattr_l(n, 1024, TCA_ATM_FD, &s, sizeof(s));
171 if (excess) addattr_l(n, 1024, TCA_ATM_EXCESS, &excess, sizeof(excess));
172 if (hdr_len != -1) addattr_l(n, 1024, TCA_ATM_HDR, hdr, hdr_len);
173 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
174 return 0;
175 }
176
177
178
179 static int atm_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
180 {
181 struct rtattr *tb[TCA_ATM_MAX+1];
182 char buffer[MAX_ATM_ADDR_LEN+1];
183
184 if (opt == NULL)
185 return 0;
186
187 parse_rtattr_nested(tb, TCA_ATM_MAX, opt);
188 if (tb[TCA_ATM_ADDR]) {
189 if (RTA_PAYLOAD(tb[TCA_ATM_ADDR]) <
190 sizeof(struct sockaddr_atmpvc))
191 fprintf(stderr, "ATM: address too short\n");
192 else {
193 if (atm2text(buffer, MAX_ATM_ADDR_LEN,
194 RTA_DATA(tb[TCA_ATM_ADDR]), A2T_PRETTY | A2T_NAME) <
195 0) fprintf(stderr, "atm2text error\n");
196 fprintf(f, "pvc %s ", buffer);
197 }
198 }
199 if (tb[TCA_ATM_HDR]) {
200 int i;
201 const __u8 *hdr = RTA_DATA(tb[TCA_ATM_HDR]);
202
203 fprintf(f, "hdr");
204 for (i = 0; i < RTA_PAYLOAD(tb[TCA_ATM_HDR]); i++)
205 fprintf(f, "%c%02x", i ? '.' : ' ', hdr[i]);
206 if (!i) fprintf(f, " .");
207 fprintf(f, " ");
208 }
209 if (tb[TCA_ATM_EXCESS]) {
210 __u32 excess;
211
212 if (RTA_PAYLOAD(tb[TCA_ATM_EXCESS]) < sizeof(excess))
213 fprintf(stderr, "ATM: excess class ID too short\n");
214 else {
215 excess = rta_getattr_u32(tb[TCA_ATM_EXCESS]);
216 if (!excess) fprintf(f, "excess clp ");
217 else {
218 char buf[64];
219
220 print_tc_classid(buf, sizeof(buf), excess);
221 fprintf(f, "excess %s ", buf);
222 }
223 }
224 }
225 if (tb[TCA_ATM_STATE]) {
226 static const char *map[] = { ATM_VS2TXT_MAP };
227 int state;
228
229 if (RTA_PAYLOAD(tb[TCA_ATM_STATE]) < sizeof(state))
230 fprintf(stderr, "ATM: state field too short\n");
231 else {
232 state = rta_getattr_u32(tb[TCA_ATM_STATE]);
233 fprintf(f, "%s ", map[state]);
234 }
235 }
236 return 0;
237 }
238
239
240 struct qdisc_util atm_qdisc_util = {
241 .id = "atm",
242 .parse_qopt = atm_parse_opt,
243 .print_qopt = atm_print_opt,
244 .parse_copt = atm_parse_class_opt,
245 .print_copt = atm_print_opt,
246 };