+ /* no routable net found go on to a different net */
+ if (!local_net)
+ continue;
+
+ /*
+ * Iterate through the NIs in this local Net and select
+ * the NI to send from. The selection is determined by
+ * these 3 criterion in the following priority:
+ * 1. NUMA
+ * 2. NI available credits
+ * 3. Round Robin
+ */
+ while ((ni = lnet_get_next_ni_locked(local_net, ni))) {
+ int ni_credits;
+
+ if (!lnet_is_ni_healthy_locked(ni))
+ continue;
+
+ ni_credits = atomic_read(&ni->ni_tx_credits);
+
+ /*
+ * calculate the distance from the cpt on which
+ * the message memory is allocated to the CPT of
+ * the NI's physical device
+ */
+ distance = cfs_cpt_distance(lnet_cpt_table(),
+ md_cpt,
+ ni->dev_cpt);
+
+ /*
+ * If we already have a closer NI within the NUMA
+ * range provided, then there is no need to
+ * consider the current NI. Move on to the next
+ * one.
+ */
+ if (distance > shortest_distance &&
+ distance > lnet_get_numa_range())
+ continue;
+
+ if (distance < shortest_distance &&
+ distance > lnet_get_numa_range()) {
+ /*
+ * The current NI is the closest one that we
+ * have found, even though it's not in the
+ * NUMA range specified. This occurs if
+ * the NUMA range is less than the least
+ * of the distances in the system.
+ * In effect NUMA range consideration is
+ * turned off.
+ */
+ shortest_distance = distance;
+ } else if ((distance <= shortest_distance &&
+ distance < lnet_get_numa_range()) ||
+ distance == shortest_distance) {
+ /*
+ * This NI is either within range or it's
+ * equidistant. In both of these cases we
+ * would want to select the NI based on
+ * its available credits first, and then
+ * via Round Robin.
+ */
+ if (distance <= shortest_distance &&
+ distance < lnet_get_numa_range()) {
+ /*
+ * If this is the first NI that's
+ * within range, then set the
+ * shortest distance to the range
+ * specified by the user. In
+ * effect we're saying that all
+ * NIs that fall within this NUMA
+ * range shall be dealt with as
+ * having equal NUMA weight. Which
+ * will mean that we should select
+ * through that set by their
+ * available credits first
+ * followed by Round Robin.
+ *
+ * And since this is the first NI
+ * in the range, let's just set it
+ * as our best_ni for now. The
+ * following NIs found in the
+ * range will be dealt with as
+ * mentioned previously.
+ */
+ shortest_distance = lnet_get_numa_range();
+ if (!found_ir) {
+ found_ir = true;
+ goto set_ni;
+ }
+ }
+ /*
+ * This NI is NUMA equidistant let's
+ * select using credits followed by Round
+ * Robin.
+ */
+ if (ni_credits < best_credits) {
+ continue;
+ } else if (ni_credits == best_credits) {
+ if (best_ni) {
+ if (best_ni->ni_seq <= ni->ni_seq)
+ continue;
+ }
+ }
+ }
+set_ni:
+ best_ni = ni;
+ best_credits = ni_credits;
+ }
+ }
+ /*
+ * if the peer is not MR capable, then we should always send to it
+ * using the first NI in the NET we determined.
+ */
+ if (!peer->lp_multi_rail && local_net != NULL)
+ best_ni = lnet_net2ni_locked(local_net->net_id, cpt);
+
+ if (!best_ni) {