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
34 * $Id: ipoib_multicast.c 1362 2004-12-18 15:56:29Z roland $
37 #include <linux/skbuff.h>
38 #include <linux/rtnetlink.h>
41 #include <linux/igmp.h>
42 #include <linux/inetdevice.h>
43 #include <linux/delay.h>
44 #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 DECLARE_MUTEX(mcast_mutex);
58 /* Used for all multicast joins (broadcast, IPv4 mcast and IPv6 mcast) */
60 struct ib_sa_mcmember_rec mcmember;
63 struct rb_node rb_node;
64 struct list_head list;
65 struct completion done;
68 struct ib_sa_query *query;
70 unsigned long created;
71 unsigned long backoff;
74 unsigned char logcount;
76 struct list_head neigh_list;
78 struct sk_buff_head pkt_queue;
80 struct net_device *dev;
83 struct ipoib_mcast_iter {
84 struct net_device *dev;
86 unsigned long created;
87 unsigned int queuelen;
88 unsigned int complete;
89 unsigned int send_only;
92 static void ipoib_mcast_free(struct ipoib_mcast *mcast)
94 struct net_device *dev = mcast->dev;
95 struct ipoib_dev_priv *priv = netdev_priv(dev);
96 struct ipoib_neigh *neigh, *tmp;
99 struct ipoib_ah *ah, *tah;
101 ipoib_dbg_mcast(netdev_priv(dev),
102 "deleting multicast group " IPOIB_GID_FMT "\n",
103 IPOIB_GID_ARG(mcast->mcmember.mgid));
105 spin_lock_irqsave(&priv->lock, flags);
107 list_for_each_entry_safe(neigh, tmp, &mcast->neigh_list, list) {
109 list_add_tail(&neigh->ah->list, &ah_list);
110 *to_ipoib_neigh(neigh->neighbour) = NULL;
111 neigh->neighbour->ops->destructor = NULL;
115 spin_unlock_irqrestore(&priv->lock, flags);
117 list_for_each_entry_safe(ah, tah, &ah_list, list)
121 ipoib_put_ah(mcast->ah);
123 while (!skb_queue_empty(&mcast->pkt_queue))
124 dev_kfree_skb_any(skb_dequeue(&mcast->pkt_queue));
129 static struct ipoib_mcast *ipoib_mcast_alloc(struct net_device *dev,
132 struct ipoib_mcast *mcast;
134 mcast = kzalloc(sizeof *mcast, can_sleep ? GFP_KERNEL : GFP_ATOMIC);
139 mcast->created = jiffies;
143 INIT_LIST_HEAD(&mcast->list);
144 INIT_LIST_HEAD(&mcast->neigh_list);
145 skb_queue_head_init(&mcast->pkt_queue);
153 static struct ipoib_mcast *__ipoib_mcast_find(struct net_device *dev, union ib_gid *mgid)
155 struct ipoib_dev_priv *priv = netdev_priv(dev);
156 struct rb_node *n = priv->multicast_tree.rb_node;
159 struct ipoib_mcast *mcast;
162 mcast = rb_entry(n, struct ipoib_mcast, rb_node);
164 ret = memcmp(mgid->raw, mcast->mcmember.mgid.raw,
165 sizeof (union ib_gid));
177 static int __ipoib_mcast_add(struct net_device *dev, struct ipoib_mcast *mcast)
179 struct ipoib_dev_priv *priv = netdev_priv(dev);
180 struct rb_node **n = &priv->multicast_tree.rb_node, *pn = NULL;
183 struct ipoib_mcast *tmcast;
187 tmcast = rb_entry(pn, struct ipoib_mcast, rb_node);
189 ret = memcmp(mcast->mcmember.mgid.raw, tmcast->mcmember.mgid.raw,
190 sizeof (union ib_gid));
199 rb_link_node(&mcast->rb_node, pn, n);
200 rb_insert_color(&mcast->rb_node, &priv->multicast_tree);
205 static int ipoib_mcast_join_finish(struct ipoib_mcast *mcast,
206 struct ib_sa_mcmember_rec *mcmember)
208 struct net_device *dev = mcast->dev;
209 struct ipoib_dev_priv *priv = netdev_priv(dev);
212 mcast->mcmember = *mcmember;
214 /* Set the cached Q_Key before we attach if it's the broadcast group */
215 if (!memcmp(mcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
216 sizeof (union ib_gid))) {
217 priv->qkey = be32_to_cpu(priv->broadcast->mcmember.qkey);
218 priv->tx_wr.wr.ud.remote_qkey = priv->qkey;
221 if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
222 if (test_and_set_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
223 ipoib_warn(priv, "multicast group " IPOIB_GID_FMT
224 " already attached\n",
225 IPOIB_GID_ARG(mcast->mcmember.mgid));
230 ret = ipoib_mcast_attach(dev, be16_to_cpu(mcast->mcmember.mlid),
231 &mcast->mcmember.mgid);
233 ipoib_warn(priv, "couldn't attach QP to multicast group "
235 IPOIB_GID_ARG(mcast->mcmember.mgid));
237 clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags);
243 struct ib_ah_attr av = {
244 .dlid = be16_to_cpu(mcast->mcmember.mlid),
245 .port_num = priv->port,
246 .sl = mcast->mcmember.sl,
247 .ah_flags = IB_AH_GRH,
249 .flow_label = be32_to_cpu(mcast->mcmember.flow_label),
250 .hop_limit = mcast->mcmember.hop_limit,
252 .traffic_class = mcast->mcmember.traffic_class
255 int path_rate = ib_sa_rate_enum_to_int(mcast->mcmember.rate);
257 av.grh.dgid = mcast->mcmember.mgid;
259 if (path_rate > 0 && priv->local_rate > path_rate)
260 av.static_rate = (priv->local_rate - 1) / path_rate;
262 ipoib_dbg_mcast(priv, "static_rate %d for local port %dX, mcmember %dX\n",
263 av.static_rate, priv->local_rate,
264 ib_sa_rate_enum_to_int(mcast->mcmember.rate));
266 mcast->ah = ipoib_create_ah(dev, priv->pd, &av);
268 ipoib_warn(priv, "ib_address_create failed\n");
270 ipoib_dbg_mcast(priv, "MGID " IPOIB_GID_FMT
271 " AV %p, LID 0x%04x, SL %d\n",
272 IPOIB_GID_ARG(mcast->mcmember.mgid),
274 be16_to_cpu(mcast->mcmember.mlid),
279 /* actually send any queued packets */
280 while (!skb_queue_empty(&mcast->pkt_queue)) {
281 struct sk_buff *skb = skb_dequeue(&mcast->pkt_queue);
285 if (!skb->dst || !skb->dst->neighbour) {
286 /* put pseudoheader back on for next time */
287 skb_push(skb, sizeof (struct ipoib_pseudoheader));
290 if (dev_queue_xmit(skb))
291 ipoib_warn(priv, "dev_queue_xmit failed to requeue packet\n");
298 ipoib_mcast_sendonly_join_complete(int status,
299 struct ib_sa_mcmember_rec *mcmember,
302 struct ipoib_mcast *mcast = mcast_ptr;
303 struct net_device *dev = mcast->dev;
306 ipoib_mcast_join_finish(mcast, mcmember);
308 if (mcast->logcount++ < 20)
309 ipoib_dbg_mcast(netdev_priv(dev), "multicast join failed for "
310 IPOIB_GID_FMT ", status %d\n",
311 IPOIB_GID_ARG(mcast->mcmember.mgid), status);
313 /* Flush out any queued packets */
314 while (!skb_queue_empty(&mcast->pkt_queue))
315 dev_kfree_skb_any(skb_dequeue(&mcast->pkt_queue));
317 /* Clear the busy flag so we try again */
318 clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
321 complete(&mcast->done);
324 static int ipoib_mcast_sendonly_join(struct ipoib_mcast *mcast)
326 struct net_device *dev = mcast->dev;
327 struct ipoib_dev_priv *priv = netdev_priv(dev);
328 struct ib_sa_mcmember_rec rec = {
329 #if 0 /* Some SMs don't support send-only yet */
337 if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
338 ipoib_dbg_mcast(priv, "device shutting down, no multicast joins\n");
342 if (test_and_set_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags)) {
343 ipoib_dbg_mcast(priv, "multicast entry busy, skipping\n");
347 rec.mgid = mcast->mcmember.mgid;
348 rec.port_gid = priv->local_gid;
349 rec.pkey = cpu_to_be16(priv->pkey);
351 init_completion(&mcast->done);
353 ret = ib_sa_mcmember_rec_set(priv->ca, priv->port, &rec,
354 IB_SA_MCMEMBER_REC_MGID |
355 IB_SA_MCMEMBER_REC_PORT_GID |
356 IB_SA_MCMEMBER_REC_PKEY |
357 IB_SA_MCMEMBER_REC_JOIN_STATE,
359 ipoib_mcast_sendonly_join_complete,
360 mcast, &mcast->query);
362 ipoib_warn(priv, "ib_sa_mcmember_rec_set failed (ret = %d)\n",
365 ipoib_dbg_mcast(priv, "no multicast record for " IPOIB_GID_FMT
367 IPOIB_GID_ARG(mcast->mcmember.mgid));
369 mcast->query_id = ret;
375 static void ipoib_mcast_join_complete(int status,
376 struct ib_sa_mcmember_rec *mcmember,
379 struct ipoib_mcast *mcast = mcast_ptr;
380 struct net_device *dev = mcast->dev;
381 struct ipoib_dev_priv *priv = netdev_priv(dev);
383 ipoib_dbg_mcast(priv, "join completion for " IPOIB_GID_FMT
385 IPOIB_GID_ARG(mcast->mcmember.mgid), status);
387 if (!status && !ipoib_mcast_join_finish(mcast, mcmember)) {
390 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
391 queue_work(ipoib_workqueue, &priv->mcast_task);
393 complete(&mcast->done);
397 if (status == -EINTR) {
398 complete(&mcast->done);
402 if (status && mcast->logcount++ < 20) {
403 if (status == -ETIMEDOUT || status == -EINTR) {
404 ipoib_dbg_mcast(priv, "multicast join failed for " IPOIB_GID_FMT
406 IPOIB_GID_ARG(mcast->mcmember.mgid),
409 ipoib_warn(priv, "multicast join failed for "
410 IPOIB_GID_FMT ", status %d\n",
411 IPOIB_GID_ARG(mcast->mcmember.mgid),
417 if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
418 mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
423 if (test_bit(IPOIB_MCAST_RUN, &priv->flags)) {
424 if (status == -ETIMEDOUT)
425 queue_work(ipoib_workqueue, &priv->mcast_task);
427 queue_delayed_work(ipoib_workqueue, &priv->mcast_task,
428 mcast->backoff * HZ);
430 complete(&mcast->done);
436 static void ipoib_mcast_join(struct net_device *dev, struct ipoib_mcast *mcast,
439 struct ipoib_dev_priv *priv = netdev_priv(dev);
440 struct ib_sa_mcmember_rec rec = {
443 ib_sa_comp_mask comp_mask;
446 ipoib_dbg_mcast(priv, "joining MGID " IPOIB_GID_FMT "\n",
447 IPOIB_GID_ARG(mcast->mcmember.mgid));
449 rec.mgid = mcast->mcmember.mgid;
450 rec.port_gid = priv->local_gid;
451 rec.pkey = cpu_to_be16(priv->pkey);
454 IB_SA_MCMEMBER_REC_MGID |
455 IB_SA_MCMEMBER_REC_PORT_GID |
456 IB_SA_MCMEMBER_REC_PKEY |
457 IB_SA_MCMEMBER_REC_JOIN_STATE;
461 IB_SA_MCMEMBER_REC_QKEY |
462 IB_SA_MCMEMBER_REC_SL |
463 IB_SA_MCMEMBER_REC_FLOW_LABEL |
464 IB_SA_MCMEMBER_REC_TRAFFIC_CLASS;
466 rec.qkey = priv->broadcast->mcmember.qkey;
467 rec.sl = priv->broadcast->mcmember.sl;
468 rec.flow_label = priv->broadcast->mcmember.flow_label;
469 rec.traffic_class = priv->broadcast->mcmember.traffic_class;
472 init_completion(&mcast->done);
474 ret = ib_sa_mcmember_rec_set(priv->ca, priv->port, &rec, comp_mask,
475 mcast->backoff * 1000, GFP_ATOMIC,
476 ipoib_mcast_join_complete,
477 mcast, &mcast->query);
480 ipoib_warn(priv, "ib_sa_mcmember_rec_set failed, status %d\n", ret);
483 if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
484 mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
487 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
488 queue_delayed_work(ipoib_workqueue,
490 mcast->backoff * HZ);
493 mcast->query_id = ret;
496 void ipoib_mcast_join_task(void *dev_ptr)
498 struct net_device *dev = dev_ptr;
499 struct ipoib_dev_priv *priv = netdev_priv(dev);
501 if (!test_bit(IPOIB_MCAST_RUN, &priv->flags))
504 if (ib_query_gid(priv->ca, priv->port, 0, &priv->local_gid))
505 ipoib_warn(priv, "ib_gid_entry_get() failed\n");
507 memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));
510 struct ib_port_attr attr;
512 if (!ib_query_port(priv->ca, priv->port, &attr)) {
513 priv->local_lid = attr.lid;
514 priv->local_rate = attr.active_speed *
515 ib_width_enum_to_int(attr.active_width);
517 ipoib_warn(priv, "ib_query_port failed\n");
520 if (!priv->broadcast) {
521 priv->broadcast = ipoib_mcast_alloc(dev, 1);
522 if (!priv->broadcast) {
523 ipoib_warn(priv, "failed to allocate broadcast group\n");
525 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
526 queue_delayed_work(ipoib_workqueue,
527 &priv->mcast_task, HZ);
532 memcpy(priv->broadcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
533 sizeof (union ib_gid));
535 spin_lock_irq(&priv->lock);
536 __ipoib_mcast_add(dev, priv->broadcast);
537 spin_unlock_irq(&priv->lock);
540 if (!test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
541 ipoib_mcast_join(dev, priv->broadcast, 0);
546 struct ipoib_mcast *mcast = NULL;
548 spin_lock_irq(&priv->lock);
549 list_for_each_entry(mcast, &priv->multicast_list, list) {
550 if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)
551 && !test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags)
552 && !test_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
553 /* Found the next unjoined group */
557 spin_unlock_irq(&priv->lock);
559 if (&mcast->list == &priv->multicast_list) {
564 ipoib_mcast_join(dev, mcast, 1);
568 priv->mcast_mtu = ib_mtu_enum_to_int(priv->broadcast->mcmember.mtu) -
570 dev->mtu = min(priv->mcast_mtu, priv->admin_mtu);
572 ipoib_dbg_mcast(priv, "successfully joined all multicast groups\n");
574 clear_bit(IPOIB_MCAST_RUN, &priv->flags);
575 netif_carrier_on(dev);
578 int ipoib_mcast_start_thread(struct net_device *dev)
580 struct ipoib_dev_priv *priv = netdev_priv(dev);
582 ipoib_dbg_mcast(priv, "starting multicast thread\n");
585 if (!test_and_set_bit(IPOIB_MCAST_RUN, &priv->flags))
586 queue_work(ipoib_workqueue, &priv->mcast_task);
592 int ipoib_mcast_stop_thread(struct net_device *dev, int flush)
594 struct ipoib_dev_priv *priv = netdev_priv(dev);
595 struct ipoib_mcast *mcast;
597 ipoib_dbg_mcast(priv, "stopping multicast thread\n");
600 clear_bit(IPOIB_MCAST_RUN, &priv->flags);
601 cancel_delayed_work(&priv->mcast_task);
605 flush_workqueue(ipoib_workqueue);
607 if (priv->broadcast && priv->broadcast->query) {
608 ib_sa_cancel_query(priv->broadcast->query_id, priv->broadcast->query);
609 priv->broadcast->query = NULL;
610 ipoib_dbg_mcast(priv, "waiting for bcast\n");
611 wait_for_completion(&priv->broadcast->done);
614 list_for_each_entry(mcast, &priv->multicast_list, list) {
616 ib_sa_cancel_query(mcast->query_id, mcast->query);
618 ipoib_dbg_mcast(priv, "waiting for MGID " IPOIB_GID_FMT "\n",
619 IPOIB_GID_ARG(mcast->mcmember.mgid));
620 wait_for_completion(&mcast->done);
627 static int ipoib_mcast_leave(struct net_device *dev, struct ipoib_mcast *mcast)
629 struct ipoib_dev_priv *priv = netdev_priv(dev);
630 struct ib_sa_mcmember_rec rec = {
635 if (!test_and_clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags))
638 ipoib_dbg_mcast(priv, "leaving MGID " IPOIB_GID_FMT "\n",
639 IPOIB_GID_ARG(mcast->mcmember.mgid));
641 rec.mgid = mcast->mcmember.mgid;
642 rec.port_gid = priv->local_gid;
643 rec.pkey = cpu_to_be16(priv->pkey);
645 /* Remove ourselves from the multicast group */
646 ret = ipoib_mcast_detach(dev, be16_to_cpu(mcast->mcmember.mlid),
647 &mcast->mcmember.mgid);
649 ipoib_warn(priv, "ipoib_mcast_detach failed (result = %d)\n", ret);
652 * Just make one shot at leaving and don't wait for a reply;
653 * if we fail, too bad.
655 ret = ib_sa_mcmember_rec_delete(priv->ca, priv->port, &rec,
656 IB_SA_MCMEMBER_REC_MGID |
657 IB_SA_MCMEMBER_REC_PORT_GID |
658 IB_SA_MCMEMBER_REC_PKEY |
659 IB_SA_MCMEMBER_REC_JOIN_STATE,
661 mcast, &mcast->query);
663 ipoib_warn(priv, "ib_sa_mcmember_rec_delete failed "
664 "for leave (result = %d)\n", ret);
669 void ipoib_mcast_send(struct net_device *dev, union ib_gid *mgid,
672 struct ipoib_dev_priv *priv = netdev_priv(dev);
673 struct ipoib_mcast *mcast;
676 * We can only be called from ipoib_start_xmit, so we're
677 * inside tx_lock -- no need to save/restore flags.
679 spin_lock(&priv->lock);
681 mcast = __ipoib_mcast_find(dev, mgid);
683 /* Let's create a new send only group now */
684 ipoib_dbg_mcast(priv, "setting up send only multicast group for "
685 IPOIB_GID_FMT "\n", IPOIB_GID_ARG(*mgid));
687 mcast = ipoib_mcast_alloc(dev, 0);
689 ipoib_warn(priv, "unable to allocate memory for "
690 "multicast structure\n");
691 dev_kfree_skb_any(skb);
695 set_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags);
696 mcast->mcmember.mgid = *mgid;
697 __ipoib_mcast_add(dev, mcast);
698 list_add_tail(&mcast->list, &priv->multicast_list);
702 if (skb_queue_len(&mcast->pkt_queue) < IPOIB_MAX_MCAST_QUEUE)
703 skb_queue_tail(&mcast->pkt_queue, skb);
705 dev_kfree_skb_any(skb);
708 ipoib_dbg_mcast(priv, "no address vector, "
709 "but multicast join already started\n");
710 else if (test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags))
711 ipoib_mcast_sendonly_join(mcast);
714 * If lookup completes between here and out:, don't
715 * want to send packet twice.
721 if (mcast && mcast->ah) {
723 skb->dst->neighbour &&
724 !*to_ipoib_neigh(skb->dst->neighbour)) {
725 struct ipoib_neigh *neigh = kmalloc(sizeof *neigh, GFP_ATOMIC);
728 kref_get(&mcast->ah->ref);
729 neigh->ah = mcast->ah;
730 neigh->neighbour = skb->dst->neighbour;
731 *to_ipoib_neigh(skb->dst->neighbour) = neigh;
732 list_add_tail(&neigh->list, &mcast->neigh_list);
736 ipoib_send(dev, skb, mcast->ah, IB_MULTICAST_QPN);
739 spin_unlock(&priv->lock);
742 void ipoib_mcast_dev_flush(struct net_device *dev)
744 struct ipoib_dev_priv *priv = netdev_priv(dev);
745 LIST_HEAD(remove_list);
746 struct ipoib_mcast *mcast, *tmcast, *nmcast;
749 ipoib_dbg_mcast(priv, "flushing multicast list\n");
751 spin_lock_irqsave(&priv->lock, flags);
752 list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
753 nmcast = ipoib_mcast_alloc(dev, 0);
756 mcast->flags & (1 << IPOIB_MCAST_FLAG_SENDONLY);
758 nmcast->mcmember.mgid = mcast->mcmember.mgid;
760 /* Add the new group in before the to-be-destroyed group */
761 list_add_tail(&nmcast->list, &mcast->list);
762 list_del_init(&mcast->list);
764 rb_replace_node(&mcast->rb_node, &nmcast->rb_node,
765 &priv->multicast_tree);
767 list_add_tail(&mcast->list, &remove_list);
769 ipoib_warn(priv, "could not reallocate multicast group "
771 IPOIB_GID_ARG(mcast->mcmember.mgid));
775 if (priv->broadcast) {
776 nmcast = ipoib_mcast_alloc(dev, 0);
778 nmcast->mcmember.mgid = priv->broadcast->mcmember.mgid;
780 rb_replace_node(&priv->broadcast->rb_node,
782 &priv->multicast_tree);
784 list_add_tail(&priv->broadcast->list, &remove_list);
787 priv->broadcast = nmcast;
790 spin_unlock_irqrestore(&priv->lock, flags);
792 list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
793 ipoib_mcast_leave(dev, mcast);
794 ipoib_mcast_free(mcast);
798 void ipoib_mcast_dev_down(struct net_device *dev)
800 struct ipoib_dev_priv *priv = netdev_priv(dev);
803 /* Delete broadcast since it will be recreated */
804 if (priv->broadcast) {
805 ipoib_dbg_mcast(priv, "deleting broadcast group\n");
807 spin_lock_irqsave(&priv->lock, flags);
808 rb_erase(&priv->broadcast->rb_node, &priv->multicast_tree);
809 spin_unlock_irqrestore(&priv->lock, flags);
810 ipoib_mcast_leave(dev, priv->broadcast);
811 ipoib_mcast_free(priv->broadcast);
812 priv->broadcast = NULL;
816 void ipoib_mcast_restart_task(void *dev_ptr)
818 struct net_device *dev = dev_ptr;
819 struct ipoib_dev_priv *priv = netdev_priv(dev);
820 struct dev_mc_list *mclist;
821 struct ipoib_mcast *mcast, *tmcast;
822 LIST_HEAD(remove_list);
825 ipoib_dbg_mcast(priv, "restarting multicast task\n");
827 ipoib_mcast_stop_thread(dev, 0);
829 spin_lock_irqsave(&priv->lock, flags);
832 * Unfortunately, the networking core only gives us a list of all of
833 * the multicast hardware addresses. We need to figure out which ones
834 * are new and which ones have been removed
837 /* Clear out the found flag */
838 list_for_each_entry(mcast, &priv->multicast_list, list)
839 clear_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
841 /* Mark all of the entries that are found or don't exist */
842 for (mclist = dev->mc_list; mclist; mclist = mclist->next) {
845 memcpy(mgid.raw, mclist->dmi_addr + 4, sizeof mgid);
847 /* Add in the P_Key */
848 mgid.raw[4] = (priv->pkey >> 8) & 0xff;
849 mgid.raw[5] = priv->pkey & 0xff;
851 mcast = __ipoib_mcast_find(dev, &mgid);
852 if (!mcast || test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
853 struct ipoib_mcast *nmcast;
855 /* Not found or send-only group, let's add a new entry */
856 ipoib_dbg_mcast(priv, "adding multicast entry for mgid "
857 IPOIB_GID_FMT "\n", IPOIB_GID_ARG(mgid));
859 nmcast = ipoib_mcast_alloc(dev, 0);
861 ipoib_warn(priv, "unable to allocate memory for multicast structure\n");
865 set_bit(IPOIB_MCAST_FLAG_FOUND, &nmcast->flags);
867 nmcast->mcmember.mgid = mgid;
870 /* Destroy the send only entry */
871 list_del(&mcast->list);
872 list_add_tail(&mcast->list, &remove_list);
874 rb_replace_node(&mcast->rb_node,
876 &priv->multicast_tree);
878 __ipoib_mcast_add(dev, nmcast);
880 list_add_tail(&nmcast->list, &priv->multicast_list);
884 set_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
887 /* Remove all of the entries don't exist anymore */
888 list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
889 if (!test_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags) &&
890 !test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
891 ipoib_dbg_mcast(priv, "deleting multicast group " IPOIB_GID_FMT "\n",
892 IPOIB_GID_ARG(mcast->mcmember.mgid));
894 rb_erase(&mcast->rb_node, &priv->multicast_tree);
896 /* Move to the remove list */
897 list_del(&mcast->list);
898 list_add_tail(&mcast->list, &remove_list);
901 spin_unlock_irqrestore(&priv->lock, flags);
903 /* We have to cancel outside of the spinlock */
904 list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
905 ipoib_mcast_leave(mcast->dev, mcast);
906 ipoib_mcast_free(mcast);
909 if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
910 ipoib_mcast_start_thread(dev);
913 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
915 struct ipoib_mcast_iter *ipoib_mcast_iter_init(struct net_device *dev)
917 struct ipoib_mcast_iter *iter;
919 iter = kmalloc(sizeof *iter, GFP_KERNEL);
924 memset(iter->mgid.raw, 0, 16);
926 if (ipoib_mcast_iter_next(iter)) {
934 int ipoib_mcast_iter_next(struct ipoib_mcast_iter *iter)
936 struct ipoib_dev_priv *priv = netdev_priv(iter->dev);
938 struct ipoib_mcast *mcast;
941 spin_lock_irq(&priv->lock);
943 n = rb_first(&priv->multicast_tree);
946 mcast = rb_entry(n, struct ipoib_mcast, rb_node);
948 if (memcmp(iter->mgid.raw, mcast->mcmember.mgid.raw,
949 sizeof (union ib_gid)) < 0) {
950 iter->mgid = mcast->mcmember.mgid;
951 iter->created = mcast->created;
952 iter->queuelen = skb_queue_len(&mcast->pkt_queue);
953 iter->complete = !!mcast->ah;
954 iter->send_only = !!(mcast->flags & (1 << IPOIB_MCAST_FLAG_SENDONLY));
964 spin_unlock_irq(&priv->lock);
969 void ipoib_mcast_iter_read(struct ipoib_mcast_iter *iter,
971 unsigned long *created,
972 unsigned int *queuelen,
973 unsigned int *complete,
974 unsigned int *send_only)
977 *created = iter->created;
978 *queuelen = iter->queuelen;
979 *complete = iter->complete;
980 *send_only = iter->send_only;
983 #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */