Whamcloud - gitweb
LU-4423 lnet: use 64-bit time for selftest
[fs/lustre-release.git] / lnet / selftest / rpc.c
index a2573b2..4079698 100644 (file)
  *
  * You should have received a copy of the GNU General Public License
  * version 2 along with this program; If not, see
- * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
- *
- * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
- * CA 95054 USA or visit www.sun.com if you need additional information or
- * have any questions.
+ * http://www.gnu.org/licenses/gpl-2.0.html
  *
  * GPL HEADER END
  */
@@ -27,7 +23,7 @@
  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
  * Use is subject to license terms.
  *
- * Copyright (c) 2012, Intel Corporation.
+ * Copyright (c) 2012, 2016, Intel Corporation.
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
@@ -54,12 +50,12 @@ typedef enum {
         SRPC_STATE_STOPPING,
 } srpc_state_t;
 
-struct smoketest_rpc {
+static struct smoketest_rpc {
        spinlock_t       rpc_glock;     /* global lock */
        srpc_service_t  *rpc_services[SRPC_SERVICE_MAX_ID + 1];
        lnet_handle_eq_t rpc_lnet_eq;   /* _the_ LNet event queue */
        srpc_state_t     rpc_state;
-       srpc_counters_t  rpc_counters;
+       struct srpc_counters     rpc_counters;
        __u64            rpc_matchbits; /* matchbits counter */
 } srpc_data;
 
@@ -71,41 +67,31 @@ srpc_serv_portal(int svc_id)
 }
 
 /* forward ref's */
-int srpc_handle_rpc (swi_workitem_t *wi);
+int srpc_handle_rpc(swi_workitem_t *wi);
 
-void srpc_get_counters (srpc_counters_t *cnt)
+void srpc_get_counters(struct srpc_counters *cnt)
 {
        spin_lock(&srpc_data.rpc_glock);
        *cnt = srpc_data.rpc_counters;
        spin_unlock(&srpc_data.rpc_glock);
 }
 
-void srpc_set_counters (const srpc_counters_t *cnt)
+void srpc_set_counters(const struct srpc_counters *cnt)
 {
        spin_lock(&srpc_data.rpc_glock);
        srpc_data.rpc_counters = *cnt;
        spin_unlock(&srpc_data.rpc_glock);
 }
 
-int
-srpc_add_bulk_page(srpc_bulk_t *bk, struct page *pg, int i, int nob)
+static int
+srpc_add_bulk_page(srpc_bulk_t *bk, struct page *pg, int i, int off, int nob)
 {
-       nob = min(nob, (int)PAGE_CACHE_SIZE);
-
-       LASSERT(nob > 0);
-       LASSERT(i >= 0 && i < bk->bk_niov);
+       LASSERT(off < PAGE_SIZE);
+       LASSERT(nob > 0 && nob <= PAGE_SIZE);
 
-#ifdef __KERNEL__
-       bk->bk_iovs[i].kiov_offset = 0;
+       bk->bk_iovs[i].kiov_offset = off;
        bk->bk_iovs[i].kiov_page   = pg;
        bk->bk_iovs[i].kiov_len    = nob;
-#else
-       LASSERT(bk->bk_pages != NULL);
-
-       bk->bk_pages[i] = pg;
-       bk->bk_iovs[i].iov_len  = nob;
-       bk->bk_iovs[i].iov_base = page_address(pg);
-#endif
        return nob;
 }
 
@@ -116,30 +102,21 @@ srpc_free_bulk (srpc_bulk_t *bk)
        struct page *pg;
 
         LASSERT (bk != NULL);
-#ifndef __KERNEL__
-        LASSERT (bk->bk_pages != NULL);
-#endif
 
         for (i = 0; i < bk->bk_niov; i++) {
-#ifdef __KERNEL__
                 pg = bk->bk_iovs[i].kiov_page;
-#else
-                pg = bk->bk_pages[i];
-#endif
                 if (pg == NULL) break;
 
                __free_page(pg);
         }
 
-#ifndef __KERNEL__
-       LIBCFS_FREE(bk->bk_pages, sizeof(struct page *) * bk->bk_niov);
-#endif
         LIBCFS_FREE(bk, offsetof(srpc_bulk_t, bk_iovs[bk->bk_niov]));
         return;
 }
 
 srpc_bulk_t *
-srpc_alloc_bulk(int cpt, unsigned bulk_npg, unsigned bulk_len, int sink)
+srpc_alloc_bulk(int cpt, unsigned bulk_off, unsigned bulk_npg,
+               unsigned bulk_len, int sink)
 {
        srpc_bulk_t  *bk;
        int           i;
@@ -157,38 +134,24 @@ srpc_alloc_bulk(int cpt, unsigned bulk_npg, unsigned bulk_len, int sink)
        bk->bk_sink   = sink;
        bk->bk_len    = bulk_len;
        bk->bk_niov   = bulk_npg;
-#ifndef __KERNEL__
-       {
-               struct page  **pages;
-
-               LIBCFS_CPT_ALLOC(pages, lnet_cpt_table(), cpt,
-                                sizeof(struct page *) * bulk_npg);
-               if (pages == NULL) {
-                       LIBCFS_FREE(bk, offsetof(srpc_bulk_t,
-                                   bk_iovs[bulk_npg]));
-                       CERROR("Can't allocate page array for %d pages\n",
-                               bulk_npg);
-                       return NULL;
-               }
-
-               memset(pages, 0, sizeof(struct page *) * bulk_npg);
-               bk->bk_pages = pages;
-       }
-#endif
 
        for (i = 0; i < bulk_npg; i++) {
                struct page *pg;
                int         nob;
 
-               pg = cfs_page_cpt_alloc(lnet_cpt_table(), cpt, GFP_IOFS);
+               pg = cfs_page_cpt_alloc(lnet_cpt_table(), cpt, GFP_KERNEL);
                if (pg == NULL) {
                        CERROR("Can't allocate page %d of %d\n", i, bulk_npg);
                        srpc_free_bulk(bk);
                        return NULL;
                }
 
-               nob = srpc_add_bulk_page(bk, pg, i, bulk_len);
+               nob = min_t(unsigned, bulk_off + bulk_len, PAGE_SIZE) -
+                     bulk_off;
+
+               srpc_add_bulk_page(bk, pg, i, bulk_off, nob);
                bulk_len -= nob;
+               bulk_off = 0;
        }
 
        return bk;
@@ -205,7 +168,7 @@ srpc_next_id (void)
        return id;
 }
 
-void
+static void
 srpc_init_server_rpc(struct srpc_server_rpc *rpc,
                     struct srpc_service_cd *scd,
                     struct srpc_buffer *buffer)
@@ -386,7 +349,7 @@ srpc_remove_service (srpc_service_t *sv)
        return 0;
 }
 
-int
+static int
 srpc_post_passive_rdma(int portal, int local, __u64 matchbits, void *buf,
                       int len, int options, lnet_process_id_t peer,
                       lnet_handle_md_t *mdh, srpc_event_t *ev)
@@ -421,12 +384,12 @@ srpc_post_passive_rdma(int portal, int local, __u64 matchbits, void *buf,
         }
 
         CDEBUG (D_NET,
-                "Posted passive RDMA: peer %s, portal %d, matchbits "LPX64"\n",
+               "Posted passive RDMA: peer %s, portal %d, matchbits %#llx\n",
                 libcfs_id2str(peer), portal, matchbits);
         return 0;
 }
 
-int
+static int
 srpc_post_active_rdma(int portal, __u64 matchbits, void *buf, int len,
                       int options, lnet_process_id_t peer, lnet_nid_t self,
                       lnet_handle_md_t *mdh, srpc_event_t *ev)
@@ -461,7 +424,7 @@ srpc_post_active_rdma(int portal, __u64 matchbits, void *buf, int len,
         }
 
         if (rc != 0) {
-                CERROR ("LNet%s(%s, %d, "LPD64") failed: %d\n",
+               CERROR ("LNet%s(%s, %d, %lld) failed: %d\n",
                         ((options & LNET_MD_OP_PUT) != 0) ? "Put" : "Get",
                         libcfs_id2str(peer), portal, matchbits, rc);
 
@@ -472,22 +435,13 @@ srpc_post_active_rdma(int portal, __u64 matchbits, void *buf, int len,
                 LASSERT (rc == 0);
         } else {
                 CDEBUG (D_NET,
-                        "Posted active RDMA: peer %s, portal %u, matchbits "LPX64"\n",
+                       "Posted active RDMA: peer %s, portal %u, matchbits %#llx\n",
                         libcfs_id2str(peer), portal, matchbits);
         }
         return 0;
 }
 
-int
-srpc_post_active_rqtbuf(lnet_process_id_t peer, int service, void *buf,
-                       int len, lnet_handle_md_t *mdh, srpc_event_t *ev)
-{
-       return srpc_post_active_rdma(srpc_serv_portal(service), service,
-                                    buf, len, LNET_MD_OP_PUT, peer,
-                                    LNET_NID_ANY, mdh, ev);
-}
-
-int
+static int
 srpc_post_passive_rqtbuf(int service, int local, void *buf, int len,
                         lnet_handle_md_t *mdh, srpc_event_t *ev)
 {
@@ -501,8 +455,9 @@ srpc_post_passive_rqtbuf(int service, int local, void *buf, int len,
                                      LNET_MD_OP_PUT, any, mdh, ev);
 }
 
-int
+static int
 srpc_service_post_buffer(struct srpc_service_cd *scd, struct srpc_buffer *buf)
+__must_hold(&scd->scd_lock)
 {
        struct srpc_service     *sv = scd->scd_svc;
        struct srpc_msg         *msg = &buf->buf_msg;
@@ -598,7 +553,7 @@ srpc_add_buffer(struct swi_workitem *wi)
        }
 
        if (rc != 0) {
-               scd->scd_buf_err_stamp = cfs_time_current_sec();
+               scd->scd_buf_err_stamp = ktime_get_real_seconds();
                scd->scd_buf_err = rc;
 
                LASSERT(scd->scd_buf_posting > 0);
@@ -700,8 +655,8 @@ srpc_finish_service(struct srpc_service *sv)
                }
 
                if (scd->scd_buf_nposted > 0) {
-                       CDEBUG(D_NET, "waiting for %d posted buffers to unlink",
-                              scd->scd_buf_nposted);
+                       CDEBUG(D_NET, "waiting for %d posted buffers to "
+                              "unlink\n", scd->scd_buf_nposted);
                        spin_unlock(&scd->scd_lock);
                        return 0;
                }
@@ -732,8 +687,9 @@ srpc_finish_service(struct srpc_service *sv)
 }
 
 /* called with sv->sv_lock held */
-void
+static void
 srpc_service_recycle_buffer(struct srpc_service_cd *scd, srpc_buffer_t *buf)
+__must_hold(&scd->scd_lock)
 {
        if (!scd->scd_svc->sv_shuttingdown && scd->scd_buf_adjust >= 0) {
                if (srpc_service_post_buffer(scd, buf) != 0) {
@@ -822,7 +778,7 @@ srpc_shutdown_service(srpc_service_t *sv)
        }
 }
 
-int
+static int
 srpc_send_request (srpc_client_rpc_t *rpc)
 {
         srpc_event_t *ev = &rpc->crpc_reqstev;
@@ -832,9 +788,11 @@ srpc_send_request (srpc_client_rpc_t *rpc)
         ev->ev_data  = rpc;
         ev->ev_type  = SRPC_REQUEST_SENT;
 
-        rc = srpc_post_active_rqtbuf(rpc->crpc_dest, rpc->crpc_service,
-                                     &rpc->crpc_reqstmsg, sizeof(srpc_msg_t),
-                                     &rpc->crpc_reqstmdh, ev);
+       rc = srpc_post_active_rdma(srpc_serv_portal(rpc->crpc_service),
+                                  rpc->crpc_service, &rpc->crpc_reqstmsg,
+                                  sizeof(srpc_msg_t), LNET_MD_OP_PUT,
+                                  rpc->crpc_dest, LNET_NID_ANY,
+                                  &rpc->crpc_reqstmdh, ev);
         if (rc != 0) {
                 LASSERT (rc == -ENOMEM);
                 ev->ev_fired = 1;  /* no more event expected */
@@ -842,7 +800,7 @@ srpc_send_request (srpc_client_rpc_t *rpc)
         return rc;
 }
 
-int
+static int
 srpc_prepare_reply (srpc_client_rpc_t *rpc)
 {
         srpc_event_t *ev = &rpc->crpc_replyev;
@@ -866,7 +824,7 @@ srpc_prepare_reply (srpc_client_rpc_t *rpc)
         return rc;
 }
 
-int
+static int
 srpc_prepare_bulk (srpc_client_rpc_t *rpc)
 {
         srpc_bulk_t  *bk = &rpc->crpc_bulk;
@@ -880,11 +838,7 @@ srpc_prepare_bulk (srpc_client_rpc_t *rpc)
         if (bk->bk_niov == 0) return 0; /* nothing to do */
 
         opt = bk->bk_sink ? LNET_MD_OP_PUT : LNET_MD_OP_GET;
-#ifdef __KERNEL__
         opt |= LNET_MD_KIOV;
-#else
-        opt |= LNET_MD_IOVEC;
-#endif
 
         ev->ev_fired = 0;
         ev->ev_data  = rpc;
@@ -902,7 +856,7 @@ srpc_prepare_bulk (srpc_client_rpc_t *rpc)
         return rc;
 }
 
-int
+static int
 srpc_do_bulk (srpc_server_rpc_t *rpc)
 {
         srpc_event_t  *ev = &rpc->srpc_ev;
@@ -914,11 +868,7 @@ srpc_do_bulk (srpc_server_rpc_t *rpc)
         LASSERT (bk != NULL);
 
         opt = bk->bk_sink ? LNET_MD_OP_GET : LNET_MD_OP_PUT;
-#ifdef __KERNEL__
         opt |= LNET_MD_KIOV;
-#else
-        opt |= LNET_MD_IOVEC;
-#endif
 
         ev->ev_fired = 0;
         ev->ev_data  = rpc;
@@ -934,7 +884,7 @@ srpc_do_bulk (srpc_server_rpc_t *rpc)
 }
 
 /* only called from srpc_handle_rpc */
-void
+static void
 srpc_server_rpc_done(srpc_server_rpc_t *rpc, int status)
 {
        struct srpc_service_cd  *scd = rpc->srpc_scd;
@@ -1109,7 +1059,7 @@ srpc_handle_rpc(swi_workitem_t *wi)
         return 0;
 }
 
-void
+static void
 srpc_client_rpc_expired (void *data)
 {
         srpc_client_rpc_t *rpc = data;
@@ -1130,7 +1080,7 @@ srpc_client_rpc_expired (void *data)
        spin_unlock(&srpc_data.rpc_glock);
 }
 
-inline void
+static void
 srpc_add_client_rpc_timer(srpc_client_rpc_t *rpc)
 {
        stt_timer_t *timer = &rpc->crpc_timer;
@@ -1141,8 +1091,7 @@ srpc_add_client_rpc_timer(srpc_client_rpc_t *rpc)
        INIT_LIST_HEAD(&timer->stt_list);
        timer->stt_data    = rpc;
        timer->stt_func    = srpc_client_rpc_expired;
-       timer->stt_expires = cfs_time_add(rpc->crpc_timeout,
-                                         cfs_time_current_sec());
+       timer->stt_expires = ktime_get_real_seconds() + rpc->crpc_timeout;
        stt_add_timer(timer);
        return;
 }
@@ -1152,18 +1101,17 @@ srpc_add_client_rpc_timer(srpc_client_rpc_t *rpc)
  *
  * Upon exit the RPC expiry timer is not queued and the handler is not
  * running on any CPU. */
-void
+static void
 srpc_del_client_rpc_timer (srpc_client_rpc_t *rpc)
 {
        /* timer not planted or already exploded */
        if (rpc->crpc_timeout == 0)
                return;
 
-       /* timer sucessfully defused */
+       /* timer successfully defused */
        if (stt_del_timer(&rpc->crpc_timer))
                return;
 
-#ifdef __KERNEL__
        /* timer detonated, wait for it to explode */
        while (rpc->crpc_timeout != 0) {
                spin_unlock(&rpc->crpc_lock);
@@ -1172,12 +1120,9 @@ srpc_del_client_rpc_timer (srpc_client_rpc_t *rpc)
 
                spin_lock(&rpc->crpc_lock);
        }
-#else
-       LBUG(); /* impossible in single-threaded runtime */
-#endif
 }
 
-void
+static void
 srpc_client_rpc_done (srpc_client_rpc_t *rpc, int status)
 {
        swi_workitem_t *wi = &rpc->crpc_wi;
@@ -1441,7 +1386,7 @@ srpc_send_reply(struct srpc_server_rpc *rpc)
 }
 
 /* when in kernel always called with LNET_LOCK() held, and in thread context */
-void
+static void
 srpc_lnet_ev_handler(lnet_event_t *ev)
 {
        struct srpc_service_cd  *scd;
@@ -1456,9 +1401,16 @@ srpc_lnet_ev_handler(lnet_event_t *ev)
        LASSERT (!in_interrupt());
 
        if (ev->status != 0) {
+               __u32 errors;
+
                spin_lock(&srpc_data.rpc_glock);
-               srpc_data.rpc_counters.errors++;
+               if (ev->status != -ECANCELED) /* cancellation is not error */
+                       srpc_data.rpc_counters.errors++;
+               errors = srpc_data.rpc_counters.errors;
                spin_unlock(&srpc_data.rpc_glock);
+
+               CNETERR("LNet event status %d type %d, RPC errors %u\n",
+                       ev->status, ev->type, errors);
        }
 
         rpcev->ev_lnet = ev->type;
@@ -1529,7 +1481,7 @@ srpc_lnet_ev_handler(lnet_event_t *ev)
                }
 
                if (scd->scd_buf_err_stamp != 0 &&
-                   scd->scd_buf_err_stamp < cfs_time_current_sec()) {
+                   scd->scd_buf_err_stamp < ktime_get_real_seconds()) {
                        /* re-enable adding buffer */
                        scd->scd_buf_err_stamp = 0;
                        scd->scd_buf_err = 0;
@@ -1623,32 +1575,6 @@ srpc_lnet_ev_handler(lnet_event_t *ev)
        }
 }
 
-#ifndef __KERNEL__
-
-int
-srpc_check_event (int timeout)
-{
-        lnet_event_t ev;
-        int          rc;
-        int          i;
-
-        rc = LNetEQPoll(&srpc_data.rpc_lnet_eq, 1,
-                        timeout * 1000, &ev, &i);
-        if (rc == 0) return 0;
-
-        LASSERT (rc == -EOVERFLOW || rc == 1);
-
-        /* We can't affort to miss any events... */
-        if (rc == -EOVERFLOW) {
-                CERROR ("Dropped an event!!!\n");
-                abort();
-        }
-
-        srpc_lnet_ev_handler(&ev);
-        return 1;
-}
-
-#endif
 
 int
 srpc_startup (void)
@@ -1658,20 +1584,14 @@ srpc_startup (void)
        memset(&srpc_data, 0, sizeof(struct smoketest_rpc));
        spin_lock_init(&srpc_data.rpc_glock);
 
-        /* 1 second pause to avoid timestamp reuse */
-        cfs_pause(cfs_time_seconds(1));
-        srpc_data.rpc_matchbits = ((__u64) cfs_time_current_sec()) << 48;
+       /* 1 second pause to avoid timestamp reuse */
+       set_current_state(TASK_UNINTERRUPTIBLE);
+       schedule_timeout(cfs_time_seconds(1));
+       srpc_data.rpc_matchbits = ((__u64) ktime_get_real_seconds()) << 48;
 
-        srpc_data.rpc_state = SRPC_STATE_NONE;
+       srpc_data.rpc_state = SRPC_STATE_NONE;
 
-#ifdef __KERNEL__
-        rc = LNetNIInit(LUSTRE_SRV_LNET_PID);
-#else
-        if (the_lnet.ln_server_mode_flag)
-                rc = LNetNIInit(LUSTRE_SRV_LNET_PID);
-        else
-                rc = LNetNIInit(getpid() | LNET_PID_USERFLAG);
-#endif
+       rc = LNetNIInit(LNET_PID_LUSTRE);
         if (rc < 0) {
                 CERROR ("LNetNIInit() has failed: %d\n", rc);
                return rc;
@@ -1680,11 +1600,7 @@ srpc_startup (void)
         srpc_data.rpc_state = SRPC_STATE_NI_INIT;
 
         LNetInvalidateHandle(&srpc_data.rpc_lnet_eq);
-#ifdef __KERNEL__
        rc = LNetEQAlloc(0, srpc_lnet_ev_handler, &srpc_data.rpc_lnet_eq);
-#else
-        rc = LNetEQAlloc(10240, LNET_EQ_HANDLER_NONE, &srpc_data.rpc_lnet_eq);
-#endif
         if (rc != 0) {
                 CERROR("LNetEQAlloc() has failed: %d\n", rc);
                 goto bail;