Whamcloud - gitweb
- Use refcounting to control lifetime of bulk descriptors.
[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 #include <linux/kp30.h>
27 #include <linux/obd.h>
28 #include <portals/p30.h>
29 #include <linux/lustre_idl.h>
30
31 /* default rpc ring length */
32 #define RPC_RING_LENGTH    10
33
34 struct ptlrpc_connection {
35         struct list_head c_link;
36         struct lustre_peer c_peer;
37         __u8 c_local_uuid[37];  /* XXX do we need this? */
38         __u8 c_remote_uuid[37]; 
39
40         int c_level;
41         __u32 c_generation;  /* changes upon new connection */
42         __u32 c_epoch;       /* changes when peer changes */
43         __u32 c_bootcount;   /* peer's boot count */ 
44
45         spinlock_t c_lock;
46         __u32 c_xid_in;
47         __u32 c_xid_out;
48
49         atomic_t c_refcount;
50         __u64 c_token;
51         __u64 c_remote_conn;
52         __u64 c_remote_token;
53 };
54
55 struct ptlrpc_client {
56         struct obd_device *cli_obd;
57         __u32 cli_request_portal;
58         __u32 cli_reply_portal;
59
60         __u64 cli_last_rcvd;
61         __u64 cli_last_committed;
62         __u32 cli_target_devno;
63
64         void *cli_data;
65         struct semaphore cli_rpc_sem; /* limits outstanding requests */
66
67         spinlock_t cli_lock; /* protects lists */
68         struct list_head cli_delayed_head; /* delayed until after recovery */
69         struct list_head cli_sending_head;
70         struct list_head cli_dying_head;
71         struct list_head cli_ha_item;
72         int (*cli_recover)(struct ptlrpc_client *); 
73
74         struct recovd_obd *cli_recovd;
75         char *cli_name;
76 };
77
78 /* state flags of requests */
79 #define PTL_RPC_FL_INTR      (1 << 0)
80 #define PTL_RPC_FL_REPLIED   (1 << 1)  /* reply was received */
81 #define PTL_RPC_FL_SENT      (1 << 2)
82 #define PTL_BULK_FL_SENT     (1 << 3)
83 #define PTL_BULK_FL_RCVD     (1 << 4)
84 #define PTL_RPC_FL_ERR       (1 << 5)
85 #define PTL_RPC_FL_TIMEOUT   (1 << 6)
86 #define PTL_RPC_FL_RESEND    (1 << 7)
87 #define PTL_RPC_FL_RECOVERY  (1 << 8)  /* retransmission for recovery */
88 #define PTL_RPC_FL_FINISHED  (1 << 9)
89 #define PTL_RPC_FL_RETAIN    (1 << 10) /* retain for replay after reply */
90 #define PTL_RPC_FL_REPLAY    (1 << 11) /* replay upon recovery */
91 #define PTL_RPC_FL_ALLOCREP  (1 << 12) /* reply buffer allocated */
92
93 struct ptlrpc_request { 
94         int rq_type; /* one of PTL_RPC_REQUEST, PTL_RPC_REPLY, PTL_RPC_BULK */
95         struct list_head rq_list;
96         struct list_head rq_multi;
97         struct obd_device *rq_obd;
98         int rq_status;
99         int rq_flags; 
100         __u32 rq_connid;
101         atomic_t rq_refcount;
102
103         int rq_reqlen;
104         struct lustre_msg *rq_reqmsg;
105
106         int rq_replen;
107         struct lustre_msg *rq_repmsg;
108         __u64 rq_transno;
109         __u64 rq_xid;
110
111         char *rq_bulkbuf;
112         int rq_bulklen;
113
114         int rq_level;
115         time_t rq_time;
116         time_t rq_timeout;
117         //        void * rq_reply_handle;
118         wait_queue_head_t rq_wait_for_rep;
119
120         /* incoming reply */
121         ptl_md_t rq_reply_md;
122         ptl_handle_md_t rq_reply_md_h;
123         ptl_handle_me_t rq_reply_me_h;
124
125         /* outgoing req/rep */
126         ptl_md_t rq_req_md;
127         ptl_handle_md_t rq_req_md_h;
128
129         struct lustre_peer rq_peer; /* XXX see service.c can this be factored away? */
130         struct obd_export *rq_export;
131         struct ptlrpc_connection *rq_connection;
132         struct ptlrpc_client *rq_client;
133         struct ptlrpc_service *rq_svc;
134 };
135
136 struct ptlrpc_bulk_page {
137         struct ptlrpc_bulk_desc *b_desc;
138         struct list_head b_link;
139         char *b_buf;
140         int b_buflen;
141         struct page *b_page;
142         __u32 b_xid;
143         __u32 b_flags;
144         struct dentry *b_dentry;
145         int (*b_cb)(struct ptlrpc_bulk_page *);
146
147         ptl_md_t b_md;
148         ptl_handle_md_t b_md_h;
149         ptl_handle_me_t b_me_h;
150 };
151
152 struct ptlrpc_bulk_desc {
153         int b_flags;
154         struct ptlrpc_connection *b_connection;
155         struct ptlrpc_client *b_client;
156         __u32 b_portal;
157         struct lustre_handle b_conn;
158         void (*b_cb)(struct ptlrpc_bulk_desc *, void *);
159         void *b_cb_data;
160
161         wait_queue_head_t b_waitq;
162         struct list_head b_page_list;
163         __u32 b_page_count;
164         atomic_t b_pages_remaining;
165         atomic_t b_refcount;
166         void *b_desc_private;
167 };
168
169 struct ptlrpc_thread {
170         struct list_head t_link;
171
172         __u32 t_flags; 
173         wait_queue_head_t t_ctl_waitq;
174 };
175
176 struct ptlrpc_service {
177         time_t srv_time;
178         time_t srv_timeout;
179
180         /* incoming request buffers */
181         /* FIXME: perhaps a list of EQs, if multiple NIs are used? */
182         char *srv_buf[RPC_RING_LENGTH];
183         __u32 srv_ref_count[RPC_RING_LENGTH];
184         ptl_handle_me_t srv_me_h[RPC_RING_LENGTH];
185         __u32 srv_buf_size;
186         __u32 srv_ring_length;
187         __u32 srv_req_portal;
188         __u32 srv_rep_portal;
189
190         __u32 srv_xid;
191
192         /* event queue */
193         ptl_handle_eq_t srv_eq_h;
194
195         struct lustre_peer srv_self;
196
197         wait_queue_head_t srv_waitq; /* all threads sleep on this */
198
199         spinlock_t srv_lock;
200         struct list_head srv_reqs;
201         struct list_head srv_threads;
202         int (*srv_handler)(struct ptlrpc_request *req);
203 };
204
205 static inline void ptlrpc_hdl2req(struct ptlrpc_request *req, struct lustre_handle *h)
206 {
207         req->rq_reqmsg->addr = h->addr;
208         req->rq_reqmsg->cookie = h->cookie;
209 }
210 struct ptlrpc_request *ptlrpc_prep_req2(struct ptlrpc_client *cl,
211                                         struct ptlrpc_connection *conn,
212                                         struct lustre_handle *handle, 
213                                        int opcode, int count, int *lengths,
214                                         char **bufs);
215
216 typedef void (*bulk_callback_t)(struct ptlrpc_bulk_desc *, void *);
217
218 typedef int (*svc_handler_t)(struct ptlrpc_request *req);
219
220 /* rpc/connection.c */
221 void ptlrpc_readdress_connection(struct ptlrpc_connection *conn, char *uuid);
222 struct ptlrpc_connection *ptlrpc_get_connection(struct lustre_peer *peer);
223 int ptlrpc_put_connection(struct ptlrpc_connection *c);
224 struct ptlrpc_connection *ptlrpc_connection_addref(struct ptlrpc_connection *);
225 void ptlrpc_init_connection(void);
226 void ptlrpc_cleanup_connection(void);
227
228 /* rpc/niobuf.c */
229 int ptlrpc_check_bulk_sent(struct ptlrpc_bulk_desc *bulk);
230 int ptlrpc_check_bulk_received(struct ptlrpc_bulk_desc *bulk);
231 int ptlrpc_send_bulk(struct ptlrpc_bulk_desc *);
232 int ptlrpc_register_bulk(struct ptlrpc_bulk_desc *);
233 int ptlrpc_abort_bulk(struct ptlrpc_bulk_desc *bulk);
234 int ptlrpc_reply(struct ptlrpc_service *svc, struct ptlrpc_request *req);
235 int ptlrpc_error(struct ptlrpc_service *svc, struct ptlrpc_request *req);
236 void ptlrpc_resend_req(struct ptlrpc_request *request);
237 int ptl_send_rpc(struct ptlrpc_request *request);
238 void ptlrpc_link_svc_me(struct ptlrpc_service *service, int i);
239
240 /* rpc/client.c */
241 void ptlrpc_init_client(struct recovd_obd *, 
242                         int (*recover)(struct ptlrpc_client *),
243                         int req_portal, int rep_portal,
244                         struct ptlrpc_client *);
245 void ptlrpc_cleanup_client(struct ptlrpc_client *cli);
246 __u8 *ptlrpc_req_to_uuid(struct ptlrpc_request *req);
247 struct ptlrpc_connection *ptlrpc_uuid_to_connection(char *uuid);
248
249 int ptlrpc_queue_wait(struct ptlrpc_request *req);
250 void ptlrpc_continue_req(struct ptlrpc_request *req);
251 int ptlrpc_replay_req(struct ptlrpc_request *req);
252 void ptlrpc_restart_req(struct ptlrpc_request *req);
253
254 struct ptlrpc_request *ptlrpc_prep_req(struct ptlrpc_client *cl,
255                                        struct ptlrpc_connection *u, int opcode,
256                                        int count, int *lengths, char **bufs);
257 void ptlrpc_free_req(struct ptlrpc_request *request);
258 void ptlrpc_req_finished(struct ptlrpc_request *request);
259 struct ptlrpc_bulk_desc *ptlrpc_prep_bulk(struct ptlrpc_connection *);
260 void ptlrpc_free_bulk(struct ptlrpc_bulk_desc *bulk);
261 struct ptlrpc_bulk_page *ptlrpc_prep_bulk_page(struct ptlrpc_bulk_desc *desc);
262 void ptlrpc_free_bulk_page(struct ptlrpc_bulk_page *page);
263 int ptlrpc_check_status(struct ptlrpc_request *req, int err);
264
265 /* rpc/service.c */
266 struct ptlrpc_service *
267 ptlrpc_init_svc(__u32 bufsize, int req_portal, int rep_portal, char *uuid,
268                 svc_handler_t);
269 void ptlrpc_stop_all_threads(struct ptlrpc_service *svc);
270 int ptlrpc_start_thread(struct obd_device *dev, struct ptlrpc_service *svc,
271                         char *name);
272 int ptlrpc_unregister_service(struct ptlrpc_service *service);
273
274 struct ptlrpc_svc_data { 
275         char *name;
276         struct ptlrpc_service *svc; 
277         struct ptlrpc_thread *thread;
278         struct obd_device *dev;
279 }; 
280
281 /* rpc/pack_generic.c */
282 int lustre_pack_msg(int count, int *lens, char **bufs, int *len,
283                     struct lustre_msg **msg);
284 int lustre_msg_size(int count, int *lengths);
285 int lustre_unpack_msg(struct lustre_msg *m, int len);
286 void *lustre_msg_buf(struct lustre_msg *m, int n);
287
288 static inline void ptlrpc_bulk_decref(struct ptlrpc_bulk_desc *desc)
289 {
290         if (atomic_dec_and_test(&desc->b_refcount)) {
291                 CDEBUG(D_PAGE, "Released last ref on %p, freeing\n", desc);
292                 ptlrpc_free_bulk(desc);
293         } else {
294                 CDEBUG(D_PAGE, "%p -> %d\n", desc,
295                        atomic_read(&desc->b_refcount));
296         }
297 }
298
299 #endif