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