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>
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 DECLARE_MUTEX(mcast_mutex);
60 /* Used for all multicast joins (broadcast, IPv4 mcast and IPv6 mcast) */
62 struct ib_sa_mcmember_rec mcmember;
65 struct rb_node rb_node;
66 struct list_head list;
67 struct completion done;
70 struct ib_sa_query *query;
72 unsigned long created;
73 unsigned long backoff;
76 unsigned char logcount;
78 struct list_head neigh_list;
80 struct sk_buff_head pkt_queue;
82 struct net_device *dev;
85 struct ipoib_mcast_iter {
86 struct net_device *dev;
88 unsigned long created;
89 unsigned int queuelen;
90 unsigned int complete;
91 unsigned int send_only;
94 static void ipoib_mcast_free(struct ipoib_mcast *mcast)
96 struct net_device *dev = mcast->dev;
97 struct ipoib_dev_priv *priv = netdev_priv(dev);
98 struct ipoib_neigh *neigh, *tmp;
101 struct ipoib_ah *ah, *tah;
103 ipoib_dbg_mcast(netdev_priv(dev),
104 "deleting multicast group " IPOIB_GID_FMT "\n",
105 IPOIB_GID_ARG(mcast->mcmember.mgid));
107 spin_lock_irqsave(&priv->lock, flags);
109 list_for_each_entry_safe(neigh, tmp, &mcast->neigh_list, list) {
111 list_add_tail(&neigh->ah->list, &ah_list);
112 *to_ipoib_neigh(neigh->neighbour) = NULL;
113 neigh->neighbour->ops->destructor = NULL;
117 spin_unlock_irqrestore(&priv->lock, flags);
119 list_for_each_entry_safe(ah, tah, &ah_list, list)
123 ipoib_put_ah(mcast->ah);
125 while (!skb_queue_empty(&mcast->pkt_queue))
126 dev_kfree_skb_any(skb_dequeue(&mcast->pkt_queue));
131 static struct ipoib_mcast *ipoib_mcast_alloc(struct net_device *dev,
134 struct ipoib_mcast *mcast;
136 mcast = kzalloc(sizeof *mcast, can_sleep ? GFP_KERNEL : GFP_ATOMIC);
141 mcast->created = jiffies;
144 INIT_LIST_HEAD(&mcast->list);
145 INIT_LIST_HEAD(&mcast->neigh_list);
146 skb_queue_head_init(&mcast->pkt_queue);
151 static struct ipoib_mcast *__ipoib_mcast_find(struct net_device *dev, union ib_gid *mgid)
153 struct ipoib_dev_priv *priv = netdev_priv(dev);
154 struct rb_node *n = priv->multicast_tree.rb_node;
157 struct ipoib_mcast *mcast;
160 mcast = rb_entry(n, struct ipoib_mcast, rb_node);
162 ret = memcmp(mgid->raw, mcast->mcmember.mgid.raw,
163 sizeof (union ib_gid));
175 static int __ipoib_mcast_add(struct net_device *dev, struct ipoib_mcast *mcast)
177 struct ipoib_dev_priv *priv = netdev_priv(dev);
178 struct rb_node **n = &priv->multicast_tree.rb_node, *pn = NULL;
181 struct ipoib_mcast *tmcast;
185 tmcast = rb_entry(pn, struct ipoib_mcast, rb_node);
187 ret = memcmp(mcast->mcmember.mgid.raw, tmcast->mcmember.mgid.raw,
188 sizeof (union ib_gid));
197 rb_link_node(&mcast->rb_node, pn, n);
198 rb_insert_color(&mcast->rb_node, &priv->multicast_tree);
203 static int ipoib_mcast_join_finish(struct ipoib_mcast *mcast,
204 struct ib_sa_mcmember_rec *mcmember)
206 struct net_device *dev = mcast->dev;
207 struct ipoib_dev_priv *priv = netdev_priv(dev);
210 mcast->mcmember = *mcmember;
212 /* Set the cached Q_Key before we attach if it's the broadcast group */
213 if (!memcmp(mcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
214 sizeof (union ib_gid))) {
215 priv->qkey = be32_to_cpu(priv->broadcast->mcmember.qkey);
216 priv->tx_wr.wr.ud.remote_qkey = priv->qkey;
219 if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
220 if (test_and_set_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
221 ipoib_warn(priv, "multicast group " IPOIB_GID_FMT
222 " already attached\n",
223 IPOIB_GID_ARG(mcast->mcmember.mgid));
228 ret = ipoib_mcast_attach(dev, be16_to_cpu(mcast->mcmember.mlid),
229 &mcast->mcmember.mgid);
231 ipoib_warn(priv, "couldn't attach QP to multicast group "
233 IPOIB_GID_ARG(mcast->mcmember.mgid));
235 clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags);
241 struct ib_ah_attr av = {
242 .dlid = be16_to_cpu(mcast->mcmember.mlid),
243 .port_num = priv->port,
244 .sl = mcast->mcmember.sl,
245 .ah_flags = IB_AH_GRH,
247 .flow_label = be32_to_cpu(mcast->mcmember.flow_label),
248 .hop_limit = mcast->mcmember.hop_limit,
250 .traffic_class = mcast->mcmember.traffic_class
253 int path_rate = ib_sa_rate_enum_to_int(mcast->mcmember.rate);
255 av.grh.dgid = mcast->mcmember.mgid;
257 if (path_rate > 0 && priv->local_rate > path_rate)
258 av.static_rate = (priv->local_rate - 1) / path_rate;
260 ipoib_dbg_mcast(priv, "static_rate %d for local port %dX, mcmember %dX\n",
261 av.static_rate, priv->local_rate,
262 ib_sa_rate_enum_to_int(mcast->mcmember.rate));
264 mcast->ah = ipoib_create_ah(dev, priv->pd, &av);
266 ipoib_warn(priv, "ib_address_create failed\n");
268 ipoib_dbg_mcast(priv, "MGID " IPOIB_GID_FMT
269 " AV %p, LID 0x%04x, SL %d\n",
270 IPOIB_GID_ARG(mcast->mcmember.mgid),
272 be16_to_cpu(mcast->mcmember.mlid),
277 /* actually send any queued packets */
278 while (!skb_queue_empty(&mcast->pkt_queue)) {
279 struct sk_buff *skb = skb_dequeue(&mcast->pkt_queue);
283 if (!skb->dst || !skb->dst->neighbour) {
284 /* put pseudoheader back on for next time */
285 skb_push(skb, sizeof (struct ipoib_pseudoheader));
288 if (dev_queue_xmit(skb))
289 ipoib_warn(priv, "dev_queue_xmit failed to requeue packet\n");
296 ipoib_mcast_sendonly_join_complete(int status,
297 struct ib_sa_mcmember_rec *mcmember,
300 struct ipoib_mcast *mcast = mcast_ptr;
301 struct net_device *dev = mcast->dev;
304 ipoib_mcast_join_finish(mcast, mcmember);
306 if (mcast->logcount++ < 20)
307 ipoib_dbg_mcast(netdev_priv(dev), "multicast join failed for "
308 IPOIB_GID_FMT ", status %d\n",
309 IPOIB_GID_ARG(mcast->mcmember.mgid), status);
311 /* Flush out any queued packets */
312 while (!skb_queue_empty(&mcast->pkt_queue))
313 dev_kfree_skb_any(skb_dequeue(&mcast->pkt_queue));
315 /* Clear the busy flag so we try again */
316 clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
319 complete(&mcast->done);
322 static int ipoib_mcast_sendonly_join(struct ipoib_mcast *mcast)
324 struct net_device *dev = mcast->dev;
325 struct ipoib_dev_priv *priv = netdev_priv(dev);
326 struct ib_sa_mcmember_rec rec = {
327 #if 0 /* Some SMs don't support send-only yet */
335 if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
336 ipoib_dbg_mcast(priv, "device shutting down, no multicast joins\n");
340 if (test_and_set_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags)) {
341 ipoib_dbg_mcast(priv, "multicast entry busy, skipping\n");
345 rec.mgid = mcast->mcmember.mgid;
346 rec.port_gid = priv->local_gid;
347 rec.pkey = cpu_to_be16(priv->pkey);
349 init_completion(&mcast->done);
351 ret = ib_sa_mcmember_rec_set(priv->ca, priv->port, &rec,
352 IB_SA_MCMEMBER_REC_MGID |
353 IB_SA_MCMEMBER_REC_PORT_GID |
354 IB_SA_MCMEMBER_REC_PKEY |
355 IB_SA_MCMEMBER_REC_JOIN_STATE,
357 ipoib_mcast_sendonly_join_complete,
358 mcast, &mcast->query);
360 ipoib_warn(priv, "ib_sa_mcmember_rec_set failed (ret = %d)\n",
363 ipoib_dbg_mcast(priv, "no multicast record for " IPOIB_GID_FMT
365 IPOIB_GID_ARG(mcast->mcmember.mgid));
367 mcast->query_id = ret;
373 static void ipoib_mcast_join_complete(int status,
374 struct ib_sa_mcmember_rec *mcmember,
377 struct ipoib_mcast *mcast = mcast_ptr;
378 struct net_device *dev = mcast->dev;
379 struct ipoib_dev_priv *priv = netdev_priv(dev);
381 ipoib_dbg_mcast(priv, "join completion for " IPOIB_GID_FMT
383 IPOIB_GID_ARG(mcast->mcmember.mgid), status);
385 if (!status && !ipoib_mcast_join_finish(mcast, mcmember)) {
388 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
389 queue_work(ipoib_workqueue, &priv->mcast_task);
391 complete(&mcast->done);
395 if (status == -EINTR) {
396 complete(&mcast->done);
400 if (status && mcast->logcount++ < 20) {
401 if (status == -ETIMEDOUT || status == -EINTR) {
402 ipoib_dbg_mcast(priv, "multicast join failed for " IPOIB_GID_FMT
404 IPOIB_GID_ARG(mcast->mcmember.mgid),
407 ipoib_warn(priv, "multicast join failed for "
408 IPOIB_GID_FMT ", status %d\n",
409 IPOIB_GID_ARG(mcast->mcmember.mgid),
415 if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
416 mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
421 if (test_bit(IPOIB_MCAST_RUN, &priv->flags)) {
422 if (status == -ETIMEDOUT)
423 queue_work(ipoib_workqueue, &priv->mcast_task);
425 queue_delayed_work(ipoib_workqueue, &priv->mcast_task,
426 mcast->backoff * HZ);
428 complete(&mcast->done);
434 static void ipoib_mcast_join(struct net_device *dev, struct ipoib_mcast *mcast,
437 struct ipoib_dev_priv *priv = netdev_priv(dev);
438 struct ib_sa_mcmember_rec rec = {
441 ib_sa_comp_mask comp_mask;
444 ipoib_dbg_mcast(priv, "joining MGID " IPOIB_GID_FMT "\n",
445 IPOIB_GID_ARG(mcast->mcmember.mgid));
447 rec.mgid = mcast->mcmember.mgid;
448 rec.port_gid = priv->local_gid;
449 rec.pkey = cpu_to_be16(priv->pkey);
452 IB_SA_MCMEMBER_REC_MGID |
453 IB_SA_MCMEMBER_REC_PORT_GID |
454 IB_SA_MCMEMBER_REC_PKEY |
455 IB_SA_MCMEMBER_REC_JOIN_STATE;
459 IB_SA_MCMEMBER_REC_QKEY |
460 IB_SA_MCMEMBER_REC_SL |
461 IB_SA_MCMEMBER_REC_FLOW_LABEL |
462 IB_SA_MCMEMBER_REC_TRAFFIC_CLASS;
464 rec.qkey = priv->broadcast->mcmember.qkey;
465 rec.sl = priv->broadcast->mcmember.sl;
466 rec.flow_label = priv->broadcast->mcmember.flow_label;
467 rec.traffic_class = priv->broadcast->mcmember.traffic_class;
470 init_completion(&mcast->done);
472 ret = ib_sa_mcmember_rec_set(priv->ca, priv->port, &rec, comp_mask,
473 mcast->backoff * 1000, GFP_ATOMIC,
474 ipoib_mcast_join_complete,
475 mcast, &mcast->query);
478 ipoib_warn(priv, "ib_sa_mcmember_rec_set failed, status %d\n", ret);
481 if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
482 mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
485 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
486 queue_delayed_work(ipoib_workqueue,
488 mcast->backoff * HZ);
491 mcast->query_id = ret;
494 void ipoib_mcast_join_task(void *dev_ptr)
496 struct net_device *dev = dev_ptr;
497 struct ipoib_dev_priv *priv = netdev_priv(dev);
499 if (!test_bit(IPOIB_MCAST_RUN, &priv->flags))
502 if (ib_query_gid(priv->ca, priv->port, 0, &priv->local_gid))
503 ipoib_warn(priv, "ib_gid_entry_get() failed\n");
505 memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));
508 struct ib_port_attr attr;
510 if (!ib_query_port(priv->ca, priv->port, &attr)) {
511 priv->local_lid = attr.lid;
512 priv->local_rate = attr.active_speed *
513 ib_width_enum_to_int(attr.active_width);
515 ipoib_warn(priv, "ib_query_port failed\n");
518 if (!priv->broadcast) {
519 priv->broadcast = ipoib_mcast_alloc(dev, 1);
520 if (!priv->broadcast) {
521 ipoib_warn(priv, "failed to allocate broadcast group\n");
523 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
524 queue_delayed_work(ipoib_workqueue,
525 &priv->mcast_task, HZ);
530 memcpy(priv->broadcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
531 sizeof (union ib_gid));
533 spin_lock_irq(&priv->lock);
534 __ipoib_mcast_add(dev, priv->broadcast);
535 spin_unlock_irq(&priv->lock);
538 if (!test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
539 ipoib_mcast_join(dev, priv->broadcast, 0);
544 struct ipoib_mcast *mcast = NULL;
546 spin_lock_irq(&priv->lock);
547 list_for_each_entry(mcast, &priv->multicast_list, list) {
548 if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)
549 && !test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags)
550 && !test_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
551 /* Found the next unjoined group */
555 spin_unlock_irq(&priv->lock);
557 if (&mcast->list == &priv->multicast_list) {
562 ipoib_mcast_join(dev, mcast, 1);
566 priv->mcast_mtu = ib_mtu_enum_to_int(priv->broadcast->mcmember.mtu) -
568 dev->mtu = min(priv->mcast_mtu, priv->admin_mtu);
570 ipoib_dbg_mcast(priv, "successfully joined all multicast groups\n");
572 clear_bit(IPOIB_MCAST_RUN, &priv->flags);
573 netif_carrier_on(dev);
576 int ipoib_mcast_start_thread(struct net_device *dev)
578 struct ipoib_dev_priv *priv = netdev_priv(dev);
580 ipoib_dbg_mcast(priv, "starting multicast thread\n");
583 if (!test_and_set_bit(IPOIB_MCAST_RUN, &priv->flags))
584 queue_work(ipoib_workqueue, &priv->mcast_task);
590 int ipoib_mcast_stop_thread(struct net_device *dev, int flush)
592 struct ipoib_dev_priv *priv = netdev_priv(dev);
593 struct ipoib_mcast *mcast;
595 ipoib_dbg_mcast(priv, "stopping multicast thread\n");
598 clear_bit(IPOIB_MCAST_RUN, &priv->flags);
599 cancel_delayed_work(&priv->mcast_task);
603 flush_workqueue(ipoib_workqueue);
605 if (priv->broadcast && priv->broadcast->query) {
606 ib_sa_cancel_query(priv->broadcast->query_id, priv->broadcast->query);
607 priv->broadcast->query = NULL;
608 ipoib_dbg_mcast(priv, "waiting for bcast\n");
609 wait_for_completion(&priv->broadcast->done);
612 list_for_each_entry(mcast, &priv->multicast_list, list) {
614 ib_sa_cancel_query(mcast->query_id, mcast->query);
616 ipoib_dbg_mcast(priv, "waiting for MGID " IPOIB_GID_FMT "\n",
617 IPOIB_GID_ARG(mcast->mcmember.mgid));
618 wait_for_completion(&mcast->done);
625 static int ipoib_mcast_leave(struct net_device *dev, struct ipoib_mcast *mcast)
627 struct ipoib_dev_priv *priv = netdev_priv(dev);
628 struct ib_sa_mcmember_rec rec = {
633 if (!test_and_clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags))
636 ipoib_dbg_mcast(priv, "leaving MGID " IPOIB_GID_FMT "\n",
637 IPOIB_GID_ARG(mcast->mcmember.mgid));
639 rec.mgid = mcast->mcmember.mgid;
640 rec.port_gid = priv->local_gid;
641 rec.pkey = cpu_to_be16(priv->pkey);
643 /* Remove ourselves from the multicast group */
644 ret = ipoib_mcast_detach(dev, be16_to_cpu(mcast->mcmember.mlid),
645 &mcast->mcmember.mgid);
647 ipoib_warn(priv, "ipoib_mcast_detach failed (result = %d)\n", ret);
650 * Just make one shot at leaving and don't wait for a reply;
651 * if we fail, too bad.
653 ret = ib_sa_mcmember_rec_delete(priv->ca, priv->port, &rec,
654 IB_SA_MCMEMBER_REC_MGID |
655 IB_SA_MCMEMBER_REC_PORT_GID |
656 IB_SA_MCMEMBER_REC_PKEY |
657 IB_SA_MCMEMBER_REC_JOIN_STATE,
659 mcast, &mcast->query);
661 ipoib_warn(priv, "ib_sa_mcmember_rec_delete failed "
662 "for leave (result = %d)\n", ret);
667 void ipoib_mcast_send(struct net_device *dev, union ib_gid *mgid,
670 struct ipoib_dev_priv *priv = netdev_priv(dev);
671 struct ipoib_mcast *mcast;
674 * We can only be called from ipoib_start_xmit, so we're
675 * inside tx_lock -- no need to save/restore flags.
677 spin_lock(&priv->lock);
679 mcast = __ipoib_mcast_find(dev, mgid);
681 /* Let's create a new send only group now */
682 ipoib_dbg_mcast(priv, "setting up send only multicast group for "
683 IPOIB_GID_FMT "\n", IPOIB_GID_ARG(*mgid));
685 mcast = ipoib_mcast_alloc(dev, 0);
687 ipoib_warn(priv, "unable to allocate memory for "
688 "multicast structure\n");
689 dev_kfree_skb_any(skb);
693 set_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags);
694 mcast->mcmember.mgid = *mgid;
695 __ipoib_mcast_add(dev, mcast);
696 list_add_tail(&mcast->list, &priv->multicast_list);
700 if (skb_queue_len(&mcast->pkt_queue) < IPOIB_MAX_MCAST_QUEUE)
701 skb_queue_tail(&mcast->pkt_queue, skb);
703 dev_kfree_skb_any(skb);
706 ipoib_dbg_mcast(priv, "no address vector, "
707 "but multicast join already started\n");
708 else if (test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags))
709 ipoib_mcast_sendonly_join(mcast);
712 * If lookup completes between here and out:, don't
713 * want to send packet twice.
719 if (mcast && mcast->ah) {
721 skb->dst->neighbour &&
722 !*to_ipoib_neigh(skb->dst->neighbour)) {
723 struct ipoib_neigh *neigh = kmalloc(sizeof *neigh, GFP_ATOMIC);
726 kref_get(&mcast->ah->ref);
727 neigh->ah = mcast->ah;
728 neigh->neighbour = skb->dst->neighbour;
729 *to_ipoib_neigh(skb->dst->neighbour) = neigh;
730 list_add_tail(&neigh->list, &mcast->neigh_list);
734 ipoib_send(dev, skb, mcast->ah, IB_MULTICAST_QPN);
737 spin_unlock(&priv->lock);
740 void ipoib_mcast_dev_flush(struct net_device *dev)
742 struct ipoib_dev_priv *priv = netdev_priv(dev);
743 LIST_HEAD(remove_list);
744 struct ipoib_mcast *mcast, *tmcast, *nmcast;
747 ipoib_dbg_mcast(priv, "flushing multicast list\n");
749 spin_lock_irqsave(&priv->lock, flags);
750 list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
751 nmcast = ipoib_mcast_alloc(dev, 0);
754 mcast->flags & (1 << IPOIB_MCAST_FLAG_SENDONLY);
756 nmcast->mcmember.mgid = mcast->mcmember.mgid;
758 /* Add the new group in before the to-be-destroyed group */
759 list_add_tail(&nmcast->list, &mcast->list);
760 list_del_init(&mcast->list);
762 rb_replace_node(&mcast->rb_node, &nmcast->rb_node,
763 &priv->multicast_tree);
765 list_add_tail(&mcast->list, &remove_list);
767 ipoib_warn(priv, "could not reallocate multicast group "
769 IPOIB_GID_ARG(mcast->mcmember.mgid));
773 if (priv->broadcast) {
774 nmcast = ipoib_mcast_alloc(dev, 0);
776 nmcast->mcmember.mgid = priv->broadcast->mcmember.mgid;
778 rb_replace_node(&priv->broadcast->rb_node,
780 &priv->multicast_tree);
782 list_add_tail(&priv->broadcast->list, &remove_list);
783 priv->broadcast = nmcast;
785 ipoib_warn(priv, "could not reallocate broadcast group "
787 IPOIB_GID_ARG(priv->broadcast->mcmember.mgid));
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 */