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.
 
 
 
 

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