2121#include <linux/skbuff.h>
2222#include <linux/jhash.h>
2323#include <linux/slab.h>
24+ #include <linux/vmalloc.h>
2425#include <net/ip.h>
2526#include <net/netlink.h>
2627#include <net/pkt_sched.h>
7677#define SFQ_DEPTH 128 /* max number of packets per flow */
7778#define SFQ_SLOTS 128 /* max number of flows */
7879#define SFQ_EMPTY_SLOT 255
79- #define SFQ_HASH_DIVISOR 1024
80+ #define SFQ_DEFAULT_HASH_DIVISOR 1024
81+
8082/* We use 16 bits to store allot, and want to handle packets up to 64K
8183 * Scale allot by 8 (1<<3) so that no overflow occurs.
8284 */
@@ -112,15 +114,15 @@ struct sfq_sched_data {
112114 int perturb_period ;
113115 unsigned int quantum ; /* Allotment per round: MUST BE >= MTU */
114116 int limit ;
115-
117+ unsigned int divisor ; /* number of slots in hash table */
116118/* Variables */
117119 struct tcf_proto * filter_list ;
118120 struct timer_list perturb_timer ;
119121 u32 perturbation ;
120122 sfq_index cur_depth ; /* depth of longest slot */
121123 unsigned short scaled_quantum ; /* SFQ_ALLOT_SIZE(quantum) */
122124 struct sfq_slot * tail ; /* current slot in round */
123- sfq_index ht [ SFQ_HASH_DIVISOR ]; /* Hash table */
125+ sfq_index * ht ; /* Hash table (divisor slots) */
124126 struct sfq_slot slots [SFQ_SLOTS ];
125127 struct sfq_head dep [SFQ_DEPTH ]; /* Linked list of slots, indexed by depth */
126128};
@@ -137,7 +139,7 @@ static inline struct sfq_head *sfq_dep_head(struct sfq_sched_data *q, sfq_index
137139
138140static unsigned int sfq_fold_hash (struct sfq_sched_data * q , u32 h , u32 h1 )
139141{
140- return jhash_2words (h , h1 , q -> perturbation ) & (SFQ_HASH_DIVISOR - 1 );
142+ return jhash_2words (h , h1 , q -> perturbation ) & (q -> divisor - 1 );
141143}
142144
143145static unsigned int sfq_hash (struct sfq_sched_data * q , struct sk_buff * skb )
@@ -201,7 +203,7 @@ static unsigned int sfq_classify(struct sk_buff *skb, struct Qdisc *sch,
201203
202204 if (TC_H_MAJ (skb -> priority ) == sch -> handle &&
203205 TC_H_MIN (skb -> priority ) > 0 &&
204- TC_H_MIN (skb -> priority ) <= SFQ_HASH_DIVISOR )
206+ TC_H_MIN (skb -> priority ) <= q -> divisor )
205207 return TC_H_MIN (skb -> priority );
206208
207209 if (!q -> filter_list )
@@ -219,7 +221,7 @@ static unsigned int sfq_classify(struct sk_buff *skb, struct Qdisc *sch,
219221 return 0 ;
220222 }
221223#endif
222- if (TC_H_MIN (res .classid ) <= SFQ_HASH_DIVISOR )
224+ if (TC_H_MIN (res .classid ) <= q -> divisor )
223225 return TC_H_MIN (res .classid );
224226 }
225227 return 0 ;
@@ -496,7 +498,11 @@ static int sfq_change(struct Qdisc *sch, struct nlattr *opt)
496498 q -> perturb_period = ctl -> perturb_period * HZ ;
497499 if (ctl -> limit )
498500 q -> limit = min_t (u32 , ctl -> limit , SFQ_DEPTH - 1 );
499-
501+ if (ctl -> divisor ) {
502+ if (!is_power_of_2 (ctl -> divisor ) || ctl -> divisor > 65536 )
503+ return - EINVAL ;
504+ q -> divisor = ctl -> divisor ;
505+ }
500506 qlen = sch -> q .qlen ;
501507 while (sch -> q .qlen > q -> limit )
502508 sfq_drop (sch );
@@ -514,15 +520,13 @@ static int sfq_change(struct Qdisc *sch, struct nlattr *opt)
514520static int sfq_init (struct Qdisc * sch , struct nlattr * opt )
515521{
516522 struct sfq_sched_data * q = qdisc_priv (sch );
523+ size_t sz ;
517524 int i ;
518525
519526 q -> perturb_timer .function = sfq_perturbation ;
520527 q -> perturb_timer .data = (unsigned long )sch ;
521528 init_timer_deferrable (& q -> perturb_timer );
522529
523- for (i = 0 ; i < SFQ_HASH_DIVISOR ; i ++ )
524- q -> ht [i ] = SFQ_EMPTY_SLOT ;
525-
526530 for (i = 0 ; i < SFQ_DEPTH ; i ++ ) {
527531 q -> dep [i ].next = i + SFQ_SLOTS ;
528532 q -> dep [i ].prev = i + SFQ_SLOTS ;
@@ -531,6 +535,7 @@ static int sfq_init(struct Qdisc *sch, struct nlattr *opt)
531535 q -> limit = SFQ_DEPTH - 1 ;
532536 q -> cur_depth = 0 ;
533537 q -> tail = NULL ;
538+ q -> divisor = SFQ_DEFAULT_HASH_DIVISOR ;
534539 if (opt == NULL ) {
535540 q -> quantum = psched_mtu (qdisc_dev (sch ));
536541 q -> scaled_quantum = SFQ_ALLOT_SIZE (q -> quantum );
@@ -542,6 +547,15 @@ static int sfq_init(struct Qdisc *sch, struct nlattr *opt)
542547 return err ;
543548 }
544549
550+ sz = sizeof (q -> ht [0 ]) * q -> divisor ;
551+ q -> ht = kmalloc (sz , GFP_KERNEL );
552+ if (!q -> ht && sz > PAGE_SIZE )
553+ q -> ht = vmalloc (sz );
554+ if (!q -> ht )
555+ return - ENOMEM ;
556+ for (i = 0 ; i < q -> divisor ; i ++ )
557+ q -> ht [i ] = SFQ_EMPTY_SLOT ;
558+
545559 for (i = 0 ; i < SFQ_SLOTS ; i ++ ) {
546560 slot_queue_init (& q -> slots [i ]);
547561 sfq_link (q , i );
@@ -556,6 +570,10 @@ static void sfq_destroy(struct Qdisc *sch)
556570 tcf_destroy_chain (& q -> filter_list );
557571 q -> perturb_period = 0 ;
558572 del_timer_sync (& q -> perturb_timer );
573+ if (is_vmalloc_addr (q -> ht ))
574+ vfree (q -> ht );
575+ else
576+ kfree (q -> ht );
559577}
560578
561579static int sfq_dump (struct Qdisc * sch , struct sk_buff * skb )
@@ -568,7 +586,7 @@ static int sfq_dump(struct Qdisc *sch, struct sk_buff *skb)
568586 opt .perturb_period = q -> perturb_period / HZ ;
569587
570588 opt .limit = q -> limit ;
571- opt .divisor = SFQ_HASH_DIVISOR ;
589+ opt .divisor = q -> divisor ;
572590 opt .flows = q -> limit ;
573591
574592 NLA_PUT (skb , TCA_OPTIONS , sizeof (opt ), & opt );
@@ -646,7 +664,7 @@ static void sfq_walk(struct Qdisc *sch, struct qdisc_walker *arg)
646664 if (arg -> stop )
647665 return ;
648666
649- for (i = 0 ; i < SFQ_HASH_DIVISOR ; i ++ ) {
667+ for (i = 0 ; i < q -> divisor ; i ++ ) {
650668 if (q -> ht [i ] == SFQ_EMPTY_SLOT ||
651669 arg -> count < arg -> skip ) {
652670 arg -> count ++ ;
0 commit comments