Jiri Pirko | 730d3f6 | 2014-01-23 17:52:54 +0100 | [diff] [blame] | 1 | /* |
| 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 Sutter | 25c93fa | 2016-07-09 11:22:46 +0200 | [diff] [blame] | 20 | static void print_explain(FILE *f) |
| 21 | { |
| 22 | fprintf(f, "Usage: ... bond_slave [ queue_id ID ]\n"); |
| 23 | } |
| 24 | |
| 25 | static void explain(void) |
| 26 | { |
| 27 | print_explain(stderr); |
| 28 | } |
| 29 | |
Jiri Pirko | 730d3f6 | 2014-01-23 17:52:54 +0100 | [diff] [blame] | 30 | static const char *slave_states[] = { |
| 31 | [BOND_STATE_ACTIVE] = "ACTIVE", |
| 32 | [BOND_STATE_BACKUP] = "BACKUP", |
| 33 | }; |
| 34 | |
| 35 | static void print_slave_state(FILE *f, struct rtattr *tb) |
| 36 | { |
| 37 | unsigned int state = rta_getattr_u8(tb); |
| 38 | |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 39 | if (state >= ARRAY_SIZE(slave_states)) |
Jiri Pirko | 730d3f6 | 2014-01-23 17:52:54 +0100 | [diff] [blame] | 40 | fprintf(f, "state %d ", state); |
| 41 | else |
| 42 | fprintf(f, "state %s ", slave_states[state]); |
| 43 | } |
| 44 | |
| 45 | static 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 | |
| 52 | static void print_slave_mii_status(FILE *f, struct rtattr *tb) |
| 53 | { |
| 54 | unsigned int status = rta_getattr_u8(tb); |
| 55 | |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 56 | if (status >= ARRAY_SIZE(slave_mii_status)) |
Jiri Pirko | 730d3f6 | 2014-01-23 17:52:54 +0100 | [diff] [blame] | 57 | fprintf(f, "mii_status %d ", status); |
| 58 | else |
| 59 | fprintf(f, "mii_status %s ", slave_mii_status[status]); |
| 60 | } |
| 61 | |
| 62 | static 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 Aleksandrov | 7d6bc3b | 2015-06-16 12:26:57 +0300 | [diff] [blame] | 91 | |
| 92 | if (tb[IFLA_BOND_SLAVE_AD_ACTOR_OPER_PORT_STATE]) |
Nikolay Aleksandrov | e6c38e2 | 2016-02-08 17:13:58 +0100 | [diff] [blame] | 93 | fprintf(f, "ad_actor_oper_port_state %d ", |
Nikolay Aleksandrov | 7d6bc3b | 2015-06-16 12:26:57 +0300 | [diff] [blame] | 94 | 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 Aleksandrov | e6c38e2 | 2016-02-08 17:13:58 +0100 | [diff] [blame] | 97 | fprintf(f, "ad_partner_oper_port_state %d ", |
Nikolay Aleksandrov | 7d6bc3b | 2015-06-16 12:26:57 +0300 | [diff] [blame] | 98 | rta_getattr_u16(tb[IFLA_BOND_SLAVE_AD_PARTNER_OPER_PORT_STATE])); |
Jiri Pirko | 730d3f6 | 2014-01-23 17:52:54 +0100 | [diff] [blame] | 99 | } |
| 100 | |
Nikolay Aleksandrov | 620dded | 2014-09-03 17:57:30 +0200 | [diff] [blame] | 101 | static 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 Sutter | 25c93fa | 2016-07-09 11:22:46 +0200 | [diff] [blame] | 112 | } 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 Aleksandrov | 620dded | 2014-09-03 17:57:30 +0200 | [diff] [blame] | 119 | } |
| 120 | argc--, argv++; |
| 121 | } |
| 122 | |
| 123 | return 0; |
| 124 | } |
| 125 | |
Phil Sutter | 25c93fa | 2016-07-09 11:22:46 +0200 | [diff] [blame] | 126 | static void bond_slave_print_help(struct link_util *lu, int argc, char **argv, |
| 127 | FILE *f) |
| 128 | { |
| 129 | print_explain(f); |
| 130 | } |
| 131 | |
Jiri Pirko | 730d3f6 | 2014-01-23 17:52:54 +0100 | [diff] [blame] | 132 | struct link_util bond_slave_link_util = { |
Hangbin Liu | 22a8471 | 2016-09-20 18:02:12 +0800 | [diff] [blame] | 133 | .id = "bond_slave", |
Jiri Pirko | 730d3f6 | 2014-01-23 17:52:54 +0100 | [diff] [blame] | 134 | .maxattr = IFLA_BOND_SLAVE_MAX, |
| 135 | .print_opt = bond_slave_print_opt, |
Nikolay Aleksandrov | 620dded | 2014-09-03 17:57:30 +0200 | [diff] [blame] | 136 | .parse_opt = bond_slave_parse_opt, |
Phil Sutter | 25c93fa | 2016-07-09 11:22:46 +0200 | [diff] [blame] | 137 | .print_help = bond_slave_print_help, |
Jiri Pirko | 730d3f6 | 2014-01-23 17:52:54 +0100 | [diff] [blame] | 138 | }; |