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