Whamcloud - gitweb
LU-12756 lnet: Refactor lnet_find_existing_preferred_best_ni
[fs/lustre-release.git] / lnet / lnet / lib-move.c
index 615d755..b735341 100644 (file)
@@ -165,7 +165,7 @@ lnet_fail_nid(lnet_nid_t nid, unsigned int threshold)
        struct lnet_test_peer *tp;
        struct list_head *el;
        struct list_head *next;
-       struct list_head  cull;
+       LIST_HEAD(cull);
 
        /* NB: use lnet_net_lock(0) to serialize operations on test peers */
        if (threshold != 0) {
@@ -183,9 +183,6 @@ lnet_fail_nid(lnet_nid_t nid, unsigned int threshold)
                return 0;
        }
 
-       /* removing entries */
-       INIT_LIST_HEAD(&cull);
-
        lnet_net_lock(0);
 
        list_for_each_safe(el, next, &the_lnet.ln_test_peers) {
@@ -215,10 +212,8 @@ fail_peer (lnet_nid_t nid, int outgoing)
        struct lnet_test_peer *tp;
        struct list_head *el;
        struct list_head *next;
-       struct list_head  cull;
-       int               fail = 0;
-
-       INIT_LIST_HEAD(&cull);
+       LIST_HEAD(cull);
+       int fail = 0;
 
        /* NB: use lnet_net_lock(0) to serialize operations on test peers */
        lnet_net_lock(0);
@@ -284,7 +279,7 @@ lnet_copy_iov2iov(unsigned int ndiov, struct kvec *diov, unsigned int doffset,
                  unsigned int nob)
 {
        /* NB diov, siov are READ-ONLY */
-       unsigned int  this_nob;
+       unsigned int this_nob;
 
        if (nob == 0)
                return;
@@ -310,9 +305,9 @@ lnet_copy_iov2iov(unsigned int ndiov, struct kvec *diov, unsigned int doffset,
        do {
                LASSERT(ndiov > 0);
                LASSERT(nsiov > 0);
-               this_nob = MIN(diov->iov_len - doffset,
-                              siov->iov_len - soffset);
-               this_nob = MIN(this_nob, nob);
+               this_nob = min3((unsigned int)diov->iov_len - doffset,
+                               (unsigned int)siov->iov_len - soffset,
+                               nob);
 
                memcpy((char *)diov->iov_base + doffset,
                       (char *)siov->iov_base + soffset, this_nob);
@@ -432,9 +427,9 @@ lnet_copy_kiov2kiov(unsigned int ndiov, lnet_kiov_t *diov, unsigned int doffset,
        do {
                LASSERT(ndiov > 0);
                LASSERT(nsiov > 0);
-               this_nob = MIN(diov->kiov_len - doffset,
-                              siov->kiov_len - soffset);
-               this_nob = MIN(this_nob, nob);
+               this_nob = min3(diov->kiov_len - doffset,
+                               siov->kiov_len - soffset,
+                               nob);
 
                if (daddr == NULL)
                        daddr = ((char *)kmap(diov->kiov_page)) +
@@ -513,9 +508,9 @@ lnet_copy_kiov2iov (unsigned int niov, struct kvec *iov, unsigned int iovoffset,
        do {
                LASSERT(niov > 0);
                LASSERT(nkiov > 0);
-               this_nob = MIN(iov->iov_len - iovoffset,
-                              kiov->kiov_len - kiovoffset);
-               this_nob = MIN(this_nob, nob);
+               this_nob = min3((unsigned int)iov->iov_len - iovoffset,
+                               (unsigned int)kiov->kiov_len - kiovoffset,
+                               nob);
 
                if (addr == NULL)
                        addr = ((char *)kmap(kiov->kiov_page)) +
@@ -583,9 +578,9 @@ lnet_copy_iov2kiov(unsigned int nkiov, lnet_kiov_t *kiov, unsigned int kiovoffse
        do {
                LASSERT(nkiov > 0);
                LASSERT(niov > 0);
-               this_nob = MIN(kiov->kiov_len - kiovoffset,
-                              iov->iov_len - iovoffset);
-               this_nob = MIN(this_nob, nob);
+               this_nob = min3((unsigned int)kiov->kiov_len - kiovoffset,
+                               (unsigned int)iov->iov_len - iovoffset,
+                               nob);
 
                if (addr == NULL)
                        addr = ((char *)kmap(kiov->kiov_page)) +
@@ -1269,8 +1264,7 @@ routing_off:
                /* drop all messages which are queued to be routed on that
                 * peer. */
                if (!the_lnet.ln_routing) {
-                       struct list_head drop;
-                       INIT_LIST_HEAD(&drop);
+                       LIST_HEAD(drop);
                        list_splice_init(&lp->lp_rtrq, &drop);
                        spin_unlock(&lp->lp_lock);
                        spin_unlock(&rxpeerni->lpni_lock);
@@ -1848,17 +1842,15 @@ lnet_handle_send(struct lnet_send_data *sd)
 }
 
 static inline void
-lnet_set_non_mr_pref_nid(struct lnet_send_data *sd)
+lnet_set_non_mr_pref_nid(struct lnet_peer_ni *lpni, struct lnet_ni *lni,
+                        struct lnet_msg *msg)
 {
-       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) {
+       if (msg->msg_type != LNET_MSG_REPLY && msg->msg_type != LNET_MSG_ACK &&
+           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);
+                      libcfs_nid2str(lni->ni_nid),
+                      libcfs_nid2str(lpni->lpni_nid));
+               lnet_peer_ni_set_non_mr_pref_nid(lpni, lni->ni_nid);
        }
 }
 
@@ -1883,10 +1875,7 @@ lnet_handle_spec_local_nmr_dst(struct lnet_send_data *sd)
                return -EINVAL;
        }
 
-       /*
-        * the preferred NID will only be set for NMR peers
-        */
-       lnet_set_non_mr_pref_nid(sd);
+       lnet_set_non_mr_pref_nid(sd->sd_best_lpni, sd->sd_best_ni, sd->sd_msg);
 
        return lnet_handle_send(sd);
 }
@@ -2187,10 +2176,11 @@ lnet_handle_spec_router_dst(struct lnet_send_data *sd)
 
        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);
+                * since the final destination is non-MR let's set its preferred
+                * NID before we send
+                */
+               lnet_set_non_mr_pref_nid(sd->sd_best_lpni, sd->sd_best_ni,
+                                        sd->sd_msg);
 
        /*
         * We're going to send to the gw found so let's set its
@@ -2244,14 +2234,11 @@ lnet_find_best_ni_on_local_net(struct lnet_peer *peer, int md_cpt,
 }
 
 static struct lnet_ni *
-lnet_find_existing_preferred_best_ni(struct lnet_send_data *sd)
+lnet_find_existing_preferred_best_ni(struct lnet_peer_ni *lpni, int cpt)
 {
        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;
+       struct lnet_peer_net *peer_net = lpni->lpni_peer_net;
+       struct lnet_peer_ni *lpni_entry;
 
        /*
         * We must use a consistent source address when sending to a
@@ -2263,18 +2250,13 @@ lnet_find_existing_preferred_best_ni(struct lnet_send_data *sd)
         * 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)
+       LASSERT(peer_net);
+       list_for_each_entry(lpni_entry, &peer_net->lpn_peer_nis,
+                           lpni_peer_nis) {
+               if (lpni_entry->lpni_pref_nnids == 0)
                        continue;
-               LASSERT(lpni->lpni_pref_nnids == 1);
-               best_ni = lnet_nid2ni_locked(
-                               lpni->lpni_pref.nid, cpt);
+               LASSERT(lpni_entry->lpni_pref_nnids == 1);
+               best_ni = lnet_nid2ni_locked(lpni_entry->lpni_pref.nid, cpt);
                break;
        }
 
@@ -2299,7 +2281,8 @@ lnet_select_preferred_best_ni(struct lnet_send_data *sd)
         * particular network.
         */
 
-       best_ni = lnet_find_existing_preferred_best_ni(sd);
+       best_ni = lnet_find_existing_preferred_best_ni(sd->sd_best_lpni,
+                                                      sd->sd_cpt);
 
        /* if best_ni is still not set just pick one */
        if (!best_ni) {
@@ -2319,7 +2302,7 @@ lnet_select_preferred_best_ni(struct lnet_send_data *sd)
        sd->sd_best_ni = best_ni;
 
        /* Set preferred NI if necessary. */
-       lnet_set_non_mr_pref_nid(sd);
+       lnet_set_non_mr_pref_nid(sd->sd_best_lpni, sd->sd_best_ni, sd->sd_msg);
 
        return 0;
 }
@@ -2552,9 +2535,10 @@ lnet_handle_any_router_nmr_dst(struct lnet_send_data *sd)
        struct lnet_peer *gw_peer = NULL;
 
        /*
-        * Let's set if we have a preferred NI to talk to this NMR peer
+        * Let's see if we have a preferred NI to talk to this NMR peer
         */
-       sd->sd_best_ni = lnet_find_existing_preferred_best_ni(sd);
+       sd->sd_best_ni = lnet_find_existing_preferred_best_ni(sd->sd_best_lpni,
+                                                             sd->sd_cpt);
 
        /*
         * find the router and that'll find the best NI if we didn't find
@@ -2569,7 +2553,7 @@ lnet_handle_any_router_nmr_dst(struct lnet_send_data *sd)
         * set the best_ni we've chosen as the preferred one for
         * this peer
         */
-       lnet_set_non_mr_pref_nid(sd);
+       lnet_set_non_mr_pref_nid(sd->sd_best_lpni, sd->sd_best_ni, sd->sd_msg);
 
        /* we'll be sending to the gw */
        sd->sd_best_lpni = gw_lpni;
@@ -2867,7 +2851,6 @@ static void
 lnet_finalize_expired_responses(void)
 {
        struct lnet_libmd *md;
-       struct list_head local_queue;
        struct lnet_rsp_tracker *rspt, *tmp;
        ktime_t now;
        int i;
@@ -2876,7 +2859,7 @@ lnet_finalize_expired_responses(void)
                return;
 
        cfs_cpt_for_each(i, lnet_cpt_table()) {
-               INIT_LIST_HEAD(&local_queue);
+               LIST_HEAD(local_queue);
 
                lnet_net_lock(i);
                if (!the_lnet.ln_mt_rstq[i]) {
@@ -3104,8 +3087,8 @@ static void
 lnet_recover_local_nis(void)
 {
        struct lnet_mt_event_info *ev_info;
-       struct list_head processed_list;
-       struct list_head local_queue;
+       LIST_HEAD(processed_list);
+       LIST_HEAD(local_queue);
        struct lnet_handle_md mdh;
        struct lnet_ni *tmp;
        struct lnet_ni *ni;
@@ -3113,9 +3096,6 @@ lnet_recover_local_nis(void)
        int healthv;
        int rc;
 
-       INIT_LIST_HEAD(&local_queue);
-       INIT_LIST_HEAD(&processed_list);
-
        /*
         * splice the recovery queue on a local queue. We will iterate
         * through the local queue and update it as needed. Once we're
@@ -3346,11 +3326,9 @@ static void
 lnet_clean_resendqs(void)
 {
        struct lnet_msg *msg, *tmp;
-       struct list_head msgs;
+       LIST_HEAD(msgs);
        int i;
 
-       INIT_LIST_HEAD(&msgs);
-
        cfs_cpt_for_each(i, lnet_cpt_table()) {
                lnet_net_lock(i);
                list_splice_init(the_lnet.ln_mt_resendqs[i], &msgs);
@@ -3369,8 +3347,8 @@ static void
 lnet_recover_peer_nis(void)
 {
        struct lnet_mt_event_info *ev_info;
-       struct list_head processed_list;
-       struct list_head local_queue;
+       LIST_HEAD(processed_list);
+       LIST_HEAD(local_queue);
        struct lnet_handle_md mdh;
        struct lnet_peer_ni *lpni;
        struct lnet_peer_ni *tmp;
@@ -3378,9 +3356,6 @@ lnet_recover_peer_nis(void)
        int healthv;
        int rc;
 
-       INIT_LIST_HEAD(&local_queue);
-       INIT_LIST_HEAD(&processed_list);
-
        /*
         * Always use cpt 0 for locking across all interactions with
         * ln_mt_peerNIRecovq
@@ -4009,8 +3984,8 @@ lnet_parse_reply(struct lnet_ni *ni, struct lnet_msg *msg)
        struct lnet_hdr *hdr = &msg->msg_hdr;
        struct lnet_process_id src = {0};
        struct lnet_libmd *md;
-       int rlength;
-       int mlength;
+       unsigned int rlength;
+       unsigned int mlength;
        int cpt;
 
        cpt = lnet_cpt_of_cookie(hdr->msg.reply.dst_wmd.wh_object_cookie);
@@ -4039,7 +4014,7 @@ lnet_parse_reply(struct lnet_ni *ni, struct lnet_msg *msg)
        LASSERT(md->md_offset == 0);
 
        rlength = hdr->payload_length;
-       mlength = MIN(rlength, (int)md->md_length);
+       mlength = min(rlength, md->md_length);
 
        if (mlength < rlength &&
            (md->md_options & LNET_MD_TRUNCATE) == 0) {
@@ -4399,8 +4374,8 @@ lnet_parse(struct lnet_ni *ni, struct lnet_hdr *hdr, lnet_nid_t from_nid,
 
        if (!list_empty(&the_lnet.ln_drop_rules) &&
            lnet_drop_rule_match(hdr, ni->ni_nid, NULL)) {
-               CDEBUG(D_NET, "%s, src %s, dst %s: Dropping %s to simulate"
-                             "silent message loss\n",
+               CDEBUG(D_NET,
+                      "%s, src %s, dst %s: Dropping %s to simulate silent message loss\n",
                       libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
                       libcfs_nid2str(dest_nid), lnet_msgtyp2str(type));
                goto drop;