Blame SOURCES/0041-multipathd-handle-fpin-events.patch

e65fa3
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
e65fa3
From: Muneendra Kumar <muneendra.kumar@broadcom.com>
e65fa3
Date: Wed, 9 Feb 2022 19:28:10 -0800
e65fa3
Subject: [PATCH] multipathd: handle fpin events
e65fa3
e65fa3
This patch incorporates the functionality to handle
e65fa3
FPIN ELS events present as part of FCTransport daemon
e65fa3
(available in EPEL8) into the multipathd. This helps us to
e65fa3
reduce the response time to react and take the necessary actions
e65fa3
on receiving the FPIN events.
e65fa3
e65fa3
This patch currently support FPIN-Li Events.
e65fa3
e65fa3
It adds a new thread to listen for ELS frames from driver and on
e65fa3
receiving the frame payload, push the payload to a list and notify the
e65fa3
fpin_els_li_consumer thread to process it.Once consumer thread is
e65fa3
notified, it returns to listen for more ELS frames from driver.
e65fa3
e65fa3
The consumer thread process the ELS frames and moves the devices paths
e65fa3
which are affected due to link integrity to marginal path groups.
e65fa3
This also sets the associated portstate to marginal.
e65fa3
The paths which are set to marginal path group will be unset
e65fa3
on receiving the RSCN events
e65fa3
e65fa3
[ MW: minor fixup for 32bit compilation ]
e65fa3
e65fa3
Signed-off-by: Muneendra Kumar <muneendra.kumar@broadcom.com>
e65fa3
Signed-off-by: Benjamin Marzinski <bmarzins@redhat.com>
e65fa3
Signed-off-by: Martin Wilck <mwilck@suse.com>
e65fa3
Reviewed-by: Martin Wilck <mwilck@suse.com>
e65fa3
---
e65fa3
 Makefile.inc                      |  13 +
e65fa3
 libmultipath/Makefile             |   5 +
e65fa3
 libmultipath/dict.c               |  56 +++-
e65fa3
 libmultipath/libmultipath.version |   5 +
e65fa3
 libmultipath/propsel.c            |  47 ++-
e65fa3
 libmultipath/structs.h            |   7 +
e65fa3
 multipath/multipath.conf.5        |  19 +-
e65fa3
 multipathd/Makefile               |  10 +
e65fa3
 multipathd/fpin.h                 |  20 ++
e65fa3
 multipathd/fpin_handlers.c        | 540 ++++++++++++++++++++++++++++++
e65fa3
 multipathd/main.c                 |  43 ++-
e65fa3
 11 files changed, 749 insertions(+), 16 deletions(-)
e65fa3
 create mode 100644 multipathd/fpin.h
e65fa3
 create mode 100644 multipathd/fpin_handlers.c
e65fa3
e65fa3
diff --git a/Makefile.inc b/Makefile.inc
e65fa3
index 5ac660de..688c4599 100644
e65fa3
--- a/Makefile.inc
e65fa3
+++ b/Makefile.inc
e65fa3
@@ -149,6 +149,19 @@ check_file = $(shell \
e65fa3
 	echo "$$found" \
e65fa3
 	)
e65fa3
 
e65fa3
+# Check whether a file contains a variable with name $1 in header file $2
e65fa3
+check_var = $(shell \
e65fa3
+	if grep -Eq "(^|[[:blank:]])$1([[:blank:]]|=|$$)" "$2"; then \
e65fa3
+                found=1; \
e65fa3
+                status="yes"; \
e65fa3
+        else \
e65fa3
+                found=0; \
e65fa3
+                status="no"; \
e65fa3
+        fi; \
e65fa3
+        echo 1>&2 "Checking for ..  $1 in $2 ... $$status"; \
e65fa3
+        echo "$$found" \
e65fa3
+        )
e65fa3
+
e65fa3
 %.o:	%.c
e65fa3
 	@echo building $@ because of $?
e65fa3
 	$(CC) $(CFLAGS) -c -o $@ $<
e65fa3
diff --git a/libmultipath/Makefile b/libmultipath/Makefile
e65fa3
index 7f3921c5..8a960419 100644
e65fa3
--- a/libmultipath/Makefile
e65fa3
+++ b/libmultipath/Makefile
e65fa3
@@ -45,6 +45,11 @@ ifneq ($(call check_func,dm_hold_control_dev,/usr/include/libdevmapper.h),0)
e65fa3
 	CFLAGS += -DLIBDM_API_HOLD_CONTROL
e65fa3
 endif
e65fa3
 
e65fa3
+ifneq ($(call check_var,ELS_DTAG_LNK_INTEGRITY,/usr/include/scsi/fc/fc_els.h),0)
e65fa3
+	CFLAGS += -DFPIN_EVENT_HANDLER
e65fa3
+endif
e65fa3
+
e65fa3
+
e65fa3
 OBJS = memory.o parser.o vector.o devmapper.o callout.o \
e65fa3
 	hwtable.o blacklist.o util.o dmparser.o config.o \
e65fa3
 	structs.o discovery.o propsel.o dict.o \
e65fa3
diff --git a/libmultipath/dict.c b/libmultipath/dict.c
e65fa3
index 1b75be47..eb5a8083 100644
e65fa3
--- a/libmultipath/dict.c
e65fa3
+++ b/libmultipath/dict.c
e65fa3
@@ -513,6 +513,59 @@ snprint_def_find_multipaths(struct config *conf, struct strbuf *buff,
e65fa3
 			 find_multipaths_optvals[conf->find_multipaths]);
e65fa3
 }
e65fa3
 
e65fa3
+static const char * const marginal_pathgroups_optvals[] = {
e65fa3
+	[MARGINAL_PATHGROUP_OFF] = "off",
e65fa3
+	[MARGINAL_PATHGROUP_ON] = "on",
e65fa3
+#ifdef FPIN_EVENT_HANDLER
e65fa3
+	[MARGINAL_PATHGROUP_FPIN] = "fpin",
e65fa3
+#endif
e65fa3
+};
e65fa3
+
e65fa3
+static int
e65fa3
+def_marginal_pathgroups_handler(struct config *conf, vector strvec,
e65fa3
+			    const char *file, int line_nr)
e65fa3
+{
e65fa3
+	char *buff;
e65fa3
+	unsigned int i;
e65fa3
+
e65fa3
+	buff = set_value(strvec);
e65fa3
+	if (!buff)
e65fa3
+		return 1;
e65fa3
+	for (i = MARGINAL_PATHGROUP_OFF;
e65fa3
+	     i < ARRAY_SIZE(marginal_pathgroups_optvals); i++) {
e65fa3
+		if (marginal_pathgroups_optvals[i] != NULL &&
e65fa3
+		    !strcmp(buff, marginal_pathgroups_optvals[i])) {
e65fa3
+			conf->marginal_pathgroups = i;
e65fa3
+			break;
e65fa3
+		}
e65fa3
+	}
e65fa3
+
e65fa3
+	if (i >= ARRAY_SIZE(marginal_pathgroups_optvals)) {
e65fa3
+		if (strcmp(buff, "no") == 0 || strcmp(buff, "0") == 0)
e65fa3
+			conf->marginal_pathgroups = MARGINAL_PATHGROUP_OFF;
e65fa3
+		else if (strcmp(buff, "yes") == 0 || strcmp(buff, "1") == 0)
e65fa3
+			conf->marginal_pathgroups = MARGINAL_PATHGROUP_ON;
e65fa3
+		/* This can only be true if FPIN_EVENT_HANDLER isn't defined,
e65fa3
+		 * otherwise this check will have already happened above */
e65fa3
+		else if (strcmp(buff, "fpin") == 0)
e65fa3
+			condlog(1, "%s line %d, support for \"fpin\" is not compiled in for marginal_pathgroups", file, line_nr);
e65fa3
+		else
e65fa3
+			condlog(1, "%s line %d, invalid value for marginal_pathgroups: \"%s\"",
e65fa3
+				file, line_nr, buff);
e65fa3
+	}
e65fa3
+	free(buff);
e65fa3
+	return 0;
e65fa3
+}
e65fa3
+
e65fa3
+static int
e65fa3
+snprint_def_marginal_pathgroups(struct config *conf, struct strbuf *buff,
e65fa3
+			    const void *data)
e65fa3
+{
e65fa3
+	return append_strbuf_quoted(buff,
e65fa3
+			 marginal_pathgroups_optvals[conf->marginal_pathgroups]);
e65fa3
+}
e65fa3
+
e65fa3
+
e65fa3
 declare_def_handler(selector, set_str)
e65fa3
 declare_def_snprint_defstr(selector, print_str, DEFAULT_SELECTOR)
e65fa3
 declare_hw_handler(selector, set_str)
e65fa3
@@ -1527,9 +1580,6 @@ declare_ovr_snprint(all_tg_pt, print_yes_no_undef)
e65fa3
 declare_hw_handler(all_tg_pt, set_yes_no_undef)
e65fa3
 declare_hw_snprint(all_tg_pt, print_yes_no_undef)
e65fa3
 
e65fa3
-declare_def_handler(marginal_pathgroups, set_yes_no)
e65fa3
-declare_def_snprint(marginal_pathgroups, print_yes_no)
e65fa3
-
e65fa3
 declare_def_handler(recheck_wwid, set_yes_no_undef)
e65fa3
 declare_def_snprint_defint(recheck_wwid, print_yes_no_undef, DEFAULT_RECHECK_WWID)
e65fa3
 declare_ovr_handler(recheck_wwid, set_yes_no_undef)
e65fa3
diff --git a/libmultipath/libmultipath.version b/libmultipath/libmultipath.version
e65fa3
index 0d89e9e1..1d018eab 100644
e65fa3
--- a/libmultipath/libmultipath.version
e65fa3
+++ b/libmultipath/libmultipath.version
e65fa3
@@ -297,3 +297,8 @@ LIBMULTIPATH_9.1.1 {
e65fa3
 global:
e65fa3
 	trigger_path_udev_change;
e65fa3
 } LIBMULTIPATH_9.1.0;
e65fa3
+
e65fa3
+LIBMULTIPATH_9.1.2 {
e65fa3
+global:
e65fa3
+	cleanup_mutex;
e65fa3
+} LIBMULTIPATH_9.1.1;
e65fa3
diff --git a/libmultipath/propsel.c b/libmultipath/propsel.c
e65fa3
index b2876670..677ab9e1 100644
e65fa3
--- a/libmultipath/propsel.c
e65fa3
+++ b/libmultipath/propsel.c
e65fa3
@@ -84,6 +84,8 @@ static const char cmdline_origin[] =
e65fa3
 	"(setting: multipath command line [-p] flag)";
e65fa3
 static const char autodetect_origin[] =
e65fa3
 	"(setting: storage device autodetected)";
e65fa3
+static const char fpin_marginal_path_origin[] =
e65fa3
+	"(setting: overridden by marginal_path_fpin)";
e65fa3
 static const char marginal_path_origin[] =
e65fa3
 	"(setting: implied by marginal_path check)";
e65fa3
 static const char delay_watch_origin[] =
e65fa3
@@ -1036,9 +1038,12 @@ int select_san_path_err_threshold(struct config *conf, struct multipath *mp)
e65fa3
 	const char *origin;
e65fa3
 	STRBUF_ON_STACK(buff);
e65fa3
 
e65fa3
-	if (marginal_path_check_enabled(mp)) {
e65fa3
+	if (marginal_path_check_enabled(mp) || (conf->marginal_pathgroups == MARGINAL_PATHGROUP_FPIN)) {
e65fa3
 		mp->san_path_err_threshold = NU_NO;
e65fa3
-		origin = marginal_path_origin;
e65fa3
+		if (conf->marginal_pathgroups == MARGINAL_PATHGROUP_FPIN)
e65fa3
+			origin = fpin_marginal_path_origin;
e65fa3
+		else
e65fa3
+			origin = marginal_path_origin;
e65fa3
 		goto out;
e65fa3
 	}
e65fa3
 	mp_set_mpe(san_path_err_threshold);
e65fa3
@@ -1059,9 +1064,12 @@ int select_san_path_err_forget_rate(struct config *conf, struct multipath *mp)
e65fa3
 	const char *origin;
e65fa3
 	STRBUF_ON_STACK(buff);
e65fa3
 
e65fa3
-	if (marginal_path_check_enabled(mp)) {
e65fa3
+	if (marginal_path_check_enabled(mp) || (conf->marginal_pathgroups == MARGINAL_PATHGROUP_FPIN)) {
e65fa3
 		mp->san_path_err_forget_rate = NU_NO;
e65fa3
-		origin = marginal_path_origin;
e65fa3
+		if (conf->marginal_pathgroups == MARGINAL_PATHGROUP_FPIN)
e65fa3
+			origin = fpin_marginal_path_origin;
e65fa3
+		else
e65fa3
+			origin = marginal_path_origin;
e65fa3
 		goto out;
e65fa3
 	}
e65fa3
 	mp_set_mpe(san_path_err_forget_rate);
e65fa3
@@ -1083,9 +1091,12 @@ int select_san_path_err_recovery_time(struct config *conf, struct multipath *mp)
e65fa3
 	const char *origin;
e65fa3
 	STRBUF_ON_STACK(buff);
e65fa3
 
e65fa3
-	if (marginal_path_check_enabled(mp)) {
e65fa3
+	if (marginal_path_check_enabled(mp) || (conf->marginal_pathgroups == MARGINAL_PATHGROUP_FPIN)) {
e65fa3
 		mp->san_path_err_recovery_time = NU_NO;
e65fa3
-		origin = marginal_path_origin;
e65fa3
+		if (conf->marginal_pathgroups == MARGINAL_PATHGROUP_FPIN)
e65fa3
+			origin = fpin_marginal_path_origin;
e65fa3
+		else
e65fa3
+			origin = marginal_path_origin;
e65fa3
 		goto out;
e65fa3
 	}
e65fa3
 	mp_set_mpe(san_path_err_recovery_time);
e65fa3
@@ -1107,6 +1118,12 @@ int select_marginal_path_err_sample_time(struct config *conf, struct multipath *
e65fa3
 	const char *origin;
e65fa3
 	STRBUF_ON_STACK(buff);
e65fa3
 
e65fa3
+	if (conf->marginal_pathgroups == MARGINAL_PATHGROUP_FPIN) {
e65fa3
+		mp->marginal_path_err_sample_time = NU_NO;
e65fa3
+		origin = fpin_marginal_path_origin;
e65fa3
+		goto out;
e65fa3
+	}
e65fa3
+
e65fa3
 	mp_set_mpe(marginal_path_err_sample_time);
e65fa3
 	mp_set_ovr(marginal_path_err_sample_time);
e65fa3
 	mp_set_hwe(marginal_path_err_sample_time);
e65fa3
@@ -1130,6 +1147,12 @@ int select_marginal_path_err_rate_threshold(struct config *conf, struct multipat
e65fa3
 	const char *origin;
e65fa3
 	STRBUF_ON_STACK(buff);
e65fa3
 
e65fa3
+	if (conf->marginal_pathgroups == MARGINAL_PATHGROUP_FPIN) {
e65fa3
+		mp->marginal_path_err_rate_threshold = NU_NO;
e65fa3
+		origin = fpin_marginal_path_origin;
e65fa3
+		goto out;
e65fa3
+	}
e65fa3
+
e65fa3
 	mp_set_mpe(marginal_path_err_rate_threshold);
e65fa3
 	mp_set_ovr(marginal_path_err_rate_threshold);
e65fa3
 	mp_set_hwe(marginal_path_err_rate_threshold);
e65fa3
@@ -1147,6 +1170,12 @@ int select_marginal_path_err_recheck_gap_time(struct config *conf, struct multip
e65fa3
 	const char *origin;
e65fa3
 	STRBUF_ON_STACK(buff);
e65fa3
 
e65fa3
+	if (conf->marginal_pathgroups == MARGINAL_PATHGROUP_FPIN) {
e65fa3
+		mp->marginal_path_err_recheck_gap_time = NU_NO;
e65fa3
+		origin = fpin_marginal_path_origin;
e65fa3
+		goto out;
e65fa3
+	}
e65fa3
+
e65fa3
 	mp_set_mpe(marginal_path_err_recheck_gap_time);
e65fa3
 	mp_set_ovr(marginal_path_err_recheck_gap_time);
e65fa3
 	mp_set_hwe(marginal_path_err_recheck_gap_time);
e65fa3
@@ -1165,6 +1194,12 @@ int select_marginal_path_double_failed_time(struct config *conf, struct multipat
e65fa3
 	const char *origin;
e65fa3
 	STRBUF_ON_STACK(buff);
e65fa3
 
e65fa3
+	if (conf->marginal_pathgroups == MARGINAL_PATHGROUP_FPIN) {
e65fa3
+		mp->marginal_path_double_failed_time = NU_NO;
e65fa3
+		origin = fpin_marginal_path_origin;
e65fa3
+		goto out;
e65fa3
+	}
e65fa3
+
e65fa3
 	mp_set_mpe(marginal_path_double_failed_time);
e65fa3
 	mp_set_ovr(marginal_path_double_failed_time);
e65fa3
 	mp_set_hwe(marginal_path_double_failed_time);
e65fa3
diff --git a/libmultipath/structs.h b/libmultipath/structs.h
e65fa3
index 399540e7..1188363e 100644
e65fa3
--- a/libmultipath/structs.h
e65fa3
+++ b/libmultipath/structs.h
e65fa3
@@ -110,6 +110,12 @@ enum find_multipaths_states {
e65fa3
 	__FIND_MULTIPATHS_LAST,
e65fa3
 };
e65fa3
 
e65fa3
+enum marginal_pathgroups_mode {
e65fa3
+	MARGINAL_PATHGROUP_OFF = YN_NO,
e65fa3
+	MARGINAL_PATHGROUP_ON = YN_YES,
e65fa3
+	MARGINAL_PATHGROUP_FPIN,
e65fa3
+};
e65fa3
+
e65fa3
 enum flush_states {
e65fa3
 	FLUSH_UNDEF = YNU_UNDEF,
e65fa3
 	FLUSH_DISABLED = YNU_NO,
e65fa3
@@ -409,6 +415,7 @@ struct multipath {
e65fa3
 	unsigned char prflag;
e65fa3
 	int all_tg_pt;
e65fa3
 	struct gen_multipath generic_mp;
e65fa3
+	bool fpin_must_reload;
e65fa3
 };
e65fa3
 
e65fa3
 static inline int marginal_path_check_enabled(const struct multipath *mpp)
e65fa3
diff --git a/multipath/multipath.conf.5 b/multipath/multipath.conf.5
e65fa3
index 7f85f766..5ed2cd3c 100644
e65fa3
--- a/multipath/multipath.conf.5
e65fa3
+++ b/multipath/multipath.conf.5
e65fa3
@@ -1088,20 +1088,26 @@ The default is: \fBno\fR
e65fa3
 .
e65fa3
 .TP
e65fa3
 .B marginal_pathgroups
e65fa3
-If set to \fIno\fR, the \fIdelay_*_checks\fR, \fImarginal_path_*\fR, and
e65fa3
+If set to \fIoff\fR, the \fIdelay_*_checks\fR, \fImarginal_path_*\fR, and
e65fa3
 \fIsan_path_err_*\fR options will keep marginal, or \(dqshaky\(dq, paths from
e65fa3
 being reinstated until they have been monitored for some time. This can cause
e65fa3
 situations where all non-marginal paths are down, and no paths are usable
e65fa3
 until multipathd detects this and reinstates a marginal path. If the multipath
e65fa3
 device is not configured to queue IO in this case, it can cause IO errors to
e65fa3
 occur, even though there are marginal paths available.  However, if this
e65fa3
-option is set to \fIyes\fR, when one of the marginal path detecting methods
e65fa3
+option is set to \fIon\fR, when one of the marginal path detecting methods
e65fa3
 determines that a path is marginal, it will be reinstated and placed in a
e65fa3
 seperate pathgroup that will only be used after all the non-marginal pathgroups
e65fa3
 have been tried first. This prevents the possibility of IO errors occuring
e65fa3
 while marginal paths are still usable. After the path has been monitored
e65fa3
 for the configured time, and is declared healthy, it will be returned to its
e65fa3
-normal pathgroup. See "Shaky paths detection" below for more information.
e65fa3
+normal pathgroup.
e65fa3
+However if this option is set to \fIfpin\fR multipathd will receive fpin
e65fa3
+notifications, set path states to "marginal" accordingly, and regroup paths
e65fa3
+as described for "marginal_pathgroups yes". This option can't be used in combination
e65fa3
+with other options for "Shaky path detection" (see below).If it is set to fpin,
e65fa3
+marginal_path_xyz and san_path_err_xyz parameters are implicitly set to 0.
e65fa3
+See "Shaky paths detection" below for more information.
e65fa3
 .RS
e65fa3
 .TP
e65fa3
 The default is: \fBno\fR
e65fa3
@@ -1842,6 +1848,13 @@ increase and the threshold is never reached. Ticks are the time between
e65fa3
 path checks by multipathd, which is variable and controlled by the
e65fa3
 \fIpolling_interval\fR and \fImax_polling_interval\fR parameters.
e65fa3
 .
e65fa3
+.TP
e65fa3
+.B \(dqFPIN \(dq failure tracking
e65fa3
+Fibre channel fabrics can notify hosts about fabric-level issues such
e65fa3
+as integrity failures or congestion with so-called Fabric Performance
e65fa3
+Impact Notifications (FPINs).On receiving the fpin notifications through ELS
e65fa3
+multipathd will move the affected path and port states to marginal.
e65fa3
+.
e65fa3
 .RS 8
e65fa3
 .LP
e65fa3
 This method is \fBdeprecated\fR in favor of the \(dqmarginal_path\(dq failure
e65fa3
diff --git a/multipathd/Makefile b/multipathd/Makefile
e65fa3
index 393b6cbb..cd6f7e6d 100644
e65fa3
--- a/multipathd/Makefile
e65fa3
+++ b/multipathd/Makefile
e65fa3
@@ -4,6 +4,10 @@ ifneq ($(call check_func,dm_task_get_errno,/usr/include/libdevmapper.h),0)
e65fa3
 	CFLAGS += -DLIBDM_API_GET_ERRNO
e65fa3
 endif
e65fa3
 
e65fa3
+ifneq ($(call check_var,ELS_DTAG_LNK_INTEGRITY,/usr/include/scsi/fc/fc_els.h),0)
e65fa3
+	CFLAGS += -DFPIN_EVENT_HANDLER
e65fa3
+	FPIN_SUPPORT = 1
e65fa3
+endif
e65fa3
 #
e65fa3
 # debugging stuff
e65fa3
 #
e65fa3
@@ -34,6 +38,12 @@ endif
e65fa3
 OBJS = main.o pidfile.o uxlsnr.o uxclnt.o cli.o cli_handlers.o waiter.o \
e65fa3
        dmevents.o init_unwinder.o
e65fa3
 
e65fa3
+ifeq ($(FPIN_SUPPORT),1)
e65fa3
+OBJS += fpin_handlers.o
e65fa3
+endif
e65fa3
+
e65fa3
+
e65fa3
+
e65fa3
 EXEC = multipathd
e65fa3
 
e65fa3
 all : $(EXEC)
e65fa3
diff --git a/multipathd/fpin.h b/multipathd/fpin.h
e65fa3
new file mode 100644
e65fa3
index 00000000..bfcc1ce2
e65fa3
--- /dev/null
e65fa3
+++ b/multipathd/fpin.h
e65fa3
@@ -0,0 +1,20 @@
e65fa3
+#ifndef __FPIN_H__
e65fa3
+#define __FPIN_H__
e65fa3
+
e65fa3
+#ifdef FPIN_EVENT_HANDLER
e65fa3
+void *fpin_fabric_notification_receiver(void *unused);
e65fa3
+void *fpin_els_li_consumer(void *data);
e65fa3
+void fpin_clean_marginal_dev_list(__attribute__((unused)) void *arg);
e65fa3
+#else
e65fa3
+static void *fpin_fabric_notification_receiver(__attribute__((unused))void *unused)
e65fa3
+{
e65fa3
+	return NULL;
e65fa3
+}
e65fa3
+static void *fpin_els_li_consumer(__attribute__((unused))void *data)
e65fa3
+{
e65fa3
+	return NULL;
e65fa3
+}
e65fa3
+/* fpin_clean_marginal_dev_list() is never called */
e65fa3
+#endif
e65fa3
+
e65fa3
+#endif
e65fa3
diff --git a/multipathd/fpin_handlers.c b/multipathd/fpin_handlers.c
e65fa3
new file mode 100644
e65fa3
index 00000000..aaf5655d
e65fa3
--- /dev/null
e65fa3
+++ b/multipathd/fpin_handlers.c
e65fa3
@@ -0,0 +1,540 @@
e65fa3
+#include <errno.h>
e65fa3
+#include <unistd.h>
e65fa3
+#include <sys/types.h>
e65fa3
+#include <sys/socket.h>
e65fa3
+#include <libudev.h>
e65fa3
+#include <scsi/scsi_netlink_fc.h>
e65fa3
+#include <scsi/fc/fc_els.h>
e65fa3
+
e65fa3
+#include "parser.h"
e65fa3
+#include "vector.h"
e65fa3
+#include "structs.h"
e65fa3
+#include "structs_vec.h"
e65fa3
+#include "main.h"
e65fa3
+#include "debug.h"
e65fa3
+#include "util.h"
e65fa3
+#include "sysfs.h"
e65fa3
+
e65fa3
+#include "fpin.h"
e65fa3
+#include "devmapper.h"
e65fa3
+
e65fa3
+static pthread_cond_t fpin_li_cond = PTHREAD_COND_INITIALIZER;
e65fa3
+static pthread_mutex_t fpin_li_mutex = PTHREAD_MUTEX_INITIALIZER;
e65fa3
+static pthread_mutex_t fpin_li_marginal_dev_mutex = PTHREAD_MUTEX_INITIALIZER;
e65fa3
+
e65fa3
+static LIST_HEAD(els_marginal_list_head);
e65fa3
+static LIST_HEAD(fpin_li_marginal_dev_list_head);
e65fa3
+
e65fa3
+
e65fa3
+#define DEF_RX_BUF_SIZE	4096
e65fa3
+#define DEV_NAME_LEN	128
e65fa3
+#define FCH_EVT_LINKUP 0x2
e65fa3
+#define FCH_EVT_LINK_FPIN 0x501
e65fa3
+#define FCH_EVT_RSCN 0x5
e65fa3
+
e65fa3
+#define list_first_entry(ptr, type, member) \
e65fa3
+	list_entry((ptr)->next, type, member)
e65fa3
+
e65fa3
+/* max ELS frame Size */
e65fa3
+#define FC_PAYLOAD_MAXLEN   2048
e65fa3
+
e65fa3
+struct els_marginal_list {
e65fa3
+	uint32_t event_code;
e65fa3
+	uint16_t host_num;
e65fa3
+	uint16_t length;
e65fa3
+	char payload[FC_PAYLOAD_MAXLEN];
e65fa3
+	struct list_head node;
e65fa3
+};
e65fa3
+/* Structure to store the marginal devices info */
e65fa3
+struct marginal_dev_list {
e65fa3
+	char dev_t[BLK_DEV_SIZE];
e65fa3
+	uint32_t host_num;
e65fa3
+	struct list_head node;
e65fa3
+};
e65fa3
+
e65fa3
+static void _udev_device_unref(void *p)
e65fa3
+{
e65fa3
+	udev_device_unref(p);
e65fa3
+}
e65fa3
+
e65fa3
+
e65fa3
+/*set/unset the path state to marginal*/
e65fa3
+static int fpin_set_pathstate(struct path *pp, bool set)
e65fa3
+{
e65fa3
+	const char *action = set ? "set" : "unset";
e65fa3
+
e65fa3
+	if (!pp || !pp->mpp || !pp->mpp->alias)
e65fa3
+		return -1;
e65fa3
+
e65fa3
+	condlog(3, "\n%s: %s  marginal path %s (fpin)",
e65fa3
+		action, pp->mpp->alias, pp->dev_t);
e65fa3
+	pp->marginal = set;
e65fa3
+	pp->mpp->fpin_must_reload = true;
e65fa3
+	return 0;
e65fa3
+}
e65fa3
+
e65fa3
+/* This will unset marginal state of a device*/
e65fa3
+static void fpin_path_unsetmarginal(char *devname, struct vectors *vecs)
e65fa3
+{
e65fa3
+	struct path *pp;
e65fa3
+
e65fa3
+	pp = find_path_by_dev(vecs->pathvec, devname);
e65fa3
+	if (!pp)
e65fa3
+		pp = find_path_by_devt(vecs->pathvec, devname);
e65fa3
+
e65fa3
+	fpin_set_pathstate(pp, false);
e65fa3
+}
e65fa3
+
e65fa3
+/*This will set the marginal state of a device*/
e65fa3
+static int fpin_path_setmarginal(struct path *pp)
e65fa3
+{
e65fa3
+	return fpin_set_pathstate(pp, true);
e65fa3
+}
e65fa3
+
e65fa3
+/* Unsets all the devices in the list from marginal state */
e65fa3
+static void
e65fa3
+fpin_unset_marginal_dev(uint32_t host_num, struct vectors *vecs)
e65fa3
+{
e65fa3
+	struct marginal_dev_list *tmp_marg = NULL;
e65fa3
+	struct marginal_dev_list *marg = NULL;
e65fa3
+	struct multipath *mpp;
e65fa3
+	int ret = 0;
e65fa3
+	int i;
e65fa3
+
e65fa3
+	pthread_cleanup_push(cleanup_lock, &vecs->lock);
e65fa3
+	lock(&vecs->lock);
e65fa3
+	pthread_testcancel();
e65fa3
+
e65fa3
+	pthread_mutex_lock(&fpin_li_marginal_dev_mutex);
e65fa3
+	pthread_cleanup_push(cleanup_mutex, &fpin_li_marginal_dev_mutex);
e65fa3
+	pthread_testcancel();
e65fa3
+	if (list_empty(&fpin_li_marginal_dev_list_head)) {
e65fa3
+		condlog(4, "Marginal List is empty\n");
e65fa3
+		goto empty;
e65fa3
+	}
e65fa3
+	list_for_each_entry_safe(marg, tmp_marg, &fpin_li_marginal_dev_list_head, node) {
e65fa3
+		if (marg->host_num != host_num)
e65fa3
+			continue;
e65fa3
+		condlog(4, " unsetting marginal dev: is %s %d\n",
e65fa3
+				tmp_marg->dev_t, tmp_marg->host_num);
e65fa3
+		fpin_path_unsetmarginal(marg->dev_t, vecs);
e65fa3
+		list_del(&marg->node);
e65fa3
+		free(marg);
e65fa3
+	}
e65fa3
+empty:
e65fa3
+	pthread_cleanup_pop(1);
e65fa3
+	/* walk backwards because reload_and_sync_map() can remove mpp */
e65fa3
+	vector_foreach_slot_backwards(vecs->mpvec, mpp, i) {
e65fa3
+		if (mpp->fpin_must_reload) {
e65fa3
+			ret = reload_and_sync_map(mpp, vecs, 0);
e65fa3
+			if (ret == 2)
e65fa3
+				condlog(2, "map removed during reload");
e65fa3
+			else
e65fa3
+				mpp->fpin_must_reload = false;
e65fa3
+		}
e65fa3
+	}
e65fa3
+	pthread_cleanup_pop(1);
e65fa3
+}
e65fa3
+
e65fa3
+/*
e65fa3
+ * On Receiving the frame from HBA driver, insert the frame into link
e65fa3
+ * integrity frame list which will be picked up later by consumer thread for
e65fa3
+ * processing.
e65fa3
+ */
e65fa3
+static int
e65fa3
+fpin_els_add_li_frame(struct fc_nl_event *fc_event)
e65fa3
+{
e65fa3
+	struct els_marginal_list *els_mrg = NULL;
e65fa3
+	int ret = 0;
e65fa3
+
e65fa3
+	if (fc_event->event_datalen > FC_PAYLOAD_MAXLEN)
e65fa3
+		return -EINVAL;
e65fa3
+
e65fa3
+	pthread_mutex_lock(&fpin_li_mutex);
e65fa3
+	pthread_cleanup_push(cleanup_mutex, &fpin_li_mutex);
e65fa3
+	pthread_testcancel();
e65fa3
+	els_mrg = calloc(1, sizeof(struct els_marginal_list));
e65fa3
+	if (els_mrg != NULL) {
e65fa3
+		els_mrg->host_num = fc_event->host_no;
e65fa3
+		els_mrg->event_code = fc_event->event_code;
e65fa3
+		els_mrg->length = fc_event->event_datalen;
e65fa3
+		memcpy(els_mrg->payload, &(fc_event->event_data), fc_event->event_datalen);
e65fa3
+		list_add_tail(&els_mrg->node, &els_marginal_list_head);
e65fa3
+		pthread_cond_signal(&fpin_li_cond);
e65fa3
+	} else
e65fa3
+		ret = -ENOMEM;
e65fa3
+	pthread_cleanup_pop(1);
e65fa3
+	return ret;
e65fa3
+
e65fa3
+}
e65fa3
+
e65fa3
+/*Sets the rport port_state to marginal*/
e65fa3
+static void fpin_set_rport_marginal(struct udev_device *rport_dev)
e65fa3
+{
e65fa3
+	sysfs_attr_set_value(rport_dev, "port_state",
e65fa3
+				"Marginal", strlen("Marginal"));
e65fa3
+}
e65fa3
+
e65fa3
+/*Add the marginal devices info into the list*/
e65fa3
+static void
e65fa3
+fpin_add_marginal_dev_info(uint32_t host_num, char *devname)
e65fa3
+{
e65fa3
+	struct marginal_dev_list *newdev = NULL;
e65fa3
+
e65fa3
+	newdev = calloc(1, sizeof(struct marginal_dev_list));
e65fa3
+	if (newdev != NULL) {
e65fa3
+		newdev->host_num = host_num;
e65fa3
+		strlcpy(newdev->dev_t, devname, BLK_DEV_SIZE);
e65fa3
+		condlog(4, "\n%s hostno %d devname %s\n", __func__,
e65fa3
+				host_num, newdev->dev_t);
e65fa3
+		pthread_mutex_lock(&fpin_li_marginal_dev_mutex);
e65fa3
+		list_add_tail(&(newdev->node),
e65fa3
+				&fpin_li_marginal_dev_list_head);
e65fa3
+		pthread_mutex_unlock(&fpin_li_marginal_dev_mutex);
e65fa3
+	}
e65fa3
+}
e65fa3
+
e65fa3
+/*
e65fa3
+ * This function goes through the vecs->pathvec, and for
e65fa3
+ * each path, check that the host  number,
e65fa3
+ * the target WWPN associated with the path matches
e65fa3
+ * with the els wwpn and sets the path and port state to
e65fa3
+ * Marginal
e65fa3
+ */
e65fa3
+static int  fpin_chk_wwn_setpath_marginal(uint16_t host_num,  struct vectors *vecs,
e65fa3
+		uint64_t els_wwpn)
e65fa3
+{
e65fa3
+	struct path *pp;
e65fa3
+	struct multipath *mpp;
e65fa3
+	int i, k;
e65fa3
+	char rport_id[42];
e65fa3
+	const char *value = NULL;
e65fa3
+	struct udev_device *rport_dev = NULL;
e65fa3
+	uint64_t wwpn;
e65fa3
+	int ret = 0;
e65fa3
+
e65fa3
+	pthread_cleanup_push(cleanup_lock, &vecs->lock);
e65fa3
+	lock(&vecs->lock);
e65fa3
+	pthread_testcancel();
e65fa3
+
e65fa3
+	vector_foreach_slot(vecs->pathvec, pp, k) {
e65fa3
+		/* Checks the host number and also for the SCSI FCP */
e65fa3
+		if (pp->sg_id.proto_id != SCSI_PROTOCOL_FCP || host_num !=  pp->sg_id.host_no)
e65fa3
+			continue;
e65fa3
+		sprintf(rport_id, "rport-%d:%d-%d",
e65fa3
+				pp->sg_id.host_no, pp->sg_id.channel, pp->sg_id.transport_id);
e65fa3
+		rport_dev = udev_device_new_from_subsystem_sysname(udev,
e65fa3
+				"fc_remote_ports", rport_id);
e65fa3
+		if (!rport_dev) {
e65fa3
+			condlog(2, "%s: No fc_remote_port device for '%s'", pp->dev,
e65fa3
+					rport_id);
e65fa3
+			continue;
e65fa3
+		}
e65fa3
+		pthread_cleanup_push(_udev_device_unref, rport_dev);
e65fa3
+		value = udev_device_get_sysattr_value(rport_dev, "port_name");
e65fa3
+		if (!value)
e65fa3
+			goto unref;
e65fa3
+
e65fa3
+		if (value)
e65fa3
+			wwpn =  strtol(value, NULL, 16);
e65fa3
+		/*
e65fa3
+		 * If the port wwpn matches sets the path and port state
e65fa3
+		 * to marginal
e65fa3
+		 */
e65fa3
+		if (wwpn == els_wwpn) {
e65fa3
+			ret = fpin_path_setmarginal(pp);
e65fa3
+			if (ret < 0)
e65fa3
+				goto unref;
e65fa3
+			fpin_set_rport_marginal(rport_dev);
e65fa3
+			fpin_add_marginal_dev_info(host_num, pp->dev);
e65fa3
+		}
e65fa3
+unref:
e65fa3
+		pthread_cleanup_pop(1);
e65fa3
+	}
e65fa3
+	/* walk backwards because reload_and_sync_map() can remove mpp */
e65fa3
+	vector_foreach_slot_backwards(vecs->mpvec, mpp, i) {
e65fa3
+		if (mpp->fpin_must_reload) {
e65fa3
+			ret = reload_and_sync_map(mpp, vecs, 0);
e65fa3
+			if (ret == 2)
e65fa3
+				condlog(2, "map removed during reload");
e65fa3
+			else
e65fa3
+				mpp->fpin_must_reload = false;
e65fa3
+		}
e65fa3
+	}
e65fa3
+	pthread_cleanup_pop(1);
e65fa3
+	return ret;
e65fa3
+}
e65fa3
+
e65fa3
+/*
e65fa3
+ * This function loops around all the impacted wwns received as part of els
e65fa3
+ * frame and sets the associated path and port states to marginal.
e65fa3
+ */
e65fa3
+static int
e65fa3
+fpin_parse_li_els_setpath_marginal(uint16_t host_num, struct fc_tlv_desc *tlv,
e65fa3
+		struct vectors *vecs)
e65fa3
+{
e65fa3
+	uint32_t wwn_count = 0, iter = 0;
e65fa3
+	uint64_t wwpn;
e65fa3
+	struct fc_fn_li_desc *li_desc = (struct fc_fn_li_desc *)tlv;
e65fa3
+	int count = 0;
e65fa3
+	int ret = 0;
e65fa3
+
e65fa3
+	/* Update the wwn to list */
e65fa3
+	wwn_count = be32_to_cpu(li_desc->pname_count);
e65fa3
+	condlog(4, "Got wwn count as %d\n", wwn_count);
e65fa3
+
e65fa3
+	for (iter = 0; iter < wwn_count; iter++) {
e65fa3
+		wwpn = be64_to_cpu(li_desc->pname_list[iter]);
e65fa3
+		ret = fpin_chk_wwn_setpath_marginal(host_num, vecs, wwpn);
e65fa3
+		if (ret < 0)
e65fa3
+			condlog(2, "failed to set the path marginal associated with wwpn: 0x%" PRIx64 "\n", wwpn);
e65fa3
+
e65fa3
+		count++;
e65fa3
+	}
e65fa3
+	return count;
e65fa3
+}
e65fa3
+
e65fa3
+/*
e65fa3
+ * This function process the ELS frame received from HBA driver,
e65fa3
+ * and sets the path associated with the port wwn to marginal
e65fa3
+ * and also set the port state to marginal.
e65fa3
+ */
e65fa3
+static int
e65fa3
+fpin_process_els_frame(uint16_t host_num, char *fc_payload, struct vectors *vecs)
e65fa3
+{
e65fa3
+
e65fa3
+	int count = -1;
e65fa3
+	struct fc_els_fpin *fpin = (struct fc_els_fpin *)fc_payload;
e65fa3
+	struct fc_tlv_desc *tlv;
e65fa3
+
e65fa3
+	tlv = (struct fc_tlv_desc *)&fpin->fpin_desc[0];
e65fa3
+
e65fa3
+	/*
e65fa3
+	 * Parse the els frame and set the affected paths and port
e65fa3
+	 * state to marginal
e65fa3
+	 */
e65fa3
+	count = fpin_parse_li_els_setpath_marginal(host_num, tlv, vecs);
e65fa3
+	if (count <= 0)
e65fa3
+		condlog(4, "Could not find any WWNs, ret = %d\n",
e65fa3
+					count);
e65fa3
+	return count;
e65fa3
+}
e65fa3
+
e65fa3
+/*
e65fa3
+ * This function process the FPIN ELS frame received from HBA driver,
e65fa3
+ * and push the frame to appropriate frame list. Currently we have only FPIN
e65fa3
+ * LI frame list.
e65fa3
+ */
e65fa3
+static int
e65fa3
+fpin_handle_els_frame(struct fc_nl_event *fc_event)
e65fa3
+{
e65fa3
+	int ret = -1;
e65fa3
+	uint32_t els_cmd;
e65fa3
+	struct fc_els_fpin *fpin = (struct fc_els_fpin *)&fc_event->event_data;
e65fa3
+	struct fc_tlv_desc *tlv;
e65fa3
+	uint32_t dtag;
e65fa3
+
e65fa3
+	els_cmd = (uint32_t)fc_event->event_data;
e65fa3
+	tlv = (struct fc_tlv_desc *)&fpin->fpin_desc[0];
e65fa3
+	dtag = be32_to_cpu(tlv->desc_tag);
e65fa3
+	condlog(4, "Got CMD in add as 0x%x fpin_cmd 0x%x dtag 0x%x\n",
e65fa3
+			els_cmd, fpin->fpin_cmd, dtag);
e65fa3
+
e65fa3
+	if ((fc_event->event_code == FCH_EVT_LINK_FPIN) ||
e65fa3
+			(fc_event->event_code == FCH_EVT_LINKUP) ||
e65fa3
+			(fc_event->event_code == FCH_EVT_RSCN)) {
e65fa3
+
e65fa3
+		if (els_cmd == ELS_FPIN) {
e65fa3
+			/*
e65fa3
+			 * Check the type of fpin by checking the tag info
e65fa3
+			 * At present we are supporting only LI events
e65fa3
+			 */
e65fa3
+			if (dtag == ELS_DTAG_LNK_INTEGRITY) {
e65fa3
+				/*Push the Payload to FPIN frame queue. */
e65fa3
+				ret = fpin_els_add_li_frame(fc_event);
e65fa3
+				if (ret != 0)
e65fa3
+					condlog(0, "Failed to process LI frame with error %d\n",
e65fa3
+							ret);
e65fa3
+			} else {
e65fa3
+				condlog(4, "Unsupported FPIN received 0x%x\n", dtag);
e65fa3
+				return ret;
e65fa3
+			}
e65fa3
+		} else {
e65fa3
+			/*Push the Payload to FPIN frame queue. */
e65fa3
+			ret = fpin_els_add_li_frame(fc_event);
e65fa3
+			if (ret != 0)
e65fa3
+				condlog(0, "Failed to process Linkup/RSCN event with error %d evnt %d\n",
e65fa3
+						ret, fc_event->event_code);
e65fa3
+		}
e65fa3
+	} else
e65fa3
+		condlog(4, "Invalid command received: 0x%x\n", els_cmd);
e65fa3
+	return ret;
e65fa3
+}
e65fa3
+
e65fa3
+/*cleans the global marginal dev list*/
e65fa3
+void fpin_clean_marginal_dev_list(__attribute__((unused)) void *arg)
e65fa3
+{
e65fa3
+	struct marginal_dev_list *tmp_marg = NULL;
e65fa3
+
e65fa3
+	pthread_mutex_lock(&fpin_li_marginal_dev_mutex);
e65fa3
+	while (!list_empty(&fpin_li_marginal_dev_list_head)) {
e65fa3
+		tmp_marg  = list_first_entry(&fpin_li_marginal_dev_list_head,
e65fa3
+				struct marginal_dev_list, node);
e65fa3
+		list_del(&tmp_marg->node);
e65fa3
+		free(tmp_marg);
e65fa3
+	}
e65fa3
+	pthread_mutex_unlock(&fpin_li_marginal_dev_mutex);
e65fa3
+}
e65fa3
+
e65fa3
+/* Cleans the global els  marginal list */
e65fa3
+static void fpin_clean_els_marginal_list(void *arg)
e65fa3
+{
e65fa3
+	struct list_head *head = (struct list_head *)arg;
e65fa3
+	struct els_marginal_list *els_marg;
e65fa3
+
e65fa3
+	while (!list_empty(head)) {
e65fa3
+		els_marg  = list_first_entry(head, struct els_marginal_list,
e65fa3
+					     node);
e65fa3
+		list_del(&els_marg->node);
e65fa3
+		free(els_marg);
e65fa3
+	}
e65fa3
+}
e65fa3
+
e65fa3
+static void rcu_unregister(__attribute__((unused)) void *param)
e65fa3
+{
e65fa3
+	rcu_unregister_thread();
e65fa3
+}
e65fa3
+/*
e65fa3
+ * This is the FPIN ELS consumer thread. The thread sleeps on pthread cond
e65fa3
+ * variable unless notified by fpin_fabric_notification_receiver thread.
e65fa3
+ * This thread is only to process FPIN-LI ELS frames. A new thread and frame
e65fa3
+ * list will be added if any more ELS frames types are to be supported.
e65fa3
+ */
e65fa3
+void *fpin_els_li_consumer(void *data)
e65fa3
+{
e65fa3
+	struct list_head marginal_list_head;
e65fa3
+	int ret = 0;
e65fa3
+	uint16_t host_num;
e65fa3
+	struct els_marginal_list *els_marg;
e65fa3
+	uint32_t event_code;
e65fa3
+	struct vectors *vecs = (struct vectors *)data;
e65fa3
+
e65fa3
+	pthread_cleanup_push(rcu_unregister, NULL);
e65fa3
+	rcu_register_thread();
e65fa3
+	pthread_cleanup_push(fpin_clean_marginal_dev_list, NULL);
e65fa3
+	INIT_LIST_HEAD(&marginal_list_head);
e65fa3
+	pthread_cleanup_push(fpin_clean_els_marginal_list,
e65fa3
+				(void *)&marginal_list_head);
e65fa3
+	for ( ; ; ) {
e65fa3
+		pthread_mutex_lock(&fpin_li_mutex);
e65fa3
+		pthread_cleanup_push(cleanup_mutex, &fpin_li_mutex);
e65fa3
+		pthread_testcancel();
e65fa3
+		while (list_empty(&els_marginal_list_head))
e65fa3
+			pthread_cond_wait(&fpin_li_cond, &fpin_li_mutex);
e65fa3
+
e65fa3
+		if (!list_empty(&els_marginal_list_head)) {
e65fa3
+			condlog(4, "Invoke List splice tail\n");
e65fa3
+			list_splice_tail_init(&els_marginal_list_head, &marginal_list_head);
e65fa3
+		}
e65fa3
+		pthread_cleanup_pop(1);
e65fa3
+
e65fa3
+		while (!list_empty(&marginal_list_head)) {
e65fa3
+			els_marg  = list_first_entry(&marginal_list_head,
e65fa3
+							struct els_marginal_list, node);
e65fa3
+			host_num = els_marg->host_num;
e65fa3
+			event_code = els_marg->event_code;
e65fa3
+			/* Now finally process FPIN LI ELS Frame */
e65fa3
+			condlog(4, "Got a new Payload buffer, processing it\n");
e65fa3
+			if ((event_code ==  FCH_EVT_LINKUP) || (event_code == FCH_EVT_RSCN))
e65fa3
+				 fpin_unset_marginal_dev(host_num, vecs);
e65fa3
+			else {
e65fa3
+				ret = fpin_process_els_frame(host_num, els_marg->payload, vecs);
e65fa3
+				if (ret <= 0)
e65fa3
+					condlog(0, "ELS frame processing failed with ret %d\n", ret);
e65fa3
+			}
e65fa3
+			list_del(&els_marg->node);
e65fa3
+			free(els_marg);
e65fa3
+
e65fa3
+		}
e65fa3
+	}
e65fa3
+
e65fa3
+	pthread_cleanup_pop(1);
e65fa3
+	pthread_cleanup_pop(1);
e65fa3
+	pthread_cleanup_pop(1);
e65fa3
+	return NULL;
e65fa3
+}
e65fa3
+
e65fa3
+static void receiver_cleanup_list(__attribute__((unused)) void *arg)
e65fa3
+{
e65fa3
+	pthread_mutex_lock(&fpin_li_mutex);
e65fa3
+	fpin_clean_els_marginal_list(&els_marginal_list_head);
e65fa3
+	pthread_mutex_unlock(&fpin_li_mutex);
e65fa3
+}
e65fa3
+
e65fa3
+/*
e65fa3
+ * Listen for ELS frames from driver. on receiving the frame payload,
e65fa3
+ * push the payload to a list, and notify the fpin_els_li_consumer thread to
e65fa3
+ * process it. Once consumer thread is notified, return to listen for more ELS
e65fa3
+ * frames from driver.
e65fa3
+ */
e65fa3
+void *fpin_fabric_notification_receiver(__attribute__((unused))void *unused)
e65fa3
+{
e65fa3
+	int ret;
e65fa3
+	long fd;
e65fa3
+	uint32_t els_cmd;
e65fa3
+	struct fc_nl_event *fc_event = NULL;
e65fa3
+	struct sockaddr_nl fc_local;
e65fa3
+	unsigned char buf[DEF_RX_BUF_SIZE] __attribute__((aligned(sizeof(uint64_t))));
e65fa3
+	size_t plen = 0;
e65fa3
+
e65fa3
+	pthread_cleanup_push(rcu_unregister, NULL);
e65fa3
+	rcu_register_thread();
e65fa3
+
e65fa3
+	pthread_cleanup_push(receiver_cleanup_list, NULL);
e65fa3
+	fd = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_SCSITRANSPORT);
e65fa3
+	if (fd < 0) {
e65fa3
+		condlog(0, "fc socket error %ld", fd);
e65fa3
+		return NULL;
e65fa3
+	}
e65fa3
+
e65fa3
+	pthread_cleanup_push(close_fd, (void *)fd);
e65fa3
+	memset(&fc_local, 0, sizeof(fc_local));
e65fa3
+	fc_local.nl_family = AF_NETLINK;
e65fa3
+	fc_local.nl_groups = ~0;
e65fa3
+	fc_local.nl_pid = getpid();
e65fa3
+	ret = bind(fd, (struct sockaddr *)&fc_local, sizeof(fc_local));
e65fa3
+	if (ret == -1) {
e65fa3
+		condlog(0, "fc socket bind error %d\n", ret);
e65fa3
+		goto out;
e65fa3
+	}
e65fa3
+	for ( ; ; ) {
e65fa3
+		condlog(4, "Waiting for ELS...\n");
e65fa3
+		ret = read(fd, buf, DEF_RX_BUF_SIZE);
e65fa3
+		if (ret < 0) {
e65fa3
+			condlog(0, "failed to read the els frame (%d)", ret);
e65fa3
+			continue;
e65fa3
+		}
e65fa3
+		condlog(4, "Got a new request %d\n", ret);
e65fa3
+		if (!NLMSG_OK((struct nlmsghdr *)buf, (unsigned int)ret)) {
e65fa3
+			condlog(0, "bad els frame read (%d)", ret);
e65fa3
+			continue;
e65fa3
+		}
e65fa3
+		/* Push the frame to appropriate frame list */
e65fa3
+		plen = NLMSG_PAYLOAD((struct nlmsghdr *)buf, 0);
e65fa3
+		fc_event = (struct fc_nl_event *)NLMSG_DATA(buf);
e65fa3
+		if (plen < sizeof(*fc_event)) {
e65fa3
+			condlog(0, "too short (%d) to be an FC event", ret);
e65fa3
+			continue;
e65fa3
+		}
e65fa3
+		els_cmd = (uint32_t)fc_event->event_data;
e65fa3
+		condlog(4, "Got host no as %d, event 0x%x, len %d evntnum %d evntcode %d\n",
e65fa3
+				fc_event->host_no, els_cmd, fc_event->event_datalen,
e65fa3
+				fc_event->event_num, fc_event->event_code);
e65fa3
+		fpin_handle_els_frame(fc_event);
e65fa3
+	}
e65fa3
+out:
e65fa3
+	pthread_cleanup_pop(1);
e65fa3
+	pthread_cleanup_pop(1);
e65fa3
+	pthread_cleanup_pop(1);
e65fa3
+	return NULL;
e65fa3
+}
e65fa3
diff --git a/multipathd/main.c b/multipathd/main.c
e65fa3
index 5def5301..53be9b95 100644
e65fa3
--- a/multipathd/main.c
e65fa3
+++ b/multipathd/main.c
e65fa3
@@ -16,6 +16,7 @@
e65fa3
 #include <linux/oom.h>
e65fa3
 #include <libudev.h>
e65fa3
 #include <urcu.h>
e65fa3
+#include "fpin.h"
e65fa3
 #ifdef USE_SYSTEMD
e65fa3
 #include <systemd/sd-daemon.h>
e65fa3
 #endif
e65fa3
@@ -130,9 +131,11 @@ static volatile enum daemon_status running_state = DAEMON_INIT;
e65fa3
 pid_t daemon_pid;
e65fa3
 static pthread_mutex_t config_lock = PTHREAD_MUTEX_INITIALIZER;
e65fa3
 static pthread_cond_t config_cond;
e65fa3
-static pthread_t check_thr, uevent_thr, uxlsnr_thr, uevq_thr, dmevent_thr;
e65fa3
+static pthread_t check_thr, uevent_thr, uxlsnr_thr, uevq_thr, dmevent_thr,
e65fa3
+	fpin_thr, fpin_consumer_thr;
e65fa3
 static bool check_thr_started, uevent_thr_started, uxlsnr_thr_started,
e65fa3
-	uevq_thr_started, dmevent_thr_started;
e65fa3
+	uevq_thr_started, dmevent_thr_started, fpin_thr_started,
e65fa3
+	fpin_consumer_thr_started;
e65fa3
 static int pid_fd = -1;
e65fa3
 
e65fa3
 static inline enum daemon_status get_running_state(void)
e65fa3
@@ -2819,7 +2822,9 @@ reconfigure (struct vectors * vecs)
e65fa3
 	conf->sequence_nr = old->sequence_nr + 1;
e65fa3
 	rcu_assign_pointer(multipath_conf, conf);
e65fa3
 	call_rcu(&old->rcu, rcu_free_config);
e65fa3
-
e65fa3
+#ifdef FPIN_EVENT_HANDLER
e65fa3
+	fpin_clean_marginal_dev_list(NULL);
e65fa3
+#endif
e65fa3
 	configure(vecs);
e65fa3
 
e65fa3
 
e65fa3
@@ -3060,6 +3065,11 @@ static void cleanup_threads(void)
e65fa3
 		pthread_cancel(uevq_thr);
e65fa3
 	if (dmevent_thr_started)
e65fa3
 		pthread_cancel(dmevent_thr);
e65fa3
+	if (fpin_thr_started)
e65fa3
+		pthread_cancel(fpin_thr);
e65fa3
+	if (fpin_consumer_thr_started)
e65fa3
+		pthread_cancel(fpin_consumer_thr);
e65fa3
+
e65fa3
 
e65fa3
 	if (check_thr_started)
e65fa3
 		pthread_join(check_thr, NULL);
e65fa3
@@ -3071,6 +3081,11 @@ static void cleanup_threads(void)
e65fa3
 		pthread_join(uevq_thr, NULL);
e65fa3
 	if (dmevent_thr_started)
e65fa3
 		pthread_join(dmevent_thr, NULL);
e65fa3
+	if (fpin_thr_started)
e65fa3
+		pthread_join(fpin_thr, NULL);
e65fa3
+	if (fpin_consumer_thr_started)
e65fa3
+		pthread_join(fpin_consumer_thr, NULL);
e65fa3
+
e65fa3
 
e65fa3
 	/*
e65fa3
 	 * As all threads are joined now, and we're in DAEMON_SHUTDOWN
e65fa3
@@ -3168,6 +3183,7 @@ child (__attribute__((unused)) void *param)
e65fa3
 	char *envp;
e65fa3
 	enum daemon_status state;
e65fa3
 	int exit_code = 1;
e65fa3
+	int fpin_marginal_paths = 0;
e65fa3
 
e65fa3
 	init_unwinder();
e65fa3
 	mlockall(MCL_CURRENT | MCL_FUTURE);
e65fa3
@@ -3246,7 +3262,10 @@ child (__attribute__((unused)) void *param)
e65fa3
 
e65fa3
 	setscheduler();
e65fa3
 	set_oom_adj();
e65fa3
-
e65fa3
+#ifdef FPIN_EVENT_HANDLER
e65fa3
+	if (conf->marginal_pathgroups == MARGINAL_PATHGROUP_FPIN)
e65fa3
+		fpin_marginal_paths = 1;
e65fa3
+#endif
e65fa3
 	/*
e65fa3
 	 * Startup done, invalidate configuration
e65fa3
 	 */
e65fa3
@@ -3314,6 +3333,22 @@ child (__attribute__((unused)) void *param)
e65fa3
 		goto failed;
e65fa3
 	} else
e65fa3
 		uevq_thr_started = true;
e65fa3
+
e65fa3
+	if (fpin_marginal_paths) {
e65fa3
+		if ((rc = pthread_create(&fpin_thr, &misc_attr,
e65fa3
+			fpin_fabric_notification_receiver, NULL))) {
e65fa3
+			condlog(0, "failed to create the fpin receiver thread: %d", rc);
e65fa3
+			goto failed;
e65fa3
+		} else
e65fa3
+			fpin_thr_started = true;
e65fa3
+
e65fa3
+		if ((rc = pthread_create(&fpin_consumer_thr,
e65fa3
+			&misc_attr, fpin_els_li_consumer, vecs))) {
e65fa3
+			condlog(0, "failed to create the fpin consumer thread thread: %d", rc);
e65fa3
+			goto failed;
e65fa3
+		} else
e65fa3
+			fpin_consumer_thr_started = true;
e65fa3
+	}
e65fa3
 	pthread_attr_destroy(&misc_attr);
e65fa3
 
e65fa3
 	while (1) {