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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  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 ( netdev ),
  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 ( netdev ),
  256. NULL ) ) != 0 ) {
  257. DBGC ( netdev, "NETDEV %p could not register settings: %s\n",
  258. netdev, strerror ( rc ) );
  259. return rc;
  260. }
  261. /* Add to device list */
  262. netdev_get ( netdev );
  263. list_add_tail ( &netdev->list, &net_devices );
  264. DBGC ( netdev, "NETDEV %p registered as %s (phys %s hwaddr %s)\n",
  265. netdev, netdev->name, netdev->dev->name,
  266. netdev_hwaddr ( netdev ) );
  267. return 0;
  268. }
  269. /**
  270. * Open network device
  271. *
  272. * @v netdev Network device
  273. * @ret rc Return status code
  274. */
  275. int netdev_open ( struct net_device *netdev ) {
  276. int rc;
  277. /* Do nothing if device is already open */
  278. if ( netdev->state & NETDEV_OPEN )
  279. return 0;
  280. DBGC ( netdev, "NETDEV %p opening\n", netdev );
  281. /* Open the device */
  282. if ( ( rc = netdev->op->open ( netdev ) ) != 0 )
  283. return rc;
  284. /* Mark as opened */
  285. netdev->state |= NETDEV_OPEN;
  286. return 0;
  287. }
  288. /**
  289. * Close network device
  290. *
  291. * @v netdev Network device
  292. */
  293. void netdev_close ( struct net_device *netdev ) {
  294. /* Do nothing if device is already closed */
  295. if ( ! ( netdev->state & NETDEV_OPEN ) )
  296. return;
  297. DBGC ( netdev, "NETDEV %p closing\n", netdev );
  298. /* Close the device */
  299. netdev->op->close ( netdev );
  300. /* Flush TX and RX queues */
  301. netdev_tx_flush ( netdev );
  302. netdev_rx_flush ( netdev );
  303. /* Mark as closed */
  304. netdev->state &= ~NETDEV_OPEN;
  305. }
  306. /**
  307. * Unregister network device
  308. *
  309. * @v netdev Network device
  310. *
  311. * Removes the network device from the list of network devices.
  312. */
  313. void unregister_netdev ( struct net_device *netdev ) {
  314. /* Ensure device is closed */
  315. netdev_close ( netdev );
  316. /* Unregister per-netdev configuration settings */
  317. unregister_settings ( netdev_settings ( netdev ) );
  318. /* Remove from device list */
  319. list_del ( &netdev->list );
  320. netdev_put ( netdev );
  321. DBGC ( netdev, "NETDEV %p unregistered\n", netdev );
  322. }
  323. /** Enable or disable interrupts
  324. *
  325. * @v netdev Network device
  326. * @v enable Interrupts should be enabled
  327. */
  328. void netdev_irq ( struct net_device *netdev, int enable ) {
  329. netdev->op->irq ( netdev, enable );
  330. }
  331. /**
  332. * Get network device by name
  333. *
  334. * @v name Network device name
  335. * @ret netdev Network device, or NULL
  336. */
  337. struct net_device * find_netdev ( const char *name ) {
  338. struct net_device *netdev;
  339. list_for_each_entry ( netdev, &net_devices, list ) {
  340. if ( strcmp ( netdev->name, name ) == 0 )
  341. return netdev;
  342. }
  343. return NULL;
  344. }
  345. /**
  346. * Get network device by PCI bus:dev.fn address
  347. *
  348. * @v bus_type Bus type
  349. * @v location Bus location
  350. * @ret netdev Network device, or NULL
  351. */
  352. struct net_device * find_netdev_by_location ( unsigned int bus_type,
  353. unsigned int location ) {
  354. struct net_device *netdev;
  355. list_for_each_entry ( netdev, &net_devices, list ) {
  356. if ( ( netdev->dev->desc.bus_type == bus_type ) &&
  357. ( netdev->dev->desc.location == location ) )
  358. return netdev;
  359. }
  360. return NULL;
  361. }
  362. /**
  363. * Transmit network-layer packet
  364. *
  365. * @v iobuf I/O buffer
  366. * @v netdev Network device
  367. * @v net_protocol Network-layer protocol
  368. * @v ll_dest Destination link-layer address
  369. * @ret rc Return status code
  370. *
  371. * Prepends link-layer headers to the I/O buffer and transmits the
  372. * packet via the specified network device. This function takes
  373. * ownership of the I/O buffer.
  374. */
  375. int net_tx ( struct io_buffer *iobuf, struct net_device *netdev,
  376. struct net_protocol *net_protocol, const void *ll_dest ) {
  377. int rc;
  378. /* Force a poll on the netdevice to (potentially) clear any
  379. * backed-up TX completions. This is needed on some network
  380. * devices to avoid excessive losses due to small TX ring
  381. * sizes.
  382. */
  383. netdev_poll ( netdev );
  384. /* Add link-layer header */
  385. if ( ( rc = netdev->ll_protocol->push ( iobuf, netdev, net_protocol,
  386. ll_dest ) ) != 0 ) {
  387. free_iob ( iobuf );
  388. return rc;
  389. }
  390. /* Transmit packet */
  391. return netdev_tx ( netdev, iobuf );
  392. }
  393. /**
  394. * Process received network-layer packet
  395. *
  396. * @v iobuf I/O buffer
  397. * @v netdev Network device
  398. * @v net_proto Network-layer protocol, in network-byte order
  399. * @v ll_source Source link-layer address
  400. * @ret rc Return status code
  401. */
  402. int net_rx ( struct io_buffer *iobuf, struct net_device *netdev,
  403. uint16_t net_proto, const void *ll_source ) {
  404. struct net_protocol *net_protocol;
  405. /* Hand off to network-layer protocol, if any */
  406. for ( net_protocol = net_protocols ; net_protocol < net_protocols_end ;
  407. net_protocol++ ) {
  408. if ( net_protocol->net_proto == net_proto ) {
  409. return net_protocol->rx ( iobuf, netdev, ll_source );
  410. }
  411. }
  412. free_iob ( iobuf );
  413. return 0;
  414. }
  415. /**
  416. * Single-step the network stack
  417. *
  418. * @v process Network stack process
  419. *
  420. * This polls all interfaces for received packets, and processes
  421. * packets from the RX queue.
  422. */
  423. static void net_step ( struct process *process __unused ) {
  424. struct net_device *netdev;
  425. struct io_buffer *iobuf;
  426. struct ll_protocol *ll_protocol;
  427. uint16_t net_proto;
  428. const void *ll_source;
  429. int rc;
  430. /* Poll and process each network device */
  431. list_for_each_entry ( netdev, &net_devices, list ) {
  432. /* Poll for new packets */
  433. netdev_poll ( netdev );
  434. /* Process at most one received packet. Give priority
  435. * to getting packets out of the NIC over processing
  436. * the received packets, because we advertise a window
  437. * that assumes that we can receive packets from the
  438. * NIC faster than they arrive.
  439. */
  440. if ( ( iobuf = netdev_rx_dequeue ( netdev ) ) ) {
  441. DBGC ( netdev, "NETDEV %p processing %p (%p+%zx)\n",
  442. netdev, iobuf, iobuf->data,
  443. iob_len ( iobuf ) );
  444. /* Remove link-layer header */
  445. ll_protocol = netdev->ll_protocol;
  446. if ( ( rc = ll_protocol->pull ( iobuf, netdev,
  447. &net_proto,
  448. &ll_source ) ) != 0 ) {
  449. free_iob ( iobuf );
  450. continue;
  451. }
  452. net_rx ( iobuf, netdev, net_proto, ll_source );
  453. }
  454. }
  455. }
  456. /** Networking stack process */
  457. struct process net_process __permanent_process = {
  458. .step = net_step,
  459. };