1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2005 Cluster File Systems, Inc. All rights reserved.
5 * Author: PJ Kirner <pjkirner@clusterfs.com>
7 * This file is part of the Lustre file system, http://www.lustre.org
8 * Lustre is a trademark of Cluster File Systems, Inc.
10 * This file is confidential source code owned by Cluster File Systems.
11 * No viewing, modification, compilation, redistribution, or any other
12 * form of use is permitted except through a signed license agreement.
14 * If you have not signed such an agreement, then you have no rights to
15 * this file. Please destroy it immediately and contact CFS.
21 #ifndef _USING_LUSTRE_PORTALS_
23 kptllnd_extract_iov (int dst_niov, ptl_md_iovec_t *dst,
24 int src_niov, struct iovec *src,
25 unsigned int offset, unsigned int len)
27 /* Initialise 'dst' to the subset of 'src' starting at 'offset',
28 * for exactly 'len' bytes, and return the number of entries.
29 * NB not destructive to 'src' */
30 unsigned int frag_len;
33 if (len == 0) /* no data => */
34 return (0); /* no frags */
36 LASSERT (src_niov > 0);
37 while (offset >= src->iov_len) { /* skip initial frags */
38 offset -= src->iov_len;
41 LASSERT (src_niov > 0);
46 LASSERT (src_niov > 0);
47 LASSERT (niov <= dst_niov);
49 frag_len = src->iov_len - offset;
50 dst->iov_base = ((char *)src->iov_base) + offset;
52 if (len <= frag_len) {
57 dst->iov_len = frag_len;
69 kptllnd_extract_phys (int dst_niov, ptl_md_iovec_t *dst,
70 int src_niov, lnet_kiov_t *src,
71 unsigned int offset, unsigned int len)
73 /* Initialise 'dst' to the physical addresses of the subset of 'src'
74 * starting at 'offset', for exactly 'len' bytes, and return the number
75 * of entries. NB not destructive to 'src' */
76 unsigned int frag_len;
81 if (len == 0) /* no data => */
82 return (0); /* no frags */
84 LASSERT (src_niov > 0);
85 while (offset >= src->kiov_len) { /* skip initial frags */
86 offset -= src->kiov_len;
89 LASSERT (src_niov > 0);
94 LASSERT (src_niov > 0);
95 LASSERT (niov <= dst_niov);
97 frag_len = min(src->kiov_len - offset, len);
98 phys_page = lnet_page2phys(src->kiov_page);
99 phys = phys_page + src->kiov_offset + offset;
101 LASSERT (sizeof(void *) > 4 ||
102 (phys <= 0xffffffffULL &&
103 phys + (frag_len - 1) <= 0xffffffffULL));
105 dst->iov_base = (void *)((unsigned long)phys);
106 dst->iov_len = frag_len;
122 kptllnd_init_rdma_md(kptl_tx_t *tx, unsigned int niov,
123 struct iovec *iov, lnet_kiov_t *kiov,
124 unsigned int offset, unsigned int nob)
126 LASSERT (iov == NULL || kiov == NULL);
128 memset(&tx->tx_rdma_md, 0, sizeof(tx->tx_rdma_md));
130 tx->tx_rdma_md.start = tx->tx_rdma_frags;
131 tx->tx_rdma_md.user_ptr = &tx->tx_rdma_eventarg;
132 tx->tx_rdma_md.eq_handle = kptllnd_data.kptl_eqh;
133 tx->tx_rdma_md.options = PTL_MD_LUSTRE_COMPLETION_SEMANTICS |
134 PTL_MD_EVENT_START_DISABLE;
135 switch (tx->tx_type) {
139 case TX_TYPE_PUT_REQUEST: /* passive: peer gets */
140 tx->tx_rdma_md.threshold = 1; /* GET event */
141 tx->tx_rdma_md.options |= PTL_MD_OP_GET;
144 case TX_TYPE_GET_REQUEST: /* passive: peer puts */
145 tx->tx_rdma_md.threshold = 1; /* PUT event */
146 tx->tx_rdma_md.options |= PTL_MD_OP_PUT;
149 case TX_TYPE_PUT_RESPONSE: /* active: I get */
150 tx->tx_rdma_md.threshold = 2; /* SEND + REPLY */
153 case TX_TYPE_GET_RESPONSE: /* active: I put */
154 tx->tx_rdma_md.threshold = 1; /* SEND */
159 tx->tx_rdma_md.length = 0;
163 #ifdef _USING_LUSTRE_PORTALS_
165 tx->tx_rdma_md.options |= PTL_MD_IOVEC;
166 tx->tx_rdma_md.length =
167 lnet_extract_iov(PTL_MD_MAX_IOV, tx->tx_rdma_frags->iov,
168 niov, iov, offset, nob);
172 /* Cheating OK since ptl_kiov_t == lnet_kiov_t */
173 CLASSERT(sizeof(ptl_kiov_t) == sizeof(lnet_kiov_t));
174 CLASSERT(offsetof(ptl_kiov_t, kiov_offset) ==
175 offsetof(lnet_kiov_t, kiov_offset));
176 CLASSERT(offsetof(ptl_kiov_t, kiov_page) ==
177 offsetof(lnet_kiov_t, kiov_page));
178 CLASSERT(offsetof(ptl_kiov_t, kiov_len) ==
179 offsetof(lnet_kiov_t, kiov_len));
181 tx->tx_rdma_md.options |= PTL_MD_KIOV;
182 tx->tx_rdma_md.length =
183 lnet_extract_kiov(PTL_MD_MAX_IOV, tx->tx_rdma_frags->kiov,
184 niov, kiov, offset, nob);
187 tx->tx_rdma_md.options |= PTL_MD_IOVEC;
188 tx->tx_rdma_md.length =
189 kptllnd_extract_iov(PTL_MD_MAX_IOV, tx->tx_rdma_frags->iov,
190 niov, iov, offset, nob);
194 tx->tx_rdma_md.options |= PTL_MD_IOVEC | PTL_MD_PHYS;
195 tx->tx_rdma_md.length =
196 kptllnd_extract_phys(PTL_MD_MAX_IOV, tx->tx_rdma_frags->iov,
197 niov, kiov, offset, nob);
202 kptllnd_active_rdma(kptl_rx_t *rx, lnet_msg_t *lntmsg, int type,
203 unsigned int niov, struct iovec *iov, lnet_kiov_t *kiov,
204 unsigned int offset, int nob)
208 kptl_msg_t *rxmsg = rx->rx_msg;
209 kptl_peer_t *peer = rx->rx_peer;
213 LASSERT (type == TX_TYPE_PUT_RESPONSE ||
214 type == TX_TYPE_GET_RESPONSE);
216 tx = kptllnd_get_idle_tx(type);
218 CERROR ("Can't do %s rdma to %s: can't allocate descriptor\n",
219 type == TX_TYPE_PUT_RESPONSE ? "GET" : "PUT",
220 libcfs_id2str(peer->peer_id));
224 kptllnd_set_tx_peer(tx, peer);
225 kptllnd_init_rdma_md(tx, niov, iov, kiov, offset, nob);
227 ptlrc = PtlMDBind(kptllnd_data.kptl_nih, tx->tx_rdma_md,
229 if (ptlrc != PTL_OK) {
230 CERROR("PtlMDBind(%s) failed: %d\n",
231 libcfs_id2str(peer->peer_id), ptlrc);
232 tx->tx_status = -EIO;
233 kptllnd_tx_decref(tx);
237 spin_lock_irqsave(&peer->peer_lock, flags);
239 tx->tx_lnet_msg = lntmsg;
240 /* lnet_finalize() will be called when tx is torn down, so I must
241 * return success from here on... */
243 tx->tx_deadline = jiffies + (*kptllnd_tunables.kptl_timeout * HZ);
244 tx->tx_rdma_mdh = mdh;
246 list_add_tail(&tx->tx_list, &peer->peer_activeq);
248 /* peer has now got my ref on 'tx' */
250 spin_unlock_irqrestore(&peer->peer_lock, flags);
252 if (type == TX_TYPE_GET_RESPONSE)
256 *kptllnd_tunables.kptl_portal,
258 rxmsg->ptlm_u.rdma.kptlrm_matchbits,
260 (lntmsg != NULL) ? /* header data */
266 *kptllnd_tunables.kptl_portal,
268 rxmsg->ptlm_u.rdma.kptlrm_matchbits,
271 if (ptlrc != PTL_OK) {
272 CERROR("Ptl%s failed: %d\n",
273 (type == TX_TYPE_GET_RESPONSE) ? "Put" : "Get", ptlrc);
275 kptllnd_peer_close(peer, -EIO);
276 /* Everything (including this RDMA) queued on the peer will
277 * be completed with failure */
284 kptllnd_send(lnet_ni_t *ni, void *private, lnet_msg_t *lntmsg)
286 lnet_hdr_t *hdr = &lntmsg->msg_hdr;
287 int type = lntmsg->msg_type;
288 lnet_process_id_t target = lntmsg->msg_target;
289 int target_is_router = lntmsg->msg_target_is_router;
290 int routing = lntmsg->msg_routing;
291 unsigned int payload_niov = lntmsg->msg_niov;
292 struct iovec *payload_iov = lntmsg->msg_iov;
293 lnet_kiov_t *payload_kiov = lntmsg->msg_kiov;
294 unsigned int payload_offset = lntmsg->msg_offset;
295 unsigned int payload_nob = lntmsg->msg_len;
299 LASSERT (payload_nob == 0 || payload_niov > 0);
300 LASSERT (payload_niov <= LNET_MAX_IOV);
301 LASSERT (payload_niov <= PTL_MD_MAX_IOV); /* !!! */
302 LASSERT (!(payload_kiov != NULL && payload_iov != NULL));
303 LASSERT (!in_interrupt());
312 /* Is the payload small enough not to need RDMA? */
313 nob = offsetof(kptl_msg_t, ptlm_u.immediate.kptlim_payload[payload_nob]);
314 if (nob <= *kptllnd_tunables.kptl_max_msg_size)
317 tx = kptllnd_get_idle_tx(TX_TYPE_PUT_REQUEST);
319 CERROR("Can't send %s to %s: can't allocate descriptor\n",
320 lnet_msgtyp2str(type),
321 libcfs_id2str(target));
325 kptllnd_init_rdma_md(tx, payload_niov,
326 payload_iov, payload_kiov,
327 payload_offset, payload_nob);
329 tx->tx_lnet_msg = lntmsg;
330 tx->tx_msg->ptlm_u.rdma.kptlrm_hdr = *hdr;
331 kptllnd_init_msg (tx->tx_msg, PTLLND_MSG_TYPE_PUT,
332 sizeof(kptl_rdma_msg_t));
334 CDEBUG(D_NETTRACE, "%s: passive PUT p %d %p\n",
335 libcfs_id2str(target),
336 le32_to_cpu(lntmsg->msg_hdr.msg.put.ptl_index), tx);
338 kptllnd_tx_launch(tx, target);
342 /* routed gets don't RDMA */
343 if (target_is_router || routing)
346 /* Is the payload small enough not to need RDMA? */
347 nob = lntmsg->msg_md->md_length;
348 nob = offsetof(kptl_msg_t,
349 ptlm_u.immediate.kptlim_payload[nob]);
350 if (nob <= *kptllnd_tunables.kptl_max_msg_size)
353 tx = kptllnd_get_idle_tx(TX_TYPE_GET_REQUEST);
355 CERROR("Can't send GET to %s: can't allocate descriptor\n",
356 libcfs_id2str(target));
360 tx->tx_lnet_replymsg =
361 lnet_create_reply_msg(kptllnd_data.kptl_ni, lntmsg);
362 if (tx->tx_lnet_replymsg == NULL) {
363 CERROR("Failed to allocate LNET reply for %s\n",
364 libcfs_id2str(target));
365 kptllnd_tx_decref(tx);
369 if ((lntmsg->msg_md->md_options & LNET_MD_KIOV) == 0)
370 kptllnd_init_rdma_md(tx, lntmsg->msg_md->md_niov,
371 lntmsg->msg_md->md_iov.iov, NULL,
372 0, lntmsg->msg_md->md_length);
374 kptllnd_init_rdma_md(tx, lntmsg->msg_md->md_niov,
375 NULL, lntmsg->msg_md->md_iov.kiov,
376 0, lntmsg->msg_md->md_length);
378 tx->tx_lnet_msg = lntmsg;
379 tx->tx_msg->ptlm_u.rdma.kptlrm_hdr = *hdr;
380 kptllnd_init_msg (tx->tx_msg, PTLLND_MSG_TYPE_GET,
381 sizeof(kptl_rdma_msg_t));
383 CDEBUG(D_NETTRACE, "%s: passive GET p %d %p\n",
384 libcfs_id2str(target),
385 le32_to_cpu(lntmsg->msg_hdr.msg.put.ptl_index), tx);
387 kptllnd_tx_launch(tx, target);
391 CDEBUG(D_NET, "LNET_MSG_ACK\n");
392 LASSERT (payload_nob == 0);
396 tx = kptllnd_get_idle_tx(TX_TYPE_SMALL_MESSAGE);
398 CERROR("Can't send %s to %s: can't allocate descriptor\n",
399 lnet_msgtyp2str(type), libcfs_id2str(target));
403 tx->tx_lnet_msg = lntmsg;
404 tx->tx_msg->ptlm_u.immediate.kptlim_hdr = *hdr;
406 if (payload_kiov != NULL)
407 lnet_copy_kiov2flat(*kptllnd_tunables.kptl_max_msg_size,
408 tx->tx_msg->ptlm_u.immediate.kptlim_payload,
410 payload_niov, payload_kiov,
411 payload_offset, payload_nob);
413 lnet_copy_iov2flat(*kptllnd_tunables.kptl_max_msg_size,
414 tx->tx_msg->ptlm_u.immediate.kptlim_payload,
416 payload_niov, payload_iov,
417 payload_offset, payload_nob);
419 nob = offsetof(kptl_immediate_msg_t, kptlim_payload[payload_nob]);
420 kptllnd_init_msg(tx->tx_msg, PTLLND_MSG_TYPE_IMMEDIATE, nob);
422 CDEBUG(D_NETTRACE, "%s: immediate %s p %d %p\n",
423 libcfs_id2str(target),
424 lnet_msgtyp2str(lntmsg->msg_type),
425 (le32_to_cpu(lntmsg->msg_type) == LNET_MSG_PUT) ?
426 le32_to_cpu(lntmsg->msg_hdr.msg.put.ptl_index) :
427 (le32_to_cpu(lntmsg->msg_type) == LNET_MSG_GET) ?
428 le32_to_cpu(lntmsg->msg_hdr.msg.get.ptl_index) : -1,
431 kptllnd_tx_launch(tx, target);
436 kptllnd_eager_recv(struct lnet_ni *ni, void *private,
437 lnet_msg_t *msg, void **new_privatep)
439 kptl_rx_t *rx = private;
441 CDEBUG(D_NET, "Eager RX=%p RXB=%p\n", rx, rx->rx_rxb);
443 /* I have to release my ref on rxb (if I have one) to ensure I'm an
444 * eager receiver, so I copy the incoming request from the buffer it
445 * landed in, into space reserved in the descriptor... */
447 #if (PTL_MD_LOCAL_ALIGN8 == 0)
448 if (rx->rx_rxb == NULL) /* already copied */
449 return 0; /* to fix alignment */
451 LASSERT(rx->rx_rxb != NULL);
453 LASSERT(rx->rx_nob <= *kptllnd_tunables.kptl_max_msg_size);
455 memcpy(rx->rx_space, rx->rx_msg, rx->rx_nob);
456 rx->rx_msg = (kptl_msg_t *)rx->rx_space;
458 kptllnd_rx_buffer_decref(rx->rx_rxb);
466 kptllnd_recv (lnet_ni_t *ni, void *private, lnet_msg_t *lntmsg, int delayed,
467 unsigned int niov, struct iovec *iov, lnet_kiov_t *kiov,
468 unsigned int offset, unsigned int mlen, unsigned int rlen)
470 kptl_rx_t *rx = private;
471 kptl_msg_t *rxmsg = rx->rx_msg;
475 CDEBUG(D_NET, "%s niov=%d offset=%d mlen=%d rlen=%d\n",
476 kptllnd_msgtype2str(rxmsg->ptlm_type),
477 niov, offset, mlen, rlen);
479 LASSERT (mlen <= rlen);
481 LASSERT (!in_interrupt());
482 LASSERT (!(kiov != NULL && iov != NULL)); /* never both */
483 LASSERT (niov <= PTL_MD_MAX_IOV); /* !!! */
486 if (lntmsg != NULL &&
488 /* Set the UID if the sender's uid isn't 0; i.e. non-root
489 * running in userspace (e.g. a catamount node; linux kernel
490 * senders, including routers have uid 0). If this is a lustre
491 * RPC request, this tells lustre not to trust the creds in the
492 * RPC message body. */
493 lnet_set_msg_uid(ni, lntmsg, rx->rx_uid);
496 switch(rxmsg->ptlm_type)
503 case PTLLND_MSG_TYPE_IMMEDIATE:
504 CDEBUG(D_NET, "PTLLND_MSG_TYPE_IMMEDIATE %d,%d\n", mlen, rlen);
506 nob = offsetof(kptl_msg_t, ptlm_u.immediate.kptlim_payload[rlen]);
507 if (nob > rx->rx_nob) {
508 CERROR ("Immediate message from %s too big: %d(%d)\n",
509 libcfs_id2str(rx->rx_peer->peer_id), nob,
518 *kptllnd_tunables.kptl_max_msg_size,
519 rxmsg->ptlm_u.immediate.kptlim_payload,
525 *kptllnd_tunables.kptl_max_msg_size,
526 rxmsg->ptlm_u.immediate.kptlim_payload,
530 lnet_finalize (ni, lntmsg, 0);
534 case PTLLND_MSG_TYPE_GET:
535 CDEBUG(D_NET, "PTLLND_MSG_TYPE_GET %d,%d\n", mlen, rlen);
537 /* NB always send RDMA so the peer can complete. I send
538 * success/failure in the portals 'hdr_data' */
541 rc = kptllnd_active_rdma(rx, NULL,
542 TX_TYPE_GET_RESPONSE,
543 0, NULL, NULL, 0, 0);
545 rc = kptllnd_active_rdma(rx, lntmsg,
546 TX_TYPE_GET_RESPONSE,
554 case PTLLND_MSG_TYPE_PUT:
555 CDEBUG(D_NET, "PTLLND_MSG_TYPE_PUT %d,%d\n", mlen, rlen);
557 /* NB always send RDMA so the peer can complete; it'll be 0
558 * bytes if there was no match (lntmsg == NULL). I have no way
559 * to let my peer know this, but she's only interested in when
560 * the net has stopped accessing her buffer in any case. */
562 rc = kptllnd_active_rdma(rx, lntmsg, TX_TYPE_PUT_RESPONSE,
563 niov, iov, kiov, offset, mlen);
568 * We're done with the RX
575 kptllnd_eq_callback(ptl_event_t *ev)
577 kptl_eventarg_t *eva = ev->md.user_ptr;
579 switch (eva->eva_type) {
583 case PTLLND_EVENTARG_TYPE_MSG:
584 case PTLLND_EVENTARG_TYPE_RDMA:
585 kptllnd_tx_callback(ev);
588 case PTLLND_EVENTARG_TYPE_BUF:
589 kptllnd_rx_buffer_callback(ev);
595 kptllnd_thread_fini (void)
597 atomic_dec(&kptllnd_data.kptl_nthreads);
601 kptllnd_thread_start (int (*fn)(void *arg), void *arg)
605 atomic_inc(&kptllnd_data.kptl_nthreads);
607 pid = kernel_thread (fn, arg, 0);
611 CERROR("Failed to start kernel_thread: error %d\n", (int)pid);
612 kptllnd_thread_fini();
617 kptllnd_watchdog(void *arg)
621 wait_queue_t waitlink;
623 unsigned long deadline = jiffies;
627 snprintf(name, sizeof(name), "kptllnd_wd_%02d", id);
631 init_waitqueue_entry(&waitlink, current);
633 /* threads shut down in phase 2 after all peers have been destroyed */
634 while (kptllnd_data.kptl_shutdown < 2) {
636 timeout = (int)(deadline - jiffies);
641 int chunk = kptllnd_data.kptl_peer_hash_size;
644 /* Time to check for RDMA timeouts on a few more
645 * peers: I do checks every 'p' seconds on a
646 * proportion of the peer table and I need to check
647 * every connection 'n' times within a timeout
648 * interval, to ensure I detect a timeout on any
649 * connection within (n+1)/n times the timeout
652 if ((*kptllnd_tunables.kptl_timeout) > n * p)
653 chunk = (chunk * n * p) /
654 (*kptllnd_tunables.kptl_timeout);
658 for (i = 0; i < chunk; i++) {
659 kptllnd_peer_check_bucket(peer_index);
660 peer_index = (peer_index + 1) %
661 kptllnd_data.kptl_peer_hash_size;
668 kptllnd_handle_closing_peers();
670 set_current_state(TASK_INTERRUPTIBLE);
671 add_wait_queue_exclusive(&kptllnd_data.kptl_watchdog_waitq,
674 schedule_timeout(timeout);
676 set_current_state (TASK_RUNNING);
677 remove_wait_queue(&kptllnd_data.kptl_watchdog_waitq, &waitlink);
680 kptllnd_thread_fini();
681 CDEBUG(D_NET, "<<<\n");
686 kptllnd_scheduler (void *arg)
690 wait_queue_t waitlink;
695 kptl_rx_buffer_t *rxb;
698 snprintf(name, sizeof(name), "kptllnd_sd_%02d", id);
702 init_waitqueue_entry(&waitlink, current);
704 spin_lock_irqsave(&kptllnd_data.kptl_sched_lock, flags);
706 /* threads shut down in phase 2 after all peers have been destroyed */
707 while (kptllnd_data.kptl_shutdown < 2) {
711 if (!list_empty(&kptllnd_data.kptl_sched_rxq)) {
712 rx = list_entry (kptllnd_data.kptl_sched_rxq.next,
714 list_del(&rx->rx_list);
716 spin_unlock_irqrestore(&kptllnd_data.kptl_sched_lock,
719 kptllnd_rx_parse(rx);
722 spin_lock_irqsave(&kptllnd_data.kptl_sched_lock, flags);
725 if (!list_empty(&kptllnd_data.kptl_sched_rxbq)) {
726 rxb = list_entry (kptllnd_data.kptl_sched_rxbq.next,
727 kptl_rx_buffer_t, rxb_repost_list);
728 list_del(&rxb->rxb_repost_list);
730 spin_unlock_irqrestore(&kptllnd_data.kptl_sched_lock,
733 kptllnd_rx_buffer_post(rxb);
736 spin_lock_irqsave(&kptllnd_data.kptl_sched_lock, flags);
739 if (!list_empty(&kptllnd_data.kptl_sched_txq)) {
740 tx = list_entry (kptllnd_data.kptl_sched_txq.next,
742 list_del_init(&tx->tx_list);
744 spin_unlock_irqrestore(&kptllnd_data.kptl_sched_lock, flags);
749 spin_lock_irqsave(&kptllnd_data.kptl_sched_lock, flags);
753 if (++counter != *kptllnd_tunables.kptl_reschedule_loops)
757 set_current_state(TASK_INTERRUPTIBLE);
758 add_wait_queue_exclusive(&kptllnd_data.kptl_sched_waitq,
760 spin_unlock_irqrestore(&kptllnd_data.kptl_sched_lock, flags);
767 set_current_state(TASK_RUNNING);
768 remove_wait_queue(&kptllnd_data.kptl_sched_waitq, &waitlink);
770 spin_lock_irqsave(&kptllnd_data.kptl_sched_lock, flags);
775 spin_unlock_irqrestore(&kptllnd_data.kptl_sched_lock, flags);
777 kptllnd_thread_fini();