flow.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
  2. /*
  3. * Copyright (c) 2018, Mellanox Technologies inc. All rights reserved.
  4. */
  5. #include <rdma/ib_user_verbs.h>
  6. #include <rdma/ib_verbs.h>
  7. #include <rdma/uverbs_types.h>
  8. #include <rdma/uverbs_ioctl.h>
  9. #include <rdma/mlx5_user_ioctl_cmds.h>
  10. #include <rdma/ib_umem.h>
  11. #include <linux/mlx5/driver.h>
  12. #include <linux/mlx5/fs.h>
  13. #include "mlx5_ib.h"
  14. #define UVERBS_MODULE_NAME mlx5_ib
  15. #include <rdma/uverbs_named_ioctl.h>
  16. static const struct uverbs_attr_spec mlx5_ib_flow_type[] = {
  17. [MLX5_IB_FLOW_TYPE_NORMAL] = {
  18. .type = UVERBS_ATTR_TYPE_PTR_IN,
  19. .u.ptr = {
  20. .len = sizeof(u16), /* data is priority */
  21. .min_len = sizeof(u16),
  22. }
  23. },
  24. [MLX5_IB_FLOW_TYPE_SNIFFER] = {
  25. .type = UVERBS_ATTR_TYPE_PTR_IN,
  26. UVERBS_ATTR_NO_DATA(),
  27. },
  28. [MLX5_IB_FLOW_TYPE_ALL_DEFAULT] = {
  29. .type = UVERBS_ATTR_TYPE_PTR_IN,
  30. UVERBS_ATTR_NO_DATA(),
  31. },
  32. [MLX5_IB_FLOW_TYPE_MC_DEFAULT] = {
  33. .type = UVERBS_ATTR_TYPE_PTR_IN,
  34. UVERBS_ATTR_NO_DATA(),
  35. },
  36. };
  37. static int UVERBS_HANDLER(MLX5_IB_METHOD_CREATE_FLOW)(
  38. struct ib_uverbs_file *file, struct uverbs_attr_bundle *attrs)
  39. {
  40. struct mlx5_ib_flow_handler *flow_handler;
  41. struct mlx5_ib_flow_matcher *fs_matcher;
  42. void *devx_obj;
  43. int dest_id, dest_type;
  44. void *cmd_in;
  45. int inlen;
  46. bool dest_devx, dest_qp;
  47. struct ib_qp *qp = NULL;
  48. struct ib_uobject *uobj =
  49. uverbs_attr_get_uobject(attrs, MLX5_IB_ATTR_CREATE_FLOW_HANDLE);
  50. struct mlx5_ib_dev *dev = to_mdev(uobj->context->device);
  51. if (!capable(CAP_NET_RAW))
  52. return -EPERM;
  53. dest_devx =
  54. uverbs_attr_is_valid(attrs, MLX5_IB_ATTR_CREATE_FLOW_DEST_DEVX);
  55. dest_qp = uverbs_attr_is_valid(attrs,
  56. MLX5_IB_ATTR_CREATE_FLOW_DEST_QP);
  57. if ((dest_devx && dest_qp) || (!dest_devx && !dest_qp))
  58. return -EINVAL;
  59. if (dest_devx) {
  60. devx_obj = uverbs_attr_get_obj(
  61. attrs, MLX5_IB_ATTR_CREATE_FLOW_DEST_DEVX);
  62. if (IS_ERR(devx_obj))
  63. return PTR_ERR(devx_obj);
  64. /* Verify that the given DEVX object is a flow
  65. * steering destination.
  66. */
  67. if (!mlx5_ib_devx_is_flow_dest(devx_obj, &dest_id, &dest_type))
  68. return -EINVAL;
  69. } else {
  70. struct mlx5_ib_qp *mqp;
  71. qp = uverbs_attr_get_obj(attrs,
  72. MLX5_IB_ATTR_CREATE_FLOW_DEST_QP);
  73. if (IS_ERR(qp))
  74. return PTR_ERR(qp);
  75. if (qp->qp_type != IB_QPT_RAW_PACKET)
  76. return -EINVAL;
  77. mqp = to_mqp(qp);
  78. if (mqp->flags & MLX5_IB_QP_RSS)
  79. dest_id = mqp->rss_qp.tirn;
  80. else
  81. dest_id = mqp->raw_packet_qp.rq.tirn;
  82. dest_type = MLX5_FLOW_DESTINATION_TYPE_TIR;
  83. }
  84. if (dev->rep)
  85. return -ENOTSUPP;
  86. cmd_in = uverbs_attr_get_alloced_ptr(
  87. attrs, MLX5_IB_ATTR_CREATE_FLOW_MATCH_VALUE);
  88. inlen = uverbs_attr_get_len(attrs,
  89. MLX5_IB_ATTR_CREATE_FLOW_MATCH_VALUE);
  90. fs_matcher = uverbs_attr_get_obj(attrs,
  91. MLX5_IB_ATTR_CREATE_FLOW_MATCHER);
  92. flow_handler = mlx5_ib_raw_fs_rule_add(dev, fs_matcher, cmd_in, inlen,
  93. dest_id, dest_type);
  94. if (IS_ERR(flow_handler))
  95. return PTR_ERR(flow_handler);
  96. ib_set_flow(uobj, &flow_handler->ibflow, qp, &dev->ib_dev);
  97. return 0;
  98. }
  99. static int flow_matcher_cleanup(struct ib_uobject *uobject,
  100. enum rdma_remove_reason why)
  101. {
  102. struct mlx5_ib_flow_matcher *obj = uobject->object;
  103. int ret;
  104. ret = ib_destroy_usecnt(&obj->usecnt, why, uobject);
  105. if (ret)
  106. return ret;
  107. kfree(obj);
  108. return 0;
  109. }
  110. static int UVERBS_HANDLER(MLX5_IB_METHOD_FLOW_MATCHER_CREATE)(
  111. struct ib_uverbs_file *file, struct uverbs_attr_bundle *attrs)
  112. {
  113. struct ib_uobject *uobj = uverbs_attr_get_uobject(
  114. attrs, MLX5_IB_ATTR_FLOW_MATCHER_CREATE_HANDLE);
  115. struct mlx5_ib_dev *dev = to_mdev(uobj->context->device);
  116. struct mlx5_ib_flow_matcher *obj;
  117. int err;
  118. obj = kzalloc(sizeof(struct mlx5_ib_flow_matcher), GFP_KERNEL);
  119. if (!obj)
  120. return -ENOMEM;
  121. obj->mask_len = uverbs_attr_get_len(
  122. attrs, MLX5_IB_ATTR_FLOW_MATCHER_MATCH_MASK);
  123. err = uverbs_copy_from(&obj->matcher_mask,
  124. attrs,
  125. MLX5_IB_ATTR_FLOW_MATCHER_MATCH_MASK);
  126. if (err)
  127. goto end;
  128. obj->flow_type = uverbs_attr_get_enum_id(
  129. attrs, MLX5_IB_ATTR_FLOW_MATCHER_FLOW_TYPE);
  130. if (obj->flow_type == MLX5_IB_FLOW_TYPE_NORMAL) {
  131. err = uverbs_copy_from(&obj->priority,
  132. attrs,
  133. MLX5_IB_ATTR_FLOW_MATCHER_FLOW_TYPE);
  134. if (err)
  135. goto end;
  136. }
  137. err = uverbs_copy_from(&obj->match_criteria_enable,
  138. attrs,
  139. MLX5_IB_ATTR_FLOW_MATCHER_MATCH_CRITERIA);
  140. if (err)
  141. goto end;
  142. uobj->object = obj;
  143. obj->mdev = dev->mdev;
  144. atomic_set(&obj->usecnt, 0);
  145. return 0;
  146. end:
  147. kfree(obj);
  148. return err;
  149. }
  150. DECLARE_UVERBS_NAMED_METHOD(
  151. MLX5_IB_METHOD_CREATE_FLOW,
  152. UVERBS_ATTR_IDR(MLX5_IB_ATTR_CREATE_FLOW_HANDLE,
  153. UVERBS_OBJECT_FLOW,
  154. UVERBS_ACCESS_NEW,
  155. UA_MANDATORY),
  156. UVERBS_ATTR_PTR_IN(
  157. MLX5_IB_ATTR_CREATE_FLOW_MATCH_VALUE,
  158. UVERBS_ATTR_SIZE(1, sizeof(struct mlx5_ib_match_params)),
  159. UA_MANDATORY,
  160. UA_ALLOC_AND_COPY),
  161. UVERBS_ATTR_IDR(MLX5_IB_ATTR_CREATE_FLOW_MATCHER,
  162. MLX5_IB_OBJECT_FLOW_MATCHER,
  163. UVERBS_ACCESS_READ,
  164. UA_MANDATORY),
  165. UVERBS_ATTR_IDR(MLX5_IB_ATTR_CREATE_FLOW_DEST_QP,
  166. UVERBS_OBJECT_QP,
  167. UVERBS_ACCESS_READ),
  168. UVERBS_ATTR_IDR(MLX5_IB_ATTR_CREATE_FLOW_DEST_DEVX,
  169. MLX5_IB_OBJECT_DEVX_OBJ,
  170. UVERBS_ACCESS_READ));
  171. DECLARE_UVERBS_NAMED_METHOD_DESTROY(
  172. MLX5_IB_METHOD_DESTROY_FLOW,
  173. UVERBS_ATTR_IDR(MLX5_IB_ATTR_CREATE_FLOW_HANDLE,
  174. UVERBS_OBJECT_FLOW,
  175. UVERBS_ACCESS_DESTROY,
  176. UA_MANDATORY));
  177. ADD_UVERBS_METHODS(mlx5_ib_fs,
  178. UVERBS_OBJECT_FLOW,
  179. &UVERBS_METHOD(MLX5_IB_METHOD_CREATE_FLOW),
  180. &UVERBS_METHOD(MLX5_IB_METHOD_DESTROY_FLOW));
  181. DECLARE_UVERBS_NAMED_METHOD(
  182. MLX5_IB_METHOD_FLOW_MATCHER_CREATE,
  183. UVERBS_ATTR_IDR(MLX5_IB_ATTR_FLOW_MATCHER_CREATE_HANDLE,
  184. MLX5_IB_OBJECT_FLOW_MATCHER,
  185. UVERBS_ACCESS_NEW,
  186. UA_MANDATORY),
  187. UVERBS_ATTR_PTR_IN(
  188. MLX5_IB_ATTR_FLOW_MATCHER_MATCH_MASK,
  189. UVERBS_ATTR_SIZE(1, sizeof(struct mlx5_ib_match_params)),
  190. UA_MANDATORY),
  191. UVERBS_ATTR_ENUM_IN(MLX5_IB_ATTR_FLOW_MATCHER_FLOW_TYPE,
  192. mlx5_ib_flow_type,
  193. UA_MANDATORY),
  194. UVERBS_ATTR_PTR_IN(MLX5_IB_ATTR_FLOW_MATCHER_MATCH_CRITERIA,
  195. UVERBS_ATTR_TYPE(u8),
  196. UA_MANDATORY));
  197. DECLARE_UVERBS_NAMED_METHOD_DESTROY(
  198. MLX5_IB_METHOD_FLOW_MATCHER_DESTROY,
  199. UVERBS_ATTR_IDR(MLX5_IB_ATTR_FLOW_MATCHER_DESTROY_HANDLE,
  200. MLX5_IB_OBJECT_FLOW_MATCHER,
  201. UVERBS_ACCESS_DESTROY,
  202. UA_MANDATORY));
  203. DECLARE_UVERBS_NAMED_OBJECT(MLX5_IB_OBJECT_FLOW_MATCHER,
  204. UVERBS_TYPE_ALLOC_IDR(flow_matcher_cleanup),
  205. &UVERBS_METHOD(MLX5_IB_METHOD_FLOW_MATCHER_CREATE),
  206. &UVERBS_METHOD(MLX5_IB_METHOD_FLOW_MATCHER_DESTROY));
  207. DECLARE_UVERBS_OBJECT_TREE(flow_objects,
  208. &UVERBS_OBJECT(MLX5_IB_OBJECT_FLOW_MATCHER));
  209. int mlx5_ib_get_flow_trees(const struct uverbs_object_tree_def **root)
  210. {
  211. int i = 0;
  212. root[i++] = &flow_objects;
  213. root[i++] = &mlx5_ib_fs;
  214. return i;
  215. }