4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2015, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Seagate, Inc.
32 * lnet/include/lnet/lib-lnet.h
35 #ifndef __LNET_LIB_LNET_H__
36 #define __LNET_LIB_LNET_H__
38 #include <linux/libcfs/libcfs.h>
39 #include <linux/lnet/api.h>
40 #include <linux/lnet/lib-types.h>
41 #include <uapi/linux/lnet/lnet-dlc.h>
42 #include <uapi/linux/lnet/lnet-types.h>
43 #include <uapi/linux/lnet/lnetctl.h>
44 #include <uapi/linux/lnet/nidstr.h>
46 extern struct lnet the_lnet; /* THE network */
48 #if (BITS_PER_LONG == 32)
49 /* 2 CPTs, allowing more CPTs might make us under memory pressure */
50 #define LNET_CPT_MAX_BITS 1
52 #else /* 64-bit system */
54 * 256 CPTs for thousands of CPUs, allowing more CPTs might make us
55 * under risk of consuming all lh_cookie.
57 #define LNET_CPT_MAX_BITS 8
58 #endif /* BITS_PER_LONG == 32 */
60 /* max allowed CPT number */
61 #define LNET_CPT_MAX (1 << LNET_CPT_MAX_BITS)
63 #define LNET_CPT_NUMBER (the_lnet.ln_cpt_number)
64 #define LNET_CPT_BITS (the_lnet.ln_cpt_bits)
65 #define LNET_CPT_MASK ((1ULL << LNET_CPT_BITS) - 1)
68 #define LNET_LOCK_EX CFS_PERCPT_LOCK_EX
70 static inline int lnet_is_route_alive(struct lnet_route *route)
73 if (!route->lr_gateway->lp_alive)
75 /* no NI status, assume it's alive */
76 if ((route->lr_gateway->lp_ping_feats &
77 LNET_PING_FEAT_NI_STATUS) == 0)
79 /* has NI status, check # down NIs */
80 return route->lr_downis == 0;
83 static inline int lnet_is_wire_handle_none(struct lnet_handle_wire *wh)
85 return (wh->wh_interface_cookie == LNET_WIRE_HANDLE_COOKIE_NONE &&
86 wh->wh_object_cookie == LNET_WIRE_HANDLE_COOKIE_NONE);
89 static inline int lnet_md_exhausted(struct lnet_libmd *md)
91 return (!md->md_threshold ||
92 ((md->md_options & LNET_MD_MAX_SIZE) &&
93 md->md_offset + md->md_max_size > md->md_length));
96 static inline int lnet_md_unlinkable(struct lnet_libmd *md)
99 * Should unlink md when its refcount is 0 and either:
100 * - md has been flagged for deletion (by auto unlink or
101 * LNetM[DE]Unlink, in the latter case md may not be exhausted).
102 * - auto unlink is on and md is exhausted.
107 if (md->md_flags & LNET_MD_FLAG_ZOMBIE)
110 return ((md->md_flags & LNET_MD_FLAG_AUTO_UNLINK) &&
111 lnet_md_exhausted(md));
114 #define lnet_cpt_table() (the_lnet.ln_cpt_table)
115 #define lnet_cpt_current() cfs_cpt_current(the_lnet.ln_cpt_table, 1)
118 lnet_cpt_of_cookie(__u64 cookie)
120 unsigned int cpt = (cookie >> LNET_COOKIE_TYPE_BITS) & LNET_CPT_MASK;
123 * LNET_CPT_NUMBER doesn't have to be power2, which means we can
124 * get illegal cpt from it's invalid cookie
126 return cpt < LNET_CPT_NUMBER ? cpt : cpt % LNET_CPT_NUMBER;
130 lnet_res_lock(int cpt)
132 cfs_percpt_lock(the_lnet.ln_res_lock, cpt);
136 lnet_res_unlock(int cpt)
138 cfs_percpt_unlock(the_lnet.ln_res_lock, cpt);
142 lnet_res_lock_current(void)
144 int cpt = lnet_cpt_current();
151 lnet_net_lock(int cpt)
153 cfs_percpt_lock(the_lnet.ln_net_lock, cpt);
157 lnet_net_unlock(int cpt)
159 cfs_percpt_unlock(the_lnet.ln_net_lock, cpt);
163 lnet_net_lock_current(void)
165 int cpt = lnet_cpt_current();
171 #define LNET_LOCK() lnet_net_lock(LNET_LOCK_EX)
172 #define LNET_UNLOCK() lnet_net_unlock(LNET_LOCK_EX)
174 #define lnet_ptl_lock(ptl) spin_lock(&(ptl)->ptl_lock)
175 #define lnet_ptl_unlock(ptl) spin_unlock(&(ptl)->ptl_lock)
176 #define lnet_eq_wait_lock() spin_lock(&the_lnet.ln_eq_wait_lock)
177 #define lnet_eq_wait_unlock() spin_unlock(&the_lnet.ln_eq_wait_lock)
178 #define lnet_ni_lock(ni) spin_lock(&(ni)->ni_lock)
179 #define lnet_ni_unlock(ni) spin_unlock(&(ni)->ni_lock)
181 #define MAX_PORTALS 64
183 static inline struct lnet_eq *
188 LIBCFS_ALLOC(eq, sizeof(*eq));
193 lnet_eq_free(struct lnet_eq *eq)
195 LIBCFS_FREE(eq, sizeof(*eq));
198 static inline struct lnet_libmd *
199 lnet_md_alloc(struct lnet_md *umd)
201 struct lnet_libmd *md;
205 if (umd->options & LNET_MD_KIOV) {
207 size = offsetof(struct lnet_libmd, md_iov.kiov[niov]);
209 niov = umd->options & LNET_MD_IOVEC ? umd->length : 1;
210 size = offsetof(struct lnet_libmd, md_iov.iov[niov]);
213 LIBCFS_ALLOC(md, size);
216 /* Set here in case of early free */
217 md->md_options = umd->options;
219 INIT_LIST_HEAD(&md->md_list);
226 lnet_md_free(struct lnet_libmd *md)
230 if (md->md_options & LNET_MD_KIOV)
231 size = offsetof(struct lnet_libmd, md_iov.kiov[md->md_niov]);
233 size = offsetof(struct lnet_libmd, md_iov.iov[md->md_niov]);
235 LIBCFS_FREE(md, size);
238 static inline struct lnet_me *
243 LIBCFS_ALLOC(me, sizeof(*me));
248 lnet_me_free(struct lnet_me *me)
250 LIBCFS_FREE(me, sizeof(*me));
253 static inline struct lnet_msg *
256 struct lnet_msg *msg;
258 LIBCFS_ALLOC(msg, sizeof(*msg));
260 /* no need to zero, LIBCFS_ALLOC does for us */
265 lnet_msg_free(struct lnet_msg *msg)
267 LASSERT(!msg->msg_onactivelist);
268 LIBCFS_FREE(msg, sizeof(*msg));
271 struct lnet_libhandle *lnet_res_lh_lookup(struct lnet_res_container *rec,
273 void lnet_res_lh_initialize(struct lnet_res_container *rec,
274 struct lnet_libhandle *lh);
276 lnet_res_lh_invalidate(struct lnet_libhandle *lh)
278 /* NB: cookie is still useful, don't reset it */
279 list_del(&lh->lh_hash_chain);
283 lnet_eq2handle(struct lnet_handle_eq *handle, struct lnet_eq *eq)
286 LNetInvalidateEQHandle(handle);
290 handle->cookie = eq->eq_lh.lh_cookie;
293 static inline struct lnet_eq *
294 lnet_handle2eq(struct lnet_handle_eq *handle)
296 struct lnet_libhandle *lh;
298 lh = lnet_res_lh_lookup(&the_lnet.ln_eq_container, handle->cookie);
302 return lh_entry(lh, struct lnet_eq, eq_lh);
306 lnet_md2handle(struct lnet_handle_md *handle, struct lnet_libmd *md)
308 handle->cookie = md->md_lh.lh_cookie;
311 static inline struct lnet_libmd *
312 lnet_handle2md(struct lnet_handle_md *handle)
314 /* ALWAYS called with resource lock held */
315 struct lnet_libhandle *lh;
318 cpt = lnet_cpt_of_cookie(handle->cookie);
319 lh = lnet_res_lh_lookup(the_lnet.ln_md_containers[cpt],
324 return lh_entry(lh, struct lnet_libmd, md_lh);
327 static inline struct lnet_libmd *
328 lnet_wire_handle2md(struct lnet_handle_wire *wh)
330 /* ALWAYS called with resource lock held */
331 struct lnet_libhandle *lh;
334 if (wh->wh_interface_cookie != the_lnet.ln_interface_cookie)
337 cpt = lnet_cpt_of_cookie(wh->wh_object_cookie);
338 lh = lnet_res_lh_lookup(the_lnet.ln_md_containers[cpt],
339 wh->wh_object_cookie);
343 return lh_entry(lh, struct lnet_libmd, md_lh);
347 lnet_me2handle(struct lnet_handle_me *handle, struct lnet_me *me)
349 handle->cookie = me->me_lh.lh_cookie;
352 static inline struct lnet_me *
353 lnet_handle2me(struct lnet_handle_me *handle)
355 /* ALWAYS called with resource lock held */
356 struct lnet_libhandle *lh;
359 cpt = lnet_cpt_of_cookie(handle->cookie);
360 lh = lnet_res_lh_lookup(the_lnet.ln_me_containers[cpt],
365 return lh_entry(lh, struct lnet_me, me_lh);
369 lnet_peer_addref_locked(struct lnet_peer *lp)
371 LASSERT(lp->lp_refcount > 0);
375 void lnet_destroy_peer_locked(struct lnet_peer *lp);
378 lnet_peer_decref_locked(struct lnet_peer *lp)
380 LASSERT(lp->lp_refcount > 0);
382 if (!lp->lp_refcount)
383 lnet_destroy_peer_locked(lp);
387 lnet_isrouter(struct lnet_peer *lp)
389 return lp->lp_rtr_refcount ? 1 : 0;
393 lnet_ni_addref_locked(struct lnet_ni *ni, int cpt)
395 LASSERT(cpt >= 0 && cpt < LNET_CPT_NUMBER);
396 LASSERT(*ni->ni_refs[cpt] >= 0);
398 (*ni->ni_refs[cpt])++;
402 lnet_ni_addref(struct lnet_ni *ni)
405 lnet_ni_addref_locked(ni, 0);
410 lnet_ni_decref_locked(struct lnet_ni *ni, int cpt)
412 LASSERT(cpt >= 0 && cpt < LNET_CPT_NUMBER);
413 LASSERT(*ni->ni_refs[cpt] > 0);
415 (*ni->ni_refs[cpt])--;
419 lnet_ni_decref(struct lnet_ni *ni)
422 lnet_ni_decref_locked(ni, 0);
426 void lnet_ni_free(struct lnet_ni *ni);
428 lnet_ni_alloc(__u32 net, struct cfs_expr_list *el, struct list_head *nilist);
431 lnet_nid2peerhash(lnet_nid_t nid)
433 return hash_long(nid, LNET_PEER_HASH_BITS);
436 static inline struct list_head *
437 lnet_net2rnethash(__u32 net)
439 return &the_lnet.ln_remote_nets_hash[(LNET_NETNUM(net) +
441 ((1U << the_lnet.ln_remote_nets_hbits) - 1)];
444 extern struct lnet_lnd the_lolnd;
445 extern int avoid_asym_router_failure;
447 int lnet_cpt_of_nid_locked(lnet_nid_t nid);
448 int lnet_cpt_of_nid(lnet_nid_t nid);
449 struct lnet_ni *lnet_nid2ni_locked(lnet_nid_t nid, int cpt);
450 struct lnet_ni *lnet_net2ni_locked(__u32 net, int cpt);
451 struct lnet_ni *lnet_net2ni(__u32 net);
453 extern int portal_rotor;
455 int lnet_lib_init(void);
456 void lnet_lib_exit(void);
458 int lnet_notify(struct lnet_ni *ni, lnet_nid_t peer, int alive,
460 void lnet_notify_locked(struct lnet_peer *lp, int notifylnd, int alive,
462 int lnet_add_route(__u32 net, __u32 hops, lnet_nid_t gateway_nid,
463 unsigned int priority);
464 int lnet_check_routes(void);
465 int lnet_del_route(__u32 net, lnet_nid_t gw_nid);
466 void lnet_destroy_routes(void);
467 int lnet_get_route(int idx, __u32 *net, __u32 *hops,
468 lnet_nid_t *gateway, __u32 *alive, __u32 *priority);
469 int lnet_get_rtr_pool_cfg(int idx, struct lnet_ioctl_pool_cfg *pool_cfg);
471 void lnet_router_debugfs_init(void);
472 void lnet_router_debugfs_fini(void);
473 int lnet_rtrpools_alloc(int im_a_router);
474 void lnet_destroy_rtrbuf(struct lnet_rtrbuf *rb, int npages);
475 int lnet_rtrpools_adjust(int tiny, int small, int large);
476 int lnet_rtrpools_enable(void);
477 void lnet_rtrpools_disable(void);
478 void lnet_rtrpools_free(int keep_pools);
479 struct lnet_remotenet *lnet_find_net_locked(__u32 net);
480 int lnet_dyn_add_ni(lnet_pid_t requested_pid,
481 struct lnet_ioctl_config_data *conf);
482 int lnet_dyn_del_ni(__u32 net);
483 int lnet_clear_lazy_portal(struct lnet_ni *ni, int portal, char *reason);
485 int lnet_islocalnid(lnet_nid_t nid);
486 int lnet_islocalnet(__u32 net);
488 void lnet_msg_attach_md(struct lnet_msg *msg, struct lnet_libmd *md,
489 unsigned int offset, unsigned int mlen);
490 void lnet_msg_detach_md(struct lnet_msg *msg, int status);
491 void lnet_build_unlink_event(struct lnet_libmd *md, struct lnet_event *ev);
492 void lnet_build_msg_event(struct lnet_msg *msg, enum lnet_event_kind ev_type);
493 void lnet_msg_commit(struct lnet_msg *msg, int cpt);
494 void lnet_msg_decommit(struct lnet_msg *msg, int cpt, int status);
496 void lnet_eq_enqueue_event(struct lnet_eq *eq, struct lnet_event *ev);
497 void lnet_prep_send(struct lnet_msg *msg, int type,
498 struct lnet_process_id target, unsigned int offset,
500 int lnet_send(lnet_nid_t nid, struct lnet_msg *msg, lnet_nid_t rtr_nid);
501 void lnet_return_tx_credits_locked(struct lnet_msg *msg);
502 void lnet_return_rx_credits_locked(struct lnet_msg *msg);
503 void lnet_schedule_blocked_locked(struct lnet_rtrbufpool *rbp);
504 void lnet_drop_routed_msgs_locked(struct list_head *list, int cpt);
506 /* portals functions */
507 /* portals attributes */
509 lnet_ptl_is_lazy(struct lnet_portal *ptl)
511 return !!(ptl->ptl_options & LNET_PTL_LAZY);
515 lnet_ptl_is_unique(struct lnet_portal *ptl)
517 return !!(ptl->ptl_options & LNET_PTL_MATCH_UNIQUE);
521 lnet_ptl_is_wildcard(struct lnet_portal *ptl)
523 return !!(ptl->ptl_options & LNET_PTL_MATCH_WILDCARD);
527 lnet_ptl_setopt(struct lnet_portal *ptl, int opt)
529 ptl->ptl_options |= opt;
533 lnet_ptl_unsetopt(struct lnet_portal *ptl, int opt)
535 ptl->ptl_options &= ~opt;
538 /* match-table functions */
539 struct list_head *lnet_mt_match_head(struct lnet_match_table *mtable,
540 struct lnet_process_id id, __u64 mbits);
541 struct lnet_match_table *lnet_mt_of_attach(unsigned int index,
542 struct lnet_process_id id,
543 __u64 mbits, __u64 ignore_bits,
544 enum lnet_ins_pos pos);
545 int lnet_mt_match_md(struct lnet_match_table *mtable,
546 struct lnet_match_info *info, struct lnet_msg *msg);
548 /* portals match/attach functions */
549 void lnet_ptl_attach_md(struct lnet_me *me, struct lnet_libmd *md,
550 struct list_head *matches, struct list_head *drops);
551 void lnet_ptl_detach_md(struct lnet_me *me, struct lnet_libmd *md);
552 int lnet_ptl_match_md(struct lnet_match_info *info, struct lnet_msg *msg);
554 /* initialized and finalize portals */
555 int lnet_portals_create(void);
556 void lnet_portals_destroy(void);
558 /* message functions */
559 int lnet_parse(struct lnet_ni *ni, struct lnet_hdr *hdr,
560 lnet_nid_t fromnid, void *private, int rdma_req);
561 int lnet_parse_local(struct lnet_ni *ni, struct lnet_msg *msg);
562 int lnet_parse_forward_locked(struct lnet_ni *ni, struct lnet_msg *msg);
564 void lnet_recv(struct lnet_ni *ni, void *private, struct lnet_msg *msg,
565 int delayed, unsigned int offset, unsigned int mlen,
567 void lnet_ni_recv(struct lnet_ni *ni, void *private, struct lnet_msg *msg,
568 int delayed, unsigned int offset,
569 unsigned int mlen, unsigned int rlen);
571 struct lnet_msg *lnet_create_reply_msg(struct lnet_ni *ni,
572 struct lnet_msg *get_msg);
573 void lnet_set_reply_msg_len(struct lnet_ni *ni, struct lnet_msg *msg,
576 void lnet_finalize(struct lnet_ni *ni, struct lnet_msg *msg, int rc);
578 void lnet_drop_message(struct lnet_ni *ni, int cpt, void *private,
580 void lnet_drop_delayed_msg_list(struct list_head *head, char *reason);
581 void lnet_recv_delayed_msg_list(struct list_head *head);
583 int lnet_msg_container_setup(struct lnet_msg_container *container, int cpt);
584 void lnet_msg_container_cleanup(struct lnet_msg_container *container);
585 void lnet_msg_containers_destroy(void);
586 int lnet_msg_containers_create(void);
588 char *lnet_msgtyp2str(int type);
589 void lnet_print_hdr(struct lnet_hdr *hdr);
590 int lnet_fail_nid(lnet_nid_t nid, unsigned int threshold);
592 /** \addtogroup lnet_fault_simulation @{ */
594 int lnet_fault_ctl(int cmd, struct libcfs_ioctl_data *data);
595 int lnet_fault_init(void);
596 void lnet_fault_fini(void);
598 bool lnet_drop_rule_match(struct lnet_hdr *hdr);
600 int lnet_delay_rule_add(struct lnet_fault_attr *attr);
601 int lnet_delay_rule_del(lnet_nid_t src, lnet_nid_t dst, bool shutdown);
602 int lnet_delay_rule_list(int pos, struct lnet_fault_attr *attr,
603 struct lnet_fault_stat *stat);
604 void lnet_delay_rule_reset(void);
605 void lnet_delay_rule_check(void);
606 bool lnet_delay_rule_match_locked(struct lnet_hdr *hdr, struct lnet_msg *msg);
608 /** @} lnet_fault_simulation */
610 void lnet_counters_get(struct lnet_counters *counters);
611 void lnet_counters_reset(void);
613 unsigned int lnet_iov_nob(unsigned int niov, struct kvec *iov);
614 int lnet_extract_iov(int dst_niov, struct kvec *dst,
615 int src_niov, const struct kvec *src,
616 unsigned int offset, unsigned int len);
618 unsigned int lnet_kiov_nob(unsigned int niov, struct bio_vec *iov);
619 int lnet_extract_kiov(int dst_niov, struct bio_vec *dst,
620 int src_niov, const struct bio_vec *src,
621 unsigned int offset, unsigned int len);
623 void lnet_copy_iov2iter(struct iov_iter *to,
624 unsigned int nsiov, const struct kvec *siov,
625 unsigned int soffset, unsigned int nob);
626 void lnet_copy_kiov2iter(struct iov_iter *to,
627 unsigned int nkiov, const struct bio_vec *kiov,
628 unsigned int kiovoffset, unsigned int nob);
630 void lnet_me_unlink(struct lnet_me *me);
632 void lnet_md_unlink(struct lnet_libmd *md);
633 void lnet_md_deconstruct(struct lnet_libmd *lmd, struct lnet_md *umd);
635 void lnet_register_lnd(struct lnet_lnd *lnd);
636 void lnet_unregister_lnd(struct lnet_lnd *lnd);
638 int lnet_connect(struct socket **sockp, lnet_nid_t peer_nid,
639 __u32 local_ip, __u32 peer_ip, int peer_port);
640 void lnet_connect_console_error(int rc, lnet_nid_t peer_nid,
641 __u32 peer_ip, int port);
642 int lnet_count_acceptor_nis(void);
643 int lnet_acceptor_timeout(void);
644 int lnet_acceptor_port(void);
646 int lnet_count_acceptor_nis(void);
647 int lnet_acceptor_port(void);
649 int lnet_acceptor_start(void);
650 void lnet_acceptor_stop(void);
652 int lnet_ipif_query(char *name, int *up, __u32 *ip, __u32 *mask);
653 int lnet_ipif_enumerate(char ***names);
654 void lnet_ipif_free_enumeration(char **names, int n);
655 int lnet_sock_setbuf(struct socket *socket, int txbufsize, int rxbufsize);
656 int lnet_sock_getbuf(struct socket *socket, int *txbufsize, int *rxbufsize);
657 int lnet_sock_getaddr(struct socket *socket, bool remote, __u32 *ip, int *port);
658 int lnet_sock_write(struct socket *sock, void *buffer, int nob, int timeout);
659 int lnet_sock_read(struct socket *sock, void *buffer, int nob, int timeout);
661 int lnet_sock_listen(struct socket **sockp, __u32 ip, int port, int backlog);
662 int lnet_sock_accept(struct socket **newsockp, struct socket *sock);
663 int lnet_sock_connect(struct socket **sockp, int *fatal,
664 __u32 local_ip, int local_port,
665 __u32 peer_ip, int peer_port);
666 void libcfs_sock_release(struct socket *sock);
668 int lnet_peers_start_down(void);
669 int lnet_peer_buffer_credits(struct lnet_ni *ni);
671 int lnet_router_checker_start(void);
672 void lnet_router_checker_stop(void);
673 void lnet_router_ni_update_locked(struct lnet_peer *gw, __u32 net);
674 void lnet_swap_pinginfo(struct lnet_ping_info *info);
676 int lnet_parse_ip2nets(char **networksp, char *ip2nets);
677 int lnet_parse_routes(char *route_str, int *im_a_router);
678 int lnet_parse_networks(struct list_head *nilist, char *networks);
679 int lnet_net_unique(__u32 net, struct list_head *nilist);
681 int lnet_nid2peer_locked(struct lnet_peer **lpp, lnet_nid_t nid, int cpt);
682 struct lnet_peer *lnet_find_peer_locked(struct lnet_peer_table *ptable,
684 void lnet_peer_tables_cleanup(struct lnet_ni *ni);
685 void lnet_peer_tables_destroy(void);
686 int lnet_peer_tables_create(void);
687 void lnet_debug_peer(lnet_nid_t nid);
688 int lnet_get_peer_info(__u32 peer_index, __u64 *nid,
689 char alivness[LNET_MAX_STR_LEN],
690 __u32 *cpt_iter, __u32 *refcount,
691 __u32 *ni_peer_tx_credits, __u32 *peer_tx_credits,
692 __u32 *peer_rtr_credits, __u32 *peer_min_rtr_credtis,
693 __u32 *peer_tx_qnob);
696 lnet_peer_set_alive(struct lnet_peer *lp)
698 lp->lp_last_query = jiffies;
699 lp->lp_last_alive = jiffies;
701 lnet_notify_locked(lp, 0, 1, lp->lp_last_alive);