Whamcloud - gitweb
LU-17744 ldiskfs: mballoc stats fixes
[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 MAX_U32_STR_LEN 10
55 #define NRS_TBF_KEY_LEN (LNET_NIDSTR_SIZE + LUSTRE_JOBID_SIZE + \
56                          MAX_U32_STR_LEN + MAX_U32_STR_LEN + 3 + 2)
57
58 enum nrs_tbf_flag {
59         NRS_TBF_FLAG_INVALID    = 0x0000000,
60         NRS_TBF_FLAG_JOBID      = 0x0000001,
61         NRS_TBF_FLAG_NID        = 0x0000002,
62         NRS_TBF_FLAG_OPCODE     = 0x0000004,
63         NRS_TBF_FLAG_GENERIC    = 0x0000008,
64         NRS_TBF_FLAG_UID        = 0x0000010,
65         NRS_TBF_FLAG_GID        = 0x0000020,
66 };
67
68 struct tbf_id {
69         enum nrs_tbf_flag       ti_type;
70         u32                     ti_uid;
71         u32                     ti_gid;
72 };
73
74 struct nrs_tbf_id {
75         struct tbf_id           nti_id;
76         struct list_head        nti_linkage;
77 };
78
79 struct nrs_tbf_client {
80         /** Resource object for policy instance. */
81         struct ptlrpc_nrs_resource       tc_res;
82         /** Node in the hash table. */
83         struct hlist_node                tc_hnode;
84         /** NID of the client. */
85         lnet_nid_t                       tc_nid;
86         /** Jobid of the client. */
87         char                             tc_jobid[LUSTRE_JOBID_SIZE];
88         /** opcode of the client. */
89         __u32                            tc_opcode;
90         /** gid or uid of the client. */
91         struct tbf_id                   tc_id;
92         /** Hash key of the client. */
93         char                             tc_key[NRS_TBF_KEY_LEN];
94         /** Reference number of the client. */
95         atomic_t                         tc_ref;
96         /** Lock to protect rule and linkage. */
97         spinlock_t                       tc_rule_lock;
98         /** Linkage to rule. */
99         struct list_head                 tc_linkage;
100         /** Pointer to rule. */
101         struct nrs_tbf_rule             *tc_rule;
102         /** Generation of the rule matched. */
103         __u64                            tc_rule_generation;
104         /** Limit of RPC rate. */
105         __u64                            tc_rpc_rate;
106         /** Time to wait for next token. */
107         __u64                            tc_nsecs;
108         /** RPC token number. */
109         __u64                            tc_ntoken;
110         /** Token bucket depth. */
111         __u64                            tc_depth;
112         /** Time check-point. */
113         __u64                            tc_check_time;
114         /** Deadline of a class */
115         __u64                            tc_deadline;
116         /**
117          * Time residue: the remainder of elapsed time
118          * divided by nsecs when dequeue a request.
119          */
120         __u64                            tc_nsecs_resid;
121         /** List of queued requests. */
122         struct list_head                 tc_list;
123         /** Node in binary heap. */
124         struct binheap_node              tc_node;
125         /** Whether the client is in heap. */
126         bool                             tc_in_heap;
127         /** Sequence of the newest rule. */
128         __u32                            tc_rule_sequence;
129         /**
130          * Linkage into LRU list. Protected bucket lock of
131          * nrs_tbf_head::th_cli_hash.
132          */
133         struct list_head                 tc_lru;
134 };
135
136 #define MAX_TBF_NAME (16)
137
138 enum nrs_rule_flags {
139         NTRS_STOPPING   = 0x00000001,
140         NTRS_DEFAULT    = 0x00000002,
141         NTRS_REALTIME   = 0x00000004,
142 };
143
144 struct nrs_tbf_rule {
145         /** Name of the rule. */
146         char                             tr_name[MAX_TBF_NAME];
147         /** Head belongs to. */
148         struct nrs_tbf_head             *tr_head;
149         /** Likage to head. */
150         struct list_head                 tr_linkage;
151         /** Nid list of the rule. */
152         struct list_head                 tr_nids;
153         /** Nid list string of the rule.*/
154         char                            *tr_nids_str;
155         /** Jobid list of the rule. */
156         struct list_head                 tr_jobids;
157         /** Jobid list string of the rule.*/
158         char                            *tr_jobids_str;
159         /** uid/gid list of the rule. */
160         struct list_head                tr_ids;
161         /** uid/gid list string of the rule. */
162         char                            *tr_ids_str;
163         /** Opcode bitmap of the rule. */
164         unsigned long                   *tr_opcodes;
165         u32                             tr_opcodes_cnt;
166         /** Opcode list string of the rule.*/
167         char                            *tr_opcodes_str;
168         /** Condition list of the rule.*/
169         struct list_head                tr_conds;
170         /** Generic condition string of the rule. */
171         char                            *tr_conds_str;
172         /** RPC/s limit. */
173         __u64                            tr_rpc_rate;
174         /** Time to wait for next token. */
175         u64                              tr_nsecs_per_rpc;
176         /** Token bucket depth. */
177         __u64                            tr_depth;
178         /** Lock to protect the list of clients. */
179         spinlock_t                       tr_rule_lock;
180         /** List of client. */
181         struct list_head                 tr_cli_list;
182         /** Flags of the rule. */
183         enum nrs_rule_flags              tr_flags;
184         /** Usage Reference count taken on the rule. */
185         atomic_t                         tr_ref;
186         /** Generation of the rule. */
187         __u64                            tr_generation;
188 };
189
190 struct nrs_tbf_ops {
191         char *o_name;
192         int (*o_startup)(struct ptlrpc_nrs_policy *, struct nrs_tbf_head *);
193         struct nrs_tbf_client *(*o_cli_find)(struct nrs_tbf_head *,
194                                              struct ptlrpc_request *);
195         struct nrs_tbf_client *(*o_cli_findadd)(struct nrs_tbf_head *,
196                                                 struct nrs_tbf_client *);
197         void (*o_cli_put)(struct nrs_tbf_head *, struct nrs_tbf_client *);
198         void (*o_cli_init)(struct nrs_tbf_client *, struct ptlrpc_request *);
199         int (*o_rule_init)(struct ptlrpc_nrs_policy *,
200                            struct nrs_tbf_rule *,
201                            struct nrs_tbf_cmd *);
202         int (*o_rule_dump)(struct nrs_tbf_rule *, struct seq_file *);
203         int (*o_rule_match)(struct nrs_tbf_rule *,
204                             struct nrs_tbf_client *);
205         void (*o_rule_fini)(struct nrs_tbf_rule *);
206 };
207
208 #define NRS_TBF_TYPE_JOBID      "jobid"
209 #define NRS_TBF_TYPE_NID        "nid"
210 #define NRS_TBF_TYPE_OPCODE     "opcode"
211 #define NRS_TBF_TYPE_GENERIC    "generic"
212 #define NRS_TBF_TYPE_UID        "uid"
213 #define NRS_TBF_TYPE_GID        "gid"
214 #define NRS_TBF_TYPE_MAX_LEN    20
215
216 struct nrs_tbf_type {
217         const char              *ntt_name;
218         enum nrs_tbf_flag        ntt_flag;
219         struct nrs_tbf_ops      *ntt_ops;
220 };
221
222 struct nrs_tbf_bucket {
223         /**
224          * LRU list, updated on each access to client. Protected by
225          * bucket lock of nrs_tbf_head::th_cli_hash.
226          */
227         struct list_head        ntb_lru;
228 };
229
230 /**
231  * Private data structure for the TBF policy
232  */
233 struct nrs_tbf_head {
234         /**
235          * Resource object for policy instance.
236          */
237         struct ptlrpc_nrs_resource       th_res;
238         /**
239          * List of rules.
240          */
241         struct list_head                 th_list;
242         /**
243          * Lock to protect the list of rules.
244          */
245         spinlock_t                       th_rule_lock;
246         /**
247          * Generation of rules.
248          */
249         atomic_t                         th_rule_sequence;
250         /**
251          * Default rule.
252          */
253         struct nrs_tbf_rule             *th_rule;
254         /**
255          * Timer for next token.
256          */
257         struct hrtimer                   th_timer;
258         /**
259          * Deadline of the timer.
260          */
261         __u64                            th_deadline;
262         /**
263          * Sequence of requests.
264          */
265         __u64                            th_sequence;
266         /**
267          * Heap of queues.
268          */
269         struct binheap          *th_binheap;
270         /**
271          * Hash of clients.
272          */
273         struct cfs_hash                 *th_cli_hash;
274         /**
275          * Type of TBF policy.
276          */
277         char                             th_type[NRS_TBF_TYPE_MAX_LEN + 1];
278         /**
279          * Rule operations.
280          */
281         struct nrs_tbf_ops              *th_ops;
282         /**
283          * Flag of type.
284          */
285         __u32                            th_type_flag;
286         /**
287          * Index of bucket on hash table while purging.
288          */
289         int                              th_purge_start;
290 };
291
292 enum nrs_tbf_cmd_type {
293         NRS_CTL_TBF_START_RULE = 0,
294         NRS_CTL_TBF_STOP_RULE,
295         NRS_CTL_TBF_CHANGE_RULE,
296 };
297
298 struct nrs_tbf_cmd {
299         enum nrs_tbf_cmd_type                    tc_cmd;
300         char                                    *tc_name;
301         union {
302                 struct nrs_tbf_cmd_start {
303                         __u64                    ts_rpc_rate;
304                         struct list_head         ts_nids;
305                         char                    *ts_nids_str;
306                         struct list_head         ts_jobids;
307                         char                    *ts_jobids_str;
308                         struct list_head         ts_ids;
309                         char                    *ts_ids_str;
310                         char                    *ts_opcodes_str;
311                         struct list_head         ts_conds;
312                         char                    *ts_conds_str;
313                         __u32                    ts_valid_type;
314                         enum nrs_rule_flags      ts_rule_flags;
315                         char                    *ts_next_name;
316                 } tc_start;
317                 struct nrs_tbf_cmd_change {
318                         __u64                    tc_rpc_rate;
319                         char                    *tc_next_name;
320                 } tc_change;
321         } u;
322 };
323
324 enum nrs_tbf_field {
325         NRS_TBF_FIELD_NID,
326         NRS_TBF_FIELD_JOBID,
327         NRS_TBF_FIELD_OPCODE,
328         NRS_TBF_FIELD_UID,
329         NRS_TBF_FIELD_GID,
330         NRS_TBF_FIELD_MAX
331 };
332
333 struct nrs_tbf_expression {
334         enum nrs_tbf_field       te_field;
335         struct list_head         te_cond;
336         unsigned long           *te_opcodes;
337         u32                      te_opcodes_cnt;
338         struct list_head         te_linkage;
339 };
340
341 struct nrs_tbf_conjunction {
342         /**
343          * link to disjunction.
344          */
345         struct list_head tc_linkage;
346         /**
347          * list of logical conjunction
348          */
349         struct list_head tc_expressions;
350 };
351
352 struct nrs_tbf_req {
353         /**
354          * Linkage to queue.
355          */
356         struct list_head        tr_list;
357         /**
358          * Sequence of the request.
359          */
360         __u64                   tr_sequence;
361 };
362
363 /**
364  * TBF policy operations.
365  */
366 enum nrs_ctl_tbf {
367         /**
368          * Read the the data of a TBF policy.
369          */
370         NRS_CTL_TBF_RD_RULE = PTLRPC_NRS_CTL_1ST_POL_SPEC,
371         /**
372          * Write the the data of a TBF policy.
373          */
374         NRS_CTL_TBF_WR_RULE,
375         /**
376          * Read the TBF policy type preset by proc entry "nrs_policies".
377          */
378         NRS_CTL_TBF_RD_TYPE_FLAG,
379 };
380
381 /** @} tbf */
382 #endif