pkt_act.h 5.1 KB

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