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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. */
  19. FILE_LICENCE ( GPL2_OR_LATER );
  20. #include <string.h>
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <errno.h>
  24. #include <ipxe/iobuf.h>
  25. #include <ipxe/xfer.h>
  26. #include <ipxe/open.h>
  27. /** @file
  28. *
  29. * Data transfer interfaces
  30. *
  31. */
  32. /**
  33. * Dummy transfer metadata
  34. *
  35. * This gets passed to xfer_interface::deliver() and equivalents when
  36. * no metadata is available.
  37. */
  38. static struct xfer_metadata dummy_metadata;
  39. /*****************************************************************************
  40. *
  41. * Data transfer interface operations
  42. *
  43. */
  44. /**
  45. * Send redirection event
  46. *
  47. * @v intf Data transfer interface
  48. * @v type New location type
  49. * @v args Remaining arguments depend upon location type
  50. * @ret rc Return status code
  51. */
  52. int xfer_vredirect ( struct interface *intf, int type, va_list args ) {
  53. struct interface tmp = INTF_INIT ( null_intf_desc );
  54. struct interface *dest;
  55. xfer_vredirect_TYPE ( void * ) *op =
  56. intf_get_dest_op_no_passthru ( intf, xfer_vredirect, &dest );
  57. void *object = intf_object ( dest );
  58. int rc;
  59. DBGC ( INTF_COL ( intf ), "INTF " INTF_INTF_FMT " redirect\n",
  60. INTF_INTF_DBG ( intf, dest ) );
  61. if ( op ) {
  62. rc = op ( object, type, args );
  63. } else {
  64. /* Default is to reopen the interface as instructed,
  65. * then send xfer_window_changed() messages to both
  66. * new child and parent interfaces. Since our
  67. * original child interface is likely to be closed and
  68. * unplugged as a result of the call to
  69. * xfer_vreopen(), we create a temporary interface in
  70. * order to be able to send xfer_window_changed() to
  71. * the parent.
  72. */
  73. intf_plug ( &tmp, dest );
  74. rc = xfer_vreopen ( dest, type, args );
  75. if ( rc == 0 ) {
  76. xfer_window_changed ( dest );
  77. xfer_window_changed ( &tmp );
  78. }
  79. intf_unplug ( &tmp );
  80. }
  81. if ( rc != 0 ) {
  82. DBGC ( INTF_COL ( intf ), "INTF " INTF_INTF_FMT " redirect "
  83. "failed: %s\n", INTF_INTF_DBG ( intf, dest ),
  84. strerror ( rc ) );
  85. }
  86. intf_put ( dest );
  87. return rc;
  88. }
  89. /**
  90. * Check flow control window
  91. *
  92. * @v intf Data transfer interface
  93. * @ret len Length of window
  94. */
  95. size_t xfer_window ( struct interface *intf ) {
  96. struct interface *dest;
  97. xfer_window_TYPE ( void * ) *op =
  98. intf_get_dest_op ( intf, xfer_window, &dest );
  99. void *object = intf_object ( dest );
  100. size_t len;
  101. if ( op ) {
  102. len = op ( object );
  103. } else {
  104. /* Default is to provide an unlimited window */
  105. len = ~( ( size_t ) 0 );
  106. }
  107. intf_put ( dest );
  108. return len;
  109. }
  110. /**
  111. * Report change of flow control window
  112. *
  113. * @v intf Data transfer interface
  114. *
  115. * Note that this method is used to indicate only unsolicited changes
  116. * in the flow control window. In particular, this method must not be
  117. * called as part of the response to xfer_deliver(), since that could
  118. * easily lead to an infinite loop. Callers of xfer_deliver() should
  119. * assume that the flow control window will have changed without
  120. * generating an xfer_window_changed() message.
  121. */
  122. void xfer_window_changed ( struct interface *intf ) {
  123. struct interface *dest;
  124. xfer_window_changed_TYPE ( void * ) *op =
  125. intf_get_dest_op ( intf, xfer_window_changed, &dest );
  126. void *object = intf_object ( dest );
  127. if ( op ) {
  128. op ( object );
  129. } else {
  130. /* Default is to do nothing */
  131. }
  132. intf_put ( dest );
  133. }
  134. /**
  135. * Allocate I/O buffer
  136. *
  137. * @v intf Data transfer interface
  138. * @v len I/O buffer payload length
  139. * @ret iobuf I/O buffer
  140. */
  141. struct io_buffer * xfer_alloc_iob ( struct interface *intf, size_t len ) {
  142. struct interface *dest;
  143. xfer_alloc_iob_TYPE ( void * ) *op =
  144. intf_get_dest_op ( intf, xfer_alloc_iob, &dest );
  145. void *object = intf_object ( dest );
  146. struct io_buffer *iobuf;
  147. DBGC ( INTF_COL ( intf ), "INTF " INTF_INTF_FMT " alloc_iob %zd\n",
  148. INTF_INTF_DBG ( intf, dest ), len );
  149. if ( op ) {
  150. iobuf = op ( object, len );
  151. } else {
  152. /* Default is to allocate an I/O buffer with no
  153. * reserved space.
  154. */
  155. iobuf = alloc_iob ( len );
  156. }
  157. if ( ! iobuf ) {
  158. DBGC ( INTF_COL ( intf ), "INTF " INTF_INTF_FMT " alloc_iob "
  159. "failed\n", INTF_INTF_DBG ( intf, dest ) );
  160. }
  161. intf_put ( dest );
  162. return iobuf;
  163. }
  164. /**
  165. * Deliver datagram
  166. *
  167. * @v intf Data transfer interface
  168. * @v iobuf Datagram I/O buffer
  169. * @v meta Data transfer metadata
  170. * @ret rc Return status code
  171. */
  172. int xfer_deliver ( struct interface *intf,
  173. struct io_buffer *iobuf,
  174. struct xfer_metadata *meta ) {
  175. struct interface *dest;
  176. xfer_deliver_TYPE ( void * ) *op =
  177. intf_get_dest_op ( intf, xfer_deliver, &dest );
  178. void *object = intf_object ( dest );
  179. int rc;
  180. DBGC ( INTF_COL ( intf ), "INTF " INTF_INTF_FMT " deliver %zd\n",
  181. INTF_INTF_DBG ( intf, dest ), iob_len ( iobuf ) );
  182. if ( op ) {
  183. rc = op ( object, iobuf, meta );
  184. } else {
  185. /* Default is to discard the I/O buffer */
  186. free_iob ( iobuf );
  187. rc = -EPIPE;
  188. }
  189. if ( rc != 0 ) {
  190. DBGC ( INTF_COL ( intf ), "INTF " INTF_INTF_FMT
  191. " deliver failed: %s\n",
  192. INTF_INTF_DBG ( intf, dest ), strerror ( rc ) );
  193. }
  194. intf_put ( dest );
  195. return rc;
  196. }
  197. /*****************************************************************************
  198. *
  199. * Data transfer interface helper functions
  200. *
  201. */
  202. /**
  203. * Send redirection event
  204. *
  205. * @v intf Data transfer interface
  206. * @v type New location type
  207. * @v ... Remaining arguments depend upon location type
  208. * @ret rc Return status code
  209. */
  210. int xfer_redirect ( struct interface *intf, int type, ... ) {
  211. va_list args;
  212. int rc;
  213. va_start ( args, type );
  214. rc = xfer_vredirect ( intf, type, args );
  215. va_end ( args );
  216. return rc;
  217. }
  218. /**
  219. * Deliver datagram as I/O buffer without metadata
  220. *
  221. * @v intf Data transfer interface
  222. * @v iobuf Datagram I/O buffer
  223. * @ret rc Return status code
  224. */
  225. int xfer_deliver_iob ( struct interface *intf, struct io_buffer *iobuf ) {
  226. return xfer_deliver ( intf, iobuf, &dummy_metadata );
  227. }
  228. /**
  229. * Deliver datagram as raw data
  230. *
  231. * @v intf Data transfer interface
  232. * @v data Data
  233. * @v len Length of data
  234. * @v meta Data transfer metadata
  235. * @ret rc Return status code
  236. */
  237. int xfer_deliver_raw_meta ( struct interface *intf, const void *data,
  238. size_t len, struct xfer_metadata *meta ) {
  239. struct io_buffer *iobuf;
  240. iobuf = xfer_alloc_iob ( intf, len );
  241. if ( ! iobuf )
  242. return -ENOMEM;
  243. memcpy ( iob_put ( iobuf, len ), data, len );
  244. return xfer_deliver ( intf, iobuf, meta );
  245. }
  246. /**
  247. * Deliver datagram as raw data without metadata
  248. *
  249. * @v intf Data transfer interface
  250. * @v data Data
  251. * @v len Length of data
  252. * @ret rc Return status code
  253. */
  254. int xfer_deliver_raw ( struct interface *intf, const void *data, size_t len ) {
  255. return xfer_deliver_raw_meta ( intf, data, len, &dummy_metadata );
  256. }
  257. /**
  258. * Deliver formatted string
  259. *
  260. * @v intf Data transfer interface
  261. * @v format Format string
  262. * @v args Arguments corresponding to the format string
  263. * @ret rc Return status code
  264. */
  265. int xfer_vprintf ( struct interface *intf, const char *format,
  266. va_list args ) {
  267. va_list args_tmp;
  268. char *buf;
  269. int len;
  270. int rc;
  271. /* Create temporary string */
  272. va_copy ( args_tmp, args );
  273. len = vasprintf ( &buf, format, args );
  274. if ( len < 0 ) {
  275. rc = len;
  276. goto err_asprintf;
  277. }
  278. va_end ( args_tmp );
  279. /* Transmit string */
  280. if ( ( rc = xfer_deliver_raw ( intf, buf, len ) ) != 0 )
  281. goto err_deliver;
  282. err_deliver:
  283. free ( buf );
  284. err_asprintf:
  285. return rc;
  286. }
  287. /**
  288. * Deliver formatted string
  289. *
  290. * @v intf Data transfer interface
  291. * @v format Format string
  292. * @v ... Arguments corresponding to the format string
  293. * @ret rc Return status code
  294. */
  295. int xfer_printf ( struct interface *intf, const char *format, ... ) {
  296. va_list args;
  297. int rc;
  298. va_start ( args, format );
  299. rc = xfer_vprintf ( intf, format, args );
  300. va_end ( args );
  301. return rc;
  302. }
  303. /**
  304. * Seek to position
  305. *
  306. * @v intf Data transfer interface
  307. * @v offset Offset to new position
  308. * @ret rc Return status code
  309. */
  310. int xfer_seek ( struct interface *intf, off_t offset ) {
  311. struct io_buffer *iobuf;
  312. struct xfer_metadata meta = {
  313. .flags = XFER_FL_ABS_OFFSET,
  314. .offset = offset,
  315. };
  316. DBGC ( INTF_COL ( intf ), "INTF " INTF_FMT " seek to %ld\n",
  317. INTF_DBG ( intf ), offset );
  318. /* Allocate and send a zero-length data buffer */
  319. iobuf = xfer_alloc_iob ( intf, 0 );
  320. if ( ! iobuf )
  321. return -ENOMEM;
  322. return xfer_deliver ( intf, iobuf, &meta );
  323. }