Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 17 Apr 2019 23:09:06 +0000 (UTC)
From:      Eric Joyner <erj@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-12@freebsd.org
Subject:   svn commit: r346338 - in stable/12: share/man/man9 sys/net sys/sys
Message-ID:  <201904172309.x3HN96Cv077258@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: erj
Date: Wed Apr 17 23:09:06 2019
New Revision: 346338
URL: https://svnweb.freebsd.org/changeset/base/346338

Log:
  MFC r345312
  
  iflib: mark isc_driver_version as constant
  
  From Jake:
  The iflib core never modifies the isc_driver_version string. Allow
  drivers to safely assign pointers to constant buffers by marking this
  parameter const.
  
  (Additional comment by erj: This also adds a new sysctl_add_oid macro,
  SYSCTL_ADD_CONST_STRING, for displaying const strings).
  
  Submitted by:   Jacob Keller <jacob.e.keller@intel.com>
  Reviewed by:    erj@, gallatin@, jhb@
  Sponsored by:   Intel Corporation
  Differential Revision:  https://reviews.freebsd.org/D19577

Modified:
  stable/12/share/man/man9/sysctl.9
  stable/12/sys/net/iflib.c
  stable/12/sys/net/iflib.h
  stable/12/sys/sys/sysctl.h
Directory Properties:
  stable/12/   (props changed)

Modified: stable/12/share/man/man9/sysctl.9
==============================================================================
--- stable/12/share/man/man9/sysctl.9	Wed Apr 17 23:02:37 2019	(r346337)
+++ stable/12/share/man/man9/sysctl.9	Wed Apr 17 23:09:06 2019	(r346338)
@@ -43,6 +43,7 @@
 .Nm SYSCTL_ADD_S32 ,
 .Nm SYSCTL_ADD_S64 ,
 .Nm SYSCTL_ADD_STRING ,
+.Nm SYSCTL_ADD_CONST_STRING ,
 .Nm SYSCTL_ADD_STRUCT ,
 .Nm SYSCTL_ADD_U8 ,
 .Nm SYSCTL_ADD_U16 ,
@@ -70,6 +71,7 @@
 .Nm SYSCTL_S32 ,
 .Nm SYSCTL_S64 ,
 .Nm SYSCTL_STRING ,
+.Nm SYSCTL_CONST_STRING ,
 .Nm SYSCTL_STRUCT ,
 .Nm SYSCTL_U8 ,
 .Nm SYSCTL_U16 ,
@@ -225,6 +227,16 @@
 .Fa "const char *descr"
 .Fc
 .Ft struct sysctl_oid *
+.Fo SYSCTL_ADD_CONST_STRING
+.Fa "struct sysctl_ctx_list *ctx"
+.Fa "struct sysctl_oid_list *parent"
+.Fa "int number"
+.Fa "const char *name"
+.Fa "int ctlflags"
+.Fa "const char *ptr"
+.Fa "const char *descr"
+.Fc
+.Ft struct sysctl_oid *
 .Fo SYSCTL_ADD_STRUCT
 .Fa "struct sysctl_ctx_list *ctx"
 .Fa "struct sysctl_oid_list *parent"
@@ -351,6 +363,7 @@
 .Fn SYSCTL_S32 parent number name ctlflags ptr val descr
 .Fn SYSCTL_S64 parent number name ctlflags ptr val descr
 .Fn SYSCTL_STRING parent number name ctlflags arg len descr
+.Fn SYSCTL_CONST_STRING parent number name ctlflags arg descr
 .Fn SYSCTL_STRUCT parent number name ctlflags ptr struct_type descr
 .Fn SYSCTL_U8 parent number name ctlflags ptr val descr
 .Fn SYSCTL_U16 parent number name ctlflags ptr val descr
@@ -512,6 +525,11 @@ If the
 .Fa len
 argument in zero, the string length is computed at every access to the OID using
 .Xr strlen 3 .
+Use the
+.Fn SYSCTL_CONST_STRING
+macro or the
+.Fn SYSCTL_ADD_CONST_STRING
+function to add a sysctl for a constant string.
 .Sh CREATING OPAQUE SYSCTLS
 The
 .Fn SYSCTL_OPAQUE
@@ -558,6 +576,7 @@ Static sysctls are declared using one of the
 .Fn SYSCTL_S32 ,
 .Fn SYSCTL_S64 ,
 .Fn SYSCTL_STRING ,
+.Fn SYSCTL_CONST_STRING ,
 .Fn SYSCTL_STRUCT ,
 .Fn SYSCTL_U8 ,
 .Fn SYSCTL_U16 ,
@@ -583,6 +602,7 @@ Dynamic nodes are created using one of the
 .Fn SYSCTL_ADD_S32 ,
 .Fn SYSCTL_ADD_S64 ,
 .Fn SYSCTL_ADD_STRING ,
+.Fn SYSCTL_ADD_CONST_STRING ,
 .Fn SYSCTL_ADD_STRUCT ,
 .Fn SYSCTL_ADD_U8 ,
 .Fn SYSCTL_ADD_U16 ,

Modified: stable/12/sys/net/iflib.c
==============================================================================
--- stable/12/sys/net/iflib.c	Wed Apr 17 23:02:37 2019	(r346337)
+++ stable/12/sys/net/iflib.c	Wed Apr 17 23:09:06 2019	(r346338)
@@ -6244,8 +6244,8 @@ iflib_add_device_sysctl_pre(if_ctx_t ctx)
 						      CTLFLAG_RD, NULL, "IFLIB fields");
 	oid_list = SYSCTL_CHILDREN(node);
 
-	SYSCTL_ADD_STRING(ctx_list, oid_list, OID_AUTO, "driver_version",
-		       CTLFLAG_RD, ctx->ifc_sctx->isc_driver_version, 0,
+	SYSCTL_ADD_CONST_STRING(ctx_list, oid_list, OID_AUTO, "driver_version",
+		       CTLFLAG_RD, ctx->ifc_sctx->isc_driver_version,
 		       "driver version");
 
 	SYSCTL_ADD_U16(ctx_list, oid_list, OID_AUTO, "override_ntxqs",

Modified: stable/12/sys/net/iflib.h
==============================================================================
--- stable/12/sys/net/iflib.h	Wed Apr 17 23:02:37 2019	(r346337)
+++ stable/12/sys/net/iflib.h	Wed Apr 17 23:09:06 2019	(r346338)
@@ -248,7 +248,7 @@ struct if_shared_ctx {
 
 	/* fields necessary for probe */
 	pci_vendor_info_t *isc_vendor_info;
-	char *isc_driver_version;
+	const char *isc_driver_version;
 	/* optional function to transform the read values to match the table*/
 	void (*isc_parse_devinfo) (uint16_t *device_id, uint16_t *subvendor_id,
 				   uint16_t *subdevice_id, uint16_t *rev_id);

Modified: stable/12/sys/sys/sysctl.h
==============================================================================
--- stable/12/sys/sys/sysctl.h	Wed Apr 17 23:02:37 2019	(r346337)
+++ stable/12/sys/sys/sysctl.h	Wed Apr 17 23:09:06 2019	(r346338)
@@ -351,6 +351,25 @@ TAILQ_HEAD(sysctl_ctx_list, sysctl_ctx_entry);
 	    NULL); \
 })
 
+/* Oid for a constant '\0' terminated string. */
+#define	SYSCTL_CONST_STRING(parent, nbr, name, access, arg, descr)	\
+	SYSCTL_OID(parent, nbr, name, CTLTYPE_STRING|(access),		\
+	    __DECONST(char *, arg), 0, sysctl_handle_string, "A", descr); \
+	CTASSERT(!(access & CTLFLAG_WR));				\
+	CTASSERT(((access) & CTLTYPE) == 0 ||				\
+	    ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_STRING)
+
+#define	SYSCTL_ADD_CONST_STRING(ctx, parent, nbr, name, access, arg, descr) \
+({									\
+	char *__arg = __DECONST(char *, arg);				\
+	CTASSERT(!(access & CTLFLAG_WR));				\
+	CTASSERT(((access) & CTLTYPE) == 0 ||				\
+	    ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_STRING);	\
+	sysctl_add_oid(ctx, parent, nbr, name, CTLTYPE_STRING|(access),	\
+	    __arg, 0, sysctl_handle_string, "A", __DESCR(descr),	\
+	    NULL); \
+})
+
 /* Oid for a bool.  If ptr is NULL, val is returned. */
 #define	SYSCTL_NULL_BOOL_PTR ((bool *)NULL)
 #define	SYSCTL_BOOL(parent, nbr, name, access, ptr, val, descr)	\



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201904172309.x3HN96Cv077258>