Tagging module iproute2 - iproute2-2.6.16-2
[iproute2.git] / tc / q_sfq.c
1 /*
2  * q_sfq.c              SFQ.
3  *
4  *              This program is free software; you can redistribute it and/or
5  *              modify it under the terms of the GNU General Public License
6  *              as published by the Free Software Foundation; either version
7  *              2 of the License, or (at your option) any later version.
8  *
9  * Authors:     Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10  *
11  */
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include <syslog.h>
17 #include <fcntl.h>
18 #include <sys/socket.h>
19 #include <netinet/in.h>
20 #include <arpa/inet.h>
21 #include <string.h>
22
23 #include "utils.h"
24 #include "tc_util.h"
25
26 static void explain(void)
27 {
28         fprintf(stderr, "Usage: ... sfq [ limit NUMBER ] [ perturb SECS ] [ quantum BYTES ]\n");
29 }
30
31 #define usage() return(-1)
32
33 static int sfq_parse_opt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n)
34 {
35         int ok=0;
36         struct tc_sfq_qopt opt;
37
38         memset(&opt, 0, sizeof(opt));
39
40         while (argc > 0) {
41                 if (strcmp(*argv, "quantum") == 0) {
42                         NEXT_ARG();
43                         if (get_size(&opt.quantum, *argv)) {
44                                 fprintf(stderr, "Illegal \"limit\"\n");
45                                 return -1;
46                         }
47                         ok++;
48                 } else if (strcmp(*argv, "perturb") == 0) {
49                         NEXT_ARG();
50                         if (get_integer(&opt.perturb_period, *argv, 0)) {
51                                 fprintf(stderr, "Illegal \"perturb\"\n");
52                                 return -1;
53                         }
54                         ok++;
55                 } else if (strcmp(*argv, "limit") == 0) {
56                         NEXT_ARG();
57                         if (get_u32(&opt.limit, *argv, 0)) {
58                                 fprintf(stderr, "Illegal \"limit\"\n");
59                                 return -1;
60                         }
61                         if (opt.limit < 2) {
62                                 fprintf(stderr, "Illegal \"limit\", must be > 1\n");
63                                 return -1;
64                         }
65                         ok++;
66                 } else if (strcmp(*argv, "help") == 0) {
67                         explain();
68                         return -1;
69                 } else {
70                         fprintf(stderr, "What is \"%s\"?\n", *argv);
71                         explain();
72                         return -1;
73                 }
74                 argc--; argv++;
75         }
76
77         if (ok)
78                 addattr_l(n, 1024, TCA_OPTIONS, &opt, sizeof(opt));
79         return 0;
80 }
81
82 static int sfq_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
83 {
84         struct tc_sfq_qopt *qopt;
85         SPRINT_BUF(b1);
86
87         if (opt == NULL)
88                 return 0;
89
90         if (RTA_PAYLOAD(opt)  < sizeof(*qopt))
91                 return -1;
92         qopt = RTA_DATA(opt);
93         fprintf(f, "limit %up ", qopt->limit);
94         fprintf(f, "quantum %s ", sprint_size(qopt->quantum, b1));
95         if (show_details) {
96                 fprintf(f, "flows %u/%u ", qopt->flows, qopt->divisor);
97         }
98         if (qopt->perturb_period)
99                 fprintf(f, "perturb %dsec ", qopt->perturb_period);
100         return 0;
101 }
102
103 struct qdisc_util sfq_qdisc_util = {
104         .id             = "sfq",
105         .parse_qopt     = sfq_parse_opt,
106         .print_qopt     = sfq_print_opt,
107 };