Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

netdevice.c 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. /*
  2. * Copyright (C) 2006 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., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. FILE_LICENCE ( GPL2_OR_LATER );
  19. #include <stdint.h>
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include <byteswap.h>
  23. #include <string.h>
  24. #include <errno.h>
  25. #include <gpxe/if_ether.h>
  26. #include <gpxe/iobuf.h>
  27. #include <gpxe/tables.h>
  28. #include <gpxe/process.h>
  29. #include <gpxe/init.h>
  30. #include <gpxe/device.h>
  31. #include <gpxe/netdevice.h>
  32. /** @file
  33. *
  34. * Network device management
  35. *
  36. */
  37. /** List of network devices */
  38. struct list_head net_devices = LIST_HEAD_INIT ( net_devices );
  39. /** List of open network devices, in reverse order of opening */
  40. struct list_head open_net_devices = LIST_HEAD_INIT ( open_net_devices );
  41. /**
  42. * Record network device statistic
  43. *
  44. * @v stats Network device statistics
  45. * @v rc Status code
  46. */
  47. static void netdev_record_stat ( struct net_device_stats *stats, int rc ) {
  48. struct net_device_error *error;
  49. struct net_device_error *least_common_error;
  50. unsigned int i;
  51. /* If this is not an error, just update the good counter */
  52. if ( rc == 0 ) {
  53. stats->good++;
  54. return;
  55. }
  56. /* Update the bad counter */
  57. stats->bad++;
  58. /* Locate the appropriate error record */
  59. least_common_error = &stats->errors[0];
  60. for ( i = 0 ; i < ( sizeof ( stats->errors ) /
  61. sizeof ( stats->errors[0] ) ) ; i++ ) {
  62. error = &stats->errors[i];
  63. /* Update matching record, if found */
  64. if ( error->rc == rc ) {
  65. error->count++;
  66. return;
  67. }
  68. if ( error->count < least_common_error->count )
  69. least_common_error = error;
  70. }
  71. /* Overwrite the least common error record */
  72. least_common_error->rc = rc;
  73. least_common_error->count = 1;
  74. }
  75. /**
  76. * Transmit raw packet via network device
  77. *
  78. * @v netdev Network device
  79. * @v iobuf I/O buffer
  80. * @ret rc Return status code
  81. *
  82. * Transmits the packet via the specified network device. This
  83. * function takes ownership of the I/O buffer.
  84. */
  85. int netdev_tx ( struct net_device *netdev, struct io_buffer *iobuf ) {
  86. int rc;
  87. DBGC ( netdev, "NETDEV %p transmitting %p (%p+%zx)\n",
  88. netdev, iobuf, iobuf->data, iob_len ( iobuf ) );
  89. list_add_tail ( &iobuf->list, &netdev->tx_queue );
  90. if ( ! ( netdev->state & NETDEV_OPEN ) ) {
  91. rc = -ENETUNREACH;
  92. goto err;
  93. }
  94. if ( ( rc = netdev->op->transmit ( netdev, iobuf ) ) != 0 )
  95. goto err;
  96. return 0;
  97. err:
  98. netdev_tx_complete_err ( netdev, iobuf, rc );
  99. return rc;
  100. }
  101. /**
  102. * Complete network transmission
  103. *
  104. * @v netdev Network device
  105. * @v iobuf I/O buffer
  106. * @v rc Packet status code
  107. *
  108. * The packet must currently be in the network device's TX queue.
  109. */
  110. void netdev_tx_complete_err ( struct net_device *netdev,
  111. struct io_buffer *iobuf, int rc ) {
  112. /* Update statistics counter */
  113. netdev_record_stat ( &netdev->tx_stats, rc );
  114. if ( rc == 0 ) {
  115. DBGC ( netdev, "NETDEV %p transmission %p complete\n",
  116. netdev, iobuf );
  117. } else {
  118. DBGC ( netdev, "NETDEV %p transmission %p failed: %s\n",
  119. netdev, iobuf, strerror ( rc ) );
  120. }
  121. /* Catch data corruption as early as possible */
  122. assert ( iobuf->list.next != NULL );
  123. assert ( iobuf->list.prev != NULL );
  124. /* Dequeue and free I/O buffer */
  125. list_del ( &iobuf->list );
  126. free_iob ( iobuf );
  127. }
  128. /**
  129. * Complete network transmission
  130. *
  131. * @v netdev Network device
  132. * @v rc Packet status code
  133. *
  134. * Completes the oldest outstanding packet in the TX queue.
  135. */
  136. void netdev_tx_complete_next_err ( struct net_device *netdev, int rc ) {
  137. struct io_buffer *iobuf;
  138. list_for_each_entry ( iobuf, &netdev->tx_queue, list ) {
  139. netdev_tx_complete_err ( netdev, iobuf, rc );
  140. return;
  141. }
  142. }
  143. /**
  144. * Flush device's transmit queue
  145. *
  146. * @v netdev Network device
  147. */
  148. static void netdev_tx_flush ( struct net_device *netdev ) {
  149. /* Discard any packets in the TX queue */
  150. while ( ! list_empty ( &netdev->tx_queue ) ) {
  151. netdev_tx_complete_next_err ( netdev, -ECANCELED );
  152. }
  153. }
  154. /**
  155. * Add packet to receive queue
  156. *
  157. * @v netdev Network device
  158. * @v iobuf I/O buffer, or NULL
  159. *
  160. * The packet is added to the network device's RX queue. This
  161. * function takes ownership of the I/O buffer.
  162. */
  163. void netdev_rx ( struct net_device *netdev, struct io_buffer *iobuf ) {
  164. DBGC ( netdev, "NETDEV %p received %p (%p+%zx)\n",
  165. netdev, iobuf, iobuf->data, iob_len ( iobuf ) );
  166. /* Enqueue packet */
  167. list_add_tail ( &iobuf->list, &netdev->rx_queue );
  168. /* Update statistics counter */
  169. netdev_record_stat ( &netdev->rx_stats, 0 );
  170. }
  171. /**
  172. * Discard received packet
  173. *
  174. * @v netdev Network device
  175. * @v iobuf I/O buffer, or NULL
  176. * @v rc Packet status code
  177. *
  178. * The packet is discarded and an RX error is recorded. This function
  179. * takes ownership of the I/O buffer. @c iobuf may be NULL if, for
  180. * example, the net device wishes to report an error due to being
  181. * unable to allocate an I/O buffer.
  182. */
  183. void netdev_rx_err ( struct net_device *netdev,
  184. struct io_buffer *iobuf, int rc ) {
  185. DBGC ( netdev, "NETDEV %p failed to receive %p: %s\n",
  186. netdev, iobuf, strerror ( rc ) );
  187. /* Discard packet */
  188. free_iob ( iobuf );
  189. /* Update statistics counter */
  190. netdev_record_stat ( &netdev->rx_stats, rc );
  191. }
  192. /**
  193. * Poll for completed and received packets on network device
  194. *
  195. * @v netdev Network device
  196. *
  197. * Polls the network device for completed transmissions and received
  198. * packets. Any received packets will be added to the RX packet queue
  199. * via netdev_rx().
  200. */
  201. void netdev_poll ( struct net_device *netdev ) {
  202. if ( netdev->state & NETDEV_OPEN )
  203. netdev->op->poll ( netdev );
  204. }
  205. /**
  206. * Remove packet from device's receive queue
  207. *
  208. * @v netdev Network device
  209. * @ret iobuf I/O buffer, or NULL
  210. *
  211. * Removes the first packet from the device's RX queue and returns it.
  212. * Ownership of the packet is transferred to the caller.
  213. */
  214. struct io_buffer * netdev_rx_dequeue ( struct net_device *netdev ) {
  215. struct io_buffer *iobuf;
  216. list_for_each_entry ( iobuf, &netdev->rx_queue, list ) {
  217. list_del ( &iobuf->list );
  218. return iobuf;
  219. }
  220. return NULL;
  221. }
  222. /**
  223. * Flush device's receive queue
  224. *
  225. * @v netdev Network device
  226. */
  227. static void netdev_rx_flush ( struct net_device *netdev ) {
  228. struct io_buffer *iobuf;
  229. /* Discard any packets in the RX queue */
  230. while ( ( iobuf = netdev_rx_dequeue ( netdev ) ) ) {
  231. netdev_rx_err ( netdev, iobuf, -ECANCELED );
  232. }
  233. }
  234. /**
  235. * Free network device
  236. *
  237. * @v refcnt Network device reference counter
  238. */
  239. static void free_netdev ( struct refcnt *refcnt ) {
  240. struct net_device *netdev =
  241. container_of ( refcnt, struct net_device, refcnt );
  242. netdev_tx_flush ( netdev );
  243. netdev_rx_flush ( netdev );
  244. clear_settings ( netdev_settings ( netdev ) );
  245. free ( netdev );
  246. }
  247. /**
  248. * Allocate network device
  249. *
  250. * @v priv_size Size of private data area (net_device::priv)
  251. * @ret netdev Network device, or NULL
  252. *
  253. * Allocates space for a network device and its private data area.
  254. */
  255. struct net_device * alloc_netdev ( size_t priv_size ) {
  256. struct net_device *netdev;
  257. size_t total_len;
  258. total_len = ( sizeof ( *netdev ) + priv_size );
  259. netdev = zalloc ( total_len );
  260. if ( netdev ) {
  261. netdev->refcnt.free = free_netdev;
  262. INIT_LIST_HEAD ( &netdev->tx_queue );
  263. INIT_LIST_HEAD ( &netdev->rx_queue );
  264. netdev_settings_init ( netdev );
  265. netdev->priv = ( ( ( void * ) netdev ) + sizeof ( *netdev ) );
  266. }
  267. return netdev;
  268. }
  269. /**
  270. * Register network device
  271. *
  272. * @v netdev Network device
  273. * @ret rc Return status code
  274. *
  275. * Gives the network device a name and adds it to the list of network
  276. * devices.
  277. */
  278. int register_netdev ( struct net_device *netdev ) {
  279. static unsigned int ifindex = 0;
  280. int rc;
  281. /* Create device name */
  282. snprintf ( netdev->name, sizeof ( netdev->name ), "net%d",
  283. ifindex++ );
  284. /* Register per-netdev configuration settings */
  285. if ( ( rc = register_settings ( netdev_settings ( netdev ),
  286. NULL ) ) != 0 ) {
  287. DBGC ( netdev, "NETDEV %p could not register settings: %s\n",
  288. netdev, strerror ( rc ) );
  289. return rc;
  290. }
  291. /* Add to device list */
  292. netdev_get ( netdev );
  293. list_add_tail ( &netdev->list, &net_devices );
  294. DBGC ( netdev, "NETDEV %p registered as %s (phys %s hwaddr %s)\n",
  295. netdev, netdev->name, netdev->dev->name,
  296. netdev_hwaddr ( netdev ) );
  297. return 0;
  298. }
  299. /**
  300. * Open network device
  301. *
  302. * @v netdev Network device
  303. * @ret rc Return status code
  304. */
  305. int netdev_open ( struct net_device *netdev ) {
  306. int rc;
  307. /* Do nothing if device is already open */
  308. if ( netdev->state & NETDEV_OPEN )
  309. return 0;
  310. DBGC ( netdev, "NETDEV %p opening\n", netdev );
  311. /* Open the device */
  312. if ( ( rc = netdev->op->open ( netdev ) ) != 0 )
  313. return rc;
  314. /* Mark as opened */
  315. netdev->state |= NETDEV_OPEN;
  316. /* Add to head of open devices list */
  317. list_add ( &netdev->open_list, &open_net_devices );
  318. return 0;
  319. }
  320. /**
  321. * Close network device
  322. *
  323. * @v netdev Network device
  324. */
  325. void netdev_close ( struct net_device *netdev ) {
  326. /* Do nothing if device is already closed */
  327. if ( ! ( netdev->state & NETDEV_OPEN ) )
  328. return;
  329. DBGC ( netdev, "NETDEV %p closing\n", netdev );
  330. /* Close the device */
  331. netdev->op->close ( netdev );
  332. /* Flush TX and RX queues */
  333. netdev_tx_flush ( netdev );
  334. netdev_rx_flush ( netdev );
  335. /* Mark as closed */
  336. netdev->state &= ~NETDEV_OPEN;
  337. /* Remove from open devices list */
  338. list_del ( &netdev->open_list );
  339. }
  340. /**
  341. * Unregister network device
  342. *
  343. * @v netdev Network device
  344. *
  345. * Removes the network device from the list of network devices.
  346. */
  347. void unregister_netdev ( struct net_device *netdev ) {
  348. /* Ensure device is closed */
  349. netdev_close ( netdev );
  350. /* Unregister per-netdev configuration settings */
  351. unregister_settings ( netdev_settings ( netdev ) );
  352. /* Remove from device list */
  353. list_del ( &netdev->list );
  354. netdev_put ( netdev );
  355. DBGC ( netdev, "NETDEV %p unregistered\n", netdev );
  356. }
  357. /** Enable or disable interrupts
  358. *
  359. * @v netdev Network device
  360. * @v enable Interrupts should be enabled
  361. */
  362. void netdev_irq ( struct net_device *netdev, int enable ) {
  363. netdev->op->irq ( netdev, enable );
  364. }
  365. /**
  366. * Get network device by name
  367. *
  368. * @v name Network device name
  369. * @ret netdev Network device, or NULL
  370. */
  371. struct net_device * find_netdev ( const char *name ) {
  372. struct net_device *netdev;
  373. list_for_each_entry ( netdev, &net_devices, list ) {
  374. if ( strcmp ( netdev->name, name ) == 0 )
  375. return netdev;
  376. }
  377. return NULL;
  378. }
  379. /**
  380. * Get network device by PCI bus:dev.fn address
  381. *
  382. * @v bus_type Bus type
  383. * @v location Bus location
  384. * @ret netdev Network device, or NULL
  385. */
  386. struct net_device * find_netdev_by_location ( unsigned int bus_type,
  387. unsigned int location ) {
  388. struct net_device *netdev;
  389. list_for_each_entry ( netdev, &net_devices, list ) {
  390. if ( ( netdev->dev->desc.bus_type == bus_type ) &&
  391. ( netdev->dev->desc.location == location ) )
  392. return netdev;
  393. }
  394. return NULL;
  395. }
  396. /**
  397. * Get most recently opened network device
  398. *
  399. * @ret netdev Most recently opened network device, or NULL
  400. */
  401. struct net_device * last_opened_netdev ( void ) {
  402. struct net_device *netdev;
  403. list_for_each_entry ( netdev, &open_net_devices, open_list ) {
  404. assert ( netdev->state & NETDEV_OPEN );
  405. return netdev;
  406. }
  407. return NULL;
  408. }
  409. /**
  410. * Transmit network-layer packet
  411. *
  412. * @v iobuf I/O buffer
  413. * @v netdev Network device
  414. * @v net_protocol Network-layer protocol
  415. * @v ll_dest Destination link-layer address
  416. * @ret rc Return status code
  417. *
  418. * Prepends link-layer headers to the I/O buffer and transmits the
  419. * packet via the specified network device. This function takes
  420. * ownership of the I/O buffer.
  421. */
  422. int net_tx ( struct io_buffer *iobuf, struct net_device *netdev,
  423. struct net_protocol *net_protocol, const void *ll_dest ) {
  424. struct ll_protocol *ll_protocol = netdev->ll_protocol;
  425. int rc;
  426. /* Force a poll on the netdevice to (potentially) clear any
  427. * backed-up TX completions. This is needed on some network
  428. * devices to avoid excessive losses due to small TX ring
  429. * sizes.
  430. */
  431. netdev_poll ( netdev );
  432. /* Add link-layer header */
  433. if ( ( rc = ll_protocol->push ( netdev, iobuf, ll_dest, netdev->ll_addr,
  434. net_protocol->net_proto ) ) != 0 ) {
  435. free_iob ( iobuf );
  436. return rc;
  437. }
  438. /* Transmit packet */
  439. return netdev_tx ( netdev, iobuf );
  440. }
  441. /**
  442. * Process received network-layer packet
  443. *
  444. * @v iobuf I/O buffer
  445. * @v netdev Network device
  446. * @v net_proto Network-layer protocol, in network-byte order
  447. * @v ll_source Source link-layer address
  448. * @ret rc Return status code
  449. */
  450. int net_rx ( struct io_buffer *iobuf, struct net_device *netdev,
  451. uint16_t net_proto, const void *ll_source ) {
  452. struct net_protocol *net_protocol;
  453. /* Hand off to network-layer protocol, if any */
  454. for_each_table_entry ( net_protocol, NET_PROTOCOLS ) {
  455. if ( net_protocol->net_proto == net_proto )
  456. return net_protocol->rx ( iobuf, netdev, ll_source );
  457. }
  458. DBGC ( netdev, "NETDEV %p unknown network protocol %04x\n",
  459. netdev, ntohs ( net_proto ) );
  460. free_iob ( iobuf );
  461. return 0;
  462. }
  463. /**
  464. * Single-step the network stack
  465. *
  466. * @v process Network stack process
  467. *
  468. * This polls all interfaces for received packets, and processes
  469. * packets from the RX queue.
  470. */
  471. static void net_step ( struct process *process __unused ) {
  472. struct net_device *netdev;
  473. struct io_buffer *iobuf;
  474. struct ll_protocol *ll_protocol;
  475. const void *ll_dest;
  476. const void *ll_source;
  477. uint16_t net_proto;
  478. int rc;
  479. /* Poll and process each network device */
  480. list_for_each_entry ( netdev, &net_devices, list ) {
  481. /* Poll for new packets */
  482. netdev_poll ( netdev );
  483. /* Process at most one received packet. Give priority
  484. * to getting packets out of the NIC over processing
  485. * the received packets, because we advertise a window
  486. * that assumes that we can receive packets from the
  487. * NIC faster than they arrive.
  488. */
  489. if ( ( iobuf = netdev_rx_dequeue ( netdev ) ) ) {
  490. DBGC ( netdev, "NETDEV %p processing %p (%p+%zx)\n",
  491. netdev, iobuf, iobuf->data,
  492. iob_len ( iobuf ) );
  493. /* Remove link-layer header */
  494. ll_protocol = netdev->ll_protocol;
  495. if ( ( rc = ll_protocol->pull ( netdev, iobuf,
  496. &ll_dest, &ll_source,
  497. &net_proto ) ) != 0 ) {
  498. free_iob ( iobuf );
  499. continue;
  500. }
  501. net_rx ( iobuf, netdev, net_proto, ll_source );
  502. }
  503. }
  504. }
  505. /** Networking stack process */
  506. struct process net_process __permanent_process = {
  507. .step = net_step,
  508. };