2 * Copyright (c) 2004, 2005 Topspin Communications. All rights reserved.
3 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
4 * Copyright (c) 2004 Voltaire, Inc. All rights reserved.
6 * This software is available to you under a choice of one of two
7 * licenses. You may choose to be licensed under the terms of the GNU
8 * General Public License (GPL) Version 2, available from the file
9 * COPYING in the main directory of this source tree, or the
10 * OpenIB.org BSD license below:
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
20 * - Redistributions in binary form must reproduce the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer in the documentation and/or other materials
23 * provided with the distribution.
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
35 #include <linux/skbuff.h>
36 #include <linux/rtnetlink.h>
39 #include <linux/igmp.h>
40 #include <linux/inetdevice.h>
41 #include <linux/delay.h>
42 #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 DEFINE_MUTEX(mcast_mutex);
58 struct ipoib_mcast_iter {
59 struct net_device *dev;
61 unsigned long created;
62 unsigned int queuelen;
63 unsigned int complete;
64 unsigned int send_only;
67 static void ipoib_mcast_free(struct ipoib_mcast *mcast)
69 struct net_device *dev = mcast->dev;
70 struct ipoib_dev_priv *priv = netdev_priv(dev);
71 struct ipoib_neigh *neigh, *tmp;
75 ipoib_dbg_mcast(netdev_priv(dev),
76 "deleting multicast group " IPOIB_GID_FMT "\n",
77 IPOIB_GID_ARG(mcast->mcmember.mgid));
79 spin_lock_irqsave(&priv->lock, flags);
81 list_for_each_entry_safe(neigh, tmp, &mcast->neigh_list, list) {
83 * It's safe to call ipoib_put_ah() inside priv->lock
84 * here, because we know that mcast->ah will always
85 * hold one more reference, so ipoib_put_ah() will
86 * never do more than decrement the ref count.
89 ipoib_put_ah(neigh->ah);
90 ipoib_neigh_free(dev, neigh);
93 spin_unlock_irqrestore(&priv->lock, flags);
96 ipoib_put_ah(mcast->ah);
98 while (!skb_queue_empty(&mcast->pkt_queue)) {
100 dev_kfree_skb_any(skb_dequeue(&mcast->pkt_queue));
103 spin_lock_irqsave(&priv->tx_lock, flags);
104 dev->stats.tx_dropped += tx_dropped;
105 spin_unlock_irqrestore(&priv->tx_lock, flags);
110 static struct ipoib_mcast *ipoib_mcast_alloc(struct net_device *dev,
113 struct ipoib_mcast *mcast;
115 mcast = kzalloc(sizeof *mcast, can_sleep ? GFP_KERNEL : GFP_ATOMIC);
120 mcast->created = jiffies;
123 INIT_LIST_HEAD(&mcast->list);
124 INIT_LIST_HEAD(&mcast->neigh_list);
125 skb_queue_head_init(&mcast->pkt_queue);
130 static struct ipoib_mcast *__ipoib_mcast_find(struct net_device *dev, void *mgid)
132 struct ipoib_dev_priv *priv = netdev_priv(dev);
133 struct rb_node *n = priv->multicast_tree.rb_node;
136 struct ipoib_mcast *mcast;
139 mcast = rb_entry(n, struct ipoib_mcast, rb_node);
141 ret = memcmp(mgid, mcast->mcmember.mgid.raw,
142 sizeof (union ib_gid));
154 static int __ipoib_mcast_add(struct net_device *dev, struct ipoib_mcast *mcast)
156 struct ipoib_dev_priv *priv = netdev_priv(dev);
157 struct rb_node **n = &priv->multicast_tree.rb_node, *pn = NULL;
160 struct ipoib_mcast *tmcast;
164 tmcast = rb_entry(pn, struct ipoib_mcast, rb_node);
166 ret = memcmp(mcast->mcmember.mgid.raw, tmcast->mcmember.mgid.raw,
167 sizeof (union ib_gid));
176 rb_link_node(&mcast->rb_node, pn, n);
177 rb_insert_color(&mcast->rb_node, &priv->multicast_tree);
182 static int ipoib_mcast_join_finish(struct ipoib_mcast *mcast,
183 struct ib_sa_mcmember_rec *mcmember)
185 struct net_device *dev = mcast->dev;
186 struct ipoib_dev_priv *priv = netdev_priv(dev);
190 mcast->mcmember = *mcmember;
192 /* Set the cached Q_Key before we attach if it's the broadcast group */
193 if (!memcmp(mcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
194 sizeof (union ib_gid))) {
195 spin_lock_irq(&priv->lock);
196 if (!priv->broadcast) {
197 spin_unlock_irq(&priv->lock);
200 priv->qkey = be32_to_cpu(priv->broadcast->mcmember.qkey);
201 spin_unlock_irq(&priv->lock);
202 priv->tx_wr.wr.ud.remote_qkey = priv->qkey;
205 if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
206 if (test_and_set_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
207 ipoib_warn(priv, "multicast group " IPOIB_GID_FMT
208 " already attached\n",
209 IPOIB_GID_ARG(mcast->mcmember.mgid));
214 ret = ipoib_mcast_attach(dev, be16_to_cpu(mcast->mcmember.mlid),
215 &mcast->mcmember.mgid);
217 ipoib_warn(priv, "couldn't attach QP to multicast group "
219 IPOIB_GID_ARG(mcast->mcmember.mgid));
221 clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags);
227 struct ib_ah_attr av = {
228 .dlid = be16_to_cpu(mcast->mcmember.mlid),
229 .port_num = priv->port,
230 .sl = mcast->mcmember.sl,
231 .ah_flags = IB_AH_GRH,
232 .static_rate = mcast->mcmember.rate,
234 .flow_label = be32_to_cpu(mcast->mcmember.flow_label),
235 .hop_limit = mcast->mcmember.hop_limit,
237 .traffic_class = mcast->mcmember.traffic_class
240 av.grh.dgid = mcast->mcmember.mgid;
242 ah = ipoib_create_ah(dev, priv->pd, &av);
244 ipoib_warn(priv, "ib_address_create failed\n");
246 spin_lock_irq(&priv->lock);
248 spin_unlock_irq(&priv->lock);
250 ipoib_dbg_mcast(priv, "MGID " IPOIB_GID_FMT
251 " AV %p, LID 0x%04x, SL %d\n",
252 IPOIB_GID_ARG(mcast->mcmember.mgid),
254 be16_to_cpu(mcast->mcmember.mlid),
259 /* actually send any queued packets */
260 spin_lock_irq(&priv->tx_lock);
261 while (!skb_queue_empty(&mcast->pkt_queue)) {
262 struct sk_buff *skb = skb_dequeue(&mcast->pkt_queue);
263 spin_unlock_irq(&priv->tx_lock);
267 if (!skb->dst || !skb->dst->neighbour) {
268 /* put pseudoheader back on for next time */
269 skb_push(skb, sizeof (struct ipoib_pseudoheader));
272 if (dev_queue_xmit(skb))
273 ipoib_warn(priv, "dev_queue_xmit failed to requeue packet\n");
274 spin_lock_irq(&priv->tx_lock);
276 spin_unlock_irq(&priv->tx_lock);
282 ipoib_mcast_sendonly_join_complete(int status,
283 struct ib_sa_multicast *multicast)
285 struct ipoib_mcast *mcast = multicast->context;
286 struct net_device *dev = mcast->dev;
287 struct ipoib_dev_priv *priv = netdev_priv(dev);
289 /* We trap for port events ourselves. */
290 if (status == -ENETRESET)
294 status = ipoib_mcast_join_finish(mcast, &multicast->rec);
297 if (mcast->logcount++ < 20)
298 ipoib_dbg_mcast(netdev_priv(dev), "multicast join failed for "
299 IPOIB_GID_FMT ", status %d\n",
300 IPOIB_GID_ARG(mcast->mcmember.mgid), status);
302 /* Flush out any queued packets */
303 spin_lock_irq(&priv->tx_lock);
304 while (!skb_queue_empty(&mcast->pkt_queue)) {
305 ++dev->stats.tx_dropped;
306 dev_kfree_skb_any(skb_dequeue(&mcast->pkt_queue));
308 spin_unlock_irq(&priv->tx_lock);
310 /* Clear the busy flag so we try again */
311 status = test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY,
317 static int ipoib_mcast_sendonly_join(struct ipoib_mcast *mcast)
319 struct net_device *dev = mcast->dev;
320 struct ipoib_dev_priv *priv = netdev_priv(dev);
321 struct ib_sa_mcmember_rec rec = {
322 #if 0 /* Some SMs don't support send-only yet */
330 if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
331 ipoib_dbg_mcast(priv, "device shutting down, no multicast joins\n");
335 if (test_and_set_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags)) {
336 ipoib_dbg_mcast(priv, "multicast entry busy, skipping\n");
340 rec.mgid = mcast->mcmember.mgid;
341 rec.port_gid = priv->local_gid;
342 rec.pkey = cpu_to_be16(priv->pkey);
344 mcast->mc = ib_sa_join_multicast(&ipoib_sa_client, priv->ca,
346 IB_SA_MCMEMBER_REC_MGID |
347 IB_SA_MCMEMBER_REC_PORT_GID |
348 IB_SA_MCMEMBER_REC_PKEY |
349 IB_SA_MCMEMBER_REC_JOIN_STATE,
351 ipoib_mcast_sendonly_join_complete,
353 if (IS_ERR(mcast->mc)) {
354 ret = PTR_ERR(mcast->mc);
355 clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
356 ipoib_warn(priv, "ib_sa_join_multicast failed (ret = %d)\n",
359 ipoib_dbg_mcast(priv, "no multicast record for " IPOIB_GID_FMT
361 IPOIB_GID_ARG(mcast->mcmember.mgid));
367 static int ipoib_mcast_join_complete(int status,
368 struct ib_sa_multicast *multicast)
370 struct ipoib_mcast *mcast = multicast->context;
371 struct net_device *dev = mcast->dev;
372 struct ipoib_dev_priv *priv = netdev_priv(dev);
374 ipoib_dbg_mcast(priv, "join completion for " IPOIB_GID_FMT
376 IPOIB_GID_ARG(mcast->mcmember.mgid), status);
378 /* We trap for port events ourselves. */
379 if (status == -ENETRESET)
383 status = ipoib_mcast_join_finish(mcast, &multicast->rec);
387 mutex_lock(&mcast_mutex);
388 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
389 queue_delayed_work(ipoib_workqueue,
390 &priv->mcast_task, 0);
391 mutex_unlock(&mcast_mutex);
393 if (mcast == priv->broadcast)
394 netif_carrier_on(dev);
399 if (mcast->logcount++ < 20) {
400 if (status == -ETIMEDOUT) {
401 ipoib_dbg_mcast(priv, "multicast join failed for " IPOIB_GID_FMT
403 IPOIB_GID_ARG(mcast->mcmember.mgid),
406 ipoib_warn(priv, "multicast join failed for "
407 IPOIB_GID_FMT ", status %d\n",
408 IPOIB_GID_ARG(mcast->mcmember.mgid),
414 if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
415 mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
417 /* Clear the busy flag so we try again */
418 status = test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
420 mutex_lock(&mcast_mutex);
421 spin_lock_irq(&priv->lock);
422 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
423 queue_delayed_work(ipoib_workqueue, &priv->mcast_task,
424 mcast->backoff * HZ);
425 spin_unlock_irq(&priv->lock);
426 mutex_unlock(&mcast_mutex);
431 static void ipoib_mcast_join(struct net_device *dev, struct ipoib_mcast *mcast,
434 struct ipoib_dev_priv *priv = netdev_priv(dev);
435 struct ib_sa_mcmember_rec rec = {
438 ib_sa_comp_mask comp_mask;
441 ipoib_dbg_mcast(priv, "joining MGID " IPOIB_GID_FMT "\n",
442 IPOIB_GID_ARG(mcast->mcmember.mgid));
444 rec.mgid = mcast->mcmember.mgid;
445 rec.port_gid = priv->local_gid;
446 rec.pkey = cpu_to_be16(priv->pkey);
449 IB_SA_MCMEMBER_REC_MGID |
450 IB_SA_MCMEMBER_REC_PORT_GID |
451 IB_SA_MCMEMBER_REC_PKEY |
452 IB_SA_MCMEMBER_REC_JOIN_STATE;
456 IB_SA_MCMEMBER_REC_QKEY |
457 IB_SA_MCMEMBER_REC_MTU_SELECTOR |
458 IB_SA_MCMEMBER_REC_MTU |
459 IB_SA_MCMEMBER_REC_TRAFFIC_CLASS |
460 IB_SA_MCMEMBER_REC_RATE_SELECTOR |
461 IB_SA_MCMEMBER_REC_RATE |
462 IB_SA_MCMEMBER_REC_SL |
463 IB_SA_MCMEMBER_REC_FLOW_LABEL |
464 IB_SA_MCMEMBER_REC_HOP_LIMIT;
466 rec.qkey = priv->broadcast->mcmember.qkey;
467 rec.mtu_selector = IB_SA_EQ;
468 rec.mtu = priv->broadcast->mcmember.mtu;
469 rec.traffic_class = priv->broadcast->mcmember.traffic_class;
470 rec.rate_selector = IB_SA_EQ;
471 rec.rate = priv->broadcast->mcmember.rate;
472 rec.sl = priv->broadcast->mcmember.sl;
473 rec.flow_label = priv->broadcast->mcmember.flow_label;
474 rec.hop_limit = priv->broadcast->mcmember.hop_limit;
477 set_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
478 mcast->mc = ib_sa_join_multicast(&ipoib_sa_client, priv->ca, priv->port,
479 &rec, comp_mask, GFP_KERNEL,
480 ipoib_mcast_join_complete, mcast);
481 if (IS_ERR(mcast->mc)) {
482 clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
483 ret = PTR_ERR(mcast->mc);
484 ipoib_warn(priv, "ib_sa_join_multicast failed, status %d\n", ret);
487 if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
488 mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
490 mutex_lock(&mcast_mutex);
491 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
492 queue_delayed_work(ipoib_workqueue,
494 mcast->backoff * HZ);
495 mutex_unlock(&mcast_mutex);
499 void ipoib_mcast_join_task(struct work_struct *work)
501 struct ipoib_dev_priv *priv =
502 container_of(work, struct ipoib_dev_priv, mcast_task.work);
503 struct net_device *dev = priv->dev;
505 if (!test_bit(IPOIB_MCAST_RUN, &priv->flags))
508 if (ib_query_gid(priv->ca, priv->port, 0, &priv->local_gid))
509 ipoib_warn(priv, "ib_query_gid() failed\n");
511 memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));
514 struct ib_port_attr attr;
516 if (!ib_query_port(priv->ca, priv->port, &attr))
517 priv->local_lid = attr.lid;
519 ipoib_warn(priv, "ib_query_port failed\n");
522 if (!priv->broadcast) {
523 struct ipoib_mcast *broadcast;
525 broadcast = ipoib_mcast_alloc(dev, 1);
527 ipoib_warn(priv, "failed to allocate broadcast group\n");
528 mutex_lock(&mcast_mutex);
529 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
530 queue_delayed_work(ipoib_workqueue,
531 &priv->mcast_task, HZ);
532 mutex_unlock(&mcast_mutex);
536 spin_lock_irq(&priv->lock);
537 memcpy(broadcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
538 sizeof (union ib_gid));
539 priv->broadcast = broadcast;
541 __ipoib_mcast_add(dev, priv->broadcast);
542 spin_unlock_irq(&priv->lock);
545 if (!test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
546 if (!test_bit(IPOIB_MCAST_FLAG_BUSY, &priv->broadcast->flags))
547 ipoib_mcast_join(dev, priv->broadcast, 0);
552 struct ipoib_mcast *mcast = NULL;
554 spin_lock_irq(&priv->lock);
555 list_for_each_entry(mcast, &priv->multicast_list, list) {
556 if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)
557 && !test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags)
558 && !test_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
559 /* Found the next unjoined group */
563 spin_unlock_irq(&priv->lock);
565 if (&mcast->list == &priv->multicast_list) {
570 ipoib_mcast_join(dev, mcast, 1);
574 priv->mcast_mtu = IPOIB_UD_MTU(ib_mtu_enum_to_int(priv->broadcast->mcmember.mtu));
576 if (!ipoib_cm_admin_enabled(dev))
577 dev->mtu = min(priv->mcast_mtu, priv->admin_mtu);
579 ipoib_dbg_mcast(priv, "successfully joined all multicast groups\n");
581 clear_bit(IPOIB_MCAST_RUN, &priv->flags);
584 int ipoib_mcast_start_thread(struct net_device *dev)
586 struct ipoib_dev_priv *priv = netdev_priv(dev);
588 ipoib_dbg_mcast(priv, "starting multicast thread\n");
590 mutex_lock(&mcast_mutex);
591 if (!test_and_set_bit(IPOIB_MCAST_RUN, &priv->flags))
592 queue_delayed_work(ipoib_workqueue, &priv->mcast_task, 0);
593 mutex_unlock(&mcast_mutex);
595 spin_lock_irq(&priv->lock);
596 set_bit(IPOIB_MCAST_STARTED, &priv->flags);
597 spin_unlock_irq(&priv->lock);
602 int ipoib_mcast_stop_thread(struct net_device *dev, int flush)
604 struct ipoib_dev_priv *priv = netdev_priv(dev);
606 ipoib_dbg_mcast(priv, "stopping multicast thread\n");
608 spin_lock_irq(&priv->lock);
609 clear_bit(IPOIB_MCAST_STARTED, &priv->flags);
610 spin_unlock_irq(&priv->lock);
612 mutex_lock(&mcast_mutex);
613 clear_bit(IPOIB_MCAST_RUN, &priv->flags);
614 cancel_delayed_work(&priv->mcast_task);
615 mutex_unlock(&mcast_mutex);
618 flush_workqueue(ipoib_workqueue);
623 static int ipoib_mcast_leave(struct net_device *dev, struct ipoib_mcast *mcast)
625 struct ipoib_dev_priv *priv = netdev_priv(dev);
628 if (test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
629 ib_sa_free_multicast(mcast->mc);
631 if (test_and_clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
632 ipoib_dbg_mcast(priv, "leaving MGID " IPOIB_GID_FMT "\n",
633 IPOIB_GID_ARG(mcast->mcmember.mgid));
635 /* Remove ourselves from the multicast group */
636 ret = ipoib_mcast_detach(dev, be16_to_cpu(mcast->mcmember.mlid),
637 &mcast->mcmember.mgid);
639 ipoib_warn(priv, "ipoib_mcast_detach failed (result = %d)\n", ret);
645 void ipoib_mcast_send(struct net_device *dev, void *mgid, struct sk_buff *skb)
647 struct ipoib_dev_priv *priv = netdev_priv(dev);
648 struct ipoib_mcast *mcast;
651 * We can only be called from ipoib_start_xmit, so we're
652 * inside tx_lock -- no need to save/restore flags.
654 spin_lock(&priv->lock);
656 if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags) ||
658 !test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
659 ++dev->stats.tx_dropped;
660 dev_kfree_skb_any(skb);
664 mcast = __ipoib_mcast_find(dev, mgid);
666 /* Let's create a new send only group now */
667 ipoib_dbg_mcast(priv, "setting up send only multicast group for "
668 IPOIB_GID_FMT "\n", IPOIB_GID_RAW_ARG(mgid));
670 mcast = ipoib_mcast_alloc(dev, 0);
672 ipoib_warn(priv, "unable to allocate memory for "
673 "multicast structure\n");
674 ++dev->stats.tx_dropped;
675 dev_kfree_skb_any(skb);
679 set_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags);
680 memcpy(mcast->mcmember.mgid.raw, mgid, sizeof (union ib_gid));
681 __ipoib_mcast_add(dev, mcast);
682 list_add_tail(&mcast->list, &priv->multicast_list);
686 if (skb_queue_len(&mcast->pkt_queue) < IPOIB_MAX_MCAST_QUEUE)
687 skb_queue_tail(&mcast->pkt_queue, skb);
689 ++dev->stats.tx_dropped;
690 dev_kfree_skb_any(skb);
693 if (test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
694 ipoib_dbg_mcast(priv, "no address vector, "
695 "but multicast join already started\n");
696 else if (test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags))
697 ipoib_mcast_sendonly_join(mcast);
700 * If lookup completes between here and out:, don't
701 * want to send packet twice.
707 if (mcast && mcast->ah) {
709 skb->dst->neighbour &&
710 !*to_ipoib_neigh(skb->dst->neighbour)) {
711 struct ipoib_neigh *neigh = ipoib_neigh_alloc(skb->dst->neighbour,
715 kref_get(&mcast->ah->ref);
716 neigh->ah = mcast->ah;
717 list_add_tail(&neigh->list, &mcast->neigh_list);
721 ipoib_send(dev, skb, mcast->ah, IB_MULTICAST_QPN);
725 spin_unlock(&priv->lock);
728 void ipoib_mcast_dev_flush(struct net_device *dev)
730 struct ipoib_dev_priv *priv = netdev_priv(dev);
731 LIST_HEAD(remove_list);
732 struct ipoib_mcast *mcast, *tmcast;
735 ipoib_dbg_mcast(priv, "flushing multicast list\n");
737 spin_lock_irqsave(&priv->lock, flags);
739 list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
740 list_del(&mcast->list);
741 rb_erase(&mcast->rb_node, &priv->multicast_tree);
742 list_add_tail(&mcast->list, &remove_list);
745 if (priv->broadcast) {
746 rb_erase(&priv->broadcast->rb_node, &priv->multicast_tree);
747 list_add_tail(&priv->broadcast->list, &remove_list);
748 priv->broadcast = NULL;
751 spin_unlock_irqrestore(&priv->lock, flags);
753 list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
754 ipoib_mcast_leave(dev, mcast);
755 ipoib_mcast_free(mcast);
759 void ipoib_mcast_restart_task(struct work_struct *work)
761 struct ipoib_dev_priv *priv =
762 container_of(work, struct ipoib_dev_priv, restart_task);
763 struct net_device *dev = priv->dev;
764 struct dev_mc_list *mclist;
765 struct ipoib_mcast *mcast, *tmcast;
766 LIST_HEAD(remove_list);
768 struct ib_sa_mcmember_rec rec;
770 ipoib_dbg_mcast(priv, "restarting multicast task\n");
772 ipoib_mcast_stop_thread(dev, 0);
774 local_irq_save(flags);
776 spin_lock(&priv->lock);
779 * Unfortunately, the networking core only gives us a list of all of
780 * the multicast hardware addresses. We need to figure out which ones
781 * are new and which ones have been removed
784 /* Clear out the found flag */
785 list_for_each_entry(mcast, &priv->multicast_list, list)
786 clear_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
788 /* Mark all of the entries that are found or don't exist */
789 for (mclist = dev->mc_list; mclist; mclist = mclist->next) {
792 memcpy(mgid.raw, mclist->dmi_addr + 4, sizeof mgid);
794 mcast = __ipoib_mcast_find(dev, &mgid);
795 if (!mcast || test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
796 struct ipoib_mcast *nmcast;
798 /* ignore group which is directly joined by userspace */
799 if (test_bit(IPOIB_FLAG_UMCAST, &priv->flags) &&
800 !ib_sa_get_mcmember_rec(priv->ca, priv->port, &mgid, &rec)) {
801 ipoib_dbg_mcast(priv, "ignoring multicast entry for mgid "
802 IPOIB_GID_FMT "\n", IPOIB_GID_ARG(mgid));
806 /* Not found or send-only group, let's add a new entry */
807 ipoib_dbg_mcast(priv, "adding multicast entry for mgid "
808 IPOIB_GID_FMT "\n", IPOIB_GID_ARG(mgid));
810 nmcast = ipoib_mcast_alloc(dev, 0);
812 ipoib_warn(priv, "unable to allocate memory for multicast structure\n");
816 set_bit(IPOIB_MCAST_FLAG_FOUND, &nmcast->flags);
818 nmcast->mcmember.mgid = mgid;
821 /* Destroy the send only entry */
822 list_move_tail(&mcast->list, &remove_list);
824 rb_replace_node(&mcast->rb_node,
826 &priv->multicast_tree);
828 __ipoib_mcast_add(dev, nmcast);
830 list_add_tail(&nmcast->list, &priv->multicast_list);
834 set_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
837 /* Remove all of the entries don't exist anymore */
838 list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
839 if (!test_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags) &&
840 !test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
841 ipoib_dbg_mcast(priv, "deleting multicast group " IPOIB_GID_FMT "\n",
842 IPOIB_GID_ARG(mcast->mcmember.mgid));
844 rb_erase(&mcast->rb_node, &priv->multicast_tree);
846 /* Move to the remove list */
847 list_move_tail(&mcast->list, &remove_list);
851 spin_unlock(&priv->lock);
852 netif_tx_unlock(dev);
853 local_irq_restore(flags);
855 /* We have to cancel outside of the spinlock */
856 list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
857 ipoib_mcast_leave(mcast->dev, mcast);
858 ipoib_mcast_free(mcast);
861 if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
862 ipoib_mcast_start_thread(dev);
865 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
867 struct ipoib_mcast_iter *ipoib_mcast_iter_init(struct net_device *dev)
869 struct ipoib_mcast_iter *iter;
871 iter = kmalloc(sizeof *iter, GFP_KERNEL);
876 memset(iter->mgid.raw, 0, 16);
878 if (ipoib_mcast_iter_next(iter)) {
886 int ipoib_mcast_iter_next(struct ipoib_mcast_iter *iter)
888 struct ipoib_dev_priv *priv = netdev_priv(iter->dev);
890 struct ipoib_mcast *mcast;
893 spin_lock_irq(&priv->lock);
895 n = rb_first(&priv->multicast_tree);
898 mcast = rb_entry(n, struct ipoib_mcast, rb_node);
900 if (memcmp(iter->mgid.raw, mcast->mcmember.mgid.raw,
901 sizeof (union ib_gid)) < 0) {
902 iter->mgid = mcast->mcmember.mgid;
903 iter->created = mcast->created;
904 iter->queuelen = skb_queue_len(&mcast->pkt_queue);
905 iter->complete = !!mcast->ah;
906 iter->send_only = !!(mcast->flags & (1 << IPOIB_MCAST_FLAG_SENDONLY));
916 spin_unlock_irq(&priv->lock);
921 void ipoib_mcast_iter_read(struct ipoib_mcast_iter *iter,
923 unsigned long *created,
924 unsigned int *queuelen,
925 unsigned int *complete,
926 unsigned int *send_only)
929 *created = iter->created;
930 *queuelen = iter->queuelen;
931 *complete = iter->complete;
932 *send_only = iter->send_only;
935 #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */