svc_xprt.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. {
  83. memset(xprt, 0, sizeof(*xprt));
  84. xprt->xpt_class = xcl;
  85. xprt->xpt_ops = xcl->xcl_ops;
  86. kref_init(&xprt->xpt_ref);
  87. }
  88. EXPORT_SYMBOL_GPL(svc_xprt_init);
  89. int svc_create_xprt(struct svc_serv *serv, char *xprt_name, unsigned short port,
  90. int flags)
  91. {
  92. struct svc_xprt_class *xcl;
  93. int ret = -ENOENT;
  94. struct sockaddr_in sin = {
  95. .sin_family = AF_INET,
  96. .sin_addr.s_addr = INADDR_ANY,
  97. .sin_port = htons(port),
  98. };
  99. dprintk("svc: creating transport %s[%d]\n", xprt_name, port);
  100. spin_lock(&svc_xprt_class_lock);
  101. list_for_each_entry(xcl, &svc_xprt_class_list, xcl_list) {
  102. if (strcmp(xprt_name, xcl->xcl_name) == 0) {
  103. spin_unlock(&svc_xprt_class_lock);
  104. if (try_module_get(xcl->xcl_owner)) {
  105. struct svc_xprt *newxprt;
  106. ret = 0;
  107. newxprt = xcl->xcl_ops->xpo_create
  108. (serv,
  109. (struct sockaddr *)&sin, sizeof(sin),
  110. flags);
  111. if (IS_ERR(newxprt)) {
  112. module_put(xcl->xcl_owner);
  113. ret = PTR_ERR(newxprt);
  114. }
  115. }
  116. goto out;
  117. }
  118. }
  119. spin_unlock(&svc_xprt_class_lock);
  120. dprintk("svc: transport %s not found\n", xprt_name);
  121. out:
  122. return ret;
  123. }
  124. EXPORT_SYMBOL_GPL(svc_create_xprt);