enic_clsf.c 7.0 KB

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