roce_gid_mgmt.c 20 KB

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