blob: c0be2b5f6a1e0db36f42ec944834a2195d4517b5 [file] [log] [blame]
Matan Baraka0aa3092017-08-03 16:06:55 +03001/*
2 * Copyright (c) 2017, Mellanox Technologies inc. All rights reserved.
3 *
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
9 *
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
13 *
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
17 *
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * SOFTWARE.
31 */
32
33#ifndef _UVERBS_IOCTL_
34#define _UVERBS_IOCTL_
35
36#include <rdma/uverbs_types.h>
Matan Barak35410302017-08-03 16:07:01 +030037#include <linux/uaccess.h>
38#include <rdma/rdma_user_ioctl.h>
Matan Barakd70724f2017-08-03 16:07:04 +030039#include <rdma/ib_user_ioctl_verbs.h>
Matan Barak1f7ff9d2018-03-19 15:02:33 +020040#include <rdma/ib_user_ioctl_cmds.h>
Matan Baraka0aa3092017-08-03 16:06:55 +030041
42/*
43 * =======================================
44 * Verbs action specifications
45 * =======================================
46 */
47
Matan Barakf43dbeb2017-08-03 16:06:56 +030048enum uverbs_attr_type {
49 UVERBS_ATTR_TYPE_NA,
Matan Barakfac96582017-08-03 16:06:57 +030050 UVERBS_ATTR_TYPE_PTR_IN,
51 UVERBS_ATTR_TYPE_PTR_OUT,
Matan Barakf43dbeb2017-08-03 16:06:56 +030052 UVERBS_ATTR_TYPE_IDR,
53 UVERBS_ATTR_TYPE_FD,
54};
55
Matan Baraka0aa3092017-08-03 16:06:55 +030056enum uverbs_obj_access {
57 UVERBS_ACCESS_READ,
58 UVERBS_ACCESS_WRITE,
59 UVERBS_ACCESS_NEW,
60 UVERBS_ACCESS_DESTROY
61};
62
Matan Barakfac96582017-08-03 16:06:57 +030063enum {
64 UVERBS_ATTR_SPEC_F_MANDATORY = 1U << 0,
65 /* Support extending attributes by length */
66 UVERBS_ATTR_SPEC_F_MIN_SZ = 1U << 1,
67};
68
Matan Barakf43dbeb2017-08-03 16:06:56 +030069struct uverbs_attr_spec {
70 enum uverbs_attr_type type;
Matan Barakfac96582017-08-03 16:06:57 +030071 union {
72 u16 len;
73 struct {
74 /*
75 * higher bits mean the namespace and lower bits mean
76 * the type id within the namespace.
77 */
78 u16 obj_type;
79 u8 access;
80 } obj;
81 };
82 /* Combination of bits from enum UVERBS_ATTR_SPEC_F_XXXX */
83 u8 flags;
Matan Barakf43dbeb2017-08-03 16:06:56 +030084};
85
86struct uverbs_attr_spec_hash {
87 size_t num_attrs;
Matan Barakfac96582017-08-03 16:06:57 +030088 unsigned long *mandatory_attrs_bitmask;
Matan Barakf43dbeb2017-08-03 16:06:56 +030089 struct uverbs_attr_spec attrs[0];
90};
91
Matan Barakfac96582017-08-03 16:06:57 +030092struct uverbs_attr_bundle;
93struct ib_uverbs_file;
94
95enum {
96 /*
97 * Action marked with this flag creates a context (or root for all
98 * objects).
99 */
100 UVERBS_ACTION_FLAG_CREATE_ROOT = 1U << 0,
101};
102
103struct uverbs_method_spec {
104 /* Combination of bits from enum UVERBS_ACTION_FLAG_XXXX */
105 u32 flags;
106 size_t num_buckets;
107 size_t num_child_attrs;
108 int (*handler)(struct ib_device *ib_dev, struct ib_uverbs_file *ufile,
109 struct uverbs_attr_bundle *ctx);
110 struct uverbs_attr_spec_hash *attr_buckets[0];
111};
112
113struct uverbs_method_spec_hash {
114 size_t num_methods;
115 struct uverbs_method_spec *methods[0];
116};
117
118struct uverbs_object_spec {
119 const struct uverbs_obj_type *type_attrs;
120 size_t num_buckets;
121 struct uverbs_method_spec_hash *method_buckets[0];
122};
123
124struct uverbs_object_spec_hash {
125 size_t num_objects;
126 struct uverbs_object_spec *objects[0];
127};
128
129struct uverbs_root_spec {
130 size_t num_buckets;
131 struct uverbs_object_spec_hash *object_buckets[0];
132};
133
Matan Barak50090102017-08-03 16:06:58 +0300134/*
135 * =======================================
136 * Verbs definitions
137 * =======================================
138 */
139
Matan Barak09e3ebf2017-08-03 16:06:59 +0300140struct uverbs_attr_def {
141 u16 id;
142 struct uverbs_attr_spec attr;
143};
144
145struct uverbs_method_def {
146 u16 id;
147 /* Combination of bits from enum UVERBS_ACTION_FLAG_XXXX */
148 u32 flags;
149 size_t num_attrs;
150 const struct uverbs_attr_def * const (*attrs)[];
151 int (*handler)(struct ib_device *ib_dev, struct ib_uverbs_file *ufile,
152 struct uverbs_attr_bundle *ctx);
153};
154
Matan Barak50090102017-08-03 16:06:58 +0300155struct uverbs_object_def {
Matan Barak09e3ebf2017-08-03 16:06:59 +0300156 u16 id;
Matan Barak50090102017-08-03 16:06:58 +0300157 const struct uverbs_obj_type *type_attrs;
Matan Barak09e3ebf2017-08-03 16:06:59 +0300158 size_t num_methods;
159 const struct uverbs_method_def * const (*methods)[];
160};
161
162struct uverbs_object_tree_def {
163 size_t num_objects;
164 const struct uverbs_object_def * const (*objects)[];
Matan Barak50090102017-08-03 16:06:58 +0300165};
166
Matan Barak35410302017-08-03 16:07:01 +0300167#define UA_FLAGS(_flags) .flags = _flags
168#define __UVERBS_ATTR0(_id, _len, _type, ...) \
169 ((const struct uverbs_attr_def) \
170 {.id = _id, .attr = {.type = _type, {.len = _len}, .flags = 0, } })
171#define __UVERBS_ATTR1(_id, _len, _type, _flags) \
172 ((const struct uverbs_attr_def) \
173 {.id = _id, .attr = {.type = _type, {.len = _len}, _flags, } })
174#define __UVERBS_ATTR(_id, _len, _type, _flags, _n, ...) \
175 __UVERBS_ATTR##_n(_id, _len, _type, _flags)
176/*
177 * In new compiler, UVERBS_ATTR could be simplified by declaring it as
178 * [_id] = {.type = _type, .len = _len, ##__VA_ARGS__}
179 * But since we support older compilers too, we need the more complex code.
180 */
181#define UVERBS_ATTR(_id, _len, _type, ...) \
182 __UVERBS_ATTR(_id, _len, _type, ##__VA_ARGS__, 1, 0)
183#define UVERBS_ATTR_PTR_IN_SZ(_id, _len, ...) \
184 UVERBS_ATTR(_id, _len, UVERBS_ATTR_TYPE_PTR_IN, ##__VA_ARGS__)
185/* If sizeof(_type) <= sizeof(u64), this will be inlined rather than a pointer */
186#define UVERBS_ATTR_PTR_IN(_id, _type, ...) \
187 UVERBS_ATTR_PTR_IN_SZ(_id, sizeof(_type), ##__VA_ARGS__)
188#define UVERBS_ATTR_PTR_OUT_SZ(_id, _len, ...) \
189 UVERBS_ATTR(_id, _len, UVERBS_ATTR_TYPE_PTR_OUT, ##__VA_ARGS__)
190#define UVERBS_ATTR_PTR_OUT(_id, _type, ...) \
191 UVERBS_ATTR_PTR_OUT_SZ(_id, sizeof(_type), ##__VA_ARGS__)
192
193/*
194 * In new compiler, UVERBS_ATTR_IDR (and FD) could be simplified by declaring
195 * it as
196 * {.id = _id, \
197 * .attr {.type = __obj_class, \
198 * .obj = {.obj_type = _idr_type, \
199 * .access = _access \
200 * }, ##__VA_ARGS__ } }
201 * But since we support older compilers too, we need the more complex code.
202 */
203#define ___UVERBS_ATTR_OBJ0(_id, _obj_class, _obj_type, _access, ...)\
204 ((const struct uverbs_attr_def) \
205 {.id = _id, \
206 .attr = {.type = _obj_class, \
207 {.obj = {.obj_type = _obj_type, .access = _access } },\
208 .flags = 0} })
209#define ___UVERBS_ATTR_OBJ1(_id, _obj_class, _obj_type, _access, _flags)\
210 ((const struct uverbs_attr_def) \
211 {.id = _id, \
212 .attr = {.type = _obj_class, \
213 {.obj = {.obj_type = _obj_type, .access = _access} }, \
214 _flags} })
215#define ___UVERBS_ATTR_OBJ(_id, _obj_class, _obj_type, _access, _flags, \
216 _n, ...) \
217 ___UVERBS_ATTR_OBJ##_n(_id, _obj_class, _obj_type, _access, _flags)
218#define __UVERBS_ATTR_OBJ(_id, _obj_class, _obj_type, _access, ...) \
219 ___UVERBS_ATTR_OBJ(_id, _obj_class, _obj_type, _access, \
220 ##__VA_ARGS__, 1, 0)
221#define UVERBS_ATTR_IDR(_id, _idr_type, _access, ...) \
222 __UVERBS_ATTR_OBJ(_id, UVERBS_ATTR_TYPE_IDR, _idr_type, _access,\
223 ##__VA_ARGS__)
224#define UVERBS_ATTR_FD(_id, _fd_type, _access, ...) \
225 __UVERBS_ATTR_OBJ(_id, UVERBS_ATTR_TYPE_FD, _fd_type, \
226 (_access) + BUILD_BUG_ON_ZERO( \
227 (_access) != UVERBS_ACCESS_NEW && \
228 (_access) != UVERBS_ACCESS_READ), \
229 ##__VA_ARGS__)
230#define DECLARE_UVERBS_ATTR_SPEC(_name, ...) \
231 const struct uverbs_attr_def _name = __VA_ARGS__
232
233#define _UVERBS_METHOD_ATTRS_SZ(...) \
234 (sizeof((const struct uverbs_attr_def * const []){__VA_ARGS__}) /\
235 sizeof(const struct uverbs_attr_def *))
236#define _UVERBS_METHOD(_id, _handler, _flags, ...) \
237 ((const struct uverbs_method_def) { \
238 .id = _id, \
239 .flags = _flags, \
240 .handler = _handler, \
241 .num_attrs = _UVERBS_METHOD_ATTRS_SZ(__VA_ARGS__), \
242 .attrs = &(const struct uverbs_attr_def * const []){__VA_ARGS__} })
243#define DECLARE_UVERBS_METHOD(_name, _id, _handler, ...) \
244 const struct uverbs_method_def _name = \
245 _UVERBS_METHOD(_id, _handler, 0, ##__VA_ARGS__)
246#define DECLARE_UVERBS_CTX_METHOD(_name, _id, _handler, _flags, ...) \
247 const struct uverbs_method_def _name = \
248 _UVERBS_METHOD(_id, _handler, \
249 UVERBS_ACTION_FLAG_CREATE_ROOT, \
250 ##__VA_ARGS__)
251#define _UVERBS_OBJECT_METHODS_SZ(...) \
252 (sizeof((const struct uverbs_method_def * const []){__VA_ARGS__}) / \
253 sizeof(const struct uverbs_method_def *))
Matan Barak50090102017-08-03 16:06:58 +0300254#define _UVERBS_OBJECT(_id, _type_attrs, ...) \
255 ((const struct uverbs_object_def) { \
Matan Barak09e3ebf2017-08-03 16:06:59 +0300256 .id = _id, \
Matan Barak35410302017-08-03 16:07:01 +0300257 .type_attrs = _type_attrs, \
258 .num_methods = _UVERBS_OBJECT_METHODS_SZ(__VA_ARGS__), \
259 .methods = &(const struct uverbs_method_def * const []){__VA_ARGS__} })
Matan Barak50090102017-08-03 16:06:58 +0300260#define DECLARE_UVERBS_OBJECT(_name, _id, _type_attrs, ...) \
261 const struct uverbs_object_def _name = \
262 _UVERBS_OBJECT(_id, _type_attrs, ##__VA_ARGS__)
Matan Barak09e3ebf2017-08-03 16:06:59 +0300263#define _UVERBS_TREE_OBJECTS_SZ(...) \
264 (sizeof((const struct uverbs_object_def * const []){__VA_ARGS__}) / \
265 sizeof(const struct uverbs_object_def *))
266#define _UVERBS_OBJECT_TREE(...) \
267 ((const struct uverbs_object_tree_def) { \
268 .num_objects = _UVERBS_TREE_OBJECTS_SZ(__VA_ARGS__), \
269 .objects = &(const struct uverbs_object_def * const []){__VA_ARGS__} })
270#define DECLARE_UVERBS_OBJECT_TREE(_name, ...) \
271 const struct uverbs_object_tree_def _name = \
272 _UVERBS_OBJECT_TREE(__VA_ARGS__)
273
Matan Barakfac96582017-08-03 16:06:57 +0300274/* =================================================
275 * Parsing infrastructure
276 * =================================================
277 */
278
279struct uverbs_ptr_attr {
Jason Gunthorpe2f360282018-02-13 12:18:31 +0200280 u64 data;
Matan Barakfac96582017-08-03 16:06:57 +0300281 u16 len;
282 /* Combination of bits from enum UVERBS_ATTR_F_XXXX */
283 u16 flags;
284};
285
Matan Barakf43dbeb2017-08-03 16:06:56 +0300286struct uverbs_obj_attr {
Matan Barakfac96582017-08-03 16:06:57 +0300287 /* pointer to the kernel descriptor -> type, access, etc */
288 const struct uverbs_obj_type *type;
Matan Barakf43dbeb2017-08-03 16:06:56 +0300289 struct ib_uobject *uobject;
Matan Barakfac96582017-08-03 16:06:57 +0300290 /* fd or id in idr of this object */
291 int id;
Matan Barakf43dbeb2017-08-03 16:06:56 +0300292};
293
294struct uverbs_attr {
Matan Barakfac96582017-08-03 16:06:57 +0300295 /*
296 * pointer to the user-space given attribute, in order to write the
297 * new uobject's id or update flags.
298 */
299 struct ib_uverbs_attr __user *uattr;
300 union {
301 struct uverbs_ptr_attr ptr_attr;
302 struct uverbs_obj_attr obj_attr;
303 };
Matan Barakf43dbeb2017-08-03 16:06:56 +0300304};
305
306struct uverbs_attr_bundle_hash {
307 /* if bit i is set, it means attrs[i] contains valid information */
308 unsigned long *valid_bitmap;
309 size_t num_attrs;
310 /*
311 * arrays of attributes, each element corresponds to the specification
312 * of the attribute in the same index.
313 */
314 struct uverbs_attr *attrs;
315};
316
317struct uverbs_attr_bundle {
318 size_t num_buckets;
319 struct uverbs_attr_bundle_hash hash[];
320};
321
322static inline bool uverbs_attr_is_valid_in_hash(const struct uverbs_attr_bundle_hash *attrs_hash,
323 unsigned int idx)
324{
325 return test_bit(idx, attrs_hash->valid_bitmap);
326}
327
Matan Barak35410302017-08-03 16:07:01 +0300328static inline bool uverbs_attr_is_valid(const struct uverbs_attr_bundle *attrs_bundle,
329 unsigned int idx)
330{
331 u16 idx_bucket = idx >> UVERBS_ID_NS_SHIFT;
332
333 if (attrs_bundle->num_buckets <= idx_bucket)
334 return false;
335
336 return uverbs_attr_is_valid_in_hash(&attrs_bundle->hash[idx_bucket],
337 idx & ~UVERBS_ID_NS_MASK);
338}
339
Matan Barakd70724f2017-08-03 16:07:04 +0300340static inline const struct uverbs_attr *uverbs_attr_get(const struct uverbs_attr_bundle *attrs_bundle,
341 u16 idx)
342{
343 u16 idx_bucket = idx >> UVERBS_ID_NS_SHIFT;
344
345 if (!uverbs_attr_is_valid(attrs_bundle, idx))
346 return ERR_PTR(-ENOENT);
347
348 return &attrs_bundle->hash[idx_bucket].attrs[idx & ~UVERBS_ID_NS_MASK];
349}
350
351static inline int uverbs_copy_to(const struct uverbs_attr_bundle *attrs_bundle,
Matan Barak89d9e8d2018-02-13 12:18:29 +0200352 size_t idx, const void *from, size_t size)
Matan Barakd70724f2017-08-03 16:07:04 +0300353{
354 const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
355 u16 flags;
Matan Barak89d9e8d2018-02-13 12:18:29 +0200356 size_t min_size;
Matan Barakd70724f2017-08-03 16:07:04 +0300357
358 if (IS_ERR(attr))
359 return PTR_ERR(attr);
360
Matan Barak89d9e8d2018-02-13 12:18:29 +0200361 min_size = min_t(size_t, attr->ptr_attr.len, size);
Jason Gunthorpe2f360282018-02-13 12:18:31 +0200362 if (copy_to_user(u64_to_user_ptr(attr->ptr_attr.data), from, min_size))
Matan Barak89d9e8d2018-02-13 12:18:29 +0200363 return -EFAULT;
364
Matan Barakd70724f2017-08-03 16:07:04 +0300365 flags = attr->ptr_attr.flags | UVERBS_ATTR_F_VALID_OUTPUT;
Matan Barak89d9e8d2018-02-13 12:18:29 +0200366 if (put_user(flags, &attr->uattr->flags))
367 return -EFAULT;
368
369 return 0;
Matan Barakd70724f2017-08-03 16:07:04 +0300370}
371
Matan Barak89d9e8d2018-02-13 12:18:29 +0200372static inline bool uverbs_attr_ptr_is_inline(const struct uverbs_attr *attr)
373{
374 return attr->ptr_attr.len <= sizeof(attr->ptr_attr.data);
375}
376
377static inline int _uverbs_copy_from(void *to,
Matan Barakd70724f2017-08-03 16:07:04 +0300378 const struct uverbs_attr_bundle *attrs_bundle,
Matan Barak89d9e8d2018-02-13 12:18:29 +0200379 size_t idx,
380 size_t size)
Matan Barakd70724f2017-08-03 16:07:04 +0300381{
382 const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
383
384 if (IS_ERR(attr))
385 return PTR_ERR(attr);
386
Matan Barak89d9e8d2018-02-13 12:18:29 +0200387 /*
388 * Validation ensures attr->ptr_attr.len >= size. If the caller is
389 * using UVERBS_ATTR_SPEC_F_MIN_SZ then it must call copy_from with
390 * the right size.
391 */
392 if (unlikely(size < attr->ptr_attr.len))
393 return -EINVAL;
394
395 if (uverbs_attr_ptr_is_inline(attr))
Matan Barakd70724f2017-08-03 16:07:04 +0300396 memcpy(to, &attr->ptr_attr.data, attr->ptr_attr.len);
Jason Gunthorpe2f360282018-02-13 12:18:31 +0200397 else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data),
398 attr->ptr_attr.len))
Matan Barakd70724f2017-08-03 16:07:04 +0300399 return -EFAULT;
400
401 return 0;
402}
403
404#define uverbs_copy_from(to, attrs_bundle, idx) \
Matan Barak89d9e8d2018-02-13 12:18:29 +0200405 _uverbs_copy_from(to, attrs_bundle, idx, sizeof(*to))
Matan Barakd70724f2017-08-03 16:07:04 +0300406
Matan Barak118620d2017-08-03 16:07:00 +0300407/* =================================================
408 * Definitions -> Specs infrastructure
409 * =================================================
410 */
Matan Baraka0aa3092017-08-03 16:06:55 +0300411
Matan Barak118620d2017-08-03 16:07:00 +0300412/*
413 * uverbs_alloc_spec_tree - Merges different common and driver specific feature
414 * into one parsing tree that every uverbs command will be parsed upon.
415 *
416 * @num_trees: Number of trees in the array @trees.
417 * @trees: Array of pointers to tree root definitions to merge. Each such tree
418 * possibly contains objects, methods and attributes definitions.
419 *
420 * Returns:
421 * uverbs_root_spec *: The root of the merged parsing tree.
422 * On error, we return an error code. Error is checked via IS_ERR.
423 *
424 * The following merges could take place:
425 * a. Two trees representing the same method with different handler
426 * -> We take the handler of the tree that its handler != NULL
427 * and its index in the trees array is greater. The incentive for that
428 * is that developers are expected to first merge common trees and then
429 * merge trees that gives specialized the behaviour.
430 * b. Two trees representing the same object with different
431 * type_attrs (struct uverbs_obj_type):
432 * -> We take the type_attrs of the tree that its type_attr != NULL
433 * and its index in the trees array is greater. This could be used
434 * in order to override the free function, allocation size, etc.
435 * c. Two trees representing the same method attribute (same id but possibly
436 * different attributes):
437 * -> ERROR (-ENOENT), we believe that's not the programmer's intent.
438 *
439 * An object without any methods is considered invalid and will abort the
440 * function with -ENOENT error.
441 */
Matan Barak52427112017-08-03 16:07:06 +0300442#if IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS)
Matan Barak118620d2017-08-03 16:07:00 +0300443struct uverbs_root_spec *uverbs_alloc_spec_tree(unsigned int num_trees,
444 const struct uverbs_object_tree_def **trees);
445void uverbs_free_spec_tree(struct uverbs_root_spec *root);
Matan Barak52427112017-08-03 16:07:06 +0300446#else
447static inline struct uverbs_root_spec *uverbs_alloc_spec_tree(unsigned int num_trees,
448 const struct uverbs_object_tree_def **trees)
449{
450 return NULL;
451}
452
453static inline void uverbs_free_spec_tree(struct uverbs_root_spec *root)
454{
455}
456#endif
Matan Barak118620d2017-08-03 16:07:00 +0300457
458#endif