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 if (ndev->reg_state >= NETREG_UNREGISTERING)
257 in_dev = in_dev_get(ndev);
262 struct sockaddr_in ip;
264 ip.sin_family = AF_INET;
265 ip.sin_addr.s_addr = ifa->ifa_address;
266 update_gid_ip(GID_ADD, ib_dev, port, ndev,
267 (struct sockaddr *)&ip);
274 static void enum_netdev_ipv6_ips(struct ib_device *ib_dev,
275 u8 port, struct net_device *ndev)
277 struct inet6_ifaddr *ifp;
278 struct inet6_dev *in6_dev;
280 struct list_head list;
281 struct sockaddr_in6 sin6;
283 struct sin6_list *sin6_iter;
284 struct sin6_list *sin6_temp;
285 struct ib_gid_attr gid_attr = {.ndev = ndev};
286 LIST_HEAD(sin6_list);
288 if (ndev->reg_state >= NETREG_UNREGISTERING)
291 in6_dev = in6_dev_get(ndev);
295 read_lock_bh(&in6_dev->lock);
296 list_for_each_entry(ifp, &in6_dev->addr_list, if_list) {
297 struct sin6_list *entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
300 pr_warn("roce_gid_mgmt: couldn't allocate entry for IPv6 update\n");
304 entry->sin6.sin6_family = AF_INET6;
305 entry->sin6.sin6_addr = ifp->addr;
306 list_add_tail(&entry->list, &sin6_list);
308 read_unlock_bh(&in6_dev->lock);
310 in6_dev_put(in6_dev);
312 list_for_each_entry_safe(sin6_iter, sin6_temp, &sin6_list, list) {
315 rdma_ip2gid((struct sockaddr *)&sin6_iter->sin6, &gid);
316 update_gid(GID_ADD, ib_dev, port, &gid, &gid_attr);
317 list_del(&sin6_iter->list);
322 static void _add_netdev_ips(struct ib_device *ib_dev, u8 port,
323 struct net_device *ndev)
325 enum_netdev_ipv4_ips(ib_dev, port, ndev);
326 if (IS_ENABLED(CONFIG_IPV6))
327 enum_netdev_ipv6_ips(ib_dev, port, ndev);
330 static void add_netdev_ips(struct ib_device *ib_dev, u8 port,
331 struct net_device *rdma_ndev, void *cookie)
333 struct net_device *event_ndev = (struct net_device *)cookie;
335 enum_netdev_default_gids(ib_dev, port, event_ndev, rdma_ndev);
336 _add_netdev_ips(ib_dev, port, event_ndev);
339 static void del_netdev_ips(struct ib_device *ib_dev, u8 port,
340 struct net_device *rdma_ndev, void *cookie)
342 struct net_device *event_ndev = (struct net_device *)cookie;
344 ib_cache_gid_del_all_netdev_gids(ib_dev, port, event_ndev);
347 static void enum_all_gids_of_dev_cb(struct ib_device *ib_dev,
349 struct net_device *rdma_ndev,
353 struct net_device *ndev;
355 /* Lock the rtnl to make sure the netdevs does not move under
360 for_each_netdev(net, ndev)
361 if (is_eth_port_of_netdev(ib_dev, port, rdma_ndev, ndev))
362 add_netdev_ips(ib_dev, port, rdma_ndev, ndev);
366 /* This function will rescan all of the network devices in the system
367 * and add their gids, as needed, to the relevant RoCE devices. */
368 int roce_rescan_device(struct ib_device *ib_dev)
370 ib_enum_roce_netdev(ib_dev, pass_all_filter, NULL,
371 enum_all_gids_of_dev_cb, NULL);
376 static void callback_for_addr_gid_device_scan(struct ib_device *device,
378 struct net_device *rdma_ndev,
381 struct update_gid_event_work *parsed = cookie;
383 return update_gid(parsed->gid_op, device,
388 static void handle_netdev_upper(struct ib_device *ib_dev, u8 port,
390 void (*handle_netdev)(struct ib_device *ib_dev,
392 struct net_device *ndev))
394 struct net_device *ndev = (struct net_device *)cookie;
396 struct list_head list;
397 struct net_device *upper;
399 struct net_device *upper;
400 struct list_head *iter;
401 struct upper_list *upper_iter;
402 struct upper_list *upper_temp;
403 LIST_HEAD(upper_list);
406 netdev_for_each_all_upper_dev_rcu(ndev, upper, iter) {
407 struct upper_list *entry = kmalloc(sizeof(*entry),
411 pr_info("roce_gid_mgmt: couldn't allocate entry to delete ndev\n");
415 list_add_tail(&entry->list, &upper_list);
417 entry->upper = upper;
421 handle_netdev(ib_dev, port, ndev);
422 list_for_each_entry_safe(upper_iter, upper_temp, &upper_list,
424 handle_netdev(ib_dev, port, upper_iter->upper);
425 dev_put(upper_iter->upper);
426 list_del(&upper_iter->list);
431 static void _roce_del_all_netdev_gids(struct ib_device *ib_dev, u8 port,
432 struct net_device *event_ndev)
434 ib_cache_gid_del_all_netdev_gids(ib_dev, port, event_ndev);
437 static void del_netdev_upper_ips(struct ib_device *ib_dev, u8 port,
438 struct net_device *rdma_ndev, void *cookie)
440 handle_netdev_upper(ib_dev, port, cookie, _roce_del_all_netdev_gids);
443 static void add_netdev_upper_ips(struct ib_device *ib_dev, u8 port,
444 struct net_device *rdma_ndev, void *cookie)
446 handle_netdev_upper(ib_dev, port, cookie, _add_netdev_ips);
449 static void del_netdev_default_ips_join(struct ib_device *ib_dev, u8 port,
450 struct net_device *rdma_ndev,
453 struct net_device *master_ndev;
456 master_ndev = netdev_master_upper_dev_get_rcu(rdma_ndev);
458 dev_hold(master_ndev);
462 bond_delete_netdev_default_gids(ib_dev, port, master_ndev,
464 dev_put(master_ndev);
468 static void del_netdev_default_ips(struct ib_device *ib_dev, u8 port,
469 struct net_device *rdma_ndev, void *cookie)
471 struct net_device *event_ndev = (struct net_device *)cookie;
473 bond_delete_netdev_default_gids(ib_dev, port, event_ndev, rdma_ndev);
476 /* The following functions operate on all IB devices. netdevice_event and
477 * addr_event execute ib_enum_all_roce_netdevs through a work.
478 * ib_enum_all_roce_netdevs iterates through all IB devices.
481 static void netdevice_event_work_handler(struct work_struct *_work)
483 struct netdev_event_work *work =
484 container_of(_work, struct netdev_event_work, work);
487 for (i = 0; i < ARRAY_SIZE(work->cmds) && work->cmds[i].cb; i++) {
488 ib_enum_all_roce_netdevs(work->cmds[i].filter,
489 work->cmds[i].filter_ndev,
492 dev_put(work->cmds[i].ndev);
493 dev_put(work->cmds[i].filter_ndev);
499 static int netdevice_queue_work(struct netdev_event_work_cmd *cmds,
500 struct net_device *ndev)
503 struct netdev_event_work *ndev_work =
504 kmalloc(sizeof(*ndev_work), GFP_KERNEL);
507 pr_warn("roce_gid_mgmt: can't allocate work for netdevice_event\n");
511 memcpy(ndev_work->cmds, cmds, sizeof(ndev_work->cmds));
512 for (i = 0; i < ARRAY_SIZE(ndev_work->cmds) && ndev_work->cmds[i].cb; i++) {
513 if (!ndev_work->cmds[i].ndev)
514 ndev_work->cmds[i].ndev = ndev;
515 if (!ndev_work->cmds[i].filter_ndev)
516 ndev_work->cmds[i].filter_ndev = ndev;
517 dev_hold(ndev_work->cmds[i].ndev);
518 dev_hold(ndev_work->cmds[i].filter_ndev);
520 INIT_WORK(&ndev_work->work, netdevice_event_work_handler);
522 queue_work(ib_wq, &ndev_work->work);
527 static const struct netdev_event_work_cmd add_cmd = {
528 .cb = add_netdev_ips, .filter = is_eth_port_of_netdev};
529 static const struct netdev_event_work_cmd add_cmd_upper_ips = {
530 .cb = add_netdev_upper_ips, .filter = is_eth_port_of_netdev};
532 static void netdevice_event_changeupper(struct netdev_changeupper_info *changeupper_info,
533 struct netdev_event_work_cmd *cmds)
535 static const struct netdev_event_work_cmd upper_ips_del_cmd = {
536 .cb = del_netdev_upper_ips, .filter = upper_device_filter};
537 static const struct netdev_event_work_cmd bonding_default_del_cmd = {
538 .cb = del_netdev_default_ips, .filter = is_eth_port_inactive_slave};
540 if (changeupper_info->event ==
541 NETDEV_CHANGEUPPER_UNLINK) {
542 cmds[0] = upper_ips_del_cmd;
543 cmds[0].ndev = changeupper_info->upper;
545 } else if (changeupper_info->event ==
546 NETDEV_CHANGEUPPER_LINK) {
547 cmds[0] = bonding_default_del_cmd;
548 cmds[0].ndev = changeupper_info->upper;
549 cmds[1] = add_cmd_upper_ips;
550 cmds[1].ndev = changeupper_info->upper;
551 cmds[1].filter_ndev = changeupper_info->upper;
555 static int netdevice_event(struct notifier_block *this, unsigned long event,
558 static const struct netdev_event_work_cmd del_cmd = {
559 .cb = del_netdev_ips, .filter = pass_all_filter};
560 static const struct netdev_event_work_cmd bonding_default_del_cmd_join = {
561 .cb = del_netdev_default_ips_join, .filter = is_eth_port_inactive_slave};
562 static const struct netdev_event_work_cmd default_del_cmd = {
563 .cb = del_netdev_default_ips, .filter = pass_all_filter};
564 static const struct netdev_event_work_cmd bonding_event_ips_del_cmd = {
565 .cb = del_netdev_upper_ips, .filter = upper_device_filter};
566 struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
567 struct netdev_event_work_cmd cmds[ROCE_NETDEV_CALLBACK_SZ] = { {NULL} };
569 if (ndev->type != ARPHRD_ETHER)
573 case NETDEV_REGISTER:
575 cmds[0] = bonding_default_del_cmd_join;
579 case NETDEV_UNREGISTER:
580 if (ndev->reg_state < NETREG_UNREGISTERED)
586 case NETDEV_CHANGEADDR:
587 cmds[0] = default_del_cmd;
591 case NETDEV_CHANGEUPPER:
592 netdevice_event_changeupper(
593 container_of(ptr, struct netdev_changeupper_info, info),
597 case NETDEV_BONDING_FAILOVER:
598 cmds[0] = bonding_event_ips_del_cmd;
599 cmds[1] = bonding_default_del_cmd_join;
600 cmds[2] = add_cmd_upper_ips;
607 return netdevice_queue_work(cmds, ndev);
610 static void update_gid_event_work_handler(struct work_struct *_work)
612 struct update_gid_event_work *work =
613 container_of(_work, struct update_gid_event_work, work);
615 ib_enum_all_roce_netdevs(is_eth_port_of_netdev, work->gid_attr.ndev,
616 callback_for_addr_gid_device_scan, work);
618 dev_put(work->gid_attr.ndev);
622 static int addr_event(struct notifier_block *this, unsigned long event,
623 struct sockaddr *sa, struct net_device *ndev)
625 struct update_gid_event_work *work;
626 enum gid_op_type gid_op;
628 if (ndev->type != ARPHRD_ETHER)
644 work = kmalloc(sizeof(*work), GFP_ATOMIC);
646 pr_warn("roce_gid_mgmt: Couldn't allocate work for addr_event\n");
650 INIT_WORK(&work->work, update_gid_event_work_handler);
652 rdma_ip2gid(sa, &work->gid);
653 work->gid_op = gid_op;
655 memset(&work->gid_attr, 0, sizeof(work->gid_attr));
657 work->gid_attr.ndev = ndev;
659 queue_work(ib_wq, &work->work);
664 static int inetaddr_event(struct notifier_block *this, unsigned long event,
667 struct sockaddr_in in;
668 struct net_device *ndev;
669 struct in_ifaddr *ifa = ptr;
671 in.sin_family = AF_INET;
672 in.sin_addr.s_addr = ifa->ifa_address;
673 ndev = ifa->ifa_dev->dev;
675 return addr_event(this, event, (struct sockaddr *)&in, ndev);
678 static int inet6addr_event(struct notifier_block *this, unsigned long event,
681 struct sockaddr_in6 in6;
682 struct net_device *ndev;
683 struct inet6_ifaddr *ifa6 = ptr;
685 in6.sin6_family = AF_INET6;
686 in6.sin6_addr = ifa6->addr;
687 ndev = ifa6->idev->dev;
689 return addr_event(this, event, (struct sockaddr *)&in6, ndev);
692 static struct notifier_block nb_netdevice = {
693 .notifier_call = netdevice_event
696 static struct notifier_block nb_inetaddr = {
697 .notifier_call = inetaddr_event
700 static struct notifier_block nb_inet6addr = {
701 .notifier_call = inet6addr_event
704 int __init roce_gid_mgmt_init(void)
706 register_inetaddr_notifier(&nb_inetaddr);
707 if (IS_ENABLED(CONFIG_IPV6))
708 register_inet6addr_notifier(&nb_inet6addr);
709 /* We relay on the netdevice notifier to enumerate all
710 * existing devices in the system. Register to this notifier
711 * last to make sure we will not miss any IP add/del
714 register_netdevice_notifier(&nb_netdevice);
719 void __exit roce_gid_mgmt_cleanup(void)
721 if (IS_ENABLED(CONFIG_IPV6))
722 unregister_inet6addr_notifier(&nb_inet6addr);
723 unregister_inetaddr_notifier(&nb_inetaddr);
724 unregister_netdevice_notifier(&nb_netdevice);
725 /* Ensure all gid deletion tasks complete before we go down,
726 * to avoid any reference to free'd memory. By the time
727 * ib-core is removed, all physical devices have been removed,
728 * so no issue with remaining hardware contexts.