pkt_act.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. #ifndef __NET_PKT_ACT_H
  2. #define __NET_PKT_ACT_H
  3. #include <asm/uaccess.h>
  4. #include <asm/system.h>
  5. #include <linux/bitops.h>
  6. #include <linux/config.h>
  7. #include <linux/types.h>
  8. #include <linux/kernel.h>
  9. #include <linux/sched.h>
  10. #include <linux/string.h>
  11. #include <linux/mm.h>
  12. #include <linux/socket.h>
  13. #include <linux/sockios.h>
  14. #include <linux/in.h>
  15. #include <linux/errno.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/netdevice.h>
  18. #include <linux/skbuff.h>
  19. #include <linux/rtnetlink.h>
  20. #include <linux/module.h>
  21. #include <linux/init.h>
  22. #include <linux/proc_fs.h>
  23. #include <net/sock.h>
  24. #include <net/pkt_sched.h>
  25. #define tca_st(val) (struct tcf_##val *)
  26. #define PRIV(a,name) ( tca_st(name) (a)->priv)
  27. #if 0 /* control */
  28. #define DPRINTK(format,args...) printk(KERN_DEBUG format,##args)
  29. #else
  30. #define DPRINTK(format,args...)
  31. #endif
  32. #if 0 /* data */
  33. #define D2PRINTK(format,args...) printk(KERN_DEBUG format,##args)
  34. #else
  35. #define D2PRINTK(format,args...)
  36. #endif
  37. static __inline__ unsigned
  38. tcf_hash(u32 index)
  39. {
  40. return index & MY_TAB_MASK;
  41. }
  42. /* probably move this from being inline
  43. * and put into act_generic
  44. */
  45. static inline void
  46. tcf_hash_destroy(struct tcf_st *p)
  47. {
  48. unsigned h = tcf_hash(p->index);
  49. struct tcf_st **p1p;
  50. for (p1p = &tcf_ht[h]; *p1p; p1p = &(*p1p)->next) {
  51. if (*p1p == p) {
  52. write_lock_bh(&tcf_t_lock);
  53. *p1p = p->next;
  54. write_unlock_bh(&tcf_t_lock);
  55. #ifdef CONFIG_NET_ESTIMATOR
  56. gen_kill_estimator(&p->bstats, &p->rate_est);
  57. #endif
  58. kfree(p);
  59. return;
  60. }
  61. }
  62. BUG_TRAP(0);
  63. }
  64. static inline int
  65. tcf_hash_release(struct tcf_st *p, int bind )
  66. {
  67. int ret = 0;
  68. if (p) {
  69. if (bind) {
  70. p->bindcnt--;
  71. }
  72. p->refcnt--;
  73. if(p->bindcnt <=0 && p->refcnt <= 0) {
  74. tcf_hash_destroy(p);
  75. ret = 1;
  76. }
  77. }
  78. return ret;
  79. }
  80. static __inline__ int
  81. tcf_dump_walker(struct sk_buff *skb, struct netlink_callback *cb,
  82. struct tc_action *a)
  83. {
  84. struct tcf_st *p;
  85. int err =0, index = -1,i= 0, s_i = 0, n_i = 0;
  86. struct rtattr *r ;
  87. read_lock(&tcf_t_lock);
  88. s_i = cb->args[0];
  89. for (i = 0; i < MY_TAB_SIZE; i++) {
  90. p = tcf_ht[tcf_hash(i)];
  91. for (; p; p = p->next) {
  92. index++;
  93. if (index < s_i)
  94. continue;
  95. a->priv = p;
  96. a->order = n_i;
  97. r = (struct rtattr*) skb->tail;
  98. RTA_PUT(skb, a->order, 0, NULL);
  99. err = tcf_action_dump_1(skb, a, 0, 0);
  100. if (0 > err) {
  101. index--;
  102. skb_trim(skb, (u8*)r - skb->data);
  103. goto done;
  104. }
  105. r->rta_len = skb->tail - (u8*)r;
  106. n_i++;
  107. if (n_i >= TCA_ACT_MAX_PRIO) {
  108. goto done;
  109. }
  110. }
  111. }
  112. done:
  113. read_unlock(&tcf_t_lock);
  114. if (n_i)
  115. cb->args[0] += n_i;
  116. return n_i;
  117. rtattr_failure:
  118. skb_trim(skb, (u8*)r - skb->data);
  119. goto done;
  120. }
  121. static __inline__ int
  122. tcf_del_walker(struct sk_buff *skb, struct tc_action *a)
  123. {
  124. struct tcf_st *p, *s_p;
  125. struct rtattr *r ;
  126. int i= 0, n_i = 0;
  127. r = (struct rtattr*) skb->tail;
  128. RTA_PUT(skb, a->order, 0, NULL);
  129. RTA_PUT(skb, TCA_KIND, IFNAMSIZ, a->ops->kind);
  130. for (i = 0; i < MY_TAB_SIZE; i++) {
  131. p = tcf_ht[tcf_hash(i)];
  132. while (p != NULL) {
  133. s_p = p->next;
  134. if (ACT_P_DELETED == tcf_hash_release(p, 0)) {
  135. module_put(a->ops->owner);
  136. }
  137. n_i++;
  138. p = s_p;
  139. }
  140. }
  141. RTA_PUT(skb, TCA_FCNT, 4, &n_i);
  142. r->rta_len = skb->tail - (u8*)r;
  143. return n_i;
  144. rtattr_failure:
  145. skb_trim(skb, (u8*)r - skb->data);
  146. return -EINVAL;
  147. }
  148. static __inline__ int
  149. tcf_generic_walker(struct sk_buff *skb, struct netlink_callback *cb, int type,
  150. struct tc_action *a)
  151. {
  152. if (type == RTM_DELACTION) {
  153. return tcf_del_walker(skb,a);
  154. } else if (type == RTM_GETACTION) {
  155. return tcf_dump_walker(skb,cb,a);
  156. } else {
  157. printk("tcf_generic_walker: unknown action %d\n",type);
  158. return -EINVAL;
  159. }
  160. }
  161. static __inline__ struct tcf_st *
  162. tcf_hash_lookup(u32 index)
  163. {
  164. struct tcf_st *p;
  165. read_lock(&tcf_t_lock);
  166. for (p = tcf_ht[tcf_hash(index)]; p; p = p->next) {
  167. if (p->index == index)
  168. break;
  169. }
  170. read_unlock(&tcf_t_lock);
  171. return p;
  172. }
  173. static __inline__ u32
  174. tcf_hash_new_index(void)
  175. {
  176. do {
  177. if (++idx_gen == 0)
  178. idx_gen = 1;
  179. } while (tcf_hash_lookup(idx_gen));
  180. return idx_gen;
  181. }
  182. static inline int
  183. tcf_hash_search(struct tc_action *a, u32 index)
  184. {
  185. struct tcf_st *p = tcf_hash_lookup(index);
  186. if (p != NULL) {
  187. a->priv = p;
  188. return 1;
  189. }
  190. return 0;
  191. }
  192. #ifdef CONFIG_NET_ACT_INIT
  193. static inline struct tcf_st *
  194. tcf_hash_check(u32 index, struct tc_action *a, int ovr, int bind)
  195. {
  196. struct tcf_st *p = NULL;
  197. if (index && (p = tcf_hash_lookup(index)) != NULL) {
  198. if (bind) {
  199. p->bindcnt++;
  200. p->refcnt++;
  201. }
  202. a->priv = p;
  203. }
  204. return p;
  205. }
  206. static inline struct tcf_st *
  207. tcf_hash_create(u32 index, struct rtattr *est, struct tc_action *a, int size, int ovr, int bind)
  208. {
  209. struct tcf_st *p = NULL;
  210. p = kmalloc(size, GFP_KERNEL);
  211. if (p == NULL)
  212. return p;
  213. memset(p, 0, size);
  214. p->refcnt = 1;
  215. if (bind) {
  216. p->bindcnt = 1;
  217. }
  218. spin_lock_init(&p->lock);
  219. p->stats_lock = &p->lock;
  220. p->index = index ? : tcf_hash_new_index();
  221. p->tm.install = jiffies;
  222. p->tm.lastuse = jiffies;
  223. #ifdef CONFIG_NET_ESTIMATOR
  224. if (est)
  225. gen_new_estimator(&p->bstats, &p->rate_est, p->stats_lock, est);
  226. #endif
  227. a->priv = (void *) p;
  228. return p;
  229. }
  230. static inline void tcf_hash_insert(struct tcf_st *p)
  231. {
  232. unsigned h = tcf_hash(p->index);
  233. write_lock_bh(&tcf_t_lock);
  234. p->next = tcf_ht[h];
  235. tcf_ht[h] = p;
  236. write_unlock_bh(&tcf_t_lock);
  237. }
  238. #endif
  239. #endif