You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

netdevice.c 15KB

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