Index: if.c =================================================================== RCS file: /cvs/src/sys/net/if.c,v retrieving revision 1.611 diff -u -p -r1.611 if.c --- if.c 30 Jun 2020 09:31:38 -0000 1.611 +++ if.c 13 Jul 2020 23:48:40 -0000 @@ -72,6 +72,8 @@ #include "switch.h" #include "if_wg.h" +#include "kstat.h" + #include #include #include @@ -129,6 +131,10 @@ #include #endif +#if NKSTAT > 0 +#include +#endif + #include void if_attachsetup(struct ifnet *); @@ -246,6 +252,22 @@ struct task if_input_task_locked = TASK_ */ struct rwlock netlock = RWLOCK_INITIALIZER("netlock"); +#if NKSTAT > 0 +struct net_kstats { + struct kstat_kv nk_cycles; + struct kstat_kv nk_packets; +}; + +struct net_kstats net_kstats = { + KSTAT_KV_UNIT_INITIALIZER("cycles", + KSTAT_KV_T_COUNTER64, KSTAT_KV_U_CYCLES), + KSTAT_KV_UNIT_INITIALIZER("packets", + KSTAT_KV_T_COUNTER64, KSTAT_KV_U_PACKETS), +}; +struct mutex net_kstats_mtx = MUTEX_INITIALIZER(IPL_SOFTNET); +struct kstat *net_ks; +#endif /* NKSTAT > 0 */ + /* * Network interface utility routines. */ @@ -265,6 +287,16 @@ ifinit(void) if (nettqmp[i] == NULL) panic("unable to create network taskq %d", i); } + +#if NKSTAT > 0 + net_ks = kstat_create("net", 0, "counters", 0, KSTAT_T_KV, 0); + if (net_ks != NULL) { + net_ks->ks_data = &net_kstats; + net_ks->ks_datalen = sizeof(net_kstats); + kstat_set_mutex(net_ks, &net_kstats_mtx); + kstat_install(net_ks); + } +#endif /* NKSTAT > 0 */ } static struct if_idxmap if_idxmap = { @@ -912,6 +992,9 @@ void if_input_process(struct ifnet *ifp, struct mbuf_list *ml) { struct mbuf *m; +#if NKSTAT > 0 + uint64_t tic, toc, packets; +#endif if (ml_empty(ml)) return; @@ -932,9 +1015,23 @@ if_input_process(struct ifnet *ifp, stru * lists and the socket layer. */ NET_LOCK(); +#if NKSTAT > 0 + packets = ml_len(ml); + tic = rdtsc_lfence(); +#endif while ((m = ml_dequeue(ml)) != NULL) if_ih_input(ifp, m); +#if NKSTAT > 0 + toc = rdtsc_lfence(); +#endif NET_UNLOCK(); + +#if NKSTAT > 0 + mtx_enter(&net_kstats_mtx); + kstat_kv_u64(&net_kstats.nk_cycles) += toc - tic; + kstat_kv_u64(&net_kstats.nk_packets) += packets; + mtx_leave(&net_kstats_mtx); +#endif } void