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