1 /* 2 * Copyright (c) 2004, 2005 Topspin Communications. All rights reserved. 3 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved. 4 * Copyright (c) 2004 Voltaire, Inc. All rights reserved. 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. 33 * 34 * $Id: ipoib_multicast.c 1362 2004-12-18 15:56:29Z roland $ 35 */ 36 37 #include <linux/skbuff.h> 38 #include <linux/rtnetlink.h> 39 #include <linux/ip.h> 40 #include <linux/in.h> 41 #include <linux/igmp.h> 42 #include <linux/inetdevice.h> 43 #include <linux/delay.h> 44 #include <linux/completion.h> 45 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 56 static DECLARE_MUTEX(mcast_mutex); 57 58 /* Used for all multicast joins (broadcast, IPv4 mcast and IPv6 mcast) */ 59 struct ipoib_mcast { 60 struct ib_sa_mcmember_rec mcmember; 61 struct ipoib_ah *ah; 62 63 struct rb_node rb_node; 64 struct list_head list; 65 struct completion done; 66 67 int query_id; 68 struct ib_sa_query *query; 69 70 unsigned long created; 71 unsigned long backoff; 72 73 unsigned long flags; 74 unsigned char logcount; 75 76 struct list_head neigh_list; 77 78 struct sk_buff_head pkt_queue; 79 80 struct net_device *dev; 81 }; 82 83 struct ipoib_mcast_iter { 84 struct net_device *dev; 85 union ib_gid mgid; 86 unsigned long created; 87 unsigned int queuelen; 88 unsigned int complete; 89 unsigned int send_only; 90 }; 91 92 static void ipoib_mcast_free(struct ipoib_mcast *mcast) 93 { 94 struct net_device *dev = mcast->dev; 95 struct ipoib_dev_priv *priv = netdev_priv(dev); 96 struct ipoib_neigh *neigh, *tmp; 97 unsigned long flags; 98 LIST_HEAD(ah_list); 99 struct ipoib_ah *ah, *tah; 100 101 ipoib_dbg_mcast(netdev_priv(dev), 102 "deleting multicast group " IPOIB_GID_FMT "\n", 103 IPOIB_GID_ARG(mcast->mcmember.mgid)); 104 105 spin_lock_irqsave(&priv->lock, flags); 106 107 list_for_each_entry_safe(neigh, tmp, &mcast->neigh_list, list) { 108 if (neigh->ah) 109 list_add_tail(&neigh->ah->list, &ah_list); 110 *to_ipoib_neigh(neigh->neighbour) = NULL; 111 neigh->neighbour->ops->destructor = NULL; 112 kfree(neigh); 113 } 114 115 spin_unlock_irqrestore(&priv->lock, flags); 116 117 list_for_each_entry_safe(ah, tah, &ah_list, list) 118 ipoib_put_ah(ah); 119 120 if (mcast->ah) 121 ipoib_put_ah(mcast->ah); 122 123 while (!skb_queue_empty(&mcast->pkt_queue)) 124 dev_kfree_skb_any(skb_dequeue(&mcast->pkt_queue)); 125 126 kfree(mcast); 127 } 128 129 static struct ipoib_mcast *ipoib_mcast_alloc(struct net_device *dev, 130 int can_sleep) 131 { 132 struct ipoib_mcast *mcast; 133 134 mcast = kzalloc(sizeof *mcast, can_sleep ? GFP_KERNEL : GFP_ATOMIC); 135 if (!mcast) 136 return NULL; 137 138 mcast->dev = dev; 139 mcast->created = jiffies; 140 mcast->backoff = 1; 141 142 INIT_LIST_HEAD(&mcast->list); 143 INIT_LIST_HEAD(&mcast->neigh_list); 144 skb_queue_head_init(&mcast->pkt_queue); 145 146 return mcast; 147 } 148 149 static struct ipoib_mcast *__ipoib_mcast_find(struct net_device *dev, union ib_gid *mgid) 150 { 151 struct ipoib_dev_priv *priv = netdev_priv(dev); 152 struct rb_node *n = priv->multicast_tree.rb_node; 153 154 while (n) { 155 struct ipoib_mcast *mcast; 156 int ret; 157 158 mcast = rb_entry(n, struct ipoib_mcast, rb_node); 159 160 ret = memcmp(mgid->raw, mcast->mcmember.mgid.raw, 161 sizeof (union ib_gid)); 162 if (ret < 0) 163 n = n->rb_left; 164 else if (ret > 0) 165 n = n->rb_right; 166 else 167 return mcast; 168 } 169 170 return NULL; 171 } 172 173 static int __ipoib_mcast_add(struct net_device *dev, struct ipoib_mcast *mcast) 174 { 175 struct ipoib_dev_priv *priv = netdev_priv(dev); 176 struct rb_node **n = &priv->multicast_tree.rb_node, *pn = NULL; 177 178 while (*n) { 179 struct ipoib_mcast *tmcast; 180 int ret; 181 182 pn = *n; 183 tmcast = rb_entry(pn, struct ipoib_mcast, rb_node); 184 185 ret = memcmp(mcast->mcmember.mgid.raw, tmcast->mcmember.mgid.raw, 186 sizeof (union ib_gid)); 187 if (ret < 0) 188 n = &pn->rb_left; 189 else if (ret > 0) 190 n = &pn->rb_right; 191 else 192 return -EEXIST; 193 } 194 195 rb_link_node(&mcast->rb_node, pn, n); 196 rb_insert_color(&mcast->rb_node, &priv->multicast_tree); 197 198 return 0; 199 } 200 201 static int ipoib_mcast_join_finish(struct ipoib_mcast *mcast, 202 struct ib_sa_mcmember_rec *mcmember) 203 { 204 struct net_device *dev = mcast->dev; 205 struct ipoib_dev_priv *priv = netdev_priv(dev); 206 int ret; 207 208 mcast->mcmember = *mcmember; 209 210 /* Set the cached Q_Key before we attach if it's the broadcast group */ 211 if (!memcmp(mcast->mcmember.mgid.raw, priv->dev->broadcast + 4, 212 sizeof (union ib_gid))) { 213 priv->qkey = be32_to_cpu(priv->broadcast->mcmember.qkey); 214 priv->tx_wr.wr.ud.remote_qkey = priv->qkey; 215 } 216 217 if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) { 218 if (test_and_set_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) { 219 ipoib_warn(priv, "multicast group " IPOIB_GID_FMT 220 " already attached\n", 221 IPOIB_GID_ARG(mcast->mcmember.mgid)); 222 223 return 0; 224 } 225 226 ret = ipoib_mcast_attach(dev, be16_to_cpu(mcast->mcmember.mlid), 227 &mcast->mcmember.mgid); 228 if (ret < 0) { 229 ipoib_warn(priv, "couldn't attach QP to multicast group " 230 IPOIB_GID_FMT "\n", 231 IPOIB_GID_ARG(mcast->mcmember.mgid)); 232 233 clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags); 234 return ret; 235 } 236 } 237 238 { 239 struct ib_ah_attr av = { 240 .dlid = be16_to_cpu(mcast->mcmember.mlid), 241 .port_num = priv->port, 242 .sl = mcast->mcmember.sl, 243 .ah_flags = IB_AH_GRH, 244 .grh = { 245 .flow_label = be32_to_cpu(mcast->mcmember.flow_label), 246 .hop_limit = mcast->mcmember.hop_limit, 247 .sgid_index = 0, 248 .traffic_class = mcast->mcmember.traffic_class 249 } 250 }; 251 int path_rate = ib_sa_rate_enum_to_int(mcast->mcmember.rate); 252 253 av.grh.dgid = mcast->mcmember.mgid; 254 255 if (path_rate > 0 && priv->local_rate > path_rate) 256 av.static_rate = (priv->local_rate - 1) / path_rate; 257 258 ipoib_dbg_mcast(priv, "static_rate %d for local port %dX, mcmember %dX\n", 259 av.static_rate, priv->local_rate, 260 ib_sa_rate_enum_to_int(mcast->mcmember.rate)); 261 262 mcast->ah = ipoib_create_ah(dev, priv->pd, &av); 263 if (!mcast->ah) { 264 ipoib_warn(priv, "ib_address_create failed\n"); 265 } else { 266 ipoib_dbg_mcast(priv, "MGID " IPOIB_GID_FMT 267 " AV %p, LID 0x%04x, SL %d\n", 268 IPOIB_GID_ARG(mcast->mcmember.mgid), 269 mcast->ah->ah, 270 be16_to_cpu(mcast->mcmember.mlid), 271 mcast->mcmember.sl); 272 } 273 } 274 275 /* actually send any queued packets */ 276 while (!skb_queue_empty(&mcast->pkt_queue)) { 277 struct sk_buff *skb = skb_dequeue(&mcast->pkt_queue); 278 279 skb->dev = dev; 280 281 if (!skb->dst || !skb->dst->neighbour) { 282 /* put pseudoheader back on for next time */ 283 skb_push(skb, sizeof (struct ipoib_pseudoheader)); 284 } 285 286 if (dev_queue_xmit(skb)) 287 ipoib_warn(priv, "dev_queue_xmit failed to requeue packet\n"); 288 } 289 290 return 0; 291 } 292 293 static void 294 ipoib_mcast_sendonly_join_complete(int status, 295 struct ib_sa_mcmember_rec *mcmember, 296 void *mcast_ptr) 297 { 298 struct ipoib_mcast *mcast = mcast_ptr; 299 struct net_device *dev = mcast->dev; 300 301 if (!status) 302 ipoib_mcast_join_finish(mcast, mcmember); 303 else { 304 if (mcast->logcount++ < 20) 305 ipoib_dbg_mcast(netdev_priv(dev), "multicast join failed for " 306 IPOIB_GID_FMT ", status %d\n", 307 IPOIB_GID_ARG(mcast->mcmember.mgid), status); 308 309 /* Flush out any queued packets */ 310 while (!skb_queue_empty(&mcast->pkt_queue)) 311 dev_kfree_skb_any(skb_dequeue(&mcast->pkt_queue)); 312 313 /* Clear the busy flag so we try again */ 314 clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags); 315 } 316 317 complete(&mcast->done); 318 } 319 320 static int ipoib_mcast_sendonly_join(struct ipoib_mcast *mcast) 321 { 322 struct net_device *dev = mcast->dev; 323 struct ipoib_dev_priv *priv = netdev_priv(dev); 324 struct ib_sa_mcmember_rec rec = { 325 #if 0 /* Some SMs don't support send-only yet */ 326 .join_state = 4 327 #else 328 .join_state = 1 329 #endif 330 }; 331 int ret = 0; 332 333 if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) { 334 ipoib_dbg_mcast(priv, "device shutting down, no multicast joins\n"); 335 return -ENODEV; 336 } 337 338 if (test_and_set_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags)) { 339 ipoib_dbg_mcast(priv, "multicast entry busy, skipping\n"); 340 return -EBUSY; 341 } 342 343 rec.mgid = mcast->mcmember.mgid; 344 rec.port_gid = priv->local_gid; 345 rec.pkey = cpu_to_be16(priv->pkey); 346 347 init_completion(&mcast->done); 348 349 ret = ib_sa_mcmember_rec_set(priv->ca, priv->port, &rec, 350 IB_SA_MCMEMBER_REC_MGID | 351 IB_SA_MCMEMBER_REC_PORT_GID | 352 IB_SA_MCMEMBER_REC_PKEY | 353 IB_SA_MCMEMBER_REC_JOIN_STATE, 354 1000, GFP_ATOMIC, 355 ipoib_mcast_sendonly_join_complete, 356 mcast, &mcast->query); 357 if (ret < 0) { 358 ipoib_warn(priv, "ib_sa_mcmember_rec_set failed (ret = %d)\n", 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)); 364 365 mcast->query_id = ret; 366 } 367 368 return ret; 369 } 370 371 static void ipoib_mcast_join_complete(int status, 372 struct ib_sa_mcmember_rec *mcmember, 373 void *mcast_ptr) 374 { 375 struct ipoib_mcast *mcast = mcast_ptr; 376 struct net_device *dev = mcast->dev; 377 struct ipoib_dev_priv *priv = netdev_priv(dev); 378 379 ipoib_dbg_mcast(priv, "join completion for " IPOIB_GID_FMT 380 " (status %d)\n", 381 IPOIB_GID_ARG(mcast->mcmember.mgid), status); 382 383 if (!status && !ipoib_mcast_join_finish(mcast, mcmember)) { 384 mcast->backoff = 1; 385 down(&mcast_mutex); 386 if (test_bit(IPOIB_MCAST_RUN, &priv->flags)) 387 queue_work(ipoib_workqueue, &priv->mcast_task); 388 up(&mcast_mutex); 389 complete(&mcast->done); 390 return; 391 } 392 393 if (status == -EINTR) { 394 complete(&mcast->done); 395 return; 396 } 397 398 if (status && mcast->logcount++ < 20) { 399 if (status == -ETIMEDOUT || status == -EINTR) { 400 ipoib_dbg_mcast(priv, "multicast join failed for " IPOIB_GID_FMT 401 ", status %d\n", 402 IPOIB_GID_ARG(mcast->mcmember.mgid), 403 status); 404 } else { 405 ipoib_warn(priv, "multicast join failed for " 406 IPOIB_GID_FMT ", status %d\n", 407 IPOIB_GID_ARG(mcast->mcmember.mgid), 408 status); 409 } 410 } 411 412 mcast->backoff *= 2; 413 if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS) 414 mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS; 415 416 mcast->query = NULL; 417 418 down(&mcast_mutex); 419 if (test_bit(IPOIB_MCAST_RUN, &priv->flags)) { 420 if (status == -ETIMEDOUT) 421 queue_work(ipoib_workqueue, &priv->mcast_task); 422 else 423 queue_delayed_work(ipoib_workqueue, &priv->mcast_task, 424 mcast->backoff * HZ); 425 } else 426 complete(&mcast->done); 427 up(&mcast_mutex); 428 429 return; 430 } 431 432 static void ipoib_mcast_join(struct net_device *dev, struct ipoib_mcast *mcast, 433 int create) 434 { 435 struct ipoib_dev_priv *priv = netdev_priv(dev); 436 struct ib_sa_mcmember_rec rec = { 437 .join_state = 1 438 }; 439 ib_sa_comp_mask comp_mask; 440 int ret = 0; 441 442 ipoib_dbg_mcast(priv, "joining MGID " IPOIB_GID_FMT "\n", 443 IPOIB_GID_ARG(mcast->mcmember.mgid)); 444 445 rec.mgid = mcast->mcmember.mgid; 446 rec.port_gid = priv->local_gid; 447 rec.pkey = cpu_to_be16(priv->pkey); 448 449 comp_mask = 450 IB_SA_MCMEMBER_REC_MGID | 451 IB_SA_MCMEMBER_REC_PORT_GID | 452 IB_SA_MCMEMBER_REC_PKEY | 453 IB_SA_MCMEMBER_REC_JOIN_STATE; 454 455 if (create) { 456 comp_mask |= 457 IB_SA_MCMEMBER_REC_QKEY | 458 IB_SA_MCMEMBER_REC_SL | 459 IB_SA_MCMEMBER_REC_FLOW_LABEL | 460 IB_SA_MCMEMBER_REC_TRAFFIC_CLASS; 461 462 rec.qkey = priv->broadcast->mcmember.qkey; 463 rec.sl = priv->broadcast->mcmember.sl; 464 rec.flow_label = priv->broadcast->mcmember.flow_label; 465 rec.traffic_class = priv->broadcast->mcmember.traffic_class; 466 } 467 468 init_completion(&mcast->done); 469 470 ret = ib_sa_mcmember_rec_set(priv->ca, priv->port, &rec, comp_mask, 471 mcast->backoff * 1000, GFP_ATOMIC, 472 ipoib_mcast_join_complete, 473 mcast, &mcast->query); 474 475 if (ret < 0) { 476 ipoib_warn(priv, "ib_sa_mcmember_rec_set failed, status %d\n", ret); 477 478 mcast->backoff *= 2; 479 if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS) 480 mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS; 481 482 down(&mcast_mutex); 483 if (test_bit(IPOIB_MCAST_RUN, &priv->flags)) 484 queue_delayed_work(ipoib_workqueue, 485 &priv->mcast_task, 486 mcast->backoff * HZ); 487 up(&mcast_mutex); 488 } else 489 mcast->query_id = ret; 490 } 491 492 void ipoib_mcast_join_task(void *dev_ptr) 493 { 494 struct net_device *dev = dev_ptr; 495 struct ipoib_dev_priv *priv = netdev_priv(dev); 496 497 if (!test_bit(IPOIB_MCAST_RUN, &priv->flags)) 498 return; 499 500 if (ib_query_gid(priv->ca, priv->port, 0, &priv->local_gid)) 501 ipoib_warn(priv, "ib_gid_entry_get() failed\n"); 502 else 503 memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid)); 504 505 { 506 struct ib_port_attr attr; 507 508 if (!ib_query_port(priv->ca, priv->port, &attr)) { 509 priv->local_lid = attr.lid; 510 priv->local_rate = attr.active_speed * 511 ib_width_enum_to_int(attr.active_width); 512 } else 513 ipoib_warn(priv, "ib_query_port failed\n"); 514 } 515 516 if (!priv->broadcast) { 517 priv->broadcast = ipoib_mcast_alloc(dev, 1); 518 if (!priv->broadcast) { 519 ipoib_warn(priv, "failed to allocate broadcast group\n"); 520 down(&mcast_mutex); 521 if (test_bit(IPOIB_MCAST_RUN, &priv->flags)) 522 queue_delayed_work(ipoib_workqueue, 523 &priv->mcast_task, HZ); 524 up(&mcast_mutex); 525 return; 526 } 527 528 memcpy(priv->broadcast->mcmember.mgid.raw, priv->dev->broadcast + 4, 529 sizeof (union ib_gid)); 530 531 spin_lock_irq(&priv->lock); 532 __ipoib_mcast_add(dev, priv->broadcast); 533 spin_unlock_irq(&priv->lock); 534 } 535 536 if (!test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) { 537 ipoib_mcast_join(dev, priv->broadcast, 0); 538 return; 539 } 540 541 while (1) { 542 struct ipoib_mcast *mcast = NULL; 543 544 spin_lock_irq(&priv->lock); 545 list_for_each_entry(mcast, &priv->multicast_list, list) { 546 if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags) 547 && !test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags) 548 && !test_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) { 549 /* Found the next unjoined group */ 550 break; 551 } 552 } 553 spin_unlock_irq(&priv->lock); 554 555 if (&mcast->list == &priv->multicast_list) { 556 /* All done */ 557 break; 558 } 559 560 ipoib_mcast_join(dev, mcast, 1); 561 return; 562 } 563 564 priv->mcast_mtu = ib_mtu_enum_to_int(priv->broadcast->mcmember.mtu) - 565 IPOIB_ENCAP_LEN; 566 dev->mtu = min(priv->mcast_mtu, priv->admin_mtu); 567 568 ipoib_dbg_mcast(priv, "successfully joined all multicast groups\n"); 569 570 clear_bit(IPOIB_MCAST_RUN, &priv->flags); 571 netif_carrier_on(dev); 572 } 573 574 int ipoib_mcast_start_thread(struct net_device *dev) 575 { 576 struct ipoib_dev_priv *priv = netdev_priv(dev); 577 578 ipoib_dbg_mcast(priv, "starting multicast thread\n"); 579 580 down(&mcast_mutex); 581 if (!test_and_set_bit(IPOIB_MCAST_RUN, &priv->flags)) 582 queue_work(ipoib_workqueue, &priv->mcast_task); 583 up(&mcast_mutex); 584 585 return 0; 586 } 587 588 int ipoib_mcast_stop_thread(struct net_device *dev, int flush) 589 { 590 struct ipoib_dev_priv *priv = netdev_priv(dev); 591 struct ipoib_mcast *mcast; 592 593 ipoib_dbg_mcast(priv, "stopping multicast thread\n"); 594 595 down(&mcast_mutex); 596 clear_bit(IPOIB_MCAST_RUN, &priv->flags); 597 cancel_delayed_work(&priv->mcast_task); 598 up(&mcast_mutex); 599 600 if (flush) 601 flush_workqueue(ipoib_workqueue); 602 603 if (priv->broadcast && priv->broadcast->query) { 604 ib_sa_cancel_query(priv->broadcast->query_id, priv->broadcast->query); 605 priv->broadcast->query = NULL; 606 ipoib_dbg_mcast(priv, "waiting for bcast\n"); 607 wait_for_completion(&priv->broadcast->done); 608 } 609 610 list_for_each_entry(mcast, &priv->multicast_list, list) { 611 if (mcast->query) { 612 ib_sa_cancel_query(mcast->query_id, mcast->query); 613 mcast->query = NULL; 614 ipoib_dbg_mcast(priv, "waiting for MGID " IPOIB_GID_FMT "\n", 615 IPOIB_GID_ARG(mcast->mcmember.mgid)); 616 wait_for_completion(&mcast->done); 617 } 618 } 619 620 return 0; 621 } 622 623 static int ipoib_mcast_leave(struct net_device *dev, struct ipoib_mcast *mcast) 624 { 625 struct ipoib_dev_priv *priv = netdev_priv(dev); 626 struct ib_sa_mcmember_rec rec = { 627 .join_state = 1 628 }; 629 int ret = 0; 630 631 if (!test_and_clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) 632 return 0; 633 634 ipoib_dbg_mcast(priv, "leaving MGID " IPOIB_GID_FMT "\n", 635 IPOIB_GID_ARG(mcast->mcmember.mgid)); 636 637 rec.mgid = mcast->mcmember.mgid; 638 rec.port_gid = priv->local_gid; 639 rec.pkey = cpu_to_be16(priv->pkey); 640 641 /* Remove ourselves from the multicast group */ 642 ret = ipoib_mcast_detach(dev, be16_to_cpu(mcast->mcmember.mlid), 643 &mcast->mcmember.mgid); 644 if (ret) 645 ipoib_warn(priv, "ipoib_mcast_detach failed (result = %d)\n", ret); 646 647 /* 648 * Just make one shot at leaving and don't wait for a reply; 649 * if we fail, too bad. 650 */ 651 ret = ib_sa_mcmember_rec_delete(priv->ca, priv->port, &rec, 652 IB_SA_MCMEMBER_REC_MGID | 653 IB_SA_MCMEMBER_REC_PORT_GID | 654 IB_SA_MCMEMBER_REC_PKEY | 655 IB_SA_MCMEMBER_REC_JOIN_STATE, 656 0, GFP_ATOMIC, NULL, 657 mcast, &mcast->query); 658 if (ret < 0) 659 ipoib_warn(priv, "ib_sa_mcmember_rec_delete failed " 660 "for leave (result = %d)\n", ret); 661 662 return 0; 663 } 664 665 void ipoib_mcast_send(struct net_device *dev, union ib_gid *mgid, 666 struct sk_buff *skb) 667 { 668 struct ipoib_dev_priv *priv = netdev_priv(dev); 669 struct ipoib_mcast *mcast; 670 671 /* 672 * We can only be called from ipoib_start_xmit, so we're 673 * inside tx_lock -- no need to save/restore flags. 674 */ 675 spin_lock(&priv->lock); 676 677 mcast = __ipoib_mcast_find(dev, mgid); 678 if (!mcast) { 679 /* Let's create a new send only group now */ 680 ipoib_dbg_mcast(priv, "setting up send only multicast group for " 681 IPOIB_GID_FMT "\n", IPOIB_GID_ARG(*mgid)); 682 683 mcast = ipoib_mcast_alloc(dev, 0); 684 if (!mcast) { 685 ipoib_warn(priv, "unable to allocate memory for " 686 "multicast structure\n"); 687 dev_kfree_skb_any(skb); 688 goto out; 689 } 690 691 set_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags); 692 mcast->mcmember.mgid = *mgid; 693 __ipoib_mcast_add(dev, mcast); 694 list_add_tail(&mcast->list, &priv->multicast_list); 695 } 696 697 if (!mcast->ah) { 698 if (skb_queue_len(&mcast->pkt_queue) < IPOIB_MAX_MCAST_QUEUE) 699 skb_queue_tail(&mcast->pkt_queue, skb); 700 else 701 dev_kfree_skb_any(skb); 702 703 if (mcast->query) 704 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 716 out: 717 if (mcast && mcast->ah) { 718 if (skb->dst && 719 skb->dst->neighbour && 720 !*to_ipoib_neigh(skb->dst->neighbour)) { 721 struct ipoib_neigh *neigh = kmalloc(sizeof *neigh, GFP_ATOMIC); 722 723 if (neigh) { 724 kref_get(&mcast->ah->ref); 725 neigh->ah = mcast->ah; 726 neigh->neighbour = skb->dst->neighbour; 727 *to_ipoib_neigh(skb->dst->neighbour) = neigh; 728 list_add_tail(&neigh->list, &mcast->neigh_list); 729 } 730 } 731 732 ipoib_send(dev, skb, mcast->ah, IB_MULTICAST_QPN); 733 } 734 735 spin_unlock(&priv->lock); 736 } 737 738 void ipoib_mcast_dev_flush(struct net_device *dev) 739 { 740 struct ipoib_dev_priv *priv = netdev_priv(dev); 741 LIST_HEAD(remove_list); 742 struct ipoib_mcast *mcast, *tmcast, *nmcast; 743 unsigned long flags; 744 745 ipoib_dbg_mcast(priv, "flushing multicast list\n"); 746 747 spin_lock_irqsave(&priv->lock, flags); 748 list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) { 749 nmcast = ipoib_mcast_alloc(dev, 0); 750 if (nmcast) { 751 nmcast->flags = 752 mcast->flags & (1 << IPOIB_MCAST_FLAG_SENDONLY); 753 754 nmcast->mcmember.mgid = mcast->mcmember.mgid; 755 756 /* Add the new group in before the to-be-destroyed group */ 757 list_add_tail(&nmcast->list, &mcast->list); 758 list_del_init(&mcast->list); 759 760 rb_replace_node(&mcast->rb_node, &nmcast->rb_node, 761 &priv->multicast_tree); 762 763 list_add_tail(&mcast->list, &remove_list); 764 } else { 765 ipoib_warn(priv, "could not reallocate multicast group " 766 IPOIB_GID_FMT "\n", 767 IPOIB_GID_ARG(mcast->mcmember.mgid)); 768 } 769 } 770 771 if (priv->broadcast) { 772 nmcast = ipoib_mcast_alloc(dev, 0); 773 if (nmcast) { 774 nmcast->mcmember.mgid = priv->broadcast->mcmember.mgid; 775 776 rb_replace_node(&priv->broadcast->rb_node, 777 &nmcast->rb_node, 778 &priv->multicast_tree); 779 780 list_add_tail(&priv->broadcast->list, &remove_list); 781 } 782 783 priv->broadcast = nmcast; 784 } 785 786 spin_unlock_irqrestore(&priv->lock, flags); 787 788 list_for_each_entry_safe(mcast, tmcast, &remove_list, list) { 789 ipoib_mcast_leave(dev, mcast); 790 ipoib_mcast_free(mcast); 791 } 792 } 793 794 void ipoib_mcast_dev_down(struct net_device *dev) 795 { 796 struct ipoib_dev_priv *priv = netdev_priv(dev); 797 unsigned long flags; 798 799 /* Delete broadcast since it will be recreated */ 800 if (priv->broadcast) { 801 ipoib_dbg_mcast(priv, "deleting broadcast group\n"); 802 803 spin_lock_irqsave(&priv->lock, flags); 804 rb_erase(&priv->broadcast->rb_node, &priv->multicast_tree); 805 spin_unlock_irqrestore(&priv->lock, flags); 806 ipoib_mcast_leave(dev, priv->broadcast); 807 ipoib_mcast_free(priv->broadcast); 808 priv->broadcast = NULL; 809 } 810 } 811 812 void ipoib_mcast_restart_task(void *dev_ptr) 813 { 814 struct net_device *dev = dev_ptr; 815 struct ipoib_dev_priv *priv = netdev_priv(dev); 816 struct dev_mc_list *mclist; 817 struct ipoib_mcast *mcast, *tmcast; 818 LIST_HEAD(remove_list); 819 unsigned long flags; 820 821 ipoib_dbg_mcast(priv, "restarting multicast task\n"); 822 823 ipoib_mcast_stop_thread(dev, 0); 824 825 spin_lock_irqsave(&priv->lock, flags); 826 827 /* 828 * Unfortunately, the networking core only gives us a list of all of 829 * the multicast hardware addresses. We need to figure out which ones 830 * are new and which ones have been removed 831 */ 832 833 /* Clear out the found flag */ 834 list_for_each_entry(mcast, &priv->multicast_list, list) 835 clear_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags); 836 837 /* Mark all of the entries that are found or don't exist */ 838 for (mclist = dev->mc_list; mclist; mclist = mclist->next) { 839 union ib_gid mgid; 840 841 memcpy(mgid.raw, mclist->dmi_addr + 4, sizeof mgid); 842 843 /* Add in the P_Key */ 844 mgid.raw[4] = (priv->pkey >> 8) & 0xff; 845 mgid.raw[5] = priv->pkey & 0xff; 846 847 mcast = __ipoib_mcast_find(dev, &mgid); 848 if (!mcast || test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) { 849 struct ipoib_mcast *nmcast; 850 851 /* Not found or send-only group, let's add a new entry */ 852 ipoib_dbg_mcast(priv, "adding multicast entry for mgid " 853 IPOIB_GID_FMT "\n", IPOIB_GID_ARG(mgid)); 854 855 nmcast = ipoib_mcast_alloc(dev, 0); 856 if (!nmcast) { 857 ipoib_warn(priv, "unable to allocate memory for multicast structure\n"); 858 continue; 859 } 860 861 set_bit(IPOIB_MCAST_FLAG_FOUND, &nmcast->flags); 862 863 nmcast->mcmember.mgid = mgid; 864 865 if (mcast) { 866 /* Destroy the send only entry */ 867 list_del(&mcast->list); 868 list_add_tail(&mcast->list, &remove_list); 869 870 rb_replace_node(&mcast->rb_node, 871 &nmcast->rb_node, 872 &priv->multicast_tree); 873 } else 874 __ipoib_mcast_add(dev, nmcast); 875 876 list_add_tail(&nmcast->list, &priv->multicast_list); 877 } 878 879 if (mcast) 880 set_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags); 881 } 882 883 /* Remove all of the entries don't exist anymore */ 884 list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) { 885 if (!test_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags) && 886 !test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) { 887 ipoib_dbg_mcast(priv, "deleting multicast group " IPOIB_GID_FMT "\n", 888 IPOIB_GID_ARG(mcast->mcmember.mgid)); 889 890 rb_erase(&mcast->rb_node, &priv->multicast_tree); 891 892 /* Move to the remove list */ 893 list_del(&mcast->list); 894 list_add_tail(&mcast->list, &remove_list); 895 } 896 } 897 spin_unlock_irqrestore(&priv->lock, flags); 898 899 /* We have to cancel outside of the spinlock */ 900 list_for_each_entry_safe(mcast, tmcast, &remove_list, list) { 901 ipoib_mcast_leave(mcast->dev, mcast); 902 ipoib_mcast_free(mcast); 903 } 904 905 if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) 906 ipoib_mcast_start_thread(dev); 907 } 908 909 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG 910 911 struct ipoib_mcast_iter *ipoib_mcast_iter_init(struct net_device *dev) 912 { 913 struct ipoib_mcast_iter *iter; 914 915 iter = kmalloc(sizeof *iter, GFP_KERNEL); 916 if (!iter) 917 return NULL; 918 919 iter->dev = dev; 920 memset(iter->mgid.raw, 0, 16); 921 922 if (ipoib_mcast_iter_next(iter)) { 923 kfree(iter); 924 return NULL; 925 } 926 927 return iter; 928 } 929 930 int ipoib_mcast_iter_next(struct ipoib_mcast_iter *iter) 931 { 932 struct ipoib_dev_priv *priv = netdev_priv(iter->dev); 933 struct rb_node *n; 934 struct ipoib_mcast *mcast; 935 int ret = 1; 936 937 spin_lock_irq(&priv->lock); 938 939 n = rb_first(&priv->multicast_tree); 940 941 while (n) { 942 mcast = rb_entry(n, struct ipoib_mcast, rb_node); 943 944 if (memcmp(iter->mgid.raw, mcast->mcmember.mgid.raw, 945 sizeof (union ib_gid)) < 0) { 946 iter->mgid = mcast->mcmember.mgid; 947 iter->created = mcast->created; 948 iter->queuelen = skb_queue_len(&mcast->pkt_queue); 949 iter->complete = !!mcast->ah; 950 iter->send_only = !!(mcast->flags & (1 << IPOIB_MCAST_FLAG_SENDONLY)); 951 952 ret = 0; 953 954 break; 955 } 956 957 n = rb_next(n); 958 } 959 960 spin_unlock_irq(&priv->lock); 961 962 return ret; 963 } 964 965 void ipoib_mcast_iter_read(struct ipoib_mcast_iter *iter, 966 union ib_gid *mgid, 967 unsigned long *created, 968 unsigned int *queuelen, 969 unsigned int *complete, 970 unsigned int *send_only) 971 { 972 *mgid = iter->mgid; 973 *created = iter->created; 974 *queuelen = iter->queuelen; 975 *complete = iter->complete; 976 *send_only = iter->send_only; 977 } 978 979 #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */ 980