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.
 
 
 
 

410 lines
9.2 KiB

  1. /*-
  2. * Copyright (c) 2004 Pawel Jakub Dawidek <pjd@FreeBSD.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
  15. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
  18. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  19. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  20. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  21. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  22. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  23. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  24. * SUCH DAMAGE.
  25. *
  26. * $FreeBSD$
  27. */
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <unistd.h>
  31. #include <fcntl.h>
  32. #include <sys/param.h>
  33. #include <sys/disk.h>
  34. #include <sys/stat.h>
  35. #include <sys/endian.h>
  36. #include <sys/socket.h>
  37. #include <sys/linker.h>
  38. #include <sys/module.h>
  39. #include <netinet/in.h>
  40. #include <netinet/tcp.h>
  41. #include <arpa/inet.h>
  42. #include <signal.h>
  43. #include <err.h>
  44. #include <errno.h>
  45. #include <string.h>
  46. #include <strings.h>
  47. #include <libgen.h>
  48. #include <libutil.h>
  49. #include <netdb.h>
  50. #include <syslog.h>
  51. #include <stdarg.h>
  52. #include <stdint.h>
  53. #include <libgeom.h>
  54. #include <geom/gate/g_gate.h>
  55. #include "ggate.h"
  56. int g_gate_devfd = -1;
  57. int g_gate_verbose = 0;
  58. void
  59. g_gate_vlog(int priority, const char *message, va_list ap)
  60. {
  61. if (g_gate_verbose) {
  62. const char *prefix;
  63. switch (priority) {
  64. case LOG_ERR:
  65. prefix = "error";
  66. break;
  67. case LOG_WARNING:
  68. prefix = "warning";
  69. break;
  70. case LOG_NOTICE:
  71. prefix = "notice";
  72. break;
  73. case LOG_INFO:
  74. prefix = "info";
  75. break;
  76. case LOG_DEBUG:
  77. prefix = "debug";
  78. break;
  79. default:
  80. prefix = "unknown";
  81. }
  82. printf("%s: ", prefix);
  83. vprintf(message, ap);
  84. printf("\n");
  85. } else {
  86. if (priority != LOG_DEBUG)
  87. vsyslog(priority, message, ap);
  88. }
  89. }
  90. void
  91. g_gate_log(int priority, const char *message, ...)
  92. {
  93. va_list ap;
  94. va_start(ap, message);
  95. g_gate_vlog(priority, message, ap);
  96. va_end(ap);
  97. }
  98. void
  99. g_gate_xvlog(const char *message, va_list ap)
  100. {
  101. g_gate_vlog(LOG_ERR, message, ap);
  102. g_gate_vlog(LOG_ERR, "Exiting.", ap);
  103. exit(EXIT_FAILURE);
  104. }
  105. void
  106. g_gate_xlog(const char *message, ...)
  107. {
  108. va_list ap;
  109. va_start(ap, message);
  110. g_gate_xvlog(message, ap);
  111. /* NOTREACHED */
  112. va_end(ap);
  113. exit(EXIT_FAILURE);
  114. }
  115. off_t
  116. g_gate_mediasize(int fd)
  117. {
  118. off_t mediasize;
  119. struct stat sb;
  120. if (fstat(fd, &sb) == -1)
  121. g_gate_xlog("fstat(): %s.", strerror(errno));
  122. if (S_ISCHR(sb.st_mode)) {
  123. if (ioctl(fd, DIOCGMEDIASIZE, &mediasize) == -1) {
  124. g_gate_xlog("Can't get media size: %s.",
  125. strerror(errno));
  126. }
  127. } else if (S_ISREG(sb.st_mode)) {
  128. mediasize = sb.st_size;
  129. } else {
  130. g_gate_xlog("Unsupported file system object.");
  131. }
  132. return (mediasize);
  133. }
  134. unsigned
  135. g_gate_sectorsize(int fd)
  136. {
  137. unsigned secsize;
  138. struct stat sb;
  139. if (fstat(fd, &sb) == -1)
  140. g_gate_xlog("fstat(): %s.", strerror(errno));
  141. if (S_ISCHR(sb.st_mode)) {
  142. if (ioctl(fd, DIOCGSECTORSIZE, &secsize) == -1) {
  143. g_gate_xlog("Can't get sector size: %s.",
  144. strerror(errno));
  145. }
  146. } else if (S_ISREG(sb.st_mode)) {
  147. secsize = 512;
  148. } else {
  149. g_gate_xlog("Unsupported file system object.");
  150. }
  151. return (secsize);
  152. }
  153. void
  154. g_gate_open_device(void)
  155. {
  156. g_gate_devfd = open("/dev/" G_GATE_CTL_NAME, O_RDWR);
  157. if (g_gate_devfd == -1)
  158. err(EXIT_FAILURE, "open(/dev/%s)", G_GATE_CTL_NAME);
  159. }
  160. void
  161. g_gate_close_device(void)
  162. {
  163. close(g_gate_devfd);
  164. }
  165. void
  166. g_gate_ioctl(unsigned long req, void *data)
  167. {
  168. if (ioctl(g_gate_devfd, req, data) == -1) {
  169. g_gate_xlog("%s: ioctl(/dev/%s): %s.", getprogname(),
  170. G_GATE_CTL_NAME, strerror(errno));
  171. }
  172. }
  173. void
  174. g_gate_destroy(int unit, int force)
  175. {
  176. struct g_gate_ctl_destroy ggio;
  177. ggio.gctl_version = G_GATE_VERSION;
  178. ggio.gctl_unit = unit;
  179. ggio.gctl_force = force;
  180. g_gate_ioctl(G_GATE_CMD_DESTROY, &ggio);
  181. }
  182. void
  183. g_gate_load_module(void)
  184. {
  185. if (modfind("g_gate") == -1) {
  186. /* Not present in kernel, try loading it. */
  187. if (kldload("geom_gate") == -1 || modfind("g_gate") == -1) {
  188. if (errno != EEXIST) {
  189. errx(EXIT_FAILURE,
  190. "geom_gate module not available!");
  191. }
  192. }
  193. }
  194. }
  195. /*
  196. * When we send from ggatec packets larger than 32kB, performance drops
  197. * significantly (eg. to 256kB/s over 1Gbit/s link). This is not a problem
  198. * when data is send from ggated. I don't know why, so for now I limit
  199. * size of packets send from ggatec to 32kB by defining MAX_SEND_SIZE
  200. * in ggatec Makefile.
  201. */
  202. #ifndef MAX_SEND_SIZE
  203. #define MAX_SEND_SIZE MAXPHYS
  204. #endif
  205. ssize_t
  206. g_gate_send(int s, const void *buf, size_t len, int flags)
  207. {
  208. ssize_t done = 0, done2;
  209. const unsigned char *p = buf;
  210. while (len > 0) {
  211. done2 = send(s, p, MIN(len, MAX_SEND_SIZE), flags);
  212. if (done2 == 0)
  213. break;
  214. else if (done2 == -1) {
  215. if (errno == EAGAIN) {
  216. printf("%s: EAGAIN\n", __func__);
  217. continue;
  218. }
  219. done = -1;
  220. break;
  221. }
  222. done += done2;
  223. p += done2;
  224. len -= done2;
  225. }
  226. return (done);
  227. }
  228. ssize_t
  229. g_gate_recv(int s, void *buf, size_t len, int flags)
  230. {
  231. ssize_t done;
  232. do {
  233. done = recv(s, buf, len, flags);
  234. } while (done == -1 && errno == EAGAIN);
  235. return (done);
  236. }
  237. int nagle = 1;
  238. unsigned rcvbuf = G_GATE_RCVBUF;
  239. unsigned sndbuf = G_GATE_SNDBUF;
  240. void
  241. g_gate_socket_settings(int sfd)
  242. {
  243. struct timeval tv;
  244. int bsize, on;
  245. /* Socket settings. */
  246. on = 1;
  247. if (nagle) {
  248. if (setsockopt(sfd, IPPROTO_TCP, TCP_NODELAY, &on,
  249. sizeof(on)) == -1) {
  250. g_gate_xlog("setsockopt() error: %s.", strerror(errno));
  251. }
  252. }
  253. if (setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1)
  254. g_gate_xlog("setsockopt(SO_REUSEADDR): %s.", strerror(errno));
  255. bsize = rcvbuf;
  256. if (setsockopt(sfd, SOL_SOCKET, SO_RCVBUF, &bsize, sizeof(bsize)) == -1)
  257. g_gate_xlog("setsockopt(SO_RCVBUF): %s.", strerror(errno));
  258. bsize = sndbuf;
  259. if (setsockopt(sfd, SOL_SOCKET, SO_SNDBUF, &bsize, sizeof(bsize)) == -1)
  260. g_gate_xlog("setsockopt(SO_SNDBUF): %s.", strerror(errno));
  261. tv.tv_sec = 8;
  262. tv.tv_usec = 0;
  263. if (setsockopt(sfd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) == -1) {
  264. g_gate_log(LOG_ERR, "setsockopt(SO_SNDTIMEO) error: %s.",
  265. strerror(errno));
  266. }
  267. if (setsockopt(sfd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) == -1) {
  268. g_gate_log(LOG_ERR, "setsockopt(SO_RCVTIMEO) error: %s.",
  269. strerror(errno));
  270. }
  271. }
  272. #ifdef LIBGEOM
  273. static struct gclass *
  274. find_class(struct gmesh *mesh, const char *name)
  275. {
  276. struct gclass *class;
  277. LIST_FOREACH(class, &mesh->lg_class, lg_class) {
  278. if (strcmp(class->lg_name, name) == 0)
  279. return (class);
  280. }
  281. return (NULL);
  282. }
  283. static const char *
  284. get_conf(struct ggeom *gp, const char *name)
  285. {
  286. struct gconfig *conf;
  287. LIST_FOREACH(conf, &gp->lg_config, lg_config) {
  288. if (strcmp(conf->lg_name, name) == 0)
  289. return (conf->lg_val);
  290. }
  291. return (NULL);
  292. }
  293. static void
  294. show_config(struct ggeom *gp, int verbose)
  295. {
  296. struct gprovider *pp;
  297. char buf[5];
  298. pp = LIST_FIRST(&gp->lg_provider);
  299. if (pp == NULL)
  300. return;
  301. if (!verbose) {
  302. printf("%s\n", pp->lg_name);
  303. return;
  304. }
  305. printf(" NAME: %s\n", pp->lg_name);
  306. printf(" info: %s\n", get_conf(gp, "info"));
  307. printf(" access: %s\n", get_conf(gp, "access"));
  308. printf(" timeout: %s\n", get_conf(gp, "timeout"));
  309. printf("queue_count: %s\n", get_conf(gp, "queue_count"));
  310. printf(" queue_size: %s\n", get_conf(gp, "queue_size"));
  311. printf(" references: %s\n", get_conf(gp, "ref"));
  312. humanize_number(buf, sizeof(buf), (int64_t)pp->lg_mediasize, "",
  313. HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
  314. printf(" mediasize: %jd (%s)\n", (intmax_t)pp->lg_mediasize, buf);
  315. printf(" sectorsize: %u\n", pp->lg_sectorsize);
  316. printf(" mode: %s\n", pp->lg_mode);
  317. printf("\n");
  318. }
  319. void
  320. g_gate_list(int unit, int verbose)
  321. {
  322. struct gmesh mesh;
  323. struct gclass *class;
  324. struct ggeom *gp;
  325. char name[64];
  326. int error;
  327. error = geom_gettree(&mesh);
  328. if (error != 0)
  329. exit(EXIT_FAILURE);
  330. class = find_class(&mesh, G_GATE_CLASS_NAME);
  331. if (class == NULL) {
  332. geom_deletetree(&mesh);
  333. exit(EXIT_SUCCESS);
  334. }
  335. if (unit >= 0) {
  336. snprintf(name, sizeof(name), "%s%d", G_GATE_PROVIDER_NAME,
  337. unit);
  338. }
  339. LIST_FOREACH(gp, &class->lg_geom, lg_geom) {
  340. if (unit != -1 && strcmp(gp->lg_name, name) != 0)
  341. continue;
  342. show_config(gp, verbose);
  343. }
  344. geom_deletetree(&mesh);
  345. exit(EXIT_SUCCESS);
  346. }
  347. #endif /* LIBGEOM */
  348. in_addr_t
  349. g_gate_str2ip(const char *str)
  350. {
  351. struct hostent *hp;
  352. in_addr_t ip;
  353. ip = inet_addr(str);
  354. if (ip != INADDR_NONE) {
  355. /* It is a valid IP address. */
  356. return (ip);
  357. }
  358. /* Check if it is a valid host name. */
  359. hp = gethostbyname(str);
  360. if (hp == NULL)
  361. return (INADDR_NONE);
  362. return (((struct in_addr *)(void *)hp->h_addr)->s_addr);
  363. }