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 DEFINE_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;
102 ipoib_dbg_mcast(netdev_priv(dev),
103 "deleting multicast group " IPOIB_GID_FMT "\n",
104 IPOIB_GID_ARG(mcast->mcmember.mgid));
106 spin_lock_irqsave(&priv->lock, flags);
108 list_for_each_entry_safe(neigh, tmp, &mcast->neigh_list, list) {
110 * It's safe to call ipoib_put_ah() inside priv->lock
111 * here, because we know that mcast->ah will always
112 * hold one more reference, so ipoib_put_ah() will
113 * never do more than decrement the ref count.
116 ipoib_put_ah(neigh->ah);
117 *to_ipoib_neigh(neigh->neighbour) = NULL;
118 neigh->neighbour->ops->destructor = NULL;
122 spin_unlock_irqrestore(&priv->lock, flags);
125 ipoib_put_ah(mcast->ah);
127 while (!skb_queue_empty(&mcast->pkt_queue)) {
129 dev_kfree_skb_any(skb_dequeue(&mcast->pkt_queue));
132 spin_lock_irqsave(&priv->tx_lock, flags);
133 priv->stats.tx_dropped += tx_dropped;
134 spin_unlock_irqrestore(&priv->tx_lock, flags);
139 static struct ipoib_mcast *ipoib_mcast_alloc(struct net_device *dev,
142 struct ipoib_mcast *mcast;
144 mcast = kzalloc(sizeof *mcast, can_sleep ? GFP_KERNEL : GFP_ATOMIC);
149 mcast->created = jiffies;
152 INIT_LIST_HEAD(&mcast->list);
153 INIT_LIST_HEAD(&mcast->neigh_list);
154 skb_queue_head_init(&mcast->pkt_queue);
159 static struct ipoib_mcast *__ipoib_mcast_find(struct net_device *dev, union ib_gid *mgid)
161 struct ipoib_dev_priv *priv = netdev_priv(dev);
162 struct rb_node *n = priv->multicast_tree.rb_node;
165 struct ipoib_mcast *mcast;
168 mcast = rb_entry(n, struct ipoib_mcast, rb_node);
170 ret = memcmp(mgid->raw, mcast->mcmember.mgid.raw,
171 sizeof (union ib_gid));
183 static int __ipoib_mcast_add(struct net_device *dev, struct ipoib_mcast *mcast)
185 struct ipoib_dev_priv *priv = netdev_priv(dev);
186 struct rb_node **n = &priv->multicast_tree.rb_node, *pn = NULL;
189 struct ipoib_mcast *tmcast;
193 tmcast = rb_entry(pn, struct ipoib_mcast, rb_node);
195 ret = memcmp(mcast->mcmember.mgid.raw, tmcast->mcmember.mgid.raw,
196 sizeof (union ib_gid));
205 rb_link_node(&mcast->rb_node, pn, n);
206 rb_insert_color(&mcast->rb_node, &priv->multicast_tree);
211 static int ipoib_mcast_join_finish(struct ipoib_mcast *mcast,
212 struct ib_sa_mcmember_rec *mcmember)
214 struct net_device *dev = mcast->dev;
215 struct ipoib_dev_priv *priv = netdev_priv(dev);
218 mcast->mcmember = *mcmember;
220 /* Set the cached Q_Key before we attach if it's the broadcast group */
221 if (!memcmp(mcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
222 sizeof (union ib_gid))) {
223 priv->qkey = be32_to_cpu(priv->broadcast->mcmember.qkey);
224 priv->tx_wr.wr.ud.remote_qkey = priv->qkey;
227 if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
228 if (test_and_set_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
229 ipoib_warn(priv, "multicast group " IPOIB_GID_FMT
230 " already attached\n",
231 IPOIB_GID_ARG(mcast->mcmember.mgid));
236 ret = ipoib_mcast_attach(dev, be16_to_cpu(mcast->mcmember.mlid),
237 &mcast->mcmember.mgid);
239 ipoib_warn(priv, "couldn't attach QP to multicast group "
241 IPOIB_GID_ARG(mcast->mcmember.mgid));
243 clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags);
249 struct ib_ah_attr av = {
250 .dlid = be16_to_cpu(mcast->mcmember.mlid),
251 .port_num = priv->port,
252 .sl = mcast->mcmember.sl,
253 .ah_flags = IB_AH_GRH,
255 .flow_label = be32_to_cpu(mcast->mcmember.flow_label),
256 .hop_limit = mcast->mcmember.hop_limit,
258 .traffic_class = mcast->mcmember.traffic_class
261 int path_rate = ib_sa_rate_enum_to_int(mcast->mcmember.rate);
263 av.grh.dgid = mcast->mcmember.mgid;
265 if (path_rate > 0 && priv->local_rate > path_rate)
266 av.static_rate = (priv->local_rate - 1) / path_rate;
268 ipoib_dbg_mcast(priv, "static_rate %d for local port %dX, mcmember %dX\n",
269 av.static_rate, priv->local_rate,
270 ib_sa_rate_enum_to_int(mcast->mcmember.rate));
272 mcast->ah = ipoib_create_ah(dev, priv->pd, &av);
274 ipoib_warn(priv, "ib_address_create failed\n");
276 ipoib_dbg_mcast(priv, "MGID " IPOIB_GID_FMT
277 " AV %p, LID 0x%04x, SL %d\n",
278 IPOIB_GID_ARG(mcast->mcmember.mgid),
280 be16_to_cpu(mcast->mcmember.mlid),
285 /* actually send any queued packets */
286 spin_lock_irq(&priv->tx_lock);
287 while (!skb_queue_empty(&mcast->pkt_queue)) {
288 struct sk_buff *skb = skb_dequeue(&mcast->pkt_queue);
289 spin_unlock_irq(&priv->tx_lock);
293 if (!skb->dst || !skb->dst->neighbour) {
294 /* put pseudoheader back on for next time */
295 skb_push(skb, sizeof (struct ipoib_pseudoheader));
298 if (dev_queue_xmit(skb))
299 ipoib_warn(priv, "dev_queue_xmit failed to requeue packet\n");
300 spin_lock_irq(&priv->tx_lock);
302 spin_unlock_irq(&priv->tx_lock);
308 ipoib_mcast_sendonly_join_complete(int status,
309 struct ib_sa_mcmember_rec *mcmember,
312 struct ipoib_mcast *mcast = mcast_ptr;
313 struct net_device *dev = mcast->dev;
314 struct ipoib_dev_priv *priv = netdev_priv(dev);
317 ipoib_mcast_join_finish(mcast, mcmember);
319 if (mcast->logcount++ < 20)
320 ipoib_dbg_mcast(netdev_priv(dev), "multicast join failed for "
321 IPOIB_GID_FMT ", status %d\n",
322 IPOIB_GID_ARG(mcast->mcmember.mgid), status);
324 /* Flush out any queued packets */
325 spin_lock_irq(&priv->tx_lock);
326 while (!skb_queue_empty(&mcast->pkt_queue)) {
327 ++priv->stats.tx_dropped;
328 dev_kfree_skb_any(skb_dequeue(&mcast->pkt_queue));
330 spin_unlock_irq(&priv->tx_lock);
332 /* Clear the busy flag so we try again */
333 clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
336 complete(&mcast->done);
339 static int ipoib_mcast_sendonly_join(struct ipoib_mcast *mcast)
341 struct net_device *dev = mcast->dev;
342 struct ipoib_dev_priv *priv = netdev_priv(dev);
343 struct ib_sa_mcmember_rec rec = {
344 #if 0 /* Some SMs don't support send-only yet */
352 if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
353 ipoib_dbg_mcast(priv, "device shutting down, no multicast joins\n");
357 if (test_and_set_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags)) {
358 ipoib_dbg_mcast(priv, "multicast entry busy, skipping\n");
362 rec.mgid = mcast->mcmember.mgid;
363 rec.port_gid = priv->local_gid;
364 rec.pkey = cpu_to_be16(priv->pkey);
366 init_completion(&mcast->done);
368 ret = ib_sa_mcmember_rec_set(priv->ca, priv->port, &rec,
369 IB_SA_MCMEMBER_REC_MGID |
370 IB_SA_MCMEMBER_REC_PORT_GID |
371 IB_SA_MCMEMBER_REC_PKEY |
372 IB_SA_MCMEMBER_REC_JOIN_STATE,
374 ipoib_mcast_sendonly_join_complete,
375 mcast, &mcast->query);
377 ipoib_warn(priv, "ib_sa_mcmember_rec_set failed (ret = %d)\n",
380 ipoib_dbg_mcast(priv, "no multicast record for " IPOIB_GID_FMT
382 IPOIB_GID_ARG(mcast->mcmember.mgid));
384 mcast->query_id = ret;
390 static void ipoib_mcast_join_complete(int status,
391 struct ib_sa_mcmember_rec *mcmember,
394 struct ipoib_mcast *mcast = mcast_ptr;
395 struct net_device *dev = mcast->dev;
396 struct ipoib_dev_priv *priv = netdev_priv(dev);
398 ipoib_dbg_mcast(priv, "join completion for " IPOIB_GID_FMT
400 IPOIB_GID_ARG(mcast->mcmember.mgid), status);
402 if (!status && !ipoib_mcast_join_finish(mcast, mcmember)) {
404 mutex_lock(&mcast_mutex);
405 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
406 queue_work(ipoib_workqueue, &priv->mcast_task);
407 mutex_unlock(&mcast_mutex);
408 complete(&mcast->done);
412 if (status == -EINTR) {
413 complete(&mcast->done);
417 if (status && mcast->logcount++ < 20) {
418 if (status == -ETIMEDOUT || status == -EINTR) {
419 ipoib_dbg_mcast(priv, "multicast join failed for " IPOIB_GID_FMT
421 IPOIB_GID_ARG(mcast->mcmember.mgid),
424 ipoib_warn(priv, "multicast join failed for "
425 IPOIB_GID_FMT ", status %d\n",
426 IPOIB_GID_ARG(mcast->mcmember.mgid),
432 if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
433 mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
437 mutex_lock(&mcast_mutex);
438 if (test_bit(IPOIB_MCAST_RUN, &priv->flags)) {
439 if (status == -ETIMEDOUT)
440 queue_work(ipoib_workqueue, &priv->mcast_task);
442 queue_delayed_work(ipoib_workqueue, &priv->mcast_task,
443 mcast->backoff * HZ);
445 complete(&mcast->done);
446 mutex_unlock(&mcast_mutex);
451 static void ipoib_mcast_join(struct net_device *dev, struct ipoib_mcast *mcast,
454 struct ipoib_dev_priv *priv = netdev_priv(dev);
455 struct ib_sa_mcmember_rec rec = {
458 ib_sa_comp_mask comp_mask;
461 ipoib_dbg_mcast(priv, "joining MGID " IPOIB_GID_FMT "\n",
462 IPOIB_GID_ARG(mcast->mcmember.mgid));
464 rec.mgid = mcast->mcmember.mgid;
465 rec.port_gid = priv->local_gid;
466 rec.pkey = cpu_to_be16(priv->pkey);
469 IB_SA_MCMEMBER_REC_MGID |
470 IB_SA_MCMEMBER_REC_PORT_GID |
471 IB_SA_MCMEMBER_REC_PKEY |
472 IB_SA_MCMEMBER_REC_JOIN_STATE;
476 IB_SA_MCMEMBER_REC_QKEY |
477 IB_SA_MCMEMBER_REC_SL |
478 IB_SA_MCMEMBER_REC_FLOW_LABEL |
479 IB_SA_MCMEMBER_REC_TRAFFIC_CLASS;
481 rec.qkey = priv->broadcast->mcmember.qkey;
482 rec.sl = priv->broadcast->mcmember.sl;
483 rec.flow_label = priv->broadcast->mcmember.flow_label;
484 rec.traffic_class = priv->broadcast->mcmember.traffic_class;
487 init_completion(&mcast->done);
489 ret = ib_sa_mcmember_rec_set(priv->ca, priv->port, &rec, comp_mask,
490 mcast->backoff * 1000, GFP_ATOMIC,
491 ipoib_mcast_join_complete,
492 mcast, &mcast->query);
495 ipoib_warn(priv, "ib_sa_mcmember_rec_set failed, status %d\n", ret);
498 if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
499 mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
501 mutex_lock(&mcast_mutex);
502 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
503 queue_delayed_work(ipoib_workqueue,
505 mcast->backoff * HZ);
506 mutex_unlock(&mcast_mutex);
508 mcast->query_id = ret;
511 void ipoib_mcast_join_task(void *dev_ptr)
513 struct net_device *dev = dev_ptr;
514 struct ipoib_dev_priv *priv = netdev_priv(dev);
516 if (!test_bit(IPOIB_MCAST_RUN, &priv->flags))
519 if (ib_query_gid(priv->ca, priv->port, 0, &priv->local_gid))
520 ipoib_warn(priv, "ib_gid_entry_get() failed\n");
522 memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));
525 struct ib_port_attr attr;
527 if (!ib_query_port(priv->ca, priv->port, &attr)) {
528 priv->local_lid = attr.lid;
529 priv->local_rate = attr.active_speed *
530 ib_width_enum_to_int(attr.active_width);
532 ipoib_warn(priv, "ib_query_port failed\n");
535 if (!priv->broadcast) {
536 struct ipoib_mcast *broadcast;
538 broadcast = ipoib_mcast_alloc(dev, 1);
540 ipoib_warn(priv, "failed to allocate broadcast group\n");
541 mutex_lock(&mcast_mutex);
542 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
543 queue_delayed_work(ipoib_workqueue,
544 &priv->mcast_task, HZ);
545 mutex_unlock(&mcast_mutex);
549 spin_lock_irq(&priv->lock);
550 memcpy(broadcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
551 sizeof (union ib_gid));
552 priv->broadcast = broadcast;
554 __ipoib_mcast_add(dev, priv->broadcast);
555 spin_unlock_irq(&priv->lock);
558 if (!test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
559 ipoib_mcast_join(dev, priv->broadcast, 0);
564 struct ipoib_mcast *mcast = NULL;
566 spin_lock_irq(&priv->lock);
567 list_for_each_entry(mcast, &priv->multicast_list, list) {
568 if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)
569 && !test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags)
570 && !test_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
571 /* Found the next unjoined group */
575 spin_unlock_irq(&priv->lock);
577 if (&mcast->list == &priv->multicast_list) {
582 ipoib_mcast_join(dev, mcast, 1);
586 priv->mcast_mtu = ib_mtu_enum_to_int(priv->broadcast->mcmember.mtu) -
588 dev->mtu = min(priv->mcast_mtu, priv->admin_mtu);
590 ipoib_dbg_mcast(priv, "successfully joined all multicast groups\n");
592 clear_bit(IPOIB_MCAST_RUN, &priv->flags);
593 netif_carrier_on(dev);
596 int ipoib_mcast_start_thread(struct net_device *dev)
598 struct ipoib_dev_priv *priv = netdev_priv(dev);
600 ipoib_dbg_mcast(priv, "starting multicast thread\n");
602 mutex_lock(&mcast_mutex);
603 if (!test_and_set_bit(IPOIB_MCAST_RUN, &priv->flags))
604 queue_work(ipoib_workqueue, &priv->mcast_task);
605 mutex_unlock(&mcast_mutex);
607 spin_lock_irq(&priv->lock);
608 set_bit(IPOIB_MCAST_STARTED, &priv->flags);
609 spin_unlock_irq(&priv->lock);
614 int ipoib_mcast_stop_thread(struct net_device *dev, int flush)
616 struct ipoib_dev_priv *priv = netdev_priv(dev);
617 struct ipoib_mcast *mcast;
619 ipoib_dbg_mcast(priv, "stopping multicast thread\n");
621 spin_lock_irq(&priv->lock);
622 clear_bit(IPOIB_MCAST_STARTED, &priv->flags);
623 spin_unlock_irq(&priv->lock);
625 mutex_lock(&mcast_mutex);
626 clear_bit(IPOIB_MCAST_RUN, &priv->flags);
627 cancel_delayed_work(&priv->mcast_task);
628 mutex_unlock(&mcast_mutex);
631 flush_workqueue(ipoib_workqueue);
633 if (priv->broadcast && priv->broadcast->query) {
634 ib_sa_cancel_query(priv->broadcast->query_id, priv->broadcast->query);
635 priv->broadcast->query = NULL;
636 ipoib_dbg_mcast(priv, "waiting for bcast\n");
637 wait_for_completion(&priv->broadcast->done);
640 list_for_each_entry(mcast, &priv->multicast_list, list) {
642 ib_sa_cancel_query(mcast->query_id, mcast->query);
644 ipoib_dbg_mcast(priv, "waiting for MGID " IPOIB_GID_FMT "\n",
645 IPOIB_GID_ARG(mcast->mcmember.mgid));
646 wait_for_completion(&mcast->done);
653 static int ipoib_mcast_leave(struct net_device *dev, struct ipoib_mcast *mcast)
655 struct ipoib_dev_priv *priv = netdev_priv(dev);
656 struct ib_sa_mcmember_rec rec = {
661 if (!test_and_clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags))
664 ipoib_dbg_mcast(priv, "leaving MGID " IPOIB_GID_FMT "\n",
665 IPOIB_GID_ARG(mcast->mcmember.mgid));
667 rec.mgid = mcast->mcmember.mgid;
668 rec.port_gid = priv->local_gid;
669 rec.pkey = cpu_to_be16(priv->pkey);
671 /* Remove ourselves from the multicast group */
672 ret = ipoib_mcast_detach(dev, be16_to_cpu(mcast->mcmember.mlid),
673 &mcast->mcmember.mgid);
675 ipoib_warn(priv, "ipoib_mcast_detach failed (result = %d)\n", ret);
678 * Just make one shot at leaving and don't wait for a reply;
679 * if we fail, too bad.
681 ret = ib_sa_mcmember_rec_delete(priv->ca, priv->port, &rec,
682 IB_SA_MCMEMBER_REC_MGID |
683 IB_SA_MCMEMBER_REC_PORT_GID |
684 IB_SA_MCMEMBER_REC_PKEY |
685 IB_SA_MCMEMBER_REC_JOIN_STATE,
687 mcast, &mcast->query);
689 ipoib_warn(priv, "ib_sa_mcmember_rec_delete failed "
690 "for leave (result = %d)\n", ret);
695 void ipoib_mcast_send(struct net_device *dev, union ib_gid *mgid,
698 struct ipoib_dev_priv *priv = netdev_priv(dev);
699 struct ipoib_mcast *mcast;
702 * We can only be called from ipoib_start_xmit, so we're
703 * inside tx_lock -- no need to save/restore flags.
705 spin_lock(&priv->lock);
707 if (!test_bit(IPOIB_MCAST_STARTED, &priv->flags) ||
709 !test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
710 ++priv->stats.tx_dropped;
711 dev_kfree_skb_any(skb);
715 mcast = __ipoib_mcast_find(dev, mgid);
717 /* Let's create a new send only group now */
718 ipoib_dbg_mcast(priv, "setting up send only multicast group for "
719 IPOIB_GID_FMT "\n", IPOIB_GID_ARG(*mgid));
721 mcast = ipoib_mcast_alloc(dev, 0);
723 ipoib_warn(priv, "unable to allocate memory for "
724 "multicast structure\n");
725 ++priv->stats.tx_dropped;
726 dev_kfree_skb_any(skb);
730 set_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags);
731 mcast->mcmember.mgid = *mgid;
732 __ipoib_mcast_add(dev, mcast);
733 list_add_tail(&mcast->list, &priv->multicast_list);
737 if (skb_queue_len(&mcast->pkt_queue) < IPOIB_MAX_MCAST_QUEUE)
738 skb_queue_tail(&mcast->pkt_queue, skb);
740 ++priv->stats.tx_dropped;
741 dev_kfree_skb_any(skb);
745 ipoib_dbg_mcast(priv, "no address vector, "
746 "but multicast join already started\n");
747 else if (test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags))
748 ipoib_mcast_sendonly_join(mcast);
751 * If lookup completes between here and out:, don't
752 * want to send packet twice.
758 if (mcast && mcast->ah) {
760 skb->dst->neighbour &&
761 !*to_ipoib_neigh(skb->dst->neighbour)) {
762 struct ipoib_neigh *neigh = kmalloc(sizeof *neigh, GFP_ATOMIC);
765 kref_get(&mcast->ah->ref);
766 neigh->ah = mcast->ah;
767 neigh->neighbour = skb->dst->neighbour;
768 *to_ipoib_neigh(skb->dst->neighbour) = neigh;
769 list_add_tail(&neigh->list, &mcast->neigh_list);
773 ipoib_send(dev, skb, mcast->ah, IB_MULTICAST_QPN);
777 spin_unlock(&priv->lock);
780 void ipoib_mcast_dev_flush(struct net_device *dev)
782 struct ipoib_dev_priv *priv = netdev_priv(dev);
783 LIST_HEAD(remove_list);
784 struct ipoib_mcast *mcast, *tmcast;
787 ipoib_dbg_mcast(priv, "flushing multicast list\n");
789 spin_lock_irqsave(&priv->lock, flags);
791 list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
792 list_del(&mcast->list);
793 rb_erase(&mcast->rb_node, &priv->multicast_tree);
794 list_add_tail(&mcast->list, &remove_list);
797 if (priv->broadcast) {
798 rb_erase(&priv->broadcast->rb_node, &priv->multicast_tree);
799 list_add_tail(&priv->broadcast->list, &remove_list);
800 priv->broadcast = NULL;
803 spin_unlock_irqrestore(&priv->lock, flags);
805 list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
806 ipoib_mcast_leave(dev, mcast);
807 ipoib_mcast_free(mcast);
811 void ipoib_mcast_restart_task(void *dev_ptr)
813 struct net_device *dev = dev_ptr;
814 struct ipoib_dev_priv *priv = netdev_priv(dev);
815 struct dev_mc_list *mclist;
816 struct ipoib_mcast *mcast, *tmcast;
817 LIST_HEAD(remove_list);
820 ipoib_dbg_mcast(priv, "restarting multicast task\n");
822 ipoib_mcast_stop_thread(dev, 0);
824 spin_lock_irqsave(&dev->xmit_lock, flags);
825 spin_lock(&priv->lock);
828 * Unfortunately, the networking core only gives us a list of all of
829 * the multicast hardware addresses. We need to figure out which ones
830 * are new and which ones have been removed
833 /* Clear out the found flag */
834 list_for_each_entry(mcast, &priv->multicast_list, list)
835 clear_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
837 /* Mark all of the entries that are found or don't exist */
838 for (mclist = dev->mc_list; mclist; mclist = mclist->next) {
841 memcpy(mgid.raw, mclist->dmi_addr + 4, sizeof mgid);
843 /* Add in the P_Key */
844 mgid.raw[4] = (priv->pkey >> 8) & 0xff;
845 mgid.raw[5] = priv->pkey & 0xff;
847 mcast = __ipoib_mcast_find(dev, &mgid);
848 if (!mcast || test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
849 struct ipoib_mcast *nmcast;
851 /* Not found or send-only group, let's add a new entry */
852 ipoib_dbg_mcast(priv, "adding multicast entry for mgid "
853 IPOIB_GID_FMT "\n", IPOIB_GID_ARG(mgid));
855 nmcast = ipoib_mcast_alloc(dev, 0);
857 ipoib_warn(priv, "unable to allocate memory for multicast structure\n");
861 set_bit(IPOIB_MCAST_FLAG_FOUND, &nmcast->flags);
863 nmcast->mcmember.mgid = mgid;
866 /* Destroy the send only entry */
867 list_del(&mcast->list);
868 list_add_tail(&mcast->list, &remove_list);
870 rb_replace_node(&mcast->rb_node,
872 &priv->multicast_tree);
874 __ipoib_mcast_add(dev, nmcast);
876 list_add_tail(&nmcast->list, &priv->multicast_list);
880 set_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
883 /* Remove all of the entries don't exist anymore */
884 list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
885 if (!test_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags) &&
886 !test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
887 ipoib_dbg_mcast(priv, "deleting multicast group " IPOIB_GID_FMT "\n",
888 IPOIB_GID_ARG(mcast->mcmember.mgid));
890 rb_erase(&mcast->rb_node, &priv->multicast_tree);
892 /* Move to the remove list */
893 list_del(&mcast->list);
894 list_add_tail(&mcast->list, &remove_list);
898 spin_unlock(&priv->lock);
899 spin_unlock_irqrestore(&dev->xmit_lock, flags);
901 /* We have to cancel outside of the spinlock */
902 list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
903 ipoib_mcast_leave(mcast->dev, mcast);
904 ipoib_mcast_free(mcast);
907 if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
908 ipoib_mcast_start_thread(dev);
911 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
913 struct ipoib_mcast_iter *ipoib_mcast_iter_init(struct net_device *dev)
915 struct ipoib_mcast_iter *iter;
917 iter = kmalloc(sizeof *iter, GFP_KERNEL);
922 memset(iter->mgid.raw, 0, 16);
924 if (ipoib_mcast_iter_next(iter)) {
932 int ipoib_mcast_iter_next(struct ipoib_mcast_iter *iter)
934 struct ipoib_dev_priv *priv = netdev_priv(iter->dev);
936 struct ipoib_mcast *mcast;
939 spin_lock_irq(&priv->lock);
941 n = rb_first(&priv->multicast_tree);
944 mcast = rb_entry(n, struct ipoib_mcast, rb_node);
946 if (memcmp(iter->mgid.raw, mcast->mcmember.mgid.raw,
947 sizeof (union ib_gid)) < 0) {
948 iter->mgid = mcast->mcmember.mgid;
949 iter->created = mcast->created;
950 iter->queuelen = skb_queue_len(&mcast->pkt_queue);
951 iter->complete = !!mcast->ah;
952 iter->send_only = !!(mcast->flags & (1 << IPOIB_MCAST_FLAG_SENDONLY));
962 spin_unlock_irq(&priv->lock);
967 void ipoib_mcast_iter_read(struct ipoib_mcast_iter *iter,
969 unsigned long *created,
970 unsigned int *queuelen,
971 unsigned int *complete,
972 unsigned int *send_only)
975 *created = iter->created;
976 *queuelen = iter->queuelen;
977 *complete = iter->complete;
978 *send_only = iter->send_only;
981 #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */