2 * Copyright (c) 2004, 2005 Topspin Communications. All rights reserved.
3 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
4 * Copyright (c) 2004 Voltaire, Inc. All rights reserved.
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:
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
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.
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
35 #include <linux/skbuff.h>
36 #include <linux/rtnetlink.h>
39 #include <linux/igmp.h>
40 #include <linux/inetdevice.h>
41 #include <linux/delay.h>
42 #include <linux/completion.h>
48 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
49 static int mcast_debug_level;
51 module_param(mcast_debug_level, int, 0644);
52 MODULE_PARM_DESC(mcast_debug_level,
53 "Enable multicast debug tracing if > 0");
56 static DEFINE_MUTEX(mcast_mutex);
58 struct ipoib_mcast_iter {
59 struct net_device *dev;
61 unsigned long created;
62 unsigned int queuelen;
63 unsigned int complete;
64 unsigned int send_only;
67 static void ipoib_mcast_free(struct ipoib_mcast *mcast)
69 struct net_device *dev = mcast->dev;
70 struct ipoib_dev_priv *priv = netdev_priv(dev);
71 struct ipoib_neigh *neigh, *tmp;
75 ipoib_dbg_mcast(netdev_priv(dev),
76 "deleting multicast group " IPOIB_GID_FMT "\n",
77 IPOIB_GID_ARG(mcast->mcmember.mgid));
79 spin_lock_irqsave(&priv->lock, flags);
81 list_for_each_entry_safe(neigh, tmp, &mcast->neigh_list, list) {
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.
89 ipoib_put_ah(neigh->ah);
90 ipoib_neigh_free(dev, neigh);
93 spin_unlock_irqrestore(&priv->lock, flags);
96 ipoib_put_ah(mcast->ah);
98 while (!skb_queue_empty(&mcast->pkt_queue)) {
100 dev_kfree_skb_any(skb_dequeue(&mcast->pkt_queue));
103 spin_lock_irqsave(&priv->tx_lock, flags);
104 dev->stats.tx_dropped += tx_dropped;
105 spin_unlock_irqrestore(&priv->tx_lock, flags);
110 static struct ipoib_mcast *ipoib_mcast_alloc(struct net_device *dev,
113 struct ipoib_mcast *mcast;
115 mcast = kzalloc(sizeof *mcast, can_sleep ? GFP_KERNEL : GFP_ATOMIC);
120 mcast->created = jiffies;
123 INIT_LIST_HEAD(&mcast->list);
124 INIT_LIST_HEAD(&mcast->neigh_list);
125 skb_queue_head_init(&mcast->pkt_queue);
130 static struct ipoib_mcast *__ipoib_mcast_find(struct net_device *dev, void *mgid)
132 struct ipoib_dev_priv *priv = netdev_priv(dev);
133 struct rb_node *n = priv->multicast_tree.rb_node;
136 struct ipoib_mcast *mcast;
139 mcast = rb_entry(n, struct ipoib_mcast, rb_node);
141 ret = memcmp(mgid, mcast->mcmember.mgid.raw,
142 sizeof (union ib_gid));
154 static int __ipoib_mcast_add(struct net_device *dev, struct ipoib_mcast *mcast)
156 struct ipoib_dev_priv *priv = netdev_priv(dev);
157 struct rb_node **n = &priv->multicast_tree.rb_node, *pn = NULL;
160 struct ipoib_mcast *tmcast;
164 tmcast = rb_entry(pn, struct ipoib_mcast, rb_node);
166 ret = memcmp(mcast->mcmember.mgid.raw, tmcast->mcmember.mgid.raw,
167 sizeof (union ib_gid));
176 rb_link_node(&mcast->rb_node, pn, n);
177 rb_insert_color(&mcast->rb_node, &priv->multicast_tree);
182 static int ipoib_mcast_join_finish(struct ipoib_mcast *mcast,
183 struct ib_sa_mcmember_rec *mcmember)
185 struct net_device *dev = mcast->dev;
186 struct ipoib_dev_priv *priv = netdev_priv(dev);
191 mcast->mcmember = *mcmember;
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))) {
196 spin_lock_irq(&priv->lock);
197 if (!priv->broadcast) {
198 spin_unlock_irq(&priv->lock);
201 priv->qkey = be32_to_cpu(priv->broadcast->mcmember.qkey);
202 spin_unlock_irq(&priv->lock);
203 priv->tx_wr.wr.ud.remote_qkey = priv->qkey;
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));
216 ret = ipoib_mcast_attach(dev, be16_to_cpu(mcast->mcmember.mlid),
217 &mcast->mcmember.mgid, set_qkey);
219 ipoib_warn(priv, "couldn't attach QP to multicast group "
221 IPOIB_GID_ARG(mcast->mcmember.mgid));
223 clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags);
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,
234 .static_rate = mcast->mcmember.rate,
236 .flow_label = be32_to_cpu(mcast->mcmember.flow_label),
237 .hop_limit = mcast->mcmember.hop_limit,
239 .traffic_class = mcast->mcmember.traffic_class
242 av.grh.dgid = mcast->mcmember.mgid;
244 ah = ipoib_create_ah(dev, priv->pd, &av);
246 ipoib_warn(priv, "ib_address_create failed\n");
248 spin_lock_irq(&priv->lock);
250 spin_unlock_irq(&priv->lock);
252 ipoib_dbg_mcast(priv, "MGID " IPOIB_GID_FMT
253 " AV %p, LID 0x%04x, SL %d\n",
254 IPOIB_GID_ARG(mcast->mcmember.mgid),
256 be16_to_cpu(mcast->mcmember.mlid),
261 /* actually send any queued packets */
262 spin_lock_irq(&priv->tx_lock);
263 while (!skb_queue_empty(&mcast->pkt_queue)) {
264 struct sk_buff *skb = skb_dequeue(&mcast->pkt_queue);
265 spin_unlock_irq(&priv->tx_lock);
269 if (!skb->dst || !skb->dst->neighbour) {
270 /* put pseudoheader back on for next time */
271 skb_push(skb, sizeof (struct ipoib_pseudoheader));
274 if (dev_queue_xmit(skb))
275 ipoib_warn(priv, "dev_queue_xmit failed to requeue packet\n");
276 spin_lock_irq(&priv->tx_lock);
278 spin_unlock_irq(&priv->tx_lock);
284 ipoib_mcast_sendonly_join_complete(int status,
285 struct ib_sa_multicast *multicast)
287 struct ipoib_mcast *mcast = multicast->context;
288 struct net_device *dev = mcast->dev;
289 struct ipoib_dev_priv *priv = netdev_priv(dev);
291 /* We trap for port events ourselves. */
292 if (status == -ENETRESET)
296 status = ipoib_mcast_join_finish(mcast, &multicast->rec);
299 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);
304 /* Flush out any queued packets */
305 spin_lock_irq(&priv->tx_lock);
306 while (!skb_queue_empty(&mcast->pkt_queue)) {
307 ++dev->stats.tx_dropped;
308 dev_kfree_skb_any(skb_dequeue(&mcast->pkt_queue));
310 spin_unlock_irq(&priv->tx_lock);
312 /* Clear the busy flag so we try again */
313 status = test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY,
319 static int ipoib_mcast_sendonly_join(struct ipoib_mcast *mcast)
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 */
332 if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
333 ipoib_dbg_mcast(priv, "device shutting down, no multicast joins\n");
337 if (test_and_set_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags)) {
338 ipoib_dbg_mcast(priv, "multicast entry busy, skipping\n");
342 rec.mgid = mcast->mcmember.mgid;
343 rec.port_gid = priv->local_gid;
344 rec.pkey = cpu_to_be16(priv->pkey);
346 mcast->mc = ib_sa_join_multicast(&ipoib_sa_client, priv->ca,
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,
353 ipoib_mcast_sendonly_join_complete,
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",
361 ipoib_dbg_mcast(priv, "no multicast record for " IPOIB_GID_FMT
363 IPOIB_GID_ARG(mcast->mcmember.mgid));
369 static int ipoib_mcast_join_complete(int status,
370 struct ib_sa_multicast *multicast)
372 struct ipoib_mcast *mcast = multicast->context;
373 struct net_device *dev = mcast->dev;
374 struct ipoib_dev_priv *priv = netdev_priv(dev);
376 ipoib_dbg_mcast(priv, "join completion for " IPOIB_GID_FMT
378 IPOIB_GID_ARG(mcast->mcmember.mgid), status);
380 /* We trap for port events ourselves. */
381 if (status == -ENETRESET)
385 status = ipoib_mcast_join_finish(mcast, &multicast->rec);
389 mutex_lock(&mcast_mutex);
390 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
391 queue_delayed_work(ipoib_workqueue,
392 &priv->mcast_task, 0);
393 mutex_unlock(&mcast_mutex);
395 if (mcast == priv->broadcast) {
397 * Take RTNL lock here to avoid racing with
398 * ipoib_stop() and turning the carrier back
399 * on while a device is being removed.
402 netif_carrier_on(dev);
409 if (mcast->logcount++ < 20) {
410 if (status == -ETIMEDOUT) {
411 ipoib_dbg_mcast(priv, "multicast join failed for " IPOIB_GID_FMT
413 IPOIB_GID_ARG(mcast->mcmember.mgid),
416 ipoib_warn(priv, "multicast join failed for "
417 IPOIB_GID_FMT ", status %d\n",
418 IPOIB_GID_ARG(mcast->mcmember.mgid),
424 if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
425 mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
427 /* Clear the busy flag so we try again */
428 status = test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
430 mutex_lock(&mcast_mutex);
431 spin_lock_irq(&priv->lock);
432 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
433 queue_delayed_work(ipoib_workqueue, &priv->mcast_task,
434 mcast->backoff * HZ);
435 spin_unlock_irq(&priv->lock);
436 mutex_unlock(&mcast_mutex);
441 static void ipoib_mcast_join(struct net_device *dev, struct ipoib_mcast *mcast,
444 struct ipoib_dev_priv *priv = netdev_priv(dev);
445 struct ib_sa_mcmember_rec rec = {
448 ib_sa_comp_mask comp_mask;
451 ipoib_dbg_mcast(priv, "joining MGID " IPOIB_GID_FMT "\n",
452 IPOIB_GID_ARG(mcast->mcmember.mgid));
454 rec.mgid = mcast->mcmember.mgid;
455 rec.port_gid = priv->local_gid;
456 rec.pkey = cpu_to_be16(priv->pkey);
459 IB_SA_MCMEMBER_REC_MGID |
460 IB_SA_MCMEMBER_REC_PORT_GID |
461 IB_SA_MCMEMBER_REC_PKEY |
462 IB_SA_MCMEMBER_REC_JOIN_STATE;
466 IB_SA_MCMEMBER_REC_QKEY |
467 IB_SA_MCMEMBER_REC_MTU_SELECTOR |
468 IB_SA_MCMEMBER_REC_MTU |
469 IB_SA_MCMEMBER_REC_TRAFFIC_CLASS |
470 IB_SA_MCMEMBER_REC_RATE_SELECTOR |
471 IB_SA_MCMEMBER_REC_RATE |
472 IB_SA_MCMEMBER_REC_SL |
473 IB_SA_MCMEMBER_REC_FLOW_LABEL |
474 IB_SA_MCMEMBER_REC_HOP_LIMIT;
476 rec.qkey = priv->broadcast->mcmember.qkey;
477 rec.mtu_selector = IB_SA_EQ;
478 rec.mtu = priv->broadcast->mcmember.mtu;
479 rec.traffic_class = priv->broadcast->mcmember.traffic_class;
480 rec.rate_selector = IB_SA_EQ;
481 rec.rate = priv->broadcast->mcmember.rate;
482 rec.sl = priv->broadcast->mcmember.sl;
483 rec.flow_label = priv->broadcast->mcmember.flow_label;
484 rec.hop_limit = priv->broadcast->mcmember.hop_limit;
487 set_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
488 mcast->mc = ib_sa_join_multicast(&ipoib_sa_client, priv->ca, priv->port,
489 &rec, comp_mask, GFP_KERNEL,
490 ipoib_mcast_join_complete, mcast);
491 if (IS_ERR(mcast->mc)) {
492 clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
493 ret = PTR_ERR(mcast->mc);
494 ipoib_warn(priv, "ib_sa_join_multicast failed, status %d\n", ret);
497 if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
498 mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
500 mutex_lock(&mcast_mutex);
501 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
502 queue_delayed_work(ipoib_workqueue,
504 mcast->backoff * HZ);
505 mutex_unlock(&mcast_mutex);
509 void ipoib_mcast_join_task(struct work_struct *work)
511 struct ipoib_dev_priv *priv =
512 container_of(work, struct ipoib_dev_priv, mcast_task.work);
513 struct net_device *dev = priv->dev;
515 if (!test_bit(IPOIB_MCAST_RUN, &priv->flags))
518 if (ib_query_gid(priv->ca, priv->port, 0, &priv->local_gid))
519 ipoib_warn(priv, "ib_query_gid() failed\n");
521 memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));
524 struct ib_port_attr attr;
526 if (!ib_query_port(priv->ca, priv->port, &attr))
527 priv->local_lid = attr.lid;
529 ipoib_warn(priv, "ib_query_port failed\n");
532 if (!priv->broadcast) {
533 struct ipoib_mcast *broadcast;
535 broadcast = ipoib_mcast_alloc(dev, 1);
537 ipoib_warn(priv, "failed to allocate broadcast group\n");
538 mutex_lock(&mcast_mutex);
539 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
540 queue_delayed_work(ipoib_workqueue,
541 &priv->mcast_task, HZ);
542 mutex_unlock(&mcast_mutex);
546 spin_lock_irq(&priv->lock);
547 memcpy(broadcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
548 sizeof (union ib_gid));
549 priv->broadcast = broadcast;
551 __ipoib_mcast_add(dev, priv->broadcast);
552 spin_unlock_irq(&priv->lock);
555 if (!test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
556 if (!test_bit(IPOIB_MCAST_FLAG_BUSY, &priv->broadcast->flags))
557 ipoib_mcast_join(dev, priv->broadcast, 0);
562 struct ipoib_mcast *mcast = NULL;
564 spin_lock_irq(&priv->lock);
565 list_for_each_entry(mcast, &priv->multicast_list, list) {
566 if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)
567 && !test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags)
568 && !test_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
569 /* Found the next unjoined group */
573 spin_unlock_irq(&priv->lock);
575 if (&mcast->list == &priv->multicast_list) {
580 ipoib_mcast_join(dev, mcast, 1);
584 priv->mcast_mtu = IPOIB_UD_MTU(ib_mtu_enum_to_int(priv->broadcast->mcmember.mtu));
586 if (!ipoib_cm_admin_enabled(dev)) {
588 dev_set_mtu(dev, min(priv->mcast_mtu, priv->admin_mtu));
592 ipoib_dbg_mcast(priv, "successfully joined all multicast groups\n");
594 clear_bit(IPOIB_MCAST_RUN, &priv->flags);
597 int ipoib_mcast_start_thread(struct net_device *dev)
599 struct ipoib_dev_priv *priv = netdev_priv(dev);
601 ipoib_dbg_mcast(priv, "starting multicast thread\n");
603 mutex_lock(&mcast_mutex);
604 if (!test_and_set_bit(IPOIB_MCAST_RUN, &priv->flags))
605 queue_delayed_work(ipoib_workqueue, &priv->mcast_task, 0);
606 mutex_unlock(&mcast_mutex);
611 int ipoib_mcast_stop_thread(struct net_device *dev, int flush)
613 struct ipoib_dev_priv *priv = netdev_priv(dev);
615 ipoib_dbg_mcast(priv, "stopping multicast thread\n");
617 mutex_lock(&mcast_mutex);
618 clear_bit(IPOIB_MCAST_RUN, &priv->flags);
619 cancel_delayed_work(&priv->mcast_task);
620 mutex_unlock(&mcast_mutex);
623 flush_workqueue(ipoib_workqueue);
628 static int ipoib_mcast_leave(struct net_device *dev, struct ipoib_mcast *mcast)
630 struct ipoib_dev_priv *priv = netdev_priv(dev);
633 if (test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
634 ib_sa_free_multicast(mcast->mc);
636 if (test_and_clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
637 ipoib_dbg_mcast(priv, "leaving MGID " IPOIB_GID_FMT "\n",
638 IPOIB_GID_ARG(mcast->mcmember.mgid));
640 /* Remove ourselves from the multicast group */
641 ret = ib_detach_mcast(priv->qp, &mcast->mcmember.mgid,
642 be16_to_cpu(mcast->mcmember.mlid));
644 ipoib_warn(priv, "ib_detach_mcast failed (result = %d)\n", ret);
650 void ipoib_mcast_send(struct net_device *dev, void *mgid, struct sk_buff *skb)
652 struct ipoib_dev_priv *priv = netdev_priv(dev);
653 struct ipoib_mcast *mcast;
656 * We can only be called from ipoib_start_xmit, so we're
657 * inside tx_lock -- no need to save/restore flags.
659 spin_lock(&priv->lock);
661 if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags) ||
663 !test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
664 ++dev->stats.tx_dropped;
665 dev_kfree_skb_any(skb);
669 mcast = __ipoib_mcast_find(dev, mgid);
671 /* Let's create a new send only group now */
672 ipoib_dbg_mcast(priv, "setting up send only multicast group for "
673 IPOIB_GID_FMT "\n", IPOIB_GID_RAW_ARG(mgid));
675 mcast = ipoib_mcast_alloc(dev, 0);
677 ipoib_warn(priv, "unable to allocate memory for "
678 "multicast structure\n");
679 ++dev->stats.tx_dropped;
680 dev_kfree_skb_any(skb);
684 set_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags);
685 memcpy(mcast->mcmember.mgid.raw, mgid, sizeof (union ib_gid));
686 __ipoib_mcast_add(dev, mcast);
687 list_add_tail(&mcast->list, &priv->multicast_list);
691 if (skb_queue_len(&mcast->pkt_queue) < IPOIB_MAX_MCAST_QUEUE)
692 skb_queue_tail(&mcast->pkt_queue, skb);
694 ++dev->stats.tx_dropped;
695 dev_kfree_skb_any(skb);
698 if (test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
699 ipoib_dbg_mcast(priv, "no address vector, "
700 "but multicast join already started\n");
701 else if (test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags))
702 ipoib_mcast_sendonly_join(mcast);
705 * If lookup completes between here and out:, don't
706 * want to send packet twice.
712 if (mcast && mcast->ah) {
714 skb->dst->neighbour &&
715 !*to_ipoib_neigh(skb->dst->neighbour)) {
716 struct ipoib_neigh *neigh = ipoib_neigh_alloc(skb->dst->neighbour,
720 kref_get(&mcast->ah->ref);
721 neigh->ah = mcast->ah;
722 list_add_tail(&neigh->list, &mcast->neigh_list);
726 ipoib_send(dev, skb, mcast->ah, IB_MULTICAST_QPN);
730 spin_unlock(&priv->lock);
733 void ipoib_mcast_dev_flush(struct net_device *dev)
735 struct ipoib_dev_priv *priv = netdev_priv(dev);
736 LIST_HEAD(remove_list);
737 struct ipoib_mcast *mcast, *tmcast;
740 ipoib_dbg_mcast(priv, "flushing multicast list\n");
742 spin_lock_irqsave(&priv->lock, flags);
744 list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
745 list_del(&mcast->list);
746 rb_erase(&mcast->rb_node, &priv->multicast_tree);
747 list_add_tail(&mcast->list, &remove_list);
750 if (priv->broadcast) {
751 rb_erase(&priv->broadcast->rb_node, &priv->multicast_tree);
752 list_add_tail(&priv->broadcast->list, &remove_list);
753 priv->broadcast = NULL;
756 spin_unlock_irqrestore(&priv->lock, flags);
758 list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
759 ipoib_mcast_leave(dev, mcast);
760 ipoib_mcast_free(mcast);
764 void ipoib_mcast_restart_task(struct work_struct *work)
766 struct ipoib_dev_priv *priv =
767 container_of(work, struct ipoib_dev_priv, restart_task);
768 struct net_device *dev = priv->dev;
769 struct dev_mc_list *mclist;
770 struct ipoib_mcast *mcast, *tmcast;
771 LIST_HEAD(remove_list);
773 struct ib_sa_mcmember_rec rec;
775 ipoib_dbg_mcast(priv, "restarting multicast task\n");
777 ipoib_mcast_stop_thread(dev, 0);
779 local_irq_save(flags);
780 netif_addr_lock(dev);
781 spin_lock(&priv->lock);
784 * Unfortunately, the networking core only gives us a list of all of
785 * the multicast hardware addresses. We need to figure out which ones
786 * are new and which ones have been removed
789 /* Clear out the found flag */
790 list_for_each_entry(mcast, &priv->multicast_list, list)
791 clear_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
793 /* Mark all of the entries that are found or don't exist */
794 for (mclist = dev->mc_list; mclist; mclist = mclist->next) {
797 memcpy(mgid.raw, mclist->dmi_addr + 4, sizeof mgid);
799 mcast = __ipoib_mcast_find(dev, &mgid);
800 if (!mcast || test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
801 struct ipoib_mcast *nmcast;
803 /* ignore group which is directly joined by userspace */
804 if (test_bit(IPOIB_FLAG_UMCAST, &priv->flags) &&
805 !ib_sa_get_mcmember_rec(priv->ca, priv->port, &mgid, &rec)) {
806 ipoib_dbg_mcast(priv, "ignoring multicast entry for mgid "
807 IPOIB_GID_FMT "\n", IPOIB_GID_ARG(mgid));
811 /* Not found or send-only group, let's add a new entry */
812 ipoib_dbg_mcast(priv, "adding multicast entry for mgid "
813 IPOIB_GID_FMT "\n", IPOIB_GID_ARG(mgid));
815 nmcast = ipoib_mcast_alloc(dev, 0);
817 ipoib_warn(priv, "unable to allocate memory for multicast structure\n");
821 set_bit(IPOIB_MCAST_FLAG_FOUND, &nmcast->flags);
823 nmcast->mcmember.mgid = mgid;
826 /* Destroy the send only entry */
827 list_move_tail(&mcast->list, &remove_list);
829 rb_replace_node(&mcast->rb_node,
831 &priv->multicast_tree);
833 __ipoib_mcast_add(dev, nmcast);
835 list_add_tail(&nmcast->list, &priv->multicast_list);
839 set_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
842 /* Remove all of the entries don't exist anymore */
843 list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
844 if (!test_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags) &&
845 !test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
846 ipoib_dbg_mcast(priv, "deleting multicast group " IPOIB_GID_FMT "\n",
847 IPOIB_GID_ARG(mcast->mcmember.mgid));
849 rb_erase(&mcast->rb_node, &priv->multicast_tree);
851 /* Move to the remove list */
852 list_move_tail(&mcast->list, &remove_list);
856 spin_unlock(&priv->lock);
857 netif_addr_unlock(dev);
858 local_irq_restore(flags);
860 /* We have to cancel outside of the spinlock */
861 list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
862 ipoib_mcast_leave(mcast->dev, mcast);
863 ipoib_mcast_free(mcast);
866 if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
867 ipoib_mcast_start_thread(dev);
870 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
872 struct ipoib_mcast_iter *ipoib_mcast_iter_init(struct net_device *dev)
874 struct ipoib_mcast_iter *iter;
876 iter = kmalloc(sizeof *iter, GFP_KERNEL);
881 memset(iter->mgid.raw, 0, 16);
883 if (ipoib_mcast_iter_next(iter)) {
891 int ipoib_mcast_iter_next(struct ipoib_mcast_iter *iter)
893 struct ipoib_dev_priv *priv = netdev_priv(iter->dev);
895 struct ipoib_mcast *mcast;
898 spin_lock_irq(&priv->lock);
900 n = rb_first(&priv->multicast_tree);
903 mcast = rb_entry(n, struct ipoib_mcast, rb_node);
905 if (memcmp(iter->mgid.raw, mcast->mcmember.mgid.raw,
906 sizeof (union ib_gid)) < 0) {
907 iter->mgid = mcast->mcmember.mgid;
908 iter->created = mcast->created;
909 iter->queuelen = skb_queue_len(&mcast->pkt_queue);
910 iter->complete = !!mcast->ah;
911 iter->send_only = !!(mcast->flags & (1 << IPOIB_MCAST_FLAG_SENDONLY));
921 spin_unlock_irq(&priv->lock);
926 void ipoib_mcast_iter_read(struct ipoib_mcast_iter *iter,
928 unsigned long *created,
929 unsigned int *queuelen,
930 unsigned int *complete,
931 unsigned int *send_only)
934 *created = iter->created;
935 *queuelen = iter->queuelen;
936 *complete = iter->complete;
937 *send_only = iter->send_only;
940 #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */