Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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