1 /*
2 * Copyright (c) 2012 Mellanox Technologies. - All rights reserved.
3 *
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
9 *
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
13 *
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
17 *
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * SOFTWARE.
31 */
32
33 #include <linux/netdevice.h>
34 #include <linux/if_arp.h> /* For ARPHRD_xxx */
35 #include <net/rtnetlink.h>
36 #include "ipoib.h"
37
38 static const struct nla_policy ipoib_policy[IFLA_IPOIB_MAX + 1] = {
39 [IFLA_IPOIB_PKEY] = { .type = NLA_U16 },
40 [IFLA_IPOIB_MODE] = { .type = NLA_U16 },
41 [IFLA_IPOIB_UMCAST] = { .type = NLA_U16 },
42 };
43
ipoib_get_max_num_queues(void)44 static unsigned int ipoib_get_max_num_queues(void)
45 {
46 return min_t(unsigned int, num_possible_cpus(), 128);
47 }
48
ipoib_fill_info(struct sk_buff * skb,const struct net_device * dev)49 static int ipoib_fill_info(struct sk_buff *skb, const struct net_device *dev)
50 {
51 struct ipoib_dev_priv *priv = ipoib_priv(dev);
52 u16 val;
53
54 if (nla_put_u16(skb, IFLA_IPOIB_PKEY, priv->pkey))
55 goto nla_put_failure;
56
57 val = test_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags);
58 if (nla_put_u16(skb, IFLA_IPOIB_MODE, val))
59 goto nla_put_failure;
60
61 val = test_bit(IPOIB_FLAG_UMCAST, &priv->flags);
62 if (nla_put_u16(skb, IFLA_IPOIB_UMCAST, val))
63 goto nla_put_failure;
64
65 return 0;
66
67 nla_put_failure:
68 return -EMSGSIZE;
69 }
70
ipoib_changelink(struct net_device * dev,struct nlattr * tb[],struct nlattr * data[],struct netlink_ext_ack * extack)71 static int ipoib_changelink(struct net_device *dev, struct nlattr *tb[],
72 struct nlattr *data[],
73 struct netlink_ext_ack *extack)
74 {
75 u16 mode, umcast;
76 int ret = 0;
77
78 if (data[IFLA_IPOIB_MODE]) {
79 mode = nla_get_u16(data[IFLA_IPOIB_MODE]);
80 if (mode == IPOIB_MODE_DATAGRAM)
81 ret = ipoib_set_mode(dev, "datagram\n");
82 else if (mode == IPOIB_MODE_CONNECTED)
83 ret = ipoib_set_mode(dev, "connected\n");
84 else
85 ret = -EINVAL;
86
87 if (ret < 0)
88 goto out_err;
89 }
90
91 if (data[IFLA_IPOIB_UMCAST]) {
92 umcast = nla_get_u16(data[IFLA_IPOIB_UMCAST]);
93 ipoib_set_umcast(dev, umcast);
94 }
95
96 out_err:
97 return ret;
98 }
99
ipoib_new_child_link(struct net_device * dev,struct rtnl_newlink_params * params,struct netlink_ext_ack * extack)100 static int ipoib_new_child_link(struct net_device *dev,
101 struct rtnl_newlink_params *params,
102 struct netlink_ext_ack *extack)
103 {
104 struct net *link_net = rtnl_newlink_link_net(params);
105 struct nlattr **data = params->data;
106 struct nlattr **tb = params->tb;
107 struct net_device *pdev;
108 struct ipoib_dev_priv *ppriv;
109 u16 child_pkey;
110 int err;
111
112 if (!tb[IFLA_LINK])
113 return -EINVAL;
114
115 pdev = __dev_get_by_index(link_net, nla_get_u32(tb[IFLA_LINK]));
116 if (!pdev || pdev->type != ARPHRD_INFINIBAND)
117 return -ENODEV;
118
119 ppriv = ipoib_priv(pdev);
120
121 if (test_bit(IPOIB_FLAG_SUBINTERFACE, &ppriv->flags)) {
122 ipoib_warn(ppriv, "child creation disallowed for child devices\n");
123 return -EINVAL;
124 }
125
126 if (!data || !data[IFLA_IPOIB_PKEY]) {
127 ipoib_dbg(ppriv, "no pkey specified, using parent pkey\n");
128 child_pkey = ppriv->pkey;
129 } else
130 child_pkey = nla_get_u16(data[IFLA_IPOIB_PKEY]);
131
132 err = ipoib_intf_init(ppriv->ca, ppriv->port, dev->name, dev);
133 if (err) {
134 ipoib_warn(ppriv, "failed to initialize pkey device\n");
135 return err;
136 }
137
138 err = __ipoib_vlan_add(ppriv, ipoib_priv(dev),
139 child_pkey, IPOIB_RTNL_CHILD);
140 if (err)
141 return err;
142
143 if (data) {
144 err = ipoib_changelink(dev, tb, data, extack);
145 if (err) {
146 unregister_netdevice(dev);
147 return err;
148 }
149 }
150
151 return 0;
152 }
153
ipoib_del_child_link(struct net_device * dev,struct list_head * head)154 static void ipoib_del_child_link(struct net_device *dev, struct list_head *head)
155 {
156 struct ipoib_dev_priv *priv = ipoib_priv(dev);
157
158 if (!priv->parent)
159 return;
160
161 unregister_netdevice_queue(dev, head);
162 }
163
ipoib_get_size(const struct net_device * dev)164 static size_t ipoib_get_size(const struct net_device *dev)
165 {
166 return nla_total_size(2) + /* IFLA_IPOIB_PKEY */
167 nla_total_size(2) + /* IFLA_IPOIB_MODE */
168 nla_total_size(2); /* IFLA_IPOIB_UMCAST */
169 }
170
171 static struct rtnl_link_ops ipoib_link_ops __read_mostly = {
172 .kind = "ipoib",
173 .netns_refund = true,
174 .maxtype = IFLA_IPOIB_MAX,
175 .policy = ipoib_policy,
176 .priv_size = sizeof(struct ipoib_dev_priv),
177 .setup = ipoib_setup_common,
178 .newlink = ipoib_new_child_link,
179 .dellink = ipoib_del_child_link,
180 .changelink = ipoib_changelink,
181 .get_size = ipoib_get_size,
182 .fill_info = ipoib_fill_info,
183 .get_num_rx_queues = ipoib_get_max_num_queues,
184 .get_num_tx_queues = ipoib_get_max_num_queues,
185 };
186
ipoib_get_link_ops(void)187 struct rtnl_link_ops *ipoib_get_link_ops(void)
188 {
189 return &ipoib_link_ops;
190 }
191
ipoib_netlink_init(void)192 int __init ipoib_netlink_init(void)
193 {
194 return rtnl_link_register(&ipoib_link_ops);
195 }
196
ipoib_netlink_fini(void)197 void __exit ipoib_netlink_fini(void)
198 {
199 rtnl_link_unregister(&ipoib_link_ops);
200 }
201
202 MODULE_ALIAS_RTNL_LINK("ipoib");
203