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