Whamcloud - gitweb
b5d00d318b8cf237b853b2ac12480f457dbc68ad
[fs/lustre-release.git] / lustre / ptlrpc / nrs_tbf.c
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.  A copy is
14  * included in the COPYING file that accompanied this code.
15
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (C) 2013 DataDirect Networks, Inc.
24  *
25  * Copyright (c) 2014, Intel Corporation.
26  */
27 /*
28  * lustre/ptlrpc/nrs_tbf.c
29  *
30  * Network Request Scheduler (NRS) Token Bucket Filter(TBF) policy
31  *
32  */
33
34 #ifdef HAVE_SERVER_SUPPORT
35
36 /**
37  * \addtogoup nrs
38  * @{
39  */
40
41 #define DEBUG_SUBSYSTEM S_RPC
42 #include <obd_support.h>
43 #include <obd_class.h>
44 #include <libcfs/libcfs.h>
45 #include "ptlrpc_internal.h"
46
47 /**
48  * \name tbf
49  *
50  * Token Bucket Filter over client NIDs
51  *
52  * @{
53  */
54
55 #define NRS_POL_NAME_TBF        "tbf"
56
57 static int tbf_jobid_cache_size = 8192;
58 CFS_MODULE_PARM(tbf_jobid_cache_size, "i", int, 0644,
59                 "The size of jobid cache");
60
61 static int tbf_rate = 10000;
62 CFS_MODULE_PARM(tbf_rate, "i", int, 0644,
63                 "Default rate limit in RPCs/s");
64
65 static int tbf_depth = 3;
66 CFS_MODULE_PARM(tbf_depth, "i", int, 0644,
67                 "How many tokens that a client can save up");
68
69 static enum hrtimer_restart nrs_tbf_timer_cb(struct hrtimer *timer)
70 {
71         struct nrs_tbf_head *head = container_of(timer, struct nrs_tbf_head,
72                                                  th_timer);
73         struct ptlrpc_nrs   *nrs = head->th_res.res_policy->pol_nrs;
74         struct ptlrpc_service_part *svcpt = nrs->nrs_svcpt;
75
76         spin_lock(&nrs->nrs_lock);
77         nrs->nrs_throttling = 0;
78         spin_unlock(&nrs->nrs_lock);
79         wake_up(&svcpt->scp_waitq);
80
81         return HRTIMER_NORESTART;
82 }
83
84 #define NRS_TBF_DEFAULT_RULE "default"
85
86 static void nrs_tbf_rule_fini(struct nrs_tbf_rule *rule)
87 {
88         LASSERT(atomic_read(&rule->tr_ref) == 0);
89         LASSERT(list_empty(&rule->tr_cli_list));
90         LASSERT(list_empty(&rule->tr_linkage));
91
92         rule->tr_head->th_ops->o_rule_fini(rule);
93         OBD_FREE_PTR(rule);
94 }
95
96 /**
97  * Decreases the rule's usage reference count, and stops the rule in case it
98  * was already stopping and have no more outstanding usage references (which
99  * indicates it has no more queued or started requests, and can be safely
100  * stopped).
101  */
102 static void nrs_tbf_rule_put(struct nrs_tbf_rule *rule)
103 {
104         if (atomic_dec_and_test(&rule->tr_ref))
105                 nrs_tbf_rule_fini(rule);
106 }
107
108 /**
109  * Increases the rule's usage reference count.
110  */
111 static inline void nrs_tbf_rule_get(struct nrs_tbf_rule *rule)
112 {
113         atomic_inc(&rule->tr_ref);
114 }
115
116 static void
117 nrs_tbf_cli_rule_put(struct nrs_tbf_client *cli)
118 {
119         LASSERT(!list_empty(&cli->tc_linkage));
120         LASSERT(cli->tc_rule);
121         list_del_init(&cli->tc_linkage);
122         nrs_tbf_rule_put(cli->tc_rule);
123         cli->tc_rule = NULL;
124 }
125
126 static void
127 nrs_tbf_cli_reset_value(struct nrs_tbf_head *head,
128                         struct nrs_tbf_client *cli)
129
130 {
131         struct nrs_tbf_rule *rule = cli->tc_rule;
132
133         cli->tc_rpc_rate = rule->tr_rpc_rate;
134         cli->tc_nsecs = rule->tr_nsecs;
135         cli->tc_depth = rule->tr_depth;
136         cli->tc_ntoken = rule->tr_depth;
137         cli->tc_check_time = ktime_to_ns(ktime_get());
138         cli->tc_rule_sequence = atomic_read(&head->th_rule_sequence);
139         cli->tc_rule_generation = rule->tr_generation;
140
141         if (cli->tc_in_heap)
142                 cfs_binheap_relocate(head->th_binheap,
143                                      &cli->tc_node);
144 }
145
146 static void
147 nrs_tbf_cli_reset(struct nrs_tbf_head *head,
148                   struct nrs_tbf_rule *rule,
149                   struct nrs_tbf_client *cli)
150 {
151         if (!list_empty(&cli->tc_linkage)) {
152                 LASSERT(rule != cli->tc_rule);
153                 nrs_tbf_cli_rule_put(cli);
154         }
155         LASSERT(cli->tc_rule == NULL);
156         LASSERT(list_empty(&cli->tc_linkage));
157         /* Rule's ref is added before called */
158         cli->tc_rule = rule;
159         list_add_tail(&cli->tc_linkage, &rule->tr_cli_list);
160         nrs_tbf_cli_reset_value(head, cli);
161 }
162
163 static int
164 nrs_tbf_rule_dump(struct nrs_tbf_rule *rule, struct seq_file *m)
165 {
166         return rule->tr_head->th_ops->o_rule_dump(rule, m);
167 }
168
169 static int
170 nrs_tbf_rule_dump_all(struct nrs_tbf_head *head, struct seq_file *m)
171 {
172         struct nrs_tbf_rule *rule;
173         int rc = 0;
174
175         LASSERT(head != NULL);
176         spin_lock(&head->th_rule_lock);
177         /* List the rules from newest to oldest */
178         list_for_each_entry(rule, &head->th_list, tr_linkage) {
179                 LASSERT((rule->tr_flags & NTRS_STOPPING) == 0);
180                 rc = nrs_tbf_rule_dump(rule, m);
181                 if (rc) {
182                         rc = -ENOSPC;
183                         break;
184                 }
185         }
186         spin_unlock(&head->th_rule_lock);
187
188         return rc;
189 }
190
191 static struct nrs_tbf_rule *
192 nrs_tbf_rule_find_nolock(struct nrs_tbf_head *head,
193                          const char *name)
194 {
195         struct nrs_tbf_rule *rule;
196
197         LASSERT(head != NULL);
198         list_for_each_entry(rule, &head->th_list, tr_linkage) {
199                 LASSERT((rule->tr_flags & NTRS_STOPPING) == 0);
200                 if (strcmp(rule->tr_name, name) == 0) {
201                         nrs_tbf_rule_get(rule);
202                         return rule;
203                 }
204         }
205         return NULL;
206 }
207
208 static struct nrs_tbf_rule *
209 nrs_tbf_rule_find(struct nrs_tbf_head *head,
210                   const char *name)
211 {
212         struct nrs_tbf_rule *rule;
213
214         LASSERT(head != NULL);
215         spin_lock(&head->th_rule_lock);
216         rule = nrs_tbf_rule_find_nolock(head, name);
217         spin_unlock(&head->th_rule_lock);
218         return rule;
219 }
220
221 static struct nrs_tbf_rule *
222 nrs_tbf_rule_match(struct nrs_tbf_head *head,
223                    struct nrs_tbf_client *cli)
224 {
225         struct nrs_tbf_rule *rule = NULL;
226         struct nrs_tbf_rule *tmp_rule;
227
228         spin_lock(&head->th_rule_lock);
229         /* Match the newest rule in the list */
230         list_for_each_entry(tmp_rule, &head->th_list, tr_linkage) {
231                 LASSERT((tmp_rule->tr_flags & NTRS_STOPPING) == 0);
232                 if (head->th_ops->o_rule_match(tmp_rule, cli)) {
233                         rule = tmp_rule;
234                         break;
235                 }
236         }
237
238         if (rule == NULL)
239                 rule = head->th_rule;
240
241         nrs_tbf_rule_get(rule);
242         spin_unlock(&head->th_rule_lock);
243         return rule;
244 }
245
246 static void
247 nrs_tbf_cli_init(struct nrs_tbf_head *head,
248                  struct nrs_tbf_client *cli,
249                  struct ptlrpc_request *req)
250 {
251         struct nrs_tbf_rule *rule;
252
253         cli->tc_in_heap = false;
254         head->th_ops->o_cli_init(cli, req);
255         INIT_LIST_HEAD(&cli->tc_list);
256         INIT_LIST_HEAD(&cli->tc_linkage);
257         atomic_set(&cli->tc_ref, 1);
258         rule = nrs_tbf_rule_match(head, cli);
259         nrs_tbf_cli_reset(head, rule, cli);
260 }
261
262 static void
263 nrs_tbf_cli_fini(struct nrs_tbf_client *cli)
264 {
265         LASSERT(list_empty(&cli->tc_list));
266         LASSERT(!cli->tc_in_heap);
267         LASSERT(atomic_read(&cli->tc_ref) == 0);
268         nrs_tbf_cli_rule_put(cli);
269         OBD_FREE_PTR(cli);
270 }
271
272 static int
273 nrs_tbf_rule_start(struct ptlrpc_nrs_policy *policy,
274                    struct nrs_tbf_head *head,
275                    struct nrs_tbf_cmd *start)
276 {
277         struct nrs_tbf_rule *rule, *tmp_rule;
278         int rc;
279
280         rule = nrs_tbf_rule_find(head, start->tc_name);
281         if (rule) {
282                 nrs_tbf_rule_put(rule);
283                 return -EEXIST;
284         }
285
286         OBD_CPT_ALLOC_PTR(rule, nrs_pol2cptab(policy), nrs_pol2cptid(policy));
287         if (rule == NULL)
288                 return -ENOMEM;
289
290         memcpy(rule->tr_name, start->tc_name, strlen(start->tc_name));
291         rule->tr_rpc_rate = start->tc_rpc_rate;
292         rule->tr_nsecs = NSEC_PER_SEC / rule->tr_rpc_rate;
293         rule->tr_depth = tbf_depth;
294         atomic_set(&rule->tr_ref, 1);
295         INIT_LIST_HEAD(&rule->tr_cli_list);
296         INIT_LIST_HEAD(&rule->tr_nids);
297
298         rc = head->th_ops->o_rule_init(policy, rule, start);
299         if (rc) {
300                 OBD_FREE_PTR(rule);
301                 return rc;
302         }
303
304         /* Add as the newest rule */
305         spin_lock(&head->th_rule_lock);
306         tmp_rule = nrs_tbf_rule_find_nolock(head, start->tc_name);
307         if (tmp_rule) {
308                 spin_unlock(&head->th_rule_lock);
309                 nrs_tbf_rule_put(tmp_rule);
310                 nrs_tbf_rule_put(rule);
311                 return -EEXIST;
312         }
313         list_add(&rule->tr_linkage, &head->th_list);
314         rule->tr_head = head;
315         spin_unlock(&head->th_rule_lock);
316         atomic_inc(&head->th_rule_sequence);
317         if (start->tc_rule_flags & NTRS_DEFAULT) {
318                 rule->tr_flags |= NTRS_DEFAULT;
319                 LASSERT(head->th_rule == NULL);
320                 head->th_rule = rule;
321         }
322
323         return 0;
324 }
325
326 static int
327 nrs_tbf_rule_change(struct ptlrpc_nrs_policy *policy,
328                     struct nrs_tbf_head *head,
329                     struct nrs_tbf_cmd *change)
330 {
331         struct nrs_tbf_rule *rule;
332
333         assert_spin_locked(&policy->pol_nrs->nrs_lock);
334
335         rule = nrs_tbf_rule_find(head, change->tc_name);
336         if (rule == NULL)
337                 return -ENOENT;
338
339         rule->tr_rpc_rate = change->tc_rpc_rate;
340         rule->tr_nsecs = NSEC_PER_SEC / rule->tr_rpc_rate;
341         rule->tr_generation++;
342         nrs_tbf_rule_put(rule);
343
344         return 0;
345 }
346
347 static int
348 nrs_tbf_rule_stop(struct ptlrpc_nrs_policy *policy,
349                   struct nrs_tbf_head *head,
350                   struct nrs_tbf_cmd *stop)
351 {
352         struct nrs_tbf_rule *rule;
353
354         assert_spin_locked(&policy->pol_nrs->nrs_lock);
355
356         if (strcmp(stop->tc_name, NRS_TBF_DEFAULT_RULE) == 0)
357                 return -EPERM;
358
359         rule = nrs_tbf_rule_find(head, stop->tc_name);
360         if (rule == NULL)
361                 return -ENOENT;
362
363         list_del_init(&rule->tr_linkage);
364         rule->tr_flags |= NTRS_STOPPING;
365         nrs_tbf_rule_put(rule);
366         nrs_tbf_rule_put(rule);
367
368         return 0;
369 }
370
371 static int
372 nrs_tbf_command(struct ptlrpc_nrs_policy *policy,
373                 struct nrs_tbf_head *head,
374                 struct nrs_tbf_cmd *cmd)
375 {
376         int rc;
377
378         assert_spin_locked(&policy->pol_nrs->nrs_lock);
379
380         switch (cmd->tc_cmd) {
381         case NRS_CTL_TBF_START_RULE:
382                 if (!(cmd->tc_valid_types & head->th_type_flag))
383                         return -EINVAL;
384
385                 spin_unlock(&policy->pol_nrs->nrs_lock);
386                 rc = nrs_tbf_rule_start(policy, head, cmd);
387                 spin_lock(&policy->pol_nrs->nrs_lock);
388                 return rc;
389         case NRS_CTL_TBF_CHANGE_RATE:
390                 rc = nrs_tbf_rule_change(policy, head, cmd);
391                 return rc;
392         case NRS_CTL_TBF_STOP_RULE:
393                 rc = nrs_tbf_rule_stop(policy, head, cmd);
394                 /* Take it as a success, if not exists at all */
395                 return rc == -ENOENT ? 0 : rc;
396         default:
397                 return -EFAULT;
398         }
399 }
400
401 /**
402  * Binary heap predicate.
403  *
404  * \param[in] e1 the first binheap node to compare
405  * \param[in] e2 the second binheap node to compare
406  *
407  * \retval 0 e1 > e2
408  * \retval 1 e1 < e2
409  */
410 static int tbf_cli_compare(cfs_binheap_node_t *e1, cfs_binheap_node_t *e2)
411 {
412         struct nrs_tbf_client *cli1;
413         struct nrs_tbf_client *cli2;
414
415         cli1 = container_of(e1, struct nrs_tbf_client, tc_node);
416         cli2 = container_of(e2, struct nrs_tbf_client, tc_node);
417
418         if (cli1->tc_check_time + cli1->tc_nsecs <
419             cli2->tc_check_time + cli2->tc_nsecs)
420                 return 1;
421         else if (cli1->tc_check_time + cli1->tc_nsecs >
422                  cli2->tc_check_time + cli2->tc_nsecs)
423                 return 0;
424
425         if (cli1->tc_check_time < cli2->tc_check_time)
426                 return 1;
427         else if (cli1->tc_check_time > cli2->tc_check_time)
428                 return 0;
429
430         /* Maybe need more comparasion, e.g. request number in the rules */
431         return 1;
432 }
433
434 /**
435  * TBF binary heap operations
436  */
437 static cfs_binheap_ops_t nrs_tbf_heap_ops = {
438         .hop_enter      = NULL,
439         .hop_exit       = NULL,
440         .hop_compare    = tbf_cli_compare,
441 };
442
443 static unsigned nrs_tbf_jobid_hop_hash(cfs_hash_t *hs, const void *key,
444                                   unsigned mask)
445 {
446         return cfs_hash_djb2_hash(key, strlen(key), mask);
447 }
448
449 static int nrs_tbf_jobid_hop_keycmp(const void *key, struct hlist_node *hnode)
450 {
451         struct nrs_tbf_client *cli = hlist_entry(hnode,
452                                                      struct nrs_tbf_client,
453                                                      tc_hnode);
454
455         return (strcmp(cli->tc_jobid, key) == 0);
456 }
457
458 static void *nrs_tbf_jobid_hop_key(struct hlist_node *hnode)
459 {
460         struct nrs_tbf_client *cli = hlist_entry(hnode,
461                                                      struct nrs_tbf_client,
462                                                      tc_hnode);
463
464         return cli->tc_jobid;
465 }
466
467 static void *nrs_tbf_jobid_hop_object(struct hlist_node *hnode)
468 {
469         return hlist_entry(hnode, struct nrs_tbf_client, tc_hnode);
470 }
471
472 static void nrs_tbf_jobid_hop_get(cfs_hash_t *hs, struct hlist_node *hnode)
473 {
474         struct nrs_tbf_client *cli = hlist_entry(hnode,
475                                                      struct nrs_tbf_client,
476                                                      tc_hnode);
477
478         atomic_inc(&cli->tc_ref);
479 }
480
481 static void nrs_tbf_jobid_hop_put(cfs_hash_t *hs, struct hlist_node *hnode)
482 {
483         struct nrs_tbf_client *cli = hlist_entry(hnode,
484                                                      struct nrs_tbf_client,
485                                                      tc_hnode);
486
487         atomic_dec(&cli->tc_ref);
488 }
489
490 static void nrs_tbf_jobid_hop_exit(cfs_hash_t *hs, struct hlist_node *hnode)
491
492 {
493         struct nrs_tbf_client *cli = hlist_entry(hnode,
494                                                      struct nrs_tbf_client,
495                                                      tc_hnode);
496
497         LASSERT(atomic_read(&cli->tc_ref) == 0);
498         nrs_tbf_cli_fini(cli);
499 }
500
501 static cfs_hash_ops_t nrs_tbf_jobid_hash_ops = {
502         .hs_hash        = nrs_tbf_jobid_hop_hash,
503         .hs_keycmp      = nrs_tbf_jobid_hop_keycmp,
504         .hs_key         = nrs_tbf_jobid_hop_key,
505         .hs_object      = nrs_tbf_jobid_hop_object,
506         .hs_get         = nrs_tbf_jobid_hop_get,
507         .hs_put         = nrs_tbf_jobid_hop_put,
508         .hs_put_locked  = nrs_tbf_jobid_hop_put,
509         .hs_exit        = nrs_tbf_jobid_hop_exit,
510 };
511
512 #define NRS_TBF_JOBID_HASH_FLAGS (CFS_HASH_SPIN_BKTLOCK | \
513                                   CFS_HASH_NO_ITEMREF | \
514                                   CFS_HASH_DEPTH)
515
516 static struct nrs_tbf_client *
517 nrs_tbf_jobid_hash_lookup(cfs_hash_t *hs,
518                           cfs_hash_bd_t *bd,
519                           const char *jobid)
520 {
521         struct hlist_node *hnode;
522         struct nrs_tbf_client *cli;
523
524         /* cfs_hash_bd_peek_locked is a somehow "internal" function
525          * of cfs_hash, it doesn't add refcount on object. */
526         hnode = cfs_hash_bd_peek_locked(hs, bd, (void *)jobid);
527         if (hnode == NULL)
528                 return NULL;
529
530         cfs_hash_get(hs, hnode);
531         cli = container_of0(hnode, struct nrs_tbf_client, tc_hnode);
532         if (!list_empty(&cli->tc_lru))
533                 list_del_init(&cli->tc_lru);
534         return cli;
535 }
536
537 #define NRS_TBF_JOBID_NULL ""
538
539 static struct nrs_tbf_client *
540 nrs_tbf_jobid_cli_find(struct nrs_tbf_head *head,
541                        struct ptlrpc_request *req)
542 {
543         const char              *jobid;
544         struct nrs_tbf_client   *cli;
545         cfs_hash_t              *hs = head->th_cli_hash;
546         cfs_hash_bd_t            bd;
547
548         jobid = lustre_msg_get_jobid(req->rq_reqmsg);
549         if (jobid == NULL)
550                 jobid = NRS_TBF_JOBID_NULL;
551         cfs_hash_bd_get_and_lock(hs, (void *)jobid, &bd, 1);
552         cli = nrs_tbf_jobid_hash_lookup(hs, &bd, jobid);
553         cfs_hash_bd_unlock(hs, &bd, 1);
554
555         return cli;
556 }
557
558 static struct nrs_tbf_client *
559 nrs_tbf_jobid_cli_findadd(struct nrs_tbf_head *head,
560                           struct nrs_tbf_client *cli)
561 {
562         const char              *jobid;
563         struct nrs_tbf_client   *ret;
564         cfs_hash_t              *hs = head->th_cli_hash;
565         cfs_hash_bd_t            bd;
566
567         jobid = cli->tc_jobid;
568         cfs_hash_bd_get_and_lock(hs, (void *)jobid, &bd, 1);
569         ret = nrs_tbf_jobid_hash_lookup(hs, &bd, jobid);
570         if (ret == NULL) {
571                 cfs_hash_bd_add_locked(hs, &bd, &cli->tc_hnode);
572                 ret = cli;
573         }
574         cfs_hash_bd_unlock(hs, &bd, 1);
575
576         return ret;
577 }
578
579 static void
580 nrs_tbf_jobid_cli_put(struct nrs_tbf_head *head,
581                       struct nrs_tbf_client *cli)
582 {
583         cfs_hash_bd_t            bd;
584         cfs_hash_t              *hs = head->th_cli_hash;
585         struct nrs_tbf_bucket   *bkt;
586         int                      hw;
587         struct list_head        zombies;
588
589         INIT_LIST_HEAD(&zombies);
590         cfs_hash_bd_get(hs, &cli->tc_jobid, &bd);
591         bkt = cfs_hash_bd_extra_get(hs, &bd);
592         if (!cfs_hash_bd_dec_and_lock(hs, &bd, &cli->tc_ref))
593                 return;
594         LASSERT(list_empty(&cli->tc_lru));
595         list_add_tail(&cli->tc_lru, &bkt->ntb_lru);
596
597         /*
598          * Check and purge the LRU, there is at least one client in the LRU.
599          */
600         hw = tbf_jobid_cache_size >>
601              (hs->hs_cur_bits - hs->hs_bkt_bits);
602         while (cfs_hash_bd_count_get(&bd) > hw) {
603                 if (unlikely(list_empty(&bkt->ntb_lru)))
604                         break;
605                 cli = list_entry(bkt->ntb_lru.next,
606                                      struct nrs_tbf_client,
607                                      tc_lru);
608                 LASSERT(atomic_read(&cli->tc_ref) == 0);
609                 cfs_hash_bd_del_locked(hs, &bd, &cli->tc_hnode);
610                 list_move(&cli->tc_lru, &zombies);
611         }
612         cfs_hash_bd_unlock(head->th_cli_hash, &bd, 1);
613
614         while (!list_empty(&zombies)) {
615                 cli = container_of0(zombies.next,
616                                     struct nrs_tbf_client, tc_lru);
617                 list_del_init(&cli->tc_lru);
618                 nrs_tbf_cli_fini(cli);
619         }
620 }
621
622 static void
623 nrs_tbf_jobid_cli_init(struct nrs_tbf_client *cli,
624                        struct ptlrpc_request *req)
625 {
626         char *jobid = lustre_msg_get_jobid(req->rq_reqmsg);
627
628         if (jobid == NULL)
629                 jobid = NRS_TBF_JOBID_NULL;
630         LASSERT(strlen(jobid) < LUSTRE_JOBID_SIZE);
631         INIT_LIST_HEAD(&cli->tc_lru);
632         memcpy(cli->tc_jobid, jobid, strlen(jobid));
633 }
634
635 static int nrs_tbf_jobid_hash_order(void)
636 {
637         int bits;
638
639         for (bits = 1; (1 << bits) < tbf_jobid_cache_size; ++bits)
640                 ;
641
642         return bits;
643 }
644
645 #define NRS_TBF_JOBID_BKT_BITS 10
646
647 static int
648 nrs_tbf_jobid_startup(struct ptlrpc_nrs_policy *policy,
649                       struct nrs_tbf_head *head)
650 {
651         struct nrs_tbf_cmd       start;
652         struct nrs_tbf_bucket   *bkt;
653         int                      bits;
654         int                      i;
655         int                      rc;
656         cfs_hash_bd_t            bd;
657
658         bits = nrs_tbf_jobid_hash_order();
659         if (bits < NRS_TBF_JOBID_BKT_BITS)
660                 bits = NRS_TBF_JOBID_BKT_BITS;
661         head->th_cli_hash = cfs_hash_create("nrs_tbf_hash",
662                                             bits,
663                                             bits,
664                                             NRS_TBF_JOBID_BKT_BITS,
665                                             sizeof(*bkt),
666                                             0,
667                                             0,
668                                             &nrs_tbf_jobid_hash_ops,
669                                             NRS_TBF_JOBID_HASH_FLAGS);
670         if (head->th_cli_hash == NULL)
671                 return -ENOMEM;
672
673         cfs_hash_for_each_bucket(head->th_cli_hash, &bd, i) {
674                 bkt = cfs_hash_bd_extra_get(head->th_cli_hash, &bd);
675                 INIT_LIST_HEAD(&bkt->ntb_lru);
676         }
677
678         memset(&start, 0, sizeof(start));
679         start.tc_jobids_str = "*";
680
681         start.tc_rpc_rate = tbf_rate;
682         start.tc_rule_flags = NTRS_DEFAULT;
683         start.tc_name = NRS_TBF_DEFAULT_RULE;
684         INIT_LIST_HEAD(&start.tc_jobids);
685         rc = nrs_tbf_rule_start(policy, head, &start);
686
687         return rc;
688 }
689
690 /**
691  * Frees jobid of \a list.
692  *
693  */
694 static void
695 nrs_tbf_jobid_list_free(struct list_head *jobid_list)
696 {
697         struct nrs_tbf_jobid *jobid, *n;
698
699         list_for_each_entry_safe(jobid, n, jobid_list, tj_linkage) {
700                 OBD_FREE(jobid->tj_id, strlen(jobid->tj_id) + 1);
701                 list_del(&jobid->tj_linkage);
702                 OBD_FREE(jobid, sizeof(struct nrs_tbf_jobid));
703         }
704 }
705
706 static int
707 nrs_tbf_jobid_list_add(const struct cfs_lstr *id, struct list_head *jobid_list)
708 {
709         struct nrs_tbf_jobid *jobid;
710
711         OBD_ALLOC(jobid, sizeof(struct nrs_tbf_jobid));
712         if (jobid == NULL)
713                 return -ENOMEM;
714
715         OBD_ALLOC(jobid->tj_id, id->ls_len + 1);
716         if (jobid->tj_id == NULL) {
717                 OBD_FREE(jobid, sizeof(struct nrs_tbf_jobid));
718                 return -ENOMEM;
719         }
720
721         memcpy(jobid->tj_id, id->ls_str, id->ls_len);
722         list_add_tail(&jobid->tj_linkage, jobid_list);
723         return 0;
724 }
725
726 static int
727 nrs_tbf_jobid_list_match(struct list_head *jobid_list, char *id)
728 {
729         struct nrs_tbf_jobid *jobid;
730
731         list_for_each_entry(jobid, jobid_list, tj_linkage) {
732                 if (strcmp(id, jobid->tj_id) == 0)
733                         return 1;
734         }
735         return 0;
736 }
737
738 static int
739 nrs_tbf_jobid_list_parse(char *str, int len, struct list_head *jobid_list)
740 {
741         struct cfs_lstr src;
742         struct cfs_lstr res;
743         int rc = 0;
744         ENTRY;
745
746         src.ls_str = str;
747         src.ls_len = len;
748         INIT_LIST_HEAD(jobid_list);
749         while (src.ls_str) {
750                 rc = cfs_gettok(&src, ' ', &res);
751                 if (rc == 0) {
752                         rc = -EINVAL;
753                         break;
754                 }
755                 rc = nrs_tbf_jobid_list_add(&res, jobid_list);
756                 if (rc)
757                         break;
758         }
759         if (rc)
760                 nrs_tbf_jobid_list_free(jobid_list);
761         RETURN(rc);
762 }
763
764 static void nrs_tbf_jobid_cmd_fini(struct nrs_tbf_cmd *cmd)
765 {
766         if (!list_empty(&cmd->tc_jobids))
767                 nrs_tbf_jobid_list_free(&cmd->tc_jobids);
768         if (cmd->tc_jobids_str)
769                 OBD_FREE(cmd->tc_jobids_str, strlen(cmd->tc_jobids_str) + 1);
770 }
771
772 static int nrs_tbf_jobid_parse(struct nrs_tbf_cmd *cmd, const char *id)
773 {
774         int rc;
775
776         OBD_ALLOC(cmd->tc_jobids_str, strlen(id) + 1);
777         if (cmd->tc_jobids_str == NULL)
778                 return -ENOMEM;
779
780         memcpy(cmd->tc_jobids_str, id, strlen(id));
781
782         /* parse jobid list */
783         rc = nrs_tbf_jobid_list_parse(cmd->tc_jobids_str,
784                                       strlen(cmd->tc_jobids_str),
785                                       &cmd->tc_jobids);
786         if (rc)
787                 nrs_tbf_jobid_cmd_fini(cmd);
788
789         return rc;
790 }
791
792 static int nrs_tbf_jobid_rule_init(struct ptlrpc_nrs_policy *policy,
793                                    struct nrs_tbf_rule *rule,
794                                    struct nrs_tbf_cmd *start)
795 {
796         int rc = 0;
797
798         LASSERT(start->tc_jobids_str);
799         OBD_ALLOC(rule->tr_jobids_str,
800                   strlen(start->tc_jobids_str) + 1);
801         if (rule->tr_jobids_str == NULL)
802                 return -ENOMEM;
803
804         memcpy(rule->tr_jobids_str,
805                start->tc_jobids_str,
806                strlen(start->tc_jobids_str));
807
808         INIT_LIST_HEAD(&rule->tr_jobids);
809         if (!list_empty(&start->tc_jobids)) {
810                 rc = nrs_tbf_jobid_list_parse(rule->tr_jobids_str,
811                                               strlen(rule->tr_jobids_str),
812                                               &rule->tr_jobids);
813                 if (rc)
814                         CERROR("jobids {%s} illegal\n", rule->tr_jobids_str);
815         }
816         if (rc)
817                 OBD_FREE(rule->tr_jobids_str,
818                          strlen(start->tc_jobids_str) + 1);
819         return rc;
820 }
821
822 static int
823 nrs_tbf_jobid_rule_dump(struct nrs_tbf_rule *rule, struct seq_file *m)
824 {
825         return seq_printf(m, "%s {%s} %llu, ref %d\n", rule->tr_name,
826                           rule->tr_jobids_str, rule->tr_rpc_rate,
827                           atomic_read(&rule->tr_ref) - 1);
828 }
829
830 static int
831 nrs_tbf_jobid_rule_match(struct nrs_tbf_rule *rule,
832                          struct nrs_tbf_client *cli)
833 {
834         return nrs_tbf_jobid_list_match(&rule->tr_jobids, cli->tc_jobid);
835 }
836
837 static void nrs_tbf_jobid_rule_fini(struct nrs_tbf_rule *rule)
838 {
839         if (!list_empty(&rule->tr_jobids))
840                 nrs_tbf_jobid_list_free(&rule->tr_jobids);
841         LASSERT(rule->tr_jobids_str != NULL);
842         OBD_FREE(rule->tr_jobids_str, strlen(rule->tr_jobids_str) + 1);
843 }
844
845 static struct nrs_tbf_ops nrs_tbf_jobid_ops = {
846         .o_name = NRS_TBF_TYPE_JOBID,
847         .o_startup = nrs_tbf_jobid_startup,
848         .o_cli_find = nrs_tbf_jobid_cli_find,
849         .o_cli_findadd = nrs_tbf_jobid_cli_findadd,
850         .o_cli_put = nrs_tbf_jobid_cli_put,
851         .o_cli_init = nrs_tbf_jobid_cli_init,
852         .o_rule_init = nrs_tbf_jobid_rule_init,
853         .o_rule_dump = nrs_tbf_jobid_rule_dump,
854         .o_rule_match = nrs_tbf_jobid_rule_match,
855         .o_rule_fini = nrs_tbf_jobid_rule_fini,
856 };
857
858 /**
859  * libcfs_hash operations for nrs_tbf_net::cn_cli_hash
860  *
861  * This uses ptlrpc_request::rq_peer.nid as its key, in order to hash
862  * nrs_tbf_client objects.
863  */
864 #define NRS_TBF_NID_BKT_BITS    8
865 #define NRS_TBF_NID_BITS        16
866
867 static unsigned nrs_tbf_nid_hop_hash(cfs_hash_t *hs, const void *key,
868                                   unsigned mask)
869 {
870         return cfs_hash_djb2_hash(key, sizeof(lnet_nid_t), mask);
871 }
872
873 static int nrs_tbf_nid_hop_keycmp(const void *key, struct hlist_node *hnode)
874 {
875         lnet_nid_t            *nid = (lnet_nid_t *)key;
876         struct nrs_tbf_client *cli = hlist_entry(hnode,
877                                                      struct nrs_tbf_client,
878                                                      tc_hnode);
879
880         return *nid == cli->tc_nid;
881 }
882
883 static void *nrs_tbf_nid_hop_key(struct hlist_node *hnode)
884 {
885         struct nrs_tbf_client *cli = hlist_entry(hnode,
886                                                      struct nrs_tbf_client,
887                                                      tc_hnode);
888
889         return &cli->tc_nid;
890 }
891
892 static void *nrs_tbf_nid_hop_object(struct hlist_node *hnode)
893 {
894         return hlist_entry(hnode, struct nrs_tbf_client, tc_hnode);
895 }
896
897 static void nrs_tbf_nid_hop_get(cfs_hash_t *hs, struct hlist_node *hnode)
898 {
899         struct nrs_tbf_client *cli = hlist_entry(hnode,
900                                                      struct nrs_tbf_client,
901                                                      tc_hnode);
902
903         atomic_inc(&cli->tc_ref);
904 }
905
906 static void nrs_tbf_nid_hop_put(cfs_hash_t *hs, struct hlist_node *hnode)
907 {
908         struct nrs_tbf_client *cli = hlist_entry(hnode,
909                                                      struct nrs_tbf_client,
910                                                      tc_hnode);
911
912         atomic_dec(&cli->tc_ref);
913 }
914
915 static void nrs_tbf_nid_hop_exit(cfs_hash_t *hs, struct hlist_node *hnode)
916 {
917         struct nrs_tbf_client *cli = hlist_entry(hnode,
918                                                      struct nrs_tbf_client,
919                                                      tc_hnode);
920
921         LASSERTF(atomic_read(&cli->tc_ref) == 0,
922                  "Busy TBF object from client with NID %s, with %d refs\n",
923                  libcfs_nid2str(cli->tc_nid), atomic_read(&cli->tc_ref));
924
925         nrs_tbf_cli_fini(cli);
926 }
927
928 static cfs_hash_ops_t nrs_tbf_nid_hash_ops = {
929         .hs_hash        = nrs_tbf_nid_hop_hash,
930         .hs_keycmp      = nrs_tbf_nid_hop_keycmp,
931         .hs_key         = nrs_tbf_nid_hop_key,
932         .hs_object      = nrs_tbf_nid_hop_object,
933         .hs_get         = nrs_tbf_nid_hop_get,
934         .hs_put         = nrs_tbf_nid_hop_put,
935         .hs_put_locked  = nrs_tbf_nid_hop_put,
936         .hs_exit        = nrs_tbf_nid_hop_exit,
937 };
938
939 static struct nrs_tbf_client *
940 nrs_tbf_nid_cli_find(struct nrs_tbf_head *head,
941                      struct ptlrpc_request *req)
942 {
943         return cfs_hash_lookup(head->th_cli_hash, &req->rq_peer.nid);
944 }
945
946 static struct nrs_tbf_client *
947 nrs_tbf_nid_cli_findadd(struct nrs_tbf_head *head,
948                         struct nrs_tbf_client *cli)
949 {
950         return cfs_hash_findadd_unique(head->th_cli_hash, &cli->tc_nid,
951                                        &cli->tc_hnode);
952 }
953
954 static void
955 nrs_tbf_nid_cli_put(struct nrs_tbf_head *head,
956                       struct nrs_tbf_client *cli)
957 {
958         cfs_hash_put(head->th_cli_hash, &cli->tc_hnode);
959 }
960
961 static int
962 nrs_tbf_nid_startup(struct ptlrpc_nrs_policy *policy,
963                     struct nrs_tbf_head *head)
964 {
965         struct nrs_tbf_cmd      start;
966         int rc;
967
968         head->th_cli_hash = cfs_hash_create("nrs_tbf_hash",
969                                             NRS_TBF_NID_BITS,
970                                             NRS_TBF_NID_BITS,
971                                             NRS_TBF_NID_BKT_BITS, 0,
972                                             CFS_HASH_MIN_THETA,
973                                             CFS_HASH_MAX_THETA,
974                                             &nrs_tbf_nid_hash_ops,
975                                             CFS_HASH_RW_BKTLOCK);
976         if (head->th_cli_hash == NULL)
977                 return -ENOMEM;
978
979         memset(&start, 0, sizeof(start));
980         start.tc_nids_str = "*";
981
982         start.tc_rpc_rate = tbf_rate;
983         start.tc_rule_flags = NTRS_DEFAULT;
984         start.tc_name = NRS_TBF_DEFAULT_RULE;
985         INIT_LIST_HEAD(&start.tc_nids);
986         rc = nrs_tbf_rule_start(policy, head, &start);
987
988         return rc;
989 }
990
991 static void
992 nrs_tbf_nid_cli_init(struct nrs_tbf_client *cli,
993                              struct ptlrpc_request *req)
994 {
995         cli->tc_nid = req->rq_peer.nid;
996 }
997
998 static int nrs_tbf_nid_rule_init(struct ptlrpc_nrs_policy *policy,
999                                  struct nrs_tbf_rule *rule,
1000                                  struct nrs_tbf_cmd *start)
1001 {
1002         LASSERT(start->tc_nids_str);
1003         OBD_ALLOC(rule->tr_nids_str,
1004                   strlen(start->tc_nids_str) + 1);
1005         if (rule->tr_nids_str == NULL)
1006                 return -ENOMEM;
1007
1008         memcpy(rule->tr_nids_str,
1009                start->tc_nids_str,
1010                strlen(start->tc_nids_str));
1011
1012         INIT_LIST_HEAD(&rule->tr_nids);
1013         if (!list_empty(&start->tc_nids)) {
1014                 if (cfs_parse_nidlist(rule->tr_nids_str,
1015                                       strlen(rule->tr_nids_str),
1016                                       &rule->tr_nids) <= 0) {
1017                         CERROR("nids {%s} illegal\n",
1018                                rule->tr_nids_str);
1019                         OBD_FREE(rule->tr_nids_str,
1020                                  strlen(start->tc_nids_str) + 1);
1021                         return -EINVAL;
1022                 }
1023         }
1024         return 0;
1025 }
1026
1027 static int
1028 nrs_tbf_nid_rule_dump(struct nrs_tbf_rule *rule, struct seq_file *m)
1029 {
1030         return seq_printf(m, "%s {%s} %llu, ref %d\n", rule->tr_name,
1031                           rule->tr_nids_str, rule->tr_rpc_rate,
1032                           atomic_read(&rule->tr_ref) - 1);
1033 }
1034
1035 static int
1036 nrs_tbf_nid_rule_match(struct nrs_tbf_rule *rule,
1037                        struct nrs_tbf_client *cli)
1038 {
1039         return cfs_match_nid(cli->tc_nid, &rule->tr_nids);
1040 }
1041
1042 static void nrs_tbf_nid_rule_fini(struct nrs_tbf_rule *rule)
1043 {
1044         if (!list_empty(&rule->tr_nids))
1045                 cfs_free_nidlist(&rule->tr_nids);
1046         LASSERT(rule->tr_nids_str != NULL);
1047         OBD_FREE(rule->tr_nids_str, strlen(rule->tr_nids_str) + 1);
1048 }
1049
1050 static void nrs_tbf_nid_cmd_fini(struct nrs_tbf_cmd *cmd)
1051 {
1052         if (!list_empty(&cmd->tc_nids))
1053                 cfs_free_nidlist(&cmd->tc_nids);
1054         if (cmd->tc_nids_str)
1055                 OBD_FREE(cmd->tc_nids_str, strlen(cmd->tc_nids_str) + 1);
1056 }
1057
1058 static int nrs_tbf_nid_parse(struct nrs_tbf_cmd *cmd, const char *id)
1059 {
1060         OBD_ALLOC(cmd->tc_nids_str, strlen(id) + 1);
1061         if (cmd->tc_nids_str == NULL)
1062                 return -ENOMEM;
1063
1064         memcpy(cmd->tc_nids_str, id, strlen(id));
1065
1066         /* parse NID list */
1067         if (cfs_parse_nidlist(cmd->tc_nids_str,
1068                               strlen(cmd->tc_nids_str),
1069                               &cmd->tc_nids) <= 0) {
1070                 nrs_tbf_nid_cmd_fini(cmd);
1071                 return -EINVAL;
1072         }
1073
1074         return 0;
1075 }
1076
1077 static struct nrs_tbf_ops nrs_tbf_nid_ops = {
1078         .o_name = NRS_TBF_TYPE_NID,
1079         .o_startup = nrs_tbf_nid_startup,
1080         .o_cli_find = nrs_tbf_nid_cli_find,
1081         .o_cli_findadd = nrs_tbf_nid_cli_findadd,
1082         .o_cli_put = nrs_tbf_nid_cli_put,
1083         .o_cli_init = nrs_tbf_nid_cli_init,
1084         .o_rule_init = nrs_tbf_nid_rule_init,
1085         .o_rule_dump = nrs_tbf_nid_rule_dump,
1086         .o_rule_match = nrs_tbf_nid_rule_match,
1087         .o_rule_fini = nrs_tbf_nid_rule_fini,
1088 };
1089
1090 /**
1091  * Is called before the policy transitions into
1092  * ptlrpc_nrs_pol_state::NRS_POL_STATE_STARTED; allocates and initializes a
1093  * policy-specific private data structure.
1094  *
1095  * \param[in] policy The policy to start
1096  *
1097  * \retval -ENOMEM OOM error
1098  * \retval  0      success
1099  *
1100  * \see nrs_policy_register()
1101  * \see nrs_policy_ctl()
1102  */
1103 static int nrs_tbf_start(struct ptlrpc_nrs_policy *policy, char *arg)
1104 {
1105         struct nrs_tbf_head     *head;
1106         struct nrs_tbf_ops      *ops;
1107         __u32                    type;
1108         int rc = 0;
1109
1110         if (arg == NULL || strlen(arg) > NRS_TBF_TYPE_MAX_LEN)
1111                 GOTO(out, rc = -EINVAL);
1112
1113         if (strcmp(arg, NRS_TBF_TYPE_NID) == 0) {
1114                 ops = &nrs_tbf_nid_ops;
1115                 type = NRS_TBF_FLAG_NID;
1116         } else if (strcmp(arg, NRS_TBF_TYPE_JOBID) == 0) {
1117                 ops = &nrs_tbf_jobid_ops;
1118                 type = NRS_TBF_FLAG_JOBID;
1119         } else
1120                 GOTO(out, rc = -ENOTSUPP);
1121
1122         OBD_CPT_ALLOC_PTR(head, nrs_pol2cptab(policy), nrs_pol2cptid(policy));
1123         if (head == NULL)
1124                 GOTO(out, rc = -ENOMEM);
1125
1126         memcpy(head->th_type, arg, strlen(arg));
1127         head->th_type[strlen(arg)] = '\0';
1128         head->th_ops = ops;
1129         head->th_type_flag = type;
1130
1131         head->th_binheap = cfs_binheap_create(&nrs_tbf_heap_ops,
1132                                               CBH_FLAG_ATOMIC_GROW, 4096, NULL,
1133                                               nrs_pol2cptab(policy),
1134                                               nrs_pol2cptid(policy));
1135         if (head->th_binheap == NULL)
1136                 GOTO(out_free_head, rc = -ENOMEM);
1137
1138         atomic_set(&head->th_rule_sequence, 0);
1139         spin_lock_init(&head->th_rule_lock);
1140         INIT_LIST_HEAD(&head->th_list);
1141         hrtimer_init(&head->th_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1142         head->th_timer.function = nrs_tbf_timer_cb;
1143         rc = head->th_ops->o_startup(policy, head);
1144         if (rc)
1145                 GOTO(out_free_heap, rc);
1146
1147         policy->pol_private = head;
1148         return 0;
1149 out_free_heap:
1150         cfs_binheap_destroy(head->th_binheap);
1151 out_free_head:
1152         OBD_FREE_PTR(head);
1153 out:
1154         return rc;
1155 }
1156
1157 /**
1158  * Is called before the policy transitions into
1159  * ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPED; deallocates the policy-specific
1160  * private data structure.
1161  *
1162  * \param[in] policy The policy to stop
1163  *
1164  * \see nrs_policy_stop0()
1165  */
1166 static void nrs_tbf_stop(struct ptlrpc_nrs_policy *policy)
1167 {
1168         struct nrs_tbf_head *head = policy->pol_private;
1169         struct ptlrpc_nrs *nrs = policy->pol_nrs;
1170         struct nrs_tbf_rule *rule, *n;
1171
1172         LASSERT(head != NULL);
1173         LASSERT(head->th_cli_hash != NULL);
1174         hrtimer_cancel(&head->th_timer);
1175         /* Should cleanup hash first before free rules */
1176         cfs_hash_putref(head->th_cli_hash);
1177         list_for_each_entry_safe(rule, n, &head->th_list, tr_linkage) {
1178                 list_del_init(&rule->tr_linkage);
1179                 nrs_tbf_rule_put(rule);
1180         }
1181         LASSERT(list_empty(&head->th_list));
1182         LASSERT(head->th_binheap != NULL);
1183         LASSERT(cfs_binheap_is_empty(head->th_binheap));
1184         cfs_binheap_destroy(head->th_binheap);
1185         OBD_FREE_PTR(head);
1186         spin_lock(&nrs->nrs_lock);
1187         nrs->nrs_throttling = 0;
1188         spin_unlock(&nrs->nrs_lock);
1189         wake_up(&policy->pol_nrs->nrs_svcpt->scp_waitq);
1190 }
1191
1192 /**
1193  * Performs a policy-specific ctl function on TBF policy instances; similar
1194  * to ioctl.
1195  *
1196  * \param[in]     policy the policy instance
1197  * \param[in]     opc    the opcode
1198  * \param[in,out] arg    used for passing parameters and information
1199  *
1200  * \pre assert_spin_locked(&policy->pol_nrs->->nrs_lock)
1201  * \post assert_spin_locked(&policy->pol_nrs->->nrs_lock)
1202  *
1203  * \retval 0   operation carried out successfully
1204  * \retval -ve error
1205  */
1206 static int nrs_tbf_ctl(struct ptlrpc_nrs_policy *policy,
1207                        enum ptlrpc_nrs_ctl opc,
1208                        void *arg)
1209 {
1210         int rc = 0;
1211         ENTRY;
1212
1213         assert_spin_locked(&policy->pol_nrs->nrs_lock);
1214
1215         switch ((enum nrs_ctl_tbf)opc) {
1216         default:
1217                 RETURN(-EINVAL);
1218
1219         /**
1220          * Read RPC rate size of a policy instance.
1221          */
1222         case NRS_CTL_TBF_RD_RULE: {
1223                 struct nrs_tbf_head *head = policy->pol_private;
1224                 struct seq_file *m = (struct seq_file *) arg;
1225                 struct ptlrpc_service_part *svcpt;
1226
1227                 svcpt = policy->pol_nrs->nrs_svcpt;
1228                 seq_printf(m, "CPT %d:\n", svcpt->scp_cpt);
1229
1230                 rc = nrs_tbf_rule_dump_all(head, m);
1231                 }
1232                 break;
1233
1234         /**
1235          * Write RPC rate of a policy instance.
1236          */
1237         case NRS_CTL_TBF_WR_RULE: {
1238                 struct nrs_tbf_head *head = policy->pol_private;
1239                 struct nrs_tbf_cmd *cmd;
1240
1241                 cmd = (struct nrs_tbf_cmd *)arg;
1242                 rc = nrs_tbf_command(policy,
1243                                      head,
1244                                      cmd);
1245                 }
1246                 break;
1247         }
1248
1249         RETURN(rc);
1250 }
1251
1252 /**
1253  * Is called for obtaining a TBF policy resource.
1254  *
1255  * \param[in]  policy     The policy on which the request is being asked for
1256  * \param[in]  nrq        The request for which resources are being taken
1257  * \param[in]  parent     Parent resource, unused in this policy
1258  * \param[out] resp       Resources references are placed in this array
1259  * \param[in]  moving_req Signifies limited caller context; unused in this
1260  *                        policy
1261  *
1262  *
1263  * \see nrs_resource_get_safe()
1264  */
1265 static int nrs_tbf_res_get(struct ptlrpc_nrs_policy *policy,
1266                            struct ptlrpc_nrs_request *nrq,
1267                            const struct ptlrpc_nrs_resource *parent,
1268                            struct ptlrpc_nrs_resource **resp,
1269                            bool moving_req)
1270 {
1271         struct nrs_tbf_head   *head;
1272         struct nrs_tbf_client *cli;
1273         struct nrs_tbf_client *tmp;
1274         struct ptlrpc_request *req;
1275
1276         if (parent == NULL) {
1277                 *resp = &((struct nrs_tbf_head *)policy->pol_private)->th_res;
1278                 return 0;
1279         }
1280
1281         head = container_of(parent, struct nrs_tbf_head, th_res);
1282         req = container_of(nrq, struct ptlrpc_request, rq_nrq);
1283         cli = head->th_ops->o_cli_find(head, req);
1284         if (cli != NULL) {
1285                 spin_lock(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
1286                 LASSERT(cli->tc_rule);
1287                 if (cli->tc_rule_sequence !=
1288                     atomic_read(&head->th_rule_sequence) ||
1289                     cli->tc_rule->tr_flags & NTRS_STOPPING) {
1290                         struct nrs_tbf_rule *rule;
1291
1292                         rule = nrs_tbf_rule_match(head, cli);
1293                         if (rule != cli->tc_rule)
1294                                 nrs_tbf_cli_reset(head, rule, cli);
1295                         else
1296                                 nrs_tbf_rule_put(rule);
1297                 } else if (cli->tc_rule_generation !=
1298                            cli->tc_rule->tr_generation) {
1299                         nrs_tbf_cli_reset_value(head, cli);
1300                 }
1301                 spin_unlock(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
1302                 goto out;
1303         }
1304
1305         OBD_CPT_ALLOC_GFP(cli, nrs_pol2cptab(policy), nrs_pol2cptid(policy),
1306                           sizeof(*cli), moving_req ? GFP_ATOMIC : __GFP_IO);
1307         if (cli == NULL)
1308                 return -ENOMEM;
1309         nrs_tbf_cli_init(head, cli, req);
1310         tmp = head->th_ops->o_cli_findadd(head, cli);
1311         if (tmp != cli) {
1312                 atomic_dec(&cli->tc_ref);
1313                 nrs_tbf_cli_fini(cli);
1314                 cli = tmp;
1315         }
1316 out:
1317         *resp = &cli->tc_res;
1318
1319         return 1;
1320 }
1321
1322 /**
1323  * Called when releasing references to the resource hierachy obtained for a
1324  * request for scheduling using the TBF policy.
1325  *
1326  * \param[in] policy   the policy the resource belongs to
1327  * \param[in] res      the resource to be released
1328  */
1329 static void nrs_tbf_res_put(struct ptlrpc_nrs_policy *policy,
1330                             const struct ptlrpc_nrs_resource *res)
1331 {
1332         struct nrs_tbf_head   *head;
1333         struct nrs_tbf_client *cli;
1334
1335         /**
1336          * Do nothing for freeing parent, nrs_tbf_net resources
1337          */
1338         if (res->res_parent == NULL)
1339                 return;
1340
1341         cli = container_of(res, struct nrs_tbf_client, tc_res);
1342         head = container_of(res->res_parent, struct nrs_tbf_head, th_res);
1343
1344         head->th_ops->o_cli_put(head, cli);
1345 }
1346
1347 /**
1348  * Called when getting a request from the TBF policy for handling, or just
1349  * peeking; removes the request from the policy when it is to be handled.
1350  *
1351  * \param[in] policy The policy
1352  * \param[in] peek   When set, signifies that we just want to examine the
1353  *                   request, and not handle it, so the request is not removed
1354  *                   from the policy.
1355  * \param[in] force  Force the policy to return a request; unused in this
1356  *                   policy
1357  *
1358  * \retval The request to be handled; this is the next request in the TBF
1359  *         rule
1360  *
1361  * \see ptlrpc_nrs_req_get_nolock()
1362  * \see nrs_request_get()
1363  */
1364 static
1365 struct ptlrpc_nrs_request *nrs_tbf_req_get(struct ptlrpc_nrs_policy *policy,
1366                                            bool peek, bool force)
1367 {
1368         struct nrs_tbf_head       *head = policy->pol_private;
1369         struct ptlrpc_nrs_request *nrq = NULL;
1370         struct nrs_tbf_client     *cli;
1371         cfs_binheap_node_t        *node;
1372
1373         assert_spin_locked(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
1374
1375         if (!peek && policy->pol_nrs->nrs_throttling)
1376                 return NULL;
1377
1378         node = cfs_binheap_root(head->th_binheap);
1379         if (unlikely(node == NULL))
1380                 return NULL;
1381
1382         cli = container_of(node, struct nrs_tbf_client, tc_node);
1383         LASSERT(cli->tc_in_heap);
1384         if (peek) {
1385                 nrq = list_entry(cli->tc_list.next,
1386                                      struct ptlrpc_nrs_request,
1387                                      nr_u.tbf.tr_list);
1388         } else {
1389                 __u64 now = ktime_to_ns(ktime_get());
1390                 __u64 passed;
1391                 long  ntoken;
1392                 __u64 deadline;
1393
1394                 deadline = cli->tc_check_time +
1395                           cli->tc_nsecs;
1396                 LASSERT(now >= cli->tc_check_time);
1397                 passed = now - cli->tc_check_time;
1398                 ntoken = (passed * cli->tc_rpc_rate) / NSEC_PER_SEC;
1399                 ntoken += cli->tc_ntoken;
1400                 if (ntoken > cli->tc_depth)
1401                         ntoken = cli->tc_depth;
1402                 if (ntoken > 0) {
1403                         struct ptlrpc_request *req;
1404                         nrq = list_entry(cli->tc_list.next,
1405                                              struct ptlrpc_nrs_request,
1406                                              nr_u.tbf.tr_list);
1407                         req = container_of(nrq,
1408                                            struct ptlrpc_request,
1409                                            rq_nrq);
1410                         ntoken--;
1411                         cli->tc_ntoken = ntoken;
1412                         cli->tc_check_time = now;
1413                         list_del_init(&nrq->nr_u.tbf.tr_list);
1414                         if (list_empty(&cli->tc_list)) {
1415                                 cfs_binheap_remove(head->th_binheap,
1416                                                    &cli->tc_node);
1417                                 cli->tc_in_heap = false;
1418                         } else {
1419                                 cfs_binheap_relocate(head->th_binheap,
1420                                                      &cli->tc_node);
1421                         }
1422                         CDEBUG(D_RPCTRACE,
1423                                "NRS start %s request from %s, "
1424                                "seq: "LPU64"\n",
1425                                policy->pol_desc->pd_name,
1426                                libcfs_id2str(req->rq_peer),
1427                                nrq->nr_u.tbf.tr_sequence);
1428                 } else {
1429                         ktime_t time;
1430
1431                         spin_lock(&policy->pol_nrs->nrs_lock);
1432                         policy->pol_nrs->nrs_throttling = 1;
1433                         spin_unlock(&policy->pol_nrs->nrs_lock);
1434                         head->th_deadline = deadline;
1435                         time = ktime_set(0, 0);
1436                         time = ktime_add_ns(time, deadline);
1437                         hrtimer_start(&head->th_timer, time, HRTIMER_MODE_ABS);
1438                 }
1439         }
1440
1441         return nrq;
1442 }
1443
1444 /**
1445  * Adds request \a nrq to \a policy's list of queued requests
1446  *
1447  * \param[in] policy The policy
1448  * \param[in] nrq    The request to add
1449  *
1450  * \retval 0 success; nrs_request_enqueue() assumes this function will always
1451  *                    succeed
1452  */
1453 static int nrs_tbf_req_add(struct ptlrpc_nrs_policy *policy,
1454                            struct ptlrpc_nrs_request *nrq)
1455 {
1456         struct nrs_tbf_head   *head;
1457         struct nrs_tbf_client *cli;
1458         int                    rc = 0;
1459
1460         assert_spin_locked(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
1461
1462         cli = container_of(nrs_request_resource(nrq),
1463                            struct nrs_tbf_client, tc_res);
1464         head = container_of(nrs_request_resource(nrq)->res_parent,
1465                             struct nrs_tbf_head, th_res);
1466         if (list_empty(&cli->tc_list)) {
1467                 LASSERT(!cli->tc_in_heap);
1468                 rc = cfs_binheap_insert(head->th_binheap, &cli->tc_node);
1469                 if (rc == 0) {
1470                         cli->tc_in_heap = true;
1471                         nrq->nr_u.tbf.tr_sequence = head->th_sequence++;
1472                         list_add_tail(&nrq->nr_u.tbf.tr_list,
1473                                           &cli->tc_list);
1474                         if (policy->pol_nrs->nrs_throttling) {
1475                                 __u64 deadline = cli->tc_check_time +
1476                                                  cli->tc_nsecs;
1477                                 if ((head->th_deadline > deadline) &&
1478                                     (hrtimer_try_to_cancel(&head->th_timer)
1479                                      >= 0)) {
1480                                         ktime_t time;
1481                                         head->th_deadline = deadline;
1482                                         time = ktime_set(0, 0);
1483                                         time = ktime_add_ns(time, deadline);
1484                                         hrtimer_start(&head->th_timer, time,
1485                                                       HRTIMER_MODE_ABS);
1486                                 }
1487                         }
1488                 }
1489         } else {
1490                 LASSERT(cli->tc_in_heap);
1491                 nrq->nr_u.tbf.tr_sequence = head->th_sequence++;
1492                 list_add_tail(&nrq->nr_u.tbf.tr_list,
1493                                   &cli->tc_list);
1494         }
1495         return rc;
1496 }
1497
1498 /**
1499  * Removes request \a nrq from \a policy's list of queued requests.
1500  *
1501  * \param[in] policy The policy
1502  * \param[in] nrq    The request to remove
1503  */
1504 static void nrs_tbf_req_del(struct ptlrpc_nrs_policy *policy,
1505                              struct ptlrpc_nrs_request *nrq)
1506 {
1507         struct nrs_tbf_head   *head;
1508         struct nrs_tbf_client *cli;
1509
1510         assert_spin_locked(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
1511
1512         cli = container_of(nrs_request_resource(nrq),
1513                            struct nrs_tbf_client, tc_res);
1514         head = container_of(nrs_request_resource(nrq)->res_parent,
1515                             struct nrs_tbf_head, th_res);
1516
1517         LASSERT(!list_empty(&nrq->nr_u.tbf.tr_list));
1518         list_del_init(&nrq->nr_u.tbf.tr_list);
1519         if (list_empty(&cli->tc_list)) {
1520                 cfs_binheap_remove(head->th_binheap,
1521                                    &cli->tc_node);
1522                 cli->tc_in_heap = false;
1523         } else {
1524                 cfs_binheap_relocate(head->th_binheap,
1525                                      &cli->tc_node);
1526         }
1527 }
1528
1529 /**
1530  * Prints a debug statement right before the request \a nrq stops being
1531  * handled.
1532  *
1533  * \param[in] policy The policy handling the request
1534  * \param[in] nrq    The request being handled
1535  *
1536  * \see ptlrpc_server_finish_request()
1537  * \see ptlrpc_nrs_req_stop_nolock()
1538  */
1539 static void nrs_tbf_req_stop(struct ptlrpc_nrs_policy *policy,
1540                               struct ptlrpc_nrs_request *nrq)
1541 {
1542         struct ptlrpc_request *req = container_of(nrq, struct ptlrpc_request,
1543                                                   rq_nrq);
1544
1545         assert_spin_locked(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
1546
1547         CDEBUG(D_RPCTRACE, "NRS stop %s request from %s, seq: "LPU64"\n",
1548                policy->pol_desc->pd_name, libcfs_id2str(req->rq_peer),
1549                nrq->nr_u.tbf.tr_sequence);
1550 }
1551
1552 #ifdef LPROCFS
1553
1554 /**
1555  * lprocfs interface
1556  */
1557
1558 /**
1559  * The maximum RPC rate.
1560  */
1561 #define LPROCFS_NRS_RATE_MAX            65535
1562
1563 static int
1564 ptlrpc_lprocfs_nrs_tbf_rule_seq_show(struct seq_file *m, void *data)
1565 {
1566         struct ptlrpc_service       *svc = m->private;
1567         int                          rc;
1568
1569         seq_printf(m, "regular_requests:\n");
1570         /**
1571          * Perform two separate calls to this as only one of the NRS heads'
1572          * policies may be in the ptlrpc_nrs_pol_state::NRS_POL_STATE_STARTED or
1573          * ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPING state.
1574          */
1575         rc = ptlrpc_nrs_policy_control(svc, PTLRPC_NRS_QUEUE_REG,
1576                                        NRS_POL_NAME_TBF,
1577                                        NRS_CTL_TBF_RD_RULE,
1578                                        false, m);
1579         if (rc == 0) {
1580                 /**
1581                  * -ENOSPC means buf in the parameter m is overflow, return 0
1582                  * here to let upper layer function seq_read alloc a larger
1583                  * memory area and do this process again.
1584                  */
1585         } else if (rc == -ENOSPC) {
1586                 return 0;
1587
1588                 /**
1589                  * Ignore -ENODEV as the regular NRS head's policy may be in the
1590                  * ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPED state.
1591                  */
1592         } else if (rc != -ENODEV) {
1593                 return rc;
1594         }
1595
1596         if (!nrs_svc_has_hp(svc))
1597                 goto no_hp;
1598
1599         seq_printf(m, "high_priority_requests:\n");
1600         rc = ptlrpc_nrs_policy_control(svc, PTLRPC_NRS_QUEUE_HP,
1601                                        NRS_POL_NAME_TBF,
1602                                        NRS_CTL_TBF_RD_RULE,
1603                                        false, m);
1604         if (rc == 0) {
1605                 /**
1606                  * -ENOSPC means buf in the parameter m is overflow, return 0
1607                  * here to let upper layer function seq_read alloc a larger
1608                  * memory area and do this process again.
1609                  */
1610         } else if (rc == -ENOSPC) {
1611                 return 0;
1612         }
1613
1614 no_hp:
1615
1616         return rc;
1617 }
1618
1619 static int nrs_tbf_id_parse(struct nrs_tbf_cmd *cmd, char **val)
1620 {
1621         int rc;
1622         char *token;
1623
1624         token = strsep(val, "}");
1625         if (*val == NULL)
1626                 GOTO(out, rc = -EINVAL);
1627
1628         if (strlen(token) <= 1 ||
1629             token[0] != '{')
1630                 GOTO(out, rc = -EINVAL);
1631         /* Skip '{' */
1632         token++;
1633
1634         /* Should be followed by ' ' or nothing */
1635         if ((*val)[0] == '\0')
1636                 *val = NULL;
1637         else if ((*val)[0] == ' ')
1638                 (*val)++;
1639         else
1640                 GOTO(out, rc = -EINVAL);
1641
1642         rc = nrs_tbf_jobid_parse(cmd, token);
1643         if (!rc)
1644                 cmd->tc_valid_types |= NRS_TBF_FLAG_JOBID;
1645
1646         rc = nrs_tbf_nid_parse(cmd, token);
1647         if (!rc)
1648                 cmd->tc_valid_types |= NRS_TBF_FLAG_NID;
1649
1650         if (!cmd->tc_valid_types)
1651                 rc = -EINVAL;
1652         else
1653                 rc = 0;
1654 out:
1655         return rc;
1656 }
1657
1658
1659 static void nrs_tbf_cmd_fini(struct nrs_tbf_cmd *cmd)
1660 {
1661         if (cmd->tc_valid_types & NRS_TBF_FLAG_JOBID)
1662                 nrs_tbf_jobid_cmd_fini(cmd);
1663         if (cmd->tc_valid_types & NRS_TBF_FLAG_NID)
1664                 nrs_tbf_nid_cmd_fini(cmd);
1665 }
1666
1667 static struct nrs_tbf_cmd *
1668 nrs_tbf_parse_cmd(char *buffer, unsigned long count)
1669 {
1670         static struct nrs_tbf_cmd *cmd;
1671         char                      *token;
1672         char                      *val;
1673         int                        i;
1674         int                        rc = 0;
1675
1676         OBD_ALLOC_PTR(cmd);
1677         if (cmd == NULL)
1678                 GOTO(out, rc = -ENOMEM);
1679
1680         val = buffer;
1681         token = strsep(&val, " ");
1682         if (val == NULL || strlen(val) == 0)
1683                 GOTO(out_free_cmd, rc = -EINVAL);
1684
1685         /* Type of the command */
1686         if (strcmp(token, "start") == 0)
1687                 cmd->tc_cmd = NRS_CTL_TBF_START_RULE;
1688         else if (strcmp(token, "stop") == 0)
1689                 cmd->tc_cmd = NRS_CTL_TBF_STOP_RULE;
1690         else if (strcmp(token, "change") == 0)
1691                 cmd->tc_cmd = NRS_CTL_TBF_CHANGE_RATE;
1692         else
1693                 GOTO(out_free_cmd, rc = -EINVAL);
1694
1695         /* Name of the rule */
1696         token = strsep(&val, " ");
1697         if (val == NULL) {
1698                 /**
1699                  * Stop comand only need name argument,
1700                  * But other commands need ID or rate argument.
1701                  */
1702                 if (cmd->tc_cmd != NRS_CTL_TBF_STOP_RULE)
1703                         GOTO(out_free_cmd, rc = -EINVAL);
1704         }
1705
1706         for (i = 0; i < strlen(token); i++) {
1707                 if ((!isalnum(token[i])) &&
1708                     (token[i] != '_'))
1709                         GOTO(out_free_cmd, rc = -EINVAL);
1710         }
1711         cmd->tc_name = token;
1712
1713         if (cmd->tc_cmd == NRS_CTL_TBF_START_RULE) {
1714                 /* List of ID */
1715                 LASSERT(val);
1716                 rc = nrs_tbf_id_parse(cmd, &val);
1717                 if (rc)
1718                         GOTO(out_free_cmd, rc);
1719         }
1720
1721         if (val != NULL) {
1722                 if (cmd->tc_cmd == NRS_CTL_TBF_STOP_RULE ||
1723                     strlen(val) == 0 || !isdigit(val[0]))
1724                         GOTO(out_free_nid, rc = -EINVAL);
1725
1726                 cmd->tc_rpc_rate = simple_strtoull(val, NULL, 10);
1727                 if (cmd->tc_rpc_rate <= 0 ||
1728                     cmd->tc_rpc_rate >= LPROCFS_NRS_RATE_MAX)
1729                         GOTO(out_free_nid, rc = -EINVAL);
1730         } else {
1731                 if (cmd->tc_cmd == NRS_CTL_TBF_CHANGE_RATE)
1732                         GOTO(out_free_nid, rc = -EINVAL);
1733                 /* No RPC rate given */
1734                 cmd->tc_rpc_rate = tbf_rate;
1735         }
1736         goto out;
1737 out_free_nid:
1738         nrs_tbf_cmd_fini(cmd);
1739 out_free_cmd:
1740         OBD_FREE_PTR(cmd);
1741 out:
1742         if (rc)
1743                 cmd = ERR_PTR(rc);
1744         return cmd;
1745 }
1746
1747 extern struct nrs_core nrs_core;
1748 #define LPROCFS_WR_NRS_TBF_MAX_CMD (4096)
1749 static ssize_t
1750 ptlrpc_lprocfs_nrs_tbf_rule_seq_write(struct file *file, const char *buffer,
1751                                       size_t count, loff_t *off)
1752 {
1753         struct seq_file           *m = file->private_data;
1754         struct ptlrpc_service     *svc = m->private;
1755         char                      *kernbuf;
1756         char                      *val;
1757         int                        rc;
1758         static struct nrs_tbf_cmd *cmd;
1759         enum ptlrpc_nrs_queue_type queue = PTLRPC_NRS_QUEUE_BOTH;
1760         unsigned long              length;
1761         char                      *token;
1762
1763         OBD_ALLOC(kernbuf, LPROCFS_WR_NRS_TBF_MAX_CMD);
1764         if (kernbuf == NULL)
1765                 GOTO(out, rc = -ENOMEM);
1766
1767         if (count > LPROCFS_WR_NRS_TBF_MAX_CMD - 1)
1768                 GOTO(out_free_kernbuff, rc = -EINVAL);
1769
1770         if (copy_from_user(kernbuf, buffer, count))
1771                 GOTO(out_free_kernbuff, rc = -EFAULT);
1772
1773         val = kernbuf;
1774         token = strsep(&val, " ");
1775         if (val == NULL)
1776                 GOTO(out_free_kernbuff, rc = -EINVAL);
1777
1778         if (strcmp(token, "reg") == 0) {
1779                 queue = PTLRPC_NRS_QUEUE_REG;
1780         } else if (strcmp(token, "hp") == 0) {
1781                 queue = PTLRPC_NRS_QUEUE_HP;
1782         } else {
1783                 kernbuf[strlen(token)] = ' ';
1784                 val = kernbuf;
1785         }
1786         length = strlen(val);
1787
1788         if (length == 0)
1789                 GOTO(out_free_kernbuff, rc = -EINVAL);
1790
1791         if (queue == PTLRPC_NRS_QUEUE_HP && !nrs_svc_has_hp(svc))
1792                 GOTO(out_free_kernbuff, rc = -ENODEV);
1793         else if (queue == PTLRPC_NRS_QUEUE_BOTH && !nrs_svc_has_hp(svc))
1794                 queue = PTLRPC_NRS_QUEUE_REG;
1795
1796         cmd = nrs_tbf_parse_cmd(val, length);
1797         if (IS_ERR(cmd))
1798                 GOTO(out_free_kernbuff, rc = PTR_ERR(cmd));
1799
1800         /**
1801          * Serialize NRS core lprocfs operations with policy registration/
1802          * unregistration.
1803          */
1804         mutex_lock(&nrs_core.nrs_mutex);
1805         rc = ptlrpc_nrs_policy_control(svc, queue,
1806                                        NRS_POL_NAME_TBF,
1807                                        NRS_CTL_TBF_WR_RULE,
1808                                        false, cmd);
1809         mutex_unlock(&nrs_core.nrs_mutex);
1810
1811         nrs_tbf_cmd_fini(cmd);
1812         OBD_FREE_PTR(cmd);
1813 out_free_kernbuff:
1814         OBD_FREE(kernbuf, LPROCFS_WR_NRS_TBF_MAX_CMD);
1815 out:
1816         return rc ? rc : count;
1817 }
1818 LPROC_SEQ_FOPS(ptlrpc_lprocfs_nrs_tbf_rule);
1819
1820 /**
1821  * Initializes a TBF policy's lprocfs interface for service \a svc
1822  *
1823  * \param[in] svc the service
1824  *
1825  * \retval 0    success
1826  * \retval != 0 error
1827  */
1828 static int nrs_tbf_lprocfs_init(struct ptlrpc_service *svc)
1829 {
1830         struct lprocfs_seq_vars nrs_tbf_lprocfs_vars[] = {
1831                 { .name         = "nrs_tbf_rule",
1832                   .fops         = &ptlrpc_lprocfs_nrs_tbf_rule_fops,
1833                   .data = svc },
1834                 { NULL }
1835         };
1836
1837         if (svc->srv_procroot == NULL)
1838                 return 0;
1839
1840         return lprocfs_seq_add_vars(svc->srv_procroot, nrs_tbf_lprocfs_vars,
1841                                     NULL);
1842 }
1843
1844 /**
1845  * Cleans up a TBF policy's lprocfs interface for service \a svc
1846  *
1847  * \param[in] svc the service
1848  */
1849 static void nrs_tbf_lprocfs_fini(struct ptlrpc_service *svc)
1850 {
1851         if (svc->srv_procroot == NULL)
1852                 return;
1853
1854         lprocfs_remove_proc_entry("nrs_tbf_rule", svc->srv_procroot);
1855 }
1856
1857 #endif /* LPROCFS */
1858
1859 /**
1860  * TBF policy operations
1861  */
1862 static const struct ptlrpc_nrs_pol_ops nrs_tbf_ops = {
1863         .op_policy_start        = nrs_tbf_start,
1864         .op_policy_stop         = nrs_tbf_stop,
1865         .op_policy_ctl          = nrs_tbf_ctl,
1866         .op_res_get             = nrs_tbf_res_get,
1867         .op_res_put             = nrs_tbf_res_put,
1868         .op_req_get             = nrs_tbf_req_get,
1869         .op_req_enqueue         = nrs_tbf_req_add,
1870         .op_req_dequeue         = nrs_tbf_req_del,
1871         .op_req_stop            = nrs_tbf_req_stop,
1872 #ifdef LPROCFS
1873         .op_lprocfs_init        = nrs_tbf_lprocfs_init,
1874         .op_lprocfs_fini        = nrs_tbf_lprocfs_fini,
1875 #endif
1876 };
1877
1878 /**
1879  * TBF policy configuration
1880  */
1881 struct ptlrpc_nrs_pol_conf nrs_conf_tbf = {
1882         .nc_name                = NRS_POL_NAME_TBF,
1883         .nc_ops                 = &nrs_tbf_ops,
1884         .nc_compat              = nrs_policy_compat_all,
1885 };
1886
1887 /** @} tbf */
1888
1889 /** @} nrs */
1890
1891 #endif /* HAVE_SERVER_SUPPORT */