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.
 
 
 
 

416 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 = 0;
  241. unsigned sndbuf = 0;
  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. if (rcvbuf != 0) {
  258. bsize = rcvbuf;
  259. if (setsockopt(sfd, SOL_SOCKET, SO_RCVBUF, &bsize, sizeof(bsize)) == -1)
  260. g_gate_xlog("setsockopt(SO_RCVBUF): %s.", strerror(errno));
  261. }
  262. if (sndbuf != 0) {
  263. bsize = sndbuf;
  264. if (setsockopt(sfd, SOL_SOCKET, SO_SNDBUF, &bsize, sizeof(bsize)) == -1)
  265. g_gate_xlog("setsockopt(SO_SNDBUF): %s.", strerror(errno));
  266. }
  267. tv.tv_sec = 8;
  268. tv.tv_usec = 0;
  269. if (setsockopt(sfd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) == -1) {
  270. g_gate_log(LOG_ERR, "setsockopt(SO_SNDTIMEO) error: %s.",
  271. strerror(errno));
  272. }
  273. if (setsockopt(sfd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) == -1) {
  274. g_gate_log(LOG_ERR, "setsockopt(SO_RCVTIMEO) error: %s.",
  275. strerror(errno));
  276. }
  277. }
  278. #ifdef LIBGEOM
  279. static struct gclass *
  280. find_class(struct gmesh *mesh, const char *name)
  281. {
  282. struct gclass *class;
  283. LIST_FOREACH(class, &mesh->lg_class, lg_class) {
  284. if (strcmp(class->lg_name, name) == 0)
  285. return (class);
  286. }
  287. return (NULL);
  288. }
  289. static const char *
  290. get_conf(struct ggeom *gp, const char *name)
  291. {
  292. struct gconfig *conf;
  293. LIST_FOREACH(conf, &gp->lg_config, lg_config) {
  294. if (strcmp(conf->lg_name, name) == 0)
  295. return (conf->lg_val);
  296. }
  297. return (NULL);
  298. }
  299. static void
  300. show_config(struct ggeom *gp, int verbose)
  301. {
  302. struct gprovider *pp;
  303. char buf[5];
  304. pp = LIST_FIRST(&gp->lg_provider);
  305. if (pp == NULL)
  306. return;
  307. if (!verbose) {
  308. printf("%s\n", pp->lg_name);
  309. return;
  310. }
  311. printf(" NAME: %s\n", pp->lg_name);
  312. printf(" info: %s\n", get_conf(gp, "info"));
  313. printf(" access: %s\n", get_conf(gp, "access"));
  314. printf(" timeout: %s\n", get_conf(gp, "timeout"));
  315. printf("queue_count: %s\n", get_conf(gp, "queue_count"));
  316. printf(" queue_size: %s\n", get_conf(gp, "queue_size"));
  317. printf(" references: %s\n", get_conf(gp, "ref"));
  318. humanize_number(buf, sizeof(buf), (int64_t)pp->lg_mediasize, "",
  319. HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
  320. printf(" mediasize: %jd (%s)\n", (intmax_t)pp->lg_mediasize, buf);
  321. printf(" sectorsize: %u\n", pp->lg_sectorsize);
  322. printf(" mode: %s\n", pp->lg_mode);
  323. printf("\n");
  324. }
  325. void
  326. g_gate_list(int unit, int verbose)
  327. {
  328. struct gmesh mesh;
  329. struct gclass *class;
  330. struct ggeom *gp;
  331. char name[64];
  332. int error;
  333. error = geom_gettree(&mesh);
  334. if (error != 0)
  335. exit(EXIT_FAILURE);
  336. class = find_class(&mesh, G_GATE_CLASS_NAME);
  337. if (class == NULL) {
  338. geom_deletetree(&mesh);
  339. exit(EXIT_SUCCESS);
  340. }
  341. if (unit >= 0) {
  342. snprintf(name, sizeof(name), "%s%d", G_GATE_PROVIDER_NAME,
  343. unit);
  344. }
  345. LIST_FOREACH(gp, &class->lg_geom, lg_geom) {
  346. if (unit != -1 && strcmp(gp->lg_name, name) != 0)
  347. continue;
  348. show_config(gp, verbose);
  349. }
  350. geom_deletetree(&mesh);
  351. exit(EXIT_SUCCESS);
  352. }
  353. #endif /* LIBGEOM */
  354. in_addr_t
  355. g_gate_str2ip(const char *str)
  356. {
  357. struct hostent *hp;
  358. in_addr_t ip;
  359. ip = inet_addr(str);
  360. if (ip != INADDR_NONE) {
  361. /* It is a valid IP address. */
  362. return (ip);
  363. }
  364. /* Check if it is a valid host name. */
  365. hp = gethostbyname(str);
  366. if (hp == NULL)
  367. return (INADDR_NONE);
  368. return (((struct in_addr *)(void *)hp->h_addr)->s_addr);
  369. }