xref: /linux/tools/net/ynl/samples/devlink.c (revision c532de5a67a70f8533d495f8f2aaa9a0491c3ad0)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <stdio.h>
3 #include <string.h>
4 
5 #include <ynl.h>
6 
7 #include "devlink-user.h"
8 
9 int main(int argc, char **argv)
10 {
11 	struct devlink_get_list *devs;
12 	struct ynl_sock *ys;
13 
14 	ys = ynl_sock_create(&ynl_devlink_family, NULL);
15 	if (!ys)
16 		return 1;
17 
18 	devs = devlink_get_dump(ys);
19 	if (!devs)
20 		goto err_close;
21 
22 	ynl_dump_foreach(devs, d) {
23 		struct devlink_info_get_req *info_req;
24 		struct devlink_info_get_rsp *info_rsp;
25 
26 		printf("%s/%s:\n", d->bus_name, d->dev_name);
27 
28 		info_req = devlink_info_get_req_alloc();
29 		devlink_info_get_req_set_bus_name(info_req, d->bus_name);
30 		devlink_info_get_req_set_dev_name(info_req, d->dev_name);
31 
32 		info_rsp = devlink_info_get(ys, info_req);
33 		devlink_info_get_req_free(info_req);
34 		if (!info_rsp)
35 			goto err_free_devs;
36 
37 		if (info_rsp->_present.info_driver_name_len)
38 			printf("    driver: %s\n", info_rsp->info_driver_name);
39 		if (info_rsp->n_info_version_running)
40 			printf("    running fw:\n");
41 		for (unsigned i = 0; i < info_rsp->n_info_version_running; i++)
42 			printf("        %s: %s\n",
43 			       info_rsp->info_version_running[i].info_version_name,
44 			       info_rsp->info_version_running[i].info_version_value);
45 		printf("    ...\n");
46 		devlink_info_get_rsp_free(info_rsp);
47 	}
48 	devlink_get_list_free(devs);
49 
50 	ynl_sock_destroy(ys);
51 
52 	return 0;
53 
54 err_free_devs:
55 	devlink_get_list_free(devs);
56 err_close:
57 	fprintf(stderr, "YNL: %s\n", ys->err.msg);
58 	ynl_sock_destroy(ys);
59 	return 2;
60 }
61