Skip to contentSkip to conversation

multipath-tools: improve config file handling

PATCH In review Martin Wilck 2 participants 34 messages Jun 8, 2018 – Jun 18, 2018 Device mapper

lore
Share & tools
lore
#0 lore raw

cover multipath-tools: improve config file handling

This patch series fixes a number of small but important issues in the way multipath-tools handle configuration files, in particular the handling of multiple "device" and "blacklist" sections matching the same device. Besides eliminating inconsistencies, the goal of the series is to be able to use "multipath -t" or "multipathd show config" ouput as configuration for multipath-tools, and obtain exactly the same results as with the original configuration.

Late in the series, two new commands "multipath -T" and "multipath show config local" are added, which avoid the lengthyness of the full configuration dump and produce output more suitable as a local configuration template.

By far the largest part of the series is a new unit test (hwtable.c) for configuration file handling. The commit message of the patch introducing this unit test ("tests/hwtable: tests for config file handling and hwentry merging") explains the problems / inconsistencies that the series is supposed to fix. Tests that fail in the first place are marked with the BROKEN macro. While reviewing the 2000 LOC of the new unit test is certainly a pain, I'd like to ask reviewers to *pay special attention to the tests marked BROKEN*, because that's where follow-up patches will change the behavior of multipath-tools in user-noticeable ways. I believe it's for the better, but I'd like to make sure we all agree. For the rest of the unit test, reviewers might appreciate the fact that tests succeed for the current code, and are intended as regeression tests for the follow-up patches.

Patch 01-07 are just simple fixes and, as usual, "const" additions. Patch 08-10 introduce the new unit test.
Patch 11-17 fix some basic problems which need to be fixed for dump/reload. Patch 18-24 add the ability for dumping the configuration, using the output as new configuration input ("reload"), and testing / comparing the results. Patch 23-24 implement "multipathd show config local" and "multipath -T" on these grounds.
Patch 23-27 fix more problems that surfaced in the dump/reload test. Patch 28 clarifies the documentation of multipath.conf.

Martin Wilck (28):
kpartx: no need to use FREE_CONST
libmultipath: fix memory leak in process_config_dir()
libmultipath: remove superfluous conditionals in load_config() libmultipath/structs.c: constify some functions
libmultipath: some const usage in hwentry handling
libmultipath: change prototypes of hwe_regmatch() and find_hwe() libmultipath/prio: constify simple getters tests/Makefile: autogenerate list of symbols to be wrapped tests/test-lib: cmocka helpers to simulate path and map discovery tests/hwtable: tests for config file handling and hwentry merging
libmultipath: add debug messages to hwentry lookup/merging code
libmultipath: use vector for for pp->hwe and mp->hwe
libmultipath: allow more than one hwentry
libmultipath: don't merge hwentries by regex
libmultipath: merge hwentries inside a conf file libmultipath/hwtable: remove inherited props from ONTAP NVMe
libmultipath: don't merge by regex in setup_default_blist() multipath, multipathd: consolidate config dumping tests/hwtable: implement configuration dump + reload test
libmultipath: allow dumping only "local" hwtable in snprint_config tests/hwtable: add test for local configuration dump
libmultipath: allow printing local maps in snprint_config
multipathd: implement "show config local"
multipath: implement "multipath -T"
libmultipath: merge "multipath" config sections by wwid
libmultipath: implement and use blacklist merging
libmultipath: escape '"' chars while dumping config multipath.conf(5): various corrections and clarifications

 kpartx/devmapper.c         |   10 +-
 libmultipath/blacklist.c   |  125 ++-
 libmultipath/blacklist.h   |    2 +
 libmultipath/config.c      |  208 +++--
 libmultipath/config.h      |    5 +-
 libmultipath/dict.c        |   40 +-
 libmultipath/discovery.c   |   10 +-
 libmultipath/hwtable.c     |    3 -
 libmultipath/print.c       |  128 ++-
 libmultipath/print.h       |    9 +-
 libmultipath/prio.c        |    8 +-
 libmultipath/prio.h        |    8 +-
 libmultipath/propsel.c     |   53 +-
 libmultipath/structs.c     |   26 +-
 libmultipath/structs.h     |   24 +-
 libmultipath/structs_vec.c |   19 +
 libmultipath/structs_vec.h |    1 +
 libmultipath/vector.c      |   12 +
 libmultipath/vector.h      |    1 +
 multipath/main.c           |   92 +-
 multipath/multipath.8      |    8 +-
 multipath/multipath.conf.5 |  174 ++--
 multipathd/cli.c           |    2 +
 multipathd/cli.h           |    2 +
 multipathd/cli_handlers.c  |   80 +-
 multipathd/cli_handlers.h  |    1 +
 multipathd/main.c          |    1 +
 multipathd/multipathd.8    |    5 +
 tests/Makefile             |   36 +-
 tests/hwtable.c            | 1707 ++++++++++++++++++++++++++++++++++++
 tests/test-lib.c           |  359 ++++++++
 tests/test-lib.h           |   67 ++
 32 files changed, 2892 insertions(+), 334 deletions(-)
 create mode 100644 tests/hwtable.c
 create mode 100644 tests/test-lib.c
 create mode 100644 tests/test-lib.h
Signature · 1 line
2.17.0
#1 lore raw

PATCH 1/28 kpartx: no need to use FREE_CONST

A function that returns a result of strdup() doesn't need to declare that result as const char*.

---
 kpartx/devmapper.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
kpartx/devmapper.c +5 -5
index 8f68a246..f94d70e7 100644
@@ -11,7 +11,6 @@
 #include <sys/sysmacros.h>
 #include "devmapper.h"
 
-#define FREE_CONST(p) do { free((void*)(long)p); p = NULL; } while(0)
 #define _UUID_PREFIX "part"
 #define UUID_PREFIX _UUID_PREFIX "%d-"
 #define _UUID_PREFIX_LEN (sizeof(_UUID_PREFIX) - 1)
@@ -252,10 +251,11 @@ out:
 	return r;
 }
 
-static const char *dm_find_uuid(const char *uuid)
+static char *dm_find_uuid(const char *uuid)
 {
 	struct dm_task *dmt;
-	const char *name = NULL, *tmp;
+	char *name = NULL;
+	const char *tmp;
 
 	if ((dmt = dm_task_create(DM_DEVICE_INFO)) == NULL)
 		return NULL;
@@ -642,7 +642,7 @@ int dm_find_part(const char *parent, const char *delim, int part,
 {
 	int r;
 	char params[PARAMS_SIZE];
-	const char *tmp;
+	char *tmp;
 	char *uuid;
 	int major, minor;
 	char dev_t[32];
@@ -696,7 +696,7 @@ int dm_find_part(const char *parent, const char *delim, int part,
 	} else
 		*part_uuid = uuid;
 out:
-	FREE_CONST(tmp);
+	free(tmp);
 	return r;
 }
 
Signature · 1 line
2.17.0
#2 lore raw

PATCH 2/28 libmultipath: fix memory leak in process_config_dir()

In reply to Martin Wilck
---
 libmultipath/config.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
libmultipath/config.c +9 -1
index 085a3e12..8769441c 100644
@@ -551,6 +551,11 @@ free_config (struct config * conf)
 	FREE(conf);
 }
 
+static void free_namelist(void *nl)
+{
+	free(nl);
+}
+
 /* if multipath fails to process the config directory, it should continue,
  * with just a warning message */
 static void
@@ -574,7 +579,9 @@ process_config_dir(struct config *conf, vector keywords, char *dir)
 			condlog(0, "couldn't open configuration dir '%s': %s",
 				dir, strerror(errno));
 		return;
-	}
+	} else if (n == 0)
+		return;
+	pthread_cleanup_push(free_namelist, namelist);
 	for (i = 0; i < n; i++) {
 		if (!strstr(namelist[i]->d_name, ".conf"))
 			continue;
@@ -586,6 +593,7 @@ process_config_dir(struct config *conf, vector keywords, char *dir)
 			factorize_hwtable(conf->hwtable, old_hwtable_size);
 
 	}
+	pthread_cleanup_pop(1);
 }
 
 struct config *
Signature · 1 line
2.17.0
#3 lore raw

PATCH 3/28 libmultipath: remove superfluous conditionals in load_config()

In reply to Martin Wilck

In load_config(), conf is freshly allocated, there's no point in checking previously set values.

---
 libmultipath/config.c | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
libmultipath/config.c +3 -7
index 8769441c..ce7bedac 100644
@@ -607,8 +607,7 @@ load_config (char * file)
 	/*
 	 * internal defaults
 	 */
-	if (!conf->verbosity)
-		conf->verbosity = DEFAULT_VERBOSITY;
+	conf->verbosity = DEFAULT_VERBOSITY;
 
 	get_sys_max_fds(&conf->max_fds);
 	conf->bindings_file = set_default(DEFAULT_BINDINGS_FILE);
@@ -634,12 +633,9 @@ load_config (char * file)
 	/*
 	 * preload default hwtable
 	 */
-	if (conf->hwtable == NULL) {
-		conf->hwtable = vector_alloc();
-
-		if (!conf->hwtable)
+	conf->hwtable = vector_alloc();
+	if (!conf->hwtable)
 			goto out;
-	}
 	if (setup_default_hwtable(conf->hwtable))
 		goto out;
 
Signature · 1 line
2.17.0
#4 lore raw

PATCH 4/28 libmultipath/structs.c: constify some functions

In reply to Martin Wilck
---
 libmultipath/structs.c | 20 ++++++++++----------
 libmultipath/structs.h | 24 ++++++++++++------------
 2 files changed, 22 insertions(+), 22 deletions(-)
libmultipath/structs.c +10 -10
index 991095cb..6df2f4ec 100644
@@ -353,7 +353,7 @@ store_adaptergroup(vector adapters, struct adapter_group * agp)
 }
 
 struct multipath *
-find_mp_by_minor (vector mpvec, int minor)
+find_mp_by_minor (const struct _vector *mpvec, int minor)
 {
 	int i;
 	struct multipath * mpp;
@@ -372,7 +372,7 @@ find_mp_by_minor (vector mpvec, int minor)
 }
 
 struct multipath *
-find_mp_by_wwid (vector mpvec, char * wwid)
+find_mp_by_wwid (const struct _vector *mpvec, const char * wwid)
 {
 	int i;
 	struct multipath * mpp;
@@ -388,7 +388,7 @@ find_mp_by_wwid (vector mpvec, char * wwid)
 }
 
 struct multipath *
-find_mp_by_alias (vector mpvec, const char * alias)
+find_mp_by_alias (const struct _vector *mpvec, const char * alias)
 {
 	int i;
 	int len;
@@ -411,7 +411,7 @@ find_mp_by_alias (vector mpvec, const char * alias)
 }
 
 struct multipath *
-find_mp_by_str (vector mpvec, char * str)
+find_mp_by_str (const struct _vector *mpvec, const char * str)
 {
 	int minor;
 
@@ -422,7 +422,7 @@ find_mp_by_str (vector mpvec, char * str)
 }
 
 struct path *
-find_path_by_dev (vector pathvec, const char * dev)
+find_path_by_dev (const struct _vector *pathvec, const char * dev)
 {
 	int i;
 	struct path * pp;
@@ -439,7 +439,7 @@ find_path_by_dev (vector pathvec, const char * dev)
 }
 
 struct path *
-find_path_by_devt (vector pathvec, const char * dev_t)
+find_path_by_devt (const struct _vector *pathvec, const char * dev_t)
 {
 	int i;
 	struct path * pp;
@@ -455,7 +455,7 @@ find_path_by_devt (vector pathvec, const char * dev_t)
 	return NULL;
 }
 
-int pathcountgr(struct pathgroup *pgp, int state)
+int pathcountgr(const struct pathgroup *pgp, int state)
 {
 	struct path *pp;
 	int count = 0;
@@ -468,7 +468,7 @@ int pathcountgr(struct pathgroup *pgp, int state)
 	return count;
 }
 
-int pathcount(struct multipath *mpp, int state)
+int pathcount(const struct multipath *mpp, int state)
 {
 	struct pathgroup *pgp;
 	int count = 0;
@@ -481,7 +481,7 @@ int pathcount(struct multipath *mpp, int state)
 	return count;
 }
 
-int pathcmp(struct pathgroup *pgp, struct pathgroup *cpgp)
+int pathcmp(const struct pathgroup *pgp, const struct pathgroup *cpgp)
 {
 	int i, j;
 	struct path *pp, *cpp;
@@ -501,7 +501,7 @@ int pathcmp(struct pathgroup *pgp, struct pathgroup *cpgp)
 }
 
 struct path *
-first_path (struct multipath * mpp)
+first_path (const struct multipath * mpp)
 {
 	struct pathgroup * pgp;
 	if (!mpp->pg)
libmultipath/structs.h +12 -12
index e424b150..fef416b4 100644
@@ -413,18 +413,18 @@ int store_path (vector pathvec, struct path * pp);
 int store_pathgroup (vector pgvec, struct pathgroup * pgp);
 int add_pathgroup(struct multipath*, struct pathgroup *);
 
-struct multipath * find_mp_by_alias (vector mp, const char * alias);
-struct multipath * find_mp_by_wwid (vector mp, char * wwid);
-struct multipath * find_mp_by_str (vector mp, char * wwid);
-struct multipath * find_mp_by_minor (vector mp, int minor);
-
-struct path * find_path_by_devt (vector pathvec, const char * devt);
-struct path * find_path_by_dev (vector pathvec, const char * dev);
-struct path * first_path (struct multipath * mpp);
-
-int pathcountgr (struct pathgroup *, int);
-int pathcount (struct multipath *, int);
-int pathcmp (struct pathgroup *, struct pathgroup *);
+struct multipath * find_mp_by_alias (const struct _vector *mp, const char *alias);
+struct multipath * find_mp_by_wwid (const struct _vector *mp, const char *wwid);
+struct multipath * find_mp_by_str (const struct _vector *mp, const char *wwid);
+struct multipath * find_mp_by_minor (const struct _vector *mp, int minor);
+
+struct path * find_path_by_devt (const struct _vector *pathvec, const char *devt);
+struct path * find_path_by_dev (const struct _vector *pathvec, const char *dev);
+struct path * first_path (const struct multipath *mpp);
+
+int pathcountgr (const struct pathgroup *, int);
+int pathcount (const struct multipath *, int);
+int pathcmp (const struct pathgroup *, const struct pathgroup *);
 int add_feature (char **, const char *);
 int remove_feature (char **, const char *);
 
Signature · 1 line
2.17.0
#5 lore raw

PATCH 5/28 libmultipath: some const usage in hwentry handling

In reply to Martin Wilck

Add some const qualifiers in in the code dealing with hwentries.

---
 libmultipath/blacklist.c | 6 ++++--
 libmultipath/config.c    | 8 ++++----
 2 files changed, 8 insertions(+), 6 deletions(-)
libmultipath/blacklist.c +4 -2
index ee396e26..34a7e717 100644
@@ -126,7 +126,8 @@ _blacklist (vector blist, const char * str)
 }
 
 int
-_blacklist_exceptions_device(vector elist, char * vendor, char * product)
+_blacklist_exceptions_device(const struct _vector *elist, const char * vendor,
+			     const char * product)
 {
 	int i;
 	struct blentry_device * ble;
@@ -144,7 +145,8 @@ _blacklist_exceptions_device(vector elist, char * vendor, char * product)
 }
 
 int
-_blacklist_device (vector blist, char * vendor, char * product)
+_blacklist_device (const struct _vector *blist, const char * vendor,
+		   const char * product)
 {
 	int i;
 	struct blentry_device * ble;
libmultipath/config.c +4 -4
index ce7bedac..44f141ba 100644
@@ -28,7 +28,7 @@
 #include "propsel.h"
 
 static int
-hwe_strmatch (struct hwentry *hwe1, struct hwentry *hwe2)
+hwe_strmatch (const struct hwentry *hwe1, const struct hwentry *hwe2)
 {
 	if ((hwe2->vendor && !hwe1->vendor) ||
 	    (hwe1->vendor && (!hwe2->vendor ||
@@ -49,7 +49,7 @@ hwe_strmatch (struct hwentry *hwe1, struct hwentry *hwe2)
 }
 
 static struct hwentry *
-find_hwe_strmatch (vector hwtable, struct hwentry *hwe)
+find_hwe_strmatch (const struct _vector *hwtable, const struct hwentry *hwe)
 {
 	int i;
 	struct hwentry *tmp, *ret = NULL;
@@ -64,7 +64,7 @@ find_hwe_strmatch (vector hwtable, struct hwentry *hwe)
 }
 
 static int
-hwe_regmatch (struct hwentry *hwe1, struct hwentry *hwe2)
+hwe_regmatch (const struct hwentry *hwe1, const struct hwentry *hwe2)
 {
 	regex_t vre, pre, rre;
 	int retval = 1;
@@ -283,7 +283,7 @@ alloc_hwe (void)
 }
 
 static char *
-set_param_str(char * str)
+set_param_str(const char * str)
 {
 	char * dst;
 	int len;
Signature · 1 line
2.17.0
#6 lore raw

PATCH 6/28 libmultipath: change prototypes of hwe_regmatch() and find_hwe()

In reply to Martin Wilck

hwentry structures are supposed to be initialized and destroyed with alloc_hwe() and free_hwe(), respectively. The second argument is of hwe_regmatch() isn't a real hwentry, just a vendor/product/revision tuple. Clarify that by adapting the prototype. This allows to use const arguments in find_hwe(), too.

---
 libmultipath/config.c | 30 +++++++++++++++---------------
 libmultipath/config.h |  4 +++-
 2 files changed, 18 insertions(+), 16 deletions(-)
libmultipath/config.c +15 -15
index 44f141ba..d2812e4a 100644
@@ -64,7 +64,8 @@ find_hwe_strmatch (const struct _vector *hwtable, const struct hwentry *hwe)
 }
 
 static int
-hwe_regmatch (const struct hwentry *hwe1, const struct hwentry *hwe2)
+hwe_regmatch (const struct hwentry *hwe1, const char *vendor,
+	      const char *product, const char *revision)
 {
 	regex_t vre, pre, rre;
 	int retval = 1;
@@ -81,13 +82,13 @@ hwe_regmatch (const struct hwentry *hwe1, const struct hwentry *hwe2)
 	    regcomp(&rre, hwe1->revision, REG_EXTENDED|REG_NOSUB))
 		goto out_pre;
 
-	if ((hwe2->vendor || hwe2->product || hwe2->revision) &&
-	    (!hwe1->vendor || !hwe2->vendor ||
-	     !regexec(&vre, hwe2->vendor, 0, NULL, 0)) &&
-	    (!hwe1->product || !hwe2->product ||
-	     !regexec(&pre, hwe2->product, 0, NULL, 0)) &&
-	    (!hwe1->revision || !hwe2->revision ||
-	     !regexec(&rre, hwe2->revision, 0, NULL, 0)))
+	if ((vendor || product || revision) &&
+	    (!hwe1->vendor || !vendor ||
+	     !regexec(&vre, vendor, 0, NULL, 0)) &&
+	    (!hwe1->product || !product ||
+	     !regexec(&pre, product, 0, NULL, 0)) &&
+	    (!hwe1->revision || !revision ||
+	     !regexec(&rre, revision, 0, NULL, 0)))
 		retval = 0;
 
 	if (hwe1->revision)
@@ -103,14 +104,12 @@ out:
 }
 
 struct hwentry *
-find_hwe (vector hwtable, char * vendor, char * product, char * revision)
+find_hwe (const struct _vector *hwtable,
+	  const char * vendor, const char * product, const char * revision)
 {
 	int i;
-	struct hwentry hwe, *tmp, *ret = NULL;
+	struct hwentry *tmp, *ret = NULL;
 
-	hwe.vendor = vendor;
-	hwe.product = product;
-	hwe.revision = revision;
 	/*
 	 * Search backwards here.
 	 * User modified entries are attached at the end of
@@ -118,7 +117,7 @@ find_hwe (vector hwtable, char * vendor, char * product, char * revision)
 	 * continuing to the generic entries
 	 */
 	vector_foreach_slot_backwards (hwtable, tmp, i) {
-		if (hwe_regmatch(tmp, &hwe))
+		if (hwe_regmatch(tmp, vendor, product, revision))
 			continue;
 		ret = tmp;
 		break;
@@ -454,7 +453,8 @@ restart:
 				free_hwe(hwe2);
 				continue;
 			}
-			if (hwe_regmatch(hwe1, hwe2))
+			if (hwe_regmatch(hwe1, hwe2->vendor,
+					 hwe2->product, hwe2->revision))
 				continue;
 			/* dup */
 			merge_hwe(hwe2, hwe1);
libmultipath/config.h +3 -1
index 6e69a37e..83eaf62f 100644
@@ -213,7 +213,9 @@ struct config {
 
 extern struct udev * udev;
 
-struct hwentry * find_hwe (vector hwtable, char * vendor, char * product, char *revision);
+struct hwentry * find_hwe (const struct _vector *hwtable,
+			   const char * vendor, const char * product,
+			   const char *revision);
 struct mpentry * find_mpe (vector mptable, char * wwid);
 char * get_mpe_wwid (vector mptable, char * alias);
 
Signature · 1 line
2.17.0
#7 lore raw

PATCH 7/28 libmultipath/prio: constify simple getters

In reply to Martin Wilck

One day we should also constify getprio(), but that requires touching all prioritizers.

---
 libmultipath/prio.c | 8 ++++----
 libmultipath/prio.h | 8 ++++----
 2 files changed, 8 insertions(+), 8 deletions(-)
libmultipath/prio.c +4 -4
index 7fce9215..17acfd05 100644
@@ -81,7 +81,7 @@ static struct prio * prio_lookup (char * name)
 	return NULL;
 }
 
-int prio_set_args (struct prio * p, char * args)
+int prio_set_args (struct prio * p, const char * args)
 {
 	return snprintf(p->args, PRIO_ARGS_LEN, "%s", args);
 }
@@ -130,19 +130,19 @@ int prio_getprio (struct prio * p, struct path * pp, unsigned int timeout)
 	return p->getprio(pp, p->args, timeout);
 }
 
-int prio_selected (struct prio * p)
+int prio_selected (const struct prio * p)
 {
 	if (!p)
 		return 0;
 	return (p->getprio) ? 1 : 0;
 }
 
-char * prio_name (struct prio * p)
+const char * prio_name (const struct prio * p)
 {
 	return p->name;
 }
 
-char * prio_args (struct prio * p)
+const char * prio_args (const struct prio * p)
 {
 	return p->args;
 }
libmultipath/prio.h +4 -4
index c97fe397..aa587ccd 100644
@@ -60,10 +60,10 @@ struct prio * add_prio (char *, char *);
 int prio_getprio (struct prio *, struct path *, unsigned int);
 void prio_get (char *, struct prio *, char *, char *);
 void prio_put (struct prio *);
-int prio_selected (struct prio *);
-char * prio_name (struct prio *);
-char * prio_args (struct prio *);
-int prio_set_args (struct prio *, char *);
+int prio_selected (const struct prio *);
+const char * prio_name (const struct prio *);
+const char * prio_args (const struct prio *);
+int prio_set_args (struct prio *, const char *);
 
 /* The only function exported by prioritizer dynamic libraries (.so) */
 int getprio(struct path *, char *, unsigned int);
Signature · 1 line
2.17.0
#8 lore raw

PATCH 8/28 tests/Makefile: autogenerate list of symbols to be wrapped

In reply to Martin Wilck

Rather than manually listing all the -Wl,--wrap=... options, autogenerate a list of functions to be wrapped in a cmocka test. Allowing this not only for the test file itself but also for additional "test library" source files requires some Makefile incantations.

Moreover, no need to list globals.c in dependencies, automatic dependency tracking takes care for this (and avoids recompilation of tests that don't pull in global.c).

---
 tests/Makefile | 28 ++++++++++++++++++++++------
 1 file changed, 22 insertions(+), 6 deletions(-)
tests/Makefile +22 -6
index 1f364110..7439c8c3 100644
@@ -10,23 +10,39 @@ TESTS := uevent parser util dmevents
 
 all:	$(TESTS:%=%.out)
 
-dmevents-test: dmevents.o ../multipathd/dmevents.c globals.c $(multipathdir)/libmultipath.so
-	@$(CC) -o $@ $< $(LDFLAGS) $(LIBDEPS) -lpthread -ldevmapper -lurcu -Wl,--wrap=open -Wl,--wrap=close -Wl,--wrap=dm_is_mpath -Wl,--wrap=dm_geteventnr -Wl,--wrap=ioctl -Wl,--wrap=libmp_dm_task_create -Wl,--wrap=dm_task_no_open_count -Wl,--wrap=dm_task_run -Wl,--wrap=dm_task_get_names -Wl,--wrap=dm_task_destroy -Wl,--wrap=poll -Wl,--wrap=remove_map_by_alias -Wl,--wrap=update_multipath
+# test-specific linker flags
+# XYZ-test-TESTDEPS: test libraries containing __wrap_xyz functions
+# XYZ-test_OBJDEPS: object files from libraries to link in explicitly
+#    That may be necessary if functions called from the object file are wrapped
+#    (wrapping works only for symbols which are undefined after processing a
+#    linker input file).
+# XYZ-test_LIBDEPS: Additional libs to link for this test
 
-%-test:	%.o globals.c $(multipathdir)/libmultipath.so
-	@$(CC) -o $@ $< $(LDFLAGS) $(LIBDEPS)
+dmevents-test_LIBDEPS = -lpthread -ldevmapper -lurcu
 
 %.out:	%-test
 	@echo == running $< ==
 	@LD_LIBRARY_PATH=$(multipathdir):$(mpathcmddir) ./$< >$@
 
 clean: dep_clean
-	rm -f $(TESTS:%=%-test) $(TESTS:%=%.out) $(TESTS:%=%.o)
+	$(RM) $(TESTS:%=%-test) $(TESTS:%=%.out) $(OBJS)
 
-OBJS = $(TESTS:%=%.o)
 .SECONDARY: $(OBJS)
 
 include $(wildcard $(OBJS:.o=.d))
 
 dep_clean:
 	$(RM) $(OBJS:.o=.d)
+
+%.o.wrap:	%.c
+	@sed -n 's/^.*__wrap_\([a-zA-Z0-9_]*\).*$$/-Wl,--wrap=\1/p' $< | \
+		sort -u | tr '\n' ' ' >$@
+
+# COLON will get expanded during second expansion below
+COLON:=:
+.SECONDEXPANSION:
+%-test:	%.o %.o.wrap $$($$@_OBJDEPS) $$($$@_TESTDEPS) $$($$@_TESTDEPS$$(COLON).o=.o.wrap) \
+		$(multipathdir)/libmultipath.so Makefile
+	$(CC) $(CFLAGS) -o $@ $(LDFLAGS) $< $($@_TESTDEPS) $($@_OBJDEPS) \
+		$(LIBDEPS) $($@_LIBDEPS) \
+		$(file <$<.wrap) $(foreach dep,$($@_TESTDEPS),$(file <$(dep).wrap))
Signature · 1 line
2.17.0
#9 lore raw

PATCH 9/28 tests/test-lib: cmocka helpers to simulate path and map discovery

In reply to Martin Wilck

These functions simulate path discovery and multipath setup with cmocka. They will be used in the hwtable test case, but may be useful for other future test cases, too.

---
 tests/test-lib.c | 359 +++++++++++++++++++++++++++++++++++++++++++++++
 tests/test-lib.h |  67 +++++++++
 2 files changed, 426 insertions(+)
 create mode 100644 tests/test-lib.c
 create mode 100644 tests/test-lib.h
tests/test-lib.c new file +359 -0
new file mode 100644
index 00000000..cff7a5d1
@@ -0,0 +1,359 @@
+#include <string.h>
+#include <setjmp.h>
+#include <stdarg.h>
+#include <cmocka.h>
+#include <libudev.h>
+#include <sys/sysmacros.h>
+#include "debug.h"
+#include "util.h"
+#include "vector.h"
+#include "structs.h"
+#include "structs_vec.h"
+#include "config.h"
+#include "discovery.h"
+#include "propsel.h"
+#include "test-lib.h"
+
+const int default_mask = (DI_SYSFS|DI_BLACKLIST|DI_WWID|DI_CHECKER|DI_PRIO);
+const char default_devnode[] = "sdTEST";
+const char default_wwid[] = "TEST-WWID";
+/* default_wwid should be a substring of default_wwid_1! */
+const char default_wwid_1[] = "TEST-WWID-1";
+
+/*
+ * Helper wrappers for mock_path().
+ *
+ * We need to make pathinfo() think it has detected a device with
+ * certain vendor/product/rev. This requires faking lots of udev
+ * and sysfs function responses.
+ *
+ * This requires hwtable-test_OBJDEPS = ../libmultipath/discovery.o
+ * in the Makefile in order to wrap calls from discovery.o.
+ *
+ * Note that functions that are called and defined in discovery.o can't
+ * be wrapped this way (e.g. sysfs_get_vendor), because symbols are
+ * resolved by the assembler before the linking stage.
+ */
+
+int __real_open(const char *path, int flags, int mode);
+
+static const char _mocked_filename[] = "mocked_path";
+int __wrap_open(const char *path, int flags, int mode)
+{
+	condlog(4, "%s: %s", __func__, path);
+
+	if (!strcmp(path, _mocked_filename))
+		return 111;
+	return __real_open(path, flags, mode);
+}
+
+int __wrap_execute_program(char *path, char *value, int len)
+{
+	char *val = mock_ptr_type(char *);
+
+	condlog(5, "%s: %s", __func__, val);
+	strlcpy(value, val, len);
+	return 0;
+}
+
+bool __wrap_is_claimed_by_foreign(struct udev_device *ud)
+{
+	condlog(5, "%s: %p", __func__, ud);
+	return false;
+}
+
+struct udev_list_entry
+*__wrap_udev_device_get_properties_list_entry(struct udev_device *ud)
+{
+	void *p = (void*)0x12345678;
+	condlog(5, "%s: %p", __func__, p);
+
+	return p;
+}
+
+struct udev_list_entry
+*__wrap_udev_list_entry_get_next(struct udev_list_entry *udle)
+{
+	void *p  = NULL;
+	condlog(5, "%s: %p", __func__, p);
+
+	return p;
+}
+
+const char *__wrap_udev_list_entry_get_name(struct udev_list_entry *udle)
+{
+	char *val = mock_ptr_type(char *);
+
+	condlog(5, "%s: %s", __func__, val);
+	return val;
+}
+
+struct udev_device *__wrap_udev_device_ref(struct udev_device *ud)
+{
+	return ud;
+}
+
+struct udev_device *__wrap_udev_device_unref(struct udev_device *ud)
+{
+	return ud;
+}
+
+char *__wrap_udev_device_get_subsystem(struct udev_device *ud)
+{
+	char *val = mock_ptr_type(char *);
+
+	condlog(5, "%s: %s", __func__, val);
+	return val;
+}
+
+char *__wrap_udev_device_get_sysname(struct udev_device *ud)
+{
+	char *val  = mock_ptr_type(char *);
+
+	condlog(5, "%s: %s", __func__, val);
+	return val;
+}
+
+char *__wrap_udev_device_get_devnode(struct udev_device *ud)
+{
+	char *val  = mock_ptr_type(char *);
+
+	condlog(5, "%s: %s", __func__, val);
+	return val;
+}
+
+dev_t __wrap_udev_device_get_devnum(struct udev_device *ud)
+{
+	condlog(5, "%s: %p", __func__, ud);
+	return makedev(17, 17);
+}
+
+char *__wrap_udev_device_get_sysattr_value(struct udev_device *ud,
+					     const char *attr)
+{
+	char *val  = mock_ptr_type(char *);
+
+	condlog(5, "%s: %s->%s", __func__, attr, val);
+	return val;
+}
+
+char *__wrap_udev_device_get_property_value(struct udev_device *ud,
+					    const char *attr)
+{
+	char *val  = mock_ptr_type(char *);
+
+	condlog(5, "%s: %s->%s", __func__, attr, val);
+	return val;
+}
+
+int __wrap_sysfs_get_size(struct path *pp, unsigned long long *sz)
+{
+	*sz = 12345678UL;
+	return 0;
+}
+
+void *__wrap_udev_device_get_parent_with_subsystem_devtype(
+	struct udev_device *ud, const char *subsys, char *type)
+{
+	/* return non-NULL for sysfs_get_tgt_nodename */
+	return type;
+}
+
+void *__wrap_udev_device_get_parent(struct udev_device *ud)
+{
+	char *val  = mock_ptr_type(void *);
+
+	condlog(5, "%s: %p", __func__, val);
+	return val;
+}
+
+ssize_t __wrap_sysfs_attr_get_value(struct udev_device *dev,
+				    const char *attr_name,
+				    char *value, size_t sz)
+{
+	char *val  = mock_ptr_type(char *);
+
+	condlog(5, "%s: %s", __func__, val);
+	strlcpy(value, val, sz);
+	return strlen(value);
+}
+
+int __wrap_checker_check(struct checker *c, int st)
+{
+	condlog(5, "%s: %d", __func__, st);
+	return st;
+}
+
+int __wrap_prio_getprio(struct prio *p, struct path *pp, unsigned int tmo)
+{
+	int pr = 5;
+
+	condlog(5, "%s: %d", __func__, pr);
+	return pr;
+}
+
+struct mocked_path *fill_mocked_path(struct mocked_path *mp,
+				     const char *vendor, const char *product,
+				     const char *rev, const char *wwid,
+				     const char *devnode, unsigned int flags)
+{
+	mp->vendor = (vendor ? vendor : "noname");
+	mp->product = (product ? product : "noprod");
+	mp->rev = (rev ? rev : "0");
+	mp->wwid = (wwid ? wwid : default_wwid);
+	mp->devnode = (devnode ? devnode : default_devnode);
+	mp->flags = flags|NEED_SELECT_PRIO|NEED_FD;
+	return mp;
+}
+
+struct mocked_path *mocked_path_from_path(struct mocked_path *mp,
+					  const struct path *pp)
+{
+	mp->vendor = pp->vendor_id;
+	mp->product = pp->product_id;
+	mp->rev = pp->rev;
+	mp->wwid = pp->wwid;
+	mp->devnode = pp->dev;
+	mp->flags = (prio_selected(&pp->prio) ? 0 : NEED_SELECT_PRIO) |
+		(pp->fd < 0 ? NEED_FD : 0) |
+		(pp->getuid ? USE_GETUID : 0);
+	return mp;
+}
+
+static void mock_sysfs_pathinfo(const struct mocked_path *mp)
+{
+	static const char hbtl[] = "4:0:3:1";
+
+	will_return(__wrap_udev_device_get_subsystem, "scsi");
+	will_return(__wrap_udev_device_get_sysname, hbtl);
+	will_return(__wrap_udev_device_get_sysname, hbtl);
+	will_return(__wrap_udev_device_get_sysattr_value, mp->vendor);
+	will_return(__wrap_udev_device_get_sysname, hbtl);
+	will_return(__wrap_udev_device_get_sysattr_value, mp->product);
+	will_return(__wrap_udev_device_get_sysname, hbtl);
+	will_return(__wrap_udev_device_get_sysattr_value, mp->rev);
+
+	/* sysfs_get_tgt_nodename */
+	will_return(__wrap_udev_device_get_sysattr_value, NULL);
+	will_return(__wrap_udev_device_get_parent, NULL);
+	will_return(__wrap_udev_device_get_parent, NULL);
+	will_return(__wrap_udev_device_get_sysname, "nofibre");
+	will_return(__wrap_udev_device_get_sysname, "noiscsi");
+	will_return(__wrap_udev_device_get_parent, NULL);
+	will_return(__wrap_udev_device_get_sysname, "ata25");
+}
+
+/*
+ * Pretend we detected a SCSI device with given vendor/prod/rev
+ */
+void mock_pathinfo(int mask, const struct mocked_path *mp)
+{
+	/* filter_property */
+	will_return(__wrap_udev_device_get_sysname, mp->devnode);
+	if (mp->flags & BL_BY_PROPERTY) {
+		will_return(__wrap_udev_list_entry_get_name, "BAZ");
+		return;
+	} else
+		will_return(__wrap_udev_list_entry_get_name,
+			    "SCSI_IDENT_LUN_NAA_EXT");
+	if (mask & DI_SYSFS)
+		mock_sysfs_pathinfo(mp);
+
+	if (mp->flags & BL_BY_DEVICE &&
+	    (mask & DI_BLACKLIST && mask & DI_SYSFS))
+		return;
+
+	/* path_offline */
+	will_return(__wrap_udev_device_get_subsystem, "scsi");
+	will_return(__wrap_sysfs_attr_get_value, "running");
+
+	if (mask & DI_NOIO)
+		return;
+
+	/* fake open() in pathinfo() */
+	if (mp->flags & NEED_FD)
+		will_return(__wrap_udev_device_get_devnode, _mocked_filename);
+	/* DI_SERIAL is unsupported */
+	assert_false(mask & DI_SERIAL);
+
+	if (mask & DI_WWID) {
+		if (mp->flags & USE_GETUID)
+			will_return(__wrap_execute_program, mp->wwid);
+		else
+			/* get_udev_uid() */
+			will_return(__wrap_udev_device_get_property_value,
+				    mp->wwid);
+	}
+
+	if (mask & DI_CHECKER) {
+		/* get_state -> sysfs_get_timeout  */
+		will_return(__wrap_udev_device_get_subsystem, "scsi");
+		will_return(__wrap_udev_device_get_sysattr_value, "180");
+	}
+
+	if (mask & DI_PRIO && mp->flags & NEED_SELECT_PRIO) {
+
+		/* sysfs_get_timeout, again (!?) */
+		will_return(__wrap_udev_device_get_subsystem, "scsi");
+		will_return(__wrap_udev_device_get_sysattr_value, "180");
+
+	}
+}
+
+void mock_store_pathinfo(int mask,  const struct mocked_path *mp)
+{
+	will_return(__wrap_udev_device_get_sysname, mp->devnode);
+	mock_pathinfo(mask, mp);
+}
+
+struct path *__mock_path(vector pathvec,
+			 const char *vnd, const char *prd,
+			 const char *rev, const char *wwid,
+			 const char *dev,
+			 unsigned int flags, int mask)
+{
+	struct mocked_path mop;
+	struct path *pp;
+	struct config *conf;
+	int r;
+
+	fill_mocked_path(&mop, vnd, prd, rev, wwid, dev, flags);
+	mock_store_pathinfo(mask, &mop);
+
+	conf = get_multipath_config();
+	r = store_pathinfo(pathvec, conf, (void *)&mop, mask, &pp);
+	put_multipath_config(conf);
+
+	if (flags & BL_MASK) {
+		assert_int_equal(r, PATHINFO_SKIPPED);
+		return NULL;
+	}
+	assert_int_equal(r, PATHINFO_OK);
+	assert_non_null(pp);
+	return pp;
+}
+
+
+struct multipath *__mock_multipath(struct vectors *vecs, struct path *pp)
+{
+	struct multipath *mp;
+	struct config *conf;
+	struct mocked_path mop;
+
+	mocked_path_from_path(&mop, pp);
+	/* pathinfo() call in adopt_paths */
+	mock_pathinfo(DI_CHECKER|DI_PRIO, &mop);
+
+	mp = add_map_with_path(vecs, pp, 1);
+	assert_ptr_not_equal(mp, NULL);
+
+	/* TBD: mock setup_map() ... */
+	conf = get_multipath_config();
+	select_pgpolicy(conf, mp);
+	select_no_path_retry(conf, mp);
+	select_retain_hwhandler(conf, mp);
+	select_minio(conf, mp);
+	put_multipath_config(conf);
+
+	return mp;
+}
tests/test-lib.h new file +67 -0

67 lines not on this page · raw message

Signature · 1 line
2.17.0
#10 lore raw

PATCH 10/28 tests/hwtable: tests for config file handling and hwentry merging

In reply to Martin Wilck

Add unit tests for parsing "device" and "blacklist" configuration entries and hwtable entry merging. The tests work by creating a multipath.conf file and one or more config_dir/*.conf files, reading device entries from both, simulating discovery of fake paths and multipaths, and verifying these are configured as expected. The focus is on the behavior if several hwtable or blacklist entries match a single device.

Most of these tests in this file serve as regression test for follow-up changes. But Some of the tests are meant to illustrate problems problems with the current code, which behaves inconsistently in certain cases. By changing the BROKEN macro at the top of the file, the user can control if broken behavior causes test failure, or succeeds with a warning. Subsequent patches will remove the BROKEN tests. Reviewers are encouraged to focus on the BROKEN tests, which annotate current behavior that follow-up patches are going to change.

Problems with the current code are mainly found in two areas:

1. It currently makes a difference whether several entries matching the same device are in in the same configuration file (default "multipath.conf") or distributed over "multipath.conf" and other files under config_dir. This is highly unexpected behavior.
Moreover, in this context, the built-in hwtable can be viewed as yet another "configuration file" which is parsed before "multipath.conf". The same problem
arises: adding a hwentry to "multipath.conf" doesn't necessarily have the same effect as adding the same entry to the built-in hwtable and recompiling, because different merging logic applies (in short: entries from different "configuration files" are merged, but entries from the same "file" aren't).

This is a major blocker for using the configuration dumped with "multipath -t" as input for multipath-tools, because a built-in entry which is dumped ends up in "multipath.conf" or a config_dir file, which may change the way it's merged with other entries.

2. The vendor/product/rev IDs are regular expressions, and merging of entries by regular expressions simply doesn't work. Entries are merged if a later regex, interpreted as a string, matches an earlier added regex. But that's incorrect. Except in trivial cases, matching of a regex r2 by another regex r1 doesn't say anything about whether strings matched by r2 will be matched by r1 as well.

Another, minor issue is the treatment of double quotes.

The way these tests are implemented using the test_driver() function may seem weird in the first place. The reason is that in a later patch, when we have configuration-dumping-and-reloading basically working, test_driver() will be extended to run the same test both with the initial and dumped configuration and ensure that there are no differences. If I didn't introduce test_driver() right away, I'd have major, confusing changes in the test code when this functionality is added.

---
 tests/Makefile  |    8 +-
 tests/hwtable.c | 1689 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 1696 insertions(+), 1 deletion(-)
 create mode 100644 tests/hwtable.c
tests/Makefile +7 -1
index 7439c8c3..78755edd 100644
@@ -3,7 +3,7 @@ include ../Makefile.inc
 CFLAGS += $(BIN_CFLAGS) -I$(multipathdir) -I$(mpathcmddir)
 LIBDEPS += -L$(multipathdir) -lmultipath -lcmocka
 
-TESTS := uevent parser util dmevents
+TESTS := uevent parser util dmevents hwtable
 
 .SILENT: $(TESTS:%=%.o)
 .PRECIOUS: $(TESTS:%=%-test)
@@ -19,11 +19,17 @@ all:	$(TESTS:%=%.out)
 # XYZ-test_LIBDEPS: Additional libs to link for this test
 
 dmevents-test_LIBDEPS = -lpthread -ldevmapper -lurcu
+hwtable-test_TESTDEPS := test-lib.o
+hwtable-test_OBJDEPS := ../libmultipath/discovery.o ../libmultipath/blacklist.o \
+	../libmultipath/prio.o ../libmultipath/callout.o ../libmultipath/structs.o
+hwtable-test_LIBDEPS := -ludev -lpthread -ldl
 
 %.out:	%-test
 	@echo == running $< ==
 	@LD_LIBRARY_PATH=$(multipathdir):$(mpathcmddir) ./$< >$@
 
+OBJS = $(TESTS:%=%.o) test-lib.o
+
 clean: dep_clean
 	$(RM) $(TESTS:%=%-test) $(TESTS:%=%.out) $(OBJS)
 
tests/hwtable.c new file +1689 -0

1,689 lines not on this page · raw message

Signature · 1 line
2.17.0
#11 lore raw

PATCH 11/28 libmultipath: add debug messages to hwentry lookup/merging code

In reply to Martin Wilck
---
 libmultipath/config.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
libmultipath/config.c +16 -0
index d2812e4a..9e2f166f 100644
@@ -103,6 +103,16 @@ out:
 	return retval;
 }
 
+static void _log_match(const char *fn, const struct hwentry *h,
+		       const char *vendor, const char *product,
+		       const char *revision)
+{
+	condlog(4, "%s: found match /%s:%s:%s/ for '%s:%s:%s'", fn,
+		h->vendor, h->product, h->revision,
+		vendor, product, revision);
+}
+#define log_match(h, v, p, r) _log_match(__func__, (h), (v), (p), (r))
+
 struct hwentry *
 find_hwe (const struct _vector *hwtable,
 	  const char * vendor, const char * product, const char * revision)
@@ -120,6 +130,7 @@ find_hwe (const struct _vector *hwtable,
 		if (hwe_regmatch(tmp, vendor, product, revision))
 			continue;
 		ret = tmp;
+		log_match(tmp, vendor, product, revision);
 		break;
 	}
 	return ret;
@@ -457,8 +468,13 @@ restart:
 					 hwe2->product, hwe2->revision))
 				continue;
 			/* dup */
+			log_match(hwe1, hwe2->vendor,
+				  hwe2->product, hwe2->revision);
 			merge_hwe(hwe2, hwe1);
 			if (hwe_strmatch(hwe2, hwe1) == 0) {
+				condlog(4, "%s: removing hwentry %s:%s:%s",
+					__func__, hwe1->vendor, hwe1->product,
+					hwe1->revision);
 				vector_del_slot(hw, i);
 				free_hwe(hwe1);
 				n -= 1;
Signature · 1 line
2.17.0
#12 lore raw

PATCH 12/28 libmultipath: use vector for for pp->hwe and mp->hwe

In reply to Martin Wilck

Change the data structure of "struct path" and "struct multipath" such that the "hwe" entry is a vector of hwentry structures rather than a single hwentry structure. Add respective code to the constructors and destructors (note that mp->hwe is never allocated, it's always a pointer to a path hwe).

Change find_hwe() to fill in the passed vector rather than returning a hwentry pointer. Change the propsel code to look through vectors of hwentries to determine a given property.

This patch just creates the new data structure and the functions to deal with them, it doesn't introduce semantic changes.

---
 libmultipath/config.c    | 21 ++++++++++------
 libmultipath/config.h    |  6 ++---
 libmultipath/discovery.c | 10 ++++----
 libmultipath/propsel.c   | 53 ++++++++++++++++++++++++++++++++++------
 libmultipath/structs.c   |  6 +++++
 libmultipath/structs.h   |  4 +--
 6 files changed, 75 insertions(+), 25 deletions(-)
libmultipath/config.c +14 -7
index 9e2f166f..95a71447 100644
@@ -113,27 +113,34 @@ static void _log_match(const char *fn, const struct hwentry *h,
 }
 #define log_match(h, v, p, r) _log_match(__func__, (h), (v), (p), (r))
 
-struct hwentry *
+int
 find_hwe (const struct _vector *hwtable,
-	  const char * vendor, const char * product, const char * revision)
+	  const char * vendor, const char * product, const char * revision,
+	  vector result)
 {
-	int i;
-	struct hwentry *tmp, *ret = NULL;
+	int i, n = 0;
+	struct hwentry *tmp;
 
 	/*
-	 * Search backwards here.
+	 * Search backwards here, and add forward.
 	 * User modified entries are attached at the end of
 	 * the list, so we have to check them first before
 	 * continuing to the generic entries
 	 */
+	vector_reset(result);
 	vector_foreach_slot_backwards (hwtable, tmp, i) {
 		if (hwe_regmatch(tmp, vendor, product, revision))
 			continue;
-		ret = tmp;
+		if (vector_alloc_slot(result) != NULL) {
+			vector_set_slot(result, tmp);
+			n++;
+		}
 		log_match(tmp, vendor, product, revision);
 		break;
 	}
-	return ret;
+	condlog(n > 1 ? 3 : 4, "%s: found %d hwtable matches for %s:%s:%s",
+		__func__, n, vendor, product, revision);
+	return n;
 }
 
 struct mpentry *find_mpe(vector mptable, char *wwid)
libmultipath/config.h +3 -3
index 83eaf62f..e1cbd59b 100644
@@ -213,9 +213,9 @@ struct config {
 
 extern struct udev * udev;
 
-struct hwentry * find_hwe (const struct _vector *hwtable,
-			   const char * vendor, const char * product,
-			   const char *revision);
+int find_hwe (const struct _vector *hwtable,
+	      const char * vendor, const char * product, const char *revision,
+	      vector result);
 struct mpentry * find_mpe (vector mptable, char * wwid);
 char * get_mpe_wwid (vector mptable, char * alias);
 
libmultipath/discovery.c +5 -5
index 1ef1dfa7..b974d6cd 100644
@@ -1176,7 +1176,7 @@ scsi_sysfs_pathinfo (struct path * pp, vector hwtable)
 	/*
 	 * set the hwe configlet pointer
 	 */
-	pp->hwe = find_hwe(hwtable, pp->vendor_id, pp->product_id, pp->rev);
+	find_hwe(hwtable, pp->vendor_id, pp->product_id, pp->rev, pp->hwe);
 
 	/*
 	 * host / bus / target / lun
@@ -1240,7 +1240,7 @@ nvme_sysfs_pathinfo (struct path * pp, vector hwtable)
 	condlog(3, "%s: serial = %s", pp->dev, pp->serial);
 	condlog(3, "%s: rev = %s", pp->dev, pp->rev);
 
-	pp->hwe = find_hwe(hwtable, pp->vendor_id, pp->product_id, NULL);
+	find_hwe(hwtable, pp->vendor_id, pp->product_id, NULL, pp->hwe);
 
 	return 0;
 }
@@ -1256,7 +1256,7 @@ rbd_sysfs_pathinfo (struct path * pp, vector hwtable)
 	/*
 	 * set the hwe configlet pointer
 	 */
-	pp->hwe = find_hwe(hwtable, pp->vendor_id, pp->product_id, NULL);
+	find_hwe(hwtable, pp->vendor_id, pp->product_id, NULL, pp->hwe);
 	return 0;
 }
 
@@ -1297,7 +1297,7 @@ ccw_sysfs_pathinfo (struct path * pp, vector hwtable)
 	/*
 	 * set the hwe configlet pointer
 	 */
-	pp->hwe = find_hwe(hwtable, pp->vendor_id, pp->product_id, NULL);
+	find_hwe(hwtable, pp->vendor_id, pp->product_id, NULL, pp->hwe);
 
 	/*
 	 * host / bus / target / lun
@@ -1360,7 +1360,7 @@ cciss_sysfs_pathinfo (struct path * pp, vector hwtable)
 	/*
 	 * set the hwe configlet pointer
 	 */
-	pp->hwe = find_hwe(hwtable, pp->vendor_id, pp->product_id, pp->rev);
+	find_hwe(hwtable, pp->vendor_id, pp->product_id, pp->rev, pp->hwe);
 
 	/*
 	 * host / bus / target / lun
libmultipath/propsel.c +45 -8

102 lines not on this page · raw message

libmultipath/structs.c +6 -0
index 6df2f4ec..ae847d61 100644
@@ -102,6 +102,11 @@ alloc_path (void)
 		pp->priority = PRIO_UNDEF;
 		checker_clear(&pp->checker);
 		dm_path_to_gen(pp)->ops = &dm_gen_path_ops;
+		pp->hwe = vector_alloc();
+		if (pp->hwe == NULL) {
+			free(pp);
+			return NULL;
+		}
 	}
 	return pp;
 }
@@ -125,6 +130,7 @@ free_path (struct path * pp)
 		udev_device_unref(pp->udev);
 		pp->udev = NULL;
 	}
+	vector_free(pp->hwe);
 
 	FREE(pp);
 }
libmultipath/structs.h +2 -2
index fef416b4..a801cd92 100644
@@ -283,7 +283,7 @@ struct path {
 	int io_err_pathfail_starttime;
 	int find_multipaths_timeout;
 	/* configlet pointers */
-	struct hwentry * hwe;
+	vector hwe;
 	struct gen_path generic_path;
 };
 
@@ -342,7 +342,7 @@ struct multipath {
 	char * features;
 	char * hwhandler;
 	struct mpentry * mpe;
-	struct hwentry * hwe;
+	vector hwe;
 
 	/* threads */
 	pthread_t waiter;
Signature · 1 line
2.17.0
#13 lore raw

PATCH 13/28 libmultipath: allow more than one hwentry

In reply to Martin Wilck

The previous patch "use vector for for pp->hwe and mp->hwe" changed the data structure for hwentries. This patch changes actual behavior by allowing more than one hwentry to match a given path (or multipath).

This fixes several currently broken test cases. The test code is adapted accordingly.

---
 libmultipath/config.c |  1 -
 tests/hwtable.c       | 64 ++++++++-----------------------------------
 2 files changed, 12 insertions(+), 53 deletions(-)
libmultipath/config.c +0 -1
index 95a71447..89aad15a 100644
@@ -136,7 +136,6 @@ find_hwe (const struct _vector *hwtable,
 			n++;
 		}
 		log_match(tmp, vendor, product, revision);
-		break;
 	}
 	condlog(n > 1 ? 3 : 4, "%s: found %d hwtable matches for %s:%s:%s",
 		__func__, n, vendor, product, revision);
tests/hwtable.c +12 -52

194 lines not on this page · raw message

Signature · 1 line
2.17.0
#14 lore raw

PATCH 14/28 libmultipath: don't merge hwentries by regex

In reply to Martin Wilck

Merging by regular expression is wrong, because regular expressions can't be matched against each other. Unexpected results for hardware properties may result. Don't do it any more.

---
 libmultipath/config.c | 8 +-------
 tests/hwtable.c       | 8 +-------
 2 files changed, 2 insertions(+), 14 deletions(-)
libmultipath/config.c +1 -7
index 89aad15a..713ac7f3 100644
@@ -470,18 +470,12 @@ restart:
 				free_hwe(hwe2);
 				continue;
 			}
-			if (hwe_regmatch(hwe1, hwe2->vendor,
-					 hwe2->product, hwe2->revision))
-				continue;
-			/* dup */
-			log_match(hwe1, hwe2->vendor,
-				  hwe2->product, hwe2->revision);
-			merge_hwe(hwe2, hwe1);
 			if (hwe_strmatch(hwe2, hwe1) == 0) {
 				condlog(4, "%s: removing hwentry %s:%s:%s",
 					__func__, hwe1->vendor, hwe1->product,
 					hwe1->revision);
 				vector_del_slot(hw, i);
+				merge_hwe(hwe2, hwe1);
 				free_hwe(hwe1);
 				n -= 1;
 				/*
tests/hwtable.c +1 -7
index 8b2ed95d..15f364e4 100644
@@ -1138,15 +1138,10 @@ static int setup_2_ident_not_self_matching_re_hwe_dir(void **state)
  * Two different non-trivial regexes kv1, kv2. The 1st one matches the 2nd, but
  * it doesn't match all possible strings matching the second.
  * ("ba[zy]" matches regex "ba[[rxy]", but "baz" does not).
- * This causes the first entry to be merged into the second, but both entries
- * to be kept.
  *
  * Expected: Devices matching both regexes get properties from both, kv2
  * taking precedence. Devices matching just one regex get properties from
  * that one regex only.
- *
- * Current: behaves as expected, except for devices that match only kv2.
- * Those get properties from kv1, too.
  */
 static void test_2_matching_res_hwe_dir(const struct hwt_state *hwt)
 {
@@ -1168,8 +1163,7 @@ static void test_2_matching_res_hwe_dir(const struct hwt_state *hwt)
 	pp = mock_path_flags(vnd_foo.value, prd_baz.value, USE_GETUID);
 	TEST_PROP(prio_name(&pp->prio), prio_hds.value);
 	TEST_PROP(pp->getuid, gui_foo.value);
-	TEST_PROP_BROKEN(_checker, pp->checker.name,
-			 chk_hp.value, DEFAULT_CHECKER);
+	TEST_PROP(pp->checker.name, DEFAULT_CHECKER);
 }
 
 static int setup_2_matching_res_hwe_dir(void **state)
Signature · 1 line
2.17.0
#15 lore raw

PATCH 15/28 libmultipath: merge hwentries inside a conf file

In reply to Martin Wilck

Merging hwentries with identical vendor/product/revision is still useful, although it's not strictly necessary any more. But such identical entries should always be merged, not only if they appear in different configuration files. This requires changing the logic of factorize_hwtable.

By setting -DCHECK_BUILTIN_HWTABLE at compile time, the built-in hwtable can be checked against duplicates as well.

---
 libmultipath/config.c | 43 +++++++++++++++++++++----------------------
 1 file changed, 21 insertions(+), 22 deletions(-)
libmultipath/config.c +21 -22
index 713ac7f3..fb41d620 100644
@@ -452,7 +452,7 @@ out:
 }
 
 static void
-factorize_hwtable (vector hw, int n)
+factorize_hwtable (vector hw, int n, const char *table_desc)
 {
 	struct hwentry *hwe1, *hwe2;
 	int i, j;
@@ -462,22 +462,26 @@ restart:
 		if (i == n)
 			break;
 		j = n;
+		/* drop invalid device configs */
+		if (i >= n && (!hwe1->vendor || !hwe1->product)) {
+			condlog(0, "device config in %s missing vendor or product parameter",
+				table_desc);
+			vector_del_slot(hw, i--);
+			free_hwe(hwe1);
+			continue;
+		}
+		j = n > i + 1 ? n : i + 1;
 		vector_foreach_slot_after(hw, hwe2, j) {
-			/* drop invalid device configs */
-			if (!hwe2->vendor || !hwe2->product) {
-				condlog(0, "device config missing vendor or product parameter");
-				vector_del_slot(hw, j--);
-				free_hwe(hwe2);
-				continue;
-			}
 			if (hwe_strmatch(hwe2, hwe1) == 0) {
-				condlog(4, "%s: removing hwentry %s:%s:%s",
+				condlog(i >= n ? 1 : 3,
+					"%s: duplicate device section for %s:%s:%s in %s",
 					__func__, hwe1->vendor, hwe1->product,
-					hwe1->revision);
+					hwe1->revision, table_desc);
 				vector_del_slot(hw, i);
 				merge_hwe(hwe2, hwe1);
 				free_hwe(hwe1);
-				n -= 1;
+				if (i < n)
+					n -= 1;
 				/*
 				 * Play safe here; we have modified
 				 * the original vector so the outer
@@ -605,9 +609,8 @@ process_config_dir(struct config *conf, vector keywords, char *dir)
 		snprintf(path, LINE_MAX, "%s/%s", dir, namelist[i]->d_name);
 		path[LINE_MAX-1] = '\0';
 		process_file(conf, path);
-		if (VECTOR_SIZE(conf->hwtable) > old_hwtable_size)
-			factorize_hwtable(conf->hwtable, old_hwtable_size);
-
+		factorize_hwtable(conf->hwtable, old_hwtable_size,
+				  namelist[i]->d_name);
 	}
 	pthread_cleanup_pop(1);
 }
@@ -655,6 +658,9 @@ load_config (char * file)
 	if (setup_default_hwtable(conf->hwtable))
 		goto out;
 
+#ifdef CHECK_BUILTIN_HWTABLE
+	factorize_hwtable(conf->hwtable, 0, "builtin");
+#endif
 	/*
 	 * read the config file
 	 */
@@ -668,14 +674,7 @@ load_config (char * file)
 			condlog(0, "error parsing config file");
 			goto out;
 		}
-		if (VECTOR_SIZE(conf->hwtable) > builtin_hwtable_size) {
-			/*
-			 * remove duplica in hwtable. config file
-			 * takes precedence over build-in hwtable
-			 */
-			factorize_hwtable(conf->hwtable, builtin_hwtable_size);
-		}
-
+		factorize_hwtable(conf->hwtable, builtin_hwtable_size, file);
 	}
 
 	conf->processed_main_config = 1;
Signature · 1 line
2.17.0
#16 lore raw

Re: [PATCH 15/28] libmultipath: merge hwentries inside a conf file

Quoted 4 lines · Martin Wilck

Merging hwentries with identical vendor/product/revision is still useful, although it's not strictly necessary any more. But such identical entries should always be merged, not only if they appear in different configuration files. This requires changing the logic of factorize_hwtable.

Sorry for my slowness in reviewing these. This one looks wrong to me

Quoted 22 lines

By setting -DCHECK_BUILTIN_HWTABLE at compile time, the built-in hwtable can be checked against duplicates as well.

---
 libmultipath/config.c | 43 +++++++++++++++++++++----------------------
 1 file changed, 21 insertions(+), 22 deletions(-)
libmultipath/config.c +1 -1
index 713ac7f3..fb41d620 100644
@@ -452,7 +452,7 @@ out:
 }
 
 static void
-factorize_hwtable (vector hw, int n)
+factorize_hwtable (vector hw, int n, const char *table_desc)
 {
 	struct hwentry *hwe1, *hwe2;
 	int i, j;

libmultipath/config.c restart: · line 466

Quoted 5 lines
libmultipath/config.c +2 -0
@@ -462,22 +462,26 @@ restart:
 		if (i == n)
 			break;
 		j = n;
+		/* drop invalid device configs */
+		if (i >= n && (!hwe1->vendor || !hwe1->product)) {

How can i >= n? vector_foreach_slot() starts i at 0, and then next lines are

                if (i == n)
                        break;

libmultipath/config.c restart: · line 469

Quoted 3 lines
libmultipath/config.c +3 -0
+			condlog(0, "device config in %s missing vendor or product parameter",
+				table_desc);
+			vector_del_slot(hw, i--);

shouldn't we also decrement n here. I realize that doesn't work well for the CHECK_BUILTIN_HWTABLE check, where n is 0, but as I said above, I'm pretty sure that will never get here.

libmultipath/config.c restart: · line 473

Quoted 4 lines
libmultipath/config.c +4 -0
+			free_hwe(hwe1);
+			continue;
+		}
+		j = n > i + 1 ? n : i + 1;

again, n must always be at least i + 1 to get here, so j is always n.

libmultipath/config.c restart: · line 476

Quoted 11 lines
libmultipath/config.c +1 -8
 		vector_foreach_slot_after(hw, hwe2, j) {
-			/* drop invalid device configs */
-			if (!hwe2->vendor || !hwe2->product) {
-				condlog(0, "device config missing vendor or product parameter");
-				vector_del_slot(hw, j--);
-				free_hwe(hwe2);
-				continue;
-			}
 			if (hwe_strmatch(hwe2, hwe1) == 0) {
-				condlog(4, "%s: removing hwentry %s:%s:%s",
+				condlog(i >= n ? 1 : 3,

this check also looks wrong

libmultipath/config.c restart: · line 483

Quoted 9 lines
libmultipath/config.c +3 -2
+					"%s: duplicate device section for %s:%s:%s in %s",
 					__func__, hwe1->vendor, hwe1->product,
-					hwe1->revision);
+					hwe1->revision, table_desc);
 				vector_del_slot(hw, i);
 				merge_hwe(hwe2, hwe1);
 				free_hwe(hwe1);
-				n -= 1;
+				if (i < n)

and this one looks unnecessary.

Quoted 44 lines
libmultipath/config.c +7 -11
+					n -= 1;
 				/*
 				 * Play safe here; we have modified
 				 * the original vector so the outer
@@ -605,9 +609,8 @@ process_config_dir(struct config *conf, vector keywords, char *dir)
 		snprintf(path, LINE_MAX, "%s/%s", dir, namelist[i]->d_name);
 		path[LINE_MAX-1] = '\0';
 		process_file(conf, path);
-		if (VECTOR_SIZE(conf->hwtable) > old_hwtable_size)
-			factorize_hwtable(conf->hwtable, old_hwtable_size);
-
+		factorize_hwtable(conf->hwtable, old_hwtable_size,
+				  namelist[i]->d_name);
 	}
 	pthread_cleanup_pop(1);
 }
@@ -655,6 +658,9 @@ load_config (char * file)
 	if (setup_default_hwtable(conf->hwtable))
 		goto out;
 
+#ifdef CHECK_BUILTIN_HWTABLE
+	factorize_hwtable(conf->hwtable, 0, "builtin");
+#endif
 	/*
 	 * read the config file
 	 */
@@ -668,14 +674,7 @@ load_config (char * file)
 			condlog(0, "error parsing config file");
 			goto out;
 		}
-		if (VECTOR_SIZE(conf->hwtable) > builtin_hwtable_size) {
-			/*
-			 * remove duplica in hwtable. config file
-			 * takes precedence over build-in hwtable
-			 */
-			factorize_hwtable(conf->hwtable, builtin_hwtable_size);
-		}
-
+		factorize_hwtable(conf->hwtable, builtin_hwtable_size, file);
 	}
 
 	conf->processed_main_config = 1;
Signature · 1 line
2.17.0
#17 lore raw

Re: [PATCH 15/28] libmultipath: merge hwentries inside a conf file

Hi Ben,

Quoted 10 lines · Benjamin Marzinski
Quoted 7 lines · Martin Wilck

Merging hwentries with identical vendor/product/revision is still useful,
although it's not strictly necessary any more. But such identical entries
should always be merged, not only if they appear in different configuration
files. This requires changing the logic of factorize_hwtable.

Sorry for my slowness in reviewing these. This one looks wrong to me

Thanks for the thorough review. Given the size of the series, I don't think this was slow.

Quoted 33 lines
Quoted 24 lines

By setting -DCHECK_BUILTIN_HWTABLE at compile time, the built-in hwtable
can be checked against duplicates as well.

---
 libmultipath/config.c | 43 +++++++++++++++++++++----------------
------
 1 file changed, 21 insertions(+), 22 deletions(-)
libmultipath/config.c +1 -1
index 713ac7f3..fb41d620 100644
@@ -452,7 +452,7 @@ out:
 }
 
 static void
-factorize_hwtable (vector hw, int n)
+factorize_hwtable (vector hw, int n, const char *table_desc)
 {
 	struct hwentry *hwe1, *hwe2;
 	int i, j;

libmultipath/config.c restart: · line 466

Quoted 5 lines
libmultipath/config.c +2 -0
@@ -462,22 +462,26 @@ restart:
 		if (i == n)
 			break;
 		j = n;
+		/* drop invalid device configs */
+		if (i >= n && (!hwe1->vendor || !hwe1->product)) {

How can i >= n? vector_foreach_slot() starts i at 0, and then next lines are

Quoted 2 lines
                if (i == n)
                        break;

The above two lines, and the line "j = n" after that, were meant to be deleted. That slipped through in my rebasing work, sorry about that. When these lines are gone, I believe your issues are resolved, see below.

n denotes the number of hwentries present before the current config file was parsed. Before my patch series, "factorize_hwtable" would only check for duplicates between the "old" (i < n) and "new" (i >= n) sections, and would not remove the dups inside either section. Now duplicates are also merged if they occur in the same section (except for the built-in hwtable, for which the "inside" check is only done with -DCHECK_BUILTIN_HWTABLE).

Well - that's what I intended to do, but by by forgetting to delete the above lines, I failed :-(

In practice, this error only breaks the "broken hwentry" and "dup removal inside a section" features - application of "good" hwentries to paths and multipaths isn't affected, and thus the test cases pass despite the bug. I'll post a fix, plus new test cases covering it.

Quoted 10 lines

libmultipath/config.c restart: · line 469

Quoted 4 lines
libmultipath/config.c +3 -0
+			condlog(0, "device config in %s missing
 vendor or product parameter",
+				table_desc);
+			vector_del_slot(hw, i--);

shouldn't we also decrement n here. I realize that doesn't work well for
the CHECK_BUILTIN_HWTABLE check, where n is 0, but as I said above, I'm
pretty sure that will never get here.

No, see above. The check for broken entries is only done for "new" ones (i > n), as we assume the entries up to n to be checked already. So when we reach this condition, is is really always above n.

Quoted 7 lines

libmultipath/config.c restart: · line 473

Quoted 5 lines
libmultipath/config.c +4 -0
+			free_hwe(hwe1);
+			continue;
+		}
+		j = n > i + 1 ? n : i + 1;

again, n must always be at least i + 1 to get here, so j is always n.

See above.

Quoted 16 lines
Quoted 4 lines
libmultipath/config.c +0 -2
 		vector_foreach_slot_after(hw, hwe2, j) {
-			/* drop invalid device configs */
-			if (!hwe2->vendor || !hwe2->product) {

libmultipath/config.c restart: · line 476

Quoted 10 lines
libmultipath/config.c +1 -6
-				condlog(0, "device config missing
 vendor or product parameter");
-				vector_del_slot(hw, j--);
-				free_hwe(hwe2);
-				continue;
-			}
 			if (hwe_strmatch(hwe2, hwe1) == 0) {
-				condlog(4, "%s: removing hwentry
 %s:%s:%s",
+				condlog(i >= n ? 1 : 3,

this check also looks wrong

The intention is not to log at prio 1 if a new section (read in most
cases: multipath.conf) contains a duplicate to a previous section (read in most cases: built-in hwtable). Such use is perfectly valid, whereas two hwentries with the same vendor and product in one config file are suspicious and should be logged.

Quoted 15 lines

libmultipath/config.c restart: · line 483

Quoted 13 lines
libmultipath/config.c +3 -2
+					"%s: duplicate device
 section for %s:%s:%s in %s",
 					__func__, hwe1->vendor,
 hwe1->product,
-					hwe1->revision);
+					hwe1->revision,
 table_desc);
 				vector_del_slot(hw, i);
 				merge_hwe(hwe2, hwe1);
 				free_hwe(hwe1);
-				n -= 1;
+				if (i < n)

and this one looks unnecessary.

See above.

I order to avoid spamming dm-devel with the whole series again, I'll send patches on top of it. If you prefer that I send the full rebased series, I'll do of course, but I'll wait for more reviews.

Thanks,
Martin

Signature · 8 lines
Dr. Martin Wilck <[email protected]>, Tel. +49 (0)911 74053 2107
SUSE Linux GmbH, GF: Felix Imendörffer, Jane Smithard, Graham Norton
HRB 21284 (AG Nürnberg)

--
dm-devel mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/dm-devel
#18 lore raw

PATCH 29/30 tests/hwtable: add test for broken hwentry filtering

In reply to Benjamin Marzinski

Broken hwentries weren't correctly filtered after patch "libmultipath: merge hwentries inside a conf file". Add a test case for that.

---
 tests/hwtable.c | 50 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 50 insertions(+)
tests/hwtable.c +50 -0
index 9db79391..3488640d 100644
@@ -651,6 +651,52 @@ static int setup_string_hwe(void **state)
 	return 0;
 }
 
+/*
+ * Device section with a broken entry (no product)
+ * It should be ignored.
+ */
+static void test_broken_hwe(const struct hwt_state *hwt)
+{
+	struct path *pp;
+
+	/* foo:bar doesn't match, as hwentry is ignored */
+	pp = mock_path(vnd_foo.value, prd_bar.value);
+	TEST_PROP_BROKEN("prio", prio_name(&pp->prio), prio_emc.value,
+			 DEFAULT_PRIO);
+
+	/* boo:bar doesn't match */
+	pp = mock_path(vnd_boo.value, prd_bar.value);
+	TEST_PROP(prio_name(&pp->prio), DEFAULT_PRIO);
+}
+
+static int setup_broken_hwe(void **state)
+{
+	struct hwt_state *hwt = CHECK_STATE(state);
+	const struct key_value kv[] = { vnd_foo, prio_emc };
+
+	WRITE_ONE_DEVICE(hwt, kv);
+	SET_TEST_FUNC(hwt, test_broken_hwe);
+	return 0;
+}
+
+/*
+ * Like test_broken_hwe, but in config_dir file.
+ */
+static int setup_broken_hwe_dir(void **state)
+{
+	struct hwt_state *hwt = CHECK_STATE(state);
+	const struct key_value kv[] = { vnd_foo, prio_emc };
+
+	begin_config(hwt);
+	begin_section_all(hwt, "devices");
+	write_device(hwt->conf_dir_file[0], ARRAY_SIZE(kv), kv);
+	end_section_all(hwt);
+	finish_config(hwt);
+	hwt->test = test_broken_hwe;
+	hwt->test_name = "test_broken_hwe_dir";
+	return 0;
+}
+
 /*
  * Device section with a single regex entry ("^.foo:(bar|baz|ba\.)$")
  */
@@ -1628,6 +1674,8 @@ static int setup_multipath_config_3(void **state)
 	}
 
 define_test(string_hwe)
+define_test(broken_hwe)
+define_test(broken_hwe_dir)
 define_test(quoted_hwe)
 define_test(internal_nvme)
 define_test(regex_hwe)
@@ -1666,6 +1714,8 @@ static int test_hwtable(void)
 		cmocka_unit_test(test_sanity_globals),
 		test_entry(internal_nvme),
 		test_entry(string_hwe),
+		test_entry(broken_hwe),
+		test_entry(broken_hwe_dir),
 		test_entry(quoted_hwe),
 		test_entry(regex_hwe),
 		test_entry(regex_string_hwe),
Signature · 1 line
2.17.1
#19 lore raw

PATCH 30/30 fixup "libmultipath: merge hwentries inside a conf file"

The previous patch "libmultipath: merge hwentries inside a conf file" meant to enable checking for duplicate entries not only between different "sections" of the configuration (i.e. built-in hwtable, multipath.conf, config dir files), but also inside every section except the built-in table. This patch fixes a bug to actually implement this new behavior.

---
 libmultipath/config.c | 3 ---
 tests/hwtable.c       | 3 +--
 2 files changed, 1 insertion(+), 5 deletions(-)
libmultipath/config.c +0 -3
index 28b76dd2..50826a1d 100644
@@ -527,9 +527,6 @@ factorize_hwtable (vector hw, int n, const char *table_desc)
 
 restart:
 	vector_foreach_slot(hw, hwe1, i) {
-		if (i == n)
-			break;
-		j = n;
 		/* drop invalid device configs */
 		if (i >= n && (!hwe1->vendor || !hwe1->product)) {
 			condlog(0, "device config in %s missing vendor or product parameter",
tests/hwtable.c +1 -2
index 3488640d..42127adf 100644
@@ -661,8 +661,7 @@ static void test_broken_hwe(const struct hwt_state *hwt)
 
 	/* foo:bar doesn't match, as hwentry is ignored */
 	pp = mock_path(vnd_foo.value, prd_bar.value);
-	TEST_PROP_BROKEN("prio", prio_name(&pp->prio), prio_emc.value,
-			 DEFAULT_PRIO);
+	TEST_PROP(prio_name(&pp->prio), DEFAULT_PRIO);
 
 	/* boo:bar doesn't match */
 	pp = mock_path(vnd_boo.value, prd_bar.value);
Signature · 1 line
2.17.1
#20 lore raw

PATCH 16/28 libmultipath/hwtable: remove inherited props from ONTAP NVMe

In reply to Martin Wilck

With the new hwtable code, properties inherited from generic NVMe don't need to be duplicated.

---
 libmultipath/hwtable.c | 3 ---
 1 file changed, 3 deletions(-)
libmultipath/hwtable.c +0 -3
index 148f0ba2..d20ed194 100644
@@ -746,11 +746,8 @@ static struct hwentry default_hw[] = {
 		 */
 		.vendor	       = "NVME",
 		.product       = "^NetApp ONTAP Controller",
-		.uid_attribute = "ID_WWN",
-		.checker_name  = NONE,
 		.pgpolicy      = MULTIBUS,
 		.no_path_retry = NO_PATH_RETRY_QUEUE,
-		.retain_hwhandler = RETAIN_HWHANDLER_OFF,
 	},
 	/*
 	 * Nexenta
Signature · 1 line
2.17.0
#21 lore raw

PATCH 17/28 libmultipath: don't merge by regex in setup_default_blist()

In reply to Martin Wilck

The merging of blacklist entries by regular expression leads to similar problems as the merging of hwentries. Only merge blacklist entries if they're exactly equal.

---
 libmultipath/blacklist.c | 23 +++++++++++++++++++++--
 tests/hwtable.c          |  8 +-------
 2 files changed, 22 insertions(+), 9 deletions(-)
libmultipath/blacklist.c +21 -2
index 34a7e717..91ed7ddf 100644
@@ -163,6 +163,25 @@ _blacklist_device (const struct _vector *blist, const char * vendor,
 	return 0;
 }
 
+static int
+find_blacklist_device (const struct _vector *blist, const char * vendor,
+		       const char * product)
+{
+	int i;
+	struct blentry_device * ble;
+
+	vector_foreach_slot (blist, ble, i) {
+		if (((!vendor && !ble->vendor) ||
+		     (vendor && ble->vendor &&
+		      !strcmp(vendor, ble->vendor))) &&
+		    ((!product && !ble->product) ||
+		     (product && ble->product &&
+		      !strcmp(product, ble->product))))
+			return 1;
+	}
+	return 0;
+}
+
 int
 setup_default_blist (struct config * conf)
 {
@@ -191,8 +210,8 @@ setup_default_blist (struct config * conf)
 
 	vector_foreach_slot (conf->hwtable, hwe, i) {
 		if (hwe->bl_product) {
-			if (_blacklist_device(conf->blist_device, hwe->vendor,
-					      hwe->bl_product))
+			if (find_blacklist_device(conf->blist_device,
+						  hwe->vendor, hwe->bl_product))
 				continue;
 			if (alloc_ble_device(conf->blist_device))
 				return 1;
tests/hwtable.c +1 -7
index 15f364e4..85215946 100644
@@ -1399,13 +1399,7 @@ static int setup_product_blacklist(void **state)
 static void test_product_blacklist_matching(const struct hwt_state *hwt)
 {
 	mock_path_flags(vnd_foo.value, prd_bar.value, BL_BY_DEVICE);
-#if BROKEN == 1
-	condlog(1, "%s: WARNING: broken blacklist test on line %d",
-		__func__, __LINE__ + 1);
-	mock_path(vnd_foo.value, prd_baz.value);
-#else
-	mock_path_blacklisted(vnd_foo.value, prd_baz.value);
-#endif
+	mock_path_flags(vnd_foo.value, prd_baz.value, BL_BY_DEVICE);
 	mock_path(vnd_foo.value, prd_bam.value);
 }
 
Signature · 1 line
2.17.0
#22 lore raw

PATCH 18/28 multipath, multipathd: consolidate config dumping

In reply to Martin Wilck

"multipath -t" and "multipathd show config" use very similar code. Consolidate it into a libmultipath function. Simplify it a bit in the process, and do some "const" and "static" cleanup.

---
 libmultipath/print.c      | 76 ++++++++++++++++++++++++++++++++++-----
 libmultipath/print.h      |  7 +---
 multipath/main.c          | 63 ++++----------------------------
 multipathd/cli_handlers.c | 52 ++-------------------------
 4 files changed, 78 insertions(+), 120 deletions(-)
libmultipath/print.c +67 -9
index b1844c98..7d4e2ace 100644
@@ -1342,7 +1342,8 @@ snprint_multipath_topology_json (char * buff, int len, const struct vectors * ve
 }
 
 static int
-snprint_hwentry (struct config *conf, char * buff, int len, const struct hwentry * hwe)
+snprint_hwentry (const struct config *conf,
+		 char * buff, int len, const struct hwentry * hwe)
 {
 	int i;
 	int fwd = 0;
@@ -1374,7 +1375,8 @@ snprint_hwentry (struct config *conf, char * buff, int len, const struct hwentry
 	return fwd;
 }
 
-int snprint_hwtable(struct config *conf, char *buff, int len, vector hwtable)
+static int snprint_hwtable(const struct config *conf,
+			   char *buff, int len, vector hwtable)
 {
 	int fwd = 0;
 	int i;
@@ -1400,7 +1402,8 @@ int snprint_hwtable(struct config *conf, char *buff, int len, vector hwtable)
 }
 
 static int
-snprint_mpentry (struct config *conf, char * buff, int len, const struct mpentry * mpe)
+snprint_mpentry (const struct config *conf, char * buff, int len,
+		 const struct mpentry * mpe)
 {
 	int i;
 	int fwd = 0;
@@ -1426,7 +1429,8 @@ snprint_mpentry (struct config *conf, char * buff, int len, const struct mpentry
 	return fwd;
 }
 
-int snprint_mptable(struct config *conf, char *buff, int len, vector mptable)
+static int snprint_mptable(const struct config *conf,
+			   char *buff, int len, vector mptable)
 {
 	int fwd = 0;
 	int i;
@@ -1451,8 +1455,8 @@ int snprint_mptable(struct config *conf, char *buff, int len, vector mptable)
 	return fwd;
 }
 
-int snprint_overrides(struct config *conf, char * buff, int len,
-		      const struct hwentry *overrides)
+static int snprint_overrides(const struct config *conf, char * buff, int len,
+			     const struct hwentry *overrides)
 {
 	int fwd = 0;
 	int i;
@@ -1481,7 +1485,7 @@ out:
 	return fwd;
 }
 
-int snprint_defaults(struct config *conf, char *buff, int len)
+static int snprint_defaults(const struct config *conf, char *buff, int len)
 {
 	int fwd = 0;
 	int i;
@@ -1624,7 +1628,7 @@ int snprint_blacklist_report(struct config *conf, char *buff, int len)
 	return fwd;
 }
 
-int snprint_blacklist(struct config *conf, char *buff, int len)
+static int snprint_blacklist(const struct config *conf, char *buff, int len)
 {
 	int i;
 	struct blentry * ble;
@@ -1700,7 +1704,8 @@ int snprint_blacklist(struct config *conf, char *buff, int len)
 	return fwd;
 }
 
-int snprint_blacklist_except(struct config *conf, char *buff, int len)
+static int snprint_blacklist_except(const struct config *conf,
+				    char *buff, int len)
 {
 	int i;
 	struct blentry * ele;
@@ -1776,6 +1781,59 @@ int snprint_blacklist_except(struct config *conf, char *buff, int len)
 	return fwd;
 }
 
+char *snprint_config(const struct config *conf, int *len)
+{
+	char *reply;
+	/* built-in config is >20kB already */
+	unsigned int maxlen = 32768;
+
+	for (reply = NULL; maxlen <= UINT_MAX/2; maxlen *= 2) {
+		char *c, *tmp = reply;
+
+		reply = REALLOC(reply, maxlen);
+		if (!reply) {
+			if (tmp)
+				free(tmp);
+			return NULL;
+		}
+
+		c = reply + snprint_defaults(conf, reply, maxlen);
+		if ((c - reply) == maxlen)
+			continue;
+
+		c += snprint_blacklist(conf, c, reply + maxlen - c);
+		if ((c - reply) == maxlen)
+			continue;
+
+		c += snprint_blacklist_except(conf, c, reply + maxlen - c);
+		if ((c - reply) == maxlen)
+			continue;
+
+		c += snprint_hwtable(conf, c, reply + maxlen - c,
+				     conf->hwtable);
+		if ((c - reply) == maxlen)
+			continue;
+
+		c += snprint_overrides(conf, c, reply + maxlen - c,
+				       conf->overrides);
+		if ((c - reply) == maxlen)
+			continue;
+
+		if (VECTOR_SIZE(conf->mptable) > 0)
+			c += snprint_mptable(conf, c, reply + maxlen - c,
+					     conf->mptable);
+
+		if ((c - reply) < maxlen) {
+			if (len)
+				*len = c - reply;
+			return reply;
+		}
+	}
+
+	free(reply);
+	return NULL;
+}
+
 int snprint_status(char *buff, int len, const struct vectors *vecs)
 {
 	int fwd = 0;
libmultipath/print.h +1 -6
index 9b5a23aa..fed80d55 100644
@@ -119,18 +119,13 @@ int _snprint_multipath_topology (const struct gen_multipath *, char *, int,
 	_snprint_multipath_topology (dm_multipath_to_gen(mpp), buf, len, v)
 int snprint_multipath_topology_json (char * buff, int len,
 				const struct vectors * vecs);
+char *snprint_config(const struct config *conf, int *len);
 int snprint_multipath_map_json (char * buff, int len,
 				const struct multipath * mpp, int last);
-int snprint_defaults (struct config *, char *, int);
-int snprint_blacklist (struct config *, char *, int);
-int snprint_blacklist_except (struct config *, char *, int);
 int snprint_blacklist_report (struct config *, char *, int);
 int snprint_wildcards (char *, int);
 int snprint_status (char *, int, const struct vectors *);
 int snprint_devices (struct config *, char *, int, const struct vectors *);
-int snprint_hwtable (struct config *, char *, int, const vector);
-int snprint_mptable (struct config *, char *, int, const vector);
-int snprint_overrides (struct config *, char *, int, const struct hwentry *);
 int snprint_path_serial (char *, size_t, const struct path *);
 int snprint_host_wwnn (char *, size_t, const struct path *);
 int snprint_host_wwpn (char *, size_t, const struct path *);
multipath/main.c +7 -56

70 lines not on this page · raw message

multipathd/cli_handlers.c +3 -49

65 lines not on this page · raw message

Signature · 1 line
2.17.0
#23 lore raw

PATCH 19/28 tests/hwtable: implement configuration dump + reload test

In reply to Martin Wilck

In order to test dumping and re-reading the configuration, we modify the test_driver() function so that runs the same test three twice:

1) with "user-supplied" configuration,
2) using the config dump from 1) (equivalent with "multipath -t" output) as input configuration.

The expectation is that actual paths and maps have same options applied. Furthermore, the "multipath -T" output from 2) should be the same as from 1), IOW, "multipath -T" in 2) reproduces its input config file exactly.

---
 tests/hwtable.c | 88 +++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 86 insertions(+), 2 deletions(-)
tests/hwtable.c +86 -2
index 85215946..85924fb7 100644
@@ -23,6 +23,7 @@
 #include "defaults.h"
 #include "pgpolicies.h"
 #include "test-lib.h"
+#include "print.h"
 
 #define ARRAY_SIZE(x) (sizeof(x)/sizeof((x)[0]))
 #define N_CONF_FILES 2
@@ -358,6 +359,16 @@ static void write_device(FILE *ff, int nkv, const struct key_value *kv)
 		conf = NULL;			\
 	} while (0)
 
+static void replace_config(const struct hwt_state *hwt,
+			   const char *conf_str)
+{
+	FREE_CONFIG(_conf);
+	reset_configs(hwt);
+	fprintf(hwt->config_file, "%s", conf_str);
+	fflush(hwt->config_file);
+	_conf = LOAD_CONFIG(hwt);
+}
+
 #define TEST_PROP(prop, val) do {				\
 		if (val == NULL)				\
 			assert_ptr_equal(prop, NULL);		\
@@ -432,8 +443,58 @@ static const struct key_value npr_queue = { _no_path_retry, "queue" };
 /***** BEGIN TESTS SECTION *****/
 
 /*
- * Run the given test case. This will later be extended
- * to run the test several times in a row.
+ * Dump the configuration, subistitute the dumped configuration
+ * for the current one, and verify that the result is identical.
+ */
+static void replicate_config(const struct hwt_state *hwt)
+{
+	char *cfg1, *cfg2;
+	struct config *conf;
+
+	condlog(1, "--- %s: replicating configuration", __func__);
+
+	conf = get_multipath_config();
+	cfg1 = snprint_config(conf, NULL);
+
+	assert_non_null(cfg1);
+	put_multipath_config(conf);
+
+	replace_config(hwt, cfg1);
+
+	conf = get_multipath_config();
+	cfg2 = snprint_config(conf, NULL);
+	assert_non_null(cfg2);
+	put_multipath_config(conf);
+
+// #define DBG_CONFIG 1
+#ifdef DBG_CONFIG
+#define DUMP_CFG_STR(x) do {						\
+		FILE *tmp = fopen("/tmp/hwtable-" #x ".txt", "w");	\
+		fprintf(tmp, "%s", x);					\
+		fclose(tmp);						\
+	} while (0)
+
+	DUMP_CFG_STR(cfg1);
+	DUMP_CFG_STR(cfg2);
+#endif
+
+#if BROKEN
+	condlog(1, "%s: WARNING: skipping tests for same configuration after dump/reload on %d",
+		__func__, __LINE__);
+#else
+	assert_int_equal(strlen(cfg2), strlen(cfg1));
+	assert_string_equal(cfg2, cfg1);
+#endif
+	free(cfg1);
+	free(cfg2);
+}
+
+/*
+ * Run hwt->test three times; once with the constructed configuration,
+ * once after re-reading the full dumped configuration, and once with the
+ * dumped local configuration.
+ *
+ * Expected: test passes every time.
  */
 static void test_driver(void **state)
 {
@@ -443,6 +504,10 @@ static void test_driver(void **state)
 	_conf = LOAD_CONFIG(hwt);
 	hwt->test(hwt);
 
+	replicate_config(hwt);
+	reset_vecs(hwt->vecs);
+	hwt->test(hwt);
+
 	reset_vecs(hwt->vecs);
 	FREE_CONFIG(_conf);
 }
@@ -513,9 +578,18 @@ static int setup_internal_nvme(void **state)
 /*
  * Device section with a simple entry qith double quotes ('foo:"bar"')
  */
+#if BROKEN
+static void test_quoted_hwe(void **state)
+#else
 static void test_quoted_hwe(const struct hwt_state *hwt)
+#endif
 {
 	struct path *pp;
+#if BROKEN
+       struct hwt_state *hwt = CHECK_STATE(state);
+
+       _conf = LOAD_CONFIG(hwt);
+#endif
 	/* foo:"bar" matches */
 	pp = mock_path(vnd_foo.value, prd_baq.value);
 	TEST_PROP(prio_name(&pp->prio), prio_emc.value);
@@ -531,7 +605,11 @@ static int setup_quoted_hwe(void **state)
 	const struct key_value kv[] = { vnd_foo, prd_baqq, prio_emc };
 
 	WRITE_ONE_DEVICE(hwt, kv);
+#if BROKEN
+       condlog(0, "%s: WARNING: skipping conf reload test", __func__);
+#else
 	SET_TEST_FUNC(hwt, test_quoted_hwe);
+#endif
 	return 0;
 }
 
@@ -1558,7 +1636,9 @@ static int setup_multipath_config_3(void **state)
 	}
 
 define_test(string_hwe)
+#if !BROKEN
 define_test(quoted_hwe)
+#endif
 define_test(internal_nvme)
 define_test(regex_hwe)
 define_test(regex_string_hwe)
@@ -1596,7 +1676,11 @@ static int test_hwtable(void)
 		cmocka_unit_test(test_sanity_globals),
 		test_entry(internal_nvme),
 		test_entry(string_hwe),
+#if BROKEN
+		cmocka_unit_test_setup(test_quoted_hwe, setup_quoted_hwe),
+#else
 		test_entry(quoted_hwe),
+#endif
 		test_entry(regex_hwe),
 		test_entry(regex_string_hwe),
 		test_entry(regex_string_hwe_dir),
Signature · 1 line
2.17.0
#24 lore raw

PATCH 20/28 libmultipath: allow dumping only "local" hwtable in snprint_config

In reply to Martin Wilck

This change is key for allowing to dump the "local" multipath configuration only, i.e. those entries of the hardware table that match existing devices in the system. By passing the output of the new helper function get_used_hwes() as the last argument for snprint_config(), only the local entries will be dumped.

The rationale is that "multipath -t" output is overwhelmingly long for most use cases; in particular it's not useful as a template for creating a local configuration.

---
 libmultipath/print.c       |  8 +++++---
 libmultipath/print.h       |  3 ++-
 libmultipath/structs_vec.c | 19 +++++++++++++++++++
 libmultipath/structs_vec.h |  1 +
 libmultipath/vector.c      | 12 ++++++++++++
 libmultipath/vector.h      |  1 +
 multipath/main.c           |  2 +-
 multipathd/cli_handlers.c  |  8 ++++----
 tests/hwtable.c            |  4 ++--
 9 files changed, 47 insertions(+), 11 deletions(-)
libmultipath/print.c +5 -3
index 7d4e2ace..af4c00e2 100644
@@ -1376,7 +1376,8 @@ snprint_hwentry (const struct config *conf,
 }
 
 static int snprint_hwtable(const struct config *conf,
-			   char *buff, int len, vector hwtable)
+			   char *buff, int len,
+			   const struct _vector *hwtable)
 {
 	int fwd = 0;
 	int i;
@@ -1781,7 +1782,8 @@ static int snprint_blacklist_except(const struct config *conf,
 	return fwd;
 }
 
-char *snprint_config(const struct config *conf, int *len)
+char *snprint_config(const struct config *conf, int *len,
+		     const struct _vector *hwtable)
 {
 	char *reply;
 	/* built-in config is >20kB already */
@@ -1810,7 +1812,7 @@ char *snprint_config(const struct config *conf, int *len)
 			continue;
 
 		c += snprint_hwtable(conf, c, reply + maxlen - c,
-				     conf->hwtable);
+				     hwtable ? hwtable : conf->hwtable);
 		if ((c - reply) == maxlen)
 			continue;
 
libmultipath/print.h +2 -1
index fed80d55..c63e0547 100644
@@ -119,7 +119,8 @@ int _snprint_multipath_topology (const struct gen_multipath *, char *, int,
 	_snprint_multipath_topology (dm_multipath_to_gen(mpp), buf, len, v)
 int snprint_multipath_topology_json (char * buff, int len,
 				const struct vectors * vecs);
-char *snprint_config(const struct config *conf, int *len);
+char *snprint_config(const struct config *conf, int *len,
+		     const struct _vector *hwtable);
 int snprint_multipath_map_json (char * buff, int len,
 				const struct multipath * mpp, int last);
 int snprint_blacklist_report (struct config *, char *, int);
libmultipath/structs_vec.c +19 -0
index 38f04387..f87d69d4 100644
@@ -445,3 +445,22 @@ void update_queue_mode_add_path(struct multipath *mpp)
 	}
 	condlog(2, "%s: remaining active paths: %d", mpp->alias, mpp->nr_active);
 }
+
+vector get_used_hwes(const struct _vector *pathvec)
+{
+	int i, j;
+	struct path *pp;
+	struct hwentry *hwe;
+	vector v = vector_alloc();
+
+	if (v == NULL)
+		return NULL;
+
+	vector_foreach_slot(pathvec, pp, i) {
+		vector_foreach_slot_backwards(pp->hwe, hwe, j) {
+			vector_find_or_add_slot(v, hwe);
+		}
+	}
+
+	return v;
+}
libmultipath/structs_vec.h +1 -0
index 4220ea33..f7777aaf 100644
@@ -38,5 +38,6 @@ void update_queue_mode_add_path(struct multipath *mpp);
 int update_multipath_table (struct multipath *mpp, vector pathvec,
 			    int is_daemon);
 int update_multipath_status (struct multipath *mpp);
+vector get_used_hwes(const struct _vector *pathvec);
 
 #endif /* _STRUCTS_VEC_H */
libmultipath/vector.c +12 -0
index f741ae08..501cf4c5 100644
@@ -196,3 +196,15 @@ vector_set_slot(vector v, void *value)
 	i = VECTOR_SIZE(v) - 1;
 	v->slot[i] = value;
 }
+
+int vector_find_or_add_slot(vector v, void *value)
+{
+	int n = find_slot(v, value);
+
+	if (n >= 0)
+		return n;
+	if (vector_alloc_slot(v) == NULL)
+		return -1;
+	vector_set_slot(v, value);
+	return VECTOR_SIZE(v) - 1;
+}
libmultipath/vector.h +1 -0
index b9450ac8..41d2b896 100644
@@ -82,6 +82,7 @@ extern void vector_set_slot(vector v, void *value);
 extern void vector_del_slot(vector v, int slot);
 extern void *vector_insert_slot(vector v, int slot, void *value);
 int find_slot(vector v, void * addr);
+int vector_find_or_add_slot(vector v, void *value);
 extern void vector_repack(vector v);
 extern void vector_dump(vector v);
 extern void dump_strvec(vector strvec);
multipath/main.c +1 -1
index 288251c3..87d80dd3 100644
@@ -753,7 +753,7 @@ out:
 static int
 dump_config (struct config *conf)
 {
-	char * reply = snprint_config(conf, NULL);
+	char * reply = snprint_config(conf, NULL, NULL);
 
 	if (reply != NULL) {
 		printf("%s", reply);
multipathd/cli_handlers.c +4 -4
index 6f043d7f..75cc8a9e 100644
@@ -245,15 +245,15 @@ show_map_json (char ** r, int * len, struct multipath * mpp,
 	return 0;
 }
 
-int
-show_config (char ** r, int * len)
+static int
+show_config (char ** r, int * len, const struct _vector *hwtable)
 {
 	struct config *conf;
 	char *reply;
 
 	conf = get_multipath_config();
 	pthread_cleanup_push(put_multipath_config, conf);
-	reply = snprint_config(conf, len);
+	reply = snprint_config(conf, len, hwtable);
 	pthread_cleanup_pop(1);
 	if (reply == NULL)
 		return 1;
@@ -277,7 +277,7 @@ cli_list_config (void * v, char ** reply, int * len, void * data)
 {
 	condlog(3, "list config (operator)");
 
-	return show_config(reply, len);
+	return show_config(reply, len, NULL);
 }
 
 int
tests/hwtable.c +2 -2
index 85924fb7..3c3caddd 100644
@@ -454,7 +454,7 @@ static void replicate_config(const struct hwt_state *hwt)
 	condlog(1, "--- %s: replicating configuration", __func__);
 
 	conf = get_multipath_config();
-	cfg1 = snprint_config(conf, NULL);
+	cfg1 = snprint_config(conf, NULL, NULL);
 
 	assert_non_null(cfg1);
 	put_multipath_config(conf);
@@ -462,7 +462,7 @@ static void replicate_config(const struct hwt_state *hwt)
 	replace_config(hwt, cfg1);
 
 	conf = get_multipath_config();
-	cfg2 = snprint_config(conf, NULL);
+	cfg2 = snprint_config(conf, NULL, NULL);
 	assert_non_null(cfg2);
 	put_multipath_config(conf);
 
Signature · 1 line
2.17.0
#25 lore raw

PATCH 21/28 tests/hwtable: add test for local configuration dump

In reply to Martin Wilck

This adds another test iteration in test_driver(): It now tests:

1) with "user-supplied" configuration,
2) using the full config dump (equivalent with "multipath -t" output),
3) using the local config dump as implemented in the previous patch.

Again, the properties of paths and maps should be the same for all 3 scenarios. For 3), the "multipath -t" output can't be expected to be exactly equal to the output in 1), because merging of hwentries may move the position of a hwentry down in the hwentry list, and empty "multipath" sections are being added.

---
 tests/hwtable.c | 33 +++++++++++++++++++++++++++++----
 1 file changed, 29 insertions(+), 4 deletions(-)
tests/hwtable.c +29 -4
index 3c3caddd..08ed67d3 100644
@@ -446,21 +446,42 @@ static const struct key_value npr_queue = { _no_path_retry, "queue" };
  * Dump the configuration, subistitute the dumped configuration
  * for the current one, and verify that the result is identical.
  */
-static void replicate_config(const struct hwt_state *hwt)
+static void replicate_config(const struct hwt_state *hwt, bool local)
 {
 	char *cfg1, *cfg2;
+	vector hwtable;
 	struct config *conf;
 
-	condlog(1, "--- %s: replicating configuration", __func__);
+	condlog(1, "--- %s: replicating %s configuration", __func__,
+		local ? "local" : "full");
 
 	conf = get_multipath_config();
-	cfg1 = snprint_config(conf, NULL, NULL);
+	if (!local)
+		/* "full" configuration */
+		cfg1 = snprint_config(conf, NULL, NULL);
+	else {
+		/* "local" configuration */
+		hwtable = get_used_hwes(hwt->vecs->pathvec);
+		cfg1 = snprint_config(conf, NULL, hwtable);
+	}
 
 	assert_non_null(cfg1);
 	put_multipath_config(conf);
 
 	replace_config(hwt, cfg1);
 
+	/*
+	 * The local configuration adds multipath entries, and may move device
+	 * entries for local devices to the end of the list. Identical config
+	 * strings therefore can't be expected in the "local" case.
+	 * That doesn't matter. The important thing is that, with the reloaded
+	 * configuration, the test case still passes.
+	 */
+	if (local) {
+		free(cfg1);
+		return;
+	}
+
 	conf = get_multipath_config();
 	cfg2 = snprint_config(conf, NULL, NULL);
 	assert_non_null(cfg2);
@@ -504,7 +525,11 @@ static void test_driver(void **state)
 	_conf = LOAD_CONFIG(hwt);
 	hwt->test(hwt);
 
-	replicate_config(hwt);
+	replicate_config(hwt, false);
+	reset_vecs(hwt->vecs);
+	hwt->test(hwt);
+
+	replicate_config(hwt, true);
 	reset_vecs(hwt->vecs);
 	hwt->test(hwt);
 
Signature · 1 line
2.17.0
#26 lore raw

PATCH 22/28 libmultipath: allow printing local maps in snprint_config

In reply to Martin Wilck

"mulitpatd show config" only dumps multipath sections for maps which are present in local configuration files. This patch adds the ability to dump "multipath" subsections for every locally detected map, even for those that have no local configuration, and at the same time, skip dumping multipath configurations for maps that aree not present locally.

This makes it possible for users to generate a multipath.conf template matching local devices, and e.g. add explicit alias entries for existing maps. If user_friendly_names is in use, a commented-out config line is added to the dump output showing the implicitly configured alias, simplifying identification of the devices.

This facility is optional and will only be used in new commands added in follow-up patches.

---
 libmultipath/print.c      | 60 ++++++++++++++++++++++++++++++++++-----
 libmultipath/print.h      |  3 +-
 multipath/main.c          |  2 +-
 multipathd/cli_handlers.c |  2 +-
 tests/hwtable.c           |  6 ++--
 5 files changed, 60 insertions(+), 13 deletions(-)
libmultipath/print.c +53 -7
index af4c00e2..222d2701 100644
@@ -1404,12 +1404,16 @@ static int snprint_hwtable(const struct config *conf,
 
 static int
 snprint_mpentry (const struct config *conf, char * buff, int len,
-		 const struct mpentry * mpe)
+		 const struct mpentry * mpe, const struct _vector *mpvec)
 {
 	int i;
 	int fwd = 0;
 	struct keyword * kw;
 	struct keyword * rootkw;
+	struct multipath *mpp = NULL;
+
+	if (mpvec != NULL && (mpp = find_mp_by_wwid(mpvec, mpe->wwid)) == NULL)
+		return 0;
 
 	rootkw = find_keyword(conf->keywords, NULL, "multipath");
 	if (!rootkw)
@@ -1424,6 +1428,15 @@ snprint_mpentry (const struct config *conf, char * buff, int len,
 		if (fwd >= len)
 			return len;
 	}
+	/*
+	 * This mpp doesn't have alias defined. Add the alias in a comment.
+	 */
+	if (mpp != NULL && strcmp(mpp->alias, mpp->wwid)) {
+		fwd += snprintf(buff + fwd, len - fwd, "\t\t# alias \"%s\"\n",
+				mpp->alias);
+		if (fwd >= len)
+			return len;
+	}
 	fwd += snprintf(buff + fwd, len - fwd, "\t}\n");
 	if (fwd >= len)
 		return len;
@@ -1431,7 +1444,7 @@ snprint_mpentry (const struct config *conf, char * buff, int len,
 }
 
 static int snprint_mptable(const struct config *conf,
-			   char *buff, int len, vector mptable)
+			   char *buff, int len, const struct _vector *mpvec)
 {
 	int fwd = 0;
 	int i;
@@ -1445,11 +1458,43 @@ static int snprint_mptable(const struct config *conf,
 	fwd += snprintf(buff + fwd, len - fwd, "multipaths {\n");
 	if (fwd >= len)
 		return len;
-	vector_foreach_slot (mptable, mpe, i) {
-		fwd += snprint_mpentry(conf, buff + fwd, len - fwd, mpe);
+	vector_foreach_slot (conf->mptable, mpe, i) {
+		fwd += snprint_mpentry(conf, buff + fwd, len - fwd, mpe, mpvec);
 		if (fwd >= len)
 			return len;
 	}
+	if (mpvec != NULL) {
+		struct multipath *mpp;
+
+		vector_foreach_slot(mpvec, mpp, i) {
+			if (find_mpe(conf->mptable, mpp->wwid) != NULL)
+				continue;
+
+			fwd += snprintf(buff + fwd, len - fwd,
+					"\tmultipath {\n");
+			if (fwd >= len)
+				return len;
+			fwd += snprintf(buff + fwd, len - fwd,
+					"\t\twwid \"%s\"\n", mpp->wwid);
+			if (fwd >= len)
+				return len;
+			/*
+			 * This mpp doesn't have alias defined in
+			 * multipath.conf - otherwise find_mpe would have
+			 * found it. Add the alias in a comment.
+			 */
+			if (strcmp(mpp->alias, mpp->wwid)) {
+				fwd += snprintf(buff + fwd, len - fwd,
+						"\t\t# alias \"%s\"\n",
+						mpp->alias);
+				if (fwd >= len)
+					return len;
+			}
+			fwd += snprintf(buff + fwd, len - fwd, "\t}\n");
+			if (fwd >= len)
+				return len;
+		}
+	}
 	fwd += snprintf(buff + fwd, len - fwd, "}\n");
 	if (fwd >= len)
 		return len;
@@ -1783,7 +1828,7 @@ static int snprint_blacklist_except(const struct config *conf,
 }
 
 char *snprint_config(const struct config *conf, int *len,
-		     const struct _vector *hwtable)
+		     const struct _vector *hwtable, const struct _vector *mpvec)
 {
 	char *reply;
 	/* built-in config is >20kB already */
@@ -1821,9 +1866,10 @@ char *snprint_config(const struct config *conf, int *len,
 		if ((c - reply) == maxlen)
 			continue;
 
-		if (VECTOR_SIZE(conf->mptable) > 0)
+		if (VECTOR_SIZE(conf->mptable) > 0 ||
+		    (mpvec != NULL && VECTOR_SIZE(mpvec) > 0))
 			c += snprint_mptable(conf, c, reply + maxlen - c,
-					     conf->mptable);
+					     mpvec);
 
 		if ((c - reply) < maxlen) {
 			if (len)
libmultipath/print.h +2 -1
index c63e0547..608b7d5f 100644
@@ -120,7 +120,8 @@ int _snprint_multipath_topology (const struct gen_multipath *, char *, int,
 int snprint_multipath_topology_json (char * buff, int len,
 				const struct vectors * vecs);
 char *snprint_config(const struct config *conf, int *len,
-		     const struct _vector *hwtable);
+		     const struct _vector *hwtable,
+		     const struct _vector *mpvec);
 int snprint_multipath_map_json (char * buff, int len,
 				const struct multipath * mpp, int last);
 int snprint_blacklist_report (struct config *, char *, int);
multipath/main.c +1 -1
index 87d80dd3..54a98026 100644
@@ -753,7 +753,7 @@ out:
 static int
 dump_config (struct config *conf)
 {
-	char * reply = snprint_config(conf, NULL, NULL);
+	char * reply = snprint_config(conf, NULL, NULL, NULL);
 
 	if (reply != NULL) {
 		printf("%s", reply);
multipathd/cli_handlers.c +1 -1
index 75cc8a9e..0ac00155 100644
@@ -253,7 +253,7 @@ show_config (char ** r, int * len, const struct _vector *hwtable)
 
 	conf = get_multipath_config();
 	pthread_cleanup_push(put_multipath_config, conf);
-	reply = snprint_config(conf, len, hwtable);
+	reply = snprint_config(conf, len, hwtable, NULL);
 	pthread_cleanup_pop(1);
 	if (reply == NULL)
 		return 1;
tests/hwtable.c +3 -3
index 08ed67d3..1a6b3188 100644
@@ -458,11 +458,11 @@ static void replicate_config(const struct hwt_state *hwt, bool local)
 	conf = get_multipath_config();
 	if (!local)
 		/* "full" configuration */
-		cfg1 = snprint_config(conf, NULL, NULL);
+		cfg1 = snprint_config(conf, NULL, NULL, NULL);
 	else {
 		/* "local" configuration */
 		hwtable = get_used_hwes(hwt->vecs->pathvec);
-		cfg1 = snprint_config(conf, NULL, hwtable);
+		cfg1 = snprint_config(conf, NULL, hwtable, hwt->vecs->mpvec);
 	}
 
 	assert_non_null(cfg1);
@@ -483,7 +483,7 @@ static void replicate_config(const struct hwt_state *hwt, bool local)
 	}
 
 	conf = get_multipath_config();
-	cfg2 = snprint_config(conf, NULL, NULL);
+	cfg2 = snprint_config(conf, NULL, NULL, NULL);
 	assert_non_null(cfg2);
 	put_multipath_config(conf);
 
Signature · 1 line
2.17.0
#27 lore raw

PATCH 23/28 multipathd: implement "show config local"

In reply to Martin Wilck

This new command is like "show config", but only those "device" sections are dumped that match actually present devices in the system. Furthermore, empty "multipath" sections for all detected multipath maps are dumped. This way, the output is suitable as a template for creating "multipath.conf".

"multipathd show config local" should produce output that, when used as configuration file, creates the same configuration as was in place while it was dumped. Add a test for this to the test suite.

Some minor differences in the configuration dump can't be avoided because the ordering of hwtable entries may change (e.g. if a user adds a device entry for a device which is also in the built-in hwtable, these entries will be merged end the merged entry will be at the position of the new entry, i.e. after all built-in hwtable entries), and multipath sections are added to the configuration. But by all means, path and mulitpath objects should have the same properties than before.

---
 multipathd/cli.c          |  2 ++
 multipathd/cli.h          |  2 ++
 multipathd/cli_handlers.c | 28 +++++++++++++++++++++++++---
 multipathd/cli_handlers.h |  1 +
 multipathd/main.c         |  1 +
 multipathd/multipathd.8   |  5 +++++
 6 files changed, 36 insertions(+), 3 deletions(-)
multipathd/cli.c +2 -0
index d5ee4ff0..a75afe3f 100644
@@ -212,6 +212,7 @@ load_keys (void)
 	r += add_key(keys, "setprkey", SETPRKEY, 0);
 	r += add_key(keys, "unsetprkey", UNSETPRKEY, 0);
 	r += add_key(keys, "key", KEY, 1);
+	r += add_key(keys, "local", LOCAL, 0);
 
 
 	if (r) {
@@ -549,6 +550,7 @@ cli_init (void) {
 	add_handler(LIST+MAP+FMT, NULL);
 	add_handler(LIST+MAP+RAW+FMT, NULL);
 	add_handler(LIST+CONFIG, NULL);
+	add_handler(LIST+CONFIG+LOCAL, NULL);
 	add_handler(LIST+BLACKLIST, NULL);
 	add_handler(LIST+DEVICES, NULL);
 	add_handler(LIST+WILDCARDS, NULL);
multipathd/cli.h +2 -0
index 021f25b7..7cc7e4be 100644
@@ -44,6 +44,7 @@ enum {
 	__SETPRKEY,
 	__UNSETPRKEY,
 	__KEY,
+	__LOCAL,
 };
 
 #define LIST		(1 << __LIST)
@@ -87,6 +88,7 @@ enum {
 #define SETPRKEY	(1ULL << __SETPRKEY)
 #define UNSETPRKEY	(1ULL << __UNSETPRKEY)
 #define KEY		(1ULL << __KEY)
+#define LOCAL		(1ULL << __LOCAL)
 
 #define INITIAL_REPLY_LEN	1200
 
multipathd/cli_handlers.c +25 -3
index 0ac00155..a6ee92b1 100644
@@ -246,14 +246,15 @@ show_map_json (char ** r, int * len, struct multipath * mpp,
 }
 
 static int
-show_config (char ** r, int * len, const struct _vector *hwtable)
+show_config (char ** r, int * len, const struct _vector *hwtable,
+	     const struct _vector *mpvec)
 {
 	struct config *conf;
 	char *reply;
 
 	conf = get_multipath_config();
 	pthread_cleanup_push(put_multipath_config, conf);
-	reply = snprint_config(conf, len, hwtable, NULL);
+	reply = snprint_config(conf, len, hwtable, mpvec);
 	pthread_cleanup_pop(1);
 	if (reply == NULL)
 		return 1;
@@ -277,7 +278,28 @@ cli_list_config (void * v, char ** reply, int * len, void * data)
 {
 	condlog(3, "list config (operator)");
 
-	return show_config(reply, len, NULL);
+	return show_config(reply, len, NULL, NULL);
+}
+
+static void v_free(void *x)
+{
+	vector_free(x);
+}
+
+int
+cli_list_config_local (void * v, char ** reply, int * len, void * data)
+{
+	struct vectors * vecs = (struct vectors *)data;
+	vector hwes;
+	int ret;
+
+	condlog(3, "list config local (operator)");
+
+	hwes = get_used_hwes(vecs->pathvec);
+	pthread_cleanup_push(v_free, hwes);
+	ret = show_config(reply, len, hwes, vecs->mpvec);
+	pthread_cleanup_pop(1);
+	return ret;
 }
 
 int
multipathd/cli_handlers.h +1 -0
index 78a3a435..edbdf063 100644
@@ -16,6 +16,7 @@ int cli_list_maps_topology (void * v, char ** reply, int * len, void * data);
 int cli_list_map_json (void * v, char ** reply, int * len, void * data);
 int cli_list_maps_json (void * v, char ** reply, int * len, void * data);
 int cli_list_config (void * v, char ** reply, int * len, void * data);
+int cli_list_config_local (void * v, char ** reply, int * len, void * data);
 int cli_list_blacklist (void * v, char ** reply, int * len, void * data);
 int cli_list_devices (void * v, char ** reply, int * len, void * data);
 int cli_list_wildcards (void * v, char ** reply, int * len, void * data);
multipathd/main.c +1 -0
index 0db88eea..4d891508 100644
@@ -1494,6 +1494,7 @@ uxlsnrloop (void * ap)
 	set_handler_callback(LIST+MAP+FMT, cli_list_map_fmt);
 	set_handler_callback(LIST+MAP+RAW+FMT, cli_list_map_fmt);
 	set_handler_callback(LIST+MAP+JSON, cli_list_map_json);
+	set_handler_callback(LIST+CONFIG+LOCAL, cli_list_config_local);
 	set_handler_callback(LIST+CONFIG, cli_list_config);
 	set_handler_callback(LIST+BLACKLIST, cli_list_blacklist);
 	set_handler_callback(LIST+DEVICES, cli_list_devices);
multipathd/multipathd.8 +5 -0
index e78ac9ed..94c3f973 100644
@@ -135,6 +135,11 @@ Show the currently used configuration, derived from default values and values
 specified within the configuration file \fI/etc/multipath.conf\fR.
 .
 .TP
+.B list|show config local
+Show the currently used configuration like \fIshow config\fR, but limiting
+the devices section to those devices that are actually present in the system.
+.
+.TP
 .B list|show blacklist
 Show the currently used blacklist rules, derived from default values and values
 specified within the configuration file \fI/etc/multipath.conf\fR.
Signature · 1 line
2.17.0
#28 lore raw

PATCH 24/28 multipath: implement "multipath -T"

In reply to Martin Wilck

This does the same as "multipathd show config local", and is provided for convenience.

---
 libmultipath/config.h |  1 +
 multipath/main.c      | 43 +++++++++++++++++++++++++++----------------
 multipath/multipath.8 |  8 +++++++-
 3 files changed, 35 insertions(+), 17 deletions(-)
libmultipath/config.h +1 -0
index e1cbd59b..e2fa1852 100644
@@ -37,6 +37,7 @@ enum mpath_cmds {
 	CMD_RESET_WWIDS,
 	CMD_ADD_WWID,
 	CMD_USABLE_PATHS,
+	CMD_DUMP_CONFIG,
 };
 
 enum force_reload_types {
multipath/main.c +27 -16

86 lines not on this page · raw message

multipath/multipath.8 +7 -1
index 914a8cb2..b5e5292f 100644
@@ -25,7 +25,7 @@ multipath \- Device mapper target autoconfig.
 .RB [\| \-b\ \c
 .IR bindings_file \|]
 .RB [\| \-d \|]
-.RB [\| \-h | \-l | \-ll | \-f | \-t | \-F | \-B | \-c | \-C | \-q | \-r | \-i | \-a | \-u | \-U | \-w | \-W \|]
+.RB [\| \-h | \-l | \-ll | \-f | \-t | \-T | \-F | \-B | \-c | \-C | \-q | \-r | \-i | \-a | \-u | \-U | \-w | \-W \|]
 .RB [\| \-p\ \c
 .IR failover | multibus | group_by_serial | group_by_prio | group_by_node_name \|]
 .RB [\| \-R\ \c
@@ -89,6 +89,12 @@ Flush all unused multipath device maps.
 Display the currently used multipathd configuration.
 .
 .TP
+.B \-T
+Display the currently used multipathd configuration, limiting the output to
+those devices actually present in the system. This can be used a template for
+creating \fImultipath.conf\fR.
+.
+.TP
 .B \-r
 Force devmap reload.
 .
Signature · 1 line
2.17.0
#29 lore raw

PATCH 25/28 libmultipath: merge "multipath" config sections by wwid

In reply to Martin Wilck

If more than one "multipath" section exists for a given wwid, only the properties from the first section are applied, and those of the later ones are silently discarded.

Fix this by merging mpentries in the same way we do it for hwentries. Actually, later entries should take precedence, for consistency with hwentry handling.

---
 libmultipath/config.c | 70 +++++++++++++++++++++++++++++++++++++++++++
 tests/hwtable.c       | 16 ----------
 2 files changed, 70 insertions(+), 16 deletions(-)
libmultipath/config.c +70 -0
index fb41d620..c8378f87 100644
@@ -376,6 +376,74 @@ merge_hwe (struct hwentry * dst, struct hwentry * src)
 	return 0;
 }
 
+static int
+merge_mpe(struct mpentry *dst, struct mpentry *src)
+{
+	if (!dst || !src)
+		return 1;
+
+	merge_str(alias);
+	merge_str(uid_attribute);
+	merge_str(getuid);
+	merge_str(selector);
+	merge_str(features);
+	merge_str(prio_name);
+	merge_str(prio_args);
+
+	if (dst->prkey_source == PRKEY_SOURCE_NONE &&
+	    src->prkey_source != PRKEY_SOURCE_NONE) {
+		dst->prkey_source = src->prkey_source;
+		memcpy(&dst->reservation_key, &src->reservation_key,
+		       sizeof(dst->reservation_key));
+	}
+
+	merge_num(pgpolicy);
+	merge_num(pgfailback);
+	merge_num(rr_weight);
+	merge_num(no_path_retry);
+	merge_num(minio);
+	merge_num(minio_rq);
+	merge_num(flush_on_last_del);
+	merge_num(attribute_flags);
+	merge_num(user_friendly_names);
+	merge_num(deferred_remove);
+	merge_num(delay_watch_checks);
+	merge_num(delay_wait_checks);
+	merge_num(marginal_path_err_sample_time);
+	merge_num(marginal_path_err_rate_threshold);
+	merge_num(marginal_path_err_recheck_gap_time);
+	merge_num(marginal_path_double_failed_time);
+	merge_num(skip_kpartx);
+	merge_num(max_sectors_kb);
+	merge_num(ghost_delay);
+	merge_num(uid);
+	merge_num(gid);
+	merge_num(mode);
+
+	return 0;
+}
+
+void merge_mptable(vector mptable)
+{
+	struct mpentry *mp1, *mp2;
+	int i, j;
+
+	vector_foreach_slot(mptable, mp1, i) {
+		j = i + 1;
+		vector_foreach_slot_after(mptable, mp2, j) {
+			if (strcmp(mp1->wwid, mp2->wwid))
+				continue;
+			condlog(1, "%s: duplicate multipath config section for %s",
+				__func__, mp1->wwid);
+			merge_mpe(mp2, mp1);
+			free_mpe(mp1);
+			vector_del_slot(mptable, i);
+			i--;
+			break;
+		}
+	}
+}
+
 int
 store_hwe (vector hwtable, struct hwentry * dhwe)
 {
@@ -747,6 +815,8 @@ load_config (char * file)
 		if (!conf->mptable)
 			goto out;
 	}
+
+	merge_mptable(conf->mptable);
 	if (conf->bindings_file == NULL)
 		conf->bindings_file = set_default(DEFAULT_BINDINGS_FILE);
 
tests/hwtable.c +0 -16
index 1a6b3188..f6fba14d 100644
@@ -1567,8 +1567,6 @@ static int setup_multipath_config(void **state)
  *
  * Expected: properties are taken from both multipath sections, later taking
  * precedence
- *
- * Current: gets properties from first entry only.
  */
 static void test_multipath_config_2(const struct hwt_state *hwt)
 {
@@ -1580,15 +1578,8 @@ static void test_multipath_config_2(const struct hwt_state *hwt)
 	assert_ptr_not_equal(mp, NULL);
 	assert_ptr_not_equal(mp->mpe, NULL);
 	TEST_PROP(prio_name(&pp->prio), prio_rdac.value);
-#if BROKEN
-	condlog(1, "%s: WARNING: broken test on %d", __func__, __LINE__ + 1);
-	assert_int_equal(mp->minio, DEFAULT_MINIO_RQ);
-	condlog(1, "%s: WARNING: broken test on %d", __func__, __LINE__ + 1);
-	assert_int_equal(mp->no_path_retry, NO_PATH_RETRY_QUEUE);
-#else
 	assert_int_equal(mp->minio, atoi(minio_99.value));
 	assert_int_equal(mp->no_path_retry, atoi(npr_37.value));
-#endif
 }
 
 static int setup_multipath_config_2(void **state)
@@ -1622,15 +1613,8 @@ static void test_multipath_config_3(const struct hwt_state *hwt)
 	assert_ptr_not_equal(mp, NULL);
 	assert_ptr_not_equal(mp->mpe, NULL);
 	TEST_PROP(prio_name(&pp->prio), prio_rdac.value);
-#if BROKEN
-	condlog(1, "%s: WARNING: broken test on %d", __func__, __LINE__ + 1);
-	assert_int_equal(mp->minio, DEFAULT_MINIO_RQ);
-	condlog(1, "%s: WARNING: broken test on %d", __func__, __LINE__ + 1);
-	assert_int_equal(mp->no_path_retry, NO_PATH_RETRY_QUEUE);
-#else
 	assert_int_equal(mp->minio, atoi(minio_99.value));
 	assert_int_equal(mp->no_path_retry, atoi(npr_37.value));
-#endif
 }
 
 static int setup_multipath_config_3(void **state)
Signature · 1 line
2.17.0
#30 lore raw

PATCH 26/28 libmultipath: implement and use blacklist merging

In reply to Martin Wilck

Implement removal of invalid and duplicate entries in blacklists.

With this fix, we can fully enable the config dump/reload test, which doesn't fail any more.

---
 libmultipath/blacklist.c | 96 +++++++++++++++++++++++++++++++++-------
 libmultipath/blacklist.h |  2 +
 libmultipath/config.c    |  9 ++++
 tests/hwtable.c          |  5 ---
 4 files changed, 91 insertions(+), 21 deletions(-)
libmultipath/blacklist.c +80 -16
index 91ed7ddf..361c6032 100644
@@ -416,6 +416,15 @@ filter_property(struct config * conf, struct udev_device * udev)
 	return MATCH_PROPERTY_BLIST_MISSING;
 }
 
+static void free_ble(struct blentry *ble)
+{
+	if (!ble)
+		return;
+	regfree(&ble->regex);
+	FREE(ble->str);
+	FREE(ble);
+}
+
 void
 free_blacklist (vector blist)
 {
@@ -426,15 +435,45 @@ free_blacklist (vector blist)
 		return;
 
 	vector_foreach_slot (blist, ble, i) {
-		if (ble) {
-			regfree(&ble->regex);
-			FREE(ble->str);
-			FREE(ble);
-		}
+		free_ble(ble);
 	}
 	vector_free(blist);
 }
 
+void merge_blacklist(vector blist)
+{
+	struct blentry *bl1, *bl2;
+	int i, j;
+
+	vector_foreach_slot(blist, bl1, i) {
+		j = i + 1;
+		vector_foreach_slot_after(blist, bl2, j) {
+			if (!bl1->str || !bl2->str || strcmp(bl1->str, bl2->str))
+				continue;
+			condlog(3, "%s: duplicate blist entry section for %s",
+				__func__, bl1->str);
+			free_ble(bl2);
+			vector_del_slot(blist, j);
+			j--;
+		}
+	}
+}
+
+static void free_ble_device(struct blentry_device *ble)
+{
+	if (ble) {
+		if (ble->vendor) {
+			regfree(&ble->vendor_reg);
+			FREE(ble->vendor);
+		}
+		if (ble->product) {
+			regfree(&ble->product_reg);
+			FREE(ble->product);
+		}
+		FREE(ble);
+	}
+}
+
 void
 free_blacklist_device (vector blist)
 {
@@ -445,17 +484,42 @@ free_blacklist_device (vector blist)
 		return;
 
 	vector_foreach_slot (blist, ble, i) {
-		if (ble) {
-			if (ble->vendor) {
-				regfree(&ble->vendor_reg);
-				FREE(ble->vendor);
-			}
-			if (ble->product) {
-				regfree(&ble->product_reg);
-				FREE(ble->product);
-			}
-			FREE(ble);
-		}
+		free_ble_device(ble);
 	}
 	vector_free(blist);
 }
+
+void merge_blacklist_device(vector blist)
+{
+	struct blentry_device *bl1, *bl2;
+	int i, j;
+
+	vector_foreach_slot(blist, bl1, i) {
+		if (!bl1->vendor && !bl1->product) {
+			free_ble_device(bl1);
+			vector_del_slot(blist, i);
+			i--;
+		}
+	}
+
+	vector_foreach_slot(blist, bl1, i) {
+		j = i + 1;
+		vector_foreach_slot_after(blist, bl2, j) {
+			if ((!bl1->vendor && bl2->vendor) ||
+			    (bl1->vendor && !bl2->vendor) ||
+			    (bl1->vendor && bl2->vendor &&
+			     strcmp(bl1->vendor, bl2->vendor)))
+				continue;
+			if ((!bl1->product && bl2->product) ||
+			    (bl1->product && !bl2->product) ||
+			    (bl1->product && bl2->product &&
+			     strcmp(bl1->product, bl2->product)))
+				continue;
+			condlog(3, "%s: duplicate blist entry section for %s:%s",
+				__func__, bl1->vendor, bl1->product);
+			free_ble_device(bl2);
+			vector_del_slot(blist, j);
+			j--;
+		}
+	}
+}
libmultipath/blacklist.h +2 -0
index 443025d2..0b028d46 100644
@@ -40,5 +40,7 @@ int store_ble (vector, char *, int);
 int set_ble_device (vector, char *, char *, int);
 void free_blacklist (vector);
 void free_blacklist_device (vector);
+void merge_blacklist(vector);
+void merge_blacklist_device(vector);
 
 #endif /* _BLACKLIST_H */
libmultipath/config.c +9 -0
index c8378f87..28b76dd2 100644
@@ -817,6 +817,15 @@ load_config (char * file)
 	}
 
 	merge_mptable(conf->mptable);
+	merge_blacklist(conf->blist_devnode);
+	merge_blacklist(conf->blist_property);
+	merge_blacklist(conf->blist_wwid);
+	merge_blacklist_device(conf->blist_device);
+	merge_blacklist(conf->elist_devnode);
+	merge_blacklist(conf->elist_property);
+	merge_blacklist(conf->elist_wwid);
+	merge_blacklist_device(conf->elist_device);
+
 	if (conf->bindings_file == NULL)
 		conf->bindings_file = set_default(DEFAULT_BINDINGS_FILE);
 
tests/hwtable.c +0 -5
index f6fba14d..7daf71eb 100644
@@ -499,13 +499,8 @@ static void replicate_config(const struct hwt_state *hwt, bool local)
 	DUMP_CFG_STR(cfg2);
 #endif
 
-#if BROKEN
-	condlog(1, "%s: WARNING: skipping tests for same configuration after dump/reload on %d",
-		__func__, __LINE__);
-#else
 	assert_int_equal(strlen(cfg2), strlen(cfg1));
 	assert_string_equal(cfg2, cfg1);
-#endif
 	free(cfg1);
 	free(cfg2);
 }
Signature · 1 line
2.17.0
#31 lore raw

PATCH 27/28 libmultipath: escape '"' chars while dumping config

In reply to Martin Wilck

Since 044c5d6b "libmultipath: config parser: Allow '"' in strings", double quotes can be part of string values in multipath.conf by escaping them as double-double quotes ('""'). When dumping the configuration, the un-escaping done during config file parsing must be reverted by replacing '"' with '""' again.

---
 libmultipath/dict.c | 40 ++++++++++++++++++++++++++++++++++++++--
 tests/hwtable.c     | 18 ------------------
 2 files changed, 38 insertions(+), 20 deletions(-)
libmultipath/dict.c +38 -2
index 3e7c5d6d..b5958980 100644
@@ -109,9 +109,45 @@ print_nonzero (char *buff, int len, long v)
 static int
 print_str (char *buff, int len, const char *ptr)
 {
-	if (!ptr)
+	char *p;
+	char *last;
+	const char *q;
+
+	if (!ptr || len <= 0)
 		return 0;
-	return snprintf(buff, len, "\"%s\"", ptr);
+
+	q = strchr(ptr, '"');
+	if (q == NULL)
+		return snprintf(buff, len, "\"%s\"", ptr);
+
+	last = buff + len - 1;
+	p = buff;
+	if (p >= last)
+		goto out;
+	*p++ = '"';
+	if (p >= last)
+		goto out;
+	for (; q; q = strchr(ptr, '"')) {
+		if (q + 1 - ptr < last - p)
+			p = mempcpy(p, ptr, q + 1 - ptr);
+		else {
+			p = mempcpy(p, ptr, last - p);
+			goto out;
+		}
+		*p++ = '"';
+		if (p >= last)
+			goto out;
+		ptr = q + 1;
+	}
+	p += strlcpy(p, ptr, last - p);
+	if (p >= last)
+		goto out;
+	*p++ = '"';
+	*p = '\0';
+	return p - buff;
+out:
+	*p = '\0';
+	return len;
 }
 
 static int
tests/hwtable.c +0 -18
index 7daf71eb..9db79391 100644
@@ -598,18 +598,10 @@ static int setup_internal_nvme(void **state)
 /*
  * Device section with a simple entry qith double quotes ('foo:"bar"')
  */
-#if BROKEN
-static void test_quoted_hwe(void **state)
-#else
 static void test_quoted_hwe(const struct hwt_state *hwt)
-#endif
 {
 	struct path *pp;
-#if BROKEN
-       struct hwt_state *hwt = CHECK_STATE(state);
 
-       _conf = LOAD_CONFIG(hwt);
-#endif
 	/* foo:"bar" matches */
 	pp = mock_path(vnd_foo.value, prd_baq.value);
 	TEST_PROP(prio_name(&pp->prio), prio_emc.value);
@@ -625,11 +617,7 @@ static int setup_quoted_hwe(void **state)
 	const struct key_value kv[] = { vnd_foo, prd_baqq, prio_emc };
 
 	WRITE_ONE_DEVICE(hwt, kv);
-#if BROKEN
-       condlog(0, "%s: WARNING: skipping conf reload test", __func__);
-#else
 	SET_TEST_FUNC(hwt, test_quoted_hwe);
-#endif
 	return 0;
 }
 
@@ -1640,9 +1628,7 @@ static int setup_multipath_config_3(void **state)
 	}
 
 define_test(string_hwe)
-#if !BROKEN
 define_test(quoted_hwe)
-#endif
 define_test(internal_nvme)
 define_test(regex_hwe)
 define_test(regex_string_hwe)
@@ -1680,11 +1666,7 @@ static int test_hwtable(void)
 		cmocka_unit_test(test_sanity_globals),
 		test_entry(internal_nvme),
 		test_entry(string_hwe),
-#if BROKEN
-		cmocka_unit_test_setup(test_quoted_hwe, setup_quoted_hwe),
-#else
 		test_entry(quoted_hwe),
-#endif
 		test_entry(regex_hwe),
 		test_entry(regex_string_hwe),
 		test_entry(regex_string_hwe_dir),
Signature · 1 line
2.17.0
#32 lore raw

PATCH 28/28 multipath.conf(5): various corrections and clarifications

In reply to Martin Wilck

... related to the topics of option precedence, and config file syntax in general.

---
 multipath/multipath.conf.5 | 174 +++++++++++++++++++++++--------------
 1 file changed, 111 insertions(+), 63 deletions(-)
multipath/multipath.conf.5 +111 -63
index f6897954..044204a1 100644
@@ -85,6 +85,16 @@ the indentation shown in the above example is helpful for human readers but
 not mandatory.
 .LP
 .
+.LP
+.B Note on regular expressions:
+The \fImultipath.conf\fR syntax allows many attribute values to be specified as POSIX
+Extended Regular Expressions (see \fBregex\fR(7)). These regular expressions
+are \fBcase sensitive\fR and \fBnot anchored\fR, thus the expression "bar" matches "barbie",
+"rhabarber", and "wunderbar", but not "Barbie". To avoid unwanted substring
+matches, standard regular expression syntax using the special characters "^" and "$" can be used.
+.
+.LP
+.
 The following \fIsection\fP keywords are recognized:
 .TP 17
 .B defaults
@@ -104,10 +114,12 @@ multipath topology discovery, despite being listed in the
 .B multipaths
 This section defines the multipath topologies. They are indexed by a
 \fIWorld Wide Identifier\fR(WWID). For details on the WWID generation
-see section \fIWWID generation\fR below.
+see section \fIWWID generation\fR below. Attributes set in this section take
+precedence over all others.
 .TP
 .B devices
-This section defines the device-specific settings.
+This section defines the device-specific settings. Devices are identified by
+vendor, product, and revision.
 .TP
 .B overrides
 This section defines values for attributes that should override the
@@ -1114,100 +1126,112 @@ The default is \fBno\fR
 .
 .
 .\" ----------------------------------------------------------------------------
-.SH "blacklist section"
+.SH "blacklist and blacklist_exceptions sections"
 .\" ----------------------------------------------------------------------------
 .
-The \fIblacklist\fR section is used to exclude specific device from inclusion in
-the multipath topology. It is most commonly used to exclude local disks or LUNs
-for the array controller.
+The \fIblacklist\fR section is used to exclude specific devices from
+the multipath topology. It is most commonly used to exclude local disks or
+non-disk devices (such as LUNs for the storage array controller) from
+being handled by multipath-tools.
 .LP
 .
 .
-The following keywords are recognized:
+The \fIblacklist_exceptions\fR section is used to revert the actions of the
+\fIblacklist\fR section. This allows one to selectively include ("whitelist") devices which
+would normally be excluded via the \fIblacklist\fR section. A common usage is
+to blacklist "everything" using a catch-all regular expression, and create
+specific blacklist_exceptions entries for those devices that should be handled
+by multipath-tools.
+.LP
+.
+.
+The following keywords are recognized in both sections. The defaults are empty
+unless explicitly stated.
 .TP 17
 .B devnode
-Regular expression of the device nodes to be excluded.
+Regular expression matching the device nodes to be excluded/included.
 .RS
-.TP
-The default is: \fB^(ram|raw|loop|fd|md|dm-|sr|scd|st|dcssblk)[0-9]\fR and \fB^(td|hd|vd)[a-z]\fR
+.PP
+The default \fIblacklist\fR consists of the regular expressions
+"^(ram|raw|loop|fd|md|dm-|sr|scd|st|dcssblk)[0-9]" and
+"^(td|hd|vd)[a-z]". This causes virtual devices, non-disk devices, and some other
+device types to be excluded from multipath handling by default.
 .RE
 .TP
 .B wwid
-The \fIWorld Wide Identification\fR of a device.
-.TP
-.B property
-Regular expression of the udev property to be excluded.
+Regular expression for the \fIWorld Wide Identifier\fR of a device to be excluded/included.
+.
 .TP
 .B device
 Subsection for the device description. This subsection recognizes the
 .B vendor
 and
 .B product
-keywords. For a full description of these keywords please see the
+keywords. Both are regular expressions. For a full description of these keywords please see the
 \fIdevices\fR section description.
-.
-.
-.\" ----------------------------------------------------------------------------
-.SH "blacklist_exceptions section"
-.\" ----------------------------------------------------------------------------
-.
-The \fIblacklist_exceptions\fR section is used to revert the actions of the
-\fIblacklist\fR section. For example to include specific device in the
-multipath topology. This allows one to selectively include devices which
-would normally be excluded via the \fIblacklist\fR section.
-.LP
-.
-.
-The following keywords are recognized:
-.TP 17
-.B devnode
-Regular expression of the device nodes to be whitelisted.
-.TP
-.B wwid
-The \fIWorld Wide Identification\fR of a device.
 .TP
 .B property
-Regular expression of the udev property to be whitelisted.
+Regular expression for an udev property. All
+devices that have matching udev properties will be excluded/included.
+The handling of the \fIproperty\fR keyword is special,
+because devices \fBmust\fR have at least one whitelisted udev property;
+otherwise they're treated as blacklisted, and the message
+"\fIblacklisted, udev property missing\fR" is displayed in the logs.
+.
 .RS
-.TP
-The default is: \fB(SCSI_IDENT_|ID_WWN)\fR
+.PP
+The default \fIblacklist exception\fR is: \fB(SCSI_IDENT_|ID_WWN)\fR, causing
+well-behaved SCSI devices and devices that provide a WWN (World Wide Number)
+to be included, and all others to be excluded.
 .RE
-.TP
-.B device
-Subsection for the device description. This subsection recognizes the
-.B vendor
-and
-.B product
-keywords. For a full description of these keywords please see the \fIdevices\fR
-section description.
 .LP
-The \fIproperty\fR blacklist and whitelist handling is different from the usual
-handling in the sense that the whitelist \fIhas\fR to be set, otherwise the
-device will be blacklisted. In these cases the message \fIblacklisted, udev
-property missing\fR will be displayed.
+For every device, these 4 blacklist criteria are evaluated in the the order
+"property, dev\%node, device, wwid". If a device turns out to be
+blacklisted by any criterion, it's excluded from handling by multipathd, and
+the later criteria aren't evaluated any more. For each
+criterion, the whitelist takes precedence over the blacklist if a device
+matches both.
+.LP
+.B
+Note:
+Besides the blacklist and whitelist, other configuration options such as
+\fIfind_multipaths\fR have an impact on
+whether or not a given device is handled by multipath-tools.
 .
 .
 .\" ----------------------------------------------------------------------------
 .SH "multipaths section"
 .\" ----------------------------------------------------------------------------
 .
+The \fImultipaths\fR section allows setting attributes of multipath maps. The
+attributes that are set via the multipaths section (see list below) take
+precedence over all other configuration settings, including those from the
+\fIoverrides\fR section.
+.LP
 The only recognized attribute for the \fImultipaths\fR section is the
-\fImultipath\fR subsection.
+\fImultipath\fR subsection. If there are multiple \fImultipath\fR subsections
+matching a given WWID, the contents of these sections are merged, and settings
+from later entries take precedence.
 .LP
 .
 .
 The \fImultipath\fR subsection recognizes the following attributes:
 .TP 17
 .B wwid
-(Mandatory) Index of the container.
+(Mandatory) World Wide Identifier. Detected multipath maps are matched agains this attribute.
+Note that, unlike the \fIwwid\fR attribute in the \fIblacklist\fR section,
+this is \fBnot\fR a regular expression or a substring; WWIDs must match
+exactly inside the multipaths section.
 .TP
 .B alias
-Symbolic name for the multipath map.
+Symbolic name for the multipath map. This takes precedence over a an entry for
+the same WWID in the \fIbindings_file\fR.
 .LP
 .
 .
 The following attributes are optional; if not set the default values
-are taken from the \fIdefaults\fR or \fIdevices\fR section:
+are taken from the \fIoverrides\fR, \fIdevices\fR, or \fIdefaults\fR
+section:
 .sp 1
 .PD .1v
 .RS
@@ -1266,26 +1290,46 @@ are taken from the \fIdefaults\fR or \fIdevices\fR section:
 .SH "devices section"
 .\" ----------------------------------------------------------------------------
 .
+\fImultipath-tools\fR have a built-in device table with reasonable defaults
+for more than 100 known multipath-capable storage devices. The devices section
+can be used to override these settings. If there are multiple matches for a
+given device, the attributes of all matching entries are applied to it.
+If an attribute is specified in several matching device subsections,
+later entries take precedence. Thus, entries in files under \fIconfig_dir\fR (in
+reverse alphabetical order) have the highest precedence, followed by entries
+in \fImultipath.conf\fR; the built-in hardware table has the lowest
+precedence. Inside a configuration file, later entries have higher precedence
+than earlier ones.
+.LP
 The only recognized attribute for the \fIdevices\fR section is the \fIdevice\fR
-subsection.
+subsection. Devices detected in the system are matched against the device entries
+using the \fBvendor\fR, \fBproduct\fR, and \fBrevision\fR fields, which are
+all POSIX Extended regular expressions (see \fBregex\fR(7)).
+.LP
+The vendor, product, and revision fields that multipath or multipathd detect for
+devices in a system depend on the device type. For SCSI devices, they correspond to the
+respective fields of the SCSI INQUIRY page. In general, the command '\fImultipathd show
+paths format "%d %s"\fR' command can be used to see the detected properties
+for all devices in the system.
 .LP
-.
 .
 The \fIdevice\fR subsection recognizes the following attributes:
-.TP
-vendor, product, revision and product_blacklist are POSIX Extended regex.
 .TP 17
 .B vendor
-(Mandatory) Vendor identifier.
+(Mandatory) Regular expression to match the vendor name.
 .TP
 .B product
-(Mandatory) Product identifier.
+(Mandatory) Regular expression to match the product name.
 .TP
 .B revision
-Revision identfier.
+Regular expression to match the product revision. If not specified, any
+revision matches.
 .TP
 .B product_blacklist
-Product strings to blacklist for this vendor.
+Products with the given \fBvendor\fR matching this string are
+blacklisted. This is equivalent to a \fBdevice\fR entry in the \fIblacklist\fR
+section with the \fIvendor\fR attribute set to this entry's \fIvendor\fR, and
+the \fIproduct\fR attribute set to the value of \fIproduct_blacklist\fR.
 .TP
 .B alias_prefix
 The user_friendly_names prefix to use for this
@@ -1475,8 +1519,12 @@ Multipath uses a \fIWorld Wide Identification\fR (WWID) to determine
 which paths belong to the same device. Each path presenting the same
 WWID is assumed to point to the same device.
 .LP
-The WWID is generated by three methods (in the order of preference):
+The WWID is generated by four methods (in the order of preference):
 .TP 17
+.B uid_attrs
+The WWID is derived from udev attributes by matching the device node name. See
+description of \fIuid_attrs\fR in the defaults section above.
+.TP
 .B getuid_callout
 Use the specified external program; cf \fIgetuid_callout\fR above.
 Care should be taken when using this method; the external program
Signature · 1 line
2.17.0
#33 lore raw
In reply to Martin Wilck

On Fri, Jun 08, 2018 at 12:20:13PM +0200, Martin Wilck wrote:

Thanks for cleaning this up.

ACK for the whole series 00-30

-Ben

Quoted 104 lines

This patch series fixes a number of small but important issues in the way multipath-tools handle configuration files, in particular the handling of multiple "device" and "blacklist" sections matching the same device. Besides eliminating inconsistencies, the goal of the series is to be able to use "multipath -t" or "multipathd show config" ouput as configuration for multipath-tools, and obtain exactly the same results as with the original configuration.

Late in the series, two new commands "multipath -T" and "multipath show config local" are added, which avoid the lengthyness of the full configuration dump and produce output more suitable as a local configuration template.

By far the largest part of the series is a new unit test (hwtable.c) for configuration file handling. The commit message of the patch introducing this unit test ("tests/hwtable: tests for config file handling and hwentry merging") explains the problems / inconsistencies that the series is supposed to fix. Tests that fail in the first place are marked with the BROKEN macro. While reviewing the 2000 LOC of the new unit test is certainly a pain, I'd like to ask reviewers to *pay special attention to the tests marked BROKEN*, because that's where follow-up patches will change the behavior of multipath-tools in user-noticeable ways. I believe it's for the better, but I'd like to make sure we all agree. For the rest of the unit test, reviewers might appreciate the fact that tests succeed for the current code, and are intended as regeression tests for the follow-up patches.

Patch 01-07 are just simple fixes and, as usual, "const" additions. Patch 08-10 introduce the new unit test.
Patch 11-17 fix some basic problems which need to be fixed for dump/reload. Patch 18-24 add the ability for dumping the configuration, using the output as new configuration input ("reload"), and testing / comparing the results. Patch 23-24 implement "multipathd show config local" and "multipath -T" on these grounds.
Patch 23-27 fix more problems that surfaced in the dump/reload test. Patch 28 clarifies the documentation of multipath.conf.

Martin Wilck (28):
kpartx: no need to use FREE_CONST
libmultipath: fix memory leak in process_config_dir()
libmultipath: remove superfluous conditionals in load_config() libmultipath/structs.c: constify some functions
libmultipath: some const usage in hwentry handling
libmultipath: change prototypes of hwe_regmatch() and find_hwe() libmultipath/prio: constify simple getters tests/Makefile: autogenerate list of symbols to be wrapped tests/test-lib: cmocka helpers to simulate path and map discovery tests/hwtable: tests for config file handling and hwentry merging
libmultipath: add debug messages to hwentry lookup/merging code
libmultipath: use vector for for pp->hwe and mp->hwe
libmultipath: allow more than one hwentry
libmultipath: don't merge hwentries by regex
libmultipath: merge hwentries inside a conf file libmultipath/hwtable: remove inherited props from ONTAP NVMe
libmultipath: don't merge by regex in setup_default_blist() multipath, multipathd: consolidate config dumping tests/hwtable: implement configuration dump + reload test
libmultipath: allow dumping only "local" hwtable in snprint_config tests/hwtable: add test for local configuration dump
libmultipath: allow printing local maps in snprint_config
multipathd: implement "show config local"
multipath: implement "multipath -T"
libmultipath: merge "multipath" config sections by wwid
libmultipath: implement and use blacklist merging
libmultipath: escape '"' chars while dumping config multipath.conf(5): various corrections and clarifications

 kpartx/devmapper.c         |   10 +-
 libmultipath/blacklist.c   |  125 ++-
 libmultipath/blacklist.h   |    2 +
 libmultipath/config.c      |  208 +++--
 libmultipath/config.h      |    5 +-
 libmultipath/dict.c        |   40 +-
 libmultipath/discovery.c   |   10 +-
 libmultipath/hwtable.c     |    3 -
 libmultipath/print.c       |  128 ++-
 libmultipath/print.h       |    9 +-
 libmultipath/prio.c        |    8 +-
 libmultipath/prio.h        |    8 +-
 libmultipath/propsel.c     |   53 +-
 libmultipath/structs.c     |   26 +-
 libmultipath/structs.h     |   24 +-
 libmultipath/structs_vec.c |   19 +
 libmultipath/structs_vec.h |    1 +
 libmultipath/vector.c      |   12 +
 libmultipath/vector.h      |    1 +
 multipath/main.c           |   92 +-
 multipath/multipath.8      |    8 +-
 multipath/multipath.conf.5 |  174 ++--
 multipathd/cli.c           |    2 +
 multipathd/cli.h           |    2 +
 multipathd/cli_handlers.c  |   80 +-
 multipathd/cli_handlers.h  |    1 +
 multipathd/main.c          |    1 +
 multipathd/multipathd.8    |    5 +
 tests/Makefile             |   36 +-
 tests/hwtable.c            | 1707 ++++++++++++++++++++++++++++++++++++
 tests/test-lib.c           |  359 ++++++++
 tests/test-lib.h           |   67 ++
 32 files changed, 2892 insertions(+), 334 deletions(-)
 create mode 100644 tests/hwtable.c
 create mode 100644 tests/test-lib.c
 create mode 100644 tests/test-lib.h
Signature · 1 line
2.17.0