blob: 71add7a8d53cecef8f6512fdd27942f858a44cb9 [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;
72 unsigned long flags;
Michael S. Tsirkinb36f1702006-01-17 12:19:40 -080073 int tx_dropped = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75 ipoib_dbg_mcast(netdev_priv(dev),
76 "deleting multicast group " IPOIB_GID_FMT "\n",
77 IPOIB_GID_ARG(mcast->mcmember.mgid));
78
79 spin_lock_irqsave(&priv->lock, flags);
80
81 list_for_each_entry_safe(neigh, tmp, &mcast->neigh_list, list) {
Eli Cohen97460df2006-01-10 07:43:02 -080082 /*
83 * It's safe to call ipoib_put_ah() inside priv->lock
84 * here, because we know that mcast->ah will always
85 * hold one more reference, so ipoib_put_ah() will
86 * never do more than decrement the ref count.
87 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 if (neigh->ah)
Eli Cohen97460df2006-01-10 07:43:02 -080089 ipoib_put_ah(neigh->ah);
Michael S. Tsirkin2745b5b2006-11-16 14:16:47 +020090 ipoib_neigh_free(dev, neigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 }
92
93 spin_unlock_irqrestore(&priv->lock, flags);
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 if (mcast->ah)
96 ipoib_put_ah(mcast->ah);
97
Michael S. Tsirkinb36f1702006-01-17 12:19:40 -080098 while (!skb_queue_empty(&mcast->pkt_queue)) {
99 ++tx_dropped;
Roland Dreier8c608a32005-11-07 10:49:38 -0800100 dev_kfree_skb_any(skb_dequeue(&mcast->pkt_queue));
Michael S. Tsirkinb36f1702006-01-17 12:19:40 -0800101 }
102
103 spin_lock_irqsave(&priv->tx_lock, flags);
Roland Dreierde903512007-09-28 15:33:51 -0700104 dev->stats.tx_dropped += tx_dropped;
Michael S. Tsirkinb36f1702006-01-17 12:19:40 -0800105 spin_unlock_irqrestore(&priv->tx_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107 kfree(mcast);
108}
109
110static struct ipoib_mcast *ipoib_mcast_alloc(struct net_device *dev,
111 int can_sleep)
112{
113 struct ipoib_mcast *mcast;
114
Roland Dreierde6eb662005-11-02 07:23:14 -0800115 mcast = kzalloc(sizeof *mcast, can_sleep ? GFP_KERNEL : GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 if (!mcast)
117 return NULL;
118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 mcast->dev = dev;
120 mcast->created = jiffies;
Hal Rosenstockce5b65c2005-09-18 13:47:53 -0700121 mcast->backoff = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123 INIT_LIST_HEAD(&mcast->list);
124 INIT_LIST_HEAD(&mcast->neigh_list);
125 skb_queue_head_init(&mcast->pkt_queue);
126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 return mcast;
128}
129
Jack Morgenstein37c22a72006-05-29 19:14:05 +0300130static struct ipoib_mcast *__ipoib_mcast_find(struct net_device *dev, void *mgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
132 struct ipoib_dev_priv *priv = netdev_priv(dev);
133 struct rb_node *n = priv->multicast_tree.rb_node;
134
135 while (n) {
136 struct ipoib_mcast *mcast;
137 int ret;
138
139 mcast = rb_entry(n, struct ipoib_mcast, rb_node);
140
Jack Morgenstein37c22a72006-05-29 19:14:05 +0300141 ret = memcmp(mgid, mcast->mcmember.mgid.raw,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 sizeof (union ib_gid));
143 if (ret < 0)
144 n = n->rb_left;
145 else if (ret > 0)
146 n = n->rb_right;
147 else
148 return mcast;
149 }
150
151 return NULL;
152}
153
154static int __ipoib_mcast_add(struct net_device *dev, struct ipoib_mcast *mcast)
155{
156 struct ipoib_dev_priv *priv = netdev_priv(dev);
157 struct rb_node **n = &priv->multicast_tree.rb_node, *pn = NULL;
158
159 while (*n) {
160 struct ipoib_mcast *tmcast;
161 int ret;
162
163 pn = *n;
164 tmcast = rb_entry(pn, struct ipoib_mcast, rb_node);
165
166 ret = memcmp(mcast->mcmember.mgid.raw, tmcast->mcmember.mgid.raw,
167 sizeof (union ib_gid));
168 if (ret < 0)
169 n = &pn->rb_left;
170 else if (ret > 0)
171 n = &pn->rb_right;
172 else
173 return -EEXIST;
174 }
175
176 rb_link_node(&mcast->rb_node, pn, n);
177 rb_insert_color(&mcast->rb_node, &priv->multicast_tree);
178
179 return 0;
180}
181
182static int ipoib_mcast_join_finish(struct ipoib_mcast *mcast,
183 struct ib_sa_mcmember_rec *mcmember)
184{
185 struct net_device *dev = mcast->dev;
186 struct ipoib_dev_priv *priv = netdev_priv(dev);
Eli Cohen7343b232006-02-27 20:47:43 -0800187 struct ipoib_ah *ah;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 int ret;
Eli Cohend0de1362008-07-14 23:48:50 -0700189 int set_qkey = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191 mcast->mcmember = *mcmember;
192
193 /* Set the cached Q_Key before we attach if it's the broadcast group */
194 if (!memcmp(mcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
195 sizeof (union ib_gid))) {
Jack Morgensteine1d50dc2008-05-20 15:41:09 -0700196 spin_lock_irq(&priv->lock);
197 if (!priv->broadcast) {
198 spin_unlock_irq(&priv->lock);
199 return -EAGAIN;
200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 priv->qkey = be32_to_cpu(priv->broadcast->mcmember.qkey);
Jack Morgensteine1d50dc2008-05-20 15:41:09 -0700202 spin_unlock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 priv->tx_wr.wr.ud.remote_qkey = priv->qkey;
Eli Cohend0de1362008-07-14 23:48:50 -0700204 set_qkey = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 }
206
207 if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
208 if (test_and_set_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
209 ipoib_warn(priv, "multicast group " IPOIB_GID_FMT
210 " already attached\n",
211 IPOIB_GID_ARG(mcast->mcmember.mgid));
212
213 return 0;
214 }
215
216 ret = ipoib_mcast_attach(dev, be16_to_cpu(mcast->mcmember.mlid),
Eli Cohend0de1362008-07-14 23:48:50 -0700217 &mcast->mcmember.mgid, set_qkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 if (ret < 0) {
219 ipoib_warn(priv, "couldn't attach QP to multicast group "
220 IPOIB_GID_FMT "\n",
221 IPOIB_GID_ARG(mcast->mcmember.mgid));
222
223 clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags);
224 return ret;
225 }
226 }
227
228 {
229 struct ib_ah_attr av = {
230 .dlid = be16_to_cpu(mcast->mcmember.mlid),
231 .port_num = priv->port,
232 .sl = mcast->mcmember.sl,
233 .ah_flags = IB_AH_GRH,
Jack Morgensteinbf6a9e32006-04-10 09:43:47 -0700234 .static_rate = mcast->mcmember.rate,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 .grh = {
236 .flow_label = be32_to_cpu(mcast->mcmember.flow_label),
237 .hop_limit = mcast->mcmember.hop_limit,
238 .sgid_index = 0,
239 .traffic_class = mcast->mcmember.traffic_class
240 }
241 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 av.grh.dgid = mcast->mcmember.mgid;
243
Eli Cohen7343b232006-02-27 20:47:43 -0800244 ah = ipoib_create_ah(dev, priv->pd, &av);
245 if (!ah) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 ipoib_warn(priv, "ib_address_create failed\n");
247 } else {
Or Gerlitz624d01f2006-07-24 10:42:00 +0300248 spin_lock_irq(&priv->lock);
249 mcast->ah = ah;
250 spin_unlock_irq(&priv->lock);
251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 ipoib_dbg_mcast(priv, "MGID " IPOIB_GID_FMT
253 " AV %p, LID 0x%04x, SL %d\n",
254 IPOIB_GID_ARG(mcast->mcmember.mgid),
255 mcast->ah->ah,
256 be16_to_cpu(mcast->mcmember.mlid),
257 mcast->mcmember.sl);
258 }
259 }
260
261 /* actually send any queued packets */
Michael S. Tsirkinb36f1702006-01-17 12:19:40 -0800262 spin_lock_irq(&priv->tx_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 while (!skb_queue_empty(&mcast->pkt_queue)) {
264 struct sk_buff *skb = skb_dequeue(&mcast->pkt_queue);
Michael S. Tsirkinb36f1702006-01-17 12:19:40 -0800265 spin_unlock_irq(&priv->tx_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
267 skb->dev = dev;
268
269 if (!skb->dst || !skb->dst->neighbour) {
270 /* put pseudoheader back on for next time */
271 skb_push(skb, sizeof (struct ipoib_pseudoheader));
272 }
273
274 if (dev_queue_xmit(skb))
275 ipoib_warn(priv, "dev_queue_xmit failed to requeue packet\n");
Michael S. Tsirkinb36f1702006-01-17 12:19:40 -0800276 spin_lock_irq(&priv->tx_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 }
Michael S. Tsirkinb36f1702006-01-17 12:19:40 -0800278 spin_unlock_irq(&priv->tx_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280 return 0;
281}
282
Sean Heftyfaec2f72007-02-15 17:00:17 -0800283static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284ipoib_mcast_sendonly_join_complete(int status,
Sean Heftyfaec2f72007-02-15 17:00:17 -0800285 struct ib_sa_multicast *multicast)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
Sean Heftyfaec2f72007-02-15 17:00:17 -0800287 struct ipoib_mcast *mcast = multicast->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 struct net_device *dev = mcast->dev;
Michael S. Tsirkinb36f1702006-01-17 12:19:40 -0800289 struct ipoib_dev_priv *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Sean Heftyfaec2f72007-02-15 17:00:17 -0800291 /* We trap for port events ourselves. */
292 if (status == -ENETRESET)
293 return 0;
294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 if (!status)
Sean Heftyfaec2f72007-02-15 17:00:17 -0800296 status = ipoib_mcast_join_finish(mcast, &multicast->rec);
297
298 if (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 if (mcast->logcount++ < 20)
300 ipoib_dbg_mcast(netdev_priv(dev), "multicast join failed for "
301 IPOIB_GID_FMT ", status %d\n",
302 IPOIB_GID_ARG(mcast->mcmember.mgid), status);
303
304 /* Flush out any queued packets */
Michael S. Tsirkinb36f1702006-01-17 12:19:40 -0800305 spin_lock_irq(&priv->tx_lock);
306 while (!skb_queue_empty(&mcast->pkt_queue)) {
Roland Dreierde903512007-09-28 15:33:51 -0700307 ++dev->stats.tx_dropped;
Roland Dreier8c608a32005-11-07 10:49:38 -0800308 dev_kfree_skb_any(skb_dequeue(&mcast->pkt_queue));
Michael S. Tsirkinb36f1702006-01-17 12:19:40 -0800309 }
310 spin_unlock_irq(&priv->tx_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
312 /* Clear the busy flag so we try again */
Sean Heftyfaec2f72007-02-15 17:00:17 -0800313 status = test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY,
314 &mcast->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 }
Sean Heftyfaec2f72007-02-15 17:00:17 -0800316 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317}
318
319static int ipoib_mcast_sendonly_join(struct ipoib_mcast *mcast)
320{
321 struct net_device *dev = mcast->dev;
322 struct ipoib_dev_priv *priv = netdev_priv(dev);
323 struct ib_sa_mcmember_rec rec = {
324#if 0 /* Some SMs don't support send-only yet */
325 .join_state = 4
326#else
327 .join_state = 1
328#endif
329 };
330 int ret = 0;
331
332 if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
333 ipoib_dbg_mcast(priv, "device shutting down, no multicast joins\n");
334 return -ENODEV;
335 }
336
337 if (test_and_set_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags)) {
338 ipoib_dbg_mcast(priv, "multicast entry busy, skipping\n");
339 return -EBUSY;
340 }
341
342 rec.mgid = mcast->mcmember.mgid;
343 rec.port_gid = priv->local_gid;
Sean Hefty97f52eb2005-08-13 21:05:57 -0700344 rec.pkey = cpu_to_be16(priv->pkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Sean Heftyfaec2f72007-02-15 17:00:17 -0800346 mcast->mc = ib_sa_join_multicast(&ipoib_sa_client, priv->ca,
347 priv->port, &rec,
348 IB_SA_MCMEMBER_REC_MGID |
349 IB_SA_MCMEMBER_REC_PORT_GID |
350 IB_SA_MCMEMBER_REC_PKEY |
351 IB_SA_MCMEMBER_REC_JOIN_STATE,
352 GFP_ATOMIC,
353 ipoib_mcast_sendonly_join_complete,
354 mcast);
355 if (IS_ERR(mcast->mc)) {
356 ret = PTR_ERR(mcast->mc);
357 clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
358 ipoib_warn(priv, "ib_sa_join_multicast failed (ret = %d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 ret);
360 } else {
361 ipoib_dbg_mcast(priv, "no multicast record for " IPOIB_GID_FMT
362 ", starting join\n",
363 IPOIB_GID_ARG(mcast->mcmember.mgid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 }
365
366 return ret;
367}
368
Sean Heftyfaec2f72007-02-15 17:00:17 -0800369static int ipoib_mcast_join_complete(int status,
370 struct ib_sa_multicast *multicast)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
Sean Heftyfaec2f72007-02-15 17:00:17 -0800372 struct ipoib_mcast *mcast = multicast->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 struct net_device *dev = mcast->dev;
374 struct ipoib_dev_priv *priv = netdev_priv(dev);
375
376 ipoib_dbg_mcast(priv, "join completion for " IPOIB_GID_FMT
377 " (status %d)\n",
378 IPOIB_GID_ARG(mcast->mcmember.mgid), status);
379
Sean Heftyfaec2f72007-02-15 17:00:17 -0800380 /* We trap for port events ourselves. */
381 if (status == -ENETRESET)
382 return 0;
383
384 if (!status)
385 status = ipoib_mcast_join_finish(mcast, &multicast->rec);
386
387 if (!status) {
Hal Rosenstockce5b65c2005-09-18 13:47:53 -0700388 mcast->backoff = 1;
Ingo Molnar95ed6442006-01-13 14:51:39 -0800389 mutex_lock(&mcast_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
David Howellsc4028952006-11-22 14:57:56 +0000391 queue_delayed_work(ipoib_workqueue,
392 &priv->mcast_task, 0);
Ingo Molnar95ed6442006-01-13 14:51:39 -0800393 mutex_unlock(&mcast_mutex);
Shirley Ma55c9add2007-03-08 14:59:30 -0800394
395 if (mcast == priv->broadcast)
396 netif_carrier_on(dev);
397
Sean Heftyfaec2f72007-02-15 17:00:17 -0800398 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 }
400
Sean Heftyfaec2f72007-02-15 17:00:17 -0800401 if (mcast->logcount++ < 20) {
402 if (status == -ETIMEDOUT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 ipoib_dbg_mcast(priv, "multicast join failed for " IPOIB_GID_FMT
404 ", status %d\n",
405 IPOIB_GID_ARG(mcast->mcmember.mgid),
406 status);
407 } else {
408 ipoib_warn(priv, "multicast join failed for "
409 IPOIB_GID_FMT ", status %d\n",
410 IPOIB_GID_ARG(mcast->mcmember.mgid),
411 status);
412 }
413 }
414
415 mcast->backoff *= 2;
416 if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
417 mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
418
Sean Heftyfaec2f72007-02-15 17:00:17 -0800419 /* Clear the busy flag so we try again */
420 status = test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
421
Michael S. Tsirkin9acf6a82006-03-02 11:07:47 -0800422 mutex_lock(&mcast_mutex);
Michael S. Tsirkin9acf6a82006-03-02 11:07:47 -0800423 spin_lock_irq(&priv->lock);
Sean Heftyfaec2f72007-02-15 17:00:17 -0800424 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
425 queue_delayed_work(ipoib_workqueue, &priv->mcast_task,
426 mcast->backoff * HZ);
Michael S. Tsirkin9acf6a82006-03-02 11:07:47 -0800427 spin_unlock_irq(&priv->lock);
Ingo Molnar95ed6442006-01-13 14:51:39 -0800428 mutex_unlock(&mcast_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
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
443 ipoib_dbg_mcast(priv, "joining MGID " IPOIB_GID_FMT "\n",
444 IPOIB_GID_ARG(mcast->mcmember.mgid));
445
446 rec.mgid = mcast->mcmember.mgid;
447 rec.port_gid = priv->local_gid;
Sean Hefty97f52eb2005-08-13 21:05:57 -0700448 rec.pkey = cpu_to_be16(priv->pkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
450 comp_mask =
451 IB_SA_MCMEMBER_REC_MGID |
452 IB_SA_MCMEMBER_REC_PORT_GID |
453 IB_SA_MCMEMBER_REC_PKEY |
454 IB_SA_MCMEMBER_REC_JOIN_STATE;
455
456 if (create) {
457 comp_mask |=
Roland Dreierd0df6d62006-09-22 15:22:56 -0700458 IB_SA_MCMEMBER_REC_QKEY |
459 IB_SA_MCMEMBER_REC_MTU_SELECTOR |
460 IB_SA_MCMEMBER_REC_MTU |
461 IB_SA_MCMEMBER_REC_TRAFFIC_CLASS |
462 IB_SA_MCMEMBER_REC_RATE_SELECTOR |
463 IB_SA_MCMEMBER_REC_RATE |
464 IB_SA_MCMEMBER_REC_SL |
465 IB_SA_MCMEMBER_REC_FLOW_LABEL |
466 IB_SA_MCMEMBER_REC_HOP_LIMIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
468 rec.qkey = priv->broadcast->mcmember.qkey;
Roland Dreierd0df6d62006-09-22 15:22:56 -0700469 rec.mtu_selector = IB_SA_EQ;
470 rec.mtu = priv->broadcast->mcmember.mtu;
471 rec.traffic_class = priv->broadcast->mcmember.traffic_class;
472 rec.rate_selector = IB_SA_EQ;
473 rec.rate = priv->broadcast->mcmember.rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 rec.sl = priv->broadcast->mcmember.sl;
475 rec.flow_label = priv->broadcast->mcmember.flow_label;
Roland Dreierd0df6d62006-09-22 15:22:56 -0700476 rec.hop_limit = priv->broadcast->mcmember.hop_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 }
478
Sean Heftyfaec2f72007-02-15 17:00:17 -0800479 set_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
480 mcast->mc = ib_sa_join_multicast(&ipoib_sa_client, priv->ca, priv->port,
481 &rec, comp_mask, GFP_KERNEL,
482 ipoib_mcast_join_complete, mcast);
483 if (IS_ERR(mcast->mc)) {
484 clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
485 ret = PTR_ERR(mcast->mc);
486 ipoib_warn(priv, "ib_sa_join_multicast failed, status %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
488 mcast->backoff *= 2;
489 if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
490 mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
491
Ingo Molnar95ed6442006-01-13 14:51:39 -0800492 mutex_lock(&mcast_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
494 queue_delayed_work(ipoib_workqueue,
495 &priv->mcast_task,
Hal Rosenstockce5b65c2005-09-18 13:47:53 -0700496 mcast->backoff * HZ);
Ingo Molnar95ed6442006-01-13 14:51:39 -0800497 mutex_unlock(&mcast_mutex);
Sean Heftyfaec2f72007-02-15 17:00:17 -0800498 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499}
500
David Howellsc4028952006-11-22 14:57:56 +0000501void ipoib_mcast_join_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
David Howellsc4028952006-11-22 14:57:56 +0000503 struct ipoib_dev_priv *priv =
504 container_of(work, struct ipoib_dev_priv, mcast_task.work);
505 struct net_device *dev = priv->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
507 if (!test_bit(IPOIB_MCAST_RUN, &priv->flags))
508 return;
509
510 if (ib_query_gid(priv->ca, priv->port, 0, &priv->local_gid))
Michael S. Tsirkin24bd1e42007-05-18 16:12:54 +0300511 ipoib_warn(priv, "ib_query_gid() failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 else
513 memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));
514
515 {
516 struct ib_port_attr attr;
517
Roland Dreier658bcef2007-02-21 20:28:05 -0800518 if (!ib_query_port(priv->ca, priv->port, &attr))
519 priv->local_lid = attr.lid;
520 else
Sean Heftyfaec2f72007-02-15 17:00:17 -0800521 ipoib_warn(priv, "ib_query_port failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 }
523
524 if (!priv->broadcast) {
Roland Dreier20b83382006-02-11 12:22:12 -0800525 struct ipoib_mcast *broadcast;
526
527 broadcast = ipoib_mcast_alloc(dev, 1);
528 if (!broadcast) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 ipoib_warn(priv, "failed to allocate broadcast group\n");
Ingo Molnar95ed6442006-01-13 14:51:39 -0800530 mutex_lock(&mcast_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
532 queue_delayed_work(ipoib_workqueue,
533 &priv->mcast_task, HZ);
Ingo Molnar95ed6442006-01-13 14:51:39 -0800534 mutex_unlock(&mcast_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 return;
536 }
537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 spin_lock_irq(&priv->lock);
Roland Dreier20b83382006-02-11 12:22:12 -0800539 memcpy(broadcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
540 sizeof (union ib_gid));
541 priv->broadcast = broadcast;
542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 __ipoib_mcast_add(dev, priv->broadcast);
544 spin_unlock_irq(&priv->lock);
545 }
546
547 if (!test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
Sean Heftyfaec2f72007-02-15 17:00:17 -0800548 if (!test_bit(IPOIB_MCAST_FLAG_BUSY, &priv->broadcast->flags))
549 ipoib_mcast_join(dev, priv->broadcast, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 return;
551 }
552
553 while (1) {
554 struct ipoib_mcast *mcast = NULL;
555
556 spin_lock_irq(&priv->lock);
557 list_for_each_entry(mcast, &priv->multicast_list, list) {
558 if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)
559 && !test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags)
560 && !test_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
561 /* Found the next unjoined group */
562 break;
563 }
564 }
565 spin_unlock_irq(&priv->lock);
566
567 if (&mcast->list == &priv->multicast_list) {
568 /* All done */
569 break;
570 }
571
572 ipoib_mcast_join(dev, mcast, 1);
573 return;
574 }
575
Shirley Mabc7b3a32008-04-23 11:55:45 -0700576 priv->mcast_mtu = IPOIB_UD_MTU(ib_mtu_enum_to_int(priv->broadcast->mcmember.mtu));
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200577
578 if (!ipoib_cm_admin_enabled(dev))
579 dev->mtu = min(priv->mcast_mtu, priv->admin_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
581 ipoib_dbg_mcast(priv, "successfully joined all multicast groups\n");
582
583 clear_bit(IPOIB_MCAST_RUN, &priv->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584}
585
586int ipoib_mcast_start_thread(struct net_device *dev)
587{
588 struct ipoib_dev_priv *priv = netdev_priv(dev);
589
590 ipoib_dbg_mcast(priv, "starting multicast thread\n");
591
Ingo Molnar95ed6442006-01-13 14:51:39 -0800592 mutex_lock(&mcast_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 if (!test_and_set_bit(IPOIB_MCAST_RUN, &priv->flags))
David Howellsc4028952006-11-22 14:57:56 +0000594 queue_delayed_work(ipoib_workqueue, &priv->mcast_task, 0);
Ingo Molnar95ed6442006-01-13 14:51:39 -0800595 mutex_unlock(&mcast_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
597 return 0;
598}
599
Roland Dreier8d2cae02005-09-20 10:52:04 -0700600int ipoib_mcast_stop_thread(struct net_device *dev, int flush)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601{
602 struct ipoib_dev_priv *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
604 ipoib_dbg_mcast(priv, "stopping multicast thread\n");
605
Ingo Molnar95ed6442006-01-13 14:51:39 -0800606 mutex_lock(&mcast_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 clear_bit(IPOIB_MCAST_RUN, &priv->flags);
608 cancel_delayed_work(&priv->mcast_task);
Ingo Molnar95ed6442006-01-13 14:51:39 -0800609 mutex_unlock(&mcast_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Roland Dreier8d2cae02005-09-20 10:52:04 -0700611 if (flush)
612 flush_workqueue(ipoib_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 return 0;
615}
616
617static int ipoib_mcast_leave(struct net_device *dev, struct ipoib_mcast *mcast)
618{
619 struct ipoib_dev_priv *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 int ret = 0;
621
Sean Heftye07832b2007-03-19 14:31:36 -0800622 if (test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
623 ib_sa_free_multicast(mcast->mc);
624
Sean Heftyfaec2f72007-02-15 17:00:17 -0800625 if (test_and_clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
626 ipoib_dbg_mcast(priv, "leaving MGID " IPOIB_GID_FMT "\n",
627 IPOIB_GID_ARG(mcast->mcmember.mgid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Sean Heftyfaec2f72007-02-15 17:00:17 -0800629 /* Remove ourselves from the multicast group */
Roland Dreier9eae5542008-07-14 23:48:50 -0700630 ret = ib_detach_mcast(priv->qp, &mcast->mcmember.mgid,
631 be16_to_cpu(mcast->mcmember.mlid));
Sean Heftyfaec2f72007-02-15 17:00:17 -0800632 if (ret)
Roland Dreier9eae5542008-07-14 23:48:50 -0700633 ipoib_warn(priv, "ib_detach_mcast failed (result = %d)\n", ret);
Sean Heftyfaec2f72007-02-15 17:00:17 -0800634 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 return 0;
637}
638
Jack Morgenstein37c22a72006-05-29 19:14:05 +0300639void ipoib_mcast_send(struct net_device *dev, void *mgid, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
641 struct ipoib_dev_priv *priv = netdev_priv(dev);
642 struct ipoib_mcast *mcast;
643
644 /*
645 * We can only be called from ipoib_start_xmit, so we're
646 * inside tx_lock -- no need to save/restore flags.
647 */
648 spin_lock(&priv->lock);
649
Or Gerlitzb3e27492008-03-11 16:10:02 +0200650 if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags) ||
Roland Dreier20b83382006-02-11 12:22:12 -0800651 !priv->broadcast ||
652 !test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
Roland Dreierde903512007-09-28 15:33:51 -0700653 ++dev->stats.tx_dropped;
Michael S. Tsirkin479a0792006-02-07 16:37:08 -0800654 dev_kfree_skb_any(skb);
655 goto unlock;
656 }
657
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 mcast = __ipoib_mcast_find(dev, mgid);
659 if (!mcast) {
660 /* Let's create a new send only group now */
661 ipoib_dbg_mcast(priv, "setting up send only multicast group for "
Jack Morgenstein37c22a72006-05-29 19:14:05 +0300662 IPOIB_GID_FMT "\n", IPOIB_GID_RAW_ARG(mgid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
664 mcast = ipoib_mcast_alloc(dev, 0);
665 if (!mcast) {
666 ipoib_warn(priv, "unable to allocate memory for "
667 "multicast structure\n");
Roland Dreierde903512007-09-28 15:33:51 -0700668 ++dev->stats.tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 dev_kfree_skb_any(skb);
670 goto out;
671 }
672
673 set_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags);
Jack Morgenstein37c22a72006-05-29 19:14:05 +0300674 memcpy(mcast->mcmember.mgid.raw, mgid, sizeof (union ib_gid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 __ipoib_mcast_add(dev, mcast);
676 list_add_tail(&mcast->list, &priv->multicast_list);
677 }
678
679 if (!mcast->ah) {
680 if (skb_queue_len(&mcast->pkt_queue) < IPOIB_MAX_MCAST_QUEUE)
681 skb_queue_tail(&mcast->pkt_queue, skb);
Michael S. Tsirkinb36f1702006-01-17 12:19:40 -0800682 else {
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);
Michael S. Tsirkinb36f1702006-01-17 12:19:40 -0800685 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Sean Heftyfaec2f72007-02-15 17:00:17 -0800687 if (test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 ipoib_dbg_mcast(priv, "no address vector, "
689 "but multicast join already started\n");
690 else if (test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags))
691 ipoib_mcast_sendonly_join(mcast);
692
693 /*
694 * If lookup completes between here and out:, don't
695 * want to send packet twice.
696 */
697 mcast = NULL;
698 }
699
700out:
701 if (mcast && mcast->ah) {
Roland Dreier2337f802007-10-23 19:57:54 -0700702 if (skb->dst &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 skb->dst->neighbour &&
704 !*to_ipoib_neigh(skb->dst->neighbour)) {
Moni Shoua732a2172007-10-09 19:43:36 -0700705 struct ipoib_neigh *neigh = ipoib_neigh_alloc(skb->dst->neighbour,
706 skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
708 if (neigh) {
709 kref_get(&mcast->ah->ref);
Roland Dreier2337f802007-10-23 19:57:54 -0700710 neigh->ah = mcast->ah;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 list_add_tail(&neigh->list, &mcast->neigh_list);
712 }
713 }
714
715 ipoib_send(dev, skb, mcast->ah, IB_MULTICAST_QPN);
716 }
717
Michael S. Tsirkin479a0792006-02-07 16:37:08 -0800718unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 spin_unlock(&priv->lock);
720}
721
722void ipoib_mcast_dev_flush(struct net_device *dev)
723{
724 struct ipoib_dev_priv *priv = netdev_priv(dev);
725 LIST_HEAD(remove_list);
Eli Cohen988bd502006-01-12 14:32:20 -0800726 struct ipoib_mcast *mcast, *tmcast;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 unsigned long flags;
728
729 ipoib_dbg_mcast(priv, "flushing multicast list\n");
730
731 spin_lock_irqsave(&priv->lock, flags);
Eli Cohen988bd502006-01-12 14:32:20 -0800732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
Eli Cohen988bd502006-01-12 14:32:20 -0800734 list_del(&mcast->list);
735 rb_erase(&mcast->rb_node, &priv->multicast_tree);
736 list_add_tail(&mcast->list, &remove_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 }
738
739 if (priv->broadcast) {
Roland Dreier3cd96562006-09-22 15:22:46 -0700740 rb_erase(&priv->broadcast->rb_node, &priv->multicast_tree);
Eli Cohen988bd502006-01-12 14:32:20 -0800741 list_add_tail(&priv->broadcast->list, &remove_list);
742 priv->broadcast = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 }
744
745 spin_unlock_irqrestore(&priv->lock, flags);
746
747 list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
748 ipoib_mcast_leave(dev, mcast);
749 ipoib_mcast_free(mcast);
750 }
751}
752
David Howellsc4028952006-11-22 14:57:56 +0000753void ipoib_mcast_restart_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754{
David Howellsc4028952006-11-22 14:57:56 +0000755 struct ipoib_dev_priv *priv =
756 container_of(work, struct ipoib_dev_priv, restart_task);
757 struct net_device *dev = priv->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 struct dev_mc_list *mclist;
759 struct ipoib_mcast *mcast, *tmcast;
760 LIST_HEAD(remove_list);
761 unsigned long flags;
Or Gerlitz335a64a5a2007-10-08 10:13:00 +0200762 struct ib_sa_mcmember_rec rec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
764 ipoib_dbg_mcast(priv, "restarting multicast task\n");
765
Roland Dreier8d2cae02005-09-20 10:52:04 -0700766 ipoib_mcast_stop_thread(dev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
Herbert Xu932ff272006-06-09 12:20:56 -0700768 local_irq_save(flags);
769 netif_tx_lock(dev);
Michael S. Tsirkin78bfe0b2006-01-11 11:47:34 -0800770 spin_lock(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
772 /*
773 * Unfortunately, the networking core only gives us a list of all of
774 * the multicast hardware addresses. We need to figure out which ones
775 * are new and which ones have been removed
776 */
777
778 /* Clear out the found flag */
779 list_for_each_entry(mcast, &priv->multicast_list, list)
780 clear_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
781
782 /* Mark all of the entries that are found or don't exist */
783 for (mclist = dev->mc_list; mclist; mclist = mclist->next) {
784 union ib_gid mgid;
785
786 memcpy(mgid.raw, mclist->dmi_addr + 4, sizeof mgid);
787
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 mcast = __ipoib_mcast_find(dev, &mgid);
789 if (!mcast || test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
790 struct ipoib_mcast *nmcast;
791
Or Gerlitz335a64a5a2007-10-08 10:13:00 +0200792 /* ignore group which is directly joined by userspace */
793 if (test_bit(IPOIB_FLAG_UMCAST, &priv->flags) &&
794 !ib_sa_get_mcmember_rec(priv->ca, priv->port, &mgid, &rec)) {
795 ipoib_dbg_mcast(priv, "ignoring multicast entry for mgid "
796 IPOIB_GID_FMT "\n", IPOIB_GID_ARG(mgid));
797 continue;
798 }
799
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 /* Not found or send-only group, let's add a new entry */
801 ipoib_dbg_mcast(priv, "adding multicast entry for mgid "
802 IPOIB_GID_FMT "\n", IPOIB_GID_ARG(mgid));
803
804 nmcast = ipoib_mcast_alloc(dev, 0);
805 if (!nmcast) {
806 ipoib_warn(priv, "unable to allocate memory for multicast structure\n");
807 continue;
808 }
809
810 set_bit(IPOIB_MCAST_FLAG_FOUND, &nmcast->flags);
811
812 nmcast->mcmember.mgid = mgid;
813
814 if (mcast) {
815 /* Destroy the send only entry */
Akinobu Mita179e0912006-06-26 00:24:41 -0700816 list_move_tail(&mcast->list, &remove_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
818 rb_replace_node(&mcast->rb_node,
819 &nmcast->rb_node,
820 &priv->multicast_tree);
821 } else
822 __ipoib_mcast_add(dev, nmcast);
823
824 list_add_tail(&nmcast->list, &priv->multicast_list);
825 }
826
827 if (mcast)
828 set_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
829 }
830
831 /* Remove all of the entries don't exist anymore */
832 list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
833 if (!test_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags) &&
834 !test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
835 ipoib_dbg_mcast(priv, "deleting multicast group " IPOIB_GID_FMT "\n",
836 IPOIB_GID_ARG(mcast->mcmember.mgid));
837
838 rb_erase(&mcast->rb_node, &priv->multicast_tree);
839
840 /* Move to the remove list */
Akinobu Mita179e0912006-06-26 00:24:41 -0700841 list_move_tail(&mcast->list, &remove_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 }
843 }
Michael S. Tsirkin78bfe0b2006-01-11 11:47:34 -0800844
845 spin_unlock(&priv->lock);
Herbert Xu932ff272006-06-09 12:20:56 -0700846 netif_tx_unlock(dev);
847 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
849 /* We have to cancel outside of the spinlock */
850 list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
851 ipoib_mcast_leave(mcast->dev, mcast);
852 ipoib_mcast_free(mcast);
853 }
854
855 if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
856 ipoib_mcast_start_thread(dev);
857}
858
Roland Dreier8ae5a8a2005-11-02 20:51:01 -0800859#ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
860
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861struct ipoib_mcast_iter *ipoib_mcast_iter_init(struct net_device *dev)
862{
863 struct ipoib_mcast_iter *iter;
864
865 iter = kmalloc(sizeof *iter, GFP_KERNEL);
866 if (!iter)
867 return NULL;
868
869 iter->dev = dev;
Roland Dreier1732b0e2005-11-07 10:33:11 -0800870 memset(iter->mgid.raw, 0, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
872 if (ipoib_mcast_iter_next(iter)) {
Roland Dreier1732b0e2005-11-07 10:33:11 -0800873 kfree(iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 return NULL;
875 }
876
877 return iter;
878}
879
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880int ipoib_mcast_iter_next(struct ipoib_mcast_iter *iter)
881{
882 struct ipoib_dev_priv *priv = netdev_priv(iter->dev);
883 struct rb_node *n;
884 struct ipoib_mcast *mcast;
885 int ret = 1;
886
887 spin_lock_irq(&priv->lock);
888
889 n = rb_first(&priv->multicast_tree);
890
891 while (n) {
892 mcast = rb_entry(n, struct ipoib_mcast, rb_node);
893
894 if (memcmp(iter->mgid.raw, mcast->mcmember.mgid.raw,
895 sizeof (union ib_gid)) < 0) {
896 iter->mgid = mcast->mcmember.mgid;
897 iter->created = mcast->created;
898 iter->queuelen = skb_queue_len(&mcast->pkt_queue);
899 iter->complete = !!mcast->ah;
900 iter->send_only = !!(mcast->flags & (1 << IPOIB_MCAST_FLAG_SENDONLY));
901
902 ret = 0;
903
904 break;
905 }
906
907 n = rb_next(n);
908 }
909
910 spin_unlock_irq(&priv->lock);
911
912 return ret;
913}
914
915void ipoib_mcast_iter_read(struct ipoib_mcast_iter *iter,
916 union ib_gid *mgid,
917 unsigned long *created,
918 unsigned int *queuelen,
919 unsigned int *complete,
920 unsigned int *send_only)
921{
922 *mgid = iter->mgid;
923 *created = iter->created;
924 *queuelen = iter->queuelen;
925 *complete = iter->complete;
926 *send_only = iter->send_only;
927}
Roland Dreier8ae5a8a2005-11-02 20:51:01 -0800928
929#endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */