From d37298af3c3df4811a27ed4ca8162ffdafca817d Mon Sep 17 00:00:00 2001 From: ae Date: Tue, 23 Dec 2014 09:39:40 +0000 Subject: [PATCH] Add if_inc_counter() and if_get_counter_default() functions that do access to ifnet counters for code compatibility with FreeBSD 11. This is direct commit to stable/10. Discussed with: glebius@, arch@ git-svn-id: svn://svn.freebsd.org/base/stable/10@276124 ccf9f872-aa2e-dd11-9fc8-001c23d0bc1f --- sys/net/if.c | 94 ++++++++++++++++++++++++++++++++++++++++++++++++ sys/net/if_var.h | 18 ++++++++++ 2 files changed, 112 insertions(+) diff --git a/sys/net/if.c b/sys/net/if.c index 2fc62ea82..417f8cb44 100644 --- a/sys/net/if.c +++ b/sys/net/if.c @@ -1449,6 +1449,100 @@ if_rtdel(struct radix_node *rn, void *arg) return (0); } +/* + * A compatibility function returns ifnet counter values. + */ +uint64_t +if_get_counter_default(struct ifnet *ifp, ift_counter cnt) +{ + + KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt)); + switch (cnt) { + case IFCOUNTER_IPACKETS: + return (ifp->if_ipackets); + case IFCOUNTER_IERRORS: + return (ifp->if_ierrors); + case IFCOUNTER_OPACKETS: + return (ifp->if_opackets); + case IFCOUNTER_OERRORS: + return (ifp->if_oerrors); + case IFCOUNTER_COLLISIONS: + return (ifp->if_collisions); + case IFCOUNTER_IBYTES: + return (ifp->if_ibytes); + case IFCOUNTER_OBYTES: + return (ifp->if_obytes); + case IFCOUNTER_IMCASTS: + return (ifp->if_imcasts); + case IFCOUNTER_OMCASTS: + return (ifp->if_omcasts); + case IFCOUNTER_IQDROPS: + return (ifp->if_iqdrops); +#ifdef _IFI_OQDROPS + case IFCOUNTER_OQDROPS: + return (ifp->if_oqdrops); +#endif + case IFCOUNTER_NOPROTO: + return (ifp->if_noproto); + default: + break; + }; + return (0); +} + +/* + * Increase an ifnet counter. Usually used for counters shared + * between the stack and a driver, but function supports them all. + */ +void +if_inc_counter(struct ifnet *ifp, ift_counter cnt, int64_t inc) +{ + + KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt)); + switch (cnt) { + case IFCOUNTER_IPACKETS: + ifp->if_ipackets += inc; + break; + case IFCOUNTER_IERRORS: + ifp->if_ierrors += inc; + break; + case IFCOUNTER_OPACKETS: + ifp->if_opackets += inc; + break; + case IFCOUNTER_OERRORS: + ifp->if_oerrors += inc; + break; + case IFCOUNTER_COLLISIONS: + ifp->if_collisions += inc; + break; + case IFCOUNTER_IBYTES: + ifp->if_ibytes += inc; + break; + case IFCOUNTER_OBYTES: + ifp->if_obytes += inc; + break; + case IFCOUNTER_IMCASTS: + ifp->if_imcasts += inc; + break; + case IFCOUNTER_OMCASTS: + ifp->if_omcasts += inc; + break; + case IFCOUNTER_IQDROPS: + ifp->if_iqdrops += inc; + break; +#ifdef _IFI_OQDROPS + case IFCOUNTER_OQDROPS: + ifp->if_oqdrops += inc; + break; +#endif + case IFCOUNTER_NOPROTO: + ifp->if_noproto += inc; + break; + default: + break; + }; +} + /* * Wrapper functions for struct ifnet address list locking macros. These are * used by kernel modules to avoid encoding programming interface or binary diff --git a/sys/net/if_var.h b/sys/net/if_var.h index e6ed31bd2..58391a17d 100644 --- a/sys/net/if_var.h +++ b/sys/net/if_var.h @@ -104,6 +104,22 @@ VNET_DECLARE(struct pfil_head, link_pfil_hook); /* packet filter hooks */ #define V_link_pfil_hook VNET(link_pfil_hook) #endif /* _KERNEL */ +typedef enum { + IFCOUNTER_IPACKETS = 0, + IFCOUNTER_IERRORS, + IFCOUNTER_OPACKETS, + IFCOUNTER_OERRORS, + IFCOUNTER_COLLISIONS, + IFCOUNTER_IBYTES, + IFCOUNTER_OBYTES, + IFCOUNTER_IMCASTS, + IFCOUNTER_OMCASTS, + IFCOUNTER_IQDROPS, + IFCOUNTER_OQDROPS, + IFCOUNTER_NOPROTO, + IFCOUNTERS /* Array size. */ +} ift_counter; + /* * Structure defining a queue for a network interface. */ @@ -981,6 +997,8 @@ typedef void *if_com_alloc_t(u_char type, struct ifnet *ifp); typedef void if_com_free_t(void *com, u_char type); void if_register_com_alloc(u_char type, if_com_alloc_t *a, if_com_free_t *f); void if_deregister_com_alloc(u_char type); +uint64_t if_get_counter_default(struct ifnet *, ift_counter); +void if_inc_counter(struct ifnet *, ift_counter, int64_t); #define IF_LLADDR(ifp) \ LLADDR((struct sockaddr_dl *)((ifp)->if_addr->ifa_addr)) -- 2.45.0