選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

neighbour.c 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /*
  2. * Copyright (C) 2013 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <stdint.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <errno.h>
  28. #include <ipxe/iobuf.h>
  29. #include <ipxe/retry.h>
  30. #include <ipxe/timer.h>
  31. #include <ipxe/malloc.h>
  32. #include <ipxe/neighbour.h>
  33. /** @file
  34. *
  35. * Neighbour discovery
  36. *
  37. * This file implements the abstract functions of neighbour discovery,
  38. * independent of the underlying network protocol (e.g. ARP or NDP).
  39. *
  40. */
  41. /** Neighbour discovery minimum timeout */
  42. #define NEIGHBOUR_MIN_TIMEOUT ( TICKS_PER_SEC / 8 )
  43. /** Neighbour discovery maximum timeout */
  44. #define NEIGHBOUR_MAX_TIMEOUT ( TICKS_PER_SEC * 3 )
  45. /** The neighbour cache */
  46. struct list_head neighbours = LIST_HEAD_INIT ( neighbours );
  47. static void neighbour_expired ( struct retry_timer *timer, int over );
  48. /**
  49. * Free neighbour cache entry
  50. *
  51. * @v refcnt Reference count
  52. */
  53. static void neighbour_free ( struct refcnt *refcnt ) {
  54. struct neighbour *neighbour =
  55. container_of ( refcnt, struct neighbour, refcnt );
  56. /* Sanity check */
  57. assert ( list_empty ( &neighbour->tx_queue ) );
  58. /* Drop reference to network device */
  59. netdev_put ( neighbour->netdev );
  60. /* Free neighbour */
  61. free ( neighbour );
  62. }
  63. /**
  64. * Create neighbour cache entry
  65. *
  66. * @v netdev Network device
  67. * @v net_protocol Network-layer protocol
  68. * @v net_dest Destination network-layer address
  69. * @ret neighbour Neighbour cache entry, or NULL if allocation failed
  70. */
  71. static struct neighbour * neighbour_create ( struct net_device *netdev,
  72. struct net_protocol *net_protocol,
  73. const void *net_dest ) {
  74. struct neighbour *neighbour;
  75. /* Allocate and initialise entry */
  76. neighbour = zalloc ( sizeof ( *neighbour ) );
  77. if ( ! neighbour )
  78. return NULL;
  79. ref_init ( &neighbour->refcnt, neighbour_free );
  80. neighbour->netdev = netdev_get ( netdev );
  81. neighbour->net_protocol = net_protocol;
  82. memcpy ( neighbour->net_dest, net_dest,
  83. net_protocol->net_addr_len );
  84. timer_init ( &neighbour->timer, neighbour_expired, &neighbour->refcnt );
  85. set_timer_limits ( &neighbour->timer, NEIGHBOUR_MIN_TIMEOUT,
  86. NEIGHBOUR_MAX_TIMEOUT );
  87. INIT_LIST_HEAD ( &neighbour->tx_queue );
  88. /* Transfer ownership to cache */
  89. list_add ( &neighbour->list, &neighbours );
  90. DBGC ( neighbour, "NEIGHBOUR %s %s %s created\n", netdev->name,
  91. net_protocol->name, net_protocol->ntoa ( net_dest ) );
  92. return neighbour;
  93. }
  94. /**
  95. * Find neighbour cache entry
  96. *
  97. * @v netdev Network device
  98. * @v net_protocol Network-layer protocol
  99. * @v net_dest Destination network-layer address
  100. * @ret neighbour Neighbour cache entry, or NULL if not found
  101. */
  102. static struct neighbour * neighbour_find ( struct net_device *netdev,
  103. struct net_protocol *net_protocol,
  104. const void *net_dest ) {
  105. struct neighbour *neighbour;
  106. list_for_each_entry ( neighbour, &neighbours, list ) {
  107. if ( ( neighbour->netdev == netdev ) &&
  108. ( neighbour->net_protocol == net_protocol ) &&
  109. ( memcmp ( neighbour->net_dest, net_dest,
  110. net_protocol->net_addr_len ) == 0 ) ) {
  111. /* Move to start of cache */
  112. list_del ( &neighbour->list );
  113. list_add ( &neighbour->list, &neighbours );
  114. return neighbour;
  115. }
  116. }
  117. return NULL;
  118. }
  119. /**
  120. * Start neighbour discovery
  121. *
  122. * @v neighbour Neighbour cache entry
  123. * @v discovery Neighbour discovery protocol
  124. * @v net_source Source network-layer address
  125. */
  126. static void neighbour_discover ( struct neighbour *neighbour,
  127. struct neighbour_discovery *discovery,
  128. const void *net_source ) {
  129. struct net_device *netdev = neighbour->netdev;
  130. struct net_protocol *net_protocol = neighbour->net_protocol;
  131. /* Record discovery protocol and source network-layer address */
  132. neighbour->discovery = discovery;
  133. memcpy ( neighbour->net_source, net_source,
  134. net_protocol->net_addr_len );
  135. /* Start timer to trigger neighbour discovery */
  136. start_timer_nodelay ( &neighbour->timer );
  137. DBGC ( neighbour, "NEIGHBOUR %s %s %s discovering via %s\n",
  138. netdev->name, net_protocol->name,
  139. net_protocol->ntoa ( neighbour->net_dest ),
  140. neighbour->discovery->name );
  141. }
  142. /**
  143. * Complete neighbour discovery
  144. *
  145. * @v neighbour Neighbour cache entry
  146. * @v ll_dest Destination link-layer address
  147. */
  148. static void neighbour_discovered ( struct neighbour *neighbour,
  149. const void *ll_dest ) {
  150. struct net_device *netdev = neighbour->netdev;
  151. struct ll_protocol *ll_protocol = netdev->ll_protocol;
  152. struct net_protocol *net_protocol = neighbour->net_protocol;
  153. struct io_buffer *iobuf;
  154. int rc;
  155. /* Fill in link-layer address */
  156. memcpy ( neighbour->ll_dest, ll_dest, ll_protocol->ll_addr_len );
  157. DBGC ( neighbour, "NEIGHBOUR %s %s %s is %s %s\n", netdev->name,
  158. net_protocol->name, net_protocol->ntoa ( neighbour->net_dest ),
  159. ll_protocol->name, ll_protocol->ntoa ( neighbour->ll_dest ) );
  160. /* Stop retransmission timer */
  161. stop_timer ( &neighbour->timer );
  162. /* Transmit any packets in queue. Take out a temporary
  163. * reference on the entry to prevent it from going out of
  164. * scope during the call to net_tx().
  165. */
  166. ref_get ( &neighbour->refcnt );
  167. while ( ( iobuf = list_first_entry ( &neighbour->tx_queue,
  168. struct io_buffer, list )) != NULL){
  169. DBGC2 ( neighbour, "NEIGHBOUR %s %s %s transmitting deferred "
  170. "packet\n", netdev->name, net_protocol->name,
  171. net_protocol->ntoa ( neighbour->net_dest ) );
  172. list_del ( &iobuf->list );
  173. if ( ( rc = net_tx ( iobuf, netdev, net_protocol, ll_dest,
  174. netdev->ll_addr ) ) != 0 ) {
  175. DBGC ( neighbour, "NEIGHBOUR %s %s %s could not "
  176. "transmit deferred packet: %s\n",
  177. netdev->name, net_protocol->name,
  178. net_protocol->ntoa ( neighbour->net_dest ),
  179. strerror ( rc ) );
  180. /* Ignore error and continue */
  181. }
  182. }
  183. ref_put ( &neighbour->refcnt );
  184. }
  185. /**
  186. * Destroy neighbour cache entry
  187. *
  188. * @v neighbour Neighbour cache entry
  189. * @v rc Reason for destruction
  190. */
  191. static void neighbour_destroy ( struct neighbour *neighbour, int rc ) {
  192. struct net_device *netdev = neighbour->netdev;
  193. struct net_protocol *net_protocol = neighbour->net_protocol;
  194. struct io_buffer *iobuf;
  195. /* Take ownership from cache */
  196. list_del ( &neighbour->list );
  197. /* Stop timer */
  198. stop_timer ( &neighbour->timer );
  199. /* Discard any outstanding I/O buffers */
  200. while ( ( iobuf = list_first_entry ( &neighbour->tx_queue,
  201. struct io_buffer, list )) != NULL){
  202. DBGC2 ( neighbour, "NEIGHBOUR %s %s %s discarding deferred "
  203. "packet: %s\n", netdev->name, net_protocol->name,
  204. net_protocol->ntoa ( neighbour->net_dest ),
  205. strerror ( rc ) );
  206. list_del ( &iobuf->list );
  207. netdev_tx_err ( neighbour->netdev, iobuf, rc );
  208. }
  209. DBGC ( neighbour, "NEIGHBOUR %s %s %s destroyed: %s\n", netdev->name,
  210. net_protocol->name, net_protocol->ntoa ( neighbour->net_dest ),
  211. strerror ( rc ) );
  212. /* Drop remaining reference */
  213. ref_put ( &neighbour->refcnt );
  214. }
  215. /**
  216. * Handle neighbour timer expiry
  217. *
  218. * @v timer Retry timer
  219. * @v fail Failure indicator
  220. */
  221. static void neighbour_expired ( struct retry_timer *timer, int fail ) {
  222. struct neighbour *neighbour =
  223. container_of ( timer, struct neighbour, timer );
  224. struct net_device *netdev = neighbour->netdev;
  225. struct net_protocol *net_protocol = neighbour->net_protocol;
  226. struct neighbour_discovery *discovery =
  227. neighbour->discovery;
  228. const void *net_dest = neighbour->net_dest;
  229. const void *net_source = neighbour->net_source;
  230. int rc;
  231. /* If we have failed, destroy the cache entry */
  232. if ( fail ) {
  233. neighbour_destroy ( neighbour, -ETIMEDOUT );
  234. return;
  235. }
  236. /* Restart the timer */
  237. start_timer ( &neighbour->timer );
  238. /* Transmit neighbour request */
  239. if ( ( rc = discovery->tx_request ( netdev, net_protocol, net_dest,
  240. net_source ) ) != 0 ) {
  241. DBGC ( neighbour, "NEIGHBOUR %s %s %s could not transmit %s "
  242. "request: %s\n", netdev->name, net_protocol->name,
  243. net_protocol->ntoa ( neighbour->net_dest ),
  244. neighbour->discovery->name, strerror ( rc ) );
  245. /* Retransmit when timer expires */
  246. return;
  247. }
  248. }
  249. /**
  250. * Transmit packet, determining link-layer address via neighbour discovery
  251. *
  252. * @v iobuf I/O buffer
  253. * @v netdev Network device
  254. * @v discovery Neighbour discovery protocol
  255. * @v net_protocol Network-layer protocol
  256. * @v net_dest Destination network-layer address
  257. * @v net_source Source network-layer address
  258. * @v ll_source Source link-layer address
  259. * @ret rc Return status code
  260. */
  261. int neighbour_tx ( struct io_buffer *iobuf, struct net_device *netdev,
  262. struct net_protocol *net_protocol, const void *net_dest,
  263. struct neighbour_discovery *discovery,
  264. const void *net_source, const void *ll_source ) {
  265. struct neighbour *neighbour;
  266. /* Find or create neighbour cache entry */
  267. neighbour = neighbour_find ( netdev, net_protocol, net_dest );
  268. if ( ! neighbour ) {
  269. neighbour = neighbour_create ( netdev, net_protocol, net_dest );
  270. if ( ! neighbour )
  271. return -ENOMEM;
  272. neighbour_discover ( neighbour, discovery, net_source );
  273. }
  274. /* If a link-layer address is available then transmit
  275. * immediately, otherwise queue for later transmission.
  276. */
  277. if ( neighbour_has_ll_dest ( neighbour ) ) {
  278. return net_tx ( iobuf, netdev, net_protocol, neighbour->ll_dest,
  279. ll_source );
  280. } else {
  281. DBGC2 ( neighbour, "NEIGHBOUR %s %s %s deferring packet\n",
  282. netdev->name, net_protocol->name,
  283. net_protocol->ntoa ( net_dest ) );
  284. list_add_tail ( &iobuf->list, &neighbour->tx_queue );
  285. return 0;
  286. }
  287. }
  288. /**
  289. * Update existing neighbour cache entry
  290. *
  291. * @v netdev Network device
  292. * @v net_protocol Network-layer protocol
  293. * @v net_dest Destination network-layer address
  294. * @v ll_dest Destination link-layer address
  295. * @ret rc Return status code
  296. */
  297. int neighbour_update ( struct net_device *netdev,
  298. struct net_protocol *net_protocol,
  299. const void *net_dest, const void *ll_dest ) {
  300. struct neighbour *neighbour;
  301. /* Find neighbour cache entry */
  302. neighbour = neighbour_find ( netdev, net_protocol, net_dest );
  303. if ( ! neighbour )
  304. return -ENOENT;
  305. /* Set destination address */
  306. neighbour_discovered ( neighbour, ll_dest );
  307. return 0;
  308. }
  309. /**
  310. * Define neighbour cache entry
  311. *
  312. * @v netdev Network device
  313. * @v net_protocol Network-layer protocol
  314. * @v net_dest Destination network-layer address
  315. * @v ll_dest Destination link-layer address, if known
  316. * @ret rc Return status code
  317. */
  318. int neighbour_define ( struct net_device *netdev,
  319. struct net_protocol *net_protocol,
  320. const void *net_dest, const void *ll_dest ) {
  321. struct neighbour *neighbour;
  322. /* Find or create neighbour cache entry */
  323. neighbour = neighbour_find ( netdev, net_protocol, net_dest );
  324. if ( ! neighbour ) {
  325. neighbour = neighbour_create ( netdev, net_protocol, net_dest );
  326. if ( ! neighbour )
  327. return -ENOMEM;
  328. }
  329. /* Set destination address */
  330. neighbour_discovered ( neighbour, ll_dest );
  331. return 0;
  332. }
  333. /**
  334. * Update neighbour cache on network device state change or removal
  335. *
  336. * @v netdev Network device
  337. */
  338. static void neighbour_flush ( struct net_device *netdev ) {
  339. struct neighbour *neighbour;
  340. struct neighbour *tmp;
  341. /* Remove all neighbour cache entries when a network device is closed */
  342. if ( ! netdev_is_open ( netdev ) ) {
  343. list_for_each_entry_safe ( neighbour, tmp, &neighbours, list )
  344. neighbour_destroy ( neighbour, -ENODEV );
  345. }
  346. }
  347. /** Neighbour driver (for net device notifications) */
  348. struct net_driver neighbour_net_driver __net_driver = {
  349. .name = "Neighbour",
  350. .notify = neighbour_flush,
  351. .remove = neighbour_flush,
  352. };
  353. /**
  354. * Discard some cached neighbour entries
  355. *
  356. * @ret discarded Number of cached items discarded
  357. */
  358. static unsigned int neighbour_discard ( void ) {
  359. struct neighbour *neighbour;
  360. /* Drop oldest cache entry, if any */
  361. neighbour = list_last_entry ( &neighbours, struct neighbour, list );
  362. if ( neighbour ) {
  363. neighbour_destroy ( neighbour, -ENOBUFS );
  364. return 1;
  365. } else {
  366. return 0;
  367. }
  368. }
  369. /**
  370. * Neighbour cache discarder
  371. *
  372. * Neighbour cache entries are deemed to have a high replacement cost,
  373. * since flushing an active neighbour cache entry midway through a TCP
  374. * transfer will cause substantial disruption.
  375. */
  376. struct cache_discarder neighbour_discarder __cache_discarder (CACHE_EXPENSIVE)={
  377. .discard = neighbour_discard,
  378. };