Whamcloud - gitweb
merge b_devel into HEAD. Includes:
[fs/lustre-release.git] / lustre / include / linux / lustre_net.h
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (C) 2002 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
8  *   Lustre is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Lustre is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Lustre; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  */
22
23 #ifndef _LUSTRE_NET_H
24 #define _LUSTRE_NET_H
25
26 #ifdef __KERNEL__
27 #include <linux/version.h>
28 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
29 #include <linux/tqueue.h>
30 #else
31 #include <linux/workqueue.h>
32 #endif
33 #endif 
34
35 #include <linux/kp30.h>
36 // #include <linux/obd.h>
37 #include <portals/p30.h>
38 #include <linux/lustre_idl.h>
39 #include <linux/lustre_ha.h>
40 #include <linux/lustre_import.h>
41
42 /* The following constants determine how much memory is devoted to
43  * buffering in the lustre services.
44  *
45  * ?_NEVENTS            # event queue entries
46  *
47  * ?_NBUFS              # request buffers
48  * ?_BUFSIZE            # bytes in a single request buffer
49  * total memory = ?_NBUFS * ?_BUFSIZE
50  *
51  * ?_MAXREQSIZE         # maximum request service will receive
52  * larger messages will get dropped.
53  * request buffers are auto-unlinked when less than ?_MAXREQSIZE
54  * is left in them.
55  */
56
57 #define LDLM_NUM_THREADS        4
58 #define LDLM_NEVENT_MAX 8192UL
59 #define LDLM_NEVENTS    min(num_physpages / 64, LDLM_NEVENT_MAX)
60 #define LDLM_NBUF_MAX   256UL
61 #define LDLM_NBUFS      min(LDLM_NEVENTS / 16, LDLM_NBUF_MAX)
62 #define LDLM_BUFSIZE    (8 * 1024)
63 #define LDLM_MAXREQSIZE 1024
64
65 #define MDT_NUM_THREADS 8
66 #define MDS_NEVENT_MAX  8192UL
67 #define MDS_NEVENTS     min(num_physpages / 64, MDS_NEVENT_MAX)
68 #define MDS_NBUF_MAX    512UL
69 #define MDS_NBUFS       min(MDS_NEVENTS / 16, MDS_NBUF_MAX)
70 #define MDS_BUFSIZE     (8 * 1024)
71 /* Assume file name length = FNAME_MAX = 256 (true for extN).
72  *        path name length = PATH_MAX = 4096
73  *        LOV MD size max  = EA_MAX = 4000
74  * symlink:  FNAME_MAX + PATH_MAX  <- largest
75  * link:     FNAME_MAX + PATH_MAX  (mds_rec_link < mds_rec_create)
76  * rename:   FNAME_MAX + FNAME_MAX
77  * open:     FNAME_MAX + EA_MAX
78  *
79  * MDS_MAXREQSIZE ~= 4736 bytes =
80  * lustre_msg + ldlm_request + mds_body + mds_rec_create + FNAME_MAX + PATH_MAX
81  *
82  * Realistic size is about 512 bytes (20 character name + 128 char symlink),
83  * except in the open case where there are a large number of OSTs in a LOV.
84  */
85 #define MDS_MAXREQSIZE  (5 * 1024)
86
87 #define OST_NUM_THREADS 6
88 #define OST_NEVENT_MAX  32768UL
89 #define OST_NEVENTS     min(num_physpages / 16, OST_NEVENT_MAX)
90 #define OST_NBUF_MAX    1280UL
91 #define OST_NBUFS       min(OST_NEVENTS / 64, OST_NBUF_MAX)
92 #define OST_BUFSIZE     (8 * 1024)
93 /* OST_MAXREQSIZE ~= 1896 bytes =
94  * lustre_msg + obdo + 16 * obd_ioobj + 64 * niobuf_remote
95  *
96  * single object with 16 pages is 576 bytes
97  */
98 #define OST_MAXREQSIZE  (2 * 1024)
99
100 #define PTLBD_NUM_THREADS        4
101 #define PTLBD_NEVENTS    1024
102 #define PTLBD_NBUFS      20
103 #define PTLBD_BUFSIZE    (32 * 1024)
104 #define PTLBD_MAXREQSIZE 1024
105
106 #define CONN_INVALID 1
107
108 struct ptlrpc_peer {
109         ptl_nid_t         peer_nid;
110         struct ptlrpc_ni *peer_ni;
111 };
112
113 struct ptlrpc_connection {
114         struct list_head        c_link;
115         struct ptlrpc_peer      c_peer;
116         struct obd_uuid         c_local_uuid;  /* XXX do we need this? */
117         struct obd_uuid         c_remote_uuid;
118
119         __u32                   c_generation;  /* changes upon new connection */
120         __u32                   c_epoch;       /* changes when peer changes */
121         __u32                   c_bootcount;   /* peer's boot count */
122
123         spinlock_t              c_lock;        /* also protects req->rq_list */
124
125         atomic_t                c_refcount;
126         __u64                   c_token;
127         __u64                   c_remote_conn;
128         __u64                   c_remote_token;
129
130         struct list_head        c_delayed_head;/* delayed until post-recovery XXX imp? */
131         struct recovd_data      c_recovd_data;
132
133         struct list_head        c_imports;
134         struct list_head        c_exports;
135         struct list_head        c_sb_chain;
136         __u32                   c_flags; // can we indicate INVALID elsewhere?
137 };
138
139 struct ptlrpc_client {
140         __u32                     cli_request_portal;
141         __u32                     cli_reply_portal;
142
143         __u32                     cli_target_devno;
144
145         void                     *cli_data;
146         char                     *cli_name;
147 };
148
149 /* state flags of requests */
150 #define PTL_RPC_FL_INTR      (1 << 0)  /* reply wait was interrupted by user */
151 #define PTL_RPC_FL_REPLIED   (1 << 1)  /* reply was received */
152 #define PTL_RPC_FL_SENT      (1 << 2)  /* request was sent */
153 #define PTL_RPC_FL_WANT_ACK  (1 << 3)  /* reply is awaiting an ACK */
154 #define PTL_BULK_FL_SENT     (1 << 4)  /* outgoing bulk was sent */
155 #define PTL_BULK_FL_RCVD     (1 << 5)  /* incoming bulk was recieved */
156 #define PTL_RPC_FL_ERR       (1 << 6)  /* request failed due to RPC error */
157 #define PTL_RPC_FL_TIMEOUT   (1 << 7)  /* request timed out waiting for reply */
158 #define PTL_RPC_FL_RESEND    (1 << 8)  /* retransmit the request */
159 #define PTL_RPC_FL_RESTART   (1 << 9)  /* operation must be restarted */
160 #define PTL_RPC_FL_RETAIN    (1 << 10) /* retain for replay after reply */
161 #define PTL_RPC_FL_REPLAY    (1 << 11) /* replay upon recovery */
162 #define PTL_RPC_FL_ALLOCREP  (1 << 12) /* reply buffer allocated */
163 #define PTL_RPC_FL_NO_RESEND (1 << 13) /* don't automatically resend this req */
164 #define PTL_RPC_FL_RESENT    (1 << 14) /* server rcvd resend of this req */
165
166 struct ptlrpc_request {
167         int rq_type; /* one of PTL_RPC_MSG_* */
168         struct list_head rq_list;
169         struct obd_device *rq_obd;
170         int rq_status;
171         int rq_flags;
172         atomic_t rq_refcount;
173
174         int rq_request_portal; /* XXX FIXME bug 249 */
175         int rq_reply_portal; /* XXX FIXME bug 249 */
176
177         int rq_reqlen;
178         struct lustre_msg *rq_reqmsg;
179
180         int rq_timeout;
181         int rq_replen;
182         struct lustre_msg *rq_repmsg;
183         __u64 rq_transno;
184         __u64 rq_xid;
185
186         int rq_level;
187         wait_queue_head_t rq_wait_for_rep; /* XXX also _for_ack */
188
189         /* incoming reply */
190         ptl_md_t rq_reply_md;
191         ptl_handle_me_t rq_reply_me_h;
192
193         /* outgoing req/rep */
194         ptl_md_t rq_req_md;
195
196         struct ptlrpc_peer rq_peer; /* XXX see service.c can this be factored away? */
197         struct obd_export *rq_export;
198         struct ptlrpc_connection *rq_connection;
199         struct obd_import *rq_import;
200         struct ptlrpc_service *rq_svc;
201
202         void (*rq_replay_cb)(struct ptlrpc_request *);
203         void  *rq_replay_data;
204
205         /* Only used on the server side for tracking acks. */
206         struct ptlrpc_req_ack_lock {
207                 struct lustre_handle lock;
208                 __u32                mode;
209         } rq_ack_locks[4];
210 };
211
212 #define DEBUG_REQ(level, req, fmt, args...)                                    \
213 do {                                                                           \
214 CDEBUG(level,                                                                  \
215        "@@@ " fmt " req@%p x"LPD64"/t"LPD64" o%d->%s:%d lens %d/%d ref %d fl " \
216        "%x/%x/%x rc %x\n" ,  ## args, req, req->rq_xid,                        \
217        req->rq_reqmsg ? req->rq_reqmsg->transno : -1,                          \
218        req->rq_reqmsg ? req->rq_reqmsg->opc : -1,                              \
219        req->rq_connection ?                                                    \
220           (char *)req->rq_connection->c_remote_uuid.uuid : "<?>",              \
221        (req->rq_import && req->rq_import->imp_client) ?                        \
222            req->rq_import->imp_client->cli_request_portal : -1,                \
223        req->rq_reqlen, req->rq_replen,                                         \
224        atomic_read (&req->rq_refcount), req->rq_flags,                         \
225        req->rq_reqmsg ? req->rq_reqmsg->flags : 0,                             \
226        req->rq_repmsg ? req->rq_repmsg->flags : 0,                             \
227        req->rq_status);                                                        \
228 } while (0)
229
230 struct ptlrpc_bulk_page {
231         struct ptlrpc_bulk_desc *bp_desc;
232         struct list_head bp_link;
233         void *bp_buf;
234         int bp_buflen;
235         struct page *bp_page;
236         __u32 bp_xid;
237         __u32 bp_flags;
238         struct dentry *bp_dentry;
239         int (*bp_cb)(struct ptlrpc_bulk_page *);
240 };
241
242
243 struct ptlrpc_bulk_desc {
244         struct list_head bd_set_chain; /* entry in obd_brw_set */
245         struct obd_brw_set *bd_brw_set;
246         int bd_flags;
247         struct ptlrpc_connection *bd_connection;
248         struct ptlrpc_client *bd_client;
249         __u32 bd_portal;
250         struct lustre_handle bd_conn;
251         void (*bd_ptl_ev_hdlr)(struct ptlrpc_bulk_desc *);
252
253         wait_queue_head_t bd_waitq;
254         struct list_head bd_page_list;
255         __u32 bd_page_count;
256         atomic_t bd_refcount;
257         void *bd_desc_private;
258
259 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
260         struct work_struct bd_queue;
261 #else
262         struct tq_struct bd_queue;
263 #endif
264
265         ptl_md_t bd_md;
266         ptl_handle_md_t bd_md_h;
267         ptl_handle_me_t bd_me_h;
268
269         atomic_t bd_source_callback_count;
270
271         struct iovec bd_iov[16];    /* self-sized pre-allocated iov */
272 };
273
274 struct ptlrpc_thread {
275         struct list_head t_link;
276
277         __u32 t_flags;
278         wait_queue_head_t t_ctl_waitq;
279 };
280
281 struct ptlrpc_request_buffer_desc {
282         struct list_head       rqbd_list;
283         struct ptlrpc_srv_ni  *rqbd_srv_ni;
284         ptl_handle_me_t        rqbd_me_h;
285         atomic_t               rqbd_refcount;
286         char                  *rqbd_buffer;
287 };
288
289 struct ptlrpc_ni {
290         /* Generic interface state */
291         char                   *pni_name;
292         ptl_handle_ni_t         pni_ni_h;
293         ptl_handle_eq_t         pni_request_out_eq_h;
294         ptl_handle_eq_t         pni_reply_in_eq_h;
295         ptl_handle_eq_t         pni_reply_out_eq_h;
296         ptl_handle_eq_t         pni_bulk_put_source_eq_h;
297         ptl_handle_eq_t         pni_bulk_put_sink_eq_h;
298         ptl_handle_eq_t         pni_bulk_get_source_eq_h;
299         ptl_handle_eq_t         pni_bulk_get_sink_eq_h;
300 };
301
302 struct ptlrpc_srv_ni {
303         /* Interface-specific service state */
304         struct ptlrpc_service  *sni_service;    /* owning service */
305         struct ptlrpc_ni       *sni_ni;         /* network interface */
306         ptl_handle_eq_t         sni_eq_h;       /* event queue handle */
307         struct list_head        sni_rqbds;      /* all the request buffer descriptors */
308         __u32                   sni_nrqbds;     /* # request buffers */
309         atomic_t                sni_nrqbds_receiving; /* # request buffers posted */
310 };
311
312 struct ptlrpc_service {
313         time_t srv_time;
314         time_t srv_timeout;
315
316         struct list_head srv_ni_list;          /* list of interfaces */
317         __u32            srv_max_req_size;     /* biggest request to receive */
318         __u32            srv_buf_size;         /* # bytes in a request buffer */
319
320         __u32 srv_req_portal;
321         __u32 srv_rep_portal;
322
323         __u32 srv_xid;
324
325         wait_queue_head_t srv_waitq; /* all threads sleep on this */
326
327         spinlock_t srv_lock;
328         struct list_head srv_threads;
329         int (*srv_handler)(struct ptlrpc_request *req);
330         char *srv_name;  /* only statically allocated strings here; we don't clean them */
331
332         int                  srv_interface_rover;
333         struct ptlrpc_srv_ni srv_interfaces[0];
334 };
335
336 static inline void ptlrpc_hdl2req(struct ptlrpc_request *req,
337                                   struct lustre_handle *h)
338 {
339         req->rq_reqmsg->addr = h->addr;
340         req->rq_reqmsg->cookie = h->cookie;
341 }
342
343 typedef void (*bulk_callback_t)(struct ptlrpc_bulk_desc *, void *);
344
345 typedef int (*svc_handler_t)(struct ptlrpc_request *req);
346
347 /* rpc/events.c */
348 extern struct ptlrpc_ni ptlrpc_interfaces[];
349 extern int              ptlrpc_ninterfaces;
350 extern int ptlrpc_uuid_to_peer (struct obd_uuid *uuid, struct ptlrpc_peer *peer);
351
352 /* rpc/connection.c */
353 void ptlrpc_readdress_connection(struct ptlrpc_connection *, struct obd_uuid *uuid);
354 struct ptlrpc_connection *ptlrpc_get_connection(struct ptlrpc_peer *peer,
355                                                 struct obd_uuid *uuid);
356 int ptlrpc_put_connection(struct ptlrpc_connection *c);
357 struct ptlrpc_connection *ptlrpc_connection_addref(struct ptlrpc_connection *);
358 void ptlrpc_init_connection(void);
359 void ptlrpc_cleanup_connection(void);
360
361 /* rpc/niobuf.c */
362 int ptlrpc_check_bulk_sent(struct ptlrpc_bulk_desc *bulk);
363 int ptlrpc_check_bulk_received(struct ptlrpc_bulk_desc *bulk);
364 int ptlrpc_bulk_put(struct ptlrpc_bulk_desc *);
365 int ptlrpc_bulk_get(struct ptlrpc_bulk_desc *);
366 int ptlrpc_register_bulk_put(struct ptlrpc_bulk_desc *);
367 int ptlrpc_register_bulk_get(struct ptlrpc_bulk_desc *);
368 int ptlrpc_abort_bulk(struct ptlrpc_bulk_desc *bulk);
369 struct obd_brw_set *obd_brw_set_new(void);
370 void obd_brw_set_add(struct obd_brw_set *, struct ptlrpc_bulk_desc *);
371 void obd_brw_set_del(struct ptlrpc_bulk_desc *);
372 void obd_brw_set_decref(struct obd_brw_set *set);
373 void obd_brw_set_addref(struct obd_brw_set *set);
374
375 int ptlrpc_reply(struct ptlrpc_service *svc, struct ptlrpc_request *req);
376 int ptlrpc_error(struct ptlrpc_service *svc, struct ptlrpc_request *req);
377 void ptlrpc_resend_req(struct ptlrpc_request *request);
378 int ptl_send_rpc(struct ptlrpc_request *request);
379 void ptlrpc_link_svc_me(struct ptlrpc_request_buffer_desc *rqbd);
380
381 /* rpc/client.c */
382 void ptlrpc_init_client(int req_portal, int rep_portal, char *name,
383                         struct ptlrpc_client *);
384 void ptlrpc_cleanup_client(struct obd_import *imp);
385 struct obd_uuid *ptlrpc_req_to_uuid(struct ptlrpc_request *req);
386 struct ptlrpc_connection *ptlrpc_uuid_to_connection(struct obd_uuid *uuid);
387
388 int ll_brw_sync_wait(struct obd_brw_set *, int phase);
389
390 int ptlrpc_queue_wait(struct ptlrpc_request *req);
391 void ptlrpc_continue_req(struct ptlrpc_request *req);
392 int ptlrpc_replay_req(struct ptlrpc_request *req);
393 int ptlrpc_abort(struct ptlrpc_request *req);
394 void ptlrpc_restart_req(struct ptlrpc_request *req);
395 void ptlrpc_abort_inflight(struct obd_import *imp, int dying_import);
396
397 struct ptlrpc_request *ptlrpc_prep_req(struct obd_import *imp, int opcode,
398                                        int count, int *lengths, char **bufs);
399 void ptlrpc_free_req(struct ptlrpc_request *request);
400 void ptlrpc_req_finished(struct ptlrpc_request *request);
401 struct ptlrpc_request *ptlrpc_request_addref(struct ptlrpc_request *req);
402 struct ptlrpc_bulk_desc *ptlrpc_prep_bulk(struct ptlrpc_connection *);
403 void ptlrpc_free_bulk(struct ptlrpc_bulk_desc *bulk);
404 struct ptlrpc_bulk_page *ptlrpc_prep_bulk_page(struct ptlrpc_bulk_desc *desc);
405 void ptlrpc_free_bulk_page(struct ptlrpc_bulk_page *page);
406 void ptlrpc_retain_replayable_request(struct ptlrpc_request *req,
407                                       struct obd_import *imp);
408
409 /* rpc/service.c */
410 struct ptlrpc_service *
411 ptlrpc_init_svc(__u32 nevents, __u32 nbufs, __u32 bufsize, __u32 max_req_size,
412                 int req_portal, int rep_portal, svc_handler_t, char *name);
413 void ptlrpc_stop_all_threads(struct ptlrpc_service *svc);
414 int ptlrpc_start_thread(struct obd_device *dev, struct ptlrpc_service *svc,
415                         char *name);
416 int ptlrpc_unregister_service(struct ptlrpc_service *service);
417
418 struct ptlrpc_svc_data {
419         char *name;
420         struct ptlrpc_service *svc;
421         struct ptlrpc_thread *thread;
422         struct obd_device *dev;
423 };
424
425 /* rpc/pack_generic.c */
426 int lustre_pack_msg(int count, int *lens, char **bufs, int *len,
427                     struct lustre_msg **msg);
428 int lustre_msg_size(int count, int *lengths);
429 int lustre_unpack_msg(struct lustre_msg *m, int len);
430 void *lustre_msg_buf(struct lustre_msg *m, int n);
431
432 /* rpc/rpc.c */
433 __u32 ptlrpc_next_xid(void);
434
435 static inline void ptlrpc_bulk_decref(struct ptlrpc_bulk_desc *desc)
436 {
437         CDEBUG(D_PAGE, "%p -> %d\n", desc, atomic_read(&desc->bd_refcount) - 1);
438
439         if (atomic_dec_and_test(&desc->bd_refcount)) {
440                 CDEBUG(D_PAGE, "Released last ref on %p, freeing\n", desc);
441                 ptlrpc_free_bulk(desc);
442         }
443 }
444
445 static inline void ptlrpc_bulk_addref(struct ptlrpc_bulk_desc *desc)
446 {
447         atomic_inc(&desc->bd_refcount);
448         CDEBUG(D_PAGE, "Set refcount of %p to %d\n", desc,
449                atomic_read(&desc->bd_refcount));
450 }
451
452 #endif