pkt_act.h 5.1 KB

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