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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. intf_plug ( &tmp, dest );
  78. rc = xfer_vreopen ( dest, type, args );
  79. if ( rc == 0 ) {
  80. xfer_window_changed ( dest );
  81. xfer_window_changed ( &tmp );
  82. }
  83. intf_unplug ( &tmp );
  84. }
  85. if ( rc != 0 ) {
  86. DBGC ( INTF_COL ( intf ), "INTF " INTF_INTF_FMT " redirect "
  87. "failed: %s\n", INTF_INTF_DBG ( intf, dest ),
  88. strerror ( rc ) );
  89. }
  90. intf_put ( dest );
  91. return rc;
  92. }
  93. /**
  94. * Check flow control window
  95. *
  96. * @v intf Data transfer interface
  97. * @ret len Length of window
  98. */
  99. size_t xfer_window ( struct interface *intf ) {
  100. struct interface *dest;
  101. xfer_window_TYPE ( void * ) *op =
  102. intf_get_dest_op ( intf, xfer_window, &dest );
  103. void *object = intf_object ( dest );
  104. size_t len;
  105. if ( op ) {
  106. len = op ( object );
  107. } else {
  108. /* Default is to provide an unlimited window */
  109. len = ~( ( size_t ) 0 );
  110. }
  111. intf_put ( dest );
  112. return len;
  113. }
  114. /**
  115. * Report change of flow control window
  116. *
  117. * @v intf Data transfer interface
  118. *
  119. * Note that this method is used to indicate only unsolicited changes
  120. * in the flow control window. In particular, this method must not be
  121. * called as part of the response to xfer_deliver(), since that could
  122. * easily lead to an infinite loop. Callers of xfer_deliver() should
  123. * assume that the flow control window will have changed without
  124. * generating an xfer_window_changed() message.
  125. */
  126. void xfer_window_changed ( struct interface *intf ) {
  127. intf_poke ( intf, xfer_window_changed );
  128. }
  129. /**
  130. * Allocate I/O buffer
  131. *
  132. * @v intf Data transfer interface
  133. * @v len I/O buffer payload length
  134. * @ret iobuf I/O buffer
  135. */
  136. struct io_buffer * xfer_alloc_iob ( struct interface *intf, size_t len ) {
  137. struct interface *dest;
  138. xfer_alloc_iob_TYPE ( void * ) *op =
  139. intf_get_dest_op ( intf, xfer_alloc_iob, &dest );
  140. void *object = intf_object ( dest );
  141. struct io_buffer *iobuf;
  142. DBGC ( INTF_COL ( intf ), "INTF " INTF_INTF_FMT " alloc_iob %zd\n",
  143. INTF_INTF_DBG ( intf, dest ), len );
  144. if ( op ) {
  145. iobuf = op ( object, len );
  146. } else {
  147. /* Default is to allocate an I/O buffer with no
  148. * reserved space.
  149. */
  150. iobuf = alloc_iob ( len );
  151. }
  152. if ( ! iobuf ) {
  153. DBGC ( INTF_COL ( intf ), "INTF " INTF_INTF_FMT " alloc_iob "
  154. "failed\n", INTF_INTF_DBG ( intf, dest ) );
  155. }
  156. intf_put ( dest );
  157. return iobuf;
  158. }
  159. /**
  160. * Deliver datagram
  161. *
  162. * @v intf Data transfer interface
  163. * @v iobuf Datagram I/O buffer
  164. * @v meta Data transfer metadata
  165. * @ret rc Return status code
  166. */
  167. int xfer_deliver ( struct interface *intf,
  168. struct io_buffer *iobuf,
  169. struct xfer_metadata *meta ) {
  170. struct interface *dest;
  171. xfer_deliver_TYPE ( void * ) *op =
  172. intf_get_dest_op ( intf, xfer_deliver, &dest );
  173. void *object = intf_object ( dest );
  174. int rc;
  175. DBGC ( INTF_COL ( intf ), "INTF " INTF_INTF_FMT " deliver %zd\n",
  176. INTF_INTF_DBG ( intf, dest ), iob_len ( iobuf ) );
  177. if ( op ) {
  178. rc = op ( object, iobuf, meta );
  179. } else {
  180. /* Default is to discard the I/O buffer */
  181. free_iob ( iobuf );
  182. rc = -EPIPE;
  183. }
  184. if ( rc != 0 ) {
  185. DBGC ( INTF_COL ( intf ), "INTF " INTF_INTF_FMT
  186. " deliver failed: %s\n",
  187. INTF_INTF_DBG ( intf, dest ), strerror ( rc ) );
  188. }
  189. intf_put ( dest );
  190. return rc;
  191. }
  192. /*****************************************************************************
  193. *
  194. * Data transfer interface helper functions
  195. *
  196. */
  197. /**
  198. * Send redirection event
  199. *
  200. * @v intf Data transfer interface
  201. * @v type New location type
  202. * @v ... Remaining arguments depend upon location type
  203. * @ret rc Return status code
  204. */
  205. int xfer_redirect ( struct interface *intf, int type, ... ) {
  206. va_list args;
  207. int rc;
  208. va_start ( args, type );
  209. rc = xfer_vredirect ( intf, type, args );
  210. va_end ( args );
  211. return rc;
  212. }
  213. /**
  214. * Deliver datagram as I/O buffer without metadata
  215. *
  216. * @v intf Data transfer interface
  217. * @v iobuf Datagram I/O buffer
  218. * @ret rc Return status code
  219. */
  220. int xfer_deliver_iob ( struct interface *intf, struct io_buffer *iobuf ) {
  221. return xfer_deliver ( intf, iobuf, &dummy_metadata );
  222. }
  223. /**
  224. * Deliver datagram as raw data
  225. *
  226. * @v intf Data transfer interface
  227. * @v data Data
  228. * @v len Length of data
  229. * @v meta Data transfer metadata
  230. * @ret rc Return status code
  231. */
  232. int xfer_deliver_raw_meta ( struct interface *intf, const void *data,
  233. size_t len, struct xfer_metadata *meta ) {
  234. struct io_buffer *iobuf;
  235. iobuf = xfer_alloc_iob ( intf, len );
  236. if ( ! iobuf )
  237. return -ENOMEM;
  238. memcpy ( iob_put ( iobuf, len ), data, len );
  239. return xfer_deliver ( intf, iobuf, meta );
  240. }
  241. /**
  242. * Deliver datagram as raw data without metadata
  243. *
  244. * @v intf Data transfer interface
  245. * @v data Data
  246. * @v len Length of data
  247. * @ret rc Return status code
  248. */
  249. int xfer_deliver_raw ( struct interface *intf, const void *data, size_t len ) {
  250. return xfer_deliver_raw_meta ( intf, data, len, &dummy_metadata );
  251. }
  252. /**
  253. * Deliver formatted string
  254. *
  255. * @v intf Data transfer interface
  256. * @v format Format string
  257. * @v args Arguments corresponding to the format string
  258. * @ret rc Return status code
  259. */
  260. int xfer_vprintf ( struct interface *intf, const char *format,
  261. va_list args ) {
  262. va_list args_tmp;
  263. char *buf;
  264. int len;
  265. int rc;
  266. /* Create temporary string */
  267. va_copy ( args_tmp, args );
  268. len = vasprintf ( &buf, format, args );
  269. if ( len < 0 ) {
  270. rc = len;
  271. goto err_asprintf;
  272. }
  273. va_end ( args_tmp );
  274. /* Transmit string */
  275. if ( ( rc = xfer_deliver_raw ( intf, buf, len ) ) != 0 )
  276. goto err_deliver;
  277. err_deliver:
  278. free ( buf );
  279. err_asprintf:
  280. return rc;
  281. }
  282. /**
  283. * Deliver formatted string
  284. *
  285. * @v intf Data transfer interface
  286. * @v format Format string
  287. * @v ... Arguments corresponding to the format string
  288. * @ret rc Return status code
  289. */
  290. int xfer_printf ( struct interface *intf, const char *format, ... ) {
  291. va_list args;
  292. int rc;
  293. va_start ( args, format );
  294. rc = xfer_vprintf ( intf, format, args );
  295. va_end ( args );
  296. return rc;
  297. }
  298. /**
  299. * Seek to position
  300. *
  301. * @v intf Data transfer interface
  302. * @v offset Offset to new position
  303. * @ret rc Return status code
  304. */
  305. int xfer_seek ( struct interface *intf, off_t offset ) {
  306. struct io_buffer *iobuf;
  307. struct xfer_metadata meta = {
  308. .flags = XFER_FL_ABS_OFFSET,
  309. .offset = offset,
  310. };
  311. DBGC ( INTF_COL ( intf ), "INTF " INTF_FMT " seek to %ld\n",
  312. INTF_DBG ( intf ), offset );
  313. /* Allocate and send a zero-length data buffer */
  314. iobuf = xfer_alloc_iob ( intf, 0 );
  315. if ( ! iobuf )
  316. return -ENOMEM;
  317. return xfer_deliver ( intf, iobuf, &meta );
  318. }
  319. /**
  320. * Check that data is delivered strictly in order
  321. *
  322. * @v meta Data transfer metadata
  323. * @v pos Current position
  324. * @v len Length of data
  325. * @ret rc Return status code
  326. */
  327. int xfer_check_order ( struct xfer_metadata *meta, size_t *pos, size_t len ) {
  328. size_t new_pos;
  329. /* Allow out-of-order zero-length packets (as used by xfer_seek()) */
  330. if ( len == 0 )
  331. return 0;
  332. /* Calculate position of this delivery */
  333. new_pos = *pos;
  334. if ( meta->flags & XFER_FL_ABS_OFFSET )
  335. new_pos = 0;
  336. new_pos += meta->offset;
  337. /* Fail if delivery position is not equal to current position */
  338. if ( new_pos != *pos )
  339. return -EPROTO;
  340. /* Update current position */
  341. *pos += len;
  342. return 0;
  343. }