Blame SOURCES/0090-UPBZ-1080038-reorder-paths-for-round-robin.patch

38852f
---
38852f
 libmultipath/configure.c |  229 +++++++++++++++++++++++++++++++++++++++++++++++
38852f
 libmultipath/configure.h |    2 
38852f
 libmultipath/discovery.c |   87 +++++++++++++++++
38852f
 libmultipath/discovery.h |    2 
38852f
 libmultipath/structs.c   |   84 +++++++++++++++++
38852f
 libmultipath/structs.h   |   25 ++++-
38852f
 6 files changed, 427 insertions(+), 2 deletions(-)
38852f
38852f
Index: multipath-tools-130222/libmultipath/configure.c
38852f
===================================================================
38852f
--- multipath-tools-130222.orig/libmultipath/configure.c
38852f
+++ multipath-tools-130222/libmultipath/configure.c
38852f
@@ -39,6 +39,219 @@
38852f
 #include "uxsock.h"
38852f
 #include "wwids.h"
38852f
 
38852f
+/* group paths in pg by host adapter
38852f
+ */
38852f
+int group_by_host_adapter(struct pathgroup *pgp, vector adapters)
38852f
+{
38852f
+	struct adapter_group *agp;
38852f
+	struct host_group *hgp;
38852f
+	struct path *pp, *pp1;
38852f
+	char adapter_name1[SLOT_NAME_SIZE];
38852f
+	char adapter_name2[SLOT_NAME_SIZE];
38852f
+	int i, j;
38852f
+	int found_hostgroup = 0;
38852f
+
38852f
+	while (VECTOR_SIZE(pgp->paths) > 0) {
38852f
+
38852f
+		pp = VECTOR_SLOT(pgp->paths, 0);
38852f
+
38852f
+		if (sysfs_get_host_adapter_name(pp, adapter_name1))
38852f
+			goto out;
38852f
+		/* create a new host adapter group
38852f
+		 */
38852f
+		agp = alloc_adaptergroup();
38852f
+		if (!agp)
38852f
+			goto out;
38852f
+		agp->pgp = pgp;
38852f
+
38852f
+		strncpy(agp->adapter_name, adapter_name1, SLOT_NAME_SIZE);
38852f
+		store_adaptergroup(adapters, agp);
38852f
+
38852f
+		/* create a new host port group
38852f
+		 */
38852f
+		hgp = alloc_hostgroup();
38852f
+		if (!hgp)
38852f
+			goto out;
38852f
+		if (store_hostgroup(agp->host_groups, hgp))
38852f
+			goto out;
38852f
+
38852f
+		hgp->host_no = pp->sg_id.host_no;
38852f
+		agp->num_hosts++;
38852f
+		if (store_path(hgp->paths, pp))
38852f
+			goto out;
38852f
+
38852f
+		hgp->num_paths++;
38852f
+		/* delete path from path group
38852f
+		 */
38852f
+		vector_del_slot(pgp->paths, 0);
38852f
+
38852f
+		/* add all paths belonging to same host adapter
38852f
+		 */
38852f
+		vector_foreach_slot(pgp->paths, pp1, i) {
38852f
+			if (sysfs_get_host_adapter_name(pp1, adapter_name2))
38852f
+				goto out;
38852f
+			if (strcmp(adapter_name1, adapter_name2) == 0) {
38852f
+				found_hostgroup = 0;
38852f
+				vector_foreach_slot(agp->host_groups, hgp, j) {
38852f
+					if (hgp->host_no == pp1->sg_id.host_no) {
38852f
+						if (store_path(hgp->paths, pp1))
38852f
+							goto out;
38852f
+						hgp->num_paths++;
38852f
+						found_hostgroup = 1;
38852f
+						break;
38852f
+					}
38852f
+				}
38852f
+				if (!found_hostgroup) {
38852f
+					/* this path belongs to new host port
38852f
+					 * within this adapter
38852f
+					 */
38852f
+					hgp = alloc_hostgroup();
38852f
+					if (!hgp)
38852f
+						goto out;
38852f
+
38852f
+					if (store_hostgroup(agp->host_groups, hgp))
38852f
+						goto out;
38852f
+
38852f
+					agp->num_hosts++;
38852f
+					if (store_path(hgp->paths, pp1))
38852f
+						goto out;
38852f
+
38852f
+					hgp->host_no = pp1->sg_id.host_no;
38852f
+					hgp->num_paths++;
38852f
+				}
38852f
+				/* delete paths from original path_group
38852f
+				 * as they are added into adapter group now
38852f
+				 */
38852f
+				vector_del_slot(pgp->paths, i);
38852f
+				i--;
38852f
+			}
38852f
+		}
38852f
+	}
38852f
+	return 0;
38852f
+
38852f
+out:	/* add back paths into pg as re-ordering failed
38852f
+	 */
38852f
+	vector_foreach_slot(adapters, agp, i) {
38852f
+			vector_foreach_slot(agp->host_groups, hgp, j) {
38852f
+				while (VECTOR_SIZE(hgp->paths) > 0) {
38852f
+					pp = VECTOR_SLOT(hgp->paths, 0);
38852f
+					if (store_path(pgp->paths, pp))
38852f
+						condlog(3, "failed to restore "
38852f
+						"path %s into path group",
38852f
+						 pp->dev);
38852f
+					vector_del_slot(hgp->paths, 0);
38852f
+				}
38852f
+			}
38852f
+		}
38852f
+	free_adaptergroup(adapters);
38852f
+	return 1;
38852f
+}
38852f
+
38852f
+/* re-order paths in pg by alternating adapters and host ports
38852f
+ * for optimized selection
38852f
+ */
38852f
+int order_paths_in_pg_by_alt_adapters(struct pathgroup *pgp, vector adapters,
38852f
+		 int total_paths)
38852f
+{
38852f
+	int next_adapter_index = 0;
38852f
+	struct adapter_group *agp;
38852f
+	struct host_group *hgp;
38852f
+	struct path *pp;
38852f
+
38852f
+	while (total_paths > 0) {
38852f
+		agp = VECTOR_SLOT(adapters, next_adapter_index);
38852f
+		if (!agp) {
38852f
+			condlog(0, "can't get adapter group %d", next_adapter_index);
38852f
+			return 1;
38852f
+		}
38852f
+
38852f
+		hgp = VECTOR_SLOT(agp->host_groups, agp->next_host_index);
38852f
+		if (!hgp) {
38852f
+			condlog(0, "can't get host group %d of adapter group %d", next_adapter_index, agp->next_host_index);
38852f
+			return 1;
38852f
+		}
38852f
+
38852f
+		if (!hgp->num_paths) {
38852f
+			agp->next_host_index++;
38852f
+			agp->next_host_index %= agp->num_hosts;
38852f
+			next_adapter_index++;
38852f
+			next_adapter_index %= VECTOR_SIZE(adapters);
38852f
+			continue;
38852f
+		}
38852f
+
38852f
+		pp  = VECTOR_SLOT(hgp->paths, 0);
38852f
+
38852f
+		if (store_path(pgp->paths, pp))
38852f
+			return 1;
38852f
+
38852f
+		total_paths--;
38852f
+
38852f
+		vector_del_slot(hgp->paths, 0);
38852f
+
38852f
+		hgp->num_paths--;
38852f
+
38852f
+		agp->next_host_index++;
38852f
+		agp->next_host_index %= agp->num_hosts;
38852f
+		next_adapter_index++;
38852f
+		next_adapter_index %= VECTOR_SIZE(adapters);
38852f
+	}
38852f
+
38852f
+	/* all paths are added into path_group
38852f
+	 * in crafted child order
38852f
+	 */
38852f
+	return 0;
38852f
+}
38852f
+
38852f
+/* round-robin: order paths in path group to alternate
38852f
+ * between all host adapters
38852f
+ */
38852f
+int rr_optimize_path_order(struct pathgroup *pgp)
38852f
+{
38852f
+	vector adapters;
38852f
+	struct path *pp;
38852f
+	int total_paths;
38852f
+	int i;
38852f
+
38852f
+	total_paths = VECTOR_SIZE(pgp->paths);
38852f
+	vector_foreach_slot(pgp->paths, pp, i) {
38852f
+		if (pp->sg_id.proto_id != SCSI_PROTOCOL_FCP &&
38852f
+			pp->sg_id.proto_id != SCSI_PROTOCOL_SAS &&
38852f
+			pp->sg_id.proto_id != SCSI_PROTOCOL_ISCSI &&
38852f
+			pp->sg_id.proto_id != SCSI_PROTOCOL_SRP) {
38852f
+			/* return success as default path order
38852f
+			 * is maintained in path group
38852f
+			 */
38852f
+			return 0;
38852f
+		}
38852f
+	}
38852f
+	adapters = vector_alloc();
38852f
+	if (!adapters)
38852f
+		return 0;
38852f
+
38852f
+	/* group paths in path group by host adapters
38852f
+	 */
38852f
+	if (group_by_host_adapter(pgp, adapters)) {
38852f
+		/* already freed adapters */
38852f
+		condlog(3, "Failed to group paths by adapters");
38852f
+		return 0;
38852f
+	}
38852f
+
38852f
+	/* re-order paths in pg to alternate between adapters and host ports
38852f
+	 */
38852f
+	if (order_paths_in_pg_by_alt_adapters(pgp, adapters, total_paths)) {
38852f
+		condlog(3, "Failed to re-order paths in pg by adapters "
38852f
+			"and host ports");
38852f
+		free_adaptergroup(adapters);
38852f
+		/* return failure as original paths are
38852f
+		 * removed form pgp
38852f
+		 */
38852f
+		return 1;
38852f
+	}
38852f
+
38852f
+	free_adaptergroup(adapters);
38852f
+	return 0;
38852f
+}
38852f
+
38852f
 extern int
38852f
 setup_map (struct multipath * mpp, char * params, int params_size)
38852f
 {
38852f
@@ -101,6 +314,22 @@ setup_map (struct multipath * mpp, char
38852f
 	 */
38852f
 	mpp->bestpg = select_path_group(mpp);
38852f
 
38852f
+	/* re-order paths in all path groups in an optimized way
38852f
+	 * for round-robin path selectors to get maximum throughput.
38852f
+	 */
38852f
+	if (!strncmp(mpp->selector, "round-robin", 11)) {
38852f
+		vector_foreach_slot(mpp->pg, pgp, i) {
38852f
+			if (VECTOR_SIZE(pgp->paths) <= 2)
38852f
+				continue;
38852f
+			if (rr_optimize_path_order(pgp)) {
38852f
+				condlog(2, "cannot re-order paths for "
38852f
+					"optimization: %s",
38852f
+					mpp->alias);
38852f
+				return 1;
38852f
+			}
38852f
+		}
38852f
+	}
38852f
+
38852f
 	/*
38852f
 	 * transform the mp->pg vector of vectors of paths
38852f
 	 * into a mp->params strings to feed the device-mapper
38852f
Index: multipath-tools-130222/libmultipath/configure.h
38852f
===================================================================
38852f
--- multipath-tools-130222.orig/libmultipath/configure.h
38852f
+++ multipath-tools-130222/libmultipath/configure.h
38852f
@@ -29,4 +29,4 @@ int reinstate_paths (struct multipath *m
38852f
 int coalesce_paths (struct vectors *vecs, vector curmp, char * refwwid, int force_reload);
38852f
 int get_refwwid (char * dev, enum devtypes dev_type, vector pathvec, char **wwid);
38852f
 int reload_map(struct vectors *vecs, struct multipath *mpp, int refresh);
38852f
-
38852f
+int sysfs_get_host_adapter_name(struct path *pp, char *adapter_name);
38852f
Index: multipath-tools-130222/libmultipath/discovery.c
38852f
===================================================================
38852f
--- multipath-tools-130222.orig/libmultipath/discovery.c
38852f
+++ multipath-tools-130222/libmultipath/discovery.c
38852f
@@ -310,6 +310,93 @@ sysfs_get_tgt_nodename (struct path *pp,
38852f
 	return 1;
38852f
 }
38852f
 
38852f
+int sysfs_get_host_adapter_name(struct path *pp, char *adapter_name)
38852f
+{
38852f
+	int proto_id;
38852f
+
38852f
+	if (!pp || !adapter_name)
38852f
+		return 1;
38852f
+
38852f
+	proto_id = pp->sg_id.proto_id;
38852f
+
38852f
+	if (proto_id != SCSI_PROTOCOL_FCP &&
38852f
+	    proto_id != SCSI_PROTOCOL_SAS &&
38852f
+	    proto_id != SCSI_PROTOCOL_ISCSI &&
38852f
+	    proto_id != SCSI_PROTOCOL_SRP) {
38852f
+		return 1;
38852f
+	}
38852f
+	/* iscsi doesn't have adapter info in sysfs
38852f
+	 * get ip_address for grouping paths
38852f
+	 */
38852f
+	if (pp->sg_id.proto_id == SCSI_PROTOCOL_ISCSI)
38852f
+		return sysfs_get_iscsi_ip_address(pp, adapter_name);
38852f
+
38852f
+	/* fetch adapter pci name for other protocols
38852f
+	 */
38852f
+	return sysfs_get_host_pci_name(pp, adapter_name);
38852f
+}
38852f
+
38852f
+int sysfs_get_host_pci_name(struct path *pp, char *pci_name)
38852f
+{
38852f
+	struct udev_device *hostdev, *parent;
38852f
+	char host_name[HOST_NAME_LEN];
38852f
+	const char *driver_name, *value;
38852f
+
38852f
+	if (!pp || !pci_name)
38852f
+		return 1;
38852f
+
38852f
+	sprintf(host_name, "host%d", pp->sg_id.host_no);
38852f
+	hostdev = udev_device_new_from_subsystem_sysname(conf->udev,
38852f
+			"scsi_host", host_name);
38852f
+	if (!hostdev)
38852f
+		return 1;
38852f
+
38852f
+	parent = udev_device_get_parent(hostdev);
38852f
+	while (parent) {
38852f
+		driver_name = udev_device_get_driver(parent);
38852f
+		if (!driver_name) {
38852f
+			parent = udev_device_get_parent(parent);
38852f
+			continue;
38852f
+		}
38852f
+		if (!strcmp(driver_name, "pcieport"))
38852f
+			break;
38852f
+		parent = udev_device_get_parent(parent);
38852f
+	}
38852f
+	if (parent) {
38852f
+		/* pci_device found
38852f
+		 */
38852f
+		value = udev_device_get_sysname(parent);
38852f
+
38852f
+		strncpy(pci_name, value, SLOT_NAME_SIZE);
38852f
+		udev_device_unref(hostdev);
38852f
+		return 0;
38852f
+	}
38852f
+	udev_device_unref(hostdev);
38852f
+	return 1;
38852f
+}
38852f
+
38852f
+int sysfs_get_iscsi_ip_address(struct path *pp, char *ip_address)
38852f
+{
38852f
+	struct udev_device *hostdev;
38852f
+	char host_name[HOST_NAME_LEN];
38852f
+	const char *value;
38852f
+
38852f
+	sprintf(host_name, "host%d", pp->sg_id.host_no);
38852f
+	hostdev = udev_device_new_from_subsystem_sysname(conf->udev,
38852f
+			"iscsi_host", host_name);
38852f
+	if (hostdev) {
38852f
+		value = udev_device_get_sysattr_value(hostdev,
38852f
+				"ipaddress");
38852f
+		if (value) {
38852f
+			strncpy(ip_address, value, SLOT_NAME_SIZE);
38852f
+			udev_device_unref(hostdev);
38852f
+			return 0;
38852f
+		} else
38852f
+			udev_device_unref(hostdev);
38852f
+	}
38852f
+	return 1;
38852f
+}
38852f
+
38852f
 static void
38852f
 sysfs_set_rport_tmo(struct multipath *mpp, struct path *pp)
38852f
 {
38852f
Index: multipath-tools-130222/libmultipath/discovery.h
38852f
===================================================================
38852f
--- multipath-tools-130222.orig/libmultipath/discovery.h
38852f
+++ multipath-tools-130222/libmultipath/discovery.h
38852f
@@ -38,6 +38,8 @@ int store_pathinfo (vector pathvec, vect
38852f
 		    struct path **pp_ptr);
38852f
 int sysfs_set_scsi_tmo (struct multipath *mpp);
38852f
 int sysfs_get_timeout(struct path *pp, unsigned int *timeout);
38852f
+int sysfs_get_host_pci_name(struct path *pp, char *pci_name);
38852f
+int sysfs_get_iscsi_ip_address(struct path *pp, char *ip_address);
38852f
 
38852f
 /*
38852f
  * discovery bitmask
38852f
Index: multipath-tools-130222/libmultipath/structs.c
38852f
===================================================================
38852f
--- multipath-tools-130222.orig/libmultipath/structs.c
38852f
+++ multipath-tools-130222/libmultipath/structs.c
38852f
@@ -18,6 +18,70 @@
38852f
 #include "blacklist.h"
38852f
 #include "prio.h"
38852f
 
38852f
+struct adapter_group *
38852f
+alloc_adaptergroup(void)
38852f
+{
38852f
+	struct adapter_group *agp;
38852f
+
38852f
+	agp = (struct adapter_group *)MALLOC(sizeof(struct adapter_group));
38852f
+
38852f
+	if (!agp)
38852f
+		return NULL;
38852f
+
38852f
+	agp->host_groups = vector_alloc();
38852f
+	if (!agp->host_groups) {
38852f
+		FREE(agp);
38852f
+		agp = NULL;
38852f
+	}
38852f
+	return agp;
38852f
+}
38852f
+
38852f
+void free_adaptergroup(vector adapters)
38852f
+{
38852f
+	int i;
38852f
+	struct adapter_group *agp;
38852f
+
38852f
+	vector_foreach_slot(adapters, agp, i) {
38852f
+		free_hostgroup(agp->host_groups);
38852f
+		FREE(agp);
38852f
+	}
38852f
+	vector_free(adapters);
38852f
+}
38852f
+
38852f
+void free_hostgroup(vector hostgroups)
38852f
+{
38852f
+	int i;
38852f
+	struct host_group *hgp;
38852f
+
38852f
+	if (!hostgroups)
38852f
+		return;
38852f
+
38852f
+	vector_foreach_slot(hostgroups, hgp, i) {
38852f
+		vector_free(hgp->paths);
38852f
+		FREE(hgp);
38852f
+	}
38852f
+	vector_free(hostgroups);
38852f
+}
38852f
+
38852f
+struct host_group *
38852f
+alloc_hostgroup(void)
38852f
+{
38852f
+	struct host_group *hgp;
38852f
+
38852f
+	hgp = (struct host_group *)MALLOC(sizeof(struct host_group));
38852f
+
38852f
+	if (!hgp)
38852f
+		return NULL;
38852f
+
38852f
+	hgp->paths = vector_alloc();
38852f
+
38852f
+	if (!hgp->paths) {
38852f
+		FREE(hgp);
38852f
+		hgp = NULL;
38852f
+	}
38852f
+	return hgp;
38852f
+}
38852f
+
38852f
 struct path *
38852f
 alloc_path (void)
38852f
 {
38852f
@@ -242,6 +306,26 @@ store_pathgroup (vector pgvec, struct pa
38852f
 	return 0;
38852f
 }
38852f
 
38852f
+int
38852f
+store_hostgroup(vector hostgroupvec, struct host_group * hgp)
38852f
+{
38852f
+	if (!vector_alloc_slot(hostgroupvec))
38852f
+		return 1;
38852f
+
38852f
+	vector_set_slot(hostgroupvec, hgp);
38852f
+	return 0;
38852f
+}
38852f
+
38852f
+int
38852f
+store_adaptergroup(vector adapters, struct adapter_group * agp)
38852f
+{
38852f
+	if (!vector_alloc_slot(adapters))
38852f
+		return 1;
38852f
+
38852f
+	vector_set_slot(adapters, agp);
38852f
+	return 0;
38852f
+}
38852f
+
38852f
 struct multipath *
38852f
 find_mp_by_minor (vector mpvec, int minor)
38852f
 {
38852f
Index: multipath-tools-130222/libmultipath/structs.h
38852f
===================================================================
38852f
--- multipath-tools-130222.orig/libmultipath/structs.h
38852f
+++ multipath-tools-130222/libmultipath/structs.h
38852f
@@ -15,7 +15,8 @@
38852f
 #define BLK_DEV_SIZE		33
38852f
 #define PATH_SIZE		512
38852f
 #define NAME_SIZE		512
38852f
-
38852f
+#define HOST_NAME_LEN		8
38852f
+#define SLOT_NAME_SIZE		40
38852f
 
38852f
 #define SCSI_VENDOR_SIZE	9
38852f
 #define SCSI_PRODUCT_SIZE	17
38852f
@@ -251,6 +252,20 @@ struct pathgroup {
38852f
 	char * selector;
38852f
 };
38852f
 
38852f
+struct adapter_group {
38852f
+	char adapter_name[SLOT_NAME_SIZE];
38852f
+	struct pathgroup *pgp;
38852f
+	int num_hosts;
38852f
+	vector host_groups;
38852f
+	int next_host_index;
38852f
+};
38852f
+
38852f
+struct host_group {
38852f
+	int host_no;
38852f
+	int num_paths;
38852f
+	vector paths;
38852f
+};
38852f
+
38852f
 struct path * alloc_path (void);
38852f
 struct pathgroup * alloc_pathgroup (void);
38852f
 struct multipath * alloc_multipath (void);
38852f
@@ -263,6 +278,14 @@ void free_multipath_attributes (struct m
38852f
 void drop_multipath (vector mpvec, char * wwid, enum free_path_mode free_paths);
38852f
 void free_multipathvec (vector mpvec, enum free_path_mode free_paths);
38852f
 
38852f
+struct adapter_group * alloc_adaptergroup(void);
38852f
+struct host_group * alloc_hostgroup(void);
38852f
+void free_adaptergroup(vector adapters);
38852f
+void free_hostgroup(vector hostgroups);
38852f
+
38852f
+int store_adaptergroup(vector adapters, struct adapter_group *agp);
38852f
+int store_hostgroup(vector hostgroupvec, struct host_group *hgp);
38852f
+
38852f
 int store_path (vector pathvec, struct path * pp);
38852f
 int store_pathgroup (vector pgvec, struct pathgroup * pgp);
38852f