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_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 = tx->tx_acked ? 2 : 1; /* SEND + ACK? */
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_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_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_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_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 tx->tx_tposted = jiffies;
254 if (type == TX_TYPE_GET_RESPONSE)
256 tx->tx_acked ? PTL_ACK_REQ : PTL_NOACK_REQ,
258 *kptllnd_tunables.kptl_portal,
260 rxmsg->ptlm_u.rdma.kptlrm_matchbits,
262 (lntmsg != NULL) ? /* header data */
268 *kptllnd_tunables.kptl_portal,
270 rxmsg->ptlm_u.rdma.kptlrm_matchbits,
273 if (ptlrc != PTL_OK) {
274 CERROR("Ptl%s failed: %d\n",
275 (type == TX_TYPE_GET_RESPONSE) ? "Put" : "Get", ptlrc);
277 kptllnd_peer_close(peer, -EIO);
278 /* Everything (including this RDMA) queued on the peer will
279 * be completed with failure */
286 kptllnd_send(lnet_ni_t *ni, void *private, lnet_msg_t *lntmsg)
288 lnet_hdr_t *hdr = &lntmsg->msg_hdr;
289 int type = lntmsg->msg_type;
290 lnet_process_id_t target = lntmsg->msg_target;
291 int target_is_router = lntmsg->msg_target_is_router;
292 int routing = lntmsg->msg_routing;
293 unsigned int payload_niov = lntmsg->msg_niov;
294 struct iovec *payload_iov = lntmsg->msg_iov;
295 lnet_kiov_t *payload_kiov = lntmsg->msg_kiov;
296 unsigned int payload_offset = lntmsg->msg_offset;
297 unsigned int payload_nob = lntmsg->msg_len;
304 LASSERT (payload_nob == 0 || payload_niov > 0);
305 LASSERT (payload_niov <= LNET_MAX_IOV);
306 LASSERT (payload_niov <= PTL_MD_MAX_IOV); /* !!! */
307 LASSERT (!(payload_kiov != NULL && payload_iov != NULL));
308 LASSERT (!in_interrupt());
310 rc = kptllnd_find_target(&peer, target);
321 /* Should the payload avoid RDMA? */
322 nob = offsetof(kptl_msg_t, ptlm_u.immediate.kptlim_payload[payload_nob]);
323 if (payload_kiov == NULL &&
324 nob <= peer->peer_max_msg_size)
327 tx = kptllnd_get_idle_tx(TX_TYPE_PUT_REQUEST);
329 CERROR("Can't send %s to %s: can't allocate descriptor\n",
330 lnet_msgtyp2str(type),
331 libcfs_id2str(target));
336 kptllnd_init_rdma_md(tx, payload_niov,
337 payload_iov, payload_kiov,
338 payload_offset, payload_nob);
340 tx->tx_lnet_msg = lntmsg;
341 tx->tx_msg->ptlm_u.rdma.kptlrm_hdr = *hdr;
342 kptllnd_init_msg (tx->tx_msg, PTLLND_MSG_TYPE_PUT,
343 sizeof(kptl_rdma_msg_t));
345 CDEBUG(D_NETTRACE, "%s: passive PUT p %d %p\n",
346 libcfs_id2str(target),
347 le32_to_cpu(lntmsg->msg_hdr.msg.put.ptl_index), tx);
349 kptllnd_tx_launch(peer, tx, 0);
353 /* routed gets don't RDMA */
354 if (target_is_router || routing)
357 /* Is the payload small enough not to need RDMA? */
358 nob = lntmsg->msg_md->md_length;
359 nob = offsetof(kptl_msg_t,
360 ptlm_u.immediate.kptlim_payload[nob]);
361 if (nob <= peer->peer_max_msg_size)
364 tx = kptllnd_get_idle_tx(TX_TYPE_GET_REQUEST);
366 CERROR("Can't send GET to %s: can't allocate descriptor\n",
367 libcfs_id2str(target));
372 tx->tx_lnet_replymsg =
373 lnet_create_reply_msg(kptllnd_data.kptl_ni, lntmsg);
374 if (tx->tx_lnet_replymsg == NULL) {
375 CERROR("Failed to allocate LNET reply for %s\n",
376 libcfs_id2str(target));
377 kptllnd_tx_decref(tx);
382 if ((lntmsg->msg_md->md_options & LNET_MD_KIOV) == 0)
383 kptllnd_init_rdma_md(tx, lntmsg->msg_md->md_niov,
384 lntmsg->msg_md->md_iov.iov, NULL,
385 0, lntmsg->msg_md->md_length);
387 kptllnd_init_rdma_md(tx, lntmsg->msg_md->md_niov,
388 NULL, lntmsg->msg_md->md_iov.kiov,
389 0, lntmsg->msg_md->md_length);
391 tx->tx_lnet_msg = lntmsg;
392 tx->tx_msg->ptlm_u.rdma.kptlrm_hdr = *hdr;
393 kptllnd_init_msg (tx->tx_msg, PTLLND_MSG_TYPE_GET,
394 sizeof(kptl_rdma_msg_t));
396 CDEBUG(D_NETTRACE, "%s: passive GET p %d %p\n",
397 libcfs_id2str(target),
398 le32_to_cpu(lntmsg->msg_hdr.msg.put.ptl_index), tx);
400 kptllnd_tx_launch(peer, tx, 0);
404 CDEBUG(D_NET, "LNET_MSG_ACK\n");
405 LASSERT (payload_nob == 0);
409 /* I don't have to handle kiovs */
410 LASSERT (payload_nob == 0 || payload_iov != NULL);
412 tx = kptllnd_get_idle_tx(TX_TYPE_SMALL_MESSAGE);
414 CERROR("Can't send %s to %s: can't allocate descriptor\n",
415 lnet_msgtyp2str(type), libcfs_id2str(target));
420 tx->tx_lnet_msg = lntmsg;
421 tx->tx_msg->ptlm_u.immediate.kptlim_hdr = *hdr;
423 if (payload_nob == 0) {
426 tx->tx_frags->iov[0].iov_base = tx->tx_msg;
427 tx->tx_frags->iov[0].iov_len = offsetof(kptl_msg_t,
428 ptlm_u.immediate.kptlim_payload);
430 /* NB relying on lustre not asking for PTL_MD_MAX_IOV
432 #ifdef _USING_LUSTRE_PORTALS_
433 nfrag = 1 + lnet_extract_iov(PTL_MD_MAX_IOV - 1,
434 &tx->tx_frags->iov[1],
435 payload_niov, payload_iov,
436 payload_offset, payload_nob);
438 nfrag = 1 + kptllnd_extract_iov(PTL_MD_MAX_IOV - 1,
439 &tx->tx_frags->iov[1],
440 payload_niov, payload_iov,
441 payload_offset, payload_nob);
445 nob = offsetof(kptl_immediate_msg_t, kptlim_payload[payload_nob]);
446 kptllnd_init_msg(tx->tx_msg, PTLLND_MSG_TYPE_IMMEDIATE, nob);
448 CDEBUG(D_NETTRACE, "%s: immediate %s p %d %p\n",
449 libcfs_id2str(target),
450 lnet_msgtyp2str(lntmsg->msg_type),
451 (le32_to_cpu(lntmsg->msg_type) == LNET_MSG_PUT) ?
452 le32_to_cpu(lntmsg->msg_hdr.msg.put.ptl_index) :
453 (le32_to_cpu(lntmsg->msg_type) == LNET_MSG_GET) ?
454 le32_to_cpu(lntmsg->msg_hdr.msg.get.ptl_index) : -1,
457 kptllnd_tx_launch(peer, tx, nfrag);
460 kptllnd_peer_decref(peer);
465 kptllnd_eager_recv(struct lnet_ni *ni, void *private,
466 lnet_msg_t *msg, void **new_privatep)
468 kptl_rx_t *rx = private;
470 CDEBUG(D_NET, "Eager RX=%p RXB=%p\n", rx, rx->rx_rxb);
472 /* I have to release my ref on rxb (if I have one) to ensure I'm an
473 * eager receiver, so I copy the incoming request from the buffer it
474 * landed in, into space reserved in the descriptor... */
476 #if (PTL_MD_LOCAL_ALIGN8 == 0)
477 if (rx->rx_rxb == NULL) /* already copied */
478 return 0; /* to fix alignment */
480 LASSERT(rx->rx_rxb != NULL);
482 LASSERT(rx->rx_nob <= *kptllnd_tunables.kptl_max_msg_size);
484 memcpy(rx->rx_space, rx->rx_msg, rx->rx_nob);
485 rx->rx_msg = (kptl_msg_t *)rx->rx_space;
487 kptllnd_rx_buffer_decref(rx->rx_rxb);
495 kptllnd_recv (lnet_ni_t *ni, void *private, lnet_msg_t *lntmsg, int delayed,
496 unsigned int niov, struct iovec *iov, lnet_kiov_t *kiov,
497 unsigned int offset, unsigned int mlen, unsigned int rlen)
499 kptl_rx_t *rx = private;
500 kptl_msg_t *rxmsg = rx->rx_msg;
504 CDEBUG(D_NET, "%s niov=%d offset=%d mlen=%d rlen=%d\n",
505 kptllnd_msgtype2str(rxmsg->ptlm_type),
506 niov, offset, mlen, rlen);
508 LASSERT (mlen <= rlen);
510 LASSERT (!in_interrupt());
511 LASSERT (!(kiov != NULL && iov != NULL)); /* never both */
512 LASSERT (niov <= PTL_MD_MAX_IOV); /* !!! */
515 if (lntmsg != NULL &&
517 /* Set the UID if the sender's uid isn't 0; i.e. non-root
518 * running in userspace (e.g. a catamount node; linux kernel
519 * senders, including routers have uid 0). If this is a lustre
520 * RPC request, this tells lustre not to trust the creds in the
521 * RPC message body. */
522 lnet_set_msg_uid(ni, lntmsg, rx->rx_uid);
525 switch(rxmsg->ptlm_type)
532 case PTLLND_MSG_TYPE_IMMEDIATE:
533 CDEBUG(D_NET, "PTLLND_MSG_TYPE_IMMEDIATE %d,%d\n", mlen, rlen);
535 nob = offsetof(kptl_msg_t, ptlm_u.immediate.kptlim_payload[rlen]);
536 if (nob > rx->rx_nob) {
537 CERROR ("Immediate message from %s too big: %d(%d)\n",
538 libcfs_id2str(rx->rx_peer->peer_id), nob,
547 *kptllnd_tunables.kptl_max_msg_size,
548 rxmsg->ptlm_u.immediate.kptlim_payload,
554 *kptllnd_tunables.kptl_max_msg_size,
555 rxmsg->ptlm_u.immediate.kptlim_payload,
559 lnet_finalize (ni, lntmsg, 0);
563 case PTLLND_MSG_TYPE_GET:
564 CDEBUG(D_NET, "PTLLND_MSG_TYPE_GET %d,%d\n", mlen, rlen);
566 /* NB always send RDMA so the peer can complete. I send
567 * success/failure in the portals 'hdr_data' */
570 rc = kptllnd_active_rdma(rx, NULL,
571 TX_TYPE_GET_RESPONSE,
572 0, NULL, NULL, 0, 0);
574 rc = kptllnd_active_rdma(rx, lntmsg,
575 TX_TYPE_GET_RESPONSE,
583 case PTLLND_MSG_TYPE_PUT:
584 CDEBUG(D_NET, "PTLLND_MSG_TYPE_PUT %d,%d\n", mlen, rlen);
586 /* NB always send RDMA so the peer can complete; it'll be 0
587 * bytes if there was no match (lntmsg == NULL). I have no way
588 * to let my peer know this, but she's only interested in when
589 * the net has stopped accessing her buffer in any case. */
591 rc = kptllnd_active_rdma(rx, lntmsg, TX_TYPE_PUT_RESPONSE,
592 niov, iov, kiov, offset, mlen);
597 * We're done with the RX
604 kptllnd_eq_callback(ptl_event_t *ev)
606 kptl_eventarg_t *eva = ev->md.user_ptr;
608 switch (eva->eva_type) {
612 case PTLLND_EVENTARG_TYPE_MSG:
613 case PTLLND_EVENTARG_TYPE_RDMA:
614 kptllnd_tx_callback(ev);
617 case PTLLND_EVENTARG_TYPE_BUF:
618 kptllnd_rx_buffer_callback(ev);
624 kptllnd_thread_fini (void)
626 atomic_dec(&kptllnd_data.kptl_nthreads);
630 kptllnd_thread_start (int (*fn)(void *arg), void *arg)
634 atomic_inc(&kptllnd_data.kptl_nthreads);
636 pid = kernel_thread (fn, arg, 0);
640 CERROR("Failed to start kernel_thread: error %d\n", (int)pid);
641 kptllnd_thread_fini();
646 kptllnd_watchdog(void *arg)
650 wait_queue_t waitlink;
652 unsigned long deadline = jiffies;
656 snprintf(name, sizeof(name), "kptllnd_wd_%02d", id);
660 init_waitqueue_entry(&waitlink, current);
662 /* threads shut down in phase 2 after all peers have been destroyed */
663 while (kptllnd_data.kptl_shutdown < 2) {
665 timeout = (int)(deadline - jiffies);
670 int chunk = kptllnd_data.kptl_peer_hash_size;
673 /* Time to check for RDMA timeouts on a few more
674 * peers: I do checks every 'p' seconds on a
675 * proportion of the peer table and I need to check
676 * every connection 'n' times within a timeout
677 * interval, to ensure I detect a timeout on any
678 * connection within (n+1)/n times the timeout
681 if ((*kptllnd_tunables.kptl_timeout) > n * p)
682 chunk = (chunk * n * p) /
683 (*kptllnd_tunables.kptl_timeout);
687 for (i = 0; i < chunk; i++) {
688 kptllnd_peer_check_bucket(peer_index);
689 peer_index = (peer_index + 1) %
690 kptllnd_data.kptl_peer_hash_size;
697 kptllnd_handle_closing_peers();
699 set_current_state(TASK_INTERRUPTIBLE);
700 add_wait_queue_exclusive(&kptllnd_data.kptl_watchdog_waitq,
703 schedule_timeout(timeout);
705 set_current_state (TASK_RUNNING);
706 remove_wait_queue(&kptllnd_data.kptl_watchdog_waitq, &waitlink);
709 kptllnd_thread_fini();
710 CDEBUG(D_NET, "<<<\n");
715 kptllnd_scheduler (void *arg)
719 wait_queue_t waitlink;
724 kptl_rx_buffer_t *rxb;
727 snprintf(name, sizeof(name), "kptllnd_sd_%02d", id);
731 init_waitqueue_entry(&waitlink, current);
733 spin_lock_irqsave(&kptllnd_data.kptl_sched_lock, flags);
735 /* threads shut down in phase 2 after all peers have been destroyed */
736 while (kptllnd_data.kptl_shutdown < 2) {
740 if (!list_empty(&kptllnd_data.kptl_sched_rxq)) {
741 rx = list_entry (kptllnd_data.kptl_sched_rxq.next,
743 list_del(&rx->rx_list);
745 spin_unlock_irqrestore(&kptllnd_data.kptl_sched_lock,
748 kptllnd_rx_parse(rx);
751 spin_lock_irqsave(&kptllnd_data.kptl_sched_lock, flags);
754 if (!list_empty(&kptllnd_data.kptl_sched_rxbq)) {
755 rxb = list_entry (kptllnd_data.kptl_sched_rxbq.next,
756 kptl_rx_buffer_t, rxb_repost_list);
757 list_del(&rxb->rxb_repost_list);
759 spin_unlock_irqrestore(&kptllnd_data.kptl_sched_lock,
762 kptllnd_rx_buffer_post(rxb);
765 spin_lock_irqsave(&kptllnd_data.kptl_sched_lock, flags);
768 if (!list_empty(&kptllnd_data.kptl_sched_txq)) {
769 tx = list_entry (kptllnd_data.kptl_sched_txq.next,
771 list_del_init(&tx->tx_list);
773 spin_unlock_irqrestore(&kptllnd_data.kptl_sched_lock, flags);
778 spin_lock_irqsave(&kptllnd_data.kptl_sched_lock, flags);
782 if (++counter != *kptllnd_tunables.kptl_reschedule_loops)
786 set_current_state(TASK_INTERRUPTIBLE);
787 add_wait_queue_exclusive(&kptllnd_data.kptl_sched_waitq,
789 spin_unlock_irqrestore(&kptllnd_data.kptl_sched_lock, flags);
796 set_current_state(TASK_RUNNING);
797 remove_wait_queue(&kptllnd_data.kptl_sched_waitq, &waitlink);
799 spin_lock_irqsave(&kptllnd_data.kptl_sched_lock, flags);
804 spin_unlock_irqrestore(&kptllnd_data.kptl_sched_lock, flags);
806 kptllnd_thread_fini();