svc_xprt.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * linux/net/sunrpc/svc_xprt.c
  3. *
  4. * Author: Tom Tucker <tom@opengridcomputing.com>
  5. */
  6. #include <linux/sched.h>
  7. #include <linux/errno.h>
  8. #include <linux/fcntl.h>
  9. #include <linux/net.h>
  10. #include <linux/in.h>
  11. #include <linux/inet.h>
  12. #include <linux/udp.h>
  13. #include <linux/tcp.h>
  14. #include <linux/unistd.h>
  15. #include <linux/slab.h>
  16. #include <linux/netdevice.h>
  17. #include <linux/skbuff.h>
  18. #include <linux/file.h>
  19. #include <linux/freezer.h>
  20. #include <net/sock.h>
  21. #include <net/checksum.h>
  22. #include <net/ip.h>
  23. #include <net/ipv6.h>
  24. #include <net/tcp_states.h>
  25. #include <linux/uaccess.h>
  26. #include <asm/ioctls.h>
  27. #include <linux/sunrpc/types.h>
  28. #include <linux/sunrpc/clnt.h>
  29. #include <linux/sunrpc/xdr.h>
  30. #include <linux/sunrpc/svcsock.h>
  31. #include <linux/sunrpc/stats.h>
  32. #include <linux/sunrpc/svc_xprt.h>
  33. #define RPCDBG_FACILITY RPCDBG_SVCXPRT
  34. /* List of registered transport classes */
  35. static DEFINE_SPINLOCK(svc_xprt_class_lock);
  36. static LIST_HEAD(svc_xprt_class_list);
  37. int svc_reg_xprt_class(struct svc_xprt_class *xcl)
  38. {
  39. struct svc_xprt_class *cl;
  40. int res = -EEXIST;
  41. dprintk("svc: Adding svc transport class '%s'\n", xcl->xcl_name);
  42. INIT_LIST_HEAD(&xcl->xcl_list);
  43. spin_lock(&svc_xprt_class_lock);
  44. /* Make sure there isn't already a class with the same name */
  45. list_for_each_entry(cl, &svc_xprt_class_list, xcl_list) {
  46. if (strcmp(xcl->xcl_name, cl->xcl_name) == 0)
  47. goto out;
  48. }
  49. list_add_tail(&xcl->xcl_list, &svc_xprt_class_list);
  50. res = 0;
  51. out:
  52. spin_unlock(&svc_xprt_class_lock);
  53. return res;
  54. }
  55. EXPORT_SYMBOL_GPL(svc_reg_xprt_class);
  56. void svc_unreg_xprt_class(struct svc_xprt_class *xcl)
  57. {
  58. dprintk("svc: Removing svc transport class '%s'\n", xcl->xcl_name);
  59. spin_lock(&svc_xprt_class_lock);
  60. list_del_init(&xcl->xcl_list);
  61. spin_unlock(&svc_xprt_class_lock);
  62. }
  63. EXPORT_SYMBOL_GPL(svc_unreg_xprt_class);
  64. static void svc_xprt_free(struct kref *kref)
  65. {
  66. struct svc_xprt *xprt =
  67. container_of(kref, struct svc_xprt, xpt_ref);
  68. struct module *owner = xprt->xpt_class->xcl_owner;
  69. xprt->xpt_ops->xpo_free(xprt);
  70. module_put(owner);
  71. }
  72. void svc_xprt_put(struct svc_xprt *xprt)
  73. {
  74. kref_put(&xprt->xpt_ref, svc_xprt_free);
  75. }
  76. EXPORT_SYMBOL_GPL(svc_xprt_put);
  77. /*
  78. * Called by transport drivers to initialize the transport independent
  79. * portion of the transport instance.
  80. */
  81. void svc_xprt_init(struct svc_xprt_class *xcl, struct svc_xprt *xprt,
  82. struct svc_serv *serv)
  83. {
  84. memset(xprt, 0, sizeof(*xprt));
  85. xprt->xpt_class = xcl;
  86. xprt->xpt_ops = xcl->xcl_ops;
  87. kref_init(&xprt->xpt_ref);
  88. xprt->xpt_server = serv;
  89. INIT_LIST_HEAD(&xprt->xpt_list);
  90. INIT_LIST_HEAD(&xprt->xpt_ready);
  91. mutex_init(&xprt->xpt_mutex);
  92. }
  93. EXPORT_SYMBOL_GPL(svc_xprt_init);
  94. int svc_create_xprt(struct svc_serv *serv, char *xprt_name, unsigned short port,
  95. int flags)
  96. {
  97. struct svc_xprt_class *xcl;
  98. int ret = -ENOENT;
  99. struct sockaddr_in sin = {
  100. .sin_family = AF_INET,
  101. .sin_addr.s_addr = INADDR_ANY,
  102. .sin_port = htons(port),
  103. };
  104. dprintk("svc: creating transport %s[%d]\n", xprt_name, port);
  105. spin_lock(&svc_xprt_class_lock);
  106. list_for_each_entry(xcl, &svc_xprt_class_list, xcl_list) {
  107. if (strcmp(xprt_name, xcl->xcl_name) == 0) {
  108. spin_unlock(&svc_xprt_class_lock);
  109. if (try_module_get(xcl->xcl_owner)) {
  110. struct svc_xprt *newxprt;
  111. ret = 0;
  112. newxprt = xcl->xcl_ops->xpo_create
  113. (serv,
  114. (struct sockaddr *)&sin, sizeof(sin),
  115. flags);
  116. if (IS_ERR(newxprt)) {
  117. module_put(xcl->xcl_owner);
  118. ret = PTR_ERR(newxprt);
  119. }
  120. }
  121. goto out;
  122. }
  123. }
  124. spin_unlock(&svc_xprt_class_lock);
  125. dprintk("svc: transport %s not found\n", xprt_name);
  126. out:
  127. return ret;
  128. }
  129. EXPORT_SYMBOL_GPL(svc_create_xprt);