Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys - centralize header align and pullup into a single inlin...



details:   https://anonhg.NetBSD.org/src/rev/acfcbad6a468
branches:  trunk
changeset: 980682:acfcbad6a468
user:      christos <christos%NetBSD.org@localhost>
date:      Sun Feb 14 20:58:34 2021 +0000

description:
- centralize header align and pullup into a single inline function
- use a single macro to align pointers and expose the alignment, instead
  of hard-coding 3 in 1/2 the macros.
- fix an issue in the ipv6 lt2p where it was aligning for ipv4 and pulling
  for ipv6.

diffstat:

 sys/net/if_arp.h           |   3 ++-
 sys/net/if_bridge.c        |  40 +++++++++++-----------------------------
 sys/netinet/icmp_private.h |   8 ++------
 sys/netinet/if_arp.c       |   8 ++++----
 sys/netinet/igmp_var.h     |   8 ++------
 sys/netinet/in_l2tp.c      |  13 ++++---------
 sys/netinet/ip_flow.c      |  11 +++++------
 sys/netinet/ip_input.c     |  20 ++++++--------------
 sys/netinet/ip_private.h   |   8 ++------
 sys/netinet/tcp_input.c    |  10 +++++-----
 sys/netinet/tcp_private.h  |   8 ++------
 sys/netinet/udp_private.h  |   8 ++------
 sys/netinet/udp_usrreq.c   |   8 ++++----
 sys/netinet6/icmp6.c       |   8 ++++----
 sys/netinet6/in6_l2tp.c    |  14 +++++---------
 sys/netinet6/ip6_flow.c    |   6 +++---
 sys/netinet6/ip6_input.c   |  29 ++++++++++-------------------
 sys/netinet6/ip6_private.h |   8 ++------
 sys/netinet6/udp6_usrreq.c |   8 ++++----
 sys/sys/mbuf.h             |  13 ++++++++++++-
 sys/sys/param.h            |   8 +++++++-
 21 files changed, 98 insertions(+), 149 deletions(-)

diffs (truncated from 710 to 300 lines):

diff -r d81380ebf99a -r acfcbad6a468 sys/net/if_arp.h
--- a/sys/net/if_arp.h  Sun Feb 14 20:30:31 2021 +0000
+++ b/sys/net/if_arp.h  Sun Feb 14 20:58:34 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: if_arp.h,v 1.39 2021/02/14 19:47:16 roy Exp $  */
+/*     $NetBSD: if_arp.h,v 1.40 2021/02/14 20:58:34 christos Exp $     */
 
 /*
  * Copyright (c) 1986, 1993
@@ -72,6 +72,7 @@
        uint8_t  ar_tpa[];      /* target protocol address */
 #endif
 };
+#define        ARP_HDR_ALIGNMENT       3
 
 static __inline uint8_t *
 ar_data(struct arphdr *ap)
diff -r d81380ebf99a -r acfcbad6a468 sys/net/if_bridge.c
--- a/sys/net/if_bridge.c       Sun Feb 14 20:30:31 2021 +0000
+++ b/sys/net/if_bridge.c       Sun Feb 14 20:58:34 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: if_bridge.c,v 1.177 2020/11/02 12:14:59 roy Exp $      */
+/*     $NetBSD: if_bridge.c,v 1.178 2021/02/14 20:58:34 christos Exp $ */
 
 /*
  * Copyright 2001 Wasabi Systems, Inc.
@@ -80,7 +80,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: if_bridge.c,v 1.177 2020/11/02 12:14:59 roy Exp $");
+__KERNEL_RCSID(0, "$NetBSD: if_bridge.c,v 1.178 2021/02/14 20:58:34 christos Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_inet.h"
@@ -2806,18 +2806,10 @@
        if (*mp == NULL)
                return -1;
 
-       if (IP_HDR_ALIGNED_P(mtod(m, void *)) == 0) {
-               if ((m = m_copyup(m, sizeof(struct ip),
-                       (max_linkhdr + 3) & ~3)) == NULL) {
-                       /* XXXJRT new stat, please */
-                       ip_statinc(IP_STAT_TOOSMALL);
-                       goto bad;
-               }
-       } else if (__predict_false(m->m_len < sizeof (struct ip))) {
-               if ((m = m_pullup(m, sizeof (struct ip))) == NULL) {
-                       ip_statinc(IP_STAT_TOOSMALL);
-                       goto bad;
-               }
+       if (m_get_aligned_hdr(&m, IP_HDR_ALIGNMENT, sizeof(*ip), true) != 0) {
+               /* XXXJRT new stat, please */
+               ip_statinc(IP_STAT_TOOSMALL);
+               goto bad;
        }
        ip = mtod(m, struct ip *);
        if (ip == NULL) goto bad;
@@ -2908,22 +2900,12 @@
         * it.  Otherwise, if it is aligned, make sure the entire base
         * IPv6 header is in the first mbuf of the chain.
         */
-       if (IP6_HDR_ALIGNED_P(mtod(m, void *)) == 0) {
+       if (m_get_aligned_hdr(&m, IP6_HDR_ALIGNMENT, sizeof(*ip6), true) != 0) {
                struct ifnet *inifp = m_get_rcvif_NOMPSAFE(m);
-               if ((m = m_copyup(m, sizeof(struct ip6_hdr),
-                                 (max_linkhdr + 3) & ~3)) == NULL) {
-                       /* XXXJRT new stat, please */
-                       ip6_statinc(IP6_STAT_TOOSMALL);
-                       in6_ifstat_inc(inifp, ifs6_in_hdrerr);
-                       goto bad;
-               }
-       } else if (__predict_false(m->m_len < sizeof(struct ip6_hdr))) {
-               struct ifnet *inifp = m_get_rcvif_NOMPSAFE(m);
-               if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
-                       ip6_statinc(IP6_STAT_TOOSMALL);
-                       in6_ifstat_inc(inifp, ifs6_in_hdrerr);
-                       goto bad;
-               }
+               /* XXXJRT new stat, please */
+               ip6_statinc(IP6_STAT_TOOSMALL);
+               in6_ifstat_inc(inifp, ifs6_in_hdrerr);
+               goto bad;
        }
 
        ip6 = mtod(m, struct ip6_hdr *);
diff -r d81380ebf99a -r acfcbad6a468 sys/netinet/icmp_private.h
--- a/sys/netinet/icmp_private.h        Sun Feb 14 20:30:31 2021 +0000
+++ b/sys/netinet/icmp_private.h        Sun Feb 14 20:58:34 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: icmp_private.h,v 1.3 2008/04/28 20:24:09 martin Exp $  */
+/*     $NetBSD: icmp_private.h,v 1.4 2021/02/14 20:58:35 christos Exp $        */
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -44,11 +44,7 @@
 
 #define        ICMP_STATINC(x)         _NET_STATINC(icmpstat_percpu, x)
 
-#ifdef __NO_STRICT_ALIGNMENT
-#define        ICMP_HDR_ALIGNED_P(ic)  1
-#else
-#define        ICMP_HDR_ALIGNED_P(ic)  ((((vaddr_t) (ic)) & 3) == 0)
-#endif
+#define        ICMP_HDR_ALIGNMENT      3
 #endif /* _KERNEL_ */
 
 #endif /* !_NETINET_ICMP_PRIVATE_H_ */
diff -r d81380ebf99a -r acfcbad6a468 sys/netinet/if_arp.c
--- a/sys/netinet/if_arp.c      Sun Feb 14 20:30:31 2021 +0000
+++ b/sys/netinet/if_arp.c      Sun Feb 14 20:58:34 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: if_arp.c,v 1.301 2021/02/14 19:47:17 roy Exp $ */
+/*     $NetBSD: if_arp.c,v 1.302 2021/02/14 20:58:35 christos Exp $    */
 
 /*
  * Copyright (c) 1998, 2000, 2008 The NetBSD Foundation, Inc.
@@ -68,7 +68,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: if_arp.c,v 1.301 2021/02/14 19:47:17 roy Exp $");
+__KERNEL_RCSID(0, "$NetBSD: if_arp.c,v 1.302 2021/02/14 20:58:35 christos Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_ddb.h"
@@ -712,7 +712,7 @@
                                goto badlen;
                }
                ar = mtod(m, struct arphdr *);
-               KASSERT(ARP_HDR_ALIGNED_P(ar));
+               KASSERT(POINTER_ALIGNED_P(ar, ARP_HDR_ALIGNMENT));
 
                rcvif = m_get_rcvif(m, &s);
                if (__predict_false(rcvif == NULL)) {
@@ -741,7 +741,7 @@
                        if ((m = m_pullup(m, arplen)) == NULL)
                                goto badlen;
                        ar = mtod(m, struct arphdr *);
-                       KASSERT(ARP_HDR_ALIGNED_P(ar));
+                       KASSERT(POINTER_ALIGNED_P(ar, ARP_HDR_ALIGNMENT));
                }
 
                switch (ntohs(ar->ar_pro)) {
diff -r d81380ebf99a -r acfcbad6a468 sys/netinet/igmp_var.h
--- a/sys/netinet/igmp_var.h    Sun Feb 14 20:30:31 2021 +0000
+++ b/sys/netinet/igmp_var.h    Sun Feb 14 20:58:34 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: igmp_var.h,v 1.25 2018/09/14 05:09:51 maxv Exp $       */
+/*     $NetBSD: igmp_var.h,v 1.26 2021/02/14 20:58:35 christos Exp $   */
 
 /*
  * Copyright (c) 1992, 1993
@@ -105,11 +105,7 @@
  */
 #define        IGMP_RANDOM_DELAY(X)    (cprng_fast32() % (X) + 1)
 
-#ifdef __NO_STRICT_ALIGNMENT
-#define        IGMP_HDR_ALIGNED_P(ig)  1
-#else
-#define        IGMP_HDR_ALIGNED_P(ig)  ((((vaddr_t) (ig)) & 3) == 0)
-#endif
+#define        IGMP_HDR_ALIGNMENT      3
 
 void   igmp_init(void);
 void   igmp_input(struct mbuf *, int, int);
diff -r d81380ebf99a -r acfcbad6a468 sys/netinet/in_l2tp.c
--- a/sys/netinet/in_l2tp.c     Sun Feb 14 20:30:31 2021 +0000
+++ b/sys/netinet/in_l2tp.c     Sun Feb 14 20:58:34 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: in_l2tp.c,v 1.18 2020/01/29 04:37:24 thorpej Exp $     */
+/*     $NetBSD: in_l2tp.c,v 1.19 2021/02/14 20:58:35 christos Exp $    */
 
 /*
  * Copyright (c) 2017 Internet Initiative Japan Inc.
@@ -27,7 +27,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: in_l2tp.c,v 1.18 2020/01/29 04:37:24 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: in_l2tp.c,v 1.19 2021/02/14 20:58:35 christos Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_l2tp.h"
@@ -197,13 +197,8 @@
                error = ENOBUFS;
                goto out;
        }
-       if (IP_HDR_ALIGNED_P(mtod(m, void *)) == 0) {
-               m = m_copyup(m, sizeof(struct ip), 0);
-       } else {
-               if (m->m_len < sizeof(struct ip))
-                       m = m_pullup(m, sizeof(struct ip));
-       }
-       if (m == NULL) {
+       if (m_get_aligned_hdr(&m, IP_HDR_ALIGNMENT, sizeof(iphdr), false) != 0)
+       {
                error = ENOBUFS;
                goto out;
        }
diff -r d81380ebf99a -r acfcbad6a468 sys/netinet/ip_flow.c
--- a/sys/netinet/ip_flow.c     Sun Feb 14 20:30:31 2021 +0000
+++ b/sys/netinet/ip_flow.c     Sun Feb 14 20:58:34 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: ip_flow.c,v 1.82 2018/04/11 08:29:19 maxv Exp $        */
+/*     $NetBSD: ip_flow.c,v 1.83 2021/02/14 20:58:35 christos Exp $    */
 
 /*-
  * Copyright (c) 1998 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: ip_flow.c,v 1.82 2018/04/11 08:29:19 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: ip_flow.c,v 1.83 2021/02/14 20:58:35 christos Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_net_mpsafe.h"
@@ -230,9 +230,8 @@
        /*
         * IP header with no option and valid version and length
         */
-       if (IP_HDR_ALIGNED_P(mtod(m, const void *)))
-               ip = mtod(m, struct ip *);
-       else {
+       ip = mtod(m, struct ip *);
+       if (!POINTER_ALIGNED_P(ip, IP_HDR_ALIGNMENT) {
                memcpy(&ip_store, mtod(m, const void *), sizeof(ip_store));
                ip = &ip_store;
        }
@@ -314,7 +313,7 @@
         *
         * XXX Use m_copyback_cow(9) here? --dyoung
         */
-       if (IP_HDR_ALIGNED_P(mtod(m, void *)) == 0)
+       if (!POINTER_ALIGNED_P(mtod(m, void *), IP_HDR_ALIGNMENT))
                memcpy(mtod(m, void *), &ip_store, sizeof(ip_store));
 
        /*
diff -r d81380ebf99a -r acfcbad6a468 sys/netinet/ip_input.c
--- a/sys/netinet/ip_input.c    Sun Feb 14 20:30:31 2021 +0000
+++ b/sys/netinet/ip_input.c    Sun Feb 14 20:58:34 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: ip_input.c,v 1.397 2020/08/28 06:31:42 ozaki-r Exp $   */
+/*     $NetBSD: ip_input.c,v 1.398 2021/02/14 20:58:35 christos Exp $  */
 
 /*
  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
@@ -91,7 +91,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: ip_input.c,v 1.397 2020/08/28 06:31:42 ozaki-r Exp $");
+__KERNEL_RCSID(0, "$NetBSD: ip_input.c,v 1.398 2021/02/14 20:58:35 christos Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_inet.h"
@@ -454,18 +454,10 @@
         * it.  Otherwise, if it is aligned, make sure the entire
         * base IP header is in the first mbuf of the chain.
         */
-       if (IP_HDR_ALIGNED_P(mtod(m, void *)) == 0) {
-               if ((m = m_copyup(m, sizeof(struct ip),
-                   (max_linkhdr + 3) & ~3)) == NULL) {
-                       /* XXXJRT new stat, please */
-                       IP_STATINC(IP_STAT_TOOSMALL);
-                       goto out;
-               }
-       } else if (__predict_false(m->m_len < sizeof(struct ip))) {
-               if ((m = m_pullup(m, sizeof(struct ip))) == NULL) {
-                       IP_STATINC(IP_STAT_TOOSMALL);
-                       goto out;
-               }
+       if (m_get_aligned_hdr(&m, IP_HDR_ALIGNMENT, sizeof(*ip), true) != 0) {
+               /* XXXJRT new stat, please */
+               IP_STATINC(IP_STAT_TOOSMALL);
+               goto out;
        }
        ip = mtod(m, struct ip *);
        if (ip->ip_v != IPVERSION) {
diff -r d81380ebf99a -r acfcbad6a468 sys/netinet/ip_private.h
--- a/sys/netinet/ip_private.h  Sun Feb 14 20:30:31 2021 +0000
+++ b/sys/netinet/ip_private.h  Sun Feb 14 20:58:34 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: ip_private.h,v 1.3 2008/04/28 20:24:09 martin Exp $    */
+/*     $NetBSD: ip_private.h,v 1.4 2021/02/14 20:58:35 christos Exp $  */
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -43,11 +43,7 @@
 #define        IP_STATINC(x)           _NET_STATINC(ipstat_percpu, x)
 #define        IP_STATDEC(x)           _NET_STATDEC(ipstat_percpu, x)
 
-#ifdef __NO_STRICT_ALIGNMENT
-#define        IP_HDR_ALIGNED_P(ip)    1
-#else
-#define        IP_HDR_ALIGNED_P(ip)    ((((vaddr_t) (ip)) & 3) == 0)
-#endif
+#define        IP_HDR_ALIGNMENT        3
 #endif /* _KERNEL */



Home | Main Index | Thread Index | Old Index