blob: 20ebc6fd1bb9e0dc16f7bf954a16eb9a149d1dd9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (c) 2004, 2005 Topspin Communications. All rights reserved.
Roland Dreier2a1d9b72005-08-10 23:03:10 -07003 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
4 * Copyright (c) 2004 Voltaire, Inc. All rights reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 */
34
35#include <linux/skbuff.h>
36#include <linux/rtnetlink.h>
Paul Gortmakerfec14d22011-08-30 12:32:52 -040037#include <linux/moduleparam.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020046#include <net/dst.h>
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#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
Ingo Molnar95ed6442006-01-13 14:51:39 -080058static DEFINE_MUTEX(mcast_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Linus Torvalds1da177e2005-04-16 15:20:36 -070060struct 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;
72 struct ipoib_dev_priv *priv = netdev_priv(dev);
73 struct ipoib_neigh *neigh, *tmp;
Michael S. Tsirkinb36f170b62006-01-17 12:19:40 -080074 int tx_dropped = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Harvey Harrison5b095d9892008-10-29 12:52:50 -070076 ipoib_dbg_mcast(netdev_priv(dev), "deleting multicast group %pI6\n",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -070077 mcast->mcmember.mgid.raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Roland Dreier943c2462008-09-30 10:36:21 -070079 spin_lock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81 list_for_each_entry_safe(neigh, tmp, &mcast->neigh_list, list) {
Eli Cohen97460df2006-01-10 07:43:02 -080082 /*
83 * It's safe to call ipoib_put_ah() inside priv->lock
84 * here, because we know that mcast->ah will always
85 * hold one more reference, so ipoib_put_ah() will
86 * never do more than decrement the ref count.
87 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 if (neigh->ah)
Eli Cohen97460df2006-01-10 07:43:02 -080089 ipoib_put_ah(neigh->ah);
Michael S. Tsirkin2745b5b2006-11-16 14:16:47 +020090 ipoib_neigh_free(dev, neigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 }
92
Roland Dreier943c2462008-09-30 10:36:21 -070093 spin_unlock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 if (mcast->ah)
96 ipoib_put_ah(mcast->ah);
97
Michael S. Tsirkinb36f170b62006-01-17 12:19:40 -080098 while (!skb_queue_empty(&mcast->pkt_queue)) {
99 ++tx_dropped;
Roland Dreier8c608a32005-11-07 10:49:38 -0800100 dev_kfree_skb_any(skb_dequeue(&mcast->pkt_queue));
Michael S. Tsirkinb36f170b62006-01-17 12:19:40 -0800101 }
102
Roland Dreier943c2462008-09-30 10:36:21 -0700103 netif_tx_lock_bh(dev);
Roland Dreierde903512007-09-28 15:33:51 -0700104 dev->stats.tx_dropped += tx_dropped;
Roland Dreier943c2462008-09-30 10:36:21 -0700105 netif_tx_unlock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107 kfree(mcast);
108}
109
110static struct ipoib_mcast *ipoib_mcast_alloc(struct net_device *dev,
111 int can_sleep)
112{
113 struct ipoib_mcast *mcast;
114
Roland Dreierde6eb662005-11-02 07:23:14 -0800115 mcast = kzalloc(sizeof *mcast, can_sleep ? GFP_KERNEL : GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 if (!mcast)
117 return NULL;
118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 mcast->dev = dev;
120 mcast->created = jiffies;
Hal Rosenstockce5b65c2005-09-18 13:47:53 -0700121 mcast->backoff = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123 INIT_LIST_HEAD(&mcast->list);
124 INIT_LIST_HEAD(&mcast->neigh_list);
125 skb_queue_head_init(&mcast->pkt_queue);
126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 return mcast;
128}
129
Jack Morgenstein37c22a72006-05-29 19:14:05 +0300130static struct ipoib_mcast *__ipoib_mcast_find(struct net_device *dev, void *mgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
132 struct ipoib_dev_priv *priv = netdev_priv(dev);
133 struct rb_node *n = priv->multicast_tree.rb_node;
134
135 while (n) {
136 struct ipoib_mcast *mcast;
137 int ret;
138
139 mcast = rb_entry(n, struct ipoib_mcast, rb_node);
140
Jack Morgenstein37c22a72006-05-29 19:14:05 +0300141 ret = memcmp(mgid, mcast->mcmember.mgid.raw,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 sizeof (union ib_gid));
143 if (ret < 0)
144 n = n->rb_left;
145 else if (ret > 0)
146 n = n->rb_right;
147 else
148 return mcast;
149 }
150
151 return NULL;
152}
153
154static int __ipoib_mcast_add(struct net_device *dev, struct ipoib_mcast *mcast)
155{
156 struct ipoib_dev_priv *priv = netdev_priv(dev);
157 struct rb_node **n = &priv->multicast_tree.rb_node, *pn = NULL;
158
159 while (*n) {
160 struct ipoib_mcast *tmcast;
161 int ret;
162
163 pn = *n;
164 tmcast = rb_entry(pn, struct ipoib_mcast, rb_node);
165
166 ret = memcmp(mcast->mcmember.mgid.raw, tmcast->mcmember.mgid.raw,
167 sizeof (union ib_gid));
168 if (ret < 0)
169 n = &pn->rb_left;
170 else if (ret > 0)
171 n = &pn->rb_right;
172 else
173 return -EEXIST;
174 }
175
176 rb_link_node(&mcast->rb_node, pn, n);
177 rb_insert_color(&mcast->rb_node, &priv->multicast_tree);
178
179 return 0;
180}
181
182static int ipoib_mcast_join_finish(struct ipoib_mcast *mcast,
183 struct ib_sa_mcmember_rec *mcmember)
184{
185 struct net_device *dev = mcast->dev;
186 struct ipoib_dev_priv *priv = netdev_priv(dev);
Eli Cohen7343b232006-02-27 20:47:43 -0800187 struct ipoib_ah *ah;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 int ret;
Eli Cohend0de1362008-07-14 23:48:50 -0700189 int set_qkey = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191 mcast->mcmember = *mcmember;
192
193 /* Set the cached Q_Key before we attach if it's the broadcast group */
194 if (!memcmp(mcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
195 sizeof (union ib_gid))) {
Jack Morgensteine1d50dc2008-05-20 15:41:09 -0700196 spin_lock_irq(&priv->lock);
197 if (!priv->broadcast) {
198 spin_unlock_irq(&priv->lock);
199 return -EAGAIN;
200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 priv->qkey = be32_to_cpu(priv->broadcast->mcmember.qkey);
Jack Morgensteine1d50dc2008-05-20 15:41:09 -0700202 spin_unlock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 priv->tx_wr.wr.ud.remote_qkey = priv->qkey;
Eli Cohend0de1362008-07-14 23:48:50 -0700204 set_qkey = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 }
206
207 if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
208 if (test_and_set_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700209 ipoib_warn(priv, "multicast group %pI6 already attached\n",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700210 mcast->mcmember.mgid.raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212 return 0;
213 }
214
215 ret = ipoib_mcast_attach(dev, be16_to_cpu(mcast->mcmember.mlid),
Eli Cohend0de1362008-07-14 23:48:50 -0700216 &mcast->mcmember.mgid, set_qkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 if (ret < 0) {
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700218 ipoib_warn(priv, "couldn't attach QP to multicast group %pI6\n",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700219 mcast->mcmember.mgid.raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221 clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags);
222 return ret;
223 }
224 }
225
226 {
227 struct ib_ah_attr av = {
228 .dlid = be16_to_cpu(mcast->mcmember.mlid),
229 .port_num = priv->port,
230 .sl = mcast->mcmember.sl,
231 .ah_flags = IB_AH_GRH,
Jack Morgensteinbf6a9e32006-04-10 09:43:47 -0700232 .static_rate = mcast->mcmember.rate,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 .grh = {
234 .flow_label = be32_to_cpu(mcast->mcmember.flow_label),
235 .hop_limit = mcast->mcmember.hop_limit,
236 .sgid_index = 0,
237 .traffic_class = mcast->mcmember.traffic_class
238 }
239 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 av.grh.dgid = mcast->mcmember.mgid;
241
Eli Cohen7343b232006-02-27 20:47:43 -0800242 ah = ipoib_create_ah(dev, priv->pd, &av);
Mike Marciniszyn38743972011-11-21 08:43:54 -0500243 if (IS_ERR(ah)) {
244 ipoib_warn(priv, "ib_address_create failed %ld\n",
245 -PTR_ERR(ah));
246 /* use original error */
247 return PTR_ERR(ah);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 } else {
Or Gerlitz624d01f2006-07-24 10:42:00 +0300249 spin_lock_irq(&priv->lock);
250 mcast->ah = ah;
251 spin_unlock_irq(&priv->lock);
252
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700253 ipoib_dbg_mcast(priv, "MGID %pI6 AV %p, LID 0x%04x, SL %d\n",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700254 mcast->mcmember.mgid.raw,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 mcast->ah->ah,
256 be16_to_cpu(mcast->mcmember.mlid),
257 mcast->mcmember.sl);
258 }
259 }
260
261 /* actually send any queued packets */
Roland Dreier943c2462008-09-30 10:36:21 -0700262 netif_tx_lock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 while (!skb_queue_empty(&mcast->pkt_queue)) {
264 struct sk_buff *skb = skb_dequeue(&mcast->pkt_queue);
David S. Miller69cce1d2011-07-17 23:09:49 -0700265
Roland Dreier943c2462008-09-30 10:36:21 -0700266 netif_tx_unlock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268 skb->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 if (dev_queue_xmit(skb))
270 ipoib_warn(priv, "dev_queue_xmit failed to requeue packet\n");
Roland Dreier936d7de2012-02-07 14:51:21 +0000271
Roland Dreier943c2462008-09-30 10:36:21 -0700272 netif_tx_lock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 }
Roland Dreier943c2462008-09-30 10:36:21 -0700274 netif_tx_unlock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276 return 0;
277}
278
Sean Heftyfaec2f72007-02-15 17:00:17 -0800279static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280ipoib_mcast_sendonly_join_complete(int status,
Sean Heftyfaec2f72007-02-15 17:00:17 -0800281 struct ib_sa_multicast *multicast)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
Sean Heftyfaec2f72007-02-15 17:00:17 -0800283 struct ipoib_mcast *mcast = multicast->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 struct net_device *dev = mcast->dev;
285
Sean Heftyfaec2f72007-02-15 17:00:17 -0800286 /* We trap for port events ourselves. */
287 if (status == -ENETRESET)
288 return 0;
289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 if (!status)
Sean Heftyfaec2f72007-02-15 17:00:17 -0800291 status = ipoib_mcast_join_finish(mcast, &multicast->rec);
292
293 if (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 if (mcast->logcount++ < 20)
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700295 ipoib_dbg_mcast(netdev_priv(dev), "multicast join failed for %pI6, status %d\n",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700296 mcast->mcmember.mgid.raw, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
298 /* Flush out any queued packets */
Roland Dreier943c2462008-09-30 10:36:21 -0700299 netif_tx_lock_bh(dev);
Michael S. Tsirkinb36f170b62006-01-17 12:19:40 -0800300 while (!skb_queue_empty(&mcast->pkt_queue)) {
Roland Dreierde903512007-09-28 15:33:51 -0700301 ++dev->stats.tx_dropped;
Roland Dreier8c608a32005-11-07 10:49:38 -0800302 dev_kfree_skb_any(skb_dequeue(&mcast->pkt_queue));
Michael S. Tsirkinb36f170b62006-01-17 12:19:40 -0800303 }
Roland Dreier943c2462008-09-30 10:36:21 -0700304 netif_tx_unlock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306 /* Clear the busy flag so we try again */
Sean Heftyfaec2f72007-02-15 17:00:17 -0800307 status = test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY,
308 &mcast->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 }
Sean Heftyfaec2f72007-02-15 17:00:17 -0800310 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311}
312
313static int ipoib_mcast_sendonly_join(struct ipoib_mcast *mcast)
314{
315 struct net_device *dev = mcast->dev;
316 struct ipoib_dev_priv *priv = netdev_priv(dev);
317 struct ib_sa_mcmember_rec rec = {
318#if 0 /* Some SMs don't support send-only yet */
319 .join_state = 4
320#else
321 .join_state = 1
322#endif
323 };
324 int ret = 0;
325
326 if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
327 ipoib_dbg_mcast(priv, "device shutting down, no multicast joins\n");
328 return -ENODEV;
329 }
330
331 if (test_and_set_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags)) {
332 ipoib_dbg_mcast(priv, "multicast entry busy, skipping\n");
333 return -EBUSY;
334 }
335
336 rec.mgid = mcast->mcmember.mgid;
337 rec.port_gid = priv->local_gid;
Sean Hefty97f52eb2005-08-13 21:05:57 -0700338 rec.pkey = cpu_to_be16(priv->pkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Sean Heftyfaec2f72007-02-15 17:00:17 -0800340 mcast->mc = ib_sa_join_multicast(&ipoib_sa_client, priv->ca,
341 priv->port, &rec,
342 IB_SA_MCMEMBER_REC_MGID |
343 IB_SA_MCMEMBER_REC_PORT_GID |
344 IB_SA_MCMEMBER_REC_PKEY |
345 IB_SA_MCMEMBER_REC_JOIN_STATE,
346 GFP_ATOMIC,
347 ipoib_mcast_sendonly_join_complete,
348 mcast);
349 if (IS_ERR(mcast->mc)) {
350 ret = PTR_ERR(mcast->mc);
351 clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
352 ipoib_warn(priv, "ib_sa_join_multicast failed (ret = %d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 ret);
354 } else {
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700355 ipoib_dbg_mcast(priv, "no multicast record for %pI6, starting join\n",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700356 mcast->mcmember.mgid.raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 }
358
359 return ret;
360}
361
Yossi Etigine8224e42008-09-16 11:57:45 -0700362void ipoib_mcast_carrier_on_task(struct work_struct *work)
363{
364 struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
365 carrier_on_task);
Moni Shoua5ee951202009-09-24 12:01:05 -0700366 struct ib_port_attr attr;
Yossi Etigine8224e42008-09-16 11:57:45 -0700367
368 /*
369 * Take rtnl_lock to avoid racing with ipoib_stop() and
370 * turning the carrier back on while a device is being
371 * removed.
372 */
Moni Shoua5ee951202009-09-24 12:01:05 -0700373 if (ib_query_port(priv->ca, priv->port, &attr) ||
374 attr.state != IB_PORT_ACTIVE) {
375 ipoib_dbg(priv, "Keeping carrier off until IB port is active\n");
376 return;
377 }
378
Yossi Etigine8224e42008-09-16 11:57:45 -0700379 rtnl_lock();
380 netif_carrier_on(priv->dev);
381 rtnl_unlock();
382}
383
Sean Heftyfaec2f72007-02-15 17:00:17 -0800384static int ipoib_mcast_join_complete(int status,
385 struct ib_sa_multicast *multicast)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
Sean Heftyfaec2f72007-02-15 17:00:17 -0800387 struct ipoib_mcast *mcast = multicast->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 struct net_device *dev = mcast->dev;
389 struct ipoib_dev_priv *priv = netdev_priv(dev);
390
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700391 ipoib_dbg_mcast(priv, "join completion for %pI6 (status %d)\n",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700392 mcast->mcmember.mgid.raw, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Sean Heftyfaec2f72007-02-15 17:00:17 -0800394 /* We trap for port events ourselves. */
395 if (status == -ENETRESET)
396 return 0;
397
398 if (!status)
399 status = ipoib_mcast_join_finish(mcast, &multicast->rec);
400
401 if (!status) {
Hal Rosenstockce5b65c2005-09-18 13:47:53 -0700402 mcast->backoff = 1;
Ingo Molnar95ed6442006-01-13 14:51:39 -0800403 mutex_lock(&mcast_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
David Howellsc4028952006-11-22 14:57:56 +0000405 queue_delayed_work(ipoib_workqueue,
406 &priv->mcast_task, 0);
Ingo Molnar95ed6442006-01-13 14:51:39 -0800407 mutex_unlock(&mcast_mutex);
Shirley Ma55c9add2007-03-08 14:59:30 -0800408
Yossi Etigine8224e42008-09-16 11:57:45 -0700409 /*
410 * Defer carrier on work to ipoib_workqueue to avoid a
411 * deadlock on rtnl_lock here.
412 */
413 if (mcast == priv->broadcast)
414 queue_work(ipoib_workqueue, &priv->carrier_on_task);
Shirley Ma55c9add2007-03-08 14:59:30 -0800415
Sean Heftyfaec2f72007-02-15 17:00:17 -0800416 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 }
418
Sean Heftyfaec2f72007-02-15 17:00:17 -0800419 if (mcast->logcount++ < 20) {
Yossi Etigin3c209622009-01-16 13:42:59 -0800420 if (status == -ETIMEDOUT || status == -EAGAIN) {
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700421 ipoib_dbg_mcast(priv, "multicast join failed for %pI6, status %d\n",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700422 mcast->mcmember.mgid.raw, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 } else {
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700424 ipoib_warn(priv, "multicast join failed for %pI6, status %d\n",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700425 mcast->mcmember.mgid.raw, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 }
427 }
428
429 mcast->backoff *= 2;
430 if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
431 mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
432
Sean Heftyfaec2f72007-02-15 17:00:17 -0800433 /* Clear the busy flag so we try again */
434 status = test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
435
Michael S. Tsirkin9acf6a82006-03-02 11:07:47 -0800436 mutex_lock(&mcast_mutex);
Michael S. Tsirkin9acf6a82006-03-02 11:07:47 -0800437 spin_lock_irq(&priv->lock);
Sean Heftyfaec2f72007-02-15 17:00:17 -0800438 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
439 queue_delayed_work(ipoib_workqueue, &priv->mcast_task,
440 mcast->backoff * HZ);
Michael S. Tsirkin9acf6a82006-03-02 11:07:47 -0800441 spin_unlock_irq(&priv->lock);
Ingo Molnar95ed6442006-01-13 14:51:39 -0800442 mutex_unlock(&mcast_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Sean Heftyfaec2f72007-02-15 17:00:17 -0800444 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445}
446
447static void ipoib_mcast_join(struct net_device *dev, struct ipoib_mcast *mcast,
448 int create)
449{
450 struct ipoib_dev_priv *priv = netdev_priv(dev);
451 struct ib_sa_mcmember_rec rec = {
452 .join_state = 1
453 };
454 ib_sa_comp_mask comp_mask;
455 int ret = 0;
456
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700457 ipoib_dbg_mcast(priv, "joining MGID %pI6\n", mcast->mcmember.mgid.raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
459 rec.mgid = mcast->mcmember.mgid;
460 rec.port_gid = priv->local_gid;
Sean Hefty97f52eb2005-08-13 21:05:57 -0700461 rec.pkey = cpu_to_be16(priv->pkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
463 comp_mask =
464 IB_SA_MCMEMBER_REC_MGID |
465 IB_SA_MCMEMBER_REC_PORT_GID |
466 IB_SA_MCMEMBER_REC_PKEY |
467 IB_SA_MCMEMBER_REC_JOIN_STATE;
468
469 if (create) {
470 comp_mask |=
Roland Dreierd0df6d62006-09-22 15:22:56 -0700471 IB_SA_MCMEMBER_REC_QKEY |
472 IB_SA_MCMEMBER_REC_MTU_SELECTOR |
473 IB_SA_MCMEMBER_REC_MTU |
474 IB_SA_MCMEMBER_REC_TRAFFIC_CLASS |
475 IB_SA_MCMEMBER_REC_RATE_SELECTOR |
476 IB_SA_MCMEMBER_REC_RATE |
477 IB_SA_MCMEMBER_REC_SL |
478 IB_SA_MCMEMBER_REC_FLOW_LABEL |
479 IB_SA_MCMEMBER_REC_HOP_LIMIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
481 rec.qkey = priv->broadcast->mcmember.qkey;
Roland Dreierd0df6d62006-09-22 15:22:56 -0700482 rec.mtu_selector = IB_SA_EQ;
483 rec.mtu = priv->broadcast->mcmember.mtu;
484 rec.traffic_class = priv->broadcast->mcmember.traffic_class;
485 rec.rate_selector = IB_SA_EQ;
486 rec.rate = priv->broadcast->mcmember.rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 rec.sl = priv->broadcast->mcmember.sl;
488 rec.flow_label = priv->broadcast->mcmember.flow_label;
Roland Dreierd0df6d62006-09-22 15:22:56 -0700489 rec.hop_limit = priv->broadcast->mcmember.hop_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 }
491
Sean Heftyfaec2f72007-02-15 17:00:17 -0800492 set_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
493 mcast->mc = ib_sa_join_multicast(&ipoib_sa_client, priv->ca, priv->port,
494 &rec, comp_mask, GFP_KERNEL,
495 ipoib_mcast_join_complete, mcast);
496 if (IS_ERR(mcast->mc)) {
497 clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
498 ret = PTR_ERR(mcast->mc);
499 ipoib_warn(priv, "ib_sa_join_multicast failed, status %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
501 mcast->backoff *= 2;
502 if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
503 mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
504
Ingo Molnar95ed6442006-01-13 14:51:39 -0800505 mutex_lock(&mcast_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
507 queue_delayed_work(ipoib_workqueue,
508 &priv->mcast_task,
Hal Rosenstockce5b65c2005-09-18 13:47:53 -0700509 mcast->backoff * HZ);
Ingo Molnar95ed6442006-01-13 14:51:39 -0800510 mutex_unlock(&mcast_mutex);
Sean Heftyfaec2f72007-02-15 17:00:17 -0800511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512}
513
David Howellsc4028952006-11-22 14:57:56 +0000514void ipoib_mcast_join_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
David Howellsc4028952006-11-22 14:57:56 +0000516 struct ipoib_dev_priv *priv =
517 container_of(work, struct ipoib_dev_priv, mcast_task.work);
518 struct net_device *dev = priv->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
520 if (!test_bit(IPOIB_MCAST_RUN, &priv->flags))
521 return;
522
523 if (ib_query_gid(priv->ca, priv->port, 0, &priv->local_gid))
Michael S. Tsirkin24bd1e42007-05-18 16:12:54 +0300524 ipoib_warn(priv, "ib_query_gid() failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 else
526 memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));
527
528 {
529 struct ib_port_attr attr;
530
Roland Dreier658bcef2007-02-21 20:28:05 -0800531 if (!ib_query_port(priv->ca, priv->port, &attr))
532 priv->local_lid = attr.lid;
533 else
Sean Heftyfaec2f72007-02-15 17:00:17 -0800534 ipoib_warn(priv, "ib_query_port failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 }
536
537 if (!priv->broadcast) {
Roland Dreier20b83382006-02-11 12:22:12 -0800538 struct ipoib_mcast *broadcast;
539
Yossi Etigin50df48f2009-01-12 19:28:42 -0800540 if (!test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
541 return;
542
Roland Dreier20b83382006-02-11 12:22:12 -0800543 broadcast = ipoib_mcast_alloc(dev, 1);
544 if (!broadcast) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 ipoib_warn(priv, "failed to allocate broadcast group\n");
Ingo Molnar95ed6442006-01-13 14:51:39 -0800546 mutex_lock(&mcast_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
548 queue_delayed_work(ipoib_workqueue,
549 &priv->mcast_task, HZ);
Ingo Molnar95ed6442006-01-13 14:51:39 -0800550 mutex_unlock(&mcast_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 return;
552 }
553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 spin_lock_irq(&priv->lock);
Roland Dreier20b83382006-02-11 12:22:12 -0800555 memcpy(broadcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
556 sizeof (union ib_gid));
557 priv->broadcast = broadcast;
558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 __ipoib_mcast_add(dev, priv->broadcast);
560 spin_unlock_irq(&priv->lock);
561 }
562
563 if (!test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
Sean Heftyfaec2f72007-02-15 17:00:17 -0800564 if (!test_bit(IPOIB_MCAST_FLAG_BUSY, &priv->broadcast->flags))
565 ipoib_mcast_join(dev, priv->broadcast, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 return;
567 }
568
569 while (1) {
570 struct ipoib_mcast *mcast = NULL;
571
572 spin_lock_irq(&priv->lock);
573 list_for_each_entry(mcast, &priv->multicast_list, list) {
574 if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)
575 && !test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags)
576 && !test_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
577 /* Found the next unjoined group */
578 break;
579 }
580 }
581 spin_unlock_irq(&priv->lock);
582
583 if (&mcast->list == &priv->multicast_list) {
584 /* All done */
585 break;
586 }
587
588 ipoib_mcast_join(dev, mcast, 1);
589 return;
590 }
591
Shirley Mabc7b3a32008-04-23 11:55:45 -0700592 priv->mcast_mtu = IPOIB_UD_MTU(ib_mtu_enum_to_int(priv->broadcast->mcmember.mtu));
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200593
Eli Cohenc8c2afe2008-07-14 23:48:51 -0700594 if (!ipoib_cm_admin_enabled(dev)) {
595 rtnl_lock();
Eli Cohenbd360672008-07-14 23:48:51 -0700596 dev_set_mtu(dev, min(priv->mcast_mtu, priv->admin_mtu));
Eli Cohenc8c2afe2008-07-14 23:48:51 -0700597 rtnl_unlock();
598 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
600 ipoib_dbg_mcast(priv, "successfully joined all multicast groups\n");
601
602 clear_bit(IPOIB_MCAST_RUN, &priv->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603}
604
605int ipoib_mcast_start_thread(struct net_device *dev)
606{
607 struct ipoib_dev_priv *priv = netdev_priv(dev);
608
609 ipoib_dbg_mcast(priv, "starting multicast thread\n");
610
Ingo Molnar95ed6442006-01-13 14:51:39 -0800611 mutex_lock(&mcast_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 if (!test_and_set_bit(IPOIB_MCAST_RUN, &priv->flags))
David Howellsc4028952006-11-22 14:57:56 +0000613 queue_delayed_work(ipoib_workqueue, &priv->mcast_task, 0);
Ingo Molnar95ed6442006-01-13 14:51:39 -0800614 mutex_unlock(&mcast_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616 return 0;
617}
618
Roland Dreier8d2cae02005-09-20 10:52:04 -0700619int ipoib_mcast_stop_thread(struct net_device *dev, int flush)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620{
621 struct ipoib_dev_priv *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
623 ipoib_dbg_mcast(priv, "stopping multicast thread\n");
624
Ingo Molnar95ed6442006-01-13 14:51:39 -0800625 mutex_lock(&mcast_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 clear_bit(IPOIB_MCAST_RUN, &priv->flags);
627 cancel_delayed_work(&priv->mcast_task);
Ingo Molnar95ed6442006-01-13 14:51:39 -0800628 mutex_unlock(&mcast_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Roland Dreier8d2cae02005-09-20 10:52:04 -0700630 if (flush)
631 flush_workqueue(ipoib_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 return 0;
634}
635
636static int ipoib_mcast_leave(struct net_device *dev, struct ipoib_mcast *mcast)
637{
638 struct ipoib_dev_priv *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 int ret = 0;
640
Sean Heftye07832b2007-03-19 14:31:36 -0800641 if (test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
642 ib_sa_free_multicast(mcast->mc);
643
Sean Heftyfaec2f72007-02-15 17:00:17 -0800644 if (test_and_clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700645 ipoib_dbg_mcast(priv, "leaving MGID %pI6\n",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700646 mcast->mcmember.mgid.raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Sean Heftyfaec2f72007-02-15 17:00:17 -0800648 /* Remove ourselves from the multicast group */
Roland Dreier9eae5542008-07-14 23:48:50 -0700649 ret = ib_detach_mcast(priv->qp, &mcast->mcmember.mgid,
650 be16_to_cpu(mcast->mcmember.mlid));
Sean Heftyfaec2f72007-02-15 17:00:17 -0800651 if (ret)
Roland Dreier9eae5542008-07-14 23:48:50 -0700652 ipoib_warn(priv, "ib_detach_mcast failed (result = %d)\n", ret);
Sean Heftyfaec2f72007-02-15 17:00:17 -0800653 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 return 0;
656}
657
Jack Morgenstein37c22a72006-05-29 19:14:05 +0300658void ipoib_mcast_send(struct net_device *dev, void *mgid, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
660 struct ipoib_dev_priv *priv = netdev_priv(dev);
661 struct ipoib_mcast *mcast;
Roland Dreier943c2462008-09-30 10:36:21 -0700662 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Roland Dreier943c2462008-09-30 10:36:21 -0700664 spin_lock_irqsave(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Or Gerlitzb3e27492008-03-11 16:10:02 +0200666 if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags) ||
Roland Dreier20b83382006-02-11 12:22:12 -0800667 !priv->broadcast ||
668 !test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
Roland Dreierde903512007-09-28 15:33:51 -0700669 ++dev->stats.tx_dropped;
Michael S. Tsirkin479a0792006-02-07 16:37:08 -0800670 dev_kfree_skb_any(skb);
671 goto unlock;
672 }
673
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 mcast = __ipoib_mcast_find(dev, mgid);
675 if (!mcast) {
676 /* Let's create a new send only group now */
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700677 ipoib_dbg_mcast(priv, "setting up send only multicast group for %pI6\n",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700678 mgid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
680 mcast = ipoib_mcast_alloc(dev, 0);
681 if (!mcast) {
682 ipoib_warn(priv, "unable to allocate memory for "
683 "multicast structure\n");
Roland Dreierde903512007-09-28 15:33:51 -0700684 ++dev->stats.tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 dev_kfree_skb_any(skb);
686 goto out;
687 }
688
689 set_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags);
Jack Morgenstein37c22a72006-05-29 19:14:05 +0300690 memcpy(mcast->mcmember.mgid.raw, mgid, sizeof (union ib_gid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 __ipoib_mcast_add(dev, mcast);
692 list_add_tail(&mcast->list, &priv->multicast_list);
693 }
694
695 if (!mcast->ah) {
696 if (skb_queue_len(&mcast->pkt_queue) < IPOIB_MAX_MCAST_QUEUE)
697 skb_queue_tail(&mcast->pkt_queue, skb);
Michael S. Tsirkinb36f170b62006-01-17 12:19:40 -0800698 else {
Roland Dreierde903512007-09-28 15:33:51 -0700699 ++dev->stats.tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 dev_kfree_skb_any(skb);
Michael S. Tsirkinb36f170b62006-01-17 12:19:40 -0800701 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Sean Heftyfaec2f72007-02-15 17:00:17 -0800703 if (test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 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);
708
709 /*
710 * If lookup completes between here and out:, don't
711 * want to send packet twice.
712 */
713 mcast = NULL;
714 }
715
716out:
717 if (mcast && mcast->ah) {
David S. Miller69cce1d2011-07-17 23:09:49 -0700718 struct dst_entry *dst = skb_dst(skb);
719 struct neighbour *n = NULL;
Eric Dumazet580da352011-11-29 22:31:23 +0100720
721 rcu_read_lock();
David S. Miller69cce1d2011-07-17 23:09:49 -0700722 if (dst)
David Miller27217452011-12-02 16:52:08 +0000723 n = dst_get_neighbour_noref(dst);
David S. Miller69cce1d2011-07-17 23:09:49 -0700724 if (n && !*to_ipoib_neigh(n)) {
725 struct ipoib_neigh *neigh = ipoib_neigh_alloc(n,
726 skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
728 if (neigh) {
729 kref_get(&mcast->ah->ref);
Roland Dreier2337f802007-10-23 19:57:54 -0700730 neigh->ah = mcast->ah;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 list_add_tail(&neigh->list, &mcast->neigh_list);
732 }
733 }
Eric Dumazet580da352011-11-29 22:31:23 +0100734 rcu_read_unlock();
Roland Dreier721d67c2009-09-05 20:23:40 -0700735 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 ipoib_send(dev, skb, mcast->ah, IB_MULTICAST_QPN);
Roland Dreier721d67c2009-09-05 20:23:40 -0700737 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 }
739
Michael S. Tsirkin479a0792006-02-07 16:37:08 -0800740unlock:
Roland Dreier943c2462008-09-30 10:36:21 -0700741 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742}
743
744void ipoib_mcast_dev_flush(struct net_device *dev)
745{
746 struct ipoib_dev_priv *priv = netdev_priv(dev);
747 LIST_HEAD(remove_list);
Eli Cohen988bd502006-01-12 14:32:20 -0800748 struct ipoib_mcast *mcast, *tmcast;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 unsigned long flags;
750
751 ipoib_dbg_mcast(priv, "flushing multicast list\n");
752
753 spin_lock_irqsave(&priv->lock, flags);
Eli Cohen988bd502006-01-12 14:32:20 -0800754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
Eli Cohen988bd502006-01-12 14:32:20 -0800756 list_del(&mcast->list);
757 rb_erase(&mcast->rb_node, &priv->multicast_tree);
758 list_add_tail(&mcast->list, &remove_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 }
760
761 if (priv->broadcast) {
Roland Dreier3cd96562006-09-22 15:22:46 -0700762 rb_erase(&priv->broadcast->rb_node, &priv->multicast_tree);
Eli Cohen988bd502006-01-12 14:32:20 -0800763 list_add_tail(&priv->broadcast->list, &remove_list);
764 priv->broadcast = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 }
766
767 spin_unlock_irqrestore(&priv->lock, flags);
768
769 list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
770 ipoib_mcast_leave(dev, mcast);
771 ipoib_mcast_free(mcast);
772 }
773}
774
Jiri Pirko3e4aa12f82010-03-22 03:21:39 +0000775static int ipoib_mcast_addr_is_valid(const u8 *addr, const u8 *broadcast)
Jason Gunthorpe5e475962009-09-05 20:23:40 -0700776{
Jason Gunthorpe5e475962009-09-05 20:23:40 -0700777 /* reserved QPN, prefix, scope */
778 if (memcmp(addr, broadcast, 6))
779 return 0;
780 /* signature lower, pkey */
781 if (memcmp(addr + 7, broadcast + 7, 3))
782 return 0;
783 return 1;
784}
785
David Howellsc4028952006-11-22 14:57:56 +0000786void ipoib_mcast_restart_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
David Howellsc4028952006-11-22 14:57:56 +0000788 struct ipoib_dev_priv *priv =
789 container_of(work, struct ipoib_dev_priv, restart_task);
790 struct net_device *dev = priv->dev;
Jiri Pirko22bedad32010-04-01 21:22:57 +0000791 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 struct ipoib_mcast *mcast, *tmcast;
793 LIST_HEAD(remove_list);
794 unsigned long flags;
Or Gerlitz335a64a5a2007-10-08 10:13:00 +0200795 struct ib_sa_mcmember_rec rec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
797 ipoib_dbg_mcast(priv, "restarting multicast task\n");
798
Roland Dreier8d2cae02005-09-20 10:52:04 -0700799 ipoib_mcast_stop_thread(dev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
Herbert Xu932ff272006-06-09 12:20:56 -0700801 local_irq_save(flags);
David S. Millere308a5d2008-07-15 00:13:44 -0700802 netif_addr_lock(dev);
Michael S. Tsirkin78bfe0b2006-01-11 11:47:34 -0800803 spin_lock(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
805 /*
806 * Unfortunately, the networking core only gives us a list of all of
807 * the multicast hardware addresses. We need to figure out which ones
808 * are new and which ones have been removed
809 */
810
811 /* Clear out the found flag */
812 list_for_each_entry(mcast, &priv->multicast_list, list)
813 clear_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
814
815 /* Mark all of the entries that are found or don't exist */
Jiri Pirko22bedad32010-04-01 21:22:57 +0000816 netdev_for_each_mc_addr(ha, dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 union ib_gid mgid;
818
Jiri Pirko22bedad32010-04-01 21:22:57 +0000819 if (!ipoib_mcast_addr_is_valid(ha->addr, dev->broadcast))
Jason Gunthorpe5e475962009-09-05 20:23:40 -0700820 continue;
821
Jiri Pirko22bedad32010-04-01 21:22:57 +0000822 memcpy(mgid.raw, ha->addr + 4, sizeof mgid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 mcast = __ipoib_mcast_find(dev, &mgid);
825 if (!mcast || test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
826 struct ipoib_mcast *nmcast;
827
Or Gerlitz335a64a5a2007-10-08 10:13:00 +0200828 /* ignore group which is directly joined by userspace */
829 if (test_bit(IPOIB_FLAG_UMCAST, &priv->flags) &&
830 !ib_sa_get_mcmember_rec(priv->ca, priv->port, &mgid, &rec)) {
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700831 ipoib_dbg_mcast(priv, "ignoring multicast entry for mgid %pI6\n",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700832 mgid.raw);
Or Gerlitz335a64a5a2007-10-08 10:13:00 +0200833 continue;
834 }
835
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 /* Not found or send-only group, let's add a new entry */
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700837 ipoib_dbg_mcast(priv, "adding multicast entry for mgid %pI6\n",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700838 mgid.raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
840 nmcast = ipoib_mcast_alloc(dev, 0);
841 if (!nmcast) {
842 ipoib_warn(priv, "unable to allocate memory for multicast structure\n");
843 continue;
844 }
845
846 set_bit(IPOIB_MCAST_FLAG_FOUND, &nmcast->flags);
847
848 nmcast->mcmember.mgid = mgid;
849
850 if (mcast) {
851 /* Destroy the send only entry */
Akinobu Mita179e0912006-06-26 00:24:41 -0700852 list_move_tail(&mcast->list, &remove_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
854 rb_replace_node(&mcast->rb_node,
855 &nmcast->rb_node,
856 &priv->multicast_tree);
857 } else
858 __ipoib_mcast_add(dev, nmcast);
859
860 list_add_tail(&nmcast->list, &priv->multicast_list);
861 }
862
863 if (mcast)
864 set_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
865 }
866
867 /* Remove all of the entries don't exist anymore */
868 list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
869 if (!test_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags) &&
870 !test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700871 ipoib_dbg_mcast(priv, "deleting multicast group %pI6\n",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700872 mcast->mcmember.mgid.raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
874 rb_erase(&mcast->rb_node, &priv->multicast_tree);
875
876 /* Move to the remove list */
Akinobu Mita179e0912006-06-26 00:24:41 -0700877 list_move_tail(&mcast->list, &remove_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 }
879 }
Michael S. Tsirkin78bfe0b2006-01-11 11:47:34 -0800880
881 spin_unlock(&priv->lock);
David S. Millere308a5d2008-07-15 00:13:44 -0700882 netif_addr_unlock(dev);
Herbert Xu932ff272006-06-09 12:20:56 -0700883 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
885 /* We have to cancel outside of the spinlock */
886 list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
887 ipoib_mcast_leave(mcast->dev, mcast);
888 ipoib_mcast_free(mcast);
889 }
890
891 if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
892 ipoib_mcast_start_thread(dev);
893}
894
Roland Dreier8ae5a8a2005-11-02 20:51:01 -0800895#ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897struct ipoib_mcast_iter *ipoib_mcast_iter_init(struct net_device *dev)
898{
899 struct ipoib_mcast_iter *iter;
900
901 iter = kmalloc(sizeof *iter, GFP_KERNEL);
902 if (!iter)
903 return NULL;
904
905 iter->dev = dev;
Roland Dreier1732b0e2005-11-07 10:33:11 -0800906 memset(iter->mgid.raw, 0, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
908 if (ipoib_mcast_iter_next(iter)) {
Roland Dreier1732b0e2005-11-07 10:33:11 -0800909 kfree(iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 return NULL;
911 }
912
913 return iter;
914}
915
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916int ipoib_mcast_iter_next(struct ipoib_mcast_iter *iter)
917{
918 struct ipoib_dev_priv *priv = netdev_priv(iter->dev);
919 struct rb_node *n;
920 struct ipoib_mcast *mcast;
921 int ret = 1;
922
923 spin_lock_irq(&priv->lock);
924
925 n = rb_first(&priv->multicast_tree);
926
927 while (n) {
928 mcast = rb_entry(n, struct ipoib_mcast, rb_node);
929
930 if (memcmp(iter->mgid.raw, mcast->mcmember.mgid.raw,
931 sizeof (union ib_gid)) < 0) {
932 iter->mgid = mcast->mcmember.mgid;
933 iter->created = mcast->created;
934 iter->queuelen = skb_queue_len(&mcast->pkt_queue);
935 iter->complete = !!mcast->ah;
936 iter->send_only = !!(mcast->flags & (1 << IPOIB_MCAST_FLAG_SENDONLY));
937
938 ret = 0;
939
940 break;
941 }
942
943 n = rb_next(n);
944 }
945
946 spin_unlock_irq(&priv->lock);
947
948 return ret;
949}
950
951void ipoib_mcast_iter_read(struct ipoib_mcast_iter *iter,
952 union ib_gid *mgid,
953 unsigned long *created,
954 unsigned int *queuelen,
955 unsigned int *complete,
956 unsigned int *send_only)
957{
958 *mgid = iter->mgid;
959 *created = iter->created;
960 *queuelen = iter->queuelen;
961 *complete = iter->complete;
962 *send_only = iter->send_only;
963}
Roland Dreier8ae5a8a2005-11-02 20:51:01 -0800964
965#endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */