1 /* SPDX-License-Identifier: GPL-2.0 */ 2 3 #ifndef _RDMA_NETLINK_H 4 #define _RDMA_NETLINK_H 5 6 #include <linux/netlink.h> 7 #include <uapi/rdma/rdma_netlink.h> 8 #include <rdma/ib_verbs.h> 9 10 struct ib_device; 11 12 enum { 13 RDMA_NLDEV_ATTR_EMPTY_STRING = 1, 14 RDMA_NLDEV_ATTR_ENTRY_STRLEN = 16, 15 RDMA_NLDEV_ATTR_CHARDEV_TYPE_SIZE = 32, 16 }; 17 18 struct rdma_nl_cbs { 19 int (*doit)(struct sk_buff *skb, struct nlmsghdr *nlh, 20 struct netlink_ext_ack *extack); 21 int (*dump)(struct sk_buff *skb, struct netlink_callback *nlcb); 22 u8 flags; 23 }; 24 25 enum rdma_nl_flags { 26 /* Require CAP_NET_ADMIN */ 27 RDMA_NL_ADMIN_PERM = 1 << 0, 28 }; 29 30 /* Define this module as providing netlink services for NETLINK_RDMA, with 31 * index _index. Since the client indexes were setup in a uapi header as an 32 * enum and we do no want to change that, the user must supply the expanded 33 * constant as well and the compiler checks they are the same. 34 */ 35 #define MODULE_ALIAS_RDMA_NETLINK(_index, _val) \ 36 static inline void __maybe_unused __chk_##_index(void) \ 37 { \ 38 BUILD_BUG_ON(_index != _val); \ 39 } \ 40 MODULE_ALIAS("rdma-netlink-subsys-" __stringify(_val)) 41 42 /** 43 * Register client in RDMA netlink. 44 * @index: Index of the added client 45 * @cb_table: A table for op->callback 46 */ 47 void rdma_nl_register(unsigned int index, 48 const struct rdma_nl_cbs cb_table[]); 49 50 /** 51 * Remove a client from IB netlink. 52 * @index: Index of the removed IB client. 53 */ 54 void rdma_nl_unregister(unsigned int index); 55 56 /** 57 * Put a new message in a supplied skb. 58 * @skb: The netlink skb. 59 * @nlh: Pointer to put the header of the new netlink message. 60 * @seq: The message sequence number. 61 * @len: The requested message length to allocate. 62 * @client: Calling IB netlink client. 63 * @op: message content op. 64 * Returns the allocated buffer on success and NULL on failure. 65 */ 66 void *ibnl_put_msg(struct sk_buff *skb, struct nlmsghdr **nlh, int seq, 67 int len, int client, int op, int flags); 68 /** 69 * Put a new attribute in a supplied skb. 70 * @skb: The netlink skb. 71 * @nlh: Header of the netlink message to append the attribute to. 72 * @len: The length of the attribute data. 73 * @data: The attribute data to put. 74 * @type: The attribute type. 75 * Returns the 0 and a negative error code on failure. 76 */ 77 int ibnl_put_attr(struct sk_buff *skb, struct nlmsghdr *nlh, 78 int len, void *data, int type); 79 80 /** 81 * Send the supplied skb to a specific userspace PID. 82 * @net: Net namespace in which to send the skb 83 * @skb: The netlink skb 84 * @pid: Userspace netlink process ID 85 * Returns 0 on success or a negative error code. 86 */ 87 int rdma_nl_unicast(struct net *net, struct sk_buff *skb, u32 pid); 88 89 /** 90 * Send, with wait/1 retry, the supplied skb to a specific userspace PID. 91 * @net: Net namespace in which to send the skb 92 * @skb: The netlink skb 93 * @pid: Userspace netlink process ID 94 * Returns 0 on success or a negative error code. 95 */ 96 int rdma_nl_unicast_wait(struct net *net, struct sk_buff *skb, __u32 pid); 97 98 /** 99 * Send the supplied skb to a netlink group. 100 * @net: Net namespace in which to send the skb 101 * @skb: The netlink skb 102 * @group: Netlink group ID 103 * @flags: allocation flags 104 * Returns 0 on success or a negative error code. 105 */ 106 int rdma_nl_multicast(struct net *net, struct sk_buff *skb, 107 unsigned int group, gfp_t flags); 108 109 /** 110 * Check if there are any listeners to the netlink group 111 * @group: the netlink group ID 112 * Returns true on success or false if no listeners. 113 */ 114 bool rdma_nl_chk_listeners(unsigned int group); 115 116 /** 117 * Prepare and send an event message 118 * @ib: the IB device which triggered the event 119 * @port_num: the port number which triggered the event - 0 if unused 120 * @type: the event type 121 * Returns 0 on success or a negative error code 122 */ 123 int rdma_nl_notify_event(struct ib_device *ib, u32 port_num, 124 enum rdma_nl_notify_event_type type); 125 126 struct rdma_link_ops { 127 struct list_head list; 128 const char *type; 129 int (*newlink)(const char *ibdev_name, struct net_device *ndev); 130 int (*dellink)(struct ib_device *dev); 131 }; 132 133 void rdma_link_register(struct rdma_link_ops *ops); 134 void rdma_link_unregister(struct rdma_link_ops *ops); 135 136 #define MODULE_ALIAS_RDMA_LINK(type) MODULE_ALIAS("rdma-link-" type) 137 #define MODULE_ALIAS_RDMA_CLIENT(type) MODULE_ALIAS("rdma-client-" type) 138 139 #endif /* _RDMA_NETLINK_H */ 140