+ * The gateway is Multi-Rail capable so now we must select the
+ * proper peer_ni
+ */
+ peer_net = lnet_peer_get_net_locked(peer, net_id);
+
+ if (!peer_net) {
+ CERROR("gateway peer %s has no NI on net %s\n",
+ libcfs_nid2str(peer->lp_primary_nid),
+ libcfs_net2str(net_id));
+ return NULL;
+ }
+
+ return lnet_select_peer_ni(sd, peer, peer_net);
+}
+
+static inline void
+lnet_set_non_mr_pref_nid(struct lnet_send_data *sd)
+{
+ if (sd->sd_send_case & NMR_DST &&
+ sd->sd_msg->msg_type != LNET_MSG_REPLY &&
+ sd->sd_msg->msg_type != LNET_MSG_ACK &&
+ sd->sd_best_lpni->lpni_pref_nnids == 0) {
+ CDEBUG(D_NET, "Setting preferred local NID %s on NMR peer %s\n",
+ libcfs_nid2str(sd->sd_best_ni->ni_nid),
+ libcfs_nid2str(sd->sd_best_lpni->lpni_nid));
+ lnet_peer_ni_set_non_mr_pref_nid(sd->sd_best_lpni,
+ sd->sd_best_ni->ni_nid);
+ }
+}
+
+/*
+ * Source Specified
+ * Local Destination
+ * non-mr peer
+ *
+ * use the source and destination NIDs as the pathway
+ */
+static int
+lnet_handle_spec_local_nmr_dst(struct lnet_send_data *sd)
+{
+ /* the destination lpni is set before we get here. */
+
+ /* find local NI */
+ sd->sd_best_ni = lnet_nid2ni_locked(sd->sd_src_nid, sd->sd_cpt);
+ if (!sd->sd_best_ni) {
+ CERROR("Can't send to %s: src %s is not a "
+ "local nid\n", libcfs_nid2str(sd->sd_dst_nid),
+ libcfs_nid2str(sd->sd_src_nid));
+ return -EINVAL;
+ }
+
+ /*
+ * the preferred NID will only be set for NMR peers
+ */
+ lnet_set_non_mr_pref_nid(sd);
+
+ return lnet_handle_send(sd);
+}
+
+/*
+ * Source Specified
+ * Local Destination
+ * MR Peer
+ *
+ * Run the selection algorithm on the peer NIs unless we're sending
+ * a response, in this case just send to the destination
+ */
+static int
+lnet_handle_spec_local_mr_dst(struct lnet_send_data *sd)
+{
+ sd->sd_best_ni = lnet_nid2ni_locked(sd->sd_src_nid, sd->sd_cpt);
+ if (!sd->sd_best_ni) {
+ CERROR("Can't send to %s: src %s is not a "
+ "local nid\n", libcfs_nid2str(sd->sd_dst_nid),
+ libcfs_nid2str(sd->sd_src_nid));
+ return -EINVAL;
+ }
+
+ /*
+ * only run the selection algorithm to pick the peer_ni if we're
+ * sending a GET or a PUT. Responses are sent to the same
+ * destination NID provided.
+ */
+ if (!(sd->sd_send_case & SND_RESP)) {
+ sd->sd_best_lpni =
+ lnet_find_best_lpni_on_net(sd, sd->sd_peer,
+ sd->sd_best_ni->ni_net->net_id);
+ }
+
+ if (sd->sd_best_lpni)
+ return lnet_handle_send(sd);
+
+ CERROR("can't send to %s. no NI on %s\n",
+ libcfs_nid2str(sd->sd_dst_nid),
+ libcfs_net2str(sd->sd_best_ni->ni_net->net_id));
+
+ return -EHOSTUNREACH;
+}
+
+struct lnet_ni *
+lnet_find_best_ni_on_spec_net(struct lnet_ni *cur_best_ni,
+ struct lnet_peer *peer,
+ struct lnet_peer_net *peer_net,
+ int cpt,
+ bool incr_seq)
+{
+ struct lnet_net *local_net;
+ struct lnet_ni *best_ni;
+
+ local_net = lnet_get_net_locked(peer_net->lpn_net_id);
+ if (!local_net)
+ return NULL;
+
+ /*
+ * 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
+ */
+ best_ni = lnet_get_best_ni(local_net, cur_best_ni,
+ peer, peer_net, cpt);
+
+ if (incr_seq && best_ni)
+ best_ni->ni_seq++;
+
+ return best_ni;
+}
+
+static int
+lnet_handle_find_routed_path(struct lnet_send_data *sd,
+ lnet_nid_t dst_nid,
+ struct lnet_peer_ni **gw_lpni,
+ struct lnet_peer **gw_peer)
+{
+ struct lnet_peer_ni *gw;
+ lnet_nid_t src_nid = sd->sd_src_nid;
+
+ gw = lnet_find_route_locked(NULL, LNET_NIDNET(dst_nid),
+ sd->sd_rtr_nid);
+ if (!gw) {
+ CERROR("no route to %s from %s\n",
+ libcfs_nid2str(dst_nid), libcfs_nid2str(src_nid));
+ return -EHOSTUNREACH;
+ }
+
+ /* get the peer of the gw_ni */
+ LASSERT(gw->lpni_peer_net);
+ LASSERT(gw->lpni_peer_net->lpn_peer);
+
+ *gw_peer = gw->lpni_peer_net->lpn_peer;
+
+ if (!sd->sd_best_ni)
+ sd->sd_best_ni = lnet_find_best_ni_on_spec_net(NULL, *gw_peer,
+ gw->lpni_peer_net,
+ sd->sd_md_cpt,
+ true);
+
+ if (!sd->sd_best_ni) {
+ CERROR("Internal Error. Expected local ni on %s "
+ "but non found :%s\n",
+ libcfs_net2str(gw->lpni_peer_net->lpn_net_id),
+ libcfs_nid2str(sd->sd_src_nid));
+ return -EFAULT;
+ }
+
+ /*
+ * if gw is MR let's find its best peer_ni
+ */
+ if (lnet_peer_is_multi_rail(*gw_peer)) {
+ gw = lnet_find_best_lpni_on_net(sd, *gw_peer,
+ sd->sd_best_ni->ni_net->net_id);
+ /*
+ * We've already verified that the gw has an NI on that
+ * desired net, but we're not finding it. Something is
+ * wrong.
+ */
+ if (!gw) {
+ CERROR("Internal Error. Route expected to %s from %s\n",
+ libcfs_nid2str(dst_nid),
+ libcfs_nid2str(src_nid));
+ return -EFAULT;
+ }
+ }
+
+ *gw_lpni = gw;
+
+ return 0;
+}
+
+/*
+ * Handle two cases:
+ *
+ * Case 1:
+ * Source specified
+ * Remote destination
+ * Non-MR destination
+ *
+ * Case 2:
+ * Source specified
+ * Remote destination
+ * MR destination
+ *
+ * The handling of these two cases is similar. Even though the destination
+ * can be MR or non-MR, we'll deal directly with the router.
+ */
+static int
+lnet_handle_spec_router_dst(struct lnet_send_data *sd)
+{
+ int rc;
+ struct lnet_peer_ni *gw_lpni = NULL;
+ struct lnet_peer *gw_peer = NULL;
+
+ /* find local NI */
+ sd->sd_best_ni = lnet_nid2ni_locked(sd->sd_src_nid, sd->sd_cpt);
+ if (!sd->sd_best_ni) {
+ CERROR("Can't send to %s: src %s is not a "
+ "local nid\n", libcfs_nid2str(sd->sd_dst_nid),
+ libcfs_nid2str(sd->sd_src_nid));
+ return -EINVAL;
+ }
+
+ rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw_lpni,
+ &gw_peer);
+ if (rc < 0)
+ return rc;
+
+ if (sd->sd_send_case & NMR_DST)
+ /*
+ * since the final destination is non-MR let's set its preferred
+ * NID before we send
+ */
+ lnet_set_non_mr_pref_nid(sd);
+
+ /*
+ * We're going to send to the gw found so let's set its
+ * info
+ */
+ sd->sd_peer = gw_peer;
+ sd->sd_best_lpni = gw_lpni;
+
+ return lnet_handle_send(sd);
+}
+
+struct lnet_ni *
+lnet_find_best_ni_on_local_net(struct lnet_peer *peer, int md_cpt)
+{
+ struct lnet_peer_net *peer_net = NULL;
+ struct lnet_ni *best_ni = NULL;
+
+ /*
+ * The peer can have multiple interfaces, some of them can be on
+ * the local network and others on a routed network. We should
+ * prefer the local network. However if the local network is not
+ * available then we need to try the routed network
+ */
+
+ /* go through all the peer nets and find the best_ni */
+ list_for_each_entry(peer_net, &peer->lp_peer_nets, lpn_peer_nets) {
+ /*
+ * The peer's list of nets can contain non-local nets. We
+ * want to only examine the local ones.
+ */
+ if (!lnet_get_net_locked(peer_net->lpn_net_id))
+ continue;
+ best_ni = lnet_find_best_ni_on_spec_net(best_ni, peer,
+ peer_net, md_cpt, false);
+ }
+
+ if (best_ni)
+ /* increment sequence number so we can round robin */
+ best_ni->ni_seq++;
+
+ return best_ni;
+}
+
+static struct lnet_ni *
+lnet_find_existing_preferred_best_ni(struct lnet_send_data *sd)
+{
+ struct lnet_ni *best_ni = NULL;
+ struct lnet_peer_net *peer_net;
+ struct lnet_peer *peer = sd->sd_peer;
+ struct lnet_peer_ni *best_lpni = sd->sd_best_lpni;
+ struct lnet_peer_ni *lpni;
+ int cpt = sd->sd_cpt;
+
+ /*
+ * We must use a consistent source address when sending to a
+ * non-MR peer. However, a non-MR peer can have multiple NIDs
+ * on multiple networks, and we may even need to talk to this
+ * peer on multiple networks -- certain types of
+ * load-balancing configuration do this.
+ *
+ * So we need to pick the NI the peer prefers for this
+ * particular network.
+ */
+
+ /* Get the target peer_ni */
+ peer_net = lnet_peer_get_net_locked(peer,
+ LNET_NIDNET(best_lpni->lpni_nid));
+ LASSERT(peer_net != NULL);
+ list_for_each_entry(lpni, &peer_net->lpn_peer_nis,
+ lpni_peer_nis) {
+ if (lpni->lpni_pref_nnids == 0)
+ continue;
+ LASSERT(lpni->lpni_pref_nnids == 1);
+ best_ni = lnet_nid2ni_locked(
+ lpni->lpni_pref.nid, cpt);
+ break;
+ }
+
+ return best_ni;
+}
+
+/* Prerequisite: sd->sd_peer and sd->sd_best_lpni should be set */
+static int
+lnet_select_preferred_best_ni(struct lnet_send_data *sd)
+{
+ struct lnet_ni *best_ni = NULL;
+ struct lnet_peer_ni *best_lpni = sd->sd_best_lpni;
+
+ /*
+ * We must use a consistent source address when sending to a
+ * non-MR peer. However, a non-MR peer can have multiple NIDs
+ * on multiple networks, and we may even need to talk to this
+ * peer on multiple networks -- certain types of
+ * load-balancing configuration do this.
+ *
+ * So we need to pick the NI the peer prefers for this
+ * particular network.
+ */
+
+ best_ni = lnet_find_existing_preferred_best_ni(sd);
+
+ /* if best_ni is still not set just pick one */
+ if (!best_ni) {
+ best_ni =
+ lnet_find_best_ni_on_spec_net(NULL, sd->sd_peer,
+ sd->sd_best_lpni->lpni_peer_net,
+ sd->sd_md_cpt, true);
+ /* If there is no best_ni we don't have a route */
+ if (!best_ni) {
+ CERROR("no path to %s from net %s\n",
+ libcfs_nid2str(best_lpni->lpni_nid),
+ libcfs_net2str(best_lpni->lpni_net->net_id));
+ return -EHOSTUNREACH;
+ }
+ }
+
+ sd->sd_best_ni = best_ni;
+
+ /* Set preferred NI if necessary. */
+ lnet_set_non_mr_pref_nid(sd);
+
+ return 0;
+}
+
+
+/*
+ * Source not specified
+ * Local destination
+ * Non-MR Peer
+ *
+ * always use the same source NID for NMR peers
+ * If we've talked to that peer before then we already have a preferred
+ * source NI associated with it. Otherwise, we select a preferred local NI
+ * and store it in the peer
+ */
+static int
+lnet_handle_any_local_nmr_dst(struct lnet_send_data *sd)
+{
+ int rc;
+
+ /* sd->sd_best_lpni is already set to the final destination */
+
+ /*
+ * At this point we should've created the peer ni and peer. If we
+ * can't find it, then something went wrong. Instead of assert
+ * output a relevant message and fail the send
+ */
+ if (!sd->sd_best_lpni) {
+ CERROR("Internal fault. Unable to send msg %s to %s. "
+ "NID not known\n",
+ lnet_msgtyp2str(sd->sd_msg->msg_type),
+ libcfs_nid2str(sd->sd_dst_nid));
+ return -EFAULT;
+ }
+
+ rc = lnet_select_preferred_best_ni(sd);
+ if (!rc)
+ rc = lnet_handle_send(sd);
+
+ return rc;
+}
+
+static int
+lnet_handle_any_mr_dsta(struct lnet_send_data *sd)
+{
+ /*
+ * NOTE we've already handled the remote peer case. So we only
+ * need to worry about the local case here.
+ *
+ * if we're sending a response, ACK or reply, we need to send it
+ * to the destination NID given to us. At this point we already
+ * have the peer_ni we're suppose to send to, so just find the
+ * best_ni on the peer net and use that. Since we're sending to an
+ * MR peer then we can just run the selection algorithm on our
+ * local NIs and pick the best one.
+ */
+ if (sd->sd_send_case & SND_RESP) {
+ sd->sd_best_ni =
+ lnet_find_best_ni_on_spec_net(NULL, sd->sd_peer,
+ sd->sd_best_lpni->lpni_peer_net,
+ sd->sd_md_cpt, true);
+
+ if (!sd->sd_best_ni) {
+ /*
+ * We're not going to deal with not able to send
+ * a response to the provided final destination
+ */
+ CERROR("Can't send response to %s. "
+ "No local NI available\n",
+ libcfs_nid2str(sd->sd_dst_nid));
+ return -EHOSTUNREACH;
+ }
+
+ return lnet_handle_send(sd);
+ }
+
+ /*
+ * If we get here that means we're sending a fresh request, PUT or
+ * GET, so we need to run our standard selection algorithm.
+ * First find the best local interface that's on any of the peer's
+ * networks.
+ */
+ sd->sd_best_ni = lnet_find_best_ni_on_local_net(sd->sd_peer,
+ sd->sd_md_cpt);
+ if (sd->sd_best_ni) {
+ sd->sd_best_lpni =
+ lnet_find_best_lpni_on_net(sd, sd->sd_peer,
+ sd->sd_best_ni->ni_net->net_id);
+
+ /*
+ * if we're successful in selecting a peer_ni on the local
+ * network, then send to it. Otherwise fall through and
+ * try and see if we can reach it over another routed
+ * network
+ */
+ if (sd->sd_best_lpni) {
+ /*
+ * in case we initially started with a routed
+ * destination, let's reset to local
+ */
+ sd->sd_send_case &= ~REMOTE_DST;
+ sd->sd_send_case |= LOCAL_DST;
+ return lnet_handle_send(sd);
+ }
+
+ CERROR("Internal Error. Expected to have a best_lpni: "
+ "%s -> %s\n",
+ libcfs_nid2str(sd->sd_src_nid),
+ libcfs_nid2str(sd->sd_dst_nid));
+
+ return -EFAULT;
+ }
+
+ /*
+ * Peer doesn't have a local network. Let's see if there is
+ * a remote network we can reach it on.
+ */
+ return PASS_THROUGH;
+}
+
+/*
+ * Case 1:
+ * Source NID not specified
+ * Local destination
+ * MR peer
+ *
+ * Case 2:
+ * Source NID not speified
+ * Remote destination
+ * MR peer
+ *
+ * In both of these cases if we're sending a response, ACK or REPLY, then
+ * we need to send to the destination NID provided.
+ *
+ * In the remote case let's deal with MR routers.
+ *
+ */
+
+static int
+lnet_handle_any_mr_dst(struct lnet_send_data *sd)
+{
+ int rc = 0;
+ struct lnet_peer *gw_peer = NULL;
+ struct lnet_peer_ni *gw_lpni = NULL;
+
+ /*
+ * handle sending a response to a remote peer here so we don't
+ * have to worry about it if we hit lnet_handle_any_mr_dsta()
+ */
+ if (sd->sd_send_case & REMOTE_DST &&
+ sd->sd_send_case & SND_RESP) {
+ struct lnet_peer_ni *gw;
+ struct lnet_peer *gw_peer;
+
+ rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw,
+ &gw_peer);
+ if (rc < 0) {
+ CERROR("Can't send response to %s. "
+ "No route available\n",
+ libcfs_nid2str(sd->sd_dst_nid));
+ return -EHOSTUNREACH;
+ }
+
+ sd->sd_best_lpni = gw;
+ sd->sd_peer = gw_peer;
+
+ return lnet_handle_send(sd);
+ }
+
+ /*
+ * Even though the NID for the peer might not be on a local network,
+ * since the peer is MR there could be other interfaces on the
+ * local network. In that case we'd still like to prefer the local
+ * network over the routed network. If we're unable to do that
+ * then we select the best router among the different routed networks,
+ * and if the router is MR then we can deal with it as such.
+ */
+ rc = lnet_handle_any_mr_dsta(sd);
+ if (rc != PASS_THROUGH)
+ return rc;
+
+ /*
+ * TODO; One possible enhancement is to run the selection
+ * algorithm on the peer. However for remote peers the credits are
+ * not decremented, so we'll be basically going over the peer NIs
+ * in round robin. An MR router will run the selection algorithm
+ * on the next-hop interfaces.
+ */
+ rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw_lpni,
+ &gw_peer);
+ if (rc < 0)
+ return rc;
+
+ sd->sd_send_case &= ~LOCAL_DST;
+ sd->sd_send_case |= REMOTE_DST;
+
+ sd->sd_peer = gw_peer;
+ sd->sd_best_lpni = gw_lpni;
+
+ return lnet_handle_send(sd);
+}
+
+/*
+ * Source not specified
+ * Remote destination
+ * Non-MR peer
+ *
+ * Must send to the specified peer NID using the same source NID that
+ * we've used before. If it's the first time to talk to that peer then
+ * find the source NI and assign it as preferred to that peer
+ */
+static int
+lnet_handle_any_router_nmr_dst(struct lnet_send_data *sd)
+{
+ int rc;
+ struct lnet_peer_ni *gw_lpni = NULL;
+ struct lnet_peer *gw_peer = NULL;
+
+ /*
+ * Let's set if we have a preferred NI to talk to this NMR peer
+ */
+ sd->sd_best_ni = lnet_find_existing_preferred_best_ni(sd);
+
+ /*
+ * find the router and that'll find the best NI if we didn't find
+ * it already.
+ */
+ rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw_lpni,
+ &gw_peer);
+ if (rc < 0)
+ return rc;
+
+ /*
+ * set the best_ni we've chosen as the preferred one for
+ * this peer
+ */
+ lnet_set_non_mr_pref_nid(sd);
+
+ /* we'll be sending to the gw */
+ sd->sd_best_lpni = gw_lpni;
+ sd->sd_peer = gw_peer;
+
+ return lnet_handle_send(sd);
+}
+
+static int
+lnet_handle_send_case_locked(struct lnet_send_data *sd)
+{
+ /*
+ * turn off the SND_RESP bit.
+ * It will be checked in the case handling
+ */
+ __u32 send_case = sd->sd_send_case &= ~SND_RESP ;
+
+ CDEBUG(D_NET, "Source %s%s to %s %s %s destination\n",
+ (send_case & SRC_SPEC) ? "Specified: " : "ANY",
+ (send_case & SRC_SPEC) ? libcfs_nid2str(sd->sd_src_nid) : "",
+ (send_case & MR_DST) ? "MR: " : "NMR: ",
+ libcfs_nid2str(sd->sd_dst_nid),
+ (send_case & LOCAL_DST) ? "local" : "routed");
+
+ switch (send_case) {
+ /*
+ * For all cases where the source is specified, we should always
+ * use the destination NID, whether it's an MR destination or not,
+ * since we're continuing a series of related messages for the
+ * same RPC
+ */
+ case SRC_SPEC_LOCAL_NMR_DST:
+ return lnet_handle_spec_local_nmr_dst(sd);
+ case SRC_SPEC_LOCAL_MR_DST:
+ return lnet_handle_spec_local_mr_dst(sd);
+ case SRC_SPEC_ROUTER_NMR_DST:
+ case SRC_SPEC_ROUTER_MR_DST:
+ return lnet_handle_spec_router_dst(sd);
+ case SRC_ANY_LOCAL_NMR_DST:
+ return lnet_handle_any_local_nmr_dst(sd);
+ case SRC_ANY_LOCAL_MR_DST:
+ case SRC_ANY_ROUTER_MR_DST:
+ return lnet_handle_any_mr_dst(sd);
+ case SRC_ANY_ROUTER_NMR_DST:
+ return lnet_handle_any_router_nmr_dst(sd);
+ default:
+ CERROR("Unknown send case\n");
+ return -1;
+ }
+}
+
+static int
+lnet_select_pathway(lnet_nid_t src_nid, lnet_nid_t dst_nid,
+ struct lnet_msg *msg, lnet_nid_t rtr_nid)
+{
+ struct lnet_peer_ni *lpni;
+ struct lnet_peer *peer;
+ struct lnet_send_data send_data;
+ int cpt, rc;
+ int md_cpt;
+ __u32 send_case = 0;
+
+ memset(&send_data, 0, sizeof(send_data));
+
+ /*
+ * get an initial CPT to use for locking. The idea here is not to
+ * serialize the calls to select_pathway, so that as many
+ * operations can run concurrently as possible. To do that we use
+ * the CPT where this call is being executed. Later on when we
+ * determine the CPT to use in lnet_message_commit, we switch the
+ * lock and check if there was any configuration change. If none,
+ * then we proceed, if there is, then we restart the operation.
+ */
+ cpt = lnet_net_lock_current();
+
+ md_cpt = lnet_cpt_of_md(msg->msg_md, msg->msg_offset);
+ if (md_cpt == CFS_CPT_ANY)
+ md_cpt = cpt;
+
+again:
+
+ /*
+ * If we're being asked to send to the loopback interface, there
+ * is no need to go through any selection. We can just shortcut
+ * the entire process and send over lolnd