2 * drivers/net/bond/bond_netlink.c - Netlink interface for bonding
3 * Copyright (c) 2013 Jiri Pirko <jiri@resnulli.us>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/module.h>
14 #include <linux/errno.h>
15 #include <linux/netdevice.h>
16 #include <linux/etherdevice.h>
17 #include <linux/if_link.h>
18 #include <linux/if_ether.h>
19 #include <net/netlink.h>
20 #include <net/rtnetlink.h>
23 static const struct nla_policy bond_policy[IFLA_BOND_MAX + 1] = {
24 [IFLA_BOND_MODE] = { .type = NLA_U8 },
25 [IFLA_BOND_ACTIVE_SLAVE] = { .type = NLA_U32 },
28 static int bond_validate(struct nlattr *tb[], struct nlattr *data[])
30 if (tb[IFLA_ADDRESS]) {
31 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
33 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
34 return -EADDRNOTAVAIL;
39 static int bond_changelink(struct net_device *bond_dev,
40 struct nlattr *tb[], struct nlattr *data[])
42 struct bonding *bond = netdev_priv(bond_dev);
45 if (data && data[IFLA_BOND_MODE]) {
46 int mode = nla_get_u8(data[IFLA_BOND_MODE]);
48 err = bond_option_mode_set(bond, mode);
52 if (data && data[IFLA_BOND_ACTIVE_SLAVE]) {
53 int ifindex = nla_get_u32(data[IFLA_BOND_ACTIVE_SLAVE]);
54 struct net_device *slave_dev;
59 slave_dev = __dev_get_by_index(dev_net(bond_dev),
64 err = bond_option_active_slave_set(bond, slave_dev);
71 static int bond_newlink(struct net *src_net, struct net_device *bond_dev,
72 struct nlattr *tb[], struct nlattr *data[])
76 err = bond_changelink(bond_dev, tb, data);
80 return register_netdevice(bond_dev);
83 static size_t bond_get_size(const struct net_device *bond_dev)
85 return nla_total_size(sizeof(u8)); /* IFLA_BOND_MODE */
86 + nla_total_size(sizeof(u32)); /* IFLA_BOND_ACTIVE_SLAVE */
89 static int bond_fill_info(struct sk_buff *skb,
90 const struct net_device *bond_dev)
92 struct bonding *bond = netdev_priv(bond_dev);
93 struct net_device *slave_dev = bond_option_active_slave_get(bond);
95 if (nla_put_u8(skb, IFLA_BOND_MODE, bond->params.mode) ||
97 nla_put_u32(skb, IFLA_BOND_ACTIVE_SLAVE, slave_dev->ifindex)))
105 struct rtnl_link_ops bond_link_ops __read_mostly = {
107 .priv_size = sizeof(struct bonding),
109 .maxtype = IFLA_BOND_MAX,
110 .policy = bond_policy,
111 .validate = bond_validate,
112 .newlink = bond_newlink,
113 .changelink = bond_changelink,
114 .get_size = bond_get_size,
115 .fill_info = bond_fill_info,
116 .get_num_tx_queues = bond_get_num_tx_queues,
117 .get_num_rx_queues = bond_get_num_tx_queues, /* Use the same number
121 int __init bond_netlink_init(void)
123 return rtnl_link_register(&bond_link_ops);
126 void bond_netlink_fini(void)
128 rtnl_link_unregister(&bond_link_ops);
131 MODULE_ALIAS_RTNL_LINK("bond");