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