~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/sched/sch_dsmark.c

Version: ~ [ linux-6.4-rc3 ] ~ [ linux-6.3.4 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.30 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.113 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.180 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.243 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.283 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.315 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /* net/sched/sch_dsmark.c - Differentiated Services field marker */
  2 
  3 /* Written 1998-2000 by Werner Almesberger, EPFL ICA */
  4 
  5 
  6 #include <linux/config.h>
  7 #include <linux/module.h>
  8 #include <linux/types.h>
  9 #include <linux/string.h>
 10 #include <linux/errno.h>
 11 #include <linux/skbuff.h>
 12 #include <linux/netdevice.h> /* for pkt_sched */
 13 #include <linux/rtnetlink.h>
 14 #include <net/pkt_sched.h>
 15 #include <net/dsfield.h>
 16 #include <asm/byteorder.h>
 17 
 18 
 19 #if 1 /* control */
 20 #define DPRINTK(format,args...) printk(KERN_DEBUG format,##args)
 21 #else
 22 #define DPRINTK(format,args...)
 23 #endif
 24 
 25 #if 0 /* data */
 26 #define D2PRINTK(format,args...) printk(KERN_DEBUG format,##args)
 27 #else
 28 #define D2PRINTK(format,args...)
 29 #endif
 30 
 31 
 32 #define PRIV(sch) ((struct dsmark_qdisc_data *) (sch)->data)
 33 
 34 
 35 /*
 36  * classid      class           marking
 37  * -------      -----           -------
 38  *   n/a          0             n/a
 39  *   x:0          1             use entry [0]
 40  *   ...         ...            ...
 41  *   x:y y>0     y+1            use entry [y]
 42  *   ...         ...            ...
 43  * x:indices-1  indices         use entry [indices-1]
 44  *   ...         ...            ...
 45  *   x:y         y+1            use entry [y & (indices-1)]
 46  *   ...         ...            ...
 47  * 0xffff       0x10000         use entry [indices-1]
 48  */
 49 
 50 
 51 #define NO_DEFAULT_INDEX        (1 << 16)
 52 
 53 struct dsmark_qdisc_data {
 54         struct Qdisc            *q;
 55         struct tcf_proto        *filter_list;
 56         __u8                    *mask;  /* "owns" the array */
 57         __u8                    *value;
 58         __u16                   indices;
 59         __u32                   default_index;  /* index range is 0...0xffff */
 60         int                     set_tc_index;
 61 };
 62 
 63 
 64 /* ------------------------- Class/flow operations ------------------------- */
 65 
 66 
 67 static int dsmark_graft(struct Qdisc *sch,unsigned long arg,
 68     struct Qdisc *new,struct Qdisc **old)
 69 {
 70         struct dsmark_qdisc_data *p = PRIV(sch);
 71 
 72         DPRINTK("dsmark_graft(sch %p,[qdisc %p],new %p,old %p)\n",sch,p,new,
 73             old);
 74         if (!new)
 75                 new = &noop_qdisc;
 76         sch_tree_lock(sch);
 77         *old = xchg(&p->q,new);
 78         if (*old)
 79                 qdisc_reset(*old);
 80         sch->q.qlen = 0;
 81         sch_tree_unlock(sch); /* @@@ move up ? */
 82         return 0;
 83 }
 84 
 85 
 86 static struct Qdisc *dsmark_leaf(struct Qdisc *sch, unsigned long arg)
 87 {
 88         struct dsmark_qdisc_data *p = PRIV(sch);
 89 
 90         return p->q;
 91 }
 92 
 93 
 94 static unsigned long dsmark_get(struct Qdisc *sch,u32 classid)
 95 {
 96         struct dsmark_qdisc_data *p __attribute__((unused)) = PRIV(sch);
 97 
 98         DPRINTK("dsmark_get(sch %p,[qdisc %p],classid %x)\n",sch,p,classid);
 99         return TC_H_MIN(classid)+1;
100 }
101 
102 
103 static unsigned long dsmark_bind_filter(struct Qdisc *sch,
104     unsigned long parent, u32 classid)
105 {
106         return dsmark_get(sch,classid);
107 }
108 
109 
110 static void dsmark_put(struct Qdisc *sch, unsigned long cl)
111 {
112 }
113 
114 
115 static int dsmark_change(struct Qdisc *sch, u32 classid, u32 parent,
116     struct rtattr **tca, unsigned long *arg)
117 {
118         struct dsmark_qdisc_data *p = PRIV(sch);
119         struct rtattr *opt = tca[TCA_OPTIONS-1];
120         struct rtattr *tb[TCA_DSMARK_MAX];
121 
122         DPRINTK("dsmark_change(sch %p,[qdisc %p],classid %x,parent %x),"
123             "arg 0x%lx\n",sch,p,classid,parent,*arg);
124         if (*arg > p->indices)
125                 return -ENOENT;
126         if (!opt || rtattr_parse(tb, TCA_DSMARK_MAX, RTA_DATA(opt),
127                                  RTA_PAYLOAD(opt)))
128                 return -EINVAL;
129         if (tb[TCA_DSMARK_MASK-1]) {
130                 if (!RTA_PAYLOAD(tb[TCA_DSMARK_MASK-1]))
131                         return -EINVAL;
132                 p->mask[*arg-1] = *(__u8 *) RTA_DATA(tb[TCA_DSMARK_MASK-1]);
133         }
134         if (tb[TCA_DSMARK_VALUE-1]) {
135                 if (!RTA_PAYLOAD(tb[TCA_DSMARK_VALUE-1]))
136                         return -EINVAL;
137                 p->value[*arg-1] = *(__u8 *) RTA_DATA(tb[TCA_DSMARK_VALUE-1]);
138         }
139         return 0;
140 }
141 
142 
143 static int dsmark_delete(struct Qdisc *sch,unsigned long arg)
144 {
145         struct dsmark_qdisc_data *p = PRIV(sch);
146 
147         if (!arg || arg > p->indices)
148                 return -EINVAL;
149         p->mask[arg-1] = 0xff;
150         p->value[arg-1] = 0;
151         return 0;
152 }
153 
154 
155 static void dsmark_walk(struct Qdisc *sch,struct qdisc_walker *walker)
156 {
157         struct dsmark_qdisc_data *p = PRIV(sch);
158         int i;
159 
160         DPRINTK("dsmark_walk(sch %p,[qdisc %p],walker %p)\n",sch,p,walker);
161         if (walker->stop)
162                 return;
163         for (i = 0; i < p->indices; i++) {
164                 if (p->mask[i] == 0xff && !p->value[i])
165                         continue;
166                 if (walker->count >= walker->skip) {
167                         if (walker->fn(sch, i+1, walker) < 0) {
168                                 walker->stop = 1;
169                                 break;
170                         }
171                 }
172                 walker->count++;
173         }
174 }
175 
176 
177 static struct tcf_proto **dsmark_find_tcf(struct Qdisc *sch,unsigned long cl)
178 {
179         struct dsmark_qdisc_data *p = PRIV(sch);
180 
181         return &p->filter_list;
182 }
183 
184 
185 /* --------------------------- Qdisc operations ---------------------------- */
186 
187 
188 static int dsmark_enqueue(struct sk_buff *skb,struct Qdisc *sch)
189 {
190         struct dsmark_qdisc_data *p = PRIV(sch);
191         struct tcf_result res;
192         int result;
193         int ret = NET_XMIT_POLICED;
194 
195         D2PRINTK("dsmark_enqueue(skb %p,sch %p,[qdisc %p])\n",skb,sch,p);
196         if (p->set_tc_index) {
197                 /* FIXME: Safe with non-linear skbs? --RR */
198                 switch (skb->protocol) {
199                         case __constant_htons(ETH_P_IP):
200                                 skb->tc_index = ipv4_get_dsfield(skb->nh.iph);
201                                 break;
202                         case __constant_htons(ETH_P_IPV6):
203                                 skb->tc_index = ipv6_get_dsfield(skb->nh.ipv6h);
204                                 break;
205                         default:
206                                 skb->tc_index = 0;
207                                 break;
208                 };
209         }
210         result = TC_POLICE_OK; /* be nice to gcc */
211         if (TC_H_MAJ(skb->priority) == sch->handle) {
212                 skb->tc_index = TC_H_MIN(skb->priority);
213         } else {
214                 result = tc_classify(skb,p->filter_list,&res);
215                 D2PRINTK("result %d class 0x%04x\n",result,res.classid);
216                 switch (result) {
217 #ifdef CONFIG_NET_CLS_POLICE
218                         case TC_POLICE_SHOT:
219                                 kfree_skb(skb);
220                                 break;
221 #if 0
222                         case TC_POLICE_RECLASSIFY:
223                                 /* FIXME: what to do here ??? */
224 #endif
225 #endif
226                         case TC_POLICE_OK:
227                                 skb->tc_index = TC_H_MIN(res.classid);
228                                 break;
229                         case TC_POLICE_UNSPEC:
230                                 /* fall through */
231                         default:
232                                 if (p->default_index != NO_DEFAULT_INDEX)
233                                         skb->tc_index = p->default_index;
234                                 break;
235                 };
236         }
237         if (
238 #ifdef CONFIG_NET_CLS_POLICE
239             result == TC_POLICE_SHOT ||
240 #endif
241 
242             ((ret = p->q->enqueue(skb,p->q)) != 0)) {
243                 sch->stats.drops++;
244                 return ret;
245         }
246         sch->stats.bytes += skb->len;
247         sch->stats.packets++;
248         sch->q.qlen++;
249         return ret;
250 }
251 
252 
253 static struct sk_buff *dsmark_dequeue(struct Qdisc *sch)
254 {
255         struct dsmark_qdisc_data *p = PRIV(sch);
256         struct sk_buff *skb;
257         int index;
258 
259         D2PRINTK("dsmark_dequeue(sch %p,[qdisc %p])\n",sch,p);
260         skb = p->q->ops->dequeue(p->q);
261         if (!skb)
262                 return NULL;
263         sch->q.qlen--;
264         index = skb->tc_index & (p->indices-1);
265         D2PRINTK("index %d->%d\n",skb->tc_index,index);
266         switch (skb->protocol) {
267                 case __constant_htons(ETH_P_IP):
268                         ipv4_change_dsfield(skb->nh.iph,
269                             p->mask[index],p->value[index]);
270                         break;
271                 case __constant_htons(ETH_P_IPV6):
272                         ipv6_change_dsfield(skb->nh.ipv6h,
273                             p->mask[index],p->value[index]);
274                         break;
275                 default:
276                         /*
277                          * Only complain if a change was actually attempted.
278                          * This way, we can send non-IP traffic through dsmark
279                          * and don't need yet another qdisc as a bypass.
280                          */
281                         if (p->mask[index] != 0xff || p->value[index])
282                                 printk(KERN_WARNING "dsmark_dequeue: "
283                                        "unsupported protocol %d\n",
284                                        htons(skb->protocol));
285                         break;
286         };
287         return skb;
288 }
289 
290 
291 static int dsmark_requeue(struct sk_buff *skb,struct Qdisc *sch)
292 {
293         int ret;
294         struct dsmark_qdisc_data *p = PRIV(sch);
295 
296         D2PRINTK("dsmark_requeue(skb %p,sch %p,[qdisc %p])\n",skb,sch,p);
297         if ((ret = p->q->ops->requeue(skb, p->q)) == 0) {
298                 sch->q.qlen++;
299                 return 0;
300         }
301         sch->stats.drops++;
302         return ret;
303 }
304 
305 
306 static unsigned int dsmark_drop(struct Qdisc *sch)
307 {
308         struct dsmark_qdisc_data *p = PRIV(sch);
309         unsigned int len;
310         
311         DPRINTK("dsmark_reset(sch %p,[qdisc %p])\n",sch,p);
312         if (!p->q->ops->drop)
313                 return 0;
314         if (!(len = p->q->ops->drop(p->q)))
315                 return 0;
316         sch->q.qlen--;
317         return len;
318 }
319 
320 
321 int dsmark_init(struct Qdisc *sch,struct rtattr *opt)
322 {
323         struct dsmark_qdisc_data *p = PRIV(sch);
324         struct rtattr *tb[TCA_DSMARK_MAX];
325         __u16 tmp;
326 
327         DPRINTK("dsmark_init(sch %p,[qdisc %p],opt %p)\n",sch,p,opt);
328         if (rtattr_parse(tb,TCA_DSMARK_MAX,RTA_DATA(opt),RTA_PAYLOAD(opt)) < 0 ||
329             !tb[TCA_DSMARK_INDICES-1] ||
330             RTA_PAYLOAD(tb[TCA_DSMARK_INDICES-1]) < sizeof(__u16))
331                 return -EINVAL;
332         memset(p,0,sizeof(*p));
333         p->filter_list = NULL;
334         p->indices = *(__u16 *) RTA_DATA(tb[TCA_DSMARK_INDICES-1]);
335         if (!p->indices)
336                 return -EINVAL;
337         for (tmp = p->indices; tmp != 1; tmp >>= 1) {
338                 if (tmp & 1)
339                         return -EINVAL;
340         }
341         p->default_index = NO_DEFAULT_INDEX;
342         if (tb[TCA_DSMARK_DEFAULT_INDEX-1]) {
343                 if (RTA_PAYLOAD(tb[TCA_DSMARK_DEFAULT_INDEX-1]) < sizeof(__u16))
344                         return -EINVAL;
345                 p->default_index =
346                     *(__u16 *) RTA_DATA(tb[TCA_DSMARK_DEFAULT_INDEX-1]);
347         }
348         p->set_tc_index = !!tb[TCA_DSMARK_SET_TC_INDEX-1];
349         p->mask = kmalloc(p->indices*2,GFP_KERNEL);
350         if (!p->mask)
351                 return -ENOMEM;
352         p->value = p->mask+p->indices;
353         memset(p->mask,0xff,p->indices);
354         memset(p->value,0,p->indices);
355         if (!(p->q = qdisc_create_dflt(sch->dev, &pfifo_qdisc_ops)))
356                 p->q = &noop_qdisc;
357         DPRINTK("dsmark_init: qdisc %p\n",&p->q);
358         return 0;
359 }
360 
361 
362 static void dsmark_reset(struct Qdisc *sch)
363 {
364         struct dsmark_qdisc_data *p = PRIV(sch);
365 
366         DPRINTK("dsmark_reset(sch %p,[qdisc %p])\n",sch,p);
367         qdisc_reset(p->q);
368         sch->q.qlen = 0;
369 }
370 
371 
372 static void dsmark_destroy(struct Qdisc *sch)
373 {
374         struct dsmark_qdisc_data *p = PRIV(sch);
375         struct tcf_proto *tp;
376 
377         DPRINTK("dsmark_destroy(sch %p,[qdisc %p])\n",sch,p);
378         while (p->filter_list) {
379                 tp = p->filter_list;
380                 p->filter_list = tp->next;
381                 tp->ops->destroy(tp);
382         }
383         qdisc_destroy(p->q);
384         p->q = &noop_qdisc;
385         kfree(p->mask);
386 }
387 
388 
389 static int dsmark_dump_class(struct Qdisc *sch, unsigned long cl,
390     struct sk_buff *skb, struct tcmsg *tcm)
391 {
392         struct dsmark_qdisc_data *p = PRIV(sch);
393         unsigned char *b = skb->tail;
394         struct rtattr *rta;
395 
396         DPRINTK("dsmark_dump_class(sch %p,[qdisc %p],class %ld\n",sch,p,cl);
397         if (!cl || cl > p->indices)
398                 return -EINVAL;
399         tcm->tcm_handle = TC_H_MAKE(TC_H_MAJ(sch->handle),cl-1);
400         rta = (struct rtattr *) b;
401         RTA_PUT(skb,TCA_OPTIONS,0,NULL);
402         RTA_PUT(skb,TCA_DSMARK_MASK,1,&p->mask[cl-1]);
403         RTA_PUT(skb,TCA_DSMARK_VALUE,1,&p->value[cl-1]);
404         rta->rta_len = skb->tail-b;
405         return skb->len;
406 
407 rtattr_failure:
408         skb_trim(skb,b-skb->data);
409         return -1;
410 }
411 
412 static int dsmark_dump(struct Qdisc *sch, struct sk_buff *skb)
413 {
414         struct dsmark_qdisc_data *p = PRIV(sch);
415         unsigned char *b = skb->tail;
416         struct rtattr *rta;
417 
418         rta = (struct rtattr *) b;
419         RTA_PUT(skb,TCA_OPTIONS,0,NULL);
420         RTA_PUT(skb,TCA_DSMARK_INDICES,sizeof(__u16),&p->indices);
421         if (p->default_index != NO_DEFAULT_INDEX) {
422                 __u16 tmp = p->default_index;
423 
424                 RTA_PUT(skb,TCA_DSMARK_DEFAULT_INDEX, sizeof(__u16), &tmp);
425         }
426         if (p->set_tc_index)
427                 RTA_PUT(skb, TCA_DSMARK_SET_TC_INDEX, 0, NULL);
428         rta->rta_len = skb->tail-b;
429         return skb->len;
430 
431 rtattr_failure:
432         skb_trim(skb,b-skb->data);
433         return -1;
434 }
435 
436 static struct Qdisc_class_ops dsmark_class_ops = {
437         .graft          =       dsmark_graft,
438         .leaf           =       dsmark_leaf,
439         .get            =       dsmark_get,
440         .put            =       dsmark_put,
441         .change         =       dsmark_change,
442         .delete         =       dsmark_delete,
443         .walk           =       dsmark_walk,
444         .tcf_chain      =       dsmark_find_tcf,
445         .bind_tcf       =       dsmark_bind_filter,
446         .unbind_tcf     =       dsmark_put,
447         .dump           =       dsmark_dump_class,
448 };
449 
450 struct Qdisc_ops dsmark_qdisc_ops = {
451         .next           =       NULL,
452         .cl_ops         =       &dsmark_class_ops,
453         .id             =       "dsmark",
454         .priv_size      =       sizeof(struct dsmark_qdisc_data),
455         .enqueue        =       dsmark_enqueue,
456         .dequeue        =       dsmark_dequeue,
457         .requeue        =       dsmark_requeue,
458         .drop           =       dsmark_drop,
459         .init           =       dsmark_init,
460         .reset          =       dsmark_reset,
461         .destroy        =       dsmark_destroy,
462         .change         =       NULL,
463         .dump           =       dsmark_dump,
464         .owner          =       THIS_MODULE,
465 };
466 
467 #ifdef MODULE
468 int init_module(void)
469 {
470         return register_qdisc(&dsmark_qdisc_ops);
471 }
472 
473 
474 void cleanup_module(void) 
475 {
476         unregister_qdisc(&dsmark_qdisc_ops);
477 }
478 #endif
479 MODULE_LICENSE("GPL");
480 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp