Merge branch 'timers-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[deliverable/linux.git] / drivers / infiniband / ulp / ipoib / ipoib_multicast.c
CommitLineData
1da177e4
LT
1/*
2 * Copyright (c) 2004, 2005 Topspin Communications. All rights reserved.
2a1d9b7f
RD
3 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
4 * Copyright (c) 2004 Voltaire, Inc. All rights reserved.
1da177e4
LT
5 *
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:
11 *
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
14 * conditions are met:
15 *
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
18 * disclaimer.
19 *
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.
24 *
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
32 * SOFTWARE.
1da177e4
LT
33 */
34
35#include <linux/skbuff.h>
36#include <linux/rtnetlink.h>
fec14d2f 37#include <linux/moduleparam.h>
1da177e4
LT
38#include <linux/ip.h>
39#include <linux/in.h>
40#include <linux/igmp.h>
41#include <linux/inetdevice.h>
42#include <linux/delay.h>
43#include <linux/completion.h>
5a0e3ad6 44#include <linux/slab.h>
1da177e4 45
14c85021
ACM
46#include <net/dst.h>
47
1da177e4
LT
48#include "ipoib.h"
49
50#ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
51static int mcast_debug_level;
52
53module_param(mcast_debug_level, int, 0644);
54MODULE_PARM_DESC(mcast_debug_level,
55 "Enable multicast debug tracing if > 0");
56#endif
57
95ed644f 58static DEFINE_MUTEX(mcast_mutex);
1da177e4 59
1da177e4
LT
60struct ipoib_mcast_iter {
61 struct net_device *dev;
62 union ib_gid mgid;
63 unsigned long created;
64 unsigned int queuelen;
65 unsigned int complete;
66 unsigned int send_only;
67};
68
69static void ipoib_mcast_free(struct ipoib_mcast *mcast)
70{
71 struct net_device *dev = mcast->dev;
b36f170b 72 int tx_dropped = 0;
1da177e4 73
5b095d98 74 ipoib_dbg_mcast(netdev_priv(dev), "deleting multicast group %pI6\n",
fcace2fe 75 mcast->mcmember.mgid.raw);
1da177e4 76
b63b70d8
SP
77 /* remove all neigh connected to this mcast */
78 ipoib_del_neighs_by_gid(dev, mcast->mcmember.mgid.raw);
1da177e4 79
1da177e4
LT
80 if (mcast->ah)
81 ipoib_put_ah(mcast->ah);
82
b36f170b
MT
83 while (!skb_queue_empty(&mcast->pkt_queue)) {
84 ++tx_dropped;
8c608a32 85 dev_kfree_skb_any(skb_dequeue(&mcast->pkt_queue));
b36f170b
MT
86 }
87
943c246e 88 netif_tx_lock_bh(dev);
de903512 89 dev->stats.tx_dropped += tx_dropped;
943c246e 90 netif_tx_unlock_bh(dev);
1da177e4
LT
91
92 kfree(mcast);
93}
94
95static struct ipoib_mcast *ipoib_mcast_alloc(struct net_device *dev,
96 int can_sleep)
97{
98 struct ipoib_mcast *mcast;
99
de6eb66b 100 mcast = kzalloc(sizeof *mcast, can_sleep ? GFP_KERNEL : GFP_ATOMIC);
1da177e4
LT
101 if (!mcast)
102 return NULL;
103
1da177e4
LT
104 mcast->dev = dev;
105 mcast->created = jiffies;
ce5b65cc 106 mcast->backoff = 1;
1da177e4
LT
107
108 INIT_LIST_HEAD(&mcast->list);
109 INIT_LIST_HEAD(&mcast->neigh_list);
110 skb_queue_head_init(&mcast->pkt_queue);
111
1da177e4
LT
112 return mcast;
113}
114
37c22a77 115static struct ipoib_mcast *__ipoib_mcast_find(struct net_device *dev, void *mgid)
1da177e4
LT
116{
117 struct ipoib_dev_priv *priv = netdev_priv(dev);
118 struct rb_node *n = priv->multicast_tree.rb_node;
119
120 while (n) {
121 struct ipoib_mcast *mcast;
122 int ret;
123
124 mcast = rb_entry(n, struct ipoib_mcast, rb_node);
125
37c22a77 126 ret = memcmp(mgid, mcast->mcmember.mgid.raw,
1da177e4
LT
127 sizeof (union ib_gid));
128 if (ret < 0)
129 n = n->rb_left;
130 else if (ret > 0)
131 n = n->rb_right;
132 else
133 return mcast;
134 }
135
136 return NULL;
137}
138
139static int __ipoib_mcast_add(struct net_device *dev, struct ipoib_mcast *mcast)
140{
141 struct ipoib_dev_priv *priv = netdev_priv(dev);
142 struct rb_node **n = &priv->multicast_tree.rb_node, *pn = NULL;
143
144 while (*n) {
145 struct ipoib_mcast *tmcast;
146 int ret;
147
148 pn = *n;
149 tmcast = rb_entry(pn, struct ipoib_mcast, rb_node);
150
151 ret = memcmp(mcast->mcmember.mgid.raw, tmcast->mcmember.mgid.raw,
152 sizeof (union ib_gid));
153 if (ret < 0)
154 n = &pn->rb_left;
155 else if (ret > 0)
156 n = &pn->rb_right;
157 else
158 return -EEXIST;
159 }
160
161 rb_link_node(&mcast->rb_node, pn, n);
162 rb_insert_color(&mcast->rb_node, &priv->multicast_tree);
163
164 return 0;
165}
166
167static int ipoib_mcast_join_finish(struct ipoib_mcast *mcast,
168 struct ib_sa_mcmember_rec *mcmember)
169{
170 struct net_device *dev = mcast->dev;
171 struct ipoib_dev_priv *priv = netdev_priv(dev);
7343b231 172 struct ipoib_ah *ah;
1da177e4 173 int ret;
d0de1362 174 int set_qkey = 0;
1da177e4
LT
175
176 mcast->mcmember = *mcmember;
177
178 /* Set the cached Q_Key before we attach if it's the broadcast group */
179 if (!memcmp(mcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
180 sizeof (union ib_gid))) {
e1d50dce
JM
181 spin_lock_irq(&priv->lock);
182 if (!priv->broadcast) {
183 spin_unlock_irq(&priv->lock);
184 return -EAGAIN;
185 }
1da177e4 186 priv->qkey = be32_to_cpu(priv->broadcast->mcmember.qkey);
e1d50dce 187 spin_unlock_irq(&priv->lock);
1da177e4 188 priv->tx_wr.wr.ud.remote_qkey = priv->qkey;
d0de1362 189 set_qkey = 1;
1da177e4
LT
190 }
191
192 if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
193 if (test_and_set_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
5b095d98 194 ipoib_warn(priv, "multicast group %pI6 already attached\n",
fcace2fe 195 mcast->mcmember.mgid.raw);
1da177e4
LT
196
197 return 0;
198 }
199
200 ret = ipoib_mcast_attach(dev, be16_to_cpu(mcast->mcmember.mlid),
d0de1362 201 &mcast->mcmember.mgid, set_qkey);
1da177e4 202 if (ret < 0) {
5b095d98 203 ipoib_warn(priv, "couldn't attach QP to multicast group %pI6\n",
fcace2fe 204 mcast->mcmember.mgid.raw);
1da177e4
LT
205
206 clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags);
207 return ret;
208 }
209 }
210
211 {
212 struct ib_ah_attr av = {
213 .dlid = be16_to_cpu(mcast->mcmember.mlid),
214 .port_num = priv->port,
215 .sl = mcast->mcmember.sl,
216 .ah_flags = IB_AH_GRH,
bf6a9e31 217 .static_rate = mcast->mcmember.rate,
1da177e4
LT
218 .grh = {
219 .flow_label = be32_to_cpu(mcast->mcmember.flow_label),
220 .hop_limit = mcast->mcmember.hop_limit,
221 .sgid_index = 0,
222 .traffic_class = mcast->mcmember.traffic_class
223 }
224 };
1da177e4
LT
225 av.grh.dgid = mcast->mcmember.mgid;
226
7343b231 227 ah = ipoib_create_ah(dev, priv->pd, &av);
3874397c
MM
228 if (IS_ERR(ah)) {
229 ipoib_warn(priv, "ib_address_create failed %ld\n",
230 -PTR_ERR(ah));
231 /* use original error */
232 return PTR_ERR(ah);
1da177e4 233 } else {
624d01f8
OG
234 spin_lock_irq(&priv->lock);
235 mcast->ah = ah;
236 spin_unlock_irq(&priv->lock);
237
5b095d98 238 ipoib_dbg_mcast(priv, "MGID %pI6 AV %p, LID 0x%04x, SL %d\n",
fcace2fe 239 mcast->mcmember.mgid.raw,
1da177e4
LT
240 mcast->ah->ah,
241 be16_to_cpu(mcast->mcmember.mlid),
242 mcast->mcmember.sl);
243 }
244 }
245
246 /* actually send any queued packets */
943c246e 247 netif_tx_lock_bh(dev);
1da177e4
LT
248 while (!skb_queue_empty(&mcast->pkt_queue)) {
249 struct sk_buff *skb = skb_dequeue(&mcast->pkt_queue);
69cce1d1 250
943c246e 251 netif_tx_unlock_bh(dev);
1da177e4
LT
252
253 skb->dev = dev;
1da177e4
LT
254 if (dev_queue_xmit(skb))
255 ipoib_warn(priv, "dev_queue_xmit failed to requeue packet\n");
936d7de3 256
943c246e 257 netif_tx_lock_bh(dev);
1da177e4 258 }
943c246e 259 netif_tx_unlock_bh(dev);
1da177e4
LT
260
261 return 0;
262}
263
faec2f7b 264static int
1da177e4 265ipoib_mcast_sendonly_join_complete(int status,
faec2f7b 266 struct ib_sa_multicast *multicast)
1da177e4 267{
faec2f7b 268 struct ipoib_mcast *mcast = multicast->context;
1da177e4
LT
269 struct net_device *dev = mcast->dev;
270
faec2f7b
SH
271 /* We trap for port events ourselves. */
272 if (status == -ENETRESET)
273 return 0;
274
1da177e4 275 if (!status)
faec2f7b
SH
276 status = ipoib_mcast_join_finish(mcast, &multicast->rec);
277
278 if (status) {
1da177e4 279 if (mcast->logcount++ < 20)
5b095d98 280 ipoib_dbg_mcast(netdev_priv(dev), "multicast join failed for %pI6, status %d\n",
fcace2fe 281 mcast->mcmember.mgid.raw, status);
1da177e4
LT
282
283 /* Flush out any queued packets */
943c246e 284 netif_tx_lock_bh(dev);
b36f170b 285 while (!skb_queue_empty(&mcast->pkt_queue)) {
de903512 286 ++dev->stats.tx_dropped;
8c608a32 287 dev_kfree_skb_any(skb_dequeue(&mcast->pkt_queue));
b36f170b 288 }
943c246e 289 netif_tx_unlock_bh(dev);
1da177e4
LT
290
291 /* Clear the busy flag so we try again */
faec2f7b
SH
292 status = test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY,
293 &mcast->flags);
1da177e4 294 }
faec2f7b 295 return status;
1da177e4
LT
296}
297
298static int ipoib_mcast_sendonly_join(struct ipoib_mcast *mcast)
299{
300 struct net_device *dev = mcast->dev;
301 struct ipoib_dev_priv *priv = netdev_priv(dev);
302 struct ib_sa_mcmember_rec rec = {
303#if 0 /* Some SMs don't support send-only yet */
304 .join_state = 4
305#else
306 .join_state = 1
307#endif
308 };
309 int ret = 0;
310
311 if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
312 ipoib_dbg_mcast(priv, "device shutting down, no multicast joins\n");
313 return -ENODEV;
314 }
315
316 if (test_and_set_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags)) {
317 ipoib_dbg_mcast(priv, "multicast entry busy, skipping\n");
318 return -EBUSY;
319 }
320
321 rec.mgid = mcast->mcmember.mgid;
322 rec.port_gid = priv->local_gid;
97f52eb4 323 rec.pkey = cpu_to_be16(priv->pkey);
1da177e4 324
faec2f7b
SH
325 mcast->mc = ib_sa_join_multicast(&ipoib_sa_client, priv->ca,
326 priv->port, &rec,
327 IB_SA_MCMEMBER_REC_MGID |
328 IB_SA_MCMEMBER_REC_PORT_GID |
329 IB_SA_MCMEMBER_REC_PKEY |
330 IB_SA_MCMEMBER_REC_JOIN_STATE,
331 GFP_ATOMIC,
332 ipoib_mcast_sendonly_join_complete,
333 mcast);
334 if (IS_ERR(mcast->mc)) {
335 ret = PTR_ERR(mcast->mc);
336 clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
337 ipoib_warn(priv, "ib_sa_join_multicast failed (ret = %d)\n",
1da177e4
LT
338 ret);
339 } else {
5b095d98 340 ipoib_dbg_mcast(priv, "no multicast record for %pI6, starting join\n",
fcace2fe 341 mcast->mcmember.mgid.raw);
1da177e4
LT
342 }
343
344 return ret;
345}
346
e8224e4b
YE
347void ipoib_mcast_carrier_on_task(struct work_struct *work)
348{
349 struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
350 carrier_on_task);
5ee95120 351 struct ib_port_attr attr;
e8224e4b
YE
352
353 /*
354 * Take rtnl_lock to avoid racing with ipoib_stop() and
355 * turning the carrier back on while a device is being
356 * removed.
357 */
5ee95120
MS
358 if (ib_query_port(priv->ca, priv->port, &attr) ||
359 attr.state != IB_PORT_ACTIVE) {
360 ipoib_dbg(priv, "Keeping carrier off until IB port is active\n");
361 return;
362 }
363
e8224e4b
YE
364 rtnl_lock();
365 netif_carrier_on(priv->dev);
366 rtnl_unlock();
367}
368
faec2f7b
SH
369static int ipoib_mcast_join_complete(int status,
370 struct ib_sa_multicast *multicast)
1da177e4 371{
faec2f7b 372 struct ipoib_mcast *mcast = multicast->context;
1da177e4
LT
373 struct net_device *dev = mcast->dev;
374 struct ipoib_dev_priv *priv = netdev_priv(dev);
375
5b095d98 376 ipoib_dbg_mcast(priv, "join completion for %pI6 (status %d)\n",
fcace2fe 377 mcast->mcmember.mgid.raw, status);
1da177e4 378
faec2f7b
SH
379 /* We trap for port events ourselves. */
380 if (status == -ENETRESET)
381 return 0;
382
383 if (!status)
384 status = ipoib_mcast_join_finish(mcast, &multicast->rec);
385
386 if (!status) {
ce5b65cc 387 mcast->backoff = 1;
95ed644f 388 mutex_lock(&mcast_mutex);
1da177e4 389 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
c4028958
DH
390 queue_delayed_work(ipoib_workqueue,
391 &priv->mcast_task, 0);
95ed644f 392 mutex_unlock(&mcast_mutex);
55c9adde 393
e8224e4b
YE
394 /*
395 * Defer carrier on work to ipoib_workqueue to avoid a
396 * deadlock on rtnl_lock here.
397 */
398 if (mcast == priv->broadcast)
399 queue_work(ipoib_workqueue, &priv->carrier_on_task);
55c9adde 400
faec2f7b 401 return 0;
1da177e4
LT
402 }
403
faec2f7b 404 if (mcast->logcount++ < 20) {
3c209620 405 if (status == -ETIMEDOUT || status == -EAGAIN) {
5b095d98 406 ipoib_dbg_mcast(priv, "multicast join failed for %pI6, status %d\n",
fcace2fe 407 mcast->mcmember.mgid.raw, status);
1da177e4 408 } else {
5b095d98 409 ipoib_warn(priv, "multicast join failed for %pI6, status %d\n",
fcace2fe 410 mcast->mcmember.mgid.raw, status);
1da177e4
LT
411 }
412 }
413
414 mcast->backoff *= 2;
415 if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
416 mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
417
faec2f7b
SH
418 /* Clear the busy flag so we try again */
419 status = test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
9acf6a85 420
faec2f7b 421 mutex_lock(&mcast_mutex);
9acf6a85 422 spin_lock_irq(&priv->lock);
faec2f7b
SH
423 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
424 queue_delayed_work(ipoib_workqueue, &priv->mcast_task,
425 mcast->backoff * HZ);
9acf6a85 426 spin_unlock_irq(&priv->lock);
95ed644f 427 mutex_unlock(&mcast_mutex);
1da177e4 428
faec2f7b 429 return status;
1da177e4
LT
430}
431
432static void ipoib_mcast_join(struct net_device *dev, struct ipoib_mcast *mcast,
433 int create)
434{
435 struct ipoib_dev_priv *priv = netdev_priv(dev);
436 struct ib_sa_mcmember_rec rec = {
437 .join_state = 1
438 };
439 ib_sa_comp_mask comp_mask;
440 int ret = 0;
441
5b095d98 442 ipoib_dbg_mcast(priv, "joining MGID %pI6\n", mcast->mcmember.mgid.raw);
1da177e4
LT
443
444 rec.mgid = mcast->mcmember.mgid;
445 rec.port_gid = priv->local_gid;
97f52eb4 446 rec.pkey = cpu_to_be16(priv->pkey);
1da177e4
LT
447
448 comp_mask =
449 IB_SA_MCMEMBER_REC_MGID |
450 IB_SA_MCMEMBER_REC_PORT_GID |
451 IB_SA_MCMEMBER_REC_PKEY |
452 IB_SA_MCMEMBER_REC_JOIN_STATE;
453
454 if (create) {
455 comp_mask |=
d0df6d6d
RD
456 IB_SA_MCMEMBER_REC_QKEY |
457 IB_SA_MCMEMBER_REC_MTU_SELECTOR |
458 IB_SA_MCMEMBER_REC_MTU |
459 IB_SA_MCMEMBER_REC_TRAFFIC_CLASS |
460 IB_SA_MCMEMBER_REC_RATE_SELECTOR |
461 IB_SA_MCMEMBER_REC_RATE |
462 IB_SA_MCMEMBER_REC_SL |
463 IB_SA_MCMEMBER_REC_FLOW_LABEL |
464 IB_SA_MCMEMBER_REC_HOP_LIMIT;
1da177e4
LT
465
466 rec.qkey = priv->broadcast->mcmember.qkey;
d0df6d6d
RD
467 rec.mtu_selector = IB_SA_EQ;
468 rec.mtu = priv->broadcast->mcmember.mtu;
469 rec.traffic_class = priv->broadcast->mcmember.traffic_class;
470 rec.rate_selector = IB_SA_EQ;
471 rec.rate = priv->broadcast->mcmember.rate;
1da177e4
LT
472 rec.sl = priv->broadcast->mcmember.sl;
473 rec.flow_label = priv->broadcast->mcmember.flow_label;
d0df6d6d 474 rec.hop_limit = priv->broadcast->mcmember.hop_limit;
1da177e4
LT
475 }
476
faec2f7b
SH
477 set_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
478 mcast->mc = ib_sa_join_multicast(&ipoib_sa_client, priv->ca, priv->port,
479 &rec, comp_mask, GFP_KERNEL,
480 ipoib_mcast_join_complete, mcast);
481 if (IS_ERR(mcast->mc)) {
482 clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
483 ret = PTR_ERR(mcast->mc);
484 ipoib_warn(priv, "ib_sa_join_multicast failed, status %d\n", ret);
1da177e4
LT
485
486 mcast->backoff *= 2;
487 if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
488 mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
489
95ed644f 490 mutex_lock(&mcast_mutex);
1da177e4
LT
491 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
492 queue_delayed_work(ipoib_workqueue,
493 &priv->mcast_task,
ce5b65cc 494 mcast->backoff * HZ);
95ed644f 495 mutex_unlock(&mcast_mutex);
faec2f7b 496 }
1da177e4
LT
497}
498
c4028958 499void ipoib_mcast_join_task(struct work_struct *work)
1da177e4 500{
c4028958
DH
501 struct ipoib_dev_priv *priv =
502 container_of(work, struct ipoib_dev_priv, mcast_task.work);
503 struct net_device *dev = priv->dev;
1da177e4
LT
504
505 if (!test_bit(IPOIB_MCAST_RUN, &priv->flags))
506 return;
507
508 if (ib_query_gid(priv->ca, priv->port, 0, &priv->local_gid))
24bd1e4e 509 ipoib_warn(priv, "ib_query_gid() failed\n");
1da177e4
LT
510 else
511 memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));
512
513 {
514 struct ib_port_attr attr;
515
658bcef6
RD
516 if (!ib_query_port(priv->ca, priv->port, &attr))
517 priv->local_lid = attr.lid;
518 else
faec2f7b 519 ipoib_warn(priv, "ib_query_port failed\n");
1da177e4
LT
520 }
521
522 if (!priv->broadcast) {
20b83382
RD
523 struct ipoib_mcast *broadcast;
524
50df48f5
YE
525 if (!test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
526 return;
527
20b83382
RD
528 broadcast = ipoib_mcast_alloc(dev, 1);
529 if (!broadcast) {
1da177e4 530 ipoib_warn(priv, "failed to allocate broadcast group\n");
95ed644f 531 mutex_lock(&mcast_mutex);
1da177e4
LT
532 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
533 queue_delayed_work(ipoib_workqueue,
534 &priv->mcast_task, HZ);
95ed644f 535 mutex_unlock(&mcast_mutex);
1da177e4
LT
536 return;
537 }
538
20b83382
RD
539 spin_lock_irq(&priv->lock);
540 memcpy(broadcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
1da177e4 541 sizeof (union ib_gid));
20b83382 542 priv->broadcast = broadcast;
1da177e4 543
1da177e4
LT
544 __ipoib_mcast_add(dev, priv->broadcast);
545 spin_unlock_irq(&priv->lock);
546 }
547
548 if (!test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
faec2f7b
SH
549 if (!test_bit(IPOIB_MCAST_FLAG_BUSY, &priv->broadcast->flags))
550 ipoib_mcast_join(dev, priv->broadcast, 0);
1da177e4
LT
551 return;
552 }
553
554 while (1) {
555 struct ipoib_mcast *mcast = NULL;
556
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 */
563 break;
564 }
565 }
566 spin_unlock_irq(&priv->lock);
567
568 if (&mcast->list == &priv->multicast_list) {
569 /* All done */
570 break;
571 }
572
573 ipoib_mcast_join(dev, mcast, 1);
574 return;
575 }
576
bc7b3a36 577 priv->mcast_mtu = IPOIB_UD_MTU(ib_mtu_enum_to_int(priv->broadcast->mcmember.mtu));
839fcaba 578
c8c2afe3
EC
579 if (!ipoib_cm_admin_enabled(dev)) {
580 rtnl_lock();
bd360671 581 dev_set_mtu(dev, min(priv->mcast_mtu, priv->admin_mtu));
c8c2afe3
EC
582 rtnl_unlock();
583 }
1da177e4
LT
584
585 ipoib_dbg_mcast(priv, "successfully joined all multicast groups\n");
586
587 clear_bit(IPOIB_MCAST_RUN, &priv->flags);
1da177e4
LT
588}
589
590int ipoib_mcast_start_thread(struct net_device *dev)
591{
592 struct ipoib_dev_priv *priv = netdev_priv(dev);
593
594 ipoib_dbg_mcast(priv, "starting multicast thread\n");
595
95ed644f 596 mutex_lock(&mcast_mutex);
1da177e4 597 if (!test_and_set_bit(IPOIB_MCAST_RUN, &priv->flags))
c4028958 598 queue_delayed_work(ipoib_workqueue, &priv->mcast_task, 0);
95ed644f 599 mutex_unlock(&mcast_mutex);
1da177e4
LT
600
601 return 0;
602}
603
8d2cae06 604int ipoib_mcast_stop_thread(struct net_device *dev, int flush)
1da177e4
LT
605{
606 struct ipoib_dev_priv *priv = netdev_priv(dev);
1da177e4
LT
607
608 ipoib_dbg_mcast(priv, "stopping multicast thread\n");
609
95ed644f 610 mutex_lock(&mcast_mutex);
1da177e4
LT
611 clear_bit(IPOIB_MCAST_RUN, &priv->flags);
612 cancel_delayed_work(&priv->mcast_task);
95ed644f 613 mutex_unlock(&mcast_mutex);
1da177e4 614
8d2cae06
RD
615 if (flush)
616 flush_workqueue(ipoib_workqueue);
1da177e4 617
1da177e4
LT
618 return 0;
619}
620
621static int ipoib_mcast_leave(struct net_device *dev, struct ipoib_mcast *mcast)
622{
623 struct ipoib_dev_priv *priv = netdev_priv(dev);
1da177e4
LT
624 int ret = 0;
625
e07832b6
SH
626 if (test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
627 ib_sa_free_multicast(mcast->mc);
628
faec2f7b 629 if (test_and_clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
5b095d98 630 ipoib_dbg_mcast(priv, "leaving MGID %pI6\n",
fcace2fe 631 mcast->mcmember.mgid.raw);
1da177e4 632
faec2f7b 633 /* Remove ourselves from the multicast group */
9eae554c
RD
634 ret = ib_detach_mcast(priv->qp, &mcast->mcmember.mgid,
635 be16_to_cpu(mcast->mcmember.mlid));
faec2f7b 636 if (ret)
9eae554c 637 ipoib_warn(priv, "ib_detach_mcast failed (result = %d)\n", ret);
faec2f7b 638 }
1da177e4 639
1da177e4
LT
640 return 0;
641}
642
b63b70d8 643void ipoib_mcast_send(struct net_device *dev, u8 *daddr, struct sk_buff *skb)
1da177e4
LT
644{
645 struct ipoib_dev_priv *priv = netdev_priv(dev);
646 struct ipoib_mcast *mcast;
943c246e 647 unsigned long flags;
b63b70d8 648 void *mgid = daddr + 4;
700db99d 649
943c246e 650 spin_lock_irqsave(&priv->lock, flags);
1da177e4 651
b3e2749b 652 if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags) ||
20b83382
RD
653 !priv->broadcast ||
654 !test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
de903512 655 ++dev->stats.tx_dropped;
479a0796
MT
656 dev_kfree_skb_any(skb);
657 goto unlock;
658 }
659
1da177e4
LT
660 mcast = __ipoib_mcast_find(dev, mgid);
661 if (!mcast) {
662 /* Let's create a new send only group now */
5b095d98 663 ipoib_dbg_mcast(priv, "setting up send only multicast group for %pI6\n",
fcace2fe 664 mgid);
1da177e4
LT
665
666 mcast = ipoib_mcast_alloc(dev, 0);
667 if (!mcast) {
668 ipoib_warn(priv, "unable to allocate memory for "
669 "multicast structure\n");
de903512 670 ++dev->stats.tx_dropped;
1da177e4
LT
671 dev_kfree_skb_any(skb);
672 goto out;
673 }
674
675 set_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags);
37c22a77 676 memcpy(mcast->mcmember.mgid.raw, mgid, sizeof (union ib_gid));
1da177e4
LT
677 __ipoib_mcast_add(dev, mcast);
678 list_add_tail(&mcast->list, &priv->multicast_list);
679 }
680
681 if (!mcast->ah) {
682 if (skb_queue_len(&mcast->pkt_queue) < IPOIB_MAX_MCAST_QUEUE)
683 skb_queue_tail(&mcast->pkt_queue, skb);
b36f170b 684 else {
de903512 685 ++dev->stats.tx_dropped;
1da177e4 686 dev_kfree_skb_any(skb);
b36f170b 687 }
1da177e4 688
faec2f7b 689 if (test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
1da177e4
LT
690 ipoib_dbg_mcast(priv, "no address vector, "
691 "but multicast join already started\n");
692 else if (test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags))
693 ipoib_mcast_sendonly_join(mcast);
694
695 /*
696 * If lookup completes between here and out:, don't
697 * want to send packet twice.
698 */
699 mcast = NULL;
700 }
701
702out:
703 if (mcast && mcast->ah) {
b63b70d8
SP
704 struct ipoib_neigh *neigh;
705
706 spin_unlock_irqrestore(&priv->lock, flags);
707 neigh = ipoib_neigh_get(dev, daddr);
708 spin_lock_irqsave(&priv->lock, flags);
709 if (!neigh) {
b63b70d8 710 neigh = ipoib_neigh_alloc(daddr, dev);
b63b70d8
SP
711 if (neigh) {
712 kref_get(&mcast->ah->ref);
713 neigh->ah = mcast->ah;
714 list_add_tail(&neigh->list, &mcast->neigh_list);
1da177e4
LT
715 }
716 }
721d67cd 717 spin_unlock_irqrestore(&priv->lock, flags);
1da177e4 718 ipoib_send(dev, skb, mcast->ah, IB_MULTICAST_QPN);
b63b70d8
SP
719 if (neigh)
720 ipoib_neigh_put(neigh);
721d67cd 721 return;
1da177e4
LT
722 }
723
479a0796 724unlock:
943c246e 725 spin_unlock_irqrestore(&priv->lock, flags);
1da177e4
LT
726}
727
728void ipoib_mcast_dev_flush(struct net_device *dev)
729{
730 struct ipoib_dev_priv *priv = netdev_priv(dev);
731 LIST_HEAD(remove_list);
988bd503 732 struct ipoib_mcast *mcast, *tmcast;
1da177e4
LT
733 unsigned long flags;
734
735 ipoib_dbg_mcast(priv, "flushing multicast list\n");
736
737 spin_lock_irqsave(&priv->lock, flags);
1da177e4 738
988bd503
EC
739 list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
740 list_del(&mcast->list);
741 rb_erase(&mcast->rb_node, &priv->multicast_tree);
742 list_add_tail(&mcast->list, &remove_list);
1da177e4
LT
743 }
744
745 if (priv->broadcast) {
3cd96564 746 rb_erase(&priv->broadcast->rb_node, &priv->multicast_tree);
988bd503
EC
747 list_add_tail(&priv->broadcast->list, &remove_list);
748 priv->broadcast = NULL;
1da177e4
LT
749 }
750
751 spin_unlock_irqrestore(&priv->lock, flags);
752
753 list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
754 ipoib_mcast_leave(dev, mcast);
755 ipoib_mcast_free(mcast);
756 }
757}
758
3e4aa12f 759static int ipoib_mcast_addr_is_valid(const u8 *addr, const u8 *broadcast)
5e47596b 760{
5e47596b
JG
761 /* reserved QPN, prefix, scope */
762 if (memcmp(addr, broadcast, 6))
763 return 0;
764 /* signature lower, pkey */
765 if (memcmp(addr + 7, broadcast + 7, 3))
766 return 0;
767 return 1;
768}
769
c4028958 770void ipoib_mcast_restart_task(struct work_struct *work)
1da177e4 771{
c4028958
DH
772 struct ipoib_dev_priv *priv =
773 container_of(work, struct ipoib_dev_priv, restart_task);
774 struct net_device *dev = priv->dev;
22bedad3 775 struct netdev_hw_addr *ha;
1da177e4
LT
776 struct ipoib_mcast *mcast, *tmcast;
777 LIST_HEAD(remove_list);
778 unsigned long flags;
335a64a5 779 struct ib_sa_mcmember_rec rec;
1da177e4
LT
780
781 ipoib_dbg_mcast(priv, "restarting multicast task\n");
782
8d2cae06 783 ipoib_mcast_stop_thread(dev, 0);
1da177e4 784
932ff279 785 local_irq_save(flags);
e308a5d8 786 netif_addr_lock(dev);
78bfe0b5 787 spin_lock(&priv->lock);
1da177e4
LT
788
789 /*
790 * Unfortunately, the networking core only gives us a list of all of
791 * the multicast hardware addresses. We need to figure out which ones
792 * are new and which ones have been removed
793 */
794
795 /* Clear out the found flag */
796 list_for_each_entry(mcast, &priv->multicast_list, list)
797 clear_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
798
799 /* Mark all of the entries that are found or don't exist */
22bedad3 800 netdev_for_each_mc_addr(ha, dev) {
1da177e4
LT
801 union ib_gid mgid;
802
22bedad3 803 if (!ipoib_mcast_addr_is_valid(ha->addr, dev->broadcast))
5e47596b
JG
804 continue;
805
22bedad3 806 memcpy(mgid.raw, ha->addr + 4, sizeof mgid);
1da177e4 807
1da177e4
LT
808 mcast = __ipoib_mcast_find(dev, &mgid);
809 if (!mcast || test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
810 struct ipoib_mcast *nmcast;
811
335a64a5
OG
812 /* ignore group which is directly joined by userspace */
813 if (test_bit(IPOIB_FLAG_UMCAST, &priv->flags) &&
814 !ib_sa_get_mcmember_rec(priv->ca, priv->port, &mgid, &rec)) {
5b095d98 815 ipoib_dbg_mcast(priv, "ignoring multicast entry for mgid %pI6\n",
fcace2fe 816 mgid.raw);
335a64a5
OG
817 continue;
818 }
819
1da177e4 820 /* Not found or send-only group, let's add a new entry */
5b095d98 821 ipoib_dbg_mcast(priv, "adding multicast entry for mgid %pI6\n",
fcace2fe 822 mgid.raw);
1da177e4
LT
823
824 nmcast = ipoib_mcast_alloc(dev, 0);
825 if (!nmcast) {
826 ipoib_warn(priv, "unable to allocate memory for multicast structure\n");
827 continue;
828 }
829
830 set_bit(IPOIB_MCAST_FLAG_FOUND, &nmcast->flags);
831
832 nmcast->mcmember.mgid = mgid;
833
834 if (mcast) {
835 /* Destroy the send only entry */
179e0917 836 list_move_tail(&mcast->list, &remove_list);
1da177e4
LT
837
838 rb_replace_node(&mcast->rb_node,
839 &nmcast->rb_node,
840 &priv->multicast_tree);
841 } else
842 __ipoib_mcast_add(dev, nmcast);
843
844 list_add_tail(&nmcast->list, &priv->multicast_list);
845 }
846
847 if (mcast)
848 set_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
849 }
850
851 /* Remove all of the entries don't exist anymore */
852 list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
853 if (!test_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags) &&
854 !test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
5b095d98 855 ipoib_dbg_mcast(priv, "deleting multicast group %pI6\n",
fcace2fe 856 mcast->mcmember.mgid.raw);
1da177e4
LT
857
858 rb_erase(&mcast->rb_node, &priv->multicast_tree);
859
860 /* Move to the remove list */
179e0917 861 list_move_tail(&mcast->list, &remove_list);
1da177e4
LT
862 }
863 }
78bfe0b5
MT
864
865 spin_unlock(&priv->lock);
e308a5d8 866 netif_addr_unlock(dev);
932ff279 867 local_irq_restore(flags);
1da177e4
LT
868
869 /* We have to cancel outside of the spinlock */
870 list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
871 ipoib_mcast_leave(mcast->dev, mcast);
872 ipoib_mcast_free(mcast);
873 }
874
875 if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
876 ipoib_mcast_start_thread(dev);
877}
878
8ae5a8a2
RD
879#ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
880
1da177e4
LT
881struct ipoib_mcast_iter *ipoib_mcast_iter_init(struct net_device *dev)
882{
883 struct ipoib_mcast_iter *iter;
884
885 iter = kmalloc(sizeof *iter, GFP_KERNEL);
886 if (!iter)
887 return NULL;
888
889 iter->dev = dev;
1732b0ef 890 memset(iter->mgid.raw, 0, 16);
1da177e4
LT
891
892 if (ipoib_mcast_iter_next(iter)) {
1732b0ef 893 kfree(iter);
1da177e4
LT
894 return NULL;
895 }
896
897 return iter;
898}
899
1da177e4
LT
900int ipoib_mcast_iter_next(struct ipoib_mcast_iter *iter)
901{
902 struct ipoib_dev_priv *priv = netdev_priv(iter->dev);
903 struct rb_node *n;
904 struct ipoib_mcast *mcast;
905 int ret = 1;
906
907 spin_lock_irq(&priv->lock);
908
909 n = rb_first(&priv->multicast_tree);
910
911 while (n) {
912 mcast = rb_entry(n, struct ipoib_mcast, rb_node);
913
914 if (memcmp(iter->mgid.raw, mcast->mcmember.mgid.raw,
915 sizeof (union ib_gid)) < 0) {
916 iter->mgid = mcast->mcmember.mgid;
917 iter->created = mcast->created;
918 iter->queuelen = skb_queue_len(&mcast->pkt_queue);
919 iter->complete = !!mcast->ah;
920 iter->send_only = !!(mcast->flags & (1 << IPOIB_MCAST_FLAG_SENDONLY));
921
922 ret = 0;
923
924 break;
925 }
926
927 n = rb_next(n);
928 }
929
930 spin_unlock_irq(&priv->lock);
931
932 return ret;
933}
934
935void ipoib_mcast_iter_read(struct ipoib_mcast_iter *iter,
936 union ib_gid *mgid,
937 unsigned long *created,
938 unsigned int *queuelen,
939 unsigned int *complete,
940 unsigned int *send_only)
941{
942 *mgid = iter->mgid;
943 *created = iter->created;
944 *queuelen = iter->queuelen;
945 *complete = iter->complete;
946 *send_only = iter->send_only;
947}
8ae5a8a2
RD
948
949#endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */
This page took 0.730545 seconds and 5 git commands to generate.