svc_raw.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * svc_raw.c, This a toy for simple testing and timing.
  3. * Interface to create an rpc client and server in the same UNIX process.
  4. * This lets us simulate rpc and get rpc (round trip) overhead, without
  5. * any interference from the kernel.
  6. *
  7. * Copyright (c) 2010, Oracle America, Inc.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions are
  11. * met:
  12. *
  13. * * Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * * Redistributions in binary form must reproduce the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer in the documentation and/or other materials
  18. * provided with the distribution.
  19. * * Neither the name of the "Oracle America, Inc." nor the names of its
  20. * contributors may be used to endorse or promote products derived
  21. * from this software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  24. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  25. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  26. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  27. * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  28. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  30. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  31. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  32. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  33. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  34. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. */
  36. #include <rpc/rpc.h>
  37. #include <rpc/svc.h>
  38. #include <shlib-compat.h>
  39. /*
  40. * This is the "network" that we will be moving data over
  41. */
  42. struct svcraw_private_s
  43. {
  44. char _raw_buf[UDPMSGSIZE];
  45. SVCXPRT server;
  46. XDR xdr_stream;
  47. char verf_body[MAX_AUTH_BYTES];
  48. };
  49. #define svcraw_private RPC_THREAD_VARIABLE(svcraw_private_s)
  50. static bool_t svcraw_recv (SVCXPRT *, struct rpc_msg *);
  51. static enum xprt_stat svcraw_stat (SVCXPRT *);
  52. static bool_t svcraw_getargs (SVCXPRT *, xdrproc_t, caddr_t);
  53. static bool_t svcraw_reply (SVCXPRT *, struct rpc_msg *);
  54. static bool_t svcraw_freeargs (SVCXPRT *, xdrproc_t, caddr_t);
  55. static void svcraw_destroy (SVCXPRT *);
  56. static const struct xp_ops server_ops =
  57. {
  58. svcraw_recv,
  59. svcraw_stat,
  60. svcraw_getargs,
  61. svcraw_reply,
  62. svcraw_freeargs,
  63. svcraw_destroy
  64. };
  65. SVCXPRT *
  66. svcraw_create (void)
  67. {
  68. struct svcraw_private_s *srp = svcraw_private;
  69. if (srp == 0)
  70. {
  71. srp = (struct svcraw_private_s *) calloc (1, sizeof (*srp));
  72. if (srp == 0)
  73. return NULL;
  74. }
  75. srp->server.xp_sock = 0;
  76. srp->server.xp_port = 0;
  77. srp->server.xp_ops = (struct xp_ops *) &server_ops;
  78. srp->server.xp_verf.oa_base = srp->verf_body;
  79. xdrmem_create (&srp->xdr_stream, srp->_raw_buf, UDPMSGSIZE, XDR_FREE);
  80. return &srp->server;
  81. }
  82. libc_hidden_nolink_sunrpc (svcraw_create, GLIBC_2_0)
  83. static enum xprt_stat
  84. svcraw_stat (SVCXPRT *xprt)
  85. {
  86. return XPRT_IDLE;
  87. }
  88. static bool_t
  89. svcraw_recv (SVCXPRT *xprt, struct rpc_msg *msg)
  90. {
  91. struct svcraw_private_s *srp = svcraw_private;
  92. XDR *xdrs;
  93. if (srp == 0)
  94. return FALSE;
  95. xdrs = &srp->xdr_stream;
  96. xdrs->x_op = XDR_DECODE;
  97. XDR_SETPOS (xdrs, 0);
  98. if (!xdr_callmsg (xdrs, msg))
  99. return FALSE;
  100. return TRUE;
  101. }
  102. static bool_t
  103. svcraw_reply (SVCXPRT *xprt, struct rpc_msg *msg)
  104. {
  105. struct svcraw_private_s *srp = svcraw_private;
  106. XDR *xdrs;
  107. if (srp == 0)
  108. return FALSE;
  109. xdrs = &srp->xdr_stream;
  110. xdrs->x_op = XDR_ENCODE;
  111. XDR_SETPOS (xdrs, 0);
  112. if (!xdr_replymsg (xdrs, msg))
  113. return FALSE;
  114. (void) XDR_GETPOS (xdrs); /* called just for overhead */
  115. return TRUE;
  116. }
  117. static bool_t
  118. svcraw_getargs (SVCXPRT *xprt, xdrproc_t xdr_args, caddr_t args_ptr)
  119. {
  120. struct svcraw_private_s *srp = svcraw_private;
  121. if (srp == 0)
  122. return FALSE;
  123. return (*xdr_args) (&srp->xdr_stream, args_ptr);
  124. }
  125. static bool_t
  126. svcraw_freeargs (SVCXPRT *xprt, xdrproc_t xdr_args, caddr_t args_ptr)
  127. {
  128. struct svcraw_private_s *srp = svcraw_private;
  129. XDR *xdrs;
  130. if (srp == 0)
  131. return FALSE;
  132. xdrs = &srp->xdr_stream;
  133. xdrs->x_op = XDR_FREE;
  134. return (*xdr_args) (xdrs, args_ptr);
  135. }
  136. static void
  137. svcraw_destroy (SVCXPRT *xprt)
  138. {
  139. }