geom_gate userland utility improvements
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.
 
 
 
 

199 lines
5.6 KiB

  1. /*-
  2. * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
  3. *
  4. * Copyright (c) 2004 Pawel Jakub Dawidek <pjd@FreeBSD.org>
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
  17. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  22. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  26. * SUCH DAMAGE.
  27. *
  28. * $FreeBSD$
  29. */
  30. #ifndef _GGATE_H_
  31. #define _GGATE_H_
  32. #include <sys/endian.h>
  33. #include <stdarg.h>
  34. #define G_GATE_PORT 3080
  35. #define G_GATE_RCVBUF 131072
  36. #define G_GATE_SNDBUF 131072
  37. #define G_GATE_QUEUE_SIZE 1024
  38. #define G_GATE_TIMEOUT 0
  39. #define GGATE_MAGIC "GEOM_GATE "
  40. #define GGATE_VERSION 0
  41. #define GGATE_FLAG_RDONLY 0x0001
  42. #define GGATE_FLAG_WRONLY 0x0002
  43. /*
  44. * If GGATE_FLAG_SEND not GGATE_FLAG_RECV flag is set, this is initial
  45. * connection.
  46. * If GGATE_FLAG_SEND flag is set - this is socket to send data.
  47. * If GGATE_FLAG_RECV flag is set - this is socket to receive data.
  48. */
  49. #define GGATE_FLAG_SEND 0x0004
  50. #define GGATE_FLAG_RECV 0x0008
  51. #define GGATE_CMD_READ 0
  52. #define GGATE_CMD_WRITE 1
  53. extern int g_gate_devfd;
  54. extern int g_gate_verbose;
  55. extern int nagle;
  56. extern unsigned rcvbuf, sndbuf;
  57. struct g_gate_version {
  58. char gv_magic[16];
  59. uint16_t gv_version;
  60. uint16_t gv_error;
  61. } __packed;
  62. /* Client's initial packet. */
  63. struct g_gate_cinit {
  64. char gc_path[PATH_MAX + 1];
  65. uint64_t gc_flags;
  66. uint16_t gc_nconn;
  67. uint32_t gc_token;
  68. } __packed;
  69. /* Server's initial packet. */
  70. struct g_gate_sinit {
  71. uint8_t gs_flags;
  72. uint64_t gs_mediasize;
  73. uint32_t gs_sectorsize;
  74. uint16_t gs_error;
  75. } __packed;
  76. /* Control struct. */
  77. struct g_gate_hdr {
  78. uint8_t gh_cmd; /* command */
  79. uint64_t gh_offset; /* device offset */
  80. uint32_t gh_length; /* size of block */
  81. uint64_t gh_seq; /* request number */
  82. uint16_t gh_error; /* error value (0 if ok) */
  83. } __packed;
  84. void g_gate_vlog(int priority, const char *message, va_list ap);
  85. void g_gate_log(int priority, const char *message, ...);
  86. void g_gate_xvlog(const char *message, va_list ap) __dead2;
  87. void g_gate_xlog(const char *message, ...) __dead2;
  88. off_t g_gate_mediasize(int fd);
  89. unsigned g_gate_sectorsize(int fd);
  90. void g_gate_open_device(void);
  91. void g_gate_close_device(void);
  92. void g_gate_ioctl(unsigned long req, void *data);
  93. void g_gate_destroy(int unit, int force);
  94. void g_gate_load_module(void);
  95. ssize_t g_gate_recv(int s, void *buf, size_t len, int flags);
  96. ssize_t g_gate_send(int s, const void *buf, size_t len, int flags);
  97. void g_gate_socket_settings(int sfd);
  98. #ifdef LIBGEOM
  99. void g_gate_list(int unit, int verbose);
  100. #endif
  101. in_addr_t g_gate_str2ip(const char *str);
  102. /*
  103. * g_gate_swap2h_* - functions swap bytes to host byte order (from big endian).
  104. * g_gate_swap2n_* - functions swap bytes to network byte order (actually
  105. * to big endian byte order).
  106. */
  107. static __inline void
  108. g_gate_swap2h_version(struct g_gate_version *ver)
  109. {
  110. ver->gv_version = be16toh(ver->gv_version);
  111. ver->gv_error = be16toh(ver->gv_error);
  112. }
  113. static __inline void
  114. g_gate_swap2n_version(struct g_gate_version *ver)
  115. {
  116. ver->gv_version = htobe16(ver->gv_version);
  117. ver->gv_error = htobe16(ver->gv_error);
  118. }
  119. static __inline void
  120. g_gate_swap2h_cinit(struct g_gate_cinit *cinit)
  121. {
  122. cinit->gc_flags = be64toh(cinit->gc_flags);
  123. cinit->gc_nconn = be16toh(cinit->gc_nconn);
  124. cinit->gc_token = be32toh(cinit->gc_token);
  125. }
  126. static __inline void
  127. g_gate_swap2n_cinit(struct g_gate_cinit *cinit)
  128. {
  129. cinit->gc_flags = htobe64(cinit->gc_flags);
  130. cinit->gc_nconn = htobe16(cinit->gc_nconn);
  131. cinit->gc_token = htobe32(cinit->gc_token);
  132. }
  133. static __inline void
  134. g_gate_swap2h_sinit(struct g_gate_sinit *sinit)
  135. {
  136. /* Swap only used fields. */
  137. sinit->gs_mediasize = be64toh(sinit->gs_mediasize);
  138. sinit->gs_sectorsize = be32toh(sinit->gs_sectorsize);
  139. sinit->gs_error = be16toh(sinit->gs_error);
  140. }
  141. static __inline void
  142. g_gate_swap2n_sinit(struct g_gate_sinit *sinit)
  143. {
  144. /* Swap only used fields. */
  145. sinit->gs_mediasize = htobe64(sinit->gs_mediasize);
  146. sinit->gs_sectorsize = htobe32(sinit->gs_sectorsize);
  147. sinit->gs_error = htobe16(sinit->gs_error);
  148. }
  149. static __inline void
  150. g_gate_swap2h_hdr(struct g_gate_hdr *hdr)
  151. {
  152. /* Swap only used fields. */
  153. hdr->gh_offset = be64toh(hdr->gh_offset);
  154. hdr->gh_length = be32toh(hdr->gh_length);
  155. hdr->gh_seq = be64toh(hdr->gh_seq);
  156. hdr->gh_error = be16toh(hdr->gh_error);
  157. }
  158. static __inline void
  159. g_gate_swap2n_hdr(struct g_gate_hdr *hdr)
  160. {
  161. /* Swap only used fields. */
  162. hdr->gh_offset = htobe64(hdr->gh_offset);
  163. hdr->gh_length = htobe32(hdr->gh_length);
  164. hdr->gh_seq = htobe64(hdr->gh_seq);
  165. hdr->gh_error = htobe16(hdr->gh_error);
  166. }
  167. #endif /* _GGATE_H_ */