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
= kzalloc(sizeof *mcast
, can_sleep
? GFP_KERNEL
: GFP_ATOMIC
);
142 init_completion(&mcast
->done
);
145 mcast
->created
= jiffies
;
149 INIT_LIST_HEAD(&mcast
->list
);
150 INIT_LIST_HEAD(&mcast
->neigh_list
);
151 skb_queue_head_init(&mcast
->pkt_queue
);
159 static struct ipoib_mcast
*__ipoib_mcast_find(struct net_device
*dev
, union ib_gid
*mgid
)
161 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
162 struct rb_node
*n
= priv
->multicast_tree
.rb_node
;
165 struct ipoib_mcast
*mcast
;
168 mcast
= rb_entry(n
, struct ipoib_mcast
, rb_node
);
170 ret
= memcmp(mgid
->raw
, mcast
->mcmember
.mgid
.raw
,
171 sizeof (union ib_gid
));
183 static int __ipoib_mcast_add(struct net_device
*dev
, struct ipoib_mcast
*mcast
)
185 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
186 struct rb_node
**n
= &priv
->multicast_tree
.rb_node
, *pn
= NULL
;
189 struct ipoib_mcast
*tmcast
;
193 tmcast
= rb_entry(pn
, struct ipoib_mcast
, rb_node
);
195 ret
= memcmp(mcast
->mcmember
.mgid
.raw
, tmcast
->mcmember
.mgid
.raw
,
196 sizeof (union ib_gid
));
205 rb_link_node(&mcast
->rb_node
, pn
, n
);
206 rb_insert_color(&mcast
->rb_node
, &priv
->multicast_tree
);
211 static int ipoib_mcast_join_finish(struct ipoib_mcast
*mcast
,
212 struct ib_sa_mcmember_rec
*mcmember
)
214 struct net_device
*dev
= mcast
->dev
;
215 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
218 mcast
->mcmember
= *mcmember
;
220 /* Set the cached Q_Key before we attach if it's the broadcast group */
221 if (!memcmp(mcast
->mcmember
.mgid
.raw
, priv
->dev
->broadcast
+ 4,
222 sizeof (union ib_gid
))) {
223 priv
->qkey
= be32_to_cpu(priv
->broadcast
->mcmember
.qkey
);
224 priv
->tx_wr
.wr
.ud
.remote_qkey
= priv
->qkey
;
227 if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY
, &mcast
->flags
)) {
228 if (test_and_set_bit(IPOIB_MCAST_FLAG_ATTACHED
, &mcast
->flags
)) {
229 ipoib_warn(priv
, "multicast group " IPOIB_GID_FMT
230 " already attached\n",
231 IPOIB_GID_ARG(mcast
->mcmember
.mgid
));
236 ret
= ipoib_mcast_attach(dev
, be16_to_cpu(mcast
->mcmember
.mlid
),
237 &mcast
->mcmember
.mgid
);
239 ipoib_warn(priv
, "couldn't attach QP to multicast group "
241 IPOIB_GID_ARG(mcast
->mcmember
.mgid
));
243 clear_bit(IPOIB_MCAST_FLAG_ATTACHED
, &mcast
->flags
);
249 struct ib_ah_attr av
= {
250 .dlid
= be16_to_cpu(mcast
->mcmember
.mlid
),
251 .port_num
= priv
->port
,
252 .sl
= mcast
->mcmember
.sl
,
253 .ah_flags
= IB_AH_GRH
,
255 .flow_label
= be32_to_cpu(mcast
->mcmember
.flow_label
),
256 .hop_limit
= mcast
->mcmember
.hop_limit
,
258 .traffic_class
= mcast
->mcmember
.traffic_class
261 int path_rate
= ib_sa_rate_enum_to_int(mcast
->mcmember
.rate
);
263 av
.grh
.dgid
= mcast
->mcmember
.mgid
;
265 if (path_rate
> 0 && priv
->local_rate
> path_rate
)
266 av
.static_rate
= (priv
->local_rate
- 1) / path_rate
;
268 ipoib_dbg_mcast(priv
, "static_rate %d for local port %dX, mcmember %dX\n",
269 av
.static_rate
, priv
->local_rate
,
270 ib_sa_rate_enum_to_int(mcast
->mcmember
.rate
));
272 mcast
->ah
= ipoib_create_ah(dev
, priv
->pd
, &av
);
274 ipoib_warn(priv
, "ib_address_create failed\n");
276 ipoib_dbg_mcast(priv
, "MGID " IPOIB_GID_FMT
277 " AV %p, LID 0x%04x, SL %d\n",
278 IPOIB_GID_ARG(mcast
->mcmember
.mgid
),
280 be16_to_cpu(mcast
->mcmember
.mlid
),
285 /* actually send any queued packets */
286 while (!skb_queue_empty(&mcast
->pkt_queue
)) {
287 struct sk_buff
*skb
= skb_dequeue(&mcast
->pkt_queue
);
291 if (!skb
->dst
|| !skb
->dst
->neighbour
) {
292 /* put pseudoheader back on for next time */
293 skb_push(skb
, sizeof (struct ipoib_pseudoheader
));
296 if (dev_queue_xmit(skb
))
297 ipoib_warn(priv
, "dev_queue_xmit failed to requeue packet\n");
304 ipoib_mcast_sendonly_join_complete(int status
,
305 struct ib_sa_mcmember_rec
*mcmember
,
308 struct ipoib_mcast
*mcast
= mcast_ptr
;
309 struct net_device
*dev
= mcast
->dev
;
312 ipoib_mcast_join_finish(mcast
, mcmember
);
314 if (mcast
->logcount
++ < 20)
315 ipoib_dbg_mcast(netdev_priv(dev
), "multicast join failed for "
316 IPOIB_GID_FMT
", status %d\n",
317 IPOIB_GID_ARG(mcast
->mcmember
.mgid
), status
);
319 /* Flush out any queued packets */
320 while (!skb_queue_empty(&mcast
->pkt_queue
)) {
321 struct sk_buff
*skb
= skb_dequeue(&mcast
->pkt_queue
);
325 dev_kfree_skb_any(skb
);
328 /* Clear the busy flag so we try again */
329 clear_bit(IPOIB_MCAST_FLAG_BUSY
, &mcast
->flags
);
332 complete(&mcast
->done
);
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 ret
= ib_sa_mcmember_rec_set(priv
->ca
, priv
->port
, &rec
,
363 IB_SA_MCMEMBER_REC_MGID
|
364 IB_SA_MCMEMBER_REC_PORT_GID
|
365 IB_SA_MCMEMBER_REC_PKEY
|
366 IB_SA_MCMEMBER_REC_JOIN_STATE
,
368 ipoib_mcast_sendonly_join_complete
,
369 mcast
, &mcast
->query
);
371 ipoib_warn(priv
, "ib_sa_mcmember_rec_set failed (ret = %d)\n",
374 ipoib_dbg_mcast(priv
, "no multicast record for " IPOIB_GID_FMT
376 IPOIB_GID_ARG(mcast
->mcmember
.mgid
));
378 mcast
->query_id
= ret
;
384 static void ipoib_mcast_join_complete(int status
,
385 struct ib_sa_mcmember_rec
*mcmember
,
388 struct ipoib_mcast
*mcast
= mcast_ptr
;
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 if (!status
&& !ipoib_mcast_join_finish(mcast
, mcmember
)) {
399 if (test_bit(IPOIB_MCAST_RUN
, &priv
->flags
))
400 queue_work(ipoib_workqueue
, &priv
->mcast_task
);
402 complete(&mcast
->done
);
406 if (status
== -EINTR
) {
407 complete(&mcast
->done
);
411 if (status
&& mcast
->logcount
++ < 20) {
412 if (status
== -ETIMEDOUT
|| status
== -EINTR
) {
413 ipoib_dbg_mcast(priv
, "multicast join failed for " IPOIB_GID_FMT
415 IPOIB_GID_ARG(mcast
->mcmember
.mgid
),
418 ipoib_warn(priv
, "multicast join failed for "
419 IPOIB_GID_FMT
", status %d\n",
420 IPOIB_GID_ARG(mcast
->mcmember
.mgid
),
426 if (mcast
->backoff
> IPOIB_MAX_BACKOFF_SECONDS
)
427 mcast
->backoff
= IPOIB_MAX_BACKOFF_SECONDS
;
432 if (test_bit(IPOIB_MCAST_RUN
, &priv
->flags
)) {
433 if (status
== -ETIMEDOUT
)
434 queue_work(ipoib_workqueue
, &priv
->mcast_task
);
436 queue_delayed_work(ipoib_workqueue
, &priv
->mcast_task
,
437 mcast
->backoff
* HZ
);
439 complete(&mcast
->done
);
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_SL
|
472 IB_SA_MCMEMBER_REC_FLOW_LABEL
|
473 IB_SA_MCMEMBER_REC_TRAFFIC_CLASS
;
475 rec
.qkey
= priv
->broadcast
->mcmember
.qkey
;
476 rec
.sl
= priv
->broadcast
->mcmember
.sl
;
477 rec
.flow_label
= priv
->broadcast
->mcmember
.flow_label
;
478 rec
.traffic_class
= priv
->broadcast
->mcmember
.traffic_class
;
481 ret
= ib_sa_mcmember_rec_set(priv
->ca
, priv
->port
, &rec
, comp_mask
,
482 mcast
->backoff
* 1000, GFP_ATOMIC
,
483 ipoib_mcast_join_complete
,
484 mcast
, &mcast
->query
);
487 ipoib_warn(priv
, "ib_sa_mcmember_rec_set failed, status %d\n", ret
);
490 if (mcast
->backoff
> IPOIB_MAX_BACKOFF_SECONDS
)
491 mcast
->backoff
= IPOIB_MAX_BACKOFF_SECONDS
;
494 if (test_bit(IPOIB_MCAST_RUN
, &priv
->flags
))
495 queue_delayed_work(ipoib_workqueue
,
497 mcast
->backoff
* HZ
);
500 mcast
->query_id
= ret
;
503 void ipoib_mcast_join_task(void *dev_ptr
)
505 struct net_device
*dev
= dev_ptr
;
506 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
508 if (!test_bit(IPOIB_MCAST_RUN
, &priv
->flags
))
511 if (ib_query_gid(priv
->ca
, priv
->port
, 0, &priv
->local_gid
))
512 ipoib_warn(priv
, "ib_gid_entry_get() failed\n");
514 memcpy(priv
->dev
->dev_addr
+ 4, priv
->local_gid
.raw
, sizeof (union ib_gid
));
517 struct ib_port_attr attr
;
519 if (!ib_query_port(priv
->ca
, priv
->port
, &attr
)) {
520 priv
->local_lid
= attr
.lid
;
521 priv
->local_rate
= attr
.active_speed
*
522 ib_width_enum_to_int(attr
.active_width
);
524 ipoib_warn(priv
, "ib_query_port failed\n");
527 if (!priv
->broadcast
) {
528 priv
->broadcast
= ipoib_mcast_alloc(dev
, 1);
529 if (!priv
->broadcast
) {
530 ipoib_warn(priv
, "failed to allocate broadcast group\n");
532 if (test_bit(IPOIB_MCAST_RUN
, &priv
->flags
))
533 queue_delayed_work(ipoib_workqueue
,
534 &priv
->mcast_task
, HZ
);
539 memcpy(priv
->broadcast
->mcmember
.mgid
.raw
, priv
->dev
->broadcast
+ 4,
540 sizeof (union ib_gid
));
542 spin_lock_irq(&priv
->lock
);
543 __ipoib_mcast_add(dev
, priv
->broadcast
);
544 spin_unlock_irq(&priv
->lock
);
547 if (!test_bit(IPOIB_MCAST_FLAG_ATTACHED
, &priv
->broadcast
->flags
)) {
548 ipoib_mcast_join(dev
, priv
->broadcast
, 0);
553 struct ipoib_mcast
*mcast
= NULL
;
555 spin_lock_irq(&priv
->lock
);
556 list_for_each_entry(mcast
, &priv
->multicast_list
, list
) {
557 if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY
, &mcast
->flags
)
558 && !test_bit(IPOIB_MCAST_FLAG_BUSY
, &mcast
->flags
)
559 && !test_bit(IPOIB_MCAST_FLAG_ATTACHED
, &mcast
->flags
)) {
560 /* Found the next unjoined group */
564 spin_unlock_irq(&priv
->lock
);
566 if (&mcast
->list
== &priv
->multicast_list
) {
571 ipoib_mcast_join(dev
, mcast
, 1);
575 priv
->mcast_mtu
= ib_mtu_enum_to_int(priv
->broadcast
->mcmember
.mtu
) -
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
);
582 netif_carrier_on(dev
);
585 int ipoib_mcast_start_thread(struct net_device
*dev
)
587 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
589 ipoib_dbg_mcast(priv
, "starting multicast thread\n");
592 if (!test_and_set_bit(IPOIB_MCAST_RUN
, &priv
->flags
))
593 queue_work(ipoib_workqueue
, &priv
->mcast_task
);
599 int ipoib_mcast_stop_thread(struct net_device
*dev
, int flush
)
601 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
602 struct ipoib_mcast
*mcast
;
604 ipoib_dbg_mcast(priv
, "stopping multicast thread\n");
607 clear_bit(IPOIB_MCAST_RUN
, &priv
->flags
);
608 cancel_delayed_work(&priv
->mcast_task
);
612 flush_workqueue(ipoib_workqueue
);
614 if (priv
->broadcast
&& priv
->broadcast
->query
) {
615 ib_sa_cancel_query(priv
->broadcast
->query_id
, priv
->broadcast
->query
);
616 priv
->broadcast
->query
= NULL
;
617 ipoib_dbg_mcast(priv
, "waiting for bcast\n");
618 wait_for_completion(&priv
->broadcast
->done
);
621 list_for_each_entry(mcast
, &priv
->multicast_list
, list
) {
623 ib_sa_cancel_query(mcast
->query_id
, mcast
->query
);
625 ipoib_dbg_mcast(priv
, "waiting for MGID " IPOIB_GID_FMT
"\n",
626 IPOIB_GID_ARG(mcast
->mcmember
.mgid
));
627 wait_for_completion(&mcast
->done
);
634 static int ipoib_mcast_leave(struct net_device
*dev
, struct ipoib_mcast
*mcast
)
636 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
637 struct ib_sa_mcmember_rec rec
= {
642 if (!test_and_clear_bit(IPOIB_MCAST_FLAG_ATTACHED
, &mcast
->flags
))
645 ipoib_dbg_mcast(priv
, "leaving MGID " IPOIB_GID_FMT
"\n",
646 IPOIB_GID_ARG(mcast
->mcmember
.mgid
));
648 rec
.mgid
= mcast
->mcmember
.mgid
;
649 rec
.port_gid
= priv
->local_gid
;
650 rec
.pkey
= cpu_to_be16(priv
->pkey
);
652 /* Remove ourselves from the multicast group */
653 ret
= ipoib_mcast_detach(dev
, be16_to_cpu(mcast
->mcmember
.mlid
),
654 &mcast
->mcmember
.mgid
);
656 ipoib_warn(priv
, "ipoib_mcast_detach failed (result = %d)\n", ret
);
659 * Just make one shot at leaving and don't wait for a reply;
660 * if we fail, too bad.
662 ret
= ib_sa_mcmember_rec_delete(priv
->ca
, priv
->port
, &rec
,
663 IB_SA_MCMEMBER_REC_MGID
|
664 IB_SA_MCMEMBER_REC_PORT_GID
|
665 IB_SA_MCMEMBER_REC_PKEY
|
666 IB_SA_MCMEMBER_REC_JOIN_STATE
,
668 mcast
, &mcast
->query
);
670 ipoib_warn(priv
, "ib_sa_mcmember_rec_delete failed "
671 "for leave (result = %d)\n", ret
);
676 void ipoib_mcast_send(struct net_device
*dev
, union ib_gid
*mgid
,
679 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
680 struct ipoib_mcast
*mcast
;
683 * We can only be called from ipoib_start_xmit, so we're
684 * inside tx_lock -- no need to save/restore flags.
686 spin_lock(&priv
->lock
);
688 mcast
= __ipoib_mcast_find(dev
, mgid
);
690 /* Let's create a new send only group now */
691 ipoib_dbg_mcast(priv
, "setting up send only multicast group for "
692 IPOIB_GID_FMT
"\n", IPOIB_GID_ARG(*mgid
));
694 mcast
= ipoib_mcast_alloc(dev
, 0);
696 ipoib_warn(priv
, "unable to allocate memory for "
697 "multicast structure\n");
698 dev_kfree_skb_any(skb
);
702 set_bit(IPOIB_MCAST_FLAG_SENDONLY
, &mcast
->flags
);
703 mcast
->mcmember
.mgid
= *mgid
;
704 __ipoib_mcast_add(dev
, mcast
);
705 list_add_tail(&mcast
->list
, &priv
->multicast_list
);
709 if (skb_queue_len(&mcast
->pkt_queue
) < IPOIB_MAX_MCAST_QUEUE
)
710 skb_queue_tail(&mcast
->pkt_queue
, skb
);
712 dev_kfree_skb_any(skb
);
715 ipoib_dbg_mcast(priv
, "no address vector, "
716 "but multicast join already started\n");
717 else if (test_bit(IPOIB_MCAST_FLAG_SENDONLY
, &mcast
->flags
))
718 ipoib_mcast_sendonly_join(mcast
);
721 * If lookup completes between here and out:, don't
722 * want to send packet twice.
728 if (mcast
&& mcast
->ah
) {
730 skb
->dst
->neighbour
&&
731 !*to_ipoib_neigh(skb
->dst
->neighbour
)) {
732 struct ipoib_neigh
*neigh
= kmalloc(sizeof *neigh
, GFP_ATOMIC
);
735 kref_get(&mcast
->ah
->ref
);
736 neigh
->ah
= mcast
->ah
;
737 neigh
->neighbour
= skb
->dst
->neighbour
;
738 *to_ipoib_neigh(skb
->dst
->neighbour
) = neigh
;
739 list_add_tail(&neigh
->list
, &mcast
->neigh_list
);
743 ipoib_send(dev
, skb
, mcast
->ah
, IB_MULTICAST_QPN
);
746 spin_unlock(&priv
->lock
);
749 void ipoib_mcast_dev_flush(struct net_device
*dev
)
751 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
752 LIST_HEAD(remove_list
);
753 struct ipoib_mcast
*mcast
, *tmcast
, *nmcast
;
756 ipoib_dbg_mcast(priv
, "flushing multicast list\n");
758 spin_lock_irqsave(&priv
->lock
, flags
);
759 list_for_each_entry_safe(mcast
, tmcast
, &priv
->multicast_list
, list
) {
760 nmcast
= ipoib_mcast_alloc(dev
, 0);
763 mcast
->flags
& (1 << IPOIB_MCAST_FLAG_SENDONLY
);
765 nmcast
->mcmember
.mgid
= mcast
->mcmember
.mgid
;
767 /* Add the new group in before the to-be-destroyed group */
768 list_add_tail(&nmcast
->list
, &mcast
->list
);
769 list_del_init(&mcast
->list
);
771 rb_replace_node(&mcast
->rb_node
, &nmcast
->rb_node
,
772 &priv
->multicast_tree
);
774 list_add_tail(&mcast
->list
, &remove_list
);
776 ipoib_warn(priv
, "could not reallocate multicast group "
778 IPOIB_GID_ARG(mcast
->mcmember
.mgid
));
782 if (priv
->broadcast
) {
783 nmcast
= ipoib_mcast_alloc(dev
, 0);
785 nmcast
->mcmember
.mgid
= priv
->broadcast
->mcmember
.mgid
;
787 rb_replace_node(&priv
->broadcast
->rb_node
,
789 &priv
->multicast_tree
);
791 list_add_tail(&priv
->broadcast
->list
, &remove_list
);
794 priv
->broadcast
= nmcast
;
797 spin_unlock_irqrestore(&priv
->lock
, flags
);
799 list_for_each_entry_safe(mcast
, tmcast
, &remove_list
, list
) {
800 ipoib_mcast_leave(dev
, mcast
);
801 ipoib_mcast_free(mcast
);
805 void ipoib_mcast_dev_down(struct net_device
*dev
)
807 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
810 /* Delete broadcast since it will be recreated */
811 if (priv
->broadcast
) {
812 ipoib_dbg_mcast(priv
, "deleting broadcast group\n");
814 spin_lock_irqsave(&priv
->lock
, flags
);
815 rb_erase(&priv
->broadcast
->rb_node
, &priv
->multicast_tree
);
816 spin_unlock_irqrestore(&priv
->lock
, flags
);
817 ipoib_mcast_leave(dev
, priv
->broadcast
);
818 ipoib_mcast_free(priv
->broadcast
);
819 priv
->broadcast
= NULL
;
823 void ipoib_mcast_restart_task(void *dev_ptr
)
825 struct net_device
*dev
= dev_ptr
;
826 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
827 struct dev_mc_list
*mclist
;
828 struct ipoib_mcast
*mcast
, *tmcast
;
829 LIST_HEAD(remove_list
);
832 ipoib_dbg_mcast(priv
, "restarting multicast task\n");
834 ipoib_mcast_stop_thread(dev
, 0);
836 spin_lock_irqsave(&priv
->lock
, flags
);
839 * Unfortunately, the networking core only gives us a list of all of
840 * the multicast hardware addresses. We need to figure out which ones
841 * are new and which ones have been removed
844 /* Clear out the found flag */
845 list_for_each_entry(mcast
, &priv
->multicast_list
, list
)
846 clear_bit(IPOIB_MCAST_FLAG_FOUND
, &mcast
->flags
);
848 /* Mark all of the entries that are found or don't exist */
849 for (mclist
= dev
->mc_list
; mclist
; mclist
= mclist
->next
) {
852 memcpy(mgid
.raw
, mclist
->dmi_addr
+ 4, sizeof mgid
);
854 /* Add in the P_Key */
855 mgid
.raw
[4] = (priv
->pkey
>> 8) & 0xff;
856 mgid
.raw
[5] = priv
->pkey
& 0xff;
858 mcast
= __ipoib_mcast_find(dev
, &mgid
);
859 if (!mcast
|| test_bit(IPOIB_MCAST_FLAG_SENDONLY
, &mcast
->flags
)) {
860 struct ipoib_mcast
*nmcast
;
862 /* Not found or send-only group, let's add a new entry */
863 ipoib_dbg_mcast(priv
, "adding multicast entry for mgid "
864 IPOIB_GID_FMT
"\n", IPOIB_GID_ARG(mgid
));
866 nmcast
= ipoib_mcast_alloc(dev
, 0);
868 ipoib_warn(priv
, "unable to allocate memory for multicast structure\n");
872 set_bit(IPOIB_MCAST_FLAG_FOUND
, &nmcast
->flags
);
874 nmcast
->mcmember
.mgid
= mgid
;
877 /* Destroy the send only entry */
878 list_del(&mcast
->list
);
879 list_add_tail(&mcast
->list
, &remove_list
);
881 rb_replace_node(&mcast
->rb_node
,
883 &priv
->multicast_tree
);
885 __ipoib_mcast_add(dev
, nmcast
);
887 list_add_tail(&nmcast
->list
, &priv
->multicast_list
);
891 set_bit(IPOIB_MCAST_FLAG_FOUND
, &mcast
->flags
);
894 /* Remove all of the entries don't exist anymore */
895 list_for_each_entry_safe(mcast
, tmcast
, &priv
->multicast_list
, list
) {
896 if (!test_bit(IPOIB_MCAST_FLAG_FOUND
, &mcast
->flags
) &&
897 !test_bit(IPOIB_MCAST_FLAG_SENDONLY
, &mcast
->flags
)) {
898 ipoib_dbg_mcast(priv
, "deleting multicast group " IPOIB_GID_FMT
"\n",
899 IPOIB_GID_ARG(mcast
->mcmember
.mgid
));
901 rb_erase(&mcast
->rb_node
, &priv
->multicast_tree
);
903 /* Move to the remove list */
904 list_del(&mcast
->list
);
905 list_add_tail(&mcast
->list
, &remove_list
);
908 spin_unlock_irqrestore(&priv
->lock
, flags
);
910 /* We have to cancel outside of the spinlock */
911 list_for_each_entry_safe(mcast
, tmcast
, &remove_list
, list
) {
912 ipoib_mcast_leave(mcast
->dev
, mcast
);
913 ipoib_mcast_free(mcast
);
916 if (test_bit(IPOIB_FLAG_ADMIN_UP
, &priv
->flags
))
917 ipoib_mcast_start_thread(dev
);
920 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
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, 16);
933 if (ipoib_mcast_iter_next(iter
)) {
941 int ipoib_mcast_iter_next(struct ipoib_mcast_iter
*iter
)
943 struct ipoib_dev_priv
*priv
= netdev_priv(iter
->dev
);
945 struct ipoib_mcast
*mcast
;
948 spin_lock_irq(&priv
->lock
);
950 n
= rb_first(&priv
->multicast_tree
);
953 mcast
= rb_entry(n
, struct ipoib_mcast
, rb_node
);
955 if (memcmp(iter
->mgid
.raw
, mcast
->mcmember
.mgid
.raw
,
956 sizeof (union ib_gid
)) < 0) {
957 iter
->mgid
= mcast
->mcmember
.mgid
;
958 iter
->created
= mcast
->created
;
959 iter
->queuelen
= skb_queue_len(&mcast
->pkt_queue
);
960 iter
->complete
= !!mcast
->ah
;
961 iter
->send_only
= !!(mcast
->flags
& (1 << IPOIB_MCAST_FLAG_SENDONLY
));
971 spin_unlock_irq(&priv
->lock
);
976 void ipoib_mcast_iter_read(struct ipoib_mcast_iter
*iter
,
978 unsigned long *created
,
979 unsigned int *queuelen
,
980 unsigned int *complete
,
981 unsigned int *send_only
)
984 *created
= iter
->created
;
985 *queuelen
= iter
->queuelen
;
986 *complete
= iter
->complete
;
987 *send_only
= iter
->send_only
;
990 #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */