blob: 9c60dea8a27574bc3c892567cf06d9091e0b5d17 [file] [log] [blame]
Jiri Pirko730d3f62014-01-23 17:52:54 +01001/*
2 * iplink_bond_slave.c Bonding slave 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 <sys/socket.h>
14#include <linux/if_bonding.h>
15
16#include "rt_names.h"
17#include "utils.h"
18#include "ip_common.h"
19
Phil Sutter25c93fa2016-07-09 11:22:46 +020020static void print_explain(FILE *f)
21{
22 fprintf(f, "Usage: ... bond_slave [ queue_id ID ]\n");
23}
24
25static void explain(void)
26{
27 print_explain(stderr);
28}
29
Jiri Pirko730d3f62014-01-23 17:52:54 +010030static const char *slave_states[] = {
31 [BOND_STATE_ACTIVE] = "ACTIVE",
32 [BOND_STATE_BACKUP] = "BACKUP",
33};
34
35static void print_slave_state(FILE *f, struct rtattr *tb)
36{
37 unsigned int state = rta_getattr_u8(tb);
38
Stephen Hemminger56f5daa2016-03-21 11:52:19 -070039 if (state >= ARRAY_SIZE(slave_states))
Jiri Pirko730d3f62014-01-23 17:52:54 +010040 fprintf(f, "state %d ", state);
41 else
42 fprintf(f, "state %s ", slave_states[state]);
43}
44
45static const char *slave_mii_status[] = {
46 [BOND_LINK_UP] = "UP",
47 [BOND_LINK_FAIL] = "GOING_DOWN",
48 [BOND_LINK_DOWN] = "DOWN",
49 [BOND_LINK_BACK] = "GOING_BACK",
50};
51
52static void print_slave_mii_status(FILE *f, struct rtattr *tb)
53{
54 unsigned int status = rta_getattr_u8(tb);
55
Stephen Hemminger56f5daa2016-03-21 11:52:19 -070056 if (status >= ARRAY_SIZE(slave_mii_status))
Jiri Pirko730d3f62014-01-23 17:52:54 +010057 fprintf(f, "mii_status %d ", status);
58 else
59 fprintf(f, "mii_status %s ", slave_mii_status[status]);
60}
61
62static void bond_slave_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
63{
64 SPRINT_BUF(b1);
65 if (!tb)
66 return;
67
68 if (tb[IFLA_BOND_SLAVE_STATE])
69 print_slave_state(f, tb[IFLA_BOND_SLAVE_STATE]);
70
71 if (tb[IFLA_BOND_SLAVE_MII_STATUS])
72 print_slave_mii_status(f, tb[IFLA_BOND_SLAVE_MII_STATUS]);
73
74 if (tb[IFLA_BOND_SLAVE_LINK_FAILURE_COUNT])
75 fprintf(f, "link_failure_count %d ",
76 rta_getattr_u32(tb[IFLA_BOND_SLAVE_LINK_FAILURE_COUNT]));
77
78 if (tb[IFLA_BOND_SLAVE_PERM_HWADDR])
79 fprintf(f, "perm_hwaddr %s ",
80 ll_addr_n2a(RTA_DATA(tb[IFLA_BOND_SLAVE_PERM_HWADDR]),
81 RTA_PAYLOAD(tb[IFLA_BOND_SLAVE_PERM_HWADDR]),
82 0, b1, sizeof(b1)));
83
84 if (tb[IFLA_BOND_SLAVE_QUEUE_ID])
85 fprintf(f, "queue_id %d ",
86 rta_getattr_u16(tb[IFLA_BOND_SLAVE_QUEUE_ID]));
87
88 if (tb[IFLA_BOND_SLAVE_AD_AGGREGATOR_ID])
89 fprintf(f, "ad_aggregator_id %d ",
90 rta_getattr_u16(tb[IFLA_BOND_SLAVE_AD_AGGREGATOR_ID]));
Nikolay Aleksandrov7d6bc3b2015-06-16 12:26:57 +030091
92 if (tb[IFLA_BOND_SLAVE_AD_ACTOR_OPER_PORT_STATE])
Nikolay Aleksandrove6c38e22016-02-08 17:13:58 +010093 fprintf(f, "ad_actor_oper_port_state %d ",
Nikolay Aleksandrov7d6bc3b2015-06-16 12:26:57 +030094 rta_getattr_u8(tb[IFLA_BOND_SLAVE_AD_ACTOR_OPER_PORT_STATE]));
95
96 if (tb[IFLA_BOND_SLAVE_AD_PARTNER_OPER_PORT_STATE])
Nikolay Aleksandrove6c38e22016-02-08 17:13:58 +010097 fprintf(f, "ad_partner_oper_port_state %d ",
Nikolay Aleksandrov7d6bc3b2015-06-16 12:26:57 +030098 rta_getattr_u16(tb[IFLA_BOND_SLAVE_AD_PARTNER_OPER_PORT_STATE]));
Jiri Pirko730d3f62014-01-23 17:52:54 +010099}
100
Nikolay Aleksandrov620dded2014-09-03 17:57:30 +0200101static int bond_slave_parse_opt(struct link_util *lu, int argc, char **argv,
102 struct nlmsghdr *n)
103{
104 __u16 queue_id;
105
106 while (argc > 0) {
107 if (matches(*argv, "queue_id") == 0) {
108 NEXT_ARG();
109 if (get_u16(&queue_id, *argv, 0))
110 invarg("queue_id is invalid", *argv);
111 addattr16(n, 1024, IFLA_BOND_SLAVE_QUEUE_ID, queue_id);
Phil Sutter25c93fa2016-07-09 11:22:46 +0200112 } else {
113 if (matches(*argv, "help") != 0)
114 fprintf(stderr,
115 "bond_slave: unknown option \"%s\"?\n",
116 *argv);
117 explain();
118 return -1;
Nikolay Aleksandrov620dded2014-09-03 17:57:30 +0200119 }
120 argc--, argv++;
121 }
122
123 return 0;
124}
125
Phil Sutter25c93fa2016-07-09 11:22:46 +0200126static void bond_slave_print_help(struct link_util *lu, int argc, char **argv,
127 FILE *f)
128{
129 print_explain(f);
130}
131
Jiri Pirko730d3f62014-01-23 17:52:54 +0100132struct link_util bond_slave_link_util = {
133 .id = "bond",
134 .maxattr = IFLA_BOND_SLAVE_MAX,
135 .print_opt = bond_slave_print_opt,
Nikolay Aleksandrov620dded2014-09-03 17:57:30 +0200136 .parse_opt = bond_slave_parse_opt,
Phil Sutter25c93fa2016-07-09 11:22:46 +0200137 .print_help = bond_slave_print_help,
Jiri Pirko730d3f62014-01-23 17:52:54 +0100138 .slave = true,
139};