Whamcloud - gitweb
LU-1267 lfsck: framework (1) for MDT-OST consistency
[fs/lustre-release.git] / lustre / include / lustre_nrs_tbf.h
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License version 2 for more details.  A copy is
14  * included in the COPYING file that accompanied this code.
15
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (C) 2013 DataDirect Networks, Inc.
24  *
25  */
26 /*
27  *
28  * Network Request Scheduler (NRS) Token Bucket Filter(TBF) policy
29  *
30  */
31
32 #ifndef _LUSTRE_NRS_TBF_H
33 #define _LUSTRE_NRS_TBF_H
34 #include <lustre_net.h>
35
36 /* \name tbf
37  *
38  * TBF policy
39  *
40  * @{
41  */
42
43 struct nrs_tbf_head;
44 struct nrs_tbf_cmd;
45
46 struct nrs_tbf_jobid {
47         char            *tj_id;
48         cfs_list_t       tj_linkage;
49 };
50
51 struct nrs_tbf_client {
52         /** Resource object for policy instance. */
53         struct ptlrpc_nrs_resource       tc_res;
54         /** Node in the hash table. */
55         cfs_hlist_node_t                 tc_hnode;
56         /** NID of the client. */
57         lnet_nid_t                       tc_nid;
58         /** Jobid of the client. */
59         char                             tc_jobid[JOBSTATS_JOBID_SIZE];
60         /** Reference number of the client. */
61         cfs_atomic_t                     tc_ref;
62         /** Likage to rule. */
63         cfs_list_t                       tc_linkage;
64         /** Pointer to rule. */
65         struct nrs_tbf_rule             *tc_rule;
66         /** Generation of the rule matched. */
67         __u64                            tc_rule_generation;
68         /** Limit of RPC rate. */
69         __u64                            tc_rpc_rate;
70         /** Time to wait for next token. */
71         __u64                            tc_nsecs;
72         /** RPC token number. */
73         __u64                            tc_ntoken;
74         /** Token bucket depth. */
75         __u64                            tc_depth;
76         /** Time check-point. */
77         __u64                            tc_check_time;
78         /** List of queued requests. */
79         cfs_list_t                       tc_list;
80         /** Node in binary heap. */
81         cfs_binheap_node_t               tc_node;
82         /** Whether the client is in heap. */
83         bool                             tc_in_heap;
84         /** Sequence of the newest rule. */
85         __u32                            tc_rule_sequence;
86         /**
87          * Linkage into LRU list. Protected bucket lock of
88          * nrs_tbf_head::th_cli_hash.
89          */
90         cfs_list_t                       tc_lru;
91 };
92
93 #define MAX_TBF_NAME (16)
94
95 #define NTRS_STOPPING   0x0000001
96 #define NTRS_DEFAULT    0x0000002
97
98 struct nrs_tbf_rule {
99         /** Name of the rule. */
100         char                             tr_name[MAX_TBF_NAME];
101         /** Head belongs to. */
102         struct nrs_tbf_head             *tr_head;
103         /** Likage to head. */
104         cfs_list_t                       tr_linkage;
105         /** Nid list of the rule. */
106         cfs_list_t                       tr_nids;
107         /** Nid list string of the rule.*/
108         char                            *tr_nids_str;
109         /** Jobid list of the rule. */
110         cfs_list_t                       tr_jobids;
111         /** Jobid list string of the rule.*/
112         char                            *tr_jobids_str;
113         /** RPC/s limit. */
114         __u64                            tr_rpc_rate;
115         /** Time to wait for next token. */
116         __u64                            tr_nsecs;
117         /** Token bucket depth. */
118         __u64                            tr_depth;
119         /** List of client. */
120         cfs_list_t                       tr_cli_list;
121         /** Flags of the rule. */
122         __u32                            tr_flags;
123         /** Usage Reference count taken on the rule. */
124         cfs_atomic_t                     tr_ref;
125         /** Generation of the rule. */
126         __u64                            tr_generation;
127 };
128
129 struct nrs_tbf_ops {
130         char *o_name;
131         int (*o_startup)(struct ptlrpc_nrs_policy *, struct nrs_tbf_head *);
132         struct nrs_tbf_client *(*o_cli_find)(struct nrs_tbf_head *,
133                                              struct ptlrpc_request *);
134         struct nrs_tbf_client *(*o_cli_findadd)(struct nrs_tbf_head *,
135                                                 struct nrs_tbf_client *);
136         void (*o_cli_put)(struct nrs_tbf_head *, struct nrs_tbf_client *);
137         void (*o_cli_init)(struct nrs_tbf_client *, struct ptlrpc_request *);
138         int (*o_rule_init)(struct ptlrpc_nrs_policy *,
139                            struct nrs_tbf_rule *,
140                            struct nrs_tbf_cmd *);
141         int (*o_rule_dump)(struct nrs_tbf_rule *,
142                            char *,
143                            int);
144         int (*o_rule_match)(struct nrs_tbf_rule *,
145                             struct nrs_tbf_client *);
146         void (*o_rule_fini)(struct nrs_tbf_rule *);
147 };
148
149 struct nrs_tbf_dump {
150         char                            *td_buff;
151         int                              td_size;
152         int                              td_length;
153 };
154
155 #define NRS_TBF_TYPE_JOBID      "jobid"
156 #define NRS_TBF_TYPE_NID        "nid"
157 #define NRS_TBF_TYPE_MAX_LEN    20
158 #define NRS_TBF_FLAG_JOBID      0x0000001
159 #define NRS_TBF_FLAG_NID        0x0000002
160
161 struct nrs_tbf_bucket {
162         /**
163          * LRU list, updated on each access to client. Protected by
164          * bucket lock of nrs_tbf_head::th_cli_hash.
165          */
166         cfs_list_t      ntb_lru;
167 };
168
169 /**
170  * Private data structure for the TBF policy
171  */
172 struct nrs_tbf_head {
173         /**
174          * Resource object for policy instance.
175          */
176         struct ptlrpc_nrs_resource       th_res;
177         /**
178          * List of rules.
179          */
180         cfs_list_t                       th_list;
181         /**
182          * Lock to protect the list of rules.
183          */
184         spinlock_t                       th_rule_lock;
185         /**
186          * Generation of rules.
187          */
188         cfs_atomic_t                     th_rule_sequence;
189         /**
190          * Default rule.
191          */
192         struct nrs_tbf_rule             *th_rule;
193         /**
194          * Timer for next token.
195          */
196 #if defined(__KERNEL__) && defined(__linux__)
197         struct hrtimer                   th_timer;
198 #endif
199         /**
200          * Deadline of the timer.
201          */
202         __u64                            th_deadline;
203         /**
204          * Sequence of requests.
205          */
206         __u64                            th_sequence;
207         /**
208          * Heap of queues.
209          */
210         cfs_binheap_t                   *th_binheap;
211         /**
212          * Hash of clients.
213          */
214         cfs_hash_t                      *th_cli_hash;
215         /**
216          * Type of TBF policy.
217          */
218         char                             th_type[NRS_TBF_TYPE_MAX_LEN + 1];
219         /**
220          * Rule operations.
221          */
222         struct nrs_tbf_ops              *th_ops;
223         /**
224          * Flag of type.
225          */
226         __u32                            th_type_flag;
227         /**
228          * Index of bucket on hash table while purging.
229          */
230         int                              th_purge_start;
231 };
232
233 enum nrs_tbf_cmd_type {
234         NRS_CTL_TBF_START_RULE = 0,
235         NRS_CTL_TBF_STOP_RULE,
236         NRS_CTL_TBF_CHANGE_RATE,
237 };
238
239 struct nrs_tbf_cmd {
240         enum nrs_tbf_cmd_type    tc_cmd;
241         char                    *tc_name;
242         __u64                    tc_rpc_rate;
243         cfs_list_t               tc_nids;
244         char                    *tc_nids_str;
245         cfs_list_t               tc_jobids;
246         char                    *tc_jobids_str;
247         __u32                    tc_valid_types;
248         __u32                    tc_rule_flags;
249 };
250
251 struct nrs_tbf_req {
252         /**
253          * Linkage to queue.
254          */
255         cfs_list_t              tr_list;
256         /**
257          * Sequence of the request.
258          */
259         __u64                   tr_sequence;
260 };
261
262 /**
263  * TBF policy operations.
264  */
265 enum nrs_ctl_tbf {
266         /**
267          * Read the the data of a TBF policy.
268          */
269         NRS_CTL_TBF_RD_RULE = PTLRPC_NRS_CTL_1ST_POL_SPEC,
270         /**
271          * Write the the data of a TBF policy.
272          */
273         NRS_CTL_TBF_WR_RULE,
274 };
275
276 /** @} tbf */
277 #endif