Skip to content

Commit 413d334

Browse files
Mark Zhangjgunthorpe
authored andcommitted
RDMA/counter: Add set/clear per-port auto mode support
Add an API to support set/clear per-port auto mode. Signed-off-by: Mark Zhang <[email protected]> Reviewed-by: Majd Dibbiny <[email protected]> Signed-off-by: Leon Romanovsky <[email protected]> Signed-off-by: Jason Gunthorpe <[email protected]>
1 parent 6a6c306 commit 413d334

File tree

6 files changed

+132
-1
lines changed

6 files changed

+132
-1
lines changed

drivers/infiniband/core/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ ib_core-y := packer.o ud_header.o verbs.o cq.o rw.o sysfs.o \
1111
device.o fmr_pool.o cache.o netlink.o \
1212
roce_gid_mgmt.o mr_pool.o addr.o sa_query.o \
1313
multicast.o mad.o smi.o agent.o mad_rmpp.o \
14-
nldev.o restrack.o
14+
nldev.o restrack.o counters.o
1515

1616
ib_core-$(CONFIG_SECURITY_INFINIBAND) += security.o
1717
ib_core-$(CONFIG_CGROUP_RDMA) += cgroup.o

drivers/infiniband/core/counters.c

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2+
/*
3+
* Copyright (c) 2019 Mellanox Technologies. All rights reserved.
4+
*/
5+
#include <rdma/ib_verbs.h>
6+
#include <rdma/rdma_counter.h>
7+
8+
#include "core_priv.h"
9+
#include "restrack.h"
10+
11+
#define ALL_AUTO_MODE_MASKS (RDMA_COUNTER_MASK_QP_TYPE)
12+
13+
static int __counter_set_mode(struct rdma_counter_mode *curr,
14+
enum rdma_nl_counter_mode new_mode,
15+
enum rdma_nl_counter_mask new_mask)
16+
{
17+
if ((new_mode == RDMA_COUNTER_MODE_AUTO) &&
18+
((new_mask & (~ALL_AUTO_MODE_MASKS)) ||
19+
(curr->mode != RDMA_COUNTER_MODE_NONE)))
20+
return -EINVAL;
21+
22+
curr->mode = new_mode;
23+
curr->mask = new_mask;
24+
return 0;
25+
}
26+
27+
/**
28+
* rdma_counter_set_auto_mode() - Turn on/off per-port auto mode
29+
*
30+
* When @on is true, the @mask must be set
31+
*/
32+
int rdma_counter_set_auto_mode(struct ib_device *dev, u8 port,
33+
bool on, enum rdma_nl_counter_mask mask)
34+
{
35+
struct rdma_port_counter *port_counter;
36+
int ret;
37+
38+
port_counter = &dev->port_data[port].port_counter;
39+
mutex_lock(&port_counter->lock);
40+
if (on) {
41+
ret = __counter_set_mode(&port_counter->mode,
42+
RDMA_COUNTER_MODE_AUTO, mask);
43+
} else {
44+
if (port_counter->mode.mode != RDMA_COUNTER_MODE_AUTO) {
45+
ret = -EINVAL;
46+
goto out;
47+
}
48+
ret = __counter_set_mode(&port_counter->mode,
49+
RDMA_COUNTER_MODE_NONE, 0);
50+
}
51+
52+
out:
53+
mutex_unlock(&port_counter->lock);
54+
return ret;
55+
}
56+
57+
void rdma_counter_init(struct ib_device *dev)
58+
{
59+
struct rdma_port_counter *port_counter;
60+
u32 port;
61+
62+
if (!dev->ops.alloc_hw_stats || !dev->port_data)
63+
return;
64+
65+
rdma_for_each_port(dev, port) {
66+
port_counter = &dev->port_data[port].port_counter;
67+
port_counter->mode.mode = RDMA_COUNTER_MODE_NONE;
68+
mutex_init(&port_counter->lock);
69+
}
70+
}
71+
72+
void rdma_counter_release(struct ib_device *dev)
73+
{
74+
}

drivers/infiniband/core/device.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#include <rdma/rdma_netlink.h>
4747
#include <rdma/ib_addr.h>
4848
#include <rdma/ib_cache.h>
49+
#include <rdma/rdma_counter.h>
4950

5051
#include "core_priv.h"
5152
#include "restrack.h"
@@ -492,10 +493,12 @@ static void ib_device_release(struct device *device)
492493
if (dev->port_data) {
493494
ib_cache_release_one(dev);
494495
ib_security_release_port_pkey_list(dev);
496+
rdma_counter_release(dev);
495497
kfree_rcu(container_of(dev->port_data, struct ib_port_data_rcu,
496498
pdata[0]),
497499
rcu_head);
498500
}
501+
499502
xa_destroy(&dev->compat_devs);
500503
xa_destroy(&dev->client_data);
501504
kfree_rcu(dev, rcu_head);
@@ -1316,6 +1319,8 @@ int ib_register_device(struct ib_device *device, const char *name)
13161319

13171320
ib_device_register_rdmacg(device);
13181321

1322+
rdma_counter_init(device);
1323+
13191324
/*
13201325
* Ensure that ADD uevent is not fired because it
13211326
* is too early amd device is not initialized yet.

include/rdma/ib_verbs.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
#include <linux/irqflags.h>
6363
#include <linux/preempt.h>
6464
#include <uapi/rdma/ib_user_verbs.h>
65+
#include <rdma/rdma_counter.h>
6566
#include <rdma/restrack.h>
6667
#include <rdma/signature.h>
6768
#include <uapi/rdma/rdma_user_ioctl.h>
@@ -2119,6 +2120,7 @@ struct ib_port_data {
21192120
spinlock_t netdev_lock;
21202121
struct net_device __rcu *netdev;
21212122
struct hlist_node ndev_hash_link;
2123+
struct rdma_port_counter port_counter;
21222124
};
21232125

21242126
/* rdma netdev type - specifies protocol type */

include/rdma/rdma_counter.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,37 @@
66
#ifndef _RDMA_COUNTER_H_
77
#define _RDMA_COUNTER_H_
88

9+
#include <linux/mutex.h>
10+
911
#include <rdma/ib_verbs.h>
1012
#include <rdma/restrack.h>
13+
#include <rdma/rdma_netlink.h>
14+
15+
struct auto_mode_param {
16+
int qp_type;
17+
};
18+
19+
struct rdma_counter_mode {
20+
enum rdma_nl_counter_mode mode;
21+
enum rdma_nl_counter_mask mask;
22+
struct auto_mode_param param;
23+
};
24+
25+
struct rdma_port_counter {
26+
struct rdma_counter_mode mode;
27+
struct mutex lock;
28+
};
1129

1230
struct rdma_counter {
1331
struct rdma_restrack_entry res;
1432
struct ib_device *device;
1533
uint32_t id;
1634
u8 port;
1735
};
36+
37+
void rdma_counter_init(struct ib_device *dev);
38+
void rdma_counter_release(struct ib_device *dev);
39+
int rdma_counter_set_auto_mode(struct ib_device *dev, u8 port,
40+
bool on, enum rdma_nl_counter_mask mask);
41+
1842
#endif /* _RDMA_COUNTER_H_ */

include/uapi/rdma/rdma_netlink.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,4 +507,30 @@ enum rdma_nldev_attr {
507507
*/
508508
RDMA_NLDEV_ATTR_MAX
509509
};
510+
511+
/*
512+
* Supported counter bind modes. All modes are mutual-exclusive.
513+
*/
514+
enum rdma_nl_counter_mode {
515+
RDMA_COUNTER_MODE_NONE,
516+
517+
/*
518+
* A qp is bound with a counter automatically during initialization
519+
* based on the auto mode (e.g., qp type, ...)
520+
*/
521+
RDMA_COUNTER_MODE_AUTO,
522+
523+
/*
524+
* Always the end
525+
*/
526+
RDMA_COUNTER_MODE_MAX,
527+
};
528+
529+
/*
530+
* Supported criteria in counter auto mode.
531+
* Currently only "qp type" is supported
532+
*/
533+
enum rdma_nl_counter_mask {
534+
RDMA_COUNTER_MASK_QP_TYPE = 1,
535+
};
510536
#endif /* _UAPI_RDMA_NETLINK_H */

0 commit comments

Comments
 (0)