1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright (c) 2016 Mellanox Technologies. All rights reserved. 4 * Copyright (c) 2016 Jiri Pirko <jiri@mellanox.com> 5 */ 6 7 #include <net/genetlink.h> 8 #include <net/sock.h> 9 10 #include "devl_internal.h" 11 12 static const struct genl_multicast_group devlink_nl_mcgrps[] = { 13 [DEVLINK_MCGRP_CONFIG] = { .name = DEVLINK_GENL_MCGRP_CONFIG_NAME }, 14 }; 15 16 static const struct nla_policy devlink_nl_policy[DEVLINK_ATTR_MAX + 1] = { 17 [DEVLINK_ATTR_UNSPEC] = { .strict_start_type = 18 DEVLINK_ATTR_TRAP_POLICER_ID }, 19 [DEVLINK_ATTR_BUS_NAME] = { .type = NLA_NUL_STRING }, 20 [DEVLINK_ATTR_DEV_NAME] = { .type = NLA_NUL_STRING }, 21 [DEVLINK_ATTR_PORT_INDEX] = { .type = NLA_U32 }, 22 [DEVLINK_ATTR_PORT_TYPE] = NLA_POLICY_RANGE(NLA_U16, DEVLINK_PORT_TYPE_AUTO, 23 DEVLINK_PORT_TYPE_IB), 24 [DEVLINK_ATTR_PORT_SPLIT_COUNT] = { .type = NLA_U32 }, 25 [DEVLINK_ATTR_SB_INDEX] = { .type = NLA_U32 }, 26 [DEVLINK_ATTR_SB_POOL_INDEX] = { .type = NLA_U16 }, 27 [DEVLINK_ATTR_SB_POOL_TYPE] = { .type = NLA_U8 }, 28 [DEVLINK_ATTR_SB_POOL_SIZE] = { .type = NLA_U32 }, 29 [DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE] = { .type = NLA_U8 }, 30 [DEVLINK_ATTR_SB_THRESHOLD] = { .type = NLA_U32 }, 31 [DEVLINK_ATTR_SB_TC_INDEX] = { .type = NLA_U16 }, 32 [DEVLINK_ATTR_ESWITCH_MODE] = NLA_POLICY_RANGE(NLA_U16, DEVLINK_ESWITCH_MODE_LEGACY, 33 DEVLINK_ESWITCH_MODE_SWITCHDEV), 34 [DEVLINK_ATTR_ESWITCH_INLINE_MODE] = { .type = NLA_U8 }, 35 [DEVLINK_ATTR_ESWITCH_ENCAP_MODE] = { .type = NLA_U8 }, 36 [DEVLINK_ATTR_DPIPE_TABLE_NAME] = { .type = NLA_NUL_STRING }, 37 [DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED] = { .type = NLA_U8 }, 38 [DEVLINK_ATTR_RESOURCE_ID] = { .type = NLA_U64}, 39 [DEVLINK_ATTR_RESOURCE_SIZE] = { .type = NLA_U64}, 40 [DEVLINK_ATTR_PARAM_NAME] = { .type = NLA_NUL_STRING }, 41 [DEVLINK_ATTR_PARAM_TYPE] = { .type = NLA_U8 }, 42 [DEVLINK_ATTR_PARAM_VALUE_CMODE] = { .type = NLA_U8 }, 43 [DEVLINK_ATTR_REGION_NAME] = { .type = NLA_NUL_STRING }, 44 [DEVLINK_ATTR_REGION_SNAPSHOT_ID] = { .type = NLA_U32 }, 45 [DEVLINK_ATTR_REGION_CHUNK_ADDR] = { .type = NLA_U64 }, 46 [DEVLINK_ATTR_REGION_CHUNK_LEN] = { .type = NLA_U64 }, 47 [DEVLINK_ATTR_HEALTH_REPORTER_NAME] = { .type = NLA_NUL_STRING }, 48 [DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD] = { .type = NLA_U64 }, 49 [DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER] = { .type = NLA_U8 }, 50 [DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME] = { .type = NLA_NUL_STRING }, 51 [DEVLINK_ATTR_FLASH_UPDATE_COMPONENT] = { .type = NLA_NUL_STRING }, 52 [DEVLINK_ATTR_FLASH_UPDATE_OVERWRITE_MASK] = 53 NLA_POLICY_BITFIELD32(DEVLINK_SUPPORTED_FLASH_OVERWRITE_SECTIONS), 54 [DEVLINK_ATTR_TRAP_NAME] = { .type = NLA_NUL_STRING }, 55 [DEVLINK_ATTR_TRAP_ACTION] = { .type = NLA_U8 }, 56 [DEVLINK_ATTR_TRAP_GROUP_NAME] = { .type = NLA_NUL_STRING }, 57 [DEVLINK_ATTR_NETNS_PID] = { .type = NLA_U32 }, 58 [DEVLINK_ATTR_NETNS_FD] = { .type = NLA_U32 }, 59 [DEVLINK_ATTR_NETNS_ID] = { .type = NLA_U32 }, 60 [DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP] = { .type = NLA_U8 }, 61 [DEVLINK_ATTR_TRAP_POLICER_ID] = { .type = NLA_U32 }, 62 [DEVLINK_ATTR_TRAP_POLICER_RATE] = { .type = NLA_U64 }, 63 [DEVLINK_ATTR_TRAP_POLICER_BURST] = { .type = NLA_U64 }, 64 [DEVLINK_ATTR_PORT_FUNCTION] = { .type = NLA_NESTED }, 65 [DEVLINK_ATTR_RELOAD_ACTION] = NLA_POLICY_RANGE(NLA_U8, DEVLINK_RELOAD_ACTION_DRIVER_REINIT, 66 DEVLINK_RELOAD_ACTION_MAX), 67 [DEVLINK_ATTR_RELOAD_LIMITS] = NLA_POLICY_BITFIELD32(DEVLINK_RELOAD_LIMITS_VALID_MASK), 68 [DEVLINK_ATTR_PORT_FLAVOUR] = { .type = NLA_U16 }, 69 [DEVLINK_ATTR_PORT_PCI_PF_NUMBER] = { .type = NLA_U16 }, 70 [DEVLINK_ATTR_PORT_PCI_SF_NUMBER] = { .type = NLA_U32 }, 71 [DEVLINK_ATTR_PORT_CONTROLLER_NUMBER] = { .type = NLA_U32 }, 72 [DEVLINK_ATTR_RATE_TYPE] = { .type = NLA_U16 }, 73 [DEVLINK_ATTR_RATE_TX_SHARE] = { .type = NLA_U64 }, 74 [DEVLINK_ATTR_RATE_TX_MAX] = { .type = NLA_U64 }, 75 [DEVLINK_ATTR_RATE_NODE_NAME] = { .type = NLA_NUL_STRING }, 76 [DEVLINK_ATTR_RATE_PARENT_NODE_NAME] = { .type = NLA_NUL_STRING }, 77 [DEVLINK_ATTR_LINECARD_INDEX] = { .type = NLA_U32 }, 78 [DEVLINK_ATTR_LINECARD_TYPE] = { .type = NLA_NUL_STRING }, 79 [DEVLINK_ATTR_SELFTESTS] = { .type = NLA_NESTED }, 80 [DEVLINK_ATTR_RATE_TX_PRIORITY] = { .type = NLA_U32 }, 81 [DEVLINK_ATTR_RATE_TX_WEIGHT] = { .type = NLA_U32 }, 82 [DEVLINK_ATTR_REGION_DIRECT] = { .type = NLA_FLAG }, 83 }; 84 85 struct devlink * 86 devlink_get_from_attrs_lock(struct net *net, struct nlattr **attrs) 87 { 88 struct devlink *devlink; 89 unsigned long index; 90 char *busname; 91 char *devname; 92 93 if (!attrs[DEVLINK_ATTR_BUS_NAME] || !attrs[DEVLINK_ATTR_DEV_NAME]) 94 return ERR_PTR(-EINVAL); 95 96 busname = nla_data(attrs[DEVLINK_ATTR_BUS_NAME]); 97 devname = nla_data(attrs[DEVLINK_ATTR_DEV_NAME]); 98 99 devlinks_xa_for_each_registered_get(net, index, devlink) { 100 devl_lock(devlink); 101 if (devl_is_registered(devlink) && 102 strcmp(devlink->dev->bus->name, busname) == 0 && 103 strcmp(dev_name(devlink->dev), devname) == 0) 104 return devlink; 105 devl_unlock(devlink); 106 devlink_put(devlink); 107 } 108 109 return ERR_PTR(-ENODEV); 110 } 111 112 static int __devlink_nl_pre_doit(struct sk_buff *skb, struct genl_info *info, 113 u8 flags) 114 { 115 struct devlink_port *devlink_port; 116 struct devlink *devlink; 117 int err; 118 119 devlink = devlink_get_from_attrs_lock(genl_info_net(info), info->attrs); 120 if (IS_ERR(devlink)) 121 return PTR_ERR(devlink); 122 123 info->user_ptr[0] = devlink; 124 if (flags & DEVLINK_NL_FLAG_NEED_PORT) { 125 devlink_port = devlink_port_get_from_info(devlink, info); 126 if (IS_ERR(devlink_port)) { 127 err = PTR_ERR(devlink_port); 128 goto unlock; 129 } 130 info->user_ptr[1] = devlink_port; 131 } else if (flags & DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT) { 132 devlink_port = devlink_port_get_from_info(devlink, info); 133 if (!IS_ERR(devlink_port)) 134 info->user_ptr[1] = devlink_port; 135 } 136 return 0; 137 138 unlock: 139 devl_unlock(devlink); 140 devlink_put(devlink); 141 return err; 142 } 143 144 int devlink_nl_pre_doit(const struct genl_split_ops *ops, 145 struct sk_buff *skb, struct genl_info *info) 146 { 147 return __devlink_nl_pre_doit(skb, info, ops->internal_flags); 148 } 149 150 int devlink_nl_pre_doit_port(const struct genl_split_ops *ops, 151 struct sk_buff *skb, struct genl_info *info) 152 { 153 return __devlink_nl_pre_doit(skb, info, DEVLINK_NL_FLAG_NEED_PORT); 154 } 155 156 int devlink_nl_pre_doit_port_optional(const struct genl_split_ops *ops, 157 struct sk_buff *skb, 158 struct genl_info *info) 159 { 160 return __devlink_nl_pre_doit(skb, info, DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT); 161 } 162 163 void devlink_nl_post_doit(const struct genl_split_ops *ops, 164 struct sk_buff *skb, struct genl_info *info) 165 { 166 struct devlink *devlink; 167 168 devlink = info->user_ptr[0]; 169 devl_unlock(devlink); 170 devlink_put(devlink); 171 } 172 173 static int devlink_nl_inst_single_dumpit(struct sk_buff *msg, 174 struct netlink_callback *cb, int flags, 175 devlink_nl_dump_one_func_t *dump_one, 176 struct nlattr **attrs) 177 { 178 struct devlink *devlink; 179 int err; 180 181 devlink = devlink_get_from_attrs_lock(sock_net(msg->sk), attrs); 182 if (IS_ERR(devlink)) 183 return PTR_ERR(devlink); 184 err = dump_one(msg, devlink, cb, flags | NLM_F_DUMP_FILTERED); 185 186 devl_unlock(devlink); 187 devlink_put(devlink); 188 189 if (err != -EMSGSIZE) 190 return err; 191 return msg->len; 192 } 193 194 static int devlink_nl_inst_iter_dumpit(struct sk_buff *msg, 195 struct netlink_callback *cb, int flags, 196 devlink_nl_dump_one_func_t *dump_one) 197 { 198 struct devlink_nl_dump_state *state = devlink_dump_state(cb); 199 struct devlink *devlink; 200 int err = 0; 201 202 while ((devlink = devlinks_xa_find_get(sock_net(msg->sk), 203 &state->instance))) { 204 devl_lock(devlink); 205 206 if (devl_is_registered(devlink)) 207 err = dump_one(msg, devlink, cb, flags); 208 else 209 err = 0; 210 211 devl_unlock(devlink); 212 devlink_put(devlink); 213 214 if (err) 215 break; 216 217 state->instance++; 218 219 /* restart sub-object walk for the next instance */ 220 state->idx = 0; 221 } 222 223 if (err != -EMSGSIZE) 224 return err; 225 return msg->len; 226 } 227 228 int devlink_nl_dumpit(struct sk_buff *msg, struct netlink_callback *cb, 229 devlink_nl_dump_one_func_t *dump_one) 230 { 231 const struct genl_info *info = genl_info_dump(cb); 232 struct nlattr **attrs = info->attrs; 233 int flags = NLM_F_MULTI; 234 235 if (attrs && 236 (attrs[DEVLINK_ATTR_BUS_NAME] || attrs[DEVLINK_ATTR_DEV_NAME])) 237 return devlink_nl_inst_single_dumpit(msg, cb, flags, dump_one, 238 attrs); 239 else 240 return devlink_nl_inst_iter_dumpit(msg, cb, flags, dump_one); 241 } 242 243 struct genl_family devlink_nl_family __ro_after_init = { 244 .name = DEVLINK_GENL_NAME, 245 .version = DEVLINK_GENL_VERSION, 246 .maxattr = DEVLINK_ATTR_MAX, 247 .policy = devlink_nl_policy, 248 .netnsok = true, 249 .parallel_ops = true, 250 .pre_doit = devlink_nl_pre_doit, 251 .post_doit = devlink_nl_post_doit, 252 .module = THIS_MODULE, 253 .small_ops = devlink_nl_small_ops, 254 .n_small_ops = ARRAY_SIZE(devlink_nl_small_ops), 255 .split_ops = devlink_nl_ops, 256 .n_split_ops = ARRAY_SIZE(devlink_nl_ops), 257 .resv_start_op = DEVLINK_CMD_SELFTESTS_RUN + 1, 258 .mcgrps = devlink_nl_mcgrps, 259 .n_mcgrps = ARRAY_SIZE(devlink_nl_mcgrps), 260 }; 261