libnl  3.4.0
nl-link-enslave.c
1 /*
2  * src/nl-link-enslave.c Enslave a link
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation version 2.1
7  * of the License.
8  *
9  * Copyright (c) 2011 Thomas Graf <tgraf@suug.ch>
10  */
11 
12 #include <netlink/cli/utils.h>
13 #include <netlink/cli/link.h>
14 #include <netlink/route/link/bonding.h>
15 
16 #include <linux/netlink.h>
17 
18 int main(int argc, char *argv[])
19 {
20  struct nl_sock *sock;
21  struct nl_cache *link_cache;
22  struct rtnl_link *master, *slave;
23  int err;
24 
25  if (argc < 3) {
26  fprintf(stderr, "Usage: nl-link-enslave master slave\n");
27  return 1;
28  }
29 
30  sock = nl_cli_alloc_socket();
31  nl_cli_connect(sock, NETLINK_ROUTE);
32  link_cache = nl_cli_link_alloc_cache(sock);
33 
34  if (!(master = rtnl_link_get_by_name(link_cache, argv[1]))) {
35  fprintf(stderr, "Unknown link: %s\n", argv[1]);
36  return 1;
37  }
38 
39  if (!(slave = rtnl_link_get_by_name(link_cache, argv[2]))) {
40  fprintf(stderr, "Unknown link: %s\n", argv[2]);
41  return 1;
42  }
43 
44  if ((err = rtnl_link_bond_enslave(sock, master, slave)) < 0) {
45  fprintf(stderr, "Unable to enslave %s to %s: %s\n",
46  argv[2], argv[1], nl_geterror(err));
47  return 1;
48  }
49 
50  return 0;
51 }
52 
int rtnl_link_bond_enslave(struct nl_sock *sock, struct rtnl_link *master, struct rtnl_link *slave)
Add a link to a bond (enslave)
Definition: bonding.c:164