1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * Top level include for library side routines
9 #ifndef __LNET_LIB_LNET_H__
10 #define __LNET_LIB_LNET_H__
12 #if defined(__linux__)
13 #include <lnet/linux/lib-lnet.h>
14 #elif defined(__APPLE__)
15 #include <lnet/darwin/lib-lnet.h>
16 #elif defined(__WINNT__)
17 #include <lnet/winnt/lib-lnet.h>
19 #error Unsupported Operating System
22 #include <libcfs/libcfs.h>
23 #include <lnet/types.h>
24 #include <lnet/lnet.h>
25 #include <lnet/lib-types.h>
27 extern lnet_t the_lnet; /* THE network */
29 static inline int lnet_is_wire_handle_none (lnet_handle_wire_t *wh)
31 return (wh->wh_interface_cookie == LNET_WIRE_HANDLE_NONE.wh_interface_cookie &&
32 wh->wh_object_cookie == LNET_WIRE_HANDLE_NONE.wh_object_cookie);
35 static inline int lnet_md_exhausted (lnet_libmd_t *md)
37 return (md->md_threshold == 0 ||
38 ((md->md_options & LNET_MD_MAX_SIZE) != 0 &&
39 md->md_offset + md->md_max_size > md->md_length));
42 static inline int lnet_md_unlinkable (lnet_libmd_t *md)
44 /* Should unlink md when its refcount is 0 and either:
45 * - md has been flagged for deletion (by auto unlink or LNetM[DE]Unlink,
46 * in the latter case md may not be exhausted).
47 * - auto unlink is on and md is exhausted.
49 if (md->md_refcount != 0)
52 if ((md->md_flags & LNET_MD_FLAG_ZOMBIE) != 0)
55 return ((md->md_flags & LNET_MD_FLAG_AUTO_UNLINK) != 0 &&
56 lnet_md_exhausted(md));
60 #define LNET_LOCK() spin_lock(&the_lnet.ln_lock)
61 #define LNET_UNLOCK() spin_unlock(&the_lnet.ln_lock)
62 #define LNET_MUTEX_DOWN(m) mutex_down(m)
63 #define LNET_MUTEX_UP(m) mutex_up(m)
65 # ifndef HAVE_LIBPTHREAD
66 #define LNET_SINGLE_THREADED_LOCK(l) \
72 #define LNET_SINGLE_THREADED_UNLOCK(l) \
78 #define LNET_LOCK() LNET_SINGLE_THREADED_LOCK(the_lnet.ln_lock)
79 #define LNET_UNLOCK() LNET_SINGLE_THREADED_UNLOCK(the_lnet.ln_lock)
80 #define LNET_MUTEX_DOWN(m) LNET_SINGLE_THREADED_LOCK(*(m))
81 #define LNET_MUTEX_UP(m) LNET_SINGLE_THREADED_UNLOCK(*(m))
83 #define LNET_LOCK() pthread_mutex_lock(&the_lnet.ln_lock)
84 #define LNET_UNLOCK() pthread_mutex_unlock(&the_lnet.ln_lock)
85 #define LNET_MUTEX_DOWN(m) pthread_mutex_lock(m)
86 #define LNET_MUTEX_UP(m) pthread_mutex_unlock(m)
90 #define MAX_PORTALS 64
92 #ifdef LNET_USE_LIB_FREELIST
96 #define MAX_MSGS 2048 /* Outstanding messages */
100 lnet_freelist_alloc (lnet_freelist_t *fl)
102 /* ALWAYS called with liblock held */
105 if (list_empty (&fl->fl_list))
108 o = list_entry (fl->fl_list.next, lnet_freeobj_t, fo_list);
109 list_del (&o->fo_list);
110 return ((void *)&o->fo_contents);
114 lnet_freelist_free (lnet_freelist_t *fl, void *obj)
116 /* ALWAYS called with liblock held */
117 lnet_freeobj_t *o = list_entry (obj, lnet_freeobj_t, fo_contents);
119 list_add (&o->fo_list, &fl->fl_list);
123 static inline lnet_eq_t *
126 /* NEVER called with liblock held */
130 eq = (lnet_eq_t *)lnet_freelist_alloc(&the_lnet.ln_free_eqs);
137 lnet_eq_free (lnet_eq_t *eq)
139 /* ALWAYS called with liblock held */
140 lnet_freelist_free(&the_lnet.ln_free_eqs, eq);
143 static inline lnet_libmd_t *
144 lnet_md_alloc (lnet_md_t *umd)
146 /* NEVER called with liblock held */
150 md = (lnet_libmd_t *)lnet_freelist_alloc(&the_lnet.ln_free_mds);
157 lnet_md_free (lnet_libmd_t *md)
159 /* ALWAYS called with liblock held */
160 lnet_freelist_free (&the_lnet.ln_free_mds, md);
163 static inline lnet_me_t *
166 /* NEVER called with liblock held */
170 me = (lnet_me_t *)lnet_freelist_alloc(&the_lnet.ln_free_mes);
177 lnet_me_free (lnet_me_t *me)
179 /* ALWAYS called with liblock held */
180 lnet_freelist_free (&the_lnet.ln_free_mes, me);
183 static inline lnet_msg_t *
184 lnet_msg_alloc (void)
186 /* NEVER called with liblock held */
190 msg = (lnet_msg_t *)lnet_freelist_alloc(&the_lnet.ln_free_msgs);
194 /* NULL pointers, clear flags etc */
195 memset (msg, 0, sizeof (*msg));
197 msg->msg_ev.uid = LNET_UID_ANY;
204 lnet_msg_free (lnet_msg_t *msg)
206 /* ALWAYS called with liblock held */
207 LASSERT (!msg->msg_onactivelist);
208 lnet_freelist_free(&the_lnet.ln_free_msgs, msg);
213 static inline lnet_eq_t *
216 /* NEVER called with liblock held */
219 LIBCFS_ALLOC(eq, sizeof(*eq));
224 lnet_eq_free (lnet_eq_t *eq)
226 /* ALWAYS called with liblock held */
227 LIBCFS_FREE(eq, sizeof(*eq));
230 static inline lnet_libmd_t *
231 lnet_md_alloc (lnet_md_t *umd)
233 /* NEVER called with liblock held */
238 if ((umd->options & LNET_MD_KIOV) != 0) {
240 size = offsetof(lnet_libmd_t, md_iov.kiov[niov]);
242 niov = ((umd->options & LNET_MD_IOVEC) != 0) ?
244 size = offsetof(lnet_libmd_t, md_iov.iov[niov]);
247 LIBCFS_ALLOC(md, size);
250 /* Set here in case of early free */
251 md->md_options = umd->options;
259 lnet_md_free (lnet_libmd_t *md)
261 /* ALWAYS called with liblock held */
264 if ((md->md_options & LNET_MD_KIOV) != 0)
265 size = offsetof(lnet_libmd_t, md_iov.kiov[md->md_niov]);
267 size = offsetof(lnet_libmd_t, md_iov.iov[md->md_niov]);
269 LIBCFS_FREE(md, size);
272 static inline lnet_me_t *
275 /* NEVER called with liblock held */
278 LIBCFS_ALLOC(me, sizeof(*me));
283 lnet_me_free(lnet_me_t *me)
285 /* ALWAYS called with liblock held */
286 LIBCFS_FREE(me, sizeof(*me));
289 static inline lnet_msg_t *
292 /* NEVER called with liblock held */
295 LIBCFS_ALLOC(msg, sizeof(*msg));
298 /* NULL pointers, clear flags etc */
299 memset (msg, 0, sizeof (*msg));
301 msg->msg_ev.uid = LNET_UID_ANY;
308 lnet_msg_free(lnet_msg_t *msg)
310 /* ALWAYS called with liblock held */
311 LASSERT (!msg->msg_onactivelist);
312 LIBCFS_FREE(msg, sizeof(*msg));
316 extern lnet_libhandle_t *lnet_lookup_cookie (__u64 cookie, int type);
317 extern void lnet_initialise_handle (lnet_libhandle_t *lh, int type);
318 extern void lnet_invalidate_handle (lnet_libhandle_t *lh);
321 lnet_eq2handle (lnet_handle_eq_t *handle, lnet_eq_t *eq)
324 *handle = LNET_EQ_NONE;
328 handle->cookie = eq->eq_lh.lh_cookie;
331 static inline lnet_eq_t *
332 lnet_handle2eq (lnet_handle_eq_t *handle)
334 /* ALWAYS called with liblock held */
335 lnet_libhandle_t *lh = lnet_lookup_cookie(handle->cookie,
336 LNET_COOKIE_TYPE_EQ);
340 return (lh_entry (lh, lnet_eq_t, eq_lh));
344 lnet_md2handle (lnet_handle_md_t *handle, lnet_libmd_t *md)
346 handle->cookie = md->md_lh.lh_cookie;
349 static inline lnet_libmd_t *
350 lnet_handle2md (lnet_handle_md_t *handle)
352 /* ALWAYS called with liblock held */
353 lnet_libhandle_t *lh = lnet_lookup_cookie(handle->cookie,
354 LNET_COOKIE_TYPE_MD);
358 return (lh_entry (lh, lnet_libmd_t, md_lh));
361 static inline lnet_libmd_t *
362 lnet_wire_handle2md (lnet_handle_wire_t *wh)
364 /* ALWAYS called with liblock held */
365 lnet_libhandle_t *lh;
367 if (wh->wh_interface_cookie != the_lnet.ln_interface_cookie)
370 lh = lnet_lookup_cookie(wh->wh_object_cookie,
371 LNET_COOKIE_TYPE_MD);
375 return (lh_entry (lh, lnet_libmd_t, md_lh));
379 lnet_me2handle (lnet_handle_me_t *handle, lnet_me_t *me)
381 handle->cookie = me->me_lh.lh_cookie;
384 static inline lnet_me_t *
385 lnet_handle2me (lnet_handle_me_t *handle)
387 /* ALWAYS called with liblock held */
388 lnet_libhandle_t *lh = lnet_lookup_cookie(handle->cookie,
389 LNET_COOKIE_TYPE_ME);
393 return (lh_entry (lh, lnet_me_t, me_lh));
397 lnet_peer_addref_locked(lnet_peer_t *lp)
399 LASSERT (lp->lp_refcount > 0);
403 extern void lnet_destroy_peer_locked(lnet_peer_t *lp);
406 lnet_peer_decref_locked(lnet_peer_t *lp)
408 LASSERT (lp->lp_refcount > 0);
410 if (lp->lp_refcount == 0)
411 lnet_destroy_peer_locked(lp);
415 lnet_isrouter(lnet_peer_t *lp)
417 return lp->lp_rtr_refcount != 0;
421 lnet_ni_addref_locked(lnet_ni_t *ni)
423 LASSERT (ni->ni_refcount > 0);
428 lnet_ni_addref(lnet_ni_t *ni)
431 lnet_ni_addref_locked(ni);
436 lnet_ni_decref_locked(lnet_ni_t *ni)
438 LASSERT (ni->ni_refcount > 0);
440 if (ni->ni_refcount == 0)
441 list_add_tail(&ni->ni_list, &the_lnet.ln_zombie_nis);
445 lnet_ni_decref(lnet_ni_t *ni)
448 lnet_ni_decref_locked(ni);
452 static inline lnet_nid_t
453 lnet_ptlcompat_srcnid(lnet_nid_t src, lnet_nid_t dst)
455 /* Give myself a portals srcnid if I'm sending to portals */
456 if (the_lnet.ln_ptlcompat > 0 &&
457 LNET_NIDNET(dst) == 0)
458 return LNET_MKNID(0, LNET_NIDADDR(src));
464 lnet_ptlcompat_matchnid(lnet_nid_t lnet_nid, lnet_nid_t ptl_nid)
466 return ((ptl_nid == lnet_nid) ||
467 (the_lnet.ln_ptlcompat > 0 &&
468 LNET_NIDNET(ptl_nid) == 0 &&
469 LNET_NETTYP(LNET_NIDNET(lnet_nid)) != LOLND &&
470 LNET_NIDADDR(ptl_nid) == LNET_NIDADDR(lnet_nid)));
474 lnet_ptlcompat_matchnet(__u32 lnet_net, __u32 ptl_net)
476 return ((ptl_net == lnet_net) ||
477 (the_lnet.ln_ptlcompat > 0 &&
479 LNET_NETTYP(lnet_net) != LOLND));
482 static inline struct list_head *
483 lnet_nid2peerhash (lnet_nid_t nid)
485 unsigned int idx = LNET_NIDADDR(nid) % LNET_PEER_HASHSIZE;
487 return &the_lnet.ln_peer_hash[idx];
490 extern lnd_t the_lolnd;
493 /* unconditional registration */
494 #define LNET_REGISTER_ULND(lnd) \
498 lnet_register_lnd(&(lnd)); \
501 /* conditional registration */
502 #define LNET_REGISTER_ULND_IF_PRESENT(lnd) \
504 extern lnd_t lnd __attribute__ ((weak, alias("the_lolnd"))); \
506 if (&(lnd) != &the_lolnd) \
507 lnet_register_lnd(&(lnd)); \
513 lnet_set_msg_uid(lnet_ni_t *ni, lnet_msg_t *msg, lnet_uid_t uid)
515 LASSERT (msg->msg_ev.uid == LNET_UID_ANY);
516 msg->msg_ev.uid = uid;
520 extern lnet_ni_t *lnet_nid2ni_locked (lnet_nid_t nid);
521 extern lnet_ni_t *lnet_net2ni_locked (__u32 net);
522 static inline lnet_ni_t *
523 lnet_net2ni (__u32 net)
528 ni = lnet_net2ni_locked(net);
534 int lnet_notify(lnet_ni_t *ni, lnet_nid_t peer, int alive, time_t when);
535 int lnet_add_route(__u32 net, unsigned int hops, lnet_nid_t gateway_nid);
536 int lnet_check_routes(void);
537 int lnet_del_route(__u32 net, lnet_nid_t gw_nid);
538 void lnet_destroy_routes(void);
539 int lnet_get_route(int idx, __u32 *net, __u32 *hops,
540 lnet_nid_t *gateway, __u32 *alive);
541 void lnet_proc_init(void);
542 void lnet_proc_fini(void);
543 void lnet_init_rtrpools(void);
544 int lnet_alloc_rtrpools(int im_a_router);
545 void lnet_free_rtrpools(void);
546 lnet_remotenet_t *lnet_find_net_locked (__u32 net);
548 int lnet_islocalnid(lnet_nid_t nid);
549 int lnet_islocalnet(__u32 net);
551 void lnet_build_unlink_event(lnet_libmd_t *md, lnet_event_t *ev);
552 void lnet_enq_event_locked(lnet_eq_t *eq, lnet_event_t *ev);
553 void lnet_prep_send(lnet_msg_t *msg, int type, lnet_process_id_t target,
554 unsigned int offset, unsigned int len);
555 int lnet_send(lnet_nid_t nid, lnet_msg_t *msg);
556 void lnet_return_credits_locked (lnet_msg_t *msg);
557 void lnet_match_blocked_msg(lnet_libmd_t *md);
558 int lnet_parse (lnet_ni_t *ni, lnet_hdr_t *hdr,
559 lnet_nid_t fromnid, void *private, int rdma_req);
560 void lnet_recv(lnet_ni_t *ni, void *private, lnet_msg_t *msg, int delayed,
561 unsigned int offset, unsigned int mlen, unsigned int rlen);
562 lnet_msg_t *lnet_create_reply_msg (lnet_ni_t *ni, lnet_msg_t *get_msg);
563 void lnet_set_reply_msg_len(lnet_ni_t *ni, lnet_msg_t *msg, unsigned int len);
564 void lnet_finalize(lnet_ni_t *ni, lnet_msg_t *msg, int rc);
566 char *lnet_msgtyp2str (int type);
567 void lnet_print_hdr (lnet_hdr_t * hdr);
568 int lnet_fail_nid(lnet_nid_t nid, unsigned int threshold);
570 unsigned int lnet_iov_nob (unsigned int niov, struct iovec *iov);
571 int lnet_extract_iov (int dst_niov, struct iovec *dst,
572 int src_niov, struct iovec *src,
573 unsigned int offset, unsigned int len);
575 unsigned int lnet_kiov_nob (unsigned int niov, lnet_kiov_t *iov);
576 int lnet_extract_kiov (int dst_niov, lnet_kiov_t *dst,
577 int src_niov, lnet_kiov_t *src,
578 unsigned int offset, unsigned int len);
580 void lnet_copy_iov2iov (unsigned int ndiov, struct iovec *diov,
581 unsigned int doffset,
582 unsigned int nsiov, struct iovec *siov,
583 unsigned int soffset, unsigned int nob);
584 void lnet_copy_kiov2iov (unsigned int niov, struct iovec *iov,
585 unsigned int iovoffset,
586 unsigned int nkiov, lnet_kiov_t *kiov,
587 unsigned int kiovoffset, unsigned int nob);
588 void lnet_copy_iov2kiov (unsigned int nkiov, lnet_kiov_t *kiov,
589 unsigned int kiovoffset,
590 unsigned int niov, struct iovec *iov,
591 unsigned int iovoffset, unsigned int nob);
592 void lnet_copy_kiov2kiov (unsigned int ndkiov, lnet_kiov_t *dkiov,
593 unsigned int doffset,
594 unsigned int nskiov, lnet_kiov_t *skiov,
595 unsigned int soffset, unsigned int nob);
598 lnet_copy_iov2flat(int dlen, void *dest, unsigned int doffset,
599 unsigned int nsiov, struct iovec *siov, unsigned int soffset,
602 struct iovec diov = {/*.iov_base = */ dest, /*.iov_len = */ dlen};
604 lnet_copy_iov2iov(1, &diov, doffset,
605 nsiov, siov, soffset, nob);
609 lnet_copy_kiov2flat(int dlen, void *dest, unsigned int doffset,
610 unsigned int nsiov, lnet_kiov_t *skiov, unsigned int soffset,
613 struct iovec diov = {/* .iov_base = */ dest, /* .iov_len = */ dlen};
615 lnet_copy_kiov2iov(1, &diov, doffset,
616 nsiov, skiov, soffset, nob);
620 lnet_copy_flat2iov(unsigned int ndiov, struct iovec *diov, unsigned int doffset,
621 int slen, void *src, unsigned int soffset, unsigned int nob)
623 struct iovec siov = {/*.iov_base = */ src, /*.iov_len = */slen};
624 lnet_copy_iov2iov(ndiov, diov, doffset,
625 1, &siov, soffset, nob);
629 lnet_copy_flat2kiov(unsigned int ndiov, lnet_kiov_t *dkiov, unsigned int doffset,
630 int slen, void *src, unsigned int soffset, unsigned int nob)
632 struct iovec siov = {/* .iov_base = */ src, /* .iov_len = */ slen};
633 lnet_copy_iov2kiov(ndiov, dkiov, doffset,
634 1, &siov, soffset, nob);
637 void lnet_me_unlink(lnet_me_t *me);
639 void lnet_md_unlink(lnet_libmd_t *md);
640 void lnet_md_deconstruct(lnet_libmd_t *lmd, lnet_md_t *umd);
642 void lnet_register_lnd(lnd_t *lnd);
643 void lnet_unregister_lnd(lnd_t *lnd);
644 int lnet_set_ip_niaddr (lnet_ni_t *ni);
647 int lnet_connect(cfs_socket_t **sockp, lnet_nid_t peer_nid,
648 __u32 local_ip, __u32 peer_ip, int peer_port);
649 void lnet_connect_console_error(int rc, lnet_nid_t peer_nid,
650 __u32 peer_ip, int port);
651 int lnet_count_acceptor_nis(lnet_ni_t **first_ni);
652 int lnet_accept(lnet_ni_t *blind_ni, cfs_socket_t *sock, __u32 magic);
653 int lnet_acceptor_timeout(void);
654 int lnet_acceptor_port(void);
657 #ifdef HAVE_LIBPTHREAD
658 int lnet_count_acceptor_nis(lnet_ni_t **first_ni);
659 int lnet_acceptor_port(void);
662 int lnet_acceptor_start(void);
663 void lnet_acceptor_stop(void);
665 int lnet_peers_start_down(void);
666 int lnet_router_checker_start(void);
667 void lnet_router_checker_stop(void);
669 int lnet_ping_target_init(void);
670 void lnet_ping_target_fini(void);
671 int lnet_ping(lnet_process_id_t id, int timeout_ms,
672 lnet_process_id_t *ids, int n_ids);
674 int lnet_parse_ip2nets (char **networksp, char *ip2nets);
675 int lnet_parse_routes (char *route_str, int *im_a_router);
676 int lnet_parse_networks (struct list_head *nilist, char *networks);
678 int lnet_nid2peer_locked(lnet_peer_t **lpp, lnet_nid_t nid);
679 lnet_peer_t *lnet_find_peer_locked (lnet_nid_t nid);
680 void lnet_clear_peer_table(void);
681 void lnet_destroy_peer_table(void);
682 int lnet_create_peer_table(void);
683 void lnet_debug_peer(lnet_nid_t nid);