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));
333 kptllnd_tx_launch(tx, target);
337 /* routed gets don't RDMA */
338 if (target_is_router || routing)
341 /* Is the payload small enough not to need RDMA? */
342 nob = lntmsg->msg_md->md_length;
343 nob = offsetof(kptl_msg_t,
344 ptlm_u.immediate.kptlim_payload[nob]);
345 if (nob <= *kptllnd_tunables.kptl_max_msg_size)
348 tx = kptllnd_get_idle_tx(TX_TYPE_GET_REQUEST);
350 CERROR("Can't send GET to %s: can't allocate descriptor\n",
351 libcfs_id2str(target));
355 tx->tx_lnet_replymsg =
356 lnet_create_reply_msg(kptllnd_data.kptl_ni, lntmsg);
357 if (tx->tx_lnet_replymsg == NULL) {
358 CERROR("Failed to allocate LNET reply for %s\n",
359 libcfs_id2str(target));
360 kptllnd_tx_decref(tx);
364 if ((lntmsg->msg_md->md_options & LNET_MD_KIOV) == 0)
365 kptllnd_init_rdma_md(tx, lntmsg->msg_md->md_niov,
366 lntmsg->msg_md->md_iov.iov, NULL,
367 0, lntmsg->msg_md->md_length);
369 kptllnd_init_rdma_md(tx, lntmsg->msg_md->md_niov,
370 NULL, lntmsg->msg_md->md_iov.kiov,
371 0, lntmsg->msg_md->md_length);
373 tx->tx_lnet_msg = lntmsg;
374 tx->tx_msg->ptlm_u.rdma.kptlrm_hdr = *hdr;
375 kptllnd_init_msg (tx->tx_msg, PTLLND_MSG_TYPE_GET,
376 sizeof(kptl_rdma_msg_t));
377 kptllnd_tx_launch(tx, target);
381 CDEBUG(D_NET, "LNET_MSG_ACK\n");
382 LASSERT (payload_nob == 0);
386 tx = kptllnd_get_idle_tx(TX_TYPE_SMALL_MESSAGE);
388 CERROR("Can't send %s to %s: can't allocate descriptor\n",
389 lnet_msgtyp2str(type), libcfs_id2str(target));
393 tx->tx_lnet_msg = lntmsg;
394 tx->tx_msg->ptlm_u.immediate.kptlim_hdr = *hdr;
396 if (payload_kiov != NULL)
397 lnet_copy_kiov2flat(*kptllnd_tunables.kptl_max_msg_size,
398 tx->tx_msg->ptlm_u.immediate.kptlim_payload,
400 payload_niov, payload_kiov,
401 payload_offset, payload_nob);
403 lnet_copy_iov2flat(*kptllnd_tunables.kptl_max_msg_size,
404 tx->tx_msg->ptlm_u.immediate.kptlim_payload,
406 payload_niov, payload_iov,
407 payload_offset, payload_nob);
409 nob = offsetof(kptl_immediate_msg_t, kptlim_payload[payload_nob]);
410 kptllnd_init_msg(tx->tx_msg, PTLLND_MSG_TYPE_IMMEDIATE, nob);
411 kptllnd_tx_launch(tx, target);
416 kptllnd_eager_recv(struct lnet_ni *ni, void *private,
417 lnet_msg_t *msg, void **new_privatep)
419 kptl_rx_t *rx = private;
421 CDEBUG(D_NET, "Eager RX=%p RXB=%p\n", rx, rx->rx_rxb);
423 /* I have to release my ref on rxb (if I have one) to ensure I'm an
424 * eager receiver, so I copy the incoming request from the buffer it
425 * landed in, into space reserved in the descriptor... */
427 #if (PTL_MD_LOCAL_ALIGN8 == 0)
428 if (rx->rx_rxb == NULL) /* already copied */
429 return 0; /* to fix alignment */
431 LASSERT(rx->rx_rxb != NULL);
433 LASSERT(rx->rx_nob <= *kptllnd_tunables.kptl_max_msg_size);
435 memcpy(rx->rx_space, rx->rx_msg, rx->rx_nob);
436 rx->rx_msg = (kptl_msg_t *)rx->rx_space;
438 kptllnd_rx_buffer_decref(rx->rx_rxb);
446 kptllnd_recv (lnet_ni_t *ni, void *private, lnet_msg_t *lntmsg, int delayed,
447 unsigned int niov, struct iovec *iov, lnet_kiov_t *kiov,
448 unsigned int offset, unsigned int mlen, unsigned int rlen)
450 kptl_rx_t *rx = private;
451 kptl_msg_t *rxmsg = rx->rx_msg;
455 CDEBUG(D_NET, "%s niov=%d offset=%d mlen=%d rlen=%d\n",
456 kptllnd_msgtype2str(rxmsg->ptlm_type),
457 niov, offset, mlen, rlen);
459 LASSERT (mlen <= rlen);
461 LASSERT (!in_interrupt());
462 LASSERT (!(kiov != NULL && iov != NULL)); /* never both */
463 LASSERT (niov <= PTL_MD_MAX_IOV); /* !!! */
466 if (lntmsg != NULL &&
468 /* Set the UID if the sender's uid isn't 0; i.e. non-root
469 * running in userspace (e.g. a catamount node; linux kernel
470 * senders, including routers have uid 0). If this is a lustre
471 * RPC request, this tells lustre not to trust the creds in the
472 * RPC message body. */
473 lnet_set_msg_uid(ni, lntmsg, rx->rx_uid);
476 switch(rxmsg->ptlm_type)
483 case PTLLND_MSG_TYPE_IMMEDIATE:
484 CDEBUG(D_NET, "PTLLND_MSG_TYPE_IMMEDIATE %d,%d\n", mlen, rlen);
486 nob = offsetof(kptl_msg_t, ptlm_u.immediate.kptlim_payload[rlen]);
487 if (nob > rx->rx_nob) {
488 CERROR ("Immediate message from %s too big: %d(%d)\n",
489 libcfs_id2str(rx->rx_peer->peer_id), nob,
498 *kptllnd_tunables.kptl_max_msg_size,
499 rxmsg->ptlm_u.immediate.kptlim_payload,
505 *kptllnd_tunables.kptl_max_msg_size,
506 rxmsg->ptlm_u.immediate.kptlim_payload,
510 lnet_finalize (ni, lntmsg, 0);
514 case PTLLND_MSG_TYPE_GET:
515 CDEBUG(D_NET, "PTLLND_MSG_TYPE_GET %d,%d\n", mlen, rlen);
517 /* NB always send RDMA so the peer can complete. I send
518 * success/failure in the portals 'hdr_data' */
521 rc = kptllnd_active_rdma(rx, NULL,
522 TX_TYPE_GET_RESPONSE,
523 0, NULL, NULL, 0, 0);
525 rc = kptllnd_active_rdma(rx, lntmsg,
526 TX_TYPE_GET_RESPONSE,
534 case PTLLND_MSG_TYPE_PUT:
535 CDEBUG(D_NET, "PTLLND_MSG_TYPE_PUT %d,%d\n", mlen, rlen);
537 /* NB always send RDMA so the peer can complete; it'll be 0
538 * bytes if there was no match (lntmsg == NULL). I have no way
539 * to let my peer know this, but she's only interested in when
540 * the net has stopped accessing her buffer in any case. */
542 rc = kptllnd_active_rdma(rx, lntmsg, TX_TYPE_PUT_RESPONSE,
543 niov, iov, kiov, offset, mlen);
548 * We're done with the RX
555 kptllnd_eq_callback(ptl_event_t *ev)
557 kptl_eventarg_t *eva = ev->md.user_ptr;
559 switch (eva->eva_type) {
563 case PTLLND_EVENTARG_TYPE_MSG:
564 case PTLLND_EVENTARG_TYPE_RDMA:
565 kptllnd_tx_callback(ev);
568 case PTLLND_EVENTARG_TYPE_BUF:
569 kptllnd_rx_buffer_callback(ev);
575 kptllnd_thread_fini (void)
577 atomic_dec(&kptllnd_data.kptl_nthreads);
581 kptllnd_thread_start (int (*fn)(void *arg), void *arg)
585 atomic_inc(&kptllnd_data.kptl_nthreads);
587 pid = kernel_thread (fn, arg, 0);
591 CERROR("Failed to start kernel_thread: error %d\n", (int)pid);
592 kptllnd_thread_fini();
597 kptllnd_watchdog(void *arg)
601 wait_queue_t waitlink;
603 unsigned long deadline = jiffies;
607 snprintf(name, sizeof(name), "kptllnd_wd_%02d", id);
611 init_waitqueue_entry(&waitlink, current);
613 /* threads shut down in phase 2 after all peers have been destroyed */
614 while (kptllnd_data.kptl_shutdown < 2) {
616 timeout = (int)(deadline - jiffies);
621 int chunk = kptllnd_data.kptl_peer_hash_size;
624 /* Time to check for RDMA timeouts on a few more
625 * peers: I do checks every 'p' seconds on a
626 * proportion of the peer table and I need to check
627 * every connection 'n' times within a timeout
628 * interval, to ensure I detect a timeout on any
629 * connection within (n+1)/n times the timeout
632 if ((*kptllnd_tunables.kptl_timeout) > n * p)
633 chunk = (chunk * n * p) /
634 (*kptllnd_tunables.kptl_timeout);
638 for (i = 0; i < chunk; i++) {
639 kptllnd_peer_check_bucket(peer_index);
640 peer_index = (peer_index + 1) %
641 kptllnd_data.kptl_peer_hash_size;
648 kptllnd_handle_closing_peers();
650 set_current_state(TASK_INTERRUPTIBLE);
651 add_wait_queue_exclusive(&kptllnd_data.kptl_watchdog_waitq,
654 schedule_timeout(timeout);
656 set_current_state (TASK_RUNNING);
657 remove_wait_queue(&kptllnd_data.kptl_watchdog_waitq, &waitlink);
660 kptllnd_thread_fini();
661 CDEBUG(D_NET, "<<<\n");
666 kptllnd_scheduler (void *arg)
670 wait_queue_t waitlink;
675 kptl_rx_buffer_t *rxb;
678 snprintf(name, sizeof(name), "kptllnd_sd_%02d", id);
682 init_waitqueue_entry(&waitlink, current);
684 spin_lock_irqsave(&kptllnd_data.kptl_sched_lock, flags);
686 /* threads shut down in phase 2 after all peers have been destroyed */
687 while (kptllnd_data.kptl_shutdown < 2) {
691 if (!list_empty(&kptllnd_data.kptl_sched_rxq)) {
692 rx = list_entry (kptllnd_data.kptl_sched_rxq.next,
694 list_del(&rx->rx_list);
696 spin_unlock_irqrestore(&kptllnd_data.kptl_sched_lock,
699 kptllnd_rx_parse(rx);
702 spin_lock_irqsave(&kptllnd_data.kptl_sched_lock, flags);
705 if (!list_empty(&kptllnd_data.kptl_sched_rxbq)) {
706 rxb = list_entry (kptllnd_data.kptl_sched_rxbq.next,
707 kptl_rx_buffer_t, rxb_repost_list);
708 list_del(&rxb->rxb_repost_list);
710 spin_unlock_irqrestore(&kptllnd_data.kptl_sched_lock,
713 kptllnd_rx_buffer_post(rxb);
716 spin_lock_irqsave(&kptllnd_data.kptl_sched_lock, flags);
719 if (!list_empty(&kptllnd_data.kptl_sched_txq)) {
720 tx = list_entry (kptllnd_data.kptl_sched_txq.next,
722 list_del_init(&tx->tx_list);
724 spin_unlock_irqrestore(&kptllnd_data.kptl_sched_lock, flags);
729 spin_lock_irqsave(&kptllnd_data.kptl_sched_lock, flags);
733 if (++counter != *kptllnd_tunables.kptl_reschedule_loops)
737 set_current_state(TASK_INTERRUPTIBLE);
738 add_wait_queue_exclusive(&kptllnd_data.kptl_sched_waitq,
740 spin_unlock_irqrestore(&kptllnd_data.kptl_sched_lock, flags);
747 set_current_state(TASK_RUNNING);
748 remove_wait_queue(&kptllnd_data.kptl_sched_waitq, &waitlink);
750 spin_lock_irqsave(&kptllnd_data.kptl_sched_lock, flags);
755 spin_unlock_irqrestore(&kptllnd_data.kptl_sched_lock, flags);
757 kptllnd_thread_fini();