2 * Copyright (c) 2015, Mellanox Technologies inc. All rights reserved.
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 #include "core_priv.h"
36 #include <linux/in6.h>
38 /* For in6_dev_get/in6_dev_put */
39 #include <net/addrconf.h>
40 #include <net/bonding.h>
42 #include <rdma/ib_cache.h>
43 #include <rdma/ib_addr.h>
50 struct update_gid_event_work {
51 struct work_struct work;
53 struct ib_gid_attr gid_attr;
54 enum gid_op_type gid_op;
57 #define ROCE_NETDEV_CALLBACK_SZ 3
58 struct netdev_event_work_cmd {
59 roce_netdev_callback cb;
60 roce_netdev_filter filter;
61 struct net_device *ndev;
62 struct net_device *filter_ndev;
65 struct netdev_event_work {
66 struct work_struct work;
67 struct netdev_event_work_cmd cmds[ROCE_NETDEV_CALLBACK_SZ];
70 static void update_gid(enum gid_op_type gid_op, struct ib_device *ib_dev,
71 u8 port, union ib_gid *gid,
72 struct ib_gid_attr *gid_attr)
76 ib_cache_gid_add(ib_dev, port, gid, gid_attr);
79 ib_cache_gid_del(ib_dev, port, gid, gid_attr);
84 enum bonding_slave_state {
85 BONDING_SLAVE_STATE_ACTIVE = 1UL << 0,
86 BONDING_SLAVE_STATE_INACTIVE = 1UL << 1,
87 /* No primary slave or the device isn't a slave in bonding */
88 BONDING_SLAVE_STATE_NA = 1UL << 2,
91 static enum bonding_slave_state is_eth_active_slave_of_bonding_rcu(struct net_device *dev,
92 struct net_device *upper)
94 if (upper && netif_is_bond_master(upper)) {
95 struct net_device *pdev =
96 bond_option_active_slave_get_rcu(netdev_priv(upper));
99 return dev == pdev ? BONDING_SLAVE_STATE_ACTIVE :
100 BONDING_SLAVE_STATE_INACTIVE;
103 return BONDING_SLAVE_STATE_NA;
106 static bool is_upper_dev_rcu(struct net_device *dev, struct net_device *upper)
108 struct net_device *_upper = NULL;
109 struct list_head *iter;
111 netdev_for_each_all_upper_dev_rcu(dev, _upper, iter)
115 return _upper == upper;
118 #define REQUIRED_BOND_STATES (BONDING_SLAVE_STATE_ACTIVE | \
119 BONDING_SLAVE_STATE_NA)
120 static int is_eth_port_of_netdev(struct ib_device *ib_dev, u8 port,
121 struct net_device *rdma_ndev, void *cookie)
123 struct net_device *event_ndev = (struct net_device *)cookie;
124 struct net_device *real_dev;
131 real_dev = rdma_vlan_dev_real_dev(event_ndev);
133 real_dev = event_ndev;
135 res = ((is_upper_dev_rcu(rdma_ndev, event_ndev) &&
136 (is_eth_active_slave_of_bonding_rcu(rdma_ndev, real_dev) &
137 REQUIRED_BOND_STATES)) ||
138 real_dev == rdma_ndev);
144 static int is_eth_port_inactive_slave(struct ib_device *ib_dev, u8 port,
145 struct net_device *rdma_ndev, void *cookie)
147 struct net_device *master_dev;
154 master_dev = netdev_master_upper_dev_get_rcu(rdma_ndev);
155 res = is_eth_active_slave_of_bonding_rcu(rdma_ndev, master_dev) ==
156 BONDING_SLAVE_STATE_INACTIVE;
162 static int pass_all_filter(struct ib_device *ib_dev, u8 port,
163 struct net_device *rdma_ndev, void *cookie)
168 static int upper_device_filter(struct ib_device *ib_dev, u8 port,
169 struct net_device *rdma_ndev, void *cookie)
171 struct net_device *event_ndev = (struct net_device *)cookie;
177 if (rdma_ndev == event_ndev)
181 res = is_upper_dev_rcu(rdma_ndev, event_ndev);
187 static void update_gid_ip(enum gid_op_type gid_op,
188 struct ib_device *ib_dev,
189 u8 port, struct net_device *ndev,
190 struct sockaddr *addr)
193 struct ib_gid_attr gid_attr;
195 rdma_ip2gid(addr, &gid);
196 memset(&gid_attr, 0, sizeof(gid_attr));
197 gid_attr.ndev = ndev;
199 update_gid(gid_op, ib_dev, port, &gid, &gid_attr);
202 static void enum_netdev_default_gids(struct ib_device *ib_dev,
203 u8 port, struct net_device *event_ndev,
204 struct net_device *rdma_ndev)
208 ((rdma_ndev != event_ndev &&
209 !is_upper_dev_rcu(rdma_ndev, event_ndev)) ||
210 is_eth_active_slave_of_bonding_rcu(rdma_ndev,
211 netdev_master_upper_dev_get_rcu(rdma_ndev)) ==
212 BONDING_SLAVE_STATE_INACTIVE)) {
218 ib_cache_gid_set_default_gid(ib_dev, port, rdma_ndev,
219 IB_CACHE_GID_DEFAULT_MODE_SET);
222 static void bond_delete_netdev_default_gids(struct ib_device *ib_dev,
224 struct net_device *event_ndev,
225 struct net_device *rdma_ndev)
227 struct net_device *real_dev = rdma_vlan_dev_real_dev(event_ndev);
233 real_dev = event_ndev;
237 if (is_upper_dev_rcu(rdma_ndev, event_ndev) &&
238 is_eth_active_slave_of_bonding_rcu(rdma_ndev, real_dev) ==
239 BONDING_SLAVE_STATE_INACTIVE) {
242 ib_cache_gid_set_default_gid(ib_dev, port, rdma_ndev,
243 IB_CACHE_GID_DEFAULT_MODE_DELETE);
249 static void enum_netdev_ipv4_ips(struct ib_device *ib_dev,
250 u8 port, struct net_device *ndev)
252 struct in_device *in_dev;
254 struct list_head list;
255 struct sockaddr_in ip;
257 struct sin_list *sin_iter;
258 struct sin_list *sin_temp;
261 if (ndev->reg_state >= NETREG_UNREGISTERING)
265 in_dev = __in_dev_get_rcu(ndev);
272 struct sin_list *entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
275 pr_warn("roce_gid_mgmt: couldn't allocate entry for IPv4 update\n");
278 entry->ip.sin_family = AF_INET;
279 entry->ip.sin_addr.s_addr = ifa->ifa_address;
280 list_add_tail(&entry->list, &sin_list);
285 list_for_each_entry_safe(sin_iter, sin_temp, &sin_list, list) {
286 update_gid_ip(GID_ADD, ib_dev, port, ndev,
287 (struct sockaddr *)&sin_iter->ip);
288 list_del(&sin_iter->list);
293 static void enum_netdev_ipv6_ips(struct ib_device *ib_dev,
294 u8 port, struct net_device *ndev)
296 struct inet6_ifaddr *ifp;
297 struct inet6_dev *in6_dev;
299 struct list_head list;
300 struct sockaddr_in6 sin6;
302 struct sin6_list *sin6_iter;
303 struct sin6_list *sin6_temp;
304 struct ib_gid_attr gid_attr = {.ndev = ndev};
305 LIST_HEAD(sin6_list);
307 if (ndev->reg_state >= NETREG_UNREGISTERING)
310 in6_dev = in6_dev_get(ndev);
314 read_lock_bh(&in6_dev->lock);
315 list_for_each_entry(ifp, &in6_dev->addr_list, if_list) {
316 struct sin6_list *entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
319 pr_warn("roce_gid_mgmt: couldn't allocate entry for IPv6 update\n");
323 entry->sin6.sin6_family = AF_INET6;
324 entry->sin6.sin6_addr = ifp->addr;
325 list_add_tail(&entry->list, &sin6_list);
327 read_unlock_bh(&in6_dev->lock);
329 in6_dev_put(in6_dev);
331 list_for_each_entry_safe(sin6_iter, sin6_temp, &sin6_list, list) {
334 rdma_ip2gid((struct sockaddr *)&sin6_iter->sin6, &gid);
335 update_gid(GID_ADD, ib_dev, port, &gid, &gid_attr);
336 list_del(&sin6_iter->list);
341 static void _add_netdev_ips(struct ib_device *ib_dev, u8 port,
342 struct net_device *ndev)
344 enum_netdev_ipv4_ips(ib_dev, port, ndev);
345 if (IS_ENABLED(CONFIG_IPV6))
346 enum_netdev_ipv6_ips(ib_dev, port, ndev);
349 static void add_netdev_ips(struct ib_device *ib_dev, u8 port,
350 struct net_device *rdma_ndev, void *cookie)
352 struct net_device *event_ndev = (struct net_device *)cookie;
354 enum_netdev_default_gids(ib_dev, port, event_ndev, rdma_ndev);
355 _add_netdev_ips(ib_dev, port, event_ndev);
358 static void del_netdev_ips(struct ib_device *ib_dev, u8 port,
359 struct net_device *rdma_ndev, void *cookie)
361 struct net_device *event_ndev = (struct net_device *)cookie;
363 ib_cache_gid_del_all_netdev_gids(ib_dev, port, event_ndev);
366 static void enum_all_gids_of_dev_cb(struct ib_device *ib_dev,
368 struct net_device *rdma_ndev,
372 struct net_device *ndev;
374 /* Lock the rtnl to make sure the netdevs does not move under
379 for_each_netdev(net, ndev)
380 if (is_eth_port_of_netdev(ib_dev, port, rdma_ndev, ndev))
381 add_netdev_ips(ib_dev, port, rdma_ndev, ndev);
385 /* This function will rescan all of the network devices in the system
386 * and add their gids, as needed, to the relevant RoCE devices. */
387 int roce_rescan_device(struct ib_device *ib_dev)
389 ib_enum_roce_netdev(ib_dev, pass_all_filter, NULL,
390 enum_all_gids_of_dev_cb, NULL);
395 static void callback_for_addr_gid_device_scan(struct ib_device *device,
397 struct net_device *rdma_ndev,
400 struct update_gid_event_work *parsed = cookie;
402 return update_gid(parsed->gid_op, device,
407 static void handle_netdev_upper(struct ib_device *ib_dev, u8 port,
409 void (*handle_netdev)(struct ib_device *ib_dev,
411 struct net_device *ndev))
413 struct net_device *ndev = (struct net_device *)cookie;
415 struct list_head list;
416 struct net_device *upper;
418 struct net_device *upper;
419 struct list_head *iter;
420 struct upper_list *upper_iter;
421 struct upper_list *upper_temp;
422 LIST_HEAD(upper_list);
425 netdev_for_each_all_upper_dev_rcu(ndev, upper, iter) {
426 struct upper_list *entry = kmalloc(sizeof(*entry),
430 pr_info("roce_gid_mgmt: couldn't allocate entry to delete ndev\n");
434 list_add_tail(&entry->list, &upper_list);
436 entry->upper = upper;
440 handle_netdev(ib_dev, port, ndev);
441 list_for_each_entry_safe(upper_iter, upper_temp, &upper_list,
443 handle_netdev(ib_dev, port, upper_iter->upper);
444 dev_put(upper_iter->upper);
445 list_del(&upper_iter->list);
450 static void _roce_del_all_netdev_gids(struct ib_device *ib_dev, u8 port,
451 struct net_device *event_ndev)
453 ib_cache_gid_del_all_netdev_gids(ib_dev, port, event_ndev);
456 static void del_netdev_upper_ips(struct ib_device *ib_dev, u8 port,
457 struct net_device *rdma_ndev, void *cookie)
459 handle_netdev_upper(ib_dev, port, cookie, _roce_del_all_netdev_gids);
462 static void add_netdev_upper_ips(struct ib_device *ib_dev, u8 port,
463 struct net_device *rdma_ndev, void *cookie)
465 handle_netdev_upper(ib_dev, port, cookie, _add_netdev_ips);
468 static void del_netdev_default_ips_join(struct ib_device *ib_dev, u8 port,
469 struct net_device *rdma_ndev,
472 struct net_device *master_ndev;
475 master_ndev = netdev_master_upper_dev_get_rcu(rdma_ndev);
477 dev_hold(master_ndev);
481 bond_delete_netdev_default_gids(ib_dev, port, master_ndev,
483 dev_put(master_ndev);
487 static void del_netdev_default_ips(struct ib_device *ib_dev, u8 port,
488 struct net_device *rdma_ndev, void *cookie)
490 struct net_device *event_ndev = (struct net_device *)cookie;
492 bond_delete_netdev_default_gids(ib_dev, port, event_ndev, rdma_ndev);
495 /* The following functions operate on all IB devices. netdevice_event and
496 * addr_event execute ib_enum_all_roce_netdevs through a work.
497 * ib_enum_all_roce_netdevs iterates through all IB devices.
500 static void netdevice_event_work_handler(struct work_struct *_work)
502 struct netdev_event_work *work =
503 container_of(_work, struct netdev_event_work, work);
506 for (i = 0; i < ARRAY_SIZE(work->cmds) && work->cmds[i].cb; i++) {
507 ib_enum_all_roce_netdevs(work->cmds[i].filter,
508 work->cmds[i].filter_ndev,
511 dev_put(work->cmds[i].ndev);
512 dev_put(work->cmds[i].filter_ndev);
518 static int netdevice_queue_work(struct netdev_event_work_cmd *cmds,
519 struct net_device *ndev)
522 struct netdev_event_work *ndev_work =
523 kmalloc(sizeof(*ndev_work), GFP_KERNEL);
526 pr_warn("roce_gid_mgmt: can't allocate work for netdevice_event\n");
530 memcpy(ndev_work->cmds, cmds, sizeof(ndev_work->cmds));
531 for (i = 0; i < ARRAY_SIZE(ndev_work->cmds) && ndev_work->cmds[i].cb; i++) {
532 if (!ndev_work->cmds[i].ndev)
533 ndev_work->cmds[i].ndev = ndev;
534 if (!ndev_work->cmds[i].filter_ndev)
535 ndev_work->cmds[i].filter_ndev = ndev;
536 dev_hold(ndev_work->cmds[i].ndev);
537 dev_hold(ndev_work->cmds[i].filter_ndev);
539 INIT_WORK(&ndev_work->work, netdevice_event_work_handler);
541 queue_work(ib_wq, &ndev_work->work);
546 static const struct netdev_event_work_cmd add_cmd = {
547 .cb = add_netdev_ips, .filter = is_eth_port_of_netdev};
548 static const struct netdev_event_work_cmd add_cmd_upper_ips = {
549 .cb = add_netdev_upper_ips, .filter = is_eth_port_of_netdev};
551 static void netdevice_event_changeupper(struct netdev_notifier_changeupper_info *changeupper_info,
552 struct netdev_event_work_cmd *cmds)
554 static const struct netdev_event_work_cmd upper_ips_del_cmd = {
555 .cb = del_netdev_upper_ips, .filter = upper_device_filter};
556 static const struct netdev_event_work_cmd bonding_default_del_cmd = {
557 .cb = del_netdev_default_ips, .filter = is_eth_port_inactive_slave};
559 if (changeupper_info->linking == false) {
560 cmds[0] = upper_ips_del_cmd;
561 cmds[0].ndev = changeupper_info->upper_dev;
564 cmds[0] = bonding_default_del_cmd;
565 cmds[0].ndev = changeupper_info->upper_dev;
566 cmds[1] = add_cmd_upper_ips;
567 cmds[1].ndev = changeupper_info->upper_dev;
568 cmds[1].filter_ndev = changeupper_info->upper_dev;
572 static int netdevice_event(struct notifier_block *this, unsigned long event,
575 static const struct netdev_event_work_cmd del_cmd = {
576 .cb = del_netdev_ips, .filter = pass_all_filter};
577 static const struct netdev_event_work_cmd bonding_default_del_cmd_join = {
578 .cb = del_netdev_default_ips_join, .filter = is_eth_port_inactive_slave};
579 static const struct netdev_event_work_cmd default_del_cmd = {
580 .cb = del_netdev_default_ips, .filter = pass_all_filter};
581 static const struct netdev_event_work_cmd bonding_event_ips_del_cmd = {
582 .cb = del_netdev_upper_ips, .filter = upper_device_filter};
583 struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
584 struct netdev_event_work_cmd cmds[ROCE_NETDEV_CALLBACK_SZ] = { {NULL} };
586 if (ndev->type != ARPHRD_ETHER)
590 case NETDEV_REGISTER:
592 cmds[0] = bonding_default_del_cmd_join;
596 case NETDEV_UNREGISTER:
597 if (ndev->reg_state < NETREG_UNREGISTERED)
603 case NETDEV_CHANGEADDR:
604 cmds[0] = default_del_cmd;
608 case NETDEV_CHANGEUPPER:
609 netdevice_event_changeupper(
610 container_of(ptr, struct netdev_notifier_changeupper_info, info),
614 case NETDEV_BONDING_FAILOVER:
615 cmds[0] = bonding_event_ips_del_cmd;
616 cmds[1] = bonding_default_del_cmd_join;
617 cmds[2] = add_cmd_upper_ips;
624 return netdevice_queue_work(cmds, ndev);
627 static void update_gid_event_work_handler(struct work_struct *_work)
629 struct update_gid_event_work *work =
630 container_of(_work, struct update_gid_event_work, work);
632 ib_enum_all_roce_netdevs(is_eth_port_of_netdev, work->gid_attr.ndev,
633 callback_for_addr_gid_device_scan, work);
635 dev_put(work->gid_attr.ndev);
639 static int addr_event(struct notifier_block *this, unsigned long event,
640 struct sockaddr *sa, struct net_device *ndev)
642 struct update_gid_event_work *work;
643 enum gid_op_type gid_op;
645 if (ndev->type != ARPHRD_ETHER)
661 work = kmalloc(sizeof(*work), GFP_ATOMIC);
663 pr_warn("roce_gid_mgmt: Couldn't allocate work for addr_event\n");
667 INIT_WORK(&work->work, update_gid_event_work_handler);
669 rdma_ip2gid(sa, &work->gid);
670 work->gid_op = gid_op;
672 memset(&work->gid_attr, 0, sizeof(work->gid_attr));
674 work->gid_attr.ndev = ndev;
676 queue_work(ib_wq, &work->work);
681 static int inetaddr_event(struct notifier_block *this, unsigned long event,
684 struct sockaddr_in in;
685 struct net_device *ndev;
686 struct in_ifaddr *ifa = ptr;
688 in.sin_family = AF_INET;
689 in.sin_addr.s_addr = ifa->ifa_address;
690 ndev = ifa->ifa_dev->dev;
692 return addr_event(this, event, (struct sockaddr *)&in, ndev);
695 static int inet6addr_event(struct notifier_block *this, unsigned long event,
698 struct sockaddr_in6 in6;
699 struct net_device *ndev;
700 struct inet6_ifaddr *ifa6 = ptr;
702 in6.sin6_family = AF_INET6;
703 in6.sin6_addr = ifa6->addr;
704 ndev = ifa6->idev->dev;
706 return addr_event(this, event, (struct sockaddr *)&in6, ndev);
709 static struct notifier_block nb_netdevice = {
710 .notifier_call = netdevice_event
713 static struct notifier_block nb_inetaddr = {
714 .notifier_call = inetaddr_event
717 static struct notifier_block nb_inet6addr = {
718 .notifier_call = inet6addr_event
721 int __init roce_gid_mgmt_init(void)
723 register_inetaddr_notifier(&nb_inetaddr);
724 if (IS_ENABLED(CONFIG_IPV6))
725 register_inet6addr_notifier(&nb_inet6addr);
726 /* We relay on the netdevice notifier to enumerate all
727 * existing devices in the system. Register to this notifier
728 * last to make sure we will not miss any IP add/del
731 register_netdevice_notifier(&nb_netdevice);
736 void __exit roce_gid_mgmt_cleanup(void)
738 if (IS_ENABLED(CONFIG_IPV6))
739 unregister_inet6addr_notifier(&nb_inet6addr);
740 unregister_inetaddr_notifier(&nb_inetaddr);
741 unregister_netdevice_notifier(&nb_netdevice);
742 /* Ensure all gid deletion tasks complete before we go down,
743 * to avoid any reference to free'd memory. By the time
744 * ib-core is removed, all physical devices have been removed,
745 * so no issue with remaining hardware contexts.