blob: fe3500bb34e4328f1cfd09086adbc13f5429fb37 [file] [log] [blame]
Jiri Pirko0a2a78c2013-10-18 17:43:33 +02001/*
2 * drivers/net/bond/bond_netlink.c - Netlink interface for bonding
3 * Copyright (c) 2013 Jiri Pirko <jiri@resnulli.us>
4 *
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.
9 */
10
11#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
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>
21#include "bonding.h"
22
Jiri Pirko90af2312013-10-18 17:43:38 +020023static const struct nla_policy bond_policy[IFLA_BOND_MAX + 1] = {
24 [IFLA_BOND_MODE] = { .type = NLA_U8 },
Jiri Pirkoec76aa42013-10-18 17:43:39 +020025 [IFLA_BOND_ACTIVE_SLAVE] = { .type = NLA_U32 },
Jiri Pirko90af2312013-10-18 17:43:38 +020026};
27
Jiri Pirko0a2a78c2013-10-18 17:43:33 +020028static int bond_validate(struct nlattr *tb[], struct nlattr *data[])
29{
30 if (tb[IFLA_ADDRESS]) {
31 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
32 return -EINVAL;
33 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
34 return -EADDRNOTAVAIL;
35 }
36 return 0;
37}
38
Jiri Pirko90af2312013-10-18 17:43:38 +020039static int bond_changelink(struct net_device *bond_dev,
40 struct nlattr *tb[], struct nlattr *data[])
41{
42 struct bonding *bond = netdev_priv(bond_dev);
43 int err;
44
45 if (data && data[IFLA_BOND_MODE]) {
46 int mode = nla_get_u8(data[IFLA_BOND_MODE]);
47
48 err = bond_option_mode_set(bond, mode);
49 if (err)
50 return err;
51 }
Jiri Pirkoec76aa42013-10-18 17:43:39 +020052 if (data && data[IFLA_BOND_ACTIVE_SLAVE]) {
53 int ifindex = nla_get_u32(data[IFLA_BOND_ACTIVE_SLAVE]);
54 struct net_device *slave_dev;
55
56 if (ifindex == 0) {
57 slave_dev = NULL;
58 } else {
59 slave_dev = __dev_get_by_index(dev_net(bond_dev),
60 ifindex);
61 if (!slave_dev)
62 return -ENODEV;
63 }
64 err = bond_option_active_slave_set(bond, slave_dev);
65 if (err)
66 return err;
67 }
Jiri Pirko90af2312013-10-18 17:43:38 +020068 return 0;
69}
70
71static int bond_newlink(struct net *src_net, struct net_device *bond_dev,
72 struct nlattr *tb[], struct nlattr *data[])
73{
74 int err;
75
76 err = bond_changelink(bond_dev, tb, data);
77 if (err < 0)
78 return err;
79
80 return register_netdevice(bond_dev);
81}
82
83static size_t bond_get_size(const struct net_device *bond_dev)
84{
85 return nla_total_size(sizeof(u8)); /* IFLA_BOND_MODE */
Jiri Pirkoec76aa42013-10-18 17:43:39 +020086 + nla_total_size(sizeof(u32)); /* IFLA_BOND_ACTIVE_SLAVE */
Jiri Pirko90af2312013-10-18 17:43:38 +020087}
88
89static int bond_fill_info(struct sk_buff *skb,
90 const struct net_device *bond_dev)
91{
92 struct bonding *bond = netdev_priv(bond_dev);
Jiri Pirkoec76aa42013-10-18 17:43:39 +020093 struct net_device *slave_dev = bond_option_active_slave_get(bond);
Jiri Pirko90af2312013-10-18 17:43:38 +020094
Jiri Pirkoec76aa42013-10-18 17:43:39 +020095 if (nla_put_u8(skb, IFLA_BOND_MODE, bond->params.mode) ||
96 (slave_dev &&
97 nla_put_u32(skb, IFLA_BOND_ACTIVE_SLAVE, slave_dev->ifindex)))
Jiri Pirko90af2312013-10-18 17:43:38 +020098 goto nla_put_failure;
99 return 0;
100
101nla_put_failure:
102 return -EMSGSIZE;
103}
104
Jiri Pirko0a2a78c2013-10-18 17:43:33 +0200105struct rtnl_link_ops bond_link_ops __read_mostly = {
106 .kind = "bond",
107 .priv_size = sizeof(struct bonding),
108 .setup = bond_setup,
Jiri Pirko90af2312013-10-18 17:43:38 +0200109 .maxtype = IFLA_BOND_MAX,
110 .policy = bond_policy,
Jiri Pirko0a2a78c2013-10-18 17:43:33 +0200111 .validate = bond_validate,
Jiri Pirko90af2312013-10-18 17:43:38 +0200112 .newlink = bond_newlink,
113 .changelink = bond_changelink,
114 .get_size = bond_get_size,
115 .fill_info = bond_fill_info,
Jiri Pirko0a2a78c2013-10-18 17:43:33 +0200116 .get_num_tx_queues = bond_get_num_tx_queues,
117 .get_num_rx_queues = bond_get_num_tx_queues, /* Use the same number
118 as for TX queues */
119};
120
121int __init bond_netlink_init(void)
122{
123 return rtnl_link_register(&bond_link_ops);
124}
125
126void __exit bond_netlink_fini(void)
127{
128 rtnl_link_unregister(&bond_link_ops);
129}
130
131MODULE_ALIAS_RTNL_LINK("bond");