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>
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 DECLARE_MUTEX(mcast_mutex
);
58 /* Used for all multicast joins (broadcast, IPv4 mcast and IPv6 mcast) */
60 struct ib_sa_mcmember_rec mcmember
;
63 struct rb_node rb_node
;
64 struct list_head list
;
65 struct completion done
;
68 struct ib_sa_query
*query
;
70 unsigned long created
;
71 unsigned long backoff
;
74 unsigned char logcount
;
76 struct list_head neigh_list
;
78 struct sk_buff_head pkt_queue
;
80 struct net_device
*dev
;
83 struct ipoib_mcast_iter
{
84 struct net_device
*dev
;
86 unsigned long created
;
87 unsigned int queuelen
;
88 unsigned int complete
;
89 unsigned int send_only
;
92 static void ipoib_mcast_free(struct ipoib_mcast
*mcast
)
94 struct net_device
*dev
= mcast
->dev
;
95 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
96 struct ipoib_neigh
*neigh
, *tmp
;
99 struct ipoib_ah
*ah
, *tah
;
101 ipoib_dbg_mcast(netdev_priv(dev
),
102 "deleting multicast group " IPOIB_GID_FMT
"\n",
103 IPOIB_GID_ARG(mcast
->mcmember
.mgid
));
105 spin_lock_irqsave(&priv
->lock
, flags
);
107 list_for_each_entry_safe(neigh
, tmp
, &mcast
->neigh_list
, list
) {
109 list_add_tail(&neigh
->ah
->list
, &ah_list
);
110 *to_ipoib_neigh(neigh
->neighbour
) = NULL
;
111 neigh
->neighbour
->ops
->destructor
= NULL
;
115 spin_unlock_irqrestore(&priv
->lock
, flags
);
117 list_for_each_entry_safe(ah
, tah
, &ah_list
, list
)
121 ipoib_put_ah(mcast
->ah
);
123 while (!skb_queue_empty(&mcast
->pkt_queue
)) {
124 struct sk_buff
*skb
= skb_dequeue(&mcast
->pkt_queue
);
127 dev_kfree_skb_any(skb
);
133 static struct ipoib_mcast
*ipoib_mcast_alloc(struct net_device
*dev
,
136 struct ipoib_mcast
*mcast
;
138 mcast
= kmalloc(sizeof (*mcast
), can_sleep
? GFP_KERNEL
: GFP_ATOMIC
);
142 memset(mcast
, 0, sizeof (*mcast
));
144 init_completion(&mcast
->done
);
147 mcast
->created
= jiffies
;
151 INIT_LIST_HEAD(&mcast
->list
);
152 INIT_LIST_HEAD(&mcast
->neigh_list
);
153 skb_queue_head_init(&mcast
->pkt_queue
);
161 static struct ipoib_mcast
*__ipoib_mcast_find(struct net_device
*dev
, union ib_gid
*mgid
)
163 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
164 struct rb_node
*n
= priv
->multicast_tree
.rb_node
;
167 struct ipoib_mcast
*mcast
;
170 mcast
= rb_entry(n
, struct ipoib_mcast
, rb_node
);
172 ret
= memcmp(mgid
->raw
, mcast
->mcmember
.mgid
.raw
,
173 sizeof (union ib_gid
));
185 static int __ipoib_mcast_add(struct net_device
*dev
, struct ipoib_mcast
*mcast
)
187 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
188 struct rb_node
**n
= &priv
->multicast_tree
.rb_node
, *pn
= NULL
;
191 struct ipoib_mcast
*tmcast
;
195 tmcast
= rb_entry(pn
, struct ipoib_mcast
, rb_node
);
197 ret
= memcmp(mcast
->mcmember
.mgid
.raw
, tmcast
->mcmember
.mgid
.raw
,
198 sizeof (union ib_gid
));
207 rb_link_node(&mcast
->rb_node
, pn
, n
);
208 rb_insert_color(&mcast
->rb_node
, &priv
->multicast_tree
);
213 static int ipoib_mcast_join_finish(struct ipoib_mcast
*mcast
,
214 struct ib_sa_mcmember_rec
*mcmember
)
216 struct net_device
*dev
= mcast
->dev
;
217 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
220 mcast
->mcmember
= *mcmember
;
222 /* Set the cached Q_Key before we attach if it's the broadcast group */
223 if (!memcmp(mcast
->mcmember
.mgid
.raw
, priv
->dev
->broadcast
+ 4,
224 sizeof (union ib_gid
))) {
225 priv
->qkey
= be32_to_cpu(priv
->broadcast
->mcmember
.qkey
);
226 priv
->tx_wr
.wr
.ud
.remote_qkey
= priv
->qkey
;
229 if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY
, &mcast
->flags
)) {
230 if (test_and_set_bit(IPOIB_MCAST_FLAG_ATTACHED
, &mcast
->flags
)) {
231 ipoib_warn(priv
, "multicast group " IPOIB_GID_FMT
232 " already attached\n",
233 IPOIB_GID_ARG(mcast
->mcmember
.mgid
));
238 ret
= ipoib_mcast_attach(dev
, be16_to_cpu(mcast
->mcmember
.mlid
),
239 &mcast
->mcmember
.mgid
);
241 ipoib_warn(priv
, "couldn't attach QP to multicast group "
243 IPOIB_GID_ARG(mcast
->mcmember
.mgid
));
245 clear_bit(IPOIB_MCAST_FLAG_ATTACHED
, &mcast
->flags
);
251 struct ib_ah_attr av
= {
252 .dlid
= be16_to_cpu(mcast
->mcmember
.mlid
),
253 .port_num
= priv
->port
,
254 .sl
= mcast
->mcmember
.sl
,
255 .ah_flags
= IB_AH_GRH
,
257 .flow_label
= be32_to_cpu(mcast
->mcmember
.flow_label
),
258 .hop_limit
= mcast
->mcmember
.hop_limit
,
260 .traffic_class
= mcast
->mcmember
.traffic_class
263 int path_rate
= ib_sa_rate_enum_to_int(mcast
->mcmember
.rate
);
265 av
.grh
.dgid
= mcast
->mcmember
.mgid
;
267 if (path_rate
> 0 && priv
->local_rate
> path_rate
)
268 av
.static_rate
= (priv
->local_rate
- 1) / path_rate
;
270 ipoib_dbg_mcast(priv
, "static_rate %d for local port %dX, mcmember %dX\n",
271 av
.static_rate
, priv
->local_rate
,
272 ib_sa_rate_enum_to_int(mcast
->mcmember
.rate
));
274 mcast
->ah
= ipoib_create_ah(dev
, priv
->pd
, &av
);
276 ipoib_warn(priv
, "ib_address_create failed\n");
278 ipoib_dbg_mcast(priv
, "MGID " IPOIB_GID_FMT
279 " AV %p, LID 0x%04x, SL %d\n",
280 IPOIB_GID_ARG(mcast
->mcmember
.mgid
),
282 be16_to_cpu(mcast
->mcmember
.mlid
),
287 /* actually send any queued packets */
288 while (!skb_queue_empty(&mcast
->pkt_queue
)) {
289 struct sk_buff
*skb
= skb_dequeue(&mcast
->pkt_queue
);
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");
306 ipoib_mcast_sendonly_join_complete(int status
,
307 struct ib_sa_mcmember_rec
*mcmember
,
310 struct ipoib_mcast
*mcast
= mcast_ptr
;
311 struct net_device
*dev
= mcast
->dev
;
314 ipoib_mcast_join_finish(mcast
, mcmember
);
316 if (mcast
->logcount
++ < 20)
317 ipoib_dbg_mcast(netdev_priv(dev
), "multicast join failed for "
318 IPOIB_GID_FMT
", status %d\n",
319 IPOIB_GID_ARG(mcast
->mcmember
.mgid
), status
);
321 /* Flush out any queued packets */
322 while (!skb_queue_empty(&mcast
->pkt_queue
)) {
323 struct sk_buff
*skb
= skb_dequeue(&mcast
->pkt_queue
);
327 dev_kfree_skb_any(skb
);
330 /* Clear the busy flag so we try again */
331 clear_bit(IPOIB_MCAST_FLAG_BUSY
, &mcast
->flags
);
334 complete(&mcast
->done
);
337 static int ipoib_mcast_sendonly_join(struct ipoib_mcast
*mcast
)
339 struct net_device
*dev
= mcast
->dev
;
340 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
341 struct ib_sa_mcmember_rec rec
= {
342 #if 0 /* Some SMs don't support send-only yet */
350 if (!test_bit(IPOIB_FLAG_OPER_UP
, &priv
->flags
)) {
351 ipoib_dbg_mcast(priv
, "device shutting down, no multicast joins\n");
355 if (test_and_set_bit(IPOIB_MCAST_FLAG_BUSY
, &mcast
->flags
)) {
356 ipoib_dbg_mcast(priv
, "multicast entry busy, skipping\n");
360 rec
.mgid
= mcast
->mcmember
.mgid
;
361 rec
.port_gid
= priv
->local_gid
;
362 rec
.pkey
= cpu_to_be16(priv
->pkey
);
364 ret
= ib_sa_mcmember_rec_set(priv
->ca
, priv
->port
, &rec
,
365 IB_SA_MCMEMBER_REC_MGID
|
366 IB_SA_MCMEMBER_REC_PORT_GID
|
367 IB_SA_MCMEMBER_REC_PKEY
|
368 IB_SA_MCMEMBER_REC_JOIN_STATE
,
370 ipoib_mcast_sendonly_join_complete
,
371 mcast
, &mcast
->query
);
373 ipoib_warn(priv
, "ib_sa_mcmember_rec_set failed (ret = %d)\n",
376 ipoib_dbg_mcast(priv
, "no multicast record for " IPOIB_GID_FMT
378 IPOIB_GID_ARG(mcast
->mcmember
.mgid
));
380 mcast
->query_id
= ret
;
386 static void ipoib_mcast_join_complete(int status
,
387 struct ib_sa_mcmember_rec
*mcmember
,
390 struct ipoib_mcast
*mcast
= mcast_ptr
;
391 struct net_device
*dev
= mcast
->dev
;
392 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
394 ipoib_dbg_mcast(priv
, "join completion for " IPOIB_GID_FMT
396 IPOIB_GID_ARG(mcast
->mcmember
.mgid
), status
);
398 if (!status
&& !ipoib_mcast_join_finish(mcast
, mcmember
)) {
401 if (test_bit(IPOIB_MCAST_RUN
, &priv
->flags
))
402 queue_work(ipoib_workqueue
, &priv
->mcast_task
);
404 complete(&mcast
->done
);
408 if (status
== -EINTR
) {
409 complete(&mcast
->done
);
413 if (status
&& mcast
->logcount
++ < 20) {
414 if (status
== -ETIMEDOUT
|| status
== -EINTR
) {
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
;
434 if (test_bit(IPOIB_MCAST_RUN
, &priv
->flags
)) {
435 if (status
== -ETIMEDOUT
)
436 queue_work(ipoib_workqueue
, &priv
->mcast_task
);
438 queue_delayed_work(ipoib_workqueue
, &priv
->mcast_task
,
439 mcast
->backoff
* HZ
);
441 complete(&mcast
->done
);
447 static void ipoib_mcast_join(struct net_device
*dev
, struct ipoib_mcast
*mcast
,
450 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
451 struct ib_sa_mcmember_rec rec
= {
454 ib_sa_comp_mask comp_mask
;
457 ipoib_dbg_mcast(priv
, "joining MGID " IPOIB_GID_FMT
"\n",
458 IPOIB_GID_ARG(mcast
->mcmember
.mgid
));
460 rec
.mgid
= mcast
->mcmember
.mgid
;
461 rec
.port_gid
= priv
->local_gid
;
462 rec
.pkey
= cpu_to_be16(priv
->pkey
);
465 IB_SA_MCMEMBER_REC_MGID
|
466 IB_SA_MCMEMBER_REC_PORT_GID
|
467 IB_SA_MCMEMBER_REC_PKEY
|
468 IB_SA_MCMEMBER_REC_JOIN_STATE
;
472 IB_SA_MCMEMBER_REC_QKEY
|
473 IB_SA_MCMEMBER_REC_SL
|
474 IB_SA_MCMEMBER_REC_FLOW_LABEL
|
475 IB_SA_MCMEMBER_REC_TRAFFIC_CLASS
;
477 rec
.qkey
= priv
->broadcast
->mcmember
.qkey
;
478 rec
.sl
= priv
->broadcast
->mcmember
.sl
;
479 rec
.flow_label
= priv
->broadcast
->mcmember
.flow_label
;
480 rec
.traffic_class
= priv
->broadcast
->mcmember
.traffic_class
;
483 ret
= ib_sa_mcmember_rec_set(priv
->ca
, priv
->port
, &rec
, comp_mask
,
484 mcast
->backoff
* 1000, GFP_ATOMIC
,
485 ipoib_mcast_join_complete
,
486 mcast
, &mcast
->query
);
489 ipoib_warn(priv
, "ib_sa_mcmember_rec_set failed, status %d\n", ret
);
492 if (mcast
->backoff
> IPOIB_MAX_BACKOFF_SECONDS
)
493 mcast
->backoff
= IPOIB_MAX_BACKOFF_SECONDS
;
496 if (test_bit(IPOIB_MCAST_RUN
, &priv
->flags
))
497 queue_delayed_work(ipoib_workqueue
,
499 mcast
->backoff
* HZ
);
502 mcast
->query_id
= ret
;
505 void ipoib_mcast_join_task(void *dev_ptr
)
507 struct net_device
*dev
= dev_ptr
;
508 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
510 if (!test_bit(IPOIB_MCAST_RUN
, &priv
->flags
))
513 if (ib_query_gid(priv
->ca
, priv
->port
, 0, &priv
->local_gid
))
514 ipoib_warn(priv
, "ib_gid_entry_get() failed\n");
516 memcpy(priv
->dev
->dev_addr
+ 4, priv
->local_gid
.raw
, sizeof (union ib_gid
));
519 struct ib_port_attr attr
;
521 if (!ib_query_port(priv
->ca
, priv
->port
, &attr
)) {
522 priv
->local_lid
= attr
.lid
;
523 priv
->local_rate
= attr
.active_speed
*
524 ib_width_enum_to_int(attr
.active_width
);
526 ipoib_warn(priv
, "ib_query_port failed\n");
529 if (!priv
->broadcast
) {
530 priv
->broadcast
= ipoib_mcast_alloc(dev
, 1);
531 if (!priv
->broadcast
) {
532 ipoib_warn(priv
, "failed to allocate broadcast group\n");
534 if (test_bit(IPOIB_MCAST_RUN
, &priv
->flags
))
535 queue_delayed_work(ipoib_workqueue
,
536 &priv
->mcast_task
, HZ
);
541 memcpy(priv
->broadcast
->mcmember
.mgid
.raw
, priv
->dev
->broadcast
+ 4,
542 sizeof (union ib_gid
));
544 spin_lock_irq(&priv
->lock
);
545 __ipoib_mcast_add(dev
, priv
->broadcast
);
546 spin_unlock_irq(&priv
->lock
);
549 if (!test_bit(IPOIB_MCAST_FLAG_ATTACHED
, &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
= ib_mtu_enum_to_int(priv
->broadcast
->mcmember
.mtu
) -
579 dev
->mtu
= min(priv
->mcast_mtu
, priv
->admin_mtu
);
581 ipoib_dbg_mcast(priv
, "successfully joined all multicast groups\n");
583 clear_bit(IPOIB_MCAST_RUN
, &priv
->flags
);
584 netif_carrier_on(dev
);
587 int ipoib_mcast_start_thread(struct net_device
*dev
)
589 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
591 ipoib_dbg_mcast(priv
, "starting multicast thread\n");
594 if (!test_and_set_bit(IPOIB_MCAST_RUN
, &priv
->flags
))
595 queue_work(ipoib_workqueue
, &priv
->mcast_task
);
601 int ipoib_mcast_stop_thread(struct net_device
*dev
, int flush
)
603 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
604 struct ipoib_mcast
*mcast
;
606 ipoib_dbg_mcast(priv
, "stopping multicast thread\n");
609 clear_bit(IPOIB_MCAST_RUN
, &priv
->flags
);
610 cancel_delayed_work(&priv
->mcast_task
);
614 flush_workqueue(ipoib_workqueue
);
616 if (priv
->broadcast
&& priv
->broadcast
->query
) {
617 ib_sa_cancel_query(priv
->broadcast
->query_id
, priv
->broadcast
->query
);
618 priv
->broadcast
->query
= NULL
;
619 ipoib_dbg_mcast(priv
, "waiting for bcast\n");
620 wait_for_completion(&priv
->broadcast
->done
);
623 list_for_each_entry(mcast
, &priv
->multicast_list
, list
) {
625 ib_sa_cancel_query(mcast
->query_id
, mcast
->query
);
627 ipoib_dbg_mcast(priv
, "waiting for MGID " IPOIB_GID_FMT
"\n",
628 IPOIB_GID_ARG(mcast
->mcmember
.mgid
));
629 wait_for_completion(&mcast
->done
);
636 static int ipoib_mcast_leave(struct net_device
*dev
, struct ipoib_mcast
*mcast
)
638 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
639 struct ib_sa_mcmember_rec rec
= {
644 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 rec
.mgid
= mcast
->mcmember
.mgid
;
651 rec
.port_gid
= priv
->local_gid
;
652 rec
.pkey
= cpu_to_be16(priv
->pkey
);
654 /* Remove ourselves from the multicast group */
655 ret
= ipoib_mcast_detach(dev
, be16_to_cpu(mcast
->mcmember
.mlid
),
656 &mcast
->mcmember
.mgid
);
658 ipoib_warn(priv
, "ipoib_mcast_detach failed (result = %d)\n", ret
);
661 * Just make one shot at leaving and don't wait for a reply;
662 * if we fail, too bad.
664 ret
= ib_sa_mcmember_rec_delete(priv
->ca
, priv
->port
, &rec
,
665 IB_SA_MCMEMBER_REC_MGID
|
666 IB_SA_MCMEMBER_REC_PORT_GID
|
667 IB_SA_MCMEMBER_REC_PKEY
|
668 IB_SA_MCMEMBER_REC_JOIN_STATE
,
670 mcast
, &mcast
->query
);
672 ipoib_warn(priv
, "ib_sa_mcmember_rec_delete failed "
673 "for leave (result = %d)\n", ret
);
678 void ipoib_mcast_send(struct net_device
*dev
, union ib_gid
*mgid
,
681 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
682 struct ipoib_mcast
*mcast
;
685 * We can only be called from ipoib_start_xmit, so we're
686 * inside tx_lock -- no need to save/restore flags.
688 spin_lock(&priv
->lock
);
690 mcast
= __ipoib_mcast_find(dev
, mgid
);
692 /* Let's create a new send only group now */
693 ipoib_dbg_mcast(priv
, "setting up send only multicast group for "
694 IPOIB_GID_FMT
"\n", IPOIB_GID_ARG(*mgid
));
696 mcast
= ipoib_mcast_alloc(dev
, 0);
698 ipoib_warn(priv
, "unable to allocate memory for "
699 "multicast structure\n");
700 dev_kfree_skb_any(skb
);
704 set_bit(IPOIB_MCAST_FLAG_SENDONLY
, &mcast
->flags
);
705 mcast
->mcmember
.mgid
= *mgid
;
706 __ipoib_mcast_add(dev
, mcast
);
707 list_add_tail(&mcast
->list
, &priv
->multicast_list
);
711 if (skb_queue_len(&mcast
->pkt_queue
) < IPOIB_MAX_MCAST_QUEUE
)
712 skb_queue_tail(&mcast
->pkt_queue
, skb
);
714 dev_kfree_skb_any(skb
);
717 ipoib_dbg_mcast(priv
, "no address vector, "
718 "but multicast join already started\n");
719 else if (test_bit(IPOIB_MCAST_FLAG_SENDONLY
, &mcast
->flags
))
720 ipoib_mcast_sendonly_join(mcast
);
723 * If lookup completes between here and out:, don't
724 * want to send packet twice.
730 if (mcast
&& mcast
->ah
) {
732 skb
->dst
->neighbour
&&
733 !*to_ipoib_neigh(skb
->dst
->neighbour
)) {
734 struct ipoib_neigh
*neigh
= kmalloc(sizeof *neigh
, GFP_ATOMIC
);
737 kref_get(&mcast
->ah
->ref
);
738 neigh
->ah
= mcast
->ah
;
739 neigh
->neighbour
= skb
->dst
->neighbour
;
740 *to_ipoib_neigh(skb
->dst
->neighbour
) = neigh
;
741 list_add_tail(&neigh
->list
, &mcast
->neigh_list
);
745 ipoib_send(dev
, skb
, mcast
->ah
, IB_MULTICAST_QPN
);
748 spin_unlock(&priv
->lock
);
751 void ipoib_mcast_dev_flush(struct net_device
*dev
)
753 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
754 LIST_HEAD(remove_list
);
755 struct ipoib_mcast
*mcast
, *tmcast
, *nmcast
;
758 ipoib_dbg_mcast(priv
, "flushing multicast list\n");
760 spin_lock_irqsave(&priv
->lock
, flags
);
761 list_for_each_entry_safe(mcast
, tmcast
, &priv
->multicast_list
, list
) {
762 nmcast
= ipoib_mcast_alloc(dev
, 0);
765 mcast
->flags
& (1 << IPOIB_MCAST_FLAG_SENDONLY
);
767 nmcast
->mcmember
.mgid
= mcast
->mcmember
.mgid
;
769 /* Add the new group in before the to-be-destroyed group */
770 list_add_tail(&nmcast
->list
, &mcast
->list
);
771 list_del_init(&mcast
->list
);
773 rb_replace_node(&mcast
->rb_node
, &nmcast
->rb_node
,
774 &priv
->multicast_tree
);
776 list_add_tail(&mcast
->list
, &remove_list
);
778 ipoib_warn(priv
, "could not reallocate multicast group "
780 IPOIB_GID_ARG(mcast
->mcmember
.mgid
));
784 if (priv
->broadcast
) {
785 nmcast
= ipoib_mcast_alloc(dev
, 0);
787 nmcast
->mcmember
.mgid
= priv
->broadcast
->mcmember
.mgid
;
789 rb_replace_node(&priv
->broadcast
->rb_node
,
791 &priv
->multicast_tree
);
793 list_add_tail(&priv
->broadcast
->list
, &remove_list
);
796 priv
->broadcast
= nmcast
;
799 spin_unlock_irqrestore(&priv
->lock
, flags
);
801 list_for_each_entry_safe(mcast
, tmcast
, &remove_list
, list
) {
802 ipoib_mcast_leave(dev
, mcast
);
803 ipoib_mcast_free(mcast
);
807 void ipoib_mcast_dev_down(struct net_device
*dev
)
809 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
812 /* Delete broadcast since it will be recreated */
813 if (priv
->broadcast
) {
814 ipoib_dbg_mcast(priv
, "deleting broadcast group\n");
816 spin_lock_irqsave(&priv
->lock
, flags
);
817 rb_erase(&priv
->broadcast
->rb_node
, &priv
->multicast_tree
);
818 spin_unlock_irqrestore(&priv
->lock
, flags
);
819 ipoib_mcast_leave(dev
, priv
->broadcast
);
820 ipoib_mcast_free(priv
->broadcast
);
821 priv
->broadcast
= NULL
;
825 void ipoib_mcast_restart_task(void *dev_ptr
)
827 struct net_device
*dev
= dev_ptr
;
828 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
829 struct dev_mc_list
*mclist
;
830 struct ipoib_mcast
*mcast
, *tmcast
;
831 LIST_HEAD(remove_list
);
834 ipoib_dbg_mcast(priv
, "restarting multicast task\n");
836 ipoib_mcast_stop_thread(dev
, 0);
838 spin_lock_irqsave(&priv
->lock
, flags
);
841 * Unfortunately, the networking core only gives us a list of all of
842 * the multicast hardware addresses. We need to figure out which ones
843 * are new and which ones have been removed
846 /* Clear out the found flag */
847 list_for_each_entry(mcast
, &priv
->multicast_list
, list
)
848 clear_bit(IPOIB_MCAST_FLAG_FOUND
, &mcast
->flags
);
850 /* Mark all of the entries that are found or don't exist */
851 for (mclist
= dev
->mc_list
; mclist
; mclist
= mclist
->next
) {
854 memcpy(mgid
.raw
, mclist
->dmi_addr
+ 4, sizeof mgid
);
856 /* Add in the P_Key */
857 mgid
.raw
[4] = (priv
->pkey
>> 8) & 0xff;
858 mgid
.raw
[5] = priv
->pkey
& 0xff;
860 mcast
= __ipoib_mcast_find(dev
, &mgid
);
861 if (!mcast
|| test_bit(IPOIB_MCAST_FLAG_SENDONLY
, &mcast
->flags
)) {
862 struct ipoib_mcast
*nmcast
;
864 /* Not found or send-only group, let's add a new entry */
865 ipoib_dbg_mcast(priv
, "adding multicast entry for mgid "
866 IPOIB_GID_FMT
"\n", IPOIB_GID_ARG(mgid
));
868 nmcast
= ipoib_mcast_alloc(dev
, 0);
870 ipoib_warn(priv
, "unable to allocate memory for multicast structure\n");
874 set_bit(IPOIB_MCAST_FLAG_FOUND
, &nmcast
->flags
);
876 nmcast
->mcmember
.mgid
= mgid
;
879 /* Destroy the send only entry */
880 list_del(&mcast
->list
);
881 list_add_tail(&mcast
->list
, &remove_list
);
883 rb_replace_node(&mcast
->rb_node
,
885 &priv
->multicast_tree
);
887 __ipoib_mcast_add(dev
, nmcast
);
889 list_add_tail(&nmcast
->list
, &priv
->multicast_list
);
893 set_bit(IPOIB_MCAST_FLAG_FOUND
, &mcast
->flags
);
896 /* Remove all of the entries don't exist anymore */
897 list_for_each_entry_safe(mcast
, tmcast
, &priv
->multicast_list
, list
) {
898 if (!test_bit(IPOIB_MCAST_FLAG_FOUND
, &mcast
->flags
) &&
899 !test_bit(IPOIB_MCAST_FLAG_SENDONLY
, &mcast
->flags
)) {
900 ipoib_dbg_mcast(priv
, "deleting multicast group " IPOIB_GID_FMT
"\n",
901 IPOIB_GID_ARG(mcast
->mcmember
.mgid
));
903 rb_erase(&mcast
->rb_node
, &priv
->multicast_tree
);
905 /* Move to the remove list */
906 list_del(&mcast
->list
);
907 list_add_tail(&mcast
->list
, &remove_list
);
910 spin_unlock_irqrestore(&priv
->lock
, flags
);
912 /* We have to cancel outside of the spinlock */
913 list_for_each_entry_safe(mcast
, tmcast
, &remove_list
, list
) {
914 ipoib_mcast_leave(mcast
->dev
, mcast
);
915 ipoib_mcast_free(mcast
);
918 if (test_bit(IPOIB_FLAG_ADMIN_UP
, &priv
->flags
))
919 ipoib_mcast_start_thread(dev
);
922 struct ipoib_mcast_iter
*ipoib_mcast_iter_init(struct net_device
*dev
)
924 struct ipoib_mcast_iter
*iter
;
926 iter
= kmalloc(sizeof *iter
, GFP_KERNEL
);
931 memset(iter
->mgid
.raw
, 0, sizeof iter
->mgid
);
933 if (ipoib_mcast_iter_next(iter
)) {
934 ipoib_mcast_iter_free(iter
);
941 void ipoib_mcast_iter_free(struct ipoib_mcast_iter
*iter
)
946 int ipoib_mcast_iter_next(struct ipoib_mcast_iter
*iter
)
948 struct ipoib_dev_priv
*priv
= netdev_priv(iter
->dev
);
950 struct ipoib_mcast
*mcast
;
953 spin_lock_irq(&priv
->lock
);
955 n
= rb_first(&priv
->multicast_tree
);
958 mcast
= rb_entry(n
, struct ipoib_mcast
, rb_node
);
960 if (memcmp(iter
->mgid
.raw
, mcast
->mcmember
.mgid
.raw
,
961 sizeof (union ib_gid
)) < 0) {
962 iter
->mgid
= mcast
->mcmember
.mgid
;
963 iter
->created
= mcast
->created
;
964 iter
->queuelen
= skb_queue_len(&mcast
->pkt_queue
);
965 iter
->complete
= !!mcast
->ah
;
966 iter
->send_only
= !!(mcast
->flags
& (1 << IPOIB_MCAST_FLAG_SENDONLY
));
976 spin_unlock_irq(&priv
->lock
);
981 void ipoib_mcast_iter_read(struct ipoib_mcast_iter
*iter
,
983 unsigned long *created
,
984 unsigned int *queuelen
,
985 unsigned int *complete
,
986 unsigned int *send_only
)
989 *created
= iter
->created
;
990 *queuelen
= iter
->queuelen
;
991 *complete
= iter
->complete
;
992 *send_only
= iter
->send_only
;