Whamcloud - gitweb
* Added loopback optimisation to lib-move.c
[fs/lustre-release.git] / lnet / include / lnet / lib-p30.h
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * lib-p30.h
5  *
6  * Top level include for library side routines
7  */
8
9 #ifndef __PORTALS_LIB_P30_H__
10 #define __PORTALS_LIB_P30_H__
11
12 #include "build_check.h"
13
14 #if defined(__linux__)
15 #include <portals/linux/lib-p30.h>
16 #elif defined(__APPLE__)
17 #include <portals/darwin/lib-p30.h>
18 #else
19 #error Unsupported Operating System
20 #endif
21
22 #include <portals/types.h>
23 #include <libcfs/kp30.h>
24 #include <portals/p30.h>
25 #include <portals/nal.h>
26 #include <portals/lib-types.h>
27
28 static inline int ptl_is_wire_handle_none (ptl_handle_wire_t *wh)
29 {
30         return (wh->wh_interface_cookie == PTL_WIRE_HANDLE_NONE.wh_interface_cookie &&
31                 wh->wh_object_cookie == PTL_WIRE_HANDLE_NONE.wh_object_cookie);
32 }
33
34 #ifdef __KERNEL__
35 #define LIB_LOCK(nal,flags)                                     \
36         spin_lock_irqsave(&(nal)->libnal_ni.ni_lock, flags)
37 #define LIB_UNLOCK(nal,flags)                                   \
38         spin_unlock_irqrestore(&(nal)->libnal_ni.ni_lock, flags)
39 #else
40 #define LIB_LOCK(nal,flags)                                             \
41         (pthread_mutex_lock(&(nal)->libnal_ni.ni_mutex), (flags) = 0)
42 #define LIB_UNLOCK(nal,flags)                                   \
43         pthread_mutex_unlock(&(nal)->libnal_ni.ni_mutex)
44 #endif
45
46
47 #ifdef PTL_USE_LIB_FREELIST
48
49 #define MAX_MES         2048
50 #define MAX_MDS         2048
51 #define MAX_MSGS        2048    /* Outstanding messages */
52 #define MAX_EQS         512
53
54 extern int lib_freelist_init (lib_nal_t *nal, lib_freelist_t *fl, int nobj, int objsize);
55 extern void lib_freelist_fini (lib_nal_t *nal, lib_freelist_t *fl);
56
57 static inline void *
58 lib_freelist_alloc (lib_freelist_t *fl)
59 {
60         /* ALWAYS called with liblock held */
61         lib_freeobj_t *o;
62
63         if (list_empty (&fl->fl_list))
64                 return (NULL);
65         
66         o = list_entry (fl->fl_list.next, lib_freeobj_t, fo_list);
67         list_del (&o->fo_list);
68         return ((void *)&o->fo_contents);
69 }
70
71 static inline void
72 lib_freelist_free (lib_freelist_t *fl, void *obj)
73 {
74         /* ALWAYS called with liblock held */
75         lib_freeobj_t *o = list_entry (obj, lib_freeobj_t, fo_contents);
76         
77         list_add (&o->fo_list, &fl->fl_list);
78 }
79
80
81 static inline lib_eq_t *
82 lib_eq_alloc (lib_nal_t *nal)
83 {
84         /* NEVER called with liblock held */
85         unsigned long  flags;
86         lib_eq_t      *eq;
87         
88         LIB_LOCK (nal, flags);
89         eq = (lib_eq_t *)lib_freelist_alloc (&nal->libnal_ni.ni_free_eqs);
90         LIB_UNLOCK (nal, flags);
91
92         return (eq);
93 }
94
95 static inline void
96 lib_eq_free (lib_nal_t *nal, lib_eq_t *eq)
97 {
98         /* ALWAYS called with liblock held */
99         lib_freelist_free (&nal->libnal_ni.ni_free_eqs, eq);
100 }
101
102 static inline lib_md_t *
103 lib_md_alloc (lib_nal_t *nal, ptl_md_t *umd)
104 {
105         /* NEVER called with liblock held */
106         unsigned long  flags;
107         lib_md_t      *md;
108         
109         LIB_LOCK (nal, flags);
110         md = (lib_md_t *)lib_freelist_alloc (&nal->libnal_ni.ni_free_mds);
111         LIB_UNLOCK (nal, flags);
112
113         return (md);
114 }
115
116 static inline void
117 lib_md_free (lib_nal_t *nal, lib_md_t *md)
118 {
119         /* ALWAYS called with liblock held */
120         lib_freelist_free (&nal->libnal_ni.ni_free_mds, md);
121 }
122
123 static inline lib_me_t *
124 lib_me_alloc (lib_nal_t *nal)
125 {
126         /* NEVER called with liblock held */
127         unsigned long  flags;
128         lib_me_t      *me;
129         
130         LIB_LOCK (nal, flags);
131         me = (lib_me_t *)lib_freelist_alloc (&nal->libnal_ni.ni_free_mes);
132         LIB_UNLOCK (nal, flags);
133         
134         return (me);
135 }
136
137 static inline void
138 lib_me_free (lib_nal_t *nal, lib_me_t *me)
139 {
140         /* ALWAYS called with liblock held */
141         lib_freelist_free (&nal->libnal_ni.ni_free_mes, me);
142 }
143
144 static inline lib_msg_t *
145 lib_msg_alloc (lib_nal_t *nal)
146 {
147         /* NEVER called with liblock held */
148         unsigned long  flags;
149         lib_msg_t     *msg;
150         
151         LIB_LOCK (nal, flags);
152         msg = (lib_msg_t *)lib_freelist_alloc (&nal->libnal_ni.ni_free_msgs);
153         LIB_UNLOCK (nal, flags);
154
155         if (msg != NULL) {
156                 /* NULL pointers, clear flags etc */
157                 memset (msg, 0, sizeof (*msg));
158                 msg->ack_wmd = PTL_WIRE_HANDLE_NONE;
159         }
160         return(msg);
161 }
162
163 static inline void
164 lib_msg_free (lib_nal_t *nal, lib_msg_t *msg)
165 {
166         /* ALWAYS called with liblock held */
167         lib_freelist_free (&nal->libnal_ni.ni_free_msgs, msg);
168 }
169
170 #else
171
172 static inline lib_eq_t *
173 lib_eq_alloc (lib_nal_t *nal)
174 {
175         /* NEVER called with liblock held */
176         lib_eq_t *eq;
177
178         PORTAL_ALLOC(eq, sizeof(*eq));
179         return (eq);
180 }
181
182 static inline void
183 lib_eq_free (lib_nal_t *nal, lib_eq_t *eq)
184 {
185         /* ALWAYS called with liblock held */
186         PORTAL_FREE(eq, sizeof(*eq));
187 }
188
189 static inline lib_md_t *
190 lib_md_alloc (lib_nal_t *nal, ptl_md_t *umd)
191 {
192         /* NEVER called with liblock held */
193         lib_md_t *md;
194         int       size;
195         int       niov;
196
197         if ((umd->options & PTL_MD_KIOV) != 0) {
198                 niov = umd->length;
199                 size = offsetof(lib_md_t, md_iov.kiov[niov]);
200         } else {
201                 niov = ((umd->options & PTL_MD_IOVEC) != 0) ?
202                        umd->length : 1;
203                 size = offsetof(lib_md_t, md_iov.iov[niov]);
204         }
205
206         PORTAL_ALLOC(md, size);
207
208         if (md != NULL) {
209                 /* Set here in case of early free */
210                 md->options = umd->options;
211                 md->md_niov = niov;
212         }
213         
214         return (md);
215 }
216
217 static inline void 
218 lib_md_free (lib_nal_t *nal, lib_md_t *md)
219 {
220         /* ALWAYS called with liblock held */
221         int       size;
222
223         if ((md->options & PTL_MD_KIOV) != 0)
224                 size = offsetof(lib_md_t, md_iov.kiov[md->md_niov]);
225         else
226                 size = offsetof(lib_md_t, md_iov.iov[md->md_niov]);
227
228         PORTAL_FREE(md, size);
229 }
230
231 static inline lib_me_t *
232 lib_me_alloc (lib_nal_t *nal)
233 {
234         /* NEVER called with liblock held */
235         lib_me_t *me;
236
237         PORTAL_ALLOC(me, sizeof(*me));
238         return (me);
239 }
240
241 static inline void 
242 lib_me_free(lib_nal_t *nal, lib_me_t *me)
243 {
244         /* ALWAYS called with liblock held */
245         PORTAL_FREE(me, sizeof(*me));
246 }
247
248 static inline lib_msg_t *
249 lib_msg_alloc(lib_nal_t *nal)
250 {
251         /* NEVER called with liblock held; may be in interrupt... */
252         lib_msg_t *msg;
253
254         if (in_interrupt())
255                 PORTAL_ALLOC_ATOMIC(msg, sizeof(*msg));
256         else
257                 PORTAL_ALLOC(msg, sizeof(*msg));
258
259         if (msg != NULL) {
260                 /* NULL pointers, clear flags etc */
261                 memset (msg, 0, sizeof (*msg));
262                 msg->ack_wmd = PTL_WIRE_HANDLE_NONE;
263         }
264         return (msg);
265 }
266
267 static inline void 
268 lib_msg_free(lib_nal_t *nal, lib_msg_t *msg)
269 {
270         /* ALWAYS called with liblock held */
271         PORTAL_FREE(msg, sizeof(*msg));
272 }
273 #endif
274
275 extern lib_handle_t *lib_lookup_cookie (lib_nal_t *nal, __u64 cookie, int type);
276 extern void lib_initialise_handle (lib_nal_t *nal, lib_handle_t *lh, int type);
277 extern void lib_invalidate_handle (lib_nal_t *nal, lib_handle_t *lh);
278
279 static inline void
280 ptl_eq2handle (ptl_handle_eq_t *handle, lib_nal_t *nal, lib_eq_t *eq)
281 {
282         if (eq == NULL) {
283                 *handle = PTL_EQ_NONE;
284                 return;
285         }
286
287         handle->nal_idx = nal->libnal_ni.ni_api->nal_handle.nal_idx;
288         handle->cookie = eq->eq_lh.lh_cookie;
289 }
290
291 static inline lib_eq_t *
292 ptl_handle2eq (ptl_handle_eq_t *handle, lib_nal_t *nal)
293 {
294         /* ALWAYS called with liblock held */
295         lib_handle_t *lh = lib_lookup_cookie (nal, handle->cookie, 
296                                               PTL_COOKIE_TYPE_EQ);
297         if (lh == NULL)
298                 return (NULL);
299
300         return (lh_entry (lh, lib_eq_t, eq_lh));
301 }
302
303 static inline void
304 ptl_md2handle (ptl_handle_md_t *handle, lib_nal_t *nal, lib_md_t *md)
305 {
306         handle->nal_idx = nal->libnal_ni.ni_api->nal_handle.nal_idx;
307         handle->cookie = md->md_lh.lh_cookie;
308 }
309
310 static inline lib_md_t *
311 ptl_handle2md (ptl_handle_md_t *handle, lib_nal_t *nal)
312 {
313         /* ALWAYS called with liblock held */
314         lib_handle_t *lh = lib_lookup_cookie (nal, handle->cookie,
315                                               PTL_COOKIE_TYPE_MD);
316         if (lh == NULL)
317                 return (NULL);
318
319         return (lh_entry (lh, lib_md_t, md_lh));
320 }
321
322 static inline lib_md_t *
323 ptl_wire_handle2md (ptl_handle_wire_t *wh, lib_nal_t *nal)
324 {
325         /* ALWAYS called with liblock held */
326         lib_handle_t *lh;
327         
328         if (wh->wh_interface_cookie != nal->libnal_ni.ni_interface_cookie)
329                 return (NULL);
330         
331         lh = lib_lookup_cookie (nal, wh->wh_object_cookie,
332                                 PTL_COOKIE_TYPE_MD);
333         if (lh == NULL)
334                 return (NULL);
335
336         return (lh_entry (lh, lib_md_t, md_lh));
337 }
338
339 static inline void
340 ptl_me2handle (ptl_handle_me_t *handle, lib_nal_t *nal, lib_me_t *me)
341 {
342         handle->nal_idx = nal->libnal_ni.ni_api->nal_handle.nal_idx;
343         handle->cookie = me->me_lh.lh_cookie;
344 }
345
346 static inline lib_me_t *
347 ptl_handle2me (ptl_handle_me_t *handle, lib_nal_t *nal)
348 {
349         /* ALWAYS called with liblock held */
350         lib_handle_t *lh = lib_lookup_cookie (nal, handle->cookie,
351                                               PTL_COOKIE_TYPE_ME);
352         if (lh == NULL)
353                 return (NULL);
354
355         return (lh_entry (lh, lib_me_t, me_lh));
356 }
357
358 extern int lib_init(lib_nal_t *libnal, nal_t *apinal,
359                     ptl_process_id_t pid,
360                     ptl_ni_limits_t *desired_limits, 
361                     ptl_ni_limits_t *actual_limits);
362 extern int lib_fini(lib_nal_t *libnal);
363
364 /*
365  * When the NAL detects an incoming message header, it should call
366  * lib_parse() decode it.  If the message header is garbage, lib_parse()
367  * returns immediately with failure, otherwise the NAL callbacks will be
368  * called to receive the message body.  They are handed the private cookie
369  * as a way for the NAL to maintain state about which transaction is being
370  * processed.  An extra parameter, lib_msg contains the lib-level message
371  * state for passing to lib_finalize() when the message body has been
372  * received.
373  */
374 extern void lib_enq_event_locked (lib_nal_t *nal, void *private,
375                                   lib_eq_t *eq, ptl_event_t *ev);
376 extern void lib_finalize (lib_nal_t *nal, void *private, lib_msg_t *msg, 
377                           ptl_ni_fail_t ni_fail_type);
378 extern ptl_err_t lib_parse (lib_nal_t *nal, ptl_hdr_t *hdr, void *private);
379 extern lib_msg_t *lib_create_reply_msg (lib_nal_t *nal, ptl_nid_t peer_nid, 
380                                         lib_msg_t *get_msg);
381 extern void print_hdr (lib_nal_t * nal, ptl_hdr_t * hdr);
382
383
384 extern ptl_size_t lib_iov_nob (int niov, struct iovec *iov);
385 extern void lib_copy_iov2buf (char *dest, int niov, struct iovec *iov, 
386                               ptl_size_t offset, ptl_size_t len);
387 extern void lib_copy_buf2iov (int niov, struct iovec *iov, ptl_size_t offset, 
388                               char *src, ptl_size_t len);
389 extern int lib_extract_iov (int dst_niov, struct iovec *dst,
390                             int src_niov, struct iovec *src,
391                             ptl_size_t offset, ptl_size_t len);
392
393 extern ptl_size_t lib_kiov_nob (int niov, ptl_kiov_t *iov);
394 extern void lib_copy_kiov2buf (char *dest, int niov, ptl_kiov_t *kiov, 
395                                ptl_size_t offset, ptl_size_t len);
396 extern void lib_copy_buf2kiov (int niov, ptl_kiov_t *kiov, ptl_size_t offset,
397                                char *src, ptl_size_t len);
398 extern int lib_extract_kiov (int dst_niov, ptl_kiov_t *dst, 
399                              int src_niov, ptl_kiov_t *src,
400                              ptl_size_t offset, ptl_size_t len);
401
402 extern void lib_assert_wire_constants (void);
403
404 extern ptl_err_t lib_recv (lib_nal_t *nal, void *private, lib_msg_t *msg, lib_md_t *md,
405                            ptl_size_t offset, ptl_size_t mlen, ptl_size_t rlen);
406 extern ptl_err_t lib_send (lib_nal_t *nal, void *private, lib_msg_t *msg,
407                            ptl_hdr_t *hdr, int type, ptl_nid_t nid, ptl_pid_t pid,
408                            lib_md_t *md, ptl_size_t offset, ptl_size_t len);
409
410 extern int lib_api_ni_status (nal_t *nal, ptl_sr_index_t sr_idx,
411                               ptl_sr_value_t *status);
412 extern int lib_api_ni_dist (nal_t *nal, ptl_process_id_t *pid, 
413                             unsigned long *dist);
414
415 extern int lib_api_eq_alloc (nal_t *nal, ptl_size_t count,
416                              ptl_eq_handler_t callback, 
417                              ptl_handle_eq_t *handle);
418 extern int lib_api_eq_free(nal_t *nal, ptl_handle_eq_t *eqh);
419 extern int lib_api_eq_poll (nal_t *nal, 
420                             ptl_handle_eq_t *eventqs, int neq, int timeout_ms,
421                             ptl_event_t *event, int *which);
422
423 extern int lib_api_me_attach(nal_t *nal,
424                              ptl_pt_index_t portal,
425                              ptl_process_id_t match_id, 
426                              ptl_match_bits_t match_bits, 
427                              ptl_match_bits_t ignore_bits,
428                              ptl_unlink_t unlink, ptl_ins_pos_t pos,
429                              ptl_handle_me_t *handle);
430 extern int lib_api_me_insert(nal_t *nal,
431                              ptl_handle_me_t *current_meh,
432                              ptl_process_id_t match_id, 
433                              ptl_match_bits_t match_bits, 
434                              ptl_match_bits_t ignore_bits,
435                              ptl_unlink_t unlink, ptl_ins_pos_t pos,
436                              ptl_handle_me_t *handle);
437 extern int lib_api_me_unlink (nal_t *nal, ptl_handle_me_t *meh);
438 extern void lib_me_unlink(lib_nal_t *nal, lib_me_t *me);
439
440 extern int lib_api_get_id(nal_t *nal, ptl_process_id_t *pid);
441
442 extern void lib_md_unlink(lib_nal_t *nal, lib_md_t *md);
443 extern void lib_md_deconstruct(lib_nal_t *nal, lib_md_t *lmd, ptl_md_t *umd);
444 extern int lib_api_md_attach(nal_t *nal, ptl_handle_me_t *meh,
445                              ptl_md_t *umd, ptl_unlink_t unlink, 
446                              ptl_handle_md_t *handle);
447 extern int lib_api_md_bind(nal_t *nal, ptl_md_t *umd, ptl_unlink_t unlink,
448                            ptl_handle_md_t *handle);
449 extern int lib_api_md_unlink (nal_t *nal, ptl_handle_md_t *mdh);
450 extern int lib_api_md_update (nal_t *nal, ptl_handle_md_t *mdh,
451                               ptl_md_t *oldumd, ptl_md_t *newumd,
452                               ptl_handle_eq_t *testqh);
453
454 extern int lib_api_get(nal_t *apinal, ptl_handle_md_t *mdh, 
455                        ptl_process_id_t *id,
456                        ptl_pt_index_t portal, ptl_ac_index_t ac,
457                        ptl_match_bits_t match_bits, ptl_size_t offset);
458 extern int lib_api_put(nal_t *apinal, ptl_handle_md_t *mdh, 
459                        ptl_ack_req_t ack, ptl_process_id_t *id,
460                        ptl_pt_index_t portal, ptl_ac_index_t ac,
461                        ptl_match_bits_t match_bits, 
462                        ptl_size_t offset, ptl_hdr_data_t hdr_data);
463 extern int lib_api_fail_nid(nal_t *apinal, ptl_nid_t nid, unsigned int threshold);
464 extern int lib_api_loopback(nal_t *apinal, int set, int *enabled);
465
466 #endif