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;
63 struct ib_sa_multicast *mc;
66 struct rb_node rb_node;
67 struct list_head list;
69 unsigned long created;
70 unsigned long backoff;
73 unsigned char logcount;
75 struct list_head neigh_list;
77 struct sk_buff_head pkt_queue;
79 struct net_device *dev;
82 struct ipoib_mcast_iter {
83 struct net_device *dev;
85 unsigned long created;
86 unsigned int queuelen;
87 unsigned int complete;
88 unsigned int send_only;
91 static void ipoib_mcast_free(struct ipoib_mcast *mcast)
93 struct net_device *dev = mcast->dev;
94 struct ipoib_dev_priv *priv = netdev_priv(dev);
95 struct ipoib_neigh *neigh, *tmp;
99 ipoib_dbg_mcast(netdev_priv(dev),
100 "deleting multicast group " IPOIB_GID_FMT "\n",
101 IPOIB_GID_ARG(mcast->mcmember.mgid));
103 spin_lock_irqsave(&priv->lock, flags);
105 list_for_each_entry_safe(neigh, tmp, &mcast->neigh_list, list) {
107 * It's safe to call ipoib_put_ah() inside priv->lock
108 * here, because we know that mcast->ah will always
109 * hold one more reference, so ipoib_put_ah() will
110 * never do more than decrement the ref count.
113 ipoib_put_ah(neigh->ah);
114 ipoib_neigh_free(dev, neigh);
117 spin_unlock_irqrestore(&priv->lock, flags);
120 ipoib_put_ah(mcast->ah);
122 while (!skb_queue_empty(&mcast->pkt_queue)) {
124 dev_kfree_skb_any(skb_dequeue(&mcast->pkt_queue));
127 spin_lock_irqsave(&priv->tx_lock, flags);
128 priv->stats.tx_dropped += tx_dropped;
129 spin_unlock_irqrestore(&priv->tx_lock, flags);
134 static struct ipoib_mcast *ipoib_mcast_alloc(struct net_device *dev,
137 struct ipoib_mcast *mcast;
139 mcast = kzalloc(sizeof *mcast, can_sleep ? GFP_KERNEL : GFP_ATOMIC);
144 mcast->created = jiffies;
147 INIT_LIST_HEAD(&mcast->list);
148 INIT_LIST_HEAD(&mcast->neigh_list);
149 skb_queue_head_init(&mcast->pkt_queue);
154 static struct ipoib_mcast *__ipoib_mcast_find(struct net_device *dev, void *mgid)
156 struct ipoib_dev_priv *priv = netdev_priv(dev);
157 struct rb_node *n = priv->multicast_tree.rb_node;
160 struct ipoib_mcast *mcast;
163 mcast = rb_entry(n, struct ipoib_mcast, rb_node);
165 ret = memcmp(mgid, mcast->mcmember.mgid.raw,
166 sizeof (union ib_gid));
178 static int __ipoib_mcast_add(struct net_device *dev, struct ipoib_mcast *mcast)
180 struct ipoib_dev_priv *priv = netdev_priv(dev);
181 struct rb_node **n = &priv->multicast_tree.rb_node, *pn = NULL;
184 struct ipoib_mcast *tmcast;
188 tmcast = rb_entry(pn, struct ipoib_mcast, rb_node);
190 ret = memcmp(mcast->mcmember.mgid.raw, tmcast->mcmember.mgid.raw,
191 sizeof (union ib_gid));
200 rb_link_node(&mcast->rb_node, pn, n);
201 rb_insert_color(&mcast->rb_node, &priv->multicast_tree);
206 static int ipoib_mcast_join_finish(struct ipoib_mcast *mcast,
207 struct ib_sa_mcmember_rec *mcmember)
209 struct net_device *dev = mcast->dev;
210 struct ipoib_dev_priv *priv = netdev_priv(dev);
214 mcast->mcmember = *mcmember;
216 /* Set the cached Q_Key before we attach if it's the broadcast group */
217 if (!memcmp(mcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
218 sizeof (union ib_gid))) {
219 priv->qkey = be32_to_cpu(priv->broadcast->mcmember.qkey);
220 priv->tx_wr.wr.ud.remote_qkey = priv->qkey;
223 if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
224 if (test_and_set_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
225 ipoib_warn(priv, "multicast group " IPOIB_GID_FMT
226 " already attached\n",
227 IPOIB_GID_ARG(mcast->mcmember.mgid));
232 ret = ipoib_mcast_attach(dev, be16_to_cpu(mcast->mcmember.mlid),
233 &mcast->mcmember.mgid);
235 ipoib_warn(priv, "couldn't attach QP to multicast group "
237 IPOIB_GID_ARG(mcast->mcmember.mgid));
239 clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags);
245 struct ib_ah_attr av = {
246 .dlid = be16_to_cpu(mcast->mcmember.mlid),
247 .port_num = priv->port,
248 .sl = mcast->mcmember.sl,
249 .ah_flags = IB_AH_GRH,
250 .static_rate = mcast->mcmember.rate,
252 .flow_label = be32_to_cpu(mcast->mcmember.flow_label),
253 .hop_limit = mcast->mcmember.hop_limit,
255 .traffic_class = mcast->mcmember.traffic_class
258 av.grh.dgid = mcast->mcmember.mgid;
260 ah = ipoib_create_ah(dev, priv->pd, &av);
262 ipoib_warn(priv, "ib_address_create failed\n");
264 spin_lock_irq(&priv->lock);
266 spin_unlock_irq(&priv->lock);
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 spin_lock_irq(&priv->tx_lock);
279 while (!skb_queue_empty(&mcast->pkt_queue)) {
280 struct sk_buff *skb = skb_dequeue(&mcast->pkt_queue);
281 spin_unlock_irq(&priv->tx_lock);
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");
292 spin_lock_irq(&priv->tx_lock);
294 spin_unlock_irq(&priv->tx_lock);
300 ipoib_mcast_sendonly_join_complete(int status,
301 struct ib_sa_multicast *multicast)
303 struct ipoib_mcast *mcast = multicast->context;
304 struct net_device *dev = mcast->dev;
305 struct ipoib_dev_priv *priv = netdev_priv(dev);
307 /* We trap for port events ourselves. */
308 if (status == -ENETRESET)
312 status = ipoib_mcast_join_finish(mcast, &multicast->rec);
315 if (mcast->logcount++ < 20)
316 ipoib_dbg_mcast(netdev_priv(dev), "multicast join failed for "
317 IPOIB_GID_FMT ", status %d\n",
318 IPOIB_GID_ARG(mcast->mcmember.mgid), status);
320 /* Flush out any queued packets */
321 spin_lock_irq(&priv->tx_lock);
322 while (!skb_queue_empty(&mcast->pkt_queue)) {
323 ++priv->stats.tx_dropped;
324 dev_kfree_skb_any(skb_dequeue(&mcast->pkt_queue));
326 spin_unlock_irq(&priv->tx_lock);
328 /* Clear the busy flag so we try again */
329 status = test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY,
335 static int ipoib_mcast_sendonly_join(struct ipoib_mcast *mcast)
337 struct net_device *dev = mcast->dev;
338 struct ipoib_dev_priv *priv = netdev_priv(dev);
339 struct ib_sa_mcmember_rec rec = {
340 #if 0 /* Some SMs don't support send-only yet */
348 if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
349 ipoib_dbg_mcast(priv, "device shutting down, no multicast joins\n");
353 if (test_and_set_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags)) {
354 ipoib_dbg_mcast(priv, "multicast entry busy, skipping\n");
358 rec.mgid = mcast->mcmember.mgid;
359 rec.port_gid = priv->local_gid;
360 rec.pkey = cpu_to_be16(priv->pkey);
362 mcast->mc = ib_sa_join_multicast(&ipoib_sa_client, priv->ca,
364 IB_SA_MCMEMBER_REC_MGID |
365 IB_SA_MCMEMBER_REC_PORT_GID |
366 IB_SA_MCMEMBER_REC_PKEY |
367 IB_SA_MCMEMBER_REC_JOIN_STATE,
369 ipoib_mcast_sendonly_join_complete,
371 if (IS_ERR(mcast->mc)) {
372 ret = PTR_ERR(mcast->mc);
373 clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
374 ipoib_warn(priv, "ib_sa_join_multicast failed (ret = %d)\n",
377 ipoib_dbg_mcast(priv, "no multicast record for " IPOIB_GID_FMT
379 IPOIB_GID_ARG(mcast->mcmember.mgid));
385 static int ipoib_mcast_join_complete(int status,
386 struct ib_sa_multicast *multicast)
388 struct ipoib_mcast *mcast = multicast->context;
389 struct net_device *dev = mcast->dev;
390 struct ipoib_dev_priv *priv = netdev_priv(dev);
392 ipoib_dbg_mcast(priv, "join completion for " IPOIB_GID_FMT
394 IPOIB_GID_ARG(mcast->mcmember.mgid), status);
396 /* We trap for port events ourselves. */
397 if (status == -ENETRESET)
401 status = ipoib_mcast_join_finish(mcast, &multicast->rec);
405 mutex_lock(&mcast_mutex);
406 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
407 queue_delayed_work(ipoib_workqueue,
408 &priv->mcast_task, 0);
409 mutex_unlock(&mcast_mutex);
413 if (mcast->logcount++ < 20) {
414 if (status == -ETIMEDOUT) {
415 ipoib_dbg_mcast(priv, "multicast join failed for " IPOIB_GID_FMT
417 IPOIB_GID_ARG(mcast->mcmember.mgid),
420 ipoib_warn(priv, "multicast join failed for "
421 IPOIB_GID_FMT ", status %d\n",
422 IPOIB_GID_ARG(mcast->mcmember.mgid),
428 if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
429 mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
431 /* Clear the busy flag so we try again */
432 status = test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
434 mutex_lock(&mcast_mutex);
435 spin_lock_irq(&priv->lock);
436 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
437 queue_delayed_work(ipoib_workqueue, &priv->mcast_task,
438 mcast->backoff * HZ);
439 spin_unlock_irq(&priv->lock);
440 mutex_unlock(&mcast_mutex);
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 " IPOIB_GID_FMT "\n",
456 IPOIB_GID_ARG(mcast->mcmember.mgid));
458 rec.mgid = mcast->mcmember.mgid;
459 rec.port_gid = priv->local_gid;
460 rec.pkey = cpu_to_be16(priv->pkey);
463 IB_SA_MCMEMBER_REC_MGID |
464 IB_SA_MCMEMBER_REC_PORT_GID |
465 IB_SA_MCMEMBER_REC_PKEY |
466 IB_SA_MCMEMBER_REC_JOIN_STATE;
470 IB_SA_MCMEMBER_REC_QKEY |
471 IB_SA_MCMEMBER_REC_MTU_SELECTOR |
472 IB_SA_MCMEMBER_REC_MTU |
473 IB_SA_MCMEMBER_REC_TRAFFIC_CLASS |
474 IB_SA_MCMEMBER_REC_RATE_SELECTOR |
475 IB_SA_MCMEMBER_REC_RATE |
476 IB_SA_MCMEMBER_REC_SL |
477 IB_SA_MCMEMBER_REC_FLOW_LABEL |
478 IB_SA_MCMEMBER_REC_HOP_LIMIT;
480 rec.qkey = priv->broadcast->mcmember.qkey;
481 rec.mtu_selector = IB_SA_EQ;
482 rec.mtu = priv->broadcast->mcmember.mtu;
483 rec.traffic_class = priv->broadcast->mcmember.traffic_class;
484 rec.rate_selector = IB_SA_EQ;
485 rec.rate = priv->broadcast->mcmember.rate;
486 rec.sl = priv->broadcast->mcmember.sl;
487 rec.flow_label = priv->broadcast->mcmember.flow_label;
488 rec.hop_limit = priv->broadcast->mcmember.hop_limit;
491 set_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
492 mcast->mc = ib_sa_join_multicast(&ipoib_sa_client, priv->ca, priv->port,
493 &rec, comp_mask, GFP_KERNEL,
494 ipoib_mcast_join_complete, mcast);
495 if (IS_ERR(mcast->mc)) {
496 clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
497 ret = PTR_ERR(mcast->mc);
498 ipoib_warn(priv, "ib_sa_join_multicast failed, status %d\n", ret);
501 if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
502 mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
504 mutex_lock(&mcast_mutex);
505 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
506 queue_delayed_work(ipoib_workqueue,
508 mcast->backoff * HZ);
509 mutex_unlock(&mcast_mutex);
513 void ipoib_mcast_join_task(struct work_struct *work)
515 struct ipoib_dev_priv *priv =
516 container_of(work, struct ipoib_dev_priv, mcast_task.work);
517 struct net_device *dev = priv->dev;
519 if (!test_bit(IPOIB_MCAST_RUN, &priv->flags))
522 if (ib_query_gid(priv->ca, priv->port, 0, &priv->local_gid))
523 ipoib_warn(priv, "ib_gid_entry_get() failed\n");
525 memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));
528 struct ib_port_attr attr;
530 if (!ib_query_port(priv->ca, priv->port, &attr)) {
531 priv->local_lid = attr.lid;
532 priv->local_rate = attr.active_speed *
533 ib_width_enum_to_int(attr.active_width);
535 ipoib_warn(priv, "ib_query_port failed\n");
538 if (!priv->broadcast) {
539 struct ipoib_mcast *broadcast;
541 broadcast = ipoib_mcast_alloc(dev, 1);
543 ipoib_warn(priv, "failed to allocate broadcast group\n");
544 mutex_lock(&mcast_mutex);
545 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
546 queue_delayed_work(ipoib_workqueue,
547 &priv->mcast_task, HZ);
548 mutex_unlock(&mcast_mutex);
552 spin_lock_irq(&priv->lock);
553 memcpy(broadcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
554 sizeof (union ib_gid));
555 priv->broadcast = broadcast;
557 __ipoib_mcast_add(dev, priv->broadcast);
558 spin_unlock_irq(&priv->lock);
561 if (!test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
562 if (!test_bit(IPOIB_MCAST_FLAG_BUSY, &priv->broadcast->flags))
563 ipoib_mcast_join(dev, priv->broadcast, 0);
568 struct ipoib_mcast *mcast = NULL;
570 spin_lock_irq(&priv->lock);
571 list_for_each_entry(mcast, &priv->multicast_list, list) {
572 if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)
573 && !test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags)
574 && !test_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
575 /* Found the next unjoined group */
579 spin_unlock_irq(&priv->lock);
581 if (&mcast->list == &priv->multicast_list) {
586 ipoib_mcast_join(dev, mcast, 1);
590 priv->mcast_mtu = ib_mtu_enum_to_int(priv->broadcast->mcmember.mtu) -
593 if (!ipoib_cm_admin_enabled(dev))
594 dev->mtu = min(priv->mcast_mtu, priv->admin_mtu);
596 ipoib_dbg_mcast(priv, "successfully joined all multicast groups\n");
598 clear_bit(IPOIB_MCAST_RUN, &priv->flags);
599 netif_carrier_on(dev);
602 int ipoib_mcast_start_thread(struct net_device *dev)
604 struct ipoib_dev_priv *priv = netdev_priv(dev);
606 ipoib_dbg_mcast(priv, "starting multicast thread\n");
608 mutex_lock(&mcast_mutex);
609 if (!test_and_set_bit(IPOIB_MCAST_RUN, &priv->flags))
610 queue_delayed_work(ipoib_workqueue, &priv->mcast_task, 0);
611 mutex_unlock(&mcast_mutex);
613 spin_lock_irq(&priv->lock);
614 set_bit(IPOIB_MCAST_STARTED, &priv->flags);
615 spin_unlock_irq(&priv->lock);
620 int ipoib_mcast_stop_thread(struct net_device *dev, int flush)
622 struct ipoib_dev_priv *priv = netdev_priv(dev);
624 ipoib_dbg_mcast(priv, "stopping multicast thread\n");
626 spin_lock_irq(&priv->lock);
627 clear_bit(IPOIB_MCAST_STARTED, &priv->flags);
628 spin_unlock_irq(&priv->lock);
630 mutex_lock(&mcast_mutex);
631 clear_bit(IPOIB_MCAST_RUN, &priv->flags);
632 cancel_delayed_work(&priv->mcast_task);
633 mutex_unlock(&mcast_mutex);
636 flush_workqueue(ipoib_workqueue);
641 static int ipoib_mcast_leave(struct net_device *dev, struct ipoib_mcast *mcast)
643 struct ipoib_dev_priv *priv = netdev_priv(dev);
646 if (test_and_clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
647 ipoib_dbg_mcast(priv, "leaving MGID " IPOIB_GID_FMT "\n",
648 IPOIB_GID_ARG(mcast->mcmember.mgid));
650 /* Remove ourselves from the multicast group */
651 ret = ipoib_mcast_detach(dev, be16_to_cpu(mcast->mcmember.mlid),
652 &mcast->mcmember.mgid);
654 ipoib_warn(priv, "ipoib_mcast_detach failed (result = %d)\n", ret);
657 if (test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
658 ib_sa_free_multicast(mcast->mc);
663 void ipoib_mcast_send(struct net_device *dev, void *mgid, struct sk_buff *skb)
665 struct ipoib_dev_priv *priv = netdev_priv(dev);
666 struct ipoib_mcast *mcast;
669 * We can only be called from ipoib_start_xmit, so we're
670 * inside tx_lock -- no need to save/restore flags.
672 spin_lock(&priv->lock);
674 if (!test_bit(IPOIB_MCAST_STARTED, &priv->flags) ||
676 !test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
677 ++priv->stats.tx_dropped;
678 dev_kfree_skb_any(skb);
682 mcast = __ipoib_mcast_find(dev, mgid);
684 /* Let's create a new send only group now */
685 ipoib_dbg_mcast(priv, "setting up send only multicast group for "
686 IPOIB_GID_FMT "\n", IPOIB_GID_RAW_ARG(mgid));
688 mcast = ipoib_mcast_alloc(dev, 0);
690 ipoib_warn(priv, "unable to allocate memory for "
691 "multicast structure\n");
692 ++priv->stats.tx_dropped;
693 dev_kfree_skb_any(skb);
697 set_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags);
698 memcpy(mcast->mcmember.mgid.raw, mgid, sizeof (union ib_gid));
699 __ipoib_mcast_add(dev, mcast);
700 list_add_tail(&mcast->list, &priv->multicast_list);
704 if (skb_queue_len(&mcast->pkt_queue) < IPOIB_MAX_MCAST_QUEUE)
705 skb_queue_tail(&mcast->pkt_queue, skb);
707 ++priv->stats.tx_dropped;
708 dev_kfree_skb_any(skb);
711 if (test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
712 ipoib_dbg_mcast(priv, "no address vector, "
713 "but multicast join already started\n");
714 else if (test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags))
715 ipoib_mcast_sendonly_join(mcast);
718 * If lookup completes between here and out:, don't
719 * want to send packet twice.
725 if (mcast && mcast->ah) {
727 skb->dst->neighbour &&
728 !*to_ipoib_neigh(skb->dst->neighbour)) {
729 struct ipoib_neigh *neigh = ipoib_neigh_alloc(skb->dst->neighbour);
732 kref_get(&mcast->ah->ref);
733 neigh->ah = mcast->ah;
734 list_add_tail(&neigh->list, &mcast->neigh_list);
738 ipoib_send(dev, skb, mcast->ah, IB_MULTICAST_QPN);
742 spin_unlock(&priv->lock);
745 void ipoib_mcast_dev_flush(struct net_device *dev)
747 struct ipoib_dev_priv *priv = netdev_priv(dev);
748 LIST_HEAD(remove_list);
749 struct ipoib_mcast *mcast, *tmcast;
752 ipoib_dbg_mcast(priv, "flushing multicast list\n");
754 spin_lock_irqsave(&priv->lock, flags);
756 list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
757 list_del(&mcast->list);
758 rb_erase(&mcast->rb_node, &priv->multicast_tree);
759 list_add_tail(&mcast->list, &remove_list);
762 if (priv->broadcast) {
763 rb_erase(&priv->broadcast->rb_node, &priv->multicast_tree);
764 list_add_tail(&priv->broadcast->list, &remove_list);
765 priv->broadcast = NULL;
768 spin_unlock_irqrestore(&priv->lock, flags);
770 list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
771 ipoib_mcast_leave(dev, mcast);
772 ipoib_mcast_free(mcast);
776 void ipoib_mcast_restart_task(struct work_struct *work)
778 struct ipoib_dev_priv *priv =
779 container_of(work, struct ipoib_dev_priv, restart_task);
780 struct net_device *dev = priv->dev;
781 struct dev_mc_list *mclist;
782 struct ipoib_mcast *mcast, *tmcast;
783 LIST_HEAD(remove_list);
786 ipoib_dbg_mcast(priv, "restarting multicast task\n");
788 ipoib_mcast_stop_thread(dev, 0);
790 local_irq_save(flags);
792 spin_lock(&priv->lock);
795 * Unfortunately, the networking core only gives us a list of all of
796 * the multicast hardware addresses. We need to figure out which ones
797 * are new and which ones have been removed
800 /* Clear out the found flag */
801 list_for_each_entry(mcast, &priv->multicast_list, list)
802 clear_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
804 /* Mark all of the entries that are found or don't exist */
805 for (mclist = dev->mc_list; mclist; mclist = mclist->next) {
808 memcpy(mgid.raw, mclist->dmi_addr + 4, sizeof mgid);
810 /* Add in the P_Key */
811 mgid.raw[4] = (priv->pkey >> 8) & 0xff;
812 mgid.raw[5] = priv->pkey & 0xff;
814 mcast = __ipoib_mcast_find(dev, &mgid);
815 if (!mcast || test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
816 struct ipoib_mcast *nmcast;
818 /* Not found or send-only group, let's add a new entry */
819 ipoib_dbg_mcast(priv, "adding multicast entry for mgid "
820 IPOIB_GID_FMT "\n", IPOIB_GID_ARG(mgid));
822 nmcast = ipoib_mcast_alloc(dev, 0);
824 ipoib_warn(priv, "unable to allocate memory for multicast structure\n");
828 set_bit(IPOIB_MCAST_FLAG_FOUND, &nmcast->flags);
830 nmcast->mcmember.mgid = mgid;
833 /* Destroy the send only entry */
834 list_move_tail(&mcast->list, &remove_list);
836 rb_replace_node(&mcast->rb_node,
838 &priv->multicast_tree);
840 __ipoib_mcast_add(dev, nmcast);
842 list_add_tail(&nmcast->list, &priv->multicast_list);
846 set_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
849 /* Remove all of the entries don't exist anymore */
850 list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
851 if (!test_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags) &&
852 !test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
853 ipoib_dbg_mcast(priv, "deleting multicast group " IPOIB_GID_FMT "\n",
854 IPOIB_GID_ARG(mcast->mcmember.mgid));
856 rb_erase(&mcast->rb_node, &priv->multicast_tree);
858 /* Move to the remove list */
859 list_move_tail(&mcast->list, &remove_list);
863 spin_unlock(&priv->lock);
864 netif_tx_unlock(dev);
865 local_irq_restore(flags);
867 /* We have to cancel outside of the spinlock */
868 list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
869 ipoib_mcast_leave(mcast->dev, mcast);
870 ipoib_mcast_free(mcast);
873 if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
874 ipoib_mcast_start_thread(dev);
877 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
879 struct ipoib_mcast_iter *ipoib_mcast_iter_init(struct net_device *dev)
881 struct ipoib_mcast_iter *iter;
883 iter = kmalloc(sizeof *iter, GFP_KERNEL);
888 memset(iter->mgid.raw, 0, 16);
890 if (ipoib_mcast_iter_next(iter)) {
898 int ipoib_mcast_iter_next(struct ipoib_mcast_iter *iter)
900 struct ipoib_dev_priv *priv = netdev_priv(iter->dev);
902 struct ipoib_mcast *mcast;
905 spin_lock_irq(&priv->lock);
907 n = rb_first(&priv->multicast_tree);
910 mcast = rb_entry(n, struct ipoib_mcast, rb_node);
912 if (memcmp(iter->mgid.raw, mcast->mcmember.mgid.raw,
913 sizeof (union ib_gid)) < 0) {
914 iter->mgid = mcast->mcmember.mgid;
915 iter->created = mcast->created;
916 iter->queuelen = skb_queue_len(&mcast->pkt_queue);
917 iter->complete = !!mcast->ah;
918 iter->send_only = !!(mcast->flags & (1 << IPOIB_MCAST_FLAG_SENDONLY));
928 spin_unlock_irq(&priv->lock);
933 void ipoib_mcast_iter_read(struct ipoib_mcast_iter *iter,
935 unsigned long *created,
936 unsigned int *queuelen,
937 unsigned int *complete,
938 unsigned int *send_only)
941 *created = iter->created;
942 *queuelen = iter->queuelen;
943 *complete = iter->complete;
944 *send_only = iter->send_only;
947 #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */