roce_gid_mgmt.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  1. /*
  2. * Copyright (c) 2015, Mellanox Technologies inc. All rights reserved.
  3. *
  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:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  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.
  22. *
  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
  30. * SOFTWARE.
  31. */
  32. #include "core_priv.h"
  33. #include <linux/in.h>
  34. #include <linux/in6.h>
  35. /* For in6_dev_get/in6_dev_put */
  36. #include <net/addrconf.h>
  37. #include <net/bonding.h>
  38. #include <rdma/ib_cache.h>
  39. #include <rdma/ib_addr.h>
  40. enum gid_op_type {
  41. GID_DEL = 0,
  42. GID_ADD
  43. };
  44. struct update_gid_event_work {
  45. struct work_struct work;
  46. union ib_gid gid;
  47. struct ib_gid_attr gid_attr;
  48. enum gid_op_type gid_op;
  49. };
  50. #define ROCE_NETDEV_CALLBACK_SZ 3
  51. struct netdev_event_work_cmd {
  52. roce_netdev_callback cb;
  53. roce_netdev_filter filter;
  54. struct net_device *ndev;
  55. struct net_device *filter_ndev;
  56. };
  57. struct netdev_event_work {
  58. struct work_struct work;
  59. struct netdev_event_work_cmd cmds[ROCE_NETDEV_CALLBACK_SZ];
  60. };
  61. static void update_gid(enum gid_op_type gid_op, struct ib_device *ib_dev,
  62. u8 port, union ib_gid *gid,
  63. struct ib_gid_attr *gid_attr)
  64. {
  65. switch (gid_op) {
  66. case GID_ADD:
  67. ib_cache_gid_add(ib_dev, port, gid, gid_attr);
  68. break;
  69. case GID_DEL:
  70. ib_cache_gid_del(ib_dev, port, gid, gid_attr);
  71. break;
  72. }
  73. }
  74. enum bonding_slave_state {
  75. BONDING_SLAVE_STATE_ACTIVE = 1UL << 0,
  76. BONDING_SLAVE_STATE_INACTIVE = 1UL << 1,
  77. /* No primary slave or the device isn't a slave in bonding */
  78. BONDING_SLAVE_STATE_NA = 1UL << 2,
  79. };
  80. static enum bonding_slave_state is_eth_active_slave_of_bonding_rcu(struct net_device *dev,
  81. struct net_device *upper)
  82. {
  83. if (upper && netif_is_bond_master(upper)) {
  84. struct net_device *pdev =
  85. bond_option_active_slave_get_rcu(netdev_priv(upper));
  86. if (pdev)
  87. return dev == pdev ? BONDING_SLAVE_STATE_ACTIVE :
  88. BONDING_SLAVE_STATE_INACTIVE;
  89. }
  90. return BONDING_SLAVE_STATE_NA;
  91. }
  92. static bool is_upper_dev_rcu(struct net_device *dev, struct net_device *upper)
  93. {
  94. struct net_device *_upper = NULL;
  95. struct list_head *iter;
  96. netdev_for_each_all_upper_dev_rcu(dev, _upper, iter)
  97. if (_upper == upper)
  98. break;
  99. return _upper == upper;
  100. }
  101. #define REQUIRED_BOND_STATES (BONDING_SLAVE_STATE_ACTIVE | \
  102. BONDING_SLAVE_STATE_NA)
  103. static int is_eth_port_of_netdev(struct ib_device *ib_dev, u8 port,
  104. struct net_device *rdma_ndev, void *cookie)
  105. {
  106. struct net_device *event_ndev = (struct net_device *)cookie;
  107. struct net_device *real_dev;
  108. int res;
  109. if (!rdma_ndev)
  110. return 0;
  111. rcu_read_lock();
  112. real_dev = rdma_vlan_dev_real_dev(event_ndev);
  113. if (!real_dev)
  114. real_dev = event_ndev;
  115. res = ((is_upper_dev_rcu(rdma_ndev, event_ndev) &&
  116. (is_eth_active_slave_of_bonding_rcu(rdma_ndev, real_dev) &
  117. REQUIRED_BOND_STATES)) ||
  118. real_dev == rdma_ndev);
  119. rcu_read_unlock();
  120. return res;
  121. }
  122. static int is_eth_port_inactive_slave(struct ib_device *ib_dev, u8 port,
  123. struct net_device *rdma_ndev, void *cookie)
  124. {
  125. struct net_device *master_dev;
  126. int res;
  127. if (!rdma_ndev)
  128. return 0;
  129. rcu_read_lock();
  130. master_dev = netdev_master_upper_dev_get_rcu(rdma_ndev);
  131. res = is_eth_active_slave_of_bonding_rcu(rdma_ndev, master_dev) ==
  132. BONDING_SLAVE_STATE_INACTIVE;
  133. rcu_read_unlock();
  134. return res;
  135. }
  136. static int pass_all_filter(struct ib_device *ib_dev, u8 port,
  137. struct net_device *rdma_ndev, void *cookie)
  138. {
  139. return 1;
  140. }
  141. static int upper_device_filter(struct ib_device *ib_dev, u8 port,
  142. struct net_device *rdma_ndev, void *cookie)
  143. {
  144. struct net_device *event_ndev = (struct net_device *)cookie;
  145. int res;
  146. if (!rdma_ndev)
  147. return 0;
  148. if (rdma_ndev == event_ndev)
  149. return 1;
  150. rcu_read_lock();
  151. res = is_upper_dev_rcu(rdma_ndev, event_ndev);
  152. rcu_read_unlock();
  153. return res;
  154. }
  155. static void update_gid_ip(enum gid_op_type gid_op,
  156. struct ib_device *ib_dev,
  157. u8 port, struct net_device *ndev,
  158. struct sockaddr *addr)
  159. {
  160. union ib_gid gid;
  161. struct ib_gid_attr gid_attr;
  162. rdma_ip2gid(addr, &gid);
  163. memset(&gid_attr, 0, sizeof(gid_attr));
  164. gid_attr.ndev = ndev;
  165. update_gid(gid_op, ib_dev, port, &gid, &gid_attr);
  166. }
  167. static void enum_netdev_default_gids(struct ib_device *ib_dev,
  168. u8 port, struct net_device *event_ndev,
  169. struct net_device *rdma_ndev)
  170. {
  171. rcu_read_lock();
  172. if (!rdma_ndev ||
  173. ((rdma_ndev != event_ndev &&
  174. !is_upper_dev_rcu(rdma_ndev, event_ndev)) ||
  175. is_eth_active_slave_of_bonding_rcu(rdma_ndev,
  176. netdev_master_upper_dev_get_rcu(rdma_ndev)) ==
  177. BONDING_SLAVE_STATE_INACTIVE)) {
  178. rcu_read_unlock();
  179. return;
  180. }
  181. rcu_read_unlock();
  182. ib_cache_gid_set_default_gid(ib_dev, port, rdma_ndev,
  183. IB_CACHE_GID_DEFAULT_MODE_SET);
  184. }
  185. static void bond_delete_netdev_default_gids(struct ib_device *ib_dev,
  186. u8 port,
  187. struct net_device *event_ndev,
  188. struct net_device *rdma_ndev)
  189. {
  190. struct net_device *real_dev = rdma_vlan_dev_real_dev(event_ndev);
  191. if (!rdma_ndev)
  192. return;
  193. if (!real_dev)
  194. real_dev = event_ndev;
  195. rcu_read_lock();
  196. if (is_upper_dev_rcu(rdma_ndev, event_ndev) &&
  197. is_eth_active_slave_of_bonding_rcu(rdma_ndev, real_dev) ==
  198. BONDING_SLAVE_STATE_INACTIVE) {
  199. rcu_read_unlock();
  200. ib_cache_gid_set_default_gid(ib_dev, port, rdma_ndev,
  201. IB_CACHE_GID_DEFAULT_MODE_DELETE);
  202. } else {
  203. rcu_read_unlock();
  204. }
  205. }
  206. static void enum_netdev_ipv4_ips(struct ib_device *ib_dev,
  207. u8 port, struct net_device *ndev)
  208. {
  209. struct in_device *in_dev;
  210. struct sin_list {
  211. struct list_head list;
  212. struct sockaddr_in ip;
  213. };
  214. struct sin_list *sin_iter;
  215. struct sin_list *sin_temp;
  216. LIST_HEAD(sin_list);
  217. if (ndev->reg_state >= NETREG_UNREGISTERING)
  218. return;
  219. rcu_read_lock();
  220. in_dev = __in_dev_get_rcu(ndev);
  221. if (!in_dev) {
  222. rcu_read_unlock();
  223. return;
  224. }
  225. for_ifa(in_dev) {
  226. struct sin_list *entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
  227. if (!entry) {
  228. pr_warn("roce_gid_mgmt: couldn't allocate entry for IPv4 update\n");
  229. continue;
  230. }
  231. entry->ip.sin_family = AF_INET;
  232. entry->ip.sin_addr.s_addr = ifa->ifa_address;
  233. list_add_tail(&entry->list, &sin_list);
  234. }
  235. endfor_ifa(in_dev);
  236. rcu_read_unlock();
  237. list_for_each_entry_safe(sin_iter, sin_temp, &sin_list, list) {
  238. update_gid_ip(GID_ADD, ib_dev, port, ndev,
  239. (struct sockaddr *)&sin_iter->ip);
  240. list_del(&sin_iter->list);
  241. kfree(sin_iter);
  242. }
  243. }
  244. static void enum_netdev_ipv6_ips(struct ib_device *ib_dev,
  245. u8 port, struct net_device *ndev)
  246. {
  247. struct inet6_ifaddr *ifp;
  248. struct inet6_dev *in6_dev;
  249. struct sin6_list {
  250. struct list_head list;
  251. struct sockaddr_in6 sin6;
  252. };
  253. struct sin6_list *sin6_iter;
  254. struct sin6_list *sin6_temp;
  255. struct ib_gid_attr gid_attr = {.ndev = ndev};
  256. LIST_HEAD(sin6_list);
  257. if (ndev->reg_state >= NETREG_UNREGISTERING)
  258. return;
  259. in6_dev = in6_dev_get(ndev);
  260. if (!in6_dev)
  261. return;
  262. read_lock_bh(&in6_dev->lock);
  263. list_for_each_entry(ifp, &in6_dev->addr_list, if_list) {
  264. struct sin6_list *entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
  265. if (!entry) {
  266. pr_warn("roce_gid_mgmt: couldn't allocate entry for IPv6 update\n");
  267. continue;
  268. }
  269. entry->sin6.sin6_family = AF_INET6;
  270. entry->sin6.sin6_addr = ifp->addr;
  271. list_add_tail(&entry->list, &sin6_list);
  272. }
  273. read_unlock_bh(&in6_dev->lock);
  274. in6_dev_put(in6_dev);
  275. list_for_each_entry_safe(sin6_iter, sin6_temp, &sin6_list, list) {
  276. union ib_gid gid;
  277. rdma_ip2gid((struct sockaddr *)&sin6_iter->sin6, &gid);
  278. update_gid(GID_ADD, ib_dev, port, &gid, &gid_attr);
  279. list_del(&sin6_iter->list);
  280. kfree(sin6_iter);
  281. }
  282. }
  283. static void _add_netdev_ips(struct ib_device *ib_dev, u8 port,
  284. struct net_device *ndev)
  285. {
  286. enum_netdev_ipv4_ips(ib_dev, port, ndev);
  287. if (IS_ENABLED(CONFIG_IPV6))
  288. enum_netdev_ipv6_ips(ib_dev, port, ndev);
  289. }
  290. static void add_netdev_ips(struct ib_device *ib_dev, u8 port,
  291. struct net_device *rdma_ndev, void *cookie)
  292. {
  293. struct net_device *event_ndev = (struct net_device *)cookie;
  294. enum_netdev_default_gids(ib_dev, port, event_ndev, rdma_ndev);
  295. _add_netdev_ips(ib_dev, port, event_ndev);
  296. }
  297. static void del_netdev_ips(struct ib_device *ib_dev, u8 port,
  298. struct net_device *rdma_ndev, void *cookie)
  299. {
  300. struct net_device *event_ndev = (struct net_device *)cookie;
  301. ib_cache_gid_del_all_netdev_gids(ib_dev, port, event_ndev);
  302. }
  303. static void enum_all_gids_of_dev_cb(struct ib_device *ib_dev,
  304. u8 port,
  305. struct net_device *rdma_ndev,
  306. void *cookie)
  307. {
  308. struct net *net;
  309. struct net_device *ndev;
  310. /* Lock the rtnl to make sure the netdevs does not move under
  311. * our feet
  312. */
  313. rtnl_lock();
  314. for_each_net(net)
  315. for_each_netdev(net, ndev)
  316. if (is_eth_port_of_netdev(ib_dev, port, rdma_ndev, ndev))
  317. add_netdev_ips(ib_dev, port, rdma_ndev, ndev);
  318. rtnl_unlock();
  319. }
  320. /* This function will rescan all of the network devices in the system
  321. * and add their gids, as needed, to the relevant RoCE devices. */
  322. int roce_rescan_device(struct ib_device *ib_dev)
  323. {
  324. ib_enum_roce_netdev(ib_dev, pass_all_filter, NULL,
  325. enum_all_gids_of_dev_cb, NULL);
  326. return 0;
  327. }
  328. static void callback_for_addr_gid_device_scan(struct ib_device *device,
  329. u8 port,
  330. struct net_device *rdma_ndev,
  331. void *cookie)
  332. {
  333. struct update_gid_event_work *parsed = cookie;
  334. return update_gid(parsed->gid_op, device,
  335. port, &parsed->gid,
  336. &parsed->gid_attr);
  337. }
  338. static void handle_netdev_upper(struct ib_device *ib_dev, u8 port,
  339. void *cookie,
  340. void (*handle_netdev)(struct ib_device *ib_dev,
  341. u8 port,
  342. struct net_device *ndev))
  343. {
  344. struct net_device *ndev = (struct net_device *)cookie;
  345. struct upper_list {
  346. struct list_head list;
  347. struct net_device *upper;
  348. };
  349. struct net_device *upper;
  350. struct list_head *iter;
  351. struct upper_list *upper_iter;
  352. struct upper_list *upper_temp;
  353. LIST_HEAD(upper_list);
  354. rcu_read_lock();
  355. netdev_for_each_all_upper_dev_rcu(ndev, upper, iter) {
  356. struct upper_list *entry = kmalloc(sizeof(*entry),
  357. GFP_ATOMIC);
  358. if (!entry) {
  359. pr_info("roce_gid_mgmt: couldn't allocate entry to delete ndev\n");
  360. continue;
  361. }
  362. list_add_tail(&entry->list, &upper_list);
  363. dev_hold(upper);
  364. entry->upper = upper;
  365. }
  366. rcu_read_unlock();
  367. handle_netdev(ib_dev, port, ndev);
  368. list_for_each_entry_safe(upper_iter, upper_temp, &upper_list,
  369. list) {
  370. handle_netdev(ib_dev, port, upper_iter->upper);
  371. dev_put(upper_iter->upper);
  372. list_del(&upper_iter->list);
  373. kfree(upper_iter);
  374. }
  375. }
  376. static void _roce_del_all_netdev_gids(struct ib_device *ib_dev, u8 port,
  377. struct net_device *event_ndev)
  378. {
  379. ib_cache_gid_del_all_netdev_gids(ib_dev, port, event_ndev);
  380. }
  381. static void del_netdev_upper_ips(struct ib_device *ib_dev, u8 port,
  382. struct net_device *rdma_ndev, void *cookie)
  383. {
  384. handle_netdev_upper(ib_dev, port, cookie, _roce_del_all_netdev_gids);
  385. }
  386. static void add_netdev_upper_ips(struct ib_device *ib_dev, u8 port,
  387. struct net_device *rdma_ndev, void *cookie)
  388. {
  389. handle_netdev_upper(ib_dev, port, cookie, _add_netdev_ips);
  390. }
  391. static void del_netdev_default_ips_join(struct ib_device *ib_dev, u8 port,
  392. struct net_device *rdma_ndev,
  393. void *cookie)
  394. {
  395. struct net_device *master_ndev;
  396. rcu_read_lock();
  397. master_ndev = netdev_master_upper_dev_get_rcu(rdma_ndev);
  398. if (master_ndev)
  399. dev_hold(master_ndev);
  400. rcu_read_unlock();
  401. if (master_ndev) {
  402. bond_delete_netdev_default_gids(ib_dev, port, master_ndev,
  403. rdma_ndev);
  404. dev_put(master_ndev);
  405. }
  406. }
  407. static void del_netdev_default_ips(struct ib_device *ib_dev, u8 port,
  408. struct net_device *rdma_ndev, void *cookie)
  409. {
  410. struct net_device *event_ndev = (struct net_device *)cookie;
  411. bond_delete_netdev_default_gids(ib_dev, port, event_ndev, rdma_ndev);
  412. }
  413. /* The following functions operate on all IB devices. netdevice_event and
  414. * addr_event execute ib_enum_all_roce_netdevs through a work.
  415. * ib_enum_all_roce_netdevs iterates through all IB devices.
  416. */
  417. static void netdevice_event_work_handler(struct work_struct *_work)
  418. {
  419. struct netdev_event_work *work =
  420. container_of(_work, struct netdev_event_work, work);
  421. unsigned int i;
  422. for (i = 0; i < ARRAY_SIZE(work->cmds) && work->cmds[i].cb; i++) {
  423. ib_enum_all_roce_netdevs(work->cmds[i].filter,
  424. work->cmds[i].filter_ndev,
  425. work->cmds[i].cb,
  426. work->cmds[i].ndev);
  427. dev_put(work->cmds[i].ndev);
  428. dev_put(work->cmds[i].filter_ndev);
  429. }
  430. kfree(work);
  431. }
  432. static int netdevice_queue_work(struct netdev_event_work_cmd *cmds,
  433. struct net_device *ndev)
  434. {
  435. unsigned int i;
  436. struct netdev_event_work *ndev_work =
  437. kmalloc(sizeof(*ndev_work), GFP_KERNEL);
  438. if (!ndev_work) {
  439. pr_warn("roce_gid_mgmt: can't allocate work for netdevice_event\n");
  440. return NOTIFY_DONE;
  441. }
  442. memcpy(ndev_work->cmds, cmds, sizeof(ndev_work->cmds));
  443. for (i = 0; i < ARRAY_SIZE(ndev_work->cmds) && ndev_work->cmds[i].cb; i++) {
  444. if (!ndev_work->cmds[i].ndev)
  445. ndev_work->cmds[i].ndev = ndev;
  446. if (!ndev_work->cmds[i].filter_ndev)
  447. ndev_work->cmds[i].filter_ndev = ndev;
  448. dev_hold(ndev_work->cmds[i].ndev);
  449. dev_hold(ndev_work->cmds[i].filter_ndev);
  450. }
  451. INIT_WORK(&ndev_work->work, netdevice_event_work_handler);
  452. queue_work(ib_wq, &ndev_work->work);
  453. return NOTIFY_DONE;
  454. }
  455. static const struct netdev_event_work_cmd add_cmd = {
  456. .cb = add_netdev_ips, .filter = is_eth_port_of_netdev};
  457. static const struct netdev_event_work_cmd add_cmd_upper_ips = {
  458. .cb = add_netdev_upper_ips, .filter = is_eth_port_of_netdev};
  459. static void netdevice_event_changeupper(struct netdev_notifier_changeupper_info *changeupper_info,
  460. struct netdev_event_work_cmd *cmds)
  461. {
  462. static const struct netdev_event_work_cmd upper_ips_del_cmd = {
  463. .cb = del_netdev_upper_ips, .filter = upper_device_filter};
  464. static const struct netdev_event_work_cmd bonding_default_del_cmd = {
  465. .cb = del_netdev_default_ips, .filter = is_eth_port_inactive_slave};
  466. if (changeupper_info->linking == false) {
  467. cmds[0] = upper_ips_del_cmd;
  468. cmds[0].ndev = changeupper_info->upper_dev;
  469. cmds[1] = add_cmd;
  470. } else {
  471. cmds[0] = bonding_default_del_cmd;
  472. cmds[0].ndev = changeupper_info->upper_dev;
  473. cmds[1] = add_cmd_upper_ips;
  474. cmds[1].ndev = changeupper_info->upper_dev;
  475. cmds[1].filter_ndev = changeupper_info->upper_dev;
  476. }
  477. }
  478. static int netdevice_event(struct notifier_block *this, unsigned long event,
  479. void *ptr)
  480. {
  481. static const struct netdev_event_work_cmd del_cmd = {
  482. .cb = del_netdev_ips, .filter = pass_all_filter};
  483. static const struct netdev_event_work_cmd bonding_default_del_cmd_join = {
  484. .cb = del_netdev_default_ips_join, .filter = is_eth_port_inactive_slave};
  485. static const struct netdev_event_work_cmd default_del_cmd = {
  486. .cb = del_netdev_default_ips, .filter = pass_all_filter};
  487. static const struct netdev_event_work_cmd bonding_event_ips_del_cmd = {
  488. .cb = del_netdev_upper_ips, .filter = upper_device_filter};
  489. struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
  490. struct netdev_event_work_cmd cmds[ROCE_NETDEV_CALLBACK_SZ] = { {NULL} };
  491. if (ndev->type != ARPHRD_ETHER)
  492. return NOTIFY_DONE;
  493. switch (event) {
  494. case NETDEV_REGISTER:
  495. case NETDEV_UP:
  496. cmds[0] = bonding_default_del_cmd_join;
  497. cmds[1] = add_cmd;
  498. break;
  499. case NETDEV_UNREGISTER:
  500. if (ndev->reg_state < NETREG_UNREGISTERED)
  501. cmds[0] = del_cmd;
  502. else
  503. return NOTIFY_DONE;
  504. break;
  505. case NETDEV_CHANGEADDR:
  506. cmds[0] = default_del_cmd;
  507. cmds[1] = add_cmd;
  508. break;
  509. case NETDEV_CHANGEUPPER:
  510. netdevice_event_changeupper(
  511. container_of(ptr, struct netdev_notifier_changeupper_info, info),
  512. cmds);
  513. break;
  514. case NETDEV_BONDING_FAILOVER:
  515. cmds[0] = bonding_event_ips_del_cmd;
  516. cmds[1] = bonding_default_del_cmd_join;
  517. cmds[2] = add_cmd_upper_ips;
  518. break;
  519. default:
  520. return NOTIFY_DONE;
  521. }
  522. return netdevice_queue_work(cmds, ndev);
  523. }
  524. static void update_gid_event_work_handler(struct work_struct *_work)
  525. {
  526. struct update_gid_event_work *work =
  527. container_of(_work, struct update_gid_event_work, work);
  528. ib_enum_all_roce_netdevs(is_eth_port_of_netdev, work->gid_attr.ndev,
  529. callback_for_addr_gid_device_scan, work);
  530. dev_put(work->gid_attr.ndev);
  531. kfree(work);
  532. }
  533. static int addr_event(struct notifier_block *this, unsigned long event,
  534. struct sockaddr *sa, struct net_device *ndev)
  535. {
  536. struct update_gid_event_work *work;
  537. enum gid_op_type gid_op;
  538. if (ndev->type != ARPHRD_ETHER)
  539. return NOTIFY_DONE;
  540. switch (event) {
  541. case NETDEV_UP:
  542. gid_op = GID_ADD;
  543. break;
  544. case NETDEV_DOWN:
  545. gid_op = GID_DEL;
  546. break;
  547. default:
  548. return NOTIFY_DONE;
  549. }
  550. work = kmalloc(sizeof(*work), GFP_ATOMIC);
  551. if (!work) {
  552. pr_warn("roce_gid_mgmt: Couldn't allocate work for addr_event\n");
  553. return NOTIFY_DONE;
  554. }
  555. INIT_WORK(&work->work, update_gid_event_work_handler);
  556. rdma_ip2gid(sa, &work->gid);
  557. work->gid_op = gid_op;
  558. memset(&work->gid_attr, 0, sizeof(work->gid_attr));
  559. dev_hold(ndev);
  560. work->gid_attr.ndev = ndev;
  561. queue_work(ib_wq, &work->work);
  562. return NOTIFY_DONE;
  563. }
  564. static int inetaddr_event(struct notifier_block *this, unsigned long event,
  565. void *ptr)
  566. {
  567. struct sockaddr_in in;
  568. struct net_device *ndev;
  569. struct in_ifaddr *ifa = ptr;
  570. in.sin_family = AF_INET;
  571. in.sin_addr.s_addr = ifa->ifa_address;
  572. ndev = ifa->ifa_dev->dev;
  573. return addr_event(this, event, (struct sockaddr *)&in, ndev);
  574. }
  575. static int inet6addr_event(struct notifier_block *this, unsigned long event,
  576. void *ptr)
  577. {
  578. struct sockaddr_in6 in6;
  579. struct net_device *ndev;
  580. struct inet6_ifaddr *ifa6 = ptr;
  581. in6.sin6_family = AF_INET6;
  582. in6.sin6_addr = ifa6->addr;
  583. ndev = ifa6->idev->dev;
  584. return addr_event(this, event, (struct sockaddr *)&in6, ndev);
  585. }
  586. static struct notifier_block nb_netdevice = {
  587. .notifier_call = netdevice_event
  588. };
  589. static struct notifier_block nb_inetaddr = {
  590. .notifier_call = inetaddr_event
  591. };
  592. static struct notifier_block nb_inet6addr = {
  593. .notifier_call = inet6addr_event
  594. };
  595. int __init roce_gid_mgmt_init(void)
  596. {
  597. register_inetaddr_notifier(&nb_inetaddr);
  598. if (IS_ENABLED(CONFIG_IPV6))
  599. register_inet6addr_notifier(&nb_inet6addr);
  600. /* We relay on the netdevice notifier to enumerate all
  601. * existing devices in the system. Register to this notifier
  602. * last to make sure we will not miss any IP add/del
  603. * callbacks.
  604. */
  605. register_netdevice_notifier(&nb_netdevice);
  606. return 0;
  607. }
  608. void __exit roce_gid_mgmt_cleanup(void)
  609. {
  610. if (IS_ENABLED(CONFIG_IPV6))
  611. unregister_inet6addr_notifier(&nb_inet6addr);
  612. unregister_inetaddr_notifier(&nb_inetaddr);
  613. unregister_netdevice_notifier(&nb_netdevice);
  614. /* Ensure all gid deletion tasks complete before we go down,
  615. * to avoid any reference to free'd memory. By the time
  616. * ib-core is removed, all physical devices have been removed,
  617. * so no issue with remaining hardware contexts.
  618. */
  619. }