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>
37 #include <linux/moduleparam.h>
40 #include <linux/igmp.h>
41 #include <linux/inetdevice.h>
42 #include <linux/delay.h>
43 #include <linux/completion.h>
44 #include <linux/slab.h>
50 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
51 static int mcast_debug_level;
53 module_param(mcast_debug_level, int, 0644);
54 MODULE_PARM_DESC(mcast_debug_level,
55 "Enable multicast debug tracing if > 0");
58 static DEFINE_MUTEX(mcast_mutex);
60 struct ipoib_mcast_iter {
61 struct net_device *dev;
63 unsigned long created;
64 unsigned int queuelen;
65 unsigned int complete;
66 unsigned int send_only;
69 static void ipoib_mcast_free(struct ipoib_mcast *mcast)
71 struct net_device *dev = mcast->dev;
74 ipoib_dbg_mcast(netdev_priv(dev), "deleting multicast group %pI6\n",
75 mcast->mcmember.mgid.raw);
77 /* remove all neigh connected to this mcast */
78 ipoib_del_neighs_by_gid(dev, mcast->mcmember.mgid.raw);
81 ipoib_put_ah(mcast->ah);
83 while (!skb_queue_empty(&mcast->pkt_queue)) {
85 dev_kfree_skb_any(skb_dequeue(&mcast->pkt_queue));
88 netif_tx_lock_bh(dev);
89 dev->stats.tx_dropped += tx_dropped;
90 netif_tx_unlock_bh(dev);
95 static struct ipoib_mcast *ipoib_mcast_alloc(struct net_device *dev,
98 struct ipoib_mcast *mcast;
100 mcast = kzalloc(sizeof *mcast, can_sleep ? GFP_KERNEL : GFP_ATOMIC);
105 mcast->created = jiffies;
108 INIT_LIST_HEAD(&mcast->list);
109 INIT_LIST_HEAD(&mcast->neigh_list);
110 skb_queue_head_init(&mcast->pkt_queue);
115 static struct ipoib_mcast *__ipoib_mcast_find(struct net_device *dev, void *mgid)
117 struct ipoib_dev_priv *priv = netdev_priv(dev);
118 struct rb_node *n = priv->multicast_tree.rb_node;
121 struct ipoib_mcast *mcast;
124 mcast = rb_entry(n, struct ipoib_mcast, rb_node);
126 ret = memcmp(mgid, mcast->mcmember.mgid.raw,
127 sizeof (union ib_gid));
139 static int __ipoib_mcast_add(struct net_device *dev, struct ipoib_mcast *mcast)
141 struct ipoib_dev_priv *priv = netdev_priv(dev);
142 struct rb_node **n = &priv->multicast_tree.rb_node, *pn = NULL;
145 struct ipoib_mcast *tmcast;
149 tmcast = rb_entry(pn, struct ipoib_mcast, rb_node);
151 ret = memcmp(mcast->mcmember.mgid.raw, tmcast->mcmember.mgid.raw,
152 sizeof (union ib_gid));
161 rb_link_node(&mcast->rb_node, pn, n);
162 rb_insert_color(&mcast->rb_node, &priv->multicast_tree);
167 static int ipoib_mcast_join_finish(struct ipoib_mcast *mcast,
168 struct ib_sa_mcmember_rec *mcmember)
170 struct net_device *dev = mcast->dev;
171 struct ipoib_dev_priv *priv = netdev_priv(dev);
176 mcast->mcmember = *mcmember;
178 /* Set the multicast MTU and cached Q_Key before we attach if it's
179 * the broadcast group.
181 if (!memcmp(mcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
182 sizeof (union ib_gid))) {
183 spin_lock_irq(&priv->lock);
184 if (!priv->broadcast) {
185 spin_unlock_irq(&priv->lock);
188 priv->mcast_mtu = IPOIB_UD_MTU(ib_mtu_enum_to_int(priv->broadcast->mcmember.mtu));
189 priv->qkey = be32_to_cpu(priv->broadcast->mcmember.qkey);
190 spin_unlock_irq(&priv->lock);
191 priv->tx_wr.wr.ud.remote_qkey = priv->qkey;
194 if (!ipoib_cm_admin_enabled(dev)) {
196 dev_set_mtu(dev, min(priv->mcast_mtu, priv->admin_mtu));
201 if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
202 if (test_and_set_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
203 ipoib_warn(priv, "multicast group %pI6 already attached\n",
204 mcast->mcmember.mgid.raw);
209 ret = ipoib_mcast_attach(dev, be16_to_cpu(mcast->mcmember.mlid),
210 &mcast->mcmember.mgid, set_qkey);
212 ipoib_warn(priv, "couldn't attach QP to multicast group %pI6\n",
213 mcast->mcmember.mgid.raw);
215 clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags);
221 struct ib_ah_attr av = {
222 .dlid = be16_to_cpu(mcast->mcmember.mlid),
223 .port_num = priv->port,
224 .sl = mcast->mcmember.sl,
225 .ah_flags = IB_AH_GRH,
226 .static_rate = mcast->mcmember.rate,
228 .flow_label = be32_to_cpu(mcast->mcmember.flow_label),
229 .hop_limit = mcast->mcmember.hop_limit,
231 .traffic_class = mcast->mcmember.traffic_class
234 av.grh.dgid = mcast->mcmember.mgid;
236 ah = ipoib_create_ah(dev, priv->pd, &av);
238 ipoib_warn(priv, "ib_address_create failed %ld\n",
240 /* use original error */
243 spin_lock_irq(&priv->lock);
245 spin_unlock_irq(&priv->lock);
247 ipoib_dbg_mcast(priv, "MGID %pI6 AV %p, LID 0x%04x, SL %d\n",
248 mcast->mcmember.mgid.raw,
250 be16_to_cpu(mcast->mcmember.mlid),
255 /* actually send any queued packets */
256 netif_tx_lock_bh(dev);
257 while (!skb_queue_empty(&mcast->pkt_queue)) {
258 struct sk_buff *skb = skb_dequeue(&mcast->pkt_queue);
260 netif_tx_unlock_bh(dev);
263 if (dev_queue_xmit(skb))
264 ipoib_warn(priv, "dev_queue_xmit failed to requeue packet\n");
266 netif_tx_lock_bh(dev);
268 netif_tx_unlock_bh(dev);
274 ipoib_mcast_sendonly_join_complete(int status,
275 struct ib_sa_multicast *multicast)
277 struct ipoib_mcast *mcast = multicast->context;
278 struct net_device *dev = mcast->dev;
280 /* We trap for port events ourselves. */
281 if (status == -ENETRESET)
285 status = ipoib_mcast_join_finish(mcast, &multicast->rec);
288 if (mcast->logcount++ < 20)
289 ipoib_dbg_mcast(netdev_priv(dev), "multicast join failed for %pI6, status %d\n",
290 mcast->mcmember.mgid.raw, status);
292 /* Flush out any queued packets */
293 netif_tx_lock_bh(dev);
294 while (!skb_queue_empty(&mcast->pkt_queue)) {
295 ++dev->stats.tx_dropped;
296 dev_kfree_skb_any(skb_dequeue(&mcast->pkt_queue));
298 netif_tx_unlock_bh(dev);
300 /* Clear the busy flag so we try again */
301 status = test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY,
307 static int ipoib_mcast_sendonly_join(struct ipoib_mcast *mcast)
309 struct net_device *dev = mcast->dev;
310 struct ipoib_dev_priv *priv = netdev_priv(dev);
311 struct ib_sa_mcmember_rec rec = {
312 #if 0 /* Some SMs don't support send-only yet */
320 if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
321 ipoib_dbg_mcast(priv, "device shutting down, no multicast joins\n");
325 if (test_and_set_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags)) {
326 ipoib_dbg_mcast(priv, "multicast entry busy, skipping\n");
330 rec.mgid = mcast->mcmember.mgid;
331 rec.port_gid = priv->local_gid;
332 rec.pkey = cpu_to_be16(priv->pkey);
334 mcast->mc = ib_sa_join_multicast(&ipoib_sa_client, priv->ca,
336 IB_SA_MCMEMBER_REC_MGID |
337 IB_SA_MCMEMBER_REC_PORT_GID |
338 IB_SA_MCMEMBER_REC_PKEY |
339 IB_SA_MCMEMBER_REC_JOIN_STATE,
341 ipoib_mcast_sendonly_join_complete,
343 if (IS_ERR(mcast->mc)) {
344 ret = PTR_ERR(mcast->mc);
345 clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
346 ipoib_warn(priv, "ib_sa_join_multicast failed (ret = %d)\n",
349 ipoib_dbg_mcast(priv, "no multicast record for %pI6, starting join\n",
350 mcast->mcmember.mgid.raw);
356 void ipoib_mcast_carrier_on_task(struct work_struct *work)
358 struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
360 struct ib_port_attr attr;
363 * Take rtnl_lock to avoid racing with ipoib_stop() and
364 * turning the carrier back on while a device is being
367 if (ib_query_port(priv->ca, priv->port, &attr) ||
368 attr.state != IB_PORT_ACTIVE) {
369 ipoib_dbg(priv, "Keeping carrier off until IB port is active\n");
374 netif_carrier_on(priv->dev);
378 static int ipoib_mcast_join_complete(int status,
379 struct ib_sa_multicast *multicast)
381 struct ipoib_mcast *mcast = multicast->context;
382 struct net_device *dev = mcast->dev;
383 struct ipoib_dev_priv *priv = netdev_priv(dev);
385 ipoib_dbg_mcast(priv, "join completion for %pI6 (status %d)\n",
386 mcast->mcmember.mgid.raw, status);
388 /* We trap for port events ourselves. */
389 if (status == -ENETRESET) {
395 status = ipoib_mcast_join_finish(mcast, &multicast->rec);
399 mutex_lock(&mcast_mutex);
400 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
401 queue_delayed_work(ipoib_workqueue,
402 &priv->mcast_task, 0);
403 mutex_unlock(&mcast_mutex);
406 * Defer carrier on work to ipoib_workqueue to avoid a
407 * deadlock on rtnl_lock here.
409 if (mcast == priv->broadcast)
410 queue_work(ipoib_workqueue, &priv->carrier_on_task);
416 if (mcast->logcount++ < 20) {
417 if (status == -ETIMEDOUT || status == -EAGAIN) {
418 ipoib_dbg_mcast(priv, "multicast join failed for %pI6, status %d\n",
419 mcast->mcmember.mgid.raw, status);
421 ipoib_warn(priv, "multicast join failed for %pI6, status %d\n",
422 mcast->mcmember.mgid.raw, status);
427 if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
428 mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
430 /* Clear the busy flag so we try again */
431 status = test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
433 mutex_lock(&mcast_mutex);
434 spin_lock_irq(&priv->lock);
435 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
436 queue_delayed_work(ipoib_workqueue, &priv->mcast_task,
437 mcast->backoff * HZ);
438 spin_unlock_irq(&priv->lock);
439 mutex_unlock(&mcast_mutex);
441 complete(&mcast->done);
445 static void ipoib_mcast_join(struct net_device *dev, struct ipoib_mcast *mcast,
448 struct ipoib_dev_priv *priv = netdev_priv(dev);
449 struct ib_sa_mcmember_rec rec = {
452 ib_sa_comp_mask comp_mask;
455 ipoib_dbg_mcast(priv, "joining MGID %pI6\n", mcast->mcmember.mgid.raw);
457 rec.mgid = mcast->mcmember.mgid;
458 rec.port_gid = priv->local_gid;
459 rec.pkey = cpu_to_be16(priv->pkey);
462 IB_SA_MCMEMBER_REC_MGID |
463 IB_SA_MCMEMBER_REC_PORT_GID |
464 IB_SA_MCMEMBER_REC_PKEY |
465 IB_SA_MCMEMBER_REC_JOIN_STATE;
469 IB_SA_MCMEMBER_REC_QKEY |
470 IB_SA_MCMEMBER_REC_MTU_SELECTOR |
471 IB_SA_MCMEMBER_REC_MTU |
472 IB_SA_MCMEMBER_REC_TRAFFIC_CLASS |
473 IB_SA_MCMEMBER_REC_RATE_SELECTOR |
474 IB_SA_MCMEMBER_REC_RATE |
475 IB_SA_MCMEMBER_REC_SL |
476 IB_SA_MCMEMBER_REC_FLOW_LABEL |
477 IB_SA_MCMEMBER_REC_HOP_LIMIT;
479 rec.qkey = priv->broadcast->mcmember.qkey;
480 rec.mtu_selector = IB_SA_EQ;
481 rec.mtu = priv->broadcast->mcmember.mtu;
482 rec.traffic_class = priv->broadcast->mcmember.traffic_class;
483 rec.rate_selector = IB_SA_EQ;
484 rec.rate = priv->broadcast->mcmember.rate;
485 rec.sl = priv->broadcast->mcmember.sl;
486 rec.flow_label = priv->broadcast->mcmember.flow_label;
487 rec.hop_limit = priv->broadcast->mcmember.hop_limit;
490 set_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
491 init_completion(&mcast->done);
492 set_bit(IPOIB_MCAST_JOIN_STARTED, &mcast->flags);
494 mcast->mc = ib_sa_join_multicast(&ipoib_sa_client, priv->ca, priv->port,
495 &rec, comp_mask, GFP_KERNEL,
496 ipoib_mcast_join_complete, mcast);
497 if (IS_ERR(mcast->mc)) {
498 clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
499 complete(&mcast->done);
500 ret = PTR_ERR(mcast->mc);
501 ipoib_warn(priv, "ib_sa_join_multicast failed, status %d\n", ret);
504 if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
505 mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
507 mutex_lock(&mcast_mutex);
508 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
509 queue_delayed_work(ipoib_workqueue,
511 mcast->backoff * HZ);
512 mutex_unlock(&mcast_mutex);
516 void ipoib_mcast_join_task(struct work_struct *work)
518 struct ipoib_dev_priv *priv =
519 container_of(work, struct ipoib_dev_priv, mcast_task.work);
520 struct net_device *dev = priv->dev;
521 struct ib_port_attr port_attr;
523 if (!test_bit(IPOIB_MCAST_RUN, &priv->flags))
526 if (ib_query_port(priv->ca, priv->port, &port_attr) ||
527 port_attr.state != IB_PORT_ACTIVE) {
528 ipoib_dbg(priv, "port state is not ACTIVE (state = %d) suspending join task\n",
532 priv->local_lid = port_attr.lid;
534 if (ib_query_gid(priv->ca, priv->port, 0, &priv->local_gid))
535 ipoib_warn(priv, "ib_query_gid() failed\n");
537 memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));
539 if (!priv->broadcast) {
540 struct ipoib_mcast *broadcast;
542 if (!test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
545 broadcast = ipoib_mcast_alloc(dev, 1);
547 ipoib_warn(priv, "failed to allocate broadcast group\n");
548 mutex_lock(&mcast_mutex);
549 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
550 queue_delayed_work(ipoib_workqueue,
551 &priv->mcast_task, HZ);
552 mutex_unlock(&mcast_mutex);
556 spin_lock_irq(&priv->lock);
557 memcpy(broadcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
558 sizeof (union ib_gid));
559 priv->broadcast = broadcast;
561 __ipoib_mcast_add(dev, priv->broadcast);
562 spin_unlock_irq(&priv->lock);
565 if (!test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
566 if (!test_bit(IPOIB_MCAST_FLAG_BUSY, &priv->broadcast->flags))
567 ipoib_mcast_join(dev, priv->broadcast, 0);
572 struct ipoib_mcast *mcast = NULL;
574 spin_lock_irq(&priv->lock);
575 list_for_each_entry(mcast, &priv->multicast_list, list) {
576 if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)
577 && !test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags)
578 && !test_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
579 /* Found the next unjoined group */
583 spin_unlock_irq(&priv->lock);
585 if (&mcast->list == &priv->multicast_list) {
590 ipoib_mcast_join(dev, mcast, 1);
594 ipoib_dbg_mcast(priv, "successfully joined all multicast groups\n");
596 clear_bit(IPOIB_MCAST_RUN, &priv->flags);
599 int ipoib_mcast_start_thread(struct net_device *dev)
601 struct ipoib_dev_priv *priv = netdev_priv(dev);
603 ipoib_dbg_mcast(priv, "starting multicast thread\n");
605 mutex_lock(&mcast_mutex);
606 if (!test_and_set_bit(IPOIB_MCAST_RUN, &priv->flags))
607 queue_delayed_work(ipoib_workqueue, &priv->mcast_task, 0);
608 mutex_unlock(&mcast_mutex);
613 int ipoib_mcast_stop_thread(struct net_device *dev, int flush)
615 struct ipoib_dev_priv *priv = netdev_priv(dev);
617 ipoib_dbg_mcast(priv, "stopping multicast thread\n");
619 mutex_lock(&mcast_mutex);
620 clear_bit(IPOIB_MCAST_RUN, &priv->flags);
621 cancel_delayed_work(&priv->mcast_task);
622 mutex_unlock(&mcast_mutex);
625 flush_workqueue(ipoib_workqueue);
630 static int ipoib_mcast_leave(struct net_device *dev, struct ipoib_mcast *mcast)
632 struct ipoib_dev_priv *priv = netdev_priv(dev);
635 if (test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
636 ib_sa_free_multicast(mcast->mc);
638 if (test_and_clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
639 ipoib_dbg_mcast(priv, "leaving MGID %pI6\n",
640 mcast->mcmember.mgid.raw);
642 /* Remove ourselves from the multicast group */
643 ret = ib_detach_mcast(priv->qp, &mcast->mcmember.mgid,
644 be16_to_cpu(mcast->mcmember.mlid));
646 ipoib_warn(priv, "ib_detach_mcast failed (result = %d)\n", ret);
652 void ipoib_mcast_send(struct net_device *dev, u8 *daddr, struct sk_buff *skb)
654 struct ipoib_dev_priv *priv = netdev_priv(dev);
655 struct ipoib_mcast *mcast;
657 void *mgid = daddr + 4;
659 spin_lock_irqsave(&priv->lock, flags);
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 %pI6\n",
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) {
713 struct ipoib_neigh *neigh;
715 spin_unlock_irqrestore(&priv->lock, flags);
716 neigh = ipoib_neigh_get(dev, daddr);
717 spin_lock_irqsave(&priv->lock, flags);
719 neigh = ipoib_neigh_alloc(daddr, dev);
721 kref_get(&mcast->ah->ref);
722 neigh->ah = mcast->ah;
723 list_add_tail(&neigh->list, &mcast->neigh_list);
726 spin_unlock_irqrestore(&priv->lock, flags);
727 ipoib_send(dev, skb, mcast->ah, IB_MULTICAST_QPN);
729 ipoib_neigh_put(neigh);
734 spin_unlock_irqrestore(&priv->lock, flags);
737 void ipoib_mcast_dev_flush(struct net_device *dev)
739 struct ipoib_dev_priv *priv = netdev_priv(dev);
740 LIST_HEAD(remove_list);
741 struct ipoib_mcast *mcast, *tmcast;
744 ipoib_dbg_mcast(priv, "flushing multicast list\n");
746 spin_lock_irqsave(&priv->lock, flags);
748 list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
749 list_del(&mcast->list);
750 rb_erase(&mcast->rb_node, &priv->multicast_tree);
751 list_add_tail(&mcast->list, &remove_list);
754 if (priv->broadcast) {
755 rb_erase(&priv->broadcast->rb_node, &priv->multicast_tree);
756 list_add_tail(&priv->broadcast->list, &remove_list);
757 priv->broadcast = NULL;
760 spin_unlock_irqrestore(&priv->lock, flags);
762 /* seperate between the wait to the leave*/
763 list_for_each_entry_safe(mcast, tmcast, &remove_list, list)
764 if (test_bit(IPOIB_MCAST_JOIN_STARTED, &mcast->flags))
765 wait_for_completion(&mcast->done);
767 list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
768 ipoib_mcast_leave(dev, mcast);
769 ipoib_mcast_free(mcast);
773 static int ipoib_mcast_addr_is_valid(const u8 *addr, const u8 *broadcast)
775 /* reserved QPN, prefix, scope */
776 if (memcmp(addr, broadcast, 6))
778 /* signature lower, pkey */
779 if (memcmp(addr + 7, broadcast + 7, 3))
784 void ipoib_mcast_restart_task(struct work_struct *work)
786 struct ipoib_dev_priv *priv =
787 container_of(work, struct ipoib_dev_priv, restart_task);
788 struct net_device *dev = priv->dev;
789 struct netdev_hw_addr *ha;
790 struct ipoib_mcast *mcast, *tmcast;
791 LIST_HEAD(remove_list);
793 struct ib_sa_mcmember_rec rec;
795 ipoib_dbg_mcast(priv, "restarting multicast task\n");
797 ipoib_mcast_stop_thread(dev, 0);
799 local_irq_save(flags);
800 netif_addr_lock(dev);
801 spin_lock(&priv->lock);
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
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);
813 /* Mark all of the entries that are found or don't exist */
814 netdev_for_each_mc_addr(ha, dev) {
817 if (!ipoib_mcast_addr_is_valid(ha->addr, dev->broadcast))
820 memcpy(mgid.raw, ha->addr + 4, sizeof mgid);
822 mcast = __ipoib_mcast_find(dev, &mgid);
823 if (!mcast || test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
824 struct ipoib_mcast *nmcast;
826 /* ignore group which is directly joined by userspace */
827 if (test_bit(IPOIB_FLAG_UMCAST, &priv->flags) &&
828 !ib_sa_get_mcmember_rec(priv->ca, priv->port, &mgid, &rec)) {
829 ipoib_dbg_mcast(priv, "ignoring multicast entry for mgid %pI6\n",
834 /* Not found or send-only group, let's add a new entry */
835 ipoib_dbg_mcast(priv, "adding multicast entry for mgid %pI6\n",
838 nmcast = ipoib_mcast_alloc(dev, 0);
840 ipoib_warn(priv, "unable to allocate memory for multicast structure\n");
844 set_bit(IPOIB_MCAST_FLAG_FOUND, &nmcast->flags);
846 nmcast->mcmember.mgid = mgid;
849 /* Destroy the send only entry */
850 list_move_tail(&mcast->list, &remove_list);
852 rb_replace_node(&mcast->rb_node,
854 &priv->multicast_tree);
856 __ipoib_mcast_add(dev, nmcast);
858 list_add_tail(&nmcast->list, &priv->multicast_list);
862 set_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
865 /* Remove all of the entries don't exist anymore */
866 list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
867 if (!test_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags) &&
868 !test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
869 ipoib_dbg_mcast(priv, "deleting multicast group %pI6\n",
870 mcast->mcmember.mgid.raw);
872 rb_erase(&mcast->rb_node, &priv->multicast_tree);
874 /* Move to the remove list */
875 list_move_tail(&mcast->list, &remove_list);
879 spin_unlock(&priv->lock);
880 netif_addr_unlock(dev);
881 local_irq_restore(flags);
883 /* We have to cancel outside of the spinlock */
884 list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
885 ipoib_mcast_leave(mcast->dev, mcast);
886 ipoib_mcast_free(mcast);
889 if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
890 ipoib_mcast_start_thread(dev);
893 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
895 struct ipoib_mcast_iter *ipoib_mcast_iter_init(struct net_device *dev)
897 struct ipoib_mcast_iter *iter;
899 iter = kmalloc(sizeof *iter, GFP_KERNEL);
904 memset(iter->mgid.raw, 0, 16);
906 if (ipoib_mcast_iter_next(iter)) {
914 int ipoib_mcast_iter_next(struct ipoib_mcast_iter *iter)
916 struct ipoib_dev_priv *priv = netdev_priv(iter->dev);
918 struct ipoib_mcast *mcast;
921 spin_lock_irq(&priv->lock);
923 n = rb_first(&priv->multicast_tree);
926 mcast = rb_entry(n, struct ipoib_mcast, rb_node);
928 if (memcmp(iter->mgid.raw, mcast->mcmember.mgid.raw,
929 sizeof (union ib_gid)) < 0) {
930 iter->mgid = mcast->mcmember.mgid;
931 iter->created = mcast->created;
932 iter->queuelen = skb_queue_len(&mcast->pkt_queue);
933 iter->complete = !!mcast->ah;
934 iter->send_only = !!(mcast->flags & (1 << IPOIB_MCAST_FLAG_SENDONLY));
944 spin_unlock_irq(&priv->lock);
949 void ipoib_mcast_iter_read(struct ipoib_mcast_iter *iter,
951 unsigned long *created,
952 unsigned int *queuelen,
953 unsigned int *complete,
954 unsigned int *send_only)
957 *created = iter->created;
958 *queuelen = iter->queuelen;
959 *complete = iter->complete;
960 *send_only = iter->send_only;
963 #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */