enic_clsf.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. #include <linux/if.h>
  2. #include <linux/if_ether.h>
  3. #include <linux/if_link.h>
  4. #include <linux/netdevice.h>
  5. #include <linux/in.h>
  6. #include <linux/types.h>
  7. #include <linux/skbuff.h>
  8. #include <net/flow_dissector.h>
  9. #include "enic_res.h"
  10. #include "enic_clsf.h"
  11. /* enic_addfltr_5t - Add ipv4 5tuple filter
  12. * @enic: enic struct of vnic
  13. * @keys: flow_keys of ipv4 5tuple
  14. * @rq: rq number to steer to
  15. *
  16. * This function returns filter_id(hardware_id) of the filter
  17. * added. In case of error it returns a negative number.
  18. */
  19. int enic_addfltr_5t(struct enic *enic, struct flow_keys *keys, u16 rq)
  20. {
  21. int res;
  22. struct filter data;
  23. switch (keys->basic.ip_proto) {
  24. case IPPROTO_TCP:
  25. data.u.ipv4.protocol = PROTO_TCP;
  26. break;
  27. case IPPROTO_UDP:
  28. data.u.ipv4.protocol = PROTO_UDP;
  29. break;
  30. default:
  31. return -EPROTONOSUPPORT;
  32. };
  33. data.type = FILTER_IPV4_5TUPLE;
  34. data.u.ipv4.src_addr = ntohl(keys->addrs.v4addrs.src);
  35. data.u.ipv4.dst_addr = ntohl(keys->addrs.v4addrs.dst);
  36. data.u.ipv4.src_port = ntohs(keys->ports.src);
  37. data.u.ipv4.dst_port = ntohs(keys->ports.dst);
  38. data.u.ipv4.flags = FILTER_FIELDS_IPV4_5TUPLE;
  39. spin_lock_bh(&enic->devcmd_lock);
  40. res = vnic_dev_classifier(enic->vdev, CLSF_ADD, &rq, &data);
  41. spin_unlock_bh(&enic->devcmd_lock);
  42. res = (res == 0) ? rq : res;
  43. return res;
  44. }
  45. /* enic_delfltr - Delete clsf filter
  46. * @enic: enic struct of vnic
  47. * @filter_id: filter_is(hardware_id) of filter to be deleted
  48. *
  49. * This function returns zero in case of success, negative number incase of
  50. * error.
  51. */
  52. int enic_delfltr(struct enic *enic, u16 filter_id)
  53. {
  54. int ret;
  55. spin_lock_bh(&enic->devcmd_lock);
  56. ret = vnic_dev_classifier(enic->vdev, CLSF_DEL, &filter_id, NULL);
  57. spin_unlock_bh(&enic->devcmd_lock);
  58. return ret;
  59. }
  60. /* enic_rfs_flw_tbl_init - initialize enic->rfs_h members
  61. * @enic: enic data
  62. */
  63. void enic_rfs_flw_tbl_init(struct enic *enic)
  64. {
  65. int i;
  66. spin_lock_init(&enic->rfs_h.lock);
  67. for (i = 0; i <= ENIC_RFS_FLW_MASK; i++)
  68. INIT_HLIST_HEAD(&enic->rfs_h.ht_head[i]);
  69. enic->rfs_h.max = enic->config.num_arfs;
  70. enic->rfs_h.free = enic->rfs_h.max;
  71. enic->rfs_h.toclean = 0;
  72. }
  73. void enic_rfs_flw_tbl_free(struct enic *enic)
  74. {
  75. int i;
  76. enic_rfs_timer_stop(enic);
  77. spin_lock_bh(&enic->rfs_h.lock);
  78. for (i = 0; i < (1 << ENIC_RFS_FLW_BITSHIFT); i++) {
  79. struct hlist_head *hhead;
  80. struct hlist_node *tmp;
  81. struct enic_rfs_fltr_node *n;
  82. hhead = &enic->rfs_h.ht_head[i];
  83. hlist_for_each_entry_safe(n, tmp, hhead, node) {
  84. enic_delfltr(enic, n->fltr_id);
  85. hlist_del(&n->node);
  86. kfree(n);
  87. enic->rfs_h.free++;
  88. }
  89. }
  90. spin_unlock_bh(&enic->rfs_h.lock);
  91. }
  92. struct enic_rfs_fltr_node *htbl_fltr_search(struct enic *enic, u16 fltr_id)
  93. {
  94. int i;
  95. for (i = 0; i < (1 << ENIC_RFS_FLW_BITSHIFT); i++) {
  96. struct hlist_head *hhead;
  97. struct hlist_node *tmp;
  98. struct enic_rfs_fltr_node *n;
  99. hhead = &enic->rfs_h.ht_head[i];
  100. hlist_for_each_entry_safe(n, tmp, hhead, node)
  101. if (n->fltr_id == fltr_id)
  102. return n;
  103. }
  104. return NULL;
  105. }
  106. #ifdef CONFIG_RFS_ACCEL
  107. void enic_flow_may_expire(unsigned long data)
  108. {
  109. struct enic *enic = (struct enic *)data;
  110. bool res;
  111. int j;
  112. spin_lock_bh(&enic->rfs_h.lock);
  113. for (j = 0; j < ENIC_CLSF_EXPIRE_COUNT; j++) {
  114. struct hlist_head *hhead;
  115. struct hlist_node *tmp;
  116. struct enic_rfs_fltr_node *n;
  117. hhead = &enic->rfs_h.ht_head[enic->rfs_h.toclean++];
  118. hlist_for_each_entry_safe(n, tmp, hhead, node) {
  119. res = rps_may_expire_flow(enic->netdev, n->rq_id,
  120. n->flow_id, n->fltr_id);
  121. if (res) {
  122. res = enic_delfltr(enic, n->fltr_id);
  123. if (unlikely(res))
  124. continue;
  125. hlist_del(&n->node);
  126. kfree(n);
  127. enic->rfs_h.free++;
  128. }
  129. }
  130. }
  131. spin_unlock_bh(&enic->rfs_h.lock);
  132. mod_timer(&enic->rfs_h.rfs_may_expire, jiffies + HZ/4);
  133. }
  134. static struct enic_rfs_fltr_node *htbl_key_search(struct hlist_head *h,
  135. struct flow_keys *k)
  136. {
  137. struct enic_rfs_fltr_node *tpos;
  138. hlist_for_each_entry(tpos, h, node)
  139. if (tpos->keys.addrs.v4addrs.src == k->addrs.v4addrs.src &&
  140. tpos->keys.addrs.v4addrs.dst == k->addrs.v4addrs.dst &&
  141. tpos->keys.ports.ports == k->ports.ports &&
  142. tpos->keys.basic.ip_proto == k->basic.ip_proto &&
  143. tpos->keys.basic.n_proto == k->basic.n_proto)
  144. return tpos;
  145. return NULL;
  146. }
  147. int enic_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
  148. u16 rxq_index, u32 flow_id)
  149. {
  150. struct flow_keys keys;
  151. struct enic_rfs_fltr_node *n;
  152. struct enic *enic;
  153. u16 tbl_idx;
  154. int res, i;
  155. enic = netdev_priv(dev);
  156. res = skb_flow_dissect_flow_keys(skb, &keys, 0);
  157. if (!res || keys.basic.n_proto != htons(ETH_P_IP) ||
  158. (keys.basic.ip_proto != IPPROTO_TCP &&
  159. keys.basic.ip_proto != IPPROTO_UDP))
  160. return -EPROTONOSUPPORT;
  161. tbl_idx = skb_get_hash_raw(skb) & ENIC_RFS_FLW_MASK;
  162. spin_lock_bh(&enic->rfs_h.lock);
  163. n = htbl_key_search(&enic->rfs_h.ht_head[tbl_idx], &keys);
  164. if (n) { /* entry already present */
  165. if (rxq_index == n->rq_id) {
  166. res = -EEXIST;
  167. goto ret_unlock;
  168. }
  169. /* desired rq changed for the flow, we need to delete
  170. * old fltr and add new one
  171. *
  172. * The moment we delete the fltr, the upcoming pkts
  173. * are put it default rq based on rss. When we add
  174. * new filter, upcoming pkts are put in desired queue.
  175. * This could cause ooo pkts.
  176. *
  177. * Lets 1st try adding new fltr and then del old one.
  178. */
  179. i = --enic->rfs_h.free;
  180. /* clsf tbl is full, we have to del old fltr first*/
  181. if (unlikely(i < 0)) {
  182. enic->rfs_h.free++;
  183. res = enic_delfltr(enic, n->fltr_id);
  184. if (unlikely(res < 0))
  185. goto ret_unlock;
  186. res = enic_addfltr_5t(enic, &keys, rxq_index);
  187. if (res < 0) {
  188. hlist_del(&n->node);
  189. enic->rfs_h.free++;
  190. goto ret_unlock;
  191. }
  192. /* add new fltr 1st then del old fltr */
  193. } else {
  194. int ret;
  195. res = enic_addfltr_5t(enic, &keys, rxq_index);
  196. if (res < 0) {
  197. enic->rfs_h.free++;
  198. goto ret_unlock;
  199. }
  200. ret = enic_delfltr(enic, n->fltr_id);
  201. /* deleting old fltr failed. Add old fltr to list.
  202. * enic_flow_may_expire() will try to delete it later.
  203. */
  204. if (unlikely(ret < 0)) {
  205. struct enic_rfs_fltr_node *d;
  206. struct hlist_head *head;
  207. head = &enic->rfs_h.ht_head[tbl_idx];
  208. d = kmalloc(sizeof(*d), GFP_ATOMIC);
  209. if (d) {
  210. d->fltr_id = n->fltr_id;
  211. INIT_HLIST_NODE(&d->node);
  212. hlist_add_head(&d->node, head);
  213. }
  214. } else {
  215. enic->rfs_h.free++;
  216. }
  217. }
  218. n->rq_id = rxq_index;
  219. n->fltr_id = res;
  220. n->flow_id = flow_id;
  221. /* entry not present */
  222. } else {
  223. i = --enic->rfs_h.free;
  224. if (i <= 0) {
  225. enic->rfs_h.free++;
  226. res = -EBUSY;
  227. goto ret_unlock;
  228. }
  229. n = kmalloc(sizeof(*n), GFP_ATOMIC);
  230. if (!n) {
  231. res = -ENOMEM;
  232. enic->rfs_h.free++;
  233. goto ret_unlock;
  234. }
  235. res = enic_addfltr_5t(enic, &keys, rxq_index);
  236. if (res < 0) {
  237. kfree(n);
  238. enic->rfs_h.free++;
  239. goto ret_unlock;
  240. }
  241. n->rq_id = rxq_index;
  242. n->fltr_id = res;
  243. n->flow_id = flow_id;
  244. n->keys = keys;
  245. INIT_HLIST_NODE(&n->node);
  246. hlist_add_head(&n->node, &enic->rfs_h.ht_head[tbl_idx]);
  247. }
  248. ret_unlock:
  249. spin_unlock_bh(&enic->rfs_h.lock);
  250. return res;
  251. }
  252. #endif /* CONFIG_RFS_ACCEL */