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