• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * utils.c	RDMA tool
3  *
4  *              This program is free software; you can redistribute it and/or
5  *              modify it under the terms of the GNU General Public License
6  *              as published by the Free Software Foundation; either version
7  *              2 of the License, or (at your option) any later version.
8  *
9  * Authors:     Leon Romanovsky <leonro@mellanox.com>
10  */
11 
12 #include "rdma.h"
13 
rd_argc(struct rd * rd)14 static int rd_argc(struct rd *rd)
15 {
16 	return rd->argc;
17 }
18 
rd_argv(struct rd * rd)19 char *rd_argv(struct rd *rd)
20 {
21 	if (!rd_argc(rd))
22 		return NULL;
23 	return *rd->argv;
24 }
25 
strcmpx(const char * str1,const char * str2)26 static int strcmpx(const char *str1, const char *str2)
27 {
28 	if (strlen(str1) > strlen(str2))
29 		return -1;
30 	return strncmp(str1, str2, strlen(str1));
31 }
32 
rd_argv_match(struct rd * rd,const char * pattern)33 static bool rd_argv_match(struct rd *rd, const char *pattern)
34 {
35 	if (!rd_argc(rd))
36 		return false;
37 	return strcmpx(rd_argv(rd), pattern) == 0;
38 }
39 
rd_arg_inc(struct rd * rd)40 void rd_arg_inc(struct rd *rd)
41 {
42 	if (!rd_argc(rd))
43 		return;
44 	rd->argc--;
45 	rd->argv++;
46 }
47 
rd_no_arg(struct rd * rd)48 bool rd_no_arg(struct rd *rd)
49 {
50 	return rd_argc(rd) == 0;
51 }
52 
get_port_from_argv(struct rd * rd)53 uint32_t get_port_from_argv(struct rd *rd)
54 {
55 	char *slash;
56 
57 	slash = strchr(rd_argv(rd), '/');
58 	/* if no port found, return 0 */
59 	return slash ? atoi(slash + 1) : 0;
60 }
61 
dev_map_alloc(const char * dev_name)62 static struct dev_map *dev_map_alloc(const char *dev_name)
63 {
64 	struct dev_map *dev_map;
65 
66 	dev_map = calloc(1, sizeof(*dev_map));
67 	if (!dev_map)
68 		return NULL;
69 	dev_map->dev_name = strdup(dev_name);
70 
71 	return dev_map;
72 }
73 
dev_map_free(struct dev_map * dev_map)74 static void dev_map_free(struct dev_map *dev_map)
75 {
76 	if (!dev_map)
77 		return;
78 
79 	free(dev_map->dev_name);
80 	free(dev_map);
81 }
82 
dev_map_cleanup(struct rd * rd)83 static void dev_map_cleanup(struct rd *rd)
84 {
85 	struct dev_map *dev_map, *tmp;
86 
87 	list_for_each_entry_safe(dev_map, tmp,
88 				 &rd->dev_map_list, list) {
89 		list_del(&dev_map->list);
90 		dev_map_free(dev_map);
91 	}
92 }
93 
94 static const enum mnl_attr_data_type nldev_policy[RDMA_NLDEV_ATTR_MAX] = {
95 	[RDMA_NLDEV_ATTR_DEV_INDEX] = MNL_TYPE_U32,
96 	[RDMA_NLDEV_ATTR_DEV_NAME] = MNL_TYPE_NUL_STRING,
97 	[RDMA_NLDEV_ATTR_PORT_INDEX] = MNL_TYPE_U32,
98 	[RDMA_NLDEV_ATTR_CAP_FLAGS] = MNL_TYPE_U64,
99 	[RDMA_NLDEV_ATTR_FW_VERSION] = MNL_TYPE_NUL_STRING,
100 	[RDMA_NLDEV_ATTR_NODE_GUID] = MNL_TYPE_U64,
101 	[RDMA_NLDEV_ATTR_SYS_IMAGE_GUID] = MNL_TYPE_U64,
102 	[RDMA_NLDEV_ATTR_LID] = MNL_TYPE_U32,
103 	[RDMA_NLDEV_ATTR_SM_LID] = MNL_TYPE_U32,
104 	[RDMA_NLDEV_ATTR_LMC] = MNL_TYPE_U8,
105 	[RDMA_NLDEV_ATTR_PORT_STATE] = MNL_TYPE_U8,
106 	[RDMA_NLDEV_ATTR_PORT_PHYS_STATE] = MNL_TYPE_U8,
107 	[RDMA_NLDEV_ATTR_DEV_NODE_TYPE] = MNL_TYPE_U8,
108 };
109 
rd_attr_cb(const struct nlattr * attr,void * data)110 int rd_attr_cb(const struct nlattr *attr, void *data)
111 {
112 	const struct nlattr **tb = data;
113 	int type;
114 
115 	if (mnl_attr_type_valid(attr, RDMA_NLDEV_ATTR_MAX) < 0)
116 		return MNL_CB_ERROR;
117 
118 	type = mnl_attr_get_type(attr);
119 
120 	if (mnl_attr_validate(attr, nldev_policy[type]) < 0)
121 		return MNL_CB_ERROR;
122 
123 	tb[type] = attr;
124 	return MNL_CB_OK;
125 }
126 
rd_dev_init_cb(const struct nlmsghdr * nlh,void * data)127 int rd_dev_init_cb(const struct nlmsghdr *nlh, void *data)
128 {
129 	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX] = {};
130 	struct dev_map *dev_map;
131 	struct rd *rd = data;
132 	const char *dev_name;
133 
134 	mnl_attr_parse(nlh, 0, rd_attr_cb, tb);
135 	if (!tb[RDMA_NLDEV_ATTR_DEV_NAME] || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
136 		return MNL_CB_ERROR;
137 	if (!tb[RDMA_NLDEV_ATTR_PORT_INDEX]) {
138 		pr_err("This tool doesn't support switches yet\n");
139 		return MNL_CB_ERROR;
140 	}
141 
142 	dev_name = mnl_attr_get_str(tb[RDMA_NLDEV_ATTR_DEV_NAME]);
143 
144 	dev_map = dev_map_alloc(dev_name);
145 	if (!dev_map)
146 		/* The main function will cleanup the allocations */
147 		return MNL_CB_ERROR;
148 	list_add_tail(&dev_map->list, &rd->dev_map_list);
149 
150 	dev_map->num_ports = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
151 	dev_map->idx = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
152 	return MNL_CB_OK;
153 }
154 
rd_free_devmap(struct rd * rd)155 void rd_free_devmap(struct rd *rd)
156 {
157 	if (!rd)
158 		return;
159 	dev_map_cleanup(rd);
160 }
161 
rd_exec_cmd(struct rd * rd,const struct rd_cmd * cmds,const char * str)162 int rd_exec_cmd(struct rd *rd, const struct rd_cmd *cmds, const char *str)
163 {
164 	const struct rd_cmd *c;
165 
166 	/* First argument in objs table is default variant */
167 	if (rd_no_arg(rd))
168 		return cmds->func(rd);
169 
170 	for (c = cmds + 1; c->cmd; ++c) {
171 		if (rd_argv_match(rd, c->cmd)) {
172 			/* Move to next argument */
173 			rd_arg_inc(rd);
174 			return c->func(rd);
175 		}
176 	}
177 
178 	pr_err("Unknown %s '%s'.\n", str, rd_argv(rd));
179 	return 0;
180 }
181 
rd_prepare_msg(struct rd * rd,uint32_t cmd,uint32_t * seq,uint16_t flags)182 void rd_prepare_msg(struct rd *rd, uint32_t cmd, uint32_t *seq, uint16_t flags)
183 {
184 	*seq = time(NULL);
185 
186 	rd->nlh = mnl_nlmsg_put_header(rd->buff);
187 	rd->nlh->nlmsg_type = RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, cmd);
188 	rd->nlh->nlmsg_seq = *seq;
189 	rd->nlh->nlmsg_flags = flags;
190 }
191 
rd_send_msg(struct rd * rd)192 int rd_send_msg(struct rd *rd)
193 {
194 	int ret;
195 
196 	rd->nl = mnl_socket_open(NETLINK_RDMA);
197 	if (!rd->nl) {
198 		pr_err("Failed to open NETLINK_RDMA socket\n");
199 		return -ENODEV;
200 	}
201 
202 	ret = mnl_socket_bind(rd->nl, 0, MNL_SOCKET_AUTOPID);
203 	if (ret < 0) {
204 		pr_err("Failed to bind socket with err %d\n", ret);
205 		goto err;
206 	}
207 
208 	ret = mnl_socket_sendto(rd->nl, rd->nlh, rd->nlh->nlmsg_len);
209 	if (ret < 0) {
210 		pr_err("Failed to send to socket with err %d\n", ret);
211 		goto err;
212 	}
213 	return 0;
214 
215 err:
216 	mnl_socket_close(rd->nl);
217 	return ret;
218 }
219 
rd_recv_msg(struct rd * rd,mnl_cb_t callback,void * data,unsigned int seq)220 int rd_recv_msg(struct rd *rd, mnl_cb_t callback, void *data, unsigned int seq)
221 {
222 	int ret;
223 	unsigned int portid;
224 	char buf[MNL_SOCKET_BUFFER_SIZE];
225 
226 	portid = mnl_socket_get_portid(rd->nl);
227 	do {
228 		ret = mnl_socket_recvfrom(rd->nl, buf, sizeof(buf));
229 		if (ret <= 0)
230 			break;
231 
232 		ret = mnl_cb_run(buf, ret, seq, portid, callback, data);
233 	} while (ret > 0);
234 
235 	mnl_socket_close(rd->nl);
236 	return ret;
237 }
238 
_dev_map_lookup(struct rd * rd,const char * dev_name)239 struct dev_map *_dev_map_lookup(struct rd *rd, const char *dev_name)
240 {
241 	struct dev_map *dev_map;
242 
243 	list_for_each_entry(dev_map, &rd->dev_map_list, list)
244 		if (strcmp(dev_name, dev_map->dev_name) == 0)
245 			return dev_map;
246 
247 	return NULL;
248 }
249 
dev_map_lookup(struct rd * rd,bool allow_port_index)250 struct dev_map *dev_map_lookup(struct rd *rd, bool allow_port_index)
251 {
252 	struct dev_map *dev_map;
253 	char *dev_name;
254 	char *slash;
255 
256 	dev_name = strdup(rd_argv(rd));
257 	if (allow_port_index) {
258 		slash = strrchr(dev_name, '/');
259 		if (slash)
260 			*slash = '\0';
261 	}
262 
263 	dev_map = _dev_map_lookup(rd, dev_name);
264 	free(dev_name);
265 	return dev_map;
266 }
267