xref: /aosp_15_r20/external/libnl/src/nl-link-enslave.c (revision 4dc78e53d49367fa8e61b07018507c90983a077d)
1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3  * Copyright (c) 2011 Thomas Graf <[email protected]>
4  */
5 
6 #include "nl-default.h"
7 
8 #include <linux/netlink.h>
9 
10 #include <netlink/cli/utils.h>
11 #include <netlink/cli/link.h>
12 #include <netlink/route/link/bonding.h>
13 
main(int argc,char * argv[])14 int main(int argc, char *argv[])
15 {
16 	struct nl_sock *sock;
17 	struct nl_cache *link_cache;
18 	struct rtnl_link *master, *slave;
19 	int err;
20 
21 	if (argc < 3) {
22 		fprintf(stderr, "Usage: nl-link-enslave master slave\n");
23 		return 1;
24 	}
25 
26 	sock = nl_cli_alloc_socket();
27 	nl_cli_connect(sock, NETLINK_ROUTE);
28 	link_cache = nl_cli_link_alloc_cache(sock);
29 
30 	if (!(master = rtnl_link_get_by_name(link_cache, argv[1]))) {
31 		fprintf(stderr, "Unknown link: %s\n", argv[1]);
32 		return 1;
33 	}
34 
35 	if (!(slave = rtnl_link_get_by_name(link_cache, argv[2]))) {
36 		fprintf(stderr, "Unknown link: %s\n", argv[2]);
37 		return 1;
38 	}
39 
40 	if ((err = rtnl_link_bond_enslave(sock, master, slave)) < 0) {
41 		fprintf(stderr, "Unable to enslave %s to %s: %s\n",
42 			argv[2], argv[1], nl_geterror(err));
43 		return 1;
44 	}
45 
46 	return 0;
47 }
48 
49