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