Jussi Kivilinna | 839c845 | 2008-07-25 16:19:09 +0300 | [diff] [blame] | 1 | /* |
| 2 | * tc_stab.c "tc qdisc ... stab *". |
| 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: Jussi Kivilinna, <jussi.kivilinna@mbnet.fi> |
| 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 <math.h> |
| 19 | #include <sys/socket.h> |
Natanael Copa | dd9cc0e | 2014-05-27 07:40:10 +0000 | [diff] [blame] | 20 | #include <sys/param.h> |
Jussi Kivilinna | 839c845 | 2008-07-25 16:19:09 +0300 | [diff] [blame] | 21 | #include <netinet/in.h> |
| 22 | #include <arpa/inet.h> |
| 23 | #include <string.h> |
| 24 | #include <malloc.h> |
| 25 | |
| 26 | #include "utils.h" |
| 27 | #include "tc_util.h" |
| 28 | #include "tc_core.h" |
| 29 | #include "tc_common.h" |
| 30 | |
| 31 | static void stab_help(void) |
| 32 | { |
| 33 | fprintf(stderr, |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 34 | "Usage: ... stab [ mtu BYTES ] [ tsize SLOTS ] [ mpu BYTES ]\n" |
Jussi Kivilinna | 839c845 | 2008-07-25 16:19:09 +0300 | [diff] [blame] | 35 | " [ overhead BYTES ] [ linklayer TYPE ] ...\n" |
| 36 | " mtu : max packet size we create rate map for {2047}\n" |
| 37 | " tsize : how many slots should size table have {512}\n" |
| 38 | " mpu : minimum packet size used in rate computations\n" |
| 39 | " overhead : per-packet size overhead used in rate computations\n" |
| 40 | " linklayer : adapting to a linklayer e.g. atm\n" |
| 41 | "Example: ... stab overhead 20 linklayer atm\n"); |
| 42 | |
Jussi Kivilinna | 839c845 | 2008-07-25 16:19:09 +0300 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | int check_size_table_opts(struct tc_sizespec *s) |
| 46 | { |
| 47 | return s->linklayer >= LINKLAYER_ETHERNET || s->mpu != 0 || |
| 48 | s->overhead != 0; |
| 49 | } |
| 50 | |
| 51 | int parse_size_table(int *argcp, char ***argvp, struct tc_sizespec *sp) |
| 52 | { |
| 53 | char **argv = *argvp; |
| 54 | int argc = *argcp; |
Phil Sutter | d17b136 | 2016-07-18 16:48:42 +0200 | [diff] [blame] | 55 | struct tc_sizespec s = {}; |
Jussi Kivilinna | 839c845 | 2008-07-25 16:19:09 +0300 | [diff] [blame] | 56 | |
| 57 | NEXT_ARG(); |
| 58 | if (matches(*argv, "help") == 0) { |
| 59 | stab_help(); |
| 60 | return -1; |
| 61 | } |
| 62 | while (argc > 0) { |
| 63 | if (matches(*argv, "mtu") == 0) { |
| 64 | NEXT_ARG(); |
| 65 | if (s.mtu) |
| 66 | duparg("mtu", *argv); |
Stephen Hemminger | bcb4a7a | 2015-08-13 14:20:40 -0700 | [diff] [blame] | 67 | if (get_u32(&s.mtu, *argv, 10)) |
Jussi Kivilinna | 839c845 | 2008-07-25 16:19:09 +0300 | [diff] [blame] | 68 | invarg("mtu", "invalid mtu"); |
Jussi Kivilinna | 839c845 | 2008-07-25 16:19:09 +0300 | [diff] [blame] | 69 | } else if (matches(*argv, "mpu") == 0) { |
| 70 | NEXT_ARG(); |
| 71 | if (s.mpu) |
| 72 | duparg("mpu", *argv); |
Stephen Hemminger | bcb4a7a | 2015-08-13 14:20:40 -0700 | [diff] [blame] | 73 | if (get_u32(&s.mpu, *argv, 10)) |
Jussi Kivilinna | 839c845 | 2008-07-25 16:19:09 +0300 | [diff] [blame] | 74 | invarg("mpu", "invalid mpu"); |
Jussi Kivilinna | 839c845 | 2008-07-25 16:19:09 +0300 | [diff] [blame] | 75 | } else if (matches(*argv, "overhead") == 0) { |
| 76 | NEXT_ARG(); |
| 77 | if (s.overhead) |
| 78 | duparg("overhead", *argv); |
Stephen Hemminger | bcb4a7a | 2015-08-13 14:20:40 -0700 | [diff] [blame] | 79 | if (get_integer(&s.overhead, *argv, 10)) |
Jussi Kivilinna | 839c845 | 2008-07-25 16:19:09 +0300 | [diff] [blame] | 80 | invarg("overhead", "invalid overhead"); |
Jussi Kivilinna | 839c845 | 2008-07-25 16:19:09 +0300 | [diff] [blame] | 81 | } else if (matches(*argv, "tsize") == 0) { |
| 82 | NEXT_ARG(); |
| 83 | if (s.tsize) |
| 84 | duparg("tsize", *argv); |
Stephen Hemminger | bcb4a7a | 2015-08-13 14:20:40 -0700 | [diff] [blame] | 85 | if (get_u32(&s.tsize, *argv, 10)) |
Jussi Kivilinna | 839c845 | 2008-07-25 16:19:09 +0300 | [diff] [blame] | 86 | invarg("tsize", "invalid table size"); |
Jussi Kivilinna | 839c845 | 2008-07-25 16:19:09 +0300 | [diff] [blame] | 87 | } else if (matches(*argv, "linklayer") == 0) { |
| 88 | NEXT_ARG(); |
| 89 | if (s.linklayer != LINKLAYER_UNSPEC) |
| 90 | duparg("linklayer", *argv); |
Stephen Hemminger | bcb4a7a | 2015-08-13 14:20:40 -0700 | [diff] [blame] | 91 | if (get_linklayer(&s.linklayer, *argv)) |
Jussi Kivilinna | 839c845 | 2008-07-25 16:19:09 +0300 | [diff] [blame] | 92 | invarg("linklayer", "invalid linklayer"); |
Jussi Kivilinna | 839c845 | 2008-07-25 16:19:09 +0300 | [diff] [blame] | 93 | } else |
| 94 | break; |
| 95 | argc--; argv++; |
| 96 | } |
| 97 | |
| 98 | if (!check_size_table_opts(&s)) |
| 99 | return -1; |
| 100 | |
| 101 | *sp = s; |
| 102 | *argvp = argv; |
| 103 | *argcp = argc; |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | void print_size_table(FILE *fp, const char *prefix, struct rtattr *rta) |
| 108 | { |
| 109 | struct rtattr *tb[TCA_STAB_MAX + 1]; |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 110 | |
Jussi Kivilinna | 839c845 | 2008-07-25 16:19:09 +0300 | [diff] [blame] | 111 | SPRINT_BUF(b1); |
| 112 | |
| 113 | parse_rtattr_nested(tb, TCA_STAB_MAX, rta); |
| 114 | |
| 115 | if (tb[TCA_STAB_BASE]) { |
| 116 | struct tc_sizespec s = {0}; |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 117 | |
Jussi Kivilinna | 839c845 | 2008-07-25 16:19:09 +0300 | [diff] [blame] | 118 | memcpy(&s, RTA_DATA(tb[TCA_STAB_BASE]), |
| 119 | MIN(RTA_PAYLOAD(tb[TCA_STAB_BASE]), sizeof(s))); |
| 120 | |
| 121 | fprintf(fp, "%s", prefix); |
| 122 | if (s.linklayer) |
| 123 | fprintf(fp, "linklayer %s ", |
| 124 | sprint_linklayer(s.linklayer, b1)); |
| 125 | if (s.overhead) |
| 126 | fprintf(fp, "overhead %d ", s.overhead); |
| 127 | if (s.mpu) |
| 128 | fprintf(fp, "mpu %u ", s.mpu); |
| 129 | if (s.mtu) |
| 130 | fprintf(fp, "mtu %u ", s.mtu); |
| 131 | if (s.tsize) |
| 132 | fprintf(fp, "tsize %u ", s.tsize); |
| 133 | } |
| 134 | |
| 135 | #if 0 |
| 136 | if (tb[TCA_STAB_DATA]) { |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 137 | unsigned int i, j, dlen; |
Jussi Kivilinna | 839c845 | 2008-07-25 16:19:09 +0300 | [diff] [blame] | 138 | __u16 *data = RTA_DATA(tb[TCA_STAB_DATA]); |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 139 | |
Jussi Kivilinna | 839c845 | 2008-07-25 16:19:09 +0300 | [diff] [blame] | 140 | dlen = RTA_PAYLOAD(tb[TCA_STAB_DATA]) / sizeof(__u16); |
| 141 | |
| 142 | fprintf(fp, "\n%sstab data:", prefix); |
| 143 | for (i = 0; i < dlen/12; i++) { |
| 144 | fprintf(fp, "\n%s %3u:", prefix, i * 12); |
| 145 | for (j = 0; i * 12 + j < dlen; j++) |
| 146 | fprintf(fp, " %05x", data[i * 12 + j]); |
| 147 | } |
| 148 | } |
| 149 | #endif |
| 150 | } |