svn commit: r256300 - in user/andre/mbuf_staging: dev/xen/netback kern sys

Andre Oppermann andre at FreeBSD.org
Thu Oct 10 21:14:50 UTC 2013


Author: andre
Date: Thu Oct 10 21:14:49 2013
New Revision: 256300
URL: http://svnweb.freebsd.org/changeset/base/256300

Log:
  Merge all mbuf related CTASSERTs into kern/kern_mbuf.c.
  
  Internalize and functionalize a couple of macros that need to have
  inside knowledge of the mbuf structure into kern/uipc_mbuf.c.  The
  conversion is very much mechanical for now and the function names
  are not finalized at all.
  
  Affected are M_WRITABLE, M_ALIGN, MH_ALIGN, MEXT_ALIGN, M_LEADINGSPACE,
  M_TRAILINGSPACE, M_PREPEND.
  
  There is some fallout in dev/xen/netback/netback.c which has const'ed
  the mbuf pointer to handled properly later.

Modified:
  user/andre/mbuf_staging/dev/xen/netback/netback.c
  user/andre/mbuf_staging/kern/kern_mbuf.c
  user/andre/mbuf_staging/kern/uipc_mbuf.c
  user/andre/mbuf_staging/sys/mbuf.h

Modified: user/andre/mbuf_staging/dev/xen/netback/netback.c
==============================================================================
--- user/andre/mbuf_staging/dev/xen/netback/netback.c	Thu Oct 10 21:10:51 2013	(r256299)
+++ user/andre/mbuf_staging/dev/xen/netback/netback.c	Thu Oct 10 21:14:49 2013	(r256300)
@@ -1706,7 +1706,7 @@ xnb_txpkt2gnttab(const struct xnb_pkt *p
 
 	while (size_remaining > 0) {
 		const netif_tx_request_t *txq = RING_GET_REQUEST(txb, r_idx);
-		const size_t mbuf_space = M_TRAILINGSPACE(mbuf) - m_ofs;
+		const size_t mbuf_space = M_TRAILINGSPACE(__DECONST(struct mbuf *, mbuf)) - m_ofs;
 		const size_t req_size =
 			r_idx == pkt->car ? pkt->car_size : txq->size;
 		const size_t pkt_space = req_size - r_ofs;
@@ -1739,7 +1739,7 @@ xnb_txpkt2gnttab(const struct xnb_pkt *p
 			r_ofs = 0;
 			r_idx = (r_idx == pkt->car) ? pkt->cdr : r_idx + 1;
 		}
-		if (M_TRAILINGSPACE(mbuf) - m_ofs <= 0) {
+		if (M_TRAILINGSPACE(__DECONST(struct mbuf *, mbuf)) - m_ofs <= 0) {
 			/* Must move to the next mbuf */
 			m_ofs = 0;
 			mbuf = mbuf->m_next;

Modified: user/andre/mbuf_staging/kern/kern_mbuf.c
==============================================================================
--- user/andre/mbuf_staging/kern/kern_mbuf.c	Thu Oct 10 21:10:51 2013	(r256299)
+++ user/andre/mbuf_staging/kern/kern_mbuf.c	Thu Oct 10 21:14:49 2013	(r256300)
@@ -122,6 +122,17 @@ SYSCTL_QUAD(_kern_ipc, OID_AUTO, maxmbuf
 static uma_zone_t	m_getzone(int);
 
 /*
+ * Ensure the correct size of various mbuf parameters.  It could be off due
+ * to compiler-induced padding and alignment artifacts.
+ */
+CTASSERT(sizeof(struct mbuf) == MSIZE);
+CTASSERT(MSIZE - offsetof(struct mbuf, m_dat) == MLEN);
+CTASSERT(MSIZE - offsetof(struct mbuf, m_pktdat) == MHLEN);
+
+/* Ensure that MSIZE is a power of 2. */
+CTASSERT((((MSIZE - 1) ^ MSIZE) + 1) >> 1 == MSIZE);
+
+/*
  * tunable_mbinit() has to be run before any mbuf allocations are done.
  */
 static void
@@ -288,9 +299,6 @@ static void	mb_zfini_pack(void *, int);
 static void	mb_reclaim(void *);
 static void    *mbuf_jumbo_alloc(uma_zone_t, int, uint8_t *, int);
 
-/* Ensure that MSIZE is a power of 2. */
-CTASSERT((((MSIZE - 1) ^ MSIZE) + 1) >> 1 == MSIZE);
-
 /*
  * Initialize FreeBSD Network buffer allocation.
  */

Modified: user/andre/mbuf_staging/kern/uipc_mbuf.c
==============================================================================
--- user/andre/mbuf_staging/kern/uipc_mbuf.c	Thu Oct 10 21:10:51 2013	(r256299)
+++ user/andre/mbuf_staging/kern/uipc_mbuf.c	Thu Oct 10 21:14:49 2013	(r256300)
@@ -85,14 +85,6 @@ SYSCTL_INT(_kern_ipc, OID_AUTO, m_defrag
 #endif
 
 /*
- * Ensure the correct size of various mbuf parameters.  It could be off due
- * to compiler-induced padding and alignment artifacts.
- */
-CTASSERT(sizeof(struct mbuf) == MSIZE);
-CTASSERT(MSIZE - offsetof(struct mbuf, m_dat) == MLEN);
-CTASSERT(MSIZE - offsetof(struct mbuf, m_pktdat) == MHLEN);
-
-/*
  * Attach the cluster from *m to *n, set up m_ext in *n
  * and bump the refcount of the cluster.
  */
@@ -1766,6 +1758,82 @@ m_unshare(struct mbuf *m0, int how)
 	return (m0);
 }
 
+int
+_m_writable(struct mbuf *m)
+{
+
+	return (!((m)->m_flags & M_RDONLY) &&
+	    (!(((m)->m_flags & M_EXT)) || (*((m)->m_ext.ref_cnt) == 1)) );
+}
+
+void
+_m_align(struct mbuf *m, int len)
+{
+	KASSERT(!((m)->m_flags & (M_PKTHDR|M_EXT)),
+		("%s: M_ALIGN not normal mbuf", __func__));
+	KASSERT((m)->m_data == (m)->m_dat,
+		("%s: M_ALIGN not a virgin mbuf", __func__));
+	(m)->m_data += (MLEN - (len)) & ~(sizeof(long) - 1);
+}
+
+void
+_mh_align(struct mbuf *m, int len)
+{
+	KASSERT((m)->m_flags & M_PKTHDR && !((m)->m_flags & M_EXT),
+		("%s: MH_ALIGN not PKTHDR mbuf", __func__));
+	KASSERT((m)->m_data == (m)->m_pktdat,
+		("%s: MH_ALIGN not a virgin mbuf", __func__));
+	(m)->m_data += (MHLEN - (len)) & ~(sizeof(long) - 1);
+}
+
+void
+_mext_align(struct mbuf *m, int len)
+{
+	KASSERT((m)->m_flags & M_EXT,
+		("%s: MEXT_ALIGN not an M_EXT mbuf", __func__));
+	KASSERT((m)->m_data == (m)->m_ext.ext_buf,
+		("%s: MEXT_ALIGN not a virgin mbuf", __func__));
+	(m)->m_data += ((m)->m_ext.ext_size - (len)) &
+	    ~(sizeof(long) - 1);
+}
+
+int
+_m_leadingspace(struct mbuf *m)
+{
+	return ((m)->m_flags & M_EXT ?
+	    (M_WRITABLE(m) ? (m)->m_data - (m)->m_ext.ext_buf : 0):
+	    (m)->m_flags & M_PKTHDR ? (m)->m_data - (m)->m_pktdat :
+	    (m)->m_data - (m)->m_dat);
+}
+
+int
+_m_trailingspace(struct mbuf *m)
+{
+	return ((m)->m_flags & M_EXT ?
+	    (M_WRITABLE(m) ? (m)->m_ext.ext_buf + (m)->m_ext.ext_size
+		- ((m)->m_data + (m)->m_len) : 0) :
+	    &(m)->m_dat[MLEN] - ((m)->m_data + (m)->m_len));
+}
+
+void
+_m_prepend(struct mbuf *m, int plen, int how)
+{
+	struct mbuf **_mmp = &(m);
+	struct mbuf *_mm = *_mmp;
+	int _mplen = (plen);
+	int __mhow = (how);
+
+	MBUF_CHECKSLEEP(how);
+	if (M_LEADINGSPACE(_mm) >= _mplen) {
+		_mm->m_data -= _mplen;
+		_mm->m_len += _mplen;
+	} else
+		_mm = m_prepend(_mm, _mplen, __mhow);
+	if (_mm != NULL && _mm->m_flags & M_PKTHDR)
+		_mm->m_pkthdr.len += _mplen;
+	*_mmp = _mm;
+}
+
 #ifdef MBUF_PROFILING
 
 #define MP_BUCKETS 32 /* don't just change this as things may overflow.*/

Modified: user/andre/mbuf_staging/sys/mbuf.h
==============================================================================
--- user/andre/mbuf_staging/sys/mbuf.h	Thu Oct 10 21:10:51 2013	(r256299)
+++ user/andre/mbuf_staging/sys/mbuf.h	Thu Oct 10 21:14:49 2013	(r256300)
@@ -560,9 +560,8 @@ m_last(struct mbuf *m)
  * be both the local data payload, or an external buffer area, depending on
  * whether M_EXT is set).
  */
-#define	M_WRITABLE(m)	(!((m)->m_flags & M_RDONLY) &&			\
-			 (!(((m)->m_flags & M_EXT)) ||			\
-			 (*((m)->m_ext.ref_cnt) == 1)) )		\
+int	_m_writable(struct mbuf *);
+#define	M_WRITABLE(m)	_m_writable(m)
 
 /* Check if the supplied mbuf has a packet header, or else panic. */
 #define	M_ASSERTPKTHDR(m)						\
@@ -582,37 +581,21 @@ m_last(struct mbuf *m)
  * Set the m_data pointer of a newly-allocated mbuf (m_get/MGET) to place an
  * object of the specified size at the end of the mbuf, longword aligned.
  */
-#define	M_ALIGN(m, len) do {						\
-	KASSERT(!((m)->m_flags & (M_PKTHDR|M_EXT)),			\
-		("%s: M_ALIGN not normal mbuf", __func__));		\
-	KASSERT((m)->m_data == (m)->m_dat,				\
-		("%s: M_ALIGN not a virgin mbuf", __func__));		\
-	(m)->m_data += (MLEN - (len)) & ~(sizeof(long) - 1);		\
-} while (0)
+void	_m_align(struct mbuf *, int);
+#define	M_ALIGN(m, len)	_m_align(m, len)
 
 /*
  * As above, for mbufs allocated with m_gethdr/MGETHDR or initialized by
  * M_DUP/MOVE_PKTHDR.
  */
-#define	MH_ALIGN(m, len) do {						\
-	KASSERT((m)->m_flags & M_PKTHDR && !((m)->m_flags & M_EXT),	\
-		("%s: MH_ALIGN not PKTHDR mbuf", __func__));		\
-	KASSERT((m)->m_data == (m)->m_pktdat,				\
-		("%s: MH_ALIGN not a virgin mbuf", __func__));		\
-	(m)->m_data += (MHLEN - (len)) & ~(sizeof(long) - 1);		\
-} while (0)
+void	_mh_align(struct mbuf *, int);
+#define	MH_ALIGN(m, len)	_mh_align(m, len)
 
 /*
  * As above, for mbuf with external storage.
  */
-#define	MEXT_ALIGN(m, len) do {						\
-	KASSERT((m)->m_flags & M_EXT,					\
-		("%s: MEXT_ALIGN not an M_EXT mbuf", __func__));	\
-	KASSERT((m)->m_data == (m)->m_ext.ext_buf,			\
-		("%s: MEXT_ALIGN not a virgin mbuf", __func__));	\
-	(m)->m_data += ((m)->m_ext.ext_size - (len)) &			\
-	    ~(sizeof(long) - 1); 					\
-} while (0)
+void	_mext_align(struct mbuf *, int);
+#define	MEXT_ALIGN(m, len)	_mext_align(m, len)
 
 /*
  * Compute the amount of space available before the current start of data in
@@ -621,11 +604,8 @@ m_last(struct mbuf *m)
  * The M_WRITABLE() is a temporary, conservative safety measure: the burden
  * of checking writability of the mbuf data area rests solely with the caller.
  */
-#define	M_LEADINGSPACE(m)						\
-	((m)->m_flags & M_EXT ?						\
-	    (M_WRITABLE(m) ? (m)->m_data - (m)->m_ext.ext_buf : 0):	\
-	    (m)->m_flags & M_PKTHDR ? (m)->m_data - (m)->m_pktdat :	\
-	    (m)->m_data - (m)->m_dat)
+int	_m_leadingspace(struct mbuf *);
+#define	M_LEADINGSPACE(m)	_m_leadingspace(m)
 
 /*
  * Compute the amount of space available after the end of data in an mbuf.
@@ -633,33 +613,16 @@ m_last(struct mbuf *m)
  * The M_WRITABLE() is a temporary, conservative safety measure: the burden
  * of checking writability of the mbuf data area rests solely with the caller.
  */
-#define	M_TRAILINGSPACE(m)						\
-	((m)->m_flags & M_EXT ?						\
-	    (M_WRITABLE(m) ? (m)->m_ext.ext_buf + (m)->m_ext.ext_size	\
-		- ((m)->m_data + (m)->m_len) : 0) :			\
-	    &(m)->m_dat[MLEN] - ((m)->m_data + (m)->m_len))
+int	_m_trailingspace(struct mbuf *);
+#define	M_TRAILINGSPACE(m)	_m_trailingspace(m)
 
 /*
  * Arrange to prepend space of size plen to mbuf m.  If a new mbuf must be
  * allocated, how specifies whether to wait.  If the allocation fails, the
  * original mbuf chain is freed and m is set to NULL.
  */
-#define	M_PREPEND(m, plen, how) do {					\
-	struct mbuf **_mmp = &(m);					\
-	struct mbuf *_mm = *_mmp;					\
-	int _mplen = (plen);						\
-	int __mhow = (how);						\
-									\
-	MBUF_CHECKSLEEP(how);						\
-	if (M_LEADINGSPACE(_mm) >= _mplen) {				\
-		_mm->m_data -= _mplen;					\
-		_mm->m_len += _mplen;					\
-	} else								\
-		_mm = m_prepend(_mm, _mplen, __mhow);			\
-	if (_mm != NULL && _mm->m_flags & M_PKTHDR)			\
-		_mm->m_pkthdr.len += _mplen;				\
-	*_mmp = _mm;							\
-} while (0)
+void	_m_prepend(struct mbuf *, int, int);
+#define	M_PREPEND(m, plen, how)	_m_prepend(m, plen, how)
 
 /*
  * Change mbuf to new type.  This is a relatively expensive operation and


More information about the svn-src-user mailing list