blob: c670d9c2cda74653bc4524d5062626e7fe071c57 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (c) 2004, 2005 Topspin Communications. All rights reserved.
Roland Dreier2a1d9b72005-08-10 23:03:10 -07003 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
4 * Copyright (c) 2004 Voltaire, Inc. All rights reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * This software is available to you under a choice of one of two
7 * licenses. You may choose to be licensed under the terms of the GNU
8 * General Public License (GPL) Version 2, available from the file
9 * COPYING in the main directory of this source tree, or the
10 * OpenIB.org BSD license below:
11 *
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
14 * conditions are met:
15 *
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
18 * disclaimer.
19 *
20 * - Redistributions in binary form must reproduce the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer in the documentation and/or other materials
23 * provided with the distribution.
24 *
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 * SOFTWARE.
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 */
34
35#include <linux/skbuff.h>
36#include <linux/rtnetlink.h>
Paul Gortmakerfec14d22011-08-30 12:32:52 -040037#include <linux/moduleparam.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/ip.h>
39#include <linux/in.h>
40#include <linux/igmp.h>
41#include <linux/inetdevice.h>
42#include <linux/delay.h>
43#include <linux/completion.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020046#include <net/dst.h>
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include "ipoib.h"
49
50#ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
51static int mcast_debug_level;
52
53module_param(mcast_debug_level, int, 0644);
54MODULE_PARM_DESC(mcast_debug_level,
55 "Enable multicast debug tracing if > 0");
56#endif
57
Ingo Molnar95ed6442006-01-13 14:51:39 -080058static DEFINE_MUTEX(mcast_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Linus Torvalds1da177e2005-04-16 15:20:36 -070060struct ipoib_mcast_iter {
61 struct net_device *dev;
62 union ib_gid mgid;
63 unsigned long created;
64 unsigned int queuelen;
65 unsigned int complete;
66 unsigned int send_only;
67};
68
Doug Ledford69911412015-02-21 19:27:05 -050069/*
70 * This should be called with the mcast_mutex held
71 */
72static void __ipoib_mcast_schedule_join_thread(struct ipoib_dev_priv *priv,
73 struct ipoib_mcast *mcast,
74 bool delay)
75{
76 if (!test_bit(IPOIB_MCAST_RUN, &priv->flags))
77 return;
78
79 /*
80 * We will be scheduling *something*, so cancel whatever is
81 * currently scheduled first
82 */
83 cancel_delayed_work(&priv->mcast_task);
84 if (mcast && delay) {
85 /*
86 * We had a failure and want to schedule a retry later
87 */
88 mcast->backoff *= 2;
89 if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
90 mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
91 mcast->delay_until = jiffies + (mcast->backoff * HZ);
92 /*
93 * Mark this mcast for its delay, but restart the
94 * task immediately. The join task will make sure to
95 * clear out all entries without delays, and then
96 * schedule itself to run again when the earliest
97 * delay expires
98 */
99 queue_delayed_work(priv->wq, &priv->mcast_task, 0);
100 } else if (delay) {
101 /*
102 * Special case of retrying after a failure to
103 * allocate the broadcast multicast group, wait
104 * 1 second and try again
105 */
106 queue_delayed_work(priv->wq, &priv->mcast_task, HZ);
107 } else
108 queue_delayed_work(priv->wq, &priv->mcast_task, 0);
109}
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111static void ipoib_mcast_free(struct ipoib_mcast *mcast)
112{
113 struct net_device *dev = mcast->dev;
Michael S. Tsirkinb36f170b62006-01-17 12:19:40 -0800114 int tx_dropped = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700116 ipoib_dbg_mcast(netdev_priv(dev), "deleting multicast group %pI6\n",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700117 mcast->mcmember.mgid.raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000119 /* remove all neigh connected to this mcast */
120 ipoib_del_neighs_by_gid(dev, mcast->mcmember.mgid.raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 if (mcast->ah)
123 ipoib_put_ah(mcast->ah);
124
Michael S. Tsirkinb36f170b62006-01-17 12:19:40 -0800125 while (!skb_queue_empty(&mcast->pkt_queue)) {
126 ++tx_dropped;
Roland Dreier8c608a32005-11-07 10:49:38 -0800127 dev_kfree_skb_any(skb_dequeue(&mcast->pkt_queue));
Michael S. Tsirkinb36f170b62006-01-17 12:19:40 -0800128 }
129
Roland Dreier943c2462008-09-30 10:36:21 -0700130 netif_tx_lock_bh(dev);
Roland Dreierde903512007-09-28 15:33:51 -0700131 dev->stats.tx_dropped += tx_dropped;
Roland Dreier943c2462008-09-30 10:36:21 -0700132 netif_tx_unlock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134 kfree(mcast);
135}
136
137static struct ipoib_mcast *ipoib_mcast_alloc(struct net_device *dev,
138 int can_sleep)
139{
140 struct ipoib_mcast *mcast;
141
Roland Dreierde6eb662005-11-02 07:23:14 -0800142 mcast = kzalloc(sizeof *mcast, can_sleep ? GFP_KERNEL : GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 if (!mcast)
144 return NULL;
145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 mcast->dev = dev;
147 mcast->created = jiffies;
Doug Ledford69911412015-02-21 19:27:05 -0500148 mcast->delay_until = jiffies;
Hal Rosenstockce5b65c2005-09-18 13:47:53 -0700149 mcast->backoff = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151 INIT_LIST_HEAD(&mcast->list);
152 INIT_LIST_HEAD(&mcast->neigh_list);
153 skb_queue_head_init(&mcast->pkt_queue);
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 return mcast;
156}
157
Jack Morgenstein37c22a72006-05-29 19:14:05 +0300158static struct ipoib_mcast *__ipoib_mcast_find(struct net_device *dev, void *mgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
160 struct ipoib_dev_priv *priv = netdev_priv(dev);
161 struct rb_node *n = priv->multicast_tree.rb_node;
162
163 while (n) {
164 struct ipoib_mcast *mcast;
165 int ret;
166
167 mcast = rb_entry(n, struct ipoib_mcast, rb_node);
168
Jack Morgenstein37c22a72006-05-29 19:14:05 +0300169 ret = memcmp(mgid, mcast->mcmember.mgid.raw,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 sizeof (union ib_gid));
171 if (ret < 0)
172 n = n->rb_left;
173 else if (ret > 0)
174 n = n->rb_right;
175 else
176 return mcast;
177 }
178
179 return NULL;
180}
181
182static int __ipoib_mcast_add(struct net_device *dev, struct ipoib_mcast *mcast)
183{
184 struct ipoib_dev_priv *priv = netdev_priv(dev);
185 struct rb_node **n = &priv->multicast_tree.rb_node, *pn = NULL;
186
187 while (*n) {
188 struct ipoib_mcast *tmcast;
189 int ret;
190
191 pn = *n;
192 tmcast = rb_entry(pn, struct ipoib_mcast, rb_node);
193
194 ret = memcmp(mcast->mcmember.mgid.raw, tmcast->mcmember.mgid.raw,
195 sizeof (union ib_gid));
196 if (ret < 0)
197 n = &pn->rb_left;
198 else if (ret > 0)
199 n = &pn->rb_right;
200 else
201 return -EEXIST;
202 }
203
204 rb_link_node(&mcast->rb_node, pn, n);
205 rb_insert_color(&mcast->rb_node, &priv->multicast_tree);
206
207 return 0;
208}
209
210static int ipoib_mcast_join_finish(struct ipoib_mcast *mcast,
211 struct ib_sa_mcmember_rec *mcmember)
212{
213 struct net_device *dev = mcast->dev;
214 struct ipoib_dev_priv *priv = netdev_priv(dev);
Eli Cohen7343b232006-02-27 20:47:43 -0800215 struct ipoib_ah *ah;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 int ret;
Eli Cohend0de1362008-07-14 23:48:50 -0700217 int set_qkey = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
219 mcast->mcmember = *mcmember;
220
Patrick McHardybea1e222012-08-30 07:01:30 +0000221 /* Set the multicast MTU and cached Q_Key before we attach if it's
222 * the broadcast group.
223 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 if (!memcmp(mcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
225 sizeof (union ib_gid))) {
Jack Morgensteine1d50dc2008-05-20 15:41:09 -0700226 spin_lock_irq(&priv->lock);
227 if (!priv->broadcast) {
228 spin_unlock_irq(&priv->lock);
229 return -EAGAIN;
230 }
Patrick McHardybea1e222012-08-30 07:01:30 +0000231 priv->mcast_mtu = IPOIB_UD_MTU(ib_mtu_enum_to_int(priv->broadcast->mcmember.mtu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 priv->qkey = be32_to_cpu(priv->broadcast->mcmember.qkey);
Jack Morgensteine1d50dc2008-05-20 15:41:09 -0700233 spin_unlock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 priv->tx_wr.wr.ud.remote_qkey = priv->qkey;
Eli Cohend0de1362008-07-14 23:48:50 -0700235 set_qkey = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 }
237
238 if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
239 if (test_and_set_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700240 ipoib_warn(priv, "multicast group %pI6 already attached\n",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700241 mcast->mcmember.mgid.raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
243 return 0;
244 }
245
246 ret = ipoib_mcast_attach(dev, be16_to_cpu(mcast->mcmember.mlid),
Eli Cohend0de1362008-07-14 23:48:50 -0700247 &mcast->mcmember.mgid, set_qkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 if (ret < 0) {
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700249 ipoib_warn(priv, "couldn't attach QP to multicast group %pI6\n",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700250 mcast->mcmember.mgid.raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252 clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags);
253 return ret;
254 }
255 }
256
257 {
258 struct ib_ah_attr av = {
259 .dlid = be16_to_cpu(mcast->mcmember.mlid),
260 .port_num = priv->port,
261 .sl = mcast->mcmember.sl,
262 .ah_flags = IB_AH_GRH,
Jack Morgensteinbf6a9e32006-04-10 09:43:47 -0700263 .static_rate = mcast->mcmember.rate,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 .grh = {
265 .flow_label = be32_to_cpu(mcast->mcmember.flow_label),
266 .hop_limit = mcast->mcmember.hop_limit,
267 .sgid_index = 0,
268 .traffic_class = mcast->mcmember.traffic_class
269 }
270 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 av.grh.dgid = mcast->mcmember.mgid;
272
Eli Cohen7343b232006-02-27 20:47:43 -0800273 ah = ipoib_create_ah(dev, priv->pd, &av);
Mike Marciniszyn38743972011-11-21 08:43:54 -0500274 if (IS_ERR(ah)) {
275 ipoib_warn(priv, "ib_address_create failed %ld\n",
276 -PTR_ERR(ah));
277 /* use original error */
278 return PTR_ERR(ah);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 } else {
Or Gerlitz624d01f2006-07-24 10:42:00 +0300280 spin_lock_irq(&priv->lock);
281 mcast->ah = ah;
282 spin_unlock_irq(&priv->lock);
283
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700284 ipoib_dbg_mcast(priv, "MGID %pI6 AV %p, LID 0x%04x, SL %d\n",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700285 mcast->mcmember.mgid.raw,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 mcast->ah->ah,
287 be16_to_cpu(mcast->mcmember.mlid),
288 mcast->mcmember.sl);
289 }
290 }
291
292 /* actually send any queued packets */
Roland Dreier943c2462008-09-30 10:36:21 -0700293 netif_tx_lock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 while (!skb_queue_empty(&mcast->pkt_queue)) {
295 struct sk_buff *skb = skb_dequeue(&mcast->pkt_queue);
David S. Miller69cce1d2011-07-17 23:09:49 -0700296
Roland Dreier943c2462008-09-30 10:36:21 -0700297 netif_tx_unlock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
299 skb->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 if (dev_queue_xmit(skb))
301 ipoib_warn(priv, "dev_queue_xmit failed to requeue packet\n");
Roland Dreier936d7de2012-02-07 14:51:21 +0000302
Roland Dreier943c2462008-09-30 10:36:21 -0700303 netif_tx_lock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 }
Roland Dreier943c2462008-09-30 10:36:21 -0700305 netif_tx_unlock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
307 return 0;
308}
309
Yossi Etigine8224e42008-09-16 11:57:45 -0700310void ipoib_mcast_carrier_on_task(struct work_struct *work)
311{
312 struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
313 carrier_on_task);
Moni Shoua5ee951202009-09-24 12:01:05 -0700314 struct ib_port_attr attr;
Yossi Etigine8224e42008-09-16 11:57:45 -0700315
Moni Shoua5ee951202009-09-24 12:01:05 -0700316 if (ib_query_port(priv->ca, priv->port, &attr) ||
317 attr.state != IB_PORT_ACTIVE) {
318 ipoib_dbg(priv, "Keeping carrier off until IB port is active\n");
319 return;
320 }
321
Doug Ledford894021a2015-02-21 19:27:02 -0500322 /*
323 * Take rtnl_lock to avoid racing with ipoib_stop() and
324 * turning the carrier back on while a device is being
325 * removed. However, ipoib_stop() will attempt to flush
326 * the workqueue while holding the rtnl lock, so loop
327 * on trylock until either we get the lock or we see
328 * FLAG_OPER_UP go away as that signals that we are bailing
329 * and can safely ignore the carrier on work.
330 */
331 while (!rtnl_trylock()) {
332 if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags))
333 return;
334 else
335 msleep(20);
336 }
Doug Ledfordc84ca6d2015-02-21 19:27:01 -0500337 if (!ipoib_cm_admin_enabled(priv->dev))
338 dev_set_mtu(priv->dev, min(priv->mcast_mtu, priv->admin_mtu));
Yossi Etigine8224e42008-09-16 11:57:45 -0700339 netif_carrier_on(priv->dev);
340 rtnl_unlock();
341}
342
Sean Heftyfaec2f72007-02-15 17:00:17 -0800343static int ipoib_mcast_join_complete(int status,
344 struct ib_sa_multicast *multicast)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
Sean Heftyfaec2f72007-02-15 17:00:17 -0800346 struct ipoib_mcast *mcast = multicast->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 struct net_device *dev = mcast->dev;
348 struct ipoib_dev_priv *priv = netdev_priv(dev);
349
Doug Ledfordd2fe9372015-02-21 19:27:06 -0500350 ipoib_dbg_mcast(priv, "%sjoin completion for %pI6 (status %d)\n",
351 test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags) ?
352 "sendonly " : "",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700353 mcast->mcmember.mgid.raw, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Doug Ledford69911412015-02-21 19:27:05 -0500355 /*
356 * We have to take the mutex to force mcast_join to
357 * return from ib_sa_multicast_join and set mcast->mc to a
358 * valid value. Otherwise we were racing with ourselves in
359 * that we might fail here, but get a valid return from
360 * ib_sa_multicast_join after we had cleared mcast->mc here,
361 * resulting in mis-matched joins and leaves and a deadlock
362 */
363 mutex_lock(&mcast_mutex);
364
Sean Heftyfaec2f72007-02-15 17:00:17 -0800365 /* We trap for port events ourselves. */
Roland Dreiere7a623d2015-01-30 15:39:20 -0800366 if (status == -ENETRESET) {
367 status = 0;
Erez Shitrita9c8ba52013-10-16 17:37:51 +0300368 goto out;
Roland Dreiere7a623d2015-01-30 15:39:20 -0800369 }
Sean Heftyfaec2f72007-02-15 17:00:17 -0800370
371 if (!status)
372 status = ipoib_mcast_join_finish(mcast, &multicast->rec);
373
374 if (!status) {
Hal Rosenstockce5b65c2005-09-18 13:47:53 -0700375 mcast->backoff = 1;
Doug Ledford69911412015-02-21 19:27:05 -0500376 mcast->delay_until = jiffies;
Shirley Ma55c9add2007-03-08 14:59:30 -0800377
Yossi Etigine8224e42008-09-16 11:57:45 -0700378 /*
Doug Ledford0b395782015-02-21 19:27:03 -0500379 * Defer carrier on work to priv->wq to avoid a
Doug Ledfordd2fe9372015-02-21 19:27:06 -0500380 * deadlock on rtnl_lock here. Requeue our multicast
381 * work too, which will end up happening right after
382 * our carrier on task work and will allow us to
383 * send out all of the non-broadcast joins
Yossi Etigine8224e42008-09-16 11:57:45 -0700384 */
Doug Ledfordd2fe9372015-02-21 19:27:06 -0500385 if (mcast == priv->broadcast) {
Doug Ledford0b395782015-02-21 19:27:03 -0500386 queue_work(priv->wq, &priv->carrier_on_task);
Doug Ledfordd2fe9372015-02-21 19:27:06 -0500387 __ipoib_mcast_schedule_join_thread(priv, NULL, 0);
388 }
Doug Ledford69911412015-02-21 19:27:05 -0500389 } else {
390 if (mcast->logcount++ < 20) {
391 if (status == -ETIMEDOUT || status == -EAGAIN) {
Doug Ledfordd2fe9372015-02-21 19:27:06 -0500392 ipoib_dbg_mcast(priv, "%smulticast join failed for %pI6, status %d\n",
393 test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags) ? "sendonly " : "",
Doug Ledford69911412015-02-21 19:27:05 -0500394 mcast->mcmember.mgid.raw, status);
395 } else {
Doug Ledfordd2fe9372015-02-21 19:27:06 -0500396 ipoib_warn(priv, "%smulticast join failed for %pI6, status %d\n",
397 test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags) ? "sendonly " : "",
Doug Ledford69911412015-02-21 19:27:05 -0500398 mcast->mcmember.mgid.raw, status);
399 }
Roland Dreiere7a623d2015-01-30 15:39:20 -0800400 }
Doug Ledford69911412015-02-21 19:27:05 -0500401
Doug Ledfordd2fe9372015-02-21 19:27:06 -0500402 if (test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags) &&
403 mcast->backoff >= 2) {
404 /*
405 * We only retry sendonly joins once before we drop
406 * the packet and quit trying to deal with the
407 * group. However, we leave the group in the
408 * mcast list as an unjoined group. If we want to
409 * try joining again, we simply queue up a packet
410 * and restart the join thread. The empty queue
411 * is why the join thread ignores this group.
412 */
413 mcast->backoff = 1;
414 netif_tx_lock_bh(dev);
415 while (!skb_queue_empty(&mcast->pkt_queue)) {
416 ++dev->stats.tx_dropped;
417 dev_kfree_skb_any(skb_dequeue(&mcast->pkt_queue));
418 }
419 netif_tx_unlock_bh(dev);
420 } else
421 /* Requeue this join task with a backoff delay */
422 __ipoib_mcast_schedule_join_thread(priv, mcast, 1);
Roland Dreiere7a623d2015-01-30 15:39:20 -0800423 }
Roland Dreiere7a623d2015-01-30 15:39:20 -0800424out:
Doug Ledford69911412015-02-21 19:27:05 -0500425 clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
426 if (status)
427 mcast->mc = NULL;
Roland Dreiere7a623d2015-01-30 15:39:20 -0800428 complete(&mcast->done);
Doug Ledford69911412015-02-21 19:27:05 -0500429 mutex_unlock(&mcast_mutex);
Sean Heftyfaec2f72007-02-15 17:00:17 -0800430 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431}
432
433static void ipoib_mcast_join(struct net_device *dev, struct ipoib_mcast *mcast,
434 int create)
435{
436 struct ipoib_dev_priv *priv = netdev_priv(dev);
437 struct ib_sa_mcmember_rec rec = {
438 .join_state = 1
439 };
440 ib_sa_comp_mask comp_mask;
441 int ret = 0;
442
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700443 ipoib_dbg_mcast(priv, "joining MGID %pI6\n", mcast->mcmember.mgid.raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 rec.mgid = mcast->mcmember.mgid;
446 rec.port_gid = priv->local_gid;
Sean Hefty97f52eb2005-08-13 21:05:57 -0700447 rec.pkey = cpu_to_be16(priv->pkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449 comp_mask =
450 IB_SA_MCMEMBER_REC_MGID |
451 IB_SA_MCMEMBER_REC_PORT_GID |
452 IB_SA_MCMEMBER_REC_PKEY |
453 IB_SA_MCMEMBER_REC_JOIN_STATE;
454
455 if (create) {
456 comp_mask |=
Roland Dreierd0df6d62006-09-22 15:22:56 -0700457 IB_SA_MCMEMBER_REC_QKEY |
458 IB_SA_MCMEMBER_REC_MTU_SELECTOR |
459 IB_SA_MCMEMBER_REC_MTU |
460 IB_SA_MCMEMBER_REC_TRAFFIC_CLASS |
461 IB_SA_MCMEMBER_REC_RATE_SELECTOR |
462 IB_SA_MCMEMBER_REC_RATE |
463 IB_SA_MCMEMBER_REC_SL |
464 IB_SA_MCMEMBER_REC_FLOW_LABEL |
465 IB_SA_MCMEMBER_REC_HOP_LIMIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
467 rec.qkey = priv->broadcast->mcmember.qkey;
Roland Dreierd0df6d62006-09-22 15:22:56 -0700468 rec.mtu_selector = IB_SA_EQ;
469 rec.mtu = priv->broadcast->mcmember.mtu;
470 rec.traffic_class = priv->broadcast->mcmember.traffic_class;
471 rec.rate_selector = IB_SA_EQ;
472 rec.rate = priv->broadcast->mcmember.rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 rec.sl = priv->broadcast->mcmember.sl;
474 rec.flow_label = priv->broadcast->mcmember.flow_label;
Roland Dreierd0df6d62006-09-22 15:22:56 -0700475 rec.hop_limit = priv->broadcast->mcmember.hop_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 }
477
Doug Ledford69911412015-02-21 19:27:05 -0500478 mutex_lock(&mcast_mutex);
Sean Heftyfaec2f72007-02-15 17:00:17 -0800479 mcast->mc = ib_sa_join_multicast(&ipoib_sa_client, priv->ca, priv->port,
480 &rec, comp_mask, GFP_KERNEL,
481 ipoib_mcast_join_complete, mcast);
482 if (IS_ERR(mcast->mc)) {
483 clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
484 ret = PTR_ERR(mcast->mc);
485 ipoib_warn(priv, "ib_sa_join_multicast failed, status %d\n", ret);
Doug Ledford69911412015-02-21 19:27:05 -0500486 __ipoib_mcast_schedule_join_thread(priv, mcast, 1);
487 complete(&mcast->done);
Sean Heftyfaec2f72007-02-15 17:00:17 -0800488 }
Doug Ledford69911412015-02-21 19:27:05 -0500489 mutex_unlock(&mcast_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490}
491
David Howellsc4028952006-11-22 14:57:56 +0000492void ipoib_mcast_join_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
David Howellsc4028952006-11-22 14:57:56 +0000494 struct ipoib_dev_priv *priv =
495 container_of(work, struct ipoib_dev_priv, mcast_task.work);
496 struct net_device *dev = priv->dev;
Erez Shitrit94232d92013-10-16 17:37:53 +0300497 struct ib_port_attr port_attr;
Doug Ledford69911412015-02-21 19:27:05 -0500498 unsigned long delay_until = 0;
499 struct ipoib_mcast *mcast = NULL;
500 int create = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502 if (!test_bit(IPOIB_MCAST_RUN, &priv->flags))
503 return;
504
Erez Shitrit94232d92013-10-16 17:37:53 +0300505 if (ib_query_port(priv->ca, priv->port, &port_attr) ||
506 port_attr.state != IB_PORT_ACTIVE) {
507 ipoib_dbg(priv, "port state is not ACTIVE (state = %d) suspending join task\n",
508 port_attr.state);
509 return;
510 }
Alex Estrin68f9d832014-08-20 11:15:08 -0400511 priv->local_lid = port_attr.lid;
Erez Shitrit94232d92013-10-16 17:37:53 +0300512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 if (ib_query_gid(priv->ca, priv->port, 0, &priv->local_gid))
Michael S. Tsirkin24bd1e42007-05-18 16:12:54 +0300514 ipoib_warn(priv, "ib_query_gid() failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 else
516 memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));
517
Doug Ledford69911412015-02-21 19:27:05 -0500518 /*
519 * We have to hold the mutex to keep from racing with the join
520 * completion threads on setting flags on mcasts, and we have
521 * to hold the priv->lock because dev_flush will remove entries
522 * out from underneath us, so at a minimum we need the lock
523 * through the time that we do the for_each loop of the mcast
524 * list or else dev_flush can make us oops.
525 */
526 mutex_lock(&mcast_mutex);
527 spin_lock_irq(&priv->lock);
528 if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags))
529 goto out;
530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 if (!priv->broadcast) {
Roland Dreier20b83382006-02-11 12:22:12 -0800532 struct ipoib_mcast *broadcast;
533
Doug Ledford69911412015-02-21 19:27:05 -0500534 broadcast = ipoib_mcast_alloc(dev, 0);
Roland Dreier20b83382006-02-11 12:22:12 -0800535 if (!broadcast) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 ipoib_warn(priv, "failed to allocate broadcast group\n");
Doug Ledford69911412015-02-21 19:27:05 -0500537 /*
538 * Restart us after a 1 second delay to retry
539 * creating our broadcast group and attaching to
540 * it. Until this succeeds, this ipoib dev is
541 * completely stalled (multicast wise).
542 */
543 __ipoib_mcast_schedule_join_thread(priv, NULL, 1);
544 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 }
546
Roland Dreier20b83382006-02-11 12:22:12 -0800547 memcpy(broadcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
548 sizeof (union ib_gid));
549 priv->broadcast = broadcast;
550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 __ipoib_mcast_add(dev, priv->broadcast);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 }
553
554 if (!test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
Doug Ledford69911412015-02-21 19:27:05 -0500555 if (IS_ERR_OR_NULL(priv->broadcast->mc) &&
556 !test_bit(IPOIB_MCAST_FLAG_BUSY, &priv->broadcast->flags)) {
557 mcast = priv->broadcast;
558 create = 0;
559 if (mcast->backoff > 1 &&
560 time_before(jiffies, mcast->delay_until)) {
561 delay_until = mcast->delay_until;
562 mcast = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 }
564 }
Doug Ledford69911412015-02-21 19:27:05 -0500565 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 }
567
Doug Ledford69911412015-02-21 19:27:05 -0500568 /*
569 * We'll never get here until the broadcast group is both allocated
570 * and attached
571 */
572 list_for_each_entry(mcast, &priv->multicast_list, list) {
573 if (IS_ERR_OR_NULL(mcast->mc) &&
574 !test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags) &&
Doug Ledfordd2fe9372015-02-21 19:27:06 -0500575 (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags) ||
576 !skb_queue_empty(&mcast->pkt_queue))) {
Doug Ledford69911412015-02-21 19:27:05 -0500577 if (mcast->backoff == 1 ||
Doug Ledfordd2fe9372015-02-21 19:27:06 -0500578 time_after_eq(jiffies, mcast->delay_until)) {
Doug Ledford69911412015-02-21 19:27:05 -0500579 /* Found the next unjoined group */
Doug Ledfordd2fe9372015-02-21 19:27:06 -0500580 init_completion(&mcast->done);
581 set_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
582 if (test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags))
583 create = 0;
584 else
585 create = 1;
586 spin_unlock_irq(&priv->lock);
587 mutex_unlock(&mcast_mutex);
588 ipoib_mcast_join(dev, mcast, create);
589 mutex_lock(&mcast_mutex);
590 spin_lock_irq(&priv->lock);
591 } else if (!delay_until ||
Doug Ledford69911412015-02-21 19:27:05 -0500592 time_before(mcast->delay_until, delay_until))
593 delay_until = mcast->delay_until;
594 }
595 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Doug Ledfordd2fe9372015-02-21 19:27:06 -0500597 mcast = NULL;
598 ipoib_dbg_mcast(priv, "successfully started all multicast joins\n");
Doug Ledford69911412015-02-21 19:27:05 -0500599
600out:
Doug Ledfordd2fe9372015-02-21 19:27:06 -0500601 if (delay_until) {
602 cancel_delayed_work(&priv->mcast_task);
603 queue_delayed_work(priv->wq, &priv->mcast_task,
604 delay_until - jiffies);
605 }
Doug Ledford69911412015-02-21 19:27:05 -0500606 if (mcast) {
607 init_completion(&mcast->done);
608 set_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
609 }
610 spin_unlock_irq(&priv->lock);
611 mutex_unlock(&mcast_mutex);
Doug Ledfordd2fe9372015-02-21 19:27:06 -0500612 if (mcast)
613 ipoib_mcast_join(dev, mcast, create);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614}
615
616int ipoib_mcast_start_thread(struct net_device *dev)
617{
618 struct ipoib_dev_priv *priv = netdev_priv(dev);
619
620 ipoib_dbg_mcast(priv, "starting multicast thread\n");
621
Ingo Molnar95ed6442006-01-13 14:51:39 -0800622 mutex_lock(&mcast_mutex);
Doug Ledford69911412015-02-21 19:27:05 -0500623 set_bit(IPOIB_MCAST_RUN, &priv->flags);
624 __ipoib_mcast_schedule_join_thread(priv, NULL, 0);
Ingo Molnar95ed6442006-01-13 14:51:39 -0800625 mutex_unlock(&mcast_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
627 return 0;
628}
629
Doug Ledfordefc82ee2015-02-21 19:27:04 -0500630int ipoib_mcast_stop_thread(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
632 struct ipoib_dev_priv *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634 ipoib_dbg_mcast(priv, "stopping multicast thread\n");
635
Ingo Molnar95ed6442006-01-13 14:51:39 -0800636 mutex_lock(&mcast_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 clear_bit(IPOIB_MCAST_RUN, &priv->flags);
638 cancel_delayed_work(&priv->mcast_task);
Ingo Molnar95ed6442006-01-13 14:51:39 -0800639 mutex_unlock(&mcast_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
Doug Ledfordefc82ee2015-02-21 19:27:04 -0500641 flush_workqueue(priv->wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 return 0;
644}
645
646static int ipoib_mcast_leave(struct net_device *dev, struct ipoib_mcast *mcast)
647{
648 struct ipoib_dev_priv *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 int ret = 0;
650
Sean Heftye07832b2007-03-19 14:31:36 -0800651 if (test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
Doug Ledford69911412015-02-21 19:27:05 -0500652 ipoib_warn(priv, "ipoib_mcast_leave on an in-flight join\n");
653
654 if (!IS_ERR_OR_NULL(mcast->mc))
Sean Heftye07832b2007-03-19 14:31:36 -0800655 ib_sa_free_multicast(mcast->mc);
656
Sean Heftyfaec2f72007-02-15 17:00:17 -0800657 if (test_and_clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700658 ipoib_dbg_mcast(priv, "leaving MGID %pI6\n",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700659 mcast->mcmember.mgid.raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Sean Heftyfaec2f72007-02-15 17:00:17 -0800661 /* Remove ourselves from the multicast group */
Roland Dreier9eae5542008-07-14 23:48:50 -0700662 ret = ib_detach_mcast(priv->qp, &mcast->mcmember.mgid,
663 be16_to_cpu(mcast->mcmember.mlid));
Sean Heftyfaec2f72007-02-15 17:00:17 -0800664 if (ret)
Roland Dreier9eae5542008-07-14 23:48:50 -0700665 ipoib_warn(priv, "ib_detach_mcast failed (result = %d)\n", ret);
Doug Ledford69911412015-02-21 19:27:05 -0500666 } else if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags))
667 ipoib_dbg(priv, "leaving with no mcmember but not a "
668 "SENDONLY join\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 return 0;
671}
672
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000673void ipoib_mcast_send(struct net_device *dev, u8 *daddr, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674{
675 struct ipoib_dev_priv *priv = netdev_priv(dev);
676 struct ipoib_mcast *mcast;
Roland Dreier943c2462008-09-30 10:36:21 -0700677 unsigned long flags;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000678 void *mgid = daddr + 4;
David S. Miller700db992012-07-05 21:08:05 -0700679
Roland Dreier943c2462008-09-30 10:36:21 -0700680 spin_lock_irqsave(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Or Gerlitzb3e27492008-03-11 16:10:02 +0200682 if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags) ||
Roland Dreier20b83382006-02-11 12:22:12 -0800683 !priv->broadcast ||
684 !test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
Roland Dreierde903512007-09-28 15:33:51 -0700685 ++dev->stats.tx_dropped;
Michael S. Tsirkin479a0792006-02-07 16:37:08 -0800686 dev_kfree_skb_any(skb);
687 goto unlock;
688 }
689
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 mcast = __ipoib_mcast_find(dev, mgid);
Doug Ledfordd2fe9372015-02-21 19:27:06 -0500691 if (!mcast || !mcast->ah) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 if (!mcast) {
Doug Ledfordd2fe9372015-02-21 19:27:06 -0500693 /* Let's create a new send only group now */
694 ipoib_dbg_mcast(priv, "setting up send only multicast group for %pI6\n",
695 mgid);
696
697 mcast = ipoib_mcast_alloc(dev, 0);
698 if (!mcast) {
699 ipoib_warn(priv, "unable to allocate memory "
700 "for multicast structure\n");
701 ++dev->stats.tx_dropped;
702 dev_kfree_skb_any(skb);
703 goto unlock;
704 }
705
706 set_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags);
707 memcpy(mcast->mcmember.mgid.raw, mgid,
708 sizeof (union ib_gid));
709 __ipoib_mcast_add(dev, mcast);
710 list_add_tail(&mcast->list, &priv->multicast_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 if (skb_queue_len(&mcast->pkt_queue) < IPOIB_MAX_MCAST_QUEUE)
713 skb_queue_tail(&mcast->pkt_queue, skb);
Michael S. Tsirkinb36f170b62006-01-17 12:19:40 -0800714 else {
Roland Dreierde903512007-09-28 15:33:51 -0700715 ++dev->stats.tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 dev_kfree_skb_any(skb);
Michael S. Tsirkinb36f170b62006-01-17 12:19:40 -0800717 }
Doug Ledfordd2fe9372015-02-21 19:27:06 -0500718 if (!test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags)) {
719 __ipoib_mcast_schedule_join_thread(priv, NULL, 0);
720 }
721 } else {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000722 struct ipoib_neigh *neigh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000724 spin_unlock_irqrestore(&priv->lock, flags);
725 neigh = ipoib_neigh_get(dev, daddr);
726 spin_lock_irqsave(&priv->lock, flags);
727 if (!neigh) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000728 neigh = ipoib_neigh_alloc(daddr, dev);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000729 if (neigh) {
730 kref_get(&mcast->ah->ref);
731 neigh->ah = mcast->ah;
732 list_add_tail(&neigh->list, &mcast->neigh_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 }
734 }
Roland Dreier721d67c2009-09-05 20:23:40 -0700735 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 ipoib_send(dev, skb, mcast->ah, IB_MULTICAST_QPN);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000737 if (neigh)
738 ipoib_neigh_put(neigh);
Roland Dreier721d67c2009-09-05 20:23:40 -0700739 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 }
741
Michael S. Tsirkin479a0792006-02-07 16:37:08 -0800742unlock:
Roland Dreier943c2462008-09-30 10:36:21 -0700743 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744}
745
746void ipoib_mcast_dev_flush(struct net_device *dev)
747{
748 struct ipoib_dev_priv *priv = netdev_priv(dev);
749 LIST_HEAD(remove_list);
Eli Cohen988bd502006-01-12 14:32:20 -0800750 struct ipoib_mcast *mcast, *tmcast;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 unsigned long flags;
752
753 ipoib_dbg_mcast(priv, "flushing multicast list\n");
754
755 spin_lock_irqsave(&priv->lock, flags);
Eli Cohen988bd502006-01-12 14:32:20 -0800756
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
Eli Cohen988bd502006-01-12 14:32:20 -0800758 list_del(&mcast->list);
759 rb_erase(&mcast->rb_node, &priv->multicast_tree);
760 list_add_tail(&mcast->list, &remove_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 }
762
763 if (priv->broadcast) {
Roland Dreier3cd96562006-09-22 15:22:46 -0700764 rb_erase(&priv->broadcast->rb_node, &priv->multicast_tree);
Eli Cohen988bd502006-01-12 14:32:20 -0800765 list_add_tail(&priv->broadcast->list, &remove_list);
766 priv->broadcast = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 }
768
769 spin_unlock_irqrestore(&priv->lock, flags);
770
Doug Ledford69911412015-02-21 19:27:05 -0500771 /*
772 * make sure the in-flight joins have finished before we attempt
773 * to leave
774 */
Erez Shitrita9c8ba52013-10-16 17:37:51 +0300775 list_for_each_entry_safe(mcast, tmcast, &remove_list, list)
Doug Ledford69911412015-02-21 19:27:05 -0500776 if (test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
Erez Shitrita9c8ba52013-10-16 17:37:51 +0300777 wait_for_completion(&mcast->done);
778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
780 ipoib_mcast_leave(dev, mcast);
781 ipoib_mcast_free(mcast);
782 }
783}
784
Jiri Pirko3e4aa12f82010-03-22 03:21:39 +0000785static int ipoib_mcast_addr_is_valid(const u8 *addr, const u8 *broadcast)
Jason Gunthorpe5e475962009-09-05 20:23:40 -0700786{
Jason Gunthorpe5e475962009-09-05 20:23:40 -0700787 /* reserved QPN, prefix, scope */
788 if (memcmp(addr, broadcast, 6))
789 return 0;
790 /* signature lower, pkey */
791 if (memcmp(addr + 7, broadcast + 7, 3))
792 return 0;
793 return 1;
794}
795
David Howellsc4028952006-11-22 14:57:56 +0000796void ipoib_mcast_restart_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797{
David Howellsc4028952006-11-22 14:57:56 +0000798 struct ipoib_dev_priv *priv =
799 container_of(work, struct ipoib_dev_priv, restart_task);
800 struct net_device *dev = priv->dev;
Jiri Pirko22bedad32010-04-01 21:22:57 +0000801 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 struct ipoib_mcast *mcast, *tmcast;
803 LIST_HEAD(remove_list);
804 unsigned long flags;
Or Gerlitz335a64a5a2007-10-08 10:13:00 +0200805 struct ib_sa_mcmember_rec rec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
Doug Ledford69911412015-02-21 19:27:05 -0500807 if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags))
808 /*
809 * shortcut...on shutdown flush is called next, just
810 * let it do all the work
811 */
812 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Doug Ledford69911412015-02-21 19:27:05 -0500814 ipoib_dbg_mcast(priv, "restarting multicast task\n");
Roland Dreier4e0ab202015-01-30 15:38:46 -0800815
Herbert Xu932ff272006-06-09 12:20:56 -0700816 local_irq_save(flags);
David S. Millere308a5d2008-07-15 00:13:44 -0700817 netif_addr_lock(dev);
Michael S. Tsirkin78bfe0b2006-01-11 11:47:34 -0800818 spin_lock(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
820 /*
821 * Unfortunately, the networking core only gives us a list of all of
822 * the multicast hardware addresses. We need to figure out which ones
823 * are new and which ones have been removed
824 */
825
826 /* Clear out the found flag */
827 list_for_each_entry(mcast, &priv->multicast_list, list)
828 clear_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
829
830 /* Mark all of the entries that are found or don't exist */
Jiri Pirko22bedad32010-04-01 21:22:57 +0000831 netdev_for_each_mc_addr(ha, dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 union ib_gid mgid;
833
Jiri Pirko22bedad32010-04-01 21:22:57 +0000834 if (!ipoib_mcast_addr_is_valid(ha->addr, dev->broadcast))
Jason Gunthorpe5e475962009-09-05 20:23:40 -0700835 continue;
836
Jiri Pirko22bedad32010-04-01 21:22:57 +0000837 memcpy(mgid.raw, ha->addr + 4, sizeof mgid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 mcast = __ipoib_mcast_find(dev, &mgid);
840 if (!mcast || test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
841 struct ipoib_mcast *nmcast;
842
Or Gerlitz335a64a5a2007-10-08 10:13:00 +0200843 /* ignore group which is directly joined by userspace */
844 if (test_bit(IPOIB_FLAG_UMCAST, &priv->flags) &&
845 !ib_sa_get_mcmember_rec(priv->ca, priv->port, &mgid, &rec)) {
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700846 ipoib_dbg_mcast(priv, "ignoring multicast entry for mgid %pI6\n",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700847 mgid.raw);
Or Gerlitz335a64a5a2007-10-08 10:13:00 +0200848 continue;
849 }
850
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 /* Not found or send-only group, let's add a new entry */
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700852 ipoib_dbg_mcast(priv, "adding multicast entry for mgid %pI6\n",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700853 mgid.raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
855 nmcast = ipoib_mcast_alloc(dev, 0);
856 if (!nmcast) {
857 ipoib_warn(priv, "unable to allocate memory for multicast structure\n");
858 continue;
859 }
860
861 set_bit(IPOIB_MCAST_FLAG_FOUND, &nmcast->flags);
862
863 nmcast->mcmember.mgid = mgid;
864
865 if (mcast) {
866 /* Destroy the send only entry */
Akinobu Mita179e0912006-06-26 00:24:41 -0700867 list_move_tail(&mcast->list, &remove_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
869 rb_replace_node(&mcast->rb_node,
870 &nmcast->rb_node,
871 &priv->multicast_tree);
872 } else
873 __ipoib_mcast_add(dev, nmcast);
874
875 list_add_tail(&nmcast->list, &priv->multicast_list);
876 }
877
878 if (mcast)
879 set_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
880 }
881
882 /* Remove all of the entries don't exist anymore */
883 list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
884 if (!test_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags) &&
885 !test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700886 ipoib_dbg_mcast(priv, "deleting multicast group %pI6\n",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700887 mcast->mcmember.mgid.raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
889 rb_erase(&mcast->rb_node, &priv->multicast_tree);
890
891 /* Move to the remove list */
Akinobu Mita179e0912006-06-26 00:24:41 -0700892 list_move_tail(&mcast->list, &remove_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 }
894 }
Michael S. Tsirkin78bfe0b2006-01-11 11:47:34 -0800895
896 spin_unlock(&priv->lock);
David S. Millere308a5d2008-07-15 00:13:44 -0700897 netif_addr_unlock(dev);
Herbert Xu932ff272006-06-09 12:20:56 -0700898 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Doug Ledford69911412015-02-21 19:27:05 -0500900 /*
901 * make sure the in-flight joins have finished before we attempt
902 * to leave
903 */
904 list_for_each_entry_safe(mcast, tmcast, &remove_list, list)
905 if (test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
906 wait_for_completion(&mcast->done);
907
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
909 ipoib_mcast_leave(mcast->dev, mcast);
910 ipoib_mcast_free(mcast);
911 }
Roland Dreier962121b2015-01-30 15:39:11 -0800912
Doug Ledford69911412015-02-21 19:27:05 -0500913 /*
914 * Double check that we are still up
915 */
916 if (test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
917 spin_lock_irqsave(&priv->lock, flags);
918 __ipoib_mcast_schedule_join_thread(priv, NULL, 0);
919 spin_unlock_irqrestore(&priv->lock, flags);
920 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921}
922
Roland Dreier8ae5a8a2005-11-02 20:51:01 -0800923#ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
924
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925struct ipoib_mcast_iter *ipoib_mcast_iter_init(struct net_device *dev)
926{
927 struct ipoib_mcast_iter *iter;
928
929 iter = kmalloc(sizeof *iter, GFP_KERNEL);
930 if (!iter)
931 return NULL;
932
933 iter->dev = dev;
Roland Dreier1732b0e2005-11-07 10:33:11 -0800934 memset(iter->mgid.raw, 0, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
936 if (ipoib_mcast_iter_next(iter)) {
Roland Dreier1732b0e2005-11-07 10:33:11 -0800937 kfree(iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 return NULL;
939 }
940
941 return iter;
942}
943
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944int ipoib_mcast_iter_next(struct ipoib_mcast_iter *iter)
945{
946 struct ipoib_dev_priv *priv = netdev_priv(iter->dev);
947 struct rb_node *n;
948 struct ipoib_mcast *mcast;
949 int ret = 1;
950
951 spin_lock_irq(&priv->lock);
952
953 n = rb_first(&priv->multicast_tree);
954
955 while (n) {
956 mcast = rb_entry(n, struct ipoib_mcast, rb_node);
957
958 if (memcmp(iter->mgid.raw, mcast->mcmember.mgid.raw,
959 sizeof (union ib_gid)) < 0) {
960 iter->mgid = mcast->mcmember.mgid;
961 iter->created = mcast->created;
962 iter->queuelen = skb_queue_len(&mcast->pkt_queue);
963 iter->complete = !!mcast->ah;
964 iter->send_only = !!(mcast->flags & (1 << IPOIB_MCAST_FLAG_SENDONLY));
965
966 ret = 0;
967
968 break;
969 }
970
971 n = rb_next(n);
972 }
973
974 spin_unlock_irq(&priv->lock);
975
976 return ret;
977}
978
979void ipoib_mcast_iter_read(struct ipoib_mcast_iter *iter,
980 union ib_gid *mgid,
981 unsigned long *created,
982 unsigned int *queuelen,
983 unsigned int *complete,
984 unsigned int *send_only)
985{
986 *mgid = iter->mgid;
987 *created = iter->created;
988 *queuelen = iter->queuelen;
989 *complete = iter->complete;
990 *send_only = iter->send_only;
991}
Roland Dreier8ae5a8a2005-11-02 20:51:01 -0800992
993#endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */