enic_clsf.c 6.8 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_keys.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 an 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->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->src);
  35. data.u.ipv4.dst_addr = ntohl(keys->dst);
  36. data.u.ipv4.src_port = ntohs(keys->port16[0]);
  37. data.u.ipv4.dst_port = ntohs(keys->port16[1]);
  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.src == k->src &&
  141. tpos->keys.dst == k->dst &&
  142. tpos->keys.ports == k->ports &&
  143. tpos->keys.ip_proto == k->ip_proto &&
  144. tpos->keys.n_proto == k->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(skb, &keys);
  158. if (!res || keys.n_proto != htons(ETH_P_IP) ||
  159. (keys.ip_proto != IPPROTO_TCP && keys.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 */