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