Whamcloud - gitweb
LU-1757 brw: add short io osc/ost transfer.
[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.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; If not, see
17  * http://www.gnu.org/licenses/gpl-2.0.html
18  *
19  * GPL HEADER END
20  */
21 /*
22  * Copyright (C) 2013 DataDirect Networks, Inc.
23  *
24  * Copyright (c) 2014, Intel Corporation.
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
35 /* \name tbf
36  *
37  * TBF policy
38  *
39  * @{
40  */
41
42 struct nrs_tbf_head;
43 struct nrs_tbf_cmd;
44
45 #define NRS_TBF_MATCH_FULL      0x0000001
46 #define NRS_TBF_MATCH_WILDCARD  0x0000002
47
48 struct nrs_tbf_jobid {
49         char            *tj_id;
50         __u32            tj_match_flag;
51         struct list_head tj_linkage;
52 };
53
54 #define NRS_TBF_KEY_LEN (LNET_NIDSTR_SIZE + LUSTRE_JOBID_SIZE + 3 + 2)
55 struct nrs_tbf_client {
56         /** Resource object for policy instance. */
57         struct ptlrpc_nrs_resource       tc_res;
58         /** Node in the hash table. */
59         struct hlist_node                tc_hnode;
60         /** NID of the client. */
61         lnet_nid_t                       tc_nid;
62         /** Jobid of the client. */
63         char                             tc_jobid[LUSTRE_JOBID_SIZE];
64         /** opcode of the client. */
65         __u32                            tc_opcode;
66         /** Hash key of the client. */
67         char                             tc_key[NRS_TBF_KEY_LEN];
68         /** Reference number of the client. */
69         atomic_t                         tc_ref;
70         /** Lock to protect rule and linkage. */
71         spinlock_t                       tc_rule_lock;
72         /** Linkage to rule. */
73         struct list_head                 tc_linkage;
74         /** Pointer to rule. */
75         struct nrs_tbf_rule             *tc_rule;
76         /** Generation of the rule matched. */
77         __u64                            tc_rule_generation;
78         /** Limit of RPC rate. */
79         __u64                            tc_rpc_rate;
80         /** Time to wait for next token. */
81         __u64                            tc_nsecs;
82         /** RPC token number. */
83         __u64                            tc_ntoken;
84         /** Token bucket depth. */
85         __u64                            tc_depth;
86         /** Time check-point. */
87         __u64                            tc_check_time;
88         /** List of queued requests. */
89         struct list_head                 tc_list;
90         /** Node in binary heap. */
91         struct cfs_binheap_node          tc_node;
92         /** Whether the client is in heap. */
93         bool                             tc_in_heap;
94         /** Sequence of the newest rule. */
95         __u32                            tc_rule_sequence;
96         /**
97          * Linkage into LRU list. Protected bucket lock of
98          * nrs_tbf_head::th_cli_hash.
99          */
100         struct list_head                 tc_lru;
101 };
102
103 #define MAX_TBF_NAME (16)
104
105 #define NTRS_STOPPING   0x0000001
106 #define NTRS_DEFAULT    0x0000002
107
108 struct nrs_tbf_rule {
109         /** Name of the rule. */
110         char                             tr_name[MAX_TBF_NAME];
111         /** Head belongs to. */
112         struct nrs_tbf_head             *tr_head;
113         /** Likage to head. */
114         struct list_head                 tr_linkage;
115         /** Nid list of the rule. */
116         struct list_head                 tr_nids;
117         /** Nid list string of the rule.*/
118         char                            *tr_nids_str;
119         /** Jobid list of the rule. */
120         struct list_head                 tr_jobids;
121         /** Jobid list string of the rule.*/
122         char                            *tr_jobids_str;
123         /** Opcode bitmap of the rule. */
124         struct cfs_bitmap               *tr_opcodes;
125         /** Opcode list string of the rule.*/
126         char                            *tr_opcodes_str;
127         /** Condition list of the rule.*/
128         struct list_head                tr_conds;
129         /** Generic condition string of the rule. */
130         char                            *tr_conds_str;
131         /** RPC/s limit. */
132         __u64                            tr_rpc_rate;
133         /** Time to wait for next token. */
134         __u64                            tr_nsecs;
135         /** Token bucket depth. */
136         __u64                            tr_depth;
137         /** Lock to protect the list of clients. */
138         spinlock_t                       tr_rule_lock;
139         /** List of client. */
140         struct list_head                 tr_cli_list;
141         /** Flags of the rule. */
142         __u32                            tr_flags;
143         /** Usage Reference count taken on the rule. */
144         atomic_t                         tr_ref;
145         /** Generation of the rule. */
146         __u64                            tr_generation;
147 };
148
149 struct nrs_tbf_ops {
150         char *o_name;
151         int (*o_startup)(struct ptlrpc_nrs_policy *, struct nrs_tbf_head *);
152         struct nrs_tbf_client *(*o_cli_find)(struct nrs_tbf_head *,
153                                              struct ptlrpc_request *);
154         struct nrs_tbf_client *(*o_cli_findadd)(struct nrs_tbf_head *,
155                                                 struct nrs_tbf_client *);
156         void (*o_cli_put)(struct nrs_tbf_head *, struct nrs_tbf_client *);
157         void (*o_cli_init)(struct nrs_tbf_client *, struct ptlrpc_request *);
158         int (*o_rule_init)(struct ptlrpc_nrs_policy *,
159                            struct nrs_tbf_rule *,
160                            struct nrs_tbf_cmd *);
161         int (*o_rule_dump)(struct nrs_tbf_rule *, struct seq_file *);
162         int (*o_rule_match)(struct nrs_tbf_rule *,
163                             struct nrs_tbf_client *);
164         void (*o_rule_fini)(struct nrs_tbf_rule *);
165 };
166
167 #define NRS_TBF_TYPE_JOBID      "jobid"
168 #define NRS_TBF_TYPE_NID        "nid"
169 #define NRS_TBF_TYPE_OPCODE     "opcode"
170 #define NRS_TBF_TYPE_GENERIC    "generic"
171 #define NRS_TBF_TYPE_MAX_LEN    20
172
173 enum nrs_tbf_flag {
174         NRS_TBF_FLAG_INVALID    = 0x0000000,
175         NRS_TBF_FLAG_JOBID      = 0x0000001,
176         NRS_TBF_FLAG_NID        = 0x0000002,
177         NRS_TBF_FLAG_OPCODE     = 0x0000004,
178         NRS_TBF_FLAG_GENERIC    = 0x0000008,
179 };
180
181 struct nrs_tbf_type {
182         const char              *ntt_name;
183         enum nrs_tbf_flag        ntt_flag;
184         struct nrs_tbf_ops      *ntt_ops;
185 };
186
187 struct nrs_tbf_bucket {
188         /**
189          * LRU list, updated on each access to client. Protected by
190          * bucket lock of nrs_tbf_head::th_cli_hash.
191          */
192         struct list_head        ntb_lru;
193 };
194
195 /**
196  * Private data structure for the TBF policy
197  */
198 struct nrs_tbf_head {
199         /**
200          * Resource object for policy instance.
201          */
202         struct ptlrpc_nrs_resource       th_res;
203         /**
204          * List of rules.
205          */
206         struct list_head                 th_list;
207         /**
208          * Lock to protect the list of rules.
209          */
210         spinlock_t                       th_rule_lock;
211         /**
212          * Generation of rules.
213          */
214         atomic_t                         th_rule_sequence;
215         /**
216          * Default rule.
217          */
218         struct nrs_tbf_rule             *th_rule;
219         /**
220          * Timer for next token.
221          */
222         struct hrtimer                   th_timer;
223         /**
224          * Deadline of the timer.
225          */
226         __u64                            th_deadline;
227         /**
228          * Sequence of requests.
229          */
230         __u64                            th_sequence;
231         /**
232          * Heap of queues.
233          */
234         struct cfs_binheap              *th_binheap;
235         /**
236          * Hash of clients.
237          */
238         struct cfs_hash                 *th_cli_hash;
239         /**
240          * Type of TBF policy.
241          */
242         char                             th_type[NRS_TBF_TYPE_MAX_LEN + 1];
243         /**
244          * Rule operations.
245          */
246         struct nrs_tbf_ops              *th_ops;
247         /**
248          * Flag of type.
249          */
250         __u32                            th_type_flag;
251         /**
252          * Index of bucket on hash table while purging.
253          */
254         int                              th_purge_start;
255 };
256
257 enum nrs_tbf_cmd_type {
258         NRS_CTL_TBF_START_RULE = 0,
259         NRS_CTL_TBF_STOP_RULE,
260         NRS_CTL_TBF_CHANGE_RULE,
261 };
262
263 struct nrs_tbf_cmd {
264         enum nrs_tbf_cmd_type                    tc_cmd;
265         char                                    *tc_name;
266         union {
267                 struct nrs_tbf_cmd_start {
268                         __u64                    ts_rpc_rate;
269                         struct list_head         ts_nids;
270                         char                    *ts_nids_str;
271                         struct list_head         ts_jobids;
272                         char                    *ts_jobids_str;
273                         struct cfs_bitmap       *ts_opcodes;
274                         char                    *ts_opcodes_str;
275                         struct list_head         ts_conds;
276                         char                    *ts_conds_str;
277                         __u32                    ts_valid_type;
278                         __u32                    ts_rule_flags;
279                         char                    *ts_next_name;
280                 } tc_start;
281                 struct nrs_tbf_cmd_change {
282                         __u64                    tc_rpc_rate;
283                         char                    *tc_next_name;
284                 } tc_change;
285         } u;
286 };
287
288 enum nrs_tbf_field {
289         NRS_TBF_FIELD_NID,
290         NRS_TBF_FIELD_JOBID,
291         NRS_TBF_FIELD_OPCODE,
292         NRS_TBF_FIELD_MAX
293 };
294
295 struct nrs_tbf_expression {
296         enum nrs_tbf_field       te_field;
297         struct list_head         te_cond;
298         struct cfs_bitmap       *te_opcodes;
299         struct list_head         te_linkage;
300 };
301
302 struct nrs_tbf_conjunction {
303         /**
304          * link to disjunction.
305          */
306         struct list_head tc_linkage;
307         /**
308          * list of logical conjunction
309          */
310         struct list_head tc_expressions;
311 };
312
313 struct nrs_tbf_req {
314         /**
315          * Linkage to queue.
316          */
317         struct list_head        tr_list;
318         /**
319          * Sequence of the request.
320          */
321         __u64                   tr_sequence;
322 };
323
324 /**
325  * TBF policy operations.
326  */
327 enum nrs_ctl_tbf {
328         /**
329          * Read the the data of a TBF policy.
330          */
331         NRS_CTL_TBF_RD_RULE = PTLRPC_NRS_CTL_1ST_POL_SPEC,
332         /**
333          * Write the the data of a TBF policy.
334          */
335         NRS_CTL_TBF_WR_RULE,
336         /**
337          * Read the TBF policy type preset by proc entry "nrs_policies".
338          */
339         NRS_CTL_TBF_RD_TYPE_FLAG,
340 };
341
342 /** @} tbf */
343 #endif