enic_clsf.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. enic_rfs_timer_start(enic);
  73. }
  74. void enic_rfs_flw_tbl_free(struct enic *enic)
  75. {
  76. int i;
  77. enic_rfs_timer_stop(enic);
  78. spin_lock_bh(&enic->rfs_h.lock);
  79. enic->rfs_h.free = 0;
  80. for (i = 0; i < (1 << ENIC_RFS_FLW_BITSHIFT); i++) {
  81. struct hlist_head *hhead;
  82. struct hlist_node *tmp;
  83. struct enic_rfs_fltr_node *n;
  84. hhead = &enic->rfs_h.ht_head[i];
  85. hlist_for_each_entry_safe(n, tmp, hhead, node) {
  86. enic_delfltr(enic, n->fltr_id);
  87. hlist_del(&n->node);
  88. kfree(n);
  89. }
  90. }
  91. spin_unlock_bh(&enic->rfs_h.lock);
  92. }
  93. struct enic_rfs_fltr_node *htbl_fltr_search(struct enic *enic, u16 fltr_id)
  94. {
  95. int i;
  96. for (i = 0; i < (1 << ENIC_RFS_FLW_BITSHIFT); i++) {
  97. struct hlist_head *hhead;
  98. struct hlist_node *tmp;
  99. struct enic_rfs_fltr_node *n;
  100. hhead = &enic->rfs_h.ht_head[i];
  101. hlist_for_each_entry_safe(n, tmp, hhead, node)
  102. if (n->fltr_id == fltr_id)
  103. return n;
  104. }
  105. return NULL;
  106. }
  107. #ifdef CONFIG_RFS_ACCEL
  108. void enic_flow_may_expire(unsigned long data)
  109. {
  110. struct enic *enic = (struct enic *)data;
  111. bool res;
  112. int j;
  113. spin_lock_bh(&enic->rfs_h.lock);
  114. for (j = 0; j < ENIC_CLSF_EXPIRE_COUNT; j++) {
  115. struct hlist_head *hhead;
  116. struct hlist_node *tmp;
  117. struct enic_rfs_fltr_node *n;
  118. hhead = &enic->rfs_h.ht_head[enic->rfs_h.toclean++];
  119. hlist_for_each_entry_safe(n, tmp, hhead, node) {
  120. res = rps_may_expire_flow(enic->netdev, n->rq_id,
  121. n->flow_id, n->fltr_id);
  122. if (res) {
  123. res = enic_delfltr(enic, n->fltr_id);
  124. if (unlikely(res))
  125. continue;
  126. hlist_del(&n->node);
  127. kfree(n);
  128. enic->rfs_h.free++;
  129. }
  130. }
  131. }
  132. spin_unlock_bh(&enic->rfs_h.lock);
  133. mod_timer(&enic->rfs_h.rfs_may_expire, jiffies + HZ/4);
  134. }
  135. static struct enic_rfs_fltr_node *htbl_key_search(struct hlist_head *h,
  136. struct flow_keys *k)
  137. {
  138. struct enic_rfs_fltr_node *tpos;
  139. hlist_for_each_entry(tpos, h, node)
  140. if (tpos->keys.addrs.v4addrs.src == k->addrs.v4addrs.src &&
  141. tpos->keys.addrs.v4addrs.dst == k->addrs.v4addrs.dst &&
  142. tpos->keys.ports.ports == k->ports.ports &&
  143. tpos->keys.basic.ip_proto == k->basic.ip_proto &&
  144. tpos->keys.basic.n_proto == k->basic.n_proto)
  145. return tpos;
  146. return NULL;
  147. }
  148. int enic_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
  149. u16 rxq_index, u32 flow_id)
  150. {
  151. struct flow_keys keys;
  152. struct enic_rfs_fltr_node *n;
  153. struct enic *enic;
  154. u16 tbl_idx;
  155. int res, i;
  156. enic = netdev_priv(dev);
  157. res = skb_flow_dissect_flow_keys(skb, &keys, 0);
  158. if (!res || keys.basic.n_proto != htons(ETH_P_IP) ||
  159. (keys.basic.ip_proto != IPPROTO_TCP &&
  160. keys.basic.ip_proto != IPPROTO_UDP))
  161. return -EPROTONOSUPPORT;
  162. tbl_idx = skb_get_hash_raw(skb) & ENIC_RFS_FLW_MASK;
  163. spin_lock_bh(&enic->rfs_h.lock);
  164. n = htbl_key_search(&enic->rfs_h.ht_head[tbl_idx], &keys);
  165. if (n) { /* entry already present */
  166. if (rxq_index == n->rq_id) {
  167. res = -EEXIST;
  168. goto ret_unlock;
  169. }
  170. /* desired rq changed for the flow, we need to delete
  171. * old fltr and add new one
  172. *
  173. * The moment we delete the fltr, the upcoming pkts
  174. * are put it default rq based on rss. When we add
  175. * new filter, upcoming pkts are put in desired queue.
  176. * This could cause ooo pkts.
  177. *
  178. * Lets 1st try adding new fltr and then del old one.
  179. */
  180. i = --enic->rfs_h.free;
  181. /* clsf tbl is full, we have to del old fltr first*/
  182. if (unlikely(i < 0)) {
  183. enic->rfs_h.free++;
  184. res = enic_delfltr(enic, n->fltr_id);
  185. if (unlikely(res < 0))
  186. goto ret_unlock;
  187. res = enic_addfltr_5t(enic, &keys, rxq_index);
  188. if (res < 0) {
  189. hlist_del(&n->node);
  190. enic->rfs_h.free++;
  191. goto ret_unlock;
  192. }
  193. /* add new fltr 1st then del old fltr */
  194. } else {
  195. int ret;
  196. res = enic_addfltr_5t(enic, &keys, rxq_index);
  197. if (res < 0) {
  198. enic->rfs_h.free++;
  199. goto ret_unlock;
  200. }
  201. ret = enic_delfltr(enic, n->fltr_id);
  202. /* deleting old fltr failed. Add old fltr to list.
  203. * enic_flow_may_expire() will try to delete it later.
  204. */
  205. if (unlikely(ret < 0)) {
  206. struct enic_rfs_fltr_node *d;
  207. struct hlist_head *head;
  208. head = &enic->rfs_h.ht_head[tbl_idx];
  209. d = kmalloc(sizeof(*d), GFP_ATOMIC);
  210. if (d) {
  211. d->fltr_id = n->fltr_id;
  212. INIT_HLIST_NODE(&d->node);
  213. hlist_add_head(&d->node, head);
  214. }
  215. } else {
  216. enic->rfs_h.free++;
  217. }
  218. }
  219. n->rq_id = rxq_index;
  220. n->fltr_id = res;
  221. n->flow_id = flow_id;
  222. /* entry not present */
  223. } else {
  224. i = --enic->rfs_h.free;
  225. if (i <= 0) {
  226. enic->rfs_h.free++;
  227. res = -EBUSY;
  228. goto ret_unlock;
  229. }
  230. n = kmalloc(sizeof(*n), GFP_ATOMIC);
  231. if (!n) {
  232. res = -ENOMEM;
  233. enic->rfs_h.free++;
  234. goto ret_unlock;
  235. }
  236. res = enic_addfltr_5t(enic, &keys, rxq_index);
  237. if (res < 0) {
  238. kfree(n);
  239. enic->rfs_h.free++;
  240. goto ret_unlock;
  241. }
  242. n->rq_id = rxq_index;
  243. n->fltr_id = res;
  244. n->flow_id = flow_id;
  245. n->keys = keys;
  246. INIT_HLIST_NODE(&n->node);
  247. hlist_add_head(&n->node, &enic->rfs_h.ht_head[tbl_idx]);
  248. }
  249. ret_unlock:
  250. spin_unlock_bh(&enic->rfs_h.lock);
  251. return res;
  252. }
  253. #endif /* CONFIG_RFS_ACCEL */