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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <string.h>
  25. #include <stdlib.h>
  26. #include <stdio.h>
  27. #include <errno.h>
  28. #include <ipxe/iobuf.h>
  29. #include <ipxe/xfer.h>
  30. #include <ipxe/open.h>
  31. /** @file
  32. *
  33. * Data transfer interfaces
  34. *
  35. */
  36. /**
  37. * Dummy transfer metadata
  38. *
  39. * This gets passed to xfer_interface::deliver() and equivalents when
  40. * no metadata is available.
  41. */
  42. static struct xfer_metadata dummy_metadata;
  43. /*****************************************************************************
  44. *
  45. * Data transfer interface operations
  46. *
  47. */
  48. /**
  49. * Send redirection event
  50. *
  51. * @v intf Data transfer interface
  52. * @v type New location type
  53. * @v args Remaining arguments depend upon location type
  54. * @ret rc Return status code
  55. */
  56. int xfer_vredirect ( struct interface *intf, int type, va_list args ) {
  57. struct interface tmp = INTF_INIT ( null_intf_desc );
  58. struct interface *dest;
  59. xfer_vredirect_TYPE ( void * ) *op =
  60. intf_get_dest_op_no_passthru ( intf, xfer_vredirect, &dest );
  61. void *object = intf_object ( dest );
  62. int rc;
  63. DBGC ( INTF_COL ( intf ), "INTF " INTF_INTF_FMT " redirect\n",
  64. INTF_INTF_DBG ( intf, dest ) );
  65. if ( op ) {
  66. rc = op ( object, type, args );
  67. } else {
  68. /* Default is to reopen the interface as instructed,
  69. * then send xfer_window_changed() messages to both
  70. * new child and parent interfaces. Since our
  71. * original child interface is likely to be closed and
  72. * unplugged as a result of the call to
  73. * xfer_vreopen(), we create a temporary interface in
  74. * order to be able to send xfer_window_changed() to
  75. * the parent.
  76. *
  77. * If redirection fails, then send intf_close() to the
  78. * parent interface.
  79. */
  80. intf_plug ( &tmp, dest );
  81. rc = xfer_vreopen ( dest, type, args );
  82. if ( rc == 0 ) {
  83. xfer_window_changed ( dest );
  84. xfer_window_changed ( &tmp );
  85. } else {
  86. intf_close ( &tmp, rc );
  87. }
  88. intf_unplug ( &tmp );
  89. }
  90. if ( rc != 0 ) {
  91. DBGC ( INTF_COL ( intf ), "INTF " INTF_INTF_FMT " redirect "
  92. "failed: %s\n", INTF_INTF_DBG ( intf, dest ),
  93. strerror ( rc ) );
  94. }
  95. intf_put ( dest );
  96. return rc;
  97. }
  98. /**
  99. * Check flow control window
  100. *
  101. * @v intf Data transfer interface
  102. * @ret len Length of window
  103. */
  104. size_t xfer_window ( struct interface *intf ) {
  105. struct interface *dest;
  106. xfer_window_TYPE ( void * ) *op =
  107. intf_get_dest_op ( intf, xfer_window, &dest );
  108. void *object = intf_object ( dest );
  109. size_t len;
  110. if ( op ) {
  111. len = op ( object );
  112. } else {
  113. /* Default is to provide an unlimited window */
  114. len = ~( ( size_t ) 0 );
  115. }
  116. intf_put ( dest );
  117. return len;
  118. }
  119. /**
  120. * Report change of flow control window
  121. *
  122. * @v intf Data transfer interface
  123. *
  124. * Note that this method is used to indicate only unsolicited changes
  125. * in the flow control window. In particular, this method must not be
  126. * called as part of the response to xfer_deliver(), since that could
  127. * easily lead to an infinite loop. Callers of xfer_deliver() should
  128. * assume that the flow control window will have changed without
  129. * generating an xfer_window_changed() message.
  130. */
  131. void xfer_window_changed ( struct interface *intf ) {
  132. intf_poke ( intf, xfer_window_changed );
  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. va_end ( args_tmp );
  275. if ( len < 0 ) {
  276. rc = len;
  277. goto err_asprintf;
  278. }
  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. }
  324. /**
  325. * Check that data is delivered strictly in order
  326. *
  327. * @v meta Data transfer metadata
  328. * @v pos Current position
  329. * @v len Length of data
  330. * @ret rc Return status code
  331. */
  332. int xfer_check_order ( struct xfer_metadata *meta, size_t *pos, size_t len ) {
  333. size_t new_pos;
  334. /* Allow out-of-order zero-length packets (as used by xfer_seek()) */
  335. if ( len == 0 )
  336. return 0;
  337. /* Calculate position of this delivery */
  338. new_pos = *pos;
  339. if ( meta->flags & XFER_FL_ABS_OFFSET )
  340. new_pos = 0;
  341. new_pos += meta->offset;
  342. /* Fail if delivery position is not equal to current position */
  343. if ( new_pos != *pos )
  344. return -EPROTO;
  345. /* Update current position */
  346. *pos += len;
  347. return 0;
  348. }