Whamcloud - gitweb
LU-9679 nrs: remove ts_opcodes bitmap.
[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         u32                              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 cfs_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         struct cfs_bitmap               *tr_opcodes;
165         /** Opcode list string of the rule.*/
166         char                            *tr_opcodes_str;
167         /** Condition list of the rule.*/
168         struct list_head                tr_conds;
169         /** Generic condition string of the rule. */
170         char                            *tr_conds_str;
171         /** RPC/s limit. */
172         u32                              tr_rpc_rate;
173         /** Time to wait for next token. */
174         u64                              tr_nsecs_per_rpc;
175         /** Token bucket depth. */
176         __u64                            tr_depth;
177         /** Lock to protect the list of clients. */
178         spinlock_t                       tr_rule_lock;
179         /** List of client. */
180         struct list_head                 tr_cli_list;
181         /** Flags of the rule. */
182         enum nrs_rule_flags              tr_flags;
183         /** Usage Reference count taken on the rule. */
184         atomic_t                         tr_ref;
185         /** Generation of the rule. */
186         __u64                            tr_generation;
187 };
188
189 struct nrs_tbf_ops {
190         char *o_name;
191         int (*o_startup)(struct ptlrpc_nrs_policy *, struct nrs_tbf_head *);
192         struct nrs_tbf_client *(*o_cli_find)(struct nrs_tbf_head *,
193                                              struct ptlrpc_request *);
194         struct nrs_tbf_client *(*o_cli_findadd)(struct nrs_tbf_head *,
195                                                 struct nrs_tbf_client *);
196         void (*o_cli_put)(struct nrs_tbf_head *, struct nrs_tbf_client *);
197         void (*o_cli_init)(struct nrs_tbf_client *, struct ptlrpc_request *);
198         int (*o_rule_init)(struct ptlrpc_nrs_policy *,
199                            struct nrs_tbf_rule *,
200                            struct nrs_tbf_cmd *);
201         int (*o_rule_dump)(struct nrs_tbf_rule *, struct seq_file *);
202         int (*o_rule_match)(struct nrs_tbf_rule *,
203                             struct nrs_tbf_client *);
204         void (*o_rule_fini)(struct nrs_tbf_rule *);
205 };
206
207 #define NRS_TBF_TYPE_JOBID      "jobid"
208 #define NRS_TBF_TYPE_NID        "nid"
209 #define NRS_TBF_TYPE_OPCODE     "opcode"
210 #define NRS_TBF_TYPE_GENERIC    "generic"
211 #define NRS_TBF_TYPE_UID        "uid"
212 #define NRS_TBF_TYPE_GID        "gid"
213 #define NRS_TBF_TYPE_MAX_LEN    20
214
215 struct nrs_tbf_type {
216         const char              *ntt_name;
217         enum nrs_tbf_flag        ntt_flag;
218         struct nrs_tbf_ops      *ntt_ops;
219 };
220
221 struct nrs_tbf_bucket {
222         /**
223          * LRU list, updated on each access to client. Protected by
224          * bucket lock of nrs_tbf_head::th_cli_hash.
225          */
226         struct list_head        ntb_lru;
227 };
228
229 /**
230  * Private data structure for the TBF policy
231  */
232 struct nrs_tbf_head {
233         /**
234          * Resource object for policy instance.
235          */
236         struct ptlrpc_nrs_resource       th_res;
237         /**
238          * List of rules.
239          */
240         struct list_head                 th_list;
241         /**
242          * Lock to protect the list of rules.
243          */
244         spinlock_t                       th_rule_lock;
245         /**
246          * Generation of rules.
247          */
248         atomic_t                         th_rule_sequence;
249         /**
250          * Default rule.
251          */
252         struct nrs_tbf_rule             *th_rule;
253         /**
254          * Timer for next token.
255          */
256         struct hrtimer                   th_timer;
257         /**
258          * Deadline of the timer.
259          */
260         __u64                            th_deadline;
261         /**
262          * Sequence of requests.
263          */
264         __u64                            th_sequence;
265         /**
266          * Heap of queues.
267          */
268         struct cfs_binheap              *th_binheap;
269         /**
270          * Hash of clients.
271          */
272         struct cfs_hash                 *th_cli_hash;
273         /**
274          * Type of TBF policy.
275          */
276         char                             th_type[NRS_TBF_TYPE_MAX_LEN + 1];
277         /**
278          * Rule operations.
279          */
280         struct nrs_tbf_ops              *th_ops;
281         /**
282          * Flag of type.
283          */
284         __u32                            th_type_flag;
285         /**
286          * Index of bucket on hash table while purging.
287          */
288         int                              th_purge_start;
289 };
290
291 enum nrs_tbf_cmd_type {
292         NRS_CTL_TBF_START_RULE = 0,
293         NRS_CTL_TBF_STOP_RULE,
294         NRS_CTL_TBF_CHANGE_RULE,
295 };
296
297 struct nrs_tbf_cmd {
298         enum nrs_tbf_cmd_type                    tc_cmd;
299         char                                    *tc_name;
300         union {
301                 struct nrs_tbf_cmd_start {
302                         __u64                    ts_rpc_rate;
303                         struct list_head         ts_nids;
304                         char                    *ts_nids_str;
305                         struct list_head         ts_jobids;
306                         char                    *ts_jobids_str;
307                         struct list_head         ts_ids;
308                         char                    *ts_ids_str;
309                         char                    *ts_opcodes_str;
310                         struct list_head         ts_conds;
311                         char                    *ts_conds_str;
312                         __u32                    ts_valid_type;
313                         enum nrs_rule_flags      ts_rule_flags;
314                         char                    *ts_next_name;
315                 } tc_start;
316                 struct nrs_tbf_cmd_change {
317                         __u64                    tc_rpc_rate;
318                         char                    *tc_next_name;
319                 } tc_change;
320         } u;
321 };
322
323 enum nrs_tbf_field {
324         NRS_TBF_FIELD_NID,
325         NRS_TBF_FIELD_JOBID,
326         NRS_TBF_FIELD_OPCODE,
327         NRS_TBF_FIELD_UID,
328         NRS_TBF_FIELD_GID,
329         NRS_TBF_FIELD_MAX
330 };
331
332 struct nrs_tbf_expression {
333         enum nrs_tbf_field       te_field;
334         struct list_head         te_cond;
335         struct cfs_bitmap       *te_opcodes;
336         struct list_head         te_linkage;
337 };
338
339 struct nrs_tbf_conjunction {
340         /**
341          * link to disjunction.
342          */
343         struct list_head tc_linkage;
344         /**
345          * list of logical conjunction
346          */
347         struct list_head tc_expressions;
348 };
349
350 struct nrs_tbf_req {
351         /**
352          * Linkage to queue.
353          */
354         struct list_head        tr_list;
355         /**
356          * Sequence of the request.
357          */
358         __u64                   tr_sequence;
359 };
360
361 /**
362  * TBF policy operations.
363  */
364 enum nrs_ctl_tbf {
365         /**
366          * Read the the data of a TBF policy.
367          */
368         NRS_CTL_TBF_RD_RULE = PTLRPC_NRS_CTL_1ST_POL_SPEC,
369         /**
370          * Write the the data of a TBF policy.
371          */
372         NRS_CTL_TBF_WR_RULE,
373         /**
374          * Read the TBF policy type preset by proc entry "nrs_policies".
375          */
376         NRS_CTL_TBF_RD_TYPE_FLAG,
377 };
378
379 /** @} tbf */
380 #endif