@@ -237,13 +237,47 @@ static void print_plain_qstats(struct netdev_qstats_get_list *qstats)
237237 }
238238}
239239
240- static int do_show (int argc , char * * argv )
240+ static struct netdev_qstats_get_list *
241+ qstats_dump (enum netdev_qstats_scope scope )
241242{
242243 struct netdev_qstats_get_list * qstats ;
243244 struct netdev_qstats_get_req * req ;
244245 struct ynl_error yerr ;
245246 struct ynl_sock * ys ;
246- int ret = 0 ;
247+
248+ ys = ynl_sock_create (& ynl_netdev_family , & yerr );
249+ if (!ys ) {
250+ p_err ("YNL: %s" , yerr .msg );
251+ return NULL ;
252+ }
253+
254+ req = netdev_qstats_get_req_alloc ();
255+ if (!req ) {
256+ p_err ("failed to allocate qstats request" );
257+ goto err_close ;
258+ }
259+
260+ if (scope )
261+ netdev_qstats_get_req_set_scope (req , scope );
262+
263+ qstats = netdev_qstats_get_dump (ys , req );
264+ netdev_qstats_get_req_free (req );
265+ if (!qstats ) {
266+ p_err ("failed to get queue stats: %s" , ys -> err .msg );
267+ goto err_close ;
268+ }
269+
270+ ynl_sock_destroy (ys );
271+ return qstats ;
272+
273+ err_close :
274+ ynl_sock_destroy (ys );
275+ return NULL ;
276+ }
277+
278+ static int do_show (int argc , char * * argv )
279+ {
280+ struct netdev_qstats_get_list * qstats ;
247281
248282 /* Parse options */
249283 while (argc > 0 ) {
@@ -268,29 +302,9 @@ static int do_show(int argc, char **argv)
268302 }
269303 }
270304
271- ys = ynl_sock_create (& ynl_netdev_family , & yerr );
272- if (!ys ) {
273- p_err ("YNL: %s" , yerr .msg );
305+ qstats = qstats_dump (scope );
306+ if (!qstats )
274307 return -1 ;
275- }
276-
277- req = netdev_qstats_get_req_alloc ();
278- if (!req ) {
279- p_err ("failed to allocate qstats request" );
280- ret = -1 ;
281- goto exit_close ;
282- }
283-
284- if (scope )
285- netdev_qstats_get_req_set_scope (req , scope );
286-
287- qstats = netdev_qstats_get_dump (ys , req );
288- netdev_qstats_get_req_free (req );
289- if (!qstats ) {
290- p_err ("failed to get queue stats: %s" , ys -> err .msg );
291- ret = -1 ;
292- goto exit_close ;
293- }
294308
295309 /* Print the stats as returned by the kernel */
296310 if (json_output )
@@ -299,9 +313,7 @@ static int do_show(int argc, char **argv)
299313 print_plain_qstats (qstats );
300314
301315 netdev_qstats_get_list_free (qstats );
302- exit_close :
303- ynl_sock_destroy (ys );
304- return ret ;
316+ return 0 ;
305317}
306318
307319static void compute_stats (__u64 * values , unsigned int count ,
@@ -406,10 +418,7 @@ static int cmp_ifindex_type(const void *a, const void *b)
406418static int do_balance (int argc , char * * argv __attribute__((unused )))
407419{
408420 struct netdev_qstats_get_list * qstats ;
409- struct netdev_qstats_get_req * req ;
410421 struct netdev_qstats_get_rsp * * sorted ;
411- struct ynl_error yerr ;
412- struct ynl_sock * ys ;
413422 unsigned int count = 0 ;
414423 unsigned int i , j ;
415424 int ret = 0 ;
@@ -419,29 +428,9 @@ static int do_balance(int argc, char **argv __attribute__((unused)))
419428 return -1 ;
420429 }
421430
422- ys = ynl_sock_create (& ynl_netdev_family , & yerr );
423- if (!ys ) {
424- p_err ("YNL: %s" , yerr .msg );
431+ qstats = qstats_dump (NETDEV_QSTATS_SCOPE_QUEUE );
432+ if (!qstats )
425433 return -1 ;
426- }
427-
428- req = netdev_qstats_get_req_alloc ();
429- if (!req ) {
430- p_err ("failed to allocate qstats request" );
431- ret = -1 ;
432- goto exit_close ;
433- }
434-
435- /* Always use queue scope for balance analysis */
436- netdev_qstats_get_req_set_scope (req , NETDEV_QSTATS_SCOPE_QUEUE );
437-
438- qstats = netdev_qstats_get_dump (ys , req );
439- netdev_qstats_get_req_free (req );
440- if (!qstats ) {
441- p_err ("failed to get queue stats: %s" , ys -> err .msg );
442- ret = -1 ;
443- goto exit_close ;
444- }
445434
446435 /* Count and sort queues */
447436 ynl_dump_foreach (qstats , qs )
@@ -576,8 +565,6 @@ static int do_balance(int argc, char **argv __attribute__((unused)))
576565 free (sorted );
577566exit_free_qstats :
578567 netdev_qstats_get_list_free (qstats );
579- exit_close :
580- ynl_sock_destroy (ys );
581568 return ret ;
582569}
583570
0 commit comments