2 * Copyright (c) 2003, 2004 Silicon Graphics International Corp.
3 * Copyright (c) 1997-2007 Kenneth D. Merry
4 * Copyright (c) 2012 The FreeBSD Foundation
7 * Portions of this software were developed by Edward Tomasz Napierala
8 * under sponsorship from the FreeBSD Foundation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions, and the following disclaimer,
15 * without modification.
16 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
17 * substantially similar to the "NO WARRANTY" disclaimer below
18 * ("Disclaimer") and any redistribution must be conditioned upon
19 * including a substantially similar Disclaimer requirement for further
20 * binary redistribution.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
32 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGES.
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include <sys/ioctl.h>
41 #include <sys/types.h>
43 #include <sys/param.h>
44 #include <sys/linker.h>
45 #include <sys/queue.h>
46 #include <sys/callout.h>
48 #include <sys/capsicum.h>
59 #include <cam/scsi/scsi_all.h>
60 #include <cam/scsi/scsi_message.h>
61 #include <cam/ctl/ctl.h>
62 #include <cam/ctl/ctl_io.h>
63 #include <cam/ctl/ctl_backend.h>
64 #include <cam/ctl/ctl_ioctl.h>
65 #include <cam/ctl/ctl_util.h>
66 #include <cam/ctl/ctl_scsi_all.h>
70 #ifdef ICL_KERNEL_PROXY
74 extern bool proxy_mode;
76 static int ctl_fd = 0;
81 int retval, saved_errno;
83 ctl_fd = open(CTL_DEFAULT_DEV, O_RDWR);
84 if (ctl_fd < 0 && errno == ENOENT) {
86 retval = kldload("ctl");
88 ctl_fd = open(CTL_DEFAULT_DEV, O_RDWR);
93 log_err(1, "failed to open %s", CTL_DEFAULT_DEV);
97 * Name/value pair used for per-LUN attributes.
102 STAILQ_ENTRY(cctl_lun_nv) links;
106 * Backend LUN information.
112 uint64_t size_blocks;
117 STAILQ_HEAD(,cctl_lun_nv) attr_list;
118 STAILQ_ENTRY(cctl_lun) links;
128 char *cfiscsi_target;
129 uint16_t cfiscsi_portal_group_tag;
130 char *ctld_portal_group_name;
131 STAILQ_HEAD(,cctl_lun_nv) attr_list;
132 STAILQ_ENTRY(cctl_port) links;
135 struct cctl_devlist_data {
137 STAILQ_HEAD(,cctl_lun) lun_list;
138 struct cctl_lun *cur_lun;
140 STAILQ_HEAD(,cctl_port) port_list;
141 struct cctl_port *cur_port;
143 struct sbuf *cur_sb[32];
147 cctl_start_element(void *user_data, const char *name, const char **attr)
150 struct cctl_devlist_data *devlist;
151 struct cctl_lun *cur_lun;
153 devlist = (struct cctl_devlist_data *)user_data;
154 cur_lun = devlist->cur_lun;
156 if ((u_int)devlist->level >= (sizeof(devlist->cur_sb) /
157 sizeof(devlist->cur_sb[0])))
158 log_errx(1, "%s: too many nesting levels, %zd max", __func__,
159 sizeof(devlist->cur_sb) / sizeof(devlist->cur_sb[0]));
161 devlist->cur_sb[devlist->level] = sbuf_new_auto();
162 if (devlist->cur_sb[devlist->level] == NULL)
163 log_err(1, "%s: unable to allocate sbuf", __func__);
165 if (strcmp(name, "lun") == 0) {
167 log_errx(1, "%s: improper lun element nesting",
170 cur_lun = calloc(1, sizeof(*cur_lun));
172 log_err(1, "%s: cannot allocate %zd bytes", __func__,
176 devlist->cur_lun = cur_lun;
178 STAILQ_INIT(&cur_lun->attr_list);
179 STAILQ_INSERT_TAIL(&devlist->lun_list, cur_lun, links);
181 for (i = 0; attr[i] != NULL; i += 2) {
182 if (strcmp(attr[i], "id") == 0) {
183 cur_lun->lun_id = strtoull(attr[i+1], NULL, 0);
185 log_errx(1, "%s: invalid LUN attribute %s = %s",
186 __func__, attr[i], attr[i+1]);
193 cctl_end_element(void *user_data, const char *name)
195 struct cctl_devlist_data *devlist;
196 struct cctl_lun *cur_lun;
199 devlist = (struct cctl_devlist_data *)user_data;
200 cur_lun = devlist->cur_lun;
202 if ((cur_lun == NULL)
203 && (strcmp(name, "ctllunlist") != 0))
204 log_errx(1, "%s: cur_lun == NULL! (name = %s)", __func__, name);
206 if (devlist->cur_sb[devlist->level] == NULL)
207 log_errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
208 devlist->level, name);
210 sbuf_finish(devlist->cur_sb[devlist->level]);
211 str = checked_strdup(sbuf_data(devlist->cur_sb[devlist->level]));
213 if (strlen(str) == 0) {
218 sbuf_delete(devlist->cur_sb[devlist->level]);
219 devlist->cur_sb[devlist->level] = NULL;
222 if (strcmp(name, "backend_type") == 0) {
223 cur_lun->backend_type = str;
225 } else if (strcmp(name, "lun_type") == 0) {
226 cur_lun->device_type = strtoull(str, NULL, 0);
227 } else if (strcmp(name, "size") == 0) {
228 cur_lun->size_blocks = strtoull(str, NULL, 0);
229 } else if (strcmp(name, "blocksize") == 0) {
230 cur_lun->blocksize = strtoul(str, NULL, 0);
231 } else if (strcmp(name, "serial_number") == 0) {
232 cur_lun->serial_number = str;
234 } else if (strcmp(name, "device_id") == 0) {
235 cur_lun->device_id = str;
237 } else if (strcmp(name, "ctld_name") == 0) {
238 cur_lun->ctld_name = str;
240 } else if (strcmp(name, "lun") == 0) {
241 devlist->cur_lun = NULL;
242 } else if (strcmp(name, "ctllunlist") == 0) {
245 struct cctl_lun_nv *nv;
247 nv = calloc(1, sizeof(*nv));
249 log_err(1, "%s: can't allocate %zd bytes for nv pair",
250 __func__, sizeof(*nv));
252 nv->name = checked_strdup(name);
256 STAILQ_INSERT_TAIL(&cur_lun->attr_list, nv, links);
263 cctl_start_pelement(void *user_data, const char *name, const char **attr)
266 struct cctl_devlist_data *devlist;
267 struct cctl_port *cur_port;
269 devlist = (struct cctl_devlist_data *)user_data;
270 cur_port = devlist->cur_port;
272 if ((u_int)devlist->level >= (sizeof(devlist->cur_sb) /
273 sizeof(devlist->cur_sb[0])))
274 log_errx(1, "%s: too many nesting levels, %zd max", __func__,
275 sizeof(devlist->cur_sb) / sizeof(devlist->cur_sb[0]));
277 devlist->cur_sb[devlist->level] = sbuf_new_auto();
278 if (devlist->cur_sb[devlist->level] == NULL)
279 log_err(1, "%s: unable to allocate sbuf", __func__);
281 if (strcmp(name, "targ_port") == 0) {
282 if (cur_port != NULL)
283 log_errx(1, "%s: improper port element nesting (%s)",
286 cur_port = calloc(1, sizeof(*cur_port));
287 if (cur_port == NULL)
288 log_err(1, "%s: cannot allocate %zd bytes", __func__,
291 devlist->num_ports++;
292 devlist->cur_port = cur_port;
294 STAILQ_INIT(&cur_port->attr_list);
295 STAILQ_INSERT_TAIL(&devlist->port_list, cur_port, links);
297 for (i = 0; attr[i] != NULL; i += 2) {
298 if (strcmp(attr[i], "id") == 0) {
299 cur_port->port_id = strtoul(attr[i+1], NULL, 0);
301 log_errx(1, "%s: invalid LUN attribute %s = %s",
302 __func__, attr[i], attr[i+1]);
309 cctl_end_pelement(void *user_data, const char *name)
311 struct cctl_devlist_data *devlist;
312 struct cctl_port *cur_port;
315 devlist = (struct cctl_devlist_data *)user_data;
316 cur_port = devlist->cur_port;
318 if ((cur_port == NULL)
319 && (strcmp(name, "ctlportlist") != 0))
320 log_errx(1, "%s: cur_port == NULL! (name = %s)", __func__, name);
322 if (devlist->cur_sb[devlist->level] == NULL)
323 log_errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
324 devlist->level, name);
326 sbuf_finish(devlist->cur_sb[devlist->level]);
327 str = checked_strdup(sbuf_data(devlist->cur_sb[devlist->level]));
329 if (strlen(str) == 0) {
334 sbuf_delete(devlist->cur_sb[devlist->level]);
335 devlist->cur_sb[devlist->level] = NULL;
338 if (strcmp(name, "frontend_type") == 0) {
339 cur_port->port_frontend = str;
341 } else if (strcmp(name, "port_name") == 0) {
342 cur_port->port_name = str;
344 } else if (strcmp(name, "physical_port") == 0) {
345 cur_port->pp = strtoul(str, NULL, 0);
346 } else if (strcmp(name, "virtual_port") == 0) {
347 cur_port->vp = strtoul(str, NULL, 0);
348 } else if (strcmp(name, "cfiscsi_target") == 0) {
349 cur_port->cfiscsi_target = str;
351 } else if (strcmp(name, "cfiscsi_state") == 0) {
352 cur_port->cfiscsi_state = strtoul(str, NULL, 0);
353 } else if (strcmp(name, "cfiscsi_portal_group_tag") == 0) {
354 cur_port->cfiscsi_portal_group_tag = strtoul(str, NULL, 0);
355 } else if (strcmp(name, "ctld_portal_group_name") == 0) {
356 cur_port->ctld_portal_group_name = str;
358 } else if (strcmp(name, "targ_port") == 0) {
359 devlist->cur_port = NULL;
360 } else if (strcmp(name, "ctlportlist") == 0) {
363 struct cctl_lun_nv *nv;
365 nv = calloc(1, sizeof(*nv));
367 log_err(1, "%s: can't allocate %zd bytes for nv pair",
368 __func__, sizeof(*nv));
370 nv->name = checked_strdup(name);
374 STAILQ_INSERT_TAIL(&cur_port->attr_list, nv, links);
381 cctl_char_handler(void *user_data, const XML_Char *str, int len)
383 struct cctl_devlist_data *devlist;
385 devlist = (struct cctl_devlist_data *)user_data;
387 sbuf_bcat(devlist->cur_sb[devlist->level], str, len);
391 conf_new_from_kernel(void)
393 struct conf *conf = NULL;
395 struct portal_group *pg;
400 struct ctl_lun_list list;
401 struct cctl_devlist_data devlist;
402 struct cctl_lun *lun;
403 struct cctl_port *port;
408 bzero(&devlist, sizeof(devlist));
409 STAILQ_INIT(&devlist.lun_list);
410 STAILQ_INIT(&devlist.port_list);
412 log_debugx("obtaining previously configured CTL luns from the kernel");
417 str = realloc(str, len);
419 log_err(1, "realloc");
421 bzero(&list, sizeof(list));
422 list.alloc_len = len;
423 list.status = CTL_LUN_LIST_NONE;
426 if (ioctl(ctl_fd, CTL_LUN_LIST, &list) == -1) {
427 log_warn("error issuing CTL_LUN_LIST ioctl");
432 if (list.status == CTL_LUN_LIST_ERROR) {
433 log_warnx("error returned from CTL_LUN_LIST ioctl: %s",
439 if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) {
444 parser = XML_ParserCreate(NULL);
445 if (parser == NULL) {
446 log_warnx("unable to create XML parser");
451 XML_SetUserData(parser, &devlist);
452 XML_SetElementHandler(parser, cctl_start_element, cctl_end_element);
453 XML_SetCharacterDataHandler(parser, cctl_char_handler);
455 retval = XML_Parse(parser, str, strlen(str), 1);
456 XML_ParserFree(parser);
459 log_warnx("XML_Parse failed");
466 str = realloc(str, len);
468 log_err(1, "realloc");
470 bzero(&list, sizeof(list));
471 list.alloc_len = len;
472 list.status = CTL_LUN_LIST_NONE;
475 if (ioctl(ctl_fd, CTL_PORT_LIST, &list) == -1) {
476 log_warn("error issuing CTL_PORT_LIST ioctl");
481 if (list.status == CTL_LUN_LIST_ERROR) {
482 log_warnx("error returned from CTL_PORT_LIST ioctl: %s",
488 if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) {
493 parser = XML_ParserCreate(NULL);
494 if (parser == NULL) {
495 log_warnx("unable to create XML parser");
500 XML_SetUserData(parser, &devlist);
501 XML_SetElementHandler(parser, cctl_start_pelement, cctl_end_pelement);
502 XML_SetCharacterDataHandler(parser, cctl_char_handler);
504 retval = XML_Parse(parser, str, strlen(str), 1);
505 XML_ParserFree(parser);
508 log_warnx("XML_Parse failed");
515 STAILQ_FOREACH(port, &devlist.port_list, links) {
516 if (strcmp(port->port_frontend, "ha") == 0)
519 if (port->pp == 0 && port->vp == 0) {
520 name = checked_strdup(port->port_name);
521 } else if (port->vp == 0) {
522 retval = asprintf(&name, "%s/%d",
523 port->port_name, port->pp);
525 log_err(1, "asprintf");
527 retval = asprintf(&name, "%s/%d/%d",
528 port->port_name, port->pp, port->vp);
530 log_err(1, "asprintf");
533 if (port->cfiscsi_target == NULL) {
534 log_debugx("CTL port %u \"%s\" wasn't managed by ctld; ",
535 port->port_id, name);
536 pp = pport_find(conf, name);
539 log_debugx("found new kernel port %u \"%s\"",
540 port->port_id, name);
542 pp = pport_new(conf, name, port->port_id);
544 log_warnx("pport_new failed");
550 if (port->cfiscsi_state != 1) {
551 log_debugx("CTL port %ju is not active (%d); ignoring",
552 (uintmax_t)port->port_id, port->cfiscsi_state);
556 targ = target_find(conf, port->cfiscsi_target);
559 log_debugx("found new kernel target %s for CTL port %ld",
560 port->cfiscsi_target, port->port_id);
562 targ = target_new(conf, port->cfiscsi_target);
564 log_warnx("target_new failed");
569 if (port->ctld_portal_group_name == NULL)
571 pg = portal_group_find(conf, port->ctld_portal_group_name);
574 log_debugx("found new kernel portal group %s for CTL port %ld",
575 port->ctld_portal_group_name, port->port_id);
577 pg = portal_group_new(conf, port->ctld_portal_group_name);
579 log_warnx("portal_group_new failed");
583 pg->pg_tag = port->cfiscsi_portal_group_tag;
584 cp = port_new(conf, targ, pg);
586 log_warnx("port_new failed");
589 cp->p_ctl_port = port->port_id;
593 STAILQ_FOREACH(lun, &devlist.lun_list, links) {
594 struct cctl_lun_nv *nv;
596 if (lun->ctld_name == NULL) {
597 log_debugx("CTL lun %ju wasn't managed by ctld; "
598 "ignoring", (uintmax_t)lun->lun_id);
602 cl = lun_find(conf, lun->ctld_name);
604 log_warnx("found CTL lun %ju \"%s\", "
605 "also backed by CTL lun %d; ignoring",
606 (uintmax_t)lun->lun_id, lun->ctld_name,
611 log_debugx("found CTL lun %ju \"%s\"",
612 (uintmax_t)lun->lun_id, lun->ctld_name);
614 cl = lun_new(conf, lun->ctld_name);
616 log_warnx("lun_new failed");
619 lun_set_backend(cl, lun->backend_type);
620 lun_set_device_type(cl, lun->device_type);
621 lun_set_blocksize(cl, lun->blocksize);
622 lun_set_device_id(cl, lun->device_id);
623 lun_set_serial(cl, lun->serial_number);
624 lun_set_size(cl, lun->size_blocks * cl->l_blocksize);
625 lun_set_ctl_lun(cl, lun->lun_id);
627 STAILQ_FOREACH(nv, &lun->attr_list, links) {
628 if (strcmp(nv->name, "file") == 0 ||
629 strcmp(nv->name, "dev") == 0) {
630 lun_set_path(cl, nv->value);
633 o = option_new(&cl->l_options, nv->name, nv->value);
635 log_warnx("unable to add CTL lun option %s "
636 "for CTL lun %ju \"%s\"",
637 nv->name, (uintmax_t) lun->lun_id,
646 str_arg(struct ctl_be_arg *arg, const char *name, const char *value)
649 arg->namelen = strlen(name) + 1;
650 arg->name = __DECONST(char *, name);
651 arg->vallen = strlen(value) + 1;
652 arg->value = __DECONST(char *, value);
653 arg->flags = CTL_BEARG_ASCII | CTL_BEARG_RD;
657 kernel_lun_add(struct lun *lun)
660 struct ctl_lun_req req;
661 int error, i, num_options;
663 bzero(&req, sizeof(req));
665 strlcpy(req.backend, lun->l_backend, sizeof(req.backend));
666 req.reqtype = CTL_LUNREQ_CREATE;
668 req.reqdata.create.blocksize_bytes = lun->l_blocksize;
670 if (lun->l_size != 0)
671 req.reqdata.create.lun_size_bytes = lun->l_size;
673 if (lun->l_ctl_lun >= 0) {
674 req.reqdata.create.req_lun_id = lun->l_ctl_lun;
675 req.reqdata.create.flags |= CTL_LUN_FLAG_ID_REQ;
678 req.reqdata.create.flags |= CTL_LUN_FLAG_DEV_TYPE;
679 req.reqdata.create.device_type = lun->l_device_type;
681 if (lun->l_serial != NULL) {
682 strncpy(req.reqdata.create.serial_num, lun->l_serial,
683 sizeof(req.reqdata.create.serial_num));
684 req.reqdata.create.flags |= CTL_LUN_FLAG_SERIAL_NUM;
687 if (lun->l_device_id != NULL) {
688 strncpy(req.reqdata.create.device_id, lun->l_device_id,
689 sizeof(req.reqdata.create.device_id));
690 req.reqdata.create.flags |= CTL_LUN_FLAG_DEVID;
693 if (lun->l_path != NULL) {
694 o = option_find(&lun->l_options, "file");
696 option_set(o, lun->l_path);
698 o = option_new(&lun->l_options, "file", lun->l_path);
703 o = option_find(&lun->l_options, "ctld_name");
705 option_set(o, lun->l_name);
707 o = option_new(&lun->l_options, "ctld_name", lun->l_name);
711 o = option_find(&lun->l_options, "scsiname");
712 if (o == NULL && lun->l_scsiname != NULL) {
713 o = option_new(&lun->l_options, "scsiname", lun->l_scsiname);
718 TAILQ_FOREACH(o, &lun->l_options, o_next)
721 req.num_be_args = num_options;
722 if (num_options > 0) {
723 req.be_args = malloc(num_options * sizeof(*req.be_args));
724 if (req.be_args == NULL) {
725 log_warn("error allocating %zd bytes",
726 num_options * sizeof(*req.be_args));
731 TAILQ_FOREACH(o, &lun->l_options, o_next) {
732 str_arg(&req.be_args[i], o->o_name, o->o_value);
735 assert(i == num_options);
738 error = ioctl(ctl_fd, CTL_LUN_REQ, &req);
741 log_warn("error issuing CTL_LUN_REQ ioctl");
745 switch (req.status) {
747 log_warnx("LUN creation error: %s", req.error_str);
749 case CTL_LUN_WARNING:
750 log_warnx("LUN creation warning: %s", req.error_str);
755 log_warnx("unknown LUN creation status: %d",
760 lun_set_ctl_lun(lun, req.reqdata.create.req_lun_id);
765 kernel_lun_modify(struct lun *lun)
768 struct ctl_lun_req req;
769 int error, i, num_options;
771 bzero(&req, sizeof(req));
773 strlcpy(req.backend, lun->l_backend, sizeof(req.backend));
774 req.reqtype = CTL_LUNREQ_MODIFY;
776 req.reqdata.modify.lun_id = lun->l_ctl_lun;
777 req.reqdata.modify.lun_size_bytes = lun->l_size;
780 TAILQ_FOREACH(o, &lun->l_options, o_next)
783 req.num_be_args = num_options;
784 if (num_options > 0) {
785 req.be_args = malloc(num_options * sizeof(*req.be_args));
786 if (req.be_args == NULL) {
787 log_warn("error allocating %zd bytes",
788 num_options * sizeof(*req.be_args));
793 TAILQ_FOREACH(o, &lun->l_options, o_next) {
794 str_arg(&req.be_args[i], o->o_name, o->o_value);
797 assert(i == num_options);
800 error = ioctl(ctl_fd, CTL_LUN_REQ, &req);
803 log_warn("error issuing CTL_LUN_REQ ioctl");
807 switch (req.status) {
809 log_warnx("LUN modification error: %s", req.error_str);
811 case CTL_LUN_WARNING:
812 log_warnx("LUN modification warning: %s", req.error_str);
817 log_warnx("unknown LUN modification status: %d",
826 kernel_lun_remove(struct lun *lun)
828 struct ctl_lun_req req;
830 bzero(&req, sizeof(req));
832 strlcpy(req.backend, lun->l_backend, sizeof(req.backend));
833 req.reqtype = CTL_LUNREQ_RM;
835 req.reqdata.rm.lun_id = lun->l_ctl_lun;
837 if (ioctl(ctl_fd, CTL_LUN_REQ, &req) == -1) {
838 log_warn("error issuing CTL_LUN_REQ ioctl");
842 switch (req.status) {
844 log_warnx("LUN removal error: %s", req.error_str);
846 case CTL_LUN_WARNING:
847 log_warnx("LUN removal warning: %s", req.error_str);
852 log_warnx("unknown LUN removal status: %d", req.status);
860 kernel_handoff(struct connection *conn)
862 struct ctl_iscsi req;
864 bzero(&req, sizeof(req));
866 req.type = CTL_ISCSI_HANDOFF;
867 strlcpy(req.data.handoff.initiator_name,
868 conn->conn_initiator_name, sizeof(req.data.handoff.initiator_name));
869 strlcpy(req.data.handoff.initiator_addr,
870 conn->conn_initiator_addr, sizeof(req.data.handoff.initiator_addr));
871 if (conn->conn_initiator_alias != NULL) {
872 strlcpy(req.data.handoff.initiator_alias,
873 conn->conn_initiator_alias, sizeof(req.data.handoff.initiator_alias));
875 memcpy(req.data.handoff.initiator_isid, conn->conn_initiator_isid,
876 sizeof(req.data.handoff.initiator_isid));
877 strlcpy(req.data.handoff.target_name,
878 conn->conn_target->t_name, sizeof(req.data.handoff.target_name));
879 if (conn->conn_portal->p_portal_group->pg_offload != NULL) {
880 strlcpy(req.data.handoff.offload,
881 conn->conn_portal->p_portal_group->pg_offload,
882 sizeof(req.data.handoff.offload));
884 #ifdef ICL_KERNEL_PROXY
886 req.data.handoff.connection_id = conn->conn_socket;
888 req.data.handoff.socket = conn->conn_socket;
890 req.data.handoff.socket = conn->conn_socket;
892 req.data.handoff.portal_group_tag =
893 conn->conn_portal->p_portal_group->pg_tag;
894 if (conn->conn_header_digest == CONN_DIGEST_CRC32C)
895 req.data.handoff.header_digest = CTL_ISCSI_DIGEST_CRC32C;
896 if (conn->conn_data_digest == CONN_DIGEST_CRC32C)
897 req.data.handoff.data_digest = CTL_ISCSI_DIGEST_CRC32C;
898 req.data.handoff.cmdsn = conn->conn_cmdsn;
899 req.data.handoff.statsn = conn->conn_statsn;
900 req.data.handoff.max_recv_data_segment_length =
901 conn->conn_max_recv_data_segment_length;
902 req.data.handoff.max_send_data_segment_length =
903 conn->conn_max_send_data_segment_length;
904 req.data.handoff.max_burst_length = conn->conn_max_burst_length;
905 req.data.handoff.first_burst_length = conn->conn_first_burst_length;
906 req.data.handoff.immediate_data = conn->conn_immediate_data;
908 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
909 log_err(1, "error issuing CTL_ISCSI ioctl; "
910 "dropping connection");
913 if (req.status != CTL_ISCSI_OK) {
914 log_errx(1, "error returned from CTL iSCSI handoff request: "
915 "%s; dropping connection", req.error_str);
920 kernel_limits(const char *offload, int *max_recv_dsl, int *max_send_dsl,
921 int *max_burst_length, int *first_burst_length)
923 struct ctl_iscsi req;
924 struct ctl_iscsi_limits_params *cilp;
926 bzero(&req, sizeof(req));
928 req.type = CTL_ISCSI_LIMITS;
929 cilp = (struct ctl_iscsi_limits_params *)&(req.data.limits);
930 if (offload != NULL) {
931 strlcpy(cilp->offload, offload, sizeof(cilp->offload));
934 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
935 log_err(1, "error issuing CTL_ISCSI ioctl; "
936 "dropping connection");
939 if (req.status != CTL_ISCSI_OK) {
940 log_errx(1, "error returned from CTL iSCSI limits request: "
941 "%s; dropping connection", req.error_str);
944 if (cilp->max_recv_data_segment_length != 0) {
945 *max_recv_dsl = cilp->max_recv_data_segment_length;
946 *max_send_dsl = cilp->max_recv_data_segment_length;
948 if (cilp->max_send_data_segment_length != 0)
949 *max_send_dsl = cilp->max_send_data_segment_length;
950 if (cilp->max_burst_length != 0)
951 *max_burst_length = cilp->max_burst_length;
952 if (cilp->first_burst_length != 0)
953 *first_burst_length = cilp->first_burst_length;
954 if (*max_burst_length < *first_burst_length)
955 *first_burst_length = *max_burst_length;
957 if (offload != NULL) {
958 log_debugx("Kernel limits for offload \"%s\" are "
959 "MaxRecvDataSegment=%d, max_send_dsl=%d, "
960 "MaxBurstLength=%d, FirstBurstLength=%d",
961 offload, *max_recv_dsl, *max_send_dsl, *max_burst_length,
962 *first_burst_length);
964 log_debugx("Kernel limits are "
965 "MaxRecvDataSegment=%d, max_send_dsl=%d, "
966 "MaxBurstLength=%d, FirstBurstLength=%d",
967 *max_recv_dsl, *max_send_dsl, *max_burst_length,
968 *first_burst_length);
973 kernel_port_add(struct port *port)
976 struct ctl_port_entry entry;
978 struct ctl_lun_map lm;
979 struct target *targ = port->p_target;
980 struct portal_group *pg = port->p_portal_group;
984 /* Create iSCSI port. */
985 if (port->p_portal_group) {
986 bzero(&req, sizeof(req));
987 strlcpy(req.driver, "iscsi", sizeof(req.driver));
988 req.reqtype = CTL_REQ_CREATE;
990 TAILQ_FOREACH(o, &pg->pg_options, o_next)
992 req.args = malloc(req.num_args * sizeof(*req.args));
993 if (req.args == NULL)
994 log_err(1, "malloc");
996 req.args[n].namelen = sizeof("port_id");
997 req.args[n].name = __DECONST(char *, "port_id");
998 req.args[n].vallen = sizeof(port->p_ctl_port);
999 req.args[n].value = &port->p_ctl_port;
1000 req.args[n++].flags = CTL_BEARG_WR;
1001 str_arg(&req.args[n++], "cfiscsi_target", targ->t_name);
1002 snprintf(tagstr, sizeof(tagstr), "%d", pg->pg_tag);
1003 str_arg(&req.args[n++], "cfiscsi_portal_group_tag", tagstr);
1005 str_arg(&req.args[n++], "cfiscsi_target_alias", targ->t_alias);
1006 str_arg(&req.args[n++], "ctld_portal_group_name", pg->pg_name);
1007 TAILQ_FOREACH(o, &pg->pg_options, o_next)
1008 str_arg(&req.args[n++], o->o_name, o->o_value);
1010 error = ioctl(ctl_fd, CTL_PORT_REQ, &req);
1013 log_warn("error issuing CTL_PORT_REQ ioctl");
1016 if (req.status == CTL_LUN_ERROR) {
1017 log_warnx("error returned from port creation request: %s",
1021 if (req.status != CTL_LUN_OK) {
1022 log_warnx("unknown port creation request status %d",
1026 } else if (port->p_pport) {
1027 port->p_ctl_port = port->p_pport->pp_ctl_port;
1029 if (strncmp(targ->t_name, "naa.", 4) == 0 &&
1030 strlen(targ->t_name) == 20) {
1031 bzero(&entry, sizeof(entry));
1032 entry.port_type = CTL_PORT_NONE;
1033 entry.targ_port = port->p_ctl_port;
1034 entry.flags |= CTL_PORT_WWNN_VALID;
1035 entry.wwnn = strtoull(targ->t_name + 4, NULL, 16);
1036 if (ioctl(ctl_fd, CTL_SET_PORT_WWNS, &entry) == -1)
1037 log_warn("CTL_SET_PORT_WWNS ioctl failed");
1041 /* Explicitly enable mapping to block any access except allowed. */
1042 lm.port = port->p_ctl_port;
1043 lm.plun = UINT32_MAX;
1045 error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
1047 log_warn("CTL_LUN_MAP ioctl failed");
1049 /* Map configured LUNs */
1050 for (i = 0; i < MAX_LUNS; i++) {
1051 if (targ->t_luns[i] == NULL)
1053 lm.port = port->p_ctl_port;
1055 lm.lun = targ->t_luns[i]->l_ctl_lun;
1056 error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
1058 log_warn("CTL_LUN_MAP ioctl failed");
1062 bzero(&entry, sizeof(entry));
1063 entry.targ_port = port->p_ctl_port;
1064 error = ioctl(ctl_fd, CTL_ENABLE_PORT, &entry);
1066 log_warn("CTL_ENABLE_PORT ioctl failed");
1074 kernel_port_update(struct port *port, struct port *oport)
1076 struct ctl_lun_map lm;
1077 struct target *targ = port->p_target;
1078 struct target *otarg = oport->p_target;
1082 /* Map configured LUNs and unmap others */
1083 for (i = 0; i < MAX_LUNS; i++) {
1084 lm.port = port->p_ctl_port;
1086 if (targ->t_luns[i] == NULL)
1087 lm.lun = UINT32_MAX;
1089 lm.lun = targ->t_luns[i]->l_ctl_lun;
1090 if (otarg->t_luns[i] == NULL)
1093 olun = otarg->t_luns[i]->l_ctl_lun;
1096 error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
1098 log_warn("CTL_LUN_MAP ioctl failed");
1104 kernel_port_remove(struct port *port)
1106 struct ctl_port_entry entry;
1107 struct ctl_lun_map lm;
1110 struct target *targ = port->p_target;
1111 struct portal_group *pg = port->p_portal_group;
1115 bzero(&entry, sizeof(entry));
1116 entry.targ_port = port->p_ctl_port;
1117 error = ioctl(ctl_fd, CTL_DISABLE_PORT, &entry);
1119 log_warn("CTL_DISABLE_PORT ioctl failed");
1123 /* Remove iSCSI port. */
1124 if (port->p_portal_group) {
1125 bzero(&req, sizeof(req));
1126 strlcpy(req.driver, "iscsi", sizeof(req.driver));
1127 req.reqtype = CTL_REQ_REMOVE;
1129 req.args = malloc(req.num_args * sizeof(*req.args));
1130 if (req.args == NULL)
1131 log_err(1, "malloc");
1132 str_arg(&req.args[0], "cfiscsi_target", targ->t_name);
1133 snprintf(tagstr, sizeof(tagstr), "%d", pg->pg_tag);
1134 str_arg(&req.args[1], "cfiscsi_portal_group_tag", tagstr);
1135 error = ioctl(ctl_fd, CTL_PORT_REQ, &req);
1138 log_warn("error issuing CTL_PORT_REQ ioctl");
1141 if (req.status == CTL_LUN_ERROR) {
1142 log_warnx("error returned from port removal request: %s",
1146 if (req.status != CTL_LUN_OK) {
1147 log_warnx("unknown port removal request status %d",
1152 /* Disable LUN mapping. */
1153 lm.port = port->p_ctl_port;
1154 lm.plun = UINT32_MAX;
1155 lm.lun = UINT32_MAX;
1156 error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
1158 log_warn("CTL_LUN_MAP ioctl failed");
1163 #ifdef ICL_KERNEL_PROXY
1165 kernel_listen(struct addrinfo *ai, bool iser, int portal_id)
1167 struct ctl_iscsi req;
1169 bzero(&req, sizeof(req));
1171 req.type = CTL_ISCSI_LISTEN;
1172 req.data.listen.iser = iser;
1173 req.data.listen.domain = ai->ai_family;
1174 req.data.listen.socktype = ai->ai_socktype;
1175 req.data.listen.protocol = ai->ai_protocol;
1176 req.data.listen.addr = ai->ai_addr;
1177 req.data.listen.addrlen = ai->ai_addrlen;
1178 req.data.listen.portal_id = portal_id;
1180 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1)
1181 log_err(1, "error issuing CTL_ISCSI ioctl");
1183 if (req.status != CTL_ISCSI_OK) {
1184 log_errx(1, "error returned from CTL iSCSI listen: %s",
1190 kernel_accept(int *connection_id, int *portal_id,
1191 struct sockaddr *client_sa, socklen_t *client_salen)
1193 struct ctl_iscsi req;
1194 struct sockaddr_storage ss;
1196 bzero(&req, sizeof(req));
1198 req.type = CTL_ISCSI_ACCEPT;
1199 req.data.accept.initiator_addr = (struct sockaddr *)&ss;
1201 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1)
1202 log_err(1, "error issuing CTL_ISCSI ioctl");
1204 if (req.status != CTL_ISCSI_OK) {
1205 log_errx(1, "error returned from CTL iSCSI accept: %s",
1209 *connection_id = req.data.accept.connection_id;
1210 *portal_id = req.data.accept.portal_id;
1211 *client_salen = req.data.accept.initiator_addrlen;
1212 memcpy(client_sa, &ss, *client_salen);
1216 kernel_send(struct pdu *pdu)
1218 struct ctl_iscsi req;
1220 bzero(&req, sizeof(req));
1222 req.type = CTL_ISCSI_SEND;
1223 req.data.send.connection_id = pdu->pdu_connection->conn_socket;
1224 req.data.send.bhs = pdu->pdu_bhs;
1225 req.data.send.data_segment_len = pdu->pdu_data_len;
1226 req.data.send.data_segment = pdu->pdu_data;
1228 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
1229 log_err(1, "error issuing CTL_ISCSI ioctl; "
1230 "dropping connection");
1233 if (req.status != CTL_ISCSI_OK) {
1234 log_errx(1, "error returned from CTL iSCSI send: "
1235 "%s; dropping connection", req.error_str);
1240 kernel_receive(struct pdu *pdu)
1242 struct connection *conn;
1243 struct ctl_iscsi req;
1245 conn = pdu->pdu_connection;
1246 pdu->pdu_data = malloc(conn->conn_max_recv_data_segment_length);
1247 if (pdu->pdu_data == NULL)
1248 log_err(1, "malloc");
1250 bzero(&req, sizeof(req));
1252 req.type = CTL_ISCSI_RECEIVE;
1253 req.data.receive.connection_id = conn->conn_socket;
1254 req.data.receive.bhs = pdu->pdu_bhs;
1255 req.data.receive.data_segment_len =
1256 conn->conn_max_recv_data_segment_length;
1257 req.data.receive.data_segment = pdu->pdu_data;
1259 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
1260 log_err(1, "error issuing CTL_ISCSI ioctl; "
1261 "dropping connection");
1264 if (req.status != CTL_ISCSI_OK) {
1265 log_errx(1, "error returned from CTL iSCSI receive: "
1266 "%s; dropping connection", req.error_str);
1271 #endif /* ICL_KERNEL_PROXY */
1274 * XXX: I CANT INTO LATIN
1277 kernel_capsicate(void)
1280 cap_rights_t rights;
1281 const unsigned long cmds[] = { CTL_ISCSI };
1283 cap_rights_init(&rights, CAP_IOCTL);
1284 error = cap_rights_limit(ctl_fd, &rights);
1285 if (error != 0 && errno != ENOSYS)
1286 log_err(1, "cap_rights_limit");
1288 error = cap_ioctls_limit(ctl_fd, cmds,
1289 sizeof(cmds) / sizeof(cmds[0]));
1290 if (error != 0 && errno != ENOSYS)
1291 log_err(1, "cap_ioctls_limit");
1293 error = cap_enter();
1294 if (error != 0 && errno != ENOSYS)
1295 log_err(1, "cap_enter");
1297 if (cap_sandboxed())
1298 log_debugx("Capsicum capability mode enabled");
1300 log_warnx("Capsicum capability mode not supported");