This source file includes following definitions.
- uverbs_free_counters
- UVERBS_HANDLER
- UVERBS_HANDLER
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 #include "rdma_core.h"
35 #include "uverbs.h"
36 #include <rdma/uverbs_std_types.h>
37
38 static int uverbs_free_counters(struct ib_uobject *uobject,
39 enum rdma_remove_reason why,
40 struct uverbs_attr_bundle *attrs)
41 {
42 struct ib_counters *counters = uobject->object;
43 int ret;
44
45 ret = ib_destroy_usecnt(&counters->usecnt, why, uobject);
46 if (ret)
47 return ret;
48
49 return counters->device->ops.destroy_counters(counters);
50 }
51
52 static int UVERBS_HANDLER(UVERBS_METHOD_COUNTERS_CREATE)(
53 struct uverbs_attr_bundle *attrs)
54 {
55 struct ib_uobject *uobj = uverbs_attr_get_uobject(
56 attrs, UVERBS_ATTR_CREATE_COUNTERS_HANDLE);
57 struct ib_device *ib_dev = attrs->context->device;
58 struct ib_counters *counters;
59 int ret;
60
61
62
63
64
65
66 if (!ib_dev->ops.create_counters)
67 return -EOPNOTSUPP;
68
69 counters = ib_dev->ops.create_counters(ib_dev, attrs);
70 if (IS_ERR(counters)) {
71 ret = PTR_ERR(counters);
72 goto err_create_counters;
73 }
74
75 counters->device = ib_dev;
76 counters->uobject = uobj;
77 uobj->object = counters;
78 atomic_set(&counters->usecnt, 0);
79
80 return 0;
81
82 err_create_counters:
83 return ret;
84 }
85
86 static int UVERBS_HANDLER(UVERBS_METHOD_COUNTERS_READ)(
87 struct uverbs_attr_bundle *attrs)
88 {
89 struct ib_counters_read_attr read_attr = {};
90 const struct uverbs_attr *uattr;
91 struct ib_counters *counters =
92 uverbs_attr_get_obj(attrs, UVERBS_ATTR_READ_COUNTERS_HANDLE);
93 int ret;
94
95 if (!counters->device->ops.read_counters)
96 return -EOPNOTSUPP;
97
98 if (!atomic_read(&counters->usecnt))
99 return -EINVAL;
100
101 ret = uverbs_get_flags32(&read_attr.flags, attrs,
102 UVERBS_ATTR_READ_COUNTERS_FLAGS,
103 IB_UVERBS_READ_COUNTERS_PREFER_CACHED);
104 if (ret)
105 return ret;
106
107 uattr = uverbs_attr_get(attrs, UVERBS_ATTR_READ_COUNTERS_BUFF);
108 read_attr.ncounters = uattr->ptr_attr.len / sizeof(u64);
109 read_attr.counters_buff = uverbs_zalloc(
110 attrs, array_size(read_attr.ncounters, sizeof(u64)));
111 if (IS_ERR(read_attr.counters_buff))
112 return PTR_ERR(read_attr.counters_buff);
113
114 ret = counters->device->ops.read_counters(counters, &read_attr, attrs);
115 if (ret)
116 return ret;
117
118 return uverbs_copy_to(attrs, UVERBS_ATTR_READ_COUNTERS_BUFF,
119 read_attr.counters_buff,
120 read_attr.ncounters * sizeof(u64));
121 }
122
123 DECLARE_UVERBS_NAMED_METHOD(
124 UVERBS_METHOD_COUNTERS_CREATE,
125 UVERBS_ATTR_IDR(UVERBS_ATTR_CREATE_COUNTERS_HANDLE,
126 UVERBS_OBJECT_COUNTERS,
127 UVERBS_ACCESS_NEW,
128 UA_MANDATORY));
129
130 DECLARE_UVERBS_NAMED_METHOD_DESTROY(
131 UVERBS_METHOD_COUNTERS_DESTROY,
132 UVERBS_ATTR_IDR(UVERBS_ATTR_DESTROY_COUNTERS_HANDLE,
133 UVERBS_OBJECT_COUNTERS,
134 UVERBS_ACCESS_DESTROY,
135 UA_MANDATORY));
136
137 DECLARE_UVERBS_NAMED_METHOD(
138 UVERBS_METHOD_COUNTERS_READ,
139 UVERBS_ATTR_IDR(UVERBS_ATTR_READ_COUNTERS_HANDLE,
140 UVERBS_OBJECT_COUNTERS,
141 UVERBS_ACCESS_READ,
142 UA_MANDATORY),
143 UVERBS_ATTR_PTR_OUT(UVERBS_ATTR_READ_COUNTERS_BUFF,
144 UVERBS_ATTR_MIN_SIZE(0),
145 UA_MANDATORY),
146 UVERBS_ATTR_FLAGS_IN(UVERBS_ATTR_READ_COUNTERS_FLAGS,
147 enum ib_uverbs_read_counters_flags));
148
149 DECLARE_UVERBS_NAMED_OBJECT(UVERBS_OBJECT_COUNTERS,
150 UVERBS_TYPE_ALLOC_IDR(uverbs_free_counters),
151 &UVERBS_METHOD(UVERBS_METHOD_COUNTERS_CREATE),
152 &UVERBS_METHOD(UVERBS_METHOD_COUNTERS_DESTROY),
153 &UVERBS_METHOD(UVERBS_METHOD_COUNTERS_READ));
154
155 const struct uapi_definition uverbs_def_obj_counters[] = {
156 UAPI_DEF_CHAIN_OBJ_TREE_NAMED(UVERBS_OBJECT_COUNTERS,
157 UAPI_DEF_OBJ_NEEDS_FN(destroy_counters)),
158 {}
159 };