roce_gid_mgmt.c 20 KB

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