From 5bc4e2d498bd4920634dbf43cc15ea46ffebefcf Mon Sep 17 00:00:00 2001 From: yamaguchi Date: Mon, 20 Jun 2022 08:20:09 +0000 Subject: bpf(4): added support for VLAN hardware offloading of ethernet devices --- sys/net/bpf.h | 11 ++++++++--- sys/net/if.h | 4 +++- sys/net/if_ethersubr.c | 36 ++++++++++++++++++++++++++++++++++-- 3 files changed, 45 insertions(+), 6 deletions(-) (limited to 'sys') diff --git a/sys/net/bpf.h b/sys/net/bpf.h index 5dca72d55c7..dda03c9d813 100644 --- a/sys/net/bpf.h +++ b/sys/net/bpf.h @@ -1,4 +1,4 @@ -/* $NetBSD: bpf.h,v 1.77 2021/06/09 15:44:15 martin Exp $ */ +/* $NetBSD: bpf.h,v 1.78 2022/06/20 08:20:09 yamaguchi Exp $ */ /* * Copyright (c) 1990, 1991, 1993 @@ -474,8 +474,13 @@ bpf_attach2(struct ifnet *_ifp, u_int _dlt, u_int _hdrlen, struct bpf_if **_dp) static __inline void bpf_mtap(struct ifnet *_ifp, struct mbuf *_m, u_int _direction) { - if (_ifp->if_bpf) - bpf_ops->bpf_mtap(_ifp->if_bpf, _m, _direction); + if (_ifp->if_bpf) { + if (_ifp->if_bpf_mtap) { + _ifp->if_bpf_mtap(_ifp->if_bpf, _m, _direction); + } else { + bpf_ops->bpf_mtap(_ifp->if_bpf, _m, _direction); + } + } } static __inline void diff --git a/sys/net/if.h b/sys/net/if.h index 9bdd5074029..f9476108558 100644 --- a/sys/net/if.h +++ b/sys/net/if.h @@ -1,4 +1,4 @@ -/* $NetBSD: if.h,v 1.297 2022/06/20 08:02:25 yamaguchi Exp $ */ +/* $NetBSD: if.h,v 1.298 2022/06/20 08:20:09 yamaguchi Exp $ */ /*- * Copyright (c) 1999, 2000, 2001 The NetBSD Foundation, Inc. @@ -340,6 +340,8 @@ typedef struct ifnet { #define if_watchdog if_slowtimo void (*if_drain) /* :: routine to release resources */ (struct ifnet *); + void (*if_bpf_mtap) /* :: bpf routine */ + (struct bpf_if *, struct mbuf *, u_int); struct ifaltq if_snd; /* q: output queue (includes altq) */ struct ifaddr *if_dl; /* i: identity of this interface. */ const struct sockaddr_dl diff --git a/sys/net/if_ethersubr.c b/sys/net/if_ethersubr.c index 1ea0a442d9b..91c6fe0a503 100644 --- a/sys/net/if_ethersubr.c +++ b/sys/net/if_ethersubr.c @@ -1,4 +1,4 @@ -/* $NetBSD: if_ethersubr.c,v 1.313 2022/06/20 08:14:48 yamaguchi Exp $ */ +/* $NetBSD: if_ethersubr.c,v 1.314 2022/06/20 08:20:09 yamaguchi Exp $ */ /* * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. @@ -61,7 +61,7 @@ */ #include -__KERNEL_RCSID(0, "$NetBSD: if_ethersubr.c,v 1.313 2022/06/20 08:14:48 yamaguchi Exp $"); +__KERNEL_RCSID(0, "$NetBSD: if_ethersubr.c,v 1.314 2022/06/20 08:20:09 yamaguchi Exp $"); #ifdef _KERNEL_OPT #include "opt_inet.h" @@ -971,6 +971,37 @@ error: return; } +static void +ether_bpf_mtap(struct bpf_if *bp, struct mbuf *m, u_int direction) +{ + struct ether_vlan_header evl; + struct m_hdr mh, md; + + KASSERT(bp != NULL); + + if (!vlan_has_tag(m)) { + bpf_mtap3(bp, m, direction); + return; + } + + memcpy(&evl, mtod(m, char *), ETHER_HDR_LEN); + evl.evl_proto = evl.evl_encap_proto; + evl.evl_encap_proto = htons(ETHERTYPE_VLAN); + evl.evl_tag = htons(vlan_get_tag(m)); + + md.mh_flags = 0; + md.mh_data = m->m_data + ETHER_HDR_LEN; + md.mh_len = m->m_len - ETHER_HDR_LEN; + md.mh_next = m->m_next; + + mh.mh_flags = 0; + mh.mh_data = (char *)&evl; + mh.mh_len = sizeof(evl); + mh.mh_next = (struct mbuf *)&md; + + bpf_mtap3(bp, (struct mbuf *)&mh, direction); +} + /* * Convert Ethernet address to printable (loggable) representation. */ @@ -1011,6 +1042,7 @@ ether_ifattach(struct ifnet *ifp, const uint8_t *lla) ifp->if_mtu = ETHERMTU; ifp->if_output = ether_output; ifp->_if_input = ether_input; + ifp->if_bpf_mtap = ether_bpf_mtap; if (ifp->if_baudrate == 0) ifp->if_baudrate = IF_Mbps(10); /* just a default */ -- cgit