@@ -38,7 +38,9 @@ static void usage(const char *prog) {
38
38
" -p eBPF program path\n"
39
39
" -i iface\n"
40
40
" -m monitor\n"
41
- " -s lua script\n" ,
41
+ " -s lua script\n"
42
+ " -I Interval\n"
43
+ " -D Duration\n" ,
42
44
prog );
43
45
}
44
46
@@ -103,13 +105,19 @@ static int do_detach(int idx, const char *name)
103
105
return err ;
104
106
}
105
107
106
- static void poll (int map_fd , int interval ) {
107
- long cnt ;
108
+ static void poll (int map_fd , int interval , int duration ) {
109
+ unsigned int nr_cpus = bpf_num_possible_cpus ();
110
+ long cnts [nr_cpus ];
108
111
unsigned int key = 0 ;
109
112
110
- while (1 ) {
111
- bpf_map_lookup_elem (map_fd , & key , & cnt );
112
- printf ("pkt count: %lu\n" , cnt );
113
+ for (int i = 0 ; i < duration ; i ++ ) {
114
+ unsigned long cnt = 0 ;
115
+ int i ;
116
+ bpf_map_lookup_elem (map_fd , & key , cnts );
117
+ for (i = 0 ; i < nr_cpus ; ++ i ) {
118
+ cnt += cnts [i ];
119
+ }
120
+ printf ("%lu\n" , cnt );
113
121
sleep (interval );
114
122
}
115
123
}
@@ -123,9 +131,11 @@ int main(int argc, char *argv[])
123
131
struct bpf_object * obj ;
124
132
int opt , prog_fd ;
125
133
int rx_cnt_map_fd ;
126
- int detach = 0 , attach_lua = 0 , attach_ebpf = 0 , monitor = 0 , attach_script = 0 ;
134
+ int detach = 0 , attach_lua = 0 , attach_ebpf = 0 , monitor = 0 , attach_script = 0 ,
135
+ interval = 1 , duration = 1 ;
136
+
127
137
char * lua_prog = NULL ;
128
- const char * optstr = "f:p:i:dms:" ;
138
+ const char * optstr = "f:p:i:dms:I:D: " ;
129
139
struct bpf_prog_load_attr prog_load_attr = {
130
140
.prog_type = BPF_PROG_TYPE_XDP ,
131
141
};
@@ -158,12 +168,19 @@ int main(int argc, char *argv[])
158
168
"%s" , optarg );
159
169
attach_script = 1 ;
160
170
break ;
171
+ case 'I' :
172
+ interval = atoi (optarg );
173
+ break ;
174
+ case 'D' :
175
+ duration = atoi (optarg );
176
+ break ;
161
177
default :
162
178
usage (basename (argv [0 ]));
163
179
return 1 ;
164
180
}
165
181
}
166
182
183
+
167
184
if (attach_ebpf || detach ) {
168
185
if (!ifindex ) {
169
186
printf ("ERROR: invalid interface name" );
@@ -219,7 +236,7 @@ int main(int argc, char *argv[])
219
236
if (monitor ) {
220
237
rx_cnt_map_fd = bpf_object__find_map_fd_by_name (obj , "rx_cnt" );
221
238
222
- poll (rx_cnt_map_fd , 1 );
239
+ poll (rx_cnt_map_fd , interval , duration );
223
240
}
224
241
return 0 ;
225
242
}
0 commit comments