utils: add fflush_monitor() helper
Subsystems (3)
PATCH v2 1/1 utils: add fflush_monitor() helper
Some fflush() calls only make sense for monitor programs.
For other cases, forcing a flush is expensive.
After this patch, ip, tc and ss are correctly buffering most of their output when redirected to a file.
--- include/utils.h | 7 +++++++ ip/ipaddress.c | 12 +++++++----- ip/iplink.c | 4 ++-- ip/ipmonitor.c | 1 + ip/ipmptcp.c | 10 +++++----- ip/ipneigh.c | 4 ++-- ip/ipnetconf.c | 2 +- ip/ipnetns.c | 2 +- ip/ipnexthop.c | 12 ++++++------ ip/iproute.c | 4 ++-- ip/iprule.c | 2 +- ip/iptoken.c | 2 +- ip/tcp_metrics.c | 2 +- lib/utils.c | 1 + misc/ss.c | 3 ++- tc/tc_class.c | 2 +- tc/tc_filter.c | 2 +- tc/tc_monitor.c | 1 + tc/tc_qdisc.c | 2 +- 19 files changed, 44 insertions(+), 31 deletions(-)
include/utils.h +7 -0
@@ -38,6 +38,7 @@ extern int numeric;extern bool do_all;extern int echo_request;extern int use_iec; +extern int monitor_mode;#define SPRINT_BSIZE 64#define SPRINT_BUF (x) char x[SPRINT_BSIZE]
@@ -399,4 +400,10 @@ FILE *generic_proc_open(const char *env, const char *name);int open_fds_add (int fd);void open_fds_close (void ); +static inline void fflush_monitor (FILE *fp) +{ +if (monitor_mode) +fflush (fp); +} +#endif /* __UTILS_H__ */
ip/ipaddress.c +7 -5
@@ -894,7 +894,7 @@ static int print_linkinfo_brief(FILE *fp, const char *name,print_string (PRINT_FP ,NULL ,"%s" ,"\n" ); } -fflush (fp); +fflush_monitor (fp);return 0 ; }
@@ -1346,10 +1346,12 @@ int print_linkinfo(struct nlmsghdr *n, void *arg) }print_string (PRINT_FP ,NULL ,"%s" ,"\n" ); -fflush (fp); +fflush_monitor (fp);/* prettier here if stderr and stdout go to the same place */ -if (truncated_vfs) +if (truncated_vfs) { +fflush (fp);fprintf (stderr,"Truncated VF list: %s\n" , name); + }return 1 ; }
@@ -1732,7 +1734,7 @@ int print_addrinfo(struct nlmsghdr *n, void *arg) }print_string (PRINT_FP ,NULL ,"%s" ,"\n" ); brief_exit: -fflush (fp); +fflush_monitor (fp);return 0 ; }
@@ -1768,7 +1770,7 @@ static int print_selected_addrinfo(struct ifinfomsg *ifi,if (brief) {print_string (PRINT_FP ,NULL ,"%s" ,"\n" ); -fflush (fp); +fflush_monitor (fp); }return 0 ; }
ip/iplink.c +2 -2
@@ -1296,7 +1296,7 @@ static void print_mpls_stats(FILE *fp, struct rtattr *attr)print_nl ();print_mpls_link_stats (fp, stats," " );print_string (PRINT_FP ,NULL ,"%s" ,"\n" ); -fflush (fp); +fflush_monitor (fp); }static void print_af_stats_attr (FILE *fp,int ifindex,struct rtattr *attr)
@@ -1359,7 +1359,7 @@ static int print_af_stats(struct nlmsghdr *n, void *arg)if (tb[IFLA_STATS_AF_SPEC ])print_af_stats_attr (fp, ifsm->ifindex, tb[IFLA_STATS_AF_SPEC ]); -fflush (fp); +fflush_monitor (fp);return 0 ; }
ip/ipmonitor.c +1 -0
@@ -324,6 +324,7 @@ int do_ipmonitor(int argc, char **argv)return err; } + monitor_mode =1 ;if (rtnl_open (&rth, groups) <0 )exit (1 );
ip/ipmptcp.c +5 -5
@@ -276,7 +276,7 @@ static int print_mptcp_addrinfo(struct rtattr *addrinfo)print_nl ();close_json_object (); -fflush (stdout); +fflush_monitor (stdout);return 0 ; }
@@ -325,7 +325,7 @@ static int mptcp_addr_dump(void) }delete_json_obj (); -fflush (stdout); +fflush_monitor (stdout);return 0 ; }
@@ -349,7 +349,7 @@ static int mptcp_addr_show(int argc, char **argv) ret =print_mptcp_addr (answer, stdout);delete_json_obj ();free (answer); -fflush (stdout); +fflush_monitor (stdout);return ret; }
@@ -430,7 +430,7 @@ static int print_mptcp_limit(struct nlmsghdr *n, void *arg)print_nl ();close_json_object (); -fflush (stdout); +fflush_monitor (stdout);return 0 ; }
@@ -592,7 +592,7 @@ static int mptcp_monitor_msg(struct rtnl_ctrl_data *ctrl, out:print_nl ();close_json_object (); -fflush (stdout); +fflush_monitor (stdout);return 0 ; }
ip/ipneigh.c +2 -2
@@ -298,7 +298,7 @@ static int print_neigh_brief(FILE *fp, struct ndmsg *r, struct rtattr *tb[])print_string (PRINT_FP ,NULL ,"%s" ,"\n" );close_json_object (); -fflush (fp); +fflush_monitor (fp);return 0 ; }
@@ -472,7 +472,7 @@ int print_neigh(struct nlmsghdr *n, void *arg)print_string (PRINT_FP ,NULL ,"\n" ,"" );close_json_object (); -fflush (fp); +fflush_monitor (fp);return 0 ; }
ip/ipnetconf.c +1 -1
@@ -140,7 +140,7 @@ int print_netconf(struct rtnl_ctrl_data *ctrl, struct nlmsghdr *n, void *arg)close_json_object ();print_string (PRINT_FP ,NULL ,"\n" ,NULL ); -fflush (fp); +fflush_monitor (fp);return 0 ; }
ip/ipnetns.c +1 -1
@@ -327,7 +327,7 @@ int print_nsid(struct nlmsghdr *n, void *arg)print_string (PRINT_FP ,NULL ,"\n" ,NULL );close_json_object (); -fflush (fp); +fflush_monitor (fp);return 0 ; }
ip/ipnexthop.c +6 -6
@@ -826,7 +826,7 @@ int print_cache_nexthop(struct nlmsghdr *n, void *arg, bool process_cache)__print_nexthop_entry (fp,NULL , &nhe, n->nlmsg_type ==RTM_DELNEXTHOP );print_string (PRINT_FP ,NULL ,"%s" ,"\n" ); -fflush (fp); +fflush_monitor (fp);if (process_cache)ipnh_cache_process_nlmsg (n, &nhe);
@@ -882,7 +882,7 @@ int print_nexthop_bucket(struct nlmsghdr *n, void *arg)print_string (PRINT_FP ,NULL ,"%s" ,"\n" );close_json_object (); -fflush (fp); +fflush_monitor (fp);return 0 ; }
@@ -1178,7 +1178,7 @@ static int ipnh_get_id(__u32 id) }delete_json_obj (); -fflush (stdout); +fflush_monitor (stdout);free (answer);
@@ -1266,7 +1266,7 @@ static int ipnh_list_flush(int argc, char **argv, int action) }delete_json_obj (); -fflush (stdout); +fflush_monitor (stdout);return 0 ; }
@@ -1342,7 +1342,7 @@ static int ipnh_bucket_list(int argc, char **argv) }delete_json_obj (); -fflush (stdout); +fflush_monitor (stdout);return 0 ; }
@@ -1383,7 +1383,7 @@ static int ipnh_bucket_get_id(__u32 id, __u16 bucket_index) }delete_json_obj (); -fflush (stdout); +fflush_monitor (stdout);free (answer);
ip/iproute.c +2 -2
@@ -1012,7 +1012,7 @@ int print_route(struct nlmsghdr *n, void *arg)print_string (PRINT_FP ,NULL ,"\n" ,NULL );close_json_object (); -fflush (fp); +fflush_monitor (fp);return 0 ; }
@@ -2035,7 +2035,7 @@ static int iproute_list_flush_or_save(int argc, char **argv, int action) }delete_json_obj (); -fflush (stdout); +fflush_monitor (stdout);return 0 ; }
ip/iprule.c +1 -1
@@ -599,7 +599,7 @@ int print_rule(struct nlmsghdr *n, void *arg)print_string (PRINT_FP ,NULL ,"\n" ,"" );close_json_object (); -fflush (fp); +fflush_monitor (fp);return 0 ; }
ip/iptoken.c +1 -1
@@ -85,7 +85,7 @@ static int print_token(struct nlmsghdr *n, void *arg)"ifname" ,"%s\n" , ll_index_to_name(ifi->ifi_index));close_json_object (); -fflush (fp); +fflush_monitor (fp);return 0 ; }
ip/tcp_metrics.c +1 -1
@@ -334,7 +334,7 @@ static int process_msg(struct nlmsghdr *n, void *arg)print_string (PRINT_FP ,NULL ,"\n" ,"" );close_json_object (); -fflush (fp); +fflush_monitor (fp);return 0 ; }
lib/utils.c +1 -0
@@ -41,6 +41,7 @@ int timestamp_short;int pretty;int use_iec;int human_readable; +int monitor_mode;const char *_SL_ ="\n" ;static int open_fds[5 ];
misc/ss.c +2 -1
@@ -5544,7 +5544,7 @@ static int generic_show_sock(struct nlmsghdr *nlh, void *arg)render (); -fflush (stdout); +fflush_monitor (stdout);return ret; }
@@ -5580,6 +5580,7 @@ static int handle_follow_request(struct filter *f) f->rth_for_killing = &rth2; } + monitor_mode =1 ;if (rtnl_dump_filter (&rth, generic_show_sock, f)) ret =-1 ;
tc/tc_class.c +1 -1
@@ -380,7 +380,7 @@ int print_class(struct nlmsghdr *n, void *arg)close_json_object (); }close_json_object (); -fflush (fp); +fflush_monitor (fp);return 0 ; }
tc/tc_filter.c +1 -1
@@ -374,7 +374,7 @@ int print_filter(struct nlmsghdr *n, void *arg)print_ext_msg (tb);close_json_object (); -fflush (fp); +fflush_monitor (fp);return 0 ; }
tc/tc_monitor.c +1 -0
@@ -100,6 +100,7 @@ int do_tcmonitor(int argc, char **argv)return ret; } + monitor_mode =1 ;if (rtnl_open (&rth, groups) <0 )exit (1 );
tc/tc_qdisc.c +1 -1
@@ -344,7 +344,7 @@ int print_qdisc(struct nlmsghdr *n, void *arg)print_ext_msg (tb);close_json_object (); -fflush (fp); +fflush_monitor (fp);return 0 ; }
Signature · 1 line
2.54.0.545.g6539524ca2-goog
Quoted 8 lines · On Mon, 27 Apr 2026 08:19:53 +0000 Eric Dumazet <[email protected]> wrote:
Some
fflush()calls only make sense for monitor programs.For other cases, forcing a flush is expensive.
After this patch, ip, tc and ss are correctly buffering most of their output when redirected to a file.
Signed-off-by Eric Dumazet <[email protected]>
What about pulling all the fflush from the leaf calls, and move the fflush to one common place. There are some of these that need to come back like the addr flush loop, this is just a semi-automated first pass.
From 815ed9eb987fc458987e753b3a2fa1786578b485 Mon Sep 17 00:00:00 2001
From: Stephen Hemminger <[email protected]>
Date: Mon, 27 Apr 2026 09:26:58 -0700
Subject: [PATCH] flush stdout only in monitor mode
As Eric pointed out fflush() only makes sense in monitor mode. In other modes buffering the output helps with large outputs like route dumps.
Move the fflush() into rtnl_listen() which is the common code used by all monitoring subcommands.
--- bridge/fdb.c | 2 -- bridge/link.c | 2 -- bridge/mdb.c | 1 - bridge/mst.c | 1 - bridge/vlan.c | 4 ---- bridge/vni.c | 2 -- genl/ctrl.c | 2 -- ip/ipaddress.c | 8 -------- ip/ipfou.c | 1 - ip/ipila.c | 1 - ip/ipioam6.c | 1 - ip/ipl2tp.c | 2 -- ip/iplink.c | 2 -- ip/ipmptcp.c | 6 ------ ip/ipmroute.c | 1 - ip/ipneigh.c | 4 ---- ip/ipnetconf.c | 1 - ip/ipnetns.c | 1 - ip/ipnexthop.c | 6 ------ ip/ipntable.c | 2 -- ip/ipprefix.c | 1 - ip/iproute.c | 4 ---- ip/iprule.c | 1 - ip/ipseg6.c | 1 - ip/ipstats.c | 2 -- ip/iptoken.c | 2 -- ip/iptuntap.c | 1 - ip/rtmon.c | 1 - ip/tcp_metrics.c | 4 ---- ip/xfrm_monitor.c | 5 ----- ip/xfrm_policy.c | 2 -- ip/xfrm_state.c | 1 - lib/libnetlink.c | 2 ++ tc/tc_class.c | 1 - tc/tc_filter.c | 1 - tc/tc_qdisc.c | 1 - 36 files changed, 2 insertions(+), 78 deletions(-)
bridge/fdb.c +0 -2
@@ -309,7 +309,6 @@ int print_fdb(struct nlmsghdr *n, void *arg)print_string (PRINT_ANY ,"state" ,"%s\n" ,state_n2a (r->ndm_state));close_json_object (); -fflush (fp);return 0 ; }
@@ -417,7 +416,6 @@ static int fdb_show(int argc, char **argv)exit (1 ); }delete_json_obj (); -fflush (stdout);return 0 ; }
bridge/link.c +0 -2
@@ -292,7 +292,6 @@ int print_linkinfo(struct nlmsghdr *n, void *arg)print_string (PRINT_FP ,NULL ,"%s" ,"\n" );close_json_object (); -fflush (fp);return 0 ; }
@@ -669,7 +668,6 @@ static int brlink_show(int argc, char **argv) }delete_json_obj (); -fflush (stdout);return 0 ; }
bridge/mdb.c +0 -1
@@ -501,7 +501,6 @@ static int mdb_show(int argc, char **argv)close_json_object ();delete_json_obj (); -fflush (stdout);return 0 ; }
bridge/mst.c +0 -1
@@ -158,7 +158,6 @@ static int mst_show(int argc, char **argv) }delete_json_obj (); -fflush (stdout);return 0 ; }
bridge/vlan.c +0 -4
@@ -756,7 +756,6 @@ static int print_vlan_stats(struct nlmsghdr *n, void *arg)struct if_stats_msg *ifsm =NLMSG_DATA (n);struct rtattr *tb[IFLA_STATS_MAX +1 ];int len = n->nlmsg_len; -FILE *fp = arg; len -=NLMSG_LENGTH (sizeof (*ifsm));if (len <0 ) {
@@ -778,7 +777,6 @@ static int print_vlan_stats(struct nlmsghdr *n, void *arg)print_vlan_stats_attr (tb[IFLA_STATS_LINK_XSTATS_SLAVE ], ifsm->ifindex); -fflush (fp);return 0 ; }
@@ -1205,7 +1203,6 @@ static int vlan_show(int argc, char **argv, int subject) out:delete_json_obj (); -fflush (stdout);return 0 ; }
@@ -1260,7 +1257,6 @@ static int vlan_global_show(int argc, char **argv)close_vlan_port ();delete_json_obj (); -fflush (stdout);return 0 ; }
bridge/vni.c +0 -2
@@ -333,7 +333,6 @@ int print_vnifilter_rtm(struct nlmsghdr *n, void *arg)if (opened)close_vni_port (); -fflush (stdout);return 0 ; }
@@ -381,7 +380,6 @@ static int vni_show(int argc, char **argv) }delete_json_obj (); -fflush (stdout);return 0 ; }
genl/ctrl.c +0 -2
@@ -275,7 +275,6 @@ static int print_ctrl(struct rtnl_ctrl_data *ctrl,struct genlmsghdr *ghdr =NLMSG_DATA (n);int len = n->nlmsg_len;struct rtattr *attrs; -FILE *fp = (FILE *) arg;if (n->nlmsg_type !=GENL_ID_CTRL ) { fprintf(stderr, "Not a controller message, nlmsg_len=%d "
@@ -339,7 +338,6 @@ static int print_ctrl(struct rtnl_ctrl_data *ctrl,if (tb[CTRL_ATTR_MCAST_GROUPS ])print_ctrl_mcast (tb[CTRL_ATTR_MCAST_GROUPS ]); -fflush (fp);return 0 ; }
ip/ipaddress.c +0 -8
@@ -894,7 +894,6 @@ static int print_linkinfo_brief(FILE *fp, const char *name,print_string (PRINT_FP ,NULL ,"%s" ,"\n" ); } -fflush (fp);return 0 ; }
@@ -1346,7 +1345,6 @@ int print_linkinfo(struct nlmsghdr *n, void *arg) }print_string (PRINT_FP ,NULL ,"%s" ,"\n" ); -fflush (fp);/* prettier here if stderr and stdout go to the same place */ if (truncated_vfs)fprintf (stderr,"Truncated VF list: %s\n" , name);
@@ -1732,7 +1730,6 @@ int print_addrinfo(struct nlmsghdr *n, void *arg) }print_string (PRINT_FP ,NULL ,"%s" ,"\n" ); brief_exit: -fflush (fp);return 0 ; }
@@ -1768,7 +1765,6 @@ static int print_selected_addrinfo(struct ifinfomsg *ifi,if (brief) {print_string (PRINT_FP ,NULL ,"%s" ,"\n" ); -fflush (fp); }return 0 ; }
@@ -2006,7 +2002,6 @@ static int ipaddr_flush(void)else printf ("*** Flush is complete after %d round%s ***\n" , round, round >1 ?"s" :"" ); } -fflush (stdout);return 0 ; } round++;
@@ -2015,7 +2010,6 @@ static int ipaddr_flush(void)if (show_stats) {printf ("\n*** Round %d, deleting %d addresses ***\n" , round, filter.flushed); -fflush (stdout); }/* If we are flushing, and specifying primary, then we
@@ -2027,7 +2021,6 @@ static int ipaddr_flush(void) goto flush_done; }fprintf (stderr,"*** Flush remains incomplete after %d rounds. ***\n" , max_flush_loops); -fflush (stderr);return 1 ; }
@@ -2351,7 +2344,6 @@ static int ipaddr_list_flush_or_save(int argc, char **argv, int action)print_link_stats (stdout, n);close_json_object (); } -fflush (stdout); out:free_nlmsg_chain (ainfo);
ip/ipfou.c +0 -1
@@ -322,7 +322,6 @@ static int do_show(int argc, char **argv)return 1 ; }delete_json_obj (); -fflush (stdout);return 0 ; }
ip/ipila.c +0 -1
@@ -159,7 +159,6 @@ static int do_list(int argc, char **argv)return 1 ; }delete_json_obj (); -fflush (stdout);return 0 ; }
ip/ipioam6.c +0 -1
@@ -261,7 +261,6 @@ static int ioam6_do_cmd(void)exit (1 ); }delete_json_obj (); -fflush (stdout); }return 0 ;
ip/ipl2tp.c +0 -2
@@ -467,7 +467,6 @@ static int get_session(struct l2tp_data *p)exit (1 ); }delete_json_obj (); -fflush (stdout);return 0 ; }
@@ -502,7 +501,6 @@ static int get_tunnel(struct l2tp_data *p)exit (1 ); }delete_json_obj (); -fflush (stdout);return 0 ; }
ip/iplink.c +0 -2
@@ -1296,7 +1296,6 @@ static void print_mpls_stats(FILE *fp, struct rtattr *attr)print_nl ();print_mpls_link_stats (fp, stats," " );print_string (PRINT_FP ,NULL ,"%s" ,"\n" ); -fflush (fp); }static void print_af_stats_attr (FILE *fp,int ifindex,struct rtattr *attr)
@@ -1359,7 +1358,6 @@ static int print_af_stats(struct nlmsghdr *n, void *arg)if (tb[IFLA_STATS_AF_SPEC ])print_af_stats_attr (fp, ifsm->ifindex, tb[IFLA_STATS_AF_SPEC ]); -fflush (fp);return 0 ; }
ip/ipmptcp.c +0 -6
@@ -276,7 +276,6 @@ static int print_mptcp_addrinfo(struct rtattr *addrinfo)print_nl ();close_json_object (); -fflush (stdout);return 0 ; }
@@ -320,12 +319,10 @@ static int mptcp_addr_dump(void)if (rtnl_dump_filter (&genl_rth, print_mptcp_addr, stdout) <0 ) {fprintf (stderr,"Dump terminated\n" );delete_json_obj (); -fflush (stdout);return -2 ; }delete_json_obj (); -fflush (stdout);return 0 ; }
@@ -349,7 +346,6 @@ static int mptcp_addr_show(int argc, char **argv) ret =print_mptcp_addr (answer, stdout);delete_json_obj ();free (answer); -fflush (stdout);return ret; }
@@ -430,7 +426,6 @@ static int print_mptcp_limit(struct nlmsghdr *n, void *arg)print_nl ();close_json_object (); -fflush (stdout);return 0 ; }
@@ -592,7 +587,6 @@ static int mptcp_monitor_msg(struct rtnl_ctrl_data *ctrl, out:print_nl ();close_json_object (); -fflush (stdout);return 0 ; }
ip/ipmroute.c +0 -1
@@ -206,7 +206,6 @@ int print_mroute(struct nlmsghdr *n, void *arg)print_string (PRINT_FP ,NULL ,"\n" ,NULL );close_json_object (); -fflush (fp);return 0 ; }
ip/ipneigh.c +0 -4
@@ -298,7 +298,6 @@ static int print_neigh_brief(FILE *fp, struct ndmsg *r, struct rtattr *tb[])print_string (PRINT_FP ,NULL ,"%s" ,"\n" );close_json_object (); -fflush (fp);return 0 ; }
@@ -472,7 +471,6 @@ int print_neigh(struct nlmsghdr *n, void *arg)print_string (PRINT_FP ,NULL ,"\n" ,"" );close_json_object (); -fflush (fp);return 0 ; }
@@ -630,7 +628,6 @@ static int do_show_or_flush(int argc, char **argv, int flush)else printf ("*** Flush is complete after %d round%s ***\n" , round, round >1 ?"s" :"" ); } -fflush (stdout);return 0 ; } round++;
@@ -638,7 +635,6 @@ static int do_show_or_flush(int argc, char **argv, int flush)exit (1 );if (show_stats) {printf ("\n*** Round %d, deleting %d entries ***\n" , round, filter.flushed); -fflush (stdout); } filter.state &= ~NUD_FAILED ; }
ip/ipnetconf.c +0 -1
@@ -140,7 +140,6 @@ int print_netconf(struct rtnl_ctrl_data *ctrl, struct nlmsghdr *n, void *arg)close_json_object ();print_string (PRINT_FP ,NULL ,"\n" ,NULL ); -fflush (fp);return 0 ; }
ip/ipnetns.c +0 -1
@@ -327,7 +327,6 @@ int print_nsid(struct nlmsghdr *n, void *arg)print_string (PRINT_FP ,NULL ,"\n" ,NULL );close_json_object (); -fflush (fp);return 0 ; }
ip/ipnexthop.c +0 -6
@@ -826,7 +826,6 @@ int print_cache_nexthop(struct nlmsghdr *n, void *arg, bool process_cache)__print_nexthop_entry (fp,NULL , &nhe, n->nlmsg_type ==RTM_DELNEXTHOP );print_string (PRINT_FP ,NULL ,"%s" ,"\n" ); -fflush (fp);if (process_cache)ipnh_cache_process_nlmsg (n, &nhe);
@@ -882,7 +881,6 @@ int print_nexthop_bucket(struct nlmsghdr *n, void *arg)print_string (PRINT_FP ,NULL ,"%s" ,"\n" );close_json_object (); -fflush (fp);return 0 ; }
@@ -1178,7 +1176,6 @@ static int ipnh_get_id(__u32 id) }delete_json_obj (); -fflush (stdout);free (answer);
@@ -1266,7 +1263,6 @@ static int ipnh_list_flush(int argc, char **argv, int action) }delete_json_obj (); -fflush (stdout);return 0 ; }
@@ -1342,7 +1338,6 @@ static int ipnh_bucket_list(int argc, char **argv) }delete_json_obj (); -fflush (stdout);return 0 ; }
@@ -1383,7 +1378,6 @@ static int ipnh_bucket_get_id(__u32 id, __u16 bucket_index) }delete_json_obj (); -fflush (stdout);free (answer);
ip/ipntable.c +0 -2
@@ -532,7 +532,6 @@ static void print_ndtstats(const struct ndt_stats *ndts)static int print_ntable (struct nlmsghdr *n,void *arg) { -FILE *fp = (FILE *)arg;struct ndtmsg *ndtm =NLMSG_DATA (n);int len = n->nlmsg_len;struct rtattr *tb[NDTA_MAX +1 ];
@@ -633,7 +632,6 @@ static int print_ntable(struct nlmsghdr *n, void *arg)print_string (PRINT_FP ,NULL ,"\n" ,"" );close_json_object (); -fflush (fp);return 0 ; }
ip/ipprefix.c +0 -1
@@ -81,7 +81,6 @@ int print_prefix(struct nlmsghdr *n, void *arg) }fprintf (fp,"\n" ); -fflush (fp);return 0 ; }
ip/iproute.c +0 -4
@@ -1012,7 +1012,6 @@ int print_route(struct nlmsghdr *n, void *arg)print_string (PRINT_FP ,NULL ,"\n" ,NULL );close_json_object (); -fflush (fp);return 0 ; }
@@ -1781,7 +1780,6 @@ static int iproute_flush(int family, rtnl_filter_t filter_fn)printf ("*** Flush is complete after %d round%s ***\n" , round, round >1 ?"s" :"" ); } -fflush (stdout);return 0 ; } round++;
@@ -1798,7 +1796,6 @@ static int iproute_flush(int family, rtnl_filter_t filter_fn)if (show_stats) {printf ("\n*** Round %d, deleting %d entries ***\n" , round, filter.flushed); -fflush (stdout); } } }
@@ -2035,7 +2032,6 @@ static int iproute_list_flush_or_save(int argc, char **argv, int action) }delete_json_obj (); -fflush (stdout);return 0 ; }
ip/iprule.c +0 -1
@@ -599,7 +599,6 @@ int print_rule(struct nlmsghdr *n, void *arg)print_string (PRINT_FP ,NULL ,"\n" ,"" );close_json_object (); -fflush (fp);return 0 ; }
ip/ipseg6.c +0 -1
@@ -197,7 +197,6 @@ static int seg6_do_cmd(void)exit (1 ); }delete_json_obj (); -fflush (stdout); }return 0 ;
ip/ipstats.c +0 -2
@@ -879,7 +879,6 @@ static int ipstats_dump(struct ipstats_stat_enabled *enabled) rc =-2 ; } -fflush (stdout);return rc; }
@@ -1355,6 +1354,5 @@ int ipstats_print(struct nlmsghdr *n, void *arg)if (rc)return rc; -fflush (fp);return 0 ; }
ip/iptoken.c +0 -2
@@ -41,7 +41,6 @@ static void usage(void)static int print_token (struct nlmsghdr *n,void *arg) {struct rtnl_dump_args *args = arg; -FILE *fp = args->fp;int ifindex = args->ifindex;struct ifinfomsg *ifi =NLMSG_DATA (n);int len = n->nlmsg_len;
@@ -85,7 +84,6 @@ static int print_token(struct nlmsghdr *n, void *arg)"ifname" ,"%s\n" , ll_index_to_name(ifi->ifi_index));close_json_object (); -fflush (fp);return 0 ; }
ip/iptuntap.c +0 -1
@@ -444,7 +444,6 @@ static int do_show(int argc, char **argv) }delete_json_obj (); -fflush (stdout);return 0 ; }
ip/rtmon.c +0 -1
@@ -46,7 +46,6 @@ static int dump_msg(struct rtnl_ctrl_data *ctrl,if (!init_phase)write_stamp (fp);fwrite ((void *)n,1 ,NLMSG_ALIGN (n->nlmsg_len), fp); -fflush (fp);return 0 ; }
ip/tcp_metrics.c +0 -4
@@ -156,7 +156,6 @@ static void print_tcp_metrics(struct rtattr *a)static int process_msg (struct nlmsghdr *n,void *arg) { -FILE *fp = (FILE *) arg;struct genlmsghdr *ghdr;struct rtattr *attrs[TCP_METRICS_ATTR_MAX +1 ], *a;const char *h;
@@ -334,7 +333,6 @@ static int process_msg(struct nlmsghdr *n, void *arg)print_string (PRINT_FP ,NULL ,"\n" ,"" );close_json_object (); -fflush (fp);return 0 ; }
@@ -477,7 +475,6 @@ static int tcpm_do_cmd(int cmd, int argc, char **argv) }else if (show_stats)printf ("*** Flush is complete after %d round%s ***\n" , round, round >1 ?"s" :"" ); -fflush (stdout);return 0 ; } round++;
@@ -486,7 +483,6 @@ static int tcpm_do_cmd(int cmd, int argc, char **argv)if (show_stats) {printf ("\n*** Round %d, deleting %d entries ***\n" , round, f.flushed); -fflush (stdout); } }return 0 ;
ip/xfrm_monitor.c +0 -5
@@ -85,7 +85,6 @@ static int xfrm_acquire_print(struct nlmsghdr *n, void *arg)if (oneline)fprintf (fp,"\n" ); -fflush (fp);return 0 ; }
@@ -114,7 +113,6 @@ static int xfrm_state_flush_print(struct nlmsghdr *n, void *arg)if (oneline)fprintf (fp,"\n" ); -fflush (fp);return 0 ; }
@@ -151,7 +149,6 @@ static int xfrm_policy_flush_print(struct nlmsghdr *n, void *arg)if (oneline)fprintf (fp,"\n" ); -fflush (fp);return 0 ; }
@@ -232,7 +229,6 @@ static int xfrm_ae_print(struct nlmsghdr *n, void *arg)xfrm_usersa_print (&id->sa_id, id->reqid, fp);fprintf (fp,"\n" ); -fflush (fp);return 0 ; }
@@ -257,7 +253,6 @@ static int xfrm_mapping_print(struct nlmsghdr *n, void *arg)xfrm_usersa_print (&map->id, map->reqid, fp);fprintf (fp,"\n" ); -fflush (fp);return 0 ; }
ip/xfrm_policy.c +0 -2
@@ -565,7 +565,6 @@ int xfrm_policy_print(struct nlmsghdr *n, void *arg)if (oneline)fprintf (fp,"\n" ); -fflush (fp);return 0 ; }
@@ -1232,7 +1231,6 @@ int xfrm_policy_default_print(struct nlmsghdr *n, FILE *fp)fprintf (fp," in: %s\n" ,xfrm_policy_to_str (up->in));fprintf (fp," fwd: %s\n" ,xfrm_policy_to_str (up->fwd));fprintf (fp," out: %s\n" ,xfrm_policy_to_str (up->out)); -fflush (fp);return 0 ; }
ip/xfrm_state.c +0 -1
@@ -1051,7 +1051,6 @@ static int __do_xfrm_state_print(struct nlmsghdr *n, void *arg, bool nokeys)if (oneline)fprintf (fp,"\n" ); -fflush (fp);return 0 ; }
lib/libnetlink.c +2 -0
@@ -1275,6 +1275,8 @@ int rtnl_listen(struct rtnl_handle *rtnl, } err =handler (&ctrl, h, jarg); +fflush (stdout); +if (err <0 )return err;
tc/tc_class.c +0 -1
@@ -380,7 +380,6 @@ int print_class(struct nlmsghdr *n, void *arg)close_json_object (); }close_json_object (); -fflush (fp);return 0 ; }
tc/tc_filter.c +0 -1
@@ -374,7 +374,6 @@ int print_filter(struct nlmsghdr *n, void *arg)print_ext_msg (tb);close_json_object (); -fflush (fp);return 0 ; }
tc/tc_qdisc.c +0 -1
@@ -344,7 +344,6 @@ int print_qdisc(struct nlmsghdr *n, void *arg)print_ext_msg (tb);close_json_object (); -fflush (fp);return 0 ; }
Signature · 1 line
2.53.0
Quoted 8 lines · Stephen Hemminger
Quoted 4 lines · On Mon, 27 Apr 2026 08:19:53 +0000 Eric Dumazet <[email protected]> wrote:
Some
fflush()calls only make sense for monitor programs.For other cases, forcing a flush is expensive.
Quoted 8 lines
Quoted 4 lines
After this patch, ip, tc and ss are correctly buffering most of their output when redirected to a file.
Signed-off-by Eric Dumazet <[email protected]>What about pulling all the fflush from the leaf calls, and move the fflush to one common place. There are some of these that need to come back like the addr flush loop, this is just a semi-automated first pass.
SGTM, thanks.
Quoted 21 lines · Eric Dumazet
Some
fflush()calls only make sense for monitor programs.For other cases, forcing a flush is expensive.
After this patch, ip, tc and ss are correctly buffering most of their output when redirected to a file.
Signed-off-by Eric Dumazet <[email protected]>--- include/utils.h | 7 +++++++ ip/ipaddress.c | 12 +++++++----- ip/iplink.c | 4 ++-- ip/ipmonitor.c | 1 + ip/ipmptcp.c | 10 +++++----- ip/ipneigh.c | 4 ++-- ip/ipnetconf.c | 2 +- ip/ipnetns.c | 2 +- ip/ipnexthop.c | 12 ++++++------ ip/iproute.c | 4 ++-- ip/iprule.c | 2 +- ip/iptoken.c | 2 +-
Quoted 9 lines
ip/tcp_metrics.c | 2 +- lib/utils.c | 1 + misc/ss.c | 3 ++- tc/tc_class.c | 2 +- tc/tc_filter.c | 2 +- tc/tc_monitor.c | 1 + tc/tc_qdisc.c | 2 +- 19 files changed, 44 insertions(+), 31 deletions(-)
no longer applies after merging main to next.
Quoted 33 lines · David Ahern
Quoted 21 lines · Eric Dumazet
Some
fflush()calls only make sense for monitor programs.For other cases, forcing a flush is expensive.
After this patch, ip, tc and ss are correctly buffering most of their output when redirected to a file.
Signed-off-by Eric Dumazet <[email protected]>--- include/utils.h | 7 +++++++ ip/ipaddress.c | 12 +++++++----- ip/iplink.c | 4 ++-- ip/ipmonitor.c | 1 + ip/ipmptcp.c | 10 +++++----- ip/ipneigh.c | 4 ++-- ip/ipnetconf.c | 2 +- ip/ipnetns.c | 2 +- ip/ipnexthop.c | 12 ++++++------ ip/iproute.c | 4 ++-- ip/iprule.c | 2 +- ip/iptoken.c | 2 +-Quoted 9 lines
ip/tcp_metrics.c | 2 +- lib/utils.c | 1 + misc/ss.c | 3 ++- tc/tc_class.c | 2 +- tc/tc_filter.c | 2 +- tc/tc_monitor.c | 1 + tc/tc_qdisc.c | 2 +- 19 files changed, 44 insertions(+), 31 deletions(-)
Quoted 1 line
no longer applies after merging main to next.0
Hmm... I thought Stephen had a better implementation?
Quoted 32 lines · On Thu, 30 Apr 2026 09:35:08 -0600 David Ahern <[email protected]> wrote:
Quoted 21 lines · Eric Dumazet
Some
fflush()calls only make sense for monitor programs.For other cases, forcing a flush is expensive.
After this patch, ip, tc and ss are correctly buffering most of their output when redirected to a file.
Signed-off-by Eric Dumazet <[email protected]>--- include/utils.h | 7 +++++++ ip/ipaddress.c | 12 +++++++----- ip/iplink.c | 4 ++-- ip/ipmonitor.c | 1 + ip/ipmptcp.c | 10 +++++----- ip/ipneigh.c | 4 ++-- ip/ipnetconf.c | 2 +- ip/ipnetns.c | 2 +- ip/ipnexthop.c | 12 ++++++------ ip/iproute.c | 4 ++-- ip/iprule.c | 2 +- ip/iptoken.c | 2 +-Quoted 9 lines
ip/tcp_metrics.c | 2 +- lib/utils.c | 1 + misc/ss.c | 3 ++- tc/tc_class.c | 2 +- tc/tc_filter.c | 2 +- tc/tc_monitor.c | 1 + tc/tc_qdisc.c | 2 +- 19 files changed, 44 insertions(+), 31 deletions(-)
Quoted 2 lines
no longer applies after merging main to next.
I put the other version in iproute2 since wanted to test and it did not depend on any later kernel changes