blob: eee66d13e5b9acf9a6c700777a7082f7bbdc8e7e [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;
Michael S. Tsirkinb36f170b62006-01-17 12:19:40 -080072 int tx_dropped = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Harvey Harrison5b095d9892008-10-29 12:52:50 -070074 ipoib_dbg_mcast(netdev_priv(dev), "deleting multicast group %pI6\n",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -070075 mcast->mcmember.mgid.raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +000077 /* remove all neigh connected to this mcast */
78 ipoib_del_neighs_by_gid(dev, mcast->mcmember.mgid.raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 if (mcast->ah)
81 ipoib_put_ah(mcast->ah);
82
Michael S. Tsirkinb36f170b62006-01-17 12:19:40 -080083 while (!skb_queue_empty(&mcast->pkt_queue)) {
84 ++tx_dropped;
Roland Dreier8c608a32005-11-07 10:49:38 -080085 dev_kfree_skb_any(skb_dequeue(&mcast->pkt_queue));
Michael S. Tsirkinb36f170b62006-01-17 12:19:40 -080086 }
87
Roland Dreier943c2462008-09-30 10:36:21 -070088 netif_tx_lock_bh(dev);
Roland Dreierde903512007-09-28 15:33:51 -070089 dev->stats.tx_dropped += tx_dropped;
Roland Dreier943c2462008-09-30 10:36:21 -070090 netif_tx_unlock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
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
Roland Dreierde6eb662005-11-02 07:23:14 -0800100 mcast = kzalloc(sizeof *mcast, can_sleep ? GFP_KERNEL : GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 if (!mcast)
102 return NULL;
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 mcast->dev = dev;
105 mcast->created = jiffies;
Hal Rosenstockce5b65c2005-09-18 13:47:53 -0700106 mcast->backoff = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108 INIT_LIST_HEAD(&mcast->list);
109 INIT_LIST_HEAD(&mcast->neigh_list);
110 skb_queue_head_init(&mcast->pkt_queue);
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 return mcast;
113}
114
Jack Morgenstein37c22a72006-05-29 19:14:05 +0300115static struct ipoib_mcast *__ipoib_mcast_find(struct net_device *dev, void *mgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
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
Jack Morgenstein37c22a72006-05-29 19:14:05 +0300126 ret = memcmp(mgid, mcast->mcmember.mgid.raw,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 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);
Eli Cohen7343b232006-02-27 20:47:43 -0800172 struct ipoib_ah *ah;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 int ret;
Eli Cohend0de1362008-07-14 23:48:50 -0700174 int set_qkey = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176 mcast->mcmember = *mcmember;
177
Patrick McHardybea1e222012-08-30 07:01:30 +0000178 /* Set the multicast MTU and cached Q_Key before we attach if it's
179 * the broadcast group.
180 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 if (!memcmp(mcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
182 sizeof (union ib_gid))) {
Jack Morgensteine1d50dc2008-05-20 15:41:09 -0700183 spin_lock_irq(&priv->lock);
184 if (!priv->broadcast) {
185 spin_unlock_irq(&priv->lock);
186 return -EAGAIN;
187 }
Patrick McHardybea1e222012-08-30 07:01:30 +0000188 priv->mcast_mtu = IPOIB_UD_MTU(ib_mtu_enum_to_int(priv->broadcast->mcmember.mtu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 priv->qkey = be32_to_cpu(priv->broadcast->mcmember.qkey);
Jack Morgensteine1d50dc2008-05-20 15:41:09 -0700190 spin_unlock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 priv->tx_wr.wr.ud.remote_qkey = priv->qkey;
Eli Cohend0de1362008-07-14 23:48:50 -0700192 set_qkey = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 }
194
195 if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
196 if (test_and_set_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700197 ipoib_warn(priv, "multicast group %pI6 already attached\n",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700198 mcast->mcmember.mgid.raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200 return 0;
201 }
202
203 ret = ipoib_mcast_attach(dev, be16_to_cpu(mcast->mcmember.mlid),
Eli Cohend0de1362008-07-14 23:48:50 -0700204 &mcast->mcmember.mgid, set_qkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 if (ret < 0) {
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700206 ipoib_warn(priv, "couldn't attach QP to multicast group %pI6\n",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700207 mcast->mcmember.mgid.raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209 clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags);
210 return ret;
211 }
212 }
213
214 {
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,
Jack Morgensteinbf6a9e32006-04-10 09:43:47 -0700220 .static_rate = mcast->mcmember.rate,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 .grh = {
222 .flow_label = be32_to_cpu(mcast->mcmember.flow_label),
223 .hop_limit = mcast->mcmember.hop_limit,
224 .sgid_index = 0,
225 .traffic_class = mcast->mcmember.traffic_class
226 }
227 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 av.grh.dgid = mcast->mcmember.mgid;
229
Eli Cohen7343b232006-02-27 20:47:43 -0800230 ah = ipoib_create_ah(dev, priv->pd, &av);
Mike Marciniszyn38743972011-11-21 08:43:54 -0500231 if (IS_ERR(ah)) {
232 ipoib_warn(priv, "ib_address_create failed %ld\n",
233 -PTR_ERR(ah));
234 /* use original error */
235 return PTR_ERR(ah);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 } else {
Or Gerlitz624d01f2006-07-24 10:42:00 +0300237 spin_lock_irq(&priv->lock);
238 mcast->ah = ah;
239 spin_unlock_irq(&priv->lock);
240
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700241 ipoib_dbg_mcast(priv, "MGID %pI6 AV %p, LID 0x%04x, SL %d\n",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700242 mcast->mcmember.mgid.raw,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 mcast->ah->ah,
244 be16_to_cpu(mcast->mcmember.mlid),
245 mcast->mcmember.sl);
246 }
247 }
248
249 /* actually send any queued packets */
Roland Dreier943c2462008-09-30 10:36:21 -0700250 netif_tx_lock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 while (!skb_queue_empty(&mcast->pkt_queue)) {
252 struct sk_buff *skb = skb_dequeue(&mcast->pkt_queue);
David S. Miller69cce1d2011-07-17 23:09:49 -0700253
Roland Dreier943c2462008-09-30 10:36:21 -0700254 netif_tx_unlock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256 skb->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 if (dev_queue_xmit(skb))
258 ipoib_warn(priv, "dev_queue_xmit failed to requeue packet\n");
Roland Dreier936d7de2012-02-07 14:51:21 +0000259
Roland Dreier943c2462008-09-30 10:36:21 -0700260 netif_tx_lock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 }
Roland Dreier943c2462008-09-30 10:36:21 -0700262 netif_tx_unlock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264 return 0;
265}
266
Sean Heftyfaec2f72007-02-15 17:00:17 -0800267static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268ipoib_mcast_sendonly_join_complete(int status,
Sean Heftyfaec2f72007-02-15 17:00:17 -0800269 struct ib_sa_multicast *multicast)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
Sean Heftyfaec2f72007-02-15 17:00:17 -0800271 struct ipoib_mcast *mcast = multicast->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 struct net_device *dev = mcast->dev;
273
Sean Heftyfaec2f72007-02-15 17:00:17 -0800274 /* We trap for port events ourselves. */
275 if (status == -ENETRESET)
276 return 0;
277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 if (!status)
Sean Heftyfaec2f72007-02-15 17:00:17 -0800279 status = ipoib_mcast_join_finish(mcast, &multicast->rec);
280
281 if (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 if (mcast->logcount++ < 20)
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700283 ipoib_dbg_mcast(netdev_priv(dev), "multicast join failed for %pI6, status %d\n",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700284 mcast->mcmember.mgid.raw, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286 /* Flush out any queued packets */
Roland Dreier943c2462008-09-30 10:36:21 -0700287 netif_tx_lock_bh(dev);
Michael S. Tsirkinb36f170b62006-01-17 12:19:40 -0800288 while (!skb_queue_empty(&mcast->pkt_queue)) {
Roland Dreierde903512007-09-28 15:33:51 -0700289 ++dev->stats.tx_dropped;
Roland Dreier8c608a32005-11-07 10:49:38 -0800290 dev_kfree_skb_any(skb_dequeue(&mcast->pkt_queue));
Michael S. Tsirkinb36f170b62006-01-17 12:19:40 -0800291 }
Roland Dreier943c2462008-09-30 10:36:21 -0700292 netif_tx_unlock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
294 /* Clear the busy flag so we try again */
Sean Heftyfaec2f72007-02-15 17:00:17 -0800295 status = test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY,
296 &mcast->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 }
Sean Heftyfaec2f72007-02-15 17:00:17 -0800298 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299}
300
301static int ipoib_mcast_sendonly_join(struct ipoib_mcast *mcast)
302{
303 struct net_device *dev = mcast->dev;
304 struct ipoib_dev_priv *priv = netdev_priv(dev);
305 struct ib_sa_mcmember_rec rec = {
306#if 0 /* Some SMs don't support send-only yet */
307 .join_state = 4
308#else
309 .join_state = 1
310#endif
311 };
312 int ret = 0;
313
314 if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
315 ipoib_dbg_mcast(priv, "device shutting down, no multicast joins\n");
316 return -ENODEV;
317 }
318
319 if (test_and_set_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags)) {
320 ipoib_dbg_mcast(priv, "multicast entry busy, skipping\n");
321 return -EBUSY;
322 }
323
324 rec.mgid = mcast->mcmember.mgid;
325 rec.port_gid = priv->local_gid;
Sean Hefty97f52eb2005-08-13 21:05:57 -0700326 rec.pkey = cpu_to_be16(priv->pkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Sean Heftyfaec2f72007-02-15 17:00:17 -0800328 mcast->mc = ib_sa_join_multicast(&ipoib_sa_client, priv->ca,
329 priv->port, &rec,
330 IB_SA_MCMEMBER_REC_MGID |
331 IB_SA_MCMEMBER_REC_PORT_GID |
332 IB_SA_MCMEMBER_REC_PKEY |
333 IB_SA_MCMEMBER_REC_JOIN_STATE,
334 GFP_ATOMIC,
335 ipoib_mcast_sendonly_join_complete,
336 mcast);
337 if (IS_ERR(mcast->mc)) {
338 ret = PTR_ERR(mcast->mc);
339 clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
340 ipoib_warn(priv, "ib_sa_join_multicast failed (ret = %d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 ret);
342 } else {
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700343 ipoib_dbg_mcast(priv, "no multicast record for %pI6, starting join\n",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700344 mcast->mcmember.mgid.raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 }
346
347 return ret;
348}
349
Yossi Etigine8224e42008-09-16 11:57:45 -0700350void ipoib_mcast_carrier_on_task(struct work_struct *work)
351{
352 struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
353 carrier_on_task);
Moni Shoua5ee951202009-09-24 12:01:05 -0700354 struct ib_port_attr attr;
Yossi Etigine8224e42008-09-16 11:57:45 -0700355
356 /*
357 * Take rtnl_lock to avoid racing with ipoib_stop() and
358 * turning the carrier back on while a device is being
359 * removed.
360 */
Moni Shoua5ee951202009-09-24 12:01:05 -0700361 if (ib_query_port(priv->ca, priv->port, &attr) ||
362 attr.state != IB_PORT_ACTIVE) {
363 ipoib_dbg(priv, "Keeping carrier off until IB port is active\n");
364 return;
365 }
366
Yossi Etigine8224e42008-09-16 11:57:45 -0700367 rtnl_lock();
Doug Ledfordafe1de62014-12-10 11:46:58 -0500368 if (!ipoib_cm_admin_enabled(priv->dev))
369 dev_set_mtu(priv->dev, min(priv->mcast_mtu, priv->admin_mtu));
Yossi Etigine8224e42008-09-16 11:57:45 -0700370 netif_carrier_on(priv->dev);
371 rtnl_unlock();
372}
373
Sean Heftyfaec2f72007-02-15 17:00:17 -0800374static int ipoib_mcast_join_complete(int status,
375 struct ib_sa_multicast *multicast)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
Sean Heftyfaec2f72007-02-15 17:00:17 -0800377 struct ipoib_mcast *mcast = multicast->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 struct net_device *dev = mcast->dev;
379 struct ipoib_dev_priv *priv = netdev_priv(dev);
380
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700381 ipoib_dbg_mcast(priv, "join completion for %pI6 (status %d)\n",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700382 mcast->mcmember.mgid.raw, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Sean Heftyfaec2f72007-02-15 17:00:17 -0800384 /* We trap for port events ourselves. */
Erez Shitrita9c8ba52013-10-16 17:37:51 +0300385 if (status == -ENETRESET) {
386 status = 0;
387 goto out;
388 }
Sean Heftyfaec2f72007-02-15 17:00:17 -0800389
390 if (!status)
391 status = ipoib_mcast_join_finish(mcast, &multicast->rec);
392
393 if (!status) {
Hal Rosenstockce5b65c2005-09-18 13:47:53 -0700394 mcast->backoff = 1;
Ingo Molnar95ed6442006-01-13 14:51:39 -0800395 mutex_lock(&mcast_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
David Howellsc4028952006-11-22 14:57:56 +0000397 queue_delayed_work(ipoib_workqueue,
398 &priv->mcast_task, 0);
Ingo Molnar95ed6442006-01-13 14:51:39 -0800399 mutex_unlock(&mcast_mutex);
Shirley Ma55c9add2007-03-08 14:59:30 -0800400
Yossi Etigine8224e42008-09-16 11:57:45 -0700401 /*
402 * Defer carrier on work to ipoib_workqueue to avoid a
403 * deadlock on rtnl_lock here.
404 */
405 if (mcast == priv->broadcast)
406 queue_work(ipoib_workqueue, &priv->carrier_on_task);
Shirley Ma55c9add2007-03-08 14:59:30 -0800407
Erez Shitrita9c8ba52013-10-16 17:37:51 +0300408 status = 0;
409 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 }
411
Sean Heftyfaec2f72007-02-15 17:00:17 -0800412 if (mcast->logcount++ < 20) {
Yossi Etigin3c209622009-01-16 13:42:59 -0800413 if (status == -ETIMEDOUT || status == -EAGAIN) {
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700414 ipoib_dbg_mcast(priv, "multicast join failed for %pI6, status %d\n",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700415 mcast->mcmember.mgid.raw, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 } else {
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700417 ipoib_warn(priv, "multicast join failed for %pI6, status %d\n",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700418 mcast->mcmember.mgid.raw, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 }
420 }
421
422 mcast->backoff *= 2;
423 if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
424 mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
425
Sean Heftyfaec2f72007-02-15 17:00:17 -0800426 /* Clear the busy flag so we try again */
427 status = test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
428
Michael S. Tsirkin9acf6a82006-03-02 11:07:47 -0800429 mutex_lock(&mcast_mutex);
Michael S. Tsirkin9acf6a82006-03-02 11:07:47 -0800430 spin_lock_irq(&priv->lock);
Sean Heftyfaec2f72007-02-15 17:00:17 -0800431 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
432 queue_delayed_work(ipoib_workqueue, &priv->mcast_task,
433 mcast->backoff * HZ);
Michael S. Tsirkin9acf6a82006-03-02 11:07:47 -0800434 spin_unlock_irq(&priv->lock);
Ingo Molnar95ed6442006-01-13 14:51:39 -0800435 mutex_unlock(&mcast_mutex);
Erez Shitrita9c8ba52013-10-16 17:37:51 +0300436out:
437 complete(&mcast->done);
Sean Heftyfaec2f72007-02-15 17:00:17 -0800438 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439}
440
441static void ipoib_mcast_join(struct net_device *dev, struct ipoib_mcast *mcast,
442 int create)
443{
444 struct ipoib_dev_priv *priv = netdev_priv(dev);
445 struct ib_sa_mcmember_rec rec = {
446 .join_state = 1
447 };
448 ib_sa_comp_mask comp_mask;
449 int ret = 0;
450
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700451 ipoib_dbg_mcast(priv, "joining MGID %pI6\n", mcast->mcmember.mgid.raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453 rec.mgid = mcast->mcmember.mgid;
454 rec.port_gid = priv->local_gid;
Sean Hefty97f52eb2005-08-13 21:05:57 -0700455 rec.pkey = cpu_to_be16(priv->pkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
457 comp_mask =
458 IB_SA_MCMEMBER_REC_MGID |
459 IB_SA_MCMEMBER_REC_PORT_GID |
460 IB_SA_MCMEMBER_REC_PKEY |
461 IB_SA_MCMEMBER_REC_JOIN_STATE;
462
463 if (create) {
464 comp_mask |=
Roland Dreierd0df6d62006-09-22 15:22:56 -0700465 IB_SA_MCMEMBER_REC_QKEY |
466 IB_SA_MCMEMBER_REC_MTU_SELECTOR |
467 IB_SA_MCMEMBER_REC_MTU |
468 IB_SA_MCMEMBER_REC_TRAFFIC_CLASS |
469 IB_SA_MCMEMBER_REC_RATE_SELECTOR |
470 IB_SA_MCMEMBER_REC_RATE |
471 IB_SA_MCMEMBER_REC_SL |
472 IB_SA_MCMEMBER_REC_FLOW_LABEL |
473 IB_SA_MCMEMBER_REC_HOP_LIMIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
475 rec.qkey = priv->broadcast->mcmember.qkey;
Roland Dreierd0df6d62006-09-22 15:22:56 -0700476 rec.mtu_selector = IB_SA_EQ;
477 rec.mtu = priv->broadcast->mcmember.mtu;
478 rec.traffic_class = priv->broadcast->mcmember.traffic_class;
479 rec.rate_selector = IB_SA_EQ;
480 rec.rate = priv->broadcast->mcmember.rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 rec.sl = priv->broadcast->mcmember.sl;
482 rec.flow_label = priv->broadcast->mcmember.flow_label;
Roland Dreierd0df6d62006-09-22 15:22:56 -0700483 rec.hop_limit = priv->broadcast->mcmember.hop_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 }
485
Sean Heftyfaec2f72007-02-15 17:00:17 -0800486 set_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
Erez Shitrita9c8ba52013-10-16 17:37:51 +0300487 init_completion(&mcast->done);
488 set_bit(IPOIB_MCAST_JOIN_STARTED, &mcast->flags);
489
Sean Heftyfaec2f72007-02-15 17:00:17 -0800490 mcast->mc = ib_sa_join_multicast(&ipoib_sa_client, priv->ca, priv->port,
491 &rec, comp_mask, GFP_KERNEL,
492 ipoib_mcast_join_complete, mcast);
493 if (IS_ERR(mcast->mc)) {
494 clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
Erez Shitrita9c8ba52013-10-16 17:37:51 +0300495 complete(&mcast->done);
Sean Heftyfaec2f72007-02-15 17:00:17 -0800496 ret = PTR_ERR(mcast->mc);
497 ipoib_warn(priv, "ib_sa_join_multicast failed, status %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499 mcast->backoff *= 2;
500 if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
501 mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
502
Ingo Molnar95ed6442006-01-13 14:51:39 -0800503 mutex_lock(&mcast_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
505 queue_delayed_work(ipoib_workqueue,
506 &priv->mcast_task,
Hal Rosenstockce5b65c2005-09-18 13:47:53 -0700507 mcast->backoff * HZ);
Ingo Molnar95ed6442006-01-13 14:51:39 -0800508 mutex_unlock(&mcast_mutex);
Sean Heftyfaec2f72007-02-15 17:00:17 -0800509 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510}
511
David Howellsc4028952006-11-22 14:57:56 +0000512void ipoib_mcast_join_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
David Howellsc4028952006-11-22 14:57:56 +0000514 struct ipoib_dev_priv *priv =
515 container_of(work, struct ipoib_dev_priv, mcast_task.work);
516 struct net_device *dev = priv->dev;
Erez Shitrit94232d92013-10-16 17:37:53 +0300517 struct ib_port_attr port_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
519 if (!test_bit(IPOIB_MCAST_RUN, &priv->flags))
520 return;
521
Erez Shitrit94232d92013-10-16 17:37:53 +0300522 if (ib_query_port(priv->ca, priv->port, &port_attr) ||
523 port_attr.state != IB_PORT_ACTIVE) {
524 ipoib_dbg(priv, "port state is not ACTIVE (state = %d) suspending join task\n",
525 port_attr.state);
526 return;
527 }
Alex Estrin68f9d832014-08-20 11:15:08 -0400528 priv->local_lid = port_attr.lid;
Erez Shitrit94232d92013-10-16 17:37:53 +0300529
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 if (ib_query_gid(priv->ca, priv->port, 0, &priv->local_gid))
Michael S. Tsirkin24bd1e42007-05-18 16:12:54 +0300531 ipoib_warn(priv, "ib_query_gid() failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 else
533 memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));
534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 if (!priv->broadcast) {
Roland Dreier20b83382006-02-11 12:22:12 -0800536 struct ipoib_mcast *broadcast;
537
Yossi Etigin50df48f2009-01-12 19:28:42 -0800538 if (!test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
539 return;
540
Roland Dreier20b83382006-02-11 12:22:12 -0800541 broadcast = ipoib_mcast_alloc(dev, 1);
542 if (!broadcast) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 ipoib_warn(priv, "failed to allocate broadcast group\n");
Ingo Molnar95ed6442006-01-13 14:51:39 -0800544 mutex_lock(&mcast_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
546 queue_delayed_work(ipoib_workqueue,
547 &priv->mcast_task, HZ);
Ingo Molnar95ed6442006-01-13 14:51:39 -0800548 mutex_unlock(&mcast_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 return;
550 }
551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 spin_lock_irq(&priv->lock);
Roland Dreier20b83382006-02-11 12:22:12 -0800553 memcpy(broadcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
554 sizeof (union ib_gid));
555 priv->broadcast = broadcast;
556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 __ipoib_mcast_add(dev, priv->broadcast);
558 spin_unlock_irq(&priv->lock);
559 }
560
561 if (!test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
Sean Heftyfaec2f72007-02-15 17:00:17 -0800562 if (!test_bit(IPOIB_MCAST_FLAG_BUSY, &priv->broadcast->flags))
563 ipoib_mcast_join(dev, priv->broadcast, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 return;
565 }
566
567 while (1) {
568 struct ipoib_mcast *mcast = NULL;
569
570 spin_lock_irq(&priv->lock);
571 list_for_each_entry(mcast, &priv->multicast_list, list) {
572 if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)
573 && !test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags)
574 && !test_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
575 /* Found the next unjoined group */
576 break;
577 }
578 }
579 spin_unlock_irq(&priv->lock);
580
581 if (&mcast->list == &priv->multicast_list) {
582 /* All done */
583 break;
584 }
585
586 ipoib_mcast_join(dev, mcast, 1);
587 return;
588 }
589
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 ipoib_dbg_mcast(priv, "successfully joined all multicast groups\n");
591
592 clear_bit(IPOIB_MCAST_RUN, &priv->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593}
594
595int ipoib_mcast_start_thread(struct net_device *dev)
596{
597 struct ipoib_dev_priv *priv = netdev_priv(dev);
598
599 ipoib_dbg_mcast(priv, "starting multicast thread\n");
600
Ingo Molnar95ed6442006-01-13 14:51:39 -0800601 mutex_lock(&mcast_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 if (!test_and_set_bit(IPOIB_MCAST_RUN, &priv->flags))
David Howellsc4028952006-11-22 14:57:56 +0000603 queue_delayed_work(ipoib_workqueue, &priv->mcast_task, 0);
Ingo Molnar95ed6442006-01-13 14:51:39 -0800604 mutex_unlock(&mcast_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606 return 0;
607}
608
Roland Dreier8d2cae02005-09-20 10:52:04 -0700609int ipoib_mcast_stop_thread(struct net_device *dev, int flush)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
611 struct ipoib_dev_priv *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
613 ipoib_dbg_mcast(priv, "stopping multicast thread\n");
614
Ingo Molnar95ed6442006-01-13 14:51:39 -0800615 mutex_lock(&mcast_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 clear_bit(IPOIB_MCAST_RUN, &priv->flags);
617 cancel_delayed_work(&priv->mcast_task);
Ingo Molnar95ed6442006-01-13 14:51:39 -0800618 mutex_unlock(&mcast_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Roland Dreier8d2cae02005-09-20 10:52:04 -0700620 if (flush)
621 flush_workqueue(ipoib_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 return 0;
624}
625
626static int ipoib_mcast_leave(struct net_device *dev, struct ipoib_mcast *mcast)
627{
628 struct ipoib_dev_priv *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 int ret = 0;
630
Sean Heftye07832b2007-03-19 14:31:36 -0800631 if (test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
632 ib_sa_free_multicast(mcast->mc);
633
Sean Heftyfaec2f72007-02-15 17:00:17 -0800634 if (test_and_clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700635 ipoib_dbg_mcast(priv, "leaving MGID %pI6\n",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700636 mcast->mcmember.mgid.raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Sean Heftyfaec2f72007-02-15 17:00:17 -0800638 /* Remove ourselves from the multicast group */
Roland Dreier9eae5542008-07-14 23:48:50 -0700639 ret = ib_detach_mcast(priv->qp, &mcast->mcmember.mgid,
640 be16_to_cpu(mcast->mcmember.mlid));
Sean Heftyfaec2f72007-02-15 17:00:17 -0800641 if (ret)
Roland Dreier9eae5542008-07-14 23:48:50 -0700642 ipoib_warn(priv, "ib_detach_mcast failed (result = %d)\n", ret);
Sean Heftyfaec2f72007-02-15 17:00:17 -0800643 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 return 0;
646}
647
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000648void ipoib_mcast_send(struct net_device *dev, u8 *daddr, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649{
650 struct ipoib_dev_priv *priv = netdev_priv(dev);
651 struct ipoib_mcast *mcast;
Roland Dreier943c2462008-09-30 10:36:21 -0700652 unsigned long flags;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000653 void *mgid = daddr + 4;
David S. Miller700db992012-07-05 21:08:05 -0700654
Roland Dreier943c2462008-09-30 10:36:21 -0700655 spin_lock_irqsave(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Or Gerlitzb3e27492008-03-11 16:10:02 +0200657 if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags) ||
Roland Dreier20b83382006-02-11 12:22:12 -0800658 !priv->broadcast ||
659 !test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
Roland Dreierde903512007-09-28 15:33:51 -0700660 ++dev->stats.tx_dropped;
Michael S. Tsirkin479a0792006-02-07 16:37:08 -0800661 dev_kfree_skb_any(skb);
662 goto unlock;
663 }
664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 mcast = __ipoib_mcast_find(dev, mgid);
666 if (!mcast) {
667 /* Let's create a new send only group now */
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700668 ipoib_dbg_mcast(priv, "setting up send only multicast group for %pI6\n",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700669 mgid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
671 mcast = ipoib_mcast_alloc(dev, 0);
672 if (!mcast) {
673 ipoib_warn(priv, "unable to allocate memory for "
674 "multicast structure\n");
Roland Dreierde903512007-09-28 15:33:51 -0700675 ++dev->stats.tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 dev_kfree_skb_any(skb);
677 goto out;
678 }
679
680 set_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags);
Jack Morgenstein37c22a72006-05-29 19:14:05 +0300681 memcpy(mcast->mcmember.mgid.raw, mgid, sizeof (union ib_gid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 __ipoib_mcast_add(dev, mcast);
683 list_add_tail(&mcast->list, &priv->multicast_list);
684 }
685
686 if (!mcast->ah) {
687 if (skb_queue_len(&mcast->pkt_queue) < IPOIB_MAX_MCAST_QUEUE)
688 skb_queue_tail(&mcast->pkt_queue, skb);
Michael S. Tsirkinb36f170b62006-01-17 12:19:40 -0800689 else {
Roland Dreierde903512007-09-28 15:33:51 -0700690 ++dev->stats.tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 dev_kfree_skb_any(skb);
Michael S. Tsirkinb36f170b62006-01-17 12:19:40 -0800692 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Sean Heftyfaec2f72007-02-15 17:00:17 -0800694 if (test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 ipoib_dbg_mcast(priv, "no address vector, "
696 "but multicast join already started\n");
697 else if (test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags))
698 ipoib_mcast_sendonly_join(mcast);
699
700 /*
701 * If lookup completes between here and out:, don't
702 * want to send packet twice.
703 */
704 mcast = NULL;
705 }
706
707out:
708 if (mcast && mcast->ah) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000709 struct ipoib_neigh *neigh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000711 spin_unlock_irqrestore(&priv->lock, flags);
712 neigh = ipoib_neigh_get(dev, daddr);
713 spin_lock_irqsave(&priv->lock, flags);
714 if (!neigh) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000715 neigh = ipoib_neigh_alloc(daddr, dev);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000716 if (neigh) {
717 kref_get(&mcast->ah->ref);
718 neigh->ah = mcast->ah;
719 list_add_tail(&neigh->list, &mcast->neigh_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 }
721 }
Roland Dreier721d67c2009-09-05 20:23:40 -0700722 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 ipoib_send(dev, skb, mcast->ah, IB_MULTICAST_QPN);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000724 if (neigh)
725 ipoib_neigh_put(neigh);
Roland Dreier721d67c2009-09-05 20:23:40 -0700726 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 }
728
Michael S. Tsirkin479a0792006-02-07 16:37:08 -0800729unlock:
Roland Dreier943c2462008-09-30 10:36:21 -0700730 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731}
732
733void ipoib_mcast_dev_flush(struct net_device *dev)
734{
735 struct ipoib_dev_priv *priv = netdev_priv(dev);
736 LIST_HEAD(remove_list);
Eli Cohen988bd502006-01-12 14:32:20 -0800737 struct ipoib_mcast *mcast, *tmcast;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 unsigned long flags;
739
740 ipoib_dbg_mcast(priv, "flushing multicast list\n");
741
742 spin_lock_irqsave(&priv->lock, flags);
Eli Cohen988bd502006-01-12 14:32:20 -0800743
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
Eli Cohen988bd502006-01-12 14:32:20 -0800745 list_del(&mcast->list);
746 rb_erase(&mcast->rb_node, &priv->multicast_tree);
747 list_add_tail(&mcast->list, &remove_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 }
749
750 if (priv->broadcast) {
Roland Dreier3cd96562006-09-22 15:22:46 -0700751 rb_erase(&priv->broadcast->rb_node, &priv->multicast_tree);
Eli Cohen988bd502006-01-12 14:32:20 -0800752 list_add_tail(&priv->broadcast->list, &remove_list);
753 priv->broadcast = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 }
755
756 spin_unlock_irqrestore(&priv->lock, flags);
757
Erez Shitrita9c8ba52013-10-16 17:37:51 +0300758 /* seperate between the wait to the leave*/
759 list_for_each_entry_safe(mcast, tmcast, &remove_list, list)
760 if (test_bit(IPOIB_MCAST_JOIN_STARTED, &mcast->flags))
761 wait_for_completion(&mcast->done);
762
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
764 ipoib_mcast_leave(dev, mcast);
765 ipoib_mcast_free(mcast);
766 }
767}
768
Jiri Pirko3e4aa12f82010-03-22 03:21:39 +0000769static int ipoib_mcast_addr_is_valid(const u8 *addr, const u8 *broadcast)
Jason Gunthorpe5e475962009-09-05 20:23:40 -0700770{
Jason Gunthorpe5e475962009-09-05 20:23:40 -0700771 /* reserved QPN, prefix, scope */
772 if (memcmp(addr, broadcast, 6))
773 return 0;
774 /* signature lower, pkey */
775 if (memcmp(addr + 7, broadcast + 7, 3))
776 return 0;
777 return 1;
778}
779
David Howellsc4028952006-11-22 14:57:56 +0000780void ipoib_mcast_restart_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
David Howellsc4028952006-11-22 14:57:56 +0000782 struct ipoib_dev_priv *priv =
783 container_of(work, struct ipoib_dev_priv, restart_task);
784 struct net_device *dev = priv->dev;
Jiri Pirko22bedad32010-04-01 21:22:57 +0000785 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 struct ipoib_mcast *mcast, *tmcast;
787 LIST_HEAD(remove_list);
788 unsigned long flags;
Or Gerlitz335a64a5a2007-10-08 10:13:00 +0200789 struct ib_sa_mcmember_rec rec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791 ipoib_dbg_mcast(priv, "restarting multicast task\n");
792
Roland Dreier8d2cae02005-09-20 10:52:04 -0700793 ipoib_mcast_stop_thread(dev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
Herbert Xu932ff272006-06-09 12:20:56 -0700795 local_irq_save(flags);
David S. Millere308a5d2008-07-15 00:13:44 -0700796 netif_addr_lock(dev);
Michael S. Tsirkin78bfe0b2006-01-11 11:47:34 -0800797 spin_lock(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
799 /*
800 * Unfortunately, the networking core only gives us a list of all of
801 * the multicast hardware addresses. We need to figure out which ones
802 * are new and which ones have been removed
803 */
804
805 /* Clear out the found flag */
806 list_for_each_entry(mcast, &priv->multicast_list, list)
807 clear_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
808
809 /* Mark all of the entries that are found or don't exist */
Jiri Pirko22bedad32010-04-01 21:22:57 +0000810 netdev_for_each_mc_addr(ha, dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 union ib_gid mgid;
812
Jiri Pirko22bedad32010-04-01 21:22:57 +0000813 if (!ipoib_mcast_addr_is_valid(ha->addr, dev->broadcast))
Jason Gunthorpe5e475962009-09-05 20:23:40 -0700814 continue;
815
Jiri Pirko22bedad32010-04-01 21:22:57 +0000816 memcpy(mgid.raw, ha->addr + 4, sizeof mgid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 mcast = __ipoib_mcast_find(dev, &mgid);
819 if (!mcast || test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
820 struct ipoib_mcast *nmcast;
821
Or Gerlitz335a64a5a2007-10-08 10:13:00 +0200822 /* ignore group which is directly joined by userspace */
823 if (test_bit(IPOIB_FLAG_UMCAST, &priv->flags) &&
824 !ib_sa_get_mcmember_rec(priv->ca, priv->port, &mgid, &rec)) {
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700825 ipoib_dbg_mcast(priv, "ignoring multicast entry for mgid %pI6\n",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700826 mgid.raw);
Or Gerlitz335a64a5a2007-10-08 10:13:00 +0200827 continue;
828 }
829
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 /* Not found or send-only group, let's add a new entry */
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700831 ipoib_dbg_mcast(priv, "adding multicast entry for mgid %pI6\n",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700832 mgid.raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
834 nmcast = ipoib_mcast_alloc(dev, 0);
835 if (!nmcast) {
836 ipoib_warn(priv, "unable to allocate memory for multicast structure\n");
837 continue;
838 }
839
840 set_bit(IPOIB_MCAST_FLAG_FOUND, &nmcast->flags);
841
842 nmcast->mcmember.mgid = mgid;
843
844 if (mcast) {
845 /* Destroy the send only entry */
Akinobu Mita179e0912006-06-26 00:24:41 -0700846 list_move_tail(&mcast->list, &remove_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
848 rb_replace_node(&mcast->rb_node,
849 &nmcast->rb_node,
850 &priv->multicast_tree);
851 } else
852 __ipoib_mcast_add(dev, nmcast);
853
854 list_add_tail(&nmcast->list, &priv->multicast_list);
855 }
856
857 if (mcast)
858 set_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
859 }
860
861 /* Remove all of the entries don't exist anymore */
862 list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
863 if (!test_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags) &&
864 !test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700865 ipoib_dbg_mcast(priv, "deleting multicast group %pI6\n",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700866 mcast->mcmember.mgid.raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
868 rb_erase(&mcast->rb_node, &priv->multicast_tree);
869
870 /* Move to the remove list */
Akinobu Mita179e0912006-06-26 00:24:41 -0700871 list_move_tail(&mcast->list, &remove_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 }
873 }
Michael S. Tsirkin78bfe0b2006-01-11 11:47:34 -0800874
875 spin_unlock(&priv->lock);
David S. Millere308a5d2008-07-15 00:13:44 -0700876 netif_addr_unlock(dev);
Herbert Xu932ff272006-06-09 12:20:56 -0700877 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
879 /* We have to cancel outside of the spinlock */
880 list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
881 ipoib_mcast_leave(mcast->dev, mcast);
882 ipoib_mcast_free(mcast);
883 }
884
885 if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
886 ipoib_mcast_start_thread(dev);
887}
888
Roland Dreier8ae5a8a2005-11-02 20:51:01 -0800889#ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
890
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891struct ipoib_mcast_iter *ipoib_mcast_iter_init(struct net_device *dev)
892{
893 struct ipoib_mcast_iter *iter;
894
895 iter = kmalloc(sizeof *iter, GFP_KERNEL);
896 if (!iter)
897 return NULL;
898
899 iter->dev = dev;
Roland Dreier1732b0e2005-11-07 10:33:11 -0800900 memset(iter->mgid.raw, 0, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
902 if (ipoib_mcast_iter_next(iter)) {
Roland Dreier1732b0e2005-11-07 10:33:11 -0800903 kfree(iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 return NULL;
905 }
906
907 return iter;
908}
909
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910int ipoib_mcast_iter_next(struct ipoib_mcast_iter *iter)
911{
912 struct ipoib_dev_priv *priv = netdev_priv(iter->dev);
913 struct rb_node *n;
914 struct ipoib_mcast *mcast;
915 int ret = 1;
916
917 spin_lock_irq(&priv->lock);
918
919 n = rb_first(&priv->multicast_tree);
920
921 while (n) {
922 mcast = rb_entry(n, struct ipoib_mcast, rb_node);
923
924 if (memcmp(iter->mgid.raw, mcast->mcmember.mgid.raw,
925 sizeof (union ib_gid)) < 0) {
926 iter->mgid = mcast->mcmember.mgid;
927 iter->created = mcast->created;
928 iter->queuelen = skb_queue_len(&mcast->pkt_queue);
929 iter->complete = !!mcast->ah;
930 iter->send_only = !!(mcast->flags & (1 << IPOIB_MCAST_FLAG_SENDONLY));
931
932 ret = 0;
933
934 break;
935 }
936
937 n = rb_next(n);
938 }
939
940 spin_unlock_irq(&priv->lock);
941
942 return ret;
943}
944
945void ipoib_mcast_iter_read(struct ipoib_mcast_iter *iter,
946 union ib_gid *mgid,
947 unsigned long *created,
948 unsigned int *queuelen,
949 unsigned int *complete,
950 unsigned int *send_only)
951{
952 *mgid = iter->mgid;
953 *created = iter->created;
954 *queuelen = iter->queuelen;
955 *complete = iter->complete;
956 *send_only = iter->send_only;
957}
Roland Dreier8ae5a8a2005-11-02 20:51:01 -0800958
959#endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */