Module Name:    src
Committed By:   roy
Date:           Sun Feb 14 19:33:29 UTC 2021

Modified Files:
        src/sys/net: if_gre.c if_gre.h

Log Message:
if_gre: Remove alignment checks in favour copying to stack

Makes the code a lot simpler, idea from dyoung@


To generate a diff of this commit:
cvs rdiff -u -r1.179 -r1.180 src/sys/net/if_gre.c
cvs rdiff -u -r1.48 -r1.49 src/sys/net/if_gre.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/net/if_gre.c
diff -u src/sys/net/if_gre.c:1.179 src/sys/net/if_gre.c:1.180
--- src/sys/net/if_gre.c:1.179	Sat Feb 13 13:00:16 2021
+++ src/sys/net/if_gre.c	Sun Feb 14 19:33:29 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: if_gre.c,v 1.179 2021/02/13 13:00:16 roy Exp $ */
+/*	$NetBSD: if_gre.c,v 1.180 2021/02/14 19:33:29 roy Exp $ */
 
 /*
  * Copyright (c) 1998, 2008 The NetBSD Foundation, Inc.
@@ -45,7 +45,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: if_gre.c,v 1.179 2021/02/13 13:00:16 roy Exp $");
+__KERNEL_RCSID(0, "$NetBSD: if_gre.c,v 1.180 2021/02/14 19:33:29 roy Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_atalk.h"
@@ -142,6 +142,8 @@ int gre_debug = 0;
 #define	GRE_DPRINTF(__sc, __fmt, ...)	do { } while (/*CONSTCOND*/0)
 #endif /* GRE_DEBUG */
 
+CTASSERT(sizeof(struct gre_h) == 4);
+
 int ip_gre_ttl = GRE_TTL;
 
 static u_int gre_count;
@@ -374,7 +376,7 @@ gre_receive(struct socket *so, void *arg
 {
 	struct gre_softc *sc = (struct gre_softc *)arg;
 	int rc;
-	const struct gre_h *gh;
+	struct gre_h gh;
 	struct mbuf *m;
 
 	GRE_DPRINTF(sc, "enter\n");
@@ -396,24 +398,16 @@ gre_receive(struct socket *so, void *arg
 		return;
 	}
 
-	/* Enforce alignment */
-	if (GRE_HDR_ALIGNED_P(mtod(m, void *)) == 0) {
-		if ((m = m_copyup(m, sizeof(struct gre_h), 0)) == NULL) {
-			/* XXXJRT new stat, please */
-			GRE_DPRINTF(sc, "m_copyup failed\n");
-			sc->sc_pullup_ev.ev_count++;
-			return;
-		}
-	} else if (__predict_false(m->m_len < sizeof(struct gre_h))) {
-		if ((m = m_pullup(m, sizeof(struct gre_h))) == NULL) {
+	if (__predict_false(m->m_len < sizeof(gh))) {
+		if ((m = m_pullup(m, sizeof(gh))) == NULL) {
 			GRE_DPRINTF(sc, "m_pullup failed\n");
 			sc->sc_pullup_ev.ev_count++;
 			return;
 		}
 	}
-	gh = mtod(m, const struct gre_h *);
+	memcpy(&gh, mtod(m, void *), sizeof(gh));
 
-	if (gre_input(sc, m, gh) == 0) {
+	if (gre_input(sc, m, &gh) == 0) {
 		sc->sc_unsupp_ev.ev_count++;
 		GRE_DPRINTF(sc, "dropping unsupported\n");
 		m_freem(m);
@@ -898,7 +892,7 @@ gre_output(struct ifnet *ifp, struct mbu
 {
 	int error = 0;
 	struct gre_softc *sc = ifp->if_softc;
-	struct gre_h *gh;
+	struct gre_h gh = { .flags = 0 };
 	uint16_t etype = 0;
 
 	KASSERT((m->m_flags & M_PKTHDR) != 0);
@@ -950,17 +944,15 @@ gre_output(struct ifnet *ifp, struct mbu
 	}
 #endif
 
-	M_PREPEND(m, sizeof(*gh), M_DONTWAIT);
+	M_PREPEND(m, sizeof(gh), M_DONTWAIT);
 	if (m == NULL) {
 		IF_DROP(&ifp->if_snd);
 		error = ENOBUFS;
 		goto end;
 	}
 
-	gh = mtod(m, struct gre_h *);
-	KASSERT(GRE_HDR_ALIGNED_P(gh));
-	gh->flags = 0;
-	gh->ptype = etype;
+	gh.ptype = etype;
+	memcpy(mtod(m, void *), &gh, sizeof(gh));
 	/* XXX Need to handle IP ToS.  Look at how I handle IP TTL. */
 
 	if_statadd2(ifp, if_opackets, 1, if_obytes, m->m_pkthdr.len);

Index: src/sys/net/if_gre.h
diff -u src/sys/net/if_gre.h:1.48 src/sys/net/if_gre.h:1.49
--- src/sys/net/if_gre.h:1.48	Fri Feb 12 19:57:49 2021
+++ src/sys/net/if_gre.h	Sun Feb 14 19:33:29 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: if_gre.h,v 1.48 2021/02/12 19:57:49 roy Exp $ */
+/*	$NetBSD: if_gre.h,v 1.49 2021/02/14 19:33:29 roy Exp $ */
 
 /*
  * Copyright (c) 1998, 2008 The NetBSD Foundation, Inc.
@@ -131,15 +131,6 @@ struct gre_h {
 				Present if (rt_pres == 1)
  */
 };
-#ifdef __NO_STRICT_ALIGNMENT
-#define	GRE_HDR_ALIGNED_P(gh)	1
-#else
-#define	GRE_HDR_ALIGNED_P(gh)	((((vaddr_t) (gh)) & 3) == 0)
-#endif
-#ifdef __CTASSERT
-__CTASSERT(sizeof(struct gre_h) == 4);
-#endif
-
 #define GRE_CP		0x8000  /* Checksum Present */
 #define GRE_RP		0x4000  /* Routing Present */
 #define GRE_KP		0x2000  /* Key Present */

Reply via email to