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 dev_kfree_skb_any(skb_dequeue(&mcast
->pkt_queue
));
129 static struct ipoib_mcast
*ipoib_mcast_alloc(struct net_device
*dev
,
132 struct ipoib_mcast
*mcast
;
134 mcast
= kzalloc(sizeof *mcast
, can_sleep
? GFP_KERNEL
: GFP_ATOMIC
);
139 mcast
->created
= jiffies
;
142 INIT_LIST_HEAD(&mcast
->list
);
143 INIT_LIST_HEAD(&mcast
->neigh_list
);
144 skb_queue_head_init(&mcast
->pkt_queue
);
149 static struct ipoib_mcast
*__ipoib_mcast_find(struct net_device
*dev
, union ib_gid
*mgid
)
151 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
152 struct rb_node
*n
= priv
->multicast_tree
.rb_node
;
155 struct ipoib_mcast
*mcast
;
158 mcast
= rb_entry(n
, struct ipoib_mcast
, rb_node
);
160 ret
= memcmp(mgid
->raw
, mcast
->mcmember
.mgid
.raw
,
161 sizeof (union ib_gid
));
173 static int __ipoib_mcast_add(struct net_device
*dev
, struct ipoib_mcast
*mcast
)
175 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
176 struct rb_node
**n
= &priv
->multicast_tree
.rb_node
, *pn
= NULL
;
179 struct ipoib_mcast
*tmcast
;
183 tmcast
= rb_entry(pn
, struct ipoib_mcast
, rb_node
);
185 ret
= memcmp(mcast
->mcmember
.mgid
.raw
, tmcast
->mcmember
.mgid
.raw
,
186 sizeof (union ib_gid
));
195 rb_link_node(&mcast
->rb_node
, pn
, n
);
196 rb_insert_color(&mcast
->rb_node
, &priv
->multicast_tree
);
201 static int ipoib_mcast_join_finish(struct ipoib_mcast
*mcast
,
202 struct ib_sa_mcmember_rec
*mcmember
)
204 struct net_device
*dev
= mcast
->dev
;
205 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
208 mcast
->mcmember
= *mcmember
;
210 /* Set the cached Q_Key before we attach if it's the broadcast group */
211 if (!memcmp(mcast
->mcmember
.mgid
.raw
, priv
->dev
->broadcast
+ 4,
212 sizeof (union ib_gid
))) {
213 priv
->qkey
= be32_to_cpu(priv
->broadcast
->mcmember
.qkey
);
214 priv
->tx_wr
.wr
.ud
.remote_qkey
= priv
->qkey
;
217 if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY
, &mcast
->flags
)) {
218 if (test_and_set_bit(IPOIB_MCAST_FLAG_ATTACHED
, &mcast
->flags
)) {
219 ipoib_warn(priv
, "multicast group " IPOIB_GID_FMT
220 " already attached\n",
221 IPOIB_GID_ARG(mcast
->mcmember
.mgid
));
226 ret
= ipoib_mcast_attach(dev
, be16_to_cpu(mcast
->mcmember
.mlid
),
227 &mcast
->mcmember
.mgid
);
229 ipoib_warn(priv
, "couldn't attach QP to multicast group "
231 IPOIB_GID_ARG(mcast
->mcmember
.mgid
));
233 clear_bit(IPOIB_MCAST_FLAG_ATTACHED
, &mcast
->flags
);
239 struct ib_ah_attr av
= {
240 .dlid
= be16_to_cpu(mcast
->mcmember
.mlid
),
241 .port_num
= priv
->port
,
242 .sl
= mcast
->mcmember
.sl
,
243 .ah_flags
= IB_AH_GRH
,
245 .flow_label
= be32_to_cpu(mcast
->mcmember
.flow_label
),
246 .hop_limit
= mcast
->mcmember
.hop_limit
,
248 .traffic_class
= mcast
->mcmember
.traffic_class
251 int path_rate
= ib_sa_rate_enum_to_int(mcast
->mcmember
.rate
);
253 av
.grh
.dgid
= mcast
->mcmember
.mgid
;
255 if (path_rate
> 0 && priv
->local_rate
> path_rate
)
256 av
.static_rate
= (priv
->local_rate
- 1) / path_rate
;
258 ipoib_dbg_mcast(priv
, "static_rate %d for local port %dX, mcmember %dX\n",
259 av
.static_rate
, priv
->local_rate
,
260 ib_sa_rate_enum_to_int(mcast
->mcmember
.rate
));
262 mcast
->ah
= ipoib_create_ah(dev
, priv
->pd
, &av
);
264 ipoib_warn(priv
, "ib_address_create failed\n");
266 ipoib_dbg_mcast(priv
, "MGID " IPOIB_GID_FMT
267 " AV %p, LID 0x%04x, SL %d\n",
268 IPOIB_GID_ARG(mcast
->mcmember
.mgid
),
270 be16_to_cpu(mcast
->mcmember
.mlid
),
275 /* actually send any queued packets */
276 while (!skb_queue_empty(&mcast
->pkt_queue
)) {
277 struct sk_buff
*skb
= skb_dequeue(&mcast
->pkt_queue
);
281 if (!skb
->dst
|| !skb
->dst
->neighbour
) {
282 /* put pseudoheader back on for next time */
283 skb_push(skb
, sizeof (struct ipoib_pseudoheader
));
286 if (dev_queue_xmit(skb
))
287 ipoib_warn(priv
, "dev_queue_xmit failed to requeue packet\n");
294 ipoib_mcast_sendonly_join_complete(int status
,
295 struct ib_sa_mcmember_rec
*mcmember
,
298 struct ipoib_mcast
*mcast
= mcast_ptr
;
299 struct net_device
*dev
= mcast
->dev
;
302 ipoib_mcast_join_finish(mcast
, mcmember
);
304 if (mcast
->logcount
++ < 20)
305 ipoib_dbg_mcast(netdev_priv(dev
), "multicast join failed for "
306 IPOIB_GID_FMT
", status %d\n",
307 IPOIB_GID_ARG(mcast
->mcmember
.mgid
), status
);
309 /* Flush out any queued packets */
310 while (!skb_queue_empty(&mcast
->pkt_queue
))
311 dev_kfree_skb_any(skb_dequeue(&mcast
->pkt_queue
));
313 /* Clear the busy flag so we try again */
314 clear_bit(IPOIB_MCAST_FLAG_BUSY
, &mcast
->flags
);
317 complete(&mcast
->done
);
320 static int ipoib_mcast_sendonly_join(struct ipoib_mcast
*mcast
)
322 struct net_device
*dev
= mcast
->dev
;
323 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
324 struct ib_sa_mcmember_rec rec
= {
325 #if 0 /* Some SMs don't support send-only yet */
333 if (!test_bit(IPOIB_FLAG_OPER_UP
, &priv
->flags
)) {
334 ipoib_dbg_mcast(priv
, "device shutting down, no multicast joins\n");
338 if (test_and_set_bit(IPOIB_MCAST_FLAG_BUSY
, &mcast
->flags
)) {
339 ipoib_dbg_mcast(priv
, "multicast entry busy, skipping\n");
343 rec
.mgid
= mcast
->mcmember
.mgid
;
344 rec
.port_gid
= priv
->local_gid
;
345 rec
.pkey
= cpu_to_be16(priv
->pkey
);
347 init_completion(&mcast
->done
);
349 ret
= ib_sa_mcmember_rec_set(priv
->ca
, priv
->port
, &rec
,
350 IB_SA_MCMEMBER_REC_MGID
|
351 IB_SA_MCMEMBER_REC_PORT_GID
|
352 IB_SA_MCMEMBER_REC_PKEY
|
353 IB_SA_MCMEMBER_REC_JOIN_STATE
,
355 ipoib_mcast_sendonly_join_complete
,
356 mcast
, &mcast
->query
);
358 ipoib_warn(priv
, "ib_sa_mcmember_rec_set failed (ret = %d)\n",
361 ipoib_dbg_mcast(priv
, "no multicast record for " IPOIB_GID_FMT
363 IPOIB_GID_ARG(mcast
->mcmember
.mgid
));
365 mcast
->query_id
= ret
;
371 static void ipoib_mcast_join_complete(int status
,
372 struct ib_sa_mcmember_rec
*mcmember
,
375 struct ipoib_mcast
*mcast
= mcast_ptr
;
376 struct net_device
*dev
= mcast
->dev
;
377 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
379 ipoib_dbg_mcast(priv
, "join completion for " IPOIB_GID_FMT
381 IPOIB_GID_ARG(mcast
->mcmember
.mgid
), status
);
383 if (!status
&& !ipoib_mcast_join_finish(mcast
, mcmember
)) {
386 if (test_bit(IPOIB_MCAST_RUN
, &priv
->flags
))
387 queue_work(ipoib_workqueue
, &priv
->mcast_task
);
389 complete(&mcast
->done
);
393 if (status
== -EINTR
) {
394 complete(&mcast
->done
);
398 if (status
&& mcast
->logcount
++ < 20) {
399 if (status
== -ETIMEDOUT
|| status
== -EINTR
) {
400 ipoib_dbg_mcast(priv
, "multicast join failed for " IPOIB_GID_FMT
402 IPOIB_GID_ARG(mcast
->mcmember
.mgid
),
405 ipoib_warn(priv
, "multicast join failed for "
406 IPOIB_GID_FMT
", status %d\n",
407 IPOIB_GID_ARG(mcast
->mcmember
.mgid
),
413 if (mcast
->backoff
> IPOIB_MAX_BACKOFF_SECONDS
)
414 mcast
->backoff
= IPOIB_MAX_BACKOFF_SECONDS
;
419 if (test_bit(IPOIB_MCAST_RUN
, &priv
->flags
)) {
420 if (status
== -ETIMEDOUT
)
421 queue_work(ipoib_workqueue
, &priv
->mcast_task
);
423 queue_delayed_work(ipoib_workqueue
, &priv
->mcast_task
,
424 mcast
->backoff
* HZ
);
426 complete(&mcast
->done
);
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 " IPOIB_GID_FMT
"\n",
443 IPOIB_GID_ARG(mcast
->mcmember
.mgid
));
445 rec
.mgid
= mcast
->mcmember
.mgid
;
446 rec
.port_gid
= priv
->local_gid
;
447 rec
.pkey
= cpu_to_be16(priv
->pkey
);
450 IB_SA_MCMEMBER_REC_MGID
|
451 IB_SA_MCMEMBER_REC_PORT_GID
|
452 IB_SA_MCMEMBER_REC_PKEY
|
453 IB_SA_MCMEMBER_REC_JOIN_STATE
;
457 IB_SA_MCMEMBER_REC_QKEY
|
458 IB_SA_MCMEMBER_REC_SL
|
459 IB_SA_MCMEMBER_REC_FLOW_LABEL
|
460 IB_SA_MCMEMBER_REC_TRAFFIC_CLASS
;
462 rec
.qkey
= priv
->broadcast
->mcmember
.qkey
;
463 rec
.sl
= priv
->broadcast
->mcmember
.sl
;
464 rec
.flow_label
= priv
->broadcast
->mcmember
.flow_label
;
465 rec
.traffic_class
= priv
->broadcast
->mcmember
.traffic_class
;
468 init_completion(&mcast
->done
);
470 ret
= ib_sa_mcmember_rec_set(priv
->ca
, priv
->port
, &rec
, comp_mask
,
471 mcast
->backoff
* 1000, GFP_ATOMIC
,
472 ipoib_mcast_join_complete
,
473 mcast
, &mcast
->query
);
476 ipoib_warn(priv
, "ib_sa_mcmember_rec_set failed, status %d\n", ret
);
479 if (mcast
->backoff
> IPOIB_MAX_BACKOFF_SECONDS
)
480 mcast
->backoff
= IPOIB_MAX_BACKOFF_SECONDS
;
483 if (test_bit(IPOIB_MCAST_RUN
, &priv
->flags
))
484 queue_delayed_work(ipoib_workqueue
,
486 mcast
->backoff
* HZ
);
489 mcast
->query_id
= ret
;
492 void ipoib_mcast_join_task(void *dev_ptr
)
494 struct net_device
*dev
= dev_ptr
;
495 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
497 if (!test_bit(IPOIB_MCAST_RUN
, &priv
->flags
))
500 if (ib_query_gid(priv
->ca
, priv
->port
, 0, &priv
->local_gid
))
501 ipoib_warn(priv
, "ib_gid_entry_get() failed\n");
503 memcpy(priv
->dev
->dev_addr
+ 4, priv
->local_gid
.raw
, sizeof (union ib_gid
));
506 struct ib_port_attr attr
;
508 if (!ib_query_port(priv
->ca
, priv
->port
, &attr
)) {
509 priv
->local_lid
= attr
.lid
;
510 priv
->local_rate
= attr
.active_speed
*
511 ib_width_enum_to_int(attr
.active_width
);
513 ipoib_warn(priv
, "ib_query_port failed\n");
516 if (!priv
->broadcast
) {
517 priv
->broadcast
= ipoib_mcast_alloc(dev
, 1);
518 if (!priv
->broadcast
) {
519 ipoib_warn(priv
, "failed to allocate broadcast group\n");
521 if (test_bit(IPOIB_MCAST_RUN
, &priv
->flags
))
522 queue_delayed_work(ipoib_workqueue
,
523 &priv
->mcast_task
, HZ
);
528 memcpy(priv
->broadcast
->mcmember
.mgid
.raw
, priv
->dev
->broadcast
+ 4,
529 sizeof (union ib_gid
));
531 spin_lock_irq(&priv
->lock
);
532 __ipoib_mcast_add(dev
, priv
->broadcast
);
533 spin_unlock_irq(&priv
->lock
);
536 if (!test_bit(IPOIB_MCAST_FLAG_ATTACHED
, &priv
->broadcast
->flags
)) {
537 ipoib_mcast_join(dev
, priv
->broadcast
, 0);
542 struct ipoib_mcast
*mcast
= NULL
;
544 spin_lock_irq(&priv
->lock
);
545 list_for_each_entry(mcast
, &priv
->multicast_list
, list
) {
546 if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY
, &mcast
->flags
)
547 && !test_bit(IPOIB_MCAST_FLAG_BUSY
, &mcast
->flags
)
548 && !test_bit(IPOIB_MCAST_FLAG_ATTACHED
, &mcast
->flags
)) {
549 /* Found the next unjoined group */
553 spin_unlock_irq(&priv
->lock
);
555 if (&mcast
->list
== &priv
->multicast_list
) {
560 ipoib_mcast_join(dev
, mcast
, 1);
564 priv
->mcast_mtu
= ib_mtu_enum_to_int(priv
->broadcast
->mcmember
.mtu
) -
566 dev
->mtu
= min(priv
->mcast_mtu
, priv
->admin_mtu
);
568 ipoib_dbg_mcast(priv
, "successfully joined all multicast groups\n");
570 clear_bit(IPOIB_MCAST_RUN
, &priv
->flags
);
571 netif_carrier_on(dev
);
574 int ipoib_mcast_start_thread(struct net_device
*dev
)
576 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
578 ipoib_dbg_mcast(priv
, "starting multicast thread\n");
581 if (!test_and_set_bit(IPOIB_MCAST_RUN
, &priv
->flags
))
582 queue_work(ipoib_workqueue
, &priv
->mcast_task
);
588 int ipoib_mcast_stop_thread(struct net_device
*dev
, int flush
)
590 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
591 struct ipoib_mcast
*mcast
;
593 ipoib_dbg_mcast(priv
, "stopping multicast thread\n");
596 clear_bit(IPOIB_MCAST_RUN
, &priv
->flags
);
597 cancel_delayed_work(&priv
->mcast_task
);
601 flush_workqueue(ipoib_workqueue
);
603 if (priv
->broadcast
&& priv
->broadcast
->query
) {
604 ib_sa_cancel_query(priv
->broadcast
->query_id
, priv
->broadcast
->query
);
605 priv
->broadcast
->query
= NULL
;
606 ipoib_dbg_mcast(priv
, "waiting for bcast\n");
607 wait_for_completion(&priv
->broadcast
->done
);
610 list_for_each_entry(mcast
, &priv
->multicast_list
, list
) {
612 ib_sa_cancel_query(mcast
->query_id
, mcast
->query
);
614 ipoib_dbg_mcast(priv
, "waiting for MGID " IPOIB_GID_FMT
"\n",
615 IPOIB_GID_ARG(mcast
->mcmember
.mgid
));
616 wait_for_completion(&mcast
->done
);
623 static int ipoib_mcast_leave(struct net_device
*dev
, struct ipoib_mcast
*mcast
)
625 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
626 struct ib_sa_mcmember_rec rec
= {
631 if (!test_and_clear_bit(IPOIB_MCAST_FLAG_ATTACHED
, &mcast
->flags
))
634 ipoib_dbg_mcast(priv
, "leaving MGID " IPOIB_GID_FMT
"\n",
635 IPOIB_GID_ARG(mcast
->mcmember
.mgid
));
637 rec
.mgid
= mcast
->mcmember
.mgid
;
638 rec
.port_gid
= priv
->local_gid
;
639 rec
.pkey
= cpu_to_be16(priv
->pkey
);
641 /* Remove ourselves from the multicast group */
642 ret
= ipoib_mcast_detach(dev
, be16_to_cpu(mcast
->mcmember
.mlid
),
643 &mcast
->mcmember
.mgid
);
645 ipoib_warn(priv
, "ipoib_mcast_detach failed (result = %d)\n", ret
);
648 * Just make one shot at leaving and don't wait for a reply;
649 * if we fail, too bad.
651 ret
= ib_sa_mcmember_rec_delete(priv
->ca
, priv
->port
, &rec
,
652 IB_SA_MCMEMBER_REC_MGID
|
653 IB_SA_MCMEMBER_REC_PORT_GID
|
654 IB_SA_MCMEMBER_REC_PKEY
|
655 IB_SA_MCMEMBER_REC_JOIN_STATE
,
657 mcast
, &mcast
->query
);
659 ipoib_warn(priv
, "ib_sa_mcmember_rec_delete failed "
660 "for leave (result = %d)\n", ret
);
665 void ipoib_mcast_send(struct net_device
*dev
, union ib_gid
*mgid
,
668 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
669 struct ipoib_mcast
*mcast
;
672 * We can only be called from ipoib_start_xmit, so we're
673 * inside tx_lock -- no need to save/restore flags.
675 spin_lock(&priv
->lock
);
677 mcast
= __ipoib_mcast_find(dev
, mgid
);
679 /* Let's create a new send only group now */
680 ipoib_dbg_mcast(priv
, "setting up send only multicast group for "
681 IPOIB_GID_FMT
"\n", IPOIB_GID_ARG(*mgid
));
683 mcast
= ipoib_mcast_alloc(dev
, 0);
685 ipoib_warn(priv
, "unable to allocate memory for "
686 "multicast structure\n");
687 dev_kfree_skb_any(skb
);
691 set_bit(IPOIB_MCAST_FLAG_SENDONLY
, &mcast
->flags
);
692 mcast
->mcmember
.mgid
= *mgid
;
693 __ipoib_mcast_add(dev
, mcast
);
694 list_add_tail(&mcast
->list
, &priv
->multicast_list
);
698 if (skb_queue_len(&mcast
->pkt_queue
) < IPOIB_MAX_MCAST_QUEUE
)
699 skb_queue_tail(&mcast
->pkt_queue
, skb
);
701 dev_kfree_skb_any(skb
);
704 ipoib_dbg_mcast(priv
, "no address vector, "
705 "but multicast join already started\n");
706 else if (test_bit(IPOIB_MCAST_FLAG_SENDONLY
, &mcast
->flags
))
707 ipoib_mcast_sendonly_join(mcast
);
710 * If lookup completes between here and out:, don't
711 * want to send packet twice.
717 if (mcast
&& mcast
->ah
) {
719 skb
->dst
->neighbour
&&
720 !*to_ipoib_neigh(skb
->dst
->neighbour
)) {
721 struct ipoib_neigh
*neigh
= kmalloc(sizeof *neigh
, GFP_ATOMIC
);
724 kref_get(&mcast
->ah
->ref
);
725 neigh
->ah
= mcast
->ah
;
726 neigh
->neighbour
= skb
->dst
->neighbour
;
727 *to_ipoib_neigh(skb
->dst
->neighbour
) = neigh
;
728 list_add_tail(&neigh
->list
, &mcast
->neigh_list
);
732 ipoib_send(dev
, skb
, mcast
->ah
, IB_MULTICAST_QPN
);
735 spin_unlock(&priv
->lock
);
738 void ipoib_mcast_dev_flush(struct net_device
*dev
)
740 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
741 LIST_HEAD(remove_list
);
742 struct ipoib_mcast
*mcast
, *tmcast
, *nmcast
;
745 ipoib_dbg_mcast(priv
, "flushing multicast list\n");
747 spin_lock_irqsave(&priv
->lock
, flags
);
748 list_for_each_entry_safe(mcast
, tmcast
, &priv
->multicast_list
, list
) {
749 nmcast
= ipoib_mcast_alloc(dev
, 0);
752 mcast
->flags
& (1 << IPOIB_MCAST_FLAG_SENDONLY
);
754 nmcast
->mcmember
.mgid
= mcast
->mcmember
.mgid
;
756 /* Add the new group in before the to-be-destroyed group */
757 list_add_tail(&nmcast
->list
, &mcast
->list
);
758 list_del_init(&mcast
->list
);
760 rb_replace_node(&mcast
->rb_node
, &nmcast
->rb_node
,
761 &priv
->multicast_tree
);
763 list_add_tail(&mcast
->list
, &remove_list
);
765 ipoib_warn(priv
, "could not reallocate multicast group "
767 IPOIB_GID_ARG(mcast
->mcmember
.mgid
));
771 if (priv
->broadcast
) {
772 nmcast
= ipoib_mcast_alloc(dev
, 0);
774 nmcast
->mcmember
.mgid
= priv
->broadcast
->mcmember
.mgid
;
776 rb_replace_node(&priv
->broadcast
->rb_node
,
778 &priv
->multicast_tree
);
780 list_add_tail(&priv
->broadcast
->list
, &remove_list
);
783 priv
->broadcast
= nmcast
;
786 spin_unlock_irqrestore(&priv
->lock
, flags
);
788 list_for_each_entry_safe(mcast
, tmcast
, &remove_list
, list
) {
789 ipoib_mcast_leave(dev
, mcast
);
790 ipoib_mcast_free(mcast
);
794 void ipoib_mcast_dev_down(struct net_device
*dev
)
796 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
799 /* Delete broadcast since it will be recreated */
800 if (priv
->broadcast
) {
801 ipoib_dbg_mcast(priv
, "deleting broadcast group\n");
803 spin_lock_irqsave(&priv
->lock
, flags
);
804 rb_erase(&priv
->broadcast
->rb_node
, &priv
->multicast_tree
);
805 spin_unlock_irqrestore(&priv
->lock
, flags
);
806 ipoib_mcast_leave(dev
, priv
->broadcast
);
807 ipoib_mcast_free(priv
->broadcast
);
808 priv
->broadcast
= NULL
;
812 void ipoib_mcast_restart_task(void *dev_ptr
)
814 struct net_device
*dev
= dev_ptr
;
815 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
816 struct dev_mc_list
*mclist
;
817 struct ipoib_mcast
*mcast
, *tmcast
;
818 LIST_HEAD(remove_list
);
821 ipoib_dbg_mcast(priv
, "restarting multicast task\n");
823 ipoib_mcast_stop_thread(dev
, 0);
825 spin_lock_irqsave(&priv
->lock
, flags
);
828 * Unfortunately, the networking core only gives us a list of all of
829 * the multicast hardware addresses. We need to figure out which ones
830 * are new and which ones have been removed
833 /* Clear out the found flag */
834 list_for_each_entry(mcast
, &priv
->multicast_list
, list
)
835 clear_bit(IPOIB_MCAST_FLAG_FOUND
, &mcast
->flags
);
837 /* Mark all of the entries that are found or don't exist */
838 for (mclist
= dev
->mc_list
; mclist
; mclist
= mclist
->next
) {
841 memcpy(mgid
.raw
, mclist
->dmi_addr
+ 4, sizeof mgid
);
843 /* Add in the P_Key */
844 mgid
.raw
[4] = (priv
->pkey
>> 8) & 0xff;
845 mgid
.raw
[5] = priv
->pkey
& 0xff;
847 mcast
= __ipoib_mcast_find(dev
, &mgid
);
848 if (!mcast
|| test_bit(IPOIB_MCAST_FLAG_SENDONLY
, &mcast
->flags
)) {
849 struct ipoib_mcast
*nmcast
;
851 /* Not found or send-only group, let's add a new entry */
852 ipoib_dbg_mcast(priv
, "adding multicast entry for mgid "
853 IPOIB_GID_FMT
"\n", IPOIB_GID_ARG(mgid
));
855 nmcast
= ipoib_mcast_alloc(dev
, 0);
857 ipoib_warn(priv
, "unable to allocate memory for multicast structure\n");
861 set_bit(IPOIB_MCAST_FLAG_FOUND
, &nmcast
->flags
);
863 nmcast
->mcmember
.mgid
= mgid
;
866 /* Destroy the send only entry */
867 list_del(&mcast
->list
);
868 list_add_tail(&mcast
->list
, &remove_list
);
870 rb_replace_node(&mcast
->rb_node
,
872 &priv
->multicast_tree
);
874 __ipoib_mcast_add(dev
, nmcast
);
876 list_add_tail(&nmcast
->list
, &priv
->multicast_list
);
880 set_bit(IPOIB_MCAST_FLAG_FOUND
, &mcast
->flags
);
883 /* Remove all of the entries don't exist anymore */
884 list_for_each_entry_safe(mcast
, tmcast
, &priv
->multicast_list
, list
) {
885 if (!test_bit(IPOIB_MCAST_FLAG_FOUND
, &mcast
->flags
) &&
886 !test_bit(IPOIB_MCAST_FLAG_SENDONLY
, &mcast
->flags
)) {
887 ipoib_dbg_mcast(priv
, "deleting multicast group " IPOIB_GID_FMT
"\n",
888 IPOIB_GID_ARG(mcast
->mcmember
.mgid
));
890 rb_erase(&mcast
->rb_node
, &priv
->multicast_tree
);
892 /* Move to the remove list */
893 list_del(&mcast
->list
);
894 list_add_tail(&mcast
->list
, &remove_list
);
897 spin_unlock_irqrestore(&priv
->lock
, flags
);
899 /* We have to cancel outside of the spinlock */
900 list_for_each_entry_safe(mcast
, tmcast
, &remove_list
, list
) {
901 ipoib_mcast_leave(mcast
->dev
, mcast
);
902 ipoib_mcast_free(mcast
);
905 if (test_bit(IPOIB_FLAG_ADMIN_UP
, &priv
->flags
))
906 ipoib_mcast_start_thread(dev
);
909 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
911 struct ipoib_mcast_iter
*ipoib_mcast_iter_init(struct net_device
*dev
)
913 struct ipoib_mcast_iter
*iter
;
915 iter
= kmalloc(sizeof *iter
, GFP_KERNEL
);
920 memset(iter
->mgid
.raw
, 0, 16);
922 if (ipoib_mcast_iter_next(iter
)) {
930 int ipoib_mcast_iter_next(struct ipoib_mcast_iter
*iter
)
932 struct ipoib_dev_priv
*priv
= netdev_priv(iter
->dev
);
934 struct ipoib_mcast
*mcast
;
937 spin_lock_irq(&priv
->lock
);
939 n
= rb_first(&priv
->multicast_tree
);
942 mcast
= rb_entry(n
, struct ipoib_mcast
, rb_node
);
944 if (memcmp(iter
->mgid
.raw
, mcast
->mcmember
.mgid
.raw
,
945 sizeof (union ib_gid
)) < 0) {
946 iter
->mgid
= mcast
->mcmember
.mgid
;
947 iter
->created
= mcast
->created
;
948 iter
->queuelen
= skb_queue_len(&mcast
->pkt_queue
);
949 iter
->complete
= !!mcast
->ah
;
950 iter
->send_only
= !!(mcast
->flags
& (1 << IPOIB_MCAST_FLAG_SENDONLY
));
960 spin_unlock_irq(&priv
->lock
);
965 void ipoib_mcast_iter_read(struct ipoib_mcast_iter
*iter
,
967 unsigned long *created
,
968 unsigned int *queuelen
,
969 unsigned int *complete
,
970 unsigned int *send_only
)
973 *created
= iter
->created
;
974 *queuelen
= iter
->queuelen
;
975 *complete
= iter
->complete
;
976 *send_only
= iter
->send_only
;
979 #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */