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 multicast MTU and cached Q_Key before we attach if it's
179 * the broadcast group.
181 if (!memcmp(mcast
->mcmember
.mgid
.raw
, priv
->dev
->broadcast
+ 4,
182 sizeof (union ib_gid
))) {
183 spin_lock_irq(&priv
->lock
);
184 if (!priv
->broadcast
) {
185 spin_unlock_irq(&priv
->lock
);
188 priv
->mcast_mtu
= IPOIB_UD_MTU(ib_mtu_enum_to_int(priv
->broadcast
->mcmember
.mtu
));
189 priv
->qkey
= be32_to_cpu(priv
->broadcast
->mcmember
.qkey
);
190 spin_unlock_irq(&priv
->lock
);
191 priv
->tx_wr
.wr
.ud
.remote_qkey
= priv
->qkey
;
195 if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY
, &mcast
->flags
)) {
196 if (test_and_set_bit(IPOIB_MCAST_FLAG_ATTACHED
, &mcast
->flags
)) {
197 ipoib_warn(priv
, "multicast group %pI6 already attached\n",
198 mcast
->mcmember
.mgid
.raw
);
203 ret
= ipoib_mcast_attach(dev
, be16_to_cpu(mcast
->mcmember
.mlid
),
204 &mcast
->mcmember
.mgid
, set_qkey
);
206 ipoib_warn(priv
, "couldn't attach QP to multicast group %pI6\n",
207 mcast
->mcmember
.mgid
.raw
);
209 clear_bit(IPOIB_MCAST_FLAG_ATTACHED
, &mcast
->flags
);
215 struct ib_ah_attr av
= {
216 .dlid
= be16_to_cpu(mcast
->mcmember
.mlid
),
217 .port_num
= priv
->port
,
218 .sl
= mcast
->mcmember
.sl
,
219 .ah_flags
= IB_AH_GRH
,
220 .static_rate
= mcast
->mcmember
.rate
,
222 .flow_label
= be32_to_cpu(mcast
->mcmember
.flow_label
),
223 .hop_limit
= mcast
->mcmember
.hop_limit
,
225 .traffic_class
= mcast
->mcmember
.traffic_class
228 av
.grh
.dgid
= mcast
->mcmember
.mgid
;
230 ah
= ipoib_create_ah(dev
, priv
->pd
, &av
);
232 ipoib_warn(priv
, "ib_address_create failed %ld\n",
234 /* use original error */
237 spin_lock_irq(&priv
->lock
);
239 spin_unlock_irq(&priv
->lock
);
241 ipoib_dbg_mcast(priv
, "MGID %pI6 AV %p, LID 0x%04x, SL %d\n",
242 mcast
->mcmember
.mgid
.raw
,
244 be16_to_cpu(mcast
->mcmember
.mlid
),
249 /* actually send any queued packets */
250 netif_tx_lock_bh(dev
);
251 while (!skb_queue_empty(&mcast
->pkt_queue
)) {
252 struct sk_buff
*skb
= skb_dequeue(&mcast
->pkt_queue
);
254 netif_tx_unlock_bh(dev
);
257 if (dev_queue_xmit(skb
))
258 ipoib_warn(priv
, "dev_queue_xmit failed to requeue packet\n");
260 netif_tx_lock_bh(dev
);
262 netif_tx_unlock_bh(dev
);
268 ipoib_mcast_sendonly_join_complete(int status
,
269 struct ib_sa_multicast
*multicast
)
271 struct ipoib_mcast
*mcast
= multicast
->context
;
272 struct net_device
*dev
= mcast
->dev
;
275 * We have to take the mutex to force mcast_sendonly_join to
276 * return from ib_sa_multicast_join and set mcast->mc to a
277 * valid value. Otherwise we were racing with ourselves in
278 * that we might fail here, but get a valid return from
279 * ib_sa_multicast_join after we had cleared mcast->mc here,
280 * resulting in mis-matched joins and leaves and a deadlock
282 mutex_lock(&mcast_mutex
);
284 /* We trap for port events ourselves. */
285 if (status
== -ENETRESET
)
289 status
= ipoib_mcast_join_finish(mcast
, &multicast
->rec
);
292 if (mcast
->logcount
++ < 20)
293 ipoib_dbg_mcast(netdev_priv(dev
), "sendonly multicast "
294 "join failed for %pI6, status %d\n",
295 mcast
->mcmember
.mgid
.raw
, status
);
297 /* Flush out any queued packets */
298 netif_tx_lock_bh(dev
);
299 while (!skb_queue_empty(&mcast
->pkt_queue
)) {
300 ++dev
->stats
.tx_dropped
;
301 dev_kfree_skb_any(skb_dequeue(&mcast
->pkt_queue
));
303 netif_tx_unlock_bh(dev
);
306 clear_bit(IPOIB_MCAST_FLAG_BUSY
, &mcast
->flags
);
309 complete(&mcast
->done
);
310 if (status
== -ENETRESET
)
312 mutex_unlock(&mcast_mutex
);
316 static int ipoib_mcast_sendonly_join(struct ipoib_mcast
*mcast
)
318 struct net_device
*dev
= mcast
->dev
;
319 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
320 struct ib_sa_mcmember_rec rec
= {
321 #if 0 /* Some SMs don't support send-only yet */
329 if (!test_bit(IPOIB_FLAG_OPER_UP
, &priv
->flags
)) {
330 ipoib_dbg_mcast(priv
, "device shutting down, no sendonly "
331 "multicast joins\n");
335 if (test_bit(IPOIB_MCAST_FLAG_BUSY
, &mcast
->flags
)) {
336 ipoib_dbg_mcast(priv
, "multicast entry busy, skipping "
341 rec
.mgid
= mcast
->mcmember
.mgid
;
342 rec
.port_gid
= priv
->local_gid
;
343 rec
.pkey
= cpu_to_be16(priv
->pkey
);
345 mutex_lock(&mcast_mutex
);
346 init_completion(&mcast
->done
);
347 set_bit(IPOIB_MCAST_FLAG_BUSY
, &mcast
->flags
);
348 mcast
->mc
= ib_sa_join_multicast(&ipoib_sa_client
, priv
->ca
,
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
,
357 if (IS_ERR(mcast
->mc
)) {
358 ret
= PTR_ERR(mcast
->mc
);
359 clear_bit(IPOIB_MCAST_FLAG_BUSY
, &mcast
->flags
);
360 complete(&mcast
->done
);
361 ipoib_warn(priv
, "ib_sa_join_multicast for sendonly join "
362 "failed (ret = %d)\n", ret
);
364 ipoib_dbg_mcast(priv
, "no multicast record for %pI6, starting "
365 "sendonly join\n", mcast
->mcmember
.mgid
.raw
);
367 mutex_unlock(&mcast_mutex
);
372 void ipoib_mcast_carrier_on_task(struct work_struct
*work
)
374 struct ipoib_dev_priv
*priv
= container_of(work
, struct ipoib_dev_priv
,
376 struct ib_port_attr attr
;
378 if (ib_query_port(priv
->ca
, priv
->port
, &attr
) ||
379 attr
.state
!= IB_PORT_ACTIVE
) {
380 ipoib_dbg(priv
, "Keeping carrier off until IB port is active\n");
385 * Take rtnl_lock to avoid racing with ipoib_stop() and
386 * turning the carrier back on while a device is being
387 * removed. However, ipoib_stop() will attempt to flush
388 * the workqueue while holding the rtnl lock, so loop
389 * on trylock until either we get the lock or we see
390 * FLAG_ADMIN_UP go away as that signals that we are bailing
391 * and can safely ignore the carrier on work
393 while (!rtnl_trylock()) {
394 if (!test_bit(IPOIB_FLAG_ADMIN_UP
, &priv
->flags
))
399 if (!ipoib_cm_admin_enabled(priv
->dev
))
400 dev_set_mtu(priv
->dev
, min(priv
->mcast_mtu
, priv
->admin_mtu
));
401 netif_carrier_on(priv
->dev
);
405 static int ipoib_mcast_join_complete(int status
,
406 struct ib_sa_multicast
*multicast
)
408 struct ipoib_mcast
*mcast
= multicast
->context
;
409 struct net_device
*dev
= mcast
->dev
;
410 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
412 ipoib_dbg_mcast(priv
, "join completion for %pI6 (status %d)\n",
413 mcast
->mcmember
.mgid
.raw
, status
);
416 * We have to take the mutex to force mcast_join to
417 * return from ib_sa_multicast_join and set mcast->mc to a
418 * valid value. Otherwise we were racing with ourselves in
419 * that we might fail here, but get a valid return from
420 * ib_sa_multicast_join after we had cleared mcast->mc here,
421 * resulting in mis-matched joins and leaves and a deadlock
423 mutex_lock(&mcast_mutex
);
425 /* We trap for port events ourselves. */
426 if (status
== -ENETRESET
)
430 status
= ipoib_mcast_join_finish(mcast
, &multicast
->rec
);
434 if (test_bit(IPOIB_MCAST_RUN
, &priv
->flags
))
435 queue_delayed_work(ipoib_workqueue
,
436 &priv
->mcast_task
, 0);
439 * Defer carrier on work to ipoib_workqueue to avoid a
440 * deadlock on rtnl_lock here.
442 if (mcast
== priv
->broadcast
)
443 queue_work(ipoib_workqueue
, &priv
->carrier_on_task
);
445 if (mcast
->logcount
++ < 20) {
446 if (status
== -ETIMEDOUT
|| status
== -EAGAIN
) {
447 ipoib_dbg_mcast(priv
, "multicast join failed for %pI6, status %d\n",
448 mcast
->mcmember
.mgid
.raw
, status
);
450 ipoib_warn(priv
, "multicast join failed for %pI6, status %d\n",
451 mcast
->mcmember
.mgid
.raw
, status
);
456 if (mcast
->backoff
> IPOIB_MAX_BACKOFF_SECONDS
)
457 mcast
->backoff
= IPOIB_MAX_BACKOFF_SECONDS
;
460 spin_lock_irq(&priv
->lock
);
461 clear_bit(IPOIB_MCAST_FLAG_BUSY
, &mcast
->flags
);
464 complete(&mcast
->done
);
465 if (status
== -ENETRESET
)
467 if (status
&& test_bit(IPOIB_MCAST_RUN
, &priv
->flags
))
468 queue_delayed_work(ipoib_workqueue
, &priv
->mcast_task
,
469 mcast
->backoff
* HZ
);
470 spin_unlock_irq(&priv
->lock
);
471 mutex_unlock(&mcast_mutex
);
476 static void ipoib_mcast_join(struct net_device
*dev
, struct ipoib_mcast
*mcast
,
479 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
480 struct ib_sa_mcmember_rec rec
= {
483 ib_sa_comp_mask comp_mask
;
486 ipoib_dbg_mcast(priv
, "joining MGID %pI6\n", mcast
->mcmember
.mgid
.raw
);
488 rec
.mgid
= mcast
->mcmember
.mgid
;
489 rec
.port_gid
= priv
->local_gid
;
490 rec
.pkey
= cpu_to_be16(priv
->pkey
);
493 IB_SA_MCMEMBER_REC_MGID
|
494 IB_SA_MCMEMBER_REC_PORT_GID
|
495 IB_SA_MCMEMBER_REC_PKEY
|
496 IB_SA_MCMEMBER_REC_JOIN_STATE
;
500 IB_SA_MCMEMBER_REC_QKEY
|
501 IB_SA_MCMEMBER_REC_MTU_SELECTOR
|
502 IB_SA_MCMEMBER_REC_MTU
|
503 IB_SA_MCMEMBER_REC_TRAFFIC_CLASS
|
504 IB_SA_MCMEMBER_REC_RATE_SELECTOR
|
505 IB_SA_MCMEMBER_REC_RATE
|
506 IB_SA_MCMEMBER_REC_SL
|
507 IB_SA_MCMEMBER_REC_FLOW_LABEL
|
508 IB_SA_MCMEMBER_REC_HOP_LIMIT
;
510 rec
.qkey
= priv
->broadcast
->mcmember
.qkey
;
511 rec
.mtu_selector
= IB_SA_EQ
;
512 rec
.mtu
= priv
->broadcast
->mcmember
.mtu
;
513 rec
.traffic_class
= priv
->broadcast
->mcmember
.traffic_class
;
514 rec
.rate_selector
= IB_SA_EQ
;
515 rec
.rate
= priv
->broadcast
->mcmember
.rate
;
516 rec
.sl
= priv
->broadcast
->mcmember
.sl
;
517 rec
.flow_label
= priv
->broadcast
->mcmember
.flow_label
;
518 rec
.hop_limit
= priv
->broadcast
->mcmember
.hop_limit
;
521 mutex_lock(&mcast_mutex
);
522 init_completion(&mcast
->done
);
523 set_bit(IPOIB_MCAST_FLAG_BUSY
, &mcast
->flags
);
524 mcast
->mc
= ib_sa_join_multicast(&ipoib_sa_client
, priv
->ca
, priv
->port
,
525 &rec
, comp_mask
, GFP_KERNEL
,
526 ipoib_mcast_join_complete
, mcast
);
527 if (IS_ERR(mcast
->mc
)) {
528 clear_bit(IPOIB_MCAST_FLAG_BUSY
, &mcast
->flags
);
529 complete(&mcast
->done
);
530 ret
= PTR_ERR(mcast
->mc
);
531 ipoib_warn(priv
, "ib_sa_join_multicast failed, status %d\n", ret
);
534 if (mcast
->backoff
> IPOIB_MAX_BACKOFF_SECONDS
)
535 mcast
->backoff
= IPOIB_MAX_BACKOFF_SECONDS
;
537 if (test_bit(IPOIB_MCAST_RUN
, &priv
->flags
))
538 queue_delayed_work(ipoib_workqueue
,
540 mcast
->backoff
* HZ
);
542 mutex_unlock(&mcast_mutex
);
545 void ipoib_mcast_join_task(struct work_struct
*work
)
547 struct ipoib_dev_priv
*priv
=
548 container_of(work
, struct ipoib_dev_priv
, mcast_task
.work
);
549 struct net_device
*dev
= priv
->dev
;
550 struct ib_port_attr port_attr
;
552 if (!test_bit(IPOIB_MCAST_RUN
, &priv
->flags
))
555 if (ib_query_port(priv
->ca
, priv
->port
, &port_attr
) ||
556 port_attr
.state
!= IB_PORT_ACTIVE
) {
557 ipoib_dbg(priv
, "port state is not ACTIVE (state = %d) suspending join task\n",
561 priv
->local_lid
= port_attr
.lid
;
563 if (ib_query_gid(priv
->ca
, priv
->port
, 0, &priv
->local_gid
))
564 ipoib_warn(priv
, "ib_query_gid() failed\n");
566 memcpy(priv
->dev
->dev_addr
+ 4, priv
->local_gid
.raw
, sizeof (union ib_gid
));
568 if (!priv
->broadcast
) {
569 struct ipoib_mcast
*broadcast
;
571 if (!test_bit(IPOIB_FLAG_ADMIN_UP
, &priv
->flags
))
574 broadcast
= ipoib_mcast_alloc(dev
, 1);
576 ipoib_warn(priv
, "failed to allocate broadcast group\n");
577 mutex_lock(&mcast_mutex
);
578 if (test_bit(IPOIB_MCAST_RUN
, &priv
->flags
))
579 queue_delayed_work(ipoib_workqueue
,
580 &priv
->mcast_task
, HZ
);
581 mutex_unlock(&mcast_mutex
);
585 spin_lock_irq(&priv
->lock
);
586 memcpy(broadcast
->mcmember
.mgid
.raw
, priv
->dev
->broadcast
+ 4,
587 sizeof (union ib_gid
));
588 priv
->broadcast
= broadcast
;
590 __ipoib_mcast_add(dev
, priv
->broadcast
);
591 spin_unlock_irq(&priv
->lock
);
594 if (!test_bit(IPOIB_MCAST_FLAG_ATTACHED
, &priv
->broadcast
->flags
)) {
595 if (IS_ERR_OR_NULL(priv
->broadcast
->mc
) &&
596 !test_bit(IPOIB_MCAST_FLAG_BUSY
, &priv
->broadcast
->flags
))
597 ipoib_mcast_join(dev
, priv
->broadcast
, 0);
602 struct ipoib_mcast
*mcast
= NULL
;
605 * Need the mutex so our flags are consistent, need the
606 * priv->lock so we don't race with list removals in either
607 * mcast_dev_flush or mcast_restart_task
609 mutex_lock(&mcast_mutex
);
610 spin_lock_irq(&priv
->lock
);
611 list_for_each_entry(mcast
, &priv
->multicast_list
, list
) {
612 if (IS_ERR_OR_NULL(mcast
->mc
) &&
613 !test_bit(IPOIB_MCAST_FLAG_BUSY
, &mcast
->flags
) &&
614 !test_bit(IPOIB_MCAST_FLAG_ATTACHED
, &mcast
->flags
)) {
615 /* Found the next unjoined group */
619 spin_unlock_irq(&priv
->lock
);
620 mutex_unlock(&mcast_mutex
);
622 if (&mcast
->list
== &priv
->multicast_list
) {
627 if (test_bit(IPOIB_MCAST_FLAG_SENDONLY
, &mcast
->flags
))
628 ipoib_mcast_sendonly_join(mcast
);
630 ipoib_mcast_join(dev
, mcast
, 1);
634 ipoib_dbg_mcast(priv
, "successfully joined all multicast groups\n");
636 clear_bit(IPOIB_MCAST_RUN
, &priv
->flags
);
639 int ipoib_mcast_start_thread(struct net_device
*dev
)
641 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
643 ipoib_dbg_mcast(priv
, "starting multicast thread\n");
645 mutex_lock(&mcast_mutex
);
646 if (!test_and_set_bit(IPOIB_MCAST_RUN
, &priv
->flags
))
647 queue_delayed_work(ipoib_workqueue
, &priv
->mcast_task
, 0);
648 mutex_unlock(&mcast_mutex
);
653 int ipoib_mcast_stop_thread(struct net_device
*dev
, int flush
)
655 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
657 ipoib_dbg_mcast(priv
, "stopping multicast thread\n");
659 mutex_lock(&mcast_mutex
);
660 clear_bit(IPOIB_MCAST_RUN
, &priv
->flags
);
661 cancel_delayed_work(&priv
->mcast_task
);
662 mutex_unlock(&mcast_mutex
);
665 flush_workqueue(ipoib_workqueue
);
670 static int ipoib_mcast_leave(struct net_device
*dev
, struct ipoib_mcast
*mcast
)
672 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
675 if (test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY
, &mcast
->flags
))
676 ipoib_warn(priv
, "ipoib_mcast_leave on an in-flight join\n");
678 if (!IS_ERR_OR_NULL(mcast
->mc
))
679 ib_sa_free_multicast(mcast
->mc
);
681 if (test_and_clear_bit(IPOIB_MCAST_FLAG_ATTACHED
, &mcast
->flags
)) {
682 ipoib_dbg_mcast(priv
, "leaving MGID %pI6\n",
683 mcast
->mcmember
.mgid
.raw
);
685 /* Remove ourselves from the multicast group */
686 ret
= ib_detach_mcast(priv
->qp
, &mcast
->mcmember
.mgid
,
687 be16_to_cpu(mcast
->mcmember
.mlid
));
689 ipoib_warn(priv
, "ib_detach_mcast failed (result = %d)\n", ret
);
695 void ipoib_mcast_send(struct net_device
*dev
, u8
*daddr
, struct sk_buff
*skb
)
697 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
698 struct ipoib_mcast
*mcast
;
700 void *mgid
= daddr
+ 4;
702 spin_lock_irqsave(&priv
->lock
, flags
);
704 if (!test_bit(IPOIB_FLAG_OPER_UP
, &priv
->flags
) ||
706 !test_bit(IPOIB_MCAST_FLAG_ATTACHED
, &priv
->broadcast
->flags
)) {
707 ++dev
->stats
.tx_dropped
;
708 dev_kfree_skb_any(skb
);
712 mcast
= __ipoib_mcast_find(dev
, mgid
);
714 /* Let's create a new send only group now */
715 ipoib_dbg_mcast(priv
, "setting up send only multicast group for %pI6\n",
718 mcast
= ipoib_mcast_alloc(dev
, 0);
720 ipoib_warn(priv
, "unable to allocate memory for "
721 "multicast structure\n");
722 ++dev
->stats
.tx_dropped
;
723 dev_kfree_skb_any(skb
);
727 set_bit(IPOIB_MCAST_FLAG_SENDONLY
, &mcast
->flags
);
728 memcpy(mcast
->mcmember
.mgid
.raw
, mgid
, sizeof (union ib_gid
));
729 __ipoib_mcast_add(dev
, mcast
);
730 list_add_tail(&mcast
->list
, &priv
->multicast_list
);
731 if (!test_and_set_bit(IPOIB_MCAST_RUN
, &priv
->flags
))
732 queue_delayed_work(ipoib_workqueue
, &priv
->mcast_task
, 0);
736 if (skb_queue_len(&mcast
->pkt_queue
) < IPOIB_MAX_MCAST_QUEUE
)
737 skb_queue_tail(&mcast
->pkt_queue
, skb
);
739 ++dev
->stats
.tx_dropped
;
740 dev_kfree_skb_any(skb
);
743 if (test_bit(IPOIB_MCAST_FLAG_BUSY
, &mcast
->flags
))
744 ipoib_dbg_mcast(priv
, "no address vector, "
745 "but multicast join already started\n");
748 * If lookup completes between here and out:, don't
749 * want to send packet twice.
755 if (mcast
&& mcast
->ah
) {
756 struct ipoib_neigh
*neigh
;
758 spin_unlock_irqrestore(&priv
->lock
, flags
);
759 neigh
= ipoib_neigh_get(dev
, daddr
);
760 spin_lock_irqsave(&priv
->lock
, flags
);
762 neigh
= ipoib_neigh_alloc(daddr
, dev
);
764 kref_get(&mcast
->ah
->ref
);
765 neigh
->ah
= mcast
->ah
;
766 list_add_tail(&neigh
->list
, &mcast
->neigh_list
);
769 spin_unlock_irqrestore(&priv
->lock
, flags
);
770 ipoib_send(dev
, skb
, mcast
->ah
, IB_MULTICAST_QPN
);
772 ipoib_neigh_put(neigh
);
777 spin_unlock_irqrestore(&priv
->lock
, flags
);
780 void ipoib_mcast_dev_flush(struct net_device
*dev
)
782 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
783 LIST_HEAD(remove_list
);
784 struct ipoib_mcast
*mcast
, *tmcast
;
787 ipoib_dbg_mcast(priv
, "flushing multicast list\n");
789 spin_lock_irqsave(&priv
->lock
, flags
);
791 list_for_each_entry_safe(mcast
, tmcast
, &priv
->multicast_list
, list
) {
792 list_del(&mcast
->list
);
793 rb_erase(&mcast
->rb_node
, &priv
->multicast_tree
);
794 list_add_tail(&mcast
->list
, &remove_list
);
797 if (priv
->broadcast
) {
798 rb_erase(&priv
->broadcast
->rb_node
, &priv
->multicast_tree
);
799 list_add_tail(&priv
->broadcast
->list
, &remove_list
);
800 priv
->broadcast
= NULL
;
803 spin_unlock_irqrestore(&priv
->lock
, flags
);
806 * make sure the in-flight joins have finished before we attempt
809 list_for_each_entry_safe(mcast
, tmcast
, &remove_list
, list
)
810 if (test_bit(IPOIB_MCAST_FLAG_BUSY
, &mcast
->flags
))
811 wait_for_completion(&mcast
->done
);
813 list_for_each_entry_safe(mcast
, tmcast
, &remove_list
, list
) {
814 ipoib_mcast_leave(dev
, mcast
);
815 ipoib_mcast_free(mcast
);
819 static int ipoib_mcast_addr_is_valid(const u8
*addr
, const u8
*broadcast
)
821 /* reserved QPN, prefix, scope */
822 if (memcmp(addr
, broadcast
, 6))
824 /* signature lower, pkey */
825 if (memcmp(addr
+ 7, broadcast
+ 7, 3))
830 void ipoib_mcast_restart_task(struct work_struct
*work
)
832 struct ipoib_dev_priv
*priv
=
833 container_of(work
, struct ipoib_dev_priv
, restart_task
);
834 struct net_device
*dev
= priv
->dev
;
835 struct netdev_hw_addr
*ha
;
836 struct ipoib_mcast
*mcast
, *tmcast
;
837 LIST_HEAD(remove_list
);
839 struct ib_sa_mcmember_rec rec
;
841 ipoib_dbg_mcast(priv
, "restarting multicast task\n");
843 ipoib_mcast_stop_thread(dev
, 0);
845 local_irq_save(flags
);
846 netif_addr_lock(dev
);
847 spin_lock(&priv
->lock
);
850 * Unfortunately, the networking core only gives us a list of all of
851 * the multicast hardware addresses. We need to figure out which ones
852 * are new and which ones have been removed
855 /* Clear out the found flag */
856 list_for_each_entry(mcast
, &priv
->multicast_list
, list
)
857 clear_bit(IPOIB_MCAST_FLAG_FOUND
, &mcast
->flags
);
859 /* Mark all of the entries that are found or don't exist */
860 netdev_for_each_mc_addr(ha
, dev
) {
863 if (!ipoib_mcast_addr_is_valid(ha
->addr
, dev
->broadcast
))
866 memcpy(mgid
.raw
, ha
->addr
+ 4, sizeof mgid
);
868 mcast
= __ipoib_mcast_find(dev
, &mgid
);
869 if (!mcast
|| test_bit(IPOIB_MCAST_FLAG_SENDONLY
, &mcast
->flags
)) {
870 struct ipoib_mcast
*nmcast
;
872 /* ignore group which is directly joined by userspace */
873 if (test_bit(IPOIB_FLAG_UMCAST
, &priv
->flags
) &&
874 !ib_sa_get_mcmember_rec(priv
->ca
, priv
->port
, &mgid
, &rec
)) {
875 ipoib_dbg_mcast(priv
, "ignoring multicast entry for mgid %pI6\n",
880 /* Not found or send-only group, let's add a new entry */
881 ipoib_dbg_mcast(priv
, "adding multicast entry for mgid %pI6\n",
884 nmcast
= ipoib_mcast_alloc(dev
, 0);
886 ipoib_warn(priv
, "unable to allocate memory for multicast structure\n");
890 set_bit(IPOIB_MCAST_FLAG_FOUND
, &nmcast
->flags
);
892 nmcast
->mcmember
.mgid
= mgid
;
895 /* Destroy the send only entry */
896 list_move_tail(&mcast
->list
, &remove_list
);
898 rb_replace_node(&mcast
->rb_node
,
900 &priv
->multicast_tree
);
902 __ipoib_mcast_add(dev
, nmcast
);
904 list_add_tail(&nmcast
->list
, &priv
->multicast_list
);
908 set_bit(IPOIB_MCAST_FLAG_FOUND
, &mcast
->flags
);
911 /* Remove all of the entries don't exist anymore */
912 list_for_each_entry_safe(mcast
, tmcast
, &priv
->multicast_list
, list
) {
913 if (!test_bit(IPOIB_MCAST_FLAG_FOUND
, &mcast
->flags
) &&
914 !test_bit(IPOIB_MCAST_FLAG_SENDONLY
, &mcast
->flags
)) {
915 ipoib_dbg_mcast(priv
, "deleting multicast group %pI6\n",
916 mcast
->mcmember
.mgid
.raw
);
918 rb_erase(&mcast
->rb_node
, &priv
->multicast_tree
);
920 /* Move to the remove list */
921 list_move_tail(&mcast
->list
, &remove_list
);
925 spin_unlock(&priv
->lock
);
926 netif_addr_unlock(dev
);
927 local_irq_restore(flags
);
930 * make sure the in-flight joins have finished before we attempt
933 list_for_each_entry_safe(mcast
, tmcast
, &remove_list
, list
)
934 if (test_bit(IPOIB_MCAST_FLAG_BUSY
, &mcast
->flags
))
935 wait_for_completion(&mcast
->done
);
938 * We have to cancel outside of the spinlock, but we have to
939 * take the rtnl lock or else we race with the removal of
940 * entries from the remove list in mcast_dev_flush as part
941 * of ipoib_stop() which will call mcast_stop_thread with
942 * flush == 1 while holding the rtnl lock, and the
943 * flush_workqueue won't complete until this restart_mcast_task
944 * completes. So do like the carrier on task and attempt to
945 * take the rtnl lock, but if we can't before the ADMIN_UP flag
946 * goes away, then just return and know that the remove list will
947 * get flushed later by mcast_dev_flush.
949 while (!rtnl_trylock()) {
950 if (!test_bit(IPOIB_FLAG_ADMIN_UP
, &priv
->flags
))
955 list_for_each_entry_safe(mcast
, tmcast
, &remove_list
, list
) {
956 ipoib_mcast_leave(mcast
->dev
, mcast
);
957 ipoib_mcast_free(mcast
);
959 ipoib_mcast_start_thread(dev
);
963 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
965 struct ipoib_mcast_iter
*ipoib_mcast_iter_init(struct net_device
*dev
)
967 struct ipoib_mcast_iter
*iter
;
969 iter
= kmalloc(sizeof *iter
, GFP_KERNEL
);
974 memset(iter
->mgid
.raw
, 0, 16);
976 if (ipoib_mcast_iter_next(iter
)) {
984 int ipoib_mcast_iter_next(struct ipoib_mcast_iter
*iter
)
986 struct ipoib_dev_priv
*priv
= netdev_priv(iter
->dev
);
988 struct ipoib_mcast
*mcast
;
991 spin_lock_irq(&priv
->lock
);
993 n
= rb_first(&priv
->multicast_tree
);
996 mcast
= rb_entry(n
, struct ipoib_mcast
, rb_node
);
998 if (memcmp(iter
->mgid
.raw
, mcast
->mcmember
.mgid
.raw
,
999 sizeof (union ib_gid
)) < 0) {
1000 iter
->mgid
= mcast
->mcmember
.mgid
;
1001 iter
->created
= mcast
->created
;
1002 iter
->queuelen
= skb_queue_len(&mcast
->pkt_queue
);
1003 iter
->complete
= !!mcast
->ah
;
1004 iter
->send_only
= !!(mcast
->flags
& (1 << IPOIB_MCAST_FLAG_SENDONLY
));
1014 spin_unlock_irq(&priv
->lock
);
1019 void ipoib_mcast_iter_read(struct ipoib_mcast_iter
*iter
,
1021 unsigned long *created
,
1022 unsigned int *queuelen
,
1023 unsigned int *complete
,
1024 unsigned int *send_only
)
1027 *created
= iter
->created
;
1028 *queuelen
= iter
->queuelen
;
1029 *complete
= iter
->complete
;
1030 *send_only
= iter
->send_only
;
1033 #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */