ipoib_main.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801
  1. /*
  2. * Copyright (c) 2004 Topspin Communications. All rights reserved.
  3. * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
  4. * Copyright (c) 2004 Voltaire, Inc. All rights reserved.
  5. *
  6. * This software is available to you under a choice of one of two
  7. * licenses. You may choose to be licensed under the terms of the GNU
  8. * General Public License (GPL) Version 2, available from the file
  9. * COPYING in the main directory of this source tree, or the
  10. * OpenIB.org BSD license below:
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above
  17. * copyright notice, this list of conditions and the following
  18. * disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials
  23. * provided with the distribution.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  29. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  30. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  31. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  32. * SOFTWARE.
  33. */
  34. #include "ipoib.h"
  35. #include <linux/module.h>
  36. #include <linux/init.h>
  37. #include <linux/slab.h>
  38. #include <linux/kernel.h>
  39. #include <linux/vmalloc.h>
  40. #include <linux/if_arp.h> /* For ARPHRD_xxx */
  41. #include <linux/ip.h>
  42. #include <linux/in.h>
  43. #include <linux/jhash.h>
  44. #include <net/arp.h>
  45. #define DRV_VERSION "1.0.0"
  46. const char ipoib_driver_version[] = DRV_VERSION;
  47. MODULE_AUTHOR("Roland Dreier");
  48. MODULE_DESCRIPTION("IP-over-InfiniBand net driver");
  49. MODULE_LICENSE("Dual BSD/GPL");
  50. MODULE_VERSION(DRV_VERSION);
  51. int ipoib_sendq_size __read_mostly = IPOIB_TX_RING_SIZE;
  52. int ipoib_recvq_size __read_mostly = IPOIB_RX_RING_SIZE;
  53. module_param_named(send_queue_size, ipoib_sendq_size, int, 0444);
  54. MODULE_PARM_DESC(send_queue_size, "Number of descriptors in send queue");
  55. module_param_named(recv_queue_size, ipoib_recvq_size, int, 0444);
  56. MODULE_PARM_DESC(recv_queue_size, "Number of descriptors in receive queue");
  57. #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
  58. int ipoib_debug_level;
  59. module_param_named(debug_level, ipoib_debug_level, int, 0644);
  60. MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0");
  61. #endif
  62. struct ipoib_path_iter {
  63. struct net_device *dev;
  64. struct ipoib_path path;
  65. };
  66. static const u8 ipv4_bcast_addr[] = {
  67. 0x00, 0xff, 0xff, 0xff,
  68. 0xff, 0x12, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
  69. 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff
  70. };
  71. struct workqueue_struct *ipoib_workqueue;
  72. struct ib_sa_client ipoib_sa_client;
  73. static void ipoib_add_one(struct ib_device *device);
  74. static void ipoib_remove_one(struct ib_device *device);
  75. static void ipoib_neigh_reclaim(struct rcu_head *rp);
  76. static struct ib_client ipoib_client = {
  77. .name = "ipoib",
  78. .add = ipoib_add_one,
  79. .remove = ipoib_remove_one
  80. };
  81. int ipoib_open(struct net_device *dev)
  82. {
  83. struct ipoib_dev_priv *priv = netdev_priv(dev);
  84. ipoib_dbg(priv, "bringing up interface\n");
  85. netif_carrier_off(dev);
  86. set_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
  87. if (ipoib_ib_dev_open(dev, 1)) {
  88. if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags))
  89. return 0;
  90. goto err_disable;
  91. }
  92. if (ipoib_ib_dev_up(dev))
  93. goto err_stop;
  94. if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
  95. struct ipoib_dev_priv *cpriv;
  96. /* Bring up any child interfaces too */
  97. down_read(&priv->vlan_rwsem);
  98. list_for_each_entry(cpriv, &priv->child_intfs, list) {
  99. int flags;
  100. flags = cpriv->dev->flags;
  101. if (flags & IFF_UP)
  102. continue;
  103. dev_change_flags(cpriv->dev, flags | IFF_UP);
  104. }
  105. up_read(&priv->vlan_rwsem);
  106. }
  107. netif_start_queue(dev);
  108. return 0;
  109. err_stop:
  110. ipoib_ib_dev_stop(dev, 1);
  111. err_disable:
  112. clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
  113. return -EINVAL;
  114. }
  115. static int ipoib_stop(struct net_device *dev)
  116. {
  117. struct ipoib_dev_priv *priv = netdev_priv(dev);
  118. ipoib_dbg(priv, "stopping interface\n");
  119. clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
  120. netif_stop_queue(dev);
  121. ipoib_ib_dev_down(dev, 1);
  122. ipoib_ib_dev_stop(dev, 0);
  123. if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
  124. struct ipoib_dev_priv *cpriv;
  125. /* Bring down any child interfaces too */
  126. down_read(&priv->vlan_rwsem);
  127. list_for_each_entry(cpriv, &priv->child_intfs, list) {
  128. int flags;
  129. flags = cpriv->dev->flags;
  130. if (!(flags & IFF_UP))
  131. continue;
  132. dev_change_flags(cpriv->dev, flags & ~IFF_UP);
  133. }
  134. up_read(&priv->vlan_rwsem);
  135. }
  136. return 0;
  137. }
  138. static void ipoib_uninit(struct net_device *dev)
  139. {
  140. ipoib_dev_cleanup(dev);
  141. }
  142. static netdev_features_t ipoib_fix_features(struct net_device *dev, netdev_features_t features)
  143. {
  144. struct ipoib_dev_priv *priv = netdev_priv(dev);
  145. if (test_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags))
  146. features &= ~(NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO);
  147. return features;
  148. }
  149. static int ipoib_change_mtu(struct net_device *dev, int new_mtu)
  150. {
  151. struct ipoib_dev_priv *priv = netdev_priv(dev);
  152. /* dev->mtu > 2K ==> connected mode */
  153. if (ipoib_cm_admin_enabled(dev)) {
  154. if (new_mtu > ipoib_cm_max_mtu(dev))
  155. return -EINVAL;
  156. if (new_mtu > priv->mcast_mtu)
  157. ipoib_warn(priv, "mtu > %d will cause multicast packet drops.\n",
  158. priv->mcast_mtu);
  159. dev->mtu = new_mtu;
  160. return 0;
  161. }
  162. if (new_mtu > IPOIB_UD_MTU(priv->max_ib_mtu))
  163. return -EINVAL;
  164. priv->admin_mtu = new_mtu;
  165. dev->mtu = min(priv->mcast_mtu, priv->admin_mtu);
  166. return 0;
  167. }
  168. int ipoib_set_mode(struct net_device *dev, const char *buf)
  169. {
  170. struct ipoib_dev_priv *priv = netdev_priv(dev);
  171. /* flush paths if we switch modes so that connections are restarted */
  172. if (IPOIB_CM_SUPPORTED(dev->dev_addr) && !strcmp(buf, "connected\n")) {
  173. set_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags);
  174. ipoib_warn(priv, "enabling connected mode "
  175. "will cause multicast packet drops\n");
  176. netdev_update_features(dev);
  177. rtnl_unlock();
  178. priv->tx_wr.send_flags &= ~IB_SEND_IP_CSUM;
  179. ipoib_flush_paths(dev);
  180. rtnl_lock();
  181. return 0;
  182. }
  183. if (!strcmp(buf, "datagram\n")) {
  184. clear_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags);
  185. netdev_update_features(dev);
  186. dev_set_mtu(dev, min(priv->mcast_mtu, dev->mtu));
  187. rtnl_unlock();
  188. ipoib_flush_paths(dev);
  189. rtnl_lock();
  190. return 0;
  191. }
  192. return -EINVAL;
  193. }
  194. static struct ipoib_path *__path_find(struct net_device *dev, void *gid)
  195. {
  196. struct ipoib_dev_priv *priv = netdev_priv(dev);
  197. struct rb_node *n = priv->path_tree.rb_node;
  198. struct ipoib_path *path;
  199. int ret;
  200. while (n) {
  201. path = rb_entry(n, struct ipoib_path, rb_node);
  202. ret = memcmp(gid, path->pathrec.dgid.raw,
  203. sizeof (union ib_gid));
  204. if (ret < 0)
  205. n = n->rb_left;
  206. else if (ret > 0)
  207. n = n->rb_right;
  208. else
  209. return path;
  210. }
  211. return NULL;
  212. }
  213. static int __path_add(struct net_device *dev, struct ipoib_path *path)
  214. {
  215. struct ipoib_dev_priv *priv = netdev_priv(dev);
  216. struct rb_node **n = &priv->path_tree.rb_node;
  217. struct rb_node *pn = NULL;
  218. struct ipoib_path *tpath;
  219. int ret;
  220. while (*n) {
  221. pn = *n;
  222. tpath = rb_entry(pn, struct ipoib_path, rb_node);
  223. ret = memcmp(path->pathrec.dgid.raw, tpath->pathrec.dgid.raw,
  224. sizeof (union ib_gid));
  225. if (ret < 0)
  226. n = &pn->rb_left;
  227. else if (ret > 0)
  228. n = &pn->rb_right;
  229. else
  230. return -EEXIST;
  231. }
  232. rb_link_node(&path->rb_node, pn, n);
  233. rb_insert_color(&path->rb_node, &priv->path_tree);
  234. list_add_tail(&path->list, &priv->path_list);
  235. return 0;
  236. }
  237. static void path_free(struct net_device *dev, struct ipoib_path *path)
  238. {
  239. struct sk_buff *skb;
  240. while ((skb = __skb_dequeue(&path->queue)))
  241. dev_kfree_skb_irq(skb);
  242. ipoib_dbg(netdev_priv(dev), "path_free\n");
  243. /* remove all neigh connected to this path */
  244. ipoib_del_neighs_by_gid(dev, path->pathrec.dgid.raw);
  245. if (path->ah)
  246. ipoib_put_ah(path->ah);
  247. kfree(path);
  248. }
  249. #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
  250. struct ipoib_path_iter *ipoib_path_iter_init(struct net_device *dev)
  251. {
  252. struct ipoib_path_iter *iter;
  253. iter = kmalloc(sizeof *iter, GFP_KERNEL);
  254. if (!iter)
  255. return NULL;
  256. iter->dev = dev;
  257. memset(iter->path.pathrec.dgid.raw, 0, 16);
  258. if (ipoib_path_iter_next(iter)) {
  259. kfree(iter);
  260. return NULL;
  261. }
  262. return iter;
  263. }
  264. int ipoib_path_iter_next(struct ipoib_path_iter *iter)
  265. {
  266. struct ipoib_dev_priv *priv = netdev_priv(iter->dev);
  267. struct rb_node *n;
  268. struct ipoib_path *path;
  269. int ret = 1;
  270. spin_lock_irq(&priv->lock);
  271. n = rb_first(&priv->path_tree);
  272. while (n) {
  273. path = rb_entry(n, struct ipoib_path, rb_node);
  274. if (memcmp(iter->path.pathrec.dgid.raw, path->pathrec.dgid.raw,
  275. sizeof (union ib_gid)) < 0) {
  276. iter->path = *path;
  277. ret = 0;
  278. break;
  279. }
  280. n = rb_next(n);
  281. }
  282. spin_unlock_irq(&priv->lock);
  283. return ret;
  284. }
  285. void ipoib_path_iter_read(struct ipoib_path_iter *iter,
  286. struct ipoib_path *path)
  287. {
  288. *path = iter->path;
  289. }
  290. #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */
  291. void ipoib_mark_paths_invalid(struct net_device *dev)
  292. {
  293. struct ipoib_dev_priv *priv = netdev_priv(dev);
  294. struct ipoib_path *path, *tp;
  295. spin_lock_irq(&priv->lock);
  296. list_for_each_entry_safe(path, tp, &priv->path_list, list) {
  297. ipoib_dbg(priv, "mark path LID 0x%04x GID %pI6 invalid\n",
  298. be16_to_cpu(path->pathrec.dlid),
  299. path->pathrec.dgid.raw);
  300. path->valid = 0;
  301. }
  302. spin_unlock_irq(&priv->lock);
  303. }
  304. void ipoib_flush_paths(struct net_device *dev)
  305. {
  306. struct ipoib_dev_priv *priv = netdev_priv(dev);
  307. struct ipoib_path *path, *tp;
  308. LIST_HEAD(remove_list);
  309. unsigned long flags;
  310. netif_tx_lock_bh(dev);
  311. spin_lock_irqsave(&priv->lock, flags);
  312. list_splice_init(&priv->path_list, &remove_list);
  313. list_for_each_entry(path, &remove_list, list)
  314. rb_erase(&path->rb_node, &priv->path_tree);
  315. list_for_each_entry_safe(path, tp, &remove_list, list) {
  316. if (path->query)
  317. ib_sa_cancel_query(path->query_id, path->query);
  318. spin_unlock_irqrestore(&priv->lock, flags);
  319. netif_tx_unlock_bh(dev);
  320. wait_for_completion(&path->done);
  321. path_free(dev, path);
  322. netif_tx_lock_bh(dev);
  323. spin_lock_irqsave(&priv->lock, flags);
  324. }
  325. spin_unlock_irqrestore(&priv->lock, flags);
  326. netif_tx_unlock_bh(dev);
  327. }
  328. static void path_rec_completion(int status,
  329. struct ib_sa_path_rec *pathrec,
  330. void *path_ptr)
  331. {
  332. struct ipoib_path *path = path_ptr;
  333. struct net_device *dev = path->dev;
  334. struct ipoib_dev_priv *priv = netdev_priv(dev);
  335. struct ipoib_ah *ah = NULL;
  336. struct ipoib_ah *old_ah = NULL;
  337. struct ipoib_neigh *neigh, *tn;
  338. struct sk_buff_head skqueue;
  339. struct sk_buff *skb;
  340. unsigned long flags;
  341. if (!status)
  342. ipoib_dbg(priv, "PathRec LID 0x%04x for GID %pI6\n",
  343. be16_to_cpu(pathrec->dlid), pathrec->dgid.raw);
  344. else
  345. ipoib_dbg(priv, "PathRec status %d for GID %pI6\n",
  346. status, path->pathrec.dgid.raw);
  347. skb_queue_head_init(&skqueue);
  348. if (!status) {
  349. struct ib_ah_attr av;
  350. if (!ib_init_ah_from_path(priv->ca, priv->port, pathrec, &av))
  351. ah = ipoib_create_ah(dev, priv->pd, &av);
  352. }
  353. spin_lock_irqsave(&priv->lock, flags);
  354. if (!IS_ERR_OR_NULL(ah)) {
  355. path->pathrec = *pathrec;
  356. old_ah = path->ah;
  357. path->ah = ah;
  358. ipoib_dbg(priv, "created address handle %p for LID 0x%04x, SL %d\n",
  359. ah, be16_to_cpu(pathrec->dlid), pathrec->sl);
  360. while ((skb = __skb_dequeue(&path->queue)))
  361. __skb_queue_tail(&skqueue, skb);
  362. list_for_each_entry_safe(neigh, tn, &path->neigh_list, list) {
  363. if (neigh->ah) {
  364. WARN_ON(neigh->ah != old_ah);
  365. /*
  366. * Dropping the ah reference inside
  367. * priv->lock is safe here, because we
  368. * will hold one more reference from
  369. * the original value of path->ah (ie
  370. * old_ah).
  371. */
  372. ipoib_put_ah(neigh->ah);
  373. }
  374. kref_get(&path->ah->ref);
  375. neigh->ah = path->ah;
  376. if (ipoib_cm_enabled(dev, neigh->daddr)) {
  377. if (!ipoib_cm_get(neigh))
  378. ipoib_cm_set(neigh, ipoib_cm_create_tx(dev,
  379. path,
  380. neigh));
  381. if (!ipoib_cm_get(neigh)) {
  382. ipoib_neigh_free(neigh);
  383. continue;
  384. }
  385. }
  386. while ((skb = __skb_dequeue(&neigh->queue)))
  387. __skb_queue_tail(&skqueue, skb);
  388. }
  389. path->valid = 1;
  390. }
  391. path->query = NULL;
  392. complete(&path->done);
  393. spin_unlock_irqrestore(&priv->lock, flags);
  394. if (IS_ERR_OR_NULL(ah))
  395. ipoib_del_neighs_by_gid(dev, path->pathrec.dgid.raw);
  396. if (old_ah)
  397. ipoib_put_ah(old_ah);
  398. while ((skb = __skb_dequeue(&skqueue))) {
  399. skb->dev = dev;
  400. if (dev_queue_xmit(skb))
  401. ipoib_warn(priv, "dev_queue_xmit failed "
  402. "to requeue packet\n");
  403. }
  404. }
  405. static struct ipoib_path *path_rec_create(struct net_device *dev, void *gid)
  406. {
  407. struct ipoib_dev_priv *priv = netdev_priv(dev);
  408. struct ipoib_path *path;
  409. if (!priv->broadcast)
  410. return NULL;
  411. path = kzalloc(sizeof *path, GFP_ATOMIC);
  412. if (!path)
  413. return NULL;
  414. path->dev = dev;
  415. skb_queue_head_init(&path->queue);
  416. INIT_LIST_HEAD(&path->neigh_list);
  417. memcpy(path->pathrec.dgid.raw, gid, sizeof (union ib_gid));
  418. path->pathrec.sgid = priv->local_gid;
  419. path->pathrec.pkey = cpu_to_be16(priv->pkey);
  420. path->pathrec.numb_path = 1;
  421. path->pathrec.traffic_class = priv->broadcast->mcmember.traffic_class;
  422. return path;
  423. }
  424. static int path_rec_start(struct net_device *dev,
  425. struct ipoib_path *path)
  426. {
  427. struct ipoib_dev_priv *priv = netdev_priv(dev);
  428. ipoib_dbg(priv, "Start path record lookup for %pI6\n",
  429. path->pathrec.dgid.raw);
  430. init_completion(&path->done);
  431. path->query_id =
  432. ib_sa_path_rec_get(&ipoib_sa_client, priv->ca, priv->port,
  433. &path->pathrec,
  434. IB_SA_PATH_REC_DGID |
  435. IB_SA_PATH_REC_SGID |
  436. IB_SA_PATH_REC_NUMB_PATH |
  437. IB_SA_PATH_REC_TRAFFIC_CLASS |
  438. IB_SA_PATH_REC_PKEY,
  439. 1000, GFP_ATOMIC,
  440. path_rec_completion,
  441. path, &path->query);
  442. if (path->query_id < 0) {
  443. ipoib_warn(priv, "ib_sa_path_rec_get failed: %d\n", path->query_id);
  444. path->query = NULL;
  445. complete(&path->done);
  446. return path->query_id;
  447. }
  448. return 0;
  449. }
  450. static void neigh_add_path(struct sk_buff *skb, u8 *daddr,
  451. struct net_device *dev)
  452. {
  453. struct ipoib_dev_priv *priv = netdev_priv(dev);
  454. struct ipoib_path *path;
  455. struct ipoib_neigh *neigh;
  456. unsigned long flags;
  457. spin_lock_irqsave(&priv->lock, flags);
  458. neigh = ipoib_neigh_alloc(daddr, dev);
  459. if (!neigh) {
  460. spin_unlock_irqrestore(&priv->lock, flags);
  461. ++dev->stats.tx_dropped;
  462. dev_kfree_skb_any(skb);
  463. return;
  464. }
  465. path = __path_find(dev, daddr + 4);
  466. if (!path) {
  467. path = path_rec_create(dev, daddr + 4);
  468. if (!path)
  469. goto err_path;
  470. __path_add(dev, path);
  471. }
  472. list_add_tail(&neigh->list, &path->neigh_list);
  473. if (path->ah) {
  474. kref_get(&path->ah->ref);
  475. neigh->ah = path->ah;
  476. if (ipoib_cm_enabled(dev, neigh->daddr)) {
  477. if (!ipoib_cm_get(neigh))
  478. ipoib_cm_set(neigh, ipoib_cm_create_tx(dev, path, neigh));
  479. if (!ipoib_cm_get(neigh)) {
  480. ipoib_neigh_free(neigh);
  481. goto err_drop;
  482. }
  483. if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE)
  484. __skb_queue_tail(&neigh->queue, skb);
  485. else {
  486. ipoib_warn(priv, "queue length limit %d. Packet drop.\n",
  487. skb_queue_len(&neigh->queue));
  488. goto err_drop;
  489. }
  490. } else {
  491. spin_unlock_irqrestore(&priv->lock, flags);
  492. ipoib_send(dev, skb, path->ah, IPOIB_QPN(daddr));
  493. ipoib_neigh_put(neigh);
  494. return;
  495. }
  496. } else {
  497. neigh->ah = NULL;
  498. if (!path->query && path_rec_start(dev, path))
  499. goto err_path;
  500. __skb_queue_tail(&neigh->queue, skb);
  501. }
  502. spin_unlock_irqrestore(&priv->lock, flags);
  503. ipoib_neigh_put(neigh);
  504. return;
  505. err_path:
  506. ipoib_neigh_free(neigh);
  507. err_drop:
  508. ++dev->stats.tx_dropped;
  509. dev_kfree_skb_any(skb);
  510. spin_unlock_irqrestore(&priv->lock, flags);
  511. ipoib_neigh_put(neigh);
  512. }
  513. static void unicast_arp_send(struct sk_buff *skb, struct net_device *dev,
  514. struct ipoib_cb *cb)
  515. {
  516. struct ipoib_dev_priv *priv = netdev_priv(dev);
  517. struct ipoib_path *path;
  518. unsigned long flags;
  519. spin_lock_irqsave(&priv->lock, flags);
  520. path = __path_find(dev, cb->hwaddr + 4);
  521. if (!path || !path->valid) {
  522. int new_path = 0;
  523. if (!path) {
  524. path = path_rec_create(dev, cb->hwaddr + 4);
  525. new_path = 1;
  526. }
  527. if (path) {
  528. __skb_queue_tail(&path->queue, skb);
  529. if (!path->query && path_rec_start(dev, path)) {
  530. spin_unlock_irqrestore(&priv->lock, flags);
  531. if (new_path)
  532. path_free(dev, path);
  533. return;
  534. } else
  535. __path_add(dev, path);
  536. } else {
  537. ++dev->stats.tx_dropped;
  538. dev_kfree_skb_any(skb);
  539. }
  540. spin_unlock_irqrestore(&priv->lock, flags);
  541. return;
  542. }
  543. if (path->ah) {
  544. ipoib_dbg(priv, "Send unicast ARP to %04x\n",
  545. be16_to_cpu(path->pathrec.dlid));
  546. spin_unlock_irqrestore(&priv->lock, flags);
  547. ipoib_send(dev, skb, path->ah, IPOIB_QPN(cb->hwaddr));
  548. return;
  549. } else if ((path->query || !path_rec_start(dev, path)) &&
  550. skb_queue_len(&path->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
  551. __skb_queue_tail(&path->queue, skb);
  552. } else {
  553. ++dev->stats.tx_dropped;
  554. dev_kfree_skb_any(skb);
  555. }
  556. spin_unlock_irqrestore(&priv->lock, flags);
  557. }
  558. static int ipoib_start_xmit(struct sk_buff *skb, struct net_device *dev)
  559. {
  560. struct ipoib_dev_priv *priv = netdev_priv(dev);
  561. struct ipoib_neigh *neigh;
  562. struct ipoib_cb *cb = ipoib_skb_cb(skb);
  563. struct ipoib_header *header;
  564. unsigned long flags;
  565. header = (struct ipoib_header *) skb->data;
  566. if (unlikely(cb->hwaddr[4] == 0xff)) {
  567. /* multicast, arrange "if" according to probability */
  568. if ((header->proto != htons(ETH_P_IP)) &&
  569. (header->proto != htons(ETH_P_IPV6)) &&
  570. (header->proto != htons(ETH_P_ARP)) &&
  571. (header->proto != htons(ETH_P_RARP)) &&
  572. (header->proto != htons(ETH_P_TIPC))) {
  573. /* ethertype not supported by IPoIB */
  574. ++dev->stats.tx_dropped;
  575. dev_kfree_skb_any(skb);
  576. return NETDEV_TX_OK;
  577. }
  578. /* Add in the P_Key for multicast*/
  579. cb->hwaddr[8] = (priv->pkey >> 8) & 0xff;
  580. cb->hwaddr[9] = priv->pkey & 0xff;
  581. neigh = ipoib_neigh_get(dev, cb->hwaddr);
  582. if (likely(neigh))
  583. goto send_using_neigh;
  584. ipoib_mcast_send(dev, cb->hwaddr, skb);
  585. return NETDEV_TX_OK;
  586. }
  587. /* unicast, arrange "switch" according to probability */
  588. switch (header->proto) {
  589. case htons(ETH_P_IP):
  590. case htons(ETH_P_IPV6):
  591. case htons(ETH_P_TIPC):
  592. neigh = ipoib_neigh_get(dev, cb->hwaddr);
  593. if (unlikely(!neigh)) {
  594. neigh_add_path(skb, cb->hwaddr, dev);
  595. return NETDEV_TX_OK;
  596. }
  597. break;
  598. case htons(ETH_P_ARP):
  599. case htons(ETH_P_RARP):
  600. /* for unicast ARP and RARP should always perform path find */
  601. unicast_arp_send(skb, dev, cb);
  602. return NETDEV_TX_OK;
  603. default:
  604. /* ethertype not supported by IPoIB */
  605. ++dev->stats.tx_dropped;
  606. dev_kfree_skb_any(skb);
  607. return NETDEV_TX_OK;
  608. }
  609. send_using_neigh:
  610. /* note we now hold a ref to neigh */
  611. if (ipoib_cm_get(neigh)) {
  612. if (ipoib_cm_up(neigh)) {
  613. ipoib_cm_send(dev, skb, ipoib_cm_get(neigh));
  614. goto unref;
  615. }
  616. } else if (neigh->ah) {
  617. ipoib_send(dev, skb, neigh->ah, IPOIB_QPN(cb->hwaddr));
  618. goto unref;
  619. }
  620. if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
  621. spin_lock_irqsave(&priv->lock, flags);
  622. __skb_queue_tail(&neigh->queue, skb);
  623. spin_unlock_irqrestore(&priv->lock, flags);
  624. } else {
  625. ++dev->stats.tx_dropped;
  626. dev_kfree_skb_any(skb);
  627. }
  628. unref:
  629. ipoib_neigh_put(neigh);
  630. return NETDEV_TX_OK;
  631. }
  632. static void ipoib_timeout(struct net_device *dev)
  633. {
  634. struct ipoib_dev_priv *priv = netdev_priv(dev);
  635. ipoib_warn(priv, "transmit timeout: latency %d msecs\n",
  636. jiffies_to_msecs(jiffies - dev->trans_start));
  637. ipoib_warn(priv, "queue stopped %d, tx_head %u, tx_tail %u\n",
  638. netif_queue_stopped(dev),
  639. priv->tx_head, priv->tx_tail);
  640. /* XXX reset QP, etc. */
  641. }
  642. static int ipoib_hard_header(struct sk_buff *skb,
  643. struct net_device *dev,
  644. unsigned short type,
  645. const void *daddr, const void *saddr, unsigned len)
  646. {
  647. struct ipoib_header *header;
  648. struct ipoib_cb *cb = ipoib_skb_cb(skb);
  649. header = (struct ipoib_header *) skb_push(skb, sizeof *header);
  650. header->proto = htons(type);
  651. header->reserved = 0;
  652. /*
  653. * we don't rely on dst_entry structure, always stuff the
  654. * destination address into skb->cb so we can figure out where
  655. * to send the packet later.
  656. */
  657. memcpy(cb->hwaddr, daddr, INFINIBAND_ALEN);
  658. return sizeof *header;
  659. }
  660. static void ipoib_set_mcast_list(struct net_device *dev)
  661. {
  662. struct ipoib_dev_priv *priv = netdev_priv(dev);
  663. if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
  664. ipoib_dbg(priv, "IPOIB_FLAG_OPER_UP not set");
  665. return;
  666. }
  667. queue_work(ipoib_workqueue, &priv->restart_task);
  668. }
  669. static int ipoib_get_iflink(const struct net_device *dev)
  670. {
  671. struct ipoib_dev_priv *priv = netdev_priv(dev);
  672. return priv->parent->ifindex;
  673. }
  674. static u32 ipoib_addr_hash(struct ipoib_neigh_hash *htbl, u8 *daddr)
  675. {
  676. /*
  677. * Use only the address parts that contributes to spreading
  678. * The subnet prefix is not used as one can not connect to
  679. * same remote port (GUID) using the same remote QPN via two
  680. * different subnets.
  681. */
  682. /* qpn octets[1:4) & port GUID octets[12:20) */
  683. u32 *d32 = (u32 *) daddr;
  684. u32 hv;
  685. hv = jhash_3words(d32[3], d32[4], IPOIB_QPN_MASK & d32[0], 0);
  686. return hv & htbl->mask;
  687. }
  688. struct ipoib_neigh *ipoib_neigh_get(struct net_device *dev, u8 *daddr)
  689. {
  690. struct ipoib_dev_priv *priv = netdev_priv(dev);
  691. struct ipoib_neigh_table *ntbl = &priv->ntbl;
  692. struct ipoib_neigh_hash *htbl;
  693. struct ipoib_neigh *neigh = NULL;
  694. u32 hash_val;
  695. rcu_read_lock_bh();
  696. htbl = rcu_dereference_bh(ntbl->htbl);
  697. if (!htbl)
  698. goto out_unlock;
  699. hash_val = ipoib_addr_hash(htbl, daddr);
  700. for (neigh = rcu_dereference_bh(htbl->buckets[hash_val]);
  701. neigh != NULL;
  702. neigh = rcu_dereference_bh(neigh->hnext)) {
  703. if (memcmp(daddr, neigh->daddr, INFINIBAND_ALEN) == 0) {
  704. /* found, take one ref on behalf of the caller */
  705. if (!atomic_inc_not_zero(&neigh->refcnt)) {
  706. /* deleted */
  707. neigh = NULL;
  708. goto out_unlock;
  709. }
  710. neigh->alive = jiffies;
  711. goto out_unlock;
  712. }
  713. }
  714. out_unlock:
  715. rcu_read_unlock_bh();
  716. return neigh;
  717. }
  718. static void __ipoib_reap_neigh(struct ipoib_dev_priv *priv)
  719. {
  720. struct ipoib_neigh_table *ntbl = &priv->ntbl;
  721. struct ipoib_neigh_hash *htbl;
  722. unsigned long neigh_obsolete;
  723. unsigned long dt;
  724. unsigned long flags;
  725. int i;
  726. if (test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
  727. return;
  728. spin_lock_irqsave(&priv->lock, flags);
  729. htbl = rcu_dereference_protected(ntbl->htbl,
  730. lockdep_is_held(&priv->lock));
  731. if (!htbl)
  732. goto out_unlock;
  733. /* neigh is obsolete if it was idle for two GC periods */
  734. dt = 2 * arp_tbl.gc_interval;
  735. neigh_obsolete = jiffies - dt;
  736. /* handle possible race condition */
  737. if (test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
  738. goto out_unlock;
  739. for (i = 0; i < htbl->size; i++) {
  740. struct ipoib_neigh *neigh;
  741. struct ipoib_neigh __rcu **np = &htbl->buckets[i];
  742. while ((neigh = rcu_dereference_protected(*np,
  743. lockdep_is_held(&priv->lock))) != NULL) {
  744. /* was the neigh idle for two GC periods */
  745. if (time_after(neigh_obsolete, neigh->alive)) {
  746. rcu_assign_pointer(*np,
  747. rcu_dereference_protected(neigh->hnext,
  748. lockdep_is_held(&priv->lock)));
  749. /* remove from path/mc list */
  750. list_del(&neigh->list);
  751. call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
  752. } else {
  753. np = &neigh->hnext;
  754. }
  755. }
  756. }
  757. out_unlock:
  758. spin_unlock_irqrestore(&priv->lock, flags);
  759. }
  760. static void ipoib_reap_neigh(struct work_struct *work)
  761. {
  762. struct ipoib_dev_priv *priv =
  763. container_of(work, struct ipoib_dev_priv, neigh_reap_task.work);
  764. __ipoib_reap_neigh(priv);
  765. if (!test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
  766. queue_delayed_work(ipoib_workqueue, &priv->neigh_reap_task,
  767. arp_tbl.gc_interval);
  768. }
  769. static struct ipoib_neigh *ipoib_neigh_ctor(u8 *daddr,
  770. struct net_device *dev)
  771. {
  772. struct ipoib_neigh *neigh;
  773. neigh = kzalloc(sizeof *neigh, GFP_ATOMIC);
  774. if (!neigh)
  775. return NULL;
  776. neigh->dev = dev;
  777. memcpy(&neigh->daddr, daddr, sizeof(neigh->daddr));
  778. skb_queue_head_init(&neigh->queue);
  779. INIT_LIST_HEAD(&neigh->list);
  780. ipoib_cm_set(neigh, NULL);
  781. /* one ref on behalf of the caller */
  782. atomic_set(&neigh->refcnt, 1);
  783. return neigh;
  784. }
  785. struct ipoib_neigh *ipoib_neigh_alloc(u8 *daddr,
  786. struct net_device *dev)
  787. {
  788. struct ipoib_dev_priv *priv = netdev_priv(dev);
  789. struct ipoib_neigh_table *ntbl = &priv->ntbl;
  790. struct ipoib_neigh_hash *htbl;
  791. struct ipoib_neigh *neigh;
  792. u32 hash_val;
  793. htbl = rcu_dereference_protected(ntbl->htbl,
  794. lockdep_is_held(&priv->lock));
  795. if (!htbl) {
  796. neigh = NULL;
  797. goto out_unlock;
  798. }
  799. /* need to add a new neigh, but maybe some other thread succeeded?
  800. * recalc hash, maybe hash resize took place so we do a search
  801. */
  802. hash_val = ipoib_addr_hash(htbl, daddr);
  803. for (neigh = rcu_dereference_protected(htbl->buckets[hash_val],
  804. lockdep_is_held(&priv->lock));
  805. neigh != NULL;
  806. neigh = rcu_dereference_protected(neigh->hnext,
  807. lockdep_is_held(&priv->lock))) {
  808. if (memcmp(daddr, neigh->daddr, INFINIBAND_ALEN) == 0) {
  809. /* found, take one ref on behalf of the caller */
  810. if (!atomic_inc_not_zero(&neigh->refcnt)) {
  811. /* deleted */
  812. neigh = NULL;
  813. break;
  814. }
  815. neigh->alive = jiffies;
  816. goto out_unlock;
  817. }
  818. }
  819. neigh = ipoib_neigh_ctor(daddr, dev);
  820. if (!neigh)
  821. goto out_unlock;
  822. /* one ref on behalf of the hash table */
  823. atomic_inc(&neigh->refcnt);
  824. neigh->alive = jiffies;
  825. /* put in hash */
  826. rcu_assign_pointer(neigh->hnext,
  827. rcu_dereference_protected(htbl->buckets[hash_val],
  828. lockdep_is_held(&priv->lock)));
  829. rcu_assign_pointer(htbl->buckets[hash_val], neigh);
  830. atomic_inc(&ntbl->entries);
  831. out_unlock:
  832. return neigh;
  833. }
  834. void ipoib_neigh_dtor(struct ipoib_neigh *neigh)
  835. {
  836. /* neigh reference count was dropprd to zero */
  837. struct net_device *dev = neigh->dev;
  838. struct ipoib_dev_priv *priv = netdev_priv(dev);
  839. struct sk_buff *skb;
  840. if (neigh->ah)
  841. ipoib_put_ah(neigh->ah);
  842. while ((skb = __skb_dequeue(&neigh->queue))) {
  843. ++dev->stats.tx_dropped;
  844. dev_kfree_skb_any(skb);
  845. }
  846. if (ipoib_cm_get(neigh))
  847. ipoib_cm_destroy_tx(ipoib_cm_get(neigh));
  848. ipoib_dbg(netdev_priv(dev),
  849. "neigh free for %06x %pI6\n",
  850. IPOIB_QPN(neigh->daddr),
  851. neigh->daddr + 4);
  852. kfree(neigh);
  853. if (atomic_dec_and_test(&priv->ntbl.entries)) {
  854. if (test_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags))
  855. complete(&priv->ntbl.flushed);
  856. }
  857. }
  858. static void ipoib_neigh_reclaim(struct rcu_head *rp)
  859. {
  860. /* Called as a result of removal from hash table */
  861. struct ipoib_neigh *neigh = container_of(rp, struct ipoib_neigh, rcu);
  862. /* note TX context may hold another ref */
  863. ipoib_neigh_put(neigh);
  864. }
  865. void ipoib_neigh_free(struct ipoib_neigh *neigh)
  866. {
  867. struct net_device *dev = neigh->dev;
  868. struct ipoib_dev_priv *priv = netdev_priv(dev);
  869. struct ipoib_neigh_table *ntbl = &priv->ntbl;
  870. struct ipoib_neigh_hash *htbl;
  871. struct ipoib_neigh __rcu **np;
  872. struct ipoib_neigh *n;
  873. u32 hash_val;
  874. htbl = rcu_dereference_protected(ntbl->htbl,
  875. lockdep_is_held(&priv->lock));
  876. if (!htbl)
  877. return;
  878. hash_val = ipoib_addr_hash(htbl, neigh->daddr);
  879. np = &htbl->buckets[hash_val];
  880. for (n = rcu_dereference_protected(*np,
  881. lockdep_is_held(&priv->lock));
  882. n != NULL;
  883. n = rcu_dereference_protected(*np,
  884. lockdep_is_held(&priv->lock))) {
  885. if (n == neigh) {
  886. /* found */
  887. rcu_assign_pointer(*np,
  888. rcu_dereference_protected(neigh->hnext,
  889. lockdep_is_held(&priv->lock)));
  890. /* remove from parent list */
  891. list_del(&neigh->list);
  892. call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
  893. return;
  894. } else {
  895. np = &n->hnext;
  896. }
  897. }
  898. }
  899. static int ipoib_neigh_hash_init(struct ipoib_dev_priv *priv)
  900. {
  901. struct ipoib_neigh_table *ntbl = &priv->ntbl;
  902. struct ipoib_neigh_hash *htbl;
  903. struct ipoib_neigh **buckets;
  904. u32 size;
  905. clear_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags);
  906. ntbl->htbl = NULL;
  907. htbl = kzalloc(sizeof(*htbl), GFP_KERNEL);
  908. if (!htbl)
  909. return -ENOMEM;
  910. set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
  911. size = roundup_pow_of_two(arp_tbl.gc_thresh3);
  912. buckets = kzalloc(size * sizeof(*buckets), GFP_KERNEL);
  913. if (!buckets) {
  914. kfree(htbl);
  915. return -ENOMEM;
  916. }
  917. htbl->size = size;
  918. htbl->mask = (size - 1);
  919. htbl->buckets = buckets;
  920. ntbl->htbl = htbl;
  921. htbl->ntbl = ntbl;
  922. atomic_set(&ntbl->entries, 0);
  923. /* start garbage collection */
  924. clear_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
  925. queue_delayed_work(ipoib_workqueue, &priv->neigh_reap_task,
  926. arp_tbl.gc_interval);
  927. return 0;
  928. }
  929. static void neigh_hash_free_rcu(struct rcu_head *head)
  930. {
  931. struct ipoib_neigh_hash *htbl = container_of(head,
  932. struct ipoib_neigh_hash,
  933. rcu);
  934. struct ipoib_neigh __rcu **buckets = htbl->buckets;
  935. struct ipoib_neigh_table *ntbl = htbl->ntbl;
  936. kfree(buckets);
  937. kfree(htbl);
  938. complete(&ntbl->deleted);
  939. }
  940. void ipoib_del_neighs_by_gid(struct net_device *dev, u8 *gid)
  941. {
  942. struct ipoib_dev_priv *priv = netdev_priv(dev);
  943. struct ipoib_neigh_table *ntbl = &priv->ntbl;
  944. struct ipoib_neigh_hash *htbl;
  945. unsigned long flags;
  946. int i;
  947. /* remove all neigh connected to a given path or mcast */
  948. spin_lock_irqsave(&priv->lock, flags);
  949. htbl = rcu_dereference_protected(ntbl->htbl,
  950. lockdep_is_held(&priv->lock));
  951. if (!htbl)
  952. goto out_unlock;
  953. for (i = 0; i < htbl->size; i++) {
  954. struct ipoib_neigh *neigh;
  955. struct ipoib_neigh __rcu **np = &htbl->buckets[i];
  956. while ((neigh = rcu_dereference_protected(*np,
  957. lockdep_is_held(&priv->lock))) != NULL) {
  958. /* delete neighs belong to this parent */
  959. if (!memcmp(gid, neigh->daddr + 4, sizeof (union ib_gid))) {
  960. rcu_assign_pointer(*np,
  961. rcu_dereference_protected(neigh->hnext,
  962. lockdep_is_held(&priv->lock)));
  963. /* remove from parent list */
  964. list_del(&neigh->list);
  965. call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
  966. } else {
  967. np = &neigh->hnext;
  968. }
  969. }
  970. }
  971. out_unlock:
  972. spin_unlock_irqrestore(&priv->lock, flags);
  973. }
  974. static void ipoib_flush_neighs(struct ipoib_dev_priv *priv)
  975. {
  976. struct ipoib_neigh_table *ntbl = &priv->ntbl;
  977. struct ipoib_neigh_hash *htbl;
  978. unsigned long flags;
  979. int i, wait_flushed = 0;
  980. init_completion(&priv->ntbl.flushed);
  981. spin_lock_irqsave(&priv->lock, flags);
  982. htbl = rcu_dereference_protected(ntbl->htbl,
  983. lockdep_is_held(&priv->lock));
  984. if (!htbl)
  985. goto out_unlock;
  986. wait_flushed = atomic_read(&priv->ntbl.entries);
  987. if (!wait_flushed)
  988. goto free_htbl;
  989. for (i = 0; i < htbl->size; i++) {
  990. struct ipoib_neigh *neigh;
  991. struct ipoib_neigh __rcu **np = &htbl->buckets[i];
  992. while ((neigh = rcu_dereference_protected(*np,
  993. lockdep_is_held(&priv->lock))) != NULL) {
  994. rcu_assign_pointer(*np,
  995. rcu_dereference_protected(neigh->hnext,
  996. lockdep_is_held(&priv->lock)));
  997. /* remove from path/mc list */
  998. list_del(&neigh->list);
  999. call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
  1000. }
  1001. }
  1002. free_htbl:
  1003. rcu_assign_pointer(ntbl->htbl, NULL);
  1004. call_rcu(&htbl->rcu, neigh_hash_free_rcu);
  1005. out_unlock:
  1006. spin_unlock_irqrestore(&priv->lock, flags);
  1007. if (wait_flushed)
  1008. wait_for_completion(&priv->ntbl.flushed);
  1009. }
  1010. static void ipoib_neigh_hash_uninit(struct net_device *dev)
  1011. {
  1012. struct ipoib_dev_priv *priv = netdev_priv(dev);
  1013. int stopped;
  1014. ipoib_dbg(priv, "ipoib_neigh_hash_uninit\n");
  1015. init_completion(&priv->ntbl.deleted);
  1016. set_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags);
  1017. /* Stop GC if called at init fail need to cancel work */
  1018. stopped = test_and_set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
  1019. if (!stopped)
  1020. cancel_delayed_work(&priv->neigh_reap_task);
  1021. ipoib_flush_neighs(priv);
  1022. wait_for_completion(&priv->ntbl.deleted);
  1023. }
  1024. int ipoib_dev_init(struct net_device *dev, struct ib_device *ca, int port)
  1025. {
  1026. struct ipoib_dev_priv *priv = netdev_priv(dev);
  1027. if (ipoib_neigh_hash_init(priv) < 0)
  1028. goto out;
  1029. /* Allocate RX/TX "rings" to hold queued skbs */
  1030. priv->rx_ring = kzalloc(ipoib_recvq_size * sizeof *priv->rx_ring,
  1031. GFP_KERNEL);
  1032. if (!priv->rx_ring) {
  1033. printk(KERN_WARNING "%s: failed to allocate RX ring (%d entries)\n",
  1034. ca->name, ipoib_recvq_size);
  1035. goto out_neigh_hash_cleanup;
  1036. }
  1037. priv->tx_ring = vzalloc(ipoib_sendq_size * sizeof *priv->tx_ring);
  1038. if (!priv->tx_ring) {
  1039. printk(KERN_WARNING "%s: failed to allocate TX ring (%d entries)\n",
  1040. ca->name, ipoib_sendq_size);
  1041. goto out_rx_ring_cleanup;
  1042. }
  1043. /* priv->tx_head, tx_tail & tx_outstanding are already 0 */
  1044. if (ipoib_ib_dev_init(dev, ca, port))
  1045. goto out_tx_ring_cleanup;
  1046. return 0;
  1047. out_tx_ring_cleanup:
  1048. vfree(priv->tx_ring);
  1049. out_rx_ring_cleanup:
  1050. kfree(priv->rx_ring);
  1051. out_neigh_hash_cleanup:
  1052. ipoib_neigh_hash_uninit(dev);
  1053. out:
  1054. return -ENOMEM;
  1055. }
  1056. void ipoib_dev_cleanup(struct net_device *dev)
  1057. {
  1058. struct ipoib_dev_priv *priv = netdev_priv(dev), *cpriv, *tcpriv;
  1059. LIST_HEAD(head);
  1060. ASSERT_RTNL();
  1061. ipoib_delete_debug_files(dev);
  1062. /* Delete any child interfaces first */
  1063. list_for_each_entry_safe(cpriv, tcpriv, &priv->child_intfs, list) {
  1064. /* Stop GC on child */
  1065. set_bit(IPOIB_STOP_NEIGH_GC, &cpriv->flags);
  1066. cancel_delayed_work(&cpriv->neigh_reap_task);
  1067. unregister_netdevice_queue(cpriv->dev, &head);
  1068. }
  1069. unregister_netdevice_many(&head);
  1070. ipoib_ib_dev_cleanup(dev);
  1071. kfree(priv->rx_ring);
  1072. vfree(priv->tx_ring);
  1073. priv->rx_ring = NULL;
  1074. priv->tx_ring = NULL;
  1075. ipoib_neigh_hash_uninit(dev);
  1076. }
  1077. static const struct header_ops ipoib_header_ops = {
  1078. .create = ipoib_hard_header,
  1079. };
  1080. static const struct net_device_ops ipoib_netdev_ops = {
  1081. .ndo_uninit = ipoib_uninit,
  1082. .ndo_open = ipoib_open,
  1083. .ndo_stop = ipoib_stop,
  1084. .ndo_change_mtu = ipoib_change_mtu,
  1085. .ndo_fix_features = ipoib_fix_features,
  1086. .ndo_start_xmit = ipoib_start_xmit,
  1087. .ndo_tx_timeout = ipoib_timeout,
  1088. .ndo_set_rx_mode = ipoib_set_mcast_list,
  1089. .ndo_get_iflink = ipoib_get_iflink,
  1090. };
  1091. void ipoib_setup(struct net_device *dev)
  1092. {
  1093. struct ipoib_dev_priv *priv = netdev_priv(dev);
  1094. dev->netdev_ops = &ipoib_netdev_ops;
  1095. dev->header_ops = &ipoib_header_ops;
  1096. ipoib_set_ethtool_ops(dev);
  1097. netif_napi_add(dev, &priv->napi, ipoib_poll, NAPI_POLL_WEIGHT);
  1098. dev->watchdog_timeo = HZ;
  1099. dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
  1100. dev->hard_header_len = IPOIB_ENCAP_LEN;
  1101. dev->addr_len = INFINIBAND_ALEN;
  1102. dev->type = ARPHRD_INFINIBAND;
  1103. dev->tx_queue_len = ipoib_sendq_size * 2;
  1104. dev->features = (NETIF_F_VLAN_CHALLENGED |
  1105. NETIF_F_HIGHDMA);
  1106. netif_keep_dst(dev);
  1107. memcpy(dev->broadcast, ipv4_bcast_addr, INFINIBAND_ALEN);
  1108. priv->dev = dev;
  1109. spin_lock_init(&priv->lock);
  1110. init_rwsem(&priv->vlan_rwsem);
  1111. INIT_LIST_HEAD(&priv->path_list);
  1112. INIT_LIST_HEAD(&priv->child_intfs);
  1113. INIT_LIST_HEAD(&priv->dead_ahs);
  1114. INIT_LIST_HEAD(&priv->multicast_list);
  1115. INIT_DELAYED_WORK(&priv->mcast_task, ipoib_mcast_join_task);
  1116. INIT_WORK(&priv->carrier_on_task, ipoib_mcast_carrier_on_task);
  1117. INIT_WORK(&priv->flush_light, ipoib_ib_dev_flush_light);
  1118. INIT_WORK(&priv->flush_normal, ipoib_ib_dev_flush_normal);
  1119. INIT_WORK(&priv->flush_heavy, ipoib_ib_dev_flush_heavy);
  1120. INIT_WORK(&priv->restart_task, ipoib_mcast_restart_task);
  1121. INIT_DELAYED_WORK(&priv->ah_reap_task, ipoib_reap_ah);
  1122. INIT_DELAYED_WORK(&priv->neigh_reap_task, ipoib_reap_neigh);
  1123. }
  1124. struct ipoib_dev_priv *ipoib_intf_alloc(const char *name)
  1125. {
  1126. struct net_device *dev;
  1127. dev = alloc_netdev((int)sizeof(struct ipoib_dev_priv), name,
  1128. NET_NAME_UNKNOWN, ipoib_setup);
  1129. if (!dev)
  1130. return NULL;
  1131. return netdev_priv(dev);
  1132. }
  1133. static ssize_t show_pkey(struct device *dev,
  1134. struct device_attribute *attr, char *buf)
  1135. {
  1136. struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(dev));
  1137. return sprintf(buf, "0x%04x\n", priv->pkey);
  1138. }
  1139. static DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
  1140. static ssize_t show_umcast(struct device *dev,
  1141. struct device_attribute *attr, char *buf)
  1142. {
  1143. struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(dev));
  1144. return sprintf(buf, "%d\n", test_bit(IPOIB_FLAG_UMCAST, &priv->flags));
  1145. }
  1146. void ipoib_set_umcast(struct net_device *ndev, int umcast_val)
  1147. {
  1148. struct ipoib_dev_priv *priv = netdev_priv(ndev);
  1149. if (umcast_val > 0) {
  1150. set_bit(IPOIB_FLAG_UMCAST, &priv->flags);
  1151. ipoib_warn(priv, "ignoring multicast groups joined directly "
  1152. "by userspace\n");
  1153. } else
  1154. clear_bit(IPOIB_FLAG_UMCAST, &priv->flags);
  1155. }
  1156. static ssize_t set_umcast(struct device *dev,
  1157. struct device_attribute *attr,
  1158. const char *buf, size_t count)
  1159. {
  1160. unsigned long umcast_val = simple_strtoul(buf, NULL, 0);
  1161. ipoib_set_umcast(to_net_dev(dev), umcast_val);
  1162. return count;
  1163. }
  1164. static DEVICE_ATTR(umcast, S_IWUSR | S_IRUGO, show_umcast, set_umcast);
  1165. int ipoib_add_umcast_attr(struct net_device *dev)
  1166. {
  1167. return device_create_file(&dev->dev, &dev_attr_umcast);
  1168. }
  1169. static ssize_t create_child(struct device *dev,
  1170. struct device_attribute *attr,
  1171. const char *buf, size_t count)
  1172. {
  1173. int pkey;
  1174. int ret;
  1175. if (sscanf(buf, "%i", &pkey) != 1)
  1176. return -EINVAL;
  1177. if (pkey <= 0 || pkey > 0xffff || pkey == 0x8000)
  1178. return -EINVAL;
  1179. /*
  1180. * Set the full membership bit, so that we join the right
  1181. * broadcast group, etc.
  1182. */
  1183. pkey |= 0x8000;
  1184. ret = ipoib_vlan_add(to_net_dev(dev), pkey);
  1185. return ret ? ret : count;
  1186. }
  1187. static DEVICE_ATTR(create_child, S_IWUSR, NULL, create_child);
  1188. static ssize_t delete_child(struct device *dev,
  1189. struct device_attribute *attr,
  1190. const char *buf, size_t count)
  1191. {
  1192. int pkey;
  1193. int ret;
  1194. if (sscanf(buf, "%i", &pkey) != 1)
  1195. return -EINVAL;
  1196. if (pkey < 0 || pkey > 0xffff)
  1197. return -EINVAL;
  1198. ret = ipoib_vlan_delete(to_net_dev(dev), pkey);
  1199. return ret ? ret : count;
  1200. }
  1201. static DEVICE_ATTR(delete_child, S_IWUSR, NULL, delete_child);
  1202. int ipoib_add_pkey_attr(struct net_device *dev)
  1203. {
  1204. return device_create_file(&dev->dev, &dev_attr_pkey);
  1205. }
  1206. int ipoib_set_dev_features(struct ipoib_dev_priv *priv, struct ib_device *hca)
  1207. {
  1208. struct ib_device_attr *device_attr;
  1209. int result = -ENOMEM;
  1210. device_attr = kmalloc(sizeof *device_attr, GFP_KERNEL);
  1211. if (!device_attr) {
  1212. printk(KERN_WARNING "%s: allocation of %zu bytes failed\n",
  1213. hca->name, sizeof *device_attr);
  1214. return result;
  1215. }
  1216. result = ib_query_device(hca, device_attr);
  1217. if (result) {
  1218. printk(KERN_WARNING "%s: ib_query_device failed (ret = %d)\n",
  1219. hca->name, result);
  1220. kfree(device_attr);
  1221. return result;
  1222. }
  1223. priv->hca_caps = device_attr->device_cap_flags;
  1224. kfree(device_attr);
  1225. if (priv->hca_caps & IB_DEVICE_UD_IP_CSUM) {
  1226. priv->dev->hw_features = NETIF_F_SG |
  1227. NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
  1228. if (priv->hca_caps & IB_DEVICE_UD_TSO)
  1229. priv->dev->hw_features |= NETIF_F_TSO;
  1230. priv->dev->features |= priv->dev->hw_features;
  1231. }
  1232. return 0;
  1233. }
  1234. static struct net_device *ipoib_add_port(const char *format,
  1235. struct ib_device *hca, u8 port)
  1236. {
  1237. struct ipoib_dev_priv *priv;
  1238. struct ib_port_attr attr;
  1239. int result = -ENOMEM;
  1240. priv = ipoib_intf_alloc(format);
  1241. if (!priv)
  1242. goto alloc_mem_failed;
  1243. SET_NETDEV_DEV(priv->dev, hca->dma_device);
  1244. priv->dev->dev_id = port - 1;
  1245. if (!ib_query_port(hca, port, &attr))
  1246. priv->max_ib_mtu = ib_mtu_enum_to_int(attr.max_mtu);
  1247. else {
  1248. printk(KERN_WARNING "%s: ib_query_port %d failed\n",
  1249. hca->name, port);
  1250. goto device_init_failed;
  1251. }
  1252. /* MTU will be reset when mcast join happens */
  1253. priv->dev->mtu = IPOIB_UD_MTU(priv->max_ib_mtu);
  1254. priv->mcast_mtu = priv->admin_mtu = priv->dev->mtu;
  1255. priv->dev->neigh_priv_len = sizeof(struct ipoib_neigh);
  1256. result = ib_query_pkey(hca, port, 0, &priv->pkey);
  1257. if (result) {
  1258. printk(KERN_WARNING "%s: ib_query_pkey port %d failed (ret = %d)\n",
  1259. hca->name, port, result);
  1260. goto device_init_failed;
  1261. }
  1262. if (ipoib_set_dev_features(priv, hca))
  1263. goto device_init_failed;
  1264. /*
  1265. * Set the full membership bit, so that we join the right
  1266. * broadcast group, etc.
  1267. */
  1268. priv->pkey |= 0x8000;
  1269. priv->dev->broadcast[8] = priv->pkey >> 8;
  1270. priv->dev->broadcast[9] = priv->pkey & 0xff;
  1271. result = ib_query_gid(hca, port, 0, &priv->local_gid);
  1272. if (result) {
  1273. printk(KERN_WARNING "%s: ib_query_gid port %d failed (ret = %d)\n",
  1274. hca->name, port, result);
  1275. goto device_init_failed;
  1276. } else
  1277. memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));
  1278. result = ipoib_dev_init(priv->dev, hca, port);
  1279. if (result < 0) {
  1280. printk(KERN_WARNING "%s: failed to initialize port %d (ret = %d)\n",
  1281. hca->name, port, result);
  1282. goto device_init_failed;
  1283. }
  1284. INIT_IB_EVENT_HANDLER(&priv->event_handler,
  1285. priv->ca, ipoib_event);
  1286. result = ib_register_event_handler(&priv->event_handler);
  1287. if (result < 0) {
  1288. printk(KERN_WARNING "%s: ib_register_event_handler failed for "
  1289. "port %d (ret = %d)\n",
  1290. hca->name, port, result);
  1291. goto event_failed;
  1292. }
  1293. result = register_netdev(priv->dev);
  1294. if (result) {
  1295. printk(KERN_WARNING "%s: couldn't register ipoib port %d; error %d\n",
  1296. hca->name, port, result);
  1297. goto register_failed;
  1298. }
  1299. ipoib_create_debug_files(priv->dev);
  1300. if (ipoib_cm_add_mode_attr(priv->dev))
  1301. goto sysfs_failed;
  1302. if (ipoib_add_pkey_attr(priv->dev))
  1303. goto sysfs_failed;
  1304. if (ipoib_add_umcast_attr(priv->dev))
  1305. goto sysfs_failed;
  1306. if (device_create_file(&priv->dev->dev, &dev_attr_create_child))
  1307. goto sysfs_failed;
  1308. if (device_create_file(&priv->dev->dev, &dev_attr_delete_child))
  1309. goto sysfs_failed;
  1310. return priv->dev;
  1311. sysfs_failed:
  1312. ipoib_delete_debug_files(priv->dev);
  1313. unregister_netdev(priv->dev);
  1314. register_failed:
  1315. ib_unregister_event_handler(&priv->event_handler);
  1316. /* Stop GC if started before flush */
  1317. set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
  1318. cancel_delayed_work(&priv->neigh_reap_task);
  1319. flush_workqueue(ipoib_workqueue);
  1320. event_failed:
  1321. ipoib_dev_cleanup(priv->dev);
  1322. device_init_failed:
  1323. free_netdev(priv->dev);
  1324. alloc_mem_failed:
  1325. return ERR_PTR(result);
  1326. }
  1327. static void ipoib_add_one(struct ib_device *device)
  1328. {
  1329. struct list_head *dev_list;
  1330. struct net_device *dev;
  1331. struct ipoib_dev_priv *priv;
  1332. int s, e, p;
  1333. if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
  1334. return;
  1335. dev_list = kmalloc(sizeof *dev_list, GFP_KERNEL);
  1336. if (!dev_list)
  1337. return;
  1338. INIT_LIST_HEAD(dev_list);
  1339. if (device->node_type == RDMA_NODE_IB_SWITCH) {
  1340. s = 0;
  1341. e = 0;
  1342. } else {
  1343. s = 1;
  1344. e = device->phys_port_cnt;
  1345. }
  1346. for (p = s; p <= e; ++p) {
  1347. if (rdma_port_get_link_layer(device, p) != IB_LINK_LAYER_INFINIBAND)
  1348. continue;
  1349. dev = ipoib_add_port("ib%d", device, p);
  1350. if (!IS_ERR(dev)) {
  1351. priv = netdev_priv(dev);
  1352. list_add_tail(&priv->list, dev_list);
  1353. }
  1354. }
  1355. ib_set_client_data(device, &ipoib_client, dev_list);
  1356. }
  1357. static void ipoib_remove_one(struct ib_device *device)
  1358. {
  1359. struct ipoib_dev_priv *priv, *tmp;
  1360. struct list_head *dev_list;
  1361. if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
  1362. return;
  1363. dev_list = ib_get_client_data(device, &ipoib_client);
  1364. if (!dev_list)
  1365. return;
  1366. list_for_each_entry_safe(priv, tmp, dev_list, list) {
  1367. ib_unregister_event_handler(&priv->event_handler);
  1368. rtnl_lock();
  1369. dev_change_flags(priv->dev, priv->dev->flags & ~IFF_UP);
  1370. rtnl_unlock();
  1371. /* Stop GC */
  1372. set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
  1373. cancel_delayed_work(&priv->neigh_reap_task);
  1374. flush_workqueue(ipoib_workqueue);
  1375. unregister_netdev(priv->dev);
  1376. free_netdev(priv->dev);
  1377. }
  1378. kfree(dev_list);
  1379. }
  1380. static int __init ipoib_init_module(void)
  1381. {
  1382. int ret;
  1383. ipoib_recvq_size = roundup_pow_of_two(ipoib_recvq_size);
  1384. ipoib_recvq_size = min(ipoib_recvq_size, IPOIB_MAX_QUEUE_SIZE);
  1385. ipoib_recvq_size = max(ipoib_recvq_size, IPOIB_MIN_QUEUE_SIZE);
  1386. ipoib_sendq_size = roundup_pow_of_two(ipoib_sendq_size);
  1387. ipoib_sendq_size = min(ipoib_sendq_size, IPOIB_MAX_QUEUE_SIZE);
  1388. ipoib_sendq_size = max3(ipoib_sendq_size, 2 * MAX_SEND_CQE, IPOIB_MIN_QUEUE_SIZE);
  1389. #ifdef CONFIG_INFINIBAND_IPOIB_CM
  1390. ipoib_max_conn_qp = min(ipoib_max_conn_qp, IPOIB_CM_MAX_CONN_QP);
  1391. #endif
  1392. /*
  1393. * When copying small received packets, we only copy from the
  1394. * linear data part of the SKB, so we rely on this condition.
  1395. */
  1396. BUILD_BUG_ON(IPOIB_CM_COPYBREAK > IPOIB_CM_HEAD_SIZE);
  1397. ret = ipoib_register_debugfs();
  1398. if (ret)
  1399. return ret;
  1400. /*
  1401. * We create our own workqueue mainly because we want to be
  1402. * able to flush it when devices are being removed. We can't
  1403. * use schedule_work()/flush_scheduled_work() because both
  1404. * unregister_netdev() and linkwatch_event take the rtnl lock,
  1405. * so flush_scheduled_work() can deadlock during device
  1406. * removal.
  1407. */
  1408. ipoib_workqueue = create_singlethread_workqueue("ipoib");
  1409. if (!ipoib_workqueue) {
  1410. ret = -ENOMEM;
  1411. goto err_fs;
  1412. }
  1413. ib_sa_register_client(&ipoib_sa_client);
  1414. ret = ib_register_client(&ipoib_client);
  1415. if (ret)
  1416. goto err_sa;
  1417. ret = ipoib_netlink_init();
  1418. if (ret)
  1419. goto err_client;
  1420. return 0;
  1421. err_client:
  1422. ib_unregister_client(&ipoib_client);
  1423. err_sa:
  1424. ib_sa_unregister_client(&ipoib_sa_client);
  1425. destroy_workqueue(ipoib_workqueue);
  1426. err_fs:
  1427. ipoib_unregister_debugfs();
  1428. return ret;
  1429. }
  1430. static void __exit ipoib_cleanup_module(void)
  1431. {
  1432. ipoib_netlink_fini();
  1433. ib_unregister_client(&ipoib_client);
  1434. ib_sa_unregister_client(&ipoib_sa_client);
  1435. ipoib_unregister_debugfs();
  1436. destroy_workqueue(ipoib_workqueue);
  1437. }
  1438. module_init(ipoib_init_module);
  1439. module_exit(ipoib_cleanup_module);