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>
37 #include <linux/moduleparam.h>
40 #include <linux/igmp.h>
41 #include <linux/inetdevice.h>
42 #include <linux/delay.h>
43 #include <linux/completion.h>
44 #include <linux/slab.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 struct ipoib_mcast_iter {
61 struct net_device *dev;
63 unsigned long created;
64 unsigned int queuelen;
65 unsigned int complete;
66 unsigned int send_only;
69 static void ipoib_mcast_free(struct ipoib_mcast *mcast)
71 struct net_device *dev = mcast->dev;
74 ipoib_dbg_mcast(netdev_priv(dev), "deleting multicast group %pI6\n",
75 mcast->mcmember.mgid.raw);
77 /* remove all neigh connected to this mcast */
78 ipoib_del_neighs_by_gid(dev, mcast->mcmember.mgid.raw);
81 ipoib_put_ah(mcast->ah);
83 while (!skb_queue_empty(&mcast->pkt_queue)) {
85 dev_kfree_skb_any(skb_dequeue(&mcast->pkt_queue));
88 netif_tx_lock_bh(dev);
89 dev->stats.tx_dropped += tx_dropped;
90 netif_tx_unlock_bh(dev);
95 static struct ipoib_mcast *ipoib_mcast_alloc(struct net_device *dev,
98 struct ipoib_mcast *mcast;
100 mcast = kzalloc(sizeof *mcast, can_sleep ? GFP_KERNEL : GFP_ATOMIC);
105 mcast->created = jiffies;
108 INIT_LIST_HEAD(&mcast->list);
109 INIT_LIST_HEAD(&mcast->neigh_list);
110 skb_queue_head_init(&mcast->pkt_queue);
115 static struct ipoib_mcast *__ipoib_mcast_find(struct net_device *dev, void *mgid)
117 struct ipoib_dev_priv *priv = netdev_priv(dev);
118 struct rb_node *n = priv->multicast_tree.rb_node;
121 struct ipoib_mcast *mcast;
124 mcast = rb_entry(n, struct ipoib_mcast, rb_node);
126 ret = memcmp(mgid, mcast->mcmember.mgid.raw,
127 sizeof (union ib_gid));
139 static int __ipoib_mcast_add(struct net_device *dev, struct ipoib_mcast *mcast)
141 struct ipoib_dev_priv *priv = netdev_priv(dev);
142 struct rb_node **n = &priv->multicast_tree.rb_node, *pn = NULL;
145 struct ipoib_mcast *tmcast;
149 tmcast = rb_entry(pn, struct ipoib_mcast, rb_node);
151 ret = memcmp(mcast->mcmember.mgid.raw, tmcast->mcmember.mgid.raw,
152 sizeof (union ib_gid));
161 rb_link_node(&mcast->rb_node, pn, n);
162 rb_insert_color(&mcast->rb_node, &priv->multicast_tree);
167 static int ipoib_mcast_join_finish(struct ipoib_mcast *mcast,
168 struct ib_sa_mcmember_rec *mcmember)
170 struct net_device *dev = mcast->dev;
171 struct ipoib_dev_priv *priv = netdev_priv(dev);
176 mcast->mcmember = *mcmember;
178 /* Set the cached Q_Key before we attach if it's the broadcast group */
179 if (!memcmp(mcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
180 sizeof (union ib_gid))) {
181 spin_lock_irq(&priv->lock);
182 if (!priv->broadcast) {
183 spin_unlock_irq(&priv->lock);
186 priv->qkey = be32_to_cpu(priv->broadcast->mcmember.qkey);
187 spin_unlock_irq(&priv->lock);
188 priv->tx_wr.wr.ud.remote_qkey = priv->qkey;
192 if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
193 if (test_and_set_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
194 ipoib_warn(priv, "multicast group %pI6 already attached\n",
195 mcast->mcmember.mgid.raw);
200 ret = ipoib_mcast_attach(dev, be16_to_cpu(mcast->mcmember.mlid),
201 &mcast->mcmember.mgid, set_qkey);
203 ipoib_warn(priv, "couldn't attach QP to multicast group %pI6\n",
204 mcast->mcmember.mgid.raw);
206 clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags);
212 struct ib_ah_attr av = {
213 .dlid = be16_to_cpu(mcast->mcmember.mlid),
214 .port_num = priv->port,
215 .sl = mcast->mcmember.sl,
216 .ah_flags = IB_AH_GRH,
217 .static_rate = mcast->mcmember.rate,
219 .flow_label = be32_to_cpu(mcast->mcmember.flow_label),
220 .hop_limit = mcast->mcmember.hop_limit,
222 .traffic_class = mcast->mcmember.traffic_class
225 av.grh.dgid = mcast->mcmember.mgid;
227 ah = ipoib_create_ah(dev, priv->pd, &av);
229 ipoib_warn(priv, "ib_address_create failed %ld\n",
231 /* use original error */
234 spin_lock_irq(&priv->lock);
236 spin_unlock_irq(&priv->lock);
238 ipoib_dbg_mcast(priv, "MGID %pI6 AV %p, LID 0x%04x, SL %d\n",
239 mcast->mcmember.mgid.raw,
241 be16_to_cpu(mcast->mcmember.mlid),
246 /* actually send any queued packets */
247 netif_tx_lock_bh(dev);
248 while (!skb_queue_empty(&mcast->pkt_queue)) {
249 struct sk_buff *skb = skb_dequeue(&mcast->pkt_queue);
251 netif_tx_unlock_bh(dev);
254 if (dev_queue_xmit(skb))
255 ipoib_warn(priv, "dev_queue_xmit failed to requeue packet\n");
257 netif_tx_lock_bh(dev);
259 netif_tx_unlock_bh(dev);
265 ipoib_mcast_sendonly_join_complete(int status,
266 struct ib_sa_multicast *multicast)
268 struct ipoib_mcast *mcast = multicast->context;
269 struct net_device *dev = mcast->dev;
271 /* We trap for port events ourselves. */
272 if (status == -ENETRESET)
276 status = ipoib_mcast_join_finish(mcast, &multicast->rec);
279 if (mcast->logcount++ < 20)
280 ipoib_dbg_mcast(netdev_priv(dev), "multicast join failed for %pI6, status %d\n",
281 mcast->mcmember.mgid.raw, status);
283 /* Flush out any queued packets */
284 netif_tx_lock_bh(dev);
285 while (!skb_queue_empty(&mcast->pkt_queue)) {
286 ++dev->stats.tx_dropped;
287 dev_kfree_skb_any(skb_dequeue(&mcast->pkt_queue));
289 netif_tx_unlock_bh(dev);
291 /* Clear the busy flag so we try again */
292 status = test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY,
298 static int ipoib_mcast_sendonly_join(struct ipoib_mcast *mcast)
300 struct net_device *dev = mcast->dev;
301 struct ipoib_dev_priv *priv = netdev_priv(dev);
302 struct ib_sa_mcmember_rec rec = {
303 #if 0 /* Some SMs don't support send-only yet */
311 if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
312 ipoib_dbg_mcast(priv, "device shutting down, no multicast joins\n");
316 if (test_and_set_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags)) {
317 ipoib_dbg_mcast(priv, "multicast entry busy, skipping\n");
321 rec.mgid = mcast->mcmember.mgid;
322 rec.port_gid = priv->local_gid;
323 rec.pkey = cpu_to_be16(priv->pkey);
325 mcast->mc = ib_sa_join_multicast(&ipoib_sa_client, priv->ca,
327 IB_SA_MCMEMBER_REC_MGID |
328 IB_SA_MCMEMBER_REC_PORT_GID |
329 IB_SA_MCMEMBER_REC_PKEY |
330 IB_SA_MCMEMBER_REC_JOIN_STATE,
332 ipoib_mcast_sendonly_join_complete,
334 if (IS_ERR(mcast->mc)) {
335 ret = PTR_ERR(mcast->mc);
336 clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
337 ipoib_warn(priv, "ib_sa_join_multicast failed (ret = %d)\n",
340 ipoib_dbg_mcast(priv, "no multicast record for %pI6, starting join\n",
341 mcast->mcmember.mgid.raw);
347 void ipoib_mcast_carrier_on_task(struct work_struct *work)
349 struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
351 struct ib_port_attr attr;
354 * Take rtnl_lock to avoid racing with ipoib_stop() and
355 * turning the carrier back on while a device is being
358 if (ib_query_port(priv->ca, priv->port, &attr) ||
359 attr.state != IB_PORT_ACTIVE) {
360 ipoib_dbg(priv, "Keeping carrier off until IB port is active\n");
365 netif_carrier_on(priv->dev);
369 static int ipoib_mcast_join_complete(int status,
370 struct ib_sa_multicast *multicast)
372 struct ipoib_mcast *mcast = multicast->context;
373 struct net_device *dev = mcast->dev;
374 struct ipoib_dev_priv *priv = netdev_priv(dev);
376 ipoib_dbg_mcast(priv, "join completion for %pI6 (status %d)\n",
377 mcast->mcmember.mgid.raw, status);
379 /* We trap for port events ourselves. */
380 if (status == -ENETRESET)
384 status = ipoib_mcast_join_finish(mcast, &multicast->rec);
388 mutex_lock(&mcast_mutex);
389 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
390 queue_delayed_work(ipoib_workqueue,
391 &priv->mcast_task, 0);
392 mutex_unlock(&mcast_mutex);
395 * Defer carrier on work to ipoib_workqueue to avoid a
396 * deadlock on rtnl_lock here.
398 if (mcast == priv->broadcast)
399 queue_work(ipoib_workqueue, &priv->carrier_on_task);
404 if (mcast->logcount++ < 20) {
405 if (status == -ETIMEDOUT || status == -EAGAIN) {
406 ipoib_dbg_mcast(priv, "multicast join failed for %pI6, status %d\n",
407 mcast->mcmember.mgid.raw, status);
409 ipoib_warn(priv, "multicast join failed for %pI6, status %d\n",
410 mcast->mcmember.mgid.raw, status);
415 if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
416 mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
418 /* Clear the busy flag so we try again */
419 status = test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
421 mutex_lock(&mcast_mutex);
422 spin_lock_irq(&priv->lock);
423 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
424 queue_delayed_work(ipoib_workqueue, &priv->mcast_task,
425 mcast->backoff * HZ);
426 spin_unlock_irq(&priv->lock);
427 mutex_unlock(&mcast_mutex);
432 static void ipoib_mcast_join(struct net_device *dev, struct ipoib_mcast *mcast,
435 struct ipoib_dev_priv *priv = netdev_priv(dev);
436 struct ib_sa_mcmember_rec rec = {
439 ib_sa_comp_mask comp_mask;
442 ipoib_dbg_mcast(priv, "joining MGID %pI6\n", mcast->mcmember.mgid.raw);
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 if (!test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
528 broadcast = ipoib_mcast_alloc(dev, 1);
530 ipoib_warn(priv, "failed to allocate broadcast group\n");
531 mutex_lock(&mcast_mutex);
532 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
533 queue_delayed_work(ipoib_workqueue,
534 &priv->mcast_task, HZ);
535 mutex_unlock(&mcast_mutex);
539 spin_lock_irq(&priv->lock);
540 memcpy(broadcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
541 sizeof (union ib_gid));
542 priv->broadcast = broadcast;
544 __ipoib_mcast_add(dev, priv->broadcast);
545 spin_unlock_irq(&priv->lock);
548 if (!test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
549 if (!test_bit(IPOIB_MCAST_FLAG_BUSY, &priv->broadcast->flags))
550 ipoib_mcast_join(dev, priv->broadcast, 0);
555 struct ipoib_mcast *mcast = NULL;
557 spin_lock_irq(&priv->lock);
558 list_for_each_entry(mcast, &priv->multicast_list, list) {
559 if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)
560 && !test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags)
561 && !test_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
562 /* Found the next unjoined group */
566 spin_unlock_irq(&priv->lock);
568 if (&mcast->list == &priv->multicast_list) {
573 ipoib_mcast_join(dev, mcast, 1);
577 priv->mcast_mtu = IPOIB_UD_MTU(ib_mtu_enum_to_int(priv->broadcast->mcmember.mtu));
579 if (!ipoib_cm_admin_enabled(dev)) {
581 dev_set_mtu(dev, min(priv->mcast_mtu, priv->admin_mtu));
585 ipoib_dbg_mcast(priv, "successfully joined all multicast groups\n");
587 clear_bit(IPOIB_MCAST_RUN, &priv->flags);
590 int ipoib_mcast_start_thread(struct net_device *dev)
592 struct ipoib_dev_priv *priv = netdev_priv(dev);
594 ipoib_dbg_mcast(priv, "starting multicast thread\n");
596 mutex_lock(&mcast_mutex);
597 if (!test_and_set_bit(IPOIB_MCAST_RUN, &priv->flags))
598 queue_delayed_work(ipoib_workqueue, &priv->mcast_task, 0);
599 mutex_unlock(&mcast_mutex);
604 int ipoib_mcast_stop_thread(struct net_device *dev, int flush)
606 struct ipoib_dev_priv *priv = netdev_priv(dev);
608 ipoib_dbg_mcast(priv, "stopping multicast thread\n");
610 mutex_lock(&mcast_mutex);
611 clear_bit(IPOIB_MCAST_RUN, &priv->flags);
612 cancel_delayed_work(&priv->mcast_task);
613 mutex_unlock(&mcast_mutex);
616 flush_workqueue(ipoib_workqueue);
621 static int ipoib_mcast_leave(struct net_device *dev, struct ipoib_mcast *mcast)
623 struct ipoib_dev_priv *priv = netdev_priv(dev);
626 if (test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
627 ib_sa_free_multicast(mcast->mc);
629 if (test_and_clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
630 ipoib_dbg_mcast(priv, "leaving MGID %pI6\n",
631 mcast->mcmember.mgid.raw);
633 /* Remove ourselves from the multicast group */
634 ret = ib_detach_mcast(priv->qp, &mcast->mcmember.mgid,
635 be16_to_cpu(mcast->mcmember.mlid));
637 ipoib_warn(priv, "ib_detach_mcast failed (result = %d)\n", ret);
643 void ipoib_mcast_send(struct net_device *dev, u8 *daddr, struct sk_buff *skb)
645 struct ipoib_dev_priv *priv = netdev_priv(dev);
646 struct ipoib_mcast *mcast;
648 void *mgid = daddr + 4;
650 spin_lock_irqsave(&priv->lock, flags);
652 if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags) ||
654 !test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
655 ++dev->stats.tx_dropped;
656 dev_kfree_skb_any(skb);
660 mcast = __ipoib_mcast_find(dev, mgid);
662 /* Let's create a new send only group now */
663 ipoib_dbg_mcast(priv, "setting up send only multicast group for %pI6\n",
666 mcast = ipoib_mcast_alloc(dev, 0);
668 ipoib_warn(priv, "unable to allocate memory for "
669 "multicast structure\n");
670 ++dev->stats.tx_dropped;
671 dev_kfree_skb_any(skb);
675 set_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags);
676 memcpy(mcast->mcmember.mgid.raw, mgid, sizeof (union ib_gid));
677 __ipoib_mcast_add(dev, mcast);
678 list_add_tail(&mcast->list, &priv->multicast_list);
682 if (skb_queue_len(&mcast->pkt_queue) < IPOIB_MAX_MCAST_QUEUE)
683 skb_queue_tail(&mcast->pkt_queue, skb);
685 ++dev->stats.tx_dropped;
686 dev_kfree_skb_any(skb);
689 if (test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
690 ipoib_dbg_mcast(priv, "no address vector, "
691 "but multicast join already started\n");
692 else if (test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags))
693 ipoib_mcast_sendonly_join(mcast);
696 * If lookup completes between here and out:, don't
697 * want to send packet twice.
703 if (mcast && mcast->ah) {
704 struct ipoib_neigh *neigh;
706 spin_unlock_irqrestore(&priv->lock, flags);
707 neigh = ipoib_neigh_get(dev, daddr);
708 spin_lock_irqsave(&priv->lock, flags);
710 neigh = ipoib_neigh_alloc(daddr, dev);
712 kref_get(&mcast->ah->ref);
713 neigh->ah = mcast->ah;
714 list_add_tail(&neigh->list, &mcast->neigh_list);
717 spin_unlock_irqrestore(&priv->lock, flags);
718 ipoib_send(dev, skb, mcast->ah, IB_MULTICAST_QPN);
720 ipoib_neigh_put(neigh);
725 spin_unlock_irqrestore(&priv->lock, flags);
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 static int ipoib_mcast_addr_is_valid(const u8 *addr, const u8 *broadcast)
761 /* reserved QPN, prefix, scope */
762 if (memcmp(addr, broadcast, 6))
764 /* signature lower, pkey */
765 if (memcmp(addr + 7, broadcast + 7, 3))
770 void ipoib_mcast_restart_task(struct work_struct *work)
772 struct ipoib_dev_priv *priv =
773 container_of(work, struct ipoib_dev_priv, restart_task);
774 struct net_device *dev = priv->dev;
775 struct netdev_hw_addr *ha;
776 struct ipoib_mcast *mcast, *tmcast;
777 LIST_HEAD(remove_list);
779 struct ib_sa_mcmember_rec rec;
781 ipoib_dbg_mcast(priv, "restarting multicast task\n");
783 ipoib_mcast_stop_thread(dev, 0);
785 local_irq_save(flags);
786 netif_addr_lock(dev);
787 spin_lock(&priv->lock);
790 * Unfortunately, the networking core only gives us a list of all of
791 * the multicast hardware addresses. We need to figure out which ones
792 * are new and which ones have been removed
795 /* Clear out the found flag */
796 list_for_each_entry(mcast, &priv->multicast_list, list)
797 clear_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
799 /* Mark all of the entries that are found or don't exist */
800 netdev_for_each_mc_addr(ha, dev) {
803 if (!ipoib_mcast_addr_is_valid(ha->addr, dev->broadcast))
806 memcpy(mgid.raw, ha->addr + 4, sizeof mgid);
808 mcast = __ipoib_mcast_find(dev, &mgid);
809 if (!mcast || test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
810 struct ipoib_mcast *nmcast;
812 /* ignore group which is directly joined by userspace */
813 if (test_bit(IPOIB_FLAG_UMCAST, &priv->flags) &&
814 !ib_sa_get_mcmember_rec(priv->ca, priv->port, &mgid, &rec)) {
815 ipoib_dbg_mcast(priv, "ignoring multicast entry for mgid %pI6\n",
820 /* Not found or send-only group, let's add a new entry */
821 ipoib_dbg_mcast(priv, "adding multicast entry for mgid %pI6\n",
824 nmcast = ipoib_mcast_alloc(dev, 0);
826 ipoib_warn(priv, "unable to allocate memory for multicast structure\n");
830 set_bit(IPOIB_MCAST_FLAG_FOUND, &nmcast->flags);
832 nmcast->mcmember.mgid = mgid;
835 /* Destroy the send only entry */
836 list_move_tail(&mcast->list, &remove_list);
838 rb_replace_node(&mcast->rb_node,
840 &priv->multicast_tree);
842 __ipoib_mcast_add(dev, nmcast);
844 list_add_tail(&nmcast->list, &priv->multicast_list);
848 set_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
851 /* Remove all of the entries don't exist anymore */
852 list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
853 if (!test_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags) &&
854 !test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
855 ipoib_dbg_mcast(priv, "deleting multicast group %pI6\n",
856 mcast->mcmember.mgid.raw);
858 rb_erase(&mcast->rb_node, &priv->multicast_tree);
860 /* Move to the remove list */
861 list_move_tail(&mcast->list, &remove_list);
865 spin_unlock(&priv->lock);
866 netif_addr_unlock(dev);
867 local_irq_restore(flags);
869 /* We have to cancel outside of the spinlock */
870 list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
871 ipoib_mcast_leave(mcast->dev, mcast);
872 ipoib_mcast_free(mcast);
875 if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
876 ipoib_mcast_start_thread(dev);
879 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
881 struct ipoib_mcast_iter *ipoib_mcast_iter_init(struct net_device *dev)
883 struct ipoib_mcast_iter *iter;
885 iter = kmalloc(sizeof *iter, GFP_KERNEL);
890 memset(iter->mgid.raw, 0, 16);
892 if (ipoib_mcast_iter_next(iter)) {
900 int ipoib_mcast_iter_next(struct ipoib_mcast_iter *iter)
902 struct ipoib_dev_priv *priv = netdev_priv(iter->dev);
904 struct ipoib_mcast *mcast;
907 spin_lock_irq(&priv->lock);
909 n = rb_first(&priv->multicast_tree);
912 mcast = rb_entry(n, struct ipoib_mcast, rb_node);
914 if (memcmp(iter->mgid.raw, mcast->mcmember.mgid.raw,
915 sizeof (union ib_gid)) < 0) {
916 iter->mgid = mcast->mcmember.mgid;
917 iter->created = mcast->created;
918 iter->queuelen = skb_queue_len(&mcast->pkt_queue);
919 iter->complete = !!mcast->ah;
920 iter->send_only = !!(mcast->flags & (1 << IPOIB_MCAST_FLAG_SENDONLY));
930 spin_unlock_irq(&priv->lock);
935 void ipoib_mcast_iter_read(struct ipoib_mcast_iter *iter,
937 unsigned long *created,
938 unsigned int *queuelen,
939 unsigned int *complete,
940 unsigned int *send_only)
943 *created = iter->created;
944 *queuelen = iter->queuelen;
945 *complete = iter->complete;
946 *send_only = iter->send_only;
949 #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */