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 13KB

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