]> git.proxmox.com Git - mirror_iproute2.git/blob - ip/iplink_bond.c
iplink: add support for bonding netlink
[mirror_iproute2.git] / ip / iplink_bond.c
1 /*
2 * iplink_bond.c Bonding device support
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: Jiri Pirko <jiri@resnulli.us>
10 */
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <linux/if_link.h>
16 #include <net/if.h>
17
18 #include "rt_names.h"
19 #include "utils.h"
20 #include "ip_common.h"
21
22 static void explain(void)
23 {
24 fprintf(stderr,
25 "Usage: ... bond [ mode BONDMODE ] [ active_slave SLAVE_DEV ]\n"
26 " [ clear_active_slave ]\n"
27 "\n"
28 "BONDMODE := 0-6\n"
29 );
30 }
31
32 static int bond_parse_opt(struct link_util *lu, int argc, char **argv,
33 struct nlmsghdr *n)
34 {
35 __u8 mode;
36 unsigned ifindex;
37
38 while (argc > 0) {
39 if (matches(*argv, "mode") == 0) {
40 NEXT_ARG();
41 if (get_u8(&mode, *argv, 0)) {
42 invarg("mode %s is invalid", *argv);
43 return -1;
44 }
45 addattr8(n, 1024, IFLA_BOND_MODE, mode);
46 } else if (matches(*argv, "active_slave") == 0) {
47 NEXT_ARG();
48 ifindex = if_nametoindex(*argv);
49 if (!ifindex)
50 return -1;
51 addattr32(n, 1024, IFLA_BOND_ACTIVE_SLAVE, ifindex);
52 } else if (matches(*argv, "clear_active_slave") == 0) {
53 addattr32(n, 1024, IFLA_BOND_ACTIVE_SLAVE, 0);
54 } else {
55 fprintf(stderr, "bond: unknown command \"%s\"?\n", *argv);
56 explain();
57 return -1;
58 }
59 argc--, argv++;
60 }
61
62 return 0;
63 }
64
65 static void bond_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
66 {
67 unsigned ifindex;
68
69 if (!tb)
70 return;
71
72 if (tb[IFLA_BOND_MODE])
73 fprintf(f, "mode %u ", rta_getattr_u8(tb[IFLA_BOND_MODE]));
74
75 if (tb[IFLA_BOND_ACTIVE_SLAVE] &&
76 (ifindex = rta_getattr_u32(tb[IFLA_BOND_ACTIVE_SLAVE]))) {
77 char buf[IFNAMSIZ];
78 const char *n = if_indextoname(ifindex, buf);
79
80 if (n)
81 fprintf(f, "active_slave %s ", n);
82 else
83 fprintf(f, "active_slave %u ", ifindex);
84 }
85 }
86
87 struct link_util bond_link_util = {
88 .id = "bond",
89 .maxattr = IFLA_BOND_MAX,
90 .parse_opt = bond_parse_opt,
91 .print_opt = bond_print_opt,
92 };