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.

xfer.c 10.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /*
  2. * Copyright (C) 2007 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 <string.h>
  20. #include <stdio.h>
  21. #include <errno.h>
  22. #include <gpxe/xfer.h>
  23. /** @file
  24. *
  25. * Data transfer interfaces
  26. *
  27. */
  28. /**
  29. * Dummy transfer metadata
  30. *
  31. * This gets passed to xfer_interface::deliver_iob() and equivalents
  32. * when no metadata is available.
  33. */
  34. static struct xfer_metadata dummy_metadata;
  35. /**
  36. * Close data transfer interface
  37. *
  38. * @v xfer Data transfer interface
  39. * @v rc Reason for close
  40. */
  41. void xfer_close ( struct xfer_interface *xfer, int rc ) {
  42. struct xfer_interface *dest = xfer_get_dest ( xfer );
  43. struct xfer_interface_operations *op = xfer->op;
  44. DBGC ( xfer, "XFER %p->%p close\n", xfer, dest );
  45. xfer_unplug ( xfer );
  46. xfer_nullify ( xfer );
  47. dest->op->close ( dest, rc );
  48. xfer->op = op;
  49. xfer_put ( dest );
  50. }
  51. /**
  52. * Send redirection event
  53. *
  54. * @v xfer Data transfer interface
  55. * @v type New location type
  56. * @v args Remaining arguments depend upon location type
  57. * @ret rc Return status code
  58. */
  59. int xfer_vredirect ( struct xfer_interface *xfer, int type, va_list args ) {
  60. struct xfer_interface *dest = xfer_get_dest ( xfer );
  61. int rc;
  62. DBGC ( xfer, "XFER %p->%p redirect\n", xfer, dest );
  63. rc = dest->op->vredirect ( dest, type, args );
  64. if ( rc != 0 ) {
  65. DBGC ( xfer, "XFER %p<-%p redirect: %s\n", xfer, dest,
  66. strerror ( rc ) );
  67. }
  68. xfer_put ( dest );
  69. return rc;
  70. }
  71. /**
  72. * Send redirection event
  73. *
  74. * @v xfer Data transfer interface
  75. * @v type New location type
  76. * @v ... Remaining arguments depend upon location type
  77. * @ret rc Return status code
  78. */
  79. int xfer_redirect ( struct xfer_interface *xfer, int type, ... ) {
  80. va_list args;
  81. int rc;
  82. va_start ( args, type );
  83. rc = xfer_vredirect ( xfer, type, args );
  84. va_end ( args );
  85. return rc;
  86. }
  87. /**
  88. * Check flow control window
  89. *
  90. * @v xfer Data transfer interface
  91. * @ret len Length of window
  92. */
  93. size_t xfer_window ( struct xfer_interface *xfer ) {
  94. struct xfer_interface *dest = xfer_get_dest ( xfer );
  95. size_t len;
  96. len = dest->op->window ( dest );
  97. xfer_put ( dest );
  98. return len;
  99. }
  100. /**
  101. * Allocate I/O buffer
  102. *
  103. * @v xfer Data transfer interface
  104. * @v len I/O buffer payload length
  105. * @ret iobuf I/O buffer
  106. */
  107. struct io_buffer * xfer_alloc_iob ( struct xfer_interface *xfer, size_t len ) {
  108. struct xfer_interface *dest = xfer_get_dest ( xfer );
  109. struct io_buffer *iobuf;
  110. DBGC ( xfer, "XFER %p->%p alloc_iob %zd\n", xfer, dest, len );
  111. iobuf = dest->op->alloc_iob ( dest, len );
  112. if ( ! iobuf ) {
  113. DBGC ( xfer, "XFER %p<-%p alloc_iob failed\n", xfer, dest );
  114. }
  115. xfer_put ( dest );
  116. return iobuf;
  117. }
  118. /**
  119. * Deliver datagram as I/O buffer with metadata
  120. *
  121. * @v xfer Data transfer interface
  122. * @v iobuf Datagram I/O buffer
  123. * @v meta Data transfer metadata
  124. * @ret rc Return status code
  125. */
  126. int xfer_deliver_iob_meta ( struct xfer_interface *xfer,
  127. struct io_buffer *iobuf,
  128. struct xfer_metadata *meta ) {
  129. struct xfer_interface *dest = xfer_get_dest ( xfer );
  130. int rc;
  131. DBGC ( xfer, "XFER %p->%p deliver_iob %zd\n", xfer, dest,
  132. iob_len ( iobuf ) );
  133. rc = dest->op->deliver_iob ( dest, iobuf, meta );
  134. if ( rc != 0 ) {
  135. DBGC ( xfer, "XFER %p<-%p deliver_iob: %s\n", xfer, dest,
  136. strerror ( rc ) );
  137. }
  138. xfer_put ( dest );
  139. return rc;
  140. }
  141. /**
  142. * Deliver datagram as I/O buffer with metadata
  143. *
  144. * @v xfer Data transfer interface
  145. * @v iobuf Datagram I/O buffer
  146. * @ret rc Return status code
  147. */
  148. int xfer_deliver_iob ( struct xfer_interface *xfer,
  149. struct io_buffer *iobuf ) {
  150. return xfer_deliver_iob_meta ( xfer, iobuf, &dummy_metadata );
  151. }
  152. /**
  153. * Deliver datagram as raw data
  154. *
  155. * @v xfer Data transfer interface
  156. * @v iobuf Datagram I/O buffer
  157. * @ret rc Return status code
  158. */
  159. int xfer_deliver_raw ( struct xfer_interface *xfer,
  160. const void *data, size_t len ) {
  161. struct xfer_interface *dest = xfer_get_dest ( xfer );
  162. int rc;
  163. DBGC ( xfer, "XFER %p->%p deliver_raw %p+%zd\n", xfer, dest,
  164. data, len );
  165. rc = dest->op->deliver_raw ( dest, data, len );
  166. if ( rc != 0 ) {
  167. DBGC ( xfer, "XFER %p<-%p deliver_raw: %s\n", xfer, dest,
  168. strerror ( rc ) );
  169. }
  170. xfer_put ( dest );
  171. return rc;
  172. }
  173. /**
  174. * Deliver formatted string
  175. *
  176. * @v xfer Data transfer interface
  177. * @v format Format string
  178. * @v args Arguments corresponding to the format string
  179. * @ret rc Return status code
  180. */
  181. int xfer_vprintf ( struct xfer_interface *xfer, const char *format,
  182. va_list args ) {
  183. size_t len;
  184. va_list args_tmp;
  185. va_copy ( args_tmp, args );
  186. len = vsnprintf ( NULL, 0, format, args );
  187. {
  188. char buf[len + 1];
  189. vsnprintf ( buf, sizeof ( buf ), format, args_tmp );
  190. va_end ( args_tmp );
  191. return xfer_deliver_raw ( xfer, buf, len );
  192. }
  193. }
  194. /**
  195. * Deliver formatted string
  196. *
  197. * @v xfer Data transfer interface
  198. * @v format Format string
  199. * @v ... Arguments corresponding to the format string
  200. * @ret rc Return status code
  201. */
  202. int xfer_printf ( struct xfer_interface *xfer, const char *format, ... ) {
  203. va_list args;
  204. int rc;
  205. va_start ( args, format );
  206. rc = xfer_vprintf ( xfer, format, args );
  207. va_end ( args );
  208. return rc;
  209. }
  210. /**
  211. * Seek to position
  212. *
  213. * @v xfer Data transfer interface
  214. * @v offset Offset to new position
  215. * @v whence Basis for new position
  216. * @ret rc Return status code
  217. */
  218. int xfer_seek ( struct xfer_interface *xfer, off_t offset, int whence ) {
  219. struct io_buffer *iobuf;
  220. struct xfer_metadata meta = {
  221. .offset = offset,
  222. .whence = whence,
  223. };
  224. DBGC ( xfer, "XFER %p seek %s+%ld\n", xfer,
  225. whence_text ( whence ), offset );
  226. /* Allocate and send a zero-length data buffer */
  227. iobuf = xfer_alloc_iob ( xfer, 0 );
  228. if ( ! iobuf )
  229. return -ENOMEM;
  230. return xfer_deliver_iob_meta ( xfer, iobuf, &meta );
  231. }
  232. /****************************************************************************
  233. *
  234. * Helper methods
  235. *
  236. * These functions are designed to be used as methods in the
  237. * xfer_interface_operations table.
  238. *
  239. */
  240. /**
  241. * Ignore close() event
  242. *
  243. * @v xfer Data transfer interface
  244. * @v rc Reason for close
  245. */
  246. void ignore_xfer_close ( struct xfer_interface *xfer __unused,
  247. int rc __unused ) {
  248. /* Nothing to do */
  249. }
  250. /**
  251. * Ignore vredirect() event
  252. *
  253. * @v xfer Data transfer interface
  254. * @v type New location type
  255. * @v args Remaining arguments depend upon location type
  256. * @ret rc Return status code
  257. */
  258. int ignore_xfer_vredirect ( struct xfer_interface *xfer __unused,
  259. int type __unused, va_list args __unused ) {
  260. return 0;
  261. }
  262. /**
  263. * Unlimited flow control window
  264. *
  265. * @v xfer Data transfer interface
  266. * @ret len Length of window
  267. *
  268. * This handler indicates that the interface is always ready to accept
  269. * data.
  270. */
  271. size_t unlimited_xfer_window ( struct xfer_interface *xfer __unused ) {
  272. return ~( ( size_t ) 0 );
  273. }
  274. /**
  275. * No flow control window
  276. *
  277. * @v xfer Data transfer interface
  278. * @ret len Length of window
  279. *
  280. * This handler indicates that the interface is never ready to accept
  281. * data.
  282. */
  283. size_t no_xfer_window ( struct xfer_interface *xfer __unused ) {
  284. return 0;
  285. }
  286. /**
  287. * Allocate I/O buffer
  288. *
  289. * @v xfer Data transfer interface
  290. * @v len I/O buffer payload length
  291. * @ret iobuf I/O buffer
  292. */
  293. struct io_buffer *
  294. default_xfer_alloc_iob ( struct xfer_interface *xfer __unused, size_t len ) {
  295. return alloc_iob ( len );
  296. }
  297. /**
  298. * Deliver datagram as raw data
  299. *
  300. * @v xfer Data transfer interface
  301. * @v iobuf Datagram I/O buffer
  302. * @v meta Data transfer metadata
  303. * @ret rc Return status code
  304. *
  305. * This function is intended to be used as the deliver() method for
  306. * data transfer interfaces that prefer to handle raw data.
  307. */
  308. int xfer_deliver_as_raw ( struct xfer_interface *xfer,
  309. struct io_buffer *iobuf,
  310. struct xfer_metadata *meta __unused ) {
  311. int rc;
  312. rc = xfer->op->deliver_raw ( xfer, iobuf->data, iob_len ( iobuf ) );
  313. free_iob ( iobuf );
  314. return rc;
  315. }
  316. /**
  317. * Deliver datagram as I/O buffer
  318. *
  319. * @v xfer Data transfer interface
  320. * @v data Data buffer
  321. * @v len Length of data buffer
  322. * @ret rc Return status code
  323. *
  324. * This function is intended to be used as the deliver_raw() method
  325. * for data transfer interfaces that prefer to handle I/O buffers.
  326. */
  327. int xfer_deliver_as_iob ( struct xfer_interface *xfer,
  328. const void *data, size_t len ) {
  329. struct io_buffer *iobuf;
  330. iobuf = xfer->op->alloc_iob ( xfer, len );
  331. if ( ! iobuf )
  332. return -ENOMEM;
  333. memcpy ( iob_put ( iobuf, len ), data, len );
  334. return xfer->op->deliver_iob ( xfer, iobuf, &dummy_metadata );
  335. }
  336. /**
  337. * Ignore datagram as raw data event
  338. *
  339. * @v xfer Data transfer interface
  340. * @v data Data buffer
  341. * @v len Length of data buffer
  342. * @ret rc Return status code
  343. */
  344. int ignore_xfer_deliver_raw ( struct xfer_interface *xfer,
  345. const void *data __unused, size_t len ) {
  346. DBGC ( xfer, "XFER %p %zd bytes delivered %s\n", xfer, len,
  347. ( ( xfer == &null_xfer ) ?
  348. "before connection" : "after termination" ) );
  349. return 0;
  350. }
  351. /** Null data transfer interface operations */
  352. struct xfer_interface_operations null_xfer_ops = {
  353. .close = ignore_xfer_close,
  354. .vredirect = ignore_xfer_vredirect,
  355. .window = unlimited_xfer_window,
  356. .alloc_iob = default_xfer_alloc_iob,
  357. .deliver_iob = xfer_deliver_as_raw,
  358. .deliver_raw = ignore_xfer_deliver_raw,
  359. };
  360. /**
  361. * Null data transfer interface
  362. *
  363. * This is the interface to which data transfer interfaces are
  364. * connected when unplugged. It will never generate messages, and
  365. * will silently absorb all received messages.
  366. */
  367. struct xfer_interface null_xfer = XFER_INIT ( &null_xfer_ops );