| 1 |
/* tc_iterate: Reliable, fast monitoring of tc |
| 2 |
* Author: Dave Taht |
| 3 |
* Date: 22 Nov 2015 |
| 4 |
* Copyright (C) 2015 Michael David Taht |
| 5 |
* Copyright (C) 2015 Toke Høiland-Jørgensen |
| 6 |
|
| 7 |
* This program is free software: you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU General Public License as published by |
| 9 |
* the Free Software Foundation, either version 3 of the License, or |
| 10 |
* (at your option) any later version. |
| 11 |
* |
| 12 |
* This program is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License |
| 18 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
*/ |
| 20 |
|
| 21 |
#include <stdio.h> |
| 22 |
#include <unistd.h> |
| 23 |
#include <string.h> |
| 24 |
#include <stdlib.h> |
| 25 |
#include <stdbool.h> |
| 26 |
#include <errno.h> |
| 27 |
#include <locale.h> |
| 28 |
#include <assert.h> |
| 29 |
#include <getopt.h> |
| 30 |
#include <iconv.h> |
| 31 |
#include <fcntl.h> |
| 32 |
#include <math.h> |
| 33 |
#include <sys/types.h> |
| 34 |
#include <sys/wait.h> |
| 35 |
#include <sys/timerfd.h> |
| 36 |
|
| 37 |
/* |
| 38 |
openwrt's ash shell does not have usleep, |
| 39 |
nor nanosecond time in the date command. |
| 40 |
|
| 41 |
for i in $(seq $count); do |
| 42 |
tc -s $command show dev $interface |
| 43 |
date '+Time: %s.%N' |
| 44 |
echo "---" |
| 45 |
sleep $interval |
| 46 |
done |
| 47 |
|
| 48 |
So this C program (which is more accurate and lighter weight |
| 49 |
than the shell script) needs to be used on things like openwrt, |
| 50 |
or when high resolution tc statistics are desired. |
| 51 |
*/ |
| 52 |
|
| 53 |
#define BUFFERSIZE (1024*1024) |
| 54 |
#define NSEC_PER_SEC (1000000000.0) |
| 55 |
|
| 56 |
struct arg { |
| 57 |
int count; |
| 58 |
struct timespec interval; |
| 59 |
double finterval; |
| 60 |
char *interface; |
| 61 |
char *command; |
| 62 |
char buffer; |
| 63 |
}; |
| 64 |
|
| 65 |
typedef struct arg args; |
| 66 |
|
| 67 |
static const struct option long_options[] = { |
| 68 |
{ "interface", required_argument , NULL , 'i' } , |
| 69 |
{ "count" , required_argument , NULL , 'c' } , |
| 70 |
{ "interval" , required_argument , NULL , 'I' } , |
| 71 |
{ "command" , required_argument , NULL , 'C' } , |
| 72 |
{ "help" , no_argument , NULL , 'h' } , |
| 73 |
{ "buffer" , no_argument , NULL , 'b' } , |
| 74 |
}; |
| 75 |
|
| 76 |
void usage (char *err) { |
| 77 |
if(err) fprintf(stderr,"%s\n",err); |
| 78 |
printf("tc_iterate [options]\n"); |
| 79 |
printf( |
| 80 |
"\t-h --help \n" |
| 81 |
"\t-b --buffer \n" |
| 82 |
"\t-i --interface [eth0*,wlan0,etc]\n" |
| 83 |
"\t-c --count [number of iterations]\n" |
| 84 |
"\t-I --interval [fractional number of seconds]\n" |
| 85 |
"\t-C --command [qdisc]\n"); |
| 86 |
exit(-1); |
| 87 |
} |
| 88 |
|
| 89 |
static void defaults(args *a) { |
| 90 |
a->interface = "eth0"; |
| 91 |
a->command = "qdisc"; |
| 92 |
a->finterval=.2; |
| 93 |
a->count=10; |
| 94 |
a->interval.tv_nsec = 0; |
| 95 |
a->interval.tv_sec = 0; |
| 96 |
a->buffer = 0; |
| 97 |
} |
| 98 |
|
| 99 |
#define QSTRING "i:c:I:C:hb" |
| 100 |
|
| 101 |
int process_options(int argc, char **argv, args *o) |
| 102 |
{ |
| 103 |
int option_index = 0; |
| 104 |
int opt = 0; |
| 105 |
optind = 1; |
| 106 |
|
| 107 |
while(1) |
| 108 |
{ |
| 109 |
opt = getopt_long(argc, argv, |
| 110 |
QSTRING, |
| 111 |
long_options, &option_index); |
| 112 |
if(opt == -1) break; |
| 113 |
|
| 114 |
switch (opt) |
| 115 |
{ |
| 116 |
case 'c': o->count = strtoul(optarg,NULL,10); break; |
| 117 |
case 'I': o->finterval = strtod(optarg,NULL); break; |
| 118 |
case 'C': o->command = optarg; break; |
| 119 |
case 'i': o->interface = optarg; break; |
| 120 |
case 'b': o->buffer = 1; break; |
| 121 |
case '?': |
| 122 |
case 'h': usage(NULL); break; |
| 123 |
default: usage(NULL); |
| 124 |
} |
| 125 |
} |
| 126 |
o->interval.tv_sec = floor(o->finterval); |
| 127 |
o->interval.tv_nsec = (long long) ((o->finterval - o->interval.tv_sec) * NSEC_PER_SEC); |
| 128 |
return 0; |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
static void result(int out, int size, int bufsize, char *buffer) { |
| 133 |
struct timespec cur_time; |
| 134 |
if(bufsize - size > 40) { |
| 135 |
clock_gettime(CLOCK_REALTIME, &cur_time); |
| 136 |
int added = sprintf(&buffer[size],"Time: %ld.%09ld\n---\n", |
| 137 |
cur_time.tv_sec,cur_time.tv_nsec); |
| 138 |
write(out,buffer,size+added); |
| 139 |
} else { |
| 140 |
write(2,"Buffer Overrun\n",sizeof("Buffer Overrun\n")); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
// Since this is linux only we can use timerfd for an isochronous clock |
| 145 |
|
| 146 |
int forkit(args *a) |
| 147 |
{ |
| 148 |
int filedes[2]; // 0 = read |
| 149 |
int filedes2[2]; // 0 = read |
| 150 |
pipe(filedes); |
| 151 |
pipe(filedes2); |
| 152 |
int tool = filedes[1]; // write this |
| 153 |
int in = filedes2[0]; // connect out to in |
| 154 |
char tmpfile[] = "/tmp/tc_iterateXXXXXX"; |
| 155 |
int out = a->buffer ? mkstemp(tmpfile) : STDOUT_FILENO; |
| 156 |
pid_t child; |
| 157 |
|
| 158 |
if(a->buffer && !out) { |
| 159 |
perror("Unable to create tmpfile"); |
| 160 |
exit(-1); |
| 161 |
} |
| 162 |
// probably want the pipe line buffered via fcntl |
| 163 |
if((child = fork())==0) |
| 164 |
{ |
| 165 |
close(filedes[1]); |
| 166 |
close(filedes2[0]); |
| 167 |
dup2(filedes2[1],STDOUT_FILENO); |
| 168 |
dup2(filedes[0],STDIN_FILENO); |
| 169 |
|
| 170 |
if(execlp("tc", "tc", "-s", "-b", "-",NULL)==-1) { |
| 171 |
perror("Failed to execute cmd"); |
| 172 |
exit(-1); |
| 173 |
} |
| 174 |
} |
| 175 |
close(filedes2[1]); |
| 176 |
close(filedes[0]); |
| 177 |
struct itimerspec new_value = {0}; |
| 178 |
|
| 179 |
int timer = timerfd_create(CLOCK_REALTIME, 0); |
| 180 |
|
| 181 |
|
| 182 |
new_value.it_interval = a->interval; |
| 183 |
new_value.it_value = a->interval; |
| 184 |
|
| 185 |
/* better method would be to poll for input (since writes from the |
| 186 |
tool could block or return no output for some reason), timestamp the input, |
| 187 |
and if the difference is less than half, skip this round. |
| 188 |
this would absorb non-completing stuff */ |
| 189 |
|
| 190 |
char buffer[BUFFERSIZE]; |
| 191 |
char cmd[1024]; |
| 192 |
int size = 0; |
| 193 |
sprintf(cmd,"%s show dev %s\n",a->command,a->interface); |
| 194 |
int csize = strlen(cmd); |
| 195 |
int ctr = 0; |
| 196 |
timerfd_settime(timer,0,&new_value,NULL); // relative timer |
| 197 |
|
| 198 |
do { |
| 199 |
long long fired; |
| 200 |
if(write(tool,cmd,csize)==-1) perror("writing cmd"); |
| 201 |
if(read(timer,&fired,sizeof(fired))!=8) perror("reading timer"); |
| 202 |
ctr+=fired; |
| 203 |
if((size = read(in,buffer,sizeof(buffer))) > 0) { |
| 204 |
result(out,size,BUFFERSIZE,buffer); |
| 205 |
} else { |
| 206 |
result(out,0,BUFFERSIZE,buffer); |
| 207 |
perror("reading cmd output"); |
| 208 |
} |
| 209 |
} while (ctr < a->count); |
| 210 |
close(tool); |
| 211 |
close(in); |
| 212 |
if(a->buffer) { |
| 213 |
lseek(out, 0, SEEK_SET); |
| 214 |
while((size = read(out, buffer, sizeof(buffer))) > 0) |
| 215 |
write(STDOUT_FILENO,buffer,size); |
| 216 |
unlink(tmpfile); |
| 217 |
} |
| 218 |
close(out); |
| 219 |
wait(NULL); |
| 220 |
return 0; |
| 221 |
} |
| 222 |
|
| 223 |
int main(int argc,char **argv) { |
| 224 |
args a; |
| 225 |
int status = 0; |
| 226 |
defaults(&a); |
| 227 |
process_options(argc, argv, &a); |
| 228 |
forkit(&a); |
| 229 |
return status; |
| 230 |
} |