blob: d41ea27be5e196998d97ced405837433d46586cb [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>
37#include <linux/ip.h>
38#include <linux/in.h>
39#include <linux/igmp.h>
40#include <linux/inetdevice.h>
41#include <linux/delay.h>
42#include <linux/completion.h>
43
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020044#include <net/dst.h>
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include "ipoib.h"
47
48#ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
49static int mcast_debug_level;
50
51module_param(mcast_debug_level, int, 0644);
52MODULE_PARM_DESC(mcast_debug_level,
53 "Enable multicast debug tracing if > 0");
54#endif
55
Ingo Molnar95ed6442006-01-13 14:51:39 -080056static DEFINE_MUTEX(mcast_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Linus Torvalds1da177e2005-04-16 15:20:36 -070058struct ipoib_mcast_iter {
59 struct net_device *dev;
60 union ib_gid mgid;
61 unsigned long created;
62 unsigned int queuelen;
63 unsigned int complete;
64 unsigned int send_only;
65};
66
67static void ipoib_mcast_free(struct ipoib_mcast *mcast)
68{
69 struct net_device *dev = mcast->dev;
70 struct ipoib_dev_priv *priv = netdev_priv(dev);
71 struct ipoib_neigh *neigh, *tmp;
Michael S. Tsirkinb36f170b62006-01-17 12:19:40 -080072 int tx_dropped = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Harvey Harrison5b095d9892008-10-29 12:52:50 -070074 ipoib_dbg_mcast(netdev_priv(dev), "deleting multicast group %pI6\n",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -070075 mcast->mcmember.mgid.raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Roland Dreier943c2462008-09-30 10:36:21 -070077 spin_lock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79 list_for_each_entry_safe(neigh, tmp, &mcast->neigh_list, list) {
Eli Cohen97460df2006-01-10 07:43:02 -080080 /*
81 * It's safe to call ipoib_put_ah() inside priv->lock
82 * here, because we know that mcast->ah will always
83 * hold one more reference, so ipoib_put_ah() will
84 * never do more than decrement the ref count.
85 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 if (neigh->ah)
Eli Cohen97460df2006-01-10 07:43:02 -080087 ipoib_put_ah(neigh->ah);
Michael S. Tsirkin2745b5b2006-11-16 14:16:47 +020088 ipoib_neigh_free(dev, neigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 }
90
Roland Dreier943c2462008-09-30 10:36:21 -070091 spin_unlock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 if (mcast->ah)
94 ipoib_put_ah(mcast->ah);
95
Michael S. Tsirkinb36f170b62006-01-17 12:19:40 -080096 while (!skb_queue_empty(&mcast->pkt_queue)) {
97 ++tx_dropped;
Roland Dreier8c608a32005-11-07 10:49:38 -080098 dev_kfree_skb_any(skb_dequeue(&mcast->pkt_queue));
Michael S. Tsirkinb36f170b62006-01-17 12:19:40 -080099 }
100
Roland Dreier943c2462008-09-30 10:36:21 -0700101 netif_tx_lock_bh(dev);
Roland Dreierde903512007-09-28 15:33:51 -0700102 dev->stats.tx_dropped += tx_dropped;
Roland Dreier943c2462008-09-30 10:36:21 -0700103 netif_tx_unlock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105 kfree(mcast);
106}
107
108static struct ipoib_mcast *ipoib_mcast_alloc(struct net_device *dev,
109 int can_sleep)
110{
111 struct ipoib_mcast *mcast;
112
Roland Dreierde6eb662005-11-02 07:23:14 -0800113 mcast = kzalloc(sizeof *mcast, can_sleep ? GFP_KERNEL : GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 if (!mcast)
115 return NULL;
116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 mcast->dev = dev;
118 mcast->created = jiffies;
Hal Rosenstockce5b65c2005-09-18 13:47:53 -0700119 mcast->backoff = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121 INIT_LIST_HEAD(&mcast->list);
122 INIT_LIST_HEAD(&mcast->neigh_list);
123 skb_queue_head_init(&mcast->pkt_queue);
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 return mcast;
126}
127
Jack Morgenstein37c22a72006-05-29 19:14:05 +0300128static struct ipoib_mcast *__ipoib_mcast_find(struct net_device *dev, void *mgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
130 struct ipoib_dev_priv *priv = netdev_priv(dev);
131 struct rb_node *n = priv->multicast_tree.rb_node;
132
133 while (n) {
134 struct ipoib_mcast *mcast;
135 int ret;
136
137 mcast = rb_entry(n, struct ipoib_mcast, rb_node);
138
Jack Morgenstein37c22a72006-05-29 19:14:05 +0300139 ret = memcmp(mgid, mcast->mcmember.mgid.raw,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 sizeof (union ib_gid));
141 if (ret < 0)
142 n = n->rb_left;
143 else if (ret > 0)
144 n = n->rb_right;
145 else
146 return mcast;
147 }
148
149 return NULL;
150}
151
152static int __ipoib_mcast_add(struct net_device *dev, struct ipoib_mcast *mcast)
153{
154 struct ipoib_dev_priv *priv = netdev_priv(dev);
155 struct rb_node **n = &priv->multicast_tree.rb_node, *pn = NULL;
156
157 while (*n) {
158 struct ipoib_mcast *tmcast;
159 int ret;
160
161 pn = *n;
162 tmcast = rb_entry(pn, struct ipoib_mcast, rb_node);
163
164 ret = memcmp(mcast->mcmember.mgid.raw, tmcast->mcmember.mgid.raw,
165 sizeof (union ib_gid));
166 if (ret < 0)
167 n = &pn->rb_left;
168 else if (ret > 0)
169 n = &pn->rb_right;
170 else
171 return -EEXIST;
172 }
173
174 rb_link_node(&mcast->rb_node, pn, n);
175 rb_insert_color(&mcast->rb_node, &priv->multicast_tree);
176
177 return 0;
178}
179
180static int ipoib_mcast_join_finish(struct ipoib_mcast *mcast,
181 struct ib_sa_mcmember_rec *mcmember)
182{
183 struct net_device *dev = mcast->dev;
184 struct ipoib_dev_priv *priv = netdev_priv(dev);
Eli Cohen7343b232006-02-27 20:47:43 -0800185 struct ipoib_ah *ah;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 int ret;
Eli Cohend0de1362008-07-14 23:48:50 -0700187 int set_qkey = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189 mcast->mcmember = *mcmember;
190
191 /* Set the cached Q_Key before we attach if it's the broadcast group */
192 if (!memcmp(mcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
193 sizeof (union ib_gid))) {
Jack Morgensteine1d50dc2008-05-20 15:41:09 -0700194 spin_lock_irq(&priv->lock);
195 if (!priv->broadcast) {
196 spin_unlock_irq(&priv->lock);
197 return -EAGAIN;
198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 priv->qkey = be32_to_cpu(priv->broadcast->mcmember.qkey);
Jack Morgensteine1d50dc2008-05-20 15:41:09 -0700200 spin_unlock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 priv->tx_wr.wr.ud.remote_qkey = priv->qkey;
Eli Cohend0de1362008-07-14 23:48:50 -0700202 set_qkey = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 }
204
205 if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
206 if (test_and_set_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700207 ipoib_warn(priv, "multicast group %pI6 already attached\n",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700208 mcast->mcmember.mgid.raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210 return 0;
211 }
212
213 ret = ipoib_mcast_attach(dev, be16_to_cpu(mcast->mcmember.mlid),
Eli Cohend0de1362008-07-14 23:48:50 -0700214 &mcast->mcmember.mgid, set_qkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 if (ret < 0) {
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700216 ipoib_warn(priv, "couldn't attach QP to multicast group %pI6\n",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700217 mcast->mcmember.mgid.raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
219 clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags);
220 return ret;
221 }
222 }
223
224 {
225 struct ib_ah_attr av = {
226 .dlid = be16_to_cpu(mcast->mcmember.mlid),
227 .port_num = priv->port,
228 .sl = mcast->mcmember.sl,
229 .ah_flags = IB_AH_GRH,
Jack Morgensteinbf6a9e32006-04-10 09:43:47 -0700230 .static_rate = mcast->mcmember.rate,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 .grh = {
232 .flow_label = be32_to_cpu(mcast->mcmember.flow_label),
233 .hop_limit = mcast->mcmember.hop_limit,
234 .sgid_index = 0,
235 .traffic_class = mcast->mcmember.traffic_class
236 }
237 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 av.grh.dgid = mcast->mcmember.mgid;
239
Eli Cohen7343b232006-02-27 20:47:43 -0800240 ah = ipoib_create_ah(dev, priv->pd, &av);
241 if (!ah) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 ipoib_warn(priv, "ib_address_create failed\n");
243 } else {
Or Gerlitz624d01f2006-07-24 10:42:00 +0300244 spin_lock_irq(&priv->lock);
245 mcast->ah = ah;
246 spin_unlock_irq(&priv->lock);
247
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700248 ipoib_dbg_mcast(priv, "MGID %pI6 AV %p, LID 0x%04x, SL %d\n",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700249 mcast->mcmember.mgid.raw,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 mcast->ah->ah,
251 be16_to_cpu(mcast->mcmember.mlid),
252 mcast->mcmember.sl);
253 }
254 }
255
256 /* actually send any queued packets */
Roland Dreier943c2462008-09-30 10:36:21 -0700257 netif_tx_lock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 while (!skb_queue_empty(&mcast->pkt_queue)) {
259 struct sk_buff *skb = skb_dequeue(&mcast->pkt_queue);
Roland Dreier943c2462008-09-30 10:36:21 -0700260 netif_tx_unlock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262 skb->dev = dev;
263
Eric Dumazetadf30902009-06-02 05:19:30 +0000264 if (!skb_dst(skb) || !skb_dst(skb)->neighbour) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 /* put pseudoheader back on for next time */
266 skb_push(skb, sizeof (struct ipoib_pseudoheader));
267 }
268
269 if (dev_queue_xmit(skb))
270 ipoib_warn(priv, "dev_queue_xmit failed to requeue packet\n");
Roland Dreier943c2462008-09-30 10:36:21 -0700271 netif_tx_lock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 }
Roland Dreier943c2462008-09-30 10:36:21 -0700273 netif_tx_unlock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
275 return 0;
276}
277
Sean Heftyfaec2f72007-02-15 17:00:17 -0800278static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279ipoib_mcast_sendonly_join_complete(int status,
Sean Heftyfaec2f72007-02-15 17:00:17 -0800280 struct ib_sa_multicast *multicast)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
Sean Heftyfaec2f72007-02-15 17:00:17 -0800282 struct ipoib_mcast *mcast = multicast->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 struct net_device *dev = mcast->dev;
284
Sean Heftyfaec2f72007-02-15 17:00:17 -0800285 /* We trap for port events ourselves. */
286 if (status == -ENETRESET)
287 return 0;
288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 if (!status)
Sean Heftyfaec2f72007-02-15 17:00:17 -0800290 status = ipoib_mcast_join_finish(mcast, &multicast->rec);
291
292 if (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 if (mcast->logcount++ < 20)
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700294 ipoib_dbg_mcast(netdev_priv(dev), "multicast join failed for %pI6, status %d\n",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700295 mcast->mcmember.mgid.raw, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
297 /* Flush out any queued packets */
Roland Dreier943c2462008-09-30 10:36:21 -0700298 netif_tx_lock_bh(dev);
Michael S. Tsirkinb36f170b62006-01-17 12:19:40 -0800299 while (!skb_queue_empty(&mcast->pkt_queue)) {
Roland Dreierde903512007-09-28 15:33:51 -0700300 ++dev->stats.tx_dropped;
Roland Dreier8c608a32005-11-07 10:49:38 -0800301 dev_kfree_skb_any(skb_dequeue(&mcast->pkt_queue));
Michael S. Tsirkinb36f170b62006-01-17 12:19:40 -0800302 }
Roland Dreier943c2462008-09-30 10:36:21 -0700303 netif_tx_unlock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305 /* Clear the busy flag so we try again */
Sean Heftyfaec2f72007-02-15 17:00:17 -0800306 status = test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY,
307 &mcast->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 }
Sean Heftyfaec2f72007-02-15 17:00:17 -0800309 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310}
311
312static int ipoib_mcast_sendonly_join(struct ipoib_mcast *mcast)
313{
314 struct net_device *dev = mcast->dev;
315 struct ipoib_dev_priv *priv = netdev_priv(dev);
316 struct ib_sa_mcmember_rec rec = {
317#if 0 /* Some SMs don't support send-only yet */
318 .join_state = 4
319#else
320 .join_state = 1
321#endif
322 };
323 int ret = 0;
324
325 if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
326 ipoib_dbg_mcast(priv, "device shutting down, no multicast joins\n");
327 return -ENODEV;
328 }
329
330 if (test_and_set_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags)) {
331 ipoib_dbg_mcast(priv, "multicast entry busy, skipping\n");
332 return -EBUSY;
333 }
334
335 rec.mgid = mcast->mcmember.mgid;
336 rec.port_gid = priv->local_gid;
Sean Hefty97f52eb2005-08-13 21:05:57 -0700337 rec.pkey = cpu_to_be16(priv->pkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Sean Heftyfaec2f72007-02-15 17:00:17 -0800339 mcast->mc = ib_sa_join_multicast(&ipoib_sa_client, priv->ca,
340 priv->port, &rec,
341 IB_SA_MCMEMBER_REC_MGID |
342 IB_SA_MCMEMBER_REC_PORT_GID |
343 IB_SA_MCMEMBER_REC_PKEY |
344 IB_SA_MCMEMBER_REC_JOIN_STATE,
345 GFP_ATOMIC,
346 ipoib_mcast_sendonly_join_complete,
347 mcast);
348 if (IS_ERR(mcast->mc)) {
349 ret = PTR_ERR(mcast->mc);
350 clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
351 ipoib_warn(priv, "ib_sa_join_multicast failed (ret = %d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 ret);
353 } else {
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700354 ipoib_dbg_mcast(priv, "no multicast record for %pI6, starting join\n",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700355 mcast->mcmember.mgid.raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 }
357
358 return ret;
359}
360
Yossi Etigine8224e42008-09-16 11:57:45 -0700361void ipoib_mcast_carrier_on_task(struct work_struct *work)
362{
363 struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
364 carrier_on_task);
Moni Shoua5ee951202009-09-24 12:01:05 -0700365 struct ib_port_attr attr;
Yossi Etigine8224e42008-09-16 11:57:45 -0700366
367 /*
368 * Take rtnl_lock to avoid racing with ipoib_stop() and
369 * turning the carrier back on while a device is being
370 * removed.
371 */
Moni Shoua5ee951202009-09-24 12:01:05 -0700372 if (ib_query_port(priv->ca, priv->port, &attr) ||
373 attr.state != IB_PORT_ACTIVE) {
374 ipoib_dbg(priv, "Keeping carrier off until IB port is active\n");
375 return;
376 }
377
Yossi Etigine8224e42008-09-16 11:57:45 -0700378 rtnl_lock();
379 netif_carrier_on(priv->dev);
380 rtnl_unlock();
381}
382
Sean Heftyfaec2f72007-02-15 17:00:17 -0800383static int ipoib_mcast_join_complete(int status,
384 struct ib_sa_multicast *multicast)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
Sean Heftyfaec2f72007-02-15 17:00:17 -0800386 struct ipoib_mcast *mcast = multicast->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 struct net_device *dev = mcast->dev;
388 struct ipoib_dev_priv *priv = netdev_priv(dev);
389
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700390 ipoib_dbg_mcast(priv, "join completion for %pI6 (status %d)\n",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700391 mcast->mcmember.mgid.raw, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Sean Heftyfaec2f72007-02-15 17:00:17 -0800393 /* We trap for port events ourselves. */
394 if (status == -ENETRESET)
395 return 0;
396
397 if (!status)
398 status = ipoib_mcast_join_finish(mcast, &multicast->rec);
399
400 if (!status) {
Hal Rosenstockce5b65c2005-09-18 13:47:53 -0700401 mcast->backoff = 1;
Ingo Molnar95ed6442006-01-13 14:51:39 -0800402 mutex_lock(&mcast_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
David Howellsc4028952006-11-22 14:57:56 +0000404 queue_delayed_work(ipoib_workqueue,
405 &priv->mcast_task, 0);
Ingo Molnar95ed6442006-01-13 14:51:39 -0800406 mutex_unlock(&mcast_mutex);
Shirley Ma55c9add2007-03-08 14:59:30 -0800407
Yossi Etigine8224e42008-09-16 11:57:45 -0700408 /*
409 * Defer carrier on work to ipoib_workqueue to avoid a
410 * deadlock on rtnl_lock here.
411 */
412 if (mcast == priv->broadcast)
413 queue_work(ipoib_workqueue, &priv->carrier_on_task);
Shirley Ma55c9add2007-03-08 14:59:30 -0800414
Sean Heftyfaec2f72007-02-15 17:00:17 -0800415 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 }
417
Sean Heftyfaec2f72007-02-15 17:00:17 -0800418 if (mcast->logcount++ < 20) {
Yossi Etigin3c209622009-01-16 13:42:59 -0800419 if (status == -ETIMEDOUT || status == -EAGAIN) {
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700420 ipoib_dbg_mcast(priv, "multicast join failed for %pI6, status %d\n",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700421 mcast->mcmember.mgid.raw, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 } else {
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700423 ipoib_warn(priv, "multicast join failed for %pI6, status %d\n",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700424 mcast->mcmember.mgid.raw, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 }
426 }
427
428 mcast->backoff *= 2;
429 if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
430 mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
431
Sean Heftyfaec2f72007-02-15 17:00:17 -0800432 /* Clear the busy flag so we try again */
433 status = test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
434
Michael S. Tsirkin9acf6a82006-03-02 11:07:47 -0800435 mutex_lock(&mcast_mutex);
Michael S. Tsirkin9acf6a82006-03-02 11:07:47 -0800436 spin_lock_irq(&priv->lock);
Sean Heftyfaec2f72007-02-15 17:00:17 -0800437 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
438 queue_delayed_work(ipoib_workqueue, &priv->mcast_task,
439 mcast->backoff * HZ);
Michael S. Tsirkin9acf6a82006-03-02 11:07:47 -0800440 spin_unlock_irq(&priv->lock);
Ingo Molnar95ed6442006-01-13 14:51:39 -0800441 mutex_unlock(&mcast_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Sean Heftyfaec2f72007-02-15 17:00:17 -0800443 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444}
445
446static void ipoib_mcast_join(struct net_device *dev, struct ipoib_mcast *mcast,
447 int create)
448{
449 struct ipoib_dev_priv *priv = netdev_priv(dev);
450 struct ib_sa_mcmember_rec rec = {
451 .join_state = 1
452 };
453 ib_sa_comp_mask comp_mask;
454 int ret = 0;
455
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700456 ipoib_dbg_mcast(priv, "joining MGID %pI6\n", mcast->mcmember.mgid.raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458 rec.mgid = mcast->mcmember.mgid;
459 rec.port_gid = priv->local_gid;
Sean Hefty97f52eb2005-08-13 21:05:57 -0700460 rec.pkey = cpu_to_be16(priv->pkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462 comp_mask =
463 IB_SA_MCMEMBER_REC_MGID |
464 IB_SA_MCMEMBER_REC_PORT_GID |
465 IB_SA_MCMEMBER_REC_PKEY |
466 IB_SA_MCMEMBER_REC_JOIN_STATE;
467
468 if (create) {
469 comp_mask |=
Roland Dreierd0df6d62006-09-22 15:22:56 -0700470 IB_SA_MCMEMBER_REC_QKEY |
471 IB_SA_MCMEMBER_REC_MTU_SELECTOR |
472 IB_SA_MCMEMBER_REC_MTU |
473 IB_SA_MCMEMBER_REC_TRAFFIC_CLASS |
474 IB_SA_MCMEMBER_REC_RATE_SELECTOR |
475 IB_SA_MCMEMBER_REC_RATE |
476 IB_SA_MCMEMBER_REC_SL |
477 IB_SA_MCMEMBER_REC_FLOW_LABEL |
478 IB_SA_MCMEMBER_REC_HOP_LIMIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
480 rec.qkey = priv->broadcast->mcmember.qkey;
Roland Dreierd0df6d62006-09-22 15:22:56 -0700481 rec.mtu_selector = IB_SA_EQ;
482 rec.mtu = priv->broadcast->mcmember.mtu;
483 rec.traffic_class = priv->broadcast->mcmember.traffic_class;
484 rec.rate_selector = IB_SA_EQ;
485 rec.rate = priv->broadcast->mcmember.rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 rec.sl = priv->broadcast->mcmember.sl;
487 rec.flow_label = priv->broadcast->mcmember.flow_label;
Roland Dreierd0df6d62006-09-22 15:22:56 -0700488 rec.hop_limit = priv->broadcast->mcmember.hop_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 }
490
Sean Heftyfaec2f72007-02-15 17:00:17 -0800491 set_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
492 mcast->mc = ib_sa_join_multicast(&ipoib_sa_client, priv->ca, priv->port,
493 &rec, comp_mask, GFP_KERNEL,
494 ipoib_mcast_join_complete, mcast);
495 if (IS_ERR(mcast->mc)) {
496 clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
497 ret = PTR_ERR(mcast->mc);
498 ipoib_warn(priv, "ib_sa_join_multicast failed, status %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
500 mcast->backoff *= 2;
501 if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
502 mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
503
Ingo Molnar95ed6442006-01-13 14:51:39 -0800504 mutex_lock(&mcast_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
506 queue_delayed_work(ipoib_workqueue,
507 &priv->mcast_task,
Hal Rosenstockce5b65c2005-09-18 13:47:53 -0700508 mcast->backoff * HZ);
Ingo Molnar95ed6442006-01-13 14:51:39 -0800509 mutex_unlock(&mcast_mutex);
Sean Heftyfaec2f72007-02-15 17:00:17 -0800510 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511}
512
David Howellsc4028952006-11-22 14:57:56 +0000513void ipoib_mcast_join_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
David Howellsc4028952006-11-22 14:57:56 +0000515 struct ipoib_dev_priv *priv =
516 container_of(work, struct ipoib_dev_priv, mcast_task.work);
517 struct net_device *dev = priv->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
519 if (!test_bit(IPOIB_MCAST_RUN, &priv->flags))
520 return;
521
522 if (ib_query_gid(priv->ca, priv->port, 0, &priv->local_gid))
Michael S. Tsirkin24bd1e42007-05-18 16:12:54 +0300523 ipoib_warn(priv, "ib_query_gid() failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 else
525 memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));
526
527 {
528 struct ib_port_attr attr;
529
Roland Dreier658bcef2007-02-21 20:28:05 -0800530 if (!ib_query_port(priv->ca, priv->port, &attr))
531 priv->local_lid = attr.lid;
532 else
Sean Heftyfaec2f72007-02-15 17:00:17 -0800533 ipoib_warn(priv, "ib_query_port failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 }
535
536 if (!priv->broadcast) {
Roland Dreier20b83382006-02-11 12:22:12 -0800537 struct ipoib_mcast *broadcast;
538
Yossi Etigin50df48f2009-01-12 19:28:42 -0800539 if (!test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
540 return;
541
Roland Dreier20b83382006-02-11 12:22:12 -0800542 broadcast = ipoib_mcast_alloc(dev, 1);
543 if (!broadcast) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 ipoib_warn(priv, "failed to allocate broadcast group\n");
Ingo Molnar95ed6442006-01-13 14:51:39 -0800545 mutex_lock(&mcast_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
547 queue_delayed_work(ipoib_workqueue,
548 &priv->mcast_task, HZ);
Ingo Molnar95ed6442006-01-13 14:51:39 -0800549 mutex_unlock(&mcast_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 return;
551 }
552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 spin_lock_irq(&priv->lock);
Roland Dreier20b83382006-02-11 12:22:12 -0800554 memcpy(broadcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
555 sizeof (union ib_gid));
556 priv->broadcast = broadcast;
557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 __ipoib_mcast_add(dev, priv->broadcast);
559 spin_unlock_irq(&priv->lock);
560 }
561
562 if (!test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
Sean Heftyfaec2f72007-02-15 17:00:17 -0800563 if (!test_bit(IPOIB_MCAST_FLAG_BUSY, &priv->broadcast->flags))
564 ipoib_mcast_join(dev, priv->broadcast, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 return;
566 }
567
568 while (1) {
569 struct ipoib_mcast *mcast = NULL;
570
571 spin_lock_irq(&priv->lock);
572 list_for_each_entry(mcast, &priv->multicast_list, list) {
573 if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)
574 && !test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags)
575 && !test_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
576 /* Found the next unjoined group */
577 break;
578 }
579 }
580 spin_unlock_irq(&priv->lock);
581
582 if (&mcast->list == &priv->multicast_list) {
583 /* All done */
584 break;
585 }
586
587 ipoib_mcast_join(dev, mcast, 1);
588 return;
589 }
590
Shirley Mabc7b3a32008-04-23 11:55:45 -0700591 priv->mcast_mtu = IPOIB_UD_MTU(ib_mtu_enum_to_int(priv->broadcast->mcmember.mtu));
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200592
Eli Cohenc8c2afe2008-07-14 23:48:51 -0700593 if (!ipoib_cm_admin_enabled(dev)) {
594 rtnl_lock();
Eli Cohenbd360672008-07-14 23:48:51 -0700595 dev_set_mtu(dev, min(priv->mcast_mtu, priv->admin_mtu));
Eli Cohenc8c2afe2008-07-14 23:48:51 -0700596 rtnl_unlock();
597 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
599 ipoib_dbg_mcast(priv, "successfully joined all multicast groups\n");
600
601 clear_bit(IPOIB_MCAST_RUN, &priv->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602}
603
604int ipoib_mcast_start_thread(struct net_device *dev)
605{
606 struct ipoib_dev_priv *priv = netdev_priv(dev);
607
608 ipoib_dbg_mcast(priv, "starting multicast thread\n");
609
Ingo Molnar95ed6442006-01-13 14:51:39 -0800610 mutex_lock(&mcast_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 if (!test_and_set_bit(IPOIB_MCAST_RUN, &priv->flags))
David Howellsc4028952006-11-22 14:57:56 +0000612 queue_delayed_work(ipoib_workqueue, &priv->mcast_task, 0);
Ingo Molnar95ed6442006-01-13 14:51:39 -0800613 mutex_unlock(&mcast_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
615 return 0;
616}
617
Roland Dreier8d2cae02005-09-20 10:52:04 -0700618int ipoib_mcast_stop_thread(struct net_device *dev, int flush)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619{
620 struct ipoib_dev_priv *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622 ipoib_dbg_mcast(priv, "stopping multicast thread\n");
623
Ingo Molnar95ed6442006-01-13 14:51:39 -0800624 mutex_lock(&mcast_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 clear_bit(IPOIB_MCAST_RUN, &priv->flags);
626 cancel_delayed_work(&priv->mcast_task);
Ingo Molnar95ed6442006-01-13 14:51:39 -0800627 mutex_unlock(&mcast_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Roland Dreier8d2cae02005-09-20 10:52:04 -0700629 if (flush)
630 flush_workqueue(ipoib_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 return 0;
633}
634
635static int ipoib_mcast_leave(struct net_device *dev, struct ipoib_mcast *mcast)
636{
637 struct ipoib_dev_priv *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 int ret = 0;
639
Sean Heftye07832b2007-03-19 14:31:36 -0800640 if (test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
641 ib_sa_free_multicast(mcast->mc);
642
Sean Heftyfaec2f72007-02-15 17:00:17 -0800643 if (test_and_clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700644 ipoib_dbg_mcast(priv, "leaving MGID %pI6\n",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700645 mcast->mcmember.mgid.raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Sean Heftyfaec2f72007-02-15 17:00:17 -0800647 /* Remove ourselves from the multicast group */
Roland Dreier9eae5542008-07-14 23:48:50 -0700648 ret = ib_detach_mcast(priv->qp, &mcast->mcmember.mgid,
649 be16_to_cpu(mcast->mcmember.mlid));
Sean Heftyfaec2f72007-02-15 17:00:17 -0800650 if (ret)
Roland Dreier9eae5542008-07-14 23:48:50 -0700651 ipoib_warn(priv, "ib_detach_mcast failed (result = %d)\n", ret);
Sean Heftyfaec2f72007-02-15 17:00:17 -0800652 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 return 0;
655}
656
Jack Morgenstein37c22a72006-05-29 19:14:05 +0300657void ipoib_mcast_send(struct net_device *dev, void *mgid, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
659 struct ipoib_dev_priv *priv = netdev_priv(dev);
660 struct ipoib_mcast *mcast;
Roland Dreier943c2462008-09-30 10:36:21 -0700661 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Roland Dreier943c2462008-09-30 10:36:21 -0700663 spin_lock_irqsave(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Or Gerlitzb3e27492008-03-11 16:10:02 +0200665 if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags) ||
Roland Dreier20b83382006-02-11 12:22:12 -0800666 !priv->broadcast ||
667 !test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
Roland Dreierde903512007-09-28 15:33:51 -0700668 ++dev->stats.tx_dropped;
Michael S. Tsirkin479a0792006-02-07 16:37:08 -0800669 dev_kfree_skb_any(skb);
670 goto unlock;
671 }
672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 mcast = __ipoib_mcast_find(dev, mgid);
674 if (!mcast) {
675 /* Let's create a new send only group now */
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700676 ipoib_dbg_mcast(priv, "setting up send only multicast group for %pI6\n",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700677 mgid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
679 mcast = ipoib_mcast_alloc(dev, 0);
680 if (!mcast) {
681 ipoib_warn(priv, "unable to allocate memory for "
682 "multicast structure\n");
Roland Dreierde903512007-09-28 15:33:51 -0700683 ++dev->stats.tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 dev_kfree_skb_any(skb);
685 goto out;
686 }
687
688 set_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags);
Jack Morgenstein37c22a72006-05-29 19:14:05 +0300689 memcpy(mcast->mcmember.mgid.raw, mgid, sizeof (union ib_gid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 __ipoib_mcast_add(dev, mcast);
691 list_add_tail(&mcast->list, &priv->multicast_list);
692 }
693
694 if (!mcast->ah) {
695 if (skb_queue_len(&mcast->pkt_queue) < IPOIB_MAX_MCAST_QUEUE)
696 skb_queue_tail(&mcast->pkt_queue, skb);
Michael S. Tsirkinb36f170b62006-01-17 12:19:40 -0800697 else {
Roland Dreierde903512007-09-28 15:33:51 -0700698 ++dev->stats.tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 dev_kfree_skb_any(skb);
Michael S. Tsirkinb36f170b62006-01-17 12:19:40 -0800700 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Sean Heftyfaec2f72007-02-15 17:00:17 -0800702 if (test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 ipoib_dbg_mcast(priv, "no address vector, "
704 "but multicast join already started\n");
705 else if (test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags))
706 ipoib_mcast_sendonly_join(mcast);
707
708 /*
709 * If lookup completes between here and out:, don't
710 * want to send packet twice.
711 */
712 mcast = NULL;
713 }
714
715out:
716 if (mcast && mcast->ah) {
Eric Dumazetadf30902009-06-02 05:19:30 +0000717 if (skb_dst(skb) &&
718 skb_dst(skb)->neighbour &&
719 !*to_ipoib_neigh(skb_dst(skb)->neighbour)) {
720 struct ipoib_neigh *neigh = ipoib_neigh_alloc(skb_dst(skb)->neighbour,
Moni Shoua732a2172007-10-09 19:43:36 -0700721 skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
723 if (neigh) {
724 kref_get(&mcast->ah->ref);
Roland Dreier2337f802007-10-23 19:57:54 -0700725 neigh->ah = mcast->ah;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 list_add_tail(&neigh->list, &mcast->neigh_list);
727 }
728 }
729
Roland Dreier721d67c2009-09-05 20:23:40 -0700730 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 ipoib_send(dev, skb, mcast->ah, IB_MULTICAST_QPN);
Roland Dreier721d67c2009-09-05 20:23:40 -0700732 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 }
734
Michael S. Tsirkin479a0792006-02-07 16:37:08 -0800735unlock:
Roland Dreier943c2462008-09-30 10:36:21 -0700736 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737}
738
739void ipoib_mcast_dev_flush(struct net_device *dev)
740{
741 struct ipoib_dev_priv *priv = netdev_priv(dev);
742 LIST_HEAD(remove_list);
Eli Cohen988bd502006-01-12 14:32:20 -0800743 struct ipoib_mcast *mcast, *tmcast;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 unsigned long flags;
745
746 ipoib_dbg_mcast(priv, "flushing multicast list\n");
747
748 spin_lock_irqsave(&priv->lock, flags);
Eli Cohen988bd502006-01-12 14:32:20 -0800749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
Eli Cohen988bd502006-01-12 14:32:20 -0800751 list_del(&mcast->list);
752 rb_erase(&mcast->rb_node, &priv->multicast_tree);
753 list_add_tail(&mcast->list, &remove_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 }
755
756 if (priv->broadcast) {
Roland Dreier3cd96562006-09-22 15:22:46 -0700757 rb_erase(&priv->broadcast->rb_node, &priv->multicast_tree);
Eli Cohen988bd502006-01-12 14:32:20 -0800758 list_add_tail(&priv->broadcast->list, &remove_list);
759 priv->broadcast = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 }
761
762 spin_unlock_irqrestore(&priv->lock, flags);
763
764 list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
765 ipoib_mcast_leave(dev, mcast);
766 ipoib_mcast_free(mcast);
767 }
768}
769
Jiri Pirko6c746512010-02-27 12:07:33 +0000770static int ipoib_mcast_addr_is_valid(const u8 *addr, unsigned int addrlen,
771 const u8 *broadcast)
Jason Gunthorpe5e475962009-09-05 20:23:40 -0700772{
Jiri Pirko6c746512010-02-27 12:07:33 +0000773 if (addrlen != INFINIBAND_ALEN)
774 return 0;
Jason Gunthorpe5e475962009-09-05 20:23:40 -0700775 /* reserved QPN, prefix, scope */
776 if (memcmp(addr, broadcast, 6))
777 return 0;
778 /* signature lower, pkey */
779 if (memcmp(addr + 7, broadcast + 7, 3))
780 return 0;
781 return 1;
782}
783
David Howellsc4028952006-11-22 14:57:56 +0000784void ipoib_mcast_restart_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785{
David Howellsc4028952006-11-22 14:57:56 +0000786 struct ipoib_dev_priv *priv =
787 container_of(work, struct ipoib_dev_priv, restart_task);
788 struct net_device *dev = priv->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 struct dev_mc_list *mclist;
790 struct ipoib_mcast *mcast, *tmcast;
791 LIST_HEAD(remove_list);
792 unsigned long flags;
Or Gerlitz335a64a5a2007-10-08 10:13:00 +0200793 struct ib_sa_mcmember_rec rec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
795 ipoib_dbg_mcast(priv, "restarting multicast task\n");
796
Roland Dreier8d2cae02005-09-20 10:52:04 -0700797 ipoib_mcast_stop_thread(dev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
Herbert Xu932ff272006-06-09 12:20:56 -0700799 local_irq_save(flags);
David S. Millere308a5d2008-07-15 00:13:44 -0700800 netif_addr_lock(dev);
Michael S. Tsirkin78bfe0b2006-01-11 11:47:34 -0800801 spin_lock(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
803 /*
804 * Unfortunately, the networking core only gives us a list of all of
805 * the multicast hardware addresses. We need to figure out which ones
806 * are new and which ones have been removed
807 */
808
809 /* Clear out the found flag */
810 list_for_each_entry(mcast, &priv->multicast_list, list)
811 clear_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
812
813 /* Mark all of the entries that are found or don't exist */
Jiri Pirkofbf219f2010-02-24 05:11:08 +0000814 netdev_for_each_mc_addr(mclist, dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 union ib_gid mgid;
816
Jason Gunthorpe5e475962009-09-05 20:23:40 -0700817 if (!ipoib_mcast_addr_is_valid(mclist->dmi_addr,
Jiri Pirko6c746512010-02-27 12:07:33 +0000818 mclist->dmi_addrlen,
Jason Gunthorpe5e475962009-09-05 20:23:40 -0700819 dev->broadcast))
820 continue;
821
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 memcpy(mgid.raw, mclist->dmi_addr + 4, sizeof mgid);
823
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 mcast = __ipoib_mcast_find(dev, &mgid);
825 if (!mcast || test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
826 struct ipoib_mcast *nmcast;
827
Or Gerlitz335a64a5a2007-10-08 10:13:00 +0200828 /* ignore group which is directly joined by userspace */
829 if (test_bit(IPOIB_FLAG_UMCAST, &priv->flags) &&
830 !ib_sa_get_mcmember_rec(priv->ca, priv->port, &mgid, &rec)) {
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700831 ipoib_dbg_mcast(priv, "ignoring multicast entry for mgid %pI6\n",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700832 mgid.raw);
Or Gerlitz335a64a5a2007-10-08 10:13:00 +0200833 continue;
834 }
835
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 /* Not found or send-only group, let's add a new entry */
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700837 ipoib_dbg_mcast(priv, "adding multicast entry for mgid %pI6\n",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700838 mgid.raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
840 nmcast = ipoib_mcast_alloc(dev, 0);
841 if (!nmcast) {
842 ipoib_warn(priv, "unable to allocate memory for multicast structure\n");
843 continue;
844 }
845
846 set_bit(IPOIB_MCAST_FLAG_FOUND, &nmcast->flags);
847
848 nmcast->mcmember.mgid = mgid;
849
850 if (mcast) {
851 /* Destroy the send only entry */
Akinobu Mita179e0912006-06-26 00:24:41 -0700852 list_move_tail(&mcast->list, &remove_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
854 rb_replace_node(&mcast->rb_node,
855 &nmcast->rb_node,
856 &priv->multicast_tree);
857 } else
858 __ipoib_mcast_add(dev, nmcast);
859
860 list_add_tail(&nmcast->list, &priv->multicast_list);
861 }
862
863 if (mcast)
864 set_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
865 }
866
867 /* Remove all of the entries don't exist anymore */
868 list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
869 if (!test_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags) &&
870 !test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700871 ipoib_dbg_mcast(priv, "deleting multicast group %pI6\n",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700872 mcast->mcmember.mgid.raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
874 rb_erase(&mcast->rb_node, &priv->multicast_tree);
875
876 /* Move to the remove list */
Akinobu Mita179e0912006-06-26 00:24:41 -0700877 list_move_tail(&mcast->list, &remove_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 }
879 }
Michael S. Tsirkin78bfe0b2006-01-11 11:47:34 -0800880
881 spin_unlock(&priv->lock);
David S. Millere308a5d2008-07-15 00:13:44 -0700882 netif_addr_unlock(dev);
Herbert Xu932ff272006-06-09 12:20:56 -0700883 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
885 /* We have to cancel outside of the spinlock */
886 list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
887 ipoib_mcast_leave(mcast->dev, mcast);
888 ipoib_mcast_free(mcast);
889 }
890
891 if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
892 ipoib_mcast_start_thread(dev);
893}
894
Roland Dreier8ae5a8a2005-11-02 20:51:01 -0800895#ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897struct ipoib_mcast_iter *ipoib_mcast_iter_init(struct net_device *dev)
898{
899 struct ipoib_mcast_iter *iter;
900
901 iter = kmalloc(sizeof *iter, GFP_KERNEL);
902 if (!iter)
903 return NULL;
904
905 iter->dev = dev;
Roland Dreier1732b0e2005-11-07 10:33:11 -0800906 memset(iter->mgid.raw, 0, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
908 if (ipoib_mcast_iter_next(iter)) {
Roland Dreier1732b0e2005-11-07 10:33:11 -0800909 kfree(iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 return NULL;
911 }
912
913 return iter;
914}
915
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916int ipoib_mcast_iter_next(struct ipoib_mcast_iter *iter)
917{
918 struct ipoib_dev_priv *priv = netdev_priv(iter->dev);
919 struct rb_node *n;
920 struct ipoib_mcast *mcast;
921 int ret = 1;
922
923 spin_lock_irq(&priv->lock);
924
925 n = rb_first(&priv->multicast_tree);
926
927 while (n) {
928 mcast = rb_entry(n, struct ipoib_mcast, rb_node);
929
930 if (memcmp(iter->mgid.raw, mcast->mcmember.mgid.raw,
931 sizeof (union ib_gid)) < 0) {
932 iter->mgid = mcast->mcmember.mgid;
933 iter->created = mcast->created;
934 iter->queuelen = skb_queue_len(&mcast->pkt_queue);
935 iter->complete = !!mcast->ah;
936 iter->send_only = !!(mcast->flags & (1 << IPOIB_MCAST_FLAG_SENDONLY));
937
938 ret = 0;
939
940 break;
941 }
942
943 n = rb_next(n);
944 }
945
946 spin_unlock_irq(&priv->lock);
947
948 return ret;
949}
950
951void ipoib_mcast_iter_read(struct ipoib_mcast_iter *iter,
952 union ib_gid *mgid,
953 unsigned long *created,
954 unsigned int *queuelen,
955 unsigned int *complete,
956 unsigned int *send_only)
957{
958 *mgid = iter->mgid;
959 *created = iter->created;
960 *queuelen = iter->queuelen;
961 *complete = iter->complete;
962 *send_only = iter->send_only;
963}
Roland Dreier8ae5a8a2005-11-02 20:51:01 -0800964
965#endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */